blob: deb4786585ac1ac80adf759b2942ba8760b679de [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>
Sjoerd Simons8e3332e2015-08-30 16:55:45 -060013#include <dm.h>
14#include <dm/device-internal.h>
Stephen Warrend4622df2014-05-23 12:47:06 -060015#include <errno.h>
Andy Fleming272cc702008-10-30 16:41:01 -050016#include <mmc.h>
17#include <part.h>
Peng Fan2051aef2016-10-11 15:08:43 +080018#include <power/regulator.h>
Andy Fleming272cc702008-10-30 16:41:01 -050019#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060020#include <memalign.h>
Andy Fleming272cc702008-10-30 16:41:01 -050021#include <linux/list.h>
Rabin Vincent9b1f9422009-04-05 13:30:54 +053022#include <div64.h>
Paul Burtonda61fa52013-09-09 15:30:26 +010023#include "mmc_private.h"
Andy Fleming272cc702008-10-30 16:41:01 -050024
Peng Fan3697e592016-09-01 11:13:38 +080025static const unsigned int sd_au_size[] = {
26 0, SZ_16K / 512, SZ_32K / 512,
27 SZ_64K / 512, SZ_128K / 512, SZ_256K / 512,
28 SZ_512K / 512, SZ_1M / 512, SZ_2M / 512,
29 SZ_4M / 512, SZ_8M / 512, (SZ_8M + SZ_4M) / 512,
30 SZ_16M / 512, (SZ_16M + SZ_8M) / 512, SZ_32M / 512, SZ_64M / 512,
31};
32
Marek Vasutb5b838f2016-12-01 02:06:33 +010033#if CONFIG_IS_ENABLED(MMC_TINY)
34static struct mmc mmc_static;
35struct mmc *find_mmc_device(int dev_num)
36{
37 return &mmc_static;
38}
39
40void mmc_do_preinit(void)
41{
42 struct mmc *m = &mmc_static;
43#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
44 mmc_set_preinit(m, 1);
45#endif
46 if (m->preinit)
47 mmc_start_init(m);
48}
49
50struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
51{
52 return &mmc->block_dev;
53}
54#endif
55
Simon Glasse7881d82017-07-29 11:35:31 -060056#if !CONFIG_IS_ENABLED(DM_MMC)
Jeroen Hofstee750121c2014-07-12 21:24:08 +020057__weak int board_mmc_getwp(struct mmc *mmc)
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +000058{
59 return -1;
60}
61
62int mmc_getwp(struct mmc *mmc)
63{
64 int wp;
65
66 wp = board_mmc_getwp(mmc);
67
Peter Korsgaardd4e1da42013-03-21 04:00:03 +000068 if (wp < 0) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +020069 if (mmc->cfg->ops->getwp)
70 wp = mmc->cfg->ops->getwp(mmc);
Peter Korsgaardd4e1da42013-03-21 04:00:03 +000071 else
72 wp = 0;
73 }
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +000074
75 return wp;
76}
77
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +020078__weak int board_mmc_getcd(struct mmc *mmc)
79{
Stefano Babic11fdade2010-02-05 15:04:43 +010080 return -1;
81}
Simon Glass8ca51e52016-06-12 23:30:22 -060082#endif
Stefano Babic11fdade2010-02-05 15:04:43 +010083
Marek Vasut8635ff92012-03-15 18:41:35 +000084#ifdef CONFIG_MMC_TRACE
Simon Glassc0c76eb2016-06-12 23:30:20 -060085void mmmc_trace_before_send(struct mmc *mmc, struct mmc_cmd *cmd)
86{
87 printf("CMD_SEND:%d\n", cmd->cmdidx);
88 printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
89}
90
91void mmmc_trace_after_send(struct mmc *mmc, struct mmc_cmd *cmd, int ret)
92{
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +000093 int i;
94 u8 *ptr;
95
Bin Meng7863ce52016-03-17 21:53:14 -070096 if (ret) {
97 printf("\t\tRET\t\t\t %d\n", ret);
98 } else {
99 switch (cmd->resp_type) {
100 case MMC_RSP_NONE:
101 printf("\t\tMMC_RSP_NONE\n");
102 break;
103 case MMC_RSP_R1:
104 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
105 cmd->response[0]);
106 break;
107 case MMC_RSP_R1b:
108 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
109 cmd->response[0]);
110 break;
111 case MMC_RSP_R2:
112 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
113 cmd->response[0]);
114 printf("\t\t \t\t 0x%08X \n",
115 cmd->response[1]);
116 printf("\t\t \t\t 0x%08X \n",
117 cmd->response[2]);
118 printf("\t\t \t\t 0x%08X \n",
119 cmd->response[3]);
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000120 printf("\n");
Bin Meng7863ce52016-03-17 21:53:14 -0700121 printf("\t\t\t\t\tDUMPING DATA\n");
122 for (i = 0; i < 4; i++) {
123 int j;
124 printf("\t\t\t\t\t%03d - ", i*4);
125 ptr = (u8 *)&cmd->response[i];
126 ptr += 3;
127 for (j = 0; j < 4; j++)
128 printf("%02X ", *ptr--);
129 printf("\n");
130 }
131 break;
132 case MMC_RSP_R3:
133 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
134 cmd->response[0]);
135 break;
136 default:
137 printf("\t\tERROR MMC rsp not supported\n");
138 break;
Bin Meng53e8e402016-03-17 21:53:13 -0700139 }
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000140 }
Simon Glassc0c76eb2016-06-12 23:30:20 -0600141}
142
143void mmc_trace_state(struct mmc *mmc, struct mmc_cmd *cmd)
144{
145 int status;
146
147 status = (cmd->response[0] & MMC_STATUS_CURR_STATE) >> 9;
148 printf("CURR STATE:%d\n", status);
149}
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000150#endif
Simon Glassc0c76eb2016-06-12 23:30:20 -0600151
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200152#if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG)
153const char *mmc_mode_name(enum bus_mode mode)
154{
155 static const char *const names[] = {
156 [MMC_LEGACY] = "MMC legacy",
157 [SD_LEGACY] = "SD Legacy",
158 [MMC_HS] = "MMC High Speed (26MHz)",
159 [SD_HS] = "SD High Speed (50MHz)",
160 [UHS_SDR12] = "UHS SDR12 (25MHz)",
161 [UHS_SDR25] = "UHS SDR25 (50MHz)",
162 [UHS_SDR50] = "UHS SDR50 (100MHz)",
163 [UHS_SDR104] = "UHS SDR104 (208MHz)",
164 [UHS_DDR50] = "UHS DDR50 (50MHz)",
165 [MMC_HS_52] = "MMC High Speed (52MHz)",
166 [MMC_DDR_52] = "MMC DDR52 (52MHz)",
167 [MMC_HS_200] = "HS200 (200MHz)",
168 };
169
170 if (mode >= MMC_MODES_END)
171 return "Unknown mode";
172 else
173 return names[mode];
174}
175#endif
176
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200177static uint mmc_mode2freq(struct mmc *mmc, enum bus_mode mode)
178{
179 static const int freqs[] = {
180 [SD_LEGACY] = 25000000,
181 [MMC_HS] = 26000000,
182 [SD_HS] = 50000000,
183 [UHS_SDR12] = 25000000,
184 [UHS_SDR25] = 50000000,
185 [UHS_SDR50] = 100000000,
186 [UHS_SDR104] = 208000000,
187 [UHS_DDR50] = 50000000,
188 [MMC_HS_52] = 52000000,
189 [MMC_DDR_52] = 52000000,
190 [MMC_HS_200] = 200000000,
191 };
192
193 if (mode == MMC_LEGACY)
194 return mmc->legacy_speed;
195 else if (mode >= MMC_MODES_END)
196 return 0;
197 else
198 return freqs[mode];
199}
200
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200201static int mmc_select_mode(struct mmc *mmc, enum bus_mode mode)
202{
203 mmc->selected_mode = mode;
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200204 mmc->tran_speed = mmc_mode2freq(mmc, mode);
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200205 debug("selecting mode %s (freq : %d MHz)\n", mmc_mode_name(mode),
206 mmc->tran_speed / 1000000);
207 return 0;
208}
209
Simon Glasse7881d82017-07-29 11:35:31 -0600210#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glassc0c76eb2016-06-12 23:30:20 -0600211int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
212{
213 int ret;
214
215 mmmc_trace_before_send(mmc, cmd);
216 ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
217 mmmc_trace_after_send(mmc, cmd, ret);
218
Marek Vasut8635ff92012-03-15 18:41:35 +0000219 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500220}
Simon Glass8ca51e52016-06-12 23:30:22 -0600221#endif
Andy Fleming272cc702008-10-30 16:41:01 -0500222
Paul Burtonda61fa52013-09-09 15:30:26 +0100223int mmc_send_status(struct mmc *mmc, int timeout)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000224{
225 struct mmc_cmd cmd;
Jan Kloetzked617c422012-02-05 22:29:12 +0000226 int err, retries = 5;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000227
228 cmd.cmdidx = MMC_CMD_SEND_STATUS;
229 cmd.resp_type = MMC_RSP_R1;
Marek Vasutaaf3d412011-08-10 09:24:48 +0200230 if (!mmc_host_is_spi(mmc))
231 cmd.cmdarg = mmc->rca << 16;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000232
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500233 while (1) {
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000234 err = mmc_send_cmd(mmc, &cmd, NULL);
Jan Kloetzked617c422012-02-05 22:29:12 +0000235 if (!err) {
236 if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
237 (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
238 MMC_STATE_PRG)
239 break;
240 else if (cmd.response[0] & MMC_STATUS_MASK) {
Paul Burton56196822013-09-04 16:12:25 +0100241#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jan Kloetzked617c422012-02-05 22:29:12 +0000242 printf("Status Error: 0x%08X\n",
243 cmd.response[0]);
Paul Burton56196822013-09-04 16:12:25 +0100244#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900245 return -ECOMM;
Jan Kloetzked617c422012-02-05 22:29:12 +0000246 }
247 } else if (--retries < 0)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000248 return err;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000249
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500250 if (timeout-- <= 0)
251 break;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000252
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500253 udelay(1000);
254 }
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000255
Simon Glassc0c76eb2016-06-12 23:30:20 -0600256 mmc_trace_state(mmc, &cmd);
Jongman Heo5b0c9422012-06-03 21:32:13 +0000257 if (timeout <= 0) {
Paul Burton56196822013-09-04 16:12:25 +0100258#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000259 printf("Timeout waiting card ready\n");
Paul Burton56196822013-09-04 16:12:25 +0100260#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900261 return -ETIMEDOUT;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000262 }
263
264 return 0;
265}
266
Paul Burtonda61fa52013-09-09 15:30:26 +0100267int mmc_set_blocklen(struct mmc *mmc, int len)
Andy Fleming272cc702008-10-30 16:41:01 -0500268{
269 struct mmc_cmd cmd;
270
Andrew Gabbasov786e8f82014-12-01 06:59:09 -0600271 if (mmc->ddr_mode)
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900272 return 0;
273
Andy Fleming272cc702008-10-30 16:41:01 -0500274 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
275 cmd.resp_type = MMC_RSP_R1;
276 cmd.cmdarg = len;
Andy Fleming272cc702008-10-30 16:41:01 -0500277
278 return mmc_send_cmd(mmc, &cmd, NULL);
279}
280
Sascha Silbeff8fef52013-06-14 13:07:25 +0200281static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000282 lbaint_t blkcnt)
Andy Fleming272cc702008-10-30 16:41:01 -0500283{
284 struct mmc_cmd cmd;
285 struct mmc_data data;
286
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700287 if (blkcnt > 1)
288 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
289 else
290 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Fleming272cc702008-10-30 16:41:01 -0500291
292 if (mmc->high_capacity)
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700293 cmd.cmdarg = start;
Andy Fleming272cc702008-10-30 16:41:01 -0500294 else
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700295 cmd.cmdarg = start * mmc->read_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500296
297 cmd.resp_type = MMC_RSP_R1;
Andy Fleming272cc702008-10-30 16:41:01 -0500298
299 data.dest = dst;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700300 data.blocks = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500301 data.blocksize = mmc->read_bl_len;
302 data.flags = MMC_DATA_READ;
303
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700304 if (mmc_send_cmd(mmc, &cmd, &data))
305 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500306
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700307 if (blkcnt > 1) {
308 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
309 cmd.cmdarg = 0;
310 cmd.resp_type = MMC_RSP_R1b;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700311 if (mmc_send_cmd(mmc, &cmd, NULL)) {
Paul Burton56196822013-09-04 16:12:25 +0100312#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700313 printf("mmc fail to send stop cmd\n");
Paul Burton56196822013-09-04 16:12:25 +0100314#endif
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700315 return 0;
316 }
Andy Fleming272cc702008-10-30 16:41:01 -0500317 }
318
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700319 return blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500320}
321
Simon Glassc4d660d2017-07-04 13:31:19 -0600322#if CONFIG_IS_ENABLED(BLK)
Simon Glass7dba0b92016-06-12 23:30:15 -0600323ulong mmc_bread(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, void *dst)
Simon Glass33fb2112016-05-01 13:52:41 -0600324#else
Simon Glass7dba0b92016-06-12 23:30:15 -0600325ulong mmc_bread(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt,
326 void *dst)
Simon Glass33fb2112016-05-01 13:52:41 -0600327#endif
Andy Fleming272cc702008-10-30 16:41:01 -0500328{
Simon Glassc4d660d2017-07-04 13:31:19 -0600329#if CONFIG_IS_ENABLED(BLK)
Simon Glass33fb2112016-05-01 13:52:41 -0600330 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
331#endif
Simon Glassbcce53d2016-02-29 15:25:51 -0700332 int dev_num = block_dev->devnum;
Stephen Warren873cc1d2015-12-07 11:38:49 -0700333 int err;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700334 lbaint_t cur, blocks_todo = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500335
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700336 if (blkcnt == 0)
337 return 0;
338
339 struct mmc *mmc = find_mmc_device(dev_num);
Andy Fleming272cc702008-10-30 16:41:01 -0500340 if (!mmc)
341 return 0;
342
Marek Vasutb5b838f2016-12-01 02:06:33 +0100343 if (CONFIG_IS_ENABLED(MMC_TINY))
344 err = mmc_switch_part(mmc, block_dev->hwpart);
345 else
346 err = blk_dselect_hwpart(block_dev, block_dev->hwpart);
347
Stephen Warren873cc1d2015-12-07 11:38:49 -0700348 if (err < 0)
349 return 0;
350
Simon Glassc40fdca2016-05-01 13:52:35 -0600351 if ((start + blkcnt) > block_dev->lba) {
Paul Burton56196822013-09-04 16:12:25 +0100352#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Sascha Silbeff8fef52013-06-14 13:07:25 +0200353 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
Simon Glassc40fdca2016-05-01 13:52:35 -0600354 start + blkcnt, block_dev->lba);
Paul Burton56196822013-09-04 16:12:25 +0100355#endif
Lei Wend2bf29e2010-09-13 22:07:27 +0800356 return 0;
357 }
Andy Fleming272cc702008-10-30 16:41:01 -0500358
Simon Glass11692992015-06-23 15:38:50 -0600359 if (mmc_set_blocklen(mmc, mmc->read_bl_len)) {
360 debug("%s: Failed to set blocklen\n", __func__);
Andy Fleming272cc702008-10-30 16:41:01 -0500361 return 0;
Simon Glass11692992015-06-23 15:38:50 -0600362 }
Andy Fleming272cc702008-10-30 16:41:01 -0500363
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700364 do {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200365 cur = (blocks_todo > mmc->cfg->b_max) ?
366 mmc->cfg->b_max : blocks_todo;
Simon Glass11692992015-06-23 15:38:50 -0600367 if (mmc_read_blocks(mmc, dst, start, cur) != cur) {
368 debug("%s: Failed to read blocks\n", __func__);
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700369 return 0;
Simon Glass11692992015-06-23 15:38:50 -0600370 }
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700371 blocks_todo -= cur;
372 start += cur;
373 dst += cur * mmc->read_bl_len;
374 } while (blocks_todo > 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500375
376 return blkcnt;
377}
378
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000379static int mmc_go_idle(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500380{
381 struct mmc_cmd cmd;
382 int err;
383
384 udelay(1000);
385
386 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
387 cmd.cmdarg = 0;
388 cmd.resp_type = MMC_RSP_NONE;
Andy Fleming272cc702008-10-30 16:41:01 -0500389
390 err = mmc_send_cmd(mmc, &cmd, NULL);
391
392 if (err)
393 return err;
394
395 udelay(2000);
396
397 return 0;
398}
399
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000400static int sd_send_op_cond(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500401{
402 int timeout = 1000;
403 int err;
404 struct mmc_cmd cmd;
405
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500406 while (1) {
Andy Fleming272cc702008-10-30 16:41:01 -0500407 cmd.cmdidx = MMC_CMD_APP_CMD;
408 cmd.resp_type = MMC_RSP_R1;
409 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500410
411 err = mmc_send_cmd(mmc, &cmd, NULL);
412
413 if (err)
414 return err;
415
416 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
417 cmd.resp_type = MMC_RSP_R3;
Stefano Babic250de122010-01-20 18:20:39 +0100418
419 /*
420 * Most cards do not answer if some reserved bits
421 * in the ocr are set. However, Some controller
422 * can set bit 7 (reserved for low voltages), but
423 * how to manage low voltages SD card is not yet
424 * specified.
425 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000426 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200427 (mmc->cfg->voltages & 0xff8000);
Andy Fleming272cc702008-10-30 16:41:01 -0500428
429 if (mmc->version == SD_VERSION_2)
430 cmd.cmdarg |= OCR_HCS;
431
432 err = mmc_send_cmd(mmc, &cmd, NULL);
433
434 if (err)
435 return err;
436
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500437 if (cmd.response[0] & OCR_BUSY)
438 break;
Andy Fleming272cc702008-10-30 16:41:01 -0500439
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500440 if (timeout-- <= 0)
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900441 return -EOPNOTSUPP;
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500442
443 udelay(1000);
444 }
Andy Fleming272cc702008-10-30 16:41:01 -0500445
446 if (mmc->version != SD_VERSION_2)
447 mmc->version = SD_VERSION_1_0;
448
Thomas Choud52ebf12010-12-24 13:12:21 +0000449 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
450 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
451 cmd.resp_type = MMC_RSP_R3;
452 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000453
454 err = mmc_send_cmd(mmc, &cmd, NULL);
455
456 if (err)
457 return err;
458 }
459
Rabin Vincent998be3d2009-04-05 13:30:56 +0530460 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500461
462 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
463 mmc->rca = 0;
464
465 return 0;
466}
467
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500468static int mmc_send_op_cond_iter(struct mmc *mmc, int use_arg)
Andy Fleming272cc702008-10-30 16:41:01 -0500469{
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500470 struct mmc_cmd cmd;
Andy Fleming272cc702008-10-30 16:41:01 -0500471 int err;
472
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500473 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
474 cmd.resp_type = MMC_RSP_R3;
475 cmd.cmdarg = 0;
Rob Herring5a203972015-03-23 17:56:59 -0500476 if (use_arg && !mmc_host_is_spi(mmc))
477 cmd.cmdarg = OCR_HCS |
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200478 (mmc->cfg->voltages &
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500479 (mmc->ocr & OCR_VOLTAGE_MASK)) |
480 (mmc->ocr & OCR_ACCESS_MODE);
Che-Liang Chioue9550442012-11-28 15:21:13 +0000481
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500482 err = mmc_send_cmd(mmc, &cmd, NULL);
Che-Liang Chioue9550442012-11-28 15:21:13 +0000483 if (err)
484 return err;
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500485 mmc->ocr = cmd.response[0];
Che-Liang Chioue9550442012-11-28 15:21:13 +0000486 return 0;
487}
488
Jeroen Hofstee750121c2014-07-12 21:24:08 +0200489static int mmc_send_op_cond(struct mmc *mmc)
Che-Liang Chioue9550442012-11-28 15:21:13 +0000490{
Che-Liang Chioue9550442012-11-28 15:21:13 +0000491 int err, i;
492
Andy Fleming272cc702008-10-30 16:41:01 -0500493 /* Some cards seem to need this */
494 mmc_go_idle(mmc);
495
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000496 /* Asking to the card its capabilities */
Che-Liang Chioue9550442012-11-28 15:21:13 +0000497 for (i = 0; i < 2; i++) {
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500498 err = mmc_send_op_cond_iter(mmc, i != 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500499 if (err)
500 return err;
501
Che-Liang Chioue9550442012-11-28 15:21:13 +0000502 /* exit if not busy (flag seems to be inverted) */
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500503 if (mmc->ocr & OCR_BUSY)
Andrew Gabbasovbd47c132015-03-19 07:44:07 -0500504 break;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000505 }
Andrew Gabbasovbd47c132015-03-19 07:44:07 -0500506 mmc->op_cond_pending = 1;
507 return 0;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000508}
Andy Fleming272cc702008-10-30 16:41:01 -0500509
Jeroen Hofstee750121c2014-07-12 21:24:08 +0200510static int mmc_complete_op_cond(struct mmc *mmc)
Che-Liang Chioue9550442012-11-28 15:21:13 +0000511{
512 struct mmc_cmd cmd;
513 int timeout = 1000;
514 uint start;
515 int err;
516
517 mmc->op_cond_pending = 0;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500518 if (!(mmc->ocr & OCR_BUSY)) {
Yangbo Lud188b112016-08-02 15:33:18 +0800519 /* Some cards seem to need this */
520 mmc_go_idle(mmc);
521
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500522 start = get_timer(0);
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500523 while (1) {
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500524 err = mmc_send_op_cond_iter(mmc, 1);
525 if (err)
526 return err;
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500527 if (mmc->ocr & OCR_BUSY)
528 break;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500529 if (get_timer(start) > timeout)
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900530 return -EOPNOTSUPP;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500531 udelay(100);
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500532 }
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500533 }
Andy Fleming272cc702008-10-30 16:41:01 -0500534
Thomas Choud52ebf12010-12-24 13:12:21 +0000535 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
536 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
537 cmd.resp_type = MMC_RSP_R3;
538 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000539
540 err = mmc_send_cmd(mmc, &cmd, NULL);
541
542 if (err)
543 return err;
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500544
545 mmc->ocr = cmd.response[0];
Thomas Choud52ebf12010-12-24 13:12:21 +0000546 }
547
Andy Fleming272cc702008-10-30 16:41:01 -0500548 mmc->version = MMC_VERSION_UNKNOWN;
Andy Fleming272cc702008-10-30 16:41:01 -0500549
550 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
Stephen Warrendef816a2014-01-30 16:11:12 -0700551 mmc->rca = 1;
Andy Fleming272cc702008-10-30 16:41:01 -0500552
553 return 0;
554}
555
556
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000557static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
Andy Fleming272cc702008-10-30 16:41:01 -0500558{
559 struct mmc_cmd cmd;
560 struct mmc_data data;
561 int err;
562
563 /* Get the Card Status Register */
564 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
565 cmd.resp_type = MMC_RSP_R1;
566 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500567
Yoshihiro Shimodacdfd1ac2012-06-07 19:09:11 +0000568 data.dest = (char *)ext_csd;
Andy Fleming272cc702008-10-30 16:41:01 -0500569 data.blocks = 1;
Simon Glass8bfa1952013-04-03 08:54:30 +0000570 data.blocksize = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -0500571 data.flags = MMC_DATA_READ;
572
573 err = mmc_send_cmd(mmc, &cmd, &data);
574
575 return err;
576}
577
Simon Glassc40704f2016-06-12 23:30:18 -0600578int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
Andy Fleming272cc702008-10-30 16:41:01 -0500579{
580 struct mmc_cmd cmd;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000581 int timeout = 1000;
Maxime Riparda9003dc2016-11-04 16:18:08 +0100582 int retries = 3;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000583 int ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500584
585 cmd.cmdidx = MMC_CMD_SWITCH;
586 cmd.resp_type = MMC_RSP_R1b;
587 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000588 (index << 16) |
589 (value << 8);
Andy Fleming272cc702008-10-30 16:41:01 -0500590
Maxime Riparda9003dc2016-11-04 16:18:08 +0100591 while (retries > 0) {
592 ret = mmc_send_cmd(mmc, &cmd, NULL);
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000593
Maxime Riparda9003dc2016-11-04 16:18:08 +0100594 /* Waiting for the ready status */
595 if (!ret) {
596 ret = mmc_send_status(mmc, timeout);
597 return ret;
598 }
599
600 retries--;
601 }
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000602
603 return ret;
604
Andy Fleming272cc702008-10-30 16:41:01 -0500605}
606
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000607static int mmc_change_freq(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500608{
Simon Glass8bfa1952013-04-03 08:54:30 +0000609 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
Andy Fleming272cc702008-10-30 16:41:01 -0500610 char cardtype;
611 int err;
612
Andrew Gabbasovfc5b32f2014-12-25 10:22:25 -0600613 mmc->card_caps = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500614
Thomas Choud52ebf12010-12-24 13:12:21 +0000615 if (mmc_host_is_spi(mmc))
616 return 0;
617
Andy Fleming272cc702008-10-30 16:41:01 -0500618 /* Only version 4 supports high-speed */
619 if (mmc->version < MMC_VERSION_4)
620 return 0;
621
Andrew Gabbasovfc5b32f2014-12-25 10:22:25 -0600622 mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT;
623
Andy Fleming272cc702008-10-30 16:41:01 -0500624 err = mmc_send_ext_csd(mmc, ext_csd);
625
626 if (err)
627 return err;
628
Lei Wen0560db12011-10-03 20:35:10 +0000629 cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -0500630
631 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
632
633 if (err)
Heiko Schochera5e27b42016-06-07 08:31:21 +0200634 return err;
Andy Fleming272cc702008-10-30 16:41:01 -0500635
636 /* Now check to see that it worked */
637 err = mmc_send_ext_csd(mmc, ext_csd);
638
639 if (err)
640 return err;
641
642 /* No high-speed support */
Lei Wen0560db12011-10-03 20:35:10 +0000643 if (!ext_csd[EXT_CSD_HS_TIMING])
Andy Fleming272cc702008-10-30 16:41:01 -0500644 return 0;
645
646 /* High Speed is set, there are two types: 52MHz and 26MHz */
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900647 if (cardtype & EXT_CSD_CARD_TYPE_52) {
Andrew Gabbasov201d5ac2014-12-01 06:59:10 -0600648 if (cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V)
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900649 mmc->card_caps |= MMC_MODE_DDR_52MHz;
Andy Fleming272cc702008-10-30 16:41:01 -0500650 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900651 } else {
Andy Fleming272cc702008-10-30 16:41:01 -0500652 mmc->card_caps |= MMC_MODE_HS;
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900653 }
Andy Fleming272cc702008-10-30 16:41:01 -0500654
655 return 0;
656}
657
Stephen Warrenf866a462013-06-11 15:14:01 -0600658static int mmc_set_capacity(struct mmc *mmc, int part_num)
659{
660 switch (part_num) {
661 case 0:
662 mmc->capacity = mmc->capacity_user;
663 break;
664 case 1:
665 case 2:
666 mmc->capacity = mmc->capacity_boot;
667 break;
668 case 3:
669 mmc->capacity = mmc->capacity_rpmb;
670 break;
671 case 4:
672 case 5:
673 case 6:
674 case 7:
675 mmc->capacity = mmc->capacity_gp[part_num - 4];
676 break;
677 default:
678 return -1;
679 }
680
Simon Glassc40fdca2016-05-01 13:52:35 -0600681 mmc_get_blk_desc(mmc)->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Stephen Warrenf866a462013-06-11 15:14:01 -0600682
683 return 0;
684}
685
Simon Glass7dba0b92016-06-12 23:30:15 -0600686int mmc_switch_part(struct mmc *mmc, unsigned int part_num)
Lei Wenbc897b12011-05-02 16:26:26 +0000687{
Stephen Warrenf866a462013-06-11 15:14:01 -0600688 int ret;
Lei Wenbc897b12011-05-02 16:26:26 +0000689
Stephen Warrenf866a462013-06-11 15:14:01 -0600690 ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
691 (mmc->part_config & ~PART_ACCESS_MASK)
692 | (part_num & PART_ACCESS_MASK));
Stephen Warrenf866a462013-06-11 15:14:01 -0600693
Peter Bigot6dc93e72014-09-02 18:31:23 -0500694 /*
695 * Set the capacity if the switch succeeded or was intended
696 * to return to representing the raw device.
697 */
Stephen Warren873cc1d2015-12-07 11:38:49 -0700698 if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0))) {
Peter Bigot6dc93e72014-09-02 18:31:23 -0500699 ret = mmc_set_capacity(mmc, part_num);
Simon Glassfdbb1392016-05-01 13:52:37 -0600700 mmc_get_blk_desc(mmc)->hwpart = part_num;
Stephen Warren873cc1d2015-12-07 11:38:49 -0700701 }
Peter Bigot6dc93e72014-09-02 18:31:23 -0500702
703 return ret;
Lei Wenbc897b12011-05-02 16:26:26 +0000704}
705
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +0100706int mmc_hwpart_config(struct mmc *mmc,
707 const struct mmc_hwpart_conf *conf,
708 enum mmc_hwpart_conf_mode mode)
709{
710 u8 part_attrs = 0;
711 u32 enh_size_mult;
712 u32 enh_start_addr;
713 u32 gp_size_mult[4];
714 u32 max_enh_size_mult;
715 u32 tot_enh_size_mult = 0;
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +0100716 u8 wr_rel_set;
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +0100717 int i, pidx, err;
718 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
719
720 if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE)
721 return -EINVAL;
722
723 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) {
724 printf("eMMC >= 4.4 required for enhanced user data area\n");
725 return -EMEDIUMTYPE;
726 }
727
728 if (!(mmc->part_support & PART_SUPPORT)) {
729 printf("Card does not support partitioning\n");
730 return -EMEDIUMTYPE;
731 }
732
733 if (!mmc->hc_wp_grp_size) {
734 printf("Card does not define HC WP group size\n");
735 return -EMEDIUMTYPE;
736 }
737
738 /* check partition alignment and total enhanced size */
739 if (conf->user.enh_size) {
740 if (conf->user.enh_size % mmc->hc_wp_grp_size ||
741 conf->user.enh_start % mmc->hc_wp_grp_size) {
742 printf("User data enhanced area not HC WP group "
743 "size aligned\n");
744 return -EINVAL;
745 }
746 part_attrs |= EXT_CSD_ENH_USR;
747 enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size;
748 if (mmc->high_capacity) {
749 enh_start_addr = conf->user.enh_start;
750 } else {
751 enh_start_addr = (conf->user.enh_start << 9);
752 }
753 } else {
754 enh_size_mult = 0;
755 enh_start_addr = 0;
756 }
757 tot_enh_size_mult += enh_size_mult;
758
759 for (pidx = 0; pidx < 4; pidx++) {
760 if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) {
761 printf("GP%i partition not HC WP group size "
762 "aligned\n", pidx+1);
763 return -EINVAL;
764 }
765 gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size;
766 if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) {
767 part_attrs |= EXT_CSD_ENH_GP(pidx);
768 tot_enh_size_mult += gp_size_mult[pidx];
769 }
770 }
771
772 if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) {
773 printf("Card does not support enhanced attribute\n");
774 return -EMEDIUMTYPE;
775 }
776
777 err = mmc_send_ext_csd(mmc, ext_csd);
778 if (err)
779 return err;
780
781 max_enh_size_mult =
782 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) +
783 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) +
784 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT];
785 if (tot_enh_size_mult > max_enh_size_mult) {
786 printf("Total enhanced size exceeds maximum (%u > %u)\n",
787 tot_enh_size_mult, max_enh_size_mult);
788 return -EMEDIUMTYPE;
789 }
790
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +0100791 /* The default value of EXT_CSD_WR_REL_SET is device
792 * dependent, the values can only be changed if the
793 * EXT_CSD_HS_CTRL_REL bit is set. The values can be
794 * changed only once and before partitioning is completed. */
795 wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
796 if (conf->user.wr_rel_change) {
797 if (conf->user.wr_rel_set)
798 wr_rel_set |= EXT_CSD_WR_DATA_REL_USR;
799 else
800 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR;
801 }
802 for (pidx = 0; pidx < 4; pidx++) {
803 if (conf->gp_part[pidx].wr_rel_change) {
804 if (conf->gp_part[pidx].wr_rel_set)
805 wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx);
806 else
807 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx);
808 }
809 }
810
811 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] &&
812 !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) {
813 puts("Card does not support host controlled partition write "
814 "reliability settings\n");
815 return -EMEDIUMTYPE;
816 }
817
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +0100818 if (ext_csd[EXT_CSD_PARTITION_SETTING] &
819 EXT_CSD_PARTITION_SETTING_COMPLETED) {
820 printf("Card already partitioned\n");
821 return -EPERM;
822 }
823
824 if (mode == MMC_HWPART_CONF_CHECK)
825 return 0;
826
827 /* Partitioning requires high-capacity size definitions */
828 if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) {
829 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
830 EXT_CSD_ERASE_GROUP_DEF, 1);
831
832 if (err)
833 return err;
834
835 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
836
837 /* update erase group size to be high-capacity */
838 mmc->erase_grp_size =
839 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
840
841 }
842
843 /* all OK, write the configuration */
844 for (i = 0; i < 4; i++) {
845 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
846 EXT_CSD_ENH_START_ADDR+i,
847 (enh_start_addr >> (i*8)) & 0xFF);
848 if (err)
849 return err;
850 }
851 for (i = 0; i < 3; i++) {
852 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
853 EXT_CSD_ENH_SIZE_MULT+i,
854 (enh_size_mult >> (i*8)) & 0xFF);
855 if (err)
856 return err;
857 }
858 for (pidx = 0; pidx < 4; pidx++) {
859 for (i = 0; i < 3; i++) {
860 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
861 EXT_CSD_GP_SIZE_MULT+pidx*3+i,
862 (gp_size_mult[pidx] >> (i*8)) & 0xFF);
863 if (err)
864 return err;
865 }
866 }
867 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
868 EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs);
869 if (err)
870 return err;
871
872 if (mode == MMC_HWPART_CONF_SET)
873 return 0;
874
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +0100875 /* The WR_REL_SET is a write-once register but shall be
876 * written before setting PART_SETTING_COMPLETED. As it is
877 * write-once we can only write it when completing the
878 * partitioning. */
879 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) {
880 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
881 EXT_CSD_WR_REL_SET, wr_rel_set);
882 if (err)
883 return err;
884 }
885
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +0100886 /* Setting PART_SETTING_COMPLETED confirms the partition
887 * configuration but it only becomes effective after power
888 * cycle, so we do not adjust the partition related settings
889 * in the mmc struct. */
890
891 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
892 EXT_CSD_PARTITION_SETTING,
893 EXT_CSD_PARTITION_SETTING_COMPLETED);
894 if (err)
895 return err;
896
897 return 0;
898}
899
Simon Glasse7881d82017-07-29 11:35:31 -0600900#if !CONFIG_IS_ENABLED(DM_MMC)
Thierry Reding48972d92012-01-02 01:15:37 +0000901int mmc_getcd(struct mmc *mmc)
902{
903 int cd;
904
905 cd = board_mmc_getcd(mmc);
906
Peter Korsgaardd4e1da42013-03-21 04:00:03 +0000907 if (cd < 0) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200908 if (mmc->cfg->ops->getcd)
909 cd = mmc->cfg->ops->getcd(mmc);
Peter Korsgaardd4e1da42013-03-21 04:00:03 +0000910 else
911 cd = 1;
912 }
Thierry Reding48972d92012-01-02 01:15:37 +0000913
914 return cd;
915}
Simon Glass8ca51e52016-06-12 23:30:22 -0600916#endif
Thierry Reding48972d92012-01-02 01:15:37 +0000917
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000918static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
Andy Fleming272cc702008-10-30 16:41:01 -0500919{
920 struct mmc_cmd cmd;
921 struct mmc_data data;
922
923 /* Switch the frequency */
924 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
925 cmd.resp_type = MMC_RSP_R1;
926 cmd.cmdarg = (mode << 31) | 0xffffff;
927 cmd.cmdarg &= ~(0xf << (group * 4));
928 cmd.cmdarg |= value << (group * 4);
Andy Fleming272cc702008-10-30 16:41:01 -0500929
930 data.dest = (char *)resp;
931 data.blocksize = 64;
932 data.blocks = 1;
933 data.flags = MMC_DATA_READ;
934
935 return mmc_send_cmd(mmc, &cmd, &data);
936}
937
938
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000939static int sd_change_freq(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500940{
941 int err;
942 struct mmc_cmd cmd;
Suniel Mahesh18e7c8f2017-10-05 11:32:00 +0530943 ALLOC_CACHE_ALIGN_BUFFER(__be32, scr, 2);
944 ALLOC_CACHE_ALIGN_BUFFER(__be32, switch_status, 16);
Andy Fleming272cc702008-10-30 16:41:01 -0500945 struct mmc_data data;
946 int timeout;
947
948 mmc->card_caps = 0;
949
Thomas Choud52ebf12010-12-24 13:12:21 +0000950 if (mmc_host_is_spi(mmc))
951 return 0;
952
Andy Fleming272cc702008-10-30 16:41:01 -0500953 /* Read the SCR to find out if this card supports higher speeds */
954 cmd.cmdidx = MMC_CMD_APP_CMD;
955 cmd.resp_type = MMC_RSP_R1;
956 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -0500957
958 err = mmc_send_cmd(mmc, &cmd, NULL);
959
960 if (err)
961 return err;
962
963 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
964 cmd.resp_type = MMC_RSP_R1;
965 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500966
967 timeout = 3;
968
969retry_scr:
Anton staaff781dd32011-10-03 13:54:59 +0000970 data.dest = (char *)scr;
Andy Fleming272cc702008-10-30 16:41:01 -0500971 data.blocksize = 8;
972 data.blocks = 1;
973 data.flags = MMC_DATA_READ;
974
975 err = mmc_send_cmd(mmc, &cmd, &data);
976
977 if (err) {
978 if (timeout--)
979 goto retry_scr;
980
981 return err;
982 }
983
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300984 mmc->scr[0] = __be32_to_cpu(scr[0]);
985 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Fleming272cc702008-10-30 16:41:01 -0500986
987 switch ((mmc->scr[0] >> 24) & 0xf) {
Bin Meng53e8e402016-03-17 21:53:13 -0700988 case 0:
989 mmc->version = SD_VERSION_1_0;
990 break;
991 case 1:
992 mmc->version = SD_VERSION_1_10;
993 break;
994 case 2:
995 mmc->version = SD_VERSION_2;
996 if ((mmc->scr[0] >> 15) & 0x1)
997 mmc->version = SD_VERSION_3;
998 break;
999 default:
1000 mmc->version = SD_VERSION_1_0;
1001 break;
Andy Fleming272cc702008-10-30 16:41:01 -05001002 }
1003
Alagu Sankarb44c7082010-05-12 15:08:24 +05301004 if (mmc->scr[0] & SD_DATA_4BIT)
1005 mmc->card_caps |= MMC_MODE_4BIT;
1006
Andy Fleming272cc702008-10-30 16:41:01 -05001007 /* Version 1.0 doesn't support switching */
1008 if (mmc->version == SD_VERSION_1_0)
1009 return 0;
1010
1011 timeout = 4;
1012 while (timeout--) {
1013 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
Anton staaff781dd32011-10-03 13:54:59 +00001014 (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -05001015
1016 if (err)
1017 return err;
1018
1019 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +03001020 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Fleming272cc702008-10-30 16:41:01 -05001021 break;
1022 }
1023
Andy Fleming272cc702008-10-30 16:41:01 -05001024 /* If high-speed isn't supported, we return */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +03001025 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
Andy Fleming272cc702008-10-30 16:41:01 -05001026 return 0;
1027
Macpaul Lin2c3fbf42011-11-28 16:31:09 +00001028 /*
1029 * If the host doesn't support SD_HIGHSPEED, do not switch card to
1030 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
1031 * This can avoid furthur problem when the card runs in different
1032 * mode between the host.
1033 */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001034 if (!((mmc->cfg->host_caps & MMC_MODE_HS_52MHz) &&
1035 (mmc->cfg->host_caps & MMC_MODE_HS)))
Macpaul Lin2c3fbf42011-11-28 16:31:09 +00001036 return 0;
1037
Anton staaff781dd32011-10-03 13:54:59 +00001038 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -05001039
1040 if (err)
1041 return err;
1042
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +03001043 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
Andy Fleming272cc702008-10-30 16:41:01 -05001044 mmc->card_caps |= MMC_MODE_HS;
1045
1046 return 0;
1047}
1048
Peng Fan3697e592016-09-01 11:13:38 +08001049static int sd_read_ssr(struct mmc *mmc)
1050{
1051 int err, i;
1052 struct mmc_cmd cmd;
1053 ALLOC_CACHE_ALIGN_BUFFER(uint, ssr, 16);
1054 struct mmc_data data;
1055 int timeout = 3;
1056 unsigned int au, eo, et, es;
1057
1058 cmd.cmdidx = MMC_CMD_APP_CMD;
1059 cmd.resp_type = MMC_RSP_R1;
1060 cmd.cmdarg = mmc->rca << 16;
1061
1062 err = mmc_send_cmd(mmc, &cmd, NULL);
1063 if (err)
1064 return err;
1065
1066 cmd.cmdidx = SD_CMD_APP_SD_STATUS;
1067 cmd.resp_type = MMC_RSP_R1;
1068 cmd.cmdarg = 0;
1069
1070retry_ssr:
1071 data.dest = (char *)ssr;
1072 data.blocksize = 64;
1073 data.blocks = 1;
1074 data.flags = MMC_DATA_READ;
1075
1076 err = mmc_send_cmd(mmc, &cmd, &data);
1077 if (err) {
1078 if (timeout--)
1079 goto retry_ssr;
1080
1081 return err;
1082 }
1083
1084 for (i = 0; i < 16; i++)
1085 ssr[i] = be32_to_cpu(ssr[i]);
1086
1087 au = (ssr[2] >> 12) & 0xF;
1088 if ((au <= 9) || (mmc->version == SD_VERSION_3)) {
1089 mmc->ssr.au = sd_au_size[au];
1090 es = (ssr[3] >> 24) & 0xFF;
1091 es |= (ssr[2] & 0xFF) << 8;
1092 et = (ssr[3] >> 18) & 0x3F;
1093 if (es && et) {
1094 eo = (ssr[3] >> 16) & 0x3;
1095 mmc->ssr.erase_timeout = (et * 1000) / es;
1096 mmc->ssr.erase_offset = eo * 1000;
1097 }
1098 } else {
1099 debug("Invalid Allocation Unit Size.\n");
1100 }
1101
1102 return 0;
1103}
1104
Andy Fleming272cc702008-10-30 16:41:01 -05001105/* frequency bases */
1106/* divided by 10 to be nice to platforms without floating point */
Mike Frysinger5f837c22010-10-20 01:15:53 +00001107static const int fbase[] = {
Andy Fleming272cc702008-10-30 16:41:01 -05001108 10000,
1109 100000,
1110 1000000,
1111 10000000,
1112};
1113
1114/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
1115 * to platforms without floating point.
1116 */
Simon Glass61fe0762016-05-14 14:02:57 -06001117static const u8 multipliers[] = {
Andy Fleming272cc702008-10-30 16:41:01 -05001118 0, /* reserved */
1119 10,
1120 12,
1121 13,
1122 15,
1123 20,
1124 25,
1125 30,
1126 35,
1127 40,
1128 45,
1129 50,
1130 55,
1131 60,
1132 70,
1133 80,
1134};
1135
Simon Glasse7881d82017-07-29 11:35:31 -06001136#if !CONFIG_IS_ENABLED(DM_MMC)
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001137static void mmc_set_ios(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001138{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001139 if (mmc->cfg->ops->set_ios)
1140 mmc->cfg->ops->set_ios(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001141}
Simon Glass8ca51e52016-06-12 23:30:22 -06001142#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001143
1144void mmc_set_clock(struct mmc *mmc, uint clock)
1145{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001146 if (clock > mmc->cfg->f_max)
1147 clock = mmc->cfg->f_max;
Andy Fleming272cc702008-10-30 16:41:01 -05001148
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001149 if (clock < mmc->cfg->f_min)
1150 clock = mmc->cfg->f_min;
Andy Fleming272cc702008-10-30 16:41:01 -05001151
1152 mmc->clock = clock;
1153
1154 mmc_set_ios(mmc);
1155}
1156
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001157static void mmc_set_bus_width(struct mmc *mmc, uint width)
Andy Fleming272cc702008-10-30 16:41:01 -05001158{
1159 mmc->bus_width = width;
1160
1161 mmc_set_ios(mmc);
1162}
1163
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001164#if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG)
1165/*
1166 * helper function to display the capabilities in a human
1167 * friendly manner. The capabilities include bus width and
1168 * supported modes.
1169 */
1170void mmc_dump_capabilities(const char *text, uint caps)
1171{
1172 enum bus_mode mode;
1173
1174 printf("%s: widths [", text);
1175 if (caps & MMC_MODE_8BIT)
1176 printf("8, ");
1177 if (caps & MMC_MODE_4BIT)
1178 printf("4, ");
1179 printf("1] modes [");
1180
1181 for (mode = MMC_LEGACY; mode < MMC_MODES_END; mode++)
1182 if (MMC_CAP(mode) & caps)
1183 printf("%s, ", mmc_mode_name(mode));
1184 printf("\b\b]\n");
1185}
1186#endif
1187
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001188static int sd_select_bus_freq_width(struct mmc *mmc)
1189{
1190 int err;
1191 struct mmc_cmd cmd;
1192
1193 err = sd_change_freq(mmc);
1194 if (err)
1195 return err;
1196
1197 /* Restrict card's capabilities by what the host can do */
1198 mmc->card_caps &= mmc->cfg->host_caps;
1199
1200 if (mmc->card_caps & MMC_MODE_4BIT) {
1201 cmd.cmdidx = MMC_CMD_APP_CMD;
1202 cmd.resp_type = MMC_RSP_R1;
1203 cmd.cmdarg = mmc->rca << 16;
1204
1205 err = mmc_send_cmd(mmc, &cmd, NULL);
1206 if (err)
1207 return err;
1208
1209 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1210 cmd.resp_type = MMC_RSP_R1;
1211 cmd.cmdarg = 2;
1212 err = mmc_send_cmd(mmc, &cmd, NULL);
1213 if (err)
1214 return err;
1215
1216 mmc_set_bus_width(mmc, 4);
1217 }
1218
1219 err = sd_read_ssr(mmc);
1220 if (err)
1221 return err;
1222
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +02001223 if (mmc->card_caps & MMC_MODE_HS)
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02001224 mmc_select_mode(mmc, SD_HS);
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +02001225 else
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02001226 mmc_select_mode(mmc, SD_LEGACY);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001227
1228 return 0;
1229}
1230
Jean-Jacques Hiblot7382e692017-09-21 16:29:52 +02001231/*
1232 * read the compare the part of ext csd that is constant.
1233 * This can be used to check that the transfer is working
1234 * as expected.
1235 */
1236static int mmc_read_and_compare_ext_csd(struct mmc *mmc)
1237{
1238 int err;
1239 const u8 *ext_csd = mmc->ext_csd;
1240 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
1241
1242 err = mmc_send_ext_csd(mmc, test_csd);
1243 if (err)
1244 return err;
1245
1246 /* Only compare read only fields */
1247 if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT]
1248 == test_csd[EXT_CSD_PARTITIONING_SUPPORT] &&
1249 ext_csd[EXT_CSD_HC_WP_GRP_SIZE]
1250 == test_csd[EXT_CSD_HC_WP_GRP_SIZE] &&
1251 ext_csd[EXT_CSD_REV]
1252 == test_csd[EXT_CSD_REV] &&
1253 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1254 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] &&
1255 memcmp(&ext_csd[EXT_CSD_SEC_CNT],
1256 &test_csd[EXT_CSD_SEC_CNT], 4) == 0)
1257 return 0;
1258
1259 return -EBADMSG;
1260}
1261
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001262static int mmc_select_bus_freq_width(struct mmc *mmc)
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001263{
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001264 /* An array of possible bus widths in order of preference */
1265 static const unsigned int ext_csd_bits[] = {
1266 EXT_CSD_DDR_BUS_WIDTH_8,
1267 EXT_CSD_DDR_BUS_WIDTH_4,
1268 EXT_CSD_BUS_WIDTH_8,
1269 EXT_CSD_BUS_WIDTH_4,
1270 EXT_CSD_BUS_WIDTH_1,
1271 };
1272 /* An array to map CSD bus widths to host cap bits */
1273 static const unsigned int ext_to_hostcaps[] = {
1274 [EXT_CSD_DDR_BUS_WIDTH_4] =
1275 MMC_MODE_DDR_52MHz | MMC_MODE_4BIT,
1276 [EXT_CSD_DDR_BUS_WIDTH_8] =
1277 MMC_MODE_DDR_52MHz | MMC_MODE_8BIT,
1278 [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
1279 [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
1280 };
1281 /* An array to map chosen bus width to an integer */
1282 static const unsigned int widths[] = {
1283 8, 4, 8, 4, 1,
1284 };
1285 int err;
1286 int idx;
1287
1288 err = mmc_change_freq(mmc);
1289 if (err)
1290 return err;
1291
1292 /* Restrict card's capabilities by what the host can do */
1293 mmc->card_caps &= mmc->cfg->host_caps;
1294
1295 /* Only version 4 of MMC supports wider bus widths */
1296 if (mmc->version < MMC_VERSION_4)
1297 return 0;
1298
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001299 if (!mmc->ext_csd) {
1300 debug("No ext_csd found!\n"); /* this should enver happen */
1301 return -ENOTSUPP;
1302 }
1303
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001304 for (idx = 0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
1305 unsigned int extw = ext_csd_bits[idx];
1306 unsigned int caps = ext_to_hostcaps[extw];
1307 /*
1308 * If the bus width is still not changed,
1309 * don't try to set the default again.
1310 * Otherwise, recover from switch attempts
1311 * by switching to 1-bit bus width.
1312 */
1313 if (extw == EXT_CSD_BUS_WIDTH_1 &&
1314 mmc->bus_width == 1) {
1315 err = 0;
1316 break;
1317 }
1318
1319 /*
1320 * Check to make sure the card and controller support
1321 * these capabilities
1322 */
1323 if ((mmc->card_caps & caps) != caps)
1324 continue;
1325
1326 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1327 EXT_CSD_BUS_WIDTH, extw);
1328
1329 if (err)
1330 continue;
1331
1332 mmc->ddr_mode = (caps & MMC_MODE_DDR_52MHz) ? 1 : 0;
1333 mmc_set_bus_width(mmc, widths[idx]);
1334
Jean-Jacques Hiblot7382e692017-09-21 16:29:52 +02001335 err = mmc_read_and_compare_ext_csd(mmc);
1336 if (!err)
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001337 break;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001338 }
1339
1340 if (err)
1341 return err;
1342
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02001343 if (mmc->card_caps & MMC_MODE_HS_52MHz) {
1344 if (mmc->ddr_mode)
1345 mmc_select_mode(mmc, MMC_DDR_52);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001346 else
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02001347 mmc_select_mode(mmc, MMC_HS_52);
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +02001348 } else if (mmc->card_caps & MMC_MODE_HS)
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02001349 mmc_select_mode(mmc, MMC_HS);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001350
1351 return err;
1352}
1353
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001354static int mmc_startup_v4(struct mmc *mmc)
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02001355{
1356 int err, i;
1357 u64 capacity;
1358 bool has_parts = false;
1359 bool part_completed;
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001360 u8 *ext_csd;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02001361
1362 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4))
1363 return 0;
1364
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001365 ext_csd = malloc_cache_aligned(MMC_MAX_BLOCK_LEN);
1366 if (!ext_csd)
1367 return -ENOMEM;
1368
1369 mmc->ext_csd = ext_csd;
1370
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02001371 /* check ext_csd version and capacity */
1372 err = mmc_send_ext_csd(mmc, ext_csd);
1373 if (err)
1374 return err;
1375 if (ext_csd[EXT_CSD_REV] >= 2) {
1376 /*
1377 * According to the JEDEC Standard, the value of
1378 * ext_csd's capacity is valid if the value is more
1379 * than 2GB
1380 */
1381 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
1382 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
1383 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
1384 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
1385 capacity *= MMC_MAX_BLOCK_LEN;
1386 if ((capacity >> 20) > 2 * 1024)
1387 mmc->capacity_user = capacity;
1388 }
1389
1390 switch (ext_csd[EXT_CSD_REV]) {
1391 case 1:
1392 mmc->version = MMC_VERSION_4_1;
1393 break;
1394 case 2:
1395 mmc->version = MMC_VERSION_4_2;
1396 break;
1397 case 3:
1398 mmc->version = MMC_VERSION_4_3;
1399 break;
1400 case 5:
1401 mmc->version = MMC_VERSION_4_41;
1402 break;
1403 case 6:
1404 mmc->version = MMC_VERSION_4_5;
1405 break;
1406 case 7:
1407 mmc->version = MMC_VERSION_5_0;
1408 break;
1409 case 8:
1410 mmc->version = MMC_VERSION_5_1;
1411 break;
1412 }
1413
1414 /* The partition data may be non-zero but it is only
1415 * effective if PARTITION_SETTING_COMPLETED is set in
1416 * EXT_CSD, so ignore any data if this bit is not set,
1417 * except for enabling the high-capacity group size
1418 * definition (see below).
1419 */
1420 part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] &
1421 EXT_CSD_PARTITION_SETTING_COMPLETED);
1422
1423 /* store the partition info of emmc */
1424 mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT];
1425 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
1426 ext_csd[EXT_CSD_BOOT_MULT])
1427 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
1428 if (part_completed &&
1429 (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT))
1430 mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE];
1431
1432 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
1433
1434 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
1435
1436 for (i = 0; i < 4; i++) {
1437 int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
1438 uint mult = (ext_csd[idx + 2] << 16) +
1439 (ext_csd[idx + 1] << 8) + ext_csd[idx];
1440 if (mult)
1441 has_parts = true;
1442 if (!part_completed)
1443 continue;
1444 mmc->capacity_gp[i] = mult;
1445 mmc->capacity_gp[i] *=
1446 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1447 mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1448 mmc->capacity_gp[i] <<= 19;
1449 }
1450
1451 if (part_completed) {
1452 mmc->enh_user_size =
1453 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16) +
1454 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) +
1455 ext_csd[EXT_CSD_ENH_SIZE_MULT];
1456 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1457 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1458 mmc->enh_user_size <<= 19;
1459 mmc->enh_user_start =
1460 (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24) +
1461 (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) +
1462 (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) +
1463 ext_csd[EXT_CSD_ENH_START_ADDR];
1464 if (mmc->high_capacity)
1465 mmc->enh_user_start <<= 9;
1466 }
1467
1468 /*
1469 * Host needs to enable ERASE_GRP_DEF bit if device is
1470 * partitioned. This bit will be lost every time after a reset
1471 * or power off. This will affect erase size.
1472 */
1473 if (part_completed)
1474 has_parts = true;
1475 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
1476 (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB))
1477 has_parts = true;
1478 if (has_parts) {
1479 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1480 EXT_CSD_ERASE_GROUP_DEF, 1);
1481
1482 if (err)
1483 return err;
1484
1485 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
1486 }
1487
1488 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) {
1489 /* Read out group size from ext_csd */
1490 mmc->erase_grp_size =
1491 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
1492 /*
1493 * if high capacity and partition setting completed
1494 * SEC_COUNT is valid even if it is smaller than 2 GiB
1495 * JEDEC Standard JESD84-B45, 6.2.4
1496 */
1497 if (mmc->high_capacity && part_completed) {
1498 capacity = (ext_csd[EXT_CSD_SEC_CNT]) |
1499 (ext_csd[EXT_CSD_SEC_CNT + 1] << 8) |
1500 (ext_csd[EXT_CSD_SEC_CNT + 2] << 16) |
1501 (ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1502 capacity *= MMC_MAX_BLOCK_LEN;
1503 mmc->capacity_user = capacity;
1504 }
1505 } else {
1506 /* Calculate the group size from the csd value. */
1507 int erase_gsz, erase_gmul;
1508
1509 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1510 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1511 mmc->erase_grp_size = (erase_gsz + 1)
1512 * (erase_gmul + 1);
1513 }
1514
1515 mmc->hc_wp_grp_size = 1024
1516 * ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1517 * ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1518
1519 mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
1520
1521 return 0;
1522}
1523
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001524static int mmc_startup(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001525{
Stephen Warrenf866a462013-06-11 15:14:01 -06001526 int err, i;
Andy Fleming272cc702008-10-30 16:41:01 -05001527 uint mult, freq;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02001528 u64 cmult, csize;
Andy Fleming272cc702008-10-30 16:41:01 -05001529 struct mmc_cmd cmd;
Simon Glassc40fdca2016-05-01 13:52:35 -06001530 struct blk_desc *bdesc;
Andy Fleming272cc702008-10-30 16:41:01 -05001531
Thomas Choud52ebf12010-12-24 13:12:21 +00001532#ifdef CONFIG_MMC_SPI_CRC_ON
1533 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
1534 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
1535 cmd.resp_type = MMC_RSP_R1;
1536 cmd.cmdarg = 1;
Thomas Choud52ebf12010-12-24 13:12:21 +00001537 err = mmc_send_cmd(mmc, &cmd, NULL);
1538
1539 if (err)
1540 return err;
1541 }
1542#endif
1543
Andy Fleming272cc702008-10-30 16:41:01 -05001544 /* Put the Card in Identify Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00001545 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
1546 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
Andy Fleming272cc702008-10-30 16:41:01 -05001547 cmd.resp_type = MMC_RSP_R2;
1548 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05001549
1550 err = mmc_send_cmd(mmc, &cmd, NULL);
1551
1552 if (err)
1553 return err;
1554
1555 memcpy(mmc->cid, cmd.response, 16);
1556
1557 /*
1558 * For MMC cards, set the Relative Address.
1559 * For SD cards, get the Relatvie Address.
1560 * This also puts the cards into Standby State
1561 */
Thomas Choud52ebf12010-12-24 13:12:21 +00001562 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1563 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
1564 cmd.cmdarg = mmc->rca << 16;
1565 cmd.resp_type = MMC_RSP_R6;
Andy Fleming272cc702008-10-30 16:41:01 -05001566
Thomas Choud52ebf12010-12-24 13:12:21 +00001567 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05001568
Thomas Choud52ebf12010-12-24 13:12:21 +00001569 if (err)
1570 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001571
Thomas Choud52ebf12010-12-24 13:12:21 +00001572 if (IS_SD(mmc))
1573 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
1574 }
Andy Fleming272cc702008-10-30 16:41:01 -05001575
1576 /* Get the Card-Specific Data */
1577 cmd.cmdidx = MMC_CMD_SEND_CSD;
1578 cmd.resp_type = MMC_RSP_R2;
1579 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05001580
1581 err = mmc_send_cmd(mmc, &cmd, NULL);
1582
1583 if (err)
1584 return err;
1585
Rabin Vincent998be3d2009-04-05 13:30:56 +05301586 mmc->csd[0] = cmd.response[0];
1587 mmc->csd[1] = cmd.response[1];
1588 mmc->csd[2] = cmd.response[2];
1589 mmc->csd[3] = cmd.response[3];
Andy Fleming272cc702008-10-30 16:41:01 -05001590
1591 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincent0b453ff2009-04-05 13:30:55 +05301592 int version = (cmd.response[0] >> 26) & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -05001593
1594 switch (version) {
Bin Meng53e8e402016-03-17 21:53:13 -07001595 case 0:
1596 mmc->version = MMC_VERSION_1_2;
1597 break;
1598 case 1:
1599 mmc->version = MMC_VERSION_1_4;
1600 break;
1601 case 2:
1602 mmc->version = MMC_VERSION_2_2;
1603 break;
1604 case 3:
1605 mmc->version = MMC_VERSION_3;
1606 break;
1607 case 4:
1608 mmc->version = MMC_VERSION_4;
1609 break;
1610 default:
1611 mmc->version = MMC_VERSION_1_2;
1612 break;
Andy Fleming272cc702008-10-30 16:41:01 -05001613 }
1614 }
1615
1616 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincent0b453ff2009-04-05 13:30:55 +05301617 freq = fbase[(cmd.response[0] & 0x7)];
1618 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Fleming272cc702008-10-30 16:41:01 -05001619
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02001620 mmc->legacy_speed = freq * mult;
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02001621 mmc_select_mode(mmc, MMC_LEGACY);
Andy Fleming272cc702008-10-30 16:41:01 -05001622
Markus Niebelab711882013-12-16 13:40:46 +01001623 mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
Rabin Vincent998be3d2009-04-05 13:30:56 +05301624 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -05001625
1626 if (IS_SD(mmc))
1627 mmc->write_bl_len = mmc->read_bl_len;
1628 else
Rabin Vincent998be3d2009-04-05 13:30:56 +05301629 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -05001630
1631 if (mmc->high_capacity) {
1632 csize = (mmc->csd[1] & 0x3f) << 16
1633 | (mmc->csd[2] & 0xffff0000) >> 16;
1634 cmult = 8;
1635 } else {
1636 csize = (mmc->csd[1] & 0x3ff) << 2
1637 | (mmc->csd[2] & 0xc0000000) >> 30;
1638 cmult = (mmc->csd[2] & 0x00038000) >> 15;
1639 }
1640
Stephen Warrenf866a462013-06-11 15:14:01 -06001641 mmc->capacity_user = (csize + 1) << (cmult + 2);
1642 mmc->capacity_user *= mmc->read_bl_len;
1643 mmc->capacity_boot = 0;
1644 mmc->capacity_rpmb = 0;
1645 for (i = 0; i < 4; i++)
1646 mmc->capacity_gp[i] = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05001647
Simon Glass8bfa1952013-04-03 08:54:30 +00001648 if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
1649 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -05001650
Simon Glass8bfa1952013-04-03 08:54:30 +00001651 if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
1652 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -05001653
Markus Niebelab711882013-12-16 13:40:46 +01001654 if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
1655 cmd.cmdidx = MMC_CMD_SET_DSR;
1656 cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
1657 cmd.resp_type = MMC_RSP_NONE;
1658 if (mmc_send_cmd(mmc, &cmd, NULL))
1659 printf("MMC: SET_DSR failed\n");
1660 }
1661
Andy Fleming272cc702008-10-30 16:41:01 -05001662 /* Select the card, and put it into Transfer Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00001663 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1664 cmd.cmdidx = MMC_CMD_SELECT_CARD;
Ajay Bhargavfe8f7062011-10-05 03:13:23 +00001665 cmd.resp_type = MMC_RSP_R1;
Thomas Choud52ebf12010-12-24 13:12:21 +00001666 cmd.cmdarg = mmc->rca << 16;
Thomas Choud52ebf12010-12-24 13:12:21 +00001667 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05001668
Thomas Choud52ebf12010-12-24 13:12:21 +00001669 if (err)
1670 return err;
1671 }
Andy Fleming272cc702008-10-30 16:41:01 -05001672
Lei Wene6f99a52011-06-22 17:03:31 +00001673 /*
1674 * For SD, its erase group is always one sector
1675 */
1676 mmc->erase_grp_size = 1;
Lei Wenbc897b12011-05-02 16:26:26 +00001677 mmc->part_config = MMCPART_NOAVAILABLE;
Lei Wenbc897b12011-05-02 16:26:26 +00001678
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001679 err = mmc_startup_v4(mmc);
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02001680 if (err)
1681 return err;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05301682
Simon Glassc40fdca2016-05-01 13:52:35 -06001683 err = mmc_set_capacity(mmc, mmc_get_blk_desc(mmc)->hwpart);
Stephen Warrenf866a462013-06-11 15:14:01 -06001684 if (err)
1685 return err;
1686
Andy Fleming272cc702008-10-30 16:41:01 -05001687 if (IS_SD(mmc))
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001688 err = sd_select_bus_freq_width(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001689 else
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001690 err = mmc_select_bus_freq_width(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001691
1692 if (err)
1693 return err;
1694
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001695
Andrew Gabbasov5af8f452014-12-01 06:59:11 -06001696 /* Fix the block length for DDR mode */
1697 if (mmc->ddr_mode) {
1698 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
1699 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
1700 }
1701
Andy Fleming272cc702008-10-30 16:41:01 -05001702 /* fill in device description */
Simon Glassc40fdca2016-05-01 13:52:35 -06001703 bdesc = mmc_get_blk_desc(mmc);
1704 bdesc->lun = 0;
1705 bdesc->hwpart = 0;
1706 bdesc->type = 0;
1707 bdesc->blksz = mmc->read_bl_len;
1708 bdesc->log2blksz = LOG2(bdesc->blksz);
1709 bdesc->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Sjoerd Simonsfc011f62015-12-04 23:27:40 +01001710#if !defined(CONFIG_SPL_BUILD) || \
1711 (defined(CONFIG_SPL_LIBCOMMON_SUPPORT) && \
1712 !defined(CONFIG_USE_TINY_PRINTF))
Simon Glassc40fdca2016-05-01 13:52:35 -06001713 sprintf(bdesc->vendor, "Man %06x Snr %04x%04x",
Taylor Huttbabce5f2012-10-20 17:15:59 +00001714 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1715 (mmc->cid[3] >> 16) & 0xffff);
Simon Glassc40fdca2016-05-01 13:52:35 -06001716 sprintf(bdesc->product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
Taylor Huttbabce5f2012-10-20 17:15:59 +00001717 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1718 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1719 (mmc->cid[2] >> 24) & 0xff);
Simon Glassc40fdca2016-05-01 13:52:35 -06001720 sprintf(bdesc->revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
Taylor Huttbabce5f2012-10-20 17:15:59 +00001721 (mmc->cid[2] >> 16) & 0xf);
Paul Burton56196822013-09-04 16:12:25 +01001722#else
Simon Glassc40fdca2016-05-01 13:52:35 -06001723 bdesc->vendor[0] = 0;
1724 bdesc->product[0] = 0;
1725 bdesc->revision[0] = 0;
Paul Burton56196822013-09-04 16:12:25 +01001726#endif
Mikhail Kshevetskiy122efd42012-07-09 08:53:38 +00001727#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
Simon Glassc40fdca2016-05-01 13:52:35 -06001728 part_init(bdesc);
Mikhail Kshevetskiy122efd42012-07-09 08:53:38 +00001729#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001730
1731 return 0;
1732}
1733
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001734static int mmc_send_if_cond(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001735{
1736 struct mmc_cmd cmd;
1737 int err;
1738
1739 cmd.cmdidx = SD_CMD_SEND_IF_COND;
1740 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001741 cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
Andy Fleming272cc702008-10-30 16:41:01 -05001742 cmd.resp_type = MMC_RSP_R7;
Andy Fleming272cc702008-10-30 16:41:01 -05001743
1744 err = mmc_send_cmd(mmc, &cmd, NULL);
1745
1746 if (err)
1747 return err;
1748
Rabin Vincent998be3d2009-04-05 13:30:56 +05301749 if ((cmd.response[0] & 0xff) != 0xaa)
Jaehoon Chung915ffa52016-07-19 16:33:36 +09001750 return -EOPNOTSUPP;
Andy Fleming272cc702008-10-30 16:41:01 -05001751 else
1752 mmc->version = SD_VERSION_2;
1753
1754 return 0;
1755}
1756
Simon Glassc4d660d2017-07-04 13:31:19 -06001757#if !CONFIG_IS_ENABLED(DM_MMC)
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01001758/* board-specific MMC power initializations. */
1759__weak void board_mmc_power_init(void)
1760{
1761}
Simon Glass05cbeb72017-04-22 19:10:56 -06001762#endif
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01001763
Peng Fan2051aef2016-10-11 15:08:43 +08001764static int mmc_power_init(struct mmc *mmc)
1765{
Simon Glassc4d660d2017-07-04 13:31:19 -06001766#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02001767#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan2051aef2016-10-11 15:08:43 +08001768 int ret;
1769
1770 ret = device_get_supply_regulator(mmc->dev, "vmmc-supply",
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02001771 &mmc->vmmc_supply);
1772 if (ret)
Jaehoon Chung288db7c2016-10-24 15:22:22 +09001773 debug("%s: No vmmc supply\n", mmc->dev->name);
Peng Fan2051aef2016-10-11 15:08:43 +08001774
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02001775 ret = device_get_supply_regulator(mmc->dev, "vqmmc-supply",
1776 &mmc->vqmmc_supply);
1777 if (ret)
1778 debug("%s: No vqmmc supply\n", mmc->dev->name);
1779
1780 if (mmc->vmmc_supply) {
1781 ret = regulator_set_enable(mmc->vmmc_supply, true);
1782 if (ret) {
1783 puts("Error enabling VMMC supply\n");
1784 return ret;
1785 }
Peng Fan2051aef2016-10-11 15:08:43 +08001786 }
1787#endif
Simon Glass05cbeb72017-04-22 19:10:56 -06001788#else /* !CONFIG_DM_MMC */
1789 /*
1790 * Driver model should use a regulator, as above, rather than calling
1791 * out to board code.
1792 */
1793 board_mmc_power_init();
1794#endif
Peng Fan2051aef2016-10-11 15:08:43 +08001795 return 0;
1796}
1797
Che-Liang Chioue9550442012-11-28 15:21:13 +00001798int mmc_start_init(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001799{
Simon Glass8ca51e52016-06-12 23:30:22 -06001800 bool no_card;
Macpaul Linafd59322011-11-14 23:35:39 +00001801 int err;
Andy Fleming272cc702008-10-30 16:41:01 -05001802
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001803 /* we pretend there's no card when init is NULL */
Simon Glass8ca51e52016-06-12 23:30:22 -06001804 no_card = mmc_getcd(mmc) == 0;
Simon Glasse7881d82017-07-29 11:35:31 -06001805#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glass8ca51e52016-06-12 23:30:22 -06001806 no_card = no_card || (mmc->cfg->ops->init == NULL);
1807#endif
1808 if (no_card) {
Thierry Reding48972d92012-01-02 01:15:37 +00001809 mmc->has_init = 0;
Paul Burton56196822013-09-04 16:12:25 +01001810#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Thierry Reding48972d92012-01-02 01:15:37 +00001811 printf("MMC: no card present\n");
Paul Burton56196822013-09-04 16:12:25 +01001812#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +09001813 return -ENOMEDIUM;
Thierry Reding48972d92012-01-02 01:15:37 +00001814 }
1815
Lei Wenbc897b12011-05-02 16:26:26 +00001816 if (mmc->has_init)
1817 return 0;
1818
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +08001819#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
1820 mmc_adapter_card_type_ident();
1821#endif
Peng Fan2051aef2016-10-11 15:08:43 +08001822 err = mmc_power_init(mmc);
1823 if (err)
1824 return err;
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01001825
Simon Glasse7881d82017-07-29 11:35:31 -06001826#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass8ca51e52016-06-12 23:30:22 -06001827 /* The device has already been probed ready for use */
1828#else
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001829 /* made sure it's not NULL earlier */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001830 err = mmc->cfg->ops->init(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001831 if (err)
1832 return err;
Simon Glass8ca51e52016-06-12 23:30:22 -06001833#endif
Andrew Gabbasov786e8f82014-12-01 06:59:09 -06001834 mmc->ddr_mode = 0;
Ilya Yanokb86b85e2009-06-29 17:53:16 +04001835 mmc_set_bus_width(mmc, 1);
1836 mmc_set_clock(mmc, 1);
1837
Andy Fleming272cc702008-10-30 16:41:01 -05001838 /* Reset the Card */
1839 err = mmc_go_idle(mmc);
1840
1841 if (err)
1842 return err;
1843
Lei Wenbc897b12011-05-02 16:26:26 +00001844 /* The internal partition reset to user partition(0) at every CMD0*/
Simon Glassc40fdca2016-05-01 13:52:35 -06001845 mmc_get_blk_desc(mmc)->hwpart = 0;
Lei Wenbc897b12011-05-02 16:26:26 +00001846
Andy Fleming272cc702008-10-30 16:41:01 -05001847 /* Test for SD version 2 */
Macpaul Linafd59322011-11-14 23:35:39 +00001848 err = mmc_send_if_cond(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001849
Andy Fleming272cc702008-10-30 16:41:01 -05001850 /* Now try to get the SD card's operating condition */
1851 err = sd_send_op_cond(mmc);
1852
1853 /* If the command timed out, we check for an MMC card */
Jaehoon Chung915ffa52016-07-19 16:33:36 +09001854 if (err == -ETIMEDOUT) {
Andy Fleming272cc702008-10-30 16:41:01 -05001855 err = mmc_send_op_cond(mmc);
1856
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05001857 if (err) {
Paul Burton56196822013-09-04 16:12:25 +01001858#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Andy Fleming272cc702008-10-30 16:41:01 -05001859 printf("Card did not respond to voltage select!\n");
Paul Burton56196822013-09-04 16:12:25 +01001860#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +09001861 return -EOPNOTSUPP;
Andy Fleming272cc702008-10-30 16:41:01 -05001862 }
1863 }
1864
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05001865 if (!err)
Che-Liang Chioue9550442012-11-28 15:21:13 +00001866 mmc->init_in_progress = 1;
1867
1868 return err;
1869}
1870
1871static int mmc_complete_init(struct mmc *mmc)
1872{
1873 int err = 0;
1874
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05001875 mmc->init_in_progress = 0;
Che-Liang Chioue9550442012-11-28 15:21:13 +00001876 if (mmc->op_cond_pending)
1877 err = mmc_complete_op_cond(mmc);
1878
1879 if (!err)
1880 err = mmc_startup(mmc);
Lei Wenbc897b12011-05-02 16:26:26 +00001881 if (err)
1882 mmc->has_init = 0;
1883 else
1884 mmc->has_init = 1;
Che-Liang Chioue9550442012-11-28 15:21:13 +00001885 return err;
1886}
1887
1888int mmc_init(struct mmc *mmc)
1889{
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05001890 int err = 0;
Marek Vasutce9eca92016-12-01 02:06:32 +01001891 __maybe_unused unsigned start;
Simon Glassc4d660d2017-07-04 13:31:19 -06001892#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass33fb2112016-05-01 13:52:41 -06001893 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc->dev);
Che-Liang Chioue9550442012-11-28 15:21:13 +00001894
Simon Glass33fb2112016-05-01 13:52:41 -06001895 upriv->mmc = mmc;
1896#endif
Che-Liang Chioue9550442012-11-28 15:21:13 +00001897 if (mmc->has_init)
1898 return 0;
Mateusz Zalegad803fea2014-04-29 20:15:30 +02001899
1900 start = get_timer(0);
1901
Che-Liang Chioue9550442012-11-28 15:21:13 +00001902 if (!mmc->init_in_progress)
1903 err = mmc_start_init(mmc);
1904
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05001905 if (!err)
Che-Liang Chioue9550442012-11-28 15:21:13 +00001906 err = mmc_complete_init(mmc);
Jagan Teki919b4852017-01-10 11:18:43 +01001907 if (err)
1908 printf("%s: %d, time %lu\n", __func__, err, get_timer(start));
1909
Lei Wenbc897b12011-05-02 16:26:26 +00001910 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001911}
1912
Markus Niebelab711882013-12-16 13:40:46 +01001913int mmc_set_dsr(struct mmc *mmc, u16 val)
1914{
1915 mmc->dsr = val;
1916 return 0;
1917}
1918
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02001919/* CPU-specific MMC initializations */
1920__weak int cpu_mmc_init(bd_t *bis)
Andy Fleming272cc702008-10-30 16:41:01 -05001921{
1922 return -1;
1923}
1924
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02001925/* board-specific MMC initializations. */
1926__weak int board_mmc_init(bd_t *bis)
1927{
1928 return -1;
1929}
Andy Fleming272cc702008-10-30 16:41:01 -05001930
Che-Liang Chioue9550442012-11-28 15:21:13 +00001931void mmc_set_preinit(struct mmc *mmc, int preinit)
1932{
1933 mmc->preinit = preinit;
1934}
1935
Simon Glassc4d660d2017-07-04 13:31:19 -06001936#if CONFIG_IS_ENABLED(DM_MMC) && defined(CONFIG_SPL_BUILD)
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06001937static int mmc_probe(bd_t *bis)
1938{
1939 return 0;
1940}
Simon Glassc4d660d2017-07-04 13:31:19 -06001941#elif CONFIG_IS_ENABLED(DM_MMC)
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06001942static int mmc_probe(bd_t *bis)
1943{
Simon Glass4a1db6d2015-12-29 05:22:49 -07001944 int ret, i;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06001945 struct uclass *uc;
Simon Glass4a1db6d2015-12-29 05:22:49 -07001946 struct udevice *dev;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06001947
1948 ret = uclass_get(UCLASS_MMC, &uc);
1949 if (ret)
1950 return ret;
1951
Simon Glass4a1db6d2015-12-29 05:22:49 -07001952 /*
1953 * Try to add them in sequence order. Really with driver model we
1954 * should allow holes, but the current MMC list does not allow that.
1955 * So if we request 0, 1, 3 we will get 0, 1, 2.
1956 */
1957 for (i = 0; ; i++) {
1958 ret = uclass_get_device_by_seq(UCLASS_MMC, i, &dev);
1959 if (ret == -ENODEV)
1960 break;
1961 }
1962 uclass_foreach_dev(dev, uc) {
1963 ret = device_probe(dev);
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06001964 if (ret)
Simon Glass4a1db6d2015-12-29 05:22:49 -07001965 printf("%s - probe failed: %d\n", dev->name, ret);
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06001966 }
1967
1968 return 0;
1969}
1970#else
1971static int mmc_probe(bd_t *bis)
1972{
1973 if (board_mmc_init(bis) < 0)
1974 cpu_mmc_init(bis);
1975
1976 return 0;
1977}
1978#endif
Che-Liang Chioue9550442012-11-28 15:21:13 +00001979
Andy Fleming272cc702008-10-30 16:41:01 -05001980int mmc_initialize(bd_t *bis)
1981{
Daniel Kochmański1b26bab2015-05-29 16:55:43 +02001982 static int initialized = 0;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06001983 int ret;
Daniel Kochmański1b26bab2015-05-29 16:55:43 +02001984 if (initialized) /* Avoid initializing mmc multiple times */
1985 return 0;
1986 initialized = 1;
1987
Simon Glassc4d660d2017-07-04 13:31:19 -06001988#if !CONFIG_IS_ENABLED(BLK)
Marek Vasutb5b838f2016-12-01 02:06:33 +01001989#if !CONFIG_IS_ENABLED(MMC_TINY)
Simon Glassc40fdca2016-05-01 13:52:35 -06001990 mmc_list_init();
1991#endif
Marek Vasutb5b838f2016-12-01 02:06:33 +01001992#endif
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06001993 ret = mmc_probe(bis);
1994 if (ret)
1995 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -05001996
Ying Zhangbb0dc102013-08-16 15:16:11 +08001997#ifndef CONFIG_SPL_BUILD
Andy Fleming272cc702008-10-30 16:41:01 -05001998 print_mmc_devices(',');
Ying Zhangbb0dc102013-08-16 15:16:11 +08001999#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002000
Simon Glassc40fdca2016-05-01 13:52:35 -06002001 mmc_do_preinit();
Andy Fleming272cc702008-10-30 16:41:01 -05002002 return 0;
2003}
Tomas Melincd3d4882016-11-25 11:01:03 +02002004
2005#ifdef CONFIG_CMD_BKOPS_ENABLE
2006int mmc_set_bkops_enable(struct mmc *mmc)
2007{
2008 int err;
2009 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
2010
2011 err = mmc_send_ext_csd(mmc, ext_csd);
2012 if (err) {
2013 puts("Could not get ext_csd register values\n");
2014 return err;
2015 }
2016
2017 if (!(ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1)) {
2018 puts("Background operations not supported on device\n");
2019 return -EMEDIUMTYPE;
2020 }
2021
2022 if (ext_csd[EXT_CSD_BKOPS_EN] & 0x1) {
2023 puts("Background operations already enabled\n");
2024 return 0;
2025 }
2026
2027 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BKOPS_EN, 1);
2028 if (err) {
2029 puts("Failed to enable manual background operations\n");
2030 return err;
2031 }
2032
2033 puts("Enabled manual background operations\n");
2034
2035 return 0;
2036}
2037#endif