blob: e8bc4b869a2edbdff1496f72dd5c5762c3dd21f2 [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>
33#include <mmc.h>
Rabin Vincent9b1f9422009-04-05 13:30:54 +053034#include <div64.h>
Andy Fleming272cc702008-10-30 16:41:01 -050035
Matt Waddelce0fbcd2011-02-24 16:35:23 +000036/* Set block count limit because of 16 bit register limit on some hardware*/
37#ifndef CONFIG_SYS_MMC_MAX_BLK_COUNT
38#define CONFIG_SYS_MMC_MAX_BLK_COUNT 65535
39#endif
40
Andy Fleming272cc702008-10-30 16:41:01 -050041static struct list_head mmc_devices;
42static int cur_dev_num = -1;
43
Stefano Babic11fdade2010-02-05 15:04:43 +010044int __board_mmc_getcd(u8 *cd, struct mmc *mmc) {
45 return -1;
46}
47
48int board_mmc_getcd(u8 *cd, struct mmc *mmc)__attribute__((weak,
49 alias("__board_mmc_getcd")));
50
Andy Fleming272cc702008-10-30 16:41:01 -050051int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
52{
53 return mmc->send_cmd(mmc, cmd, data);
54}
55
56int mmc_set_blocklen(struct mmc *mmc, int len)
57{
58 struct mmc_cmd cmd;
59
60 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
61 cmd.resp_type = MMC_RSP_R1;
62 cmd.cmdarg = len;
63 cmd.flags = 0;
64
65 return mmc_send_cmd(mmc, &cmd, NULL);
66}
67
68struct mmc *find_mmc_device(int dev_num)
69{
70 struct mmc *m;
71 struct list_head *entry;
72
73 list_for_each(entry, &mmc_devices) {
74 m = list_entry(entry, struct mmc, link);
75
76 if (m->block_dev.dev == dev_num)
77 return m;
78 }
79
80 printf("MMC Device %d not found\n", dev_num);
81
82 return NULL;
83}
84
85static ulong
Lei Wen01581262010-10-14 13:38:11 +080086mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
Andy Fleming272cc702008-10-30 16:41:01 -050087{
88 struct mmc_cmd cmd;
89 struct mmc_data data;
Andy Fleming272cc702008-10-30 16:41:01 -050090
Lei Wend2bf29e2010-09-13 22:07:27 +080091 if ((start + blkcnt) > mmc->block_dev.lba) {
Steve Sakomandef412b2010-10-28 09:00:26 -070092 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
Lei Wend2bf29e2010-09-13 22:07:27 +080093 start + blkcnt, mmc->block_dev.lba);
94 return 0;
95 }
Andy Fleming272cc702008-10-30 16:41:01 -050096
97 if (blkcnt > 1)
98 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
99 else
100 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
101
102 if (mmc->high_capacity)
103 cmd.cmdarg = start;
104 else
Steve Sakomandef412b2010-10-28 09:00:26 -0700105 cmd.cmdarg = start * mmc->write_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500106
107 cmd.resp_type = MMC_RSP_R1;
108 cmd.flags = 0;
109
110 data.src = src;
111 data.blocks = blkcnt;
Steve Sakomandef412b2010-10-28 09:00:26 -0700112 data.blocksize = mmc->write_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500113 data.flags = MMC_DATA_WRITE;
114
Steve Sakomandef412b2010-10-28 09:00:26 -0700115 if (mmc_send_cmd(mmc, &cmd, &data)) {
116 printf("mmc write failed\n");
117 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500118 }
119
120 if (blkcnt > 1) {
121 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
122 cmd.cmdarg = 0;
123 cmd.resp_type = MMC_RSP_R1b;
124 cmd.flags = 0;
Steve Sakomandef412b2010-10-28 09:00:26 -0700125 if (mmc_send_cmd(mmc, &cmd, NULL)) {
126 printf("mmc fail to send stop cmd\n");
127 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800128 }
Andy Fleming272cc702008-10-30 16:41:01 -0500129 }
130
131 return blkcnt;
132}
133
Lei Wen01581262010-10-14 13:38:11 +0800134static ulong
135mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
136{
Lei Wen01581262010-10-14 13:38:11 +0800137 lbaint_t cur, blocks_todo = blkcnt;
138
Steve Sakomandef412b2010-10-28 09:00:26 -0700139 struct mmc *mmc = find_mmc_device(dev_num);
Lei Wen01581262010-10-14 13:38:11 +0800140 if (!mmc)
Steve Sakomandef412b2010-10-28 09:00:26 -0700141 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800142
Steve Sakomandef412b2010-10-28 09:00:26 -0700143 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
144 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800145
146 do {
Matt Waddelce0fbcd2011-02-24 16:35:23 +0000147 cur = (blocks_todo > CONFIG_SYS_MMC_MAX_BLK_COUNT) ?
148 CONFIG_SYS_MMC_MAX_BLK_COUNT : blocks_todo;
Lei Wen01581262010-10-14 13:38:11 +0800149 if(mmc_write_blocks(mmc, start, cur, src) != cur)
Steve Sakomandef412b2010-10-28 09:00:26 -0700150 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800151 blocks_todo -= cur;
152 start += cur;
153 src += cur * mmc->write_bl_len;
154 } while (blocks_todo > 0);
155
156 return blkcnt;
157}
158
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700159int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt)
Andy Fleming272cc702008-10-30 16:41:01 -0500160{
161 struct mmc_cmd cmd;
162 struct mmc_data data;
163
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700164 if (blkcnt > 1)
165 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
166 else
167 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Fleming272cc702008-10-30 16:41:01 -0500168
169 if (mmc->high_capacity)
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700170 cmd.cmdarg = start;
Andy Fleming272cc702008-10-30 16:41:01 -0500171 else
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700172 cmd.cmdarg = start * mmc->read_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500173
174 cmd.resp_type = MMC_RSP_R1;
175 cmd.flags = 0;
176
177 data.dest = dst;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700178 data.blocks = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500179 data.blocksize = mmc->read_bl_len;
180 data.flags = MMC_DATA_READ;
181
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700182 if (mmc_send_cmd(mmc, &cmd, &data))
183 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500184
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700185 if (blkcnt > 1) {
186 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
187 cmd.cmdarg = 0;
188 cmd.resp_type = MMC_RSP_R1b;
189 cmd.flags = 0;
190 if (mmc_send_cmd(mmc, &cmd, NULL)) {
191 printf("mmc fail to send stop cmd\n");
192 return 0;
193 }
Andy Fleming272cc702008-10-30 16:41:01 -0500194 }
195
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700196 return blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500197}
198
199static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
200{
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700201 lbaint_t cur, blocks_todo = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500202
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700203 if (blkcnt == 0)
204 return 0;
205
206 struct mmc *mmc = find_mmc_device(dev_num);
Andy Fleming272cc702008-10-30 16:41:01 -0500207 if (!mmc)
208 return 0;
209
Lei Wend2bf29e2010-09-13 22:07:27 +0800210 if ((start + blkcnt) > mmc->block_dev.lba) {
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700211 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
Lei Wend2bf29e2010-09-13 22:07:27 +0800212 start + blkcnt, mmc->block_dev.lba);
213 return 0;
214 }
Andy Fleming272cc702008-10-30 16:41:01 -0500215
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700216 if (mmc_set_blocklen(mmc, mmc->read_bl_len))
Andy Fleming272cc702008-10-30 16:41:01 -0500217 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500218
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700219 do {
Matt Waddelce0fbcd2011-02-24 16:35:23 +0000220 cur = (blocks_todo > CONFIG_SYS_MMC_MAX_BLK_COUNT) ?
221 CONFIG_SYS_MMC_MAX_BLK_COUNT : blocks_todo;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700222 if(mmc_read_blocks(mmc, dst, start, cur) != cur)
223 return 0;
224 blocks_todo -= cur;
225 start += cur;
226 dst += cur * mmc->read_bl_len;
227 } while (blocks_todo > 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500228
229 return blkcnt;
230}
231
232int mmc_go_idle(struct mmc* mmc)
233{
234 struct mmc_cmd cmd;
235 int err;
236
237 udelay(1000);
238
239 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
240 cmd.cmdarg = 0;
241 cmd.resp_type = MMC_RSP_NONE;
242 cmd.flags = 0;
243
244 err = mmc_send_cmd(mmc, &cmd, NULL);
245
246 if (err)
247 return err;
248
249 udelay(2000);
250
251 return 0;
252}
253
254int
255sd_send_op_cond(struct mmc *mmc)
256{
257 int timeout = 1000;
258 int err;
259 struct mmc_cmd cmd;
260
261 do {
262 cmd.cmdidx = MMC_CMD_APP_CMD;
263 cmd.resp_type = MMC_RSP_R1;
264 cmd.cmdarg = 0;
265 cmd.flags = 0;
266
267 err = mmc_send_cmd(mmc, &cmd, NULL);
268
269 if (err)
270 return err;
271
272 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
273 cmd.resp_type = MMC_RSP_R3;
Stefano Babic250de122010-01-20 18:20:39 +0100274
275 /*
276 * Most cards do not answer if some reserved bits
277 * in the ocr are set. However, Some controller
278 * can set bit 7 (reserved for low voltages), but
279 * how to manage low voltages SD card is not yet
280 * specified.
281 */
282 cmd.cmdarg = mmc->voltages & 0xff8000;
Andy Fleming272cc702008-10-30 16:41:01 -0500283
284 if (mmc->version == SD_VERSION_2)
285 cmd.cmdarg |= OCR_HCS;
286
287 err = mmc_send_cmd(mmc, &cmd, NULL);
288
289 if (err)
290 return err;
291
292 udelay(1000);
293 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
294
295 if (timeout <= 0)
296 return UNUSABLE_ERR;
297
298 if (mmc->version != SD_VERSION_2)
299 mmc->version = SD_VERSION_1_0;
300
Rabin Vincent998be3d2009-04-05 13:30:56 +0530301 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500302
303 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
304 mmc->rca = 0;
305
306 return 0;
307}
308
309int mmc_send_op_cond(struct mmc *mmc)
310{
311 int timeout = 1000;
312 struct mmc_cmd cmd;
313 int err;
314
315 /* Some cards seem to need this */
316 mmc_go_idle(mmc);
317
318 do {
319 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
320 cmd.resp_type = MMC_RSP_R3;
321 cmd.cmdarg = OCR_HCS | mmc->voltages;
322 cmd.flags = 0;
323
324 err = mmc_send_cmd(mmc, &cmd, NULL);
325
326 if (err)
327 return err;
328
329 udelay(1000);
330 } while (!(cmd.response[0] & OCR_BUSY) && timeout--);
331
332 if (timeout <= 0)
333 return UNUSABLE_ERR;
334
335 mmc->version = MMC_VERSION_UNKNOWN;
Rabin Vincent998be3d2009-04-05 13:30:56 +0530336 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500337
338 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
339 mmc->rca = 0;
340
341 return 0;
342}
343
344
345int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd)
346{
347 struct mmc_cmd cmd;
348 struct mmc_data data;
349 int err;
350
351 /* Get the Card Status Register */
352 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
353 cmd.resp_type = MMC_RSP_R1;
354 cmd.cmdarg = 0;
355 cmd.flags = 0;
356
357 data.dest = ext_csd;
358 data.blocks = 1;
359 data.blocksize = 512;
360 data.flags = MMC_DATA_READ;
361
362 err = mmc_send_cmd(mmc, &cmd, &data);
363
364 return err;
365}
366
367
368int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
369{
370 struct mmc_cmd cmd;
371
372 cmd.cmdidx = MMC_CMD_SWITCH;
373 cmd.resp_type = MMC_RSP_R1b;
374 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
375 (index << 16) |
376 (value << 8);
377 cmd.flags = 0;
378
379 return mmc_send_cmd(mmc, &cmd, NULL);
380}
381
382int mmc_change_freq(struct mmc *mmc)
383{
384 char ext_csd[512];
385 char cardtype;
386 int err;
387
388 mmc->card_caps = 0;
389
390 /* Only version 4 supports high-speed */
391 if (mmc->version < MMC_VERSION_4)
392 return 0;
393
394 mmc->card_caps |= MMC_MODE_4BIT;
395
396 err = mmc_send_ext_csd(mmc, ext_csd);
397
398 if (err)
399 return err;
400
401 if (ext_csd[212] || ext_csd[213] || ext_csd[214] || ext_csd[215])
402 mmc->high_capacity = 1;
403
404 cardtype = ext_csd[196] & 0xf;
405
406 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
407
408 if (err)
409 return err;
410
411 /* Now check to see that it worked */
412 err = mmc_send_ext_csd(mmc, ext_csd);
413
414 if (err)
415 return err;
416
417 /* No high-speed support */
418 if (!ext_csd[185])
419 return 0;
420
421 /* High Speed is set, there are two types: 52MHz and 26MHz */
422 if (cardtype & MMC_HS_52MHZ)
423 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
424 else
425 mmc->card_caps |= MMC_MODE_HS;
426
427 return 0;
428}
429
430int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
431{
432 struct mmc_cmd cmd;
433 struct mmc_data data;
434
435 /* Switch the frequency */
436 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
437 cmd.resp_type = MMC_RSP_R1;
438 cmd.cmdarg = (mode << 31) | 0xffffff;
439 cmd.cmdarg &= ~(0xf << (group * 4));
440 cmd.cmdarg |= value << (group * 4);
441 cmd.flags = 0;
442
443 data.dest = (char *)resp;
444 data.blocksize = 64;
445 data.blocks = 1;
446 data.flags = MMC_DATA_READ;
447
448 return mmc_send_cmd(mmc, &cmd, &data);
449}
450
451
452int sd_change_freq(struct mmc *mmc)
453{
454 int err;
455 struct mmc_cmd cmd;
456 uint scr[2];
457 uint switch_status[16];
458 struct mmc_data data;
459 int timeout;
460
461 mmc->card_caps = 0;
462
463 /* Read the SCR to find out if this card supports higher speeds */
464 cmd.cmdidx = MMC_CMD_APP_CMD;
465 cmd.resp_type = MMC_RSP_R1;
466 cmd.cmdarg = mmc->rca << 16;
467 cmd.flags = 0;
468
469 err = mmc_send_cmd(mmc, &cmd, NULL);
470
471 if (err)
472 return err;
473
474 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
475 cmd.resp_type = MMC_RSP_R1;
476 cmd.cmdarg = 0;
477 cmd.flags = 0;
478
479 timeout = 3;
480
481retry_scr:
482 data.dest = (char *)&scr;
483 data.blocksize = 8;
484 data.blocks = 1;
485 data.flags = MMC_DATA_READ;
486
487 err = mmc_send_cmd(mmc, &cmd, &data);
488
489 if (err) {
490 if (timeout--)
491 goto retry_scr;
492
493 return err;
494 }
495
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300496 mmc->scr[0] = __be32_to_cpu(scr[0]);
497 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Fleming272cc702008-10-30 16:41:01 -0500498
499 switch ((mmc->scr[0] >> 24) & 0xf) {
500 case 0:
501 mmc->version = SD_VERSION_1_0;
502 break;
503 case 1:
504 mmc->version = SD_VERSION_1_10;
505 break;
506 case 2:
507 mmc->version = SD_VERSION_2;
508 break;
509 default:
510 mmc->version = SD_VERSION_1_0;
511 break;
512 }
513
Alagu Sankarb44c7082010-05-12 15:08:24 +0530514 if (mmc->scr[0] & SD_DATA_4BIT)
515 mmc->card_caps |= MMC_MODE_4BIT;
516
Andy Fleming272cc702008-10-30 16:41:01 -0500517 /* Version 1.0 doesn't support switching */
518 if (mmc->version == SD_VERSION_1_0)
519 return 0;
520
521 timeout = 4;
522 while (timeout--) {
523 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
524 (u8 *)&switch_status);
525
526 if (err)
527 return err;
528
529 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300530 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Fleming272cc702008-10-30 16:41:01 -0500531 break;
532 }
533
Andy Fleming272cc702008-10-30 16:41:01 -0500534 /* If high-speed isn't supported, we return */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300535 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
Andy Fleming272cc702008-10-30 16:41:01 -0500536 return 0;
537
538 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)&switch_status);
539
540 if (err)
541 return err;
542
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300543 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
Andy Fleming272cc702008-10-30 16:41:01 -0500544 mmc->card_caps |= MMC_MODE_HS;
545
546 return 0;
547}
548
549/* frequency bases */
550/* divided by 10 to be nice to platforms without floating point */
Mike Frysinger5f837c22010-10-20 01:15:53 +0000551static const int fbase[] = {
Andy Fleming272cc702008-10-30 16:41:01 -0500552 10000,
553 100000,
554 1000000,
555 10000000,
556};
557
558/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
559 * to platforms without floating point.
560 */
Mike Frysinger5f837c22010-10-20 01:15:53 +0000561static const int multipliers[] = {
Andy Fleming272cc702008-10-30 16:41:01 -0500562 0, /* reserved */
563 10,
564 12,
565 13,
566 15,
567 20,
568 25,
569 30,
570 35,
571 40,
572 45,
573 50,
574 55,
575 60,
576 70,
577 80,
578};
579
580void mmc_set_ios(struct mmc *mmc)
581{
582 mmc->set_ios(mmc);
583}
584
585void mmc_set_clock(struct mmc *mmc, uint clock)
586{
587 if (clock > mmc->f_max)
588 clock = mmc->f_max;
589
590 if (clock < mmc->f_min)
591 clock = mmc->f_min;
592
593 mmc->clock = clock;
594
595 mmc_set_ios(mmc);
596}
597
598void mmc_set_bus_width(struct mmc *mmc, uint width)
599{
600 mmc->bus_width = width;
601
602 mmc_set_ios(mmc);
603}
604
605int mmc_startup(struct mmc *mmc)
606{
607 int err;
608 uint mult, freq;
609 u64 cmult, csize;
610 struct mmc_cmd cmd;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +0530611 char ext_csd[512];
Andy Fleming272cc702008-10-30 16:41:01 -0500612
613 /* Put the Card in Identify Mode */
614 cmd.cmdidx = MMC_CMD_ALL_SEND_CID;
615 cmd.resp_type = MMC_RSP_R2;
616 cmd.cmdarg = 0;
617 cmd.flags = 0;
618
619 err = mmc_send_cmd(mmc, &cmd, NULL);
620
621 if (err)
622 return err;
623
624 memcpy(mmc->cid, cmd.response, 16);
625
626 /*
627 * For MMC cards, set the Relative Address.
628 * For SD cards, get the Relatvie Address.
629 * This also puts the cards into Standby State
630 */
631 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
632 cmd.cmdarg = mmc->rca << 16;
633 cmd.resp_type = MMC_RSP_R6;
634 cmd.flags = 0;
635
636 err = mmc_send_cmd(mmc, &cmd, NULL);
637
638 if (err)
639 return err;
640
641 if (IS_SD(mmc))
Rabin Vincent998be3d2009-04-05 13:30:56 +0530642 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
Andy Fleming272cc702008-10-30 16:41:01 -0500643
644 /* Get the Card-Specific Data */
645 cmd.cmdidx = MMC_CMD_SEND_CSD;
646 cmd.resp_type = MMC_RSP_R2;
647 cmd.cmdarg = mmc->rca << 16;
648 cmd.flags = 0;
649
650 err = mmc_send_cmd(mmc, &cmd, NULL);
651
652 if (err)
653 return err;
654
Rabin Vincent998be3d2009-04-05 13:30:56 +0530655 mmc->csd[0] = cmd.response[0];
656 mmc->csd[1] = cmd.response[1];
657 mmc->csd[2] = cmd.response[2];
658 mmc->csd[3] = cmd.response[3];
Andy Fleming272cc702008-10-30 16:41:01 -0500659
660 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincent0b453ff2009-04-05 13:30:55 +0530661 int version = (cmd.response[0] >> 26) & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -0500662
663 switch (version) {
664 case 0:
665 mmc->version = MMC_VERSION_1_2;
666 break;
667 case 1:
668 mmc->version = MMC_VERSION_1_4;
669 break;
670 case 2:
671 mmc->version = MMC_VERSION_2_2;
672 break;
673 case 3:
674 mmc->version = MMC_VERSION_3;
675 break;
676 case 4:
677 mmc->version = MMC_VERSION_4;
678 break;
679 default:
680 mmc->version = MMC_VERSION_1_2;
681 break;
682 }
683 }
684
685 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincent0b453ff2009-04-05 13:30:55 +0530686 freq = fbase[(cmd.response[0] & 0x7)];
687 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Fleming272cc702008-10-30 16:41:01 -0500688
689 mmc->tran_speed = freq * mult;
690
Rabin Vincent998be3d2009-04-05 13:30:56 +0530691 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -0500692
693 if (IS_SD(mmc))
694 mmc->write_bl_len = mmc->read_bl_len;
695 else
Rabin Vincent998be3d2009-04-05 13:30:56 +0530696 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -0500697
698 if (mmc->high_capacity) {
699 csize = (mmc->csd[1] & 0x3f) << 16
700 | (mmc->csd[2] & 0xffff0000) >> 16;
701 cmult = 8;
702 } else {
703 csize = (mmc->csd[1] & 0x3ff) << 2
704 | (mmc->csd[2] & 0xc0000000) >> 30;
705 cmult = (mmc->csd[2] & 0x00038000) >> 15;
706 }
707
708 mmc->capacity = (csize + 1) << (cmult + 2);
709 mmc->capacity *= mmc->read_bl_len;
710
711 if (mmc->read_bl_len > 512)
712 mmc->read_bl_len = 512;
713
714 if (mmc->write_bl_len > 512)
715 mmc->write_bl_len = 512;
716
717 /* Select the card, and put it into Transfer Mode */
718 cmd.cmdidx = MMC_CMD_SELECT_CARD;
719 cmd.resp_type = MMC_RSP_R1b;
720 cmd.cmdarg = mmc->rca << 16;
721 cmd.flags = 0;
722 err = mmc_send_cmd(mmc, &cmd, NULL);
723
724 if (err)
725 return err;
726
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +0530727 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
728 /* check ext_csd version and capacity */
729 err = mmc_send_ext_csd(mmc, ext_csd);
730 if (!err & (ext_csd[192] >= 2)) {
731 mmc->capacity = ext_csd[212] << 0 | ext_csd[213] << 8 |
732 ext_csd[214] << 16 | ext_csd[215] << 24;
733 mmc->capacity *= 512;
734 }
735 }
736
Andy Fleming272cc702008-10-30 16:41:01 -0500737 if (IS_SD(mmc))
738 err = sd_change_freq(mmc);
739 else
740 err = mmc_change_freq(mmc);
741
742 if (err)
743 return err;
744
745 /* Restrict card's capabilities by what the host can do */
746 mmc->card_caps &= mmc->host_caps;
747
748 if (IS_SD(mmc)) {
749 if (mmc->card_caps & MMC_MODE_4BIT) {
750 cmd.cmdidx = MMC_CMD_APP_CMD;
751 cmd.resp_type = MMC_RSP_R1;
752 cmd.cmdarg = mmc->rca << 16;
753 cmd.flags = 0;
754
755 err = mmc_send_cmd(mmc, &cmd, NULL);
756 if (err)
757 return err;
758
759 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
760 cmd.resp_type = MMC_RSP_R1;
761 cmd.cmdarg = 2;
762 cmd.flags = 0;
763 err = mmc_send_cmd(mmc, &cmd, NULL);
764 if (err)
765 return err;
766
767 mmc_set_bus_width(mmc, 4);
768 }
769
770 if (mmc->card_caps & MMC_MODE_HS)
771 mmc_set_clock(mmc, 50000000);
772 else
773 mmc_set_clock(mmc, 25000000);
774 } else {
775 if (mmc->card_caps & MMC_MODE_4BIT) {
776 /* Set the card to use 4 bit*/
777 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
778 EXT_CSD_BUS_WIDTH,
779 EXT_CSD_BUS_WIDTH_4);
780
781 if (err)
782 return err;
783
784 mmc_set_bus_width(mmc, 4);
785 } else if (mmc->card_caps & MMC_MODE_8BIT) {
786 /* Set the card to use 8 bit*/
787 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
788 EXT_CSD_BUS_WIDTH,
789 EXT_CSD_BUS_WIDTH_8);
790
791 if (err)
792 return err;
793
794 mmc_set_bus_width(mmc, 8);
795 }
796
797 if (mmc->card_caps & MMC_MODE_HS) {
798 if (mmc->card_caps & MMC_MODE_HS_52MHz)
799 mmc_set_clock(mmc, 52000000);
800 else
801 mmc_set_clock(mmc, 26000000);
802 } else
803 mmc_set_clock(mmc, 20000000);
804 }
805
806 /* fill in device description */
807 mmc->block_dev.lun = 0;
808 mmc->block_dev.type = 0;
809 mmc->block_dev.blksz = mmc->read_bl_len;
Rabin Vincent9b1f9422009-04-05 13:30:54 +0530810 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
Rabin Vincent0b453ff2009-04-05 13:30:55 +0530811 sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8,
812 (mmc->cid[2] << 8) | (mmc->cid[3] >> 24));
813 sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff,
814 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
815 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
816 sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28,
817 (mmc->cid[2] >> 24) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -0500818 init_part(&mmc->block_dev);
819
820 return 0;
821}
822
823int mmc_send_if_cond(struct mmc *mmc)
824{
825 struct mmc_cmd cmd;
826 int err;
827
828 cmd.cmdidx = SD_CMD_SEND_IF_COND;
829 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
830 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
831 cmd.resp_type = MMC_RSP_R7;
832 cmd.flags = 0;
833
834 err = mmc_send_cmd(mmc, &cmd, NULL);
835
836 if (err)
837 return err;
838
Rabin Vincent998be3d2009-04-05 13:30:56 +0530839 if ((cmd.response[0] & 0xff) != 0xaa)
Andy Fleming272cc702008-10-30 16:41:01 -0500840 return UNUSABLE_ERR;
841 else
842 mmc->version = SD_VERSION_2;
843
844 return 0;
845}
846
847int mmc_register(struct mmc *mmc)
848{
849 /* Setup the universal parts of the block interface just once */
850 mmc->block_dev.if_type = IF_TYPE_MMC;
851 mmc->block_dev.dev = cur_dev_num++;
852 mmc->block_dev.removable = 1;
853 mmc->block_dev.block_read = mmc_bread;
854 mmc->block_dev.block_write = mmc_bwrite;
855
856 INIT_LIST_HEAD (&mmc->link);
857
858 list_add_tail (&mmc->link, &mmc_devices);
859
860 return 0;
861}
862
863block_dev_desc_t *mmc_get_dev(int dev)
864{
865 struct mmc *mmc = find_mmc_device(dev);
866
Rabin Vincente85649c2009-04-05 13:30:53 +0530867 return mmc ? &mmc->block_dev : NULL;
Andy Fleming272cc702008-10-30 16:41:01 -0500868}
869
870int mmc_init(struct mmc *mmc)
871{
872 int err;
873
874 err = mmc->init(mmc);
875
876 if (err)
877 return err;
878
Ilya Yanokb86b85e2009-06-29 17:53:16 +0400879 mmc_set_bus_width(mmc, 1);
880 mmc_set_clock(mmc, 1);
881
Andy Fleming272cc702008-10-30 16:41:01 -0500882 /* Reset the Card */
883 err = mmc_go_idle(mmc);
884
885 if (err)
886 return err;
887
888 /* Test for SD version 2 */
889 err = mmc_send_if_cond(mmc);
890
Andy Fleming272cc702008-10-30 16:41:01 -0500891 /* Now try to get the SD card's operating condition */
892 err = sd_send_op_cond(mmc);
893
894 /* If the command timed out, we check for an MMC card */
895 if (err == TIMEOUT) {
896 err = mmc_send_op_cond(mmc);
897
898 if (err) {
899 printf("Card did not respond to voltage select!\n");
900 return UNUSABLE_ERR;
901 }
902 }
903
904 return mmc_startup(mmc);
905}
906
907/*
908 * CPU and board-specific MMC initializations. Aliased function
909 * signals caller to move on
910 */
911static int __def_mmc_init(bd_t *bis)
912{
913 return -1;
914}
915
Peter Tyserf9a109b2009-04-20 11:08:46 -0500916int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
917int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
Andy Fleming272cc702008-10-30 16:41:01 -0500918
919void print_mmc_devices(char separator)
920{
921 struct mmc *m;
922 struct list_head *entry;
923
924 list_for_each(entry, &mmc_devices) {
925 m = list_entry(entry, struct mmc, link);
926
927 printf("%s: %d", m->name, m->block_dev.dev);
928
929 if (entry->next != &mmc_devices)
930 printf("%c ", separator);
931 }
932
933 printf("\n");
934}
935
936int mmc_initialize(bd_t *bis)
937{
938 INIT_LIST_HEAD (&mmc_devices);
939 cur_dev_num = 0;
940
941 if (board_mmc_init(bis) < 0)
942 cpu_mmc_init(bis);
943
944 print_mmc_devices(',');
945
946 return 0;
947}