if_ep_pccard.c revision 112829
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 * $FreeBSD: head/sys/dev/ep/if_ep_pccard.c 112829 2003-03-29 22:27:41Z mdodd $
31 */
32
33/*
34 * Pccard support for 3C589 by:
35 *		HAMADA Naoki
36 *		nao@tom-yam.or.jp
37 */
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#include <sys/socket.h>
43
44#include <sys/module.h>
45#include <sys/bus.h>
46
47#include <machine/bus.h>
48#include <machine/resource.h>
49
50#include <net/ethernet.h>
51#include <net/if.h>
52#include <net/if_arp.h>
53#include <net/if_media.h>
54
55
56#include <dev/ep/if_epreg.h>
57#include <dev/ep/if_epvar.h>
58
59#include <dev/pccard/pccardvar.h>
60#include <dev/pccard/pccarddevs.h>
61#include "card_if.h"
62
63static const char *ep_pccard_identify(u_short id);
64
65/*
66 * Initialize the device - called from Slot manager.
67 */
68static int
69ep_pccard_probe(device_t dev)
70{
71	struct ep_softc *	sc = device_get_softc(dev);
72	struct ep_board *	epb = &sc->epb;
73	const char *		desc;
74	u_int16_t		result;
75	int			error;
76
77	error = ep_alloc(dev);
78	if (error)
79		return error;
80
81	/*
82	 * XXX - Certain (newer?) 3Com cards need epb->cmd_off ==
83	 * 2. Sadly, you need to have a correct cmd_off in order to
84	 * identify the card.  So we have to hit it with both and
85	 * cross our virtual fingers.  There's got to be a better way
86	 * to do this.  jyoung@accessus.net 09/11/1999
87	 */
88
89	epb->cmd_off = 0;
90
91	error = get_e(sc, EEPROM_PROD_ID, &result);	/* XXX check return */
92	epb->prod_id = result;
93
94	if ((desc = ep_pccard_identify(epb->prod_id)) == NULL) {
95		if (bootverbose)
96			device_printf(dev, "Pass 1 of 2 detection "
97			    "failed (nonfatal) id 0x%x\n", epb->prod_id);
98		epb->cmd_off = 2;
99		error = get_e(sc, EEPROM_PROD_ID, &result);	/* XXX check return */
100		epb->prod_id = result;
101		if ((desc = ep_pccard_identify(epb->prod_id)) == NULL) {
102			device_printf(dev, "Unit failed to come ready or "
103			    "product ID unknown! (id 0x%x)\n", epb->prod_id);
104			ep_free(dev);
105			return (ENXIO);
106		}
107	}
108	device_set_desc(dev, desc);
109
110	/*
111	 * For some reason the 3c574 needs this.
112	 */
113	error = ep_get_macaddr(sc, (u_char *)&sc->arpcom.ac_enaddr);
114
115	ep_free(dev);
116	return (0);
117}
118
119static const char *
120ep_pccard_identify(u_short id)
121{
122	/* Determine device type and associated MII capabilities  */
123	switch (id) {
124	case 0x6055: /* 3C556 */
125		return ("3Com 3C556");
126	case 0x4057: /* 3C574 */
127		return ("3Com 3C574");
128	case 0x4b57: /* 3C574B */
129		return ("3Com 3C574B, Megahertz 3CCFE574BT or "
130		    "Fast Etherlink 3C574-TX");
131	case 0x2b57: /* 3CXSH572BT */
132		return ("3Com OfficeConnect 572BT");
133	case 0x9058: /* 3C589 */
134		return ("3Com Etherlink III 3C589");
135	case 0x2056: /* 3C562/3C563 */
136		return ("3Com 3C562D/3C563D");
137	case 0x0010:	/* 3C1 */
138		return ("3Com Megahertz C1");
139	}
140	return (NULL);
141}
142
143static int
144ep_pccard_card_attach(struct ep_board *epb)
145{
146	/* Determine device type and associated MII capabilities  */
147	switch (epb->prod_id) {
148	case 0x6055: /* 3C556 */
149	case 0x2b57: /* 3C572BT */
150	case 0x4057: /* 3C574 */
151	case 0x4b57: /* 3C574B */
152	case 0x0010: /* 3C1 */
153		epb->mii_trans = 1;
154		return (1);
155	case 0x2056: /* 3C562D/3C563D */
156	case 0x9058: /* 3C589 */
157		epb->mii_trans = 0;
158		return (1);
159	}
160	return (0);
161}
162
163static int
164ep_pccard_attach(device_t dev)
165{
166	struct ep_softc *	sc = device_get_softc(dev);
167	u_int16_t		result;
168	int			error = 0;
169
170	if ((error = ep_alloc(dev))) {
171		device_printf(dev, "ep_alloc() failed! (%d)\n", error);
172		goto bad;
173	}
174
175	sc->epb.cmd_off = 0;
176
177	error = get_e(sc, EEPROM_PROD_ID, &result);	/* XXX check return */
178	sc->epb.prod_id = result;
179
180	if (!ep_pccard_card_attach(&sc->epb)) {
181		sc->epb.cmd_off = 2;
182		error = get_e(sc, EEPROM_PROD_ID, &result);
183		sc->epb.prod_id = result;
184		error = get_e(sc, EEPROM_RESOURCE_CFG, &result);
185		sc->epb.res_cfg = result;
186		if (!ep_pccard_card_attach(&sc->epb)) {
187			device_printf(dev,
188			    "Probe found ID, attach failed so ignore card!\n");
189			error = ENXIO;
190			goto bad;
191		}
192	}
193
194	error = get_e(sc, EEPROM_ADDR_CFG, &result);
195
196	/* ROM size = 0, ROM base = 0 */
197	/* For now, ignore AUTO SELECT feature of 3C589B and later. */
198	outw(BASE + EP_W0_ADDRESS_CFG, result & 0xc000);
199
200	/* Fake IRQ must be 3 */
201	outw(BASE + EP_W0_RESOURCE_CFG, (sc->epb.res_cfg & 0x0fff) | 0x3000);
202
203	outw(BASE + EP_W0_PRODUCT_ID, sc->epb.prod_id);
204
205	if (sc->epb.mii_trans) {
206		/*
207		 * turn on the MII transciever
208		 */
209		GO_WINDOW(3);
210		outw(BASE + EP_W3_OPTIONS, 0x8040);
211		DELAY(1000);
212		outw(BASE + EP_W3_OPTIONS, 0xc040);
213		outw(BASE + EP_COMMAND, RX_RESET);
214		outw(BASE + EP_COMMAND, TX_RESET);
215		while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
216		DELAY(1000);
217		outw(BASE + EP_W3_OPTIONS, 0x8040);
218	} else {
219		ep_get_media(sc);
220	}
221
222	if ((error = ep_attach(sc))) {
223		device_printf(dev, "ep_attach() failed! (%d)\n", error);
224		goto bad;
225	}
226
227	if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET, ep_intr,
228				    sc, &sc->ep_intrhand))) {
229		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
230		goto bad;
231	}
232
233	return (0);
234bad:
235	ep_free(dev);
236	return (error);
237}
238
239static const struct pccard_product ep_pccard_products[] = {
240	PCMCIA_CARD(3COM, 3C1, 0),
241	PCMCIA_CARD(3COM, 3C562, 0),
242	PCMCIA_CARD(3COM, 3C574, 0),		/* ROADRUNNER */
243	PCMCIA_CARD(3COM, 3C589, 0),
244	PCMCIA_CARD(3COM, 3CCFEM556BI, 0),	/* ROADRUNNER */
245	PCMCIA_CARD(3COM, 3CXEM556, 0),
246	PCMCIA_CARD(3COM, 3CXEM556INT, 0),
247	{ NULL }
248};
249
250static int
251ep_pccard_match(device_t dev)
252{
253	const struct pccard_product *pp;
254
255	if ((pp = pccard_product_lookup(dev, ep_pccard_products,
256	    sizeof(ep_pccard_products[0]), NULL)) != NULL) {
257		device_set_desc(dev, pp->pp_name);
258		return 0;
259	}
260	return EIO;
261}
262
263static device_method_t ep_pccard_methods[] = {
264	/* Device interface */
265	DEVMETHOD(device_probe,		pccard_compat_probe),
266	DEVMETHOD(device_attach,	pccard_compat_attach),
267	DEVMETHOD(device_detach,	ep_detach),
268
269	/* Card interface */
270	DEVMETHOD(card_compat_match,	ep_pccard_match),
271	DEVMETHOD(card_compat_probe,	ep_pccard_probe),
272	DEVMETHOD(card_compat_attach,	ep_pccard_attach),
273
274	{ 0, 0 }
275};
276
277static driver_t ep_pccard_driver = {
278	"ep",
279	ep_pccard_methods,
280	sizeof(struct ep_softc),
281};
282
283extern devclass_t ep_devclass;
284
285DRIVER_MODULE(ep, pccard, ep_pccard_driver, ep_devclass, 0, 0);
286