if_ep_pccard.c revision 147649
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/*
32 * Pccard support for 3C589 by:
33 *		HAMADA Naoki
34 *		nao@tom-yam.or.jp
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/sys/dev/ep/if_ep_pccard.c 147649 2005-06-28 21:56:04Z imp $");
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/socket.h>
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#include <dev/ep/if_epreg.h>
56#include <dev/ep/if_epvar.h>
57
58#include <dev/pccard/pccardvar.h>
59#include <dev/pccard/pccard_cis.h>
60
61#include "pccarddevs.h"
62
63struct ep_pccard_product
64{
65	struct pccard_product prod;
66	int chipset;
67};
68
69#define EP_CHIP_589	1	/* Classic 3c5x9 chipset */
70#define EP_CHIP_574	2	/* Roadrunner */
71
72static const struct ep_pccard_product ep_pccard_products[] = {
73	{ PCMCIA_CARD(3COM, 3C1),		EP_CHIP_589 },
74	{ PCMCIA_CARD(3COM, 3C562),		EP_CHIP_589 },
75	{ PCMCIA_CARD(3COM, 3C589),		EP_CHIP_589 },
76	{ PCMCIA_CARD(3COM, 3CXEM556),		EP_CHIP_589 },
77	{ PCMCIA_CARD(3COM, 3CXEM556INT),	EP_CHIP_589 },
78	{ PCMCIA_CARD(3COM, 3C574),		EP_CHIP_574 },
79	{ PCMCIA_CARD(3COM, 3CCFEM556BI),	EP_CHIP_574 },
80	{ { NULL } }
81};
82
83static const struct ep_pccard_product *
84ep_pccard_lookup(device_t dev)
85{
86	return ((const struct ep_pccard_product *)pccard_product_lookup(dev,
87	    (const struct pccard_product *)ep_pccard_products,
88	    sizeof(ep_pccard_products[0]), NULL));
89}
90
91static int
92ep_pccard_probe(device_t dev)
93{
94	const struct ep_pccard_product *pp;
95	int		error;
96	uint32_t	fcn = PCCARD_FUNCTION_UNSPEC;
97
98	/* Make sure we're a network function */
99	error = pccard_get_function(dev, &fcn);
100	if (error != 0)
101		return (error);
102	if (fcn != PCCARD_FUNCTION_NETWORK)
103		return (ENXIO);
104
105	/* Check to see if we know about this card */
106	if ((pp = ep_pccard_lookup(dev)) == NULL)
107		return EIO;
108	if (pp->prod.pp_name != NULL)
109		device_set_desc(dev, pp->prod.pp_name);
110
111	return 0;
112}
113
114static int
115ep_pccard_attach(device_t dev)
116{
117	struct ep_softc *sc = device_get_softc(dev);
118	uint16_t result;
119	int error = 0;
120	const struct ep_pccard_product *pp;
121
122	if ((pp = ep_pccard_lookup(dev)) == NULL)
123		panic("ep_pccard_attach: can't find product in attach.");
124
125	if ((error = ep_alloc(dev))) {
126		device_printf(dev, "ep_alloc() failed! (%d)\n", error);
127		goto bad;
128	}
129
130	if (pp->chipset == EP_CHIP_589) {
131		sc->epb.mii_trans = 0;
132		sc->epb.cmd_off = 0;
133	} else {
134		sc->epb.mii_trans = 1;
135		sc->epb.cmd_off = 2;
136	}
137
138	error = ep_get_e(sc, EEPROM_PROD_ID, &result);
139	sc->epb.prod_id = result;
140
141	/* ROM size = 0, ROM base = 0 */
142	/* For now, ignore AUTO SELECT feature of 3C589B and later. */
143	error = ep_get_e(sc, EEPROM_ADDR_CFG, &result);
144	CSR_WRITE_2(sc, EP_W0_ADDRESS_CFG, result & 0xc000);
145
146	/*
147	 * Fake IRQ must be 3 for 3C589 and 3C589B.  3C589D and newer
148	 * ignore this value.  3C589C is unknown, as are the other
149	 * cards supported by this driver, but it appears to never hurt
150	 * and always helps.
151	 */
152	SET_IRQ(sc, 3);
153	CSR_WRITE_2(sc, EP_W0_PRODUCT_ID, sc->epb.prod_id);
154
155	if (sc->epb.mii_trans) {
156		/*
157		 * turn on the MII transciever
158		 */
159		GO_WINDOW(sc, 3);
160		CSR_WRITE_2(sc, EP_W3_OPTIONS, 0x8040);
161		DELAY(1000);
162		CSR_WRITE_2(sc, EP_W3_OPTIONS, 0xc040);
163		CSR_WRITE_2(sc, EP_COMMAND, RX_RESET);
164		CSR_WRITE_2(sc, EP_COMMAND, TX_RESET);
165		EP_BUSY_WAIT(sc);
166		DELAY(1000);
167		CSR_WRITE_2(sc, EP_W3_OPTIONS, 0x8040);
168	} else
169		ep_get_media(sc);
170
171	if ((error = ep_attach(sc))) {
172		device_printf(dev, "ep_attach() failed! (%d)\n", error);
173		goto bad;
174	}
175	if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
176	    ep_intr, sc, &sc->ep_intrhand))) {
177		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
178		goto bad;
179	}
180	return (0);
181bad:
182	ep_free(dev);
183	return (error);
184}
185
186static device_method_t ep_pccard_methods[] = {
187	/* Device interface */
188	DEVMETHOD(device_probe, ep_pccard_probe),
189	DEVMETHOD(device_attach, ep_pccard_attach),
190	DEVMETHOD(device_detach, ep_detach),
191
192	{0, 0}
193};
194
195static driver_t ep_pccard_driver = {
196	"ep",
197	ep_pccard_methods,
198	sizeof(struct ep_softc),
199};
200
201extern devclass_t ep_devclass;
202
203DRIVER_MODULE(ep, pccard, ep_pccard_driver, ep_devclass, 0, 0);
204