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