if_rtk_cardbus.c revision 1.2 1 1.2 thorpej /* $NetBSD: if_rtk_cardbus.c,v 1.2 2000/05/15 01:55:13 thorpej Exp $ */
2 1.2 thorpej
3 1.1 haya /*
4 1.1 haya * Copyright (c) 2000 Masanori Kanaoka
5 1.1 haya * All rights reserved.
6 1.1 haya *
7 1.1 haya * Redistribution and use in source and binary forms, with or without
8 1.1 haya * modification, are permitted provided that the following conditions
9 1.1 haya * are met:
10 1.1 haya * 1. Redistributions of source code must retain the above copyright
11 1.1 haya * notice, this list of conditions and the following disclaimer.
12 1.1 haya * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 haya * notice, this list of conditions and the following disclaimer in the
14 1.1 haya * documentation and/or other materials provided with the distribution.
15 1.1 haya * 3. The name of the author may not be used to endorse or promote products
16 1.1 haya * derived from this software without specific prior written permission.
17 1.1 haya *
18 1.1 haya * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 haya * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 haya * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 haya * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 haya * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1 haya * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1 haya * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1 haya * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1 haya * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 1.1 haya * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 haya */
29 1.1 haya
30 1.1 haya /*
31 1.1 haya * if_rtk_cardbus.c:
32 1.1 haya * Cardbus specific routines for RealTek 8139 ethernet adapter.
33 1.1 haya * Tested for
34 1.1 haya * - elecom-Laneed LD-10/100CBA (Accton MPX5030)
35 1.2 thorpej * - MELCO LPC3-TX-CB (RealTek 8139)
36 1.1 haya */
37 1.1 haya
38 1.1 haya #include "opt_inet.h"
39 1.1 haya #include "opt_ns.h"
40 1.1 haya #include "bpfilter.h"
41 1.1 haya #include "rnd.h"
42 1.1 haya
43 1.1 haya #include <sys/param.h>
44 1.1 haya #include <sys/systm.h>
45 1.1 haya #include <sys/callout.h>
46 1.1 haya #include <sys/device.h>
47 1.1 haya #include <sys/sockio.h>
48 1.1 haya #include <sys/mbuf.h>
49 1.1 haya #include <sys/malloc.h>
50 1.1 haya #include <sys/kernel.h>
51 1.1 haya #include <sys/socket.h>
52 1.1 haya
53 1.1 haya #include <net/if.h>
54 1.1 haya #include <net/if_arp.h>
55 1.1 haya #include <net/if_ether.h>
56 1.1 haya #include <net/if_dl.h>
57 1.1 haya #include <net/if_media.h>
58 1.1 haya #ifdef INET
59 1.1 haya #include <netinet/in.h>
60 1.1 haya #include <netinet/if_inarp.h>
61 1.1 haya #endif
62 1.1 haya #ifdef NS
63 1.1 haya #include <netns/ns.h>
64 1.1 haya #include <netns/ns_if.h>
65 1.1 haya #endif
66 1.1 haya
67 1.1 haya #if NBPFILTER > 0
68 1.1 haya #include <net/bpf.h>
69 1.1 haya #endif
70 1.1 haya #if NRND > 0
71 1.1 haya #include <sys/rnd.h>
72 1.1 haya #endif
73 1.1 haya
74 1.1 haya #include <machine/bus.h>
75 1.1 haya
76 1.1 haya #include <dev/pci/pcireg.h>
77 1.1 haya #include <dev/pci/pcivar.h>
78 1.1 haya #include <dev/pci/pcidevs.h>
79 1.1 haya
80 1.1 haya #include <dev/cardbus/cardbusvar.h>
81 1.1 haya #include <dev/cardbus/cardbusdevs.h>
82 1.1 haya
83 1.1 haya #include <dev/mii/mii.h>
84 1.1 haya #include <dev/mii/miivar.h>
85 1.1 haya
86 1.1 haya /*
87 1.1 haya * Default to using PIO access for this driver. On SMP systems,
88 1.1 haya * there appear to be problems with memory mapped mode: it looks like
89 1.1 haya * doing too many memory mapped access back to back in rapid succession
90 1.1 haya * can hang the bus. I'm inclined to blame this on crummy design/construction
91 1.1 haya * on the part of RealTek. Memory mapped mode does appear to work on
92 1.1 haya * uniprocessor systems though.
93 1.1 haya */
94 1.1 haya #define RL_USEIOSPACE
95 1.1 haya
96 1.1 haya #include <dev/ic/rtl81x9reg.h>
97 1.1 haya #include <dev/ic/rtl81x9var.h>
98 1.1 haya
99 1.1 haya /*
100 1.1 haya * Various supported device vendors/types and their names.
101 1.1 haya */
102 1.2 thorpej static const struct rtk_type rtk_cardbus_devs[] = {
103 1.1 haya { CARDBUS_VENDOR_ACCTON, CARDBUS_PRODUCT_ACCTON_MPX5030,
104 1.2 thorpej "Accton MPX 5030/5038 10/100BaseTX",
105 1.2 thorpej RL_8139 },
106 1.1 haya { CARDBUS_VENDOR_REALTEK, CARDBUS_PRODUCT_REALTEK_RT8138,
107 1.2 thorpej "RealTek 8138 10/100BaseTX", RL_8139 },
108 1.2 thorpej { 0, 0, NULL, 0 }
109 1.1 haya };
110 1.1 haya
111 1.2 thorpej const struct rtk_type *rtk_cardbus_lookup
112 1.1 haya __P((const struct cardbus_attach_args *));
113 1.2 thorpej static int rtk_cardbus_match __P((struct device *, struct cfdata *, void *));
114 1.2 thorpej static void rtk_cardbus_attach __P((struct device *, struct device *, void *));
115 1.1 haya
116 1.2 thorpej struct rtk_cardbus_softc {
117 1.2 thorpej struct rtk_softc sc_rtk; /* real rtk softc */
118 1.1 haya
119 1.1 haya /* CardBus-specific goo. */
120 1.1 haya void *sc_ih;
121 1.1 haya cardbus_devfunc_t sc_ct;
122 1.1 haya cardbustag_t sc_tag;
123 1.1 haya int sc_csr;
124 1.1 haya int sc_cben;
125 1.1 haya int sc_bar_reg;
126 1.1 haya pcireg_t sc_bar_val;
127 1.1 haya bus_size_t sc_mapsize;
128 1.1 haya int sc_intrline;
129 1.1 haya };
130 1.1 haya
131 1.1 haya struct cfattach rtk_cardbus_ca = {
132 1.2 thorpej sizeof(struct rtk_cardbus_softc), rtk_cardbus_match, rtk_cardbus_attach,
133 1.1 haya };
134 1.1 haya
135 1.2 thorpej const struct rtk_type *
136 1.2 thorpej rtk_cardbus_lookup(ca)
137 1.1 haya const struct cardbus_attach_args *ca;
138 1.1 haya {
139 1.2 thorpej const struct rtk_type *t;
140 1.1 haya
141 1.2 thorpej for (t = rtk_cardbus_devs; t->rtk_name != NULL; t++){
142 1.2 thorpej if (CARDBUS_VENDOR(ca->ca_id) == t->rtk_vid &&
143 1.2 thorpej CARDBUS_PRODUCT(ca->ca_id) == t->rtk_did) {
144 1.1 haya return (t);
145 1.1 haya }
146 1.1 haya }
147 1.1 haya return (NULL);
148 1.1 haya }
149 1.1 haya
150 1.1 haya int
151 1.2 thorpej rtk_cardbus_match(parent, match, aux)
152 1.1 haya struct device *parent;
153 1.1 haya struct cfdata *match;
154 1.1 haya void *aux;
155 1.1 haya {
156 1.1 haya struct cardbus_attach_args *ca = aux;
157 1.1 haya
158 1.2 thorpej if (rtk_cardbus_lookup(ca) != NULL)
159 1.1 haya return (1);
160 1.2 thorpej
161 1.1 haya return (0);
162 1.1 haya }
163 1.1 haya
164 1.1 haya
165 1.1 haya void
166 1.2 thorpej rtk_cardbus_attach(parent, self, aux)
167 1.1 haya struct device *parent, *self;
168 1.1 haya void *aux;
169 1.1 haya {
170 1.2 thorpej struct rtk_cardbus_softc *csc = (struct rtk_cardbus_softc *)self;
171 1.2 thorpej struct rtk_softc *sc = &csc->sc_rtk;
172 1.1 haya pcireg_t command;
173 1.1 haya struct cardbus_attach_args *ca = aux;
174 1.1 haya cardbus_devfunc_t ct = ca->ca_ct;
175 1.1 haya cardbus_chipset_tag_t cc = ct->ct_cc;
176 1.1 haya cardbus_function_tag_t cf = ct->ct_cf;
177 1.2 thorpej const struct rtk_type *t;
178 1.1 haya bus_addr_t adr;
179 1.1 haya pcireg_t reg;
180 1.2 thorpej int pmreg;
181 1.1 haya
182 1.1 haya sc->sc_dmat = ca->ca_dmat;
183 1.1 haya csc->sc_ct = ct;
184 1.1 haya csc->sc_tag = ca->ca_tag;
185 1.1 haya csc->sc_intrline = ca->ca_intrline;
186 1.1 haya
187 1.2 thorpej t = rtk_cardbus_lookup(ca);
188 1.1 haya if (t == NULL) {
189 1.1 haya printf("\n");
190 1.2 thorpej panic("rtk_cardbus_attach: impossible");
191 1.1 haya }
192 1.2 thorpej printf(": %s\n", t->rtk_name);
193 1.1 haya
194 1.1 haya /*
195 1.1 haya * Handle power management nonsense.
196 1.1 haya */
197 1.1 haya if (cardbus_get_capability(cc, cf, csc->sc_tag,
198 1.1 haya PCI_CAP_PWRMGMT, &pmreg, 0)) {
199 1.1 haya command = cardbus_conf_read(cc, cf, csc->sc_tag, pmreg + 4);
200 1.1 haya if (command & RL_PSTATE_MASK) {
201 1.1 haya pcireg_t iobase, membase, irq;
202 1.1 haya
203 1.1 haya /* Save important PCI config data. */
204 1.1 haya iobase = cardbus_conf_read(cc, cf, csc->sc_tag,
205 1.1 haya RL_PCI_LOIO);
206 1.1 haya membase = cardbus_conf_read(cc, cf,csc->sc_tag,
207 1.1 haya RL_PCI_LOMEM);
208 1.1 haya irq = cardbus_conf_read(cc, cf,csc->sc_tag,
209 1.1 haya PCI_PRODUCT_DELTA_8139);
210 1.1 haya
211 1.1 haya /* Reset the power state. */
212 1.1 haya printf("%s: chip is is in D%d power mode "
213 1.1 haya "-- setting to D0\n", sc->sc_dev.dv_xname,
214 1.1 haya command & RL_PSTATE_MASK);
215 1.1 haya command &= 0xFFFFFFFC;
216 1.1 haya cardbus_conf_write(cc, cf, csc->sc_tag,
217 1.1 haya pmreg + 4, command);
218 1.1 haya
219 1.1 haya /* Restore PCI config data. */
220 1.1 haya cardbus_conf_write(cc, cf, csc->sc_tag,
221 1.1 haya RL_PCI_LOIO, iobase);
222 1.1 haya cardbus_conf_write(cc, cf, csc->sc_tag,
223 1.1 haya RL_PCI_LOMEM, membase);
224 1.1 haya cardbus_conf_write(cc, cf, csc->sc_tag,
225 1.1 haya PCI_PRODUCT_DELTA_8139, irq);
226 1.1 haya }
227 1.1 haya }
228 1.1 haya /*
229 1.1 haya * Map control/status registers.
230 1.1 haya */
231 1.1 haya #ifdef RL_USEIOSPACE
232 1.1 haya if (Cardbus_mapreg_map(ct, RL_PCI_LOIO, CARDBUS_MAPREG_TYPE_IO, 0,
233 1.2 thorpej &sc->rtk_btag, &sc->rtk_bhandle, &adr, &csc->sc_mapsize) == 0) {
234 1.1 haya #if rbus
235 1.1 haya #else
236 1.1 haya (*ct->ct_cf->cardbus_io_open)(cc, 0, adr, adr+csc->sc_mapsize);
237 1.1 haya #endif
238 1.1 haya csc->sc_cben = CARDBUS_IO_ENABLE;
239 1.1 haya csc->sc_csr |=
240 1.1 haya (CARDBUS_COMMAND_IO_ENABLE|CARDBUS_COMMAND_MASTER_ENABLE);
241 1.1 haya csc->sc_bar_reg = RL_PCI_LOIO;
242 1.1 haya csc->sc_bar_val = adr | CARDBUS_MAPREG_TYPE_IO;
243 1.1 haya }
244 1.1 haya #else
245 1.1 haya if (Cardbus_mapreg_map(ct, RL_PCI_LOMEM, CARDBUS_MAPREG_TYPE_MEM, 0,
246 1.2 thorpej &sc->rtk_btag, &sc->rtk_bhandle, &adr, &csc->sc_mapsize) == 0) {
247 1.1 haya #if rbus
248 1.1 haya #else
249 1.1 haya (*ct->ct_cf->cardbus_mem_open)(cc, 0, adr, adr+csc->sc_mapsize);
250 1.1 haya #endif
251 1.1 haya csc->sc_cben = CARDBUS_MEM_ENABLE;
252 1.1 haya csc->sc_csr |=
253 1.1 haya (CARDBUS_COMMAND_MEM_ENABLE|CARDBUS_COMMAND_MASTER_ENABLE);
254 1.1 haya csc->sc_bar_reg = RL_PCI_LOMEM;
255 1.1 haya csc->sc_bar_val = adr | CARDBUS_MAPREG_TYPE_MEM;
256 1.1 haya }
257 1.1 haya #endif
258 1.1 haya else {
259 1.1 haya printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
260 1.2 thorpej return;
261 1.1 haya }
262 1.1 haya /* Make sure the right access type is on the CardBus bridge. */
263 1.1 haya (*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cben);
264 1.1 haya (*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
265 1.1 haya
266 1.1 haya /* Program the BAR */
267 1.1 haya cardbus_conf_write(cc, cf, csc->sc_tag,
268 1.1 haya csc->sc_bar_reg, csc->sc_bar_val);
269 1.1 haya
270 1.1 haya /* Enable the appropriate bits in the CARDBUS CSR. */
271 1.1 haya reg = cardbus_conf_read(cc, cf, csc->sc_tag,
272 1.1 haya CARDBUS_COMMAND_STATUS_REG);
273 1.1 haya reg &= ~(CARDBUS_COMMAND_IO_ENABLE|CARDBUS_COMMAND_MEM_ENABLE);
274 1.1 haya reg |= csc->sc_csr;
275 1.1 haya cardbus_conf_write(cc, cf, csc->sc_tag,
276 1.1 haya CARDBUS_COMMAND_STATUS_REG, reg);
277 1.1 haya
278 1.1 haya /*
279 1.1 haya * Make sure the latency timer is set to some reasonable
280 1.1 haya * value.
281 1.1 haya */
282 1.1 haya reg = cardbus_conf_read(cc, cf, csc->sc_tag, CARDBUS_BHLC_REG);
283 1.1 haya if (CARDBUS_LATTIMER(reg) < 0x20) {
284 1.1 haya reg &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
285 1.1 haya reg |= (0x20 << CARDBUS_LATTIMER_SHIFT);
286 1.1 haya cardbus_conf_write(cc, cf, csc->sc_tag, CARDBUS_BHLC_REG, reg);
287 1.1 haya }
288 1.1 haya
289 1.2 thorpej sc->rtk_type = t->rtk_type;
290 1.1 haya
291 1.1 haya /* Allocate interrupt */
292 1.1 haya printf("%s: interrupting at %d\n",
293 1.1 haya sc->sc_dev.dv_xname, csc->sc_intrline);
294 1.1 haya csc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline, IPL_NET,
295 1.2 thorpej rtk_intr, sc);
296 1.1 haya if (csc->sc_ih == NULL) {
297 1.1 haya printf("%s: unable to establish interrupt at %d\n",
298 1.1 haya sc->sc_dev.dv_xname, csc->sc_intrline);
299 1.1 haya printf("\n");
300 1.2 thorpej return;
301 1.1 haya }
302 1.1 haya
303 1.2 thorpej rtk_attach(sc);
304 1.1 haya }
305