mmc.c 52 KB
Newer Older
Andy Fleming's avatar
Andy Fleming committed
1 2 3 4 5 6
/*
 * Copyright 2008, Freescale Semiconductor, Inc
 * Andy Fleming
 *
 * Based vaguely on the Linux code
 *
7
 * SPDX-License-Identifier:	GPL-2.0+
Andy Fleming's avatar
Andy Fleming committed
8 9 10 11 12
 */

#include <config.h>
#include <common.h>
#include <command.h>
13 14
#include <dm.h>
#include <dm/device-internal.h>
15
#include <errno.h>
Andy Fleming's avatar
Andy Fleming committed
16 17
#include <mmc.h>
#include <part.h>
Peng Fan's avatar
Peng Fan committed
18
#include <power/regulator.h>
Andy Fleming's avatar
Andy Fleming committed
19
#include <malloc.h>
20
#include <memalign.h>
Andy Fleming's avatar
Andy Fleming committed
21
#include <linux/list.h>
22
#include <div64.h>
23
#include "mmc_private.h"
Andy Fleming's avatar
Andy Fleming committed
24

25 26 27 28 29 30 31
static const unsigned int sd_au_size[] = {
	0,		SZ_16K / 512,		SZ_32K / 512,
	SZ_64K / 512,	SZ_128K / 512,		SZ_256K / 512,
	SZ_512K / 512,	SZ_1M / 512,		SZ_2M / 512,
	SZ_4M / 512,	SZ_8M / 512,		(SZ_8M + SZ_4M) / 512,
	SZ_16M / 512,	(SZ_16M + SZ_8M) / 512,	SZ_32M / 512,	SZ_64M / 512,
};
32
static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage);
33
static void mmc_power_cycle(struct mmc *mmc);
34
static int mmc_card_busy(struct mmc *mmc);
35
static int mmc_select_mode_and_width(struct mmc *mmc, uint card_caps);
36

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#if CONFIG_IS_ENABLED(MMC_TINY)
static struct mmc mmc_static;
struct mmc *find_mmc_device(int dev_num)
{
	return &mmc_static;
}

void mmc_do_preinit(void)
{
	struct mmc *m = &mmc_static;
#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
	mmc_set_preinit(m, 1);
#endif
	if (m->preinit)
		mmc_start_init(m);
}

struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
{
	return &mmc->block_dev;
}
#endif

60
#ifndef CONFIG_DM_MMC_OPS
61
__weak int board_mmc_getwp(struct mmc *mmc)
62 63 64 65 66 67 68 69 70 71
{
	return -1;
}

int mmc_getwp(struct mmc *mmc)
{
	int wp;

	wp = board_mmc_getwp(mmc);

72
	if (wp < 0) {
73 74
		if (mmc->cfg->ops->getwp)
			wp = mmc->cfg->ops->getwp(mmc);
75 76 77
		else
			wp = 0;
	}
78 79 80 81

	return wp;
}

Jeroen Hofstee's avatar
Jeroen Hofstee committed
82 83
__weak int board_mmc_getcd(struct mmc *mmc)
{
84 85
	return -1;
}
86
#endif
87

88 89
#ifdef CONFIG_MMC_TRACE
void mmmc_trace_before_send(struct mmc *mmc, struct mmc_cmd *cmd)
Andy Fleming's avatar
Andy Fleming committed
90
{
91 92 93
	printf("CMD_SEND:%d\n", cmd->cmdidx);
	printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
}
94

95 96
void mmmc_trace_after_send(struct mmc *mmc, struct mmc_cmd *cmd, int ret)
{
Raffaele Recalcati's avatar
Raffaele Recalcati committed
97 98 99
	int i;
	u8 *ptr;

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	if (ret) {
		printf("\t\tRET\t\t\t %d\n", ret);
	} else {
		switch (cmd->resp_type) {
		case MMC_RSP_NONE:
			printf("\t\tMMC_RSP_NONE\n");
			break;
		case MMC_RSP_R1:
			printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
				cmd->response[0]);
			break;
		case MMC_RSP_R1b:
			printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
				cmd->response[0]);
			break;
		case MMC_RSP_R2:
			printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
				cmd->response[0]);
			printf("\t\t          \t\t 0x%08X \n",
				cmd->response[1]);
			printf("\t\t          \t\t 0x%08X \n",
				cmd->response[2]);
			printf("\t\t          \t\t 0x%08X \n",
				cmd->response[3]);
Raffaele Recalcati's avatar
Raffaele Recalcati committed
124
			printf("\n");
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
			printf("\t\t\t\t\tDUMPING DATA\n");
			for (i = 0; i < 4; i++) {
				int j;
				printf("\t\t\t\t\t%03d - ", i*4);
				ptr = (u8 *)&cmd->response[i];
				ptr += 3;
				for (j = 0; j < 4; j++)
					printf("%02X ", *ptr--);
				printf("\n");
			}
			break;
		case MMC_RSP_R3:
			printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
				cmd->response[0]);
			break;
		default:
			printf("\t\tERROR MMC rsp not supported\n");
			break;
Bin Meng's avatar
Bin Meng committed
143
		}
Raffaele Recalcati's avatar
Raffaele Recalcati committed
144
	}
145 146 147 148 149 150 151 152 153
}

void mmc_trace_state(struct mmc *mmc, struct mmc_cmd *cmd)
{
	int status;

	status = (cmd->response[0] & MMC_STATUS_CURR_STATE) >> 9;
	printf("CURR STATE:%d\n", status);
}
Raffaele Recalcati's avatar
Raffaele Recalcati committed
154
#endif
155

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
const char *mmc_mode_name(enum bus_mode mode)
{
	static const char *const names[] = {
	      [MMC_LEGACY]	= "MMC legacy",
	      [SD_LEGACY]	= "SD Legacy",
	      [MMC_HS]		= "MMC High Speed (26MHz)",
	      [SD_HS]		= "SD High Speed (50MHz)",
	      [UHS_SDR12]	= "UHS SDR12 (25MHz)",
	      [UHS_SDR25]	= "UHS SDR25 (50MHz)",
	      [UHS_SDR50]	= "UHS SDR50 (100MHz)",
	      [UHS_SDR104]	= "UHS SDR104 (208MHz)",
	      [UHS_DDR50]	= "UHS DDR50 (50MHz)",
	      [MMC_HS_52]	= "MMC High Speed (52MHz)",
	      [MMC_DDR_52]	= "MMC DDR52 (52MHz)",
	      [MMC_HS_200]	= "HS200 (200MHz)",
	};

	if (mode >= MMC_MODES_END)
		return "Unknown mode";
	else
		return names[mode];
}
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

