if_ex_cardbus.c revision 1.15 1 /* $NetBSD: if_ex_cardbus.c,v 1.15 2000/08/29 08:54:51 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 /* #define EX_DEBUG 4 */ /* define to report infomation for debugging */
40
41 #define EX_POWER_STATIC /* do not use enable/disable functions */
42 /* I'm waiting elinkxl.c uses
43 sc->enable and sc->disable
44 functions. */
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50 #include <sys/ioctl.h>
51 #include <sys/errno.h>
52 #include <sys/syslog.h>
53 #include <sys/select.h>
54 #include <sys/device.h>
55
56 #include <net/if.h>
57 #include <net/if_dl.h>
58 #include <net/if_ether.h>
59 #include <net/if_media.h>
60
61 #include <machine/cpu.h>
62 #include <machine/bus.h>
63
64 #include <dev/cardbus/cardbusvar.h>
65 #include <dev/cardbus/cardbusdevs.h>
66
67 #include <dev/mii/miivar.h>
68
69 #include <dev/ic/elink3var.h>
70 #include <dev/ic/elink3reg.h>
71 #include <dev/ic/elinkxlreg.h>
72 #include <dev/ic/elinkxlvar.h>
73
74 #if defined DEBUG && !defined EX_DEBUG
75 #define EX_DEBUG
76 #endif
77
78 #if defined EX_DEBUG
79 #define DPRINTF(a) printf a
80 #else
81 #define DPRINTF(a)
82 #endif
83
84 #define CARDBUS_3C575BTX_FUNCSTAT_PCIREG CARDBUS_BASE2_REG /* means 0x18 */
85 #define EX_CB_INTR 4 /* intr acknowledge reg. CardBus only */
86 #define EX_CB_INTR_ACK 0x8000 /* intr acknowledge bit */
87
88 int ex_cardbus_match __P((struct device *, struct cfdata *, void *));
89 void ex_cardbus_attach __P((struct device *, struct device *,void *));
90 int ex_cardbus_detach __P((struct device *, int));
91 void ex_cardbus_intr_ack __P((struct ex_softc *));
92
93 #if !defined EX_POWER_STATIC
94 int ex_cardbus_enable __P((struct ex_softc *sc));
95 void ex_cardbus_disable __P((struct ex_softc *sc));
96 #endif /* !defined EX_POWER_STATIC */
97
98 struct ex_cardbus_softc {
99 struct ex_softc sc_softc;
100
101 cardbus_devfunc_t sc_ct;
102 int sc_intrline;
103 u_int8_t sc_cardbus_flags;
104 #define EX_REATTACH 0x01
105 #define EX_ABSENT 0x02
106 u_int8_t sc_cardtype;
107 #define EX_3C575 1
108 #define EX_3C575B 2
109 #define EX_3C575C 3
110
111 /* CardBus function status space. 575B requests it. */
112 bus_space_tag_t sc_funct;
113 bus_space_handle_t sc_funch;
114 bus_size_t sc_funcsize;
115
116 bus_size_t sc_mapsize; /* the size of mapped bus space region */
117 };
118
119 struct cfattach ex_cardbus_ca = {
120 sizeof(struct ex_cardbus_softc), ex_cardbus_match,
121 ex_cardbus_attach, ex_cardbus_detach, ex_activate
122 };
123
124 const struct ex_cardbus_product {
125 u_int32_t ecp_prodid; /* CardBus product ID */
126 int ecp_flags; /* initial softc flags */
127 pcireg_t ecp_csr; /* PCI CSR flags */
128 int ecp_cardtype; /* card type */
129 const char *ecp_name; /* device name */
130 } ex_cardbus_products[] = {
131 { CARDBUS_PRODUCT_3COM_3C575TX,
132 EX_CONF_MII,
133 CARDBUS_COMMAND_IO_ENABLE | CARDBUS_COMMAND_MASTER_ENABLE,
134 EX_3C575,
135 "3c575-TX Ethernet" },
136
137 { CARDBUS_PRODUCT_3COM_3C575BTX,
138 EX_CONF_90XB|EX_CONF_MII|EX_CONF_INV_LED_POLARITY,
139 CARDBUS_COMMAND_IO_ENABLE | CARDBUS_COMMAND_MEM_ENABLE |
140 CARDBUS_COMMAND_MASTER_ENABLE,
141 EX_3C575B,
142 "3c575B-TX Ethernet" },
143
144 { CARDBUS_PRODUCT_3COM_3C575CTX,
145 EX_CONF_90XB|EX_CONF_PHY_POWER,
146 CARDBUS_COMMAND_IO_ENABLE | CARDBUS_COMMAND_MEM_ENABLE |
147 CARDBUS_COMMAND_MASTER_ENABLE,
148 EX_3C575C,
149 "3c575CT Ethernet" },
150
151 { 0,
152 0,
153 0,
154 0,
155 NULL },
156 };
157
158 const struct ex_cardbus_product *ex_cardbus_lookup
159 __P((const struct cardbus_attach_args *));
160
161 const struct ex_cardbus_product *
162 ex_cardbus_lookup(ca)
163 const struct cardbus_attach_args *ca;
164 {
165 const struct ex_cardbus_product *ecp;
166
167 if (CARDBUS_VENDOR(ca->ca_id) != CARDBUS_VENDOR_3COM)
168 return (NULL);
169
170 for (ecp = ex_cardbus_products; ecp->ecp_name != NULL; ecp++)
171 if (CARDBUS_PRODUCT(ca->ca_id) == ecp->ecp_prodid)
172 return (ecp);
173 return (NULL);
174 }
175
176 int
177 ex_cardbus_match(parent, cf, aux)
178 struct device *parent;
179 struct cfdata *cf;
180 void *aux;
181 {
182 struct cardbus_attach_args *ca = aux;
183
184 if (ex_cardbus_lookup(ca) != NULL)
185 return (1);
186
187 return (0);
188 }
189
190 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 const struct ex_cardbus_product *ecp;
204 bus_space_handle_t ioh;
205 bus_addr_t adr;
206
207 if (Cardbus_mapreg_map(ct, CARDBUS_BASE0_REG, CARDBUS_MAPREG_TYPE_IO, 0,
208 &sc->sc_iot, &ioh, &adr, &psc->sc_mapsize)) {
209 printf(": can't map i/o space\n");
210 return;
211 }
212
213 ecp = ex_cardbus_lookup(ca);
214 if (ecp == NULL) {
215 printf("\n");
216 panic("ex_cardbus_attach: impossible");
217 }
218
219 printf(": 3Com %s\n", ecp->ecp_name);
220
221 #if !defined EX_POWER_STATIC
222 sc->enable = ex_cardbus_enable;
223 sc->disable = ex_cardbus_disable;
224 #else
225 sc->enable = NULL;
226 sc->disable = NULL;
227 #endif
228 sc->enabled = 1;
229
230 sc->sc_dmat = ca->ca_dmat;
231
232 sc->ex_bustype = EX_BUS_CARDBUS;
233 sc->ex_conf = ecp->ecp_flags;
234
235 iob = adr;
236 sc->sc_ioh = ioh;
237
238 #if rbus
239 #else
240 (ct->ct_cf->cardbus_io_open)(cc, 0, iob, iob + 0x40);
241 #endif
242 (ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_IO_ENABLE);
243
244 command = cardbus_conf_read(cc, cf, ca->ca_tag,
245 CARDBUS_COMMAND_STATUS_REG);
246 command |= ecp->ecp_csr;
247 psc->sc_cardtype = ecp->ecp_cardtype;
248
249 if ((psc->sc_cardtype == EX_3C575B) ||
250 (psc->sc_cardtype == EX_3C575C)) {
251 /* Map CardBus function status window. */
252 if (Cardbus_mapreg_map(ct, CARDBUS_3C575BTX_FUNCSTAT_PCIREG,
253 CARDBUS_MAPREG_TYPE_MEM, 0, &psc->sc_funct,
254 &psc->sc_funch, 0, &psc->sc_funcsize)) {
255 printf("%s: unable to map function status window\n",
256 self->dv_xname);
257 return;
258 }
259
260 /*
261 * Make sure CardBus brigde can access memory space. Usually
262 * memory access is enabled by BIOS, but some BIOSes do not
263 * enable it.
264 */
265 (ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
266
267 /* Setup interrupt acknowledge hook */
268 sc->intr_ack = ex_cardbus_intr_ack;
269 }
270
271 (ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
272 cardbus_conf_write(cc, cf, ca->ca_tag, CARDBUS_COMMAND_STATUS_REG,
273 command);
274
275 /*
276 * set latency timmer
277 */
278 bhlc = cardbus_conf_read(cc, cf, ca->ca_tag, CARDBUS_BHLC_REG);
279 if (CARDBUS_LATTIMER(bhlc) < 0x20) {
280 /* at least the value of latency timer should 0x20. */
281 DPRINTF(("if_ex_cardbus: lattimer 0x%x -> 0x20\n",
282 CARDBUS_LATTIMER(bhlc)));
283 bhlc &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
284 bhlc |= (0x20 << CARDBUS_LATTIMER_SHIFT);
285 cardbus_conf_write(cc, cf, ca->ca_tag, CARDBUS_BHLC_REG, bhlc);
286 }
287
288 psc->sc_ct = ca->ca_ct;
289 psc->sc_intrline = ca->ca_intrline;
290
291 #if defined EX_POWER_STATIC
292 /* Map and establish the interrupt. */
293
294 sc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline, IPL_NET,
295 ex_intr, psc);
296 if (sc->sc_ih == NULL) {
297 printf("%s: couldn't establish interrupt",
298 sc->sc_dev.dv_xname);
299 printf(" at %d", ca->ca_intrline);
300 printf("\n");
301 return;
302 }
303 printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
304 ca->ca_intrline);
305 #endif
306
307 bus_space_write_2(sc->sc_iot, sc->sc_ioh, ELINK_COMMAND, GLOBAL_RESET);
308 delay(400);
309 {
310 int i = 0;
311 while (bus_space_read_2(sc->sc_iot, sc->sc_ioh, ELINK_STATUS) &
312 S_COMMAND_IN_PROGRESS) {
313 if (++i > 10000) {
314 printf("ex: timeout %x\n",
315 bus_space_read_2(sc->sc_iot, sc->sc_ioh,
316 ELINK_STATUS));
317 printf("ex: addr %x\n",
318 cardbus_conf_read(cc, cf, ca->ca_tag,
319 CARDBUS_BASE0_REG));
320 return; /* emergency exit */
321 }
322 }
323 }
324
325 ex_config(sc);
326
327 if ((psc->sc_cardtype == EX_3C575B) ||
328 (psc->sc_cardtype == EX_3C575C))
329 bus_space_write_4(psc->sc_funct, psc->sc_funch,
330 EX_CB_INTR, EX_CB_INTR_ACK);
331
332 if (psc->sc_cardtype == EX_3C575C) {
333 u_int16_t cbcard_config;
334
335 GO_WINDOW(2);
336 cbcard_config = bus_space_read_2(sc->sc_iot, sc->sc_ioh, 0x0c);
337 cbcard_config |= 0x4000; /* turn on MII power */
338 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x0c, cbcard_config);
339 }
340
341 #if !defined EX_POWER_STATIC
342 cardbus_function_disable(psc->sc_ct);
343 sc->enabled = 0;
344 #endif
345 }
346
347 void
348 ex_cardbus_intr_ack(sc)
349 struct ex_softc *sc;
350 {
351 struct ex_cardbus_softc *psc = (struct ex_cardbus_softc *)sc;
352
353 bus_space_write_4(psc->sc_funct, psc->sc_funch, EX_CB_INTR,
354 EX_CB_INTR_ACK);
355 }
356
357 int
358 ex_cardbus_detach(self, arg)
359 struct device *self;
360 int arg;
361 {
362 struct ex_cardbus_softc *psc = (void *)self;
363 struct ex_softc *sc = &psc->sc_softc;
364 struct cardbus_devfunc *ct = psc->sc_ct;
365 int rv;
366
367 #if defined(DIAGNOSTIC)
368 if (ct == NULL) {
369 panic("%s: data structure lacks\n", sc->sc_dev.dv_xname);
370 }
371 #endif
372
373 rv = ex_detach(sc);
374 if (rv == 0) {
375 /*
376 * Unhook the interrupt handler.
377 */
378 cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, sc->sc_ih);
379
380 if ((psc->sc_cardtype == EX_3C575B) ||
381 (psc->sc_cardtype == EX_3C575C)) {
382 Cardbus_mapreg_unmap(ct,
383 CARDBUS_3C575BTX_FUNCSTAT_PCIREG,
384 psc->sc_funct, psc->sc_funch, psc->sc_funcsize);
385 }
386
387 Cardbus_mapreg_unmap(ct, CARDBUS_BASE0_REG, sc->sc_iot,
388 sc->sc_ioh, psc->sc_mapsize);
389 }
390 return (rv);
391 }
392
393 #if !defined EX_POWER_STATIC
394 int
395 ex_cardbus_enable(sc)
396 struct ex_softc *sc;
397 {
398 struct ex_cardbus_softc *csc = (struct ex_cardbus_softc *)sc;
399 cardbus_function_tag_t cf = csc->sc_ct->ct_cf;
400 cardbus_chipset_tag_t cc = csc->sc_ct->ct_cc;
401
402 Cardbus_function_enable(csc->sc_ct);
403 cardbus_restore_bar(csc->sc_ct);
404
405 sc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline,
406 IPL_NET, ex_intr, sc);
407 if (NULL == sc->sc_ih) {
408 printf("%s: couldn't establish interrupt\n",
409 sc->sc_dev.dv_xname);
410 return (1);
411 }
412
413 return (0);
414 }
415
416 void
417 ex_cardbus_disable(sc)
418 struct ex_softc *sc;
419 {
420 struct ex_cardbus_softc *csc = (struct ex_cardbus_softc *)sc;
421 cardbus_function_tag_t cf = csc->sc_ct->ct_cf;
422 cardbus_chipset_tag_t cc = csc->sc_ct->ct_cc;
423
424 cardbus_save_bar(csc->sc_ct);
425
426 Cardbus_function_disable(csc->sc_ct);
427
428 cardbus_intr_disestablish(cc, cf, sc->sc_ih);
429 }
430 #endif /* EX_POWER_STATIC */
431