davinci_mmc.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Davinci MMC Controller Driver
  3. *
  4. * Copyright (C) 2010 Texas Instruments Incorporated
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <config.h>
  9. #include <common.h>
  10. #include <command.h>
  11. #include <errno.h>
  12. #include <mmc.h>
  13. #include <part.h>
  14. #include <malloc.h>
  15. #include <asm/io.h>
  16. #include <asm/arch/sdmmc_defs.h>
  17. #define DAVINCI_MAX_BLOCKS (32)
  18. #define WATCHDOG_COUNT (100000)
  19. #define get_val(addr) REG(addr)
  20. #define set_val(addr, val) REG(addr) = (val)
  21. #define set_bit(addr, val) set_val((addr), (get_val(addr) | (val)))
  22. #define clear_bit(addr, val) set_val((addr), (get_val(addr) & ~(val)))
  23. /* Set davinci clock prescalar value based on the required clock in HZ */
  24. static void dmmc_set_clock(struct mmc *mmc, uint clock)
  25. {
  26. struct davinci_mmc *host = mmc->priv;
  27. struct davinci_mmc_regs *regs = host->reg_base;
  28. uint clkrt, sysclk2, act_clock;
  29. if (clock < mmc->cfg->f_min)
  30. clock = mmc->cfg->f_min;
  31. if (clock > mmc->cfg->f_max)
  32. clock = mmc->cfg->f_max;
  33. set_val(&regs->mmcclk, 0);
  34. sysclk2 = host->input_clk;
  35. clkrt = (sysclk2 / (2 * clock)) - 1;
  36. /* Calculate the actual clock for the divider used */
  37. act_clock = (sysclk2 / (2 * (clkrt + 1)));
  38. /* Adjust divider if actual clock exceeds the required clock */
  39. if (act_clock > clock)
  40. clkrt++;
  41. /* check clock divider boundary and correct it */
  42. if (clkrt > 0xFF)
  43. clkrt = 0xFF;
  44. set_val(&regs->mmcclk, (clkrt | MMCCLK_CLKEN));
  45. }
  46. /* Status bit wait loop for MMCST1 */
  47. static int
  48. dmmc_wait_fifo_status(volatile struct davinci_mmc_regs *regs, uint status)
  49. {
  50. uint wdog = WATCHDOG_COUNT;
  51. while (--wdog && ((get_val(&regs->mmcst1) & status) != status))
  52. udelay(10);
  53. if (!(get_val(&regs->mmcctl) & MMCCTL_WIDTH_4_BIT))
  54. udelay(100);
  55. if (wdog == 0)
  56. return -ECOMM;
  57. return 0;
  58. }
  59. /* Busy bit wait loop for MMCST1 */
  60. static int dmmc_busy_wait(volatile struct davinci_mmc_regs *regs)
  61. {
  62. uint wdog = WATCHDOG_COUNT;
  63. while (--wdog && (get_val(&regs->mmcst1) & MMCST1_BUSY))
  64. udelay(10);
  65. if (wdog == 0)
  66. return -ECOMM;
  67. return 0;
  68. }
  69. /* Status bit wait loop for MMCST0 - Checks for error bits as well */
  70. static int dmmc_check_status(volatile struct davinci_mmc_regs *regs,
  71. uint *cur_st, uint st_ready, uint st_error)
  72. {
  73. uint wdog = WATCHDOG_COUNT;
  74. uint mmcstatus = *cur_st;
  75. while (wdog--) {
  76. if (mmcstatus & st_ready) {
  77. *cur_st = mmcstatus;
  78. mmcstatus = get_val(&regs->mmcst1);
  79. return 0;
  80. } else if (mmcstatus & st_error) {
  81. if (mmcstatus & MMCST0_TOUTRS)
  82. return -ETIMEDOUT;
  83. printf("[ ST0 ERROR %x]\n", mmcstatus);
  84. /*
  85. * Ignore CRC errors as some MMC cards fail to
  86. * initialize on DM365-EVM on the SD1 slot
  87. */
  88. if (mmcstatus & MMCST0_CRCRS)
  89. return 0;
  90. return -ECOMM;
  91. }
  92. udelay(10);
  93. mmcstatus = get_val(&regs->mmcst0);
  94. }
  95. printf("Status %x Timeout ST0:%x ST1:%x\n", st_ready, mmcstatus,
  96. get_val(&regs->mmcst1));
  97. return -ECOMM;
  98. }
  99. /*
  100. * Sends a command out on the bus. Takes the mmc pointer,
  101. * a command pointer, and an optional data pointer.
  102. */
  103. static int
  104. dmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
  105. {
  106. struct davinci_mmc *host = mmc->priv;
  107. volatile struct davinci_mmc_regs *regs = host->reg_base;
  108. uint mmcstatus, status_rdy, status_err;
  109. uint i, cmddata, bytes_left = 0;
  110. int fifo_words, fifo_bytes, err;
  111. char *data_buf = NULL;
  112. /* Clear status registers */
  113. mmcstatus = get_val(&regs->mmcst0);
  114. fifo_words = (host->version == MMC_CTLR_VERSION_2) ? 16 : 8;
  115. fifo_bytes = fifo_words << 2;
  116. /* Wait for any previous busy signal to be cleared */
  117. dmmc_busy_wait(regs);
  118. cmddata = cmd->cmdidx;
  119. cmddata |= MMCCMD_PPLEN;
  120. /* Send init clock for CMD0 */
  121. if (cmd->cmdidx == MMC_CMD_GO_IDLE_STATE)
  122. cmddata |= MMCCMD_INITCK;
  123. switch (cmd->resp_type) {
  124. case MMC_RSP_R1b:
  125. cmddata |= MMCCMD_BSYEXP;
  126. /* Fall-through */
  127. case MMC_RSP_R1: /* R1, R1b, R5, R6, R7 */
  128. cmddata |= MMCCMD_RSPFMT_R1567;
  129. break;
  130. case MMC_RSP_R2:
  131. cmddata |= MMCCMD_RSPFMT_R2;
  132. break;
  133. case MMC_RSP_R3: /* R3, R4 */
  134. cmddata |= MMCCMD_RSPFMT_R3;
  135. break;
  136. }
  137. set_val(&regs->mmcim, 0);
  138. if (data) {
  139. /* clear previous data transfer if any and set new one */
  140. bytes_left = (data->blocksize * data->blocks);
  141. /* Reset FIFO - Always use 32 byte fifo threshold */
  142. set_val(&regs->mmcfifoctl,
  143. (MMCFIFOCTL_FIFOLEV | MMCFIFOCTL_FIFORST));
  144. if (host->version == MMC_CTLR_VERSION_2)
  145. cmddata |= MMCCMD_DMATRIG;
  146. cmddata |= MMCCMD_WDATX;
  147. if (data->flags == MMC_DATA_READ) {
  148. set_val(&regs->mmcfifoctl, MMCFIFOCTL_FIFOLEV);
  149. } else if (data->flags == MMC_DATA_WRITE) {
  150. set_val(&regs->mmcfifoctl,
  151. (MMCFIFOCTL_FIFOLEV |
  152. MMCFIFOCTL_FIFODIR));
  153. cmddata |= MMCCMD_DTRW;
  154. }
  155. set_val(&regs->mmctod, 0xFFFF);
  156. set_val(&regs->mmcnblk, (data->blocks & MMCNBLK_NBLK_MASK));
  157. set_val(&regs->mmcblen, (data->blocksize & MMCBLEN_BLEN_MASK));
  158. if (data->flags == MMC_DATA_WRITE) {
  159. uint val;
  160. data_buf = (char *)data->src;
  161. /* For write, fill FIFO with data before issue of CMD */
  162. for (i = 0; (i < fifo_words) && bytes_left; i++) {
  163. memcpy((char *)&val, data_buf, 4);
  164. set_val(&regs->mmcdxr, val);
  165. data_buf += 4;
  166. bytes_left -= 4;
  167. }
  168. }
  169. } else {
  170. set_val(&regs->mmcblen, 0);
  171. set_val(&regs->mmcnblk, 0);
  172. }
  173. set_val(&regs->mmctor, 0x1FFF);
  174. /* Send the command */
  175. set_val(&regs->mmcarghl, cmd->cmdarg);
  176. set_val(&regs->mmccmd, cmddata);
  177. status_rdy = MMCST0_RSPDNE;
  178. status_err = (MMCST0_TOUTRS | MMCST0_TOUTRD |
  179. MMCST0_CRCWR | MMCST0_CRCRD);
  180. if (cmd->resp_type & MMC_RSP_CRC)
  181. status_err |= MMCST0_CRCRS;
  182. mmcstatus = get_val(&regs->mmcst0);
  183. err = dmmc_check_status(regs, &mmcstatus, status_rdy, status_err);
  184. if (err)
  185. return err;
  186. /* For R1b wait for busy done */
  187. if (cmd->resp_type == MMC_RSP_R1b)
  188. dmmc_busy_wait(regs);
  189. /* Collect response from controller for specific commands */
  190. if (mmcstatus & MMCST0_RSPDNE) {
  191. /* Copy the response to the response buffer */
  192. if (cmd->resp_type & MMC_RSP_136) {
  193. cmd->response[0] = get_val(&regs->mmcrsp67);
  194. cmd->response[1] = get_val(&regs->mmcrsp45);
  195. cmd->response[2] = get_val(&regs->mmcrsp23);
  196. cmd->response[3] = get_val(&regs->mmcrsp01);
  197. } else if (cmd->resp_type & MMC_RSP_PRESENT) {
  198. cmd->response[0] = get_val(&regs->mmcrsp67);
  199. }
  200. }
  201. if (data == NULL)
  202. return 0;
  203. if (data->flags == MMC_DATA_READ) {
  204. /* check for DATDNE along with DRRDY as the controller might
  205. * set the DATDNE without DRRDY for smaller transfers with
  206. * less than FIFO threshold bytes
  207. */
  208. status_rdy = MMCST0_DRRDY | MMCST0_DATDNE;
  209. status_err = MMCST0_TOUTRD | MMCST0_CRCRD;
  210. data_buf = data->dest;
  211. } else {
  212. status_rdy = MMCST0_DXRDY | MMCST0_DATDNE;
  213. status_err = MMCST0_CRCWR;
  214. }
  215. /* Wait until all of the blocks are transferred */
  216. while (bytes_left) {
  217. err = dmmc_check_status(regs, &mmcstatus, status_rdy,
  218. status_err);
  219. if (err)
  220. return err;
  221. if (data->flags == MMC_DATA_READ) {
  222. /*
  223. * MMC controller sets the Data receive ready bit
  224. * (DRRDY) in MMCST0 even before the entire FIFO is
  225. * full. This results in erratic behavior if we start
  226. * reading the FIFO soon after DRRDY. Wait for the
  227. * FIFO full bit in MMCST1 for proper FIFO clearing.
  228. */
  229. if (bytes_left > fifo_bytes)
  230. dmmc_wait_fifo_status(regs, 0x4a);
  231. else if (bytes_left == fifo_bytes) {
  232. dmmc_wait_fifo_status(regs, 0x40);
  233. if (cmd->cmdidx == MMC_CMD_SEND_EXT_CSD)
  234. udelay(600);
  235. }
  236. for (i = 0; bytes_left && (i < fifo_words); i++) {
  237. cmddata = get_val(&regs->mmcdrr);
  238. memcpy(data_buf, (char *)&cmddata, 4);
  239. data_buf += 4;
  240. bytes_left -= 4;
  241. }
  242. } else {
  243. /*
  244. * MMC controller sets the Data transmit ready bit
  245. * (DXRDY) in MMCST0 even before the entire FIFO is
  246. * empty. This results in erratic behavior if we start
  247. * writing the FIFO soon after DXRDY. Wait for the
  248. * FIFO empty bit in MMCST1 for proper FIFO clearing.
  249. */
  250. dmmc_wait_fifo_status(regs, MMCST1_FIFOEMP);
  251. for (i = 0; bytes_left && (i < fifo_words); i++) {
  252. memcpy((char *)&cmddata, data_buf, 4);
  253. set_val(&regs->mmcdxr, cmddata);
  254. data_buf += 4;
  255. bytes_left -= 4;
  256. }
  257. dmmc_busy_wait(regs);
  258. }
  259. }
  260. err = dmmc_check_status(regs, &mmcstatus, MMCST0_DATDNE, status_err);
  261. if (err)
  262. return err;
  263. return 0;
  264. }
  265. /* Initialize Davinci MMC controller */
  266. static int dmmc_init(struct mmc *mmc)
  267. {
  268. struct davinci_mmc *host = mmc->priv;
  269. struct davinci_mmc_regs *regs = host->reg_base;
  270. /* Clear status registers explicitly - soft reset doesn't clear it
  271. * If Uboot is invoked from UBL with SDMMC Support, the status
  272. * registers can have uncleared bits
  273. */
  274. get_val(&regs->mmcst0);
  275. get_val(&regs->mmcst1);
  276. /* Hold software reset */
  277. set_bit(&regs->mmcctl, MMCCTL_DATRST);
  278. set_bit(&regs->mmcctl, MMCCTL_CMDRST);
  279. udelay(10);
  280. set_val(&regs->mmcclk, 0x0);
  281. set_val(&regs->mmctor, 0x1FFF);
  282. set_val(&regs->mmctod, 0xFFFF);
  283. /* Clear software reset */
  284. clear_bit(&regs->mmcctl, MMCCTL_DATRST);
  285. clear_bit(&regs->mmcctl, MMCCTL_CMDRST);
  286. udelay(10);
  287. /* Reset FIFO - Always use the maximum fifo threshold */
  288. set_val(&regs->mmcfifoctl, (MMCFIFOCTL_FIFOLEV | MMCFIFOCTL_FIFORST));
  289. set_val(&regs->mmcfifoctl, MMCFIFOCTL_FIFOLEV);
  290. return 0;
  291. }
  292. /* Set buswidth or clock as indicated by the GENERIC_MMC framework */
  293. static int dmmc_set_ios(struct mmc *mmc)
  294. {
  295. struct davinci_mmc *host = mmc->priv;
  296. struct davinci_mmc_regs *regs = host->reg_base;
  297. /* Set the bus width */
  298. if (mmc->bus_width == 4)
  299. set_bit(&regs->mmcctl, MMCCTL_WIDTH_4_BIT);
  300. else
  301. clear_bit(&regs->mmcctl, MMCCTL_WIDTH_4_BIT);
  302. /* Set clock speed */
  303. if (mmc->clock)
  304. dmmc_set_clock(mmc, mmc->clock);
  305. return 0;
  306. }
  307. static const struct mmc_ops dmmc_ops = {
  308. .send_cmd = dmmc_send_cmd,
  309. .set_ios = dmmc_set_ios,
  310. .init = dmmc_init,
  311. };
  312. /* Called from board_mmc_init during startup. Can be called multiple times
  313. * depending on the number of slots available on board and controller
  314. */
  315. int davinci_mmc_init(bd_t *bis, struct davinci_mmc *host)
  316. {
  317. host->cfg.name = "davinci";
  318. host->cfg.ops = &dmmc_ops;
  319. host->cfg.f_min = 200000;
  320. host->cfg.f_max = 25000000;
  321. host->cfg.voltages = host->voltages;
  322. host->cfg.host_caps = host->host_caps;
  323. host->cfg.b_max = DAVINCI_MAX_BLOCKS;
  324. mmc_create(&host->cfg, host);
  325. return 0;
  326. }