xref: /rk3399_rockchip-uboot/drivers/mmc/sdhci.c (revision 1976213d42cdd94113a7872b166b083dccab52df)
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 
sdhci_reset(struct sdhci_host * host,u8 mask)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 
sdhci_cmd_done(struct sdhci_host * host,struct mmc_cmd * cmd)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 
sdhci_transfer_pio(struct sdhci_host * host,struct mmc_data * data)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 
sdhci_transfer_data(struct sdhci_host * host,struct mmc_data * data,unsigned int start_addr)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
sdhci_send_command(struct udevice * dev,struct mmc_cmd * cmd,struct mmc_data * data)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);
1776d216b78SYifeng Zhao 				sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
17856b34bc6SPrzemyslaw Marczak 			} else {
17956b34bc6SPrzemyslaw Marczak 				puts("timeout.\n");
1806d216b78SYifeng Zhao 				/* remove timeout return error and try to send command */
181f827f114SYifeng Zhao 				break;
182af62a557SLei Wen 			}
18356b34bc6SPrzemyslaw Marczak 		}
18456b34bc6SPrzemyslaw Marczak 		time++;
185af62a557SLei Wen 		udelay(1000);
186af62a557SLei Wen 	}
187af62a557SLei Wen 
188c060f28dSJorge Ramirez-Ortiz 	sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
189c060f28dSJorge Ramirez-Ortiz 
190af62a557SLei Wen 	mask = SDHCI_INT_RESPONSE;
191af62a557SLei Wen 	if (!(cmd->resp_type & MMC_RSP_PRESENT))
192af62a557SLei Wen 		flags = SDHCI_CMD_RESP_NONE;
193af62a557SLei Wen 	else if (cmd->resp_type & MMC_RSP_136)
194af62a557SLei Wen 		flags = SDHCI_CMD_RESP_LONG;
195af62a557SLei Wen 	else if (cmd->resp_type & MMC_RSP_BUSY) {
196af62a557SLei Wen 		flags = SDHCI_CMD_RESP_SHORT_BUSY;
19717ea3c86SJaehoon Chung 		if (data)
198af62a557SLei Wen 			mask |= SDHCI_INT_DATA_END;
199af62a557SLei Wen 	} else
200af62a557SLei Wen 		flags = SDHCI_CMD_RESP_SHORT;
201af62a557SLei Wen 
202af62a557SLei Wen 	if (cmd->resp_type & MMC_RSP_CRC)
203af62a557SLei Wen 		flags |= SDHCI_CMD_CRC;
204af62a557SLei Wen 	if (cmd->resp_type & MMC_RSP_OPCODE)
205af62a557SLei Wen 		flags |= SDHCI_CMD_INDEX;
206af62a557SLei Wen 	if (data)
207af62a557SLei Wen 		flags |= SDHCI_CMD_DATA;
208af62a557SLei Wen 
2097279e487SZiyuan Xu 	if (cmd->cmdidx == MMC_SEND_TUNING_BLOCK ||
2107279e487SZiyuan Xu 	    cmd->cmdidx == MMC_SEND_TUNING_BLOCK_HS200) {
2117279e487SZiyuan Xu 		mask &= ~SDHCI_INT_RESPONSE;
2127279e487SZiyuan Xu 		mask |= SDHCI_INT_DATA_AVAIL;
2137279e487SZiyuan Xu 		flags |= SDHCI_CMD_DATA;
2147279e487SZiyuan Xu 	}
2157279e487SZiyuan Xu 
216af62a557SLei Wen 	/* Set Transfer mode regarding to data flag */
217af62a557SLei Wen 	if (data != 0) {
218af62a557SLei Wen 		sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL);
219af62a557SLei Wen 		mode = SDHCI_TRNS_BLK_CNT_EN;
220af62a557SLei Wen 		trans_bytes = data->blocks * data->blocksize;
221af62a557SLei Wen 		if (data->blocks > 1)
222af62a557SLei Wen 			mode |= SDHCI_TRNS_MULTI;
223af62a557SLei Wen 
224af62a557SLei Wen 		if (data->flags == MMC_DATA_READ)
225af62a557SLei Wen 			mode |= SDHCI_TRNS_READ;
226af62a557SLei Wen 
22745a68fe2SMasahiro Yamada #ifdef CONFIG_MMC_SDHCI_SDMA
228af62a557SLei Wen 		if (data->flags == MMC_DATA_READ)
2293c1fcb77SRob Herring 			start_addr = (unsigned long)data->dest;
230af62a557SLei Wen 		else
2313c1fcb77SRob Herring 			start_addr = (unsigned long)data->src;
232af62a557SLei Wen 		if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
233af62a557SLei Wen 				(start_addr & 0x7) != 0x0) {
234af62a557SLei Wen 			is_aligned = 0;
2353c1fcb77SRob Herring 			start_addr = (unsigned long)aligned_buffer;
236af62a557SLei Wen 			if (data->flags != MMC_DATA_READ)
237af62a557SLei Wen 				memcpy(aligned_buffer, data->src, trans_bytes);
238af62a557SLei Wen 		}
239af62a557SLei Wen 
240492d3223SStefan Roese #if defined(CONFIG_FIXED_SDHCI_ALIGNED_BUFFER)
241492d3223SStefan Roese 		/*
242492d3223SStefan Roese 		 * Always use this bounce-buffer when
243492d3223SStefan Roese 		 * CONFIG_FIXED_SDHCI_ALIGNED_BUFFER is defined
244492d3223SStefan Roese 		 */
245492d3223SStefan Roese 		is_aligned = 0;
246492d3223SStefan Roese 		start_addr = (unsigned long)aligned_buffer;
247492d3223SStefan Roese 		if (data->flags != MMC_DATA_READ)
248492d3223SStefan Roese 			memcpy(aligned_buffer, data->src, trans_bytes);
249492d3223SStefan Roese #endif
250492d3223SStefan Roese 
251af62a557SLei Wen 		sdhci_writel(host, start_addr, SDHCI_DMA_ADDRESS);
252af62a557SLei Wen 		mode |= SDHCI_TRNS_DMA;
253af62a557SLei Wen #endif
254af62a557SLei Wen 		sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
255af62a557SLei Wen 				data->blocksize),
256af62a557SLei Wen 				SDHCI_BLOCK_SIZE);
257af62a557SLei Wen 		sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
258af62a557SLei Wen 		sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
2595e1c23cdSKevin Liu 	} else if (cmd->resp_type & MMC_RSP_BUSY) {
2605e1c23cdSKevin Liu 		sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL);
261af62a557SLei Wen 	}
262af62a557SLei Wen 
263af62a557SLei Wen 	sdhci_writel(host, cmd->cmdarg, SDHCI_ARGUMENT);
26445a68fe2SMasahiro Yamada #ifdef CONFIG_MMC_SDHCI_SDMA
265fa7720b2SKevin Liu 	if (data != 0) {
266be256cbfSJaehoon Chung 		trans_bytes = ALIGN(trans_bytes, CONFIG_SYS_CACHELINE_SIZE);
2672c2ec4c9SLei Wen 		flush_cache(start_addr, trans_bytes);
268fa7720b2SKevin Liu 	}
269af62a557SLei Wen #endif
270af62a557SLei Wen 	sdhci_writew(host, SDHCI_MAKE_CMD(cmd->cmdidx, flags), SDHCI_COMMAND);
27129905a45SStefan Roese 	start = get_timer(0);
272af62a557SLei Wen 	do {
273af62a557SLei Wen 		stat = sdhci_readl(host, SDHCI_INT_STATUS);
274af62a557SLei Wen 		if (stat & SDHCI_INT_ERROR)
275af62a557SLei Wen 			break;
276af62a557SLei Wen 
277d90bb439SSteve Rae 		if (get_timer(start) >= SDHCI_READ_STATUS_TIMEOUT) {
278bae4a1fdSMasahiro Yamada 			if (host->quirks & SDHCI_QUIRK_BROKEN_R1B) {
2793a638320SJaehoon Chung 				return 0;
280bae4a1fdSMasahiro Yamada 			} else {
281bae4a1fdSMasahiro Yamada 				printf("%s: Timeout for status update!\n",
282bae4a1fdSMasahiro Yamada 				       __func__);
283915ffa52SJaehoon Chung 				return -ETIMEDOUT;
2843a638320SJaehoon Chung 			}
2853a638320SJaehoon Chung 		}
286bae4a1fdSMasahiro Yamada 	} while ((stat & mask) != mask);
2873a638320SJaehoon Chung 
288af62a557SLei Wen 	if ((stat & (SDHCI_INT_ERROR | mask)) == mask) {
289af62a557SLei Wen 		sdhci_cmd_done(host, cmd);
290af62a557SLei Wen 		sdhci_writel(host, mask, SDHCI_INT_STATUS);
291af62a557SLei Wen 	} else
292af62a557SLei Wen 		ret = -1;
293af62a557SLei Wen 
294af62a557SLei Wen 	if (!ret && data)
295af62a557SLei Wen 		ret = sdhci_transfer_data(host, data, start_addr);
296af62a557SLei Wen 
29713243f2eSTushar Behera 	if (host->quirks & SDHCI_QUIRK_WAIT_SEND_CMD)
29813243f2eSTushar Behera 		udelay(1000);
29913243f2eSTushar Behera 
300af62a557SLei Wen 	stat = sdhci_readl(host, SDHCI_INT_STATUS);
301af62a557SLei Wen 	sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
302af62a557SLei Wen 	if (!ret) {
303af62a557SLei Wen 		if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
304af62a557SLei Wen 				!is_aligned && (data->flags == MMC_DATA_READ))
305af62a557SLei Wen 			memcpy(data->dest, aligned_buffer, trans_bytes);
306af62a557SLei Wen 		return 0;
307af62a557SLei Wen 	}
308af62a557SLei Wen 
309af62a557SLei Wen 	sdhci_reset(host, SDHCI_RESET_CMD);
310af62a557SLei Wen 	sdhci_reset(host, SDHCI_RESET_DATA);
311af62a557SLei Wen 	if (stat & SDHCI_INT_TIMEOUT)
312915ffa52SJaehoon Chung 		return -ETIMEDOUT;
313af62a557SLei Wen 	else
314915ffa52SJaehoon Chung 		return -ECOMM;
315af62a557SLei Wen }
316af62a557SLei Wen 
317*1976213dSYifeng Zhao void sdhci_enable_clk(struct sdhci_host *host, u16 clk)
318*1976213dSYifeng Zhao {
319*1976213dSYifeng Zhao 	unsigned int timeout;
320*1976213dSYifeng Zhao 
321*1976213dSYifeng Zhao 	clk |= SDHCI_CLOCK_INT_EN;
322*1976213dSYifeng Zhao 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
323*1976213dSYifeng Zhao 
324*1976213dSYifeng Zhao 	/* Wait max 20 ms */
325*1976213dSYifeng Zhao 	timeout = 20;
326*1976213dSYifeng Zhao 	while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
327*1976213dSYifeng Zhao 		& SDHCI_CLOCK_INT_STABLE)) {
328*1976213dSYifeng Zhao 		if (timeout == 0) {
329*1976213dSYifeng Zhao 			printf("%s: Internal clock never stabilised.\n",
330*1976213dSYifeng Zhao 			       __func__);
331*1976213dSYifeng Zhao 			return;
332*1976213dSYifeng Zhao 		}
333*1976213dSYifeng Zhao 		timeout--;
334*1976213dSYifeng Zhao 		udelay(1000);
335*1976213dSYifeng Zhao 	}
336*1976213dSYifeng Zhao 	clk |= SDHCI_CLOCK_CARD_EN;
337*1976213dSYifeng Zhao 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
338*1976213dSYifeng Zhao }
339*1976213dSYifeng Zhao 
340a15c58b2SZiyuan Xu int sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
341af62a557SLei Wen {
342899fb9e3SStefan Roese 	unsigned int div, clk = 0, timeout;
343af62a557SLei Wen 
34479667b7bSWenyou Yang 	/* Wait max 20 ms */
34579667b7bSWenyou Yang 	timeout = 200;
34679667b7bSWenyou Yang 	while (sdhci_readl(host, SDHCI_PRESENT_STATE) &
34779667b7bSWenyou Yang 			   (SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT)) {
34879667b7bSWenyou Yang 		if (timeout == 0) {
34979667b7bSWenyou Yang 			printf("%s: Timeout to wait cmd & data inhibit\n",
35079667b7bSWenyou Yang 			       __func__);
3512cb5d67cSJaehoon Chung 			return -EBUSY;
35279667b7bSWenyou Yang 		}
35379667b7bSWenyou Yang 
35479667b7bSWenyou Yang 		timeout--;
35579667b7bSWenyou Yang 		udelay(100);
35679667b7bSWenyou Yang 	}
357899fb9e3SStefan Roese 	sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
358af62a557SLei Wen 
359af62a557SLei Wen 	if (clock == 0)
360af62a557SLei Wen 		return 0;
361113e5dfcSJaehoon Chung 	if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) {
3626dffdbc3SWenyou Yang 		/*
3636dffdbc3SWenyou Yang 		 * Check if the Host Controller supports Programmable Clock
3646dffdbc3SWenyou Yang 		 * Mode.
3656dffdbc3SWenyou Yang 		 */
3666dffdbc3SWenyou Yang 		if (host->clk_mul) {
3676dffdbc3SWenyou Yang 			for (div = 1; div <= 1024; div++) {
3680e0dcc19SWenyou Yang 				if ((host->max_clk / div) <= clock)
3696dffdbc3SWenyou Yang 					break;
3706dffdbc3SWenyou Yang 			}
3716dffdbc3SWenyou Yang 
3726dffdbc3SWenyou Yang 			/*
3736dffdbc3SWenyou Yang 			 * Set Programmable Clock Mode in the Clock
3746dffdbc3SWenyou Yang 			 * Control register.
3756dffdbc3SWenyou Yang 			 */
3766dffdbc3SWenyou Yang 			clk = SDHCI_PROG_CLOCK_MODE;
3776dffdbc3SWenyou Yang 			div--;
3786dffdbc3SWenyou Yang 		} else {
379af62a557SLei Wen 			/* Version 3.00 divisors must be a multiple of 2. */
3806d0e34bfSStefan Herbrechtsmeier 			if (host->max_clk <= clock) {
381af62a557SLei Wen 				div = 1;
3826dffdbc3SWenyou Yang 			} else {
3836dffdbc3SWenyou Yang 				for (div = 2;
3846dffdbc3SWenyou Yang 				     div < SDHCI_MAX_DIV_SPEC_300;
3856dffdbc3SWenyou Yang 				     div += 2) {
3866d0e34bfSStefan Herbrechtsmeier 					if ((host->max_clk / div) <= clock)
387af62a557SLei Wen 						break;
388af62a557SLei Wen 				}
389af62a557SLei Wen 			}
3906dffdbc3SWenyou Yang 			div >>= 1;
3916dffdbc3SWenyou Yang 		}
392af62a557SLei Wen 	} else {
393af62a557SLei Wen 		/* Version 2.00 divisors must be a power of 2. */
394af62a557SLei Wen 		for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
3956d0e34bfSStefan Herbrechtsmeier 			if ((host->max_clk / div) <= clock)
396af62a557SLei Wen 				break;
397af62a557SLei Wen 		}
398af62a557SLei Wen 		div >>= 1;
3996dffdbc3SWenyou Yang 	}
4005de82122SZiyuan Xu 	if (host->ops && host->ops->set_clock_ext)
4015de82122SZiyuan Xu 		host->ops->set_clock_ext(host, div);
402b09ed6e4SJaehoon Chung 
4036dffdbc3SWenyou Yang 	clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
404af62a557SLei Wen 	clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
405af62a557SLei Wen 		<< SDHCI_DIVIDER_HI_SHIFT;
406af62a557SLei Wen 
407*1976213dSYifeng Zhao 	sdhci_enable_clk(host, clk);
40831044c33SZiyuan Xu 
40931044c33SZiyuan Xu 	host->clock = clock;
410af62a557SLei Wen 	return 0;
411af62a557SLei Wen }
412af62a557SLei Wen 
413af62a557SLei Wen static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
414af62a557SLei Wen {
415af62a557SLei Wen 	u8 pwr = 0;
416af62a557SLei Wen 
417af62a557SLei Wen 	if (power != (unsigned short)-1) {
418af62a557SLei Wen 		switch (1 << power) {
419af62a557SLei Wen 		case MMC_VDD_165_195:
420af62a557SLei Wen 			pwr = SDHCI_POWER_180;
421af62a557SLei Wen 			break;
422af62a557SLei Wen 		case MMC_VDD_29_30:
423af62a557SLei Wen 		case MMC_VDD_30_31:
424af62a557SLei Wen 			pwr = SDHCI_POWER_300;
425af62a557SLei Wen 			break;
426af62a557SLei Wen 		case MMC_VDD_32_33:
427af62a557SLei Wen 		case MMC_VDD_33_34:
428af62a557SLei Wen 			pwr = SDHCI_POWER_330;
429af62a557SLei Wen 			break;
430af62a557SLei Wen 		}
431af62a557SLei Wen 	}
432af62a557SLei Wen 
433af62a557SLei Wen 	if (pwr == 0) {
434af62a557SLei Wen 		sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
435af62a557SLei Wen 		return;
436af62a557SLei Wen 	}
437af62a557SLei Wen 
438af62a557SLei Wen 	pwr |= SDHCI_POWER_ON;
439af62a557SLei Wen 
440af62a557SLei Wen 	sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
441af62a557SLei Wen }
442af62a557SLei Wen 
44376194d8cSZiyuan Xu static void sdhci_set_uhs_signaling(struct sdhci_host *host)
44476194d8cSZiyuan Xu {
44576194d8cSZiyuan Xu 	u16 ctrl_2;
44676194d8cSZiyuan Xu 	u32 timing = host->mmc->timing;
44776194d8cSZiyuan Xu 
44876194d8cSZiyuan Xu 	ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
44976194d8cSZiyuan Xu 	/* Select Bus Speed Mode for host */
45076194d8cSZiyuan Xu 	ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
45176194d8cSZiyuan Xu 
45276194d8cSZiyuan Xu 	if ((timing != MMC_TIMING_LEGACY) &&
45376194d8cSZiyuan Xu 	    (timing != MMC_TIMING_MMC_HS) &&
45476194d8cSZiyuan Xu 	    (timing != MMC_TIMING_SD_HS))
45576194d8cSZiyuan Xu 		ctrl_2 |= SDHCI_CTRL_VDD_180;
45676194d8cSZiyuan Xu 
45776194d8cSZiyuan Xu 	if ((timing == MMC_TIMING_MMC_HS200) ||
45876194d8cSZiyuan Xu 	    (timing == MMC_TIMING_UHS_SDR104))
45976194d8cSZiyuan Xu 		ctrl_2 |= SDHCI_CTRL_UHS_SDR104 | SDHCI_CTRL_DRV_TYPE_A;
46076194d8cSZiyuan Xu 	else if (timing == MMC_TIMING_UHS_SDR12)
46176194d8cSZiyuan Xu 		ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
46276194d8cSZiyuan Xu 	else if (timing == MMC_TIMING_UHS_SDR25)
46376194d8cSZiyuan Xu 		ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
464850fcf3eSchenfen 	else if ((timing == MMC_TIMING_UHS_SDR50) ||
465850fcf3eSchenfen 		(timing == MMC_TIMING_MMC_HS))
46676194d8cSZiyuan Xu 		ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
46776194d8cSZiyuan Xu 	else if ((timing == MMC_TIMING_UHS_DDR50) ||
46876194d8cSZiyuan Xu 		 (timing == MMC_TIMING_MMC_DDR52))
46976194d8cSZiyuan Xu 		ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
47076194d8cSZiyuan Xu 	else if (timing == MMC_TIMING_MMC_HS400 ||
47176194d8cSZiyuan Xu 		 timing == MMC_TIMING_MMC_HS400ES)
47276194d8cSZiyuan Xu 		ctrl_2 |= SDHCI_CTRL_HS400 | SDHCI_CTRL_DRV_TYPE_A;
47376194d8cSZiyuan Xu 
47476194d8cSZiyuan Xu 	sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
47576194d8cSZiyuan Xu }
47676194d8cSZiyuan Xu 
477e7881d85SSimon Glass #ifdef CONFIG_DM_MMC
478bdd003c0SZiyuan Xu static bool sdhci_card_busy(struct udevice *dev)
479bdd003c0SZiyuan Xu {
480bdd003c0SZiyuan Xu 	struct mmc *mmc = mmc_get_mmc_dev(dev);
481bdd003c0SZiyuan Xu #else
482bdd003c0SZiyuan Xu static bool sdhci_card_busy(struct mmc *mmc)
483bdd003c0SZiyuan Xu {
484bdd003c0SZiyuan Xu #endif
485bdd003c0SZiyuan Xu 	struct sdhci_host *host = mmc->priv;
486bdd003c0SZiyuan Xu 	u32 present_state;
487bdd003c0SZiyuan Xu 
488bdd003c0SZiyuan Xu 	/* Check whether DAT[0] is 0 */
489bdd003c0SZiyuan Xu 	present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
490bdd003c0SZiyuan Xu 
491bdd003c0SZiyuan Xu 	return !(present_state & SDHCI_DATA_0_LVL);
492bdd003c0SZiyuan Xu }
493bdd003c0SZiyuan Xu 
494bdd003c0SZiyuan Xu #ifdef CONFIG_DM_MMC
495ef1e4edaSSimon Glass static int sdhci_set_ios(struct udevice *dev)
496ef1e4edaSSimon Glass {
497ef1e4edaSSimon Glass 	struct mmc *mmc = mmc_get_mmc_dev(dev);
498ef1e4edaSSimon Glass #else
49907b0b9c0SJaehoon Chung static int sdhci_set_ios(struct mmc *mmc)
500af62a557SLei Wen {
501ef1e4edaSSimon Glass #endif
502af62a557SLei Wen 	u32 ctrl;
50393bfd616SPantelis Antoniou 	struct sdhci_host *host = mmc->priv;
504af62a557SLei Wen 
505bf9c4d14SMasahiro Yamada 	if (host->ops && host->ops->set_control_reg)
50662226b68SJaehoon Chung 		host->ops->set_control_reg(host);
507236bfecfSJaehoon Chung 
508a15c58b2SZiyuan Xu 	if (mmc->clock != host->clock) {
509a15c58b2SZiyuan Xu 		if (host->ops && host->ops->set_clock)
510a15c58b2SZiyuan Xu 			host->ops->set_clock(host, mmc->clock);
511a15c58b2SZiyuan Xu 		else
512a15c58b2SZiyuan Xu 			sdhci_set_clock(host, mmc->clock);
513a15c58b2SZiyuan Xu 	}
514af62a557SLei Wen 
515af62a557SLei Wen 	/* Set bus width */
516af62a557SLei Wen 	ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
517af62a557SLei Wen 	if (mmc->bus_width == 8) {
518af62a557SLei Wen 		ctrl &= ~SDHCI_CTRL_4BITBUS;
519113e5dfcSJaehoon Chung 		if ((SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) ||
520113e5dfcSJaehoon Chung 				(host->quirks & SDHCI_QUIRK_USE_WIDE8))
521af62a557SLei Wen 			ctrl |= SDHCI_CTRL_8BITBUS;
522af62a557SLei Wen 	} else {
523f88a429fSMatt Reimer 		if ((SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) ||
524f88a429fSMatt Reimer 				(host->quirks & SDHCI_QUIRK_USE_WIDE8))
525af62a557SLei Wen 			ctrl &= ~SDHCI_CTRL_8BITBUS;
526af62a557SLei Wen 		if (mmc->bus_width == 4)
527af62a557SLei Wen 			ctrl |= SDHCI_CTRL_4BITBUS;
528af62a557SLei Wen 		else
529af62a557SLei Wen 			ctrl &= ~SDHCI_CTRL_4BITBUS;
530af62a557SLei Wen 	}
531af62a557SLei Wen 
5329f83e5c6SZiyuan Xu 	if (!(mmc->timing == MMC_TIMING_LEGACY) &&
5339f83e5c6SZiyuan Xu 	    !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
534af62a557SLei Wen 		ctrl |= SDHCI_CTRL_HISPD;
535af62a557SLei Wen 	else
536af62a557SLei Wen 		ctrl &= ~SDHCI_CTRL_HISPD;
537af62a557SLei Wen 
538af62a557SLei Wen 	sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
53907b0b9c0SJaehoon Chung 
54076194d8cSZiyuan Xu 	if ((mmc->timing != MMC_TIMING_LEGACY) &&
54176194d8cSZiyuan Xu 	    (mmc->timing != MMC_TIMING_MMC_HS) &&
54276194d8cSZiyuan Xu 	    (mmc->timing != MMC_TIMING_SD_HS))
54376194d8cSZiyuan Xu 		sdhci_set_power(host, MMC_VDD_165_195_SHIFT);
54476194d8cSZiyuan Xu 
54576194d8cSZiyuan Xu 	sdhci_set_uhs_signaling(host);
54676194d8cSZiyuan Xu 
547210841c6SStefan Roese 	/* If available, call the driver specific "post" set_ios() function */
548210841c6SStefan Roese 	if (host->ops && host->ops->set_ios_post)
549210841c6SStefan Roese 		host->ops->set_ios_post(host);
550210841c6SStefan Roese 
551ef1e4edaSSimon Glass 	return 0;
552af62a557SLei Wen }
553af62a557SLei Wen 
5546588c78bSJeroen Hofstee static int sdhci_init(struct mmc *mmc)
555af62a557SLei Wen {
55693bfd616SPantelis Antoniou 	struct sdhci_host *host = mmc->priv;
557af62a557SLei Wen 
5588d549b61SMasahiro Yamada 	sdhci_reset(host, SDHCI_RESET_ALL);
5598d549b61SMasahiro Yamada 
560af62a557SLei Wen 	if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) && !aligned_buffer) {
561af62a557SLei Wen 		aligned_buffer = memalign(8, 512*1024);
562af62a557SLei Wen 		if (!aligned_buffer) {
56330e6d979SDarwin Rambo 			printf("%s: Aligned buffer alloc failed!!!\n",
56430e6d979SDarwin Rambo 			       __func__);
5652cb5d67cSJaehoon Chung 			return -ENOMEM;
566af62a557SLei Wen 		}
567af62a557SLei Wen 	}
568af62a557SLei Wen 
56993bfd616SPantelis Antoniou 	sdhci_set_power(host, fls(mmc->cfg->voltages) - 1);
570470dcc75SJoe Hershberger 
571bf9c4d14SMasahiro Yamada 	if (host->ops && host->ops->get_cd)
5725e96217fSJaehoon Chung 		host->ops->get_cd(host);
573470dcc75SJoe Hershberger 
574ce0c1bc1SŁukasz Majewski 	/* Enable only interrupts served by the SD controller */
57530e6d979SDarwin Rambo 	sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK,
57630e6d979SDarwin Rambo 		     SDHCI_INT_ENABLE);
577ce0c1bc1SŁukasz Majewski 	/* Mask all sdhci interrupt sources */
578ce0c1bc1SŁukasz Majewski 	sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE);
579af62a557SLei Wen 
580af62a557SLei Wen 	return 0;
581af62a557SLei Wen }
582af62a557SLei Wen 
5837279e487SZiyuan Xu static int sdhci_send_tuning(struct sdhci_host *host, u32 opcode)
5847279e487SZiyuan Xu {
5857279e487SZiyuan Xu 	struct mmc_cmd cmd;
5867279e487SZiyuan Xu 
5877279e487SZiyuan Xu 	cmd.cmdidx = opcode;
5887279e487SZiyuan Xu 	cmd.resp_type = MMC_RSP_R1;
5897279e487SZiyuan Xu 	cmd.cmdarg = 0;
5907279e487SZiyuan Xu 	/*
5917279e487SZiyuan Xu 	 * In response to CMD19, the card sends 64 bytes of tuning
5927279e487SZiyuan Xu 	 * block to the Host Controller. So we set the block size
5937279e487SZiyuan Xu 	 * to 64 here.
5947279e487SZiyuan Xu 	 */
5957279e487SZiyuan Xu 	if (opcode == MMC_SEND_TUNING_BLOCK_HS200 &&
5967279e487SZiyuan Xu 	    host->mmc->bus_width == MMC_BUS_WIDTH_8BIT)
5977279e487SZiyuan Xu 		sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128), SDHCI_BLOCK_SIZE);
5987279e487SZiyuan Xu 	else
5997279e487SZiyuan Xu 		sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64), SDHCI_BLOCK_SIZE);
6007279e487SZiyuan Xu 
6017279e487SZiyuan Xu 	/*
6027279e487SZiyuan Xu 	 * The tuning block is sent by the card to the host controller.
6037279e487SZiyuan Xu 	 * So we set the TRNS_READ bit in the Transfer Mode register.
6047279e487SZiyuan Xu 	 * This also takes care of setting DMA Enable and Multi Block
6057279e487SZiyuan Xu 	 * Select in the same register to 0.
6067279e487SZiyuan Xu 	 */
6077279e487SZiyuan Xu 	sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
6087279e487SZiyuan Xu 
6097279e487SZiyuan Xu #ifdef CONFIG_DM_MMC
6107279e487SZiyuan Xu 	return sdhci_send_command(host->mmc->dev, &cmd, NULL);
6117279e487SZiyuan Xu #else
6127279e487SZiyuan Xu 	return sdhci_send_command(host->mmc, &cmd, NULL);
6137279e487SZiyuan Xu #endif
6147279e487SZiyuan Xu }
6157279e487SZiyuan Xu 
6167279e487SZiyuan Xu #define MAX_TUNING_LOOP 40
6177279e487SZiyuan Xu static int __sdhci_execute_tuning(struct sdhci_host *host, u32 opcode)
6187279e487SZiyuan Xu {
6197279e487SZiyuan Xu 	int i;
6207279e487SZiyuan Xu 	int ret;
6217279e487SZiyuan Xu 
6227279e487SZiyuan Xu 	/*
6237279e487SZiyuan Xu 	 * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
6247279e487SZiyuan Xu 	 * of loops reaches 40 times.
6257279e487SZiyuan Xu 	 */
6267279e487SZiyuan Xu 	for (i = 0; i < MAX_TUNING_LOOP; i++) {
6277279e487SZiyuan Xu 		u16 ctrl;
6287279e487SZiyuan Xu 
6297279e487SZiyuan Xu 		ret = sdhci_send_tuning(host, opcode);
6307279e487SZiyuan Xu 
6317279e487SZiyuan Xu 		if (ret)
6327279e487SZiyuan Xu 			return ret;
6337279e487SZiyuan Xu 
6347279e487SZiyuan Xu 		ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
6357279e487SZiyuan Xu 		if (!(ctrl & SDHCI_CTRL_EXEC_TUNING)) {
6367279e487SZiyuan Xu 			if (ctrl & SDHCI_CTRL_TUNED_CLK)
6377279e487SZiyuan Xu 				/* Tuning successfully */
6387279e487SZiyuan Xu 				return 0;
6397279e487SZiyuan Xu 			break;
6407279e487SZiyuan Xu 		}
6417279e487SZiyuan Xu 	}
6427279e487SZiyuan Xu 
6437279e487SZiyuan Xu 	return -ETIMEDOUT;
6447279e487SZiyuan Xu }
6457279e487SZiyuan Xu 
6467279e487SZiyuan Xu #ifdef CONFIG_DM_MMC
6477279e487SZiyuan Xu static int sdhci_execute_tuning(struct udevice *dev, u32 opcode)
6487279e487SZiyuan Xu {
6497279e487SZiyuan Xu 	struct mmc *mmc = mmc_get_mmc_dev(dev);
6507279e487SZiyuan Xu #else
6517279e487SZiyuan Xu static int sdhci_execute_tuning(struct mmc *mmc, u32 opcode)
6527279e487SZiyuan Xu {
6537279e487SZiyuan Xu #endif
6547279e487SZiyuan Xu 	struct sdhci_host *host = mmc->priv;
6557279e487SZiyuan Xu 	u16 ctrl;
6567279e487SZiyuan Xu 
6577279e487SZiyuan Xu 	/*
6587279e487SZiyuan Xu 	 * The Host Controller needs tuning in case of SDR104 and DDR50
6597279e487SZiyuan Xu 	 * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
6607279e487SZiyuan Xu 	 * the Capabilities register.
6617279e487SZiyuan Xu 	 * If the Host Controller supports the HS200 mode then the
6627279e487SZiyuan Xu 	 * tuning function has to be executed.
6637279e487SZiyuan Xu 	 */
6647279e487SZiyuan Xu 	switch (mmc->timing) {
6657279e487SZiyuan Xu 	/* HS400 tuning is done in HS200 mode */
6667279e487SZiyuan Xu 	case MMC_TIMING_MMC_HS400:
6677279e487SZiyuan Xu 		return -EINVAL;
6687279e487SZiyuan Xu 	case MMC_TIMING_MMC_HS200:
6697279e487SZiyuan Xu 		/*
6707279e487SZiyuan Xu 		 * Periodic re-tuning for HS400 is not expected to be needed, so
6717279e487SZiyuan Xu 		 * disable it here.
6727279e487SZiyuan Xu 		 */
6737279e487SZiyuan Xu 		break;
6747279e487SZiyuan Xu 	default:
6757279e487SZiyuan Xu 		return -EINVAL;
6767279e487SZiyuan Xu 	}
6777279e487SZiyuan Xu 
6787279e487SZiyuan Xu 	ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
6797279e487SZiyuan Xu 	ctrl |= SDHCI_CTRL_EXEC_TUNING;
6807279e487SZiyuan Xu 	sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
6817279e487SZiyuan Xu 
682fb43afc4SYifeng Zhao 	return __sdhci_execute_tuning(host, opcode);
6837279e487SZiyuan Xu }
6847279e487SZiyuan Xu 
685e7881d85SSimon Glass #ifdef CONFIG_DM_MMC
686ef1e4edaSSimon Glass int sdhci_probe(struct udevice *dev)
687ef1e4edaSSimon Glass {
688ef1e4edaSSimon Glass 	struct mmc *mmc = mmc_get_mmc_dev(dev);
689ab769f22SPantelis Antoniou 
690ef1e4edaSSimon Glass 	return sdhci_init(mmc);
691ef1e4edaSSimon Glass }
692ef1e4edaSSimon Glass 
69313669fc5SYifeng Zhao static int sdhci_set_enhanced_strobe(struct udevice *dev)
69413669fc5SYifeng Zhao {
69513669fc5SYifeng Zhao 	struct mmc *mmc = mmc_get_mmc_dev(dev);
69613669fc5SYifeng Zhao 	struct sdhci_host *host = mmc->priv;
69713669fc5SYifeng Zhao 
69813669fc5SYifeng Zhao 	if (host->ops && host->ops->set_enhanced_strobe)
69913669fc5SYifeng Zhao 		return host->ops->set_enhanced_strobe(host);
70013669fc5SYifeng Zhao 
70113669fc5SYifeng Zhao 	return -ENOTSUPP;
70213669fc5SYifeng Zhao }
70313669fc5SYifeng Zhao 
704ef1e4edaSSimon Glass const struct dm_mmc_ops sdhci_ops = {
705bdd003c0SZiyuan Xu 	.card_busy	= sdhci_card_busy,
706ef1e4edaSSimon Glass 	.send_cmd	= sdhci_send_command,
707ef1e4edaSSimon Glass 	.set_ios	= sdhci_set_ios,
7087279e487SZiyuan Xu 	.execute_tuning = sdhci_execute_tuning,
70913669fc5SYifeng Zhao 	.set_enhanced_strobe = sdhci_set_enhanced_strobe,
710ef1e4edaSSimon Glass };
711ef1e4edaSSimon Glass #else
712ab769f22SPantelis Antoniou static const struct mmc_ops sdhci_ops = {
713bdd003c0SZiyuan Xu 	.card_busy	= sdhci_card_busy,
714ab769f22SPantelis Antoniou 	.send_cmd	= sdhci_send_command,
715ab769f22SPantelis Antoniou 	.set_ios	= sdhci_set_ios,
716ab769f22SPantelis Antoniou 	.init		= sdhci_init,
7177279e487SZiyuan Xu 	.execute_tuning = sdhci_execute_tuning,
718ab769f22SPantelis Antoniou };
719ef1e4edaSSimon Glass #endif
720ab769f22SPantelis Antoniou 
72114bed52dSJaehoon Chung int sdhci_setup_cfg(struct mmc_config *cfg, struct sdhci_host *host,
7226d0e34bfSStefan Herbrechtsmeier 		u32 f_max, u32 f_min)
7232a809093SSimon Glass {
7246dffdbc3SWenyou Yang 	u32 caps, caps_1;
72514bed52dSJaehoon Chung 
72614bed52dSJaehoon Chung 	caps = sdhci_readl(host, SDHCI_CAPABILITIES);
72715bd0995SMasahiro Yamada 
72845a68fe2SMasahiro Yamada #ifdef CONFIG_MMC_SDHCI_SDMA
72915bd0995SMasahiro Yamada 	if (!(caps & SDHCI_CAN_DO_SDMA)) {
73015bd0995SMasahiro Yamada 		printf("%s: Your controller doesn't support SDMA!!\n",
73115bd0995SMasahiro Yamada 		       __func__);
73215bd0995SMasahiro Yamada 		return -EINVAL;
73315bd0995SMasahiro Yamada 	}
73415bd0995SMasahiro Yamada #endif
735895549a2SJaehoon Chung 	if (host->quirks & SDHCI_QUIRK_REG32_RW)
736895549a2SJaehoon Chung 		host->version =
737895549a2SJaehoon Chung 			sdhci_readl(host, SDHCI_HOST_VERSION - 2) >> 16;
738895549a2SJaehoon Chung 	else
73914bed52dSJaehoon Chung 		host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
74014bed52dSJaehoon Chung 
74114bed52dSJaehoon Chung 	cfg->name = host->name;
742e7881d85SSimon Glass #ifndef CONFIG_DM_MMC
7432a809093SSimon Glass 	cfg->ops = &sdhci_ops;
7442a809093SSimon Glass #endif
7450e0dcc19SWenyou Yang 
7460e0dcc19SWenyou Yang 	/* Check whether the clock multiplier is supported or not */
7470e0dcc19SWenyou Yang 	if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) {
7480e0dcc19SWenyou Yang 		caps_1 = sdhci_readl(host, SDHCI_CAPABILITIES_1);
7490e0dcc19SWenyou Yang 		host->clk_mul = (caps_1 & SDHCI_CLOCK_MUL_MASK) >>
7500e0dcc19SWenyou Yang 				SDHCI_CLOCK_MUL_SHIFT;
7510e0dcc19SWenyou Yang 	}
7520e0dcc19SWenyou Yang 
7536d0e34bfSStefan Herbrechtsmeier 	if (host->max_clk == 0) {
75414bed52dSJaehoon Chung 		if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300)
7556d0e34bfSStefan Herbrechtsmeier 			host->max_clk = (caps & SDHCI_CLOCK_V3_BASE_MASK) >>
7562a809093SSimon Glass 				SDHCI_CLOCK_BASE_SHIFT;
7572a809093SSimon Glass 		else
7586d0e34bfSStefan Herbrechtsmeier 			host->max_clk = (caps & SDHCI_CLOCK_BASE_MASK) >>
7592a809093SSimon Glass 				SDHCI_CLOCK_BASE_SHIFT;
7606d0e34bfSStefan Herbrechtsmeier 		host->max_clk *= 1000000;
7610e0dcc19SWenyou Yang 		if (host->clk_mul)
7620e0dcc19SWenyou Yang 			host->max_clk *= host->clk_mul;
7632a809093SSimon Glass 	}
7646d0e34bfSStefan Herbrechtsmeier 	if (host->max_clk == 0) {
7656c67954cSMasahiro Yamada 		printf("%s: Hardware doesn't specify base clock frequency\n",
7666c67954cSMasahiro Yamada 		       __func__);
7672a809093SSimon Glass 		return -EINVAL;
7686c67954cSMasahiro Yamada 	}
7696d0e34bfSStefan Herbrechtsmeier 	if (f_max && (f_max < host->max_clk))
7706d0e34bfSStefan Herbrechtsmeier 		cfg->f_max = f_max;
7716d0e34bfSStefan Herbrechtsmeier 	else
7726d0e34bfSStefan Herbrechtsmeier 		cfg->f_max = host->max_clk;
7736d0e34bfSStefan Herbrechtsmeier 	if (f_min)
7746d0e34bfSStefan Herbrechtsmeier 		cfg->f_min = f_min;
7752a809093SSimon Glass 	else {
77614bed52dSJaehoon Chung 		if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300)
7772a809093SSimon Glass 			cfg->f_min = cfg->f_max / SDHCI_MAX_DIV_SPEC_300;
7782a809093SSimon Glass 		else
7792a809093SSimon Glass 			cfg->f_min = cfg->f_max / SDHCI_MAX_DIV_SPEC_200;
7802a809093SSimon Glass 	}
7812a809093SSimon Glass 	cfg->voltages = 0;
7822a809093SSimon Glass 	if (caps & SDHCI_CAN_VDD_330)
7832a809093SSimon Glass 		cfg->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
7842a809093SSimon Glass 	if (caps & SDHCI_CAN_VDD_300)
7852a809093SSimon Glass 		cfg->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
7862a809093SSimon Glass 	if (caps & SDHCI_CAN_VDD_180)
7872a809093SSimon Glass 		cfg->voltages |= MMC_VDD_165_195;
7882a809093SSimon Glass 
7893137e645SMasahiro Yamada 	if (host->quirks & SDHCI_QUIRK_BROKEN_VOLTAGE)
7903137e645SMasahiro Yamada 		cfg->voltages |= host->voltages;
7913137e645SMasahiro Yamada 
7922a809093SSimon Glass 	cfg->host_caps = MMC_MODE_HS | MMC_MODE_HS_52MHz | MMC_MODE_4BIT;
7933fd0a9baSJaehoon Chung 
7943fd0a9baSJaehoon Chung 	/* Since Host Controller Version3.0 */
79514bed52dSJaehoon Chung 	if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) {
796ecd7b246SJaehoon Chung 		if (!(caps & SDHCI_CAN_DO_8BIT))
797ecd7b246SJaehoon Chung 			cfg->host_caps &= ~MMC_MODE_8BIT;
7982a809093SSimon Glass 	}
7992a809093SSimon Glass 
80014bed52dSJaehoon Chung 	if (host->host_caps)
80114bed52dSJaehoon Chung 		cfg->host_caps |= host->host_caps;
8022a809093SSimon Glass 
8032a809093SSimon Glass 	cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
8042a809093SSimon Glass 
8052a809093SSimon Glass 	return 0;
8062a809093SSimon Glass }
8072a809093SSimon Glass 
808ef1e4edaSSimon Glass #ifdef CONFIG_BLK
809ef1e4edaSSimon Glass int sdhci_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg)
810ef1e4edaSSimon Glass {
811ef1e4edaSSimon Glass 	return mmc_bind(dev, mmc, cfg);
812ef1e4edaSSimon Glass }
813ef1e4edaSSimon Glass #else
8146d0e34bfSStefan Herbrechtsmeier int add_sdhci(struct sdhci_host *host, u32 f_max, u32 f_min)
815af62a557SLei Wen {
8166c67954cSMasahiro Yamada 	int ret;
8176c67954cSMasahiro Yamada 
8186d0e34bfSStefan Herbrechtsmeier 	ret = sdhci_setup_cfg(&host->cfg, host, f_max, f_min);
8196c67954cSMasahiro Yamada 	if (ret)
8206c67954cSMasahiro Yamada 		return ret;
821236bfecfSJaehoon Chung 
82293bfd616SPantelis Antoniou 	host->mmc = mmc_create(&host->cfg, host);
82393bfd616SPantelis Antoniou 	if (host->mmc == NULL) {
82493bfd616SPantelis Antoniou 		printf("%s: mmc create fail!\n", __func__);
8252cb5d67cSJaehoon Chung 		return -ENOMEM;
82693bfd616SPantelis Antoniou 	}
827af62a557SLei Wen 
828af62a557SLei Wen 	return 0;
829af62a557SLei Wen }
830ef1e4edaSSimon Glass #endif
831