]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ed/if_ed_pccard.c
Minor newbus/style(9) cleanups.
[FreeBSD/FreeBSD.git] / sys / dev / 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$
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
38 #include <sys/module.h>
39 #include <sys/bus.h>
40 #include <machine/bus.h>
41 #include <sys/rman.h>
42 #include <machine/resource.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
49 #include <dev/ed/if_edreg.h>
50 #include <dev/ed/if_edvar.h>
51 #include <dev/pccard/pccardvar.h>
52 #include <dev/pccard/pccarddevs.h>
53
54 #include "card_if.h"
55
56 /*
57  *      PC-Card (PCMCIA) specific code.
58  */
59 static int      ed_pccard_match(device_t);
60 static int      ed_pccard_probe(device_t);
61 static int      ed_pccard_attach(device_t);
62 static int      ed_pccard_detach(device_t);
63
64 static int      ed_pccard_Linksys(device_t dev);
65 static int      ed_pccard_ax88190(device_t dev);
66
67 static void     ax88190_geteprom(struct ed_softc *);
68 static int      ed_pccard_memwrite(device_t dev, off_t offset, u_char byte);
69 static int      linksys;
70
71 /*
72  *      ed_pccard_detach - unload the driver and clear the table.
73  *      XXX TODO:
74  *      This is usually called when the card is ejected, but
75  *      can be caused by a modunload of a controller driver.
76  *      The idea is to reset the driver's view of the device
77  *      and ensure that any driver entry points such as
78  *      read and write do not hang.
79  */
80 static int
81 ed_pccard_detach(device_t dev)
82 {
83         struct ed_softc *sc = device_get_softc(dev);
84         struct ifnet *ifp = &sc->arpcom.ac_if;
85
86         if (sc->gone) {
87                 device_printf(dev, "already unloaded\n");
88                 return (0);
89         }
90         ed_stop(sc);
91         ifp->if_flags &= ~IFF_RUNNING;
92         ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
93         sc->gone = 1;
94         bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
95         ed_release_resources(dev);
96         return (0);
97 }
98
99 static const struct pccard_product ed_pccard_products[] = {
100         { PCCARD_STR_KINGSTON_KNE2,             PCCARD_VENDOR_KINGSTON,
101           PCCARD_PRODUCT_KINGSTON_KNE2,         0, NULL, NULL },
102         { NULL }
103 };
104
105 static int
106 ed_pccard_match(device_t dev)
107 {
108         const struct pccard_product *pp;
109
110         if ((pp = pccard_product_lookup(dev, ed_pccard_products,
111             sizeof(ed_pccard_products[0]), NULL)) != NULL) {
112                 device_set_desc(dev, pp->pp_name);
113                 return 0;
114         }
115         return EIO;
116 }
117
118 /* 
119  * Probe framework for pccards.  Replicates the standard framework,
120  * minus the pccard driver registration and ignores the ether address
121  * supplied (from the CIS), relying on the probe to find it instead.
122  */
123 static int
124 ed_pccard_probe(device_t dev)
125 {
126         int     error;
127         int     flags = device_get_flags(dev);
128
129         if (ED_FLAGS_GETTYPE(flags) == ED_FLAGS_AX88190) {
130                 error = ed_pccard_ax88190(dev);
131                 goto end2;
132         }
133
134         error = ed_probe_Novell(dev, 0, flags);
135         if (error == 0)
136                 goto end;
137         ed_release_resources(dev);
138
139         error = ed_probe_WD80x3(dev, 0, flags);
140         if (error == 0)
141                 goto end;
142         ed_release_resources(dev);
143         goto end2;
144
145 end:
146         if (ED_FLAGS_GETTYPE(flags) & ED_FLAGS_LINKSYS) {
147                 linksys = ed_pccard_Linksys(dev);
148         } else {
149                 linksys = 0;
150         }
151 end2:
152         if (error == 0)
153                 error = ed_alloc_irq(dev, 0, 0);
154
155         ed_release_resources(dev);
156         return (error);
157 }
158
159 static int
160 ed_pccard_attach(device_t dev)
161 {
162         int error;
163         int     flags = device_get_flags(dev);
164         int i;
165         struct ed_softc *sc = device_get_softc(dev);
166         u_char sum;
167         u_char ether_addr[ETHER_ADDR_LEN];
168         
169         if (sc->port_used > 0)
170                 ed_alloc_port(dev, sc->port_rid, sc->port_used);
171         if (sc->mem_used)
172                 ed_alloc_memory(dev, sc->mem_rid, sc->mem_used);
173         ed_alloc_irq(dev, sc->irq_rid, 0);
174                 
175         error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET,
176                                edintr, sc, &sc->irq_handle);
177         if (error) {
178                 printf("setup intr failed %d \n", error);
179                 ed_release_resources(dev);
180                 return (error);
181         }             
182
183         if (linksys == 0) {
184                 pccard_get_ether(dev, ether_addr);
185                 for (i = 0, sum = 0; i < ETHER_ADDR_LEN; i++)
186                         sum |= ether_addr[i];
187                 if (sum)
188                         bcopy(ether_addr, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
189         }
190
191         error = ed_attach(sc, device_get_unit(dev), flags);
192         return (error);
193 }
194
195 static void
196 ax88190_geteprom(struct ed_softc *sc)
197 {
198         int prom[16],i;
199         u_char tmp;
200         struct {
201                 unsigned char offset, value;
202         } pg_seq[] = {
203                 {ED_P0_CR, ED_CR_RD2|ED_CR_STP},/* Select Page0 */
204                 {ED_P0_DCR, 0x01},
205                 {ED_P0_RBCR0, 0x00},            /* Clear the count regs. */
206                 {ED_P0_RBCR1, 0x00},
207                 {ED_P0_IMR, 0x00},              /* Mask completion irq. */
208                 {ED_P0_ISR, 0xff},
209                 {ED_P0_RCR, ED_RCR_MON | ED_RCR_INTT}, /* Set To Monitor */
210                 {ED_P0_TCR, ED_TCR_LB0},        /* loopback mode. */
211                 {ED_P0_RBCR0, 32},
212                 {ED_P0_RBCR1, 0x00},
213                 {ED_P0_RSAR0, 0x00},
214                 {ED_P0_RSAR1, 0x04},
215                 {ED_P0_CR ,ED_CR_RD0 | ED_CR_STA},
216         };
217
218         /* Reset Card */
219         tmp = ed_asic_inb(sc, ED_NOVELL_RESET);
220         ed_asic_outb(sc, ED_NOVELL_RESET, tmp);
221         DELAY(5000);
222         ed_asic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STP);
223         DELAY(5000);
224
225         /* Card Settings */
226         for (i = 0; i < sizeof(pg_seq) / sizeof(pg_seq[0]); i++)
227                 ed_nic_outb(sc, pg_seq[i].offset, pg_seq[i].value);
228
229         /* Get Data */
230         for (i = 0; i < 16; i++)
231                 prom[i] = ed_asic_inb(sc, 0);
232         sc->arpcom.ac_enaddr[0] = prom[0] & 0xff;
233         sc->arpcom.ac_enaddr[1] = prom[0] >> 8;
234         sc->arpcom.ac_enaddr[2] = prom[1] & 0xff;
235         sc->arpcom.ac_enaddr[3] = prom[1] >> 8;
236         sc->arpcom.ac_enaddr[4] = prom[2] & 0xff;
237         sc->arpcom.ac_enaddr[5] = prom[2] >> 8;
238 }
239
240 static int
241 ed_pccard_memwrite(device_t dev, off_t offset, u_char byte)
242 {
243         int cis_rid;
244         struct resource *cis;
245
246         cis_rid = 0;
247         cis = bus_alloc_resource(dev, SYS_RES_MEMORY, &cis_rid, 0, ~0, 
248             4 << 10, RF_ACTIVE | RF_SHAREABLE);
249         if (cis == NULL)
250                 return (ENXIO);
251         CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY,
252             cis_rid, PCCARD_A_MEM_ATTR);
253
254         bus_space_write_1(rman_get_bustag(cis), rman_get_bushandle(cis),
255             offset, byte);
256
257         bus_deactivate_resource(dev, SYS_RES_MEMORY, cis_rid, cis);
258         bus_release_resource(dev, SYS_RES_MEMORY, cis_rid, cis);
259
260         return (0);
261 }
262
263 /*
264  * Probe the Ethernet MAC addrees for PCMCIA Linksys EtherFast 10/100 
265  * and compatible cards (DL10019C Ethernet controller).
266  *
267  * Note: The PAO patches try to use more memory for the card, but that
268  * seems to fail for my card.  A future optimization would add this back
269  * conditionally.
270  */
271 static int
272 ed_pccard_Linksys(device_t dev)
273 {
274         struct ed_softc *sc = device_get_softc(dev);
275         u_char sum;
276         int i;
277
278         /*
279          * Linksys registers(offset from ASIC base)
280          *
281          * 0x04-0x09 : Physical Address Register 0-5 (PAR0-PAR5)
282          * 0x0A      : Card ID Register (CIR)
283          * 0x0B      : Check Sum Register (SR)
284          */
285         for (sum = 0, i = 0x04; i < 0x0c; i++)
286                 sum += ed_asic_inb(sc, i);
287         if (sum != 0xff)
288                 return (0);             /* invalid DL10019C */
289         for (i = 0; i < ETHER_ADDR_LEN; i++) {
290                 sc->arpcom.ac_enaddr[i] = ed_asic_inb(sc, 0x04 + i);
291         }
292
293         ed_nic_outb(sc, ED_P0_DCR, ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS);
294         sc->isa16bit = 1;
295         sc->type = ED_TYPE_NE2000;
296         sc->type_str = "Linksys";
297         return (1);
298 }
299
300 /*
301  * Special setup for AX88190
302  */
303 static int
304 ed_pccard_ax88190(device_t dev)
305 {
306         int     error;
307         int     flags = device_get_flags(dev);
308         int     iobase;
309         struct  ed_softc *sc = device_get_softc(dev);
310
311         /* Allocate the port resource during setup. */
312         error = ed_alloc_port(dev, 0, ED_NOVELL_IO_PORTS);
313         if (error)
314                 return (error);
315
316         sc->asic_offset = ED_NOVELL_ASIC_OFFSET;
317         sc->nic_offset  = ED_NOVELL_NIC_OFFSET;
318         sc->chip_type = ED_CHIP_TYPE_AX88190;
319
320         /*
321          * Set Attribute Memory IOBASE Register
322          */
323         iobase = rman_get_start(sc->port_res);
324         ed_pccard_memwrite(dev, ED_AX88190_IOBASE0, iobase & 0xff);
325         ed_pccard_memwrite(dev, ED_AX88190_IOBASE1, (iobase >> 8) & 0xff);
326         ax88190_geteprom(sc);
327         ed_release_resources(dev);
328         error = ed_probe_Novell(dev, 0, flags);
329         return (error);
330 }
331
332 static device_method_t ed_pccard_methods[] = {
333         /* Device interface */
334         DEVMETHOD(device_probe,         pccard_compat_probe),
335         DEVMETHOD(device_attach,        pccard_compat_attach),
336         DEVMETHOD(device_detach,        ed_pccard_detach),
337
338         /* Card interface */
339         DEVMETHOD(card_compat_match,    ed_pccard_match),
340         DEVMETHOD(card_compat_probe,    ed_pccard_probe),
341         DEVMETHOD(card_compat_attach,   ed_pccard_attach),
342         { 0, 0 }
343 };
344
345 static driver_t ed_pccard_driver = {
346         "ed",
347         ed_pccard_methods,
348         sizeof(struct ed_softc)
349 };
350
351 DRIVER_MODULE(if_ed, pccard, ed_pccard_driver, ed_devclass, 0, 0);