mmc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. /*
  2. * Copyright (c) 2018-2022, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. /* Define a simple and generic interface to access eMMC and SD-card devices. */
  7. #include <assert.h>
  8. #include <errno.h>
  9. #include <stdbool.h>
  10. #include <string.h>
  11. #include <arch_helpers.h>
  12. #include <common/debug.h>
  13. #include <drivers/delay_timer.h>
  14. #include <drivers/mmc.h>
  15. #include <lib/utils.h>
  16. #include <plat/common/common_def.h>
  17. #define MMC_DEFAULT_MAX_RETRIES 5
  18. #define SEND_OP_COND_MAX_RETRIES 100
  19. #define MULT_BY_512K_SHIFT 19
  20. static const struct mmc_ops *ops;
  21. static unsigned int mmc_ocr_value;
  22. static struct mmc_csd_emmc mmc_csd;
  23. static struct sd_switch_status sd_switch_func_status;
  24. static unsigned char mmc_ext_csd[512] __aligned(16);
  25. static unsigned int mmc_flags;
  26. static struct mmc_device_info *mmc_dev_info;
  27. static unsigned int rca;
  28. static unsigned int scr[2]__aligned(16) = { 0 };
  29. static const unsigned char tran_speed_base[16] = {
  30. 0, 10, 12, 13, 15, 20, 26, 30, 35, 40, 45, 52, 55, 60, 70, 80
  31. };
  32. static const unsigned char sd_tran_speed_base[16] = {
  33. 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
  34. };
  35. static bool is_cmd23_enabled(void)
  36. {
  37. return ((mmc_flags & MMC_FLAG_CMD23) != 0U);
  38. }
  39. static bool is_sd_cmd6_enabled(void)
  40. {
  41. return ((mmc_flags & MMC_FLAG_SD_CMD6) != 0U);
  42. }
  43. static int mmc_send_cmd(unsigned int idx, unsigned int arg,
  44. unsigned int r_type, unsigned int *r_data)
  45. {
  46. struct mmc_cmd cmd;
  47. int ret;
  48. zeromem(&cmd, sizeof(struct mmc_cmd));
  49. cmd.cmd_idx = idx;
  50. cmd.cmd_arg = arg;
  51. cmd.resp_type = r_type;
  52. ret = ops->send_cmd(&cmd);
  53. if ((ret == 0) && (r_data != NULL)) {
  54. int i;
  55. for (i = 0; i < 4; i++) {
  56. r_data[i] = cmd.resp_data[i];
  57. }
  58. }
  59. if (ret != 0) {
  60. VERBOSE("Send command %u error: %d\n", idx, ret);
  61. }
  62. return ret;
  63. }
  64. static int mmc_device_state(void)
  65. {
  66. int retries = MMC_DEFAULT_MAX_RETRIES;
  67. unsigned int resp_data[4] = {0};
  68. do {
  69. int ret;
  70. if (retries == 0) {
  71. ERROR("CMD13 failed after %d retries\n",
  72. MMC_DEFAULT_MAX_RETRIES);
  73. return -EIO;
  74. }
  75. ret = mmc_send_cmd(MMC_CMD(13), rca << RCA_SHIFT_OFFSET,
  76. MMC_RESPONSE_R1, &resp_data[0]);
  77. if (ret != 0) {
  78. retries--;
  79. continue;
  80. }
  81. if ((resp_data[0] & STATUS_SWITCH_ERROR) != 0U) {
  82. return -EIO;
  83. }
  84. retries--;
  85. } while ((resp_data[0] & STATUS_READY_FOR_DATA) == 0U);
  86. return MMC_GET_STATE(resp_data[0]);
  87. }
  88. static int mmc_send_part_switch_cmd(unsigned char part_config)
  89. {
  90. int ret;
  91. unsigned int part_time = 0;
  92. ret = mmc_send_cmd(MMC_CMD(6),
  93. EXTCSD_WRITE_BYTES |
  94. EXTCSD_CMD(CMD_EXTCSD_PARTITION_CONFIG) |
  95. EXTCSD_VALUE(part_config) |
  96. EXTCSD_CMD_SET_NORMAL,
  97. MMC_RESPONSE_R1B, NULL);
  98. if (ret != 0) {
  99. return ret;
  100. }
  101. /* Partition switch timing is in 10ms units */
  102. part_time = mmc_ext_csd[CMD_EXTCSD_PART_SWITCH_TIME] * 10;
  103. mdelay(part_time);
  104. do {
  105. ret = mmc_device_state();
  106. if (ret < 0) {
  107. return ret;
  108. }
  109. } while (ret == MMC_STATE_PRG);
  110. return 0;
  111. }
  112. static int mmc_set_ext_csd(unsigned int ext_cmd, unsigned int value)
  113. {
  114. int ret;
  115. ret = mmc_send_cmd(MMC_CMD(6),
  116. EXTCSD_WRITE_BYTES | EXTCSD_CMD(ext_cmd) |
  117. EXTCSD_VALUE(value) | EXTCSD_CMD_SET_NORMAL,
  118. MMC_RESPONSE_R1B, NULL);
  119. if (ret != 0) {
  120. return ret;
  121. }
  122. do {
  123. ret = mmc_device_state();
  124. if (ret < 0) {
  125. return ret;
  126. }
  127. } while (ret == MMC_STATE_PRG);
  128. return 0;
  129. }
  130. static int mmc_sd_switch(unsigned int bus_width)
  131. {
  132. int ret;
  133. int retries = MMC_DEFAULT_MAX_RETRIES;
  134. unsigned int bus_width_arg = 0;
  135. ret = ops->prepare(0, (uintptr_t)&scr, sizeof(scr));
  136. if (ret != 0) {
  137. return ret;
  138. }
  139. /* CMD55: Application Specific Command */
  140. ret = mmc_send_cmd(MMC_CMD(55), rca << RCA_SHIFT_OFFSET,
  141. MMC_RESPONSE_R5, NULL);
  142. if (ret != 0) {
  143. return ret;
  144. }
  145. /* ACMD51: SEND_SCR */
  146. do {
  147. ret = mmc_send_cmd(MMC_ACMD(51), 0, MMC_RESPONSE_R1, NULL);
  148. if ((ret != 0) && (retries == 0)) {
  149. ERROR("ACMD51 failed after %d retries (ret=%d)\n",
  150. MMC_DEFAULT_MAX_RETRIES, ret);
  151. return ret;
  152. }
  153. retries--;
  154. } while (ret != 0);
  155. ret = ops->read(0, (uintptr_t)&scr, sizeof(scr));
  156. if (ret != 0) {
  157. return ret;
  158. }
  159. if (((scr[0] & SD_SCR_BUS_WIDTH_4) != 0U) &&
  160. (bus_width == MMC_BUS_WIDTH_4)) {
  161. bus_width_arg = 2;
  162. }
  163. /* CMD55: Application Specific Command */
  164. ret = mmc_send_cmd(MMC_CMD(55), rca << RCA_SHIFT_OFFSET,
  165. MMC_RESPONSE_R5, NULL);
  166. if (ret != 0) {
  167. return ret;
  168. }
  169. /* ACMD6: SET_BUS_WIDTH */
  170. ret = mmc_send_cmd(MMC_ACMD(6), bus_width_arg, MMC_RESPONSE_R1, NULL);
  171. if (ret != 0) {
  172. return ret;
  173. }
  174. do {
  175. ret = mmc_device_state();
  176. if (ret < 0) {
  177. return ret;
  178. }
  179. } while (ret == MMC_STATE_PRG);
  180. return 0;
  181. }
  182. static int mmc_set_ios(unsigned int clk, unsigned int bus_width)
  183. {
  184. int ret;
  185. unsigned int width = bus_width;
  186. if (mmc_dev_info->mmc_dev_type != MMC_IS_EMMC) {
  187. if (width == MMC_BUS_WIDTH_8) {
  188. WARN("Wrong bus config for SD-card, force to 4\n");
  189. width = MMC_BUS_WIDTH_4;
  190. }
  191. ret = mmc_sd_switch(width);
  192. if (ret != 0) {
  193. return ret;
  194. }
  195. } else if (mmc_csd.spec_vers == 4U) {
  196. ret = mmc_set_ext_csd(CMD_EXTCSD_BUS_WIDTH,
  197. (unsigned int)width);
  198. if (ret != 0) {
  199. return ret;
  200. }
  201. } else {
  202. VERBOSE("Wrong MMC type or spec version\n");
  203. }
  204. return ops->set_ios(clk, width);
  205. }
  206. static int mmc_fill_device_info(void)
  207. {
  208. unsigned long long c_size;
  209. unsigned int speed_idx;
  210. unsigned int nb_blocks;
  211. unsigned int freq_unit;
  212. int ret = 0;
  213. struct mmc_csd_sd_v2 *csd_sd_v2;
  214. switch (mmc_dev_info->mmc_dev_type) {
  215. case MMC_IS_EMMC:
  216. mmc_dev_info->block_size = MMC_BLOCK_SIZE;
  217. ret = ops->prepare(0, (uintptr_t)&mmc_ext_csd,
  218. sizeof(mmc_ext_csd));
  219. if (ret != 0) {
  220. return ret;
  221. }
  222. /* MMC CMD8: SEND_EXT_CSD */
  223. ret = mmc_send_cmd(MMC_CMD(8), 0, MMC_RESPONSE_R1, NULL);
  224. if (ret != 0) {
  225. return ret;
  226. }
  227. ret = ops->read(0, (uintptr_t)&mmc_ext_csd,
  228. sizeof(mmc_ext_csd));
  229. if (ret != 0) {
  230. return ret;
  231. }
  232. do {
  233. ret = mmc_device_state();
  234. if (ret < 0) {
  235. return ret;
  236. }
  237. } while (ret != MMC_STATE_TRAN);
  238. nb_blocks = (mmc_ext_csd[CMD_EXTCSD_SEC_CNT] << 0) |
  239. (mmc_ext_csd[CMD_EXTCSD_SEC_CNT + 1] << 8) |
  240. (mmc_ext_csd[CMD_EXTCSD_SEC_CNT + 2] << 16) |
  241. (mmc_ext_csd[CMD_EXTCSD_SEC_CNT + 3] << 24);
  242. mmc_dev_info->device_size = (unsigned long long)nb_blocks *
  243. mmc_dev_info->block_size;
  244. break;
  245. case MMC_IS_SD:
  246. /*
  247. * Use the same mmc_csd struct, as required fields here
  248. * (READ_BL_LEN, C_SIZE, CSIZE_MULT) are common with eMMC.
  249. */
  250. mmc_dev_info->block_size = BIT_32(mmc_csd.read_bl_len);
  251. c_size = ((unsigned long long)mmc_csd.c_size_high << 2U) |
  252. (unsigned long long)mmc_csd.c_size_low;
  253. assert(c_size != 0xFFFU);
  254. mmc_dev_info->device_size = (c_size + 1U) *
  255. BIT_64(mmc_csd.c_size_mult + 2U) *
  256. mmc_dev_info->block_size;
  257. break;
  258. case MMC_IS_SD_HC:
  259. assert(mmc_csd.csd_structure == 1U);
  260. mmc_dev_info->block_size = MMC_BLOCK_SIZE;
  261. /* Need to use mmc_csd_sd_v2 struct */
  262. csd_sd_v2 = (struct mmc_csd_sd_v2 *)&mmc_csd;
  263. c_size = ((unsigned long long)csd_sd_v2->c_size_high << 16) |
  264. (unsigned long long)csd_sd_v2->c_size_low;
  265. mmc_dev_info->device_size = (c_size + 1U) << MULT_BY_512K_SHIFT;
  266. break;
  267. default:
  268. ret = -EINVAL;
  269. break;
  270. }
  271. if (ret < 0) {
  272. return ret;
  273. }
  274. speed_idx = (mmc_csd.tran_speed & CSD_TRAN_SPEED_MULT_MASK) >>
  275. CSD_TRAN_SPEED_MULT_SHIFT;
  276. assert(speed_idx > 0U);
  277. if (mmc_dev_info->mmc_dev_type == MMC_IS_EMMC) {
  278. mmc_dev_info->max_bus_freq = tran_speed_base[speed_idx];
  279. } else {
  280. mmc_dev_info->max_bus_freq = sd_tran_speed_base[speed_idx];
  281. }
  282. freq_unit = mmc_csd.tran_speed & CSD_TRAN_SPEED_UNIT_MASK;
  283. while (freq_unit != 0U) {
  284. mmc_dev_info->max_bus_freq *= 10U;
  285. --freq_unit;
  286. }
  287. mmc_dev_info->max_bus_freq *= 10000U;
  288. return 0;
  289. }
  290. static int sd_switch(unsigned int mode, unsigned char group,
  291. unsigned char func)
  292. {
  293. unsigned int group_shift = (group - 1U) * 4U;
  294. unsigned int group_mask = GENMASK(group_shift + 3U, group_shift);
  295. unsigned int arg;
  296. int ret;
  297. ret = ops->prepare(0, (uintptr_t)&sd_switch_func_status,
  298. sizeof(sd_switch_func_status));
  299. if (ret != 0) {
  300. return ret;
  301. }
  302. /* MMC CMD6: SWITCH_FUNC */
  303. arg = mode | SD_SWITCH_ALL_GROUPS_MASK;
  304. arg &= ~group_mask;
  305. arg |= func << group_shift;
  306. ret = mmc_send_cmd(MMC_CMD(6), arg, MMC_RESPONSE_R1, NULL);
  307. if (ret != 0) {
  308. return ret;
  309. }
  310. return ops->read(0, (uintptr_t)&sd_switch_func_status,
  311. sizeof(sd_switch_func_status));
  312. }
  313. static int sd_send_op_cond(void)
  314. {
  315. int n;
  316. unsigned int resp_data[4];
  317. for (n = 0; n < SEND_OP_COND_MAX_RETRIES; n++) {
  318. int ret;
  319. /* CMD55: Application Specific Command */
  320. ret = mmc_send_cmd(MMC_CMD(55), 0, MMC_RESPONSE_R1, NULL);
  321. if (ret != 0) {
  322. return ret;
  323. }
  324. /* ACMD41: SD_SEND_OP_COND */
  325. ret = mmc_send_cmd(MMC_ACMD(41), OCR_HCS |
  326. mmc_dev_info->ocr_voltage, MMC_RESPONSE_R3,
  327. &resp_data[0]);
  328. if (ret != 0) {
  329. return ret;
  330. }
  331. if ((resp_data[0] & OCR_POWERUP) != 0U) {
  332. mmc_ocr_value = resp_data[0];
  333. if ((mmc_ocr_value & OCR_HCS) != 0U) {
  334. mmc_dev_info->mmc_dev_type = MMC_IS_SD_HC;
  335. } else {
  336. mmc_dev_info->mmc_dev_type = MMC_IS_SD;
  337. }
  338. return 0;
  339. }
  340. mdelay(10);
  341. }
  342. ERROR("ACMD41 failed after %d retries\n", SEND_OP_COND_MAX_RETRIES);
  343. return -EIO;
  344. }
  345. static int mmc_reset_to_idle(void)
  346. {
  347. int ret;
  348. /* CMD0: reset to IDLE */
  349. ret = mmc_send_cmd(MMC_CMD(0), 0, 0, NULL);
  350. if (ret != 0) {
  351. return ret;
  352. }
  353. mdelay(2);
  354. return 0;
  355. }
  356. static int mmc_send_op_cond(void)
  357. {
  358. int ret, n;
  359. unsigned int resp_data[4];
  360. for (n = 0; n < SEND_OP_COND_MAX_RETRIES; n++) {
  361. ret = mmc_send_cmd(MMC_CMD(1), OCR_SECTOR_MODE |
  362. OCR_VDD_MIN_2V7 | OCR_VDD_MIN_1V7,
  363. MMC_RESPONSE_R3, &resp_data[0]);
  364. if (ret != 0) {
  365. return ret;
  366. }
  367. if ((resp_data[0] & OCR_POWERUP) != 0U) {
  368. mmc_ocr_value = resp_data[0];
  369. return 0;
  370. }
  371. mdelay(10);
  372. }
  373. ERROR("CMD1 failed after %d retries\n", SEND_OP_COND_MAX_RETRIES);
  374. return -EIO;
  375. }
  376. static int mmc_enumerate(unsigned int clk, unsigned int bus_width)
  377. {
  378. int ret;
  379. unsigned int resp_data[4];
  380. ops->init();
  381. ret = mmc_reset_to_idle();
  382. if (ret != 0) {
  383. return ret;
  384. }
  385. if (mmc_dev_info->mmc_dev_type == MMC_IS_EMMC) {
  386. ret = mmc_send_op_cond();
  387. } else {
  388. /* CMD8: Send Interface Condition Command */
  389. ret = mmc_send_cmd(MMC_CMD(8), VHS_2_7_3_6_V | CMD8_CHECK_PATTERN,
  390. MMC_RESPONSE_R5, &resp_data[0]);
  391. if ((ret == 0) && ((resp_data[0] & 0xffU) == CMD8_CHECK_PATTERN)) {
  392. ret = sd_send_op_cond();
  393. }
  394. }
  395. if (ret != 0) {
  396. return ret;
  397. }
  398. /* CMD2: Card Identification */
  399. ret = mmc_send_cmd(MMC_CMD(2), 0, MMC_RESPONSE_R2, NULL);
  400. if (ret != 0) {
  401. return ret;
  402. }
  403. /* CMD3: Set Relative Address */
  404. if (mmc_dev_info->mmc_dev_type == MMC_IS_EMMC) {
  405. rca = MMC_FIX_RCA;
  406. ret = mmc_send_cmd(MMC_CMD(3), rca << RCA_SHIFT_OFFSET,
  407. MMC_RESPONSE_R1, NULL);
  408. if (ret != 0) {
  409. return ret;
  410. }
  411. } else {
  412. ret = mmc_send_cmd(MMC_CMD(3), 0,
  413. MMC_RESPONSE_R6, &resp_data[0]);
  414. if (ret != 0) {
  415. return ret;
  416. }
  417. rca = (resp_data[0] & 0xFFFF0000U) >> 16;
  418. }
  419. /* CMD9: CSD Register */
  420. ret = mmc_send_cmd(MMC_CMD(9), rca << RCA_SHIFT_OFFSET,
  421. MMC_RESPONSE_R2, &resp_data[0]);
  422. if (ret != 0) {
  423. return ret;
  424. }
  425. memcpy(&mmc_csd, &resp_data, sizeof(resp_data));
  426. /* CMD7: Select Card */
  427. ret = mmc_send_cmd(MMC_CMD(7), rca << RCA_SHIFT_OFFSET,
  428. MMC_RESPONSE_R1, NULL);
  429. if (ret != 0) {
  430. return ret;
  431. }
  432. do {
  433. ret = mmc_device_state();
  434. if (ret < 0) {
  435. return ret;
  436. }
  437. } while (ret != MMC_STATE_TRAN);
  438. ret = mmc_set_ios(clk, bus_width);
  439. if (ret != 0) {
  440. return ret;
  441. }
  442. ret = mmc_fill_device_info();
  443. if (ret != 0) {
  444. return ret;
  445. }
  446. if (is_sd_cmd6_enabled() &&
  447. (mmc_dev_info->mmc_dev_type == MMC_IS_SD_HC)) {
  448. /* Try to switch to High Speed Mode */
  449. ret = sd_switch(SD_SWITCH_FUNC_CHECK, 1U, 1U);
  450. if (ret != 0) {
  451. return ret;
  452. }
  453. if ((sd_switch_func_status.support_g1 & BIT(9)) == 0U) {
  454. /* High speed not supported, keep default speed */
  455. return 0;
  456. }
  457. ret = sd_switch(SD_SWITCH_FUNC_SWITCH, 1U, 1U);
  458. if (ret != 0) {
  459. return ret;
  460. }
  461. if ((sd_switch_func_status.sel_g2_g1 & 0x1U) == 0U) {
  462. /* Cannot switch to high speed, keep default speed */
  463. return 0;
  464. }
  465. mmc_dev_info->max_bus_freq = 50000000U;
  466. ret = ops->set_ios(clk, bus_width);
  467. }
  468. return ret;
  469. }
  470. size_t mmc_read_blocks(int lba, uintptr_t buf, size_t size)
  471. {
  472. int ret;
  473. unsigned int cmd_idx, cmd_arg;
  474. assert((ops != NULL) &&
  475. (ops->read != NULL) &&
  476. (size != 0U) &&
  477. ((size & MMC_BLOCK_MASK) == 0U));
  478. ret = ops->prepare(lba, buf, size);
  479. if (ret != 0) {
  480. return 0;
  481. }
  482. if (is_cmd23_enabled()) {
  483. /* Set block count */
  484. ret = mmc_send_cmd(MMC_CMD(23), size / MMC_BLOCK_SIZE,
  485. MMC_RESPONSE_R1, NULL);
  486. if (ret != 0) {
  487. return 0;
  488. }
  489. cmd_idx = MMC_CMD(18);
  490. } else {
  491. if (size > MMC_BLOCK_SIZE) {
  492. cmd_idx = MMC_CMD(18);
  493. } else {
  494. cmd_idx = MMC_CMD(17);
  495. }
  496. }
  497. if (((mmc_ocr_value & OCR_ACCESS_MODE_MASK) == OCR_BYTE_MODE) &&
  498. (mmc_dev_info->mmc_dev_type != MMC_IS_SD_HC)) {
  499. cmd_arg = lba * MMC_BLOCK_SIZE;
  500. } else {
  501. cmd_arg = lba;
  502. }
  503. ret = mmc_send_cmd(cmd_idx, cmd_arg, MMC_RESPONSE_R1, NULL);
  504. if (ret != 0) {
  505. return 0;
  506. }
  507. ret = ops->read(lba, buf, size);
  508. if (ret != 0) {
  509. return 0;
  510. }
  511. /* Wait buffer empty */
  512. do {
  513. ret = mmc_device_state();
  514. if (ret < 0) {
  515. return 0;
  516. }
  517. } while ((ret != MMC_STATE_TRAN) && (ret != MMC_STATE_DATA));
  518. if (!is_cmd23_enabled() && (size > MMC_BLOCK_SIZE)) {
  519. ret = mmc_send_cmd(MMC_CMD(12), 0, MMC_RESPONSE_R1B, NULL);
  520. if (ret != 0) {
  521. return 0;
  522. }
  523. }
  524. return size;
  525. }
  526. size_t mmc_write_blocks(int lba, const uintptr_t buf, size_t size)
  527. {
  528. int ret;
  529. unsigned int cmd_idx, cmd_arg;
  530. assert((ops != NULL) &&
  531. (ops->write != NULL) &&
  532. (size != 0U) &&
  533. ((buf & MMC_BLOCK_MASK) == 0U) &&
  534. ((size & MMC_BLOCK_MASK) == 0U));
  535. ret = ops->prepare(lba, buf, size);
  536. if (ret != 0) {
  537. return 0;
  538. }
  539. if (is_cmd23_enabled()) {
  540. /* Set block count */
  541. ret = mmc_send_cmd(MMC_CMD(23), size / MMC_BLOCK_SIZE,
  542. MMC_RESPONSE_R1, NULL);
  543. if (ret != 0) {
  544. return 0;
  545. }
  546. cmd_idx = MMC_CMD(25);
  547. } else {
  548. if (size > MMC_BLOCK_SIZE) {
  549. cmd_idx = MMC_CMD(25);
  550. } else {
  551. cmd_idx = MMC_CMD(24);
  552. }
  553. }
  554. if ((mmc_ocr_value & OCR_ACCESS_MODE_MASK) == OCR_BYTE_MODE) {
  555. cmd_arg = lba * MMC_BLOCK_SIZE;
  556. } else {
  557. cmd_arg = lba;
  558. }
  559. ret = mmc_send_cmd(cmd_idx, cmd_arg, MMC_RESPONSE_R1, NULL);
  560. if (ret != 0) {
  561. return 0;
  562. }
  563. ret = ops->write(lba, buf, size);
  564. if (ret != 0) {
  565. return 0;
  566. }
  567. /* Wait buffer empty */
  568. do {
  569. ret = mmc_device_state();
  570. if (ret < 0) {
  571. return 0;
  572. }
  573. } while ((ret != MMC_STATE_TRAN) && (ret != MMC_STATE_RCV));
  574. if (!is_cmd23_enabled() && (size > MMC_BLOCK_SIZE)) {
  575. ret = mmc_send_cmd(MMC_CMD(12), 0, MMC_RESPONSE_R1B, NULL);
  576. if (ret != 0) {
  577. return 0;
  578. }
  579. }
  580. return size;
  581. }
  582. size_t mmc_erase_blocks(int lba, size_t size)
  583. {
  584. int ret;
  585. assert(ops != NULL);
  586. assert((size != 0U) && ((size & MMC_BLOCK_MASK) == 0U));
  587. ret = mmc_send_cmd(MMC_CMD(35), lba, MMC_RESPONSE_R1, NULL);
  588. if (ret != 0) {
  589. return 0;
  590. }
  591. ret = mmc_send_cmd(MMC_CMD(36), lba + (size / MMC_BLOCK_SIZE) - 1U,
  592. MMC_RESPONSE_R1, NULL);
  593. if (ret != 0) {
  594. return 0;
  595. }
  596. ret = mmc_send_cmd(MMC_CMD(38), lba, MMC_RESPONSE_R1B, NULL);
  597. if (ret != 0) {
  598. return 0;
  599. }
  600. do {
  601. ret = mmc_device_state();
  602. if (ret < 0) {
  603. return 0;
  604. }
  605. } while (ret != MMC_STATE_TRAN);
  606. return size;
  607. }
  608. static int mmc_part_switch(unsigned char part_type)
  609. {
  610. unsigned char part_config = mmc_ext_csd[CMD_EXTCSD_PARTITION_CONFIG];
  611. part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
  612. part_config |= part_type;
  613. return mmc_send_part_switch_cmd(part_config);
  614. }
  615. static unsigned char mmc_current_boot_part(void)
  616. {
  617. return PART_CFG_CURRENT_BOOT_PARTITION(mmc_ext_csd[CMD_EXTCSD_PARTITION_CONFIG]);
  618. }
  619. int mmc_part_switch_current_boot(void)
  620. {
  621. unsigned char current_boot_part = mmc_current_boot_part();
  622. int ret;
  623. if ((current_boot_part != 1U) && (current_boot_part != 2U)) {
  624. ERROR("Got unexpected value for active boot partition, %u\n", current_boot_part);
  625. return -EIO;
  626. }
  627. ret = mmc_part_switch(current_boot_part);
  628. if (ret < 0) {
  629. ERROR("Failed to switch to boot partition, %d\n", ret);
  630. }
  631. return ret;
  632. }
  633. int mmc_part_switch_user(void)
  634. {
  635. int ret;
  636. ret = mmc_part_switch(PART_CFG_BOOT_PARTITION_NO_ACCESS);
  637. if (ret < 0) {
  638. ERROR("Failed to switch to user partition, %d\n", ret);
  639. }
  640. return ret;
  641. }
  642. size_t mmc_boot_part_size(void)
  643. {
  644. return mmc_ext_csd[CMD_EXTCSD_BOOT_SIZE_MULT] * SZ_128K;
  645. }
  646. size_t mmc_boot_part_read_blocks(int lba, uintptr_t buf, size_t size)
  647. {
  648. size_t size_read;
  649. int ret;
  650. ret = mmc_part_switch_current_boot();
  651. if (ret < 0) {
  652. return 0;
  653. }
  654. size_read = mmc_read_blocks(lba, buf, size);
  655. ret = mmc_part_switch_user();
  656. if (ret < 0) {
  657. return 0;
  658. }
  659. return size_read;
  660. }
  661. int mmc_init(const struct mmc_ops *ops_ptr, unsigned int clk,
  662. unsigned int width, unsigned int flags,
  663. struct mmc_device_info *device_info)
  664. {
  665. assert((ops_ptr != NULL) &&
  666. (ops_ptr->init != NULL) &&
  667. (ops_ptr->send_cmd != NULL) &&
  668. (ops_ptr->set_ios != NULL) &&
  669. (ops_ptr->prepare != NULL) &&
  670. (ops_ptr->read != NULL) &&
  671. (ops_ptr->write != NULL) &&
  672. (device_info != NULL) &&
  673. (clk != 0) &&
  674. ((width == MMC_BUS_WIDTH_1) ||
  675. (width == MMC_BUS_WIDTH_4) ||
  676. (width == MMC_BUS_WIDTH_8) ||
  677. (width == MMC_BUS_WIDTH_DDR_4) ||
  678. (width == MMC_BUS_WIDTH_DDR_8)));
  679. ops = ops_ptr;
  680. mmc_flags = flags;
  681. mmc_dev_info = device_info;
  682. return mmc_enumerate(clk, width);
  683. }