Do a major clean-up of the BUSDMA architecture. A large number of
[dragonfly.git] / sys / dev / netif / ep / if_ep_eisa.c
1 /*
2  * Product specific probe and attach routines for:
3  *      3COM 3C579 and 3C509(in eisa config mode) ethernet controllers
4  *
5  * Copyright (c) 1996 Justin T. Gibbs
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice immediately at the beginning of the file, without modification,
13  *    this list of conditions, and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Absolutely no warranty of function or purpose is made by the author
18  *    Justin T. Gibbs.
19  * 4. Modifications may be freely made to this file if the above conditions
20  *    are met.
21  *
22  * $FreeBSD: src/sys/dev/ep/if_ep_eisa.c,v 1.18 2000/01/14 07:14:00 peter Exp $
23  * $DragonFly: src/sys/dev/netif/ep/if_ep_eisa.c,v 1.12 2006/10/25 20:55:56 dillon Exp $
24  */
25
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/kernel.h>
29 #include <sys/socket.h>
30 #include <sys/module.h>
31 #include <sys/bus.h>
32 #include <sys/rman.h> 
33
34 #include <net/if.h>
35 #include <net/if_arp.h>
36 #include <net/if_media.h> 
37
38 #include <machine/clock.h>
39
40 #include <bus/eisa/eisaconf.h>
41
42 #include "if_epreg.h"
43 #include "if_epvar.h"
44
45 #define EISA_DEVICE_ID_3COM_3C509_TP    0x506d5090
46 #define EISA_DEVICE_ID_3COM_3C509_BNC   0x506d5091
47 #define EISA_DEVICE_ID_3COM_3C579_TP    0x506d5092
48 #define EISA_DEVICE_ID_3COM_3C579_BNC   0x506d5093
49 #define EISA_DEVICE_ID_3COM_3C509_COMBO 0x506d5094
50 #define EISA_DEVICE_ID_3COM_3C509_TPO   0x506d5095
51
52 #define EP_EISA_SLOT_OFFSET             0x0c80
53 #define EP_EISA_IOSIZE                  0x000a
54
55 #define EISA_IOCONF                     0x0008
56 #define         IRQ_CHANNEL             0xf000
57 #define                 INT_3           0x3000
58 #define                 INT_5           0x5000
59 #define                 INT_7           0x7000
60 #define                 INT_9           0x9000
61 #define                 INT_10          0xa000
62 #define                 INT_11          0xb000
63 #define                 INT_12          0xc000
64 #define                 INT_15          0xf000
65 #define EISA_BPROM_MEDIA_CONF           0x0006
66 #define         TRANS_TYPE              0xc000
67 #define                 TRANS_TP        0x0000
68 #define                 TRANS_AUI       0x4000
69 #define                 TRANS_BNC       0xc000
70
71 static const char *ep_match (eisa_id_t type);
72
73 static const char*
74 ep_match(eisa_id_t type)
75 {
76         switch(type) {
77                 case EISA_DEVICE_ID_3COM_3C509_TP:
78                         return "3Com 3C509-TP Network Adapter";
79                         break;
80                 case EISA_DEVICE_ID_3COM_3C509_BNC:
81                         return "3Com 3C509-BNC Network Adapter";
82                         break;
83                 case EISA_DEVICE_ID_3COM_3C579_TP:
84                         return "3Com 3C579-TP EISA Network Adapter";
85                         break;
86                 case EISA_DEVICE_ID_3COM_3C579_BNC:
87                         return "3Com 3C579-BNC EISA Network Adapter";
88                         break;
89                 case EISA_DEVICE_ID_3COM_3C509_COMBO:
90                         return "3Com 3C509-Combo Network Adapter";
91                         break;
92                 case EISA_DEVICE_ID_3COM_3C509_TPO:
93                         return "3Com 3C509-TPO Network Adapter";
94                         break;
95                 default:
96                         break;
97         }
98         return (NULL);
99 }
100
101 static int
102 ep_eisa_probe(device_t dev)
103 {
104         const char *desc;
105         u_long iobase;
106         u_short conf;
107         u_long port;
108         int irq;
109         int int_trig;
110
111         desc = ep_match(eisa_get_id(dev));
112         if (!desc)
113                 return (ENXIO);
114         device_set_desc(dev, desc);
115
116         port = (eisa_get_slot(dev) * EISA_SLOT_SIZE);
117         iobase = port + EP_EISA_SLOT_OFFSET;
118
119         /* We must be in EISA configuration mode */
120         if ((inw(iobase + EP_W0_ADDRESS_CFG) & 0x1f) != 0x1f)
121             return ENXIO;
122
123         eisa_add_iospace(dev, iobase, EP_EISA_IOSIZE, RESVADDR_NONE);
124         eisa_add_iospace(dev, port, EP_IOSIZE, RESVADDR_NONE);
125
126         conf = inw(iobase + EISA_IOCONF);
127         /* Determine our IRQ */
128         switch (conf & IRQ_CHANNEL) {
129         case INT_3:
130             irq = 3;
131             break;
132         case INT_5:
133             irq = 5;
134             break;
135         case INT_7:
136             irq = 7;
137             break;
138         case INT_9:
139             irq = 9;
140             break;
141         case INT_10:
142             irq = 10;
143             break;
144         case INT_11:
145             irq = 11;
146             break;
147         case INT_12:
148             irq = 12;
149             break;
150         case INT_15:
151             irq = 15;
152             break;
153         default:
154                                 /* Disabled */
155             device_printf(dev, "3COM Network Adapter at "
156                           "slot %d has its IRQ disabled. "
157                           "Probe failed.\n", 
158                           eisa_get_slot(dev));
159             return ENXIO;
160         }
161
162         switch(eisa_get_id(dev)) {
163                 case EISA_DEVICE_ID_3COM_3C579_BNC:
164                 case EISA_DEVICE_ID_3COM_3C579_TP:
165                         int_trig = EISA_TRIGGER_LEVEL;
166                         break;
167                 default:
168                         int_trig = EISA_TRIGGER_EDGE;
169                         break;
170         }
171                         
172         eisa_add_intr(dev, irq, int_trig);
173
174         return 0;
175 }
176
177 static int
178 ep_eisa_attach(device_t dev)
179 {
180         struct ep_softc *       sc = device_get_softc(dev);
181         struct resource *       eisa_io = NULL;
182         u_int32_t               eisa_iobase;
183         int                     irq;
184         int                     error = 0;
185         int                     rid;
186
187         rid = 1;
188         eisa_io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
189         if (!eisa_io) {
190                 device_printf(dev, "No I/O space?!\n");
191                 error = ENXIO;
192                 goto bad;
193         }
194         eisa_iobase = rman_get_start(eisa_io);
195
196         /* Reset and Enable the card */
197         outb(eisa_iobase + EP_W0_CONFIG_CTRL, W0_P4_CMD_RESET_ADAPTER);
198         DELAY(1000); /* we must wait at least 1 ms */
199         outb(eisa_iobase + EP_W0_CONFIG_CTRL, W0_P4_CMD_ENABLE_ADAPTER);
200         /* Now the registers are availible through the lower ioport */
201
202         if ((error = ep_alloc(dev))) {
203                 device_printf(dev, "ep_alloc() failed! (%d)\n", error);
204                 goto bad;
205         }
206
207         switch(eisa_get_id(dev)) {
208                 case EISA_DEVICE_ID_3COM_3C579_BNC:
209                 case EISA_DEVICE_ID_3COM_3C579_TP:
210                         sc->stat = F_ACCESS_32_BITS;
211                         break;
212                 default:
213                         break;
214         }
215
216         ep_get_media(sc);
217
218         irq = rman_get_start(sc->irq);
219         if (irq == 9)
220                 irq = 2;
221
222         GO_WINDOW(0);
223         SET_IRQ(BASE, irq);
224
225         if ((error = ep_attach(sc))) {
226                 device_printf(dev, "ep_attach() failed! (%d)\n", error);
227                 goto bad;
228         }
229
230         error = bus_setup_intr(dev, sc->irq, INTR_NETSAFE,
231                                ep_intr, sc, &sc->ep_intrhand, 
232                                sc->arpcom.ac_if.if_serializer);
233         if (error) {
234                 device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
235                 goto bad;
236         }
237
238         return (0);
239
240  bad:
241         if (eisa_io)
242                 bus_release_resource(dev, SYS_RES_IOPORT, 0, eisa_io);
243
244         ep_free(dev);
245         return (error);
246 }
247
248 static device_method_t ep_eisa_methods[] = {
249         /* Device interface */
250         DEVMETHOD(device_probe,         ep_eisa_probe),
251         DEVMETHOD(device_attach,        ep_eisa_attach),
252
253         { 0, 0 }
254 };
255
256 static driver_t ep_eisa_driver = {
257         "ep",
258         ep_eisa_methods,
259         sizeof(struct ep_softc),
260 };
261
262 extern devclass_t ep_devclass;
263
264 DRIVER_MODULE(if_ep, eisa, ep_eisa_driver, ep_devclass, 0, 0);