blob: ba93c2625586fdd3136c00ab5486d751a13c3f61 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jaehoon Chung757bff42012-10-15 19:10:29 +00002/*
3 * (C) Copyright 2012 SAMSUNG Electronics
4 * Jaehoon Chung <jh80.chung@samsung.com>
5 * Rajeshawari Shinde <rajeshwari.s@samsung.com>
Jaehoon Chung757bff42012-10-15 19:10:29 +00006 */
7
Alexey Brodkin2a7a2102013-12-26 15:29:07 +04008#include <bouncebuf.h>
Jaehoon Chung757bff42012-10-15 19:10:29 +00009#include <common.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070010#include <cpu_func.h>
Simon Glass1c87ffe2015-08-06 20:16:27 -060011#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Jaehoon Chung757bff42012-10-15 19:10:29 +000013#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060014#include <memalign.h>
Jaehoon Chung757bff42012-10-15 19:10:29 +000015#include <mmc.h>
16#include <dwmmc.h>
Ley Foon Tan79975992018-12-20 17:55:41 +080017#include <wait_bit.h>
Simon Glass90526e92020-05-10 11:39:56 -060018#include <asm/cache.h>
Urja Rannikko2b157012019-05-13 13:25:27 +000019#include <power/regulator.h>
Jaehoon Chung757bff42012-10-15 19:10:29 +000020
21#define PAGE_SIZE 4096
22
23static int dwmci_wait_reset(struct dwmci_host *host, u32 value)
24{
25 unsigned long timeout = 1000;
26 u32 ctrl;
27
28 dwmci_writel(host, DWMCI_CTRL, value);
29
30 while (timeout--) {
31 ctrl = dwmci_readl(host, DWMCI_CTRL);
32 if (!(ctrl & DWMCI_RESET_ALL))
33 return 1;
34 }
35 return 0;
36}
37
38static void dwmci_set_idma_desc(struct dwmci_idmac *idmac,
39 u32 desc0, u32 desc1, u32 desc2)
40{
41 struct dwmci_idmac *desc = idmac;
42
43 desc->flags = desc0;
44 desc->cnt = desc1;
45 desc->addr = desc2;
Prabhakar Kushwaha41f7be32015-10-25 13:18:25 +053046 desc->next_addr = (ulong)desc + sizeof(struct dwmci_idmac);
Jaehoon Chung757bff42012-10-15 19:10:29 +000047}
48
49static void dwmci_prepare_data(struct dwmci_host *host,
Alexey Brodkin2a7a2102013-12-26 15:29:07 +040050 struct mmc_data *data,
51 struct dwmci_idmac *cur_idmac,
52 void *bounce_buffer)
Jaehoon Chung757bff42012-10-15 19:10:29 +000053{
54 unsigned long ctrl;
55 unsigned int i = 0, flags, cnt, blk_cnt;
Alexey Brodkin2a7a2102013-12-26 15:29:07 +040056 ulong data_start, data_end;
Jaehoon Chung757bff42012-10-15 19:10:29 +000057
58
59 blk_cnt = data->blocks;
60
61 dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
62
Ley Foon Tan79975992018-12-20 17:55:41 +080063 /* Clear IDMAC interrupt */
64 dwmci_writel(host, DWMCI_IDSTS, 0xFFFFFFFF);
65
Jaehoon Chung757bff42012-10-15 19:10:29 +000066 data_start = (ulong)cur_idmac;
Prabhakar Kushwaha41f7be32015-10-25 13:18:25 +053067 dwmci_writel(host, DWMCI_DBADDR, (ulong)cur_idmac);
Jaehoon Chung757bff42012-10-15 19:10:29 +000068
Jaehoon Chung757bff42012-10-15 19:10:29 +000069 do {
70 flags = DWMCI_IDMAC_OWN | DWMCI_IDMAC_CH ;
71 flags |= (i == 0) ? DWMCI_IDMAC_FS : 0;
72 if (blk_cnt <= 8) {
73 flags |= DWMCI_IDMAC_LD;
74 cnt = data->blocksize * blk_cnt;
75 } else
76 cnt = data->blocksize * 8;
77
78 dwmci_set_idma_desc(cur_idmac, flags, cnt,
Prabhakar Kushwaha41f7be32015-10-25 13:18:25 +053079 (ulong)bounce_buffer + (i * PAGE_SIZE));
Jaehoon Chung757bff42012-10-15 19:10:29 +000080
Marek Vasutbdb5df12019-02-13 20:16:20 +010081 cur_idmac++;
Mischa Jonker21bd5762013-07-26 16:18:40 +020082 if (blk_cnt <= 8)
Jaehoon Chung757bff42012-10-15 19:10:29 +000083 break;
84 blk_cnt -= 8;
Jaehoon Chung757bff42012-10-15 19:10:29 +000085 i++;
86 } while(1);
87
88 data_end = (ulong)cur_idmac;
Marek Vasutbdb5df12019-02-13 20:16:20 +010089 flush_dcache_range(data_start, roundup(data_end, ARCH_DMA_MINALIGN));
Jaehoon Chung757bff42012-10-15 19:10:29 +000090
91 ctrl = dwmci_readl(host, DWMCI_CTRL);
92 ctrl |= DWMCI_IDMAC_EN | DWMCI_DMA_EN;
93 dwmci_writel(host, DWMCI_CTRL, ctrl);
94
95 ctrl = dwmci_readl(host, DWMCI_BMOD);
96 ctrl |= DWMCI_BMOD_IDMAC_FB | DWMCI_BMOD_IDMAC_EN;
97 dwmci_writel(host, DWMCI_BMOD, ctrl);
98
99 dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
100 dwmci_writel(host, DWMCI_BYTCNT, data->blocksize * data->blocks);
101}
102
Heiko Stuebner05fa06b2018-09-21 10:59:45 +0200103static int dwmci_fifo_ready(struct dwmci_host *host, u32 bit, u32 *len)
104{
105 u32 timeout = 20000;
106
107 *len = dwmci_readl(host, DWMCI_STATUS);
108 while (--timeout && (*len & bit)) {
109 udelay(200);
110 *len = dwmci_readl(host, DWMCI_STATUS);
111 }
112
113 if (!timeout) {
114 debug("%s: FIFO underflow timeout\n", __func__);
115 return -ETIMEDOUT;
116 }
117
118 return 0;
119}
120
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100121static unsigned int dwmci_get_timeout(struct mmc *mmc, const unsigned int size)
122{
123 unsigned int timeout;
124
Kever Yangc077c052019-08-29 15:42:41 +0800125 timeout = size * 8; /* counting in bits */
126 timeout *= 10; /* wait 10 times as long */
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100127 timeout /= mmc->clock;
128 timeout /= mmc->bus_width;
129 timeout /= mmc->ddr_mode ? 2 : 1;
Kever Yangc077c052019-08-29 15:42:41 +0800130 timeout *= 1000; /* counting in msec */
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100131 timeout = (timeout < 1000) ? 1000 : timeout;
132
133 return timeout;
134}
135
huang lina65f51b2015-11-17 14:20:22 +0800136static int dwmci_data_transfer(struct dwmci_host *host, struct mmc_data *data)
huang linf382eb82015-11-17 14:20:21 +0800137{
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100138 struct mmc *mmc = host->mmc;
huang linf382eb82015-11-17 14:20:21 +0800139 int ret = 0;
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100140 u32 timeout, mask, size, i, len = 0;
huang lina65f51b2015-11-17 14:20:22 +0800141 u32 *buf = NULL;
huang linf382eb82015-11-17 14:20:21 +0800142 ulong start = get_timer(0);
huang lina65f51b2015-11-17 14:20:22 +0800143 u32 fifo_depth = (((host->fifoth_val & RX_WMARK_MASK) >>
144 RX_WMARK_SHIFT) + 1) * 2;
145
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100146 size = data->blocksize * data->blocks;
huang lina65f51b2015-11-17 14:20:22 +0800147 if (data->flags == MMC_DATA_READ)
148 buf = (unsigned int *)data->dest;
149 else
150 buf = (unsigned int *)data->src;
huang linf382eb82015-11-17 14:20:21 +0800151
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100152 timeout = dwmci_get_timeout(mmc, size);
153
154 size /= 4;
155
huang linf382eb82015-11-17 14:20:21 +0800156 for (;;) {
157 mask = dwmci_readl(host, DWMCI_RINTSTS);
158 /* Error during data transfer. */
159 if (mask & (DWMCI_DATA_ERR | DWMCI_DATA_TOUT)) {
160 debug("%s: DATA ERROR!\n", __func__);
161 ret = -EINVAL;
162 break;
163 }
164
huang lina65f51b2015-11-17 14:20:22 +0800165 if (host->fifo_mode && size) {
Xu Ziyuan720724d2016-07-28 10:25:48 +0800166 len = 0;
Jacob Chen2b429032016-09-19 10:16:50 +0800167 if (data->flags == MMC_DATA_READ &&
168 (mask & DWMCI_INTMSK_RXDR)) {
169 while (size) {
Heiko Stuebner05fa06b2018-09-21 10:59:45 +0200170 ret = dwmci_fifo_ready(host,
171 DWMCI_FIFO_EMPTY,
172 &len);
173 if (ret < 0)
174 break;
175
huang lina65f51b2015-11-17 14:20:22 +0800176 len = (len >> DWMCI_FIFO_SHIFT) &
177 DWMCI_FIFO_MASK;
Xu Ziyuan2990e072016-07-28 10:25:47 +0800178 len = min(size, len);
huang lina65f51b2015-11-17 14:20:22 +0800179 for (i = 0; i < len; i++)
180 *buf++ =
181 dwmci_readl(host, DWMCI_DATA);
Jacob Chen2b429032016-09-19 10:16:50 +0800182 size = size > len ? (size - len) : 0;
huang lina65f51b2015-11-17 14:20:22 +0800183 }
Jacob Chen2b429032016-09-19 10:16:50 +0800184 dwmci_writel(host, DWMCI_RINTSTS,
185 DWMCI_INTMSK_RXDR);
186 } else if (data->flags == MMC_DATA_WRITE &&
187 (mask & DWMCI_INTMSK_TXDR)) {
188 while (size) {
Heiko Stuebner05fa06b2018-09-21 10:59:45 +0200189 ret = dwmci_fifo_ready(host,
190 DWMCI_FIFO_FULL,
191 &len);
192 if (ret < 0)
193 break;
194
huang lina65f51b2015-11-17 14:20:22 +0800195 len = fifo_depth - ((len >>
196 DWMCI_FIFO_SHIFT) &
197 DWMCI_FIFO_MASK);
Xu Ziyuan2990e072016-07-28 10:25:47 +0800198 len = min(size, len);
huang lina65f51b2015-11-17 14:20:22 +0800199 for (i = 0; i < len; i++)
200 dwmci_writel(host, DWMCI_DATA,
201 *buf++);
Jacob Chen2b429032016-09-19 10:16:50 +0800202 size = size > len ? (size - len) : 0;
huang lina65f51b2015-11-17 14:20:22 +0800203 }
Jacob Chen2b429032016-09-19 10:16:50 +0800204 dwmci_writel(host, DWMCI_RINTSTS,
205 DWMCI_INTMSK_TXDR);
huang lina65f51b2015-11-17 14:20:22 +0800206 }
huang lina65f51b2015-11-17 14:20:22 +0800207 }
208
huang linf382eb82015-11-17 14:20:21 +0800209 /* Data arrived correctly. */
210 if (mask & DWMCI_INTMSK_DTO) {
211 ret = 0;
212 break;
213 }
214
215 /* Check for timeout. */
216 if (get_timer(start) > timeout) {
217 debug("%s: Timeout waiting for data!\n",
218 __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900219 ret = -ETIMEDOUT;
huang linf382eb82015-11-17 14:20:21 +0800220 break;
221 }
222 }
223
224 dwmci_writel(host, DWMCI_RINTSTS, mask);
225
226 return ret;
227}
228
Jaehoon Chung757bff42012-10-15 19:10:29 +0000229static int dwmci_set_transfer_mode(struct dwmci_host *host,
230 struct mmc_data *data)
231{
232 unsigned long mode;
233
234 mode = DWMCI_CMD_DATA_EXP;
235 if (data->flags & MMC_DATA_WRITE)
236 mode |= DWMCI_CMD_RW;
237
238 return mode;
239}
240
Simon Glasse7881d82017-07-29 11:35:31 -0600241#ifdef CONFIG_DM_MMC
Jaehoon Chung56283472016-06-28 15:52:21 +0900242static int dwmci_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
Simon Glass691272f2016-06-12 23:30:23 -0600243 struct mmc_data *data)
244{
245 struct mmc *mmc = mmc_get_mmc_dev(dev);
246#else
Jaehoon Chung757bff42012-10-15 19:10:29 +0000247static int dwmci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
248 struct mmc_data *data)
249{
Simon Glass691272f2016-06-12 23:30:23 -0600250#endif
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200251 struct dwmci_host *host = mmc->priv;
Mischa Jonker2136d222013-07-26 14:08:14 +0200252 ALLOC_CACHE_ALIGN_BUFFER(struct dwmci_idmac, cur_idmac,
Mischa Jonker21bd5762013-07-26 16:18:40 +0200253 data ? DIV_ROUND_UP(data->blocks, 8) : 0);
Marek Vasut9042d972015-07-27 22:39:38 +0200254 int ret = 0, flags = 0, i;
Xu Ziyuan02ebd422016-07-19 09:38:22 +0800255 unsigned int timeout = 500;
Alexander Graf9b5b8b62016-03-04 01:09:52 +0100256 u32 retry = 100000;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000257 u32 mask, ctrl;
Amar9c50e352013-04-27 11:42:54 +0530258 ulong start = get_timer(0);
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400259 struct bounce_buffer bbstate;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000260
261 while (dwmci_readl(host, DWMCI_STATUS) & DWMCI_BUSY) {
Amar9c50e352013-04-27 11:42:54 +0530262 if (get_timer(start) > timeout) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600263 debug("%s: Timeout on data busy\n", __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900264 return -ETIMEDOUT;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000265 }
Jaehoon Chung757bff42012-10-15 19:10:29 +0000266 }
267
268 dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_ALL);
269
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400270 if (data) {
huang lina65f51b2015-11-17 14:20:22 +0800271 if (host->fifo_mode) {
272 dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
273 dwmci_writel(host, DWMCI_BYTCNT,
274 data->blocksize * data->blocks);
275 dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400276 } else {
huang lina65f51b2015-11-17 14:20:22 +0800277 if (data->flags == MMC_DATA_READ) {
Marek Vasut6ad5aec2019-03-23 18:45:27 +0100278 ret = bounce_buffer_start(&bbstate,
279 (void*)data->dest,
huang lina65f51b2015-11-17 14:20:22 +0800280 data->blocksize *
281 data->blocks, GEN_BB_WRITE);
282 } else {
Marek Vasut6ad5aec2019-03-23 18:45:27 +0100283 ret = bounce_buffer_start(&bbstate,
284 (void*)data->src,
huang lina65f51b2015-11-17 14:20:22 +0800285 data->blocksize *
286 data->blocks, GEN_BB_READ);
287 }
Marek Vasut6ad5aec2019-03-23 18:45:27 +0100288
289 if (ret)
290 return ret;
291
huang lina65f51b2015-11-17 14:20:22 +0800292 dwmci_prepare_data(host, data, cur_idmac,
293 bbstate.bounce_buffer);
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400294 }
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400295 }
Jaehoon Chung757bff42012-10-15 19:10:29 +0000296
Jaehoon Chung757bff42012-10-15 19:10:29 +0000297 dwmci_writel(host, DWMCI_CMDARG, cmd->cmdarg);
298
299 if (data)
300 flags = dwmci_set_transfer_mode(host, data);
301
302 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
303 return -1;
304
305 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
306 flags |= DWMCI_CMD_ABORT_STOP;
307 else
308 flags |= DWMCI_CMD_PRV_DAT_WAIT;
309
310 if (cmd->resp_type & MMC_RSP_PRESENT) {
311 flags |= DWMCI_CMD_RESP_EXP;
312 if (cmd->resp_type & MMC_RSP_136)
313 flags |= DWMCI_CMD_RESP_LENGTH;
314 }
315
316 if (cmd->resp_type & MMC_RSP_CRC)
317 flags |= DWMCI_CMD_CHECK_CRC;
318
319 flags |= (cmd->cmdidx | DWMCI_CMD_START | DWMCI_CMD_USE_HOLD_REG);
320
321 debug("Sending CMD%d\n",cmd->cmdidx);
322
323 dwmci_writel(host, DWMCI_CMD, flags);
324
325 for (i = 0; i < retry; i++) {
326 mask = dwmci_readl(host, DWMCI_RINTSTS);
327 if (mask & DWMCI_INTMSK_CDONE) {
328 if (!data)
329 dwmci_writel(host, DWMCI_RINTSTS, mask);
330 break;
331 }
332 }
333
Pavel Machekf33c9302014-09-05 12:49:48 +0200334 if (i == retry) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600335 debug("%s: Timeout.\n", __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900336 return -ETIMEDOUT;
Pavel Machekf33c9302014-09-05 12:49:48 +0200337 }
Jaehoon Chung757bff42012-10-15 19:10:29 +0000338
339 if (mask & DWMCI_INTMSK_RTO) {
Pavel Machekf33c9302014-09-05 12:49:48 +0200340 /*
341 * Timeout here is not necessarily fatal. (e)MMC cards
342 * will splat here when they receive CMD55 as they do
343 * not support this command and that is exactly the way
344 * to tell them apart from SD cards. Thus, this output
345 * below shall be debug(). eMMC cards also do not favor
346 * CMD8, please keep that in mind.
347 */
348 debug("%s: Response Timeout.\n", __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900349 return -ETIMEDOUT;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000350 } else if (mask & DWMCI_INTMSK_RE) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600351 debug("%s: Response Error.\n", __func__);
352 return -EIO;
Marek Vasut26cc40d2018-11-06 23:42:11 +0100353 } else if ((cmd->resp_type & MMC_RSP_CRC) &&
354 (mask & DWMCI_INTMSK_RCRC)) {
355 debug("%s: Response CRC Error.\n", __func__);
356 return -EIO;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000357 }
358
359
360 if (cmd->resp_type & MMC_RSP_PRESENT) {
361 if (cmd->resp_type & MMC_RSP_136) {
362 cmd->response[0] = dwmci_readl(host, DWMCI_RESP3);
363 cmd->response[1] = dwmci_readl(host, DWMCI_RESP2);
364 cmd->response[2] = dwmci_readl(host, DWMCI_RESP1);
365 cmd->response[3] = dwmci_readl(host, DWMCI_RESP0);
366 } else {
367 cmd->response[0] = dwmci_readl(host, DWMCI_RESP0);
368 }
369 }
370
371 if (data) {
huang lina65f51b2015-11-17 14:20:22 +0800372 ret = dwmci_data_transfer(host, data);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000373
huang lina65f51b2015-11-17 14:20:22 +0800374 /* only dma mode need it */
375 if (!host->fifo_mode) {
Ley Foon Tan79975992018-12-20 17:55:41 +0800376 if (data->flags == MMC_DATA_READ)
377 mask = DWMCI_IDINTEN_RI;
378 else
379 mask = DWMCI_IDINTEN_TI;
380 ret = wait_for_bit_le32(host->ioaddr + DWMCI_IDSTS,
381 mask, true, 1000, false);
382 if (ret)
383 debug("%s: DWMCI_IDINTEN mask 0x%x timeout.\n",
384 __func__, mask);
385 /* clear interrupts */
386 dwmci_writel(host, DWMCI_IDSTS, DWMCI_IDINTEN_MASK);
387
huang lina65f51b2015-11-17 14:20:22 +0800388 ctrl = dwmci_readl(host, DWMCI_CTRL);
389 ctrl &= ~(DWMCI_DMA_EN);
390 dwmci_writel(host, DWMCI_CTRL, ctrl);
391 bounce_buffer_stop(&bbstate);
392 }
Jaehoon Chung757bff42012-10-15 19:10:29 +0000393 }
394
395 udelay(100);
396
Marek Vasut9042d972015-07-27 22:39:38 +0200397 return ret;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000398}
399
400static int dwmci_setup_bus(struct dwmci_host *host, u32 freq)
401{
402 u32 div, status;
403 int timeout = 10000;
404 unsigned long sclk;
405
Amar9c50e352013-04-27 11:42:54 +0530406 if ((freq == host->clock) || (freq == 0))
Jaehoon Chung757bff42012-10-15 19:10:29 +0000407 return 0;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000408 /*
Pavel Machekf33c9302014-09-05 12:49:48 +0200409 * If host->get_mmc_clk isn't defined,
Jaehoon Chung757bff42012-10-15 19:10:29 +0000410 * then assume that host->bus_hz is source clock value.
Pavel Machekf33c9302014-09-05 12:49:48 +0200411 * host->bus_hz should be set by user.
Jaehoon Chung757bff42012-10-15 19:10:29 +0000412 */
Jaehoon Chungb44fe832013-10-06 18:59:31 +0900413 if (host->get_mmc_clk)
Simon Glasse3563f22015-08-30 16:55:15 -0600414 sclk = host->get_mmc_clk(host, freq);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000415 else if (host->bus_hz)
416 sclk = host->bus_hz;
417 else {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600418 debug("%s: Didn't get source clock value.\n", __func__);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000419 return -EINVAL;
420 }
421
Chin Liang See6ace1532014-06-10 01:26:52 -0500422 if (sclk == freq)
423 div = 0; /* bypass mode */
424 else
425 div = DIV_ROUND_UP(sclk, 2 * freq);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000426
427 dwmci_writel(host, DWMCI_CLKENA, 0);
428 dwmci_writel(host, DWMCI_CLKSRC, 0);
429
430 dwmci_writel(host, DWMCI_CLKDIV, div);
431 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
432 DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
433
434 do {
435 status = dwmci_readl(host, DWMCI_CMD);
436 if (timeout-- < 0) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600437 debug("%s: Timeout!\n", __func__);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000438 return -ETIMEDOUT;
439 }
440 } while (status & DWMCI_CMD_START);
441
442 dwmci_writel(host, DWMCI_CLKENA, DWMCI_CLKEN_ENABLE |
443 DWMCI_CLKEN_LOW_PWR);
444
445 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
446 DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
447
448 timeout = 10000;
449 do {
450 status = dwmci_readl(host, DWMCI_CMD);
451 if (timeout-- < 0) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600452 debug("%s: Timeout!\n", __func__);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000453 return -ETIMEDOUT;
454 }
455 } while (status & DWMCI_CMD_START);
456
457 host->clock = freq;
458
459 return 0;
460}
461
Simon Glasse7881d82017-07-29 11:35:31 -0600462#ifdef CONFIG_DM_MMC
Jaehoon Chung56283472016-06-28 15:52:21 +0900463static int dwmci_set_ios(struct udevice *dev)
Simon Glass691272f2016-06-12 23:30:23 -0600464{
465 struct mmc *mmc = mmc_get_mmc_dev(dev);
466#else
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900467static int dwmci_set_ios(struct mmc *mmc)
Jaehoon Chung757bff42012-10-15 19:10:29 +0000468{
Simon Glass691272f2016-06-12 23:30:23 -0600469#endif
Jaehoon Chung045bdcd2014-05-16 13:59:55 +0900470 struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
471 u32 ctype, regs;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000472
Pavel Machekf33c9302014-09-05 12:49:48 +0200473 debug("Buswidth = %d, clock: %d\n", mmc->bus_width, mmc->clock);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000474
475 dwmci_setup_bus(host, mmc->clock);
476 switch (mmc->bus_width) {
477 case 8:
478 ctype = DWMCI_CTYPE_8BIT;
479 break;
480 case 4:
481 ctype = DWMCI_CTYPE_4BIT;
482 break;
483 default:
484 ctype = DWMCI_CTYPE_1BIT;
485 break;
486 }
487
488 dwmci_writel(host, DWMCI_CTYPE, ctype);
489
Jaehoon Chung045bdcd2014-05-16 13:59:55 +0900490 regs = dwmci_readl(host, DWMCI_UHS_REG);
Andrew Gabbasov2b8a9692014-12-01 06:59:12 -0600491 if (mmc->ddr_mode)
Jaehoon Chung045bdcd2014-05-16 13:59:55 +0900492 regs |= DWMCI_DDR_MODE;
493 else
Jaehoon Chungafc9e2b2015-01-14 17:37:53 +0900494 regs &= ~DWMCI_DDR_MODE;
Jaehoon Chung045bdcd2014-05-16 13:59:55 +0900495
496 dwmci_writel(host, DWMCI_UHS_REG, regs);
497
Jaehoon Chung757bff42012-10-15 19:10:29 +0000498 if (host->clksel)
499 host->clksel(host);
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900500
Urja Rannikko2b157012019-05-13 13:25:27 +0000501#if CONFIG_IS_ENABLED(DM_REGULATOR)
502 if (mmc->vqmmc_supply) {
503 int ret;
504
505 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
506 regulator_set_value(mmc->vqmmc_supply, 1800000);
507 else
508 regulator_set_value(mmc->vqmmc_supply, 3300000);
509
510 ret = regulator_set_enable_if_allowed(mmc->vqmmc_supply, true);
511 if (ret)
512 return ret;
513 }
514#endif
515
Simon Glass691272f2016-06-12 23:30:23 -0600516 return 0;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000517}
518
519static int dwmci_init(struct mmc *mmc)
520{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200521 struct dwmci_host *host = mmc->priv;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000522
Jaehoon Chung18ab6752013-11-29 20:08:57 +0900523 if (host->board_init)
524 host->board_init(host);
Rajeshwari Shinde6f0b7ca2013-10-29 12:53:13 +0530525
Jaehoon Chung757bff42012-10-15 19:10:29 +0000526 dwmci_writel(host, DWMCI_PWREN, 1);
527
528 if (!dwmci_wait_reset(host, DWMCI_RESET_ALL)) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600529 debug("%s[%d] Fail-reset!!\n", __func__, __LINE__);
530 return -EIO;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000531 }
532
Amar9c50e352013-04-27 11:42:54 +0530533 /* Enumerate at 400KHz */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200534 dwmci_setup_bus(host, mmc->cfg->f_min);
Amar9c50e352013-04-27 11:42:54 +0530535
Jaehoon Chung757bff42012-10-15 19:10:29 +0000536 dwmci_writel(host, DWMCI_RINTSTS, 0xFFFFFFFF);
537 dwmci_writel(host, DWMCI_INTMASK, 0);
538
539 dwmci_writel(host, DWMCI_TMOUT, 0xFFFFFFFF);
540
541 dwmci_writel(host, DWMCI_IDINTEN, 0);
542 dwmci_writel(host, DWMCI_BMOD, 1);
543
Simon Glass760177d2015-08-06 20:16:29 -0600544 if (!host->fifoth_val) {
545 uint32_t fifo_size;
546
547 fifo_size = dwmci_readl(host, DWMCI_FIFOTH);
548 fifo_size = ((fifo_size & RX_WMARK_MASK) >> RX_WMARK_SHIFT) + 1;
549 host->fifoth_val = MSIZE(0x2) | RX_WMARK(fifo_size / 2 - 1) |
550 TX_WMARK(fifo_size / 2);
Amar9c50e352013-04-27 11:42:54 +0530551 }
Simon Glass760177d2015-08-06 20:16:29 -0600552 dwmci_writel(host, DWMCI_FIFOTH, host->fifoth_val);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000553
554 dwmci_writel(host, DWMCI_CLKENA, 0);
555 dwmci_writel(host, DWMCI_CLKSRC, 0);
556
Ley Foon Tan79975992018-12-20 17:55:41 +0800557 if (!host->fifo_mode)
558 dwmci_writel(host, DWMCI_IDINTEN, DWMCI_IDINTEN_MASK);
559
Jaehoon Chung757bff42012-10-15 19:10:29 +0000560 return 0;
561}
562
Simon Glasse7881d82017-07-29 11:35:31 -0600563#ifdef CONFIG_DM_MMC
Simon Glass691272f2016-06-12 23:30:23 -0600564int dwmci_probe(struct udevice *dev)
565{
566 struct mmc *mmc = mmc_get_mmc_dev(dev);
567
568 return dwmci_init(mmc);
569}
570
571const struct dm_mmc_ops dm_dwmci_ops = {
572 .send_cmd = dwmci_send_cmd,
573 .set_ios = dwmci_set_ios,
574};
575
576#else
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200577static const struct mmc_ops dwmci_ops = {
578 .send_cmd = dwmci_send_cmd,
579 .set_ios = dwmci_set_ios,
580 .init = dwmci_init,
581};
Simon Glass691272f2016-06-12 23:30:23 -0600582#endif
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200583
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900584void dwmci_setup_cfg(struct mmc_config *cfg, struct dwmci_host *host,
585 u32 max_clk, u32 min_clk)
Simon Glass5e6ff812016-05-14 14:03:07 -0600586{
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900587 cfg->name = host->name;
Simon Glasse7881d82017-07-29 11:35:31 -0600588#ifndef CONFIG_DM_MMC
Simon Glass5e6ff812016-05-14 14:03:07 -0600589 cfg->ops = &dwmci_ops;
Simon Glass691272f2016-06-12 23:30:23 -0600590#endif
Simon Glass5e6ff812016-05-14 14:03:07 -0600591 cfg->f_min = min_clk;
592 cfg->f_max = max_clk;
593
594 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
595
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900596 cfg->host_caps = host->caps;
Simon Glass5e6ff812016-05-14 14:03:07 -0600597
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900598 if (host->buswidth == 8) {
Simon Glass5e6ff812016-05-14 14:03:07 -0600599 cfg->host_caps |= MMC_MODE_8BIT;
600 cfg->host_caps &= ~MMC_MODE_4BIT;
601 } else {
602 cfg->host_caps |= MMC_MODE_4BIT;
603 cfg->host_caps &= ~MMC_MODE_8BIT;
604 }
605 cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz;
606
607 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
608}
609
610#ifdef CONFIG_BLK
611int dwmci_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg)
612{
613 return mmc_bind(dev, mmc, cfg);
614}
615#else
Jaehoon Chung757bff42012-10-15 19:10:29 +0000616int add_dwmci(struct dwmci_host *host, u32 max_clk, u32 min_clk)
617{
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900618 dwmci_setup_cfg(&host->cfg, host, max_clk, min_clk);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000619
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200620 host->mmc = mmc_create(&host->cfg, host);
621 if (host->mmc == NULL)
622 return -1;
623
624 return 0;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000625}
Simon Glass5e6ff812016-05-14 14:03:07 -0600626#endif