if_ep_pccard.c revision 56017
1248590Smm/*
2248590Smm * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca>
3248590Smm * All rights reserved.
4248590Smm *
5248590Smm * Redistribution and use in source and binary forms, with or without
6248590Smm * modification, are permitted provided that the following conditions
7248590Smm * are met:
8248590Smm * 1. Redistributions of source code must retain the above copyright
9248590Smm *    notice, this list of conditions and the following disclaimer.
10248590Smm * 2. Redistributions in binary form must reproduce the above copyright
11248590Smm *    notice, this list of conditions and the following disclaimer in the
12248590Smm *    documentation and/or other materials provided with the distribution.
13248590Smm * 3. All advertising materials mentioning features or use of this software
14248590Smm *    must display the following acknowledgement:
15248590Smm *      This product includes software developed by Herb Peyerl.
16248590Smm * 4. The name of Herb Peyerl may not be used to endorse or promote products
17248590Smm *    derived from this software without specific prior written permission.
18248590Smm *
19248590Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20248590Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21248590Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22248590Smm * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23248590Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24248590Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25248590Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26248590Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27248590Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28248590Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29248590Smm *
30248590Smm * $FreeBSD: head/sys/dev/ep/if_ep_pccard.c 56017 2000-01-15 05:21:43Z mdodd $
31248590Smm */
32248590Smm
33248590Smm/*
34248590Smm * Pccard support for 3C589 by:
35248590Smm *		HAMADA Naoki
36248590Smm *		nao@tom-yam.or.jp
37248590Smm */
38248590Smm
39248590Smm#include <sys/param.h>
40248590Smm#include <sys/systm.h>
41248590Smm#include <sys/kernel.h>
42248590Smm#include <sys/socket.h>
43248590Smm
44248590Smm#include <sys/module.h>
45248590Smm#include <sys/bus.h>
46248590Smm
47248590Smm#include <machine/bus.h>
48248590Smm#include <machine/resource.h>
49248590Smm#include <sys/rman.h>
50248590Smm
51248590Smm#include <net/if.h>
52248590Smm#include <net/if_arp.h>
53248590Smm#include <net/if_media.h>
54248590Smm
55248590Smm#include <machine/clock.h>
56248590Smm
57248590Smm#include <dev/ep/if_epreg.h>
58248590Smm#include <dev/ep/if_epvar.h>
59248590Smm
60248590Smmstatic const char *ep_pccard_identify(u_short id);
61248590Smm
62248590Smm/*
63248590Smm * Initialize the device - called from Slot manager.
64248590Smm */
65248590Smmstatic int
66248590Smmep_pccard_probe(device_t dev)
67248590Smm{
68248590Smm	struct ep_softc *	sc = device_get_softc(dev);
69248590Smm	struct ep_board *	epb = &sc->epb;
70248590Smm	const char *		desc;
71248590Smm	int			error;
72248590Smm
73248590Smm	error = ep_alloc(dev);
74248590Smm	if (error)
75248590Smm		return error;
76248590Smm
77248590Smm	/*
78248590Smm	 * XXX - Certain (newer?) 3Com cards need epb->cmd_off ==
79248590Smm	 * 2. Sadly, you need to have a correct cmd_off in order to
80248590Smm	 * identify the card.  So we have to hit it with both and
81248590Smm	 * cross our virtual fingers.  There's got to be a better way
82248590Smm	 * to do this.  jyoung@accessus.net 09/11/1999
83248590Smm	 */
84248590Smm
85248590Smm	epb->cmd_off = 0;
86248590Smm	epb->prod_id = get_e(sc, EEPROM_PROD_ID);
87248590Smm	if ((desc = ep_pccard_identify(epb->prod_id)) == NULL) {
88248590Smm		if (bootverbose)
89248590Smm			device_printf(dev, "Pass 1 of 2 detection "
90248590Smm			    "failed (nonfatal)\n");
91248590Smm		epb->cmd_off = 2;
92248590Smm		epb->prod_id = get_e(sc, EEPROM_PROD_ID);
93248590Smm		if ((desc = ep_pccard_identify(epb->prod_id)) == NULL) {
94248590Smm			device_printf(dev, "Unit failed to come ready or "
95248590Smm			    "product ID unknown! (id 0x%x)\n", epb->prod_id);
96248590Smm			ep_free(dev);
97248590Smm			return (ENXIO);
98248590Smm		}
99248590Smm	}
100248590Smm	device_set_desc(dev, desc);
101248590Smm
102248590Smm	/*
103248590Smm	 * For some reason the 3c574 needs this.
104248590Smm	 */
105248590Smm	ep_get_macaddr(sc, (u_char *)&sc->arpcom.ac_enaddr);
106248590Smm
107248590Smm	ep_free(dev);
108248590Smm	return (0);
109248590Smm}
110248590Smm
111248590Smmstatic const char *
112248590Smmep_pccard_identify(u_short id)
113248590Smm{
114248590Smm	/* Determine device type and associated MII capabilities  */
115248590Smm	switch (id) {
116248590Smm	case 0x6055: /* 3C556 */
117248590Smm		return ("3Com 3C556");
118248590Smm	case 0x4057: /* 3C574 */
119248590Smm		return ("3Com 3C574");
120248590Smm	case 0x4b57: /* 3C574B */
121248590Smm		return ("3Com 3C574B, Megahertz 3CCFE574BT or "
122248590Smm		    "Fast Etherlink 3C574-TX");
123248590Smm	case 0x2b57: /* 3CXSH572BT */
124248590Smm		return ("3Com OfficeConnect 572BT");
125248590Smm	case 0x9058: /* 3C589 */
126248590Smm		return ("3Com Etherlink III 3C589");
127248590Smm	case 0x2056: /* 3C562/3C563 */
128248590Smm		return ("3Com 3C562D/3C563D");
129248590Smm	}
130248590Smm	return (NULL);
131248590Smm}
132248590Smm
133248590Smmstatic int
134248590Smmep_pccard_card_attach(struct ep_board *epb)
135248590Smm{
136248590Smm	/* Determine device type and associated MII capabilities  */
137248590Smm	switch (epb->prod_id) {
138248590Smm	case 0x6055: /* 3C556 */
139248590Smm	case 0x2b57: /* 3C572BT */
140248590Smm	case 0x4057: /* 3C574 */
141248590Smm	case 0x4b57: /* 3C574B */
142248590Smm		epb->mii_trans = 1;
143248590Smm		return (1);
144248590Smm	case 0x2056: /* 3C562D/3C563D */
145248590Smm	case 0x9058: /* 3C589 */
146248590Smm		epb->mii_trans = 0;
147248590Smm		return (1);
148248590Smm	}
149248590Smm	return (0);
150248590Smm}
151248590Smm
152248590Smmstatic int
153248590Smmep_pccard_attach(device_t dev)
154248590Smm{
155248590Smm	struct ep_softc *	sc = device_get_softc(dev);
156248590Smm	int			error = 0;
157248590Smm
158248590Smm	if ((error = ep_alloc(dev))) {
159248590Smm		device_printf(dev, "ep_alloc() failed! (%d)\n", error);
160248590Smm		goto bad;
161248590Smm	}
162248590Smm
163248590Smm	sc->epb.cmd_off = 0;
164	sc->epb.prod_id = get_e(sc, EEPROM_PROD_ID);
165	if (!ep_pccard_card_attach(&sc->epb)) {
166		sc->epb.cmd_off = 2;
167		sc->epb.prod_id = get_e(sc, EEPROM_PROD_ID);
168		sc->epb.res_cfg = get_e(sc, EEPROM_RESOURCE_CFG);
169		if (!ep_pccard_card_attach(&sc->epb)) {
170			device_printf(dev,
171			    "Probe found ID, attach failed so ignore card!\n");
172			error = ENXIO;
173			goto bad;
174		}
175	}
176
177	/* ROM size = 0, ROM base = 0 */
178	/* For now, ignore AUTO SELECT feature of 3C589B and later. */
179	outw(BASE + EP_W0_ADDRESS_CFG, get_e(sc, EEPROM_ADDR_CFG) & 0xc000);
180
181	/* Fake IRQ must be 3 */
182	outw(BASE + EP_W0_RESOURCE_CFG, (sc->epb.res_cfg & 0x0fff) | 0x3000);
183
184	outw(BASE + EP_W0_PRODUCT_ID, sc->epb.prod_id);
185
186	if (sc->epb.mii_trans) {
187		/*
188		 * turn on the MII transciever
189		 */
190		GO_WINDOW(3);
191		outw(BASE + EP_W3_OPTIONS, 0x8040);
192		DELAY(1000);
193		outw(BASE + EP_W3_OPTIONS, 0xc040);
194		outw(BASE + EP_COMMAND, RX_RESET);
195		outw(BASE + EP_COMMAND, TX_RESET);
196		while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
197		DELAY(1000);
198		outw(BASE + EP_W3_OPTIONS, 0x8040);
199	} else {
200		ep_get_media(sc);
201	}
202
203	if ((error = ep_attach(sc))) {
204		device_printf(dev, "ep_attach() failed! (%d)\n", error);
205		goto bad;
206	}
207
208	if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET, ep_intr,
209				    sc, &sc->ep_intrhand))) {
210		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
211		goto bad;
212	}
213
214	return (0);
215bad:
216	ep_free(dev);
217	return (error);
218}
219
220static int
221ep_pccard_detach(device_t dev)
222{
223	struct ep_softc *sc = device_get_softc(dev);
224
225	if (sc->gone) {
226		device_printf(dev, "already unloaded\n");
227		return (0);
228	}
229	sc->arpcom.ac_if.if_flags &= ~IFF_RUNNING;
230	if_detach(&sc->arpcom.ac_if);
231	sc->gone = 1;
232	bus_teardown_intr(dev, sc->irq, sc->ep_intrhand);
233	ep_free(dev);
234	device_printf(dev, "unload\n");
235	return (0);
236}
237
238static device_method_t ep_pccard_methods[] = {
239	/* Device interface */
240	DEVMETHOD(device_probe,		ep_pccard_probe),
241	DEVMETHOD(device_attach,	ep_pccard_attach),
242	DEVMETHOD(device_detach,	ep_pccard_detach),
243
244	{ 0, 0 }
245};
246
247static driver_t ep_pccard_driver = {
248	"ep",
249	ep_pccard_methods,
250	sizeof(struct ep_softc),
251};
252
253extern devclass_t ep_devclass;
254
255DRIVER_MODULE(ep, pccard, ep_pccard_driver, ep_devclass, 0, 0);
256