1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2011, Marvell Semiconductor Inc.
4 * Lei Wen <leiwen@marvell.com>
6 * Back ported to the 8xx platform (from the 8260 platform) by
7 * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
19 static void sdhci_reset(struct sdhci_host *host, u8 mask)
21 unsigned long timeout;
25 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
26 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
28 printf("%s: Reset 0x%x never completed.\n",
37 static void sdhci_cmd_done(struct sdhci_host *host, struct mmc_cmd *cmd)
40 if (cmd->resp_type & MMC_RSP_136) {
41 /* CRC is stripped so we need to do some shifting. */
42 for (i = 0; i < 4; i++) {
43 cmd->response[i] = sdhci_readl(host,
44 SDHCI_RESPONSE + (3-i)*4) << 8;
46 cmd->response[i] |= sdhci_readb(host,
47 SDHCI_RESPONSE + (3-i)*4-1);
50 cmd->response[0] = sdhci_readl(host, SDHCI_RESPONSE);
54 static void sdhci_transfer_pio(struct sdhci_host *host, struct mmc_data *data)
58 for (i = 0; i < data->blocksize; i += 4) {
59 offs = data->dest + i;
60 if (data->flags == MMC_DATA_READ)
61 *(u32 *)offs = sdhci_readl(host, SDHCI_BUFFER);
63 sdhci_writel(host, *(u32 *)offs, SDHCI_BUFFER);
67 #if CONFIG_IS_ENABLED(MMC_SDHCI_ADMA)
68 static void sdhci_adma_desc(struct sdhci_host *host, char *buf, u16 len,
71 struct sdhci_adma_desc *desc;
74 desc = &host->adma_desc_table[host->desc_slot];
76 attr = ADMA_DESC_ATTR_VALID | ADMA_DESC_TRANSFER_DATA;
80 attr |= ADMA_DESC_ATTR_END;
85 desc->addr_lo = (dma_addr_t)buf;
86 #ifdef CONFIG_DMA_ADDR_T_64BIT
87 desc->addr_hi = (u64)buf >> 32;
91 static void sdhci_prepare_adma_table(struct sdhci_host *host,
92 struct mmc_data *data)
94 uint trans_bytes = data->blocksize * data->blocks;
95 uint desc_count = DIV_ROUND_UP(trans_bytes, ADMA_MAX_LEN);
101 if (data->flags & MMC_DATA_READ)
104 buf = (char *)data->src;
107 sdhci_adma_desc(host, buf, ADMA_MAX_LEN, false);
109 trans_bytes -= ADMA_MAX_LEN;
112 sdhci_adma_desc(host, buf, trans_bytes, true);
114 flush_cache((dma_addr_t)host->adma_desc_table,
115 ROUND(desc_count * sizeof(struct sdhci_adma_desc),
118 #elif defined(CONFIG_MMC_SDHCI_SDMA)
119 static void sdhci_prepare_adma_table(struct sdhci_host *host,
120 struct mmc_data *data)
123 #if (defined(CONFIG_MMC_SDHCI_SDMA) || CONFIG_IS_ENABLED(MMC_SDHCI_ADMA))
124 static void sdhci_prepare_dma(struct sdhci_host *host, struct mmc_data *data,
125 int *is_aligned, int trans_bytes)
129 if (data->flags == MMC_DATA_READ)
130 host->start_addr = (dma_addr_t)data->dest;
132 host->start_addr = (dma_addr_t)data->src;
134 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
135 ctrl &= ~SDHCI_CTRL_DMA_MASK;
136 if (host->flags & USE_ADMA64)
137 ctrl |= SDHCI_CTRL_ADMA64;
138 else if (host->flags & USE_ADMA)
139 ctrl |= SDHCI_CTRL_ADMA32;
140 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
142 if (host->flags & USE_SDMA) {
143 if (host->force_align_buffer ||
144 (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR &&
145 (host->start_addr & 0x7) != 0x0)) {
147 host->start_addr = (unsigned long)host->align_buffer;
148 if (data->flags != MMC_DATA_READ)
149 memcpy(host->align_buffer, data->src,
152 sdhci_writel(host, host->start_addr, SDHCI_DMA_ADDRESS);
153 } else if (host->flags & (USE_ADMA | USE_ADMA64)) {
154 sdhci_prepare_adma_table(host, data);
156 sdhci_writel(host, lower_32_bits(host->adma_addr),
158 if (host->flags & USE_ADMA64)
159 sdhci_writel(host, upper_32_bits(host->adma_addr),
160 SDHCI_ADMA_ADDRESS_HI);
163 flush_cache(host->start_addr, ROUND(trans_bytes, ARCH_DMA_MINALIGN));
166 static void sdhci_prepare_dma(struct sdhci_host *host, struct mmc_data *data,
167 int *is_aligned, int trans_bytes)
170 static int sdhci_transfer_data(struct sdhci_host *host, struct mmc_data *data)
172 dma_addr_t start_addr = host->start_addr;
173 unsigned int stat, rdy, mask, timeout, block = 0;
174 bool transfer_done = false;
177 rdy = SDHCI_INT_SPACE_AVAIL | SDHCI_INT_DATA_AVAIL;
178 mask = SDHCI_DATA_AVAILABLE | SDHCI_SPACE_AVAILABLE;
180 stat = sdhci_readl(host, SDHCI_INT_STATUS);
181 if (stat & SDHCI_INT_ERROR) {
182 pr_debug("%s: Error detected in status(0x%X)!\n",
186 if (!transfer_done && (stat & rdy)) {
187 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & mask))
189 sdhci_writel(host, rdy, SDHCI_INT_STATUS);
190 sdhci_transfer_pio(host, data);
191 data->dest += data->blocksize;
192 if (++block >= data->blocks) {
193 /* Keep looping until the SDHCI_INT_DATA_END is
194 * cleared, even if we finished sending all the
197 transfer_done = true;
201 if ((host->flags & USE_DMA) && !transfer_done &&
202 (stat & SDHCI_INT_DMA_END)) {
203 sdhci_writel(host, SDHCI_INT_DMA_END, SDHCI_INT_STATUS);
204 if (host->flags & USE_SDMA) {
206 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1);
207 start_addr += SDHCI_DEFAULT_BOUNDARY_SIZE;
208 sdhci_writel(host, start_addr,
215 printf("%s: Transfer data timeout\n", __func__);
218 } while (!(stat & SDHCI_INT_DATA_END));
223 * No command will be sent by driver if card is busy, so driver must wait
224 * for card ready state.
225 * Every time when card is busy after timeout then (last) timeout value will be
226 * increased twice but only if it doesn't exceed global defined maximum.
227 * Each function call will use last timeout value.
229 #define SDHCI_CMD_MAX_TIMEOUT 3200
230 #define SDHCI_CMD_DEFAULT_TIMEOUT 100
231 #define SDHCI_READ_STATUS_TIMEOUT 1000
234 static int sdhci_send_command(struct udevice *dev, struct mmc_cmd *cmd,
235 struct mmc_data *data)
237 struct mmc *mmc = mmc_get_mmc_dev(dev);
240 static int sdhci_send_command(struct mmc *mmc, struct mmc_cmd *cmd,
241 struct mmc_data *data)
244 struct sdhci_host *host = mmc->priv;
245 unsigned int stat = 0;
247 int trans_bytes = 0, is_aligned = 1;
248 u32 mask, flags, mode;
249 unsigned int time = 0;
250 int mmc_dev = mmc_get_blk_desc(mmc)->devnum;
251 ulong start = get_timer(0);
253 host->start_addr = 0;
254 /* Timeout unit - ms */
255 static unsigned int cmd_timeout = SDHCI_CMD_DEFAULT_TIMEOUT;
257 mask = SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT;
259 /* We shouldn't wait for data inihibit for stop commands, even
260 though they might use busy signaling */
261 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION ||
262 ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK ||
263 cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200) && !data))
264 mask &= ~SDHCI_DATA_INHIBIT;
266 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
267 if (time >= cmd_timeout) {
268 printf("%s: MMC: %d busy ", __func__, mmc_dev);
269 if (2 * cmd_timeout <= SDHCI_CMD_MAX_TIMEOUT) {
270 cmd_timeout += cmd_timeout;
271 printf("timeout increasing to: %u ms.\n",
282 sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
284 mask = SDHCI_INT_RESPONSE;
285 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK ||
286 cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200) && !data)
287 mask = SDHCI_INT_DATA_AVAIL;
289 if (!(cmd->resp_type & MMC_RSP_PRESENT))
290 flags = SDHCI_CMD_RESP_NONE;
291 else if (cmd->resp_type & MMC_RSP_136)
292 flags = SDHCI_CMD_RESP_LONG;
293 else if (cmd->resp_type & MMC_RSP_BUSY) {
294 flags = SDHCI_CMD_RESP_SHORT_BUSY;
296 mask |= SDHCI_INT_DATA_END;
298 flags = SDHCI_CMD_RESP_SHORT;
300 if (cmd->resp_type & MMC_RSP_CRC)
301 flags |= SDHCI_CMD_CRC;
302 if (cmd->resp_type & MMC_RSP_OPCODE)
303 flags |= SDHCI_CMD_INDEX;
304 if (data || cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK ||
305 cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)
306 flags |= SDHCI_CMD_DATA;
308 /* Set Transfer mode regarding to data flag */
310 sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL);
311 mode = SDHCI_TRNS_BLK_CNT_EN;
312 trans_bytes = data->blocks * data->blocksize;
313 if (data->blocks > 1)
314 mode |= SDHCI_TRNS_MULTI;
316 if (data->flags == MMC_DATA_READ)
317 mode |= SDHCI_TRNS_READ;
319 if (host->flags & USE_DMA) {
320 mode |= SDHCI_TRNS_DMA;
321 sdhci_prepare_dma(host, data, &is_aligned, trans_bytes);
324 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
327 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
328 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
329 } else if (cmd->resp_type & MMC_RSP_BUSY) {
330 sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL);
333 sdhci_writel(host, cmd->cmdarg, SDHCI_ARGUMENT);
334 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->cmdidx, flags), SDHCI_COMMAND);
335 start = get_timer(0);
337 stat = sdhci_readl(host, SDHCI_INT_STATUS);
338 if (stat & SDHCI_INT_ERROR)
341 if (get_timer(start) >= SDHCI_READ_STATUS_TIMEOUT) {
342 if (host->quirks & SDHCI_QUIRK_BROKEN_R1B) {
345 printf("%s: Timeout for status update!\n",
350 } while ((stat & mask) != mask);
352 if ((stat & (SDHCI_INT_ERROR | mask)) == mask) {
353 sdhci_cmd_done(host, cmd);
354 sdhci_writel(host, mask, SDHCI_INT_STATUS);
359 ret = sdhci_transfer_data(host, data);
361 if (host->quirks & SDHCI_QUIRK_WAIT_SEND_CMD)
364 stat = sdhci_readl(host, SDHCI_INT_STATUS);
365 sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
367 if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
368 !is_aligned && (data->flags == MMC_DATA_READ))
369 memcpy(data->dest, host->align_buffer, trans_bytes);
373 sdhci_reset(host, SDHCI_RESET_CMD);
374 sdhci_reset(host, SDHCI_RESET_DATA);
375 if (stat & SDHCI_INT_TIMEOUT)
381 #if defined(CONFIG_DM_MMC) && defined(MMC_SUPPORTS_TUNING)
382 static int sdhci_execute_tuning(struct udevice *dev, uint opcode)
385 struct mmc *mmc = mmc_get_mmc_dev(dev);
386 struct sdhci_host *host = mmc->priv;
388 debug("%s\n", __func__);
390 if (host->ops && host->ops->platform_execute_tuning) {
391 err = host->ops->platform_execute_tuning(mmc, opcode);
399 int sdhci_set_clock(struct mmc *mmc, unsigned int clock)
401 struct sdhci_host *host = mmc->priv;
402 unsigned int div, clk = 0, timeout;
406 while (sdhci_readl(host, SDHCI_PRESENT_STATE) &
407 (SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT)) {
409 printf("%s: Timeout to wait cmd & data inhibit\n",
418 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
423 if (host->ops && host->ops->set_delay)
424 host->ops->set_delay(host);
426 if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) {
428 * Check if the Host Controller supports Programmable Clock
432 for (div = 1; div <= 1024; div++) {
433 if ((host->max_clk / div) <= clock)
438 * Set Programmable Clock Mode in the Clock
441 clk = SDHCI_PROG_CLOCK_MODE;
444 /* Version 3.00 divisors must be a multiple of 2. */
445 if (host->max_clk <= clock) {
449 div < SDHCI_MAX_DIV_SPEC_300;
451 if ((host->max_clk / div) <= clock)
458 /* Version 2.00 divisors must be a power of 2. */
459 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
460 if ((host->max_clk / div) <= clock)
466 if (host->ops && host->ops->set_clock)
467 host->ops->set_clock(host, div);
469 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
470 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
471 << SDHCI_DIVIDER_HI_SHIFT;
472 clk |= SDHCI_CLOCK_INT_EN;
473 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
477 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
478 & SDHCI_CLOCK_INT_STABLE)) {
480 printf("%s: Internal clock never stabilised.\n",
488 clk |= SDHCI_CLOCK_CARD_EN;
489 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
493 static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
497 if (power != (unsigned short)-1) {
498 switch (1 << power) {
499 case MMC_VDD_165_195:
500 pwr = SDHCI_POWER_180;
504 pwr = SDHCI_POWER_300;
508 pwr = SDHCI_POWER_330;
514 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
518 pwr |= SDHCI_POWER_ON;
520 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
523 void sdhci_set_uhs_timing(struct sdhci_host *host)
525 struct mmc *mmc = (struct mmc *)host->mmc;
528 reg = sdhci_readw(host, SDHCI_HOST_CONTROL2);
529 reg &= ~SDHCI_CTRL_UHS_MASK;
531 switch (mmc->selected_mode) {
534 reg |= SDHCI_CTRL_UHS_SDR50;
538 reg |= SDHCI_CTRL_UHS_DDR50;
542 reg |= SDHCI_CTRL_UHS_SDR104;
545 reg |= SDHCI_CTRL_UHS_SDR12;
548 sdhci_writew(host, reg, SDHCI_HOST_CONTROL2);
552 static int sdhci_set_ios(struct udevice *dev)
554 struct mmc *mmc = mmc_get_mmc_dev(dev);
556 static int sdhci_set_ios(struct mmc *mmc)
560 struct sdhci_host *host = mmc->priv;
562 if (host->ops && host->ops->set_control_reg)
563 host->ops->set_control_reg(host);
565 if (mmc->clock != host->clock)
566 sdhci_set_clock(mmc, mmc->clock);
568 if (mmc->clk_disable)
569 sdhci_set_clock(mmc, 0);
572 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
573 if (mmc->bus_width == 8) {
574 ctrl &= ~SDHCI_CTRL_4BITBUS;
575 if ((SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) ||
576 (host->quirks & SDHCI_QUIRK_USE_WIDE8))
577 ctrl |= SDHCI_CTRL_8BITBUS;
579 if ((SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) ||
580 (host->quirks & SDHCI_QUIRK_USE_WIDE8))
581 ctrl &= ~SDHCI_CTRL_8BITBUS;
582 if (mmc->bus_width == 4)
583 ctrl |= SDHCI_CTRL_4BITBUS;
585 ctrl &= ~SDHCI_CTRL_4BITBUS;
588 if (mmc->clock > 26000000)
589 ctrl |= SDHCI_CTRL_HISPD;
591 ctrl &= ~SDHCI_CTRL_HISPD;
593 if ((host->quirks & SDHCI_QUIRK_NO_HISPD_BIT) ||
594 (host->quirks & SDHCI_QUIRK_BROKEN_HISPD_MODE))
595 ctrl &= ~SDHCI_CTRL_HISPD;
597 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
599 /* If available, call the driver specific "post" set_ios() function */
600 if (host->ops && host->ops->set_ios_post)
601 return host->ops->set_ios_post(host);
606 static int sdhci_init(struct mmc *mmc)
608 struct sdhci_host *host = mmc->priv;
609 #if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_GPIO)
610 struct udevice *dev = mmc->dev;
612 gpio_request_by_name(dev, "cd-gpios", 0,
613 &host->cd_gpio, GPIOD_IS_IN);
616 sdhci_reset(host, SDHCI_RESET_ALL);
618 #if defined(CONFIG_FIXED_SDHCI_ALIGNED_BUFFER)
619 host->align_buffer = (void *)CONFIG_FIXED_SDHCI_ALIGNED_BUFFER;
621 * Always use this bounce-buffer when CONFIG_FIXED_SDHCI_ALIGNED_BUFFER
624 host->force_align_buffer = true;
626 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) {
627 host->align_buffer = memalign(8, 512 * 1024);
628 if (!host->align_buffer) {
629 printf("%s: Aligned buffer alloc failed!!!\n",
636 sdhci_set_power(host, fls(mmc->cfg->voltages) - 1);
638 if (host->ops && host->ops->get_cd)
639 host->ops->get_cd(host);
641 /* Enable only interrupts served by the SD controller */
642 sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK,
644 /* Mask all sdhci interrupt sources */
645 sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE);
651 int sdhci_probe(struct udevice *dev)
653 struct mmc *mmc = mmc_get_mmc_dev(dev);
655 return sdhci_init(mmc);
658 static int sdhci_get_cd(struct udevice *dev)
660 struct mmc *mmc = mmc_get_mmc_dev(dev);
661 struct sdhci_host *host = mmc->priv;
664 /* If nonremovable, assume that the card is always present. */
665 if (mmc->cfg->host_caps & MMC_CAP_NONREMOVABLE)
667 /* If polling, assume that the card is always present. */
668 if (mmc->cfg->host_caps & MMC_CAP_NEEDS_POLL)
671 #if CONFIG_IS_ENABLED(DM_GPIO)
672 value = dm_gpio_get_value(&host->cd_gpio);
674 if (mmc->cfg->host_caps & MMC_CAP_CD_ACTIVE_HIGH)
680 value = !!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
682 if (mmc->cfg->host_caps & MMC_CAP_CD_ACTIVE_HIGH)
688 const struct dm_mmc_ops sdhci_ops = {
689 .send_cmd = sdhci_send_command,
690 .set_ios = sdhci_set_ios,
691 .get_cd = sdhci_get_cd,
692 #ifdef MMC_SUPPORTS_TUNING
693 .execute_tuning = sdhci_execute_tuning,
697 static const struct mmc_ops sdhci_ops = {
698 .send_cmd = sdhci_send_command,
699 .set_ios = sdhci_set_ios,
704 int sdhci_setup_cfg(struct mmc_config *cfg, struct sdhci_host *host,
705 u32 f_max, u32 f_min)
707 u32 caps, caps_1 = 0;
708 #if CONFIG_IS_ENABLED(DM_MMC)
709 u64 dt_caps, dt_caps_mask;
711 dt_caps_mask = dev_read_u64_default(host->mmc->dev,
712 "sdhci-caps-mask", 0);
713 dt_caps = dev_read_u64_default(host->mmc->dev,
715 caps = ~(u32)dt_caps_mask &
716 sdhci_readl(host, SDHCI_CAPABILITIES);
717 caps |= (u32)dt_caps;
719 caps = sdhci_readl(host, SDHCI_CAPABILITIES);
721 debug("%s, caps: 0x%x\n", __func__, caps);
723 #ifdef CONFIG_MMC_SDHCI_SDMA
724 if (!(caps & SDHCI_CAN_DO_SDMA)) {
725 printf("%s: Your controller doesn't support SDMA!!\n",
730 host->flags |= USE_SDMA;
732 #if CONFIG_IS_ENABLED(MMC_SDHCI_ADMA)
733 if (!(caps & SDHCI_CAN_DO_ADMA2)) {
734 printf("%s: Your controller doesn't support SDMA!!\n",
738 host->adma_desc_table = (struct sdhci_adma_desc *)
739 memalign(ARCH_DMA_MINALIGN, ADMA_TABLE_SZ);
741 host->adma_addr = (dma_addr_t)host->adma_desc_table;
742 #ifdef CONFIG_DMA_ADDR_T_64BIT
743 host->flags |= USE_ADMA64;
745 host->flags |= USE_ADMA;
748 if (host->quirks & SDHCI_QUIRK_REG32_RW)
750 sdhci_readl(host, SDHCI_HOST_VERSION - 2) >> 16;
752 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
754 cfg->name = host->name;
755 #ifndef CONFIG_DM_MMC
756 cfg->ops = &sdhci_ops;
759 /* Check whether the clock multiplier is supported or not */
760 if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) {
761 #if CONFIG_IS_ENABLED(DM_MMC)
762 caps_1 = ~(u32)(dt_caps_mask >> 32) &
763 sdhci_readl(host, SDHCI_CAPABILITIES_1);
764 caps_1 |= (u32)(dt_caps >> 32);
766 caps_1 = sdhci_readl(host, SDHCI_CAPABILITIES_1);
768 debug("%s, caps_1: 0x%x\n", __func__, caps_1);
769 host->clk_mul = (caps_1 & SDHCI_CLOCK_MUL_MASK) >>
770 SDHCI_CLOCK_MUL_SHIFT;
773 if (host->max_clk == 0) {
774 if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300)
775 host->max_clk = (caps & SDHCI_CLOCK_V3_BASE_MASK) >>
776 SDHCI_CLOCK_BASE_SHIFT;
778 host->max_clk = (caps & SDHCI_CLOCK_BASE_MASK) >>
779 SDHCI_CLOCK_BASE_SHIFT;
780 host->max_clk *= 1000000;
782 host->max_clk *= host->clk_mul;
784 if (host->max_clk == 0) {
785 printf("%s: Hardware doesn't specify base clock frequency\n",
789 if (f_max && (f_max < host->max_clk))
792 cfg->f_max = host->max_clk;
796 if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300)
797 cfg->f_min = cfg->f_max / SDHCI_MAX_DIV_SPEC_300;
799 cfg->f_min = cfg->f_max / SDHCI_MAX_DIV_SPEC_200;
802 if (caps & SDHCI_CAN_VDD_330)
803 cfg->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
804 if (caps & SDHCI_CAN_VDD_300)
805 cfg->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
806 if (caps & SDHCI_CAN_VDD_180)
807 cfg->voltages |= MMC_VDD_165_195;
809 if (host->quirks & SDHCI_QUIRK_BROKEN_VOLTAGE)
810 cfg->voltages |= host->voltages;
812 cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz | MMC_MODE_4BIT;
814 /* Since Host Controller Version3.0 */
815 if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) {
816 if (!(caps & SDHCI_CAN_DO_8BIT))
817 cfg->host_caps &= ~MMC_MODE_8BIT;
820 if (host->quirks & SDHCI_QUIRK_BROKEN_HISPD_MODE) {
821 cfg->host_caps &= ~MMC_MODE_HS;
822 cfg->host_caps &= ~MMC_MODE_HS_52MHz;
825 if (!(cfg->voltages & MMC_VDD_165_195) ||
826 (host->quirks & SDHCI_QUIRK_NO_1_8_V))
827 caps_1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
828 SDHCI_SUPPORT_DDR50);
830 if (caps_1 & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
831 SDHCI_SUPPORT_DDR50))
832 cfg->host_caps |= MMC_CAP(UHS_SDR12) | MMC_CAP(UHS_SDR25);
834 if (caps_1 & SDHCI_SUPPORT_SDR104) {
835 cfg->host_caps |= MMC_CAP(UHS_SDR104) | MMC_CAP(UHS_SDR50);
837 * SD3.0: SDR104 is supported so (for eMMC) the caps2
838 * field can be promoted to support HS200.
840 cfg->host_caps |= MMC_CAP(MMC_HS_200);
841 } else if (caps_1 & SDHCI_SUPPORT_SDR50) {
842 cfg->host_caps |= MMC_CAP(UHS_SDR50);
845 if (caps_1 & SDHCI_SUPPORT_DDR50)
846 cfg->host_caps |= MMC_CAP(UHS_DDR50);
849 cfg->host_caps |= host->host_caps;
851 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
857 int sdhci_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg)
859 return mmc_bind(dev, mmc, cfg);
862 int add_sdhci(struct sdhci_host *host, u32 f_max, u32 f_min)
866 ret = sdhci_setup_cfg(&host->cfg, host, f_max, f_min);
870 host->mmc = mmc_create(&host->cfg, host);
871 if (host->mmc == NULL) {
872 printf("%s: mmc create fail!\n", __func__);