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