if_ex_cardbus.c revision 1.13 1 /* $NetBSD: if_ex_cardbus.c,v 1.13 2000/03/07 00:32:52 mycroft 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
110 /* CardBus function status space. 575B requests it. */
111 bus_space_tag_t sc_funct;
112 bus_space_handle_t sc_funch;
113 bus_size_t sc_funcsize;
114
115 bus_size_t sc_mapsize; /* the size of mapped bus space region */
116 };
117
118 struct cfattach ex_cardbus_ca = {
119 sizeof(struct ex_cardbus_softc), ex_cardbus_match,
120 ex_cardbus_attach, ex_cardbus_detach, ex_activate
121 };
122
123 const struct ex_cardbus_product {
124 u_int32_t ecp_prodid; /* CardBus product ID */
125 int ecp_flags; /* initial softc flags */
126 pcireg_t ecp_csr; /* PCI CSR flags */
127 int ecp_cardtype; /* card type */
128 const char *ecp_name; /* device name */
129 } ex_cardbus_products[] = {
130 { CARDBUS_PRODUCT_3COM_3C575TX,
131 EX_CONF_MII,
132 CARDBUS_COMMAND_IO_ENABLE | CARDBUS_COMMAND_MASTER_ENABLE,
133 EX_3C575,
134 "3c575-TX Ethernet" },
135
136 { CARDBUS_PRODUCT_3COM_3C575BTX,
137 EX_CONF_90XB|EX_CONF_MII,
138 CARDBUS_COMMAND_IO_ENABLE | CARDBUS_COMMAND_MEM_ENABLE |
139 CARDBUS_COMMAND_MASTER_ENABLE,
140 EX_3C575B,
141 "3c575B-TX Ethernet" },
142
143 { 0,
144 0,
145 0,
146 NULL },
147 };
148
149 const struct ex_cardbus_product *ex_cardbus_lookup
150 __P((const struct cardbus_attach_args *));
151
152 const struct ex_cardbus_product *
153 ex_cardbus_lookup(ca)
154 const struct cardbus_attach_args *ca;
155 {
156 const struct ex_cardbus_product *ecp;
157
158 if (CARDBUS_VENDOR(ca->ca_id) != CARDBUS_VENDOR_3COM)
159 return (NULL);
160
161 for (ecp = ex_cardbus_products; ecp->ecp_name != NULL; ecp++)
162 if (CARDBUS_PRODUCT(ca->ca_id) == ecp->ecp_prodid)
163 return (ecp);
164 return (NULL);
165 }
166
167 int
168 ex_cardbus_match(parent, cf, aux)
169 struct device *parent;
170 struct cfdata *cf;
171 void *aux;
172 {
173 struct cardbus_attach_args *ca = aux;
174
175 if (ex_cardbus_lookup(ca) != NULL)
176 return (1);
177
178 return (0);
179 }
180
181 void
182 ex_cardbus_attach(parent, self, aux)
183 struct device *parent;
184 struct device *self;
185 void *aux;
186 {
187 struct ex_cardbus_softc *psc = (void *)self;
188 struct ex_softc *sc = &psc->sc_softc;
189 struct cardbus_attach_args *ca = aux;
190 cardbus_devfunc_t ct = ca->ca_ct;
191 cardbus_chipset_tag_t cc = ct->ct_cc;
192 cardbus_function_tag_t cf = ct->ct_cf;
193 cardbusreg_t iob, command, bhlc;
194 const struct ex_cardbus_product *ecp;
195 bus_space_handle_t ioh;
196 bus_addr_t adr;
197
198 if (Cardbus_mapreg_map(ct, CARDBUS_BASE0_REG, CARDBUS_MAPREG_TYPE_IO, 0,
199 &sc->sc_iot, &ioh, &adr, &psc->sc_mapsize)) {
200 printf(": can't map i/o space\n");
201 return;
202 }
203
204 ecp = ex_cardbus_lookup(ca);
205 if (ecp == NULL) {
206 printf("\n");
207 panic("ex_cardbus_attach: impossible");
208 }
209
210 printf(": 3Com %s\n", ecp->ecp_name);
211
212 #if !defined EX_POWER_STATIC
213 sc->enable = ex_cardbus_enable;
214 sc->disable = ex_cardbus_disable;
215 #else
216 sc->enable = NULL;
217 sc->disable = NULL;
218 #endif
219 sc->enabled = 1;
220
221 sc->sc_dmat = ca->ca_dmat;
222
223 sc->ex_bustype = EX_BUS_CARDBUS;
224 sc->ex_conf = ecp->ecp_flags;
225
226 iob = adr;
227 sc->sc_ioh = ioh;
228
229 #if rbus
230 #else
231 (ct->ct_cf->cardbus_io_open)(cc, 0, iob, iob + 0x40);
232 #endif
233 (ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_IO_ENABLE);
234
235 command = cardbus_conf_read(cc, cf, ca->ca_tag,
236 CARDBUS_COMMAND_STATUS_REG);
237 command |= ecp->ecp_csr;
238 psc->sc_cardtype = ecp->ecp_cardtype;
239
240 if (psc->sc_cardtype == EX_3C575B) {
241 /* Map CardBus function status window. */
242 if (Cardbus_mapreg_map(ct, CARDBUS_3C575BTX_FUNCSTAT_PCIREG,
243 CARDBUS_MAPREG_TYPE_MEM, 0, &psc->sc_funct,
244 &psc->sc_funch, 0, &psc->sc_funcsize)) {
245 printf("%s: unable to map function status window\n",
246 self->dv_xname);
247 return;
248 }
249
250 /*
251 * Make sure CardBus brigde can access memory space. Usually
252 * memory access is enabled by BIOS, but some BIOSes do not
253 * enable it.
254 */
255 (ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
256
257 /* Setup interrupt acknowledge hook */
258 sc->intr_ack = ex_cardbus_intr_ack;
259 }
260
261 (ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
262 cardbus_conf_write(cc, cf, ca->ca_tag, CARDBUS_COMMAND_STATUS_REG,
263 command);
264
265 /*
266 * set latency timmer
267 */
268 bhlc = cardbus_conf_read(cc, cf, ca->ca_tag, CARDBUS_BHLC_REG);
269 if (CARDBUS_LATTIMER(bhlc) < 0x20) {
270 /* at least the value of latency timer should 0x20. */
271 DPRINTF(("if_ex_cardbus: lattimer 0x%x -> 0x20\n",
272 CARDBUS_LATTIMER(bhlc)));
273 bhlc &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
274 bhlc |= (0x20 << CARDBUS_LATTIMER_SHIFT);
275 cardbus_conf_write(cc, cf, ca->ca_tag, CARDBUS_BHLC_REG, bhlc);
276 }
277
278 psc->sc_ct = ca->ca_ct;
279 psc->sc_intrline = ca->ca_intrline;
280
281 #if defined EX_POWER_STATIC
282 /* Map and establish the interrupt. */
283
284 sc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline, IPL_NET,
285 ex_intr, psc);
286 if (sc->sc_ih == NULL) {
287 printf("%s: couldn't establish interrupt",
288 sc->sc_dev.dv_xname);
289 printf(" at %d", ca->ca_intrline);
290 printf("\n");
291 return;
292 }
293 printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
294 ca->ca_intrline);
295 #endif
296
297 bus_space_write_2(sc->sc_iot, sc->sc_ioh, ELINK_COMMAND, GLOBAL_RESET);
298 delay(400);
299 {
300 int i = 0;
301 while (bus_space_read_2(sc->sc_iot, sc->sc_ioh, ELINK_STATUS) &
302 S_COMMAND_IN_PROGRESS) {
303 if (++i > 10000) {
304 printf("ex: timeout %x\n",
305 bus_space_read_2(sc->sc_iot, sc->sc_ioh,
306 ELINK_STATUS));
307 printf("ex: addr %x\n",
308 cardbus_conf_read(cc, cf, ca->ca_tag,
309 CARDBUS_BASE0_REG));
310 return; /* emergency exit */
311 }
312 }
313 }
314
315 ex_config(sc);
316
317 if (psc->sc_cardtype == EX_3C575B)
318 bus_space_write_4(psc->sc_funct, psc->sc_funch,
319 EX_CB_INTR, EX_CB_INTR_ACK);
320
321 #if !defined EX_POWER_STATIC
322 cardbus_function_disable(psc->sc_ct);
323 sc->enabled = 0;
324 #endif
325 }
326
327 void
328 ex_cardbus_intr_ack(sc)
329 struct ex_softc *sc;
330 {
331 struct ex_cardbus_softc *psc = (struct ex_cardbus_softc *)sc;
332
333 bus_space_write_4(psc->sc_funct, psc->sc_funch, EX_CB_INTR,
334 EX_CB_INTR_ACK);
335 }
336
337 int
338 ex_cardbus_detach(self, arg)
339 struct device *self;
340 int arg;
341 {
342 struct ex_cardbus_softc *psc = (void *)self;
343 struct ex_softc *sc = &psc->sc_softc;
344 struct cardbus_devfunc *ct = psc->sc_ct;
345 int rv;
346
347 #if defined(DIAGNOSTIC)
348 if (ct == NULL) {
349 panic("%s: data structure lacks\n", sc->sc_dev.dv_xname);
350 }
351 #endif
352
353 rv = ex_detach(sc);
354 if (rv == 0) {
355 /*
356 * Unhook the interrupt handler.
357 */
358 cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, sc->sc_ih);
359
360 if (psc->sc_cardtype == EX_3C575B) {
361 Cardbus_mapreg_unmap(ct,
362 CARDBUS_3C575BTX_FUNCSTAT_PCIREG,
363 psc->sc_funct, psc->sc_funch, psc->sc_funcsize);
364 }
365
366 Cardbus_mapreg_unmap(ct, CARDBUS_BASE0_REG, sc->sc_iot,
367 sc->sc_ioh, psc->sc_mapsize);
368 }
369 return (rv);
370 }
371
372 #if !defined EX_POWER_STATIC
373 int
374 ex_cardbus_enable(sc)
375 struct ex_softc *sc;
376 {
377 struct ex_cardbus_softc *csc = (struct ex_cardbus_softc *)sc;
378 cardbus_function_tag_t cf = csc->sc_ct->ct_cf;
379 cardbus_chipset_tag_t cc = csc->sc_ct->ct_cc;
380
381 Cardbus_function_enable(csc->sc_ct);
382 cardbus_restore_bar(csc->sc_ct);
383
384 sc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline,
385 IPL_NET, ex_intr, sc);
386 if (NULL == sc->sc_ih) {
387 printf("%s: couldn't establish interrupt\n",
388 sc->sc_dev.dv_xname);
389 return (1);
390 }
391
392 return (0);
393 }
394
395 void
396 ex_cardbus_disable(sc)
397 struct ex_softc *sc;
398 {
399 struct ex_cardbus_softc *csc = (struct ex_cardbus_softc *)sc;
400 cardbus_function_tag_t cf = csc->sc_ct->ct_cf;
401 cardbus_chipset_tag_t cc = csc->sc_ct->ct_cc;
402
403 cardbus_save_bar(csc->sc_ct);
404
405 Cardbus_function_disable(csc->sc_ct);
406
407 cardbus_intr_disestablish(cc, cf, sc->sc_ih);
408 }
409 #endif /* EX_POWER_STATIC */
410