No empty .Rs/.Re
[netbsd-mini2440.git] / sys / dev / sdmmc / sdmmc_io.c
blob20e71427f39f54a5be2522d5440ab4a9a4d3bd99
1 /* $NetBSD: sdmmc_io.c,v 1.1 2009/04/21 03:00:30 nonaka Exp $ */
2 /* $OpenBSD: sdmmc_io.c,v 1.10 2007/09/17 01:33:33 krw Exp $ */
4 /*
5 * Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 /* Routines for SD I/O cards. */
22 #include <sys/cdefs.h>
23 __KERNEL_RCSID(0, "$NetBSD: sdmmc_io.c,v 1.1 2009/04/21 03:00:30 nonaka Exp $");
25 #include <sys/param.h>
26 #include <sys/kernel.h>
27 #include <sys/malloc.h>
28 #include <sys/proc.h>
29 #include <sys/systm.h>
31 #include <dev/sdmmc/sdmmc_ioreg.h>
32 #include <dev/sdmmc/sdmmcchip.h>
33 #include <dev/sdmmc/sdmmcreg.h>
34 #include <dev/sdmmc/sdmmcvar.h>
36 #ifdef SDMMC_DEBUG
37 #define DPRINTF(s) do { printf s; } while (0)
38 #else
39 #define DPRINTF(s) do {} while (0)
40 #endif
42 struct sdmmc_intr_handler {
43 struct sdmmc_softc *ih_softc;
44 char *ih_name;
45 int (*ih_fun)(void *);
46 void *ih_arg;
47 TAILQ_ENTRY(sdmmc_intr_handler) entry;
50 static int sdmmc_io_rw_direct(struct sdmmc_softc *,
51 struct sdmmc_function *, int, u_char *, int);
52 static int sdmmc_io_rw_extended(struct sdmmc_softc *,
53 struct sdmmc_function *, int, u_char *, int, int);
54 #if 0
55 static int sdmmc_io_xchg(struct sdmmc_softc *, struct sdmmc_function *,
56 int, u_char *);
57 #endif
58 static void sdmmc_io_reset(struct sdmmc_softc *);
59 static int sdmmc_io_send_op_cond(struct sdmmc_softc *, uint32_t,
60 uint32_t *);
63 * Initialize SD I/O card functions (before memory cards). The host
64 * system and controller must support card interrupts in order to use
65 * I/O functions.
67 int
68 sdmmc_io_enable(struct sdmmc_softc *sc)
70 uint32_t host_ocr;
71 uint32_t card_ocr;
72 int error;
74 SDMMC_LOCK(sc);
76 /* Set host mode to SD "combo" card. */
77 SET(sc->sc_flags, SMF_SD_MODE|SMF_IO_MODE|SMF_MEM_MODE);
79 /* Reset I/O functions. */
80 sdmmc_io_reset(sc);
83 * Read the I/O OCR value, determine the number of I/O
84 * functions and whether memory is also present (a "combo
85 * card") by issuing CMD5. SD memory-only and MMC cards
86 * do not respond to CMD5.
88 error = sdmmc_io_send_op_cond(sc, 0, &card_ocr);
89 if (error) {
90 /* No SDIO card; switch to SD memory-only mode. */
91 CLR(sc->sc_flags, SMF_IO_MODE);
92 error = 0;
93 goto out;
96 /* Parse the additional bits in the I/O OCR value. */
97 if (!ISSET(card_ocr, SD_IO_OCR_MEM_PRESENT)) {
98 /* SDIO card without memory (not a "combo card"). */
99 DPRINTF(("%s: no memory present\n", SDMMCDEVNAME(sc)));
100 CLR(sc->sc_flags, SMF_MEM_MODE);
102 sc->sc_function_count = SD_IO_OCR_NUM_FUNCTIONS(card_ocr);
103 if (sc->sc_function_count == 0) {
104 /* Useless SDIO card without any I/O functions. */
105 DPRINTF(("%s: no I/O functions\n", SDMMCDEVNAME(sc)));
106 CLR(sc->sc_flags, SMF_IO_MODE);
107 error = 0;
108 goto out;
110 card_ocr &= SD_IO_OCR_MASK;
112 /* Set the lowest voltage supported by the card and host. */
113 host_ocr = sdmmc_chip_host_ocr(sc->sc_sct, sc->sc_sch);
114 error = sdmmc_set_bus_power(sc, host_ocr, card_ocr);
115 if (error) {
116 aprint_error_dev(sc->sc_dev,
117 "couldn't supply voltage requested by card\n");
118 goto out;
121 /* Reset I/O functions (again). */
122 sdmmc_io_reset(sc);
124 /* Send the new OCR value until all cards are ready. */
125 error = sdmmc_io_send_op_cond(sc, host_ocr, NULL);
126 if (error) {
127 aprint_error_dev(sc->sc_dev, "couldn't send I/O OCR\n");
128 goto out;
131 out:
132 SDMMC_UNLOCK(sc);
134 return error;
138 * Allocate sdmmc_function structures for SD card I/O function
139 * (including function 0).
141 void
142 sdmmc_io_scan(struct sdmmc_softc *sc)
144 struct sdmmc_function *sf0, *sf;
145 int error;
146 int i;
148 SDMMC_LOCK(sc);
150 sf0 = sdmmc_function_alloc(sc);
151 sf0->number = 0;
152 error = sdmmc_set_relative_addr(sc, sf0);
153 if (error) {
154 aprint_error_dev(sc->sc_dev, "couldn't set I/O RCA\n");
155 SET(sf0->flags, SFF_ERROR);
156 goto out;
158 sc->sc_fn0 = sf0;
159 SIMPLEQ_INSERT_TAIL(&sc->sf_head, sf0, sf_list);
161 /* Verify that the RCA has been set by selecting the card. */
162 error = sdmmc_select_card(sc, sf0);
163 if (error) {
164 aprint_error_dev(sc->sc_dev, "couldn't select I/O RCA %d\n",
165 sf0->rca);
166 SET(sf0->flags, SFF_ERROR);
167 goto out;
170 for (i = 1; i <= sc->sc_function_count; i++) {
171 sf = sdmmc_function_alloc(sc);
172 sf->number = i;
173 sf->rca = sf0->rca;
174 SIMPLEQ_INSERT_TAIL(&sc->sf_head, sf, sf_list);
177 out:
178 SDMMC_UNLOCK(sc);
182 * Initialize SDIO card functions.
185 sdmmc_io_init(struct sdmmc_softc *sc, struct sdmmc_function *sf)
187 int error = 0;
189 SDMMC_LOCK(sc);
191 if (sf->number == 0) {
192 sdmmc_io_write_1(sf, SD_IO_CCCR_BUS_WIDTH, CCCR_BUS_WIDTH_1);
194 error = sdmmc_read_cis(sf, &sf->cis);
195 if (error) {
196 aprint_error_dev(sc->sc_dev, "couldn't read CIS\n");
197 SET(sf->flags, SFF_ERROR);
198 goto out;
201 sdmmc_check_cis_quirks(sf);
203 #ifdef SDMMC_DEBUG
204 if (sdmmcdebug)
205 sdmmc_print_cis(sf);
206 #endif
209 out:
210 SDMMC_UNLOCK(sc);
212 return error;
216 * Indicate whether the function is ready to operate.
218 static int
219 sdmmc_io_function_ready(struct sdmmc_function *sf)
221 struct sdmmc_softc *sc = sf->sc;
222 struct sdmmc_function *sf0 = sc->sc_fn0;
223 uint8_t reg;
225 if (sf->number == 0)
226 return 1; /* FN0 is always ready */
228 SDMMC_LOCK(sc);
229 reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_IOREADY);
230 SDMMC_UNLOCK(sc);
231 return (reg & (1 << sf->number)) != 0;
235 sdmmc_io_function_enable(struct sdmmc_function *sf)
237 struct sdmmc_softc *sc = sf->sc;
238 struct sdmmc_function *sf0 = sc->sc_fn0;
239 uint8_t reg;
240 int retry;
242 if (sf->number == 0)
243 return 0; /* FN0 is always enabled */
245 SDMMC_LOCK(sc);
246 reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_ENABLE);
247 SET(reg, (1U << sf->number));
248 sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_ENABLE, reg);
249 SDMMC_UNLOCK(sc);
251 retry = 5;
252 while (!sdmmc_io_function_ready(sf) && retry-- > 0)
253 kpause("pause", false, hz, NULL);
254 return (retry >= 0) ? 0 : ETIMEDOUT;
258 * Disable the I/O function. Return zero if the function was
259 * disabled successfully.
261 void
262 sdmmc_io_function_disable(struct sdmmc_function *sf)
264 struct sdmmc_softc *sc = sf->sc;
265 struct sdmmc_function *sf0 = sc->sc_fn0;
266 uint8_t reg;
268 if (sf->number == 0)
269 return; /* FN0 is always enabled */
271 SDMMC_LOCK(sc);
272 reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_ENABLE);
273 CLR(reg, (1U << sf->number));
274 sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_ENABLE, reg);
275 SDMMC_UNLOCK(sc);
278 static int
279 sdmmc_io_rw_direct(struct sdmmc_softc *sc, struct sdmmc_function *sf,
280 int reg, u_char *datap, int arg)
282 struct sdmmc_command cmd;
283 int error;
285 /* Don't lock */
287 /* Make sure the card is selected. */
288 error = sdmmc_select_card(sc, sf);
289 if (error)
290 return error;
292 arg |= ((sf == NULL ? 0 : sf->number) & SD_ARG_CMD52_FUNC_MASK) <<
293 SD_ARG_CMD52_FUNC_SHIFT;
294 arg |= (reg & SD_ARG_CMD52_REG_MASK) <<
295 SD_ARG_CMD52_REG_SHIFT;
296 arg |= (*datap & SD_ARG_CMD52_DATA_MASK) <<
297 SD_ARG_CMD52_DATA_SHIFT;
299 memset(&cmd, 0, sizeof cmd);
300 cmd.c_opcode = SD_IO_RW_DIRECT;
301 cmd.c_arg = arg;
302 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R5;
304 error = sdmmc_mmc_command(sc, &cmd);
305 *datap = SD_R5_DATA(cmd.c_resp);
307 return error;
311 * Useful values of `arg' to pass in are either SD_ARG_CMD53_READ or
312 * SD_ARG_CMD53_WRITE. SD_ARG_CMD53_INCREMENT may be ORed into `arg'
313 * to access successive register locations instead of accessing the
314 * same register many times.
316 static int
317 sdmmc_io_rw_extended(struct sdmmc_softc *sc, struct sdmmc_function *sf,
318 int reg, u_char *datap, int datalen, int arg)
320 struct sdmmc_command cmd;
321 int error;
323 /* Don't lock */
325 #if 0
326 /* Make sure the card is selected. */
327 error = sdmmc_select_card(sc, sf);
328 if (error)
329 return error;
330 #endif
332 arg |= (((sf == NULL) ? 0 : sf->number) & SD_ARG_CMD53_FUNC_MASK) <<
333 SD_ARG_CMD53_FUNC_SHIFT;
334 arg |= (reg & SD_ARG_CMD53_REG_MASK) <<
335 SD_ARG_CMD53_REG_SHIFT;
336 arg |= (datalen & SD_ARG_CMD53_LENGTH_MASK) <<
337 SD_ARG_CMD53_LENGTH_SHIFT;
339 memset(&cmd, 0, sizeof cmd);
340 cmd.c_opcode = SD_IO_RW_EXTENDED;
341 cmd.c_arg = arg;
342 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R5;
343 cmd.c_data = datap;
344 cmd.c_datalen = datalen;
345 cmd.c_blklen = MIN(datalen,
346 sdmmc_chip_host_maxblklen(sc->sc_sct,sc->sc_sch));
347 if (!ISSET(arg, SD_ARG_CMD53_WRITE))
348 cmd.c_flags |= SCF_CMD_READ;
350 error = sdmmc_mmc_command(sc, &cmd);
352 return error;
355 uint8_t
356 sdmmc_io_read_1(struct sdmmc_function *sf, int reg)
358 uint8_t data = 0;
360 /* Don't lock */
362 (void)sdmmc_io_rw_direct(sf->sc, sf, reg, (u_char *)&data,
363 SD_ARG_CMD52_READ);
364 return data;
367 void
368 sdmmc_io_write_1(struct sdmmc_function *sf, int reg, uint8_t data)
371 /* Don't lock */
373 (void)sdmmc_io_rw_direct(sf->sc, sf, reg, (u_char *)&data,
374 SD_ARG_CMD52_WRITE);
377 uint16_t
378 sdmmc_io_read_2(struct sdmmc_function *sf, int reg)
380 uint16_t data = 0;
382 /* Don't lock */
384 (void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 2,
385 SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
386 return data;
389 void
390 sdmmc_io_write_2(struct sdmmc_function *sf, int reg, uint16_t data)
393 /* Don't lock */
395 (void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 2,
396 SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
399 uint32_t
400 sdmmc_io_read_4(struct sdmmc_function *sf, int reg)
402 uint32_t data = 0;
404 /* Don't lock */
406 (void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 4,
407 SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
408 return data;
411 void
412 sdmmc_io_write_4(struct sdmmc_function *sf, int reg, uint32_t data)
415 /* Don't lock */
417 (void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 4,
418 SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
423 sdmmc_io_read_multi_1(struct sdmmc_function *sf, int reg, u_char *data,
424 int datalen)
426 int error;
428 /* Don't lock */
430 while (datalen > SD_ARG_CMD53_LENGTH_MAX) {
431 error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
432 SD_ARG_CMD53_LENGTH_MAX, SD_ARG_CMD53_READ);
433 if (error)
434 goto error;
435 data += SD_ARG_CMD53_LENGTH_MAX;
436 datalen -= SD_ARG_CMD53_LENGTH_MAX;
439 error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
440 SD_ARG_CMD53_READ);
441 error:
442 return error;
446 sdmmc_io_write_multi_1(struct sdmmc_function *sf, int reg, u_char *data,
447 int datalen)
449 int error;
451 /* Don't lock */
453 while (datalen > SD_ARG_CMD53_LENGTH_MAX) {
454 error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
455 SD_ARG_CMD53_LENGTH_MAX, SD_ARG_CMD53_WRITE);
456 if (error)
457 goto error;
458 data += SD_ARG_CMD53_LENGTH_MAX;
459 datalen -= SD_ARG_CMD53_LENGTH_MAX;
462 error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
463 SD_ARG_CMD53_WRITE);
464 error:
465 return error;
468 #if 0
469 static int
470 sdmmc_io_xchg(struct sdmmc_softc *sc, struct sdmmc_function *sf,
471 int reg, u_char *datap)
474 /* Don't lock */
476 return sdmmc_io_rw_direct(sc, sf, reg, datap,
477 SD_ARG_CMD52_WRITE|SD_ARG_CMD52_EXCHANGE);
479 #endif
482 * Reset the I/O functions of the card.
484 static void
485 sdmmc_io_reset(struct sdmmc_softc *sc)
488 /* Don't lock */
489 #if 0 /* XXX command fails */
490 (void)sdmmc_io_write(sc, NULL, SD_IO_REG_CCCR_CTL, CCCR_CTL_RES);
491 sdmmc_delay(100000);
492 #endif
496 * Get or set the card's I/O OCR value (SDIO).
498 static int
499 sdmmc_io_send_op_cond(struct sdmmc_softc *sc, u_int32_t ocr, u_int32_t *ocrp)
501 struct sdmmc_command cmd;
502 int error;
503 int retry;
505 DPRINTF(("sdmmc_io_send_op_cond: ocr = %#x\n", ocr));
507 /* Don't lock */
510 * If we change the OCR value, retry the command until the OCR
511 * we receive in response has the "CARD BUSY" bit set, meaning
512 * that all cards are ready for identification.
514 for (retry = 0; retry < 100; retry++) {
515 memset(&cmd, 0, sizeof cmd);
516 cmd.c_opcode = SD_IO_SEND_OP_COND;
517 cmd.c_arg = ocr;
518 cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R4;
520 error = sdmmc_mmc_command(sc, &cmd);
521 if (error)
522 break;
523 if (ISSET(MMC_R4(cmd.c_resp), SD_IO_OCR_MEM_READY) || ocr == 0)
524 break;
526 error = ETIMEDOUT;
527 sdmmc_delay(10000);
529 if (error == 0 && ocrp != NULL)
530 *ocrp = MMC_R4(cmd.c_resp);
532 DPRINTF(("sdmmc_io_send_op_cond: error = %d\n", error));
534 return error;
538 * Card interrupt handling
541 void
542 sdmmc_intr_enable(struct sdmmc_function *sf)
544 struct sdmmc_softc *sc = sf->sc;
545 struct sdmmc_function *sf0 = sc->sc_fn0;
546 uint8_t reg;
548 SDMMC_LOCK(sc);
549 reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_INTEN);
550 reg |= 1 << sf->number;
551 sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_INTEN, reg);
552 SDMMC_UNLOCK(sc);
555 void
556 sdmmc_intr_disable(struct sdmmc_function *sf)
558 struct sdmmc_softc *sc = sf->sc;
559 struct sdmmc_function *sf0 = sc->sc_fn0;
560 uint8_t reg;
562 SDMMC_LOCK(sc);
563 reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_INTEN);
564 reg &= ~(1 << sf->number);
565 sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_INTEN, reg);
566 SDMMC_UNLOCK(sc);
570 * Establish a handler for the SDIO card interrupt. Because the
571 * interrupt may be shared with different SDIO functions, multiple
572 * handlers can be established.
574 void *
575 sdmmc_intr_establish(device_t dev, int (*fun)(void *), void *arg,
576 const char *name)
578 struct sdmmc_softc *sc = device_private(dev);
579 struct sdmmc_intr_handler *ih;
580 int s;
582 if (sc->sc_sct->card_enable_intr == NULL)
583 return NULL;
585 ih = malloc(sizeof *ih, M_DEVBUF, M_WAITOK|M_CANFAIL|M_ZERO);
586 if (ih == NULL)
587 return NULL;
589 ih->ih_name = malloc(strlen(name) + 1, M_DEVBUF,
590 M_WAITOK|M_CANFAIL|M_ZERO);
591 if (ih->ih_name == NULL) {
592 free(ih, M_DEVBUF);
593 return NULL;
595 strlcpy(ih->ih_name, name, strlen(name));
596 ih->ih_softc = sc;
597 ih->ih_fun = fun;
598 ih->ih_arg = arg;
600 s = splhigh();
601 if (TAILQ_EMPTY(&sc->sc_intrq)) {
602 sdmmc_intr_enable(sc->sc_fn0);
603 sdmmc_chip_card_enable_intr(sc->sc_sct, sc->sc_sch, 1);
605 TAILQ_INSERT_TAIL(&sc->sc_intrq, ih, entry);
606 splx(s);
608 return ih;
612 * Disestablish the given handler.
614 void
615 sdmmc_intr_disestablish(void *cookie)
617 struct sdmmc_intr_handler *ih = cookie;
618 struct sdmmc_softc *sc = ih->ih_softc;
619 int s;
621 if (sc->sc_sct->card_enable_intr == NULL)
622 return;
624 s = splhigh();
625 TAILQ_REMOVE(&sc->sc_intrq, ih, entry);
626 if (TAILQ_EMPTY(&sc->sc_intrq)) {
627 sdmmc_chip_card_enable_intr(sc->sc_sct, sc->sc_sch, 0);
628 sdmmc_intr_disable(sc->sc_fn0);
630 splx(s);
632 free(ih->ih_name, M_DEVBUF);
633 free(ih, M_DEVBUF);
637 * Call established SDIO card interrupt handlers. The host controller
638 * must call this function from its own interrupt handler to handle an
639 * SDIO interrupt from the card.
641 void
642 sdmmc_card_intr(device_t dev)
644 struct sdmmc_softc *sc = device_private(dev);
646 if (sc->sc_sct->card_enable_intr) {
647 mutex_enter(&sc->sc_intr_task_mtx);
648 if (!sdmmc_task_pending(&sc->sc_intr_task))
649 sdmmc_add_task(sc, &sc->sc_intr_task);
650 mutex_exit(&sc->sc_intr_task_mtx);
654 void
655 sdmmc_intr_task(void *arg)
657 struct sdmmc_softc *sc = (struct sdmmc_softc *)arg;
658 struct sdmmc_intr_handler *ih;
659 int s;
661 s = splsdmmc();
662 TAILQ_FOREACH(ih, &sc->sc_intrq, entry) {
663 splx(s);
664 /* XXX examine return value and do evcount stuff*/
665 (void)(*ih->ih_fun)(ih->ih_arg);
666 s = splsdmmc();
668 sdmmc_chip_card_intr_ack(sc->sc_sct, sc->sc_sch);
669 splx(s);