2 * Copyright (c) 2012 Robert N. M. Watson
5 * This software was developed by SRI International and the University of
6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7 * ("CTSRD"), as part of the DARPA CRASH research programme.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include "opt_altera_sdcard.h"
36 #include <sys/param.h>
38 #include <sys/condvar.h>
41 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/mutex.h>
47 #include <sys/systm.h>
48 #include <sys/taskqueue.h>
50 #include <machine/bus.h>
51 #include <machine/resource.h>
53 #include <geom/geom_disk.h>
55 #include <dev/altera/sdcard/altera_sdcard.h>
58 * Device driver for the Altera University Program Secure Data Card IP Core,
59 * as described in the similarly named SOPC Builder IP Core specification.
60 * This soft core is not a full SD host controller interface (SDHCI) but
61 * instead provides a set of memory mapped registers and memory buffer that
62 * mildly abstract the SD Card protocol, but without providing DMA or
63 * interrupts. However, it does hide the details of voltage and
64 * communications negotiation. This driver implements disk(9), but due to the
65 * lack of interrupt support, must rely on timer-driven polling to determine
66 * when I/Os have completed.
70 * 1. Implement DISKFLAG_CANDELETE / SD Card sector erase support.
71 * 2. Implement d_ident from SD Card CID serial number field.
72 * 3. Handle read-only SD Cards.
73 * 4. Tune timeouts based on real-world SD Card speeds.
75 devclass_t altera_sdcard_devclass;
78 altera_sdcard_attach(struct altera_sdcard_softc *sc)
81 ALTERA_SDCARD_LOCK_INIT(sc);
82 ALTERA_SDCARD_CONDVAR_INIT(sc);
84 bioq_init(&sc->as_bioq);
85 sc->as_currentbio = NULL;
86 sc->as_state = ALTERA_SDCARD_STATE_NOCARD;
87 sc->as_taskqueue = taskqueue_create("altera_sdcardc taskq", M_WAITOK,
88 taskqueue_thread_enqueue, &sc->as_taskqueue);
89 taskqueue_start_threads(&sc->as_taskqueue, 1, PI_DISK,
90 "altera_sdcardc%d taskqueue", sc->as_unit);
91 TIMEOUT_TASK_INIT(sc->as_taskqueue, &sc->as_task, 0,
92 altera_sdcard_task, sc);
95 * Kick off timer-driven processing with a manual poll so that we
96 * synchronously detect an already-inserted SD Card during the boot or
97 * other driver attach point.
99 altera_sdcard_task(sc, 1);
103 altera_sdcard_detach(struct altera_sdcard_softc *sc)
106 KASSERT(sc->as_taskqueue != NULL, ("%s: taskqueue not present",
110 * Winding down the driver on detach is a bit complex. Update the
111 * flags to indicate that a detach has been requested, and then wait
112 * for in-progress I/O to wind down before continuing.
114 ALTERA_SDCARD_LOCK(sc);
115 sc->as_flags |= ALTERA_SDCARD_FLAG_DETACHREQ;
116 while (sc->as_state != ALTERA_SDCARD_STATE_DETACHED)
117 ALTERA_SDCARD_CONDVAR_WAIT(sc);
118 ALTERA_SDCARD_UNLOCK(sc);
121 * Now wait for the possibly still executing taskqueue to drain. In
122 * principle no more events will be scheduled as we've transitioned to
123 * a detached state, but there might still be a request in execution.
125 while (taskqueue_cancel_timeout(sc->as_taskqueue, &sc->as_task, NULL))
126 taskqueue_drain_timeout(sc->as_taskqueue, &sc->as_task);
129 * Simulate a disk removal if one is present to deal with any pending
132 if (sc->as_disk != NULL)
133 altera_sdcard_disk_remove(sc);
134 KASSERT(bioq_first(&sc->as_bioq) == NULL,
135 ("%s: non-empty bioq", __func__));
138 * Free any remaining allocated resources.
140 taskqueue_free(sc->as_taskqueue);
141 sc->as_taskqueue = NULL;
142 ALTERA_SDCARD_CONDVAR_DESTROY(sc);
143 ALTERA_SDCARD_LOCK_DESTROY(sc);
147 * Set up and start the next I/O. Transition to the I/O state, but allow the
148 * caller to schedule the next timeout, as this may be called either from an
149 * initial attach context, or from the task queue, which requires different
153 altera_sdcard_nextio(struct altera_sdcard_softc *sc)
157 ALTERA_SDCARD_LOCK_ASSERT(sc);
158 KASSERT(sc->as_currentbio == NULL,
159 ("%s: bio already active", __func__));
161 bp = bioq_takefirst(&sc->as_bioq);
163 panic("%s: bioq empty", __func__);
164 altera_sdcard_io_start(sc, bp);
165 sc->as_state = ALTERA_SDCARD_STATE_IO;
169 altera_sdcard_task_nocard(struct altera_sdcard_softc *sc)
172 ALTERA_SDCARD_LOCK_ASSERT(sc);
175 * Handle device driver detach.
177 if (sc->as_flags & ALTERA_SDCARD_FLAG_DETACHREQ) {
178 sc->as_state = ALTERA_SDCARD_STATE_DETACHED;
183 * If there is no card insertion, remain in NOCARD.
185 if (!(altera_sdcard_read_asr(sc) & ALTERA_SDCARD_ASR_CARDPRESENT))
189 * Read the CSD -- it may contain values that the driver can't handle,
190 * either because of an unsupported version/feature, or because the
191 * card is misbehaving. This triggers a transition to
192 * ALTERA_SDCARD_STATE_BADCARD. We rely on the CSD read to print a
193 * banner about how the card is problematic, since it has more
194 * information. The bad card state allows us to print that banner
195 * once rather than each time we notice the card is there, and still
198 if (altera_sdcard_read_csd(sc) != 0) {
199 sc->as_state = ALTERA_SDCARD_STATE_BADCARD;
204 * Process card insertion and upgrade to the IDLE state.
206 altera_sdcard_disk_insert(sc);
207 sc->as_state = ALTERA_SDCARD_STATE_IDLE;
211 altera_sdcard_task_badcard(struct altera_sdcard_softc *sc)
214 ALTERA_SDCARD_LOCK_ASSERT(sc);
217 * Handle device driver detach.
219 if (sc->as_flags & ALTERA_SDCARD_FLAG_DETACHREQ) {
220 sc->as_state = ALTERA_SDCARD_STATE_DETACHED;
225 * Handle safe card removal -- no teardown is required, just a state
228 if (!(altera_sdcard_read_asr(sc) & ALTERA_SDCARD_ASR_CARDPRESENT))
229 sc->as_state = ALTERA_SDCARD_STATE_NOCARD;
233 altera_sdcard_task_idle(struct altera_sdcard_softc *sc)
236 ALTERA_SDCARD_LOCK_ASSERT(sc);
239 * Handle device driver detach.
241 if (sc->as_flags & ALTERA_SDCARD_FLAG_DETACHREQ) {
242 sc->as_state = ALTERA_SDCARD_STATE_DETACHED;
247 * Handle safe card removal.
249 if (!(altera_sdcard_read_asr(sc) & ALTERA_SDCARD_ASR_CARDPRESENT)) {
250 altera_sdcard_disk_remove(sc);
251 sc->as_state = ALTERA_SDCARD_STATE_NOCARD;
256 altera_sdcard_task_io(struct altera_sdcard_softc *sc)
260 ALTERA_SDCARD_LOCK_ASSERT(sc);
261 KASSERT(sc->as_currentbio != NULL, ("%s: no current I/O", __func__));
263 #ifdef ALTERA_SDCARD_FAST_SIM
266 asr = altera_sdcard_read_asr(sc);
269 * Check for unexpected card removal during an I/O.
271 if (!(asr & ALTERA_SDCARD_ASR_CARDPRESENT)) {
272 altera_sdcard_disk_remove(sc);
273 if (sc->as_flags & ALTERA_SDCARD_FLAG_DETACHREQ)
274 sc->as_state = ALTERA_SDCARD_STATE_DETACHED;
276 sc->as_state = ALTERA_SDCARD_STATE_NOCARD;
281 * If the I/O isn't complete, remain in the IO state without further
282 * action, even if DETACHREQ is in flight.
284 if (asr & ALTERA_SDCARD_ASR_CMDINPROGRESS)
288 * Handle various forms of I/O completion, successful and otherwise.
289 * The I/O layer may restart the transaction if an error occurred, in
290 * which case remain in the IO state and reschedule.
292 if (!altera_sdcard_io_complete(sc, asr))
296 * Now that I/O is complete, process detach requests in preference to
299 if (sc->as_flags & ALTERA_SDCARD_FLAG_DETACHREQ) {
300 sc->as_state = ALTERA_SDCARD_STATE_DETACHED;
305 * Finally, either start the next I/O or transition to the IDLE state.
307 if (bioq_first(&sc->as_bioq) != NULL) {
308 altera_sdcard_nextio(sc);
309 #ifdef ALTERA_SDCARD_FAST_SIM
313 sc->as_state = ALTERA_SDCARD_STATE_IDLE;
317 altera_sdcard_task_rechedule(struct altera_sdcard_softc *sc)
322 * Reschedule based on new state. Or not, if detaching the device
323 * driver. Treat a bad card as though it were no card at all.
325 switch (sc->as_state) {
326 case ALTERA_SDCARD_STATE_NOCARD:
327 case ALTERA_SDCARD_STATE_BADCARD:
328 interval = ALTERA_SDCARD_TIMEOUT_NOCARD;
331 case ALTERA_SDCARD_STATE_IDLE:
332 interval = ALTERA_SDCARD_TIMEOUT_IDLE;
335 case ALTERA_SDCARD_STATE_IO:
336 if (sc->as_flags & ALTERA_SDCARD_FLAG_IOERROR)
337 interval = ALTERA_SDCARD_TIMEOUT_IOERROR;
339 interval = ALTERA_SDCARD_TIMEOUT_IO;
343 panic("%s: invalid exit state %d", __func__, sc->as_state);
345 taskqueue_enqueue_timeout(sc->as_taskqueue, &sc->as_task, interval);
349 * Because the Altera SD Card IP Core doesn't support interrupts, we do all
350 * asynchronous work from a timeout. Poll at two different rates -- an
351 * infrequent check for card insertion status changes, and a frequent one for
352 * I/O completion. The task should never start in DETACHED, as that would
353 * imply that a previous instance failed to cancel rather than reschedule.
356 altera_sdcard_task(void *arg, int pending)
358 struct altera_sdcard_softc *sc;
361 KASSERT(sc->as_state != ALTERA_SDCARD_STATE_DETACHED,
362 ("%s: already in detached", __func__));
364 ALTERA_SDCARD_LOCK(sc);
365 switch (sc->as_state) {
366 case ALTERA_SDCARD_STATE_NOCARD:
367 altera_sdcard_task_nocard(sc);
370 case ALTERA_SDCARD_STATE_BADCARD:
371 altera_sdcard_task_badcard(sc);
374 case ALTERA_SDCARD_STATE_IDLE:
375 altera_sdcard_task_idle(sc);
378 case ALTERA_SDCARD_STATE_IO:
379 altera_sdcard_task_io(sc);
383 panic("%s: invalid enter state %d", __func__, sc->as_state);
387 * If we have transitioned to DETACHED, signal the detach thread and
388 * cancel the timeout-driven task. Otherwise reschedule on an
389 * appropriate timeout.
391 if (sc->as_state == ALTERA_SDCARD_STATE_DETACHED)
392 ALTERA_SDCARD_CONDVAR_SIGNAL(sc);
394 altera_sdcard_task_rechedule(sc);
395 ALTERA_SDCARD_UNLOCK(sc);
399 altera_sdcard_start(struct altera_sdcard_softc *sc)
402 ALTERA_SDCARD_LOCK_ASSERT(sc);
404 KASSERT(sc->as_state == ALTERA_SDCARD_STATE_IDLE,
405 ("%s: starting when not IDLE", __func__));
407 taskqueue_cancel_timeout(sc->as_taskqueue, &sc->as_task, NULL);
408 altera_sdcard_nextio(sc);
409 #ifdef ALTERA_SDCARD_FAST_SIM
410 altera_sdcard_task_io(sc);
412 altera_sdcard_task_rechedule(sc);