xref: /rk3399_rockchip-uboot/drivers/mmc/sdhci.c (revision 6d216b78f2d05dd226056c8f255ea7e7e761e32a)
1af62a557SLei Wen /*
2af62a557SLei Wen  * Copyright 2011, Marvell Semiconductor Inc.
3af62a557SLei Wen  * Lei Wen <leiwen@marvell.com>
4af62a557SLei Wen  *
51a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
6af62a557SLei Wen  *
7af62a557SLei Wen  * Back ported to the 8xx platform (from the 8260 platform) by
8af62a557SLei Wen  * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
9af62a557SLei Wen  */
10af62a557SLei Wen 
11af62a557SLei Wen #include <common.h>
122a809093SSimon Glass #include <errno.h>
13af62a557SLei Wen #include <malloc.h>
14af62a557SLei Wen #include <mmc.h>
15af62a557SLei Wen #include <sdhci.h>
16af62a557SLei Wen 
17492d3223SStefan Roese #if defined(CONFIG_FIXED_SDHCI_ALIGNED_BUFFER)
18492d3223SStefan Roese void *aligned_buffer = (void *)CONFIG_FIXED_SDHCI_ALIGNED_BUFFER;
19492d3223SStefan Roese #else
20af62a557SLei Wen void *aligned_buffer;
21492d3223SStefan Roese #endif
22af62a557SLei Wen 
23af62a557SLei Wen static void sdhci_reset(struct sdhci_host *host, u8 mask)
24af62a557SLei Wen {
25af62a557SLei Wen 	unsigned long timeout;
26af62a557SLei Wen 
27af62a557SLei Wen 	/* Wait max 100 ms */
28af62a557SLei Wen 	timeout = 100;
29af62a557SLei Wen 	sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
30af62a557SLei Wen 	while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
31af62a557SLei Wen 		if (timeout == 0) {
3230e6d979SDarwin Rambo 			printf("%s: Reset 0x%x never completed.\n",
3330e6d979SDarwin Rambo 			       __func__, (int)mask);
34af62a557SLei Wen 			return;
35af62a557SLei Wen 		}
36af62a557SLei Wen 		timeout--;
37af62a557SLei Wen 		udelay(1000);
38af62a557SLei Wen 	}
39af62a557SLei Wen }
40af62a557SLei Wen 
41af62a557SLei Wen static void sdhci_cmd_done(struct sdhci_host *host, struct mmc_cmd *cmd)
42af62a557SLei Wen {
43af62a557SLei Wen 	int i;
44af62a557SLei Wen 	if (cmd->resp_type & MMC_RSP_136) {
45af62a557SLei Wen 		/* CRC is stripped so we need to do some shifting. */
46af62a557SLei Wen 		for (i = 0; i < 4; i++) {
47af62a557SLei Wen 			cmd->response[i] = sdhci_readl(host,
48af62a557SLei Wen 					SDHCI_RESPONSE + (3-i)*4) << 8;
49af62a557SLei Wen 			if (i != 3)
50af62a557SLei Wen 				cmd->response[i] |= sdhci_readb(host,
51af62a557SLei Wen 						SDHCI_RESPONSE + (3-i)*4-1);
52af62a557SLei Wen 		}
53af62a557SLei Wen 	} else {
54af62a557SLei Wen 		cmd->response[0] = sdhci_readl(host, SDHCI_RESPONSE);
55af62a557SLei Wen 	}
56af62a557SLei Wen }
57af62a557SLei Wen 
58af62a557SLei Wen static void sdhci_transfer_pio(struct sdhci_host *host, struct mmc_data *data)
59af62a557SLei Wen {
60af62a557SLei Wen 	int i;
61af62a557SLei Wen 	char *offs;
62af62a557SLei Wen 	for (i = 0; i < data->blocksize; i += 4) {
63af62a557SLei Wen 		offs = data->dest + i;
64af62a557SLei Wen 		if (data->flags == MMC_DATA_READ)
65af62a557SLei Wen 			*(u32 *)offs = sdhci_readl(host, SDHCI_BUFFER);
66af62a557SLei Wen 		else
67af62a557SLei Wen 			sdhci_writel(host, *(u32 *)offs, SDHCI_BUFFER);
68af62a557SLei Wen 	}
69af62a557SLei Wen }
70af62a557SLei Wen 
71af62a557SLei Wen static int sdhci_transfer_data(struct sdhci_host *host, struct mmc_data *data,
72af62a557SLei Wen 				unsigned int start_addr)
73af62a557SLei Wen {
74a004abdeSLei Wen 	unsigned int stat, rdy, mask, timeout, block = 0;
757dde50d7SAlex Deymo 	bool transfer_done = false;
7645a68fe2SMasahiro Yamada #ifdef CONFIG_MMC_SDHCI_SDMA
77804c7f42SJaehoon Chung 	unsigned char ctrl;
782c011847SJuhyun \(Justin\) Oh 	ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
79804c7f42SJaehoon Chung 	ctrl &= ~SDHCI_CTRL_DMA_MASK;
802c011847SJuhyun \(Justin\) Oh 	sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
81804c7f42SJaehoon Chung #endif
82af62a557SLei Wen 
835d48e422SJaehoon Chung 	timeout = 1000000;
84af62a557SLei Wen 	rdy = SDHCI_INT_SPACE_AVAIL | SDHCI_INT_DATA_AVAIL;
85af62a557SLei Wen 	mask = SDHCI_DATA_AVAILABLE | SDHCI_SPACE_AVAILABLE;
86af62a557SLei Wen 	do {
87af62a557SLei Wen 		stat = sdhci_readl(host, SDHCI_INT_STATUS);
88af62a557SLei Wen 		if (stat & SDHCI_INT_ERROR) {
8930e6d979SDarwin Rambo 			printf("%s: Error detected in status(0x%X)!\n",
9030e6d979SDarwin Rambo 			       __func__, stat);
912cb5d67cSJaehoon Chung 			return -EIO;
92af62a557SLei Wen 		}
937dde50d7SAlex Deymo 		if (!transfer_done && (stat & rdy)) {
94af62a557SLei Wen 			if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & mask))
95af62a557SLei Wen 				continue;
96af62a557SLei Wen 			sdhci_writel(host, rdy, SDHCI_INT_STATUS);
97af62a557SLei Wen 			sdhci_transfer_pio(host, data);
98af62a557SLei Wen 			data->dest += data->blocksize;
997dde50d7SAlex Deymo 			if (++block >= data->blocks) {
1007dde50d7SAlex Deymo 				/* Keep looping until the SDHCI_INT_DATA_END is
1017dde50d7SAlex Deymo 				 * cleared, even if we finished sending all the
1027dde50d7SAlex Deymo 				 * blocks.
1037dde50d7SAlex Deymo 				 */
1047dde50d7SAlex Deymo 				transfer_done = true;
1057dde50d7SAlex Deymo 				continue;
1067dde50d7SAlex Deymo 			}
107af62a557SLei Wen 		}
10845a68fe2SMasahiro Yamada #ifdef CONFIG_MMC_SDHCI_SDMA
1097dde50d7SAlex Deymo 		if (!transfer_done && (stat & SDHCI_INT_DMA_END)) {
110af62a557SLei Wen 			sdhci_writel(host, SDHCI_INT_DMA_END, SDHCI_INT_STATUS);
1113e81c772SLei Wen 			start_addr &= ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1);
112af62a557SLei Wen 			start_addr += SDHCI_DEFAULT_BOUNDARY_SIZE;
113af62a557SLei Wen 			sdhci_writel(host, start_addr, SDHCI_DMA_ADDRESS);
114af62a557SLei Wen 		}
115af62a557SLei Wen #endif
116a004abdeSLei Wen 		if (timeout-- > 0)
117a004abdeSLei Wen 			udelay(10);
118a004abdeSLei Wen 		else {
11930e6d979SDarwin Rambo 			printf("%s: Transfer data timeout\n", __func__);
1202cb5d67cSJaehoon Chung 			return -ETIMEDOUT;
121a004abdeSLei Wen 		}
122af62a557SLei Wen 	} while (!(stat & SDHCI_INT_DATA_END));
123af62a557SLei Wen 	return 0;
124af62a557SLei Wen }
125af62a557SLei Wen 
12656b34bc6SPrzemyslaw Marczak /*
12756b34bc6SPrzemyslaw Marczak  * No command will be sent by driver if card is busy, so driver must wait
12856b34bc6SPrzemyslaw Marczak  * for card ready state.
12956b34bc6SPrzemyslaw Marczak  * Every time when card is busy after timeout then (last) timeout value will be
13056b34bc6SPrzemyslaw Marczak  * increased twice but only if it doesn't exceed global defined maximum.
13165a25b20SMasahiro Yamada  * Each function call will use last timeout value.
13256b34bc6SPrzemyslaw Marczak  */
13365a25b20SMasahiro Yamada #define SDHCI_CMD_MAX_TIMEOUT			3200
134d8ce77b2SMasahiro Yamada #define SDHCI_CMD_DEFAULT_TIMEOUT		100
135d90bb439SSteve Rae #define SDHCI_READ_STATUS_TIMEOUT		1000
13656b34bc6SPrzemyslaw Marczak 
137e7881d85SSimon Glass #ifdef CONFIG_DM_MMC
138ef1e4edaSSimon Glass static int sdhci_send_command(struct udevice *dev, struct mmc_cmd *cmd,
139ef1e4edaSSimon Glass 			      struct mmc_data *data)
140ef1e4edaSSimon Glass {
141ef1e4edaSSimon Glass 	struct mmc *mmc = mmc_get_mmc_dev(dev);
142ef1e4edaSSimon Glass 
143ef1e4edaSSimon Glass #else
1446588c78bSJeroen Hofstee static int sdhci_send_command(struct mmc *mmc, struct mmc_cmd *cmd,
145af62a557SLei Wen 			      struct mmc_data *data)
146af62a557SLei Wen {
147ef1e4edaSSimon Glass #endif
14893bfd616SPantelis Antoniou 	struct sdhci_host *host = mmc->priv;
149af62a557SLei Wen 	unsigned int stat = 0;
150af62a557SLei Wen 	int ret = 0;
151af62a557SLei Wen 	int trans_bytes = 0, is_aligned = 1;
152af62a557SLei Wen 	u32 mask, flags, mode;
15356b34bc6SPrzemyslaw Marczak 	unsigned int time = 0, start_addr = 0;
15419d2e342SSimon Glass 	int mmc_dev = mmc_get_blk_desc(mmc)->devnum;
15529905a45SStefan Roese 	unsigned start = get_timer(0);
156af62a557SLei Wen 
15756b34bc6SPrzemyslaw Marczak 	/* Timeout unit - ms */
158d8ce77b2SMasahiro Yamada 	static unsigned int cmd_timeout = SDHCI_CMD_DEFAULT_TIMEOUT;
159af62a557SLei Wen 
1607279e487SZiyuan Xu 	mask = SDHCI_CMD_INHIBIT;
1617279e487SZiyuan Xu 
1627279e487SZiyuan Xu 	if (data)
1637279e487SZiyuan Xu 		mask |= SDHCI_DATA_INHIBIT;
164af62a557SLei Wen 
165af62a557SLei Wen 	/* We shouldn't wait for data inihibit for stop commands, even
166af62a557SLei Wen 	   though they might use busy signaling */
167af62a557SLei Wen 	if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
168af62a557SLei Wen 		mask &= ~SDHCI_DATA_INHIBIT;
169af62a557SLei Wen 
170af62a557SLei Wen 	while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
17156b34bc6SPrzemyslaw Marczak 		if (time >= cmd_timeout) {
17230e6d979SDarwin Rambo 			printf("%s: MMC: %d busy ", __func__, mmc_dev);
17365a25b20SMasahiro Yamada 			if (2 * cmd_timeout <= SDHCI_CMD_MAX_TIMEOUT) {
17456b34bc6SPrzemyslaw Marczak 				cmd_timeout += cmd_timeout;
17556b34bc6SPrzemyslaw Marczak 				printf("timeout increasing to: %u ms.\n",
17656b34bc6SPrzemyslaw Marczak 				       cmd_timeout);
177*6d216b78SYifeng Zhao 				sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
17856b34bc6SPrzemyslaw Marczak 			} else {
17956b34bc6SPrzemyslaw Marczak 				puts("timeout.\n");
180*6d216b78SYifeng Zhao 				/* remove timeout return error and try to send command */
181af62a557SLei Wen 			}
18256b34bc6SPrzemyslaw Marczak 		}
18356b34bc6SPrzemyslaw Marczak 		time++;
184af62a557SLei Wen 		udelay(1000);
185af62a557SLei Wen 	}
186af62a557SLei Wen 
187c060f28dSJorge Ramirez-Ortiz 	sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
188c060f28dSJorge Ramirez-Ortiz 
189af62a557SLei Wen 	mask = SDHCI_INT_RESPONSE;
190af62a557SLei Wen 	if (!(cmd->resp_type & MMC_RSP_PRESENT))
191af62a557SLei Wen 		flags = SDHCI_CMD_RESP_NONE;
192af62a557SLei Wen 	else if (cmd->resp_type & MMC_RSP_136)
193af62a557SLei Wen 		flags = SDHCI_CMD_RESP_LONG;
194af62a557SLei Wen 	else if (cmd->resp_type & MMC_RSP_BUSY) {
195af62a557SLei Wen 		flags = SDHCI_CMD_RESP_SHORT_BUSY;
19617ea3c86SJaehoon Chung 		if (data)
197af62a557SLei Wen 			mask |= SDHCI_INT_DATA_END;
198af62a557SLei Wen 	} else
199af62a557SLei Wen 		flags = SDHCI_CMD_RESP_SHORT;
200af62a557SLei Wen 
201af62a557SLei Wen 	if (cmd->resp_type & MMC_RSP_CRC)
202af62a557SLei Wen 		flags |= SDHCI_CMD_CRC;
203af62a557SLei Wen 	if (cmd->resp_type & MMC_RSP_OPCODE)
204af62a557SLei Wen 		flags |= SDHCI_CMD_INDEX;
205af62a557SLei Wen 	if (data)
206af62a557SLei Wen 		flags |= SDHCI_CMD_DATA;
207af62a557SLei Wen 
2087279e487SZiyuan Xu 	if (cmd->cmdidx == MMC_SEND_TUNING_BLOCK ||
2097279e487SZiyuan Xu 	    cmd->cmdidx == MMC_SEND_TUNING_BLOCK_HS200) {
2107279e487SZiyuan Xu 		mask &= ~SDHCI_INT_RESPONSE;
2117279e487SZiyuan Xu 		mask |= SDHCI_INT_DATA_AVAIL;
2127279e487SZiyuan Xu 		flags |= SDHCI_CMD_DATA;
2137279e487SZiyuan Xu 	}
2147279e487SZiyuan Xu 
215af62a557SLei Wen 	/* Set Transfer mode regarding to data flag */
216af62a557SLei Wen 	if (data != 0) {
217af62a557SLei Wen 		sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL);
218af62a557SLei Wen 		mode = SDHCI_TRNS_BLK_CNT_EN;
219af62a557SLei Wen 		trans_bytes = data->blocks * data->blocksize;
220af62a557SLei Wen 		if (data->blocks > 1)
221af62a557SLei Wen 			mode |= SDHCI_TRNS_MULTI;
222af62a557SLei Wen 
223af62a557SLei Wen 		if (data->flags == MMC_DATA_READ)
224af62a557SLei Wen 			mode |= SDHCI_TRNS_READ;
225af62a557SLei Wen 
22645a68fe2SMasahiro Yamada #ifdef CONFIG_MMC_SDHCI_SDMA
227af62a557SLei Wen 		if (data->flags == MMC_DATA_READ)
2283c1fcb77SRob Herring 			start_addr = (unsigned long)data->dest;
229af62a557SLei Wen 		else
2303c1fcb77SRob Herring 			start_addr = (unsigned long)data->src;
231af62a557SLei Wen 		if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
232af62a557SLei Wen 				(start_addr & 0x7) != 0x0) {
233af62a557SLei Wen 			is_aligned = 0;
2343c1fcb77SRob Herring 			start_addr = (unsigned long)aligned_buffer;
235af62a557SLei Wen 			if (data->flags != MMC_DATA_READ)
236af62a557SLei Wen 				memcpy(aligned_buffer, data->src, trans_bytes);
237af62a557SLei Wen 		}
238af62a557SLei Wen 
239492d3223SStefan Roese #if defined(CONFIG_FIXED_SDHCI_ALIGNED_BUFFER)
240492d3223SStefan Roese 		/*
241492d3223SStefan Roese 		 * Always use this bounce-buffer when
242492d3223SStefan Roese 		 * CONFIG_FIXED_SDHCI_ALIGNED_BUFFER is defined
243492d3223SStefan Roese 		 */
244492d3223SStefan Roese 		is_aligned = 0;
245492d3223SStefan Roese 		start_addr = (unsigned long)aligned_buffer;
246492d3223SStefan Roese 		if (data->flags != MMC_DATA_READ)
247492d3223SStefan Roese 			memcpy(aligned_buffer, data->src, trans_bytes);
248492d3223SStefan Roese #endif
249492d3223SStefan Roese 
250af62a557SLei Wen 		sdhci_writel(host, start_addr, SDHCI_DMA_ADDRESS);
251af62a557SLei Wen 		mode |= SDHCI_TRNS_DMA;
252af62a557SLei Wen #endif
253af62a557SLei Wen 		sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
254af62a557SLei Wen 				data->blocksize),
255af62a557SLei Wen 				SDHCI_BLOCK_SIZE);
256af62a557SLei Wen 		sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
257af62a557SLei Wen 		sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
2585e1c23cdSKevin Liu 	} else if (cmd->resp_type & MMC_RSP_BUSY) {
2595e1c23cdSKevin Liu 		sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL);
260af62a557SLei Wen 	}
261af62a557SLei Wen 
262af62a557SLei Wen 	sdhci_writel(host, cmd->cmdarg, SDHCI_ARGUMENT);
26345a68fe2SMasahiro Yamada #ifdef CONFIG_MMC_SDHCI_SDMA
264fa7720b2SKevin Liu 	if (data != 0) {
265be256cbfSJaehoon Chung 		trans_bytes = ALIGN(trans_bytes, CONFIG_SYS_CACHELINE_SIZE);
2662c2ec4c9SLei Wen 		flush_cache(start_addr, trans_bytes);
267fa7720b2SKevin Liu 	}
268af62a557SLei Wen #endif
269af62a557SLei Wen 	sdhci_writew(host, SDHCI_MAKE_CMD(cmd->cmdidx, flags), SDHCI_COMMAND);
27029905a45SStefan Roese 	start = get_timer(0);
271af62a557SLei Wen 	do {
272af62a557SLei Wen 		stat = sdhci_readl(host, SDHCI_INT_STATUS);
273af62a557SLei Wen 		if (stat & SDHCI_INT_ERROR)
274af62a557SLei Wen 			break;
275af62a557SLei Wen 
276d90bb439SSteve Rae 		if (get_timer(start) >= SDHCI_READ_STATUS_TIMEOUT) {
277bae4a1fdSMasahiro Yamada 			if (host->quirks & SDHCI_QUIRK_BROKEN_R1B) {
2783a638320SJaehoon Chung 				return 0;
279bae4a1fdSMasahiro Yamada 			} else {
280bae4a1fdSMasahiro Yamada 				printf("%s: Timeout for status update!\n",
281bae4a1fdSMasahiro Yamada 				       __func__);
282915ffa52SJaehoon Chung 				return -ETIMEDOUT;
2833a638320SJaehoon Chung 			}
2843a638320SJaehoon Chung 		}
285bae4a1fdSMasahiro Yamada 	} while ((stat & mask) != mask);
2863a638320SJaehoon Chung 
287af62a557SLei Wen 	if ((stat & (SDHCI_INT_ERROR | mask)) == mask) {
288af62a557SLei Wen 		sdhci_cmd_done(host, cmd);
289af62a557SLei Wen 		sdhci_writel(host, mask, SDHCI_INT_STATUS);
290af62a557SLei Wen 	} else
291af62a557SLei Wen 		ret = -1;
292af62a557SLei Wen 
293af62a557SLei Wen 	if (!ret && data)
294af62a557SLei Wen 		ret = sdhci_transfer_data(host, data, start_addr);
295af62a557SLei Wen 
29613243f2eSTushar Behera 	if (host->quirks & SDHCI_QUIRK_WAIT_SEND_CMD)
29713243f2eSTushar Behera 		udelay(1000);
29813243f2eSTushar Behera 
299af62a557SLei Wen 	stat = sdhci_readl(host, SDHCI_INT_STATUS);
300af62a557SLei Wen 	sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
301af62a557SLei Wen 	if (!ret) {
302af62a557SLei Wen 		if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
303af62a557SLei Wen 				!is_aligned && (data->flags == MMC_DATA_READ))
304af62a557SLei Wen 			memcpy(data->dest, aligned_buffer, trans_bytes);
305af62a557SLei Wen 		return 0;
306af62a557SLei Wen 	}
307af62a557SLei Wen 
308af62a557SLei Wen 	sdhci_reset(host, SDHCI_RESET_CMD);
309af62a557SLei Wen 	sdhci_reset(host, SDHCI_RESET_DATA);
310af62a557SLei Wen 	if (stat & SDHCI_INT_TIMEOUT)
311915ffa52SJaehoon Chung 		return -ETIMEDOUT;
312af62a557SLei Wen 	else
313915ffa52SJaehoon Chung 		return -ECOMM;
314af62a557SLei Wen }
315af62a557SLei Wen 
316a15c58b2SZiyuan Xu int sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
317af62a557SLei Wen {
318899fb9e3SStefan Roese 	unsigned int div, clk = 0, timeout;
319af62a557SLei Wen 
32079667b7bSWenyou Yang 	/* Wait max 20 ms */
32179667b7bSWenyou Yang 	timeout = 200;
32279667b7bSWenyou Yang 	while (sdhci_readl(host, SDHCI_PRESENT_STATE) &
32379667b7bSWenyou Yang 			   (SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT)) {
32479667b7bSWenyou Yang 		if (timeout == 0) {
32579667b7bSWenyou Yang 			printf("%s: Timeout to wait cmd & data inhibit\n",
32679667b7bSWenyou Yang 			       __func__);
3272cb5d67cSJaehoon Chung 			return -EBUSY;
32879667b7bSWenyou Yang 		}
32979667b7bSWenyou Yang 
33079667b7bSWenyou Yang 		timeout--;
33179667b7bSWenyou Yang 		udelay(100);
33279667b7bSWenyou Yang 	}
333899fb9e3SStefan Roese 	sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
334af62a557SLei Wen 
335af62a557SLei Wen 	if (clock == 0)
336af62a557SLei Wen 		return 0;
337113e5dfcSJaehoon Chung 	if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) {
3386dffdbc3SWenyou Yang 		/*
3396dffdbc3SWenyou Yang 		 * Check if the Host Controller supports Programmable Clock
3406dffdbc3SWenyou Yang 		 * Mode.
3416dffdbc3SWenyou Yang 		 */
3426dffdbc3SWenyou Yang 		if (host->clk_mul) {
3436dffdbc3SWenyou Yang 			for (div = 1; div <= 1024; div++) {
3440e0dcc19SWenyou Yang 				if ((host->max_clk / div) <= clock)
3456dffdbc3SWenyou Yang 					break;
3466dffdbc3SWenyou Yang 			}
3476dffdbc3SWenyou Yang 
3486dffdbc3SWenyou Yang 			/*
3496dffdbc3SWenyou Yang 			 * Set Programmable Clock Mode in the Clock
3506dffdbc3SWenyou Yang 			 * Control register.
3516dffdbc3SWenyou Yang 			 */
3526dffdbc3SWenyou Yang 			clk = SDHCI_PROG_CLOCK_MODE;
3536dffdbc3SWenyou Yang 			div--;
3546dffdbc3SWenyou Yang 		} else {
355af62a557SLei Wen 			/* Version 3.00 divisors must be a multiple of 2. */
3566d0e34bfSStefan Herbrechtsmeier 			if (host->max_clk <= clock) {
357af62a557SLei Wen 				div = 1;
3586dffdbc3SWenyou Yang 			} else {
3596dffdbc3SWenyou Yang 				for (div = 2;
3606dffdbc3SWenyou Yang 				     div < SDHCI_MAX_DIV_SPEC_300;
3616dffdbc3SWenyou Yang 				     div += 2) {
3626d0e34bfSStefan Herbrechtsmeier 					if ((host->max_clk / div) <= clock)
363af62a557SLei Wen 						break;
364af62a557SLei Wen 				}
365af62a557SLei Wen 			}
3666dffdbc3SWenyou Yang 			div >>= 1;
3676dffdbc3SWenyou Yang 		}
368af62a557SLei Wen 	} else {
369af62a557SLei Wen 		/* Version 2.00 divisors must be a power of 2. */
370af62a557SLei Wen 		for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
3716d0e34bfSStefan Herbrechtsmeier 			if ((host->max_clk / div) <= clock)
372af62a557SLei Wen 				break;
373af62a557SLei Wen 		}
374af62a557SLei Wen 		div >>= 1;
3756dffdbc3SWenyou Yang 	}
3765de82122SZiyuan Xu 	if (host->ops && host->ops->set_clock_ext)
3775de82122SZiyuan Xu 		host->ops->set_clock_ext(host, div);
378b09ed6e4SJaehoon Chung 
3796dffdbc3SWenyou Yang 	clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
380af62a557SLei Wen 	clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
381af62a557SLei Wen 		<< SDHCI_DIVIDER_HI_SHIFT;
382af62a557SLei Wen 	clk |= SDHCI_CLOCK_INT_EN;
383af62a557SLei Wen 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
384af62a557SLei Wen 
385af62a557SLei Wen 	/* Wait max 20 ms */
386af62a557SLei Wen 	timeout = 20;
387af62a557SLei Wen 	while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
388af62a557SLei Wen 		& SDHCI_CLOCK_INT_STABLE)) {
389af62a557SLei Wen 		if (timeout == 0) {
39030e6d979SDarwin Rambo 			printf("%s: Internal clock never stabilised.\n",
39130e6d979SDarwin Rambo 			       __func__);
3922cb5d67cSJaehoon Chung 			return -EBUSY;
393af62a557SLei Wen 		}
394af62a557SLei Wen 		timeout--;
395af62a557SLei Wen 		udelay(1000);
396af62a557SLei Wen 	}
397af62a557SLei Wen 	clk |= SDHCI_CLOCK_CARD_EN;
398af62a557SLei Wen 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
39931044c33SZiyuan Xu 
40031044c33SZiyuan Xu 	host->clock = clock;
401af62a557SLei Wen 	return 0;
402af62a557SLei Wen }
403af62a557SLei Wen 
404af62a557SLei Wen static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
405af62a557SLei Wen {
406af62a557SLei Wen 	u8 pwr = 0;
407af62a557SLei Wen 
408af62a557SLei Wen 	if (power != (unsigned short)-1) {
409af62a557SLei Wen 		switch (1 << power) {
410af62a557SLei Wen 		case MMC_VDD_165_195:
411af62a557SLei Wen 			pwr = SDHCI_POWER_180;
412af62a557SLei Wen 			break;
413af62a557SLei Wen 		case MMC_VDD_29_30:
414af62a557SLei Wen 		case MMC_VDD_30_31:
415af62a557SLei Wen 			pwr = SDHCI_POWER_300;
416af62a557SLei Wen 			break;
417af62a557SLei Wen 		case MMC_VDD_32_33:
418af62a557SLei Wen 		case MMC_VDD_33_34:
419af62a557SLei Wen 			pwr = SDHCI_POWER_330;
420af62a557SLei Wen 			break;
421af62a557SLei Wen 		}
422af62a557SLei Wen 	}
423af62a557SLei Wen 
424af62a557SLei Wen 	if (pwr == 0) {
425af62a557SLei Wen 		sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
426af62a557SLei Wen 		return;
427af62a557SLei Wen 	}
428af62a557SLei Wen 
429af62a557SLei Wen 	pwr |= SDHCI_POWER_ON;
430af62a557SLei Wen 
431af62a557SLei Wen 	sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
432af62a557SLei Wen }
433af62a557SLei Wen 
43476194d8cSZiyuan Xu static void sdhci_set_uhs_signaling(struct sdhci_host *host)
43576194d8cSZiyuan Xu {
43676194d8cSZiyuan Xu 	u16 ctrl_2;
43776194d8cSZiyuan Xu 	u32 timing = host->mmc->timing;
43876194d8cSZiyuan Xu 
43976194d8cSZiyuan Xu 	ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
44076194d8cSZiyuan Xu 	/* Select Bus Speed Mode for host */
44176194d8cSZiyuan Xu 	ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
44276194d8cSZiyuan Xu 
44376194d8cSZiyuan Xu 	if ((timing != MMC_TIMING_LEGACY) &&
44476194d8cSZiyuan Xu 	    (timing != MMC_TIMING_MMC_HS) &&
44576194d8cSZiyuan Xu 	    (timing != MMC_TIMING_SD_HS))
44676194d8cSZiyuan Xu 		ctrl_2 |= SDHCI_CTRL_VDD_180;
44776194d8cSZiyuan Xu 
44876194d8cSZiyuan Xu 	if ((timing == MMC_TIMING_MMC_HS200) ||
44976194d8cSZiyuan Xu 	    (timing == MMC_TIMING_UHS_SDR104))
45076194d8cSZiyuan Xu 		ctrl_2 |= SDHCI_CTRL_UHS_SDR104 | SDHCI_CTRL_DRV_TYPE_A;
45176194d8cSZiyuan Xu 	else if (timing == MMC_TIMING_UHS_SDR12)
45276194d8cSZiyuan Xu 		ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
45376194d8cSZiyuan Xu 	else if (timing == MMC_TIMING_UHS_SDR25)
45476194d8cSZiyuan Xu 		ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
455850fcf3eSchenfen 	else if ((timing == MMC_TIMING_UHS_SDR50) ||
456850fcf3eSchenfen 		(timing == MMC_TIMING_MMC_HS))
45776194d8cSZiyuan Xu 		ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
45876194d8cSZiyuan Xu 	else if ((timing == MMC_TIMING_UHS_DDR50) ||
45976194d8cSZiyuan Xu 		 (timing == MMC_TIMING_MMC_DDR52))
46076194d8cSZiyuan Xu 		ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
46176194d8cSZiyuan Xu 	else if (timing == MMC_TIMING_MMC_HS400 ||
46276194d8cSZiyuan Xu 		 timing == MMC_TIMING_MMC_HS400ES)
46376194d8cSZiyuan Xu 		ctrl_2 |= SDHCI_CTRL_HS400 | SDHCI_CTRL_DRV_TYPE_A;
46476194d8cSZiyuan Xu 
46576194d8cSZiyuan Xu 	sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
46676194d8cSZiyuan Xu }
46776194d8cSZiyuan Xu 
468e7881d85SSimon Glass #ifdef CONFIG_DM_MMC
469bdd003c0SZiyuan Xu static bool sdhci_card_busy(struct udevice *dev)
470bdd003c0SZiyuan Xu {
471bdd003c0SZiyuan Xu 	struct mmc *mmc = mmc_get_mmc_dev(dev);
472bdd003c0SZiyuan Xu #else
473bdd003c0SZiyuan Xu static bool sdhci_card_busy(struct mmc *mmc)
474bdd003c0SZiyuan Xu {
475bdd003c0SZiyuan Xu #endif
476bdd003c0SZiyuan Xu 	struct sdhci_host *host = mmc->priv;
477bdd003c0SZiyuan Xu 	u32 present_state;
478bdd003c0SZiyuan Xu 
479bdd003c0SZiyuan Xu 	/* Check whether DAT[0] is 0 */
480bdd003c0SZiyuan Xu 	present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
481bdd003c0SZiyuan Xu 
482bdd003c0SZiyuan Xu 	return !(present_state & SDHCI_DATA_0_LVL);
483bdd003c0SZiyuan Xu }
484bdd003c0SZiyuan Xu 
485bdd003c0SZiyuan Xu #ifdef CONFIG_DM_MMC
486ef1e4edaSSimon Glass static int sdhci_set_ios(struct udevice *dev)
487ef1e4edaSSimon Glass {
488ef1e4edaSSimon Glass 	struct mmc *mmc = mmc_get_mmc_dev(dev);
489ef1e4edaSSimon Glass #else
49007b0b9c0SJaehoon Chung static int sdhci_set_ios(struct mmc *mmc)
491af62a557SLei Wen {
492ef1e4edaSSimon Glass #endif
493af62a557SLei Wen 	u32 ctrl;
49493bfd616SPantelis Antoniou 	struct sdhci_host *host = mmc->priv;
495af62a557SLei Wen 
496bf9c4d14SMasahiro Yamada 	if (host->ops && host->ops->set_control_reg)
49762226b68SJaehoon Chung 		host->ops->set_control_reg(host);
498236bfecfSJaehoon Chung 
499a15c58b2SZiyuan Xu 	if (mmc->clock != host->clock) {
500a15c58b2SZiyuan Xu 		if (host->ops && host->ops->set_clock)
501a15c58b2SZiyuan Xu 			host->ops->set_clock(host, mmc->clock);
502a15c58b2SZiyuan Xu 		else
503a15c58b2SZiyuan Xu 			sdhci_set_clock(host, mmc->clock);
504a15c58b2SZiyuan Xu 	}
505af62a557SLei Wen 
506af62a557SLei Wen 	/* Set bus width */
507af62a557SLei Wen 	ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
508af62a557SLei Wen 	if (mmc->bus_width == 8) {
509af62a557SLei Wen 		ctrl &= ~SDHCI_CTRL_4BITBUS;
510113e5dfcSJaehoon Chung 		if ((SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) ||
511113e5dfcSJaehoon Chung 				(host->quirks & SDHCI_QUIRK_USE_WIDE8))
512af62a557SLei Wen 			ctrl |= SDHCI_CTRL_8BITBUS;
513af62a557SLei Wen 	} else {
514f88a429fSMatt Reimer 		if ((SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) ||
515f88a429fSMatt Reimer 				(host->quirks & SDHCI_QUIRK_USE_WIDE8))
516af62a557SLei Wen 			ctrl &= ~SDHCI_CTRL_8BITBUS;
517af62a557SLei Wen 		if (mmc->bus_width == 4)
518af62a557SLei Wen 			ctrl |= SDHCI_CTRL_4BITBUS;
519af62a557SLei Wen 		else
520af62a557SLei Wen 			ctrl &= ~SDHCI_CTRL_4BITBUS;
521af62a557SLei Wen 	}
522af62a557SLei Wen 
5239f83e5c6SZiyuan Xu 	if (!(mmc->timing == MMC_TIMING_LEGACY) &&
5249f83e5c6SZiyuan Xu 	    !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
525af62a557SLei Wen 		ctrl |= SDHCI_CTRL_HISPD;
526af62a557SLei Wen 	else
527af62a557SLei Wen 		ctrl &= ~SDHCI_CTRL_HISPD;
528af62a557SLei Wen 
529af62a557SLei Wen 	sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
53007b0b9c0SJaehoon Chung 
53176194d8cSZiyuan Xu 	if ((mmc->timing != MMC_TIMING_LEGACY) &&
53276194d8cSZiyuan Xu 	    (mmc->timing != MMC_TIMING_MMC_HS) &&
53376194d8cSZiyuan Xu 	    (mmc->timing != MMC_TIMING_SD_HS))
53476194d8cSZiyuan Xu 		sdhci_set_power(host, MMC_VDD_165_195_SHIFT);
53576194d8cSZiyuan Xu 
53676194d8cSZiyuan Xu 	sdhci_set_uhs_signaling(host);
53776194d8cSZiyuan Xu 
538210841c6SStefan Roese 	/* If available, call the driver specific "post" set_ios() function */
539210841c6SStefan Roese 	if (host->ops && host->ops->set_ios_post)
540210841c6SStefan Roese 		host->ops->set_ios_post(host);
541210841c6SStefan Roese 
542ef1e4edaSSimon Glass 	return 0;
543af62a557SLei Wen }
544af62a557SLei Wen 
5456588c78bSJeroen Hofstee static int sdhci_init(struct mmc *mmc)
546af62a557SLei Wen {
54793bfd616SPantelis Antoniou 	struct sdhci_host *host = mmc->priv;
548af62a557SLei Wen 
5498d549b61SMasahiro Yamada 	sdhci_reset(host, SDHCI_RESET_ALL);
5508d549b61SMasahiro Yamada 
551af62a557SLei Wen 	if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) && !aligned_buffer) {
552af62a557SLei Wen 		aligned_buffer = memalign(8, 512*1024);
553af62a557SLei Wen 		if (!aligned_buffer) {
55430e6d979SDarwin Rambo 			printf("%s: Aligned buffer alloc failed!!!\n",
55530e6d979SDarwin Rambo 			       __func__);
5562cb5d67cSJaehoon Chung 			return -ENOMEM;
557af62a557SLei Wen 		}
558af62a557SLei Wen 	}
559af62a557SLei Wen 
56093bfd616SPantelis Antoniou 	sdhci_set_power(host, fls(mmc->cfg->voltages) - 1);
561470dcc75SJoe Hershberger 
562bf9c4d14SMasahiro Yamada 	if (host->ops && host->ops->get_cd)
5635e96217fSJaehoon Chung 		host->ops->get_cd(host);
564470dcc75SJoe Hershberger 
565ce0c1bc1SŁukasz Majewski 	/* Enable only interrupts served by the SD controller */
56630e6d979SDarwin Rambo 	sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK,
56730e6d979SDarwin Rambo 		     SDHCI_INT_ENABLE);
568ce0c1bc1SŁukasz Majewski 	/* Mask all sdhci interrupt sources */
569ce0c1bc1SŁukasz Majewski 	sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE);
570af62a557SLei Wen 
571af62a557SLei Wen 	return 0;
572af62a557SLei Wen }
573af62a557SLei Wen 
5747279e487SZiyuan Xu static int sdhci_send_tuning(struct sdhci_host *host, u32 opcode)
5757279e487SZiyuan Xu {
5767279e487SZiyuan Xu 	struct mmc_cmd cmd;
5777279e487SZiyuan Xu 
5787279e487SZiyuan Xu 	cmd.cmdidx = opcode;
5797279e487SZiyuan Xu 	cmd.resp_type = MMC_RSP_R1;
5807279e487SZiyuan Xu 	cmd.cmdarg = 0;
5817279e487SZiyuan Xu 	/*
5827279e487SZiyuan Xu 	 * In response to CMD19, the card sends 64 bytes of tuning
5837279e487SZiyuan Xu 	 * block to the Host Controller. So we set the block size
5847279e487SZiyuan Xu 	 * to 64 here.
5857279e487SZiyuan Xu 	 */
5867279e487SZiyuan Xu 	if (opcode == MMC_SEND_TUNING_BLOCK_HS200 &&
5877279e487SZiyuan Xu 	    host->mmc->bus_width == MMC_BUS_WIDTH_8BIT)
5887279e487SZiyuan Xu 		sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128), SDHCI_BLOCK_SIZE);
5897279e487SZiyuan Xu 	else
5907279e487SZiyuan Xu 		sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64), SDHCI_BLOCK_SIZE);
5917279e487SZiyuan Xu 
5927279e487SZiyuan Xu 	/*
5937279e487SZiyuan Xu 	 * The tuning block is sent by the card to the host controller.
5947279e487SZiyuan Xu 	 * So we set the TRNS_READ bit in the Transfer Mode register.
5957279e487SZiyuan Xu 	 * This also takes care of setting DMA Enable and Multi Block
5967279e487SZiyuan Xu 	 * Select in the same register to 0.
5977279e487SZiyuan Xu 	 */
5987279e487SZiyuan Xu 	sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
5997279e487SZiyuan Xu 
6007279e487SZiyuan Xu #ifdef CONFIG_DM_MMC
6017279e487SZiyuan Xu 	return sdhci_send_command(host->mmc->dev, &cmd, NULL);
6027279e487SZiyuan Xu #else
6037279e487SZiyuan Xu 	return sdhci_send_command(host->mmc, &cmd, NULL);
6047279e487SZiyuan Xu #endif
6057279e487SZiyuan Xu }
6067279e487SZiyuan Xu 
6077279e487SZiyuan Xu #define MAX_TUNING_LOOP 40
6087279e487SZiyuan Xu static int __sdhci_execute_tuning(struct sdhci_host *host, u32 opcode)
6097279e487SZiyuan Xu {
6107279e487SZiyuan Xu 	int i;
6117279e487SZiyuan Xu 	int ret;
6127279e487SZiyuan Xu 
6137279e487SZiyuan Xu 	/*
6147279e487SZiyuan Xu 	 * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
6157279e487SZiyuan Xu 	 * of loops reaches 40 times.
6167279e487SZiyuan Xu 	 */
6177279e487SZiyuan Xu 	for (i = 0; i < MAX_TUNING_LOOP; i++) {
6187279e487SZiyuan Xu 		u16 ctrl;
6197279e487SZiyuan Xu 
6207279e487SZiyuan Xu 		ret = sdhci_send_tuning(host, opcode);
6217279e487SZiyuan Xu 
6227279e487SZiyuan Xu 		if (ret)
6237279e487SZiyuan Xu 			return ret;
6247279e487SZiyuan Xu 
6257279e487SZiyuan Xu 		ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
6267279e487SZiyuan Xu 		if (!(ctrl & SDHCI_CTRL_EXEC_TUNING)) {
6277279e487SZiyuan Xu 			if (ctrl & SDHCI_CTRL_TUNED_CLK)
6287279e487SZiyuan Xu 				/* Tuning successfully */
6297279e487SZiyuan Xu 				return 0;
6307279e487SZiyuan Xu 			break;
6317279e487SZiyuan Xu 		}
6327279e487SZiyuan Xu 	}
6337279e487SZiyuan Xu 
6347279e487SZiyuan Xu 	return -ETIMEDOUT;
6357279e487SZiyuan Xu }
6367279e487SZiyuan Xu 
6377279e487SZiyuan Xu #ifdef CONFIG_DM_MMC
6387279e487SZiyuan Xu static int sdhci_execute_tuning(struct udevice *dev, u32 opcode)
6397279e487SZiyuan Xu {
6407279e487SZiyuan Xu 	struct mmc *mmc = mmc_get_mmc_dev(dev);
6417279e487SZiyuan Xu #else
6427279e487SZiyuan Xu static int sdhci_execute_tuning(struct mmc *mmc, u32 opcode)
6437279e487SZiyuan Xu {
6447279e487SZiyuan Xu #endif
6457279e487SZiyuan Xu 	struct sdhci_host *host = mmc->priv;
6467279e487SZiyuan Xu 	u16 ctrl;
6477279e487SZiyuan Xu 
6487279e487SZiyuan Xu 	/*
6497279e487SZiyuan Xu 	 * The Host Controller needs tuning in case of SDR104 and DDR50
6507279e487SZiyuan Xu 	 * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
6517279e487SZiyuan Xu 	 * the Capabilities register.
6527279e487SZiyuan Xu 	 * If the Host Controller supports the HS200 mode then the
6537279e487SZiyuan Xu 	 * tuning function has to be executed.
6547279e487SZiyuan Xu 	 */
6557279e487SZiyuan Xu 	switch (mmc->timing) {
6567279e487SZiyuan Xu 	/* HS400 tuning is done in HS200 mode */
6577279e487SZiyuan Xu 	case MMC_TIMING_MMC_HS400:
6587279e487SZiyuan Xu 		return -EINVAL;
6597279e487SZiyuan Xu 	case MMC_TIMING_MMC_HS200:
6607279e487SZiyuan Xu 		/*
6617279e487SZiyuan Xu 		 * Periodic re-tuning for HS400 is not expected to be needed, so
6627279e487SZiyuan Xu 		 * disable it here.
6637279e487SZiyuan Xu 		 */
6647279e487SZiyuan Xu 		break;
6657279e487SZiyuan Xu 	default:
6667279e487SZiyuan Xu 		return -EINVAL;
6677279e487SZiyuan Xu 	}
6687279e487SZiyuan Xu 
6697279e487SZiyuan Xu 	ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
6707279e487SZiyuan Xu 	ctrl |= SDHCI_CTRL_EXEC_TUNING;
6717279e487SZiyuan Xu 	sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
6727279e487SZiyuan Xu 
6737279e487SZiyuan Xu 	return __sdhci_execute_tuning(host, opcode);
6747279e487SZiyuan Xu }
6757279e487SZiyuan Xu 
676e7881d85SSimon Glass #ifdef CONFIG_DM_MMC
677ef1e4edaSSimon Glass int sdhci_probe(struct udevice *dev)
678ef1e4edaSSimon Glass {
679ef1e4edaSSimon Glass 	struct mmc *mmc = mmc_get_mmc_dev(dev);
680ab769f22SPantelis Antoniou 
681ef1e4edaSSimon Glass 	return sdhci_init(mmc);
682ef1e4edaSSimon Glass }
683ef1e4edaSSimon Glass 
684ef1e4edaSSimon Glass const struct dm_mmc_ops sdhci_ops = {
685bdd003c0SZiyuan Xu 	.card_busy	= sdhci_card_busy,
686ef1e4edaSSimon Glass 	.send_cmd	= sdhci_send_command,
687ef1e4edaSSimon Glass 	.set_ios	= sdhci_set_ios,
6887279e487SZiyuan Xu 	.execute_tuning = sdhci_execute_tuning,
689ef1e4edaSSimon Glass };
690ef1e4edaSSimon Glass #else
691ab769f22SPantelis Antoniou static const struct mmc_ops sdhci_ops = {
692bdd003c0SZiyuan Xu 	.card_busy	= sdhci_card_busy,
693ab769f22SPantelis Antoniou 	.send_cmd	= sdhci_send_command,
694ab769f22SPantelis Antoniou 	.set_ios	= sdhci_set_ios,
695ab769f22SPantelis Antoniou 	.init		= sdhci_init,
6967279e487SZiyuan Xu 	.execute_tuning = sdhci_execute_tuning,
697ab769f22SPantelis Antoniou };
698ef1e4edaSSimon Glass #endif
699ab769f22SPantelis Antoniou 
70014bed52dSJaehoon Chung int sdhci_setup_cfg(struct mmc_config *cfg, struct sdhci_host *host,
7016d0e34bfSStefan Herbrechtsmeier 		u32 f_max, u32 f_min)
7022a809093SSimon Glass {
7036dffdbc3SWenyou Yang 	u32 caps, caps_1;
70414bed52dSJaehoon Chung 
70514bed52dSJaehoon Chung 	caps = sdhci_readl(host, SDHCI_CAPABILITIES);
70615bd0995SMasahiro Yamada 
70745a68fe2SMasahiro Yamada #ifdef CONFIG_MMC_SDHCI_SDMA
70815bd0995SMasahiro Yamada 	if (!(caps & SDHCI_CAN_DO_SDMA)) {
70915bd0995SMasahiro Yamada 		printf("%s: Your controller doesn't support SDMA!!\n",
71015bd0995SMasahiro Yamada 		       __func__);
71115bd0995SMasahiro Yamada 		return -EINVAL;
71215bd0995SMasahiro Yamada 	}
71315bd0995SMasahiro Yamada #endif
714895549a2SJaehoon Chung 	if (host->quirks & SDHCI_QUIRK_REG32_RW)
715895549a2SJaehoon Chung 		host->version =
716895549a2SJaehoon Chung 			sdhci_readl(host, SDHCI_HOST_VERSION - 2) >> 16;
717895549a2SJaehoon Chung 	else
71814bed52dSJaehoon Chung 		host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
71914bed52dSJaehoon Chung 
72014bed52dSJaehoon Chung 	cfg->name = host->name;
721e7881d85SSimon Glass #ifndef CONFIG_DM_MMC
7222a809093SSimon Glass 	cfg->ops = &sdhci_ops;
7232a809093SSimon Glass #endif
7240e0dcc19SWenyou Yang 
7250e0dcc19SWenyou Yang 	/* Check whether the clock multiplier is supported or not */
7260e0dcc19SWenyou Yang 	if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) {
7270e0dcc19SWenyou Yang 		caps_1 = sdhci_readl(host, SDHCI_CAPABILITIES_1);
7280e0dcc19SWenyou Yang 		host->clk_mul = (caps_1 & SDHCI_CLOCK_MUL_MASK) >>
7290e0dcc19SWenyou Yang 				SDHCI_CLOCK_MUL_SHIFT;
7300e0dcc19SWenyou Yang 	}
7310e0dcc19SWenyou Yang 
7326d0e34bfSStefan Herbrechtsmeier 	if (host->max_clk == 0) {
73314bed52dSJaehoon Chung 		if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300)
7346d0e34bfSStefan Herbrechtsmeier 			host->max_clk = (caps & SDHCI_CLOCK_V3_BASE_MASK) >>
7352a809093SSimon Glass 				SDHCI_CLOCK_BASE_SHIFT;
7362a809093SSimon Glass 		else
7376d0e34bfSStefan Herbrechtsmeier 			host->max_clk = (caps & SDHCI_CLOCK_BASE_MASK) >>
7382a809093SSimon Glass 				SDHCI_CLOCK_BASE_SHIFT;
7396d0e34bfSStefan Herbrechtsmeier 		host->max_clk *= 1000000;
7400e0dcc19SWenyou Yang 		if (host->clk_mul)
7410e0dcc19SWenyou Yang 			host->max_clk *= host->clk_mul;
7422a809093SSimon Glass 	}
7436d0e34bfSStefan Herbrechtsmeier 	if (host->max_clk == 0) {
7446c67954cSMasahiro Yamada 		printf("%s: Hardware doesn't specify base clock frequency\n",
7456c67954cSMasahiro Yamada 		       __func__);
7462a809093SSimon Glass 		return -EINVAL;
7476c67954cSMasahiro Yamada 	}
7486d0e34bfSStefan Herbrechtsmeier 	if (f_max && (f_max < host->max_clk))
7496d0e34bfSStefan Herbrechtsmeier 		cfg->f_max = f_max;
7506d0e34bfSStefan Herbrechtsmeier 	else
7516d0e34bfSStefan Herbrechtsmeier 		cfg->f_max = host->max_clk;
7526d0e34bfSStefan Herbrechtsmeier 	if (f_min)
7536d0e34bfSStefan Herbrechtsmeier 		cfg->f_min = f_min;
7542a809093SSimon Glass 	else {
75514bed52dSJaehoon Chung 		if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300)
7562a809093SSimon Glass 			cfg->f_min = cfg->f_max / SDHCI_MAX_DIV_SPEC_300;
7572a809093SSimon Glass 		else
7582a809093SSimon Glass 			cfg->f_min = cfg->f_max / SDHCI_MAX_DIV_SPEC_200;
7592a809093SSimon Glass 	}
7602a809093SSimon Glass 	cfg->voltages = 0;
7612a809093SSimon Glass 	if (caps & SDHCI_CAN_VDD_330)
7622a809093SSimon Glass 		cfg->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
7632a809093SSimon Glass 	if (caps & SDHCI_CAN_VDD_300)
7642a809093SSimon Glass 		cfg->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
7652a809093SSimon Glass 	if (caps & SDHCI_CAN_VDD_180)
7662a809093SSimon Glass 		cfg->voltages |= MMC_VDD_165_195;
7672a809093SSimon Glass 
7683137e645SMasahiro Yamada 	if (host->quirks & SDHCI_QUIRK_BROKEN_VOLTAGE)
7693137e645SMasahiro Yamada 		cfg->voltages |= host->voltages;
7703137e645SMasahiro Yamada 
7712a809093SSimon Glass 	cfg->host_caps = MMC_MODE_HS | MMC_MODE_HS_52MHz | MMC_MODE_4BIT;
7723fd0a9baSJaehoon Chung 
7733fd0a9baSJaehoon Chung 	/* Since Host Controller Version3.0 */
77414bed52dSJaehoon Chung 	if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) {
775ecd7b246SJaehoon Chung 		if (!(caps & SDHCI_CAN_DO_8BIT))
776ecd7b246SJaehoon Chung 			cfg->host_caps &= ~MMC_MODE_8BIT;
7772a809093SSimon Glass 	}
7782a809093SSimon Glass 
77914bed52dSJaehoon Chung 	if (host->host_caps)
78014bed52dSJaehoon Chung 		cfg->host_caps |= host->host_caps;
7812a809093SSimon Glass 
7822a809093SSimon Glass 	cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
7832a809093SSimon Glass 
7842a809093SSimon Glass 	return 0;
7852a809093SSimon Glass }
7862a809093SSimon Glass 
787ef1e4edaSSimon Glass #ifdef CONFIG_BLK
788ef1e4edaSSimon Glass int sdhci_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg)
789ef1e4edaSSimon Glass {
790ef1e4edaSSimon Glass 	return mmc_bind(dev, mmc, cfg);
791ef1e4edaSSimon Glass }
792ef1e4edaSSimon Glass #else
7936d0e34bfSStefan Herbrechtsmeier int add_sdhci(struct sdhci_host *host, u32 f_max, u32 f_min)
794af62a557SLei Wen {
7956c67954cSMasahiro Yamada 	int ret;
7966c67954cSMasahiro Yamada 
7976d0e34bfSStefan Herbrechtsmeier 	ret = sdhci_setup_cfg(&host->cfg, host, f_max, f_min);
7986c67954cSMasahiro Yamada 	if (ret)
7996c67954cSMasahiro Yamada 		return ret;
800236bfecfSJaehoon Chung 
80193bfd616SPantelis Antoniou 	host->mmc = mmc_create(&host->cfg, host);
80293bfd616SPantelis Antoniou 	if (host->mmc == NULL) {
80393bfd616SPantelis Antoniou 		printf("%s: mmc create fail!\n", __func__);
8042cb5d67cSJaehoon Chung 		return -ENOMEM;
80593bfd616SPantelis Antoniou 	}
806af62a557SLei Wen 
807af62a557SLei Wen 	return 0;
808af62a557SLei Wen }
809ef1e4edaSSimon Glass #endif
810