2 * Copyright (c) 2002 - 2008 Søren Schmidt <sos@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
40 #include <sys/taskqueue.h>
42 #include <machine/resource.h>
43 #include <machine/bus.h>
45 #include <isa/isavar.h>
46 #include <dev/ata/ata-all.h>
50 struct ata_cbus_controller {
52 struct resource *ctlio;
53 struct resource *bankio;
64 void (*function)(void *);
69 /* local prototypes */
70 static void ata_cbus_intr(void *);
72 static int ata_cbuschannel_banking(device_t dev, int flags);
76 ata_cbus_probe(device_t dev)
82 /* dont probe PnP devices */
83 if (isa_get_vendorid(dev))
86 /* allocate the ioport range */
88 if (!(io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
89 ATA_PC98_IOSIZE, RF_ACTIVE)))
92 /* calculate & set the altport range */
93 rid = ATA_PC98_CTLADDR_RID;
94 if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &tmp, &tmp)) {
95 bus_set_resource(dev, SYS_RES_IOPORT, rid,
96 rman_get_start(io)+ATA_PC98_CTLOFFSET, ATA_CTLIOSIZE);
99 /* calculate & set the bank range */
100 rid = ATA_PC98_BANKADDR_RID;
101 if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &tmp, &tmp)) {
102 bus_set_resource(dev, SYS_RES_IOPORT, rid,
103 ATA_PC98_BANK, ATA_PC98_BANKIOSIZE);
106 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
111 ata_cbus_attach(device_t dev)
113 struct ata_cbus_controller *ctlr = device_get_softc(dev);
117 /* allocate resources */
118 rid = ATA_IOADDR_RID;
119 if (!(ctlr->io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
120 ATA_PC98_IOSIZE, RF_ACTIVE)))
123 rid = ATA_PC98_CTLADDR_RID;
125 bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
126 rman_get_start(ctlr->io) + ATA_PC98_CTLOFFSET, ~0,
127 ATA_CTLIOSIZE, RF_ACTIVE))) {
128 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
132 rid = ATA_PC98_BANKADDR_RID;
133 if (!(ctlr->bankio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
135 ATA_PC98_BANKIOSIZE, RF_ACTIVE))) {
136 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
137 bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, ctlr->ctlio);
142 if (!(ctlr->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
143 RF_ACTIVE | RF_SHAREABLE))) {
144 device_printf(dev, "unable to alloc interrupt\n");
145 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
146 bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, ctlr->ctlio);
147 bus_release_resource(dev, SYS_RES_IOPORT,
148 ATA_PC98_BANKADDR_RID, ctlr->bankio);
152 if ((bus_setup_intr(dev, ctlr->irq, ATA_INTR_FLAGS,
153 NULL, ata_cbus_intr, ctlr, &ctlr->ih))) {
154 device_printf(dev, "unable to setup interrupt\n");
155 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
156 bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, ctlr->ctlio);
157 bus_release_resource(dev, SYS_RES_IOPORT,
158 ATA_PC98_BANKADDR_RID, ctlr->bankio);
159 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IRQ_RID, ctlr->irq);
165 mtx_init(&ctlr->bank_mtx, "ATA cbus bank lock", NULL, MTX_DEF);
166 ctlr->hardware_bank = -1;
167 ctlr->locked_bank = -1;
168 ctlr->restart_bank = -1;
170 /* Work around the lack of channel serialization in ATA_CAM. */
172 device_printf(dev, "second channel ignored\n");
175 for (unit = 0; unit < ctlr->channels; unit++) {
176 child = device_add_child(dev, "ata", unit);
178 device_printf(dev, "failed to add ata child device\n");
180 device_set_ivars(child, (void *)(intptr_t)unit);
183 bus_generic_attach(dev);
187 static struct resource *
188 ata_cbus_alloc_resource(device_t dev, device_t child, int type, int *rid,
189 u_long start, u_long end, u_long count, u_int flags)
191 struct ata_cbus_controller *ctlr = device_get_softc(dev);
193 if (type == SYS_RES_IOPORT) {
197 case ATA_CTLADDR_RID:
201 if (type == SYS_RES_IRQ)
207 ata_cbus_setup_intr(device_t dev, device_t child, struct resource *irq,
208 int flags, driver_filter_t *filter, driver_intr_t *intr,
209 void *arg, void **cookiep)
211 struct ata_cbus_controller *controller = device_get_softc(dev);
212 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
214 if (filter != NULL) {
215 printf("ata-cbus.c: we cannot use a filter here\n");
218 controller->interrupt[unit].function = intr;
219 controller->interrupt[unit].argument = arg;
220 *cookiep = controller;
226 ata_cbus_print_child(device_t dev, device_t child)
228 struct ata_channel *ch = device_get_softc(child);
231 retval += bus_print_child_header(dev, child);
232 retval += printf(" at bank %d", ch->unit);
233 retval += bus_print_child_footer(dev, child);
238 ata_cbus_intr(void *data)
240 struct ata_cbus_controller *ctlr = data;
241 struct ata_channel *ch;
244 for (unit = 0; unit < ctlr->channels; unit++) {
245 if (!(ch = ctlr->interrupt[unit].argument))
248 if (ata_cbuschannel_banking(ch->dev, ATA_LF_WHICH) == unit)
250 ctlr->interrupt[unit].function(ch);
254 static device_method_t ata_cbus_methods[] = {
255 /* device interface */
256 DEVMETHOD(device_probe, ata_cbus_probe),
257 DEVMETHOD(device_attach, ata_cbus_attach),
258 // DEVMETHOD(device_detach, ata_cbus_detach),
261 DEVMETHOD(bus_alloc_resource, ata_cbus_alloc_resource),
262 DEVMETHOD(bus_setup_intr, ata_cbus_setup_intr),
263 DEVMETHOD(bus_print_child, ata_cbus_print_child),
268 static driver_t ata_cbus_driver = {
271 sizeof(struct ata_cbus_controller),
274 static devclass_t ata_cbus_devclass;
276 DRIVER_MODULE(atacbus, isa, ata_cbus_driver, ata_cbus_devclass, NULL, NULL);
279 ata_cbuschannel_probe(device_t dev)
283 sprintf(buffer, "ATA channel %d", (int)(intptr_t)device_get_ivars(dev));
284 device_set_desc_copy(dev, buffer);
286 return ata_probe(dev);
290 ata_cbuschannel_attach(device_t dev)
292 struct ata_cbus_controller *ctlr = device_get_softc(device_get_parent(dev));
293 struct ata_channel *ch = device_get_softc(dev);
300 ch->unit = (intptr_t)device_get_ivars(dev);
301 /* setup the resource vectors */
302 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
303 ch->r_io[i].res = ctlr->io;
304 ch->r_io[i].offset = i << 1;
306 ch->r_io[ATA_CONTROL].res = ctlr->ctlio;
307 ch->r_io[ATA_CONTROL].offset = 0;
308 ch->r_io[ATA_IDX_ADDR].res = ctlr->io;
309 ata_default_registers(dev);
311 /* initialize softc for this channel */
312 ch->flags |= ATA_USE_16BIT;
315 return ata_attach(dev);
319 ata_cbuschannel_detach(device_t dev)
321 struct ata_channel *ch = device_get_softc(dev);
327 return ata_detach(dev);
331 ata_cbuschannel_suspend(device_t dev)
333 struct ata_channel *ch = device_get_softc(dev);
338 return ata_suspend(dev);
342 ata_cbuschannel_resume(device_t dev)
344 struct ata_channel *ch = device_get_softc(dev);
349 return ata_resume(dev);
354 ata_cbuschannel_banking(device_t dev, int flags)
356 struct ata_cbus_controller *ctlr = device_get_softc(device_get_parent(dev));
357 struct ata_channel *ch = device_get_softc(dev);
360 mtx_lock(&ctlr->bank_mtx);
363 if (ctlr->locked_bank == -1)
364 ctlr->locked_bank = ch->unit;
365 if (ctlr->locked_bank == ch->unit) {
366 ctlr->hardware_bank = ch->unit;
367 ATA_OUTB(ctlr->bankio, 0, ch->unit);
370 ctlr->restart_bank = ch->unit;
374 if (ctlr->locked_bank == ch->unit) {
375 ctlr->locked_bank = -1;
376 if (ctlr->restart_bank != -1) {
377 if ((ch = ctlr->interrupt[ctlr->restart_bank].argument)) {
378 ctlr->restart_bank = -1;
379 mtx_unlock(&ctlr->bank_mtx);
390 res = ctlr->locked_bank;
391 mtx_unlock(&ctlr->bank_mtx);
396 static device_method_t ata_cbuschannel_methods[] = {
397 /* device interface */
398 DEVMETHOD(device_probe, ata_cbuschannel_probe),
399 DEVMETHOD(device_attach, ata_cbuschannel_attach),
400 DEVMETHOD(device_detach, ata_cbuschannel_detach),
401 DEVMETHOD(device_suspend, ata_cbuschannel_suspend),
402 DEVMETHOD(device_resume, ata_cbuschannel_resume),
406 DEVMETHOD(ata_locking, ata_cbuschannel_banking),
411 static driver_t ata_cbuschannel_driver = {
413 ata_cbuschannel_methods,
414 sizeof(struct ata_channel),
417 DRIVER_MODULE(ata, atacbus, ata_cbuschannel_driver, ata_devclass, NULL, NULL);
418 MODULE_DEPEND(ata, ata, 1, 1, 1);