blob: 6db37b1fc548a49be870d06f4737e5b799e6db76 [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
Thierry Reding314284b2012-01-02 01:15:36 +000043int __board_mmc_getcd(struct mmc *mmc) {
Stefano Babic11fdade2010-02-05 15:04:43 +010044 return -1;
45}
46
Thierry Reding314284b2012-01-02 01:15:36 +000047int board_mmc_getcd(struct mmc *mmc)__attribute__((weak,
Stefano Babic11fdade2010-02-05 15:04:43 +010048 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;
Marek Vasutaaf3d412011-08-10 09:24:48 +0200118 if (!mmc_host_is_spi(mmc))
119 cmd.cmdarg = mmc->rca << 16;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000120 cmd.flags = 0;
121
122 do {
123 err = mmc_send_cmd(mmc, &cmd, NULL);
124 if (err)
125 return err;
126 else if (cmd.response[0] & MMC_STATUS_RDY_FOR_DATA)
127 break;
128
129 udelay(1000);
130
131 if (cmd.response[0] & MMC_STATUS_MASK) {
132 printf("Status Error: 0x%08X\n", cmd.response[0]);
133 return COMM_ERR;
134 }
135 } while (timeout--);
136
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000137#ifdef CONFIG_MMC_TRACE
138 status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
139 printf("CURR STATE:%d\n", status);
140#endif
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000141 if (!timeout) {
142 printf("Timeout waiting card ready\n");
143 return TIMEOUT;
144 }
145
146 return 0;
147}
148
Andy Fleming272cc702008-10-30 16:41:01 -0500149int mmc_set_blocklen(struct mmc *mmc, int len)
150{
151 struct mmc_cmd cmd;
152
153 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
154 cmd.resp_type = MMC_RSP_R1;
155 cmd.cmdarg = len;
156 cmd.flags = 0;
157
158 return mmc_send_cmd(mmc, &cmd, NULL);
159}
160
161struct mmc *find_mmc_device(int dev_num)
162{
163 struct mmc *m;
164 struct list_head *entry;
165
166 list_for_each(entry, &mmc_devices) {
167 m = list_entry(entry, struct mmc, link);
168
169 if (m->block_dev.dev == dev_num)
170 return m;
171 }
172
173 printf("MMC Device %d not found\n", dev_num);
174
175 return NULL;
176}
177
Lei Wene6f99a52011-06-22 17:03:31 +0000178static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
179{
180 struct mmc_cmd cmd;
181 ulong end;
182 int err, start_cmd, end_cmd;
183
184 if (mmc->high_capacity)
185 end = start + blkcnt - 1;
186 else {
187 end = (start + blkcnt - 1) * mmc->write_bl_len;
188 start *= mmc->write_bl_len;
189 }
190
191 if (IS_SD(mmc)) {
192 start_cmd = SD_CMD_ERASE_WR_BLK_START;
193 end_cmd = SD_CMD_ERASE_WR_BLK_END;
194 } else {
195 start_cmd = MMC_CMD_ERASE_GROUP_START;
196 end_cmd = MMC_CMD_ERASE_GROUP_END;
197 }
198
199 cmd.cmdidx = start_cmd;
200 cmd.cmdarg = start;
201 cmd.resp_type = MMC_RSP_R1;
202 cmd.flags = 0;
203
204 err = mmc_send_cmd(mmc, &cmd, NULL);
205 if (err)
206 goto err_out;
207
208 cmd.cmdidx = end_cmd;
209 cmd.cmdarg = end;
210
211 err = mmc_send_cmd(mmc, &cmd, NULL);
212 if (err)
213 goto err_out;
214
215 cmd.cmdidx = MMC_CMD_ERASE;
216 cmd.cmdarg = SECURE_ERASE;
217 cmd.resp_type = MMC_RSP_R1b;
218
219 err = mmc_send_cmd(mmc, &cmd, NULL);
220 if (err)
221 goto err_out;
222
223 return 0;
224
225err_out:
226 puts("mmc erase failed\n");
227 return err;
228}
229
230static unsigned long
231mmc_berase(int dev_num, unsigned long start, lbaint_t blkcnt)
232{
233 int err = 0;
234 struct mmc *mmc = find_mmc_device(dev_num);
235 lbaint_t blk = 0, blk_r = 0;
236
237 if (!mmc)
238 return -1;
239
240 if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size))
241 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
242 "The erase range would be change to 0x%lx~0x%lx\n\n",
243 mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
244 ((start + blkcnt + mmc->erase_grp_size)
245 & ~(mmc->erase_grp_size - 1)) - 1);
246
247 while (blk < blkcnt) {
248 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
249 mmc->erase_grp_size : (blkcnt - blk);
250 err = mmc_erase_t(mmc, start + blk, blk_r);
251 if (err)
252 break;
253
254 blk += blk_r;
255 }
256
257 return blk;
258}
259
Andy Fleming272cc702008-10-30 16:41:01 -0500260static ulong
Lei Wen01581262010-10-14 13:38:11 +0800261mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
Andy Fleming272cc702008-10-30 16:41:01 -0500262{
263 struct mmc_cmd cmd;
264 struct mmc_data data;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000265 int timeout = 1000;
Andy Fleming272cc702008-10-30 16:41:01 -0500266
Lei Wend2bf29e2010-09-13 22:07:27 +0800267 if ((start + blkcnt) > mmc->block_dev.lba) {
Steve Sakomandef412b2010-10-28 09:00:26 -0700268 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
Lei Wend2bf29e2010-09-13 22:07:27 +0800269 start + blkcnt, mmc->block_dev.lba);
270 return 0;
271 }
Andy Fleming272cc702008-10-30 16:41:01 -0500272
273 if (blkcnt > 1)
274 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
275 else
276 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
277
278 if (mmc->high_capacity)
279 cmd.cmdarg = start;
280 else
Steve Sakomandef412b2010-10-28 09:00:26 -0700281 cmd.cmdarg = start * mmc->write_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500282
283 cmd.resp_type = MMC_RSP_R1;
284 cmd.flags = 0;
285
286 data.src = src;
287 data.blocks = blkcnt;
Steve Sakomandef412b2010-10-28 09:00:26 -0700288 data.blocksize = mmc->write_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500289 data.flags = MMC_DATA_WRITE;
290
Steve Sakomandef412b2010-10-28 09:00:26 -0700291 if (mmc_send_cmd(mmc, &cmd, &data)) {
292 printf("mmc write failed\n");
293 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500294 }
295
Thomas Choud52ebf12010-12-24 13:12:21 +0000296 /* SPI multiblock writes terminate using a special
297 * token, not a STOP_TRANSMISSION request.
298 */
299 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
Andy Fleming272cc702008-10-30 16:41:01 -0500300 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
301 cmd.cmdarg = 0;
302 cmd.resp_type = MMC_RSP_R1b;
303 cmd.flags = 0;
Steve Sakomandef412b2010-10-28 09:00:26 -0700304 if (mmc_send_cmd(mmc, &cmd, NULL)) {
305 printf("mmc fail to send stop cmd\n");
306 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800307 }
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000308
309 /* Waiting for the ready status */
310 mmc_send_status(mmc, timeout);
Andy Fleming272cc702008-10-30 16:41:01 -0500311 }
312
313 return blkcnt;
314}
315
Lei Wen01581262010-10-14 13:38:11 +0800316static ulong
317mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
318{
Lei Wen01581262010-10-14 13:38:11 +0800319 lbaint_t cur, blocks_todo = blkcnt;
320
Steve Sakomandef412b2010-10-28 09:00:26 -0700321 struct mmc *mmc = find_mmc_device(dev_num);
Lei Wen01581262010-10-14 13:38:11 +0800322 if (!mmc)
Steve Sakomandef412b2010-10-28 09:00:26 -0700323 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800324
Steve Sakomandef412b2010-10-28 09:00:26 -0700325 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
326 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800327
328 do {
John Rigby8feafcc2011-04-18 05:50:08 +0000329 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
Lei Wen01581262010-10-14 13:38:11 +0800330 if(mmc_write_blocks(mmc, start, cur, src) != cur)
Steve Sakomandef412b2010-10-28 09:00:26 -0700331 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800332 blocks_todo -= cur;
333 start += cur;
334 src += cur * mmc->write_bl_len;
335 } while (blocks_todo > 0);
336
337 return blkcnt;
338}
339
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700340int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt)
Andy Fleming272cc702008-10-30 16:41:01 -0500341{
342 struct mmc_cmd cmd;
343 struct mmc_data data;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000344 int timeout = 1000;
Andy Fleming272cc702008-10-30 16:41:01 -0500345
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700346 if (blkcnt > 1)
347 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
348 else
349 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Fleming272cc702008-10-30 16:41:01 -0500350
351 if (mmc->high_capacity)
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700352 cmd.cmdarg = start;
Andy Fleming272cc702008-10-30 16:41:01 -0500353 else
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700354 cmd.cmdarg = start * mmc->read_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500355
356 cmd.resp_type = MMC_RSP_R1;
357 cmd.flags = 0;
358
359 data.dest = dst;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700360 data.blocks = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500361 data.blocksize = mmc->read_bl_len;
362 data.flags = MMC_DATA_READ;
363
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700364 if (mmc_send_cmd(mmc, &cmd, &data))
365 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500366
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700367 if (blkcnt > 1) {
368 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
369 cmd.cmdarg = 0;
370 cmd.resp_type = MMC_RSP_R1b;
371 cmd.flags = 0;
372 if (mmc_send_cmd(mmc, &cmd, NULL)) {
373 printf("mmc fail to send stop cmd\n");
374 return 0;
375 }
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000376
377 /* Waiting for the ready status */
378 mmc_send_status(mmc, timeout);
Andy Fleming272cc702008-10-30 16:41:01 -0500379 }
380
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700381 return blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500382}
383
384static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
385{
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700386 lbaint_t cur, blocks_todo = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500387
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700388 if (blkcnt == 0)
389 return 0;
390
391 struct mmc *mmc = find_mmc_device(dev_num);
Andy Fleming272cc702008-10-30 16:41:01 -0500392 if (!mmc)
393 return 0;
394
Lei Wend2bf29e2010-09-13 22:07:27 +0800395 if ((start + blkcnt) > mmc->block_dev.lba) {
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700396 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
Lei Wend2bf29e2010-09-13 22:07:27 +0800397 start + blkcnt, mmc->block_dev.lba);
398 return 0;
399 }
Andy Fleming272cc702008-10-30 16:41:01 -0500400
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700401 if (mmc_set_blocklen(mmc, mmc->read_bl_len))
Andy Fleming272cc702008-10-30 16:41:01 -0500402 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500403
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700404 do {
John Rigby8feafcc2011-04-18 05:50:08 +0000405 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700406 if(mmc_read_blocks(mmc, dst, start, cur) != cur)
407 return 0;
408 blocks_todo -= cur;
409 start += cur;
410 dst += cur * mmc->read_bl_len;
411 } while (blocks_todo > 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500412
413 return blkcnt;
414}
415
416int mmc_go_idle(struct mmc* mmc)
417{
418 struct mmc_cmd cmd;
419 int err;
420
421 udelay(1000);
422
423 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
424 cmd.cmdarg = 0;
425 cmd.resp_type = MMC_RSP_NONE;
426 cmd.flags = 0;
427
428 err = mmc_send_cmd(mmc, &cmd, NULL);
429
430 if (err)
431 return err;
432
433 udelay(2000);
434
435 return 0;
436}
437
438int
439sd_send_op_cond(struct mmc *mmc)
440{
441 int timeout = 1000;
442 int err;
443 struct mmc_cmd cmd;
444
445 do {
446 cmd.cmdidx = MMC_CMD_APP_CMD;
447 cmd.resp_type = MMC_RSP_R1;
448 cmd.cmdarg = 0;
449 cmd.flags = 0;
450
451 err = mmc_send_cmd(mmc, &cmd, NULL);
452
453 if (err)
454 return err;
455
456 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
457 cmd.resp_type = MMC_RSP_R3;
Stefano Babic250de122010-01-20 18:20:39 +0100458
459 /*
460 * Most cards do not answer if some reserved bits
461 * in the ocr are set. However, Some controller
462 * can set bit 7 (reserved for low voltages), but
463 * how to manage low voltages SD card is not yet
464 * specified.
465 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000466 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
467 (mmc->voltages & 0xff8000);
Andy Fleming272cc702008-10-30 16:41:01 -0500468
469 if (mmc->version == SD_VERSION_2)
470 cmd.cmdarg |= OCR_HCS;
471
472 err = mmc_send_cmd(mmc, &cmd, NULL);
473
474 if (err)
475 return err;
476
477 udelay(1000);
478 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
479
480 if (timeout <= 0)
481 return UNUSABLE_ERR;
482
483 if (mmc->version != SD_VERSION_2)
484 mmc->version = SD_VERSION_1_0;
485
Thomas Choud52ebf12010-12-24 13:12:21 +0000486 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
487 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
488 cmd.resp_type = MMC_RSP_R3;
489 cmd.cmdarg = 0;
490 cmd.flags = 0;
491
492 err = mmc_send_cmd(mmc, &cmd, NULL);
493
494 if (err)
495 return err;
496 }
497
Rabin Vincent998be3d2009-04-05 13:30:56 +0530498 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500499
500 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
501 mmc->rca = 0;
502
503 return 0;
504}
505
506int mmc_send_op_cond(struct mmc *mmc)
507{
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000508 int timeout = 10000;
Andy Fleming272cc702008-10-30 16:41:01 -0500509 struct mmc_cmd cmd;
510 int err;
511
512 /* Some cards seem to need this */
513 mmc_go_idle(mmc);
514
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000515 /* Asking to the card its capabilities */
516 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
517 cmd.resp_type = MMC_RSP_R3;
518 cmd.cmdarg = 0;
519 cmd.flags = 0;
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200520
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000521 err = mmc_send_cmd(mmc, &cmd, NULL);
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200522
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000523 if (err)
524 return err;
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200525
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000526 udelay(1000);
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200527
Andy Fleming272cc702008-10-30 16:41:01 -0500528 do {
529 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
530 cmd.resp_type = MMC_RSP_R3;
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000531 cmd.cmdarg = (mmc_host_is_spi(mmc) ? 0 :
532 (mmc->voltages &
533 (cmd.response[0] & OCR_VOLTAGE_MASK)) |
534 (cmd.response[0] & OCR_ACCESS_MODE));
Łukasz Majewskib1f1e8212011-07-05 02:19:44 +0000535
536 if (mmc->host_caps & MMC_MODE_HC)
537 cmd.cmdarg |= OCR_HCS;
538
Andy Fleming272cc702008-10-30 16:41:01 -0500539 cmd.flags = 0;
540
541 err = mmc_send_cmd(mmc, &cmd, NULL);
542
543 if (err)
544 return err;
545
546 udelay(1000);
547 } while (!(cmd.response[0] & OCR_BUSY) && timeout--);
548
549 if (timeout <= 0)
550 return UNUSABLE_ERR;
551
Thomas Choud52ebf12010-12-24 13:12:21 +0000552 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
553 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
554 cmd.resp_type = MMC_RSP_R3;
555 cmd.cmdarg = 0;
556 cmd.flags = 0;
557
558 err = mmc_send_cmd(mmc, &cmd, NULL);
559
560 if (err)
561 return err;
562 }
563
Andy Fleming272cc702008-10-30 16:41:01 -0500564 mmc->version = MMC_VERSION_UNKNOWN;
Rabin Vincent998be3d2009-04-05 13:30:56 +0530565 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500566
567 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
568 mmc->rca = 0;
569
570 return 0;
571}
572
573
574int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd)
575{
576 struct mmc_cmd cmd;
577 struct mmc_data data;
578 int err;
579
580 /* Get the Card Status Register */
581 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
582 cmd.resp_type = MMC_RSP_R1;
583 cmd.cmdarg = 0;
584 cmd.flags = 0;
585
586 data.dest = ext_csd;
587 data.blocks = 1;
588 data.blocksize = 512;
589 data.flags = MMC_DATA_READ;
590
591 err = mmc_send_cmd(mmc, &cmd, &data);
592
593 return err;
594}
595
596
597int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
598{
599 struct mmc_cmd cmd;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000600 int timeout = 1000;
601 int ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500602
603 cmd.cmdidx = MMC_CMD_SWITCH;
604 cmd.resp_type = MMC_RSP_R1b;
605 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000606 (index << 16) |
607 (value << 8);
Andy Fleming272cc702008-10-30 16:41:01 -0500608 cmd.flags = 0;
609
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000610 ret = mmc_send_cmd(mmc, &cmd, NULL);
611
612 /* Waiting for the ready status */
613 mmc_send_status(mmc, timeout);
614
615 return ret;
616
Andy Fleming272cc702008-10-30 16:41:01 -0500617}
618
619int mmc_change_freq(struct mmc *mmc)
620{
Anton staafa1969922011-10-04 11:24:50 +0000621 ALLOC_CACHE_ALIGN_BUFFER(char, ext_csd, 512);
Andy Fleming272cc702008-10-30 16:41:01 -0500622 char cardtype;
623 int err;
624
625 mmc->card_caps = 0;
626
Thomas Choud52ebf12010-12-24 13:12:21 +0000627 if (mmc_host_is_spi(mmc))
628 return 0;
629
Andy Fleming272cc702008-10-30 16:41:01 -0500630 /* Only version 4 supports high-speed */
631 if (mmc->version < MMC_VERSION_4)
632 return 0;
633
Andy Fleming272cc702008-10-30 16:41:01 -0500634 err = mmc_send_ext_csd(mmc, ext_csd);
635
636 if (err)
637 return err;
638
Lei Wen0560db12011-10-03 20:35:10 +0000639 cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -0500640
641 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
642
643 if (err)
644 return err;
645
646 /* Now check to see that it worked */
647 err = mmc_send_ext_csd(mmc, ext_csd);
648
649 if (err)
650 return err;
651
652 /* No high-speed support */
Lei Wen0560db12011-10-03 20:35:10 +0000653 if (!ext_csd[EXT_CSD_HS_TIMING])
Andy Fleming272cc702008-10-30 16:41:01 -0500654 return 0;
655
656 /* High Speed is set, there are two types: 52MHz and 26MHz */
657 if (cardtype & MMC_HS_52MHZ)
658 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
659 else
660 mmc->card_caps |= MMC_MODE_HS;
661
662 return 0;
663}
664
Lei Wenbc897b12011-05-02 16:26:26 +0000665int mmc_switch_part(int dev_num, unsigned int part_num)
666{
667 struct mmc *mmc = find_mmc_device(dev_num);
668
669 if (!mmc)
670 return -1;
671
672 return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
673 (mmc->part_config & ~PART_ACCESS_MASK)
674 | (part_num & PART_ACCESS_MASK));
675}
676
Thierry Reding48972d92012-01-02 01:15:37 +0000677int mmc_getcd(struct mmc *mmc)
678{
679 int cd;
680
681 cd = board_mmc_getcd(mmc);
682
683 if ((cd < 0) && mmc->getcd)
684 cd = mmc->getcd(mmc);
685
686 return cd;
687}
688
Andy Fleming272cc702008-10-30 16:41:01 -0500689int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
690{
691 struct mmc_cmd cmd;
692 struct mmc_data data;
693
694 /* Switch the frequency */
695 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
696 cmd.resp_type = MMC_RSP_R1;
697 cmd.cmdarg = (mode << 31) | 0xffffff;
698 cmd.cmdarg &= ~(0xf << (group * 4));
699 cmd.cmdarg |= value << (group * 4);
700 cmd.flags = 0;
701
702 data.dest = (char *)resp;
703 data.blocksize = 64;
704 data.blocks = 1;
705 data.flags = MMC_DATA_READ;
706
707 return mmc_send_cmd(mmc, &cmd, &data);
708}
709
710
711int sd_change_freq(struct mmc *mmc)
712{
713 int err;
714 struct mmc_cmd cmd;
Anton staaff781dd32011-10-03 13:54:59 +0000715 ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
716 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
Andy Fleming272cc702008-10-30 16:41:01 -0500717 struct mmc_data data;
718 int timeout;
719
720 mmc->card_caps = 0;
721
Thomas Choud52ebf12010-12-24 13:12:21 +0000722 if (mmc_host_is_spi(mmc))
723 return 0;
724
Andy Fleming272cc702008-10-30 16:41:01 -0500725 /* Read the SCR to find out if this card supports higher speeds */
726 cmd.cmdidx = MMC_CMD_APP_CMD;
727 cmd.resp_type = MMC_RSP_R1;
728 cmd.cmdarg = mmc->rca << 16;
729 cmd.flags = 0;
730
731 err = mmc_send_cmd(mmc, &cmd, NULL);
732
733 if (err)
734 return err;
735
736 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
737 cmd.resp_type = MMC_RSP_R1;
738 cmd.cmdarg = 0;
739 cmd.flags = 0;
740
741 timeout = 3;
742
743retry_scr:
Anton staaff781dd32011-10-03 13:54:59 +0000744 data.dest = (char *)scr;
Andy Fleming272cc702008-10-30 16:41:01 -0500745 data.blocksize = 8;
746 data.blocks = 1;
747 data.flags = MMC_DATA_READ;
748
749 err = mmc_send_cmd(mmc, &cmd, &data);
750
751 if (err) {
752 if (timeout--)
753 goto retry_scr;
754
755 return err;
756 }
757
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300758 mmc->scr[0] = __be32_to_cpu(scr[0]);
759 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Fleming272cc702008-10-30 16:41:01 -0500760
761 switch ((mmc->scr[0] >> 24) & 0xf) {
762 case 0:
763 mmc->version = SD_VERSION_1_0;
764 break;
765 case 1:
766 mmc->version = SD_VERSION_1_10;
767 break;
768 case 2:
769 mmc->version = SD_VERSION_2;
770 break;
771 default:
772 mmc->version = SD_VERSION_1_0;
773 break;
774 }
775
Alagu Sankarb44c7082010-05-12 15:08:24 +0530776 if (mmc->scr[0] & SD_DATA_4BIT)
777 mmc->card_caps |= MMC_MODE_4BIT;
778
Andy Fleming272cc702008-10-30 16:41:01 -0500779 /* Version 1.0 doesn't support switching */
780 if (mmc->version == SD_VERSION_1_0)
781 return 0;
782
783 timeout = 4;
784 while (timeout--) {
785 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
Anton staaff781dd32011-10-03 13:54:59 +0000786 (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -0500787
788 if (err)
789 return err;
790
791 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300792 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Fleming272cc702008-10-30 16:41:01 -0500793 break;
794 }
795
Andy Fleming272cc702008-10-30 16:41:01 -0500796 /* If high-speed isn't supported, we return */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300797 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
Andy Fleming272cc702008-10-30 16:41:01 -0500798 return 0;
799
Macpaul Lin2c3fbf42011-11-28 16:31:09 +0000800 /*
801 * If the host doesn't support SD_HIGHSPEED, do not switch card to
802 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
803 * This can avoid furthur problem when the card runs in different
804 * mode between the host.
805 */
806 if (!((mmc->host_caps & MMC_MODE_HS_52MHz) &&
807 (mmc->host_caps & MMC_MODE_HS)))
808 return 0;
809
Anton staaff781dd32011-10-03 13:54:59 +0000810 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -0500811
812 if (err)
813 return err;
814
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300815 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
Andy Fleming272cc702008-10-30 16:41:01 -0500816 mmc->card_caps |= MMC_MODE_HS;
817
818 return 0;
819}
820
821/* frequency bases */
822/* divided by 10 to be nice to platforms without floating point */
Mike Frysinger5f837c22010-10-20 01:15:53 +0000823static const int fbase[] = {
Andy Fleming272cc702008-10-30 16:41:01 -0500824 10000,
825 100000,
826 1000000,
827 10000000,
828};
829
830/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
831 * to platforms without floating point.
832 */
Mike Frysinger5f837c22010-10-20 01:15:53 +0000833static const int multipliers[] = {
Andy Fleming272cc702008-10-30 16:41:01 -0500834 0, /* reserved */
835 10,
836 12,
837 13,
838 15,
839 20,
840 25,
841 30,
842 35,
843 40,
844 45,
845 50,
846 55,
847 60,
848 70,
849 80,
850};
851
852void mmc_set_ios(struct mmc *mmc)
853{
854 mmc->set_ios(mmc);
855}
856
857void mmc_set_clock(struct mmc *mmc, uint clock)
858{
859 if (clock > mmc->f_max)
860 clock = mmc->f_max;
861
862 if (clock < mmc->f_min)
863 clock = mmc->f_min;
864
865 mmc->clock = clock;
866
867 mmc_set_ios(mmc);
868}
869
870void mmc_set_bus_width(struct mmc *mmc, uint width)
871{
872 mmc->bus_width = width;
873
874 mmc_set_ios(mmc);
875}
876
877int mmc_startup(struct mmc *mmc)
878{
Lei Wen41378942011-10-03 20:35:11 +0000879 int err, width;
Andy Fleming272cc702008-10-30 16:41:01 -0500880 uint mult, freq;
Yoshihiro Shimoda639b7822011-07-04 22:13:26 +0000881 u64 cmult, csize, capacity;
Andy Fleming272cc702008-10-30 16:41:01 -0500882 struct mmc_cmd cmd;
Anton staafa1969922011-10-04 11:24:50 +0000883 ALLOC_CACHE_ALIGN_BUFFER(char, ext_csd, 512);
Lei Wen41378942011-10-03 20:35:11 +0000884 ALLOC_CACHE_ALIGN_BUFFER(char, test_csd, 512);
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000885 int timeout = 1000;
Andy Fleming272cc702008-10-30 16:41:01 -0500886
Thomas Choud52ebf12010-12-24 13:12:21 +0000887#ifdef CONFIG_MMC_SPI_CRC_ON
888 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
889 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
890 cmd.resp_type = MMC_RSP_R1;
891 cmd.cmdarg = 1;
892 cmd.flags = 0;
893 err = mmc_send_cmd(mmc, &cmd, NULL);
894
895 if (err)
896 return err;
897 }
898#endif
899
Andy Fleming272cc702008-10-30 16:41:01 -0500900 /* Put the Card in Identify Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +0000901 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
902 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
Andy Fleming272cc702008-10-30 16:41:01 -0500903 cmd.resp_type = MMC_RSP_R2;
904 cmd.cmdarg = 0;
905 cmd.flags = 0;
906
907 err = mmc_send_cmd(mmc, &cmd, NULL);
908
909 if (err)
910 return err;
911
912 memcpy(mmc->cid, cmd.response, 16);
913
914 /*
915 * For MMC cards, set the Relative Address.
916 * For SD cards, get the Relatvie Address.
917 * This also puts the cards into Standby State
918 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000919 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
920 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
921 cmd.cmdarg = mmc->rca << 16;
922 cmd.resp_type = MMC_RSP_R6;
923 cmd.flags = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500924
Thomas Choud52ebf12010-12-24 13:12:21 +0000925 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -0500926
Thomas Choud52ebf12010-12-24 13:12:21 +0000927 if (err)
928 return err;
Andy Fleming272cc702008-10-30 16:41:01 -0500929
Thomas Choud52ebf12010-12-24 13:12:21 +0000930 if (IS_SD(mmc))
931 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
932 }
Andy Fleming272cc702008-10-30 16:41:01 -0500933
934 /* Get the Card-Specific Data */
935 cmd.cmdidx = MMC_CMD_SEND_CSD;
936 cmd.resp_type = MMC_RSP_R2;
937 cmd.cmdarg = mmc->rca << 16;
938 cmd.flags = 0;
939
940 err = mmc_send_cmd(mmc, &cmd, NULL);
941
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000942 /* Waiting for the ready status */
943 mmc_send_status(mmc, timeout);
944
Andy Fleming272cc702008-10-30 16:41:01 -0500945 if (err)
946 return err;
947
Rabin Vincent998be3d2009-04-05 13:30:56 +0530948 mmc->csd[0] = cmd.response[0];
949 mmc->csd[1] = cmd.response[1];
950 mmc->csd[2] = cmd.response[2];
951 mmc->csd[3] = cmd.response[3];
Andy Fleming272cc702008-10-30 16:41:01 -0500952
953 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincent0b453ff2009-04-05 13:30:55 +0530954 int version = (cmd.response[0] >> 26) & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -0500955
956 switch (version) {
957 case 0:
958 mmc->version = MMC_VERSION_1_2;
959 break;
960 case 1:
961 mmc->version = MMC_VERSION_1_4;
962 break;
963 case 2:
964 mmc->version = MMC_VERSION_2_2;
965 break;
966 case 3:
967 mmc->version = MMC_VERSION_3;
968 break;
969 case 4:
970 mmc->version = MMC_VERSION_4;
971 break;
972 default:
973 mmc->version = MMC_VERSION_1_2;
974 break;
975 }
976 }
977
978 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincent0b453ff2009-04-05 13:30:55 +0530979 freq = fbase[(cmd.response[0] & 0x7)];
980 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Fleming272cc702008-10-30 16:41:01 -0500981
982 mmc->tran_speed = freq * mult;
983
Rabin Vincent998be3d2009-04-05 13:30:56 +0530984 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -0500985
986 if (IS_SD(mmc))
987 mmc->write_bl_len = mmc->read_bl_len;
988 else
Rabin Vincent998be3d2009-04-05 13:30:56 +0530989 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -0500990
991 if (mmc->high_capacity) {
992 csize = (mmc->csd[1] & 0x3f) << 16
993 | (mmc->csd[2] & 0xffff0000) >> 16;
994 cmult = 8;
995 } else {
996 csize = (mmc->csd[1] & 0x3ff) << 2
997 | (mmc->csd[2] & 0xc0000000) >> 30;
998 cmult = (mmc->csd[2] & 0x00038000) >> 15;
999 }
1000
1001 mmc->capacity = (csize + 1) << (cmult + 2);
1002 mmc->capacity *= mmc->read_bl_len;
1003
1004 if (mmc->read_bl_len > 512)
1005 mmc->read_bl_len = 512;
1006
1007 if (mmc->write_bl_len > 512)
1008 mmc->write_bl_len = 512;
1009
1010 /* Select the card, and put it into Transfer Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00001011 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1012 cmd.cmdidx = MMC_CMD_SELECT_CARD;
Ajay Bhargavfe8f7062011-10-05 03:13:23 +00001013 cmd.resp_type = MMC_RSP_R1;
Thomas Choud52ebf12010-12-24 13:12:21 +00001014 cmd.cmdarg = mmc->rca << 16;
1015 cmd.flags = 0;
1016 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05001017
Thomas Choud52ebf12010-12-24 13:12:21 +00001018 if (err)
1019 return err;
1020 }
Andy Fleming272cc702008-10-30 16:41:01 -05001021
Lei Wene6f99a52011-06-22 17:03:31 +00001022 /*
1023 * For SD, its erase group is always one sector
1024 */
1025 mmc->erase_grp_size = 1;
Lei Wenbc897b12011-05-02 16:26:26 +00001026 mmc->part_config = MMCPART_NOAVAILABLE;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05301027 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
1028 /* check ext_csd version and capacity */
1029 err = mmc_send_ext_csd(mmc, ext_csd);
Lei Wen0560db12011-10-03 20:35:10 +00001030 if (!err & (ext_csd[EXT_CSD_REV] >= 2)) {
Yoshihiro Shimoda639b7822011-07-04 22:13:26 +00001031 /*
1032 * According to the JEDEC Standard, the value of
1033 * ext_csd's capacity is valid if the value is more
1034 * than 2GB
1035 */
Lei Wen0560db12011-10-03 20:35:10 +00001036 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
1037 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
1038 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
1039 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
Yoshihiro Shimoda639b7822011-07-04 22:13:26 +00001040 capacity *= 512;
Łukasz Majewskib1f1e8212011-07-05 02:19:44 +00001041 if ((capacity >> 20) > 2 * 1024)
Yoshihiro Shimoda639b7822011-07-04 22:13:26 +00001042 mmc->capacity = capacity;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05301043 }
Lei Wenbc897b12011-05-02 16:26:26 +00001044
Lei Wene6f99a52011-06-22 17:03:31 +00001045 /*
1046 * Check whether GROUP_DEF is set, if yes, read out
1047 * group size from ext_csd directly, or calculate
1048 * the group size from the csd value.
1049 */
Lei Wen0560db12011-10-03 20:35:10 +00001050 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF])
1051 mmc->erase_grp_size =
1052 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 512 * 1024;
Lei Wene6f99a52011-06-22 17:03:31 +00001053 else {
1054 int erase_gsz, erase_gmul;
1055 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1056 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1057 mmc->erase_grp_size = (erase_gsz + 1)
1058 * (erase_gmul + 1);
1059 }
1060
Lei Wenbc897b12011-05-02 16:26:26 +00001061 /* store the partition info of emmc */
Lei Wen0560db12011-10-03 20:35:10 +00001062 if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT)
1063 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05301064 }
1065
Andy Fleming272cc702008-10-30 16:41:01 -05001066 if (IS_SD(mmc))
1067 err = sd_change_freq(mmc);
1068 else
1069 err = mmc_change_freq(mmc);
1070
1071 if (err)
1072 return err;
1073
1074 /* Restrict card's capabilities by what the host can do */
1075 mmc->card_caps &= mmc->host_caps;
1076
1077 if (IS_SD(mmc)) {
1078 if (mmc->card_caps & MMC_MODE_4BIT) {
1079 cmd.cmdidx = MMC_CMD_APP_CMD;
1080 cmd.resp_type = MMC_RSP_R1;
1081 cmd.cmdarg = mmc->rca << 16;
1082 cmd.flags = 0;
1083
1084 err = mmc_send_cmd(mmc, &cmd, NULL);
1085 if (err)
1086 return err;
1087
1088 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1089 cmd.resp_type = MMC_RSP_R1;
1090 cmd.cmdarg = 2;
1091 cmd.flags = 0;
1092 err = mmc_send_cmd(mmc, &cmd, NULL);
1093 if (err)
1094 return err;
1095
1096 mmc_set_bus_width(mmc, 4);
1097 }
1098
1099 if (mmc->card_caps & MMC_MODE_HS)
1100 mmc_set_clock(mmc, 50000000);
1101 else
1102 mmc_set_clock(mmc, 25000000);
1103 } else {
Lei Wen41378942011-10-03 20:35:11 +00001104 for (width = EXT_CSD_BUS_WIDTH_8; width >= 0; width--) {
Andy Fleming272cc702008-10-30 16:41:01 -05001105 /* Set the card to use 4 bit*/
1106 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
Lei Wen41378942011-10-03 20:35:11 +00001107 EXT_CSD_BUS_WIDTH, width);
Andy Fleming272cc702008-10-30 16:41:01 -05001108
1109 if (err)
Lei Wen41378942011-10-03 20:35:11 +00001110 continue;
Andy Fleming272cc702008-10-30 16:41:01 -05001111
Lei Wen41378942011-10-03 20:35:11 +00001112 if (!width) {
1113 mmc_set_bus_width(mmc, 1);
1114 break;
1115 } else
1116 mmc_set_bus_width(mmc, 4 * width);
Andy Fleming272cc702008-10-30 16:41:01 -05001117
Lei Wen41378942011-10-03 20:35:11 +00001118 err = mmc_send_ext_csd(mmc, test_csd);
1119 if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \
1120 == test_csd[EXT_CSD_PARTITIONING_SUPPORT]
1121 && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \
1122 == test_csd[EXT_CSD_ERASE_GROUP_DEF] \
1123 && ext_csd[EXT_CSD_REV] \
1124 == test_csd[EXT_CSD_REV]
1125 && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \
1126 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1127 && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \
1128 &test_csd[EXT_CSD_SEC_CNT], 4) == 0) {
Andy Fleming272cc702008-10-30 16:41:01 -05001129
Lei Wen41378942011-10-03 20:35:11 +00001130 mmc->card_caps |= width;
1131 break;
1132 }
Andy Fleming272cc702008-10-30 16:41:01 -05001133 }
1134
1135 if (mmc->card_caps & MMC_MODE_HS) {
1136 if (mmc->card_caps & MMC_MODE_HS_52MHz)
1137 mmc_set_clock(mmc, 52000000);
1138 else
1139 mmc_set_clock(mmc, 26000000);
1140 } else
1141 mmc_set_clock(mmc, 20000000);
1142 }
1143
1144 /* fill in device description */
1145 mmc->block_dev.lun = 0;
1146 mmc->block_dev.type = 0;
1147 mmc->block_dev.blksz = mmc->read_bl_len;
Rabin Vincent9b1f9422009-04-05 13:30:54 +05301148 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
Rabin Vincent0b453ff2009-04-05 13:30:55 +05301149 sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8,
1150 (mmc->cid[2] << 8) | (mmc->cid[3] >> 24));
1151 sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff,
1152 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1153 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
1154 sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28,
1155 (mmc->cid[2] >> 24) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -05001156 init_part(&mmc->block_dev);
1157
1158 return 0;
1159}
1160
1161int mmc_send_if_cond(struct mmc *mmc)
1162{
1163 struct mmc_cmd cmd;
1164 int err;
1165
1166 cmd.cmdidx = SD_CMD_SEND_IF_COND;
1167 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1168 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
1169 cmd.resp_type = MMC_RSP_R7;
1170 cmd.flags = 0;
1171
1172 err = mmc_send_cmd(mmc, &cmd, NULL);
1173
1174 if (err)
1175 return err;
1176
Rabin Vincent998be3d2009-04-05 13:30:56 +05301177 if ((cmd.response[0] & 0xff) != 0xaa)
Andy Fleming272cc702008-10-30 16:41:01 -05001178 return UNUSABLE_ERR;
1179 else
1180 mmc->version = SD_VERSION_2;
1181
1182 return 0;
1183}
1184
1185int mmc_register(struct mmc *mmc)
1186{
1187 /* Setup the universal parts of the block interface just once */
1188 mmc->block_dev.if_type = IF_TYPE_MMC;
1189 mmc->block_dev.dev = cur_dev_num++;
1190 mmc->block_dev.removable = 1;
1191 mmc->block_dev.block_read = mmc_bread;
1192 mmc->block_dev.block_write = mmc_bwrite;
Lei Wene6f99a52011-06-22 17:03:31 +00001193 mmc->block_dev.block_erase = mmc_berase;
John Rigby8feafcc2011-04-18 05:50:08 +00001194 if (!mmc->b_max)
1195 mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Andy Fleming272cc702008-10-30 16:41:01 -05001196
1197 INIT_LIST_HEAD (&mmc->link);
1198
1199 list_add_tail (&mmc->link, &mmc_devices);
1200
1201 return 0;
1202}
1203
Matthew McClintockdf3fc522011-05-24 05:31:19 +00001204#ifdef CONFIG_PARTITIONS
Andy Fleming272cc702008-10-30 16:41:01 -05001205block_dev_desc_t *mmc_get_dev(int dev)
1206{
1207 struct mmc *mmc = find_mmc_device(dev);
1208
Rabin Vincente85649c2009-04-05 13:30:53 +05301209 return mmc ? &mmc->block_dev : NULL;
Andy Fleming272cc702008-10-30 16:41:01 -05001210}
Matthew McClintockdf3fc522011-05-24 05:31:19 +00001211#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001212
1213int mmc_init(struct mmc *mmc)
1214{
Macpaul Linafd59322011-11-14 23:35:39 +00001215 int err;
Andy Fleming272cc702008-10-30 16:41:01 -05001216
Thierry Reding48972d92012-01-02 01:15:37 +00001217 if (mmc_getcd(mmc) == 0) {
1218 mmc->has_init = 0;
1219 printf("MMC: no card present\n");
1220 return NO_CARD_ERR;
1221 }
1222
Lei Wenbc897b12011-05-02 16:26:26 +00001223 if (mmc->has_init)
1224 return 0;
1225
Andy Fleming272cc702008-10-30 16:41:01 -05001226 err = mmc->init(mmc);
1227
1228 if (err)
1229 return err;
1230
Ilya Yanokb86b85e2009-06-29 17:53:16 +04001231 mmc_set_bus_width(mmc, 1);
1232 mmc_set_clock(mmc, 1);
1233
Andy Fleming272cc702008-10-30 16:41:01 -05001234 /* Reset the Card */
1235 err = mmc_go_idle(mmc);
1236
1237 if (err)
1238 return err;
1239
Lei Wenbc897b12011-05-02 16:26:26 +00001240 /* The internal partition reset to user partition(0) at every CMD0*/
1241 mmc->part_num = 0;
1242
Andy Fleming272cc702008-10-30 16:41:01 -05001243 /* Test for SD version 2 */
Macpaul Linafd59322011-11-14 23:35:39 +00001244 err = mmc_send_if_cond(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001245
Andy Fleming272cc702008-10-30 16:41:01 -05001246 /* Now try to get the SD card's operating condition */
1247 err = sd_send_op_cond(mmc);
1248
1249 /* If the command timed out, we check for an MMC card */
1250 if (err == TIMEOUT) {
1251 err = mmc_send_op_cond(mmc);
1252
1253 if (err) {
1254 printf("Card did not respond to voltage select!\n");
1255 return UNUSABLE_ERR;
1256 }
1257 }
1258
Lei Wenbc897b12011-05-02 16:26:26 +00001259 err = mmc_startup(mmc);
1260 if (err)
1261 mmc->has_init = 0;
1262 else
1263 mmc->has_init = 1;
1264 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001265}
1266
1267/*
1268 * CPU and board-specific MMC initializations. Aliased function
1269 * signals caller to move on
1270 */
1271static int __def_mmc_init(bd_t *bis)
1272{
1273 return -1;
1274}
1275
Peter Tyserf9a109b2009-04-20 11:08:46 -05001276int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1277int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
Andy Fleming272cc702008-10-30 16:41:01 -05001278
1279void print_mmc_devices(char separator)
1280{
1281 struct mmc *m;
1282 struct list_head *entry;
1283
1284 list_for_each(entry, &mmc_devices) {
1285 m = list_entry(entry, struct mmc, link);
1286
1287 printf("%s: %d", m->name, m->block_dev.dev);
1288
1289 if (entry->next != &mmc_devices)
1290 printf("%c ", separator);
1291 }
1292
1293 printf("\n");
1294}
1295
Lei Wenea6ebe22011-05-02 16:26:25 +00001296int get_mmc_num(void)
1297{
1298 return cur_dev_num;
1299}
1300
Andy Fleming272cc702008-10-30 16:41:01 -05001301int mmc_initialize(bd_t *bis)
1302{
1303 INIT_LIST_HEAD (&mmc_devices);
1304 cur_dev_num = 0;
1305
1306 if (board_mmc_init(bis) < 0)
1307 cpu_mmc_init(bis);
1308
1309 print_mmc_devices(',');
1310
1311 return 0;
1312}