dw_mmc.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * (C) Copyright 2012 SAMSUNG Electronics
  3. * Jaehoon Chung <jh80.chung@samsung.com>
  4. * Rajeshawari Shinde <rajeshwari.s@samsung.com>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <bouncebuf.h>
  9. #include <common.h>
  10. #include <malloc.h>
  11. #include <mmc.h>
  12. #include <dwmmc.h>
  13. #include <asm-generic/errno.h>
  14. #define PAGE_SIZE 4096
  15. static int dwmci_wait_reset(struct dwmci_host *host, u32 value)
  16. {
  17. unsigned long timeout = 1000;
  18. u32 ctrl;
  19. dwmci_writel(host, DWMCI_CTRL, value);
  20. while (timeout--) {
  21. ctrl = dwmci_readl(host, DWMCI_CTRL);
  22. if (!(ctrl & DWMCI_RESET_ALL))
  23. return 1;
  24. }
  25. return 0;
  26. }
  27. static void dwmci_set_idma_desc(struct dwmci_idmac *idmac,
  28. u32 desc0, u32 desc1, u32 desc2)
  29. {
  30. struct dwmci_idmac *desc = idmac;
  31. desc->flags = desc0;
  32. desc->cnt = desc1;
  33. desc->addr = desc2;
  34. desc->next_addr = (unsigned int)desc + sizeof(struct dwmci_idmac);
  35. }
  36. static void dwmci_prepare_data(struct dwmci_host *host,
  37. struct mmc_data *data,
  38. struct dwmci_idmac *cur_idmac,
  39. void *bounce_buffer)
  40. {
  41. unsigned long ctrl;
  42. unsigned int i = 0, flags, cnt, blk_cnt;
  43. ulong data_start, data_end;
  44. blk_cnt = data->blocks;
  45. dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
  46. data_start = (ulong)cur_idmac;
  47. dwmci_writel(host, DWMCI_DBADDR, (unsigned int)cur_idmac);
  48. do {
  49. flags = DWMCI_IDMAC_OWN | DWMCI_IDMAC_CH ;
  50. flags |= (i == 0) ? DWMCI_IDMAC_FS : 0;
  51. if (blk_cnt <= 8) {
  52. flags |= DWMCI_IDMAC_LD;
  53. cnt = data->blocksize * blk_cnt;
  54. } else
  55. cnt = data->blocksize * 8;
  56. dwmci_set_idma_desc(cur_idmac, flags, cnt,
  57. (u32)bounce_buffer + (i * PAGE_SIZE));
  58. if (blk_cnt <= 8)
  59. break;
  60. blk_cnt -= 8;
  61. cur_idmac++;
  62. i++;
  63. } while(1);
  64. data_end = (ulong)cur_idmac;
  65. flush_dcache_range(data_start, data_end + ARCH_DMA_MINALIGN);
  66. ctrl = dwmci_readl(host, DWMCI_CTRL);
  67. ctrl |= DWMCI_IDMAC_EN | DWMCI_DMA_EN;
  68. dwmci_writel(host, DWMCI_CTRL, ctrl);
  69. ctrl = dwmci_readl(host, DWMCI_BMOD);
  70. ctrl |= DWMCI_BMOD_IDMAC_FB | DWMCI_BMOD_IDMAC_EN;
  71. dwmci_writel(host, DWMCI_BMOD, ctrl);
  72. dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
  73. dwmci_writel(host, DWMCI_BYTCNT, data->blocksize * data->blocks);
  74. }
  75. static int dwmci_set_transfer_mode(struct dwmci_host *host,
  76. struct mmc_data *data)
  77. {
  78. unsigned long mode;
  79. mode = DWMCI_CMD_DATA_EXP;
  80. if (data->flags & MMC_DATA_WRITE)
  81. mode |= DWMCI_CMD_RW;
  82. return mode;
  83. }
  84. static int dwmci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
  85. struct mmc_data *data)
  86. {
  87. struct dwmci_host *host = mmc->priv;
  88. ALLOC_CACHE_ALIGN_BUFFER(struct dwmci_idmac, cur_idmac,
  89. data ? DIV_ROUND_UP(data->blocks, 8) : 0);
  90. int flags = 0, i;
  91. unsigned int timeout = 100000;
  92. u32 retry = 10000;
  93. u32 mask, ctrl;
  94. ulong start = get_timer(0);
  95. struct bounce_buffer bbstate;
  96. while (dwmci_readl(host, DWMCI_STATUS) & DWMCI_BUSY) {
  97. if (get_timer(start) > timeout) {
  98. printf("%s: Timeout on data busy\n", __func__);
  99. return TIMEOUT;
  100. }
  101. }
  102. dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_ALL);
  103. if (data) {
  104. if (data->flags == MMC_DATA_READ) {
  105. bounce_buffer_start(&bbstate, (void*)data->dest,
  106. data->blocksize *
  107. data->blocks, GEN_BB_WRITE);
  108. } else {
  109. bounce_buffer_start(&bbstate, (void*)data->src,
  110. data->blocksize *
  111. data->blocks, GEN_BB_READ);
  112. }
  113. dwmci_prepare_data(host, data, cur_idmac,
  114. bbstate.bounce_buffer);
  115. }
  116. dwmci_writel(host, DWMCI_CMDARG, cmd->cmdarg);
  117. if (data)
  118. flags = dwmci_set_transfer_mode(host, data);
  119. if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
  120. return -1;
  121. if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
  122. flags |= DWMCI_CMD_ABORT_STOP;
  123. else
  124. flags |= DWMCI_CMD_PRV_DAT_WAIT;
  125. if (cmd->resp_type & MMC_RSP_PRESENT) {
  126. flags |= DWMCI_CMD_RESP_EXP;
  127. if (cmd->resp_type & MMC_RSP_136)
  128. flags |= DWMCI_CMD_RESP_LENGTH;
  129. }
  130. if (cmd->resp_type & MMC_RSP_CRC)
  131. flags |= DWMCI_CMD_CHECK_CRC;
  132. flags |= (cmd->cmdidx | DWMCI_CMD_START | DWMCI_CMD_USE_HOLD_REG);
  133. debug("Sending CMD%d\n",cmd->cmdidx);
  134. dwmci_writel(host, DWMCI_CMD, flags);
  135. for (i = 0; i < retry; i++) {
  136. mask = dwmci_readl(host, DWMCI_RINTSTS);
  137. if (mask & DWMCI_INTMSK_CDONE) {
  138. if (!data)
  139. dwmci_writel(host, DWMCI_RINTSTS, mask);
  140. break;
  141. }
  142. }
  143. if (i == retry) {
  144. printf("%s: Timeout.\n", __func__);
  145. return TIMEOUT;
  146. }
  147. if (mask & DWMCI_INTMSK_RTO) {
  148. /*
  149. * Timeout here is not necessarily fatal. (e)MMC cards
  150. * will splat here when they receive CMD55 as they do
  151. * not support this command and that is exactly the way
  152. * to tell them apart from SD cards. Thus, this output
  153. * below shall be debug(). eMMC cards also do not favor
  154. * CMD8, please keep that in mind.
  155. */
  156. debug("%s: Response Timeout.\n", __func__);
  157. return TIMEOUT;
  158. } else if (mask & DWMCI_INTMSK_RE) {
  159. printf("%s: Response Error.\n", __func__);
  160. return -1;
  161. }
  162. if (cmd->resp_type & MMC_RSP_PRESENT) {
  163. if (cmd->resp_type & MMC_RSP_136) {
  164. cmd->response[0] = dwmci_readl(host, DWMCI_RESP3);
  165. cmd->response[1] = dwmci_readl(host, DWMCI_RESP2);
  166. cmd->response[2] = dwmci_readl(host, DWMCI_RESP1);
  167. cmd->response[3] = dwmci_readl(host, DWMCI_RESP0);
  168. } else {
  169. cmd->response[0] = dwmci_readl(host, DWMCI_RESP0);
  170. }
  171. }
  172. if (data) {
  173. do {
  174. mask = dwmci_readl(host, DWMCI_RINTSTS);
  175. if (mask & (DWMCI_DATA_ERR | DWMCI_DATA_TOUT)) {
  176. printf("%s: DATA ERROR!\n", __func__);
  177. return -1;
  178. }
  179. } while (!(mask & DWMCI_INTMSK_DTO));
  180. dwmci_writel(host, DWMCI_RINTSTS, mask);
  181. ctrl = dwmci_readl(host, DWMCI_CTRL);
  182. ctrl &= ~(DWMCI_DMA_EN);
  183. dwmci_writel(host, DWMCI_CTRL, ctrl);
  184. bounce_buffer_stop(&bbstate);
  185. }
  186. udelay(100);
  187. return 0;
  188. }
  189. static int dwmci_setup_bus(struct dwmci_host *host, u32 freq)
  190. {
  191. u32 div, status;
  192. int timeout = 10000;
  193. unsigned long sclk;
  194. if ((freq == host->clock) || (freq == 0))
  195. return 0;
  196. /*
  197. * If host->get_mmc_clk isn't defined,
  198. * then assume that host->bus_hz is source clock value.
  199. * host->bus_hz should be set by user.
  200. */
  201. if (host->get_mmc_clk)
  202. sclk = host->get_mmc_clk(host);
  203. else if (host->bus_hz)
  204. sclk = host->bus_hz;
  205. else {
  206. printf("%s: Didn't get source clock value.\n", __func__);
  207. return -EINVAL;
  208. }
  209. if (sclk == freq)
  210. div = 0; /* bypass mode */
  211. else
  212. div = DIV_ROUND_UP(sclk, 2 * freq);
  213. dwmci_writel(host, DWMCI_CLKENA, 0);
  214. dwmci_writel(host, DWMCI_CLKSRC, 0);
  215. dwmci_writel(host, DWMCI_CLKDIV, div);
  216. dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
  217. DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
  218. do {
  219. status = dwmci_readl(host, DWMCI_CMD);
  220. if (timeout-- < 0) {
  221. printf("%s: Timeout!\n", __func__);
  222. return -ETIMEDOUT;
  223. }
  224. } while (status & DWMCI_CMD_START);
  225. dwmci_writel(host, DWMCI_CLKENA, DWMCI_CLKEN_ENABLE |
  226. DWMCI_CLKEN_LOW_PWR);
  227. dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
  228. DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
  229. timeout = 10000;
  230. do {
  231. status = dwmci_readl(host, DWMCI_CMD);
  232. if (timeout-- < 0) {
  233. printf("%s: Timeout!\n", __func__);
  234. return -ETIMEDOUT;
  235. }
  236. } while (status & DWMCI_CMD_START);
  237. host->clock = freq;
  238. return 0;
  239. }
  240. static void dwmci_set_ios(struct mmc *mmc)
  241. {
  242. struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
  243. u32 ctype, regs;
  244. debug("Buswidth = %d, clock: %d\n", mmc->bus_width, mmc->clock);
  245. dwmci_setup_bus(host, mmc->clock);
  246. switch (mmc->bus_width) {
  247. case 8:
  248. ctype = DWMCI_CTYPE_8BIT;
  249. break;
  250. case 4:
  251. ctype = DWMCI_CTYPE_4BIT;
  252. break;
  253. default:
  254. ctype = DWMCI_CTYPE_1BIT;
  255. break;
  256. }
  257. dwmci_writel(host, DWMCI_CTYPE, ctype);
  258. regs = dwmci_readl(host, DWMCI_UHS_REG);
  259. if (mmc->card_caps & MMC_MODE_DDR_52MHz)
  260. regs |= DWMCI_DDR_MODE;
  261. else
  262. regs &= DWMCI_DDR_MODE;
  263. dwmci_writel(host, DWMCI_UHS_REG, regs);
  264. if (host->clksel)
  265. host->clksel(host);
  266. }
  267. static int dwmci_init(struct mmc *mmc)
  268. {
  269. struct dwmci_host *host = mmc->priv;
  270. if (host->board_init)
  271. host->board_init(host);
  272. dwmci_writel(host, DWMCI_PWREN, 1);
  273. if (!dwmci_wait_reset(host, DWMCI_RESET_ALL)) {
  274. printf("%s[%d] Fail-reset!!\n", __func__, __LINE__);
  275. return -1;
  276. }
  277. /* Enumerate at 400KHz */
  278. dwmci_setup_bus(host, mmc->cfg->f_min);
  279. dwmci_writel(host, DWMCI_RINTSTS, 0xFFFFFFFF);
  280. dwmci_writel(host, DWMCI_INTMASK, 0);
  281. dwmci_writel(host, DWMCI_TMOUT, 0xFFFFFFFF);
  282. dwmci_writel(host, DWMCI_IDINTEN, 0);
  283. dwmci_writel(host, DWMCI_BMOD, 1);
  284. if (host->fifoth_val) {
  285. dwmci_writel(host, DWMCI_FIFOTH, host->fifoth_val);
  286. }
  287. dwmci_writel(host, DWMCI_CLKENA, 0);
  288. dwmci_writel(host, DWMCI_CLKSRC, 0);
  289. return 0;
  290. }
  291. static const struct mmc_ops dwmci_ops = {
  292. .send_cmd = dwmci_send_cmd,
  293. .set_ios = dwmci_set_ios,
  294. .init = dwmci_init,
  295. };
  296. int add_dwmci(struct dwmci_host *host, u32 max_clk, u32 min_clk)
  297. {
  298. host->cfg.name = host->name;
  299. host->cfg.ops = &dwmci_ops;
  300. host->cfg.f_min = min_clk;
  301. host->cfg.f_max = max_clk;
  302. host->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
  303. host->cfg.host_caps = host->caps;
  304. if (host->buswidth == 8) {
  305. host->cfg.host_caps |= MMC_MODE_8BIT;
  306. host->cfg.host_caps &= ~MMC_MODE_4BIT;
  307. } else {
  308. host->cfg.host_caps |= MMC_MODE_4BIT;
  309. host->cfg.host_caps &= ~MMC_MODE_8BIT;
  310. }
  311. host->cfg.host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz | MMC_MODE_HC;
  312. host->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  313. host->mmc = mmc_create(&host->cfg, host);
  314. if (host->mmc == NULL)
  315. return -1;
  316. return 0;
  317. }