blob: 89b674bb4b5aea673ae3a749f7f6c472092847a5 [file] [log] [blame]
Andy Fleming272cc702008-10-30 16:41:01 -05001/*
2 * Copyright 2008, Freescale Semiconductor, Inc
3 * Andy Fleming
4 *
5 * Based vaguely on the Linux code
6 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
25
26#include <config.h>
27#include <common.h>
28#include <command.h>
29#include <mmc.h>
30#include <part.h>
31#include <malloc.h>
32#include <linux/list.h>
Rabin Vincent9b1f9422009-04-05 13:30:54 +053033#include <div64.h>
Andy Fleming272cc702008-10-30 16:41:01 -050034
Matt Waddelce0fbcd2011-02-24 16:35:23 +000035/* Set block count limit because of 16 bit register limit on some hardware*/
36#ifndef CONFIG_SYS_MMC_MAX_BLK_COUNT
37#define CONFIG_SYS_MMC_MAX_BLK_COUNT 65535
38#endif
39
Andy Fleming272cc702008-10-30 16:41:01 -050040static struct list_head mmc_devices;
41static int cur_dev_num = -1;
42
Thierry Reding314284b2012-01-02 01:15:36 +000043int __board_mmc_getcd(struct mmc *mmc) {
Stefano Babic11fdade2010-02-05 15:04:43 +010044 return -1;
45}
46
Thierry Reding314284b2012-01-02 01:15:36 +000047int board_mmc_getcd(struct mmc *mmc)__attribute__((weak,
Stefano Babic11fdade2010-02-05 15:04:43 +010048 alias("__board_mmc_getcd")));
49
Marek Vasut8635ff92012-03-15 18:41:35 +000050#ifdef CONFIG_MMC_BOUNCE_BUFFER
51static int mmc_bounce_need_bounce(struct mmc_data *orig)
52{
53 ulong addr, len;
54
55 if (orig->flags & MMC_DATA_READ)
56 addr = (ulong)orig->dest;
57 else
58 addr = (ulong)orig->src;
59
60 if (addr % ARCH_DMA_MINALIGN) {
61 debug("MMC: Unaligned data destination address %08lx!\n", addr);
62 return 1;
63 }
64
65 len = (ulong)(orig->blocksize * orig->blocks);
66 if (len % ARCH_DMA_MINALIGN) {
67 debug("MMC: Unaligned data destination length %08lx!\n", len);
68 return 1;
69 }
70
71 return 0;
72}
73
74static int mmc_bounce_buffer_start(struct mmc_data *backup,
75 struct mmc_data *orig)
76{
77 ulong origlen, len;
78 void *buffer;
79
80 if (!orig)
81 return 0;
82
83 if (!mmc_bounce_need_bounce(orig))
84 return 0;
85
86 memcpy(backup, orig, sizeof(struct mmc_data));
87
88 origlen = orig->blocksize * orig->blocks;
89 len = roundup(origlen, ARCH_DMA_MINALIGN);
90 buffer = memalign(ARCH_DMA_MINALIGN, len);
91 if (!buffer) {
92 puts("MMC: Error allocating MMC bounce buffer!\n");
93 return 1;
94 }
95
96 if (orig->flags & MMC_DATA_READ) {
97 orig->dest = buffer;
98 } else {
99 memcpy(buffer, orig->src, origlen);
100 orig->src = buffer;
101 }
102
103 return 0;
104}
105
106static void mmc_bounce_buffer_stop(struct mmc_data *backup,
107 struct mmc_data *orig)
108{
109 ulong len;
110
111 if (!orig)
112 return;
113
114 if (!mmc_bounce_need_bounce(backup))
115 return;
116
117 if (backup->flags & MMC_DATA_READ) {
118 len = backup->blocksize * backup->blocks;
119 memcpy(backup->dest, orig->dest, len);
120 free(orig->dest);
121 orig->dest = backup->dest;
122 } else {
123 free((void *)orig->src);
124 orig->src = backup->src;
125 }
126
127 return;
128
129}
130#else
131static inline int mmc_bounce_buffer_start(struct mmc_data *backup,
Anatolij Gustschindc3faf02012-03-28 21:24:32 +0000132 struct mmc_data *orig) { return 0; }
Marek Vasut8635ff92012-03-15 18:41:35 +0000133static inline void mmc_bounce_buffer_stop(struct mmc_data *backup,
134 struct mmc_data *orig) { }
135#endif
136
Andy Fleming272cc702008-10-30 16:41:01 -0500137int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
138{
Marek Vasut8635ff92012-03-15 18:41:35 +0000139 struct mmc_data backup;
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000140 int ret;
Marek Vasut8635ff92012-03-15 18:41:35 +0000141
142 memset(&backup, 0, sizeof(backup));
143
144 ret = mmc_bounce_buffer_start(&backup, data);
145 if (ret)
146 return ret;
147
148#ifdef CONFIG_MMC_TRACE
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000149 int i;
150 u8 *ptr;
151
152 printf("CMD_SEND:%d\n", cmd->cmdidx);
153 printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000154 ret = mmc->send_cmd(mmc, cmd, data);
155 switch (cmd->resp_type) {
156 case MMC_RSP_NONE:
157 printf("\t\tMMC_RSP_NONE\n");
158 break;
159 case MMC_RSP_R1:
160 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
161 cmd->response[0]);
162 break;
163 case MMC_RSP_R1b:
164 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
165 cmd->response[0]);
166 break;
167 case MMC_RSP_R2:
168 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
169 cmd->response[0]);
170 printf("\t\t \t\t 0x%08X \n",
171 cmd->response[1]);
172 printf("\t\t \t\t 0x%08X \n",
173 cmd->response[2]);
174 printf("\t\t \t\t 0x%08X \n",
175 cmd->response[3]);
176 printf("\n");
177 printf("\t\t\t\t\tDUMPING DATA\n");
178 for (i = 0; i < 4; i++) {
179 int j;
180 printf("\t\t\t\t\t%03d - ", i*4);
Dirk Behme146bec72012-03-08 02:35:34 +0000181 ptr = (u8 *)&cmd->response[i];
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000182 ptr += 3;
183 for (j = 0; j < 4; j++)
184 printf("%02X ", *ptr--);
185 printf("\n");
186 }
187 break;
188 case MMC_RSP_R3:
189 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
190 cmd->response[0]);
191 break;
192 default:
193 printf("\t\tERROR MMC rsp not supported\n");
194 break;
195 }
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000196#else
Marek Vasut8635ff92012-03-15 18:41:35 +0000197 ret = mmc->send_cmd(mmc, cmd, data);
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000198#endif
Marek Vasut8635ff92012-03-15 18:41:35 +0000199 mmc_bounce_buffer_stop(&backup, data);
200 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500201}
202
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000203int mmc_send_status(struct mmc *mmc, int timeout)
204{
205 struct mmc_cmd cmd;
Jan Kloetzked617c422012-02-05 22:29:12 +0000206 int err, retries = 5;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000207#ifdef CONFIG_MMC_TRACE
208 int status;
209#endif
210
211 cmd.cmdidx = MMC_CMD_SEND_STATUS;
212 cmd.resp_type = MMC_RSP_R1;
Marek Vasutaaf3d412011-08-10 09:24:48 +0200213 if (!mmc_host_is_spi(mmc))
214 cmd.cmdarg = mmc->rca << 16;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000215
216 do {
217 err = mmc_send_cmd(mmc, &cmd, NULL);
Jan Kloetzked617c422012-02-05 22:29:12 +0000218 if (!err) {
219 if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
220 (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
221 MMC_STATE_PRG)
222 break;
223 else if (cmd.response[0] & MMC_STATUS_MASK) {
224 printf("Status Error: 0x%08X\n",
225 cmd.response[0]);
226 return COMM_ERR;
227 }
228 } else if (--retries < 0)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000229 return err;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000230
231 udelay(1000);
232
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000233 } while (timeout--);
234
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000235#ifdef CONFIG_MMC_TRACE
236 status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
237 printf("CURR STATE:%d\n", status);
238#endif
Jongman Heo5b0c9422012-06-03 21:32:13 +0000239 if (timeout <= 0) {
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000240 printf("Timeout waiting card ready\n");
241 return TIMEOUT;
242 }
243
244 return 0;
245}
246
Andy Fleming272cc702008-10-30 16:41:01 -0500247int mmc_set_blocklen(struct mmc *mmc, int len)
248{
249 struct mmc_cmd cmd;
250
251 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
252 cmd.resp_type = MMC_RSP_R1;
253 cmd.cmdarg = len;
Andy Fleming272cc702008-10-30 16:41:01 -0500254
255 return mmc_send_cmd(mmc, &cmd, NULL);
256}
257
258struct mmc *find_mmc_device(int dev_num)
259{
260 struct mmc *m;
261 struct list_head *entry;
262
263 list_for_each(entry, &mmc_devices) {
264 m = list_entry(entry, struct mmc, link);
265
266 if (m->block_dev.dev == dev_num)
267 return m;
268 }
269
270 printf("MMC Device %d not found\n", dev_num);
271
272 return NULL;
273}
274
Lei Wene6f99a52011-06-22 17:03:31 +0000275static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
276{
277 struct mmc_cmd cmd;
278 ulong end;
279 int err, start_cmd, end_cmd;
280
281 if (mmc->high_capacity)
282 end = start + blkcnt - 1;
283 else {
284 end = (start + blkcnt - 1) * mmc->write_bl_len;
285 start *= mmc->write_bl_len;
286 }
287
288 if (IS_SD(mmc)) {
289 start_cmd = SD_CMD_ERASE_WR_BLK_START;
290 end_cmd = SD_CMD_ERASE_WR_BLK_END;
291 } else {
292 start_cmd = MMC_CMD_ERASE_GROUP_START;
293 end_cmd = MMC_CMD_ERASE_GROUP_END;
294 }
295
296 cmd.cmdidx = start_cmd;
297 cmd.cmdarg = start;
298 cmd.resp_type = MMC_RSP_R1;
Lei Wene6f99a52011-06-22 17:03:31 +0000299
300 err = mmc_send_cmd(mmc, &cmd, NULL);
301 if (err)
302 goto err_out;
303
304 cmd.cmdidx = end_cmd;
305 cmd.cmdarg = end;
306
307 err = mmc_send_cmd(mmc, &cmd, NULL);
308 if (err)
309 goto err_out;
310
311 cmd.cmdidx = MMC_CMD_ERASE;
312 cmd.cmdarg = SECURE_ERASE;
313 cmd.resp_type = MMC_RSP_R1b;
314
315 err = mmc_send_cmd(mmc, &cmd, NULL);
316 if (err)
317 goto err_out;
318
319 return 0;
320
321err_out:
322 puts("mmc erase failed\n");
323 return err;
324}
325
326static unsigned long
327mmc_berase(int dev_num, unsigned long start, lbaint_t blkcnt)
328{
329 int err = 0;
330 struct mmc *mmc = find_mmc_device(dev_num);
331 lbaint_t blk = 0, blk_r = 0;
Jerry Huangd2d8afa2012-05-17 23:00:51 +0000332 int timeout = 1000;
Lei Wene6f99a52011-06-22 17:03:31 +0000333
334 if (!mmc)
335 return -1;
336
337 if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size))
338 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
339 "The erase range would be change to 0x%lx~0x%lx\n\n",
340 mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
341 ((start + blkcnt + mmc->erase_grp_size)
342 & ~(mmc->erase_grp_size - 1)) - 1);
343
344 while (blk < blkcnt) {
345 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
346 mmc->erase_grp_size : (blkcnt - blk);
347 err = mmc_erase_t(mmc, start + blk, blk_r);
348 if (err)
349 break;
350
351 blk += blk_r;
Jerry Huangd2d8afa2012-05-17 23:00:51 +0000352
353 /* Waiting for the ready status */
354 if (mmc_send_status(mmc, timeout))
355 return 0;
Lei Wene6f99a52011-06-22 17:03:31 +0000356 }
357
358 return blk;
359}
360
Andy Fleming272cc702008-10-30 16:41:01 -0500361static ulong
Lei Wen01581262010-10-14 13:38:11 +0800362mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
Andy Fleming272cc702008-10-30 16:41:01 -0500363{
364 struct mmc_cmd cmd;
365 struct mmc_data data;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000366 int timeout = 1000;
Andy Fleming272cc702008-10-30 16:41:01 -0500367
Lei Wend2bf29e2010-09-13 22:07:27 +0800368 if ((start + blkcnt) > mmc->block_dev.lba) {
Steve Sakomandef412b2010-10-28 09:00:26 -0700369 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
Lei Wend2bf29e2010-09-13 22:07:27 +0800370 start + blkcnt, mmc->block_dev.lba);
371 return 0;
372 }
Andy Fleming272cc702008-10-30 16:41:01 -0500373
374 if (blkcnt > 1)
375 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
376 else
377 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
378
379 if (mmc->high_capacity)
380 cmd.cmdarg = start;
381 else
Steve Sakomandef412b2010-10-28 09:00:26 -0700382 cmd.cmdarg = start * mmc->write_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500383
384 cmd.resp_type = MMC_RSP_R1;
Andy Fleming272cc702008-10-30 16:41:01 -0500385
386 data.src = src;
387 data.blocks = blkcnt;
Steve Sakomandef412b2010-10-28 09:00:26 -0700388 data.blocksize = mmc->write_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500389 data.flags = MMC_DATA_WRITE;
390
Steve Sakomandef412b2010-10-28 09:00:26 -0700391 if (mmc_send_cmd(mmc, &cmd, &data)) {
392 printf("mmc write failed\n");
393 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500394 }
395
Thomas Choud52ebf12010-12-24 13:12:21 +0000396 /* SPI multiblock writes terminate using a special
397 * token, not a STOP_TRANSMISSION request.
398 */
399 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
Andy Fleming272cc702008-10-30 16:41:01 -0500400 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
401 cmd.cmdarg = 0;
402 cmd.resp_type = MMC_RSP_R1b;
Steve Sakomandef412b2010-10-28 09:00:26 -0700403 if (mmc_send_cmd(mmc, &cmd, NULL)) {
404 printf("mmc fail to send stop cmd\n");
405 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800406 }
Andy Fleming272cc702008-10-30 16:41:01 -0500407 }
408
Jan Kloetzke93ad0d12012-02-05 22:29:11 +0000409 /* Waiting for the ready status */
410 if (mmc_send_status(mmc, timeout))
411 return 0;
412
Andy Fleming272cc702008-10-30 16:41:01 -0500413 return blkcnt;
414}
415
Lei Wen01581262010-10-14 13:38:11 +0800416static ulong
417mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
418{
Lei Wen01581262010-10-14 13:38:11 +0800419 lbaint_t cur, blocks_todo = blkcnt;
420
Steve Sakomandef412b2010-10-28 09:00:26 -0700421 struct mmc *mmc = find_mmc_device(dev_num);
Lei Wen01581262010-10-14 13:38:11 +0800422 if (!mmc)
Steve Sakomandef412b2010-10-28 09:00:26 -0700423 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800424
Steve Sakomandef412b2010-10-28 09:00:26 -0700425 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
426 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800427
428 do {
John Rigby8feafcc2011-04-18 05:50:08 +0000429 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
Lei Wen01581262010-10-14 13:38:11 +0800430 if(mmc_write_blocks(mmc, start, cur, src) != cur)
Steve Sakomandef412b2010-10-28 09:00:26 -0700431 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800432 blocks_todo -= cur;
433 start += cur;
434 src += cur * mmc->write_bl_len;
435 } while (blocks_todo > 0);
436
437 return blkcnt;
438}
439
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700440int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt)
Andy Fleming272cc702008-10-30 16:41:01 -0500441{
442 struct mmc_cmd cmd;
443 struct mmc_data data;
444
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700445 if (blkcnt > 1)
446 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
447 else
448 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Fleming272cc702008-10-30 16:41:01 -0500449
450 if (mmc->high_capacity)
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700451 cmd.cmdarg = start;
Andy Fleming272cc702008-10-30 16:41:01 -0500452 else
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700453 cmd.cmdarg = start * mmc->read_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500454
455 cmd.resp_type = MMC_RSP_R1;
Andy Fleming272cc702008-10-30 16:41:01 -0500456
457 data.dest = dst;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700458 data.blocks = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500459 data.blocksize = mmc->read_bl_len;
460 data.flags = MMC_DATA_READ;
461
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700462 if (mmc_send_cmd(mmc, &cmd, &data))
463 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500464
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700465 if (blkcnt > 1) {
466 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
467 cmd.cmdarg = 0;
468 cmd.resp_type = MMC_RSP_R1b;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700469 if (mmc_send_cmd(mmc, &cmd, NULL)) {
470 printf("mmc fail to send stop cmd\n");
471 return 0;
472 }
Andy Fleming272cc702008-10-30 16:41:01 -0500473 }
474
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700475 return blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500476}
477
478static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
479{
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700480 lbaint_t cur, blocks_todo = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500481
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700482 if (blkcnt == 0)
483 return 0;
484
485 struct mmc *mmc = find_mmc_device(dev_num);
Andy Fleming272cc702008-10-30 16:41:01 -0500486 if (!mmc)
487 return 0;
488
Lei Wend2bf29e2010-09-13 22:07:27 +0800489 if ((start + blkcnt) > mmc->block_dev.lba) {
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700490 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
Lei Wend2bf29e2010-09-13 22:07:27 +0800491 start + blkcnt, mmc->block_dev.lba);
492 return 0;
493 }
Andy Fleming272cc702008-10-30 16:41:01 -0500494
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700495 if (mmc_set_blocklen(mmc, mmc->read_bl_len))
Andy Fleming272cc702008-10-30 16:41:01 -0500496 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500497
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700498 do {
John Rigby8feafcc2011-04-18 05:50:08 +0000499 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700500 if(mmc_read_blocks(mmc, dst, start, cur) != cur)
501 return 0;
502 blocks_todo -= cur;
503 start += cur;
504 dst += cur * mmc->read_bl_len;
505 } while (blocks_todo > 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500506
507 return blkcnt;
508}
509
510int mmc_go_idle(struct mmc* mmc)
511{
512 struct mmc_cmd cmd;
513 int err;
514
515 udelay(1000);
516
517 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
518 cmd.cmdarg = 0;
519 cmd.resp_type = MMC_RSP_NONE;
Andy Fleming272cc702008-10-30 16:41:01 -0500520
521 err = mmc_send_cmd(mmc, &cmd, NULL);
522
523 if (err)
524 return err;
525
526 udelay(2000);
527
528 return 0;
529}
530
531int
532sd_send_op_cond(struct mmc *mmc)
533{
534 int timeout = 1000;
535 int err;
536 struct mmc_cmd cmd;
537
538 do {
539 cmd.cmdidx = MMC_CMD_APP_CMD;
540 cmd.resp_type = MMC_RSP_R1;
541 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500542
543 err = mmc_send_cmd(mmc, &cmd, NULL);
544
545 if (err)
546 return err;
547
548 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
549 cmd.resp_type = MMC_RSP_R3;
Stefano Babic250de122010-01-20 18:20:39 +0100550
551 /*
552 * Most cards do not answer if some reserved bits
553 * in the ocr are set. However, Some controller
554 * can set bit 7 (reserved for low voltages), but
555 * how to manage low voltages SD card is not yet
556 * specified.
557 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000558 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
559 (mmc->voltages & 0xff8000);
Andy Fleming272cc702008-10-30 16:41:01 -0500560
561 if (mmc->version == SD_VERSION_2)
562 cmd.cmdarg |= OCR_HCS;
563
564 err = mmc_send_cmd(mmc, &cmd, NULL);
565
566 if (err)
567 return err;
568
569 udelay(1000);
570 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
571
572 if (timeout <= 0)
573 return UNUSABLE_ERR;
574
575 if (mmc->version != SD_VERSION_2)
576 mmc->version = SD_VERSION_1_0;
577
Thomas Choud52ebf12010-12-24 13:12:21 +0000578 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
579 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
580 cmd.resp_type = MMC_RSP_R3;
581 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000582
583 err = mmc_send_cmd(mmc, &cmd, NULL);
584
585 if (err)
586 return err;
587 }
588
Rabin Vincent998be3d2009-04-05 13:30:56 +0530589 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500590
591 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
592 mmc->rca = 0;
593
594 return 0;
595}
596
597int mmc_send_op_cond(struct mmc *mmc)
598{
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000599 int timeout = 10000;
Andy Fleming272cc702008-10-30 16:41:01 -0500600 struct mmc_cmd cmd;
601 int err;
602
603 /* Some cards seem to need this */
604 mmc_go_idle(mmc);
605
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000606 /* Asking to the card its capabilities */
607 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
608 cmd.resp_type = MMC_RSP_R3;
609 cmd.cmdarg = 0;
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200610
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000611 err = mmc_send_cmd(mmc, &cmd, NULL);
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200612
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000613 if (err)
614 return err;
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200615
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000616 udelay(1000);
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200617
Andy Fleming272cc702008-10-30 16:41:01 -0500618 do {
619 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
620 cmd.resp_type = MMC_RSP_R3;
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000621 cmd.cmdarg = (mmc_host_is_spi(mmc) ? 0 :
622 (mmc->voltages &
623 (cmd.response[0] & OCR_VOLTAGE_MASK)) |
624 (cmd.response[0] & OCR_ACCESS_MODE));
Łukasz Majewskib1f1e8212011-07-05 02:19:44 +0000625
626 if (mmc->host_caps & MMC_MODE_HC)
627 cmd.cmdarg |= OCR_HCS;
628
Andy Fleming272cc702008-10-30 16:41:01 -0500629 err = mmc_send_cmd(mmc, &cmd, NULL);
630
631 if (err)
632 return err;
633
634 udelay(1000);
635 } while (!(cmd.response[0] & OCR_BUSY) && timeout--);
636
637 if (timeout <= 0)
638 return UNUSABLE_ERR;
639
Thomas Choud52ebf12010-12-24 13:12:21 +0000640 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
641 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
642 cmd.resp_type = MMC_RSP_R3;
643 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000644
645 err = mmc_send_cmd(mmc, &cmd, NULL);
646
647 if (err)
648 return err;
649 }
650
Andy Fleming272cc702008-10-30 16:41:01 -0500651 mmc->version = MMC_VERSION_UNKNOWN;
Rabin Vincent998be3d2009-04-05 13:30:56 +0530652 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500653
654 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
655 mmc->rca = 0;
656
657 return 0;
658}
659
660
661int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd)
662{
663 struct mmc_cmd cmd;
664 struct mmc_data data;
665 int err;
666
667 /* Get the Card Status Register */
668 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
669 cmd.resp_type = MMC_RSP_R1;
670 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500671
672 data.dest = ext_csd;
673 data.blocks = 1;
674 data.blocksize = 512;
675 data.flags = MMC_DATA_READ;
676
677 err = mmc_send_cmd(mmc, &cmd, &data);
678
679 return err;
680}
681
682
683int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
684{
685 struct mmc_cmd cmd;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000686 int timeout = 1000;
687 int ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500688
689 cmd.cmdidx = MMC_CMD_SWITCH;
690 cmd.resp_type = MMC_RSP_R1b;
691 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000692 (index << 16) |
693 (value << 8);
Andy Fleming272cc702008-10-30 16:41:01 -0500694
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000695 ret = mmc_send_cmd(mmc, &cmd, NULL);
696
697 /* Waiting for the ready status */
Jan Kloetzke93ad0d12012-02-05 22:29:11 +0000698 if (!ret)
699 ret = mmc_send_status(mmc, timeout);
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000700
701 return ret;
702
Andy Fleming272cc702008-10-30 16:41:01 -0500703}
704
705int mmc_change_freq(struct mmc *mmc)
706{
Anton staafa1969922011-10-04 11:24:50 +0000707 ALLOC_CACHE_ALIGN_BUFFER(char, ext_csd, 512);
Andy Fleming272cc702008-10-30 16:41:01 -0500708 char cardtype;
709 int err;
710
711 mmc->card_caps = 0;
712
Thomas Choud52ebf12010-12-24 13:12:21 +0000713 if (mmc_host_is_spi(mmc))
714 return 0;
715
Andy Fleming272cc702008-10-30 16:41:01 -0500716 /* Only version 4 supports high-speed */
717 if (mmc->version < MMC_VERSION_4)
718 return 0;
719
Andy Fleming272cc702008-10-30 16:41:01 -0500720 err = mmc_send_ext_csd(mmc, ext_csd);
721
722 if (err)
723 return err;
724
Lei Wen0560db12011-10-03 20:35:10 +0000725 cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -0500726
727 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
728
729 if (err)
730 return err;
731
732 /* Now check to see that it worked */
733 err = mmc_send_ext_csd(mmc, ext_csd);
734
735 if (err)
736 return err;
737
738 /* No high-speed support */
Lei Wen0560db12011-10-03 20:35:10 +0000739 if (!ext_csd[EXT_CSD_HS_TIMING])
Andy Fleming272cc702008-10-30 16:41:01 -0500740 return 0;
741
742 /* High Speed is set, there are two types: 52MHz and 26MHz */
743 if (cardtype & MMC_HS_52MHZ)
744 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
745 else
746 mmc->card_caps |= MMC_MODE_HS;
747
748 return 0;
749}
750
Lei Wenbc897b12011-05-02 16:26:26 +0000751int mmc_switch_part(int dev_num, unsigned int part_num)
752{
753 struct mmc *mmc = find_mmc_device(dev_num);
754
755 if (!mmc)
756 return -1;
757
758 return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
759 (mmc->part_config & ~PART_ACCESS_MASK)
760 | (part_num & PART_ACCESS_MASK));
761}
762
Thierry Reding48972d92012-01-02 01:15:37 +0000763int mmc_getcd(struct mmc *mmc)
764{
765 int cd;
766
767 cd = board_mmc_getcd(mmc);
768
769 if ((cd < 0) && mmc->getcd)
770 cd = mmc->getcd(mmc);
771
772 return cd;
773}
774
Andy Fleming272cc702008-10-30 16:41:01 -0500775int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
776{
777 struct mmc_cmd cmd;
778 struct mmc_data data;
779
780 /* Switch the frequency */
781 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
782 cmd.resp_type = MMC_RSP_R1;
783 cmd.cmdarg = (mode << 31) | 0xffffff;
784 cmd.cmdarg &= ~(0xf << (group * 4));
785 cmd.cmdarg |= value << (group * 4);
Andy Fleming272cc702008-10-30 16:41:01 -0500786
787 data.dest = (char *)resp;
788 data.blocksize = 64;
789 data.blocks = 1;
790 data.flags = MMC_DATA_READ;
791
792 return mmc_send_cmd(mmc, &cmd, &data);
793}
794
795
796int sd_change_freq(struct mmc *mmc)
797{
798 int err;
799 struct mmc_cmd cmd;
Anton staaff781dd32011-10-03 13:54:59 +0000800 ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
801 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
Andy Fleming272cc702008-10-30 16:41:01 -0500802 struct mmc_data data;
803 int timeout;
804
805 mmc->card_caps = 0;
806
Thomas Choud52ebf12010-12-24 13:12:21 +0000807 if (mmc_host_is_spi(mmc))
808 return 0;
809
Andy Fleming272cc702008-10-30 16:41:01 -0500810 /* Read the SCR to find out if this card supports higher speeds */
811 cmd.cmdidx = MMC_CMD_APP_CMD;
812 cmd.resp_type = MMC_RSP_R1;
813 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -0500814
815 err = mmc_send_cmd(mmc, &cmd, NULL);
816
817 if (err)
818 return err;
819
820 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
821 cmd.resp_type = MMC_RSP_R1;
822 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500823
824 timeout = 3;
825
826retry_scr:
Anton staaff781dd32011-10-03 13:54:59 +0000827 data.dest = (char *)scr;
Andy Fleming272cc702008-10-30 16:41:01 -0500828 data.blocksize = 8;
829 data.blocks = 1;
830 data.flags = MMC_DATA_READ;
831
832 err = mmc_send_cmd(mmc, &cmd, &data);
833
834 if (err) {
835 if (timeout--)
836 goto retry_scr;
837
838 return err;
839 }
840
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300841 mmc->scr[0] = __be32_to_cpu(scr[0]);
842 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Fleming272cc702008-10-30 16:41:01 -0500843
844 switch ((mmc->scr[0] >> 24) & 0xf) {
845 case 0:
846 mmc->version = SD_VERSION_1_0;
847 break;
848 case 1:
849 mmc->version = SD_VERSION_1_10;
850 break;
851 case 2:
852 mmc->version = SD_VERSION_2;
853 break;
854 default:
855 mmc->version = SD_VERSION_1_0;
856 break;
857 }
858
Alagu Sankarb44c7082010-05-12 15:08:24 +0530859 if (mmc->scr[0] & SD_DATA_4BIT)
860 mmc->card_caps |= MMC_MODE_4BIT;
861
Andy Fleming272cc702008-10-30 16:41:01 -0500862 /* Version 1.0 doesn't support switching */
863 if (mmc->version == SD_VERSION_1_0)
864 return 0;
865
866 timeout = 4;
867 while (timeout--) {
868 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
Anton staaff781dd32011-10-03 13:54:59 +0000869 (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -0500870
871 if (err)
872 return err;
873
874 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300875 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Fleming272cc702008-10-30 16:41:01 -0500876 break;
877 }
878
Andy Fleming272cc702008-10-30 16:41:01 -0500879 /* If high-speed isn't supported, we return */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300880 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
Andy Fleming272cc702008-10-30 16:41:01 -0500881 return 0;
882
Macpaul Lin2c3fbf42011-11-28 16:31:09 +0000883 /*
884 * If the host doesn't support SD_HIGHSPEED, do not switch card to
885 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
886 * This can avoid furthur problem when the card runs in different
887 * mode between the host.
888 */
889 if (!((mmc->host_caps & MMC_MODE_HS_52MHz) &&
890 (mmc->host_caps & MMC_MODE_HS)))
891 return 0;
892
Anton staaff781dd32011-10-03 13:54:59 +0000893 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -0500894
895 if (err)
896 return err;
897
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300898 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
Andy Fleming272cc702008-10-30 16:41:01 -0500899 mmc->card_caps |= MMC_MODE_HS;
900
901 return 0;
902}
903
904/* frequency bases */
905/* divided by 10 to be nice to platforms without floating point */
Mike Frysinger5f837c22010-10-20 01:15:53 +0000906static const int fbase[] = {
Andy Fleming272cc702008-10-30 16:41:01 -0500907 10000,
908 100000,
909 1000000,
910 10000000,
911};
912
913/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
914 * to platforms without floating point.
915 */
Mike Frysinger5f837c22010-10-20 01:15:53 +0000916static const int multipliers[] = {
Andy Fleming272cc702008-10-30 16:41:01 -0500917 0, /* reserved */
918 10,
919 12,
920 13,
921 15,
922 20,
923 25,
924 30,
925 35,
926 40,
927 45,
928 50,
929 55,
930 60,
931 70,
932 80,
933};
934
935void mmc_set_ios(struct mmc *mmc)
936{
937 mmc->set_ios(mmc);
938}
939
940void mmc_set_clock(struct mmc *mmc, uint clock)
941{
942 if (clock > mmc->f_max)
943 clock = mmc->f_max;
944
945 if (clock < mmc->f_min)
946 clock = mmc->f_min;
947
948 mmc->clock = clock;
949
950 mmc_set_ios(mmc);
951}
952
953void mmc_set_bus_width(struct mmc *mmc, uint width)
954{
955 mmc->bus_width = width;
956
957 mmc_set_ios(mmc);
958}
959
960int mmc_startup(struct mmc *mmc)
961{
Lei Wen41378942011-10-03 20:35:11 +0000962 int err, width;
Andy Fleming272cc702008-10-30 16:41:01 -0500963 uint mult, freq;
Yoshihiro Shimoda639b7822011-07-04 22:13:26 +0000964 u64 cmult, csize, capacity;
Andy Fleming272cc702008-10-30 16:41:01 -0500965 struct mmc_cmd cmd;
Anton staafa1969922011-10-04 11:24:50 +0000966 ALLOC_CACHE_ALIGN_BUFFER(char, ext_csd, 512);
Lei Wen41378942011-10-03 20:35:11 +0000967 ALLOC_CACHE_ALIGN_BUFFER(char, test_csd, 512);
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000968 int timeout = 1000;
Andy Fleming272cc702008-10-30 16:41:01 -0500969
Thomas Choud52ebf12010-12-24 13:12:21 +0000970#ifdef CONFIG_MMC_SPI_CRC_ON
971 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
972 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
973 cmd.resp_type = MMC_RSP_R1;
974 cmd.cmdarg = 1;
Thomas Choud52ebf12010-12-24 13:12:21 +0000975 err = mmc_send_cmd(mmc, &cmd, NULL);
976
977 if (err)
978 return err;
979 }
980#endif
981
Andy Fleming272cc702008-10-30 16:41:01 -0500982 /* Put the Card in Identify Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +0000983 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
984 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
Andy Fleming272cc702008-10-30 16:41:01 -0500985 cmd.resp_type = MMC_RSP_R2;
986 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500987
988 err = mmc_send_cmd(mmc, &cmd, NULL);
989
990 if (err)
991 return err;
992
993 memcpy(mmc->cid, cmd.response, 16);
994
995 /*
996 * For MMC cards, set the Relative Address.
997 * For SD cards, get the Relatvie Address.
998 * This also puts the cards into Standby State
999 */
Thomas Choud52ebf12010-12-24 13:12:21 +00001000 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1001 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
1002 cmd.cmdarg = mmc->rca << 16;
1003 cmd.resp_type = MMC_RSP_R6;
Andy Fleming272cc702008-10-30 16:41:01 -05001004
Thomas Choud52ebf12010-12-24 13:12:21 +00001005 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05001006
Thomas Choud52ebf12010-12-24 13:12:21 +00001007 if (err)
1008 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001009
Thomas Choud52ebf12010-12-24 13:12:21 +00001010 if (IS_SD(mmc))
1011 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
1012 }
Andy Fleming272cc702008-10-30 16:41:01 -05001013
1014 /* Get the Card-Specific Data */
1015 cmd.cmdidx = MMC_CMD_SEND_CSD;
1016 cmd.resp_type = MMC_RSP_R2;
1017 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05001018
1019 err = mmc_send_cmd(mmc, &cmd, NULL);
1020
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +00001021 /* Waiting for the ready status */
1022 mmc_send_status(mmc, timeout);
1023
Andy Fleming272cc702008-10-30 16:41:01 -05001024 if (err)
1025 return err;
1026
Rabin Vincent998be3d2009-04-05 13:30:56 +05301027 mmc->csd[0] = cmd.response[0];
1028 mmc->csd[1] = cmd.response[1];
1029 mmc->csd[2] = cmd.response[2];
1030 mmc->csd[3] = cmd.response[3];
Andy Fleming272cc702008-10-30 16:41:01 -05001031
1032 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincent0b453ff2009-04-05 13:30:55 +05301033 int version = (cmd.response[0] >> 26) & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -05001034
1035 switch (version) {
1036 case 0:
1037 mmc->version = MMC_VERSION_1_2;
1038 break;
1039 case 1:
1040 mmc->version = MMC_VERSION_1_4;
1041 break;
1042 case 2:
1043 mmc->version = MMC_VERSION_2_2;
1044 break;
1045 case 3:
1046 mmc->version = MMC_VERSION_3;
1047 break;
1048 case 4:
1049 mmc->version = MMC_VERSION_4;
1050 break;
1051 default:
1052 mmc->version = MMC_VERSION_1_2;
1053 break;
1054 }
1055 }
1056
1057 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincent0b453ff2009-04-05 13:30:55 +05301058 freq = fbase[(cmd.response[0] & 0x7)];
1059 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Fleming272cc702008-10-30 16:41:01 -05001060
1061 mmc->tran_speed = freq * mult;
1062
Rabin Vincent998be3d2009-04-05 13:30:56 +05301063 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -05001064
1065 if (IS_SD(mmc))
1066 mmc->write_bl_len = mmc->read_bl_len;
1067 else
Rabin Vincent998be3d2009-04-05 13:30:56 +05301068 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -05001069
1070 if (mmc->high_capacity) {
1071 csize = (mmc->csd[1] & 0x3f) << 16
1072 | (mmc->csd[2] & 0xffff0000) >> 16;
1073 cmult = 8;
1074 } else {
1075 csize = (mmc->csd[1] & 0x3ff) << 2
1076 | (mmc->csd[2] & 0xc0000000) >> 30;
1077 cmult = (mmc->csd[2] & 0x00038000) >> 15;
1078 }
1079
1080 mmc->capacity = (csize + 1) << (cmult + 2);
1081 mmc->capacity *= mmc->read_bl_len;
1082
1083 if (mmc->read_bl_len > 512)
1084 mmc->read_bl_len = 512;
1085
1086 if (mmc->write_bl_len > 512)
1087 mmc->write_bl_len = 512;
1088
1089 /* Select the card, and put it into Transfer Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00001090 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1091 cmd.cmdidx = MMC_CMD_SELECT_CARD;
Ajay Bhargavfe8f7062011-10-05 03:13:23 +00001092 cmd.resp_type = MMC_RSP_R1;
Thomas Choud52ebf12010-12-24 13:12:21 +00001093 cmd.cmdarg = mmc->rca << 16;
Thomas Choud52ebf12010-12-24 13:12:21 +00001094 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05001095
Thomas Choud52ebf12010-12-24 13:12:21 +00001096 if (err)
1097 return err;
1098 }
Andy Fleming272cc702008-10-30 16:41:01 -05001099
Lei Wene6f99a52011-06-22 17:03:31 +00001100 /*
1101 * For SD, its erase group is always one sector
1102 */
1103 mmc->erase_grp_size = 1;
Lei Wenbc897b12011-05-02 16:26:26 +00001104 mmc->part_config = MMCPART_NOAVAILABLE;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05301105 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
1106 /* check ext_csd version and capacity */
1107 err = mmc_send_ext_csd(mmc, ext_csd);
Lei Wen0560db12011-10-03 20:35:10 +00001108 if (!err & (ext_csd[EXT_CSD_REV] >= 2)) {
Yoshihiro Shimoda639b7822011-07-04 22:13:26 +00001109 /*
1110 * According to the JEDEC Standard, the value of
1111 * ext_csd's capacity is valid if the value is more
1112 * than 2GB
1113 */
Lei Wen0560db12011-10-03 20:35:10 +00001114 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
1115 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
1116 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
1117 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
Yoshihiro Shimoda639b7822011-07-04 22:13:26 +00001118 capacity *= 512;
Łukasz Majewskib1f1e8212011-07-05 02:19:44 +00001119 if ((capacity >> 20) > 2 * 1024)
Yoshihiro Shimoda639b7822011-07-04 22:13:26 +00001120 mmc->capacity = capacity;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05301121 }
Lei Wenbc897b12011-05-02 16:26:26 +00001122
Lei Wene6f99a52011-06-22 17:03:31 +00001123 /*
1124 * Check whether GROUP_DEF is set, if yes, read out
1125 * group size from ext_csd directly, or calculate
1126 * the group size from the csd value.
1127 */
Lei Wen0560db12011-10-03 20:35:10 +00001128 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF])
1129 mmc->erase_grp_size =
1130 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 512 * 1024;
Lei Wene6f99a52011-06-22 17:03:31 +00001131 else {
1132 int erase_gsz, erase_gmul;
1133 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1134 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1135 mmc->erase_grp_size = (erase_gsz + 1)
1136 * (erase_gmul + 1);
1137 }
1138
Lei Wenbc897b12011-05-02 16:26:26 +00001139 /* store the partition info of emmc */
Lei Wen0560db12011-10-03 20:35:10 +00001140 if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT)
1141 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05301142 }
1143
Andy Fleming272cc702008-10-30 16:41:01 -05001144 if (IS_SD(mmc))
1145 err = sd_change_freq(mmc);
1146 else
1147 err = mmc_change_freq(mmc);
1148
1149 if (err)
1150 return err;
1151
1152 /* Restrict card's capabilities by what the host can do */
1153 mmc->card_caps &= mmc->host_caps;
1154
1155 if (IS_SD(mmc)) {
1156 if (mmc->card_caps & MMC_MODE_4BIT) {
1157 cmd.cmdidx = MMC_CMD_APP_CMD;
1158 cmd.resp_type = MMC_RSP_R1;
1159 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05001160
1161 err = mmc_send_cmd(mmc, &cmd, NULL);
1162 if (err)
1163 return err;
1164
1165 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1166 cmd.resp_type = MMC_RSP_R1;
1167 cmd.cmdarg = 2;
Andy Fleming272cc702008-10-30 16:41:01 -05001168 err = mmc_send_cmd(mmc, &cmd, NULL);
1169 if (err)
1170 return err;
1171
1172 mmc_set_bus_width(mmc, 4);
1173 }
1174
1175 if (mmc->card_caps & MMC_MODE_HS)
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001176 mmc->tran_speed = 50000000;
Andy Fleming272cc702008-10-30 16:41:01 -05001177 else
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001178 mmc->tran_speed = 25000000;
Andy Fleming272cc702008-10-30 16:41:01 -05001179 } else {
Łukasz Majewski62722032012-03-12 22:07:18 +00001180 width = ((mmc->host_caps & MMC_MODE_MASK_WIDTH_BITS) >>
1181 MMC_MODE_WIDTH_BITS_SHIFT);
1182 for (; width >= 0; width--) {
Andy Fleming272cc702008-10-30 16:41:01 -05001183 /* Set the card to use 4 bit*/
1184 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
Lei Wen41378942011-10-03 20:35:11 +00001185 EXT_CSD_BUS_WIDTH, width);
Andy Fleming272cc702008-10-30 16:41:01 -05001186
1187 if (err)
Lei Wen41378942011-10-03 20:35:11 +00001188 continue;
Andy Fleming272cc702008-10-30 16:41:01 -05001189
Lei Wen41378942011-10-03 20:35:11 +00001190 if (!width) {
1191 mmc_set_bus_width(mmc, 1);
1192 break;
1193 } else
1194 mmc_set_bus_width(mmc, 4 * width);
Andy Fleming272cc702008-10-30 16:41:01 -05001195
Lei Wen41378942011-10-03 20:35:11 +00001196 err = mmc_send_ext_csd(mmc, test_csd);
1197 if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \
1198 == test_csd[EXT_CSD_PARTITIONING_SUPPORT]
1199 && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \
1200 == test_csd[EXT_CSD_ERASE_GROUP_DEF] \
1201 && ext_csd[EXT_CSD_REV] \
1202 == test_csd[EXT_CSD_REV]
1203 && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \
1204 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1205 && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \
1206 &test_csd[EXT_CSD_SEC_CNT], 4) == 0) {
Andy Fleming272cc702008-10-30 16:41:01 -05001207
Lei Wen41378942011-10-03 20:35:11 +00001208 mmc->card_caps |= width;
1209 break;
1210 }
Andy Fleming272cc702008-10-30 16:41:01 -05001211 }
1212
1213 if (mmc->card_caps & MMC_MODE_HS) {
1214 if (mmc->card_caps & MMC_MODE_HS_52MHz)
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001215 mmc->tran_speed = 52000000;
Andy Fleming272cc702008-10-30 16:41:01 -05001216 else
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001217 mmc->tran_speed = 26000000;
1218 }
Andy Fleming272cc702008-10-30 16:41:01 -05001219 }
1220
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001221 mmc_set_clock(mmc, mmc->tran_speed);
1222
Andy Fleming272cc702008-10-30 16:41:01 -05001223 /* fill in device description */
1224 mmc->block_dev.lun = 0;
1225 mmc->block_dev.type = 0;
1226 mmc->block_dev.blksz = mmc->read_bl_len;
Rabin Vincent9b1f9422009-04-05 13:30:54 +05301227 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
Rabin Vincent0b453ff2009-04-05 13:30:55 +05301228 sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8,
1229 (mmc->cid[2] << 8) | (mmc->cid[3] >> 24));
1230 sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff,
1231 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1232 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
1233 sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28,
1234 (mmc->cid[2] >> 24) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -05001235 init_part(&mmc->block_dev);
1236
1237 return 0;
1238}
1239
1240int mmc_send_if_cond(struct mmc *mmc)
1241{
1242 struct mmc_cmd cmd;
1243 int err;
1244
1245 cmd.cmdidx = SD_CMD_SEND_IF_COND;
1246 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1247 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
1248 cmd.resp_type = MMC_RSP_R7;
Andy Fleming272cc702008-10-30 16:41:01 -05001249
1250 err = mmc_send_cmd(mmc, &cmd, NULL);
1251
1252 if (err)
1253 return err;
1254
Rabin Vincent998be3d2009-04-05 13:30:56 +05301255 if ((cmd.response[0] & 0xff) != 0xaa)
Andy Fleming272cc702008-10-30 16:41:01 -05001256 return UNUSABLE_ERR;
1257 else
1258 mmc->version = SD_VERSION_2;
1259
1260 return 0;
1261}
1262
1263int mmc_register(struct mmc *mmc)
1264{
1265 /* Setup the universal parts of the block interface just once */
1266 mmc->block_dev.if_type = IF_TYPE_MMC;
1267 mmc->block_dev.dev = cur_dev_num++;
1268 mmc->block_dev.removable = 1;
1269 mmc->block_dev.block_read = mmc_bread;
1270 mmc->block_dev.block_write = mmc_bwrite;
Lei Wene6f99a52011-06-22 17:03:31 +00001271 mmc->block_dev.block_erase = mmc_berase;
John Rigby8feafcc2011-04-18 05:50:08 +00001272 if (!mmc->b_max)
1273 mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Andy Fleming272cc702008-10-30 16:41:01 -05001274
1275 INIT_LIST_HEAD (&mmc->link);
1276
1277 list_add_tail (&mmc->link, &mmc_devices);
1278
1279 return 0;
1280}
1281
Matthew McClintockdf3fc522011-05-24 05:31:19 +00001282#ifdef CONFIG_PARTITIONS
Andy Fleming272cc702008-10-30 16:41:01 -05001283block_dev_desc_t *mmc_get_dev(int dev)
1284{
1285 struct mmc *mmc = find_mmc_device(dev);
Łukasz Majewski40242bc2012-04-19 02:39:18 +00001286 if (!mmc)
1287 return NULL;
Andy Fleming272cc702008-10-30 16:41:01 -05001288
Łukasz Majewski40242bc2012-04-19 02:39:18 +00001289 mmc_init(mmc);
1290 return &mmc->block_dev;
Andy Fleming272cc702008-10-30 16:41:01 -05001291}
Matthew McClintockdf3fc522011-05-24 05:31:19 +00001292#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001293
1294int mmc_init(struct mmc *mmc)
1295{
Macpaul Linafd59322011-11-14 23:35:39 +00001296 int err;
Andy Fleming272cc702008-10-30 16:41:01 -05001297
Thierry Reding48972d92012-01-02 01:15:37 +00001298 if (mmc_getcd(mmc) == 0) {
1299 mmc->has_init = 0;
1300 printf("MMC: no card present\n");
1301 return NO_CARD_ERR;
1302 }
1303
Lei Wenbc897b12011-05-02 16:26:26 +00001304 if (mmc->has_init)
1305 return 0;
1306
Andy Fleming272cc702008-10-30 16:41:01 -05001307 err = mmc->init(mmc);
1308
1309 if (err)
1310 return err;
1311
Ilya Yanokb86b85e2009-06-29 17:53:16 +04001312 mmc_set_bus_width(mmc, 1);
1313 mmc_set_clock(mmc, 1);
1314
Andy Fleming272cc702008-10-30 16:41:01 -05001315 /* Reset the Card */
1316 err = mmc_go_idle(mmc);
1317
1318 if (err)
1319 return err;
1320
Lei Wenbc897b12011-05-02 16:26:26 +00001321 /* The internal partition reset to user partition(0) at every CMD0*/
1322 mmc->part_num = 0;
1323
Andy Fleming272cc702008-10-30 16:41:01 -05001324 /* Test for SD version 2 */
Macpaul Linafd59322011-11-14 23:35:39 +00001325 err = mmc_send_if_cond(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001326
Andy Fleming272cc702008-10-30 16:41:01 -05001327 /* Now try to get the SD card's operating condition */
1328 err = sd_send_op_cond(mmc);
1329
1330 /* If the command timed out, we check for an MMC card */
1331 if (err == TIMEOUT) {
1332 err = mmc_send_op_cond(mmc);
1333
1334 if (err) {
1335 printf("Card did not respond to voltage select!\n");
1336 return UNUSABLE_ERR;
1337 }
1338 }
1339
Lei Wenbc897b12011-05-02 16:26:26 +00001340 err = mmc_startup(mmc);
1341 if (err)
1342 mmc->has_init = 0;
1343 else
1344 mmc->has_init = 1;
1345 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001346}
1347
1348/*
1349 * CPU and board-specific MMC initializations. Aliased function
1350 * signals caller to move on
1351 */
1352static int __def_mmc_init(bd_t *bis)
1353{
1354 return -1;
1355}
1356
Peter Tyserf9a109b2009-04-20 11:08:46 -05001357int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1358int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
Andy Fleming272cc702008-10-30 16:41:01 -05001359
1360void print_mmc_devices(char separator)
1361{
1362 struct mmc *m;
1363 struct list_head *entry;
1364
1365 list_for_each(entry, &mmc_devices) {
1366 m = list_entry(entry, struct mmc, link);
1367
1368 printf("%s: %d", m->name, m->block_dev.dev);
1369
1370 if (entry->next != &mmc_devices)
1371 printf("%c ", separator);
1372 }
1373
1374 printf("\n");
1375}
1376
Lei Wenea6ebe22011-05-02 16:26:25 +00001377int get_mmc_num(void)
1378{
1379 return cur_dev_num;
1380}
1381
Andy Fleming272cc702008-10-30 16:41:01 -05001382int mmc_initialize(bd_t *bis)
1383{
1384 INIT_LIST_HEAD (&mmc_devices);
1385 cur_dev_num = 0;
1386
1387 if (board_mmc_init(bis) < 0)
1388 cpu_mmc_init(bis);
1389
1390 print_mmc_devices(',');
1391
1392 return 0;
1393}