if_ep_pccard.c revision 140523
1139749Simp/*-
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
3151673Smdodd/*
3251673Smdodd * Pccard support for 3C589 by:
3351673Smdodd *		HAMADA Naoki
3451673Smdodd *		nao@tom-yam.or.jp
3551673Smdodd */
3651673Smdodd
37117700Smarkm#include <sys/cdefs.h>
38117700Smarkm__FBSDID("$FreeBSD: head/sys/dev/ep/if_ep_pccard.c 140523 2005-01-20 19:39:33Z imp $");
39117700Smarkm
4051673Smdodd#include <sys/param.h>
4152549Smdodd#include <sys/systm.h>
4251673Smdodd#include <sys/kernel.h>
4351673Smdodd#include <sys/socket.h>
4452549Smdodd#include <sys/module.h>
4552472Simp#include <sys/bus.h>
4652549Smdodd
4752472Simp#include <machine/bus.h>
4852549Smdodd#include <machine/resource.h>
49117700Smarkm
5063090Sarchie#include <net/ethernet.h>
51117700Smarkm#include <net/if.h>
5252549Smdodd#include <net/if_arp.h>
5352549Smdodd#include <net/if_media.h>
5451673Smdodd
5552472Simp#include <dev/ep/if_epreg.h>
5652472Simp#include <dev/ep/if_epvar.h>
5752472Simp
5866058Simp#include <dev/pccard/pccardvar.h>
59140522Simp#include <dev/pccard/pccard_cis.h>
60117700Smarkm
6197645Stakawata#include "card_if.h"
62129764Simp#include "pccarddevs.h"
6366058Simp
6452472Simpstatic const char *ep_pccard_identify(u_short id);
6551673Smdodd
6651673Smdodd/*
6751673Smdodd * Initialize the device - called from Slot manager.
6851673Smdodd */
6951673Smdoddstatic int
7052472Simpep_pccard_probe(device_t dev)
7151673Smdodd{
72117700Smarkm	struct ep_softc *sc = device_get_softc(dev);
73117700Smarkm	struct ep_board *epb = &sc->epb;
74117700Smarkm	const char *desc;
75140523Simp	uint16_t result;
76117700Smarkm	int error;
7751673Smdodd
7852589Simp	error = ep_alloc(dev);
7952589Simp	if (error)
80117700Smarkm		return (error);
8152549Smdodd
8252472Simp	/*
83121206Simp	 * It appears that the eeprom comes in two sizes.  There's
84121206Simp	 * a 512 byte eeprom and a 2k eeprom.  Bit 13 of the eeprom
85121206Simp	 * command register is supposed to contain the size of the
86121206Simp	 * eeprom.
87121206Simp	 */
88121206Simp	/*
8952472Simp	 * XXX - Certain (newer?) 3Com cards need epb->cmd_off ==
9052472Simp	 * 2. Sadly, you need to have a correct cmd_off in order to
9152472Simp	 * identify the card.  So we have to hit it with both and
9252472Simp	 * cross our virtual fingers.  There's got to be a better way
93117700Smarkm	 * to do this.  jyoung@accessus.net 09/11/1999
9452472Simp	 */
9551673Smdodd
9652472Simp	epb->cmd_off = 0;
97112822Smdodd
98119572Smarkm	/* XXX check return */
99119572Smarkm	error = get_e(sc, EEPROM_PROD_ID, &result);
100112822Smdodd	epb->prod_id = result;
101112822Smdodd
10252472Simp	if ((desc = ep_pccard_identify(epb->prod_id)) == NULL) {
103117700Smarkm		if (bootverbose)
10452472Simp			device_printf(dev, "Pass 1 of 2 detection "
10565794Simp			    "failed (nonfatal) id 0x%x\n", epb->prod_id);
10652472Simp		epb->cmd_off = 2;
107119572Smarkm		/* XXX check return */
108119572Smarkm		error = get_e(sc, EEPROM_PROD_ID, &result);
109112822Smdodd		epb->prod_id = result;
11052589Simp		if ((desc = ep_pccard_identify(epb->prod_id)) == NULL) {
11152472Simp			device_printf(dev, "Unit failed to come ready or "
11252472Simp			    "product ID unknown! (id 0x%x)\n", epb->prod_id);
11352589Simp			ep_free(dev);
11452472Simp			return (ENXIO);
11552472Simp		}
11651673Smdodd	}
11752589Simp	device_set_desc(dev, desc);
11856017Smdodd
11956017Smdodd	/*
120121904Simp	 * Newer cards supported by this device need to have their
121121904Simp	 * MAC address set.
12256017Smdodd	 */
123112822Smdodd	error = ep_get_macaddr(sc, (u_char *)&sc->arpcom.ac_enaddr);
12456017Smdodd
12552589Simp	ep_free(dev);
12652472Simp	return (0);
12752472Simp}
12851673Smdodd
12952472Simpstatic const char *
13052472Simpep_pccard_identify(u_short id)
13152472Simp{
13252472Simp	/* Determine device type and associated MII capabilities  */
13352472Simp	switch (id) {
134121904Simp	case 0x6055:	/* 3C556 */
13552472Simp		return ("3Com 3C556");
136117700Smarkm	case 0x4057:		/* 3C574 */
13752589Simp		return ("3Com 3C574");
138117700Smarkm	case 0x4b57:		/* 3C574B */
13952589Simp		return ("3Com 3C574B, Megahertz 3CCFE574BT or "
14052472Simp		    "Fast Etherlink 3C574-TX");
141117700Smarkm	case 0x2b57:		/* 3CXSH572BT */
14255702Simp		return ("3Com OfficeConnect 572BT");
143117700Smarkm	case 0x9058:		/* 3C589 */
14452589Simp		return ("3Com Etherlink III 3C589");
145117700Smarkm	case 0x2056:		/* 3C562/3C563 */
14652621Simp		return ("3Com 3C562D/3C563D");
147117700Smarkm	case 0x0010:		/* 3C1 */
14865794Simp		return ("3Com Megahertz C1");
149121904Simp	case 0x0035:
150121904Simp		return ("3Com 3CCEM556");
151117700Smarkm	default:
152121904Simp		printf("Unknown ID: 0x%x\n", id);
153117700Smarkm		return (NULL);
15452472Simp	}
15551673Smdodd}
15651673Smdodd
15751673Smdoddstatic int
158117700Smarkmep_pccard_card_attach(struct ep_board * epb)
15951673Smdodd{
16052472Simp	/* Determine device type and associated MII capabilities  */
16152472Simp	switch (epb->prod_id) {
162121206Simp	case 0x6055:		/* 3C556 */
163121206Simp	case 0x2b57:		/* 3C572BT */
164121206Simp	case 0x4057:		/* 3C574 */
165121206Simp	case 0x4b57:		/* 3C574B */
16652472Simp		epb->mii_trans = 1;
16752472Simp		return (1);
168117700Smarkm	case 0x2056:		/* 3C562D/3C563D */
169117700Smarkm	case 0x9058:		/* 3C589 */
170121206Simp	case 0x0010:		/* 3C1 */
171121904Simp	case 0x0035:		/* 3C[XC]EM556 */
17252472Simp		epb->mii_trans = 0;
17352472Simp		return (1);
174117700Smarkm	default:
175117700Smarkm		return (0);
17652472Simp	}
17751673Smdodd}
17851673Smdodd
17951673Smdoddstatic int
18052472Simpep_pccard_attach(device_t dev)
18151673Smdodd{
182117700Smarkm	struct ep_softc *sc = device_get_softc(dev);
183140523Simp	uint16_t result;
184117700Smarkm	int error = 0;
18551673Smdodd
18652585Simp	if ((error = ep_alloc(dev))) {
18752549Smdodd		device_printf(dev, "ep_alloc() failed! (%d)\n", error);
18852472Simp		goto bad;
18952472Simp	}
19052589Simp	sc->epb.cmd_off = 0;
191112822Smdodd
192119572Smarkm	/* XXX check return */
193119572Smarkm	error = get_e(sc, EEPROM_PROD_ID, &result);
194112822Smdodd	sc->epb.prod_id = result;
195112822Smdodd
19652549Smdodd	if (!ep_pccard_card_attach(&sc->epb)) {
19752549Smdodd		sc->epb.cmd_off = 2;
198112822Smdodd		error = get_e(sc, EEPROM_PROD_ID, &result);
199112822Smdodd		sc->epb.prod_id = result;
200112822Smdodd		error = get_e(sc, EEPROM_RESOURCE_CFG, &result);
201112822Smdodd		sc->epb.res_cfg = result;
20252549Smdodd		if (!ep_pccard_card_attach(&sc->epb)) {
20352472Simp			device_printf(dev,
20452472Simp			    "Probe found ID, attach failed so ignore card!\n");
20552549Smdodd			error = ENXIO;
20652549Smdodd			goto bad;
20752472Simp		}
20852472Simp	}
209112822Smdodd	error = get_e(sc, EEPROM_ADDR_CFG, &result);
210112822Smdodd
21152472Simp	/* ROM size = 0, ROM base = 0 */
21252472Simp	/* For now, ignore AUTO SELECT feature of 3C589B and later. */
213121492Simp	CSR_WRITE_2(sc, EP_W0_ADDRESS_CFG, result & 0xc000);
214121904Simp
215121904Simp	/*
216121904Simp	 * Fake IRQ must be 3 for 3C589 and 3C589B.  3C589D and newer
217121904Simp	 * ignore this value.  3C589C is unknown, as are the other
218121904Simp	 * cards supported by this driver, but it appears to never hurt
219121904Simp	 * and always helps.
220121904Simp	 */
221121588Simp	SET_IRQ(sc, 3);
222121492Simp	CSR_WRITE_2(sc, EP_W0_PRODUCT_ID, sc->epb.prod_id);
22352472Simp
22452549Smdodd	if (sc->epb.mii_trans) {
22552472Simp		/*
22652472Simp		 * turn on the MII transciever
22752472Simp		 */
228121588Simp		GO_WINDOW(sc, 3);
229121492Simp		CSR_WRITE_2(sc, EP_W3_OPTIONS, 0x8040);
23052472Simp		DELAY(1000);
231121492Simp		CSR_WRITE_2(sc, EP_W3_OPTIONS, 0xc040);
232121492Simp		CSR_WRITE_2(sc, EP_COMMAND, RX_RESET);
233121492Simp		CSR_WRITE_2(sc, EP_COMMAND, TX_RESET);
234121515Simp		EP_BUSY_WAIT(sc);
23552472Simp		DELAY(1000);
236121492Simp		CSR_WRITE_2(sc, EP_W3_OPTIONS, 0x8040);
237117700Smarkm	} else
23852549Smdodd		ep_get_media(sc);
23952472Simp
24052549Smdodd	if ((error = ep_attach(sc))) {
24152549Smdodd		device_printf(dev, "ep_attach() failed! (%d)\n", error);
24252472Simp		goto bad;
24352472Simp	}
244121492Simp	if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE, ep_intr,
245117700Smarkm		    sc, &sc->ep_intrhand))) {
24652549Smdodd		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
24752472Simp		goto bad;
24852472Simp	}
24952549Smdodd	return (0);
25052472Simpbad:
25152585Simp	ep_free(dev);
25252549Smdodd	return (error);
25351673Smdodd}
25451673Smdodd
25570765Speterstatic const struct pccard_product ep_pccard_products[] = {
25686395Simp	PCMCIA_CARD(3COM, 3C1, 0),
25786395Simp	PCMCIA_CARD(3COM, 3C562, 0),
258117700Smarkm	PCMCIA_CARD(3COM, 3C574, 0),	/* ROADRUNNER */
25986394Simp	PCMCIA_CARD(3COM, 3C589, 0),
26086395Simp	PCMCIA_CARD(3COM, 3CCFEM556BI, 0),	/* ROADRUNNER */
26186394Simp	PCMCIA_CARD(3COM, 3CXEM556, 0),
26286394Simp	PCMCIA_CARD(3COM, 3CXEM556INT, 0),
263117700Smarkm	{NULL}
26470765Speter};
26570765Speter
26666058Simpstatic int
26766058Simpep_pccard_match(device_t dev)
26866058Simp{
26970765Speter	const struct pccard_product *pp;
270140522Simp	int		error;
271140522Simp	uint32_t	fcn = PCCARD_FUNCTION_UNSPEC;
27270765Speter
273140522Simp	/* Make sure we're a network function */
274140522Simp	error = pccard_get_function(dev, &fcn);
275140522Simp	if (error != 0)
276140522Simp		return (error);
277140522Simp	if (fcn != PCCARD_FUNCTION_NETWORK)
278140522Simp		return (ENXIO);
279140522Simp
28070765Speter	if ((pp = pccard_product_lookup(dev, ep_pccard_products,
281117700Smarkm		    sizeof(ep_pccard_products[0]), NULL)) != NULL) {
282113315Simp		if (pp->pp_name != NULL)
283113315Simp			device_set_desc(dev, pp->pp_name);
28470765Speter		return 0;
28570765Speter	}
28666058Simp	return EIO;
28766058Simp}
28866058Simp
28952472Simpstatic device_method_t ep_pccard_methods[] = {
29052472Simp	/* Device interface */
291117700Smarkm	DEVMETHOD(device_probe, pccard_compat_probe),
292117700Smarkm	DEVMETHOD(device_attach, pccard_compat_attach),
293117700Smarkm	DEVMETHOD(device_detach, ep_detach),
29451673Smdodd
29566058Simp	/* Card interface */
296117700Smarkm	DEVMETHOD(card_compat_match, ep_pccard_match),
297117700Smarkm	DEVMETHOD(card_compat_probe, ep_pccard_probe),
298117700Smarkm	DEVMETHOD(card_compat_attach, ep_pccard_attach),
29966058Simp
300117700Smarkm	{0, 0}
30152472Simp};
30251673Smdodd
30352472Simpstatic driver_t ep_pccard_driver = {
30452472Simp	"ep",
30552472Simp	ep_pccard_methods,
30652549Smdodd	sizeof(struct ep_softc),
30752472Simp};
30851673Smdodd
30952472Simpextern devclass_t ep_devclass;
31052472Simp
31152472SimpDRIVER_MODULE(ep, pccard, ep_pccard_driver, ep_devclass, 0, 0);
312