static uint mmc_mode2freq(struct mmc *mmc, enum bus_mode mode)
{
	static const int freqs[] = {
	      [SD_LEGACY]	= 25000000,
	      [MMC_HS]		= 26000000,
	      [SD_HS]		= 50000000,
	      [UHS_SDR12]	= 25000000,
	      [UHS_SDR25]	= 50000000,
	      [UHS_SDR50]	= 100000000,
	      [UHS_SDR104]	= 208000000,
	      [UHS_DDR50]	= 50000000,
	      [MMC_HS_52]	= 52000000,
	      [MMC_DDR_52]	= 52000000,
	      [MMC_HS_200]	= 200000000,
	};

	if (mode == MMC_LEGACY)
		return mmc->legacy_speed;
	else if (mode >= MMC_MODES_END)
		return 0;
	else
		return freqs[mode];
}

203 204 205
static int mmc_select_mode(struct mmc *mmc, enum bus_mode mode)
{
	mmc->selected_mode = mode;
206
	mmc->tran_speed = mmc_mode2freq(mmc, mode);
207
	mmc->ddr_mode = mmc_is_mode_ddr(mode);
208 209 210 211 212
	debug("selecting mode %s (freq : %d MHz)\n", mmc_mode_name(mode),
	      mmc->tran_speed / 1000000);
	return 0;
}

213
#ifndef CONFIG_DM_MMC_OPS
214 215 216 217 218 219 220 221
int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
{
	int ret;

	mmmc_trace_before_send(mmc, cmd);
	ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
	mmmc_trace_after_send(mmc, cmd, ret);

222
	return ret;
Andy Fleming's avatar
Andy Fleming committed
223
}
224 225 226 227 228

int mmc_execute_tuning(struct mmc *mmc, uint opcode)
{
	return mmc->cfg->ops->execute_tuning(mmc, opcode);
}
229
#endif
Andy Fleming's avatar
Andy Fleming committed
230

231
int mmc_send_status(struct mmc *mmc, int timeout)
232 233
{
	struct mmc_cmd cmd;
234
	int err, retries = 5;
235 236 237

	cmd.cmdidx = MMC_CMD_SEND_STATUS;
	cmd.resp_type = MMC_RSP_R1;
Marek Vasut's avatar
Marek Vasut committed
238 239
	if (!mmc_host_is_spi(mmc))
		cmd.cmdarg = mmc->rca << 16;
240

241
	while (1) {
242
		err = mmc_send_cmd(mmc, &cmd, NULL);
243 244 245 246 247 248
		if (!err) {
			if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
			    (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
			     MMC_STATE_PRG)
				break;
			else if (cmd.response[0] & MMC_STATUS_MASK) {
249
#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
250 251
				printf("Status Error: 0x%08X\n",
					cmd.response[0]);
252
#endif
253
				return -ECOMM;
254 255
			}
		} else if (--retries < 0)
256 257
			return err;

258 259
		if (timeout-- <= 0)
			break;
260

261 262
		udelay(1000);
	}
263

264
	mmc_trace_state(mmc, &cmd);
265
	if (timeout <= 0) {
266
#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
267
		printf("Timeout waiting card ready\n");
268
#endif
269
		return -ETIMEDOUT;
270 271 272 273 274
	}

	return 0;
}

275
int mmc_set_blocklen(struct mmc *mmc, int len)
Andy Fleming's avatar
Andy Fleming committed
276 277 278
{
	struct mmc_cmd cmd;

279
	if (mmc->ddr_mode)
280 281
		return 0;

Andy Fleming's avatar
Andy Fleming committed
282 283 284 285 286 287 288
	cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
	cmd.resp_type = MMC_RSP_R1;
	cmd.cmdarg = len;

	return mmc_send_cmd(mmc, &cmd, NULL);
}

289
static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
290
			   lbaint_t blkcnt)
Andy Fleming's avatar
Andy Fleming committed
291 292 293 294
{
	struct mmc_cmd cmd;
	struct mmc_data data;

295 296 297 298
	if (blkcnt > 1)
		cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
	else
		cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Fleming's avatar
Andy Fleming committed
299 300

	if (mmc->high_capacity)
301
		cmd.cmdarg = start;
Andy Fleming's avatar
Andy Fleming committed
302
	else
303
		cmd.cmdarg = start * mmc->read_bl_len;
Andy Fleming's avatar
Andy Fleming committed
304 305 306 307

	cmd.resp_type = MMC_RSP_R1;

	data.dest = dst;
308
	data.blocks = blkcnt;
Andy Fleming's avatar
Andy Fleming committed
309 310 311
	data.blocksize = mmc->read_bl_len;
	data.flags = MMC_DATA_READ;

312 313
	if (mmc_send_cmd(mmc, &cmd, &data))
		return 0;
Andy Fleming's avatar
Andy Fleming committed
314

315 316 317 318 319
	if (blkcnt > 1) {
		cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
		cmd.cmdarg = 0;
		cmd.resp_type = MMC_RSP_R1b;
		if (mmc_send_cmd(mmc, &cmd, NULL)) {
320
#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
321
			printf("mmc fail to send stop cmd\n");
322
#endif
323 324
			return 0;
		}
Andy Fleming's avatar
Andy Fleming committed
325 326
	}

327
	return blkcnt;
Andy Fleming's avatar
Andy Fleming committed
328 329
}

330
#ifdef CONFIG_BLK
331
ulong mmc_bread(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, void *dst)
332
#else
333 334
ulong mmc_bread(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt,
		void *dst)
