Rip out bad spl manipulating junk from mpt and clean it up.
[dragonfly.git] / sys / dev / disk / buslogic / bt_eisa.c
1 /*
2  * Product specific probe and attach routines for:
3  *      Buslogic BT74x SCSI controllers
4  *
5  * Copyright (c) 1995, 1998, 1999 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. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/sys/dev/buslogic/bt_eisa.c,v 1.12 2000/01/29 14:27:26 peter Exp $
30  * $DragonFly: src/sys/dev/disk/buslogic/bt_eisa.c,v 1.3 2003/08/07 21:16:52 dillon Exp $
31  */
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/bus.h>
38
39 #include <machine/bus_pio.h>
40 #include <machine/bus.h>
41 #include <machine/resource.h>
42 #include <sys/rman.h>
43
44 #include <bus/eisa/eisaconf.h>
45
46 #include "btreg.h"
47
48 #define EISA_DEVICE_ID_BUSLOGIC_74X_B   0x0ab34201
49 #define EISA_DEVICE_ID_BUSLOGIC_74X_C   0x0ab34202
50 #define EISA_DEVICE_ID_SDC3222F         0x0ab34781
51 #define EISA_DEVICE_ID_AMI_4801         0x05a94801
52
53 #define BT_IOSIZE               0x04            /* Move to central header */
54 #define BT_EISA_IOSIZE          0x100
55 #define BT_EISA_SLOT_OFFSET     0xc00
56
57 #define EISA_IOCONF                     0x08C
58 #define         PORTADDR                0x07
59 #define                 PORT_330        0x00
60 #define                 PORT_334        0x01
61 #define                 PORT_230        0x02
62 #define                 PORT_234        0x03
63 #define                 PORT_130        0x04
64 #define                 PORT_134        0x05
65 #define         IRQ_CHANNEL             0xe0
66 #define                 INT_11          0x40
67 #define                 INT_10          0x20
68 #define                 INT_15          0xa0
69 #define                 INT_12          0x60
70 #define                 INT_14          0x80
71 #define                 INT_9           0x00
72
73 #define EISA_IRQ_TYPE                   0x08D
74 #define       LEVEL                     0x40
75
76 /* Definitions for the AMI Series 48 controler */
77 #define AMI_EISA_IOSIZE                 0x500   /* Two separate ranges?? */
78 #define AMI_EISA_SLOT_OFFSET            0x800
79 #define AMI_EISA_IOCONF                 0x000
80 #define         AMI_DMA_CHANNEL         0x03
81 #define         AMI_IRQ_CHANNEL         0x1c
82 #define                 AMI_INT_15      0x14
83 #define                 AMI_INT_14      0x10
84 #define                 AMI_INT_12      0x0c
85 #define                 AMI_INT_11      0x00
86 #define                 AMI_INT_10      0x08
87 #define                 AMI_INT_9       0x04
88 #define         AMI_BIOS_ADDR           0xe0
89
90 #define AMI_EISA_IOCONF1                0x001
91 #define         AMI_PORTADDR            0x0e
92 #define                 AMI_PORT_334    0x08
93 #define                 AMI_PORT_330    0x00
94 #define                 AMI_PORT_234    0x0c
95 #define                 AMI_PORT_230    0x04
96 #define                 AMI_PORT_134    0x0a
97 #define                 AMI_PORT_130    0x02
98 #define         AMI_IRQ_LEVEL           0x01
99
100
101 #define AMI_MISC2_OPTIONS               0x49E
102 #define         AMI_ENABLE_ISA_DMA      0x08
103
104 static const char *bt_match(eisa_id_t type);
105
106 static int
107 bt_eisa_alloc_resources(device_t dev)
108 {
109         struct  bt_softc *bt = device_get_softc(dev);
110         int rid;
111         struct resource *port;
112         struct resource *irq;
113         int shared;
114
115         /*
116          * XXX assumes that the iospace ranges are sorted in increasing
117          * order.
118          */
119         rid = 0;
120         port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
121                                   0, ~0, 1, RF_ACTIVE);
122         if (!port)
123                 return (ENOMEM);
124
125         bt_init_softc(dev, port, 0, 0);
126
127         if (eisa_get_irq(dev) != -1) {
128                 shared = bt->level_trigger_ints ? RF_SHAREABLE : 0;
129                 rid = 0;
130                 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
131                                          0, ~0, 1, shared | RF_ACTIVE);
132                 if (!irq) {
133                         if (port)
134                                 bus_release_resource(dev, SYS_RES_IOPORT,
135                                                      0, port);
136                         return (ENOMEM);
137                 }
138         } else
139                 irq = 0;
140         bt->irq = irq;
141
142         return (0);
143 }
144
145 static void
146 bt_eisa_release_resources(device_t dev)
147 {
148         struct  bt_softc *bt = device_get_softc(dev);
149
150         if (bt->port)
151                 bus_release_resource(dev, SYS_RES_IOPORT, 0, bt->port);
152         if (bt->irq)
153                 bus_release_resource(dev, SYS_RES_IRQ, 0, bt->irq);
154         bt_free_softc(dev);
155 }
156
157 static const char*
158 bt_match(eisa_id_t type)
159 {
160         switch(type) {
161                 case EISA_DEVICE_ID_BUSLOGIC_74X_B:
162                         return ("Buslogic 74xB SCSI host adapter");
163                         break;
164                 case EISA_DEVICE_ID_BUSLOGIC_74X_C:
165                         return ("Buslogic 74xC SCSI host adapter");
166                         break;
167                 case EISA_DEVICE_ID_SDC3222F:
168                         return ("Storage Dimensions SDC3222F SCSI host adapter");
169                         break;
170                 case EISA_DEVICE_ID_AMI_4801:
171                         return ("AMI Series 48 SCSI host adapter");
172                         break;
173                 default:
174                         break;
175         }
176         return (NULL);
177 }
178
179 static int
180 bt_eisa_probe(device_t dev)
181 {
182         const char *desc;
183         u_long iobase;
184         struct bt_probe_info info;
185         u_long port;
186         u_long iosize;
187         u_int  ioconf;
188         int    result;
189         int    shared;
190
191         desc = bt_match(eisa_get_id(dev));
192         if (!desc)
193                 return (ENXIO);
194         device_set_desc(dev, desc);
195
196         iobase = (eisa_get_slot(dev) * EISA_SLOT_SIZE); 
197         if (eisa_get_id(dev) == EISA_DEVICE_ID_AMI_4801) {
198                 u_int ioconf1;
199
200                 iobase += AMI_EISA_SLOT_OFFSET;
201                 iosize = AMI_EISA_IOSIZE;
202                 ioconf1 = inb(iobase + AMI_EISA_IOCONF1);
203                 /* Determine "ISA" I/O port */
204                 switch (ioconf1 & AMI_PORTADDR) {
205                 case AMI_PORT_330:
206                         port = 0x330;
207                         break;
208                 case AMI_PORT_334:
209                         port = 0x334;
210                         break;
211                 case AMI_PORT_230:
212                         port = 0x230;
213                         break;
214                 case AMI_PORT_234:
215                         port = 0x234;
216                         break;
217                 case AMI_PORT_134:
218                         port = 0x134;
219                         break;
220                 case AMI_PORT_130:
221                         port = 0x130;
222                         break;
223                 default:
224                         /* Disabled */
225                         printf("bt: AMI EISA Adapter at "
226                                "slot %d has a disabled I/O "
227                                "port.  Cannot attach.\n",
228                                eisa_get_slot(dev));
229                         return (ENXIO);
230                 }
231                 shared = (inb(iobase + AMI_EISA_IOCONF1) & AMI_IRQ_LEVEL) ?
232                                 EISA_TRIGGER_LEVEL : EISA_TRIGGER_EDGE;
233         } else {
234                 iobase += BT_EISA_SLOT_OFFSET;
235                 iosize = BT_EISA_IOSIZE;
236
237                 ioconf = inb(iobase + EISA_IOCONF);
238                 /* Determine "ISA" I/O port */
239                 switch (ioconf & PORTADDR) {
240                 case PORT_330:
241                         port = 0x330;
242                         break;
243                 case PORT_334:
244                         port = 0x334;
245                         break;
246                 case PORT_230:
247                         port = 0x230;
248                         break;
249                 case PORT_234:
250                         port = 0x234;
251                         break;
252                 case PORT_130:
253                         port = 0x130;
254                         break;
255                 case PORT_134:
256                         port = 0x134;
257                         break;
258                 default:
259                         /* Disabled */
260                         printf("bt: Buslogic EISA Adapter at "
261                                "slot %d has a disabled I/O "
262                                "port.  Cannot attach.\n",
263                                eisa_get_slot(dev));
264                         return (ENXIO);
265                 }
266                 shared = (inb(iobase + EISA_IRQ_TYPE) & LEVEL) ?
267                                 EISA_TRIGGER_LEVEL : EISA_TRIGGER_EDGE;
268         }
269         bt_mark_probed_iop(port);
270
271         /* Tell parent where our resources are going to be */
272         eisa_add_iospace(dev, iobase, iosize, RESVADDR_NONE);
273         eisa_add_iospace(dev, port, BT_IOSIZE, RESVADDR_NONE);
274
275         /* And allocate them */
276         bt_eisa_alloc_resources(dev);
277
278         if (bt_port_probe(dev, &info) != 0) {
279                 printf("bt_eisa_probe: Probe failed for "
280                        "card at slot 0x%x\n", eisa_get_slot(dev));
281                 result = ENXIO;
282         } else {
283                 eisa_add_intr(dev, info.irq, shared);
284                 result = 0;
285         }
286         bt_eisa_release_resources(dev);
287
288         return (result);
289 }
290
291 static int
292 bt_eisa_attach(device_t dev)
293 {
294         struct bt_softc *bt = device_get_softc(dev);
295
296         /* Allocate resources */
297         bt_eisa_alloc_resources(dev);
298
299         /* Allocate a dmatag for our SCB DMA maps */
300         /* XXX Should be a child of the PCI bus dma tag */
301         if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/1, /*boundary*/0,
302                                /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
303                                /*highaddr*/BUS_SPACE_MAXADDR,
304                                /*filter*/NULL, /*filterarg*/NULL,
305                                /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
306                                /*nsegments*/BUS_SPACE_UNRESTRICTED,
307                                /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
308                                /*flags*/0, &bt->parent_dmat) != 0) {
309                 bt_eisa_release_resources(dev);
310                 return -1;
311         }
312
313         /*
314          * Now that we know we own the resources we need, do the full
315          * card initialization.
316          */
317         if (bt_probe(dev) || bt_fetch_adapter_info(dev) || bt_init(dev)) {
318                 bt_eisa_release_resources(dev);
319                 return -1;
320         }
321
322         /* Attach sub-devices - always succeeds (sets up intr) */
323         bt_attach(dev);
324
325         return 0;
326 }
327
328 static device_method_t bt_eisa_methods[] = {
329         /* Device interface */
330         DEVMETHOD(device_probe,         bt_eisa_probe),
331         DEVMETHOD(device_attach,        bt_eisa_attach),
332
333         { 0, 0 }
334 };
335
336 static driver_t bt_eisa_driver = {
337         "bt",
338         bt_eisa_methods,
339         sizeof(struct bt_softc),
340 };
341
342 static devclass_t bt_devclass;
343
344 DRIVER_MODULE(bt, eisa, bt_eisa_driver, bt_devclass, 0, 0);