if_tlp_cardbus.c revision 1.65 1 1.65 dyoung /* $NetBSD: if_tlp_cardbus.c,v 1.65 2010/02/26 00:01:27 dyoung Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.10 thorpej * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.1 thorpej * NASA Ames Research Center.
10 1.1 thorpej *
11 1.1 thorpej * Redistribution and use in source and binary forms, with or without
12 1.1 thorpej * modification, are permitted provided that the following conditions
13 1.1 thorpej * are met:
14 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer.
16 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.1 thorpej * documentation and/or other materials provided with the distribution.
19 1.1 thorpej *
20 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
31 1.1 thorpej */
32 1.1 thorpej
33 1.1 thorpej /*
34 1.1 thorpej * CardBus bus front-end for the Digital Semiconductor ``Tulip'' (21x4x)
35 1.1 thorpej * Ethernet controller family driver.
36 1.1 thorpej */
37 1.31 lukem
38 1.31 lukem #include <sys/cdefs.h>
39 1.65 dyoung __KERNEL_RCSID(0, "$NetBSD: if_tlp_cardbus.c,v 1.65 2010/02/26 00:01:27 dyoung Exp $");
40 1.1 thorpej
41 1.1 thorpej #include "opt_inet.h"
42 1.1 thorpej
43 1.1 thorpej #include <sys/param.h>
44 1.45 perry #include <sys/systm.h>
45 1.45 perry #include <sys/mbuf.h>
46 1.1 thorpej #include <sys/malloc.h>
47 1.1 thorpej #include <sys/kernel.h>
48 1.1 thorpej #include <sys/socket.h>
49 1.1 thorpej #include <sys/ioctl.h>
50 1.1 thorpej #include <sys/errno.h>
51 1.1 thorpej #include <sys/device.h>
52 1.5 thorpej
53 1.5 thorpej #include <machine/endian.h>
54 1.45 perry
55 1.1 thorpej #include <net/if.h>
56 1.1 thorpej #include <net/if_dl.h>
57 1.1 thorpej #include <net/if_media.h>
58 1.1 thorpej #include <net/if_ether.h>
59 1.1 thorpej
60 1.1 thorpej #ifdef INET
61 1.45 perry #include <netinet/in.h>
62 1.1 thorpej #include <netinet/if_inarp.h>
63 1.1 thorpej #endif
64 1.1 thorpej
65 1.1 thorpej
66 1.54 ad #include <sys/bus.h>
67 1.54 ad #include <sys/intr.h>
68 1.1 thorpej
69 1.1 thorpej #include <dev/mii/miivar.h>
70 1.1 thorpej #include <dev/mii/mii_bitbang.h>
71 1.1 thorpej
72 1.1 thorpej #include <dev/ic/tulipreg.h>
73 1.1 thorpej #include <dev/ic/tulipvar.h>
74 1.1 thorpej
75 1.1 thorpej #include <dev/pci/pcivar.h>
76 1.1 thorpej #include <dev/pci/pcireg.h>
77 1.1 thorpej #include <dev/pci/pcidevs.h>
78 1.1 thorpej
79 1.1 thorpej #include <dev/cardbus/cardbusvar.h>
80 1.43 mycroft #include <dev/pci/pcidevs.h>
81 1.1 thorpej
82 1.1 thorpej /*
83 1.1 thorpej * PCI configuration space registers used by the Tulip.
84 1.1 thorpej */
85 1.1 thorpej #define TULIP_PCI_IOBA 0x10 /* i/o mapped base */
86 1.1 thorpej #define TULIP_PCI_MMBA 0x14 /* memory mapped base */
87 1.1 thorpej #define TULIP_PCI_CFDA 0x40 /* configuration driver area */
88 1.1 thorpej
89 1.1 thorpej #define CFDA_SLEEP 0x80000000 /* sleep mode */
90 1.4 thorpej #define CFDA_SNOOZE 0x40000000 /* snooze mode */
91 1.1 thorpej
92 1.1 thorpej struct tulip_cardbus_softc {
93 1.1 thorpej struct tulip_softc sc_tulip; /* real Tulip softc */
94 1.1 thorpej
95 1.1 thorpej /* CardBus-specific goo. */
96 1.1 thorpej void *sc_ih; /* interrupt handle */
97 1.1 thorpej cardbus_devfunc_t sc_ct; /* our CardBus devfuncs */
98 1.63 dyoung pcitag_t sc_tag; /* our CardBus tag */
99 1.65 dyoung pcireg_t sc_csr;
100 1.17 thorpej bus_size_t sc_mapsize; /* the size of mapped bus space
101 1.17 thorpej region */
102 1.18 thorpej
103 1.18 thorpej int sc_bar_reg; /* which BAR to use */
104 1.18 thorpej pcireg_t sc_bar_val; /* value of the BAR */
105 1.20 thorpej
106 1.59 drochner cardbus_intr_line_t sc_intrline; /* interrupt line */
107 1.1 thorpej };
108 1.1 thorpej
109 1.61 cegger int tlp_cardbus_match(device_t, cfdata_t, void *);
110 1.61 cegger void tlp_cardbus_attach(device_t, device_t, void *);
111 1.61 cegger int tlp_cardbus_detach(device_t, int);
112 1.1 thorpej
113 1.61 cegger CFATTACH_DECL_NEW(tlp_cardbus, sizeof(struct tulip_cardbus_softc),
114 1.37 thorpej tlp_cardbus_match, tlp_cardbus_attach, tlp_cardbus_detach, tlp_activate);
115 1.1 thorpej
116 1.1 thorpej const struct tulip_cardbus_product {
117 1.1 thorpej u_int32_t tcp_vendor; /* PCI vendor ID */
118 1.1 thorpej u_int32_t tcp_product; /* PCI product ID */
119 1.1 thorpej tulip_chip_t tcp_chip; /* base Tulip chip type */
120 1.1 thorpej } tlp_cardbus_products[] = {
121 1.43 mycroft { PCI_VENDOR_DEC, PCI_PRODUCT_DEC_21142,
122 1.23 thorpej TULIP_CHIP_21142 },
123 1.1 thorpej
124 1.43 mycroft { PCI_VENDOR_XIRCOM, PCI_PRODUCT_XIRCOM_X3201_3_21143,
125 1.23 thorpej TULIP_CHIP_X3201_3 },
126 1.27 kanaoka
127 1.50 gdamore { PCI_VENDOR_ADMTEK, PCI_PRODUCT_ADMTEK_AN983,
128 1.43 mycroft TULIP_CHIP_AN985 },
129 1.43 mycroft
130 1.43 mycroft { PCI_VENDOR_ACCTON, PCI_PRODUCT_ACCTON_EN2242,
131 1.27 kanaoka TULIP_CHIP_AN985 },
132 1.10 thorpej
133 1.43 mycroft { PCI_VENDOR_ABOCOM, PCI_PRODUCT_ABOCOM_FE2500,
134 1.25 thorpej TULIP_CHIP_AN985 },
135 1.25 thorpej
136 1.43 mycroft { PCI_VENDOR_ABOCOM, PCI_PRODUCT_ABOCOM_PCM200,
137 1.26 christos TULIP_CHIP_AN985 },
138 1.26 christos
139 1.43 mycroft { PCI_VENDOR_ABOCOM, PCI_PRODUCT_ABOCOM_FE2500MX,
140 1.38 onoe TULIP_CHIP_AN985 },
141 1.38 onoe
142 1.43 mycroft { PCI_VENDOR_HAWKING, PCI_PRODUCT_HAWKING_PN672TX,
143 1.32 augustss TULIP_CHIP_AN985 },
144 1.32 augustss
145 1.50 gdamore { PCI_VENDOR_ADMTEK, PCI_PRODUCT_ADMTEK_AN985,
146 1.39 mycroft TULIP_CHIP_AN985 },
147 1.39 mycroft
148 1.43 mycroft { PCI_VENDOR_MICROSOFT, PCI_PRODUCT_MICROSOFT_MN120,
149 1.25 thorpej TULIP_CHIP_AN985 },
150 1.25 thorpej
151 1.46 tron { PCI_VENDOR_LINKSYS, PCI_PRODUCT_LINKSYS_PCMPC200,
152 1.46 tron TULIP_CHIP_AN985 },
153 1.46 tron
154 1.1 thorpej { 0, 0,
155 1.23 thorpej TULIP_CHIP_INVALID },
156 1.1 thorpej };
157 1.1 thorpej
158 1.33 mycroft struct tlp_cardbus_quirks {
159 1.44 perry void (*tpq_func)(struct tulip_cardbus_softc *,
160 1.44 perry const u_int8_t *);
161 1.33 mycroft u_int8_t tpq_oui[3];
162 1.33 mycroft };
163 1.33 mycroft
164 1.44 perry void tlp_cardbus_lxt_quirks(struct tulip_cardbus_softc *,
165 1.44 perry const u_int8_t *);
166 1.33 mycroft
167 1.33 mycroft const struct tlp_cardbus_quirks tlp_cardbus_21142_quirks[] = {
168 1.33 mycroft { tlp_cardbus_lxt_quirks, { 0x00, 0x40, 0x05 } },
169 1.33 mycroft { NULL, { 0, 0, 0 } }
170 1.33 mycroft };
171 1.33 mycroft
172 1.44 perry void tlp_cardbus_setup(struct tulip_cardbus_softc *);
173 1.17 thorpej
174 1.44 perry int tlp_cardbus_enable(struct tulip_softc *);
175 1.44 perry void tlp_cardbus_disable(struct tulip_softc *);
176 1.44 perry void tlp_cardbus_power(struct tulip_softc *, int);
177 1.20 thorpej
178 1.44 perry void tlp_cardbus_x3201_reset(struct tulip_softc *);
179 1.10 thorpej
180 1.1 thorpej const struct tulip_cardbus_product *tlp_cardbus_lookup
181 1.44 perry (const struct cardbus_attach_args *);
182 1.44 perry void tlp_cardbus_get_quirks(struct tulip_cardbus_softc *,
183 1.44 perry const u_int8_t *, const struct tlp_cardbus_quirks *);
184 1.1 thorpej
185 1.1 thorpej const struct tulip_cardbus_product *
186 1.60 dsl tlp_cardbus_lookup(const struct cardbus_attach_args *ca)
187 1.1 thorpej {
188 1.1 thorpej const struct tulip_cardbus_product *tcp;
189 1.1 thorpej
190 1.1 thorpej for (tcp = tlp_cardbus_products;
191 1.9 thorpej tlp_chip_names[tcp->tcp_chip] != NULL;
192 1.1 thorpej tcp++) {
193 1.1 thorpej if (PCI_VENDOR(ca->ca_id) == tcp->tcp_vendor &&
194 1.1 thorpej PCI_PRODUCT(ca->ca_id) == tcp->tcp_product)
195 1.1 thorpej return (tcp);
196 1.1 thorpej }
197 1.1 thorpej return (NULL);
198 1.1 thorpej }
199 1.1 thorpej
200 1.33 mycroft void
201 1.60 dsl tlp_cardbus_get_quirks(struct tulip_cardbus_softc *csc, const u_int8_t *enaddr, const struct tlp_cardbus_quirks *tpq)
202 1.33 mycroft {
203 1.33 mycroft
204 1.33 mycroft for (; tpq->tpq_func != NULL; tpq++) {
205 1.33 mycroft if (tpq->tpq_oui[0] == enaddr[0] &&
206 1.33 mycroft tpq->tpq_oui[1] == enaddr[1] &&
207 1.33 mycroft tpq->tpq_oui[2] == enaddr[2]) {
208 1.33 mycroft (*tpq->tpq_func)(csc, enaddr);
209 1.33 mycroft return;
210 1.33 mycroft }
211 1.33 mycroft }
212 1.33 mycroft }
213 1.33 mycroft
214 1.1 thorpej int
215 1.61 cegger tlp_cardbus_match(device_t parent, cfdata_t match,
216 1.52 christos void *aux)
217 1.1 thorpej {
218 1.1 thorpej struct cardbus_attach_args *ca = aux;
219 1.1 thorpej
220 1.1 thorpej if (tlp_cardbus_lookup(ca) != NULL)
221 1.1 thorpej return (1);
222 1.1 thorpej
223 1.1 thorpej return (0);
224 1.1 thorpej }
225 1.1 thorpej
226 1.1 thorpej void
227 1.61 cegger tlp_cardbus_attach(device_t parent, device_t self,
228 1.52 christos void *aux)
229 1.1 thorpej {
230 1.49 thorpej struct tulip_cardbus_softc *csc = device_private(self);
231 1.1 thorpej struct tulip_softc *sc = &csc->sc_tulip;
232 1.1 thorpej struct cardbus_attach_args *ca = aux;
233 1.1 thorpej cardbus_devfunc_t ct = ca->ca_ct;
234 1.1 thorpej const struct tulip_cardbus_product *tcp;
235 1.1 thorpej u_int8_t enaddr[ETHER_ADDR_LEN];
236 1.15 mycroft bus_addr_t adr;
237 1.25 thorpej pcireg_t reg;
238 1.1 thorpej
239 1.61 cegger sc->sc_dev = self;
240 1.47 drochner sc->sc_devno = 0;
241 1.1 thorpej sc->sc_dmat = ca->ca_dmat;
242 1.1 thorpej csc->sc_ct = ct;
243 1.17 thorpej csc->sc_tag = ca->ca_tag;
244 1.1 thorpej
245 1.1 thorpej tcp = tlp_cardbus_lookup(ca);
246 1.1 thorpej if (tcp == NULL) {
247 1.1 thorpej printf("\n");
248 1.1 thorpej panic("tlp_cardbus_attach: impossible");
249 1.1 thorpej }
250 1.1 thorpej sc->sc_chip = tcp->tcp_chip;
251 1.1 thorpej
252 1.1 thorpej /*
253 1.1 thorpej * By default, Tulip registers are 8 bytes long (4 bytes
254 1.1 thorpej * followed by a 4 byte pad).
255 1.1 thorpej */
256 1.1 thorpej sc->sc_regshift = 3;
257 1.1 thorpej
258 1.1 thorpej /*
259 1.20 thorpej * Power management hooks.
260 1.20 thorpej */
261 1.20 thorpej sc->sc_enable = tlp_cardbus_enable;
262 1.20 thorpej sc->sc_disable = tlp_cardbus_disable;
263 1.21 thorpej sc->sc_power = tlp_cardbus_power;
264 1.20 thorpej
265 1.20 thorpej /*
266 1.1 thorpej * Get revision info, and set some chip-specific variables.
267 1.1 thorpej */
268 1.1 thorpej sc->sc_rev = PCI_REVISION(ca->ca_class);
269 1.1 thorpej switch (sc->sc_chip) {
270 1.1 thorpej case TULIP_CHIP_21142:
271 1.1 thorpej if (sc->sc_rev >= 0x20)
272 1.1 thorpej sc->sc_chip = TULIP_CHIP_21143;
273 1.1 thorpej break;
274 1.1 thorpej
275 1.25 thorpej case TULIP_CHIP_AN985:
276 1.25 thorpej /*
277 1.25 thorpej * The AN983 and AN985 are very similar, and are
278 1.25 thorpej * differentiated by a "signature" register that
279 1.25 thorpej * is like, but not identical, to a PCI ID register.
280 1.25 thorpej */
281 1.25 thorpej reg = cardbus_conf_read(ct->ct_cc, ct->ct_cf, csc->sc_tag,
282 1.25 thorpej 0x80);
283 1.25 thorpej switch (reg) {
284 1.25 thorpej case 0x09811317:
285 1.25 thorpej sc->sc_chip = TULIP_CHIP_AN985;
286 1.25 thorpej break;
287 1.25 thorpej
288 1.25 thorpej case 0x09851317:
289 1.25 thorpej sc->sc_chip = TULIP_CHIP_AN983;
290 1.25 thorpej break;
291 1.25 thorpej
292 1.25 thorpej }
293 1.25 thorpej break;
294 1.25 thorpej
295 1.29 onoe default:
296 1.29 onoe /* Nothing. -- to make gcc happy */
297 1.30 thorpej break;
298 1.1 thorpej }
299 1.1 thorpej
300 1.1 thorpej printf(": %s Ethernet, pass %d.%d\n",
301 1.9 thorpej tlp_chip_names[sc->sc_chip],
302 1.1 thorpej (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
303 1.2 thorpej
304 1.2 thorpej /*
305 1.1 thorpej * Map the device.
306 1.1 thorpej */
307 1.33 mycroft csc->sc_csr = CARDBUS_COMMAND_MASTER_ENABLE;
308 1.33 mycroft if (Cardbus_mapreg_map(ct, TULIP_PCI_MMBA,
309 1.33 mycroft CARDBUS_MAPREG_TYPE_MEM, 0, &sc->sc_st, &sc->sc_sh, &adr,
310 1.33 mycroft &csc->sc_mapsize) == 0) {
311 1.33 mycroft #if rbus
312 1.33 mycroft #else
313 1.33 mycroft (*ct->ct_cf->cardbus_mem_open)(cc, 0, adr, adr+csc->sc_mapsize);
314 1.33 mycroft #endif
315 1.33 mycroft csc->sc_csr |= CARDBUS_COMMAND_MEM_ENABLE;
316 1.33 mycroft csc->sc_bar_reg = TULIP_PCI_MMBA;
317 1.33 mycroft csc->sc_bar_val = adr | CARDBUS_MAPREG_TYPE_MEM;
318 1.33 mycroft } else if (Cardbus_mapreg_map(ct, TULIP_PCI_IOBA,
319 1.33 mycroft CARDBUS_MAPREG_TYPE_IO, 0, &sc->sc_st, &sc->sc_sh, &adr,
320 1.19 thorpej &csc->sc_mapsize) == 0) {
321 1.19 thorpej #if rbus
322 1.19 thorpej #else
323 1.19 thorpej (*ct->ct_cf->cardbus_io_open)(cc, 0, adr, adr+csc->sc_mapsize);
324 1.19 thorpej #endif
325 1.33 mycroft csc->sc_csr |= CARDBUS_COMMAND_IO_ENABLE;
326 1.19 thorpej csc->sc_bar_reg = TULIP_PCI_IOBA;
327 1.33 mycroft csc->sc_bar_val = adr | CARDBUS_MAPREG_TYPE_IO;
328 1.1 thorpej } else {
329 1.61 cegger aprint_error_dev(self, "unable to map device registers\n");
330 1.1 thorpej return;
331 1.1 thorpej }
332 1.1 thorpej
333 1.7 thorpej /*
334 1.18 thorpej * Bring the chip out of powersave mode and initialize the
335 1.18 thorpej * configuration registers.
336 1.7 thorpej */
337 1.18 thorpej tlp_cardbus_setup(csc);
338 1.1 thorpej
339 1.1 thorpej /*
340 1.10 thorpej * Read the contents of the Ethernet Address ROM/SROM.
341 1.1 thorpej */
342 1.10 thorpej switch (sc->sc_chip) {
343 1.10 thorpej case TULIP_CHIP_X3201_3:
344 1.10 thorpej /*
345 1.10 thorpej * No SROM on this chip.
346 1.10 thorpej */
347 1.10 thorpej break;
348 1.10 thorpej
349 1.10 thorpej default:
350 1.15 mycroft if (tlp_read_srom(sc) == 0)
351 1.15 mycroft goto cant_cope;
352 1.15 mycroft break;
353 1.1 thorpej }
354 1.1 thorpej
355 1.1 thorpej /*
356 1.1 thorpej * Deal with chip/board quirks. This includes setting up
357 1.1 thorpej * the mediasw, and extracting the Ethernet address from
358 1.1 thorpej * the rombuf.
359 1.1 thorpej */
360 1.1 thorpej switch (sc->sc_chip) {
361 1.1 thorpej case TULIP_CHIP_21142:
362 1.1 thorpej case TULIP_CHIP_21143:
363 1.1 thorpej /* Check for new format SROM. */
364 1.24 thorpej if (tlp_isv_srom_enaddr(sc, enaddr) != 0) {
365 1.1 thorpej /*
366 1.1 thorpej * We start out with the 2114x ISV media switch.
367 1.1 thorpej * When we search for quirks, we may change to
368 1.1 thorpej * a different switch.
369 1.1 thorpej */
370 1.1 thorpej sc->sc_mediasw = &tlp_2114x_isv_mediasw;
371 1.24 thorpej } else if (tlp_parse_old_srom(sc, enaddr) == 0) {
372 1.23 thorpej /*
373 1.24 thorpej * Not an ISV SROM, and not in old DEC Address
374 1.24 thorpej * ROM format. Try to snarf it out of the CIS.
375 1.23 thorpej */
376 1.23 thorpej if (ca->ca_cis.funce.network.netid_present == 0)
377 1.23 thorpej goto cant_cope;
378 1.23 thorpej
379 1.23 thorpej /* Grab the MAC address from the CIS. */
380 1.23 thorpej memcpy(enaddr, ca->ca_cis.funce.network.netid,
381 1.23 thorpej sizeof(enaddr));
382 1.24 thorpej }
383 1.23 thorpej
384 1.33 mycroft /*
385 1.33 mycroft * Deal with any quirks this board might have.
386 1.33 mycroft */
387 1.33 mycroft tlp_cardbus_get_quirks(csc, enaddr, tlp_cardbus_21142_quirks);
388 1.24 thorpej
389 1.24 thorpej /*
390 1.24 thorpej * If we don't already have a media switch, default to
391 1.24 thorpej * MII-over-SIO, with no special reset routine.
392 1.24 thorpej */
393 1.24 thorpej if (sc->sc_mediasw == NULL) {
394 1.24 thorpej printf("%s: defaulting to MII-over-SIO; no bets...\n",
395 1.61 cegger device_xname(self));
396 1.23 thorpej sc->sc_mediasw = &tlp_sio_mii_mediasw;
397 1.23 thorpej }
398 1.25 thorpej break;
399 1.25 thorpej
400 1.25 thorpej case TULIP_CHIP_AN983:
401 1.25 thorpej case TULIP_CHIP_AN985:
402 1.25 thorpej /*
403 1.25 thorpej * The ADMtek AN985's Ethernet address is located
404 1.25 thorpej * at offset 8 of its EEPROM.
405 1.25 thorpej */
406 1.25 thorpej memcpy(enaddr, &sc->sc_srom[8], ETHER_ADDR_LEN);
407 1.25 thorpej
408 1.25 thorpej /*
409 1.25 thorpej * The ADMtek AN985 can be configured in Single-Chip
410 1.25 thorpej * mode or MAC-only mode. Single-Chip uses the built-in
411 1.25 thorpej * PHY, MAC-only has an external PHY (usually HomePNA).
412 1.25 thorpej * The selection is based on an EEPROM setting, and both
413 1.25 thorpej * PHYs are access via MII attached to SIO.
414 1.25 thorpej *
415 1.25 thorpej * The AN985 "ghosts" the internal PHY onto all
416 1.25 thorpej * MII addresses, so we have to use a media init
417 1.25 thorpej * routine that limits the search.
418 1.25 thorpej * XXX How does this work with MAC-only mode?
419 1.25 thorpej */
420 1.25 thorpej sc->sc_mediasw = &tlp_an985_mediasw;
421 1.1 thorpej break;
422 1.1 thorpej
423 1.10 thorpej case TULIP_CHIP_X3201_3:
424 1.1 thorpej /*
425 1.15 mycroft * The X3201 doesn't have an SROM. Lift the MAC address
426 1.10 thorpej * from the CIS. Also, we have a special media switch:
427 1.10 thorpej * MII-on-SIO, plus some special GPIO setup.
428 1.1 thorpej */
429 1.10 thorpej memcpy(enaddr, ca->ca_cis.funce.network.netid, sizeof(enaddr));
430 1.10 thorpej sc->sc_reset = tlp_cardbus_x3201_reset;
431 1.10 thorpej sc->sc_mediasw = &tlp_sio_mii_mediasw;
432 1.10 thorpej break;
433 1.1 thorpej
434 1.10 thorpej default:
435 1.10 thorpej cant_cope:
436 1.1 thorpej printf("%s: sorry, unable to handle your board\n",
437 1.61 cegger device_xname(self));
438 1.1 thorpej return;
439 1.1 thorpej }
440 1.1 thorpej
441 1.20 thorpej /* Remember which interrupt line. */
442 1.20 thorpej csc->sc_intrline = ca->ca_intrline;
443 1.20 thorpej
444 1.1 thorpej /*
445 1.20 thorpej * Finish off the attach.
446 1.1 thorpej */
447 1.20 thorpej tlp_attach(sc, enaddr);
448 1.1 thorpej
449 1.1 thorpej /*
450 1.20 thorpej * Power down the socket.
451 1.1 thorpej */
452 1.20 thorpej Cardbus_function_disable(csc->sc_ct);
453 1.11 thorpej }
454 1.11 thorpej
455 1.11 thorpej int
456 1.61 cegger tlp_cardbus_detach(device_t self, int flags)
457 1.11 thorpej {
458 1.49 thorpej struct tulip_cardbus_softc *csc = device_private(self);
459 1.11 thorpej struct tulip_softc *sc = &csc->sc_tulip;
460 1.13 augustss struct cardbus_devfunc *ct = csc->sc_ct;
461 1.11 thorpej int rv;
462 1.12 haya
463 1.13 augustss #if defined(DIAGNOSTIC)
464 1.20 thorpej if (ct == NULL)
465 1.61 cegger panic("%s: data structure lacks", device_xname(self));
466 1.12 haya #endif
467 1.11 thorpej
468 1.20 thorpej rv = tlp_detach(sc);
469 1.20 thorpej if (rv)
470 1.20 thorpej return (rv);
471 1.15 mycroft
472 1.15 mycroft /*
473 1.15 mycroft * Unhook the interrupt handler.
474 1.15 mycroft */
475 1.20 thorpej if (csc->sc_ih != NULL)
476 1.20 thorpej cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, csc->sc_ih);
477 1.12 haya
478 1.15 mycroft /*
479 1.20 thorpej * Release bus space and close window.
480 1.15 mycroft */
481 1.22 thorpej if (csc->sc_bar_reg != 0)
482 1.22 thorpej Cardbus_mapreg_unmap(ct, csc->sc_bar_reg,
483 1.22 thorpej sc->sc_st, sc->sc_sh, csc->sc_mapsize);
484 1.12 haya
485 1.15 mycroft return (0);
486 1.20 thorpej }
487 1.20 thorpej
488 1.20 thorpej int
489 1.60 dsl tlp_cardbus_enable(struct tulip_softc *sc)
490 1.20 thorpej {
491 1.20 thorpej struct tulip_cardbus_softc *csc = (void *) sc;
492 1.20 thorpej cardbus_devfunc_t ct = csc->sc_ct;
493 1.20 thorpej cardbus_chipset_tag_t cc = ct->ct_cc;
494 1.20 thorpej cardbus_function_tag_t cf = ct->ct_cf;
495 1.20 thorpej
496 1.20 thorpej /*
497 1.20 thorpej * Power on the socket.
498 1.20 thorpej */
499 1.20 thorpej Cardbus_function_enable(ct);
500 1.20 thorpej
501 1.20 thorpej /*
502 1.20 thorpej * Set up the PCI configuration registers.
503 1.20 thorpej */
504 1.20 thorpej tlp_cardbus_setup(csc);
505 1.20 thorpej
506 1.20 thorpej /*
507 1.20 thorpej * Map and establish the interrupt.
508 1.20 thorpej */
509 1.20 thorpej csc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline, IPL_NET,
510 1.20 thorpej tlp_intr, sc);
511 1.20 thorpej if (csc->sc_ih == NULL) {
512 1.61 cegger aprint_error_dev(sc->sc_dev,
513 1.59 drochner "unable to establish interrupt\n");
514 1.20 thorpej Cardbus_function_disable(csc->sc_ct);
515 1.20 thorpej return (1);
516 1.20 thorpej }
517 1.20 thorpej return (0);
518 1.20 thorpej }
519 1.20 thorpej
520 1.20 thorpej void
521 1.60 dsl tlp_cardbus_disable(struct tulip_softc *sc)
522 1.20 thorpej {
523 1.20 thorpej struct tulip_cardbus_softc *csc = (void *) sc;
524 1.20 thorpej cardbus_devfunc_t ct = csc->sc_ct;
525 1.20 thorpej cardbus_chipset_tag_t cc = ct->ct_cc;
526 1.20 thorpej cardbus_function_tag_t cf = ct->ct_cf;
527 1.20 thorpej
528 1.20 thorpej /* Unhook the interrupt handler. */
529 1.20 thorpej cardbus_intr_disestablish(cc, cf, csc->sc_ih);
530 1.20 thorpej csc->sc_ih = NULL;
531 1.20 thorpej
532 1.20 thorpej /* Power down the socket. */
533 1.20 thorpej Cardbus_function_disable(ct);
534 1.21 thorpej }
535 1.21 thorpej
536 1.21 thorpej void
537 1.60 dsl tlp_cardbus_power(struct tulip_softc *sc, int why)
538 1.21 thorpej {
539 1.21 thorpej
540 1.43 mycroft switch (why) {
541 1.43 mycroft case PWR_RESUME:
542 1.43 mycroft tlp_cardbus_enable(sc);
543 1.43 mycroft break;
544 1.43 mycroft case PWR_SUSPEND:
545 1.43 mycroft tlp_cardbus_disable(sc);
546 1.43 mycroft break;
547 1.21 thorpej }
548 1.17 thorpej }
549 1.17 thorpej
550 1.17 thorpej void
551 1.60 dsl tlp_cardbus_setup(struct tulip_cardbus_softc *csc)
552 1.17 thorpej {
553 1.17 thorpej struct tulip_softc *sc = &csc->sc_tulip;
554 1.17 thorpej cardbus_devfunc_t ct = csc->sc_ct;
555 1.17 thorpej cardbus_chipset_tag_t cc = ct->ct_cc;
556 1.17 thorpej cardbus_function_tag_t cf = ct->ct_cf;
557 1.17 thorpej pcireg_t reg;
558 1.17 thorpej
559 1.17 thorpej /*
560 1.17 thorpej * Check to see if the device is in power-save mode, and
561 1.17 thorpej * bring it out if necessary.
562 1.17 thorpej */
563 1.17 thorpej switch (sc->sc_chip) {
564 1.17 thorpej case TULIP_CHIP_21142:
565 1.17 thorpej case TULIP_CHIP_21143:
566 1.17 thorpej case TULIP_CHIP_X3201_3:
567 1.17 thorpej /*
568 1.17 thorpej * Clear the "sleep mode" bit in the CFDA register.
569 1.17 thorpej */
570 1.17 thorpej reg = cardbus_conf_read(cc, cf, csc->sc_tag, TULIP_PCI_CFDA);
571 1.17 thorpej if (reg & (CFDA_SLEEP|CFDA_SNOOZE))
572 1.17 thorpej cardbus_conf_write(cc, cf, csc->sc_tag, TULIP_PCI_CFDA,
573 1.17 thorpej reg & ~(CFDA_SLEEP|CFDA_SNOOZE));
574 1.17 thorpej break;
575 1.29 onoe
576 1.29 onoe default:
577 1.29 onoe /* Nothing. -- to make gcc happy */
578 1.30 thorpej break;
579 1.17 thorpej }
580 1.17 thorpej
581 1.55 jmcneill (void)cardbus_set_powerstate(ct, csc->sc_tag, PCI_PWR_D0);
582 1.18 thorpej
583 1.42 mycroft /* Program the BAR. */
584 1.42 mycroft cardbus_conf_write(cc, cf, csc->sc_tag, csc->sc_bar_reg,
585 1.42 mycroft csc->sc_bar_val);
586 1.42 mycroft
587 1.18 thorpej /* Enable the appropriate bits in the PCI CSR. */
588 1.18 thorpej reg = cardbus_conf_read(cc, cf, csc->sc_tag, PCI_COMMAND_STATUS_REG);
589 1.18 thorpej reg &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE);
590 1.18 thorpej reg |= csc->sc_csr;
591 1.18 thorpej cardbus_conf_write(cc, cf, csc->sc_tag, PCI_COMMAND_STATUS_REG, reg);
592 1.18 thorpej
593 1.18 thorpej /*
594 1.18 thorpej * Make sure the latency timer is set to some reasonable
595 1.18 thorpej * value.
596 1.18 thorpej */
597 1.18 thorpej reg = cardbus_conf_read(cc, cf, csc->sc_tag, PCI_BHLC_REG);
598 1.18 thorpej if (PCI_LATTIMER(reg) < 0x20) {
599 1.18 thorpej reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
600 1.18 thorpej reg |= (0x20 << PCI_LATTIMER_SHIFT);
601 1.18 thorpej cardbus_conf_write(cc, cf, csc->sc_tag, PCI_BHLC_REG, reg);
602 1.17 thorpej }
603 1.10 thorpej }
604 1.10 thorpej
605 1.10 thorpej void
606 1.60 dsl tlp_cardbus_x3201_reset(struct tulip_softc *sc)
607 1.10 thorpej {
608 1.10 thorpej u_int32_t reg;
609 1.10 thorpej
610 1.10 thorpej reg = TULIP_READ(sc, CSR_SIAGEN);
611 1.10 thorpej
612 1.10 thorpej /* make GP[2,0] outputs */
613 1.10 thorpej TULIP_WRITE(sc, CSR_SIAGEN, (reg & ~SIAGEN_MD) | SIAGEN_CWE |
614 1.10 thorpej 0x00050000);
615 1.10 thorpej TULIP_WRITE(sc, CSR_SIAGEN, (reg & ~SIAGEN_CWE) | SIAGEN_MD);
616 1.33 mycroft }
617 1.33 mycroft
618 1.33 mycroft void
619 1.52 christos tlp_cardbus_lxt_quirks(struct tulip_cardbus_softc *csc,
620 1.53 christos const u_int8_t *enaddr)
621 1.33 mycroft {
622 1.33 mycroft struct tulip_softc *sc = &csc->sc_tulip;
623 1.33 mycroft
624 1.33 mycroft sc->sc_mediasw = &tlp_sio_mii_mediasw;
625 1.1 thorpej }
626