blob: 12245408114551bb96dce4ed70541ca4df847b13 [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>
Jaehoon Chung757bff42012-10-15 19:10:29 +000012#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060013#include <memalign.h>
Jaehoon Chung757bff42012-10-15 19:10:29 +000014#include <mmc.h>
15#include <dwmmc.h>
Ley Foon Tan79975992018-12-20 17:55:41 +080016#include <wait_bit.h>
Urja Rannikko2b157012019-05-13 13:25:27 +000017#include <power/regulator.h>
Jaehoon Chung757bff42012-10-15 19:10:29 +000018
19#define PAGE_SIZE 4096
20
21static int dwmci_wait_reset(struct dwmci_host *host, u32 value)
22{
23 unsigned long timeout = 1000;
24 u32 ctrl;
25
26 dwmci_writel(host, DWMCI_CTRL, value);
27
28 while (timeout--) {
29 ctrl = dwmci_readl(host, DWMCI_CTRL);
30 if (!(ctrl & DWMCI_RESET_ALL))
31 return 1;
32 }
33 return 0;
34}
35
36static void dwmci_set_idma_desc(struct dwmci_idmac *idmac,
37 u32 desc0, u32 desc1, u32 desc2)
38{
39 struct dwmci_idmac *desc = idmac;
40
41 desc->flags = desc0;
42 desc->cnt = desc1;
43 desc->addr = desc2;
Prabhakar Kushwaha41f7be32015-10-25 13:18:25 +053044 desc->next_addr = (ulong)desc + sizeof(struct dwmci_idmac);
Jaehoon Chung757bff42012-10-15 19:10:29 +000045}
46
47static void dwmci_prepare_data(struct dwmci_host *host,
Alexey Brodkin2a7a2102013-12-26 15:29:07 +040048 struct mmc_data *data,
49 struct dwmci_idmac *cur_idmac,
50 void *bounce_buffer)
Jaehoon Chung757bff42012-10-15 19:10:29 +000051{
52 unsigned long ctrl;
53 unsigned int i = 0, flags, cnt, blk_cnt;
Alexey Brodkin2a7a2102013-12-26 15:29:07 +040054 ulong data_start, data_end;
Jaehoon Chung757bff42012-10-15 19:10:29 +000055
56
57 blk_cnt = data->blocks;
58
59 dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
60
Ley Foon Tan79975992018-12-20 17:55:41 +080061 /* Clear IDMAC interrupt */
62 dwmci_writel(host, DWMCI_IDSTS, 0xFFFFFFFF);
63
Jaehoon Chung757bff42012-10-15 19:10:29 +000064 data_start = (ulong)cur_idmac;
Prabhakar Kushwaha41f7be32015-10-25 13:18:25 +053065 dwmci_writel(host, DWMCI_DBADDR, (ulong)cur_idmac);
Jaehoon Chung757bff42012-10-15 19:10:29 +000066
Jaehoon Chung757bff42012-10-15 19:10:29 +000067 do {
68 flags = DWMCI_IDMAC_OWN | DWMCI_IDMAC_CH ;
69 flags |= (i == 0) ? DWMCI_IDMAC_FS : 0;
70 if (blk_cnt <= 8) {
71 flags |= DWMCI_IDMAC_LD;
72 cnt = data->blocksize * blk_cnt;
73 } else
74 cnt = data->blocksize * 8;
75
76 dwmci_set_idma_desc(cur_idmac, flags, cnt,
Prabhakar Kushwaha41f7be32015-10-25 13:18:25 +053077 (ulong)bounce_buffer + (i * PAGE_SIZE));
Jaehoon Chung757bff42012-10-15 19:10:29 +000078
Marek Vasutbdb5df12019-02-13 20:16:20 +010079 cur_idmac++;
Mischa Jonker21bd5762013-07-26 16:18:40 +020080 if (blk_cnt <= 8)
Jaehoon Chung757bff42012-10-15 19:10:29 +000081 break;
82 blk_cnt -= 8;
Jaehoon Chung757bff42012-10-15 19:10:29 +000083 i++;
84 } while(1);
85
86 data_end = (ulong)cur_idmac;
Marek Vasutbdb5df12019-02-13 20:16:20 +010087 flush_dcache_range(data_start, roundup(data_end, ARCH_DMA_MINALIGN));
Jaehoon Chung757bff42012-10-15 19:10:29 +000088
89 ctrl = dwmci_readl(host, DWMCI_CTRL);
90 ctrl |= DWMCI_IDMAC_EN | DWMCI_DMA_EN;
91 dwmci_writel(host, DWMCI_CTRL, ctrl);
92
93 ctrl = dwmci_readl(host, DWMCI_BMOD);
94 ctrl |= DWMCI_BMOD_IDMAC_FB | DWMCI_BMOD_IDMAC_EN;
95 dwmci_writel(host, DWMCI_BMOD, ctrl);
96
97 dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
98 dwmci_writel(host, DWMCI_BYTCNT, data->blocksize * data->blocks);
99}
100
Heiko Stuebner05fa06b2018-09-21 10:59:45 +0200101static int dwmci_fifo_ready(struct dwmci_host *host, u32 bit, u32 *len)
102{
103 u32 timeout = 20000;
104
105 *len = dwmci_readl(host, DWMCI_STATUS);
106 while (--timeout && (*len & bit)) {
107 udelay(200);
108 *len = dwmci_readl(host, DWMCI_STATUS);
109 }
110
111 if (!timeout) {
112 debug("%s: FIFO underflow timeout\n", __func__);
113 return -ETIMEDOUT;
114 }
115
116 return 0;
117}
118
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100119static unsigned int dwmci_get_timeout(struct mmc *mmc, const unsigned int size)
120{
121 unsigned int timeout;
122
Kever Yangc077c052019-08-29 15:42:41 +0800123 timeout = size * 8; /* counting in bits */
124 timeout *= 10; /* wait 10 times as long */
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100125 timeout /= mmc->clock;
126 timeout /= mmc->bus_width;
127 timeout /= mmc->ddr_mode ? 2 : 1;
Kever Yangc077c052019-08-29 15:42:41 +0800128 timeout *= 1000; /* counting in msec */
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100129 timeout = (timeout < 1000) ? 1000 : timeout;
130
131 return timeout;
132}
133
huang lina65f51b2015-11-17 14:20:22 +0800134static int dwmci_data_transfer(struct dwmci_host *host, struct mmc_data *data)
huang linf382eb82015-11-17 14:20:21 +0800135{
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100136 struct mmc *mmc = host->mmc;
huang linf382eb82015-11-17 14:20:21 +0800137 int ret = 0;
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100138 u32 timeout, mask, size, i, len = 0;
huang lina65f51b2015-11-17 14:20:22 +0800139 u32 *buf = NULL;
huang linf382eb82015-11-17 14:20:21 +0800140 ulong start = get_timer(0);
huang lina65f51b2015-11-17 14:20:22 +0800141 u32 fifo_depth = (((host->fifoth_val & RX_WMARK_MASK) >>
142 RX_WMARK_SHIFT) + 1) * 2;
143
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100144 size = data->blocksize * data->blocks;
huang lina65f51b2015-11-17 14:20:22 +0800145 if (data->flags == MMC_DATA_READ)
146 buf = (unsigned int *)data->dest;
147 else
148 buf = (unsigned int *)data->src;
huang linf382eb82015-11-17 14:20:21 +0800149
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100150 timeout = dwmci_get_timeout(mmc, size);
151
152 size /= 4;
153
huang linf382eb82015-11-17 14:20:21 +0800154 for (;;) {
155 mask = dwmci_readl(host, DWMCI_RINTSTS);
156 /* Error during data transfer. */
157 if (mask & (DWMCI_DATA_ERR | DWMCI_DATA_TOUT)) {
158 debug("%s: DATA ERROR!\n", __func__);
159 ret = -EINVAL;
160 break;
161 }
162
huang lina65f51b2015-11-17 14:20:22 +0800163 if (host->fifo_mode && size) {
Xu Ziyuan720724d2016-07-28 10:25:48 +0800164 len = 0;
Jacob Chen2b429032016-09-19 10:16:50 +0800165 if (data->flags == MMC_DATA_READ &&
166 (mask & DWMCI_INTMSK_RXDR)) {
167 while (size) {
Heiko Stuebner05fa06b2018-09-21 10:59:45 +0200168 ret = dwmci_fifo_ready(host,
169 DWMCI_FIFO_EMPTY,
170 &len);
171 if (ret < 0)
172 break;
173
huang lina65f51b2015-11-17 14:20:22 +0800174 len = (len >> DWMCI_FIFO_SHIFT) &
175 DWMCI_FIFO_MASK;
Xu Ziyuan2990e072016-07-28 10:25:47 +0800176 len = min(size, len);
huang lina65f51b2015-11-17 14:20:22 +0800177 for (i = 0; i < len; i++)
178 *buf++ =
179 dwmci_readl(host, DWMCI_DATA);
Jacob Chen2b429032016-09-19 10:16:50 +0800180 size = size > len ? (size - len) : 0;
huang lina65f51b2015-11-17 14:20:22 +0800181 }
Jacob Chen2b429032016-09-19 10:16:50 +0800182 dwmci_writel(host, DWMCI_RINTSTS,
183 DWMCI_INTMSK_RXDR);
184 } else if (data->flags == MMC_DATA_WRITE &&
185 (mask & DWMCI_INTMSK_TXDR)) {
186 while (size) {
Heiko Stuebner05fa06b2018-09-21 10:59:45 +0200187 ret = dwmci_fifo_ready(host,
188 DWMCI_FIFO_FULL,
189 &len);
190 if (ret < 0)
191 break;
192
huang lina65f51b2015-11-17 14:20:22 +0800193 len = fifo_depth - ((len >>
194 DWMCI_FIFO_SHIFT) &
195 DWMCI_FIFO_MASK);
Xu Ziyuan2990e072016-07-28 10:25:47 +0800196 len = min(size, len);
huang lina65f51b2015-11-17 14:20:22 +0800197 for (i = 0; i < len; i++)
198 dwmci_writel(host, DWMCI_DATA,
199 *buf++);
Jacob Chen2b429032016-09-19 10:16:50 +0800200 size = size > len ? (size - len) : 0;
huang lina65f51b2015-11-17 14:20:22 +0800201 }
Jacob Chen2b429032016-09-19 10:16:50 +0800202 dwmci_writel(host, DWMCI_RINTSTS,
203 DWMCI_INTMSK_TXDR);
huang lina65f51b2015-11-17 14:20:22 +0800204 }
huang lina65f51b2015-11-17 14:20:22 +0800205 }
206
huang linf382eb82015-11-17 14:20:21 +0800207 /* Data arrived correctly. */
208 if (mask & DWMCI_INTMSK_DTO) {
209 ret = 0;
210 break;
211 }
212
213 /* Check for timeout. */
214 if (get_timer(start) > timeout) {
215 debug("%s: Timeout waiting for data!\n",
216 __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900217 ret = -ETIMEDOUT;
huang linf382eb82015-11-17 14:20:21 +0800218 break;
219 }
220 }
221
222 dwmci_writel(host, DWMCI_RINTSTS, mask);
223
224 return ret;
225}
226
Jaehoon Chung757bff42012-10-15 19:10:29 +0000227static int dwmci_set_transfer_mode(struct dwmci_host *host,
228 struct mmc_data *data)
229{
230 unsigned long mode;
231
232 mode = DWMCI_CMD_DATA_EXP;
233 if (data->flags & MMC_DATA_WRITE)
234 mode |= DWMCI_CMD_RW;
235
236 return mode;
237}
238
Simon Glasse7881d82017-07-29 11:35:31 -0600239#ifdef CONFIG_DM_MMC
Jaehoon Chung56283472016-06-28 15:52:21 +0900240static int dwmci_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
Simon Glass691272f2016-06-12 23:30:23 -0600241 struct mmc_data *data)
242{
243 struct mmc *mmc = mmc_get_mmc_dev(dev);
244#else
Jaehoon Chung757bff42012-10-15 19:10:29 +0000245static int dwmci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
246 struct mmc_data *data)
247{
Simon Glass691272f2016-06-12 23:30:23 -0600248#endif
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200249 struct dwmci_host *host = mmc->priv;
Mischa Jonker2136d222013-07-26 14:08:14 +0200250 ALLOC_CACHE_ALIGN_BUFFER(struct dwmci_idmac, cur_idmac,
Mischa Jonker21bd5762013-07-26 16:18:40 +0200251 data ? DIV_ROUND_UP(data->blocks, 8) : 0);
Marek Vasut9042d972015-07-27 22:39:38 +0200252 int ret = 0, flags = 0, i;
Xu Ziyuan02ebd422016-07-19 09:38:22 +0800253 unsigned int timeout = 500;
Alexander Graf9b5b8b62016-03-04 01:09:52 +0100254 u32 retry = 100000;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000255 u32 mask, ctrl;
Amar9c50e352013-04-27 11:42:54 +0530256 ulong start = get_timer(0);
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400257 struct bounce_buffer bbstate;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000258
259 while (dwmci_readl(host, DWMCI_STATUS) & DWMCI_BUSY) {
Amar9c50e352013-04-27 11:42:54 +0530260 if (get_timer(start) > timeout) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600261 debug("%s: Timeout on data busy\n", __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900262 return -ETIMEDOUT;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000263 }
Jaehoon Chung757bff42012-10-15 19:10:29 +0000264 }
265
266 dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_ALL);
267
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400268 if (data) {
huang lina65f51b2015-11-17 14:20:22 +0800269 if (host->fifo_mode) {
270 dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
271 dwmci_writel(host, DWMCI_BYTCNT,
272 data->blocksize * data->blocks);
273 dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400274 } else {
huang lina65f51b2015-11-17 14:20:22 +0800275 if (data->flags == MMC_DATA_READ) {
Marek Vasut6ad5aec2019-03-23 18:45:27 +0100276 ret = bounce_buffer_start(&bbstate,
277 (void*)data->dest,
huang lina65f51b2015-11-17 14:20:22 +0800278 data->blocksize *
279 data->blocks, GEN_BB_WRITE);
280 } else {
Marek Vasut6ad5aec2019-03-23 18:45:27 +0100281 ret = bounce_buffer_start(&bbstate,
282 (void*)data->src,
huang lina65f51b2015-11-17 14:20:22 +0800283 data->blocksize *
284 data->blocks, GEN_BB_READ);
285 }
Marek Vasut6ad5aec2019-03-23 18:45:27 +0100286
287 if (ret)
288 return ret;
289
huang lina65f51b2015-11-17 14:20:22 +0800290 dwmci_prepare_data(host, data, cur_idmac,
291 bbstate.bounce_buffer);
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400292 }
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400293 }
Jaehoon Chung757bff42012-10-15 19:10:29 +0000294
Jaehoon Chung757bff42012-10-15 19:10:29 +0000295 dwmci_writel(host, DWMCI_CMDARG, cmd->cmdarg);
296
297 if (data)
298 flags = dwmci_set_transfer_mode(host, data);
299
300 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
301 return -1;
302
303 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
304 flags |= DWMCI_CMD_ABORT_STOP;
305 else
306 flags |= DWMCI_CMD_PRV_DAT_WAIT;
307
308 if (cmd->resp_type & MMC_RSP_PRESENT) {
309 flags |= DWMCI_CMD_RESP_EXP;
310 if (cmd->resp_type & MMC_RSP_136)
311 flags |= DWMCI_CMD_RESP_LENGTH;
312 }
313
314 if (cmd->resp_type & MMC_RSP_CRC)
315 flags |= DWMCI_CMD_CHECK_CRC;
316
317 flags |= (cmd->cmdidx | DWMCI_CMD_START | DWMCI_CMD_USE_HOLD_REG);
318
319 debug("Sending CMD%d\n",cmd->cmdidx);
320
321 dwmci_writel(host, DWMCI_CMD, flags);
322
323 for (i = 0; i < retry; i++) {
324 mask = dwmci_readl(host, DWMCI_RINTSTS);
325 if (mask & DWMCI_INTMSK_CDONE) {
326 if (!data)
327 dwmci_writel(host, DWMCI_RINTSTS, mask);
328 break;
329 }
330 }
331
Pavel Machekf33c9302014-09-05 12:49:48 +0200332 if (i == retry) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600333 debug("%s: Timeout.\n", __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900334 return -ETIMEDOUT;
Pavel Machekf33c9302014-09-05 12:49:48 +0200335 }
Jaehoon Chung757bff42012-10-15 19:10:29 +0000336
337 if (mask & DWMCI_INTMSK_RTO) {
Pavel Machekf33c9302014-09-05 12:49:48 +0200338 /*
339 * Timeout here is not necessarily fatal. (e)MMC cards
340 * will splat here when they receive CMD55 as they do
341 * not support this command and that is exactly the way
342 * to tell them apart from SD cards. Thus, this output
343 * below shall be debug(). eMMC cards also do not favor
344 * CMD8, please keep that in mind.
345 */
346 debug("%s: Response Timeout.\n", __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900347 return -ETIMEDOUT;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000348 } else if (mask & DWMCI_INTMSK_RE) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600349 debug("%s: Response Error.\n", __func__);
350 return -EIO;
Marek Vasut26cc40d2018-11-06 23:42:11 +0100351 } else if ((cmd->resp_type & MMC_RSP_CRC) &&
352 (mask & DWMCI_INTMSK_RCRC)) {
353 debug("%s: Response CRC Error.\n", __func__);
354 return -EIO;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000355 }
356
357
358 if (cmd->resp_type & MMC_RSP_PRESENT) {
359 if (cmd->resp_type & MMC_RSP_136) {
360 cmd->response[0] = dwmci_readl(host, DWMCI_RESP3);
361 cmd->response[1] = dwmci_readl(host, DWMCI_RESP2);
362 cmd->response[2] = dwmci_readl(host, DWMCI_RESP1);
363 cmd->response[3] = dwmci_readl(host, DWMCI_RESP0);
364 } else {
365 cmd->response[0] = dwmci_readl(host, DWMCI_RESP0);
366 }
367 }
368
369 if (data) {
huang lina65f51b2015-11-17 14:20:22 +0800370 ret = dwmci_data_transfer(host, data);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000371
huang lina65f51b2015-11-17 14:20:22 +0800372 /* only dma mode need it */
373 if (!host->fifo_mode) {
Ley Foon Tan79975992018-12-20 17:55:41 +0800374 if (data->flags == MMC_DATA_READ)
375 mask = DWMCI_IDINTEN_RI;
376 else
377 mask = DWMCI_IDINTEN_TI;
378 ret = wait_for_bit_le32(host->ioaddr + DWMCI_IDSTS,
379 mask, true, 1000, false);
380 if (ret)
381 debug("%s: DWMCI_IDINTEN mask 0x%x timeout.\n",
382 __func__, mask);
383 /* clear interrupts */
384 dwmci_writel(host, DWMCI_IDSTS, DWMCI_IDINTEN_MASK);
385
huang lina65f51b2015-11-17 14:20:22 +0800386 ctrl = dwmci_readl(host, DWMCI_CTRL);
387 ctrl &= ~(DWMCI_DMA_EN);
388 dwmci_writel(host, DWMCI_CTRL, ctrl);
389 bounce_buffer_stop(&bbstate);
390 }
Jaehoon Chung757bff42012-10-15 19:10:29 +0000391 }
392
393 udelay(100);
394
Marek Vasut9042d972015-07-27 22:39:38 +0200395 return ret;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000396}
397
398static int dwmci_setup_bus(struct dwmci_host *host, u32 freq)
399{
400 u32 div, status;
401 int timeout = 10000;
402 unsigned long sclk;
403
Amar9c50e352013-04-27 11:42:54 +0530404 if ((freq == host->clock) || (freq == 0))
Jaehoon Chung757bff42012-10-15 19:10:29 +0000405 return 0;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000406 /*
Pavel Machekf33c9302014-09-05 12:49:48 +0200407 * If host->get_mmc_clk isn't defined,
Jaehoon Chung757bff42012-10-15 19:10:29 +0000408 * then assume that host->bus_hz is source clock value.
Pavel Machekf33c9302014-09-05 12:49:48 +0200409 * host->bus_hz should be set by user.
Jaehoon Chung757bff42012-10-15 19:10:29 +0000410 */
Jaehoon Chungb44fe832013-10-06 18:59:31 +0900411 if (host->get_mmc_clk)
Simon Glasse3563f22015-08-30 16:55:15 -0600412 sclk = host->get_mmc_clk(host, freq);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000413 else if (host->bus_hz)
414 sclk = host->bus_hz;
415 else {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600416 debug("%s: Didn't get source clock value.\n", __func__);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000417 return -EINVAL;
418 }
419
Chin Liang See6ace1532014-06-10 01:26:52 -0500420 if (sclk == freq)
421 div = 0; /* bypass mode */
422 else
423 div = DIV_ROUND_UP(sclk, 2 * freq);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000424
425 dwmci_writel(host, DWMCI_CLKENA, 0);
426 dwmci_writel(host, DWMCI_CLKSRC, 0);
427
428 dwmci_writel(host, DWMCI_CLKDIV, div);
429 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
430 DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
431
432 do {
433 status = dwmci_readl(host, DWMCI_CMD);
434 if (timeout-- < 0) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600435 debug("%s: Timeout!\n", __func__);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000436 return -ETIMEDOUT;
437 }
438 } while (status & DWMCI_CMD_START);
439
440 dwmci_writel(host, DWMCI_CLKENA, DWMCI_CLKEN_ENABLE |
441 DWMCI_CLKEN_LOW_PWR);
442
443 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
444 DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
445
446 timeout = 10000;
447 do {
448 status = dwmci_readl(host, DWMCI_CMD);
449 if (timeout-- < 0) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600450 debug("%s: Timeout!\n", __func__);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000451 return -ETIMEDOUT;
452 }
453 } while (status & DWMCI_CMD_START);
454
455 host->clock = freq;
456
457 return 0;
458}
459
Simon Glasse7881d82017-07-29 11:35:31 -0600460#ifdef CONFIG_DM_MMC
Jaehoon Chung56283472016-06-28 15:52:21 +0900461static int dwmci_set_ios(struct udevice *dev)
Simon Glass691272f2016-06-12 23:30:23 -0600462{
463 struct mmc *mmc = mmc_get_mmc_dev(dev);
464#else
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900465static int dwmci_set_ios(struct mmc *mmc)
Jaehoon Chung757bff42012-10-15 19:10:29 +0000466{
Simon Glass691272f2016-06-12 23:30:23 -0600467#endif
Jaehoon Chung045bdcd2014-05-16 13:59:55 +0900468 struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
469 u32 ctype, regs;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000470
Pavel Machekf33c9302014-09-05 12:49:48 +0200471 debug("Buswidth = %d, clock: %d\n", mmc->bus_width, mmc->clock);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000472
473 dwmci_setup_bus(host, mmc->clock);
474 switch (mmc->bus_width) {
475 case 8:
476 ctype = DWMCI_CTYPE_8BIT;
477 break;
478 case 4:
479 ctype = DWMCI_CTYPE_4BIT;
480 break;
481 default:
482 ctype = DWMCI_CTYPE_1BIT;
483 break;
484 }
485
486 dwmci_writel(host, DWMCI_CTYPE, ctype);
487
Jaehoon Chung045bdcd2014-05-16 13:59:55 +0900488 regs = dwmci_readl(host, DWMCI_UHS_REG);
Andrew Gabbasov2b8a9692014-12-01 06:59:12 -0600489 if (mmc->ddr_mode)
Jaehoon Chung045bdcd2014-05-16 13:59:55 +0900490 regs |= DWMCI_DDR_MODE;
491 else
Jaehoon Chungafc9e2b2015-01-14 17:37:53 +0900492 regs &= ~DWMCI_DDR_MODE;
Jaehoon Chung045bdcd2014-05-16 13:59:55 +0900493
494 dwmci_writel(host, DWMCI_UHS_REG, regs);
495
Jaehoon Chung757bff42012-10-15 19:10:29 +0000496 if (host->clksel)
497 host->clksel(host);
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900498
Urja Rannikko2b157012019-05-13 13:25:27 +0000499#if CONFIG_IS_ENABLED(DM_REGULATOR)
500 if (mmc->vqmmc_supply) {
501 int ret;
502
503 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
504 regulator_set_value(mmc->vqmmc_supply, 1800000);
505 else
506 regulator_set_value(mmc->vqmmc_supply, 3300000);
507
508 ret = regulator_set_enable_if_allowed(mmc->vqmmc_supply, true);
509 if (ret)
510 return ret;
511 }
512#endif
513
Simon Glass691272f2016-06-12 23:30:23 -0600514 return 0;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000515}
516
517static int dwmci_init(struct mmc *mmc)
518{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200519 struct dwmci_host *host = mmc->priv;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000520
Jaehoon Chung18ab6752013-11-29 20:08:57 +0900521 if (host->board_init)
522 host->board_init(host);
Rajeshwari Shinde6f0b7ca2013-10-29 12:53:13 +0530523
Jaehoon Chung757bff42012-10-15 19:10:29 +0000524 dwmci_writel(host, DWMCI_PWREN, 1);
525
526 if (!dwmci_wait_reset(host, DWMCI_RESET_ALL)) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600527 debug("%s[%d] Fail-reset!!\n", __func__, __LINE__);
528 return -EIO;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000529 }
530
Amar9c50e352013-04-27 11:42:54 +0530531 /* Enumerate at 400KHz */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200532 dwmci_setup_bus(host, mmc->cfg->f_min);
Amar9c50e352013-04-27 11:42:54 +0530533
Jaehoon Chung757bff42012-10-15 19:10:29 +0000534 dwmci_writel(host, DWMCI_RINTSTS, 0xFFFFFFFF);
535 dwmci_writel(host, DWMCI_INTMASK, 0);
536
537 dwmci_writel(host, DWMCI_TMOUT, 0xFFFFFFFF);
538
539 dwmci_writel(host, DWMCI_IDINTEN, 0);
540 dwmci_writel(host, DWMCI_BMOD, 1);
541
Simon Glass760177d2015-08-06 20:16:29 -0600542 if (!host->fifoth_val) {
543 uint32_t fifo_size;
544
545 fifo_size = dwmci_readl(host, DWMCI_FIFOTH);
546 fifo_size = ((fifo_size & RX_WMARK_MASK) >> RX_WMARK_SHIFT) + 1;
547 host->fifoth_val = MSIZE(0x2) | RX_WMARK(fifo_size / 2 - 1) |
548 TX_WMARK(fifo_size / 2);
Amar9c50e352013-04-27 11:42:54 +0530549 }
Simon Glass760177d2015-08-06 20:16:29 -0600550 dwmci_writel(host, DWMCI_FIFOTH, host->fifoth_val);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000551
552 dwmci_writel(host, DWMCI_CLKENA, 0);
553 dwmci_writel(host, DWMCI_CLKSRC, 0);
554
Ley Foon Tan79975992018-12-20 17:55:41 +0800555 if (!host->fifo_mode)
556 dwmci_writel(host, DWMCI_IDINTEN, DWMCI_IDINTEN_MASK);
557
Jaehoon Chung757bff42012-10-15 19:10:29 +0000558 return 0;
559}
560
Simon Glasse7881d82017-07-29 11:35:31 -0600561#ifdef CONFIG_DM_MMC
Simon Glass691272f2016-06-12 23:30:23 -0600562int dwmci_probe(struct udevice *dev)
563{
564 struct mmc *mmc = mmc_get_mmc_dev(dev);
565
566 return dwmci_init(mmc);
567}
568
569const struct dm_mmc_ops dm_dwmci_ops = {
570 .send_cmd = dwmci_send_cmd,
571 .set_ios = dwmci_set_ios,
572};
573
574#else
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200575static const struct mmc_ops dwmci_ops = {
576 .send_cmd = dwmci_send_cmd,
577 .set_ios = dwmci_set_ios,
578 .init = dwmci_init,
579};
Simon Glass691272f2016-06-12 23:30:23 -0600580#endif
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200581
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900582void dwmci_setup_cfg(struct mmc_config *cfg, struct dwmci_host *host,
583 u32 max_clk, u32 min_clk)
Simon Glass5e6ff812016-05-14 14:03:07 -0600584{
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900585 cfg->name = host->name;
Simon Glasse7881d82017-07-29 11:35:31 -0600586#ifndef CONFIG_DM_MMC
Simon Glass5e6ff812016-05-14 14:03:07 -0600587 cfg->ops = &dwmci_ops;
Simon Glass691272f2016-06-12 23:30:23 -0600588#endif
Simon Glass5e6ff812016-05-14 14:03:07 -0600589 cfg->f_min = min_clk;
590 cfg->f_max = max_clk;
591
592 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
593
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900594 cfg->host_caps = host->caps;
Simon Glass5e6ff812016-05-14 14:03:07 -0600595
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900596 if (host->buswidth == 8) {
Simon Glass5e6ff812016-05-14 14:03:07 -0600597 cfg->host_caps |= MMC_MODE_8BIT;
598 cfg->host_caps &= ~MMC_MODE_4BIT;
599 } else {
600 cfg->host_caps |= MMC_MODE_4BIT;
601 cfg->host_caps &= ~MMC_MODE_8BIT;
602 }
603 cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz;
604
605 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
606}
607
608#ifdef CONFIG_BLK
609int dwmci_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg)
610{
611 return mmc_bind(dev, mmc, cfg);
612}
613#else
Jaehoon Chung757bff42012-10-15 19:10:29 +0000614int add_dwmci(struct dwmci_host *host, u32 max_clk, u32 min_clk)
615{
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900616 dwmci_setup_cfg(&host->cfg, host, max_clk, min_clk);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000617
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200618 host->mmc = mmc_create(&host->cfg, host);
619 if (host->mmc == NULL)
620 return -1;
621
622 return 0;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000623}
Simon Glass5e6ff812016-05-14 14:03:07 -0600624#endif