blob: 12245408114551bb96dce4ed70541ca4df847b13 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jaehoon Chung7cf73072012-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 Chung7cf73072012-10-15 19:10:29 +00006 */
7
Alexey Brodkin55bab5e2013-12-26 15:29:07 +04008#include <bouncebuf.h>
Jaehoon Chung7cf73072012-10-15 19:10:29 +00009#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -070010#include <cpu_func.h>
Simon Glass4c9b9482015-08-06 20:16:27 -060011#include <errno.h>
Jaehoon Chung7cf73072012-10-15 19:10:29 +000012#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060013#include <memalign.h>
Jaehoon Chung7cf73072012-10-15 19:10:29 +000014#include <mmc.h>
15#include <dwmmc.h>
Ley Foon Tanb98e8922018-12-20 17:55:41 +080016#include <wait_bit.h>
Urja Rannikko9932a012019-05-13 13:25:27 +000017#include <power/regulator.h>
Jaehoon Chung7cf73072012-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 Kushwahafdefb902015-10-25 13:18:25 +053044 desc->next_addr = (ulong)desc + sizeof(struct dwmci_idmac);
Jaehoon Chung7cf73072012-10-15 19:10:29 +000045}
46
47static void dwmci_prepare_data(struct dwmci_host *host,
Alexey Brodkin55bab5e2013-12-26 15:29:07 +040048 struct mmc_data *data,
49 struct dwmci_idmac *cur_idmac,
50 void *bounce_buffer)
Jaehoon Chung7cf73072012-10-15 19:10:29 +000051{
52 unsigned long ctrl;
53 unsigned int i = 0, flags, cnt, blk_cnt;
Alexey Brodkin55bab5e2013-12-26 15:29:07 +040054 ulong data_start, data_end;
Jaehoon Chung7cf73072012-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 Tanb98e8922018-12-20 17:55:41 +080061 /* Clear IDMAC interrupt */
62 dwmci_writel(host, DWMCI_IDSTS, 0xFFFFFFFF);
63
Jaehoon Chung7cf73072012-10-15 19:10:29 +000064 data_start = (ulong)cur_idmac;
Prabhakar Kushwahafdefb902015-10-25 13:18:25 +053065 dwmci_writel(host, DWMCI_DBADDR, (ulong)cur_idmac);
Jaehoon Chung7cf73072012-10-15 19:10:29 +000066
Jaehoon Chung7cf73072012-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 Kushwahafdefb902015-10-25 13:18:25 +053077 (ulong)bounce_buffer + (i * PAGE_SIZE));
Jaehoon Chung7cf73072012-10-15 19:10:29 +000078
Marek Vasutb6da37b2019-02-13 20:16:20 +010079 cur_idmac++;
Mischa Jonkera7a60912013-07-26 16:18:40 +020080 if (blk_cnt <= 8)
Jaehoon Chung7cf73072012-10-15 19:10:29 +000081 break;
82 blk_cnt -= 8;
Jaehoon Chung7cf73072012-10-15 19:10:29 +000083 i++;
84 } while(1);
85
86 data_end = (ulong)cur_idmac;
Marek Vasutb6da37b2019-02-13 20:16:20 +010087 flush_dcache_range(data_start, roundup(data_end, ARCH_DMA_MINALIGN));
Jaehoon Chung7cf73072012-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 Stuebner46b7a4f2018-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 Vasutffac5122019-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 Yang4889d832019-08-29 15:42:41 +0800123 timeout = size * 8; /* counting in bits */
124 timeout *= 10; /* wait 10 times as long */
Marek Vasutffac5122019-03-23 03:32:24 +0100125 timeout /= mmc->clock;
126 timeout /= mmc->bus_width;
127 timeout /= mmc->ddr_mode ? 2 : 1;
Kever Yang4889d832019-08-29 15:42:41 +0800128 timeout *= 1000; /* counting in msec */
Marek Vasutffac5122019-03-23 03:32:24 +0100129 timeout = (timeout < 1000) ? 1000 : timeout;
130
131 return timeout;
132}
133
huang lin50b73752015-11-17 14:20:22 +0800134static int dwmci_data_transfer(struct dwmci_host *host, struct mmc_data *data)
huang linf9836762015-11-17 14:20:21 +0800135{
Marek Vasutffac5122019-03-23 03:32:24 +0100136 struct mmc *mmc = host->mmc;
huang linf9836762015-11-17 14:20:21 +0800137 int ret = 0;
Marek Vasutffac5122019-03-23 03:32:24 +0100138 u32 timeout, mask, size, i, len = 0;
huang lin50b73752015-11-17 14:20:22 +0800139 u32 *buf = NULL;
huang linf9836762015-11-17 14:20:21 +0800140 ulong start = get_timer(0);
huang lin50b73752015-11-17 14:20:22 +0800141 u32 fifo_depth = (((host->fifoth_val & RX_WMARK_MASK) >>
142 RX_WMARK_SHIFT) + 1) * 2;
143
Marek Vasutffac5122019-03-23 03:32:24 +0100144 size = data->blocksize * data->blocks;
huang lin50b73752015-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 linf9836762015-11-17 14:20:21 +0800149
Marek Vasutffac5122019-03-23 03:32:24 +0100150 timeout = dwmci_get_timeout(mmc, size);
151
152 size /= 4;
153
huang linf9836762015-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 lin50b73752015-11-17 14:20:22 +0800163 if (host->fifo_mode && size) {
Xu Ziyuan5b8bf122016-07-28 10:25:48 +0800164 len = 0;
Jacob Chen953d9752016-09-19 10:16:50 +0800165 if (data->flags == MMC_DATA_READ &&
166 (mask & DWMCI_INTMSK_RXDR)) {
167 while (size) {
Heiko Stuebner46b7a4f2018-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 lin50b73752015-11-17 14:20:22 +0800174 len = (len >> DWMCI_FIFO_SHIFT) &
175 DWMCI_FIFO_MASK;
Xu Ziyuan6577a2a2016-07-28 10:25:47 +0800176 len = min(size, len);
huang lin50b73752015-11-17 14:20:22 +0800177 for (i = 0; i < len; i++)
178 *buf++ =
179 dwmci_readl(host, DWMCI_DATA);
Jacob Chen953d9752016-09-19 10:16:50 +0800180 size = size > len ? (size - len) : 0;
huang lin50b73752015-11-17 14:20:22 +0800181 }
Jacob Chen953d9752016-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 Stuebner46b7a4f2018-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 lin50b73752015-11-17 14:20:22 +0800193 len = fifo_depth - ((len >>
194 DWMCI_FIFO_SHIFT) &
195 DWMCI_FIFO_MASK);
Xu Ziyuan6577a2a2016-07-28 10:25:47 +0800196 len = min(size, len);
huang lin50b73752015-11-17 14:20:22 +0800197 for (i = 0; i < len; i++)
198 dwmci_writel(host, DWMCI_DATA,
199 *buf++);
Jacob Chen953d9752016-09-19 10:16:50 +0800200 size = size > len ? (size - len) : 0;
huang lin50b73752015-11-17 14:20:22 +0800201 }
Jacob Chen953d9752016-09-19 10:16:50 +0800202 dwmci_writel(host, DWMCI_RINTSTS,
203 DWMCI_INTMSK_TXDR);
huang lin50b73752015-11-17 14:20:22 +0800204 }
huang lin50b73752015-11-17 14:20:22 +0800205 }
206
huang linf9836762015-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 Chung7825d202016-07-19 16:33:36 +0900217 ret = -ETIMEDOUT;
huang linf9836762015-11-17 14:20:21 +0800218 break;
219 }
220 }
221
222 dwmci_writel(host, DWMCI_RINTSTS, mask);
223
224 return ret;
225}
226
Jaehoon Chung7cf73072012-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 Glasseba48f92017-07-29 11:35:31 -0600239#ifdef CONFIG_DM_MMC
Jaehoon Chungad220ac2016-06-28 15:52:21 +0900240static int dwmci_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
Simon Glassff5c1b72016-06-12 23:30:23 -0600241 struct mmc_data *data)
242{
243 struct mmc *mmc = mmc_get_mmc_dev(dev);
244#else
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000245static int dwmci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
246 struct mmc_data *data)
247{
Simon Glassff5c1b72016-06-12 23:30:23 -0600248#endif
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200249 struct dwmci_host *host = mmc->priv;
Mischa Jonker7423bed2013-07-26 14:08:14 +0200250 ALLOC_CACHE_ALIGN_BUFFER(struct dwmci_idmac, cur_idmac,
Mischa Jonkera7a60912013-07-26 16:18:40 +0200251 data ? DIV_ROUND_UP(data->blocks, 8) : 0);
Marek Vasut81e093f2015-07-27 22:39:38 +0200252 int ret = 0, flags = 0, i;
Xu Ziyuan34a10d32016-07-19 09:38:22 +0800253 unsigned int timeout = 500;
Alexander Graf61c2a662016-03-04 01:09:52 +0100254 u32 retry = 100000;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000255 u32 mask, ctrl;
Amar902664c2013-04-27 11:42:54 +0530256 ulong start = get_timer(0);
Alexey Brodkin55bab5e2013-12-26 15:29:07 +0400257 struct bounce_buffer bbstate;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000258
259 while (dwmci_readl(host, DWMCI_STATUS) & DWMCI_BUSY) {
Amar902664c2013-04-27 11:42:54 +0530260 if (get_timer(start) > timeout) {
Simon Glass4c9b9482015-08-06 20:16:27 -0600261 debug("%s: Timeout on data busy\n", __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900262 return -ETIMEDOUT;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000263 }
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000264 }
265
266 dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_ALL);
267
Alexey Brodkin55bab5e2013-12-26 15:29:07 +0400268 if (data) {
huang lin50b73752015-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 Brodkin55bab5e2013-12-26 15:29:07 +0400274 } else {
huang lin50b73752015-11-17 14:20:22 +0800275 if (data->flags == MMC_DATA_READ) {
Marek Vasut72d37b62019-03-23 18:45:27 +0100276 ret = bounce_buffer_start(&bbstate,
277 (void*)data->dest,
huang lin50b73752015-11-17 14:20:22 +0800278 data->blocksize *
279 data->blocks, GEN_BB_WRITE);
280 } else {
Marek Vasut72d37b62019-03-23 18:45:27 +0100281 ret = bounce_buffer_start(&bbstate,
282 (void*)data->src,
huang lin50b73752015-11-17 14:20:22 +0800283 data->blocksize *
284 data->blocks, GEN_BB_READ);
285 }
Marek Vasut72d37b62019-03-23 18:45:27 +0100286
287 if (ret)
288 return ret;
289
huang lin50b73752015-11-17 14:20:22 +0800290 dwmci_prepare_data(host, data, cur_idmac,
291 bbstate.bounce_buffer);
Alexey Brodkin55bab5e2013-12-26 15:29:07 +0400292 }
Alexey Brodkin55bab5e2013-12-26 15:29:07 +0400293 }
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000294
Jaehoon Chung7cf73072012-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 Macheka425f5d2014-09-05 12:49:48 +0200332 if (i == retry) {
Simon Glass4c9b9482015-08-06 20:16:27 -0600333 debug("%s: Timeout.\n", __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900334 return -ETIMEDOUT;
Pavel Macheka425f5d2014-09-05 12:49:48 +0200335 }
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000336
337 if (mask & DWMCI_INTMSK_RTO) {
Pavel Macheka425f5d2014-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 Chung7825d202016-07-19 16:33:36 +0900347 return -ETIMEDOUT;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000348 } else if (mask & DWMCI_INTMSK_RE) {
Simon Glass4c9b9482015-08-06 20:16:27 -0600349 debug("%s: Response Error.\n", __func__);
350 return -EIO;
Marek Vasuta6d91992018-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 Chung7cf73072012-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 lin50b73752015-11-17 14:20:22 +0800370 ret = dwmci_data_transfer(host, data);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000371
huang lin50b73752015-11-17 14:20:22 +0800372 /* only dma mode need it */
373 if (!host->fifo_mode) {
Ley Foon Tanb98e8922018-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 lin50b73752015-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 Chung7cf73072012-10-15 19:10:29 +0000391 }
392
393 udelay(100);
394
Marek Vasut81e093f2015-07-27 22:39:38 +0200395 return ret;
Jaehoon Chung7cf73072012-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
Amar902664c2013-04-27 11:42:54 +0530404 if ((freq == host->clock) || (freq == 0))
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000405 return 0;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000406 /*
Pavel Macheka425f5d2014-09-05 12:49:48 +0200407 * If host->get_mmc_clk isn't defined,
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000408 * then assume that host->bus_hz is source clock value.
Pavel Macheka425f5d2014-09-05 12:49:48 +0200409 * host->bus_hz should be set by user.
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000410 */
Jaehoon Chungd94735b2013-10-06 18:59:31 +0900411 if (host->get_mmc_clk)
Simon Glasseff76682015-08-30 16:55:15 -0600412 sclk = host->get_mmc_clk(host, freq);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000413 else if (host->bus_hz)
414 sclk = host->bus_hz;
415 else {
Simon Glass4c9b9482015-08-06 20:16:27 -0600416 debug("%s: Didn't get source clock value.\n", __func__);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000417 return -EINVAL;
418 }
419
Chin Liang See4cfff952014-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 Chung7cf73072012-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 Glass4c9b9482015-08-06 20:16:27 -0600435 debug("%s: Timeout!\n", __func__);
Jaehoon Chung7cf73072012-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 Glass4c9b9482015-08-06 20:16:27 -0600450 debug("%s: Timeout!\n", __func__);
Jaehoon Chung7cf73072012-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 Glasseba48f92017-07-29 11:35:31 -0600460#ifdef CONFIG_DM_MMC
Jaehoon Chungad220ac2016-06-28 15:52:21 +0900461static int dwmci_set_ios(struct udevice *dev)
Simon Glassff5c1b72016-06-12 23:30:23 -0600462{
463 struct mmc *mmc = mmc_get_mmc_dev(dev);
464#else
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900465static int dwmci_set_ios(struct mmc *mmc)
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000466{
Simon Glassff5c1b72016-06-12 23:30:23 -0600467#endif
Jaehoon Chunge8672942014-05-16 13:59:55 +0900468 struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
469 u32 ctype, regs;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000470
Pavel Macheka425f5d2014-09-05 12:49:48 +0200471 debug("Buswidth = %d, clock: %d\n", mmc->bus_width, mmc->clock);
Jaehoon Chung7cf73072012-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 Chunge8672942014-05-16 13:59:55 +0900488 regs = dwmci_readl(host, DWMCI_UHS_REG);
Andrew Gabbasov54c0e222014-12-01 06:59:12 -0600489 if (mmc->ddr_mode)
Jaehoon Chunge8672942014-05-16 13:59:55 +0900490 regs |= DWMCI_DDR_MODE;
491 else
Jaehoon Chung401fc502015-01-14 17:37:53 +0900492 regs &= ~DWMCI_DDR_MODE;
Jaehoon Chunge8672942014-05-16 13:59:55 +0900493
494 dwmci_writel(host, DWMCI_UHS_REG, regs);
495
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000496 if (host->clksel)
497 host->clksel(host);
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900498
Urja Rannikko9932a012019-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 Glassff5c1b72016-06-12 23:30:23 -0600514 return 0;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000515}
516
517static int dwmci_init(struct mmc *mmc)
518{
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200519 struct dwmci_host *host = mmc->priv;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000520
Jaehoon Chung42f81a82013-11-29 20:08:57 +0900521 if (host->board_init)
522 host->board_init(host);
Rajeshwari Shinde70163092013-10-29 12:53:13 +0530523
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000524 dwmci_writel(host, DWMCI_PWREN, 1);
525
526 if (!dwmci_wait_reset(host, DWMCI_RESET_ALL)) {
Simon Glass4c9b9482015-08-06 20:16:27 -0600527 debug("%s[%d] Fail-reset!!\n", __func__, __LINE__);
528 return -EIO;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000529 }
530
Amar902664c2013-04-27 11:42:54 +0530531 /* Enumerate at 400KHz */
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200532 dwmci_setup_bus(host, mmc->cfg->f_min);
Amar902664c2013-04-27 11:42:54 +0530533
Jaehoon Chung7cf73072012-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 Glass6133efa2015-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);
Amar902664c2013-04-27 11:42:54 +0530549 }
Simon Glass6133efa2015-08-06 20:16:29 -0600550 dwmci_writel(host, DWMCI_FIFOTH, host->fifoth_val);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000551
552 dwmci_writel(host, DWMCI_CLKENA, 0);
553 dwmci_writel(host, DWMCI_CLKSRC, 0);
554
Ley Foon Tanb98e8922018-12-20 17:55:41 +0800555 if (!host->fifo_mode)
556 dwmci_writel(host, DWMCI_IDINTEN, DWMCI_IDINTEN_MASK);
557
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000558 return 0;
559}
560
Simon Glasseba48f92017-07-29 11:35:31 -0600561#ifdef CONFIG_DM_MMC
Simon Glassff5c1b72016-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 Antoniouc9e75912014-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 Glassff5c1b72016-06-12 23:30:23 -0600580#endif
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200581
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900582void dwmci_setup_cfg(struct mmc_config *cfg, struct dwmci_host *host,
583 u32 max_clk, u32 min_clk)
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000584{
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900585 cfg->name = host->name;
Simon Glasseba48f92017-07-29 11:35:31 -0600586#ifndef CONFIG_DM_MMC
Simon Glass82682542016-05-14 14:03:07 -0600587 cfg->ops = &dwmci_ops;
Simon Glassff5c1b72016-06-12 23:30:23 -0600588#endif
Simon Glass82682542016-05-14 14:03:07 -0600589 cfg->f_min = min_clk;
590 cfg->f_max = max_clk;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000591
Simon Glass82682542016-05-14 14:03:07 -0600592 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000593
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900594 cfg->host_caps = host->caps;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000595
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900596 if (host->buswidth == 8) {
Simon Glass82682542016-05-14 14:03:07 -0600597 cfg->host_caps |= MMC_MODE_8BIT;
598 cfg->host_caps &= ~MMC_MODE_4BIT;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000599 } else {
Simon Glass82682542016-05-14 14:03:07 -0600600 cfg->host_caps |= MMC_MODE_4BIT;
601 cfg->host_caps &= ~MMC_MODE_8BIT;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000602 }
Simon Glass82682542016-05-14 14:03:07 -0600603 cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz;
604
605 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
606}
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200607
Simon Glass82682542016-05-14 14:03:07 -0600608#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
614int add_dwmci(struct dwmci_host *host, u32 max_clk, u32 min_clk)
615{
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900616 dwmci_setup_cfg(&host->cfg, host, max_clk, min_clk);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000617
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200618 host->mmc = mmc_create(&host->cfg, host);
619 if (host->mmc == NULL)
620 return -1;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000621
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200622 return 0;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000623}
Simon Glass82682542016-05-14 14:03:07 -0600624#endif