FreeBSD/Linux Kernel Cross Reference
sys/dev/aha/aha_isa.c
1 /*
2 * Product specific probe and attach routines for:
3 * Adaptec 154x.
4 */
5 /*-
6 * Copyright (c) 1999-2003 M. Warner Losh
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification, immediately at the beginning of the file.
15 * 2. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * Derived from bt isa from end, written by:
31 *
32 * Copyright (c) 1998 Justin T. Gibbs
33 * All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions, and the following disclaimer,
40 * without modification, immediately at the beginning of the file.
41 * 2. The name of the author may not be used to endorse or promote products
42 * derived from this software without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
48 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 */
56
57 #include <sys/cdefs.h>
58 __FBSDID("$FreeBSD: src/sys/dev/aha/aha_isa.c,v 1.29.2.2 2005/03/02 09:38:19 obrien Exp $");
59
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/kernel.h>
63 #include <sys/lock.h>
64 #include <sys/mutex.h>
65
66 #include <machine/bus_pio.h>
67 #include <machine/bus.h>
68 #include <machine/resource.h>
69 #include <sys/module.h>
70 #include <sys/bus.h>
71 #include <sys/rman.h>
72
73 #include <isa/isavar.h>
74
75 #include <dev/aha/ahareg.h>
76
77 #include <cam/scsi/scsi_all.h>
78
79 static struct isa_pnp_id aha_ids[] = {
80 {ADP0100_PNP, "Adaptec 1540/1542 ISA SCSI"}, /* ADP0100 */
81 {AHA1540_PNP, "Adaptec 1540/aha-1640/aha-1535"},/* ADP1540 */
82 {AHA1542_PNP, "Adaptec 1542/aha-1535"}, /* ADP1542 */
83 {AHA1542_PNPCOMPAT, "Adaptec 1542 compatible"}, /* PNP00A0 */
84 {ICU0091_PNP, "Adaptec AHA-1540/1542 SCSI"}, /* ICU0091 */
85 {0}
86 };
87
88 /*
89 * I/O ports listed in the order enumerated by the card for certain op codes.
90 */
91 static bus_addr_t aha_board_ports[] =
92 {
93 0x330,
94 0x334,
95 0x230,
96 0x234,
97 0x130,
98 0x134
99 };
100
101 /*
102 * Check if the device can be found at the port given
103 */
104 static int
105 aha_isa_probe(device_t dev)
106 {
107 /*
108 * find unit and check we have that many defined
109 */
110 struct aha_softc *aha = device_get_softc(dev);
111 int error;
112 u_long port_start;
113 struct resource *port_res;
114 int port_rid;
115 int drq;
116 int irq;
117 config_data_t config_data;
118
119 aha->dev = dev;
120 /* Check isapnp ids */
121 if (ISA_PNP_PROBE(device_get_parent(dev), dev, aha_ids) == ENXIO)
122 return (ENXIO);
123
124 port_rid = 0;
125 port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &port_rid,
126 0, ~0, AHA_NREGS, RF_ACTIVE);
127
128 if (port_res == NULL)
129 return (ENXIO);
130
131 port_start = rman_get_start(port_res);
132 aha_alloc(aha, device_get_unit(dev), rman_get_bustag(port_res),
133 rman_get_bushandle(port_res));
134
135 /* See if there is really a card present */
136 if (aha_probe(aha) || aha_fetch_adapter_info(aha)) {
137 aha_free(aha);
138 bus_release_resource(dev, SYS_RES_IOPORT, port_rid, port_res);
139 return (ENXIO);
140 }
141
142 /*
143 * Determine our IRQ, and DMA settings and
144 * export them to the configuration system.
145 */
146 error = aha_cmd(aha, AOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
147 (uint8_t*)&config_data, sizeof(config_data), DEFAULT_CMD_TIMEOUT);
148
149 if (error != 0) {
150 device_printf(dev, "Could not determine IRQ or DMA "
151 "settings for adapter at %#jx. Failing probe\n",
152 (uintmax_t)port_start);
153 aha_free(aha);
154 bus_release_resource(dev, SYS_RES_IOPORT, port_rid,
155 port_res);
156 return (ENXIO);
157 }
158
159 bus_release_resource(dev, SYS_RES_IOPORT, port_rid, port_res);
160
161 switch (config_data.dma_chan) {
162 case DMA_CHAN_5:
163 drq = 5;
164 break;
165 case DMA_CHAN_6:
166 drq = 6;
167 break;
168 case DMA_CHAN_7:
169 drq = 7;
170 break;
171 default:
172 device_printf(dev, "Invalid DMA setting for adapter at %#jx.",
173 (uintmax_t)port_start);
174 return (ENXIO);
175 }
176 error = bus_set_resource(dev, SYS_RES_DRQ, 0, drq, 1);
177 if (error)
178 return error;
179
180 irq = ffs(config_data.irq) + 8;
181 error = bus_set_resource(dev, SYS_RES_IRQ, 0, irq, 1);
182 return (error);
183 }
184
185 /*
186 * Attach all the sub-devices we can find
187 */
188 static int
189 aha_isa_attach(device_t dev)
190 {
191 struct aha_softc *aha = device_get_softc(dev);
192 bus_dma_filter_t *filter;
193 void *filter_arg;
194 bus_addr_t lowaddr;
195 void *ih;
196 int error;
197
198 aha->dev = dev;
199 aha->portrid = 0;
200 aha->port = bus_alloc_resource(dev, SYS_RES_IOPORT, &aha->portrid,
201 0, ~0, AHA_NREGS, RF_ACTIVE);
202 if (!aha->port) {
203 device_printf(dev, "Unable to allocate I/O ports\n");
204 return ENOMEM;
205 }
206
207 aha->irqrid = 0;
208 aha->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &aha->irqrid,
209 RF_ACTIVE);
210 if (!aha->irq) {
211 device_printf(dev, "Unable to allocate excluse use of irq\n");
212 bus_release_resource(dev, SYS_RES_IOPORT, aha->portrid, aha->port);
213 return ENOMEM;
214 }
215
216 aha->drqrid = 0;
217 aha->drq = bus_alloc_resource_any(dev, SYS_RES_DRQ, &aha->drqrid,
218 RF_ACTIVE);
219 if (!aha->drq) {
220 device_printf(dev, "Unable to allocate drq\n");
221 bus_release_resource(dev, SYS_RES_IOPORT, aha->portrid, aha->port);
222 bus_release_resource(dev, SYS_RES_IRQ, aha->irqrid, aha->irq);
223 return ENOMEM;
224 }
225
226 #if 0 /* is the drq ever unset? */
227 if (dev->id_drq != -1)
228 isa_dmacascade(dev->id_drq);
229 #endif
230 isa_dmacascade(rman_get_start(aha->drq));
231
232 /* Allocate our parent dmatag */
233 filter = NULL;
234 filter_arg = NULL;
235 lowaddr = BUS_SPACE_MAXADDR_24BIT;
236
237 if (bus_dma_tag_create( /* parent */ NULL,
238 /* alignemnt */ 1,
239 /* boundary */ 0,
240 /* lowaddr */ lowaddr,
241 /* highaddr */ BUS_SPACE_MAXADDR,
242 /* filter */ filter,
243 /* filterarg */ filter_arg,
244 /* maxsize */ BUS_SPACE_MAXSIZE_24BIT,
245 /* nsegments */ ~0,
246 /* maxsegsz */ BUS_SPACE_MAXSIZE_24BIT,
247 /* flags */ 0,
248 /* lockfunc */ busdma_lock_mutex,
249 /* lockarg */ &Giant,
250 &aha->parent_dmat) != 0) {
251 aha_free(aha);
252 bus_release_resource(dev, SYS_RES_IOPORT, aha->portrid, aha->port);
253 bus_release_resource(dev, SYS_RES_IRQ, aha->irqrid, aha->irq);
254 bus_release_resource(dev, SYS_RES_DRQ, aha->drqrid, aha->drq);
255 return (ENOMEM);
256 }
257
258 if (aha_init(aha)) {
259 device_printf(dev, "init failed\n");
260 aha_free(aha);
261 bus_release_resource(dev, SYS_RES_IOPORT, aha->portrid, aha->port);
262 bus_release_resource(dev, SYS_RES_IRQ, aha->irqrid, aha->irq);
263 bus_release_resource(dev, SYS_RES_DRQ, aha->drqrid, aha->drq);
264 return (ENOMEM);
265 }
266
267 error = aha_attach(aha);
268 if (error) {
269 device_printf(dev, "attach failed\n");
270 aha_free(aha);
271 bus_release_resource(dev, SYS_RES_IOPORT, aha->portrid, aha->port);
272 bus_release_resource(dev, SYS_RES_IRQ, aha->irqrid, aha->irq);
273 bus_release_resource(dev, SYS_RES_DRQ, aha->drqrid, aha->drq);
274 return (error);
275 }
276
277 error = bus_setup_intr(dev, aha->irq, INTR_TYPE_CAM|INTR_ENTROPY, aha_intr, aha,
278 &ih);
279 if (error) {
280 device_printf(dev, "Unable to register interrupt handler\n");
281 aha_free(aha);
282 bus_release_resource(dev, SYS_RES_IOPORT, aha->portrid, aha->port);
283 bus_release_resource(dev, SYS_RES_IRQ, aha->irqrid, aha->irq);
284 bus_release_resource(dev, SYS_RES_DRQ, aha->drqrid, aha->drq);
285 return (error);
286 }
287
288 return (0);
289 }
290
291 static int
292 aha_isa_detach(device_t dev)
293 {
294 struct aha_softc *aha = (struct aha_softc *)device_get_softc(dev);
295 int error;
296
297 error = bus_teardown_intr(dev, aha->irq, aha->ih);
298 if (error) {
299 device_printf(dev, "failed to unregister interrupt handler\n");
300 }
301
302 bus_release_resource(dev, SYS_RES_IOPORT, aha->portrid, aha->port);
303 bus_release_resource(dev, SYS_RES_IRQ, aha->irqrid, aha->irq);
304 bus_release_resource(dev, SYS_RES_DRQ, aha->drqrid, aha->drq);
305
306 error = aha_detach(aha);
307 if (error) {
308 device_printf(dev, "detach failed\n");
309 return (error);
310 }
311 aha_free(aha);
312
313 return (0);
314 }
315
316 static void
317 aha_isa_identify(driver_t *driver, device_t parent)
318 {
319 int i;
320 bus_addr_t ioport;
321 struct aha_softc aha;
322 int rid;
323 struct resource *res;
324 device_t child;
325
326 /* Attempt to find an adapter */
327 for (i = 0; i < sizeof(aha_board_ports) / sizeof(aha_board_ports[0]);
328 i++) {
329 bzero(&aha, sizeof(aha));
330 ioport = aha_board_ports[i];
331 /*
332 * XXX Check to see if we have a hard-wired aha device at
333 * XXX this port, if so, skip. This should also cover the
334 * XXX case where we are run multiple times due to, eg,
335 * XXX kldload/kldunload.
336 */
337 rid = 0;
338 res = bus_alloc_resource(parent, SYS_RES_IOPORT, &rid,
339 ioport, ioport, AHA_NREGS, RF_ACTIVE);
340 if (res == NULL)
341 continue;
342 aha_alloc(&aha, -1, rman_get_bustag(res),
343 rman_get_bushandle(res));
344 /* See if there is really a card present */
345 if (aha_probe(&aha) || aha_fetch_adapter_info(&aha))
346 goto not_this_one;
347 child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "aha", -1);
348 bus_set_resource(child, SYS_RES_IOPORT, 0, ioport, AHA_NREGS);
349 /*
350 * Could query the board and set IRQ/DRQ, but probe does
351 * that.
352 */
353 not_this_one:;
354 bus_release_resource(parent, SYS_RES_IOPORT, rid, res);
355 aha_free(&aha);
356 }
357 }
358
359 static device_method_t aha_isa_methods[] = {
360 /* Device interface */
361 DEVMETHOD(device_probe, aha_isa_probe),
362 DEVMETHOD(device_attach, aha_isa_attach),
363 DEVMETHOD(device_detach, aha_isa_detach),
364 DEVMETHOD(device_identify, aha_isa_identify),
365
366 { 0, 0 }
367 };
368
369 static driver_t aha_isa_driver = {
370 "aha",
371 aha_isa_methods,
372 sizeof(struct aha_softc),
373 };
374
375 static devclass_t aha_devclass;
376
377 DRIVER_MODULE(aha, isa, aha_isa_driver, aha_devclass, 0, 0);
Cache object: ab2d61d3e65154238f2fbcfbb4a5fc8d
|