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 achive 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/cpu.h>
51 #include <machine/cpufunc.h>
52 #include <machine/resource.h>
53 #include <machine/intr.h>
56 #include <dev/ata/ata-all.h>
59 struct at91_cfata_softc {
61 struct resource *mem_res;
63 void (*isr_cb)(void *);
68 static int at91_cfata_detach(device_t dev);
69 static void at91_cfata_callout(void *arg);
72 at91_cfata_probe(device_t dev)
75 device_set_desc_copy(dev, "AT91RM9200 CompactFlash controller");
80 at91_cfata_attach(device_t dev)
82 struct at91_cfata_softc *sc;
85 sc = device_get_softc(dev);
89 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
91 if (sc->mem_res == NULL)
94 /* XXX: init CF controller? */
96 callout_init(&sc->tick, 1); /* Callout to poll the device. */
97 device_add_child(dev, "ata", -1);
98 bus_generic_attach(dev);
103 at91_cfata_detach(device_t dev)
105 struct at91_cfata_softc *sc;
107 sc = device_get_softc(dev);
108 bus_generic_detach(sc->dev);
109 if (sc->mem_res != NULL) {
110 bus_release_resource(dev, SYS_RES_MEMORY,
111 rman_get_rid(sc->mem_res), sc->mem_res);
117 static struct resource *
118 ata_at91_alloc_resource(device_t dev, device_t child, int type, int *rid,
119 u_long start, u_long end, u_long count, u_int flags)
121 struct at91_cfata_softc *sc = device_get_softc(dev);
123 KASSERT(type == SYS_RES_IRQ && *rid == ATA_IRQ_RID,
124 ("[at91_cfata, %d]: illegal resource request (type %u rid %u)",
125 __LINE__, type, *rid));
130 ata_at91_release_resource(device_t dev, device_t child, int type, int rid,
134 KASSERT(type == SYS_RES_IRQ && rid == ATA_IRQ_RID,
135 ("[at91_cfata, %d]: illegal resource request (type %u rid %u)",
136 __LINE__, type, rid));
142 ata_at91_setup_intr(device_t dev, device_t child, struct resource *irq,
143 int flags, driver_filter_t *filt,
144 driver_intr_t *function, void *argument, void **cookiep)
146 struct at91_cfata_softc *sc = device_get_softc(dev);
148 KASSERT(sc->isr_cb == NULL,
149 ("[at91_cfata, %d]: overwriting the old handler", __LINE__));
150 sc->isr_cb = function;
151 sc->isr_arg = argument;
153 callout_reset(&sc->tick, 1, at91_cfata_callout, sc);
158 ata_at91_teardown_intr(device_t dev, device_t child, struct resource *irq,
161 struct at91_cfata_softc *sc = device_get_softc(dev);
169 at91_cfata_callout(void *arg)
171 struct at91_cfata_softc *sc;
173 sc = (struct at91_cfata_softc *)arg;
174 if (sc->isr_cb != NULL)
175 sc->isr_cb(sc->isr_arg);
176 callout_reset(&sc->tick, 1, at91_cfata_callout, sc);
180 at91_channel_probe(device_t dev)
182 struct ata_channel *ch = device_get_softc(dev);
185 ch->flags |= ATA_USE_16BIT | ATA_NO_SLAVE;
186 device_set_desc_copy(dev, "ATA channel 0");
188 return (ata_probe(dev));
192 at91_channel_attach(device_t dev)
194 struct at91_cfata_softc *sc = device_get_softc(device_get_parent(dev));
195 struct ata_channel *ch = device_get_softc(dev);
198 for (i = 0; i < ATA_MAX_RES; i++)
199 ch->r_io[i].res = sc->mem_res;
203 * 6.1.3 Memory Mapped Addressing.
205 ch->r_io[ATA_DATA].offset = 0x00;
206 ch->r_io[ATA_FEATURE].offset = 0x01;
207 ch->r_io[ATA_COUNT].offset = 0x02;
208 ch->r_io[ATA_SECTOR].offset = 0x03;
209 ch->r_io[ATA_CYL_LSB].offset = 0x04;
210 ch->r_io[ATA_CYL_MSB].offset = 0x05;
211 ch->r_io[ATA_DRIVE].offset = 0x06;
212 ch->r_io[ATA_COMMAND].offset = 0x07;
213 ch->r_io[ATA_ERROR].offset = 0x01;
214 ch->r_io[ATA_IREASON].offset = 0x02;
215 ch->r_io[ATA_STATUS].offset = 0x07;
216 ch->r_io[ATA_ALTSTAT].offset = 0x0e;
217 ch->r_io[ATA_CONTROL].offset = 0x0e;
219 /* Should point at the base of registers. */
220 ch->r_io[ATA_IDX_ADDR].offset = 0x0;
223 return (ata_attach(dev));
226 static device_method_t at91_cfata_methods[] = {
227 /* Device interface. */
228 DEVMETHOD(device_probe, at91_cfata_probe),
229 DEVMETHOD(device_attach, at91_cfata_attach),
230 DEVMETHOD(device_detach, at91_cfata_detach),
231 DEVMETHOD(device_shutdown, bus_generic_shutdown),
232 DEVMETHOD(device_suspend, bus_generic_suspend),
233 DEVMETHOD(device_resume, bus_generic_resume),
235 /* ATA bus methods. */
236 DEVMETHOD(bus_alloc_resource, ata_at91_alloc_resource),
237 DEVMETHOD(bus_release_resource, ata_at91_release_resource),
238 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
239 DEVMETHOD(bus_deactivate_resource,
240 bus_generic_deactivate_resource),
241 DEVMETHOD(bus_setup_intr, ata_at91_setup_intr),
242 DEVMETHOD(bus_teardown_intr, ata_at91_teardown_intr),
247 devclass_t at91_cfata_devclass;
249 static driver_t at91_cfata_driver = {
252 sizeof(struct at91_cfata_softc),
255 DRIVER_MODULE(at91_cfata, atmelarm, at91_cfata_driver, at91_cfata_devclass, 0,
257 MODULE_VERSION(at91_cfata, 1);
258 MODULE_DEPEND(at91_cfata, ata, 1, 1, 1);
261 * ATA channel driver.
263 static device_method_t at91_channel_methods[] = {
264 /* device interface */
265 DEVMETHOD(device_probe, at91_channel_probe),
266 DEVMETHOD(device_attach, at91_channel_attach),
267 DEVMETHOD(device_detach, ata_detach),
268 DEVMETHOD(device_shutdown, bus_generic_shutdown),
269 DEVMETHOD(device_suspend, ata_suspend),
270 DEVMETHOD(device_resume, ata_resume),
275 driver_t at91_channel_driver = {
277 at91_channel_methods,
278 sizeof(struct ata_channel),
281 DRIVER_MODULE(ata, at91_cfata, at91_channel_driver, ata_devclass, 0, 0);