1 #include <minix/blockdriver.h>
2 #include <minix/board.h>
4 #include <minix/mmio.h>
5 #include <minix/spin.h>
6 #include <minix/syslib.h>
14 /* MINIX IRQ timeout. Twice the host controller data/busy timeout @ 48MHz. */
15 #define IRQ_TIMEOUT 5600000 /* 5,600,000 us */
17 #define MMCHS_TIMEOUT 500000 /* 500,000 us */
19 /* Reference clock frequency divisors: */
20 #define MMCHS_SD_SYSCTL_CLKD_400KHZ 240 /* 96MHz/400kHz */
21 #define MMCHS_SD_SYSCTL_CLKD_26MHZ 4 /* ceiling 96MHz/26MHz */
22 #define MMCHS_SD_SYSCTL_CLKD_52MHZ 2 /* ceiling 96MHz/52MHz */
24 /* The host SD_DATA register is 128 words (512B). */
25 #define SD_DATA_WLEN 128
28 * Card initialization timeout, twice the standard:
29 * "The device must complete its initialization within 1 second of the first
30 * CMD1 issued with a valid OCR range." (MMCA, 4.41)
32 #define CARD_INI_TIMEOUT 2000000 /* 2,000,000 us */
34 /* Card EXT_CSD register fields. */
35 #define MMC_EXT_CSD_SEC_COUNT (*(uint32_t *)&card_ext_csd[212])
36 #define MMC_EXT_CSD_CARD_TYPE (card_ext_csd[196])
37 #define MMC_EXT_CSD_CARD_TYPE_HS_MMC_52MHZ (0x1 << 1)
39 /* Card intended operating voltage range: 2.7V to 3.6V */
40 #define MMC_OCR_VDD_RANGE 0x00FF8000
42 /* Error bits in the card status (R1) response. */
43 #define R1_ERROR_MASK 0xFDFFA080
45 /* Relative Card Address. Must be greater than 1. */
48 /* The card sector size is 512B. */
52 * AM335x Control Module registers CONF_GPMC_ADn.
53 * Configuration do multiplex CONF_GPMC_ADn to signals MMC1_DATn (Mode 1).
55 #define CONF_GPMC_AD(N) (0x800 + 4*(N))
56 #define CONF_GPMC_AD_MASK 0x7F
57 #define CONF_GPMC_AD_VAL 0x31
59 /* AM335x MMC1 memory map (physical start address and size). */
60 #define AM335X_MMC1_BASE_ADDR 0x481D8000
61 #define AM335X_MMC1_SIZE (4 << 10)
62 /* AM335x MMC1 interrupt number. */
63 #define AM335X_MMCSD1INT 28
65 static uint32_t bus_width
;
67 /* AM335x MMCHS registers virtual addresses: virtual base + offset. */
68 static struct omap_mmchs_registers
*reg
;
71 static uint32_t card_csd
[4];
72 static uint8_t card_ext_csd
[512];
74 static uint32_t card_write_protect
;
75 static uint64_t card_size
;
77 /* IRQ_HOOK_ID for SYS_IRQCTL kernel call. */
78 static int hook_id
= 1;
80 /* Initialize the log system. */
81 static struct log log
= {
83 .log_level
= LEVEL_INFO
,
84 .log_func
= default_log
,
89 * Spin until a register flag is set, or the time runs out.
90 * Return the flag value.
93 spin_until_set(uint32_t address
, uint32_t flag
)
99 spin_init(&s
, MMCHS_TIMEOUT
);
101 spin
= spin_check(&s
);
102 v
= (read32(address
) & flag
);
103 } while ((v
== 0) && (spin
== TRUE
));
109 * Spin until a register flag is clear, or the time runs out.
110 * Return the flag value.
113 spin_until_clear(uint32_t address
, uint32_t flag
)
119 spin_init(&s
, MMCHS_TIMEOUT
);
121 spin
= spin_check(&s
);
122 v
= (read32(address
) & flag
);
123 } while ((v
!= 0) && (spin
== TRUE
));
129 * Change the bus clock frequency (divisor).
130 * Return 0 on success, a negative integer on error.
133 set_bus_clkd(uint32_t clkd
)
136 * Disable the bus clock, set the clock divider, wait until the
137 * internal clock is stable, enable the bus clock.
139 set32(reg
->SYSCTL
, MMCHS_SD_SYSCTL_CEN
, MMCHS_SD_SYSCTL_CEN_DIS
);
140 set32(reg
->SYSCTL
, MMCHS_SD_SYSCTL_CLKD
, clkd
<< 6);
141 if (spin_until_set(reg
->SYSCTL
, MMCHS_SD_SYSCTL_ICS
)
142 == MMCHS_SD_SYSCTL_ICS_UNSTABLE
)
144 set32(reg
->SYSCTL
, MMCHS_SD_SYSCTL_CEN
, MMCHS_SD_SYSCTL_CEN_EN
);
150 * Receive an interrupt request.
151 * Return 0 on success, a negative integer on error.
160 if (driver_receive(ANY
, &m
, &ipc_status
) != OK
)
162 if (is_ipc_notify(ipc_status
)
163 && (_ENDPOINT_P(m
.m_source
) == CLOCK
))
165 if (is_ipc_notify(ipc_status
)
166 && (_ENDPOINT_P(m
.m_source
) == HARDWARE
))
169 * m will be discarded if the driver is out of memory.
171 blockdriver_mq_queue(&m
, ipc_status
);
176 * Wait for an interrupt request.
177 * Return 0 on interrupt, a negative integer on error.
184 if (sys_irqenable(&hook_id
) != OK
)
186 sys_setalarm(micros_to_ticks(IRQ_TIMEOUT
), 0);
190 sys_irqdisable(&hook_id
);
196 * Software reset for mmc_cmd or mmc_dat line.
199 reset_mmchs_fsm(uint32_t line
)
202 * "The proper procedure is: (a) Set to 1 to start reset,
203 * (b) Poll for 1 to identify start of reset, and
204 * (c) Poll for 0 to identify reset is complete." (AM335x TRM)
206 set32(reg
->SYSCTL
, line
, line
);
207 spin_until_set(reg
->SYSCTL
, line
);
208 spin_until_clear(reg
->SYSCTL
, line
);
212 * Send a command to the card.
213 * Return 0 on success, a negative integer on error.
216 send_cmd(uint32_t arg
, uint32_t cmd
)
220 if (read32(reg
->PSTATE
)
221 & (MMCHS_SD_PSTATE_DATI
| MMCHS_SD_PSTATE_CMDI
))
222 return -1; /* Issuing of commands is not allowed. */
223 write32(reg
->ARG
, arg
);
224 write32(reg
->CMD
, cmd
);
225 /* Wait for the command completion. */
228 stat
= read32(reg
->SD_STAT
);
230 * Clear only the command status/error bits. The transfer status/error
231 * bits (including ERRI) must be preserved.
233 write32(reg
->SD_STAT
, MMCHS_SD_STAT_CIE
238 if (stat
& MMCHS_SD_STAT_CTO
) {
239 reset_mmchs_fsm(MMCHS_SD_SYSCTL_SRC
);
247 * Send a command to the card, and check for errors in the response (R1).
248 * Return 0 on success, a negative integer on error.
251 send_cmd_check_r1(uint32_t arg
, uint32_t cmd
)
253 if (send_cmd(arg
, cmd
) < 0)
255 /* Check for card errors in the card response (R1). */
256 if (read32(reg
->RSP10
) & R1_ERROR_MASK
)
262 /* Send CMD0 (GO_IDLE_STATE) command to the card. */
266 return send_cmd(MMC_GO_IDLE_STATE
, MMC_GO_IDLE_STATE
);
269 /* Send CMD1 (SEND_OP_COND) command to the card. */
275 /* The driver is capable of handling sector type of addressing. */
276 cmd
= MMCHS_SD_CMD_INDX_CMD(MMC_SEND_OP_COND
)
277 | MMCHS_SD_CMD_RSP_TYPE_48B
;
278 return send_cmd((MMC_OCR_HCS
| MMC_OCR_VDD_RANGE
), cmd
);
281 /* Send CMD2 (ALL_SEND_CID) command to the card. */
287 cmd
= MMCHS_SD_CMD_INDX_CMD(MMC_ALL_SEND_CID
)
288 | MMCHS_SD_CMD_CCCE_ENABLE
289 | MMCHS_SD_CMD_RSP_TYPE_136B
;
290 return send_cmd(0, cmd
);
293 /* Send CMD3 (SET_RELATIVE_ADDR) command to the card. */
295 set_relative_addr(void)
299 cmd
= MMCHS_SD_CMD_INDX_CMD(MMC_SET_RELATIVE_ADDR
)
300 | MMCHS_SD_CMD_CICE_ENABLE
301 | MMCHS_SD_CMD_CCCE_ENABLE
302 | MMCHS_SD_CMD_RSP_TYPE_48B
;
303 return send_cmd_check_r1(MMC_ARG_RCA(RCA
), cmd
);
306 /* Send CMD6 (SWITCH) command to the card. */
308 mmc_switch(uint32_t access
, uint32_t index
, uint32_t value
)
312 /* SWITCH argument: [25:24] Access, [23:16] Index, [15:8] Value. */
313 arg
= (access
<< 24) | (index
<< 16) | (value
<< 8);
314 cmd
= MMCHS_SD_CMD_INDX_CMD(MMC_SWITCH
)
315 | MMCHS_SD_CMD_CICE_ENABLE
316 | MMCHS_SD_CMD_CCCE_ENABLE
317 | MMCHS_SD_CMD_RSP_TYPE_48B_BUSY
;
318 return send_cmd_check_r1(arg
, cmd
);
321 /* Send CMD7 (SELECT_CARD) command to the card. */
327 cmd
= MMCHS_SD_CMD_INDX_CMD(MMC_SELECT_CARD
)
328 | MMCHS_SD_CMD_CICE_ENABLE
329 | MMCHS_SD_CMD_CCCE_ENABLE
330 | MMCHS_SD_CMD_RSP_TYPE_48B
;
331 return send_cmd_check_r1(MMC_ARG_RCA(RCA
), cmd
);
334 /* Send CMD8 (SEND_EXT_CSD) command to the card. */
340 cmd
= MMCHS_SD_CMD_INDX_CMD(MMC_SEND_EXT_CSD
)
341 | MMCHS_SD_CMD_DP_DATA
342 | MMCHS_SD_CMD_CICE_ENABLE
343 | MMCHS_SD_CMD_CCCE_ENABLE
344 | MMCHS_SD_CMD_RSP_TYPE_48B
345 | MMCHS_SD_CMD_DDIR_READ
;
346 return send_cmd_check_r1(0, cmd
);
349 /* Send CMD9 (SEND_CSD) command to the card. */
355 cmd
= MMCHS_SD_CMD_INDX_CMD(MMC_SEND_CSD
)
356 | MMCHS_SD_CMD_CCCE_ENABLE
357 | MMCHS_SD_CMD_RSP_TYPE_136B
;
358 return send_cmd(MMC_ARG_RCA(RCA
), cmd
);
361 /* Send CMD13 (SEND_STATUS) command to the card. */
367 cmd
= MMCHS_SD_CMD_INDX_CMD(MMC_SEND_STATUS
)
368 | MMCHS_SD_CMD_CICE_ENABLE
369 | MMCHS_SD_CMD_CCCE_ENABLE
370 | MMCHS_SD_CMD_RSP_TYPE_48B
;
371 return send_cmd_check_r1(MMC_ARG_RCA(RCA
), cmd
);
374 /* Send CMD16 (SET_BLOCKLEN) command to the card. */
380 /* Set block length to sector size (512B). */
381 cmd
= MMCHS_SD_CMD_INDX_CMD(MMC_SET_BLOCKLEN
)
382 | MMCHS_SD_CMD_CICE_ENABLE
383 | MMCHS_SD_CMD_CCCE_ENABLE
384 | MMCHS_SD_CMD_RSP_TYPE_48B
;
385 return send_cmd_check_r1(SEC_SIZE
, cmd
);
388 /* Send CMD17 (READ_SINGLE_BLOCK) to the card. */
390 read_single_block(uint32_t addr
)
394 cmd
= MMCHS_SD_CMD_INDX_CMD(MMC_READ_BLOCK_SINGLE
)
395 | MMCHS_SD_CMD_DP_DATA
396 | MMCHS_SD_CMD_CICE_ENABLE
397 | MMCHS_SD_CMD_CCCE_ENABLE
398 | MMCHS_SD_CMD_RSP_TYPE_48B
399 | MMCHS_SD_CMD_DDIR_READ
;
400 return send_cmd_check_r1(addr
, cmd
);
403 /* Send CMD24 (WRITE_BLOCK) to the card. */
405 write_block(uint32_t addr
)
409 cmd
= MMCHS_SD_CMD_INDX_CMD(MMC_WRITE_BLOCK_SINGLE
)
410 | MMCHS_SD_CMD_DP_DATA
411 | MMCHS_SD_CMD_CICE_ENABLE
412 | MMCHS_SD_CMD_CCCE_ENABLE
413 | MMCHS_SD_CMD_RSP_TYPE_48B
414 | MMCHS_SD_CMD_DDIR_WRITE
;
415 return send_cmd_check_r1(addr
, cmd
);
419 * Repeat CMD1 until the card is ready, or the time runs out.
420 * Return 0 on ready, a negative integer on error.
423 repeat_send_op_cond(void)
429 spin_init(&s
, CARD_INI_TIMEOUT
);
431 spin
= spin_check(&s
);
432 if (send_op_cond() < 0)
434 card_ocr
= read32(reg
->RSP10
);
435 } while (((card_ocr
& MMC_OCR_MEM_READY
) == 0) && (spin
== TRUE
));
437 if ((card_ocr
& MMC_OCR_MEM_READY
) == 0)
438 return -1; /* Card is still busy. */
444 * Read (receive) the busy signal from the card.
445 * Return 0 on success, a negative integer on error.
452 * The busy signal is optional, but the host controller will assert
453 * SD_STAT[1] TC even if the card does not send it.
457 stat
= read32(reg
->SD_STAT
);
458 write32(reg
->SD_STAT
, MMCHS_SD_STAT_DCRC
461 if (stat
& MMCHS_SD_STAT_ERRI
) {
462 reset_mmchs_fsm(MMCHS_SD_SYSCTL_SRD
);
470 * Read (receive) data from the card.
471 * Return 0 on success, a negative integer on error.
474 read_data(uint32_t *data
)
478 /* Wait for BRR interrupt. */
481 if (read32(reg
->SD_STAT
) & MMCHS_SD_STAT_BRR
) {
482 write32(reg
->SD_STAT
, MMCHS_SD_STAT_BRR
);
483 for (i
=SD_DATA_WLEN
; i
>0; i
--)
484 *data
++ = read32(reg
->DATA
);
487 /* Wait for TC or ERRI interrupt. */
490 stat
= read32(reg
->SD_STAT
);
491 write32(reg
->SD_STAT
, MMCHS_SD_STAT_DEB
495 if (stat
& MMCHS_SD_STAT_ERRI
) {
496 reset_mmchs_fsm(MMCHS_SD_SYSCTL_SRD
);
504 * Write (send) data to the card.
505 * Return 0 on success, a negative integer on error.
508 write_data(uint32_t *data
)
512 /* Wait for BWR interrupt. */
515 if (read32(reg
->SD_STAT
) & MMCHS_SD_STAT_BWR
) {
516 write32(reg
->SD_STAT
, MMCHS_SD_STAT_BWR
);
517 for (i
=SD_DATA_WLEN
; i
>0; i
--)
518 write32(reg
->DATA
, *data
++);
521 /* Wait for TC or ERRI interrupt. */
524 stat
= read32(reg
->SD_STAT
);
525 write32(reg
->SD_STAT
, MMCHS_SD_STAT_DEB
529 if (stat
& MMCHS_SD_STAT_ERRI
) {
530 reset_mmchs_fsm(MMCHS_SD_SYSCTL_SRD
);
538 * Read a block from the card.
539 * Return 0 on success, a negative integer on error.
542 cim_read_block(uint32_t addr
, uint32_t *data
)
545 if (read_single_block(addr
) < 0)
547 /* Read from the host buffer. */
548 return read_data(data
);
552 * Write a block to the card.
553 * Return 0 on success, a negative integer on error.
556 cim_write_block(uint32_t addr
, uint32_t *data
)
559 if (write_block(addr
) < 0)
561 /* Write into the host buffer. */
562 if (write_data(data
) < 0)
564 /* CMD13. Check the result of the write operation. */
565 return send_status();
570 * Interface to the MINIX block device driver.
573 emmc_host_set_instance(struct mmc_host
*host
, int instance
)
581 * Initialize the driver and kernel structures.
582 * Return 0 on success, a negative integer on error.
587 struct minix_mem_range mr
;
591 * On the BeagleBone Black, the eMMC device is connected to MMC1.
592 * Add the MMC1 memory address range to the process' resources.
594 mr
.mr_base
= AM335X_MMC1_BASE_ADDR
;
595 mr
.mr_limit
= AM335X_MMC1_BASE_ADDR
+ AM335X_MMC1_SIZE
- 1;
596 if (sys_privctl(SELF
, SYS_PRIV_ADD_MEM
, &mr
) != OK
)
599 /* Map the MMC1 physical base address to a virtual address. */
600 v_base
= (uint32_t)vm_map_phys(SELF
, (void *)mr
.mr_base
,
602 if (v_base
== (uint32_t)MAP_FAILED
)
605 /* Set the registers virtual addresses. */
607 reg
->SYSCONFIG
+= v_base
;
608 reg
->SYSSTATUS
+= v_base
;
613 reg
->RSP10
+= v_base
;
614 reg
->RSP32
+= v_base
;
615 reg
->RSP54
+= v_base
;
616 reg
->RSP76
+= v_base
;
618 reg
->PSTATE
+= v_base
;
620 reg
->SYSCTL
+= v_base
;
621 reg
->SD_STAT
+= v_base
;
625 /* Register the MMC1 interrupt number. */
626 if (sys_irqsetpolicy(AM335X_MMCSD1INT
, 0, &hook_id
) != OK
)
633 * Configure the Control Module registers CONF_GPMC_AD4-7.
634 * Multiplex pins GPMC_AD4-7 to signals MMC1_DAT4-7 (Mode 1).
635 * Return 0 on success, a negative integer on error.
642 for (i
=4; i
<8; i
++) {
643 if (sys_padconf(CONF_GPMC_AD(i
), CONF_GPMC_AD_MASK
,
644 CONF_GPMC_AD_VAL
) != OK
)
651 * Interface to the MINIX block device driver.
652 * Host controller initialization.
653 * Return 0 on success, a negative integer on error.
656 emmc_host_init(struct mmc_host
*host
)
658 struct machine machine
;
660 /* The eMMC is present on the BBB only. */
661 sys_getmachine(&machine
);
662 if (!BOARD_IS_BBB(machine
.board_id
))
665 /* Initialize the driver and kernel structures. */
666 if (minix_init() < 0)
670 * Multiplex pins GPMC_AD4-7 to signals MMC1_DAT4-7 (Mode 1), in order
671 * to allow the use of 8-bit mode.
672 * U-Boot multiplexes only pins GPMC_AD0-3 to signals MMC1_DAT0-3.
674 if (conf_gpmc_ad() < 0)
675 bus_width
= EXT_CSD_BUS_WIDTH_4
;
677 bus_width
= EXT_CSD_BUS_WIDTH_8
;
679 /* Reset the host controller. */
680 set32(reg
->SYSCONFIG
, MMCHS_SD_SYSCONFIG_SOFTRESET
,
681 MMCHS_SD_SYSCONFIG_SOFTRESET
);
682 if (spin_until_set(reg
->SYSSTATUS
, MMCHS_SD_SYSSTATUS_RESETDONE
)
683 != MMCHS_SD_SYSSTATUS_RESETDONE
)
687 * SD_CAPA: "The host driver shall not modify this register after the
688 * initialization." (AM335x TRM)
692 * Set the bus voltage to 3V, and turn the bus power on.
693 * On the BeagleBone Black, the bus voltage is pulled up to 3.3V, but
694 * the MMCHS supports only 1.8V or 3V.
696 set32(reg
->HCTL
, MMCHS_SD_HCTL_SDVS
, MMCHS_SD_HCTL_SDVS_VS30
);
697 set32(reg
->HCTL
, MMCHS_SD_HCTL_SDBP
, MMCHS_SD_HCTL_SDBP_ON
);
698 if (spin_until_set(reg
->HCTL
, MMCHS_SD_HCTL_SDBP
)
699 == MMCHS_SD_HCTL_SDBP_OFF
)
702 /* Set the bus clock frequency to FOD (400kHz). */
703 set32(reg
->SYSCTL
, MMCHS_SD_SYSCTL_CLKD
,
704 MMCHS_SD_SYSCTL_CLKD_400KHZ
<< 6);
706 /* Set data and busy time-out: ~2,6s @ 400kHz.*/
707 set32(reg
->SYSCTL
, MMCHS_SD_SYSCTL_DTO
, MMCHS_SD_SYSCTL_DTO_2POW20
);
709 /* Enable the internal clock. */
710 set32(reg
->SYSCTL
, MMCHS_SD_SYSCTL_ICE
, MMCHS_SD_SYSCTL_ICE_EN
);
711 if (spin_until_set(reg
->SYSCTL
, MMCHS_SD_SYSCTL_ICS
)
712 == MMCHS_SD_SYSCTL_ICS_UNSTABLE
)
715 /* Enable the bus clock. */
716 set32(reg
->SYSCTL
, MMCHS_SD_SYSCTL_CEN
, MMCHS_SD_SYSCTL_CEN_EN
);
719 * Set the internal clock gating strategy to automatic, and enable
720 * Smart Idle mode. The host controller does not implement wake-up
721 * request (SWAKEUP pin is not connected).
723 set32(reg
->SYSCONFIG
, MMCHS_SD_SYSCONFIG_AUTOIDLE
,
724 MMCHS_SD_SYSCONFIG_AUTOIDLE_EN
);
725 set32(reg
->SYSCONFIG
, MMCHS_SD_SYSCONFIG_SIDLEMODE
,
726 MMCHS_SD_SYSCONFIG_SIDLEMODE_IDLE
);
728 /* The driver reads and writes single 512B blocks. */
729 set32(reg
->BLK
, MMCHS_SD_BLK_BLEN
, SEC_SIZE
);
731 /* Enable interrupt status and requests. */
732 write32(reg
->IE
, MMCHS_SD_IE_ERROR_MASK
733 | MMCHS_SD_IE_BRR_ENABLE_ENABLE
734 | MMCHS_SD_IE_BWR_ENABLE_ENABLE
735 | MMCHS_SD_IE_TC_ENABLE_ENABLE
736 | MMCHS_SD_IE_CC_ENABLE_ENABLE
);
737 write32(reg
->ISE
, MMCHS_SD_IE_ERROR_MASK
738 | MMCHS_SD_IE_BRR_ENABLE_ENABLE
739 | MMCHS_SD_IE_BWR_ENABLE_ENABLE
740 | MMCHS_SD_IE_TC_ENABLE_ENABLE
741 | MMCHS_SD_IE_CC_ENABLE_ENABLE
);
747 * Interface to the MINIX block device driver.
751 emmc_set_log_level(int level
)
753 log
.log_level
= level
;
758 * Interface to the MINIX block device driver.
759 * Unused, but declared in mmchost.h.
763 emmc_host_reset(struct mmc_host
*host
)
770 * Interface to the MINIX block device driver.
774 emmc_card_detect(struct sd_slot
*slot
)
776 /* The card is detected during card initialization. */
781 * Interface to the MINIX block device driver.
782 * Card initialization. Also, finish the MMCHS initialization.
783 * Return NULL on error.
785 static struct sd_card
*
786 emmc_card_initialize(struct sd_slot
*slot
)
791 if (go_idle_state() < 0)
795 * Set the MMC_CMD line to open drain.
796 * "The host starts the card identification process in open-drain mode
797 * with the identification clock rate FOD." (MMCA, 4.41)
799 set32(reg
->CON
, MMCHS_SD_CON_OD
, MMCHS_SD_CON_OD_OD
);
802 if (repeat_send_op_cond() < 0)
805 /* CMD2. The driver has no use for the CID. */
806 if (all_send_cid() < 0)
810 if (set_relative_addr() < 0)
814 * Set the MMC_CMD line to push-pull.
815 * "When the card is in Stand-by State, communication over the CMD and
816 * DAT lines will be performed in push-pull mode." (MMCA, 4.41)
818 set32(reg
->CON
, MMCHS_SD_CON_OD
, MMCHS_SD_CON_OD_PP
);
823 card_csd
[0] = read32(reg
->RSP10
);
824 card_csd
[1] = read32(reg
->RSP32
);
825 card_csd
[2] = read32(reg
->RSP54
);
826 card_csd
[3] = read32(reg
->RSP76
);
828 /* Card capacity for cards up to 2GB of density. */
829 card_size
= (uint64_t)MMC_CSD_CAPACITY(card_csd
)
830 << MMC_CSD_READ_BL_LEN(card_csd
);
832 card_write_protect
= (SD_CSD_PERM_WRITE_PROTECT(card_csd
)
833 | SD_CSD_TMP_WRITE_PROTECT(card_csd
));
834 if (card_write_protect
)
835 log_info(&log
, "the eMMC is write protected\n");
838 if (select_card() < 0)
842 if (send_ext_csd() < 0)
844 /* Receive the Extended CSD register. */
845 if (read_data((uint32_t *)card_ext_csd
) < 0)
848 /* Card capacity for densities greater than 2GB. */
849 if (MMC_EXT_CSD_SEC_COUNT
> 0)
850 card_size
= (uint64_t)MMC_EXT_CSD_SEC_COUNT
* SEC_SIZE
;
852 /* CMD6. Switch to high-speed mode: EXT_CSD[185] HS_TIMING = 1. */
853 if (mmc_switch(MMC_SWITCH_MODE_WRITE_BYTE
, EXT_CSD_HS_TIMING
, 1) < 0)
855 /* Wait for the (optional) busy signal. */
858 /* CMD13. Check the result of the SWITCH operation. */
859 if (send_status() < 0)
862 /* Change the bus clock frequency. */
863 if (MMC_EXT_CSD_CARD_TYPE
& MMC_EXT_CSD_CARD_TYPE_HS_MMC_52MHZ
)
864 clkd
= MMCHS_SD_SYSCTL_CLKD_52MHZ
; /* 48 MHz */
866 clkd
= MMCHS_SD_SYSCTL_CLKD_26MHZ
; /* 24 MHz */
867 if (set_bus_clkd(clkd
) < 0)
870 /* Set data and busy time-out: ~ 2,8s @ 48MHz.*/
871 set32(reg
->SYSCTL
, MMCHS_SD_SYSCTL_DTO
, MMCHS_SD_SYSCTL_DTO_2POW27
);
873 /* CMD6. Set data bus width. */
874 if (mmc_switch(MMC_SWITCH_MODE_WRITE_BYTE
, EXT_CSD_BUS_WIDTH
,
877 /* Wait for the (optional) busy signal. */
880 /* CMD13. Check the result of the SWITCH operation. */
881 if (send_status() < 0)
884 /* Host controller: set data bus width. */
885 if (bus_width
== EXT_CSD_BUS_WIDTH_4
)
886 set32(reg
->HCTL
, MMCHS_SD_HCTL_DTW
, MMCHS_SD_HCTL_DTW_4BIT
);
888 set32(reg
->CON
, MMCHS_SD_CON_DW8
, MMCHS_SD_CON_DW8_8BITS
);
890 /* CMD16. Set block length to sector size (512B). */
891 if (set_blocklen() < 0)
894 /* Initialize the block device driver structures. */
895 slot
->card
.blk_size
= SEC_SIZE
;
896 slot
->card
.blk_count
= card_size
/ SEC_SIZE
;
897 slot
->card
.state
= SD_MODE_DATA_TRANSFER_MODE
;
898 slot
->card
.open_ct
= 0;
899 memset(slot
->card
.part
, 0, sizeof(slot
->card
.part
));
900 memset(slot
->card
.subpart
, 0, sizeof(slot
->card
.subpart
));
901 slot
->card
.part
[0].dv_size
= card_size
;
903 return &(slot
->card
);
907 * Interface to the MINIX block device driver.
911 emmc_card_release(struct sd_card
*card
)
913 /* Decrements the "in-use count." */
917 * The block special file is closed, but the driver does not need to
918 * "release" the eMMC, even if the driver is unloaded.
925 * Interface to the MINIX block device driver.
926 * Handle unexpected interrupts.
929 emmc_hw_intr(unsigned int irqs
)
931 log_warn(&log
, "register SD_STAT == 0x%08x\n", reg
->SD_STAT
);
935 * Interface to the MINIX block device driver.
937 * Return the number of blocks read/written, or a negative integer on error.
940 emmc_read_write(int (*cim_read_write
)(uint32_t, uint32_t *),
941 uint32_t blknr
, uint32_t count
, unsigned char *buf
)
946 blocks
= 0; /* count of blocks read/written. */
948 while ((count
> 0) && (r
== 0)) {
950 * Data address for media =< 2GB is byte address, and data
951 * address for media > 2GB is sector address.
953 if (card_size
<= (2U << 30))
954 addr
= blknr
* SEC_SIZE
;
958 r
= (*cim_read_write
)(addr
, (uint32_t *)buf
);
965 else if (blocks
== 0)
973 * Interface to the MINIX block device driver.
977 emmc_read(struct sd_card
*card
,
978 uint32_t blknr
, uint32_t count
, unsigned char *buf
)
980 return emmc_read_write(&cim_read_block
, blknr
, count
, buf
);
984 * Interface to the MINIX block device driver.
988 emmc_write(struct sd_card
*card
,
989 uint32_t blknr
, uint32_t count
, unsigned char *buf
)
991 if (card_write_protect
)
992 return -1; /* The card is write protected. */
993 return emmc_read_write(&cim_write_block
, blknr
, count
, buf
);
997 * Interface to the MINIX block device driver.
998 * Driver interface registration.
1001 host_initialize_host_structure_mmchs(struct mmc_host
*host
)
1005 /* Register the driver interface at the block device driver. */
1006 host
->host_set_instance
= &emmc_host_set_instance
;
1007 host
->host_init
= &emmc_host_init
;
1008 host
->set_log_level
= &emmc_set_log_level
;
1009 host
->host_reset
= NULL
;
1010 host
->card_detect
= &emmc_card_detect
;
1011 host
->card_initialize
= &emmc_card_initialize
;
1012 host
->card_release
= &emmc_card_release
;
1013 host
->hw_intr
= &emmc_hw_intr
;
1014 host
->read
= &emmc_read
;
1015 host
->write
= &emmc_write
;
1016 for (i
=0; i
<MAX_SD_SLOTS
; i
++) {
1017 host
->slot
[i
].host
= host
;
1018 host
->slot
[i
].card
.state
= SD_MODE_UNINITIALIZED
;
1019 host
->slot
[i
].card
.slot
= &host
->slot
[i
];
1024 * Interface to the MINIX block device driver.
1025 * Unused, but declared in mmchost.h.
1028 host_initialize_host_structure_dummy(struct mmc_host
*host
)