blob: 3002b727c83412bde3402808293696ce53250797 [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;
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +0200240
241 if (cmd.response[0] & MMC_STATUS_MASK) {
Paul Burton56196822013-09-04 16:12:25 +0100242#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jan Kloetzked617c422012-02-05 22:29:12 +0000243 printf("Status Error: 0x%08X\n",
244 cmd.response[0]);
Paul Burton56196822013-09-04 16:12:25 +0100245#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900246 return -ECOMM;
Jan Kloetzked617c422012-02-05 22:29:12 +0000247 }
248 } else if (--retries < 0)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000249 return err;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000250
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500251 if (timeout-- <= 0)
252 break;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000253
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500254 udelay(1000);
255 }
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000256
Simon Glassc0c76eb2016-06-12 23:30:20 -0600257 mmc_trace_state(mmc, &cmd);
Jongman Heo5b0c9422012-06-03 21:32:13 +0000258 if (timeout <= 0) {
Paul Burton56196822013-09-04 16:12:25 +0100259#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000260 printf("Timeout waiting card ready\n");
Paul Burton56196822013-09-04 16:12:25 +0100261#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900262 return -ETIMEDOUT;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000263 }
264
265 return 0;
266}
267
Paul Burtonda61fa52013-09-09 15:30:26 +0100268int mmc_set_blocklen(struct mmc *mmc, int len)
Andy Fleming272cc702008-10-30 16:41:01 -0500269{
270 struct mmc_cmd cmd;
271
Andrew Gabbasov786e8f82014-12-01 06:59:09 -0600272 if (mmc->ddr_mode)
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900273 return 0;
274
Andy Fleming272cc702008-10-30 16:41:01 -0500275 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
276 cmd.resp_type = MMC_RSP_R1;
277 cmd.cmdarg = len;
Andy Fleming272cc702008-10-30 16:41:01 -0500278
279 return mmc_send_cmd(mmc, &cmd, NULL);
280}
281
Sascha Silbeff8fef52013-06-14 13:07:25 +0200282static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000283 lbaint_t blkcnt)
Andy Fleming272cc702008-10-30 16:41:01 -0500284{
285 struct mmc_cmd cmd;
286 struct mmc_data data;
287
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700288 if (blkcnt > 1)
289 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
290 else
291 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Fleming272cc702008-10-30 16:41:01 -0500292
293 if (mmc->high_capacity)
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700294 cmd.cmdarg = start;
Andy Fleming272cc702008-10-30 16:41:01 -0500295 else
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700296 cmd.cmdarg = start * mmc->read_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500297
298 cmd.resp_type = MMC_RSP_R1;
Andy Fleming272cc702008-10-30 16:41:01 -0500299
300 data.dest = dst;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700301 data.blocks = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500302 data.blocksize = mmc->read_bl_len;
303 data.flags = MMC_DATA_READ;
304
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700305 if (mmc_send_cmd(mmc, &cmd, &data))
306 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500307
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700308 if (blkcnt > 1) {
309 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
310 cmd.cmdarg = 0;
311 cmd.resp_type = MMC_RSP_R1b;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700312 if (mmc_send_cmd(mmc, &cmd, NULL)) {
Paul Burton56196822013-09-04 16:12:25 +0100313#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700314 printf("mmc fail to send stop cmd\n");
Paul Burton56196822013-09-04 16:12:25 +0100315#endif
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700316 return 0;
317 }
Andy Fleming272cc702008-10-30 16:41:01 -0500318 }
319
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700320 return blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500321}
322
Simon Glassc4d660d2017-07-04 13:31:19 -0600323#if CONFIG_IS_ENABLED(BLK)
Simon Glass7dba0b92016-06-12 23:30:15 -0600324ulong mmc_bread(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, void *dst)
Simon Glass33fb2112016-05-01 13:52:41 -0600325#else
Simon Glass7dba0b92016-06-12 23:30:15 -0600326ulong mmc_bread(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt,
327 void *dst)
Simon Glass33fb2112016-05-01 13:52:41 -0600328#endif
Andy Fleming272cc702008-10-30 16:41:01 -0500329{
Simon Glassc4d660d2017-07-04 13:31:19 -0600330#if CONFIG_IS_ENABLED(BLK)
Simon Glass33fb2112016-05-01 13:52:41 -0600331 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
332#endif
Simon Glassbcce53d2016-02-29 15:25:51 -0700333 int dev_num = block_dev->devnum;
Stephen Warren873cc1d2015-12-07 11:38:49 -0700334 int err;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700335 lbaint_t cur, blocks_todo = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500336
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700337 if (blkcnt == 0)
338 return 0;
339
340 struct mmc *mmc = find_mmc_device(dev_num);
Andy Fleming272cc702008-10-30 16:41:01 -0500341 if (!mmc)
342 return 0;
343
Marek Vasutb5b838f2016-12-01 02:06:33 +0100344 if (CONFIG_IS_ENABLED(MMC_TINY))
345 err = mmc_switch_part(mmc, block_dev->hwpart);
346 else
347 err = blk_dselect_hwpart(block_dev, block_dev->hwpart);
348
Stephen Warren873cc1d2015-12-07 11:38:49 -0700349 if (err < 0)
350 return 0;
351
Simon Glassc40fdca2016-05-01 13:52:35 -0600352 if ((start + blkcnt) > block_dev->lba) {
Paul Burton56196822013-09-04 16:12:25 +0100353#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Sascha Silbeff8fef52013-06-14 13:07:25 +0200354 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
Simon Glassc40fdca2016-05-01 13:52:35 -0600355 start + blkcnt, block_dev->lba);
Paul Burton56196822013-09-04 16:12:25 +0100356#endif
Lei Wend2bf29e2010-09-13 22:07:27 +0800357 return 0;
358 }
Andy Fleming272cc702008-10-30 16:41:01 -0500359
Simon Glass11692992015-06-23 15:38:50 -0600360 if (mmc_set_blocklen(mmc, mmc->read_bl_len)) {
361 debug("%s: Failed to set blocklen\n", __func__);
Andy Fleming272cc702008-10-30 16:41:01 -0500362 return 0;
Simon Glass11692992015-06-23 15:38:50 -0600363 }
Andy Fleming272cc702008-10-30 16:41:01 -0500364
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700365 do {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200366 cur = (blocks_todo > mmc->cfg->b_max) ?
367 mmc->cfg->b_max : blocks_todo;
Simon Glass11692992015-06-23 15:38:50 -0600368 if (mmc_read_blocks(mmc, dst, start, cur) != cur) {
369 debug("%s: Failed to read blocks\n", __func__);
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700370 return 0;
Simon Glass11692992015-06-23 15:38:50 -0600371 }
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700372 blocks_todo -= cur;
373 start += cur;
374 dst += cur * mmc->read_bl_len;
375 } while (blocks_todo > 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500376
377 return blkcnt;
378}
379
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000380static int mmc_go_idle(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500381{
382 struct mmc_cmd cmd;
383 int err;
384
385 udelay(1000);
386
387 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
388 cmd.cmdarg = 0;
389 cmd.resp_type = MMC_RSP_NONE;
Andy Fleming272cc702008-10-30 16:41:01 -0500390
391 err = mmc_send_cmd(mmc, &cmd, NULL);
392
393 if (err)
394 return err;
395
396 udelay(2000);
397
398 return 0;
399}
400
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000401static int sd_send_op_cond(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500402{
403 int timeout = 1000;
404 int err;
405 struct mmc_cmd cmd;
406
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500407 while (1) {
Andy Fleming272cc702008-10-30 16:41:01 -0500408 cmd.cmdidx = MMC_CMD_APP_CMD;
409 cmd.resp_type = MMC_RSP_R1;
410 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500411
412 err = mmc_send_cmd(mmc, &cmd, NULL);
413
414 if (err)
415 return err;
416
417 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
418 cmd.resp_type = MMC_RSP_R3;
Stefano Babic250de122010-01-20 18:20:39 +0100419
420 /*
421 * Most cards do not answer if some reserved bits
422 * in the ocr are set. However, Some controller
423 * can set bit 7 (reserved for low voltages), but
424 * how to manage low voltages SD card is not yet
425 * specified.
426 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000427 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200428 (mmc->cfg->voltages & 0xff8000);
Andy Fleming272cc702008-10-30 16:41:01 -0500429
430 if (mmc->version == SD_VERSION_2)
431 cmd.cmdarg |= OCR_HCS;
432
433 err = mmc_send_cmd(mmc, &cmd, NULL);
434
435 if (err)
436 return err;
437
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500438 if (cmd.response[0] & OCR_BUSY)
439 break;
Andy Fleming272cc702008-10-30 16:41:01 -0500440
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500441 if (timeout-- <= 0)
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900442 return -EOPNOTSUPP;
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500443
444 udelay(1000);
445 }
Andy Fleming272cc702008-10-30 16:41:01 -0500446
447 if (mmc->version != SD_VERSION_2)
448 mmc->version = SD_VERSION_1_0;
449
Thomas Choud52ebf12010-12-24 13:12:21 +0000450 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
451 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
452 cmd.resp_type = MMC_RSP_R3;
453 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000454
455 err = mmc_send_cmd(mmc, &cmd, NULL);
456
457 if (err)
458 return err;
459 }
460
Rabin Vincent998be3d2009-04-05 13:30:56 +0530461 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500462
463 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
464 mmc->rca = 0;
465
466 return 0;
467}
468
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500469static int mmc_send_op_cond_iter(struct mmc *mmc, int use_arg)
Andy Fleming272cc702008-10-30 16:41:01 -0500470{
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500471 struct mmc_cmd cmd;
Andy Fleming272cc702008-10-30 16:41:01 -0500472 int err;
473
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500474 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
475 cmd.resp_type = MMC_RSP_R3;
476 cmd.cmdarg = 0;
Rob Herring5a203972015-03-23 17:56:59 -0500477 if (use_arg && !mmc_host_is_spi(mmc))
478 cmd.cmdarg = OCR_HCS |
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200479 (mmc->cfg->voltages &
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500480 (mmc->ocr & OCR_VOLTAGE_MASK)) |
481 (mmc->ocr & OCR_ACCESS_MODE);
Che-Liang Chioue9550442012-11-28 15:21:13 +0000482
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500483 err = mmc_send_cmd(mmc, &cmd, NULL);
Che-Liang Chioue9550442012-11-28 15:21:13 +0000484 if (err)
485 return err;
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500486 mmc->ocr = cmd.response[0];
Che-Liang Chioue9550442012-11-28 15:21:13 +0000487 return 0;
488}
489
Jeroen Hofstee750121c2014-07-12 21:24:08 +0200490static int mmc_send_op_cond(struct mmc *mmc)
Che-Liang Chioue9550442012-11-28 15:21:13 +0000491{
Che-Liang Chioue9550442012-11-28 15:21:13 +0000492 int err, i;
493
Andy Fleming272cc702008-10-30 16:41:01 -0500494 /* Some cards seem to need this */
495 mmc_go_idle(mmc);
496
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000497 /* Asking to the card its capabilities */
Che-Liang Chioue9550442012-11-28 15:21:13 +0000498 for (i = 0; i < 2; i++) {
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500499 err = mmc_send_op_cond_iter(mmc, i != 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500500 if (err)
501 return err;
502
Che-Liang Chioue9550442012-11-28 15:21:13 +0000503 /* exit if not busy (flag seems to be inverted) */
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500504 if (mmc->ocr & OCR_BUSY)
Andrew Gabbasovbd47c132015-03-19 07:44:07 -0500505 break;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000506 }
Andrew Gabbasovbd47c132015-03-19 07:44:07 -0500507 mmc->op_cond_pending = 1;
508 return 0;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000509}
Andy Fleming272cc702008-10-30 16:41:01 -0500510
Jeroen Hofstee750121c2014-07-12 21:24:08 +0200511static int mmc_complete_op_cond(struct mmc *mmc)
Che-Liang Chioue9550442012-11-28 15:21:13 +0000512{
513 struct mmc_cmd cmd;
514 int timeout = 1000;
515 uint start;
516 int err;
517
518 mmc->op_cond_pending = 0;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500519 if (!(mmc->ocr & OCR_BUSY)) {
Yangbo Lud188b112016-08-02 15:33:18 +0800520 /* Some cards seem to need this */
521 mmc_go_idle(mmc);
522
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500523 start = get_timer(0);
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500524 while (1) {
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500525 err = mmc_send_op_cond_iter(mmc, 1);
526 if (err)
527 return err;
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500528 if (mmc->ocr & OCR_BUSY)
529 break;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500530 if (get_timer(start) > timeout)
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900531 return -EOPNOTSUPP;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500532 udelay(100);
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500533 }
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500534 }
Andy Fleming272cc702008-10-30 16:41:01 -0500535
Thomas Choud52ebf12010-12-24 13:12:21 +0000536 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
537 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
538 cmd.resp_type = MMC_RSP_R3;
539 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000540
541 err = mmc_send_cmd(mmc, &cmd, NULL);
542
543 if (err)
544 return err;
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500545
546 mmc->ocr = cmd.response[0];
Thomas Choud52ebf12010-12-24 13:12:21 +0000547 }
548
Andy Fleming272cc702008-10-30 16:41:01 -0500549 mmc->version = MMC_VERSION_UNKNOWN;
Andy Fleming272cc702008-10-30 16:41:01 -0500550
551 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
Stephen Warrendef816a2014-01-30 16:11:12 -0700552 mmc->rca = 1;
Andy Fleming272cc702008-10-30 16:41:01 -0500553
554 return 0;
555}
556
557
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000558static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
Andy Fleming272cc702008-10-30 16:41:01 -0500559{
560 struct mmc_cmd cmd;
561 struct mmc_data data;
562 int err;
563
564 /* Get the Card Status Register */
565 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
566 cmd.resp_type = MMC_RSP_R1;
567 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500568
Yoshihiro Shimodacdfd1ac2012-06-07 19:09:11 +0000569 data.dest = (char *)ext_csd;
Andy Fleming272cc702008-10-30 16:41:01 -0500570 data.blocks = 1;
Simon Glass8bfa1952013-04-03 08:54:30 +0000571 data.blocksize = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -0500572 data.flags = MMC_DATA_READ;
573
574 err = mmc_send_cmd(mmc, &cmd, &data);
575
576 return err;
577}
578
Simon Glassc40704f2016-06-12 23:30:18 -0600579int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
Andy Fleming272cc702008-10-30 16:41:01 -0500580{
581 struct mmc_cmd cmd;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000582 int timeout = 1000;
Maxime Riparda9003dc2016-11-04 16:18:08 +0100583 int retries = 3;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000584 int ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500585
586 cmd.cmdidx = MMC_CMD_SWITCH;
587 cmd.resp_type = MMC_RSP_R1b;
588 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000589 (index << 16) |
590 (value << 8);
Andy Fleming272cc702008-10-30 16:41:01 -0500591
Maxime Riparda9003dc2016-11-04 16:18:08 +0100592 while (retries > 0) {
593 ret = mmc_send_cmd(mmc, &cmd, NULL);
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000594
Maxime Riparda9003dc2016-11-04 16:18:08 +0100595 /* Waiting for the ready status */
596 if (!ret) {
597 ret = mmc_send_status(mmc, timeout);
598 return ret;
599 }
600
601 retries--;
602 }
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000603
604 return ret;
605
Andy Fleming272cc702008-10-30 16:41:01 -0500606}
607
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000608static int mmc_change_freq(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500609{
Simon Glass8bfa1952013-04-03 08:54:30 +0000610 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
Andy Fleming272cc702008-10-30 16:41:01 -0500611 char cardtype;
612 int err;
613
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +0200614 mmc->card_caps = MMC_MODE_1BIT;
Andy Fleming272cc702008-10-30 16:41:01 -0500615
Thomas Choud52ebf12010-12-24 13:12:21 +0000616 if (mmc_host_is_spi(mmc))
617 return 0;
618
Andy Fleming272cc702008-10-30 16:41:01 -0500619 /* Only version 4 supports high-speed */
620 if (mmc->version < MMC_VERSION_4)
621 return 0;
622
Andrew Gabbasovfc5b32f2014-12-25 10:22:25 -0600623 mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT;
624
Andy Fleming272cc702008-10-30 16:41:01 -0500625 err = mmc_send_ext_csd(mmc, ext_csd);
626
627 if (err)
628 return err;
629
Lei Wen0560db12011-10-03 20:35:10 +0000630 cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -0500631
632 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
633
634 if (err)
Heiko Schochera5e27b42016-06-07 08:31:21 +0200635 return err;
Andy Fleming272cc702008-10-30 16:41:01 -0500636
637 /* Now check to see that it worked */
638 err = mmc_send_ext_csd(mmc, ext_csd);
639
640 if (err)
641 return err;
642
643 /* No high-speed support */
Lei Wen0560db12011-10-03 20:35:10 +0000644 if (!ext_csd[EXT_CSD_HS_TIMING])
Andy Fleming272cc702008-10-30 16:41:01 -0500645 return 0;
646
647 /* High Speed is set, there are two types: 52MHz and 26MHz */
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900648 if (cardtype & EXT_CSD_CARD_TYPE_52) {
Andrew Gabbasov201d5ac2014-12-01 06:59:10 -0600649 if (cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V)
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900650 mmc->card_caps |= MMC_MODE_DDR_52MHz;
Andy Fleming272cc702008-10-30 16:41:01 -0500651 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900652 } else {
Andy Fleming272cc702008-10-30 16:41:01 -0500653 mmc->card_caps |= MMC_MODE_HS;
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900654 }
Andy Fleming272cc702008-10-30 16:41:01 -0500655
656 return 0;
657}
658
Stephen Warrenf866a462013-06-11 15:14:01 -0600659static int mmc_set_capacity(struct mmc *mmc, int part_num)
660{
661 switch (part_num) {
662 case 0:
663 mmc->capacity = mmc->capacity_user;
664 break;
665 case 1:
666 case 2:
667 mmc->capacity = mmc->capacity_boot;
668 break;
669 case 3:
670 mmc->capacity = mmc->capacity_rpmb;
671 break;
672 case 4:
673 case 5:
674 case 6:
675 case 7:
676 mmc->capacity = mmc->capacity_gp[part_num - 4];
677 break;
678 default:
679 return -1;
680 }
681
Simon Glassc40fdca2016-05-01 13:52:35 -0600682 mmc_get_blk_desc(mmc)->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Stephen Warrenf866a462013-06-11 15:14:01 -0600683
684 return 0;
685}
686
Simon Glass7dba0b92016-06-12 23:30:15 -0600687int mmc_switch_part(struct mmc *mmc, unsigned int part_num)
Lei Wenbc897b12011-05-02 16:26:26 +0000688{
Stephen Warrenf866a462013-06-11 15:14:01 -0600689 int ret;
Lei Wenbc897b12011-05-02 16:26:26 +0000690
Stephen Warrenf866a462013-06-11 15:14:01 -0600691 ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
692 (mmc->part_config & ~PART_ACCESS_MASK)
693 | (part_num & PART_ACCESS_MASK));
Stephen Warrenf866a462013-06-11 15:14:01 -0600694
Peter Bigot6dc93e72014-09-02 18:31:23 -0500695 /*
696 * Set the capacity if the switch succeeded or was intended
697 * to return to representing the raw device.
698 */
Stephen Warren873cc1d2015-12-07 11:38:49 -0700699 if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0))) {
Peter Bigot6dc93e72014-09-02 18:31:23 -0500700 ret = mmc_set_capacity(mmc, part_num);
Simon Glassfdbb1392016-05-01 13:52:37 -0600701 mmc_get_blk_desc(mmc)->hwpart = part_num;
Stephen Warren873cc1d2015-12-07 11:38:49 -0700702 }
Peter Bigot6dc93e72014-09-02 18:31:23 -0500703
704 return ret;
Lei Wenbc897b12011-05-02 16:26:26 +0000705}
706
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +0100707int mmc_hwpart_config(struct mmc *mmc,
708 const struct mmc_hwpart_conf *conf,
709 enum mmc_hwpart_conf_mode mode)
710{
711 u8 part_attrs = 0;
712 u32 enh_size_mult;
713 u32 enh_start_addr;
714 u32 gp_size_mult[4];
715 u32 max_enh_size_mult;
716 u32 tot_enh_size_mult = 0;
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +0100717 u8 wr_rel_set;
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +0100718 int i, pidx, err;
719 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
720
721 if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE)
722 return -EINVAL;
723
724 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) {
725 printf("eMMC >= 4.4 required for enhanced user data area\n");
726 return -EMEDIUMTYPE;
727 }
728
729 if (!(mmc->part_support & PART_SUPPORT)) {
730 printf("Card does not support partitioning\n");
731 return -EMEDIUMTYPE;
732 }
733
734 if (!mmc->hc_wp_grp_size) {
735 printf("Card does not define HC WP group size\n");
736 return -EMEDIUMTYPE;
737 }
738
739 /* check partition alignment and total enhanced size */
740 if (conf->user.enh_size) {
741 if (conf->user.enh_size % mmc->hc_wp_grp_size ||
742 conf->user.enh_start % mmc->hc_wp_grp_size) {
743 printf("User data enhanced area not HC WP group "
744 "size aligned\n");
745 return -EINVAL;
746 }
747 part_attrs |= EXT_CSD_ENH_USR;
748 enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size;
749 if (mmc->high_capacity) {
750 enh_start_addr = conf->user.enh_start;
751 } else {
752 enh_start_addr = (conf->user.enh_start << 9);
753 }
754 } else {
755 enh_size_mult = 0;
756 enh_start_addr = 0;
757 }
758 tot_enh_size_mult += enh_size_mult;
759
760 for (pidx = 0; pidx < 4; pidx++) {
761 if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) {
762 printf("GP%i partition not HC WP group size "
763 "aligned\n", pidx+1);
764 return -EINVAL;
765 }
766 gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size;
767 if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) {
768 part_attrs |= EXT_CSD_ENH_GP(pidx);
769 tot_enh_size_mult += gp_size_mult[pidx];
770 }
771 }
772
773 if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) {
774 printf("Card does not support enhanced attribute\n");
775 return -EMEDIUMTYPE;
776 }
777
778 err = mmc_send_ext_csd(mmc, ext_csd);
779 if (err)
780 return err;
781
782 max_enh_size_mult =
783 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) +
784 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) +
785 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT];
786 if (tot_enh_size_mult > max_enh_size_mult) {
787 printf("Total enhanced size exceeds maximum (%u > %u)\n",
788 tot_enh_size_mult, max_enh_size_mult);
789 return -EMEDIUMTYPE;
790 }
791
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +0100792 /* The default value of EXT_CSD_WR_REL_SET is device
793 * dependent, the values can only be changed if the
794 * EXT_CSD_HS_CTRL_REL bit is set. The values can be
795 * changed only once and before partitioning is completed. */
796 wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
797 if (conf->user.wr_rel_change) {
798 if (conf->user.wr_rel_set)
799 wr_rel_set |= EXT_CSD_WR_DATA_REL_USR;
800 else
801 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR;
802 }
803 for (pidx = 0; pidx < 4; pidx++) {
804 if (conf->gp_part[pidx].wr_rel_change) {
805 if (conf->gp_part[pidx].wr_rel_set)
806 wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx);
807 else
808 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx);
809 }
810 }
811
812 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] &&
813 !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) {
814 puts("Card does not support host controlled partition write "
815 "reliability settings\n");
816 return -EMEDIUMTYPE;
817 }
818
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +0100819 if (ext_csd[EXT_CSD_PARTITION_SETTING] &
820 EXT_CSD_PARTITION_SETTING_COMPLETED) {
821 printf("Card already partitioned\n");
822 return -EPERM;
823 }
824
825 if (mode == MMC_HWPART_CONF_CHECK)
826 return 0;
827
828 /* Partitioning requires high-capacity size definitions */
829 if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) {
830 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
831 EXT_CSD_ERASE_GROUP_DEF, 1);
832
833 if (err)
834 return err;
835
836 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
837
838 /* update erase group size to be high-capacity */
839 mmc->erase_grp_size =
840 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
841
842 }
843
844 /* all OK, write the configuration */
845 for (i = 0; i < 4; i++) {
846 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
847 EXT_CSD_ENH_START_ADDR+i,
848 (enh_start_addr >> (i*8)) & 0xFF);
849 if (err)
850 return err;
851 }
852 for (i = 0; i < 3; i++) {
853 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
854 EXT_CSD_ENH_SIZE_MULT+i,
855 (enh_size_mult >> (i*8)) & 0xFF);
856 if (err)
857 return err;
858 }
859 for (pidx = 0; pidx < 4; pidx++) {
860 for (i = 0; i < 3; i++) {
861 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
862 EXT_CSD_GP_SIZE_MULT+pidx*3+i,
863 (gp_size_mult[pidx] >> (i*8)) & 0xFF);
864 if (err)
865 return err;
866 }
867 }
868 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
869 EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs);
870 if (err)
871 return err;
872
873 if (mode == MMC_HWPART_CONF_SET)
874 return 0;
875
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +0100876 /* The WR_REL_SET is a write-once register but shall be
877 * written before setting PART_SETTING_COMPLETED. As it is
878 * write-once we can only write it when completing the
879 * partitioning. */
880 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) {
881 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
882 EXT_CSD_WR_REL_SET, wr_rel_set);
883 if (err)
884 return err;
885 }
886
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +0100887 /* Setting PART_SETTING_COMPLETED confirms the partition
888 * configuration but it only becomes effective after power
889 * cycle, so we do not adjust the partition related settings
890 * in the mmc struct. */
891
892 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
893 EXT_CSD_PARTITION_SETTING,
894 EXT_CSD_PARTITION_SETTING_COMPLETED);
895 if (err)
896 return err;
897
898 return 0;
899}
900
Simon Glasse7881d82017-07-29 11:35:31 -0600901#if !CONFIG_IS_ENABLED(DM_MMC)
Thierry Reding48972d92012-01-02 01:15:37 +0000902int mmc_getcd(struct mmc *mmc)
903{
904 int cd;
905
906 cd = board_mmc_getcd(mmc);
907
Peter Korsgaardd4e1da42013-03-21 04:00:03 +0000908 if (cd < 0) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200909 if (mmc->cfg->ops->getcd)
910 cd = mmc->cfg->ops->getcd(mmc);
Peter Korsgaardd4e1da42013-03-21 04:00:03 +0000911 else
912 cd = 1;
913 }
Thierry Reding48972d92012-01-02 01:15:37 +0000914
915 return cd;
916}
Simon Glass8ca51e52016-06-12 23:30:22 -0600917#endif
Thierry Reding48972d92012-01-02 01:15:37 +0000918
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000919static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
Andy Fleming272cc702008-10-30 16:41:01 -0500920{
921 struct mmc_cmd cmd;
922 struct mmc_data data;
923
924 /* Switch the frequency */
925 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
926 cmd.resp_type = MMC_RSP_R1;
927 cmd.cmdarg = (mode << 31) | 0xffffff;
928 cmd.cmdarg &= ~(0xf << (group * 4));
929 cmd.cmdarg |= value << (group * 4);
Andy Fleming272cc702008-10-30 16:41:01 -0500930
931 data.dest = (char *)resp;
932 data.blocksize = 64;
933 data.blocks = 1;
934 data.flags = MMC_DATA_READ;
935
936 return mmc_send_cmd(mmc, &cmd, &data);
937}
938
939
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +0200940static int sd_get_capabilities(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500941{
942 int err;
943 struct mmc_cmd cmd;
Suniel Mahesh18e7c8f2017-10-05 11:32:00 +0530944 ALLOC_CACHE_ALIGN_BUFFER(__be32, scr, 2);
945 ALLOC_CACHE_ALIGN_BUFFER(__be32, switch_status, 16);
Andy Fleming272cc702008-10-30 16:41:01 -0500946 struct mmc_data data;
947 int timeout;
948
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +0200949 mmc->card_caps = MMC_MODE_1BIT;
Andy Fleming272cc702008-10-30 16:41:01 -0500950
Thomas Choud52ebf12010-12-24 13:12:21 +0000951 if (mmc_host_is_spi(mmc))
952 return 0;
953
Andy Fleming272cc702008-10-30 16:41:01 -0500954 /* Read the SCR to find out if this card supports higher speeds */
955 cmd.cmdidx = MMC_CMD_APP_CMD;
956 cmd.resp_type = MMC_RSP_R1;
957 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -0500958
959 err = mmc_send_cmd(mmc, &cmd, NULL);
960
961 if (err)
962 return err;
963
964 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
965 cmd.resp_type = MMC_RSP_R1;
966 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500967
968 timeout = 3;
969
970retry_scr:
Anton staaff781dd32011-10-03 13:54:59 +0000971 data.dest = (char *)scr;
Andy Fleming272cc702008-10-30 16:41:01 -0500972 data.blocksize = 8;
973 data.blocks = 1;
974 data.flags = MMC_DATA_READ;
975
976 err = mmc_send_cmd(mmc, &cmd, &data);
977
978 if (err) {
979 if (timeout--)
980 goto retry_scr;
981
982 return err;
983 }
984
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300985 mmc->scr[0] = __be32_to_cpu(scr[0]);
986 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Fleming272cc702008-10-30 16:41:01 -0500987
988 switch ((mmc->scr[0] >> 24) & 0xf) {
Bin Meng53e8e402016-03-17 21:53:13 -0700989 case 0:
990 mmc->version = SD_VERSION_1_0;
991 break;
992 case 1:
993 mmc->version = SD_VERSION_1_10;
994 break;
995 case 2:
996 mmc->version = SD_VERSION_2;
997 if ((mmc->scr[0] >> 15) & 0x1)
998 mmc->version = SD_VERSION_3;
999 break;
1000 default:
1001 mmc->version = SD_VERSION_1_0;
1002 break;
Andy Fleming272cc702008-10-30 16:41:01 -05001003 }
1004
Alagu Sankarb44c7082010-05-12 15:08:24 +05301005 if (mmc->scr[0] & SD_DATA_4BIT)
1006 mmc->card_caps |= MMC_MODE_4BIT;
1007
Andy Fleming272cc702008-10-30 16:41:01 -05001008 /* Version 1.0 doesn't support switching */
1009 if (mmc->version == SD_VERSION_1_0)
1010 return 0;
1011
1012 timeout = 4;
1013 while (timeout--) {
1014 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
Anton staaff781dd32011-10-03 13:54:59 +00001015 (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -05001016
1017 if (err)
1018 return err;
1019
1020 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +03001021 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Fleming272cc702008-10-30 16:41:01 -05001022 break;
1023 }
1024
Andy Fleming272cc702008-10-30 16:41:01 -05001025 /* If high-speed isn't supported, we return */
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001026 if (__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)
1027 mmc->card_caps |= MMC_CAP(SD_HS);
Andy Fleming272cc702008-10-30 16:41:01 -05001028
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001029 return 0;
1030}
1031
1032static int sd_set_card_speed(struct mmc *mmc, enum bus_mode mode)
1033{
1034 int err;
1035
1036 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
Macpaul Lin2c3fbf42011-11-28 16:31:09 +00001037
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 if (err)
1040 return err;
1041
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001042 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) != 0x01000000)
1043 return -ENOTSUPP;
1044
1045 return 0;
1046}
1047
1048int sd_select_bus_width(struct mmc *mmc, int w)
1049{
1050 int err;
1051 struct mmc_cmd cmd;
1052
1053 if ((w != 4) && (w != 1))
1054 return -EINVAL;
1055
1056 cmd.cmdidx = MMC_CMD_APP_CMD;
1057 cmd.resp_type = MMC_RSP_R1;
1058 cmd.cmdarg = mmc->rca << 16;
1059
1060 err = mmc_send_cmd(mmc, &cmd, NULL);
1061 if (err)
1062 return err;
1063
1064 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1065 cmd.resp_type = MMC_RSP_R1;
1066 if (w == 4)
1067 cmd.cmdarg = 2;
1068 else if (w == 1)
1069 cmd.cmdarg = 0;
1070 err = mmc_send_cmd(mmc, &cmd, NULL);
1071 if (err)
1072 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001073
1074 return 0;
1075}
1076
Peng Fan3697e592016-09-01 11:13:38 +08001077static int sd_read_ssr(struct mmc *mmc)
1078{
1079 int err, i;
1080 struct mmc_cmd cmd;
1081 ALLOC_CACHE_ALIGN_BUFFER(uint, ssr, 16);
1082 struct mmc_data data;
1083 int timeout = 3;
1084 unsigned int au, eo, et, es;
1085
1086 cmd.cmdidx = MMC_CMD_APP_CMD;
1087 cmd.resp_type = MMC_RSP_R1;
1088 cmd.cmdarg = mmc->rca << 16;
1089
1090 err = mmc_send_cmd(mmc, &cmd, NULL);
1091 if (err)
1092 return err;
1093
1094 cmd.cmdidx = SD_CMD_APP_SD_STATUS;
1095 cmd.resp_type = MMC_RSP_R1;
1096 cmd.cmdarg = 0;
1097
1098retry_ssr:
1099 data.dest = (char *)ssr;
1100 data.blocksize = 64;
1101 data.blocks = 1;
1102 data.flags = MMC_DATA_READ;
1103
1104 err = mmc_send_cmd(mmc, &cmd, &data);
1105 if (err) {
1106 if (timeout--)
1107 goto retry_ssr;
1108
1109 return err;
1110 }
1111
1112 for (i = 0; i < 16; i++)
1113 ssr[i] = be32_to_cpu(ssr[i]);
1114
1115 au = (ssr[2] >> 12) & 0xF;
1116 if ((au <= 9) || (mmc->version == SD_VERSION_3)) {
1117 mmc->ssr.au = sd_au_size[au];
1118 es = (ssr[3] >> 24) & 0xFF;
1119 es |= (ssr[2] & 0xFF) << 8;
1120 et = (ssr[3] >> 18) & 0x3F;
1121 if (es && et) {
1122 eo = (ssr[3] >> 16) & 0x3;
1123 mmc->ssr.erase_timeout = (et * 1000) / es;
1124 mmc->ssr.erase_offset = eo * 1000;
1125 }
1126 } else {
1127 debug("Invalid Allocation Unit Size.\n");
1128 }
1129
1130 return 0;
1131}
1132
Andy Fleming272cc702008-10-30 16:41:01 -05001133/* frequency bases */
1134/* divided by 10 to be nice to platforms without floating point */
Mike Frysinger5f837c22010-10-20 01:15:53 +00001135static const int fbase[] = {
Andy Fleming272cc702008-10-30 16:41:01 -05001136 10000,
1137 100000,
1138 1000000,
1139 10000000,
1140};
1141
1142/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
1143 * to platforms without floating point.
1144 */
Simon Glass61fe0762016-05-14 14:02:57 -06001145static const u8 multipliers[] = {
Andy Fleming272cc702008-10-30 16:41:01 -05001146 0, /* reserved */
1147 10,
1148 12,
1149 13,
1150 15,
1151 20,
1152 25,
1153 30,
1154 35,
1155 40,
1156 45,
1157 50,
1158 55,
1159 60,
1160 70,
1161 80,
1162};
1163
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001164static inline int bus_width(uint cap)
1165{
1166 if (cap == MMC_MODE_8BIT)
1167 return 8;
1168 if (cap == MMC_MODE_4BIT)
1169 return 4;
1170 if (cap == MMC_MODE_1BIT)
1171 return 1;
1172 printf("invalid bus witdh capability 0x%x\n", cap);
1173 return 0;
1174}
1175
Simon Glasse7881d82017-07-29 11:35:31 -06001176#if !CONFIG_IS_ENABLED(DM_MMC)
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001177static void mmc_set_ios(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001178{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001179 if (mmc->cfg->ops->set_ios)
1180 mmc->cfg->ops->set_ios(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001181}
Simon Glass8ca51e52016-06-12 23:30:22 -06001182#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001183
1184void mmc_set_clock(struct mmc *mmc, uint clock)
1185{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001186 if (clock > mmc->cfg->f_max)
1187 clock = mmc->cfg->f_max;
Andy Fleming272cc702008-10-30 16:41:01 -05001188
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001189 if (clock < mmc->cfg->f_min)
1190 clock = mmc->cfg->f_min;
Andy Fleming272cc702008-10-30 16:41:01 -05001191
1192 mmc->clock = clock;
1193
1194 mmc_set_ios(mmc);
1195}
1196
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001197static void mmc_set_bus_width(struct mmc *mmc, uint width)
Andy Fleming272cc702008-10-30 16:41:01 -05001198{
1199 mmc->bus_width = width;
1200
1201 mmc_set_ios(mmc);
1202}
1203
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001204#if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG)
1205/*
1206 * helper function to display the capabilities in a human
1207 * friendly manner. The capabilities include bus width and
1208 * supported modes.
1209 */
1210void mmc_dump_capabilities(const char *text, uint caps)
1211{
1212 enum bus_mode mode;
1213
1214 printf("%s: widths [", text);
1215 if (caps & MMC_MODE_8BIT)
1216 printf("8, ");
1217 if (caps & MMC_MODE_4BIT)
1218 printf("4, ");
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001219 if (caps & MMC_MODE_1BIT)
1220 printf("1, ");
1221 printf("\b\b] modes [");
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001222 for (mode = MMC_LEGACY; mode < MMC_MODES_END; mode++)
1223 if (MMC_CAP(mode) & caps)
1224 printf("%s, ", mmc_mode_name(mode));
1225 printf("\b\b]\n");
1226}
1227#endif
1228
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001229struct mode_width_tuning {
1230 enum bus_mode mode;
1231 uint widths;
1232};
1233
1234static const struct mode_width_tuning sd_modes_by_pref[] = {
1235 {
1236 .mode = SD_HS,
1237 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1238 },
1239 {
1240 .mode = SD_LEGACY,
1241 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1242 }
1243};
1244
1245#define for_each_sd_mode_by_pref(caps, mwt) \
1246 for (mwt = sd_modes_by_pref;\
1247 mwt < sd_modes_by_pref + ARRAY_SIZE(sd_modes_by_pref);\
1248 mwt++) \
1249 if (caps & MMC_CAP(mwt->mode))
1250
1251static int sd_select_mode_and_width(struct mmc *mmc)
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001252{
1253 int err;
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001254 uint widths[] = {MMC_MODE_4BIT, MMC_MODE_1BIT};
1255 const struct mode_width_tuning *mwt;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001256
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001257 err = sd_get_capabilities(mmc);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001258 if (err)
1259 return err;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001260 /* Restrict card's capabilities by what the host can do */
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001261 mmc->card_caps &= (mmc->cfg->host_caps | MMC_MODE_1BIT);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001262
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001263 for_each_sd_mode_by_pref(mmc->card_caps, mwt) {
1264 uint *w;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001265
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001266 for (w = widths; w < widths + ARRAY_SIZE(widths); w++) {
1267 if (*w & mmc->card_caps & mwt->widths) {
1268 debug("trying mode %s width %d (at %d MHz)\n",
1269 mmc_mode_name(mwt->mode),
1270 bus_width(*w),
1271 mmc_mode2freq(mmc, mwt->mode) / 1000000);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001272
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001273 /* configure the bus width (card + host) */
1274 err = sd_select_bus_width(mmc, bus_width(*w));
1275 if (err)
1276 goto error;
1277 mmc_set_bus_width(mmc, bus_width(*w));
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001278
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001279 /* configure the bus mode (card) */
1280 err = sd_set_card_speed(mmc, mwt->mode);
1281 if (err)
1282 goto error;
1283
1284 /* configure the bus mode (host) */
1285 mmc_select_mode(mmc, mwt->mode);
1286 mmc_set_clock(mmc, mmc->tran_speed);
1287
1288 err = sd_read_ssr(mmc);
1289 if (!err)
1290 return 0;
1291
1292 printf("bad ssr\n");
1293
1294error:
1295 /* revert to a safer bus speed */
1296 mmc_select_mode(mmc, SD_LEGACY);
1297 mmc_set_clock(mmc, mmc->tran_speed);
1298 }
1299 }
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001300 }
1301
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001302 printf("unable to select a mode\n");
1303 return -ENOTSUPP;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001304}
1305
Jean-Jacques Hiblot7382e692017-09-21 16:29:52 +02001306/*
1307 * read the compare the part of ext csd that is constant.
1308 * This can be used to check that the transfer is working
1309 * as expected.
1310 */
1311static int mmc_read_and_compare_ext_csd(struct mmc *mmc)
1312{
1313 int err;
1314 const u8 *ext_csd = mmc->ext_csd;
1315 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
1316
1317 err = mmc_send_ext_csd(mmc, test_csd);
1318 if (err)
1319 return err;
1320
1321 /* Only compare read only fields */
1322 if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT]
1323 == test_csd[EXT_CSD_PARTITIONING_SUPPORT] &&
1324 ext_csd[EXT_CSD_HC_WP_GRP_SIZE]
1325 == test_csd[EXT_CSD_HC_WP_GRP_SIZE] &&
1326 ext_csd[EXT_CSD_REV]
1327 == test_csd[EXT_CSD_REV] &&
1328 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1329 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] &&
1330 memcmp(&ext_csd[EXT_CSD_SEC_CNT],
1331 &test_csd[EXT_CSD_SEC_CNT], 4) == 0)
1332 return 0;
1333
1334 return -EBADMSG;
1335}
1336
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001337static int mmc_select_bus_freq_width(struct mmc *mmc)
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001338{
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001339 /* An array of possible bus widths in order of preference */
1340 static const unsigned int ext_csd_bits[] = {
1341 EXT_CSD_DDR_BUS_WIDTH_8,
1342 EXT_CSD_DDR_BUS_WIDTH_4,
1343 EXT_CSD_BUS_WIDTH_8,
1344 EXT_CSD_BUS_WIDTH_4,
1345 EXT_CSD_BUS_WIDTH_1,
1346 };
1347 /* An array to map CSD bus widths to host cap bits */
1348 static const unsigned int ext_to_hostcaps[] = {
1349 [EXT_CSD_DDR_BUS_WIDTH_4] =
1350 MMC_MODE_DDR_52MHz | MMC_MODE_4BIT,
1351 [EXT_CSD_DDR_BUS_WIDTH_8] =
1352 MMC_MODE_DDR_52MHz | MMC_MODE_8BIT,
1353 [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
1354 [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
1355 };
1356 /* An array to map chosen bus width to an integer */
1357 static const unsigned int widths[] = {
1358 8, 4, 8, 4, 1,
1359 };
1360 int err;
1361 int idx;
1362
1363 err = mmc_change_freq(mmc);
1364 if (err)
1365 return err;
1366
1367 /* Restrict card's capabilities by what the host can do */
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001368 mmc->card_caps &= (mmc->cfg->host_caps | MMC_MODE_1BIT);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001369
1370 /* Only version 4 of MMC supports wider bus widths */
1371 if (mmc->version < MMC_VERSION_4)
1372 return 0;
1373
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001374 if (!mmc->ext_csd) {
1375 debug("No ext_csd found!\n"); /* this should enver happen */
1376 return -ENOTSUPP;
1377 }
1378
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001379 for (idx = 0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
1380 unsigned int extw = ext_csd_bits[idx];
1381 unsigned int caps = ext_to_hostcaps[extw];
1382 /*
1383 * If the bus width is still not changed,
1384 * don't try to set the default again.
1385 * Otherwise, recover from switch attempts
1386 * by switching to 1-bit bus width.
1387 */
1388 if (extw == EXT_CSD_BUS_WIDTH_1 &&
1389 mmc->bus_width == 1) {
1390 err = 0;
1391 break;
1392 }
1393
1394 /*
1395 * Check to make sure the card and controller support
1396 * these capabilities
1397 */
1398 if ((mmc->card_caps & caps) != caps)
1399 continue;
1400
1401 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1402 EXT_CSD_BUS_WIDTH, extw);
1403
1404 if (err)
1405 continue;
1406
1407 mmc->ddr_mode = (caps & MMC_MODE_DDR_52MHz) ? 1 : 0;
1408 mmc_set_bus_width(mmc, widths[idx]);
1409
Jean-Jacques Hiblot7382e692017-09-21 16:29:52 +02001410 err = mmc_read_and_compare_ext_csd(mmc);
1411 if (!err)
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001412 break;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001413 }
1414
1415 if (err)
1416 return err;
1417
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02001418 if (mmc->card_caps & MMC_MODE_HS_52MHz) {
1419 if (mmc->ddr_mode)
1420 mmc_select_mode(mmc, MMC_DDR_52);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001421 else
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02001422 mmc_select_mode(mmc, MMC_HS_52);
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +02001423 } else if (mmc->card_caps & MMC_MODE_HS)
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02001424 mmc_select_mode(mmc, MMC_HS);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001425
1426 return err;
1427}
1428
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001429static int mmc_startup_v4(struct mmc *mmc)
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02001430{
1431 int err, i;
1432 u64 capacity;
1433 bool has_parts = false;
1434 bool part_completed;
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001435 u8 *ext_csd;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02001436
1437 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4))
1438 return 0;
1439
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001440 ext_csd = malloc_cache_aligned(MMC_MAX_BLOCK_LEN);
1441 if (!ext_csd)
1442 return -ENOMEM;
1443
1444 mmc->ext_csd = ext_csd;
1445
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02001446 /* check ext_csd version and capacity */
1447 err = mmc_send_ext_csd(mmc, ext_csd);
1448 if (err)
1449 return err;
1450 if (ext_csd[EXT_CSD_REV] >= 2) {
1451 /*
1452 * According to the JEDEC Standard, the value of
1453 * ext_csd's capacity is valid if the value is more
1454 * than 2GB
1455 */
1456 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
1457 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
1458 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
1459 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
1460 capacity *= MMC_MAX_BLOCK_LEN;
1461 if ((capacity >> 20) > 2 * 1024)
1462 mmc->capacity_user = capacity;
1463 }
1464
1465 switch (ext_csd[EXT_CSD_REV]) {
1466 case 1:
1467 mmc->version = MMC_VERSION_4_1;
1468 break;
1469 case 2:
1470 mmc->version = MMC_VERSION_4_2;
1471 break;
1472 case 3:
1473 mmc->version = MMC_VERSION_4_3;
1474 break;
1475 case 5:
1476 mmc->version = MMC_VERSION_4_41;
1477 break;
1478 case 6:
1479 mmc->version = MMC_VERSION_4_5;
1480 break;
1481 case 7:
1482 mmc->version = MMC_VERSION_5_0;
1483 break;
1484 case 8:
1485 mmc->version = MMC_VERSION_5_1;
1486 break;
1487 }
1488
1489 /* The partition data may be non-zero but it is only
1490 * effective if PARTITION_SETTING_COMPLETED is set in
1491 * EXT_CSD, so ignore any data if this bit is not set,
1492 * except for enabling the high-capacity group size
1493 * definition (see below).
1494 */
1495 part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] &
1496 EXT_CSD_PARTITION_SETTING_COMPLETED);
1497
1498 /* store the partition info of emmc */
1499 mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT];
1500 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
1501 ext_csd[EXT_CSD_BOOT_MULT])
1502 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
1503 if (part_completed &&
1504 (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT))
1505 mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE];
1506
1507 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
1508
1509 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
1510
1511 for (i = 0; i < 4; i++) {
1512 int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
1513 uint mult = (ext_csd[idx + 2] << 16) +
1514 (ext_csd[idx + 1] << 8) + ext_csd[idx];
1515 if (mult)
1516 has_parts = true;
1517 if (!part_completed)
1518 continue;
1519 mmc->capacity_gp[i] = mult;
1520 mmc->capacity_gp[i] *=
1521 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1522 mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1523 mmc->capacity_gp[i] <<= 19;
1524 }
1525
1526 if (part_completed) {
1527 mmc->enh_user_size =
1528 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16) +
1529 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) +
1530 ext_csd[EXT_CSD_ENH_SIZE_MULT];
1531 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1532 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1533 mmc->enh_user_size <<= 19;
1534 mmc->enh_user_start =
1535 (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24) +
1536 (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) +
1537 (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) +
1538 ext_csd[EXT_CSD_ENH_START_ADDR];
1539 if (mmc->high_capacity)
1540 mmc->enh_user_start <<= 9;
1541 }
1542
1543 /*
1544 * Host needs to enable ERASE_GRP_DEF bit if device is
1545 * partitioned. This bit will be lost every time after a reset
1546 * or power off. This will affect erase size.
1547 */
1548 if (part_completed)
1549 has_parts = true;
1550 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
1551 (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB))
1552 has_parts = true;
1553 if (has_parts) {
1554 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1555 EXT_CSD_ERASE_GROUP_DEF, 1);
1556
1557 if (err)
1558 return err;
1559
1560 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
1561 }
1562
1563 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) {
1564 /* Read out group size from ext_csd */
1565 mmc->erase_grp_size =
1566 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
1567 /*
1568 * if high capacity and partition setting completed
1569 * SEC_COUNT is valid even if it is smaller than 2 GiB
1570 * JEDEC Standard JESD84-B45, 6.2.4
1571 */
1572 if (mmc->high_capacity && part_completed) {
1573 capacity = (ext_csd[EXT_CSD_SEC_CNT]) |
1574 (ext_csd[EXT_CSD_SEC_CNT + 1] << 8) |
1575 (ext_csd[EXT_CSD_SEC_CNT + 2] << 16) |
1576 (ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1577 capacity *= MMC_MAX_BLOCK_LEN;
1578 mmc->capacity_user = capacity;
1579 }
1580 } else {
1581 /* Calculate the group size from the csd value. */
1582 int erase_gsz, erase_gmul;
1583
1584 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1585 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1586 mmc->erase_grp_size = (erase_gsz + 1)
1587 * (erase_gmul + 1);
1588 }
1589
1590 mmc->hc_wp_grp_size = 1024
1591 * ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1592 * ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1593
1594 mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
1595
1596 return 0;
1597}
1598
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001599static int mmc_startup(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001600{
Stephen Warrenf866a462013-06-11 15:14:01 -06001601 int err, i;
Andy Fleming272cc702008-10-30 16:41:01 -05001602 uint mult, freq;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02001603 u64 cmult, csize;
Andy Fleming272cc702008-10-30 16:41:01 -05001604 struct mmc_cmd cmd;
Simon Glassc40fdca2016-05-01 13:52:35 -06001605 struct blk_desc *bdesc;
Andy Fleming272cc702008-10-30 16:41:01 -05001606
Thomas Choud52ebf12010-12-24 13:12:21 +00001607#ifdef CONFIG_MMC_SPI_CRC_ON
1608 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
1609 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
1610 cmd.resp_type = MMC_RSP_R1;
1611 cmd.cmdarg = 1;
Thomas Choud52ebf12010-12-24 13:12:21 +00001612 err = mmc_send_cmd(mmc, &cmd, NULL);
1613
1614 if (err)
1615 return err;
1616 }
1617#endif
1618
Andy Fleming272cc702008-10-30 16:41:01 -05001619 /* Put the Card in Identify Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00001620 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
1621 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
Andy Fleming272cc702008-10-30 16:41:01 -05001622 cmd.resp_type = MMC_RSP_R2;
1623 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05001624
1625 err = mmc_send_cmd(mmc, &cmd, NULL);
1626
1627 if (err)
1628 return err;
1629
1630 memcpy(mmc->cid, cmd.response, 16);
1631
1632 /*
1633 * For MMC cards, set the Relative Address.
1634 * For SD cards, get the Relatvie Address.
1635 * This also puts the cards into Standby State
1636 */
Thomas Choud52ebf12010-12-24 13:12:21 +00001637 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1638 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
1639 cmd.cmdarg = mmc->rca << 16;
1640 cmd.resp_type = MMC_RSP_R6;
Andy Fleming272cc702008-10-30 16:41:01 -05001641
Thomas Choud52ebf12010-12-24 13:12:21 +00001642 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05001643
Thomas Choud52ebf12010-12-24 13:12:21 +00001644 if (err)
1645 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001646
Thomas Choud52ebf12010-12-24 13:12:21 +00001647 if (IS_SD(mmc))
1648 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
1649 }
Andy Fleming272cc702008-10-30 16:41:01 -05001650
1651 /* Get the Card-Specific Data */
1652 cmd.cmdidx = MMC_CMD_SEND_CSD;
1653 cmd.resp_type = MMC_RSP_R2;
1654 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05001655
1656 err = mmc_send_cmd(mmc, &cmd, NULL);
1657
1658 if (err)
1659 return err;
1660
Rabin Vincent998be3d2009-04-05 13:30:56 +05301661 mmc->csd[0] = cmd.response[0];
1662 mmc->csd[1] = cmd.response[1];
1663 mmc->csd[2] = cmd.response[2];
1664 mmc->csd[3] = cmd.response[3];
Andy Fleming272cc702008-10-30 16:41:01 -05001665
1666 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincent0b453ff2009-04-05 13:30:55 +05301667 int version = (cmd.response[0] >> 26) & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -05001668
1669 switch (version) {
Bin Meng53e8e402016-03-17 21:53:13 -07001670 case 0:
1671 mmc->version = MMC_VERSION_1_2;
1672 break;
1673 case 1:
1674 mmc->version = MMC_VERSION_1_4;
1675 break;
1676 case 2:
1677 mmc->version = MMC_VERSION_2_2;
1678 break;
1679 case 3:
1680 mmc->version = MMC_VERSION_3;
1681 break;
1682 case 4:
1683 mmc->version = MMC_VERSION_4;
1684 break;
1685 default:
1686 mmc->version = MMC_VERSION_1_2;
1687 break;
Andy Fleming272cc702008-10-30 16:41:01 -05001688 }
1689 }
1690
1691 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincent0b453ff2009-04-05 13:30:55 +05301692 freq = fbase[(cmd.response[0] & 0x7)];
1693 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Fleming272cc702008-10-30 16:41:01 -05001694
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02001695 mmc->legacy_speed = freq * mult;
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02001696 mmc_select_mode(mmc, MMC_LEGACY);
Andy Fleming272cc702008-10-30 16:41:01 -05001697
Markus Niebelab711882013-12-16 13:40:46 +01001698 mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
Rabin Vincent998be3d2009-04-05 13:30:56 +05301699 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -05001700
1701 if (IS_SD(mmc))
1702 mmc->write_bl_len = mmc->read_bl_len;
1703 else
Rabin Vincent998be3d2009-04-05 13:30:56 +05301704 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -05001705
1706 if (mmc->high_capacity) {
1707 csize = (mmc->csd[1] & 0x3f) << 16
1708 | (mmc->csd[2] & 0xffff0000) >> 16;
1709 cmult = 8;
1710 } else {
1711 csize = (mmc->csd[1] & 0x3ff) << 2
1712 | (mmc->csd[2] & 0xc0000000) >> 30;
1713 cmult = (mmc->csd[2] & 0x00038000) >> 15;
1714 }
1715
Stephen Warrenf866a462013-06-11 15:14:01 -06001716 mmc->capacity_user = (csize + 1) << (cmult + 2);
1717 mmc->capacity_user *= mmc->read_bl_len;
1718 mmc->capacity_boot = 0;
1719 mmc->capacity_rpmb = 0;
1720 for (i = 0; i < 4; i++)
1721 mmc->capacity_gp[i] = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05001722
Simon Glass8bfa1952013-04-03 08:54:30 +00001723 if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
1724 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -05001725
Simon Glass8bfa1952013-04-03 08:54:30 +00001726 if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
1727 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -05001728
Markus Niebelab711882013-12-16 13:40:46 +01001729 if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
1730 cmd.cmdidx = MMC_CMD_SET_DSR;
1731 cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
1732 cmd.resp_type = MMC_RSP_NONE;
1733 if (mmc_send_cmd(mmc, &cmd, NULL))
1734 printf("MMC: SET_DSR failed\n");
1735 }
1736
Andy Fleming272cc702008-10-30 16:41:01 -05001737 /* Select the card, and put it into Transfer Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00001738 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1739 cmd.cmdidx = MMC_CMD_SELECT_CARD;
Ajay Bhargavfe8f7062011-10-05 03:13:23 +00001740 cmd.resp_type = MMC_RSP_R1;
Thomas Choud52ebf12010-12-24 13:12:21 +00001741 cmd.cmdarg = mmc->rca << 16;
Thomas Choud52ebf12010-12-24 13:12:21 +00001742 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05001743
Thomas Choud52ebf12010-12-24 13:12:21 +00001744 if (err)
1745 return err;
1746 }
Andy Fleming272cc702008-10-30 16:41:01 -05001747
Lei Wene6f99a52011-06-22 17:03:31 +00001748 /*
1749 * For SD, its erase group is always one sector
1750 */
1751 mmc->erase_grp_size = 1;
Lei Wenbc897b12011-05-02 16:26:26 +00001752 mmc->part_config = MMCPART_NOAVAILABLE;
Lei Wenbc897b12011-05-02 16:26:26 +00001753
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001754 err = mmc_startup_v4(mmc);
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02001755 if (err)
1756 return err;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05301757
Simon Glassc40fdca2016-05-01 13:52:35 -06001758 err = mmc_set_capacity(mmc, mmc_get_blk_desc(mmc)->hwpart);
Stephen Warrenf866a462013-06-11 15:14:01 -06001759 if (err)
1760 return err;
1761
Andy Fleming272cc702008-10-30 16:41:01 -05001762 if (IS_SD(mmc))
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001763 err = sd_select_mode_and_width(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001764 else
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001765 err = mmc_select_bus_freq_width(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001766
1767 if (err)
1768 return err;
1769
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001770
Andrew Gabbasov5af8f452014-12-01 06:59:11 -06001771 /* Fix the block length for DDR mode */
1772 if (mmc->ddr_mode) {
1773 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
1774 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
1775 }
1776
Andy Fleming272cc702008-10-30 16:41:01 -05001777 /* fill in device description */
Simon Glassc40fdca2016-05-01 13:52:35 -06001778 bdesc = mmc_get_blk_desc(mmc);
1779 bdesc->lun = 0;
1780 bdesc->hwpart = 0;
1781 bdesc->type = 0;
1782 bdesc->blksz = mmc->read_bl_len;
1783 bdesc->log2blksz = LOG2(bdesc->blksz);
1784 bdesc->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Sjoerd Simonsfc011f62015-12-04 23:27:40 +01001785#if !defined(CONFIG_SPL_BUILD) || \
1786 (defined(CONFIG_SPL_LIBCOMMON_SUPPORT) && \
1787 !defined(CONFIG_USE_TINY_PRINTF))
Simon Glassc40fdca2016-05-01 13:52:35 -06001788 sprintf(bdesc->vendor, "Man %06x Snr %04x%04x",
Taylor Huttbabce5f2012-10-20 17:15:59 +00001789 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1790 (mmc->cid[3] >> 16) & 0xffff);
Simon Glassc40fdca2016-05-01 13:52:35 -06001791 sprintf(bdesc->product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
Taylor Huttbabce5f2012-10-20 17:15:59 +00001792 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1793 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1794 (mmc->cid[2] >> 24) & 0xff);
Simon Glassc40fdca2016-05-01 13:52:35 -06001795 sprintf(bdesc->revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
Taylor Huttbabce5f2012-10-20 17:15:59 +00001796 (mmc->cid[2] >> 16) & 0xf);
Paul Burton56196822013-09-04 16:12:25 +01001797#else
Simon Glassc40fdca2016-05-01 13:52:35 -06001798 bdesc->vendor[0] = 0;
1799 bdesc->product[0] = 0;
1800 bdesc->revision[0] = 0;
Paul Burton56196822013-09-04 16:12:25 +01001801#endif
Mikhail Kshevetskiy122efd42012-07-09 08:53:38 +00001802#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
Simon Glassc40fdca2016-05-01 13:52:35 -06001803 part_init(bdesc);
Mikhail Kshevetskiy122efd42012-07-09 08:53:38 +00001804#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001805
1806 return 0;
1807}
1808
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001809static int mmc_send_if_cond(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001810{
1811 struct mmc_cmd cmd;
1812 int err;
1813
1814 cmd.cmdidx = SD_CMD_SEND_IF_COND;
1815 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001816 cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
Andy Fleming272cc702008-10-30 16:41:01 -05001817 cmd.resp_type = MMC_RSP_R7;
Andy Fleming272cc702008-10-30 16:41:01 -05001818
1819 err = mmc_send_cmd(mmc, &cmd, NULL);
1820
1821 if (err)
1822 return err;
1823
Rabin Vincent998be3d2009-04-05 13:30:56 +05301824 if ((cmd.response[0] & 0xff) != 0xaa)
Jaehoon Chung915ffa52016-07-19 16:33:36 +09001825 return -EOPNOTSUPP;
Andy Fleming272cc702008-10-30 16:41:01 -05001826 else
1827 mmc->version = SD_VERSION_2;
1828
1829 return 0;
1830}
1831
Simon Glassc4d660d2017-07-04 13:31:19 -06001832#if !CONFIG_IS_ENABLED(DM_MMC)
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01001833/* board-specific MMC power initializations. */
1834__weak void board_mmc_power_init(void)
1835{
1836}
Simon Glass05cbeb72017-04-22 19:10:56 -06001837#endif
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01001838
Peng Fan2051aef2016-10-11 15:08:43 +08001839static int mmc_power_init(struct mmc *mmc)
1840{
Simon Glassc4d660d2017-07-04 13:31:19 -06001841#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02001842#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan2051aef2016-10-11 15:08:43 +08001843 int ret;
1844
1845 ret = device_get_supply_regulator(mmc->dev, "vmmc-supply",
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02001846 &mmc->vmmc_supply);
1847 if (ret)
Jaehoon Chung288db7c2016-10-24 15:22:22 +09001848 debug("%s: No vmmc supply\n", mmc->dev->name);
Peng Fan2051aef2016-10-11 15:08:43 +08001849
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02001850 ret = device_get_supply_regulator(mmc->dev, "vqmmc-supply",
1851 &mmc->vqmmc_supply);
1852 if (ret)
1853 debug("%s: No vqmmc supply\n", mmc->dev->name);
1854
1855 if (mmc->vmmc_supply) {
1856 ret = regulator_set_enable(mmc->vmmc_supply, true);
1857 if (ret) {
1858 puts("Error enabling VMMC supply\n");
1859 return ret;
1860 }
Peng Fan2051aef2016-10-11 15:08:43 +08001861 }
1862#endif
Simon Glass05cbeb72017-04-22 19:10:56 -06001863#else /* !CONFIG_DM_MMC */
1864 /*
1865 * Driver model should use a regulator, as above, rather than calling
1866 * out to board code.
1867 */
1868 board_mmc_power_init();
1869#endif
Peng Fan2051aef2016-10-11 15:08:43 +08001870 return 0;
1871}
1872
Che-Liang Chioue9550442012-11-28 15:21:13 +00001873int mmc_start_init(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001874{
Simon Glass8ca51e52016-06-12 23:30:22 -06001875 bool no_card;
Macpaul Linafd59322011-11-14 23:35:39 +00001876 int err;
Andy Fleming272cc702008-10-30 16:41:01 -05001877
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001878 /* we pretend there's no card when init is NULL */
Simon Glass8ca51e52016-06-12 23:30:22 -06001879 no_card = mmc_getcd(mmc) == 0;
Simon Glasse7881d82017-07-29 11:35:31 -06001880#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glass8ca51e52016-06-12 23:30:22 -06001881 no_card = no_card || (mmc->cfg->ops->init == NULL);
1882#endif
1883 if (no_card) {
Thierry Reding48972d92012-01-02 01:15:37 +00001884 mmc->has_init = 0;
Paul Burton56196822013-09-04 16:12:25 +01001885#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Thierry Reding48972d92012-01-02 01:15:37 +00001886 printf("MMC: no card present\n");
Paul Burton56196822013-09-04 16:12:25 +01001887#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +09001888 return -ENOMEDIUM;
Thierry Reding48972d92012-01-02 01:15:37 +00001889 }
1890
Lei Wenbc897b12011-05-02 16:26:26 +00001891 if (mmc->has_init)
1892 return 0;
1893
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +08001894#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
1895 mmc_adapter_card_type_ident();
1896#endif
Peng Fan2051aef2016-10-11 15:08:43 +08001897 err = mmc_power_init(mmc);
1898 if (err)
1899 return err;
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01001900
Simon Glasse7881d82017-07-29 11:35:31 -06001901#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass8ca51e52016-06-12 23:30:22 -06001902 /* The device has already been probed ready for use */
1903#else
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001904 /* made sure it's not NULL earlier */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001905 err = mmc->cfg->ops->init(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001906 if (err)
1907 return err;
Simon Glass8ca51e52016-06-12 23:30:22 -06001908#endif
Andrew Gabbasov786e8f82014-12-01 06:59:09 -06001909 mmc->ddr_mode = 0;
Ilya Yanokb86b85e2009-06-29 17:53:16 +04001910 mmc_set_bus_width(mmc, 1);
1911 mmc_set_clock(mmc, 1);
1912
Andy Fleming272cc702008-10-30 16:41:01 -05001913 /* Reset the Card */
1914 err = mmc_go_idle(mmc);
1915
1916 if (err)
1917 return err;
1918
Lei Wenbc897b12011-05-02 16:26:26 +00001919 /* The internal partition reset to user partition(0) at every CMD0*/
Simon Glassc40fdca2016-05-01 13:52:35 -06001920 mmc_get_blk_desc(mmc)->hwpart = 0;
Lei Wenbc897b12011-05-02 16:26:26 +00001921
Andy Fleming272cc702008-10-30 16:41:01 -05001922 /* Test for SD version 2 */
Macpaul Linafd59322011-11-14 23:35:39 +00001923 err = mmc_send_if_cond(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001924
Andy Fleming272cc702008-10-30 16:41:01 -05001925 /* Now try to get the SD card's operating condition */
1926 err = sd_send_op_cond(mmc);
1927
1928 /* If the command timed out, we check for an MMC card */
Jaehoon Chung915ffa52016-07-19 16:33:36 +09001929 if (err == -ETIMEDOUT) {
Andy Fleming272cc702008-10-30 16:41:01 -05001930 err = mmc_send_op_cond(mmc);
1931
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05001932 if (err) {
Paul Burton56196822013-09-04 16:12:25 +01001933#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Andy Fleming272cc702008-10-30 16:41:01 -05001934 printf("Card did not respond to voltage select!\n");
Paul Burton56196822013-09-04 16:12:25 +01001935#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +09001936 return -EOPNOTSUPP;
Andy Fleming272cc702008-10-30 16:41:01 -05001937 }
1938 }
1939
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05001940 if (!err)
Che-Liang Chioue9550442012-11-28 15:21:13 +00001941 mmc->init_in_progress = 1;
1942
1943 return err;
1944}
1945
1946static int mmc_complete_init(struct mmc *mmc)
1947{
1948 int err = 0;
1949
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05001950 mmc->init_in_progress = 0;
Che-Liang Chioue9550442012-11-28 15:21:13 +00001951 if (mmc->op_cond_pending)
1952 err = mmc_complete_op_cond(mmc);
1953
1954 if (!err)
1955 err = mmc_startup(mmc);
Lei Wenbc897b12011-05-02 16:26:26 +00001956 if (err)
1957 mmc->has_init = 0;
1958 else
1959 mmc->has_init = 1;
Che-Liang Chioue9550442012-11-28 15:21:13 +00001960 return err;
1961}
1962
1963int mmc_init(struct mmc *mmc)
1964{
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05001965 int err = 0;
Marek Vasutce9eca92016-12-01 02:06:32 +01001966 __maybe_unused unsigned start;
Simon Glassc4d660d2017-07-04 13:31:19 -06001967#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass33fb2112016-05-01 13:52:41 -06001968 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc->dev);
Che-Liang Chioue9550442012-11-28 15:21:13 +00001969
Simon Glass33fb2112016-05-01 13:52:41 -06001970 upriv->mmc = mmc;
1971#endif
Che-Liang Chioue9550442012-11-28 15:21:13 +00001972 if (mmc->has_init)
1973 return 0;
Mateusz Zalegad803fea2014-04-29 20:15:30 +02001974
1975 start = get_timer(0);
1976
Che-Liang Chioue9550442012-11-28 15:21:13 +00001977 if (!mmc->init_in_progress)
1978 err = mmc_start_init(mmc);
1979
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05001980 if (!err)
Che-Liang Chioue9550442012-11-28 15:21:13 +00001981 err = mmc_complete_init(mmc);
Jagan Teki919b4852017-01-10 11:18:43 +01001982 if (err)
1983 printf("%s: %d, time %lu\n", __func__, err, get_timer(start));
1984
Lei Wenbc897b12011-05-02 16:26:26 +00001985 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001986}
1987
Markus Niebelab711882013-12-16 13:40:46 +01001988int mmc_set_dsr(struct mmc *mmc, u16 val)
1989{
1990 mmc->dsr = val;
1991 return 0;
1992}
1993
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02001994/* CPU-specific MMC initializations */
1995__weak int cpu_mmc_init(bd_t *bis)
Andy Fleming272cc702008-10-30 16:41:01 -05001996{
1997 return -1;
1998}
1999
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02002000/* board-specific MMC initializations. */
2001__weak int board_mmc_init(bd_t *bis)
2002{
2003 return -1;
2004}
Andy Fleming272cc702008-10-30 16:41:01 -05002005
Che-Liang Chioue9550442012-11-28 15:21:13 +00002006void mmc_set_preinit(struct mmc *mmc, int preinit)
2007{
2008 mmc->preinit = preinit;
2009}
2010
Simon Glassc4d660d2017-07-04 13:31:19 -06002011#if CONFIG_IS_ENABLED(DM_MMC) && defined(CONFIG_SPL_BUILD)
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002012static int mmc_probe(bd_t *bis)
2013{
2014 return 0;
2015}
Simon Glassc4d660d2017-07-04 13:31:19 -06002016#elif CONFIG_IS_ENABLED(DM_MMC)
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002017static int mmc_probe(bd_t *bis)
2018{
Simon Glass4a1db6d2015-12-29 05:22:49 -07002019 int ret, i;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002020 struct uclass *uc;
Simon Glass4a1db6d2015-12-29 05:22:49 -07002021 struct udevice *dev;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002022
2023 ret = uclass_get(UCLASS_MMC, &uc);
2024 if (ret)
2025 return ret;
2026
Simon Glass4a1db6d2015-12-29 05:22:49 -07002027 /*
2028 * Try to add them in sequence order. Really with driver model we
2029 * should allow holes, but the current MMC list does not allow that.
2030 * So if we request 0, 1, 3 we will get 0, 1, 2.
2031 */
2032 for (i = 0; ; i++) {
2033 ret = uclass_get_device_by_seq(UCLASS_MMC, i, &dev);
2034 if (ret == -ENODEV)
2035 break;
2036 }
2037 uclass_foreach_dev(dev, uc) {
2038 ret = device_probe(dev);
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002039 if (ret)
Simon Glass4a1db6d2015-12-29 05:22:49 -07002040 printf("%s - probe failed: %d\n", dev->name, ret);
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002041 }
2042
2043 return 0;
2044}
2045#else
2046static int mmc_probe(bd_t *bis)
2047{
2048 if (board_mmc_init(bis) < 0)
2049 cpu_mmc_init(bis);
2050
2051 return 0;
2052}
2053#endif
Che-Liang Chioue9550442012-11-28 15:21:13 +00002054
Andy Fleming272cc702008-10-30 16:41:01 -05002055int mmc_initialize(bd_t *bis)
2056{
Daniel Kochmański1b26bab2015-05-29 16:55:43 +02002057 static int initialized = 0;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002058 int ret;
Daniel Kochmański1b26bab2015-05-29 16:55:43 +02002059 if (initialized) /* Avoid initializing mmc multiple times */
2060 return 0;
2061 initialized = 1;
2062
Simon Glassc4d660d2017-07-04 13:31:19 -06002063#if !CONFIG_IS_ENABLED(BLK)
Marek Vasutb5b838f2016-12-01 02:06:33 +01002064#if !CONFIG_IS_ENABLED(MMC_TINY)
Simon Glassc40fdca2016-05-01 13:52:35 -06002065 mmc_list_init();
2066#endif
Marek Vasutb5b838f2016-12-01 02:06:33 +01002067#endif
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002068 ret = mmc_probe(bis);
2069 if (ret)
2070 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -05002071
Ying Zhangbb0dc102013-08-16 15:16:11 +08002072#ifndef CONFIG_SPL_BUILD
Andy Fleming272cc702008-10-30 16:41:01 -05002073 print_mmc_devices(',');
Ying Zhangbb0dc102013-08-16 15:16:11 +08002074#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002075
Simon Glassc40fdca2016-05-01 13:52:35 -06002076 mmc_do_preinit();
Andy Fleming272cc702008-10-30 16:41:01 -05002077 return 0;
2078}
Tomas Melincd3d4882016-11-25 11:01:03 +02002079
2080#ifdef CONFIG_CMD_BKOPS_ENABLE
2081int mmc_set_bkops_enable(struct mmc *mmc)
2082{
2083 int err;
2084 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
2085
2086 err = mmc_send_ext_csd(mmc, ext_csd);
2087 if (err) {
2088 puts("Could not get ext_csd register values\n");
2089 return err;
2090 }
2091
2092 if (!(ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1)) {
2093 puts("Background operations not supported on device\n");
2094 return -EMEDIUMTYPE;
2095 }
2096
2097 if (ext_csd[EXT_CSD_BKOPS_EN] & 0x1) {
2098 puts("Background operations already enabled\n");
2099 return 0;
2100 }
2101
2102 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BKOPS_EN, 1);
2103 if (err) {
2104 puts("Failed to enable manual background operations\n");
2105 return err;
2106 }
2107
2108 puts("Enabled manual background operations\n");
2109
2110 return 0;
2111}
2112#endif