emmc_csl_sdcard.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. /*
  2. * Copyright (c) 2016 - 2020, Broadcom
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <inttypes.h>
  7. #include <stddef.h>
  8. #include <stdint.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <arch_helpers.h>
  12. #include <lib/mmio.h>
  13. #include "bcm_emmc.h"
  14. #include "emmc_chal_types.h"
  15. #include "emmc_csl_sdprot.h"
  16. #include "emmc_chal_sd.h"
  17. #include "emmc_csl_sdcmd.h"
  18. #include "emmc_csl_sd.h"
  19. #include "emmc_pboot_hal_memory_drv.h"
  20. #define SD_CARD_BUSY 0x80000000
  21. #define SD_CARD_RETRY_LIMIT 1000
  22. #define SD_CARD_HIGH_SPEED_PS 13
  23. #define SD_CHK_HIGH_SPEED_MODE 0x00FFFFF1
  24. #define SD_SET_HIGH_SPEED_MODE 0x80FFFFF1
  25. #define SD_MMC_ENABLE_HIGH_SPEED 0x03b90100 //0x03b90103
  26. #define SD_MMC_8BIT_MODE 0x03b70200
  27. #define SD_MMC_4BIT_MODE 0x03b70100
  28. #define SD_MMC_1BIT_MODE 0x03b70000
  29. #define SD_MMC_BOOT_8BIT_MODE 0x03b10200
  30. #define SD_MMC_BOOT_4BIT_MODE 0x03b10100
  31. #define SD_MMC_BOOT_1BIT_MODE 0x03b10000
  32. #define SDIO_HW_EMMC_EXT_CSD_BOOT_CNF 0X03B30000
  33. #ifdef USE_EMMC_FIP_TOC_CACHE
  34. /*
  35. * Cache size mirrors the size of the global eMMC temp buffer
  36. * which is used for non-image body reads such as headers, ToC etc.
  37. */
  38. #define CACHE_SIZE ((EMMC_BLOCK_SIZE) * 2)
  39. #define PARTITION_BLOCK_ADDR ((PLAT_FIP_ATTEMPT_OFFSET)/(EMMC_BLOCK_SIZE))
  40. static uint32_t cached_partition_block;
  41. static uint8_t cached_block[CACHE_SIZE];
  42. #endif
  43. static int set_card_data_width(struct sd_handle *handle, int width);
  44. static int abort_err(struct sd_handle *handle);
  45. static int err_recovery(struct sd_handle *handle, uint32_t errors);
  46. static int xfer_data(struct sd_handle *handle, uint32_t mode, uint32_t addr,
  47. uint32_t length, uint8_t *base);
  48. int set_boot_config(struct sd_handle *handle, uint32_t config)
  49. {
  50. return mmc_cmd6(handle, SDIO_HW_EMMC_EXT_CSD_BOOT_CNF | config);
  51. }
  52. void process_csd_mmc_speed(struct sd_handle *handle, uint32_t csd_mmc_speed)
  53. {
  54. uint32_t div_ctrl_setting;
  55. /* CSD field TRAN_SPEED:
  56. * Bits [2:0] 0 = 100 KHz
  57. * 1 = 1 MHz
  58. * 2 = 10 MHz
  59. * 3 = 100 MHz
  60. * 4...7 Reserved.
  61. * Bits [6:3] 0 = Reserved
  62. * 1 = 1.0
  63. * 2 = 1.2
  64. * 3 = 1.3
  65. * 4 = 1.5
  66. * 5 = 2.0
  67. * 6 = 2.6
  68. * 7 = 3.0
  69. * 8 = 3.5
  70. * 9 = 4.0
  71. * A = 4.5
  72. * B = 5.2
  73. * C = 5.5
  74. * D = 6.0
  75. * E = 7.0
  76. * F = 8.0
  77. * For cards supporting version 4.0, 4.1, and 4.2 of the standard,
  78. * the value shall be 20 MHz (0x2A).
  79. * For cards supporting version 4.3 , the value shall be 26 MHz (0x32)
  80. */
  81. switch (csd_mmc_speed & 0x7F) {
  82. case 0x2A:
  83. EMMC_TRACE("Speeding up eMMC clock to 20MHz\n");
  84. div_ctrl_setting =
  85. chal_sd_freq_2_div_ctrl_setting(20 * 1000 * 1000);
  86. break;
  87. case 0x32:
  88. EMMC_TRACE("Speeding up eMMC clock to 26MHz\n");
  89. div_ctrl_setting =
  90. chal_sd_freq_2_div_ctrl_setting(26 * 1000 * 1000);
  91. break;
  92. default:
  93. /* Unknown */
  94. return;
  95. }
  96. chal_sd_set_clock((CHAL_HANDLE *) handle->device, div_ctrl_setting, 0);
  97. chal_sd_set_clock((CHAL_HANDLE *) handle->device, div_ctrl_setting, 1);
  98. SD_US_DELAY(1000);
  99. }
  100. /*
  101. * The function changes SD/SDIO/MMC card data width if
  102. * the card support configurable data width. The host controller
  103. * and the card has to be in the same bus data width.
  104. */
  105. int set_card_data_width(struct sd_handle *handle, int width)
  106. {
  107. uint32_t data_width = 0;
  108. int is_valid_arg = 1;
  109. int rc = SD_FAIL;
  110. char *bitwidth_str = " ";
  111. char *result_str = "failed";
  112. switch (width) {
  113. #ifdef DRIVER_EMMC_ENABLE_DATA_WIDTH_8BIT
  114. case SD_BUS_DATA_WIDTH_8BIT:
  115. data_width = SD_MMC_8BIT_MODE;
  116. #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
  117. bitwidth_str = "8_BIT";
  118. #endif
  119. break;
  120. #endif
  121. case SD_BUS_DATA_WIDTH_4BIT:
  122. data_width = SD_MMC_4BIT_MODE;
  123. #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
  124. bitwidth_str = "4_BIT";
  125. #endif
  126. break;
  127. case SD_BUS_DATA_WIDTH_1BIT:
  128. data_width = SD_MMC_1BIT_MODE;
  129. #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
  130. bitwidth_str = "1_BIT";
  131. #endif
  132. break;
  133. default:
  134. is_valid_arg = 0;
  135. #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
  136. bitwidth_str = "unknown";
  137. #endif
  138. break;
  139. }
  140. if (is_valid_arg) {
  141. rc = mmc_cmd6(handle, data_width);
  142. if (rc == SD_OK) {
  143. #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
  144. result_str = "succeeded";
  145. #endif
  146. chal_sd_config_bus_width((CHAL_HANDLE *) handle->device,
  147. width);
  148. } else {
  149. #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
  150. result_str = "failed";
  151. #endif
  152. }
  153. } else {
  154. rc = SD_FAIL;
  155. #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
  156. result_str = "ignored";
  157. #endif
  158. }
  159. VERBOSE("SDIO Data Width(%s) %s.\n", bitwidth_str, result_str);
  160. return rc;
  161. }
  162. /*
  163. * Error handling routine. Does abort data
  164. * transmission if error is found.
  165. */
  166. static int abort_err(struct sd_handle *handle)
  167. {
  168. uint32_t present, options, event, rel = 0;
  169. struct sd_resp cmdRsp;
  170. handle->device->ctrl.argReg = 0;
  171. handle->device->ctrl.cmdIndex = SD_CMD_STOP_TRANSMISSION;
  172. options = (SD_CMD_STOP_TRANSMISSION << 24) |
  173. (SD_CMDR_RSP_TYPE_R1b_5b << SD_CMDR_RSP_TYPE_S) |
  174. SD4_EMMC_TOP_CMD_CRC_EN_MASK |
  175. SD4_EMMC_TOP_CMD_CCHK_EN_MASK;
  176. chal_sd_send_cmd((CHAL_HANDLE *) handle->device,
  177. handle->device->ctrl.cmdIndex,
  178. handle->device->ctrl.argReg, options);
  179. event = wait_for_event(handle,
  180. SD4_EMMC_TOP_INTR_CMDDONE_MASK |
  181. SD_ERR_INTERRUPTS,
  182. handle->device->cfg.wfe_retry);
  183. if (event & SD_CMD_ERROR_INT) {
  184. rel = SD_ERROR_NON_RECOVERABLE;
  185. } else {
  186. if (event & SD_DAT_TIMEOUT) {
  187. return SD_ERROR_NON_RECOVERABLE;
  188. }
  189. chal_sd_get_response((CHAL_HANDLE *) handle->device,
  190. (uint32_t *)&cmdRsp);
  191. process_cmd_response(handle, handle->device->ctrl.cmdIndex,
  192. cmdRsp.data.r2.rsp1, cmdRsp.data.r2.rsp2,
  193. cmdRsp.data.r2.rsp3, cmdRsp.data.r2.rsp4,
  194. &cmdRsp);
  195. SD_US_DELAY(2000);
  196. present =
  197. chal_sd_get_present_status((CHAL_HANDLE *) handle->device);
  198. if ((present & 0x00F00000) == 0x00F00000)
  199. rel = SD_ERROR_RECOVERABLE;
  200. else
  201. rel = SD_ERROR_NON_RECOVERABLE;
  202. }
  203. return rel;
  204. }
  205. /*
  206. * The function handles real data transmission on both DMA and
  207. * none DMA mode, In None DMA mode the data transfer starts
  208. * when the command is sent to the card, data has to be written
  209. * into the host controllers buffer at this time one block
  210. * at a time.
  211. * In DMA mode, the real data transfer is done by the DMA engine
  212. * and this functions just waits for the data transfer to complete.
  213. *
  214. */
  215. int process_data_xfer(struct sd_handle *handle, uint8_t *buffer, uint32_t addr,
  216. uint32_t length, int dir)
  217. {
  218. if (dir == SD_XFER_HOST_TO_CARD) {
  219. #ifdef INCLUDE_EMMC_DRIVER_WRITE_CODE
  220. if (handle->device->cfg.dma == SD_DMA_OFF) {
  221. /*
  222. * In NON DMA mode, the real data xfer starts from here
  223. */
  224. if (write_buffer(handle, length, buffer))
  225. return SD_WRITE_ERROR;
  226. } else {
  227. wait_for_event(handle,
  228. SD4_EMMC_TOP_INTR_TXDONE_MASK |
  229. SD_ERR_INTERRUPTS,
  230. handle->device->cfg.wfe_retry);
  231. if (handle->device->ctrl.cmdStatus == SD_OK)
  232. return SD_OK;
  233. check_error(handle, handle->device->ctrl.cmdStatus);
  234. return SD_WRITE_ERROR;
  235. }
  236. #else
  237. return SD_WRITE_ERROR;
  238. #endif
  239. } else { /* SD_XFER_CARD_TO_HOST */
  240. if (handle->device->cfg.dma == SD_DMA_OFF) {
  241. /* In NON DMA mode, the real data
  242. * transfer starts from here
  243. */
  244. if (read_buffer(handle, length, buffer))
  245. return SD_READ_ERROR;
  246. } else { /* for DMA mode */
  247. /*
  248. * once the data transmission is done
  249. * copy data to the host buffer.
  250. */
  251. wait_for_event(handle,
  252. SD4_EMMC_TOP_INTR_TXDONE_MASK |
  253. SD_ERR_INTERRUPTS,
  254. handle->device->cfg.wfe_retry);
  255. if (handle->device->ctrl.cmdStatus == SD_OK)
  256. return SD_OK;
  257. check_error(handle, handle->device->ctrl.cmdStatus);
  258. return SD_READ_ERROR;
  259. }
  260. }
  261. return SD_OK;
  262. }
  263. /*
  264. * The function sets block size for the next SD/SDIO/MMC
  265. * card read/write command.
  266. */
  267. int select_blk_sz(struct sd_handle *handle, uint16_t size)
  268. {
  269. return sd_cmd16(handle, size);
  270. }
  271. /*
  272. * The function initializes the SD/SDIO/MMC/CEATA and detects
  273. * the card according to the flag of detection.
  274. * Once this function is called, the card is put into ready state
  275. * so application can do data transfer to and from the card.
  276. */
  277. int init_card(struct sd_handle *handle, int detection)
  278. {
  279. /*
  280. * After Reset, eMMC comes up in 1 Bit Data Width by default.
  281. * Set host side to match.
  282. */
  283. chal_sd_config_bus_width((CHAL_HANDLE *) handle->device,
  284. SD_BUS_DATA_WIDTH_1BIT);
  285. #ifdef USE_EMMC_FIP_TOC_CACHE
  286. cached_partition_block = 0;
  287. #endif
  288. handle->device->ctrl.present = 0; /* init card present to be no card */
  289. init_mmc_card(handle);
  290. handle->device->ctrl.present = 1; /* card is detected */
  291. /* switch the data width back */
  292. if (handle->card->type != SD_CARD_MMC)
  293. return SD_FAIL;
  294. /*
  295. * Dynamically set Data Width to highest supported value.
  296. * Try different data width settings (highest to lowest).
  297. * Verify each setting by reading EXT_CSD and comparing
  298. * against the EXT_CSD contents previously read in call to
  299. * init_mmc_card() earlier. Stop at first verified data width
  300. * setting.
  301. */
  302. {
  303. #define EXT_CSD_PROPERTIES_SECTION_START_INDEX 192
  304. #define EXT_CSD_PROPERTIES_SECTION_END_INDEX 511
  305. uint8_t buffer[EXT_CSD_SIZE];
  306. #ifdef DRIVER_EMMC_ENABLE_DATA_WIDTH_8BIT
  307. /* Try 8 Bit Data Width */
  308. chal_sd_config_bus_width((CHAL_HANDLE *) handle->device,
  309. SD_BUS_DATA_WIDTH_8BIT);
  310. if ((!set_card_data_width(handle, SD_BUS_DATA_WIDTH_8BIT)) &&
  311. (!mmc_cmd8(handle, buffer)) &&
  312. (!memcmp(&buffer[EXT_CSD_PROPERTIES_SECTION_START_INDEX],
  313. &(emmc_global_buf_ptr->u.Ext_CSD_storage[EXT_CSD_PROPERTIES_SECTION_START_INDEX]),
  314. EXT_CSD_PROPERTIES_SECTION_END_INDEX - EXT_CSD_PROPERTIES_SECTION_START_INDEX + 1)))
  315. return SD_OK;
  316. #endif
  317. /* Fall back to 4 Bit Data Width */
  318. chal_sd_config_bus_width((CHAL_HANDLE *) handle->device,
  319. SD_BUS_DATA_WIDTH_4BIT);
  320. if ((!set_card_data_width(handle, SD_BUS_DATA_WIDTH_4BIT)) &&
  321. (!mmc_cmd8(handle, buffer)) &&
  322. (!memcmp(&buffer[EXT_CSD_PROPERTIES_SECTION_START_INDEX],
  323. &(emmc_global_buf_ptr->u.Ext_CSD_storage[EXT_CSD_PROPERTIES_SECTION_START_INDEX]),
  324. EXT_CSD_PROPERTIES_SECTION_END_INDEX - EXT_CSD_PROPERTIES_SECTION_START_INDEX + 1)))
  325. return SD_OK;
  326. /* Fall back to 1 Bit Data Width */
  327. chal_sd_config_bus_width((CHAL_HANDLE *) handle->device,
  328. SD_BUS_DATA_WIDTH_1BIT);
  329. /* Just use 1 Bit Data Width then. */
  330. if (!set_card_data_width(handle, SD_BUS_DATA_WIDTH_1BIT))
  331. return SD_OK;
  332. }
  333. return SD_CARD_INIT_ERROR;
  334. }
  335. /*
  336. * The function handles MMC/CEATA card initialization.
  337. */
  338. int init_mmc_card(struct sd_handle *handle)
  339. {
  340. uint32_t ocr = 0, newOcr, rc, limit = 0;
  341. uint32_t cmd1_option = 0x40300000;
  342. uint32_t sec_count;
  343. handle->card->type = SD_CARD_MMC;
  344. do {
  345. SD_US_DELAY(1000);
  346. newOcr = 0;
  347. ocr = 0;
  348. rc = sd_cmd1(handle, cmd1_option, &newOcr);
  349. limit++;
  350. if (rc == SD_OK)
  351. ocr = newOcr;
  352. } while (((ocr & SD_CARD_BUSY) == 0) && (limit < SD_CARD_RETRY_LIMIT));
  353. if (limit >= SD_CARD_RETRY_LIMIT) {
  354. handle->card->type = SD_CARD_UNKNOWN;
  355. EMMC_TRACE("CMD1 Timeout: Device is not ready\n");
  356. return SD_CARD_UNKNOWN;
  357. }
  358. /* Save the ocr register */
  359. handle->device->ctrl.ocr = ocr;
  360. /* Ready State */
  361. rc = sd_cmd2(handle);
  362. if (rc != SD_OK) {
  363. handle->card->type = SD_CARD_UNKNOWN;
  364. return SD_CARD_UNKNOWN;
  365. }
  366. rc = sd_cmd3(handle);
  367. if (rc != SD_OK) {
  368. handle->card->type = SD_CARD_UNKNOWN;
  369. return SD_CARD_UNKNOWN;
  370. }
  371. /* read CSD */
  372. rc = sd_cmd9(handle, &emmc_global_vars_ptr->cardData);
  373. if (rc != SD_OK) {
  374. handle->card->type = SD_CARD_UNKNOWN;
  375. return SD_CARD_UNKNOWN;
  376. }
  377. /* Increase clock frequency according to what the card advertises */
  378. EMMC_TRACE("From CSD... cardData.csd.mmc.speed = 0x%X\n",
  379. emmc_global_vars_ptr->cardData.csd.mmc.speed);
  380. process_csd_mmc_speed(handle,
  381. emmc_global_vars_ptr->cardData.csd.mmc.speed);
  382. /* goto transfer mode */
  383. rc = sd_cmd7(handle, handle->device->ctrl.rca);
  384. if (rc != SD_OK) {
  385. handle->card->type = SD_CARD_UNKNOWN;
  386. return SD_CARD_UNKNOWN;
  387. }
  388. rc = mmc_cmd8(handle, emmc_global_buf_ptr->u.Ext_CSD_storage);
  389. if (rc == SD_OK) {
  390. /* calcul real capacity */
  391. sec_count = emmc_global_buf_ptr->u.Ext_CSD_storage[212] |
  392. emmc_global_buf_ptr->u.Ext_CSD_storage[213] << 8 |
  393. emmc_global_buf_ptr->u.Ext_CSD_storage[214] << 16 |
  394. emmc_global_buf_ptr->u.Ext_CSD_storage[215] << 24;
  395. EMMC_TRACE("Device density = %ldMBytes\n",
  396. handle->card->size / (1024 * 1024));
  397. if (sec_count > 0) {
  398. handle->card->size = (uint64_t)sec_count * 512;
  399. EMMC_TRACE("Updated Device density = %ldMBytes\n",
  400. handle->card->size / (1024 * 1024));
  401. }
  402. if (sec_count > (2u * 1024 * 1024 * 1024) / 512) {
  403. handle->device->ctrl.ocr |= SD_CARD_HIGH_CAPACITY;
  404. handle->device->cfg.blockSize = 512;
  405. }
  406. if (handle->device->ctrl.ocr & SD_CARD_HIGH_CAPACITY) {
  407. EMMC_TRACE("Sector addressing\n");
  408. } else {
  409. EMMC_TRACE("Byte addressing\n");
  410. }
  411. EMMC_TRACE("Ext_CSD_storage[162]: 0x%02X Ext_CSD_storage[179]: 0x%02X\n",
  412. emmc_global_buf_ptr->u.Ext_CSD_storage[162],
  413. emmc_global_buf_ptr->u.Ext_CSD_storage[179]);
  414. }
  415. return handle->card->type;
  416. }
  417. /*
  418. * The function send reset command to the card.
  419. * The card will be in ready status after the reset.
  420. */
  421. int reset_card(struct sd_handle *handle)
  422. {
  423. int res = SD_OK;
  424. /* on reset, card's RCA should return to 0 */
  425. handle->device->ctrl.rca = 0;
  426. res = sd_cmd0(handle);
  427. if (res != SD_OK)
  428. return SD_RESET_ERROR;
  429. return res;
  430. }
  431. /*
  432. * The function sends command to the card and starts
  433. * data transmission.
  434. */
  435. static int xfer_data(struct sd_handle *handle,
  436. uint32_t mode,
  437. uint32_t addr, uint32_t length, uint8_t *base)
  438. {
  439. int rc = SD_OK;
  440. VERBOSE("XFER: dest: 0x%" PRIx64 ", addr: 0x%x, size: 0x%x bytes\n",
  441. (uint64_t)base, addr, length);
  442. if ((length / handle->device->cfg.blockSize) > 1) {
  443. if (mode == SD_OP_READ) {
  444. inv_dcache_range((uintptr_t)base, (uint64_t)length);
  445. rc = sd_cmd18(handle, addr, length, base);
  446. } else {
  447. #ifdef INCLUDE_EMMC_DRIVER_WRITE_CODE
  448. flush_dcache_range((uintptr_t)base, (uint64_t)length);
  449. rc = sd_cmd25(handle, addr, length, base);
  450. #else
  451. rc = SD_DATA_XFER_ERROR;
  452. #endif
  453. }
  454. } else {
  455. if (mode == SD_OP_READ) {
  456. inv_dcache_range((uintptr_t)base, (uint64_t)length);
  457. rc = sd_cmd17(handle, addr,
  458. handle->device->cfg.blockSize, base);
  459. } else {
  460. #ifdef INCLUDE_EMMC_DRIVER_WRITE_CODE
  461. flush_dcache_range((uintptr_t)base, (uint64_t)length);
  462. rc = sd_cmd24(handle, addr,
  463. handle->device->cfg.blockSize, base);
  464. #else
  465. rc = SD_DATA_XFER_ERROR;
  466. #endif
  467. }
  468. }
  469. if (rc != SD_OK)
  470. return SD_DATA_XFER_ERROR;
  471. return SD_OK;
  472. }
  473. #ifdef INCLUDE_EMMC_DRIVER_ERASE_CODE
  474. int erase_card(struct sd_handle *handle, uint32_t addr, uint32_t blocks)
  475. {
  476. uint32_t end_addr;
  477. INFO("ERASE: addr: 0x%x, num of sectors: 0x%x\n", addr, blocks);
  478. if (sd_cmd35(handle, addr) != SD_OK)
  479. return SD_FAIL;
  480. end_addr = addr + blocks - 1;
  481. if (sd_cmd36(handle, end_addr) != SD_OK)
  482. return SD_FAIL;
  483. if (sd_cmd38(handle) != SD_OK)
  484. return SD_FAIL;
  485. return SD_OK;
  486. }
  487. #endif
  488. /*
  489. * The function reads block data from a card.
  490. */
  491. #ifdef USE_EMMC_FIP_TOC_CACHE
  492. int read_block(struct sd_handle *handle,
  493. uint8_t *dst, uint32_t addr, uint32_t len)
  494. {
  495. int rel = SD_OK;
  496. /*
  497. * Avoid doing repeated reads of the partition block
  498. * by caching.
  499. */
  500. if (cached_partition_block &&
  501. addr == PARTITION_BLOCK_ADDR &&
  502. len == CACHE_SIZE) {
  503. memcpy(dst, cached_block, len);
  504. } else {
  505. rel = xfer_data(handle, SD_OP_READ, addr, len, dst);
  506. if (len == CACHE_SIZE && addr == PARTITION_BLOCK_ADDR) {
  507. cached_partition_block = 1;
  508. memcpy(cached_block, dst, len);
  509. }
  510. }
  511. return rel;
  512. }
  513. #else
  514. int read_block(struct sd_handle *handle,
  515. uint8_t *dst, uint32_t addr, uint32_t len)
  516. {
  517. return xfer_data(handle, SD_OP_READ, addr, len, dst);
  518. }
  519. #endif
  520. #ifdef INCLUDE_EMMC_DRIVER_WRITE_CODE
  521. /*
  522. * The function writes block data to a card.
  523. */
  524. int write_block(struct sd_handle *handle,
  525. uint8_t *src, uint32_t addr, uint32_t len)
  526. {
  527. int rel = SD_OK;
  528. /*
  529. * Current HC has problem to get response of cmd16 after cmd12,
  530. * the delay is necessary to sure the next cmd16 will not be timed out.
  531. * The delay has to be at least 4 ms.
  532. * The code removed cmd16 and use cmd13 to get card status before
  533. * sending cmd18 or cmd25 to make sure the card is ready and thus
  534. * no need to have delay here.
  535. */
  536. rel = xfer_data(handle, SD_OP_WRITE, addr, len, src);
  537. EMMC_TRACE("wr_blk addr:0x%08X src:0x%08X len:0x%08X result:%d\n",
  538. addr, src, len, rel);
  539. return rel;
  540. }
  541. /*
  542. * The function is called to write one block data directly to
  543. * a card's data buffer.
  544. * it is used in Non-DMA mode for card data transmission.
  545. */
  546. int write_buffer(struct sd_handle *handle, uint32_t length, uint8_t *data)
  547. {
  548. uint32_t rem, blockSize, event;
  549. uint8_t *pData = data;
  550. blockSize = handle->device->cfg.blockSize;
  551. rem = length;
  552. if (rem == 0)
  553. return SD_OK;
  554. while (rem > 0) {
  555. event = wait_for_event(handle,
  556. SD4_EMMC_TOP_INTR_BWRDY_MASK |
  557. SD_ERR_INTERRUPTS,
  558. handle->device->cfg.wfe_retry);
  559. if (handle->device->ctrl.cmdStatus) {
  560. check_error(handle, handle->device->ctrl.cmdStatus);
  561. return SD_WRITE_ERROR;
  562. }
  563. if (rem >= blockSize)
  564. chal_sd_write_buffer((CHAL_HANDLE *) handle->device,
  565. blockSize, pData);
  566. else
  567. chal_sd_write_buffer((CHAL_HANDLE *) handle->device,
  568. rem, pData);
  569. if (rem > blockSize) {
  570. rem -= blockSize;
  571. pData += blockSize;
  572. } else {
  573. pData += rem;
  574. rem = 0;
  575. }
  576. }
  577. if ((event & SD4_EMMC_TOP_INTR_TXDONE_MASK) !=
  578. SD4_EMMC_TOP_INTR_TXDONE_MASK) {
  579. event = wait_for_event(handle,
  580. SD4_EMMC_TOP_INTR_TXDONE_MASK |
  581. SD_ERR_INTERRUPTS,
  582. handle->device->cfg.wfe_retry);
  583. if (handle->device->ctrl.cmdStatus != SD_OK) {
  584. check_error(handle, handle->device->ctrl.cmdStatus);
  585. return SD_WRITE_ERROR;
  586. }
  587. } else {
  588. handle->device->ctrl.eventList &= ~SD4_EMMC_TOP_INTR_TXDONE_MASK;
  589. }
  590. return SD_OK;
  591. }
  592. #endif /* INCLUDE_EMMC_DRIVER_WRITE_CODE */
  593. /*
  594. * The function is called to read maximal one block data
  595. * directly from a card
  596. * It is used in Non-DMA mode for card data transmission.
  597. */
  598. int read_buffer(struct sd_handle *handle, uint32_t length, uint8_t *data)
  599. {
  600. uint32_t rem, blockSize, event = 0;
  601. uint8_t *pData = data;
  602. blockSize = handle->device->cfg.blockSize;
  603. rem = length;
  604. if (rem == 0)
  605. return SD_OK;
  606. while (rem > 0) {
  607. event = wait_for_event(handle,
  608. SD4_EMMC_TOP_INTR_BRRDY_MASK |
  609. SD_ERR_INTERRUPTS,
  610. handle->device->cfg.wfe_retry);
  611. if (handle->device->ctrl.cmdStatus) {
  612. check_error(handle, handle->device->ctrl.cmdStatus);
  613. return SD_READ_ERROR;
  614. }
  615. if (rem >= blockSize)
  616. chal_sd_read_buffer((CHAL_HANDLE *) handle->device,
  617. blockSize, pData);
  618. else
  619. chal_sd_read_buffer((CHAL_HANDLE *) handle->device, rem,
  620. pData);
  621. if (rem > blockSize) {
  622. rem -= blockSize;
  623. pData += blockSize;
  624. } else {
  625. pData += rem;
  626. rem = 0;
  627. }
  628. }
  629. /* In case, there are extra data in the SD FIFO, just dump them. */
  630. chal_sd_dump_fifo((CHAL_HANDLE *) handle->device);
  631. if ((event & SD4_EMMC_TOP_INTR_TXDONE_MASK) !=
  632. SD4_EMMC_TOP_INTR_TXDONE_MASK) {
  633. event = wait_for_event(handle, SD4_EMMC_TOP_INTR_TXDONE_MASK,
  634. handle->device->cfg.wfe_retry);
  635. if (handle->device->ctrl.cmdStatus) {
  636. check_error(handle, handle->device->ctrl.cmdStatus);
  637. return SD_READ_ERROR;
  638. }
  639. } else {
  640. handle->device->ctrl.eventList &= ~SD4_EMMC_TOP_INTR_TXDONE_MASK;
  641. }
  642. return SD_OK;
  643. }
  644. /*
  645. * Error handling routine.
  646. * The function just reset the DAT
  647. * and CMD line if an error occures during data transmission.
  648. */
  649. int check_error(struct sd_handle *handle, uint32_t ints)
  650. {
  651. uint32_t rel;
  652. chal_sd_set_irq_signal((CHAL_HANDLE *) handle->device,
  653. SD_ERR_INTERRUPTS, 0);
  654. if (ints & SD4_EMMC_TOP_INTR_CMDERROR_MASK) {
  655. chal_sd_reset_line((CHAL_HANDLE *) handle->device,
  656. SD4_EMMC_TOP_CTRL1_CMDRST_MASK);
  657. rel = abort_err(handle);
  658. chal_sd_reset_line((CHAL_HANDLE *) handle->device,
  659. SD4_EMMC_TOP_CTRL1_DATRST_MASK);
  660. chal_sd_set_irq_signal((CHAL_HANDLE *) handle->device,
  661. SD_ERR_INTERRUPTS, 1);
  662. return (rel == SD_ERROR_NON_RECOVERABLE) ?
  663. SD_ERROR_NON_RECOVERABLE : SD_ERROR_RECOVERABLE;
  664. } else {
  665. rel = err_recovery(handle, ints);
  666. }
  667. chal_sd_set_irq_signal((CHAL_HANDLE *) handle->device,
  668. SD_ERR_INTERRUPTS, 1);
  669. return rel;
  670. }
  671. /*
  672. * Error recovery routine.
  673. * Try to recover from the error.
  674. */
  675. static int err_recovery(struct sd_handle *handle, uint32_t errors)
  676. {
  677. uint32_t rel = 0;
  678. /*
  679. * In case of timeout error, the cmd line and data line maybe
  680. * still active or stuck at atcitve so it is needed to reset
  681. * either data line or cmd line to make sure a new cmd can be sent.
  682. */
  683. if (errors & SD_CMD_ERROR_INT)
  684. chal_sd_reset_line((CHAL_HANDLE *) handle->device,
  685. SD4_EMMC_TOP_CTRL1_CMDRST_MASK);
  686. if (errors & SD_DAT_ERROR_INT)
  687. chal_sd_reset_line((CHAL_HANDLE *) handle->device,
  688. SD4_EMMC_TOP_CTRL1_DATRST_MASK);
  689. /* Abort transaction by sending out stop command */
  690. if ((handle->device->ctrl.cmdIndex == 18) ||
  691. (handle->device->ctrl.cmdIndex == 25))
  692. rel = abort_err(handle);
  693. return rel;
  694. }
  695. /*
  696. * The function is called to read one block data directly from a card.
  697. * It is used in Non-DMA mode for card data transmission.
  698. */
  699. int process_cmd_response(struct sd_handle *handle,
  700. uint32_t cmdIndex,
  701. uint32_t rsp0,
  702. uint32_t rsp1,
  703. uint32_t rsp2, uint32_t rsp3, struct sd_resp *resp)
  704. {
  705. int result = SD_OK;
  706. /* R6 */
  707. uint32_t rca = (rsp0 >> 16) & 0xffff;
  708. uint32_t cardStatus = rsp0;
  709. /* R4 */
  710. uint32_t cBit = (rsp0 >> 31) & 0x1;
  711. uint32_t funcs = (rsp0 >> 28) & 0x7;
  712. uint32_t memPresent = (rsp0 >> 27) & 0x1;
  713. resp->r1 = 0x3f;
  714. resp->cardStatus = cardStatus;
  715. if (cmdIndex == SD_CMD_IO_SEND_OP_COND) {
  716. resp->data.r4.cardReady = cBit;
  717. resp->data.r4.funcs = funcs;
  718. resp->data.r4.memPresent = memPresent;
  719. resp->data.r4.ocr = cardStatus;
  720. }
  721. if (cmdIndex == SD_CMD_MMC_SET_RCA) {
  722. resp->data.r6.rca = rca;
  723. resp->data.r6.cardStatus = cardStatus & 0xFFFF;
  724. }
  725. if (cmdIndex == SD_CMD_SELECT_DESELECT_CARD) {
  726. resp->data.r7.rca = rca;
  727. }
  728. if (cmdIndex == SD_CMD_IO_RW_DIRECT) {
  729. if (((rsp0 >> 16) & 0xffff) != 0)
  730. result = SD_CMD_ERR_INVALID_RESPONSE;
  731. resp->data.r5.data = rsp0 & 0xff;
  732. }
  733. if (cmdIndex == SD_CMD_IO_RW_EXTENDED) {
  734. if (((rsp0 >> 16) & 0xffff) != 0)
  735. result = SD_CMD_ERR_INVALID_RESPONSE;
  736. resp->data.r5.data = rsp0 & 0xff;
  737. }
  738. if (cmdIndex == SD_ACMD_SD_SEND_OP_COND ||
  739. cmdIndex == SD_CMD_SEND_OPCOND)
  740. resp->data.r3.ocr = cardStatus;
  741. if (cmdIndex == SD_CMD_SEND_CSD ||
  742. cmdIndex == SD_CMD_SEND_CID ||
  743. cmdIndex == SD_CMD_ALL_SEND_CID) {
  744. resp->data.r2.rsp4 = rsp3;
  745. resp->data.r2.rsp3 = rsp2;
  746. resp->data.r2.rsp2 = rsp1;
  747. resp->data.r2.rsp1 = rsp0;
  748. }
  749. if ((cmdIndex == SD_CMD_READ_EXT_CSD) &&
  750. (handle->card->type == SD_CARD_SD)) {
  751. if ((resp->cardStatus & 0xAA) != 0xAA) {
  752. result = SD_CMD_ERR_INVALID_RESPONSE;
  753. }
  754. }
  755. return result;
  756. }
  757. /*
  758. * The function sets DMA buffer and data length, process
  759. * block size and the number of blocks to be transferred.
  760. * It returns the DMA buffer address.
  761. * It copies dma data from user buffer to the DMA buffer
  762. * if the operation is to write data to the SD card.
  763. */
  764. void data_xfer_setup(struct sd_handle *handle, uint8_t *data, uint32_t length,
  765. int dir)
  766. {
  767. chal_sd_setup_xfer((CHAL_HANDLE *)handle->device, data, length, dir);
  768. }
  769. /*
  770. * The function does soft reset the host SD controller. After
  771. * the function call all host controller's register are reset
  772. * to default vallue;
  773. *
  774. * Note This function only resets the host controller it does not
  775. * reset the controller's handler.
  776. */
  777. int reset_host_ctrl(struct sd_handle *handle)
  778. {
  779. chal_sd_stop();
  780. return SD_OK;
  781. }
  782. static void pstate_log(struct sd_handle *handle)
  783. {
  784. ERROR("PSTATE: 0x%x\n", mmio_read_32
  785. (handle->device->ctrl.sdRegBaseAddr +
  786. SD4_EMMC_TOP_PSTATE_SD4_OFFSET));
  787. ERROR("ERRSTAT: 0x%x\n", mmio_read_32
  788. (handle->device->ctrl.sdRegBaseAddr +
  789. SD4_EMMC_TOP_ERRSTAT_OFFSET));
  790. }
  791. /*
  792. * The function waits for one or a group of interrupts specified
  793. * by mask. The function returns if any one the interrupt status
  794. * is set. If interrupt mode is not enabled then it will poll
  795. * the interrupt status register until a interrupt status is set
  796. * an error interrupt happens. If interrupt mode is enabled then
  797. * this function should be called after the interrupt
  798. * is received by ISR routine.
  799. */
  800. uint32_t wait_for_event(struct sd_handle *handle,
  801. uint32_t mask, uint32_t retry)
  802. {
  803. uint32_t regval, cmd12, time = 0;
  804. handle->device->ctrl.cmdStatus = 0; /* no error */
  805. EMMC_TRACE("%s %d mask:0x%x timeout:%d irq_status:0x%x\n",
  806. __func__, __LINE__, mask, retry,
  807. chal_sd_get_irq_status((CHAL_HANDLE *)handle->device));
  808. /* Polling mode */
  809. do {
  810. regval = chal_sd_get_irq_status((CHAL_HANDLE *)handle->device);
  811. if (regval & SD4_EMMC_TOP_INTR_DMAIRQ_MASK) {
  812. chal_sd_set_dma_addr((CHAL_HANDLE *)handle->device,
  813. (uintptr_t)
  814. chal_sd_get_dma_addr((CHAL_HANDLE *)
  815. handle->device));
  816. chal_sd_clear_irq((CHAL_HANDLE *)handle->device,
  817. SD4_EMMC_TOP_INTR_DMAIRQ_MASK);
  818. }
  819. if (time++ > retry) {
  820. ERROR("EMMC: No response (cmd%d) after %dus.\n",
  821. handle->device->ctrl.cmdIndex,
  822. time * EMMC_WFE_RETRY_DELAY_US);
  823. handle->device->ctrl.cmdStatus = SD_CMD_MISSING;
  824. pstate_log(handle);
  825. ERROR("EMMC: INT[0x%x]\n", regval);
  826. break;
  827. }
  828. if (regval & SD4_EMMC_TOP_INTR_CTOERR_MASK) {
  829. ERROR("EMMC: Cmd%d timeout INT[0x%x]\n",
  830. handle->device->ctrl.cmdIndex, regval);
  831. handle->device->ctrl.cmdStatus =
  832. SD4_EMMC_TOP_INTR_CTOERR_MASK;
  833. pstate_log(handle);
  834. break;
  835. }
  836. if (regval & SD_CMD_ERROR_FLAGS) {
  837. ERROR("EMMC: Cmd%d error INT[0x%x]\n",
  838. handle->device->ctrl.cmdIndex, regval);
  839. handle->device->ctrl.cmdStatus = SD_CMD_ERROR_FLAGS;
  840. pstate_log(handle);
  841. break;
  842. }
  843. cmd12 = chal_sd_get_atuo12_error((CHAL_HANDLE *)handle->device);
  844. if (cmd12) {
  845. ERROR("EMMC: Cmd%d auto cmd12 err:0x%x\n",
  846. handle->device->ctrl.cmdIndex, cmd12);
  847. handle->device->ctrl.cmdStatus = cmd12;
  848. pstate_log(handle);
  849. break;
  850. }
  851. if (SD_DATA_ERROR_FLAGS & regval) {
  852. ERROR("EMMC: Data for cmd%d error, INT[0x%x]\n",
  853. handle->device->ctrl.cmdIndex, regval);
  854. handle->device->ctrl.cmdStatus =
  855. (SD_DATA_ERROR_FLAGS & regval);
  856. pstate_log(handle);
  857. break;
  858. }
  859. if ((regval & mask) == 0)
  860. udelay(EMMC_WFE_RETRY_DELAY_US);
  861. } while ((regval & mask) == 0);
  862. /* clear the interrupt since it is processed */
  863. chal_sd_clear_irq((CHAL_HANDLE *)handle->device, (regval & mask));
  864. return (regval & mask);
  865. }
  866. int32_t set_config(struct sd_handle *handle, uint32_t speed, uint32_t retry,
  867. uint32_t dma, uint32_t dmaBound, uint32_t blkSize,
  868. uint32_t wfe_retry)
  869. {
  870. int32_t rel = 0;
  871. if (handle == NULL)
  872. return SD_FAIL;
  873. handle->device->cfg.wfe_retry = wfe_retry;
  874. rel = chal_sd_config((CHAL_HANDLE *)handle->device, speed, retry,
  875. dmaBound, blkSize, dma);
  876. return rel;
  877. }
  878. int mmc_cmd1(struct sd_handle *handle)
  879. {
  880. uint32_t newOcr, res;
  881. uint32_t cmd1_option = MMC_OCR_OP_VOLT | MMC_OCR_SECTOR_ACCESS_MODE;
  882. /*
  883. * After Reset, eMMC comes up in 1 Bit Data Width by default.
  884. * Set host side to match.
  885. */
  886. chal_sd_config_bus_width((CHAL_HANDLE *) handle->device,
  887. SD_BUS_DATA_WIDTH_1BIT);
  888. #ifdef USE_EMMC_FIP_TOC_CACHE
  889. cached_partition_block = 0;
  890. #endif
  891. handle->device->ctrl.present = 0; /* init card present to be no card */
  892. handle->card->type = SD_CARD_MMC;
  893. res = sd_cmd1(handle, cmd1_option, &newOcr);
  894. if (res != SD_OK) {
  895. EMMC_TRACE("CMD1 Timeout: Device is not ready\n");
  896. res = SD_CARD_UNKNOWN;
  897. }
  898. return res;
  899. }