xref: /rk3399_rockchip-uboot/drivers/mmc/sdhci.c (revision 073cfd1c040b9cce9289cacf4d4963460322dc2b)
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;
85af62a557SLei Wen 
86a004abdeSLei Wen 	timeout = 10000;
87af62a557SLei Wen 	rdy = SDHCI_INT_SPACE_AVAIL | SDHCI_INT_DATA_AVAIL;
88af62a557SLei Wen 	mask = SDHCI_DATA_AVAILABLE | SDHCI_SPACE_AVAILABLE;
89af62a557SLei Wen 	do {
90af62a557SLei Wen 		stat = sdhci_readl(host, SDHCI_INT_STATUS);
91af62a557SLei Wen 		if (stat & SDHCI_INT_ERROR) {
92af62a557SLei Wen 			printf("Error detected in status(0x%X)!\n", stat);
93af62a557SLei Wen 			return -1;
94af62a557SLei Wen 		}
95af62a557SLei Wen 		if (stat & rdy) {
96af62a557SLei Wen 			if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & mask))
97af62a557SLei Wen 				continue;
98af62a557SLei Wen 			sdhci_writel(host, rdy, SDHCI_INT_STATUS);
99af62a557SLei Wen 			sdhci_transfer_pio(host, data);
100af62a557SLei Wen 			data->dest += data->blocksize;
101af62a557SLei Wen 			if (++block >= data->blocks)
102af62a557SLei Wen 				break;
103af62a557SLei Wen 		}
104af62a557SLei Wen #ifdef CONFIG_MMC_SDMA
105af62a557SLei Wen 		if (stat & SDHCI_INT_DMA_END) {
106af62a557SLei Wen 			sdhci_writel(host, SDHCI_INT_DMA_END, SDHCI_INT_STATUS);
1073e81c772SLei Wen 			start_addr &= ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1);
108af62a557SLei Wen 			start_addr += SDHCI_DEFAULT_BOUNDARY_SIZE;
109af62a557SLei Wen 			sdhci_writel(host, start_addr, SDHCI_DMA_ADDRESS);
110af62a557SLei Wen 		}
111af62a557SLei Wen #endif
112a004abdeSLei Wen 		if (timeout-- > 0)
113a004abdeSLei Wen 			udelay(10);
114a004abdeSLei Wen 		else {
115a004abdeSLei Wen 			printf("Transfer data timeout\n");
116a004abdeSLei Wen 			return -1;
117a004abdeSLei Wen 		}
118af62a557SLei Wen 	} while (!(stat & SDHCI_INT_DATA_END));
119af62a557SLei Wen 	return 0;
120af62a557SLei Wen }
121af62a557SLei Wen 
122af62a557SLei Wen int sdhci_send_command(struct mmc *mmc, struct mmc_cmd *cmd,
123af62a557SLei Wen 		       struct mmc_data *data)
124af62a557SLei Wen {
125af62a557SLei Wen 	struct sdhci_host *host = (struct sdhci_host *)mmc->priv;
126af62a557SLei Wen 	unsigned int stat = 0;
127af62a557SLei Wen 	int ret = 0;
128af62a557SLei Wen 	int trans_bytes = 0, is_aligned = 1;
129af62a557SLei Wen 	u32 mask, flags, mode;
130af62a557SLei Wen 	unsigned int timeout, start_addr = 0;
1313a638320SJaehoon Chung 	unsigned int retry = 10000;
132af62a557SLei Wen 
133af62a557SLei Wen 	/* Wait max 10 ms */
134af62a557SLei Wen 	timeout = 10;
135af62a557SLei Wen 
136af62a557SLei Wen 	sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
137af62a557SLei Wen 	mask = SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT;
138af62a557SLei Wen 
139af62a557SLei Wen 	/* We shouldn't wait for data inihibit for stop commands, even
140af62a557SLei Wen 	   though they might use busy signaling */
141af62a557SLei Wen 	if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
142af62a557SLei Wen 		mask &= ~SDHCI_DATA_INHIBIT;
143af62a557SLei Wen 
144af62a557SLei Wen 	while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
145af62a557SLei Wen 		if (timeout == 0) {
146af62a557SLei Wen 			printf("Controller never released inhibit bit(s).\n");
147af62a557SLei Wen 			return COMM_ERR;
148af62a557SLei Wen 		}
149af62a557SLei Wen 		timeout--;
150af62a557SLei Wen 		udelay(1000);
151af62a557SLei Wen 	}
152af62a557SLei Wen 
153af62a557SLei Wen 	mask = SDHCI_INT_RESPONSE;
154af62a557SLei Wen 	if (!(cmd->resp_type & MMC_RSP_PRESENT))
155af62a557SLei Wen 		flags = SDHCI_CMD_RESP_NONE;
156af62a557SLei Wen 	else if (cmd->resp_type & MMC_RSP_136)
157af62a557SLei Wen 		flags = SDHCI_CMD_RESP_LONG;
158af62a557SLei Wen 	else if (cmd->resp_type & MMC_RSP_BUSY) {
159af62a557SLei Wen 		flags = SDHCI_CMD_RESP_SHORT_BUSY;
160af62a557SLei Wen 		mask |= SDHCI_INT_DATA_END;
161af62a557SLei Wen 	} else
162af62a557SLei Wen 		flags = SDHCI_CMD_RESP_SHORT;
163af62a557SLei Wen 
164af62a557SLei Wen 	if (cmd->resp_type & MMC_RSP_CRC)
165af62a557SLei Wen 		flags |= SDHCI_CMD_CRC;
166af62a557SLei Wen 	if (cmd->resp_type & MMC_RSP_OPCODE)
167af62a557SLei Wen 		flags |= SDHCI_CMD_INDEX;
168af62a557SLei Wen 	if (data)
169af62a557SLei Wen 		flags |= SDHCI_CMD_DATA;
170af62a557SLei Wen 
171af62a557SLei Wen 	/*Set Transfer mode regarding to data flag*/
172af62a557SLei Wen 	if (data != 0) {
173af62a557SLei Wen 		sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL);
174af62a557SLei Wen 		mode = SDHCI_TRNS_BLK_CNT_EN;
175af62a557SLei Wen 		trans_bytes = data->blocks * data->blocksize;
176af62a557SLei Wen 		if (data->blocks > 1)
177af62a557SLei Wen 			mode |= SDHCI_TRNS_MULTI;
178af62a557SLei Wen 
179af62a557SLei Wen 		if (data->flags == MMC_DATA_READ)
180af62a557SLei Wen 			mode |= SDHCI_TRNS_READ;
181af62a557SLei Wen 
182af62a557SLei Wen #ifdef CONFIG_MMC_SDMA
183af62a557SLei Wen 		if (data->flags == MMC_DATA_READ)
184af62a557SLei Wen 			start_addr = (unsigned int)data->dest;
185af62a557SLei Wen 		else
186af62a557SLei Wen 			start_addr = (unsigned int)data->src;
187af62a557SLei Wen 		if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
188af62a557SLei Wen 				(start_addr & 0x7) != 0x0) {
189af62a557SLei Wen 			is_aligned = 0;
190af62a557SLei Wen 			start_addr = (unsigned int)aligned_buffer;
191af62a557SLei Wen 			if (data->flags != MMC_DATA_READ)
192af62a557SLei Wen 				memcpy(aligned_buffer, data->src, trans_bytes);
193af62a557SLei Wen 		}
194af62a557SLei Wen 
195af62a557SLei Wen 		sdhci_writel(host, start_addr, SDHCI_DMA_ADDRESS);
196af62a557SLei Wen 		mode |= SDHCI_TRNS_DMA;
197af62a557SLei Wen #endif
198af62a557SLei Wen 		sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
199af62a557SLei Wen 				data->blocksize),
200af62a557SLei Wen 				SDHCI_BLOCK_SIZE);
201af62a557SLei Wen 		sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
202af62a557SLei Wen 		sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
203af62a557SLei Wen 	}
204af62a557SLei Wen 
205af62a557SLei Wen 	sdhci_writel(host, cmd->cmdarg, SDHCI_ARGUMENT);
206af62a557SLei Wen #ifdef CONFIG_MMC_SDMA
2072c2ec4c9SLei Wen 	flush_cache(start_addr, trans_bytes);
208af62a557SLei Wen #endif
209af62a557SLei Wen 	sdhci_writew(host, SDHCI_MAKE_CMD(cmd->cmdidx, flags), SDHCI_COMMAND);
210af62a557SLei Wen 	do {
211af62a557SLei Wen 		stat = sdhci_readl(host, SDHCI_INT_STATUS);
212af62a557SLei Wen 		if (stat & SDHCI_INT_ERROR)
213af62a557SLei Wen 			break;
2143a638320SJaehoon Chung 		if (--retry == 0)
2153a638320SJaehoon Chung 			break;
216af62a557SLei Wen 	} while ((stat & mask) != mask);
217af62a557SLei Wen 
2183a638320SJaehoon Chung 	if (retry == 0) {
2193a638320SJaehoon Chung 		if (host->quirks & SDHCI_QUIRK_BROKEN_R1B)
2203a638320SJaehoon Chung 			return 0;
2213a638320SJaehoon Chung 		else {
2223a638320SJaehoon Chung 			printf("Timeout for status update!\n");
2233a638320SJaehoon Chung 			return TIMEOUT;
2243a638320SJaehoon Chung 		}
2253a638320SJaehoon Chung 	}
2263a638320SJaehoon Chung 
227af62a557SLei Wen 	if ((stat & (SDHCI_INT_ERROR | mask)) == mask) {
228af62a557SLei Wen 		sdhci_cmd_done(host, cmd);
229af62a557SLei Wen 		sdhci_writel(host, mask, SDHCI_INT_STATUS);
230af62a557SLei Wen 	} else
231af62a557SLei Wen 		ret = -1;
232af62a557SLei Wen 
233af62a557SLei Wen 	if (!ret && data)
234af62a557SLei Wen 		ret = sdhci_transfer_data(host, data, start_addr);
235af62a557SLei Wen 
236af62a557SLei Wen 	stat = sdhci_readl(host, SDHCI_INT_STATUS);
237af62a557SLei Wen 	sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
238af62a557SLei Wen 	if (!ret) {
239af62a557SLei Wen 		if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
240af62a557SLei Wen 				!is_aligned && (data->flags == MMC_DATA_READ))
241af62a557SLei Wen 			memcpy(data->dest, aligned_buffer, trans_bytes);
242af62a557SLei Wen 		return 0;
243af62a557SLei Wen 	}
244af62a557SLei Wen 
245af62a557SLei Wen 	sdhci_reset(host, SDHCI_RESET_CMD);
246af62a557SLei Wen 	sdhci_reset(host, SDHCI_RESET_DATA);
247af62a557SLei Wen 	if (stat & SDHCI_INT_TIMEOUT)
248af62a557SLei Wen 		return TIMEOUT;
249af62a557SLei Wen 	else
250af62a557SLei Wen 		return COMM_ERR;
251af62a557SLei Wen }
252af62a557SLei Wen 
253af62a557SLei Wen static int sdhci_set_clock(struct mmc *mmc, unsigned int clock)
254af62a557SLei Wen {
255af62a557SLei Wen 	struct sdhci_host *host = (struct sdhci_host *)mmc->priv;
256af62a557SLei Wen 	unsigned int div, clk, timeout;
257af62a557SLei Wen 
258af62a557SLei Wen 	sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
259af62a557SLei Wen 
260af62a557SLei Wen 	if (clock == 0)
261af62a557SLei Wen 		return 0;
262af62a557SLei Wen 
263*073cfd1cSJoe Hershberger 	if ((host->version & SDHCI_SPEC_VER_MASK) >= SDHCI_SPEC_300) {
264af62a557SLei Wen 		/* Version 3.00 divisors must be a multiple of 2. */
265af62a557SLei Wen 		if (mmc->f_max <= clock)
266af62a557SLei Wen 			div = 1;
267af62a557SLei Wen 		else {
268af62a557SLei Wen 			for (div = 2; div < SDHCI_MAX_DIV_SPEC_300; div += 2) {
269af62a557SLei Wen 				if ((mmc->f_max / div) <= clock)
270af62a557SLei Wen 					break;
271af62a557SLei Wen 			}
272af62a557SLei Wen 		}
273af62a557SLei Wen 	} else {
274af62a557SLei Wen 		/* Version 2.00 divisors must be a power of 2. */
275af62a557SLei Wen 		for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
276af62a557SLei Wen 			if ((mmc->f_max / div) <= clock)
277af62a557SLei Wen 				break;
278af62a557SLei Wen 		}
279af62a557SLei Wen 	}
280af62a557SLei Wen 	div >>= 1;
281af62a557SLei Wen 
282af62a557SLei Wen 	clk = (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
283af62a557SLei Wen 	clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
284af62a557SLei Wen 		<< SDHCI_DIVIDER_HI_SHIFT;
285af62a557SLei Wen 	clk |= SDHCI_CLOCK_INT_EN;
286af62a557SLei Wen 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
287af62a557SLei Wen 
288af62a557SLei Wen 	/* Wait max 20 ms */
289af62a557SLei Wen 	timeout = 20;
290af62a557SLei Wen 	while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
291af62a557SLei Wen 		& SDHCI_CLOCK_INT_STABLE)) {
292af62a557SLei Wen 		if (timeout == 0) {
293af62a557SLei Wen 			printf("Internal clock never stabilised.\n");
294af62a557SLei Wen 			return -1;
295af62a557SLei Wen 		}
296af62a557SLei Wen 		timeout--;
297af62a557SLei Wen 		udelay(1000);
298af62a557SLei Wen 	}
299af62a557SLei Wen 
300af62a557SLei Wen 	clk |= SDHCI_CLOCK_CARD_EN;
301af62a557SLei Wen 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
302af62a557SLei Wen 	return 0;
303af62a557SLei Wen }
304af62a557SLei Wen 
305af62a557SLei Wen static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
306af62a557SLei Wen {
307af62a557SLei Wen 	u8 pwr = 0;
308af62a557SLei Wen 
309af62a557SLei Wen 	if (power != (unsigned short)-1) {
310af62a557SLei Wen 		switch (1 << power) {
311af62a557SLei Wen 		case MMC_VDD_165_195:
312af62a557SLei Wen 			pwr = SDHCI_POWER_180;
313af62a557SLei Wen 			break;
314af62a557SLei Wen 		case MMC_VDD_29_30:
315af62a557SLei Wen 		case MMC_VDD_30_31:
316af62a557SLei Wen 			pwr = SDHCI_POWER_300;
317af62a557SLei Wen 			break;
318af62a557SLei Wen 		case MMC_VDD_32_33:
319af62a557SLei Wen 		case MMC_VDD_33_34:
320af62a557SLei Wen 			pwr = SDHCI_POWER_330;
321af62a557SLei Wen 			break;
322af62a557SLei Wen 		}
323af62a557SLei Wen 	}
324af62a557SLei Wen 
325af62a557SLei Wen 	if (pwr == 0) {
326af62a557SLei Wen 		sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
327af62a557SLei Wen 		return;
328af62a557SLei Wen 	}
329af62a557SLei Wen 
330af62a557SLei Wen 	pwr |= SDHCI_POWER_ON;
331af62a557SLei Wen 
332af62a557SLei Wen 	sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
333af62a557SLei Wen }
334af62a557SLei Wen 
335af62a557SLei Wen void sdhci_set_ios(struct mmc *mmc)
336af62a557SLei Wen {
337af62a557SLei Wen 	u32 ctrl;
338af62a557SLei Wen 	struct sdhci_host *host = (struct sdhci_host *)mmc->priv;
339af62a557SLei Wen 
340236bfecfSJaehoon Chung 	if (host->set_control_reg)
341236bfecfSJaehoon Chung 		host->set_control_reg(host);
342236bfecfSJaehoon Chung 
343af62a557SLei Wen 	if (mmc->clock != host->clock)
344af62a557SLei Wen 		sdhci_set_clock(mmc, mmc->clock);
345af62a557SLei Wen 
346af62a557SLei Wen 	/* Set bus width */
347af62a557SLei Wen 	ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
348af62a557SLei Wen 	if (mmc->bus_width == 8) {
349af62a557SLei Wen 		ctrl &= ~SDHCI_CTRL_4BITBUS;
350*073cfd1cSJoe Hershberger 		if ((host->version & SDHCI_SPEC_VER_MASK) >= SDHCI_SPEC_300)
351af62a557SLei Wen 			ctrl |= SDHCI_CTRL_8BITBUS;
352af62a557SLei Wen 	} else {
353*073cfd1cSJoe Hershberger 		if ((host->version & SDHCI_SPEC_VER_MASK) >= SDHCI_SPEC_300)
354af62a557SLei Wen 			ctrl &= ~SDHCI_CTRL_8BITBUS;
355af62a557SLei Wen 		if (mmc->bus_width == 4)
356af62a557SLei Wen 			ctrl |= SDHCI_CTRL_4BITBUS;
357af62a557SLei Wen 		else
358af62a557SLei Wen 			ctrl &= ~SDHCI_CTRL_4BITBUS;
359af62a557SLei Wen 	}
360af62a557SLei Wen 
361af62a557SLei Wen 	if (mmc->clock > 26000000)
362af62a557SLei Wen 		ctrl |= SDHCI_CTRL_HISPD;
363af62a557SLei Wen 	else
364af62a557SLei Wen 		ctrl &= ~SDHCI_CTRL_HISPD;
365af62a557SLei Wen 
366236bfecfSJaehoon Chung 	if (host->quirks & SDHCI_QUIRK_NO_HISPD_BIT)
367236bfecfSJaehoon Chung 		ctrl &= ~SDHCI_CTRL_HISPD;
368236bfecfSJaehoon Chung 
369af62a557SLei Wen 	sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
370af62a557SLei Wen }
371af62a557SLei Wen 
372af62a557SLei Wen int sdhci_init(struct mmc *mmc)
373af62a557SLei Wen {
374af62a557SLei Wen 	struct sdhci_host *host = (struct sdhci_host *)mmc->priv;
375af62a557SLei Wen 
376af62a557SLei Wen 	if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) && !aligned_buffer) {
377af62a557SLei Wen 		aligned_buffer = memalign(8, 512*1024);
378af62a557SLei Wen 		if (!aligned_buffer) {
379af62a557SLei Wen 			printf("Aligned buffer alloc failed!!!");
380af62a557SLei Wen 			return -1;
381af62a557SLei Wen 		}
382af62a557SLei Wen 	}
383af62a557SLei Wen 
384af62a557SLei Wen 	/* Eable all state */
385af62a557SLei Wen 	sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_ENABLE);
386af62a557SLei Wen 	sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_SIGNAL_ENABLE);
387af62a557SLei Wen 
388af62a557SLei Wen 	sdhci_set_power(host, fls(mmc->voltages) - 1);
389af62a557SLei Wen 
390af62a557SLei Wen 	return 0;
391af62a557SLei Wen }
392af62a557SLei Wen 
393af62a557SLei Wen int add_sdhci(struct sdhci_host *host, u32 max_clk, u32 min_clk)
394af62a557SLei Wen {
395af62a557SLei Wen 	struct mmc *mmc;
396af62a557SLei Wen 	unsigned int caps;
397af62a557SLei Wen 
398af62a557SLei Wen 	mmc = malloc(sizeof(struct mmc));
399af62a557SLei Wen 	if (!mmc) {
400af62a557SLei Wen 		printf("mmc malloc fail!\n");
401af62a557SLei Wen 		return -1;
402af62a557SLei Wen 	}
403af62a557SLei Wen 
404af62a557SLei Wen 	mmc->priv = host;
4056cf1b17cSLei Wen 	host->mmc = mmc;
406af62a557SLei Wen 
407af62a557SLei Wen 	sprintf(mmc->name, "%s", host->name);
408af62a557SLei Wen 	mmc->send_cmd = sdhci_send_command;
409af62a557SLei Wen 	mmc->set_ios = sdhci_set_ios;
410af62a557SLei Wen 	mmc->init = sdhci_init;
41148972d90SThierry Reding 	mmc->getcd = NULL;
412af62a557SLei Wen 
413af62a557SLei Wen 	caps = sdhci_readl(host, SDHCI_CAPABILITIES);
414af62a557SLei Wen #ifdef CONFIG_MMC_SDMA
415af62a557SLei Wen 	if (!(caps & SDHCI_CAN_DO_SDMA)) {
416af62a557SLei Wen 		printf("Your controller don't support sdma!!\n");
417af62a557SLei Wen 		return -1;
418af62a557SLei Wen 	}
419af62a557SLei Wen #endif
420af62a557SLei Wen 
421af62a557SLei Wen 	if (max_clk)
422af62a557SLei Wen 		mmc->f_max = max_clk;
423af62a557SLei Wen 	else {
424*073cfd1cSJoe Hershberger 		if ((host->version & SDHCI_SPEC_VER_MASK) >= SDHCI_SPEC_300)
425af62a557SLei Wen 			mmc->f_max = (caps & SDHCI_CLOCK_V3_BASE_MASK)
426af62a557SLei Wen 				>> SDHCI_CLOCK_BASE_SHIFT;
427af62a557SLei Wen 		else
428af62a557SLei Wen 			mmc->f_max = (caps & SDHCI_CLOCK_BASE_MASK)
429af62a557SLei Wen 				>> SDHCI_CLOCK_BASE_SHIFT;
430af62a557SLei Wen 		mmc->f_max *= 1000000;
431af62a557SLei Wen 	}
432af62a557SLei Wen 	if (mmc->f_max == 0) {
433af62a557SLei Wen 		printf("Hardware doesn't specify base clock frequency\n");
434af62a557SLei Wen 		return -1;
435af62a557SLei Wen 	}
436af62a557SLei Wen 	if (min_clk)
437af62a557SLei Wen 		mmc->f_min = min_clk;
438af62a557SLei Wen 	else {
439*073cfd1cSJoe Hershberger 		if ((host->version & SDHCI_SPEC_VER_MASK) >= SDHCI_SPEC_300)
440af62a557SLei Wen 			mmc->f_min = mmc->f_max / SDHCI_MAX_DIV_SPEC_300;
441af62a557SLei Wen 		else
442af62a557SLei Wen 			mmc->f_min = mmc->f_max / SDHCI_MAX_DIV_SPEC_200;
443af62a557SLei Wen 	}
444af62a557SLei Wen 
445af62a557SLei Wen 	mmc->voltages = 0;
446af62a557SLei Wen 	if (caps & SDHCI_CAN_VDD_330)
447af62a557SLei Wen 		mmc->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
448af62a557SLei Wen 	if (caps & SDHCI_CAN_VDD_300)
449af62a557SLei Wen 		mmc->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
450af62a557SLei Wen 	if (caps & SDHCI_CAN_VDD_180)
451af62a557SLei Wen 		mmc->voltages |= MMC_VDD_165_195;
452236bfecfSJaehoon Chung 
453236bfecfSJaehoon Chung 	if (host->quirks & SDHCI_QUIRK_BROKEN_VOLTAGE)
454236bfecfSJaehoon Chung 		mmc->voltages |= host->voltages;
455236bfecfSJaehoon Chung 
456af62a557SLei Wen 	mmc->host_caps = MMC_MODE_HS | MMC_MODE_HS_52MHz | MMC_MODE_4BIT;
457af62a557SLei Wen 	if (caps & SDHCI_CAN_DO_8BIT)
458af62a557SLei Wen 		mmc->host_caps |= MMC_MODE_8BIT;
459236bfecfSJaehoon Chung 	if (host->host_caps)
460236bfecfSJaehoon Chung 		mmc->host_caps |= host->host_caps;
461af62a557SLei Wen 
462af62a557SLei Wen 	sdhci_reset(host, SDHCI_RESET_ALL);
463af62a557SLei Wen 	mmc_register(mmc);
464af62a557SLei Wen 
465af62a557SLei Wen 	return 0;
466af62a557SLei Wen }
467