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