1 /*-
2  * Copyright (c) 2000 Mitsuru IWASAKI
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD: stable/10/sys/dev/ex/if_ex_pccard.c 182227 2008-08-27 04:11:03Z imp $");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/socket.h>
35 
36 #include <sys/module.h>
37 #include <sys/bus.h>
38 
39 #include <machine/bus.h>
40 #include <machine/resource.h>
41 #include <sys/rman.h>
42 
43 #include <net/if.h>
44 #include <net/if_arp.h>
45 #include <net/if_media.h>
46 
47 #include <dev/ex/if_exreg.h>
48 #include <dev/ex/if_exvar.h>
49 
50 #include <dev/pccard/pccardvar.h>
51 #include <dev/pccard/pccard_cis.h>
52 #include "pccarddevs.h"
53 
54 static const struct pccard_product ex_pccard_products[] = {
55 	PCMCIA_CARD(OLICOM, OC2220),
56 	PCMCIA_CARD(OLICOM, OC2231),
57 	PCMCIA_CARD(OLICOM, OC2232),
58 	PCMCIA_CARD(INTEL, ETHEREXPPRO),
59 	{ NULL }
60 };
61 
62 /* Bus Front End Functions */
63 static int	ex_pccard_probe(device_t);
64 static int	ex_pccard_attach(device_t);
65 
66 static int
ex_pccard_enet_ok(u_char * enaddr)67 ex_pccard_enet_ok(u_char *enaddr)
68 {
69 	int			i;
70 	u_char			sum;
71 
72 	if (enaddr[0] == 0xff)
73 		return (0);
74 	for (i = 0, sum = 0; i < ETHER_ADDR_LEN; i++)
75 		sum |= enaddr[i];
76 	return (sum != 0);
77 }
78 
79 static int
ex_pccard_silicom_cb(const struct pccard_tuple * tuple,void * arg)80 ex_pccard_silicom_cb(const struct pccard_tuple *tuple, void *arg)
81 {
82 	u_char *enaddr = arg;
83 	int i;
84 
85 	if (tuple->code != CISTPL_FUNCE)
86 		return (0);
87 	if (tuple->length != 15)
88 		return (0);
89 	if (pccard_tuple_read_1(tuple, 6) != 6)
90 		return (0);
91 	for (i = 0; i < 6; i++)
92 		enaddr[i] = pccard_tuple_read_1(tuple, 7 + i);
93 	return (1);
94 }
95 
96 static void
ex_pccard_get_silicom_mac(device_t dev,u_char * ether_addr)97 ex_pccard_get_silicom_mac(device_t dev, u_char *ether_addr)
98 {
99 	pccard_cis_scan(dev, ex_pccard_silicom_cb, ether_addr);
100 }
101 
102 static int
ex_pccard_probe(device_t dev)103 ex_pccard_probe(device_t dev)
104 {
105 	const struct pccard_product *pp;
106 	int error, i, j;
107 	uint32_t	fcn = PCCARD_FUNCTION_UNSPEC;
108 
109 	if ((pp = pccard_product_lookup(dev, ex_pccard_products,
110 	    sizeof(ex_pccard_products[0]), NULL)) == NULL)
111 		return (EIO);
112 	if (pp->pp_name != NULL)
113 		device_set_desc(dev, pp->pp_name);
114 	/*
115 	 * Olicom 22.8k and 33.6k modems need to activate the right
116 	 * CFE.  The odd formula below replicates the sequence of cfes
117 	 * that have multiple resources:
118 	 *	 9, 11, 13, 15,		0 + 9
119 	 *	25, 27, 29, 31,		16 + 9
120 	 *	41, 43, 45, 47,		32 + 9
121 	 *	57, 59, 61, 63		48 + 9
122 	 * (entries 8, 24, 40 and 56 are single resoruce cfes)
123 	 * Fortunately the code that enables and disables the multiple
124 	 * fuctions of the card won't mess with the lower bit for cards
125 	 * that aren't stanards conforming MFC cards (which these olicom
126 	 * cards aren't).
127 	 *
128 	 * Note: These cards still don't get interrupts for reasons
129 	 * unknown, even when the right cfe is selected.  There's likely
130 	 * something in the CCR that needs to be manually tweaked, but
131 	 * the COR bits seem to all be used.  Bit 0 and 3 are always set
132 	 * and the other bits select the config to use.  Maybe one of those
133 	 * two bits needs to be cleared, or there's something else in the
134 	 * CCR that needs tweaking.  The pattern of resources suggests
135 	 * bit 0 turns on the ethernet, however...
136 	 */
137 	if (pp->pp_vendor == PCMCIA_VENDOR_OLICOM &&
138 	    (pp->pp_product == PCMCIA_PRODUCT_OLICOM_OC2231 ||
139 	    pp->pp_product == PCMCIA_PRODUCT_OLICOM_OC2232)) {
140 		if (pccard_select_cfe(dev, 1) == 0)
141 			goto good;
142 		for (i = 0; i < 4; i++) {
143 			for (j = 0; j < 4; j++) {
144 				printf("Trying %d %d\n", i, j);
145 				if (pccard_select_cfe(dev,
146 				    (i << 4) + (j << 1) + 9) == 0)
147 					goto good;
148 			}
149 		}
150 		/* Can't activate the net entries, punt */
151 		return (EIO);
152 	}
153 	/*
154 	 * All other cards supported by this driver don't need specail
155 	 * treatment, so just filter based on the type of card.  The
156 	 * special treatment ones are setup to 'fail safe' to a modem so
157 	 * this check would effectively filter them out as well.
158 	 */
159 	error = pccard_get_function(dev, &fcn);
160 	if (error != 0)
161 		return (error);
162 	if (fcn != PCCARD_FUNCTION_NETWORK)
163 		return (EIO);
164 good:;
165 	return (0);
166 }
167 
168 static int
ex_pccard_attach(device_t dev)169 ex_pccard_attach(device_t dev)
170 {
171 	struct ex_softc *	sc = device_get_softc(dev);
172 	int			error = 0;
173 	u_char			ether_addr[ETHER_ADDR_LEN];
174 
175 	sc->dev = dev;
176 	sc->ioport_rid = 0;
177 	sc->irq_rid = 0;
178 
179 	if ((error = ex_alloc_resources(dev)) != 0) {
180 		device_printf(dev, "ex_alloc_resources() failed!\n");
181 		goto bad;
182 	}
183 
184 	/*
185 	 * Fill in several fields of the softc structure:
186 	 *	- Hardware Ethernet address.
187 	 *	- IRQ number.
188 	 */
189 	sc->irq_no = rman_get_start(sc->irq);
190 
191 	/* Try to get the ethernet address from the chip, then the CIS */
192 	ex_get_address(sc, ether_addr);
193 	if (!ex_pccard_enet_ok(ether_addr))
194 		pccard_get_ether(dev, ether_addr);
195 	if (!ex_pccard_enet_ok(ether_addr))
196 		ex_pccard_get_silicom_mac(dev, ether_addr);
197 	if (!ex_pccard_enet_ok(ether_addr)) {
198 		device_printf(dev, "No NIC address found.\n");
199 		error = ENXIO;
200 		goto bad;
201 	}
202 	bcopy(ether_addr, sc->enaddr, ETHER_ADDR_LEN);
203 
204 	if ((error = ex_attach(dev)) != 0) {
205 		device_printf(dev, "ex_attach() failed!\n");
206 		goto bad;
207 	}
208 
209 	return(0);
210 bad:
211 	ex_release_resources(dev);
212 	return (error);
213 }
214 static device_method_t ex_pccard_methods[] = {
215 	/* Device interface */
216 	DEVMETHOD(device_probe,		ex_pccard_probe),
217 	DEVMETHOD(device_attach,	ex_pccard_attach),
218 	DEVMETHOD(device_detach,	ex_detach),
219 
220 	{ 0, 0 }
221 };
222 
223 static driver_t ex_pccard_driver = {
224 	"ex",
225 	ex_pccard_methods,
226 	sizeof(struct ex_softc),
227 };
228 
229 DRIVER_MODULE(ex, pccard, ex_pccard_driver, ex_devclass, 0, 0);
230 MODULE_DEPEND(ex, pccard, 1, 1, 1);
231