if_ep_pcmcia.c revision 1.56 1 /* $NetBSD: if_ep_pcmcia.c,v 1.56 2006/09/24 03:54:00 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2000, 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, and by Charles M. Hannum.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1997 Marc Horowitz. All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by Marc Horowitz.
54 * 4. The name of the author may not be used to endorse or promote products
55 * derived from this software without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
58 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
61 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
62 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
63 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
64 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
66 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 */
68
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: if_ep_pcmcia.c,v 1.56 2006/09/24 03:54:00 jmcneill Exp $");
71
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/mbuf.h>
75 #include <sys/socket.h>
76 #include <sys/ioctl.h>
77 #include <sys/errno.h>
78 #include <sys/syslog.h>
79 #include <sys/select.h>
80 #include <sys/device.h>
81
82 #include <net/if.h>
83 #include <net/if_dl.h>
84 #include <net/if_ether.h>
85 #include <net/if_media.h>
86
87 #include <machine/cpu.h>
88 #include <machine/bus.h>
89 #include <machine/intr.h>
90
91 #include <dev/mii/miivar.h>
92
93 #include <dev/ic/elink3var.h>
94 #include <dev/ic/elink3reg.h>
95
96 #include <dev/pcmcia/pcmciareg.h>
97 #include <dev/pcmcia/pcmciavar.h>
98 #include <dev/pcmcia/pcmciadevs.h>
99
100 int ep_pcmcia_match(struct device *, struct cfdata *, void *);
101 void ep_pcmcia_attach(struct device *, struct device *, void *);
102 int ep_pcmcia_detach(struct device *, int);
103
104 int ep_pcmcia_get_enaddr(struct pcmcia_tuple *, void *);
105 int ep_pcmcia_enable(struct ep_softc *);
106 void ep_pcmcia_disable(struct ep_softc *);
107
108 void ep_pcmcia_disable1(struct ep_softc *);
109
110 struct ep_pcmcia_softc {
111 struct ep_softc sc_ep; /* real "ep" softc */
112
113 /* PCMCIA-specific goo */
114 struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
115 int sc_io_window; /* our i/o window */
116 struct pcmcia_function *sc_pf; /* our PCMCIA function */
117
118 void *sc_powerhook; /* power management hook */
119 };
120
121 CFATTACH_DECL(ep_pcmcia, sizeof(struct ep_pcmcia_softc),
122 ep_pcmcia_match, ep_pcmcia_attach, ep_pcmcia_detach, ep_activate);
123
124 const struct ep_pcmcia_product {
125 struct pcmcia_product epp_product;
126 u_short epp_chipset; /* 3Com chipset used */
127 int epp_flags; /* initial softc flags */
128 } ep_pcmcia_products[] = {
129 { { PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3C562,
130 PCMCIA_CIS_INVALID },
131 ELINK_CHIPSET_3C509, 0 },
132
133 { { PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3C589,
134 PCMCIA_CIS_INVALID },
135 ELINK_CHIPSET_3C509, 0 },
136
137 { { PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3CXEM556,
138 PCMCIA_CIS_INVALID },
139 ELINK_CHIPSET_3C509, 0 },
140
141 { { PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3CXEM556INT,
142 PCMCIA_CIS_INVALID },
143 ELINK_CHIPSET_3C509, 0 },
144
145 { { PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3C574,
146 PCMCIA_CIS_INVALID },
147 ELINK_CHIPSET_ROADRUNNER, ELINK_FLAGS_MII },
148
149 { { PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3CCFEM556BI,
150 PCMCIA_CIS_INVALID },
151 ELINK_CHIPSET_ROADRUNNER, ELINK_FLAGS_MII },
152
153 { { PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3C1,
154 PCMCIA_CIS_INVALID },
155 ELINK_CHIPSET_3C509, 0 },
156 };
157 const size_t ep_pcmcia_nproducts =
158 sizeof(ep_pcmcia_products) / sizeof(ep_pcmcia_products[0]);
159
160 int
161 ep_pcmcia_match(parent, match, aux)
162 struct device *parent;
163 struct cfdata *match;
164 void *aux;
165 {
166 struct pcmcia_attach_args *pa = aux;
167
168 /* This is to differentiate the serial function of some cards. */
169 if (pa->pf->function != PCMCIA_FUNCTION_NETWORK)
170 return (0);
171
172 if (pcmcia_product_lookup(pa, ep_pcmcia_products, ep_pcmcia_nproducts,
173 sizeof(ep_pcmcia_products[0]), NULL))
174 return (1);
175 return (0);
176 }
177
178 int
179 ep_pcmcia_enable(sc)
180 struct ep_softc *sc;
181 {
182 struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
183 struct pcmcia_function *pf = psc->sc_pf;
184 int error;
185
186 /* establish the interrupt. */
187 sc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, epintr, sc);
188 if (!sc->sc_ih)
189 return (EIO);
190
191 error = pcmcia_function_enable(pf);
192 if (error) {
193 pcmcia_intr_disestablish(pf, sc->sc_ih);
194 sc->sc_ih = 0;
195 return (error);
196 }
197
198 if ((psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3C562) ||
199 (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556) ||
200 (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556INT)) {
201 int reg;
202
203 /* turn off the serial-disable bit */
204
205 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
206 if (reg & 0x08) {
207 reg &= ~0x08;
208 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
209 }
210
211 }
212
213 return (0);
214 }
215
216 void
217 ep_pcmcia_disable(sc)
218 struct ep_softc *sc;
219 {
220 struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
221
222 pcmcia_function_disable(psc->sc_pf);
223 pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
224 sc->sc_ih = 0;
225 }
226
227 void
228 ep_pcmcia_attach(parent, self, aux)
229 struct device *parent, *self;
230 void *aux;
231 {
232 struct ep_pcmcia_softc *psc = (void *) self;
233 struct ep_softc *sc = &psc->sc_ep;
234 struct pcmcia_attach_args *pa = aux;
235 struct pcmcia_config_entry *cfe;
236 const struct ep_pcmcia_product *epp;
237 u_int8_t myla[ETHER_ADDR_LEN];
238 u_int8_t *enaddr = NULL;
239 int i;
240 int error;
241
242 psc->sc_pf = pa->pf;
243
244 SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
245 if (cfe->num_memspace != 0)
246 continue;
247 if (cfe->num_iospace != 1)
248 continue;
249
250 if (pa->product == PCMCIA_PRODUCT_3COM_3C562) {
251 /*
252 * the 3c562 can only use 0x??00-0x??7f
253 * according to the Linux driver
254 */
255
256 /*
257 * 3c562 i/o may decodes address line not only A0-3
258 * but also A7. Anyway, we must sweep at most
259 * [0x0000, 0x0100). The address higher is given by a
260 * pcmcia bridge. But pcmcia bus-space allocation
261 * function implies cards will decode 10-bit address
262 * line. So we must search [0x0000, 0x0400).
263 *
264 * XXX: We must not check the bunch of I/O space range
265 * [0x400*n, 0x300 + 0x400*n) because they are
266 * reserved for legacy ISA devices and their alias
267 * images on PC/AT architecture.
268 */
269 for (i = 0x0300; i < 0x0380; i += 0x10) {
270 if (pcmcia_io_alloc(pa->pf, i,
271 cfe->iospace[0].length,
272 cfe->iospace[0].length,
273 &psc->sc_pcioh) == 0)
274 break;
275 }
276 if (i != 0x0380)
277 break;
278 } else {
279 if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
280 cfe->iospace[0].length, cfe->iospace[0].length,
281 &psc->sc_pcioh) == 0)
282 break;
283 }
284 }
285 if (!cfe) {
286 aprint_error("%s: failed to allocate I/O space\n",
287 self->dv_xname);
288 goto ioalloc_failed;
289 }
290
291 sc->sc_iot = psc->sc_pcioh.iot;
292 sc->sc_ioh = psc->sc_pcioh.ioh;
293
294 /* Enable the card. */
295 pcmcia_function_init(pa->pf, cfe);
296
297 if (pcmcia_io_map(pa->pf, ((cfe->flags & PCMCIA_CFE_IO16) ?
298 PCMCIA_WIDTH_AUTO : PCMCIA_WIDTH_IO8), &psc->sc_pcioh,
299 &psc->sc_io_window)) {
300 aprint_error("%s: can't map i/o space\n", self->dv_xname);
301 goto iomap_failed;
302 }
303
304 error = ep_pcmcia_enable(sc);
305 if (error)
306 goto enable_failed;
307 sc->enabled = 1;
308
309 switch (pa->product) {
310 case PCMCIA_PRODUCT_3COM_3C562:
311 /*
312 * 3c562a-c use this; 3c562d does it in the regular way.
313 * we might want to check the revision and produce a warning
314 * in the future.
315 */
316 /* FALLTHROUGH */
317 case PCMCIA_PRODUCT_3COM_3C574:
318 case PCMCIA_PRODUCT_3COM_3CCFEM556BI:
319 /*
320 * Apparently, some 3c574s do it this way, as well.
321 */
322 if (pcmcia_scan_cis(parent, ep_pcmcia_get_enaddr, myla))
323 enaddr = myla;
324 break;
325 }
326
327 epp = pcmcia_product_lookup(pa, ep_pcmcia_products, ep_pcmcia_nproducts,
328 sizeof(ep_pcmcia_products[0]), NULL);
329 if (!epp)
330 panic("ep_pcmcia_attach: impossible");
331
332 sc->bustype = ELINK_BUS_PCMCIA;
333 sc->ep_flags = epp->epp_flags;
334
335 sc->enable = ep_pcmcia_enable;
336 sc->disable = ep_pcmcia_disable;
337
338 if (epconfig(sc, epp->epp_chipset, enaddr))
339 aprint_error("%s: couldn't configure controller\n",
340 self->dv_xname);
341
342 psc->sc_powerhook = powerhook_establish(self->dv_xname, ep_power, sc);
343 if (psc->sc_powerhook == NULL)
344 aprint_error("%s: WARNING: unable to establish power hook\n",
345 self->dv_xname);
346
347 sc->enabled = 0;
348 ep_pcmcia_disable(sc);
349 return;
350
351 enable_failed:
352 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
353 iomap_failed:
354 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
355 ioalloc_failed:
356 psc->sc_io_window = -1;
357 }
358
359 int
360 ep_pcmcia_detach(self, flags)
361 struct device *self;
362 int flags;
363 {
364 struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *)self;
365 int rv;
366
367 if (psc->sc_io_window == -1)
368 /* Nothing to detach. */
369 return (0);
370
371 if (psc->sc_powerhook != NULL)
372 powerhook_disestablish(psc->sc_powerhook);
373
374 rv = ep_detach(self, flags);
375 if (rv != 0)
376 return (rv);
377
378 /* Unmap our i/o window. */
379 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
380
381 /* Free our i/o space. */
382 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
383
384 return (0);
385 }
386
387 int
388 ep_pcmcia_get_enaddr(tuple, arg)
389 struct pcmcia_tuple *tuple;
390 void *arg;
391 {
392 u_int8_t *myla = arg;
393 int i;
394
395 /* this is 3c562a-c magic */
396 if (tuple->code == 0x88) {
397 if (tuple->length < ETHER_ADDR_LEN)
398 return (0);
399
400 for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
401 myla[i] = pcmcia_tuple_read_1(tuple, i + 1);
402 myla[i + 1] = pcmcia_tuple_read_1(tuple, i);
403 }
404
405 return (1);
406 }
407 return (0);
408 }
409