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