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.2 2003/06/17 04:28:24 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 <machine/clock.h>
40 #include <sys/module.h>
42 #include <machine/bus.h>
44 #include <net/ethernet.h>
46 #include <net/if_arp.h>
47 #include <net/if_mib.h>
48 #include <net/if_media.h>
50 #include <dev/ed/if_edreg.h>
51 #include <dev/ed/if_edvar.h>
52 #include <dev/pccard/pccardvar.h>
53 #include <pccard/cardinfo.h>
54 #include <pccard/slot.h>
55 #include <dev/mii/mii.h>
56 #include <dev/mii/miivar.h>
58 /* "device miibus" required. See GENERIC if you get errors here. */
59 #include "miibus_if.h"
64 * PC-Card (PCMCIA) specific code.
66 static int ed_pccard_probe(device_t);
67 static int ed_pccard_attach(device_t);
68 static int ed_pccard_detach(device_t);
70 static void ax88190_geteprom(struct ed_softc *);
71 static int ed_pccard_memwrite(device_t dev, off_t offset, u_char byte);
72 static int ed_pccard_memread(device_t dev, off_t offset, u_char *buf, int size);
73 static int ed_pccard_Linksys(device_t dev);
74 static void ed_pccard_dlink_mii_reset(struct ed_softc *sc);
75 static u_int ed_pccard_dlink_mii_readbits(struct ed_softc *sc, int nbits);
76 static void ed_pccard_dlink_mii_writebits(struct ed_softc *sc, u_int val,
80 static device_method_t ed_pccard_methods[] = {
81 /* Device interface */
82 DEVMETHOD(device_probe, ed_pccard_probe),
83 DEVMETHOD(device_attach, ed_pccard_attach),
84 DEVMETHOD(device_detach, ed_pccard_detach),
87 DEVMETHOD(bus_child_detached, ed_child_detached),
90 DEVMETHOD(miibus_readreg, ed_miibus_readreg),
91 DEVMETHOD(miibus_writereg, ed_miibus_writereg),
95 static driver_t ed_pccard_driver = {
98 sizeof(struct ed_softc)
101 static devclass_t ed_pccard_devclass;
103 DRIVER_MODULE(ed, pccard, ed_pccard_driver, ed_pccard_devclass, 0, 0);
104 DRIVER_MODULE(miibus, ed, miibus_driver, miibus_devclass, 0, 0);
107 * ed_pccard_detach - unload the driver and clear the table.
109 * This is usually called when the card is ejected, but
110 * can be caused by a modunload of a controller driver.
111 * The idea is to reset the driver's view of the device
112 * and ensure that any driver entry points such as
113 * read and write do not hang.
116 ed_pccard_detach(device_t dev)
118 struct ed_softc *sc = device_get_softc(dev);
119 struct ifnet *ifp = &sc->arpcom.ac_if;
122 device_printf(dev, "already unloaded\n");
126 ifp->if_flags &= ~IFF_RUNNING;
127 ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
129 bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
130 ed_release_resources(dev);
135 * Probe framework for pccards. Replicates the standard framework,
136 * minus the pccard driver registration and ignores the ether address
137 * supplied (from the CIS), relying on the probe to find it instead.
140 ed_pccard_probe(device_t dev)
142 struct ed_softc *sc = device_get_softc(dev);
143 int flags = device_get_flags(dev);
146 if (ED_FLAGS_GETTYPE(flags) == ED_FLAGS_AX88190) {
147 /* Special setup for AX88190 */
152 /* XXX Allocate the port resource during setup. */
153 error = ed_alloc_port(dev, 0, ED_NOVELL_IO_PORTS);
157 sc->asic_offset = ED_NOVELL_ASIC_OFFSET;
158 sc->nic_offset = ED_NOVELL_NIC_OFFSET;
160 sc->chip_type = ED_CHIP_TYPE_AX88190;
163 * Check & Set Attribute Memory IOBASE Register
165 ed_pccard_memread(dev, ED_AX88190_IOBASE0, rdbuf, 4);
166 attr_ioport = rdbuf[2] << 8 | rdbuf[0];
167 iobase = rman_get_start(sc->port_res);
168 if (attr_ioport != iobase) {
170 printf("AX88190 IOBASE MISMATCH %04x -> %04x Setting\n", attr_ioport, iobase);
172 ed_pccard_memwrite(dev, ED_AX88190_IOBASE0,
174 ed_pccard_memwrite(dev, ED_AX88190_IOBASE1,
175 (iobase >> 8) & 0xff);
177 ax88190_geteprom(sc);
179 ed_release_resources(dev);
182 error = ed_probe_Novell(dev, 0, flags);
185 ed_release_resources(dev);
187 error = ed_probe_WD80x3(dev, 0, flags);
190 ed_release_resources(dev);
194 if (ED_FLAGS_GETTYPE(flags) & ED_FLAGS_LINKSYS) {
195 linksys = ed_pccard_Linksys(dev);
201 error = ed_alloc_irq(dev, 0, 0);
203 ed_release_resources(dev);
208 ed_pccard_attach(device_t dev)
210 struct ed_softc *sc = device_get_softc(dev);
211 int flags = device_get_flags(dev);
215 u_char ether_addr[ETHER_ADDR_LEN];
217 if (sc->port_used > 0)
218 ed_alloc_port(dev, sc->port_rid, sc->port_used);
220 ed_alloc_memory(dev, sc->mem_rid, sc->mem_used);
221 ed_alloc_irq(dev, sc->irq_rid, 0);
223 error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET,
224 edintr, sc, &sc->irq_handle);
226 printf("setup intr failed %d \n", error);
227 ed_release_resources(dev);
232 pccard_get_ether(dev, ether_addr);
233 for (i = 0, sum = 0; i < ETHER_ADDR_LEN; i++)
234 sum |= ether_addr[i];
236 bcopy(ether_addr, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
239 error = ed_attach(sc, device_get_unit(dev), flags);
240 if (error == 0 && sc->vendor == ED_VENDOR_LINKSYS) {
241 /* Probe for an MII bus, but ignore errors. */
242 ed_pccard_dlink_mii_reset(sc);
243 sc->mii_readbits = ed_pccard_dlink_mii_readbits;
244 sc->mii_writebits = ed_pccard_dlink_mii_writebits;
245 mii_phy_probe(dev, &sc->miibus, ed_ifmedia_upd,
253 ax88190_geteprom(struct ed_softc *sc)
258 unsigned char offset, value;
260 {ED_P0_CR, ED_CR_RD2|ED_CR_STP}, /* Select Page0 */
262 {ED_P0_RBCR0, 0x00}, /* Clear the count regs. */
264 {ED_P0_IMR, 0x00}, /* Mask completion irq. */
266 {ED_P0_RCR, ED_RCR_MON | ED_RCR_INTT}, /* Set To Monitor */
267 {ED_P0_TCR, ED_TCR_LB0}, /* loopback mode. */
272 {ED_P0_CR ,ED_CR_RD0 | ED_CR_STA},
276 tmp = ed_asic_inb(sc, ED_NOVELL_RESET);
277 ed_asic_outb(sc, ED_NOVELL_RESET, tmp);
279 ed_asic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STP);
283 for (i = 0; i < sizeof(pg_seq) / sizeof(pg_seq[0]); i++)
284 ed_nic_outb(sc, pg_seq[i].offset, pg_seq[i].value);
287 for (i = 0; i < 16; i++)
288 prom[i] = ed_asic_inb(sc, 0);
290 for (i = 0; i < 16; i++)
291 printf("ax88190 eprom [%02d] %02x %02x\n",
292 i,prom[i] & 0xff,prom[i] >> 8);
294 sc->arpcom.ac_enaddr[0] = prom[0] & 0xff;
295 sc->arpcom.ac_enaddr[1] = prom[0] >> 8;
296 sc->arpcom.ac_enaddr[2] = prom[1] & 0xff;
297 sc->arpcom.ac_enaddr[3] = prom[1] >> 8;
298 sc->arpcom.ac_enaddr[4] = prom[2] & 0xff;
299 sc->arpcom.ac_enaddr[5] = prom[2] >> 8;
302 /* XXX: Warner-san, any plan to provide access to the attribute memory? */
304 ed_pccard_memwrite(device_t dev, off_t offset, u_char byte)
306 struct pccard_devinfo *devi;
311 devi = device_get_ivars(dev);
313 iov.iov_base = &byte;
314 iov.iov_len = sizeof(byte);
318 uios.uio_offset = offset;
319 uios.uio_resid = sizeof(byte);
320 uios.uio_segflg = UIO_SYSSPACE;
321 uios.uio_rw = UIO_WRITE;
324 d = makedev(CARD_MAJOR, devi->slt->slotnum);
325 return devsw(d)->d_write(d, &uios, 0);
329 ed_pccard_memread(device_t dev, off_t offset, u_char *buf, int size)
331 struct pccard_devinfo *devi;
336 devi = device_get_ivars(dev);
343 uios.uio_offset = offset;
344 uios.uio_resid = size;
345 uios.uio_segflg = UIO_SYSSPACE;
346 uios.uio_rw = UIO_READ;
349 d = makedev(CARD_MAJOR, devi->slt->slotnum);
350 return devsw(d)->d_read(d, &uios, 0);
354 * Probe the Ethernet MAC addrees for PCMCIA Linksys EtherFast 10/100
355 * and compatible cards (DL10019C Ethernet controller).
357 * Note: The PAO patches try to use more memory for the card, but that
358 * seems to fail for my card. A future optimization would add this back
362 ed_pccard_Linksys(device_t dev)
364 struct ed_softc *sc = device_get_softc(dev);
369 * Linksys registers(offset from ASIC base)
371 * 0x04-0x09 : Physical Address Register 0-5 (PAR0-PAR5)
372 * 0x0A : Card ID Register (CIR)
373 * 0x0B : Check Sum Register (SR)
375 for (sum = 0, i = 0x04; i < 0x0c; i++)
376 sum += ed_asic_inb(sc, i);
378 return (0); /* invalid DL10019C */
379 for (i = 0; i < ETHER_ADDR_LEN; i++) {
380 sc->arpcom.ac_enaddr[i] = ed_asic_inb(sc, 0x04 + i);
383 ed_nic_outb(sc, ED_P0_DCR, ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS);
385 sc->vendor = ED_VENDOR_LINKSYS;
386 sc->type = ED_TYPE_NE2000;
387 sc->type_str = "Linksys";
392 /* MII bit-twiddling routines for cards using Dlink chipset */
393 #define DLINK_MIISET(sc, x) ed_asic_outb(sc, ED_DLINK_MIIBUS, \
394 ed_asic_inb(sc, ED_DLINK_MIIBUS) | (x))
395 #define DLINK_MIICLR(sc, x) ed_asic_outb(sc, ED_DLINK_MIIBUS, \
396 ed_asic_inb(sc, ED_DLINK_MIIBUS) & ~(x))
399 ed_pccard_dlink_mii_reset(sc)
402 ed_asic_outb(sc, ED_DLINK_MIIBUS, 0);
404 DLINK_MIISET(sc, ED_DLINK_MII_RESET2);
406 DLINK_MIISET(sc, ED_DLINK_MII_RESET1);
408 DLINK_MIICLR(sc, ED_DLINK_MII_RESET1);
410 DLINK_MIICLR(sc, ED_DLINK_MII_RESET2);
415 ed_pccard_dlink_mii_writebits(sc, val, nbits)
422 DLINK_MIISET(sc, ED_DLINK_MII_DIROUT);
424 for (i = nbits - 1; i >= 0; i--) {
426 DLINK_MIISET(sc, ED_DLINK_MII_DATAOUT);
428 DLINK_MIICLR(sc, ED_DLINK_MII_DATAOUT);
430 DLINK_MIISET(sc, ED_DLINK_MII_CLK);
432 DLINK_MIICLR(sc, ED_DLINK_MII_CLK);
438 ed_pccard_dlink_mii_readbits(sc, nbits)
445 DLINK_MIICLR(sc, ED_DLINK_MII_DIROUT);
447 for (i = nbits - 1; i >= 0; i--) {
448 DLINK_MIISET(sc, ED_DLINK_MII_CLK);
451 if (ed_asic_inb(sc, ED_DLINK_MIIBUS) & ED_DLINK_MII_DATATIN)
453 DLINK_MIICLR(sc, ED_DLINK_MII_CLK);