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