xref: /rk3399_rockchip-uboot/drivers/mmc/dw_mmc.c (revision c5ff44bc8ebec81979fbcd4aacb56e0ca3b2fb53)
1 /*
2  * (C) Copyright 2012 SAMSUNG Electronics
3  * Jaehoon Chung <jh80.chung@samsung.com>
4  * Rajeshawari Shinde <rajeshwari.s@samsung.com>
5  *
6  * SPDX-License-Identifier:	GPL-2.0+
7  */
8 
9 #include <common.h>
10 #include <bouncebuf.h>
11 #include <div64.h>
12 #include <errno.h>
13 #include <malloc.h>
14 #include <memalign.h>
15 #include <mmc.h>
16 #include <dwmmc.h>
17 #include <dm/pinctrl.h>
18 #include <dm.h>
19 #ifdef CONFIG_DM_GPIO
20 #include <asm/gpio.h>
21 #include <asm-generic/gpio.h>
22 #endif
23 
24 #define PAGE_SIZE 4096
25 #define MSEC_PER_SEC	1000ULL
26 
27 /*
28  * Currently it supports read/write up to 8*8*4 Bytes per
29  * stride as a burst mode. Please note that if you change
30  * MAX_STRIDE, you should also update dwmci_memcpy_fromio
31  * to augment the groups of {ldm, stm}.
32  */
33 #define MAX_STRIDE 64
34 #if (CONFIG_ARM && CONFIG_CPU_V7 && !defined(CONFIG_MMC_SIMPLE))
35 void noinline dwmci_memcpy_fromio(void *buffer, void *fifo_addr)
36 {
37 	__asm__ __volatile__ (
38 		"push {r2, r3, r4, r5, r6, r7, r8, r9}\n"
39 		"ldm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
40 		"stm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
41 		"ldm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
42 		"stm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
43 		"ldm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
44 		"stm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
45 		"ldm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
46 		"stm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
47 		"ldm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
48 		"stm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
49 		"ldm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
50 		"stm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
51 		"ldm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
52 		"stm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
53 		"ldm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
54 		"stm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
55 		"pop {r2, r3, r4, r5, r6,r7,r8,r9}\n"
56 		:::"memory"
57 	);
58 }
59 
60 void noinline dwmci_memcpy_toio(void *buffer, void *fifo_addr)
61 {
62 	__asm__ __volatile__ (
63 		"push {r2, r3, r4, r5, r6, r7, r8, r9}\n"
64 		"ldm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
65 		"stm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
66 		"ldm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
67 		"stm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
68 		"ldm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
69 		"stm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
70 		"ldm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
71 		"stm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
72 		"ldm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
73 		"stm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
74 		"ldm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
75 		"stm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
76 		"ldm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
77 		"stm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
78 		"ldm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
79 		"stm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n"
80 		"pop {r2, r3, r4, r5, r6,r7,r8,r9}\n"
81 		:::"memory"
82 	);
83 }
84 #else
85 void dwmci_memcpy_fromio(void *buffer, void *fifo_addr) {};
86 void dwmci_memcpy_toio(void *buffer, void *fifo_addr) {};
87 #endif
88 
89 static int dwmci_wait_reset(struct dwmci_host *host, u32 value)
90 {
91 	unsigned long timeout = 1000;
92 	u32 ctrl;
93 
94 	dwmci_writel(host, DWMCI_CTRL, value);
95 
96 	while (timeout--) {
97 		ctrl = dwmci_readl(host, DWMCI_CTRL);
98 		if (!(ctrl & DWMCI_RESET_ALL))
99 			return 1;
100 	}
101 	return 0;
102 }
103 
104 static void dwmci_set_idma_desc(struct dwmci_idmac *idmac,
105 		u32 desc0, u32 desc1, u32 desc2)
106 {
107 	struct dwmci_idmac *desc = idmac;
108 
109 	desc->flags = desc0;
110 	desc->cnt = desc1;
111 	desc->addr = desc2;
112 	desc->next_addr = (ulong)desc + sizeof(struct dwmci_idmac);
113 }
114 
115 static void dwmci_prepare_data(struct dwmci_host *host,
116 			       struct mmc_data *data,
117 			       struct dwmci_idmac *cur_idmac,
118 			       void *bounce_buffer)
119 {
120 	unsigned long ctrl;
121 	unsigned int i = 0, flags, cnt, blk_cnt;
122 	ulong data_start, data_end;
123 
124 
125 	blk_cnt = data->blocks;
126 
127 	dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
128 
129 	data_start = (ulong)cur_idmac;
130 	dwmci_writel(host, DWMCI_DBADDR, (ulong)cur_idmac);
131 
132 	do {
133 		flags = DWMCI_IDMAC_OWN | DWMCI_IDMAC_CH ;
134 		flags |= (i == 0) ? DWMCI_IDMAC_FS : 0;
135 		if (blk_cnt <= 8) {
136 			flags |= DWMCI_IDMAC_LD;
137 			cnt = data->blocksize * blk_cnt;
138 		} else
139 			cnt = data->blocksize * 8;
140 
141 		dwmci_set_idma_desc(cur_idmac, flags, cnt,
142 				    (ulong)bounce_buffer + (i * PAGE_SIZE));
143 
144 		if (blk_cnt <= 8)
145 			break;
146 		blk_cnt -= 8;
147 		cur_idmac++;
148 		i++;
149 	} while(1);
150 
151 	data_end = (ulong)cur_idmac;
152 	flush_dcache_range(data_start, data_end + ARCH_DMA_MINALIGN);
153 
154 	ctrl = dwmci_readl(host, DWMCI_CTRL);
155 	ctrl |= DWMCI_IDMAC_EN | DWMCI_DMA_EN;
156 	dwmci_writel(host, DWMCI_CTRL, ctrl);
157 
158 	ctrl = dwmci_readl(host, DWMCI_BMOD);
159 	ctrl |= DWMCI_BMOD_IDMAC_FB | DWMCI_BMOD_IDMAC_EN;
160 	dwmci_writel(host, DWMCI_BMOD, ctrl);
161 
162 	dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
163 	dwmci_writel(host, DWMCI_BYTCNT, data->blocksize * data->blocks);
164 }
165 
166 #ifdef CONFIG_SPL_BUILD
167 static unsigned int dwmci_get_drto(struct dwmci_host *host,
168 				   const unsigned int size)
169 {
170 	unsigned int drto_clks;
171 	unsigned int drto_div;
172 	unsigned int drto_ms;
173 
174 	drto_clks = dwmci_readl(host, DWMCI_TMOUT) >> 8;
175 	drto_div = (dwmci_readl(host, DWMCI_CLKDIV) & 0xff) * 2;
176 	if (drto_div == 0)
177 		drto_div = 1;
178 
179 	drto_ms = DIV_ROUND_UP_ULL((u64)MSEC_PER_SEC * drto_clks * drto_div,
180 				   host->mmc->clock);
181 
182 	/* add a bit spare time */
183 	drto_ms += 50;
184 
185 	return drto_ms;
186 }
187 #else
188 static unsigned int dwmci_get_drto(struct dwmci_host *host,
189 				   const unsigned int size)
190 {
191 	unsigned int timeout;
192 
193 	timeout = size * 8;	/* counting in bits */
194 	timeout *= 10;		/* wait 10 times as long */
195 	timeout /= host->mmc->clock;
196 	timeout /= host->mmc->bus_width;
197 	timeout *= 1000;	/* counting in msec */
198 	timeout = (timeout < 10000) ? 10000 : timeout;
199 
200 	return timeout;
201 }
202 #endif
203 
204 static unsigned int dwmci_get_cto(struct dwmci_host *host)
205 {
206 	unsigned int cto_clks;
207 	unsigned int cto_div;
208 	unsigned int cto_ms;
209 
210 	cto_clks = dwmci_readl(host, DWMCI_TMOUT) & 0xff;
211 	cto_div = (dwmci_readl(host, DWMCI_CLKDIV) & 0xff) * 2;
212 	if (cto_div == 0)
213 		cto_div = 1;
214 
215 	cto_ms = DIV_ROUND_UP_ULL((u64)MSEC_PER_SEC * cto_clks * cto_div,
216 				  host->mmc->clock);
217 
218 	/* add a bit spare time */
219 	cto_ms += 10;
220 
221 	return cto_ms;
222 }
223 
224 static int dwmci_data_transfer(struct dwmci_host *host, struct mmc_data *data)
225 {
226 	int ret = 0;
227 	int reset_timeout = 100;
228 	u32 timeout, status, ctrl, mask, size, i, len = 0;
229 	u32 *buf = NULL;
230 	ulong start = get_timer(0);
231 	u32 fifo_depth = (((host->fifoth_val & RX_WMARK_MASK) >>
232 			    RX_WMARK_SHIFT) + 1) * 2;
233 	bool stride;
234 
235 	size = data->blocksize * data->blocks;
236 	/* Still use legacy PIO mode if size < 512(128 * 4) Bytes */
237 	stride = host->stride_pio && size > 128;
238 	if (data->flags == MMC_DATA_READ)
239 		buf = (unsigned int *)data->dest;
240 	else
241 		buf = (unsigned int *)data->src;
242 
243 	timeout = dwmci_get_drto(host, size);
244 	/* The tuning data is 128bytes, a timeout of 1ms is sufficient.*/
245 	if ((dwmci_readl(host, DWMCI_CMD) & 0x1F) == MMC_SEND_TUNING_BLOCK_HS200)
246 		timeout = 1;
247 
248 	size /= 4;
249 
250 	for (;;) {
251 		mask = dwmci_readl(host, DWMCI_RINTSTS);
252 		/* Error during data transfer. */
253 		if (mask & (DWMCI_DATA_ERR | DWMCI_DATA_TOUT)) {
254 			debug("%s: DATA ERROR!\n", __func__);
255 			/*
256 			 * It is necessary to wait for several cycles before
257 			 * resetting the controller while data timeout or error.
258 			 */
259 			udelay(1);
260 			dwmci_wait_reset(host, DWMCI_RESET_ALL);
261 			dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
262 				     DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
263 
264 			do {
265 				status = dwmci_readl(host, DWMCI_CMD);
266 				if (reset_timeout-- < 0)
267 					break;
268 				udelay(100);
269 			} while (status & DWMCI_CMD_START);
270 
271 			if (!host->fifo_mode) {
272 				ctrl = dwmci_readl(host, DWMCI_BMOD);
273 				ctrl |= DWMCI_BMOD_IDMAC_RESET;
274 				dwmci_writel(host, DWMCI_BMOD, ctrl);
275 			}
276 
277 			ret = -EINVAL;
278 			break;
279 		}
280 
281 		if (host->fifo_mode && size) {
282 			len = 0;
283 			if (data->flags == MMC_DATA_READ &&
284 			    (mask & DWMCI_INTMSK_RXDR)) {
285 				while (size) {
286 					len = dwmci_readl(host, DWMCI_STATUS);
287 					len = (len >> DWMCI_FIFO_SHIFT) &
288 						    DWMCI_FIFO_MASK;
289 					len = min(size, len);
290 					if (!stride) {
291 						/* Legacy pio mode */
292 						for (i = 0; i < len; i++)
293 							*buf++ = dwmci_readl(host, DWMCI_DATA);
294 						goto read_again;
295 					}
296 
297 					/* dwmci_memcpy_fromio now bursts 256 Bytes once */
298 					if (len < MAX_STRIDE)
299 						continue;
300 
301 					for (i = 0; i < len / MAX_STRIDE; i++) {
302 						dwmci_memcpy_fromio(buf, host->ioaddr + DWMCI_DATA);
303 						buf += MAX_STRIDE;
304 					}
305 
306 					len = i * MAX_STRIDE;
307 read_again:
308 					size = size > len ? (size - len) : 0;
309 				}
310 				dwmci_writel(host, DWMCI_RINTSTS,
311 					     DWMCI_INTMSK_RXDR);
312 				start = get_timer(0);
313 			} else if (data->flags == MMC_DATA_WRITE &&
314 				   (mask & DWMCI_INTMSK_TXDR)) {
315 				while (size) {
316 					len = dwmci_readl(host, DWMCI_STATUS);
317 					len = fifo_depth - ((len >>
318 						   DWMCI_FIFO_SHIFT) &
319 						   DWMCI_FIFO_MASK);
320 					len = min(size, len);
321 					if (!stride) {
322 						for (i = 0; i < len; i++)
323 							dwmci_writel(host, DWMCI_DATA,
324 								     *buf++);
325 						goto write_again;
326 					}
327 					/* dwmci_memcpy_toio now bursts 256 Bytes once */
328 					if (len < MAX_STRIDE)
329 						continue;
330 
331 					for (i = 0; i < len / MAX_STRIDE; i++) {
332 						dwmci_memcpy_toio(buf, host->ioaddr + DWMCI_DATA);
333 						buf += MAX_STRIDE;
334 					}
335 
336 					len = i * MAX_STRIDE;
337 write_again:
338 					size = size > len ? (size - len) : 0;
339 				}
340 				dwmci_writel(host, DWMCI_RINTSTS,
341 					     DWMCI_INTMSK_TXDR);
342 				start = get_timer(0);
343 			}
344 		}
345 
346 		/* Data arrived correctly. */
347 		if (mask & DWMCI_INTMSK_DTO) {
348 			ret = 0;
349 			break;
350 		}
351 
352 		/* Check for timeout. */
353 		if (get_timer(start) > timeout) {
354 			debug("%s: Timeout waiting for data!\n",
355 			      __func__);
356 			ret = -ETIMEDOUT;
357 			break;
358 		}
359 	}
360 
361 	dwmci_writel(host, DWMCI_RINTSTS, mask);
362 
363 	return ret;
364 }
365 
366 static int dwmci_set_transfer_mode(struct dwmci_host *host,
367 		struct mmc_data *data)
368 {
369 	unsigned long mode;
370 
371 	mode = DWMCI_CMD_DATA_EXP;
372 	if (data->flags & MMC_DATA_WRITE)
373 		mode |= DWMCI_CMD_RW;
374 
375 	return mode;
376 }
377 
378 #ifdef CONFIG_DM_MMC
379 static int dwmci_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
380 		   struct mmc_data *data)
381 {
382 	struct mmc *mmc = mmc_get_mmc_dev(dev);
383 #else
384 static int dwmci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
385 		struct mmc_data *data)
386 {
387 #endif
388 	struct dwmci_host *host = mmc->priv;
389 	ALLOC_CACHE_ALIGN_BUFFER(struct dwmci_idmac, cur_idmac,
390 				 data ? DIV_ROUND_UP(data->blocks, 8) : 0);
391 	int ret = 0, flags = 0;
392 	unsigned int timeout = 500;
393 	u32 mask, ctrl;
394 	ulong start = get_timer(0);
395 	struct bounce_buffer bbstate;
396 
397 	while (dwmci_readl(host, DWMCI_STATUS) & DWMCI_BUSY) {
398 		if (get_timer(start) > timeout) {
399 			debug("%s: Timeout on data busy\n", __func__);
400 			return -ETIMEDOUT;
401 		}
402 	}
403 
404 	dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_ALL);
405 
406 	if (data) {
407 		if (host->fifo_mode) {
408 			dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
409 			dwmci_writel(host, DWMCI_BYTCNT,
410 				     data->blocksize * data->blocks);
411 			dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
412 		} else {
413 			if (data->flags == MMC_DATA_READ) {
414 				ret = bounce_buffer_start(&bbstate,
415 						(void*)data->dest,
416 						data->blocksize *
417 						data->blocks, GEN_BB_WRITE);
418 			} else {
419 				ret = bounce_buffer_start(&bbstate,
420 						(void*)data->src,
421 						data->blocksize *
422 						data->blocks, GEN_BB_READ);
423 			}
424 
425 			if (ret)
426 				return ret;
427 
428 			dwmci_prepare_data(host, data, cur_idmac,
429 					   bbstate.bounce_buffer);
430 		}
431 	}
432 
433 	dwmci_writel(host, DWMCI_CMDARG, cmd->cmdarg);
434 
435 	if (data)
436 		flags = dwmci_set_transfer_mode(host, data);
437 
438 	if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
439 		return -1;
440 
441 	if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
442 		flags |= DWMCI_CMD_ABORT_STOP;
443 	else if (cmd->cmdidx == MMC_CMD_GO_IDLE_STATE)
444 		flags |= SDMMC_CMD_INIT | DWMCI_CMD_ABORT_STOP;
445 	else
446 		flags |= DWMCI_CMD_PRV_DAT_WAIT;
447 
448 	if (cmd->resp_type & MMC_RSP_PRESENT) {
449 		flags |= DWMCI_CMD_RESP_EXP;
450 		if (cmd->resp_type & MMC_RSP_136)
451 			flags |= DWMCI_CMD_RESP_LENGTH;
452 	}
453 
454 	if (cmd->resp_type & MMC_RSP_CRC)
455 		flags |= DWMCI_CMD_CHECK_CRC;
456 
457 	flags |= (cmd->cmdidx | DWMCI_CMD_START | DWMCI_CMD_USE_HOLD_REG);
458 
459 	debug("Sending CMD%d\n",cmd->cmdidx);
460 
461 	dwmci_writel(host, DWMCI_CMD, flags);
462 
463 	timeout = dwmci_get_cto(host);
464 	start = get_timer(0);
465 	do {
466 		mask = dwmci_readl(host, DWMCI_RINTSTS);
467 		if (mask & DWMCI_INTMSK_CDONE) {
468 			if (!data)
469 				dwmci_writel(host, DWMCI_RINTSTS, mask);
470 			break;
471 		}
472 	} while (!(get_timer(start) > timeout));
473 
474 	if (get_timer(start) > timeout) {
475 		debug("%s: Timeout.\n", __func__);
476 		return -ETIMEDOUT;
477 	}
478 
479 	if (mask & DWMCI_INTMSK_RTO) {
480 		/*
481 		 * Timeout here is not necessarily fatal. (e)MMC cards
482 		 * will splat here when they receive CMD55 as they do
483 		 * not support this command and that is exactly the way
484 		 * to tell them apart from SD cards. Thus, this output
485 		 * below shall be debug(). eMMC cards also do not favor
486 		 * CMD8, please keep that in mind.
487 		 */
488 		debug("%s: Response Timeout.\n", __func__);
489 		return -ETIMEDOUT;
490 	} else if (mask & DWMCI_INTMSK_RE) {
491 		debug("%s: Response Error.\n", __func__);
492 		return -EIO;
493 	}
494 
495 
496 	if (cmd->resp_type & MMC_RSP_PRESENT) {
497 		if (cmd->resp_type & MMC_RSP_136) {
498 			cmd->response[0] = dwmci_readl(host, DWMCI_RESP3);
499 			cmd->response[1] = dwmci_readl(host, DWMCI_RESP2);
500 			cmd->response[2] = dwmci_readl(host, DWMCI_RESP1);
501 			cmd->response[3] = dwmci_readl(host, DWMCI_RESP0);
502 		} else {
503 			cmd->response[0] = dwmci_readl(host, DWMCI_RESP0);
504 		}
505 	}
506 
507 	if (data) {
508 		ret = dwmci_data_transfer(host, data);
509 
510 		/* only dma mode need it */
511 		if (!host->fifo_mode) {
512 			ctrl = dwmci_readl(host, DWMCI_CTRL);
513 			ctrl &= ~(DWMCI_DMA_EN);
514 			dwmci_writel(host, DWMCI_CTRL, ctrl);
515 			bounce_buffer_stop(&bbstate);
516 		}
517 	}
518 
519 	return ret;
520 }
521 
522 #ifdef CONFIG_SPL_BLK_READ_PREPARE
523 #ifdef CONFIG_DM_MMC
524 static int dwmci_send_cmd_prepare(struct udevice *dev, struct mmc_cmd *cmd,
525 				  struct mmc_data *data)
526 {
527 	struct mmc *mmc = mmc_get_mmc_dev(dev);
528 #else
529 static int dwmci_send_cmd_prepare(struct mmc *mmc, struct mmc_cmd *cmd,
530 				  struct mmc_data *data)
531 {
532 #endif
533 	struct dwmci_host *host = mmc->priv;
534 	struct dwmci_idmac *cur_idmac;
535 	int ret = 0, flags = 0;
536 	unsigned int timeout = 500;
537 	u32 mask;
538 	ulong start = get_timer(0);
539 	struct bounce_buffer bbstate;
540 
541 	cur_idmac = malloc(ROUND(DIV_ROUND_UP(data->blocks, 8) *
542 			   sizeof(struct dwmci_idmac),
543 			   ARCH_DMA_MINALIGN) + ARCH_DMA_MINALIGN - 1);
544 	if (!cur_idmac)
545 		return -ENODATA;
546 
547 	while (dwmci_readl(host, DWMCI_STATUS) & DWMCI_BUSY) {
548 		if (get_timer(start) > timeout) {
549 			debug("%s: Timeout on data busy\n", __func__);
550 			return -ETIMEDOUT;
551 		}
552 	}
553 
554 	dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_ALL);
555 
556 	if (data) {
557 		if (host->fifo_mode) {
558 			dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
559 			dwmci_writel(host, DWMCI_BYTCNT,
560 				     data->blocksize * data->blocks);
561 			dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
562 		} else {
563 			if (data->flags == MMC_DATA_READ) {
564 				bounce_buffer_start(&bbstate, (void *)data->dest,
565 						    data->blocksize *
566 						    data->blocks, GEN_BB_WRITE);
567 			} else {
568 				bounce_buffer_start(&bbstate, (void *)data->src,
569 						    data->blocksize *
570 						    data->blocks, GEN_BB_READ);
571 			}
572 			dwmci_prepare_data(host, data, cur_idmac,
573 					   bbstate.bounce_buffer);
574 		}
575 	}
576 
577 	dwmci_writel(host, DWMCI_CMDARG, cmd->cmdarg);
578 
579 	if (data)
580 		flags = dwmci_set_transfer_mode(host, data);
581 
582 	if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
583 		return -1;
584 
585 	if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
586 		flags |= DWMCI_CMD_ABORT_STOP;
587 	else
588 		flags |= DWMCI_CMD_PRV_DAT_WAIT;
589 
590 	if (cmd->resp_type & MMC_RSP_PRESENT) {
591 		flags |= DWMCI_CMD_RESP_EXP;
592 		if (cmd->resp_type & MMC_RSP_136)
593 			flags |= DWMCI_CMD_RESP_LENGTH;
594 	}
595 
596 	if (cmd->resp_type & MMC_RSP_CRC)
597 		flags |= DWMCI_CMD_CHECK_CRC;
598 
599 	flags |= (cmd->cmdidx | DWMCI_CMD_START | DWMCI_CMD_USE_HOLD_REG);
600 
601 	debug("Sending CMD%d\n", cmd->cmdidx);
602 
603 	dwmci_writel(host, DWMCI_CMD, flags);
604 
605 	timeout = dwmci_get_cto(host);
606 	start = get_timer(0);
607 	do {
608 		mask = dwmci_readl(host, DWMCI_RINTSTS);
609 		if (mask & DWMCI_INTMSK_CDONE) {
610 			if (!data)
611 				dwmci_writel(host, DWMCI_RINTSTS, mask);
612 			break;
613 		}
614 	} while (!(get_timer(start) > timeout));
615 
616 	if (get_timer(start) > timeout) {
617 		debug("%s: Timeout.\n", __func__);
618 		return -ETIMEDOUT;
619 	}
620 
621 	if (mask & DWMCI_INTMSK_RTO) {
622 		/*
623 		 * Timeout here is not necessarily fatal. (e)MMC cards
624 		 * will splat here when they receive CMD55 as they do
625 		 * not support this command and that is exactly the way
626 		 * to tell them apart from SD cards. Thus, this output
627 		 * below shall be debug(). eMMC cards also do not favor
628 		 * CMD8, please keep that in mind.
629 		 */
630 		debug("%s: Response Timeout.\n", __func__);
631 		return -ETIMEDOUT;
632 	} else if (mask & DWMCI_INTMSK_RE) {
633 		debug("%s: Response Error.\n", __func__);
634 		return -EIO;
635 	}
636 
637 	if (cmd->resp_type & MMC_RSP_PRESENT) {
638 		if (cmd->resp_type & MMC_RSP_136) {
639 			cmd->response[0] = dwmci_readl(host, DWMCI_RESP3);
640 			cmd->response[1] = dwmci_readl(host, DWMCI_RESP2);
641 			cmd->response[2] = dwmci_readl(host, DWMCI_RESP1);
642 			cmd->response[3] = dwmci_readl(host, DWMCI_RESP0);
643 		} else {
644 			cmd->response[0] = dwmci_readl(host, DWMCI_RESP0);
645 		}
646 	}
647 
648 	return ret;
649 }
650 #endif
651 
652 static int dwmci_setup_bus(struct dwmci_host *host, u32 freq)
653 {
654 	u32 div, status;
655 	int timeout = 10000;
656 	unsigned long sclk;
657 
658 	if (freq == 0)
659 		return 0;
660 	/*
661 	 * If host->get_mmc_clk isn't defined,
662 	 * then assume that host->bus_hz is source clock value.
663 	 * host->bus_hz should be set by user.
664 	 */
665 	if (host->get_mmc_clk)
666 		sclk = host->get_mmc_clk(host, freq);
667 	else if (host->bus_hz)
668 		sclk = host->bus_hz;
669 	else {
670 		debug("%s: Didn't get source clock value.\n", __func__);
671 		return -EINVAL;
672 	}
673 
674 	if (sclk == 0)
675 		return -EINVAL;
676 
677 	if (sclk == freq)
678 		div = 0;	/* bypass mode */
679 	else
680 		div = DIV_ROUND_UP(sclk, 2 * freq);
681 
682 	dwmci_writel(host, DWMCI_CLKENA, 0);
683 	dwmci_writel(host, DWMCI_CLKSRC, 0);
684 
685 	dwmci_writel(host, DWMCI_CLKDIV, div);
686 	dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
687 			DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
688 
689 	do {
690 		status = dwmci_readl(host, DWMCI_CMD);
691 		if (timeout-- < 0) {
692 			debug("%s: Timeout!\n", __func__);
693 			return -ETIMEDOUT;
694 		}
695 	} while (status & DWMCI_CMD_START);
696 
697 	dwmci_writel(host, DWMCI_CLKENA, DWMCI_CLKEN_ENABLE |
698 			DWMCI_CLKEN_LOW_PWR);
699 
700 	dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
701 			DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
702 
703 	timeout = 10000;
704 	do {
705 		status = dwmci_readl(host, DWMCI_CMD);
706 		if (timeout-- < 0) {
707 			debug("%s: Timeout!\n", __func__);
708 			return -ETIMEDOUT;
709 		}
710 	} while (status & DWMCI_CMD_START);
711 
712 	host->clock = freq;
713 
714 	return 0;
715 }
716 
717 #ifdef CONFIG_DM_MMC
718 static bool dwmci_card_busy(struct udevice *dev)
719 {
720 	struct mmc *mmc = mmc_get_mmc_dev(dev);
721 #else
722 static bool dwmci_card_busy(struct mmc *mmc)
723 {
724 #endif
725 	u32 status;
726 	struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
727 
728 	/*
729 	 * Check the busy bit which is low when DAT[3:0]
730 	 * (the data lines) are 0000
731 	 */
732 	status = dwmci_readl(host, DWMCI_STATUS);
733 
734 	return !!(status & DWMCI_BUSY);
735 }
736 
737 #ifdef CONFIG_DM_MMC
738 static int dwmci_execute_tuning(struct udevice *dev, u32 opcode)
739 {
740 	struct mmc *mmc = mmc_get_mmc_dev(dev);
741 #else
742 static int dwmci_execute_tuning(struct mmc *mmc, u32 opcode)
743 {
744 #endif
745 	struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
746 
747 	if (!host->execute_tuning)
748 		return -EIO;
749 
750 	return host->execute_tuning(host, opcode);
751 }
752 
753 #ifdef CONFIG_DM_MMC
754 static int dwmci_set_ios(struct udevice *dev)
755 {
756 	struct mmc *mmc = mmc_get_mmc_dev(dev);
757 #else
758 static int dwmci_set_ios(struct mmc *mmc)
759 {
760 #endif
761 	struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
762 	u32 ctype, regs;
763 
764 	debug("Buswidth = %d, clock: %d\n", mmc->bus_width, mmc->clock);
765 
766 	dwmci_setup_bus(host, mmc->clock);
767 	switch (mmc->bus_width) {
768 	case 8:
769 		ctype = DWMCI_CTYPE_8BIT;
770 		break;
771 	case 4:
772 		ctype = DWMCI_CTYPE_4BIT;
773 		break;
774 	default:
775 		ctype = DWMCI_CTYPE_1BIT;
776 		break;
777 	}
778 
779 	dwmci_writel(host, DWMCI_CTYPE, ctype);
780 
781 	regs = dwmci_readl(host, DWMCI_UHS_REG);
782 	if (mmc_card_ddr(mmc))
783 		regs |= DWMCI_DDR_MODE;
784 	else
785 		regs &= ~DWMCI_DDR_MODE;
786 
787 	dwmci_writel(host, DWMCI_UHS_REG, regs);
788 
789 	if (host->clksel)
790 		host->clksel(host);
791 
792 	return 0;
793 }
794 
795 static int dwmci_init(struct mmc *mmc)
796 {
797 	struct dwmci_host *host = mmc->priv;
798 	uint32_t use_dma;
799 	uint32_t verid;
800 
801 #if defined(CONFIG_DM_GPIO) && (defined(CONFIG_SPL_GPIO_SUPPORT) || !defined(CONFIG_SPL_BUILD))
802 	struct gpio_desc pwr_en_gpio;
803 	u32 delay_ms;
804 
805 	if (mmc_getcd(mmc) == 1 &&
806 	    !gpio_request_by_name(mmc->dev, "pwr-en-gpios", 0, &pwr_en_gpio, GPIOD_IS_OUT)) {
807 		dm_gpio_set_value(&pwr_en_gpio, 0);
808 		pinctrl_select_state(mmc->dev, "idle");
809 		delay_ms = dev_read_u32_default(mmc->dev, "power-off-delay-ms", 200);
810 		mdelay(delay_ms);
811 		dm_gpio_set_value(&pwr_en_gpio, 1);
812 		pinctrl_select_state(mmc->dev, "default");
813 		dm_gpio_free(mmc->dev, &pwr_en_gpio);
814 	}
815 #endif
816 
817 	if (host->board_init)
818 		host->board_init(host);
819 #ifdef CONFIG_ARCH_ROCKCHIP
820 	if (host->dev_index == 0)
821 		dwmci_writel(host, DWMCI_PWREN, 1);
822 	else if (host->dev_index == 1)
823 		dwmci_writel(host, DWMCI_PWREN, CONFIG_MMC_DW_PWREN_VALUE);
824 	else
825 		dwmci_writel(host, DWMCI_PWREN, 1);
826 #else
827 	dwmci_writel(host, DWMCI_PWREN, 1);
828 #endif
829 
830 	verid = dwmci_readl(host, DWMCI_VERID) & 0x0000ffff;
831 	if (verid >= DW_MMC_240A)
832 		dwmci_writel(host, DWMCI_CARDTHRCTL, DWMCI_CDTHRCTRL_CONFIG);
833 
834 	if (!dwmci_wait_reset(host, DWMCI_RESET_ALL)) {
835 		debug("%s[%d] Fail-reset!!\n", __func__, __LINE__);
836 		return -EIO;
837 	}
838 
839 	use_dma = SDMMC_GET_TRANS_MODE(dwmci_readl(host, DWMCI_HCON));
840 	if (use_dma == DMA_INTERFACE_IDMA) {
841 		host->fifo_mode = 0;
842 	} else {
843 		host->fifo_mode = 1;
844 	}
845 
846 	/* Enumerate at 400KHz */
847 	dwmci_setup_bus(host, mmc->cfg->f_min);
848 
849 	dwmci_writel(host, DWMCI_RINTSTS, 0xFFFFFFFF);
850 	dwmci_writel(host, DWMCI_INTMASK, 0);
851 
852 	dwmci_writel(host, DWMCI_TMOUT, 0xFFFFFFFF);
853 
854 	dwmci_writel(host, DWMCI_IDINTEN, 0);
855 	dwmci_writel(host, DWMCI_BMOD, 1);
856 
857 	if (!host->fifoth_val) {
858 		uint32_t fifo_size;
859 
860 		fifo_size = dwmci_readl(host, DWMCI_FIFOTH);
861 		fifo_size = ((fifo_size & RX_WMARK_MASK) >> RX_WMARK_SHIFT) + 1;
862 		host->fifoth_val = MSIZE(DWMCI_MSIZE) |
863 				RX_WMARK(fifo_size / 2 - 1) |
864 				TX_WMARK(fifo_size / 2);
865 	}
866 	dwmci_writel(host, DWMCI_FIFOTH, host->fifoth_val);
867 
868 	dwmci_writel(host, DWMCI_CLKENA, 0);
869 	dwmci_writel(host, DWMCI_CLKSRC, 0);
870 
871 	return 0;
872 }
873 
874 static int dwmci_get_cd(struct udevice *dev)
875 {
876 	int ret = -1;
877 	struct mmc *mmc = mmc_get_mmc_dev(dev);
878 	struct dwmci_host *host = mmc->priv;
879 
880 #if defined(CONFIG_DM_GPIO) && (defined(CONFIG_SPL_GPIO_SUPPORT) || !defined(CONFIG_SPL_BUILD))
881 	struct gpio_desc detect;
882 
883 	ret = gpio_request_by_name(dev, "cd-gpios", 0, &detect, GPIOD_IS_IN);
884 	if (ret) {
885 		goto dw_mmc_cdetect;
886 	}
887 
888 	ret = !dm_gpio_get_value(&detect);
889 	dm_gpio_free(dev, &detect);
890 	return ret;
891 dw_mmc_cdetect:
892 #endif
893 	ret = (dwmci_readl(host, DWMCI_CDETECT) & (1 << 0)) == 0 ? 1 : 0;
894 
895 	return ret;
896 }
897 
898 #ifdef CONFIG_DM_MMC
899 int dwmci_probe(struct udevice *dev)
900 {
901 	struct mmc *mmc = mmc_get_mmc_dev(dev);
902 
903 	return dwmci_init(mmc);
904 }
905 
906 const struct dm_mmc_ops dm_dwmci_ops = {
907 	.card_busy	= dwmci_card_busy,
908 	.send_cmd	= dwmci_send_cmd,
909 #ifdef CONFIG_SPL_BLK_READ_PREPARE
910 	.send_cmd_prepare = dwmci_send_cmd_prepare,
911 #endif
912 	.set_ios	= dwmci_set_ios,
913 	.get_cd         = dwmci_get_cd,
914 	.execute_tuning	= dwmci_execute_tuning,
915 };
916 
917 #else
918 static const struct mmc_ops dwmci_ops = {
919 	.card_busy	= dwmci_card_busy,
920 	.send_cmd	= dwmci_send_cmd,
921 	.set_ios	= dwmci_set_ios,
922 	.get_cd         = dwmci_get_cd,
923 	.init		= dwmci_init,
924 	.execute_tuning	= dwmci_execute_tuning,
925 };
926 #endif
927 
928 void dwmci_setup_cfg(struct mmc_config *cfg, struct dwmci_host *host,
929 		u32 max_clk, u32 min_clk)
930 {
931 	cfg->name = host->name;
932 #ifndef CONFIG_DM_MMC
933 	cfg->ops = &dwmci_ops;
934 #endif
935 	cfg->f_min = min_clk;
936 	cfg->f_max = max_clk;
937 
938 	cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
939 
940 	cfg->host_caps = host->caps;
941 
942 	switch (host->buswidth) {
943 	case 8:
944 		cfg->host_caps |= MMC_MODE_8BIT | MMC_MODE_4BIT;
945 		break;
946 	case 4:
947 		cfg->host_caps |= MMC_MODE_4BIT;
948 		cfg->host_caps &= ~MMC_MODE_8BIT;
949 		break;
950 	case 1:
951 		cfg->host_caps &= ~MMC_MODE_4BIT;
952 		cfg->host_caps &= ~MMC_MODE_8BIT;
953 		break;
954 	default:
955 		printf("Unsupported bus width: %d\n", host->buswidth);
956 		break;
957 	}
958 	cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz;
959 
960 	cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
961 }
962 
963 #ifdef CONFIG_BLK
964 int dwmci_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg)
965 {
966 	return mmc_bind(dev, mmc, cfg);
967 }
968 #else
969 int add_dwmci(struct dwmci_host *host, u32 max_clk, u32 min_clk)
970 {
971 	dwmci_setup_cfg(&host->cfg, host, max_clk, min_clk);
972 
973 	host->mmc = mmc_create(&host->cfg, host);
974 	if (host->mmc == NULL)
975 		return -1;
976 
977 	return 0;
978 }
979 #endif
980