xref: /openbmc/u-boot/drivers/mmc/sdhci.c (revision fd6d0541958c9d9b325edf7c558e0e99ac9221fa)
183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2af62a557SLei Wen /*
3af62a557SLei Wen  * Copyright 2011, Marvell Semiconductor Inc.
4af62a557SLei Wen  * Lei Wen <leiwen@marvell.com>
5af62a557SLei Wen  *
6af62a557SLei Wen  * Back ported to the 8xx platform (from the 8260 platform) by
7af62a557SLei Wen  * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
8af62a557SLei Wen  */
9af62a557SLei Wen 
10af62a557SLei Wen #include <common.h>
112a809093SSimon Glass #include <errno.h>
12af62a557SLei Wen #include <malloc.h>
13af62a557SLei Wen #include <mmc.h>
14af62a557SLei Wen #include <sdhci.h>
15af62a557SLei Wen 
16492d3223SStefan Roese #if defined(CONFIG_FIXED_SDHCI_ALIGNED_BUFFER)
17492d3223SStefan Roese void *aligned_buffer = (void *)CONFIG_FIXED_SDHCI_ALIGNED_BUFFER;
18492d3223SStefan Roese #else
19af62a557SLei Wen void *aligned_buffer;
20492d3223SStefan Roese #endif
21af62a557SLei Wen 
sdhci_reset(struct sdhci_host * host,u8 mask)22af62a557SLei Wen static void sdhci_reset(struct sdhci_host *host, u8 mask)
23af62a557SLei Wen {
24af62a557SLei Wen 	unsigned long timeout;
25af62a557SLei Wen 
26af62a557SLei Wen 	/* Wait max 100 ms */
27af62a557SLei Wen 	timeout = 100;
28af62a557SLei Wen 	sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
29af62a557SLei Wen 	while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
30af62a557SLei Wen 		if (timeout == 0) {
3130e6d979SDarwin Rambo 			printf("%s: Reset 0x%x never completed.\n",
3230e6d979SDarwin Rambo 			       __func__, (int)mask);
33af62a557SLei Wen 			return;
34af62a557SLei Wen 		}
35af62a557SLei Wen 		timeout--;
36af62a557SLei Wen 		udelay(1000);
37af62a557SLei Wen 	}
38af62a557SLei Wen }
39af62a557SLei Wen 
sdhci_cmd_done(struct sdhci_host * host,struct mmc_cmd * cmd)40af62a557SLei Wen static void sdhci_cmd_done(struct sdhci_host *host, struct mmc_cmd *cmd)
41af62a557SLei Wen {
42af62a557SLei Wen 	int i;
43af62a557SLei Wen 	if (cmd->resp_type & MMC_RSP_136) {
44af62a557SLei Wen 		/* CRC is stripped so we need to do some shifting. */
45af62a557SLei Wen 		for (i = 0; i < 4; i++) {
46af62a557SLei Wen 			cmd->response[i] = sdhci_readl(host,
47af62a557SLei Wen 					SDHCI_RESPONSE + (3-i)*4) << 8;
48af62a557SLei Wen 			if (i != 3)
49af62a557SLei Wen 				cmd->response[i] |= sdhci_readb(host,
50af62a557SLei Wen 						SDHCI_RESPONSE + (3-i)*4-1);
51af62a557SLei Wen 		}
52af62a557SLei Wen 	} else {
53af62a557SLei Wen 		cmd->response[0] = sdhci_readl(host, SDHCI_RESPONSE);
54af62a557SLei Wen 	}
55af62a557SLei Wen }
56af62a557SLei Wen 
sdhci_transfer_pio(struct sdhci_host * host,struct mmc_data * data)57af62a557SLei Wen static void sdhci_transfer_pio(struct sdhci_host *host, struct mmc_data *data)
58af62a557SLei Wen {
59af62a557SLei Wen 	int i;
60af62a557SLei Wen 	char *offs;
61af62a557SLei Wen 	for (i = 0; i < data->blocksize; i += 4) {
62af62a557SLei Wen 		offs = data->dest + i;
63af62a557SLei Wen 		if (data->flags == MMC_DATA_READ)
64af62a557SLei Wen 			*(u32 *)offs = sdhci_readl(host, SDHCI_BUFFER);
65af62a557SLei Wen 		else
66af62a557SLei Wen 			sdhci_writel(host, *(u32 *)offs, SDHCI_BUFFER);
67af62a557SLei Wen 	}
68af62a557SLei Wen }
69af62a557SLei Wen 
sdhci_transfer_data(struct sdhci_host * host,struct mmc_data * data,unsigned int start_addr)70af62a557SLei Wen static int sdhci_transfer_data(struct sdhci_host *host, struct mmc_data *data,
71af62a557SLei Wen 				unsigned int start_addr)
72af62a557SLei Wen {
73a004abdeSLei Wen 	unsigned int stat, rdy, mask, timeout, block = 0;
747dde50d7SAlex Deymo 	bool transfer_done = false;
7545a68fe2SMasahiro Yamada #ifdef CONFIG_MMC_SDHCI_SDMA
76804c7f42SJaehoon Chung 	unsigned char ctrl;
772c011847SJuhyun \(Justin\) Oh 	ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
78804c7f42SJaehoon Chung 	ctrl &= ~SDHCI_CTRL_DMA_MASK;
792c011847SJuhyun \(Justin\) Oh 	sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
80804c7f42SJaehoon Chung #endif
81af62a557SLei Wen 
825d48e422SJaehoon Chung 	timeout = 1000000;
83af62a557SLei Wen 	rdy = SDHCI_INT_SPACE_AVAIL | SDHCI_INT_DATA_AVAIL;
84af62a557SLei Wen 	mask = SDHCI_DATA_AVAILABLE | SDHCI_SPACE_AVAILABLE;
85af62a557SLei Wen 	do {
86af62a557SLei Wen 		stat = sdhci_readl(host, SDHCI_INT_STATUS);
87af62a557SLei Wen 		if (stat & SDHCI_INT_ERROR) {
8861f2e5eeSMasahiro Yamada 			pr_debug("%s: Error detected in status(0x%X)!\n",
8930e6d979SDarwin Rambo 				 __func__, stat);
902cb5d67cSJaehoon Chung 			return -EIO;
91af62a557SLei Wen 		}
927dde50d7SAlex Deymo 		if (!transfer_done && (stat & rdy)) {
93af62a557SLei Wen 			if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & mask))
94af62a557SLei Wen 				continue;
95af62a557SLei Wen 			sdhci_writel(host, rdy, SDHCI_INT_STATUS);
96af62a557SLei Wen 			sdhci_transfer_pio(host, data);
97af62a557SLei Wen 			data->dest += data->blocksize;
987dde50d7SAlex Deymo 			if (++block >= data->blocks) {
997dde50d7SAlex Deymo 				/* Keep looping until the SDHCI_INT_DATA_END is
1007dde50d7SAlex Deymo 				 * cleared, even if we finished sending all the
1017dde50d7SAlex Deymo 				 * blocks.
1027dde50d7SAlex Deymo 				 */
1037dde50d7SAlex Deymo 				transfer_done = true;
1047dde50d7SAlex Deymo 				continue;
1057dde50d7SAlex Deymo 			}
106af62a557SLei Wen 		}
10745a68fe2SMasahiro Yamada #ifdef CONFIG_MMC_SDHCI_SDMA
1087dde50d7SAlex Deymo 		if (!transfer_done && (stat & SDHCI_INT_DMA_END)) {
109af62a557SLei Wen 			sdhci_writel(host, SDHCI_INT_DMA_END, SDHCI_INT_STATUS);
1103e81c772SLei Wen 			start_addr &= ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1);
111af62a557SLei Wen 			start_addr += SDHCI_DEFAULT_BOUNDARY_SIZE;
112af62a557SLei Wen 			sdhci_writel(host, start_addr, SDHCI_DMA_ADDRESS);
113af62a557SLei Wen 		}
114af62a557SLei Wen #endif
115a004abdeSLei Wen 		if (timeout-- > 0)
116a004abdeSLei Wen 			udelay(10);
117a004abdeSLei Wen 		else {
11830e6d979SDarwin Rambo 			printf("%s: Transfer data timeout\n", __func__);
1192cb5d67cSJaehoon Chung 			return -ETIMEDOUT;
120a004abdeSLei Wen 		}
121af62a557SLei Wen 	} while (!(stat & SDHCI_INT_DATA_END));
122af62a557SLei Wen 	return 0;
123af62a557SLei Wen }
124af62a557SLei Wen 
12556b34bc6SPrzemyslaw Marczak /*
12656b34bc6SPrzemyslaw Marczak  * No command will be sent by driver if card is busy, so driver must wait
12756b34bc6SPrzemyslaw Marczak  * for card ready state.
12856b34bc6SPrzemyslaw Marczak  * Every time when card is busy after timeout then (last) timeout value will be
12956b34bc6SPrzemyslaw Marczak  * increased twice but only if it doesn't exceed global defined maximum.
13065a25b20SMasahiro Yamada  * Each function call will use last timeout value.
13156b34bc6SPrzemyslaw Marczak  */
13265a25b20SMasahiro Yamada #define SDHCI_CMD_MAX_TIMEOUT			3200
133d8ce77b2SMasahiro Yamada #define SDHCI_CMD_DEFAULT_TIMEOUT		100
134d90bb439SSteve Rae #define SDHCI_READ_STATUS_TIMEOUT		1000
13556b34bc6SPrzemyslaw Marczak 
136e7881d85SSimon Glass #ifdef CONFIG_DM_MMC
sdhci_send_command(struct udevice * dev,struct mmc_cmd * cmd,struct mmc_data * data)137ef1e4edaSSimon Glass static int sdhci_send_command(struct udevice *dev, struct mmc_cmd *cmd,
138ef1e4edaSSimon Glass 			      struct mmc_data *data)
139ef1e4edaSSimon Glass {
140ef1e4edaSSimon Glass 	struct mmc *mmc = mmc_get_mmc_dev(dev);
141ef1e4edaSSimon Glass 
142ef1e4edaSSimon Glass #else
1436588c78bSJeroen Hofstee static int sdhci_send_command(struct mmc *mmc, struct mmc_cmd *cmd,
144af62a557SLei Wen 			      struct mmc_data *data)
145af62a557SLei Wen {
146ef1e4edaSSimon Glass #endif
14793bfd616SPantelis Antoniou 	struct sdhci_host *host = mmc->priv;
148af62a557SLei Wen 	unsigned int stat = 0;
149af62a557SLei Wen 	int ret = 0;
150af62a557SLei Wen 	int trans_bytes = 0, is_aligned = 1;
151af62a557SLei Wen 	u32 mask, flags, mode;
15256b34bc6SPrzemyslaw Marczak 	unsigned int time = 0, start_addr = 0;
15319d2e342SSimon Glass 	int mmc_dev = mmc_get_blk_desc(mmc)->devnum;
15436332b6eSVipul Kumar 	ulong start = get_timer(0);
155af62a557SLei Wen 
15656b34bc6SPrzemyslaw Marczak 	/* Timeout unit - ms */
157d8ce77b2SMasahiro Yamada 	static unsigned int cmd_timeout = SDHCI_CMD_DEFAULT_TIMEOUT;
158af62a557SLei Wen 
159af62a557SLei Wen 	mask = SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT;
160af62a557SLei Wen 
161af62a557SLei Wen 	/* We shouldn't wait for data inihibit for stop commands, even
162af62a557SLei Wen 	   though they might use busy signaling */
163b88a7a4cSSiva Durga Prasad Paladugu 	if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION ||
1641a7414f6SSiva Durga Prasad Paladugu 	    ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK ||
1651a7414f6SSiva Durga Prasad Paladugu 	      cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200) && !data))
166af62a557SLei Wen 		mask &= ~SDHCI_DATA_INHIBIT;
167af62a557SLei Wen 
168af62a557SLei Wen 	while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
16956b34bc6SPrzemyslaw Marczak 		if (time >= cmd_timeout) {
17030e6d979SDarwin Rambo 			printf("%s: MMC: %d busy ", __func__, mmc_dev);
17165a25b20SMasahiro Yamada 			if (2 * cmd_timeout <= SDHCI_CMD_MAX_TIMEOUT) {
17256b34bc6SPrzemyslaw Marczak 				cmd_timeout += cmd_timeout;
17356b34bc6SPrzemyslaw Marczak 				printf("timeout increasing to: %u ms.\n",
17456b34bc6SPrzemyslaw Marczak 				       cmd_timeout);
17556b34bc6SPrzemyslaw Marczak 			} else {
17656b34bc6SPrzemyslaw Marczak 				puts("timeout.\n");
177915ffa52SJaehoon Chung 				return -ECOMM;
178af62a557SLei Wen 			}
17956b34bc6SPrzemyslaw Marczak 		}
18056b34bc6SPrzemyslaw Marczak 		time++;
181af62a557SLei Wen 		udelay(1000);
182af62a557SLei Wen 	}
183af62a557SLei Wen 
184713e6815SJorge Ramirez-Ortiz 	sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
185713e6815SJorge Ramirez-Ortiz 
186af62a557SLei Wen 	mask = SDHCI_INT_RESPONSE;
1871a7414f6SSiva Durga Prasad Paladugu 	if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK ||
1881a7414f6SSiva Durga Prasad Paladugu 	     cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200) && !data)
189b88a7a4cSSiva Durga Prasad Paladugu 		mask = SDHCI_INT_DATA_AVAIL;
190b88a7a4cSSiva Durga Prasad Paladugu 
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;
206434f9d45SSiva Durga Prasad Paladugu 	if (data || cmd->cmdidx ==  MMC_CMD_SEND_TUNING_BLOCK ||
207434f9d45SSiva Durga Prasad Paladugu 	    cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)
208af62a557SLei Wen 		flags |= SDHCI_CMD_DATA;
209af62a557SLei Wen 
210af62a557SLei Wen 	/* Set Transfer mode regarding to data flag */
211bb7b4ef3SHeinrich Schuchardt 	if (data) {
212af62a557SLei Wen 		sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL);
213af62a557SLei Wen 		mode = SDHCI_TRNS_BLK_CNT_EN;
214af62a557SLei Wen 		trans_bytes = data->blocks * data->blocksize;
215af62a557SLei Wen 		if (data->blocks > 1)
216af62a557SLei Wen 			mode |= SDHCI_TRNS_MULTI;
217af62a557SLei Wen 
218af62a557SLei Wen 		if (data->flags == MMC_DATA_READ)
219af62a557SLei Wen 			mode |= SDHCI_TRNS_READ;
220af62a557SLei Wen 
22145a68fe2SMasahiro Yamada #ifdef CONFIG_MMC_SDHCI_SDMA
222af62a557SLei Wen 		if (data->flags == MMC_DATA_READ)
2233c1fcb77SRob Herring 			start_addr = (unsigned long)data->dest;
224af62a557SLei Wen 		else
2253c1fcb77SRob Herring 			start_addr = (unsigned long)data->src;
226af62a557SLei Wen 		if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
227af62a557SLei Wen 				(start_addr & 0x7) != 0x0) {
228af62a557SLei Wen 			is_aligned = 0;
2293c1fcb77SRob Herring 			start_addr = (unsigned long)aligned_buffer;
230af62a557SLei Wen 			if (data->flags != MMC_DATA_READ)
231af62a557SLei Wen 				memcpy(aligned_buffer, data->src, trans_bytes);
232af62a557SLei Wen 		}
233af62a557SLei Wen 
234492d3223SStefan Roese #if defined(CONFIG_FIXED_SDHCI_ALIGNED_BUFFER)
235492d3223SStefan Roese 		/*
236492d3223SStefan Roese 		 * Always use this bounce-buffer when
237492d3223SStefan Roese 		 * CONFIG_FIXED_SDHCI_ALIGNED_BUFFER is defined
238492d3223SStefan Roese 		 */
239492d3223SStefan Roese 		is_aligned = 0;
240492d3223SStefan Roese 		start_addr = (unsigned long)aligned_buffer;
241492d3223SStefan Roese 		if (data->flags != MMC_DATA_READ)
242492d3223SStefan Roese 			memcpy(aligned_buffer, data->src, trans_bytes);
243492d3223SStefan Roese #endif
244492d3223SStefan Roese 
245af62a557SLei Wen 		sdhci_writel(host, start_addr, SDHCI_DMA_ADDRESS);
246af62a557SLei Wen 		mode |= SDHCI_TRNS_DMA;
247af62a557SLei Wen #endif
248af62a557SLei Wen 		sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
249af62a557SLei Wen 				data->blocksize),
250af62a557SLei Wen 				SDHCI_BLOCK_SIZE);
251af62a557SLei Wen 		sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
252af62a557SLei Wen 		sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
2535e1c23cdSKevin Liu 	} else if (cmd->resp_type & MMC_RSP_BUSY) {
2545e1c23cdSKevin Liu 		sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL);
255af62a557SLei Wen 	}
256af62a557SLei Wen 
257af62a557SLei Wen 	sdhci_writel(host, cmd->cmdarg, SDHCI_ARGUMENT);
25845a68fe2SMasahiro Yamada #ifdef CONFIG_MMC_SDHCI_SDMA
259bb7b4ef3SHeinrich Schuchardt 	if (data) {
260be256cbfSJaehoon Chung 		trans_bytes = ALIGN(trans_bytes, CONFIG_SYS_CACHELINE_SIZE);
2612c2ec4c9SLei Wen 		flush_cache(start_addr, trans_bytes);
262fa7720b2SKevin Liu 	}
263af62a557SLei Wen #endif
264af62a557SLei Wen 	sdhci_writew(host, SDHCI_MAKE_CMD(cmd->cmdidx, flags), SDHCI_COMMAND);
26529905a45SStefan Roese 	start = get_timer(0);
266af62a557SLei Wen 	do {
267af62a557SLei Wen 		stat = sdhci_readl(host, SDHCI_INT_STATUS);
268af62a557SLei Wen 		if (stat & SDHCI_INT_ERROR)
269af62a557SLei Wen 			break;
270af62a557SLei Wen 
271d90bb439SSteve Rae 		if (get_timer(start) >= SDHCI_READ_STATUS_TIMEOUT) {
272bae4a1fdSMasahiro Yamada 			if (host->quirks & SDHCI_QUIRK_BROKEN_R1B) {
2733a638320SJaehoon Chung 				return 0;
274bae4a1fdSMasahiro Yamada 			} else {
275bae4a1fdSMasahiro Yamada 				printf("%s: Timeout for status update!\n",
276bae4a1fdSMasahiro Yamada 				       __func__);
277915ffa52SJaehoon Chung 				return -ETIMEDOUT;
2783a638320SJaehoon Chung 			}
2793a638320SJaehoon Chung 		}
280bae4a1fdSMasahiro Yamada 	} while ((stat & mask) != mask);
2813a638320SJaehoon Chung 
282af62a557SLei Wen 	if ((stat & (SDHCI_INT_ERROR | mask)) == mask) {
283af62a557SLei Wen 		sdhci_cmd_done(host, cmd);
284af62a557SLei Wen 		sdhci_writel(host, mask, SDHCI_INT_STATUS);
285af62a557SLei Wen 	} else
286af62a557SLei Wen 		ret = -1;
287af62a557SLei Wen 
288af62a557SLei Wen 	if (!ret && data)
289af62a557SLei Wen 		ret = sdhci_transfer_data(host, data, start_addr);
290af62a557SLei Wen 
29113243f2eSTushar Behera 	if (host->quirks & SDHCI_QUIRK_WAIT_SEND_CMD)
29213243f2eSTushar Behera 		udelay(1000);
29313243f2eSTushar Behera 
294af62a557SLei Wen 	stat = sdhci_readl(host, SDHCI_INT_STATUS);
295af62a557SLei Wen 	sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
296af62a557SLei Wen 	if (!ret) {
297af62a557SLei Wen 		if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
298af62a557SLei Wen 				!is_aligned && (data->flags == MMC_DATA_READ))
299af62a557SLei Wen 			memcpy(data->dest, aligned_buffer, trans_bytes);
300af62a557SLei Wen 		return 0;
301af62a557SLei Wen 	}
302af62a557SLei Wen 
303af62a557SLei Wen 	sdhci_reset(host, SDHCI_RESET_CMD);
304af62a557SLei Wen 	sdhci_reset(host, SDHCI_RESET_DATA);
305af62a557SLei Wen 	if (stat & SDHCI_INT_TIMEOUT)
306915ffa52SJaehoon Chung 		return -ETIMEDOUT;
307af62a557SLei Wen 	else
308915ffa52SJaehoon Chung 		return -ECOMM;
309af62a557SLei Wen }
310af62a557SLei Wen 
311ca992e82SSiva Durga Prasad Paladugu #if defined(CONFIG_DM_MMC) && defined(MMC_SUPPORTS_TUNING)
312ca992e82SSiva Durga Prasad Paladugu static int sdhci_execute_tuning(struct udevice *dev, uint opcode)
313ca992e82SSiva Durga Prasad Paladugu {
314ca992e82SSiva Durga Prasad Paladugu 	int err;
315ca992e82SSiva Durga Prasad Paladugu 	struct mmc *mmc = mmc_get_mmc_dev(dev);
316ca992e82SSiva Durga Prasad Paladugu 	struct sdhci_host *host = mmc->priv;
317ca992e82SSiva Durga Prasad Paladugu 
318ca992e82SSiva Durga Prasad Paladugu 	debug("%s\n", __func__);
319ca992e82SSiva Durga Prasad Paladugu 
320b70fe965SRamon Fried 	if (host->ops && host->ops->platform_execute_tuning) {
321ca992e82SSiva Durga Prasad Paladugu 		err = host->ops->platform_execute_tuning(mmc, opcode);
322ca992e82SSiva Durga Prasad Paladugu 		if (err)
323ca992e82SSiva Durga Prasad Paladugu 			return err;
324ca992e82SSiva Durga Prasad Paladugu 		return 0;
325ca992e82SSiva Durga Prasad Paladugu 	}
326ca992e82SSiva Durga Prasad Paladugu 	return 0;
327ca992e82SSiva Durga Prasad Paladugu }
328ca992e82SSiva Durga Prasad Paladugu #endif
329af62a557SLei Wen static int sdhci_set_clock(struct mmc *mmc, unsigned int clock)
330af62a557SLei Wen {
33193bfd616SPantelis Antoniou 	struct sdhci_host *host = mmc->priv;
332899fb9e3SStefan Roese 	unsigned int div, clk = 0, timeout;
333af62a557SLei Wen 
33479667b7bSWenyou Yang 	/* Wait max 20 ms */
33579667b7bSWenyou Yang 	timeout = 200;
33679667b7bSWenyou Yang 	while (sdhci_readl(host, SDHCI_PRESENT_STATE) &
33779667b7bSWenyou Yang 			   (SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT)) {
33879667b7bSWenyou Yang 		if (timeout == 0) {
33979667b7bSWenyou Yang 			printf("%s: Timeout to wait cmd & data inhibit\n",
34079667b7bSWenyou Yang 			       __func__);
3412cb5d67cSJaehoon Chung 			return -EBUSY;
34279667b7bSWenyou Yang 		}
34379667b7bSWenyou Yang 
34479667b7bSWenyou Yang 		timeout--;
34579667b7bSWenyou Yang 		udelay(100);
34679667b7bSWenyou Yang 	}
34779667b7bSWenyou Yang 
348899fb9e3SStefan Roese 	sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
349af62a557SLei Wen 
350af62a557SLei Wen 	if (clock == 0)
351af62a557SLei Wen 		return 0;
352af62a557SLei Wen 
353b70fe965SRamon Fried 	if (host->ops && host->ops->set_delay)
354ca992e82SSiva Durga Prasad Paladugu 		host->ops->set_delay(host);
355ca992e82SSiva Durga Prasad Paladugu 
356113e5dfcSJaehoon Chung 	if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) {
3576dffdbc3SWenyou Yang 		/*
3586dffdbc3SWenyou Yang 		 * Check if the Host Controller supports Programmable Clock
3596dffdbc3SWenyou Yang 		 * Mode.
3606dffdbc3SWenyou Yang 		 */
3616dffdbc3SWenyou Yang 		if (host->clk_mul) {
3626dffdbc3SWenyou Yang 			for (div = 1; div <= 1024; div++) {
3630e0dcc19SWenyou Yang 				if ((host->max_clk / div) <= clock)
3646dffdbc3SWenyou Yang 					break;
3656dffdbc3SWenyou Yang 			}
3666dffdbc3SWenyou Yang 
3676dffdbc3SWenyou Yang 			/*
3686dffdbc3SWenyou Yang 			 * Set Programmable Clock Mode in the Clock
3696dffdbc3SWenyou Yang 			 * Control register.
3706dffdbc3SWenyou Yang 			 */
3716dffdbc3SWenyou Yang 			clk = SDHCI_PROG_CLOCK_MODE;
3726dffdbc3SWenyou Yang 			div--;
3736dffdbc3SWenyou Yang 		} else {
374af62a557SLei Wen 			/* Version 3.00 divisors must be a multiple of 2. */
3756d0e34bfSStefan Herbrechtsmeier 			if (host->max_clk <= clock) {
376af62a557SLei Wen 				div = 1;
3776dffdbc3SWenyou Yang 			} else {
3786dffdbc3SWenyou Yang 				for (div = 2;
3796dffdbc3SWenyou Yang 				     div < SDHCI_MAX_DIV_SPEC_300;
3806dffdbc3SWenyou Yang 				     div += 2) {
3816d0e34bfSStefan Herbrechtsmeier 					if ((host->max_clk / div) <= clock)
382af62a557SLei Wen 						break;
383af62a557SLei Wen 				}
384af62a557SLei Wen 			}
3856dffdbc3SWenyou Yang 			div >>= 1;
3866dffdbc3SWenyou Yang 		}
387af62a557SLei Wen 	} else {
388af62a557SLei Wen 		/* Version 2.00 divisors must be a power of 2. */
389af62a557SLei Wen 		for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
3906d0e34bfSStefan Herbrechtsmeier 			if ((host->max_clk / div) <= clock)
391af62a557SLei Wen 				break;
392af62a557SLei Wen 		}
393af62a557SLei Wen 		div >>= 1;
3946dffdbc3SWenyou Yang 	}
395af62a557SLei Wen 
396bf9c4d14SMasahiro Yamada 	if (host->ops && host->ops->set_clock)
39762226b68SJaehoon Chung 		host->ops->set_clock(host, div);
398b09ed6e4SJaehoon Chung 
3996dffdbc3SWenyou Yang 	clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
400af62a557SLei Wen 	clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
401af62a557SLei Wen 		<< SDHCI_DIVIDER_HI_SHIFT;
402af62a557SLei Wen 	clk |= SDHCI_CLOCK_INT_EN;
403af62a557SLei Wen 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
404af62a557SLei Wen 
405af62a557SLei Wen 	/* Wait max 20 ms */
406af62a557SLei Wen 	timeout = 20;
407af62a557SLei Wen 	while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
408af62a557SLei Wen 		& SDHCI_CLOCK_INT_STABLE)) {
409af62a557SLei Wen 		if (timeout == 0) {
41030e6d979SDarwin Rambo 			printf("%s: Internal clock never stabilised.\n",
41130e6d979SDarwin Rambo 			       __func__);
4122cb5d67cSJaehoon Chung 			return -EBUSY;
413af62a557SLei Wen 		}
414af62a557SLei Wen 		timeout--;
415af62a557SLei Wen 		udelay(1000);
416af62a557SLei Wen 	}
417af62a557SLei Wen 
418af62a557SLei Wen 	clk |= SDHCI_CLOCK_CARD_EN;
419af62a557SLei Wen 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
420af62a557SLei Wen 	return 0;
421af62a557SLei Wen }
422af62a557SLei Wen 
423af62a557SLei Wen static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
424af62a557SLei Wen {
425af62a557SLei Wen 	u8 pwr = 0;
426af62a557SLei Wen 
427af62a557SLei Wen 	if (power != (unsigned short)-1) {
428af62a557SLei Wen 		switch (1 << power) {
429af62a557SLei Wen 		case MMC_VDD_165_195:
430af62a557SLei Wen 			pwr = SDHCI_POWER_180;
431af62a557SLei Wen 			break;
432af62a557SLei Wen 		case MMC_VDD_29_30:
433af62a557SLei Wen 		case MMC_VDD_30_31:
434af62a557SLei Wen 			pwr = SDHCI_POWER_300;
435af62a557SLei Wen 			break;
436af62a557SLei Wen 		case MMC_VDD_32_33:
437af62a557SLei Wen 		case MMC_VDD_33_34:
438af62a557SLei Wen 			pwr = SDHCI_POWER_330;
439af62a557SLei Wen 			break;
440af62a557SLei Wen 		}
441af62a557SLei Wen 	}
442af62a557SLei Wen 
443af62a557SLei Wen 	if (pwr == 0) {
444af62a557SLei Wen 		sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
445af62a557SLei Wen 		return;
446af62a557SLei Wen 	}
447af62a557SLei Wen 
448af62a557SLei Wen 	pwr |= SDHCI_POWER_ON;
449af62a557SLei Wen 
450af62a557SLei Wen 	sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
451af62a557SLei Wen }
452af62a557SLei Wen 
453e7881d85SSimon Glass #ifdef CONFIG_DM_MMC
454ef1e4edaSSimon Glass static int sdhci_set_ios(struct udevice *dev)
455ef1e4edaSSimon Glass {
456ef1e4edaSSimon Glass 	struct mmc *mmc = mmc_get_mmc_dev(dev);
457ef1e4edaSSimon Glass #else
45807b0b9c0SJaehoon Chung static int sdhci_set_ios(struct mmc *mmc)
459af62a557SLei Wen {
460ef1e4edaSSimon Glass #endif
461af62a557SLei Wen 	u32 ctrl;
4620a93863fSryan_chen 	u32 gen_addr, gen_ctrl;
463*730fd353SChin-Ting Kuo 	u16 ctrl_2;
46493bfd616SPantelis Antoniou 	struct sdhci_host *host = mmc->priv;
465af62a557SLei Wen 
466bf9c4d14SMasahiro Yamada 	if (host->ops && host->ops->set_control_reg)
46762226b68SJaehoon Chung 		host->ops->set_control_reg(host);
468236bfecfSJaehoon Chung 
469af62a557SLei Wen 	if (mmc->clock != host->clock)
470af62a557SLei Wen 		sdhci_set_clock(mmc, mmc->clock);
471af62a557SLei Wen 
4722a2d7efeSSiva Durga Prasad Paladugu 	if (mmc->clk_disable)
4732a2d7efeSSiva Durga Prasad Paladugu 		sdhci_set_clock(mmc, 0);
4742a2d7efeSSiva Durga Prasad Paladugu 
4750a93863fSryan_chen #ifdef CONFIG_MMC_SDHCI_ASPEED
4760a93863fSryan_chen 	/* Set bus width */
4770a93863fSryan_chen 	ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
4780a93863fSryan_chen 	gen_addr = (u32)host->ioaddr;
4790a93863fSryan_chen 	gen_addr &= ~0x300;
4800a93863fSryan_chen 	gen_ctrl = readl(gen_addr);
4810a93863fSryan_chen 	if (mmc->bus_width == 8) {
4820a93863fSryan_chen 		if((u32)host->ioaddr & 0x100)
4830a93863fSryan_chen 			writel(gen_ctrl | BIT(24), gen_addr);
4840a93863fSryan_chen 		else
4850a93863fSryan_chen 			writel(gen_ctrl | BIT(25), gen_addr);
4860a93863fSryan_chen 	} else {
4870a93863fSryan_chen 		writel(gen_ctrl & ~(BIT(24) | BIT(25)), gen_addr);
4880a93863fSryan_chen 		if (mmc->bus_width == 4)
4890a93863fSryan_chen 			ctrl |= SDHCI_CTRL_4BITBUS;
4900a93863fSryan_chen 		else
4910a93863fSryan_chen 			ctrl &= ~SDHCI_CTRL_4BITBUS;
4920a93863fSryan_chen 	}
4930a93863fSryan_chen #else
494af62a557SLei Wen 	/* Set bus width */
495af62a557SLei Wen 	ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
496af62a557SLei Wen 	if (mmc->bus_width == 8) {
497af62a557SLei Wen 		ctrl &= ~SDHCI_CTRL_4BITBUS;
498113e5dfcSJaehoon Chung 		if ((SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) ||
499113e5dfcSJaehoon Chung 				(host->quirks & SDHCI_QUIRK_USE_WIDE8))
500af62a557SLei Wen 			ctrl |= SDHCI_CTRL_8BITBUS;
501af62a557SLei Wen 	} else {
502f88a429fSMatt Reimer 		if ((SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) ||
503f88a429fSMatt Reimer 				(host->quirks & SDHCI_QUIRK_USE_WIDE8))
504af62a557SLei Wen 			ctrl &= ~SDHCI_CTRL_8BITBUS;
505af62a557SLei Wen 		if (mmc->bus_width == 4)
506af62a557SLei Wen 			ctrl |= SDHCI_CTRL_4BITBUS;
507af62a557SLei Wen 		else
508af62a557SLei Wen 			ctrl &= ~SDHCI_CTRL_4BITBUS;
509af62a557SLei Wen 	}
5100a93863fSryan_chen #endif
511af62a557SLei Wen 	if (mmc->clock > 26000000)
512af62a557SLei Wen 		ctrl |= SDHCI_CTRL_HISPD;
513af62a557SLei Wen 	else
514af62a557SLei Wen 		ctrl &= ~SDHCI_CTRL_HISPD;
515af62a557SLei Wen 
51688a57125SHannes Schmelzer 	if ((host->quirks & SDHCI_QUIRK_NO_HISPD_BIT) ||
51788a57125SHannes Schmelzer 	    (host->quirks & SDHCI_QUIRK_BROKEN_HISPD_MODE))
518236bfecfSJaehoon Chung 		ctrl &= ~SDHCI_CTRL_HISPD;
519236bfecfSJaehoon Chung 
520af62a557SLei Wen 	sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
52107b0b9c0SJaehoon Chung 
522*730fd353SChin-Ting Kuo 	if ((SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300)) {
523*730fd353SChin-Ting Kuo 		ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL_2);
524*730fd353SChin-Ting Kuo 		ctrl_2 &= ~SDHCI_DRIVER_STRENGTH_MASK;
525*730fd353SChin-Ting Kuo 		ctrl_2 |= host->mmc->drv_type << SDHCI_DRIVER_STRENGTH_SHIFT;
526*730fd353SChin-Ting Kuo 		sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL_2);
527*730fd353SChin-Ting Kuo 	}
528*730fd353SChin-Ting Kuo 
529210841c6SStefan Roese 	/* If available, call the driver specific "post" set_ios() function */
530210841c6SStefan Roese 	if (host->ops && host->ops->set_ios_post)
531210841c6SStefan Roese 		host->ops->set_ios_post(host);
532210841c6SStefan Roese 
533ef1e4edaSSimon Glass 	return 0;
534af62a557SLei Wen }
535af62a557SLei Wen 
5366588c78bSJeroen Hofstee static int sdhci_init(struct mmc *mmc)
537af62a557SLei Wen {
53893bfd616SPantelis Antoniou 	struct sdhci_host *host = mmc->priv;
539af62a557SLei Wen 
5408d549b61SMasahiro Yamada 	sdhci_reset(host, SDHCI_RESET_ALL);
5418d549b61SMasahiro Yamada 
542af62a557SLei Wen 	if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) && !aligned_buffer) {
543af62a557SLei Wen 		aligned_buffer = memalign(8, 512*1024);
544af62a557SLei Wen 		if (!aligned_buffer) {
54530e6d979SDarwin Rambo 			printf("%s: Aligned buffer alloc failed!!!\n",
54630e6d979SDarwin Rambo 			       __func__);
5472cb5d67cSJaehoon Chung 			return -ENOMEM;
548af62a557SLei Wen 		}
549af62a557SLei Wen 	}
550af62a557SLei Wen 
55193bfd616SPantelis Antoniou 	sdhci_set_power(host, fls(mmc->cfg->voltages) - 1);
552470dcc75SJoe Hershberger 
553bf9c4d14SMasahiro Yamada 	if (host->ops && host->ops->get_cd)
5545e96217fSJaehoon Chung 		host->ops->get_cd(host);
555470dcc75SJoe Hershberger 
556ce0c1bc1SŁukasz Majewski 	/* Enable only interrupts served by the SD controller */
55730e6d979SDarwin Rambo 	sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK,
55830e6d979SDarwin Rambo 		     SDHCI_INT_ENABLE);
559ce0c1bc1SŁukasz Majewski 	/* Mask all sdhci interrupt sources */
560ce0c1bc1SŁukasz Majewski 	sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE);
561af62a557SLei Wen 
562af62a557SLei Wen 	return 0;
563af62a557SLei Wen }
564af62a557SLei Wen 
565e7881d85SSimon Glass #ifdef CONFIG_DM_MMC
566ef1e4edaSSimon Glass int sdhci_probe(struct udevice *dev)
567ef1e4edaSSimon Glass {
568ef1e4edaSSimon Glass 	struct mmc *mmc = mmc_get_mmc_dev(dev);
569ab769f22SPantelis Antoniou 
570ef1e4edaSSimon Glass 	return sdhci_init(mmc);
571ef1e4edaSSimon Glass }
572ef1e4edaSSimon Glass 
573ef1e4edaSSimon Glass const struct dm_mmc_ops sdhci_ops = {
574ef1e4edaSSimon Glass 	.send_cmd	= sdhci_send_command,
575ef1e4edaSSimon Glass 	.set_ios	= sdhci_set_ios,
576ca992e82SSiva Durga Prasad Paladugu #ifdef MMC_SUPPORTS_TUNING
577ca992e82SSiva Durga Prasad Paladugu 	.execute_tuning	= sdhci_execute_tuning,
578ca992e82SSiva Durga Prasad Paladugu #endif
579ef1e4edaSSimon Glass };
580ef1e4edaSSimon Glass #else
581ab769f22SPantelis Antoniou static const struct mmc_ops sdhci_ops = {
582ab769f22SPantelis Antoniou 	.send_cmd	= sdhci_send_command,
583ab769f22SPantelis Antoniou 	.set_ios	= sdhci_set_ios,
584ab769f22SPantelis Antoniou 	.init		= sdhci_init,
585ab769f22SPantelis Antoniou };
586ef1e4edaSSimon Glass #endif
587ab769f22SPantelis Antoniou 
58814bed52dSJaehoon Chung int sdhci_setup_cfg(struct mmc_config *cfg, struct sdhci_host *host,
5896d0e34bfSStefan Herbrechtsmeier 		u32 f_max, u32 f_min)
5902a809093SSimon Glass {
591b8e25ef1SSiva Durga Prasad Paladugu 	u32 caps, caps_1 = 0;
59214bed52dSJaehoon Chung 
59314bed52dSJaehoon Chung 	caps = sdhci_readl(host, SDHCI_CAPABILITIES);
59415bd0995SMasahiro Yamada 
59545a68fe2SMasahiro Yamada #ifdef CONFIG_MMC_SDHCI_SDMA
59615bd0995SMasahiro Yamada 	if (!(caps & SDHCI_CAN_DO_SDMA)) {
59715bd0995SMasahiro Yamada 		printf("%s: Your controller doesn't support SDMA!!\n",
59815bd0995SMasahiro Yamada 		       __func__);
59915bd0995SMasahiro Yamada 		return -EINVAL;
60015bd0995SMasahiro Yamada 	}
60115bd0995SMasahiro Yamada #endif
602895549a2SJaehoon Chung 	if (host->quirks & SDHCI_QUIRK_REG32_RW)
603895549a2SJaehoon Chung 		host->version =
604895549a2SJaehoon Chung 			sdhci_readl(host, SDHCI_HOST_VERSION - 2) >> 16;
605895549a2SJaehoon Chung 	else
60614bed52dSJaehoon Chung 		host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
60714bed52dSJaehoon Chung 
60814bed52dSJaehoon Chung 	cfg->name = host->name;
609e7881d85SSimon Glass #ifndef CONFIG_DM_MMC
6102a809093SSimon Glass 	cfg->ops = &sdhci_ops;
6112a809093SSimon Glass #endif
6120e0dcc19SWenyou Yang 
6130e0dcc19SWenyou Yang 	/* Check whether the clock multiplier is supported or not */
6140e0dcc19SWenyou Yang 	if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) {
6150e0dcc19SWenyou Yang 		caps_1 = sdhci_readl(host, SDHCI_CAPABILITIES_1);
6160e0dcc19SWenyou Yang 		host->clk_mul = (caps_1 & SDHCI_CLOCK_MUL_MASK) >>
6170e0dcc19SWenyou Yang 				SDHCI_CLOCK_MUL_SHIFT;
6180e0dcc19SWenyou Yang 	}
6190e0dcc19SWenyou Yang 
6206d0e34bfSStefan Herbrechtsmeier 	if (host->max_clk == 0) {
62114bed52dSJaehoon Chung 		if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300)
6226d0e34bfSStefan Herbrechtsmeier 			host->max_clk = (caps & SDHCI_CLOCK_V3_BASE_MASK) >>
6232a809093SSimon Glass 				SDHCI_CLOCK_BASE_SHIFT;
6242a809093SSimon Glass 		else
6256d0e34bfSStefan Herbrechtsmeier 			host->max_clk = (caps & SDHCI_CLOCK_BASE_MASK) >>
6262a809093SSimon Glass 				SDHCI_CLOCK_BASE_SHIFT;
6276d0e34bfSStefan Herbrechtsmeier 		host->max_clk *= 1000000;
6280e0dcc19SWenyou Yang 		if (host->clk_mul)
6290e0dcc19SWenyou Yang 			host->max_clk *= host->clk_mul;
6302a809093SSimon Glass 	}
6316d0e34bfSStefan Herbrechtsmeier 	if (host->max_clk == 0) {
6326c67954cSMasahiro Yamada 		printf("%s: Hardware doesn't specify base clock frequency\n",
6336c67954cSMasahiro Yamada 		       __func__);
6342a809093SSimon Glass 		return -EINVAL;
6356c67954cSMasahiro Yamada 	}
6366d0e34bfSStefan Herbrechtsmeier 	if (f_max && (f_max < host->max_clk))
6376d0e34bfSStefan Herbrechtsmeier 		cfg->f_max = f_max;
6386d0e34bfSStefan Herbrechtsmeier 	else
6396d0e34bfSStefan Herbrechtsmeier 		cfg->f_max = host->max_clk;
6406d0e34bfSStefan Herbrechtsmeier 	if (f_min)
6416d0e34bfSStefan Herbrechtsmeier 		cfg->f_min = f_min;
6422a809093SSimon Glass 	else {
64314bed52dSJaehoon Chung 		if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300)
6442a809093SSimon Glass 			cfg->f_min = cfg->f_max / SDHCI_MAX_DIV_SPEC_300;
6452a809093SSimon Glass 		else
6462a809093SSimon Glass 			cfg->f_min = cfg->f_max / SDHCI_MAX_DIV_SPEC_200;
6472a809093SSimon Glass 	}
6482a809093SSimon Glass 	cfg->voltages = 0;
6492a809093SSimon Glass 	if (caps & SDHCI_CAN_VDD_330)
6502a809093SSimon Glass 		cfg->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
6512a809093SSimon Glass 	if (caps & SDHCI_CAN_VDD_300)
6522a809093SSimon Glass 		cfg->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
6532a809093SSimon Glass 	if (caps & SDHCI_CAN_VDD_180)
6542a809093SSimon Glass 		cfg->voltages |= MMC_VDD_165_195;
6552a809093SSimon Glass 
6563137e645SMasahiro Yamada 	if (host->quirks & SDHCI_QUIRK_BROKEN_VOLTAGE)
6573137e645SMasahiro Yamada 		cfg->voltages |= host->voltages;
6583137e645SMasahiro Yamada 
659be165fbbSMasahiro Yamada 	cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz | MMC_MODE_4BIT;
6603fd0a9baSJaehoon Chung 
6613fd0a9baSJaehoon Chung 	/* Since Host Controller Version3.0 */
66214bed52dSJaehoon Chung 	if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) {
663ecd7b246SJaehoon Chung 		if (!(caps & SDHCI_CAN_DO_8BIT))
664ecd7b246SJaehoon Chung 			cfg->host_caps &= ~MMC_MODE_8BIT;
6652a809093SSimon Glass 	}
6662a809093SSimon Glass 
66788a57125SHannes Schmelzer 	if (host->quirks & SDHCI_QUIRK_BROKEN_HISPD_MODE) {
66888a57125SHannes Schmelzer 		cfg->host_caps &= ~MMC_MODE_HS;
66988a57125SHannes Schmelzer 		cfg->host_caps &= ~MMC_MODE_HS_52MHz;
67088a57125SHannes Schmelzer 	}
67188a57125SHannes Schmelzer 
672b8e25ef1SSiva Durga Prasad Paladugu 	if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300)
673b8e25ef1SSiva Durga Prasad Paladugu 		caps_1 = sdhci_readl(host, SDHCI_CAPABILITIES_1);
674b8e25ef1SSiva Durga Prasad Paladugu 
675b8e25ef1SSiva Durga Prasad Paladugu 	if (!(cfg->voltages & MMC_VDD_165_195) ||
676b8e25ef1SSiva Durga Prasad Paladugu 	    (host->quirks & SDHCI_QUIRK_NO_1_8_V))
677b8e25ef1SSiva Durga Prasad Paladugu 		caps_1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
678b8e25ef1SSiva Durga Prasad Paladugu 			    SDHCI_SUPPORT_DDR50);
679b8e25ef1SSiva Durga Prasad Paladugu 
680b8e25ef1SSiva Durga Prasad Paladugu 	if (caps_1 & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
681b8e25ef1SSiva Durga Prasad Paladugu 		      SDHCI_SUPPORT_DDR50))
682b8e25ef1SSiva Durga Prasad Paladugu 		cfg->host_caps |= MMC_CAP(UHS_SDR12) | MMC_CAP(UHS_SDR25);
683b8e25ef1SSiva Durga Prasad Paladugu 
684b8e25ef1SSiva Durga Prasad Paladugu 	if (caps_1 & SDHCI_SUPPORT_SDR104) {
685b8e25ef1SSiva Durga Prasad Paladugu 		cfg->host_caps |= MMC_CAP(UHS_SDR104) | MMC_CAP(UHS_SDR50);
686b8e25ef1SSiva Durga Prasad Paladugu 		/*
687b8e25ef1SSiva Durga Prasad Paladugu 		 * SD3.0: SDR104 is supported so (for eMMC) the caps2
688b8e25ef1SSiva Durga Prasad Paladugu 		 * field can be promoted to support HS200.
689b8e25ef1SSiva Durga Prasad Paladugu 		 */
690b8e25ef1SSiva Durga Prasad Paladugu 		cfg->host_caps |= MMC_CAP(MMC_HS_200);
691b8e25ef1SSiva Durga Prasad Paladugu 	} else if (caps_1 & SDHCI_SUPPORT_SDR50) {
692b8e25ef1SSiva Durga Prasad Paladugu 		cfg->host_caps |= MMC_CAP(UHS_SDR50);
693b8e25ef1SSiva Durga Prasad Paladugu 	}
694b8e25ef1SSiva Durga Prasad Paladugu 
695b8e25ef1SSiva Durga Prasad Paladugu 	if (caps_1 & SDHCI_SUPPORT_DDR50)
696b8e25ef1SSiva Durga Prasad Paladugu 		cfg->host_caps |= MMC_CAP(UHS_DDR50);
697b8e25ef1SSiva Durga Prasad Paladugu 
69814bed52dSJaehoon Chung 	if (host->host_caps)
69914bed52dSJaehoon Chung 		cfg->host_caps |= host->host_caps;
7002a809093SSimon Glass 
7012a809093SSimon Glass 	cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
7022a809093SSimon Glass 
7032a809093SSimon Glass 	return 0;
7042a809093SSimon Glass }
7052a809093SSimon Glass 
706ef1e4edaSSimon Glass #ifdef CONFIG_BLK
707ef1e4edaSSimon Glass int sdhci_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg)
708ef1e4edaSSimon Glass {
709ef1e4edaSSimon Glass 	return mmc_bind(dev, mmc, cfg);
710ef1e4edaSSimon Glass }
711ef1e4edaSSimon Glass #else
7126d0e34bfSStefan Herbrechtsmeier int add_sdhci(struct sdhci_host *host, u32 f_max, u32 f_min)
713af62a557SLei Wen {
7146c67954cSMasahiro Yamada 	int ret;
7156c67954cSMasahiro Yamada 
7166d0e34bfSStefan Herbrechtsmeier 	ret = sdhci_setup_cfg(&host->cfg, host, f_max, f_min);
7176c67954cSMasahiro Yamada 	if (ret)
7186c67954cSMasahiro Yamada 		return ret;
719236bfecfSJaehoon Chung 
72093bfd616SPantelis Antoniou 	host->mmc = mmc_create(&host->cfg, host);
72193bfd616SPantelis Antoniou 	if (host->mmc == NULL) {
72293bfd616SPantelis Antoniou 		printf("%s: mmc create fail!\n", __func__);
7232cb5d67cSJaehoon Chung 		return -ENOMEM;
72493bfd616SPantelis Antoniou 	}
725af62a557SLei Wen 
726af62a557SLei Wen 	return 0;
727af62a557SLei Wen }
728ef1e4edaSSimon Glass #endif
729