blob: 1992d611821dc833398ca921b979c153b08e3135 [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>
Jaehoon Chung757bff42012-10-15 19:10:29 +000016
17#define PAGE_SIZE 4096
18
19static int dwmci_wait_reset(struct dwmci_host *host, u32 value)
20{
21 unsigned long timeout = 1000;
22 u32 ctrl;
23
24 dwmci_writel(host, DWMCI_CTRL, value);
25
26 while (timeout--) {
27 ctrl = dwmci_readl(host, DWMCI_CTRL);
28 if (!(ctrl & DWMCI_RESET_ALL))
29 return 1;
30 }
31 return 0;
32}
33
34static void dwmci_set_idma_desc(struct dwmci_idmac *idmac,
35 u32 desc0, u32 desc1, u32 desc2)
36{
37 struct dwmci_idmac *desc = idmac;
38
39 desc->flags = desc0;
40 desc->cnt = desc1;
41 desc->addr = desc2;
Prabhakar Kushwaha41f7be32015-10-25 13:18:25 +053042 desc->next_addr = (ulong)desc + sizeof(struct dwmci_idmac);
Jaehoon Chung757bff42012-10-15 19:10:29 +000043}
44
45static void dwmci_prepare_data(struct dwmci_host *host,
Alexey Brodkin2a7a2102013-12-26 15:29:07 +040046 struct mmc_data *data,
47 struct dwmci_idmac *cur_idmac,
48 void *bounce_buffer)
Jaehoon Chung757bff42012-10-15 19:10:29 +000049{
50 unsigned long ctrl;
51 unsigned int i = 0, flags, cnt, blk_cnt;
Alexey Brodkin2a7a2102013-12-26 15:29:07 +040052 ulong data_start, data_end;
Jaehoon Chung757bff42012-10-15 19:10:29 +000053
54
55 blk_cnt = data->blocks;
56
57 dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
58
Ley Foon Tan79975992018-12-20 17:55:41 +080059 /* Clear IDMAC interrupt */
60 dwmci_writel(host, DWMCI_IDSTS, 0xFFFFFFFF);
61
Jaehoon Chung757bff42012-10-15 19:10:29 +000062 data_start = (ulong)cur_idmac;
Prabhakar Kushwaha41f7be32015-10-25 13:18:25 +053063 dwmci_writel(host, DWMCI_DBADDR, (ulong)cur_idmac);
Jaehoon Chung757bff42012-10-15 19:10:29 +000064
Jaehoon Chung757bff42012-10-15 19:10:29 +000065 do {
66 flags = DWMCI_IDMAC_OWN | DWMCI_IDMAC_CH ;
67 flags |= (i == 0) ? DWMCI_IDMAC_FS : 0;
68 if (blk_cnt <= 8) {
69 flags |= DWMCI_IDMAC_LD;
70 cnt = data->blocksize * blk_cnt;
71 } else
72 cnt = data->blocksize * 8;
73
74 dwmci_set_idma_desc(cur_idmac, flags, cnt,
Prabhakar Kushwaha41f7be32015-10-25 13:18:25 +053075 (ulong)bounce_buffer + (i * PAGE_SIZE));
Jaehoon Chung757bff42012-10-15 19:10:29 +000076
Marek Vasutbdb5df12019-02-13 20:16:20 +010077 cur_idmac++;
Mischa Jonker21bd5762013-07-26 16:18:40 +020078 if (blk_cnt <= 8)
Jaehoon Chung757bff42012-10-15 19:10:29 +000079 break;
80 blk_cnt -= 8;
Jaehoon Chung757bff42012-10-15 19:10:29 +000081 i++;
82 } while(1);
83
84 data_end = (ulong)cur_idmac;
Marek Vasutbdb5df12019-02-13 20:16:20 +010085 flush_dcache_range(data_start, roundup(data_end, ARCH_DMA_MINALIGN));
Jaehoon Chung757bff42012-10-15 19:10:29 +000086
87 ctrl = dwmci_readl(host, DWMCI_CTRL);
88 ctrl |= DWMCI_IDMAC_EN | DWMCI_DMA_EN;
89 dwmci_writel(host, DWMCI_CTRL, ctrl);
90
91 ctrl = dwmci_readl(host, DWMCI_BMOD);
92 ctrl |= DWMCI_BMOD_IDMAC_FB | DWMCI_BMOD_IDMAC_EN;
93 dwmci_writel(host, DWMCI_BMOD, ctrl);
94
95 dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
96 dwmci_writel(host, DWMCI_BYTCNT, data->blocksize * data->blocks);
97}
98
Heiko Stuebner05fa06b2018-09-21 10:59:45 +020099static int dwmci_fifo_ready(struct dwmci_host *host, u32 bit, u32 *len)
100{
101 u32 timeout = 20000;
102
103 *len = dwmci_readl(host, DWMCI_STATUS);
104 while (--timeout && (*len & bit)) {
105 udelay(200);
106 *len = dwmci_readl(host, DWMCI_STATUS);
107 }
108
109 if (!timeout) {
110 debug("%s: FIFO underflow timeout\n", __func__);
111 return -ETIMEDOUT;
112 }
113
114 return 0;
115}
116
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100117static unsigned int dwmci_get_timeout(struct mmc *mmc, const unsigned int size)
118{
119 unsigned int timeout;
120
121 timeout = size * 8 * 1000; /* counting in bits and msec */
122 timeout *= 2; /* wait twice as long */
123 timeout /= mmc->clock;
124 timeout /= mmc->bus_width;
125 timeout /= mmc->ddr_mode ? 2 : 1;
126 timeout = (timeout < 1000) ? 1000 : timeout;
127
128 return timeout;
129}
130
huang lina65f51b2015-11-17 14:20:22 +0800131static int dwmci_data_transfer(struct dwmci_host *host, struct mmc_data *data)
huang linf382eb82015-11-17 14:20:21 +0800132{
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100133 struct mmc *mmc = host->mmc;
huang linf382eb82015-11-17 14:20:21 +0800134 int ret = 0;
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100135 u32 timeout, mask, size, i, len = 0;
huang lina65f51b2015-11-17 14:20:22 +0800136 u32 *buf = NULL;
huang linf382eb82015-11-17 14:20:21 +0800137 ulong start = get_timer(0);
huang lina65f51b2015-11-17 14:20:22 +0800138 u32 fifo_depth = (((host->fifoth_val & RX_WMARK_MASK) >>
139 RX_WMARK_SHIFT) + 1) * 2;
140
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100141 size = data->blocksize * data->blocks;
huang lina65f51b2015-11-17 14:20:22 +0800142 if (data->flags == MMC_DATA_READ)
143 buf = (unsigned int *)data->dest;
144 else
145 buf = (unsigned int *)data->src;
huang linf382eb82015-11-17 14:20:21 +0800146
Marek Vasut4e16f0a2019-03-23 03:32:24 +0100147 timeout = dwmci_get_timeout(mmc, size);
148
149 size /= 4;
150
huang linf382eb82015-11-17 14:20:21 +0800151 for (;;) {
152 mask = dwmci_readl(host, DWMCI_RINTSTS);
153 /* Error during data transfer. */
154 if (mask & (DWMCI_DATA_ERR | DWMCI_DATA_TOUT)) {
155 debug("%s: DATA ERROR!\n", __func__);
156 ret = -EINVAL;
157 break;
158 }
159
huang lina65f51b2015-11-17 14:20:22 +0800160 if (host->fifo_mode && size) {
Xu Ziyuan720724d2016-07-28 10:25:48 +0800161 len = 0;
Jacob Chen2b429032016-09-19 10:16:50 +0800162 if (data->flags == MMC_DATA_READ &&
163 (mask & DWMCI_INTMSK_RXDR)) {
164 while (size) {
Heiko Stuebner05fa06b2018-09-21 10:59:45 +0200165 ret = dwmci_fifo_ready(host,
166 DWMCI_FIFO_EMPTY,
167 &len);
168 if (ret < 0)
169 break;
170
huang lina65f51b2015-11-17 14:20:22 +0800171 len = (len >> DWMCI_FIFO_SHIFT) &
172 DWMCI_FIFO_MASK;
Xu Ziyuan2990e072016-07-28 10:25:47 +0800173 len = min(size, len);
huang lina65f51b2015-11-17 14:20:22 +0800174 for (i = 0; i < len; i++)
175 *buf++ =
176 dwmci_readl(host, DWMCI_DATA);
Jacob Chen2b429032016-09-19 10:16:50 +0800177 size = size > len ? (size - len) : 0;
huang lina65f51b2015-11-17 14:20:22 +0800178 }
Jacob Chen2b429032016-09-19 10:16:50 +0800179 dwmci_writel(host, DWMCI_RINTSTS,
180 DWMCI_INTMSK_RXDR);
181 } else if (data->flags == MMC_DATA_WRITE &&
182 (mask & DWMCI_INTMSK_TXDR)) {
183 while (size) {
Heiko Stuebner05fa06b2018-09-21 10:59:45 +0200184 ret = dwmci_fifo_ready(host,
185 DWMCI_FIFO_FULL,
186 &len);
187 if (ret < 0)
188 break;
189
huang lina65f51b2015-11-17 14:20:22 +0800190 len = fifo_depth - ((len >>
191 DWMCI_FIFO_SHIFT) &
192 DWMCI_FIFO_MASK);
Xu Ziyuan2990e072016-07-28 10:25:47 +0800193 len = min(size, len);
huang lina65f51b2015-11-17 14:20:22 +0800194 for (i = 0; i < len; i++)
195 dwmci_writel(host, DWMCI_DATA,
196 *buf++);
Jacob Chen2b429032016-09-19 10:16:50 +0800197 size = size > len ? (size - len) : 0;
huang lina65f51b2015-11-17 14:20:22 +0800198 }
Jacob Chen2b429032016-09-19 10:16:50 +0800199 dwmci_writel(host, DWMCI_RINTSTS,
200 DWMCI_INTMSK_TXDR);
huang lina65f51b2015-11-17 14:20:22 +0800201 }
huang lina65f51b2015-11-17 14:20:22 +0800202 }
203
huang linf382eb82015-11-17 14:20:21 +0800204 /* Data arrived correctly. */
205 if (mask & DWMCI_INTMSK_DTO) {
206 ret = 0;
207 break;
208 }
209
210 /* Check for timeout. */
211 if (get_timer(start) > timeout) {
212 debug("%s: Timeout waiting for data!\n",
213 __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900214 ret = -ETIMEDOUT;
huang linf382eb82015-11-17 14:20:21 +0800215 break;
216 }
217 }
218
219 dwmci_writel(host, DWMCI_RINTSTS, mask);
220
221 return ret;
222}
223
Jaehoon Chung757bff42012-10-15 19:10:29 +0000224static int dwmci_set_transfer_mode(struct dwmci_host *host,
225 struct mmc_data *data)
226{
227 unsigned long mode;
228
229 mode = DWMCI_CMD_DATA_EXP;
230 if (data->flags & MMC_DATA_WRITE)
231 mode |= DWMCI_CMD_RW;
232
233 return mode;
234}
235
Simon Glasse7881d82017-07-29 11:35:31 -0600236#ifdef CONFIG_DM_MMC
Jaehoon Chung56283472016-06-28 15:52:21 +0900237static int dwmci_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
Simon Glass691272f2016-06-12 23:30:23 -0600238 struct mmc_data *data)
239{
240 struct mmc *mmc = mmc_get_mmc_dev(dev);
241#else
Jaehoon Chung757bff42012-10-15 19:10:29 +0000242static int dwmci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
243 struct mmc_data *data)
244{
Simon Glass691272f2016-06-12 23:30:23 -0600245#endif
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200246 struct dwmci_host *host = mmc->priv;
Mischa Jonker2136d222013-07-26 14:08:14 +0200247 ALLOC_CACHE_ALIGN_BUFFER(struct dwmci_idmac, cur_idmac,
Mischa Jonker21bd5762013-07-26 16:18:40 +0200248 data ? DIV_ROUND_UP(data->blocks, 8) : 0);
Marek Vasut9042d972015-07-27 22:39:38 +0200249 int ret = 0, flags = 0, i;
Xu Ziyuan02ebd422016-07-19 09:38:22 +0800250 unsigned int timeout = 500;
Alexander Graf9b5b8b62016-03-04 01:09:52 +0100251 u32 retry = 100000;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000252 u32 mask, ctrl;
Amar9c50e352013-04-27 11:42:54 +0530253 ulong start = get_timer(0);
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400254 struct bounce_buffer bbstate;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000255
256 while (dwmci_readl(host, DWMCI_STATUS) & DWMCI_BUSY) {
Amar9c50e352013-04-27 11:42:54 +0530257 if (get_timer(start) > timeout) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600258 debug("%s: Timeout on data busy\n", __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900259 return -ETIMEDOUT;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000260 }
Jaehoon Chung757bff42012-10-15 19:10:29 +0000261 }
262
263 dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_ALL);
264
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400265 if (data) {
huang lina65f51b2015-11-17 14:20:22 +0800266 if (host->fifo_mode) {
267 dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
268 dwmci_writel(host, DWMCI_BYTCNT,
269 data->blocksize * data->blocks);
270 dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400271 } else {
huang lina65f51b2015-11-17 14:20:22 +0800272 if (data->flags == MMC_DATA_READ) {
Marek Vasut6ad5aec2019-03-23 18:45:27 +0100273 ret = bounce_buffer_start(&bbstate,
274 (void*)data->dest,
huang lina65f51b2015-11-17 14:20:22 +0800275 data->blocksize *
276 data->blocks, GEN_BB_WRITE);
277 } else {
Marek Vasut6ad5aec2019-03-23 18:45:27 +0100278 ret = bounce_buffer_start(&bbstate,
279 (void*)data->src,
huang lina65f51b2015-11-17 14:20:22 +0800280 data->blocksize *
281 data->blocks, GEN_BB_READ);
282 }
Marek Vasut6ad5aec2019-03-23 18:45:27 +0100283
284 if (ret)
285 return ret;
286
huang lina65f51b2015-11-17 14:20:22 +0800287 dwmci_prepare_data(host, data, cur_idmac,
288 bbstate.bounce_buffer);
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400289 }
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400290 }
Jaehoon Chung757bff42012-10-15 19:10:29 +0000291
Jaehoon Chung757bff42012-10-15 19:10:29 +0000292 dwmci_writel(host, DWMCI_CMDARG, cmd->cmdarg);
293
294 if (data)
295 flags = dwmci_set_transfer_mode(host, data);
296
297 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
298 return -1;
299
300 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
301 flags |= DWMCI_CMD_ABORT_STOP;
302 else
303 flags |= DWMCI_CMD_PRV_DAT_WAIT;
304
305 if (cmd->resp_type & MMC_RSP_PRESENT) {
306 flags |= DWMCI_CMD_RESP_EXP;
307 if (cmd->resp_type & MMC_RSP_136)
308 flags |= DWMCI_CMD_RESP_LENGTH;
309 }
310
311 if (cmd->resp_type & MMC_RSP_CRC)
312 flags |= DWMCI_CMD_CHECK_CRC;
313
314 flags |= (cmd->cmdidx | DWMCI_CMD_START | DWMCI_CMD_USE_HOLD_REG);
315
316 debug("Sending CMD%d\n",cmd->cmdidx);
317
318 dwmci_writel(host, DWMCI_CMD, flags);
319
320 for (i = 0; i < retry; i++) {
321 mask = dwmci_readl(host, DWMCI_RINTSTS);
322 if (mask & DWMCI_INTMSK_CDONE) {
323 if (!data)
324 dwmci_writel(host, DWMCI_RINTSTS, mask);
325 break;
326 }
327 }
328
Pavel Machekf33c9302014-09-05 12:49:48 +0200329 if (i == retry) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600330 debug("%s: Timeout.\n", __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900331 return -ETIMEDOUT;
Pavel Machekf33c9302014-09-05 12:49:48 +0200332 }
Jaehoon Chung757bff42012-10-15 19:10:29 +0000333
334 if (mask & DWMCI_INTMSK_RTO) {
Pavel Machekf33c9302014-09-05 12:49:48 +0200335 /*
336 * Timeout here is not necessarily fatal. (e)MMC cards
337 * will splat here when they receive CMD55 as they do
338 * not support this command and that is exactly the way
339 * to tell them apart from SD cards. Thus, this output
340 * below shall be debug(). eMMC cards also do not favor
341 * CMD8, please keep that in mind.
342 */
343 debug("%s: Response Timeout.\n", __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900344 return -ETIMEDOUT;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000345 } else if (mask & DWMCI_INTMSK_RE) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600346 debug("%s: Response Error.\n", __func__);
347 return -EIO;
Marek Vasut26cc40d2018-11-06 23:42:11 +0100348 } else if ((cmd->resp_type & MMC_RSP_CRC) &&
349 (mask & DWMCI_INTMSK_RCRC)) {
350 debug("%s: Response CRC Error.\n", __func__);
351 return -EIO;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000352 }
353
354
355 if (cmd->resp_type & MMC_RSP_PRESENT) {
356 if (cmd->resp_type & MMC_RSP_136) {
357 cmd->response[0] = dwmci_readl(host, DWMCI_RESP3);
358 cmd->response[1] = dwmci_readl(host, DWMCI_RESP2);
359 cmd->response[2] = dwmci_readl(host, DWMCI_RESP1);
360 cmd->response[3] = dwmci_readl(host, DWMCI_RESP0);
361 } else {
362 cmd->response[0] = dwmci_readl(host, DWMCI_RESP0);
363 }
364 }
365
366 if (data) {
huang lina65f51b2015-11-17 14:20:22 +0800367 ret = dwmci_data_transfer(host, data);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000368
huang lina65f51b2015-11-17 14:20:22 +0800369 /* only dma mode need it */
370 if (!host->fifo_mode) {
Ley Foon Tan79975992018-12-20 17:55:41 +0800371 if (data->flags == MMC_DATA_READ)
372 mask = DWMCI_IDINTEN_RI;
373 else
374 mask = DWMCI_IDINTEN_TI;
375 ret = wait_for_bit_le32(host->ioaddr + DWMCI_IDSTS,
376 mask, true, 1000, false);
377 if (ret)
378 debug("%s: DWMCI_IDINTEN mask 0x%x timeout.\n",
379 __func__, mask);
380 /* clear interrupts */
381 dwmci_writel(host, DWMCI_IDSTS, DWMCI_IDINTEN_MASK);
382
huang lina65f51b2015-11-17 14:20:22 +0800383 ctrl = dwmci_readl(host, DWMCI_CTRL);
384 ctrl &= ~(DWMCI_DMA_EN);
385 dwmci_writel(host, DWMCI_CTRL, ctrl);
386 bounce_buffer_stop(&bbstate);
387 }
Jaehoon Chung757bff42012-10-15 19:10:29 +0000388 }
389
390 udelay(100);
391
Marek Vasut9042d972015-07-27 22:39:38 +0200392 return ret;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000393}
394
395static int dwmci_setup_bus(struct dwmci_host *host, u32 freq)
396{
397 u32 div, status;
398 int timeout = 10000;
399 unsigned long sclk;
400
Amar9c50e352013-04-27 11:42:54 +0530401 if ((freq == host->clock) || (freq == 0))
Jaehoon Chung757bff42012-10-15 19:10:29 +0000402 return 0;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000403 /*
Pavel Machekf33c9302014-09-05 12:49:48 +0200404 * If host->get_mmc_clk isn't defined,
Jaehoon Chung757bff42012-10-15 19:10:29 +0000405 * then assume that host->bus_hz is source clock value.
Pavel Machekf33c9302014-09-05 12:49:48 +0200406 * host->bus_hz should be set by user.
Jaehoon Chung757bff42012-10-15 19:10:29 +0000407 */
Jaehoon Chungb44fe832013-10-06 18:59:31 +0900408 if (host->get_mmc_clk)
Simon Glasse3563f22015-08-30 16:55:15 -0600409 sclk = host->get_mmc_clk(host, freq);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000410 else if (host->bus_hz)
411 sclk = host->bus_hz;
412 else {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600413 debug("%s: Didn't get source clock value.\n", __func__);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000414 return -EINVAL;
415 }
416
Chin Liang See6ace1532014-06-10 01:26:52 -0500417 if (sclk == freq)
418 div = 0; /* bypass mode */
419 else
420 div = DIV_ROUND_UP(sclk, 2 * freq);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000421
422 dwmci_writel(host, DWMCI_CLKENA, 0);
423 dwmci_writel(host, DWMCI_CLKSRC, 0);
424
425 dwmci_writel(host, DWMCI_CLKDIV, div);
426 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
427 DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
428
429 do {
430 status = dwmci_readl(host, DWMCI_CMD);
431 if (timeout-- < 0) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600432 debug("%s: Timeout!\n", __func__);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000433 return -ETIMEDOUT;
434 }
435 } while (status & DWMCI_CMD_START);
436
437 dwmci_writel(host, DWMCI_CLKENA, DWMCI_CLKEN_ENABLE |
438 DWMCI_CLKEN_LOW_PWR);
439
440 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
441 DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
442
443 timeout = 10000;
444 do {
445 status = dwmci_readl(host, DWMCI_CMD);
446 if (timeout-- < 0) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600447 debug("%s: Timeout!\n", __func__);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000448 return -ETIMEDOUT;
449 }
450 } while (status & DWMCI_CMD_START);
451
452 host->clock = freq;
453
454 return 0;
455}
456
Simon Glasse7881d82017-07-29 11:35:31 -0600457#ifdef CONFIG_DM_MMC
Jaehoon Chung56283472016-06-28 15:52:21 +0900458static int dwmci_set_ios(struct udevice *dev)
Simon Glass691272f2016-06-12 23:30:23 -0600459{
460 struct mmc *mmc = mmc_get_mmc_dev(dev);
461#else
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900462static int dwmci_set_ios(struct mmc *mmc)
Jaehoon Chung757bff42012-10-15 19:10:29 +0000463{
Simon Glass691272f2016-06-12 23:30:23 -0600464#endif
Jaehoon Chung045bdcd2014-05-16 13:59:55 +0900465 struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
466 u32 ctype, regs;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000467
Pavel Machekf33c9302014-09-05 12:49:48 +0200468 debug("Buswidth = %d, clock: %d\n", mmc->bus_width, mmc->clock);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000469
470 dwmci_setup_bus(host, mmc->clock);
471 switch (mmc->bus_width) {
472 case 8:
473 ctype = DWMCI_CTYPE_8BIT;
474 break;
475 case 4:
476 ctype = DWMCI_CTYPE_4BIT;
477 break;
478 default:
479 ctype = DWMCI_CTYPE_1BIT;
480 break;
481 }
482
483 dwmci_writel(host, DWMCI_CTYPE, ctype);
484
Jaehoon Chung045bdcd2014-05-16 13:59:55 +0900485 regs = dwmci_readl(host, DWMCI_UHS_REG);
Andrew Gabbasov2b8a9692014-12-01 06:59:12 -0600486 if (mmc->ddr_mode)
Jaehoon Chung045bdcd2014-05-16 13:59:55 +0900487 regs |= DWMCI_DDR_MODE;
488 else
Jaehoon Chungafc9e2b2015-01-14 17:37:53 +0900489 regs &= ~DWMCI_DDR_MODE;
Jaehoon Chung045bdcd2014-05-16 13:59:55 +0900490
491 dwmci_writel(host, DWMCI_UHS_REG, regs);
492
Jaehoon Chung757bff42012-10-15 19:10:29 +0000493 if (host->clksel)
494 host->clksel(host);
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900495
Simon Glass691272f2016-06-12 23:30:23 -0600496 return 0;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000497}
498
499static int dwmci_init(struct mmc *mmc)
500{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200501 struct dwmci_host *host = mmc->priv;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000502
Jaehoon Chung18ab6752013-11-29 20:08:57 +0900503 if (host->board_init)
504 host->board_init(host);
Rajeshwari Shinde6f0b7ca2013-10-29 12:53:13 +0530505
Jaehoon Chung757bff42012-10-15 19:10:29 +0000506 dwmci_writel(host, DWMCI_PWREN, 1);
507
508 if (!dwmci_wait_reset(host, DWMCI_RESET_ALL)) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600509 debug("%s[%d] Fail-reset!!\n", __func__, __LINE__);
510 return -EIO;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000511 }
512
Amar9c50e352013-04-27 11:42:54 +0530513 /* Enumerate at 400KHz */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200514 dwmci_setup_bus(host, mmc->cfg->f_min);
Amar9c50e352013-04-27 11:42:54 +0530515
Jaehoon Chung757bff42012-10-15 19:10:29 +0000516 dwmci_writel(host, DWMCI_RINTSTS, 0xFFFFFFFF);
517 dwmci_writel(host, DWMCI_INTMASK, 0);
518
519 dwmci_writel(host, DWMCI_TMOUT, 0xFFFFFFFF);
520
521 dwmci_writel(host, DWMCI_IDINTEN, 0);
522 dwmci_writel(host, DWMCI_BMOD, 1);
523
Simon Glass760177d2015-08-06 20:16:29 -0600524 if (!host->fifoth_val) {
525 uint32_t fifo_size;
526
527 fifo_size = dwmci_readl(host, DWMCI_FIFOTH);
528 fifo_size = ((fifo_size & RX_WMARK_MASK) >> RX_WMARK_SHIFT) + 1;
529 host->fifoth_val = MSIZE(0x2) | RX_WMARK(fifo_size / 2 - 1) |
530 TX_WMARK(fifo_size / 2);
Amar9c50e352013-04-27 11:42:54 +0530531 }
Simon Glass760177d2015-08-06 20:16:29 -0600532 dwmci_writel(host, DWMCI_FIFOTH, host->fifoth_val);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000533
534 dwmci_writel(host, DWMCI_CLKENA, 0);
535 dwmci_writel(host, DWMCI_CLKSRC, 0);
536
Ley Foon Tan79975992018-12-20 17:55:41 +0800537 if (!host->fifo_mode)
538 dwmci_writel(host, DWMCI_IDINTEN, DWMCI_IDINTEN_MASK);
539
Jaehoon Chung757bff42012-10-15 19:10:29 +0000540 return 0;
541}
542
Simon Glasse7881d82017-07-29 11:35:31 -0600543#ifdef CONFIG_DM_MMC
Simon Glass691272f2016-06-12 23:30:23 -0600544int dwmci_probe(struct udevice *dev)
545{
546 struct mmc *mmc = mmc_get_mmc_dev(dev);
547
548 return dwmci_init(mmc);
549}
550
551const struct dm_mmc_ops dm_dwmci_ops = {
552 .send_cmd = dwmci_send_cmd,
553 .set_ios = dwmci_set_ios,
554};
555
556#else
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200557static const struct mmc_ops dwmci_ops = {
558 .send_cmd = dwmci_send_cmd,
559 .set_ios = dwmci_set_ios,
560 .init = dwmci_init,
561};
Simon Glass691272f2016-06-12 23:30:23 -0600562#endif
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200563
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900564void dwmci_setup_cfg(struct mmc_config *cfg, struct dwmci_host *host,
565 u32 max_clk, u32 min_clk)
Simon Glass5e6ff812016-05-14 14:03:07 -0600566{
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900567 cfg->name = host->name;
Simon Glasse7881d82017-07-29 11:35:31 -0600568#ifndef CONFIG_DM_MMC
Simon Glass5e6ff812016-05-14 14:03:07 -0600569 cfg->ops = &dwmci_ops;
Simon Glass691272f2016-06-12 23:30:23 -0600570#endif
Simon Glass5e6ff812016-05-14 14:03:07 -0600571 cfg->f_min = min_clk;
572 cfg->f_max = max_clk;
573
574 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
575
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900576 cfg->host_caps = host->caps;
Simon Glass5e6ff812016-05-14 14:03:07 -0600577
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900578 if (host->buswidth == 8) {
Simon Glass5e6ff812016-05-14 14:03:07 -0600579 cfg->host_caps |= MMC_MODE_8BIT;
580 cfg->host_caps &= ~MMC_MODE_4BIT;
581 } else {
582 cfg->host_caps |= MMC_MODE_4BIT;
583 cfg->host_caps &= ~MMC_MODE_8BIT;
584 }
585 cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz;
586
587 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
588}
589
590#ifdef CONFIG_BLK
591int dwmci_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg)
592{
593 return mmc_bind(dev, mmc, cfg);
594}
595#else
Jaehoon Chung757bff42012-10-15 19:10:29 +0000596int add_dwmci(struct dwmci_host *host, u32 max_clk, u32 min_clk)
597{
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900598 dwmci_setup_cfg(&host->cfg, host, max_clk, min_clk);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000599
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200600 host->mmc = mmc_create(&host->cfg, host);
601 if (host->mmc == NULL)
602 return -1;
603
604 return 0;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000605}
Simon Glass5e6ff812016-05-14 14:03:07 -0600606#endif