2 * Copyright 2002 by Peter Grehan. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
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,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * PSIM local bus ATA controller
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
39 #include <sys/malloc.h>
41 #include <sys/taskqueue.h>
42 #include <machine/stdarg.h>
44 #include <machine/resource.h>
45 #include <machine/bus.h>
48 #include <dev/ata/ata-all.h>
51 #include <dev/ofw/openfirm.h>
52 #include <powerpc/psim/iobusvar.h>
55 * Define the iobus ata bus attachment. This creates a pseudo-bus that
56 * the ATA device can be attached to
58 static int ata_iobus_attach(device_t dev);
59 static int ata_iobus_probe(device_t dev);
60 static int ata_iobus_print_child(device_t dev, device_t child);
61 struct resource *ata_iobus_alloc_resource(device_t, device_t, int, int *,
62 u_long, u_long, u_long, u_int);
63 static int ata_iobus_release_resource(device_t, device_t, int, int,
66 static device_method_t ata_iobus_methods[] = {
67 /* Device interface */
68 DEVMETHOD(device_probe, ata_iobus_probe),
69 DEVMETHOD(device_attach, ata_iobus_attach),
70 DEVMETHOD(device_shutdown, bus_generic_shutdown),
71 DEVMETHOD(device_suspend, bus_generic_suspend),
72 DEVMETHOD(device_resume, bus_generic_resume),
75 DEVMETHOD(bus_print_child, ata_iobus_print_child),
76 DEVMETHOD(bus_alloc_resource, ata_iobus_alloc_resource),
77 DEVMETHOD(bus_release_resource, ata_iobus_release_resource),
78 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
79 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
80 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
81 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
86 static driver_t ata_iobus_driver = {
92 static devclass_t ata_iobus_devclass;
94 DRIVER_MODULE(ataiobus, iobus, ata_iobus_driver, ata_iobus_devclass, 0, 0);
95 MODULE_DEPEND(ata, ata, 1, 1, 1);
98 ata_iobus_probe(device_t dev)
100 char *type = iobus_get_name(dev);
102 if (strncmp(type, "ata", 3) != 0)
105 device_set_desc(dev, "PSIM ATA Controller");
111 ata_iobus_attach(device_t dev)
114 * Add a single child per controller. Should be able
117 device_add_child(dev, "ata", -1);
118 return (bus_generic_attach(dev));
123 ata_iobus_print_child(device_t dev, device_t child)
127 retval += bus_print_child_header(dev, child);
129 retval += bus_print_child_footer(dev, child);
136 ata_iobus_alloc_resource(device_t dev, device_t child, int type, int *rid,
137 u_long start, u_long end, u_long count, u_int flags)
139 struct resource *res = NULL;
143 ofw_regs = iobus_get_regs(dev);
146 * The reg array for the PSIM ata device has 6 start/size entries:
149 * 4/5/6 - primary command
150 * 7/8/9 - secondary command
151 * 10/11/12 - primary control
152 * 13/14/15 - secondary control
153 * 16/17/18 - primary/secondary dma registers, unimplemented
155 * The resource values are calculated from these registers
157 if (type == SYS_RES_IOPORT) {
162 end = start + ATA_IOSIZE - 1;
164 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
165 SYS_RES_MEMORY, &myrid,
166 start, end, count, flags);
169 case ATA_CTLADDR_RID:
171 start = ofw_regs[10];
172 end = start + ATA_CTLIOSIZE - 1;
173 count = ATA_CTLIOSIZE;
174 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
175 SYS_RES_MEMORY, &myrid,
176 start, end, count, flags);
180 /* DMA not properly supported by psim */
185 } else if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
187 * Pass this on to the parent
189 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
190 SYS_RES_IRQ, rid, 0, ~0, 1, flags));
199 ata_iobus_release_resource(device_t dev, device_t child, int type, int rid,
208 * Define the actual ATA device. This is a sub-bus to the ata-iobus layer
209 * to allow the higher layer bus to massage the resource allocation.
212 static int ata_iobus_sub_probe(device_t dev);
213 static int ata_iobus_sub_setmode(device_t dev, int target, int mode);
215 static device_method_t ata_iobus_sub_methods[] = {
216 /* Device interface */
217 DEVMETHOD(device_probe, ata_iobus_sub_probe),
218 DEVMETHOD(device_attach, ata_attach),
219 DEVMETHOD(device_detach, ata_detach),
220 DEVMETHOD(device_resume, ata_resume),
223 DEVMETHOD(ata_setmode, ata_iobus_sub_setmode),
227 static driver_t ata_iobus_sub_driver = {
229 ata_iobus_sub_methods,
230 sizeof(struct ata_channel),
233 DRIVER_MODULE(ata, ataiobus, ata_iobus_sub_driver, ata_devclass, 0, 0);
236 ata_iobus_sub_probe(device_t dev)
238 struct ata_channel *ch = device_get_softc(dev);
240 /* Only a single unit per controller thus far */
242 ch->flags = (ATA_USE_16BIT|ATA_NO_SLAVE);
245 return ata_probe(dev);
249 ata_iobus_sub_setmode(device_t parent, int target, int mode)
251 /* Only ever PIO mode here... */