Remove the INTR_TYPE_* flags. The interrupt type is no longer used to
[dragonfly.git] / sys / dev / disk / advansys / adv_eisa.c
1 /*
2  * Device probe and attach routines for the following
3  * Advanced Systems Inc. SCSI controllers:
4  *
5  *   Single Channel Products:
6  *      ABP742 - Bus-Master EISA (240 CDB)
7  *
8  *   Dual Channel Products:  
9  *      ABP752 - Dual Channel Bus-Master EISA (240 CDB Per Channel)
10  *
11  * Copyright (c) 1997 Justin Gibbs.
12  * All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions, and the following disclaimer,
19  *    without modification, immediately at the beginning of the file.
20  * 2. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * $FreeBSD: src/sys/dev/advansys/adv_eisa.c,v 1.9.2.1 2000/04/14 13:32:44 nyan Exp $
36  * $DragonFly: src/sys/dev/disk/advansys/adv_eisa.c,v 1.5 2005/10/12 17:35:49 dillon Exp $
37  */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/bus.h>
44
45 #include <machine/bus_pio.h>
46 #include <machine/bus.h>
47 #include <machine/resource.h>
48 #include <sys/rman.h>
49
50 #include <bus/eisa/eisaconf.h>
51
52 #include "advansys.h"
53
54 #define EISA_DEVICE_ID_ADVANSYS_740     0x04507400
55 #define EISA_DEVICE_ID_ADVANSYS_750     0x04507500
56
57 #define ADV_EISA_SLOT_OFFSET            0xc00
58 #define ADV_EISA_OFFSET_CHAN1           0x30
59 #define ADV_EISA_OFFSET_CHAN2           0x50
60 #define ADV_EISA_IOSIZE                 0x100
61
62 #define ADV_EISA_ROM_BIOS_ADDR_REG      0x86
63 #define ADV_EISA_IRQ_BURST_LEN_REG      0x87
64 #define         ADV_EISA_IRQ_MASK       0x07
65 #define         ADV_EISA_IRQ_10         0x00
66 #define         ADV_EISA_IRQ_11         0x01
67 #define         ADV_EISA_IRQ_12         0x02
68 #define         ADV_EISA_IRQ_14         0x04
69 #define         ADV_EISA_IRQ_15         0x05
70
71 #define ADV_EISA_MAX_DMA_ADDR   (0x07FFFFFFL)
72 #define ADV_EISA_MAX_DMA_COUNT  (0x07FFFFFFL)
73
74 /* 
75  * The overrun buffer shared amongst all EISA adapters.
76  */
77 static  u_int8_t*       overrun_buf;
78 static  bus_dma_tag_t   overrun_dmat;
79 static  bus_dmamap_t    overrun_dmamap;
80 static  bus_addr_t      overrun_physbase;
81
82 static const char*
83 adv_eisa_match(eisa_id_t type)
84 {
85         switch (type & ~0xF) {
86         case EISA_DEVICE_ID_ADVANSYS_740:
87                 return ("AdvanSys ABP-740/742 SCSI adapter");
88                 break;
89         case EISA_DEVICE_ID_ADVANSYS_750:
90                 return ("AdvanSys ABP-750/752 SCSI adapter");
91                 break;
92         default:
93                 break;
94         }
95         return (NULL);
96 }
97
98 static int
99 adv_eisa_probe(device_t dev)
100 {
101         const char *desc;
102         u_int32_t iobase;
103         u_int8_t irq;
104
105         desc = adv_eisa_match(eisa_get_id(dev));
106         if (!desc)
107                 return (ENXIO);
108         device_set_desc(dev, desc);
109
110         iobase = (eisa_get_slot(dev) * EISA_SLOT_SIZE) + ADV_EISA_SLOT_OFFSET;
111
112         eisa_add_iospace(dev, iobase, ADV_EISA_IOSIZE, RESVADDR_NONE);
113         irq = inb(iobase + ADV_EISA_IRQ_BURST_LEN_REG);
114         irq &= ADV_EISA_IRQ_MASK;
115         switch (irq) {
116         case 0:
117         case 1:
118         case 2:
119         case 4:
120         case 5:
121             break;
122         default:
123             printf("adv at slot %d: illegal "
124                    "irq setting %d\n", eisa_get_slot(dev),
125                    irq);
126             return ENXIO;
127         }
128         eisa_add_intr(dev, irq + 10, EISA_TRIGGER_LEVEL);
129
130         return 0;
131 }
132
133 static int
134 adv_eisa_attach(device_t dev)
135 {
136         struct adv_softc *adv;
137         struct adv_softc *adv_b;
138         struct resource *io;
139         struct resource *irq;
140         int rid, error;
141         void *ih;
142
143         adv_b = NULL;
144
145         rid = 0;
146         io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
147                                 0, ~0, 1, RF_ACTIVE);
148         if (!io) {
149                 device_printf(dev, "No I/O space?!\n");
150                 return ENOMEM;
151         }
152
153         rid = 0;
154         irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
155                                  0, ~0, 1, RF_SHAREABLE | RF_ACTIVE);
156         if (!irq) {
157                 device_printf(dev, "No irq?!\n");
158                 bus_release_resource(dev, SYS_RES_IOPORT, 0, io);
159                 return ENOMEM;
160
161         }
162
163         switch (eisa_get_id(dev) & ~0xF) {
164         case EISA_DEVICE_ID_ADVANSYS_750:
165                 adv_b = adv_alloc(dev, rman_get_bustag(io),
166                                   rman_get_bushandle(io) + ADV_EISA_OFFSET_CHAN2);
167                 if (adv_b == NULL)
168                         goto bad;
169                 
170                 /*
171                  * Allocate a parent dmatag for all tags created
172                  * by the MI portions of the advansys driver
173                  */
174                 /* XXX Should be a child of the PCI bus dma tag */
175                 error = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1,
176                                            /*boundary*/0,
177                                            /*lowaddr*/ADV_EISA_MAX_DMA_ADDR,
178                                            /*highaddr*/BUS_SPACE_MAXADDR,
179                                            /*filter*/NULL, /*filterarg*/NULL,
180                                            /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
181                                            /*nsegments*/BUS_SPACE_UNRESTRICTED,
182                                            /*maxsegsz*/ADV_EISA_MAX_DMA_COUNT,
183                                            /*flags*/0,
184                                            &adv_b->parent_dmat);
185  
186                 if (error != 0) {
187                         printf("%s: Could not allocate DMA tag - error %d\n",
188                                adv_name(adv_b), error);
189                         adv_free(adv_b);
190                         goto bad;
191                 }
192
193                 adv_b->init_level++;
194
195                 /* FALLTHROUGH */
196         case EISA_DEVICE_ID_ADVANSYS_740:
197                 adv = adv_alloc(dev, rman_get_bustag(io),
198                                 rman_get_bushandle(io) + ADV_EISA_OFFSET_CHAN1);
199                 if (adv == NULL) {
200                         if (adv_b != NULL)
201                                 adv_free(adv_b);
202                         goto bad;
203                 }
204
205                 /*
206                  * Allocate a parent dmatag for all tags created
207                  * by the MI portions of the advansys driver
208                  */
209                 /* XXX Should be a child of the PCI bus dma tag */
210                 error = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1,
211                                            /*boundary*/0,
212                                            /*lowaddr*/ADV_EISA_MAX_DMA_ADDR,
213                                            /*highaddr*/BUS_SPACE_MAXADDR,
214                                            /*filter*/NULL, /*filterarg*/NULL,
215                                            /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
216                                            /*nsegments*/BUS_SPACE_UNRESTRICTED,
217                                            /*maxsegsz*/ADV_EISA_MAX_DMA_COUNT,
218                                            /*flags*/0,
219                                            &adv->parent_dmat);
220  
221                 if (error != 0) {
222                         printf("%s: Could not allocate DMA tag - error %d\n",
223                                adv_name(adv), error);
224                         adv_free(adv);
225                         goto bad;
226                 }
227
228                 adv->init_level++;
229                 break;
230         default: 
231                 printf("adveisaattach: Unknown device type!\n");
232                 goto bad;
233                 break;
234         }
235
236         if (overrun_buf == NULL) {
237                 /* Need to allocate our overrun buffer */
238                 if (bus_dma_tag_create(adv->parent_dmat,
239                                        /*alignment*/8,
240                                        /*boundary*/0,
241                                        ADV_EISA_MAX_DMA_ADDR,
242                                        BUS_SPACE_MAXADDR,
243                                        /*filter*/NULL,
244                                        /*filterarg*/NULL,
245                                        ADV_OVERRUN_BSIZE,
246                                        /*nsegments*/1,
247                                        BUS_SPACE_MAXSIZE_32BIT,
248                                        /*flags*/0,
249                                        &overrun_dmat) != 0) {
250                         adv_free(adv);
251                         goto bad;
252                 }
253                 if (bus_dmamem_alloc(overrun_dmat,
254                                      (void **)&overrun_buf,
255                                      BUS_DMA_NOWAIT,
256                                      &overrun_dmamap) != 0) {
257                         bus_dma_tag_destroy(overrun_dmat);
258                         adv_free(adv);
259                         goto bad;
260                 }
261                 /* And permanently map it in */  
262                 bus_dmamap_load(overrun_dmat, overrun_dmamap,
263                                 overrun_buf, ADV_OVERRUN_BSIZE,
264                                 adv_map, &overrun_physbase,
265                                 /*flags*/0);
266         }
267         
268         /*
269          * Now that we know we own the resources we need, do the 
270          * card initialization.
271          */
272
273         /*
274          * Stop the chip.
275          */
276         ADV_OUTB(adv, ADV_CHIP_CTRL, ADV_CC_HALT);
277         ADV_OUTW(adv, ADV_CHIP_STATUS, 0);
278
279         adv->chip_version = EISA_REVISION_ID(eisa_get_id(dev))
280                           + ADV_CHIP_MIN_VER_EISA - 1;
281
282         if (adv_init(adv) != 0) {
283                 adv_free(adv);
284                 if (adv_b != NULL)
285                         adv_free(adv_b);
286                 return(-1);
287         }
288
289         adv->max_dma_count = ADV_EISA_MAX_DMA_COUNT;
290         adv->max_dma_addr = ADV_EISA_MAX_DMA_ADDR;
291
292         if (adv_b != NULL) {
293                 /*
294                  * Stop the chip.
295                  */
296                 ADV_OUTB(adv_b, ADV_CHIP_CTRL, ADV_CC_HALT);
297                 ADV_OUTW(adv_b, ADV_CHIP_STATUS, 0);
298
299                 adv_b->chip_version = EISA_REVISION_ID(eisa_get_id(dev))
300                                     + ADV_CHIP_MIN_VER_EISA - 1;
301
302                 if (adv_init(adv_b) != 0) {
303                         adv_free(adv_b);
304                 } else {
305                         adv_b->max_dma_count = ADV_EISA_MAX_DMA_COUNT;
306                         adv_b->max_dma_addr = ADV_EISA_MAX_DMA_ADDR;
307                 }
308         }
309
310         /*
311          * Enable our interrupt handler.
312          */
313         bus_setup_intr(dev, irq, 0, adv_intr, adv, &ih, NULL);
314
315         /* Attach sub-devices - always succeeds */
316         adv_attach(adv);
317         if (adv_b != NULL)
318                 adv_attach(adv_b);
319
320         return 0;
321
322  bad:
323         bus_release_resource(dev, SYS_RES_IOPORT, 0, io);
324         bus_release_resource(dev, SYS_RES_IRQ, 0, irq);
325         return -1;
326 }
327
328 static device_method_t adv_eisa_methods[] = {
329         /* Device interface */
330         DEVMETHOD(device_probe,         adv_eisa_probe),
331         DEVMETHOD(device_attach,        adv_eisa_attach),
332         { 0, 0 }
333 };
334
335 static driver_t adv_eisa_driver = {
336         "adv", adv_eisa_methods, sizeof(struct adv_softc)
337 };
338
339 static devclass_t adv_eisa_devclass;
340 DRIVER_MODULE(adv, eisa, adv_eisa_driver, adv_eisa_devclass, 0, 0);