Merge branch 'vendor/AWK'
[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  * $DragonFly: src/sys/dev/disk/buslogic/bt_isa.c,v 1.5 2006/12/22 23:26:16 swildner Exp $
31  */
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/bus.h>
38 #include <sys/rman.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);
172                 bus_set_resource(dev, SYS_RES_IRQ, 0, info.irq, 1);
173
174                 return (0);
175         }
176
177         return (ENXIO);
178 }
179
180 /*
181  * Attach all the sub-devices we can find
182  */
183 static int
184 bt_isa_attach(device_t dev)
185 {
186         struct  bt_softc *bt = device_get_softc(dev);
187         bus_dma_filter_t *filter;
188         void             *filter_arg;
189         bus_addr_t       lowaddr;
190         int              error, drq;
191
192         /* Initialise softc */
193         error = bt_isa_alloc_resources(dev, 0, ~0);
194         if (error) {
195                 device_printf(dev, "can't allocate resources in bt_isa_attach\n");
196                 return error;
197         }
198
199         /* Program the DMA channel for external control */
200         if ((drq = isa_get_drq(dev)) != -1)
201                 isa_dmacascade(drq);
202
203         /* Allocate our parent dmatag */
204         filter = NULL;
205         filter_arg = NULL;
206         lowaddr = BUS_SPACE_MAXADDR_24BIT;
207
208         /* XXX Should be a child of the ISA bus dma tag */
209         if (bus_dma_tag_create(/*parent*/NULL, /*alignemnt*/1, /*boundary*/0,
210                                lowaddr, /*highaddr*/BUS_SPACE_MAXADDR,
211                                filter, filter_arg,
212                                /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
213                                /*nsegments*/BUS_SPACE_UNRESTRICTED,
214                                /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
215                                /*flags*/0, &bt->parent_dmat) != 0) {
216                 bt_isa_release_resources(dev);
217                 return (ENOMEM);
218         }                              
219
220         error = bt_init(dev);
221         if (error) {
222                 bt_isa_release_resources(dev);
223                 return (ENOMEM);
224         }
225
226         if (lowaddr != BUS_SPACE_MAXADDR_32BIT) {
227                 /* DMA tag for our sense buffers */
228                 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1,
229                                        /*boundary*/0,
230                                        /*lowaddr*/BUS_SPACE_MAXADDR,
231                                        /*highaddr*/BUS_SPACE_MAXADDR,
232                                        /*filter*/NULL, /*filterarg*/NULL,
233                                        bt->max_ccbs
234                                            * sizeof(struct scsi_sense_data),
235                                        /*nsegments*/1,
236                                        /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
237                                        /*flags*/0, &bt->sense_dmat) != 0) {
238                         bt_isa_release_resources(dev);
239                         return (ENOMEM);
240                 }
241
242                 bt->init_level++;
243
244                 /* Allocation of sense buffers */
245                 if (bus_dmamem_alloc(bt->sense_dmat,
246                                      (void *)&bt->sense_buffers,
247                                      BUS_DMA_NOWAIT, &bt->sense_dmamap) != 0) {
248                         bt_isa_release_resources(dev);
249                         return (ENOMEM);
250                 }
251
252                 bt->init_level++;
253
254                 /* And permanently map them */
255                 bus_dmamap_load(bt->sense_dmat, bt->sense_dmamap,
256                                 bt->sense_buffers,
257                                 bt->max_ccbs * sizeof(*bt->sense_buffers),
258                                 btmapsensebuffers, bt, /*flags*/0);
259
260                 bt->init_level++;
261         }
262
263         error = bt_attach(dev);
264         if (error) {
265                 bt_isa_release_resources(dev);
266                 return (error);
267         }
268
269         return (0);
270 }
271
272 #define BIOS_MAP_SIZE (16 * 1024)
273
274 static void
275 btmapsensebuffers(void *arg, bus_dma_segment_t *segs, int nseg, int error)
276 {
277         struct bt_softc* bt;
278
279         bt = (struct bt_softc*)arg;
280         bt->sense_buffers_physbase = segs->ds_addr;
281 }
282
283 static device_method_t bt_isa_methods[] = {
284         /* Device interface */
285         DEVMETHOD(device_probe,         bt_isa_probe),
286         DEVMETHOD(device_attach,        bt_isa_attach),
287
288         { 0, 0 }
289 };
290
291 static driver_t bt_isa_driver = {
292         "bt",
293         bt_isa_methods,
294         sizeof(struct bt_softc),
295 };
296
297 static devclass_t bt_devclass;
298
299 DRIVER_MODULE(bt, isa, bt_isa_driver, bt_devclass, 0, 0);