Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / dev / netif / ed / if_ed_pccard.c
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: 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 $
29  */
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/socket.h>
34 #include <sys/kernel.h>
35 #include <sys/conf.h>
36 #include <sys/uio.h>
37 #include <sys/select.h>
38 #include <machine/clock.h>
39
40 #include <sys/module.h>
41 #include <sys/bus.h>
42 #include <machine/bus.h>
43
44 #include <net/ethernet.h>
45 #include <net/if.h>
46 #include <net/if_arp.h>
47 #include <net/if_mib.h>
48 #include <net/if_media.h>
49
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>
57
58 /* "device miibus" required.  See GENERIC if you get errors here. */
59 #include "miibus_if.h"
60
61 #define CARD_MAJOR      50
62
63 /*
64  *      PC-Card (PCMCIA) specific code.
65  */
66 static int      ed_pccard_probe(device_t);
67 static int      ed_pccard_attach(device_t);
68 static int      ed_pccard_detach(device_t);
69
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,
77     int nbits);
78 static int      linksys;
79
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),
85
86         /* Bus interface */
87         DEVMETHOD(bus_child_detached,   ed_child_detached),
88
89         /* MII interface */
90         DEVMETHOD(miibus_readreg,       ed_miibus_readreg),
91         DEVMETHOD(miibus_writereg,      ed_miibus_writereg),
92         { 0, 0 }
93 };
94
95 static driver_t ed_pccard_driver = {
96         "ed",
97         ed_pccard_methods,
98         sizeof(struct ed_softc)
99 };
100
101 static devclass_t ed_pccard_devclass;
102
103 DRIVER_MODULE(ed, pccard, ed_pccard_driver, ed_pccard_devclass, 0, 0);
104 DRIVER_MODULE(miibus, ed, miibus_driver, miibus_devclass, 0, 0);
105
106 /*
107  *      ed_pccard_detach - unload the driver and clear the table.
108  *      XXX TODO:
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.
114  */
115 static int
116 ed_pccard_detach(device_t dev)
117 {
118         struct ed_softc *sc = device_get_softc(dev);
119         struct ifnet *ifp = &sc->arpcom.ac_if;
120
121         if (sc->gone) {
122                 device_printf(dev, "already unloaded\n");
123                 return (0);
124         }
125         ed_stop(sc);
126         ifp->if_flags &= ~IFF_RUNNING;
127         ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
128         sc->gone = 1;
129         bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
130         ed_release_resources(dev);
131         return (0);
132 }
133
134 /* 
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.
138  */
139 static int
140 ed_pccard_probe(device_t dev)
141 {
142         struct  ed_softc *sc = device_get_softc(dev);
143         int     flags = device_get_flags(dev);
144         int     error;
145
146         if (ED_FLAGS_GETTYPE(flags) == ED_FLAGS_AX88190) {
147                 /* Special setup for AX88190 */
148                 u_char rdbuf[4];
149                 int iobase;
150                 int attr_ioport;
151
152                 /* XXX Allocate the port resource during setup. */
153                 error = ed_alloc_port(dev, 0, ED_NOVELL_IO_PORTS);
154                 if (error)
155                         return (error);
156
157                 sc->asic_offset = ED_NOVELL_ASIC_OFFSET;
158                 sc->nic_offset  = ED_NOVELL_NIC_OFFSET;
159
160                 sc->chip_type = ED_CHIP_TYPE_AX88190;
161
162                 /*
163                  * Check & Set Attribute Memory IOBASE Register
164                  */
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) {
169 #if notdef
170                         printf("AX88190 IOBASE MISMATCH %04x -> %04x Setting\n", attr_ioport, iobase);
171 #endif /* notdef */
172                         ed_pccard_memwrite(dev, ED_AX88190_IOBASE0,
173                                            iobase & 0xff);
174                         ed_pccard_memwrite(dev, ED_AX88190_IOBASE1,
175                                            (iobase >> 8) & 0xff);
176                 }
177                 ax88190_geteprom(sc);
178
179                 ed_release_resources(dev);
180         }
181
182         error = ed_probe_Novell(dev, 0, flags);
183         if (error == 0)
184                 goto end;
185         ed_release_resources(dev);
186
187         error = ed_probe_WD80x3(dev, 0, flags);
188         if (error == 0)
189                 goto end;
190         ed_release_resources(dev);
191         goto end2;
192
193 end:
194         if (ED_FLAGS_GETTYPE(flags) & ED_FLAGS_LINKSYS) {
195                 linksys = ed_pccard_Linksys(dev);
196         } else {
197                 linksys = 0;
198         }
199 end2:
200         if (error == 0)
201                 error = ed_alloc_irq(dev, 0, 0);
202
203         ed_release_resources(dev);
204         return (error);
205 }
206
207 static int
208 ed_pccard_attach(device_t dev)
209 {
210         struct ed_softc *sc = device_get_softc(dev);
211         int flags = device_get_flags(dev);
212         int error;
213         int i;
214         u_char sum;
215         u_char ether_addr[ETHER_ADDR_LEN];
216         
217         if (sc->port_used > 0)
218                 ed_alloc_port(dev, sc->port_rid, sc->port_used);
219         if (sc->mem_used)
220                 ed_alloc_memory(dev, sc->mem_rid, sc->mem_used);
221         ed_alloc_irq(dev, sc->irq_rid, 0);
222                 
223         error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET,
224                                edintr, sc, &sc->irq_handle);
225         if (error) {
226                 printf("setup intr failed %d \n", error);
227                 ed_release_resources(dev);
228                 return (error);
229         }             
230
231         if (linksys == 0) {
232                 pccard_get_ether(dev, ether_addr);
233                 for (i = 0, sum = 0; i < ETHER_ADDR_LEN; i++)
234                         sum |= ether_addr[i];
235                 if (sum)
236                         bcopy(ether_addr, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
237         }
238
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,
246                     ed_ifmedia_sts);
247         }
248
249         return (error);
250 }
251
252 static void
253 ax88190_geteprom(struct ed_softc *sc)
254 {
255         int prom[16],i;
256         u_char tmp;
257         struct {
258                 unsigned char offset, value;
259         } pg_seq[] = {
260                 {ED_P0_CR, ED_CR_RD2|ED_CR_STP},        /* Select Page0 */
261                 {ED_P0_DCR, 0x01},
262                 {ED_P0_RBCR0, 0x00},    /* Clear the count regs. */
263                 {ED_P0_RBCR1, 0x00},
264                 {ED_P0_IMR, 0x00},      /* Mask completion irq. */
265                 {ED_P0_ISR, 0xff},
266                 {ED_P0_RCR, ED_RCR_MON | ED_RCR_INTT},  /* Set To Monitor */
267                 {ED_P0_TCR, ED_TCR_LB0},        /* loopback mode. */
268                 {ED_P0_RBCR0, 32},
269                 {ED_P0_RBCR1, 0x00},
270                 {ED_P0_RSAR0, 0x00},
271                 {ED_P0_RSAR1, 0x04},
272                 {ED_P0_CR ,ED_CR_RD0 | ED_CR_STA},
273         };
274
275         /* Reset Card */
276         tmp = ed_asic_inb(sc, ED_NOVELL_RESET);
277         ed_asic_outb(sc, ED_NOVELL_RESET, tmp);
278         DELAY(5000);
279         ed_asic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STP);
280         DELAY(5000);
281
282         /* Card Settings */
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);
285
286         /* Get Data */
287         for (i = 0; i < 16; i++)
288                 prom[i] = ed_asic_inb(sc, 0);
289 /*
290         for (i = 0; i < 16; i++)
291                 printf("ax88190 eprom [%02d] %02x %02x\n",
292                         i,prom[i] & 0xff,prom[i] >> 8);
293 */
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;
300 }
301
302 /* XXX: Warner-san, any plan to provide access to the attribute memory? */
303 static int
304 ed_pccard_memwrite(device_t dev, off_t offset, u_char byte)
305 {
306         struct pccard_devinfo *devi;
307         dev_t d;
308         struct iovec iov;
309         struct uio uios;
310
311         devi = device_get_ivars(dev);
312
313         iov.iov_base = &byte;
314         iov.iov_len = sizeof(byte);
315
316         uios.uio_iov = &iov;
317         uios.uio_iovcnt = 1;
318         uios.uio_offset = offset;
319         uios.uio_resid = sizeof(byte);
320         uios.uio_segflg = UIO_SYSSPACE;
321         uios.uio_rw = UIO_WRITE;
322         uios.uio_procp = 0;
323
324         d = makedev(CARD_MAJOR, devi->slt->slotnum);
325         return devsw(d)->d_write(d, &uios, 0);
326 }
327
328 static int
329 ed_pccard_memread(device_t dev, off_t offset, u_char *buf, int size)
330 {
331         struct pccard_devinfo *devi;
332         dev_t d;
333         struct iovec iov;
334         struct uio uios;
335
336         devi = device_get_ivars(dev);
337
338         iov.iov_base = buf;
339         iov.iov_len = size;
340
341         uios.uio_iov = &iov;
342         uios.uio_iovcnt = 1;
343         uios.uio_offset = offset;
344         uios.uio_resid = size;
345         uios.uio_segflg = UIO_SYSSPACE;
346         uios.uio_rw = UIO_READ;
347         uios.uio_procp = 0;
348
349         d = makedev(CARD_MAJOR, devi->slt->slotnum);
350         return devsw(d)->d_read(d, &uios, 0);
351 }
352
353 /*
354  * Probe the Ethernet MAC addrees for PCMCIA Linksys EtherFast 10/100 
355  * and compatible cards (DL10019C Ethernet controller).
356  *
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
359  * conditionally.
360  */
361 static int
362 ed_pccard_Linksys(device_t dev)
363 {
364         struct ed_softc *sc = device_get_softc(dev);
365         u_char sum;
366         int i;
367
368         /*
369          * Linksys registers(offset from ASIC base)
370          *
371          * 0x04-0x09 : Physical Address Register 0-5 (PAR0-PAR5)
372          * 0x0A      : Card ID Register (CIR)
373          * 0x0B      : Check Sum Register (SR)
374          */
375         for (sum = 0, i = 0x04; i < 0x0c; i++)
376                 sum += ed_asic_inb(sc, i);
377         if (sum != 0xff)
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);
381         }
382
383         ed_nic_outb(sc, ED_P0_DCR, ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS);
384         sc->isa16bit = 1;
385         sc->vendor = ED_VENDOR_LINKSYS;
386         sc->type = ED_TYPE_NE2000;
387         sc->type_str = "Linksys";
388
389         return (1);
390 }
391
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))
397
398 static void
399 ed_pccard_dlink_mii_reset(sc)
400         struct ed_softc *sc;
401 {
402         ed_asic_outb(sc, ED_DLINK_MIIBUS, 0);
403         DELAY(10);
404         DLINK_MIISET(sc, ED_DLINK_MII_RESET2);
405         DELAY(10);
406         DLINK_MIISET(sc, ED_DLINK_MII_RESET1);
407         DELAY(10);
408         DLINK_MIICLR(sc, ED_DLINK_MII_RESET1);
409         DELAY(10);
410         DLINK_MIICLR(sc, ED_DLINK_MII_RESET2);
411         DELAY(10);
412 }
413
414 static void
415 ed_pccard_dlink_mii_writebits(sc, val, nbits)
416         struct ed_softc *sc;
417         u_int val;
418         int nbits;
419 {
420         int i;
421
422         DLINK_MIISET(sc, ED_DLINK_MII_DIROUT);
423
424         for (i = nbits - 1; i >= 0; i--) {
425                 if ((val >> i) & 1)
426                         DLINK_MIISET(sc, ED_DLINK_MII_DATAOUT);
427                 else
428                         DLINK_MIICLR(sc, ED_DLINK_MII_DATAOUT);
429                 DELAY(10);
430                 DLINK_MIISET(sc, ED_DLINK_MII_CLK);
431                 DELAY(10);
432                 DLINK_MIICLR(sc, ED_DLINK_MII_CLK);
433                 DELAY(10);
434         }
435 }
436
437 static u_int
438 ed_pccard_dlink_mii_readbits(sc, nbits)
439         struct ed_softc *sc;
440         int nbits;
441 {
442         int i;
443         u_int val = 0;
444
445         DLINK_MIICLR(sc, ED_DLINK_MII_DIROUT);
446
447         for (i = nbits - 1; i >= 0; i--) {
448                 DLINK_MIISET(sc, ED_DLINK_MII_CLK);
449                 DELAY(10);
450                 val <<= 1;
451                 if (ed_asic_inb(sc, ED_DLINK_MIIBUS) & ED_DLINK_MII_DATATIN)
452                         val++;
453                 DLINK_MIICLR(sc, ED_DLINK_MII_CLK);
454                 DELAY(10);
455         }
456
457         return val;
458 }