blob: f4ecd7422cef6128e852eb1c1ed061a402ab4c3e [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>
Simon Glass63334482019-11-14 12:57:39 -07009#include <cpu_func.h>
Simon Glass4c9b9482015-08-06 20:16:27 -060010#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.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>
Simon Glass274e0b02020-05-10 11:39:56 -060017#include <asm/cache.h>
Simon Glassdbd79542020-05-10 11:40:11 -060018#include <linux/delay.h>
Urja Rannikko9932a012019-05-13 13:25:27 +000019#include <power/regulator.h>
Jaehoon Chung7cf73072012-10-15 19:10:29 +000020
21#define PAGE_SIZE 4096
22
23static int dwmci_wait_reset(struct dwmci_host *host, u32 value)
24{
25 unsigned long timeout = 1000;
26 u32 ctrl;
27
28 dwmci_writel(host, DWMCI_CTRL, value);
29
30 while (timeout--) {
31 ctrl = dwmci_readl(host, DWMCI_CTRL);
32 if (!(ctrl & DWMCI_RESET_ALL))
33 return 1;
34 }
35 return 0;
36}
37
38static void dwmci_set_idma_desc(struct dwmci_idmac *idmac,
39 u32 desc0, u32 desc1, u32 desc2)
40{
41 struct dwmci_idmac *desc = idmac;
42
43 desc->flags = desc0;
44 desc->cnt = desc1;
45 desc->addr = desc2;
Prabhakar Kushwahafdefb902015-10-25 13:18:25 +053046 desc->next_addr = (ulong)desc + sizeof(struct dwmci_idmac);
Jaehoon Chung7cf73072012-10-15 19:10:29 +000047}
48
49static void dwmci_prepare_data(struct dwmci_host *host,
Alexey Brodkin55bab5e2013-12-26 15:29:07 +040050 struct mmc_data *data,
51 struct dwmci_idmac *cur_idmac,
52 void *bounce_buffer)
Jaehoon Chung7cf73072012-10-15 19:10:29 +000053{
54 unsigned long ctrl;
55 unsigned int i = 0, flags, cnt, blk_cnt;
Alexey Brodkin55bab5e2013-12-26 15:29:07 +040056 ulong data_start, data_end;
Jaehoon Chung7cf73072012-10-15 19:10:29 +000057
Jaehoon Chung7cf73072012-10-15 19:10:29 +000058 blk_cnt = data->blocks;
59
60 dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
61
Ley Foon Tanb98e8922018-12-20 17:55:41 +080062 /* Clear IDMAC interrupt */
63 dwmci_writel(host, DWMCI_IDSTS, 0xFFFFFFFF);
64
Jaehoon Chung7cf73072012-10-15 19:10:29 +000065 data_start = (ulong)cur_idmac;
Prabhakar Kushwahafdefb902015-10-25 13:18:25 +053066 dwmci_writel(host, DWMCI_DBADDR, (ulong)cur_idmac);
Jaehoon Chung7cf73072012-10-15 19:10:29 +000067
Jaehoon Chung7cf73072012-10-15 19:10:29 +000068 do {
69 flags = DWMCI_IDMAC_OWN | DWMCI_IDMAC_CH ;
70 flags |= (i == 0) ? DWMCI_IDMAC_FS : 0;
71 if (blk_cnt <= 8) {
72 flags |= DWMCI_IDMAC_LD;
73 cnt = data->blocksize * blk_cnt;
74 } else
75 cnt = data->blocksize * 8;
76
77 dwmci_set_idma_desc(cur_idmac, flags, cnt,
Prabhakar Kushwahafdefb902015-10-25 13:18:25 +053078 (ulong)bounce_buffer + (i * PAGE_SIZE));
Jaehoon Chung7cf73072012-10-15 19:10:29 +000079
Marek Vasutb6da37b2019-02-13 20:16:20 +010080 cur_idmac++;
Mischa Jonkera7a60912013-07-26 16:18:40 +020081 if (blk_cnt <= 8)
Jaehoon Chung7cf73072012-10-15 19:10:29 +000082 break;
83 blk_cnt -= 8;
Jaehoon Chung7cf73072012-10-15 19:10:29 +000084 i++;
85 } while(1);
86
87 data_end = (ulong)cur_idmac;
Marek Vasutb6da37b2019-02-13 20:16:20 +010088 flush_dcache_range(data_start, roundup(data_end, ARCH_DMA_MINALIGN));
Jaehoon Chung7cf73072012-10-15 19:10:29 +000089
90 ctrl = dwmci_readl(host, DWMCI_CTRL);
91 ctrl |= DWMCI_IDMAC_EN | DWMCI_DMA_EN;
92 dwmci_writel(host, DWMCI_CTRL, ctrl);
93
94 ctrl = dwmci_readl(host, DWMCI_BMOD);
95 ctrl |= DWMCI_BMOD_IDMAC_FB | DWMCI_BMOD_IDMAC_EN;
96 dwmci_writel(host, DWMCI_BMOD, ctrl);
97
98 dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
99 dwmci_writel(host, DWMCI_BYTCNT, data->blocksize * data->blocks);
100}
101
Heiko Stuebner46b7a4f2018-09-21 10:59:45 +0200102static int dwmci_fifo_ready(struct dwmci_host *host, u32 bit, u32 *len)
103{
104 u32 timeout = 20000;
105
106 *len = dwmci_readl(host, DWMCI_STATUS);
107 while (--timeout && (*len & bit)) {
108 udelay(200);
109 *len = dwmci_readl(host, DWMCI_STATUS);
110 }
111
112 if (!timeout) {
113 debug("%s: FIFO underflow timeout\n", __func__);
114 return -ETIMEDOUT;
115 }
116
117 return 0;
118}
119
Marek Vasutffac5122019-03-23 03:32:24 +0100120static unsigned int dwmci_get_timeout(struct mmc *mmc, const unsigned int size)
121{
122 unsigned int timeout;
123
Kever Yang4889d832019-08-29 15:42:41 +0800124 timeout = size * 8; /* counting in bits */
125 timeout *= 10; /* wait 10 times as long */
Marek Vasutffac5122019-03-23 03:32:24 +0100126 timeout /= mmc->clock;
127 timeout /= mmc->bus_width;
128 timeout /= mmc->ddr_mode ? 2 : 1;
Kever Yang4889d832019-08-29 15:42:41 +0800129 timeout *= 1000; /* counting in msec */
Marek Vasutffac5122019-03-23 03:32:24 +0100130 timeout = (timeout < 1000) ? 1000 : timeout;
131
132 return timeout;
133}
134
huang lin50b73752015-11-17 14:20:22 +0800135static int dwmci_data_transfer(struct dwmci_host *host, struct mmc_data *data)
huang linf9836762015-11-17 14:20:21 +0800136{
Marek Vasutffac5122019-03-23 03:32:24 +0100137 struct mmc *mmc = host->mmc;
huang linf9836762015-11-17 14:20:21 +0800138 int ret = 0;
Marek Vasutffac5122019-03-23 03:32:24 +0100139 u32 timeout, mask, size, i, len = 0;
huang lin50b73752015-11-17 14:20:22 +0800140 u32 *buf = NULL;
huang linf9836762015-11-17 14:20:21 +0800141 ulong start = get_timer(0);
huang lin50b73752015-11-17 14:20:22 +0800142 u32 fifo_depth = (((host->fifoth_val & RX_WMARK_MASK) >>
143 RX_WMARK_SHIFT) + 1) * 2;
144
Marek Vasutffac5122019-03-23 03:32:24 +0100145 size = data->blocksize * data->blocks;
huang lin50b73752015-11-17 14:20:22 +0800146 if (data->flags == MMC_DATA_READ)
147 buf = (unsigned int *)data->dest;
148 else
149 buf = (unsigned int *)data->src;
huang linf9836762015-11-17 14:20:21 +0800150
Marek Vasutffac5122019-03-23 03:32:24 +0100151 timeout = dwmci_get_timeout(mmc, size);
152
153 size /= 4;
154
huang linf9836762015-11-17 14:20:21 +0800155 for (;;) {
156 mask = dwmci_readl(host, DWMCI_RINTSTS);
157 /* Error during data transfer. */
158 if (mask & (DWMCI_DATA_ERR | DWMCI_DATA_TOUT)) {
159 debug("%s: DATA ERROR!\n", __func__);
160 ret = -EINVAL;
161 break;
162 }
163
huang lin50b73752015-11-17 14:20:22 +0800164 if (host->fifo_mode && size) {
Xu Ziyuan5b8bf122016-07-28 10:25:48 +0800165 len = 0;
Jacob Chen953d9752016-09-19 10:16:50 +0800166 if (data->flags == MMC_DATA_READ &&
Ley Foon Tan1cead232021-04-26 11:35:05 +0800167 (mask & (DWMCI_INTMSK_RXDR | DWMCI_INTMSK_DTO))) {
168 dwmci_writel(host, DWMCI_RINTSTS,
John Keepinga6a71572022-09-15 18:56:56 +0100169 mask & (DWMCI_INTMSK_RXDR |
170 DWMCI_INTMSK_DTO));
Jacob Chen953d9752016-09-19 10:16:50 +0800171 while (size) {
Heiko Stuebner46b7a4f2018-09-21 10:59:45 +0200172 ret = dwmci_fifo_ready(host,
173 DWMCI_FIFO_EMPTY,
174 &len);
175 if (ret < 0)
176 break;
177
huang lin50b73752015-11-17 14:20:22 +0800178 len = (len >> DWMCI_FIFO_SHIFT) &
179 DWMCI_FIFO_MASK;
Xu Ziyuan6577a2a2016-07-28 10:25:47 +0800180 len = min(size, len);
huang lin50b73752015-11-17 14:20:22 +0800181 for (i = 0; i < len; i++)
182 *buf++ =
183 dwmci_readl(host, DWMCI_DATA);
Jacob Chen953d9752016-09-19 10:16:50 +0800184 size = size > len ? (size - len) : 0;
huang lin50b73752015-11-17 14:20:22 +0800185 }
Jacob Chen953d9752016-09-19 10:16:50 +0800186 } else if (data->flags == MMC_DATA_WRITE &&
187 (mask & DWMCI_INTMSK_TXDR)) {
188 while (size) {
Heiko Stuebner46b7a4f2018-09-21 10:59:45 +0200189 ret = dwmci_fifo_ready(host,
190 DWMCI_FIFO_FULL,
191 &len);
192 if (ret < 0)
193 break;
194
huang lin50b73752015-11-17 14:20:22 +0800195 len = fifo_depth - ((len >>
196 DWMCI_FIFO_SHIFT) &
197 DWMCI_FIFO_MASK);
Xu Ziyuan6577a2a2016-07-28 10:25:47 +0800198 len = min(size, len);
huang lin50b73752015-11-17 14:20:22 +0800199 for (i = 0; i < len; i++)
200 dwmci_writel(host, DWMCI_DATA,
201 *buf++);
Jacob Chen953d9752016-09-19 10:16:50 +0800202 size = size > len ? (size - len) : 0;
huang lin50b73752015-11-17 14:20:22 +0800203 }
Jacob Chen953d9752016-09-19 10:16:50 +0800204 dwmci_writel(host, DWMCI_RINTSTS,
205 DWMCI_INTMSK_TXDR);
huang lin50b73752015-11-17 14:20:22 +0800206 }
huang lin50b73752015-11-17 14:20:22 +0800207 }
208
huang linf9836762015-11-17 14:20:21 +0800209 /* Data arrived correctly. */
210 if (mask & DWMCI_INTMSK_DTO) {
211 ret = 0;
212 break;
213 }
214
215 /* Check for timeout. */
216 if (get_timer(start) > timeout) {
217 debug("%s: Timeout waiting for data!\n",
218 __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900219 ret = -ETIMEDOUT;
huang linf9836762015-11-17 14:20:21 +0800220 break;
221 }
222 }
223
224 dwmci_writel(host, DWMCI_RINTSTS, mask);
225
226 return ret;
227}
228
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000229static int dwmci_set_transfer_mode(struct dwmci_host *host,
230 struct mmc_data *data)
231{
232 unsigned long mode;
233
234 mode = DWMCI_CMD_DATA_EXP;
235 if (data->flags & MMC_DATA_WRITE)
236 mode |= DWMCI_CMD_RW;
237
238 return mode;
239}
240
Simon Glasseba48f92017-07-29 11:35:31 -0600241#ifdef CONFIG_DM_MMC
Jaehoon Chungad220ac2016-06-28 15:52:21 +0900242static int dwmci_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
Simon Glassff5c1b72016-06-12 23:30:23 -0600243 struct mmc_data *data)
244{
245 struct mmc *mmc = mmc_get_mmc_dev(dev);
246#else
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000247static int dwmci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
248 struct mmc_data *data)
249{
Simon Glassff5c1b72016-06-12 23:30:23 -0600250#endif
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200251 struct dwmci_host *host = mmc->priv;
Mischa Jonker7423bed2013-07-26 14:08:14 +0200252 ALLOC_CACHE_ALIGN_BUFFER(struct dwmci_idmac, cur_idmac,
Mischa Jonkera7a60912013-07-26 16:18:40 +0200253 data ? DIV_ROUND_UP(data->blocks, 8) : 0);
Marek Vasut81e093f2015-07-27 22:39:38 +0200254 int ret = 0, flags = 0, i;
Xu Ziyuan34a10d32016-07-19 09:38:22 +0800255 unsigned int timeout = 500;
Alexander Graf61c2a662016-03-04 01:09:52 +0100256 u32 retry = 100000;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000257 u32 mask, ctrl;
Amar902664c2013-04-27 11:42:54 +0530258 ulong start = get_timer(0);
Alexey Brodkin55bab5e2013-12-26 15:29:07 +0400259 struct bounce_buffer bbstate;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000260
261 while (dwmci_readl(host, DWMCI_STATUS) & DWMCI_BUSY) {
Amar902664c2013-04-27 11:42:54 +0530262 if (get_timer(start) > timeout) {
Yang Xiwen84df6a72024-02-01 22:05:43 +0800263 debug("%s: Timeout on data busy, continue anyway\n", __func__);
264 break;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000265 }
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000266 }
267
268 dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_ALL);
269
Alexey Brodkin55bab5e2013-12-26 15:29:07 +0400270 if (data) {
huang lin50b73752015-11-17 14:20:22 +0800271 if (host->fifo_mode) {
272 dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
273 dwmci_writel(host, DWMCI_BYTCNT,
274 data->blocksize * data->blocks);
275 dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
Alexey Brodkin55bab5e2013-12-26 15:29:07 +0400276 } else {
huang lin50b73752015-11-17 14:20:22 +0800277 if (data->flags == MMC_DATA_READ) {
Marek Vasut72d37b62019-03-23 18:45:27 +0100278 ret = bounce_buffer_start(&bbstate,
279 (void*)data->dest,
huang lin50b73752015-11-17 14:20:22 +0800280 data->blocksize *
281 data->blocks, GEN_BB_WRITE);
282 } else {
Marek Vasut72d37b62019-03-23 18:45:27 +0100283 ret = bounce_buffer_start(&bbstate,
284 (void*)data->src,
huang lin50b73752015-11-17 14:20:22 +0800285 data->blocksize *
286 data->blocks, GEN_BB_READ);
287 }
Marek Vasut72d37b62019-03-23 18:45:27 +0100288
289 if (ret)
290 return ret;
291
huang lin50b73752015-11-17 14:20:22 +0800292 dwmci_prepare_data(host, data, cur_idmac,
293 bbstate.bounce_buffer);
Alexey Brodkin55bab5e2013-12-26 15:29:07 +0400294 }
Alexey Brodkin55bab5e2013-12-26 15:29:07 +0400295 }
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000296
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000297 dwmci_writel(host, DWMCI_CMDARG, cmd->cmdarg);
298
299 if (data)
300 flags = dwmci_set_transfer_mode(host, data);
301
302 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
John Keepingfeb7fa32021-12-07 16:09:35 +0000303 return -EBUSY;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000304
305 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
306 flags |= DWMCI_CMD_ABORT_STOP;
307 else
308 flags |= DWMCI_CMD_PRV_DAT_WAIT;
309
310 if (cmd->resp_type & MMC_RSP_PRESENT) {
311 flags |= DWMCI_CMD_RESP_EXP;
312 if (cmd->resp_type & MMC_RSP_136)
313 flags |= DWMCI_CMD_RESP_LENGTH;
314 }
315
316 if (cmd->resp_type & MMC_RSP_CRC)
317 flags |= DWMCI_CMD_CHECK_CRC;
318
319 flags |= (cmd->cmdidx | DWMCI_CMD_START | DWMCI_CMD_USE_HOLD_REG);
320
321 debug("Sending CMD%d\n",cmd->cmdidx);
322
323 dwmci_writel(host, DWMCI_CMD, flags);
324
325 for (i = 0; i < retry; i++) {
326 mask = dwmci_readl(host, DWMCI_RINTSTS);
327 if (mask & DWMCI_INTMSK_CDONE) {
328 if (!data)
329 dwmci_writel(host, DWMCI_RINTSTS, mask);
330 break;
331 }
332 }
333
Pavel Macheka425f5d2014-09-05 12:49:48 +0200334 if (i == retry) {
Simon Glass4c9b9482015-08-06 20:16:27 -0600335 debug("%s: Timeout.\n", __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900336 return -ETIMEDOUT;
Pavel Macheka425f5d2014-09-05 12:49:48 +0200337 }
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000338
339 if (mask & DWMCI_INTMSK_RTO) {
Pavel Macheka425f5d2014-09-05 12:49:48 +0200340 /*
341 * Timeout here is not necessarily fatal. (e)MMC cards
342 * will splat here when they receive CMD55 as they do
343 * not support this command and that is exactly the way
344 * to tell them apart from SD cards. Thus, this output
345 * below shall be debug(). eMMC cards also do not favor
346 * CMD8, please keep that in mind.
347 */
348 debug("%s: Response Timeout.\n", __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900349 return -ETIMEDOUT;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000350 } else if (mask & DWMCI_INTMSK_RE) {
Simon Glass4c9b9482015-08-06 20:16:27 -0600351 debug("%s: Response Error.\n", __func__);
352 return -EIO;
Marek Vasuta6d91992018-11-06 23:42:11 +0100353 } else if ((cmd->resp_type & MMC_RSP_CRC) &&
354 (mask & DWMCI_INTMSK_RCRC)) {
355 debug("%s: Response CRC Error.\n", __func__);
356 return -EIO;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000357 }
358
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000359 if (cmd->resp_type & MMC_RSP_PRESENT) {
360 if (cmd->resp_type & MMC_RSP_136) {
361 cmd->response[0] = dwmci_readl(host, DWMCI_RESP3);
362 cmd->response[1] = dwmci_readl(host, DWMCI_RESP2);
363 cmd->response[2] = dwmci_readl(host, DWMCI_RESP1);
364 cmd->response[3] = dwmci_readl(host, DWMCI_RESP0);
365 } else {
366 cmd->response[0] = dwmci_readl(host, DWMCI_RESP0);
367 }
368 }
369
370 if (data) {
huang lin50b73752015-11-17 14:20:22 +0800371 ret = dwmci_data_transfer(host, data);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000372
huang lin50b73752015-11-17 14:20:22 +0800373 /* only dma mode need it */
374 if (!host->fifo_mode) {
Ley Foon Tanb98e8922018-12-20 17:55:41 +0800375 if (data->flags == MMC_DATA_READ)
376 mask = DWMCI_IDINTEN_RI;
377 else
378 mask = DWMCI_IDINTEN_TI;
379 ret = wait_for_bit_le32(host->ioaddr + DWMCI_IDSTS,
380 mask, true, 1000, false);
381 if (ret)
382 debug("%s: DWMCI_IDINTEN mask 0x%x timeout.\n",
383 __func__, mask);
384 /* clear interrupts */
385 dwmci_writel(host, DWMCI_IDSTS, DWMCI_IDINTEN_MASK);
386
huang lin50b73752015-11-17 14:20:22 +0800387 ctrl = dwmci_readl(host, DWMCI_CTRL);
388 ctrl &= ~(DWMCI_DMA_EN);
389 dwmci_writel(host, DWMCI_CTRL, ctrl);
390 bounce_buffer_stop(&bbstate);
391 }
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000392 }
393
394 udelay(100);
395
Marek Vasut81e093f2015-07-27 22:39:38 +0200396 return ret;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000397}
398
399static int dwmci_setup_bus(struct dwmci_host *host, u32 freq)
400{
401 u32 div, status;
402 int timeout = 10000;
403 unsigned long sclk;
404
Amar902664c2013-04-27 11:42:54 +0530405 if ((freq == host->clock) || (freq == 0))
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000406 return 0;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000407 /*
Pavel Macheka425f5d2014-09-05 12:49:48 +0200408 * If host->get_mmc_clk isn't defined,
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000409 * then assume that host->bus_hz is source clock value.
Pavel Macheka425f5d2014-09-05 12:49:48 +0200410 * host->bus_hz should be set by user.
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000411 */
Jaehoon Chungd94735b2013-10-06 18:59:31 +0900412 if (host->get_mmc_clk)
Simon Glasseff76682015-08-30 16:55:15 -0600413 sclk = host->get_mmc_clk(host, freq);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000414 else if (host->bus_hz)
415 sclk = host->bus_hz;
416 else {
Simon Glass4c9b9482015-08-06 20:16:27 -0600417 debug("%s: Didn't get source clock value.\n", __func__);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000418 return -EINVAL;
419 }
420
Chin Liang See4cfff952014-06-10 01:26:52 -0500421 if (sclk == freq)
422 div = 0; /* bypass mode */
423 else
424 div = DIV_ROUND_UP(sclk, 2 * freq);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000425
426 dwmci_writel(host, DWMCI_CLKENA, 0);
427 dwmci_writel(host, DWMCI_CLKSRC, 0);
428
429 dwmci_writel(host, DWMCI_CLKDIV, div);
430 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
431 DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
432
433 do {
434 status = dwmci_readl(host, DWMCI_CMD);
435 if (timeout-- < 0) {
Simon Glass4c9b9482015-08-06 20:16:27 -0600436 debug("%s: Timeout!\n", __func__);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000437 return -ETIMEDOUT;
438 }
439 } while (status & DWMCI_CMD_START);
440
441 dwmci_writel(host, DWMCI_CLKENA, DWMCI_CLKEN_ENABLE |
442 DWMCI_CLKEN_LOW_PWR);
443
444 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
445 DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
446
447 timeout = 10000;
448 do {
449 status = dwmci_readl(host, DWMCI_CMD);
450 if (timeout-- < 0) {
Simon Glass4c9b9482015-08-06 20:16:27 -0600451 debug("%s: Timeout!\n", __func__);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000452 return -ETIMEDOUT;
453 }
454 } while (status & DWMCI_CMD_START);
455
456 host->clock = freq;
457
458 return 0;
459}
460
Simon Glasseba48f92017-07-29 11:35:31 -0600461#ifdef CONFIG_DM_MMC
Jaehoon Chungad220ac2016-06-28 15:52:21 +0900462static int dwmci_set_ios(struct udevice *dev)
Simon Glassff5c1b72016-06-12 23:30:23 -0600463{
464 struct mmc *mmc = mmc_get_mmc_dev(dev);
465#else
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900466static int dwmci_set_ios(struct mmc *mmc)
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000467{
Simon Glassff5c1b72016-06-12 23:30:23 -0600468#endif
Jaehoon Chunge8672942014-05-16 13:59:55 +0900469 struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
470 u32 ctype, regs;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000471
Pavel Macheka425f5d2014-09-05 12:49:48 +0200472 debug("Buswidth = %d, clock: %d\n", mmc->bus_width, mmc->clock);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000473
474 dwmci_setup_bus(host, mmc->clock);
475 switch (mmc->bus_width) {
476 case 8:
477 ctype = DWMCI_CTYPE_8BIT;
478 break;
479 case 4:
480 ctype = DWMCI_CTYPE_4BIT;
481 break;
482 default:
483 ctype = DWMCI_CTYPE_1BIT;
484 break;
485 }
486
487 dwmci_writel(host, DWMCI_CTYPE, ctype);
488
Jaehoon Chunge8672942014-05-16 13:59:55 +0900489 regs = dwmci_readl(host, DWMCI_UHS_REG);
Andrew Gabbasov54c0e222014-12-01 06:59:12 -0600490 if (mmc->ddr_mode)
Jaehoon Chunge8672942014-05-16 13:59:55 +0900491 regs |= DWMCI_DDR_MODE;
492 else
Jaehoon Chung401fc502015-01-14 17:37:53 +0900493 regs &= ~DWMCI_DDR_MODE;
Jaehoon Chunge8672942014-05-16 13:59:55 +0900494
495 dwmci_writel(host, DWMCI_UHS_REG, regs);
496
Siew Chin Limc51e7e12020-12-24 18:21:03 +0800497 if (host->clksel) {
498 int ret;
499
500 ret = host->clksel(host);
501 if (ret)
502 return ret;
503 }
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900504
Urja Rannikko9932a012019-05-13 13:25:27 +0000505#if CONFIG_IS_ENABLED(DM_REGULATOR)
506 if (mmc->vqmmc_supply) {
507 int ret;
508
Jonas Karlmana117d612023-07-19 21:21:00 +0000509 ret = regulator_set_enable_if_allowed(mmc->vqmmc_supply, false);
510 if (ret)
511 return ret;
512
Urja Rannikko9932a012019-05-13 13:25:27 +0000513 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
514 regulator_set_value(mmc->vqmmc_supply, 1800000);
515 else
516 regulator_set_value(mmc->vqmmc_supply, 3300000);
517
518 ret = regulator_set_enable_if_allowed(mmc->vqmmc_supply, true);
519 if (ret)
520 return ret;
521 }
522#endif
523
Simon Glassff5c1b72016-06-12 23:30:23 -0600524 return 0;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000525}
526
527static int dwmci_init(struct mmc *mmc)
528{
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200529 struct dwmci_host *host = mmc->priv;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000530
Jaehoon Chung42f81a82013-11-29 20:08:57 +0900531 if (host->board_init)
532 host->board_init(host);
Rajeshwari Shinde70163092013-10-29 12:53:13 +0530533
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000534 dwmci_writel(host, DWMCI_PWREN, 1);
535
536 if (!dwmci_wait_reset(host, DWMCI_RESET_ALL)) {
Simon Glass4c9b9482015-08-06 20:16:27 -0600537 debug("%s[%d] Fail-reset!!\n", __func__, __LINE__);
538 return -EIO;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000539 }
540
Amar902664c2013-04-27 11:42:54 +0530541 /* Enumerate at 400KHz */
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200542 dwmci_setup_bus(host, mmc->cfg->f_min);
Amar902664c2013-04-27 11:42:54 +0530543
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000544 dwmci_writel(host, DWMCI_RINTSTS, 0xFFFFFFFF);
545 dwmci_writel(host, DWMCI_INTMASK, 0);
546
547 dwmci_writel(host, DWMCI_TMOUT, 0xFFFFFFFF);
548
549 dwmci_writel(host, DWMCI_IDINTEN, 0);
550 dwmci_writel(host, DWMCI_BMOD, 1);
551
Simon Glass6133efa2015-08-06 20:16:29 -0600552 if (!host->fifoth_val) {
553 uint32_t fifo_size;
554
555 fifo_size = dwmci_readl(host, DWMCI_FIFOTH);
556 fifo_size = ((fifo_size & RX_WMARK_MASK) >> RX_WMARK_SHIFT) + 1;
557 host->fifoth_val = MSIZE(0x2) | RX_WMARK(fifo_size / 2 - 1) |
558 TX_WMARK(fifo_size / 2);
Amar902664c2013-04-27 11:42:54 +0530559 }
Simon Glass6133efa2015-08-06 20:16:29 -0600560 dwmci_writel(host, DWMCI_FIFOTH, host->fifoth_val);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000561
562 dwmci_writel(host, DWMCI_CLKENA, 0);
563 dwmci_writel(host, DWMCI_CLKSRC, 0);
564
Ley Foon Tanb98e8922018-12-20 17:55:41 +0800565 if (!host->fifo_mode)
566 dwmci_writel(host, DWMCI_IDINTEN, DWMCI_IDINTEN_MASK);
567
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000568 return 0;
569}
570
Simon Glasseba48f92017-07-29 11:35:31 -0600571#ifdef CONFIG_DM_MMC
Simon Glassff5c1b72016-06-12 23:30:23 -0600572int dwmci_probe(struct udevice *dev)
573{
574 struct mmc *mmc = mmc_get_mmc_dev(dev);
575
576 return dwmci_init(mmc);
577}
578
579const struct dm_mmc_ops dm_dwmci_ops = {
580 .send_cmd = dwmci_send_cmd,
581 .set_ios = dwmci_set_ios,
582};
583
584#else
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200585static const struct mmc_ops dwmci_ops = {
586 .send_cmd = dwmci_send_cmd,
587 .set_ios = dwmci_set_ios,
588 .init = dwmci_init,
589};
Simon Glassff5c1b72016-06-12 23:30:23 -0600590#endif
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200591
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900592void dwmci_setup_cfg(struct mmc_config *cfg, struct dwmci_host *host,
593 u32 max_clk, u32 min_clk)
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000594{
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900595 cfg->name = host->name;
Simon Glasseba48f92017-07-29 11:35:31 -0600596#ifndef CONFIG_DM_MMC
Simon Glass82682542016-05-14 14:03:07 -0600597 cfg->ops = &dwmci_ops;
Simon Glassff5c1b72016-06-12 23:30:23 -0600598#endif
Simon Glass82682542016-05-14 14:03:07 -0600599 cfg->f_min = min_clk;
600 cfg->f_max = max_clk;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000601
Simon Glass82682542016-05-14 14:03:07 -0600602 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000603
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900604 cfg->host_caps = host->caps;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000605
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900606 if (host->buswidth == 8) {
Simon Glass82682542016-05-14 14:03:07 -0600607 cfg->host_caps |= MMC_MODE_8BIT;
608 cfg->host_caps &= ~MMC_MODE_4BIT;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000609 } else {
Simon Glass82682542016-05-14 14:03:07 -0600610 cfg->host_caps |= MMC_MODE_4BIT;
611 cfg->host_caps &= ~MMC_MODE_8BIT;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000612 }
Simon Glass82682542016-05-14 14:03:07 -0600613 cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz;
614
615 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
616}
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200617
Simon Glass82682542016-05-14 14:03:07 -0600618#ifdef CONFIG_BLK
619int dwmci_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg)
620{
621 return mmc_bind(dev, mmc, cfg);
622}
623#else
624int add_dwmci(struct dwmci_host *host, u32 max_clk, u32 min_clk)
625{
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900626 dwmci_setup_cfg(&host->cfg, host, max_clk, min_clk);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000627
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200628 host->mmc = mmc_create(&host->cfg, host);
629 if (host->mmc == NULL)
630 return -1;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000631
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200632 return 0;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000633}
Simon Glass82682542016-05-14 14:03:07 -0600634#endif