if_fe_pccard.c revision 179493
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: head/sys/dev/fe/if_fe_pccard.c 179493 2008-06-02 19:58:48Z jhb $");
26
27#include <sys/param.h>
28#include <sys/kernel.h>
29#include <sys/socket.h>
30#include <sys/systm.h>
31#include <sys/module.h>
32
33#include <sys/bus.h>
34#include <machine/bus.h>
35#include <machine/resource.h>
36#include <sys/rman.h>
37
38#include <net/ethernet.h>
39#include <net/if.h>
40#include <net/if_mib.h>
41#include <net/if_media.h>
42
43#include <netinet/in.h>
44#include <netinet/if_ether.h>
45
46#include <dev/fe/mb86960.h>
47#include <dev/fe/if_fereg.h>
48#include <dev/fe/if_fevar.h>
49
50#include <dev/pccard/pccardvar.h>
51#include <dev/pccard/pccard_cis.h>
52
53#include "card_if.h"
54#include "pccarddevs.h"
55
56/*
57 *	PC Card (PCMCIA) specific code.
58 */
59static int fe_pccard_probe(device_t);
60static int fe_pccard_attach(device_t);
61static int fe_pccard_detach(device_t);
62
63static const struct fe_pccard_product {
64        struct pccard_product mpp_product;
65	int mpp_flags;
66#define MPP_MBH10302 1
67#define MPP_ANYFUNC 2
68} fe_pccard_products[] = {
69	/* These need to be first */
70	{ PCMCIA_CARD(FUJITSU2, FMV_J181), MPP_MBH10302 },
71	{ PCMCIA_CARD(FUJITSU2, FMV_J182), 0 },
72	{ PCMCIA_CARD(FUJITSU2, FMV_J182A), 0 },
73	{ PCMCIA_CARD(FUJITSU2, ITCFJ182A), 0 },
74	/* These need to be second */
75        { PCMCIA_CARD(TDK, LAK_CD021BX), 0 },
76        { PCMCIA_CARD(TDK, LAK_CF010), 0 },
77#if 0 /* XXX 86960-based? */
78        { PCMCIA_CARD(TDK, LAK_DFL9610), 0 },
79#endif
80        { PCMCIA_CARD(CONTEC, CNETPC), 0 },
81	{ PCMCIA_CARD(FUJITSU, LA501), 0 },
82	{ PCMCIA_CARD(FUJITSU, LA10S), 0 },
83	{ PCMCIA_CARD(FUJITSU, NE200T), MPP_MBH10302 },/* Sold by Eagle */
84	{ PCMCIA_CARD(RATOC, REX_R280), 0 },
85	{ PCMCIA_CARD(XIRCOM, CE), MPP_ANYFUNC },
86        { { NULL } }
87};
88
89static int
90fe_pccard_probe(device_t dev)
91{
92	int		error;
93	uint32_t	fcn = PCCARD_FUNCTION_UNSPEC;
94        const struct fe_pccard_product *pp;
95
96        if ((pp = (const struct fe_pccard_product *)pccard_product_lookup(dev,
97	    (const struct pccard_product *)fe_pccard_products,
98            sizeof(fe_pccard_products[0]), NULL)) != NULL) {
99		if (pp->mpp_product.pp_name != NULL)
100			device_set_desc(dev, pp->mpp_product.pp_name);
101		if (pp->mpp_flags & MPP_ANYFUNC)
102			return (0);
103		/* Make sure we're a network function */
104		error = pccard_get_function(dev, &fcn);
105		if (error != 0)
106			return (error);
107		if (fcn != PCCARD_FUNCTION_NETWORK)
108			return (ENXIO);
109		return (0);
110        }
111        return (ENXIO);
112}
113
114static device_method_t fe_pccard_methods[] = {
115        /* Device interface */
116        DEVMETHOD(device_probe,         fe_pccard_probe),
117        DEVMETHOD(device_attach,        fe_pccard_attach),
118        DEVMETHOD(device_detach,        fe_pccard_detach),
119
120	{ 0, 0 }
121};
122
123static driver_t fe_pccard_driver = {
124	"fe",
125	fe_pccard_methods,
126	sizeof (struct fe_softc)
127};
128
129DRIVER_MODULE(fe, pccard, fe_pccard_driver, fe_devclass, 0, 0);
130
131static int fe_probe_mbh(device_t, const struct fe_pccard_product *);
132static int fe_probe_tdk(device_t, const struct fe_pccard_product *);
133/*
134 *      Initialize the device - called from Slot manager.
135 */
136static int
137fe_pccard_attach(device_t dev)
138{
139	struct fe_softc *sc;
140        const struct fe_pccard_product *pp;
141	int error;
142
143	/* Prepare for the device probe process.  */
144	sc = device_get_softc(dev);
145	sc->sc_unit = device_get_unit(dev);
146
147        pp = (const struct fe_pccard_product *) pccard_product_lookup(dev,
148	    (const struct pccard_product *)fe_pccard_products,
149            sizeof(fe_pccard_products[0]), NULL);
150	if (pp == NULL)
151		return (ENXIO);
152
153	if (pp->mpp_flags & MPP_MBH10302)
154		error = fe_probe_mbh(dev, pp);
155	else
156		error = fe_probe_tdk(dev, pp);
157	if (error != 0) {
158		fe_release_resource(dev);
159		return (error);
160	}
161	error = fe_alloc_irq(dev, 0);
162	if (error != 0) {
163		fe_release_resource(dev);
164		return (error);
165	}
166	return (fe_attach(dev));
167}
168
169/*
170 *	feunload - unload the driver and clear the table.
171 */
172static int
173fe_pccard_detach(device_t dev)
174{
175	struct fe_softc *sc = device_get_softc(dev);
176	struct ifnet *ifp = sc->ifp;
177
178	FE_LOCK(sc);
179	fe_stop(sc);
180	FE_UNLOCK(sc);
181	callout_drain(&sc->timer);
182	ether_ifdetach(ifp);
183	bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
184	if_free(ifp);
185	fe_release_resource(dev);
186	mtx_destroy(&sc->lock);
187
188	return 0;
189}
190
191
192/*
193 * Probe and initialization for Fujitsu MBH10302 PCMCIA Ethernet interface.
194 * Note that this is for 10302 only; MBH10304 is handled by fe_probe_tdk().
195 */
196static void
197fe_init_mbh(struct fe_softc *sc)
198{
199	/* Minimal initialization of 86960.  */
200	DELAY(200);
201	fe_outb(sc, FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
202	DELAY(200);
203
204	/* Disable all interrupts.  */
205	fe_outb(sc, FE_DLCR2, 0);
206	fe_outb(sc, FE_DLCR3, 0);
207
208	/* Enable master interrupt flag.  */
209	fe_outb(sc, FE_MBH0, FE_MBH0_MAGIC | FE_MBH0_INTR_ENABLE);
210}
211
212static int
213fe_probe_mbh(device_t dev, const struct fe_pccard_product *pp)
214{
215	struct fe_softc *sc = device_get_softc(dev);
216
217	static struct fe_simple_probe_struct probe_table [] = {
218		{ FE_DLCR2, 0x58, 0x00 },
219		{ FE_DLCR4, 0x08, 0x00 },
220		{ FE_DLCR6, 0xFF, 0xB6 },
221		{ 0 }
222	};
223
224	/* MBH10302 occupies 32 I/O addresses. */
225	if (fe_alloc_port(dev, 32))
226		return ENXIO;
227
228	/* Fill the softc struct with default values.  */
229	fe_softc_defaults(sc);
230
231	/*
232	 * See if MBH10302 is on its address.
233	 * I'm not sure the following probe code works.  FIXME.
234	 */
235	if (!fe_simple_probe(sc, probe_table))
236		return ENXIO;
237
238	/* Get our station address from EEPROM.  */
239	fe_inblk(sc, FE_MBH10, sc->enaddr, ETHER_ADDR_LEN);
240
241	/* Make sure we got a valid station address.  */
242	if (!fe_valid_Ether_p(sc->enaddr, 0))
243		return ENXIO;
244
245	/* Determine the card type.  */
246	sc->type = FE_TYPE_MBH;
247	sc->typestr = "MBH10302 (PCMCIA)";
248
249	/* We seems to need our own IDENT bits...  FIXME.  */
250	sc->proto_dlcr7 = FE_D7_BYTSWP_LH | FE_D7_IDENT_NICE;
251
252	/* Setup hooks.  We need a special initialization procedure.  */
253	sc->init = fe_init_mbh;
254
255	return 0;
256}
257
258static int
259sn_pccard_xircom_mac(const struct pccard_tuple *tuple, void *argp)
260{
261	uint8_t *enaddr = argp;
262	int i;
263
264#if 1
265	/*
266	 * We fail to map the CIS twice, for reasons unknown.  We
267	 * may fix this in the future by loading the CIS with a sane
268	 * CIS from userland.
269	 */
270	static uint8_t defaultmac[ETHER_ADDR_LEN] = {
271		0x00, 0x80, 0xc7, 0xed, 0x16, 0x7b};
272
273	/* Copy the MAC ADDR and return success */
274	for (i = 0; i < ETHER_ADDR_LEN; i++)
275		enaddr[i] = defaultmac[i];
276#else
277	/* FUNCE is not after FUNCID, so we gotta go find it */
278	if (tuple->code != 0x22)
279		return (0);
280
281	/* Make sure this is a sane node */
282	if (tuple->length < ETHER_ADDR_LEN + 3)
283		return (0);
284
285	/* Copy the MAC ADDR and return success */
286	for (i = 0; i < ETHER_ADDR_LEN; i++)
287		enaddr[i] = pccard_tuple_read_1(tuple, i + 3);
288#endif
289	return (1);
290}
291
292/*
293 * Probe and initialization for TDK/CONTEC PCMCIA Ethernet interface.
294 * by MASUI Kenji <masui@cs.titech.ac.jp>
295 *
296 * (Contec uses TDK Ethenet chip -- hosokawa)
297 *
298 * This version of fe_probe_tdk has been rewrote to handle
299 * *generic* PC Card implementation of Fujitsu MB8696x family.  The
300 * name _tdk is just for a historical reason. :-)
301 */
302static int
303fe_probe_tdk (device_t dev, const struct fe_pccard_product *pp)
304{
305	struct fe_softc *sc = device_get_softc(dev);
306
307        static struct fe_simple_probe_struct probe_table [] = {
308                { FE_DLCR2, 0x10, 0x00 },
309                { FE_DLCR4, 0x08, 0x00 },
310            /*  { FE_DLCR5, 0x80, 0x00 },       Does not work well.  */
311                { 0 }
312        };
313
314
315        /* C-NET(PC)C occupies 16 I/O addresses. */
316	if (fe_alloc_port(dev, 16))
317		return ENXIO;
318
319	/* Fill the softc struct with default values.  */
320	fe_softc_defaults(sc);
321
322        /*
323         * See if C-NET(PC)C is on its address.
324         */
325        if (!fe_simple_probe(sc, probe_table))
326		return ENXIO;
327
328        /* Determine the card type.  */
329	sc->type = FE_TYPE_TDK;
330        sc->typestr = "Generic MB8696x/78Q837x Ethernet (PCMCIA)";
331
332	pccard_get_ether(dev, sc->enaddr);
333
334        /* Make sure we got a valid station address.  */
335        if (!fe_valid_Ether_p(sc->enaddr, 0)) {
336	        pccard_cis_scan(dev, sn_pccard_xircom_mac, sc->enaddr);
337	}
338
339        /* Make sure we got a valid station address.  */
340        if (!fe_valid_Ether_p(sc->enaddr, 0))
341		return ENXIO;
342
343        return 0;
344}
345