kernel: Use NULL for DRIVER_MODULE()'s evh & arg (which are pointers).
[dragonfly.git] / sys / dev / disk / advansys / adv_isa.c
1 /*
2  * Device probe and attach routines for the following
3  * Advanced Systems Inc. SCSI controllers:
4  *
5  *   Connectivity Products:
6  *      ABP510/5150 - Bus-Master ISA (240 CDB) *
7  *      ABP5140 - Bus-Master ISA PnP (16 CDB) * **
8  *      ABP5142 - Bus-Master ISA PnP with floppy (16 CDB) ***
9  *
10  *   Single Channel Products:
11  *      ABP542 - Bus-Master ISA with floppy (240 CDB)
12  *      ABP842 - Bus-Master VL (240 CDB) 
13  *
14  *   Dual Channel Products:  
15  *      ABP852 - Dual Channel Bus-Master VL (240 CDB Per Channel)
16  *
17  *    * This board has been shipped by HP with the 4020i CD-R drive.
18  *      The board has no BIOS so it cannot control a boot device, but 
19  *      it can control any secondary SCSI device.
20  *   ** This board has been sold by SIIG as the i540 SpeedMaster.
21  *  *** This board has been sold by SIIG as the i542 SpeedMaster.
22  *
23  * Copyright (c) 1996, 1997 Justin T. Gibbs.
24  * All rights reserved.
25  *
26  * Redistribution and use in source and binary forms, with or without
27  * modification, are permitted provided that the following conditions
28  * are met:
29  * 1. Redistributions of source code must retain the above copyright
30  *    notice, this list of conditions, and the following disclaimer,
31  *    without modification, immediately at the beginning of the file.
32  * 2. The name of the author may not be used to endorse or promote products
33  *    derived from this software without specific prior written permission.
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
36  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
39  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45  * SUCH DAMAGE.
46  *
47  * $FreeBSD: src/sys/dev/advansys/adv_isa.c,v 1.14.2.5 2002/01/06 21:21:42 dwmalone Exp $
48  */
49
50 #include <sys/param.h>
51 #include <sys/systm.h> 
52 #include <sys/kernel.h> 
53 #include <sys/bus.h> 
54 #include <sys/rman.h> 
55
56 #include <bus/isa/isavar.h>
57
58 #include "advansys.h"
59
60 #include <bus/cam/scsi/scsi_all.h>
61
62 #define ADV_ISA_MAX_DMA_ADDR    (0x00FFFFFFL)
63 #define ADV_ISA_MAX_DMA_COUNT   (0x00FFFFFFL)
64
65 #define ADV_VL_MAX_DMA_ADDR     (0x07FFFFFFL)
66 #define ADV_VL_MAX_DMA_COUNT    (0x07FFFFFFL)
67
68 /*
69  * The overrun buffer shared amongst all ISA/VL adapters.
70  */
71 static  u_int8_t*       overrun_buf;
72 static  bus_dma_tag_t   overrun_dmat;
73 static  bus_dmamap_t    overrun_dmamap;
74 static  bus_addr_t      overrun_physbase;
75
76 /* Possible port addresses an ISA or VL adapter can live at */
77 static u_int16_t adv_isa_ioports[] =
78 {
79         0x100,
80         0x110,  /* First selection in BIOS setup */
81         0x120,
82         0x130,  /* Second selection in BIOS setup */
83         0x140,
84         0x150,  /* Third selection in BIOS setup */
85         0x190,  /* Fourth selection in BIOS setup */
86         0x210,  /* Fifth selection in BIOS setup */
87         0x230,  /* Sixth selection in BIOS setup */
88         0x250,  /* Seventh selection in BIOS setup */
89         0x330   /* Eighth and default selection in BIOS setup */
90 };
91
92 #define MAX_ISA_IOPORT_INDEX (sizeof(adv_isa_ioports)/sizeof(u_int16_t) - 1)
93
94 static  int     adv_isa_probe(device_t dev);
95 static  int     adv_isa_attach(device_t dev);
96 static  void    adv_set_isapnp_wait_for_key(void);
97 static  int     adv_get_isa_dma_channel(struct adv_softc *adv);
98 static  int     adv_set_isa_dma_settings(struct adv_softc *adv);
99
100 static int
101 adv_isa_probe(device_t dev)
102 {
103         int     port_index;
104         int     max_port_index;
105         u_long  iobase, iocount, irq;
106         int     user_iobase = 0;
107         int     rid = 0;
108         void    *ih;
109         struct resource *iores, *irqres;
110
111         /*
112          * Default to scanning all possible device locations.
113          */
114         port_index = 0;
115         max_port_index = MAX_ISA_IOPORT_INDEX;
116
117         if (bus_get_resource(dev, SYS_RES_IOPORT, 0, &iobase, &iocount) == 0) {
118                 user_iobase = 1;
119                 for (;port_index <= max_port_index; port_index++)
120                         if (iobase <= adv_isa_ioports[port_index])
121                                 break;
122                 if ((port_index > max_port_index)
123                  || (iobase != adv_isa_ioports[port_index])) {
124                         if (bootverbose)
125                             kprintf("adv%d: Invalid baseport of 0x%lx specified. "
126                                 "Nearest valid baseport is 0x%x.  Failing "
127                                 "probe.\n", device_get_unit(dev), iobase,
128                                 (port_index <= max_port_index) ?
129                                         adv_isa_ioports[port_index] :
130                                         adv_isa_ioports[max_port_index]);
131                         return ENXIO;
132                 }
133                 max_port_index = port_index;
134         }
135
136         /* Perform the actual probing */
137         adv_set_isapnp_wait_for_key();
138         for (;port_index <= max_port_index; port_index++) {
139                 u_int16_t port_addr = adv_isa_ioports[port_index];
140                 bus_size_t maxsegsz;
141                 bus_size_t maxsize;
142                 bus_addr_t lowaddr;
143                 int error;
144                 struct adv_softc *adv;
145
146                 if (port_addr == 0)
147                         /* Already been attached */
148                         continue;
149                 
150                 if (bus_set_resource(dev, SYS_RES_IOPORT, 0, port_addr, 1))
151                         continue;
152
153                 /* XXX what is the real portsize? */
154                 iores = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1,
155                                            RF_ACTIVE);
156                 if (iores == NULL)
157                         continue;
158
159                 if (adv_find_signature(rman_get_bustag(iores),
160                                        rman_get_bushandle(iores)) == 0) {
161                         bus_release_resource(dev, SYS_RES_IOPORT, 0, iores);
162                         continue;
163                 }
164
165                 /*
166                  * Got one.  Now allocate our softc
167                  * and see if we can initialize the card.
168                  */
169                 adv = adv_alloc(dev, rman_get_bustag(iores),
170                                 rman_get_bushandle(iores));
171                 if (adv == NULL) {
172                         bus_release_resource(dev, SYS_RES_IOPORT, 0, iores);
173                         break;
174                 }
175
176                 /*
177                  * Stop the chip.
178                  */
179                 ADV_OUTB(adv, ADV_CHIP_CTRL, ADV_CC_HALT);
180                 ADV_OUTW(adv, ADV_CHIP_STATUS, 0);
181                 /*
182                  * Determine the chip version.
183                  */
184                 adv->chip_version = ADV_INB(adv, ADV_NONEISA_CHIP_REVISION);
185                 if ((adv->chip_version >= ADV_CHIP_MIN_VER_VL)
186                     && (adv->chip_version <= ADV_CHIP_MAX_VER_VL)) {
187                         adv->type = ADV_VL;
188                         maxsegsz = ADV_VL_MAX_DMA_COUNT;
189                         maxsize = BUS_SPACE_MAXSIZE_32BIT;
190                         lowaddr = ADV_VL_MAX_DMA_ADDR;
191                         bus_delete_resource(dev, SYS_RES_DRQ, 0);
192                 } else if ((adv->chip_version >= ADV_CHIP_MIN_VER_ISA)
193                            && (adv->chip_version <= ADV_CHIP_MAX_VER_ISA)) {
194                         if (adv->chip_version >= ADV_CHIP_MIN_VER_ISA_PNP) {
195                                 adv->type = ADV_ISAPNP;
196                                 ADV_OUTB(adv, ADV_REG_IFC,
197                                          ADV_IFC_INIT_DEFAULT);
198                         } else {
199                                 adv->type = ADV_ISA;
200                         }
201                         maxsegsz = ADV_ISA_MAX_DMA_COUNT;
202                         maxsize = BUS_SPACE_MAXSIZE_24BIT;
203                         lowaddr = ADV_ISA_MAX_DMA_ADDR;
204                         adv->isa_dma_speed = ADV_DEF_ISA_DMA_SPEED;
205                         adv->isa_dma_channel = adv_get_isa_dma_channel(adv);
206                         bus_set_resource(dev, SYS_RES_DRQ, 0,
207                                          adv->isa_dma_channel, 1);
208                 } else {
209                         panic("advisaprobe: Unknown card revision\n");
210                 }
211
212                 /*
213                  * Allocate a parent dmatag for all tags created
214                  * by the MI portions of the advansys driver
215                  */
216                 /* XXX Should be a child of the ISA bus dma tag */ 
217                 error = bus_dma_tag_create(/*parent*/NULL,
218                                            /*alignemnt*/1,
219                                            /*boundary*/0,
220                                            lowaddr,
221                                            /*highaddr*/BUS_SPACE_MAXADDR,
222                                            /*filter*/NULL,
223                                            /*filterarg*/NULL,
224                                            maxsize,
225                                            /*nsegs*/BUS_SPACE_UNRESTRICTED,
226                                            maxsegsz,
227                                            /*flags*/0,
228                                            &adv->parent_dmat); 
229
230                 if (error != 0) {
231                         kprintf("%s: Could not allocate DMA tag - error %d\n",
232                                adv_name(adv), error); 
233                         adv_free(adv); 
234                         bus_release_resource(dev, SYS_RES_IOPORT, 0, iores);
235                         break;
236                 }
237
238                 adv->init_level += 2;
239
240                 if (overrun_buf == NULL) {
241                         /* Need to allocate our overrun buffer */
242                         if (bus_dma_tag_create(adv->parent_dmat,
243                                                /*alignment*/8,
244                                                /*boundary*/0,
245                                                ADV_ISA_MAX_DMA_ADDR,
246                                                BUS_SPACE_MAXADDR,
247                                                /*filter*/NULL,
248                                                /*filterarg*/NULL,
249                                                ADV_OVERRUN_BSIZE,
250                                                /*nsegments*/1,
251                                                BUS_SPACE_MAXSIZE_32BIT,
252                                                /*flags*/0,
253                                                &overrun_dmat) != 0) {
254                                 adv_free(adv);
255                                 bus_release_resource(dev, SYS_RES_IOPORT, 0,
256                                                      iores);
257                                 break;
258                         }
259                         if (bus_dmamem_alloc(overrun_dmat,
260                                              (void **)&overrun_buf,
261                                              BUS_DMA_NOWAIT,
262                                              &overrun_dmamap) != 0) {
263                                 bus_dma_tag_destroy(overrun_dmat);
264                                 adv_free(adv);
265                                 bus_release_resource(dev, SYS_RES_IOPORT, 0,
266                                                      iores);
267                                 break;
268                         }
269                         /* And permanently map it in */  
270                         bus_dmamap_load(overrun_dmat, overrun_dmamap,
271                                         overrun_buf, ADV_OVERRUN_BSIZE,
272                                         adv_map, &overrun_physbase,
273                                         /*flags*/0);
274                 }
275
276                 adv->overrun_physbase = overrun_physbase;
277
278                 if (adv_init(adv) != 0) {
279                         bus_dmamap_unload(overrun_dmat, overrun_dmamap);
280                         bus_dmamem_free(overrun_dmat, overrun_buf,
281                             overrun_dmamap);
282                         bus_dma_tag_destroy(overrun_dmat);
283                         adv_free(adv);
284                         bus_release_resource(dev, SYS_RES_IOPORT, 0, iores);
285                         break;
286                 }
287
288                 switch (adv->type) {
289                 case ADV_ISAPNP:
290                         if (adv->chip_version == ADV_CHIP_VER_ASYN_BUG) {
291                                 adv->bug_fix_control
292                                     |= ADV_BUG_FIX_ASYN_USE_SYN;
293                                 adv->fix_asyn_xfer = ~0;
294                         }
295                         /* Fall Through */
296                 case ADV_ISA:
297                         adv->max_dma_count = ADV_ISA_MAX_DMA_COUNT;
298                         adv->max_dma_addr = ADV_ISA_MAX_DMA_ADDR;
299                         adv_set_isa_dma_settings(adv);
300                         break;
301
302                 case ADV_VL:
303                         adv->max_dma_count = ADV_VL_MAX_DMA_COUNT;
304                         adv->max_dma_addr = ADV_VL_MAX_DMA_ADDR;
305                         break;
306                 default:
307                         panic("advisaprobe: Invalid card type\n");
308                 }
309                         
310                 /* Determine our IRQ */
311                 if (bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, NULL))
312                         bus_set_resource(dev, SYS_RES_IRQ, 0,
313                                          adv_get_chip_irq(adv), 1);
314                 else
315                         adv_set_chip_irq(adv, irq);
316
317                 irqres = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
318                                             RF_ACTIVE);
319                 if (irqres == NULL ||
320                     bus_setup_intr(dev, irqres, 0, adv_intr, adv,
321                                    &ih, NULL)) {
322                         bus_dmamap_unload(overrun_dmat, overrun_dmamap);
323                         bus_dmamem_free(overrun_dmat, overrun_buf,
324                             overrun_dmamap);
325                         bus_dma_tag_destroy(overrun_dmat);
326                         adv_free(adv);
327                         bus_release_resource(dev, SYS_RES_IOPORT, 0, iores);
328                         break;
329                 }
330
331                 /* Mark as probed */
332                 adv_isa_ioports[port_index] = 0;
333                 return 0;
334         }
335
336         if (user_iobase)
337                 bus_set_resource(dev, SYS_RES_IOPORT, 0, iobase, iocount);
338         else
339                 bus_delete_resource(dev, SYS_RES_IOPORT, 0);
340
341         return ENXIO;
342 }
343
344 static int
345 adv_isa_attach(device_t dev)
346 {
347         struct adv_softc *adv = device_get_softc(dev);
348
349         return (adv_attach(adv));
350 }
351
352 static int
353 adv_get_isa_dma_channel(struct adv_softc *adv)
354 {
355         int channel;
356
357         channel = ADV_INW(adv, ADV_CONFIG_LSW) & ADV_CFG_LSW_ISA_DMA_CHANNEL;  
358         if (channel == 0x03)
359                 return (0);
360         else if (channel == 0x00)
361                 return (7);
362         return (channel + 4);
363 }
364
365 static int
366 adv_set_isa_dma_settings(struct adv_softc *adv)
367 {
368         u_int16_t cfg_lsw;
369         u_int8_t  value;
370
371         if ((adv->isa_dma_channel >= 5) && (adv->isa_dma_channel <= 7)) { 
372                 if (adv->isa_dma_channel == 7)
373                         value = 0x00;
374                 else     
375                         value = adv->isa_dma_channel - 4;
376                 cfg_lsw = ADV_INW(adv, ADV_CONFIG_LSW)
377                         & ~ADV_CFG_LSW_ISA_DMA_CHANNEL;  
378                 cfg_lsw |= value;
379                 ADV_OUTW(adv, ADV_CONFIG_LSW, cfg_lsw);
380
381                 adv->isa_dma_speed &= 0x07;
382                 adv_set_bank(adv, 1);
383                 ADV_OUTB(adv, ADV_DMA_SPEED, adv->isa_dma_speed);
384                 adv_set_bank(adv, 0);
385                 isa_dmacascade(adv->isa_dma_channel);
386         }
387         return (0);
388 }
389
390 static void
391 adv_set_isapnp_wait_for_key(void)
392 {
393         static  int isapnp_wait_set = 0;
394         if (isapnp_wait_set == 0) {
395                 outb(ADV_ISA_PNP_PORT_ADDR, 0x02);
396                 outb(ADV_ISA_PNP_PORT_WRITE, 0x02);
397                 isapnp_wait_set++;
398         }
399 }
400
401 static device_method_t adv_isa_methods[] = {
402         /* Device interface */
403         DEVMETHOD(device_probe,         adv_isa_probe),
404         DEVMETHOD(device_attach,        adv_isa_attach),
405         { 0, 0 }
406 };
407
408 static driver_t adv_isa_driver = {
409         "adv", adv_isa_methods, sizeof(struct adv_softc)
410 };
411
412 static devclass_t adv_isa_devclass;
413 DRIVER_MODULE(adv, isa, adv_isa_driver, adv_isa_devclass, NULL, NULL);