Merge from vendor branch GROFF:
[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  * $DragonFly: src/sys/dev/disk/advansys/adv_isa.c,v 1.4 2005/05/24 20:58:59 dillon Exp $
49  */
50
51 #include <sys/param.h>
52 #include <sys/systm.h> 
53 #include <sys/kernel.h> 
54
55 #include <machine/bus_pio.h>
56 #include <machine/bus.h>
57 #include <machine/resource.h>
58 #include <sys/bus.h> 
59 #include <sys/rman.h> 
60
61 #include <bus/isa/isavar.h>
62
63 #include "advansys.h"
64
65 #include <bus/cam/scsi/scsi_all.h>
66
67 #define ADV_ISA_MAX_DMA_ADDR    (0x00FFFFFFL)
68 #define ADV_ISA_MAX_DMA_COUNT   (0x00FFFFFFL)
69
70 #define ADV_VL_MAX_DMA_ADDR     (0x07FFFFFFL)
71 #define ADV_VL_MAX_DMA_COUNT    (0x07FFFFFFL)
72
73 /*
74  * The overrun buffer shared amongst all ISA/VL adapters.
75  */
76 static  u_int8_t*       overrun_buf;
77 static  bus_dma_tag_t   overrun_dmat;
78 static  bus_dmamap_t    overrun_dmamap;
79 static  bus_addr_t      overrun_physbase;
80
81 /* Possible port addresses an ISA or VL adapter can live at */
82 static u_int16_t adv_isa_ioports[] =
83 {
84         0x100,
85         0x110,  /* First selection in BIOS setup */
86         0x120,
87         0x130,  /* Second selection in BIOS setup */
88         0x140,
89         0x150,  /* Third selection in BIOS setup */
90         0x190,  /* Fourth selection in BIOS setup */
91         0x210,  /* Fifth selection in BIOS setup */
92         0x230,  /* Sixth selection in BIOS setup */
93         0x250,  /* Seventh selection in BIOS setup */
94         0x330   /* Eighth and default selection in BIOS setup */
95 };
96
97 #define MAX_ISA_IOPORT_INDEX (sizeof(adv_isa_ioports)/sizeof(u_int16_t) - 1)
98
99 static  int     adv_isa_probe(device_t dev);
100 static  int     adv_isa_attach(device_t dev);
101 static  void    adv_set_isapnp_wait_for_key(void);
102 static  int     adv_get_isa_dma_channel(struct adv_softc *adv);
103 static  int     adv_set_isa_dma_settings(struct adv_softc *adv);
104
105 static int
106 adv_isa_probe(device_t dev)
107 {
108         int     port_index;
109         int     max_port_index;
110         u_long  iobase, iocount, irq;
111         int     user_iobase = 0;
112         int     rid = 0;
113         void    *ih;
114         struct resource *iores, *irqres;
115
116         /*
117          * Default to scanning all possible device locations.
118          */
119         port_index = 0;
120         max_port_index = MAX_ISA_IOPORT_INDEX;
121
122         if (bus_get_resource(dev, SYS_RES_IOPORT, 0, &iobase, &iocount) == 0) {
123                 user_iobase = 1;
124                 for (;port_index <= max_port_index; port_index++)
125                         if (iobase <= adv_isa_ioports[port_index])
126                                 break;
127                 if ((port_index > max_port_index)
128                  || (iobase != adv_isa_ioports[port_index])) {
129                         if (bootverbose)
130                             printf("adv%d: Invalid baseport of 0x%lx specified. "
131                                 "Nearest valid baseport is 0x%x.  Failing "
132                                 "probe.\n", device_get_unit(dev), iobase,
133                                 (port_index <= max_port_index) ?
134                                         adv_isa_ioports[port_index] :
135                                         adv_isa_ioports[max_port_index]);
136                         return ENXIO;
137                 }
138                 max_port_index = port_index;
139         }
140
141         /* Perform the actual probing */
142         adv_set_isapnp_wait_for_key();
143         for (;port_index <= max_port_index; port_index++) {
144                 u_int16_t port_addr = adv_isa_ioports[port_index];
145                 bus_size_t maxsegsz;
146                 bus_size_t maxsize;
147                 bus_addr_t lowaddr;
148                 int error;
149                 struct adv_softc *adv;
150
151                 if (port_addr == 0)
152                         /* Already been attached */
153                         continue;
154                 
155                 if (bus_set_resource(dev, SYS_RES_IOPORT, 0, port_addr, 1))
156                         continue;
157
158                 /* XXX what is the real portsize? */
159                 iores = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1,
160                                            RF_ACTIVE);
161                 if (iores == NULL)
162                         continue;
163
164                 if (adv_find_signature(rman_get_bustag(iores),
165                                        rman_get_bushandle(iores)) == 0) {
166                         bus_release_resource(dev, SYS_RES_IOPORT, 0, iores);
167                         continue;
168                 }
169
170                 /*
171                  * Got one.  Now allocate our softc
172                  * and see if we can initialize the card.
173                  */
174                 adv = adv_alloc(dev, rman_get_bustag(iores),
175                                 rman_get_bushandle(iores));
176                 if (adv == NULL) {
177                         bus_release_resource(dev, SYS_RES_IOPORT, 0, iores);
178                         break;
179                 }
180
181                 /*
182                  * Stop the chip.
183                  */
184                 ADV_OUTB(adv, ADV_CHIP_CTRL, ADV_CC_HALT);
185                 ADV_OUTW(adv, ADV_CHIP_STATUS, 0);
186                 /*
187                  * Determine the chip version.
188                  */
189                 adv->chip_version = ADV_INB(adv, ADV_NONEISA_CHIP_REVISION);
190                 if ((adv->chip_version >= ADV_CHIP_MIN_VER_VL)
191                     && (adv->chip_version <= ADV_CHIP_MAX_VER_VL)) {
192                         adv->type = ADV_VL;
193                         maxsegsz = ADV_VL_MAX_DMA_COUNT;
194                         maxsize = BUS_SPACE_MAXSIZE_32BIT;
195                         lowaddr = ADV_VL_MAX_DMA_ADDR;
196                         bus_delete_resource(dev, SYS_RES_DRQ, 0);
197                 } else if ((adv->chip_version >= ADV_CHIP_MIN_VER_ISA)
198                            && (adv->chip_version <= ADV_CHIP_MAX_VER_ISA)) {
199                         if (adv->chip_version >= ADV_CHIP_MIN_VER_ISA_PNP) {
200                                 adv->type = ADV_ISAPNP;
201                                 ADV_OUTB(adv, ADV_REG_IFC,
202                                          ADV_IFC_INIT_DEFAULT);
203                         } else {
204                                 adv->type = ADV_ISA;
205                         }
206                         maxsegsz = ADV_ISA_MAX_DMA_COUNT;
207                         maxsize = BUS_SPACE_MAXSIZE_24BIT;
208                         lowaddr = ADV_ISA_MAX_DMA_ADDR;
209                         adv->isa_dma_speed = ADV_DEF_ISA_DMA_SPEED;
210                         adv->isa_dma_channel = adv_get_isa_dma_channel(adv);
211                         bus_set_resource(dev, SYS_RES_DRQ, 0,
212                                          adv->isa_dma_channel, 1);
213                 } else {
214                         panic("advisaprobe: Unknown card revision\n");
215                 }
216
217                 /*
218                  * Allocate a parent dmatag for all tags created
219                  * by the MI portions of the advansys driver
220                  */
221                 /* XXX Should be a child of the ISA bus dma tag */ 
222                 error = bus_dma_tag_create(/*parent*/NULL,
223                                            /*alignemnt*/1,
224                                            /*boundary*/0,
225                                            lowaddr,
226                                            /*highaddr*/BUS_SPACE_MAXADDR,
227                                            /*filter*/NULL,
228                                            /*filterarg*/NULL,
229                                            maxsize,
230                                            /*nsegs*/BUS_SPACE_UNRESTRICTED,
231                                            maxsegsz,
232                                            /*flags*/0,
233                                            &adv->parent_dmat); 
234
235                 if (error != 0) {
236                         printf("%s: Could not allocate DMA tag - error %d\n",
237                                adv_name(adv), error); 
238                         adv_free(adv); 
239                         bus_release_resource(dev, SYS_RES_IOPORT, 0, iores);
240                         break;
241                 }
242
243                 adv->init_level += 2;
244
245                 if (overrun_buf == NULL) {
246                         /* Need to allocate our overrun buffer */
247                         if (bus_dma_tag_create(adv->parent_dmat,
248                                                /*alignment*/8,
249                                                /*boundary*/0,
250                                                ADV_ISA_MAX_DMA_ADDR,
251                                                BUS_SPACE_MAXADDR,
252                                                /*filter*/NULL,
253                                                /*filterarg*/NULL,
254                                                ADV_OVERRUN_BSIZE,
255                                                /*nsegments*/1,
256                                                BUS_SPACE_MAXSIZE_32BIT,
257                                                /*flags*/0,
258                                                &overrun_dmat) != 0) {
259                                 adv_free(adv);
260                                 bus_release_resource(dev, SYS_RES_IOPORT, 0,
261                                                      iores);
262                                 break;
263                         }
264                         if (bus_dmamem_alloc(overrun_dmat,
265                                              (void **)&overrun_buf,
266                                              BUS_DMA_NOWAIT,
267                                              &overrun_dmamap) != 0) {
268                                 bus_dma_tag_destroy(overrun_dmat);
269                                 adv_free(adv);
270                                 bus_release_resource(dev, SYS_RES_IOPORT, 0,
271                                                      iores);
272                                 break;
273                         }
274                         /* And permanently map it in */  
275                         bus_dmamap_load(overrun_dmat, overrun_dmamap,
276                                         overrun_buf, ADV_OVERRUN_BSIZE,
277                                         adv_map, &overrun_physbase,
278                                         /*flags*/0);
279                 }
280
281                 adv->overrun_physbase = overrun_physbase;
282
283                 if (adv_init(adv) != 0) {
284                         bus_dmamap_unload(overrun_dmat, overrun_dmamap);
285                         bus_dmamem_free(overrun_dmat, overrun_buf,
286                             overrun_dmamap);
287                         bus_dma_tag_destroy(overrun_dmat);
288                         adv_free(adv);
289                         bus_release_resource(dev, SYS_RES_IOPORT, 0, iores);
290                         break;
291                 }
292
293                 switch (adv->type) {
294                 case ADV_ISAPNP:
295                         if (adv->chip_version == ADV_CHIP_VER_ASYN_BUG) {
296                                 adv->bug_fix_control
297                                     |= ADV_BUG_FIX_ASYN_USE_SYN;
298                                 adv->fix_asyn_xfer = ~0;
299                         }
300                         /* Fall Through */
301                 case ADV_ISA:
302                         adv->max_dma_count = ADV_ISA_MAX_DMA_COUNT;
303                         adv->max_dma_addr = ADV_ISA_MAX_DMA_ADDR;
304                         adv_set_isa_dma_settings(adv);
305                         break;
306
307                 case ADV_VL:
308                         adv->max_dma_count = ADV_VL_MAX_DMA_COUNT;
309                         adv->max_dma_addr = ADV_VL_MAX_DMA_ADDR;
310                         break;
311                 default:
312                         panic("advisaprobe: Invalid card type\n");
313                 }
314                         
315                 /* Determine our IRQ */
316                 if (bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, NULL))
317                         bus_set_resource(dev, SYS_RES_IRQ, 0,
318                                          adv_get_chip_irq(adv), 1);
319                 else
320                         adv_set_chip_irq(adv, irq);
321
322                 irqres = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
323                                             RF_ACTIVE);
324                 if (irqres == NULL ||
325                     bus_setup_intr(dev, irqres, INTR_TYPE_CAM, adv_intr, adv,
326                                    &ih, NULL)) {
327                         bus_dmamap_unload(overrun_dmat, overrun_dmamap);
328                         bus_dmamem_free(overrun_dmat, overrun_buf,
329                             overrun_dmamap);
330                         bus_dma_tag_destroy(overrun_dmat);
331                         adv_free(adv);
332                         bus_release_resource(dev, SYS_RES_IOPORT, 0, iores);
333                         break;
334                 }
335
336                 /* Mark as probed */
337                 adv_isa_ioports[port_index] = 0;
338                 return 0;
339         }
340
341         if (user_iobase)
342                 bus_set_resource(dev, SYS_RES_IOPORT, 0, iobase, iocount);
343         else
344                 bus_delete_resource(dev, SYS_RES_IOPORT, 0);
345
346         return ENXIO;
347 }
348
349 static int
350 adv_isa_attach(device_t dev)
351 {
352         struct adv_softc *adv = device_get_softc(dev);
353
354         return (adv_attach(adv));
355 }
356
357 static int
358 adv_get_isa_dma_channel(struct adv_softc *adv)
359 {
360         int channel;
361
362         channel = ADV_INW(adv, ADV_CONFIG_LSW) & ADV_CFG_LSW_ISA_DMA_CHANNEL;  
363         if (channel == 0x03)
364                 return (0);
365         else if (channel == 0x00)
366                 return (7);
367         return (channel + 4);
368 }
369
370 static int
371 adv_set_isa_dma_settings(struct adv_softc *adv)
372 {
373         u_int16_t cfg_lsw;
374         u_int8_t  value;
375
376         if ((adv->isa_dma_channel >= 5) && (adv->isa_dma_channel <= 7)) { 
377                 if (adv->isa_dma_channel == 7)
378                         value = 0x00;
379                 else     
380                         value = adv->isa_dma_channel - 4;
381                 cfg_lsw = ADV_INW(adv, ADV_CONFIG_LSW)
382                         & ~ADV_CFG_LSW_ISA_DMA_CHANNEL;  
383                 cfg_lsw |= value;
384                 ADV_OUTW(adv, ADV_CONFIG_LSW, cfg_lsw);
385
386                 adv->isa_dma_speed &= 0x07;
387                 adv_set_bank(adv, 1);
388                 ADV_OUTB(adv, ADV_DMA_SPEED, adv->isa_dma_speed);
389                 adv_set_bank(adv, 0);
390                 isa_dmacascade(adv->isa_dma_channel);
391         }
392         return (0);
393 }
394
395 static void
396 adv_set_isapnp_wait_for_key(void)
397 {
398         static  int isapnp_wait_set = 0;
399         if (isapnp_wait_set == 0) {
400                 outb(ADV_ISA_PNP_PORT_ADDR, 0x02);
401                 outb(ADV_ISA_PNP_PORT_WRITE, 0x02);
402                 isapnp_wait_set++;
403         }
404 }
405
406 static device_method_t adv_isa_methods[] = {
407         /* Device interface */
408         DEVMETHOD(device_probe,         adv_isa_probe),
409         DEVMETHOD(device_attach,        adv_isa_attach),
410         { 0, 0 }
411 };
412
413 static driver_t adv_isa_driver = {
414         "adv", adv_isa_methods, sizeof(struct adv_softc)
415 };
416
417 static devclass_t adv_isa_devclass;
418 DRIVER_MODULE(adv, isa, adv_isa_driver, adv_isa_devclass, 0, 0);