Cleanup some of the newbus infrastructure.
[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  *      $DragonFly: src/sys/dev/netif/ex/if_ex_isa.c,v 1.9 2005/10/28 03:25:52 dillon Exp $
28  */
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/socket.h>
34
35 #include <sys/module.h>
36 #include <sys/bus.h>
37
38 #include <machine/bus.h>
39 #include <machine/resource.h>
40 #include <machine/clock.h>
41 #include <sys/rman.h>
42
43 #include <net/if.h>
44 #include <net/if_arp.h>
45 #include <net/if_media.h> 
46
47
48 #include <bus/isa/isavar.h>
49 #include <bus/isa/pnpvar.h>
50
51 #include "if_exreg.h"
52 #include "if_exvar.h"
53
54 /* Bus Front End Functions */
55 static int      ex_isa_identify (driver_t *, device_t);
56 static int      ex_isa_probe    (device_t);
57 static int      ex_isa_attach   (device_t);
58
59 #if 0
60 static  void    ex_pnp_wakeup   (void *);
61
62 SYSINIT(ex_pnpwakeup, SI_SUB_CPU, SI_ORDER_ANY, ex_pnp_wakeup, NULL);
63 #endif
64
65 /*
66  * We need an identify function to 'probe' the ISA bus.
67  */
68 static device_method_t ex_methods[] = {
69         /* Device interface */
70         DEVMETHOD(device_identify,      ex_isa_identify),
71         DEVMETHOD(device_probe,         ex_isa_probe),
72         DEVMETHOD(device_attach,        ex_isa_attach),
73
74         { 0, 0 }
75 };
76
77 static driver_t ex_driver = {
78         "ex",
79         ex_methods,
80         sizeof(struct ex_softc),
81 };
82
83 devclass_t ex_devclass;
84
85 DRIVER_MODULE(if_ex, isa, ex_driver, ex_devclass, 0, 0);
86
87 static struct isa_pnp_id ex_ids[] = {
88         { 0x3110d425,   NULL }, /* INT1031 */
89         { 0x3010d425,   NULL }, /* INT1030 */
90         { 0,            NULL },
91 };
92
93 #if 0
94 #define EX_PNP_WAKE             0x279
95
96 static u_int8_t ex_pnp_wake_seq[] =
97                         { 0x6A, 0xB5, 0xDA, 0xED, 0xF6, 0xFB, 0x7D, 0xBE,
98                           0xDF, 0x6F, 0x37, 0x1B, 0x0D, 0x86, 0xC3, 0x61,
99                           0xB0, 0x58, 0x2C, 0x16, 0x8B, 0x45, 0xA2, 0xD1,
100                           0xE8, 0x74, 0x3A, 0x9D, 0xCE, 0xE7, 0x73, 0x43 };
101
102 static void
103 ex_pnp_wakeup (void * dummy)
104 {
105         int     tmp;
106
107         if (bootverbose)
108                 printf("ex_pnp_wakeup()\n");
109
110         outb(EX_PNP_WAKE, 0);
111         outb(EX_PNP_WAKE, 0);
112         for (tmp = 0; tmp < 32; tmp++) {
113                 outb(EX_PNP_WAKE, ex_pnp_wake_seq[tmp]);
114         }
115 }
116 #endif
117
118 /*
119  * Non-destructive identify.
120  */
121 static int
122 ex_isa_identify (driver_t *driver, device_t parent)
123 {
124         device_t        child;
125         u_int32_t       ioport;
126         u_char          enaddr[6];
127         u_int           irq;
128         int             tmp;
129         int             count;
130         const char *    desc;
131
132         /*
133          * Rescanning ISA I/O ports is not supported.
134          */
135         if (device_get_state(parent) == DS_ATTACHED)
136                 return (0);
137
138         if (bootverbose)
139                 printf("ex_isa_identify()\n");
140
141         count = 0;
142         for (ioport = 0x200; ioport < 0x3a0; ioport += 0x10) {
143
144                 /* No board found at address */
145                 if (!look_for_card(ioport)) {
146                         continue;
147                 }
148
149                 if (bootverbose)
150                         printf("ex: Found card at 0x%03x!\n", ioport);
151
152                 /* Board in PnP mode */
153                 if (eeprom_read(ioport, EE_W0) & EE_W0_PNP) {
154                         /* Reset the card. */
155                         outb(ioport + CMD_REG, Reset_CMD);
156                         DELAY(500);
157                         if (bootverbose)
158                                 printf("ex: card at 0x%03x in PnP mode!\n", ioport);
159                         continue;
160                 }
161
162                 bzero(enaddr, sizeof(enaddr));
163
164                 /* Reset the card. */
165                 outb(ioport + CMD_REG, Reset_CMD);
166                 DELAY(400);
167
168                 ex_get_address(ioport, enaddr);
169                 tmp = eeprom_read(ioport, EE_W1) & EE_W1_INT_SEL;
170
171                 /* work out which set of irq <-> internal tables to use */
172                 if (ex_card_type(enaddr) == CARD_TYPE_EX_10_PLUS) {
173                         irq  = plus_ee2irqmap[tmp];
174                         desc = "Intel Pro/10+";
175                 } else {
176                         irq = ee2irqmap[tmp];
177                         desc = "Intel Pro/10";
178                 }
179
180                 child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "ex", -1);
181                 device_set_desc_copy(child, desc);
182                 device_set_driver(child, driver);
183                 bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
184                 bus_set_resource(child, SYS_RES_IOPORT, 0, ioport, EX_IOSIZE);
185                 ++count;
186
187                 if (bootverbose)
188                         printf("ex: Adding board at 0x%03x, irq %d\n", ioport, irq);
189         }
190         return (count ? 0 : ENXIO);
191 }
192
193 static int
194 ex_isa_probe(device_t dev)
195 {
196         u_int           iobase;
197         u_int           irq;
198         char *          irq2ee;
199         u_char *        ee2irq;
200         u_char          enaddr[6];
201         int             tmp;
202         int             error;
203
204         /* Check isapnp ids */
205         error = ISA_PNP_PROBE(device_get_parent(dev), dev, ex_ids);
206
207         /* If the card had a PnP ID that didn't match any we know about */
208         if (error == ENXIO) {
209                 return(error);
210         }
211
212         /* If we had some other problem. */
213         if (!(error == 0 || error == ENOENT)) {
214                 return(error);
215         }
216
217         iobase = bus_get_resource_start(dev, SYS_RES_IOPORT, 0);
218         if (!iobase) {
219                 printf("ex: no iobase?\n");
220                 return(ENXIO);
221         }
222
223         if (!look_for_card(iobase)) {
224                 printf("ex: no card found at 0x%03x\n", iobase);
225                 return(ENXIO);
226         }
227
228         if (bootverbose)
229                 printf("ex: ex_isa_probe() found card at 0x%03x\n", iobase);
230
231         /*
232          * Reset the card.
233          */
234         outb(iobase + CMD_REG, Reset_CMD);
235         DELAY(800);
236
237         ex_get_address(iobase, enaddr);
238
239         /* work out which set of irq <-> internal tables to use */
240         if (ex_card_type(enaddr) == CARD_TYPE_EX_10_PLUS) {
241                 irq2ee = plus_irq2eemap;
242                 ee2irq = plus_ee2irqmap;
243         } else {
244                 irq2ee = irq2eemap;
245                 ee2irq = ee2irqmap;
246         }
247
248         tmp = eeprom_read(iobase, EE_W1) & EE_W1_INT_SEL;
249         irq = bus_get_resource_start(dev, SYS_RES_IRQ, 0);
250
251         if (irq > 0) {
252                 /* This will happen if board is in PnP mode. */
253                 if (ee2irq[tmp] != irq) {
254                         printf("ex: WARNING: board's EEPROM is configured"
255                                 " for IRQ %d, using %d\n",
256                                 ee2irq[tmp], irq);
257                 }
258         } else {
259                 irq = ee2irq[tmp];
260                 bus_set_resource(dev, SYS_RES_IRQ, 0, irq, 1);
261         }
262
263         if (irq == 0) {
264                 printf("ex: invalid IRQ.\n");
265                 return(ENXIO);
266         }
267
268         return(0);
269 }
270
271 static int
272 ex_isa_attach(device_t dev)
273 {
274         struct ex_softc *       sc = device_get_softc(dev);
275         int                     error = 0;
276         u_int16_t               temp;
277
278         sc->dev = dev;
279         sc->ioport_rid = 0;
280         sc->irq_rid = 0;
281
282         if ((error = ex_alloc_resources(dev)) != 0) {
283                 device_printf(dev, "ex_alloc_resources() failed!\n");
284                 goto bad;
285         }
286
287         /*
288          * Fill in several fields of the softc structure:
289          *      - I/O base address.
290          *      - Hardware Ethernet address.
291          *      - IRQ number (if not supplied in config file, read it from EEPROM).
292          *      - Connector type.
293          */
294         sc->iobase = rman_get_start(sc->ioport);
295         sc->irq_no = rman_get_start(sc->irq);
296
297         ex_get_address(sc->iobase, sc->arpcom.ac_enaddr);
298
299         temp = eeprom_read(sc->iobase, EE_W0);
300         device_printf(sc->dev, "%s config, %s bus, ",
301                 (temp & EE_W0_PNP) ? "PnP" : "Manual",
302                 (temp & EE_W0_BUS16) ? "16-bit" : "8-bit");
303
304         temp = eeprom_read(sc->iobase, EE_W6);
305         printf("board id 0x%03x, stepping 0x%01x\n",
306                 (temp & EE_W6_BOARD_MASK) >> EE_W6_BOARD_SHIFT,
307                 temp & EE_W6_STEP_MASK);
308
309         if ((error = ex_attach(dev)) != 0) {
310                 device_printf(dev, "ex_attach() failed!\n");
311                 goto bad;
312         }
313
314         error = bus_setup_intr(dev, sc->irq, 0,
315                                 ex_intr, (void *)sc,
316                                 &sc->ih, NULL);
317         if (error) {
318                 device_printf(dev, "bus_setup_intr() failed!\n");
319                 goto bad;
320         }
321
322         return(0);
323 bad:
324         ex_release_resources(dev);
325         return (error);
326 }