335
#endif
Andy Fleming's avatar
Andy Fleming committed
336
{
337 338 339
#ifdef CONFIG_BLK
	struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
#endif
340
	int dev_num = block_dev->devnum;
341
	int err;
342 343 344 345
	lbaint_t cur, blocks_todo = blkcnt;

	if (blkcnt == 0)
		return 0;
Andy Fleming's avatar
Andy Fleming committed
346

347
	struct mmc *mmc = find_mmc_device(dev_num);
Andy Fleming's avatar
Andy Fleming committed
348 349 350
	if (!mmc)
		return 0;

351 352 353 354 355
	if (CONFIG_IS_ENABLED(MMC_TINY))
		err = mmc_switch_part(mmc, block_dev->hwpart);
	else
		err = blk_dselect_hwpart(block_dev, block_dev->hwpart);

356 357 358
	if (err < 0)
		return 0;

359
	if ((start + blkcnt) > block_dev->lba) {
360
#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
361
		printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
362
			start + blkcnt, block_dev->lba);
363
#endif
364 365
		return 0;
	}
Andy Fleming's avatar
Andy Fleming committed
366

367 368
	if (mmc_set_blocklen(mmc, mmc->read_bl_len)) {
		debug("%s: Failed to set blocklen\n", __func__);
Andy Fleming's avatar
Andy Fleming committed
369
		return 0;
370
	}
Andy Fleming's avatar
Andy Fleming committed
371

372
	do {
373 374
		cur = (blocks_todo > mmc->cfg->b_max) ?
			mmc->cfg->b_max : blocks_todo;
375 376
		if (mmc_read_blocks(mmc, dst, start, cur) != cur) {
			debug("%s: Failed to read blocks\n", __func__);
377
			return 0;
378
		}
379 380 381 382
		blocks_todo -= cur;
		start += cur;
		dst += cur * mmc->read_bl_len;
	} while (blocks_todo > 0);
Andy Fleming's avatar
Andy Fleming committed
383 384 385 386

	return blkcnt;
}

387
static int mmc_go_idle(struct mmc *mmc)
Andy Fleming's avatar
Andy Fleming committed
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
{
	struct mmc_cmd cmd;
	int err;

	udelay(1000);

	cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
	cmd.cmdarg = 0;
	cmd.resp_type = MMC_RSP_NONE;

	err = mmc_send_cmd(mmc, &cmd, NULL);

	if (err)
		return err;

	udelay(2000);

	return 0;
}

408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
static int mmc_switch_voltage(struct mmc *mmc, int signal_voltage)
{
	struct mmc_cmd cmd;
	int err = 0;

	/*
	 * Send CMD11 only if the request is to switch the card to
	 * 1.8V signalling.
	 */
	if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
		return mmc_set_signal_voltage(mmc, signal_voltage);

	cmd.cmdidx = SD_CMD_SWITCH_UHS18V;
	cmd.cmdarg = 0;
	cmd.resp_type = MMC_RSP_R1;

	err = mmc_send_cmd(mmc, &cmd, NULL);
	if (err)
		goto fail;

	if (!mmc_host_is_spi(host) && (cmd.response[0] & MMC_STATUS_ERROR))
		goto fail;

	/*
	 * The card should drive cmd and dat[0:3] low immediately
	 * after the response of cmd11, but wait 1 ms to be sure
	 */
	udelay(1000);
	if (mmc_card_busy(mmc))
		goto fail;

	/*
	 * During a signal voltage level switch, the clock must be gated
	 * for 5 ms according to the SD spec
	 */
	mmc_set_clock(mmc, mmc->clock, true);

	err = mmc_set_signal_voltage(mmc, signal_voltage);
	if (err)
		goto fail;

	/* Keep clock gated for at least 10 ms, though spec only says 5 ms */
	udelay(10000);
	mmc_set_clock(mmc, mmc->clock, false);

	/* Wait for at least 1 ms according to spec */
	udelay(1000);

	/*
	 * Failure to switch is indicated by the card holding
	 * dat[0:3] low
	 */
	if (mmc_card_busy(mmc))
		goto fail;

	return 0;

fail:
	return -EIO;
}

static int sd_send_op_cond(struct mmc *mmc, bool uhs_en)
Andy Fleming's avatar
Andy Fleming committed
470 471 472 473 474
{
	int timeout = 1000;
	int err;
	struct mmc_cmd cmd;

475
	while (1) {
Andy Fleming's avatar
Andy Fleming committed
476 477 478 479 480 481 482 483 484 485 486
		cmd.cmdidx = MMC_CMD_APP_CMD;
		cmd.resp_type = MMC_RSP_R1;
		cmd.cmdarg = 0;

		err = mmc_send_cmd(mmc, &cmd, NULL);

		if (err)
			return err;

		cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
		cmd.resp_type = MMC_RSP_R3;
487 488 489 490 491 492 493 494

		/*
		 * Most cards do not answer if some reserved bits
		 * in the ocr are set. However, Some controller
		 * can set bit 7 (reserved for low voltages), but
		 * how to manage low voltages SD card is not yet
		 * specified.
		 */
495
		cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
496
			(mmc->cfg->voltages & 0xff8000);
Andy Fleming's avatar
Andy Fleming committed
497 498 499 500

		if (mmc->version == SD_VERSION_2)
			cmd.cmdarg |= OCR_HCS;

501 502 503
		if (uhs_en)
			cmd.cmdarg |= OCR_S18R;

Andy Fleming's avatar
Andy Fleming committed
504 505 506 507 508
		err = mmc_send_cmd(mmc, &cmd, NULL);

		if (err)
			return err;

509 510 511 512
		if (cmd.response[0] & OCR_BUSY)
			break;

		if (timeout-- <= 0)
513
			return -EOPNOTSUPP;
Andy Fleming's avatar
Andy Fleming committed
514

515 516
		udelay(1000);
	}
Andy Fleming's avatar
Andy Fleming committed
517 518 519 520

	if (mmc->version != SD_VERSION_2)
		mmc->version = SD_VERSION_1_0;

521 522 523 524 525 526 527 528 529 530 531
	if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
		cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
		cmd.resp_type = MMC_RSP_R3;
		cmd.cmdarg = 0;

		err = mmc_send_cmd(mmc, &cmd, NULL);

		if (err)
			return err;
	}

Rabin Vincent's avatar
Rabin Vincent committed
532
	mmc->ocr = cmd.response[0];
Andy Fleming's avatar
Andy Fleming committed
533

