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 $ */
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>
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>
37 #define DPRINTF(s) do { printf s; } while (0)
39 #define DPRINTF(s) do {} while (0)
42 struct sdmmc_intr_handler
{
43 struct sdmmc_softc
*ih_softc
;
45 int (*ih_fun
)(void *);
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);
55 static int sdmmc_io_xchg(struct sdmmc_softc
*, struct sdmmc_function
*,
58 static void sdmmc_io_reset(struct sdmmc_softc
*);
59 static int sdmmc_io_send_op_cond(struct sdmmc_softc
*, 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
68 sdmmc_io_enable(struct sdmmc_softc
*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. */
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
);
90 /* No SDIO card; switch to SD memory-only mode. */
91 CLR(sc
->sc_flags
, SMF_IO_MODE
);
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
);
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
);
116 aprint_error_dev(sc
->sc_dev
,
117 "couldn't supply voltage requested by card\n");
121 /* Reset I/O functions (again). */
124 /* Send the new OCR value until all cards are ready. */
125 error
= sdmmc_io_send_op_cond(sc
, host_ocr
, NULL
);
127 aprint_error_dev(sc
->sc_dev
, "couldn't send I/O OCR\n");
138 * Allocate sdmmc_function structures for SD card I/O function
139 * (including function 0).
142 sdmmc_io_scan(struct sdmmc_softc
*sc
)
144 struct sdmmc_function
*sf0
, *sf
;
150 sf0
= sdmmc_function_alloc(sc
);
152 error
= sdmmc_set_relative_addr(sc
, sf0
);
154 aprint_error_dev(sc
->sc_dev
, "couldn't set I/O RCA\n");
155 SET(sf0
->flags
, SFF_ERROR
);
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
);
164 aprint_error_dev(sc
->sc_dev
, "couldn't select I/O RCA %d\n",
166 SET(sf0
->flags
, SFF_ERROR
);
170 for (i
= 1; i
<= sc
->sc_function_count
; i
++) {
171 sf
= sdmmc_function_alloc(sc
);
174 SIMPLEQ_INSERT_TAIL(&sc
->sf_head
, sf
, sf_list
);
182 * Initialize SDIO card functions.
185 sdmmc_io_init(struct sdmmc_softc
*sc
, struct sdmmc_function
*sf
)
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
);
196 aprint_error_dev(sc
->sc_dev
, "couldn't read CIS\n");
197 SET(sf
->flags
, SFF_ERROR
);
201 sdmmc_check_cis_quirks(sf
);
216 * Indicate whether the function is ready to operate.
219 sdmmc_io_function_ready(struct sdmmc_function
*sf
)
221 struct sdmmc_softc
*sc
= sf
->sc
;
222 struct sdmmc_function
*sf0
= sc
->sc_fn0
;
226 return 1; /* FN0 is always ready */
229 reg
= sdmmc_io_read_1(sf0
, SD_IO_CCCR_FN_IOREADY
);
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
;
243 return 0; /* FN0 is always enabled */
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
);
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.
262 sdmmc_io_function_disable(struct sdmmc_function
*sf
)
264 struct sdmmc_softc
*sc
= sf
->sc
;
265 struct sdmmc_function
*sf0
= sc
->sc_fn0
;
269 return; /* FN0 is always enabled */
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
);
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
;
287 /* Make sure the card is selected. */
288 error
= sdmmc_select_card(sc
, sf
);
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
;
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
);
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.
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
;
326 /* Make sure the card is selected. */
327 error
= sdmmc_select_card(sc
, sf
);
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
;
342 cmd
.c_flags
= SCF_CMD_AC
| SCF_RSP_R5
;
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
);
356 sdmmc_io_read_1(struct sdmmc_function
*sf
, int reg
)
362 (void)sdmmc_io_rw_direct(sf
->sc
, sf
, reg
, (u_char
*)&data
,
368 sdmmc_io_write_1(struct sdmmc_function
*sf
, int reg
, uint8_t data
)
373 (void)sdmmc_io_rw_direct(sf
->sc
, sf
, reg
, (u_char
*)&data
,
378 sdmmc_io_read_2(struct sdmmc_function
*sf
, int reg
)
384 (void)sdmmc_io_rw_extended(sf
->sc
, sf
, reg
, (u_char
*)&data
, 2,
385 SD_ARG_CMD53_READ
| SD_ARG_CMD53_INCREMENT
);
390 sdmmc_io_write_2(struct sdmmc_function
*sf
, int reg
, uint16_t data
)
395 (void)sdmmc_io_rw_extended(sf
->sc
, sf
, reg
, (u_char
*)&data
, 2,
396 SD_ARG_CMD53_WRITE
| SD_ARG_CMD53_INCREMENT
);
400 sdmmc_io_read_4(struct sdmmc_function
*sf
, int reg
)
406 (void)sdmmc_io_rw_extended(sf
->sc
, sf
, reg
, (u_char
*)&data
, 4,
407 SD_ARG_CMD53_READ
| SD_ARG_CMD53_INCREMENT
);
412 sdmmc_io_write_4(struct sdmmc_function
*sf
, int reg
, uint32_t data
)
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
,
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
);
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
,
446 sdmmc_io_write_multi_1(struct sdmmc_function
*sf
, int reg
, u_char
*data
,
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
);
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
,
470 sdmmc_io_xchg(struct sdmmc_softc
*sc
, struct sdmmc_function
*sf
,
471 int reg
, u_char
*datap
)
476 return sdmmc_io_rw_direct(sc
, sf
, reg
, datap
,
477 SD_ARG_CMD52_WRITE
|SD_ARG_CMD52_EXCHANGE
);
482 * Reset the I/O functions of the card.
485 sdmmc_io_reset(struct sdmmc_softc
*sc
)
489 #if 0 /* XXX command fails */
490 (void)sdmmc_io_write(sc
, NULL
, SD_IO_REG_CCCR_CTL
, CCCR_CTL_RES
);
496 * Get or set the card's I/O OCR value (SDIO).
499 sdmmc_io_send_op_cond(struct sdmmc_softc
*sc
, u_int32_t ocr
, u_int32_t
*ocrp
)
501 struct sdmmc_command cmd
;
505 DPRINTF(("sdmmc_io_send_op_cond: ocr = %#x\n", ocr
));
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
;
518 cmd
.c_flags
= SCF_CMD_BCR
| SCF_RSP_R4
;
520 error
= sdmmc_mmc_command(sc
, &cmd
);
523 if (ISSET(MMC_R4(cmd
.c_resp
), SD_IO_OCR_MEM_READY
) || ocr
== 0)
529 if (error
== 0 && ocrp
!= NULL
)
530 *ocrp
= MMC_R4(cmd
.c_resp
);
532 DPRINTF(("sdmmc_io_send_op_cond: error = %d\n", error
));
538 * Card interrupt handling
542 sdmmc_intr_enable(struct sdmmc_function
*sf
)
544 struct sdmmc_softc
*sc
= sf
->sc
;
545 struct sdmmc_function
*sf0
= sc
->sc_fn0
;
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
);
556 sdmmc_intr_disable(struct sdmmc_function
*sf
)
558 struct sdmmc_softc
*sc
= sf
->sc
;
559 struct sdmmc_function
*sf0
= sc
->sc_fn0
;
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
);
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.
575 sdmmc_intr_establish(device_t dev
, int (*fun
)(void *), void *arg
,
578 struct sdmmc_softc
*sc
= device_private(dev
);
579 struct sdmmc_intr_handler
*ih
;
582 if (sc
->sc_sct
->card_enable_intr
== NULL
)
585 ih
= malloc(sizeof *ih
, M_DEVBUF
, M_WAITOK
|M_CANFAIL
|M_ZERO
);
589 ih
->ih_name
= malloc(strlen(name
) + 1, M_DEVBUF
,
590 M_WAITOK
|M_CANFAIL
|M_ZERO
);
591 if (ih
->ih_name
== NULL
) {
595 strlcpy(ih
->ih_name
, name
, strlen(name
));
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
);
612 * Disestablish the given handler.
615 sdmmc_intr_disestablish(void *cookie
)
617 struct sdmmc_intr_handler
*ih
= cookie
;
618 struct sdmmc_softc
*sc
= ih
->ih_softc
;
621 if (sc
->sc_sct
->card_enable_intr
== NULL
)
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
);
632 free(ih
->ih_name
, 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.
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
);
655 sdmmc_intr_task(void *arg
)
657 struct sdmmc_softc
*sc
= (struct sdmmc_softc
*)arg
;
658 struct sdmmc_intr_handler
*ih
;
662 TAILQ_FOREACH(ih
, &sc
->sc_intrq
, entry
) {
664 /* XXX examine return value and do evcount stuff*/
665 (void)(*ih
->ih_fun
)(ih
->ih_arg
);
668 sdmmc_chip_card_intr_ack(sc
->sc_sct
, sc
->sc_sch
);