1 /*-
2 * Copyright (c) 2008 Deglitch Networks, Stanislav Sedov
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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.
24 *
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
28 * of it.
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
31 * config.
32 */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/time.h>
42 #include <sys/bus.h>
43 #include <sys/resource.h>
44 #include <sys/rman.h>
45 #include <sys/sema.h>
46 #include <sys/taskqueue.h>
47 #include <vm/uma.h>
48
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>
54
55 #include <sys/ata.h>
56 #include <dev/ata/ata-all.h>
57 #include <ata_if.h>
58
59 struct at91_cfata_softc {
60 device_t dev;
61 struct resource *mem_res;
62 struct resource irq;
63 void (*isr_cb)(void *);
64 void *isr_arg;
65 struct callout tick;
66 };
67
68 static int at91_cfata_detach(device_t dev);
69 static void at91_cfata_callout(void *arg);
70
71 static int
72 at91_cfata_probe(device_t dev)
73 {
74
75 device_set_desc_copy(dev, "AT91RM9200 CompactFlash controller");
76 return (0);
77 }
78
79 static int
80 at91_cfata_attach(device_t dev)
81 {
82 struct at91_cfata_softc *sc;
83 int rid, error;
84
85 sc = device_get_softc(dev);
86 sc->dev = dev;
87 rid = 0;
88 error = 0;
89 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
90 RF_ACTIVE);
91 if (sc->mem_res == NULL)
92 return (ENOMEM);
93
94 /* XXX: init CF controller? */
95
96 callout_init(&sc->tick, 1); /* Callout to poll the device. */
97 device_add_child(dev, "ata", -1);
98 bus_generic_attach(dev);
99 return (0);
100 }
101
102 static int
103 at91_cfata_detach(device_t dev)
104 {
105 struct at91_cfata_softc *sc;
106
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);
112 sc->mem_res = NULL;
113 }
114 return (0);
115 }
116
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)
120 {
121 struct at91_cfata_softc *sc = device_get_softc(dev);
122
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));
126 return (&sc->irq);
127 }
128
129 static int
130 ata_at91_release_resource(device_t dev, device_t child, int type, int rid,
131 struct resource *r)
132 {
133
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));
137 return (0);
138 }
139
140
141 static int
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)
145 {
146 struct at91_cfata_softc *sc = device_get_softc(dev);
147
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;
152 *cookiep = sc;
153 callout_reset(&sc->tick, 1, at91_cfata_callout, sc);
154 return (0);
155 }
156
157 static int
158 ata_at91_teardown_intr(device_t dev, device_t child, struct resource *irq,
159 void *cookie)
160 {
161 struct at91_cfata_softc *sc = device_get_softc(dev);
162
163 sc->isr_cb = NULL;
164 sc->isr_arg = NULL;
165 return (0);
166 }
167
168 static void
169 at91_cfata_callout(void *arg)
170 {
171 struct at91_cfata_softc *sc;
172
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);
177 }
178
179 static int
180 at91_channel_probe(device_t dev)
181 {
182 struct ata_channel *ch = device_get_softc(dev);
183
184 ch->unit = 0;
185 ch->flags |= ATA_USE_16BIT | ATA_NO_SLAVE;
186 device_set_desc_copy(dev, "ATA channel 0");
187
188 return (ata_probe(dev));
189 }
190
191 static int
192 at91_channel_attach(device_t dev)
193 {
194 struct at91_cfata_softc *sc = device_get_softc(device_get_parent(dev));
195 struct ata_channel *ch = device_get_softc(dev);
196 int i;
197
198 for (i = 0; i < ATA_MAX_RES; i++)
199 ch->r_io[i].res = sc->mem_res;
200
201 /*
202 * CF+ Specification.
203 * 6.1.3 Memory Mapped Addressing.
204 */
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;
218
219 /* Should point at the base of registers. */
220 ch->r_io[ATA_IDX_ADDR].offset = 0x0;
221
222 ata_generic_hw(dev);
223 return (ata_attach(dev));
224 }
225
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),
234
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),
243
244 { 0, 0 }
245 };
246
247 devclass_t at91_cfata_devclass;
248
249 static driver_t at91_cfata_driver = {
250 "at91_cfata",
251 at91_cfata_methods,
252 sizeof(struct at91_cfata_softc),
253 };
254
255 DRIVER_MODULE(at91_cfata, atmelarm, at91_cfata_driver, at91_cfata_devclass, 0,
256 0);
257 MODULE_VERSION(at91_cfata, 1);
258 MODULE_DEPEND(at91_cfata, ata, 1, 1, 1);
259
260 /*
261 * ATA channel driver.
262 */
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),
271
272 { 0, 0 }
273 };
274
275 driver_t at91_channel_driver = {
276 "ata",
277 at91_channel_methods,
278 sizeof(struct ata_channel),
279 };
280
281 DRIVER_MODULE(ata, at91_cfata, at91_channel_driver, ata_devclass, 0, 0);
Cache object: 600b5bad262bc96594d3454dd408c75b
|