xref: /openbmc/u-boot/drivers/mmc/mmc.c (revision 0158126e252b6860e7a7d8408d0872278a3483cf)
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
81*0158126eSLei 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;
85*0158126eSLei Wen 	int blklen, err;
86272cc70bSAndy Fleming 
87272cc70bSAndy Fleming 	blklen = mmc->write_bl_len;
88272cc70bSAndy Fleming 
89d2bf29e3SLei Wen 	if ((start + blkcnt) > mmc->block_dev.lba) {
90fe64fd42SWolfgang Denk 		printf("MMC: block number 0x%lx exceeds max(0x%lx)",
91d2bf29e3SLei Wen 			start + blkcnt, mmc->block_dev.lba);
92d2bf29e3SLei Wen 		return 0;
93d2bf29e3SLei Wen 	}
94272cc70bSAndy Fleming 
95272cc70bSAndy Fleming 	if (blkcnt > 1)
96272cc70bSAndy Fleming 		cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
97272cc70bSAndy Fleming 	else
98272cc70bSAndy Fleming 		cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
99272cc70bSAndy Fleming 
100272cc70bSAndy Fleming 	if (mmc->high_capacity)
101272cc70bSAndy Fleming 		cmd.cmdarg = start;
102272cc70bSAndy Fleming 	else
103272cc70bSAndy Fleming 		cmd.cmdarg = start * blklen;
104272cc70bSAndy Fleming 
105272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
106272cc70bSAndy Fleming 	cmd.flags = 0;
107272cc70bSAndy Fleming 
108272cc70bSAndy Fleming 	data.src = src;
109272cc70bSAndy Fleming 	data.blocks = blkcnt;
110272cc70bSAndy Fleming 	data.blocksize = blklen;
111272cc70bSAndy Fleming 	data.flags = MMC_DATA_WRITE;
112272cc70bSAndy Fleming 
113272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, &data);
114272cc70bSAndy Fleming 
115272cc70bSAndy Fleming 	if (err) {
116272cc70bSAndy Fleming 		printf("mmc write failed\n\r");
117272cc70bSAndy Fleming 		return err;
118272cc70bSAndy Fleming 	}
119272cc70bSAndy Fleming 
120272cc70bSAndy Fleming 	if (blkcnt > 1) {
121272cc70bSAndy Fleming 		cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
122272cc70bSAndy Fleming 		cmd.cmdarg = 0;
123272cc70bSAndy Fleming 		cmd.resp_type = MMC_RSP_R1b;
124272cc70bSAndy Fleming 		cmd.flags = 0;
125*0158126eSLei Wen 		err = mmc_send_cmd(mmc, &cmd, NULL);
126*0158126eSLei Wen 		if (err) {
127*0158126eSLei Wen 			printf("mmc fail to send stop cmd\n\r");
128*0158126eSLei Wen 			return err;
129272cc70bSAndy Fleming 		}
130*0158126eSLei Wen 	}
131*0158126eSLei Wen 
132*0158126eSLei Wen 	return blkcnt;
133*0158126eSLei Wen }
134*0158126eSLei Wen 
135*0158126eSLei Wen static ulong
136*0158126eSLei Wen mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
137*0158126eSLei Wen {
138*0158126eSLei Wen 	int err;
139*0158126eSLei Wen 	struct mmc *mmc = find_mmc_device(dev_num);
140*0158126eSLei Wen 	lbaint_t cur, blocks_todo = blkcnt;
141*0158126eSLei Wen 
142*0158126eSLei Wen 	if (!mmc)
143*0158126eSLei Wen 		return -1;
144*0158126eSLei Wen 
145*0158126eSLei Wen 	err = mmc_set_blocklen(mmc, mmc->write_bl_len);
146*0158126eSLei Wen 	if (err) {
147*0158126eSLei Wen 		printf("set write bl len failed\n\r");
148*0158126eSLei Wen 		return err;
149*0158126eSLei Wen 	}
150*0158126eSLei Wen 
151*0158126eSLei Wen 	do {
152*0158126eSLei Wen 		/*
153*0158126eSLei Wen 		 * The 65535 constraint comes from some hardware has
154*0158126eSLei Wen 		 * only 16 bit width block number counter
155*0158126eSLei Wen 		 */
156*0158126eSLei Wen 		cur = (blocks_todo > 65535) ? 65535 : blocks_todo;
157*0158126eSLei Wen 		if(mmc_write_blocks(mmc, start, cur, src) != cur)
158*0158126eSLei Wen 			return -1;
159*0158126eSLei Wen 		blocks_todo -= cur;
160*0158126eSLei Wen 		start += cur;
161*0158126eSLei Wen 		src += cur * mmc->write_bl_len;
162*0158126eSLei Wen 	} while (blocks_todo > 0);
163272cc70bSAndy Fleming 
164272cc70bSAndy Fleming 	return blkcnt;
165272cc70bSAndy Fleming }
166272cc70bSAndy Fleming 
167272cc70bSAndy Fleming int mmc_read_block(struct mmc *mmc, void *dst, uint blocknum)
168272cc70bSAndy Fleming {
169272cc70bSAndy Fleming 	struct mmc_cmd cmd;
170272cc70bSAndy Fleming 	struct mmc_data data;
171272cc70bSAndy Fleming 
172272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
173272cc70bSAndy Fleming 
174272cc70bSAndy Fleming 	if (mmc->high_capacity)
175272cc70bSAndy Fleming 		cmd.cmdarg = blocknum;
176272cc70bSAndy Fleming 	else
177272cc70bSAndy Fleming 		cmd.cmdarg = blocknum * mmc->read_bl_len;
178272cc70bSAndy Fleming 
179272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
180272cc70bSAndy Fleming 	cmd.flags = 0;
181272cc70bSAndy Fleming 
182272cc70bSAndy Fleming 	data.dest = dst;
183272cc70bSAndy Fleming 	data.blocks = 1;
184272cc70bSAndy Fleming 	data.blocksize = mmc->read_bl_len;
185272cc70bSAndy Fleming 	data.flags = MMC_DATA_READ;
186272cc70bSAndy Fleming 
187272cc70bSAndy Fleming 	return mmc_send_cmd(mmc, &cmd, &data);
188272cc70bSAndy Fleming }
189272cc70bSAndy Fleming 
190272cc70bSAndy Fleming int mmc_read(struct mmc *mmc, u64 src, uchar *dst, int size)
191272cc70bSAndy Fleming {
192272cc70bSAndy Fleming 	char *buffer;
193272cc70bSAndy Fleming 	int i;
194272cc70bSAndy Fleming 	int blklen = mmc->read_bl_len;
1959b1f942cSRabin Vincent 	int startblock = lldiv(src, mmc->read_bl_len);
1969b1f942cSRabin Vincent 	int endblock = lldiv(src + size - 1, mmc->read_bl_len);
197272cc70bSAndy Fleming 	int err = 0;
198272cc70bSAndy Fleming 
199272cc70bSAndy Fleming 	/* Make a buffer big enough to hold all the blocks we might read */
200272cc70bSAndy Fleming 	buffer = malloc(blklen);
201272cc70bSAndy Fleming 
202272cc70bSAndy Fleming 	if (!buffer) {
203272cc70bSAndy Fleming 		printf("Could not allocate buffer for MMC read!\n");
204272cc70bSAndy Fleming 		return -1;
205272cc70bSAndy Fleming 	}
206272cc70bSAndy Fleming 
207272cc70bSAndy Fleming 	/* We always do full block reads from the card */
208272cc70bSAndy Fleming 	err = mmc_set_blocklen(mmc, mmc->read_bl_len);
209272cc70bSAndy Fleming 
210272cc70bSAndy Fleming 	if (err)
2118c4444ffSWolfgang Denk 		goto free_buffer;
212272cc70bSAndy Fleming 
213272cc70bSAndy Fleming 	for (i = startblock; i <= endblock; i++) {
214272cc70bSAndy Fleming 		int segment_size;
215272cc70bSAndy Fleming 		int offset;
216272cc70bSAndy Fleming 
217272cc70bSAndy Fleming 		err = mmc_read_block(mmc, buffer, i);
218272cc70bSAndy Fleming 
219272cc70bSAndy Fleming 		if (err)
220272cc70bSAndy Fleming 			goto free_buffer;
221272cc70bSAndy Fleming 
222272cc70bSAndy Fleming 		/*
223272cc70bSAndy Fleming 		 * The first block may not be aligned, so we
224272cc70bSAndy Fleming 		 * copy from the desired point in the block
225272cc70bSAndy Fleming 		 */
226272cc70bSAndy Fleming 		offset = (src & (blklen - 1));
227272cc70bSAndy Fleming 		segment_size = MIN(blklen - offset, size);
228272cc70bSAndy Fleming 
229272cc70bSAndy Fleming 		memcpy(dst, buffer + offset, segment_size);
230272cc70bSAndy Fleming 
231272cc70bSAndy Fleming 		dst += segment_size;
232272cc70bSAndy Fleming 		src += segment_size;
233272cc70bSAndy Fleming 		size -= segment_size;
234272cc70bSAndy Fleming 	}
235272cc70bSAndy Fleming 
236272cc70bSAndy Fleming free_buffer:
237272cc70bSAndy Fleming 	free(buffer);
238272cc70bSAndy Fleming 
239272cc70bSAndy Fleming 	return err;
240272cc70bSAndy Fleming }
241272cc70bSAndy Fleming 
242272cc70bSAndy Fleming static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
243272cc70bSAndy Fleming {
244272cc70bSAndy Fleming 	int err;
245272cc70bSAndy Fleming 	int i;
246272cc70bSAndy Fleming 	struct mmc *mmc = find_mmc_device(dev_num);
247272cc70bSAndy Fleming 
248272cc70bSAndy Fleming 	if (!mmc)
249272cc70bSAndy Fleming 		return 0;
250272cc70bSAndy Fleming 
251d2bf29e3SLei Wen 	if ((start + blkcnt) > mmc->block_dev.lba) {
252fe64fd42SWolfgang Denk 		printf("MMC: block number 0x%lx exceeds max(0x%lx)",
253d2bf29e3SLei Wen 			start + blkcnt, mmc->block_dev.lba);
254d2bf29e3SLei Wen 		return 0;
255d2bf29e3SLei Wen 	}
256272cc70bSAndy Fleming 	/* We always do full block reads from the card */
257272cc70bSAndy Fleming 	err = mmc_set_blocklen(mmc, mmc->read_bl_len);
258272cc70bSAndy Fleming 
259272cc70bSAndy Fleming 	if (err) {
260272cc70bSAndy Fleming 		return 0;
261272cc70bSAndy Fleming 	}
262272cc70bSAndy Fleming 
263272cc70bSAndy Fleming 	for (i = start; i < start + blkcnt; i++, dst += mmc->read_bl_len) {
264272cc70bSAndy Fleming 		err = mmc_read_block(mmc, dst, i);
265272cc70bSAndy Fleming 
266272cc70bSAndy Fleming 		if (err) {
267272cc70bSAndy Fleming 			printf("block read failed: %d\n", err);
268272cc70bSAndy Fleming 			return i - start;
269272cc70bSAndy Fleming 		}
270272cc70bSAndy Fleming 	}
271272cc70bSAndy Fleming 
272272cc70bSAndy Fleming 	return blkcnt;
273272cc70bSAndy Fleming }
274272cc70bSAndy Fleming 
275272cc70bSAndy Fleming int mmc_go_idle(struct mmc* mmc)
276272cc70bSAndy Fleming {
277272cc70bSAndy Fleming 	struct mmc_cmd cmd;
278272cc70bSAndy Fleming 	int err;
279272cc70bSAndy Fleming 
280272cc70bSAndy Fleming 	udelay(1000);
281272cc70bSAndy Fleming 
282272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
283272cc70bSAndy Fleming 	cmd.cmdarg = 0;
284272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_NONE;
285272cc70bSAndy Fleming 	cmd.flags = 0;
286272cc70bSAndy Fleming 
287272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
288272cc70bSAndy Fleming 
289272cc70bSAndy Fleming 	if (err)
290272cc70bSAndy Fleming 		return err;
291272cc70bSAndy Fleming 
292272cc70bSAndy Fleming 	udelay(2000);
293272cc70bSAndy Fleming 
294272cc70bSAndy Fleming 	return 0;
295272cc70bSAndy Fleming }
296272cc70bSAndy Fleming 
297272cc70bSAndy Fleming int
298272cc70bSAndy Fleming sd_send_op_cond(struct mmc *mmc)
299272cc70bSAndy Fleming {
300272cc70bSAndy Fleming 	int timeout = 1000;
301272cc70bSAndy Fleming 	int err;
302272cc70bSAndy Fleming 	struct mmc_cmd cmd;
303272cc70bSAndy Fleming 
304272cc70bSAndy Fleming 	do {
305272cc70bSAndy Fleming 		cmd.cmdidx = MMC_CMD_APP_CMD;
306272cc70bSAndy Fleming 		cmd.resp_type = MMC_RSP_R1;
307272cc70bSAndy Fleming 		cmd.cmdarg = 0;
308272cc70bSAndy Fleming 		cmd.flags = 0;
309272cc70bSAndy Fleming 
310272cc70bSAndy Fleming 		err = mmc_send_cmd(mmc, &cmd, NULL);
311272cc70bSAndy Fleming 
312272cc70bSAndy Fleming 		if (err)
313272cc70bSAndy Fleming 			return err;
314272cc70bSAndy Fleming 
315272cc70bSAndy Fleming 		cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
316272cc70bSAndy Fleming 		cmd.resp_type = MMC_RSP_R3;
317250de12bSStefano Babic 
318250de12bSStefano Babic 		/*
319250de12bSStefano Babic 		 * Most cards do not answer if some reserved bits
320250de12bSStefano Babic 		 * in the ocr are set. However, Some controller
321250de12bSStefano Babic 		 * can set bit 7 (reserved for low voltages), but
322250de12bSStefano Babic 		 * how to manage low voltages SD card is not yet
323250de12bSStefano Babic 		 * specified.
324250de12bSStefano Babic 		 */
325250de12bSStefano Babic 		cmd.cmdarg = mmc->voltages & 0xff8000;
326272cc70bSAndy Fleming 
327272cc70bSAndy Fleming 		if (mmc->version == SD_VERSION_2)
328272cc70bSAndy Fleming 			cmd.cmdarg |= OCR_HCS;
329272cc70bSAndy Fleming 
330272cc70bSAndy Fleming 		err = mmc_send_cmd(mmc, &cmd, NULL);
331272cc70bSAndy Fleming 
332272cc70bSAndy Fleming 		if (err)
333272cc70bSAndy Fleming 			return err;
334272cc70bSAndy Fleming 
335272cc70bSAndy Fleming 		udelay(1000);
336272cc70bSAndy Fleming 	} while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
337272cc70bSAndy Fleming 
338272cc70bSAndy Fleming 	if (timeout <= 0)
339272cc70bSAndy Fleming 		return UNUSABLE_ERR;
340272cc70bSAndy Fleming 
341272cc70bSAndy Fleming 	if (mmc->version != SD_VERSION_2)
342272cc70bSAndy Fleming 		mmc->version = SD_VERSION_1_0;
343272cc70bSAndy Fleming 
344998be3ddSRabin Vincent 	mmc->ocr = cmd.response[0];
345272cc70bSAndy Fleming 
346272cc70bSAndy Fleming 	mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
347272cc70bSAndy Fleming 	mmc->rca = 0;
348272cc70bSAndy Fleming 
349272cc70bSAndy Fleming 	return 0;
350272cc70bSAndy Fleming }
351272cc70bSAndy Fleming 
352272cc70bSAndy Fleming int mmc_send_op_cond(struct mmc *mmc)
353272cc70bSAndy Fleming {
354272cc70bSAndy Fleming 	int timeout = 1000;
355272cc70bSAndy Fleming 	struct mmc_cmd cmd;
356272cc70bSAndy Fleming 	int err;
357272cc70bSAndy Fleming 
358272cc70bSAndy Fleming 	/* Some cards seem to need this */
359272cc70bSAndy Fleming 	mmc_go_idle(mmc);
360272cc70bSAndy Fleming 
361272cc70bSAndy Fleming 	do {
362272cc70bSAndy Fleming 		cmd.cmdidx = MMC_CMD_SEND_OP_COND;
363272cc70bSAndy Fleming 		cmd.resp_type = MMC_RSP_R3;
364272cc70bSAndy Fleming 		cmd.cmdarg = OCR_HCS | mmc->voltages;
365272cc70bSAndy Fleming 		cmd.flags = 0;
366272cc70bSAndy Fleming 
367272cc70bSAndy Fleming 		err = mmc_send_cmd(mmc, &cmd, NULL);
368272cc70bSAndy Fleming 
369272cc70bSAndy Fleming 		if (err)
370272cc70bSAndy Fleming 			return err;
371272cc70bSAndy Fleming 
372272cc70bSAndy Fleming 		udelay(1000);
373272cc70bSAndy Fleming 	} while (!(cmd.response[0] & OCR_BUSY) && timeout--);
374272cc70bSAndy Fleming 
375272cc70bSAndy Fleming 	if (timeout <= 0)
376272cc70bSAndy Fleming 		return UNUSABLE_ERR;
377272cc70bSAndy Fleming 
378272cc70bSAndy Fleming 	mmc->version = MMC_VERSION_UNKNOWN;
379998be3ddSRabin Vincent 	mmc->ocr = cmd.response[0];
380272cc70bSAndy Fleming 
381272cc70bSAndy Fleming 	mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
382272cc70bSAndy Fleming 	mmc->rca = 0;
383272cc70bSAndy Fleming 
384272cc70bSAndy Fleming 	return 0;
385272cc70bSAndy Fleming }
386272cc70bSAndy Fleming 
387272cc70bSAndy Fleming 
388272cc70bSAndy Fleming int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd)
389272cc70bSAndy Fleming {
390272cc70bSAndy Fleming 	struct mmc_cmd cmd;
391272cc70bSAndy Fleming 	struct mmc_data data;
392272cc70bSAndy Fleming 	int err;
393272cc70bSAndy Fleming 
394272cc70bSAndy Fleming 	/* Get the Card Status Register */
395272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
396272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
397272cc70bSAndy Fleming 	cmd.cmdarg = 0;
398272cc70bSAndy Fleming 	cmd.flags = 0;
399272cc70bSAndy Fleming 
400272cc70bSAndy Fleming 	data.dest = ext_csd;
401272cc70bSAndy Fleming 	data.blocks = 1;
402272cc70bSAndy Fleming 	data.blocksize = 512;
403272cc70bSAndy Fleming 	data.flags = MMC_DATA_READ;
404272cc70bSAndy Fleming 
405272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, &data);
406272cc70bSAndy Fleming 
407272cc70bSAndy Fleming 	return err;
408272cc70bSAndy Fleming }
409272cc70bSAndy Fleming 
410272cc70bSAndy Fleming 
411272cc70bSAndy Fleming int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
412272cc70bSAndy Fleming {
413272cc70bSAndy Fleming 	struct mmc_cmd cmd;
414272cc70bSAndy Fleming 
415272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SWITCH;
416272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1b;
417272cc70bSAndy Fleming 	cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
418272cc70bSAndy Fleming 		(index << 16) |
419272cc70bSAndy Fleming 		(value << 8);
420272cc70bSAndy Fleming 	cmd.flags = 0;
421272cc70bSAndy Fleming 
422272cc70bSAndy Fleming 	return mmc_send_cmd(mmc, &cmd, NULL);
423272cc70bSAndy Fleming }
424272cc70bSAndy Fleming 
425272cc70bSAndy Fleming int mmc_change_freq(struct mmc *mmc)
426272cc70bSAndy Fleming {
427272cc70bSAndy Fleming 	char ext_csd[512];
428272cc70bSAndy Fleming 	char cardtype;
429272cc70bSAndy Fleming 	int err;
430272cc70bSAndy Fleming 
431272cc70bSAndy Fleming 	mmc->card_caps = 0;
432272cc70bSAndy Fleming 
433272cc70bSAndy Fleming 	/* Only version 4 supports high-speed */
434272cc70bSAndy Fleming 	if (mmc->version < MMC_VERSION_4)
435272cc70bSAndy Fleming 		return 0;
436272cc70bSAndy Fleming 
437272cc70bSAndy Fleming 	mmc->card_caps |= MMC_MODE_4BIT;
438272cc70bSAndy Fleming 
439272cc70bSAndy Fleming 	err = mmc_send_ext_csd(mmc, ext_csd);
440272cc70bSAndy Fleming 
441272cc70bSAndy Fleming 	if (err)
442272cc70bSAndy Fleming 		return err;
443272cc70bSAndy Fleming 
444272cc70bSAndy Fleming 	if (ext_csd[212] || ext_csd[213] || ext_csd[214] || ext_csd[215])
445272cc70bSAndy Fleming 		mmc->high_capacity = 1;
446272cc70bSAndy Fleming 
447272cc70bSAndy Fleming 	cardtype = ext_csd[196] & 0xf;
448272cc70bSAndy Fleming 
449272cc70bSAndy Fleming 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
450272cc70bSAndy Fleming 
451272cc70bSAndy Fleming 	if (err)
452272cc70bSAndy Fleming 		return err;
453272cc70bSAndy Fleming 
454272cc70bSAndy Fleming 	/* Now check to see that it worked */
455272cc70bSAndy Fleming 	err = mmc_send_ext_csd(mmc, ext_csd);
456272cc70bSAndy Fleming 
457272cc70bSAndy Fleming 	if (err)
458272cc70bSAndy Fleming 		return err;
459272cc70bSAndy Fleming 
460272cc70bSAndy Fleming 	/* No high-speed support */
461272cc70bSAndy Fleming 	if (!ext_csd[185])
462272cc70bSAndy Fleming 		return 0;
463272cc70bSAndy Fleming 
464272cc70bSAndy Fleming 	/* High Speed is set, there are two types: 52MHz and 26MHz */
465272cc70bSAndy Fleming 	if (cardtype & MMC_HS_52MHZ)
466272cc70bSAndy Fleming 		mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
467272cc70bSAndy Fleming 	else
468272cc70bSAndy Fleming 		mmc->card_caps |= MMC_MODE_HS;
469272cc70bSAndy Fleming 
470272cc70bSAndy Fleming 	return 0;
471272cc70bSAndy Fleming }
472272cc70bSAndy Fleming 
473272cc70bSAndy Fleming int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
474272cc70bSAndy Fleming {
475272cc70bSAndy Fleming 	struct mmc_cmd cmd;
476272cc70bSAndy Fleming 	struct mmc_data data;
477272cc70bSAndy Fleming 
478272cc70bSAndy Fleming 	/* Switch the frequency */
479272cc70bSAndy Fleming 	cmd.cmdidx = SD_CMD_SWITCH_FUNC;
480272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
481272cc70bSAndy Fleming 	cmd.cmdarg = (mode << 31) | 0xffffff;
482272cc70bSAndy Fleming 	cmd.cmdarg &= ~(0xf << (group * 4));
483272cc70bSAndy Fleming 	cmd.cmdarg |= value << (group * 4);
484272cc70bSAndy Fleming 	cmd.flags = 0;
485272cc70bSAndy Fleming 
486272cc70bSAndy Fleming 	data.dest = (char *)resp;
487272cc70bSAndy Fleming 	data.blocksize = 64;
488272cc70bSAndy Fleming 	data.blocks = 1;
489272cc70bSAndy Fleming 	data.flags = MMC_DATA_READ;
490272cc70bSAndy Fleming 
491272cc70bSAndy Fleming 	return mmc_send_cmd(mmc, &cmd, &data);
492272cc70bSAndy Fleming }
493272cc70bSAndy Fleming 
494272cc70bSAndy Fleming 
495272cc70bSAndy Fleming int sd_change_freq(struct mmc *mmc)
496272cc70bSAndy Fleming {
497272cc70bSAndy Fleming 	int err;
498272cc70bSAndy Fleming 	struct mmc_cmd cmd;
499272cc70bSAndy Fleming 	uint scr[2];
500272cc70bSAndy Fleming 	uint switch_status[16];
501272cc70bSAndy Fleming 	struct mmc_data data;
502272cc70bSAndy Fleming 	int timeout;
503272cc70bSAndy Fleming 
504272cc70bSAndy Fleming 	mmc->card_caps = 0;
505272cc70bSAndy Fleming 
506272cc70bSAndy Fleming 	/* Read the SCR to find out if this card supports higher speeds */
507272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_APP_CMD;
508272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
509272cc70bSAndy Fleming 	cmd.cmdarg = mmc->rca << 16;
510272cc70bSAndy Fleming 	cmd.flags = 0;
511272cc70bSAndy Fleming 
512272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
513272cc70bSAndy Fleming 
514272cc70bSAndy Fleming 	if (err)
515272cc70bSAndy Fleming 		return err;
516272cc70bSAndy Fleming 
517272cc70bSAndy Fleming 	cmd.cmdidx = SD_CMD_APP_SEND_SCR;
518272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
519272cc70bSAndy Fleming 	cmd.cmdarg = 0;
520272cc70bSAndy Fleming 	cmd.flags = 0;
521272cc70bSAndy Fleming 
522272cc70bSAndy Fleming 	timeout = 3;
523272cc70bSAndy Fleming 
524272cc70bSAndy Fleming retry_scr:
525272cc70bSAndy Fleming 	data.dest = (char *)&scr;
526272cc70bSAndy Fleming 	data.blocksize = 8;
527272cc70bSAndy Fleming 	data.blocks = 1;
528272cc70bSAndy Fleming 	data.flags = MMC_DATA_READ;
529272cc70bSAndy Fleming 
530272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, &data);
531272cc70bSAndy Fleming 
532272cc70bSAndy Fleming 	if (err) {
533272cc70bSAndy Fleming 		if (timeout--)
534272cc70bSAndy Fleming 			goto retry_scr;
535272cc70bSAndy Fleming 
536272cc70bSAndy Fleming 		return err;
537272cc70bSAndy Fleming 	}
538272cc70bSAndy Fleming 
5394e3d89baSYauhen Kharuzhy 	mmc->scr[0] = __be32_to_cpu(scr[0]);
5404e3d89baSYauhen Kharuzhy 	mmc->scr[1] = __be32_to_cpu(scr[1]);
541272cc70bSAndy Fleming 
542272cc70bSAndy Fleming 	switch ((mmc->scr[0] >> 24) & 0xf) {
543272cc70bSAndy Fleming 		case 0:
544272cc70bSAndy Fleming 			mmc->version = SD_VERSION_1_0;
545272cc70bSAndy Fleming 			break;
546272cc70bSAndy Fleming 		case 1:
547272cc70bSAndy Fleming 			mmc->version = SD_VERSION_1_10;
548272cc70bSAndy Fleming 			break;
549272cc70bSAndy Fleming 		case 2:
550272cc70bSAndy Fleming 			mmc->version = SD_VERSION_2;
551272cc70bSAndy Fleming 			break;
552272cc70bSAndy Fleming 		default:
553272cc70bSAndy Fleming 			mmc->version = SD_VERSION_1_0;
554272cc70bSAndy Fleming 			break;
555272cc70bSAndy Fleming 	}
556272cc70bSAndy Fleming 
557272cc70bSAndy Fleming 	/* Version 1.0 doesn't support switching */
558272cc70bSAndy Fleming 	if (mmc->version == SD_VERSION_1_0)
559272cc70bSAndy Fleming 		return 0;
560272cc70bSAndy Fleming 
561272cc70bSAndy Fleming 	timeout = 4;
562272cc70bSAndy Fleming 	while (timeout--) {
563272cc70bSAndy Fleming 		err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
564272cc70bSAndy Fleming 				(u8 *)&switch_status);
565272cc70bSAndy Fleming 
566272cc70bSAndy Fleming 		if (err)
567272cc70bSAndy Fleming 			return err;
568272cc70bSAndy Fleming 
569272cc70bSAndy Fleming 		/* The high-speed function is busy.  Try again */
5704e3d89baSYauhen Kharuzhy 		if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
571272cc70bSAndy Fleming 			break;
572272cc70bSAndy Fleming 	}
573272cc70bSAndy Fleming 
574272cc70bSAndy Fleming 	if (mmc->scr[0] & SD_DATA_4BIT)
575272cc70bSAndy Fleming 		mmc->card_caps |= MMC_MODE_4BIT;
576272cc70bSAndy Fleming 
577272cc70bSAndy Fleming 	/* If high-speed isn't supported, we return */
5784e3d89baSYauhen Kharuzhy 	if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
579272cc70bSAndy Fleming 		return 0;
580272cc70bSAndy Fleming 
581272cc70bSAndy Fleming 	err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)&switch_status);
582272cc70bSAndy Fleming 
583272cc70bSAndy Fleming 	if (err)
584272cc70bSAndy Fleming 		return err;
585272cc70bSAndy Fleming 
5864e3d89baSYauhen Kharuzhy 	if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
587272cc70bSAndy Fleming 		mmc->card_caps |= MMC_MODE_HS;
588272cc70bSAndy Fleming 
589272cc70bSAndy Fleming 	return 0;
590272cc70bSAndy Fleming }
591272cc70bSAndy Fleming 
592272cc70bSAndy Fleming /* frequency bases */
593272cc70bSAndy Fleming /* divided by 10 to be nice to platforms without floating point */
594272cc70bSAndy Fleming int fbase[] = {
595272cc70bSAndy Fleming 	10000,
596272cc70bSAndy Fleming 	100000,
597272cc70bSAndy Fleming 	1000000,
598272cc70bSAndy Fleming 	10000000,
599272cc70bSAndy Fleming };
600272cc70bSAndy Fleming 
601272cc70bSAndy Fleming /* Multiplier values for TRAN_SPEED.  Multiplied by 10 to be nice
602272cc70bSAndy Fleming  * to platforms without floating point.
603272cc70bSAndy Fleming  */
604272cc70bSAndy Fleming int multipliers[] = {
605272cc70bSAndy Fleming 	0,	/* reserved */
606272cc70bSAndy Fleming 	10,
607272cc70bSAndy Fleming 	12,
608272cc70bSAndy Fleming 	13,
609272cc70bSAndy Fleming 	15,
610272cc70bSAndy Fleming 	20,
611272cc70bSAndy Fleming 	25,
612272cc70bSAndy Fleming 	30,
613272cc70bSAndy Fleming 	35,
614272cc70bSAndy Fleming 	40,
615272cc70bSAndy Fleming 	45,
616272cc70bSAndy Fleming 	50,
617272cc70bSAndy Fleming 	55,
618272cc70bSAndy Fleming 	60,
619272cc70bSAndy Fleming 	70,
620272cc70bSAndy Fleming 	80,
621272cc70bSAndy Fleming };
622272cc70bSAndy Fleming 
623272cc70bSAndy Fleming void mmc_set_ios(struct mmc *mmc)
624272cc70bSAndy Fleming {
625272cc70bSAndy Fleming 	mmc->set_ios(mmc);
626272cc70bSAndy Fleming }
627272cc70bSAndy Fleming 
628272cc70bSAndy Fleming void mmc_set_clock(struct mmc *mmc, uint clock)
629272cc70bSAndy Fleming {
630272cc70bSAndy Fleming 	if (clock > mmc->f_max)
631272cc70bSAndy Fleming 		clock = mmc->f_max;
632272cc70bSAndy Fleming 
633272cc70bSAndy Fleming 	if (clock < mmc->f_min)
634272cc70bSAndy Fleming 		clock = mmc->f_min;
635272cc70bSAndy Fleming 
636272cc70bSAndy Fleming 	mmc->clock = clock;
637272cc70bSAndy Fleming 
638272cc70bSAndy Fleming 	mmc_set_ios(mmc);
639272cc70bSAndy Fleming }
640272cc70bSAndy Fleming 
641272cc70bSAndy Fleming void mmc_set_bus_width(struct mmc *mmc, uint width)
642272cc70bSAndy Fleming {
643272cc70bSAndy Fleming 	mmc->bus_width = width;
644272cc70bSAndy Fleming 
645272cc70bSAndy Fleming 	mmc_set_ios(mmc);
646272cc70bSAndy Fleming }
647272cc70bSAndy Fleming 
648272cc70bSAndy Fleming int mmc_startup(struct mmc *mmc)
649272cc70bSAndy Fleming {
650272cc70bSAndy Fleming 	int err;
651272cc70bSAndy Fleming 	uint mult, freq;
652272cc70bSAndy Fleming 	u64 cmult, csize;
653272cc70bSAndy Fleming 	struct mmc_cmd cmd;
654d23e2c09SSukumar Ghorai 	char ext_csd[512];
655272cc70bSAndy Fleming 
656272cc70bSAndy Fleming 	/* Put the Card in Identify Mode */
657272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_ALL_SEND_CID;
658272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R2;
659272cc70bSAndy Fleming 	cmd.cmdarg = 0;
660272cc70bSAndy Fleming 	cmd.flags = 0;
661272cc70bSAndy Fleming 
662272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
663272cc70bSAndy Fleming 
664272cc70bSAndy Fleming 	if (err)
665272cc70bSAndy Fleming 		return err;
666272cc70bSAndy Fleming 
667272cc70bSAndy Fleming 	memcpy(mmc->cid, cmd.response, 16);
668272cc70bSAndy Fleming 
669272cc70bSAndy Fleming 	/*
670272cc70bSAndy Fleming 	 * For MMC cards, set the Relative Address.
671272cc70bSAndy Fleming 	 * For SD cards, get the Relatvie Address.
672272cc70bSAndy Fleming 	 * This also puts the cards into Standby State
673272cc70bSAndy Fleming 	 */
674272cc70bSAndy Fleming 	cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
675272cc70bSAndy Fleming 	cmd.cmdarg = mmc->rca << 16;
676272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R6;
677272cc70bSAndy Fleming 	cmd.flags = 0;
678272cc70bSAndy Fleming 
679272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
680272cc70bSAndy Fleming 
681272cc70bSAndy Fleming 	if (err)
682272cc70bSAndy Fleming 		return err;
683272cc70bSAndy Fleming 
684272cc70bSAndy Fleming 	if (IS_SD(mmc))
685998be3ddSRabin Vincent 		mmc->rca = (cmd.response[0] >> 16) & 0xffff;
686272cc70bSAndy Fleming 
687272cc70bSAndy Fleming 	/* Get the Card-Specific Data */
688272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SEND_CSD;
689272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R2;
690272cc70bSAndy Fleming 	cmd.cmdarg = mmc->rca << 16;
691272cc70bSAndy Fleming 	cmd.flags = 0;
692272cc70bSAndy Fleming 
693272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
694272cc70bSAndy Fleming 
695272cc70bSAndy Fleming 	if (err)
696272cc70bSAndy Fleming 		return err;
697272cc70bSAndy Fleming 
698998be3ddSRabin Vincent 	mmc->csd[0] = cmd.response[0];
699998be3ddSRabin Vincent 	mmc->csd[1] = cmd.response[1];
700998be3ddSRabin Vincent 	mmc->csd[2] = cmd.response[2];
701998be3ddSRabin Vincent 	mmc->csd[3] = cmd.response[3];
702272cc70bSAndy Fleming 
703272cc70bSAndy Fleming 	if (mmc->version == MMC_VERSION_UNKNOWN) {
7040b453ffeSRabin Vincent 		int version = (cmd.response[0] >> 26) & 0xf;
705272cc70bSAndy Fleming 
706272cc70bSAndy Fleming 		switch (version) {
707272cc70bSAndy Fleming 			case 0:
708272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_1_2;
709272cc70bSAndy Fleming 				break;
710272cc70bSAndy Fleming 			case 1:
711272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_1_4;
712272cc70bSAndy Fleming 				break;
713272cc70bSAndy Fleming 			case 2:
714272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_2_2;
715272cc70bSAndy Fleming 				break;
716272cc70bSAndy Fleming 			case 3:
717272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_3;
718272cc70bSAndy Fleming 				break;
719272cc70bSAndy Fleming 			case 4:
720272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_4;
721272cc70bSAndy Fleming 				break;
722272cc70bSAndy Fleming 			default:
723272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_1_2;
724272cc70bSAndy Fleming 				break;
725272cc70bSAndy Fleming 		}
726272cc70bSAndy Fleming 	}
727272cc70bSAndy Fleming 
728272cc70bSAndy Fleming 	/* divide frequency by 10, since the mults are 10x bigger */
7290b453ffeSRabin Vincent 	freq = fbase[(cmd.response[0] & 0x7)];
7300b453ffeSRabin Vincent 	mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
731272cc70bSAndy Fleming 
732272cc70bSAndy Fleming 	mmc->tran_speed = freq * mult;
733272cc70bSAndy Fleming 
734998be3ddSRabin Vincent 	mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
735272cc70bSAndy Fleming 
736272cc70bSAndy Fleming 	if (IS_SD(mmc))
737272cc70bSAndy Fleming 		mmc->write_bl_len = mmc->read_bl_len;
738272cc70bSAndy Fleming 	else
739998be3ddSRabin Vincent 		mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
740272cc70bSAndy Fleming 
741272cc70bSAndy Fleming 	if (mmc->high_capacity) {
742272cc70bSAndy Fleming 		csize = (mmc->csd[1] & 0x3f) << 16
743272cc70bSAndy Fleming 			| (mmc->csd[2] & 0xffff0000) >> 16;
744272cc70bSAndy Fleming 		cmult = 8;
745272cc70bSAndy Fleming 	} else {
746272cc70bSAndy Fleming 		csize = (mmc->csd[1] & 0x3ff) << 2
747272cc70bSAndy Fleming 			| (mmc->csd[2] & 0xc0000000) >> 30;
748272cc70bSAndy Fleming 		cmult = (mmc->csd[2] & 0x00038000) >> 15;
749272cc70bSAndy Fleming 	}
750272cc70bSAndy Fleming 
751272cc70bSAndy Fleming 	mmc->capacity = (csize + 1) << (cmult + 2);
752272cc70bSAndy Fleming 	mmc->capacity *= mmc->read_bl_len;
753272cc70bSAndy Fleming 
754272cc70bSAndy Fleming 	if (mmc->read_bl_len > 512)
755272cc70bSAndy Fleming 		mmc->read_bl_len = 512;
756272cc70bSAndy Fleming 
757272cc70bSAndy Fleming 	if (mmc->write_bl_len > 512)
758272cc70bSAndy Fleming 		mmc->write_bl_len = 512;
759272cc70bSAndy Fleming 
760272cc70bSAndy Fleming 	/* Select the card, and put it into Transfer Mode */
761272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SELECT_CARD;
762272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1b;
763272cc70bSAndy Fleming 	cmd.cmdarg = mmc->rca << 16;
764272cc70bSAndy Fleming 	cmd.flags = 0;
765272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
766272cc70bSAndy Fleming 
767272cc70bSAndy Fleming 	if (err)
768272cc70bSAndy Fleming 		return err;
769272cc70bSAndy Fleming 
770d23e2c09SSukumar Ghorai 	if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
771d23e2c09SSukumar Ghorai 		/* check  ext_csd version and capacity */
772d23e2c09SSukumar Ghorai 		err = mmc_send_ext_csd(mmc, ext_csd);
773d23e2c09SSukumar Ghorai 		if (!err & (ext_csd[192] >= 2)) {
774d23e2c09SSukumar Ghorai 			mmc->capacity = ext_csd[212] << 0 | ext_csd[213] << 8 |
775d23e2c09SSukumar Ghorai 					ext_csd[214] << 16 | ext_csd[215] << 24;
776d23e2c09SSukumar Ghorai 			mmc->capacity *= 512;
777d23e2c09SSukumar Ghorai 		}
778d23e2c09SSukumar Ghorai 	}
779d23e2c09SSukumar Ghorai 
780272cc70bSAndy Fleming 	if (IS_SD(mmc))
781272cc70bSAndy Fleming 		err = sd_change_freq(mmc);
782272cc70bSAndy Fleming 	else
783272cc70bSAndy Fleming 		err = mmc_change_freq(mmc);
784272cc70bSAndy Fleming 
785272cc70bSAndy Fleming 	if (err)
786272cc70bSAndy Fleming 		return err;
787272cc70bSAndy Fleming 
788272cc70bSAndy Fleming 	/* Restrict card's capabilities by what the host can do */
789272cc70bSAndy Fleming 	mmc->card_caps &= mmc->host_caps;
790272cc70bSAndy Fleming 
791272cc70bSAndy Fleming 	if (IS_SD(mmc)) {
792272cc70bSAndy Fleming 		if (mmc->card_caps & MMC_MODE_4BIT) {
793272cc70bSAndy Fleming 			cmd.cmdidx = MMC_CMD_APP_CMD;
794272cc70bSAndy Fleming 			cmd.resp_type = MMC_RSP_R1;
795272cc70bSAndy Fleming 			cmd.cmdarg = mmc->rca << 16;
796272cc70bSAndy Fleming 			cmd.flags = 0;
797272cc70bSAndy Fleming 
798272cc70bSAndy Fleming 			err = mmc_send_cmd(mmc, &cmd, NULL);
799272cc70bSAndy Fleming 			if (err)
800272cc70bSAndy Fleming 				return err;
801272cc70bSAndy Fleming 
802272cc70bSAndy Fleming 			cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
803272cc70bSAndy Fleming 			cmd.resp_type = MMC_RSP_R1;
804272cc70bSAndy Fleming 			cmd.cmdarg = 2;
805272cc70bSAndy Fleming 			cmd.flags = 0;
806272cc70bSAndy Fleming 			err = mmc_send_cmd(mmc, &cmd, NULL);
807272cc70bSAndy Fleming 			if (err)
808272cc70bSAndy Fleming 				return err;
809272cc70bSAndy Fleming 
810272cc70bSAndy Fleming 			mmc_set_bus_width(mmc, 4);
811272cc70bSAndy Fleming 		}
812272cc70bSAndy Fleming 
813272cc70bSAndy Fleming 		if (mmc->card_caps & MMC_MODE_HS)
814272cc70bSAndy Fleming 			mmc_set_clock(mmc, 50000000);
815272cc70bSAndy Fleming 		else
816272cc70bSAndy Fleming 			mmc_set_clock(mmc, 25000000);
817272cc70bSAndy Fleming 	} else {
818272cc70bSAndy Fleming 		if (mmc->card_caps & MMC_MODE_4BIT) {
819272cc70bSAndy Fleming 			/* Set the card to use 4 bit*/
820272cc70bSAndy Fleming 			err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
821272cc70bSAndy Fleming 					EXT_CSD_BUS_WIDTH,
822272cc70bSAndy Fleming 					EXT_CSD_BUS_WIDTH_4);
823272cc70bSAndy Fleming 
824272cc70bSAndy Fleming 			if (err)
825272cc70bSAndy Fleming 				return err;
826272cc70bSAndy Fleming 
827272cc70bSAndy Fleming 			mmc_set_bus_width(mmc, 4);
828272cc70bSAndy Fleming 		} else if (mmc->card_caps & MMC_MODE_8BIT) {
829272cc70bSAndy Fleming 			/* Set the card to use 8 bit*/
830272cc70bSAndy Fleming 			err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
831272cc70bSAndy Fleming 					EXT_CSD_BUS_WIDTH,
832272cc70bSAndy Fleming 					EXT_CSD_BUS_WIDTH_8);
833272cc70bSAndy Fleming 
834272cc70bSAndy Fleming 			if (err)
835272cc70bSAndy Fleming 				return err;
836272cc70bSAndy Fleming 
837272cc70bSAndy Fleming 			mmc_set_bus_width(mmc, 8);
838272cc70bSAndy Fleming 		}
839272cc70bSAndy Fleming 
840272cc70bSAndy Fleming 		if (mmc->card_caps & MMC_MODE_HS) {
841272cc70bSAndy Fleming 			if (mmc->card_caps & MMC_MODE_HS_52MHz)
842272cc70bSAndy Fleming 				mmc_set_clock(mmc, 52000000);
843272cc70bSAndy Fleming 			else
844272cc70bSAndy Fleming 				mmc_set_clock(mmc, 26000000);
845272cc70bSAndy Fleming 		} else
846272cc70bSAndy Fleming 			mmc_set_clock(mmc, 20000000);
847272cc70bSAndy Fleming 	}
848272cc70bSAndy Fleming 
849272cc70bSAndy Fleming 	/* fill in device description */
850272cc70bSAndy Fleming 	mmc->block_dev.lun = 0;
851272cc70bSAndy Fleming 	mmc->block_dev.type = 0;
852272cc70bSAndy Fleming 	mmc->block_dev.blksz = mmc->read_bl_len;
8539b1f942cSRabin Vincent 	mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
8540b453ffeSRabin Vincent 	sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8,
8550b453ffeSRabin Vincent 			(mmc->cid[2] << 8) | (mmc->cid[3] >> 24));
8560b453ffeSRabin Vincent 	sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff,
8570b453ffeSRabin Vincent 			(mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
8580b453ffeSRabin Vincent 			(mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
8590b453ffeSRabin Vincent 	sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28,
8600b453ffeSRabin Vincent 			(mmc->cid[2] >> 24) & 0xf);
861272cc70bSAndy Fleming 	init_part(&mmc->block_dev);
862272cc70bSAndy Fleming 
863272cc70bSAndy Fleming 	return 0;
864272cc70bSAndy Fleming }
865272cc70bSAndy Fleming 
866272cc70bSAndy Fleming int mmc_send_if_cond(struct mmc *mmc)
867272cc70bSAndy Fleming {
868272cc70bSAndy Fleming 	struct mmc_cmd cmd;
869272cc70bSAndy Fleming 	int err;
870272cc70bSAndy Fleming 
871272cc70bSAndy Fleming 	cmd.cmdidx = SD_CMD_SEND_IF_COND;
872272cc70bSAndy Fleming 	/* We set the bit if the host supports voltages between 2.7 and 3.6 V */
873272cc70bSAndy Fleming 	cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
874272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R7;
875272cc70bSAndy Fleming 	cmd.flags = 0;
876272cc70bSAndy Fleming 
877272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
878272cc70bSAndy Fleming 
879272cc70bSAndy Fleming 	if (err)
880272cc70bSAndy Fleming 		return err;
881272cc70bSAndy Fleming 
882998be3ddSRabin Vincent 	if ((cmd.response[0] & 0xff) != 0xaa)
883272cc70bSAndy Fleming 		return UNUSABLE_ERR;
884272cc70bSAndy Fleming 	else
885272cc70bSAndy Fleming 		mmc->version = SD_VERSION_2;
886272cc70bSAndy Fleming 
887272cc70bSAndy Fleming 	return 0;
888272cc70bSAndy Fleming }
889272cc70bSAndy Fleming 
890272cc70bSAndy Fleming int mmc_register(struct mmc *mmc)
891272cc70bSAndy Fleming {
892272cc70bSAndy Fleming 	/* Setup the universal parts of the block interface just once */
893272cc70bSAndy Fleming 	mmc->block_dev.if_type = IF_TYPE_MMC;
894272cc70bSAndy Fleming 	mmc->block_dev.dev = cur_dev_num++;
895272cc70bSAndy Fleming 	mmc->block_dev.removable = 1;
896272cc70bSAndy Fleming 	mmc->block_dev.block_read = mmc_bread;
897272cc70bSAndy Fleming 	mmc->block_dev.block_write = mmc_bwrite;
898272cc70bSAndy Fleming 
899272cc70bSAndy Fleming 	INIT_LIST_HEAD (&mmc->link);
900272cc70bSAndy Fleming 
901272cc70bSAndy Fleming 	list_add_tail (&mmc->link, &mmc_devices);
902272cc70bSAndy Fleming 
903272cc70bSAndy Fleming 	return 0;
904272cc70bSAndy Fleming }
905272cc70bSAndy Fleming 
906272cc70bSAndy Fleming block_dev_desc_t *mmc_get_dev(int dev)
907272cc70bSAndy Fleming {
908272cc70bSAndy Fleming 	struct mmc *mmc = find_mmc_device(dev);
909272cc70bSAndy Fleming 
910e85649c7SRabin Vincent 	return mmc ? &mmc->block_dev : NULL;
911272cc70bSAndy Fleming }
912272cc70bSAndy Fleming 
913272cc70bSAndy Fleming int mmc_init(struct mmc *mmc)
914272cc70bSAndy Fleming {
915272cc70bSAndy Fleming 	int err;
916272cc70bSAndy Fleming 
917272cc70bSAndy Fleming 	err = mmc->init(mmc);
918272cc70bSAndy Fleming 
919272cc70bSAndy Fleming 	if (err)
920272cc70bSAndy Fleming 		return err;
921272cc70bSAndy Fleming 
922b86b85e2SIlya Yanok 	mmc_set_bus_width(mmc, 1);
923b86b85e2SIlya Yanok 	mmc_set_clock(mmc, 1);
924b86b85e2SIlya Yanok 
925272cc70bSAndy Fleming 	/* Reset the Card */
926272cc70bSAndy Fleming 	err = mmc_go_idle(mmc);
927272cc70bSAndy Fleming 
928272cc70bSAndy Fleming 	if (err)
929272cc70bSAndy Fleming 		return err;
930272cc70bSAndy Fleming 
931272cc70bSAndy Fleming 	/* Test for SD version 2 */
932272cc70bSAndy Fleming 	err = mmc_send_if_cond(mmc);
933272cc70bSAndy Fleming 
934272cc70bSAndy Fleming 	/* Now try to get the SD card's operating condition */
935272cc70bSAndy Fleming 	err = sd_send_op_cond(mmc);
936272cc70bSAndy Fleming 
937272cc70bSAndy Fleming 	/* If the command timed out, we check for an MMC card */
938272cc70bSAndy Fleming 	if (err == TIMEOUT) {
939272cc70bSAndy Fleming 		err = mmc_send_op_cond(mmc);
940272cc70bSAndy Fleming 
941272cc70bSAndy Fleming 		if (err) {
942272cc70bSAndy Fleming 			printf("Card did not respond to voltage select!\n");
943272cc70bSAndy Fleming 			return UNUSABLE_ERR;
944272cc70bSAndy Fleming 		}
945272cc70bSAndy Fleming 	}
946272cc70bSAndy Fleming 
947272cc70bSAndy Fleming 	return mmc_startup(mmc);
948272cc70bSAndy Fleming }
949272cc70bSAndy Fleming 
950272cc70bSAndy Fleming /*
951272cc70bSAndy Fleming  * CPU and board-specific MMC initializations.  Aliased function
952272cc70bSAndy Fleming  * signals caller to move on
953272cc70bSAndy Fleming  */
954272cc70bSAndy Fleming static int __def_mmc_init(bd_t *bis)
955272cc70bSAndy Fleming {
956272cc70bSAndy Fleming 	return -1;
957272cc70bSAndy Fleming }
958272cc70bSAndy Fleming 
959f9a109b3SPeter Tyser int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
960f9a109b3SPeter Tyser int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
961272cc70bSAndy Fleming 
962272cc70bSAndy Fleming void print_mmc_devices(char separator)
963272cc70bSAndy Fleming {
964272cc70bSAndy Fleming 	struct mmc *m;
965272cc70bSAndy Fleming 	struct list_head *entry;
966272cc70bSAndy Fleming 
967272cc70bSAndy Fleming 	list_for_each(entry, &mmc_devices) {
968272cc70bSAndy Fleming 		m = list_entry(entry, struct mmc, link);
969272cc70bSAndy Fleming 
970272cc70bSAndy Fleming 		printf("%s: %d", m->name, m->block_dev.dev);
971272cc70bSAndy Fleming 
972272cc70bSAndy Fleming 		if (entry->next != &mmc_devices)
973272cc70bSAndy Fleming 			printf("%c ", separator);
974272cc70bSAndy Fleming 	}
975272cc70bSAndy Fleming 
976272cc70bSAndy Fleming 	printf("\n");
977272cc70bSAndy Fleming }
978272cc70bSAndy Fleming 
979272cc70bSAndy Fleming int mmc_initialize(bd_t *bis)
980272cc70bSAndy Fleming {
981272cc70bSAndy Fleming 	INIT_LIST_HEAD (&mmc_devices);
982272cc70bSAndy Fleming 	cur_dev_num = 0;
983272cc70bSAndy Fleming 
984272cc70bSAndy Fleming 	if (board_mmc_init(bis) < 0)
985272cc70bSAndy Fleming 		cpu_mmc_init(bis);
986272cc70bSAndy Fleming 
987272cc70bSAndy Fleming 	print_mmc_devices(',');
988272cc70bSAndy Fleming 
989272cc70bSAndy Fleming 	return 0;
990272cc70bSAndy Fleming }
991