if_ep_pccard.c revision 52621
151673Smdodd/*
251673Smdodd * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca>
351673Smdodd * All rights reserved.
451673Smdodd *
551673Smdodd * Redistribution and use in source and binary forms, with or without
651673Smdodd * modification, are permitted provided that the following conditions
751673Smdodd * are met:
851673Smdodd * 1. Redistributions of source code must retain the above copyright
951673Smdodd *    notice, this list of conditions and the following disclaimer.
1051673Smdodd * 2. Redistributions in binary form must reproduce the above copyright
1151673Smdodd *    notice, this list of conditions and the following disclaimer in the
1251673Smdodd *    documentation and/or other materials provided with the distribution.
1351673Smdodd * 3. All advertising materials mentioning features or use of this software
1451673Smdodd *    must display the following acknowledgement:
1551673Smdodd *      This product includes software developed by Herb Peyerl.
1651673Smdodd * 4. The name of Herb Peyerl may not be used to endorse or promote products
1751673Smdodd *    derived from this software without specific prior written permission.
1851673Smdodd *
1951673Smdodd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2051673Smdodd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2151673Smdodd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2251673Smdodd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2351673Smdodd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2451673Smdodd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2551673Smdodd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2651673Smdodd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2751673Smdodd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2851673Smdodd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2951673Smdodd *
3051673Smdodd * $FreeBSD: head/sys/dev/ep/if_ep_pccard.c 52621 1999-10-29 06:27:07Z imp $
3151673Smdodd */
3251673Smdodd
3351673Smdodd/*
3451673Smdodd * Pccard support for 3C589 by:
3551673Smdodd *		HAMADA Naoki
3651673Smdodd *		nao@tom-yam.or.jp
3751673Smdodd */
3851673Smdodd
3951673Smdodd#include <sys/param.h>
4052549Smdodd#include <sys/systm.h>
4151673Smdodd#include <sys/kernel.h>
4251673Smdodd#include <sys/socket.h>
4352549Smdodd
4452549Smdodd#include <sys/module.h>
4552472Simp#include <sys/bus.h>
4652549Smdodd
4752472Simp#include <machine/bus.h>
4852549Smdodd#include <machine/resource.h>
4952472Simp#include <sys/rman.h>
5052549Smdodd
5152549Smdodd#include <net/if.h>
5252549Smdodd#include <net/if_arp.h>
5352549Smdodd#include <net/if_media.h>
5451673Smdodd
5551673Smdodd#include <machine/clock.h>
5651673Smdodd
5752472Simp#include <dev/ep/if_epreg.h>
5852472Simp#include <dev/ep/if_epvar.h>
5952472Simp
6052472Simp/* XXX should die XXX */
6151673Smdodd#include <sys/select.h>
6251673Smdodd#include <sys/module.h>
6351673Smdodd#include <pccard/cardinfo.h>
6451673Smdodd#include <pccard/slot.h>
6551673Smdodd
6652472Simpstatic const char *ep_pccard_identify(u_short id);
6751673Smdodd
6851673Smdodd/*
6951673Smdodd * Initialize the device - called from Slot manager.
7051673Smdodd */
7151673Smdoddstatic int
7252472Simpep_pccard_probe(device_t dev)
7351673Smdodd{
7452549Smdodd	struct ep_softc *	sc = device_get_softc(dev);
7552549Smdodd	struct ep_board *	epb = &sc->epb;
7652549Smdodd	const char *		desc;
7752549Smdodd	int			error;
7851673Smdodd
7952589Simp	error = ep_alloc(dev);
8052589Simp	if (error)
8152472Simp		return error;
8252549Smdodd
8352472Simp	/*
8452472Simp	 * XXX - Certain (newer?) 3Com cards need epb->cmd_off ==
8552472Simp	 * 2. Sadly, you need to have a correct cmd_off in order to
8652472Simp	 * identify the card.  So we have to hit it with both and
8752472Simp	 * cross our virtual fingers.  There's got to be a better way
8852472Simp	 * to do this.  jyoung@accessus.net 09/11/1999
8952472Simp	 */
9051673Smdodd
9152472Simp	epb->cmd_off = 0;
9251673Smdodd	epb->prod_id = get_e(sc, EEPROM_PROD_ID);
9352472Simp	if ((desc = ep_pccard_identify(epb->prod_id)) == NULL) {
9452472Simp		if (bootverbose)
9552472Simp			device_printf(dev, "Pass 1 of 2 detection "
9652472Simp			    "failed (nonfatal)\n");
9752472Simp		epb->cmd_off = 2;
9852472Simp		epb->prod_id = get_e(sc, EEPROM_PROD_ID);
9952589Simp		if ((desc = ep_pccard_identify(epb->prod_id)) == NULL) {
10052472Simp			device_printf(dev, "Unit failed to come ready or "
10152472Simp			    "product ID unknown! (id 0x%x)\n", epb->prod_id);
10252589Simp			ep_free(dev);
10352472Simp			return (ENXIO);
10452472Simp		}
10551673Smdodd	}
10652589Simp	device_set_desc(dev, desc);
10752589Simp	ep_free(dev);
10852472Simp	return (0);
10952472Simp}
11051673Smdodd
11152472Simpstatic const char *
11252472Simpep_pccard_identify(u_short id)
11352472Simp{
11452472Simp	/* Determine device type and associated MII capabilities  */
11552472Simp	switch (id) {
11652472Simp	case 0x6055: /* 3C556 */
11752472Simp		return ("3Com 3C556");
11852472Simp	case 0x4057: /* 3C574 */
11952589Simp		return ("3Com 3C574");
12052472Simp	case 0x4b57: /* 3C574B */
12152589Simp		return ("3Com 3C574B, Megahertz 3CCFE574BT or "
12252472Simp		    "Fast Etherlink 3C574-TX");
12352472Simp	case 0x9058: /* 3C589 */
12452589Simp		return ("3Com Etherlink III 3C589");
12552621Simp	case 0x2056: /* 3C562/3C563 */
12652621Simp		return ("3Com 3C562D/3C563D");
12752472Simp	}
12852589Simp	return (NULL);
12951673Smdodd}
13051673Smdodd
13151673Smdoddstatic int
13252472Simpep_pccard_card_attach(struct ep_board *epb)
13351673Smdodd{
13452472Simp	/* Determine device type and associated MII capabilities  */
13552472Simp	switch (epb->prod_id) {
13651673Smdodd	case 0x6055: /* 3C556 */
13752472Simp		epb->mii_trans = 1;
13852472Simp		return (1);
13951673Smdodd	case 0x4057: /* 3C574 */
14052472Simp		epb->mii_trans = 1;
14152472Simp		return (1);
14251673Smdodd	case 0x4b57: /* 3C574B */
14352472Simp		epb->mii_trans = 1;
14452472Simp		return (1);
14552621Simp	case 0x2056: /* 3C562D/3C563D */
14651673Smdodd	case 0x9058: /* 3C589 */
14752472Simp		epb->mii_trans = 0;
14852472Simp		return (1);
14952472Simp	}
15052472Simp	return (0);
15151673Smdodd}
15251673Smdodd
15351673Smdoddstatic int
15452472Simpep_pccard_attach(device_t dev)
15551673Smdodd{
15652549Smdodd	struct ep_softc *	sc = device_get_softc(dev);
15752549Smdodd	int			error = 0;
15851673Smdodd
15952585Simp	if ((error = ep_alloc(dev))) {
16052549Smdodd		device_printf(dev, "ep_alloc() failed! (%d)\n", error);
16152472Simp		goto bad;
16252472Simp	}
16351673Smdodd
16452589Simp	sc->epb.cmd_off = 0;
16552589Simp	sc->epb.prod_id = get_e(sc, EEPROM_PROD_ID);
16652549Smdodd	if (!ep_pccard_card_attach(&sc->epb)) {
16752549Smdodd		sc->epb.cmd_off = 2;
16852549Smdodd		sc->epb.prod_id = get_e(sc, EEPROM_PROD_ID);
16952549Smdodd		sc->epb.res_cfg = get_e(sc, EEPROM_RESOURCE_CFG);
17052549Smdodd		if (!ep_pccard_card_attach(&sc->epb)) {
17152472Simp			device_printf(dev,
17252472Simp			    "Probe found ID, attach failed so ignore card!\n");
17352549Smdodd			error = ENXIO;
17452549Smdodd			goto bad;
17552472Simp		}
17652472Simp	}
17751673Smdodd
17852472Simp	/* ROM size = 0, ROM base = 0 */
17952472Simp	/* For now, ignore AUTO SELECT feature of 3C589B and later. */
18052472Simp	outw(BASE + EP_W0_ADDRESS_CFG, get_e(sc, EEPROM_ADDR_CFG) & 0xc000);
18152472Simp
18252472Simp	/* Fake IRQ must be 3 */
18352549Smdodd	outw(BASE + EP_W0_RESOURCE_CFG, (sc->epb.res_cfg & 0x0fff) | 0x3000);
18452472Simp
18552549Smdodd	outw(BASE + EP_W0_PRODUCT_ID, sc->epb.prod_id);
18652472Simp
18752549Smdodd	if (sc->epb.mii_trans) {
18852472Simp		/*
18952472Simp		 * turn on the MII transciever
19052472Simp		 */
19152472Simp		GO_WINDOW(3);
19252472Simp		outw(BASE + EP_W3_OPTIONS, 0x8040);
19352472Simp		DELAY(1000);
19452472Simp		outw(BASE + EP_W3_OPTIONS, 0xc040);
19552472Simp		outw(BASE + EP_COMMAND, RX_RESET);
19652472Simp		outw(BASE + EP_COMMAND, TX_RESET);
19752472Simp		while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
19852472Simp		DELAY(1000);
19952472Simp		outw(BASE + EP_W3_OPTIONS, 0x8040);
20052549Smdodd	} else {
20152549Smdodd		ep_get_media(sc);
20252472Simp	}
20352472Simp
20452549Smdodd	if ((error = ep_attach(sc))) {
20552549Smdodd		device_printf(dev, "ep_attach() failed! (%d)\n", error);
20652472Simp		goto bad;
20752472Simp	}
20852472Simp
20952585Simp	if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET, ep_intr,
21052585Simp				    sc, &sc->ep_intrhand))) {
21152549Smdodd		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
21252472Simp		goto bad;
21352472Simp	}
21452472Simp
21552549Smdodd	return (0);
21652472Simpbad:
21752585Simp	ep_free(dev);
21852549Smdodd	return (error);
21951673Smdodd}
22051673Smdodd
22151673Smdoddstatic void
22252472Simpep_pccard_detach(device_t dev)
22351673Smdodd{
22452472Simp	struct ep_softc *sc = device_get_softc(dev);
22551673Smdodd
22652472Simp	printf("detach\n");
22752472Simp
22852472Simp	if (sc->gone) {
22952472Simp		device_printf(dev, "already unloaded\n");
23052472Simp		return;
23152472Simp	}
23252472Simp	sc->arpcom.ac_if.if_flags &= ~IFF_RUNNING;
23352472Simp	if_down(&sc->arpcom.ac_if);
23452472Simp	sc->gone = 1;
23552549Smdodd	bus_teardown_intr(dev, sc->irq, sc->ep_intrhand);
23652549Smdodd	ep_free(dev);
23752472Simp	device_printf(dev, "unload\n");
23851673Smdodd}
23951673Smdodd
24052472Simpstatic device_method_t ep_pccard_methods[] = {
24152472Simp	/* Device interface */
24252472Simp	DEVMETHOD(device_probe,		ep_pccard_probe),
24352472Simp	DEVMETHOD(device_attach,	ep_pccard_attach),
24452472Simp	DEVMETHOD(device_detach,	ep_pccard_detach),
24551673Smdodd
24652472Simp	{ 0, 0 }
24752472Simp};
24851673Smdodd
24952472Simpstatic driver_t ep_pccard_driver = {
25052472Simp	"ep",
25152472Simp	ep_pccard_methods,
25252549Smdodd	sizeof(struct ep_softc),
25352472Simp};
25451673Smdodd
25552472Simpextern devclass_t ep_devclass;
25652472Simp
25752472SimpDRIVER_MODULE(ep, pccard, ep_pccard_driver, ep_devclass, 0, 0);
258