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