xref: /openbmc/u-boot/drivers/mmc/mmc.c (revision def412b6618f5b887b80fcdad6ab4ee2fee0a110)
1272cc70bSAndy Fleming /*
2272cc70bSAndy Fleming  * Copyright 2008, Freescale Semiconductor, Inc
3272cc70bSAndy Fleming  * Andy Fleming
4272cc70bSAndy Fleming  *
5272cc70bSAndy Fleming  * Based vaguely on the Linux code
6272cc70bSAndy Fleming  *
7272cc70bSAndy Fleming  * See file CREDITS for list of people who contributed to this
8272cc70bSAndy Fleming  * project.
9272cc70bSAndy Fleming  *
10272cc70bSAndy Fleming  * This program is free software; you can redistribute it and/or
11272cc70bSAndy Fleming  * modify it under the terms of the GNU General Public License as
12272cc70bSAndy Fleming  * published by the Free Software Foundation; either version 2 of
13272cc70bSAndy Fleming  * the License, or (at your option) any later version.
14272cc70bSAndy Fleming  *
15272cc70bSAndy Fleming  * This program is distributed in the hope that it will be useful,
16272cc70bSAndy Fleming  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17272cc70bSAndy Fleming  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18272cc70bSAndy Fleming  * GNU General Public License for more details.
19272cc70bSAndy Fleming  *
20272cc70bSAndy Fleming  * You should have received a copy of the GNU General Public License
21272cc70bSAndy Fleming  * along with this program; if not, write to the Free Software
22272cc70bSAndy Fleming  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23272cc70bSAndy Fleming  * MA 02111-1307 USA
24272cc70bSAndy Fleming  */
25272cc70bSAndy Fleming 
26272cc70bSAndy Fleming #include <config.h>
27272cc70bSAndy Fleming #include <common.h>
28272cc70bSAndy Fleming #include <command.h>
29272cc70bSAndy Fleming #include <mmc.h>
30272cc70bSAndy Fleming #include <part.h>
31272cc70bSAndy Fleming #include <malloc.h>
32272cc70bSAndy Fleming #include <linux/list.h>
33272cc70bSAndy Fleming #include <mmc.h>
349b1f942cSRabin Vincent #include <div64.h>
35272cc70bSAndy Fleming 
36272cc70bSAndy Fleming static struct list_head mmc_devices;
37272cc70bSAndy Fleming static int cur_dev_num = -1;
38272cc70bSAndy Fleming 
3911fdade2SStefano Babic int __board_mmc_getcd(u8 *cd, struct mmc *mmc) {
4011fdade2SStefano Babic 	return -1;
4111fdade2SStefano Babic }
4211fdade2SStefano Babic 
4311fdade2SStefano Babic int board_mmc_getcd(u8 *cd, struct mmc *mmc)__attribute__((weak,
4411fdade2SStefano Babic 	alias("__board_mmc_getcd")));
4511fdade2SStefano Babic 
46272cc70bSAndy Fleming int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
47272cc70bSAndy Fleming {
48272cc70bSAndy Fleming 	return mmc->send_cmd(mmc, cmd, data);
49272cc70bSAndy Fleming }
50272cc70bSAndy Fleming 
51272cc70bSAndy Fleming int mmc_set_blocklen(struct mmc *mmc, int len)
52272cc70bSAndy Fleming {
53272cc70bSAndy Fleming 	struct mmc_cmd cmd;
54272cc70bSAndy Fleming 
55272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
56272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
57272cc70bSAndy Fleming 	cmd.cmdarg = len;
58272cc70bSAndy Fleming 	cmd.flags = 0;
59272cc70bSAndy Fleming 
60272cc70bSAndy Fleming 	return mmc_send_cmd(mmc, &cmd, NULL);
61272cc70bSAndy Fleming }
62272cc70bSAndy Fleming 
63272cc70bSAndy Fleming struct mmc *find_mmc_device(int dev_num)
64272cc70bSAndy Fleming {
65272cc70bSAndy Fleming 	struct mmc *m;
66272cc70bSAndy Fleming 	struct list_head *entry;
67272cc70bSAndy Fleming 
68272cc70bSAndy Fleming 	list_for_each(entry, &mmc_devices) {
69272cc70bSAndy Fleming 		m = list_entry(entry, struct mmc, link);
70272cc70bSAndy Fleming 
71272cc70bSAndy Fleming 		if (m->block_dev.dev == dev_num)
72272cc70bSAndy Fleming 			return m;
73272cc70bSAndy Fleming 	}
74272cc70bSAndy Fleming 
75272cc70bSAndy Fleming 	printf("MMC Device %d not found\n", dev_num);
76272cc70bSAndy Fleming 
77272cc70bSAndy Fleming 	return NULL;
78272cc70bSAndy Fleming }
79272cc70bSAndy Fleming 
80272cc70bSAndy Fleming static ulong
810158126eSLei Wen mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
82272cc70bSAndy Fleming {
83272cc70bSAndy Fleming 	struct mmc_cmd cmd;
84272cc70bSAndy Fleming 	struct mmc_data data;
85272cc70bSAndy Fleming 
86d2bf29e3SLei Wen 	if ((start + blkcnt) > mmc->block_dev.lba) {
87*def412b6SSteve Sakoman 		printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
88d2bf29e3SLei Wen 			start + blkcnt, mmc->block_dev.lba);
89d2bf29e3SLei Wen 		return 0;
90d2bf29e3SLei Wen 	}
91272cc70bSAndy Fleming 
92272cc70bSAndy Fleming 	if (blkcnt > 1)
93272cc70bSAndy Fleming 		cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
94272cc70bSAndy Fleming 	else
95272cc70bSAndy Fleming 		cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
96272cc70bSAndy Fleming 
97272cc70bSAndy Fleming 	if (mmc->high_capacity)
98272cc70bSAndy Fleming 		cmd.cmdarg = start;
99272cc70bSAndy Fleming 	else
100*def412b6SSteve Sakoman 		cmd.cmdarg = start * mmc->write_bl_len;
101272cc70bSAndy Fleming 
102272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
103272cc70bSAndy Fleming 	cmd.flags = 0;
104272cc70bSAndy Fleming 
105272cc70bSAndy Fleming 	data.src = src;
106272cc70bSAndy Fleming 	data.blocks = blkcnt;
107*def412b6SSteve Sakoman 	data.blocksize = mmc->write_bl_len;
108272cc70bSAndy Fleming 	data.flags = MMC_DATA_WRITE;
109272cc70bSAndy Fleming 
110*def412b6SSteve Sakoman 	if (mmc_send_cmd(mmc, &cmd, &data)) {
111*def412b6SSteve Sakoman 		printf("mmc write failed\n");
112*def412b6SSteve Sakoman 		return 0;
113272cc70bSAndy Fleming 	}
114272cc70bSAndy Fleming 
115272cc70bSAndy Fleming 	if (blkcnt > 1) {
116272cc70bSAndy Fleming 		cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
117272cc70bSAndy Fleming 		cmd.cmdarg = 0;
118272cc70bSAndy Fleming 		cmd.resp_type = MMC_RSP_R1b;
119272cc70bSAndy Fleming 		cmd.flags = 0;
120*def412b6SSteve Sakoman 		if (mmc_send_cmd(mmc, &cmd, NULL)) {
121*def412b6SSteve Sakoman 			printf("mmc fail to send stop cmd\n");
122*def412b6SSteve Sakoman 			return 0;
123272cc70bSAndy Fleming 		}
1240158126eSLei Wen 	}
1250158126eSLei Wen 
1260158126eSLei Wen 	return blkcnt;
1270158126eSLei Wen }
1280158126eSLei Wen 
1290158126eSLei Wen static ulong
1300158126eSLei Wen mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
1310158126eSLei Wen {
1320158126eSLei Wen 	lbaint_t cur, blocks_todo = blkcnt;
1330158126eSLei Wen 
134*def412b6SSteve Sakoman 	struct mmc *mmc = find_mmc_device(dev_num);
1350158126eSLei Wen 	if (!mmc)
136*def412b6SSteve Sakoman 		return 0;
1370158126eSLei Wen 
138*def412b6SSteve Sakoman 	if (mmc_set_blocklen(mmc, mmc->write_bl_len))
139*def412b6SSteve Sakoman 		return 0;
1400158126eSLei Wen 
1410158126eSLei Wen 	do {
1420158126eSLei Wen 		/*
1430158126eSLei Wen 		 * The 65535 constraint comes from some hardware has
1440158126eSLei Wen 		 * only 16 bit width block number counter
1450158126eSLei Wen 		 */
1460158126eSLei Wen 		cur = (blocks_todo > 65535) ? 65535 : blocks_todo;
1470158126eSLei Wen 		if(mmc_write_blocks(mmc, start, cur, src) != cur)
148*def412b6SSteve Sakoman 			return 0;
1490158126eSLei Wen 		blocks_todo -= cur;
1500158126eSLei Wen 		start += cur;
1510158126eSLei Wen 		src += cur * mmc->write_bl_len;
1520158126eSLei Wen 	} while (blocks_todo > 0);
153272cc70bSAndy Fleming 
154272cc70bSAndy Fleming 	return blkcnt;
155272cc70bSAndy Fleming }
156272cc70bSAndy Fleming 
157272cc70bSAndy Fleming int mmc_read_block(struct mmc *mmc, void *dst, uint blocknum)
158272cc70bSAndy Fleming {
159272cc70bSAndy Fleming 	struct mmc_cmd cmd;
160272cc70bSAndy Fleming 	struct mmc_data data;
161272cc70bSAndy Fleming 
162272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
163272cc70bSAndy Fleming 
164272cc70bSAndy Fleming 	if (mmc->high_capacity)
165272cc70bSAndy Fleming 		cmd.cmdarg = blocknum;
166272cc70bSAndy Fleming 	else
167272cc70bSAndy Fleming 		cmd.cmdarg = blocknum * mmc->read_bl_len;
168272cc70bSAndy Fleming 
169272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
170272cc70bSAndy Fleming 	cmd.flags = 0;
171272cc70bSAndy Fleming 
172272cc70bSAndy Fleming 	data.dest = dst;
173272cc70bSAndy Fleming 	data.blocks = 1;
174272cc70bSAndy Fleming 	data.blocksize = mmc->read_bl_len;
175272cc70bSAndy Fleming 	data.flags = MMC_DATA_READ;
176272cc70bSAndy Fleming 
177272cc70bSAndy Fleming 	return mmc_send_cmd(mmc, &cmd, &data);
178272cc70bSAndy Fleming }
179272cc70bSAndy Fleming 
180272cc70bSAndy Fleming int mmc_read(struct mmc *mmc, u64 src, uchar *dst, int size)
181272cc70bSAndy Fleming {
182272cc70bSAndy Fleming 	char *buffer;
183272cc70bSAndy Fleming 	int i;
184272cc70bSAndy Fleming 	int blklen = mmc->read_bl_len;
1859b1f942cSRabin Vincent 	int startblock = lldiv(src, mmc->read_bl_len);
1869b1f942cSRabin Vincent 	int endblock = lldiv(src + size - 1, mmc->read_bl_len);
187272cc70bSAndy Fleming 	int err = 0;
188272cc70bSAndy Fleming 
189272cc70bSAndy Fleming 	/* Make a buffer big enough to hold all the blocks we might read */
190272cc70bSAndy Fleming 	buffer = malloc(blklen);
191272cc70bSAndy Fleming 
192272cc70bSAndy Fleming 	if (!buffer) {
193272cc70bSAndy Fleming 		printf("Could not allocate buffer for MMC read!\n");
194272cc70bSAndy Fleming 		return -1;
195272cc70bSAndy Fleming 	}
196272cc70bSAndy Fleming 
197272cc70bSAndy Fleming 	/* We always do full block reads from the card */
198272cc70bSAndy Fleming 	err = mmc_set_blocklen(mmc, mmc->read_bl_len);
199272cc70bSAndy Fleming 
200272cc70bSAndy Fleming 	if (err)
2018c4444ffSWolfgang Denk 		goto free_buffer;
202272cc70bSAndy Fleming 
203272cc70bSAndy Fleming 	for (i = startblock; i <= endblock; i++) {
204272cc70bSAndy Fleming 		int segment_size;
205272cc70bSAndy Fleming 		int offset;
206272cc70bSAndy Fleming 
207272cc70bSAndy Fleming 		err = mmc_read_block(mmc, buffer, i);
208272cc70bSAndy Fleming 
209272cc70bSAndy Fleming 		if (err)
210272cc70bSAndy Fleming 			goto free_buffer;
211272cc70bSAndy Fleming 
212272cc70bSAndy Fleming 		/*
213272cc70bSAndy Fleming 		 * The first block may not be aligned, so we
214272cc70bSAndy Fleming 		 * copy from the desired point in the block
215272cc70bSAndy Fleming 		 */
216272cc70bSAndy Fleming 		offset = (src & (blklen - 1));
217272cc70bSAndy Fleming 		segment_size = MIN(blklen - offset, size);
218272cc70bSAndy Fleming 
219272cc70bSAndy Fleming 		memcpy(dst, buffer + offset, segment_size);
220272cc70bSAndy Fleming 
221272cc70bSAndy Fleming 		dst += segment_size;
222272cc70bSAndy Fleming 		src += segment_size;
223272cc70bSAndy Fleming 		size -= segment_size;
224272cc70bSAndy Fleming 	}
225272cc70bSAndy Fleming 
226272cc70bSAndy Fleming free_buffer:
227272cc70bSAndy Fleming 	free(buffer);
228272cc70bSAndy Fleming 
229272cc70bSAndy Fleming 	return err;
230272cc70bSAndy Fleming }
231272cc70bSAndy Fleming 
232272cc70bSAndy Fleming static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
233272cc70bSAndy Fleming {
234272cc70bSAndy Fleming 	int err;
235272cc70bSAndy Fleming 	int i;
236272cc70bSAndy Fleming 	struct mmc *mmc = find_mmc_device(dev_num);
237272cc70bSAndy Fleming 
238272cc70bSAndy Fleming 	if (!mmc)
239272cc70bSAndy Fleming 		return 0;
240272cc70bSAndy Fleming 
241d2bf29e3SLei Wen 	if ((start + blkcnt) > mmc->block_dev.lba) {
242fe64fd42SWolfgang Denk 		printf("MMC: block number 0x%lx exceeds max(0x%lx)",
243d2bf29e3SLei Wen 			start + blkcnt, mmc->block_dev.lba);
244d2bf29e3SLei Wen 		return 0;
245d2bf29e3SLei Wen 	}
246272cc70bSAndy Fleming 	/* We always do full block reads from the card */
247272cc70bSAndy Fleming 	err = mmc_set_blocklen(mmc, mmc->read_bl_len);
248272cc70bSAndy Fleming 
249272cc70bSAndy Fleming 	if (err) {
250272cc70bSAndy Fleming 		return 0;
251272cc70bSAndy Fleming 	}
252272cc70bSAndy Fleming 
253272cc70bSAndy Fleming 	for (i = start; i < start + blkcnt; i++, dst += mmc->read_bl_len) {
254272cc70bSAndy Fleming 		err = mmc_read_block(mmc, dst, i);
255272cc70bSAndy Fleming 
256272cc70bSAndy Fleming 		if (err) {
257272cc70bSAndy Fleming 			printf("block read failed: %d\n", err);
258272cc70bSAndy Fleming 			return i - start;
259272cc70bSAndy Fleming 		}
260272cc70bSAndy Fleming 	}
261272cc70bSAndy Fleming 
262272cc70bSAndy Fleming 	return blkcnt;
263272cc70bSAndy Fleming }
264272cc70bSAndy Fleming 
265272cc70bSAndy Fleming int mmc_go_idle(struct mmc* mmc)
266272cc70bSAndy Fleming {
267272cc70bSAndy Fleming 	struct mmc_cmd cmd;
268272cc70bSAndy Fleming 	int err;
269272cc70bSAndy Fleming 
270272cc70bSAndy Fleming 	udelay(1000);
271272cc70bSAndy Fleming 
272272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
273272cc70bSAndy Fleming 	cmd.cmdarg = 0;
274272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_NONE;
275272cc70bSAndy Fleming 	cmd.flags = 0;
276272cc70bSAndy Fleming 
277272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
278272cc70bSAndy Fleming 
279272cc70bSAndy Fleming 	if (err)
280272cc70bSAndy Fleming 		return err;
281272cc70bSAndy Fleming 
282272cc70bSAndy Fleming 	udelay(2000);
283272cc70bSAndy Fleming 
284272cc70bSAndy Fleming 	return 0;
285272cc70bSAndy Fleming }
286272cc70bSAndy Fleming 
287272cc70bSAndy Fleming int
288272cc70bSAndy Fleming sd_send_op_cond(struct mmc *mmc)
289272cc70bSAndy Fleming {
290272cc70bSAndy Fleming 	int timeout = 1000;
291272cc70bSAndy Fleming 	int err;
292272cc70bSAndy Fleming 	struct mmc_cmd cmd;
293272cc70bSAndy Fleming 
294272cc70bSAndy Fleming 	do {
295272cc70bSAndy Fleming 		cmd.cmdidx = MMC_CMD_APP_CMD;
296272cc70bSAndy Fleming 		cmd.resp_type = MMC_RSP_R1;
297272cc70bSAndy Fleming 		cmd.cmdarg = 0;
298272cc70bSAndy Fleming 		cmd.flags = 0;
299272cc70bSAndy Fleming 
300272cc70bSAndy Fleming 		err = mmc_send_cmd(mmc, &cmd, NULL);
301272cc70bSAndy Fleming 
302272cc70bSAndy Fleming 		if (err)
303272cc70bSAndy Fleming 			return err;
304272cc70bSAndy Fleming 
305272cc70bSAndy Fleming 		cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
306272cc70bSAndy Fleming 		cmd.resp_type = MMC_RSP_R3;
307250de12bSStefano Babic 
308250de12bSStefano Babic 		/*
309250de12bSStefano Babic 		 * Most cards do not answer if some reserved bits
310250de12bSStefano Babic 		 * in the ocr are set. However, Some controller
311250de12bSStefano Babic 		 * can set bit 7 (reserved for low voltages), but
312250de12bSStefano Babic 		 * how to manage low voltages SD card is not yet
313250de12bSStefano Babic 		 * specified.
314250de12bSStefano Babic 		 */
315250de12bSStefano Babic 		cmd.cmdarg = mmc->voltages & 0xff8000;
316272cc70bSAndy Fleming 
317272cc70bSAndy Fleming 		if (mmc->version == SD_VERSION_2)
318272cc70bSAndy Fleming 			cmd.cmdarg |= OCR_HCS;
319272cc70bSAndy Fleming 
320272cc70bSAndy Fleming 		err = mmc_send_cmd(mmc, &cmd, NULL);
321272cc70bSAndy Fleming 
322272cc70bSAndy Fleming 		if (err)
323272cc70bSAndy Fleming 			return err;
324272cc70bSAndy Fleming 
325272cc70bSAndy Fleming 		udelay(1000);
326272cc70bSAndy Fleming 	} while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
327272cc70bSAndy Fleming 
328272cc70bSAndy Fleming 	if (timeout <= 0)
329272cc70bSAndy Fleming 		return UNUSABLE_ERR;
330272cc70bSAndy Fleming 
331272cc70bSAndy Fleming 	if (mmc->version != SD_VERSION_2)
332272cc70bSAndy Fleming 		mmc->version = SD_VERSION_1_0;
333272cc70bSAndy Fleming 
334998be3ddSRabin Vincent 	mmc->ocr = cmd.response[0];
335272cc70bSAndy Fleming 
336272cc70bSAndy Fleming 	mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
337272cc70bSAndy Fleming 	mmc->rca = 0;
338272cc70bSAndy Fleming 
339272cc70bSAndy Fleming 	return 0;
340272cc70bSAndy Fleming }
341272cc70bSAndy Fleming 
342272cc70bSAndy Fleming int mmc_send_op_cond(struct mmc *mmc)
343272cc70bSAndy Fleming {
344272cc70bSAndy Fleming 	int timeout = 1000;
345272cc70bSAndy Fleming 	struct mmc_cmd cmd;
346272cc70bSAndy Fleming 	int err;
347272cc70bSAndy Fleming 
348272cc70bSAndy Fleming 	/* Some cards seem to need this */
349272cc70bSAndy Fleming 	mmc_go_idle(mmc);
350272cc70bSAndy Fleming 
351272cc70bSAndy Fleming 	do {
352272cc70bSAndy Fleming 		cmd.cmdidx = MMC_CMD_SEND_OP_COND;
353272cc70bSAndy Fleming 		cmd.resp_type = MMC_RSP_R3;
354272cc70bSAndy Fleming 		cmd.cmdarg = OCR_HCS | mmc->voltages;
355272cc70bSAndy Fleming 		cmd.flags = 0;
356272cc70bSAndy Fleming 
357272cc70bSAndy Fleming 		err = mmc_send_cmd(mmc, &cmd, NULL);
358272cc70bSAndy Fleming 
359272cc70bSAndy Fleming 		if (err)
360272cc70bSAndy Fleming 			return err;
361272cc70bSAndy Fleming 
362272cc70bSAndy Fleming 		udelay(1000);
363272cc70bSAndy Fleming 	} while (!(cmd.response[0] & OCR_BUSY) && timeout--);
364272cc70bSAndy Fleming 
365272cc70bSAndy Fleming 	if (timeout <= 0)
366272cc70bSAndy Fleming 		return UNUSABLE_ERR;
367272cc70bSAndy Fleming 
368272cc70bSAndy Fleming 	mmc->version = MMC_VERSION_UNKNOWN;
369998be3ddSRabin Vincent 	mmc->ocr = cmd.response[0];
370272cc70bSAndy Fleming 
371272cc70bSAndy Fleming 	mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
372272cc70bSAndy Fleming 	mmc->rca = 0;
373272cc70bSAndy Fleming 
374272cc70bSAndy Fleming 	return 0;
375272cc70bSAndy Fleming }
376272cc70bSAndy Fleming 
377272cc70bSAndy Fleming 
378272cc70bSAndy Fleming int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd)
379272cc70bSAndy Fleming {
380272cc70bSAndy Fleming 	struct mmc_cmd cmd;
381272cc70bSAndy Fleming 	struct mmc_data data;
382272cc70bSAndy Fleming 	int err;
383272cc70bSAndy Fleming 
384272cc70bSAndy Fleming 	/* Get the Card Status Register */
385272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
386272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
387272cc70bSAndy Fleming 	cmd.cmdarg = 0;
388272cc70bSAndy Fleming 	cmd.flags = 0;
389272cc70bSAndy Fleming 
390272cc70bSAndy Fleming 	data.dest = ext_csd;
391272cc70bSAndy Fleming 	data.blocks = 1;
392272cc70bSAndy Fleming 	data.blocksize = 512;
393272cc70bSAndy Fleming 	data.flags = MMC_DATA_READ;
394272cc70bSAndy Fleming 
395272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, &data);
396272cc70bSAndy Fleming 
397272cc70bSAndy Fleming 	return err;
398272cc70bSAndy Fleming }
399272cc70bSAndy Fleming 
400272cc70bSAndy Fleming 
401272cc70bSAndy Fleming int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
402272cc70bSAndy Fleming {
403272cc70bSAndy Fleming 	struct mmc_cmd cmd;
404272cc70bSAndy Fleming 
405272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SWITCH;
406272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1b;
407272cc70bSAndy Fleming 	cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
408272cc70bSAndy Fleming 		(index << 16) |
409272cc70bSAndy Fleming 		(value << 8);
410272cc70bSAndy Fleming 	cmd.flags = 0;
411272cc70bSAndy Fleming 
412272cc70bSAndy Fleming 	return mmc_send_cmd(mmc, &cmd, NULL);
413272cc70bSAndy Fleming }
414272cc70bSAndy Fleming 
415272cc70bSAndy Fleming int mmc_change_freq(struct mmc *mmc)
416272cc70bSAndy Fleming {
417272cc70bSAndy Fleming 	char ext_csd[512];
418272cc70bSAndy Fleming 	char cardtype;
419272cc70bSAndy Fleming 	int err;
420272cc70bSAndy Fleming 
421272cc70bSAndy Fleming 	mmc->card_caps = 0;
422272cc70bSAndy Fleming 
423272cc70bSAndy Fleming 	/* Only version 4 supports high-speed */
424272cc70bSAndy Fleming 	if (mmc->version < MMC_VERSION_4)
425272cc70bSAndy Fleming 		return 0;
426272cc70bSAndy Fleming 
427272cc70bSAndy Fleming 	mmc->card_caps |= MMC_MODE_4BIT;
428272cc70bSAndy Fleming 
429272cc70bSAndy Fleming 	err = mmc_send_ext_csd(mmc, ext_csd);
430272cc70bSAndy Fleming 
431272cc70bSAndy Fleming 	if (err)
432272cc70bSAndy Fleming 		return err;
433272cc70bSAndy Fleming 
434272cc70bSAndy Fleming 	if (ext_csd[212] || ext_csd[213] || ext_csd[214] || ext_csd[215])
435272cc70bSAndy Fleming 		mmc->high_capacity = 1;
436272cc70bSAndy Fleming 
437272cc70bSAndy Fleming 	cardtype = ext_csd[196] & 0xf;
438272cc70bSAndy Fleming 
439272cc70bSAndy Fleming 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
440272cc70bSAndy Fleming 
441272cc70bSAndy Fleming 	if (err)
442272cc70bSAndy Fleming 		return err;
443272cc70bSAndy Fleming 
444272cc70bSAndy Fleming 	/* Now check to see that it worked */
445272cc70bSAndy Fleming 	err = mmc_send_ext_csd(mmc, ext_csd);
446272cc70bSAndy Fleming 
447272cc70bSAndy Fleming 	if (err)
448272cc70bSAndy Fleming 		return err;
449272cc70bSAndy Fleming 
450272cc70bSAndy Fleming 	/* No high-speed support */
451272cc70bSAndy Fleming 	if (!ext_csd[185])
452272cc70bSAndy Fleming 		return 0;
453272cc70bSAndy Fleming 
454272cc70bSAndy Fleming 	/* High Speed is set, there are two types: 52MHz and 26MHz */
455272cc70bSAndy Fleming 	if (cardtype & MMC_HS_52MHZ)
456272cc70bSAndy Fleming 		mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
457272cc70bSAndy Fleming 	else
458272cc70bSAndy Fleming 		mmc->card_caps |= MMC_MODE_HS;
459272cc70bSAndy Fleming 
460272cc70bSAndy Fleming 	return 0;
461272cc70bSAndy Fleming }
462272cc70bSAndy Fleming 
463272cc70bSAndy Fleming int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
464272cc70bSAndy Fleming {
465272cc70bSAndy Fleming 	struct mmc_cmd cmd;
466272cc70bSAndy Fleming 	struct mmc_data data;
467272cc70bSAndy Fleming 
468272cc70bSAndy Fleming 	/* Switch the frequency */
469272cc70bSAndy Fleming 	cmd.cmdidx = SD_CMD_SWITCH_FUNC;
470272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
471272cc70bSAndy Fleming 	cmd.cmdarg = (mode << 31) | 0xffffff;
472272cc70bSAndy Fleming 	cmd.cmdarg &= ~(0xf << (group * 4));
473272cc70bSAndy Fleming 	cmd.cmdarg |= value << (group * 4);
474272cc70bSAndy Fleming 	cmd.flags = 0;
475272cc70bSAndy Fleming 
476272cc70bSAndy Fleming 	data.dest = (char *)resp;
477272cc70bSAndy Fleming 	data.blocksize = 64;
478272cc70bSAndy Fleming 	data.blocks = 1;
479272cc70bSAndy Fleming 	data.flags = MMC_DATA_READ;
480272cc70bSAndy Fleming 
481272cc70bSAndy Fleming 	return mmc_send_cmd(mmc, &cmd, &data);
482272cc70bSAndy Fleming }
483272cc70bSAndy Fleming 
484272cc70bSAndy Fleming 
485272cc70bSAndy Fleming int sd_change_freq(struct mmc *mmc)
486272cc70bSAndy Fleming {
487272cc70bSAndy Fleming 	int err;
488272cc70bSAndy Fleming 	struct mmc_cmd cmd;
489272cc70bSAndy Fleming 	uint scr[2];
490272cc70bSAndy Fleming 	uint switch_status[16];
491272cc70bSAndy Fleming 	struct mmc_data data;
492272cc70bSAndy Fleming 	int timeout;
493272cc70bSAndy Fleming 
494272cc70bSAndy Fleming 	mmc->card_caps = 0;
495272cc70bSAndy Fleming 
496272cc70bSAndy Fleming 	/* Read the SCR to find out if this card supports higher speeds */
497272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_APP_CMD;
498272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
499272cc70bSAndy Fleming 	cmd.cmdarg = mmc->rca << 16;
500272cc70bSAndy Fleming 	cmd.flags = 0;
501272cc70bSAndy Fleming 
502272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
503272cc70bSAndy Fleming 
504272cc70bSAndy Fleming 	if (err)
505272cc70bSAndy Fleming 		return err;
506272cc70bSAndy Fleming 
507272cc70bSAndy Fleming 	cmd.cmdidx = SD_CMD_APP_SEND_SCR;
508272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
509272cc70bSAndy Fleming 	cmd.cmdarg = 0;
510272cc70bSAndy Fleming 	cmd.flags = 0;
511272cc70bSAndy Fleming 
512272cc70bSAndy Fleming 	timeout = 3;
513272cc70bSAndy Fleming 
514272cc70bSAndy Fleming retry_scr:
515272cc70bSAndy Fleming 	data.dest = (char *)&scr;
516272cc70bSAndy Fleming 	data.blocksize = 8;
517272cc70bSAndy Fleming 	data.blocks = 1;
518272cc70bSAndy Fleming 	data.flags = MMC_DATA_READ;
519272cc70bSAndy Fleming 
520272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, &data);
521272cc70bSAndy Fleming 
522272cc70bSAndy Fleming 	if (err) {
523272cc70bSAndy Fleming 		if (timeout--)
524272cc70bSAndy Fleming 			goto retry_scr;
525272cc70bSAndy Fleming 
526272cc70bSAndy Fleming 		return err;
527272cc70bSAndy Fleming 	}
528272cc70bSAndy Fleming 
5294e3d89baSYauhen Kharuzhy 	mmc->scr[0] = __be32_to_cpu(scr[0]);
5304e3d89baSYauhen Kharuzhy 	mmc->scr[1] = __be32_to_cpu(scr[1]);
531272cc70bSAndy Fleming 
532272cc70bSAndy Fleming 	switch ((mmc->scr[0] >> 24) & 0xf) {
533272cc70bSAndy Fleming 		case 0:
534272cc70bSAndy Fleming 			mmc->version = SD_VERSION_1_0;
535272cc70bSAndy Fleming 			break;
536272cc70bSAndy Fleming 		case 1:
537272cc70bSAndy Fleming 			mmc->version = SD_VERSION_1_10;
538272cc70bSAndy Fleming 			break;
539272cc70bSAndy Fleming 		case 2:
540272cc70bSAndy Fleming 			mmc->version = SD_VERSION_2;
541272cc70bSAndy Fleming 			break;
542272cc70bSAndy Fleming 		default:
543272cc70bSAndy Fleming 			mmc->version = SD_VERSION_1_0;
544272cc70bSAndy Fleming 			break;
545272cc70bSAndy Fleming 	}
546272cc70bSAndy Fleming 
547272cc70bSAndy Fleming 	/* Version 1.0 doesn't support switching */
548272cc70bSAndy Fleming 	if (mmc->version == SD_VERSION_1_0)
549272cc70bSAndy Fleming 		return 0;
550272cc70bSAndy Fleming 
551272cc70bSAndy Fleming 	timeout = 4;
552272cc70bSAndy Fleming 	while (timeout--) {
553272cc70bSAndy Fleming 		err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
554272cc70bSAndy Fleming 				(u8 *)&switch_status);
555272cc70bSAndy Fleming 
556272cc70bSAndy Fleming 		if (err)
557272cc70bSAndy Fleming 			return err;
558272cc70bSAndy Fleming 
559272cc70bSAndy Fleming 		/* The high-speed function is busy.  Try again */
5604e3d89baSYauhen Kharuzhy 		if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
561272cc70bSAndy Fleming 			break;
562272cc70bSAndy Fleming 	}
563272cc70bSAndy Fleming 
564272cc70bSAndy Fleming 	if (mmc->scr[0] & SD_DATA_4BIT)
565272cc70bSAndy Fleming 		mmc->card_caps |= MMC_MODE_4BIT;
566272cc70bSAndy Fleming 
567272cc70bSAndy Fleming 	/* If high-speed isn't supported, we return */
5684e3d89baSYauhen Kharuzhy 	if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
569272cc70bSAndy Fleming 		return 0;
570272cc70bSAndy Fleming 
571272cc70bSAndy Fleming 	err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)&switch_status);
572272cc70bSAndy Fleming 
573272cc70bSAndy Fleming 	if (err)
574272cc70bSAndy Fleming 		return err;
575272cc70bSAndy Fleming 
5764e3d89baSYauhen Kharuzhy 	if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
577272cc70bSAndy Fleming 		mmc->card_caps |= MMC_MODE_HS;
578272cc70bSAndy Fleming 
579272cc70bSAndy Fleming 	return 0;
580272cc70bSAndy Fleming }
581272cc70bSAndy Fleming 
582272cc70bSAndy Fleming /* frequency bases */
583272cc70bSAndy Fleming /* divided by 10 to be nice to platforms without floating point */
584272cc70bSAndy Fleming int fbase[] = {
585272cc70bSAndy Fleming 	10000,
586272cc70bSAndy Fleming 	100000,
587272cc70bSAndy Fleming 	1000000,
588272cc70bSAndy Fleming 	10000000,
589272cc70bSAndy Fleming };
590272cc70bSAndy Fleming 
591272cc70bSAndy Fleming /* Multiplier values for TRAN_SPEED.  Multiplied by 10 to be nice
592272cc70bSAndy Fleming  * to platforms without floating point.
593272cc70bSAndy Fleming  */
594272cc70bSAndy Fleming int multipliers[] = {
595272cc70bSAndy Fleming 	0,	/* reserved */
596272cc70bSAndy Fleming 	10,
597272cc70bSAndy Fleming 	12,
598272cc70bSAndy Fleming 	13,
599272cc70bSAndy Fleming 	15,
600272cc70bSAndy Fleming 	20,
601272cc70bSAndy Fleming 	25,
602272cc70bSAndy Fleming 	30,
603272cc70bSAndy Fleming 	35,
604272cc70bSAndy Fleming 	40,
605272cc70bSAndy Fleming 	45,
606272cc70bSAndy Fleming 	50,
607272cc70bSAndy Fleming 	55,
608272cc70bSAndy Fleming 	60,
609272cc70bSAndy Fleming 	70,
610272cc70bSAndy Fleming 	80,
611272cc70bSAndy Fleming };
612272cc70bSAndy Fleming 
613272cc70bSAndy Fleming void mmc_set_ios(struct mmc *mmc)
614272cc70bSAndy Fleming {
615272cc70bSAndy Fleming 	mmc->set_ios(mmc);
616272cc70bSAndy Fleming }
617272cc70bSAndy Fleming 
618272cc70bSAndy Fleming void mmc_set_clock(struct mmc *mmc, uint clock)
619272cc70bSAndy Fleming {
620272cc70bSAndy Fleming 	if (clock > mmc->f_max)
621272cc70bSAndy Fleming 		clock = mmc->f_max;
622272cc70bSAndy Fleming 
623272cc70bSAndy Fleming 	if (clock < mmc->f_min)
624272cc70bSAndy Fleming 		clock = mmc->f_min;
625272cc70bSAndy Fleming 
626272cc70bSAndy Fleming 	mmc->clock = clock;
627272cc70bSAndy Fleming 
628272cc70bSAndy Fleming 	mmc_set_ios(mmc);
629272cc70bSAndy Fleming }
630272cc70bSAndy Fleming 
631272cc70bSAndy Fleming void mmc_set_bus_width(struct mmc *mmc, uint width)
632272cc70bSAndy Fleming {
633272cc70bSAndy Fleming 	mmc->bus_width = width;
634272cc70bSAndy Fleming 
635272cc70bSAndy Fleming 	mmc_set_ios(mmc);
636272cc70bSAndy Fleming }
637272cc70bSAndy Fleming 
638272cc70bSAndy Fleming int mmc_startup(struct mmc *mmc)
639272cc70bSAndy Fleming {
640272cc70bSAndy Fleming 	int err;
641272cc70bSAndy Fleming 	uint mult, freq;
642272cc70bSAndy Fleming 	u64 cmult, csize;
643272cc70bSAndy Fleming 	struct mmc_cmd cmd;
644d23e2c09SSukumar Ghorai 	char ext_csd[512];
645272cc70bSAndy Fleming 
646272cc70bSAndy Fleming 	/* Put the Card in Identify Mode */
647272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_ALL_SEND_CID;
648272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R2;
649272cc70bSAndy Fleming 	cmd.cmdarg = 0;
650272cc70bSAndy Fleming 	cmd.flags = 0;
651272cc70bSAndy Fleming 
652272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
653272cc70bSAndy Fleming 
654272cc70bSAndy Fleming 	if (err)
655272cc70bSAndy Fleming 		return err;
656272cc70bSAndy Fleming 
657272cc70bSAndy Fleming 	memcpy(mmc->cid, cmd.response, 16);
658272cc70bSAndy Fleming 
659272cc70bSAndy Fleming 	/*
660272cc70bSAndy Fleming 	 * For MMC cards, set the Relative Address.
661272cc70bSAndy Fleming 	 * For SD cards, get the Relatvie Address.
662272cc70bSAndy Fleming 	 * This also puts the cards into Standby State
663272cc70bSAndy Fleming 	 */
664272cc70bSAndy Fleming 	cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
665272cc70bSAndy Fleming 	cmd.cmdarg = mmc->rca << 16;
666272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R6;
667272cc70bSAndy Fleming 	cmd.flags = 0;
668272cc70bSAndy Fleming 
669272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
670272cc70bSAndy Fleming 
671272cc70bSAndy Fleming 	if (err)
672272cc70bSAndy Fleming 		return err;
673272cc70bSAndy Fleming 
674272cc70bSAndy Fleming 	if (IS_SD(mmc))
675998be3ddSRabin Vincent 		mmc->rca = (cmd.response[0] >> 16) & 0xffff;
676272cc70bSAndy Fleming 
677272cc70bSAndy Fleming 	/* Get the Card-Specific Data */
678272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SEND_CSD;
679272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R2;
680272cc70bSAndy Fleming 	cmd.cmdarg = mmc->rca << 16;
681272cc70bSAndy Fleming 	cmd.flags = 0;
682272cc70bSAndy Fleming 
683272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
684272cc70bSAndy Fleming 
685272cc70bSAndy Fleming 	if (err)
686272cc70bSAndy Fleming 		return err;
687272cc70bSAndy Fleming 
688998be3ddSRabin Vincent 	mmc->csd[0] = cmd.response[0];
689998be3ddSRabin Vincent 	mmc->csd[1] = cmd.response[1];
690998be3ddSRabin Vincent 	mmc->csd[2] = cmd.response[2];
691998be3ddSRabin Vincent 	mmc->csd[3] = cmd.response[3];
692272cc70bSAndy Fleming 
693272cc70bSAndy Fleming 	if (mmc->version == MMC_VERSION_UNKNOWN) {
6940b453ffeSRabin Vincent 		int version = (cmd.response[0] >> 26) & 0xf;
695272cc70bSAndy Fleming 
696272cc70bSAndy Fleming 		switch (version) {
697272cc70bSAndy Fleming 			case 0:
698272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_1_2;
699272cc70bSAndy Fleming 				break;
700272cc70bSAndy Fleming 			case 1:
701272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_1_4;
702272cc70bSAndy Fleming 				break;
703272cc70bSAndy Fleming 			case 2:
704272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_2_2;
705272cc70bSAndy Fleming 				break;
706272cc70bSAndy Fleming 			case 3:
707272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_3;
708272cc70bSAndy Fleming 				break;
709272cc70bSAndy Fleming 			case 4:
710272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_4;
711272cc70bSAndy Fleming 				break;
712272cc70bSAndy Fleming 			default:
713272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_1_2;
714272cc70bSAndy Fleming 				break;
715272cc70bSAndy Fleming 		}
716272cc70bSAndy Fleming 	}
717272cc70bSAndy Fleming 
718272cc70bSAndy Fleming 	/* divide frequency by 10, since the mults are 10x bigger */
7190b453ffeSRabin Vincent 	freq = fbase[(cmd.response[0] & 0x7)];
7200b453ffeSRabin Vincent 	mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
721272cc70bSAndy Fleming 
722272cc70bSAndy Fleming 	mmc->tran_speed = freq * mult;
723272cc70bSAndy Fleming 
724998be3ddSRabin Vincent 	mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
725272cc70bSAndy Fleming 
726272cc70bSAndy Fleming 	if (IS_SD(mmc))
727272cc70bSAndy Fleming 		mmc->write_bl_len = mmc->read_bl_len;
728272cc70bSAndy Fleming 	else
729998be3ddSRabin Vincent 		mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
730272cc70bSAndy Fleming 
731272cc70bSAndy Fleming 	if (mmc->high_capacity) {
732272cc70bSAndy Fleming 		csize = (mmc->csd[1] & 0x3f) << 16
733272cc70bSAndy Fleming 			| (mmc->csd[2] & 0xffff0000) >> 16;
734272cc70bSAndy Fleming 		cmult = 8;
735272cc70bSAndy Fleming 	} else {
736272cc70bSAndy Fleming 		csize = (mmc->csd[1] & 0x3ff) << 2
737272cc70bSAndy Fleming 			| (mmc->csd[2] & 0xc0000000) >> 30;
738272cc70bSAndy Fleming 		cmult = (mmc->csd[2] & 0x00038000) >> 15;
739272cc70bSAndy Fleming 	}
740272cc70bSAndy Fleming 
741272cc70bSAndy Fleming 	mmc->capacity = (csize + 1) << (cmult + 2);
742272cc70bSAndy Fleming 	mmc->capacity *= mmc->read_bl_len;
743272cc70bSAndy Fleming 
744272cc70bSAndy Fleming 	if (mmc->read_bl_len > 512)
745272cc70bSAndy Fleming 		mmc->read_bl_len = 512;
746272cc70bSAndy Fleming 
747272cc70bSAndy Fleming 	if (mmc->write_bl_len > 512)
748272cc70bSAndy Fleming 		mmc->write_bl_len = 512;
749272cc70bSAndy Fleming 
750272cc70bSAndy Fleming 	/* Select the card, and put it into Transfer Mode */
751272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SELECT_CARD;
752272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1b;
753272cc70bSAndy Fleming 	cmd.cmdarg = mmc->rca << 16;
754272cc70bSAndy Fleming 	cmd.flags = 0;
755272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
756272cc70bSAndy Fleming 
757272cc70bSAndy Fleming 	if (err)
758272cc70bSAndy Fleming 		return err;
759272cc70bSAndy Fleming 
760d23e2c09SSukumar Ghorai 	if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
761d23e2c09SSukumar Ghorai 		/* check  ext_csd version and capacity */
762d23e2c09SSukumar Ghorai 		err = mmc_send_ext_csd(mmc, ext_csd);
763d23e2c09SSukumar Ghorai 		if (!err & (ext_csd[192] >= 2)) {
764d23e2c09SSukumar Ghorai 			mmc->capacity = ext_csd[212] << 0 | ext_csd[213] << 8 |
765d23e2c09SSukumar Ghorai 					ext_csd[214] << 16 | ext_csd[215] << 24;
766d23e2c09SSukumar Ghorai 			mmc->capacity *= 512;
767d23e2c09SSukumar Ghorai 		}
768d23e2c09SSukumar Ghorai 	}
769d23e2c09SSukumar Ghorai 
770272cc70bSAndy Fleming 	if (IS_SD(mmc))
771272cc70bSAndy Fleming 		err = sd_change_freq(mmc);
772272cc70bSAndy Fleming 	else
773272cc70bSAndy Fleming 		err = mmc_change_freq(mmc);
774272cc70bSAndy Fleming 
775272cc70bSAndy Fleming 	if (err)
776272cc70bSAndy Fleming 		return err;
777272cc70bSAndy Fleming 
778272cc70bSAndy Fleming 	/* Restrict card's capabilities by what the host can do */
779272cc70bSAndy Fleming 	mmc->card_caps &= mmc->host_caps;
780272cc70bSAndy Fleming 
781272cc70bSAndy Fleming 	if (IS_SD(mmc)) {
782272cc70bSAndy Fleming 		if (mmc->card_caps & MMC_MODE_4BIT) {
783272cc70bSAndy Fleming 			cmd.cmdidx = MMC_CMD_APP_CMD;
784272cc70bSAndy Fleming 			cmd.resp_type = MMC_RSP_R1;
785272cc70bSAndy Fleming 			cmd.cmdarg = mmc->rca << 16;
786272cc70bSAndy Fleming 			cmd.flags = 0;
787272cc70bSAndy Fleming 
788272cc70bSAndy Fleming 			err = mmc_send_cmd(mmc, &cmd, NULL);
789272cc70bSAndy Fleming 			if (err)
790272cc70bSAndy Fleming 				return err;
791272cc70bSAndy Fleming 
792272cc70bSAndy Fleming 			cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
793272cc70bSAndy Fleming 			cmd.resp_type = MMC_RSP_R1;
794272cc70bSAndy Fleming 			cmd.cmdarg = 2;
795272cc70bSAndy Fleming 			cmd.flags = 0;
796272cc70bSAndy Fleming 			err = mmc_send_cmd(mmc, &cmd, NULL);
797272cc70bSAndy Fleming 			if (err)
798272cc70bSAndy Fleming 				return err;
799272cc70bSAndy Fleming 
800272cc70bSAndy Fleming 			mmc_set_bus_width(mmc, 4);
801272cc70bSAndy Fleming 		}
802272cc70bSAndy Fleming 
803272cc70bSAndy Fleming 		if (mmc->card_caps & MMC_MODE_HS)
804272cc70bSAndy Fleming 			mmc_set_clock(mmc, 50000000);
805272cc70bSAndy Fleming 		else
806272cc70bSAndy Fleming 			mmc_set_clock(mmc, 25000000);
807272cc70bSAndy Fleming 	} else {
808272cc70bSAndy Fleming 		if (mmc->card_caps & MMC_MODE_4BIT) {
809272cc70bSAndy Fleming 			/* Set the card to use 4 bit*/
810272cc70bSAndy Fleming 			err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
811272cc70bSAndy Fleming 					EXT_CSD_BUS_WIDTH,
812272cc70bSAndy Fleming 					EXT_CSD_BUS_WIDTH_4);
813272cc70bSAndy Fleming 
814272cc70bSAndy Fleming 			if (err)
815272cc70bSAndy Fleming 				return err;
816272cc70bSAndy Fleming 
817272cc70bSAndy Fleming 			mmc_set_bus_width(mmc, 4);
818272cc70bSAndy Fleming 		} else if (mmc->card_caps & MMC_MODE_8BIT) {
819272cc70bSAndy Fleming 			/* Set the card to use 8 bit*/
820272cc70bSAndy Fleming 			err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
821272cc70bSAndy Fleming 					EXT_CSD_BUS_WIDTH,
822272cc70bSAndy Fleming 					EXT_CSD_BUS_WIDTH_8);
823272cc70bSAndy Fleming 
824272cc70bSAndy Fleming 			if (err)
825272cc70bSAndy Fleming 				return err;
826272cc70bSAndy Fleming 
827272cc70bSAndy Fleming 			mmc_set_bus_width(mmc, 8);
828272cc70bSAndy Fleming 		}
829272cc70bSAndy Fleming 
830272cc70bSAndy Fleming 		if (mmc->card_caps & MMC_MODE_HS) {
831272cc70bSAndy Fleming 			if (mmc->card_caps & MMC_MODE_HS_52MHz)
832272cc70bSAndy Fleming 				mmc_set_clock(mmc, 52000000);
833272cc70bSAndy Fleming 			else
834272cc70bSAndy Fleming 				mmc_set_clock(mmc, 26000000);
835272cc70bSAndy Fleming 		} else
836272cc70bSAndy Fleming 			mmc_set_clock(mmc, 20000000);
837272cc70bSAndy Fleming 	}
838272cc70bSAndy Fleming 
839272cc70bSAndy Fleming 	/* fill in device description */
840272cc70bSAndy Fleming 	mmc->block_dev.lun = 0;
841272cc70bSAndy Fleming 	mmc->block_dev.type = 0;
842272cc70bSAndy Fleming 	mmc->block_dev.blksz = mmc->read_bl_len;
8439b1f942cSRabin Vincent 	mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
8440b453ffeSRabin Vincent 	sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8,
8450b453ffeSRabin Vincent 			(mmc->cid[2] << 8) | (mmc->cid[3] >> 24));
8460b453ffeSRabin Vincent 	sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff,
8470b453ffeSRabin Vincent 			(mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
8480b453ffeSRabin Vincent 			(mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
8490b453ffeSRabin Vincent 	sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28,
8500b453ffeSRabin Vincent 			(mmc->cid[2] >> 24) & 0xf);
851272cc70bSAndy Fleming 	init_part(&mmc->block_dev);
852272cc70bSAndy Fleming 
853272cc70bSAndy Fleming 	return 0;
854272cc70bSAndy Fleming }
855272cc70bSAndy Fleming 
856272cc70bSAndy Fleming int mmc_send_if_cond(struct mmc *mmc)
857272cc70bSAndy Fleming {
858272cc70bSAndy Fleming 	struct mmc_cmd cmd;
859272cc70bSAndy Fleming 	int err;
860272cc70bSAndy Fleming 
861272cc70bSAndy Fleming 	cmd.cmdidx = SD_CMD_SEND_IF_COND;
862272cc70bSAndy Fleming 	/* We set the bit if the host supports voltages between 2.7 and 3.6 V */
863272cc70bSAndy Fleming 	cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
864272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R7;
865272cc70bSAndy Fleming 	cmd.flags = 0;
866272cc70bSAndy Fleming 
867272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
868272cc70bSAndy Fleming 
869272cc70bSAndy Fleming 	if (err)
870272cc70bSAndy Fleming 		return err;
871272cc70bSAndy Fleming 
872998be3ddSRabin Vincent 	if ((cmd.response[0] & 0xff) != 0xaa)
873272cc70bSAndy Fleming 		return UNUSABLE_ERR;
874272cc70bSAndy Fleming 	else
875272cc70bSAndy Fleming 		mmc->version = SD_VERSION_2;
876272cc70bSAndy Fleming 
877272cc70bSAndy Fleming 	return 0;
878272cc70bSAndy Fleming }
879272cc70bSAndy Fleming 
880272cc70bSAndy Fleming int mmc_register(struct mmc *mmc)
881272cc70bSAndy Fleming {
882272cc70bSAndy Fleming 	/* Setup the universal parts of the block interface just once */
883272cc70bSAndy Fleming 	mmc->block_dev.if_type = IF_TYPE_MMC;
884272cc70bSAndy Fleming 	mmc->block_dev.dev = cur_dev_num++;
885272cc70bSAndy Fleming 	mmc->block_dev.removable = 1;
886272cc70bSAndy Fleming 	mmc->block_dev.block_read = mmc_bread;
887272cc70bSAndy Fleming 	mmc->block_dev.block_write = mmc_bwrite;
888272cc70bSAndy Fleming 
889272cc70bSAndy Fleming 	INIT_LIST_HEAD (&mmc->link);
890272cc70bSAndy Fleming 
891272cc70bSAndy Fleming 	list_add_tail (&mmc->link, &mmc_devices);
892272cc70bSAndy Fleming 
893272cc70bSAndy Fleming 	return 0;
894272cc70bSAndy Fleming }
895272cc70bSAndy Fleming 
896272cc70bSAndy Fleming block_dev_desc_t *mmc_get_dev(int dev)
897272cc70bSAndy Fleming {
898272cc70bSAndy Fleming 	struct mmc *mmc = find_mmc_device(dev);
899272cc70bSAndy Fleming 
900e85649c7SRabin Vincent 	return mmc ? &mmc->block_dev : NULL;
901272cc70bSAndy Fleming }
902272cc70bSAndy Fleming 
903272cc70bSAndy Fleming int mmc_init(struct mmc *mmc)
904272cc70bSAndy Fleming {
905272cc70bSAndy Fleming 	int err;
906272cc70bSAndy Fleming 
907272cc70bSAndy Fleming 	err = mmc->init(mmc);
908272cc70bSAndy Fleming 
909272cc70bSAndy Fleming 	if (err)
910272cc70bSAndy Fleming 		return err;
911272cc70bSAndy Fleming 
912b86b85e2SIlya Yanok 	mmc_set_bus_width(mmc, 1);
913b86b85e2SIlya Yanok 	mmc_set_clock(mmc, 1);
914b86b85e2SIlya Yanok 
915272cc70bSAndy Fleming 	/* Reset the Card */
916272cc70bSAndy Fleming 	err = mmc_go_idle(mmc);
917272cc70bSAndy Fleming 
918272cc70bSAndy Fleming 	if (err)
919272cc70bSAndy Fleming 		return err;
920272cc70bSAndy Fleming 
921272cc70bSAndy Fleming 	/* Test for SD version 2 */
922272cc70bSAndy Fleming 	err = mmc_send_if_cond(mmc);
923272cc70bSAndy Fleming 
924272cc70bSAndy Fleming 	/* Now try to get the SD card's operating condition */
925272cc70bSAndy Fleming 	err = sd_send_op_cond(mmc);
926272cc70bSAndy Fleming 
927272cc70bSAndy Fleming 	/* If the command timed out, we check for an MMC card */
928272cc70bSAndy Fleming 	if (err == TIMEOUT) {
929272cc70bSAndy Fleming 		err = mmc_send_op_cond(mmc);
930272cc70bSAndy Fleming 
931272cc70bSAndy Fleming 		if (err) {
932272cc70bSAndy Fleming 			printf("Card did not respond to voltage select!\n");
933272cc70bSAndy Fleming 			return UNUSABLE_ERR;
934272cc70bSAndy Fleming 		}
935272cc70bSAndy Fleming 	}
936272cc70bSAndy Fleming 
937272cc70bSAndy Fleming 	return mmc_startup(mmc);
938272cc70bSAndy Fleming }
939272cc70bSAndy Fleming 
940272cc70bSAndy Fleming /*
941272cc70bSAndy Fleming  * CPU and board-specific MMC initializations.  Aliased function
942272cc70bSAndy Fleming  * signals caller to move on
943272cc70bSAndy Fleming  */
944272cc70bSAndy Fleming static int __def_mmc_init(bd_t *bis)
945272cc70bSAndy Fleming {
946272cc70bSAndy Fleming 	return -1;
947272cc70bSAndy Fleming }
948272cc70bSAndy Fleming 
949f9a109b3SPeter Tyser int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
950f9a109b3SPeter Tyser int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
951272cc70bSAndy Fleming 
952272cc70bSAndy Fleming void print_mmc_devices(char separator)
953272cc70bSAndy Fleming {
954272cc70bSAndy Fleming 	struct mmc *m;
955272cc70bSAndy Fleming 	struct list_head *entry;
956272cc70bSAndy Fleming 
957272cc70bSAndy Fleming 	list_for_each(entry, &mmc_devices) {
958272cc70bSAndy Fleming 		m = list_entry(entry, struct mmc, link);
959272cc70bSAndy Fleming 
960272cc70bSAndy Fleming 		printf("%s: %d", m->name, m->block_dev.dev);
961272cc70bSAndy Fleming 
962272cc70bSAndy Fleming 		if (entry->next != &mmc_devices)
963272cc70bSAndy Fleming 			printf("%c ", separator);
964272cc70bSAndy Fleming 	}
965272cc70bSAndy Fleming 
966272cc70bSAndy Fleming 	printf("\n");
967272cc70bSAndy Fleming }
968272cc70bSAndy Fleming 
969272cc70bSAndy Fleming int mmc_initialize(bd_t *bis)
970272cc70bSAndy Fleming {
971272cc70bSAndy Fleming 	INIT_LIST_HEAD (&mmc_devices);
972272cc70bSAndy Fleming 	cur_dev_num = 0;
973272cc70bSAndy Fleming 
974272cc70bSAndy Fleming 	if (board_mmc_init(bis) < 0)
975272cc70bSAndy Fleming 		cpu_mmc_init(bis);
976272cc70bSAndy Fleming 
977272cc70bSAndy Fleming 	print_mmc_devices(',');
978272cc70bSAndy Fleming 
979272cc70bSAndy Fleming 	return 0;
980272cc70bSAndy Fleming }
981