blob: 5fbf95630295a770e1eba3bd82bd676715a91a90 [file] [log] [blame]
Andy Flemingad347bb2008-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 Vincent69d4e2c2009-04-05 13:30:54 +053033#include <div64.h>
Andy Flemingad347bb2008-10-30 16:41:01 -050034
Matt Waddeld0e3c802011-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 Flemingad347bb2008-10-30 16:41:01 -050040static struct list_head mmc_devices;
41static int cur_dev_num = -1;
42
Thierry Redingd7aebf42012-01-02 01:15:36 +000043int __board_mmc_getcd(struct mmc *mmc) {
Stefano Babic6e00edf2010-02-05 15:04:43 +010044 return -1;
45}
46
Thierry Redingd7aebf42012-01-02 01:15:36 +000047int board_mmc_getcd(struct mmc *mmc)__attribute__((weak,
Stefano Babic6e00edf2010-02-05 15:04:43 +010048 alias("__board_mmc_getcd")));
49
Andy Flemingad347bb2008-10-30 16:41:01 -050050int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
51{
Marek Vasutdccb6082012-03-15 18:41:35 +000052 struct mmc_data backup;
Raffaele Recalcati894b1e22011-03-11 02:01:14 +000053 int ret;
Marek Vasutdccb6082012-03-15 18:41:35 +000054
55 memset(&backup, 0, sizeof(backup));
56
Marek Vasutdccb6082012-03-15 18:41:35 +000057#ifdef CONFIG_MMC_TRACE
Raffaele Recalcati894b1e22011-03-11 02:01:14 +000058 int i;
59 u8 *ptr;
60
61 printf("CMD_SEND:%d\n", cmd->cmdidx);
62 printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
Raffaele Recalcati894b1e22011-03-11 02:01:14 +000063 ret = mmc->send_cmd(mmc, cmd, data);
64 switch (cmd->resp_type) {
65 case MMC_RSP_NONE:
66 printf("\t\tMMC_RSP_NONE\n");
67 break;
68 case MMC_RSP_R1:
69 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
70 cmd->response[0]);
71 break;
72 case MMC_RSP_R1b:
73 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
74 cmd->response[0]);
75 break;
76 case MMC_RSP_R2:
77 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
78 cmd->response[0]);
79 printf("\t\t \t\t 0x%08X \n",
80 cmd->response[1]);
81 printf("\t\t \t\t 0x%08X \n",
82 cmd->response[2]);
83 printf("\t\t \t\t 0x%08X \n",
84 cmd->response[3]);
85 printf("\n");
86 printf("\t\t\t\t\tDUMPING DATA\n");
87 for (i = 0; i < 4; i++) {
88 int j;
89 printf("\t\t\t\t\t%03d - ", i*4);
Dirk Behmec9cb4a92012-03-08 02:35:34 +000090 ptr = (u8 *)&cmd->response[i];
Raffaele Recalcati894b1e22011-03-11 02:01:14 +000091 ptr += 3;
92 for (j = 0; j < 4; j++)
93 printf("%02X ", *ptr--);
94 printf("\n");
95 }
96 break;
97 case MMC_RSP_R3:
98 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
99 cmd->response[0]);
100 break;
101 default:
102 printf("\t\tERROR MMC rsp not supported\n");
103 break;
104 }
Raffaele Recalcati894b1e22011-03-11 02:01:14 +0000105#else
Marek Vasutdccb6082012-03-15 18:41:35 +0000106 ret = mmc->send_cmd(mmc, cmd, data);
Raffaele Recalcati894b1e22011-03-11 02:01:14 +0000107#endif
Marek Vasutdccb6082012-03-15 18:41:35 +0000108 return ret;
Andy Flemingad347bb2008-10-30 16:41:01 -0500109}
110
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000111int mmc_send_status(struct mmc *mmc, int timeout)
112{
113 struct mmc_cmd cmd;
Jan Kloetzke31789322012-02-05 22:29:12 +0000114 int err, retries = 5;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000115#ifdef CONFIG_MMC_TRACE
116 int status;
117#endif
118
119 cmd.cmdidx = MMC_CMD_SEND_STATUS;
120 cmd.resp_type = MMC_RSP_R1;
Marek Vasutc4427392011-08-10 09:24:48 +0200121 if (!mmc_host_is_spi(mmc))
122 cmd.cmdarg = mmc->rca << 16;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000123
124 do {
125 err = mmc_send_cmd(mmc, &cmd, NULL);
Jan Kloetzke31789322012-02-05 22:29:12 +0000126 if (!err) {
127 if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
128 (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
129 MMC_STATE_PRG)
130 break;
131 else if (cmd.response[0] & MMC_STATUS_MASK) {
132 printf("Status Error: 0x%08X\n",
133 cmd.response[0]);
134 return COMM_ERR;
135 }
136 } else if (--retries < 0)
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000137 return err;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000138
139 udelay(1000);
140
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000141 } while (timeout--);
142
Raffaele Recalcati894b1e22011-03-11 02:01:14 +0000143#ifdef CONFIG_MMC_TRACE
144 status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
145 printf("CURR STATE:%d\n", status);
146#endif
Jongman Heo1be00d92012-06-03 21:32:13 +0000147 if (timeout <= 0) {
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000148 printf("Timeout waiting card ready\n");
149 return TIMEOUT;
150 }
151
152 return 0;
153}
154
Andy Flemingad347bb2008-10-30 16:41:01 -0500155int mmc_set_blocklen(struct mmc *mmc, int len)
156{
157 struct mmc_cmd cmd;
158
159 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
160 cmd.resp_type = MMC_RSP_R1;
161 cmd.cmdarg = len;
Andy Flemingad347bb2008-10-30 16:41:01 -0500162
163 return mmc_send_cmd(mmc, &cmd, NULL);
164}
165
166struct mmc *find_mmc_device(int dev_num)
167{
168 struct mmc *m;
169 struct list_head *entry;
170
171 list_for_each(entry, &mmc_devices) {
172 m = list_entry(entry, struct mmc, link);
173
174 if (m->block_dev.dev == dev_num)
175 return m;
176 }
177
178 printf("MMC Device %d not found\n", dev_num);
179
180 return NULL;
181}
182
Lei Wenea526762011-06-22 17:03:31 +0000183static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
184{
185 struct mmc_cmd cmd;
186 ulong end;
187 int err, start_cmd, end_cmd;
188
189 if (mmc->high_capacity)
190 end = start + blkcnt - 1;
191 else {
192 end = (start + blkcnt - 1) * mmc->write_bl_len;
193 start *= mmc->write_bl_len;
194 }
195
196 if (IS_SD(mmc)) {
197 start_cmd = SD_CMD_ERASE_WR_BLK_START;
198 end_cmd = SD_CMD_ERASE_WR_BLK_END;
199 } else {
200 start_cmd = MMC_CMD_ERASE_GROUP_START;
201 end_cmd = MMC_CMD_ERASE_GROUP_END;
202 }
203
204 cmd.cmdidx = start_cmd;
205 cmd.cmdarg = start;
206 cmd.resp_type = MMC_RSP_R1;
Lei Wenea526762011-06-22 17:03:31 +0000207
208 err = mmc_send_cmd(mmc, &cmd, NULL);
209 if (err)
210 goto err_out;
211
212 cmd.cmdidx = end_cmd;
213 cmd.cmdarg = end;
214
215 err = mmc_send_cmd(mmc, &cmd, NULL);
216 if (err)
217 goto err_out;
218
219 cmd.cmdidx = MMC_CMD_ERASE;
220 cmd.cmdarg = SECURE_ERASE;
221 cmd.resp_type = MMC_RSP_R1b;
222
223 err = mmc_send_cmd(mmc, &cmd, NULL);
224 if (err)
225 goto err_out;
226
227 return 0;
228
229err_out:
230 puts("mmc erase failed\n");
231 return err;
232}
233
234static unsigned long
235mmc_berase(int dev_num, unsigned long start, lbaint_t blkcnt)
236{
237 int err = 0;
238 struct mmc *mmc = find_mmc_device(dev_num);
239 lbaint_t blk = 0, blk_r = 0;
Jerry Huang15c9ad92012-05-17 23:00:51 +0000240 int timeout = 1000;
Lei Wenea526762011-06-22 17:03:31 +0000241
242 if (!mmc)
243 return -1;
244
245 if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size))
246 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
247 "The erase range would be change to 0x%lx~0x%lx\n\n",
248 mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
249 ((start + blkcnt + mmc->erase_grp_size)
250 & ~(mmc->erase_grp_size - 1)) - 1);
251
252 while (blk < blkcnt) {
253 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
254 mmc->erase_grp_size : (blkcnt - blk);
255 err = mmc_erase_t(mmc, start + blk, blk_r);
256 if (err)
257 break;
258
259 blk += blk_r;
Jerry Huang15c9ad92012-05-17 23:00:51 +0000260
261 /* Waiting for the ready status */
262 if (mmc_send_status(mmc, timeout))
263 return 0;
Lei Wenea526762011-06-22 17:03:31 +0000264 }
265
266 return blk;
267}
268
Andy Flemingad347bb2008-10-30 16:41:01 -0500269static ulong
Lei Wen6b405b72010-10-14 13:38:11 +0800270mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
Andy Flemingad347bb2008-10-30 16:41:01 -0500271{
272 struct mmc_cmd cmd;
273 struct mmc_data data;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000274 int timeout = 1000;
Andy Flemingad347bb2008-10-30 16:41:01 -0500275
Lei Wene1cc9c82010-09-13 22:07:27 +0800276 if ((start + blkcnt) > mmc->block_dev.lba) {
Steve Sakomaneb288862010-10-28 09:00:26 -0700277 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
Lei Wene1cc9c82010-09-13 22:07:27 +0800278 start + blkcnt, mmc->block_dev.lba);
279 return 0;
280 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500281
282 if (blkcnt > 1)
283 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
284 else
285 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
286
287 if (mmc->high_capacity)
288 cmd.cmdarg = start;
289 else
Steve Sakomaneb288862010-10-28 09:00:26 -0700290 cmd.cmdarg = start * mmc->write_bl_len;
Andy Flemingad347bb2008-10-30 16:41:01 -0500291
292 cmd.resp_type = MMC_RSP_R1;
Andy Flemingad347bb2008-10-30 16:41:01 -0500293
294 data.src = src;
295 data.blocks = blkcnt;
Steve Sakomaneb288862010-10-28 09:00:26 -0700296 data.blocksize = mmc->write_bl_len;
Andy Flemingad347bb2008-10-30 16:41:01 -0500297 data.flags = MMC_DATA_WRITE;
298
Steve Sakomaneb288862010-10-28 09:00:26 -0700299 if (mmc_send_cmd(mmc, &cmd, &data)) {
300 printf("mmc write failed\n");
301 return 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500302 }
303
Thomas Chou1254c3d2010-12-24 13:12:21 +0000304 /* SPI multiblock writes terminate using a special
305 * token, not a STOP_TRANSMISSION request.
306 */
307 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
Andy Flemingad347bb2008-10-30 16:41:01 -0500308 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
309 cmd.cmdarg = 0;
310 cmd.resp_type = MMC_RSP_R1b;
Steve Sakomaneb288862010-10-28 09:00:26 -0700311 if (mmc_send_cmd(mmc, &cmd, NULL)) {
312 printf("mmc fail to send stop cmd\n");
313 return 0;
Lei Wen6b405b72010-10-14 13:38:11 +0800314 }
315 }
316
Jan Kloetzke4e929dd2012-02-05 22:29:11 +0000317 /* Waiting for the ready status */
318 if (mmc_send_status(mmc, timeout))
319 return 0;
320
Lei Wen6b405b72010-10-14 13:38:11 +0800321 return blkcnt;
322}
323
324static ulong
325mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
326{
Lei Wen6b405b72010-10-14 13:38:11 +0800327 lbaint_t cur, blocks_todo = blkcnt;
328
Steve Sakomaneb288862010-10-28 09:00:26 -0700329 struct mmc *mmc = find_mmc_device(dev_num);
Lei Wen6b405b72010-10-14 13:38:11 +0800330 if (!mmc)
Steve Sakomaneb288862010-10-28 09:00:26 -0700331 return 0;
Lei Wen6b405b72010-10-14 13:38:11 +0800332
Steve Sakomaneb288862010-10-28 09:00:26 -0700333 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
334 return 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500335
Lei Wen6b405b72010-10-14 13:38:11 +0800336 do {
John Rigbyf2f43662011-04-18 05:50:08 +0000337 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
Lei Wen6b405b72010-10-14 13:38:11 +0800338 if(mmc_write_blocks(mmc, start, cur, src) != cur)
Steve Sakomaneb288862010-10-28 09:00:26 -0700339 return 0;
Lei Wen6b405b72010-10-14 13:38:11 +0800340 blocks_todo -= cur;
341 start += cur;
342 src += cur * mmc->write_bl_len;
343 } while (blocks_todo > 0);
344
Andy Flemingad347bb2008-10-30 16:41:01 -0500345 return blkcnt;
346}
347
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700348int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt)
Andy Flemingad347bb2008-10-30 16:41:01 -0500349{
350 struct mmc_cmd cmd;
351 struct mmc_data data;
352
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700353 if (blkcnt > 1)
354 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
355 else
356 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Flemingad347bb2008-10-30 16:41:01 -0500357
358 if (mmc->high_capacity)
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700359 cmd.cmdarg = start;
Andy Flemingad347bb2008-10-30 16:41:01 -0500360 else
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700361 cmd.cmdarg = start * mmc->read_bl_len;
Andy Flemingad347bb2008-10-30 16:41:01 -0500362
363 cmd.resp_type = MMC_RSP_R1;
Andy Flemingad347bb2008-10-30 16:41:01 -0500364
365 data.dest = dst;
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700366 data.blocks = blkcnt;
Andy Flemingad347bb2008-10-30 16:41:01 -0500367 data.blocksize = mmc->read_bl_len;
368 data.flags = MMC_DATA_READ;
369
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700370 if (mmc_send_cmd(mmc, &cmd, &data))
371 return 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500372
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700373 if (blkcnt > 1) {
374 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
375 cmd.cmdarg = 0;
376 cmd.resp_type = MMC_RSP_R1b;
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700377 if (mmc_send_cmd(mmc, &cmd, NULL)) {
378 printf("mmc fail to send stop cmd\n");
379 return 0;
380 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500381 }
382
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700383 return blkcnt;
Andy Flemingad347bb2008-10-30 16:41:01 -0500384}
385
386static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
387{
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700388 lbaint_t cur, blocks_todo = blkcnt;
389
390 if (blkcnt == 0)
391 return 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500392
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700393 struct mmc *mmc = find_mmc_device(dev_num);
Andy Flemingad347bb2008-10-30 16:41:01 -0500394 if (!mmc)
395 return 0;
396
Lei Wene1cc9c82010-09-13 22:07:27 +0800397 if ((start + blkcnt) > mmc->block_dev.lba) {
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700398 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
Lei Wene1cc9c82010-09-13 22:07:27 +0800399 start + blkcnt, mmc->block_dev.lba);
400 return 0;
401 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500402
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700403 if (mmc_set_blocklen(mmc, mmc->read_bl_len))
Andy Flemingad347bb2008-10-30 16:41:01 -0500404 return 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500405
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700406 do {
John Rigbyf2f43662011-04-18 05:50:08 +0000407 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700408 if(mmc_read_blocks(mmc, dst, start, cur) != cur)
409 return 0;
410 blocks_todo -= cur;
411 start += cur;
412 dst += cur * mmc->read_bl_len;
413 } while (blocks_todo > 0);
Andy Flemingad347bb2008-10-30 16:41:01 -0500414
415 return blkcnt;
416}
417
418int mmc_go_idle(struct mmc* mmc)
419{
420 struct mmc_cmd cmd;
421 int err;
422
423 udelay(1000);
424
425 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
426 cmd.cmdarg = 0;
427 cmd.resp_type = MMC_RSP_NONE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500428
429 err = mmc_send_cmd(mmc, &cmd, NULL);
430
431 if (err)
432 return err;
433
434 udelay(2000);
435
436 return 0;
437}
438
439int
440sd_send_op_cond(struct mmc *mmc)
441{
442 int timeout = 1000;
443 int err;
444 struct mmc_cmd cmd;
445
446 do {
447 cmd.cmdidx = MMC_CMD_APP_CMD;
448 cmd.resp_type = MMC_RSP_R1;
449 cmd.cmdarg = 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500450
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 Babicf8e9a212010-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 Chou1254c3d2010-12-24 13:12:21 +0000466 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
467 (mmc->voltages & 0xff8000);
Andy Flemingad347bb2008-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 Chou1254c3d2010-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;
Thomas Chou1254c3d2010-12-24 13:12:21 +0000490
491 err = mmc_send_cmd(mmc, &cmd, NULL);
492
493 if (err)
494 return err;
495 }
496
Rabin Vincentb6eed942009-04-05 13:30:56 +0530497 mmc->ocr = cmd.response[0];
Andy Flemingad347bb2008-10-30 16:41:01 -0500498
499 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
500 mmc->rca = 0;
501
502 return 0;
503}
504
505int mmc_send_op_cond(struct mmc *mmc)
506{
Raffaele Recalcati1df837e2011-03-11 02:01:13 +0000507 int timeout = 10000;
Andy Flemingad347bb2008-10-30 16:41:01 -0500508 struct mmc_cmd cmd;
509 int err;
510
511 /* Some cards seem to need this */
512 mmc_go_idle(mmc);
513
Raffaele Recalcati1df837e2011-03-11 02:01:13 +0000514 /* Asking to the card its capabilities */
515 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
516 cmd.resp_type = MMC_RSP_R3;
517 cmd.cmdarg = 0;
Wolfgang Denk80f70212011-05-19 22:21:41 +0200518
Raffaele Recalcati1df837e2011-03-11 02:01:13 +0000519 err = mmc_send_cmd(mmc, &cmd, NULL);
Wolfgang Denk80f70212011-05-19 22:21:41 +0200520
Raffaele Recalcati1df837e2011-03-11 02:01:13 +0000521 if (err)
522 return err;
Wolfgang Denk80f70212011-05-19 22:21:41 +0200523
Raffaele Recalcati1df837e2011-03-11 02:01:13 +0000524 udelay(1000);
Wolfgang Denk80f70212011-05-19 22:21:41 +0200525
Andy Flemingad347bb2008-10-30 16:41:01 -0500526 do {
527 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
528 cmd.resp_type = MMC_RSP_R3;
Raffaele Recalcati1df837e2011-03-11 02:01:13 +0000529 cmd.cmdarg = (mmc_host_is_spi(mmc) ? 0 :
530 (mmc->voltages &
531 (cmd.response[0] & OCR_VOLTAGE_MASK)) |
532 (cmd.response[0] & OCR_ACCESS_MODE));
Łukasz Majewski237823e2011-07-05 02:19:44 +0000533
534 if (mmc->host_caps & MMC_MODE_HC)
535 cmd.cmdarg |= OCR_HCS;
536
Andy Flemingad347bb2008-10-30 16:41:01 -0500537 err = mmc_send_cmd(mmc, &cmd, NULL);
538
539 if (err)
540 return err;
541
542 udelay(1000);
543 } while (!(cmd.response[0] & OCR_BUSY) && timeout--);
544
545 if (timeout <= 0)
546 return UNUSABLE_ERR;
547
Thomas Chou1254c3d2010-12-24 13:12:21 +0000548 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
549 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
550 cmd.resp_type = MMC_RSP_R3;
551 cmd.cmdarg = 0;
Thomas Chou1254c3d2010-12-24 13:12:21 +0000552
553 err = mmc_send_cmd(mmc, &cmd, NULL);
554
555 if (err)
556 return err;
557 }
558
Andy Flemingad347bb2008-10-30 16:41:01 -0500559 mmc->version = MMC_VERSION_UNKNOWN;
Rabin Vincentb6eed942009-04-05 13:30:56 +0530560 mmc->ocr = cmd.response[0];
Andy Flemingad347bb2008-10-30 16:41:01 -0500561
562 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
563 mmc->rca = 0;
564
565 return 0;
566}
567
568
Yoshihiro Shimodaf6bec732012-06-07 19:09:11 +0000569int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
Andy Flemingad347bb2008-10-30 16:41:01 -0500570{
571 struct mmc_cmd cmd;
572 struct mmc_data data;
573 int err;
574
575 /* Get the Card Status Register */
576 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
577 cmd.resp_type = MMC_RSP_R1;
578 cmd.cmdarg = 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500579
Yoshihiro Shimodaf6bec732012-06-07 19:09:11 +0000580 data.dest = (char *)ext_csd;
Andy Flemingad347bb2008-10-30 16:41:01 -0500581 data.blocks = 1;
582 data.blocksize = 512;
583 data.flags = MMC_DATA_READ;
584
585 err = mmc_send_cmd(mmc, &cmd, &data);
586
587 return err;
588}
589
590
591int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
592{
593 struct mmc_cmd cmd;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000594 int timeout = 1000;
595 int ret;
Andy Flemingad347bb2008-10-30 16:41:01 -0500596
597 cmd.cmdidx = MMC_CMD_SWITCH;
598 cmd.resp_type = MMC_RSP_R1b;
599 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000600 (index << 16) |
601 (value << 8);
Andy Flemingad347bb2008-10-30 16:41:01 -0500602
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000603 ret = mmc_send_cmd(mmc, &cmd, NULL);
604
605 /* Waiting for the ready status */
Jan Kloetzke4e929dd2012-02-05 22:29:11 +0000606 if (!ret)
607 ret = mmc_send_status(mmc, timeout);
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000608
609 return ret;
610
Andy Flemingad347bb2008-10-30 16:41:01 -0500611}
612
613int mmc_change_freq(struct mmc *mmc)
614{
Yoshihiro Shimodaf6bec732012-06-07 19:09:11 +0000615 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, 512);
Andy Flemingad347bb2008-10-30 16:41:01 -0500616 char cardtype;
617 int err;
618
619 mmc->card_caps = 0;
620
Thomas Chou1254c3d2010-12-24 13:12:21 +0000621 if (mmc_host_is_spi(mmc))
622 return 0;
623
Andy Flemingad347bb2008-10-30 16:41:01 -0500624 /* Only version 4 supports high-speed */
625 if (mmc->version < MMC_VERSION_4)
626 return 0;
627
Andy Flemingad347bb2008-10-30 16:41:01 -0500628 err = mmc_send_ext_csd(mmc, ext_csd);
629
630 if (err)
631 return err;
632
Lei Wen217467f2011-10-03 20:35:10 +0000633 cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
Andy Flemingad347bb2008-10-30 16:41:01 -0500634
635 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
636
637 if (err)
638 return err;
639
640 /* Now check to see that it worked */
641 err = mmc_send_ext_csd(mmc, ext_csd);
642
643 if (err)
644 return err;
645
646 /* No high-speed support */
Lei Wen217467f2011-10-03 20:35:10 +0000647 if (!ext_csd[EXT_CSD_HS_TIMING])
Andy Flemingad347bb2008-10-30 16:41:01 -0500648 return 0;
649
650 /* High Speed is set, there are two types: 52MHz and 26MHz */
651 if (cardtype & MMC_HS_52MHZ)
652 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
653 else
654 mmc->card_caps |= MMC_MODE_HS;
655
656 return 0;
657}
658
Lei Wen31b99802011-05-02 16:26:26 +0000659int mmc_switch_part(int dev_num, unsigned int part_num)
660{
661 struct mmc *mmc = find_mmc_device(dev_num);
662
663 if (!mmc)
664 return -1;
665
666 return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
667 (mmc->part_config & ~PART_ACCESS_MASK)
668 | (part_num & PART_ACCESS_MASK));
669}
670
Thierry Redingb9c8b772012-01-02 01:15:37 +0000671int mmc_getcd(struct mmc *mmc)
672{
673 int cd;
674
675 cd = board_mmc_getcd(mmc);
676
677 if ((cd < 0) && mmc->getcd)
678 cd = mmc->getcd(mmc);
679
680 return cd;
681}
682
Andy Flemingad347bb2008-10-30 16:41:01 -0500683int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
684{
685 struct mmc_cmd cmd;
686 struct mmc_data data;
687
688 /* Switch the frequency */
689 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
690 cmd.resp_type = MMC_RSP_R1;
691 cmd.cmdarg = (mode << 31) | 0xffffff;
692 cmd.cmdarg &= ~(0xf << (group * 4));
693 cmd.cmdarg |= value << (group * 4);
Andy Flemingad347bb2008-10-30 16:41:01 -0500694
695 data.dest = (char *)resp;
696 data.blocksize = 64;
697 data.blocks = 1;
698 data.flags = MMC_DATA_READ;
699
700 return mmc_send_cmd(mmc, &cmd, &data);
701}
702
703
704int sd_change_freq(struct mmc *mmc)
705{
706 int err;
707 struct mmc_cmd cmd;
Anton staaf9b00f0d2011-10-03 13:54:59 +0000708 ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
709 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
Andy Flemingad347bb2008-10-30 16:41:01 -0500710 struct mmc_data data;
711 int timeout;
712
713 mmc->card_caps = 0;
714
Thomas Chou1254c3d2010-12-24 13:12:21 +0000715 if (mmc_host_is_spi(mmc))
716 return 0;
717
Andy Flemingad347bb2008-10-30 16:41:01 -0500718 /* Read the SCR to find out if this card supports higher speeds */
719 cmd.cmdidx = MMC_CMD_APP_CMD;
720 cmd.resp_type = MMC_RSP_R1;
721 cmd.cmdarg = mmc->rca << 16;
Andy Flemingad347bb2008-10-30 16:41:01 -0500722
723 err = mmc_send_cmd(mmc, &cmd, NULL);
724
725 if (err)
726 return err;
727
728 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
729 cmd.resp_type = MMC_RSP_R1;
730 cmd.cmdarg = 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500731
732 timeout = 3;
733
734retry_scr:
Anton staaf9b00f0d2011-10-03 13:54:59 +0000735 data.dest = (char *)scr;
Andy Flemingad347bb2008-10-30 16:41:01 -0500736 data.blocksize = 8;
737 data.blocks = 1;
738 data.flags = MMC_DATA_READ;
739
740 err = mmc_send_cmd(mmc, &cmd, &data);
741
742 if (err) {
743 if (timeout--)
744 goto retry_scr;
745
746 return err;
747 }
748
Yauhen Kharuzhy6e8edf42009-05-07 00:43:30 +0300749 mmc->scr[0] = __be32_to_cpu(scr[0]);
750 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Flemingad347bb2008-10-30 16:41:01 -0500751
752 switch ((mmc->scr[0] >> 24) & 0xf) {
753 case 0:
754 mmc->version = SD_VERSION_1_0;
755 break;
756 case 1:
757 mmc->version = SD_VERSION_1_10;
758 break;
759 case 2:
760 mmc->version = SD_VERSION_2;
761 break;
762 default:
763 mmc->version = SD_VERSION_1_0;
764 break;
765 }
766
Alagu Sankar24bb5ab2010-05-12 15:08:24 +0530767 if (mmc->scr[0] & SD_DATA_4BIT)
768 mmc->card_caps |= MMC_MODE_4BIT;
769
Andy Flemingad347bb2008-10-30 16:41:01 -0500770 /* Version 1.0 doesn't support switching */
771 if (mmc->version == SD_VERSION_1_0)
772 return 0;
773
774 timeout = 4;
775 while (timeout--) {
776 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
Anton staaf9b00f0d2011-10-03 13:54:59 +0000777 (u8 *)switch_status);
Andy Flemingad347bb2008-10-30 16:41:01 -0500778
779 if (err)
780 return err;
781
782 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy6e8edf42009-05-07 00:43:30 +0300783 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Flemingad347bb2008-10-30 16:41:01 -0500784 break;
785 }
786
Andy Flemingad347bb2008-10-30 16:41:01 -0500787 /* If high-speed isn't supported, we return */
Yauhen Kharuzhy6e8edf42009-05-07 00:43:30 +0300788 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
Andy Flemingad347bb2008-10-30 16:41:01 -0500789 return 0;
790
Macpaul Lin24e92ec2011-11-28 16:31:09 +0000791 /*
792 * If the host doesn't support SD_HIGHSPEED, do not switch card to
793 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
794 * This can avoid furthur problem when the card runs in different
795 * mode between the host.
796 */
797 if (!((mmc->host_caps & MMC_MODE_HS_52MHz) &&
798 (mmc->host_caps & MMC_MODE_HS)))
799 return 0;
800
Anton staaf9b00f0d2011-10-03 13:54:59 +0000801 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
Andy Flemingad347bb2008-10-30 16:41:01 -0500802
803 if (err)
804 return err;
805
Yauhen Kharuzhy6e8edf42009-05-07 00:43:30 +0300806 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
Andy Flemingad347bb2008-10-30 16:41:01 -0500807 mmc->card_caps |= MMC_MODE_HS;
808
809 return 0;
810}
811
812/* frequency bases */
813/* divided by 10 to be nice to platforms without floating point */
Mike Frysingerb588caf2010-10-20 01:15:53 +0000814static const int fbase[] = {
Andy Flemingad347bb2008-10-30 16:41:01 -0500815 10000,
816 100000,
817 1000000,
818 10000000,
819};
820
821/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
822 * to platforms without floating point.
823 */
Mike Frysingerb588caf2010-10-20 01:15:53 +0000824static const int multipliers[] = {
Andy Flemingad347bb2008-10-30 16:41:01 -0500825 0, /* reserved */
826 10,
827 12,
828 13,
829 15,
830 20,
831 25,
832 30,
833 35,
834 40,
835 45,
836 50,
837 55,
838 60,
839 70,
840 80,
841};
842
843void mmc_set_ios(struct mmc *mmc)
844{
845 mmc->set_ios(mmc);
846}
847
848void mmc_set_clock(struct mmc *mmc, uint clock)
849{
850 if (clock > mmc->f_max)
851 clock = mmc->f_max;
852
853 if (clock < mmc->f_min)
854 clock = mmc->f_min;
855
856 mmc->clock = clock;
857
858 mmc_set_ios(mmc);
859}
860
861void mmc_set_bus_width(struct mmc *mmc, uint width)
862{
863 mmc->bus_width = width;
864
865 mmc_set_ios(mmc);
866}
867
868int mmc_startup(struct mmc *mmc)
869{
Lei Wen4f5a6a52011-10-03 20:35:11 +0000870 int err, width;
Andy Flemingad347bb2008-10-30 16:41:01 -0500871 uint mult, freq;
Yoshihiro Shimoda7d88de52011-07-04 22:13:26 +0000872 u64 cmult, csize, capacity;
Andy Flemingad347bb2008-10-30 16:41:01 -0500873 struct mmc_cmd cmd;
Yoshihiro Shimodaf6bec732012-06-07 19:09:11 +0000874 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, 512);
875 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, 512);
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000876 int timeout = 1000;
Andy Flemingad347bb2008-10-30 16:41:01 -0500877
Thomas Chou1254c3d2010-12-24 13:12:21 +0000878#ifdef CONFIG_MMC_SPI_CRC_ON
879 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
880 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
881 cmd.resp_type = MMC_RSP_R1;
882 cmd.cmdarg = 1;
Thomas Chou1254c3d2010-12-24 13:12:21 +0000883 err = mmc_send_cmd(mmc, &cmd, NULL);
884
885 if (err)
886 return err;
887 }
888#endif
889
Andy Flemingad347bb2008-10-30 16:41:01 -0500890 /* Put the Card in Identify Mode */
Thomas Chou1254c3d2010-12-24 13:12:21 +0000891 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
892 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
Andy Flemingad347bb2008-10-30 16:41:01 -0500893 cmd.resp_type = MMC_RSP_R2;
894 cmd.cmdarg = 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500895
896 err = mmc_send_cmd(mmc, &cmd, NULL);
897
898 if (err)
899 return err;
900
901 memcpy(mmc->cid, cmd.response, 16);
902
903 /*
904 * For MMC cards, set the Relative Address.
905 * For SD cards, get the Relatvie Address.
906 * This also puts the cards into Standby State
907 */
Thomas Chou1254c3d2010-12-24 13:12:21 +0000908 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
909 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
910 cmd.cmdarg = mmc->rca << 16;
911 cmd.resp_type = MMC_RSP_R6;
Andy Flemingad347bb2008-10-30 16:41:01 -0500912
Thomas Chou1254c3d2010-12-24 13:12:21 +0000913 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Flemingad347bb2008-10-30 16:41:01 -0500914
Thomas Chou1254c3d2010-12-24 13:12:21 +0000915 if (err)
916 return err;
Andy Flemingad347bb2008-10-30 16:41:01 -0500917
Thomas Chou1254c3d2010-12-24 13:12:21 +0000918 if (IS_SD(mmc))
919 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
920 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500921
922 /* Get the Card-Specific Data */
923 cmd.cmdidx = MMC_CMD_SEND_CSD;
924 cmd.resp_type = MMC_RSP_R2;
925 cmd.cmdarg = mmc->rca << 16;
Andy Flemingad347bb2008-10-30 16:41:01 -0500926
927 err = mmc_send_cmd(mmc, &cmd, NULL);
928
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000929 /* Waiting for the ready status */
930 mmc_send_status(mmc, timeout);
931
Andy Flemingad347bb2008-10-30 16:41:01 -0500932 if (err)
933 return err;
934
Rabin Vincentb6eed942009-04-05 13:30:56 +0530935 mmc->csd[0] = cmd.response[0];
936 mmc->csd[1] = cmd.response[1];
937 mmc->csd[2] = cmd.response[2];
938 mmc->csd[3] = cmd.response[3];
Andy Flemingad347bb2008-10-30 16:41:01 -0500939
940 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincentbdf7a682009-04-05 13:30:55 +0530941 int version = (cmd.response[0] >> 26) & 0xf;
Andy Flemingad347bb2008-10-30 16:41:01 -0500942
943 switch (version) {
944 case 0:
945 mmc->version = MMC_VERSION_1_2;
946 break;
947 case 1:
948 mmc->version = MMC_VERSION_1_4;
949 break;
950 case 2:
951 mmc->version = MMC_VERSION_2_2;
952 break;
953 case 3:
954 mmc->version = MMC_VERSION_3;
955 break;
956 case 4:
957 mmc->version = MMC_VERSION_4;
958 break;
959 default:
960 mmc->version = MMC_VERSION_1_2;
961 break;
962 }
963 }
964
965 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincentbdf7a682009-04-05 13:30:55 +0530966 freq = fbase[(cmd.response[0] & 0x7)];
967 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Flemingad347bb2008-10-30 16:41:01 -0500968
969 mmc->tran_speed = freq * mult;
970
Rabin Vincentb6eed942009-04-05 13:30:56 +0530971 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Andy Flemingad347bb2008-10-30 16:41:01 -0500972
973 if (IS_SD(mmc))
974 mmc->write_bl_len = mmc->read_bl_len;
975 else
Rabin Vincentb6eed942009-04-05 13:30:56 +0530976 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Andy Flemingad347bb2008-10-30 16:41:01 -0500977
978 if (mmc->high_capacity) {
979 csize = (mmc->csd[1] & 0x3f) << 16
980 | (mmc->csd[2] & 0xffff0000) >> 16;
981 cmult = 8;
982 } else {
983 csize = (mmc->csd[1] & 0x3ff) << 2
984 | (mmc->csd[2] & 0xc0000000) >> 30;
985 cmult = (mmc->csd[2] & 0x00038000) >> 15;
986 }
987
988 mmc->capacity = (csize + 1) << (cmult + 2);
989 mmc->capacity *= mmc->read_bl_len;
990
991 if (mmc->read_bl_len > 512)
992 mmc->read_bl_len = 512;
993
994 if (mmc->write_bl_len > 512)
995 mmc->write_bl_len = 512;
996
997 /* Select the card, and put it into Transfer Mode */
Thomas Chou1254c3d2010-12-24 13:12:21 +0000998 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
999 cmd.cmdidx = MMC_CMD_SELECT_CARD;
Ajay Bhargav4a32fba2011-10-05 03:13:23 +00001000 cmd.resp_type = MMC_RSP_R1;
Thomas Chou1254c3d2010-12-24 13:12:21 +00001001 cmd.cmdarg = mmc->rca << 16;
Thomas Chou1254c3d2010-12-24 13:12:21 +00001002 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Flemingad347bb2008-10-30 16:41:01 -05001003
Thomas Chou1254c3d2010-12-24 13:12:21 +00001004 if (err)
1005 return err;
1006 }
Andy Flemingad347bb2008-10-30 16:41:01 -05001007
Lei Wenea526762011-06-22 17:03:31 +00001008 /*
1009 * For SD, its erase group is always one sector
1010 */
1011 mmc->erase_grp_size = 1;
Lei Wen31b99802011-05-02 16:26:26 +00001012 mmc->part_config = MMCPART_NOAVAILABLE;
Sukumar Ghorai232293c2010-09-20 18:29:29 +05301013 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
1014 /* check ext_csd version and capacity */
1015 err = mmc_send_ext_csd(mmc, ext_csd);
Lei Wen217467f2011-10-03 20:35:10 +00001016 if (!err & (ext_csd[EXT_CSD_REV] >= 2)) {
Yoshihiro Shimoda7d88de52011-07-04 22:13:26 +00001017 /*
1018 * According to the JEDEC Standard, the value of
1019 * ext_csd's capacity is valid if the value is more
1020 * than 2GB
1021 */
Lei Wen217467f2011-10-03 20:35:10 +00001022 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
1023 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
1024 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
1025 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
Yoshihiro Shimoda7d88de52011-07-04 22:13:26 +00001026 capacity *= 512;
Łukasz Majewski237823e2011-07-05 02:19:44 +00001027 if ((capacity >> 20) > 2 * 1024)
Yoshihiro Shimoda7d88de52011-07-04 22:13:26 +00001028 mmc->capacity = capacity;
Sukumar Ghorai232293c2010-09-20 18:29:29 +05301029 }
Lei Wen31b99802011-05-02 16:26:26 +00001030
Lei Wenea526762011-06-22 17:03:31 +00001031 /*
1032 * Check whether GROUP_DEF is set, if yes, read out
1033 * group size from ext_csd directly, or calculate
1034 * the group size from the csd value.
1035 */
Lei Wen217467f2011-10-03 20:35:10 +00001036 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF])
1037 mmc->erase_grp_size =
1038 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 512 * 1024;
Lei Wenea526762011-06-22 17:03:31 +00001039 else {
1040 int erase_gsz, erase_gmul;
1041 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1042 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1043 mmc->erase_grp_size = (erase_gsz + 1)
1044 * (erase_gmul + 1);
1045 }
1046
Lei Wen31b99802011-05-02 16:26:26 +00001047 /* store the partition info of emmc */
Stephen Warren009784c2012-07-30 10:55:43 +00001048 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
1049 ext_csd[EXT_CSD_BOOT_MULT])
Lei Wen217467f2011-10-03 20:35:10 +00001050 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
Sukumar Ghorai232293c2010-09-20 18:29:29 +05301051 }
1052
Andy Flemingad347bb2008-10-30 16:41:01 -05001053 if (IS_SD(mmc))
1054 err = sd_change_freq(mmc);
1055 else
1056 err = mmc_change_freq(mmc);
1057
1058 if (err)
1059 return err;
1060
1061 /* Restrict card's capabilities by what the host can do */
1062 mmc->card_caps &= mmc->host_caps;
1063
1064 if (IS_SD(mmc)) {
1065 if (mmc->card_caps & MMC_MODE_4BIT) {
1066 cmd.cmdidx = MMC_CMD_APP_CMD;
1067 cmd.resp_type = MMC_RSP_R1;
1068 cmd.cmdarg = mmc->rca << 16;
Andy Flemingad347bb2008-10-30 16:41:01 -05001069
1070 err = mmc_send_cmd(mmc, &cmd, NULL);
1071 if (err)
1072 return err;
1073
1074 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1075 cmd.resp_type = MMC_RSP_R1;
1076 cmd.cmdarg = 2;
Andy Flemingad347bb2008-10-30 16:41:01 -05001077 err = mmc_send_cmd(mmc, &cmd, NULL);
1078 if (err)
1079 return err;
1080
1081 mmc_set_bus_width(mmc, 4);
1082 }
1083
1084 if (mmc->card_caps & MMC_MODE_HS)
Jaehoon Chunge1d4c7b2012-03-26 21:16:03 +00001085 mmc->tran_speed = 50000000;
Andy Flemingad347bb2008-10-30 16:41:01 -05001086 else
Jaehoon Chunge1d4c7b2012-03-26 21:16:03 +00001087 mmc->tran_speed = 25000000;
Andy Flemingad347bb2008-10-30 16:41:01 -05001088 } else {
Łukasz Majewskib6fe0dc2012-03-12 22:07:18 +00001089 width = ((mmc->host_caps & MMC_MODE_MASK_WIDTH_BITS) >>
1090 MMC_MODE_WIDTH_BITS_SHIFT);
1091 for (; width >= 0; width--) {
Andy Flemingad347bb2008-10-30 16:41:01 -05001092 /* Set the card to use 4 bit*/
1093 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
Lei Wen4f5a6a52011-10-03 20:35:11 +00001094 EXT_CSD_BUS_WIDTH, width);
Andy Flemingad347bb2008-10-30 16:41:01 -05001095
1096 if (err)
Lei Wen4f5a6a52011-10-03 20:35:11 +00001097 continue;
Andy Flemingad347bb2008-10-30 16:41:01 -05001098
Lei Wen4f5a6a52011-10-03 20:35:11 +00001099 if (!width) {
1100 mmc_set_bus_width(mmc, 1);
1101 break;
1102 } else
1103 mmc_set_bus_width(mmc, 4 * width);
Andy Flemingad347bb2008-10-30 16:41:01 -05001104
Lei Wen4f5a6a52011-10-03 20:35:11 +00001105 err = mmc_send_ext_csd(mmc, test_csd);
1106 if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \
1107 == test_csd[EXT_CSD_PARTITIONING_SUPPORT]
1108 && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \
1109 == test_csd[EXT_CSD_ERASE_GROUP_DEF] \
1110 && ext_csd[EXT_CSD_REV] \
1111 == test_csd[EXT_CSD_REV]
1112 && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \
1113 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1114 && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \
1115 &test_csd[EXT_CSD_SEC_CNT], 4) == 0) {
Andy Flemingad347bb2008-10-30 16:41:01 -05001116
Lei Wen4f5a6a52011-10-03 20:35:11 +00001117 mmc->card_caps |= width;
1118 break;
1119 }
Andy Flemingad347bb2008-10-30 16:41:01 -05001120 }
1121
1122 if (mmc->card_caps & MMC_MODE_HS) {
1123 if (mmc->card_caps & MMC_MODE_HS_52MHz)
Jaehoon Chunge1d4c7b2012-03-26 21:16:03 +00001124 mmc->tran_speed = 52000000;
Andy Flemingad347bb2008-10-30 16:41:01 -05001125 else
Jaehoon Chunge1d4c7b2012-03-26 21:16:03 +00001126 mmc->tran_speed = 26000000;
1127 }
Andy Flemingad347bb2008-10-30 16:41:01 -05001128 }
1129
Jaehoon Chunge1d4c7b2012-03-26 21:16:03 +00001130 mmc_set_clock(mmc, mmc->tran_speed);
1131
Andy Flemingad347bb2008-10-30 16:41:01 -05001132 /* fill in device description */
1133 mmc->block_dev.lun = 0;
1134 mmc->block_dev.type = 0;
1135 mmc->block_dev.blksz = mmc->read_bl_len;
Rabin Vincent69d4e2c2009-04-05 13:30:54 +05301136 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
Rabin Vincentbdf7a682009-04-05 13:30:55 +05301137 sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8,
1138 (mmc->cid[2] << 8) | (mmc->cid[3] >> 24));
1139 sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff,
1140 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1141 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
1142 sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28,
1143 (mmc->cid[2] >> 24) & 0xf);
Mikhail Kshevetskiy5cbfa8e7a2012-07-09 08:53:38 +00001144#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
Andy Flemingad347bb2008-10-30 16:41:01 -05001145 init_part(&mmc->block_dev);
Mikhail Kshevetskiy5cbfa8e7a2012-07-09 08:53:38 +00001146#endif
Andy Flemingad347bb2008-10-30 16:41:01 -05001147
1148 return 0;
1149}
1150
1151int mmc_send_if_cond(struct mmc *mmc)
1152{
1153 struct mmc_cmd cmd;
1154 int err;
1155
1156 cmd.cmdidx = SD_CMD_SEND_IF_COND;
1157 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1158 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
1159 cmd.resp_type = MMC_RSP_R7;
Andy Flemingad347bb2008-10-30 16:41:01 -05001160
1161 err = mmc_send_cmd(mmc, &cmd, NULL);
1162
1163 if (err)
1164 return err;
1165
Rabin Vincentb6eed942009-04-05 13:30:56 +05301166 if ((cmd.response[0] & 0xff) != 0xaa)
Andy Flemingad347bb2008-10-30 16:41:01 -05001167 return UNUSABLE_ERR;
1168 else
1169 mmc->version = SD_VERSION_2;
1170
1171 return 0;
1172}
1173
1174int mmc_register(struct mmc *mmc)
1175{
1176 /* Setup the universal parts of the block interface just once */
1177 mmc->block_dev.if_type = IF_TYPE_MMC;
1178 mmc->block_dev.dev = cur_dev_num++;
1179 mmc->block_dev.removable = 1;
1180 mmc->block_dev.block_read = mmc_bread;
1181 mmc->block_dev.block_write = mmc_bwrite;
Lei Wenea526762011-06-22 17:03:31 +00001182 mmc->block_dev.block_erase = mmc_berase;
John Rigbyf2f43662011-04-18 05:50:08 +00001183 if (!mmc->b_max)
1184 mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Andy Flemingad347bb2008-10-30 16:41:01 -05001185
1186 INIT_LIST_HEAD (&mmc->link);
1187
1188 list_add_tail (&mmc->link, &mmc_devices);
1189
1190 return 0;
1191}
1192
Matthew McClintock6252b4f2011-05-24 05:31:19 +00001193#ifdef CONFIG_PARTITIONS
Andy Flemingad347bb2008-10-30 16:41:01 -05001194block_dev_desc_t *mmc_get_dev(int dev)
1195{
1196 struct mmc *mmc = find_mmc_device(dev);
Benoît Thébaudeau6ce8aed2012-08-10 08:59:12 +00001197 if (!mmc || mmc_init(mmc))
Łukasz Majewski65b04cf2012-04-19 02:39:18 +00001198 return NULL;
Andy Flemingad347bb2008-10-30 16:41:01 -05001199
Łukasz Majewski65b04cf2012-04-19 02:39:18 +00001200 return &mmc->block_dev;
Andy Flemingad347bb2008-10-30 16:41:01 -05001201}
Matthew McClintock6252b4f2011-05-24 05:31:19 +00001202#endif
Andy Flemingad347bb2008-10-30 16:41:01 -05001203
1204int mmc_init(struct mmc *mmc)
1205{
Macpaul Lin028bde12011-11-14 23:35:39 +00001206 int err;
Andy Flemingad347bb2008-10-30 16:41:01 -05001207
Thierry Redingb9c8b772012-01-02 01:15:37 +00001208 if (mmc_getcd(mmc) == 0) {
1209 mmc->has_init = 0;
1210 printf("MMC: no card present\n");
1211 return NO_CARD_ERR;
1212 }
1213
Lei Wen31b99802011-05-02 16:26:26 +00001214 if (mmc->has_init)
1215 return 0;
1216
Andy Flemingad347bb2008-10-30 16:41:01 -05001217 err = mmc->init(mmc);
1218
1219 if (err)
1220 return err;
1221
Ilya Yanok8459aab2009-06-29 17:53:16 +04001222 mmc_set_bus_width(mmc, 1);
1223 mmc_set_clock(mmc, 1);
1224
Andy Flemingad347bb2008-10-30 16:41:01 -05001225 /* Reset the Card */
1226 err = mmc_go_idle(mmc);
1227
1228 if (err)
1229 return err;
1230
Lei Wen31b99802011-05-02 16:26:26 +00001231 /* The internal partition reset to user partition(0) at every CMD0*/
1232 mmc->part_num = 0;
1233
Andy Flemingad347bb2008-10-30 16:41:01 -05001234 /* Test for SD version 2 */
Macpaul Lin028bde12011-11-14 23:35:39 +00001235 err = mmc_send_if_cond(mmc);
Andy Flemingad347bb2008-10-30 16:41:01 -05001236
Andy Flemingad347bb2008-10-30 16:41:01 -05001237 /* Now try to get the SD card's operating condition */
1238 err = sd_send_op_cond(mmc);
1239
1240 /* If the command timed out, we check for an MMC card */
1241 if (err == TIMEOUT) {
1242 err = mmc_send_op_cond(mmc);
1243
1244 if (err) {
1245 printf("Card did not respond to voltage select!\n");
1246 return UNUSABLE_ERR;
1247 }
1248 }
1249
Lei Wen31b99802011-05-02 16:26:26 +00001250 err = mmc_startup(mmc);
1251 if (err)
1252 mmc->has_init = 0;
1253 else
1254 mmc->has_init = 1;
1255 return err;
Andy Flemingad347bb2008-10-30 16:41:01 -05001256}
1257
1258/*
1259 * CPU and board-specific MMC initializations. Aliased function
1260 * signals caller to move on
1261 */
1262static int __def_mmc_init(bd_t *bis)
1263{
1264 return -1;
1265}
1266
Peter Tyser21d2cd22009-04-20 11:08:46 -05001267int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1268int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
Andy Flemingad347bb2008-10-30 16:41:01 -05001269
1270void print_mmc_devices(char separator)
1271{
1272 struct mmc *m;
1273 struct list_head *entry;
1274
1275 list_for_each(entry, &mmc_devices) {
1276 m = list_entry(entry, struct mmc, link);
1277
1278 printf("%s: %d", m->name, m->block_dev.dev);
1279
1280 if (entry->next != &mmc_devices)
1281 printf("%c ", separator);
1282 }
1283
1284 printf("\n");
1285}
1286
Lei Wend430d7c2011-05-02 16:26:25 +00001287int get_mmc_num(void)
1288{
1289 return cur_dev_num;
1290}
1291
Andy Flemingad347bb2008-10-30 16:41:01 -05001292int mmc_initialize(bd_t *bis)
1293{
1294 INIT_LIST_HEAD (&mmc_devices);
1295 cur_dev_num = 0;
1296
1297 if (board_mmc_init(bis) < 0)
1298 cpu_mmc_init(bis);
1299
1300 print_mmc_devices(',');
1301
1302 return 0;
1303}