xref: /rk3399_rockchip-uboot/drivers/mmc/sdhci.c (revision d23d8d7e069c3aca071b7f68d9c15d11f8d4c84d)
1af62a557SLei Wen /*
2af62a557SLei Wen  * Copyright 2011, Marvell Semiconductor Inc.
3af62a557SLei Wen  * Lei Wen <leiwen@marvell.com>
4af62a557SLei Wen  *
5af62a557SLei Wen  * See file CREDITS for list of people who contributed to this
6af62a557SLei Wen  * project.
7af62a557SLei Wen  *
8af62a557SLei Wen  * This program is free software; you can redistribute it and/or
9af62a557SLei Wen  * modify it under the terms of the GNU General Public License as
10af62a557SLei Wen  * published by the Free Software Foundation; either version 2 of
11af62a557SLei Wen  * the License, or (at your option) any later version.
12af62a557SLei Wen  *
13af62a557SLei Wen  * This program is distributed in the hope that it will be useful,
14af62a557SLei Wen  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15af62a557SLei Wen  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16af62a557SLei Wen  * GNU General Public License for more details.
17af62a557SLei Wen  *
18af62a557SLei Wen  * You should have received a copy of the GNU General Public License
19af62a557SLei Wen  * along with this program; if not, write to the Free Software
20af62a557SLei Wen  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21af62a557SLei Wen  * MA 02111-1307 USA
22af62a557SLei Wen  *
23af62a557SLei Wen  * Back ported to the 8xx platform (from the 8260 platform) by
24af62a557SLei Wen  * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
25af62a557SLei Wen  */
26af62a557SLei Wen 
27af62a557SLei Wen #include <common.h>
28af62a557SLei Wen #include <malloc.h>
29af62a557SLei Wen #include <mmc.h>
30af62a557SLei Wen #include <sdhci.h>
31af62a557SLei Wen 
32af62a557SLei Wen void *aligned_buffer;
33af62a557SLei Wen 
34af62a557SLei Wen static void sdhci_reset(struct sdhci_host *host, u8 mask)
35af62a557SLei Wen {
36af62a557SLei Wen 	unsigned long timeout;
37af62a557SLei Wen 
38af62a557SLei Wen 	/* Wait max 100 ms */
39af62a557SLei Wen 	timeout = 100;
40af62a557SLei Wen 	sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
41af62a557SLei Wen 	while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
42af62a557SLei Wen 		if (timeout == 0) {
43af62a557SLei Wen 			printf("Reset 0x%x never completed.\n", (int)mask);
44af62a557SLei Wen 			return;
45af62a557SLei Wen 		}
46af62a557SLei Wen 		timeout--;
47af62a557SLei Wen 		udelay(1000);
48af62a557SLei Wen 	}
49af62a557SLei Wen }
50af62a557SLei Wen 
51af62a557SLei Wen static void sdhci_cmd_done(struct sdhci_host *host, struct mmc_cmd *cmd)
52af62a557SLei Wen {
53af62a557SLei Wen 	int i;
54af62a557SLei Wen 	if (cmd->resp_type & MMC_RSP_136) {
55af62a557SLei Wen 		/* CRC is stripped so we need to do some shifting. */
56af62a557SLei Wen 		for (i = 0; i < 4; i++) {
57af62a557SLei Wen 			cmd->response[i] = sdhci_readl(host,
58af62a557SLei Wen 					SDHCI_RESPONSE + (3-i)*4) << 8;
59af62a557SLei Wen 			if (i != 3)
60af62a557SLei Wen 				cmd->response[i] |= sdhci_readb(host,
61af62a557SLei Wen 						SDHCI_RESPONSE + (3-i)*4-1);
62af62a557SLei Wen 		}
63af62a557SLei Wen 	} else {
64af62a557SLei Wen 		cmd->response[0] = sdhci_readl(host, SDHCI_RESPONSE);
65af62a557SLei Wen 	}
66af62a557SLei Wen }
67af62a557SLei Wen 
68af62a557SLei Wen static void sdhci_transfer_pio(struct sdhci_host *host, struct mmc_data *data)
69af62a557SLei Wen {
70af62a557SLei Wen 	int i;
71af62a557SLei Wen 	char *offs;
72af62a557SLei Wen 	for (i = 0; i < data->blocksize; i += 4) {
73af62a557SLei Wen 		offs = data->dest + i;
74af62a557SLei Wen 		if (data->flags == MMC_DATA_READ)
75af62a557SLei Wen 			*(u32 *)offs = sdhci_readl(host, SDHCI_BUFFER);
76af62a557SLei Wen 		else
77af62a557SLei Wen 			sdhci_writel(host, *(u32 *)offs, SDHCI_BUFFER);
78af62a557SLei Wen 	}
79af62a557SLei Wen }
80af62a557SLei Wen 
81af62a557SLei Wen static int sdhci_transfer_data(struct sdhci_host *host, struct mmc_data *data,
82af62a557SLei Wen 				unsigned int start_addr)
83af62a557SLei Wen {
84a004abdeSLei Wen 	unsigned int stat, rdy, mask, timeout, block = 0;
85804c7f42SJaehoon Chung #ifdef CONFIG_MMC_SDMA
86804c7f42SJaehoon Chung 	unsigned char ctrl;
87804c7f42SJaehoon Chung 	ctrl = sdhci_readl(host, SDHCI_HOST_CONTROL);
88804c7f42SJaehoon Chung 	ctrl &= ~SDHCI_CTRL_DMA_MASK;
89804c7f42SJaehoon Chung 	ctrl |= SDHCI_CTRL_SDMA;
90804c7f42SJaehoon Chung 	sdhci_writel(host, ctrl, SDHCI_HOST_CONTROL);
91804c7f42SJaehoon Chung #endif
92af62a557SLei Wen 
935d48e422SJaehoon Chung 	timeout = 1000000;
94af62a557SLei Wen 	rdy = SDHCI_INT_SPACE_AVAIL | SDHCI_INT_DATA_AVAIL;
95af62a557SLei Wen 	mask = SDHCI_DATA_AVAILABLE | SDHCI_SPACE_AVAILABLE;
96af62a557SLei Wen 	do {
97af62a557SLei Wen 		stat = sdhci_readl(host, SDHCI_INT_STATUS);
98af62a557SLei Wen 		if (stat & SDHCI_INT_ERROR) {
99af62a557SLei Wen 			printf("Error detected in status(0x%X)!\n", stat);
100af62a557SLei Wen 			return -1;
101af62a557SLei Wen 		}
102af62a557SLei Wen 		if (stat & rdy) {
103af62a557SLei Wen 			if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & mask))
104af62a557SLei Wen 				continue;
105af62a557SLei Wen 			sdhci_writel(host, rdy, SDHCI_INT_STATUS);
106af62a557SLei Wen 			sdhci_transfer_pio(host, data);
107af62a557SLei Wen 			data->dest += data->blocksize;
108af62a557SLei Wen 			if (++block >= data->blocks)
109af62a557SLei Wen 				break;
110af62a557SLei Wen 		}
111af62a557SLei Wen #ifdef CONFIG_MMC_SDMA
112af62a557SLei Wen 		if (stat & SDHCI_INT_DMA_END) {
113af62a557SLei Wen 			sdhci_writel(host, SDHCI_INT_DMA_END, SDHCI_INT_STATUS);
1143e81c772SLei Wen 			start_addr &= ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1);
115af62a557SLei Wen 			start_addr += SDHCI_DEFAULT_BOUNDARY_SIZE;
116af62a557SLei Wen 			sdhci_writel(host, start_addr, SDHCI_DMA_ADDRESS);
117af62a557SLei Wen 		}
118af62a557SLei Wen #endif
119a004abdeSLei Wen 		if (timeout-- > 0)
120a004abdeSLei Wen 			udelay(10);
121a004abdeSLei Wen 		else {
122a004abdeSLei Wen 			printf("Transfer data timeout\n");
123a004abdeSLei Wen 			return -1;
124a004abdeSLei Wen 		}
125af62a557SLei Wen 	} while (!(stat & SDHCI_INT_DATA_END));
126af62a557SLei Wen 	return 0;
127af62a557SLei Wen }
128af62a557SLei Wen 
129af62a557SLei Wen int sdhci_send_command(struct mmc *mmc, struct mmc_cmd *cmd,
130af62a557SLei Wen 		       struct mmc_data *data)
131af62a557SLei Wen {
132af62a557SLei Wen 	struct sdhci_host *host = (struct sdhci_host *)mmc->priv;
133af62a557SLei Wen 	unsigned int stat = 0;
134af62a557SLei Wen 	int ret = 0;
135af62a557SLei Wen 	int trans_bytes = 0, is_aligned = 1;
136af62a557SLei Wen 	u32 mask, flags, mode;
137af62a557SLei Wen 	unsigned int timeout, start_addr = 0;
1383a638320SJaehoon Chung 	unsigned int retry = 10000;
139af62a557SLei Wen 
140af62a557SLei Wen 	/* Wait max 10 ms */
141af62a557SLei Wen 	timeout = 10;
142af62a557SLei Wen 
143af62a557SLei Wen 	sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
144af62a557SLei Wen 	mask = SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT;
145af62a557SLei Wen 
146af62a557SLei Wen 	/* We shouldn't wait for data inihibit for stop commands, even
147af62a557SLei Wen 	   though they might use busy signaling */
148af62a557SLei Wen 	if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
149af62a557SLei Wen 		mask &= ~SDHCI_DATA_INHIBIT;
150af62a557SLei Wen 
151af62a557SLei Wen 	while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
152af62a557SLei Wen 		if (timeout == 0) {
153af62a557SLei Wen 			printf("Controller never released inhibit bit(s).\n");
154af62a557SLei Wen 			return COMM_ERR;
155af62a557SLei Wen 		}
156af62a557SLei Wen 		timeout--;
157af62a557SLei Wen 		udelay(1000);
158af62a557SLei Wen 	}
159af62a557SLei Wen 
160af62a557SLei Wen 	mask = SDHCI_INT_RESPONSE;
161af62a557SLei Wen 	if (!(cmd->resp_type & MMC_RSP_PRESENT))
162af62a557SLei Wen 		flags = SDHCI_CMD_RESP_NONE;
163af62a557SLei Wen 	else if (cmd->resp_type & MMC_RSP_136)
164af62a557SLei Wen 		flags = SDHCI_CMD_RESP_LONG;
165af62a557SLei Wen 	else if (cmd->resp_type & MMC_RSP_BUSY) {
166af62a557SLei Wen 		flags = SDHCI_CMD_RESP_SHORT_BUSY;
167af62a557SLei Wen 		mask |= SDHCI_INT_DATA_END;
168af62a557SLei Wen 	} else
169af62a557SLei Wen 		flags = SDHCI_CMD_RESP_SHORT;
170af62a557SLei Wen 
171af62a557SLei Wen 	if (cmd->resp_type & MMC_RSP_CRC)
172af62a557SLei Wen 		flags |= SDHCI_CMD_CRC;
173af62a557SLei Wen 	if (cmd->resp_type & MMC_RSP_OPCODE)
174af62a557SLei Wen 		flags |= SDHCI_CMD_INDEX;
175af62a557SLei Wen 	if (data)
176af62a557SLei Wen 		flags |= SDHCI_CMD_DATA;
177af62a557SLei Wen 
178af62a557SLei Wen 	/*Set Transfer mode regarding to data flag*/
179af62a557SLei Wen 	if (data != 0) {
180af62a557SLei Wen 		sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL);
181af62a557SLei Wen 		mode = SDHCI_TRNS_BLK_CNT_EN;
182af62a557SLei Wen 		trans_bytes = data->blocks * data->blocksize;
183af62a557SLei Wen 		if (data->blocks > 1)
184af62a557SLei Wen 			mode |= SDHCI_TRNS_MULTI;
185af62a557SLei Wen 
186af62a557SLei Wen 		if (data->flags == MMC_DATA_READ)
187af62a557SLei Wen 			mode |= SDHCI_TRNS_READ;
188af62a557SLei Wen 
189af62a557SLei Wen #ifdef CONFIG_MMC_SDMA
190af62a557SLei Wen 		if (data->flags == MMC_DATA_READ)
191af62a557SLei Wen 			start_addr = (unsigned int)data->dest;
192af62a557SLei Wen 		else
193af62a557SLei Wen 			start_addr = (unsigned int)data->src;
194af62a557SLei Wen 		if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
195af62a557SLei Wen 				(start_addr & 0x7) != 0x0) {
196af62a557SLei Wen 			is_aligned = 0;
197af62a557SLei Wen 			start_addr = (unsigned int)aligned_buffer;
198af62a557SLei Wen 			if (data->flags != MMC_DATA_READ)
199af62a557SLei Wen 				memcpy(aligned_buffer, data->src, trans_bytes);
200af62a557SLei Wen 		}
201af62a557SLei Wen 
202af62a557SLei Wen 		sdhci_writel(host, start_addr, SDHCI_DMA_ADDRESS);
203af62a557SLei Wen 		mode |= SDHCI_TRNS_DMA;
204af62a557SLei Wen #endif
205af62a557SLei Wen 		sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
206af62a557SLei Wen 				data->blocksize),
207af62a557SLei Wen 				SDHCI_BLOCK_SIZE);
208af62a557SLei Wen 		sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
209af62a557SLei Wen 		sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
210af62a557SLei Wen 	}
211af62a557SLei Wen 
212af62a557SLei Wen 	sdhci_writel(host, cmd->cmdarg, SDHCI_ARGUMENT);
213af62a557SLei Wen #ifdef CONFIG_MMC_SDMA
2142c2ec4c9SLei Wen 	flush_cache(start_addr, trans_bytes);
215af62a557SLei Wen #endif
216af62a557SLei Wen 	sdhci_writew(host, SDHCI_MAKE_CMD(cmd->cmdidx, flags), SDHCI_COMMAND);
217af62a557SLei Wen 	do {
218af62a557SLei Wen 		stat = sdhci_readl(host, SDHCI_INT_STATUS);
219af62a557SLei Wen 		if (stat & SDHCI_INT_ERROR)
220af62a557SLei Wen 			break;
2213a638320SJaehoon Chung 		if (--retry == 0)
2223a638320SJaehoon Chung 			break;
223af62a557SLei Wen 	} while ((stat & mask) != mask);
224af62a557SLei Wen 
2253a638320SJaehoon Chung 	if (retry == 0) {
2263a638320SJaehoon Chung 		if (host->quirks & SDHCI_QUIRK_BROKEN_R1B)
2273a638320SJaehoon Chung 			return 0;
2283a638320SJaehoon Chung 		else {
2293a638320SJaehoon Chung 			printf("Timeout for status update!\n");
2303a638320SJaehoon Chung 			return TIMEOUT;
2313a638320SJaehoon Chung 		}
2323a638320SJaehoon Chung 	}
2333a638320SJaehoon Chung 
234af62a557SLei Wen 	if ((stat & (SDHCI_INT_ERROR | mask)) == mask) {
235af62a557SLei Wen 		sdhci_cmd_done(host, cmd);
236af62a557SLei Wen 		sdhci_writel(host, mask, SDHCI_INT_STATUS);
237af62a557SLei Wen 	} else
238af62a557SLei Wen 		ret = -1;
239af62a557SLei Wen 
240af62a557SLei Wen 	if (!ret && data)
241af62a557SLei Wen 		ret = sdhci_transfer_data(host, data, start_addr);
242af62a557SLei Wen 
24313243f2eSTushar Behera 	if (host->quirks & SDHCI_QUIRK_WAIT_SEND_CMD)
24413243f2eSTushar Behera 		udelay(1000);
24513243f2eSTushar Behera 
246af62a557SLei Wen 	stat = sdhci_readl(host, SDHCI_INT_STATUS);
247af62a557SLei Wen 	sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
248af62a557SLei Wen 	if (!ret) {
249af62a557SLei Wen 		if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
250af62a557SLei Wen 				!is_aligned && (data->flags == MMC_DATA_READ))
251af62a557SLei Wen 			memcpy(data->dest, aligned_buffer, trans_bytes);
252af62a557SLei Wen 		return 0;
253af62a557SLei Wen 	}
254af62a557SLei Wen 
255af62a557SLei Wen 	sdhci_reset(host, SDHCI_RESET_CMD);
256af62a557SLei Wen 	sdhci_reset(host, SDHCI_RESET_DATA);
257af62a557SLei Wen 	if (stat & SDHCI_INT_TIMEOUT)
258af62a557SLei Wen 		return TIMEOUT;
259af62a557SLei Wen 	else
260af62a557SLei Wen 		return COMM_ERR;
261af62a557SLei Wen }
262af62a557SLei Wen 
263af62a557SLei Wen static int sdhci_set_clock(struct mmc *mmc, unsigned int clock)
264af62a557SLei Wen {
265af62a557SLei Wen 	struct sdhci_host *host = (struct sdhci_host *)mmc->priv;
266af62a557SLei Wen 	unsigned int div, clk, timeout;
267af62a557SLei Wen 
268af62a557SLei Wen 	sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
269af62a557SLei Wen 
270af62a557SLei Wen 	if (clock == 0)
271af62a557SLei Wen 		return 0;
272af62a557SLei Wen 
273073cfd1cSJoe Hershberger 	if ((host->version & SDHCI_SPEC_VER_MASK) >= SDHCI_SPEC_300) {
274af62a557SLei Wen 		/* Version 3.00 divisors must be a multiple of 2. */
275af62a557SLei Wen 		if (mmc->f_max <= clock)
276af62a557SLei Wen 			div = 1;
277af62a557SLei Wen 		else {
278af62a557SLei Wen 			for (div = 2; div < SDHCI_MAX_DIV_SPEC_300; div += 2) {
279af62a557SLei Wen 				if ((mmc->f_max / div) <= clock)
280af62a557SLei Wen 					break;
281af62a557SLei Wen 			}
282af62a557SLei Wen 		}
283af62a557SLei Wen 	} else {
284af62a557SLei Wen 		/* Version 2.00 divisors must be a power of 2. */
285af62a557SLei Wen 		for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
286af62a557SLei Wen 			if ((mmc->f_max / div) <= clock)
287af62a557SLei Wen 				break;
288af62a557SLei Wen 		}
289af62a557SLei Wen 	}
290af62a557SLei Wen 	div >>= 1;
291af62a557SLei Wen 
292b09ed6e4SJaehoon Chung 	if (host->set_clock)
293b09ed6e4SJaehoon Chung 		host->set_clock(host->index, div);
294b09ed6e4SJaehoon Chung 
295af62a557SLei Wen 	clk = (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
296af62a557SLei Wen 	clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
297af62a557SLei Wen 		<< SDHCI_DIVIDER_HI_SHIFT;
298af62a557SLei Wen 	clk |= SDHCI_CLOCK_INT_EN;
299af62a557SLei Wen 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
300af62a557SLei Wen 
301af62a557SLei Wen 	/* Wait max 20 ms */
302af62a557SLei Wen 	timeout = 20;
303af62a557SLei Wen 	while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
304af62a557SLei Wen 		& SDHCI_CLOCK_INT_STABLE)) {
305af62a557SLei Wen 		if (timeout == 0) {
306af62a557SLei Wen 			printf("Internal clock never stabilised.\n");
307af62a557SLei Wen 			return -1;
308af62a557SLei Wen 		}
309af62a557SLei Wen 		timeout--;
310af62a557SLei Wen 		udelay(1000);
311af62a557SLei Wen 	}
312af62a557SLei Wen 
313af62a557SLei Wen 	clk |= SDHCI_CLOCK_CARD_EN;
314af62a557SLei Wen 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
315af62a557SLei Wen 	return 0;
316af62a557SLei Wen }
317af62a557SLei Wen 
318af62a557SLei Wen static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
319af62a557SLei Wen {
320af62a557SLei Wen 	u8 pwr = 0;
321af62a557SLei Wen 
322af62a557SLei Wen 	if (power != (unsigned short)-1) {
323af62a557SLei Wen 		switch (1 << power) {
324af62a557SLei Wen 		case MMC_VDD_165_195:
325af62a557SLei Wen 			pwr = SDHCI_POWER_180;
326af62a557SLei Wen 			break;
327af62a557SLei Wen 		case MMC_VDD_29_30:
328af62a557SLei Wen 		case MMC_VDD_30_31:
329af62a557SLei Wen 			pwr = SDHCI_POWER_300;
330af62a557SLei Wen 			break;
331af62a557SLei Wen 		case MMC_VDD_32_33:
332af62a557SLei Wen 		case MMC_VDD_33_34:
333af62a557SLei Wen 			pwr = SDHCI_POWER_330;
334af62a557SLei Wen 			break;
335af62a557SLei Wen 		}
336af62a557SLei Wen 	}
337af62a557SLei Wen 
338af62a557SLei Wen 	if (pwr == 0) {
339af62a557SLei Wen 		sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
340af62a557SLei Wen 		return;
341af62a557SLei Wen 	}
342af62a557SLei Wen 
343688c2d14SMela Custodio 	if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
344688c2d14SMela Custodio 		sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
345688c2d14SMela Custodio 
346af62a557SLei Wen 	pwr |= SDHCI_POWER_ON;
347af62a557SLei Wen 
348af62a557SLei Wen 	sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
349af62a557SLei Wen }
350af62a557SLei Wen 
351af62a557SLei Wen void sdhci_set_ios(struct mmc *mmc)
352af62a557SLei Wen {
353af62a557SLei Wen 	u32 ctrl;
354af62a557SLei Wen 	struct sdhci_host *host = (struct sdhci_host *)mmc->priv;
355af62a557SLei Wen 
356236bfecfSJaehoon Chung 	if (host->set_control_reg)
357236bfecfSJaehoon Chung 		host->set_control_reg(host);
358236bfecfSJaehoon Chung 
359af62a557SLei Wen 	if (mmc->clock != host->clock)
360af62a557SLei Wen 		sdhci_set_clock(mmc, mmc->clock);
361af62a557SLei Wen 
362af62a557SLei Wen 	/* Set bus width */
363af62a557SLei Wen 	ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
364af62a557SLei Wen 	if (mmc->bus_width == 8) {
365af62a557SLei Wen 		ctrl &= ~SDHCI_CTRL_4BITBUS;
366073cfd1cSJoe Hershberger 		if ((host->version & SDHCI_SPEC_VER_MASK) >= SDHCI_SPEC_300)
367af62a557SLei Wen 			ctrl |= SDHCI_CTRL_8BITBUS;
368af62a557SLei Wen 	} else {
369073cfd1cSJoe Hershberger 		if ((host->version & SDHCI_SPEC_VER_MASK) >= SDHCI_SPEC_300)
370af62a557SLei Wen 			ctrl &= ~SDHCI_CTRL_8BITBUS;
371af62a557SLei Wen 		if (mmc->bus_width == 4)
372af62a557SLei Wen 			ctrl |= SDHCI_CTRL_4BITBUS;
373af62a557SLei Wen 		else
374af62a557SLei Wen 			ctrl &= ~SDHCI_CTRL_4BITBUS;
375af62a557SLei Wen 	}
376af62a557SLei Wen 
377af62a557SLei Wen 	if (mmc->clock > 26000000)
378af62a557SLei Wen 		ctrl |= SDHCI_CTRL_HISPD;
379af62a557SLei Wen 	else
380af62a557SLei Wen 		ctrl &= ~SDHCI_CTRL_HISPD;
381af62a557SLei Wen 
382236bfecfSJaehoon Chung 	if (host->quirks & SDHCI_QUIRK_NO_HISPD_BIT)
383236bfecfSJaehoon Chung 		ctrl &= ~SDHCI_CTRL_HISPD;
384236bfecfSJaehoon Chung 
385af62a557SLei Wen 	sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
386af62a557SLei Wen }
387af62a557SLei Wen 
388af62a557SLei Wen int sdhci_init(struct mmc *mmc)
389af62a557SLei Wen {
390af62a557SLei Wen 	struct sdhci_host *host = (struct sdhci_host *)mmc->priv;
391af62a557SLei Wen 
392af62a557SLei Wen 	if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) && !aligned_buffer) {
393af62a557SLei Wen 		aligned_buffer = memalign(8, 512*1024);
394af62a557SLei Wen 		if (!aligned_buffer) {
395af62a557SLei Wen 			printf("Aligned buffer alloc failed!!!");
396af62a557SLei Wen 			return -1;
397af62a557SLei Wen 		}
398af62a557SLei Wen 	}
399af62a557SLei Wen 
400470dcc75SJoe Hershberger 	sdhci_set_power(host, fls(mmc->voltages) - 1);
401470dcc75SJoe Hershberger 
402470dcc75SJoe Hershberger 	if (host->quirks & SDHCI_QUIRK_NO_CD) {
403470dcc75SJoe Hershberger 		unsigned int status;
404470dcc75SJoe Hershberger 
405470dcc75SJoe Hershberger 		sdhci_writel(host, SDHCI_CTRL_CD_TEST_INS | SDHCI_CTRL_CD_TEST,
406470dcc75SJoe Hershberger 			SDHCI_HOST_CONTROL);
407470dcc75SJoe Hershberger 
408470dcc75SJoe Hershberger 		status = sdhci_readl(host, SDHCI_PRESENT_STATE);
409470dcc75SJoe Hershberger 		while ((!(status & SDHCI_CARD_PRESENT)) ||
410470dcc75SJoe Hershberger 		    (!(status & SDHCI_CARD_STATE_STABLE)) ||
411470dcc75SJoe Hershberger 		    (!(status & SDHCI_CARD_DETECT_PIN_LEVEL)))
412470dcc75SJoe Hershberger 			status = sdhci_readl(host, SDHCI_PRESENT_STATE);
413470dcc75SJoe Hershberger 	}
414470dcc75SJoe Hershberger 
415af62a557SLei Wen 	/* Eable all state */
416af62a557SLei Wen 	sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_ENABLE);
417af62a557SLei Wen 	sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_SIGNAL_ENABLE);
418af62a557SLei Wen 
419af62a557SLei Wen 	return 0;
420af62a557SLei Wen }
421af62a557SLei Wen 
422af62a557SLei Wen int add_sdhci(struct sdhci_host *host, u32 max_clk, u32 min_clk)
423af62a557SLei Wen {
424af62a557SLei Wen 	struct mmc *mmc;
425af62a557SLei Wen 	unsigned int caps;
426af62a557SLei Wen 
427af62a557SLei Wen 	mmc = malloc(sizeof(struct mmc));
428af62a557SLei Wen 	if (!mmc) {
429af62a557SLei Wen 		printf("mmc malloc fail!\n");
430af62a557SLei Wen 		return -1;
431af62a557SLei Wen 	}
432af62a557SLei Wen 
433af62a557SLei Wen 	mmc->priv = host;
4346cf1b17cSLei Wen 	host->mmc = mmc;
435af62a557SLei Wen 
436af62a557SLei Wen 	sprintf(mmc->name, "%s", host->name);
437af62a557SLei Wen 	mmc->send_cmd = sdhci_send_command;
438af62a557SLei Wen 	mmc->set_ios = sdhci_set_ios;
439af62a557SLei Wen 	mmc->init = sdhci_init;
44048972d90SThierry Reding 	mmc->getcd = NULL;
441*d23d8d7eSNikita Kiryanov 	mmc->getwp = NULL;
442af62a557SLei Wen 
443af62a557SLei Wen 	caps = sdhci_readl(host, SDHCI_CAPABILITIES);
444af62a557SLei Wen #ifdef CONFIG_MMC_SDMA
445af62a557SLei Wen 	if (!(caps & SDHCI_CAN_DO_SDMA)) {
446af62a557SLei Wen 		printf("Your controller don't support sdma!!\n");
447af62a557SLei Wen 		return -1;
448af62a557SLei Wen 	}
449af62a557SLei Wen #endif
450af62a557SLei Wen 
451af62a557SLei Wen 	if (max_clk)
452af62a557SLei Wen 		mmc->f_max = max_clk;
453af62a557SLei Wen 	else {
454073cfd1cSJoe Hershberger 		if ((host->version & SDHCI_SPEC_VER_MASK) >= SDHCI_SPEC_300)
455af62a557SLei Wen 			mmc->f_max = (caps & SDHCI_CLOCK_V3_BASE_MASK)
456af62a557SLei Wen 				>> SDHCI_CLOCK_BASE_SHIFT;
457af62a557SLei Wen 		else
458af62a557SLei Wen 			mmc->f_max = (caps & SDHCI_CLOCK_BASE_MASK)
459af62a557SLei Wen 				>> SDHCI_CLOCK_BASE_SHIFT;
460af62a557SLei Wen 		mmc->f_max *= 1000000;
461af62a557SLei Wen 	}
462af62a557SLei Wen 	if (mmc->f_max == 0) {
463af62a557SLei Wen 		printf("Hardware doesn't specify base clock frequency\n");
464af62a557SLei Wen 		return -1;
465af62a557SLei Wen 	}
466af62a557SLei Wen 	if (min_clk)
467af62a557SLei Wen 		mmc->f_min = min_clk;
468af62a557SLei Wen 	else {
469073cfd1cSJoe Hershberger 		if ((host->version & SDHCI_SPEC_VER_MASK) >= SDHCI_SPEC_300)
470af62a557SLei Wen 			mmc->f_min = mmc->f_max / SDHCI_MAX_DIV_SPEC_300;
471af62a557SLei Wen 		else
472af62a557SLei Wen 			mmc->f_min = mmc->f_max / SDHCI_MAX_DIV_SPEC_200;
473af62a557SLei Wen 	}
474af62a557SLei Wen 
475af62a557SLei Wen 	mmc->voltages = 0;
476af62a557SLei Wen 	if (caps & SDHCI_CAN_VDD_330)
477af62a557SLei Wen 		mmc->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
478af62a557SLei Wen 	if (caps & SDHCI_CAN_VDD_300)
479af62a557SLei Wen 		mmc->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
480af62a557SLei Wen 	if (caps & SDHCI_CAN_VDD_180)
481af62a557SLei Wen 		mmc->voltages |= MMC_VDD_165_195;
482236bfecfSJaehoon Chung 
483236bfecfSJaehoon Chung 	if (host->quirks & SDHCI_QUIRK_BROKEN_VOLTAGE)
484236bfecfSJaehoon Chung 		mmc->voltages |= host->voltages;
485236bfecfSJaehoon Chung 
486af62a557SLei Wen 	mmc->host_caps = MMC_MODE_HS | MMC_MODE_HS_52MHz | MMC_MODE_4BIT;
487af62a557SLei Wen 	if (caps & SDHCI_CAN_DO_8BIT)
488af62a557SLei Wen 		mmc->host_caps |= MMC_MODE_8BIT;
489236bfecfSJaehoon Chung 	if (host->host_caps)
490236bfecfSJaehoon Chung 		mmc->host_caps |= host->host_caps;
491af62a557SLei Wen 
492af62a557SLei Wen 	sdhci_reset(host, SDHCI_RESET_ALL);
493af62a557SLei Wen 	mmc_register(mmc);
494af62a557SLei Wen 
495af62a557SLei Wen 	return 0;
496af62a557SLei Wen }
497