534 535 536 537 538 539 540
	if (!(mmc_host_is_spi(mmc)) && (cmd.response[0] & 0x41000000)
	    == 0x41000000) {
		err = mmc_switch_voltage(mmc, MMC_SIGNAL_VOLTAGE_180);
		if (err)
			return err;
	}

Andy Fleming's avatar
Andy Fleming committed
541 542 543 544 545 546
	mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
	mmc->rca = 0;

	return 0;
}

547
static int mmc_send_op_cond_iter(struct mmc *mmc, int use_arg)
Andy Fleming's avatar
Andy Fleming committed
548
{
549
	struct mmc_cmd cmd;
Andy Fleming's avatar
Andy Fleming committed
550 551
	int err;

552 553 554
	cmd.cmdidx = MMC_CMD_SEND_OP_COND;
	cmd.resp_type = MMC_RSP_R3;
	cmd.cmdarg = 0;
555 556
	if (use_arg && !mmc_host_is_spi(mmc))
		cmd.cmdarg = OCR_HCS |
557
			(mmc->cfg->voltages &
558 559
			(mmc->ocr & OCR_VOLTAGE_MASK)) |
			(mmc->ocr & OCR_ACCESS_MODE);
560

561
	err = mmc_send_cmd(mmc, &cmd, NULL);
562 563
	if (err)
		return err;
564
	mmc->ocr = cmd.response[0];
565 566 567
	return 0;
}

568
static int mmc_send_op_cond(struct mmc *mmc)
569 570 571
{
	int err, i;

Andy Fleming's avatar
Andy Fleming committed
572 573 574
	/* Some cards seem to need this */
	mmc_go_idle(mmc);

575
 	/* Asking to the card its capabilities */
576
	for (i = 0; i < 2; i++) {
577
		err = mmc_send_op_cond_iter(mmc, i != 0);
578 579
		if (err)
			return err;
Wolfgang Denk's avatar
Wolfgang Denk committed
580

581
		/* exit if not busy (flag seems to be inverted) */
582
		if (mmc->ocr & OCR_BUSY)
583
			break;
584
	}
585 586
	mmc->op_cond_pending = 1;
	return 0;
587
}
Wolfgang Denk's avatar
Wolfgang Denk committed
588

589
static int mmc_complete_op_cond(struct mmc *mmc)
590 591 592 593 594
{
	struct mmc_cmd cmd;
	int timeout = 1000;
	uint start;
	int err;
Wolfgang Denk's avatar
Wolfgang Denk committed
595

596
	mmc->op_cond_pending = 0;
597
	if (!(mmc->ocr & OCR_BUSY)) {
598 599 600
		/* Some cards seem to need this */
		mmc_go_idle(mmc);

601
		start = get_timer(0);
602
		while (1) {
603 604 605
			err = mmc_send_op_cond_iter(mmc, 1);
			if (err)
				return err;
606 607
			if (mmc->ocr & OCR_BUSY)
				break;
608
			if (get_timer(start) > timeout)
609
				return -EOPNOTSUPP;
610
			udelay(100);
611
		}
612
	}
Andy Fleming's avatar
Andy Fleming committed
613

614 615 616 617 618 619 620 621 622
	if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
		cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
		cmd.resp_type = MMC_RSP_R3;
		cmd.cmdarg = 0;

		err = mmc_send_cmd(mmc, &cmd, NULL);

		if (err)
			return err;
623 624

		mmc->ocr = cmd.response[0];
625 626
	}

Andy Fleming's avatar
Andy Fleming committed
627 628 629
	mmc->version = MMC_VERSION_UNKNOWN;

	mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
630
	mmc->rca = 1;
Andy Fleming's avatar
Andy Fleming committed
631 632 633 634 635

	return 0;
}


636
static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
Andy Fleming's avatar
Andy Fleming committed
637 638 639 640 641 642 643 644 645 646
{
	struct mmc_cmd cmd;
	struct mmc_data data;
	int err;

	/* Get the Card Status Register */
	cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
	cmd.resp_type = MMC_RSP_R1;
	cmd.cmdarg = 0;

647
	data.dest = (char *)ext_csd;
Andy Fleming's avatar
Andy Fleming committed
648
	data.blocks = 1;
649
	data.blocksize = MMC_MAX_BLOCK_LEN;
Andy Fleming's avatar
Andy Fleming committed
650 651 652 653 654 655 656
	data.flags = MMC_DATA_READ;

	err = mmc_send_cmd(mmc, &cmd, &data);

	return err;
}

657
int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
Andy Fleming's avatar
Andy Fleming committed
658 659
{
	struct mmc_cmd cmd;
660
	int timeout = 1000;
661
	int retries = 3;
662
	int ret;
Andy Fleming's avatar
Andy Fleming committed
663 664 665 666

	cmd.cmdidx = MMC_CMD_SWITCH;
	cmd.resp_type = MMC_RSP_R1b;
	cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
667 668
				 (index << 16) |
				 (value << 8);
Andy Fleming's avatar
Andy Fleming committed
669

670 671
	while (retries > 0) {
		ret = mmc_send_cmd(mmc, &cmd, NULL);
672

673 674 675 676 677 678 679 680
		/* Waiting for the ready status */
		if (!ret) {
			ret = mmc_send_status(mmc, timeout);
			return ret;
		}

		retries--;
	}
681 682 683

	return ret;

Andy Fleming's avatar
Andy Fleming committed
684 685
}

686
static int mmc_set_card_speed(struct mmc *mmc, enum bus_mode mode)
Andy Fleming's avatar
Andy Fleming committed
687 688
{
	int err;
689 690 691 692 693 694 695 696
	int speed_bits;
	ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);

	switch (mode) {
	case MMC_HS:
	case MMC_HS_52:
	case MMC_DDR_52:
		  speed_bits = EXT_CSD_TIMING_HS;
697 698 699 700
		  break;
	case MMC_HS_200:
		  speed_bits = EXT_CSD_TIMING_HS200;
		  break;
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
	case MMC_LEGACY:
		  speed_bits = EXT_CSD_TIMING_LEGACY;
		  break;
	default:
		  return -EINVAL;
	}
	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
			 speed_bits);
	if (err)
		return err;

	if ((mode == MMC_HS) || (mode == MMC_HS_52)) {
		/* Now check to see that it worked */
		err = mmc_send_ext_csd(mmc, test_csd);
		if (err)
			return err;

		/* No high-speed support */
		if (!test_csd[EXT_CSD_HS_TIMING])
			return -ENOTSUPP;
	}

	return 0;
}

