if_fe_pccard.c revision 82778
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 * $FreeBSD: head/sys/dev/fe/if_fe_pccard.c 82778 2001-09-02 06:27:07Z shiba $
23 */
24
25#include <sys/param.h>
26#include <sys/kernel.h>
27#include <sys/socket.h>
28#include <sys/systm.h>
29#include <sys/module.h>
30
31#include <sys/bus.h>
32#include <machine/bus.h>
33#include <machine/resource.h>
34
35#include <net/ethernet.h>
36#include <net/if.h>
37#include <net/if_mib.h>
38#include <net/if_media.h>
39
40#include <netinet/in.h>
41#include <netinet/if_ether.h>
42
43#include <i386/isa/ic/mb86960.h>
44#include <dev/fe/if_fereg.h>
45#include <dev/fe/if_fevar.h>
46
47#include <dev/pccard/pccardvar.h>
48#include <dev/pccard/pccarddevs.h>
49#include "card_if.h"
50#include <pccard/cardinfo.h>
51
52/*
53 *	PC-Card (PCMCIA) specific code.
54 */
55static int fe_pccard_probe(device_t);
56static int fe_pccard_attach(device_t);
57static int fe_pccard_detach(device_t);
58static int fe_pccard_match(device_t);
59
60static const struct fe_pccard_product {
61        struct pccard_product mpp_product;
62        u_int32_t mpp_ioalign;                  /* required alignment */
63        int mpp_enet_maddr;
64} fe_pccard_products[] = {
65        { { PCCARD_STR_TDK_LAK_CD021BX,         PCCARD_VENDOR_TDK,
66            PCCARD_PRODUCT_TDK_LAK_CD021BX,     0 },
67          0, -1 },
68
69        { { PCCARD_STR_TDK_LAK_CF010,           PCCARD_VENDOR_TDK,
70            PCCARD_PRODUCT_TDK_LAK_CF010,       0 },
71          0, -1 },
72
73#if 0 /* XXX 86960-based? */
74        { { PCCARD_STR_TDK_LAK_DFL9610,         PCCARD_VENDOR_TDK,
75            PCCARD_PRODUCT_TDK_LAK_DFL9610,     1 },
76          0, -1 },
77#endif
78
79        { { PCCARD_STR_CONTEC_CNETPC,           PCCARD_VENDOR_CONTEC,
80            PCCARD_PRODUCT_CONTEC_CNETPC,       0 },
81          0, -1 },
82
83        { { PCCARD_STR_FUJITSU_LA501,           PCCARD_VENDOR_FUJITSU,
84            PCCARD_PRODUCT_FUJITSU_LA501,       0 },
85          0x20, -1 },
86
87        { { PCCARD_STR_FUJITSU_LA10S,           PCCARD_VENDOR_FUJITSU,
88            PCCARD_PRODUCT_FUJITSU_LA10S,       0 },
89          0, -1 },
90
91        { { PCCARD_STR_RATOC_REX_R280,          PCCARD_VENDOR_RATOC,
92            PCCARD_PRODUCT_RATOC_REX_R280,      0 },
93          0, 0x1fc },
94
95        { { NULL } }
96};
97
98static int
99fe_pccard_match(device_t dev)
100{
101        const struct pccard_product *pp;
102
103        if ((pp = pccard_product_lookup(dev,
104	    (const struct pccard_product *)fe_pccard_products,
105            sizeof(fe_pccard_products[0]), NULL)) != NULL) {
106                device_set_desc(dev, pp->pp_name);
107                return 0;
108        }
109        return EIO;
110}
111
112static device_method_t fe_pccard_methods[] = {
113        /* Device interface */
114        DEVMETHOD(device_probe,         pccard_compat_probe),
115        DEVMETHOD(device_attach,        pccard_compat_attach),
116        DEVMETHOD(device_detach,        fe_pccard_detach),
117
118        /* Card interface */
119        DEVMETHOD(card_compat_match,    fe_pccard_match),
120        DEVMETHOD(card_compat_probe,    fe_pccard_probe),
121        DEVMETHOD(card_compat_attach,   fe_pccard_attach),
122
123	{ 0, 0 }
124};
125
126static driver_t fe_pccard_driver = {
127	"fe",
128	fe_pccard_methods,
129	sizeof (struct fe_softc)
130};
131
132DRIVER_MODULE(fe, pccard, fe_pccard_driver, fe_devclass, 0, 0);
133
134
135static int fe_probe_mbh(device_t);
136static int fe_probe_tdk(device_t);
137
138/*
139 *      Initialize the device - called from Slot manager.
140 */
141static int
142fe_pccard_probe(device_t dev)
143{
144	struct fe_softc *sc;
145	int error;
146
147	/* Prepare for the device probe process.  */
148	sc = device_get_softc(dev);
149	sc->sc_unit = device_get_unit(dev);
150
151	pccard_get_ether(dev, sc->sc_enaddr);
152
153	/* Probe for supported cards.  */
154	if ((error = fe_probe_mbh(dev)) == 0)
155		goto end;
156	fe_release_resource(dev);
157
158	if ((error = fe_probe_tdk(dev)) == 0)
159		goto end;
160	fe_release_resource(dev);
161
162end:
163	if (error == 0)
164		error = fe_alloc_irq(dev, 0);
165
166	fe_release_resource(dev);
167	return (error);
168}
169
170static int
171fe_pccard_attach(device_t dev)
172{
173	struct fe_softc *sc = device_get_softc(dev);
174
175	if (sc->port_used)
176		fe_alloc_port(dev, sc->port_used);
177	fe_alloc_irq(dev, 0);
178
179	return fe_attach(dev);
180}
181
182/*
183 *	feunload - unload the driver and clear the table.
184 *	XXX TODO:
185 *	This is usually called when the card is ejected, but
186 *	can be caused by a modunload of a controller driver.
187 *	The idea is to reset the driver's view of the device
188 *	and ensure that any driver entry points such as
189 *	read and write do not hang.
190 */
191static int
192fe_pccard_detach(device_t dev)
193{
194	struct fe_softc *sc = device_get_softc(dev);
195	struct ifnet *ifp = &sc->arpcom.ac_if;
196
197	fe_stop(sc);
198	ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
199	bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
200	fe_release_resource(dev);
201
202	return 0;
203}
204
205
206/*
207 * Probe and initialization for Fujitsu MBH10302 PCMCIA Ethernet interface.
208 * Note that this is for 10302 only; MBH10304 is handled by fe_probe_tdk().
209 */
210static void
211fe_init_mbh(struct fe_softc *sc)
212{
213	/* Minimal initialization of 86960.  */
214	DELAY(200);
215	fe_outb(sc, FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
216	DELAY(200);
217
218	/* Disable all interrupts.  */
219	fe_outb(sc, FE_DLCR2, 0);
220	fe_outb(sc, FE_DLCR3, 0);
221
222	/* Enable master interrupt flag.  */
223	fe_outb(sc, FE_MBH0, FE_MBH0_MAGIC | FE_MBH0_INTR_ENABLE);
224}
225
226static int
227fe_probe_mbh(device_t dev)
228{
229	struct fe_softc *sc = device_get_softc(dev);
230
231	static struct fe_simple_probe_struct probe_table [] = {
232		{ FE_DLCR2, 0x58, 0x00 },
233		{ FE_DLCR4, 0x08, 0x00 },
234		{ FE_DLCR6, 0xFF, 0xB6 },
235		{ 0 }
236	};
237
238	/* MBH10302 occupies 32 I/O addresses. */
239	if (fe_alloc_port(dev, 32))
240		return ENXIO;
241
242	/* Ethernet MAC address should *NOT* have been given by pccardd,
243	   if this is a true MBH10302; i.e., Ethernet address must be
244	   "all-zero" upon entry.  */
245	if (sc->sc_enaddr[0] || sc->sc_enaddr[1] || sc->sc_enaddr[2] ||
246	    sc->sc_enaddr[3] || sc->sc_enaddr[4] || sc->sc_enaddr[5])
247		return ENXIO;
248
249	/* Fill the softc struct with default values.  */
250	fe_softc_defaults(sc);
251
252	/*
253	 * See if MBH10302 is on its address.
254	 * I'm not sure the following probe code works.  FIXME.
255	 */
256	if (!fe_simple_probe(sc, probe_table))
257		return ENXIO;
258
259	/* Get our station address from EEPROM.  */
260	fe_inblk(sc, FE_MBH10, sc->sc_enaddr, ETHER_ADDR_LEN);
261
262	/* Make sure we got a valid station address.  */
263	if (!valid_Ether_p(sc->sc_enaddr, 0))
264		return ENXIO;
265
266	/* Determine the card type.  */
267	sc->type = FE_TYPE_MBH;
268	sc->typestr = "MBH10302 (PCMCIA)";
269
270	/* We seems to need our own IDENT bits...  FIXME.  */
271	sc->proto_dlcr7 = FE_D7_BYTSWP_LH | FE_D7_IDENT_NICE;
272
273	/* Setup hooks.  We need a special initialization procedure.  */
274	sc->init = fe_init_mbh;
275
276	return 0;
277}
278
279/*
280 * Probe and initialization for TDK/CONTEC PCMCIA Ethernet interface.
281 * by MASUI Kenji <masui@cs.titech.ac.jp>
282 *
283 * (Contec uses TDK Ethenet chip -- hosokawa)
284 *
285 * This version of fe_probe_tdk has been rewrote to handle
286 * *generic* PC card implementation of Fujitsu MB8696x family.  The
287 * name _tdk is just for a historical reason. :-)
288 */
289static int
290fe_probe_tdk (device_t dev)
291{
292	struct fe_softc *sc = device_get_softc(dev);
293
294        static struct fe_simple_probe_struct probe_table [] = {
295                { FE_DLCR2, 0x50, 0x00 },
296                { FE_DLCR4, 0x08, 0x00 },
297            /*  { FE_DLCR5, 0x80, 0x00 },       Does not work well.  */
298                { 0 }
299        };
300
301        /* C-NET(PC)C occupies 16 I/O addresses. */
302	if (fe_alloc_port(dev, 16))
303		return ENXIO;
304
305	/* Fill the softc struct with default values.  */
306	fe_softc_defaults(sc);
307
308        /*
309         * See if C-NET(PC)C is on its address.
310         */
311        if (!fe_simple_probe(sc, probe_table))
312		return ENXIO;
313
314        /* Determine the card type.  */
315	sc->type = FE_TYPE_TDK;
316        sc->typestr = "Generic MB8696x/78Q837x Ethernet (PCMCIA)";
317
318        /* Make sure we got a valid station address.  */
319        if (!valid_Ether_p(sc->sc_enaddr, 0))
320		return ENXIO;
321
322        return 0;
323}
324