if_fe_pccard.c revision 150396
1139749Simp/*-
265832Snyan * All Rights Reserved, Copyright (C) Fujitsu Limited 1995
365832Snyan *
465832Snyan * This software may be used, modified, copied, distributed, and sold, in
565832Snyan * both source and binary form provided that the above copyright, these
665832Snyan * terms and the following disclaimer are retained.  The name of the author
765832Snyan * and/or the contributor may not be used to endorse or promote products
865832Snyan * derived from this software without specific prior written permission.
965832Snyan *
1065832Snyan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND THE CONTRIBUTOR ``AS IS'' AND
1165832Snyan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1265832Snyan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1365832Snyan * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR THE CONTRIBUTOR BE LIABLE
1465832Snyan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1565832Snyan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1665832Snyan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION.
1765832Snyan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1865832Snyan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1965832Snyan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2065832Snyan * SUCH DAMAGE.
2165832Snyan *
2265832Snyan */
2365832Snyan
24119418Sobrien#include <sys/cdefs.h>
25119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/fe/if_fe_pccard.c 150396 2005-09-20 19:54:11Z imp $");
26119418Sobrien
2765832Snyan#include <sys/param.h>
2865832Snyan#include <sys/kernel.h>
2965832Snyan#include <sys/socket.h>
3067158Sphk#include <sys/systm.h>
3165832Snyan#include <sys/module.h>
3265832Snyan
3365832Snyan#include <sys/bus.h>
3465832Snyan#include <machine/bus.h>
3565832Snyan#include <machine/resource.h>
36140543Simp#include <sys/rman.h>
3765832Snyan
3865832Snyan#include <net/ethernet.h>
3965832Snyan#include <net/if.h>
4065832Snyan#include <net/if_mib.h>
4165832Snyan#include <net/if_media.h>
4265832Snyan
4365832Snyan#include <netinet/in.h>
4465832Snyan#include <netinet/if_ether.h>
4565832Snyan
46142135Simp#include <dev/fe/mb86960.h>
4765832Snyan#include <dev/fe/if_fereg.h>
4865832Snyan#include <dev/fe/if_fevar.h>
4965832Snyan
5065832Snyan#include <dev/pccard/pccardvar.h>
51140528Simp#include <dev/pccard/pccard_cis.h>
52129764Simp
53129764Simp#include "card_if.h"
54129740Simp#include "pccarddevs.h"
5565832Snyan
5665832Snyan/*
5765832Snyan *	PC-Card (PCMCIA) specific code.
5865832Snyan */
5965832Snyanstatic int fe_pccard_probe(device_t);
6065832Snyanstatic int fe_pccard_attach(device_t);
6165832Snyanstatic int fe_pccard_detach(device_t);
6265832Snyan
6382778Sshibastatic const struct fe_pccard_product {
6482778Sshiba        struct pccard_product mpp_product;
65140041Simp	int mpp_flags;
66140543Simp#define MPP_MBH10302 1
6782778Sshiba} fe_pccard_products[] = {
68140041Simp	/* These need to be first */
69147580Simp	{ PCMCIA_CARD(FUJITSU2, FMV_J181), MPP_MBH10302 },
70147580Simp	{ PCMCIA_CARD(FUJITSU2, FMV_J182), 0 },
71147580Simp	{ PCMCIA_CARD(FUJITSU2, FMV_J182A), 0 },
72147580Simp	{ PCMCIA_CARD(FUJITSU2, ITCFJ182A), 0 },
73140041Simp	/* These need to be second */
74147580Simp        { PCMCIA_CARD(TDK, LAK_CD021BX), 0 },
75147580Simp        { PCMCIA_CARD(TDK, LAK_CF010), 0 },
7682778Sshiba#if 0 /* XXX 86960-based? */
77147580Simp        { PCMCIA_CARD(TDK, LAK_DFL9610), 0 },
7882778Sshiba#endif
79147580Simp        { PCMCIA_CARD(CONTEC, CNETPC), 0 },
80147580Simp	{ PCMCIA_CARD(FUJITSU, LA501), 0 },
81147580Simp	{ PCMCIA_CARD(FUJITSU, LA10S), 0 },
82147580Simp	{ PCMCIA_CARD(FUJITSU, NE200T), MPP_MBH10302 },/* Sold by Eagle */
83147580Simp	{ PCMCIA_CARD(RATOC, REX_R280), 0 },
8482778Sshiba        { { NULL } }
8582778Sshiba};
8682778Sshiba
8782778Sshibastatic int
88150396Simpfe_pccard_probe(device_t dev)
8982778Sshiba{
9082778Sshiba        const struct pccard_product *pp;
91140528Simp	int		error;
92140528Simp	uint32_t	fcn = PCCARD_FUNCTION_UNSPEC;
9382778Sshiba
94140528Simp	/* Make sure we're a network function */
95140528Simp	error = pccard_get_function(dev, &fcn);
96140528Simp	if (error != 0)
97140528Simp		return (error);
98140528Simp	if (fcn != PCCARD_FUNCTION_NETWORK)
99140528Simp		return (ENXIO);
100140528Simp
10182778Sshiba        if ((pp = pccard_product_lookup(dev,
10282778Sshiba	    (const struct pccard_product *)fe_pccard_products,
10382778Sshiba            sizeof(fe_pccard_products[0]), NULL)) != NULL) {
104113315Simp		if (pp->pp_name != NULL)
105113315Simp			device_set_desc(dev, pp->pp_name);
10682778Sshiba                return 0;
10782778Sshiba        }
10882778Sshiba        return EIO;
10982778Sshiba}
11082778Sshiba
11165832Snyanstatic device_method_t fe_pccard_methods[] = {
11282778Sshiba        /* Device interface */
113150396Simp        DEVMETHOD(device_probe,         fe_pccard_probe),
114150396Simp        DEVMETHOD(device_attach,        fe_pccard_attach),
11582778Sshiba        DEVMETHOD(device_detach,        fe_pccard_detach),
11665832Snyan
11765832Snyan	{ 0, 0 }
11865832Snyan};
11965832Snyan
12065832Snyanstatic driver_t fe_pccard_driver = {
12165832Snyan	"fe",
12265832Snyan	fe_pccard_methods,
12365832Snyan	sizeof (struct fe_softc)
12465832Snyan};
12565832Snyan
12665832SnyanDRIVER_MODULE(fe, pccard, fe_pccard_driver, fe_devclass, 0, 0);
12765832Snyan
128140543Simpstatic int fe_probe_mbh(device_t, const struct fe_pccard_product *);
129140543Simpstatic int fe_probe_tdk(device_t, const struct fe_pccard_product *);
13065832Snyan/*
13165832Snyan *      Initialize the device - called from Slot manager.
13265832Snyan */
13365832Snyanstatic int
134150396Simpfe_pccard_attach(device_t dev)
13565832Snyan{
13665832Snyan	struct fe_softc *sc;
137140543Simp        const struct fe_pccard_product *pp;
13865832Snyan	int error;
13965832Snyan
14065832Snyan	/* Prepare for the device probe process.  */
14165832Snyan	sc = device_get_softc(dev);
14265832Snyan	sc->sc_unit = device_get_unit(dev);
14365832Snyan
144140543Simp        pp = (const struct fe_pccard_product *) pccard_product_lookup(dev,
145140543Simp	    (const struct pccard_product *)fe_pccard_products,
146140543Simp            sizeof(fe_pccard_products[0]), NULL);
147140543Simp	if (pp == NULL)
148140543Simp		return (ENXIO);
14965832Snyan
150140543Simp	if (pp->mpp_flags & MPP_MBH10302)
151140543Simp		error = fe_probe_mbh(dev, pp);
152140543Simp	else
153140543Simp		error = fe_probe_tdk(dev, pp);
154150396Simp	if (error != 0) {
155150396Simp		fe_release_resource(dev);
156150396Simp		return (error);
157150396Simp	}
158150396Simp	error = fe_alloc_irq(dev, 0);
159150396Simp	if (error != 0) {
160150396Simp		fe_release_resource(dev);
161150396Simp		return (error);
162150396Simp	}
163150396Simp	return (fe_attach(dev));
16465832Snyan}
16565832Snyan
16665832Snyan/*
16765832Snyan *	feunload - unload the driver and clear the table.
16865832Snyan */
16965832Snyanstatic int
17065832Snyanfe_pccard_detach(device_t dev)
17165832Snyan{
17265832Snyan	struct fe_softc *sc = device_get_softc(dev);
173147256Sbrooks	struct ifnet *ifp = sc->ifp;
17465832Snyan
17565832Snyan	fe_stop(sc);
176106937Ssam	ether_ifdetach(ifp);
177150306Simp	bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
178147256Sbrooks	if_free(ifp);
17965832Snyan	fe_release_resource(dev);
18065832Snyan
18165832Snyan	return 0;
18265832Snyan}
18365832Snyan
18465832Snyan
18565832Snyan/*
18665832Snyan * Probe and initialization for Fujitsu MBH10302 PCMCIA Ethernet interface.
18765832Snyan * Note that this is for 10302 only; MBH10304 is handled by fe_probe_tdk().
18865832Snyan */
18965832Snyanstatic void
19065832Snyanfe_init_mbh(struct fe_softc *sc)
19165832Snyan{
19265832Snyan	/* Minimal initialization of 86960.  */
19365832Snyan	DELAY(200);
19465832Snyan	fe_outb(sc, FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
19565832Snyan	DELAY(200);
19665832Snyan
19765832Snyan	/* Disable all interrupts.  */
19865832Snyan	fe_outb(sc, FE_DLCR2, 0);
19965832Snyan	fe_outb(sc, FE_DLCR3, 0);
20065832Snyan
20165832Snyan	/* Enable master interrupt flag.  */
20265832Snyan	fe_outb(sc, FE_MBH0, FE_MBH0_MAGIC | FE_MBH0_INTR_ENABLE);
20365832Snyan}
20465832Snyan
20565832Snyanstatic int
206140543Simpfe_probe_mbh(device_t dev, const struct fe_pccard_product *pp)
20765832Snyan{
20865832Snyan	struct fe_softc *sc = device_get_softc(dev);
20965832Snyan
21065832Snyan	static struct fe_simple_probe_struct probe_table [] = {
21165832Snyan		{ FE_DLCR2, 0x58, 0x00 },
21265832Snyan		{ FE_DLCR4, 0x08, 0x00 },
21365832Snyan		{ FE_DLCR6, 0xFF, 0xB6 },
21465832Snyan		{ 0 }
21565832Snyan	};
21665832Snyan
21765832Snyan	/* MBH10302 occupies 32 I/O addresses. */
21865832Snyan	if (fe_alloc_port(dev, 32))
21965832Snyan		return ENXIO;
22065832Snyan
22165832Snyan	/* Fill the softc struct with default values.  */
22265832Snyan	fe_softc_defaults(sc);
22365832Snyan
22465832Snyan	/*
22565832Snyan	 * See if MBH10302 is on its address.
22665832Snyan	 * I'm not sure the following probe code works.  FIXME.
22765832Snyan	 */
22865832Snyan	if (!fe_simple_probe(sc, probe_table))
22965832Snyan		return ENXIO;
23065832Snyan
23165832Snyan	/* Get our station address from EEPROM.  */
232147256Sbrooks	fe_inblk(sc, FE_MBH10, sc->enaddr, ETHER_ADDR_LEN);
23365832Snyan
23465832Snyan	/* Make sure we got a valid station address.  */
235147256Sbrooks	if (!fe_valid_Ether_p(sc->enaddr, 0))
23665832Snyan		return ENXIO;
23765832Snyan
23865832Snyan	/* Determine the card type.  */
23965832Snyan	sc->type = FE_TYPE_MBH;
24065832Snyan	sc->typestr = "MBH10302 (PCMCIA)";
24165832Snyan
24265832Snyan	/* We seems to need our own IDENT bits...  FIXME.  */
24365832Snyan	sc->proto_dlcr7 = FE_D7_BYTSWP_LH | FE_D7_IDENT_NICE;
24465832Snyan
24565832Snyan	/* Setup hooks.  We need a special initialization procedure.  */
24665832Snyan	sc->init = fe_init_mbh;
24765832Snyan
24865832Snyan	return 0;
24965832Snyan}
25065832Snyan
25165832Snyan/*
25265832Snyan * Probe and initialization for TDK/CONTEC PCMCIA Ethernet interface.
25365832Snyan * by MASUI Kenji <masui@cs.titech.ac.jp>
25465832Snyan *
25565832Snyan * (Contec uses TDK Ethenet chip -- hosokawa)
25665832Snyan *
25765832Snyan * This version of fe_probe_tdk has been rewrote to handle
25865832Snyan * *generic* PC card implementation of Fujitsu MB8696x family.  The
25965832Snyan * name _tdk is just for a historical reason. :-)
26065832Snyan */
26165832Snyanstatic int
262140543Simpfe_probe_tdk (device_t dev, const struct fe_pccard_product *pp)
26365832Snyan{
26465832Snyan	struct fe_softc *sc = device_get_softc(dev);
26565832Snyan
26665832Snyan        static struct fe_simple_probe_struct probe_table [] = {
267142137Simp                { FE_DLCR2, 0x10, 0x00 },
26865832Snyan                { FE_DLCR4, 0x08, 0x00 },
26965832Snyan            /*  { FE_DLCR5, 0x80, 0x00 },       Does not work well.  */
27065832Snyan                { 0 }
27165832Snyan        };
27265832Snyan
27365832Snyan        /* C-NET(PC)C occupies 16 I/O addresses. */
27465832Snyan	if (fe_alloc_port(dev, 16))
27565832Snyan		return ENXIO;
27665832Snyan
27765832Snyan	/* Fill the softc struct with default values.  */
27865832Snyan	fe_softc_defaults(sc);
27965832Snyan
28065832Snyan        /*
28165832Snyan         * See if C-NET(PC)C is on its address.
28265832Snyan         */
283142136Simp        if (!fe_simple_probe(sc, probe_table))
284142136Simp		return ENXIO;
28565832Snyan
28665832Snyan        /* Determine the card type.  */
28765832Snyan	sc->type = FE_TYPE_TDK;
28865832Snyan        sc->typestr = "Generic MB8696x/78Q837x Ethernet (PCMCIA)";
28965832Snyan
290147256Sbrooks	pccard_get_ether(dev, sc->enaddr);
291140543Simp
29265832Snyan        /* Make sure we got a valid station address.  */
293147256Sbrooks        if (!fe_valid_Ether_p(sc->enaddr, 0))
294142136Simp		return ENXIO;
29565832Snyan
29665832Snyan        return 0;
29765832Snyan}
298