static int mmc_get_capabilities(struct mmc *mmc)
{
	u8 *ext_csd = mmc->ext_csd;
	char cardtype;
Andy Fleming's avatar
Andy Fleming committed
730

731
	mmc->card_caps = MMC_MODE_1BIT;
Andy Fleming's avatar
Andy Fleming committed
732

733 734 735
	if (mmc_host_is_spi(mmc))
		return 0;

Andy Fleming's avatar
Andy Fleming committed
736 737 738 739
	/* Only version 4 supports high-speed */
	if (mmc->version < MMC_VERSION_4)
		return 0;

740 741 742 743
	if (!ext_csd) {
		error("No ext_csd found!\n"); /* this should enver happen */
		return -ENOTSUPP;
	}
Andy Fleming's avatar
Andy Fleming committed
744

745
	mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT;
Andy Fleming's avatar
Andy Fleming committed
746

747
	cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0x3f;
Andy Fleming's avatar
Andy Fleming committed
748

749 750 751 752
	if (cardtype & (EXT_CSD_CARD_TYPE_HS200_1_2V |
			EXT_CSD_CARD_TYPE_HS200_1_8V)) {
		mmc->card_caps |= MMC_MODE_HS200;
	}
753
	if (cardtype & EXT_CSD_CARD_TYPE_52) {
754
		if (cardtype & EXT_CSD_CARD_TYPE_DDR_52)
755
			mmc->card_caps |= MMC_MODE_DDR_52MHz;
756
		mmc->card_caps |= MMC_MODE_HS_52MHz;
757
	}
758 759
	if (cardtype & EXT_CSD_CARD_TYPE_26)
		mmc->card_caps |= MMC_MODE_HS;
Andy Fleming's avatar
Andy Fleming committed
760 761 762 763

	return 0;
}

764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
static int mmc_set_capacity(struct mmc *mmc, int part_num)
{
	switch (part_num) {
	case 0:
		mmc->capacity = mmc->capacity_user;
		break;
	case 1:
	case 2:
		mmc->capacity = mmc->capacity_boot;
		break;
	case 3:
		mmc->capacity = mmc->capacity_rpmb;
		break;
	case 4:
	case 5:
	case 6:
	case 7:
		mmc->capacity = mmc->capacity_gp[part_num - 4];
		break;
	default:
		return -1;
	}

787
	mmc_get_blk_desc(mmc)->lba = lldiv(mmc->capacity, mmc->read_bl_len);
788 789 790 791

	return 0;
}

792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
static int mmc_boot_part_access_chk(struct mmc *mmc, unsigned int part_num)
{
	int forbiden = 0;
	bool change = false;

	if (part_num & PART_ACCESS_MASK)
		forbiden = MMC_CAP(MMC_HS_200);

	if (MMC_CAP(mmc->selected_mode) & forbiden) {
		debug("selected mode (%s) is forbiden for part %d\n",
		      mmc_mode_name(mmc->selected_mode), part_num);
		change = true;
	} else if (mmc->selected_mode != mmc->best_mode) {
		debug("selected mode is not optimal\n");
		change = true;
	}

	if (change)
		return mmc_select_mode_and_width(mmc,
						 mmc->card_caps & ~forbiden);

	return 0;
}

816
int mmc_switch_part(struct mmc *mmc, unsigned int part_num)
817
{
818
	int ret;
819

820 821 822 823
	ret = mmc_boot_part_access_chk(mmc, part_num);
	if (ret)
		return ret;

824 825 826 827
	ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
			 (mmc->part_config & ~PART_ACCESS_MASK)
			 | (part_num & PART_ACCESS_MASK));

828 829 830 831
	/*
	 * Set the capacity if the switch succeeded or was intended
	 * to return to representing the raw device.
	 */
832
	if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0))) {
833
		ret = mmc_set_capacity(mmc, part_num);
834
		mmc_get_blk_desc(mmc)->hwpart = part_num;
835
	}
836 837

	return ret;
838 839
}

840 841 842 843 844 845 846 847 848 849
int mmc_hwpart_config(struct mmc *mmc,
		      const struct mmc_hwpart_conf *conf,
		      enum mmc_hwpart_conf_mode mode)
{
	u8 part_attrs = 0;
	u32 enh_size_mult;
	u32 enh_start_addr;
	u32 gp_size_mult[4];
	u32 max_enh_size_mult;
	u32 tot_enh_size_mult = 0;
850
	u8 wr_rel_set;
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
	int i, pidx, err;
	ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);

	if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE)
		return -EINVAL;

	if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) {
		printf("eMMC >= 4.4 required for enhanced user data area\n");
		return -EMEDIUMTYPE;
	}

	if (!(mmc->part_support & PART_SUPPORT)) {
		printf("Card does not support partitioning\n");
		return -EMEDIUMTYPE;
	}

	if (!mmc->hc_wp_grp_size) {
		printf("Card does not define HC WP group size\n");
		return -EMEDIUMTYPE;
	}

	/* check partition alignment and total enhanced size */
	if (conf->user.enh_size) {
		if (conf->user.enh_size % mmc->hc_wp_grp_size ||
		    conf->user.enh_start % mmc->hc_wp_grp_size) {
			printf("User data enhanced area not HC WP group "
			       "size aligned\n");
			return -EINVAL;
		}
		part_attrs |= EXT_CSD_ENH_USR;
		enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size;
		if (mmc->high_capacity) {
			enh_start_addr = conf->user.enh_start;
		} else {
			enh_start_addr = (conf->user.enh_start << 9);
		}
	} else {
		enh_size_mult = 0;
		enh_start_addr = 0;
	}
	tot_enh_size_mult += enh_size_mult;

	for (pidx = 0; pidx < 4; pidx++) {
		if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) {
			printf("GP%i partition not HC WP group size "
			       "aligned\n", pidx+1);
			return -EINVAL;
		}
		gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size;
		if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) {
			part_attrs |= EXT_CSD_ENH_GP(pidx);
			tot_enh_size_mult += gp_size_mult[pidx];
		}
	}

	if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) {
		printf("Card does not support enhanced attribute\n");
		return -EMEDIUMTYPE;
	}

	err = mmc_send_ext_csd(mmc, ext_csd);
	if (err)
		return err;

	max_enh_size_mult =
		(ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) +
		(ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) +
		ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT];
	if (tot_enh_size_mult > max_enh_size_mult) {
		printf("Total enhanced size exceeds maximum (%u > %u)\n",
		       tot_enh_size_mult, max_enh_size_mult);
		return -EMEDIUMTYPE;
	}

