if_ed_pccard.c revision 65771
1/*
2 * Copyright (c) 1995, David Greenman
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 unmodified, this list of conditions, and the following
10 *    disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: head/sys/dev/ed/if_ed_pccard.c 65771 2000-09-12 11:03:12Z ps $
28 */
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/socket.h>
33#include <sys/kernel.h>
34#include <sys/conf.h>
35#include <sys/uio.h>
36#include <sys/select.h>
37#include <machine/clock.h>
38
39#include <sys/module.h>
40#include <sys/bus.h>
41#include <machine/bus.h>
42
43#include <net/ethernet.h>
44#include <net/if.h>
45#include <net/if_arp.h>
46#include <net/if_mib.h>
47
48#include <dev/ed/if_edreg.h>
49#include <dev/ed/if_edvar.h>
50#include <dev/pccard/pccardvar.h>
51#include <pccard/cardinfo.h>
52#include <pccard/slot.h>
53
54#define CARD_MAJOR	50
55
56/*
57 *      PC-Card (PCMCIA) specific code.
58 */
59static int	ed_pccard_probe(device_t);
60static int	ed_pccard_attach(device_t);
61static int	ed_pccard_detach(device_t);
62
63static void	ax88190_geteprom(struct ed_softc *);
64static int	ed_pccard_memwrite(device_t dev, off_t offset, u_char byte);
65static int	ed_pccard_memread(device_t dev, off_t offset, u_char *buf, int size);
66static int	linksys;
67
68static device_method_t ed_pccard_methods[] = {
69	/* Device interface */
70	DEVMETHOD(device_probe,		ed_pccard_probe),
71	DEVMETHOD(device_attach,	ed_pccard_attach),
72	DEVMETHOD(device_detach,	ed_pccard_detach),
73
74	{ 0, 0 }
75};
76
77static driver_t ed_pccard_driver = {
78	"ed",
79	ed_pccard_methods,
80	sizeof(struct ed_softc)
81};
82
83static devclass_t ed_pccard_devclass;
84
85DRIVER_MODULE(ed, pccard, ed_pccard_driver, ed_pccard_devclass, 0, 0);
86
87/*
88 *      ed_pccard_detach - unload the driver and clear the table.
89 *      XXX TODO:
90 *      This is usually called when the card is ejected, but
91 *      can be caused by a modunload of a controller driver.
92 *      The idea is to reset the driver's view of the device
93 *      and ensure that any driver entry points such as
94 *      read and write do not hang.
95 */
96static int
97ed_pccard_detach(device_t dev)
98{
99	struct ed_softc *sc = device_get_softc(dev);
100	struct ifnet *ifp = &sc->arpcom.ac_if;
101
102	if (sc->gone) {
103		device_printf(dev, "already unloaded\n");
104		return (0);
105	}
106	ed_stop(sc);
107	ifp->if_flags &= ~IFF_RUNNING;
108	ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
109	sc->gone = 1;
110	bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
111	ed_release_resources(dev);
112	return (0);
113}
114
115/*
116 * Probe framework for pccards.  Replicates the standard framework,
117 * minus the pccard driver registration and ignores the ether address
118 * supplied (from the CIS), relying on the probe to find it instead.
119 */
120static int
121ed_pccard_probe(device_t dev)
122{
123	struct	ed_softc *sc = device_get_softc(dev);
124	int	flags = device_get_flags(dev);
125	int	error;
126
127	if (ED_FLAGS_GETTYPE(flags) == ED_FLAGS_AX88190) {
128		/* Special setup for AX88190 */
129		u_char rdbuf[4];
130		int iobase;
131		int attr_ioport;
132
133		/* XXX Allocate the port resource during setup. */
134		error = ed_alloc_port(dev, 0, ED_NOVELL_IO_PORTS);
135		if (error)
136			return (error);
137
138		sc->asic_offset = ED_NOVELL_ASIC_OFFSET;
139		sc->nic_offset  = ED_NOVELL_NIC_OFFSET;
140
141		sc->chip_type = ED_CHIP_TYPE_AX88190;
142
143		/*
144		 * Check & Set Attribute Memory IOBASE Register
145		 */
146		ed_pccard_memread(dev, ED_AX88190_IOBASE0, rdbuf, 4);
147		attr_ioport = rdbuf[2] << 8 | rdbuf[0];
148		iobase = rman_get_start(sc->port_res);
149		if (attr_ioport != iobase) {
150#if notdef
151			printf("AX88190 IOBASE MISMATCH %04x -> %04x Setting\n", attr_ioport, iobase);
152#endif /* notdef */
153			ed_pccard_memwrite(dev, ED_AX88190_IOBASE0,
154					   iobase & 0xff);
155			ed_pccard_memwrite(dev, ED_AX88190_IOBASE1,
156					   (iobase >> 8) & 0xff);
157		}
158		ax88190_geteprom(sc);
159
160		ed_release_resources(dev);
161	}
162
163	error = ed_probe_Novell(dev, 0, flags);
164	if (error == 0)
165		goto end;
166	ed_release_resources(dev);
167
168	error = ed_probe_WD80x3(dev, 0, flags);
169	if (error == 0)
170		goto end;
171	ed_release_resources(dev);
172
173end:
174	linksys = ed_get_Linksys(dev);
175	if (error == 0)
176		error = ed_alloc_irq(dev, 0, 0);
177
178	ed_release_resources(dev);
179	return (error);
180}
181
182static int
183ed_pccard_attach(device_t dev)
184{
185	struct ed_softc *sc = device_get_softc(dev);
186	int flags = device_get_flags(dev);
187	int error;
188	int i;
189	u_char sum;
190	u_char ether_addr[ETHER_ADDR_LEN];
191
192	if (sc->port_used > 0)
193		ed_alloc_port(dev, sc->port_rid, sc->port_used);
194	if (sc->mem_used)
195		ed_alloc_memory(dev, sc->mem_rid, sc->mem_used);
196	ed_alloc_irq(dev, sc->irq_rid, 0);
197
198	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET,
199			       edintr, sc, &sc->irq_handle);
200	if (error) {
201		printf("setup intr failed %d \n", error);
202		ed_release_resources(dev);
203		return (error);
204	}
205
206	if (linksys == 0) {
207		pccard_get_ether(dev, ether_addr);
208		for (i = 0, sum = 0; i < ETHER_ADDR_LEN; i++)
209			sum |= ether_addr[i];
210		if (sum)
211			bcopy(ether_addr, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
212	}
213
214	error = ed_attach(sc, device_get_unit(dev), flags);
215	return (error);
216}
217
218static void
219ax88190_geteprom(struct ed_softc *sc)
220{
221	int prom[16],i;
222	u_char tmp;
223	struct {
224		unsigned char offset, value;
225	} pg_seq[] = {
226		{ED_P0_CR, ED_CR_RD2|ED_CR_STP},	/* Select Page0 */
227		{ED_P0_DCR, 0x01},
228		{ED_P0_RBCR0, 0x00},	/* Clear the count regs. */
229		{ED_P0_RBCR1, 0x00},
230		{ED_P0_IMR, 0x00},	/* Mask completion irq. */
231		{ED_P0_ISR, 0xff},
232		{ED_P0_RCR, ED_RCR_MON | ED_RCR_INTT},	/* Set To Monitor */
233		{ED_P0_TCR, ED_TCR_LB0},	/* loopback mode. */
234		{ED_P0_RBCR0, 32},
235		{ED_P0_RBCR1, 0x00},
236		{ED_P0_RSAR0, 0x00},
237		{ED_P0_RSAR1, 0x04},
238		{ED_P0_CR ,ED_CR_RD0 | ED_CR_STA},
239	};
240
241	/* Reset Card */
242	tmp = ed_asic_inb(sc, ED_NOVELL_RESET);
243	ed_asic_outb(sc, ED_NOVELL_RESET, tmp);
244	DELAY(5000);
245	ed_asic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STP);
246	DELAY(5000);
247
248	/* Card Settings */
249	for (i = 0; i < sizeof(pg_seq) / sizeof(pg_seq[0]); i++)
250		ed_nic_outb(sc, pg_seq[i].offset, pg_seq[i].value);
251
252	/* Get Data */
253	for (i = 0; i < 16; i++)
254		prom[i] = ed_asic_inb(sc, 0);
255/*
256	for (i = 0; i < 16; i++)
257		printf("ax88190 eprom [%02d] %02x %02x\n",
258			i,prom[i] & 0xff,prom[i] >> 8);
259*/
260	sc->arpcom.ac_enaddr[0] = prom[0] & 0xff;
261	sc->arpcom.ac_enaddr[1] = prom[0] >> 8;
262	sc->arpcom.ac_enaddr[2] = prom[1] & 0xff;
263	sc->arpcom.ac_enaddr[3] = prom[1] >> 8;
264	sc->arpcom.ac_enaddr[4] = prom[2] & 0xff;
265	sc->arpcom.ac_enaddr[5] = prom[2] >> 8;
266}
267
268/* XXX: Warner-san, any plan to provide access to the attribute memory? */
269static int
270ed_pccard_memwrite(device_t dev, off_t offset, u_char byte)
271{
272	struct pccard_devinfo *devi;
273	dev_t d;
274	struct iovec iov;
275	struct uio uios;
276
277	devi = device_get_ivars(dev);
278
279	iov.iov_base = &byte;
280	iov.iov_len = sizeof(byte);
281
282	uios.uio_iov = &iov;
283	uios.uio_iovcnt = 1;
284	uios.uio_offset = offset;
285	uios.uio_resid = sizeof(byte);
286	uios.uio_segflg = UIO_SYSSPACE;
287	uios.uio_rw = UIO_WRITE;
288	uios.uio_procp = 0;
289
290	d = makedev(CARD_MAJOR, devi->slt->slotnum);
291	return devsw(d)->d_write(d, &uios, 0);
292}
293
294static int
295ed_pccard_memread(device_t dev, off_t offset, u_char *buf, int size)
296{
297	struct pccard_devinfo *devi;
298	dev_t d;
299	struct iovec iov;
300	struct uio uios;
301
302	devi = device_get_ivars(dev);
303
304	iov.iov_base = buf;
305	iov.iov_len = size;
306
307	uios.uio_iov = &iov;
308	uios.uio_iovcnt = 1;
309	uios.uio_offset = offset;
310	uios.uio_resid = size;
311	uios.uio_segflg = UIO_SYSSPACE;
312	uios.uio_rw = UIO_READ;
313	uios.uio_procp = 0;
314
315	d = makedev(CARD_MAJOR, devi->slt->slotnum);
316	return devsw(d)->d_read(d, &uios, 0);
317}
318