dw_mmc.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 = (struct dwmci_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("Timeout on data busy\n");
  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. return TIMEOUT;
  145. if (mask & DWMCI_INTMSK_RTO) {
  146. debug("Response Timeout..\n");
  147. return TIMEOUT;
  148. } else if (mask & DWMCI_INTMSK_RE) {
  149. debug("Response Error..\n");
  150. return -1;
  151. }
  152. if (cmd->resp_type & MMC_RSP_PRESENT) {
  153. if (cmd->resp_type & MMC_RSP_136) {
  154. cmd->response[0] = dwmci_readl(host, DWMCI_RESP3);
  155. cmd->response[1] = dwmci_readl(host, DWMCI_RESP2);
  156. cmd->response[2] = dwmci_readl(host, DWMCI_RESP1);
  157. cmd->response[3] = dwmci_readl(host, DWMCI_RESP0);
  158. } else {
  159. cmd->response[0] = dwmci_readl(host, DWMCI_RESP0);
  160. }
  161. }
  162. if (data) {
  163. do {
  164. mask = dwmci_readl(host, DWMCI_RINTSTS);
  165. if (mask & (DWMCI_DATA_ERR | DWMCI_DATA_TOUT)) {
  166. debug("DATA ERROR!\n");
  167. return -1;
  168. }
  169. } while (!(mask & DWMCI_INTMSK_DTO));
  170. dwmci_writel(host, DWMCI_RINTSTS, mask);
  171. ctrl = dwmci_readl(host, DWMCI_CTRL);
  172. ctrl &= ~(DWMCI_DMA_EN);
  173. dwmci_writel(host, DWMCI_CTRL, ctrl);
  174. bounce_buffer_stop(&bbstate);
  175. }
  176. udelay(100);
  177. return 0;
  178. }
  179. static int dwmci_setup_bus(struct dwmci_host *host, u32 freq)
  180. {
  181. u32 div, status;
  182. int timeout = 10000;
  183. unsigned long sclk;
  184. if ((freq == host->clock) || (freq == 0))
  185. return 0;
  186. /*
  187. * If host->get_mmc_clk didn't define,
  188. * then assume that host->bus_hz is source clock value.
  189. * host->bus_hz should be set from user.
  190. */
  191. if (host->get_mmc_clk)
  192. sclk = host->get_mmc_clk(host);
  193. else if (host->bus_hz)
  194. sclk = host->bus_hz;
  195. else {
  196. printf("Didn't get source clock value..\n");
  197. return -EINVAL;
  198. }
  199. div = DIV_ROUND_UP(sclk, 2 * freq);
  200. dwmci_writel(host, DWMCI_CLKENA, 0);
  201. dwmci_writel(host, DWMCI_CLKSRC, 0);
  202. dwmci_writel(host, DWMCI_CLKDIV, div);
  203. dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
  204. DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
  205. do {
  206. status = dwmci_readl(host, DWMCI_CMD);
  207. if (timeout-- < 0) {
  208. printf("TIMEOUT error!!\n");
  209. return -ETIMEDOUT;
  210. }
  211. } while (status & DWMCI_CMD_START);
  212. dwmci_writel(host, DWMCI_CLKENA, DWMCI_CLKEN_ENABLE |
  213. DWMCI_CLKEN_LOW_PWR);
  214. dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
  215. DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
  216. timeout = 10000;
  217. do {
  218. status = dwmci_readl(host, DWMCI_CMD);
  219. if (timeout-- < 0) {
  220. printf("TIMEOUT error!!\n");
  221. return -ETIMEDOUT;
  222. }
  223. } while (status & DWMCI_CMD_START);
  224. host->clock = freq;
  225. return 0;
  226. }
  227. static void dwmci_set_ios(struct mmc *mmc)
  228. {
  229. struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
  230. u32 ctype;
  231. debug("Buswidth = %d, clock: %d\n",mmc->bus_width, mmc->clock);
  232. dwmci_setup_bus(host, mmc->clock);
  233. switch (mmc->bus_width) {
  234. case 8:
  235. ctype = DWMCI_CTYPE_8BIT;
  236. break;
  237. case 4:
  238. ctype = DWMCI_CTYPE_4BIT;
  239. break;
  240. default:
  241. ctype = DWMCI_CTYPE_1BIT;
  242. break;
  243. }
  244. dwmci_writel(host, DWMCI_CTYPE, ctype);
  245. if (host->clksel)
  246. host->clksel(host);
  247. }
  248. static int dwmci_init(struct mmc *mmc)
  249. {
  250. struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
  251. if (host->board_init)
  252. host->board_init(host);
  253. dwmci_writel(host, DWMCI_PWREN, 1);
  254. if (!dwmci_wait_reset(host, DWMCI_RESET_ALL)) {
  255. debug("%s[%d] Fail-reset!!\n",__func__,__LINE__);
  256. return -1;
  257. }
  258. /* Enumerate at 400KHz */
  259. dwmci_setup_bus(host, mmc->f_min);
  260. dwmci_writel(host, DWMCI_RINTSTS, 0xFFFFFFFF);
  261. dwmci_writel(host, DWMCI_INTMASK, 0);
  262. dwmci_writel(host, DWMCI_TMOUT, 0xFFFFFFFF);
  263. dwmci_writel(host, DWMCI_IDINTEN, 0);
  264. dwmci_writel(host, DWMCI_BMOD, 1);
  265. if (host->fifoth_val) {
  266. dwmci_writel(host, DWMCI_FIFOTH, host->fifoth_val);
  267. }
  268. dwmci_writel(host, DWMCI_CLKENA, 0);
  269. dwmci_writel(host, DWMCI_CLKSRC, 0);
  270. return 0;
  271. }
  272. static const struct mmc_ops dwmci_ops = {
  273. .send_cmd = dwmci_send_cmd,
  274. .set_ios = dwmci_set_ios,
  275. .init = dwmci_init,
  276. };
  277. int add_dwmci(struct dwmci_host *host, u32 max_clk, u32 min_clk)
  278. {
  279. struct mmc *mmc;
  280. int err = 0;
  281. mmc = calloc(sizeof(struct mmc), 1);
  282. if (!mmc) {
  283. printf("mmc calloc fail!\n");
  284. return -1;
  285. }
  286. mmc->priv = host;
  287. host->mmc = mmc;
  288. sprintf(mmc->name, "%s", host->name);
  289. mmc->ops = &dwmci_ops;
  290. mmc->f_min = min_clk;
  291. mmc->f_max = max_clk;
  292. mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
  293. mmc->host_caps = host->caps;
  294. if (host->buswidth == 8) {
  295. mmc->host_caps |= MMC_MODE_8BIT;
  296. mmc->host_caps &= ~MMC_MODE_4BIT;
  297. } else {
  298. mmc->host_caps |= MMC_MODE_4BIT;
  299. mmc->host_caps &= ~MMC_MODE_8BIT;
  300. }
  301. mmc->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz | MMC_MODE_HC;
  302. err = mmc_register(mmc);
  303. return err;
  304. }