925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
	/* The default value of EXT_CSD_WR_REL_SET is device
	 * dependent, the values can only be changed if the
	 * EXT_CSD_HS_CTRL_REL bit is set. The values can be
	 * changed only once and before partitioning is completed. */
	wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
	if (conf->user.wr_rel_change) {
		if (conf->user.wr_rel_set)
			wr_rel_set |= EXT_CSD_WR_DATA_REL_USR;
		else
			wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR;
	}
	for (pidx = 0; pidx < 4; pidx++) {
		if (conf->gp_part[pidx].wr_rel_change) {
			if (conf->gp_part[pidx].wr_rel_set)
				wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx);
			else
				wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx);
		}
	}

	if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] &&
	    !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) {
		puts("Card does not support host controlled partition write "
		     "reliability settings\n");
		return -EMEDIUMTYPE;
	}

952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
	if (ext_csd[EXT_CSD_PARTITION_SETTING] &
	    EXT_CSD_PARTITION_SETTING_COMPLETED) {
		printf("Card already partitioned\n");
		return -EPERM;
	}

	if (mode == MMC_HWPART_CONF_CHECK)
		return 0;

	/* Partitioning requires high-capacity size definitions */
	if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) {
		err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
				 EXT_CSD_ERASE_GROUP_DEF, 1);

		if (err)
			return err;

		ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;

		/* update erase group size to be high-capacity */
		mmc->erase_grp_size =
			ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;

	}

	/* all OK, write the configuration */
	for (i = 0; i < 4; i++) {
		err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
				 EXT_CSD_ENH_START_ADDR+i,
				 (enh_start_addr >> (i*8)) & 0xFF);
		if (err)
			return err;
	}
	for (i = 0; i < 3; i++) {
		err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
				 EXT_CSD_ENH_SIZE_MULT+i,
				 (enh_size_mult >> (i*8)) & 0xFF);
		if (err)
			return err;
	}
	for (pidx = 0; pidx < 4; pidx++) {
		for (i = 0; i < 3; i++) {
			err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
					 EXT_CSD_GP_SIZE_MULT+pidx*3+i,
					 (gp_size_mult[pidx] >> (i*8)) & 0xFF);
			if (err)
				return err;
		}
	}
	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
			 EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs);
	if (err)
		return err;

	if (mode == MMC_HWPART_CONF_SET)
		return 0;

1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
	/* The WR_REL_SET is a write-once register but shall be
	 * written before setting PART_SETTING_COMPLETED. As it is
	 * write-once we can only write it when completing the
	 * partitioning. */
	if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) {
		err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
				 EXT_CSD_WR_REL_SET, wr_rel_set);
		if (err)
			return err;
	}

1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
	/* Setting PART_SETTING_COMPLETED confirms the partition
	 * configuration but it only becomes effective after power
	 * cycle, so we do not adjust the partition related settings
	 * in the mmc struct. */

	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
			 EXT_CSD_PARTITION_SETTING,
			 EXT_CSD_PARTITION_SETTING_COMPLETED);
	if (err)
		return err;

	return 0;
}

1034
#ifndef CONFIG_DM_MMC_OPS
1035 1036 1037 1038 1039 1040
int mmc_getcd(struct mmc *mmc)
{
	int cd;

	cd = board_mmc_getcd(mmc);

1041
	if (cd < 0) {
1042 1043
		if (mmc->cfg->ops->getcd)
			cd = mmc->cfg->ops->getcd(mmc);
1044 1045 1046
		else
			cd = 1;
	}
1047 1048 1049

	return cd;
}
1050
#endif
1051

1052
static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
Andy Fleming's avatar
Andy Fleming committed
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
{
	struct mmc_cmd cmd;
	struct mmc_data data;

	/* Switch the frequency */
	cmd.cmdidx = SD_CMD_SWITCH_FUNC;
	cmd.resp_type = MMC_RSP_R1;
	cmd.cmdarg = (mode << 31) | 0xffffff;
	cmd.cmdarg &= ~(0xf << (group * 4));
	cmd.cmdarg |= value << (group * 4);

	data.dest = (char *)resp;
	data.blocksize = 64;
	data.blocks = 1;
	data.flags = MMC_DATA_READ;

	return mmc_send_cmd(mmc, &cmd, &data);
}


