blob: 93a836eac3629bde23f89e62747e17c41eea3ab0 [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 Glass4c9b9482015-08-06 20:16:27 -060010#include <errno.h>
Jaehoon Chung7cf73072012-10-15 19:10:29 +000011#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060012#include <memalign.h>
Jaehoon Chung7cf73072012-10-15 19:10:29 +000013#include <mmc.h>
14#include <dwmmc.h>
Ley Foon Tanb98e8922018-12-20 17:55:41 +080015#include <wait_bit.h>
Jaehoon Chung7cf73072012-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 Kushwahafdefb902015-10-25 13:18:25 +053042 desc->next_addr = (ulong)desc + sizeof(struct dwmci_idmac);
Jaehoon Chung7cf73072012-10-15 19:10:29 +000043}
44
45static void dwmci_prepare_data(struct dwmci_host *host,
Alexey Brodkin55bab5e2013-12-26 15:29:07 +040046 struct mmc_data *data,
47 struct dwmci_idmac *cur_idmac,
48 void *bounce_buffer)
Jaehoon Chung7cf73072012-10-15 19:10:29 +000049{
50 unsigned long ctrl;
51 unsigned int i = 0, flags, cnt, blk_cnt;
Alexey Brodkin55bab5e2013-12-26 15:29:07 +040052 ulong data_start, data_end;
Jaehoon Chung7cf73072012-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 Tanb98e8922018-12-20 17:55:41 +080059 /* Clear IDMAC interrupt */
60 dwmci_writel(host, DWMCI_IDSTS, 0xFFFFFFFF);
61
Jaehoon Chung7cf73072012-10-15 19:10:29 +000062 data_start = (ulong)cur_idmac;
Prabhakar Kushwahafdefb902015-10-25 13:18:25 +053063 dwmci_writel(host, DWMCI_DBADDR, (ulong)cur_idmac);
Jaehoon Chung7cf73072012-10-15 19:10:29 +000064
Jaehoon Chung7cf73072012-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 Kushwahafdefb902015-10-25 13:18:25 +053075 (ulong)bounce_buffer + (i * PAGE_SIZE));
Jaehoon Chung7cf73072012-10-15 19:10:29 +000076
Mischa Jonkera7a60912013-07-26 16:18:40 +020077 if (blk_cnt <= 8)
Jaehoon Chung7cf73072012-10-15 19:10:29 +000078 break;
79 blk_cnt -= 8;
80 cur_idmac++;
81 i++;
82 } while(1);
83
84 data_end = (ulong)cur_idmac;
85 flush_dcache_range(data_start, data_end + ARCH_DMA_MINALIGN);
86
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 Stuebner46b7a4f2018-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
huang lin50b73752015-11-17 14:20:22 +0800117static int dwmci_data_transfer(struct dwmci_host *host, struct mmc_data *data)
huang linf9836762015-11-17 14:20:21 +0800118{
119 int ret = 0;
huang lin50b73752015-11-17 14:20:22 +0800120 u32 timeout = 240000;
121 u32 mask, size, i, len = 0;
122 u32 *buf = NULL;
huang linf9836762015-11-17 14:20:21 +0800123 ulong start = get_timer(0);
huang lin50b73752015-11-17 14:20:22 +0800124 u32 fifo_depth = (((host->fifoth_val & RX_WMARK_MASK) >>
125 RX_WMARK_SHIFT) + 1) * 2;
126
127 size = data->blocksize * data->blocks / 4;
128 if (data->flags == MMC_DATA_READ)
129 buf = (unsigned int *)data->dest;
130 else
131 buf = (unsigned int *)data->src;
huang linf9836762015-11-17 14:20:21 +0800132
133 for (;;) {
134 mask = dwmci_readl(host, DWMCI_RINTSTS);
135 /* Error during data transfer. */
136 if (mask & (DWMCI_DATA_ERR | DWMCI_DATA_TOUT)) {
137 debug("%s: DATA ERROR!\n", __func__);
138 ret = -EINVAL;
139 break;
140 }
141
huang lin50b73752015-11-17 14:20:22 +0800142 if (host->fifo_mode && size) {
Xu Ziyuan5b8bf122016-07-28 10:25:48 +0800143 len = 0;
Jacob Chen953d9752016-09-19 10:16:50 +0800144 if (data->flags == MMC_DATA_READ &&
145 (mask & DWMCI_INTMSK_RXDR)) {
146 while (size) {
Heiko Stuebner46b7a4f2018-09-21 10:59:45 +0200147 ret = dwmci_fifo_ready(host,
148 DWMCI_FIFO_EMPTY,
149 &len);
150 if (ret < 0)
151 break;
152
huang lin50b73752015-11-17 14:20:22 +0800153 len = (len >> DWMCI_FIFO_SHIFT) &
154 DWMCI_FIFO_MASK;
Xu Ziyuan6577a2a2016-07-28 10:25:47 +0800155 len = min(size, len);
huang lin50b73752015-11-17 14:20:22 +0800156 for (i = 0; i < len; i++)
157 *buf++ =
158 dwmci_readl(host, DWMCI_DATA);
Jacob Chen953d9752016-09-19 10:16:50 +0800159 size = size > len ? (size - len) : 0;
huang lin50b73752015-11-17 14:20:22 +0800160 }
Jacob Chen953d9752016-09-19 10:16:50 +0800161 dwmci_writel(host, DWMCI_RINTSTS,
162 DWMCI_INTMSK_RXDR);
163 } else if (data->flags == MMC_DATA_WRITE &&
164 (mask & DWMCI_INTMSK_TXDR)) {
165 while (size) {
Heiko Stuebner46b7a4f2018-09-21 10:59:45 +0200166 ret = dwmci_fifo_ready(host,
167 DWMCI_FIFO_FULL,
168 &len);
169 if (ret < 0)
170 break;
171
huang lin50b73752015-11-17 14:20:22 +0800172 len = fifo_depth - ((len >>
173 DWMCI_FIFO_SHIFT) &
174 DWMCI_FIFO_MASK);
Xu Ziyuan6577a2a2016-07-28 10:25:47 +0800175 len = min(size, len);
huang lin50b73752015-11-17 14:20:22 +0800176 for (i = 0; i < len; i++)
177 dwmci_writel(host, DWMCI_DATA,
178 *buf++);
Jacob Chen953d9752016-09-19 10:16:50 +0800179 size = size > len ? (size - len) : 0;
huang lin50b73752015-11-17 14:20:22 +0800180 }
Jacob Chen953d9752016-09-19 10:16:50 +0800181 dwmci_writel(host, DWMCI_RINTSTS,
182 DWMCI_INTMSK_TXDR);
huang lin50b73752015-11-17 14:20:22 +0800183 }
huang lin50b73752015-11-17 14:20:22 +0800184 }
185
huang linf9836762015-11-17 14:20:21 +0800186 /* Data arrived correctly. */
187 if (mask & DWMCI_INTMSK_DTO) {
188 ret = 0;
189 break;
190 }
191
192 /* Check for timeout. */
193 if (get_timer(start) > timeout) {
194 debug("%s: Timeout waiting for data!\n",
195 __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900196 ret = -ETIMEDOUT;
huang linf9836762015-11-17 14:20:21 +0800197 break;
198 }
199 }
200
201 dwmci_writel(host, DWMCI_RINTSTS, mask);
202
203 return ret;
204}
205
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000206static int dwmci_set_transfer_mode(struct dwmci_host *host,
207 struct mmc_data *data)
208{
209 unsigned long mode;
210
211 mode = DWMCI_CMD_DATA_EXP;
212 if (data->flags & MMC_DATA_WRITE)
213 mode |= DWMCI_CMD_RW;
214
215 return mode;
216}
217
Simon Glasseba48f92017-07-29 11:35:31 -0600218#ifdef CONFIG_DM_MMC
Jaehoon Chungad220ac2016-06-28 15:52:21 +0900219static int dwmci_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
Simon Glassff5c1b72016-06-12 23:30:23 -0600220 struct mmc_data *data)
221{
222 struct mmc *mmc = mmc_get_mmc_dev(dev);
223#else
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000224static int dwmci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
225 struct mmc_data *data)
226{
Simon Glassff5c1b72016-06-12 23:30:23 -0600227#endif
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200228 struct dwmci_host *host = mmc->priv;
Mischa Jonker7423bed2013-07-26 14:08:14 +0200229 ALLOC_CACHE_ALIGN_BUFFER(struct dwmci_idmac, cur_idmac,
Mischa Jonkera7a60912013-07-26 16:18:40 +0200230 data ? DIV_ROUND_UP(data->blocks, 8) : 0);
Marek Vasut81e093f2015-07-27 22:39:38 +0200231 int ret = 0, flags = 0, i;
Xu Ziyuan34a10d32016-07-19 09:38:22 +0800232 unsigned int timeout = 500;
Alexander Graf61c2a662016-03-04 01:09:52 +0100233 u32 retry = 100000;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000234 u32 mask, ctrl;
Amar902664c2013-04-27 11:42:54 +0530235 ulong start = get_timer(0);
Alexey Brodkin55bab5e2013-12-26 15:29:07 +0400236 struct bounce_buffer bbstate;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000237
238 while (dwmci_readl(host, DWMCI_STATUS) & DWMCI_BUSY) {
Amar902664c2013-04-27 11:42:54 +0530239 if (get_timer(start) > timeout) {
Simon Glass4c9b9482015-08-06 20:16:27 -0600240 debug("%s: Timeout on data busy\n", __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900241 return -ETIMEDOUT;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000242 }
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000243 }
244
245 dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_ALL);
246
Alexey Brodkin55bab5e2013-12-26 15:29:07 +0400247 if (data) {
huang lin50b73752015-11-17 14:20:22 +0800248 if (host->fifo_mode) {
249 dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
250 dwmci_writel(host, DWMCI_BYTCNT,
251 data->blocksize * data->blocks);
252 dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
Alexey Brodkin55bab5e2013-12-26 15:29:07 +0400253 } else {
huang lin50b73752015-11-17 14:20:22 +0800254 if (data->flags == MMC_DATA_READ) {
255 bounce_buffer_start(&bbstate, (void*)data->dest,
256 data->blocksize *
257 data->blocks, GEN_BB_WRITE);
258 } else {
259 bounce_buffer_start(&bbstate, (void*)data->src,
260 data->blocksize *
261 data->blocks, GEN_BB_READ);
262 }
263 dwmci_prepare_data(host, data, cur_idmac,
264 bbstate.bounce_buffer);
Alexey Brodkin55bab5e2013-12-26 15:29:07 +0400265 }
Alexey Brodkin55bab5e2013-12-26 15:29:07 +0400266 }
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000267
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000268 dwmci_writel(host, DWMCI_CMDARG, cmd->cmdarg);
269
270 if (data)
271 flags = dwmci_set_transfer_mode(host, data);
272
273 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
274 return -1;
275
276 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
277 flags |= DWMCI_CMD_ABORT_STOP;
278 else
279 flags |= DWMCI_CMD_PRV_DAT_WAIT;
280
281 if (cmd->resp_type & MMC_RSP_PRESENT) {
282 flags |= DWMCI_CMD_RESP_EXP;
283 if (cmd->resp_type & MMC_RSP_136)
284 flags |= DWMCI_CMD_RESP_LENGTH;
285 }
286
287 if (cmd->resp_type & MMC_RSP_CRC)
288 flags |= DWMCI_CMD_CHECK_CRC;
289
290 flags |= (cmd->cmdidx | DWMCI_CMD_START | DWMCI_CMD_USE_HOLD_REG);
291
292 debug("Sending CMD%d\n",cmd->cmdidx);
293
294 dwmci_writel(host, DWMCI_CMD, flags);
295
296 for (i = 0; i < retry; i++) {
297 mask = dwmci_readl(host, DWMCI_RINTSTS);
298 if (mask & DWMCI_INTMSK_CDONE) {
299 if (!data)
300 dwmci_writel(host, DWMCI_RINTSTS, mask);
301 break;
302 }
303 }
304
Pavel Macheka425f5d2014-09-05 12:49:48 +0200305 if (i == retry) {
Simon Glass4c9b9482015-08-06 20:16:27 -0600306 debug("%s: Timeout.\n", __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900307 return -ETIMEDOUT;
Pavel Macheka425f5d2014-09-05 12:49:48 +0200308 }
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000309
310 if (mask & DWMCI_INTMSK_RTO) {
Pavel Macheka425f5d2014-09-05 12:49:48 +0200311 /*
312 * Timeout here is not necessarily fatal. (e)MMC cards
313 * will splat here when they receive CMD55 as they do
314 * not support this command and that is exactly the way
315 * to tell them apart from SD cards. Thus, this output
316 * below shall be debug(). eMMC cards also do not favor
317 * CMD8, please keep that in mind.
318 */
319 debug("%s: Response Timeout.\n", __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900320 return -ETIMEDOUT;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000321 } else if (mask & DWMCI_INTMSK_RE) {
Simon Glass4c9b9482015-08-06 20:16:27 -0600322 debug("%s: Response Error.\n", __func__);
323 return -EIO;
Marek Vasuta6d91992018-11-06 23:42:11 +0100324 } else if ((cmd->resp_type & MMC_RSP_CRC) &&
325 (mask & DWMCI_INTMSK_RCRC)) {
326 debug("%s: Response CRC Error.\n", __func__);
327 return -EIO;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000328 }
329
330
331 if (cmd->resp_type & MMC_RSP_PRESENT) {
332 if (cmd->resp_type & MMC_RSP_136) {
333 cmd->response[0] = dwmci_readl(host, DWMCI_RESP3);
334 cmd->response[1] = dwmci_readl(host, DWMCI_RESP2);
335 cmd->response[2] = dwmci_readl(host, DWMCI_RESP1);
336 cmd->response[3] = dwmci_readl(host, DWMCI_RESP0);
337 } else {
338 cmd->response[0] = dwmci_readl(host, DWMCI_RESP0);
339 }
340 }
341
342 if (data) {
huang lin50b73752015-11-17 14:20:22 +0800343 ret = dwmci_data_transfer(host, data);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000344
huang lin50b73752015-11-17 14:20:22 +0800345 /* only dma mode need it */
346 if (!host->fifo_mode) {
Ley Foon Tanb98e8922018-12-20 17:55:41 +0800347 if (data->flags == MMC_DATA_READ)
348 mask = DWMCI_IDINTEN_RI;
349 else
350 mask = DWMCI_IDINTEN_TI;
351 ret = wait_for_bit_le32(host->ioaddr + DWMCI_IDSTS,
352 mask, true, 1000, false);
353 if (ret)
354 debug("%s: DWMCI_IDINTEN mask 0x%x timeout.\n",
355 __func__, mask);
356 /* clear interrupts */
357 dwmci_writel(host, DWMCI_IDSTS, DWMCI_IDINTEN_MASK);
358
huang lin50b73752015-11-17 14:20:22 +0800359 ctrl = dwmci_readl(host, DWMCI_CTRL);
360 ctrl &= ~(DWMCI_DMA_EN);
361 dwmci_writel(host, DWMCI_CTRL, ctrl);
362 bounce_buffer_stop(&bbstate);
363 }
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000364 }
365
366 udelay(100);
367
Marek Vasut81e093f2015-07-27 22:39:38 +0200368 return ret;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000369}
370
371static int dwmci_setup_bus(struct dwmci_host *host, u32 freq)
372{
373 u32 div, status;
374 int timeout = 10000;
375 unsigned long sclk;
376
Amar902664c2013-04-27 11:42:54 +0530377 if ((freq == host->clock) || (freq == 0))
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000378 return 0;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000379 /*
Pavel Macheka425f5d2014-09-05 12:49:48 +0200380 * If host->get_mmc_clk isn't defined,
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000381 * then assume that host->bus_hz is source clock value.
Pavel Macheka425f5d2014-09-05 12:49:48 +0200382 * host->bus_hz should be set by user.
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000383 */
Jaehoon Chungd94735b2013-10-06 18:59:31 +0900384 if (host->get_mmc_clk)
Simon Glasseff76682015-08-30 16:55:15 -0600385 sclk = host->get_mmc_clk(host, freq);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000386 else if (host->bus_hz)
387 sclk = host->bus_hz;
388 else {
Simon Glass4c9b9482015-08-06 20:16:27 -0600389 debug("%s: Didn't get source clock value.\n", __func__);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000390 return -EINVAL;
391 }
392
Chin Liang See4cfff952014-06-10 01:26:52 -0500393 if (sclk == freq)
394 div = 0; /* bypass mode */
395 else
396 div = DIV_ROUND_UP(sclk, 2 * freq);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000397
398 dwmci_writel(host, DWMCI_CLKENA, 0);
399 dwmci_writel(host, DWMCI_CLKSRC, 0);
400
401 dwmci_writel(host, DWMCI_CLKDIV, div);
402 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
403 DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
404
405 do {
406 status = dwmci_readl(host, DWMCI_CMD);
407 if (timeout-- < 0) {
Simon Glass4c9b9482015-08-06 20:16:27 -0600408 debug("%s: Timeout!\n", __func__);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000409 return -ETIMEDOUT;
410 }
411 } while (status & DWMCI_CMD_START);
412
413 dwmci_writel(host, DWMCI_CLKENA, DWMCI_CLKEN_ENABLE |
414 DWMCI_CLKEN_LOW_PWR);
415
416 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
417 DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
418
419 timeout = 10000;
420 do {
421 status = dwmci_readl(host, DWMCI_CMD);
422 if (timeout-- < 0) {
Simon Glass4c9b9482015-08-06 20:16:27 -0600423 debug("%s: Timeout!\n", __func__);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000424 return -ETIMEDOUT;
425 }
426 } while (status & DWMCI_CMD_START);
427
428 host->clock = freq;
429
430 return 0;
431}
432
Simon Glasseba48f92017-07-29 11:35:31 -0600433#ifdef CONFIG_DM_MMC
Jaehoon Chungad220ac2016-06-28 15:52:21 +0900434static int dwmci_set_ios(struct udevice *dev)
Simon Glassff5c1b72016-06-12 23:30:23 -0600435{
436 struct mmc *mmc = mmc_get_mmc_dev(dev);
437#else
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900438static int dwmci_set_ios(struct mmc *mmc)
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000439{
Simon Glassff5c1b72016-06-12 23:30:23 -0600440#endif
Jaehoon Chunge8672942014-05-16 13:59:55 +0900441 struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
442 u32 ctype, regs;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000443
Pavel Macheka425f5d2014-09-05 12:49:48 +0200444 debug("Buswidth = %d, clock: %d\n", mmc->bus_width, mmc->clock);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000445
446 dwmci_setup_bus(host, mmc->clock);
447 switch (mmc->bus_width) {
448 case 8:
449 ctype = DWMCI_CTYPE_8BIT;
450 break;
451 case 4:
452 ctype = DWMCI_CTYPE_4BIT;
453 break;
454 default:
455 ctype = DWMCI_CTYPE_1BIT;
456 break;
457 }
458
459 dwmci_writel(host, DWMCI_CTYPE, ctype);
460
Jaehoon Chunge8672942014-05-16 13:59:55 +0900461 regs = dwmci_readl(host, DWMCI_UHS_REG);
Andrew Gabbasov54c0e222014-12-01 06:59:12 -0600462 if (mmc->ddr_mode)
Jaehoon Chunge8672942014-05-16 13:59:55 +0900463 regs |= DWMCI_DDR_MODE;
464 else
Jaehoon Chung401fc502015-01-14 17:37:53 +0900465 regs &= ~DWMCI_DDR_MODE;
Jaehoon Chunge8672942014-05-16 13:59:55 +0900466
467 dwmci_writel(host, DWMCI_UHS_REG, regs);
468
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000469 if (host->clksel)
470 host->clksel(host);
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900471
Simon Glassff5c1b72016-06-12 23:30:23 -0600472 return 0;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000473}
474
475static int dwmci_init(struct mmc *mmc)
476{
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200477 struct dwmci_host *host = mmc->priv;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000478
Jaehoon Chung42f81a82013-11-29 20:08:57 +0900479 if (host->board_init)
480 host->board_init(host);
Rajeshwari Shinde70163092013-10-29 12:53:13 +0530481
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000482 dwmci_writel(host, DWMCI_PWREN, 1);
483
484 if (!dwmci_wait_reset(host, DWMCI_RESET_ALL)) {
Simon Glass4c9b9482015-08-06 20:16:27 -0600485 debug("%s[%d] Fail-reset!!\n", __func__, __LINE__);
486 return -EIO;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000487 }
488
Amar902664c2013-04-27 11:42:54 +0530489 /* Enumerate at 400KHz */
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200490 dwmci_setup_bus(host, mmc->cfg->f_min);
Amar902664c2013-04-27 11:42:54 +0530491
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000492 dwmci_writel(host, DWMCI_RINTSTS, 0xFFFFFFFF);
493 dwmci_writel(host, DWMCI_INTMASK, 0);
494
495 dwmci_writel(host, DWMCI_TMOUT, 0xFFFFFFFF);
496
497 dwmci_writel(host, DWMCI_IDINTEN, 0);
498 dwmci_writel(host, DWMCI_BMOD, 1);
499
Simon Glass6133efa2015-08-06 20:16:29 -0600500 if (!host->fifoth_val) {
501 uint32_t fifo_size;
502
503 fifo_size = dwmci_readl(host, DWMCI_FIFOTH);
504 fifo_size = ((fifo_size & RX_WMARK_MASK) >> RX_WMARK_SHIFT) + 1;
505 host->fifoth_val = MSIZE(0x2) | RX_WMARK(fifo_size / 2 - 1) |
506 TX_WMARK(fifo_size / 2);
Amar902664c2013-04-27 11:42:54 +0530507 }
Simon Glass6133efa2015-08-06 20:16:29 -0600508 dwmci_writel(host, DWMCI_FIFOTH, host->fifoth_val);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000509
510 dwmci_writel(host, DWMCI_CLKENA, 0);
511 dwmci_writel(host, DWMCI_CLKSRC, 0);
512
Ley Foon Tanb98e8922018-12-20 17:55:41 +0800513 if (!host->fifo_mode)
514 dwmci_writel(host, DWMCI_IDINTEN, DWMCI_IDINTEN_MASK);
515
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000516 return 0;
517}
518
Simon Glasseba48f92017-07-29 11:35:31 -0600519#ifdef CONFIG_DM_MMC
Simon Glassff5c1b72016-06-12 23:30:23 -0600520int dwmci_probe(struct udevice *dev)
521{
522 struct mmc *mmc = mmc_get_mmc_dev(dev);
523
524 return dwmci_init(mmc);
525}
526
527const struct dm_mmc_ops dm_dwmci_ops = {
528 .send_cmd = dwmci_send_cmd,
529 .set_ios = dwmci_set_ios,
530};
531
532#else
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200533static const struct mmc_ops dwmci_ops = {
534 .send_cmd = dwmci_send_cmd,
535 .set_ios = dwmci_set_ios,
536 .init = dwmci_init,
537};
Simon Glassff5c1b72016-06-12 23:30:23 -0600538#endif
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200539
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900540void dwmci_setup_cfg(struct mmc_config *cfg, struct dwmci_host *host,
541 u32 max_clk, u32 min_clk)
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000542{
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900543 cfg->name = host->name;
Simon Glasseba48f92017-07-29 11:35:31 -0600544#ifndef CONFIG_DM_MMC
Simon Glass82682542016-05-14 14:03:07 -0600545 cfg->ops = &dwmci_ops;
Simon Glassff5c1b72016-06-12 23:30:23 -0600546#endif
Simon Glass82682542016-05-14 14:03:07 -0600547 cfg->f_min = min_clk;
548 cfg->f_max = max_clk;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000549
Simon Glass82682542016-05-14 14:03:07 -0600550 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000551
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900552 cfg->host_caps = host->caps;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000553
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900554 if (host->buswidth == 8) {
Simon Glass82682542016-05-14 14:03:07 -0600555 cfg->host_caps |= MMC_MODE_8BIT;
556 cfg->host_caps &= ~MMC_MODE_4BIT;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000557 } else {
Simon Glass82682542016-05-14 14:03:07 -0600558 cfg->host_caps |= MMC_MODE_4BIT;
559 cfg->host_caps &= ~MMC_MODE_8BIT;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000560 }
Simon Glass82682542016-05-14 14:03:07 -0600561 cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz;
562
563 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
564}
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200565
Simon Glass82682542016-05-14 14:03:07 -0600566#ifdef CONFIG_BLK
567int dwmci_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg)
568{
569 return mmc_bind(dev, mmc, cfg);
570}
571#else
572int add_dwmci(struct dwmci_host *host, u32 max_clk, u32 min_clk)
573{
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900574 dwmci_setup_cfg(&host->cfg, host, max_clk, min_clk);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000575
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200576 host->mmc = mmc_create(&host->cfg, host);
577 if (host->mmc == NULL)
578 return -1;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000579
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200580 return 0;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000581}
Simon Glass82682542016-05-14 14:03:07 -0600582#endif