2 * Copyright (c) 2008 Deglitch Networks, Stanislav Sedov
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 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 * Driver for the AT91RM9200 CompactFlash controller operating in a
26 * common memory mode. Interrupts are driven by polling. The driver
27 * implements an ATA bridge and attached ATA channel driver on top
29 * NOTE WELL: this driver uses polling mode. To achieve an acceptable
30 * operating speed you will probably want to use HZ=2000 in kernel
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
43 #include <sys/resource.h>
46 #include <sys/taskqueue.h>
49 #include <machine/bus.h>
50 #include <machine/resource.h>
51 #include <machine/intr.h>
54 #include <dev/ata/ata-all.h>
57 struct at91_cfata_softc {
59 struct resource *mem_res;
61 void (*isr_cb)(void *);
66 static int at91_cfata_detach(device_t dev);
67 static void at91_cfata_callout(void *arg);
70 at91_cfata_probe(device_t dev)
73 device_set_desc_copy(dev, "AT91RM9200 CompactFlash controller");
78 at91_cfata_attach(device_t dev)
80 struct at91_cfata_softc *sc;
83 sc = device_get_softc(dev);
87 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
89 if (sc->mem_res == NULL)
92 /* XXX: init CF controller? */
94 callout_init(&sc->tick, 1); /* Callout to poll the device. */
95 device_add_child(dev, "ata", -1);
96 bus_generic_attach(dev);
101 at91_cfata_detach(device_t dev)
103 struct at91_cfata_softc *sc;
105 sc = device_get_softc(dev);
106 bus_generic_detach(sc->dev);
107 if (sc->mem_res != NULL) {
108 bus_release_resource(dev, SYS_RES_MEMORY,
109 rman_get_rid(sc->mem_res), sc->mem_res);
115 static struct resource *
116 ata_at91_alloc_resource(device_t dev, device_t child, int type, int *rid,
117 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
119 struct at91_cfata_softc *sc = device_get_softc(dev);
121 KASSERT(type == SYS_RES_IRQ && *rid == ATA_IRQ_RID,
122 ("[at91_cfata, %d]: illegal resource request (type %u rid %u)",
123 __LINE__, type, *rid));
128 ata_at91_release_resource(device_t dev, device_t child, int type, int rid,
132 KASSERT(type == SYS_RES_IRQ && rid == ATA_IRQ_RID,
133 ("[at91_cfata, %d]: illegal resource request (type %u rid %u)",
134 __LINE__, type, rid));
140 ata_at91_setup_intr(device_t dev, device_t child, struct resource *irq,
141 int flags, driver_filter_t *filt,
142 driver_intr_t *function, void *argument, void **cookiep)
144 struct at91_cfata_softc *sc = device_get_softc(dev);
146 KASSERT(sc->isr_cb == NULL,
147 ("[at91_cfata, %d]: overwriting the old handler", __LINE__));
148 sc->isr_cb = function;
149 sc->isr_arg = argument;
151 callout_reset(&sc->tick, 1, at91_cfata_callout, sc);
156 ata_at91_teardown_intr(device_t dev, device_t child, struct resource *irq,
159 struct at91_cfata_softc *sc = device_get_softc(dev);
167 at91_cfata_callout(void *arg)
169 struct at91_cfata_softc *sc;
171 sc = (struct at91_cfata_softc *)arg;
172 if (sc->isr_cb != NULL)
173 sc->isr_cb(sc->isr_arg);
174 callout_reset(&sc->tick, 1, at91_cfata_callout, sc);
178 at91_channel_probe(device_t dev)
180 struct ata_channel *ch = device_get_softc(dev);
183 ch->flags |= ATA_USE_16BIT | ATA_NO_SLAVE;
184 device_set_desc_copy(dev, "ATA channel 0");
186 return (ata_probe(dev));
190 at91_channel_attach(device_t dev)
192 struct at91_cfata_softc *sc = device_get_softc(device_get_parent(dev));
193 struct ata_channel *ch = device_get_softc(dev);
196 for (i = 0; i < ATA_MAX_RES; i++)
197 ch->r_io[i].res = sc->mem_res;
201 * 6.1.3 Memory Mapped Addressing.
203 ch->r_io[ATA_DATA].offset = 0x00;
204 ch->r_io[ATA_FEATURE].offset = 0x01;
205 ch->r_io[ATA_COUNT].offset = 0x02;
206 ch->r_io[ATA_SECTOR].offset = 0x03;
207 ch->r_io[ATA_CYL_LSB].offset = 0x04;
208 ch->r_io[ATA_CYL_MSB].offset = 0x05;
209 ch->r_io[ATA_DRIVE].offset = 0x06;
210 ch->r_io[ATA_COMMAND].offset = 0x07;
211 ch->r_io[ATA_ERROR].offset = 0x01;
212 ch->r_io[ATA_IREASON].offset = 0x02;
213 ch->r_io[ATA_STATUS].offset = 0x07;
214 ch->r_io[ATA_ALTSTAT].offset = 0x0e;
215 ch->r_io[ATA_CONTROL].offset = 0x0e;
217 /* Should point at the base of registers. */
218 ch->r_io[ATA_IDX_ADDR].offset = 0x0;
221 return (ata_attach(dev));
224 static device_method_t at91_cfata_methods[] = {
225 /* Device interface. */
226 DEVMETHOD(device_probe, at91_cfata_probe),
227 DEVMETHOD(device_attach, at91_cfata_attach),
228 DEVMETHOD(device_detach, at91_cfata_detach),
229 DEVMETHOD(device_shutdown, bus_generic_shutdown),
230 DEVMETHOD(device_suspend, bus_generic_suspend),
231 DEVMETHOD(device_resume, bus_generic_resume),
233 /* ATA bus methods. */
234 DEVMETHOD(bus_alloc_resource, ata_at91_alloc_resource),
235 DEVMETHOD(bus_release_resource, ata_at91_release_resource),
236 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
237 DEVMETHOD(bus_deactivate_resource,
238 bus_generic_deactivate_resource),
239 DEVMETHOD(bus_setup_intr, ata_at91_setup_intr),
240 DEVMETHOD(bus_teardown_intr, ata_at91_teardown_intr),
245 devclass_t at91_cfata_devclass;
247 static driver_t at91_cfata_driver = {
250 sizeof(struct at91_cfata_softc),
253 DRIVER_MODULE(at91_cfata, atmelarm, at91_cfata_driver, at91_cfata_devclass, 0,
255 MODULE_VERSION(at91_cfata, 1);
256 MODULE_DEPEND(at91_cfata, ata, 1, 1, 1);
259 * ATA channel driver.
261 static device_method_t at91_channel_methods[] = {
262 /* device interface */
263 DEVMETHOD(device_probe, at91_channel_probe),
264 DEVMETHOD(device_attach, at91_channel_attach),
265 DEVMETHOD(device_detach, ata_detach),
266 DEVMETHOD(device_shutdown, bus_generic_shutdown),
267 DEVMETHOD(device_suspend, ata_suspend),
268 DEVMETHOD(device_resume, ata_resume),
273 driver_t at91_channel_driver = {
275 at91_channel_methods,
276 sizeof(struct ata_channel),
279 DRIVER_MODULE(ata, at91_cfata, at91_channel_driver, ata_devclass, 0, 0);