xref: /netbsd/sys/dev/cardbus/if_rtk_cardbus.c (revision c4a72b64)
1 /*	$NetBSD: if_rtk_cardbus.c,v 1.12 2002/11/11 14:21:18 kanaoka Exp $	*/
2 
3 /*
4  * Copyright (c) 2000 Masanori Kanaoka
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_rtk_cardbus.c:
32  *	Cardbus specific routines for RealTek 8139 ethernet adapter.
33  *	Tested for
34  *		- elecom-Laneed	LD-10/100CBA (Accton MPX5030)
35  *		- MELCO		LPC3-TX-CB   (RealTek 8139)
36  */
37 
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: if_rtk_cardbus.c,v 1.12 2002/11/11 14:21: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/cardbus/cardbusdevs.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 /*
103  * Various supported device vendors/types and their names.
104  */
105 static const struct rtk_type rtk_cardbus_devs[] = {
106 	{ CARDBUS_VENDOR_ACCTON, CARDBUS_PRODUCT_ACCTON_MPX5030,
107 		"Accton MPX 5030/5038 10/100BaseTX",
108 		RTK_8139 },
109 	{ CARDBUS_VENDOR_DLINK, CARDBUS_PRODUCT_DLINK_DFE_690TXD,
110 		"D-Link DFE-690TXD 10/100BaseTX", RTK_8139 },
111 	{ CARDBUS_VENDOR_REALTEK, CARDBUS_PRODUCT_REALTEK_RT8138,
112 		"RealTek 8138 10/100BaseTX", RTK_8139 },
113 	{ CARDBUS_VENDOR_REALTEK, CARDBUS_PRODUCT_REALTEK_RT8139,
114 		"RealTek 8139 10/100BaseTX", RTK_8139 },
115 	{ CARDBUS_VENDOR_COREGA, CARDBUS_PRODUCT_COREGA_CB_TXD,
116 		"Corega FEther CB-TXD 10/100BaseTX", RTK_8139 },
117 	{ CARDBUS_VENDOR_PLANEX, CARDBUS_PRODUCT_PLANEX_FNW_3603_TX,
118 		"Planex FNW-3603 10/100BaseTX", RTK_8139 },
119 	{ 0, 0, NULL, 0 }
120 };
121 
122 static int rtk_cardbus_match	__P((struct device *, struct cfdata *, void *));
123 static void rtk_cardbus_attach	__P((struct device *, struct device *, void *));
124 static int rtk_cardbus_detach	__P((struct device *, int));
125 
126 struct rtk_cardbus_softc {
127 	struct rtk_softc sc_rtk;	/* real rtk softc */
128 
129 	/* CardBus-specific goo. */
130 	void *sc_ih;
131 	cardbus_devfunc_t sc_ct;
132 	cardbustag_t sc_tag;
133 	int sc_csr;
134 	int sc_cben;
135 	int sc_bar_reg;
136 	pcireg_t sc_bar_val;
137 	bus_size_t sc_mapsize;
138 	int sc_intrline;
139 };
140 
141 CFATTACH_DECL(rtk_cardbus, sizeof(struct rtk_cardbus_softc),
142     rtk_cardbus_match, rtk_cardbus_attach, rtk_cardbus_detach, rtk_activate);
143 
144 const struct rtk_type *rtk_cardbus_lookup
145 	__P((const struct cardbus_attach_args *));
146 
147 void rtk_cardbus_setup		__P((struct rtk_cardbus_softc *));
148 
149 int rtk_cardbus_enable		__P((struct rtk_softc *));
150 void rtk_cardbus_disable	__P((struct rtk_softc *));
151 void rtk_cardbus_power		__P((struct rtk_softc *, int));
152 const struct rtk_type *
153 rtk_cardbus_lookup(ca)
154 	const struct cardbus_attach_args *ca;
155 {
156 	const struct rtk_type *t;
157 
158 	for (t = rtk_cardbus_devs; t->rtk_name != NULL; t++){
159 		if (CARDBUS_VENDOR(ca->ca_id) == t->rtk_vid &&
160 		    CARDBUS_PRODUCT(ca->ca_id) == t->rtk_did) {
161 			return (t);
162 		}
163 	}
164 	return (NULL);
165 }
166 
167 int
168 rtk_cardbus_match(parent, match, aux)
169 	struct device *parent;
170 	struct cfdata *match;
171 	void *aux;
172 {
173 	struct cardbus_attach_args *ca = aux;
174 
175 	if (rtk_cardbus_lookup(ca) != NULL)
176 		return (1);
177 
178 	return (0);
179 }
180 
181 
182 void
183 rtk_cardbus_attach(parent, self, aux)
184 	struct device *parent, *self;
185 	void *aux;
186 {
187 	struct rtk_cardbus_softc *csc = (struct rtk_cardbus_softc *)self;
188 	struct rtk_softc *sc = &csc->sc_rtk;
189 	struct cardbus_attach_args *ca = aux;
190 	cardbus_devfunc_t ct = ca->ca_ct;
191 	const struct rtk_type *t;
192 	bus_addr_t adr;
193 
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 	t = rtk_cardbus_lookup(ca);
200 	if (t == NULL) {
201 		printf("\n");
202 		panic("rtk_cardbus_attach: impossible");
203 	 }
204 	printf(": %s\n", t->rtk_name);
205 
206 	/*
207 	 * Power management hooks.
208 	 */
209 	sc->sc_enable = rtk_cardbus_enable;
210 	sc->sc_disable = rtk_cardbus_disable;
211 	sc->sc_power = rtk_cardbus_power;
212 
213 	/*
214 	 * Map control/status registers.
215 	 */
216 	csc->sc_csr = CARDBUS_COMMAND_MASTER_ENABLE;
217 #ifdef RTK_USEIOSPACE
218 	if (Cardbus_mapreg_map(ct, RTK_PCI_LOIO, CARDBUS_MAPREG_TYPE_IO, 0,
219 	    &sc->rtk_btag, &sc->rtk_bhandle, &adr, &csc->sc_mapsize) == 0) {
220 #if rbus
221 #else
222 		(*ct->ct_cf->cardbus_io_open)(cc, 0, adr, adr+csc->sc_mapsize);
223 #endif
224 		csc->sc_cben = CARDBUS_IO_ENABLE;
225 		csc->sc_csr |= CARDBUS_COMMAND_IO_ENABLE;
226 		csc->sc_bar_reg = RTK_PCI_LOIO;
227 		csc->sc_bar_val = adr | CARDBUS_MAPREG_TYPE_IO;
228 	}
229 #else
230 	if (Cardbus_mapreg_map(ct, RTK_PCI_LOMEM, CARDBUS_MAPREG_TYPE_MEM, 0,
231 	    &sc->rtk_btag, &sc->rtk_bhandle, &adr, &csc->sc_mapsize) == 0) {
232 #if rbus
233 #else
234 		(*ct->ct_cf->cardbus_mem_open)(cc, 0, adr, adr+csc->sc_mapsize);
235 #endif
236 		csc->sc_cben = CARDBUS_MEM_ENABLE;
237 		csc->sc_csr |= CARDBUS_COMMAND_MEM_ENABLE;
238 		csc->sc_bar_reg = RTK_PCI_LOMEM;
239 		csc->sc_bar_val = adr | CARDBUS_MAPREG_TYPE_MEM;
240 	}
241 #endif
242 	else {
243 		printf("%s: unable to map deviceregisters\n",
244 			 sc->sc_dev.dv_xname);
245 		return;
246 	}
247 	/*
248 	 * Handle power management nonsense and initialize the
249 	 * configuration registers.
250 	 */
251 	rtk_cardbus_setup(csc);
252 	sc->rtk_type = t->rtk_type;
253 
254 	rtk_attach(sc);
255 
256 	/*
257 	 * Power down the socket.
258 	 */
259 	Cardbus_function_disable(csc->sc_ct);
260 }
261 
262 int
263 rtk_cardbus_detach(self, flags)
264 	struct device *self;
265 	int flags;
266 {
267 	struct rtk_cardbus_softc *csc = (void *) self;
268 	struct rtk_softc *sc = &csc->sc_rtk;
269 	struct cardbus_devfunc *ct = csc->sc_ct;
270 	int	rv;
271 
272 #ifdef DIAGNOSTIC
273 	if (ct == NULL)
274 		panic("%s: data structure lacks", sc->sc_dev.dv_xname);
275 #endif
276 	rv = rtk_detach(sc);
277 	if (rv)
278 		return (rv);
279 	/*
280 	 * Unhook the interrut handler.
281 	 */
282 	if (csc->sc_ih != NULL)
283 		cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, csc->sc_ih);
284 
285 	/*
286 	 * Release bus space and close window.
287 	 */
288 	if (csc->sc_bar_reg != 0)
289 		Cardbus_mapreg_unmap(ct, csc->sc_bar_reg,
290 			sc->rtk_btag, sc->rtk_bhandle, csc->sc_mapsize);
291 
292 	return (0);
293 }
294 
295 void
296 rtk_cardbus_setup(csc)
297 	struct rtk_cardbus_softc *csc;
298 {
299 	struct rtk_softc *sc = &csc->sc_rtk;
300 	cardbus_devfunc_t ct = csc->sc_ct;
301 	cardbus_chipset_tag_t cc = ct->ct_cc;
302 	cardbus_function_tag_t cf = ct->ct_cf;
303 	pcireg_t	reg,command;
304 	int		pmreg;
305 
306 	/*
307 	 * Handle power management nonsense.
308 	 */
309 	if (cardbus_get_capability(cc, cf, csc->sc_tag,
310 	    PCI_CAP_PWRMGMT, &pmreg, 0)) {
311 		command = cardbus_conf_read(cc, cf, csc->sc_tag, pmreg + 4);
312 		if (command & RTK_PSTATE_MASK) {
313 			pcireg_t		iobase, membase, irq;
314 
315 			/* Save important PCI config data. */
316 			iobase = cardbus_conf_read(cc, cf, csc->sc_tag,
317 			    RTK_PCI_LOIO);
318 			membase = cardbus_conf_read(cc, cf,csc->sc_tag,
319 			    RTK_PCI_LOMEM);
320 			irq = cardbus_conf_read(cc, cf,csc->sc_tag,
321 			    PCI_PRODUCT_DELTA_8139);
322 
323 			/* Reset the power state. */
324 			printf("%s: chip is is in D%d power mode "
325 			    "-- setting to D0\n", sc->sc_dev.dv_xname,
326 			    command & RTK_PSTATE_MASK);
327 			command &= 0xFFFFFFFC;
328 			cardbus_conf_write(cc, cf, csc->sc_tag,
329 			    pmreg + 4, command);
330 
331 			/* Restore PCI config data. */
332 			cardbus_conf_write(cc, cf, csc->sc_tag,
333 			    RTK_PCI_LOIO, iobase);
334 			cardbus_conf_write(cc, cf, csc->sc_tag,
335 			    RTK_PCI_LOMEM, membase);
336 			cardbus_conf_write(cc, cf, csc->sc_tag,
337 			    PCI_PRODUCT_DELTA_8139, irq);
338 		}
339 	}
340 
341 	/* Make sure the right access type is on the CardBus bridge. */
342 	(*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cben);
343 	(*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
344 
345 	/* Program the BAR */
346 	cardbus_conf_write(cc, cf, csc->sc_tag,
347 		csc->sc_bar_reg, csc->sc_bar_val);
348 
349 	/* Enable the appropriate bits in the CARDBUS CSR. */
350 	reg = cardbus_conf_read(cc, cf, csc->sc_tag,
351 	    CARDBUS_COMMAND_STATUS_REG);
352 	reg &= ~(CARDBUS_COMMAND_IO_ENABLE|CARDBUS_COMMAND_MEM_ENABLE);
353 	reg |= csc->sc_csr;
354 	cardbus_conf_write(cc, cf, csc->sc_tag,
355 	    CARDBUS_COMMAND_STATUS_REG, reg);
356 
357 	/*
358 	 * Make sure the latency timer is set to some reasonable
359 	 * value.
360 	 */
361 	reg = cardbus_conf_read(cc, cf, csc->sc_tag, CARDBUS_BHLC_REG);
362 	if (CARDBUS_LATTIMER(reg) < 0x20) {
363 		reg &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
364 		reg |= (0x20 << CARDBUS_LATTIMER_SHIFT);
365 		cardbus_conf_write(cc, cf, csc->sc_tag, CARDBUS_BHLC_REG, reg);
366 	}
367 }
368 
369 int
370 rtk_cardbus_enable(sc)
371 	struct rtk_softc *sc;
372 {
373 	struct rtk_cardbus_softc *csc = (void *) sc;
374 	cardbus_devfunc_t ct = csc->sc_ct;
375 	cardbus_chipset_tag_t cc = ct->ct_cc;
376 	cardbus_function_tag_t cf = ct->ct_cf;
377 
378 	/*
379 	 * Power on the socket.
380 	 */
381 	Cardbus_function_enable(ct);
382 
383 	/*
384 	 * Set up the PCI configuration registers.
385 	 */
386 	rtk_cardbus_setup(csc);
387 
388 	/*
389 	 * Map and establish the interrupt.
390 	 */
391 	csc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline,
392 		IPL_NET, rtk_intr, sc);
393 	if (csc->sc_ih == NULL) {
394 		printf("%s: unable to establish interrupt at %d\n",
395 			sc->sc_dev.dv_xname, csc->sc_intrline);
396 		Cardbus_function_disable(csc->sc_ct);
397 		return (1);
398 	}
399 	printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
400 		csc->sc_intrline);
401 	return (0);
402 }
403 
404 void
405 rtk_cardbus_disable(sc)
406 	struct rtk_softc *sc;
407 {
408 	struct rtk_cardbus_softc *csc = (void *) sc;
409 	cardbus_devfunc_t ct = csc->sc_ct;
410 	cardbus_chipset_tag_t cc = ct->ct_cc;
411 	cardbus_function_tag_t cf = ct->ct_cf;
412 
413 	/* Unhook the interrupt handler. */
414 	cardbus_intr_disestablish(cc, cf, csc->sc_ih);
415 
416 	/* Power down the socket. */
417 	Cardbus_function_disable(ct);
418 }
419 
420 void
421 rtk_cardbus_power(sc, why)
422 	struct rtk_softc *sc;
423 	int why;
424 {
425 	struct rtk_cardbus_softc *csc = (void *) sc;
426 
427 	if (why == PWR_RESUME) {
428 		/*
429 		 * Give the PCI configuration registers a kick
430 		 * in the head.
431 		 */
432 #ifdef DIAGNOSTIC
433 		if (RTK_IS_ENABLED(sc) == 0)
434 			panic("rtk_cardbus_power");
435 #endif
436 		rtk_cardbus_setup(csc);
437 	}
438 }
439