if_ep_pccard.c revision 119572
1/*
2 * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *      This product includes software developed by Herb Peyerl.
16 * 4. The name of Herb Peyerl may not be used to endorse or promote products
17 *    derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: head/sys/dev/ep/if_ep_pccard.c 119572 2003-08-30 08:10:58Z markm $");
33
34/*
35 * Pccard support for 3C589 by:
36 *		HAMADA Naoki
37 *		nao@tom-yam.or.jp
38 */
39
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: head/sys/dev/ep/if_ep_pccard.c 119572 2003-08-30 08:10:58Z markm $");
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/kernel.h>
46#include <sys/socket.h>
47#include <sys/module.h>
48#include <sys/bus.h>
49
50#include <machine/bus.h>
51#include <machine/resource.h>
52
53#include <net/ethernet.h>
54#include <net/if.h>
55#include <net/if_arp.h>
56#include <net/if_media.h>
57
58#include <dev/ep/if_epreg.h>
59#include <dev/ep/if_epvar.h>
60
61#include <dev/pccard/pccardvar.h>
62#include <dev/pccard/pccarddevs.h>
63
64#include "card_if.h"
65
66static const char *ep_pccard_identify(u_short id);
67
68/*
69 * Initialize the device - called from Slot manager.
70 */
71static int
72ep_pccard_probe(device_t dev)
73{
74	struct ep_softc *sc = device_get_softc(dev);
75	struct ep_board *epb = &sc->epb;
76	const char *desc;
77	u_int16_t result;
78	int error;
79
80	error = ep_alloc(dev);
81	if (error)
82		return (error);
83
84	/*
85	 * XXX - Certain (newer?) 3Com cards need epb->cmd_off ==
86	 * 2. Sadly, you need to have a correct cmd_off in order to
87	 * identify the card.  So we have to hit it with both and
88	 * cross our virtual fingers.  There's got to be a better way
89	 * to do this.  jyoung@accessus.net 09/11/1999
90	 */
91
92	epb->cmd_off = 0;
93
94	/* XXX check return */
95	error = get_e(sc, EEPROM_PROD_ID, &result);
96	epb->prod_id = result;
97
98	if ((desc = ep_pccard_identify(epb->prod_id)) == NULL) {
99		if (bootverbose)
100			device_printf(dev, "Pass 1 of 2 detection "
101			    "failed (nonfatal) id 0x%x\n", epb->prod_id);
102		epb->cmd_off = 2;
103		/* XXX check return */
104		error = get_e(sc, EEPROM_PROD_ID, &result);
105		epb->prod_id = result;
106		if ((desc = ep_pccard_identify(epb->prod_id)) == NULL) {
107			device_printf(dev, "Unit failed to come ready or "
108			    "product ID unknown! (id 0x%x)\n", epb->prod_id);
109			ep_free(dev);
110			return (ENXIO);
111		}
112	}
113	device_set_desc(dev, desc);
114
115	/*
116	 * For some reason the 3c574 needs this.
117	 */
118	error = ep_get_macaddr(sc, (u_char *)&sc->arpcom.ac_enaddr);
119
120	ep_free(dev);
121	return (0);
122}
123
124static const char *
125ep_pccard_identify(u_short id)
126{
127	/* Determine device type and associated MII capabilities  */
128	switch (id) {
129		case 0x6055:	/* 3C556 */
130		return ("3Com 3C556");
131	case 0x4057:		/* 3C574 */
132		return ("3Com 3C574");
133	case 0x4b57:		/* 3C574B */
134		return ("3Com 3C574B, Megahertz 3CCFE574BT or "
135		    "Fast Etherlink 3C574-TX");
136	case 0x2b57:		/* 3CXSH572BT */
137		return ("3Com OfficeConnect 572BT");
138	case 0x9058:		/* 3C589 */
139		return ("3Com Etherlink III 3C589");
140	case 0x2056:		/* 3C562/3C563 */
141		return ("3Com 3C562D/3C563D");
142	case 0x0010:		/* 3C1 */
143		return ("3Com Megahertz C1");
144	default:
145		return (NULL);
146	}
147}
148
149static int
150ep_pccard_card_attach(struct ep_board * epb)
151{
152	/* Determine device type and associated MII capabilities  */
153	switch (epb->prod_id) {
154		case 0x6055:	/* 3C556 */
155		case 0x2b57:	/* 3C572BT */
156		case 0x4057:	/* 3C574 */
157		case 0x4b57:	/* 3C574B */
158		case 0x0010:	/* 3C1 */
159		epb->mii_trans = 1;
160		return (1);
161	case 0x2056:		/* 3C562D/3C563D */
162	case 0x9058:		/* 3C589 */
163		epb->mii_trans = 0;
164		return (1);
165	default:
166		return (0);
167	}
168}
169
170static int
171ep_pccard_attach(device_t dev)
172{
173	struct ep_softc *sc = device_get_softc(dev);
174	u_int16_t result;
175	int error = 0;
176
177	if ((error = ep_alloc(dev))) {
178		device_printf(dev, "ep_alloc() failed! (%d)\n", error);
179		goto bad;
180	}
181	sc->epb.cmd_off = 0;
182
183	/* XXX check return */
184	error = get_e(sc, EEPROM_PROD_ID, &result);
185	sc->epb.prod_id = result;
186
187	if (!ep_pccard_card_attach(&sc->epb)) {
188		sc->epb.cmd_off = 2;
189		error = get_e(sc, EEPROM_PROD_ID, &result);
190		sc->epb.prod_id = result;
191		error = get_e(sc, EEPROM_RESOURCE_CFG, &result);
192		sc->epb.res_cfg = result;
193		if (!ep_pccard_card_attach(&sc->epb)) {
194			device_printf(dev,
195			    "Probe found ID, attach failed so ignore card!\n");
196			error = ENXIO;
197			goto bad;
198		}
199	}
200	error = get_e(sc, EEPROM_ADDR_CFG, &result);
201
202	/* ROM size = 0, ROM base = 0 */
203	/* For now, ignore AUTO SELECT feature of 3C589B and later. */
204	outw(BASE + EP_W0_ADDRESS_CFG, result & 0xc000);
205
206	/* Fake IRQ must be 3 */
207	outw(BASE + EP_W0_RESOURCE_CFG, (sc->epb.res_cfg & 0x0fff) | 0x3000);
208
209	outw(BASE + EP_W0_PRODUCT_ID, sc->epb.prod_id);
210
211	if (sc->epb.mii_trans) {
212		/*
213		 * turn on the MII transciever
214		 */
215		GO_WINDOW(3);
216		outw(BASE + EP_W3_OPTIONS, 0x8040);
217		DELAY(1000);
218		outw(BASE + EP_W3_OPTIONS, 0xc040);
219		outw(BASE + EP_COMMAND, RX_RESET);
220		outw(BASE + EP_COMMAND, TX_RESET);
221		while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
222		DELAY(1000);
223		outw(BASE + EP_W3_OPTIONS, 0x8040);
224	} else
225		ep_get_media(sc);
226
227	if ((error = ep_attach(sc))) {
228		device_printf(dev, "ep_attach() failed! (%d)\n", error);
229		goto bad;
230	}
231	if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET, ep_intr,
232		    sc, &sc->ep_intrhand))) {
233		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
234		goto bad;
235	}
236	return (0);
237bad:
238	ep_free(dev);
239	return (error);
240}
241
242static const struct pccard_product ep_pccard_products[] = {
243	PCMCIA_CARD(3COM, 3C1, 0),
244	PCMCIA_CARD(3COM, 3C562, 0),
245	PCMCIA_CARD(3COM, 3C574, 0),	/* ROADRUNNER */
246	PCMCIA_CARD(3COM, 3C589, 0),
247	PCMCIA_CARD(3COM, 3CCFEM556BI, 0),	/* ROADRUNNER */
248	PCMCIA_CARD(3COM, 3CXEM556, 0),
249	PCMCIA_CARD(3COM, 3CXEM556INT, 0),
250	{NULL}
251};
252
253static int
254ep_pccard_match(device_t dev)
255{
256	const struct pccard_product *pp;
257
258	if ((pp = pccard_product_lookup(dev, ep_pccard_products,
259		    sizeof(ep_pccard_products[0]), NULL)) != NULL) {
260		if (pp->pp_name != NULL)
261			device_set_desc(dev, pp->pp_name);
262		return 0;
263	}
264	return EIO;
265}
266
267static device_method_t ep_pccard_methods[] = {
268	/* Device interface */
269	DEVMETHOD(device_probe, pccard_compat_probe),
270	DEVMETHOD(device_attach, pccard_compat_attach),
271	DEVMETHOD(device_detach, ep_detach),
272
273	/* Card interface */
274	DEVMETHOD(card_compat_match, ep_pccard_match),
275	DEVMETHOD(card_compat_probe, ep_pccard_probe),
276	DEVMETHOD(card_compat_attach, ep_pccard_attach),
277
278	{0, 0}
279};
280
281static driver_t ep_pccard_driver = {
282	"ep",
283	ep_pccard_methods,
284	sizeof(struct ep_softc),
285};
286
287extern devclass_t ep_devclass;
288
289DRIVER_MODULE(ep, pccard, ep_pccard_driver, ep_devclass, 0, 0);
290