if_re_cardbus.c revision 1.3 1 /* $NetBSD: if_re_cardbus.c,v 1.3 2005/01/13 14:13:18 kanaoka Exp $ */
2
3 /*
4 * Copyright (c) 2004 Jonathan Stone
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /*
31 * if_re_cardbus.c:
32 * Cardbus specific routines for Realtek 8169 ethernet adapter.
33 * Tested for :
34 * Netgear GA-511 (8169S)
35 * Buffalo LPC-CB-CLGT
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: if_re_cardbus.c,v 1.3 2005/01/13 14:13:18 kanaoka Exp $");
40
41 #include "opt_inet.h"
42 #include "opt_ns.h"
43 #include "bpfilter.h"
44 #include "rnd.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/callout.h>
49 #include <sys/device.h>
50 #include <sys/sockio.h>
51 #include <sys/mbuf.h>
52 #include <sys/malloc.h>
53 #include <sys/kernel.h>
54 #include <sys/socket.h>
55
56 #include <net/if.h>
57 #include <net/if_arp.h>
58 #include <net/if_ether.h>
59 #include <net/if_dl.h>
60 #include <net/if_media.h>
61 #ifdef INET
62 #include <netinet/in.h>
63 #include <netinet/if_inarp.h>
64 #endif
65 #ifdef NS
66 #include <netns/ns.h>
67 #include <netns/ns_if.h>
68 #endif
69
70 #if NBPFILTER > 0
71 #include <net/bpf.h>
72 #endif
73 #if NRND > 0
74 #include <sys/rnd.h>
75 #endif
76
77 #include <machine/bus.h>
78
79 #include <dev/pci/pcireg.h>
80 #include <dev/pci/pcivar.h>
81 #include <dev/pci/pcidevs.h>
82
83 #include <dev/cardbus/cardbusvar.h>
84 #include <dev/pci/pcidevs.h>
85
86 #include <dev/mii/mii.h>
87 #include <dev/mii/miivar.h>
88
89 /*
90 * Default to using PIO access for this driver. On SMP systems,
91 * there appear to be problems with memory mapped mode: it looks like
92 * doing too many memory mapped access back to back in rapid succession
93 * can hang the bus. I'm inclined to blame this on crummy design/construction
94 * on the part of Realtek. Memory mapped mode does appear to work on
95 * uniprocessor systems though.
96 */
97 #define RTK_USEIOSPACE
98
99 #include <dev/ic/rtl81x9reg.h>
100 #include <dev/ic/rtl81x9var.h>
101
102 #include <dev/ic/rtl8169var.h>
103
104 /*
105 * Various supported device vendors/types and their names.
106 */
107 static const struct rtk_type re_cardbus_devs[] = {
108 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169,
109 RTK_8169, "Realtek 10/100/1000baseT" },
110 { 0, 0, 0, NULL }
111 };
112
113 static int re_cardbus_match(struct device *, struct cfdata *, void *);
114 static void re_cardbus_attach(struct device *, struct device *, void *);
115 static int re_cardbus_detach(struct device *, int);
116
117 struct re_cardbus_softc {
118 struct rtk_softc sc_rtk; /* real rtk softc */
119
120 /* CardBus-specific goo. */
121 void *sc_ih;
122 cardbus_devfunc_t sc_ct;
123 cardbustag_t sc_tag;
124 int sc_csr;
125 int sc_cben;
126 int sc_bar_reg;
127 pcireg_t sc_bar_val;
128 bus_size_t sc_mapsize;
129 int sc_intrline;
130 };
131
132 CFATTACH_DECL(re_cardbus, sizeof(struct re_cardbus_softc),
133 re_cardbus_match, re_cardbus_attach, re_cardbus_detach, re_activate);
134
135 const struct rtk_type *re_cardbus_lookup
136 __P((const struct cardbus_attach_args *));
137
138 void re_cardbus_setup __P((struct re_cardbus_softc *));
139
140 int re_cardbus_enable __P((struct rtk_softc *));
141 void re_cardbus_disable __P((struct rtk_softc *));
142 void re_cardbus_power __P((struct rtk_softc *, int));
143
144 const struct rtk_type *
145 re_cardbus_lookup(ca)
146 const struct cardbus_attach_args *ca;
147 {
148 const struct rtk_type *t;
149
150 for (t = re_cardbus_devs; t->rtk_name != NULL; t++) {
151 if (CARDBUS_VENDOR(ca->ca_id) == t->rtk_vid &&
152 CARDBUS_PRODUCT(ca->ca_id) == t->rtk_did) {
153 return (t);
154 }
155 }
156 return (NULL);
157 }
158
159 int
160 re_cardbus_match(parent, match, aux)
161 struct device *parent;
162 struct cfdata *match;
163 void *aux;
164 {
165 struct cardbus_attach_args *ca = aux;
166
167 if (re_cardbus_lookup(ca) != NULL)
168 return (1);
169
170 return (0);
171 }
172
173
174 void
175 re_cardbus_attach(struct device *parent, struct device *self, void *aux)
176 {
177 struct re_cardbus_softc *csc = (struct re_cardbus_softc *)self;
178 struct rtk_softc *sc = &csc->sc_rtk;
179 struct cardbus_attach_args *ca = aux;
180 cardbus_devfunc_t ct = ca->ca_ct;
181 const struct rtk_type *t;
182 bus_addr_t adr;
183
184 sc->sc_dmat = ca->ca_dmat;
185 csc->sc_ct = ct;
186 csc->sc_tag = ca->ca_tag;
187 csc->sc_intrline = ca->ca_intrline;
188
189 t = re_cardbus_lookup(ca);
190 if (t == NULL) {
191 printf("\n");
192 panic("re_cardbus_attach: impossible");
193 }
194 printf(": %s\n", t->rtk_name);
195
196 /*
197 * Power management hooks.
198 */
199 sc->sc_enable = re_cardbus_enable;
200 sc->sc_disable = re_cardbus_disable;
201 sc->sc_power = re_cardbus_power;
202
203 /*
204 * Map control/status registers.
205 */
206 csc->sc_csr = CARDBUS_COMMAND_MASTER_ENABLE;
207 #ifdef RTK_USEIOSPACE
208 if (Cardbus_mapreg_map(ct, RTK_PCI_LOIO, CARDBUS_MAPREG_TYPE_IO, 0,
209 &sc->rtk_btag, &sc->rtk_bhandle, &adr, &csc->sc_mapsize) == 0) {
210 #if rbus
211 #else
212 (*ct->ct_cf->cardbus_io_open)(cc, 0, adr, adr+csc->sc_mapsize);
213 #endif
214 csc->sc_cben = CARDBUS_IO_ENABLE;
215 csc->sc_csr |= CARDBUS_COMMAND_IO_ENABLE;
216 csc->sc_bar_reg = RTK_PCI_LOIO;
217 csc->sc_bar_val = adr | CARDBUS_MAPREG_TYPE_IO;
218 }
219 #else
220 if (Cardbus_mapreg_map(ct, RTK_PCI_LOMEM, CARDBUS_MAPREG_TYPE_MEM, 0,
221 &sc->rtk_btag, &sc->rtk_bhandle, &adr, &csc->sc_mapsize) == 0) {
222 #if rbus
223 #else
224 (*ct->ct_cf->cardbus_mem_open)(cc, 0, adr, adr+csc->sc_mapsize);
225 #endif
226 csc->sc_cben = CARDBUS_MEM_ENABLE;
227 csc->sc_csr |= CARDBUS_COMMAND_MEM_ENABLE;
228 csc->sc_bar_reg = RTK_PCI_LOMEM;
229 csc->sc_bar_val = adr | CARDBUS_MAPREG_TYPE_MEM;
230 }
231 #endif
232 else {
233 printf("%s: unable to map deviceregisters\n",
234 sc->sc_dev.dv_xname);
235 return;
236 }
237 /*
238 * Handle power management nonsense and initialize the
239 * configuration registers.
240 */
241 re_cardbus_setup(csc);
242
243 sc->rtk_type = t->rtk_basetype;
244 sc->sc_dmat = ca->ca_dmat;
245 re_attach(sc);
246
247 /*
248 * Power down the socket.
249 */
250 Cardbus_function_disable(csc->sc_ct);
251 }
252
253 int
254 re_cardbus_detach(struct device *self, int flags)
255 {
256 struct re_cardbus_softc *csc = (void *) self;
257 struct rtk_softc *sc = &csc->sc_rtk;
258 struct cardbus_devfunc *ct = csc->sc_ct;
259 int rv;
260
261 #ifdef DIAGNOSTIC
262 if (ct == NULL)
263 panic("%s: cardbus softc, cardbus_devfunc NULL",
264 sc->sc_dev.dv_xname);
265 #endif
266 rv = re_detach(sc);
267 if (rv)
268 return (rv);
269 /*
270 * Unhook the interrupt handler.
271 */
272 if (csc->sc_ih != NULL)
273 cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, csc->sc_ih);
274
275 /*
276 * Release bus space and close window.
277 */
278 if (csc->sc_bar_reg != 0)
279 Cardbus_mapreg_unmap(ct, csc->sc_bar_reg,
280 sc->rtk_btag, sc->rtk_bhandle, csc->sc_mapsize);
281
282 return (0);
283 }
284
285 void
286 re_cardbus_setup(struct re_cardbus_softc *csc)
287 {
288 struct rtk_softc *sc = &csc->sc_rtk;
289 cardbus_devfunc_t ct = csc->sc_ct;
290 cardbus_chipset_tag_t cc = ct->ct_cc;
291 cardbus_function_tag_t cf = ct->ct_cf;
292 pcireg_t reg,command;
293 int pmreg;
294
295 /*
296 * Handle power management nonsense.
297 */
298 if (cardbus_get_capability(cc, cf, csc->sc_tag,
299 PCI_CAP_PWRMGMT, &pmreg, 0)) {
300 command = cardbus_conf_read(cc, cf, csc->sc_tag,
301 pmreg + PCI_PMCSR);
302 if (command & RTK_PSTATE_MASK) {
303 pcireg_t iobase, membase, irq;
304
305 /* Save important PCI config data. */
306 iobase = cardbus_conf_read(cc, cf, csc->sc_tag,
307 RTK_PCI_LOIO);
308 membase = cardbus_conf_read(cc, cf,csc->sc_tag,
309 RTK_PCI_LOMEM);
310 irq = cardbus_conf_read(cc, cf,csc->sc_tag,
311 CARDBUS_INTERRUPT_REG);
312
313 /* Reset the power state. */
314 printf("%s: chip is in D%d power mode "
315 "-- setting to D0\n", sc->sc_dev.dv_xname,
316 command & RTK_PSTATE_MASK);
317 command &= ~RTK_PSTATE_MASK;
318 cardbus_conf_write(cc, cf, csc->sc_tag,
319 pmreg + PCI_PMCSR, command);
320
321 /* Restore PCI config data. */
322 cardbus_conf_write(cc, cf, csc->sc_tag,
323 RTK_PCI_LOIO, iobase);
324 cardbus_conf_write(cc, cf, csc->sc_tag,
325 RTK_PCI_LOMEM, membase);
326 cardbus_conf_write(cc, cf, csc->sc_tag,
327 CARDBUS_INTERRUPT_REG, irq);
328 }
329 }
330
331 /* Program the BAR */
332 cardbus_conf_write(cc, cf, csc->sc_tag,
333 csc->sc_bar_reg, csc->sc_bar_val);
334
335 /* Make sure the right access type is on the CardBus bridge. */
336 (*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cben);
337 (*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
338
339 /* Enable the appropriate bits in the CARDBUS CSR. */
340 reg = cardbus_conf_read(cc, cf, csc->sc_tag,
341 CARDBUS_COMMAND_STATUS_REG);
342 reg &= ~(CARDBUS_COMMAND_IO_ENABLE|CARDBUS_COMMAND_MEM_ENABLE);
343 reg |= csc->sc_csr;
344 cardbus_conf_write(cc, cf, csc->sc_tag,
345 CARDBUS_COMMAND_STATUS_REG, reg);
346
347 /*
348 * Make sure the latency timer is set to some reasonable
349 * value.
350 */
351 reg = cardbus_conf_read(cc, cf, csc->sc_tag, CARDBUS_BHLC_REG);
352 if (CARDBUS_LATTIMER(reg) < 0x40) {
353 reg &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
354 reg |= (0x40 << CARDBUS_LATTIMER_SHIFT);
355 cardbus_conf_write(cc, cf, csc->sc_tag, CARDBUS_BHLC_REG, reg);
356 }
357 }
358
359 int
360 re_cardbus_enable(struct rtk_softc *sc)
361 {
362 struct re_cardbus_softc *csc = (void *) sc;
363 cardbus_devfunc_t ct = csc->sc_ct;
364 cardbus_chipset_tag_t cc = ct->ct_cc;
365 cardbus_function_tag_t cf = ct->ct_cf;
366
367 /*
368 * Power on the socket.
369 */
370 Cardbus_function_enable(ct);
371
372 /*
373 * Set up the PCI configuration registers.
374 */
375 re_cardbus_setup(csc);
376
377 /*
378 * Map and establish the interrupt.
379 */
380 csc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline,
381 IPL_NET, re_intr, sc);
382 if (csc->sc_ih == NULL) {
383 printf("%s: unable to establish interrupt at %d\n",
384 sc->sc_dev.dv_xname, csc->sc_intrline);
385 Cardbus_function_disable(csc->sc_ct);
386 return (1);
387 }
388 printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
389 csc->sc_intrline);
390 return (0);
391 }
392
393 void
394 re_cardbus_disable(struct rtk_softc *sc)
395 {
396 struct re_cardbus_softc *csc = (void *) sc;
397 cardbus_devfunc_t ct = csc->sc_ct;
398 cardbus_chipset_tag_t cc = ct->ct_cc;
399 cardbus_function_tag_t cf = ct->ct_cf;
400
401 /* Unhook the interrupt handler. */
402 cardbus_intr_disestablish(cc, cf, csc->sc_ih);
403 csc->sc_ih = NULL;
404
405 /* Power down the socket. */
406 Cardbus_function_disable(ct);
407 }
408
409 void
410 re_cardbus_power(struct rtk_softc *sc, int why)
411 {
412 struct re_cardbus_softc *csc = (void *) sc;
413
414 if (why == PWR_RESUME) {
415 /*
416 * Give the PCI configuration registers a kick
417 * in the head.
418 */
419 #ifdef DIAGNOSTIC
420 if (RTK_IS_ENABLED(sc) == 0)
421 panic("re_cardbus_power");
422 #endif
423 re_cardbus_setup(csc);
424 }
425 }
426