blob: 4da8db9bed2d1ef0e4f4d4b3460d8689eeeeb9e8 [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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Andy Fleming272cc702008-10-30 16:41:01 -05008 */
9
10#include <config.h>
11#include <common.h>
12#include <command.h>
13#include <mmc.h>
14#include <part.h>
15#include <malloc.h>
16#include <linux/list.h>
Rabin Vincent9b1f9422009-04-05 13:30:54 +053017#include <div64.h>
Andy Fleming272cc702008-10-30 16:41:01 -050018
Matt Waddelce0fbcd2011-02-24 16:35:23 +000019/* Set block count limit because of 16 bit register limit on some hardware*/
20#ifndef CONFIG_SYS_MMC_MAX_BLK_COUNT
21#define CONFIG_SYS_MMC_MAX_BLK_COUNT 65535
22#endif
23
Andy Fleming272cc702008-10-30 16:41:01 -050024static struct list_head mmc_devices;
25static int cur_dev_num = -1;
26
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +000027int __weak board_mmc_getwp(struct mmc *mmc)
28{
29 return -1;
30}
31
32int mmc_getwp(struct mmc *mmc)
33{
34 int wp;
35
36 wp = board_mmc_getwp(mmc);
37
Peter Korsgaardd4e1da42013-03-21 04:00:03 +000038 if (wp < 0) {
39 if (mmc->getwp)
40 wp = mmc->getwp(mmc);
41 else
42 wp = 0;
43 }
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +000044
45 return wp;
46}
47
Thierry Reding314284b2012-01-02 01:15:36 +000048int __board_mmc_getcd(struct mmc *mmc) {
Stefano Babic11fdade2010-02-05 15:04:43 +010049 return -1;
50}
51
Thierry Reding314284b2012-01-02 01:15:36 +000052int board_mmc_getcd(struct mmc *mmc)__attribute__((weak,
Stefano Babic11fdade2010-02-05 15:04:43 +010053 alias("__board_mmc_getcd")));
54
Kim Phillipsfdbb8732012-10-29 13:34:43 +000055static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
56 struct mmc_data *data)
Andy Fleming272cc702008-10-30 16:41:01 -050057{
Marek Vasut8635ff92012-03-15 18:41:35 +000058 struct mmc_data backup;
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +000059 int ret;
Marek Vasut8635ff92012-03-15 18:41:35 +000060
61 memset(&backup, 0, sizeof(backup));
62
Marek Vasut8635ff92012-03-15 18:41:35 +000063#ifdef CONFIG_MMC_TRACE
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +000064 int i;
65 u8 *ptr;
66
67 printf("CMD_SEND:%d\n", cmd->cmdidx);
68 printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +000069 ret = mmc->send_cmd(mmc, cmd, data);
70 switch (cmd->resp_type) {
71 case MMC_RSP_NONE:
72 printf("\t\tMMC_RSP_NONE\n");
73 break;
74 case MMC_RSP_R1:
75 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
76 cmd->response[0]);
77 break;
78 case MMC_RSP_R1b:
79 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
80 cmd->response[0]);
81 break;
82 case MMC_RSP_R2:
83 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
84 cmd->response[0]);
85 printf("\t\t \t\t 0x%08X \n",
86 cmd->response[1]);
87 printf("\t\t \t\t 0x%08X \n",
88 cmd->response[2]);
89 printf("\t\t \t\t 0x%08X \n",
90 cmd->response[3]);
91 printf("\n");
92 printf("\t\t\t\t\tDUMPING DATA\n");
93 for (i = 0; i < 4; i++) {
94 int j;
95 printf("\t\t\t\t\t%03d - ", i*4);
Dirk Behme146bec72012-03-08 02:35:34 +000096 ptr = (u8 *)&cmd->response[i];
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +000097 ptr += 3;
98 for (j = 0; j < 4; j++)
99 printf("%02X ", *ptr--);
100 printf("\n");
101 }
102 break;
103 case MMC_RSP_R3:
104 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
105 cmd->response[0]);
106 break;
107 default:
108 printf("\t\tERROR MMC rsp not supported\n");
109 break;
110 }
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000111#else
Marek Vasut8635ff92012-03-15 18:41:35 +0000112 ret = mmc->send_cmd(mmc, cmd, data);
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000113#endif
Marek Vasut8635ff92012-03-15 18:41:35 +0000114 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500115}
116
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000117static int mmc_send_status(struct mmc *mmc, int timeout)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000118{
119 struct mmc_cmd cmd;
Jan Kloetzked617c422012-02-05 22:29:12 +0000120 int err, retries = 5;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000121#ifdef CONFIG_MMC_TRACE
122 int status;
123#endif
124
125 cmd.cmdidx = MMC_CMD_SEND_STATUS;
126 cmd.resp_type = MMC_RSP_R1;
Marek Vasutaaf3d412011-08-10 09:24:48 +0200127 if (!mmc_host_is_spi(mmc))
128 cmd.cmdarg = mmc->rca << 16;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000129
130 do {
131 err = mmc_send_cmd(mmc, &cmd, NULL);
Jan Kloetzked617c422012-02-05 22:29:12 +0000132 if (!err) {
133 if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
134 (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
135 MMC_STATE_PRG)
136 break;
137 else if (cmd.response[0] & MMC_STATUS_MASK) {
138 printf("Status Error: 0x%08X\n",
139 cmd.response[0]);
140 return COMM_ERR;
141 }
142 } else if (--retries < 0)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000143 return err;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000144
145 udelay(1000);
146
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000147 } while (timeout--);
148
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000149#ifdef CONFIG_MMC_TRACE
150 status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
151 printf("CURR STATE:%d\n", status);
152#endif
Jongman Heo5b0c9422012-06-03 21:32:13 +0000153 if (timeout <= 0) {
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000154 printf("Timeout waiting card ready\n");
155 return TIMEOUT;
156 }
157
158 return 0;
159}
160
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000161static int mmc_set_blocklen(struct mmc *mmc, int len)
Andy Fleming272cc702008-10-30 16:41:01 -0500162{
163 struct mmc_cmd cmd;
164
165 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
166 cmd.resp_type = MMC_RSP_R1;
167 cmd.cmdarg = len;
Andy Fleming272cc702008-10-30 16:41:01 -0500168
169 return mmc_send_cmd(mmc, &cmd, NULL);
170}
171
172struct mmc *find_mmc_device(int dev_num)
173{
174 struct mmc *m;
175 struct list_head *entry;
176
177 list_for_each(entry, &mmc_devices) {
178 m = list_entry(entry, struct mmc, link);
179
180 if (m->block_dev.dev == dev_num)
181 return m;
182 }
183
184 printf("MMC Device %d not found\n", dev_num);
185
186 return NULL;
187}
188
Lei Wene6f99a52011-06-22 17:03:31 +0000189static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
190{
191 struct mmc_cmd cmd;
192 ulong end;
193 int err, start_cmd, end_cmd;
194
195 if (mmc->high_capacity)
196 end = start + blkcnt - 1;
197 else {
198 end = (start + blkcnt - 1) * mmc->write_bl_len;
199 start *= mmc->write_bl_len;
200 }
201
202 if (IS_SD(mmc)) {
203 start_cmd = SD_CMD_ERASE_WR_BLK_START;
204 end_cmd = SD_CMD_ERASE_WR_BLK_END;
205 } else {
206 start_cmd = MMC_CMD_ERASE_GROUP_START;
207 end_cmd = MMC_CMD_ERASE_GROUP_END;
208 }
209
210 cmd.cmdidx = start_cmd;
211 cmd.cmdarg = start;
212 cmd.resp_type = MMC_RSP_R1;
Lei Wene6f99a52011-06-22 17:03:31 +0000213
214 err = mmc_send_cmd(mmc, &cmd, NULL);
215 if (err)
216 goto err_out;
217
218 cmd.cmdidx = end_cmd;
219 cmd.cmdarg = end;
220
221 err = mmc_send_cmd(mmc, &cmd, NULL);
222 if (err)
223 goto err_out;
224
225 cmd.cmdidx = MMC_CMD_ERASE;
226 cmd.cmdarg = SECURE_ERASE;
227 cmd.resp_type = MMC_RSP_R1b;
228
229 err = mmc_send_cmd(mmc, &cmd, NULL);
230 if (err)
231 goto err_out;
232
233 return 0;
234
235err_out:
236 puts("mmc erase failed\n");
237 return err;
238}
239
240static unsigned long
Sascha Silbeff8fef52013-06-14 13:07:25 +0200241mmc_berase(int dev_num, lbaint_t start, lbaint_t blkcnt)
Lei Wene6f99a52011-06-22 17:03:31 +0000242{
243 int err = 0;
244 struct mmc *mmc = find_mmc_device(dev_num);
245 lbaint_t blk = 0, blk_r = 0;
Jerry Huangd2d8afa2012-05-17 23:00:51 +0000246 int timeout = 1000;
Lei Wene6f99a52011-06-22 17:03:31 +0000247
248 if (!mmc)
249 return -1;
250
251 if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size))
252 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
Sascha Silbeff8fef52013-06-14 13:07:25 +0200253 "The erase range would be change to "
254 "0x" LBAF "~0x" LBAF "\n\n",
Lei Wene6f99a52011-06-22 17:03:31 +0000255 mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
256 ((start + blkcnt + mmc->erase_grp_size)
257 & ~(mmc->erase_grp_size - 1)) - 1);
258
259 while (blk < blkcnt) {
260 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
261 mmc->erase_grp_size : (blkcnt - blk);
262 err = mmc_erase_t(mmc, start + blk, blk_r);
263 if (err)
264 break;
265
266 blk += blk_r;
Jerry Huangd2d8afa2012-05-17 23:00:51 +0000267
268 /* Waiting for the ready status */
269 if (mmc_send_status(mmc, timeout))
270 return 0;
Lei Wene6f99a52011-06-22 17:03:31 +0000271 }
272
273 return blk;
274}
275
Andy Fleming272cc702008-10-30 16:41:01 -0500276static ulong
Sascha Silbeff8fef52013-06-14 13:07:25 +0200277mmc_write_blocks(struct mmc *mmc, lbaint_t start, lbaint_t blkcnt, const void*src)
Andy Fleming272cc702008-10-30 16:41:01 -0500278{
279 struct mmc_cmd cmd;
280 struct mmc_data data;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000281 int timeout = 1000;
Andy Fleming272cc702008-10-30 16:41:01 -0500282
Lei Wend2bf29e2010-09-13 22:07:27 +0800283 if ((start + blkcnt) > mmc->block_dev.lba) {
Sascha Silbeff8fef52013-06-14 13:07:25 +0200284 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
Lei Wend2bf29e2010-09-13 22:07:27 +0800285 start + blkcnt, mmc->block_dev.lba);
286 return 0;
287 }
Andy Fleming272cc702008-10-30 16:41:01 -0500288
Ruud Commandeura586c0a2013-05-22 13:19:43 +0200289 if (blkcnt == 0)
290 return 0;
291 else if (blkcnt == 1)
Andy Fleming272cc702008-10-30 16:41:01 -0500292 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
Ruud Commandeura586c0a2013-05-22 13:19:43 +0200293 else
294 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
Andy Fleming272cc702008-10-30 16:41:01 -0500295
296 if (mmc->high_capacity)
297 cmd.cmdarg = start;
298 else
Steve Sakomandef412b2010-10-28 09:00:26 -0700299 cmd.cmdarg = start * mmc->write_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500300
301 cmd.resp_type = MMC_RSP_R1;
Andy Fleming272cc702008-10-30 16:41:01 -0500302
303 data.src = src;
304 data.blocks = blkcnt;
Steve Sakomandef412b2010-10-28 09:00:26 -0700305 data.blocksize = mmc->write_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500306 data.flags = MMC_DATA_WRITE;
307
Steve Sakomandef412b2010-10-28 09:00:26 -0700308 if (mmc_send_cmd(mmc, &cmd, &data)) {
309 printf("mmc write failed\n");
310 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500311 }
312
Thomas Choud52ebf12010-12-24 13:12:21 +0000313 /* SPI multiblock writes terminate using a special
314 * token, not a STOP_TRANSMISSION request.
315 */
316 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
Andy Fleming272cc702008-10-30 16:41:01 -0500317 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
318 cmd.cmdarg = 0;
319 cmd.resp_type = MMC_RSP_R1b;
Steve Sakomandef412b2010-10-28 09:00:26 -0700320 if (mmc_send_cmd(mmc, &cmd, NULL)) {
321 printf("mmc fail to send stop cmd\n");
322 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800323 }
Andy Fleming272cc702008-10-30 16:41:01 -0500324 }
325
Jan Kloetzke93ad0d12012-02-05 22:29:11 +0000326 /* Waiting for the ready status */
327 if (mmc_send_status(mmc, timeout))
328 return 0;
329
Andy Fleming272cc702008-10-30 16:41:01 -0500330 return blkcnt;
331}
332
Lei Wen01581262010-10-14 13:38:11 +0800333static ulong
Sascha Silbeff8fef52013-06-14 13:07:25 +0200334mmc_bwrite(int dev_num, lbaint_t start, lbaint_t blkcnt, const void*src)
Lei Wen01581262010-10-14 13:38:11 +0800335{
Lei Wen01581262010-10-14 13:38:11 +0800336 lbaint_t cur, blocks_todo = blkcnt;
337
Steve Sakomandef412b2010-10-28 09:00:26 -0700338 struct mmc *mmc = find_mmc_device(dev_num);
Lei Wen01581262010-10-14 13:38:11 +0800339 if (!mmc)
Steve Sakomandef412b2010-10-28 09:00:26 -0700340 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800341
Steve Sakomandef412b2010-10-28 09:00:26 -0700342 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
343 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800344
345 do {
John Rigby8feafcc2011-04-18 05:50:08 +0000346 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
Lei Wen01581262010-10-14 13:38:11 +0800347 if(mmc_write_blocks(mmc, start, cur, src) != cur)
Steve Sakomandef412b2010-10-28 09:00:26 -0700348 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800349 blocks_todo -= cur;
350 start += cur;
351 src += cur * mmc->write_bl_len;
352 } while (blocks_todo > 0);
353
354 return blkcnt;
355}
356
Sascha Silbeff8fef52013-06-14 13:07:25 +0200357static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000358 lbaint_t blkcnt)
Andy Fleming272cc702008-10-30 16:41:01 -0500359{
360 struct mmc_cmd cmd;
361 struct mmc_data data;
362
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700363 if (blkcnt > 1)
364 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
365 else
366 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Fleming272cc702008-10-30 16:41:01 -0500367
368 if (mmc->high_capacity)
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700369 cmd.cmdarg = start;
Andy Fleming272cc702008-10-30 16:41:01 -0500370 else
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700371 cmd.cmdarg = start * mmc->read_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500372
373 cmd.resp_type = MMC_RSP_R1;
Andy Fleming272cc702008-10-30 16:41:01 -0500374
375 data.dest = dst;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700376 data.blocks = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500377 data.blocksize = mmc->read_bl_len;
378 data.flags = MMC_DATA_READ;
379
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700380 if (mmc_send_cmd(mmc, &cmd, &data))
381 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500382
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700383 if (blkcnt > 1) {
384 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
385 cmd.cmdarg = 0;
386 cmd.resp_type = MMC_RSP_R1b;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700387 if (mmc_send_cmd(mmc, &cmd, NULL)) {
388 printf("mmc fail to send stop cmd\n");
389 return 0;
390 }
Andy Fleming272cc702008-10-30 16:41:01 -0500391 }
392
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700393 return blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500394}
395
Sascha Silbeff8fef52013-06-14 13:07:25 +0200396static ulong mmc_bread(int dev_num, lbaint_t start, lbaint_t blkcnt, void *dst)
Andy Fleming272cc702008-10-30 16:41:01 -0500397{
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700398 lbaint_t cur, blocks_todo = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500399
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700400 if (blkcnt == 0)
401 return 0;
402
403 struct mmc *mmc = find_mmc_device(dev_num);
Andy Fleming272cc702008-10-30 16:41:01 -0500404 if (!mmc)
405 return 0;
406
Lei Wend2bf29e2010-09-13 22:07:27 +0800407 if ((start + blkcnt) > mmc->block_dev.lba) {
Sascha Silbeff8fef52013-06-14 13:07:25 +0200408 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
Lei Wend2bf29e2010-09-13 22:07:27 +0800409 start + blkcnt, mmc->block_dev.lba);
410 return 0;
411 }
Andy Fleming272cc702008-10-30 16:41:01 -0500412
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700413 if (mmc_set_blocklen(mmc, mmc->read_bl_len))
Andy Fleming272cc702008-10-30 16:41:01 -0500414 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500415
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700416 do {
John Rigby8feafcc2011-04-18 05:50:08 +0000417 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700418 if(mmc_read_blocks(mmc, dst, start, cur) != cur)
419 return 0;
420 blocks_todo -= cur;
421 start += cur;
422 dst += cur * mmc->read_bl_len;
423 } while (blocks_todo > 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500424
425 return blkcnt;
426}
427
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000428static int mmc_go_idle(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500429{
430 struct mmc_cmd cmd;
431 int err;
432
433 udelay(1000);
434
435 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
436 cmd.cmdarg = 0;
437 cmd.resp_type = MMC_RSP_NONE;
Andy Fleming272cc702008-10-30 16:41:01 -0500438
439 err = mmc_send_cmd(mmc, &cmd, NULL);
440
441 if (err)
442 return err;
443
444 udelay(2000);
445
446 return 0;
447}
448
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000449static int sd_send_op_cond(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500450{
451 int timeout = 1000;
452 int err;
453 struct mmc_cmd cmd;
454
455 do {
456 cmd.cmdidx = MMC_CMD_APP_CMD;
457 cmd.resp_type = MMC_RSP_R1;
458 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500459
460 err = mmc_send_cmd(mmc, &cmd, NULL);
461
462 if (err)
463 return err;
464
465 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
466 cmd.resp_type = MMC_RSP_R3;
Stefano Babic250de122010-01-20 18:20:39 +0100467
468 /*
469 * Most cards do not answer if some reserved bits
470 * in the ocr are set. However, Some controller
471 * can set bit 7 (reserved for low voltages), but
472 * how to manage low voltages SD card is not yet
473 * specified.
474 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000475 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
476 (mmc->voltages & 0xff8000);
Andy Fleming272cc702008-10-30 16:41:01 -0500477
478 if (mmc->version == SD_VERSION_2)
479 cmd.cmdarg |= OCR_HCS;
480
481 err = mmc_send_cmd(mmc, &cmd, NULL);
482
483 if (err)
484 return err;
485
486 udelay(1000);
487 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
488
489 if (timeout <= 0)
490 return UNUSABLE_ERR;
491
492 if (mmc->version != SD_VERSION_2)
493 mmc->version = SD_VERSION_1_0;
494
Thomas Choud52ebf12010-12-24 13:12:21 +0000495 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
496 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
497 cmd.resp_type = MMC_RSP_R3;
498 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000499
500 err = mmc_send_cmd(mmc, &cmd, NULL);
501
502 if (err)
503 return err;
504 }
505
Rabin Vincent998be3d2009-04-05 13:30:56 +0530506 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500507
508 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
509 mmc->rca = 0;
510
511 return 0;
512}
513
Che-Liang Chioue9550442012-11-28 15:21:13 +0000514/* We pass in the cmd since otherwise the init seems to fail */
515static int mmc_send_op_cond_iter(struct mmc *mmc, struct mmc_cmd *cmd,
516 int use_arg)
Andy Fleming272cc702008-10-30 16:41:01 -0500517{
Andy Fleming272cc702008-10-30 16:41:01 -0500518 int err;
519
Che-Liang Chioue9550442012-11-28 15:21:13 +0000520 cmd->cmdidx = MMC_CMD_SEND_OP_COND;
521 cmd->resp_type = MMC_RSP_R3;
522 cmd->cmdarg = 0;
523 if (use_arg && !mmc_host_is_spi(mmc)) {
524 cmd->cmdarg =
525 (mmc->voltages &
526 (mmc->op_cond_response & OCR_VOLTAGE_MASK)) |
527 (mmc->op_cond_response & OCR_ACCESS_MODE);
528
529 if (mmc->host_caps & MMC_MODE_HC)
530 cmd->cmdarg |= OCR_HCS;
531 }
532 err = mmc_send_cmd(mmc, cmd, NULL);
533 if (err)
534 return err;
535 mmc->op_cond_response = cmd->response[0];
536 return 0;
537}
538
539int mmc_send_op_cond(struct mmc *mmc)
540{
541 struct mmc_cmd cmd;
542 int err, i;
543
Andy Fleming272cc702008-10-30 16:41:01 -0500544 /* Some cards seem to need this */
545 mmc_go_idle(mmc);
546
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000547 /* Asking to the card its capabilities */
Che-Liang Chioue9550442012-11-28 15:21:13 +0000548 mmc->op_cond_pending = 1;
549 for (i = 0; i < 2; i++) {
550 err = mmc_send_op_cond_iter(mmc, &cmd, i != 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500551 if (err)
552 return err;
553
Che-Liang Chioue9550442012-11-28 15:21:13 +0000554 /* exit if not busy (flag seems to be inverted) */
555 if (mmc->op_cond_response & OCR_BUSY)
556 return 0;
557 }
558 return IN_PROGRESS;
559}
Andy Fleming272cc702008-10-30 16:41:01 -0500560
Che-Liang Chioue9550442012-11-28 15:21:13 +0000561int mmc_complete_op_cond(struct mmc *mmc)
562{
563 struct mmc_cmd cmd;
564 int timeout = 1000;
565 uint start;
566 int err;
567
568 mmc->op_cond_pending = 0;
569 start = get_timer(0);
570 do {
571 err = mmc_send_op_cond_iter(mmc, &cmd, 1);
572 if (err)
573 return err;
574 if (get_timer(start) > timeout)
575 return UNUSABLE_ERR;
576 udelay(100);
577 } while (!(mmc->op_cond_response & OCR_BUSY));
Andy Fleming272cc702008-10-30 16:41:01 -0500578
Thomas Choud52ebf12010-12-24 13:12:21 +0000579 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
580 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
581 cmd.resp_type = MMC_RSP_R3;
582 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000583
584 err = mmc_send_cmd(mmc, &cmd, NULL);
585
586 if (err)
587 return err;
588 }
589
Andy Fleming272cc702008-10-30 16:41:01 -0500590 mmc->version = MMC_VERSION_UNKNOWN;
Rabin Vincent998be3d2009-04-05 13:30:56 +0530591 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500592
593 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
594 mmc->rca = 0;
595
596 return 0;
597}
598
599
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000600static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
Andy Fleming272cc702008-10-30 16:41:01 -0500601{
602 struct mmc_cmd cmd;
603 struct mmc_data data;
604 int err;
605
606 /* Get the Card Status Register */
607 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
608 cmd.resp_type = MMC_RSP_R1;
609 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500610
Yoshihiro Shimodacdfd1ac2012-06-07 19:09:11 +0000611 data.dest = (char *)ext_csd;
Andy Fleming272cc702008-10-30 16:41:01 -0500612 data.blocks = 1;
Simon Glass8bfa1952013-04-03 08:54:30 +0000613 data.blocksize = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -0500614 data.flags = MMC_DATA_READ;
615
616 err = mmc_send_cmd(mmc, &cmd, &data);
617
618 return err;
619}
620
621
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000622static int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
Andy Fleming272cc702008-10-30 16:41:01 -0500623{
624 struct mmc_cmd cmd;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000625 int timeout = 1000;
626 int ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500627
628 cmd.cmdidx = MMC_CMD_SWITCH;
629 cmd.resp_type = MMC_RSP_R1b;
630 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000631 (index << 16) |
632 (value << 8);
Andy Fleming272cc702008-10-30 16:41:01 -0500633
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000634 ret = mmc_send_cmd(mmc, &cmd, NULL);
635
636 /* Waiting for the ready status */
Jan Kloetzke93ad0d12012-02-05 22:29:11 +0000637 if (!ret)
638 ret = mmc_send_status(mmc, timeout);
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000639
640 return ret;
641
Andy Fleming272cc702008-10-30 16:41:01 -0500642}
643
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000644static int mmc_change_freq(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500645{
Simon Glass8bfa1952013-04-03 08:54:30 +0000646 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
Andy Fleming272cc702008-10-30 16:41:01 -0500647 char cardtype;
648 int err;
649
650 mmc->card_caps = 0;
651
Thomas Choud52ebf12010-12-24 13:12:21 +0000652 if (mmc_host_is_spi(mmc))
653 return 0;
654
Andy Fleming272cc702008-10-30 16:41:01 -0500655 /* Only version 4 supports high-speed */
656 if (mmc->version < MMC_VERSION_4)
657 return 0;
658
Andy Fleming272cc702008-10-30 16:41:01 -0500659 err = mmc_send_ext_csd(mmc, ext_csd);
660
661 if (err)
662 return err;
663
Lei Wen0560db12011-10-03 20:35:10 +0000664 cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -0500665
666 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
667
668 if (err)
669 return err;
670
671 /* Now check to see that it worked */
672 err = mmc_send_ext_csd(mmc, ext_csd);
673
674 if (err)
675 return err;
676
677 /* No high-speed support */
Lei Wen0560db12011-10-03 20:35:10 +0000678 if (!ext_csd[EXT_CSD_HS_TIMING])
Andy Fleming272cc702008-10-30 16:41:01 -0500679 return 0;
680
681 /* High Speed is set, there are two types: 52MHz and 26MHz */
682 if (cardtype & MMC_HS_52MHZ)
683 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
684 else
685 mmc->card_caps |= MMC_MODE_HS;
686
687 return 0;
688}
689
Stephen Warrenf866a462013-06-11 15:14:01 -0600690static int mmc_set_capacity(struct mmc *mmc, int part_num)
691{
692 switch (part_num) {
693 case 0:
694 mmc->capacity = mmc->capacity_user;
695 break;
696 case 1:
697 case 2:
698 mmc->capacity = mmc->capacity_boot;
699 break;
700 case 3:
701 mmc->capacity = mmc->capacity_rpmb;
702 break;
703 case 4:
704 case 5:
705 case 6:
706 case 7:
707 mmc->capacity = mmc->capacity_gp[part_num - 4];
708 break;
709 default:
710 return -1;
711 }
712
713 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
714
715 return 0;
716}
717
Lei Wenbc897b12011-05-02 16:26:26 +0000718int mmc_switch_part(int dev_num, unsigned int part_num)
719{
720 struct mmc *mmc = find_mmc_device(dev_num);
Stephen Warrenf866a462013-06-11 15:14:01 -0600721 int ret;
Lei Wenbc897b12011-05-02 16:26:26 +0000722
723 if (!mmc)
724 return -1;
725
Stephen Warrenf866a462013-06-11 15:14:01 -0600726 ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
727 (mmc->part_config & ~PART_ACCESS_MASK)
728 | (part_num & PART_ACCESS_MASK));
729 if (ret)
730 return ret;
731
732 return mmc_set_capacity(mmc, part_num);
Lei Wenbc897b12011-05-02 16:26:26 +0000733}
734
Thierry Reding48972d92012-01-02 01:15:37 +0000735int mmc_getcd(struct mmc *mmc)
736{
737 int cd;
738
739 cd = board_mmc_getcd(mmc);
740
Peter Korsgaardd4e1da42013-03-21 04:00:03 +0000741 if (cd < 0) {
742 if (mmc->getcd)
743 cd = mmc->getcd(mmc);
744 else
745 cd = 1;
746 }
Thierry Reding48972d92012-01-02 01:15:37 +0000747
748 return cd;
749}
750
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000751static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
Andy Fleming272cc702008-10-30 16:41:01 -0500752{
753 struct mmc_cmd cmd;
754 struct mmc_data data;
755
756 /* Switch the frequency */
757 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
758 cmd.resp_type = MMC_RSP_R1;
759 cmd.cmdarg = (mode << 31) | 0xffffff;
760 cmd.cmdarg &= ~(0xf << (group * 4));
761 cmd.cmdarg |= value << (group * 4);
Andy Fleming272cc702008-10-30 16:41:01 -0500762
763 data.dest = (char *)resp;
764 data.blocksize = 64;
765 data.blocks = 1;
766 data.flags = MMC_DATA_READ;
767
768 return mmc_send_cmd(mmc, &cmd, &data);
769}
770
771
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000772static int sd_change_freq(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500773{
774 int err;
775 struct mmc_cmd cmd;
Anton staaff781dd32011-10-03 13:54:59 +0000776 ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
777 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
Andy Fleming272cc702008-10-30 16:41:01 -0500778 struct mmc_data data;
779 int timeout;
780
781 mmc->card_caps = 0;
782
Thomas Choud52ebf12010-12-24 13:12:21 +0000783 if (mmc_host_is_spi(mmc))
784 return 0;
785
Andy Fleming272cc702008-10-30 16:41:01 -0500786 /* Read the SCR to find out if this card supports higher speeds */
787 cmd.cmdidx = MMC_CMD_APP_CMD;
788 cmd.resp_type = MMC_RSP_R1;
789 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -0500790
791 err = mmc_send_cmd(mmc, &cmd, NULL);
792
793 if (err)
794 return err;
795
796 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
797 cmd.resp_type = MMC_RSP_R1;
798 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500799
800 timeout = 3;
801
802retry_scr:
Anton staaff781dd32011-10-03 13:54:59 +0000803 data.dest = (char *)scr;
Andy Fleming272cc702008-10-30 16:41:01 -0500804 data.blocksize = 8;
805 data.blocks = 1;
806 data.flags = MMC_DATA_READ;
807
808 err = mmc_send_cmd(mmc, &cmd, &data);
809
810 if (err) {
811 if (timeout--)
812 goto retry_scr;
813
814 return err;
815 }
816
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300817 mmc->scr[0] = __be32_to_cpu(scr[0]);
818 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Fleming272cc702008-10-30 16:41:01 -0500819
820 switch ((mmc->scr[0] >> 24) & 0xf) {
821 case 0:
822 mmc->version = SD_VERSION_1_0;
823 break;
824 case 1:
825 mmc->version = SD_VERSION_1_10;
826 break;
827 case 2:
828 mmc->version = SD_VERSION_2;
Jaehoon Chung1741c642013-01-29 22:58:16 +0000829 if ((mmc->scr[0] >> 15) & 0x1)
830 mmc->version = SD_VERSION_3;
Andy Fleming272cc702008-10-30 16:41:01 -0500831 break;
832 default:
833 mmc->version = SD_VERSION_1_0;
834 break;
835 }
836
Alagu Sankarb44c7082010-05-12 15:08:24 +0530837 if (mmc->scr[0] & SD_DATA_4BIT)
838 mmc->card_caps |= MMC_MODE_4BIT;
839
Andy Fleming272cc702008-10-30 16:41:01 -0500840 /* Version 1.0 doesn't support switching */
841 if (mmc->version == SD_VERSION_1_0)
842 return 0;
843
844 timeout = 4;
845 while (timeout--) {
846 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
Anton staaff781dd32011-10-03 13:54:59 +0000847 (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -0500848
849 if (err)
850 return err;
851
852 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300853 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Fleming272cc702008-10-30 16:41:01 -0500854 break;
855 }
856
Andy Fleming272cc702008-10-30 16:41:01 -0500857 /* If high-speed isn't supported, we return */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300858 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
Andy Fleming272cc702008-10-30 16:41:01 -0500859 return 0;
860
Macpaul Lin2c3fbf42011-11-28 16:31:09 +0000861 /*
862 * If the host doesn't support SD_HIGHSPEED, do not switch card to
863 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
864 * This can avoid furthur problem when the card runs in different
865 * mode between the host.
866 */
867 if (!((mmc->host_caps & MMC_MODE_HS_52MHz) &&
868 (mmc->host_caps & MMC_MODE_HS)))
869 return 0;
870
Anton staaff781dd32011-10-03 13:54:59 +0000871 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -0500872
873 if (err)
874 return err;
875
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300876 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
Andy Fleming272cc702008-10-30 16:41:01 -0500877 mmc->card_caps |= MMC_MODE_HS;
878
879 return 0;
880}
881
882/* frequency bases */
883/* divided by 10 to be nice to platforms without floating point */
Mike Frysinger5f837c22010-10-20 01:15:53 +0000884static const int fbase[] = {
Andy Fleming272cc702008-10-30 16:41:01 -0500885 10000,
886 100000,
887 1000000,
888 10000000,
889};
890
891/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
892 * to platforms without floating point.
893 */
Mike Frysinger5f837c22010-10-20 01:15:53 +0000894static const int multipliers[] = {
Andy Fleming272cc702008-10-30 16:41:01 -0500895 0, /* reserved */
896 10,
897 12,
898 13,
899 15,
900 20,
901 25,
902 30,
903 35,
904 40,
905 45,
906 50,
907 55,
908 60,
909 70,
910 80,
911};
912
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000913static void mmc_set_ios(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500914{
915 mmc->set_ios(mmc);
916}
917
918void mmc_set_clock(struct mmc *mmc, uint clock)
919{
920 if (clock > mmc->f_max)
921 clock = mmc->f_max;
922
923 if (clock < mmc->f_min)
924 clock = mmc->f_min;
925
926 mmc->clock = clock;
927
928 mmc_set_ios(mmc);
929}
930
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000931static void mmc_set_bus_width(struct mmc *mmc, uint width)
Andy Fleming272cc702008-10-30 16:41:01 -0500932{
933 mmc->bus_width = width;
934
935 mmc_set_ios(mmc);
936}
937
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000938static int mmc_startup(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500939{
Stephen Warrenf866a462013-06-11 15:14:01 -0600940 int err, i;
Andy Fleming272cc702008-10-30 16:41:01 -0500941 uint mult, freq;
Yoshihiro Shimoda639b7822011-07-04 22:13:26 +0000942 u64 cmult, csize, capacity;
Andy Fleming272cc702008-10-30 16:41:01 -0500943 struct mmc_cmd cmd;
Simon Glass8bfa1952013-04-03 08:54:30 +0000944 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
945 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000946 int timeout = 1000;
Andy Fleming272cc702008-10-30 16:41:01 -0500947
Thomas Choud52ebf12010-12-24 13:12:21 +0000948#ifdef CONFIG_MMC_SPI_CRC_ON
949 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
950 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
951 cmd.resp_type = MMC_RSP_R1;
952 cmd.cmdarg = 1;
Thomas Choud52ebf12010-12-24 13:12:21 +0000953 err = mmc_send_cmd(mmc, &cmd, NULL);
954
955 if (err)
956 return err;
957 }
958#endif
959
Andy Fleming272cc702008-10-30 16:41:01 -0500960 /* Put the Card in Identify Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +0000961 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
962 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
Andy Fleming272cc702008-10-30 16:41:01 -0500963 cmd.resp_type = MMC_RSP_R2;
964 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500965
966 err = mmc_send_cmd(mmc, &cmd, NULL);
967
968 if (err)
969 return err;
970
971 memcpy(mmc->cid, cmd.response, 16);
972
973 /*
974 * For MMC cards, set the Relative Address.
975 * For SD cards, get the Relatvie Address.
976 * This also puts the cards into Standby State
977 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000978 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
979 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
980 cmd.cmdarg = mmc->rca << 16;
981 cmd.resp_type = MMC_RSP_R6;
Andy Fleming272cc702008-10-30 16:41:01 -0500982
Thomas Choud52ebf12010-12-24 13:12:21 +0000983 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -0500984
Thomas Choud52ebf12010-12-24 13:12:21 +0000985 if (err)
986 return err;
Andy Fleming272cc702008-10-30 16:41:01 -0500987
Thomas Choud52ebf12010-12-24 13:12:21 +0000988 if (IS_SD(mmc))
989 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
990 }
Andy Fleming272cc702008-10-30 16:41:01 -0500991
992 /* Get the Card-Specific Data */
993 cmd.cmdidx = MMC_CMD_SEND_CSD;
994 cmd.resp_type = MMC_RSP_R2;
995 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -0500996
997 err = mmc_send_cmd(mmc, &cmd, NULL);
998
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000999 /* Waiting for the ready status */
1000 mmc_send_status(mmc, timeout);
1001
Andy Fleming272cc702008-10-30 16:41:01 -05001002 if (err)
1003 return err;
1004
Rabin Vincent998be3d2009-04-05 13:30:56 +05301005 mmc->csd[0] = cmd.response[0];
1006 mmc->csd[1] = cmd.response[1];
1007 mmc->csd[2] = cmd.response[2];
1008 mmc->csd[3] = cmd.response[3];
Andy Fleming272cc702008-10-30 16:41:01 -05001009
1010 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincent0b453ff2009-04-05 13:30:55 +05301011 int version = (cmd.response[0] >> 26) & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -05001012
1013 switch (version) {
1014 case 0:
1015 mmc->version = MMC_VERSION_1_2;
1016 break;
1017 case 1:
1018 mmc->version = MMC_VERSION_1_4;
1019 break;
1020 case 2:
1021 mmc->version = MMC_VERSION_2_2;
1022 break;
1023 case 3:
1024 mmc->version = MMC_VERSION_3;
1025 break;
1026 case 4:
1027 mmc->version = MMC_VERSION_4;
1028 break;
1029 default:
1030 mmc->version = MMC_VERSION_1_2;
1031 break;
1032 }
1033 }
1034
1035 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincent0b453ff2009-04-05 13:30:55 +05301036 freq = fbase[(cmd.response[0] & 0x7)];
1037 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Fleming272cc702008-10-30 16:41:01 -05001038
1039 mmc->tran_speed = freq * mult;
1040
Rabin Vincent998be3d2009-04-05 13:30:56 +05301041 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -05001042
1043 if (IS_SD(mmc))
1044 mmc->write_bl_len = mmc->read_bl_len;
1045 else
Rabin Vincent998be3d2009-04-05 13:30:56 +05301046 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -05001047
1048 if (mmc->high_capacity) {
1049 csize = (mmc->csd[1] & 0x3f) << 16
1050 | (mmc->csd[2] & 0xffff0000) >> 16;
1051 cmult = 8;
1052 } else {
1053 csize = (mmc->csd[1] & 0x3ff) << 2
1054 | (mmc->csd[2] & 0xc0000000) >> 30;
1055 cmult = (mmc->csd[2] & 0x00038000) >> 15;
1056 }
1057
Stephen Warrenf866a462013-06-11 15:14:01 -06001058 mmc->capacity_user = (csize + 1) << (cmult + 2);
1059 mmc->capacity_user *= mmc->read_bl_len;
1060 mmc->capacity_boot = 0;
1061 mmc->capacity_rpmb = 0;
1062 for (i = 0; i < 4; i++)
1063 mmc->capacity_gp[i] = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05001064
Simon Glass8bfa1952013-04-03 08:54:30 +00001065 if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
1066 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -05001067
Simon Glass8bfa1952013-04-03 08:54:30 +00001068 if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
1069 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -05001070
1071 /* Select the card, and put it into Transfer Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00001072 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1073 cmd.cmdidx = MMC_CMD_SELECT_CARD;
Ajay Bhargavfe8f7062011-10-05 03:13:23 +00001074 cmd.resp_type = MMC_RSP_R1;
Thomas Choud52ebf12010-12-24 13:12:21 +00001075 cmd.cmdarg = mmc->rca << 16;
Thomas Choud52ebf12010-12-24 13:12:21 +00001076 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05001077
Thomas Choud52ebf12010-12-24 13:12:21 +00001078 if (err)
1079 return err;
1080 }
Andy Fleming272cc702008-10-30 16:41:01 -05001081
Lei Wene6f99a52011-06-22 17:03:31 +00001082 /*
1083 * For SD, its erase group is always one sector
1084 */
1085 mmc->erase_grp_size = 1;
Lei Wenbc897b12011-05-02 16:26:26 +00001086 mmc->part_config = MMCPART_NOAVAILABLE;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05301087 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
1088 /* check ext_csd version and capacity */
1089 err = mmc_send_ext_csd(mmc, ext_csd);
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001090 if (!err && (ext_csd[EXT_CSD_REV] >= 2)) {
Yoshihiro Shimoda639b7822011-07-04 22:13:26 +00001091 /*
1092 * According to the JEDEC Standard, the value of
1093 * ext_csd's capacity is valid if the value is more
1094 * than 2GB
1095 */
Lei Wen0560db12011-10-03 20:35:10 +00001096 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
1097 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
1098 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
1099 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
Simon Glass8bfa1952013-04-03 08:54:30 +00001100 capacity *= MMC_MAX_BLOCK_LEN;
Łukasz Majewskib1f1e8212011-07-05 02:19:44 +00001101 if ((capacity >> 20) > 2 * 1024)
Stephen Warrenf866a462013-06-11 15:14:01 -06001102 mmc->capacity_user = capacity;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05301103 }
Lei Wenbc897b12011-05-02 16:26:26 +00001104
Jaehoon Chung64f4a612013-01-29 19:31:16 +00001105 switch (ext_csd[EXT_CSD_REV]) {
1106 case 1:
1107 mmc->version = MMC_VERSION_4_1;
1108 break;
1109 case 2:
1110 mmc->version = MMC_VERSION_4_2;
1111 break;
1112 case 3:
1113 mmc->version = MMC_VERSION_4_3;
1114 break;
1115 case 5:
1116 mmc->version = MMC_VERSION_4_41;
1117 break;
1118 case 6:
1119 mmc->version = MMC_VERSION_4_5;
1120 break;
1121 }
1122
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 */
Simon Glass8bfa1952013-04-03 08:54:30 +00001128 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF]) {
Lei Wen0560db12011-10-03 20:35:10 +00001129 mmc->erase_grp_size =
Simon Glass8bfa1952013-04-03 08:54:30 +00001130 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] *
1131 MMC_MAX_BLOCK_LEN * 1024;
1132 } else {
Lei Wene6f99a52011-06-22 17:03:31 +00001133 int erase_gsz, erase_gmul;
1134 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1135 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1136 mmc->erase_grp_size = (erase_gsz + 1)
1137 * (erase_gmul + 1);
1138 }
1139
Lei Wenbc897b12011-05-02 16:26:26 +00001140 /* store the partition info of emmc */
Stephen Warren8948ea82012-07-30 10:55:43 +00001141 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
1142 ext_csd[EXT_CSD_BOOT_MULT])
Lei Wen0560db12011-10-03 20:35:10 +00001143 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
Stephen Warrenf866a462013-06-11 15:14:01 -06001144
1145 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
1146
1147 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
1148
1149 for (i = 0; i < 4; i++) {
1150 int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
1151 mmc->capacity_gp[i] = (ext_csd[idx + 2] << 16) +
1152 (ext_csd[idx + 1] << 8) + ext_csd[idx];
1153 mmc->capacity_gp[i] *=
1154 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1155 mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1156 }
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05301157 }
1158
Stephen Warrenf866a462013-06-11 15:14:01 -06001159 err = mmc_set_capacity(mmc, mmc->part_num);
1160 if (err)
1161 return err;
1162
Andy Fleming272cc702008-10-30 16:41:01 -05001163 if (IS_SD(mmc))
1164 err = sd_change_freq(mmc);
1165 else
1166 err = mmc_change_freq(mmc);
1167
1168 if (err)
1169 return err;
1170
1171 /* Restrict card's capabilities by what the host can do */
1172 mmc->card_caps &= mmc->host_caps;
1173
1174 if (IS_SD(mmc)) {
1175 if (mmc->card_caps & MMC_MODE_4BIT) {
1176 cmd.cmdidx = MMC_CMD_APP_CMD;
1177 cmd.resp_type = MMC_RSP_R1;
1178 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05001179
1180 err = mmc_send_cmd(mmc, &cmd, NULL);
1181 if (err)
1182 return err;
1183
1184 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1185 cmd.resp_type = MMC_RSP_R1;
1186 cmd.cmdarg = 2;
Andy Fleming272cc702008-10-30 16:41:01 -05001187 err = mmc_send_cmd(mmc, &cmd, NULL);
1188 if (err)
1189 return err;
1190
1191 mmc_set_bus_width(mmc, 4);
1192 }
1193
1194 if (mmc->card_caps & MMC_MODE_HS)
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001195 mmc->tran_speed = 50000000;
Andy Fleming272cc702008-10-30 16:41:01 -05001196 else
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001197 mmc->tran_speed = 25000000;
Andy Fleming272cc702008-10-30 16:41:01 -05001198 } else {
Andy Fleming7798f6d2012-10-31 19:02:38 +00001199 int idx;
1200
1201 /* An array of possible bus widths in order of preference */
1202 static unsigned ext_csd_bits[] = {
1203 EXT_CSD_BUS_WIDTH_8,
1204 EXT_CSD_BUS_WIDTH_4,
1205 EXT_CSD_BUS_WIDTH_1,
1206 };
1207
1208 /* An array to map CSD bus widths to host cap bits */
1209 static unsigned ext_to_hostcaps[] = {
1210 [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
1211 [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
1212 };
1213
1214 /* An array to map chosen bus width to an integer */
1215 static unsigned widths[] = {
1216 8, 4, 1,
1217 };
1218
1219 for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
1220 unsigned int extw = ext_csd_bits[idx];
1221
1222 /*
1223 * Check to make sure the controller supports
1224 * this bus width, if it's more than 1
1225 */
1226 if (extw != EXT_CSD_BUS_WIDTH_1 &&
1227 !(mmc->host_caps & ext_to_hostcaps[extw]))
1228 continue;
1229
Andy Fleming272cc702008-10-30 16:41:01 -05001230 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
Andy Fleming7798f6d2012-10-31 19:02:38 +00001231 EXT_CSD_BUS_WIDTH, extw);
Andy Fleming272cc702008-10-30 16:41:01 -05001232
1233 if (err)
Lei Wen41378942011-10-03 20:35:11 +00001234 continue;
Andy Fleming272cc702008-10-30 16:41:01 -05001235
Andy Fleming7798f6d2012-10-31 19:02:38 +00001236 mmc_set_bus_width(mmc, widths[idx]);
Andy Fleming272cc702008-10-30 16:41:01 -05001237
Lei Wen41378942011-10-03 20:35:11 +00001238 err = mmc_send_ext_csd(mmc, test_csd);
1239 if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \
1240 == test_csd[EXT_CSD_PARTITIONING_SUPPORT]
1241 && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \
1242 == test_csd[EXT_CSD_ERASE_GROUP_DEF] \
1243 && ext_csd[EXT_CSD_REV] \
1244 == test_csd[EXT_CSD_REV]
1245 && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \
1246 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1247 && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \
1248 &test_csd[EXT_CSD_SEC_CNT], 4) == 0) {
Andy Fleming272cc702008-10-30 16:41:01 -05001249
Andy Fleming7798f6d2012-10-31 19:02:38 +00001250 mmc->card_caps |= ext_to_hostcaps[extw];
Lei Wen41378942011-10-03 20:35:11 +00001251 break;
1252 }
Andy Fleming272cc702008-10-30 16:41:01 -05001253 }
1254
1255 if (mmc->card_caps & MMC_MODE_HS) {
1256 if (mmc->card_caps & MMC_MODE_HS_52MHz)
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001257 mmc->tran_speed = 52000000;
Andy Fleming272cc702008-10-30 16:41:01 -05001258 else
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001259 mmc->tran_speed = 26000000;
1260 }
Andy Fleming272cc702008-10-30 16:41:01 -05001261 }
1262
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001263 mmc_set_clock(mmc, mmc->tran_speed);
1264
Andy Fleming272cc702008-10-30 16:41:01 -05001265 /* fill in device description */
1266 mmc->block_dev.lun = 0;
1267 mmc->block_dev.type = 0;
1268 mmc->block_dev.blksz = mmc->read_bl_len;
Egbert Eich0472fbf2013-04-09 21:11:56 +00001269 mmc->block_dev.log2blksz = LOG2(mmc->block_dev.blksz);
Rabin Vincent9b1f9422009-04-05 13:30:54 +05301270 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
Taylor Huttbabce5f2012-10-20 17:15:59 +00001271 sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x",
1272 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1273 (mmc->cid[3] >> 16) & 0xffff);
1274 sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
1275 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1276 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1277 (mmc->cid[2] >> 24) & 0xff);
1278 sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
1279 (mmc->cid[2] >> 16) & 0xf);
Mikhail Kshevetskiy122efd42012-07-09 08:53:38 +00001280#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
Andy Fleming272cc702008-10-30 16:41:01 -05001281 init_part(&mmc->block_dev);
Mikhail Kshevetskiy122efd42012-07-09 08:53:38 +00001282#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001283
1284 return 0;
1285}
1286
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001287static int mmc_send_if_cond(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001288{
1289 struct mmc_cmd cmd;
1290 int err;
1291
1292 cmd.cmdidx = SD_CMD_SEND_IF_COND;
1293 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1294 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
1295 cmd.resp_type = MMC_RSP_R7;
Andy Fleming272cc702008-10-30 16:41:01 -05001296
1297 err = mmc_send_cmd(mmc, &cmd, NULL);
1298
1299 if (err)
1300 return err;
1301
Rabin Vincent998be3d2009-04-05 13:30:56 +05301302 if ((cmd.response[0] & 0xff) != 0xaa)
Andy Fleming272cc702008-10-30 16:41:01 -05001303 return UNUSABLE_ERR;
1304 else
1305 mmc->version = SD_VERSION_2;
1306
1307 return 0;
1308}
1309
1310int mmc_register(struct mmc *mmc)
1311{
1312 /* Setup the universal parts of the block interface just once */
1313 mmc->block_dev.if_type = IF_TYPE_MMC;
1314 mmc->block_dev.dev = cur_dev_num++;
1315 mmc->block_dev.removable = 1;
1316 mmc->block_dev.block_read = mmc_bread;
1317 mmc->block_dev.block_write = mmc_bwrite;
Lei Wene6f99a52011-06-22 17:03:31 +00001318 mmc->block_dev.block_erase = mmc_berase;
John Rigby8feafcc2011-04-18 05:50:08 +00001319 if (!mmc->b_max)
1320 mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Andy Fleming272cc702008-10-30 16:41:01 -05001321
1322 INIT_LIST_HEAD (&mmc->link);
1323
1324 list_add_tail (&mmc->link, &mmc_devices);
1325
1326 return 0;
1327}
1328
Matthew McClintockdf3fc522011-05-24 05:31:19 +00001329#ifdef CONFIG_PARTITIONS
Andy Fleming272cc702008-10-30 16:41:01 -05001330block_dev_desc_t *mmc_get_dev(int dev)
1331{
1332 struct mmc *mmc = find_mmc_device(dev);
Benoît Thébaudeau6bb4b4b2012-08-10 08:59:12 +00001333 if (!mmc || mmc_init(mmc))
Łukasz Majewski40242bc2012-04-19 02:39:18 +00001334 return NULL;
Andy Fleming272cc702008-10-30 16:41:01 -05001335
Łukasz Majewski40242bc2012-04-19 02:39:18 +00001336 return &mmc->block_dev;
Andy Fleming272cc702008-10-30 16:41:01 -05001337}
Matthew McClintockdf3fc522011-05-24 05:31:19 +00001338#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001339
Che-Liang Chioue9550442012-11-28 15:21:13 +00001340int mmc_start_init(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001341{
Macpaul Linafd59322011-11-14 23:35:39 +00001342 int err;
Andy Fleming272cc702008-10-30 16:41:01 -05001343
Thierry Reding48972d92012-01-02 01:15:37 +00001344 if (mmc_getcd(mmc) == 0) {
1345 mmc->has_init = 0;
1346 printf("MMC: no card present\n");
1347 return NO_CARD_ERR;
1348 }
1349
Lei Wenbc897b12011-05-02 16:26:26 +00001350 if (mmc->has_init)
1351 return 0;
1352
Andy Fleming272cc702008-10-30 16:41:01 -05001353 err = mmc->init(mmc);
1354
1355 if (err)
1356 return err;
1357
Ilya Yanokb86b85e2009-06-29 17:53:16 +04001358 mmc_set_bus_width(mmc, 1);
1359 mmc_set_clock(mmc, 1);
1360
Andy Fleming272cc702008-10-30 16:41:01 -05001361 /* Reset the Card */
1362 err = mmc_go_idle(mmc);
1363
1364 if (err)
1365 return err;
1366
Lei Wenbc897b12011-05-02 16:26:26 +00001367 /* The internal partition reset to user partition(0) at every CMD0*/
1368 mmc->part_num = 0;
1369
Andy Fleming272cc702008-10-30 16:41:01 -05001370 /* Test for SD version 2 */
Macpaul Linafd59322011-11-14 23:35:39 +00001371 err = mmc_send_if_cond(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001372
Andy Fleming272cc702008-10-30 16:41:01 -05001373 /* Now try to get the SD card's operating condition */
1374 err = sd_send_op_cond(mmc);
1375
1376 /* If the command timed out, we check for an MMC card */
1377 if (err == TIMEOUT) {
1378 err = mmc_send_op_cond(mmc);
1379
Che-Liang Chioue9550442012-11-28 15:21:13 +00001380 if (err && err != IN_PROGRESS) {
Andy Fleming272cc702008-10-30 16:41:01 -05001381 printf("Card did not respond to voltage select!\n");
1382 return UNUSABLE_ERR;
1383 }
1384 }
1385
Che-Liang Chioue9550442012-11-28 15:21:13 +00001386 if (err == IN_PROGRESS)
1387 mmc->init_in_progress = 1;
1388
1389 return err;
1390}
1391
1392static int mmc_complete_init(struct mmc *mmc)
1393{
1394 int err = 0;
1395
1396 if (mmc->op_cond_pending)
1397 err = mmc_complete_op_cond(mmc);
1398
1399 if (!err)
1400 err = mmc_startup(mmc);
Lei Wenbc897b12011-05-02 16:26:26 +00001401 if (err)
1402 mmc->has_init = 0;
1403 else
1404 mmc->has_init = 1;
Che-Liang Chioue9550442012-11-28 15:21:13 +00001405 mmc->init_in_progress = 0;
1406 return err;
1407}
1408
1409int mmc_init(struct mmc *mmc)
1410{
1411 int err = IN_PROGRESS;
1412 unsigned start = get_timer(0);
1413
1414 if (mmc->has_init)
1415 return 0;
1416 if (!mmc->init_in_progress)
1417 err = mmc_start_init(mmc);
1418
1419 if (!err || err == IN_PROGRESS)
1420 err = mmc_complete_init(mmc);
1421 debug("%s: %d, time %lu\n", __func__, err, get_timer(start));
Lei Wenbc897b12011-05-02 16:26:26 +00001422 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001423}
1424
1425/*
1426 * CPU and board-specific MMC initializations. Aliased function
1427 * signals caller to move on
1428 */
1429static int __def_mmc_init(bd_t *bis)
1430{
1431 return -1;
1432}
1433
Peter Tyserf9a109b2009-04-20 11:08:46 -05001434int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1435int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
Andy Fleming272cc702008-10-30 16:41:01 -05001436
1437void print_mmc_devices(char separator)
1438{
1439 struct mmc *m;
1440 struct list_head *entry;
1441
1442 list_for_each(entry, &mmc_devices) {
1443 m = list_entry(entry, struct mmc, link);
1444
1445 printf("%s: %d", m->name, m->block_dev.dev);
1446
1447 if (entry->next != &mmc_devices)
1448 printf("%c ", separator);
1449 }
1450
1451 printf("\n");
1452}
1453
Lei Wenea6ebe22011-05-02 16:26:25 +00001454int get_mmc_num(void)
1455{
1456 return cur_dev_num;
1457}
1458
Che-Liang Chioue9550442012-11-28 15:21:13 +00001459void mmc_set_preinit(struct mmc *mmc, int preinit)
1460{
1461 mmc->preinit = preinit;
1462}
1463
1464static void do_preinit(void)
1465{
1466 struct mmc *m;
1467 struct list_head *entry;
1468
1469 list_for_each(entry, &mmc_devices) {
1470 m = list_entry(entry, struct mmc, link);
1471
1472 if (m->preinit)
1473 mmc_start_init(m);
1474 }
1475}
1476
1477
Andy Fleming272cc702008-10-30 16:41:01 -05001478int mmc_initialize(bd_t *bis)
1479{
1480 INIT_LIST_HEAD (&mmc_devices);
1481 cur_dev_num = 0;
1482
1483 if (board_mmc_init(bis) < 0)
1484 cpu_mmc_init(bis);
1485
1486 print_mmc_devices(',');
1487
Che-Liang Chioue9550442012-11-28 15:21:13 +00001488 do_preinit();
Andy Fleming272cc702008-10-30 16:41:01 -05001489 return 0;
1490}
Amar3690d6d2013-04-27 11:42:58 +05301491
1492#ifdef CONFIG_SUPPORT_EMMC_BOOT
1493/*
1494 * This function changes the size of boot partition and the size of rpmb
1495 * partition present on EMMC devices.
1496 *
1497 * Input Parameters:
1498 * struct *mmc: pointer for the mmc device strcuture
1499 * bootsize: size of boot partition
1500 * rpmbsize: size of rpmb partition
1501 *
1502 * Returns 0 on success.
1503 */
1504
1505int mmc_boot_partition_size_change(struct mmc *mmc, unsigned long bootsize,
1506 unsigned long rpmbsize)
1507{
1508 int err;
1509 struct mmc_cmd cmd;
1510
1511 /* Only use this command for raw EMMC moviNAND. Enter backdoor mode */
1512 cmd.cmdidx = MMC_CMD_RES_MAN;
1513 cmd.resp_type = MMC_RSP_R1b;
1514 cmd.cmdarg = MMC_CMD62_ARG1;
1515
1516 err = mmc_send_cmd(mmc, &cmd, NULL);
1517 if (err) {
1518 debug("mmc_boot_partition_size_change: Error1 = %d\n", err);
1519 return err;
1520 }
1521
1522 /* Boot partition changing mode */
1523 cmd.cmdidx = MMC_CMD_RES_MAN;
1524 cmd.resp_type = MMC_RSP_R1b;
1525 cmd.cmdarg = MMC_CMD62_ARG2;
1526
1527 err = mmc_send_cmd(mmc, &cmd, NULL);
1528 if (err) {
1529 debug("mmc_boot_partition_size_change: Error2 = %d\n", err);
1530 return err;
1531 }
1532 /* boot partition size is multiple of 128KB */
1533 bootsize = (bootsize * 1024) / 128;
1534
1535 /* Arg: boot partition size */
1536 cmd.cmdidx = MMC_CMD_RES_MAN;
1537 cmd.resp_type = MMC_RSP_R1b;
1538 cmd.cmdarg = bootsize;
1539
1540 err = mmc_send_cmd(mmc, &cmd, NULL);
1541 if (err) {
1542 debug("mmc_boot_partition_size_change: Error3 = %d\n", err);
1543 return err;
1544 }
1545 /* RPMB partition size is multiple of 128KB */
1546 rpmbsize = (rpmbsize * 1024) / 128;
1547 /* Arg: RPMB partition size */
1548 cmd.cmdidx = MMC_CMD_RES_MAN;
1549 cmd.resp_type = MMC_RSP_R1b;
1550 cmd.cmdarg = rpmbsize;
1551
1552 err = mmc_send_cmd(mmc, &cmd, NULL);
1553 if (err) {
1554 debug("mmc_boot_partition_size_change: Error4 = %d\n", err);
1555 return err;
1556 }
1557 return 0;
1558}
1559
1560/*
1561 * This function shall form and send the commands to open / close the
1562 * boot partition specified by user.
1563 *
1564 * Input Parameters:
1565 * ack: 0x0 - No boot acknowledge sent (default)
1566 * 0x1 - Boot acknowledge sent during boot operation
1567 * part_num: User selects boot data that will be sent to master
1568 * 0x0 - Device not boot enabled (default)
1569 * 0x1 - Boot partition 1 enabled for boot
1570 * 0x2 - Boot partition 2 enabled for boot
1571 * access: User selects partitions to access
1572 * 0x0 : No access to boot partition (default)
1573 * 0x1 : R/W boot partition 1
1574 * 0x2 : R/W boot partition 2
1575 * 0x3 : R/W Replay Protected Memory Block (RPMB)
1576 *
1577 * Returns 0 on success.
1578 */
1579int mmc_boot_part_access(struct mmc *mmc, u8 ack, u8 part_num, u8 access)
1580{
1581 int err;
1582 struct mmc_cmd cmd;
1583
1584 /* Boot ack enable, boot partition enable , boot partition access */
1585 cmd.cmdidx = MMC_CMD_SWITCH;
1586 cmd.resp_type = MMC_RSP_R1b;
1587
1588 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1589 (EXT_CSD_PART_CONF << 16) |
1590 ((EXT_CSD_BOOT_ACK(ack) |
1591 EXT_CSD_BOOT_PART_NUM(part_num) |
1592 EXT_CSD_PARTITION_ACCESS(access)) << 8);
1593
1594 err = mmc_send_cmd(mmc, &cmd, NULL);
1595 if (err) {
1596 if (access) {
1597 debug("mmc boot partition#%d open fail:Error1 = %d\n",
1598 part_num, err);
1599 } else {
1600 debug("mmc boot partition#%d close fail:Error = %d\n",
1601 part_num, err);
1602 }
1603 return err;
1604 }
1605
1606 if (access) {
1607 /* 4bit transfer mode at booting time. */
1608 cmd.cmdidx = MMC_CMD_SWITCH;
1609 cmd.resp_type = MMC_RSP_R1b;
1610
1611 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1612 (EXT_CSD_BOOT_BUS_WIDTH << 16) |
1613 ((1 << 0) << 8);
1614
1615 err = mmc_send_cmd(mmc, &cmd, NULL);
1616 if (err) {
1617 debug("mmc boot partition#%d open fail:Error2 = %d\n",
1618 part_num, err);
1619 return err;
1620 }
1621 }
1622 return 0;
1623}
1624#endif