if_tlp_cardbus.c revision 1.4 1 /* $NetBSD: if_tlp_cardbus.c,v 1.4 1999/12/07 07:36: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 <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 #define CFDA_SNOOZE 0x40000000 /* snooze mode */
107
108 struct tulip_cardbus_softc {
109 struct tulip_softc sc_tulip; /* real Tulip softc */
110
111 /* CardBus-specific goo. */
112 void *sc_ih; /* interrupt handle */
113
114 cardbus_devfunc_t sc_ct; /* our CardBus devfuncs */
115 int sc_intrline; /* our interrupt line */
116 cardbustag_t sc_tag;
117
118 int sc_cbenable; /* what CardBus access type to enable */
119 int sc_csr; /* CSR bits */
120 };
121
122 int tlp_cardbus_match __P((struct device *, struct cfdata *, void *));
123 void tlp_cardbus_attach __P((struct device *, struct device *, void *));
124
125 struct cfattach tlp_cardbus_ca = {
126 sizeof(struct tulip_cardbus_softc),
127 tlp_cardbus_match, tlp_cardbus_attach,
128 };
129
130 const struct tulip_cardbus_product {
131 u_int32_t tcp_vendor; /* PCI vendor ID */
132 u_int32_t tcp_product; /* PCI product ID */
133 tulip_chip_t tcp_chip; /* base Tulip chip type */
134 int tcp_pmreg; /* power management register offset */
135 } tlp_cardbus_products[] = {
136 { PCI_VENDOR_DEC, PCI_PRODUCT_DEC_21142,
137 TULIP_CHIP_21142, 0xe0 },
138
139 { 0, 0,
140 TULIP_CHIP_INVALID, 0 },
141 };
142
143 const char *tlp_cardbus_chip_names[] = TULIP_CHIP_NAMES;
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_cardbus_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_cardbus_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 /*
251 * Map the device.
252 */
253 csc->sc_csr = PCI_COMMAND_MASTER_ENABLE;
254 if (Cardbus_mapreg_map(csc->sc_ct, TULIP_PCI_MMBA,
255 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
256 &sc->sc_st, &sc->sc_sh, NULL, NULL) == 0) {
257 csc->sc_cbenable = CARDBUS_MEM_ENABLE;
258 csc->sc_csr |= PCI_COMMAND_MEM_ENABLE;
259 } else if (Cardbus_mapreg_map(csc->sc_ct, TULIP_PCI_IOBA,
260 PCI_MAPREG_TYPE_IO, 0, &sc->sc_st, &sc->sc_sh, NULL, NULL) == 0) {
261 csc->sc_cbenable = CARDBUS_IO_ENABLE;
262 csc->sc_csr |= PCI_COMMAND_IO_ENABLE;
263 } else {
264 printf("%s: unable to map device registers\n",
265 sc->sc_dev.dv_xname);
266 return;
267 }
268
269 /* Make sure the right access type is on the CardBus bridge. */
270 (*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cbenable);
271
272 /* Enable the appropriate bits in the PCI CSR. */
273 reg = cardbus_conf_read(cc, cf, ca->ca_tag, PCI_COMMAND_STATUS_REG);
274 reg = (reg & ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE)) |
275 csc->sc_csr;
276 cardbus_conf_write(cc, cf, ca->ca_tag, PCI_COMMAND_STATUS_REG, reg);
277
278 /*
279 * Read the contents of the Ethernet Address ROM/SROM. Some
280 * chips have a 128 byte SROM (6 address bits), and some
281 * have a 512 byte SROM (8 address bits).
282 */
283 sc->sc_srom_addrbits = 6;
284 try_again:
285 memset(sc->sc_srom, 0, sizeof(sc->sc_srom));
286 tlp_read_srom(sc, 0, TULIP_ROM_SIZE(sc->sc_srom_addrbits) >> 1,
287 sc->sc_srom);
288 #if 0
289 {
290 int i;
291
292 printf("SROM CONTENTS:");
293 for (i = 0; i < TULIP_ROM_SIZE(sc->sc_srom_addrbits); i++) {
294 if ((i % 8) == 0)
295 printf("\n\t");
296 printf("0x%02x ", sc->sc_srom[i]);
297 }
298 printf("\n");
299 }
300 #endif
301
302 /*
303 * Deal with chip/board quirks. This includes setting up
304 * the mediasw, and extracting the Ethernet address from
305 * the rombuf.
306 */
307 switch (sc->sc_chip) {
308 case TULIP_CHIP_21142:
309 case TULIP_CHIP_21143:
310 /* Check for new format SROM. */
311 if (tlp_isv_srom_enaddr(sc, enaddr) == 0) {
312 /*
313 * Not an ISV SROM; can't cope, for now.
314 */
315 goto cant_cope;
316 } else {
317 /*
318 * We start out with the 2114x ISV media switch.
319 * When we search for quirks, we may change to
320 * a different switch.
321 */
322 sc->sc_mediasw = &tlp_2114x_isv_mediasw;
323 }
324
325 /*
326 * Bail out now if we can't deal with this board.
327 */
328 if (sc->sc_mediasw == NULL)
329 goto cant_cope;
330 break;
331
332 default:
333 cant_cope:
334 /*
335 * Try reading it again, with larger SROM
336 * size, if we haven't already.
337 */
338 if (sc->sc_srom_addrbits != 8) {
339 sc->sc_srom_addrbits = 8;
340 goto try_again;
341 }
342
343 printf("%s: sorry, unable to handle your board\n",
344 sc->sc_dev.dv_xname);
345 return;
346 }
347
348 /*
349 * Map and establish the interrupt.
350 */
351 csc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline, IPL_NET,
352 tlp_intr, sc);
353 if (csc->sc_ih == NULL) {
354 printf("%s: unable to establish interrupt at %d\n",
355 sc->sc_dev.dv_xname, ca->ca_intrline);
356 return;
357 }
358 printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
359 ca->ca_intrline);
360
361 /*
362 * Finish off the attach.
363 */
364 tlp_attach(sc, enaddr);
365 }
366