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;
61 void (*function)(void *);
66 /* local prototypes */
67 static void ata_cbus_intr(void *);
68 static int ata_cbuschannel_banking(device_t dev, int flags);
71 ata_cbus_probe(device_t dev)
77 /* dont probe PnP devices */
78 if (isa_get_vendorid(dev))
81 /* allocate the ioport range */
83 if (!(io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
84 ATA_PC98_IOSIZE, RF_ACTIVE)))
87 /* calculate & set the altport range */
88 rid = ATA_PC98_CTLADDR_RID;
89 if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &tmp, &tmp)) {
90 bus_set_resource(dev, SYS_RES_IOPORT, rid,
91 rman_get_start(io)+ATA_PC98_CTLOFFSET, ATA_CTLIOSIZE);
94 /* calculate & set the bank range */
95 rid = ATA_PC98_BANKADDR_RID;
96 if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &tmp, &tmp)) {
97 bus_set_resource(dev, SYS_RES_IOPORT, rid,
98 ATA_PC98_BANK, ATA_PC98_BANKIOSIZE);
101 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
106 ata_cbus_attach(device_t dev)
108 struct ata_cbus_controller *ctlr = device_get_softc(dev);
112 /* allocate resources */
113 rid = ATA_IOADDR_RID;
114 if (!(ctlr->io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
115 ATA_PC98_IOSIZE, RF_ACTIVE)))
118 rid = ATA_PC98_CTLADDR_RID;
120 bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
121 rman_get_start(ctlr->io) + ATA_PC98_CTLOFFSET, ~0,
122 ATA_CTLIOSIZE, RF_ACTIVE))) {
123 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
127 rid = ATA_PC98_BANKADDR_RID;
128 if (!(ctlr->bankio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
130 ATA_PC98_BANKIOSIZE, RF_ACTIVE))) {
131 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
132 bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, ctlr->ctlio);
137 if (!(ctlr->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
138 RF_ACTIVE | RF_SHAREABLE))) {
139 device_printf(dev, "unable to alloc interrupt\n");
140 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
141 bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, ctlr->ctlio);
142 bus_release_resource(dev, SYS_RES_IOPORT,
143 ATA_PC98_BANKADDR_RID, ctlr->bankio);
147 if ((bus_setup_intr(dev, ctlr->irq, ATA_INTR_FLAGS,
148 NULL, ata_cbus_intr, ctlr, &ctlr->ih))) {
149 device_printf(dev, "unable to setup interrupt\n");
150 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
151 bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, ctlr->ctlio);
152 bus_release_resource(dev, SYS_RES_IOPORT,
153 ATA_PC98_BANKADDR_RID, ctlr->bankio);
154 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IRQ_RID, ctlr->irq);
158 mtx_init(&ctlr->bank_mtx, "ATA cbus bank lock", NULL, MTX_DEF);
159 ctlr->hardware_bank = -1;
160 ctlr->locked_bank = -1;
161 ctlr->restart_bank = -1;
163 for (unit = 0; unit < 2; unit++) {
164 child = device_add_child(dev, "ata", unit);
166 device_printf(dev, "failed to add ata child device\n");
168 device_set_ivars(child, (void *)(intptr_t)unit);
171 bus_generic_attach(dev);
175 static struct resource *
176 ata_cbus_alloc_resource(device_t dev, device_t child, int type, int *rid,
177 u_long start, u_long end, u_long count, u_int flags)
179 struct ata_cbus_controller *ctlr = device_get_softc(dev);
181 if (type == SYS_RES_IOPORT) {
185 case ATA_CTLADDR_RID:
189 if (type == SYS_RES_IRQ)
195 ata_cbus_setup_intr(device_t dev, device_t child, struct resource *irq,
196 int flags, driver_filter_t *filter, driver_intr_t *intr,
197 void *arg, void **cookiep)
199 struct ata_cbus_controller *controller = device_get_softc(dev);
200 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
202 if (filter != NULL) {
203 printf("ata-cbus.c: we cannot use a filter here\n");
206 controller->interrupt[unit].function = intr;
207 controller->interrupt[unit].argument = arg;
208 *cookiep = controller;
214 ata_cbus_print_child(device_t dev, device_t child)
216 struct ata_channel *ch = device_get_softc(child);
219 retval += bus_print_child_header(dev, child);
220 retval += printf(" at bank %d", ch->unit);
221 retval += bus_print_child_footer(dev, child);
226 ata_cbus_intr(void *data)
228 struct ata_cbus_controller *ctlr = data;
229 struct ata_channel *ch;
232 for (unit = 0; unit < 2; unit++) {
233 if (!(ch = ctlr->interrupt[unit].argument))
235 if (ata_cbuschannel_banking(ch->dev, ATA_LF_WHICH) == unit)
236 ctlr->interrupt[unit].function(ch);
240 static device_method_t ata_cbus_methods[] = {
241 /* device interface */
242 DEVMETHOD(device_probe, ata_cbus_probe),
243 DEVMETHOD(device_attach, ata_cbus_attach),
244 // DEVMETHOD(device_detach, ata_cbus_detach),
247 DEVMETHOD(bus_alloc_resource, ata_cbus_alloc_resource),
248 DEVMETHOD(bus_setup_intr, ata_cbus_setup_intr),
249 DEVMETHOD(bus_print_child, ata_cbus_print_child),
254 static driver_t ata_cbus_driver = {
257 sizeof(struct ata_cbus_controller),
260 static devclass_t ata_cbus_devclass;
262 DRIVER_MODULE(atacbus, isa, ata_cbus_driver, ata_cbus_devclass, 0, 0);
265 ata_cbuschannel_probe(device_t dev)
269 sprintf(buffer, "ATA channel %d", (int)(intptr_t)device_get_ivars(dev));
270 device_set_desc_copy(dev, buffer);
272 return ata_probe(dev);
276 ata_cbuschannel_attach(device_t dev)
278 struct ata_cbus_controller *ctlr = device_get_softc(device_get_parent(dev));
279 struct ata_channel *ch = device_get_softc(dev);
286 ch->unit = (intptr_t)device_get_ivars(dev);
287 /* setup the resource vectors */
288 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
289 ch->r_io[i].res = ctlr->io;
290 ch->r_io[i].offset = i << 1;
292 ch->r_io[ATA_CONTROL].res = ctlr->ctlio;
293 ch->r_io[ATA_CONTROL].offset = 0;
294 ch->r_io[ATA_IDX_ADDR].res = ctlr->io;
295 ata_default_registers(dev);
297 /* initialize softc for this channel */
298 ch->flags |= ATA_USE_16BIT;
301 return ata_attach(dev);
305 ata_cbuschannel_detach(device_t dev)
307 struct ata_channel *ch = device_get_softc(dev);
313 return ata_detach(dev);
317 ata_cbuschannel_suspend(device_t dev)
319 struct ata_channel *ch = device_get_softc(dev);
324 return ata_suspend(dev);
328 ata_cbuschannel_resume(device_t dev)
330 struct ata_channel *ch = device_get_softc(dev);
335 return ata_resume(dev);
339 ata_cbuschannel_banking(device_t dev, int flags)
341 struct ata_cbus_controller *ctlr = device_get_softc(device_get_parent(dev));
342 struct ata_channel *ch = device_get_softc(dev);
345 mtx_lock(&ctlr->bank_mtx);
348 if (ctlr->locked_bank == -1)
349 ctlr->locked_bank = ch->unit;
350 if (ctlr->locked_bank == ch->unit) {
351 ctlr->hardware_bank = ch->unit;
352 ATA_OUTB(ctlr->bankio, 0, ch->unit);
355 ctlr->restart_bank = ch->unit;
359 if (ctlr->locked_bank == ch->unit) {
360 ctlr->locked_bank = -1;
361 if (ctlr->restart_bank != -1) {
362 if ((ch = ctlr->interrupt[ctlr->restart_bank].argument)) {
363 ctlr->restart_bank = -1;
364 mtx_unlock(&ctlr->bank_mtx);
375 res = ctlr->locked_bank;
376 mtx_unlock(&ctlr->bank_mtx);
380 static device_method_t ata_cbuschannel_methods[] = {
381 /* device interface */
382 DEVMETHOD(device_probe, ata_cbuschannel_probe),
383 DEVMETHOD(device_attach, ata_cbuschannel_attach),
384 DEVMETHOD(device_detach, ata_cbuschannel_detach),
385 DEVMETHOD(device_suspend, ata_cbuschannel_suspend),
386 DEVMETHOD(device_resume, ata_cbuschannel_resume),
389 DEVMETHOD(ata_locking, ata_cbuschannel_banking),
393 static driver_t ata_cbuschannel_driver = {
395 ata_cbuschannel_methods,
396 sizeof(struct ata_channel),
399 DRIVER_MODULE(ata, atacbus, ata_cbuschannel_driver, ata_devclass, 0, 0);
400 MODULE_DEPEND(ata, ata, 1, 1, 1);