blob: 4e4e0fb0f3cf43bb77e758d3f55c5d18bd14e7c8 [file] [log] [blame]
Andy Fleming272cc702008-10-30 16:41:01 -05001/*
2 * Copyright 2008, Freescale Semiconductor, Inc
3 * Andy Fleming
4 *
5 * Based vaguely on the Linux code
6 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
25
26#include <config.h>
27#include <common.h>
28#include <command.h>
29#include <mmc.h>
30#include <part.h>
31#include <malloc.h>
32#include <linux/list.h>
Rabin Vincent9b1f9422009-04-05 13:30:54 +053033#include <div64.h>
Andy Fleming272cc702008-10-30 16:41:01 -050034
Matt Waddelce0fbcd2011-02-24 16:35:23 +000035/* Set block count limit because of 16 bit register limit on some hardware*/
36#ifndef CONFIG_SYS_MMC_MAX_BLK_COUNT
37#define CONFIG_SYS_MMC_MAX_BLK_COUNT 65535
38#endif
39
Andy Fleming272cc702008-10-30 16:41:01 -050040static struct list_head mmc_devices;
41static int cur_dev_num = -1;
42
Stefano Babic11fdade2010-02-05 15:04:43 +010043int __board_mmc_getcd(u8 *cd, struct mmc *mmc) {
44 return -1;
45}
46
47int board_mmc_getcd(u8 *cd, struct mmc *mmc)__attribute__((weak,
48 alias("__board_mmc_getcd")));
49
Andy Fleming272cc702008-10-30 16:41:01 -050050int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
51{
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +000052#ifdef CONFIG_MMC_TRACE
53 int ret;
54 int i;
55 u8 *ptr;
56
57 printf("CMD_SEND:%d\n", cmd->cmdidx);
58 printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
59 printf("\t\tFLAG\t\t\t %d\n", cmd->flags);
60 ret = mmc->send_cmd(mmc, cmd, data);
61 switch (cmd->resp_type) {
62 case MMC_RSP_NONE:
63 printf("\t\tMMC_RSP_NONE\n");
64 break;
65 case MMC_RSP_R1:
66 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
67 cmd->response[0]);
68 break;
69 case MMC_RSP_R1b:
70 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
71 cmd->response[0]);
72 break;
73 case MMC_RSP_R2:
74 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
75 cmd->response[0]);
76 printf("\t\t \t\t 0x%08X \n",
77 cmd->response[1]);
78 printf("\t\t \t\t 0x%08X \n",
79 cmd->response[2]);
80 printf("\t\t \t\t 0x%08X \n",
81 cmd->response[3]);
82 printf("\n");
83 printf("\t\t\t\t\tDUMPING DATA\n");
84 for (i = 0; i < 4; i++) {
85 int j;
86 printf("\t\t\t\t\t%03d - ", i*4);
87 ptr = &cmd->response[i];
88 ptr += 3;
89 for (j = 0; j < 4; j++)
90 printf("%02X ", *ptr--);
91 printf("\n");
92 }
93 break;
94 case MMC_RSP_R3:
95 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
96 cmd->response[0]);
97 break;
98 default:
99 printf("\t\tERROR MMC rsp not supported\n");
100 break;
101 }
102 return ret;
103#else
Andy Fleming272cc702008-10-30 16:41:01 -0500104 return mmc->send_cmd(mmc, cmd, data);
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000105#endif
Andy Fleming272cc702008-10-30 16:41:01 -0500106}
107
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000108int mmc_send_status(struct mmc *mmc, int timeout)
109{
110 struct mmc_cmd cmd;
111 int err;
112#ifdef CONFIG_MMC_TRACE
113 int status;
114#endif
115
116 cmd.cmdidx = MMC_CMD_SEND_STATUS;
117 cmd.resp_type = MMC_RSP_R1;
118 cmd.cmdarg = 0;
119 cmd.flags = 0;
120
121 do {
122 err = mmc_send_cmd(mmc, &cmd, NULL);
123 if (err)
124 return err;
125 else if (cmd.response[0] & MMC_STATUS_RDY_FOR_DATA)
126 break;
127
128 udelay(1000);
129
130 if (cmd.response[0] & MMC_STATUS_MASK) {
131 printf("Status Error: 0x%08X\n", cmd.response[0]);
132 return COMM_ERR;
133 }
134 } while (timeout--);
135
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000136#ifdef CONFIG_MMC_TRACE
137 status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
138 printf("CURR STATE:%d\n", status);
139#endif
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000140 if (!timeout) {
141 printf("Timeout waiting card ready\n");
142 return TIMEOUT;
143 }
144
145 return 0;
146}
147
Andy Fleming272cc702008-10-30 16:41:01 -0500148int mmc_set_blocklen(struct mmc *mmc, int len)
149{
150 struct mmc_cmd cmd;
151
152 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
153 cmd.resp_type = MMC_RSP_R1;
154 cmd.cmdarg = len;
155 cmd.flags = 0;
156
157 return mmc_send_cmd(mmc, &cmd, NULL);
158}
159
160struct mmc *find_mmc_device(int dev_num)
161{
162 struct mmc *m;
163 struct list_head *entry;
164
165 list_for_each(entry, &mmc_devices) {
166 m = list_entry(entry, struct mmc, link);
167
168 if (m->block_dev.dev == dev_num)
169 return m;
170 }
171
172 printf("MMC Device %d not found\n", dev_num);
173
174 return NULL;
175}
176
Lei Wene6f99a52011-06-22 17:03:31 +0000177static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
178{
179 struct mmc_cmd cmd;
180 ulong end;
181 int err, start_cmd, end_cmd;
182
183 if (mmc->high_capacity)
184 end = start + blkcnt - 1;
185 else {
186 end = (start + blkcnt - 1) * mmc->write_bl_len;
187 start *= mmc->write_bl_len;
188 }
189
190 if (IS_SD(mmc)) {
191 start_cmd = SD_CMD_ERASE_WR_BLK_START;
192 end_cmd = SD_CMD_ERASE_WR_BLK_END;
193 } else {
194 start_cmd = MMC_CMD_ERASE_GROUP_START;
195 end_cmd = MMC_CMD_ERASE_GROUP_END;
196 }
197
198 cmd.cmdidx = start_cmd;
199 cmd.cmdarg = start;
200 cmd.resp_type = MMC_RSP_R1;
201 cmd.flags = 0;
202
203 err = mmc_send_cmd(mmc, &cmd, NULL);
204 if (err)
205 goto err_out;
206
207 cmd.cmdidx = end_cmd;
208 cmd.cmdarg = end;
209
210 err = mmc_send_cmd(mmc, &cmd, NULL);
211 if (err)
212 goto err_out;
213
214 cmd.cmdidx = MMC_CMD_ERASE;
215 cmd.cmdarg = SECURE_ERASE;
216 cmd.resp_type = MMC_RSP_R1b;
217
218 err = mmc_send_cmd(mmc, &cmd, NULL);
219 if (err)
220 goto err_out;
221
222 return 0;
223
224err_out:
225 puts("mmc erase failed\n");
226 return err;
227}
228
229static unsigned long
230mmc_berase(int dev_num, unsigned long start, lbaint_t blkcnt)
231{
232 int err = 0;
233 struct mmc *mmc = find_mmc_device(dev_num);
234 lbaint_t blk = 0, blk_r = 0;
235
236 if (!mmc)
237 return -1;
238
239 if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size))
240 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
241 "The erase range would be change to 0x%lx~0x%lx\n\n",
242 mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
243 ((start + blkcnt + mmc->erase_grp_size)
244 & ~(mmc->erase_grp_size - 1)) - 1);
245
246 while (blk < blkcnt) {
247 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
248 mmc->erase_grp_size : (blkcnt - blk);
249 err = mmc_erase_t(mmc, start + blk, blk_r);
250 if (err)
251 break;
252
253 blk += blk_r;
254 }
255
256 return blk;
257}
258
Andy Fleming272cc702008-10-30 16:41:01 -0500259static ulong
Lei Wen01581262010-10-14 13:38:11 +0800260mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
Andy Fleming272cc702008-10-30 16:41:01 -0500261{
262 struct mmc_cmd cmd;
263 struct mmc_data data;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000264 int timeout = 1000;
Andy Fleming272cc702008-10-30 16:41:01 -0500265
Lei Wend2bf29e2010-09-13 22:07:27 +0800266 if ((start + blkcnt) > mmc->block_dev.lba) {
Steve Sakomandef412b2010-10-28 09:00:26 -0700267 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
Lei Wend2bf29e2010-09-13 22:07:27 +0800268 start + blkcnt, mmc->block_dev.lba);
269 return 0;
270 }
Andy Fleming272cc702008-10-30 16:41:01 -0500271
272 if (blkcnt > 1)
273 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
274 else
275 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
276
277 if (mmc->high_capacity)
278 cmd.cmdarg = start;
279 else
Steve Sakomandef412b2010-10-28 09:00:26 -0700280 cmd.cmdarg = start * mmc->write_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500281
282 cmd.resp_type = MMC_RSP_R1;
283 cmd.flags = 0;
284
285 data.src = src;
286 data.blocks = blkcnt;
Steve Sakomandef412b2010-10-28 09:00:26 -0700287 data.blocksize = mmc->write_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500288 data.flags = MMC_DATA_WRITE;
289
Steve Sakomandef412b2010-10-28 09:00:26 -0700290 if (mmc_send_cmd(mmc, &cmd, &data)) {
291 printf("mmc write failed\n");
292 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500293 }
294
Thomas Choud52ebf12010-12-24 13:12:21 +0000295 /* SPI multiblock writes terminate using a special
296 * token, not a STOP_TRANSMISSION request.
297 */
298 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
Andy Fleming272cc702008-10-30 16:41:01 -0500299 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
300 cmd.cmdarg = 0;
301 cmd.resp_type = MMC_RSP_R1b;
302 cmd.flags = 0;
Steve Sakomandef412b2010-10-28 09:00:26 -0700303 if (mmc_send_cmd(mmc, &cmd, NULL)) {
304 printf("mmc fail to send stop cmd\n");
305 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800306 }
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000307
308 /* Waiting for the ready status */
309 mmc_send_status(mmc, timeout);
Andy Fleming272cc702008-10-30 16:41:01 -0500310 }
311
312 return blkcnt;
313}
314
Lei Wen01581262010-10-14 13:38:11 +0800315static ulong
316mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
317{
Lei Wen01581262010-10-14 13:38:11 +0800318 lbaint_t cur, blocks_todo = blkcnt;
319
Steve Sakomandef412b2010-10-28 09:00:26 -0700320 struct mmc *mmc = find_mmc_device(dev_num);
Lei Wen01581262010-10-14 13:38:11 +0800321 if (!mmc)
Steve Sakomandef412b2010-10-28 09:00:26 -0700322 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800323
Steve Sakomandef412b2010-10-28 09:00:26 -0700324 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
325 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800326
327 do {
John Rigby8feafcc2011-04-18 05:50:08 +0000328 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
Lei Wen01581262010-10-14 13:38:11 +0800329 if(mmc_write_blocks(mmc, start, cur, src) != cur)
Steve Sakomandef412b2010-10-28 09:00:26 -0700330 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800331 blocks_todo -= cur;
332 start += cur;
333 src += cur * mmc->write_bl_len;
334 } while (blocks_todo > 0);
335
336 return blkcnt;
337}
338
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700339int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt)
Andy Fleming272cc702008-10-30 16:41:01 -0500340{
341 struct mmc_cmd cmd;
342 struct mmc_data data;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000343 int timeout = 1000;
Andy Fleming272cc702008-10-30 16:41:01 -0500344
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700345 if (blkcnt > 1)
346 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
347 else
348 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Fleming272cc702008-10-30 16:41:01 -0500349
350 if (mmc->high_capacity)
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700351 cmd.cmdarg = start;
Andy Fleming272cc702008-10-30 16:41:01 -0500352 else
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700353 cmd.cmdarg = start * mmc->read_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500354
355 cmd.resp_type = MMC_RSP_R1;
356 cmd.flags = 0;
357
358 data.dest = dst;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700359 data.blocks = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500360 data.blocksize = mmc->read_bl_len;
361 data.flags = MMC_DATA_READ;
362
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700363 if (mmc_send_cmd(mmc, &cmd, &data))
364 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500365
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700366 if (blkcnt > 1) {
367 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
368 cmd.cmdarg = 0;
369 cmd.resp_type = MMC_RSP_R1b;
370 cmd.flags = 0;
371 if (mmc_send_cmd(mmc, &cmd, NULL)) {
372 printf("mmc fail to send stop cmd\n");
373 return 0;
374 }
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000375
376 /* Waiting for the ready status */
377 mmc_send_status(mmc, timeout);
Andy Fleming272cc702008-10-30 16:41:01 -0500378 }
379
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700380 return blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500381}
382
383static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
384{
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700385 lbaint_t cur, blocks_todo = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500386
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700387 if (blkcnt == 0)
388 return 0;
389
390 struct mmc *mmc = find_mmc_device(dev_num);
Andy Fleming272cc702008-10-30 16:41:01 -0500391 if (!mmc)
392 return 0;
393
Lei Wend2bf29e2010-09-13 22:07:27 +0800394 if ((start + blkcnt) > mmc->block_dev.lba) {
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700395 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
Lei Wend2bf29e2010-09-13 22:07:27 +0800396 start + blkcnt, mmc->block_dev.lba);
397 return 0;
398 }
Andy Fleming272cc702008-10-30 16:41:01 -0500399
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700400 if (mmc_set_blocklen(mmc, mmc->read_bl_len))
Andy Fleming272cc702008-10-30 16:41:01 -0500401 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500402
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700403 do {
John Rigby8feafcc2011-04-18 05:50:08 +0000404 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700405 if(mmc_read_blocks(mmc, dst, start, cur) != cur)
406 return 0;
407 blocks_todo -= cur;
408 start += cur;
409 dst += cur * mmc->read_bl_len;
410 } while (blocks_todo > 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500411
412 return blkcnt;
413}
414
415int mmc_go_idle(struct mmc* mmc)
416{
417 struct mmc_cmd cmd;
418 int err;
419
420 udelay(1000);
421
422 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
423 cmd.cmdarg = 0;
424 cmd.resp_type = MMC_RSP_NONE;
425 cmd.flags = 0;
426
427 err = mmc_send_cmd(mmc, &cmd, NULL);
428
429 if (err)
430 return err;
431
432 udelay(2000);
433
434 return 0;
435}
436
437int
438sd_send_op_cond(struct mmc *mmc)
439{
440 int timeout = 1000;
441 int err;
442 struct mmc_cmd cmd;
443
444 do {
445 cmd.cmdidx = MMC_CMD_APP_CMD;
446 cmd.resp_type = MMC_RSP_R1;
447 cmd.cmdarg = 0;
448 cmd.flags = 0;
449
450 err = mmc_send_cmd(mmc, &cmd, NULL);
451
452 if (err)
453 return err;
454
455 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
456 cmd.resp_type = MMC_RSP_R3;
Stefano Babic250de122010-01-20 18:20:39 +0100457
458 /*
459 * Most cards do not answer if some reserved bits
460 * in the ocr are set. However, Some controller
461 * can set bit 7 (reserved for low voltages), but
462 * how to manage low voltages SD card is not yet
463 * specified.
464 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000465 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
466 (mmc->voltages & 0xff8000);
Andy Fleming272cc702008-10-30 16:41:01 -0500467
468 if (mmc->version == SD_VERSION_2)
469 cmd.cmdarg |= OCR_HCS;
470
471 err = mmc_send_cmd(mmc, &cmd, NULL);
472
473 if (err)
474 return err;
475
476 udelay(1000);
477 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
478
479 if (timeout <= 0)
480 return UNUSABLE_ERR;
481
482 if (mmc->version != SD_VERSION_2)
483 mmc->version = SD_VERSION_1_0;
484
Thomas Choud52ebf12010-12-24 13:12:21 +0000485 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
486 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
487 cmd.resp_type = MMC_RSP_R3;
488 cmd.cmdarg = 0;
489 cmd.flags = 0;
490
491 err = mmc_send_cmd(mmc, &cmd, NULL);
492
493 if (err)
494 return err;
495 }
496
Rabin Vincent998be3d2009-04-05 13:30:56 +0530497 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500498
499 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
500 mmc->rca = 0;
501
502 return 0;
503}
504
505int mmc_send_op_cond(struct mmc *mmc)
506{
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000507 int timeout = 10000;
Andy Fleming272cc702008-10-30 16:41:01 -0500508 struct mmc_cmd cmd;
509 int err;
510
511 /* Some cards seem to need this */
512 mmc_go_idle(mmc);
513
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000514 /* Asking to the card its capabilities */
515 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
516 cmd.resp_type = MMC_RSP_R3;
517 cmd.cmdarg = 0;
518 cmd.flags = 0;
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200519
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000520 err = mmc_send_cmd(mmc, &cmd, NULL);
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200521
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000522 if (err)
523 return err;
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200524
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000525 udelay(1000);
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200526
Andy Fleming272cc702008-10-30 16:41:01 -0500527 do {
528 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
529 cmd.resp_type = MMC_RSP_R3;
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000530 cmd.cmdarg = (mmc_host_is_spi(mmc) ? 0 :
531 (mmc->voltages &
532 (cmd.response[0] & OCR_VOLTAGE_MASK)) |
533 (cmd.response[0] & OCR_ACCESS_MODE));
Andy Fleming272cc702008-10-30 16:41:01 -0500534 cmd.flags = 0;
535
536 err = mmc_send_cmd(mmc, &cmd, NULL);
537
538 if (err)
539 return err;
540
541 udelay(1000);
542 } while (!(cmd.response[0] & OCR_BUSY) && timeout--);
543
544 if (timeout <= 0)
545 return UNUSABLE_ERR;
546
Thomas Choud52ebf12010-12-24 13:12:21 +0000547 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
548 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
549 cmd.resp_type = MMC_RSP_R3;
550 cmd.cmdarg = 0;
551 cmd.flags = 0;
552
553 err = mmc_send_cmd(mmc, &cmd, NULL);
554
555 if (err)
556 return err;
557 }
558
Andy Fleming272cc702008-10-30 16:41:01 -0500559 mmc->version = MMC_VERSION_UNKNOWN;
Rabin Vincent998be3d2009-04-05 13:30:56 +0530560 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500561
562 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
563 mmc->rca = 0;
564
565 return 0;
566}
567
568
569int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd)
570{
571 struct mmc_cmd cmd;
572 struct mmc_data data;
573 int err;
574
575 /* Get the Card Status Register */
576 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
577 cmd.resp_type = MMC_RSP_R1;
578 cmd.cmdarg = 0;
579 cmd.flags = 0;
580
581 data.dest = ext_csd;
582 data.blocks = 1;
583 data.blocksize = 512;
584 data.flags = MMC_DATA_READ;
585
586 err = mmc_send_cmd(mmc, &cmd, &data);
587
588 return err;
589}
590
591
592int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
593{
594 struct mmc_cmd cmd;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000595 int timeout = 1000;
596 int ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500597
598 cmd.cmdidx = MMC_CMD_SWITCH;
599 cmd.resp_type = MMC_RSP_R1b;
600 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000601 (index << 16) |
602 (value << 8);
Andy Fleming272cc702008-10-30 16:41:01 -0500603 cmd.flags = 0;
604
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000605 ret = mmc_send_cmd(mmc, &cmd, NULL);
606
607 /* Waiting for the ready status */
608 mmc_send_status(mmc, timeout);
609
610 return ret;
611
Andy Fleming272cc702008-10-30 16:41:01 -0500612}
613
614int mmc_change_freq(struct mmc *mmc)
615{
616 char ext_csd[512];
617 char cardtype;
618 int err;
619
620 mmc->card_caps = 0;
621
Thomas Choud52ebf12010-12-24 13:12:21 +0000622 if (mmc_host_is_spi(mmc))
623 return 0;
624
Andy Fleming272cc702008-10-30 16:41:01 -0500625 /* Only version 4 supports high-speed */
626 if (mmc->version < MMC_VERSION_4)
627 return 0;
628
629 mmc->card_caps |= MMC_MODE_4BIT;
630
631 err = mmc_send_ext_csd(mmc, ext_csd);
632
633 if (err)
634 return err;
635
Andy Fleming272cc702008-10-30 16:41:01 -0500636 cardtype = ext_csd[196] & 0xf;
637
638 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
639
640 if (err)
641 return err;
642
643 /* Now check to see that it worked */
644 err = mmc_send_ext_csd(mmc, ext_csd);
645
646 if (err)
647 return err;
648
649 /* No high-speed support */
650 if (!ext_csd[185])
651 return 0;
652
653 /* High Speed is set, there are two types: 52MHz and 26MHz */
654 if (cardtype & MMC_HS_52MHZ)
655 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
656 else
657 mmc->card_caps |= MMC_MODE_HS;
658
659 return 0;
660}
661
Lei Wenbc897b12011-05-02 16:26:26 +0000662int mmc_switch_part(int dev_num, unsigned int part_num)
663{
664 struct mmc *mmc = find_mmc_device(dev_num);
665
666 if (!mmc)
667 return -1;
668
669 return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
670 (mmc->part_config & ~PART_ACCESS_MASK)
671 | (part_num & PART_ACCESS_MASK));
672}
673
Andy Fleming272cc702008-10-30 16:41:01 -0500674int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
675{
676 struct mmc_cmd cmd;
677 struct mmc_data data;
678
679 /* Switch the frequency */
680 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
681 cmd.resp_type = MMC_RSP_R1;
682 cmd.cmdarg = (mode << 31) | 0xffffff;
683 cmd.cmdarg &= ~(0xf << (group * 4));
684 cmd.cmdarg |= value << (group * 4);
685 cmd.flags = 0;
686
687 data.dest = (char *)resp;
688 data.blocksize = 64;
689 data.blocks = 1;
690 data.flags = MMC_DATA_READ;
691
692 return mmc_send_cmd(mmc, &cmd, &data);
693}
694
695
696int sd_change_freq(struct mmc *mmc)
697{
698 int err;
699 struct mmc_cmd cmd;
700 uint scr[2];
701 uint switch_status[16];
702 struct mmc_data data;
703 int timeout;
704
705 mmc->card_caps = 0;
706
Thomas Choud52ebf12010-12-24 13:12:21 +0000707 if (mmc_host_is_spi(mmc))
708 return 0;
709
Andy Fleming272cc702008-10-30 16:41:01 -0500710 /* Read the SCR to find out if this card supports higher speeds */
711 cmd.cmdidx = MMC_CMD_APP_CMD;
712 cmd.resp_type = MMC_RSP_R1;
713 cmd.cmdarg = mmc->rca << 16;
714 cmd.flags = 0;
715
716 err = mmc_send_cmd(mmc, &cmd, NULL);
717
718 if (err)
719 return err;
720
721 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
722 cmd.resp_type = MMC_RSP_R1;
723 cmd.cmdarg = 0;
724 cmd.flags = 0;
725
726 timeout = 3;
727
728retry_scr:
729 data.dest = (char *)&scr;
730 data.blocksize = 8;
731 data.blocks = 1;
732 data.flags = MMC_DATA_READ;
733
734 err = mmc_send_cmd(mmc, &cmd, &data);
735
736 if (err) {
737 if (timeout--)
738 goto retry_scr;
739
740 return err;
741 }
742
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300743 mmc->scr[0] = __be32_to_cpu(scr[0]);
744 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Fleming272cc702008-10-30 16:41:01 -0500745
746 switch ((mmc->scr[0] >> 24) & 0xf) {
747 case 0:
748 mmc->version = SD_VERSION_1_0;
749 break;
750 case 1:
751 mmc->version = SD_VERSION_1_10;
752 break;
753 case 2:
754 mmc->version = SD_VERSION_2;
755 break;
756 default:
757 mmc->version = SD_VERSION_1_0;
758 break;
759 }
760
Alagu Sankarb44c7082010-05-12 15:08:24 +0530761 if (mmc->scr[0] & SD_DATA_4BIT)
762 mmc->card_caps |= MMC_MODE_4BIT;
763
Andy Fleming272cc702008-10-30 16:41:01 -0500764 /* Version 1.0 doesn't support switching */
765 if (mmc->version == SD_VERSION_1_0)
766 return 0;
767
768 timeout = 4;
769 while (timeout--) {
770 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
771 (u8 *)&switch_status);
772
773 if (err)
774 return err;
775
776 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300777 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Fleming272cc702008-10-30 16:41:01 -0500778 break;
779 }
780
Andy Fleming272cc702008-10-30 16:41:01 -0500781 /* If high-speed isn't supported, we return */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300782 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
Andy Fleming272cc702008-10-30 16:41:01 -0500783 return 0;
784
785 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)&switch_status);
786
787 if (err)
788 return err;
789
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300790 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
Andy Fleming272cc702008-10-30 16:41:01 -0500791 mmc->card_caps |= MMC_MODE_HS;
792
793 return 0;
794}
795
796/* frequency bases */
797/* divided by 10 to be nice to platforms without floating point */
Mike Frysinger5f837c22010-10-20 01:15:53 +0000798static const int fbase[] = {
Andy Fleming272cc702008-10-30 16:41:01 -0500799 10000,
800 100000,
801 1000000,
802 10000000,
803};
804
805/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
806 * to platforms without floating point.
807 */
Mike Frysinger5f837c22010-10-20 01:15:53 +0000808static const int multipliers[] = {
Andy Fleming272cc702008-10-30 16:41:01 -0500809 0, /* reserved */
810 10,
811 12,
812 13,
813 15,
814 20,
815 25,
816 30,
817 35,
818 40,
819 45,
820 50,
821 55,
822 60,
823 70,
824 80,
825};
826
827void mmc_set_ios(struct mmc *mmc)
828{
829 mmc->set_ios(mmc);
830}
831
832void mmc_set_clock(struct mmc *mmc, uint clock)
833{
834 if (clock > mmc->f_max)
835 clock = mmc->f_max;
836
837 if (clock < mmc->f_min)
838 clock = mmc->f_min;
839
840 mmc->clock = clock;
841
842 mmc_set_ios(mmc);
843}
844
845void mmc_set_bus_width(struct mmc *mmc, uint width)
846{
847 mmc->bus_width = width;
848
849 mmc_set_ios(mmc);
850}
851
852int mmc_startup(struct mmc *mmc)
853{
854 int err;
855 uint mult, freq;
Yoshihiro Shimoda639b7822011-07-04 22:13:26 +0000856 u64 cmult, csize, capacity;
Andy Fleming272cc702008-10-30 16:41:01 -0500857 struct mmc_cmd cmd;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +0530858 char ext_csd[512];
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000859 int timeout = 1000;
Andy Fleming272cc702008-10-30 16:41:01 -0500860
Thomas Choud52ebf12010-12-24 13:12:21 +0000861#ifdef CONFIG_MMC_SPI_CRC_ON
862 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
863 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
864 cmd.resp_type = MMC_RSP_R1;
865 cmd.cmdarg = 1;
866 cmd.flags = 0;
867 err = mmc_send_cmd(mmc, &cmd, NULL);
868
869 if (err)
870 return err;
871 }
872#endif
873
Andy Fleming272cc702008-10-30 16:41:01 -0500874 /* Put the Card in Identify Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +0000875 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
876 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
Andy Fleming272cc702008-10-30 16:41:01 -0500877 cmd.resp_type = MMC_RSP_R2;
878 cmd.cmdarg = 0;
879 cmd.flags = 0;
880
881 err = mmc_send_cmd(mmc, &cmd, NULL);
882
883 if (err)
884 return err;
885
886 memcpy(mmc->cid, cmd.response, 16);
887
888 /*
889 * For MMC cards, set the Relative Address.
890 * For SD cards, get the Relatvie Address.
891 * This also puts the cards into Standby State
892 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000893 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
894 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
895 cmd.cmdarg = mmc->rca << 16;
896 cmd.resp_type = MMC_RSP_R6;
897 cmd.flags = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500898
Thomas Choud52ebf12010-12-24 13:12:21 +0000899 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -0500900
Thomas Choud52ebf12010-12-24 13:12:21 +0000901 if (err)
902 return err;
Andy Fleming272cc702008-10-30 16:41:01 -0500903
Thomas Choud52ebf12010-12-24 13:12:21 +0000904 if (IS_SD(mmc))
905 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
906 }
Andy Fleming272cc702008-10-30 16:41:01 -0500907
908 /* Get the Card-Specific Data */
909 cmd.cmdidx = MMC_CMD_SEND_CSD;
910 cmd.resp_type = MMC_RSP_R2;
911 cmd.cmdarg = mmc->rca << 16;
912 cmd.flags = 0;
913
914 err = mmc_send_cmd(mmc, &cmd, NULL);
915
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000916 /* Waiting for the ready status */
917 mmc_send_status(mmc, timeout);
918
Andy Fleming272cc702008-10-30 16:41:01 -0500919 if (err)
920 return err;
921
Rabin Vincent998be3d2009-04-05 13:30:56 +0530922 mmc->csd[0] = cmd.response[0];
923 mmc->csd[1] = cmd.response[1];
924 mmc->csd[2] = cmd.response[2];
925 mmc->csd[3] = cmd.response[3];
Andy Fleming272cc702008-10-30 16:41:01 -0500926
927 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincent0b453ff2009-04-05 13:30:55 +0530928 int version = (cmd.response[0] >> 26) & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -0500929
930 switch (version) {
931 case 0:
932 mmc->version = MMC_VERSION_1_2;
933 break;
934 case 1:
935 mmc->version = MMC_VERSION_1_4;
936 break;
937 case 2:
938 mmc->version = MMC_VERSION_2_2;
939 break;
940 case 3:
941 mmc->version = MMC_VERSION_3;
942 break;
943 case 4:
944 mmc->version = MMC_VERSION_4;
945 break;
946 default:
947 mmc->version = MMC_VERSION_1_2;
948 break;
949 }
950 }
951
952 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincent0b453ff2009-04-05 13:30:55 +0530953 freq = fbase[(cmd.response[0] & 0x7)];
954 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Fleming272cc702008-10-30 16:41:01 -0500955
956 mmc->tran_speed = freq * mult;
957
Rabin Vincent998be3d2009-04-05 13:30:56 +0530958 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -0500959
960 if (IS_SD(mmc))
961 mmc->write_bl_len = mmc->read_bl_len;
962 else
Rabin Vincent998be3d2009-04-05 13:30:56 +0530963 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -0500964
965 if (mmc->high_capacity) {
966 csize = (mmc->csd[1] & 0x3f) << 16
967 | (mmc->csd[2] & 0xffff0000) >> 16;
968 cmult = 8;
969 } else {
970 csize = (mmc->csd[1] & 0x3ff) << 2
971 | (mmc->csd[2] & 0xc0000000) >> 30;
972 cmult = (mmc->csd[2] & 0x00038000) >> 15;
973 }
974
975 mmc->capacity = (csize + 1) << (cmult + 2);
976 mmc->capacity *= mmc->read_bl_len;
977
978 if (mmc->read_bl_len > 512)
979 mmc->read_bl_len = 512;
980
981 if (mmc->write_bl_len > 512)
982 mmc->write_bl_len = 512;
983
984 /* Select the card, and put it into Transfer Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +0000985 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
986 cmd.cmdidx = MMC_CMD_SELECT_CARD;
987 cmd.resp_type = MMC_RSP_R1b;
988 cmd.cmdarg = mmc->rca << 16;
989 cmd.flags = 0;
990 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -0500991
Thomas Choud52ebf12010-12-24 13:12:21 +0000992 if (err)
993 return err;
994 }
Andy Fleming272cc702008-10-30 16:41:01 -0500995
Lei Wene6f99a52011-06-22 17:03:31 +0000996 /*
997 * For SD, its erase group is always one sector
998 */
999 mmc->erase_grp_size = 1;
Lei Wenbc897b12011-05-02 16:26:26 +00001000 mmc->part_config = MMCPART_NOAVAILABLE;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05301001 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
1002 /* check ext_csd version and capacity */
1003 err = mmc_send_ext_csd(mmc, ext_csd);
1004 if (!err & (ext_csd[192] >= 2)) {
Yoshihiro Shimoda639b7822011-07-04 22:13:26 +00001005 /*
1006 * According to the JEDEC Standard, the value of
1007 * ext_csd's capacity is valid if the value is more
1008 * than 2GB
1009 */
1010 capacity = ext_csd[212] << 0 | ext_csd[213] << 8 |
1011 ext_csd[214] << 16 | ext_csd[215] << 24;
1012 capacity *= 512;
1013 if (capacity > 2 * 1024 * 1024 * 1024)
1014 mmc->capacity = capacity;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05301015 }
Lei Wenbc897b12011-05-02 16:26:26 +00001016
Lei Wene6f99a52011-06-22 17:03:31 +00001017 /*
1018 * Check whether GROUP_DEF is set, if yes, read out
1019 * group size from ext_csd directly, or calculate
1020 * the group size from the csd value.
1021 */
1022 if (ext_csd[175])
1023 mmc->erase_grp_size = ext_csd[224] * 512 * 1024;
1024 else {
1025 int erase_gsz, erase_gmul;
1026 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1027 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1028 mmc->erase_grp_size = (erase_gsz + 1)
1029 * (erase_gmul + 1);
1030 }
1031
Lei Wenbc897b12011-05-02 16:26:26 +00001032 /* store the partition info of emmc */
1033 if (ext_csd[160] & PART_SUPPORT)
1034 mmc->part_config = ext_csd[179];
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05301035 }
1036
Andy Fleming272cc702008-10-30 16:41:01 -05001037 if (IS_SD(mmc))
1038 err = sd_change_freq(mmc);
1039 else
1040 err = mmc_change_freq(mmc);
1041
1042 if (err)
1043 return err;
1044
1045 /* Restrict card's capabilities by what the host can do */
1046 mmc->card_caps &= mmc->host_caps;
1047
1048 if (IS_SD(mmc)) {
1049 if (mmc->card_caps & MMC_MODE_4BIT) {
1050 cmd.cmdidx = MMC_CMD_APP_CMD;
1051 cmd.resp_type = MMC_RSP_R1;
1052 cmd.cmdarg = mmc->rca << 16;
1053 cmd.flags = 0;
1054
1055 err = mmc_send_cmd(mmc, &cmd, NULL);
1056 if (err)
1057 return err;
1058
1059 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1060 cmd.resp_type = MMC_RSP_R1;
1061 cmd.cmdarg = 2;
1062 cmd.flags = 0;
1063 err = mmc_send_cmd(mmc, &cmd, NULL);
1064 if (err)
1065 return err;
1066
1067 mmc_set_bus_width(mmc, 4);
1068 }
1069
1070 if (mmc->card_caps & MMC_MODE_HS)
1071 mmc_set_clock(mmc, 50000000);
1072 else
1073 mmc_set_clock(mmc, 25000000);
1074 } else {
1075 if (mmc->card_caps & MMC_MODE_4BIT) {
1076 /* Set the card to use 4 bit*/
1077 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1078 EXT_CSD_BUS_WIDTH,
1079 EXT_CSD_BUS_WIDTH_4);
1080
1081 if (err)
1082 return err;
1083
1084 mmc_set_bus_width(mmc, 4);
1085 } else if (mmc->card_caps & MMC_MODE_8BIT) {
1086 /* Set the card to use 8 bit*/
1087 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1088 EXT_CSD_BUS_WIDTH,
1089 EXT_CSD_BUS_WIDTH_8);
1090
1091 if (err)
1092 return err;
1093
1094 mmc_set_bus_width(mmc, 8);
1095 }
1096
1097 if (mmc->card_caps & MMC_MODE_HS) {
1098 if (mmc->card_caps & MMC_MODE_HS_52MHz)
1099 mmc_set_clock(mmc, 52000000);
1100 else
1101 mmc_set_clock(mmc, 26000000);
1102 } else
1103 mmc_set_clock(mmc, 20000000);
1104 }
1105
1106 /* fill in device description */
1107 mmc->block_dev.lun = 0;
1108 mmc->block_dev.type = 0;
1109 mmc->block_dev.blksz = mmc->read_bl_len;
Rabin Vincent9b1f9422009-04-05 13:30:54 +05301110 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
Rabin Vincent0b453ff2009-04-05 13:30:55 +05301111 sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8,
1112 (mmc->cid[2] << 8) | (mmc->cid[3] >> 24));
1113 sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff,
1114 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1115 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
1116 sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28,
1117 (mmc->cid[2] >> 24) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -05001118 init_part(&mmc->block_dev);
1119
1120 return 0;
1121}
1122
1123int mmc_send_if_cond(struct mmc *mmc)
1124{
1125 struct mmc_cmd cmd;
1126 int err;
1127
1128 cmd.cmdidx = SD_CMD_SEND_IF_COND;
1129 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1130 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
1131 cmd.resp_type = MMC_RSP_R7;
1132 cmd.flags = 0;
1133
1134 err = mmc_send_cmd(mmc, &cmd, NULL);
1135
1136 if (err)
1137 return err;
1138
Rabin Vincent998be3d2009-04-05 13:30:56 +05301139 if ((cmd.response[0] & 0xff) != 0xaa)
Andy Fleming272cc702008-10-30 16:41:01 -05001140 return UNUSABLE_ERR;
1141 else
1142 mmc->version = SD_VERSION_2;
1143
1144 return 0;
1145}
1146
1147int mmc_register(struct mmc *mmc)
1148{
1149 /* Setup the universal parts of the block interface just once */
1150 mmc->block_dev.if_type = IF_TYPE_MMC;
1151 mmc->block_dev.dev = cur_dev_num++;
1152 mmc->block_dev.removable = 1;
1153 mmc->block_dev.block_read = mmc_bread;
1154 mmc->block_dev.block_write = mmc_bwrite;
Lei Wene6f99a52011-06-22 17:03:31 +00001155 mmc->block_dev.block_erase = mmc_berase;
John Rigby8feafcc2011-04-18 05:50:08 +00001156 if (!mmc->b_max)
1157 mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Andy Fleming272cc702008-10-30 16:41:01 -05001158
1159 INIT_LIST_HEAD (&mmc->link);
1160
1161 list_add_tail (&mmc->link, &mmc_devices);
1162
1163 return 0;
1164}
1165
1166block_dev_desc_t *mmc_get_dev(int dev)
1167{
1168 struct mmc *mmc = find_mmc_device(dev);
1169
Rabin Vincente85649c2009-04-05 13:30:53 +05301170 return mmc ? &mmc->block_dev : NULL;
Andy Fleming272cc702008-10-30 16:41:01 -05001171}
1172
1173int mmc_init(struct mmc *mmc)
1174{
1175 int err;
1176
Lei Wenbc897b12011-05-02 16:26:26 +00001177 if (mmc->has_init)
1178 return 0;
1179
Andy Fleming272cc702008-10-30 16:41:01 -05001180 err = mmc->init(mmc);
1181
1182 if (err)
1183 return err;
1184
Ilya Yanokb86b85e2009-06-29 17:53:16 +04001185 mmc_set_bus_width(mmc, 1);
1186 mmc_set_clock(mmc, 1);
1187
Andy Fleming272cc702008-10-30 16:41:01 -05001188 /* Reset the Card */
1189 err = mmc_go_idle(mmc);
1190
1191 if (err)
1192 return err;
1193
Lei Wenbc897b12011-05-02 16:26:26 +00001194 /* The internal partition reset to user partition(0) at every CMD0*/
1195 mmc->part_num = 0;
1196
Andy Fleming272cc702008-10-30 16:41:01 -05001197 /* Test for SD version 2 */
1198 err = mmc_send_if_cond(mmc);
1199
Andy Fleming272cc702008-10-30 16:41:01 -05001200 /* Now try to get the SD card's operating condition */
1201 err = sd_send_op_cond(mmc);
1202
1203 /* If the command timed out, we check for an MMC card */
1204 if (err == TIMEOUT) {
1205 err = mmc_send_op_cond(mmc);
1206
1207 if (err) {
1208 printf("Card did not respond to voltage select!\n");
1209 return UNUSABLE_ERR;
1210 }
1211 }
1212
Lei Wenbc897b12011-05-02 16:26:26 +00001213 err = mmc_startup(mmc);
1214 if (err)
1215 mmc->has_init = 0;
1216 else
1217 mmc->has_init = 1;
1218 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001219}
1220
1221/*
1222 * CPU and board-specific MMC initializations. Aliased function
1223 * signals caller to move on
1224 */
1225static int __def_mmc_init(bd_t *bis)
1226{
1227 return -1;
1228}
1229
Peter Tyserf9a109b2009-04-20 11:08:46 -05001230int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1231int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
Andy Fleming272cc702008-10-30 16:41:01 -05001232
1233void print_mmc_devices(char separator)
1234{
1235 struct mmc *m;
1236 struct list_head *entry;
1237
1238 list_for_each(entry, &mmc_devices) {
1239 m = list_entry(entry, struct mmc, link);
1240
1241 printf("%s: %d", m->name, m->block_dev.dev);
1242
1243 if (entry->next != &mmc_devices)
1244 printf("%c ", separator);
1245 }
1246
1247 printf("\n");
1248}
1249
Lei Wenea6ebe22011-05-02 16:26:25 +00001250int get_mmc_num(void)
1251{
1252 return cur_dev_num;
1253}
1254
Andy Fleming272cc702008-10-30 16:41:01 -05001255int mmc_initialize(bd_t *bis)
1256{
1257 INIT_LIST_HEAD (&mmc_devices);
1258 cur_dev_num = 0;
1259
1260 if (board_mmc_init(bis) < 0)
1261 cpu_mmc_init(bis);
1262
1263 print_mmc_devices(',');
1264
1265 return 0;
1266}