emmc_chal_sd.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. /*
  2. * Copyright (c) 2016 - 2020, Broadcom
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <string.h>
  8. #include <lib/mmio.h>
  9. #include <platform_def.h>
  10. #include "bcm_emmc.h"
  11. #include "emmc_chal_types.h"
  12. #include "emmc_chal_sd.h"
  13. #include "emmc_pboot_hal_memory_drv.h"
  14. extern void emmc_soft_reset(void);
  15. #define SD_VDD_WINDOW_1_6_TO_1_7 0x00000010 // 1.6 V to 1.7 Volts
  16. #define SD_VDD_WINDOW_1_7_TO_1_8 0x00000020 // 1.7 V to 1.8 Volts
  17. #define SD_VDD_WINDOW_1_8_TO_1_9 0x00000040 // 1.8 V to 1.9 Volts
  18. #define SD_VDD_WINDOW_1_9_TO_2_0 0x00000080 // 1.9 V to 2.0 Volts
  19. #define SD_VDD_WINDOW_2_0_TO_2_1 0x00000100 // 2.0 V to 2.1 Volts
  20. #define SD_VDD_WINDOW_2_1_TO_2_2 0x00000200 // 2.1 V to 2.2 Volts
  21. #define SD_VDD_WINDOW_2_2_TO_2_3 0x00000400 // 2.2 V to 2.3 Volts
  22. #define SD_VDD_WINDOW_2_3_TO_2_4 0x00000800 // 2.3 V to 2.4 Volts
  23. #define SD_VDD_WINDOW_2_4_TO_2_5 0x00001000 // 2.4 V to 2.5 Volts
  24. #define SD_VDD_WINDOW_2_5_TO_2_6 0x00002000 // 2.5 V to 2.6 Volts
  25. #define SD_VDD_WINDOW_2_6_TO_2_7 0x00004000 // 2.6 V to 2.7 Volts
  26. #define SD_VDD_WINDOW_2_7_TO_2_8 0x00008000 // 2.7 V to 2.8 Volts
  27. #define SD_VDD_WINDOW_2_8_TO_2_9 0x00010000 // 2.8 V to 2.9 Volts
  28. #define SD_VDD_WINDOW_2_9_TO_3_0 0x00020000 // 2.9 V to 3.0 Volts
  29. #define SD_VDD_WINDOW_3_0_TO_3_1 0x00040000 // 3.0 V to 3.1 Volts
  30. #define SD_VDD_WINDOW_3_1_TO_3_2 0x00080000 // 3.1 V to 3.2 Volts
  31. #define SD_VDD_WINDOW_3_2_TO_3_3 0x00100000 // 3.2 V to 3.3 Volts
  32. #define SD_VDD_WINDOW_3_3_TO_3_4 0x00200000 // 3.3 V to 3.4 Volts
  33. #define SD_VDD_WINDOW_3_4_TO_3_5 0x00400000 // 3.4 V to 3.5 Volts
  34. #define SD_VDD_WINDOW_3_5_TO_3_6 0x00800000 // 3.5 V to 3.6 Volts
  35. #define SD_VDD_WINDOW_1_6_TO_2_6 (SD_VDD_WINDOW_1_6_TO_1_7 | \
  36. SD_VDD_WINDOW_1_7_TO_1_8 | \
  37. SD_VDD_WINDOW_1_8_TO_1_9 | \
  38. SD_VDD_WINDOW_1_9_TO_2_0 | \
  39. SD_VDD_WINDOW_2_0_TO_2_1 | \
  40. SD_VDD_WINDOW_2_1_TO_2_2 | \
  41. SD_VDD_WINDOW_2_2_TO_2_3 | \
  42. SD_VDD_WINDOW_2_3_TO_2_4 | \
  43. SD_VDD_WINDOW_2_4_TO_2_5 | \
  44. SD_VDD_WINDOW_2_5_TO_2_6)
  45. #define SD_VDD_WINDOW_2_6_TO_3_2 (SD_VDD_WINDOW_2_6_TO_2_7 | \
  46. SD_VDD_WINDOW_2_7_TO_2_8 | \
  47. SD_VDD_WINDOW_2_8_TO_2_9 | \
  48. SD_VDD_WINDOW_2_9_TO_3_0 | \
  49. SD_VDD_WINDOW_3_0_TO_3_1 | \
  50. SD_VDD_WINDOW_3_1_TO_3_2)
  51. #define SD_VDD_WINDOW_3_2_TO_3_6 (SD_VDD_WINDOW_3_2_TO_3_3 | \
  52. SD_VDD_WINDOW_3_3_TO_3_4 | \
  53. SD_VDD_WINDOW_3_4_TO_3_5 | \
  54. SD_VDD_WINDOW_3_5_TO_3_6)
  55. static int32_t chal_sd_set_power(struct sd_dev *handle,
  56. uint32_t voltage, uint32_t state);
  57. static void chal_sd_set_dma_boundary(struct sd_dev *handle, uint32_t boundary);
  58. static int32_t chal_sd_setup_handler(struct sd_dev *handle,
  59. uint32_t sdBbase, uint32_t hostBase);
  60. /*
  61. * Configure host controller pwr settings,
  62. * to match voltage requirements by SD Card
  63. */
  64. static int32_t chal_sd_set_power(struct sd_dev *handle,
  65. uint32_t voltage, uint32_t state)
  66. {
  67. int32_t rc, rval = SD_FAIL;
  68. uint32_t time = 0;
  69. if (handle == NULL)
  70. return SD_INVALID_HANDLE;
  71. mmio_clrsetbits_32(handle->ctrl.sdRegBaseAddr +
  72. SD4_EMMC_TOP_CTRL_OFFSET,
  73. (SD4_EMMC_TOP_CTRL_SDVSELVDD1_MASK |
  74. SD4_EMMC_TOP_CTRL_SDPWR_MASK),
  75. (voltage << 9));
  76. /*
  77. * Long delay is required here in emulation. Without this, the initial
  78. * commands sent to the eMMC card timeout. We don't know if this
  79. * delay is necessary with silicon, leaving in for safety.
  80. * It is observed that 403ms on emulation system and as per the clock
  81. * calculations it is expected to complete with in 1ms on chip
  82. */
  83. do {
  84. rc = mmio_read_32(handle->ctrl.sdRegBaseAddr +
  85. SD4_EMMC_TOP_INTR_OFFSET);
  86. if ((rc & SD4_EMMC_TOP_INTR_CRDINS_MASK) ==
  87. SD4_EMMC_TOP_INTR_CRDINS_MASK)
  88. break;
  89. mdelay(1);
  90. } while (time++ < EMMC_CARD_DETECT_TIMEOUT_MS);
  91. if (time >= EMMC_CARD_DETECT_TIMEOUT_MS) {
  92. ERROR("EMMC: Card insert event detection timeout\n");
  93. return rval;
  94. }
  95. VERBOSE("EMMC: Card detection delay: %dms\n", time);
  96. if (state)
  97. mmio_setbits_32(handle->ctrl.sdRegBaseAddr + SD4_EMMC_TOP_CTRL_OFFSET,
  98. SD4_EMMC_TOP_CTRL_SDPWR_MASK);
  99. /* dummy write & ack to verify if the sdio is ready to send commands */
  100. mmio_write_32(handle->ctrl.sdRegBaseAddr + SD4_EMMC_TOP_ARG_OFFSET, 0);
  101. mmio_write_32(handle->ctrl.sdRegBaseAddr + SD4_EMMC_TOP_CMD_OFFSET, 0);
  102. /*
  103. * 63ms observed on emulation system, As per clock calculations
  104. * it will complete < 1ms on chip.
  105. */
  106. time = 0;
  107. do {
  108. rc = mmio_read_32(handle->ctrl.sdRegBaseAddr +
  109. SD4_EMMC_TOP_INTR_OFFSET);
  110. if (rc & SD4_EMMC_TOP_INTR_ERRIRQ_MASK)
  111. break;
  112. if ((rc & SD4_EMMC_TOP_INTR_CMDDONE_MASK) ==
  113. SD4_EMMC_TOP_INTR_CMDDONE_MASK)
  114. break;
  115. mdelay(1);
  116. } while (time++ < EMMC_CMD_TIMEOUT_MS);
  117. if (time >= EMMC_CMD_TIMEOUT_MS) {
  118. WARN("%s %d Initial dummy command timeout is happened\n",
  119. __func__, __LINE__);
  120. return rval;
  121. }
  122. VERBOSE("EMMC: Dummy Command delay: %dms\n", time);
  123. return SD_OK;
  124. }
  125. /*
  126. * Configure DMA Boundaries
  127. */
  128. static void chal_sd_set_dma_boundary(struct sd_dev *handle, uint32_t boundary)
  129. {
  130. if (handle == NULL)
  131. return;
  132. mmio_clrsetbits_32(handle->ctrl.sdRegBaseAddr +
  133. SD4_EMMC_TOP_BLOCK_OFFSET,
  134. SD4_EMMC_TOP_BLOCK_HSBS_MASK, boundary);
  135. }
  136. static int32_t chal_sd_setup_handler(struct sd_dev *handle, uint32_t sdBase,
  137. uint32_t hostBase)
  138. {
  139. if (handle == NULL)
  140. return SD_INVALID_HANDLE;
  141. handle->ctrl.sdRegBaseAddr = sdBase;
  142. handle->ctrl.hostRegBaseAddr = hostBase;
  143. handle->ctrl.present = 0;
  144. handle->ctrl.rca = 0;
  145. handle->ctrl.blkGapEnable = 0;
  146. handle->ctrl.cmdStatus = 0;
  147. return SD_OK;
  148. }
  149. /*
  150. * Initialize SD Host controller
  151. */
  152. int32_t chal_sd_init(CHAL_HANDLE *sd_handle)
  153. {
  154. uint32_t cap_val_l = 0;
  155. uint32_t ctl_val, voltage;
  156. uint32_t timeout_val;
  157. struct sd_dev *handle;
  158. uint32_t reg_val;
  159. int32_t rval = SD_FAIL;
  160. if (sd_handle == NULL)
  161. return SD_INVALID_HANDLE;
  162. handle = (struct sd_dev *)sd_handle;
  163. /*
  164. * Set SDIO Host Controller capabilities register
  165. */
  166. EMMC_TRACE("Set Host Controller Capabilities register\n");
  167. reg_val = 0;
  168. reg_val |= (1 << ICFG_SDIO0_CAP0__SLOT_TYPE_R);
  169. reg_val |= (0 << ICFG_SDIO0_CAP0__INT_MODE_R);
  170. reg_val |= (0 << ICFG_SDIO0_CAP0__SYS_BUS_64BIT_R);
  171. reg_val |= (1 << ICFG_SDIO0_CAP0__VOLTAGE_1P8V_R);
  172. reg_val |= (1 << ICFG_SDIO0_CAP0__VOLTAGE_3P0V_R);
  173. reg_val |= (1 << ICFG_SDIO0_CAP0__VOLTAGE_3P3V_R);
  174. reg_val |= (1 << ICFG_SDIO0_CAP0__SUSPEND_RESUME_R);
  175. reg_val |= (1 << ICFG_SDIO0_CAP0__SDMA_R);
  176. reg_val |= (1 << ICFG_SDIO0_CAP0__HIGH_SPEED_R);
  177. reg_val |= (1 << ICFG_SDIO0_CAP0__ADMA2_R);
  178. reg_val |= (1 << ICFG_SDIO0_CAP0__EXTENDED_MEDIA_R);
  179. reg_val |= (2 << ICFG_SDIO0_CAP0__MAX_BLOCK_LEN_R);
  180. reg_val |= (0xd0 << ICFG_SDIO0_CAP0__BASE_CLK_FREQ_R);
  181. reg_val |= (1 << ICFG_SDIO0_CAP0__TIMEOUT_UNIT_R);
  182. reg_val |= (0x30 << ICFG_SDIO0_CAP0__TIMEOUT_CLK_FREQ_R);
  183. mmio_write_32(ICFG_SDIO0_CAP0, reg_val);
  184. reg_val = 0;
  185. reg_val |= (1 << ICFG_SDIO0_CAP1__SPI_BLOCK_MODE_R);
  186. reg_val |= (1 << ICFG_SDIO0_CAP1__SPI_MODE_R);
  187. reg_val |= (0 << ICFG_SDIO0_CAP1__CLK_MULT_R);
  188. reg_val |= (0 << ICFG_SDIO0_CAP1__RETUNING_MODE_R);
  189. reg_val |= (1 << ICFG_SDIO0_CAP1__TUNE_SDR50_R);
  190. reg_val |= (1 << ICFG_SDIO0_CAP1__TIME_RETUNE_R);
  191. reg_val |= (1 << ICFG_SDIO0_CAP1__DRIVER_D_R);
  192. reg_val |= (1 << ICFG_SDIO0_CAP1__DRIVER_C_R);
  193. reg_val |= (1 << ICFG_SDIO0_CAP1__DRIVER_A_R);
  194. reg_val |= (1 << ICFG_SDIO0_CAP1__DDR50_R);
  195. reg_val |= (1 << ICFG_SDIO0_CAP1__SDR104_R);
  196. reg_val |= (1 << ICFG_SDIO0_CAP1__SDR50_R);
  197. mmio_write_32(ICFG_SDIO0_CAP1, reg_val);
  198. /* Reset the SDIO controller */
  199. chal_sd_stop();
  200. /* Turn on SD clock */
  201. chal_sd_set_clock(sd_handle,
  202. chal_sd_freq_2_div_ctrl_setting(INIT_CLK_FREQ), 1);
  203. /* program data time out value to the max */
  204. timeout_val = SD_HOST_CORE_TIMEOUT;
  205. ctl_val = mmio_read_32(handle->ctrl.sdRegBaseAddr +
  206. SD4_EMMC_TOP_CTRL1_OFFSET);
  207. ctl_val |= ((timeout_val & 0xf) << SD4_EMMC_TOP_CTRL1_DTCNT_SHIFT);
  208. mmio_write_32(handle->ctrl.sdRegBaseAddr + SD4_EMMC_TOP_CTRL1_OFFSET,
  209. ctl_val);
  210. /* enable all interrupt status */
  211. mmio_write_32(handle->ctrl.sdRegBaseAddr + SD4_EMMC_TOP_INTREN1_OFFSET,
  212. 0);
  213. mmio_write_32(handle->ctrl.sdRegBaseAddr + SD4_EMMC_TOP_INTREN2_OFFSET,
  214. 0);
  215. SD_US_DELAY(100);
  216. mmio_write_32(handle->ctrl.sdRegBaseAddr + SD4_EMMC_TOP_INTREN1_OFFSET,
  217. SD_NOR_INTERRUPTS | SD_ERR_INTERRUPTS);
  218. mmio_write_32(handle->ctrl.sdRegBaseAddr + SD4_EMMC_TOP_INTREN2_OFFSET,
  219. SD_NOR_INTERRUPTS | SD_ERR_INTERRUPTS);
  220. /* Select SD bus voltage */
  221. cap_val_l = mmio_read_32(handle->ctrl.sdRegBaseAddr +
  222. SD4_EMMC_TOP_CAPABILITIES1_OFFSET);
  223. handle->cfg.voltage = 0;
  224. voltage = 0x7;
  225. if (cap_val_l & SD4_EMMC_TOP_CAPABILITIES1_V33_MASK) {
  226. handle->cfg.voltage |= SD_VDD_WINDOW_3_3_TO_3_4;
  227. voltage = 0x7;
  228. } else if (cap_val_l & SD4_EMMC_TOP_CAPABILITIES1_V3_MASK) {
  229. handle->cfg.voltage |= SD_VDD_WINDOW_3_0_TO_3_1;
  230. voltage = 0x6;
  231. } else if (cap_val_l & SD4_EMMC_TOP_CAPABILITIES1_V18_MASK) {
  232. handle->cfg.voltage |= SD_VDD_WINDOW_1_8_TO_1_9;
  233. voltage = 0x5;
  234. }
  235. rval = chal_sd_set_power(handle, voltage, SD4_EMMC_TOP_CTRL_SDPWR_MASK);
  236. ctl_val = mmio_read_32(handle->ctrl.sdRegBaseAddr +
  237. SD4_EMMC_TOP_HCVERSIRQ_OFFSET);
  238. handle->ctrl.version = ((ctl_val >> 16) & 0xFF);
  239. return rval;
  240. }
  241. void chal_sd_set_speed(CHAL_HANDLE *sd_handle, uint32_t speed)
  242. {
  243. struct sd_dev *handle;
  244. if (sd_handle == NULL)
  245. return;
  246. handle = (struct sd_dev *) sd_handle;
  247. if (speed) {
  248. EMMC_TRACE("enable HighSpeed\n");
  249. mmio_setbits_32(handle->ctrl.sdRegBaseAddr +
  250. SD4_EMMC_TOP_CTRL_OFFSET,
  251. SD4_EMMC_TOP_CTRL_HSEN_MASK);
  252. } else {
  253. EMMC_TRACE("disable HighSpeed\n");
  254. mmio_clrbits_32(handle->ctrl.sdRegBaseAddr +
  255. SD4_EMMC_TOP_CTRL_OFFSET,
  256. SD4_EMMC_TOP_CTRL_HSEN_MASK);
  257. }
  258. }
  259. int32_t chal_sd_stop(void)
  260. {
  261. uintptr_t idm_rst_ctrl_addr = EMMC_IDM_RESET_CTRL_ADDR;
  262. /* Configure IO pins */
  263. emmc_soft_reset();
  264. /* Reset the SDIO controller */
  265. mmio_write_32(idm_rst_ctrl_addr, 1);
  266. SD_US_DELAY(100);
  267. mmio_write_32(idm_rst_ctrl_addr, 0);
  268. SD_US_DELAY(100);
  269. return SD_OK;
  270. }
  271. /*
  272. * Check if host supports specified capability
  273. * returns -ve val on error, 0 if capability not supported else 1.
  274. */
  275. int32_t chal_sd_check_cap(CHAL_HANDLE *sd_handle, uint32_t caps)
  276. {
  277. struct sd_dev *handle;
  278. if (sd_handle == NULL)
  279. return SD_INVALID_HANDLE;
  280. handle = (struct sd_dev *) sd_handle;
  281. if (caps & mmio_read_32(handle->ctrl.sdRegBaseAddr +
  282. SD4_EMMC_TOP_CAPABILITIES1_OFFSET))
  283. return 1;
  284. else
  285. return 0;
  286. }
  287. int32_t chal_sd_start(CHAL_HANDLE *sd_handle,
  288. uint32_t mode, uint32_t sd_base, uint32_t host_base)
  289. {
  290. struct sd_dev *handle;
  291. int32_t rval = SD_FAIL;
  292. if (sd_handle == NULL)
  293. return SD_INVALID_HANDLE;
  294. handle = (struct sd_dev *) sd_handle;
  295. handle->cfg.mode = SD_PIO_MODE; /* set to PIO mode first for init */
  296. handle->cfg.dma = SD_DMA_OFF;
  297. chal_sd_setup_handler(handle, sd_base, host_base);
  298. /* init and start hw */
  299. rval = chal_sd_init(sd_handle);
  300. if (rval != SD_OK)
  301. return rval;
  302. chal_sd_clear_pending_irq(sd_handle);
  303. handle->ctrl.eventList = 0;
  304. handle->cfg.mode = mode;
  305. return SD_OK;
  306. }
  307. /*
  308. * Function to check 8bits of err generated from auto CMD12
  309. */
  310. int32_t chal_sd_get_atuo12_error(CHAL_HANDLE *sd_handle)
  311. {
  312. struct sd_dev *handle;
  313. if (sd_handle == NULL)
  314. return SD_INVALID_HANDLE;
  315. handle = (struct sd_dev *) sd_handle;
  316. return (mmio_read_32(handle->ctrl.sdRegBaseAddr +
  317. SD4_EMMC_TOP_ERRSTAT_OFFSET) & 0xFF);
  318. }
  319. /*
  320. * Read present state register
  321. */
  322. uint32_t chal_sd_get_present_status(CHAL_HANDLE *sd_handle)
  323. {
  324. struct sd_dev *handle;
  325. if (sd_handle == NULL)
  326. return SD_INVALID_HANDLE;
  327. handle = (struct sd_dev *) sd_handle;
  328. return mmio_read_32(handle->ctrl.sdRegBaseAddr +
  329. SD4_EMMC_TOP_PSTATE_OFFSET);
  330. }
  331. /*
  332. * Set SD bus width
  333. */
  334. int32_t chal_sd_config_bus_width(CHAL_HANDLE *sd_handle, int32_t width)
  335. {
  336. uint32_t ctl_val;
  337. struct sd_dev *handle;
  338. if (sd_handle == NULL)
  339. return SD_INVALID_HANDLE;
  340. handle = (struct sd_dev *)sd_handle;
  341. ctl_val = mmio_read_32(handle->ctrl.sdRegBaseAddr +
  342. SD4_EMMC_TOP_CTRL_OFFSET);
  343. switch (width) {
  344. #ifdef DRIVER_EMMC_ENABLE_DATA_WIDTH_8BIT
  345. case SD_BUS_DATA_WIDTH_8BIT:
  346. ctl_val &= ~SD_BUS_DATA_WIDTH_4BIT;
  347. ctl_val |= SD_BUS_DATA_WIDTH_8BIT;
  348. break;
  349. #endif
  350. case SD_BUS_DATA_WIDTH_4BIT:
  351. ctl_val &= ~SD_BUS_DATA_WIDTH_8BIT;
  352. ctl_val |= SD_BUS_DATA_WIDTH_4BIT;
  353. break;
  354. case SD_BUS_DATA_WIDTH_1BIT:
  355. ctl_val &= ~(SD_BUS_DATA_WIDTH_4BIT | SD_BUS_DATA_WIDTH_8BIT);
  356. break;
  357. default:
  358. return SD_INV_DATA_WIDTH;
  359. };
  360. mmio_write_32(handle->ctrl.sdRegBaseAddr + SD4_EMMC_TOP_CTRL_OFFSET,
  361. ctl_val);
  362. return SD_OK;
  363. }
  364. /*
  365. * Function to enable or disable DMA control.
  366. */
  367. int32_t chal_sd_set_dma(CHAL_HANDLE *sd_handle, uint32_t mode)
  368. {
  369. uint32_t val;
  370. struct sd_dev *handle;
  371. int32_t rc;
  372. if (sd_handle == NULL)
  373. return SD_INVALID_HANDLE;
  374. handle = (struct sd_dev *)sd_handle;
  375. if (mode) {
  376. rc = chal_sd_check_cap(sd_handle,
  377. SD4_EMMC_TOP_CAPABILITIES1_SDMA_MASK |
  378. SD4_EMMC_TOP_CAPABILITIES1_ADMA2_MASK);
  379. if (rc < 0)
  380. return rc;
  381. if (rc) {
  382. handle->cfg.dma = mode;
  383. val = mmio_read_32(handle->ctrl.sdRegBaseAddr +
  384. SD4_EMMC_TOP_CTRL_OFFSET);
  385. val &= ~(SD4_EMMC_TOP_CTRL_DMASEL_MASK);
  386. val |= handle->cfg.dma - 1;
  387. mmio_write_32(handle->ctrl.sdRegBaseAddr +
  388. SD4_EMMC_TOP_CTRL_OFFSET, val);
  389. return SD_OK;
  390. }
  391. }
  392. handle->cfg.dma = 0;
  393. return SD_FAIL;
  394. }
  395. /*
  396. * Get current DMA address.
  397. * Called only when there is no data transaction activity.
  398. */
  399. uintptr_t chal_sd_get_dma_addr(CHAL_HANDLE *sd_handle)
  400. {
  401. struct sd_dev *handle;
  402. if (sd_handle == NULL)
  403. return SD_INVALID_HANDLE;
  404. handle = (struct sd_dev *) sd_handle;
  405. if (handle->cfg.dma == SD_DMA_OFF)
  406. return 0;
  407. return (uintptr_t)mmio_read_32(handle->ctrl.sdRegBaseAddr +
  408. SD4_EMMC_TOP_SYSADDR_OFFSET);
  409. }
  410. int32_t chal_sd_send_cmd(CHAL_HANDLE *sd_handle, uint32_t cmd_idx,
  411. uint32_t argument, uint32_t options)
  412. {
  413. uint32_t cmd_mode_reg = 0;
  414. struct sd_dev *handle;
  415. if (sd_handle == NULL)
  416. return SD_INVALID_HANDLE;
  417. handle = (struct sd_dev *) sd_handle;
  418. EMMC_TRACE("%s %d cmd:%d argReg:%x options:%x\n",
  419. __func__, __LINE__, cmd_idx, argument, options);
  420. /* Configure the value for command and mode registers */
  421. cmd_mode_reg = (cmd_idx << 24) | options;
  422. /*
  423. * 1. Write block size reg & block count reg,
  424. * this is done in the tx or rx setup
  425. */
  426. mmio_write_32(handle->ctrl.sdRegBaseAddr + SD4_EMMC_TOP_BLOCK_OFFSET,
  427. handle->ctrl.blkReg);
  428. /* 2. Write argument reg */
  429. mmio_write_32(handle->ctrl.sdRegBaseAddr + SD4_EMMC_TOP_ARG_OFFSET,
  430. argument);
  431. handle->ctrl.argReg = argument;
  432. /*
  433. * 3. Write transfer mode reg & command reg, check the DMA bit which is
  434. * set before this function call if it is selected.
  435. */
  436. if (cmd_idx == 24 || cmd_idx == 25 || cmd_idx == 18 || cmd_idx == 17 ||
  437. cmd_idx == 42 || cmd_idx == 51 || cmd_idx == 53)
  438. cmd_mode_reg |= ((handle->cfg.dma) ? 1 : 0);
  439. mmio_write_32(handle->ctrl.sdRegBaseAddr + SD4_EMMC_TOP_CMD_OFFSET,
  440. cmd_mode_reg);
  441. handle->ctrl.cmdIndex = cmd_idx;
  442. return SD_OK;
  443. }
  444. int32_t chal_sd_set_dma_addr(CHAL_HANDLE *sd_handle, uintptr_t address)
  445. {
  446. struct sd_dev *handle;
  447. if (sd_handle == NULL)
  448. return SD_INVALID_HANDLE;
  449. handle = (struct sd_dev *) sd_handle;
  450. if (handle->cfg.dma == SD_DMA_OFF)
  451. return SD_FAIL;
  452. mmio_write_32(handle->ctrl.sdRegBaseAddr + SD4_EMMC_TOP_SYSADDR_OFFSET,
  453. address);
  454. return SD_OK;
  455. }
  456. uint32_t chal_sd_freq_2_div_ctrl_setting(uint32_t desired_freq)
  457. {
  458. /*
  459. * Divider control setting represents 1/2 of the actual divider value.
  460. *
  461. * DesiredFreq = BaseClockFreq / (2 * div_ctrl_setting)
  462. *
  463. * ==> div_ctrl_setting = BaseClockFreq / (2 * DesiredFreq)
  464. */
  465. uint32_t div_ctrl_setting;
  466. uint32_t actual_freq;
  467. assert(desired_freq != 0);
  468. /* Special case, 0 = divider of 1. */
  469. if (desired_freq >= BASE_CLK_FREQ)
  470. return 0;
  471. /* Normal case, desired_freq < BASE_CLK_FREQ */
  472. div_ctrl_setting = BASE_CLK_FREQ / (2 * desired_freq);
  473. actual_freq = BASE_CLK_FREQ / (2 * div_ctrl_setting);
  474. if (actual_freq > desired_freq) {
  475. /*
  476. * Division does not result in exact frequency match.
  477. * Make sure resulting frequency does not exceed requested freq.
  478. */
  479. div_ctrl_setting++;
  480. }
  481. return div_ctrl_setting;
  482. }
  483. int32_t chal_sd_set_clock(CHAL_HANDLE *sd_handle, uint32_t div_ctrl_setting,
  484. uint32_t on)
  485. {
  486. uint32_t value;
  487. struct sd_dev *handle;
  488. uint32_t time;
  489. uint32_t clk_sel_high_byte = 0xFF & (div_ctrl_setting >> 8);
  490. uint32_t clk_sel_low_byte = 0xFF & div_ctrl_setting;
  491. if (sd_handle == NULL)
  492. return SD_INVALID_HANDLE;
  493. EMMC_TRACE("set_clock(div_ctrl_setting=%d,on=%d)\n",
  494. div_ctrl_setting, on);
  495. handle = (struct sd_dev *) sd_handle;
  496. /* Read control register content. */
  497. value = mmio_read_32(handle->ctrl.sdRegBaseAddr +
  498. SD4_EMMC_TOP_CTRL1_OFFSET);
  499. /* Disable Clock */
  500. value &= ~(SD4_EMMC_TOP_CTRL1_SDCLKEN_MASK);
  501. mmio_write_32(handle->ctrl.sdRegBaseAddr + SD4_EMMC_TOP_CTRL1_OFFSET,
  502. value);
  503. /* Clear bits of interest. */
  504. value &= ~(SD4_EMMC_TOP_CTRL1_SDCLKSEL_MASK |
  505. SD4_EMMC_TOP_CTRL1_SDCLKSEL_UP_MASK);
  506. /* Set bits of interest to new value. */
  507. value |= (SD4_EMMC_TOP_CTRL1_SDCLKSEL_MASK &
  508. (clk_sel_low_byte << SD4_EMMC_TOP_CTRL1_SDCLKSEL_SHIFT));
  509. value |= (SD4_EMMC_TOP_CTRL1_SDCLKSEL_UP_MASK &
  510. (clk_sel_high_byte << SD4_EMMC_TOP_CTRL1_SDCLKSEL_UP_SHIFT));
  511. value |= SD4_EMMC_TOP_CTRL1_ICLKEN_MASK;
  512. /* Write updated value back to control register. */
  513. mmio_write_32(handle->ctrl.sdRegBaseAddr + SD4_EMMC_TOP_CTRL1_OFFSET,
  514. value);
  515. time = 0;
  516. do {
  517. value = mmio_read_32(handle->ctrl.sdRegBaseAddr +
  518. SD4_EMMC_TOP_CTRL1_OFFSET);
  519. if ((value & SD4_EMMC_TOP_CTRL1_ICLKSTB_MASK) ==
  520. SD4_EMMC_TOP_CTRL1_ICLKSTB_MASK)
  521. break;
  522. mdelay(1);
  523. } while (time++ < EMMC_CLOCK_SETTING_TIMEOUT_MS);
  524. if (time >= EMMC_CLOCK_SETTING_TIMEOUT_MS)
  525. WARN("%s %d clock settings timeout happenedi (%dms)\n",
  526. __func__, __LINE__, time);
  527. VERBOSE("EMMC: clock settings delay: %dms\n", time);
  528. value = mmio_read_32(handle->ctrl.sdRegBaseAddr +
  529. SD4_EMMC_TOP_CTRL1_OFFSET);
  530. if (on)
  531. value |= SD4_EMMC_TOP_CTRL1_SDCLKEN_MASK;
  532. mmio_write_32(handle->ctrl.sdRegBaseAddr + SD4_EMMC_TOP_CTRL1_OFFSET,
  533. value);
  534. return SD_OK;
  535. }
  536. /*
  537. * function to setup DMA buffer and data length, calculates block
  538. * size and the number of blocks to be transferred and return
  539. * the DMA buffer address.
  540. */
  541. int32_t chal_sd_setup_xfer(CHAL_HANDLE *sd_handle,
  542. uint8_t *data, uint32_t length, int32_t dir)
  543. {
  544. uint32_t blocks = 0;
  545. struct sd_dev *handle;
  546. if (sd_handle == NULL)
  547. return SD_INVALID_HANDLE;
  548. handle = (struct sd_dev *) sd_handle;
  549. if (length <= handle->cfg.blockSize) {
  550. handle->ctrl.blkReg = length | handle->cfg.dmaBoundary;
  551. } else {
  552. blocks = length / handle->cfg.blockSize;
  553. handle->ctrl.blkReg = (blocks << 16) | handle->cfg.blockSize |
  554. handle->cfg.dmaBoundary;
  555. }
  556. if (handle->cfg.dma != SD_DMA_OFF) {
  557. /* For DMA target address setting, physical address should be used */
  558. mmio_write_32(handle->ctrl.sdRegBaseAddr + SD4_EMMC_TOP_SYSADDR_OFFSET,
  559. (uintptr_t)data);
  560. }
  561. return SD_OK;
  562. }
  563. #ifdef INCLUDE_EMMC_DRIVER_WRITE_CODE
  564. /*
  565. * function to write one block data directly to the
  566. * host controller's FIFO which is 1K uint8_t or
  567. * 2K uint8_t in size.
  568. * It is used in Non-DMA mode for data transmission.
  569. */
  570. int32_t chal_sd_write_buffer(CHAL_HANDLE *sd_handle, uint32_t length,
  571. uint8_t *data)
  572. {
  573. uint32_t i, leftOver = 0, blockSize, size, value = 0;
  574. struct sd_dev *handle;
  575. if (sd_handle == NULL)
  576. return SD_INVALID_HANDLE;
  577. handle = (struct sd_dev *) sd_handle;
  578. blockSize = handle->cfg.blockSize;
  579. if (length == 0)
  580. return SD_OK;
  581. /* PIO mode, push into fifo word by word */
  582. if (length >= blockSize) {
  583. size = blockSize;
  584. } else {
  585. size = ((length >> 2) << 2);
  586. leftOver = length % 4;
  587. }
  588. for (i = 0; i < size; i += 4) {
  589. value = *(uint32_t *)(data + i);
  590. mmio_write_32(handle->ctrl.sdRegBaseAddr +
  591. SD4_EMMC_TOP_BUFDAT_OFFSET, value);
  592. }
  593. /*
  594. * BUG ALERT:
  595. * This implementation has TWO issues that must be addressed before you
  596. * can safely INCLUDE_EMMC_DRIVER_WRITE_CODE.
  597. *
  598. * (1) For the last leftOver bytes, driver writes full word, which means
  599. * some of the eMMC content (i.e. "4 - leftOver" will be erroneously
  600. * overwritten).
  601. * (2) eMMC is a block device. What happens when less than a full block of
  602. * data is submitted???
  603. */
  604. if (leftOver > 0) {
  605. value = ((*(uint32_t *)(data + i)) << (4 - leftOver));
  606. mmio_write_32(handle->ctrl.sdRegBaseAddr +
  607. SD4_EMMC_TOP_BUFDAT_OFFSET, value);
  608. }
  609. return SD_OK;
  610. }
  611. #endif /* INCLUDE_EMMC_DRIVER_WRITE_CODE */
  612. /*
  613. * Function to read maximal one block data directly
  614. * from the data port of the host controller (FIFO). It is used
  615. * in Non-DMA mode for data transmission.
  616. */
  617. int32_t chal_sd_read_buffer(CHAL_HANDLE *sd_handle, uint32_t length,
  618. uint8_t *data)
  619. {
  620. uint32_t i, size, leftOver, blockSize, value;
  621. struct sd_dev *handle;
  622. if (sd_handle == NULL)
  623. return SD_INVALID_HANDLE;
  624. handle = (struct sd_dev *)sd_handle;
  625. value = 0;
  626. blockSize = handle->cfg.blockSize;
  627. /* PIO mode, extract fifo word by word */
  628. if (length >= blockSize) {
  629. size = blockSize;
  630. leftOver = 0;
  631. } else {
  632. leftOver = length % 4;
  633. size = ((length >> 2) << 2);
  634. }
  635. for (i = 0; i < size; i += 4) {
  636. value =
  637. mmio_read_32(handle->ctrl.sdRegBaseAddr +
  638. SD4_EMMC_TOP_BUFDAT_OFFSET);
  639. memcpy((void *)(data + i), &value, sizeof(uint32_t));
  640. }
  641. if (leftOver > 0) {
  642. value = mmio_read_32(handle->ctrl.sdRegBaseAddr +
  643. SD4_EMMC_TOP_BUFDAT_OFFSET);
  644. /*
  645. * Copy remaining non-full word bytes.
  646. * (We run ARM as Little Endian)
  647. */
  648. uint8_t j = 0;
  649. for (j = 0; j < leftOver; j++) {
  650. data[i + j] = (value >> (j * 8)) & 0xFF;
  651. }
  652. }
  653. return SD_OK;
  654. }
  655. /*
  656. * Resets both DAT or CMD line.
  657. */
  658. int32_t chal_sd_reset_line(CHAL_HANDLE *sd_handle, uint32_t line)
  659. {
  660. uint32_t control, flag;
  661. struct sd_dev *handle;
  662. if (sd_handle == NULL)
  663. return SD_INVALID_HANDLE;
  664. handle = (struct sd_dev *) sd_handle;
  665. flag = SD4_EMMC_TOP_CTRL1_CMDRST_MASK | SD4_EMMC_TOP_CTRL1_DATRST_MASK;
  666. if (flag != (line | flag))
  667. return SD_FAIL;
  668. control = mmio_read_32(handle->ctrl.sdRegBaseAddr +
  669. SD4_EMMC_TOP_CTRL1_OFFSET);
  670. control |= line;
  671. mmio_write_32(handle->ctrl.sdRegBaseAddr + SD4_EMMC_TOP_CTRL1_OFFSET,
  672. control);
  673. /* reset CMD and DATA line should always work, no need to timed out */
  674. do {
  675. control = mmio_read_32(handle->ctrl.sdRegBaseAddr +
  676. SD4_EMMC_TOP_CTRL1_OFFSET);
  677. } while (control & line);
  678. return SD_OK;
  679. }
  680. /*
  681. * Function to be called once a SD command is done to read
  682. * back it's response data.
  683. */
  684. int32_t chal_sd_get_response(CHAL_HANDLE *sd_handle, uint32_t *resp)
  685. {
  686. struct sd_dev *handle;
  687. if (sd_handle == NULL)
  688. return SD_INVALID_HANDLE;
  689. handle = (struct sd_dev *) sd_handle;
  690. resp[0] = mmio_read_32(handle->ctrl.sdRegBaseAddr +
  691. SD4_EMMC_TOP_RESP0_OFFSET);
  692. resp[1] = mmio_read_32(handle->ctrl.sdRegBaseAddr +
  693. SD4_EMMC_TOP_RESP2_OFFSET);
  694. resp[2] = mmio_read_32(handle->ctrl.sdRegBaseAddr +
  695. SD4_EMMC_TOP_RESP4_OFFSET);
  696. resp[3] = mmio_read_32(handle->ctrl.sdRegBaseAddr +
  697. SD4_EMMC_TOP_RESP6_OFFSET);
  698. return SD_OK;
  699. }
  700. /*
  701. * The function is called to clean all the pending interrupts.
  702. */
  703. int32_t chal_sd_clear_pending_irq(CHAL_HANDLE *sd_handle)
  704. {
  705. uint32_t status = SD_OK;
  706. struct sd_dev *handle;
  707. if (sd_handle == NULL)
  708. return SD_INVALID_HANDLE;
  709. handle = (struct sd_dev *)sd_handle;
  710. /* Make sure clean all interrupts */
  711. do {
  712. mmio_write_32(handle->ctrl.sdRegBaseAddr +
  713. SD4_EMMC_TOP_INTR_OFFSET, 0xFFFFFFFF);
  714. SD_US_DELAY(10);
  715. } while (mmio_read_32(handle->ctrl.sdRegBaseAddr +
  716. SD4_EMMC_TOP_INTR_OFFSET));
  717. return status;
  718. }
  719. /*
  720. * The function returns interrupt status register value.
  721. */
  722. int32_t chal_sd_get_irq_status(CHAL_HANDLE *sd_handle)
  723. {
  724. struct sd_dev *handle;
  725. if (sd_handle == NULL)
  726. return SD_INVALID_HANDLE;
  727. handle = (struct sd_dev *) sd_handle;
  728. return (mmio_read_32(handle->ctrl.sdRegBaseAddr +
  729. SD4_EMMC_TOP_INTR_OFFSET));
  730. }
  731. /*
  732. * The function clears interrupt(s) specified in the mask.
  733. */
  734. int32_t chal_sd_clear_irq(CHAL_HANDLE *sd_handle, uint32_t mask)
  735. {
  736. struct sd_dev *handle;
  737. if (sd_handle == NULL)
  738. return SD_INVALID_HANDLE;
  739. handle = (struct sd_dev *) sd_handle;
  740. /* Make sure clean masked interrupts */
  741. do {
  742. mmio_write_32(handle->ctrl.sdRegBaseAddr +
  743. SD4_EMMC_TOP_INTR_OFFSET, mask);
  744. SD_US_DELAY(10);
  745. } while (mask &
  746. mmio_read_32(handle->ctrl.sdRegBaseAddr +
  747. SD4_EMMC_TOP_INTR_OFFSET));
  748. return SD_OK;
  749. }
  750. /*
  751. * Description: The function configures the SD host controller.
  752. */
  753. int32_t chal_sd_config(CHAL_HANDLE *sd_handle, uint32_t speed, uint32_t retry,
  754. uint32_t boundary, uint32_t blkSize, uint32_t dma)
  755. {
  756. struct sd_dev *handle;
  757. if (sd_handle == NULL)
  758. return SD_INVALID_HANDLE;
  759. handle = (struct sd_dev *) sd_handle;
  760. handle->cfg.speedMode = speed;
  761. handle->cfg.retryLimit = retry;
  762. handle->cfg.dmaBoundary = boundary;
  763. handle->cfg.blockSize = blkSize;
  764. chal_sd_set_dma(sd_handle, dma);
  765. SD_US_DELAY(100);
  766. chal_sd_set_dma_boundary(handle, boundary);
  767. SD_US_DELAY(100);
  768. chal_sd_set_speed(sd_handle, speed);
  769. SD_US_DELAY(100);
  770. return SD_OK;
  771. }
  772. /*
  773. * Cleans up HC FIFO.
  774. */
  775. void chal_sd_dump_fifo(CHAL_HANDLE *sd_handle)
  776. {
  777. struct sd_dev *handle;
  778. if (sd_handle == NULL)
  779. return;
  780. handle = (struct sd_dev *)sd_handle;
  781. /* in case there still data in the host buffer */
  782. while (mmio_read_32(handle->ctrl.sdRegBaseAddr +
  783. SD4_EMMC_TOP_PSTATE_OFFSET) & 0x800) {
  784. mmio_read_32(handle->ctrl.sdRegBaseAddr +
  785. SD4_EMMC_TOP_BUFDAT_OFFSET);
  786. };
  787. }
  788. /*
  789. * Enable or disable a SD interrupt signal.
  790. */
  791. void chal_sd_set_irq_signal(CHAL_HANDLE *sd_handle, uint32_t mask,
  792. uint32_t state)
  793. {
  794. struct sd_dev *handle;
  795. if (sd_handle == NULL)
  796. return;
  797. handle = (struct sd_dev *)sd_handle;
  798. if (state)
  799. mmio_setbits_32(handle->ctrl.sdRegBaseAddr +
  800. SD4_EMMC_TOP_INTREN2_OFFSET, mask);
  801. else
  802. mmio_clrbits_32(handle->ctrl.sdRegBaseAddr +
  803. SD4_EMMC_TOP_INTREN2_OFFSET, mask);
  804. }