Merge branch 'vendor/GCC'
[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.7 2006/12/22 23:26:15 swildner 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 #include <sys/rman.h>
45
46 #include <bus/eisa/eisaconf.h>
47
48 #include "advansys.h"
49
50 #define EISA_DEVICE_ID_ADVANSYS_740     0x04507400
51 #define EISA_DEVICE_ID_ADVANSYS_750     0x04507500
52
53 #define ADV_EISA_SLOT_OFFSET            0xc00
54 #define ADV_EISA_OFFSET_CHAN1           0x30
55 #define ADV_EISA_OFFSET_CHAN2           0x50
56 #define ADV_EISA_IOSIZE                 0x100
57
58 #define ADV_EISA_ROM_BIOS_ADDR_REG      0x86
59 #define ADV_EISA_IRQ_BURST_LEN_REG      0x87
60 #define         ADV_EISA_IRQ_MASK       0x07
61 #define         ADV_EISA_IRQ_10         0x00
62 #define         ADV_EISA_IRQ_11         0x01
63 #define         ADV_EISA_IRQ_12         0x02
64 #define         ADV_EISA_IRQ_14         0x04
65 #define         ADV_EISA_IRQ_15         0x05
66
67 #define ADV_EISA_MAX_DMA_ADDR   (0x07FFFFFFL)
68 #define ADV_EISA_MAX_DMA_COUNT  (0x07FFFFFFL)
69
70 /* 
71  * The overrun buffer shared amongst all EISA adapters.
72  */
73 static  u_int8_t*       overrun_buf;
74 static  bus_dma_tag_t   overrun_dmat;
75 static  bus_dmamap_t    overrun_dmamap;
76 static  bus_addr_t      overrun_physbase;
77
78 static const char*
79 adv_eisa_match(eisa_id_t type)
80 {
81         switch (type & ~0xF) {
82         case EISA_DEVICE_ID_ADVANSYS_740:
83                 return ("AdvanSys ABP-740/742 SCSI adapter");
84                 break;
85         case EISA_DEVICE_ID_ADVANSYS_750:
86                 return ("AdvanSys ABP-750/752 SCSI adapter");
87                 break;
88         default:
89                 break;
90         }
91         return (NULL);
92 }
93
94 static int
95 adv_eisa_probe(device_t dev)
96 {
97         const char *desc;
98         u_int32_t iobase;
99         u_int8_t irq;
100
101         desc = adv_eisa_match(eisa_get_id(dev));
102         if (!desc)
103                 return (ENXIO);
104         device_set_desc(dev, desc);
105
106         iobase = (eisa_get_slot(dev) * EISA_SLOT_SIZE) + ADV_EISA_SLOT_OFFSET;
107
108         eisa_add_iospace(dev, iobase, ADV_EISA_IOSIZE, RESVADDR_NONE);
109         irq = inb(iobase + ADV_EISA_IRQ_BURST_LEN_REG);
110         irq &= ADV_EISA_IRQ_MASK;
111         switch (irq) {
112         case 0:
113         case 1:
114         case 2:
115         case 4:
116         case 5:
117             break;
118         default:
119             kprintf("adv at slot %d: illegal "
120                    "irq setting %d\n", eisa_get_slot(dev),
121                    irq);
122             return ENXIO;
123         }
124         eisa_add_intr(dev, irq + 10, EISA_TRIGGER_LEVEL);
125
126         return 0;
127 }
128
129 static int
130 adv_eisa_attach(device_t dev)
131 {
132         struct adv_softc *adv;
133         struct adv_softc *adv_b;
134         struct resource *io;
135         struct resource *irq;
136         int rid, error;
137         void *ih;
138
139         adv_b = NULL;
140
141         rid = 0;
142         io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
143                                 0, ~0, 1, RF_ACTIVE);
144         if (!io) {
145                 device_printf(dev, "No I/O space?!\n");
146                 return ENOMEM;
147         }
148
149         rid = 0;
150         irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
151                                  0, ~0, 1, RF_SHAREABLE | RF_ACTIVE);
152         if (!irq) {
153                 device_printf(dev, "No irq?!\n");
154                 bus_release_resource(dev, SYS_RES_IOPORT, 0, io);
155                 return ENOMEM;
156
157         }
158
159         switch (eisa_get_id(dev) & ~0xF) {
160         case EISA_DEVICE_ID_ADVANSYS_750:
161                 adv_b = adv_alloc(dev, rman_get_bustag(io),
162                                   rman_get_bushandle(io) + ADV_EISA_OFFSET_CHAN2);
163                 if (adv_b == NULL)
164                         goto bad;
165                 
166                 /*
167                  * Allocate a parent dmatag for all tags created
168                  * by the MI portions of the advansys driver
169                  */
170                 /* XXX Should be a child of the PCI bus dma tag */
171                 error = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1,
172                                            /*boundary*/0,
173                                            /*lowaddr*/ADV_EISA_MAX_DMA_ADDR,
174                                            /*highaddr*/BUS_SPACE_MAXADDR,
175                                            /*filter*/NULL, /*filterarg*/NULL,
176                                            /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
177                                            /*nsegments*/BUS_SPACE_UNRESTRICTED,
178                                            /*maxsegsz*/ADV_EISA_MAX_DMA_COUNT,
179                                            /*flags*/0,
180                                            &adv_b->parent_dmat);
181  
182                 if (error != 0) {
183                         kprintf("%s: Could not allocate DMA tag - error %d\n",
184                                adv_name(adv_b), error);
185                         adv_free(adv_b);
186                         goto bad;
187                 }
188
189                 adv_b->init_level++;
190
191                 /* FALLTHROUGH */
192         case EISA_DEVICE_ID_ADVANSYS_740:
193                 adv = adv_alloc(dev, rman_get_bustag(io),
194                                 rman_get_bushandle(io) + ADV_EISA_OFFSET_CHAN1);
195                 if (adv == NULL) {
196                         if (adv_b != NULL)
197                                 adv_free(adv_b);
198                         goto bad;
199                 }
200
201                 /*
202                  * Allocate a parent dmatag for all tags created
203                  * by the MI portions of the advansys driver
204                  */
205                 /* XXX Should be a child of the PCI bus dma tag */
206                 error = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1,
207                                            /*boundary*/0,
208                                            /*lowaddr*/ADV_EISA_MAX_DMA_ADDR,
209                                            /*highaddr*/BUS_SPACE_MAXADDR,
210                                            /*filter*/NULL, /*filterarg*/NULL,
211                                            /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
212                                            /*nsegments*/BUS_SPACE_UNRESTRICTED,
213                                            /*maxsegsz*/ADV_EISA_MAX_DMA_COUNT,
214                                            /*flags*/0,
215                                            &adv->parent_dmat);
216  
217                 if (error != 0) {
218                         kprintf("%s: Could not allocate DMA tag - error %d\n",
219                                adv_name(adv), error);
220                         adv_free(adv);
221                         goto bad;
222                 }
223
224                 adv->init_level++;
225                 break;
226         default: 
227                 kprintf("adveisaattach: Unknown device type!\n");
228                 goto bad;
229                 break;
230         }
231
232         if (overrun_buf == NULL) {
233                 /* Need to allocate our overrun buffer */
234                 if (bus_dma_tag_create(adv->parent_dmat,
235                                        /*alignment*/8,
236                                        /*boundary*/0,
237                                        ADV_EISA_MAX_DMA_ADDR,
238                                        BUS_SPACE_MAXADDR,
239                                        /*filter*/NULL,
240                                        /*filterarg*/NULL,
241                                        ADV_OVERRUN_BSIZE,
242                                        /*nsegments*/1,
243                                        BUS_SPACE_MAXSIZE_32BIT,
244                                        /*flags*/0,
245                                        &overrun_dmat) != 0) {
246                         adv_free(adv);
247                         goto bad;
248                 }
249                 if (bus_dmamem_alloc(overrun_dmat,
250                                      (void **)&overrun_buf,
251                                      BUS_DMA_NOWAIT,
252                                      &overrun_dmamap) != 0) {
253                         bus_dma_tag_destroy(overrun_dmat);
254                         adv_free(adv);
255                         goto bad;
256                 }
257                 /* And permanently map it in */  
258                 bus_dmamap_load(overrun_dmat, overrun_dmamap,
259                                 overrun_buf, ADV_OVERRUN_BSIZE,
260                                 adv_map, &overrun_physbase,
261                                 /*flags*/0);
262         }
263         
264         /*
265          * Now that we know we own the resources we need, do the 
266          * card initialization.
267          */
268
269         /*
270          * Stop the chip.
271          */
272         ADV_OUTB(adv, ADV_CHIP_CTRL, ADV_CC_HALT);
273         ADV_OUTW(adv, ADV_CHIP_STATUS, 0);
274
275         adv->chip_version = EISA_REVISION_ID(eisa_get_id(dev))
276                           + ADV_CHIP_MIN_VER_EISA - 1;
277
278         if (adv_init(adv) != 0) {
279                 adv_free(adv);
280                 if (adv_b != NULL)
281                         adv_free(adv_b);
282                 return(-1);
283         }
284
285         adv->max_dma_count = ADV_EISA_MAX_DMA_COUNT;
286         adv->max_dma_addr = ADV_EISA_MAX_DMA_ADDR;
287
288         if (adv_b != NULL) {
289                 /*
290                  * Stop the chip.
291                  */
292                 ADV_OUTB(adv_b, ADV_CHIP_CTRL, ADV_CC_HALT);
293                 ADV_OUTW(adv_b, ADV_CHIP_STATUS, 0);
294
295                 adv_b->chip_version = EISA_REVISION_ID(eisa_get_id(dev))
296                                     + ADV_CHIP_MIN_VER_EISA - 1;
297
298                 if (adv_init(adv_b) != 0) {
299                         adv_free(adv_b);
300                 } else {
301                         adv_b->max_dma_count = ADV_EISA_MAX_DMA_COUNT;
302                         adv_b->max_dma_addr = ADV_EISA_MAX_DMA_ADDR;
303                 }
304         }
305
306         /*
307          * Enable our interrupt handler.
308          */
309         bus_setup_intr(dev, irq, 0, adv_intr, adv, &ih, NULL);
310
311         /* Attach sub-devices - always succeeds */
312         adv_attach(adv);
313         if (adv_b != NULL)
314                 adv_attach(adv_b);
315
316         return 0;
317
318  bad:
319         bus_release_resource(dev, SYS_RES_IOPORT, 0, io);
320         bus_release_resource(dev, SYS_RES_IRQ, 0, irq);
321         return -1;
322 }
323
324 static device_method_t adv_eisa_methods[] = {
325         /* Device interface */
326         DEVMETHOD(device_probe,         adv_eisa_probe),
327         DEVMETHOD(device_attach,        adv_eisa_attach),
328         { 0, 0 }
329 };
330
331 static driver_t adv_eisa_driver = {
332         "adv", adv_eisa_methods, sizeof(struct adv_softc)
333 };
334
335 static devclass_t adv_eisa_devclass;
336 DRIVER_MODULE(adv, eisa, adv_eisa_driver, adv_eisa_devclass, 0, 0);