blob: ebe7bcdd900591f80b26b2558b7f31110764ed09 [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 Glass1c87ffe2015-08-06 20:16:27 -060010#include <errno.h>
Jaehoon Chung757bff42012-10-15 19:10:29 +000011#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060012#include <memalign.h>
Jaehoon Chung757bff42012-10-15 19:10:29 +000013#include <mmc.h>
14#include <dwmmc.h>
Ley Foon Tan79975992018-12-20 17:55:41 +080015#include <wait_bit.h>
Urja Rannikko2b157012019-05-13 13:25:27 +000016#include <power/regulator.h>
Jaehoon Chung757bff42012-10-15 19:10:29 +000017
18#define PAGE_SIZE 4096
19
20static int dwmci_wait_reset(struct dwmci_host *host, u32 value)
21{
22 unsigned long timeout = 1000;
23 u32 ctrl;
24
25 dwmci_writel(host, DWMCI_CTRL, value);
26
27 while (timeout--) {
28 ctrl = dwmci_readl(host, DWMCI_CTRL);
29 if (!(ctrl & DWMCI_RESET_ALL))
30 return 1;
31 }
32 return 0;
33}
34
35static void dwmci_set_idma_desc(struct dwmci_idmac *idmac,
36 u32 desc0, u32 desc1, u32 desc2)
37{
38 struct dwmci_idmac *desc = idmac;
39
40 desc->flags = desc0;
41 desc->cnt = desc1;
42 desc->addr = desc2;
Prabhakar Kushwaha41f7be32015-10-25 13:18:25 +053043 desc->next_addr = (ulong)desc + sizeof(struct dwmci_idmac);
Jaehoon Chung757bff42012-10-15 19:10:29 +000044}
45
46static void dwmci_prepare_data(struct dwmci_host *host,
Alexey Brodkin2a7a2102013-12-26 15:29:07 +040047 struct mmc_data *data,
48 struct dwmci_idmac *cur_idmac,
49 void *bounce_buffer)
Jaehoon Chung757bff42012-10-15 19:10:29 +000050{
51 unsigned long ctrl;
52 unsigned int i = 0, flags, cnt, blk_cnt;
Alexey Brodkin2a7a2102013-12-26 15:29:07 +040053 ulong data_start, data_end;
Jaehoon Chung757bff42012-10-15 19:10:29 +000054
55
56 blk_cnt = data->blocks;
57
58 dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
59
Ley Foon Tan79975992018-12-20 17:55:41 +080060 /* Clear IDMAC interrupt */
61 dwmci_writel(host, DWMCI_IDSTS, 0xFFFFFFFF);
62
Jaehoon Chung757bff42012-10-15 19:10:29 +000063 data_start = (ulong)cur_idmac;
Prabhakar Kushwaha41f7be32015-10-25 13:18:25 +053064 dwmci_writel(host, DWMCI_DBADDR, (ulong)cur_idmac);
Jaehoon Chung757bff42012-10-15 19:10:29 +000065
Jaehoon Chung757bff42012-10-15 19:10:29 +000066 do {
67 flags = DWMCI_IDMAC_OWN | DWMCI_IDMAC_CH ;
68 flags |= (i == 0) ? DWMCI_IDMAC_FS : 0;
69 if (blk_cnt <= 8) {
70 flags |= DWMCI_IDMAC_LD;
71 cnt = data->blocksize * blk_cnt;
72 } else
73 cnt = data->blocksize * 8;
74
75 dwmci_set_idma_desc(cur_idmac, flags, cnt,
Prabhakar Kushwaha41f7be32015-10-25 13:18:25 +053076 (ulong)bounce_buffer + (i * PAGE_SIZE));
Jaehoon Chung757bff42012-10-15 19:10:29 +000077
Marek Vasutbdb5df12019-02-13 20:16:20 +010078 cur_idmac++;
Mischa Jonker21bd5762013-07-26 16:18:40 +020079 if (blk_cnt <= 8)
Jaehoon Chung757bff42012-10-15 19:10:29 +000080 break;
81 blk_cnt -= 8;
Jaehoon Chung757bff42012-10-15 19:10:29 +000082 i++;
83 } while(1);
84
85 data_end = (ulong)cur_idmac;
Marek Vasutbdb5df12019-02-13 20:16:20 +010086 flush_dcache_range(data_start, roundup(data_end, ARCH_DMA_MINALIGN));
Jaehoon Chung757bff42012-10-15 19:10:29 +000087
88 ctrl = dwmci_readl(host, DWMCI_CTRL);
89 ctrl |= DWMCI_IDMAC_EN | DWMCI_DMA_EN;
90 dwmci_writel(host, DWMCI_CTRL, ctrl);
91
92 ctrl = dwmci_readl(host, DWMCI_BMOD);
93 ctrl |= DWMCI_BMOD_IDMAC_FB | DWMCI_BMOD_IDMAC_EN;
94 dwmci_writel(host, DWMCI_BMOD, ctrl);
95
96 dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
97 dwmci_writel(host, DWMCI_BYTCNT, data->blocksize * data->blocks);
98}
99
Heiko Stuebner05fa06b2018-09-21 10:59:45 +0200100static int dwmci_fifo_ready(struct dwmci_host *host, u32 bit, u32 *len)
101{
102 u32 timeout = 20000;
103
104 *len = dwmci_readl(host, DWMCI_STATUS);
105 while (--timeout && (*len & bit)) {
106 udelay(200);
107 *len = dwmci_readl(host, DWMCI_STATUS);
108 }
109
110 if (!timeout) {
111 debug("%s: FIFO underflow timeout\n", __func__);
112 return -ETIMEDOUT;
113 }
114
115 return 0;
116}
117
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100118static unsigned int dwmci_get_timeout(struct mmc *mmc, const unsigned int size)
119{
120 unsigned int timeout;
121
Kever Yangc077c052019-08-29 15:42:41 +0800122 timeout = size * 8; /* counting in bits */
123 timeout *= 10; /* wait 10 times as long */
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100124 timeout /= mmc->clock;
125 timeout /= mmc->bus_width;
126 timeout /= mmc->ddr_mode ? 2 : 1;
Kever Yangc077c052019-08-29 15:42:41 +0800127 timeout *= 1000; /* counting in msec */
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100128 timeout = (timeout < 1000) ? 1000 : timeout;
129
130 return timeout;
131}
132
huang lina65f51b2015-11-17 14:20:22 +0800133static int dwmci_data_transfer(struct dwmci_host *host, struct mmc_data *data)
huang linf382eb82015-11-17 14:20:21 +0800134{
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100135 struct mmc *mmc = host->mmc;
huang linf382eb82015-11-17 14:20:21 +0800136 int ret = 0;
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100137 u32 timeout, mask, size, i, len = 0;
huang lina65f51b2015-11-17 14:20:22 +0800138 u32 *buf = NULL;
huang linf382eb82015-11-17 14:20:21 +0800139 ulong start = get_timer(0);
huang lina65f51b2015-11-17 14:20:22 +0800140 u32 fifo_depth = (((host->fifoth_val & RX_WMARK_MASK) >>
141 RX_WMARK_SHIFT) + 1) * 2;
142
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100143 size = data->blocksize * data->blocks;
huang lina65f51b2015-11-17 14:20:22 +0800144 if (data->flags == MMC_DATA_READ)
145 buf = (unsigned int *)data->dest;
146 else
147 buf = (unsigned int *)data->src;
huang linf382eb82015-11-17 14:20:21 +0800148
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100149 timeout = dwmci_get_timeout(mmc, size);
150
151 size /= 4;
152
huang linf382eb82015-11-17 14:20:21 +0800153 for (;;) {
154 mask = dwmci_readl(host, DWMCI_RINTSTS);
155 /* Error during data transfer. */
156 if (mask & (DWMCI_DATA_ERR | DWMCI_DATA_TOUT)) {
157 debug("%s: DATA ERROR!\n", __func__);
158 ret = -EINVAL;
159 break;
160 }
161
huang lina65f51b2015-11-17 14:20:22 +0800162 if (host->fifo_mode && size) {
Xu Ziyuan720724d2016-07-28 10:25:48 +0800163 len = 0;
Jacob Chen2b429032016-09-19 10:16:50 +0800164 if (data->flags == MMC_DATA_READ &&
165 (mask & DWMCI_INTMSK_RXDR)) {
166 while (size) {
Heiko Stuebner05fa06b2018-09-21 10:59:45 +0200167 ret = dwmci_fifo_ready(host,
168 DWMCI_FIFO_EMPTY,
169 &len);
170 if (ret < 0)
171 break;
172
huang lina65f51b2015-11-17 14:20:22 +0800173 len = (len >> DWMCI_FIFO_SHIFT) &
174 DWMCI_FIFO_MASK;
Xu Ziyuan2990e072016-07-28 10:25:47 +0800175 len = min(size, len);
huang lina65f51b2015-11-17 14:20:22 +0800176 for (i = 0; i < len; i++)
177 *buf++ =
178 dwmci_readl(host, DWMCI_DATA);
Jacob Chen2b429032016-09-19 10:16:50 +0800179 size = size > len ? (size - len) : 0;
huang lina65f51b2015-11-17 14:20:22 +0800180 }
Jacob Chen2b429032016-09-19 10:16:50 +0800181 dwmci_writel(host, DWMCI_RINTSTS,
182 DWMCI_INTMSK_RXDR);
183 } else if (data->flags == MMC_DATA_WRITE &&
184 (mask & DWMCI_INTMSK_TXDR)) {
185 while (size) {
Heiko Stuebner05fa06b2018-09-21 10:59:45 +0200186 ret = dwmci_fifo_ready(host,
187 DWMCI_FIFO_FULL,
188 &len);
189 if (ret < 0)
190 break;
191
huang lina65f51b2015-11-17 14:20:22 +0800192 len = fifo_depth - ((len >>
193 DWMCI_FIFO_SHIFT) &
194 DWMCI_FIFO_MASK);
Xu Ziyuan2990e072016-07-28 10:25:47 +0800195 len = min(size, len);
huang lina65f51b2015-11-17 14:20:22 +0800196 for (i = 0; i < len; i++)
197 dwmci_writel(host, DWMCI_DATA,
198 *buf++);
Jacob Chen2b429032016-09-19 10:16:50 +0800199 size = size > len ? (size - len) : 0;
huang lina65f51b2015-11-17 14:20:22 +0800200 }
Jacob Chen2b429032016-09-19 10:16:50 +0800201 dwmci_writel(host, DWMCI_RINTSTS,
202 DWMCI_INTMSK_TXDR);
huang lina65f51b2015-11-17 14:20:22 +0800203 }
huang lina65f51b2015-11-17 14:20:22 +0800204 }
205
huang linf382eb82015-11-17 14:20:21 +0800206 /* Data arrived correctly. */
207 if (mask & DWMCI_INTMSK_DTO) {
208 ret = 0;
209 break;
210 }
211
212 /* Check for timeout. */
213 if (get_timer(start) > timeout) {
214 debug("%s: Timeout waiting for data!\n",
215 __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900216 ret = -ETIMEDOUT;
huang linf382eb82015-11-17 14:20:21 +0800217 break;
218 }
219 }
220
221 dwmci_writel(host, DWMCI_RINTSTS, mask);
222
223 return ret;
224}
225
Jaehoon Chung757bff42012-10-15 19:10:29 +0000226static int dwmci_set_transfer_mode(struct dwmci_host *host,
227 struct mmc_data *data)
228{
229 unsigned long mode;
230
231 mode = DWMCI_CMD_DATA_EXP;
232 if (data->flags & MMC_DATA_WRITE)
233 mode |= DWMCI_CMD_RW;
234
235 return mode;
236}
237
Simon Glasse7881d82017-07-29 11:35:31 -0600238#ifdef CONFIG_DM_MMC
Jaehoon Chung56283472016-06-28 15:52:21 +0900239static int dwmci_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
Simon Glass691272f2016-06-12 23:30:23 -0600240 struct mmc_data *data)
241{
242 struct mmc *mmc = mmc_get_mmc_dev(dev);
243#else
Jaehoon Chung757bff42012-10-15 19:10:29 +0000244static int dwmci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
245 struct mmc_data *data)
246{
Simon Glass691272f2016-06-12 23:30:23 -0600247#endif
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200248 struct dwmci_host *host = mmc->priv;
Mischa Jonker2136d222013-07-26 14:08:14 +0200249 ALLOC_CACHE_ALIGN_BUFFER(struct dwmci_idmac, cur_idmac,
Mischa Jonker21bd5762013-07-26 16:18:40 +0200250 data ? DIV_ROUND_UP(data->blocks, 8) : 0);
Marek Vasut9042d972015-07-27 22:39:38 +0200251 int ret = 0, flags = 0, i;
Xu Ziyuan02ebd422016-07-19 09:38:22 +0800252 unsigned int timeout = 500;
Alexander Graf9b5b8b62016-03-04 01:09:52 +0100253 u32 retry = 100000;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000254 u32 mask, ctrl;
Amar9c50e352013-04-27 11:42:54 +0530255 ulong start = get_timer(0);
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400256 struct bounce_buffer bbstate;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000257
258 while (dwmci_readl(host, DWMCI_STATUS) & DWMCI_BUSY) {
Amar9c50e352013-04-27 11:42:54 +0530259 if (get_timer(start) > timeout) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600260 debug("%s: Timeout on data busy\n", __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900261 return -ETIMEDOUT;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000262 }
Jaehoon Chung757bff42012-10-15 19:10:29 +0000263 }
264
265 dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_ALL);
266
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400267 if (data) {
huang lina65f51b2015-11-17 14:20:22 +0800268 if (host->fifo_mode) {
269 dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
270 dwmci_writel(host, DWMCI_BYTCNT,
271 data->blocksize * data->blocks);
272 dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400273 } else {
huang lina65f51b2015-11-17 14:20:22 +0800274 if (data->flags == MMC_DATA_READ) {
Marek Vasut6ad5aec2019-03-23 18:45:27 +0100275 ret = bounce_buffer_start(&bbstate,
276 (void*)data->dest,
huang lina65f51b2015-11-17 14:20:22 +0800277 data->blocksize *
278 data->blocks, GEN_BB_WRITE);
279 } else {
Marek Vasut6ad5aec2019-03-23 18:45:27 +0100280 ret = bounce_buffer_start(&bbstate,
281 (void*)data->src,
huang lina65f51b2015-11-17 14:20:22 +0800282 data->blocksize *
283 data->blocks, GEN_BB_READ);
284 }
Marek Vasut6ad5aec2019-03-23 18:45:27 +0100285
286 if (ret)
287 return ret;
288
huang lina65f51b2015-11-17 14:20:22 +0800289 dwmci_prepare_data(host, data, cur_idmac,
290 bbstate.bounce_buffer);
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400291 }
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400292 }
Jaehoon Chung757bff42012-10-15 19:10:29 +0000293
Jaehoon Chung757bff42012-10-15 19:10:29 +0000294 dwmci_writel(host, DWMCI_CMDARG, cmd->cmdarg);
295
296 if (data)
297 flags = dwmci_set_transfer_mode(host, data);
298
299 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
300 return -1;
301
302 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
303 flags |= DWMCI_CMD_ABORT_STOP;
304 else
305 flags |= DWMCI_CMD_PRV_DAT_WAIT;
306
307 if (cmd->resp_type & MMC_RSP_PRESENT) {
308 flags |= DWMCI_CMD_RESP_EXP;
309 if (cmd->resp_type & MMC_RSP_136)
310 flags |= DWMCI_CMD_RESP_LENGTH;
311 }
312
313 if (cmd->resp_type & MMC_RSP_CRC)
314 flags |= DWMCI_CMD_CHECK_CRC;
315
316 flags |= (cmd->cmdidx | DWMCI_CMD_START | DWMCI_CMD_USE_HOLD_REG);
317
318 debug("Sending CMD%d\n",cmd->cmdidx);
319
320 dwmci_writel(host, DWMCI_CMD, flags);
321
322 for (i = 0; i < retry; i++) {
323 mask = dwmci_readl(host, DWMCI_RINTSTS);
324 if (mask & DWMCI_INTMSK_CDONE) {
325 if (!data)
326 dwmci_writel(host, DWMCI_RINTSTS, mask);
327 break;
328 }
329 }
330
Pavel Machekf33c9302014-09-05 12:49:48 +0200331 if (i == retry) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600332 debug("%s: Timeout.\n", __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900333 return -ETIMEDOUT;
Pavel Machekf33c9302014-09-05 12:49:48 +0200334 }
Jaehoon Chung757bff42012-10-15 19:10:29 +0000335
336 if (mask & DWMCI_INTMSK_RTO) {
Pavel Machekf33c9302014-09-05 12:49:48 +0200337 /*
338 * Timeout here is not necessarily fatal. (e)MMC cards
339 * will splat here when they receive CMD55 as they do
340 * not support this command and that is exactly the way
341 * to tell them apart from SD cards. Thus, this output
342 * below shall be debug(). eMMC cards also do not favor
343 * CMD8, please keep that in mind.
344 */
345 debug("%s: Response Timeout.\n", __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900346 return -ETIMEDOUT;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000347 } else if (mask & DWMCI_INTMSK_RE) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600348 debug("%s: Response Error.\n", __func__);
349 return -EIO;
Marek Vasut26cc40d2018-11-06 23:42:11 +0100350 } else if ((cmd->resp_type & MMC_RSP_CRC) &&
351 (mask & DWMCI_INTMSK_RCRC)) {
352 debug("%s: Response CRC Error.\n", __func__);
353 return -EIO;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000354 }
355
356
357 if (cmd->resp_type & MMC_RSP_PRESENT) {
358 if (cmd->resp_type & MMC_RSP_136) {
359 cmd->response[0] = dwmci_readl(host, DWMCI_RESP3);
360 cmd->response[1] = dwmci_readl(host, DWMCI_RESP2);
361 cmd->response[2] = dwmci_readl(host, DWMCI_RESP1);
362 cmd->response[3] = dwmci_readl(host, DWMCI_RESP0);
363 } else {
364 cmd->response[0] = dwmci_readl(host, DWMCI_RESP0);
365 }
366 }
367
368 if (data) {
huang lina65f51b2015-11-17 14:20:22 +0800369 ret = dwmci_data_transfer(host, data);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000370
huang lina65f51b2015-11-17 14:20:22 +0800371 /* only dma mode need it */
372 if (!host->fifo_mode) {
Ley Foon Tan79975992018-12-20 17:55:41 +0800373 if (data->flags == MMC_DATA_READ)
374 mask = DWMCI_IDINTEN_RI;
375 else
376 mask = DWMCI_IDINTEN_TI;
377 ret = wait_for_bit_le32(host->ioaddr + DWMCI_IDSTS,
378 mask, true, 1000, false);
379 if (ret)
380 debug("%s: DWMCI_IDINTEN mask 0x%x timeout.\n",
381 __func__, mask);
382 /* clear interrupts */
383 dwmci_writel(host, DWMCI_IDSTS, DWMCI_IDINTEN_MASK);
384
huang lina65f51b2015-11-17 14:20:22 +0800385 ctrl = dwmci_readl(host, DWMCI_CTRL);
386 ctrl &= ~(DWMCI_DMA_EN);
387 dwmci_writel(host, DWMCI_CTRL, ctrl);
388 bounce_buffer_stop(&bbstate);
389 }
Jaehoon Chung757bff42012-10-15 19:10:29 +0000390 }
391
392 udelay(100);
393
Marek Vasut9042d972015-07-27 22:39:38 +0200394 return ret;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000395}
396
397static int dwmci_setup_bus(struct dwmci_host *host, u32 freq)
398{
399 u32 div, status;
400 int timeout = 10000;
401 unsigned long sclk;
402
Amar9c50e352013-04-27 11:42:54 +0530403 if ((freq == host->clock) || (freq == 0))
Jaehoon Chung757bff42012-10-15 19:10:29 +0000404 return 0;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000405 /*
Pavel Machekf33c9302014-09-05 12:49:48 +0200406 * If host->get_mmc_clk isn't defined,
Jaehoon Chung757bff42012-10-15 19:10:29 +0000407 * then assume that host->bus_hz is source clock value.
Pavel Machekf33c9302014-09-05 12:49:48 +0200408 * host->bus_hz should be set by user.
Jaehoon Chung757bff42012-10-15 19:10:29 +0000409 */
Jaehoon Chungb44fe832013-10-06 18:59:31 +0900410 if (host->get_mmc_clk)
Simon Glasse3563f22015-08-30 16:55:15 -0600411 sclk = host->get_mmc_clk(host, freq);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000412 else if (host->bus_hz)
413 sclk = host->bus_hz;
414 else {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600415 debug("%s: Didn't get source clock value.\n", __func__);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000416 return -EINVAL;
417 }
418
Chin Liang See6ace1532014-06-10 01:26:52 -0500419 if (sclk == freq)
420 div = 0; /* bypass mode */
421 else
422 div = DIV_ROUND_UP(sclk, 2 * freq);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000423
424 dwmci_writel(host, DWMCI_CLKENA, 0);
425 dwmci_writel(host, DWMCI_CLKSRC, 0);
426
427 dwmci_writel(host, DWMCI_CLKDIV, div);
428 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
429 DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
430
431 do {
432 status = dwmci_readl(host, DWMCI_CMD);
433 if (timeout-- < 0) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600434 debug("%s: Timeout!\n", __func__);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000435 return -ETIMEDOUT;
436 }
437 } while (status & DWMCI_CMD_START);
438
439 dwmci_writel(host, DWMCI_CLKENA, DWMCI_CLKEN_ENABLE |
440 DWMCI_CLKEN_LOW_PWR);
441
442 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
443 DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
444
445 timeout = 10000;
446 do {
447 status = dwmci_readl(host, DWMCI_CMD);
448 if (timeout-- < 0) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600449 debug("%s: Timeout!\n", __func__);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000450 return -ETIMEDOUT;
451 }
452 } while (status & DWMCI_CMD_START);
453
454 host->clock = freq;
455
456 return 0;
457}
458
Simon Glasse7881d82017-07-29 11:35:31 -0600459#ifdef CONFIG_DM_MMC
Jaehoon Chung56283472016-06-28 15:52:21 +0900460static int dwmci_set_ios(struct udevice *dev)
Simon Glass691272f2016-06-12 23:30:23 -0600461{
462 struct mmc *mmc = mmc_get_mmc_dev(dev);
463#else
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900464static int dwmci_set_ios(struct mmc *mmc)
Jaehoon Chung757bff42012-10-15 19:10:29 +0000465{
Simon Glass691272f2016-06-12 23:30:23 -0600466#endif
Jaehoon Chung045bdcd2014-05-16 13:59:55 +0900467 struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
468 u32 ctype, regs;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000469
Pavel Machekf33c9302014-09-05 12:49:48 +0200470 debug("Buswidth = %d, clock: %d\n", mmc->bus_width, mmc->clock);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000471
472 dwmci_setup_bus(host, mmc->clock);
473 switch (mmc->bus_width) {
474 case 8:
475 ctype = DWMCI_CTYPE_8BIT;
476 break;
477 case 4:
478 ctype = DWMCI_CTYPE_4BIT;
479 break;
480 default:
481 ctype = DWMCI_CTYPE_1BIT;
482 break;
483 }
484
485 dwmci_writel(host, DWMCI_CTYPE, ctype);
486
Jaehoon Chung045bdcd2014-05-16 13:59:55 +0900487 regs = dwmci_readl(host, DWMCI_UHS_REG);
Andrew Gabbasov2b8a9692014-12-01 06:59:12 -0600488 if (mmc->ddr_mode)
Jaehoon Chung045bdcd2014-05-16 13:59:55 +0900489 regs |= DWMCI_DDR_MODE;
490 else
Jaehoon Chungafc9e2b2015-01-14 17:37:53 +0900491 regs &= ~DWMCI_DDR_MODE;
Jaehoon Chung045bdcd2014-05-16 13:59:55 +0900492
493 dwmci_writel(host, DWMCI_UHS_REG, regs);
494
Jaehoon Chung757bff42012-10-15 19:10:29 +0000495 if (host->clksel)
496 host->clksel(host);
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900497
Urja Rannikko2b157012019-05-13 13:25:27 +0000498#if CONFIG_IS_ENABLED(DM_REGULATOR)
499 if (mmc->vqmmc_supply) {
500 int ret;
501
502 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
503 regulator_set_value(mmc->vqmmc_supply, 1800000);
504 else
505 regulator_set_value(mmc->vqmmc_supply, 3300000);
506
507 ret = regulator_set_enable_if_allowed(mmc->vqmmc_supply, true);
508 if (ret)
509 return ret;
510 }
511#endif
512
Simon Glass691272f2016-06-12 23:30:23 -0600513 return 0;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000514}
515
516static int dwmci_init(struct mmc *mmc)
517{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200518 struct dwmci_host *host = mmc->priv;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000519
Jaehoon Chung18ab6752013-11-29 20:08:57 +0900520 if (host->board_init)
521 host->board_init(host);
Rajeshwari Shinde6f0b7ca2013-10-29 12:53:13 +0530522
Jaehoon Chung757bff42012-10-15 19:10:29 +0000523 dwmci_writel(host, DWMCI_PWREN, 1);
524
525 if (!dwmci_wait_reset(host, DWMCI_RESET_ALL)) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600526 debug("%s[%d] Fail-reset!!\n", __func__, __LINE__);
527 return -EIO;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000528 }
529
Amar9c50e352013-04-27 11:42:54 +0530530 /* Enumerate at 400KHz */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200531 dwmci_setup_bus(host, mmc->cfg->f_min);
Amar9c50e352013-04-27 11:42:54 +0530532
Jaehoon Chung757bff42012-10-15 19:10:29 +0000533 dwmci_writel(host, DWMCI_RINTSTS, 0xFFFFFFFF);
534 dwmci_writel(host, DWMCI_INTMASK, 0);
535
536 dwmci_writel(host, DWMCI_TMOUT, 0xFFFFFFFF);
537
538 dwmci_writel(host, DWMCI_IDINTEN, 0);
539 dwmci_writel(host, DWMCI_BMOD, 1);
540
Simon Glass760177d2015-08-06 20:16:29 -0600541 if (!host->fifoth_val) {
542 uint32_t fifo_size;
543
544 fifo_size = dwmci_readl(host, DWMCI_FIFOTH);
545 fifo_size = ((fifo_size & RX_WMARK_MASK) >> RX_WMARK_SHIFT) + 1;
546 host->fifoth_val = MSIZE(0x2) | RX_WMARK(fifo_size / 2 - 1) |
547 TX_WMARK(fifo_size / 2);
Amar9c50e352013-04-27 11:42:54 +0530548 }
Simon Glass760177d2015-08-06 20:16:29 -0600549 dwmci_writel(host, DWMCI_FIFOTH, host->fifoth_val);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000550
551 dwmci_writel(host, DWMCI_CLKENA, 0);
552 dwmci_writel(host, DWMCI_CLKSRC, 0);
553
Ley Foon Tan79975992018-12-20 17:55:41 +0800554 if (!host->fifo_mode)
555 dwmci_writel(host, DWMCI_IDINTEN, DWMCI_IDINTEN_MASK);
556
Jaehoon Chung757bff42012-10-15 19:10:29 +0000557 return 0;
558}
559
Simon Glasse7881d82017-07-29 11:35:31 -0600560#ifdef CONFIG_DM_MMC
Simon Glass691272f2016-06-12 23:30:23 -0600561int dwmci_probe(struct udevice *dev)
562{
563 struct mmc *mmc = mmc_get_mmc_dev(dev);
564
565 return dwmci_init(mmc);
566}
567
568const struct dm_mmc_ops dm_dwmci_ops = {
569 .send_cmd = dwmci_send_cmd,
570 .set_ios = dwmci_set_ios,
571};
572
573#else
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200574static const struct mmc_ops dwmci_ops = {
575 .send_cmd = dwmci_send_cmd,
576 .set_ios = dwmci_set_ios,
577 .init = dwmci_init,
578};
Simon Glass691272f2016-06-12 23:30:23 -0600579#endif
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200580
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900581void dwmci_setup_cfg(struct mmc_config *cfg, struct dwmci_host *host,
582 u32 max_clk, u32 min_clk)
Simon Glass5e6ff812016-05-14 14:03:07 -0600583{
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900584 cfg->name = host->name;
Simon Glasse7881d82017-07-29 11:35:31 -0600585#ifndef CONFIG_DM_MMC
Simon Glass5e6ff812016-05-14 14:03:07 -0600586 cfg->ops = &dwmci_ops;
Simon Glass691272f2016-06-12 23:30:23 -0600587#endif
Simon Glass5e6ff812016-05-14 14:03:07 -0600588 cfg->f_min = min_clk;
589 cfg->f_max = max_clk;
590
591 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
592
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900593 cfg->host_caps = host->caps;
Simon Glass5e6ff812016-05-14 14:03:07 -0600594
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900595 if (host->buswidth == 8) {
Simon Glass5e6ff812016-05-14 14:03:07 -0600596 cfg->host_caps |= MMC_MODE_8BIT;
597 cfg->host_caps &= ~MMC_MODE_4BIT;
598 } else {
599 cfg->host_caps |= MMC_MODE_4BIT;
600 cfg->host_caps &= ~MMC_MODE_8BIT;
601 }
602 cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz;
603
604 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
605}
606
607#ifdef CONFIG_BLK
608int dwmci_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg)
609{
610 return mmc_bind(dev, mmc, cfg);
611}
612#else
Jaehoon Chung757bff42012-10-15 19:10:29 +0000613int add_dwmci(struct dwmci_host *host, u32 max_clk, u32 min_clk)
614{
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900615 dwmci_setup_cfg(&host->cfg, host, max_clk, min_clk);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000616
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200617 host->mmc = mmc_create(&host->cfg, host);
618 if (host->mmc == NULL)
619 return -1;
620
621 return 0;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000622}
Simon Glass5e6ff812016-05-14 14:03:07 -0600623#endif