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