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