if_tlp_cardbus.c revision 1.3 1 /* $NetBSD: if_tlp_cardbus.c,v 1.3 1999/12/07 07:33:25 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
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 bus front-end for the Digital Semiconductor ``Tulip'' (21x4x)
42 * Ethernet controller family driver.
43 *
44 * TODO:
45 *
46 * - power management
47 * - add support for the Xircom clone
48 */
49
50 #include "opt_inet.h"
51 #include "opt_ns.h"
52 #include "bpfilter.h"
53
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/mbuf.h>
57 #include <sys/malloc.h>
58 #include <sys/kernel.h>
59 #include <sys/socket.h>
60 #include <sys/ioctl.h>
61 #include <sys/errno.h>
62 #include <sys/device.h>
63
64 #include <net/if.h>
65 #include <net/if_dl.h>
66 #include <net/if_media.h>
67 #include <net/if_ether.h>
68
69 #if NBPFILTER > 0
70 #include <net/bpf.h>
71 #endif
72
73 #ifdef INET
74 #include <netinet/in.h>
75 #include <netinet/if_inarp.h>
76 #endif
77
78 #ifdef NS
79 #include <netns/ns.h>
80 #include <netns/ns_if.h>
81 #endif
82
83 #include <machine/bus.h>
84 #include <machine/intr.h>
85
86 #include <dev/mii/miivar.h>
87 #include <dev/mii/mii_bitbang.h>
88
89 #include <dev/ic/tulipreg.h>
90 #include <dev/ic/tulipvar.h>
91
92 #include <dev/pci/pcivar.h>
93 #include <dev/pci/pcireg.h>
94 #include <dev/pci/pcidevs.h>
95
96 #include <dev/cardbus/cardbusvar.h>
97
98 /*
99 * PCI configuration space registers used by the Tulip.
100 */
101 #define TULIP_PCI_IOBA 0x10 /* i/o mapped base */
102 #define TULIP_PCI_MMBA 0x14 /* memory mapped base */
103 #define TULIP_PCI_CFDA 0x40 /* configuration driver area */
104
105 #define CFDA_SLEEP 0x80000000 /* sleep mode */
106
107 struct tulip_cardbus_softc {
108 struct tulip_softc sc_tulip; /* real Tulip softc */
109
110 /* CardBus-specific goo. */
111 void *sc_ih; /* interrupt handle */
112
113 cardbus_devfunc_t sc_ct; /* our CardBus devfuncs */
114 int sc_intrline; /* our interrupt line */
115 cardbustag_t sc_tag;
116
117 int sc_cbenable; /* what CardBus access type to enable */
118 int sc_csr; /* CSR bits */
119 };
120
121 int tlp_cardbus_match __P((struct device *, struct cfdata *, void *));
122 void tlp_cardbus_attach __P((struct device *, struct device *, void *));
123
124 struct cfattach tlp_cardbus_ca = {
125 sizeof(struct tulip_cardbus_softc),
126 tlp_cardbus_match, tlp_cardbus_attach,
127 };
128
129 const struct tulip_cardbus_product {
130 u_int32_t tcp_vendor; /* PCI vendor ID */
131 u_int32_t tcp_product; /* PCI product ID */
132 tulip_chip_t tcp_chip; /* base Tulip chip type */
133 int tcp_pmreg; /* power management register offset */
134 } tlp_cardbus_products[] = {
135 { PCI_VENDOR_DEC, PCI_PRODUCT_DEC_21142,
136 TULIP_CHIP_21142, 0xe0 },
137
138 { 0, 0,
139 TULIP_CHIP_INVALID, 0 },
140 };
141
142 const char *tlp_cardbus_chip_names[] = TULIP_CHIP_NAMES;
143
144 const struct tulip_cardbus_product *tlp_cardbus_lookup
145 __P((const struct cardbus_attach_args *));
146
147 const struct tulip_cardbus_product *
148 tlp_cardbus_lookup(ca)
149 const struct cardbus_attach_args *ca;
150 {
151 const struct tulip_cardbus_product *tcp;
152
153 for (tcp = tlp_cardbus_products;
154 tlp_cardbus_chip_names[tcp->tcp_chip] != NULL;
155 tcp++) {
156 if (PCI_VENDOR(ca->ca_id) == tcp->tcp_vendor &&
157 PCI_PRODUCT(ca->ca_id) == tcp->tcp_product)
158 return (tcp);
159 }
160 return (NULL);
161 }
162
163 int
164 tlp_cardbus_match(parent, match, aux)
165 struct device *parent;
166 struct cfdata *match;
167 void *aux;
168 {
169 struct cardbus_attach_args *ca = aux;
170
171 if (tlp_cardbus_lookup(ca) != NULL)
172 return (1);
173
174 return (0);
175 }
176
177 void
178 tlp_cardbus_attach(parent, self, aux)
179 struct device *parent, *self;
180 void *aux;
181 {
182 struct tulip_cardbus_softc *csc = (void *) self;
183 struct tulip_softc *sc = &csc->sc_tulip;
184 struct cardbus_attach_args *ca = aux;
185 cardbus_devfunc_t ct = ca->ca_ct;
186 cardbus_chipset_tag_t cc = ct->ct_cc;
187 cardbus_function_tag_t cf = ct->ct_cf;
188 const struct tulip_cardbus_product *tcp;
189 u_int8_t enaddr[ETHER_ADDR_LEN];
190 pcireg_t reg;
191
192 sc->sc_devno = ca->ca_device;
193 sc->sc_dmat = ca->ca_dmat;
194 csc->sc_ct = ct;
195 csc->sc_tag = ca->ca_tag;
196 csc->sc_intrline = ca->ca_intrline;
197
198 tcp = tlp_cardbus_lookup(ca);
199 if (tcp == NULL) {
200 printf("\n");
201 panic("tlp_cardbus_attach: impossible");
202 }
203 sc->sc_chip = tcp->tcp_chip;
204
205 /*
206 * By default, Tulip registers are 8 bytes long (4 bytes
207 * followed by a 4 byte pad).
208 */
209 sc->sc_regshift = 3;
210
211 /*
212 * Get revision info, and set some chip-specific variables.
213 */
214 sc->sc_rev = PCI_REVISION(ca->ca_class);
215 switch (sc->sc_chip) {
216 case TULIP_CHIP_21142:
217 if (sc->sc_rev >= 0x20)
218 sc->sc_chip = TULIP_CHIP_21143;
219 break;
220
221 default:
222 /* Nothing. */
223 }
224
225 printf(": %s Ethernet, pass %d.%d\n",
226 tlp_cardbus_chip_names[sc->sc_chip],
227 (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
228
229 /*
230 * Check to see if the device is in power-save mode, and
231 * bring it out if necessary.
232 */
233 switch (sc->sc_chip) {
234 case TULIP_CHIP_21142:
235 case TULIP_CHIP_21143:
236 /*
237 * Clear the "sleep mode" bit in the CFDA register.
238 */
239 reg = cardbus_conf_read(cc, cf, ca->ca_tag, TULIP_PCI_CFDA);
240 if (reg & CFDA_SLEEP)
241 cardbus_conf_write(cc, cf, ca->ca_tag, TULIP_PCI_CFDA,
242 reg & ~CFDA_SLEEP);
243 break;
244
245 default:
246 /* Nothing. */
247 }
248
249 /*
250 * Map the device.
251 */
252 csc->sc_csr = PCI_COMMAND_MASTER_ENABLE;
253 if (Cardbus_mapreg_map(csc->sc_ct, TULIP_PCI_MMBA,
254 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
255 &sc->sc_st, &sc->sc_sh, NULL, NULL) == 0) {
256 csc->sc_cbenable = CARDBUS_MEM_ENABLE;
257 csc->sc_csr |= PCI_COMMAND_MEM_ENABLE;
258 } else if (Cardbus_mapreg_map(csc->sc_ct, TULIP_PCI_IOBA,
259 PCI_MAPREG_TYPE_IO, 0, &sc->sc_st, &sc->sc_sh, NULL, NULL) == 0) {
260 csc->sc_cbenable = CARDBUS_IO_ENABLE;
261 csc->sc_csr |= PCI_COMMAND_IO_ENABLE;
262 } else {
263 printf("%s: unable to map device registers\n",
264 sc->sc_dev.dv_xname);
265 return;
266 }
267
268 /* Make sure the right access type is on the CardBus bridge. */
269 (*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cbenable);
270
271 /* Enable the appropriate bits in the PCI CSR. */
272 reg = cardbus_conf_read(cc, cf, ca->ca_tag, PCI_COMMAND_STATUS_REG);
273 reg = (reg & ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE)) |
274 csc->sc_csr;
275 cardbus_conf_write(cc, cf, ca->ca_tag, PCI_COMMAND_STATUS_REG, reg);
276
277 /*
278 * Read the contents of the Ethernet Address ROM/SROM. Some
279 * chips have a 128 byte SROM (6 address bits), and some
280 * have a 512 byte SROM (8 address bits).
281 */
282 sc->sc_srom_addrbits = 6;
283 try_again:
284 memset(sc->sc_srom, 0, sizeof(sc->sc_srom));
285 tlp_read_srom(sc, 0, TULIP_ROM_SIZE(sc->sc_srom_addrbits) >> 1,
286 sc->sc_srom);
287 #if 0
288 {
289 int i;
290
291 printf("SROM CONTENTS:");
292 for (i = 0; i < TULIP_ROM_SIZE(sc->sc_srom_addrbits); i++) {
293 if ((i % 8) == 0)
294 printf("\n\t");
295 printf("0x%02x ", sc->sc_srom[i]);
296 }
297 printf("\n");
298 }
299 #endif
300
301 /*
302 * Deal with chip/board quirks. This includes setting up
303 * the mediasw, and extracting the Ethernet address from
304 * the rombuf.
305 */
306 switch (sc->sc_chip) {
307 case TULIP_CHIP_21142:
308 case TULIP_CHIP_21143:
309 /* Check for new format SROM. */
310 if (tlp_isv_srom_enaddr(sc, enaddr) == 0) {
311 /*
312 * Not an ISV SROM; can't cope, for now.
313 */
314 goto cant_cope;
315 } else {
316 /*
317 * We start out with the 2114x ISV media switch.
318 * When we search for quirks, we may change to
319 * a different switch.
320 */
321 sc->sc_mediasw = &tlp_2114x_isv_mediasw;
322 }
323
324 /*
325 * Bail out now if we can't deal with this board.
326 */
327 if (sc->sc_mediasw == NULL)
328 goto cant_cope;
329 break;
330
331 default:
332 cant_cope:
333 /*
334 * Try reading it again, with larger SROM
335 * size, if we haven't already.
336 */
337 if (sc->sc_srom_addrbits != 8) {
338 sc->sc_srom_addrbits = 8;
339 goto try_again;
340 }
341
342 printf("%s: sorry, unable to handle your board\n",
343 sc->sc_dev.dv_xname);
344 return;
345 }
346
347 /*
348 * Map and establish the interrupt.
349 */
350 csc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline, IPL_NET,
351 tlp_intr, sc);
352 if (csc->sc_ih == NULL) {
353 printf("%s: unable to establish interrupt at %d\n",
354 sc->sc_dev.dv_xname, ca->ca_intrline);
355 return;
356 }
357 printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
358 ca->ca_intrline);
359
360 /*
361 * Finish off the attach.
362 */
363 tlp_attach(sc, enaddr);
364 }
365