1073
static int sd_get_capabilities(struct mmc *mmc)
Andy Fleming's avatar
Andy Fleming committed
1074 1075 1076
{
	int err;
	struct mmc_cmd cmd;
1077 1078
	ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
	ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
Andy Fleming's avatar
Andy Fleming committed
1079 1080
	struct mmc_data data;
	int timeout;
1081
	u32 sd3_bus_mode;
Andy Fleming's avatar
Andy Fleming committed
1082

1083
	mmc->card_caps = MMC_MODE_1BIT;
Andy Fleming's avatar
Andy Fleming committed
1084

1085 1086 1087
	if (mmc_host_is_spi(mmc))
		return 0;

Andy Fleming's avatar
Andy Fleming committed
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
	/* Read the SCR to find out if this card supports higher speeds */
	cmd.cmdidx = MMC_CMD_APP_CMD;
	cmd.resp_type = MMC_RSP_R1;
	cmd.cmdarg = mmc->rca << 16;

	err = mmc_send_cmd(mmc, &cmd, NULL);

	if (err)
		return err;

	cmd.cmdidx = SD_CMD_APP_SEND_SCR;
	cmd.resp_type = MMC_RSP_R1;
	cmd.cmdarg = 0;

	timeout = 3;

retry_scr:
1105
	data.dest = (char *)scr;
Andy Fleming's avatar
Andy Fleming committed
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
	data.blocksize = 8;
	data.blocks = 1;
	data.flags = MMC_DATA_READ;

	err = mmc_send_cmd(mmc, &cmd, &data);

	if (err) {
		if (timeout--)
			goto retry_scr;

		return err;
	}

1119 1120
	mmc->scr[0] = __be32_to_cpu(scr[0]);
	mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Fleming's avatar
Andy Fleming committed
1121 1122

	switch ((mmc->scr[0] >> 24) & 0xf) {
Bin Meng's avatar
Bin Meng committed
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
	case 0:
		mmc->version = SD_VERSION_1_0;
		break;
	case 1:
		mmc->version = SD_VERSION_1_10;
		break;
	case 2:
		mmc->version = SD_VERSION_2;
		if ((mmc->scr[0] >> 15) & 0x1)
			mmc->version = SD_VERSION_3;
		break;
	default:
		mmc->version = SD_VERSION_1_0;
		break;
Andy Fleming's avatar
Andy Fleming committed
1137 1138
	}

Alagu Sankar's avatar
Alagu Sankar committed
1139 1140 1141
	if (mmc->scr[0] & SD_DATA_4BIT)
		mmc->card_caps |= MMC_MODE_4BIT;

Andy Fleming's avatar
Andy Fleming committed
1142 1143 1144 1145 1146 1147 1148
	/* Version 1.0 doesn't support switching */
	if (mmc->version == SD_VERSION_1_0)
		return 0;

	timeout = 4;
	while (timeout--) {
		err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
1149
				(u8 *)switch_status);
Andy Fleming's avatar
Andy Fleming committed
1150 1151 1152 1153 1154

		if (err)
			return err;

		/* The high-speed function is busy.  Try again */
1155
		if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Fleming's avatar
Andy Fleming committed
1156 1157 1158 1159
			break;
	}

	/* If high-speed isn't supported, we return */
1160 1161
	if (__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)
		mmc->card_caps |= MMC_CAP(SD_HS);
Andy Fleming's avatar
Andy Fleming committed
1162

1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
	/* Version before 3.0 don't support UHS modes */
	if (mmc->version < SD_VERSION_3)
		return 0;

	sd3_bus_mode = __be32_to_cpu(switch_status[3]) >> 16 & 0x1f;
	if (sd3_bus_mode & SD_MODE_UHS_SDR104)
		mmc->card_caps |= MMC_CAP(UHS_SDR104);
	if (sd3_bus_mode & SD_MODE_UHS_SDR50)
		mmc->card_caps |= MMC_CAP(UHS_SDR50);
	if (sd3_bus_mode & SD_MODE_UHS_SDR25)
		mmc->card_caps |= MMC_CAP(UHS_SDR25);
	if (sd3_bus_mode & SD_MODE_UHS_SDR12)
		mmc->card_caps |= MMC_CAP(UHS_SDR12);
	if (sd3_bus_mode & SD_MODE_UHS_DDR50)
		mmc->card_caps |= MMC_CAP(UHS_DDR50);

1179 1180 1181 1182 1183 1184 1185
	return 0;
}

static int sd_set_card_speed(struct mmc *mmc, enum bus_mode mode)
{
	int err;
	ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
	int speed;

	switch (mode) {
	case SD_LEGACY:
	case UHS_SDR12:
		speed = UHS_SDR12_BUS_SPEED;
		break;
	case SD_HS:
	case UHS_SDR25:
		speed = UHS_SDR25_BUS_SPEED;
		break;
	case UHS_SDR50:
		speed = UHS_SDR50_BUS_SPEED;
		break;
	case UHS_DDR50:
		speed = UHS_DDR50_BUS_SPEED;
		break;
	case UHS_SDR104:
		speed = UHS_SDR104_BUS_SPEED;
		break;
	default:
		return -EINVAL;
	}
1209

1210
	err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, speed, (u8 *)switch_status);
Andy Fleming's avatar
Andy Fleming committed
1211 1212 1213 1214

	if (err)
		return err;

1215
	if ((__be32_to_cpu(switch_status[4]) >> 24) != speed)
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
		return -ENOTSUPP;

	return 0;
}

int sd_select_bus_width(struct mmc *mmc, int w)
{
	int err;
	struct mmc_cmd cmd;

	if ((w != 4) && (w != 1))
		return -EINVAL;

	cmd.cmdidx = MMC_CMD_APP_CMD;
	cmd.resp_type = MMC_RSP_R1;
	cmd.cmdarg = mmc->rca << 16;

	err = mmc_send_cmd(mmc, &cmd, NULL);
	if (err)
		return err;

	cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
	cmd.resp_type = MMC_RSP_R1;
	if (w == 4)
		cmd.cmdarg = 2;
	else if (w == 1)
		cmd.cmdarg = 0;
	err = mmc_send_cmd(mmc, &cmd, NULL);
	if (err)
		return err;
Andy Fleming's avatar
Andy Fleming committed
1246 1247 1248 1249

	return 0;
}

1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
static int sd_read_ssr(struct mmc *mmc)
{
	int err, i;
	struct mmc_cmd cmd;
	ALLOC_CACHE_ALIGN_BUFFER(uint, ssr, 16);
	struct mmc_data data;
	int timeout = 3;
	unsigned int au, eo, et, es;

	cmd.cmdidx = MMC_CMD_APP_CMD;
	cmd.resp_type = MMC_RSP_R1;
	cmd.cmdarg = mmc->rca << 16;

	err = mmc_send_cmd(mmc, &cmd, NULL);
	if (err)
		return err;

	cmd.cmdidx = SD_CMD_APP_SD_STATUS;
	cmd.resp_type = MMC_RSP_R1;
	cmd.cmdarg = 0;

retry_ssr:
	data.dest = (char *)ssr;
	data.blocksize = 64;
	data.blocks = 1;
	data.flags = MMC_DATA_READ;

	err = mmc_send_cmd(mmc, &cmd, &data);
	if (err) {
		if (timeout--)
			goto retry_ssr;

		return err;
	}

	for (i = 0; i < 16; i++)
		ssr[i] = be32_to_cpu(ssr[i]);

	au = (ssr[2] >> 12) & 0xF;
	if ((au <= 9) || (mmc->version == SD_VERSION_3)) {
		mmc->ssr.au = sd_au_size[au];
		es = (ssr[3] >> 24) & 0xFF;
		es |= (ssr[2] & 0xFF) << 8;
		et = (ssr[3] >> 18) & 0x3F;
		if (es && et) {
			eo = (ssr[3] >> 16) & 0x3;
			mmc->ssr.erase_timeout = (et * 1000) / es;
			mmc->ssr.erase_offset = eo * 1000;
		}
	} else {
		debug("Invalid Allocation Unit Size.\n");
	}

	return 0;
}

