blob: 64c8d567de1004e4c3158f2728faf5f5d3ee04b7 [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{
52 return mmc->send_cmd(mmc, cmd, data);
53}
54
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +000055int mmc_send_status(struct mmc *mmc, int timeout)
56{
57 struct mmc_cmd cmd;
58 int err;
59#ifdef CONFIG_MMC_TRACE
60 int status;
61#endif
62
63 cmd.cmdidx = MMC_CMD_SEND_STATUS;
64 cmd.resp_type = MMC_RSP_R1;
65 cmd.cmdarg = 0;
66 cmd.flags = 0;
67
68 do {
69 err = mmc_send_cmd(mmc, &cmd, NULL);
70 if (err)
71 return err;
72 else if (cmd.response[0] & MMC_STATUS_RDY_FOR_DATA)
73 break;
74
75 udelay(1000);
76
77 if (cmd.response[0] & MMC_STATUS_MASK) {
78 printf("Status Error: 0x%08X\n", cmd.response[0]);
79 return COMM_ERR;
80 }
81 } while (timeout--);
82
83 if (!timeout) {
84 printf("Timeout waiting card ready\n");
85 return TIMEOUT;
86 }
87
88 return 0;
89}
90
Andy Fleming272cc702008-10-30 16:41:01 -050091int mmc_set_blocklen(struct mmc *mmc, int len)
92{
93 struct mmc_cmd cmd;
94
95 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
96 cmd.resp_type = MMC_RSP_R1;
97 cmd.cmdarg = len;
98 cmd.flags = 0;
99
100 return mmc_send_cmd(mmc, &cmd, NULL);
101}
102
103struct mmc *find_mmc_device(int dev_num)
104{
105 struct mmc *m;
106 struct list_head *entry;
107
108 list_for_each(entry, &mmc_devices) {
109 m = list_entry(entry, struct mmc, link);
110
111 if (m->block_dev.dev == dev_num)
112 return m;
113 }
114
115 printf("MMC Device %d not found\n", dev_num);
116
117 return NULL;
118}
119
120static ulong
Lei Wen01581262010-10-14 13:38:11 +0800121mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
Andy Fleming272cc702008-10-30 16:41:01 -0500122{
123 struct mmc_cmd cmd;
124 struct mmc_data data;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000125 int timeout = 1000;
Andy Fleming272cc702008-10-30 16:41:01 -0500126
Lei Wend2bf29e2010-09-13 22:07:27 +0800127 if ((start + blkcnt) > mmc->block_dev.lba) {
Steve Sakomandef412b2010-10-28 09:00:26 -0700128 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
Lei Wend2bf29e2010-09-13 22:07:27 +0800129 start + blkcnt, mmc->block_dev.lba);
130 return 0;
131 }
Andy Fleming272cc702008-10-30 16:41:01 -0500132
133 if (blkcnt > 1)
134 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
135 else
136 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
137
138 if (mmc->high_capacity)
139 cmd.cmdarg = start;
140 else
Steve Sakomandef412b2010-10-28 09:00:26 -0700141 cmd.cmdarg = start * mmc->write_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500142
143 cmd.resp_type = MMC_RSP_R1;
144 cmd.flags = 0;
145
146 data.src = src;
147 data.blocks = blkcnt;
Steve Sakomandef412b2010-10-28 09:00:26 -0700148 data.blocksize = mmc->write_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500149 data.flags = MMC_DATA_WRITE;
150
Steve Sakomandef412b2010-10-28 09:00:26 -0700151 if (mmc_send_cmd(mmc, &cmd, &data)) {
152 printf("mmc write failed\n");
153 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500154 }
155
Thomas Choud52ebf12010-12-24 13:12:21 +0000156 /* SPI multiblock writes terminate using a special
157 * token, not a STOP_TRANSMISSION request.
158 */
159 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
Andy Fleming272cc702008-10-30 16:41:01 -0500160 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
161 cmd.cmdarg = 0;
162 cmd.resp_type = MMC_RSP_R1b;
163 cmd.flags = 0;
Steve Sakomandef412b2010-10-28 09:00:26 -0700164 if (mmc_send_cmd(mmc, &cmd, NULL)) {
165 printf("mmc fail to send stop cmd\n");
166 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800167 }
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000168
169 /* Waiting for the ready status */
170 mmc_send_status(mmc, timeout);
Andy Fleming272cc702008-10-30 16:41:01 -0500171 }
172
173 return blkcnt;
174}
175
Lei Wen01581262010-10-14 13:38:11 +0800176static ulong
177mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
178{
Lei Wen01581262010-10-14 13:38:11 +0800179 lbaint_t cur, blocks_todo = blkcnt;
180
Steve Sakomandef412b2010-10-28 09:00:26 -0700181 struct mmc *mmc = find_mmc_device(dev_num);
Lei Wen01581262010-10-14 13:38:11 +0800182 if (!mmc)
Steve Sakomandef412b2010-10-28 09:00:26 -0700183 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800184
Steve Sakomandef412b2010-10-28 09:00:26 -0700185 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
186 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800187
188 do {
Matt Waddelce0fbcd2011-02-24 16:35:23 +0000189 cur = (blocks_todo > CONFIG_SYS_MMC_MAX_BLK_COUNT) ?
190 CONFIG_SYS_MMC_MAX_BLK_COUNT : blocks_todo;
Lei Wen01581262010-10-14 13:38:11 +0800191 if(mmc_write_blocks(mmc, start, cur, src) != cur)
Steve Sakomandef412b2010-10-28 09:00:26 -0700192 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800193 blocks_todo -= cur;
194 start += cur;
195 src += cur * mmc->write_bl_len;
196 } while (blocks_todo > 0);
197
198 return blkcnt;
199}
200
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700201int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt)
Andy Fleming272cc702008-10-30 16:41:01 -0500202{
203 struct mmc_cmd cmd;
204 struct mmc_data data;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000205 int timeout = 1000;
Andy Fleming272cc702008-10-30 16:41:01 -0500206
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700207 if (blkcnt > 1)
208 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
209 else
210 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Fleming272cc702008-10-30 16:41:01 -0500211
212 if (mmc->high_capacity)
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700213 cmd.cmdarg = start;
Andy Fleming272cc702008-10-30 16:41:01 -0500214 else
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700215 cmd.cmdarg = start * mmc->read_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500216
217 cmd.resp_type = MMC_RSP_R1;
218 cmd.flags = 0;
219
220 data.dest = dst;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700221 data.blocks = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500222 data.blocksize = mmc->read_bl_len;
223 data.flags = MMC_DATA_READ;
224
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700225 if (mmc_send_cmd(mmc, &cmd, &data))
226 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500227
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700228 if (blkcnt > 1) {
229 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
230 cmd.cmdarg = 0;
231 cmd.resp_type = MMC_RSP_R1b;
232 cmd.flags = 0;
233 if (mmc_send_cmd(mmc, &cmd, NULL)) {
234 printf("mmc fail to send stop cmd\n");
235 return 0;
236 }
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000237
238 /* Waiting for the ready status */
239 mmc_send_status(mmc, timeout);
Andy Fleming272cc702008-10-30 16:41:01 -0500240 }
241
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700242 return blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500243}
244
245static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
246{
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700247 lbaint_t cur, blocks_todo = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500248
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700249 if (blkcnt == 0)
250 return 0;
251
252 struct mmc *mmc = find_mmc_device(dev_num);
Andy Fleming272cc702008-10-30 16:41:01 -0500253 if (!mmc)
254 return 0;
255
Lei Wend2bf29e2010-09-13 22:07:27 +0800256 if ((start + blkcnt) > mmc->block_dev.lba) {
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700257 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
Lei Wend2bf29e2010-09-13 22:07:27 +0800258 start + blkcnt, mmc->block_dev.lba);
259 return 0;
260 }
Andy Fleming272cc702008-10-30 16:41:01 -0500261
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700262 if (mmc_set_blocklen(mmc, mmc->read_bl_len))
Andy Fleming272cc702008-10-30 16:41:01 -0500263 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500264
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700265 do {
Matt Waddelce0fbcd2011-02-24 16:35:23 +0000266 cur = (blocks_todo > CONFIG_SYS_MMC_MAX_BLK_COUNT) ?
267 CONFIG_SYS_MMC_MAX_BLK_COUNT : blocks_todo;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700268 if(mmc_read_blocks(mmc, dst, start, cur) != cur)
269 return 0;
270 blocks_todo -= cur;
271 start += cur;
272 dst += cur * mmc->read_bl_len;
273 } while (blocks_todo > 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500274
275 return blkcnt;
276}
277
278int mmc_go_idle(struct mmc* mmc)
279{
280 struct mmc_cmd cmd;
281 int err;
282
283 udelay(1000);
284
285 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
286 cmd.cmdarg = 0;
287 cmd.resp_type = MMC_RSP_NONE;
288 cmd.flags = 0;
289
290 err = mmc_send_cmd(mmc, &cmd, NULL);
291
292 if (err)
293 return err;
294
295 udelay(2000);
296
297 return 0;
298}
299
300int
301sd_send_op_cond(struct mmc *mmc)
302{
303 int timeout = 1000;
304 int err;
305 struct mmc_cmd cmd;
306
307 do {
308 cmd.cmdidx = MMC_CMD_APP_CMD;
309 cmd.resp_type = MMC_RSP_R1;
310 cmd.cmdarg = 0;
311 cmd.flags = 0;
312
313 err = mmc_send_cmd(mmc, &cmd, NULL);
314
315 if (err)
316 return err;
317
318 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
319 cmd.resp_type = MMC_RSP_R3;
Stefano Babic250de122010-01-20 18:20:39 +0100320
321 /*
322 * Most cards do not answer if some reserved bits
323 * in the ocr are set. However, Some controller
324 * can set bit 7 (reserved for low voltages), but
325 * how to manage low voltages SD card is not yet
326 * specified.
327 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000328 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
329 (mmc->voltages & 0xff8000);
Andy Fleming272cc702008-10-30 16:41:01 -0500330
331 if (mmc->version == SD_VERSION_2)
332 cmd.cmdarg |= OCR_HCS;
333
334 err = mmc_send_cmd(mmc, &cmd, NULL);
335
336 if (err)
337 return err;
338
339 udelay(1000);
340 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
341
342 if (timeout <= 0)
343 return UNUSABLE_ERR;
344
345 if (mmc->version != SD_VERSION_2)
346 mmc->version = SD_VERSION_1_0;
347
Thomas Choud52ebf12010-12-24 13:12:21 +0000348 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
349 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
350 cmd.resp_type = MMC_RSP_R3;
351 cmd.cmdarg = 0;
352 cmd.flags = 0;
353
354 err = mmc_send_cmd(mmc, &cmd, NULL);
355
356 if (err)
357 return err;
358 }
359
Rabin Vincent998be3d2009-04-05 13:30:56 +0530360 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500361
362 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
363 mmc->rca = 0;
364
365 return 0;
366}
367
368int mmc_send_op_cond(struct mmc *mmc)
369{
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000370 int timeout = 10000;
Andy Fleming272cc702008-10-30 16:41:01 -0500371 struct mmc_cmd cmd;
372 int err;
373
374 /* Some cards seem to need this */
375 mmc_go_idle(mmc);
376
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000377 /* Asking to the card its capabilities */
378 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
379 cmd.resp_type = MMC_RSP_R3;
380 cmd.cmdarg = 0;
381 cmd.flags = 0;
382
383 err = mmc_send_cmd(mmc, &cmd, NULL);
384
385 if (err)
386 return err;
387
388 udelay(1000);
389
Andy Fleming272cc702008-10-30 16:41:01 -0500390 do {
391 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
392 cmd.resp_type = MMC_RSP_R3;
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000393 cmd.cmdarg = (mmc_host_is_spi(mmc) ? 0 :
394 (mmc->voltages &
395 (cmd.response[0] & OCR_VOLTAGE_MASK)) |
396 (cmd.response[0] & OCR_ACCESS_MODE));
Andy Fleming272cc702008-10-30 16:41:01 -0500397 cmd.flags = 0;
398
399 err = mmc_send_cmd(mmc, &cmd, NULL);
400
401 if (err)
402 return err;
403
404 udelay(1000);
405 } while (!(cmd.response[0] & OCR_BUSY) && timeout--);
406
407 if (timeout <= 0)
408 return UNUSABLE_ERR;
409
Thomas Choud52ebf12010-12-24 13:12:21 +0000410 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
411 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
412 cmd.resp_type = MMC_RSP_R3;
413 cmd.cmdarg = 0;
414 cmd.flags = 0;
415
416 err = mmc_send_cmd(mmc, &cmd, NULL);
417
418 if (err)
419 return err;
420 }
421
Andy Fleming272cc702008-10-30 16:41:01 -0500422 mmc->version = MMC_VERSION_UNKNOWN;
Rabin Vincent998be3d2009-04-05 13:30:56 +0530423 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500424
425 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
426 mmc->rca = 0;
427
428 return 0;
429}
430
431
432int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd)
433{
434 struct mmc_cmd cmd;
435 struct mmc_data data;
436 int err;
437
438 /* Get the Card Status Register */
439 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
440 cmd.resp_type = MMC_RSP_R1;
441 cmd.cmdarg = 0;
442 cmd.flags = 0;
443
444 data.dest = ext_csd;
445 data.blocks = 1;
446 data.blocksize = 512;
447 data.flags = MMC_DATA_READ;
448
449 err = mmc_send_cmd(mmc, &cmd, &data);
450
451 return err;
452}
453
454
455int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
456{
457 struct mmc_cmd cmd;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000458 int timeout = 1000;
459 int ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500460
461 cmd.cmdidx = MMC_CMD_SWITCH;
462 cmd.resp_type = MMC_RSP_R1b;
463 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000464 (index << 16) |
465 (value << 8);
Andy Fleming272cc702008-10-30 16:41:01 -0500466 cmd.flags = 0;
467
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000468 ret = mmc_send_cmd(mmc, &cmd, NULL);
469
470 /* Waiting for the ready status */
471 mmc_send_status(mmc, timeout);
472
473 return ret;
474
Andy Fleming272cc702008-10-30 16:41:01 -0500475}
476
477int mmc_change_freq(struct mmc *mmc)
478{
479 char ext_csd[512];
480 char cardtype;
481 int err;
482
483 mmc->card_caps = 0;
484
Thomas Choud52ebf12010-12-24 13:12:21 +0000485 if (mmc_host_is_spi(mmc))
486 return 0;
487
Andy Fleming272cc702008-10-30 16:41:01 -0500488 /* Only version 4 supports high-speed */
489 if (mmc->version < MMC_VERSION_4)
490 return 0;
491
492 mmc->card_caps |= MMC_MODE_4BIT;
493
494 err = mmc_send_ext_csd(mmc, ext_csd);
495
496 if (err)
497 return err;
498
499 if (ext_csd[212] || ext_csd[213] || ext_csd[214] || ext_csd[215])
500 mmc->high_capacity = 1;
501
502 cardtype = ext_csd[196] & 0xf;
503
504 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
505
506 if (err)
507 return err;
508
509 /* Now check to see that it worked */
510 err = mmc_send_ext_csd(mmc, ext_csd);
511
512 if (err)
513 return err;
514
515 /* No high-speed support */
516 if (!ext_csd[185])
517 return 0;
518
519 /* High Speed is set, there are two types: 52MHz and 26MHz */
520 if (cardtype & MMC_HS_52MHZ)
521 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
522 else
523 mmc->card_caps |= MMC_MODE_HS;
524
525 return 0;
526}
527
528int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
529{
530 struct mmc_cmd cmd;
531 struct mmc_data data;
532
533 /* Switch the frequency */
534 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
535 cmd.resp_type = MMC_RSP_R1;
536 cmd.cmdarg = (mode << 31) | 0xffffff;
537 cmd.cmdarg &= ~(0xf << (group * 4));
538 cmd.cmdarg |= value << (group * 4);
539 cmd.flags = 0;
540
541 data.dest = (char *)resp;
542 data.blocksize = 64;
543 data.blocks = 1;
544 data.flags = MMC_DATA_READ;
545
546 return mmc_send_cmd(mmc, &cmd, &data);
547}
548
549
550int sd_change_freq(struct mmc *mmc)
551{
552 int err;
553 struct mmc_cmd cmd;
554 uint scr[2];
555 uint switch_status[16];
556 struct mmc_data data;
557 int timeout;
558
559 mmc->card_caps = 0;
560
Thomas Choud52ebf12010-12-24 13:12:21 +0000561 if (mmc_host_is_spi(mmc))
562 return 0;
563
Andy Fleming272cc702008-10-30 16:41:01 -0500564 /* Read the SCR to find out if this card supports higher speeds */
565 cmd.cmdidx = MMC_CMD_APP_CMD;
566 cmd.resp_type = MMC_RSP_R1;
567 cmd.cmdarg = mmc->rca << 16;
568 cmd.flags = 0;
569
570 err = mmc_send_cmd(mmc, &cmd, NULL);
571
572 if (err)
573 return err;
574
575 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
576 cmd.resp_type = MMC_RSP_R1;
577 cmd.cmdarg = 0;
578 cmd.flags = 0;
579
580 timeout = 3;
581
582retry_scr:
583 data.dest = (char *)&scr;
584 data.blocksize = 8;
585 data.blocks = 1;
586 data.flags = MMC_DATA_READ;
587
588 err = mmc_send_cmd(mmc, &cmd, &data);
589
590 if (err) {
591 if (timeout--)
592 goto retry_scr;
593
594 return err;
595 }
596
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300597 mmc->scr[0] = __be32_to_cpu(scr[0]);
598 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Fleming272cc702008-10-30 16:41:01 -0500599
600 switch ((mmc->scr[0] >> 24) & 0xf) {
601 case 0:
602 mmc->version = SD_VERSION_1_0;
603 break;
604 case 1:
605 mmc->version = SD_VERSION_1_10;
606 break;
607 case 2:
608 mmc->version = SD_VERSION_2;
609 break;
610 default:
611 mmc->version = SD_VERSION_1_0;
612 break;
613 }
614
Alagu Sankarb44c7082010-05-12 15:08:24 +0530615 if (mmc->scr[0] & SD_DATA_4BIT)
616 mmc->card_caps |= MMC_MODE_4BIT;
617
Andy Fleming272cc702008-10-30 16:41:01 -0500618 /* Version 1.0 doesn't support switching */
619 if (mmc->version == SD_VERSION_1_0)
620 return 0;
621
622 timeout = 4;
623 while (timeout--) {
624 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
625 (u8 *)&switch_status);
626
627 if (err)
628 return err;
629
630 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300631 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Fleming272cc702008-10-30 16:41:01 -0500632 break;
633 }
634
Andy Fleming272cc702008-10-30 16:41:01 -0500635 /* If high-speed isn't supported, we return */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300636 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
Andy Fleming272cc702008-10-30 16:41:01 -0500637 return 0;
638
639 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)&switch_status);
640
641 if (err)
642 return err;
643
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300644 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
Andy Fleming272cc702008-10-30 16:41:01 -0500645 mmc->card_caps |= MMC_MODE_HS;
646
647 return 0;
648}
649
650/* frequency bases */
651/* divided by 10 to be nice to platforms without floating point */
Mike Frysinger5f837c22010-10-20 01:15:53 +0000652static const int fbase[] = {
Andy Fleming272cc702008-10-30 16:41:01 -0500653 10000,
654 100000,
655 1000000,
656 10000000,
657};
658
659/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
660 * to platforms without floating point.
661 */
Mike Frysinger5f837c22010-10-20 01:15:53 +0000662static const int multipliers[] = {
Andy Fleming272cc702008-10-30 16:41:01 -0500663 0, /* reserved */
664 10,
665 12,
666 13,
667 15,
668 20,
669 25,
670 30,
671 35,
672 40,
673 45,
674 50,
675 55,
676 60,
677 70,
678 80,
679};
680
681void mmc_set_ios(struct mmc *mmc)
682{
683 mmc->set_ios(mmc);
684}
685
686void mmc_set_clock(struct mmc *mmc, uint clock)
687{
688 if (clock > mmc->f_max)
689 clock = mmc->f_max;
690
691 if (clock < mmc->f_min)
692 clock = mmc->f_min;
693
694 mmc->clock = clock;
695
696 mmc_set_ios(mmc);
697}
698
699void mmc_set_bus_width(struct mmc *mmc, uint width)
700{
701 mmc->bus_width = width;
702
703 mmc_set_ios(mmc);
704}
705
706int mmc_startup(struct mmc *mmc)
707{
708 int err;
709 uint mult, freq;
710 u64 cmult, csize;
711 struct mmc_cmd cmd;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +0530712 char ext_csd[512];
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000713 int timeout = 1000;
Andy Fleming272cc702008-10-30 16:41:01 -0500714
Thomas Choud52ebf12010-12-24 13:12:21 +0000715#ifdef CONFIG_MMC_SPI_CRC_ON
716 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
717 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
718 cmd.resp_type = MMC_RSP_R1;
719 cmd.cmdarg = 1;
720 cmd.flags = 0;
721 err = mmc_send_cmd(mmc, &cmd, NULL);
722
723 if (err)
724 return err;
725 }
726#endif
727
Andy Fleming272cc702008-10-30 16:41:01 -0500728 /* Put the Card in Identify Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +0000729 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
730 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
Andy Fleming272cc702008-10-30 16:41:01 -0500731 cmd.resp_type = MMC_RSP_R2;
732 cmd.cmdarg = 0;
733 cmd.flags = 0;
734
735 err = mmc_send_cmd(mmc, &cmd, NULL);
736
737 if (err)
738 return err;
739
740 memcpy(mmc->cid, cmd.response, 16);
741
742 /*
743 * For MMC cards, set the Relative Address.
744 * For SD cards, get the Relatvie Address.
745 * This also puts the cards into Standby State
746 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000747 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
748 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
749 cmd.cmdarg = mmc->rca << 16;
750 cmd.resp_type = MMC_RSP_R6;
751 cmd.flags = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500752
Thomas Choud52ebf12010-12-24 13:12:21 +0000753 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -0500754
Thomas Choud52ebf12010-12-24 13:12:21 +0000755 if (err)
756 return err;
Andy Fleming272cc702008-10-30 16:41:01 -0500757
Thomas Choud52ebf12010-12-24 13:12:21 +0000758 if (IS_SD(mmc))
759 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
760 }
Andy Fleming272cc702008-10-30 16:41:01 -0500761
762 /* Get the Card-Specific Data */
763 cmd.cmdidx = MMC_CMD_SEND_CSD;
764 cmd.resp_type = MMC_RSP_R2;
765 cmd.cmdarg = mmc->rca << 16;
766 cmd.flags = 0;
767
768 err = mmc_send_cmd(mmc, &cmd, NULL);
769
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000770 /* Waiting for the ready status */
771 mmc_send_status(mmc, timeout);
772
Andy Fleming272cc702008-10-30 16:41:01 -0500773 if (err)
774 return err;
775
Rabin Vincent998be3d2009-04-05 13:30:56 +0530776 mmc->csd[0] = cmd.response[0];
777 mmc->csd[1] = cmd.response[1];
778 mmc->csd[2] = cmd.response[2];
779 mmc->csd[3] = cmd.response[3];
Andy Fleming272cc702008-10-30 16:41:01 -0500780
781 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincent0b453ff2009-04-05 13:30:55 +0530782 int version = (cmd.response[0] >> 26) & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -0500783
784 switch (version) {
785 case 0:
786 mmc->version = MMC_VERSION_1_2;
787 break;
788 case 1:
789 mmc->version = MMC_VERSION_1_4;
790 break;
791 case 2:
792 mmc->version = MMC_VERSION_2_2;
793 break;
794 case 3:
795 mmc->version = MMC_VERSION_3;
796 break;
797 case 4:
798 mmc->version = MMC_VERSION_4;
799 break;
800 default:
801 mmc->version = MMC_VERSION_1_2;
802 break;
803 }
804 }
805
806 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincent0b453ff2009-04-05 13:30:55 +0530807 freq = fbase[(cmd.response[0] & 0x7)];
808 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Fleming272cc702008-10-30 16:41:01 -0500809
810 mmc->tran_speed = freq * mult;
811
Rabin Vincent998be3d2009-04-05 13:30:56 +0530812 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -0500813
814 if (IS_SD(mmc))
815 mmc->write_bl_len = mmc->read_bl_len;
816 else
Rabin Vincent998be3d2009-04-05 13:30:56 +0530817 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -0500818
819 if (mmc->high_capacity) {
820 csize = (mmc->csd[1] & 0x3f) << 16
821 | (mmc->csd[2] & 0xffff0000) >> 16;
822 cmult = 8;
823 } else {
824 csize = (mmc->csd[1] & 0x3ff) << 2
825 | (mmc->csd[2] & 0xc0000000) >> 30;
826 cmult = (mmc->csd[2] & 0x00038000) >> 15;
827 }
828
829 mmc->capacity = (csize + 1) << (cmult + 2);
830 mmc->capacity *= mmc->read_bl_len;
831
832 if (mmc->read_bl_len > 512)
833 mmc->read_bl_len = 512;
834
835 if (mmc->write_bl_len > 512)
836 mmc->write_bl_len = 512;
837
838 /* Select the card, and put it into Transfer Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +0000839 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
840 cmd.cmdidx = MMC_CMD_SELECT_CARD;
841 cmd.resp_type = MMC_RSP_R1b;
842 cmd.cmdarg = mmc->rca << 16;
843 cmd.flags = 0;
844 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -0500845
Thomas Choud52ebf12010-12-24 13:12:21 +0000846 if (err)
847 return err;
848 }
Andy Fleming272cc702008-10-30 16:41:01 -0500849
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +0530850 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
851 /* check ext_csd version and capacity */
852 err = mmc_send_ext_csd(mmc, ext_csd);
853 if (!err & (ext_csd[192] >= 2)) {
854 mmc->capacity = ext_csd[212] << 0 | ext_csd[213] << 8 |
855 ext_csd[214] << 16 | ext_csd[215] << 24;
856 mmc->capacity *= 512;
857 }
858 }
859
Andy Fleming272cc702008-10-30 16:41:01 -0500860 if (IS_SD(mmc))
861 err = sd_change_freq(mmc);
862 else
863 err = mmc_change_freq(mmc);
864
865 if (err)
866 return err;
867
868 /* Restrict card's capabilities by what the host can do */
869 mmc->card_caps &= mmc->host_caps;
870
871 if (IS_SD(mmc)) {
872 if (mmc->card_caps & MMC_MODE_4BIT) {
873 cmd.cmdidx = MMC_CMD_APP_CMD;
874 cmd.resp_type = MMC_RSP_R1;
875 cmd.cmdarg = mmc->rca << 16;
876 cmd.flags = 0;
877
878 err = mmc_send_cmd(mmc, &cmd, NULL);
879 if (err)
880 return err;
881
882 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
883 cmd.resp_type = MMC_RSP_R1;
884 cmd.cmdarg = 2;
885 cmd.flags = 0;
886 err = mmc_send_cmd(mmc, &cmd, NULL);
887 if (err)
888 return err;
889
890 mmc_set_bus_width(mmc, 4);
891 }
892
893 if (mmc->card_caps & MMC_MODE_HS)
894 mmc_set_clock(mmc, 50000000);
895 else
896 mmc_set_clock(mmc, 25000000);
897 } else {
898 if (mmc->card_caps & MMC_MODE_4BIT) {
899 /* Set the card to use 4 bit*/
900 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
901 EXT_CSD_BUS_WIDTH,
902 EXT_CSD_BUS_WIDTH_4);
903
904 if (err)
905 return err;
906
907 mmc_set_bus_width(mmc, 4);
908 } else if (mmc->card_caps & MMC_MODE_8BIT) {
909 /* Set the card to use 8 bit*/
910 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
911 EXT_CSD_BUS_WIDTH,
912 EXT_CSD_BUS_WIDTH_8);
913
914 if (err)
915 return err;
916
917 mmc_set_bus_width(mmc, 8);
918 }
919
920 if (mmc->card_caps & MMC_MODE_HS) {
921 if (mmc->card_caps & MMC_MODE_HS_52MHz)
922 mmc_set_clock(mmc, 52000000);
923 else
924 mmc_set_clock(mmc, 26000000);
925 } else
926 mmc_set_clock(mmc, 20000000);
927 }
928
929 /* fill in device description */
930 mmc->block_dev.lun = 0;
931 mmc->block_dev.type = 0;
932 mmc->block_dev.blksz = mmc->read_bl_len;
Rabin Vincent9b1f9422009-04-05 13:30:54 +0530933 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
Rabin Vincent0b453ff2009-04-05 13:30:55 +0530934 sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8,
935 (mmc->cid[2] << 8) | (mmc->cid[3] >> 24));
936 sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff,
937 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
938 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
939 sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28,
940 (mmc->cid[2] >> 24) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -0500941 init_part(&mmc->block_dev);
942
943 return 0;
944}
945
946int mmc_send_if_cond(struct mmc *mmc)
947{
948 struct mmc_cmd cmd;
949 int err;
950
951 cmd.cmdidx = SD_CMD_SEND_IF_COND;
952 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
953 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
954 cmd.resp_type = MMC_RSP_R7;
955 cmd.flags = 0;
956
957 err = mmc_send_cmd(mmc, &cmd, NULL);
958
959 if (err)
960 return err;
961
Rabin Vincent998be3d2009-04-05 13:30:56 +0530962 if ((cmd.response[0] & 0xff) != 0xaa)
Andy Fleming272cc702008-10-30 16:41:01 -0500963 return UNUSABLE_ERR;
964 else
965 mmc->version = SD_VERSION_2;
966
967 return 0;
968}
969
970int mmc_register(struct mmc *mmc)
971{
972 /* Setup the universal parts of the block interface just once */
973 mmc->block_dev.if_type = IF_TYPE_MMC;
974 mmc->block_dev.dev = cur_dev_num++;
975 mmc->block_dev.removable = 1;
976 mmc->block_dev.block_read = mmc_bread;
977 mmc->block_dev.block_write = mmc_bwrite;
978
979 INIT_LIST_HEAD (&mmc->link);
980
981 list_add_tail (&mmc->link, &mmc_devices);
982
983 return 0;
984}
985
986block_dev_desc_t *mmc_get_dev(int dev)
987{
988 struct mmc *mmc = find_mmc_device(dev);
989
Rabin Vincente85649c2009-04-05 13:30:53 +0530990 return mmc ? &mmc->block_dev : NULL;
Andy Fleming272cc702008-10-30 16:41:01 -0500991}
992
993int mmc_init(struct mmc *mmc)
994{
995 int err;
996
997 err = mmc->init(mmc);
998
999 if (err)
1000 return err;
1001
Ilya Yanokb86b85e2009-06-29 17:53:16 +04001002 mmc_set_bus_width(mmc, 1);
1003 mmc_set_clock(mmc, 1);
1004
Andy Fleming272cc702008-10-30 16:41:01 -05001005 /* Reset the Card */
1006 err = mmc_go_idle(mmc);
1007
1008 if (err)
1009 return err;
1010
1011 /* Test for SD version 2 */
1012 err = mmc_send_if_cond(mmc);
1013
Andy Fleming272cc702008-10-30 16:41:01 -05001014 /* Now try to get the SD card's operating condition */
1015 err = sd_send_op_cond(mmc);
1016
1017 /* If the command timed out, we check for an MMC card */
1018 if (err == TIMEOUT) {
1019 err = mmc_send_op_cond(mmc);
1020
1021 if (err) {
1022 printf("Card did not respond to voltage select!\n");
1023 return UNUSABLE_ERR;
1024 }
1025 }
1026
1027 return mmc_startup(mmc);
1028}
1029
1030/*
1031 * CPU and board-specific MMC initializations. Aliased function
1032 * signals caller to move on
1033 */
1034static int __def_mmc_init(bd_t *bis)
1035{
1036 return -1;
1037}
1038
Peter Tyserf9a109b2009-04-20 11:08:46 -05001039int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1040int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
Andy Fleming272cc702008-10-30 16:41:01 -05001041
1042void print_mmc_devices(char separator)
1043{
1044 struct mmc *m;
1045 struct list_head *entry;
1046
1047 list_for_each(entry, &mmc_devices) {
1048 m = list_entry(entry, struct mmc, link);
1049
1050 printf("%s: %d", m->name, m->block_dev.dev);
1051
1052 if (entry->next != &mmc_devices)
1053 printf("%c ", separator);
1054 }
1055
1056 printf("\n");
1057}
1058
1059int mmc_initialize(bd_t *bis)
1060{
1061 INIT_LIST_HEAD (&mmc_devices);
1062 cur_dev_num = 0;
1063
1064 if (board_mmc_init(bis) < 0)
1065 cpu_mmc_init(bis);
1066
1067 print_mmc_devices(',');
1068
1069 return 0;
1070}