blob: 7702f4be3f8913965971f16806d739f53c690526 [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>
Simon Glassc05ed002020-05-10 11:40:11 -060019#include <linux/delay.h>
Urja Rannikko2b157012019-05-13 13:25:27 +000020#include <power/regulator.h>
Jaehoon Chung757bff42012-10-15 19:10:29 +000021
22#define PAGE_SIZE 4096
23
24static int dwmci_wait_reset(struct dwmci_host *host, u32 value)
25{
26 unsigned long timeout = 1000;
27 u32 ctrl;
28
29 dwmci_writel(host, DWMCI_CTRL, value);
30
31 while (timeout--) {
32 ctrl = dwmci_readl(host, DWMCI_CTRL);
33 if (!(ctrl & DWMCI_RESET_ALL))
34 return 1;
35 }
36 return 0;
37}
38
39static void dwmci_set_idma_desc(struct dwmci_idmac *idmac,
40 u32 desc0, u32 desc1, u32 desc2)
41{
42 struct dwmci_idmac *desc = idmac;
43
44 desc->flags = desc0;
45 desc->cnt = desc1;
46 desc->addr = desc2;
Prabhakar Kushwaha41f7be32015-10-25 13:18:25 +053047 desc->next_addr = (ulong)desc + sizeof(struct dwmci_idmac);
Jaehoon Chung757bff42012-10-15 19:10:29 +000048}
49
50static void dwmci_prepare_data(struct dwmci_host *host,
Alexey Brodkin2a7a2102013-12-26 15:29:07 +040051 struct mmc_data *data,
52 struct dwmci_idmac *cur_idmac,
53 void *bounce_buffer)
Jaehoon Chung757bff42012-10-15 19:10:29 +000054{
55 unsigned long ctrl;
56 unsigned int i = 0, flags, cnt, blk_cnt;
Alexey Brodkin2a7a2102013-12-26 15:29:07 +040057 ulong data_start, data_end;
Jaehoon Chung757bff42012-10-15 19:10:29 +000058
59
60 blk_cnt = data->blocks;
61
62 dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
63
Ley Foon Tan79975992018-12-20 17:55:41 +080064 /* Clear IDMAC interrupt */
65 dwmci_writel(host, DWMCI_IDSTS, 0xFFFFFFFF);
66
Jaehoon Chung757bff42012-10-15 19:10:29 +000067 data_start = (ulong)cur_idmac;
Prabhakar Kushwaha41f7be32015-10-25 13:18:25 +053068 dwmci_writel(host, DWMCI_DBADDR, (ulong)cur_idmac);
Jaehoon Chung757bff42012-10-15 19:10:29 +000069
Jaehoon Chung757bff42012-10-15 19:10:29 +000070 do {
71 flags = DWMCI_IDMAC_OWN | DWMCI_IDMAC_CH ;
72 flags |= (i == 0) ? DWMCI_IDMAC_FS : 0;
73 if (blk_cnt <= 8) {
74 flags |= DWMCI_IDMAC_LD;
75 cnt = data->blocksize * blk_cnt;
76 } else
77 cnt = data->blocksize * 8;
78
79 dwmci_set_idma_desc(cur_idmac, flags, cnt,
Prabhakar Kushwaha41f7be32015-10-25 13:18:25 +053080 (ulong)bounce_buffer + (i * PAGE_SIZE));
Jaehoon Chung757bff42012-10-15 19:10:29 +000081
Marek Vasutbdb5df12019-02-13 20:16:20 +010082 cur_idmac++;
Mischa Jonker21bd5762013-07-26 16:18:40 +020083 if (blk_cnt <= 8)
Jaehoon Chung757bff42012-10-15 19:10:29 +000084 break;
85 blk_cnt -= 8;
Jaehoon Chung757bff42012-10-15 19:10:29 +000086 i++;
87 } while(1);
88
89 data_end = (ulong)cur_idmac;
Marek Vasutbdb5df12019-02-13 20:16:20 +010090 flush_dcache_range(data_start, roundup(data_end, ARCH_DMA_MINALIGN));
Jaehoon Chung757bff42012-10-15 19:10:29 +000091
92 ctrl = dwmci_readl(host, DWMCI_CTRL);
93 ctrl |= DWMCI_IDMAC_EN | DWMCI_DMA_EN;
94 dwmci_writel(host, DWMCI_CTRL, ctrl);
95
96 ctrl = dwmci_readl(host, DWMCI_BMOD);
97 ctrl |= DWMCI_BMOD_IDMAC_FB | DWMCI_BMOD_IDMAC_EN;
98 dwmci_writel(host, DWMCI_BMOD, ctrl);
99
100 dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
101 dwmci_writel(host, DWMCI_BYTCNT, data->blocksize * data->blocks);
102}
103
Heiko Stuebner05fa06b2018-09-21 10:59:45 +0200104static int dwmci_fifo_ready(struct dwmci_host *host, u32 bit, u32 *len)
105{
106 u32 timeout = 20000;
107
108 *len = dwmci_readl(host, DWMCI_STATUS);
109 while (--timeout && (*len & bit)) {
110 udelay(200);
111 *len = dwmci_readl(host, DWMCI_STATUS);
112 }
113
114 if (!timeout) {
115 debug("%s: FIFO underflow timeout\n", __func__);
116 return -ETIMEDOUT;
117 }
118
119 return 0;
120}
121
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100122static unsigned int dwmci_get_timeout(struct mmc *mmc, const unsigned int size)
123{
124 unsigned int timeout;
125
Kever Yangc077c052019-08-29 15:42:41 +0800126 timeout = size * 8; /* counting in bits */
127 timeout *= 10; /* wait 10 times as long */
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100128 timeout /= mmc->clock;
129 timeout /= mmc->bus_width;
130 timeout /= mmc->ddr_mode ? 2 : 1;
Kever Yangc077c052019-08-29 15:42:41 +0800131 timeout *= 1000; /* counting in msec */
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100132 timeout = (timeout < 1000) ? 1000 : timeout;
133
134 return timeout;
135}
136
huang lina65f51b2015-11-17 14:20:22 +0800137static int dwmci_data_transfer(struct dwmci_host *host, struct mmc_data *data)
huang linf382eb82015-11-17 14:20:21 +0800138{
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100139 struct mmc *mmc = host->mmc;
huang linf382eb82015-11-17 14:20:21 +0800140 int ret = 0;
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100141 u32 timeout, mask, size, i, len = 0;
huang lina65f51b2015-11-17 14:20:22 +0800142 u32 *buf = NULL;
huang linf382eb82015-11-17 14:20:21 +0800143 ulong start = get_timer(0);
huang lina65f51b2015-11-17 14:20:22 +0800144 u32 fifo_depth = (((host->fifoth_val & RX_WMARK_MASK) >>
145 RX_WMARK_SHIFT) + 1) * 2;
146
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100147 size = data->blocksize * data->blocks;
huang lina65f51b2015-11-17 14:20:22 +0800148 if (data->flags == MMC_DATA_READ)
149 buf = (unsigned int *)data->dest;
150 else
151 buf = (unsigned int *)data->src;
huang linf382eb82015-11-17 14:20:21 +0800152
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100153 timeout = dwmci_get_timeout(mmc, size);
154
155 size /= 4;
156
huang linf382eb82015-11-17 14:20:21 +0800157 for (;;) {
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__);
162 ret = -EINVAL;
163 break;
164 }
165
huang lina65f51b2015-11-17 14:20:22 +0800166 if (host->fifo_mode && size) {
Xu Ziyuan720724d2016-07-28 10:25:48 +0800167 len = 0;
Jacob Chen2b429032016-09-19 10:16:50 +0800168 if (data->flags == MMC_DATA_READ &&
169 (mask & DWMCI_INTMSK_RXDR)) {
170 while (size) {
Heiko Stuebner05fa06b2018-09-21 10:59:45 +0200171 ret = dwmci_fifo_ready(host,
172 DWMCI_FIFO_EMPTY,
173 &len);
174 if (ret < 0)
175 break;
176
huang lina65f51b2015-11-17 14:20:22 +0800177 len = (len >> DWMCI_FIFO_SHIFT) &
178 DWMCI_FIFO_MASK;
Xu Ziyuan2990e072016-07-28 10:25:47 +0800179 len = min(size, len);
huang lina65f51b2015-11-17 14:20:22 +0800180 for (i = 0; i < len; i++)
181 *buf++ =
182 dwmci_readl(host, DWMCI_DATA);
Jacob Chen2b429032016-09-19 10:16:50 +0800183 size = size > len ? (size - len) : 0;
huang lina65f51b2015-11-17 14:20:22 +0800184 }
Jacob Chen2b429032016-09-19 10:16:50 +0800185 dwmci_writel(host, DWMCI_RINTSTS,
186 DWMCI_INTMSK_RXDR);
187 } else if (data->flags == MMC_DATA_WRITE &&
188 (mask & DWMCI_INTMSK_TXDR)) {
189 while (size) {
Heiko Stuebner05fa06b2018-09-21 10:59:45 +0200190 ret = dwmci_fifo_ready(host,
191 DWMCI_FIFO_FULL,
192 &len);
193 if (ret < 0)
194 break;
195
huang lina65f51b2015-11-17 14:20:22 +0800196 len = fifo_depth - ((len >>
197 DWMCI_FIFO_SHIFT) &
198 DWMCI_FIFO_MASK);
Xu Ziyuan2990e072016-07-28 10:25:47 +0800199 len = min(size, len);
huang lina65f51b2015-11-17 14:20:22 +0800200 for (i = 0; i < len; i++)
201 dwmci_writel(host, DWMCI_DATA,
202 *buf++);
Jacob Chen2b429032016-09-19 10:16:50 +0800203 size = size > len ? (size - len) : 0;
huang lina65f51b2015-11-17 14:20:22 +0800204 }
Jacob Chen2b429032016-09-19 10:16:50 +0800205 dwmci_writel(host, DWMCI_RINTSTS,
206 DWMCI_INTMSK_TXDR);
huang lina65f51b2015-11-17 14:20:22 +0800207 }
huang lina65f51b2015-11-17 14:20:22 +0800208 }
209
huang linf382eb82015-11-17 14:20:21 +0800210 /* Data arrived correctly. */
211 if (mask & DWMCI_INTMSK_DTO) {
212 ret = 0;
213 break;
214 }
215
216 /* Check for timeout. */
217 if (get_timer(start) > timeout) {
218 debug("%s: Timeout waiting for data!\n",
219 __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900220 ret = -ETIMEDOUT;
huang linf382eb82015-11-17 14:20:21 +0800221 break;
222 }
223 }
224
225 dwmci_writel(host, DWMCI_RINTSTS, mask);
226
227 return ret;
228}
229
Jaehoon Chung757bff42012-10-15 19:10:29 +0000230static int dwmci_set_transfer_mode(struct dwmci_host *host,
231 struct mmc_data *data)
232{
233 unsigned long mode;
234
235 mode = DWMCI_CMD_DATA_EXP;
236 if (data->flags & MMC_DATA_WRITE)
237 mode |= DWMCI_CMD_RW;
238
239 return mode;
240}
241
Simon Glasse7881d82017-07-29 11:35:31 -0600242#ifdef CONFIG_DM_MMC
Jaehoon Chung56283472016-06-28 15:52:21 +0900243static int dwmci_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
Simon Glass691272f2016-06-12 23:30:23 -0600244 struct mmc_data *data)
245{
246 struct mmc *mmc = mmc_get_mmc_dev(dev);
247#else
Jaehoon Chung757bff42012-10-15 19:10:29 +0000248static int dwmci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
249 struct mmc_data *data)
250{
Simon Glass691272f2016-06-12 23:30:23 -0600251#endif
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200252 struct dwmci_host *host = mmc->priv;
Mischa Jonker2136d222013-07-26 14:08:14 +0200253 ALLOC_CACHE_ALIGN_BUFFER(struct dwmci_idmac, cur_idmac,
Mischa Jonker21bd5762013-07-26 16:18:40 +0200254 data ? DIV_ROUND_UP(data->blocks, 8) : 0);
Marek Vasut9042d972015-07-27 22:39:38 +0200255 int ret = 0, flags = 0, i;
Xu Ziyuan02ebd422016-07-19 09:38:22 +0800256 unsigned int timeout = 500;
Alexander Graf9b5b8b62016-03-04 01:09:52 +0100257 u32 retry = 100000;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000258 u32 mask, ctrl;
Amar9c50e352013-04-27 11:42:54 +0530259 ulong start = get_timer(0);
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400260 struct bounce_buffer bbstate;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000261
262 while (dwmci_readl(host, DWMCI_STATUS) & DWMCI_BUSY) {
Amar9c50e352013-04-27 11:42:54 +0530263 if (get_timer(start) > timeout) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600264 debug("%s: Timeout on data busy\n", __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900265 return -ETIMEDOUT;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000266 }
Jaehoon Chung757bff42012-10-15 19:10:29 +0000267 }
268
269 dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_ALL);
270
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400271 if (data) {
huang lina65f51b2015-11-17 14:20:22 +0800272 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);
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400277 } else {
huang lina65f51b2015-11-17 14:20:22 +0800278 if (data->flags == MMC_DATA_READ) {
Marek Vasut6ad5aec2019-03-23 18:45:27 +0100279 ret = bounce_buffer_start(&bbstate,
280 (void*)data->dest,
huang lina65f51b2015-11-17 14:20:22 +0800281 data->blocksize *
282 data->blocks, GEN_BB_WRITE);
283 } else {
Marek Vasut6ad5aec2019-03-23 18:45:27 +0100284 ret = bounce_buffer_start(&bbstate,
285 (void*)data->src,
huang lina65f51b2015-11-17 14:20:22 +0800286 data->blocksize *
287 data->blocks, GEN_BB_READ);
288 }
Marek Vasut6ad5aec2019-03-23 18:45:27 +0100289
290 if (ret)
291 return ret;
292
huang lina65f51b2015-11-17 14:20:22 +0800293 dwmci_prepare_data(host, data, cur_idmac,
294 bbstate.bounce_buffer);
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400295 }
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400296 }
Jaehoon Chung757bff42012-10-15 19:10:29 +0000297
Jaehoon Chung757bff42012-10-15 19:10:29 +0000298 dwmci_writel(host, DWMCI_CMDARG, cmd->cmdarg);
299
300 if (data)
301 flags = dwmci_set_transfer_mode(host, data);
302
303 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
304 return -1;
305
306 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
307 flags |= DWMCI_CMD_ABORT_STOP;
308 else
309 flags |= DWMCI_CMD_PRV_DAT_WAIT;
310
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;
315 }
316
317 if (cmd->resp_type & MMC_RSP_CRC)
318 flags |= DWMCI_CMD_CHECK_CRC;
319
320 flags |= (cmd->cmdidx | DWMCI_CMD_START | DWMCI_CMD_USE_HOLD_REG);
321
322 debug("Sending CMD%d\n",cmd->cmdidx);
323
324 dwmci_writel(host, DWMCI_CMD, flags);
325
326 for (i = 0; i < retry; i++) {
327 mask = dwmci_readl(host, DWMCI_RINTSTS);
328 if (mask & DWMCI_INTMSK_CDONE) {
329 if (!data)
330 dwmci_writel(host, DWMCI_RINTSTS, mask);
331 break;
332 }
333 }
334
Pavel Machekf33c9302014-09-05 12:49:48 +0200335 if (i == retry) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600336 debug("%s: Timeout.\n", __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900337 return -ETIMEDOUT;
Pavel Machekf33c9302014-09-05 12:49:48 +0200338 }
Jaehoon Chung757bff42012-10-15 19:10:29 +0000339
340 if (mask & DWMCI_INTMSK_RTO) {
Pavel Machekf33c9302014-09-05 12:49:48 +0200341 /*
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.
348 */
349 debug("%s: Response Timeout.\n", __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900350 return -ETIMEDOUT;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000351 } else if (mask & DWMCI_INTMSK_RE) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600352 debug("%s: Response Error.\n", __func__);
353 return -EIO;
Marek Vasut26cc40d2018-11-06 23:42:11 +0100354 } else if ((cmd->resp_type & MMC_RSP_CRC) &&
355 (mask & DWMCI_INTMSK_RCRC)) {
356 debug("%s: Response CRC Error.\n", __func__);
357 return -EIO;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000358 }
359
360
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);
367 } else {
368 cmd->response[0] = dwmci_readl(host, DWMCI_RESP0);
369 }
370 }
371
372 if (data) {
huang lina65f51b2015-11-17 14:20:22 +0800373 ret = dwmci_data_transfer(host, data);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000374
huang lina65f51b2015-11-17 14:20:22 +0800375 /* only dma mode need it */
376 if (!host->fifo_mode) {
Ley Foon Tan79975992018-12-20 17:55:41 +0800377 if (data->flags == MMC_DATA_READ)
378 mask = DWMCI_IDINTEN_RI;
379 else
380 mask = DWMCI_IDINTEN_TI;
381 ret = wait_for_bit_le32(host->ioaddr + DWMCI_IDSTS,
382 mask, true, 1000, false);
383 if (ret)
384 debug("%s: DWMCI_IDINTEN mask 0x%x timeout.\n",
385 __func__, mask);
386 /* clear interrupts */
387 dwmci_writel(host, DWMCI_IDSTS, DWMCI_IDINTEN_MASK);
388
huang lina65f51b2015-11-17 14:20:22 +0800389 ctrl = dwmci_readl(host, DWMCI_CTRL);
390 ctrl &= ~(DWMCI_DMA_EN);
391 dwmci_writel(host, DWMCI_CTRL, ctrl);
392 bounce_buffer_stop(&bbstate);
393 }
Jaehoon Chung757bff42012-10-15 19:10:29 +0000394 }
395
396 udelay(100);
397
Marek Vasut9042d972015-07-27 22:39:38 +0200398 return ret;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000399}
400
401static int dwmci_setup_bus(struct dwmci_host *host, u32 freq)
402{
403 u32 div, status;
404 int timeout = 10000;
405 unsigned long sclk;
406
Amar9c50e352013-04-27 11:42:54 +0530407 if ((freq == host->clock) || (freq == 0))
Jaehoon Chung757bff42012-10-15 19:10:29 +0000408 return 0;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000409 /*
Pavel Machekf33c9302014-09-05 12:49:48 +0200410 * If host->get_mmc_clk isn't defined,
Jaehoon Chung757bff42012-10-15 19:10:29 +0000411 * then assume that host->bus_hz is source clock value.
Pavel Machekf33c9302014-09-05 12:49:48 +0200412 * host->bus_hz should be set by user.
Jaehoon Chung757bff42012-10-15 19:10:29 +0000413 */
Jaehoon Chungb44fe832013-10-06 18:59:31 +0900414 if (host->get_mmc_clk)
Simon Glasse3563f22015-08-30 16:55:15 -0600415 sclk = host->get_mmc_clk(host, freq);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000416 else if (host->bus_hz)
417 sclk = host->bus_hz;
418 else {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600419 debug("%s: Didn't get source clock value.\n", __func__);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000420 return -EINVAL;
421 }
422
Chin Liang See6ace1532014-06-10 01:26:52 -0500423 if (sclk == freq)
424 div = 0; /* bypass mode */
425 else
426 div = DIV_ROUND_UP(sclk, 2 * freq);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000427
428 dwmci_writel(host, DWMCI_CLKENA, 0);
429 dwmci_writel(host, DWMCI_CLKSRC, 0);
430
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);
434
435 do {
436 status = dwmci_readl(host, DWMCI_CMD);
437 if (timeout-- < 0) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600438 debug("%s: Timeout!\n", __func__);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000439 return -ETIMEDOUT;
440 }
441 } while (status & DWMCI_CMD_START);
442
443 dwmci_writel(host, DWMCI_CLKENA, DWMCI_CLKEN_ENABLE |
444 DWMCI_CLKEN_LOW_PWR);
445
446 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
447 DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
448
449 timeout = 10000;
450 do {
451 status = dwmci_readl(host, DWMCI_CMD);
452 if (timeout-- < 0) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600453 debug("%s: Timeout!\n", __func__);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000454 return -ETIMEDOUT;
455 }
456 } while (status & DWMCI_CMD_START);
457
458 host->clock = freq;
459
460 return 0;
461}
462
Simon Glasse7881d82017-07-29 11:35:31 -0600463#ifdef CONFIG_DM_MMC
Jaehoon Chung56283472016-06-28 15:52:21 +0900464static int dwmci_set_ios(struct udevice *dev)
Simon Glass691272f2016-06-12 23:30:23 -0600465{
466 struct mmc *mmc = mmc_get_mmc_dev(dev);
467#else
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900468static int dwmci_set_ios(struct mmc *mmc)
Jaehoon Chung757bff42012-10-15 19:10:29 +0000469{
Simon Glass691272f2016-06-12 23:30:23 -0600470#endif
Jaehoon Chung045bdcd2014-05-16 13:59:55 +0900471 struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
472 u32 ctype, regs;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000473
Pavel Machekf33c9302014-09-05 12:49:48 +0200474 debug("Buswidth = %d, clock: %d\n", mmc->bus_width, mmc->clock);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000475
476 dwmci_setup_bus(host, mmc->clock);
477 switch (mmc->bus_width) {
478 case 8:
479 ctype = DWMCI_CTYPE_8BIT;
480 break;
481 case 4:
482 ctype = DWMCI_CTYPE_4BIT;
483 break;
484 default:
485 ctype = DWMCI_CTYPE_1BIT;
486 break;
487 }
488
489 dwmci_writel(host, DWMCI_CTYPE, ctype);
490
Jaehoon Chung045bdcd2014-05-16 13:59:55 +0900491 regs = dwmci_readl(host, DWMCI_UHS_REG);
Andrew Gabbasov2b8a9692014-12-01 06:59:12 -0600492 if (mmc->ddr_mode)
Jaehoon Chung045bdcd2014-05-16 13:59:55 +0900493 regs |= DWMCI_DDR_MODE;
494 else
Jaehoon Chungafc9e2b2015-01-14 17:37:53 +0900495 regs &= ~DWMCI_DDR_MODE;
Jaehoon Chung045bdcd2014-05-16 13:59:55 +0900496
497 dwmci_writel(host, DWMCI_UHS_REG, regs);
498
Jaehoon Chung757bff42012-10-15 19:10:29 +0000499 if (host->clksel)
500 host->clksel(host);
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900501
Urja Rannikko2b157012019-05-13 13:25:27 +0000502#if CONFIG_IS_ENABLED(DM_REGULATOR)
503 if (mmc->vqmmc_supply) {
504 int ret;
505
506 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
507 regulator_set_value(mmc->vqmmc_supply, 1800000);
508 else
509 regulator_set_value(mmc->vqmmc_supply, 3300000);
510
511 ret = regulator_set_enable_if_allowed(mmc->vqmmc_supply, true);
512 if (ret)
513 return ret;
514 }
515#endif
516
Simon Glass691272f2016-06-12 23:30:23 -0600517 return 0;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000518}
519
520static int dwmci_init(struct mmc *mmc)
521{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200522 struct dwmci_host *host = mmc->priv;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000523
Jaehoon Chung18ab6752013-11-29 20:08:57 +0900524 if (host->board_init)
525 host->board_init(host);
Rajeshwari Shinde6f0b7ca2013-10-29 12:53:13 +0530526
Jaehoon Chung757bff42012-10-15 19:10:29 +0000527 dwmci_writel(host, DWMCI_PWREN, 1);
528
529 if (!dwmci_wait_reset(host, DWMCI_RESET_ALL)) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600530 debug("%s[%d] Fail-reset!!\n", __func__, __LINE__);
531 return -EIO;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000532 }
533
Amar9c50e352013-04-27 11:42:54 +0530534 /* Enumerate at 400KHz */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200535 dwmci_setup_bus(host, mmc->cfg->f_min);
Amar9c50e352013-04-27 11:42:54 +0530536
Jaehoon Chung757bff42012-10-15 19:10:29 +0000537 dwmci_writel(host, DWMCI_RINTSTS, 0xFFFFFFFF);
538 dwmci_writel(host, DWMCI_INTMASK, 0);
539
540 dwmci_writel(host, DWMCI_TMOUT, 0xFFFFFFFF);
541
542 dwmci_writel(host, DWMCI_IDINTEN, 0);
543 dwmci_writel(host, DWMCI_BMOD, 1);
544
Simon Glass760177d2015-08-06 20:16:29 -0600545 if (!host->fifoth_val) {
546 uint32_t fifo_size;
547
548 fifo_size = dwmci_readl(host, DWMCI_FIFOTH);
549 fifo_size = ((fifo_size & RX_WMARK_MASK) >> RX_WMARK_SHIFT) + 1;
550 host->fifoth_val = MSIZE(0x2) | RX_WMARK(fifo_size / 2 - 1) |
551 TX_WMARK(fifo_size / 2);
Amar9c50e352013-04-27 11:42:54 +0530552 }
Simon Glass760177d2015-08-06 20:16:29 -0600553 dwmci_writel(host, DWMCI_FIFOTH, host->fifoth_val);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000554
555 dwmci_writel(host, DWMCI_CLKENA, 0);
556 dwmci_writel(host, DWMCI_CLKSRC, 0);
557
Ley Foon Tan79975992018-12-20 17:55:41 +0800558 if (!host->fifo_mode)
559 dwmci_writel(host, DWMCI_IDINTEN, DWMCI_IDINTEN_MASK);
560
Jaehoon Chung757bff42012-10-15 19:10:29 +0000561 return 0;
562}
563
Simon Glasse7881d82017-07-29 11:35:31 -0600564#ifdef CONFIG_DM_MMC
Simon Glass691272f2016-06-12 23:30:23 -0600565int dwmci_probe(struct udevice *dev)
566{
567 struct mmc *mmc = mmc_get_mmc_dev(dev);
568
569 return dwmci_init(mmc);
570}
571
572const struct dm_mmc_ops dm_dwmci_ops = {
573 .send_cmd = dwmci_send_cmd,
574 .set_ios = dwmci_set_ios,
575};
576
577#else
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200578static const struct mmc_ops dwmci_ops = {
579 .send_cmd = dwmci_send_cmd,
580 .set_ios = dwmci_set_ios,
581 .init = dwmci_init,
582};
Simon Glass691272f2016-06-12 23:30:23 -0600583#endif
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200584
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900585void dwmci_setup_cfg(struct mmc_config *cfg, struct dwmci_host *host,
586 u32 max_clk, u32 min_clk)
Simon Glass5e6ff812016-05-14 14:03:07 -0600587{
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900588 cfg->name = host->name;
Simon Glasse7881d82017-07-29 11:35:31 -0600589#ifndef CONFIG_DM_MMC
Simon Glass5e6ff812016-05-14 14:03:07 -0600590 cfg->ops = &dwmci_ops;
Simon Glass691272f2016-06-12 23:30:23 -0600591#endif
Simon Glass5e6ff812016-05-14 14:03:07 -0600592 cfg->f_min = min_clk;
593 cfg->f_max = max_clk;
594
595 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
596
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900597 cfg->host_caps = host->caps;
Simon Glass5e6ff812016-05-14 14:03:07 -0600598
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900599 if (host->buswidth == 8) {
Simon Glass5e6ff812016-05-14 14:03:07 -0600600 cfg->host_caps |= MMC_MODE_8BIT;
601 cfg->host_caps &= ~MMC_MODE_4BIT;
602 } else {
603 cfg->host_caps |= MMC_MODE_4BIT;
604 cfg->host_caps &= ~MMC_MODE_8BIT;
605 }
606 cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz;
607
608 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
609}
610
611#ifdef CONFIG_BLK
612int dwmci_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg)
613{
614 return mmc_bind(dev, mmc, cfg);
615}
616#else
Jaehoon Chung757bff42012-10-15 19:10:29 +0000617int add_dwmci(struct dwmci_host *host, u32 max_clk, u32 min_clk)
618{
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900619 dwmci_setup_cfg(&host->cfg, host, max_clk, min_clk);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000620
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200621 host->mmc = mmc_create(&host->cfg, host);
622 if (host->mmc == NULL)
623 return -1;
624
625 return 0;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000626}
Simon Glass5e6ff812016-05-14 14:03:07 -0600627#endif