blob: d732581eb8d9708d6c30019d5f376b9beaea29b2 [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
Nikita Kiryanovd23d8d72012-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
Peter Korsgaardd4e1da42013-03-21 04:00:03 +000054 if (wp < 0) {
55 if (mmc->getwp)
56 wp = mmc->getwp(mmc);
57 else
58 wp = 0;
59 }
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +000060
61 return wp;
62}
63
Thierry Reding314284b2012-01-02 01:15:36 +000064int __board_mmc_getcd(struct mmc *mmc) {
Stefano Babic11fdade2010-02-05 15:04:43 +010065 return -1;
66}
67
Thierry Reding314284b2012-01-02 01:15:36 +000068int board_mmc_getcd(struct mmc *mmc)__attribute__((weak,
Stefano Babic11fdade2010-02-05 15:04:43 +010069 alias("__board_mmc_getcd")));
70
Kim Phillipsfdbb8732012-10-29 13:34:43 +000071static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
72 struct mmc_data *data)
Andy Fleming272cc702008-10-30 16:41:01 -050073{
Marek Vasut8635ff92012-03-15 18:41:35 +000074 struct mmc_data backup;
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +000075 int ret;
Marek Vasut8635ff92012-03-15 18:41:35 +000076
77 memset(&backup, 0, sizeof(backup));
78
Marek Vasut8635ff92012-03-15 18:41:35 +000079#ifdef CONFIG_MMC_TRACE
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +000080 int i;
81 u8 *ptr;
82
83 printf("CMD_SEND:%d\n", cmd->cmdidx);
84 printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +000085 ret = mmc->send_cmd(mmc, cmd, data);
86 switch (cmd->resp_type) {
87 case MMC_RSP_NONE:
88 printf("\t\tMMC_RSP_NONE\n");
89 break;
90 case MMC_RSP_R1:
91 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
92 cmd->response[0]);
93 break;
94 case MMC_RSP_R1b:
95 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
96 cmd->response[0]);
97 break;
98 case MMC_RSP_R2:
99 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
100 cmd->response[0]);
101 printf("\t\t \t\t 0x%08X \n",
102 cmd->response[1]);
103 printf("\t\t \t\t 0x%08X \n",
104 cmd->response[2]);
105 printf("\t\t \t\t 0x%08X \n",
106 cmd->response[3]);
107 printf("\n");
108 printf("\t\t\t\t\tDUMPING DATA\n");
109 for (i = 0; i < 4; i++) {
110 int j;
111 printf("\t\t\t\t\t%03d - ", i*4);
Dirk Behme146bec72012-03-08 02:35:34 +0000112 ptr = (u8 *)&cmd->response[i];
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000113 ptr += 3;
114 for (j = 0; j < 4; j++)
115 printf("%02X ", *ptr--);
116 printf("\n");
117 }
118 break;
119 case MMC_RSP_R3:
120 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
121 cmd->response[0]);
122 break;
123 default:
124 printf("\t\tERROR MMC rsp not supported\n");
125 break;
126 }
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000127#else
Marek Vasut8635ff92012-03-15 18:41:35 +0000128 ret = mmc->send_cmd(mmc, cmd, data);
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000129#endif
Marek Vasut8635ff92012-03-15 18:41:35 +0000130 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500131}
132
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000133static int mmc_send_status(struct mmc *mmc, int timeout)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000134{
135 struct mmc_cmd cmd;
Jan Kloetzked617c422012-02-05 22:29:12 +0000136 int err, retries = 5;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000137#ifdef CONFIG_MMC_TRACE
138 int status;
139#endif
140
141 cmd.cmdidx = MMC_CMD_SEND_STATUS;
142 cmd.resp_type = MMC_RSP_R1;
Marek Vasutaaf3d412011-08-10 09:24:48 +0200143 if (!mmc_host_is_spi(mmc))
144 cmd.cmdarg = mmc->rca << 16;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000145
146 do {
147 err = mmc_send_cmd(mmc, &cmd, NULL);
Jan Kloetzked617c422012-02-05 22:29:12 +0000148 if (!err) {
149 if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
150 (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
151 MMC_STATE_PRG)
152 break;
153 else if (cmd.response[0] & MMC_STATUS_MASK) {
154 printf("Status Error: 0x%08X\n",
155 cmd.response[0]);
156 return COMM_ERR;
157 }
158 } else if (--retries < 0)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000159 return err;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000160
161 udelay(1000);
162
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000163 } while (timeout--);
164
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000165#ifdef CONFIG_MMC_TRACE
166 status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
167 printf("CURR STATE:%d\n", status);
168#endif
Jongman Heo5b0c9422012-06-03 21:32:13 +0000169 if (timeout <= 0) {
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000170 printf("Timeout waiting card ready\n");
171 return TIMEOUT;
172 }
173
174 return 0;
175}
176
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000177static int mmc_set_blocklen(struct mmc *mmc, int len)
Andy Fleming272cc702008-10-30 16:41:01 -0500178{
179 struct mmc_cmd cmd;
180
181 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
182 cmd.resp_type = MMC_RSP_R1;
183 cmd.cmdarg = len;
Andy Fleming272cc702008-10-30 16:41:01 -0500184
185 return mmc_send_cmd(mmc, &cmd, NULL);
186}
187
188struct mmc *find_mmc_device(int dev_num)
189{
190 struct mmc *m;
191 struct list_head *entry;
192
193 list_for_each(entry, &mmc_devices) {
194 m = list_entry(entry, struct mmc, link);
195
196 if (m->block_dev.dev == dev_num)
197 return m;
198 }
199
200 printf("MMC Device %d not found\n", dev_num);
201
202 return NULL;
203}
204
Lei Wene6f99a52011-06-22 17:03:31 +0000205static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
206{
207 struct mmc_cmd cmd;
208 ulong end;
209 int err, start_cmd, end_cmd;
210
211 if (mmc->high_capacity)
212 end = start + blkcnt - 1;
213 else {
214 end = (start + blkcnt - 1) * mmc->write_bl_len;
215 start *= mmc->write_bl_len;
216 }
217
218 if (IS_SD(mmc)) {
219 start_cmd = SD_CMD_ERASE_WR_BLK_START;
220 end_cmd = SD_CMD_ERASE_WR_BLK_END;
221 } else {
222 start_cmd = MMC_CMD_ERASE_GROUP_START;
223 end_cmd = MMC_CMD_ERASE_GROUP_END;
224 }
225
226 cmd.cmdidx = start_cmd;
227 cmd.cmdarg = start;
228 cmd.resp_type = MMC_RSP_R1;
Lei Wene6f99a52011-06-22 17:03:31 +0000229
230 err = mmc_send_cmd(mmc, &cmd, NULL);
231 if (err)
232 goto err_out;
233
234 cmd.cmdidx = end_cmd;
235 cmd.cmdarg = end;
236
237 err = mmc_send_cmd(mmc, &cmd, NULL);
238 if (err)
239 goto err_out;
240
241 cmd.cmdidx = MMC_CMD_ERASE;
242 cmd.cmdarg = SECURE_ERASE;
243 cmd.resp_type = MMC_RSP_R1b;
244
245 err = mmc_send_cmd(mmc, &cmd, NULL);
246 if (err)
247 goto err_out;
248
249 return 0;
250
251err_out:
252 puts("mmc erase failed\n");
253 return err;
254}
255
256static unsigned long
257mmc_berase(int dev_num, unsigned long start, lbaint_t blkcnt)
258{
259 int err = 0;
260 struct mmc *mmc = find_mmc_device(dev_num);
261 lbaint_t blk = 0, blk_r = 0;
Jerry Huangd2d8afa2012-05-17 23:00:51 +0000262 int timeout = 1000;
Lei Wene6f99a52011-06-22 17:03:31 +0000263
264 if (!mmc)
265 return -1;
266
267 if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size))
268 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
269 "The erase range would be change to 0x%lx~0x%lx\n\n",
270 mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
271 ((start + blkcnt + mmc->erase_grp_size)
272 & ~(mmc->erase_grp_size - 1)) - 1);
273
274 while (blk < blkcnt) {
275 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
276 mmc->erase_grp_size : (blkcnt - blk);
277 err = mmc_erase_t(mmc, start + blk, blk_r);
278 if (err)
279 break;
280
281 blk += blk_r;
Jerry Huangd2d8afa2012-05-17 23:00:51 +0000282
283 /* Waiting for the ready status */
284 if (mmc_send_status(mmc, timeout))
285 return 0;
Lei Wene6f99a52011-06-22 17:03:31 +0000286 }
287
288 return blk;
289}
290
Andy Fleming272cc702008-10-30 16:41:01 -0500291static ulong
Lei Wen01581262010-10-14 13:38:11 +0800292mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
Andy Fleming272cc702008-10-30 16:41:01 -0500293{
294 struct mmc_cmd cmd;
295 struct mmc_data data;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000296 int timeout = 1000;
Andy Fleming272cc702008-10-30 16:41:01 -0500297
Lei Wend2bf29e2010-09-13 22:07:27 +0800298 if ((start + blkcnt) > mmc->block_dev.lba) {
Steve Sakomandef412b2010-10-28 09:00:26 -0700299 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
Lei Wend2bf29e2010-09-13 22:07:27 +0800300 start + blkcnt, mmc->block_dev.lba);
301 return 0;
302 }
Andy Fleming272cc702008-10-30 16:41:01 -0500303
304 if (blkcnt > 1)
305 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
306 else
307 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
308
309 if (mmc->high_capacity)
310 cmd.cmdarg = start;
311 else
Steve Sakomandef412b2010-10-28 09:00:26 -0700312 cmd.cmdarg = start * mmc->write_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500313
314 cmd.resp_type = MMC_RSP_R1;
Andy Fleming272cc702008-10-30 16:41:01 -0500315
316 data.src = src;
317 data.blocks = blkcnt;
Steve Sakomandef412b2010-10-28 09:00:26 -0700318 data.blocksize = mmc->write_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500319 data.flags = MMC_DATA_WRITE;
320
Steve Sakomandef412b2010-10-28 09:00:26 -0700321 if (mmc_send_cmd(mmc, &cmd, &data)) {
322 printf("mmc write failed\n");
323 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500324 }
325
Thomas Choud52ebf12010-12-24 13:12:21 +0000326 /* SPI multiblock writes terminate using a special
327 * token, not a STOP_TRANSMISSION request.
328 */
329 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
Andy Fleming272cc702008-10-30 16:41:01 -0500330 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
331 cmd.cmdarg = 0;
332 cmd.resp_type = MMC_RSP_R1b;
Steve Sakomandef412b2010-10-28 09:00:26 -0700333 if (mmc_send_cmd(mmc, &cmd, NULL)) {
334 printf("mmc fail to send stop cmd\n");
335 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800336 }
Andy Fleming272cc702008-10-30 16:41:01 -0500337 }
338
Jan Kloetzke93ad0d12012-02-05 22:29:11 +0000339 /* Waiting for the ready status */
340 if (mmc_send_status(mmc, timeout))
341 return 0;
342
Andy Fleming272cc702008-10-30 16:41:01 -0500343 return blkcnt;
344}
345
Lei Wen01581262010-10-14 13:38:11 +0800346static ulong
347mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
348{
Lei Wen01581262010-10-14 13:38:11 +0800349 lbaint_t cur, blocks_todo = blkcnt;
350
Steve Sakomandef412b2010-10-28 09:00:26 -0700351 struct mmc *mmc = find_mmc_device(dev_num);
Lei Wen01581262010-10-14 13:38:11 +0800352 if (!mmc)
Steve Sakomandef412b2010-10-28 09:00:26 -0700353 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800354
Steve Sakomandef412b2010-10-28 09:00:26 -0700355 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
356 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800357
358 do {
John Rigby8feafcc2011-04-18 05:50:08 +0000359 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
Lei Wen01581262010-10-14 13:38:11 +0800360 if(mmc_write_blocks(mmc, start, cur, src) != cur)
Steve Sakomandef412b2010-10-28 09:00:26 -0700361 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800362 blocks_todo -= cur;
363 start += cur;
364 src += cur * mmc->write_bl_len;
365 } while (blocks_todo > 0);
366
367 return blkcnt;
368}
369
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000370static int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start,
371 lbaint_t blkcnt)
Andy Fleming272cc702008-10-30 16:41:01 -0500372{
373 struct mmc_cmd cmd;
374 struct mmc_data data;
375
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700376 if (blkcnt > 1)
377 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
378 else
379 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Fleming272cc702008-10-30 16:41:01 -0500380
381 if (mmc->high_capacity)
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700382 cmd.cmdarg = start;
Andy Fleming272cc702008-10-30 16:41:01 -0500383 else
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700384 cmd.cmdarg = start * mmc->read_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500385
386 cmd.resp_type = MMC_RSP_R1;
Andy Fleming272cc702008-10-30 16:41:01 -0500387
388 data.dest = dst;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700389 data.blocks = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500390 data.blocksize = mmc->read_bl_len;
391 data.flags = MMC_DATA_READ;
392
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700393 if (mmc_send_cmd(mmc, &cmd, &data))
394 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500395
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700396 if (blkcnt > 1) {
397 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
398 cmd.cmdarg = 0;
399 cmd.resp_type = MMC_RSP_R1b;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700400 if (mmc_send_cmd(mmc, &cmd, NULL)) {
401 printf("mmc fail to send stop cmd\n");
402 return 0;
403 }
Andy Fleming272cc702008-10-30 16:41:01 -0500404 }
405
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700406 return blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500407}
408
409static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
410{
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700411 lbaint_t cur, blocks_todo = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500412
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700413 if (blkcnt == 0)
414 return 0;
415
416 struct mmc *mmc = find_mmc_device(dev_num);
Andy Fleming272cc702008-10-30 16:41:01 -0500417 if (!mmc)
418 return 0;
419
Lei Wend2bf29e2010-09-13 22:07:27 +0800420 if ((start + blkcnt) > mmc->block_dev.lba) {
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700421 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
Lei Wend2bf29e2010-09-13 22:07:27 +0800422 start + blkcnt, mmc->block_dev.lba);
423 return 0;
424 }
Andy Fleming272cc702008-10-30 16:41:01 -0500425
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700426 if (mmc_set_blocklen(mmc, mmc->read_bl_len))
Andy Fleming272cc702008-10-30 16:41:01 -0500427 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500428
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700429 do {
John Rigby8feafcc2011-04-18 05:50:08 +0000430 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700431 if(mmc_read_blocks(mmc, dst, start, cur) != cur)
432 return 0;
433 blocks_todo -= cur;
434 start += cur;
435 dst += cur * mmc->read_bl_len;
436 } while (blocks_todo > 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500437
438 return blkcnt;
439}
440
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000441static int mmc_go_idle(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500442{
443 struct mmc_cmd cmd;
444 int err;
445
446 udelay(1000);
447
448 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
449 cmd.cmdarg = 0;
450 cmd.resp_type = MMC_RSP_NONE;
Andy Fleming272cc702008-10-30 16:41:01 -0500451
452 err = mmc_send_cmd(mmc, &cmd, NULL);
453
454 if (err)
455 return err;
456
457 udelay(2000);
458
459 return 0;
460}
461
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000462static int sd_send_op_cond(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500463{
464 int timeout = 1000;
465 int err;
466 struct mmc_cmd cmd;
467
468 do {
469 cmd.cmdidx = MMC_CMD_APP_CMD;
470 cmd.resp_type = MMC_RSP_R1;
471 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500472
473 err = mmc_send_cmd(mmc, &cmd, NULL);
474
475 if (err)
476 return err;
477
478 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
479 cmd.resp_type = MMC_RSP_R3;
Stefano Babic250de122010-01-20 18:20:39 +0100480
481 /*
482 * Most cards do not answer if some reserved bits
483 * in the ocr are set. However, Some controller
484 * can set bit 7 (reserved for low voltages), but
485 * how to manage low voltages SD card is not yet
486 * specified.
487 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000488 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
489 (mmc->voltages & 0xff8000);
Andy Fleming272cc702008-10-30 16:41:01 -0500490
491 if (mmc->version == SD_VERSION_2)
492 cmd.cmdarg |= OCR_HCS;
493
494 err = mmc_send_cmd(mmc, &cmd, NULL);
495
496 if (err)
497 return err;
498
499 udelay(1000);
500 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
501
502 if (timeout <= 0)
503 return UNUSABLE_ERR;
504
505 if (mmc->version != SD_VERSION_2)
506 mmc->version = SD_VERSION_1_0;
507
Thomas Choud52ebf12010-12-24 13:12:21 +0000508 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
509 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
510 cmd.resp_type = MMC_RSP_R3;
511 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000512
513 err = mmc_send_cmd(mmc, &cmd, NULL);
514
515 if (err)
516 return err;
517 }
518
Rabin Vincent998be3d2009-04-05 13:30:56 +0530519 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500520
521 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
522 mmc->rca = 0;
523
524 return 0;
525}
526
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000527static int mmc_send_op_cond(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500528{
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000529 int timeout = 10000;
Andy Fleming272cc702008-10-30 16:41:01 -0500530 struct mmc_cmd cmd;
531 int err;
532
533 /* Some cards seem to need this */
534 mmc_go_idle(mmc);
535
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000536 /* Asking to the card its capabilities */
537 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
538 cmd.resp_type = MMC_RSP_R3;
539 cmd.cmdarg = 0;
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200540
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000541 err = mmc_send_cmd(mmc, &cmd, NULL);
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200542
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000543 if (err)
544 return err;
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200545
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000546 udelay(1000);
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200547
Andy Fleming272cc702008-10-30 16:41:01 -0500548 do {
549 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
550 cmd.resp_type = MMC_RSP_R3;
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000551 cmd.cmdarg = (mmc_host_is_spi(mmc) ? 0 :
552 (mmc->voltages &
553 (cmd.response[0] & OCR_VOLTAGE_MASK)) |
554 (cmd.response[0] & OCR_ACCESS_MODE));
Łukasz Majewskib1f1e8212011-07-05 02:19:44 +0000555
556 if (mmc->host_caps & MMC_MODE_HC)
557 cmd.cmdarg |= OCR_HCS;
558
Andy Fleming272cc702008-10-30 16:41:01 -0500559 err = mmc_send_cmd(mmc, &cmd, NULL);
560
561 if (err)
562 return err;
563
564 udelay(1000);
565 } while (!(cmd.response[0] & OCR_BUSY) && timeout--);
566
567 if (timeout <= 0)
568 return UNUSABLE_ERR;
569
Thomas Choud52ebf12010-12-24 13:12:21 +0000570 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
571 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
572 cmd.resp_type = MMC_RSP_R3;
573 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000574
575 err = mmc_send_cmd(mmc, &cmd, NULL);
576
577 if (err)
578 return err;
579 }
580
Andy Fleming272cc702008-10-30 16:41:01 -0500581 mmc->version = MMC_VERSION_UNKNOWN;
Rabin Vincent998be3d2009-04-05 13:30:56 +0530582 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500583
584 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
585 mmc->rca = 0;
586
587 return 0;
588}
589
590
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000591static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
Andy Fleming272cc702008-10-30 16:41:01 -0500592{
593 struct mmc_cmd cmd;
594 struct mmc_data data;
595 int err;
596
597 /* Get the Card Status Register */
598 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
599 cmd.resp_type = MMC_RSP_R1;
600 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500601
Yoshihiro Shimodacdfd1ac2012-06-07 19:09:11 +0000602 data.dest = (char *)ext_csd;
Andy Fleming272cc702008-10-30 16:41:01 -0500603 data.blocks = 1;
604 data.blocksize = 512;
605 data.flags = MMC_DATA_READ;
606
607 err = mmc_send_cmd(mmc, &cmd, &data);
608
609 return err;
610}
611
612
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000613static int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
Andy Fleming272cc702008-10-30 16:41:01 -0500614{
615 struct mmc_cmd cmd;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000616 int timeout = 1000;
617 int ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500618
619 cmd.cmdidx = MMC_CMD_SWITCH;
620 cmd.resp_type = MMC_RSP_R1b;
621 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000622 (index << 16) |
623 (value << 8);
Andy Fleming272cc702008-10-30 16:41:01 -0500624
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000625 ret = mmc_send_cmd(mmc, &cmd, NULL);
626
627 /* Waiting for the ready status */
Jan Kloetzke93ad0d12012-02-05 22:29:11 +0000628 if (!ret)
629 ret = mmc_send_status(mmc, timeout);
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000630
631 return ret;
632
Andy Fleming272cc702008-10-30 16:41:01 -0500633}
634
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000635static int mmc_change_freq(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500636{
Yoshihiro Shimodacdfd1ac2012-06-07 19:09:11 +0000637 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, 512);
Andy Fleming272cc702008-10-30 16:41:01 -0500638 char cardtype;
639 int err;
640
641 mmc->card_caps = 0;
642
Thomas Choud52ebf12010-12-24 13:12:21 +0000643 if (mmc_host_is_spi(mmc))
644 return 0;
645
Andy Fleming272cc702008-10-30 16:41:01 -0500646 /* Only version 4 supports high-speed */
647 if (mmc->version < MMC_VERSION_4)
648 return 0;
649
Andy Fleming272cc702008-10-30 16:41:01 -0500650 err = mmc_send_ext_csd(mmc, ext_csd);
651
652 if (err)
653 return err;
654
Lei Wen0560db12011-10-03 20:35:10 +0000655 cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -0500656
657 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
658
659 if (err)
660 return err;
661
662 /* Now check to see that it worked */
663 err = mmc_send_ext_csd(mmc, ext_csd);
664
665 if (err)
666 return err;
667
668 /* No high-speed support */
Lei Wen0560db12011-10-03 20:35:10 +0000669 if (!ext_csd[EXT_CSD_HS_TIMING])
Andy Fleming272cc702008-10-30 16:41:01 -0500670 return 0;
671
672 /* High Speed is set, there are two types: 52MHz and 26MHz */
673 if (cardtype & MMC_HS_52MHZ)
674 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
675 else
676 mmc->card_caps |= MMC_MODE_HS;
677
678 return 0;
679}
680
Lei Wenbc897b12011-05-02 16:26:26 +0000681int mmc_switch_part(int dev_num, unsigned int part_num)
682{
683 struct mmc *mmc = find_mmc_device(dev_num);
684
685 if (!mmc)
686 return -1;
687
688 return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
689 (mmc->part_config & ~PART_ACCESS_MASK)
690 | (part_num & PART_ACCESS_MASK));
691}
692
Thierry Reding48972d92012-01-02 01:15:37 +0000693int mmc_getcd(struct mmc *mmc)
694{
695 int cd;
696
697 cd = board_mmc_getcd(mmc);
698
Peter Korsgaardd4e1da42013-03-21 04:00:03 +0000699 if (cd < 0) {
700 if (mmc->getcd)
701 cd = mmc->getcd(mmc);
702 else
703 cd = 1;
704 }
Thierry Reding48972d92012-01-02 01:15:37 +0000705
706 return cd;
707}
708
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000709static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
Andy Fleming272cc702008-10-30 16:41:01 -0500710{
711 struct mmc_cmd cmd;
712 struct mmc_data data;
713
714 /* Switch the frequency */
715 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
716 cmd.resp_type = MMC_RSP_R1;
717 cmd.cmdarg = (mode << 31) | 0xffffff;
718 cmd.cmdarg &= ~(0xf << (group * 4));
719 cmd.cmdarg |= value << (group * 4);
Andy Fleming272cc702008-10-30 16:41:01 -0500720
721 data.dest = (char *)resp;
722 data.blocksize = 64;
723 data.blocks = 1;
724 data.flags = MMC_DATA_READ;
725
726 return mmc_send_cmd(mmc, &cmd, &data);
727}
728
729
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000730static int sd_change_freq(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500731{
732 int err;
733 struct mmc_cmd cmd;
Anton staaff781dd32011-10-03 13:54:59 +0000734 ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
735 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
Andy Fleming272cc702008-10-30 16:41:01 -0500736 struct mmc_data data;
737 int timeout;
738
739 mmc->card_caps = 0;
740
Thomas Choud52ebf12010-12-24 13:12:21 +0000741 if (mmc_host_is_spi(mmc))
742 return 0;
743
Andy Fleming272cc702008-10-30 16:41:01 -0500744 /* Read the SCR to find out if this card supports higher speeds */
745 cmd.cmdidx = MMC_CMD_APP_CMD;
746 cmd.resp_type = MMC_RSP_R1;
747 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -0500748
749 err = mmc_send_cmd(mmc, &cmd, NULL);
750
751 if (err)
752 return err;
753
754 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
755 cmd.resp_type = MMC_RSP_R1;
756 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500757
758 timeout = 3;
759
760retry_scr:
Anton staaff781dd32011-10-03 13:54:59 +0000761 data.dest = (char *)scr;
Andy Fleming272cc702008-10-30 16:41:01 -0500762 data.blocksize = 8;
763 data.blocks = 1;
764 data.flags = MMC_DATA_READ;
765
766 err = mmc_send_cmd(mmc, &cmd, &data);
767
768 if (err) {
769 if (timeout--)
770 goto retry_scr;
771
772 return err;
773 }
774
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300775 mmc->scr[0] = __be32_to_cpu(scr[0]);
776 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Fleming272cc702008-10-30 16:41:01 -0500777
778 switch ((mmc->scr[0] >> 24) & 0xf) {
779 case 0:
780 mmc->version = SD_VERSION_1_0;
781 break;
782 case 1:
783 mmc->version = SD_VERSION_1_10;
784 break;
785 case 2:
786 mmc->version = SD_VERSION_2;
787 break;
788 default:
789 mmc->version = SD_VERSION_1_0;
790 break;
791 }
792
Alagu Sankarb44c7082010-05-12 15:08:24 +0530793 if (mmc->scr[0] & SD_DATA_4BIT)
794 mmc->card_caps |= MMC_MODE_4BIT;
795
Andy Fleming272cc702008-10-30 16:41:01 -0500796 /* Version 1.0 doesn't support switching */
797 if (mmc->version == SD_VERSION_1_0)
798 return 0;
799
800 timeout = 4;
801 while (timeout--) {
802 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
Anton staaff781dd32011-10-03 13:54:59 +0000803 (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -0500804
805 if (err)
806 return err;
807
808 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300809 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Fleming272cc702008-10-30 16:41:01 -0500810 break;
811 }
812
Andy Fleming272cc702008-10-30 16:41:01 -0500813 /* If high-speed isn't supported, we return */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300814 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
Andy Fleming272cc702008-10-30 16:41:01 -0500815 return 0;
816
Macpaul Lin2c3fbf42011-11-28 16:31:09 +0000817 /*
818 * If the host doesn't support SD_HIGHSPEED, do not switch card to
819 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
820 * This can avoid furthur problem when the card runs in different
821 * mode between the host.
822 */
823 if (!((mmc->host_caps & MMC_MODE_HS_52MHz) &&
824 (mmc->host_caps & MMC_MODE_HS)))
825 return 0;
826
Anton staaff781dd32011-10-03 13:54:59 +0000827 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -0500828
829 if (err)
830 return err;
831
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300832 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
Andy Fleming272cc702008-10-30 16:41:01 -0500833 mmc->card_caps |= MMC_MODE_HS;
834
835 return 0;
836}
837
838/* frequency bases */
839/* divided by 10 to be nice to platforms without floating point */
Mike Frysinger5f837c22010-10-20 01:15:53 +0000840static const int fbase[] = {
Andy Fleming272cc702008-10-30 16:41:01 -0500841 10000,
842 100000,
843 1000000,
844 10000000,
845};
846
847/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
848 * to platforms without floating point.
849 */
Mike Frysinger5f837c22010-10-20 01:15:53 +0000850static const int multipliers[] = {
Andy Fleming272cc702008-10-30 16:41:01 -0500851 0, /* reserved */
852 10,
853 12,
854 13,
855 15,
856 20,
857 25,
858 30,
859 35,
860 40,
861 45,
862 50,
863 55,
864 60,
865 70,
866 80,
867};
868
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000869static void mmc_set_ios(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500870{
871 mmc->set_ios(mmc);
872}
873
874void mmc_set_clock(struct mmc *mmc, uint clock)
875{
876 if (clock > mmc->f_max)
877 clock = mmc->f_max;
878
879 if (clock < mmc->f_min)
880 clock = mmc->f_min;
881
882 mmc->clock = clock;
883
884 mmc_set_ios(mmc);
885}
886
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000887static void mmc_set_bus_width(struct mmc *mmc, uint width)
Andy Fleming272cc702008-10-30 16:41:01 -0500888{
889 mmc->bus_width = width;
890
891 mmc_set_ios(mmc);
892}
893
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000894static int mmc_startup(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500895{
Andy Fleming7798f6d2012-10-31 19:02:38 +0000896 int err;
Andy Fleming272cc702008-10-30 16:41:01 -0500897 uint mult, freq;
Yoshihiro Shimoda639b7822011-07-04 22:13:26 +0000898 u64 cmult, csize, capacity;
Andy Fleming272cc702008-10-30 16:41:01 -0500899 struct mmc_cmd cmd;
Yoshihiro Shimodacdfd1ac2012-06-07 19:09:11 +0000900 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, 512);
901 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, 512);
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000902 int timeout = 1000;
Andy Fleming272cc702008-10-30 16:41:01 -0500903
Thomas Choud52ebf12010-12-24 13:12:21 +0000904#ifdef CONFIG_MMC_SPI_CRC_ON
905 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
906 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
907 cmd.resp_type = MMC_RSP_R1;
908 cmd.cmdarg = 1;
Thomas Choud52ebf12010-12-24 13:12:21 +0000909 err = mmc_send_cmd(mmc, &cmd, NULL);
910
911 if (err)
912 return err;
913 }
914#endif
915
Andy Fleming272cc702008-10-30 16:41:01 -0500916 /* Put the Card in Identify Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +0000917 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
918 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
Andy Fleming272cc702008-10-30 16:41:01 -0500919 cmd.resp_type = MMC_RSP_R2;
920 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500921
922 err = mmc_send_cmd(mmc, &cmd, NULL);
923
924 if (err)
925 return err;
926
927 memcpy(mmc->cid, cmd.response, 16);
928
929 /*
930 * For MMC cards, set the Relative Address.
931 * For SD cards, get the Relatvie Address.
932 * This also puts the cards into Standby State
933 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000934 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
935 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
936 cmd.cmdarg = mmc->rca << 16;
937 cmd.resp_type = MMC_RSP_R6;
Andy Fleming272cc702008-10-30 16:41:01 -0500938
Thomas Choud52ebf12010-12-24 13:12:21 +0000939 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -0500940
Thomas Choud52ebf12010-12-24 13:12:21 +0000941 if (err)
942 return err;
Andy Fleming272cc702008-10-30 16:41:01 -0500943
Thomas Choud52ebf12010-12-24 13:12:21 +0000944 if (IS_SD(mmc))
945 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
946 }
Andy Fleming272cc702008-10-30 16:41:01 -0500947
948 /* Get the Card-Specific Data */
949 cmd.cmdidx = MMC_CMD_SEND_CSD;
950 cmd.resp_type = MMC_RSP_R2;
951 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -0500952
953 err = mmc_send_cmd(mmc, &cmd, NULL);
954
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000955 /* Waiting for the ready status */
956 mmc_send_status(mmc, timeout);
957
Andy Fleming272cc702008-10-30 16:41:01 -0500958 if (err)
959 return err;
960
Rabin Vincent998be3d2009-04-05 13:30:56 +0530961 mmc->csd[0] = cmd.response[0];
962 mmc->csd[1] = cmd.response[1];
963 mmc->csd[2] = cmd.response[2];
964 mmc->csd[3] = cmd.response[3];
Andy Fleming272cc702008-10-30 16:41:01 -0500965
966 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincent0b453ff2009-04-05 13:30:55 +0530967 int version = (cmd.response[0] >> 26) & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -0500968
969 switch (version) {
970 case 0:
971 mmc->version = MMC_VERSION_1_2;
972 break;
973 case 1:
974 mmc->version = MMC_VERSION_1_4;
975 break;
976 case 2:
977 mmc->version = MMC_VERSION_2_2;
978 break;
979 case 3:
980 mmc->version = MMC_VERSION_3;
981 break;
982 case 4:
983 mmc->version = MMC_VERSION_4;
984 break;
985 default:
986 mmc->version = MMC_VERSION_1_2;
987 break;
988 }
989 }
990
991 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincent0b453ff2009-04-05 13:30:55 +0530992 freq = fbase[(cmd.response[0] & 0x7)];
993 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Fleming272cc702008-10-30 16:41:01 -0500994
995 mmc->tran_speed = freq * mult;
996
Rabin Vincent998be3d2009-04-05 13:30:56 +0530997 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -0500998
999 if (IS_SD(mmc))
1000 mmc->write_bl_len = mmc->read_bl_len;
1001 else
Rabin Vincent998be3d2009-04-05 13:30:56 +05301002 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -05001003
1004 if (mmc->high_capacity) {
1005 csize = (mmc->csd[1] & 0x3f) << 16
1006 | (mmc->csd[2] & 0xffff0000) >> 16;
1007 cmult = 8;
1008 } else {
1009 csize = (mmc->csd[1] & 0x3ff) << 2
1010 | (mmc->csd[2] & 0xc0000000) >> 30;
1011 cmult = (mmc->csd[2] & 0x00038000) >> 15;
1012 }
1013
1014 mmc->capacity = (csize + 1) << (cmult + 2);
1015 mmc->capacity *= mmc->read_bl_len;
1016
1017 if (mmc->read_bl_len > 512)
1018 mmc->read_bl_len = 512;
1019
1020 if (mmc->write_bl_len > 512)
1021 mmc->write_bl_len = 512;
1022
1023 /* Select the card, and put it into Transfer Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00001024 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1025 cmd.cmdidx = MMC_CMD_SELECT_CARD;
Ajay Bhargavfe8f7062011-10-05 03:13:23 +00001026 cmd.resp_type = MMC_RSP_R1;
Thomas Choud52ebf12010-12-24 13:12:21 +00001027 cmd.cmdarg = mmc->rca << 16;
Thomas Choud52ebf12010-12-24 13:12:21 +00001028 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05001029
Thomas Choud52ebf12010-12-24 13:12:21 +00001030 if (err)
1031 return err;
1032 }
Andy Fleming272cc702008-10-30 16:41:01 -05001033
Lei Wene6f99a52011-06-22 17:03:31 +00001034 /*
1035 * For SD, its erase group is always one sector
1036 */
1037 mmc->erase_grp_size = 1;
Lei Wenbc897b12011-05-02 16:26:26 +00001038 mmc->part_config = MMCPART_NOAVAILABLE;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05301039 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
1040 /* check ext_csd version and capacity */
1041 err = mmc_send_ext_csd(mmc, ext_csd);
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001042 if (!err && (ext_csd[EXT_CSD_REV] >= 2)) {
Yoshihiro Shimoda639b7822011-07-04 22:13:26 +00001043 /*
1044 * According to the JEDEC Standard, the value of
1045 * ext_csd's capacity is valid if the value is more
1046 * than 2GB
1047 */
Lei Wen0560db12011-10-03 20:35:10 +00001048 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
1049 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
1050 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
1051 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
Yoshihiro Shimoda639b7822011-07-04 22:13:26 +00001052 capacity *= 512;
Łukasz Majewskib1f1e8212011-07-05 02:19:44 +00001053 if ((capacity >> 20) > 2 * 1024)
Yoshihiro Shimoda639b7822011-07-04 22:13:26 +00001054 mmc->capacity = capacity;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05301055 }
Lei Wenbc897b12011-05-02 16:26:26 +00001056
Lei Wene6f99a52011-06-22 17:03:31 +00001057 /*
1058 * Check whether GROUP_DEF is set, if yes, read out
1059 * group size from ext_csd directly, or calculate
1060 * the group size from the csd value.
1061 */
Lei Wen0560db12011-10-03 20:35:10 +00001062 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF])
1063 mmc->erase_grp_size =
1064 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 512 * 1024;
Lei Wene6f99a52011-06-22 17:03:31 +00001065 else {
1066 int erase_gsz, erase_gmul;
1067 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1068 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1069 mmc->erase_grp_size = (erase_gsz + 1)
1070 * (erase_gmul + 1);
1071 }
1072
Lei Wenbc897b12011-05-02 16:26:26 +00001073 /* store the partition info of emmc */
Stephen Warren8948ea82012-07-30 10:55:43 +00001074 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
1075 ext_csd[EXT_CSD_BOOT_MULT])
Lei Wen0560db12011-10-03 20:35:10 +00001076 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05301077 }
1078
Andy Fleming272cc702008-10-30 16:41:01 -05001079 if (IS_SD(mmc))
1080 err = sd_change_freq(mmc);
1081 else
1082 err = mmc_change_freq(mmc);
1083
1084 if (err)
1085 return err;
1086
1087 /* Restrict card's capabilities by what the host can do */
1088 mmc->card_caps &= mmc->host_caps;
1089
1090 if (IS_SD(mmc)) {
1091 if (mmc->card_caps & MMC_MODE_4BIT) {
1092 cmd.cmdidx = MMC_CMD_APP_CMD;
1093 cmd.resp_type = MMC_RSP_R1;
1094 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05001095
1096 err = mmc_send_cmd(mmc, &cmd, NULL);
1097 if (err)
1098 return err;
1099
1100 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1101 cmd.resp_type = MMC_RSP_R1;
1102 cmd.cmdarg = 2;
Andy Fleming272cc702008-10-30 16:41:01 -05001103 err = mmc_send_cmd(mmc, &cmd, NULL);
1104 if (err)
1105 return err;
1106
1107 mmc_set_bus_width(mmc, 4);
1108 }
1109
1110 if (mmc->card_caps & MMC_MODE_HS)
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001111 mmc->tran_speed = 50000000;
Andy Fleming272cc702008-10-30 16:41:01 -05001112 else
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001113 mmc->tran_speed = 25000000;
Andy Fleming272cc702008-10-30 16:41:01 -05001114 } else {
Andy Fleming7798f6d2012-10-31 19:02:38 +00001115 int idx;
1116
1117 /* An array of possible bus widths in order of preference */
1118 static unsigned ext_csd_bits[] = {
1119 EXT_CSD_BUS_WIDTH_8,
1120 EXT_CSD_BUS_WIDTH_4,
1121 EXT_CSD_BUS_WIDTH_1,
1122 };
1123
1124 /* An array to map CSD bus widths to host cap bits */
1125 static unsigned ext_to_hostcaps[] = {
1126 [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
1127 [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
1128 };
1129
1130 /* An array to map chosen bus width to an integer */
1131 static unsigned widths[] = {
1132 8, 4, 1,
1133 };
1134
1135 for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
1136 unsigned int extw = ext_csd_bits[idx];
1137
1138 /*
1139 * Check to make sure the controller supports
1140 * this bus width, if it's more than 1
1141 */
1142 if (extw != EXT_CSD_BUS_WIDTH_1 &&
1143 !(mmc->host_caps & ext_to_hostcaps[extw]))
1144 continue;
1145
Andy Fleming272cc702008-10-30 16:41:01 -05001146 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
Andy Fleming7798f6d2012-10-31 19:02:38 +00001147 EXT_CSD_BUS_WIDTH, extw);
Andy Fleming272cc702008-10-30 16:41:01 -05001148
1149 if (err)
Lei Wen41378942011-10-03 20:35:11 +00001150 continue;
Andy Fleming272cc702008-10-30 16:41:01 -05001151
Andy Fleming7798f6d2012-10-31 19:02:38 +00001152 mmc_set_bus_width(mmc, widths[idx]);
Andy Fleming272cc702008-10-30 16:41:01 -05001153
Lei Wen41378942011-10-03 20:35:11 +00001154 err = mmc_send_ext_csd(mmc, test_csd);
1155 if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \
1156 == test_csd[EXT_CSD_PARTITIONING_SUPPORT]
1157 && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \
1158 == test_csd[EXT_CSD_ERASE_GROUP_DEF] \
1159 && ext_csd[EXT_CSD_REV] \
1160 == test_csd[EXT_CSD_REV]
1161 && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \
1162 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1163 && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \
1164 &test_csd[EXT_CSD_SEC_CNT], 4) == 0) {
Andy Fleming272cc702008-10-30 16:41:01 -05001165
Andy Fleming7798f6d2012-10-31 19:02:38 +00001166 mmc->card_caps |= ext_to_hostcaps[extw];
Lei Wen41378942011-10-03 20:35:11 +00001167 break;
1168 }
Andy Fleming272cc702008-10-30 16:41:01 -05001169 }
1170
1171 if (mmc->card_caps & MMC_MODE_HS) {
1172 if (mmc->card_caps & MMC_MODE_HS_52MHz)
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001173 mmc->tran_speed = 52000000;
Andy Fleming272cc702008-10-30 16:41:01 -05001174 else
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001175 mmc->tran_speed = 26000000;
1176 }
Andy Fleming272cc702008-10-30 16:41:01 -05001177 }
1178
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001179 mmc_set_clock(mmc, mmc->tran_speed);
1180
Andy Fleming272cc702008-10-30 16:41:01 -05001181 /* fill in device description */
1182 mmc->block_dev.lun = 0;
1183 mmc->block_dev.type = 0;
1184 mmc->block_dev.blksz = mmc->read_bl_len;
Rabin Vincent9b1f9422009-04-05 13:30:54 +05301185 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
Taylor Huttbabce5f2012-10-20 17:15:59 +00001186 sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x",
1187 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1188 (mmc->cid[3] >> 16) & 0xffff);
1189 sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
1190 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1191 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1192 (mmc->cid[2] >> 24) & 0xff);
1193 sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
1194 (mmc->cid[2] >> 16) & 0xf);
Mikhail Kshevetskiy122efd42012-07-09 08:53:38 +00001195#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
Andy Fleming272cc702008-10-30 16:41:01 -05001196 init_part(&mmc->block_dev);
Mikhail Kshevetskiy122efd42012-07-09 08:53:38 +00001197#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001198
1199 return 0;
1200}
1201
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001202static int mmc_send_if_cond(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001203{
1204 struct mmc_cmd cmd;
1205 int err;
1206
1207 cmd.cmdidx = SD_CMD_SEND_IF_COND;
1208 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1209 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
1210 cmd.resp_type = MMC_RSP_R7;
Andy Fleming272cc702008-10-30 16:41:01 -05001211
1212 err = mmc_send_cmd(mmc, &cmd, NULL);
1213
1214 if (err)
1215 return err;
1216
Rabin Vincent998be3d2009-04-05 13:30:56 +05301217 if ((cmd.response[0] & 0xff) != 0xaa)
Andy Fleming272cc702008-10-30 16:41:01 -05001218 return UNUSABLE_ERR;
1219 else
1220 mmc->version = SD_VERSION_2;
1221
1222 return 0;
1223}
1224
1225int mmc_register(struct mmc *mmc)
1226{
1227 /* Setup the universal parts of the block interface just once */
1228 mmc->block_dev.if_type = IF_TYPE_MMC;
1229 mmc->block_dev.dev = cur_dev_num++;
1230 mmc->block_dev.removable = 1;
1231 mmc->block_dev.block_read = mmc_bread;
1232 mmc->block_dev.block_write = mmc_bwrite;
Lei Wene6f99a52011-06-22 17:03:31 +00001233 mmc->block_dev.block_erase = mmc_berase;
John Rigby8feafcc2011-04-18 05:50:08 +00001234 if (!mmc->b_max)
1235 mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Andy Fleming272cc702008-10-30 16:41:01 -05001236
1237 INIT_LIST_HEAD (&mmc->link);
1238
1239 list_add_tail (&mmc->link, &mmc_devices);
1240
1241 return 0;
1242}
1243
Matthew McClintockdf3fc522011-05-24 05:31:19 +00001244#ifdef CONFIG_PARTITIONS
Andy Fleming272cc702008-10-30 16:41:01 -05001245block_dev_desc_t *mmc_get_dev(int dev)
1246{
1247 struct mmc *mmc = find_mmc_device(dev);
Benoît Thébaudeau6bb4b4b2012-08-10 08:59:12 +00001248 if (!mmc || mmc_init(mmc))
Łukasz Majewski40242bc2012-04-19 02:39:18 +00001249 return NULL;
Andy Fleming272cc702008-10-30 16:41:01 -05001250
Łukasz Majewski40242bc2012-04-19 02:39:18 +00001251 return &mmc->block_dev;
Andy Fleming272cc702008-10-30 16:41:01 -05001252}
Matthew McClintockdf3fc522011-05-24 05:31:19 +00001253#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001254
1255int mmc_init(struct mmc *mmc)
1256{
Macpaul Linafd59322011-11-14 23:35:39 +00001257 int err;
Andy Fleming272cc702008-10-30 16:41:01 -05001258
Thierry Reding48972d92012-01-02 01:15:37 +00001259 if (mmc_getcd(mmc) == 0) {
1260 mmc->has_init = 0;
1261 printf("MMC: no card present\n");
1262 return NO_CARD_ERR;
1263 }
1264
Lei Wenbc897b12011-05-02 16:26:26 +00001265 if (mmc->has_init)
1266 return 0;
1267
Andy Fleming272cc702008-10-30 16:41:01 -05001268 err = mmc->init(mmc);
1269
1270 if (err)
1271 return err;
1272
Ilya Yanokb86b85e2009-06-29 17:53:16 +04001273 mmc_set_bus_width(mmc, 1);
1274 mmc_set_clock(mmc, 1);
1275
Andy Fleming272cc702008-10-30 16:41:01 -05001276 /* Reset the Card */
1277 err = mmc_go_idle(mmc);
1278
1279 if (err)
1280 return err;
1281
Lei Wenbc897b12011-05-02 16:26:26 +00001282 /* The internal partition reset to user partition(0) at every CMD0*/
1283 mmc->part_num = 0;
1284
Andy Fleming272cc702008-10-30 16:41:01 -05001285 /* Test for SD version 2 */
Macpaul Linafd59322011-11-14 23:35:39 +00001286 err = mmc_send_if_cond(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001287
Andy Fleming272cc702008-10-30 16:41:01 -05001288 /* Now try to get the SD card's operating condition */
1289 err = sd_send_op_cond(mmc);
1290
1291 /* If the command timed out, we check for an MMC card */
1292 if (err == TIMEOUT) {
1293 err = mmc_send_op_cond(mmc);
1294
1295 if (err) {
1296 printf("Card did not respond to voltage select!\n");
1297 return UNUSABLE_ERR;
1298 }
1299 }
1300
Lei Wenbc897b12011-05-02 16:26:26 +00001301 err = mmc_startup(mmc);
1302 if (err)
1303 mmc->has_init = 0;
1304 else
1305 mmc->has_init = 1;
1306 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001307}
1308
1309/*
1310 * CPU and board-specific MMC initializations. Aliased function
1311 * signals caller to move on
1312 */
1313static int __def_mmc_init(bd_t *bis)
1314{
1315 return -1;
1316}
1317
Peter Tyserf9a109b2009-04-20 11:08:46 -05001318int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1319int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
Andy Fleming272cc702008-10-30 16:41:01 -05001320
1321void print_mmc_devices(char separator)
1322{
1323 struct mmc *m;
1324 struct list_head *entry;
1325
1326 list_for_each(entry, &mmc_devices) {
1327 m = list_entry(entry, struct mmc, link);
1328
1329 printf("%s: %d", m->name, m->block_dev.dev);
1330
1331 if (entry->next != &mmc_devices)
1332 printf("%c ", separator);
1333 }
1334
1335 printf("\n");
1336}
1337
Lei Wenea6ebe22011-05-02 16:26:25 +00001338int get_mmc_num(void)
1339{
1340 return cur_dev_num;
1341}
1342
Andy Fleming272cc702008-10-30 16:41:01 -05001343int mmc_initialize(bd_t *bis)
1344{
1345 INIT_LIST_HEAD (&mmc_devices);
1346 cur_dev_num = 0;
1347
1348 if (board_mmc_init(bis) < 0)
1349 cpu_mmc_init(bis);
1350
1351 print_mmc_devices(',');
1352
1353 return 0;
1354}