1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Secure Digital (SD) Host Controller interface specific code
7 #include <commonlib/sd_mmc_ctrlr.h>
8 #include <commonlib/sdhci.h>
9 #include <commonlib/stdlib.h>
10 #include <commonlib/storage.h>
19 #define DMA_AVAILABLE ((CONFIG(SDHCI_ADMA_IN_BOOTBLOCK) && ENV_BOOTBLOCK) \
20 || (CONFIG(SDHCI_ADMA_IN_VERSTAGE) && ENV_SEPARATE_VERSTAGE) \
21 || (CONFIG(SDHCI_ADMA_IN_ROMSTAGE) && ENV_ROMSTAGE) \
22 || ENV_POSTCAR || ENV_RAMSTAGE)
24 __weak
void *dma_malloc(size_t length_in_bytes
)
26 return malloc(length_in_bytes
);
29 void sdhci_reset(struct sdhci_ctrlr
*sdhci_ctrlr
, u8 mask
)
34 stopwatch_init_msecs_expire(&sw
, 100);
36 sdhci_writeb(sdhci_ctrlr
, mask
, SDHCI_SOFTWARE_RESET
);
37 while (sdhci_readb(sdhci_ctrlr
, SDHCI_SOFTWARE_RESET
) & mask
) {
38 if (stopwatch_expired(&sw
)) {
39 sdhc_error("Reset 0x%x never completed.\n", (int)mask
);
46 void sdhci_cmd_done(struct sdhci_ctrlr
*sdhci_ctrlr
, struct mmc_command
*cmd
)
49 if (cmd
->resp_type
& CARD_RSP_136
) {
50 /* CRC is stripped so we need to do some shifting. */
51 for (i
= 0; i
< 4; i
++) {
52 cmd
->response
[i
] = sdhci_readl(sdhci_ctrlr
,
53 SDHCI_RESPONSE
+ (3-i
)*4) << 8;
55 cmd
->response
[i
] |= sdhci_readb(sdhci_ctrlr
,
56 SDHCI_RESPONSE
+ (3-i
)*4-1);
58 sdhc_log_response(4, &cmd
->response
[0]);
59 sdhc_trace("Response: 0x%08x.%08x.%08x.%08x\n",
60 cmd
->response
[3], cmd
->response
[2], cmd
->response
[1],
63 cmd
->response
[0] = sdhci_readl(sdhci_ctrlr
, SDHCI_RESPONSE
);
64 sdhc_log_response(1, &cmd
->response
[0]);
65 sdhc_trace("Response: 0x%08x\n", cmd
->response
[0]);
69 static int sdhci_transfer_data(struct sdhci_ctrlr
*sdhci_ctrlr
,
70 struct mmc_data
*data
, unsigned int start_addr
)
81 buffer
= (uint32_t *)data
->dest
;
82 ps_mask
= (data
->flags
& DATA_FLAG_READ
)
83 ? SDHCI_DATA_AVAILABLE
: SDHCI_SPACE_AVAILABLE
;
84 stopwatch_init_msecs_expire(&sw
, 100);
86 /* Stop transfers if there is an error */
87 stat
= sdhci_readl(sdhci_ctrlr
, SDHCI_INT_STATUS
);
88 sdhci_writel(sdhci_ctrlr
, stat
, SDHCI_INT_STATUS
);
89 if (stat
& SDHCI_INT_ERROR
) {
90 sdhc_error("Error detected in status(0x%X)!\n", stat
);
94 /* Determine if the buffer is ready to move data */
95 ps
= sdhci_readl(sdhci_ctrlr
, SDHCI_PRESENT_STATE
);
96 if (!(ps
& ps_mask
)) {
97 if (stopwatch_expired(&sw
)) {
98 sdhc_error("Transfer data timeout\n");
105 /* Transfer a block of data */
106 buffer_end
= &buffer
[data
->blocksize
>> 2];
107 if (data
->flags
== DATA_FLAG_READ
)
108 while (buffer_end
> buffer
)
109 *buffer
++ = sdhci_readl(sdhci_ctrlr
,
112 while (buffer_end
> buffer
)
113 sdhci_writel(sdhci_ctrlr
, *buffer
++,
115 if (++block_count
>= data
->blocks
)
117 } while (!(stat
& SDHCI_INT_DATA_END
));
121 static int sdhci_send_command_bounced(struct sd_mmc_ctrlr
*ctrlr
,
122 struct mmc_command
*cmd
, struct mmc_data
*data
,
123 struct bounce_buffer
*bbstate
)
125 struct sdhci_ctrlr
*sdhci_ctrlr
= (struct sdhci_ctrlr
*)ctrlr
;
127 unsigned int stat
= 0;
130 unsigned int timeout
, start_addr
= 0;
136 sdhci_writel(sdhci_ctrlr
, SDHCI_INT_ALL_MASK
, SDHCI_INT_STATUS
);
137 mask
= SDHCI_CMD_INHIBIT
| SDHCI_DATA_INHIBIT
;
139 /* We shouldn't wait for data inhibit for stop commands, even
140 though they might use busy signaling */
141 if (cmd
->flags
& CMD_FLAG_IGNORE_INHIBIT
)
142 mask
&= ~SDHCI_DATA_INHIBIT
;
144 while (sdhci_readl(sdhci_ctrlr
, SDHCI_PRESENT_STATE
) & mask
) {
146 sdhc_trace("Cmd: %2d, Arg: 0x%08x, not sent\n",
147 cmd
->cmdidx
, cmd
->cmdarg
);
148 sdhc_error("Controller never released inhibit bit(s), "
149 "present state %#8.8x.\n",
150 sdhci_readl(sdhci_ctrlr
, SDHCI_PRESENT_STATE
));
151 return CARD_COMM_ERR
;
157 mask
= SDHCI_INT_RESPONSE
;
158 if (!(cmd
->resp_type
& CARD_RSP_PRESENT
))
159 flags
= SDHCI_CMD_RESP_NONE
;
160 else if (cmd
->resp_type
& CARD_RSP_136
)
161 flags
= SDHCI_CMD_RESP_LONG
;
162 else if (cmd
->resp_type
& CARD_RSP_BUSY
) {
163 flags
= SDHCI_CMD_RESP_SHORT_BUSY
;
164 mask
|= SDHCI_INT_DATA_END
;
166 flags
= SDHCI_CMD_RESP_SHORT
;
168 if (cmd
->resp_type
& CARD_RSP_CRC
)
169 flags
|= SDHCI_CMD_CRC
;
170 if (cmd
->resp_type
& CARD_RSP_OPCODE
)
171 flags
|= SDHCI_CMD_INDEX
;
173 flags
|= SDHCI_CMD_DATA
;
175 /* Set Transfer mode regarding to data flag */
177 sdhci_writew(sdhci_ctrlr
,
178 SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG
,
179 data
->blocksize
), SDHCI_BLOCK_SIZE
);
181 if (data
->flags
== DATA_FLAG_READ
)
182 mode
|= SDHCI_TRNS_READ
;
184 if (data
->blocks
> 1)
185 mode
|= SDHCI_TRNS_BLK_CNT_EN
|
186 SDHCI_TRNS_MULTI
| SDHCI_TRNS_ACMD12
;
188 sdhci_writew(sdhci_ctrlr
, data
->blocks
, SDHCI_BLOCK_COUNT
);
190 if (DMA_AVAILABLE
&& (ctrlr
->caps
& DRVR_CAP_AUTO_CMD12
)
191 && (cmd
->cmdidx
!= MMC_CMD_AUTO_TUNING_SEQUENCE
)) {
192 if (sdhci_setup_adma(sdhci_ctrlr
, data
))
194 mode
|= SDHCI_TRNS_DMA
;
196 sdhci_writew(sdhci_ctrlr
, mode
, SDHCI_TRANSFER_MODE
);
199 sdhc_trace("Cmd: %2d, Arg: 0x%08x\n", cmd
->cmdidx
, cmd
->cmdarg
);
200 sdhci_writel(sdhci_ctrlr
, cmd
->cmdarg
, SDHCI_ARGUMENT
);
201 sdhci_writew(sdhci_ctrlr
, SDHCI_MAKE_CMD(cmd
->cmdidx
, flags
),
203 sdhc_log_command_issued();
205 if (DMA_AVAILABLE
&& (mode
& SDHCI_TRNS_DMA
))
206 return sdhci_complete_adma(sdhci_ctrlr
, cmd
);
208 stopwatch_init_msecs_expire(&sw
, 2550);
210 stat
= sdhci_readl(sdhci_ctrlr
, SDHCI_INT_STATUS
);
211 if (stat
& SDHCI_INT_ERROR
) {
212 sdhc_trace("Error - IntStatus: 0x%08x\n", stat
);
216 if (stat
& SDHCI_INT_DATA_AVAIL
) {
217 sdhci_writel(sdhci_ctrlr
, stat
, SDHCI_INT_STATUS
);
221 /* Apply max timeout for R1b-type CMD defined in eMMC ext_csd
222 except for erase ones */
223 if (stopwatch_expired(&sw
)) {
224 if (ctrlr
->caps
& DRVR_CAP_BROKEN_R1B
)
227 "Timeout for status update! IntStatus: 0x%08x\n",
231 } while ((stat
& mask
) != mask
);
233 if ((stat
& (SDHCI_INT_ERROR
| mask
)) == mask
) {
235 sdhci_cmd_done(sdhci_ctrlr
, cmd
);
236 sdhci_writel(sdhci_ctrlr
, mask
, SDHCI_INT_STATUS
);
241 ret
= sdhci_transfer_data(sdhci_ctrlr
, data
, start_addr
);
243 if (ctrlr
->udelay_wait_after_cmd
)
244 udelay(ctrlr
->udelay_wait_after_cmd
);
246 stat
= sdhci_readl(sdhci_ctrlr
, SDHCI_INT_STATUS
);
247 sdhci_writel(sdhci_ctrlr
, SDHCI_INT_ALL_MASK
, SDHCI_INT_STATUS
);
252 sdhci_reset(sdhci_ctrlr
, SDHCI_RESET_CMD
);
253 sdhci_reset(sdhci_ctrlr
, SDHCI_RESET_DATA
);
254 if (stat
& SDHCI_INT_TIMEOUT
) {
255 sdhc_error("CMD%d timeout, IntStatus: 0x%08x\n", cmd
->cmdidx
,
260 sdhc_error("CMD%d failed, IntStatus: 0x%08x\n", cmd
->cmdidx
, stat
);
261 return CARD_COMM_ERR
;
264 __weak
void sdhc_log_command(struct mmc_command
*cmd
)
268 __weak
void sdhc_log_command_issued(void)
272 __weak
void sdhc_log_response(uint32_t entries
,
277 __weak
void sdhc_log_ret(int ret
)
281 static void sdhci_led_control(struct sd_mmc_ctrlr
*ctrlr
, int on
)
284 struct sdhci_ctrlr
*sdhci_ctrlr
= (struct sdhci_ctrlr
*)ctrlr
;
286 host_ctrl
= sdhci_readb(sdhci_ctrlr
, SDHCI_HOST_CONTROL
);
287 host_ctrl
&= ~SDHCI_CTRL_LED
;
289 host_ctrl
|= SDHCI_CTRL_LED
;
290 sdhci_writeb(sdhci_ctrlr
, host_ctrl
, SDHCI_HOST_CONTROL
);
293 static int sdhci_send_command(struct sd_mmc_ctrlr
*ctrlr
,
294 struct mmc_command
*cmd
, struct mmc_data
*data
)
297 unsigned int bbflags
;
299 struct bounce_buffer
*bbstate
= NULL
;
300 struct bounce_buffer bbstate_val
;
303 sdhc_log_command(cmd
);
305 if (CONFIG(SDHCI_BOUNCE_BUFFER
) && data
) {
306 if (data
->flags
& DATA_FLAG_READ
) {
308 bbflags
= GEN_BB_WRITE
;
310 buf
= (void *)data
->src
;
311 bbflags
= GEN_BB_READ
;
313 len
= data
->blocks
* data
->blocksize
;
316 * on some platform(like rk3399 etc) need to worry about
317 * cache coherency, so check the buffer, if not dma
318 * coherent, use bounce_buffer to do DMA management.
320 if (!dma_coherent(buf
)) {
321 bbstate
= &bbstate_val
;
322 if (bounce_buffer_start(bbstate
, buf
, len
, bbflags
)) {
323 sdhc_error("Failed to get bounce buffer.\n");
329 sdhci_led_control(ctrlr
, 1);
330 ret
= sdhci_send_command_bounced(ctrlr
, cmd
, data
, bbstate
);
331 sdhci_led_control(ctrlr
, 0);
334 if (CONFIG(SDHCI_BOUNCE_BUFFER
) && bbstate
)
335 bounce_buffer_stop(bbstate
);
340 static int sdhci_set_clock(struct sdhci_ctrlr
*sdhci_ctrlr
, unsigned int clock
)
342 struct sd_mmc_ctrlr
*ctrlr
= &sdhci_ctrlr
->sd_mmc_ctrlr
;
343 unsigned int actual
, div
, clk
, timeout
;
345 /* Turn off the clock if requested */
348 sdhci_writew(sdhci_ctrlr
, 0, SDHCI_CLOCK_CONTROL
);
349 sdhc_debug("SDHCI bus clock: Off\n");
353 /* Compute the divisor for the new clock frequency */
354 actual
= MIN(actual
, ctrlr
->f_max
);
355 actual
= MAX(actual
, ctrlr
->f_min
);
356 if (ctrlr
->clock_base
<= actual
)
359 /* Version 3.00 divisors must be a multiple of 2. */
360 if ((ctrlr
->version
& SDHCI_SPEC_VER_MASK
)
362 div
= MIN(((ctrlr
->clock_base
+ actual
- 1)
363 / actual
), SDHCI_MAX_DIV_SPEC_300
);
364 actual
= ctrlr
->clock_base
/ div
;
367 /* Version 2.00 divisors must be a power of 2. */
368 for (div
= 1; div
< SDHCI_MAX_DIV_SPEC_200
; div
*= 2) {
369 if ((ctrlr
->clock_base
/ div
) <= actual
)
372 actual
= ctrlr
->clock_base
/ div
;
377 /* Set the new clock frequency */
378 if (actual
!= ctrlr
->bus_hz
) {
379 /* Turn off the clock */
380 sdhci_writew(sdhci_ctrlr
, 0, SDHCI_CLOCK_CONTROL
);
382 /* Set the new clock frequency */
383 clk
= (div
& SDHCI_DIV_MASK
) << SDHCI_DIVIDER_SHIFT
;
384 clk
|= ((div
& SDHCI_DIV_HI_MASK
) >> SDHCI_DIV_MASK_LEN
)
385 << SDHCI_DIVIDER_HI_SHIFT
;
386 clk
|= SDHCI_CLOCK_INT_EN
;
387 sdhci_writew(sdhci_ctrlr
, clk
, SDHCI_CLOCK_CONTROL
);
389 /* Display the requested clock frequency */
390 sdhc_debug("SDHCI bus clock: %d.%03d MHz\n",
392 (actual
/ 1000) % 1000);
396 while (!((clk
= sdhci_readw(sdhci_ctrlr
, SDHCI_CLOCK_CONTROL
))
397 & SDHCI_CLOCK_INT_STABLE
)) {
400 "Internal clock never stabilised.\n");
407 clk
|= SDHCI_CLOCK_CARD_EN
;
408 sdhci_writew(sdhci_ctrlr
, clk
, SDHCI_CLOCK_CONTROL
);
409 ctrlr
->bus_hz
= actual
;
414 static void sdhci_set_power(struct sdhci_ctrlr
*sdhci_ctrlr
,
415 unsigned short power
)
417 struct sd_mmc_ctrlr
*ctrlr
= &sdhci_ctrlr
->sd_mmc_ctrlr
;
422 if (power
!= (unsigned short)-1) {
423 switch (1 << power
) {
424 case MMC_VDD_165_195
:
426 pwr
= SDHCI_POWER_180
;
431 pwr
= SDHCI_POWER_300
;
436 pwr
= SDHCI_POWER_330
;
441 /* Determine the power state */
442 pwr_ctrl
= sdhci_readb(sdhci_ctrlr
, SDHCI_POWER_CONTROL
);
444 if (pwr_ctrl
& SDHCI_POWER_ON
)
445 sdhc_debug("SDHCI voltage: Off\n");
446 sdhci_writeb(sdhci_ctrlr
, 0, SDHCI_POWER_CONTROL
);
450 /* Determine if the power has changed */
451 if (pwr_ctrl
!= (pwr
| SDHCI_POWER_ON
)) {
452 sdhc_debug("SDHCI voltage: %s Volts\n", voltage
);
454 /* Select the voltage */
455 if (ctrlr
->caps
& DRVR_CAP_NO_SIMULT_VDD_AND_POWER
)
456 sdhci_writeb(sdhci_ctrlr
, pwr
, SDHCI_POWER_CONTROL
);
458 /* Apply power to the SD/MMC device */
459 pwr
|= SDHCI_POWER_ON
;
460 sdhci_writeb(sdhci_ctrlr
, pwr
, SDHCI_POWER_CONTROL
);
464 const u16 speed_driver_voltage
[] = {
465 0, /* 0: BUS_TIMING_LEGACY */
466 0, /* 1: BUS_TIMING_MMC_HS */
467 0, /* 2: BUS_TIMING_SD_HS */
468 SDHCI_CTRL_UHS_SDR12
| SDHCI_CTRL_VDD_180
, /* 3: BUS_TIMING_UHS_SDR12 */
469 SDHCI_CTRL_UHS_SDR25
| SDHCI_CTRL_VDD_180
, /* 4: BUS_TIMING_UHS_SDR25 */
470 SDHCI_CTRL_UHS_SDR50
| SDHCI_CTRL_VDD_180
, /* 5: BUS_TIMING_UHS_SDR50 */
471 /* 6: BUS_TIMING_UHS_SDR104 */
472 SDHCI_CTRL_UHS_SDR104
| SDHCI_CTRL_DRV_TYPE_A
| SDHCI_CTRL_VDD_180
,
473 SDHCI_CTRL_UHS_DDR50
| SDHCI_CTRL_VDD_180
, /* 7: BUS_TIMING_UHS_DDR50 */
474 SDHCI_CTRL_UHS_DDR50
| SDHCI_CTRL_VDD_180
, /* 8: BUS_TIMING_MMC_DDR52 */
475 /* 9: BUS_TIMING_MMC_HS200 */
476 SDHCI_CTRL_UHS_SDR104
| SDHCI_CTRL_DRV_TYPE_A
| SDHCI_CTRL_VDD_180
,
477 /* 10: BUS_TIMING_MMC_HS400 */
478 SDHCI_CTRL_HS400
| SDHCI_CTRL_DRV_TYPE_A
| SDHCI_CTRL_VDD_180
,
479 /* 11: BUS_TIMING_MMC_HS400ES */
480 SDHCI_CTRL_HS400
| SDHCI_CTRL_DRV_TYPE_A
| SDHCI_CTRL_VDD_180
483 static void sdhci_set_uhs_signaling(struct sdhci_ctrlr
*sdhci_ctrlr
,
488 /* Select bus speed mode, driver and VDD 1.8 volt support */
489 ctrl_2
= sdhci_readw(sdhci_ctrlr
, SDHCI_HOST_CONTROL2
);
490 ctrl_2
&= ~(SDHCI_CTRL_UHS_MASK
| SDHCI_CTRL_DRV_TYPE_MASK
491 | SDHCI_CTRL_VDD_180
);
492 if (timing
< ARRAY_SIZE(speed_driver_voltage
))
493 ctrl_2
|= speed_driver_voltage
[timing
];
494 sdhci_writew(sdhci_ctrlr
, ctrl_2
, SDHCI_HOST_CONTROL2
);
497 static void sdhci_set_ios(struct sd_mmc_ctrlr
*ctrlr
)
499 struct sdhci_ctrlr
*sdhci_ctrlr
= (struct sdhci_ctrlr
*)ctrlr
;
505 /* Set the clock frequency */
506 if (ctrlr
->bus_hz
!= ctrlr
->request_hz
)
507 sdhci_set_clock(sdhci_ctrlr
, ctrlr
->request_hz
);
509 /* Switch to 1.8 volt for HS200 */
510 if (ctrlr
->caps
& DRVR_CAP_1V8_VDD
)
511 if (ctrlr
->bus_hz
== CLOCK_200MHZ
)
512 sdhci_set_power(sdhci_ctrlr
, MMC_VDD_165_195_SHIFT
);
514 /* Determine the new bus width */
516 ctrl
= sdhci_readb(sdhci_ctrlr
, SDHCI_HOST_CONTROL
);
517 previous_ctrl
= ctrl
;
518 ctrl
&= ~SDHCI_CTRL_4BITBUS
;
519 version
= ctrlr
->version
& SDHCI_SPEC_VER_MASK
;
520 if (version
>= SDHCI_SPEC_300
)
521 ctrl
&= ~SDHCI_CTRL_8BITBUS
;
523 if ((ctrlr
->bus_width
== 8) && (version
>= SDHCI_SPEC_300
)) {
524 ctrl
|= SDHCI_CTRL_8BITBUS
;
526 } else if (ctrlr
->bus_width
== 4) {
527 ctrl
|= SDHCI_CTRL_4BITBUS
;
531 if (!(ctrlr
->timing
== BUS_TIMING_LEGACY
) &&
532 !(ctrlr
->caps
& DRVR_CAP_NO_HISPD_BIT
))
533 ctrl
|= SDHCI_CTRL_HISPD
;
535 ctrl
&= ~SDHCI_CTRL_HISPD
;
537 sdhci_set_uhs_signaling(sdhci_ctrlr
, ctrlr
->timing
);
540 if (ctrlr
->caps
& DRVR_CAP_AUTO_CMD12
) {
541 ctrl
&= ~SDHCI_CTRL_DMA_MASK
;
542 if (ctrlr
->caps
& DRVR_CAP_DMA_64BIT
)
543 ctrl
|= SDHCI_CTRL_ADMA64
;
545 ctrl
|= SDHCI_CTRL_ADMA32
;
549 /* Set the new bus width */
550 if (CONFIG(SDHC_DEBUG
)
551 && ((ctrl
^ previous_ctrl
) & (SDHCI_CTRL_4BITBUS
552 | ((version
>= SDHCI_SPEC_300
) ? SDHCI_CTRL_8BITBUS
: 0))))
553 sdhc_debug("SDHCI bus width: %d bit%s\n", bus_width
,
554 (bus_width
!= 1) ? "s" : "");
555 sdhci_writeb(sdhci_ctrlr
, ctrl
, SDHCI_HOST_CONTROL
);
558 static void sdhci_tuning_start(struct sd_mmc_ctrlr
*ctrlr
, int retune
)
561 struct sdhci_ctrlr
*sdhci_ctrlr
= (struct sdhci_ctrlr
*)ctrlr
;
563 /* Start the bus tuning */
564 host_ctrl2
= sdhci_readw(sdhci_ctrlr
, SDHCI_HOST_CONTROL2
);
565 host_ctrl2
&= ~SDHCI_CTRL_TUNED_CLK
;
566 host_ctrl2
|= (retune
? SDHCI_CTRL_TUNED_CLK
: 0)
567 | SDHCI_CTRL_EXEC_TUNING
;
568 sdhci_writew(sdhci_ctrlr
, host_ctrl2
, SDHCI_HOST_CONTROL2
);
571 static int sdhci_is_tuning_complete(struct sd_mmc_ctrlr
*ctrlr
, int *successful
)
574 struct sdhci_ctrlr
*sdhci_ctrlr
= (struct sdhci_ctrlr
*)ctrlr
;
576 /* Determine if the bus tuning has completed */
577 host_ctrl2
= sdhci_readw(sdhci_ctrlr
, SDHCI_HOST_CONTROL2
);
578 *successful
= ((host_ctrl2
& SDHCI_CTRL_TUNED_CLK
) != 0);
579 return ((host_ctrl2
& SDHCI_CTRL_EXEC_TUNING
) == 0);
582 /* Prepare SDHCI controller to be initialized */
583 static int sdhci_pre_init(struct sdhci_ctrlr
*sdhci_ctrlr
)
585 struct sd_mmc_ctrlr
*ctrlr
= &sdhci_ctrlr
->sd_mmc_ctrlr
;
586 unsigned int caps
, caps_1
;
589 * If the device needs to do anything non-standard before
590 * sdhci initialization, run it here.
592 if (sdhci_ctrlr
->attach
) {
593 int rv
= sdhci_ctrlr
->attach(sdhci_ctrlr
);
598 /* Get controller version and capabilities */
599 ctrlr
->version
= sdhci_readw(sdhci_ctrlr
, SDHCI_HOST_VERSION
) & 0xff;
600 caps
= sdhci_readl(sdhci_ctrlr
, SDHCI_CAPABILITIES
);
601 caps_1
= sdhci_readl(sdhci_ctrlr
, SDHCI_CAPABILITIES_1
);
603 /* Determine the supported voltages */
604 if (caps
& SDHCI_CAN_VDD_330
)
605 ctrlr
->voltages
|= MMC_VDD_32_33
| MMC_VDD_33_34
;
606 if (caps
& SDHCI_CAN_VDD_300
)
607 ctrlr
->voltages
|= MMC_VDD_29_30
| MMC_VDD_30_31
;
608 if (caps
& SDHCI_CAN_VDD_180
)
609 ctrlr
->voltages
|= MMC_VDD_165_195
;
611 /* Get the controller's base clock frequency */
612 if ((ctrlr
->version
& SDHCI_SPEC_VER_MASK
) >= SDHCI_SPEC_300
)
613 ctrlr
->clock_base
= (caps
& SDHCI_CLOCK_V3_BASE_MASK
)
614 >> SDHCI_CLOCK_BASE_SHIFT
;
616 ctrlr
->clock_base
= (caps
& SDHCI_CLOCK_BASE_MASK
)
617 >> SDHCI_CLOCK_BASE_SHIFT
;
618 ctrlr
->clock_base
*= 1000000;
619 ctrlr
->f_max
= ctrlr
->clock_base
;
621 /* Determine the controller's clock frequency range */
623 if ((ctrlr
->version
& SDHCI_SPEC_VER_MASK
) >= SDHCI_SPEC_300
)
625 ctrlr
->clock_base
/ SDHCI_MAX_DIV_SPEC_300
;
628 ctrlr
->clock_base
/ SDHCI_MAX_DIV_SPEC_200
;
630 /* Determine the controller's modes of operation */
631 ctrlr
->caps
|= DRVR_CAP_HS52
| DRVR_CAP_HS
;
632 if (ctrlr
->clock_base
>= CLOCK_200MHZ
) {
633 ctrlr
->caps
|= DRVR_CAP_HS200
| DRVR_CAP_HS200_TUNING
;
634 if (caps_1
& SDHCI_SUPPORT_HS400
)
635 ctrlr
->caps
|= DRVR_CAP_HS400
636 | DRVR_CAP_ENHANCED_STROBE
;
639 /* Determine the bus widths the controller supports */
640 ctrlr
->caps
|= DRVR_CAP_4BIT
;
641 if (caps
& SDHCI_CAN_DO_8BIT
)
642 ctrlr
->caps
|= DRVR_CAP_8BIT
;
644 /* Determine the controller's DMA support */
645 if (caps
& SDHCI_CAN_DO_ADMA2
)
646 ctrlr
->caps
|= DRVR_CAP_AUTO_CMD12
;
647 if (DMA_AVAILABLE
&& (caps
& SDHCI_CAN_64BIT
))
648 ctrlr
->caps
|= DRVR_CAP_DMA_64BIT
;
650 /* Specify the modes that the driver stack supports */
651 ctrlr
->caps
|= DRVR_CAP_HC
;
653 /* Let the SOC adjust the configuration to handle controller quirks */
654 soc_sd_mmc_controller_quirks(&sdhci_ctrlr
->sd_mmc_ctrlr
);
655 if (ctrlr
->clock_base
== 0) {
656 sdhc_error("Hardware doesn't specify base clock frequency\n");
660 ctrlr
->f_max
= ctrlr
->clock_base
;
662 /* Display the results */
663 sdhc_trace("0x%08x: ctrlr->caps\n", ctrlr
->caps
);
664 sdhc_trace("%d.%03d MHz: ctrlr->clock_base\n",
665 ctrlr
->clock_base
/ 1000000,
666 (ctrlr
->clock_base
/ 1000) % 1000);
667 sdhc_trace("%d.%03d MHz: ctrlr->f_max\n",
668 ctrlr
->f_max
/ 1000000,
669 (ctrlr
->f_max
/ 1000) % 1000);
670 sdhc_trace("%d.%03d MHz: ctrlr->f_min\n",
671 ctrlr
->f_min
/ 1000000,
672 (ctrlr
->f_min
/ 1000) % 1000);
673 sdhc_trace("0x%08x: ctrlr->voltages\n", ctrlr
->voltages
);
675 sdhci_reset(sdhci_ctrlr
, SDHCI_RESET_ALL
);
680 __weak
void soc_sd_mmc_controller_quirks(struct sd_mmc_ctrlr
685 static int sdhci_init(struct sdhci_ctrlr
*sdhci_ctrlr
)
687 struct sd_mmc_ctrlr
*ctrlr
= &sdhci_ctrlr
->sd_mmc_ctrlr
;
690 /* Only initialize the controller upon reset or card insertion */
691 if (ctrlr
->initialized
)
694 sdhc_debug("SDHCI Controller Base Address: %p\n",
695 sdhci_ctrlr
->ioaddr
);
697 rv
= sdhci_pre_init(sdhci_ctrlr
);
699 return rv
; /* The error has been already reported */
701 sdhci_set_power(sdhci_ctrlr
, __fls(ctrlr
->voltages
));
703 if (ctrlr
->caps
& DRVR_CAP_NO_CD
) {
706 sdhci_writel(sdhci_ctrlr
, SDHCI_CTRL_CD_TEST_INS
707 | SDHCI_CTRL_CD_TEST
, SDHCI_HOST_CONTROL
);
709 status
= sdhci_readl(sdhci_ctrlr
, SDHCI_PRESENT_STATE
);
710 while ((!(status
& SDHCI_CARD_PRESENT
)) ||
711 (!(status
& SDHCI_CARD_STATE_STABLE
)) ||
712 (!(status
& SDHCI_CARD_DETECT_PIN_LEVEL
)))
713 status
= sdhci_readl(sdhci_ctrlr
, SDHCI_PRESENT_STATE
);
716 /* Enable only interrupts served by the SD controller */
717 sdhci_writel(sdhci_ctrlr
, SDHCI_INT_DATA_MASK
| SDHCI_INT_CMD_MASK
,
719 /* Mask all sdhci interrupt sources */
720 sdhci_writel(sdhci_ctrlr
, 0x0, SDHCI_SIGNAL_ENABLE
);
722 /* Set timeout to maximum, shouldn't happen if everything's right. */
723 sdhci_writeb(sdhci_ctrlr
, 0xe, SDHCI_TIMEOUT_CONTROL
);
726 ctrlr
->initialized
= 1;
730 static int sdhci_update(struct sdhci_ctrlr
*sdhci_ctrlr
)
732 struct sd_mmc_ctrlr
*ctrlr
= &sdhci_ctrlr
->sd_mmc_ctrlr
;
734 if (ctrlr
->caps
& DRVR_CAP_REMOVABLE
) {
735 int present
= (sdhci_readl(sdhci_ctrlr
, SDHCI_PRESENT_STATE
) &
736 SDHCI_CARD_PRESENT
) != 0;
739 /* A card was present indicate the controller needs
740 * initialization on the next call.
742 ctrlr
->initialized
= 0;
747 /* A card is present, get it ready. */
748 if (sdhci_init(sdhci_ctrlr
))
753 void sdhci_update_pointers(struct sdhci_ctrlr
*sdhci_ctrlr
)
755 struct sd_mmc_ctrlr
*ctrlr
= &sdhci_ctrlr
->sd_mmc_ctrlr
;
757 /* Update the routine pointers */
758 ctrlr
->send_cmd
= &sdhci_send_command
;
759 ctrlr
->set_ios
= &sdhci_set_ios
;
760 ctrlr
->tuning_start
= &sdhci_tuning_start
;
761 ctrlr
->is_tuning_complete
= &sdhci_is_tuning_complete
;
764 int add_sdhci(struct sdhci_ctrlr
*sdhci_ctrlr
)
766 struct sd_mmc_ctrlr
*ctrlr
= &sdhci_ctrlr
->sd_mmc_ctrlr
;
768 sdhci_update_pointers(sdhci_ctrlr
);
770 /* TODO(vbendeb): check if SDHCI spec allows to retrieve this value. */
771 ctrlr
->b_max
= 65535;
773 /* Initialize the SDHC controller */
774 return sdhci_update(sdhci_ctrlr
);