xref: /netbsd/sys/dev/pcmcia/if_ep_pcmcia.c (revision c4a72b64)
1 /*	$NetBSD: if_ep_pcmcia.c,v 1.41 2002/10/02 16:52:11 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 2000 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.
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.41 2002/10/02 16:52:11 thorpej 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 __P((struct device *, struct cfdata *, void *));
101 void	ep_pcmcia_attach __P((struct device *, struct device *, void *));
102 int	ep_pcmcia_detach __P((struct device *, int));
103 
104 int	ep_pcmcia_get_enaddr __P((struct pcmcia_tuple *, void *));
105 int	ep_pcmcia_enable __P((struct ep_softc *));
106 void	ep_pcmcia_disable __P((struct ep_softc *));
107 
108 int	ep_pcmcia_enable1 __P((struct ep_softc *));
109 void	ep_pcmcia_disable1 __P((struct ep_softc *));
110 
111 struct ep_pcmcia_softc {
112 	struct ep_softc sc_ep;			/* real "ep" softc */
113 
114 	/* PCMCIA-specific goo */
115 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
116 	int sc_io_window;			/* our i/o window */
117 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
118 };
119 
120 CFATTACH_DECL(ep_pcmcia, sizeof(struct ep_pcmcia_softc),
121     ep_pcmcia_match, ep_pcmcia_attach, ep_pcmcia_detach, ep_activate);
122 
123 const struct ep_pcmcia_product {
124 	struct pcmcia_product epp_product;
125 	u_short		epp_chipset;	/* 3Com chipset used */
126 	int		epp_flags;	/* initial softc flags */
127 } ep_pcmcia_products[] = {
128 	{ { PCMCIA_STR_3COM_3C562,		PCMCIA_VENDOR_3COM,
129 	    PCMCIA_PRODUCT_3COM_3C562,		0 },
130 	  ELINK_CHIPSET_3C509, 0 },
131 
132 	{ { PCMCIA_STR_3COM_3C589,		PCMCIA_VENDOR_3COM,
133 	    PCMCIA_PRODUCT_3COM_3C589,		0 },
134 	  ELINK_CHIPSET_3C509, 0 },
135 
136 	{ { PCMCIA_STR_3COM_3CXEM556,		PCMCIA_VENDOR_3COM,
137 	    PCMCIA_PRODUCT_3COM_3CXEM556,	0 },
138 	  ELINK_CHIPSET_3C509, 0 },
139 
140 	{ { PCMCIA_STR_3COM_3CXEM556INT,	PCMCIA_VENDOR_3COM,
141 	    PCMCIA_PRODUCT_3COM_3CXEM556INT,	0 },
142 	  ELINK_CHIPSET_3C509, 0 },
143 
144 	{ { PCMCIA_STR_3COM_3C574,		PCMCIA_VENDOR_3COM,
145 	    PCMCIA_PRODUCT_3COM_3C574,		0 },
146 	  ELINK_CHIPSET_ROADRUNNER, ELINK_FLAGS_MII },
147 
148 	{ { PCMCIA_STR_3COM_3CCFEM556BI,	PCMCIA_VENDOR_3COM,
149 	    PCMCIA_PRODUCT_3COM_3CCFEM556BI,	0 },
150 	  ELINK_CHIPSET_ROADRUNNER, ELINK_FLAGS_MII },
151 
152 	{ { PCMCIA_STR_3COM_3C1,		PCMCIA_VENDOR_3COM,
153 	    PCMCIA_PRODUCT_3COM_3C1,		0 },
154 	  ELINK_CHIPSET_3C509, 0 },
155 
156 	{ { NULL } }
157 };
158 
159 int
160 ep_pcmcia_match(parent, match, aux)
161 	struct device *parent;
162 	struct cfdata *match;
163 	void *aux;
164 {
165 	struct pcmcia_attach_args *pa = aux;
166 
167 	if (pcmcia_product_lookup(pa,
168 	    (const struct pcmcia_product *)ep_pcmcia_products,
169 	    sizeof ep_pcmcia_products[0], NULL) != NULL)
170 		return (1);
171 
172 	return (0);
173 }
174 
175 int
176 ep_pcmcia_enable(sc)
177 	struct ep_softc *sc;
178 {
179 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
180 	struct pcmcia_function *pf = psc->sc_pf;
181 	int error;
182 
183 	if ((error = ep_pcmcia_enable1(sc)) != 0)
184 		return error;
185 
186 	/* establish the interrupt. */
187 	sc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, epintr, sc);
188 	if (sc->sc_ih == NULL) {
189 		printf("%s: couldn't establish interrupt\n",
190 		    sc->sc_dev.dv_xname);
191 		ep_pcmcia_disable1(sc);
192 		return (1);
193 	}
194 
195 	return 0;
196 }
197 
198 int
199 ep_pcmcia_enable1(sc)
200 	struct ep_softc *sc;
201 {
202 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
203 	struct pcmcia_function *pf = psc->sc_pf;
204 	int ret;
205 
206 	if ((ret = pcmcia_function_enable(pf)))
207 		return (ret);
208 
209 	if ((psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3C562) ||
210 	    (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556) ||
211 	    (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556INT)) {
212 		int reg;
213 
214 		/* turn off the serial-disable bit */
215 
216 		reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
217 		if (reg & 0x08) {
218 			reg &= ~0x08;
219 			pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
220 		}
221 
222 	}
223 
224 	return (ret);
225 }
226 
227 void
228 ep_pcmcia_disable(sc)
229 	struct ep_softc *sc;
230 {
231 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
232 
233 	/*
234 	 * We must disestablish the interrupt before disabling the function,
235 	 * because on a multifunction card the interrupt disestablishment
236 	 * accesses CCR registers.
237 	 */
238 	pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
239 	ep_pcmcia_disable1(sc);
240 }
241 
242 void
243 ep_pcmcia_disable1(sc)
244 	struct ep_softc *sc;
245 {
246 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
247 
248 	pcmcia_function_disable(psc->sc_pf);
249 }
250 
251 void
252 ep_pcmcia_attach(parent, self, aux)
253 	struct device  *parent, *self;
254 	void           *aux;
255 {
256 	struct ep_pcmcia_softc *psc = (void *) self;
257 	struct ep_softc *sc = &psc->sc_ep;
258 	struct pcmcia_attach_args *pa = aux;
259 	struct pcmcia_config_entry *cfe;
260 	const struct ep_pcmcia_product *epp;
261 	u_int8_t myla[ETHER_ADDR_LEN];
262 	u_int8_t *enaddr = NULL;
263 	int i;
264 
265 	psc->sc_pf = pa->pf;
266 	cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head);
267 
268 	/* Enable the card. */
269 	pcmcia_function_init(pa->pf, cfe);
270 	if (ep_pcmcia_enable1(sc)) {
271 		printf(": function enable failed\n");
272 		goto enable_failed;
273 	}
274 	sc->enabled = 1;
275 
276 	if (cfe->num_memspace != 0) {
277 		printf(": unexpected number of memory spaces %d should be 0\n",
278 		    cfe->num_memspace);
279 		goto ioalloc_failed;
280 	}
281 
282 	if (cfe->num_iospace != 1) {
283 		printf(": unexpected number of I/O spaces %d should be 1\n",
284 		    cfe->num_iospace);
285 		goto ioalloc_failed;
286 	}
287 
288 	if (pa->product == PCMCIA_PRODUCT_3COM_3C562) {
289 		/*
290 		 * the 3c562 can only use 0x??00-0x??7f
291 		 * according to the Linux driver
292 		 */
293 
294 		/*
295 		 * 3c562 i/o may decodes address line not only A0-3
296 		 * but also A7.  Anyway, we must sweep at most
297 		 * [0x0000, 0x0100).  The address higher is given by a
298 		 * pcmcia bridge.  But pcmcia bus-space allocation
299 		 * function implies cards will decode 10-bit address
300 		 * line.  So we must search [0x0000, 0x0400).
301 		 *
302 		 * XXX: We must not check the bunch of I/O space range
303 		 * [0x400*n, 0x300 + 0x400*n) because they are
304 		 * reserved for legacy ISA devices and their alias
305 		 * images on PC/AT architecture.
306 		 */
307 		for (i = 0x0300; i < 0x0380; i += 0x10) {
308 			if (pcmcia_io_alloc(pa->pf, i, cfe->iospace[0].length,
309 			    0, &psc->sc_pcioh) == 0)
310 				break;
311 		}
312 		if (i == 0x0380) {
313 			printf(": can't allocate i/o space\n");
314 			goto ioalloc_failed;
315 		}
316 	} else {
317 		if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
318 		    cfe->iospace[0].length, &psc->sc_pcioh)) {
319 			printf(": can't allocate i/o space\n");
320 			goto ioalloc_failed;
321 		}
322 	}
323 
324 	sc->sc_iot = psc->sc_pcioh.iot;
325 	sc->sc_ioh = psc->sc_pcioh.ioh;
326 
327 	if (pcmcia_io_map(pa->pf, ((cfe->flags & PCMCIA_CFE_IO16) ?
328 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8), 0, cfe->iospace[0].length,
329 	    &psc->sc_pcioh, &psc->sc_io_window)) {
330 		printf(": can't map i/o space\n");
331 		goto iomap_failed;
332 	}
333 
334 	switch (pa->product) {
335 	case PCMCIA_PRODUCT_3COM_3C562:
336 		/*
337 		 * 3c562a-c use this; 3c562d does it in the regular way.
338 		 * we might want to check the revision and produce a warning
339 		 * in the future.
340 		 */
341 		/* FALLTHROUGH */
342 	case PCMCIA_PRODUCT_3COM_3C574:
343 	case PCMCIA_PRODUCT_3COM_3CCFEM556BI:
344 		/*
345 		 * Apparently, some 3c574s do it this way, as well.
346 		 */
347 		if (pcmcia_scan_cis(parent, ep_pcmcia_get_enaddr, myla))
348 			enaddr = myla;
349 		break;
350 	}
351 
352 	epp = (const struct ep_pcmcia_product *)pcmcia_product_lookup(pa,
353             (const struct pcmcia_product *)ep_pcmcia_products,
354             sizeof ep_pcmcia_products[0], NULL);
355 	if (epp == NULL)
356 		panic("ep_pcmcia_attach: impossible");
357 
358 	printf(": %s\n", epp->epp_product.pp_name);
359 
360 	sc->bustype = ELINK_BUS_PCMCIA;
361 	sc->ep_flags = epp->epp_flags;
362 
363 	sc->enable = ep_pcmcia_enable;
364 	sc->disable = ep_pcmcia_disable;
365 
366 	if (epconfig(sc, epp->epp_chipset, enaddr)) {
367 		printf("%s: couldn't configure controller\n",
368 		    sc->sc_dev.dv_xname);
369 		goto config_failed;
370 	}
371 
372 	sc->enabled = 0;
373 	ep_pcmcia_disable1(sc);
374 	return;
375 
376  config_failed:
377 	/* Unmap our i/o window. */
378 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
379 
380  iomap_failed:
381 	/* Free our i/o space. */
382 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
383 
384  ioalloc_failed:
385 	sc->enabled = 0;
386 	ep_pcmcia_disable1(sc);
387 
388  enable_failed:
389 	psc->sc_io_window = -1;
390 }
391 
392 int
393 ep_pcmcia_detach(self, flags)
394 	struct device *self;
395 	int flags;
396 {
397 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *)self;
398 	int rv;
399 
400 	if (psc->sc_io_window == -1)
401 		/* Nothing to detach. */
402 		return (0);
403 
404 	rv = ep_detach(self, flags);
405 	if (rv != 0)
406 		return (rv);
407 
408 	/* Unmap our i/o window. */
409 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
410 
411 	/* Free our i/o space. */
412 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
413 
414 	return (0);
415 }
416 
417 int
418 ep_pcmcia_get_enaddr(tuple, arg)
419 	struct pcmcia_tuple *tuple;
420 	void *arg;
421 {
422 	u_int8_t *myla = arg;
423 	int i;
424 
425 	/* this is 3c562a-c magic */
426 	if (tuple->code == 0x88) {
427 		if (tuple->length < ETHER_ADDR_LEN)
428 			return (0);
429 
430 		for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
431 			myla[i] = pcmcia_tuple_read_1(tuple, i + 1);
432 			myla[i + 1] = pcmcia_tuple_read_1(tuple, i);
433 		}
434 
435 		return (1);
436 	}
437 	return (0);
438 }
439