xref: /freebsd-12.1/sys/dev/fe/if_fe_pccard.c (revision f6cea53f)
1 /*-
2  * All Rights Reserved, Copyright (C) Fujitsu Limited 1995
3  *
4  * This software may be used, modified, copied, distributed, and sold, in
5  * both source and binary form provided that the above copyright, these
6  * terms and the following disclaimer are retained.  The name of the author
7  * and/or the contributor may not be used to endorse or promote products
8  * derived from this software without specific prior written permission.
9  *
10  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND THE CONTRIBUTOR ``AS IS'' AND
11  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
12  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
13  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR THE CONTRIBUTOR BE LIABLE
14  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
15  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
16  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION.
17  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
18  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
19  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
20  * SUCH DAMAGE.
21  *
22  */
23 
24 #include <sys/cdefs.h>
25 __FBSDID("$FreeBSD$");
26 
27 #include <sys/param.h>
28 #include <sys/kernel.h>
29 #include <sys/socket.h>
30 #include <sys/systm.h>
31 #include <sys/lock.h>
32 #include <sys/module.h>
33 #include <sys/mutex.h>
34 
35 #include <sys/bus.h>
36 #include <machine/bus.h>
37 #include <sys/rman.h>
38 
39 #include <net/ethernet.h>
40 #include <net/if.h>
41 #include <net/if_var.h>
42 #include <net/if_mib.h>
43 #include <net/if_media.h>
44 
45 #include <netinet/in.h>
46 #include <netinet/if_ether.h>
47 
48 #include <dev/fe/mb86960.h>
49 #include <dev/fe/if_fereg.h>
50 #include <dev/fe/if_fevar.h>
51 
52 #include <dev/pccard/pccardvar.h>
53 #include <dev/pccard/pccard_cis.h>
54 
55 #include "card_if.h"
56 #include "pccarddevs.h"
57 
58 /*
59  *	PC Card (PCMCIA) specific code.
60  */
61 static int fe_pccard_probe(device_t);
62 static int fe_pccard_attach(device_t);
63 static int fe_pccard_detach(device_t);
64 
65 static const struct fe_pccard_product {
66 	struct pccard_product mpp_product;
67 	int mpp_flags;
68 	int mpp_cfe;
69 #define MPP_MBH10302 1
70 #define MPP_ANYFUNC 2
71 #define MPP_SKIP_TO_CFE 4
72 } fe_pccard_products[] = {
73 	/* These need to be first */
74 	{ PCMCIA_CARD(FUJITSU2, FMV_J181), MPP_MBH10302 },
75 	{ PCMCIA_CARD(FUJITSU2, FMV_J182), 0 },
76 	{ PCMCIA_CARD(FUJITSU2, FMV_J182A), 0 },
77 	{ PCMCIA_CARD(FUJITSU2, ITCFJ182A), 0 },
78 	/* These need to be second */
79 	{ PCMCIA_CARD(TDK, LAK_CD011), 0 },
80 	{ PCMCIA_CARD(TDK, LAK_CD021BX), 0 },
81 	{ PCMCIA_CARD(TDK, LAK_CF010), 0 },
82 #if 0 /* XXX 86960-based? */
83 	{ PCMCIA_CARD(TDK, LAK_DFL9610), 0 },
84 #endif
85 	{ PCMCIA_CARD(CONTEC, CNETPC), MPP_SKIP_TO_CFE, 2 },
86 	{ PCMCIA_CARD(FUJITSU, LA501), 0 },
87 	{ PCMCIA_CARD(FUJITSU, LA10S), 0 },
88 	{ PCMCIA_CARD(FUJITSU, NE200T), MPP_MBH10302 },/* Sold by Eagle */
89 	{ PCMCIA_CARD(HITACHI, HT_4840), MPP_MBH10302 | MPP_SKIP_TO_CFE, 10 },
90 	{ PCMCIA_CARD(RATOC, REX_R280), 0 },
91 	{ PCMCIA_CARD(XIRCOM, CE), MPP_ANYFUNC },
92 	{ { NULL } }
93 };
94 
95 static int
fe_pccard_probe(device_t dev)96 fe_pccard_probe(device_t dev)
97 {
98 	int		error;
99 	uint32_t	fcn = PCCARD_FUNCTION_UNSPEC;
100 	const struct fe_pccard_product *pp;
101 	int i;
102 
103 	if ((pp = (const struct fe_pccard_product *)pccard_product_lookup(dev,
104 		 (const struct pccard_product *)fe_pccard_products,
105 		 sizeof(fe_pccard_products[0]), NULL)) != NULL) {
106 		if (pp->mpp_product.pp_name != NULL)
107 			device_set_desc(dev, pp->mpp_product.pp_name);
108 		if (pp->mpp_flags & MPP_ANYFUNC)
109 			return (0);
110 		/* Make sure we're a network function */
111 		error = pccard_get_function(dev, &fcn);
112 		if (error != 0)
113 			return (error);
114 		if (fcn != PCCARD_FUNCTION_NETWORK)
115 			return (ENXIO);
116 		if (pp->mpp_flags & MPP_SKIP_TO_CFE) {
117 			for (i = pp->mpp_cfe; i < 32; i++) {
118 				if (pccard_select_cfe(dev, i) == 0)
119 					goto good;
120 			}
121 			device_printf(dev,
122 			    "Failed to map CFE %d or higher\n", pp->mpp_cfe);
123 			return ENXIO;
124 		}
125 	good:;
126 		return (0);
127 	}
128 	return (ENXIO);
129 }
130 
131 static device_method_t fe_pccard_methods[] = {
132 	/* Device interface */
133 	DEVMETHOD(device_probe,		fe_pccard_probe),
134 	DEVMETHOD(device_attach,	fe_pccard_attach),
135 	DEVMETHOD(device_detach,	fe_pccard_detach),
136 
137 	{ 0, 0 }
138 };
139 
140 static driver_t fe_pccard_driver = {
141 	"fe",
142 	fe_pccard_methods,
143 	sizeof (struct fe_softc)
144 };
145 
146 DRIVER_MODULE(fe, pccard, fe_pccard_driver, fe_devclass, 0, 0);
147 MODULE_DEPEND(fe, pccard, 1, 1, 1);
148 PCCARD_PNP_INFO(fe_pccard_products);
149 
150 static int fe_probe_mbh(device_t, const struct fe_pccard_product *);
151 static int fe_probe_tdk(device_t, const struct fe_pccard_product *);
152 
153 static int
fe_pccard_attach(device_t dev)154 fe_pccard_attach(device_t dev)
155 {
156 	struct fe_softc *sc;
157 	const struct fe_pccard_product *pp;
158 	int error;
159 
160 	/* Prepare for the device probe process.  */
161 	sc = device_get_softc(dev);
162 	sc->sc_unit = device_get_unit(dev);
163 
164 	pp = (const struct fe_pccard_product *) pccard_product_lookup(dev,
165 	    (const struct pccard_product *)fe_pccard_products,
166 	    sizeof(fe_pccard_products[0]), NULL);
167 	if (pp == NULL)
168 		return (ENXIO);
169 
170 	if (pp->mpp_flags & MPP_MBH10302)
171 		error = fe_probe_mbh(dev, pp);
172 	else
173 		error = fe_probe_tdk(dev, pp);
174 	if (error != 0) {
175 		fe_release_resource(dev);
176 		return (error);
177 	}
178 	error = fe_alloc_irq(dev, 0);
179 	if (error != 0) {
180 		fe_release_resource(dev);
181 		return (error);
182 	}
183 	return (fe_attach(dev));
184 }
185 
186 /*
187  *	feunload - unload the driver and clear the table.
188  */
189 static int
fe_pccard_detach(device_t dev)190 fe_pccard_detach(device_t dev)
191 {
192 	struct fe_softc *sc = device_get_softc(dev);
193 	struct ifnet *ifp = sc->ifp;
194 
195 	FE_LOCK(sc);
196 	fe_stop(sc);
197 	FE_UNLOCK(sc);
198 	callout_drain(&sc->timer);
199 	ether_ifdetach(ifp);
200 	bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
201 	if_free(ifp);
202 	fe_release_resource(dev);
203 	mtx_destroy(&sc->lock);
204 
205 	return 0;
206 }
207 
208 
209 /*
210  * Probe and initialization for Fujitsu MBH10302 PCMCIA Ethernet interface.
211  * Note that this is for 10302 only; MBH10304 is handled by fe_probe_tdk().
212  */
213 static void
fe_init_mbh(struct fe_softc * sc)214 fe_init_mbh(struct fe_softc *sc)
215 {
216 	/* Minimal initialization of 86960.  */
217 	DELAY(200);
218 	fe_outb(sc, FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
219 	DELAY(200);
220 
221 	/* Disable all interrupts.  */
222 	fe_outb(sc, FE_DLCR2, 0);
223 	fe_outb(sc, FE_DLCR3, 0);
224 
225 	/* Enable master interrupt flag.  */
226 	fe_outb(sc, FE_MBH0, FE_MBH0_MAGIC | FE_MBH0_INTR_ENABLE);
227 }
228 
229 static int
fe_probe_mbh(device_t dev,const struct fe_pccard_product * pp)230 fe_probe_mbh(device_t dev, const struct fe_pccard_product *pp)
231 {
232 	struct fe_softc *sc = device_get_softc(dev);
233 
234 	static struct fe_simple_probe_struct probe_table [] = {
235 		{ FE_DLCR2, 0x58, 0x00 },
236 		{ FE_DLCR4, 0x08, 0x00 },
237 		{ FE_DLCR6, 0xFF, 0xB6 },
238 		{ 0 }
239 	};
240 
241 	/* MBH10302 occupies 32 I/O addresses. */
242 	if (fe_alloc_port(dev, 32))
243 		return ENXIO;
244 
245 	/* Fill the softc struct with default values.  */
246 	fe_softc_defaults(sc);
247 
248 	/*
249 	 * See if MBH10302 is on its address.
250 	 * I'm not sure the following probe code works.  FIXME.
251 	 */
252 	if (!fe_simple_probe(sc, probe_table))
253 		return ENXIO;
254 
255 	/* Get our station address from EEPROM.  */
256 	fe_inblk(sc, FE_MBH10, sc->enaddr, ETHER_ADDR_LEN);
257 
258 	/* Make sure we got a valid station address.  */
259 	if (!fe_valid_Ether_p(sc->enaddr, 0))
260 		return ENXIO;
261 
262 	/* Determine the card type.  */
263 	sc->type = FE_TYPE_MBH;
264 	sc->typestr = "MBH10302 (PCMCIA)";
265 
266 	/* We seems to need our own IDENT bits...  FIXME.  */
267 	sc->proto_dlcr7 = FE_D7_BYTSWP_LH | FE_D7_IDENT_NICE;
268 
269 	/* Setup hooks.  We need a special initialization procedure.  */
270 	sc->init = fe_init_mbh;
271 
272 	return 0;
273 }
274 
275 static int
fe_pccard_xircom_mac(const struct pccard_tuple * tuple,void * argp)276 fe_pccard_xircom_mac(const struct pccard_tuple *tuple, void *argp)
277 {
278 	uint8_t *enaddr = argp;
279 	int i;
280 
281 #if 1
282 	/*
283 	 * We fail to map the CIS twice, for reasons unknown.  We
284 	 * may fix this in the future by loading the CIS with a sane
285 	 * CIS from userland.
286 	 */
287 	static uint8_t defaultmac[ETHER_ADDR_LEN] = {
288 		0x00, 0x80, 0xc7, 0xed, 0x16, 0x7b};
289 
290 	/* Copy the MAC ADDR and return success */
291 	for (i = 0; i < ETHER_ADDR_LEN; i++)
292 		enaddr[i] = defaultmac[i];
293 #else
294 	/* FUNCE is not after FUNCID, so we gotta go find it */
295 	if (tuple->code != 0x22)
296 		return (0);
297 
298 	/* Make sure this is a sane node */
299 	if (tuple->length < ETHER_ADDR_LEN + 3)
300 		return (0);
301 
302 	/* Copy the MAC ADDR and return success */
303 	for (i = 0; i < ETHER_ADDR_LEN; i++)
304 		enaddr[i] = pccard_tuple_read_1(tuple, i + 3);
305 #endif
306 	return (1);
307 }
308 
309 /*
310  * Probe and initialization for TDK/CONTEC PCMCIA Ethernet interface.
311  * by MASUI Kenji <[email protected]>
312  *
313  * (Contec uses TDK Ethernet chip -- hosokawa)
314  *
315  * This version of fe_probe_tdk has been rewrote to handle
316  * *generic* PC Card implementation of Fujitsu MB8696x family.  The
317  * name _tdk is just for a historical reason. :-)
318  */
319 static int
fe_probe_tdk(device_t dev,const struct fe_pccard_product * pp)320 fe_probe_tdk (device_t dev, const struct fe_pccard_product *pp)
321 {
322 	struct fe_softc *sc = device_get_softc(dev);
323 
324 	static struct fe_simple_probe_struct probe_table [] = {
325 		{ FE_DLCR2, 0x10, 0x00 },
326 		{ FE_DLCR4, 0x08, 0x00 },
327 /*		{ FE_DLCR5, 0x80, 0x00 },	Does not work well.  */
328 		{ 0 }
329 	};
330 
331 
332 	/* C-NET(PC)C occupies 16 I/O addresses. */
333 	if (fe_alloc_port(dev, 16))
334 		return ENXIO;
335 
336 	/* Fill the softc struct with default values.  */
337 	fe_softc_defaults(sc);
338 
339 	/*
340 	 * See if C-NET(PC)C is on its address.
341 	 */
342 	if (!fe_simple_probe(sc, probe_table))
343 		return ENXIO;
344 
345 	/* Determine the card type.  */
346 	sc->type = FE_TYPE_TDK;
347 	sc->typestr = "Generic MB8696x/78Q837x Ethernet (PCMCIA)";
348 
349 	pccard_get_ether(dev, sc->enaddr);
350 
351 	/* Make sure we got a valid station address.  */
352 	if (!fe_valid_Ether_p(sc->enaddr, 0)) {
353 		pccard_cis_scan(dev, fe_pccard_xircom_mac, sc->enaddr);
354 	}
355 
356 	/* Make sure we got a valid station address.  */
357 	if (!fe_valid_Ether_p(sc->enaddr, 0))
358 		return ENXIO;
359 
360 	return 0;
361 }
362