Andy Fleming's avatar
Andy Fleming committed
1306 1307
/* frequency bases */
/* divided by 10 to be nice to platforms without floating point */
1308
static const int fbase[] = {
Andy Fleming's avatar
Andy Fleming committed
1309 1310 1311 1312 1313 1314 1315 1316 1317
	10000,
	100000,
	1000000,
	10000000,
};

/* Multiplier values for TRAN_SPEED.  Multiplied by 10 to be nice
 * to platforms without floating point.
 */
1318
static const u8 multipliers[] = {
Andy Fleming's avatar
Andy Fleming committed
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
	0,	/* reserved */
	10,
	12,
	13,
	15,
	20,
	25,
	30,
	35,
	40,
	45,
	50,
	55,
	60,
	70,
	80,
};

1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349

static inline int bus_width(uint cap)
{
	if (cap == MMC_MODE_8BIT)
		return 8;
	if (cap == MMC_MODE_4BIT)
		return 4;
	if (cap == MMC_MODE_1BIT)
		return 1;
	error("invalid bus witdh capability 0x%x\n", cap);
	return 0;
}

1350
#ifndef CONFIG_DM_MMC_OPS
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
static int mmc_set_vdd(struct mmc *mmc, bool enable)
{
	int ret = 0;

	if (mmc->cfg->ops->set_vdd)
		ret = mmc->cfg->ops->set_vdd(mmc, enable);

	return ret;
}

1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
static int mmc_card_busy(struct mmc *mmc)
{
	int ret = 0;

	if (mmc->cfg->ops->card_busy)
		ret = mmc->cfg->ops->card_busy(mmc);

	return ret;
}

1371
static int mmc_set_ios(struct mmc *mmc)
Andy Fleming's avatar
Andy Fleming committed
1372
{
1373 1374
	int ret = 0;

1375
	if (mmc->cfg->ops->set_ios)
1376 1377 1378
		ret = mmc->cfg->ops->set_ios(mmc);

	return ret;
Andy Fleming's avatar
Andy Fleming committed
1379
}
1380
#endif
Andy Fleming's avatar
Andy Fleming committed
1381

1382
int mmc_set_clock(struct mmc *mmc, uint clock, u8 disable)
Andy Fleming's avatar
Andy Fleming committed
1383
{
1384 1385
	if (clock > mmc->cfg->f_max)
		clock = mmc->cfg->f_max;
Andy Fleming's avatar
Andy Fleming committed
1386

1387 1388
	if (clock < mmc->cfg->f_min)
		clock = mmc->cfg->f_min;
Andy Fleming's avatar
Andy Fleming committed
1389 1390

	mmc->clock = clock;
1391
	mmc->clk_disable = disable;
Andy Fleming's avatar
Andy Fleming committed
1392

1393
	return mmc_set_ios(mmc);
Andy Fleming's avatar
Andy Fleming committed
1394 1395
}

1396
static int mmc_set_bus_width(struct mmc *mmc, uint width)
Andy Fleming's avatar
Andy Fleming committed
1397 1398 1399
{
	mmc->bus_width = width;

1400
	return mmc_set_ios(mmc);
Andy Fleming's avatar
Andy Fleming committed
1401 1402
}

1403 1404 1405 1406 1407 1408 1409 1410 1411
void mmc_dump_capabilities(const char *text, uint caps)
{
	enum bus_mode mode;

	printf("%s: widths [", text);
	if (caps & MMC_MODE_8BIT)
		printf("8, ");
	if (caps & MMC_MODE_4BIT)
		printf("4, ");
1412 1413 1414
	if (caps & MMC_MODE_1BIT)
		printf("1, ");
	printf("\b\b] modes [");
1415 1416 1417 1418 1419 1420
	for (mode = MMC_LEGACY; mode < MMC_MODES_END; mode++)
		if (MMC_CAP(mode) & caps)
			printf("%s, ", mmc_mode_name(mode));
	printf("\b\b]\n");
}

1421 1422 1423
struct mode_width_tuning {
	enum bus_mode mode;
	uint widths;
1424
	uint tuning;
1425 1426
};

1427 1428 1429 1430 1431 1432
static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage)
{
	mmc->signal_voltage = signal_voltage;
	return mmc_set_ios(mmc);
}

1433
static const struct mode_width_tuning sd_modes_by_pref[] = {
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
	{
		.mode = UHS_SDR104,
		.widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
		.tuning = MMC_SEND_TUNING_BLOCK
	},
	{
		.mode = UHS_SDR50,
		.widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
	},
	{
		.mode = UHS_DDR50,
		.widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
	},
	{
		.mode = UHS_SDR25,
		.widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
	},
1451 1452 1453 1454
	{
		.mode = SD_HS,
		.widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
	},
1455 1456 1457 1458
	{
		.mode = UHS_SDR12,
		.widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
	},
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469
	{
		.mode = SD_LEGACY,
		.widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
	}
};
#define for_each_sd_mode_by_pref(caps, mwt) \
	for (mwt = sd_modes_by_pref;\
	     mwt < sd_modes_by_pref + ARRAY_SIZE(sd_modes_by_pref);\
	     mwt++) \
		if (caps & MMC_CAP(mwt->mode))

1470
static int sd_select_mode_and_width(struct mmc *mmc, uint card_caps)
1471 1472
{
	int err;
1473 1474
	uint widths[] = {MMC_MODE_4BIT, MMC_MODE_1BIT};
	const struct mode_width_tuning *mwt;
1475 1476 1477
	bool uhs_en = (mmc->ocr & OCR_S18R) ? true : false;
	uint caps;

1478 1479

	/* Restrict card's capabilities by what the host can do */
1480
	caps = card_caps & (mmc->cfg->host_caps | MMC_MODE_1BIT);
1481

1482 1483 1484 1485
	if (!uhs_en)
		caps &= ~UHS_CAPS;

	for_each_sd_mode_by_pref(caps, mwt) {
1486 1487