2 * (C) Copyright 2009 SAMSUNG Electronics
3 * Minkyu Kang <mk7.kang@samsung.com>
4 * Jaehoon Chung <jh80.chung@samsung.com>
5 * Portions Copyright 2011-2013 NVIDIA Corporation
7 * SPDX-License-Identifier: GPL-2.0+
10 #include <bouncebuf.h>
14 #include <asm/arch/clock.h>
15 #include <asm/arch-tegra/clk_rst.h>
16 #include <asm/arch-tegra/tegra_mmc.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 struct mmc mmc_dev[MAX_HOSTS];
22 struct mmc_host mmc_host[MAX_HOSTS];
24 #ifndef CONFIG_OF_CONTROL
25 #error "Please enable device tree support to use this driver"
28 static void mmc_set_power(struct mmc_host *host, unsigned short power)
31 debug("%s: power = %x\n", __func__, power);
33 if (power != (unsigned short)-1) {
36 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V1_8;
40 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_0;
44 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_3;
48 debug("%s: pwr = %X\n", __func__, pwr);
50 /* Set the bus voltage first (if any) */
51 writeb(pwr, &host->reg->pwrcon);
55 /* Now enable bus power */
56 pwr |= TEGRA_MMC_PWRCTL_SD_BUS_POWER;
57 writeb(pwr, &host->reg->pwrcon);
60 static void mmc_prepare_data(struct mmc_host *host, struct mmc_data *data,
61 struct bounce_buffer *bbstate)
66 debug("buf: %p (%p), data->blocks: %u, data->blocksize: %u\n",
67 bbstate->bounce_buffer, bbstate->user_buffer, data->blocks,
70 writel((u32)bbstate->bounce_buffer, &host->reg->sysad);
75 * 10 = Selects 32-bit Address ADMA2
76 * 11 = Selects 64-bit Address ADMA2
78 ctrl = readb(&host->reg->hostctl);
79 ctrl &= ~TEGRA_MMC_HOSTCTL_DMASEL_MASK;
80 ctrl |= TEGRA_MMC_HOSTCTL_DMASEL_SDMA;
81 writeb(ctrl, &host->reg->hostctl);
83 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
84 writew((7 << 12) | (data->blocksize & 0xFFF), &host->reg->blksize);
85 writew(data->blocks, &host->reg->blkcnt);
88 static void mmc_set_transfer_mode(struct mmc_host *host, struct mmc_data *data)
91 debug(" mmc_set_transfer_mode called\n");
94 * MUL1SIN0[5] : Multi/Single Block Select
95 * RD1WT0[4] : Data Transfer Direction Select
98 * ENACMD12[2] : Auto CMD12 Enable
99 * ENBLKCNT[1] : Block Count Enable
100 * ENDMA[0] : DMA Enable
102 mode = (TEGRA_MMC_TRNMOD_DMA_ENABLE |
103 TEGRA_MMC_TRNMOD_BLOCK_COUNT_ENABLE);
105 if (data->blocks > 1)
106 mode |= TEGRA_MMC_TRNMOD_MULTI_BLOCK_SELECT;
108 if (data->flags & MMC_DATA_READ)
109 mode |= TEGRA_MMC_TRNMOD_DATA_XFER_DIR_SEL_READ;
111 writew(mode, &host->reg->trnmod);
114 static int mmc_wait_inhibit(struct mmc_host *host,
116 struct mmc_data *data,
117 unsigned int timeout)
121 * CMDINHDAT[1] : Command Inhibit (DAT)
122 * CMDINHCMD[0] : Command Inhibit (CMD)
124 unsigned int mask = TEGRA_MMC_PRNSTS_CMD_INHIBIT_CMD;
127 * We shouldn't wait for data inhibit for stop commands, even
128 * though they might use busy signaling
130 if ((data == NULL) && (cmd->resp_type & MMC_RSP_BUSY))
131 mask |= TEGRA_MMC_PRNSTS_CMD_INHIBIT_DAT;
133 while (readl(&host->reg->prnsts) & mask) {
135 printf("%s: timeout error\n", __func__);
145 static int mmc_send_cmd_bounced(struct mmc *mmc, struct mmc_cmd *cmd,
146 struct mmc_data *data, struct bounce_buffer *bbstate)
148 struct mmc_host *host = (struct mmc_host *)mmc->priv;
151 unsigned int mask = 0;
152 unsigned int retry = 0x100000;
153 debug(" mmc_send_cmd called\n");
155 result = mmc_wait_inhibit(host, cmd, data, 10 /* ms */);
161 mmc_prepare_data(host, data, bbstate);
163 debug("cmd->arg: %08x\n", cmd->cmdarg);
164 writel(cmd->cmdarg, &host->reg->argument);
167 mmc_set_transfer_mode(host, data);
169 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
174 * CMDIDX[13:8] : Command index
175 * DATAPRNT[5] : Data Present Select
176 * ENCMDIDX[4] : Command Index Check Enable
177 * ENCMDCRC[3] : Command CRC Check Enable
182 * 11 = Length 48 Check busy after response
184 if (!(cmd->resp_type & MMC_RSP_PRESENT))
185 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_NO_RESPONSE;
186 else if (cmd->resp_type & MMC_RSP_136)
187 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_136;
188 else if (cmd->resp_type & MMC_RSP_BUSY)
189 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48_BUSY;
191 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48;
193 if (cmd->resp_type & MMC_RSP_CRC)
194 flags |= TEGRA_MMC_TRNMOD_CMD_CRC_CHECK;
195 if (cmd->resp_type & MMC_RSP_OPCODE)
196 flags |= TEGRA_MMC_TRNMOD_CMD_INDEX_CHECK;
198 flags |= TEGRA_MMC_TRNMOD_DATA_PRESENT_SELECT_DATA_TRANSFER;
200 debug("cmd: %d\n", cmd->cmdidx);
202 writew((cmd->cmdidx << 8) | flags, &host->reg->cmdreg);
204 for (i = 0; i < retry; i++) {
205 mask = readl(&host->reg->norintsts);
206 /* Command Complete */
207 if (mask & TEGRA_MMC_NORINTSTS_CMD_COMPLETE) {
209 writel(mask, &host->reg->norintsts);
215 printf("%s: waiting for status update\n", __func__);
216 writel(mask, &host->reg->norintsts);
220 if (mask & TEGRA_MMC_NORINTSTS_CMD_TIMEOUT) {
222 debug("timeout: %08x cmd %d\n", mask, cmd->cmdidx);
223 writel(mask, &host->reg->norintsts);
225 } else if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
226 /* Error Interrupt */
227 debug("error: %08x cmd %d\n", mask, cmd->cmdidx);
228 writel(mask, &host->reg->norintsts);
232 if (cmd->resp_type & MMC_RSP_PRESENT) {
233 if (cmd->resp_type & MMC_RSP_136) {
234 /* CRC is stripped so we need to do some shifting. */
235 for (i = 0; i < 4; i++) {
236 unsigned int offset =
237 (unsigned int)(&host->reg->rspreg3 - i);
238 cmd->response[i] = readl(offset) << 8;
244 debug("cmd->resp[%d]: %08x\n",
245 i, cmd->response[i]);
247 } else if (cmd->resp_type & MMC_RSP_BUSY) {
248 for (i = 0; i < retry; i++) {
249 /* PRNTDATA[23:20] : DAT[3:0] Line Signal */
250 if (readl(&host->reg->prnsts)
251 & (1 << 20)) /* DAT[0] */
256 printf("%s: card is still busy\n", __func__);
257 writel(mask, &host->reg->norintsts);
261 cmd->response[0] = readl(&host->reg->rspreg0);
262 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
264 cmd->response[0] = readl(&host->reg->rspreg0);
265 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
270 unsigned long start = get_timer(0);
273 mask = readl(&host->reg->norintsts);
275 if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
276 /* Error Interrupt */
277 writel(mask, &host->reg->norintsts);
278 printf("%s: error during transfer: 0x%08x\n",
281 } else if (mask & TEGRA_MMC_NORINTSTS_DMA_INTERRUPT) {
283 * DMA Interrupt, restart the transfer where
284 * it was interrupted.
286 unsigned int address = readl(&host->reg->sysad);
289 writel(TEGRA_MMC_NORINTSTS_DMA_INTERRUPT,
290 &host->reg->norintsts);
291 writel(address, &host->reg->sysad);
292 } else if (mask & TEGRA_MMC_NORINTSTS_XFER_COMPLETE) {
293 /* Transfer Complete */
294 debug("r/w is done\n");
296 } else if (get_timer(start) > 2000UL) {
297 writel(mask, &host->reg->norintsts);
298 printf("%s: MMC Timeout\n"
299 " Interrupt status 0x%08x\n"
300 " Interrupt status enable 0x%08x\n"
301 " Interrupt signal enable 0x%08x\n"
302 " Present status 0x%08x\n",
304 readl(&host->reg->norintstsen),
305 readl(&host->reg->norintsigen),
306 readl(&host->reg->prnsts));
310 writel(mask, &host->reg->norintsts);
317 static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
318 struct mmc_data *data)
321 unsigned int bbflags;
323 struct bounce_buffer bbstate;
327 if (data->flags & MMC_DATA_READ) {
329 bbflags = GEN_BB_WRITE;
331 buf = (void *)data->src;
332 bbflags = GEN_BB_READ;
334 len = data->blocks * data->blocksize;
336 bounce_buffer_start(&bbstate, buf, len, bbflags);
339 ret = mmc_send_cmd_bounced(mmc, cmd, data, &bbstate);
342 bounce_buffer_stop(&bbstate);
347 static void mmc_change_clock(struct mmc_host *host, uint clock)
351 unsigned long timeout;
353 debug(" mmc_change_clock called\n");
356 * Change Tegra SDMMCx clock divisor here. Source is PLLP_OUT0
360 clock_adjust_periph_pll_div(host->mmc_id, CLOCK_ID_PERIPH, clock,
362 debug("div = %d\n", div);
364 writew(0, &host->reg->clkcon);
368 * SELFREQ[15:8] : base clock divided by value
369 * ENSDCLK[2] : SD Clock Enable
370 * STBLINTCLK[1] : Internal Clock Stable
371 * ENINTCLK[0] : Internal Clock Enable
374 clk = ((div << TEGRA_MMC_CLKCON_SDCLK_FREQ_SEL_SHIFT) |
375 TEGRA_MMC_CLKCON_INTERNAL_CLOCK_ENABLE);
376 writew(clk, &host->reg->clkcon);
380 while (!(readw(&host->reg->clkcon) &
381 TEGRA_MMC_CLKCON_INTERNAL_CLOCK_STABLE)) {
383 printf("%s: timeout error\n", __func__);
390 clk |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE;
391 writew(clk, &host->reg->clkcon);
393 debug("mmc_change_clock: clkcon = %08X\n", clk);
399 static void mmc_set_ios(struct mmc *mmc)
401 struct mmc_host *host = mmc->priv;
403 debug(" mmc_set_ios called\n");
405 debug("bus_width: %x, clock: %d\n", mmc->bus_width, mmc->clock);
407 /* Change clock first */
408 mmc_change_clock(host, mmc->clock);
410 ctrl = readb(&host->reg->hostctl);
414 * 0 = Depend on WIDE4
420 if (mmc->bus_width == 8)
422 else if (mmc->bus_width == 4)
427 writeb(ctrl, &host->reg->hostctl);
428 debug("mmc_set_ios: hostctl = %08X\n", ctrl);
431 static void mmc_reset(struct mmc_host *host, struct mmc *mmc)
433 unsigned int timeout;
434 debug(" mmc_reset called\n");
437 * RSTALL[0] : Software reset for all
441 writeb(TEGRA_MMC_SWRST_SW_RESET_FOR_ALL, &host->reg->swrst);
445 /* Wait max 100 ms */
448 /* hw clears the bit when it's done */
449 while (readb(&host->reg->swrst) & TEGRA_MMC_SWRST_SW_RESET_FOR_ALL) {
451 printf("%s: timeout error\n", __func__);
458 /* Set SD bus voltage & enable bus power */
459 mmc_set_power(host, fls(mmc->voltages) - 1);
460 debug("%s: power control = %02X, host control = %02X\n", __func__,
461 readb(&host->reg->pwrcon), readb(&host->reg->hostctl));
463 /* Make sure SDIO pads are set up */
467 static int mmc_core_init(struct mmc *mmc)
469 struct mmc_host *host = (struct mmc_host *)mmc->priv;
471 debug(" mmc_core_init called\n");
473 mmc_reset(host, mmc);
475 host->version = readw(&host->reg->hcver);
476 debug("host version = %x\n", host->version);
479 writel(0xffffffff, &host->reg->norintstsen);
480 writel(0xffffffff, &host->reg->norintsigen);
482 writeb(0xe, &host->reg->timeoutcon); /* TMCLK * 2^27 */
484 * NORMAL Interrupt Status Enable Register init
485 * [5] ENSTABUFRDRDY : Buffer Read Ready Status Enable
486 * [4] ENSTABUFWTRDY : Buffer write Ready Status Enable
487 * [3] ENSTADMAINT : DMA boundary interrupt
488 * [1] ENSTASTANSCMPLT : Transfre Complete Status Enable
489 * [0] ENSTACMDCMPLT : Command Complete Status Enable
491 mask = readl(&host->reg->norintstsen);
493 mask |= (TEGRA_MMC_NORINTSTSEN_CMD_COMPLETE |
494 TEGRA_MMC_NORINTSTSEN_XFER_COMPLETE |
495 TEGRA_MMC_NORINTSTSEN_DMA_INTERRUPT |
496 TEGRA_MMC_NORINTSTSEN_BUFFER_WRITE_READY |
497 TEGRA_MMC_NORINTSTSEN_BUFFER_READ_READY);
498 writel(mask, &host->reg->norintstsen);
501 * NORMAL Interrupt Signal Enable Register init
502 * [1] ENSTACMDCMPLT : Transfer Complete Signal Enable
504 mask = readl(&host->reg->norintsigen);
506 mask |= TEGRA_MMC_NORINTSIGEN_XFER_COMPLETE;
507 writel(mask, &host->reg->norintsigen);
512 int tegra_mmc_getcd(struct mmc *mmc)
514 struct mmc_host *host = (struct mmc_host *)mmc->priv;
516 debug("tegra_mmc_getcd called\n");
518 if (fdt_gpio_isvalid(&host->cd_gpio))
519 return fdtdec_get_gpio(&host->cd_gpio);
524 static int do_mmc_init(int dev_index)
526 struct mmc_host *host;
527 char gpusage[12]; /* "SD/MMCn PWR" or "SD/MMCn CD" */
530 /* DT should have been read & host config filled in */
531 host = &mmc_host[dev_index];
535 debug(" do_mmc_init: index %d, bus width %d "
536 "pwr_gpio %d cd_gpio %d\n",
537 dev_index, host->width,
538 host->pwr_gpio.gpio, host->cd_gpio.gpio);
541 clock_start_periph_pll(host->mmc_id, CLOCK_ID_PERIPH, 20000000);
543 if (fdt_gpio_isvalid(&host->pwr_gpio)) {
544 sprintf(gpusage, "SD/MMC%d PWR", dev_index);
545 gpio_request(host->pwr_gpio.gpio, gpusage);
546 gpio_direction_output(host->pwr_gpio.gpio, 1);
547 debug(" Power GPIO name = %s\n", host->pwr_gpio.name);
550 if (fdt_gpio_isvalid(&host->cd_gpio)) {
551 sprintf(gpusage, "SD/MMC%d CD", dev_index);
552 gpio_request(host->cd_gpio.gpio, gpusage);
553 gpio_direction_input(host->cd_gpio.gpio);
554 debug(" CD GPIO name = %s\n", host->cd_gpio.name);
557 mmc = &mmc_dev[dev_index];
559 sprintf(mmc->name, "Tegra SD/MMC");
561 mmc->send_cmd = mmc_send_cmd;
562 mmc->set_ios = mmc_set_ios;
563 mmc->init = mmc_core_init;
564 mmc->getcd = tegra_mmc_getcd;
567 mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
569 if (host->width == 8)
570 mmc->host_caps |= MMC_MODE_8BIT;
571 if (host->width >= 4)
572 mmc->host_caps |= MMC_MODE_4BIT;
573 mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS | MMC_MODE_HC;
576 * min freq is for card identification, and is the highest
577 * low-speed SDIO card frequency (actually 400KHz)
578 * max freq is highest HS eMMC clock as per the SD/MMC spec
582 mmc->f_max = 48000000;
590 * Get the host address and peripheral ID for a node.
592 * @param blob fdt blob
593 * @param node Device index (0-3)
594 * @param host Structure to fill in (reg, width, mmc_id)
596 static int mmc_get_config(const void *blob, int node, struct mmc_host *host)
598 debug("%s: node = %d\n", __func__, node);
600 host->enabled = fdtdec_get_is_enabled(blob, node);
602 host->reg = (struct tegra_mmc *)fdtdec_get_addr(blob, node, "reg");
603 if ((fdt_addr_t)host->reg == FDT_ADDR_T_NONE) {
604 debug("%s: no sdmmc base reg info found\n", __func__);
605 return -FDT_ERR_NOTFOUND;
608 host->mmc_id = clock_decode_periph_id(blob, node);
609 if (host->mmc_id == PERIPH_ID_NONE) {
610 debug("%s: could not decode periph id\n", __func__);
611 return -FDT_ERR_NOTFOUND;
615 * NOTE: mmc->bus_width is determined by mmc.c dynamically.
616 * TBD: Override it with this value?
618 host->width = fdtdec_get_int(blob, node, "bus-width", 0);
620 debug("%s: no sdmmc width found\n", __func__);
622 /* These GPIOs are optional */
623 fdtdec_decode_gpio(blob, node, "cd-gpios", &host->cd_gpio);
624 fdtdec_decode_gpio(blob, node, "wp-gpios", &host->wp_gpio);
625 fdtdec_decode_gpio(blob, node, "power-gpios", &host->pwr_gpio);
627 debug("%s: found controller at %p, width = %d, periph_id = %d\n",
628 __func__, host->reg, host->width, host->mmc_id);
633 * Process a list of nodes, adding them to our list of SDMMC ports.
635 * @param blob fdt blob
636 * @param node_list list of nodes to process (any <=0 are ignored)
637 * @param count number of nodes to process
638 * @return 0 if ok, -1 on error
640 static int process_nodes(const void *blob, int node_list[], int count)
642 struct mmc_host *host;
645 debug("%s: count = %d\n", __func__, count);
647 /* build mmc_host[] for each controller */
648 for (i = 0; i < count; i++) {
656 if (mmc_get_config(blob, node, host)) {
657 printf("%s: failed to decode dev %d\n", __func__, i);
665 void tegra_mmc_init(void)
667 int node_list[MAX_HOSTS], count;
668 const void *blob = gd->fdt_blob;
669 debug("%s entry\n", __func__);
671 /* See if any Tegra30 MMC controllers are present */
672 count = fdtdec_find_aliases_for_id(blob, "sdhci",
673 COMPAT_NVIDIA_TEGRA30_SDMMC, node_list, MAX_HOSTS);
674 debug("%s: count of T30 sdhci nodes is %d\n", __func__, count);
675 if (process_nodes(blob, node_list, count)) {
676 printf("%s: Error processing T30 mmc node(s)!\n", __func__);
680 /* Now look for any Tegra20 MMC controllers */
681 count = fdtdec_find_aliases_for_id(blob, "sdhci",
682 COMPAT_NVIDIA_TEGRA20_SDMMC, node_list, MAX_HOSTS);
683 debug("%s: count of T20 sdhci nodes is %d\n", __func__, count);
684 if (process_nodes(blob, node_list, count)) {
685 printf("%s: Error processing T20 mmc node(s)!\n", __func__);