if_fxp_cardbus.c revision 1.2
1/*	$OpenBSD: if_fxp_cardbus.c,v 1.2 2000/09/17 17:08:16 aaron Exp $ */
2/*	$NetBSD: if_fxp_cardbus.c,v 1.12 2000/05/08 18:23:36 thorpej Exp $	*/
3
4/*
5 * Copyright (c) 1999 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Johan Danielsson.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *	This product includes software developed by the NetBSD
22 *	Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 *    contributors may be used to endorse or promote products derived
25 *    from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40/*
41 * CardBus front-end for the Intel i82557 family of Ethernet chips.
42 */
43
44#include "bpfilter.h"
45
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/mbuf.h>
49#include <sys/socket.h>
50#include <sys/ioctl.h>
51#include <sys/errno.h>
52#include <sys/malloc.h>
53#include <sys/kernel.h>
54#include <sys/device.h>
55
56#include <net/if.h>
57#include <net/if_dl.h>
58#include <net/if_types.h>
59#include <net/if_media.h>
60
61#include <machine/endian.h>
62
63#if NBPFILTER > 0
64#include <net/bpf.h>
65#endif
66
67#ifdef INET
68#include <netinet/in.h>
69#include <netinet/in_systm.h>
70#include <netinet/in_var.h>
71#include <netinet/ip.h>
72#include <netinet/if_ether.h>
73#endif
74
75#include <machine/bus.h>
76#include <machine/intr.h>
77
78#include <dev/mii/miivar.h>
79
80#include <dev/ic/fxpreg.h>
81#include <dev/ic/fxpvar.h>
82
83#include <dev/pci/pcivar.h>
84#include <dev/pci/pcireg.h>
85#include <dev/pci/pcidevs.h>
86
87#include <dev/cardbus/cardbusvar.h>
88#include <dev/cardbus/cardbusdevs.h>
89
90int fxp_cardbus_match __P((struct device *, void *, void *));
91void fxp_cardbus_attach __P((struct device *, struct device *, void *));
92int fxp_cardbus_detach __P((struct device * self, int flags));
93void fxp_cardbus_setup __P((struct fxp_softc * sc));
94
95struct fxp_cardbus_softc {
96	struct fxp_softc sc;
97	cardbus_devfunc_t ct;
98	pcireg_t base0_reg;
99	pcireg_t base1_reg;
100	bus_size_t size;
101};
102
103struct cfattach fxp_cardbus_ca = {
104	sizeof(struct fxp_cardbus_softc), fxp_cardbus_match, fxp_cardbus_attach,
105	    fxp_cardbus_detach
106};
107
108#ifdef CBB_DEBUG
109#define DPRINTF(X) printf X
110#else
111#define DPRINTF(X)
112#endif
113
114int
115fxp_cardbus_match(parent, match, aux)
116	struct device *parent;
117	void *match;
118	void *aux;
119{
120	struct cardbus_attach_args *ca = aux;
121
122	if (CARDBUS_VENDOR(ca->ca_id) == CARDBUS_VENDOR_INTEL &&
123	    CARDBUS_PRODUCT(ca->ca_id) == CARDBUS_PRODUCT_INTEL_82557)
124		return (1);
125
126	return (0);
127}
128
129void
130fxp_cardbus_attach(parent, self, aux)
131	struct device *parent, *self;
132	void *aux;
133{
134	static const char thisfunc[] = "fxp_cardbus_attach";
135
136	char intrstr[16];
137	struct fxp_softc *sc = (struct fxp_softc *) self;
138	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) self;
139	struct cardbus_attach_args *ca = aux;
140	struct cardbus_softc *psc =
141	    (struct cardbus_softc *)sc->sc_dev.dv_parent;
142	cardbus_chipset_tag_t cc = psc->sc_cc;
143	cardbus_function_tag_t cf = psc->sc_cf;
144	bus_space_tag_t iot, memt;
145	bus_space_handle_t ioh, memh;
146	u_int8_t enaddr[6];
147
148	bus_addr_t adr;
149	bus_size_t size;
150
151	csc->ct = ca->ca_ct;
152
153	/*
154         * Map control/status registers.
155         */
156	if (Cardbus_mapreg_map(csc->ct, CARDBUS_BASE1_REG,
157	    PCI_MAPREG_TYPE_IO, 0, &iot, &ioh, &adr, &size) == 0) {
158		csc->base1_reg = adr | 1;
159		sc->sc_st = iot;
160		sc->sc_sh = ioh;
161		csc->size = size;
162	} else if (Cardbus_mapreg_map(csc->ct, CARDBUS_BASE0_REG,
163	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
164	    0, &memt, &memh, &adr, &size) == 0) {
165		csc->base0_reg = adr;
166		sc->sc_st = memt;
167		sc->sc_sh = memh;
168		csc->size = size;
169	} else
170		panic("%s: failed to allocate mem and io space", thisfunc);
171
172	if (ca->ca_cis.cis1_info[0] && ca->ca_cis.cis1_info[1])
173		printf(": %s %s", ca->ca_cis.cis1_info[0],
174		    ca->ca_cis.cis1_info[1]);
175	else
176		printf("\n");
177
178#if 0
179	sc->sc_dmat = ca->ca_dmat;
180	sc->sc_enable = fxp_cardbus_enable;
181	sc->sc_disable = fxp_cardbus_disable;
182	sc->sc_enabled = 0;
183#endif
184
185	Cardbus_function_enable(csc->ct);
186
187	fxp_cardbus_setup(sc);
188
189	/* Map and establish the interrupt. */
190	sc->sc_ih = cardbus_intr_establish(cc, cf, psc->sc_intrline, IPL_NET,
191	    fxp_intr, sc);
192	if (NULL == sc->sc_ih) {
193		printf(": couldn't establish interrupt");
194		printf("at %d\n", ca->ca_intrline);
195		return;
196	}
197	snprintf(intrstr, sizeof(intrstr), "irq %d", ca->ca_intrline);
198
199	fxp_attach_common(sc, enaddr, intrstr);
200}
201
202void
203fxp_cardbus_setup(struct fxp_softc * sc)
204{
205	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) sc;
206	struct cardbus_softc *psc =
207	    (struct cardbus_softc *) sc->sc_dev.dv_parent;
208	cardbus_chipset_tag_t cc = psc->sc_cc;
209	cardbus_function_tag_t cf = psc->sc_cf;
210	pcireg_t command;
211
212	cardbustag_t tag = cardbus_make_tag(cc, cf, csc->ct->ct_bus,
213	    csc->ct->ct_dev, csc->ct->ct_func);
214
215	command = Cardbus_conf_read(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG);
216	if (csc->base0_reg) {
217		Cardbus_conf_write(csc->ct, tag,
218		    CARDBUS_BASE0_REG, csc->base0_reg);
219		(cf->cardbus_ctrl) (cc, CARDBUS_MEM_ENABLE);
220		command |= CARDBUS_COMMAND_MEM_ENABLE |
221		    CARDBUS_COMMAND_MASTER_ENABLE;
222	} else if (csc->base1_reg) {
223		Cardbus_conf_write(csc->ct, tag,
224		    CARDBUS_BASE1_REG, csc->base1_reg);
225		(cf->cardbus_ctrl) (cc, CARDBUS_IO_ENABLE);
226		command |= (CARDBUS_COMMAND_IO_ENABLE |
227		    CARDBUS_COMMAND_MASTER_ENABLE);
228	}
229
230	(cf->cardbus_ctrl) (cc, CARDBUS_BM_ENABLE);
231
232	/* enable the card */
233	Cardbus_conf_write(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG, command);
234}
235
236int
237fxp_cardbus_detach(self, flags)
238	struct device *self;
239	int flags;
240{
241	struct fxp_softc *sc = (struct fxp_softc *) self;
242	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) self;
243	struct cardbus_devfunc *ct = csc->ct;
244	int rv, reg;
245
246#ifdef DIAGNOSTIC
247	if (ct == NULL)
248		panic("%s: data structure lacks\n", sc->sc_dev.dv_xname);
249#endif
250
251	rv = fxp_detach(sc);
252	if (rv == 0) {
253		/*
254		 * Unhook the interrupt handler.
255		 */
256		cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, sc->sc_ih);
257
258		/*
259		 * release bus space and close window
260		 */
261		if (csc->base0_reg)
262			reg = CARDBUS_BASE0_REG;
263		else
264			reg = CARDBUS_BASE1_REG;
265		Cardbus_mapreg_unmap(ct, reg, sc->sc_st, sc->sc_sh, csc->size);
266	}
267	return (rv);
268}
269