1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2012 SAMSUNG Electronics
4 * Jaehoon Chung <jh80.chung@samsung.com>
5 * Rajeshawari Shinde <rajeshwari.s@samsung.com>
16 #define PAGE_SIZE 4096
18 static int dwmci_wait_reset(struct dwmci_host *host, u32 value)
20 unsigned long timeout = 1000;
23 dwmci_writel(host, DWMCI_CTRL, value);
26 ctrl = dwmci_readl(host, DWMCI_CTRL);
27 if (!(ctrl & DWMCI_RESET_ALL))
33 static void dwmci_set_idma_desc(struct dwmci_idmac *idmac,
34 u32 desc0, u32 desc1, u32 desc2)
36 struct dwmci_idmac *desc = idmac;
41 desc->next_addr = (ulong)desc + sizeof(struct dwmci_idmac);
44 static void dwmci_prepare_data(struct dwmci_host *host,
45 struct mmc_data *data,
46 struct dwmci_idmac *cur_idmac,
50 unsigned int i = 0, flags, cnt, blk_cnt;
51 ulong data_start, data_end;
54 blk_cnt = data->blocks;
56 dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
58 data_start = (ulong)cur_idmac;
59 dwmci_writel(host, DWMCI_DBADDR, (ulong)cur_idmac);
62 flags = DWMCI_IDMAC_OWN | DWMCI_IDMAC_CH ;
63 flags |= (i == 0) ? DWMCI_IDMAC_FS : 0;
65 flags |= DWMCI_IDMAC_LD;
66 cnt = data->blocksize * blk_cnt;
68 cnt = data->blocksize * 8;
70 dwmci_set_idma_desc(cur_idmac, flags, cnt,
71 (ulong)bounce_buffer + (i * PAGE_SIZE));
80 data_end = (ulong)cur_idmac;
81 flush_dcache_range(data_start, data_end + ARCH_DMA_MINALIGN);
83 ctrl = dwmci_readl(host, DWMCI_CTRL);
84 ctrl |= DWMCI_IDMAC_EN | DWMCI_DMA_EN;
85 dwmci_writel(host, DWMCI_CTRL, ctrl);
87 ctrl = dwmci_readl(host, DWMCI_BMOD);
88 ctrl |= DWMCI_BMOD_IDMAC_FB | DWMCI_BMOD_IDMAC_EN;
89 dwmci_writel(host, DWMCI_BMOD, ctrl);
91 dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
92 dwmci_writel(host, DWMCI_BYTCNT, data->blocksize * data->blocks);
95 static int dwmci_data_transfer(struct dwmci_host *host, struct mmc_data *data)
99 u32 mask, size, i, len = 0;
101 ulong start = get_timer(0);
102 u32 fifo_depth = (((host->fifoth_val & RX_WMARK_MASK) >>
103 RX_WMARK_SHIFT) + 1) * 2;
105 size = data->blocksize * data->blocks / 4;
106 if (data->flags == MMC_DATA_READ)
107 buf = (unsigned int *)data->dest;
109 buf = (unsigned int *)data->src;
112 mask = dwmci_readl(host, DWMCI_RINTSTS);
113 /* Error during data transfer. */
114 if (mask & (DWMCI_DATA_ERR | DWMCI_DATA_TOUT)) {
115 debug("%s: DATA ERROR!\n", __func__);
120 if (host->fifo_mode && size) {
122 if (data->flags == MMC_DATA_READ &&
123 (mask & DWMCI_INTMSK_RXDR)) {
125 len = dwmci_readl(host, DWMCI_STATUS);
126 len = (len >> DWMCI_FIFO_SHIFT) &
128 len = min(size, len);
129 for (i = 0; i < len; i++)
131 dwmci_readl(host, DWMCI_DATA);
132 size = size > len ? (size - len) : 0;
134 dwmci_writel(host, DWMCI_RINTSTS,
136 } else if (data->flags == MMC_DATA_WRITE &&
137 (mask & DWMCI_INTMSK_TXDR)) {
139 len = dwmci_readl(host, DWMCI_STATUS);
140 len = fifo_depth - ((len >>
143 len = min(size, len);
144 for (i = 0; i < len; i++)
145 dwmci_writel(host, DWMCI_DATA,
147 size = size > len ? (size - len) : 0;
149 dwmci_writel(host, DWMCI_RINTSTS,
154 /* Data arrived correctly. */
155 if (mask & DWMCI_INTMSK_DTO) {
160 /* Check for timeout. */
161 if (get_timer(start) > timeout) {
162 debug("%s: Timeout waiting for data!\n",
169 dwmci_writel(host, DWMCI_RINTSTS, mask);
174 static int dwmci_set_transfer_mode(struct dwmci_host *host,
175 struct mmc_data *data)
179 mode = DWMCI_CMD_DATA_EXP;
180 if (data->flags & MMC_DATA_WRITE)
181 mode |= DWMCI_CMD_RW;
187 static int dwmci_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
188 struct mmc_data *data)
190 struct mmc *mmc = mmc_get_mmc_dev(dev);
192 static int dwmci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
193 struct mmc_data *data)
196 struct dwmci_host *host = mmc->priv;
197 ALLOC_CACHE_ALIGN_BUFFER(struct dwmci_idmac, cur_idmac,
198 data ? DIV_ROUND_UP(data->blocks, 8) : 0);
199 int ret = 0, flags = 0, i;
200 unsigned int timeout = 500;
203 ulong start = get_timer(0);
204 struct bounce_buffer bbstate;
206 while (dwmci_readl(host, DWMCI_STATUS) & DWMCI_BUSY) {
207 if (get_timer(start) > timeout) {
208 debug("%s: Timeout on data busy\n", __func__);
213 dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_ALL);
216 if (host->fifo_mode) {
217 dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
218 dwmci_writel(host, DWMCI_BYTCNT,
219 data->blocksize * data->blocks);
220 dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
222 if (data->flags == MMC_DATA_READ) {
223 bounce_buffer_start(&bbstate, (void*)data->dest,
225 data->blocks, GEN_BB_WRITE);
227 bounce_buffer_start(&bbstate, (void*)data->src,
229 data->blocks, GEN_BB_READ);
231 dwmci_prepare_data(host, data, cur_idmac,
232 bbstate.bounce_buffer);
236 dwmci_writel(host, DWMCI_CMDARG, cmd->cmdarg);
239 flags = dwmci_set_transfer_mode(host, data);
241 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
244 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
245 flags |= DWMCI_CMD_ABORT_STOP;
247 flags |= DWMCI_CMD_PRV_DAT_WAIT;
249 if (cmd->resp_type & MMC_RSP_PRESENT) {
250 flags |= DWMCI_CMD_RESP_EXP;
251 if (cmd->resp_type & MMC_RSP_136)
252 flags |= DWMCI_CMD_RESP_LENGTH;
255 if (cmd->resp_type & MMC_RSP_CRC)
256 flags |= DWMCI_CMD_CHECK_CRC;
258 flags |= (cmd->cmdidx | DWMCI_CMD_START | DWMCI_CMD_USE_HOLD_REG);
260 debug("Sending CMD%d\n",cmd->cmdidx);
262 dwmci_writel(host, DWMCI_CMD, flags);
264 for (i = 0; i < retry; i++) {
265 mask = dwmci_readl(host, DWMCI_RINTSTS);
266 if (mask & DWMCI_INTMSK_CDONE) {
268 dwmci_writel(host, DWMCI_RINTSTS, mask);
274 debug("%s: Timeout.\n", __func__);
278 if (mask & DWMCI_INTMSK_RTO) {
280 * Timeout here is not necessarily fatal. (e)MMC cards
281 * will splat here when they receive CMD55 as they do
282 * not support this command and that is exactly the way
283 * to tell them apart from SD cards. Thus, this output
284 * below shall be debug(). eMMC cards also do not favor
285 * CMD8, please keep that in mind.
287 debug("%s: Response Timeout.\n", __func__);
289 } else if (mask & DWMCI_INTMSK_RE) {
290 debug("%s: Response Error.\n", __func__);
295 if (cmd->resp_type & MMC_RSP_PRESENT) {
296 if (cmd->resp_type & MMC_RSP_136) {
297 cmd->response[0] = dwmci_readl(host, DWMCI_RESP3);
298 cmd->response[1] = dwmci_readl(host, DWMCI_RESP2);
299 cmd->response[2] = dwmci_readl(host, DWMCI_RESP1);
300 cmd->response[3] = dwmci_readl(host, DWMCI_RESP0);
302 cmd->response[0] = dwmci_readl(host, DWMCI_RESP0);
307 ret = dwmci_data_transfer(host, data);
309 /* only dma mode need it */
310 if (!host->fifo_mode) {
311 ctrl = dwmci_readl(host, DWMCI_CTRL);
312 ctrl &= ~(DWMCI_DMA_EN);
313 dwmci_writel(host, DWMCI_CTRL, ctrl);
314 bounce_buffer_stop(&bbstate);
323 static int dwmci_setup_bus(struct dwmci_host *host, u32 freq)
329 if ((freq == host->clock) || (freq == 0))
332 * If host->get_mmc_clk isn't defined,
333 * then assume that host->bus_hz is source clock value.
334 * host->bus_hz should be set by user.
336 if (host->get_mmc_clk)
337 sclk = host->get_mmc_clk(host, freq);
338 else if (host->bus_hz)
341 debug("%s: Didn't get source clock value.\n", __func__);
346 div = 0; /* bypass mode */
348 div = DIV_ROUND_UP(sclk, 2 * freq);
350 dwmci_writel(host, DWMCI_CLKENA, 0);
351 dwmci_writel(host, DWMCI_CLKSRC, 0);
353 dwmci_writel(host, DWMCI_CLKDIV, div);
354 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
355 DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
358 status = dwmci_readl(host, DWMCI_CMD);
360 debug("%s: Timeout!\n", __func__);
363 } while (status & DWMCI_CMD_START);
365 dwmci_writel(host, DWMCI_CLKENA, DWMCI_CLKEN_ENABLE |
366 DWMCI_CLKEN_LOW_PWR);
368 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
369 DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
373 status = dwmci_readl(host, DWMCI_CMD);
375 debug("%s: Timeout!\n", __func__);
378 } while (status & DWMCI_CMD_START);
386 static int dwmci_set_ios(struct udevice *dev)
388 struct mmc *mmc = mmc_get_mmc_dev(dev);
390 static int dwmci_set_ios(struct mmc *mmc)
393 struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
396 debug("Buswidth = %d, clock: %d\n", mmc->bus_width, mmc->clock);
398 dwmci_setup_bus(host, mmc->clock);
399 switch (mmc->bus_width) {
401 ctype = DWMCI_CTYPE_8BIT;
404 ctype = DWMCI_CTYPE_4BIT;
407 ctype = DWMCI_CTYPE_1BIT;
411 dwmci_writel(host, DWMCI_CTYPE, ctype);
413 regs = dwmci_readl(host, DWMCI_UHS_REG);
415 regs |= DWMCI_DDR_MODE;
417 regs &= ~DWMCI_DDR_MODE;
419 dwmci_writel(host, DWMCI_UHS_REG, regs);
427 static int dwmci_init(struct mmc *mmc)
429 struct dwmci_host *host = mmc->priv;
431 if (host->board_init)
432 host->board_init(host);
434 dwmci_writel(host, DWMCI_PWREN, 1);
436 if (!dwmci_wait_reset(host, DWMCI_RESET_ALL)) {
437 debug("%s[%d] Fail-reset!!\n", __func__, __LINE__);
441 /* Enumerate at 400KHz */
442 dwmci_setup_bus(host, mmc->cfg->f_min);
444 dwmci_writel(host, DWMCI_RINTSTS, 0xFFFFFFFF);
445 dwmci_writel(host, DWMCI_INTMASK, 0);
447 dwmci_writel(host, DWMCI_TMOUT, 0xFFFFFFFF);
449 dwmci_writel(host, DWMCI_IDINTEN, 0);
450 dwmci_writel(host, DWMCI_BMOD, 1);
452 if (!host->fifoth_val) {
455 fifo_size = dwmci_readl(host, DWMCI_FIFOTH);
456 fifo_size = ((fifo_size & RX_WMARK_MASK) >> RX_WMARK_SHIFT) + 1;
457 host->fifoth_val = MSIZE(0x2) | RX_WMARK(fifo_size / 2 - 1) |
458 TX_WMARK(fifo_size / 2);
460 dwmci_writel(host, DWMCI_FIFOTH, host->fifoth_val);
462 dwmci_writel(host, DWMCI_CLKENA, 0);
463 dwmci_writel(host, DWMCI_CLKSRC, 0);
469 int dwmci_probe(struct udevice *dev)
471 struct mmc *mmc = mmc_get_mmc_dev(dev);
473 return dwmci_init(mmc);
476 const struct dm_mmc_ops dm_dwmci_ops = {
477 .send_cmd = dwmci_send_cmd,
478 .set_ios = dwmci_set_ios,
482 static const struct mmc_ops dwmci_ops = {
483 .send_cmd = dwmci_send_cmd,
484 .set_ios = dwmci_set_ios,
489 void dwmci_setup_cfg(struct mmc_config *cfg, struct dwmci_host *host,
490 u32 max_clk, u32 min_clk)
492 cfg->name = host->name;
493 #ifndef CONFIG_DM_MMC
494 cfg->ops = &dwmci_ops;
496 cfg->f_min = min_clk;
497 cfg->f_max = max_clk;
499 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
501 cfg->host_caps = host->caps;
503 if (host->buswidth == 8) {
504 cfg->host_caps |= MMC_MODE_8BIT;
505 cfg->host_caps &= ~MMC_MODE_4BIT;
507 cfg->host_caps |= MMC_MODE_4BIT;
508 cfg->host_caps &= ~MMC_MODE_8BIT;
510 cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz;
512 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
516 int dwmci_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg)
518 return mmc_bind(dev, mmc, cfg);
521 int add_dwmci(struct dwmci_host *host, u32 max_clk, u32 min_clk)
523 dwmci_setup_cfg(&host->cfg, host, max_clk, min_clk);
525 host->mmc = mmc_create(&host->cfg, host);
526 if (host->mmc == NULL)