blob: 7b5fdd9f66ed51445c2588b33a1e6c4ad0961b6f [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
Nikita Kiryanov020f2612012-12-03 02:19:46 +000043int __weak board_mmc_getwp(struct mmc *mmc)
44{
45 return -1;
46}
47
48int mmc_getwp(struct mmc *mmc)
49{
50 int wp;
51
52 wp = board_mmc_getwp(mmc);
53
54 if ((wp < 0) && mmc->getwp)
55 wp = mmc->getwp(mmc);
56
57 return wp;
58}
59
Thierry Redingd7aebf42012-01-02 01:15:36 +000060int __board_mmc_getcd(struct mmc *mmc) {
Stefano Babic6e00edf2010-02-05 15:04:43 +010061 return -1;
62}
63
Thierry Redingd7aebf42012-01-02 01:15:36 +000064int board_mmc_getcd(struct mmc *mmc)__attribute__((weak,
Stefano Babic6e00edf2010-02-05 15:04:43 +010065 alias("__board_mmc_getcd")));
66
Kim Phillips87ea3892012-10-29 13:34:43 +000067static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
68 struct mmc_data *data)
Andy Flemingad347bb2008-10-30 16:41:01 -050069{
Marek Vasutdccb6082012-03-15 18:41:35 +000070 struct mmc_data backup;
Raffaele Recalcati894b1e22011-03-11 02:01:14 +000071 int ret;
Marek Vasutdccb6082012-03-15 18:41:35 +000072
73 memset(&backup, 0, sizeof(backup));
74
Marek Vasutdccb6082012-03-15 18:41:35 +000075#ifdef CONFIG_MMC_TRACE
Raffaele Recalcati894b1e22011-03-11 02:01:14 +000076 int i;
77 u8 *ptr;
78
79 printf("CMD_SEND:%d\n", cmd->cmdidx);
80 printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
Raffaele Recalcati894b1e22011-03-11 02:01:14 +000081 ret = mmc->send_cmd(mmc, cmd, data);
82 switch (cmd->resp_type) {
83 case MMC_RSP_NONE:
84 printf("\t\tMMC_RSP_NONE\n");
85 break;
86 case MMC_RSP_R1:
87 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
88 cmd->response[0]);
89 break;
90 case MMC_RSP_R1b:
91 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
92 cmd->response[0]);
93 break;
94 case MMC_RSP_R2:
95 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
96 cmd->response[0]);
97 printf("\t\t \t\t 0x%08X \n",
98 cmd->response[1]);
99 printf("\t\t \t\t 0x%08X \n",
100 cmd->response[2]);
101 printf("\t\t \t\t 0x%08X \n",
102 cmd->response[3]);
103 printf("\n");
104 printf("\t\t\t\t\tDUMPING DATA\n");
105 for (i = 0; i < 4; i++) {
106 int j;
107 printf("\t\t\t\t\t%03d - ", i*4);
Dirk Behmec9cb4a92012-03-08 02:35:34 +0000108 ptr = (u8 *)&cmd->response[i];
Raffaele Recalcati894b1e22011-03-11 02:01:14 +0000109 ptr += 3;
110 for (j = 0; j < 4; j++)
111 printf("%02X ", *ptr--);
112 printf("\n");
113 }
114 break;
115 case MMC_RSP_R3:
116 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
117 cmd->response[0]);
118 break;
119 default:
120 printf("\t\tERROR MMC rsp not supported\n");
121 break;
122 }
Raffaele Recalcati894b1e22011-03-11 02:01:14 +0000123#else
Marek Vasutdccb6082012-03-15 18:41:35 +0000124 ret = mmc->send_cmd(mmc, cmd, data);
Raffaele Recalcati894b1e22011-03-11 02:01:14 +0000125#endif
Marek Vasutdccb6082012-03-15 18:41:35 +0000126 return ret;
Andy Flemingad347bb2008-10-30 16:41:01 -0500127}
128
Kim Phillips87ea3892012-10-29 13:34:43 +0000129static int mmc_send_status(struct mmc *mmc, int timeout)
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000130{
131 struct mmc_cmd cmd;
Jan Kloetzke31789322012-02-05 22:29:12 +0000132 int err, retries = 5;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000133#ifdef CONFIG_MMC_TRACE
134 int status;
135#endif
136
137 cmd.cmdidx = MMC_CMD_SEND_STATUS;
138 cmd.resp_type = MMC_RSP_R1;
Marek Vasutc4427392011-08-10 09:24:48 +0200139 if (!mmc_host_is_spi(mmc))
140 cmd.cmdarg = mmc->rca << 16;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000141
142 do {
143 err = mmc_send_cmd(mmc, &cmd, NULL);
Jan Kloetzke31789322012-02-05 22:29:12 +0000144 if (!err) {
145 if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
146 (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
147 MMC_STATE_PRG)
148 break;
149 else if (cmd.response[0] & MMC_STATUS_MASK) {
150 printf("Status Error: 0x%08X\n",
151 cmd.response[0]);
152 return COMM_ERR;
153 }
154 } else if (--retries < 0)
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000155 return err;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000156
157 udelay(1000);
158
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000159 } while (timeout--);
160
Raffaele Recalcati894b1e22011-03-11 02:01:14 +0000161#ifdef CONFIG_MMC_TRACE
162 status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
163 printf("CURR STATE:%d\n", status);
164#endif
Jongman Heo1be00d92012-06-03 21:32:13 +0000165 if (timeout <= 0) {
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000166 printf("Timeout waiting card ready\n");
167 return TIMEOUT;
168 }
169
170 return 0;
171}
172
Kim Phillips87ea3892012-10-29 13:34:43 +0000173static int mmc_set_blocklen(struct mmc *mmc, int len)
Andy Flemingad347bb2008-10-30 16:41:01 -0500174{
175 struct mmc_cmd cmd;
176
177 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
178 cmd.resp_type = MMC_RSP_R1;
179 cmd.cmdarg = len;
Andy Flemingad347bb2008-10-30 16:41:01 -0500180
181 return mmc_send_cmd(mmc, &cmd, NULL);
182}
183
184struct mmc *find_mmc_device(int dev_num)
185{
186 struct mmc *m;
187 struct list_head *entry;
188
189 list_for_each(entry, &mmc_devices) {
190 m = list_entry(entry, struct mmc, link);
191
192 if (m->block_dev.dev == dev_num)
193 return m;
194 }
195
196 printf("MMC Device %d not found\n", dev_num);
197
198 return NULL;
199}
200
Lei Wenea526762011-06-22 17:03:31 +0000201static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
202{
203 struct mmc_cmd cmd;
204 ulong end;
205 int err, start_cmd, end_cmd;
206
207 if (mmc->high_capacity)
208 end = start + blkcnt - 1;
209 else {
210 end = (start + blkcnt - 1) * mmc->write_bl_len;
211 start *= mmc->write_bl_len;
212 }
213
214 if (IS_SD(mmc)) {
215 start_cmd = SD_CMD_ERASE_WR_BLK_START;
216 end_cmd = SD_CMD_ERASE_WR_BLK_END;
217 } else {
218 start_cmd = MMC_CMD_ERASE_GROUP_START;
219 end_cmd = MMC_CMD_ERASE_GROUP_END;
220 }
221
222 cmd.cmdidx = start_cmd;
223 cmd.cmdarg = start;
224 cmd.resp_type = MMC_RSP_R1;
Lei Wenea526762011-06-22 17:03:31 +0000225
226 err = mmc_send_cmd(mmc, &cmd, NULL);
227 if (err)
228 goto err_out;
229
230 cmd.cmdidx = end_cmd;
231 cmd.cmdarg = end;
232
233 err = mmc_send_cmd(mmc, &cmd, NULL);
234 if (err)
235 goto err_out;
236
237 cmd.cmdidx = MMC_CMD_ERASE;
238 cmd.cmdarg = SECURE_ERASE;
239 cmd.resp_type = MMC_RSP_R1b;
240
241 err = mmc_send_cmd(mmc, &cmd, NULL);
242 if (err)
243 goto err_out;
244
245 return 0;
246
247err_out:
248 puts("mmc erase failed\n");
249 return err;
250}
251
252static unsigned long
253mmc_berase(int dev_num, unsigned long start, lbaint_t blkcnt)
254{
255 int err = 0;
256 struct mmc *mmc = find_mmc_device(dev_num);
257 lbaint_t blk = 0, blk_r = 0;
Jerry Huang15c9ad92012-05-17 23:00:51 +0000258 int timeout = 1000;
Lei Wenea526762011-06-22 17:03:31 +0000259
260 if (!mmc)
261 return -1;
262
263 if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size))
264 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
265 "The erase range would be change to 0x%lx~0x%lx\n\n",
266 mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
267 ((start + blkcnt + mmc->erase_grp_size)
268 & ~(mmc->erase_grp_size - 1)) - 1);
269
270 while (blk < blkcnt) {
271 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
272 mmc->erase_grp_size : (blkcnt - blk);
273 err = mmc_erase_t(mmc, start + blk, blk_r);
274 if (err)
275 break;
276
277 blk += blk_r;
Jerry Huang15c9ad92012-05-17 23:00:51 +0000278
279 /* Waiting for the ready status */
280 if (mmc_send_status(mmc, timeout))
281 return 0;
Lei Wenea526762011-06-22 17:03:31 +0000282 }
283
284 return blk;
285}
286
Andy Flemingad347bb2008-10-30 16:41:01 -0500287static ulong
Lei Wen6b405b72010-10-14 13:38:11 +0800288mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
Andy Flemingad347bb2008-10-30 16:41:01 -0500289{
290 struct mmc_cmd cmd;
291 struct mmc_data data;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000292 int timeout = 1000;
Andy Flemingad347bb2008-10-30 16:41:01 -0500293
Lei Wene1cc9c82010-09-13 22:07:27 +0800294 if ((start + blkcnt) > mmc->block_dev.lba) {
Steve Sakomaneb288862010-10-28 09:00:26 -0700295 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
Lei Wene1cc9c82010-09-13 22:07:27 +0800296 start + blkcnt, mmc->block_dev.lba);
297 return 0;
298 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500299
300 if (blkcnt > 1)
301 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
302 else
303 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
304
305 if (mmc->high_capacity)
306 cmd.cmdarg = start;
307 else
Steve Sakomaneb288862010-10-28 09:00:26 -0700308 cmd.cmdarg = start * mmc->write_bl_len;
Andy Flemingad347bb2008-10-30 16:41:01 -0500309
310 cmd.resp_type = MMC_RSP_R1;
Andy Flemingad347bb2008-10-30 16:41:01 -0500311
312 data.src = src;
313 data.blocks = blkcnt;
Steve Sakomaneb288862010-10-28 09:00:26 -0700314 data.blocksize = mmc->write_bl_len;
Andy Flemingad347bb2008-10-30 16:41:01 -0500315 data.flags = MMC_DATA_WRITE;
316
Steve Sakomaneb288862010-10-28 09:00:26 -0700317 if (mmc_send_cmd(mmc, &cmd, &data)) {
318 printf("mmc write failed\n");
319 return 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500320 }
321
Thomas Chou1254c3d2010-12-24 13:12:21 +0000322 /* SPI multiblock writes terminate using a special
323 * token, not a STOP_TRANSMISSION request.
324 */
325 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
Andy Flemingad347bb2008-10-30 16:41:01 -0500326 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
327 cmd.cmdarg = 0;
328 cmd.resp_type = MMC_RSP_R1b;
Steve Sakomaneb288862010-10-28 09:00:26 -0700329 if (mmc_send_cmd(mmc, &cmd, NULL)) {
330 printf("mmc fail to send stop cmd\n");
331 return 0;
Lei Wen6b405b72010-10-14 13:38:11 +0800332 }
333 }
334
Jan Kloetzke4e929dd2012-02-05 22:29:11 +0000335 /* Waiting for the ready status */
336 if (mmc_send_status(mmc, timeout))
337 return 0;
338
Lei Wen6b405b72010-10-14 13:38:11 +0800339 return blkcnt;
340}
341
342static ulong
343mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
344{
Lei Wen6b405b72010-10-14 13:38:11 +0800345 lbaint_t cur, blocks_todo = blkcnt;
346
Steve Sakomaneb288862010-10-28 09:00:26 -0700347 struct mmc *mmc = find_mmc_device(dev_num);
Lei Wen6b405b72010-10-14 13:38:11 +0800348 if (!mmc)
Steve Sakomaneb288862010-10-28 09:00:26 -0700349 return 0;
Lei Wen6b405b72010-10-14 13:38:11 +0800350
Steve Sakomaneb288862010-10-28 09:00:26 -0700351 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
352 return 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500353
Lei Wen6b405b72010-10-14 13:38:11 +0800354 do {
John Rigbyf2f43662011-04-18 05:50:08 +0000355 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
Lei Wen6b405b72010-10-14 13:38:11 +0800356 if(mmc_write_blocks(mmc, start, cur, src) != cur)
Steve Sakomaneb288862010-10-28 09:00:26 -0700357 return 0;
Lei Wen6b405b72010-10-14 13:38:11 +0800358 blocks_todo -= cur;
359 start += cur;
360 src += cur * mmc->write_bl_len;
361 } while (blocks_todo > 0);
362
Andy Flemingad347bb2008-10-30 16:41:01 -0500363 return blkcnt;
364}
365
Kim Phillips87ea3892012-10-29 13:34:43 +0000366static int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start,
367 lbaint_t blkcnt)
Andy Flemingad347bb2008-10-30 16:41:01 -0500368{
369 struct mmc_cmd cmd;
370 struct mmc_data data;
371
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700372 if (blkcnt > 1)
373 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
374 else
375 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Flemingad347bb2008-10-30 16:41:01 -0500376
377 if (mmc->high_capacity)
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700378 cmd.cmdarg = start;
Andy Flemingad347bb2008-10-30 16:41:01 -0500379 else
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700380 cmd.cmdarg = start * mmc->read_bl_len;
Andy Flemingad347bb2008-10-30 16:41:01 -0500381
382 cmd.resp_type = MMC_RSP_R1;
Andy Flemingad347bb2008-10-30 16:41:01 -0500383
384 data.dest = dst;
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700385 data.blocks = blkcnt;
Andy Flemingad347bb2008-10-30 16:41:01 -0500386 data.blocksize = mmc->read_bl_len;
387 data.flags = MMC_DATA_READ;
388
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700389 if (mmc_send_cmd(mmc, &cmd, &data))
390 return 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500391
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700392 if (blkcnt > 1) {
393 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
394 cmd.cmdarg = 0;
395 cmd.resp_type = MMC_RSP_R1b;
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700396 if (mmc_send_cmd(mmc, &cmd, NULL)) {
397 printf("mmc fail to send stop cmd\n");
398 return 0;
399 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500400 }
401
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700402 return blkcnt;
Andy Flemingad347bb2008-10-30 16:41:01 -0500403}
404
405static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
406{
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700407 lbaint_t cur, blocks_todo = blkcnt;
408
409 if (blkcnt == 0)
410 return 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500411
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700412 struct mmc *mmc = find_mmc_device(dev_num);
Andy Flemingad347bb2008-10-30 16:41:01 -0500413 if (!mmc)
414 return 0;
415
Lei Wene1cc9c82010-09-13 22:07:27 +0800416 if ((start + blkcnt) > mmc->block_dev.lba) {
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700417 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
Lei Wene1cc9c82010-09-13 22:07:27 +0800418 start + blkcnt, mmc->block_dev.lba);
419 return 0;
420 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500421
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700422 if (mmc_set_blocklen(mmc, mmc->read_bl_len))
Andy Flemingad347bb2008-10-30 16:41:01 -0500423 return 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500424
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700425 do {
John Rigbyf2f43662011-04-18 05:50:08 +0000426 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700427 if(mmc_read_blocks(mmc, dst, start, cur) != cur)
428 return 0;
429 blocks_todo -= cur;
430 start += cur;
431 dst += cur * mmc->read_bl_len;
432 } while (blocks_todo > 0);
Andy Flemingad347bb2008-10-30 16:41:01 -0500433
434 return blkcnt;
435}
436
Kim Phillips87ea3892012-10-29 13:34:43 +0000437static int mmc_go_idle(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -0500438{
439 struct mmc_cmd cmd;
440 int err;
441
442 udelay(1000);
443
444 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
445 cmd.cmdarg = 0;
446 cmd.resp_type = MMC_RSP_NONE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500447
448 err = mmc_send_cmd(mmc, &cmd, NULL);
449
450 if (err)
451 return err;
452
453 udelay(2000);
454
455 return 0;
456}
457
Kim Phillips87ea3892012-10-29 13:34:43 +0000458static int sd_send_op_cond(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -0500459{
460 int timeout = 1000;
461 int err;
462 struct mmc_cmd cmd;
463
464 do {
465 cmd.cmdidx = MMC_CMD_APP_CMD;
466 cmd.resp_type = MMC_RSP_R1;
467 cmd.cmdarg = 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500468
469 err = mmc_send_cmd(mmc, &cmd, NULL);
470
471 if (err)
472 return err;
473
474 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
475 cmd.resp_type = MMC_RSP_R3;
Stefano Babicf8e9a212010-01-20 18:20:39 +0100476
477 /*
478 * Most cards do not answer if some reserved bits
479 * in the ocr are set. However, Some controller
480 * can set bit 7 (reserved for low voltages), but
481 * how to manage low voltages SD card is not yet
482 * specified.
483 */
Thomas Chou1254c3d2010-12-24 13:12:21 +0000484 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
485 (mmc->voltages & 0xff8000);
Andy Flemingad347bb2008-10-30 16:41:01 -0500486
487 if (mmc->version == SD_VERSION_2)
488 cmd.cmdarg |= OCR_HCS;
489
490 err = mmc_send_cmd(mmc, &cmd, NULL);
491
492 if (err)
493 return err;
494
495 udelay(1000);
496 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
497
498 if (timeout <= 0)
499 return UNUSABLE_ERR;
500
501 if (mmc->version != SD_VERSION_2)
502 mmc->version = SD_VERSION_1_0;
503
Thomas Chou1254c3d2010-12-24 13:12:21 +0000504 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
505 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
506 cmd.resp_type = MMC_RSP_R3;
507 cmd.cmdarg = 0;
Thomas Chou1254c3d2010-12-24 13:12:21 +0000508
509 err = mmc_send_cmd(mmc, &cmd, NULL);
510
511 if (err)
512 return err;
513 }
514
Rabin Vincentb6eed942009-04-05 13:30:56 +0530515 mmc->ocr = cmd.response[0];
Andy Flemingad347bb2008-10-30 16:41:01 -0500516
517 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
518 mmc->rca = 0;
519
520 return 0;
521}
522
Kim Phillips87ea3892012-10-29 13:34:43 +0000523static int mmc_send_op_cond(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -0500524{
Raffaele Recalcati1df837e2011-03-11 02:01:13 +0000525 int timeout = 10000;
Andy Flemingad347bb2008-10-30 16:41:01 -0500526 struct mmc_cmd cmd;
527 int err;
528
529 /* Some cards seem to need this */
530 mmc_go_idle(mmc);
531
Raffaele Recalcati1df837e2011-03-11 02:01:13 +0000532 /* Asking to the card its capabilities */
533 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
534 cmd.resp_type = MMC_RSP_R3;
535 cmd.cmdarg = 0;
Wolfgang Denk80f70212011-05-19 22:21:41 +0200536
Raffaele Recalcati1df837e2011-03-11 02:01:13 +0000537 err = mmc_send_cmd(mmc, &cmd, NULL);
Wolfgang Denk80f70212011-05-19 22:21:41 +0200538
Raffaele Recalcati1df837e2011-03-11 02:01:13 +0000539 if (err)
540 return err;
Wolfgang Denk80f70212011-05-19 22:21:41 +0200541
Raffaele Recalcati1df837e2011-03-11 02:01:13 +0000542 udelay(1000);
Wolfgang Denk80f70212011-05-19 22:21:41 +0200543
Andy Flemingad347bb2008-10-30 16:41:01 -0500544 do {
545 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
546 cmd.resp_type = MMC_RSP_R3;
Raffaele Recalcati1df837e2011-03-11 02:01:13 +0000547 cmd.cmdarg = (mmc_host_is_spi(mmc) ? 0 :
548 (mmc->voltages &
549 (cmd.response[0] & OCR_VOLTAGE_MASK)) |
550 (cmd.response[0] & OCR_ACCESS_MODE));
Łukasz Majewski237823e2011-07-05 02:19:44 +0000551
552 if (mmc->host_caps & MMC_MODE_HC)
553 cmd.cmdarg |= OCR_HCS;
554
Andy Flemingad347bb2008-10-30 16:41:01 -0500555 err = mmc_send_cmd(mmc, &cmd, NULL);
556
557 if (err)
558 return err;
559
560 udelay(1000);
561 } while (!(cmd.response[0] & OCR_BUSY) && timeout--);
562
563 if (timeout <= 0)
564 return UNUSABLE_ERR;
565
Thomas Chou1254c3d2010-12-24 13:12:21 +0000566 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
567 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
568 cmd.resp_type = MMC_RSP_R3;
569 cmd.cmdarg = 0;
Thomas Chou1254c3d2010-12-24 13:12:21 +0000570
571 err = mmc_send_cmd(mmc, &cmd, NULL);
572
573 if (err)
574 return err;
575 }
576
Andy Flemingad347bb2008-10-30 16:41:01 -0500577 mmc->version = MMC_VERSION_UNKNOWN;
Rabin Vincentb6eed942009-04-05 13:30:56 +0530578 mmc->ocr = cmd.response[0];
Andy Flemingad347bb2008-10-30 16:41:01 -0500579
580 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
581 mmc->rca = 0;
582
583 return 0;
584}
585
586
Kim Phillips87ea3892012-10-29 13:34:43 +0000587static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
Andy Flemingad347bb2008-10-30 16:41:01 -0500588{
589 struct mmc_cmd cmd;
590 struct mmc_data data;
591 int err;
592
593 /* Get the Card Status Register */
594 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
595 cmd.resp_type = MMC_RSP_R1;
596 cmd.cmdarg = 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500597
Yoshihiro Shimodaf6bec732012-06-07 19:09:11 +0000598 data.dest = (char *)ext_csd;
Andy Flemingad347bb2008-10-30 16:41:01 -0500599 data.blocks = 1;
600 data.blocksize = 512;
601 data.flags = MMC_DATA_READ;
602
603 err = mmc_send_cmd(mmc, &cmd, &data);
604
605 return err;
606}
607
608
Kim Phillips87ea3892012-10-29 13:34:43 +0000609static int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
Andy Flemingad347bb2008-10-30 16:41:01 -0500610{
611 struct mmc_cmd cmd;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000612 int timeout = 1000;
613 int ret;
Andy Flemingad347bb2008-10-30 16:41:01 -0500614
615 cmd.cmdidx = MMC_CMD_SWITCH;
616 cmd.resp_type = MMC_RSP_R1b;
617 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000618 (index << 16) |
619 (value << 8);
Andy Flemingad347bb2008-10-30 16:41:01 -0500620
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000621 ret = mmc_send_cmd(mmc, &cmd, NULL);
622
623 /* Waiting for the ready status */
Jan Kloetzke4e929dd2012-02-05 22:29:11 +0000624 if (!ret)
625 ret = mmc_send_status(mmc, timeout);
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000626
627 return ret;
628
Andy Flemingad347bb2008-10-30 16:41:01 -0500629}
630
Kim Phillips87ea3892012-10-29 13:34:43 +0000631static int mmc_change_freq(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -0500632{
Yoshihiro Shimodaf6bec732012-06-07 19:09:11 +0000633 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, 512);
Andy Flemingad347bb2008-10-30 16:41:01 -0500634 char cardtype;
635 int err;
636
637 mmc->card_caps = 0;
638
Thomas Chou1254c3d2010-12-24 13:12:21 +0000639 if (mmc_host_is_spi(mmc))
640 return 0;
641
Andy Flemingad347bb2008-10-30 16:41:01 -0500642 /* Only version 4 supports high-speed */
643 if (mmc->version < MMC_VERSION_4)
644 return 0;
645
Andy Flemingad347bb2008-10-30 16:41:01 -0500646 err = mmc_send_ext_csd(mmc, ext_csd);
647
648 if (err)
649 return err;
650
Lei Wen217467f2011-10-03 20:35:10 +0000651 cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
Andy Flemingad347bb2008-10-30 16:41:01 -0500652
653 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
654
655 if (err)
656 return err;
657
658 /* Now check to see that it worked */
659 err = mmc_send_ext_csd(mmc, ext_csd);
660
661 if (err)
662 return err;
663
664 /* No high-speed support */
Lei Wen217467f2011-10-03 20:35:10 +0000665 if (!ext_csd[EXT_CSD_HS_TIMING])
Andy Flemingad347bb2008-10-30 16:41:01 -0500666 return 0;
667
668 /* High Speed is set, there are two types: 52MHz and 26MHz */
669 if (cardtype & MMC_HS_52MHZ)
670 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
671 else
672 mmc->card_caps |= MMC_MODE_HS;
673
674 return 0;
675}
676
Lei Wen31b99802011-05-02 16:26:26 +0000677int mmc_switch_part(int dev_num, unsigned int part_num)
678{
679 struct mmc *mmc = find_mmc_device(dev_num);
680
681 if (!mmc)
682 return -1;
683
684 return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
685 (mmc->part_config & ~PART_ACCESS_MASK)
686 | (part_num & PART_ACCESS_MASK));
687}
688
Thierry Redingb9c8b772012-01-02 01:15:37 +0000689int mmc_getcd(struct mmc *mmc)
690{
691 int cd;
692
693 cd = board_mmc_getcd(mmc);
694
695 if ((cd < 0) && mmc->getcd)
696 cd = mmc->getcd(mmc);
697
698 return cd;
699}
700
Kim Phillips87ea3892012-10-29 13:34:43 +0000701static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
Andy Flemingad347bb2008-10-30 16:41:01 -0500702{
703 struct mmc_cmd cmd;
704 struct mmc_data data;
705
706 /* Switch the frequency */
707 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
708 cmd.resp_type = MMC_RSP_R1;
709 cmd.cmdarg = (mode << 31) | 0xffffff;
710 cmd.cmdarg &= ~(0xf << (group * 4));
711 cmd.cmdarg |= value << (group * 4);
Andy Flemingad347bb2008-10-30 16:41:01 -0500712
713 data.dest = (char *)resp;
714 data.blocksize = 64;
715 data.blocks = 1;
716 data.flags = MMC_DATA_READ;
717
718 return mmc_send_cmd(mmc, &cmd, &data);
719}
720
721
Kim Phillips87ea3892012-10-29 13:34:43 +0000722static int sd_change_freq(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -0500723{
724 int err;
725 struct mmc_cmd cmd;
Anton staaf9b00f0d2011-10-03 13:54:59 +0000726 ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
727 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
Andy Flemingad347bb2008-10-30 16:41:01 -0500728 struct mmc_data data;
729 int timeout;
730
731 mmc->card_caps = 0;
732
Thomas Chou1254c3d2010-12-24 13:12:21 +0000733 if (mmc_host_is_spi(mmc))
734 return 0;
735
Andy Flemingad347bb2008-10-30 16:41:01 -0500736 /* Read the SCR to find out if this card supports higher speeds */
737 cmd.cmdidx = MMC_CMD_APP_CMD;
738 cmd.resp_type = MMC_RSP_R1;
739 cmd.cmdarg = mmc->rca << 16;
Andy Flemingad347bb2008-10-30 16:41:01 -0500740
741 err = mmc_send_cmd(mmc, &cmd, NULL);
742
743 if (err)
744 return err;
745
746 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
747 cmd.resp_type = MMC_RSP_R1;
748 cmd.cmdarg = 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500749
750 timeout = 3;
751
752retry_scr:
Anton staaf9b00f0d2011-10-03 13:54:59 +0000753 data.dest = (char *)scr;
Andy Flemingad347bb2008-10-30 16:41:01 -0500754 data.blocksize = 8;
755 data.blocks = 1;
756 data.flags = MMC_DATA_READ;
757
758 err = mmc_send_cmd(mmc, &cmd, &data);
759
760 if (err) {
761 if (timeout--)
762 goto retry_scr;
763
764 return err;
765 }
766
Yauhen Kharuzhy6e8edf42009-05-07 00:43:30 +0300767 mmc->scr[0] = __be32_to_cpu(scr[0]);
768 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Flemingad347bb2008-10-30 16:41:01 -0500769
770 switch ((mmc->scr[0] >> 24) & 0xf) {
771 case 0:
772 mmc->version = SD_VERSION_1_0;
773 break;
774 case 1:
775 mmc->version = SD_VERSION_1_10;
776 break;
777 case 2:
778 mmc->version = SD_VERSION_2;
779 break;
780 default:
781 mmc->version = SD_VERSION_1_0;
782 break;
783 }
784
Alagu Sankar24bb5ab2010-05-12 15:08:24 +0530785 if (mmc->scr[0] & SD_DATA_4BIT)
786 mmc->card_caps |= MMC_MODE_4BIT;
787
Andy Flemingad347bb2008-10-30 16:41:01 -0500788 /* Version 1.0 doesn't support switching */
789 if (mmc->version == SD_VERSION_1_0)
790 return 0;
791
792 timeout = 4;
793 while (timeout--) {
794 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
Anton staaf9b00f0d2011-10-03 13:54:59 +0000795 (u8 *)switch_status);
Andy Flemingad347bb2008-10-30 16:41:01 -0500796
797 if (err)
798 return err;
799
800 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy6e8edf42009-05-07 00:43:30 +0300801 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Flemingad347bb2008-10-30 16:41:01 -0500802 break;
803 }
804
Andy Flemingad347bb2008-10-30 16:41:01 -0500805 /* If high-speed isn't supported, we return */
Yauhen Kharuzhy6e8edf42009-05-07 00:43:30 +0300806 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
Andy Flemingad347bb2008-10-30 16:41:01 -0500807 return 0;
808
Macpaul Lin24e92ec2011-11-28 16:31:09 +0000809 /*
810 * If the host doesn't support SD_HIGHSPEED, do not switch card to
811 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
812 * This can avoid furthur problem when the card runs in different
813 * mode between the host.
814 */
815 if (!((mmc->host_caps & MMC_MODE_HS_52MHz) &&
816 (mmc->host_caps & MMC_MODE_HS)))
817 return 0;
818
Anton staaf9b00f0d2011-10-03 13:54:59 +0000819 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
Andy Flemingad347bb2008-10-30 16:41:01 -0500820
821 if (err)
822 return err;
823
Yauhen Kharuzhy6e8edf42009-05-07 00:43:30 +0300824 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
Andy Flemingad347bb2008-10-30 16:41:01 -0500825 mmc->card_caps |= MMC_MODE_HS;
826
827 return 0;
828}
829
830/* frequency bases */
831/* divided by 10 to be nice to platforms without floating point */
Mike Frysingerb588caf2010-10-20 01:15:53 +0000832static const int fbase[] = {
Andy Flemingad347bb2008-10-30 16:41:01 -0500833 10000,
834 100000,
835 1000000,
836 10000000,
837};
838
839/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
840 * to platforms without floating point.
841 */
Mike Frysingerb588caf2010-10-20 01:15:53 +0000842static const int multipliers[] = {
Andy Flemingad347bb2008-10-30 16:41:01 -0500843 0, /* reserved */
844 10,
845 12,
846 13,
847 15,
848 20,
849 25,
850 30,
851 35,
852 40,
853 45,
854 50,
855 55,
856 60,
857 70,
858 80,
859};
860
Kim Phillips87ea3892012-10-29 13:34:43 +0000861static void mmc_set_ios(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -0500862{
863 mmc->set_ios(mmc);
864}
865
866void mmc_set_clock(struct mmc *mmc, uint clock)
867{
868 if (clock > mmc->f_max)
869 clock = mmc->f_max;
870
871 if (clock < mmc->f_min)
872 clock = mmc->f_min;
873
874 mmc->clock = clock;
875
876 mmc_set_ios(mmc);
877}
878
Kim Phillips87ea3892012-10-29 13:34:43 +0000879static void mmc_set_bus_width(struct mmc *mmc, uint width)
Andy Flemingad347bb2008-10-30 16:41:01 -0500880{
881 mmc->bus_width = width;
882
883 mmc_set_ios(mmc);
884}
885
Kim Phillips87ea3892012-10-29 13:34:43 +0000886static int mmc_startup(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -0500887{
Andy Flemingeb766ad2012-10-31 19:02:38 +0000888 int err;
Andy Flemingad347bb2008-10-30 16:41:01 -0500889 uint mult, freq;
Yoshihiro Shimoda7d88de52011-07-04 22:13:26 +0000890 u64 cmult, csize, capacity;
Andy Flemingad347bb2008-10-30 16:41:01 -0500891 struct mmc_cmd cmd;
Yoshihiro Shimodaf6bec732012-06-07 19:09:11 +0000892 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, 512);
893 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, 512);
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000894 int timeout = 1000;
Andy Flemingad347bb2008-10-30 16:41:01 -0500895
Thomas Chou1254c3d2010-12-24 13:12:21 +0000896#ifdef CONFIG_MMC_SPI_CRC_ON
897 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
898 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
899 cmd.resp_type = MMC_RSP_R1;
900 cmd.cmdarg = 1;
Thomas Chou1254c3d2010-12-24 13:12:21 +0000901 err = mmc_send_cmd(mmc, &cmd, NULL);
902
903 if (err)
904 return err;
905 }
906#endif
907
Andy Flemingad347bb2008-10-30 16:41:01 -0500908 /* Put the Card in Identify Mode */
Thomas Chou1254c3d2010-12-24 13:12:21 +0000909 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
910 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
Andy Flemingad347bb2008-10-30 16:41:01 -0500911 cmd.resp_type = MMC_RSP_R2;
912 cmd.cmdarg = 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500913
914 err = mmc_send_cmd(mmc, &cmd, NULL);
915
916 if (err)
917 return err;
918
919 memcpy(mmc->cid, cmd.response, 16);
920
921 /*
922 * For MMC cards, set the Relative Address.
923 * For SD cards, get the Relatvie Address.
924 * This also puts the cards into Standby State
925 */
Thomas Chou1254c3d2010-12-24 13:12:21 +0000926 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
927 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
928 cmd.cmdarg = mmc->rca << 16;
929 cmd.resp_type = MMC_RSP_R6;
Andy Flemingad347bb2008-10-30 16:41:01 -0500930
Thomas Chou1254c3d2010-12-24 13:12:21 +0000931 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Flemingad347bb2008-10-30 16:41:01 -0500932
Thomas Chou1254c3d2010-12-24 13:12:21 +0000933 if (err)
934 return err;
Andy Flemingad347bb2008-10-30 16:41:01 -0500935
Thomas Chou1254c3d2010-12-24 13:12:21 +0000936 if (IS_SD(mmc))
937 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
938 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500939
940 /* Get the Card-Specific Data */
941 cmd.cmdidx = MMC_CMD_SEND_CSD;
942 cmd.resp_type = MMC_RSP_R2;
943 cmd.cmdarg = mmc->rca << 16;
Andy Flemingad347bb2008-10-30 16:41:01 -0500944
945 err = mmc_send_cmd(mmc, &cmd, NULL);
946
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000947 /* Waiting for the ready status */
948 mmc_send_status(mmc, timeout);
949
Andy Flemingad347bb2008-10-30 16:41:01 -0500950 if (err)
951 return err;
952
Rabin Vincentb6eed942009-04-05 13:30:56 +0530953 mmc->csd[0] = cmd.response[0];
954 mmc->csd[1] = cmd.response[1];
955 mmc->csd[2] = cmd.response[2];
956 mmc->csd[3] = cmd.response[3];
Andy Flemingad347bb2008-10-30 16:41:01 -0500957
958 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincentbdf7a682009-04-05 13:30:55 +0530959 int version = (cmd.response[0] >> 26) & 0xf;
Andy Flemingad347bb2008-10-30 16:41:01 -0500960
961 switch (version) {
962 case 0:
963 mmc->version = MMC_VERSION_1_2;
964 break;
965 case 1:
966 mmc->version = MMC_VERSION_1_4;
967 break;
968 case 2:
969 mmc->version = MMC_VERSION_2_2;
970 break;
971 case 3:
972 mmc->version = MMC_VERSION_3;
973 break;
974 case 4:
975 mmc->version = MMC_VERSION_4;
976 break;
977 default:
978 mmc->version = MMC_VERSION_1_2;
979 break;
980 }
981 }
982
983 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincentbdf7a682009-04-05 13:30:55 +0530984 freq = fbase[(cmd.response[0] & 0x7)];
985 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Flemingad347bb2008-10-30 16:41:01 -0500986
987 mmc->tran_speed = freq * mult;
988
Rabin Vincentb6eed942009-04-05 13:30:56 +0530989 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Andy Flemingad347bb2008-10-30 16:41:01 -0500990
991 if (IS_SD(mmc))
992 mmc->write_bl_len = mmc->read_bl_len;
993 else
Rabin Vincentb6eed942009-04-05 13:30:56 +0530994 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Andy Flemingad347bb2008-10-30 16:41:01 -0500995
996 if (mmc->high_capacity) {
997 csize = (mmc->csd[1] & 0x3f) << 16
998 | (mmc->csd[2] & 0xffff0000) >> 16;
999 cmult = 8;
1000 } else {
1001 csize = (mmc->csd[1] & 0x3ff) << 2
1002 | (mmc->csd[2] & 0xc0000000) >> 30;
1003 cmult = (mmc->csd[2] & 0x00038000) >> 15;
1004 }
1005
1006 mmc->capacity = (csize + 1) << (cmult + 2);
1007 mmc->capacity *= mmc->read_bl_len;
1008
1009 if (mmc->read_bl_len > 512)
1010 mmc->read_bl_len = 512;
1011
1012 if (mmc->write_bl_len > 512)
1013 mmc->write_bl_len = 512;
1014
1015 /* Select the card, and put it into Transfer Mode */
Thomas Chou1254c3d2010-12-24 13:12:21 +00001016 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1017 cmd.cmdidx = MMC_CMD_SELECT_CARD;
Ajay Bhargav4a32fba2011-10-05 03:13:23 +00001018 cmd.resp_type = MMC_RSP_R1;
Thomas Chou1254c3d2010-12-24 13:12:21 +00001019 cmd.cmdarg = mmc->rca << 16;
Thomas Chou1254c3d2010-12-24 13:12:21 +00001020 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Flemingad347bb2008-10-30 16:41:01 -05001021
Thomas Chou1254c3d2010-12-24 13:12:21 +00001022 if (err)
1023 return err;
1024 }
Andy Flemingad347bb2008-10-30 16:41:01 -05001025
Lei Wenea526762011-06-22 17:03:31 +00001026 /*
1027 * For SD, its erase group is always one sector
1028 */
1029 mmc->erase_grp_size = 1;
Lei Wen31b99802011-05-02 16:26:26 +00001030 mmc->part_config = MMCPART_NOAVAILABLE;
Sukumar Ghorai232293c2010-09-20 18:29:29 +05301031 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
1032 /* check ext_csd version and capacity */
1033 err = mmc_send_ext_csd(mmc, ext_csd);
Kim Phillips87ea3892012-10-29 13:34:43 +00001034 if (!err && (ext_csd[EXT_CSD_REV] >= 2)) {
Yoshihiro Shimoda7d88de52011-07-04 22:13:26 +00001035 /*
1036 * According to the JEDEC Standard, the value of
1037 * ext_csd's capacity is valid if the value is more
1038 * than 2GB
1039 */
Lei Wen217467f2011-10-03 20:35:10 +00001040 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
1041 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
1042 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
1043 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
Yoshihiro Shimoda7d88de52011-07-04 22:13:26 +00001044 capacity *= 512;
Łukasz Majewski237823e2011-07-05 02:19:44 +00001045 if ((capacity >> 20) > 2 * 1024)
Yoshihiro Shimoda7d88de52011-07-04 22:13:26 +00001046 mmc->capacity = capacity;
Sukumar Ghorai232293c2010-09-20 18:29:29 +05301047 }
Lei Wen31b99802011-05-02 16:26:26 +00001048
Lei Wenea526762011-06-22 17:03:31 +00001049 /*
1050 * Check whether GROUP_DEF is set, if yes, read out
1051 * group size from ext_csd directly, or calculate
1052 * the group size from the csd value.
1053 */
Lei Wen217467f2011-10-03 20:35:10 +00001054 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF])
1055 mmc->erase_grp_size =
1056 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 512 * 1024;
Lei Wenea526762011-06-22 17:03:31 +00001057 else {
1058 int erase_gsz, erase_gmul;
1059 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1060 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1061 mmc->erase_grp_size = (erase_gsz + 1)
1062 * (erase_gmul + 1);
1063 }
1064
Lei Wen31b99802011-05-02 16:26:26 +00001065 /* store the partition info of emmc */
Stephen Warren009784c2012-07-30 10:55:43 +00001066 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
1067 ext_csd[EXT_CSD_BOOT_MULT])
Lei Wen217467f2011-10-03 20:35:10 +00001068 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
Sukumar Ghorai232293c2010-09-20 18:29:29 +05301069 }
1070
Andy Flemingad347bb2008-10-30 16:41:01 -05001071 if (IS_SD(mmc))
1072 err = sd_change_freq(mmc);
1073 else
1074 err = mmc_change_freq(mmc);
1075
1076 if (err)
1077 return err;
1078
1079 /* Restrict card's capabilities by what the host can do */
1080 mmc->card_caps &= mmc->host_caps;
1081
1082 if (IS_SD(mmc)) {
1083 if (mmc->card_caps & MMC_MODE_4BIT) {
1084 cmd.cmdidx = MMC_CMD_APP_CMD;
1085 cmd.resp_type = MMC_RSP_R1;
1086 cmd.cmdarg = mmc->rca << 16;
Andy Flemingad347bb2008-10-30 16:41:01 -05001087
1088 err = mmc_send_cmd(mmc, &cmd, NULL);
1089 if (err)
1090 return err;
1091
1092 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1093 cmd.resp_type = MMC_RSP_R1;
1094 cmd.cmdarg = 2;
Andy Flemingad347bb2008-10-30 16:41:01 -05001095 err = mmc_send_cmd(mmc, &cmd, NULL);
1096 if (err)
1097 return err;
1098
1099 mmc_set_bus_width(mmc, 4);
1100 }
1101
1102 if (mmc->card_caps & MMC_MODE_HS)
Jaehoon Chunge1d4c7b2012-03-26 21:16:03 +00001103 mmc->tran_speed = 50000000;
Andy Flemingad347bb2008-10-30 16:41:01 -05001104 else
Jaehoon Chunge1d4c7b2012-03-26 21:16:03 +00001105 mmc->tran_speed = 25000000;
Andy Flemingad347bb2008-10-30 16:41:01 -05001106 } else {
Andy Flemingeb766ad2012-10-31 19:02:38 +00001107 int idx;
1108
1109 /* An array of possible bus widths in order of preference */
1110 static unsigned ext_csd_bits[] = {
1111 EXT_CSD_BUS_WIDTH_8,
1112 EXT_CSD_BUS_WIDTH_4,
1113 EXT_CSD_BUS_WIDTH_1,
1114 };
1115
1116 /* An array to map CSD bus widths to host cap bits */
1117 static unsigned ext_to_hostcaps[] = {
1118 [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
1119 [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
1120 };
1121
1122 /* An array to map chosen bus width to an integer */
1123 static unsigned widths[] = {
1124 8, 4, 1,
1125 };
1126
1127 for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
1128 unsigned int extw = ext_csd_bits[idx];
1129
1130 /*
1131 * Check to make sure the controller supports
1132 * this bus width, if it's more than 1
1133 */
1134 if (extw != EXT_CSD_BUS_WIDTH_1 &&
1135 !(mmc->host_caps & ext_to_hostcaps[extw]))
1136 continue;
1137
Andy Flemingad347bb2008-10-30 16:41:01 -05001138 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
Andy Flemingeb766ad2012-10-31 19:02:38 +00001139 EXT_CSD_BUS_WIDTH, extw);
Andy Flemingad347bb2008-10-30 16:41:01 -05001140
1141 if (err)
Lei Wen4f5a6a52011-10-03 20:35:11 +00001142 continue;
Andy Flemingad347bb2008-10-30 16:41:01 -05001143
Andy Flemingeb766ad2012-10-31 19:02:38 +00001144 mmc_set_bus_width(mmc, widths[idx]);
Andy Flemingad347bb2008-10-30 16:41:01 -05001145
Lei Wen4f5a6a52011-10-03 20:35:11 +00001146 err = mmc_send_ext_csd(mmc, test_csd);
1147 if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \
1148 == test_csd[EXT_CSD_PARTITIONING_SUPPORT]
1149 && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \
1150 == test_csd[EXT_CSD_ERASE_GROUP_DEF] \
1151 && ext_csd[EXT_CSD_REV] \
1152 == test_csd[EXT_CSD_REV]
1153 && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \
1154 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1155 && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \
1156 &test_csd[EXT_CSD_SEC_CNT], 4) == 0) {
Andy Flemingad347bb2008-10-30 16:41:01 -05001157
Andy Flemingeb766ad2012-10-31 19:02:38 +00001158 mmc->card_caps |= ext_to_hostcaps[extw];
Lei Wen4f5a6a52011-10-03 20:35:11 +00001159 break;
1160 }
Andy Flemingad347bb2008-10-30 16:41:01 -05001161 }
1162
1163 if (mmc->card_caps & MMC_MODE_HS) {
1164 if (mmc->card_caps & MMC_MODE_HS_52MHz)
Jaehoon Chunge1d4c7b2012-03-26 21:16:03 +00001165 mmc->tran_speed = 52000000;
Andy Flemingad347bb2008-10-30 16:41:01 -05001166 else
Jaehoon Chunge1d4c7b2012-03-26 21:16:03 +00001167 mmc->tran_speed = 26000000;
1168 }
Andy Flemingad347bb2008-10-30 16:41:01 -05001169 }
1170
Jaehoon Chunge1d4c7b2012-03-26 21:16:03 +00001171 mmc_set_clock(mmc, mmc->tran_speed);
1172
Andy Flemingad347bb2008-10-30 16:41:01 -05001173 /* fill in device description */
1174 mmc->block_dev.lun = 0;
1175 mmc->block_dev.type = 0;
1176 mmc->block_dev.blksz = mmc->read_bl_len;
Rabin Vincent69d4e2c2009-04-05 13:30:54 +05301177 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
Taylor Hutt7367ec22012-10-20 17:15:59 +00001178 sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x",
1179 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1180 (mmc->cid[3] >> 16) & 0xffff);
1181 sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
1182 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1183 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1184 (mmc->cid[2] >> 24) & 0xff);
1185 sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
1186 (mmc->cid[2] >> 16) & 0xf);
Mikhail Kshevetskiy5cbfa8e7a2012-07-09 08:53:38 +00001187#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
Andy Flemingad347bb2008-10-30 16:41:01 -05001188 init_part(&mmc->block_dev);
Mikhail Kshevetskiy5cbfa8e7a2012-07-09 08:53:38 +00001189#endif
Andy Flemingad347bb2008-10-30 16:41:01 -05001190
1191 return 0;
1192}
1193
Kim Phillips87ea3892012-10-29 13:34:43 +00001194static int mmc_send_if_cond(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -05001195{
1196 struct mmc_cmd cmd;
1197 int err;
1198
1199 cmd.cmdidx = SD_CMD_SEND_IF_COND;
1200 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1201 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
1202 cmd.resp_type = MMC_RSP_R7;
Andy Flemingad347bb2008-10-30 16:41:01 -05001203
1204 err = mmc_send_cmd(mmc, &cmd, NULL);
1205
1206 if (err)
1207 return err;
1208
Rabin Vincentb6eed942009-04-05 13:30:56 +05301209 if ((cmd.response[0] & 0xff) != 0xaa)
Andy Flemingad347bb2008-10-30 16:41:01 -05001210 return UNUSABLE_ERR;
1211 else
1212 mmc->version = SD_VERSION_2;
1213
1214 return 0;
1215}
1216
1217int mmc_register(struct mmc *mmc)
1218{
1219 /* Setup the universal parts of the block interface just once */
1220 mmc->block_dev.if_type = IF_TYPE_MMC;
1221 mmc->block_dev.dev = cur_dev_num++;
1222 mmc->block_dev.removable = 1;
1223 mmc->block_dev.block_read = mmc_bread;
1224 mmc->block_dev.block_write = mmc_bwrite;
Lei Wenea526762011-06-22 17:03:31 +00001225 mmc->block_dev.block_erase = mmc_berase;
John Rigbyf2f43662011-04-18 05:50:08 +00001226 if (!mmc->b_max)
1227 mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Andy Flemingad347bb2008-10-30 16:41:01 -05001228
1229 INIT_LIST_HEAD (&mmc->link);
1230
1231 list_add_tail (&mmc->link, &mmc_devices);
1232
1233 return 0;
1234}
1235
Matthew McClintock6252b4f2011-05-24 05:31:19 +00001236#ifdef CONFIG_PARTITIONS
Andy Flemingad347bb2008-10-30 16:41:01 -05001237block_dev_desc_t *mmc_get_dev(int dev)
1238{
1239 struct mmc *mmc = find_mmc_device(dev);
Benoît Thébaudeau6ce8aed2012-08-10 08:59:12 +00001240 if (!mmc || mmc_init(mmc))
Łukasz Majewski65b04cf2012-04-19 02:39:18 +00001241 return NULL;
Andy Flemingad347bb2008-10-30 16:41:01 -05001242
Łukasz Majewski65b04cf2012-04-19 02:39:18 +00001243 return &mmc->block_dev;
Andy Flemingad347bb2008-10-30 16:41:01 -05001244}
Matthew McClintock6252b4f2011-05-24 05:31:19 +00001245#endif
Andy Flemingad347bb2008-10-30 16:41:01 -05001246
1247int mmc_init(struct mmc *mmc)
1248{
Macpaul Lin028bde12011-11-14 23:35:39 +00001249 int err;
Andy Flemingad347bb2008-10-30 16:41:01 -05001250
Thierry Redingb9c8b772012-01-02 01:15:37 +00001251 if (mmc_getcd(mmc) == 0) {
1252 mmc->has_init = 0;
1253 printf("MMC: no card present\n");
1254 return NO_CARD_ERR;
1255 }
1256
Lei Wen31b99802011-05-02 16:26:26 +00001257 if (mmc->has_init)
1258 return 0;
1259
Andy Flemingad347bb2008-10-30 16:41:01 -05001260 err = mmc->init(mmc);
1261
1262 if (err)
1263 return err;
1264
Ilya Yanok8459aab2009-06-29 17:53:16 +04001265 mmc_set_bus_width(mmc, 1);
1266 mmc_set_clock(mmc, 1);
1267
Andy Flemingad347bb2008-10-30 16:41:01 -05001268 /* Reset the Card */
1269 err = mmc_go_idle(mmc);
1270
1271 if (err)
1272 return err;
1273
Lei Wen31b99802011-05-02 16:26:26 +00001274 /* The internal partition reset to user partition(0) at every CMD0*/
1275 mmc->part_num = 0;
1276
Andy Flemingad347bb2008-10-30 16:41:01 -05001277 /* Test for SD version 2 */
Macpaul Lin028bde12011-11-14 23:35:39 +00001278 err = mmc_send_if_cond(mmc);
Andy Flemingad347bb2008-10-30 16:41:01 -05001279
Andy Flemingad347bb2008-10-30 16:41:01 -05001280 /* Now try to get the SD card's operating condition */
1281 err = sd_send_op_cond(mmc);
1282
1283 /* If the command timed out, we check for an MMC card */
1284 if (err == TIMEOUT) {
1285 err = mmc_send_op_cond(mmc);
1286
1287 if (err) {
1288 printf("Card did not respond to voltage select!\n");
1289 return UNUSABLE_ERR;
1290 }
1291 }
1292
Lei Wen31b99802011-05-02 16:26:26 +00001293 err = mmc_startup(mmc);
1294 if (err)
1295 mmc->has_init = 0;
1296 else
1297 mmc->has_init = 1;
1298 return err;
Andy Flemingad347bb2008-10-30 16:41:01 -05001299}
1300
1301/*
1302 * CPU and board-specific MMC initializations. Aliased function
1303 * signals caller to move on
1304 */
1305static int __def_mmc_init(bd_t *bis)
1306{
1307 return -1;
1308}
1309
Peter Tyser21d2cd22009-04-20 11:08:46 -05001310int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1311int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
Andy Flemingad347bb2008-10-30 16:41:01 -05001312
1313void print_mmc_devices(char separator)
1314{
1315 struct mmc *m;
1316 struct list_head *entry;
1317
1318 list_for_each(entry, &mmc_devices) {
1319 m = list_entry(entry, struct mmc, link);
1320
1321 printf("%s: %d", m->name, m->block_dev.dev);
1322
1323 if (entry->next != &mmc_devices)
1324 printf("%c ", separator);
1325 }
1326
1327 printf("\n");
1328}
1329
Lei Wend430d7c2011-05-02 16:26:25 +00001330int get_mmc_num(void)
1331{
1332 return cur_dev_num;
1333}
1334
Andy Flemingad347bb2008-10-30 16:41:01 -05001335int mmc_initialize(bd_t *bis)
1336{
1337 INIT_LIST_HEAD (&mmc_devices);
1338 cur_dev_num = 0;
1339
1340 if (board_mmc_init(bis) < 0)
1341 cpu_mmc_init(bis);
1342
1343 print_mmc_devices(',');
1344
1345 return 0;
1346}