2 * Copyright (c) 2002 - 2005 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.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
42 #include <sys/taskqueue.h>
44 #include <machine/resource.h>
45 #include <machine/bus.h>
47 #include <isa/isavar.h>
48 #include <dev/ata/ata-all.h>
52 struct ata_cbus_controller {
54 struct resource *ctlio;
55 struct resource *bankio;
63 void (*function)(void *);
68 /* local prototypes */
69 static void ata_cbus_intr(void *);
70 static int ata_cbuschannel_banking(device_t dev, int flags);
73 ata_cbus_probe(device_t dev)
79 /* dont probe PnP devices */
80 if (isa_get_vendorid(dev))
83 /* allocate the ioport range */
85 if (!(io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
86 ATA_PC98_IOSIZE, RF_ACTIVE)))
89 /* calculate & set the altport range */
90 rid = ATA_PC98_CTLADDR_RID;
91 if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &tmp, &tmp)) {
92 bus_set_resource(dev, SYS_RES_IOPORT, rid,
93 rman_get_start(io)+ATA_PC98_CTLOFFSET, ATA_CTLIOSIZE);
96 /* calculate & set the bank range */
97 rid = ATA_PC98_BANKADDR_RID;
98 if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &tmp, &tmp)) {
99 bus_set_resource(dev, SYS_RES_IOPORT, rid,
100 ATA_PC98_BANK, ATA_PC98_BANKIOSIZE);
103 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
108 ata_cbus_attach(device_t dev)
110 struct ata_cbus_controller *ctlr = device_get_softc(dev);
113 /* allocate resources */
114 rid = ATA_IOADDR_RID;
115 if (!(ctlr->io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
116 ATA_PC98_IOSIZE, RF_ACTIVE)))
119 rid = ATA_PC98_CTLADDR_RID;
121 bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
122 rman_get_start(ctlr->io) + ATA_PC98_CTLOFFSET, ~0,
123 ATA_CTLIOSIZE, RF_ACTIVE))) {
124 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
128 rid = ATA_PC98_BANKADDR_RID;
129 if (!(ctlr->bankio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
131 ATA_PC98_BANKIOSIZE, RF_ACTIVE))) {
132 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
133 bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, ctlr->ctlio);
138 if (!(ctlr->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
139 RF_ACTIVE | RF_SHAREABLE))) {
140 device_printf(dev, "unable to alloc interrupt\n");
141 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
142 bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, ctlr->ctlio);
143 bus_release_resource(dev, SYS_RES_IOPORT,
144 ATA_PC98_BANKADDR_RID, ctlr->bankio);
148 if ((bus_setup_intr(dev, ctlr->irq, ATA_INTR_FLAGS,
149 ata_cbus_intr, ctlr, &ctlr->ih))) {
150 device_printf(dev, "unable to setup interrupt\n");
151 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
152 bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, ctlr->ctlio);
153 bus_release_resource(dev, SYS_RES_IOPORT,
154 ATA_PC98_BANKADDR_RID, ctlr->bankio);
155 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IRQ_RID, ctlr->irq);
159 mtx_init(&ctlr->bank_mtx, "ATA cbus bank lock", NULL, MTX_DEF);
160 ctlr->hardware_bank = -1;
161 ctlr->locked_bank = -1;
162 ctlr->restart_bank = -1;
164 if (!device_add_child(dev, "ata", 0))
166 if (!device_add_child(dev, "ata", 1))
169 return bus_generic_attach(dev);
172 static struct resource *
173 ata_cbus_alloc_resource(device_t dev, device_t child, int type, int *rid,
174 u_long start, u_long end, u_long count, u_int flags)
176 struct ata_cbus_controller *ctlr = device_get_softc(dev);
178 if (type == SYS_RES_IOPORT) {
182 case ATA_CTLADDR_RID:
186 if (type == SYS_RES_IRQ)
192 ata_cbus_setup_intr(device_t dev, device_t child, struct resource *irq,
193 int flags, driver_intr_t *intr, void *arg,
196 struct ata_cbus_controller *controller = device_get_softc(dev);
197 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
199 controller->interrupt[unit].function = intr;
200 controller->interrupt[unit].argument = arg;
201 *cookiep = controller;
207 ata_cbus_print_child(device_t dev, device_t child)
209 struct ata_channel *ch = device_get_softc(child);
212 retval += bus_print_child_header(dev, child);
213 retval += printf(" at bank %d", ch->unit);
214 retval += bus_print_child_footer(dev, child);
219 ata_cbus_intr(void *data)
221 struct ata_cbus_controller *ctlr = data;
222 struct ata_channel *ch;
225 for (unit = 0; unit < 2; unit++) {
226 if (!(ch = ctlr->interrupt[unit].argument))
228 if (ata_cbuschannel_banking(ch->dev, ATA_LF_WHICH) == unit)
229 ctlr->interrupt[unit].function(ch);
233 static device_method_t ata_cbus_methods[] = {
234 /* device interface */
235 DEVMETHOD(device_probe, ata_cbus_probe),
236 DEVMETHOD(device_attach, ata_cbus_attach),
237 // DEVMETHOD(device_detach, ata_cbus_detach),
240 DEVMETHOD(bus_alloc_resource, ata_cbus_alloc_resource),
241 DEVMETHOD(bus_setup_intr, ata_cbus_setup_intr),
242 DEVMETHOD(bus_print_child, ata_cbus_print_child),
247 static driver_t ata_cbus_driver = {
250 sizeof(struct ata_cbus_controller),
253 static devclass_t ata_cbus_devclass;
255 DRIVER_MODULE(atacbus, isa, ata_cbus_driver, ata_cbus_devclass, 0, 0);
258 ata_cbuschannel_probe(device_t dev)
260 struct ata_cbus_controller *ctlr = device_get_softc(device_get_parent(dev));
261 struct ata_channel *ch = device_get_softc(dev);
265 /* find channel number on this controller */
266 device_get_children(device_get_parent(dev), &children, &count);
267 for (i = 0; i < count; i++) {
268 if (children[i] == dev)
271 free(children, M_TEMP);
273 /* setup the resource vectors */
274 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
275 ch->r_io[i].res = ctlr->io;
276 ch->r_io[i].offset = i << 1;
278 ch->r_io[ATA_CONTROL].res = ctlr->ctlio;
279 ch->r_io[ATA_CONTROL].offset = 0;
280 ch->r_io[ATA_IDX_ADDR].res = ctlr->io;
281 ata_default_registers(dev);
283 /* initialize softc for this channel */
284 ch->flags |= ATA_USE_16BIT;
286 return ata_probe(dev);
290 ata_cbuschannel_banking(device_t dev, int flags)
292 struct ata_cbus_controller *ctlr = device_get_softc(device_get_parent(dev));
293 struct ata_channel *ch = device_get_softc(dev);
296 mtx_lock(&ctlr->bank_mtx);
299 if (ctlr->locked_bank == -1)
300 ctlr->locked_bank = ch->unit;
301 if (ctlr->locked_bank == ch->unit) {
302 ctlr->hardware_bank = ch->unit;
303 ATA_OUTB(ctlr->bankio, 0, ch->unit);
306 ctlr->restart_bank = ch->unit;
310 if (ctlr->locked_bank == ch->unit) {
311 ctlr->locked_bank = -1;
312 if (ctlr->restart_bank != -1) {
313 if ((ch = ctlr->interrupt[ctlr->restart_bank].argument)) {
314 ctlr->restart_bank = -1;
315 mtx_unlock(&ctlr->bank_mtx);
326 res = ctlr->locked_bank;
327 mtx_unlock(&ctlr->bank_mtx);
331 static device_method_t ata_cbuschannel_methods[] = {
332 /* device interface */
333 DEVMETHOD(device_probe, ata_cbuschannel_probe),
334 DEVMETHOD(device_attach, ata_attach),
335 DEVMETHOD(device_detach, ata_detach),
336 DEVMETHOD(device_suspend, ata_suspend),
337 DEVMETHOD(device_resume, ata_resume),
340 DEVMETHOD(ata_locking, ata_cbuschannel_banking),
344 static driver_t ata_cbuschannel_driver = {
346 ata_cbuschannel_methods,
347 sizeof(struct ata_channel),
350 DRIVER_MODULE(ata, atacbus, ata_cbuschannel_driver, ata_devclass, 0, 0);
351 MODULE_DEPEND(ata, ata, 1, 1, 1);