1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2012 SAMSUNG Electronics
18 #include <asm/cache.h>
19 #include <linux/delay.h>
20 #include <power/regulator.h>
22 #define PAGE_SIZE 4096
24 static int dwmci_wait_reset(struct dwmci_host *host, u32 value)
26 unsigned long timeout = 1000;
29 dwmci_writel(host, DWMCI_CTRL, value);
32 ctrl = dwmci_readl(host, DWMCI_CTRL);
33 if (!(ctrl & DWMCI_RESET_ALL))
39 static void dwmci_set_idma_desc(struct dwmci_idmac *idmac,
40 u32 desc0, u32 desc1, u32 desc2)
42 struct dwmci_idmac *desc = idmac;
47 desc->next_addr = (ulong)desc + sizeof(struct dwmci_idmac);
50 static void dwmci_prepare_data(struct dwmci_host *host,
51 struct mmc_data *data,
52 struct dwmci_idmac *cur_idmac,
56 unsigned int i = 0, flags, cnt, blk_cnt;
57 ulong data_start, data_end;
60 blk_cnt = data->blocks;
62 dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
64 /* Clear IDMAC interrupt */
65 dwmci_writel(host, DWMCI_IDSTS, 0xFFFFFFFF);
67 data_start = (ulong)cur_idmac;
68 dwmci_writel(host, DWMCI_DBADDR, (ulong)cur_idmac);
71 flags = DWMCI_IDMAC_OWN | DWMCI_IDMAC_CH ;
72 flags |= (i == 0) ? DWMCI_IDMAC_FS : 0;
74 flags |= DWMCI_IDMAC_LD;
75 cnt = data->blocksize * blk_cnt;
77 cnt = data->blocksize * 8;
79 dwmci_set_idma_desc(cur_idmac, flags, cnt,
80 (ulong)bounce_buffer + (i * PAGE_SIZE));
89 data_end = (ulong)cur_idmac;
90 flush_dcache_range(data_start, roundup(data_end, ARCH_DMA_MINALIGN));
92 ctrl = dwmci_readl(host, DWMCI_CTRL);
93 ctrl |= DWMCI_IDMAC_EN | DWMCI_DMA_EN;
94 dwmci_writel(host, DWMCI_CTRL, ctrl);
96 ctrl = dwmci_readl(host, DWMCI_BMOD);
97 ctrl |= DWMCI_BMOD_IDMAC_FB | DWMCI_BMOD_IDMAC_EN;
98 dwmci_writel(host, DWMCI_BMOD, ctrl);
100 dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
101 dwmci_writel(host, DWMCI_BYTCNT, data->blocksize * data->blocks);
104 static int dwmci_fifo_ready(struct dwmci_host *host, u32 bit, u32 *len)
108 *len = dwmci_readl(host, DWMCI_STATUS);
109 while (--timeout && (*len & bit)) {
111 *len = dwmci_readl(host, DWMCI_STATUS);
115 debug("%s: FIFO underflow timeout\n", __func__);
122 static unsigned int dwmci_get_timeout(struct mmc *mmc, const unsigned int size)
124 unsigned int timeout;
126 timeout = size * 8; /* counting in bits */
127 timeout *= 10; /* wait 10 times as long */
128 timeout /= mmc->clock;
129 timeout /= mmc->bus_width;
130 timeout /= mmc->ddr_mode ? 2 : 1;
131 timeout *= 1000; /* counting in msec */
132 timeout = (timeout < 1000) ? 1000 : timeout;
137 static int dwmci_data_transfer(struct dwmci_host *host, struct mmc_data *data)
139 struct mmc *mmc = host->mmc;
141 u32 timeout, mask, size, i, len = 0;
143 ulong start = get_timer(0);
144 u32 fifo_depth = (((host->fifoth_val & RX_WMARK_MASK) >>
145 RX_WMARK_SHIFT) + 1) * 2;
147 size = data->blocksize * data->blocks;
148 if (data->flags == MMC_DATA_READ)
149 buf = (unsigned int *)data->dest;
151 buf = (unsigned int *)data->src;
153 timeout = dwmci_get_timeout(mmc, size);
158 mask = dwmci_readl(host, DWMCI_RINTSTS);
159 /* Error during data transfer. */
160 if (mask & (DWMCI_DATA_ERR | DWMCI_DATA_TOUT)) {
161 debug("%s: DATA ERROR!\n", __func__);
166 if (host->fifo_mode && size) {
168 if (data->flags == MMC_DATA_READ &&
169 (mask & (DWMCI_INTMSK_RXDR | DWMCI_INTMSK_DTO))) {
170 dwmci_writel(host, DWMCI_RINTSTS,
171 DWMCI_INTMSK_RXDR | DWMCI_INTMSK_DTO);
173 ret = dwmci_fifo_ready(host,
179 len = (len >> DWMCI_FIFO_SHIFT) &
181 len = min(size, len);
182 for (i = 0; i < len; i++)
184 dwmci_readl(host, DWMCI_DATA);
185 size = size > len ? (size - len) : 0;
187 } else if (data->flags == MMC_DATA_WRITE &&
188 (mask & DWMCI_INTMSK_TXDR)) {
190 ret = dwmci_fifo_ready(host,
196 len = fifo_depth - ((len >>
199 len = min(size, len);
200 for (i = 0; i < len; i++)
201 dwmci_writel(host, DWMCI_DATA,
203 size = size > len ? (size - len) : 0;
205 dwmci_writel(host, DWMCI_RINTSTS,
210 /* Data arrived correctly. */
211 if (mask & DWMCI_INTMSK_DTO) {
216 /* Check for timeout. */
217 if (get_timer(start) > timeout) {
218 debug("%s: Timeout waiting for data!\n",
225 dwmci_writel(host, DWMCI_RINTSTS, mask);
230 static int dwmci_set_transfer_mode(struct dwmci_host *host,
231 struct mmc_data *data)
235 mode = DWMCI_CMD_DATA_EXP;
236 if (data->flags & MMC_DATA_WRITE)
237 mode |= DWMCI_CMD_RW;
243 static int dwmci_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
244 struct mmc_data *data)
246 struct mmc *mmc = mmc_get_mmc_dev(dev);
248 static int dwmci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
249 struct mmc_data *data)
252 struct dwmci_host *host = mmc->priv;
253 ALLOC_CACHE_ALIGN_BUFFER(struct dwmci_idmac, cur_idmac,
254 data ? DIV_ROUND_UP(data->blocks, 8) : 0);
255 int ret = 0, flags = 0, i;
256 unsigned int timeout = 500;
259 ulong start = get_timer(0);
260 struct bounce_buffer bbstate;
262 while (dwmci_readl(host, DWMCI_STATUS) & DWMCI_BUSY) {
263 if (get_timer(start) > timeout) {
264 debug("%s: Timeout on data busy\n", __func__);
269 dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_ALL);
272 if (host->fifo_mode) {
273 dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
274 dwmci_writel(host, DWMCI_BYTCNT,
275 data->blocksize * data->blocks);
276 dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
278 if (data->flags == MMC_DATA_READ) {
279 ret = bounce_buffer_start(&bbstate,
282 data->blocks, GEN_BB_WRITE);
284 ret = bounce_buffer_start(&bbstate,
287 data->blocks, GEN_BB_READ);
293 dwmci_prepare_data(host, data, cur_idmac,
294 bbstate.bounce_buffer);
298 dwmci_writel(host, DWMCI_CMDARG, cmd->cmdarg);
301 flags = dwmci_set_transfer_mode(host, data);
303 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
306 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
307 flags |= DWMCI_CMD_ABORT_STOP;
309 flags |= DWMCI_CMD_PRV_DAT_WAIT;
311 if (cmd->resp_type & MMC_RSP_PRESENT) {
312 flags |= DWMCI_CMD_RESP_EXP;
313 if (cmd->resp_type & MMC_RSP_136)
314 flags |= DWMCI_CMD_RESP_LENGTH;
317 if (cmd->resp_type & MMC_RSP_CRC)
318 flags |= DWMCI_CMD_CHECK_CRC;
320 flags |= (cmd->cmdidx | DWMCI_CMD_START | DWMCI_CMD_USE_HOLD_REG);
322 debug("Sending CMD%d\n",cmd->cmdidx);
324 dwmci_writel(host, DWMCI_CMD, flags);
326 for (i = 0; i < retry; i++) {
327 mask = dwmci_readl(host, DWMCI_RINTSTS);
328 if (mask & DWMCI_INTMSK_CDONE) {
330 dwmci_writel(host, DWMCI_RINTSTS, mask);
336 debug("%s: Timeout.\n", __func__);
340 if (mask & DWMCI_INTMSK_RTO) {
342 * Timeout here is not necessarily fatal. (e)MMC cards
343 * will splat here when they receive CMD55 as they do
344 * not support this command and that is exactly the way
345 * to tell them apart from SD cards. Thus, this output
346 * below shall be debug(). eMMC cards also do not favor
347 * CMD8, please keep that in mind.
349 debug("%s: Response Timeout.\n", __func__);
351 } else if (mask & DWMCI_INTMSK_RE) {
352 debug("%s: Response Error.\n", __func__);
354 } else if ((cmd->resp_type & MMC_RSP_CRC) &&
355 (mask & DWMCI_INTMSK_RCRC)) {
356 debug("%s: Response CRC Error.\n", __func__);
361 if (cmd->resp_type & MMC_RSP_PRESENT) {
362 if (cmd->resp_type & MMC_RSP_136) {
363 cmd->response[0] = dwmci_readl(host, DWMCI_RESP3);
364 cmd->response[1] = dwmci_readl(host, DWMCI_RESP2);
365 cmd->response[2] = dwmci_readl(host, DWMCI_RESP1);
366 cmd->response[3] = dwmci_readl(host, DWMCI_RESP0);
368 cmd->response[0] = dwmci_readl(host, DWMCI_RESP0);
373 ret = dwmci_data_transfer(host, data);
375 /* only dma mode need it */
376 if (!host->fifo_mode) {
377 if (data->flags == MMC_DATA_READ)
378 mask = DWMCI_IDINTEN_RI;
380 mask = DWMCI_IDINTEN_TI;
381 ret = wait_for_bit_le32(host->ioaddr + DWMCI_IDSTS,
382 mask, true, 1000, false);
384 debug("%s: DWMCI_IDINTEN mask 0x%x timeout.\n",
386 /* clear interrupts */
387 dwmci_writel(host, DWMCI_IDSTS, DWMCI_IDINTEN_MASK);
389 ctrl = dwmci_readl(host, DWMCI_CTRL);
390 ctrl &= ~(DWMCI_DMA_EN);
391 dwmci_writel(host, DWMCI_CTRL, ctrl);
392 bounce_buffer_stop(&bbstate);
401 static int dwmci_setup_bus(struct dwmci_host *host, u32 freq)
407 if ((freq == host->clock) || (freq == 0))
410 * If host->get_mmc_clk isn't defined,
411 * then assume that host->bus_hz is source clock value.
412 * host->bus_hz should be set by user.
414 if (host->get_mmc_clk)
415 sclk = host->get_mmc_clk(host, freq);
416 else if (host->bus_hz)
419 debug("%s: Didn't get source clock value.\n", __func__);
424 div = 0; /* bypass mode */
426 div = DIV_ROUND_UP(sclk, 2 * freq);
428 dwmci_writel(host, DWMCI_CLKENA, 0);
429 dwmci_writel(host, DWMCI_CLKSRC, 0);
431 dwmci_writel(host, DWMCI_CLKDIV, div);
432 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
433 DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
436 status = dwmci_readl(host, DWMCI_CMD);
438 debug("%s: Timeout!\n", __func__);
441 } while (status & DWMCI_CMD_START);
443 dwmci_writel(host, DWMCI_CLKENA, DWMCI_CLKEN_ENABLE |
444 DWMCI_CLKEN_LOW_PWR);
446 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
447 DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
451 status = dwmci_readl(host, DWMCI_CMD);
453 debug("%s: Timeout!\n", __func__);
456 } while (status & DWMCI_CMD_START);
464 static int dwmci_set_ios(struct udevice *dev)
466 struct mmc *mmc = mmc_get_mmc_dev(dev);
468 static int dwmci_set_ios(struct mmc *mmc)
471 struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
474 debug("Buswidth = %d, clock: %d\n", mmc->bus_width, mmc->clock);
476 dwmci_setup_bus(host, mmc->clock);
477 switch (mmc->bus_width) {
479 ctype = DWMCI_CTYPE_8BIT;
482 ctype = DWMCI_CTYPE_4BIT;
485 ctype = DWMCI_CTYPE_1BIT;
489 dwmci_writel(host, DWMCI_CTYPE, ctype);
491 regs = dwmci_readl(host, DWMCI_UHS_REG);
493 regs |= DWMCI_DDR_MODE;
495 regs &= ~DWMCI_DDR_MODE;
497 dwmci_writel(host, DWMCI_UHS_REG, regs);
502 ret = host->clksel(host);
507 #if CONFIG_IS_ENABLED(DM_REGULATOR)
508 if (mmc->vqmmc_supply) {
511 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
512 regulator_set_value(mmc->vqmmc_supply, 1800000);
514 regulator_set_value(mmc->vqmmc_supply, 3300000);
516 ret = regulator_set_enable_if_allowed(mmc->vqmmc_supply, true);
525 static int dwmci_init(struct mmc *mmc)
527 struct dwmci_host *host = mmc->priv;
529 if (host->board_init)
530 host->board_init(host);
532 dwmci_writel(host, DWMCI_PWREN, 1);
534 if (!dwmci_wait_reset(host, DWMCI_RESET_ALL)) {
535 debug("%s[%d] Fail-reset!!\n", __func__, __LINE__);
539 /* Enumerate at 400KHz */
540 dwmci_setup_bus(host, mmc->cfg->f_min);
542 dwmci_writel(host, DWMCI_RINTSTS, 0xFFFFFFFF);
543 dwmci_writel(host, DWMCI_INTMASK, 0);
545 dwmci_writel(host, DWMCI_TMOUT, 0xFFFFFFFF);
547 dwmci_writel(host, DWMCI_IDINTEN, 0);
548 dwmci_writel(host, DWMCI_BMOD, 1);
550 if (!host->fifoth_val) {
553 fifo_size = dwmci_readl(host, DWMCI_FIFOTH);
554 fifo_size = ((fifo_size & RX_WMARK_MASK) >> RX_WMARK_SHIFT) + 1;
555 host->fifoth_val = MSIZE(0x2) | RX_WMARK(fifo_size / 2 - 1) |
556 TX_WMARK(fifo_size / 2);
558 dwmci_writel(host, DWMCI_FIFOTH, host->fifoth_val);
560 dwmci_writel(host, DWMCI_CLKENA, 0);
561 dwmci_writel(host, DWMCI_CLKSRC, 0);
563 if (!host->fifo_mode)
564 dwmci_writel(host, DWMCI_IDINTEN, DWMCI_IDINTEN_MASK);
570 int dwmci_probe(struct udevice *dev)
572 struct mmc *mmc = mmc_get_mmc_dev(dev);
574 return dwmci_init(mmc);
577 const struct dm_mmc_ops dm_dwmci_ops = {
578 .send_cmd = dwmci_send_cmd,
579 .set_ios = dwmci_set_ios,
583 static const struct mmc_ops dwmci_ops = {
584 .send_cmd = dwmci_send_cmd,
585 .set_ios = dwmci_set_ios,
590 void dwmci_setup_cfg(struct mmc_config *cfg, struct dwmci_host *host,
591 u32 max_clk, u32 min_clk)
593 cfg->name = host->name;
594 #ifndef CONFIG_DM_MMC
595 cfg->ops = &dwmci_ops;
597 cfg->f_min = min_clk;
598 cfg->f_max = max_clk;
600 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
602 cfg->host_caps = host->caps;
604 if (host->buswidth == 8) {
605 cfg->host_caps |= MMC_MODE_8BIT;
606 cfg->host_caps &= ~MMC_MODE_4BIT;
608 cfg->host_caps |= MMC_MODE_4BIT;
609 cfg->host_caps &= ~MMC_MODE_8BIT;
611 cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz;
613 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
617 int dwmci_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg)
619 return mmc_bind(dev, mmc, cfg);
622 int add_dwmci(struct dwmci_host *host, u32 max_clk, u32 min_clk)
624 dwmci_setup_cfg(&host->cfg, host, max_clk, min_clk);
626 host->mmc = mmc_create(&host->cfg, host);
627 if (host->mmc == NULL)