Fix typo.
[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.5 2006/12/22 23:26:16 swildner 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 #include <sys/rman.h>
39
40 #include <bus/eisa/eisaconf.h>
41
42 #include "btreg.h"
43
44 #define EISA_DEVICE_ID_BUSLOGIC_74X_B   0x0ab34201
45 #define EISA_DEVICE_ID_BUSLOGIC_74X_C   0x0ab34202
46 #define EISA_DEVICE_ID_SDC3222F         0x0ab34781
47 #define EISA_DEVICE_ID_AMI_4801         0x05a94801
48
49 #define BT_IOSIZE               0x04            /* Move to central header */
50 #define BT_EISA_IOSIZE          0x100
51 #define BT_EISA_SLOT_OFFSET     0xc00
52
53 #define EISA_IOCONF                     0x08C
54 #define         PORTADDR                0x07
55 #define                 PORT_330        0x00
56 #define                 PORT_334        0x01
57 #define                 PORT_230        0x02
58 #define                 PORT_234        0x03
59 #define                 PORT_130        0x04
60 #define                 PORT_134        0x05
61 #define         IRQ_CHANNEL             0xe0
62 #define                 INT_11          0x40
63 #define                 INT_10          0x20
64 #define                 INT_15          0xa0
65 #define                 INT_12          0x60
66 #define                 INT_14          0x80
67 #define                 INT_9           0x00
68
69 #define EISA_IRQ_TYPE                   0x08D
70 #define       LEVEL                     0x40
71
72 /* Definitions for the AMI Series 48 controler */
73 #define AMI_EISA_IOSIZE                 0x500   /* Two separate ranges?? */
74 #define AMI_EISA_SLOT_OFFSET            0x800
75 #define AMI_EISA_IOCONF                 0x000
76 #define         AMI_DMA_CHANNEL         0x03
77 #define         AMI_IRQ_CHANNEL         0x1c
78 #define                 AMI_INT_15      0x14
79 #define                 AMI_INT_14      0x10
80 #define                 AMI_INT_12      0x0c
81 #define                 AMI_INT_11      0x00
82 #define                 AMI_INT_10      0x08
83 #define                 AMI_INT_9       0x04
84 #define         AMI_BIOS_ADDR           0xe0
85
86 #define AMI_EISA_IOCONF1                0x001
87 #define         AMI_PORTADDR            0x0e
88 #define                 AMI_PORT_334    0x08
89 #define                 AMI_PORT_330    0x00
90 #define                 AMI_PORT_234    0x0c
91 #define                 AMI_PORT_230    0x04
92 #define                 AMI_PORT_134    0x0a
93 #define                 AMI_PORT_130    0x02
94 #define         AMI_IRQ_LEVEL           0x01
95
96
97 #define AMI_MISC2_OPTIONS               0x49E
98 #define         AMI_ENABLE_ISA_DMA      0x08
99
100 static const char *bt_match(eisa_id_t type);
101
102 static int
103 bt_eisa_alloc_resources(device_t dev)
104 {
105         struct  bt_softc *bt = device_get_softc(dev);
106         int rid;
107         struct resource *port;
108         struct resource *irq;
109         int shared;
110
111         /*
112          * XXX assumes that the iospace ranges are sorted in increasing
113          * order.
114          */
115         rid = 0;
116         port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
117                                   0, ~0, 1, RF_ACTIVE);
118         if (!port)
119                 return (ENOMEM);
120
121         bt_init_softc(dev, port, 0, 0);
122
123         if (eisa_get_irq(dev) != -1) {
124                 shared = bt->level_trigger_ints ? RF_SHAREABLE : 0;
125                 rid = 0;
126                 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
127                                          0, ~0, 1, shared | RF_ACTIVE);
128                 if (!irq) {
129                         if (port)
130                                 bus_release_resource(dev, SYS_RES_IOPORT,
131                                                      0, port);
132                         return (ENOMEM);
133                 }
134         } else
135                 irq = 0;
136         bt->irq = irq;
137
138         return (0);
139 }
140
141 static void
142 bt_eisa_release_resources(device_t dev)
143 {
144         struct  bt_softc *bt = device_get_softc(dev);
145
146         if (bt->port)
147                 bus_release_resource(dev, SYS_RES_IOPORT, 0, bt->port);
148         if (bt->irq)
149                 bus_release_resource(dev, SYS_RES_IRQ, 0, bt->irq);
150         bt_free_softc(dev);
151 }
152
153 static const char*
154 bt_match(eisa_id_t type)
155 {
156         switch(type) {
157                 case EISA_DEVICE_ID_BUSLOGIC_74X_B:
158                         return ("Buslogic 74xB SCSI host adapter");
159                         break;
160                 case EISA_DEVICE_ID_BUSLOGIC_74X_C:
161                         return ("Buslogic 74xC SCSI host adapter");
162                         break;
163                 case EISA_DEVICE_ID_SDC3222F:
164                         return ("Storage Dimensions SDC3222F SCSI host adapter");
165                         break;
166                 case EISA_DEVICE_ID_AMI_4801:
167                         return ("AMI Series 48 SCSI host adapter");
168                         break;
169                 default:
170                         break;
171         }
172         return (NULL);
173 }
174
175 static int
176 bt_eisa_probe(device_t dev)
177 {
178         const char *desc;
179         u_long iobase;
180         struct bt_probe_info info;
181         u_long port;
182         u_long iosize;
183         u_int  ioconf;
184         int    result;
185         int    shared;
186
187         desc = bt_match(eisa_get_id(dev));
188         if (!desc)
189                 return (ENXIO);
190         device_set_desc(dev, desc);
191
192         iobase = (eisa_get_slot(dev) * EISA_SLOT_SIZE); 
193         if (eisa_get_id(dev) == EISA_DEVICE_ID_AMI_4801) {
194                 u_int ioconf1;
195
196                 iobase += AMI_EISA_SLOT_OFFSET;
197                 iosize = AMI_EISA_IOSIZE;
198                 ioconf1 = inb(iobase + AMI_EISA_IOCONF1);
199                 /* Determine "ISA" I/O port */
200                 switch (ioconf1 & AMI_PORTADDR) {
201                 case AMI_PORT_330:
202                         port = 0x330;
203                         break;
204                 case AMI_PORT_334:
205                         port = 0x334;
206                         break;
207                 case AMI_PORT_230:
208                         port = 0x230;
209                         break;
210                 case AMI_PORT_234:
211                         port = 0x234;
212                         break;
213                 case AMI_PORT_134:
214                         port = 0x134;
215                         break;
216                 case AMI_PORT_130:
217                         port = 0x130;
218                         break;
219                 default:
220                         /* Disabled */
221                         kprintf("bt: AMI EISA Adapter at "
222                                "slot %d has a disabled I/O "
223                                "port.  Cannot attach.\n",
224                                eisa_get_slot(dev));
225                         return (ENXIO);
226                 }
227                 shared = (inb(iobase + AMI_EISA_IOCONF1) & AMI_IRQ_LEVEL) ?
228                                 EISA_TRIGGER_LEVEL : EISA_TRIGGER_EDGE;
229         } else {
230                 iobase += BT_EISA_SLOT_OFFSET;
231                 iosize = BT_EISA_IOSIZE;
232
233                 ioconf = inb(iobase + EISA_IOCONF);
234                 /* Determine "ISA" I/O port */
235                 switch (ioconf & PORTADDR) {
236                 case PORT_330:
237                         port = 0x330;
238                         break;
239                 case PORT_334:
240                         port = 0x334;
241                         break;
242                 case PORT_230:
243                         port = 0x230;
244                         break;
245                 case PORT_234:
246                         port = 0x234;
247                         break;
248                 case PORT_130:
249                         port = 0x130;
250                         break;
251                 case PORT_134:
252                         port = 0x134;
253                         break;
254                 default:
255                         /* Disabled */
256                         kprintf("bt: Buslogic EISA Adapter at "
257                                "slot %d has a disabled I/O "
258                                "port.  Cannot attach.\n",
259                                eisa_get_slot(dev));
260                         return (ENXIO);
261                 }
262                 shared = (inb(iobase + EISA_IRQ_TYPE) & LEVEL) ?
263                                 EISA_TRIGGER_LEVEL : EISA_TRIGGER_EDGE;
264         }
265         bt_mark_probed_iop(port);
266
267         /* Tell parent where our resources are going to be */
268         eisa_add_iospace(dev, iobase, iosize, RESVADDR_NONE);
269         eisa_add_iospace(dev, port, BT_IOSIZE, RESVADDR_NONE);
270
271         /* And allocate them */
272         bt_eisa_alloc_resources(dev);
273
274         if (bt_port_probe(dev, &info) != 0) {
275                 kprintf("bt_eisa_probe: Probe failed for "
276                        "card at slot 0x%x\n", eisa_get_slot(dev));
277                 result = ENXIO;
278         } else {
279                 eisa_add_intr(dev, info.irq, shared);
280                 result = 0;
281         }
282         bt_eisa_release_resources(dev);
283
284         return (result);
285 }
286
287 static int
288 bt_eisa_attach(device_t dev)
289 {
290         struct bt_softc *bt = device_get_softc(dev);
291
292         /* Allocate resources */
293         bt_eisa_alloc_resources(dev);
294
295         /* Allocate a dmatag for our SCB DMA maps */
296         /* XXX Should be a child of the PCI bus dma tag */
297         if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/1, /*boundary*/0,
298                                /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
299                                /*highaddr*/BUS_SPACE_MAXADDR,
300                                /*filter*/NULL, /*filterarg*/NULL,
301                                /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
302                                /*nsegments*/BUS_SPACE_UNRESTRICTED,
303                                /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
304                                /*flags*/0, &bt->parent_dmat) != 0) {
305                 bt_eisa_release_resources(dev);
306                 return -1;
307         }
308
309         /*
310          * Now that we know we own the resources we need, do the full
311          * card initialization.
312          */
313         if (bt_probe(dev) || bt_fetch_adapter_info(dev) || bt_init(dev)) {
314                 bt_eisa_release_resources(dev);
315                 return -1;
316         }
317
318         /* Attach sub-devices - always succeeds (sets up intr) */
319         bt_attach(dev);
320
321         return 0;
322 }
323
324 static device_method_t bt_eisa_methods[] = {
325         /* Device interface */
326         DEVMETHOD(device_probe,         bt_eisa_probe),
327         DEVMETHOD(device_attach,        bt_eisa_attach),
328
329         { 0, 0 }
330 };
331
332 static driver_t bt_eisa_driver = {
333         "bt",
334         bt_eisa_methods,
335         sizeof(struct bt_softc),
336 };
337
338 static devclass_t bt_devclass;
339
340 DRIVER_MODULE(bt, eisa, bt_eisa_driver, bt_devclass, 0, 0);