blob: e8d61c307db3dcaa9a5831b27eb7092624f80ebc [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
Thomas Choud52ebf12010-12-24 13:12:21 +0000120 /* SPI multiblock writes terminate using a special
121 * token, not a STOP_TRANSMISSION request.
122 */
123 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
Andy Fleming272cc702008-10-30 16:41:01 -0500124 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
125 cmd.cmdarg = 0;
126 cmd.resp_type = MMC_RSP_R1b;
127 cmd.flags = 0;
Steve Sakomandef412b2010-10-28 09:00:26 -0700128 if (mmc_send_cmd(mmc, &cmd, NULL)) {
129 printf("mmc fail to send stop cmd\n");
130 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800131 }
Andy Fleming272cc702008-10-30 16:41:01 -0500132 }
133
134 return blkcnt;
135}
136
Lei Wen01581262010-10-14 13:38:11 +0800137static ulong
138mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
139{
Lei Wen01581262010-10-14 13:38:11 +0800140 lbaint_t cur, blocks_todo = blkcnt;
141
Steve Sakomandef412b2010-10-28 09:00:26 -0700142 struct mmc *mmc = find_mmc_device(dev_num);
Lei Wen01581262010-10-14 13:38:11 +0800143 if (!mmc)
Steve Sakomandef412b2010-10-28 09:00:26 -0700144 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800145
Steve Sakomandef412b2010-10-28 09:00:26 -0700146 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
147 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800148
149 do {
Matt Waddelce0fbcd2011-02-24 16:35:23 +0000150 cur = (blocks_todo > CONFIG_SYS_MMC_MAX_BLK_COUNT) ?
151 CONFIG_SYS_MMC_MAX_BLK_COUNT : blocks_todo;
Lei Wen01581262010-10-14 13:38:11 +0800152 if(mmc_write_blocks(mmc, start, cur, src) != cur)
Steve Sakomandef412b2010-10-28 09:00:26 -0700153 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800154 blocks_todo -= cur;
155 start += cur;
156 src += cur * mmc->write_bl_len;
157 } while (blocks_todo > 0);
158
159 return blkcnt;
160}
161
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700162int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt)
Andy Fleming272cc702008-10-30 16:41:01 -0500163{
164 struct mmc_cmd cmd;
165 struct mmc_data data;
166
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700167 if (blkcnt > 1)
168 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
169 else
170 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Fleming272cc702008-10-30 16:41:01 -0500171
172 if (mmc->high_capacity)
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700173 cmd.cmdarg = start;
Andy Fleming272cc702008-10-30 16:41:01 -0500174 else
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700175 cmd.cmdarg = start * mmc->read_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500176
177 cmd.resp_type = MMC_RSP_R1;
178 cmd.flags = 0;
179
180 data.dest = dst;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700181 data.blocks = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500182 data.blocksize = mmc->read_bl_len;
183 data.flags = MMC_DATA_READ;
184
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700185 if (mmc_send_cmd(mmc, &cmd, &data))
186 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500187
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700188 if (blkcnt > 1) {
189 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
190 cmd.cmdarg = 0;
191 cmd.resp_type = MMC_RSP_R1b;
192 cmd.flags = 0;
193 if (mmc_send_cmd(mmc, &cmd, NULL)) {
194 printf("mmc fail to send stop cmd\n");
195 return 0;
196 }
Andy Fleming272cc702008-10-30 16:41:01 -0500197 }
198
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700199 return blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500200}
201
202static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
203{
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700204 lbaint_t cur, blocks_todo = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500205
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700206 if (blkcnt == 0)
207 return 0;
208
209 struct mmc *mmc = find_mmc_device(dev_num);
Andy Fleming272cc702008-10-30 16:41:01 -0500210 if (!mmc)
211 return 0;
212
Lei Wend2bf29e2010-09-13 22:07:27 +0800213 if ((start + blkcnt) > mmc->block_dev.lba) {
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700214 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
Lei Wend2bf29e2010-09-13 22:07:27 +0800215 start + blkcnt, mmc->block_dev.lba);
216 return 0;
217 }
Andy Fleming272cc702008-10-30 16:41:01 -0500218
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700219 if (mmc_set_blocklen(mmc, mmc->read_bl_len))
Andy Fleming272cc702008-10-30 16:41:01 -0500220 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500221
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700222 do {
Matt Waddelce0fbcd2011-02-24 16:35:23 +0000223 cur = (blocks_todo > CONFIG_SYS_MMC_MAX_BLK_COUNT) ?
224 CONFIG_SYS_MMC_MAX_BLK_COUNT : blocks_todo;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700225 if(mmc_read_blocks(mmc, dst, start, cur) != cur)
226 return 0;
227 blocks_todo -= cur;
228 start += cur;
229 dst += cur * mmc->read_bl_len;
230 } while (blocks_todo > 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500231
232 return blkcnt;
233}
234
235int mmc_go_idle(struct mmc* mmc)
236{
237 struct mmc_cmd cmd;
238 int err;
239
240 udelay(1000);
241
242 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
243 cmd.cmdarg = 0;
244 cmd.resp_type = MMC_RSP_NONE;
245 cmd.flags = 0;
246
247 err = mmc_send_cmd(mmc, &cmd, NULL);
248
249 if (err)
250 return err;
251
252 udelay(2000);
253
254 return 0;
255}
256
257int
258sd_send_op_cond(struct mmc *mmc)
259{
260 int timeout = 1000;
261 int err;
262 struct mmc_cmd cmd;
263
264 do {
265 cmd.cmdidx = MMC_CMD_APP_CMD;
266 cmd.resp_type = MMC_RSP_R1;
267 cmd.cmdarg = 0;
268 cmd.flags = 0;
269
270 err = mmc_send_cmd(mmc, &cmd, NULL);
271
272 if (err)
273 return err;
274
275 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
276 cmd.resp_type = MMC_RSP_R3;
Stefano Babic250de122010-01-20 18:20:39 +0100277
278 /*
279 * Most cards do not answer if some reserved bits
280 * in the ocr are set. However, Some controller
281 * can set bit 7 (reserved for low voltages), but
282 * how to manage low voltages SD card is not yet
283 * specified.
284 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000285 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
286 (mmc->voltages & 0xff8000);
Andy Fleming272cc702008-10-30 16:41:01 -0500287
288 if (mmc->version == SD_VERSION_2)
289 cmd.cmdarg |= OCR_HCS;
290
291 err = mmc_send_cmd(mmc, &cmd, NULL);
292
293 if (err)
294 return err;
295
296 udelay(1000);
297 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
298
299 if (timeout <= 0)
300 return UNUSABLE_ERR;
301
302 if (mmc->version != SD_VERSION_2)
303 mmc->version = SD_VERSION_1_0;
304
Thomas Choud52ebf12010-12-24 13:12:21 +0000305 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
306 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
307 cmd.resp_type = MMC_RSP_R3;
308 cmd.cmdarg = 0;
309 cmd.flags = 0;
310
311 err = mmc_send_cmd(mmc, &cmd, NULL);
312
313 if (err)
314 return err;
315 }
316
Rabin Vincent998be3d2009-04-05 13:30:56 +0530317 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500318
319 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
320 mmc->rca = 0;
321
322 return 0;
323}
324
325int mmc_send_op_cond(struct mmc *mmc)
326{
327 int timeout = 1000;
328 struct mmc_cmd cmd;
329 int err;
330
331 /* Some cards seem to need this */
332 mmc_go_idle(mmc);
333
334 do {
335 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
336 cmd.resp_type = MMC_RSP_R3;
Thomas Choud52ebf12010-12-24 13:12:21 +0000337 cmd.cmdarg = OCR_HCS | (mmc_host_is_spi(mmc) ? 0 :
338 mmc->voltages);
Andy Fleming272cc702008-10-30 16:41:01 -0500339 cmd.flags = 0;
340
341 err = mmc_send_cmd(mmc, &cmd, NULL);
342
343 if (err)
344 return err;
345
346 udelay(1000);
347 } while (!(cmd.response[0] & OCR_BUSY) && timeout--);
348
349 if (timeout <= 0)
350 return UNUSABLE_ERR;
351
Thomas Choud52ebf12010-12-24 13:12:21 +0000352 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
353 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
354 cmd.resp_type = MMC_RSP_R3;
355 cmd.cmdarg = 0;
356 cmd.flags = 0;
357
358 err = mmc_send_cmd(mmc, &cmd, NULL);
359
360 if (err)
361 return err;
362 }
363
Andy Fleming272cc702008-10-30 16:41:01 -0500364 mmc->version = MMC_VERSION_UNKNOWN;
Rabin Vincent998be3d2009-04-05 13:30:56 +0530365 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500366
367 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
368 mmc->rca = 0;
369
370 return 0;
371}
372
373
374int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd)
375{
376 struct mmc_cmd cmd;
377 struct mmc_data data;
378 int err;
379
380 /* Get the Card Status Register */
381 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
382 cmd.resp_type = MMC_RSP_R1;
383 cmd.cmdarg = 0;
384 cmd.flags = 0;
385
386 data.dest = ext_csd;
387 data.blocks = 1;
388 data.blocksize = 512;
389 data.flags = MMC_DATA_READ;
390
391 err = mmc_send_cmd(mmc, &cmd, &data);
392
393 return err;
394}
395
396
397int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
398{
399 struct mmc_cmd cmd;
400
401 cmd.cmdidx = MMC_CMD_SWITCH;
402 cmd.resp_type = MMC_RSP_R1b;
403 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
404 (index << 16) |
405 (value << 8);
406 cmd.flags = 0;
407
408 return mmc_send_cmd(mmc, &cmd, NULL);
409}
410
411int mmc_change_freq(struct mmc *mmc)
412{
413 char ext_csd[512];
414 char cardtype;
415 int err;
416
417 mmc->card_caps = 0;
418
Thomas Choud52ebf12010-12-24 13:12:21 +0000419 if (mmc_host_is_spi(mmc))
420 return 0;
421
Andy Fleming272cc702008-10-30 16:41:01 -0500422 /* Only version 4 supports high-speed */
423 if (mmc->version < MMC_VERSION_4)
424 return 0;
425
426 mmc->card_caps |= MMC_MODE_4BIT;
427
428 err = mmc_send_ext_csd(mmc, ext_csd);
429
430 if (err)
431 return err;
432
433 if (ext_csd[212] || ext_csd[213] || ext_csd[214] || ext_csd[215])
434 mmc->high_capacity = 1;
435
436 cardtype = ext_csd[196] & 0xf;
437
438 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
439
440 if (err)
441 return err;
442
443 /* Now check to see that it worked */
444 err = mmc_send_ext_csd(mmc, ext_csd);
445
446 if (err)
447 return err;
448
449 /* No high-speed support */
450 if (!ext_csd[185])
451 return 0;
452
453 /* High Speed is set, there are two types: 52MHz and 26MHz */
454 if (cardtype & MMC_HS_52MHZ)
455 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
456 else
457 mmc->card_caps |= MMC_MODE_HS;
458
459 return 0;
460}
461
462int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
463{
464 struct mmc_cmd cmd;
465 struct mmc_data data;
466
467 /* Switch the frequency */
468 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
469 cmd.resp_type = MMC_RSP_R1;
470 cmd.cmdarg = (mode << 31) | 0xffffff;
471 cmd.cmdarg &= ~(0xf << (group * 4));
472 cmd.cmdarg |= value << (group * 4);
473 cmd.flags = 0;
474
475 data.dest = (char *)resp;
476 data.blocksize = 64;
477 data.blocks = 1;
478 data.flags = MMC_DATA_READ;
479
480 return mmc_send_cmd(mmc, &cmd, &data);
481}
482
483
484int sd_change_freq(struct mmc *mmc)
485{
486 int err;
487 struct mmc_cmd cmd;
488 uint scr[2];
489 uint switch_status[16];
490 struct mmc_data data;
491 int timeout;
492
493 mmc->card_caps = 0;
494
Thomas Choud52ebf12010-12-24 13:12:21 +0000495 if (mmc_host_is_spi(mmc))
496 return 0;
497
Andy Fleming272cc702008-10-30 16:41:01 -0500498 /* Read the SCR to find out if this card supports higher speeds */
499 cmd.cmdidx = MMC_CMD_APP_CMD;
500 cmd.resp_type = MMC_RSP_R1;
501 cmd.cmdarg = mmc->rca << 16;
502 cmd.flags = 0;
503
504 err = mmc_send_cmd(mmc, &cmd, NULL);
505
506 if (err)
507 return err;
508
509 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
510 cmd.resp_type = MMC_RSP_R1;
511 cmd.cmdarg = 0;
512 cmd.flags = 0;
513
514 timeout = 3;
515
516retry_scr:
517 data.dest = (char *)&scr;
518 data.blocksize = 8;
519 data.blocks = 1;
520 data.flags = MMC_DATA_READ;
521
522 err = mmc_send_cmd(mmc, &cmd, &data);
523
524 if (err) {
525 if (timeout--)
526 goto retry_scr;
527
528 return err;
529 }
530
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300531 mmc->scr[0] = __be32_to_cpu(scr[0]);
532 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Fleming272cc702008-10-30 16:41:01 -0500533
534 switch ((mmc->scr[0] >> 24) & 0xf) {
535 case 0:
536 mmc->version = SD_VERSION_1_0;
537 break;
538 case 1:
539 mmc->version = SD_VERSION_1_10;
540 break;
541 case 2:
542 mmc->version = SD_VERSION_2;
543 break;
544 default:
545 mmc->version = SD_VERSION_1_0;
546 break;
547 }
548
Alagu Sankarb44c7082010-05-12 15:08:24 +0530549 if (mmc->scr[0] & SD_DATA_4BIT)
550 mmc->card_caps |= MMC_MODE_4BIT;
551
Andy Fleming272cc702008-10-30 16:41:01 -0500552 /* Version 1.0 doesn't support switching */
553 if (mmc->version == SD_VERSION_1_0)
554 return 0;
555
556 timeout = 4;
557 while (timeout--) {
558 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
559 (u8 *)&switch_status);
560
561 if (err)
562 return err;
563
564 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300565 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Fleming272cc702008-10-30 16:41:01 -0500566 break;
567 }
568
Andy Fleming272cc702008-10-30 16:41:01 -0500569 /* If high-speed isn't supported, we return */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300570 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
Andy Fleming272cc702008-10-30 16:41:01 -0500571 return 0;
572
573 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)&switch_status);
574
575 if (err)
576 return err;
577
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300578 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
Andy Fleming272cc702008-10-30 16:41:01 -0500579 mmc->card_caps |= MMC_MODE_HS;
580
581 return 0;
582}
583
584/* frequency bases */
585/* divided by 10 to be nice to platforms without floating point */
Mike Frysinger5f837c22010-10-20 01:15:53 +0000586static const int fbase[] = {
Andy Fleming272cc702008-10-30 16:41:01 -0500587 10000,
588 100000,
589 1000000,
590 10000000,
591};
592
593/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
594 * to platforms without floating point.
595 */
Mike Frysinger5f837c22010-10-20 01:15:53 +0000596static const int multipliers[] = {
Andy Fleming272cc702008-10-30 16:41:01 -0500597 0, /* reserved */
598 10,
599 12,
600 13,
601 15,
602 20,
603 25,
604 30,
605 35,
606 40,
607 45,
608 50,
609 55,
610 60,
611 70,
612 80,
613};
614
615void mmc_set_ios(struct mmc *mmc)
616{
617 mmc->set_ios(mmc);
618}
619
620void mmc_set_clock(struct mmc *mmc, uint clock)
621{
622 if (clock > mmc->f_max)
623 clock = mmc->f_max;
624
625 if (clock < mmc->f_min)
626 clock = mmc->f_min;
627
628 mmc->clock = clock;
629
630 mmc_set_ios(mmc);
631}
632
633void mmc_set_bus_width(struct mmc *mmc, uint width)
634{
635 mmc->bus_width = width;
636
637 mmc_set_ios(mmc);
638}
639
640int mmc_startup(struct mmc *mmc)
641{
642 int err;
643 uint mult, freq;
644 u64 cmult, csize;
645 struct mmc_cmd cmd;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +0530646 char ext_csd[512];
Andy Fleming272cc702008-10-30 16:41:01 -0500647
Thomas Choud52ebf12010-12-24 13:12:21 +0000648#ifdef CONFIG_MMC_SPI_CRC_ON
649 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
650 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
651 cmd.resp_type = MMC_RSP_R1;
652 cmd.cmdarg = 1;
653 cmd.flags = 0;
654 err = mmc_send_cmd(mmc, &cmd, NULL);
655
656 if (err)
657 return err;
658 }
659#endif
660
Andy Fleming272cc702008-10-30 16:41:01 -0500661 /* Put the Card in Identify Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +0000662 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
663 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
Andy Fleming272cc702008-10-30 16:41:01 -0500664 cmd.resp_type = MMC_RSP_R2;
665 cmd.cmdarg = 0;
666 cmd.flags = 0;
667
668 err = mmc_send_cmd(mmc, &cmd, NULL);
669
670 if (err)
671 return err;
672
673 memcpy(mmc->cid, cmd.response, 16);
674
675 /*
676 * For MMC cards, set the Relative Address.
677 * For SD cards, get the Relatvie Address.
678 * This also puts the cards into Standby State
679 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000680 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
681 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
682 cmd.cmdarg = mmc->rca << 16;
683 cmd.resp_type = MMC_RSP_R6;
684 cmd.flags = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500685
Thomas Choud52ebf12010-12-24 13:12:21 +0000686 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -0500687
Thomas Choud52ebf12010-12-24 13:12:21 +0000688 if (err)
689 return err;
Andy Fleming272cc702008-10-30 16:41:01 -0500690
Thomas Choud52ebf12010-12-24 13:12:21 +0000691 if (IS_SD(mmc))
692 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
693 }
Andy Fleming272cc702008-10-30 16:41:01 -0500694
695 /* Get the Card-Specific Data */
696 cmd.cmdidx = MMC_CMD_SEND_CSD;
697 cmd.resp_type = MMC_RSP_R2;
698 cmd.cmdarg = mmc->rca << 16;
699 cmd.flags = 0;
700
701 err = mmc_send_cmd(mmc, &cmd, NULL);
702
703 if (err)
704 return err;
705
Rabin Vincent998be3d2009-04-05 13:30:56 +0530706 mmc->csd[0] = cmd.response[0];
707 mmc->csd[1] = cmd.response[1];
708 mmc->csd[2] = cmd.response[2];
709 mmc->csd[3] = cmd.response[3];
Andy Fleming272cc702008-10-30 16:41:01 -0500710
711 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincent0b453ff2009-04-05 13:30:55 +0530712 int version = (cmd.response[0] >> 26) & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -0500713
714 switch (version) {
715 case 0:
716 mmc->version = MMC_VERSION_1_2;
717 break;
718 case 1:
719 mmc->version = MMC_VERSION_1_4;
720 break;
721 case 2:
722 mmc->version = MMC_VERSION_2_2;
723 break;
724 case 3:
725 mmc->version = MMC_VERSION_3;
726 break;
727 case 4:
728 mmc->version = MMC_VERSION_4;
729 break;
730 default:
731 mmc->version = MMC_VERSION_1_2;
732 break;
733 }
734 }
735
736 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincent0b453ff2009-04-05 13:30:55 +0530737 freq = fbase[(cmd.response[0] & 0x7)];
738 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Fleming272cc702008-10-30 16:41:01 -0500739
740 mmc->tran_speed = freq * mult;
741
Rabin Vincent998be3d2009-04-05 13:30:56 +0530742 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -0500743
744 if (IS_SD(mmc))
745 mmc->write_bl_len = mmc->read_bl_len;
746 else
Rabin Vincent998be3d2009-04-05 13:30:56 +0530747 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -0500748
749 if (mmc->high_capacity) {
750 csize = (mmc->csd[1] & 0x3f) << 16
751 | (mmc->csd[2] & 0xffff0000) >> 16;
752 cmult = 8;
753 } else {
754 csize = (mmc->csd[1] & 0x3ff) << 2
755 | (mmc->csd[2] & 0xc0000000) >> 30;
756 cmult = (mmc->csd[2] & 0x00038000) >> 15;
757 }
758
759 mmc->capacity = (csize + 1) << (cmult + 2);
760 mmc->capacity *= mmc->read_bl_len;
761
762 if (mmc->read_bl_len > 512)
763 mmc->read_bl_len = 512;
764
765 if (mmc->write_bl_len > 512)
766 mmc->write_bl_len = 512;
767
768 /* Select the card, and put it into Transfer Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +0000769 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
770 cmd.cmdidx = MMC_CMD_SELECT_CARD;
771 cmd.resp_type = MMC_RSP_R1b;
772 cmd.cmdarg = mmc->rca << 16;
773 cmd.flags = 0;
774 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -0500775
Thomas Choud52ebf12010-12-24 13:12:21 +0000776 if (err)
777 return err;
778 }
Andy Fleming272cc702008-10-30 16:41:01 -0500779
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +0530780 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
781 /* check ext_csd version and capacity */
782 err = mmc_send_ext_csd(mmc, ext_csd);
783 if (!err & (ext_csd[192] >= 2)) {
784 mmc->capacity = ext_csd[212] << 0 | ext_csd[213] << 8 |
785 ext_csd[214] << 16 | ext_csd[215] << 24;
786 mmc->capacity *= 512;
787 }
788 }
789
Andy Fleming272cc702008-10-30 16:41:01 -0500790 if (IS_SD(mmc))
791 err = sd_change_freq(mmc);
792 else
793 err = mmc_change_freq(mmc);
794
795 if (err)
796 return err;
797
798 /* Restrict card's capabilities by what the host can do */
799 mmc->card_caps &= mmc->host_caps;
800
801 if (IS_SD(mmc)) {
802 if (mmc->card_caps & MMC_MODE_4BIT) {
803 cmd.cmdidx = MMC_CMD_APP_CMD;
804 cmd.resp_type = MMC_RSP_R1;
805 cmd.cmdarg = mmc->rca << 16;
806 cmd.flags = 0;
807
808 err = mmc_send_cmd(mmc, &cmd, NULL);
809 if (err)
810 return err;
811
812 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
813 cmd.resp_type = MMC_RSP_R1;
814 cmd.cmdarg = 2;
815 cmd.flags = 0;
816 err = mmc_send_cmd(mmc, &cmd, NULL);
817 if (err)
818 return err;
819
820 mmc_set_bus_width(mmc, 4);
821 }
822
823 if (mmc->card_caps & MMC_MODE_HS)
824 mmc_set_clock(mmc, 50000000);
825 else
826 mmc_set_clock(mmc, 25000000);
827 } else {
828 if (mmc->card_caps & MMC_MODE_4BIT) {
829 /* Set the card to use 4 bit*/
830 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
831 EXT_CSD_BUS_WIDTH,
832 EXT_CSD_BUS_WIDTH_4);
833
834 if (err)
835 return err;
836
837 mmc_set_bus_width(mmc, 4);
838 } else if (mmc->card_caps & MMC_MODE_8BIT) {
839 /* Set the card to use 8 bit*/
840 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
841 EXT_CSD_BUS_WIDTH,
842 EXT_CSD_BUS_WIDTH_8);
843
844 if (err)
845 return err;
846
847 mmc_set_bus_width(mmc, 8);
848 }
849
850 if (mmc->card_caps & MMC_MODE_HS) {
851 if (mmc->card_caps & MMC_MODE_HS_52MHz)
852 mmc_set_clock(mmc, 52000000);
853 else
854 mmc_set_clock(mmc, 26000000);
855 } else
856 mmc_set_clock(mmc, 20000000);
857 }
858
859 /* fill in device description */
860 mmc->block_dev.lun = 0;
861 mmc->block_dev.type = 0;
862 mmc->block_dev.blksz = mmc->read_bl_len;
Rabin Vincent9b1f9422009-04-05 13:30:54 +0530863 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
Rabin Vincent0b453ff2009-04-05 13:30:55 +0530864 sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8,
865 (mmc->cid[2] << 8) | (mmc->cid[3] >> 24));
866 sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff,
867 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
868 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
869 sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28,
870 (mmc->cid[2] >> 24) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -0500871 init_part(&mmc->block_dev);
872
873 return 0;
874}
875
876int mmc_send_if_cond(struct mmc *mmc)
877{
878 struct mmc_cmd cmd;
879 int err;
880
881 cmd.cmdidx = SD_CMD_SEND_IF_COND;
882 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
883 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
884 cmd.resp_type = MMC_RSP_R7;
885 cmd.flags = 0;
886
887 err = mmc_send_cmd(mmc, &cmd, NULL);
888
889 if (err)
890 return err;
891
Rabin Vincent998be3d2009-04-05 13:30:56 +0530892 if ((cmd.response[0] & 0xff) != 0xaa)
Andy Fleming272cc702008-10-30 16:41:01 -0500893 return UNUSABLE_ERR;
894 else
895 mmc->version = SD_VERSION_2;
896
897 return 0;
898}
899
900int mmc_register(struct mmc *mmc)
901{
902 /* Setup the universal parts of the block interface just once */
903 mmc->block_dev.if_type = IF_TYPE_MMC;
904 mmc->block_dev.dev = cur_dev_num++;
905 mmc->block_dev.removable = 1;
906 mmc->block_dev.block_read = mmc_bread;
907 mmc->block_dev.block_write = mmc_bwrite;
908
909 INIT_LIST_HEAD (&mmc->link);
910
911 list_add_tail (&mmc->link, &mmc_devices);
912
913 return 0;
914}
915
916block_dev_desc_t *mmc_get_dev(int dev)
917{
918 struct mmc *mmc = find_mmc_device(dev);
919
Rabin Vincente85649c2009-04-05 13:30:53 +0530920 return mmc ? &mmc->block_dev : NULL;
Andy Fleming272cc702008-10-30 16:41:01 -0500921}
922
923int mmc_init(struct mmc *mmc)
924{
925 int err;
926
927 err = mmc->init(mmc);
928
929 if (err)
930 return err;
931
Ilya Yanokb86b85e2009-06-29 17:53:16 +0400932 mmc_set_bus_width(mmc, 1);
933 mmc_set_clock(mmc, 1);
934
Andy Fleming272cc702008-10-30 16:41:01 -0500935 /* Reset the Card */
936 err = mmc_go_idle(mmc);
937
938 if (err)
939 return err;
940
941 /* Test for SD version 2 */
942 err = mmc_send_if_cond(mmc);
943
Andy Fleming272cc702008-10-30 16:41:01 -0500944 /* Now try to get the SD card's operating condition */
945 err = sd_send_op_cond(mmc);
946
947 /* If the command timed out, we check for an MMC card */
948 if (err == TIMEOUT) {
949 err = mmc_send_op_cond(mmc);
950
951 if (err) {
952 printf("Card did not respond to voltage select!\n");
953 return UNUSABLE_ERR;
954 }
955 }
956
957 return mmc_startup(mmc);
958}
959
960/*
961 * CPU and board-specific MMC initializations. Aliased function
962 * signals caller to move on
963 */
964static int __def_mmc_init(bd_t *bis)
965{
966 return -1;
967}
968
Peter Tyserf9a109b2009-04-20 11:08:46 -0500969int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
970int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
Andy Fleming272cc702008-10-30 16:41:01 -0500971
972void print_mmc_devices(char separator)
973{
974 struct mmc *m;
975 struct list_head *entry;
976
977 list_for_each(entry, &mmc_devices) {
978 m = list_entry(entry, struct mmc, link);
979
980 printf("%s: %d", m->name, m->block_dev.dev);
981
982 if (entry->next != &mmc_devices)
983 printf("%c ", separator);
984 }
985
986 printf("\n");
987}
988
989int mmc_initialize(bd_t *bis)
990{
991 INIT_LIST_HEAD (&mmc_devices);
992 cur_dev_num = 0;
993
994 if (board_mmc_init(bis) < 0)
995 cpu_mmc_init(bis);
996
997 print_mmc_devices(',');
998
999 return 0;
1000}