c23cfd1d228b3e366e1e0314b20d695beeb65663
[dragonfly.git] / sys / dev / disk / buslogic / bt_isa.c
1 /*
2  * Product specific probe and attach routines for:
3  *      Buslogic BT-54X and BT-445 cards
4  *
5  * Copyright (c) 1998, 1999 Justin T. Gibbs
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions, and the following disclaimer,
13  *    without modification, immediately at the beginning of the file.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/sys/dev/buslogic/bt_isa.c,v 1.18 1999/10/12 21:35:43 dfr Exp $
30  */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/rman.h>
38 #include <sys/machintr.h>
39
40 #include <bus/isa/isavar.h>
41 #include "btreg.h"
42
43 #include <bus/cam/scsi/scsi_all.h>
44
45 static  bus_dmamap_callback_t btmapsensebuffers;
46
47 static int
48 bt_isa_alloc_resources(device_t dev, u_long portstart, u_long portend)
49 {
50         int rid;
51         struct resource *port;
52         struct resource *irq;
53         struct resource *drq;
54
55         rid = 0;
56         port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
57                                   portstart, portend, BT_NREGS, RF_ACTIVE);
58         if (!port)
59                 return (ENOMEM);
60
61         if (isa_get_irq(dev) != -1) {
62                 rid = 0;
63                 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
64                                          0, ~0, 1, RF_ACTIVE);
65                 if (!irq) {
66                         if (port)
67                                 bus_release_resource(dev, SYS_RES_IOPORT,
68                                                      0, port);
69                         return (ENOMEM);
70                 }
71         } else
72                 irq = 0;
73
74         if (isa_get_drq(dev) != -1) {
75                 rid = 0;
76                 drq = bus_alloc_resource(dev, SYS_RES_DRQ, &rid,
77                                          0, ~0, 1, RF_ACTIVE);
78                 if (!drq) {
79                         if (port)
80                                 bus_release_resource(dev, SYS_RES_IOPORT,
81                                                      0, port);
82                         if (irq)
83                                 bus_release_resource(dev, SYS_RES_IRQ,
84                                                      0, irq);
85                         return (ENOMEM);
86                 }
87         } else
88                 drq = 0;
89
90         bt_init_softc(dev, port, irq, drq);
91
92         return (0);
93 }
94
95 static void
96 bt_isa_release_resources(device_t dev)
97 {
98         struct  bt_softc *bt = device_get_softc(dev);
99
100         if (bt->port)
101                 bus_release_resource(dev, SYS_RES_IOPORT, 0, bt->port);
102         if (bt->irq)
103                 bus_release_resource(dev, SYS_RES_IRQ, 0, bt->irq);
104         if (bt->drq)
105                 bus_release_resource(dev, SYS_RES_DRQ, 0, bt->drq);
106         bt_free_softc(dev);
107 }
108
109 /*
110  * Check if the device can be found at the port given
111  * and if so, set it up ready for further work
112  * as an argument, takes the isa_device structure from
113  * autoconf.c
114  */
115 static int
116 bt_isa_probe(device_t dev)
117 {
118         /*
119          * find unit and check we have that many defined
120          */
121         int     port_index;
122         int     max_port_index;
123
124         /* No pnp support */
125         if (isa_get_vendorid(dev))
126                 return (ENXIO);
127
128         port_index = 0;
129         max_port_index = BT_NUM_ISAPORTS - 1;
130         /*
131          * Bound our board search if the user has
132          * specified an exact port.
133          */
134         bt_find_probe_range(isa_get_port(dev), &port_index, &max_port_index);
135
136         if (port_index < 0)
137                 return (ENXIO);
138
139         /* Attempt to find an adapter */
140         for (;port_index <= max_port_index; port_index++) {
141                 struct bt_probe_info info;
142                 u_int ioport;
143
144                 ioport = bt_iop_from_bio(port_index);
145
146                 /*
147                  * Ensure this port has not already been claimed already
148                  * by a PCI, EISA or ISA adapter.
149                  */
150                 if (bt_check_probed_iop(ioport) != 0)
151                         continue;
152
153                 /* Initialise the softc for use during probing */
154                 if (bt_isa_alloc_resources(dev, ioport,
155                                            ioport + BT_NREGS -1) != 0)
156                         continue;
157
158                 /* We're going to attempt to probe it now, so mark it probed */
159                 bt_mark_probed_bio(port_index);
160
161                 if (bt_port_probe(dev, &info) != 0) {
162                         if (bootverbose)
163                                 kprintf("bt_isa_probe: Probe failed at 0x%x\n",
164                                        ioport);
165                         bt_isa_release_resources(dev);
166                         continue;
167                 }
168
169                 bt_isa_release_resources(dev);
170
171                 bus_set_resource(dev, SYS_RES_DRQ, 0, info.drq, 1, -1);
172                 bus_set_resource(dev, SYS_RES_IRQ, 0, info.irq, 1,
173                     machintr_intr_cpuid(info.irq));
174
175                 return (0);
176         }
177
178         return (ENXIO);
179 }
180
181 /*
182  * Attach all the sub-devices we can find
183  */
184 static int
185 bt_isa_attach(device_t dev)
186 {
187         struct  bt_softc *bt = device_get_softc(dev);
188         bus_dma_filter_t *filter;
189         void             *filter_arg;
190         bus_addr_t       lowaddr;
191         int              error, drq;
192
193         /* Initialise softc */
194         error = bt_isa_alloc_resources(dev, 0, ~0);
195         if (error) {
196                 device_printf(dev, "can't allocate resources in bt_isa_attach\n");
197                 return error;
198         }
199
200         /* Program the DMA channel for external control */
201         if ((drq = isa_get_drq(dev)) != -1)
202                 isa_dmacascade(drq);
203
204         /* Allocate our parent dmatag */
205         filter = NULL;
206         filter_arg = NULL;
207         lowaddr = BUS_SPACE_MAXADDR_24BIT;
208
209         /* XXX Should be a child of the ISA bus dma tag */
210         if (bus_dma_tag_create(/*parent*/NULL, /*alignemnt*/1, /*boundary*/0,
211                                lowaddr, /*highaddr*/BUS_SPACE_MAXADDR,
212                                filter, filter_arg,
213                                /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
214                                /*nsegments*/BUS_SPACE_UNRESTRICTED,
215                                /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
216                                /*flags*/0, &bt->parent_dmat) != 0) {
217                 bt_isa_release_resources(dev);
218                 return (ENOMEM);
219         }                              
220
221         error = bt_init(dev);
222         if (error) {
223                 bt_isa_release_resources(dev);
224                 return (ENOMEM);
225         }
226
227         if (lowaddr != BUS_SPACE_MAXADDR_32BIT) {
228                 /* DMA tag for our sense buffers */
229                 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1,
230                                        /*boundary*/0,
231                                        /*lowaddr*/BUS_SPACE_MAXADDR,
232                                        /*highaddr*/BUS_SPACE_MAXADDR,
233                                        /*filter*/NULL, /*filterarg*/NULL,
234                                        bt->max_ccbs
235                                            * sizeof(struct scsi_sense_data),
236                                        /*nsegments*/1,
237                                        /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
238                                        /*flags*/0, &bt->sense_dmat) != 0) {
239                         bt_isa_release_resources(dev);
240                         return (ENOMEM);
241                 }
242
243                 bt->init_level++;
244
245                 /* Allocation of sense buffers */
246                 if (bus_dmamem_alloc(bt->sense_dmat,
247                                      (void *)&bt->sense_buffers,
248                                      BUS_DMA_NOWAIT, &bt->sense_dmamap) != 0) {
249                         bt_isa_release_resources(dev);
250                         return (ENOMEM);
251                 }
252
253                 bt->init_level++;
254
255                 /* And permanently map them */
256                 bus_dmamap_load(bt->sense_dmat, bt->sense_dmamap,
257                                 bt->sense_buffers,
258                                 bt->max_ccbs * sizeof(*bt->sense_buffers),
259                                 btmapsensebuffers, bt, /*flags*/0);
260
261                 bt->init_level++;
262         }
263
264         error = bt_attach(dev);
265         if (error) {
266                 bt_isa_release_resources(dev);
267                 return (error);
268         }
269
270         return (0);
271 }
272
273 #define BIOS_MAP_SIZE (16 * 1024)
274
275 static void
276 btmapsensebuffers(void *arg, bus_dma_segment_t *segs, int nseg, int error)
277 {
278         struct bt_softc* bt;
279
280         bt = (struct bt_softc*)arg;
281         bt->sense_buffers_physbase = segs->ds_addr;
282 }
283
284 static device_method_t bt_isa_methods[] = {
285         /* Device interface */
286         DEVMETHOD(device_probe,         bt_isa_probe),
287         DEVMETHOD(device_attach,        bt_isa_attach),
288
289         { 0, 0 }
290 };
291
292 static driver_t bt_isa_driver = {
293         "bt",
294         bt_isa_methods,
295         sizeof(struct bt_softc),
296 };
297
298 static devclass_t bt_devclass;
299
300 DRIVER_MODULE(bt, isa, bt_isa_driver, bt_devclass, NULL, NULL);