if_ex_cardbus.c revision 1.3 1 1.3 augustss /* $NetBSD: if_ex_cardbus.c,v 1.3 1999/10/15 10:59:57 augustss Exp $ */
2 1.1 haya
3 1.1 haya /*
4 1.1 haya * CardBus specific routines for 3Com 3C575-family CardBus ethernet adapter
5 1.1 haya *
6 1.1 haya * Copyright (c) 1998 and 1999
7 1.1 haya * HAYAKAWA Koichi. All rights reserved.
8 1.1 haya *
9 1.1 haya * Redistribution and use in source and binary forms, with or without
10 1.1 haya * modification, are permitted provided that the following conditions
11 1.1 haya * are met:
12 1.1 haya * 1. Redistributions of source code must retain the above copyright
13 1.1 haya * notice, this list of conditions and the following disclaimer.
14 1.1 haya * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 haya * notice, this list of conditions and the following disclaimer in the
16 1.1 haya * documentation and/or other materials provided with the distribution.
17 1.1 haya * 3. All advertising materials mentioning features or use of this software
18 1.1 haya * must display the following acknowledgement:
19 1.1 haya * This product includes software developed by the author.
20 1.1 haya * 4. Neither the name of the author nor the names of any co-contributors
21 1.1 haya * may be used to endorse or promote products derived from this software
22 1.1 haya * without specific prior written permission.
23 1.1 haya *
24 1.1 haya * THIS SOFTWARE IS PROVIDED BY HAYAKAWA KOICHI ``AS IS'' AND
25 1.1 haya * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 haya * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 haya * ARE DISCLAIMED. IN NO EVENT SHALL TAKESHI OHASHI OR CONTRIBUTORS BE LIABLE
28 1.1 haya * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 haya * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 haya * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 haya * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 haya * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 haya * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 haya * SUCH DAMAGE.
35 1.1 haya *
36 1.1 haya *
37 1.1 haya */
38 1.1 haya
39 1.1 haya
40 1.3 augustss /* #define EX_DEBUG 4 */ /* define to report infomation for debugging */
41 1.1 haya
42 1.1 haya #define EX_POWER_STATIC /* do not use enable/disable functions */
43 1.1 haya /* I'm waiting elinkxl.c uses
44 1.1 haya sc->enable and sc->disable
45 1.1 haya functions. */
46 1.1 haya
47 1.1 haya #include <sys/param.h>
48 1.1 haya #include <sys/systm.h>
49 1.1 haya #include <sys/kernel.h>
50 1.1 haya #include <sys/conf.h>
51 1.1 haya #include <sys/ioctl.h>
52 1.1 haya #include <sys/device.h>
53 1.1 haya #include <sys/errno.h>
54 1.1 haya #include <sys/syslog.h>
55 1.1 haya /* #include <sys/signalvar.h> */
56 1.1 haya #include <sys/mbuf.h>
57 1.1 haya #include <sys/socket.h>
58 1.1 haya #include <sys/ioctl.h>
59 1.1 haya #include <sys/select.h>
60 1.1 haya #include <sys/queue.h>
61 1.1 haya
62 1.1 haya #include <net/if.h>
63 1.1 haya #include <net/if_dl.h>
64 1.1 haya #include <net/if_media.h>
65 1.1 haya #include <net/if_types.h>
66 1.1 haya #include <net/netisr.h>
67 1.1 haya #include <net/if_ether.h>
68 1.1 haya
69 1.1 haya #if defined INET
70 1.1 haya #include <netinet/in.h>
71 1.1 haya #include <netinet/in_systm.h>
72 1.1 haya #include <netinet/in_var.h>
73 1.1 haya #include <netinet/ip.h>
74 1.1 haya #endif
75 1.1 haya
76 1.1 haya #ifdef NS
77 1.1 haya #include <netns/ns.h>
78 1.1 haya #include <netns/ns_if.h>
79 1.1 haya #endif
80 1.1 haya
81 1.1 haya #if NBPFILTER > 0
82 1.1 haya #include <net/bpf.h>
83 1.1 haya #include <net/bpfdesc.h>
84 1.1 haya #endif
85 1.1 haya
86 1.1 haya #include <machine/cpu.h>
87 1.1 haya #include <machine/pio.h>
88 1.1 haya #include <machine/bus.h>
89 1.1 haya #if defined pciinc
90 1.1 haya #include <dev/pci/pcidevs.h>
91 1.1 haya #endif
92 1.1 haya #if pccard
93 1.1 haya #include <dev/pccard/cardbusvar.h>
94 1.1 haya #include <dev/pccard/pccardcis.h>
95 1.1 haya #else
96 1.1 haya #include <dev/cardbus/cardbusvar.h>
97 1.1 haya #include <dev/cardbus/cardbusdevs.h>
98 1.1 haya #endif
99 1.1 haya
100 1.1 haya #include <dev/mii/miivar.h>
101 1.1 haya
102 1.1 haya #include <dev/ic/elink3var.h>
103 1.1 haya #include <dev/ic/elink3reg.h>
104 1.1 haya #include <dev/ic/elinkxlreg.h>
105 1.1 haya #include <dev/ic/elinkxlvar.h>
106 1.1 haya
107 1.1 haya #if defined DEBUG && !defined EX_DEBUG
108 1.1 haya #define EX_DEBUG
109 1.1 haya #endif
110 1.1 haya
111 1.1 haya #if defined EX_DEBUG
112 1.1 haya #define STATIC
113 1.1 haya #define DPRINTF(a) printf a
114 1.1 haya #else
115 1.1 haya #define STATIC static
116 1.1 haya #define DPRINTF(a)
117 1.1 haya #endif
118 1.1 haya
119 1.1 haya #define CARDBUS_3C575BTX_FUNCSTAT_PCIREG CARDBUS_BASE2_REG /* means 0x18 */
120 1.1 haya #define EX_CB_INTR 4 /* intr acknowledge reg. CardBus only */
121 1.1 haya #define EX_CB_INTR_ACK 0x8000 /* intr acknowledge bit */
122 1.1 haya
123 1.1 haya
124 1.1 haya /* Definitions, most of them has turned out to be unneccesary, but here they
125 1.1 haya * are anyway.
126 1.1 haya */
127 1.1 haya
128 1.1 haya
129 1.1 haya STATIC int ex_cardbus_match __P((struct device *, struct cfdata *, void *));
130 1.1 haya STATIC void ex_cardbus_attach __P((struct device *, struct device *,void *));
131 1.1 haya STATIC int ex_cardbus_detach __P((struct device *, int));
132 1.1 haya STATIC void ex_cardbus_intr_ack __P((struct ex_softc *));
133 1.1 haya
134 1.1 haya #if 0
135 1.1 haya static void expoll __P((void *arg));
136 1.1 haya #endif
137 1.1 haya
138 1.1 haya #if !defined EX_POWER_STATIC
139 1.1 haya STATIC int ex_cardbus_enable __P((struct ex_softc *sc));
140 1.1 haya STATIC void ex_cardbus_disable __P((struct ex_softc *sc));
141 1.1 haya #endif /* !defined EX_POWER_STATIC */
142 1.1 haya
143 1.1 haya
144 1.1 haya struct ex_cardbus_softc {
145 1.1 haya struct ex_softc sc_softc;
146 1.1 haya
147 1.1 haya cardbus_devfunc_t sc_ct;
148 1.1 haya int sc_intrline;
149 1.1 haya u_int8_t sc_cardbus_flags;
150 1.1 haya #define EX_REATTACH 0x01
151 1.1 haya #define EX_ABSENT 0x02
152 1.1 haya u_int8_t sc_cardtype;
153 1.1 haya #define EX_3C575 1
154 1.1 haya #define EX_3C575B 2
155 1.1 haya
156 1.1 haya /* CardBus function status space. 575B requests it. */
157 1.1 haya bus_space_tag_t sc_funct;
158 1.1 haya bus_space_handle_t sc_funch;
159 1.1 haya };
160 1.1 haya
161 1.1 haya struct cfattach ex_cardbus_ca = {
162 1.1 haya sizeof(struct ex_cardbus_softc), ex_cardbus_match,
163 1.1 haya ex_cardbus_attach, ex_cardbus_detach
164 1.1 haya };
165 1.1 haya
166 1.1 haya
167 1.1 haya
168 1.1 haya STATIC int
169 1.1 haya ex_cardbus_match(parent, cf, aux)
170 1.1 haya struct device *parent;
171 1.1 haya struct cfdata *cf;
172 1.1 haya void *aux;
173 1.1 haya {
174 1.1 haya struct cardbus_attach_args *ca = aux;
175 1.1 haya
176 1.1 haya if ((CARDBUS_VENDOR(ca->ca_id) == CARDBUS_VENDOR_3COM)) {
177 1.1 haya if (CARDBUS_PRODUCT(ca->ca_id) == CARDBUS_PRODUCT_3COM_3C575TX
178 1.1 haya || CARDBUS_PRODUCT(ca->ca_id) == CARDBUS_PRODUCT_3COM_3C575BTX) {
179 1.1 haya return 1;
180 1.1 haya }
181 1.1 haya }
182 1.1 haya return 0;
183 1.1 haya }
184 1.1 haya
185 1.1 haya
186 1.1 haya
187 1.1 haya
188 1.1 haya
189 1.1 haya
190 1.1 haya STATIC void
191 1.1 haya ex_cardbus_attach(parent, self, aux)
192 1.1 haya struct device *parent;
193 1.1 haya struct device *self;
194 1.1 haya void *aux;
195 1.1 haya {
196 1.1 haya struct ex_cardbus_softc *psc = (void *)self;
197 1.1 haya struct ex_softc *sc = &psc->sc_softc;
198 1.1 haya struct cardbus_attach_args *ca = aux;
199 1.1 haya cardbus_devfunc_t ct = ca->ca_ct;
200 1.1 haya cardbus_chipset_tag_t cc = ct->ct_cc;
201 1.1 haya cardbus_function_tag_t cf = ct->ct_cf;
202 1.1 haya cardbusreg_t iob, command, bhlc;
203 1.1 haya bus_space_handle_t ioh;
204 1.1 haya bus_addr_t adr;
205 1.1 haya
206 1.1 haya
207 1.1 haya if (cardbus_mapreg_map(ct, CARDBUS_BASE0_REG, CARDBUS_MAPREG_TYPE_IO, 0,
208 1.1 haya &(sc->sc_iot), &ioh, &adr, NULL)) {
209 1.1 haya panic("io alloc in ex_attach_cardbus\n");
210 1.1 haya }
211 1.1 haya iob = adr;
212 1.1 haya sc->sc_ioh = ioh;
213 1.1 haya
214 1.1 haya #if rbus
215 1.1 haya #else
216 1.1 haya (ct->ct_cf->cardbus_io_open)(cc, 0, iob, iob + 0x40);
217 1.1 haya #endif
218 1.1 haya (ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_IO_ENABLE);
219 1.1 haya
220 1.1 haya /* enable the card */
221 1.1 haya command = cardbus_conf_read(cc, cf, ca->ca_tag, CARDBUS_COMMAND_STATUS_REG);
222 1.1 haya
223 1.1 haya /* Card specific configuration */
224 1.1 haya switch (CARDBUS_PRODUCT(ca->ca_id)) {
225 1.1 haya case CARDBUS_PRODUCT_3COM_3C575TX:
226 1.1 haya psc->sc_cardtype = EX_3C575;
227 1.1 haya command |= (CARDBUS_COMMAND_IO_ENABLE | CARDBUS_COMMAND_MASTER_ENABLE);
228 1.3 augustss printf(": 3Com 3C575TX (boomerang)\n");
229 1.1 haya break;
230 1.1 haya case CARDBUS_PRODUCT_3COM_3C575BTX:
231 1.1 haya psc->sc_cardtype = EX_3C575B;
232 1.1 haya command |= (CARDBUS_COMMAND_IO_ENABLE |
233 1.1 haya CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_MASTER_ENABLE);
234 1.1 haya
235 1.1 haya /* Cardbus function status window */
236 1.1 haya if (cardbus_mapreg_map(ct, CARDBUS_3C575BTX_FUNCSTAT_PCIREG,
237 1.1 haya CARDBUS_MAPREG_TYPE_MEM, 0,
238 1.1 haya &psc->sc_funct, &psc->sc_funch, 0, NULL)) {
239 1.1 haya panic("mem alloc in ex_attach_cardbus\n");
240 1.1 haya }
241 1.1 haya
242 1.1 haya /*
243 1.1 haya * Make sure CardBus brigde can access memory space. Usually
244 1.1 haya * memory access is enabled by BIOS, but some BIOSes do not enable
245 1.1 haya * it.
246 1.1 haya */
247 1.1 haya (ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
248 1.1 haya
249 1.1 haya /* Setup interrupt acknowledge hook */
250 1.1 haya sc->intr_ack = ex_cardbus_intr_ack;
251 1.1 haya
252 1.3 augustss printf(": 3Com 3C575BTX (cyclone)\n");
253 1.1 haya break;
254 1.1 haya }
255 1.1 haya
256 1.1 haya cardbus_conf_write(cc, cf, ca->ca_tag, CARDBUS_COMMAND_STATUS_REG, command);
257 1.1 haya
258 1.1 haya /*
259 1.1 haya * set latency timmer
260 1.1 haya */
261 1.1 haya bhlc = cardbus_conf_read(cc, cf, ca->ca_tag, CARDBUS_BHLC_REG);
262 1.1 haya if (CARDBUS_LATTIMER(bhlc) < 0x20) {
263 1.1 haya /* at least the value of latency timer should 0x20. */
264 1.3 augustss DPRINTF(("if_ex_cardbus: lattimer 0x%x -> 0x20\n",
265 1.3 augustss CARDBUS_LATTIMER(bhlc)));
266 1.1 haya bhlc &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
267 1.1 haya bhlc |= (0x20 << CARDBUS_LATTIMER_SHIFT);
268 1.1 haya cardbus_conf_write(cc, cf, ca->ca_tag, CARDBUS_BHLC_REG, bhlc);
269 1.1 haya }
270 1.1 haya
271 1.1 haya sc->sc_dmat = ca->ca_dmat;
272 1.1 haya sc->ex_bustype = EX_BUS_CARDBUS;
273 1.1 haya psc->sc_ct = ca->ca_ct;
274 1.1 haya psc->sc_intrline = ca->ca_intrline;
275 1.1 haya
276 1.1 haya switch (psc->sc_cardtype) {
277 1.1 haya case EX_3C575:
278 1.1 haya sc->ex_conf = EX_CONF_MII|EX_CONF_INTPHY;
279 1.1 haya break;
280 1.1 haya case EX_3C575B:
281 1.1 haya sc->ex_conf = EX_CONF_90XB|EX_CONF_MII|EX_CONF_INTPHY;
282 1.1 haya break;
283 1.1 haya }
284 1.1 haya
285 1.1 haya #if !defined EX_POWER_STATIC
286 1.1 haya sc->enable = ex_cardbus_enable;
287 1.1 haya sc->disable = ex_cardbus_disable;
288 1.1 haya #else
289 1.1 haya sc->enable = NULL;
290 1.1 haya sc->disable = NULL;
291 1.1 haya #endif
292 1.1 haya sc->enabled = 1;
293 1.1 haya
294 1.1 haya
295 1.1 haya #if defined EX_POWER_STATIC
296 1.1 haya /* Map and establish the interrupt. */
297 1.1 haya
298 1.1 haya sc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline, IPL_NET,
299 1.1 haya ex_intr, psc);
300 1.1 haya if (sc->sc_ih == NULL) {
301 1.1 haya printf("%s: couldn't establish interrupt",
302 1.1 haya sc->sc_dev.dv_xname);
303 1.1 haya printf(" at %d", ca->ca_intrline);
304 1.1 haya printf("\n");
305 1.1 haya return;
306 1.1 haya }
307 1.1 haya printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname, ca->ca_intrline);
308 1.1 haya #endif
309 1.1 haya
310 1.1 haya #if 0
311 1.1 haya timeout(expoll, sc, hz/20); /* XXX */
312 1.1 haya #endif
313 1.1 haya
314 1.1 haya bus_space_write_2(sc->sc_iot, sc->sc_ioh, ELINK_COMMAND, GLOBAL_RESET);
315 1.1 haya delay(400);
316 1.1 haya {
317 1.1 haya int i = 0;
318 1.1 haya while (bus_space_read_2((sc)->sc_iot, (sc)->sc_ioh, ELINK_STATUS) \
319 1.1 haya & S_COMMAND_IN_PROGRESS) {
320 1.1 haya if (++i > 10000) {
321 1.1 haya printf("ex: timeout %x\n", bus_space_read_2((sc)->sc_iot, (sc)->sc_ioh, ELINK_STATUS));
322 1.1 haya printf("ex: addr %x\n", cardbus_conf_read(cc, cf, ca->ca_tag, CARDBUS_BASE0_REG));
323 1.1 haya return; /* emargency exit */
324 1.1 haya }
325 1.1 haya }
326 1.1 haya }
327 1.1 haya
328 1.1 haya ex_config(sc); /* I'm BOOMERANG or CYCLONE */
329 1.1 haya
330 1.1 haya if (psc->sc_cardtype == EX_3C575B) {
331 1.1 haya bus_space_write_4(psc->sc_funct, psc->sc_funch, EX_CB_INTR, EX_CB_INTR_ACK);
332 1.1 haya }
333 1.1 haya
334 1.1 haya #if !defined EX_POWER_STATIC
335 1.1 haya cardbus_function_disable(psc->sc_ct);
336 1.1 haya sc->enabled = 0;
337 1.1 haya #endif
338 1.1 haya return;
339 1.1 haya }
340 1.1 haya
341 1.1 haya
342 1.1 haya
343 1.1 haya STATIC void
344 1.1 haya ex_cardbus_intr_ack(sc)
345 1.1 haya struct ex_softc *sc;
346 1.1 haya {
347 1.1 haya struct ex_cardbus_softc *psc = (struct ex_cardbus_softc *)sc;
348 1.1 haya bus_space_write_4 (psc->sc_funct, psc->sc_funch, EX_CB_INTR, EX_CB_INTR_ACK);
349 1.1 haya }
350 1.1 haya
351 1.1 haya
352 1.1 haya STATIC int
353 1.1 haya ex_cardbus_detach(self, arg)
354 1.1 haya struct device *self;
355 1.1 haya int arg;
356 1.1 haya {
357 1.1 haya struct ex_cardbus_softc *psc = (void *)self;
358 1.1 haya struct ex_softc *sc = &psc->sc_softc;
359 1.1 haya cardbus_function_tag_t cf = psc->sc_ct->ct_cf;
360 1.1 haya cardbus_chipset_tag_t cc = psc->sc_ct->ct_cc;
361 1.1 haya
362 1.1 haya /*
363 1.1 haya * XXX Currently, no detach.
364 1.1 haya */
365 1.1 haya printf("- ex_cardbus_detach\n");
366 1.1 haya
367 1.1 haya cardbus_intr_disestablish(cc, cf, sc->sc_ih);
368 1.1 haya
369 1.1 haya sc->enabled = 0;
370 1.1 haya
371 1.1 haya return EBUSY;
372 1.1 haya }
373 1.1 haya
374 1.1 haya
375 1.1 haya
376 1.1 haya #if !defined EX_POWER_STATIC
377 1.1 haya STATIC int
378 1.1 haya ex_cardbus_enable(sc)
379 1.1 haya struct ex_softc *sc;
380 1.1 haya {
381 1.1 haya struct ex_cardbus_softc *csc = (struct ex_cardbus_softc *)sc;
382 1.1 haya cardbus_function_tag_t cf = csc->sc_ct->ct_cf;
383 1.1 haya cardbus_chipset_tag_t cc = csc->sc_ct->ct_cc;
384 1.1 haya
385 1.1 haya cardbus_function_enable(csc->sc_ct);
386 1.1 haya cardbus_restore_bar(csc->sc_ct);
387 1.1 haya
388 1.1 haya sc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline, IPL_NET, ex_intr, sc);
389 1.1 haya if (NULL == sc->sc_ih) {
390 1.1 haya printf("%s: couldn't establish interrupt\n", sc->sc_dev.dv_xname);
391 1.1 haya return 1;
392 1.1 haya }
393 1.1 haya
394 1.1 haya /* printf("ex_pccard_enable: %s turned on\n", sc->sc_dev.dv_xname); */
395 1.1 haya return 0;
396 1.1 haya }
397 1.1 haya
398 1.1 haya
399 1.1 haya
400 1.1 haya
401 1.1 haya STATIC void
402 1.1 haya ex_cardbus_disable(sc)
403 1.1 haya struct ex_softc *sc;
404 1.1 haya {
405 1.1 haya struct ex_cardbus_softc *csc = (struct ex_cardbus_softc *)sc;
406 1.1 haya cardbus_function_tag_t cf = csc->sc_ct->ct_cf;
407 1.1 haya cardbus_chipset_tag_t cc = csc->sc_ct->ct_cc;
408 1.1 haya
409 1.1 haya cardbus_save_bar(csc->sc_ct);
410 1.1 haya
411 1.1 haya cardbus_function_disable(csc->sc_ct);
412 1.1 haya
413 1.1 haya cardbus_intr_disestablish(cc, cf, sc->sc_ih);
414 1.1 haya }
415 1.1 haya
416 1.1 haya #endif /* EX_POWER_STATIC */
417