2 #include <minix/blockdriver.h>
5 #include <minix/spin.h>
7 #include <minix/mmio.h>
8 #include <minix/type.h>
9 #include <minix/board.h>
26 /* header imported from netbsd */
31 /* omap /hardware related */
37 static int hook_id
= 1;
42 #define SANE_TIMEOUT 500000 /* 500 ms */
44 struct omap_mmchs
*mmchs
; /* pointer to the current mmchs */
46 struct omap_mmchs bone_sdcard
= {
49 .hw_base
= 0x48060000,
50 .irq_nr
= 64, /* MMC/SD module 1 */
54 struct omap_mmchs bbxm_sdcard
= {
57 .hw_base
= 0x4809C000,
58 .irq_nr
= 83, /* MMC/SD module 1 */
62 /* Integer divide x by y and ensure that the result z is
63 * such that x / z is smaller or equal y
65 #define div_roundup(x, y) (((x)+((y)-1))/(y))
68 * Define a structure to be used for logging
70 static struct log log
= {
71 .name
= "mmc_host_mmchs",
72 .log_level
= LEVEL_INFO
,
73 .log_func
= default_log
76 #define HSMMCSD_0_IN_FREQ 96000000 /* 96MHz */
77 #define HSMMCSD_0_INIT_FREQ 400000 /* 400kHz */
78 #define HSMMCSD_0_FREQ_25MHZ 25000000 /* 25MHz */
79 #define HSMMCSD_0_FREQ_50MHZ 50000000 /* 50MHz */
82 mmc_set32(vir_bytes reg
, u32_t mask
, u32_t value
)
84 assert(reg
>= 0 && reg
<= mmchs
->io_size
);
85 set32(mmchs
->io_base
+ reg
, mask
, value
);
89 mmc_read32(vir_bytes reg
)
91 assert(reg
>= 0 && reg
<= mmchs
->io_size
);
92 return read32(mmchs
->io_base
+ reg
);
96 mmc_write32(vir_bytes reg
, u32_t value
)
98 assert(reg
>= 0 && reg
<= mmchs
->io_size
);
99 write32(mmchs
->io_base
+ reg
, value
);
103 mmchs_set_bus_freq(u32_t freq
)
105 u32_t freq_in
= HSMMCSD_0_IN_FREQ
;
106 u32_t freq_out
= freq
;
108 /* Calculate and program the divisor */
109 u32_t clkd
= div_roundup(freq_in
, freq_out
);
110 clkd
= (clkd
< 2) ? 2 : clkd
;
111 clkd
= (clkd
> 1023) ? 1023 : clkd
;
113 log_debug(&log
, "Setting divider to %d\n", clkd
);
114 mmc_set32(mmchs
->regs
->SYSCTL
, MMCHS_SD_SYSCTL_CLKD
, (clkd
<< 6));
118 * Initialize the MMC controller given a certain
119 * instance. this driver only handles a single
120 * mmchs controller at a given time.
123 mmchs_init(uint32_t instance
)
128 struct minix_mem_range mr
;
132 mr
.mr_base
= mmchs
->hw_base
;
133 mr
.mr_limit
= mmchs
->hw_base
+ mmchs
->io_size
;
135 /* grant ourself rights to map the register memory */
136 if (sys_privctl(SELF
, SYS_PRIV_ADD_MEM
, &mr
) != 0) {
137 panic("Unable to request permission to map memory");
140 /* Set the base address to use */
142 (uint32_t) vm_map_phys(SELF
, (void *) mmchs
->hw_base
,
145 if (mmchs
->io_base
== (uint32_t) MAP_FAILED
)
146 panic("Unable to map MMC memory");
148 /* Soft reset of the controller. This section is documented in the TRM
151 /* Write 1 to sysconfig[0] to trigger a reset */
152 mmc_set32(mmchs
->regs
->SYSCONFIG
, MMCHS_SD_SYSCONFIG_SOFTRESET
,
153 MMCHS_SD_SYSCONFIG_SOFTRESET
);
155 /* Read sysstatus to know when it's done */
157 spin_init(&spin
, SANE_TIMEOUT
);
158 while (!(mmc_read32(mmchs
->regs
->SYSSTATUS
)
159 & MMCHS_SD_SYSSTATUS_RESETDONE
)) {
160 if (spin_check(&spin
) == FALSE
) {
161 log_warn(&log
, "mmc init timeout\n");
166 /* Set SD default capabilities */
167 mmc_set32(mmchs
->regs
->CAPA
, MMCHS_SD_CAPA_VS_MASK
,
168 MMCHS_SD_CAPA_VS18
| MMCHS_SD_CAPA_VS30
);
170 /* TRM mentions MMCHS_SD_CUR_CAPA but does not describe how to limit
174 MMCHS_SD_SYSCONFIG_AUTOIDLE
| MMCHS_SD_SYSCONFIG_ENAWAKEUP
|
175 MMCHS_SD_SYSCONFIG_STANDBYMODE
| MMCHS_SD_SYSCONFIG_CLOCKACTIVITY
|
176 MMCHS_SD_SYSCONFIG_SIDLEMODE
;
178 /* Automatic clock gating strategy */
179 value
= MMCHS_SD_SYSCONFIG_AUTOIDLE_EN
;
180 /* Enable wake-up capability */
181 value
|= MMCHS_SD_SYSCONFIG_ENAWAKEUP_EN
;
183 value
|= MMCHS_SD_SYSCONFIG_SIDLEMODE_IDLE
;
184 /* Both the interface and functional can be switched off */
185 value
|= MMCHS_SD_SYSCONFIG_CLOCKACTIVITY_OFF
;
186 /* Go into wake-up mode when possible */
187 value
|= MMCHS_SD_SYSCONFIG_STANDBYMODE_WAKEUP_INTERNAL
;
190 * wake-up configuration
192 mmc_set32(mmchs
->regs
->SYSCONFIG
, mask
, value
);
194 /* Wake-up on sd interrupt for SDIO */
195 mmc_set32(mmchs
->regs
->HCTL
, MMCHS_SD_HCTL_IWE
, MMCHS_SD_HCTL_IWE_EN
);
198 * MMC host and bus configuration
201 /* Configure data and command transfer (1 bit mode) switching to
202 * higher bit modes happens after a card is detected */
203 mmc_set32(mmchs
->regs
->CON
, MMCHS_SD_CON_DW8
, MMCHS_SD_CON_DW8_1BIT
);
204 mmc_set32(mmchs
->regs
->HCTL
, MMCHS_SD_HCTL_DTW
,
205 MMCHS_SD_HCTL_DTW_1BIT
);
207 /* Configure card voltage to 3.0 volt */
208 mmc_set32(mmchs
->regs
->HCTL
, MMCHS_SD_HCTL_SDVS
,
209 MMCHS_SD_HCTL_SDVS_VS30
);
211 /* Power on the host controller and wait for the
212 * MMCHS_SD_HCTL_SDBP_POWER_ON to be set */
213 mmc_set32(mmchs
->regs
->HCTL
, MMCHS_SD_HCTL_SDBP
,
214 MMCHS_SD_HCTL_SDBP_ON
);
216 spin_init(&spin
, SANE_TIMEOUT
);
217 while ((mmc_read32(mmchs
->regs
->HCTL
) & MMCHS_SD_HCTL_SDBP
)
218 != MMCHS_SD_HCTL_SDBP_ON
) {
219 if (spin_check(&spin
) == FALSE
) {
220 log_warn(&log
, "mmc init timeout SDBP not set\n");
225 /* Enable internal clock and clock to the card */
226 mmc_set32(mmchs
->regs
->SYSCTL
, MMCHS_SD_SYSCTL_ICE
,
227 MMCHS_SD_SYSCTL_ICE_EN
);
229 mmchs_set_bus_freq(HSMMCSD_0_INIT_FREQ
);
231 mmc_set32(mmchs
->regs
->SYSCTL
, MMCHS_SD_SYSCTL_CEN
,
232 MMCHS_SD_SYSCTL_CEN_EN
);
234 spin_init(&spin
, SANE_TIMEOUT
);
235 while ((mmc_read32(mmchs
->regs
->SYSCTL
) & MMCHS_SD_SYSCTL_ICS
)
236 != MMCHS_SD_SYSCTL_ICS_STABLE
) {
237 if (spin_check(&spin
) == FALSE
) {
238 log_warn(&log
, "clock not stable\n");
244 * See spruh73e page 3576 Card Detection, Identification, and Selection
247 /* Enable command interrupt */
248 mmc_set32(mmchs
->regs
->IE
, MMCHS_SD_IE_CC_ENABLE
,
249 MMCHS_SD_IE_CC_ENABLE_ENABLE
);
250 /* Enable transfer complete interrupt */
251 mmc_set32(mmchs
->regs
->IE
, MMCHS_SD_IE_TC_ENABLE
,
252 MMCHS_SD_IE_TC_ENABLE_ENABLE
);
254 /* enable error interrupts */
255 mmc_set32(mmchs
->regs
->IE
, MMCHS_SD_IE_ERROR_MASK
, 0xffffffffu
);
257 /* clear the error interrupts */
258 mmc_set32(mmchs
->regs
->SD_STAT
, MMCHS_SD_STAT_ERROR_MASK
, 0xffffffffu
);
260 /* send a init signal to the host controller. This does not actually
261 * send a command to a card manner */
262 mmc_set32(mmchs
->regs
->CON
, MMCHS_SD_CON_INIT
, MMCHS_SD_CON_INIT_INIT
);
263 /* command 0 , type other commands not response etc) */
264 mmc_write32(mmchs
->regs
->CMD
, 0x00);
266 spin_init(&spin
, SANE_TIMEOUT
);
267 while ((mmc_read32(mmchs
->regs
->SD_STAT
) & MMCHS_SD_STAT_CC
)
268 != MMCHS_SD_STAT_CC_RAISED
) {
269 if (mmc_read32(mmchs
->regs
->SD_STAT
) & 0x8000) {
270 log_warn(&log
, "%s, error stat %x\n",
271 __FUNCTION__
, mmc_read32(mmchs
->regs
->SD_STAT
));
275 if (spin_check(&spin
) == FALSE
) {
276 log_warn(&log
, "Interrupt not raised during init\n");
281 /* clear the cc interrupt status */
282 mmc_set32(mmchs
->regs
->SD_STAT
, MMCHS_SD_IE_CC_ENABLE
,
283 MMCHS_SD_IE_CC_ENABLE_ENABLE
);
286 * Set Set SD_CON[1] INIT bit to 0x0 to end the initialization sequence
288 mmc_set32(mmchs
->regs
->CON
, MMCHS_SD_CON_INIT
,
289 MMCHS_SD_CON_INIT_NOINIT
);
292 mmc_set32(mmchs
->regs
->SYSCTL
, MMCHS_SD_SYSCTL_DTO
,
293 MMCHS_SD_SYSCTL_DTO_2POW27
);
295 /* Clean the MMCHS_SD_STAT register */
296 mmc_write32(mmchs
->regs
->SD_STAT
, 0xffffffffu
);
299 if (sys_irqsetpolicy(mmchs
->irq_nr
, 0, &hook_id
) != OK
) {
300 log_warn(&log
, "mmc: couldn't set IRQ policy %d\n",
304 /* enable signaling from MMC controller towards interrupt controller */
305 mmc_write32(mmchs
->regs
->ISE
, 0xffffffffu
);
312 intr_deassert(int mask
)
314 if (mmc_read32(mmchs
->regs
->SD_STAT
) & 0x8000) {
315 log_warn(&log
, "%s, error stat %08x\n", __FUNCTION__
,
316 mmc_read32(mmchs
->regs
->SD_STAT
));
317 mmc_set32(mmchs
->regs
->SD_STAT
, MMCHS_SD_STAT_ERROR_MASK
,
320 mmc_write32(mmchs
->regs
->SD_STAT
, mask
);
324 /* pointer to the data to transfer used in bwr and brr */
325 unsigned char *io_data
;
331 /* handle buffer write ready interrupts. These happen in a non
332 * predictable way (eg. we send a request but don't know if we are
333 * first doing to get a request completed before we are allowed to
334 * send the data to the hardware or not */
337 assert(mmc_read32(mmchs
->regs
->PSTATE
) & MMCHS_SD_PSTATE_BWE_EN
);
338 assert(io_data
!= NULL
);
340 for (count
= 0; count
< io_len
; count
+= 4) {
341 while (!(mmc_read32(mmchs
->regs
->
342 PSTATE
) & MMCHS_SD_PSTATE_BWE_EN
)) {
344 "Error expected Buffer to be write enabled(%d)\n",
347 *((char *) &value
) = io_data
[count
];
348 *((char *) &value
+ 1) = io_data
[count
+ 1];
349 *((char *) &value
+ 2) = io_data
[count
+ 2];
350 *((char *) &value
+ 3) = io_data
[count
+ 3];
351 mmc_write32(mmchs
->regs
->DATA
, value
);
353 intr_deassert(MMCHS_SD_IE_BWR_ENABLE
);
354 /* expect buffer to be write enabled */
361 /* handle buffer read ready interrupts. genrally these happen afther
362 * the data is read from the sd card. */
367 /* Problem BRE should be true */
368 assert(mmc_read32(mmchs
->regs
->PSTATE
) & MMCHS_SD_PSTATE_BRE_EN
);
370 assert(io_data
!= NULL
);
372 for (count
= 0; count
< io_len
; count
+= 4) {
373 value
= mmc_read32(mmchs
->regs
->DATA
);
374 io_data
[count
] = *((char *) &value
);
375 io_data
[count
+ 1] = *((char *) &value
+ 1);
376 io_data
[count
+ 2] = *((char *) &value
+ 2);
377 io_data
[count
+ 3] = *((char *) &value
+ 3);
379 /* clear bbr interrupt */
380 intr_deassert(MMCHS_SD_IE_BRR_ENABLE_ENABLE
);
385 mmchs_hw_intr(unsigned int irqs
)
387 log_warn(&log
, "Hardware interrupt left over (0x%08lx)\n",
388 mmc_read32(mmchs
->regs
->SD_STAT
));
391 if (sys_irqenable(&hook_id
) != OK
)
392 printf("couldn't re-enable interrupt \n");
394 /* Leftover interrupt(s) received; ack it/them. */
397 /*===========================================================================*
399 *===========================================================================*/
405 if (sys_irqenable(&hook_id
) != OK
)
406 printf("Failed to enable irqenable irq\n");
407 /* Wait for a task completion interrupt. */
410 int ticks
= SANE_TIMEOUT
* sys_hz() / 1000000;
416 sys_setalarm(ticks
, 0);
417 if ((rr
= driver_receive(ANY
, &m
, &ipc_status
)) != OK
) {
418 panic("driver_receive failed: %d", rr
);
420 if (is_ipc_notify(ipc_status
)) {
421 switch (_ENDPOINT_P(m
.m_source
)) {
424 log_warn(&log
, "TIMEOUT\n");
429 mmc_read32(mmchs
->regs
->SD_STAT
)) !=
431 if (v
& MMCHS_SD_IE_BWR_ENABLE
) {
435 if (v
& MMCHS_SD_IE_BRR_ENABLE
) {
441 /* this is the normal return
442 * path, the mask given
443 * matches the pending
444 * interrupt. cancel the alarm
448 } else if (v
& (1 << 15)) {
449 return 1; /* error */
453 "unexpected HW interrupt 0x%08x mask 0X%08x\n",
455 if (sys_irqenable(&hook_id
) != OK
)
457 ("Failed to re-enable irqenable irq\n");
459 /* if we end up here re-enable interrupts for
461 if (sys_irqenable(&hook_id
) != OK
)
463 ("Failed to re-enable irqenable irq\n");
467 * unhandled notify message. Queue it and
468 * handle it in the blockdriver loop.
470 blockdriver_mq_queue(&m
, ipc_status
);
474 * unhandled message. Queue it and handle it in the
477 blockdriver_mq_queue(&m
, ipc_status
);
480 sys_setalarm(0, 0); /* cancel the alarm */
484 spin_init(&spin
, SANE_TIMEOUT
);
485 /* Wait for completion */
489 v
= mmc_read32(mmchs
->regs
->SD_STAT
);
490 if (spin_check(&spin
) == FALSE
) {
492 "Timeout waiting for interrupt (%d) value 0x%08x mask 0x%08x\n",
496 if (v
& MMCHS_SD_IE_BWR_ENABLE
) {
500 if (v
& MMCHS_SD_IE_BRR_ENABLE
) {
506 } else if (v
& 0xFF00) {
508 "unexpected HW interrupt (%d) 0x%08x mask 0x%08x\n",
513 return 1; /* unreached */
514 #endif /* USE_INTR */
518 mmchs_send_cmd(uint32_t command
, uint32_t arg
)
521 /* Read current interrupt status and fail it an interrupt is already
523 assert(mmc_read32(mmchs
->regs
->SD_STAT
) == 0);
526 mmc_write32(mmchs
->regs
->ARG
, arg
);
528 mmc_set32(mmchs
->regs
->CMD
, MMCHS_SD_CMD_MASK
, command
);
530 if (intr_wait(MMCHS_SD_STAT_CC
)) {
531 uint32_t v
= mmc_read32(mmchs
->regs
->SD_STAT
);
532 intr_deassert(MMCHS_SD_STAT_CC
);
533 log_warn(&log
, "Failure waiting for interrupt 0x%lx\n", v
);
536 intr_deassert(MMCHS_SD_STAT_CC
);
538 if ((command
& MMCHS_SD_CMD_RSP_TYPE
) ==
539 MMCHS_SD_CMD_RSP_TYPE_48B_BUSY
) {
541 * Command with busy response *CAN* also set the TC bit if they exit busy
543 if ((mmc_read32(mmchs
->regs
->SD_STAT
)
544 & MMCHS_SD_IE_TC_ENABLE_ENABLE
) == 0) {
545 log_warn(&log
, "TC should be raised\n");
547 intr_deassert(MMCHS_SD_STAT_TC
);
553 mmc_send_cmd(struct mmc_command
*c
)
556 /* convert the command to a hsmmc command */
559 cmd
= MMCHS_SD_CMD_INDX_CMD(c
->cmd
);
561 assert(c
->data_type
== DATA_NONE
|| c
->data_type
== DATA_READ
562 || c
->data_type
== DATA_WRITE
);
564 switch (c
->resp_type
) {
565 case RESP_LEN_48_CHK_BUSY
:
566 cmd
|= MMCHS_SD_CMD_RSP_TYPE_48B_BUSY
;
569 cmd
|= MMCHS_SD_CMD_RSP_TYPE_48B
;
572 cmd
|= MMCHS_SD_CMD_RSP_TYPE_136B
;
574 case RESP_NO_RESPONSE
:
575 cmd
|= MMCHS_SD_CMD_RSP_TYPE_NO_RESP
;
581 /* read single block */
582 if (c
->data_type
== DATA_READ
) {
583 cmd
|= MMCHS_SD_CMD_DP_DATA
; /* Command with data transfer */
584 cmd
|= MMCHS_SD_CMD_MSBS_SINGLE
; /* single block */
585 cmd
|= MMCHS_SD_CMD_DDIR_READ
; /* read data from card */
588 /* write single block */
589 if (c
->data_type
== DATA_WRITE
) {
590 cmd
|= MMCHS_SD_CMD_DP_DATA
; /* Command with data transfer */
591 cmd
|= MMCHS_SD_CMD_MSBS_SINGLE
; /* single block */
592 cmd
|= MMCHS_SD_CMD_DDIR_WRITE
; /* write to the card */
595 /* check we are in a sane state */
596 if ((mmc_read32(mmchs
->regs
->SD_STAT
) & 0xffffu
)) {
597 log_warn(&log
, "%s, interrupt already raised stat %08x\n",
598 __FUNCTION__
, mmc_read32(mmchs
->regs
->SD_STAT
));
599 mmc_write32(mmchs
->regs
->SD_STAT
, MMCHS_SD_IE_CC_ENABLE_CLEAR
);
602 if (cmd
& MMCHS_SD_CMD_DP_DATA
) {
603 if (cmd
& MMCHS_SD_CMD_DDIR_READ
) {
604 /* if we are going to read enable the buffer ready
606 mmc_set32(mmchs
->regs
->IE
,
607 MMCHS_SD_IE_BRR_ENABLE
,
608 MMCHS_SD_IE_BRR_ENABLE_ENABLE
);
610 mmc_set32(mmchs
->regs
->IE
,
611 MMCHS_SD_IE_BWR_ENABLE
,
612 MMCHS_SD_IE_BWR_ENABLE_ENABLE
);
615 io_len
= c
->data_len
;
616 assert(io_len
<= 0xFFF); /* only 12 bits */
617 assert(io_data
!= NULL
);
618 mmc_set32(mmchs
->regs
->BLK
, MMCHS_SD_BLK_BLEN
, io_len
);
621 ret
= mmchs_send_cmd(cmd
, arg
);
623 if (cmd
& MMCHS_SD_CMD_DP_DATA
) {
625 if (cmd
& MMCHS_SD_CMD_DDIR_READ
) {
627 if (intr_wait(MMCHS_SD_IE_TC_ENABLE_ENABLE
)) {
628 intr_deassert(MMCHS_SD_IE_TC_ENABLE_ENABLE
);
630 "(Read) Timeout waiting for interrupt\n");
634 mmc_write32(mmchs
->regs
->SD_STAT
,
635 MMCHS_SD_IE_TC_ENABLE_CLEAR
);
637 /* disable the bbr interrupt */
638 mmc_set32(mmchs
->regs
->IE
,
639 MMCHS_SD_IE_BRR_ENABLE
,
640 MMCHS_SD_IE_BRR_ENABLE_DISABLE
);
643 if (intr_wait(MMCHS_SD_IE_TC_ENABLE_ENABLE
)) {
644 intr_deassert(MMCHS_SD_IE_TC_ENABLE_CLEAR
);
646 "(Write) Timeout waiting for transfer complete\n");
649 intr_deassert(MMCHS_SD_IE_TC_ENABLE_CLEAR
);
651 mmc_set32(mmchs
->regs
->IE
,
652 MMCHS_SD_IE_BWR_ENABLE
,
653 MMCHS_SD_IE_BWR_ENABLE_DISABLE
);
658 /* copy response into cmd->resp */
659 switch (c
->resp_type
) {
660 case RESP_LEN_48_CHK_BUSY
:
662 c
->resp
[0] = mmc_read32(mmchs
->regs
->RSP10
);
665 c
->resp
[0] = mmc_read32(mmchs
->regs
->RSP10
);
666 c
->resp
[1] = mmc_read32(mmchs
->regs
->RSP32
);
667 c
->resp
[2] = mmc_read32(mmchs
->regs
->RSP54
);
668 c
->resp
[3] = mmc_read32(mmchs
->regs
->RSP76
);
670 case RESP_NO_RESPONSE
:
680 mmc_send_app_cmd(struct sd_card_regs
*card
, struct mmc_command
*c
)
682 struct mmc_command command
;
683 command
.cmd
= MMC_APP_CMD
;
684 command
.resp_type
= RESP_LEN_48
;
685 command
.data_type
= DATA_NONE
;
686 command
.args
= MMC_ARG_RCA(card
->rca
);
687 if (mmc_send_cmd(&command
)) {
690 return mmc_send_cmd(c
);
694 card_goto_idle_state()
696 struct mmc_command command
;
697 command
.cmd
= MMC_GO_IDLE_STATE
;
698 command
.resp_type
= RESP_NO_RESPONSE
;
699 command
.data_type
= DATA_NONE
;
701 if (mmc_send_cmd(&command
)) {
709 card_identification()
711 struct mmc_command command
;
712 command
.cmd
= SD_SEND_IF_COND
; /* Send CMD8 */
713 command
.resp_type
= RESP_LEN_48
;
714 command
.data_type
= DATA_NONE
;
715 command
.args
= MMCHS_SD_ARG_CMD8_VHS
| MMCHS_SD_ARG_CMD8_CHECK_PATTERN
;
717 if (mmc_send_cmd(&command
)) {
718 /* We currently only support 2.0, and 1.0 won't respond to
720 log_warn(&log
, "%s, non SDHC card inserted\n", __FUNCTION__
);
724 if (!(command
.resp
[0]
725 == (MMCHS_SD_ARG_CMD8_VHS
| MMCHS_SD_ARG_CMD8_CHECK_PATTERN
))) {
726 log_warn(&log
, "%s, check pattern check failed %08x\n",
727 __FUNCTION__
, command
.resp
[0]);
734 card_query_voltage_and_type(struct sd_card_regs
*card
)
736 struct mmc_command command
;
739 command
.cmd
= SD_APP_OP_COND
;
740 command
.resp_type
= RESP_LEN_48
;
741 command
.data_type
= DATA_NONE
;
743 /* 0x1 << 30 == send HCS (Host capacity support) and get OCR register */
745 MMC_OCR_3_3V_3_4V
| MMC_OCR_3_2V_3_3V
| MMC_OCR_3_1V_3_2V
|
746 MMC_OCR_3_0V_3_1V
| MMC_OCR_2_9V_3_0V
| MMC_OCR_2_8V_2_9V
|
748 command
.args
|= MMC_OCR_HCS
; /* RCA=0000 */
750 if (mmc_send_app_cmd(card
, &command
)) {
754 spin_init(&spin
, SANE_TIMEOUT
);
755 while (!(command
.resp
[0] & MMC_OCR_MEM_READY
)) {
758 /* 0x1 << 30 == send HCS (Host capacity support) and get OCR
760 command
.cmd
= SD_APP_OP_COND
;
761 command
.resp_type
= RESP_LEN_48
;
762 command
.data_type
= DATA_NONE
;
763 /* 0x1 << 30 == send HCS (Host capacity support) */
764 command
.args
= MMC_OCR_3_3V_3_4V
| MMC_OCR_3_2V_3_3V
765 | MMC_OCR_3_1V_3_2V
| MMC_OCR_3_0V_3_1V
| MMC_OCR_2_9V_3_0V
766 | MMC_OCR_2_8V_2_9V
| MMC_OCR_2_7V_2_8V
;
767 command
.args
|= MMC_OCR_HCS
; /* RCA=0000 */
769 if (mmc_send_app_cmd(card
, &command
)) {
773 /* if bit 31 is set the response is valid */
774 if ((command
.resp
[0] & MMC_OCR_MEM_READY
)) {
777 if (spin_check(&spin
) == FALSE
) {
778 log_warn(&log
, "TIMEOUT waiting for the SD card\n");
782 card
->ocr
= command
.resp
[3];
787 card_identify(struct sd_card_regs
*card
)
789 struct mmc_command command
;
790 /* Send cmd 2 (all_send_cid) and expect 136 bits response */
791 command
.cmd
= MMC_ALL_SEND_CID
;
792 command
.resp_type
= RESP_LEN_136
;
793 command
.data_type
= DATA_NONE
;
794 command
.args
= MMC_ARG_RCA(0x0); /* RCA=0000 */
796 if (mmc_send_cmd(&command
)) {
800 card
->cid
[0] = command
.resp
[0];
801 card
->cid
[1] = command
.resp
[1];
802 card
->cid
[2] = command
.resp
[2];
803 card
->cid
[3] = command
.resp
[3];
805 command
.cmd
= MMC_SET_RELATIVE_ADDR
;
806 command
.resp_type
= RESP_LEN_48
;
807 command
.data_type
= DATA_NONE
;
808 command
.args
= 0x0; /* RCA=0000 */
811 if (mmc_send_cmd(&command
)) {
815 card
->rca
= SD_R6_RCA(command
.resp
);
816 /* MMHCS only supports a single card so sending MMCHS_SD_CMD_CMD2 is
817 * useless Still we should make it possible in the API to support
824 card_csd(struct sd_card_regs
*card
)
826 /* Read the Card Specific Data register */
827 struct mmc_command command
;
829 /* send_csd -> r2 response */
830 command
.cmd
= MMC_SEND_CSD
;
831 command
.resp_type
= RESP_LEN_136
;
832 command
.data_type
= DATA_NONE
;
833 command
.args
= MMC_ARG_RCA(card
->rca
); /* card rca */
835 if (mmc_send_cmd(&command
)) {
839 card
->csd
[0] = command
.resp
[0];
840 card
->csd
[1] = command
.resp
[1];
841 card
->csd
[2] = command
.resp
[2];
842 card
->csd
[3] = command
.resp
[3];
844 log_trace(&log
, "CSD version %d\n", SD_CSD_CSDVER(card
->csd
));
845 if (SD_CSD_CSDVER(card
->csd
) != SD_CSD_CSDVER_2_0
) {
846 log_warn(&log
, "Version 2.0 of CSD register expected\n");
854 select_card(struct sd_card_regs
*card
)
856 struct mmc_command command
;
858 command
.cmd
= MMC_SELECT_CARD
;
859 command
.resp_type
= RESP_LEN_48_CHK_BUSY
;
860 command
.data_type
= DATA_NONE
;
861 command
.args
= MMC_ARG_RCA(card
->rca
); /* card rca */
863 if (mmc_send_cmd(&command
)) {
870 card_scr(struct sd_card_regs
*card
)
872 uint8_t buffer
[8]; /* 64 bits */
875 /* the SD CARD configuration register. This is an additional register
876 * next to the Card Specific register containing additional data we
878 struct mmc_command command
;
880 log_trace(&log
, "Read card scr\n");
881 /* send_csd -> r2 response */
882 command
.cmd
= SD_APP_SEND_SCR
;
883 command
.resp_type
= RESP_LEN_48
;
884 command
.data_type
= DATA_READ
;
885 command
.args
= 0xaaaaaaaa;
886 command
.data
= buffer
;
887 command
.data_len
= 8;
889 if (mmc_send_app_cmd(card
, &command
)) {
893 p
= (uint8_t *) card
->scr
;
895 /* copy the data to card->scr */
896 for (c
= 7; c
>= 0; c
--) {
900 if (!SCR_SD_BUS_WIDTHS(card
->scr
) & SCR_SD_BUS_WIDTHS_4BIT
) {
901 /* it would be very weird not to support 4 bits access */
902 log_warn(&log
, "4 bit access not supported\n");
905 log_trace(&log
, "1 bit bus width %ssupported\n",
906 (SCR_SD_BUS_WIDTHS(card
->scr
) & SCR_SD_BUS_WIDTHS_1BIT
) ? "" :
908 log_trace(&log
, "4 bit bus width %ssupported\n",
909 (SCR_SD_BUS_WIDTHS(card
->scr
) & SCR_SD_BUS_WIDTHS_4BIT
) ? "" :
916 enable_4bit_mode(struct sd_card_regs
*card
)
918 struct mmc_command command
;
920 if (SCR_SD_BUS_WIDTHS(card
->scr
) & SCR_SD_BUS_WIDTHS_4BIT
) {
921 /* set transfer width */
922 command
.cmd
= SD_APP_SET_BUS_WIDTH
;
923 command
.resp_type
= RESP_LEN_48
;
924 command
.data_type
= DATA_NONE
;
925 command
.args
= 2; /* 4 bits */
927 if (mmc_send_app_cmd(card
, &command
)) {
929 "SD-card does not support 4 bit transfer\n");
932 /* now configure the controller to use 4 bit access */
933 mmc_set32(mmchs
->regs
->HCTL
, MMCHS_SD_HCTL_DTW
,
934 MMCHS_SD_HCTL_DTW_4BIT
);
937 return 1; /* expect 4 bits mode to work so having a card
938 * that doesn't support 4 bits mode */
942 dump_char(char *out
, char in
)
946 for (i
= 0; i
< 8; i
++) {
947 out
[i
] = ((in
>> i
) & 0x1) ? '1' : '0';
953 dump(uint8_t * data
, int len
)
959 for (c
= 0; c
< len
;) {
960 memset(digit
, 0, sizeof(digit
));
962 dump_char(digit
[0], *data
++);
964 dump_char(digit
[1], *data
++);
966 dump_char(digit
[2], *data
++);
968 dump_char(digit
[3], *data
++);
969 printf("%x %s %s %s %s\n", c
, digit
[0], digit
[1], digit
[2],
975 mmc_switch(int function
, int value
, uint8_t * data
)
977 struct mmc_command command
;
981 findex
= function
- 1;
982 fshift
= findex
<< 2; /* bits used per function */
984 command
.cmd
= MMC_SWITCH
;
985 command
.resp_type
= RESP_LEN_48
;
986 command
.data_type
= DATA_READ
;
988 command
.data_len
= 64;
989 command
.args
= (1 << 31) | (0x00ffffff & ~(0xf << fshift
));
990 command
.args
|= (value
<< fshift
);
991 if (mmc_send_cmd(&command
)) {
992 log_warn(&log
, "Failed to set device in high speed mode\n");
999 enable_high_speed_mode(struct sd_card_regs
*card
)
1001 /* MMC cards using version 4.0 or higher of the specs can work at
1002 * higher bus rates. After setting the bus width one can send the
1003 * HS_TIMING command to set the card in high speed mode after witch
1004 * one can higher up the frequency */
1006 uint8_t buffer
[64]; /* 512 bits */
1007 log_info(&log
, "Enabling high speed mode\n");
1009 Doesnt currently work
1010 if (SCR_SD_SPEC(&card
->scr
[0]) >= SCR_SD_SPEC_VER_1_10
)
1012 mmc_switch(1, 1, buffer
);
1016 if (SD_CSD_SPEED(card
->csd
) == SD_CSD_SPEED_25_MHZ
) {
1017 log_trace(&log
, "Using 25MHz clock\n");
1018 mmchs_set_bus_freq(HSMMCSD_0_FREQ_25MHZ
);
1019 } else if (SD_CSD_SPEED(card
->csd
) == SD_CSD_SPEED_50_MHZ
) {
1020 log_trace(&log
, "Using 50MHz clock\n");
1021 mmchs_set_bus_freq(HSMMCSD_0_FREQ_50MHZ
);
1023 log_warn(&log
, "Unknown speed 0x%x in CSD register\n",
1024 SD_CSD_SPEED(card
->csd
));
1031 read_single_block(struct sd_card_regs
*card
,
1032 uint32_t blknr
, unsigned char *buf
)
1034 struct mmc_command command
;
1036 command
.cmd
= MMC_READ_BLOCK_SINGLE
;
1037 command
.args
= blknr
;
1038 command
.resp_type
= RESP_LEN_48
;
1039 command
.data_type
= DATA_READ
;
1041 command
.data_len
= 512;
1043 if (mmc_send_cmd(&command
)) {
1044 log_warn(&log
, "Error sending command\n");
1052 write_single_block(struct sd_card_regs
*card
,
1053 uint32_t blknr
, unsigned char *buf
)
1055 struct mmc_command command
;
1057 command
.cmd
= MMC_WRITE_BLOCK_SINGLE
;
1058 command
.args
= blknr
;
1059 command
.resp_type
= RESP_LEN_48
;
1060 command
.data_type
= DATA_WRITE
;
1062 command
.data_len
= 512;
1064 /* write single block */
1065 if (mmc_send_cmd(&command
)) {
1066 log_warn(&log
, "Write single block command failed\n");
1074 mmchs_host_init(struct mmc_host
*host
)
1081 mmchs_set_log_level(int level
)
1083 if (level
>= 0 && level
<= 4) {
1084 log
.log_level
= level
;
1089 mmchs_host_set_instance(struct mmc_host
*host
, int instance
)
1091 log_info(&log
, "Using instance number %d\n", instance
);
1092 if (instance
!= 0) {
1099 mmchs_host_reset(struct mmc_host
*host
)
1106 mmchs_card_detect(struct sd_slot
*slot
)
1108 /* @TODO implement proper card detect */
1113 mmchs_card_initialize(struct sd_slot
*slot
)
1117 struct sd_card
*card
;
1119 memset(card
, 0, sizeof(struct sd_card
));
1122 if (card_goto_idle_state()) {
1123 log_warn(&log
, "Failed to go idle state\n");
1127 if (card_identification()) {
1128 log_warn(&log
, "Failed to do card_identification\n");
1132 if (card_query_voltage_and_type(&slot
->card
.regs
)) {
1133 log_warn(&log
, "Failed to do card_query_voltage_and_type\n");
1137 if (card_identify(&slot
->card
.regs
)) {
1138 log_warn(&log
, "Failed to identify card\n");
1141 /* We have now initialized the hardware identified the card */
1142 if (card_csd(&slot
->card
.regs
)) {
1143 log_warn(&log
, "failed to read csd (card specific data)\n");
1147 if (select_card(&slot
->card
.regs
)) {
1148 log_warn(&log
, "Failed to select card\n");
1152 if (card_scr(&slot
->card
.regs
)) {
1154 "failed to read scr (card additional specific data)\n");
1158 if (enable_4bit_mode(&slot
->card
.regs
)) {
1159 log_warn(&log
, "failed to configure 4 bit access mode\n");
1163 if (enable_high_speed_mode(&slot
->card
.regs
)) {
1164 log_warn(&log
, "failed to configure high speed mode mode\n");
1168 if (SD_CSD_READ_BL_LEN(slot
->card
.regs
.csd
) != 0x09) {
1169 /* for CSD version 2.0 the value is fixed to 0x09 and means a
1170 * block size of 512 */
1171 log_warn(&log
, "Block size expect to be 512\n");
1175 slot
->card
.blk_size
= 512; /* HARDCODED value */
1176 slot
->card
.blk_count
= SD_CSD_V2_CAPACITY(slot
->card
.regs
.csd
);
1177 slot
->card
.state
= SD_MODE_DATA_TRANSFER_MODE
;
1179 /* MINIX related stuff to keep track of partitions */
1180 memset(slot
->card
.part
, 0, sizeof(slot
->card
.part
));
1181 memset(slot
->card
.subpart
, 0, sizeof(slot
->card
.subpart
));
1182 slot
->card
.part
[0].dv_base
= 0;
1183 slot
->card
.part
[0].dv_size
=
1184 (unsigned long long) SD_CSD_V2_CAPACITY(slot
->card
.regs
.csd
) * 512;
1188 /* read count blocks into existing buf */
1190 mmchs_host_read(struct sd_card
*card
,
1191 uint32_t blknr
, uint32_t count
, unsigned char *buf
)
1195 for (i
= 0; i
< count
; i
++) {
1196 read_single_block(&card
->regs
, blknr
+ i
,
1197 buf
+ (i
* card
->blk_size
));
1202 /* write count blocks */
1204 mmchs_host_write(struct sd_card
*card
,
1205 uint32_t blknr
, uint32_t count
, unsigned char *buf
)
1210 for (i
= 0; i
< count
; i
++) {
1211 write_single_block(&card
->regs
, blknr
+ i
,
1212 buf
+ (i
* card
->blk_size
));
1219 mmchs_card_release(struct sd_card
*card
)
1221 assert(card
->open_ct
== 1);
1223 card
->state
= SD_MODE_UNINITIALIZED
;
1224 /* TODO:Set card state */
1226 /* now configure the controller to use 4 bit access */
1227 mmc_set32(mmchs
->regs
->HCTL
, MMCHS_SD_HCTL_DTW
,
1228 MMCHS_SD_HCTL_DTW_1BIT
);
1234 host_initialize_host_structure_mmchs(struct mmc_host
*host
)
1236 /* Initialize the basic data structures host slots and cards */
1240 struct machine machine
;
1241 sys_getmachine(&machine
);
1243 if (BOARD_IS_BBXM(machine
.board_id
)){
1244 mmchs
= &bbxm_sdcard
;
1245 } else if ( BOARD_IS_BB(machine
.board_id
)){
1246 mmchs
= &bone_sdcard
;
1250 host
->host_set_instance
= mmchs_host_set_instance
;
1251 host
->host_init
= mmchs_host_init
;
1252 host
->set_log_level
= mmchs_set_log_level
;
1253 host
->host_reset
= mmchs_host_reset
;
1254 host
->card_detect
= mmchs_card_detect
;
1255 host
->card_initialize
= mmchs_card_initialize
;
1256 host
->card_release
= mmchs_card_release
;
1257 host
->hw_intr
= mmchs_hw_intr
;
1258 host
->read
= mmchs_host_read
;
1259 host
->write
= mmchs_host_write
;
1261 /* initialize data structures */
1262 for (i
= 0; i
< sizeof(host
->slot
) / sizeof(host
->slot
[0]); i
++) {
1263 // @TODO set initial card and slot state
1264 host
->slot
[i
].host
= host
;
1265 host
->slot
[i
].card
.slot
= &host
->slot
[i
];