2 * Copyright (c) 1995, David Greenman
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
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.
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
27 * $FreeBSD: src/sys/dev/ed/if_ed_pccard.c,v 1.9.2.6 2001/07/25 18:06:01 iedowse Exp $
28 * $DragonFly: src/sys/dev/netif/ed/if_ed_pccard.c,v 1.4 2003/07/22 17:03:28 dillon Exp $
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/socket.h>
34 #include <sys/kernel.h>
37 #include <sys/select.h>
38 #include <sys/device.h>
39 #include <machine/clock.h>
41 #include <sys/module.h>
43 #include <machine/bus.h>
45 #include <net/ethernet.h>
47 #include <net/if_arp.h>
48 #include <net/if_mib.h>
49 #include <net/if_media.h>
51 #include <dev/ed/if_edreg.h>
52 #include <dev/ed/if_edvar.h>
53 #include <dev/pccard/pccardvar.h>
54 #include <pccard/cardinfo.h>
55 #include <pccard/slot.h>
56 #include <dev/mii/mii.h>
57 #include <dev/mii/miivar.h>
59 /* "device miibus" required. See GENERIC if you get errors here. */
60 #include "miibus_if.h"
65 * PC-Card (PCMCIA) specific code.
67 static int ed_pccard_probe(device_t);
68 static int ed_pccard_attach(device_t);
69 static int ed_pccard_detach(device_t);
71 static void ax88190_geteprom(struct ed_softc *);
72 static int ed_pccard_memwrite(device_t dev, off_t offset, u_char byte);
73 static int ed_pccard_memread(device_t dev, off_t offset, u_char *buf, int size);
74 static int ed_pccard_Linksys(device_t dev);
75 static void ed_pccard_dlink_mii_reset(struct ed_softc *sc);
76 static u_int ed_pccard_dlink_mii_readbits(struct ed_softc *sc, int nbits);
77 static void ed_pccard_dlink_mii_writebits(struct ed_softc *sc, u_int val,
81 static device_method_t ed_pccard_methods[] = {
82 /* Device interface */
83 DEVMETHOD(device_probe, ed_pccard_probe),
84 DEVMETHOD(device_attach, ed_pccard_attach),
85 DEVMETHOD(device_detach, ed_pccard_detach),
88 DEVMETHOD(bus_child_detached, ed_child_detached),
91 DEVMETHOD(miibus_readreg, ed_miibus_readreg),
92 DEVMETHOD(miibus_writereg, ed_miibus_writereg),
96 static driver_t ed_pccard_driver = {
99 sizeof(struct ed_softc)
102 static devclass_t ed_pccard_devclass;
104 DRIVER_MODULE(ed, pccard, ed_pccard_driver, ed_pccard_devclass, 0, 0);
105 DRIVER_MODULE(miibus, ed, miibus_driver, miibus_devclass, 0, 0);
108 * ed_pccard_detach - unload the driver and clear the table.
110 * This is usually called when the card is ejected, but
111 * can be caused by a modunload of a controller driver.
112 * The idea is to reset the driver's view of the device
113 * and ensure that any driver entry points such as
114 * read and write do not hang.
117 ed_pccard_detach(device_t dev)
119 struct ed_softc *sc = device_get_softc(dev);
120 struct ifnet *ifp = &sc->arpcom.ac_if;
123 device_printf(dev, "already unloaded\n");
127 ifp->if_flags &= ~IFF_RUNNING;
128 ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
130 bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
131 ed_release_resources(dev);
136 * Probe framework for pccards. Replicates the standard framework,
137 * minus the pccard driver registration and ignores the ether address
138 * supplied (from the CIS), relying on the probe to find it instead.
141 ed_pccard_probe(device_t dev)
143 struct ed_softc *sc = device_get_softc(dev);
144 int flags = device_get_flags(dev);
147 if (ED_FLAGS_GETTYPE(flags) == ED_FLAGS_AX88190) {
148 /* Special setup for AX88190 */
153 /* XXX Allocate the port resource during setup. */
154 error = ed_alloc_port(dev, 0, ED_NOVELL_IO_PORTS);
158 sc->asic_offset = ED_NOVELL_ASIC_OFFSET;
159 sc->nic_offset = ED_NOVELL_NIC_OFFSET;
161 sc->chip_type = ED_CHIP_TYPE_AX88190;
164 * Check & Set Attribute Memory IOBASE Register
166 ed_pccard_memread(dev, ED_AX88190_IOBASE0, rdbuf, 4);
167 attr_ioport = rdbuf[2] << 8 | rdbuf[0];
168 iobase = rman_get_start(sc->port_res);
169 if (attr_ioport != iobase) {
171 printf("AX88190 IOBASE MISMATCH %04x -> %04x Setting\n", attr_ioport, iobase);
173 ed_pccard_memwrite(dev, ED_AX88190_IOBASE0,
175 ed_pccard_memwrite(dev, ED_AX88190_IOBASE1,
176 (iobase >> 8) & 0xff);
178 ax88190_geteprom(sc);
180 ed_release_resources(dev);
183 error = ed_probe_Novell(dev, 0, flags);
186 ed_release_resources(dev);
188 error = ed_probe_WD80x3(dev, 0, flags);
191 ed_release_resources(dev);
195 if (ED_FLAGS_GETTYPE(flags) & ED_FLAGS_LINKSYS) {
196 linksys = ed_pccard_Linksys(dev);
202 error = ed_alloc_irq(dev, 0, 0);
204 ed_release_resources(dev);
209 ed_pccard_attach(device_t dev)
211 struct ed_softc *sc = device_get_softc(dev);
212 int flags = device_get_flags(dev);
216 u_char ether_addr[ETHER_ADDR_LEN];
218 if (sc->port_used > 0)
219 ed_alloc_port(dev, sc->port_rid, sc->port_used);
221 ed_alloc_memory(dev, sc->mem_rid, sc->mem_used);
222 ed_alloc_irq(dev, sc->irq_rid, 0);
224 error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET,
225 edintr, sc, &sc->irq_handle);
227 printf("setup intr failed %d \n", error);
228 ed_release_resources(dev);
233 pccard_get_ether(dev, ether_addr);
234 for (i = 0, sum = 0; i < ETHER_ADDR_LEN; i++)
235 sum |= ether_addr[i];
237 bcopy(ether_addr, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
240 error = ed_attach(sc, device_get_unit(dev), flags);
241 if (error == 0 && sc->vendor == ED_VENDOR_LINKSYS) {
242 /* Probe for an MII bus, but ignore errors. */
243 ed_pccard_dlink_mii_reset(sc);
244 sc->mii_readbits = ed_pccard_dlink_mii_readbits;
245 sc->mii_writebits = ed_pccard_dlink_mii_writebits;
246 mii_phy_probe(dev, &sc->miibus, ed_ifmedia_upd,
254 ax88190_geteprom(struct ed_softc *sc)
259 unsigned char offset, value;
261 {ED_P0_CR, ED_CR_RD2|ED_CR_STP}, /* Select Page0 */
263 {ED_P0_RBCR0, 0x00}, /* Clear the count regs. */
265 {ED_P0_IMR, 0x00}, /* Mask completion irq. */
267 {ED_P0_RCR, ED_RCR_MON | ED_RCR_INTT}, /* Set To Monitor */
268 {ED_P0_TCR, ED_TCR_LB0}, /* loopback mode. */
273 {ED_P0_CR ,ED_CR_RD0 | ED_CR_STA},
277 tmp = ed_asic_inb(sc, ED_NOVELL_RESET);
278 ed_asic_outb(sc, ED_NOVELL_RESET, tmp);
280 ed_asic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STP);
284 for (i = 0; i < sizeof(pg_seq) / sizeof(pg_seq[0]); i++)
285 ed_nic_outb(sc, pg_seq[i].offset, pg_seq[i].value);
288 for (i = 0; i < 16; i++)
289 prom[i] = ed_asic_inb(sc, 0);
291 for (i = 0; i < 16; i++)
292 printf("ax88190 eprom [%02d] %02x %02x\n",
293 i,prom[i] & 0xff,prom[i] >> 8);
295 sc->arpcom.ac_enaddr[0] = prom[0] & 0xff;
296 sc->arpcom.ac_enaddr[1] = prom[0] >> 8;
297 sc->arpcom.ac_enaddr[2] = prom[1] & 0xff;
298 sc->arpcom.ac_enaddr[3] = prom[1] >> 8;
299 sc->arpcom.ac_enaddr[4] = prom[2] & 0xff;
300 sc->arpcom.ac_enaddr[5] = prom[2] >> 8;
303 /* XXX: Warner-san, any plan to provide access to the attribute memory? */
305 ed_pccard_memwrite(device_t dev, off_t offset, u_char byte)
307 struct pccard_devinfo *devi;
312 devi = device_get_ivars(dev);
314 iov.iov_base = &byte;
315 iov.iov_len = sizeof(byte);
319 uios.uio_offset = offset;
320 uios.uio_resid = sizeof(byte);
321 uios.uio_segflg = UIO_SYSSPACE;
322 uios.uio_rw = UIO_WRITE;
325 d = makedev(CARD_MAJOR, devi->slt->slotnum);
326 return (dev_dwrite(d, &uios, 0));
330 ed_pccard_memread(device_t dev, off_t offset, u_char *buf, int size)
332 struct pccard_devinfo *devi;
337 devi = device_get_ivars(dev);
344 uios.uio_offset = offset;
345 uios.uio_resid = size;
346 uios.uio_segflg = UIO_SYSSPACE;
347 uios.uio_rw = UIO_READ;
350 d = makedev(CARD_MAJOR, devi->slt->slotnum);
351 return (dev_dread(d, &uios, 0));
355 * Probe the Ethernet MAC addrees for PCMCIA Linksys EtherFast 10/100
356 * and compatible cards (DL10019C Ethernet controller).
358 * Note: The PAO patches try to use more memory for the card, but that
359 * seems to fail for my card. A future optimization would add this back
363 ed_pccard_Linksys(device_t dev)
365 struct ed_softc *sc = device_get_softc(dev);
370 * Linksys registers(offset from ASIC base)
372 * 0x04-0x09 : Physical Address Register 0-5 (PAR0-PAR5)
373 * 0x0A : Card ID Register (CIR)
374 * 0x0B : Check Sum Register (SR)
376 for (sum = 0, i = 0x04; i < 0x0c; i++)
377 sum += ed_asic_inb(sc, i);
379 return (0); /* invalid DL10019C */
380 for (i = 0; i < ETHER_ADDR_LEN; i++) {
381 sc->arpcom.ac_enaddr[i] = ed_asic_inb(sc, 0x04 + i);
384 ed_nic_outb(sc, ED_P0_DCR, ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS);
386 sc->vendor = ED_VENDOR_LINKSYS;
387 sc->type = ED_TYPE_NE2000;
388 sc->type_str = "Linksys";
393 /* MII bit-twiddling routines for cards using Dlink chipset */
394 #define DLINK_MIISET(sc, x) ed_asic_outb(sc, ED_DLINK_MIIBUS, \
395 ed_asic_inb(sc, ED_DLINK_MIIBUS) | (x))
396 #define DLINK_MIICLR(sc, x) ed_asic_outb(sc, ED_DLINK_MIIBUS, \
397 ed_asic_inb(sc, ED_DLINK_MIIBUS) & ~(x))
400 ed_pccard_dlink_mii_reset(sc)
403 ed_asic_outb(sc, ED_DLINK_MIIBUS, 0);
405 DLINK_MIISET(sc, ED_DLINK_MII_RESET2);
407 DLINK_MIISET(sc, ED_DLINK_MII_RESET1);
409 DLINK_MIICLR(sc, ED_DLINK_MII_RESET1);
411 DLINK_MIICLR(sc, ED_DLINK_MII_RESET2);
416 ed_pccard_dlink_mii_writebits(sc, val, nbits)
423 DLINK_MIISET(sc, ED_DLINK_MII_DIROUT);
425 for (i = nbits - 1; i >= 0; i--) {
427 DLINK_MIISET(sc, ED_DLINK_MII_DATAOUT);
429 DLINK_MIICLR(sc, ED_DLINK_MII_DATAOUT);
431 DLINK_MIISET(sc, ED_DLINK_MII_CLK);
433 DLINK_MIICLR(sc, ED_DLINK_MII_CLK);
439 ed_pccard_dlink_mii_readbits(sc, nbits)
446 DLINK_MIICLR(sc, ED_DLINK_MII_DIROUT);
448 for (i = nbits - 1; i >= 0; i--) {
449 DLINK_MIISET(sc, ED_DLINK_MII_CLK);
452 if (ed_asic_inb(sc, ED_DLINK_MIIBUS) & ED_DLINK_MII_DATATIN)
454 DLINK_MIICLR(sc, ED_DLINK_MII_CLK);