emmc_pboot_hal_memory_drv.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /*
  2. * Copyright (c) 2016 - 2020, Broadcom
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <string.h>
  7. #include <emmc_api.h>
  8. #include <cmn_plat_util.h>
  9. #define MAX_CMD_RETRY 10
  10. #if EMMC_USE_DMA
  11. #define USE_DMA 1
  12. #else
  13. #define USE_DMA 0
  14. #endif
  15. struct emmc_global_buffer emmc_global_buf;
  16. struct emmc_global_buffer *emmc_global_buf_ptr = &emmc_global_buf;
  17. struct emmc_global_vars emmc_global_vars;
  18. struct emmc_global_vars *emmc_global_vars_ptr = &emmc_global_vars;
  19. static struct sd_handle *sdio_gethandle(void);
  20. static uint32_t sdio_idle(struct sd_handle *p_sdhandle);
  21. static uint32_t sdio_read(struct sd_handle *p_sdhandle,
  22. uintptr_t mem_addr,
  23. uintptr_t storage_addr,
  24. size_t storage_size,
  25. size_t bytes_to_read);
  26. #ifdef INCLUDE_EMMC_DRIVER_WRITE_CODE
  27. static uint32_t sdio_write(struct sd_handle *p_sdhandle,
  28. uintptr_t mem_addr,
  29. uintptr_t data_addr,
  30. size_t bytes_to_write);
  31. #endif
  32. static struct sd_handle *sdio_init(void);
  33. static int32_t bcm_emmc_card_ready_state(struct sd_handle *p_sdhandle);
  34. static void init_globals(void)
  35. {
  36. memset((void *)emmc_global_buf_ptr, 0, sizeof(*emmc_global_buf_ptr));
  37. memset((void *)emmc_global_vars_ptr, 0, sizeof(*emmc_global_vars_ptr));
  38. }
  39. /*
  40. * This function is used to change partition
  41. */
  42. uint32_t emmc_partition_select(uint32_t partition)
  43. {
  44. int rc;
  45. struct sd_handle *sd_handle = sdio_gethandle();
  46. if (sd_handle->device == 0) {
  47. EMMC_TRACE("eMMC init is not done");
  48. return 0;
  49. }
  50. switch (partition) {
  51. case EMMC_BOOT_PARTITION1:
  52. rc = set_boot_config(sd_handle,
  53. SDIO_HW_EMMC_EXT_CSD_BOOT_ACC_BOOT1);
  54. EMMC_TRACE(
  55. "Change to Boot Partition 1 result:%d (0 means SD_OK)\n",
  56. rc);
  57. break;
  58. case EMMC_BOOT_PARTITION2:
  59. rc = set_boot_config(sd_handle,
  60. SDIO_HW_EMMC_EXT_CSD_BOOT_ACC_BOOT2);
  61. EMMC_TRACE(
  62. "Change to Boot Partition 2 result:%d (0 means SD_OK)\n",
  63. rc);
  64. break;
  65. case EMMC_USE_CURRENT_PARTITION:
  66. rc = SD_OK;
  67. EMMC_TRACE("Stay on current partition");
  68. break;
  69. case EMMC_USER_AREA:
  70. default:
  71. rc = set_boot_config(sd_handle,
  72. SDIO_HW_EMMC_EXT_CSD_BOOT_ACC_USER);
  73. EMMC_TRACE("Change to User area result:%d (0 means SD_OK)\n",
  74. rc);
  75. break;
  76. }
  77. return (rc == SD_OK);
  78. }
  79. /*
  80. * Initialize emmc controller for eMMC
  81. * Returns 0 on fail condition
  82. */
  83. uint32_t bcm_emmc_init(bool card_rdy_only)
  84. {
  85. struct sd_handle *p_sdhandle;
  86. uint32_t result = 0;
  87. EMMC_TRACE("Enter emmc_controller_init()\n");
  88. /* If eMMC is already initialized, skip init */
  89. if (emmc_global_vars_ptr->init_done)
  90. return 1;
  91. init_globals();
  92. p_sdhandle = sdio_init();
  93. if (p_sdhandle == NULL) {
  94. ERROR("eMMC init failed");
  95. return result;
  96. }
  97. if (card_rdy_only) {
  98. /* Put the card in Ready state, Not complete init */
  99. result = bcm_emmc_card_ready_state(p_sdhandle);
  100. return !result;
  101. }
  102. if (sdio_idle(p_sdhandle) == EMMC_BOOT_OK) {
  103. set_config(p_sdhandle, SD_NORMAL_SPEED, MAX_CMD_RETRY, USE_DMA,
  104. SD_DMA_BOUNDARY_256K, EMMC_BLOCK_SIZE,
  105. EMMC_WFE_RETRY);
  106. if (!select_blk_sz(p_sdhandle,
  107. p_sdhandle->device->cfg.blockSize)) {
  108. emmc_global_vars_ptr->init_done = 1;
  109. result = 1;
  110. } else {
  111. ERROR("Select Block Size failed\n");
  112. }
  113. } else {
  114. ERROR("eMMC init failed");
  115. }
  116. /* Initialization is failed, so deinit HW setting */
  117. if (result == 0)
  118. emmc_deinit();
  119. return result;
  120. }
  121. /*
  122. * Function to de-init SDIO controller for eMMC
  123. */
  124. void emmc_deinit(void)
  125. {
  126. emmc_global_vars_ptr->init_done = 0;
  127. emmc_global_vars_ptr->sdHandle.card = 0;
  128. emmc_global_vars_ptr->sdHandle.device = 0;
  129. }
  130. /*
  131. * Read eMMC memory
  132. * Returns read_size
  133. */
  134. uint32_t emmc_read(uintptr_t mem_addr, uintptr_t storage_addr,
  135. size_t storage_size, size_t bytes_to_read)
  136. {
  137. struct sd_handle *sd_handle = sdio_gethandle();
  138. if (sd_handle->device == 0) {
  139. EMMC_TRACE("eMMC init is not done");
  140. return 0;
  141. }
  142. return sdio_read(sdio_gethandle(), mem_addr, storage_addr,
  143. storage_size, bytes_to_read);
  144. }
  145. #ifdef INCLUDE_EMMC_DRIVER_ERASE_CODE
  146. #define EXT_CSD_ERASE_GRP_SIZE 224
  147. static int emmc_block_erase(uintptr_t mem_addr, size_t blocks)
  148. {
  149. struct sd_handle *sd_handle = sdio_gethandle();
  150. if (sd_handle->device == 0) {
  151. ERROR("eMMC init is not done");
  152. return -1;
  153. }
  154. return erase_card(sdio_gethandle(), mem_addr, blocks);
  155. }
  156. int emmc_erase(uintptr_t mem_addr, size_t num_of_blocks, uint32_t partition)
  157. {
  158. int err = 0;
  159. size_t block_count = 0, blocks = 0;
  160. size_t erase_group = 0;
  161. erase_group =
  162. emmc_global_buf_ptr->u.Ext_CSD_storage[EXT_CSD_ERASE_GRP_SIZE]*1024;
  163. INFO("eMMC Erase Group Size=0x%lx\n", erase_group);
  164. emmc_partition_select(partition);
  165. while (block_count < num_of_blocks) {
  166. blocks = ((num_of_blocks - block_count) > erase_group) ?
  167. erase_group : (num_of_blocks - block_count);
  168. err = emmc_block_erase(mem_addr + block_count, blocks);
  169. if (err)
  170. break;
  171. block_count += blocks;
  172. }
  173. if (err == 0)
  174. INFO("eMMC Erase of partition %d successful\n", partition);
  175. else
  176. ERROR("eMMC Erase of partition %d Failed(%i)\n", partition, err);
  177. return err;
  178. }
  179. #endif
  180. #ifdef INCLUDE_EMMC_DRIVER_WRITE_CODE
  181. /*
  182. * Write to eMMC memory
  183. * Returns written_size
  184. */
  185. uint32_t emmc_write(uintptr_t mem_addr, uintptr_t data_addr,
  186. size_t bytes_to_write)
  187. {
  188. struct sd_handle *sd_handle = sdio_gethandle();
  189. if (sd_handle->device == 0) {
  190. EMMC_TRACE("eMMC init is not done");
  191. return 0;
  192. }
  193. return sdio_write(sd_handle, mem_addr, data_addr, bytes_to_write);
  194. }
  195. #endif
  196. /*
  197. * Send SDIO Cmd
  198. * Return 0 for pass condition
  199. */
  200. uint32_t send_sdio_cmd(uint32_t cmdIndex, uint32_t argument,
  201. uint32_t options, struct sd_resp *resp)
  202. {
  203. struct sd_handle *sd_handle = sdio_gethandle();
  204. if (sd_handle->device == 0) {
  205. EMMC_TRACE("eMMC init is not done");
  206. return 1;
  207. }
  208. return send_cmd(sd_handle, cmdIndex, argument, options, resp);
  209. }
  210. /*
  211. * This function return SDIO handle
  212. */
  213. struct sd_handle *sdio_gethandle(void)
  214. {
  215. return &emmc_global_vars_ptr->sdHandle;
  216. }
  217. /*
  218. * Initialize SDIO controller
  219. */
  220. struct sd_handle *sdio_init(void)
  221. {
  222. uint32_t SDIO_base;
  223. struct sd_handle *p_sdhandle = &emmc_global_vars_ptr->sdHandle;
  224. SDIO_base = EMMC_CTRL_REGS_BASE_ADDR;
  225. if (SDIO_base == SDIO0_EMMCSDXC_SYSADDR) {
  226. EMMC_TRACE(" ---> for SDIO 0 Controller\n\n");
  227. }
  228. memset(p_sdhandle, 0, sizeof(struct sd_handle));
  229. p_sdhandle->device = &emmc_global_vars_ptr->sdDevice;
  230. p_sdhandle->card = &emmc_global_vars_ptr->sdCard;
  231. memset(p_sdhandle->device, 0, sizeof(struct sd_dev));
  232. memset(p_sdhandle->card, 0, sizeof(struct sd_card_info));
  233. if (chal_sd_start((CHAL_HANDLE *) p_sdhandle->device,
  234. SD_PIO_MODE, SDIO_base, SDIO_base) != SD_OK) {
  235. return NULL;
  236. }
  237. set_config(p_sdhandle, SD_NORMAL_SPEED, MAX_CMD_RETRY, SD_DMA_OFF,
  238. SD_DMA_BOUNDARY_4K, EMMC_BLOCK_SIZE, EMMC_WFE_RETRY);
  239. return &emmc_global_vars_ptr->sdHandle;
  240. }
  241. uint32_t sdio_idle(struct sd_handle *p_sdhandle)
  242. {
  243. reset_card(p_sdhandle);
  244. SD_US_DELAY(1000);
  245. if (init_card(p_sdhandle, SD_CARD_DETECT_MMC) != SD_OK) {
  246. reset_card(p_sdhandle);
  247. reset_host_ctrl(p_sdhandle);
  248. return EMMC_BOOT_NO_CARD;
  249. }
  250. return EMMC_BOOT_OK;
  251. }
  252. /*
  253. * This function read eMMC
  254. */
  255. uint32_t sdio_read(struct sd_handle *p_sdhandle,
  256. uintptr_t mem_addr,
  257. uintptr_t storage_addr,
  258. size_t storage_size, size_t bytes_to_read)
  259. {
  260. uint32_t offset = 0, blockAddr, readLen = 0, rdCount;
  261. uint32_t remSize, manual_copy_size;
  262. uint8_t *outputBuf = (uint8_t *) storage_addr;
  263. const size_t blockSize = p_sdhandle->device->cfg.blockSize;
  264. VERBOSE("EMMC READ: dst=0x%lx, src=0x%lx, size=0x%lx\n",
  265. storage_addr, mem_addr, bytes_to_read);
  266. if (storage_size < bytes_to_read) {
  267. /* Don't have sufficient storage to complete the operation */
  268. return 0;
  269. }
  270. /* Range check non high capacity memory */
  271. if ((p_sdhandle->device->ctrl.ocr & SD_CARD_HIGH_CAPACITY) == 0) {
  272. if (mem_addr > 0x80000000) {
  273. return 0;
  274. }
  275. }
  276. /* High capacity card use block address mode */
  277. if (p_sdhandle->device->ctrl.ocr & SD_CARD_HIGH_CAPACITY) {
  278. blockAddr = (uint32_t) (mem_addr / blockSize);
  279. offset = (uint32_t) (mem_addr - (blockAddr * blockSize));
  280. } else {
  281. blockAddr = (uint32_t) (mem_addr / blockSize) * blockSize;
  282. offset = (uint32_t) (mem_addr - blockAddr);
  283. }
  284. remSize = bytes_to_read;
  285. rdCount = 0;
  286. /* Process first unaligned block of MAX_READ_LENGTH */
  287. if (offset > 0) {
  288. if (!read_block(p_sdhandle, emmc_global_buf_ptr->u.tempbuf,
  289. blockAddr, SD_MAX_READ_LENGTH)) {
  290. if (remSize < (blockSize - offset)) {
  291. rdCount += remSize;
  292. manual_copy_size = remSize;
  293. remSize = 0; /* read is done */
  294. } else {
  295. remSize -= (blockSize - offset);
  296. rdCount += (blockSize - offset);
  297. manual_copy_size = blockSize - offset;
  298. }
  299. /* Check for overflow */
  300. if (manual_copy_size > storage_size ||
  301. (((uintptr_t)outputBuf + manual_copy_size) >
  302. (storage_addr + storage_size))) {
  303. ERROR("EMMC READ: Overflow 1\n");
  304. return 0;
  305. }
  306. memcpy(outputBuf,
  307. (void *)((uintptr_t)
  308. (emmc_global_buf_ptr->u.tempbuf + offset)),
  309. manual_copy_size);
  310. /* Update Physical address */
  311. outputBuf += manual_copy_size;
  312. if (p_sdhandle->device->ctrl.ocr & SD_CARD_HIGH_CAPACITY) {
  313. blockAddr++;
  314. } else {
  315. blockAddr += blockSize;
  316. }
  317. } else {
  318. return 0;
  319. }
  320. }
  321. while (remSize >= blockSize) {
  322. if (remSize >= SD_MAX_BLK_TRANSFER_LENGTH) {
  323. readLen = SD_MAX_BLK_TRANSFER_LENGTH;
  324. } else {
  325. readLen = (remSize / blockSize) * blockSize;
  326. }
  327. /* Check for overflow */
  328. if ((rdCount + readLen) > storage_size ||
  329. (((uintptr_t) outputBuf + readLen) >
  330. (storage_addr + storage_size))) {
  331. ERROR("EMMC READ: Overflow\n");
  332. return 0;
  333. }
  334. if (!read_block(p_sdhandle, outputBuf, blockAddr, readLen)) {
  335. if (p_sdhandle->device->ctrl.ocr & SD_CARD_HIGH_CAPACITY) {
  336. blockAddr += (readLen / blockSize);
  337. } else {
  338. blockAddr += readLen;
  339. }
  340. remSize -= readLen;
  341. rdCount += readLen;
  342. /* Update Physical address */
  343. outputBuf += readLen;
  344. } else {
  345. return 0;
  346. }
  347. }
  348. /* process the last unaligned block reading */
  349. if (remSize > 0) {
  350. if (!read_block(p_sdhandle, emmc_global_buf_ptr->u.tempbuf,
  351. blockAddr, SD_MAX_READ_LENGTH)) {
  352. rdCount += remSize;
  353. /* Check for overflow */
  354. if (rdCount > storage_size ||
  355. (((uintptr_t) outputBuf + remSize) >
  356. (storage_addr + storage_size))) {
  357. ERROR("EMMC READ: Overflow\n");
  358. return 0;
  359. }
  360. memcpy(outputBuf,
  361. emmc_global_buf_ptr->u.tempbuf, remSize);
  362. /* Update Physical address */
  363. outputBuf += remSize;
  364. } else {
  365. rdCount = 0;
  366. }
  367. }
  368. return rdCount;
  369. }
  370. #ifdef INCLUDE_EMMC_DRIVER_WRITE_CODE
  371. static uint32_t sdio_write(struct sd_handle *p_sdhandle, uintptr_t mem_addr,
  372. uintptr_t data_addr, size_t bytes_to_write)
  373. {
  374. uint32_t offset, blockAddr, writeLen, wtCount = 0;
  375. uint32_t remSize, manual_copy_size = 0;
  376. uint8_t *inputBuf = (uint8_t *)data_addr;
  377. /* range check non high capacity memory */
  378. if ((p_sdhandle->device->ctrl.ocr & SD_CARD_HIGH_CAPACITY) == 0) {
  379. if (mem_addr > 0x80000000) {
  380. return 0;
  381. }
  382. }
  383. /* the high capacity card use block address mode */
  384. if (p_sdhandle->device->ctrl.ocr & SD_CARD_HIGH_CAPACITY) {
  385. blockAddr =
  386. (uint32_t)(mem_addr / p_sdhandle->device->cfg.blockSize);
  387. offset =
  388. (uint32_t)(mem_addr -
  389. blockAddr * p_sdhandle->device->cfg.blockSize);
  390. } else {
  391. blockAddr =
  392. ((uint32_t)mem_addr / p_sdhandle->device->cfg.blockSize) *
  393. p_sdhandle->device->cfg.blockSize;
  394. offset = (uint32_t) mem_addr - blockAddr;
  395. }
  396. remSize = bytes_to_write;
  397. wtCount = 0;
  398. /* process first unaligned block */
  399. if (offset > 0) {
  400. if (!read_block(p_sdhandle, emmc_global_buf_ptr->u.tempbuf,
  401. blockAddr, p_sdhandle->device->cfg.blockSize)) {
  402. if (remSize <
  403. (p_sdhandle->device->cfg.blockSize - offset)) {
  404. manual_copy_size = remSize;
  405. } else {
  406. manual_copy_size =
  407. p_sdhandle->device->cfg.blockSize - offset;
  408. }
  409. memcpy((void *)((uintptr_t)
  410. (emmc_global_buf_ptr->u.tempbuf + offset)),
  411. inputBuf,
  412. manual_copy_size);
  413. /* Update Physical address */
  414. if (!write_block(p_sdhandle,
  415. emmc_global_buf_ptr->u.tempbuf,
  416. blockAddr,
  417. p_sdhandle->device->cfg.blockSize)) {
  418. if (remSize <
  419. (p_sdhandle->device->cfg.blockSize -
  420. offset)) {
  421. wtCount += remSize;
  422. manual_copy_size = remSize;
  423. remSize = 0; /* read is done */
  424. } else {
  425. remSize -=
  426. (p_sdhandle->device->cfg.blockSize -
  427. offset);
  428. wtCount +=
  429. (p_sdhandle->device->cfg.blockSize -
  430. offset);
  431. manual_copy_size =
  432. p_sdhandle->device->cfg.blockSize -
  433. offset;
  434. }
  435. inputBuf += manual_copy_size;
  436. if (p_sdhandle->device->ctrl.ocr &
  437. SD_CARD_HIGH_CAPACITY) {
  438. blockAddr++;
  439. } else {
  440. blockAddr +=
  441. p_sdhandle->device->cfg.blockSize;
  442. }
  443. } else
  444. return 0;
  445. } else {
  446. return 0;
  447. }
  448. }
  449. /* process block writing */
  450. while (remSize >= p_sdhandle->device->cfg.blockSize) {
  451. if (remSize >= SD_MAX_READ_LENGTH) {
  452. writeLen = SD_MAX_READ_LENGTH;
  453. } else {
  454. writeLen =
  455. (remSize / p_sdhandle->device->cfg.blockSize) *
  456. p_sdhandle->device->cfg.blockSize;
  457. }
  458. if (!write_block(p_sdhandle, inputBuf, blockAddr, writeLen)) {
  459. if (p_sdhandle->device->ctrl.ocr & SD_CARD_HIGH_CAPACITY)
  460. blockAddr +=
  461. (writeLen /
  462. p_sdhandle->device->cfg.blockSize);
  463. else
  464. blockAddr += writeLen;
  465. remSize -= writeLen;
  466. wtCount += writeLen;
  467. inputBuf += writeLen;
  468. } else {
  469. return 0;
  470. }
  471. }
  472. /* process the last unaligned block reading */
  473. if (remSize > 0) {
  474. if (!read_block(p_sdhandle,
  475. emmc_global_buf_ptr->u.tempbuf,
  476. blockAddr, p_sdhandle->device->cfg.blockSize)) {
  477. memcpy(emmc_global_buf_ptr->u.tempbuf,
  478. inputBuf, remSize);
  479. /* Update Physical address */
  480. if (!write_block(p_sdhandle,
  481. emmc_global_buf_ptr->u.tempbuf,
  482. blockAddr,
  483. p_sdhandle->device->cfg.blockSize)) {
  484. wtCount += remSize;
  485. inputBuf += remSize;
  486. } else {
  487. return 0;
  488. }
  489. } else {
  490. wtCount = 0;
  491. }
  492. }
  493. return wtCount;
  494. }
  495. #endif
  496. /*
  497. * Function to put the card in Ready state by sending CMD0 and CMD1
  498. */
  499. static int32_t bcm_emmc_card_ready_state(struct sd_handle *p_sdhandle)
  500. {
  501. int32_t result = 0;
  502. uint32_t argument = MMC_CMD_IDLE_RESET_ARG; /* Exit from Boot mode */
  503. if (p_sdhandle) {
  504. send_sdio_cmd(SD_CMD_GO_IDLE_STATE, argument, 0, NULL);
  505. result = reset_card(p_sdhandle);
  506. if (result != SD_OK) {
  507. EMMC_TRACE("eMMC Reset error\n");
  508. return SD_RESET_ERROR;
  509. }
  510. SD_US_DELAY(2000);
  511. result = mmc_cmd1(p_sdhandle);
  512. }
  513. return result;
  514. }