8bc5c8080896ec14bf11ac6648ac00d339b16fb6
[dragonfly.git] / sys / dev / netif / ex / if_ex_isa.c
1 /*-
2  * Copyright (c) 2000 Matthew N. Dodd
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, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *      $FreeBSD: src/sys/dev/ex/if_ex_isa.c,v 1.3.2.1 2001/03/05 05:33:20 imp Exp $
27  */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/interrupt.h>
33 #include <sys/socket.h>
34 #include <sys/module.h>
35 #include <sys/bus.h>
36 #include <sys/rman.h>
37 #include <sys/serialize.h>
38 #include <sys/machintr.h>
39
40 #include <machine/clock.h>
41
42 #include <net/if.h>
43 #include <net/if_arp.h>
44 #include <net/if_media.h> 
45
46 #include <bus/isa/isavar.h>
47 #include <bus/isa/pnpvar.h>
48
49 #include "if_exreg.h"
50 #include "if_exvar.h"
51
52 /* Bus Front End Functions */
53 static int      ex_isa_identify (driver_t *, device_t);
54 static int      ex_isa_probe    (device_t);
55 static int      ex_isa_attach   (device_t);
56
57 /*
58  * We need an identify function to 'probe' the ISA bus.
59  */
60 static device_method_t ex_methods[] = {
61         /* Device interface */
62         DEVMETHOD(device_identify,      ex_isa_identify),
63         DEVMETHOD(device_probe,         ex_isa_probe),
64         DEVMETHOD(device_attach,        ex_isa_attach),
65
66         { 0, 0 }
67 };
68
69 static driver_t ex_driver = {
70         "ex",
71         ex_methods,
72         sizeof(struct ex_softc),
73 };
74
75 devclass_t ex_devclass;
76
77 DRIVER_MODULE(if_ex, isa, ex_driver, ex_devclass, NULL, NULL);
78
79 static struct isa_pnp_id ex_ids[] = {
80         { 0x3110d425,   NULL }, /* INT1031 */
81         { 0x3010d425,   NULL }, /* INT1030 */
82         { 0,            NULL },
83 };
84
85 /*
86  * Non-destructive identify.
87  */
88 static int
89 ex_isa_identify (driver_t *driver, device_t parent)
90 {
91         device_t        child;
92         u_int32_t       ioport;
93         u_char          enaddr[6];
94         u_int           irq;
95         int             tmp;
96         int             count;
97         const char *    desc;
98
99         /*
100          * Rescanning ISA I/O ports is not supported.
101          */
102         if (device_get_state(parent) == DS_ATTACHED)
103                 return (0);
104
105         if (bootverbose)
106                 kprintf("ex_isa_identify()\n");
107
108         count = 0;
109         for (ioport = 0x200; ioport < 0x3a0; ioport += 0x10) {
110
111                 /* No board found at address */
112                 if (!look_for_card(ioport)) {
113                         continue;
114                 }
115
116                 if (bootverbose)
117                         kprintf("ex: Found card at 0x%03x!\n", ioport);
118
119                 /* Board in PnP mode */
120                 if (eeprom_read(ioport, EE_W0) & EE_W0_PNP) {
121                         /* Reset the card. */
122                         outb(ioport + CMD_REG, Reset_CMD);
123                         DELAY(500);
124                         if (bootverbose)
125                                 kprintf("ex: card at 0x%03x in PnP mode!\n", ioport);
126                         continue;
127                 }
128
129                 bzero(enaddr, sizeof(enaddr));
130
131                 /* Reset the card. */
132                 outb(ioport + CMD_REG, Reset_CMD);
133                 DELAY(400);
134
135                 ex_get_address(ioport, enaddr);
136                 tmp = eeprom_read(ioport, EE_W1) & EE_W1_INT_SEL;
137
138                 /* work out which set of irq <-> internal tables to use */
139                 if (ex_card_type(enaddr) == CARD_TYPE_EX_10_PLUS) {
140                         irq  = plus_ee2irqmap[tmp];
141                         desc = "Intel Pro/10+";
142                 } else {
143                         irq = ee2irqmap[tmp];
144                         desc = "Intel Pro/10";
145                 }
146
147                 child = BUS_ADD_CHILD(parent, parent,
148                                       ISA_ORDER_SPECULATIVE, "ex", -1);
149                 device_set_desc_copy(child, desc);
150                 device_set_driver(child, driver);
151                 bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1,
152                     machintr_intr_cpuid(irq));
153                 bus_set_resource(child, SYS_RES_IOPORT, 0, ioport, EX_IOSIZE, -1);
154                 ++count;
155
156                 if (bootverbose)
157                         kprintf("ex: Adding board at 0x%03x, irq %d\n", ioport, irq);
158         }
159         return (count ? 0 : ENXIO);
160 }
161
162 static int
163 ex_isa_probe(device_t dev)
164 {
165         u_int           iobase;
166         u_int           irq;
167         u_char *        ee2irq;
168         u_char          enaddr[6];
169         int             tmp;
170         int             error;
171
172         /* Check isapnp ids */
173         error = ISA_PNP_PROBE(device_get_parent(dev), dev, ex_ids);
174
175         /* If the card had a PnP ID that didn't match any we know about */
176         if (error == ENXIO) {
177                 return(error);
178         }
179
180         /* If we had some other problem. */
181         if (!(error == 0 || error == ENOENT)) {
182                 return(error);
183         }
184
185         iobase = bus_get_resource_start(dev, SYS_RES_IOPORT, 0);
186         if (!iobase) {
187                 kprintf("ex: no iobase?\n");
188                 return(ENXIO);
189         }
190
191         if (!look_for_card(iobase)) {
192                 kprintf("ex: no card found at 0x%03x\n", iobase);
193                 return(ENXIO);
194         }
195
196         if (bootverbose)
197                 kprintf("ex: ex_isa_probe() found card at 0x%03x\n", iobase);
198
199         /*
200          * Reset the card.
201          */
202         outb(iobase + CMD_REG, Reset_CMD);
203         DELAY(800);
204
205         ex_get_address(iobase, enaddr);
206
207         /* work out which set of irq <-> internal tables to use */
208         if (ex_card_type(enaddr) == CARD_TYPE_EX_10_PLUS)
209                 ee2irq = plus_ee2irqmap;
210         else
211                 ee2irq = ee2irqmap;
212
213         tmp = eeprom_read(iobase, EE_W1) & EE_W1_INT_SEL;
214         irq = bus_get_resource_start(dev, SYS_RES_IRQ, 0);
215
216         if (irq > 0) {
217                 /* This will happen if board is in PnP mode. */
218                 if (ee2irq[tmp] != irq) {
219                         kprintf("ex: WARNING: board's EEPROM is configured"
220                                 " for IRQ %d, using %d\n",
221                                 ee2irq[tmp], irq);
222                 }
223         } else {
224                 irq = ee2irq[tmp];
225                 bus_set_resource(dev, SYS_RES_IRQ, 0, irq, 1,
226                     machintr_intr_cpuid(irq));
227         }
228
229         if (irq == 0) {
230                 kprintf("ex: invalid IRQ.\n");
231                 return(ENXIO);
232         }
233
234         return(0);
235 }
236
237 static int
238 ex_isa_attach(device_t dev)
239 {
240         struct ex_softc *       sc = device_get_softc(dev);
241         struct ifnet *          ifp = &sc->arpcom.ac_if;
242         int                     error = 0;
243         u_int16_t               temp;
244
245         sc->dev = dev;
246         sc->ioport_rid = 0;
247         sc->irq_rid = 0;
248
249         if ((error = ex_alloc_resources(dev)) != 0) {
250                 device_printf(dev, "ex_alloc_resources() failed!\n");
251                 goto bad;
252         }
253
254         /*
255          * Fill in several fields of the softc structure:
256          *      - I/O base address.
257          *      - Hardware Ethernet address.
258          *      - IRQ number (if not supplied in config file, read it from EEPROM).
259          *      - Connector type.
260          */
261         sc->iobase = rman_get_start(sc->ioport);
262         sc->irq_no = rman_get_start(sc->irq);
263
264         ex_get_address(sc->iobase, sc->arpcom.ac_enaddr);
265
266         temp = eeprom_read(sc->iobase, EE_W0);
267         device_printf(sc->dev, "%s config, %s bus, ",
268                 (temp & EE_W0_PNP) ? "PnP" : "Manual",
269                 (temp & EE_W0_BUS16) ? "16-bit" : "8-bit");
270
271         temp = eeprom_read(sc->iobase, EE_W6);
272         kprintf("board id 0x%03x, stepping 0x%01x\n",
273                 (temp & EE_W6_BOARD_MASK) >> EE_W6_BOARD_SHIFT,
274                 temp & EE_W6_STEP_MASK);
275
276         if ((error = ex_attach(dev)) != 0) {
277                 device_printf(dev, "ex_attach() failed!\n");
278                 goto bad;
279         }
280
281         error = bus_setup_intr(dev, sc->irq, INTR_MPSAFE,
282                                 ex_intr, (void *)sc, &sc->ih, 
283                                 ifp->if_serializer);
284         if (error) {
285                 device_printf(dev, "bus_setup_intr() failed!\n");
286                 goto bad;
287         }
288
289         ifp->if_cpuid = ithread_cpuid(rman_get_start(sc->irq));
290         KKASSERT(ifp->if_cpuid >= 0 && ifp->if_cpuid < ncpus);
291
292         return(0);
293 bad:
294         ex_release_resources(dev);
295         return (error);
296 }