if_fe_pccard.c revision 199798
1198090Srdivacky/*-
2198090Srdivacky * All Rights Reserved, Copyright (C) Fujitsu Limited 1995
3198090Srdivacky *
4198090Srdivacky * This software may be used, modified, copied, distributed, and sold, in
5198090Srdivacky * both source and binary form provided that the above copyright, these
6198090Srdivacky * terms and the following disclaimer are retained.  The name of the author
7198090Srdivacky * and/or the contributor may not be used to endorse or promote products
8198090Srdivacky * derived from this software without specific prior written permission.
9249423Sdim *
10249423Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND THE CONTRIBUTOR ``AS IS'' AND
11198090Srdivacky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
12249423Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
13263508Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR THE CONTRIBUTOR BE LIABLE
14263508Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
15263508Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
16218893Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION.
17198090Srdivacky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
18198090Srdivacky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
19239462Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
20198090Srdivacky * SUCH DAMAGE.
21226633Sdim *
22198090Srdivacky */
23198090Srdivacky
24221345Sdim#include <sys/cdefs.h>
25239462Sdim__FBSDID("$FreeBSD: head/sys/dev/fe/if_fe_pccard.c 199798 2009-11-25 13:31:17Z rwatson $");
26198090Srdivacky
27198090Srdivacky#include <sys/param.h>
28198090Srdivacky#include <sys/kernel.h>
29198090Srdivacky#include <sys/socket.h>
30226633Sdim#include <sys/systm.h>
31226633Sdim#include <sys/module.h>
32226633Sdim
33226633Sdim#include <sys/bus.h>
34226633Sdim#include <machine/bus.h>
35226633Sdim#include <sys/rman.h>
36226633Sdim
37226633Sdim#include <net/ethernet.h>
38226633Sdim#include <net/if.h>
39226633Sdim#include <net/if_mib.h>
40226633Sdim#include <net/if_media.h>
41226633Sdim
42226633Sdim#include <netinet/in.h>
43226633Sdim#include <netinet/if_ether.h>
44226633Sdim
45226633Sdim#include <dev/fe/mb86960.h>
46226633Sdim#include <dev/fe/if_fereg.h>
47226633Sdim#include <dev/fe/if_fevar.h>
48226633Sdim
49226633Sdim#include <dev/pccard/pccardvar.h>
50226633Sdim#include <dev/pccard/pccard_cis.h>
51226633Sdim
52226633Sdim#include "card_if.h"
53226633Sdim#include "pccarddevs.h"
54226633Sdim
55226633Sdim/*
56226633Sdim *	PC Card (PCMCIA) specific code.
57226633Sdim */
58198090Srdivackystatic int fe_pccard_probe(device_t);
59226633Sdimstatic int fe_pccard_attach(device_t);
60226633Sdimstatic int fe_pccard_detach(device_t);
61263508Sdim
62263508Sdimstatic const struct fe_pccard_product {
63239462Sdim	struct pccard_product mpp_product;
64198090Srdivacky	int mpp_flags;
65239462Sdim	int mpp_cfe;
66198090Srdivacky#define MPP_MBH10302 1
67198090Srdivacky#define MPP_ANYFUNC 2
68239462Sdim#define MPP_SKIP_TO_CFE 4
69198090Srdivacky} fe_pccard_products[] = {
70198090Srdivacky	/* These need to be first */
71198090Srdivacky	{ PCMCIA_CARD(FUJITSU2, FMV_J181), MPP_MBH10302 },
72198090Srdivacky	{ PCMCIA_CARD(FUJITSU2, FMV_J182), 0 },
73198090Srdivacky	{ PCMCIA_CARD(FUJITSU2, FMV_J182A), 0 },
74198090Srdivacky	{ PCMCIA_CARD(FUJITSU2, ITCFJ182A), 0 },
75198090Srdivacky	/* These need to be second */
76198090Srdivacky	{ PCMCIA_CARD(TDK, LAK_CD011), 0 },
77226633Sdim	{ PCMCIA_CARD(TDK, LAK_CD021BX), 0 },
78226633Sdim	{ PCMCIA_CARD(TDK, LAK_CF010), 0 },
79239462Sdim#if 0 /* XXX 86960-based? */
80226633Sdim	{ PCMCIA_CARD(TDK, LAK_DFL9610), 0 },
81226633Sdim#endif
82226633Sdim	{ PCMCIA_CARD(CONTEC, CNETPC), MPP_SKIP_TO_CFE, 2 },
83198090Srdivacky	{ PCMCIA_CARD(FUJITSU, LA501), 0 },
84198090Srdivacky	{ PCMCIA_CARD(FUJITSU, LA10S), 0 },
85198090Srdivacky	{ PCMCIA_CARD(FUJITSU, NE200T), MPP_MBH10302 },/* Sold by Eagle */
86226633Sdim	{ PCMCIA_CARD(HITACHI, HT_4840), MPP_MBH10302 | MPP_SKIP_TO_CFE, 10 },
87226633Sdim	{ PCMCIA_CARD(RATOC, REX_R280), 0 },
88221345Sdim	{ PCMCIA_CARD(XIRCOM, CE), MPP_ANYFUNC },
89221345Sdim	{ { NULL } }
90221345Sdim};
91221345Sdim
92221345Sdimstatic int
93221345Sdimfe_pccard_probe(device_t dev)
94226633Sdim{
95226633Sdim	int		error;
96221345Sdim	uint32_t	fcn = PCCARD_FUNCTION_UNSPEC;
97221345Sdim	const struct fe_pccard_product *pp;
98221345Sdim	int i;
99221345Sdim
100221345Sdim	if ((pp = (const struct fe_pccard_product *)pccard_product_lookup(dev,
101263508Sdim		 (const struct pccard_product *)fe_pccard_products,
102226633Sdim		 sizeof(fe_pccard_products[0]), NULL)) != NULL) {
103226633Sdim		if (pp->mpp_product.pp_name != NULL)
104226633Sdim			device_set_desc(dev, pp->mpp_product.pp_name);
105263508Sdim		if (pp->mpp_flags & MPP_ANYFUNC)
106221345Sdim			return (0);
107221345Sdim		/* Make sure we're a network function */
108263508Sdim		error = pccard_get_function(dev, &fcn);
109263508Sdim		if (error != 0)
110263508Sdim			return (error);
111263508Sdim		if (fcn != PCCARD_FUNCTION_NETWORK)
112263508Sdim			return (ENXIO);
113263508Sdim		if (pp->mpp_flags & MPP_SKIP_TO_CFE) {
114263508Sdim			for (i = pp->mpp_cfe; i < 32; i++) {
115263508Sdim				if (pccard_select_cfe(dev, i) == 0)
116263508Sdim					goto good;
117263508Sdim			}
118263508Sdim			device_printf(dev,
119263508Sdim			    "Failed to map CFE %d or higher\n", pp->mpp_cfe);
120263508Sdim			return ENXIO;
121263508Sdim		}
122263508Sdim	good:;
123263508Sdim		return (0);
124263508Sdim	}
125263508Sdim	return (ENXIO);
126263508Sdim}
127221345Sdim
128226633Sdimstatic device_method_t fe_pccard_methods[] = {
129226633Sdim	/* Device interface */
130226633Sdim	DEVMETHOD(device_probe,		fe_pccard_probe),
131221345Sdim	DEVMETHOD(device_attach,	fe_pccard_attach),
132221345Sdim	DEVMETHOD(device_detach,	fe_pccard_detach),
133226633Sdim
134263508Sdim	{ 0, 0 }
135263508Sdim};
136226633Sdim
137226633Sdimstatic driver_t fe_pccard_driver = {
138226633Sdim	"fe",
139207618Srdivacky	fe_pccard_methods,
140207618Srdivacky	sizeof (struct fe_softc)
141198090Srdivacky};
142198090Srdivacky
143198090SrdivackyDRIVER_MODULE(fe, pccard, fe_pccard_driver, fe_devclass, 0, 0);
144MODULE_DEPEND(fe, pccard, 1, 1, 1);
145
146static int fe_probe_mbh(device_t, const struct fe_pccard_product *);
147static int fe_probe_tdk(device_t, const struct fe_pccard_product *);
148
149static int
150fe_pccard_attach(device_t dev)
151{
152	struct fe_softc *sc;
153	const struct fe_pccard_product *pp;
154	int error;
155
156	/* Prepare for the device probe process.  */
157	sc = device_get_softc(dev);
158	sc->sc_unit = device_get_unit(dev);
159
160	pp = (const struct fe_pccard_product *) pccard_product_lookup(dev,
161	    (const struct pccard_product *)fe_pccard_products,
162	    sizeof(fe_pccard_products[0]), NULL);
163	if (pp == NULL)
164		return (ENXIO);
165
166	if (pp->mpp_flags & MPP_MBH10302)
167		error = fe_probe_mbh(dev, pp);
168	else
169		error = fe_probe_tdk(dev, pp);
170	if (error != 0) {
171		fe_release_resource(dev);
172		return (error);
173	}
174	error = fe_alloc_irq(dev, 0);
175	if (error != 0) {
176		fe_release_resource(dev);
177		return (error);
178	}
179	return (fe_attach(dev));
180}
181
182/*
183 *	feunload - unload the driver and clear the table.
184 */
185static int
186fe_pccard_detach(device_t dev)
187{
188	struct fe_softc *sc = device_get_softc(dev);
189	struct ifnet *ifp = sc->ifp;
190
191	FE_LOCK(sc);
192	fe_stop(sc);
193	FE_UNLOCK(sc);
194	callout_drain(&sc->timer);
195	ether_ifdetach(ifp);
196	bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
197	if_free(ifp);
198	fe_release_resource(dev);
199	mtx_destroy(&sc->lock);
200
201	return 0;
202}
203
204
205/*
206 * Probe and initialization for Fujitsu MBH10302 PCMCIA Ethernet interface.
207 * Note that this is for 10302 only; MBH10304 is handled by fe_probe_tdk().
208 */
209static void
210fe_init_mbh(struct fe_softc *sc)
211{
212	/* Minimal initialization of 86960.  */
213	DELAY(200);
214	fe_outb(sc, FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
215	DELAY(200);
216
217	/* Disable all interrupts.  */
218	fe_outb(sc, FE_DLCR2, 0);
219	fe_outb(sc, FE_DLCR3, 0);
220
221	/* Enable master interrupt flag.  */
222	fe_outb(sc, FE_MBH0, FE_MBH0_MAGIC | FE_MBH0_INTR_ENABLE);
223}
224
225static int
226fe_probe_mbh(device_t dev, const struct fe_pccard_product *pp)
227{
228	struct fe_softc *sc = device_get_softc(dev);
229
230	static struct fe_simple_probe_struct probe_table [] = {
231		{ FE_DLCR2, 0x58, 0x00 },
232		{ FE_DLCR4, 0x08, 0x00 },
233		{ FE_DLCR6, 0xFF, 0xB6 },
234		{ 0 }
235	};
236
237	/* MBH10302 occupies 32 I/O addresses. */
238	if (fe_alloc_port(dev, 32))
239		return ENXIO;
240
241	/* Fill the softc struct with default values.  */
242	fe_softc_defaults(sc);
243
244	/*
245	 * See if MBH10302 is on its address.
246	 * I'm not sure the following probe code works.  FIXME.
247	 */
248	if (!fe_simple_probe(sc, probe_table))
249		return ENXIO;
250
251	/* Get our station address from EEPROM.  */
252	fe_inblk(sc, FE_MBH10, sc->enaddr, ETHER_ADDR_LEN);
253
254	/* Make sure we got a valid station address.  */
255	if (!fe_valid_Ether_p(sc->enaddr, 0))
256		return ENXIO;
257
258	/* Determine the card type.  */
259	sc->type = FE_TYPE_MBH;
260	sc->typestr = "MBH10302 (PCMCIA)";
261
262	/* We seems to need our own IDENT bits...  FIXME.  */
263	sc->proto_dlcr7 = FE_D7_BYTSWP_LH | FE_D7_IDENT_NICE;
264
265	/* Setup hooks.  We need a special initialization procedure.  */
266	sc->init = fe_init_mbh;
267
268	return 0;
269}
270
271static int
272fe_pccard_xircom_mac(const struct pccard_tuple *tuple, void *argp)
273{
274	uint8_t *enaddr = argp;
275	int i;
276
277#if 1
278	/*
279	 * We fail to map the CIS twice, for reasons unknown.  We
280	 * may fix this in the future by loading the CIS with a sane
281	 * CIS from userland.
282	 */
283	static uint8_t defaultmac[ETHER_ADDR_LEN] = {
284		0x00, 0x80, 0xc7, 0xed, 0x16, 0x7b};
285
286	/* Copy the MAC ADDR and return success */
287	for (i = 0; i < ETHER_ADDR_LEN; i++)
288		enaddr[i] = defaultmac[i];
289#else
290	/* FUNCE is not after FUNCID, so we gotta go find it */
291	if (tuple->code != 0x22)
292		return (0);
293
294	/* Make sure this is a sane node */
295	if (tuple->length < ETHER_ADDR_LEN + 3)
296		return (0);
297
298	/* Copy the MAC ADDR and return success */
299	for (i = 0; i < ETHER_ADDR_LEN; i++)
300		enaddr[i] = pccard_tuple_read_1(tuple, i + 3);
301#endif
302	return (1);
303}
304
305/*
306 * Probe and initialization for TDK/CONTEC PCMCIA Ethernet interface.
307 * by MASUI Kenji <masui@cs.titech.ac.jp>
308 *
309 * (Contec uses TDK Ethernet chip -- hosokawa)
310 *
311 * This version of fe_probe_tdk has been rewrote to handle
312 * *generic* PC Card implementation of Fujitsu MB8696x family.  The
313 * name _tdk is just for a historical reason. :-)
314 */
315static int
316fe_probe_tdk (device_t dev, const struct fe_pccard_product *pp)
317{
318	struct fe_softc *sc = device_get_softc(dev);
319
320	static struct fe_simple_probe_struct probe_table [] = {
321		{ FE_DLCR2, 0x10, 0x00 },
322		{ FE_DLCR4, 0x08, 0x00 },
323/*		{ FE_DLCR5, 0x80, 0x00 },	Does not work well.  */
324		{ 0 }
325	};
326
327
328	/* C-NET(PC)C occupies 16 I/O addresses. */
329	if (fe_alloc_port(dev, 16))
330		return ENXIO;
331
332	/* Fill the softc struct with default values.  */
333	fe_softc_defaults(sc);
334
335	/*
336	 * See if C-NET(PC)C is on its address.
337	 */
338	if (!fe_simple_probe(sc, probe_table))
339		return ENXIO;
340
341	/* Determine the card type.  */
342	sc->type = FE_TYPE_TDK;
343	sc->typestr = "Generic MB8696x/78Q837x Ethernet (PCMCIA)";
344
345	pccard_get_ether(dev, sc->enaddr);
346
347	/* Make sure we got a valid station address.  */
348	if (!fe_valid_Ether_p(sc->enaddr, 0)) {
349		pccard_cis_scan(dev, fe_pccard_xircom_mac, sc->enaddr);
350	}
351
352	/* Make sure we got a valid station address.  */
353	if (!fe_valid_Ether_p(sc->enaddr, 0))
354		return ENXIO;
355
356	return 0;
357}
358