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