if_ex_cardbus.c revision 1.3.4.1 1 /* $NetBSD: if_ex_cardbus.c,v 1.3.4.1 1999/11/15 00:40:20 fvdl 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 #include <machine/cpu.h>
70 #include <machine/pio.h>
71 #include <machine/bus.h>
72 #if defined pciinc
73 #include <dev/pci/pcidevs.h>
74 #endif
75
76 #include <dev/cardbus/cardbusvar.h>
77 #include <dev/cardbus/cardbusdevs.h>
78
79 #include <dev/mii/miivar.h>
80
81 #include <dev/ic/elink3var.h>
82 #include <dev/ic/elink3reg.h>
83 #include <dev/ic/elinkxlreg.h>
84 #include <dev/ic/elinkxlvar.h>
85
86 #if defined DEBUG && !defined EX_DEBUG
87 #define EX_DEBUG
88 #endif
89
90 #if defined EX_DEBUG
91 #define STATIC
92 #define DPRINTF(a) printf a
93 #else
94 #define STATIC static
95 #define DPRINTF(a)
96 #endif
97
98 #define CARDBUS_3C575BTX_FUNCSTAT_PCIREG CARDBUS_BASE2_REG /* means 0x18 */
99 #define EX_CB_INTR 4 /* intr acknowledge reg. CardBus only */
100 #define EX_CB_INTR_ACK 0x8000 /* intr acknowledge bit */
101
102
103 /* Definitions, most of them has turned out to be unneccesary, but here they
104 * are anyway.
105 */
106
107
108 STATIC int ex_cardbus_match __P((struct device *, struct cfdata *, void *));
109 STATIC void ex_cardbus_attach __P((struct device *, struct device *,void *));
110 STATIC int ex_cardbus_detach __P((struct device *, int));
111 STATIC void ex_cardbus_intr_ack __P((struct ex_softc *));
112
113 #if 0
114 static void expoll __P((void *arg));
115 #endif
116
117 #if !defined EX_POWER_STATIC
118 STATIC int ex_cardbus_enable __P((struct ex_softc *sc));
119 STATIC void ex_cardbus_disable __P((struct ex_softc *sc));
120 #endif /* !defined EX_POWER_STATIC */
121
122
123 struct ex_cardbus_softc {
124 struct ex_softc sc_softc;
125
126 cardbus_devfunc_t sc_ct;
127 int sc_intrline;
128 u_int8_t sc_cardbus_flags;
129 #define EX_REATTACH 0x01
130 #define EX_ABSENT 0x02
131 u_int8_t sc_cardtype;
132 #define EX_3C575 1
133 #define EX_3C575B 2
134
135 /* CardBus function status space. 575B requests it. */
136 bus_space_tag_t sc_funct;
137 bus_space_handle_t sc_funch;
138 };
139
140 struct cfattach ex_cardbus_ca = {
141 sizeof(struct ex_cardbus_softc), ex_cardbus_match,
142 ex_cardbus_attach, ex_cardbus_detach
143 };
144
145
146
147 STATIC int
148 ex_cardbus_match(parent, cf, aux)
149 struct device *parent;
150 struct cfdata *cf;
151 void *aux;
152 {
153 struct cardbus_attach_args *ca = aux;
154
155 if ((CARDBUS_VENDOR(ca->ca_id) == CARDBUS_VENDOR_3COM)) {
156 if (CARDBUS_PRODUCT(ca->ca_id) == CARDBUS_PRODUCT_3COM_3C575TX
157 || CARDBUS_PRODUCT(ca->ca_id) == CARDBUS_PRODUCT_3COM_3C575BTX) {
158 return 1;
159 }
160 }
161 return 0;
162 }
163
164
165
166
167
168
169 STATIC void
170 ex_cardbus_attach(parent, self, aux)
171 struct device *parent;
172 struct device *self;
173 void *aux;
174 {
175 struct ex_cardbus_softc *psc = (void *)self;
176 struct ex_softc *sc = &psc->sc_softc;
177 struct cardbus_attach_args *ca = aux;
178 cardbus_devfunc_t ct = ca->ca_ct;
179 cardbus_chipset_tag_t cc = ct->ct_cc;
180 cardbus_function_tag_t cf = ct->ct_cf;
181 cardbusreg_t iob, command, bhlc;
182 bus_space_handle_t ioh;
183 bus_addr_t adr;
184
185
186 if (Cardbus_mapreg_map(ct, CARDBUS_BASE0_REG, CARDBUS_MAPREG_TYPE_IO, 0,
187 &(sc->sc_iot), &ioh, &adr, NULL)) {
188 panic("io alloc in ex_attach_cardbus\n");
189 }
190 iob = adr;
191 sc->sc_ioh = ioh;
192
193 #if rbus
194 #else
195 (ct->ct_cf->cardbus_io_open)(cc, 0, iob, iob + 0x40);
196 #endif
197 (ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_IO_ENABLE);
198
199 /* enable the card */
200 command = cardbus_conf_read(cc, cf, ca->ca_tag, CARDBUS_COMMAND_STATUS_REG);
201
202 /* Card specific configuration */
203 switch (CARDBUS_PRODUCT(ca->ca_id)) {
204 case CARDBUS_PRODUCT_3COM_3C575TX:
205 psc->sc_cardtype = EX_3C575;
206 command |= (CARDBUS_COMMAND_IO_ENABLE | CARDBUS_COMMAND_MASTER_ENABLE);
207 printf(": 3Com 3C575TX (boomerang)\n");
208 break;
209 case CARDBUS_PRODUCT_3COM_3C575BTX:
210 psc->sc_cardtype = EX_3C575B;
211 command |= (CARDBUS_COMMAND_IO_ENABLE |
212 CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_MASTER_ENABLE);
213
214 /* Cardbus function status window */
215 if (Cardbus_mapreg_map(ct, CARDBUS_3C575BTX_FUNCSTAT_PCIREG,
216 CARDBUS_MAPREG_TYPE_MEM, 0,
217 &psc->sc_funct, &psc->sc_funch, 0, NULL)) {
218 panic("mem alloc in ex_attach_cardbus\n");
219 }
220
221 /*
222 * Make sure CardBus brigde can access memory space. Usually
223 * memory access is enabled by BIOS, but some BIOSes do not enable
224 * it.
225 */
226 (ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
227
228 /* Setup interrupt acknowledge hook */
229 sc->intr_ack = ex_cardbus_intr_ack;
230
231 printf(": 3Com 3C575BTX (cyclone)\n");
232 break;
233 }
234
235 cardbus_conf_write(cc, cf, ca->ca_tag, CARDBUS_COMMAND_STATUS_REG, command);
236
237 /*
238 * set latency timmer
239 */
240 bhlc = cardbus_conf_read(cc, cf, ca->ca_tag, CARDBUS_BHLC_REG);
241 if (CARDBUS_LATTIMER(bhlc) < 0x20) {
242 /* at least the value of latency timer should 0x20. */
243 DPRINTF(("if_ex_cardbus: lattimer 0x%x -> 0x20\n",
244 CARDBUS_LATTIMER(bhlc)));
245 bhlc &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
246 bhlc |= (0x20 << CARDBUS_LATTIMER_SHIFT);
247 cardbus_conf_write(cc, cf, ca->ca_tag, CARDBUS_BHLC_REG, bhlc);
248 }
249
250 sc->sc_dmat = ca->ca_dmat;
251 sc->ex_bustype = EX_BUS_CARDBUS;
252 psc->sc_ct = ca->ca_ct;
253 psc->sc_intrline = ca->ca_intrline;
254
255 switch (psc->sc_cardtype) {
256 case EX_3C575:
257 sc->ex_conf = EX_CONF_MII|EX_CONF_INTPHY;
258 break;
259 case EX_3C575B:
260 sc->ex_conf = EX_CONF_90XB|EX_CONF_MII|EX_CONF_INTPHY;
261 break;
262 }
263
264 #if !defined EX_POWER_STATIC
265 sc->enable = ex_cardbus_enable;
266 sc->disable = ex_cardbus_disable;
267 #else
268 sc->enable = NULL;
269 sc->disable = NULL;
270 #endif
271 sc->enabled = 1;
272
273
274 #if defined EX_POWER_STATIC
275 /* Map and establish the interrupt. */
276
277 sc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline, IPL_NET,
278 ex_intr, psc);
279 if (sc->sc_ih == NULL) {
280 printf("%s: couldn't establish interrupt",
281 sc->sc_dev.dv_xname);
282 printf(" at %d", ca->ca_intrline);
283 printf("\n");
284 return;
285 }
286 printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname, ca->ca_intrline);
287 #endif
288
289 #if 0
290 timeout(expoll, sc, hz/20); /* XXX */
291 #endif
292
293 bus_space_write_2(sc->sc_iot, sc->sc_ioh, ELINK_COMMAND, GLOBAL_RESET);
294 delay(400);
295 {
296 int i = 0;
297 while (bus_space_read_2((sc)->sc_iot, (sc)->sc_ioh, ELINK_STATUS) \
298 & S_COMMAND_IN_PROGRESS) {
299 if (++i > 10000) {
300 printf("ex: timeout %x\n", bus_space_read_2((sc)->sc_iot, (sc)->sc_ioh, ELINK_STATUS));
301 printf("ex: addr %x\n", cardbus_conf_read(cc, cf, ca->ca_tag, CARDBUS_BASE0_REG));
302 return; /* emargency exit */
303 }
304 }
305 }
306
307 ex_config(sc); /* I'm BOOMERANG or CYCLONE */
308
309 if (psc->sc_cardtype == EX_3C575B) {
310 bus_space_write_4(psc->sc_funct, psc->sc_funch, EX_CB_INTR, EX_CB_INTR_ACK);
311 }
312
313 #if !defined EX_POWER_STATIC
314 cardbus_function_disable(psc->sc_ct);
315 sc->enabled = 0;
316 #endif
317 return;
318 }
319
320
321
322 STATIC void
323 ex_cardbus_intr_ack(sc)
324 struct ex_softc *sc;
325 {
326 struct ex_cardbus_softc *psc = (struct ex_cardbus_softc *)sc;
327 bus_space_write_4 (psc->sc_funct, psc->sc_funch, EX_CB_INTR, EX_CB_INTR_ACK);
328 }
329
330
331 STATIC int
332 ex_cardbus_detach(self, arg)
333 struct device *self;
334 int arg;
335 {
336 struct ex_cardbus_softc *psc = (void *)self;
337 struct ex_softc *sc = &psc->sc_softc;
338 cardbus_function_tag_t cf = psc->sc_ct->ct_cf;
339 cardbus_chipset_tag_t cc = psc->sc_ct->ct_cc;
340
341 /*
342 * XXX Currently, no detach.
343 */
344 printf("- ex_cardbus_detach\n");
345
346 cardbus_intr_disestablish(cc, cf, sc->sc_ih);
347
348 sc->enabled = 0;
349
350 return EBUSY;
351 }
352
353
354
355 #if !defined EX_POWER_STATIC
356 STATIC int
357 ex_cardbus_enable(sc)
358 struct ex_softc *sc;
359 {
360 struct ex_cardbus_softc *csc = (struct ex_cardbus_softc *)sc;
361 cardbus_function_tag_t cf = csc->sc_ct->ct_cf;
362 cardbus_chipset_tag_t cc = csc->sc_ct->ct_cc;
363
364 Cardbus_function_enable(csc->sc_ct);
365 cardbus_restore_bar(csc->sc_ct);
366
367 sc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline, IPL_NET, ex_intr, sc);
368 if (NULL == sc->sc_ih) {
369 printf("%s: couldn't establish interrupt\n", sc->sc_dev.dv_xname);
370 return 1;
371 }
372
373 /* printf("ex_pccard_enable: %s turned on\n", sc->sc_dev.dv_xname); */
374 return 0;
375 }
376
377
378
379
380 STATIC void
381 ex_cardbus_disable(sc)
382 struct ex_softc *sc;
383 {
384 struct ex_cardbus_softc *csc = (struct ex_cardbus_softc *)sc;
385 cardbus_function_tag_t cf = csc->sc_ct->ct_cf;
386 cardbus_chipset_tag_t cc = csc->sc_ct->ct_cc;
387
388 cardbus_save_bar(csc->sc_ct);
389
390 Cardbus_function_disable(csc->sc_ct);
391
392 cardbus_intr_disestablish(cc, cf, sc->sc_ih);
393 }
394
395 #endif /* EX_POWER_STATIC */
396