if_tlp_cardbus.c revision 1.9 1 /* $NetBSD: if_tlp_cardbus.c,v 1.9 2000/01/25 19:29:19 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 <machine/endian.h>
65
66 #include <net/if.h>
67 #include <net/if_dl.h>
68 #include <net/if_media.h>
69 #include <net/if_ether.h>
70
71 #if NBPFILTER > 0
72 #include <net/bpf.h>
73 #endif
74
75 #ifdef INET
76 #include <netinet/in.h>
77 #include <netinet/if_inarp.h>
78 #endif
79
80 #ifdef NS
81 #include <netns/ns.h>
82 #include <netns/ns_if.h>
83 #endif
84
85 #include <machine/bus.h>
86 #include <machine/intr.h>
87
88 #include <dev/mii/miivar.h>
89 #include <dev/mii/mii_bitbang.h>
90
91 #include <dev/ic/tulipreg.h>
92 #include <dev/ic/tulipvar.h>
93
94 #include <dev/pci/pcivar.h>
95 #include <dev/pci/pcireg.h>
96 #include <dev/pci/pcidevs.h>
97
98 #include <dev/cardbus/cardbusvar.h>
99
100 /*
101 * PCI configuration space registers used by the Tulip.
102 */
103 #define TULIP_PCI_IOBA 0x10 /* i/o mapped base */
104 #define TULIP_PCI_MMBA 0x14 /* memory mapped base */
105 #define TULIP_PCI_CFDA 0x40 /* configuration driver area */
106
107 #define CFDA_SLEEP 0x80000000 /* sleep mode */
108 #define CFDA_SNOOZE 0x40000000 /* snooze mode */
109
110 struct tulip_cardbus_softc {
111 struct tulip_softc sc_tulip; /* real Tulip softc */
112
113 /* CardBus-specific goo. */
114 void *sc_ih; /* interrupt handle */
115
116 cardbus_devfunc_t sc_ct; /* our CardBus devfuncs */
117 int sc_intrline; /* our interrupt line */
118 cardbustag_t sc_tag;
119
120 int sc_cbenable; /* what CardBus access type to enable */
121 int sc_csr; /* CSR bits */
122 };
123
124 int tlp_cardbus_match __P((struct device *, struct cfdata *, void *));
125 void tlp_cardbus_attach __P((struct device *, struct device *, void *));
126
127 struct cfattach tlp_cardbus_ca = {
128 sizeof(struct tulip_cardbus_softc),
129 tlp_cardbus_match, tlp_cardbus_attach,
130 };
131
132 const struct tulip_cardbus_product {
133 u_int32_t tcp_vendor; /* PCI vendor ID */
134 u_int32_t tcp_product; /* PCI product ID */
135 tulip_chip_t tcp_chip; /* base Tulip chip type */
136 int tcp_pmreg; /* power management register offset */
137 } tlp_cardbus_products[] = {
138 { PCI_VENDOR_DEC, PCI_PRODUCT_DEC_21142,
139 TULIP_CHIP_21142, 0xe0 },
140
141 { 0, 0,
142 TULIP_CHIP_INVALID, 0 },
143 };
144
145 const struct tulip_cardbus_product *tlp_cardbus_lookup
146 __P((const struct cardbus_attach_args *));
147
148 const struct tulip_cardbus_product *
149 tlp_cardbus_lookup(ca)
150 const struct cardbus_attach_args *ca;
151 {
152 const struct tulip_cardbus_product *tcp;
153
154 for (tcp = tlp_cardbus_products;
155 tlp_chip_names[tcp->tcp_chip] != NULL;
156 tcp++) {
157 if (PCI_VENDOR(ca->ca_id) == tcp->tcp_vendor &&
158 PCI_PRODUCT(ca->ca_id) == tcp->tcp_product)
159 return (tcp);
160 }
161 return (NULL);
162 }
163
164 int
165 tlp_cardbus_match(parent, match, aux)
166 struct device *parent;
167 struct cfdata *match;
168 void *aux;
169 {
170 struct cardbus_attach_args *ca = aux;
171
172 if (tlp_cardbus_lookup(ca) != NULL)
173 return (1);
174
175 return (0);
176 }
177
178 void
179 tlp_cardbus_attach(parent, self, aux)
180 struct device *parent, *self;
181 void *aux;
182 {
183 struct tulip_cardbus_softc *csc = (void *) self;
184 struct tulip_softc *sc = &csc->sc_tulip;
185 struct cardbus_attach_args *ca = aux;
186 cardbus_devfunc_t ct = ca->ca_ct;
187 cardbus_chipset_tag_t cc = ct->ct_cc;
188 cardbus_function_tag_t cf = ct->ct_cf;
189 const struct tulip_cardbus_product *tcp;
190 u_int8_t enaddr[ETHER_ADDR_LEN];
191 pcireg_t reg;
192
193 sc->sc_devno = ca->ca_device;
194 sc->sc_dmat = ca->ca_dmat;
195 csc->sc_ct = ct;
196 csc->sc_tag = ca->ca_tag;
197 csc->sc_intrline = ca->ca_intrline;
198
199 tcp = tlp_cardbus_lookup(ca);
200 if (tcp == NULL) {
201 printf("\n");
202 panic("tlp_cardbus_attach: impossible");
203 }
204 sc->sc_chip = tcp->tcp_chip;
205
206 /*
207 * By default, Tulip registers are 8 bytes long (4 bytes
208 * followed by a 4 byte pad).
209 */
210 sc->sc_regshift = 3;
211
212 /*
213 * Get revision info, and set some chip-specific variables.
214 */
215 sc->sc_rev = PCI_REVISION(ca->ca_class);
216 switch (sc->sc_chip) {
217 case TULIP_CHIP_21142:
218 if (sc->sc_rev >= 0x20)
219 sc->sc_chip = TULIP_CHIP_21143;
220 break;
221
222 default:
223 /* Nothing. */
224 }
225
226 printf(": %s Ethernet, pass %d.%d\n",
227 tlp_chip_names[sc->sc_chip],
228 (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
229
230 /*
231 * Check to see if the device is in power-save mode, and
232 * bring it out if necessary.
233 */
234 switch (sc->sc_chip) {
235 case TULIP_CHIP_21142:
236 case TULIP_CHIP_21143:
237 /*
238 * Clear the "sleep mode" bit in the CFDA register.
239 */
240 reg = cardbus_conf_read(cc, cf, ca->ca_tag, TULIP_PCI_CFDA);
241 if (reg & (CFDA_SLEEP|CFDA_SNOOZE))
242 cardbus_conf_write(cc, cf, ca->ca_tag, TULIP_PCI_CFDA,
243 reg & ~(CFDA_SLEEP|CFDA_SNOOZE));
244 break;
245
246 default:
247 /* Nothing. */
248 }
249
250 if (cardbus_get_capability(cc, cf, ca->ca_tag, PCI_CAP_PWRMGMT, 0, 0)) {
251 if (tcp->tcp_pmreg == 0) {
252 printf("%s: don't know location of PMCSR for this "
253 "chip\n", sc->sc_dev.dv_xname);
254 return;
255 }
256 reg = cardbus_conf_read(cc, cf, ca->ca_tag,
257 tcp->tcp_pmreg) & 0x03;
258 #if 1 /* XXX Probably not right for CardBus. */
259 if (reg == 3) {
260 /*
261 * The card has lost all configuration data in
262 * this state, so punt.
263 */
264 printf("%s: unable to wake up from power state D3\n",
265 sc->sc_dev.dv_xname);
266 return;
267 }
268 #endif
269 if (reg != 0) {
270 printf("%s: waking up from power state D%d\n",
271 sc->sc_dev.dv_xname, reg);
272 cardbus_conf_write(cc, cf, ca->ca_tag,
273 tcp->tcp_pmreg, 0);
274 }
275 }
276
277 /*
278 * Map the device.
279 */
280 csc->sc_csr = PCI_COMMAND_MASTER_ENABLE;
281 if (Cardbus_mapreg_map(csc->sc_ct, TULIP_PCI_MMBA,
282 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
283 &sc->sc_st, &sc->sc_sh, NULL, NULL) == 0) {
284 csc->sc_cbenable = CARDBUS_MEM_ENABLE;
285 csc->sc_csr |= PCI_COMMAND_MEM_ENABLE;
286 } else if (Cardbus_mapreg_map(csc->sc_ct, TULIP_PCI_IOBA,
287 PCI_MAPREG_TYPE_IO, 0, &sc->sc_st, &sc->sc_sh, NULL, NULL) == 0) {
288 csc->sc_cbenable = CARDBUS_IO_ENABLE;
289 csc->sc_csr |= PCI_COMMAND_IO_ENABLE;
290 } else {
291 printf("%s: unable to map device registers\n",
292 sc->sc_dev.dv_xname);
293 return;
294 }
295
296 /* Make sure the right access type is on the CardBus bridge. */
297 (*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cbenable);
298 (*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
299
300 /* Enable the appropriate bits in the PCI CSR. */
301 reg = cardbus_conf_read(cc, cf, ca->ca_tag, PCI_COMMAND_STATUS_REG);
302 reg &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE);
303 reg |= csc->sc_csr;
304 cardbus_conf_write(cc, cf, ca->ca_tag, PCI_COMMAND_STATUS_REG, reg);
305
306 /*
307 * Make sure the latency timer is set to some reasonable
308 * value.
309 */
310 reg = cardbus_conf_read(cc, cf, ca->ca_tag, PCI_BHLC_REG);
311 if (PCI_LATTIMER(reg) < 0x20) {
312 reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
313 reg |= (0x20 << PCI_LATTIMER_SHIFT);
314 cardbus_conf_write(cc, cf, ca->ca_tag, PCI_BHLC_REG, reg);
315 }
316
317 /*
318 * Read the contents of the Ethernet Address ROM/SROM. Some
319 * chips have a 128 byte SROM (6 address bits), and some
320 * have a 512 byte SROM (8 address bits).
321 */
322 sc->sc_srom_addrbits = 6;
323 try_again:
324 memset(sc->sc_srom, 0, sizeof(sc->sc_srom));
325 tlp_read_srom(sc, 0, TULIP_ROM_SIZE(sc->sc_srom_addrbits) >> 1,
326 sc->sc_srom);
327 #if 0
328 {
329 int i;
330
331 printf("SROM CONTENTS:");
332 for (i = 0; i < TULIP_ROM_SIZE(sc->sc_srom_addrbits); i++) {
333 if ((i % 8) == 0)
334 printf("\n\t");
335 printf("0x%02x ", sc->sc_srom[i]);
336 }
337 printf("\n");
338 }
339 #endif
340
341 /*
342 * Deal with chip/board quirks. This includes setting up
343 * the mediasw, and extracting the Ethernet address from
344 * the rombuf.
345 */
346 switch (sc->sc_chip) {
347 case TULIP_CHIP_21142:
348 case TULIP_CHIP_21143:
349 /* Check for new format SROM. */
350 if (tlp_isv_srom_enaddr(sc, enaddr) == 0) {
351 /*
352 * Not an ISV SROM; can't cope, for now.
353 */
354 goto cant_cope;
355 } else {
356 /*
357 * We start out with the 2114x ISV media switch.
358 * When we search for quirks, we may change to
359 * a different switch.
360 */
361 sc->sc_mediasw = &tlp_2114x_isv_mediasw;
362 }
363
364 /*
365 * Bail out now if we can't deal with this board.
366 */
367 if (sc->sc_mediasw == NULL)
368 goto cant_cope;
369 break;
370
371 default:
372 cant_cope:
373 /*
374 * Try reading it again, with larger SROM
375 * size, if we haven't already.
376 */
377 if (sc->sc_srom_addrbits != 8) {
378 sc->sc_srom_addrbits = 8;
379 goto try_again;
380 }
381
382 printf("%s: sorry, unable to handle your board\n",
383 sc->sc_dev.dv_xname);
384 return;
385 }
386
387 /*
388 * Map and establish the interrupt.
389 */
390 csc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline, IPL_NET,
391 tlp_intr, sc);
392 if (csc->sc_ih == NULL) {
393 printf("%s: unable to establish interrupt at %d\n",
394 sc->sc_dev.dv_xname, ca->ca_intrline);
395 return;
396 }
397 printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
398 ca->ca_intrline);
399
400 /*
401 * Finish off the attach.
402 */
403 tlp_attach(sc, enaddr);
404 }
405