dw_mmc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2012 SAMSUNG Electronics
  4. * Jaehoon Chung <jh80.chung@samsung.com>
  5. * Rajeshawari Shinde <rajeshwari.s@samsung.com>
  6. */
  7. #include <bouncebuf.h>
  8. #include <common.h>
  9. #include <errno.h>
  10. #include <malloc.h>
  11. #include <memalign.h>
  12. #include <mmc.h>
  13. #include <dwmmc.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 = (ulong)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, (ulong)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. (ulong)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_data_transfer(struct dwmci_host *host, struct mmc_data *data)
  76. {
  77. int ret = 0;
  78. u32 timeout = 240000;
  79. u32 mask, size, i, len = 0;
  80. u32 *buf = NULL;
  81. ulong start = get_timer(0);
  82. u32 fifo_depth = (((host->fifoth_val & RX_WMARK_MASK) >>
  83. RX_WMARK_SHIFT) + 1) * 2;
  84. size = data->blocksize * data->blocks / 4;
  85. if (data->flags == MMC_DATA_READ)
  86. buf = (unsigned int *)data->dest;
  87. else
  88. buf = (unsigned int *)data->src;
  89. for (;;) {
  90. mask = dwmci_readl(host, DWMCI_RINTSTS);
  91. /* Error during data transfer. */
  92. if (mask & (DWMCI_DATA_ERR | DWMCI_DATA_TOUT)) {
  93. debug("%s: DATA ERROR!\n", __func__);
  94. ret = -EINVAL;
  95. break;
  96. }
  97. if (host->fifo_mode && size) {
  98. len = 0;
  99. if (data->flags == MMC_DATA_READ &&
  100. (mask & DWMCI_INTMSK_RXDR)) {
  101. while (size) {
  102. len = dwmci_readl(host, DWMCI_STATUS);
  103. len = (len >> DWMCI_FIFO_SHIFT) &
  104. DWMCI_FIFO_MASK;
  105. len = min(size, len);
  106. for (i = 0; i < len; i++)
  107. *buf++ =
  108. dwmci_readl(host, DWMCI_DATA);
  109. size = size > len ? (size - len) : 0;
  110. }
  111. dwmci_writel(host, DWMCI_RINTSTS,
  112. DWMCI_INTMSK_RXDR);
  113. } else if (data->flags == MMC_DATA_WRITE &&
  114. (mask & DWMCI_INTMSK_TXDR)) {
  115. while (size) {
  116. len = dwmci_readl(host, DWMCI_STATUS);
  117. len = fifo_depth - ((len >>
  118. DWMCI_FIFO_SHIFT) &
  119. DWMCI_FIFO_MASK);
  120. len = min(size, len);
  121. for (i = 0; i < len; i++)
  122. dwmci_writel(host, DWMCI_DATA,
  123. *buf++);
  124. size = size > len ? (size - len) : 0;
  125. }
  126. dwmci_writel(host, DWMCI_RINTSTS,
  127. DWMCI_INTMSK_TXDR);
  128. }
  129. }
  130. /* Data arrived correctly. */
  131. if (mask & DWMCI_INTMSK_DTO) {
  132. ret = 0;
  133. break;
  134. }
  135. /* Check for timeout. */
  136. if (get_timer(start) > timeout) {
  137. debug("%s: Timeout waiting for data!\n",
  138. __func__);
  139. ret = -ETIMEDOUT;
  140. break;
  141. }
  142. }
  143. dwmci_writel(host, DWMCI_RINTSTS, mask);
  144. return ret;
  145. }
  146. static int dwmci_set_transfer_mode(struct dwmci_host *host,
  147. struct mmc_data *data)
  148. {
  149. unsigned long mode;
  150. mode = DWMCI_CMD_DATA_EXP;
  151. if (data->flags & MMC_DATA_WRITE)
  152. mode |= DWMCI_CMD_RW;
  153. return mode;
  154. }
  155. #ifdef CONFIG_DM_MMC
  156. static int dwmci_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
  157. struct mmc_data *data)
  158. {
  159. struct mmc *mmc = mmc_get_mmc_dev(dev);
  160. #else
  161. static int dwmci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
  162. struct mmc_data *data)
  163. {
  164. #endif
  165. struct dwmci_host *host = mmc->priv;
  166. ALLOC_CACHE_ALIGN_BUFFER(struct dwmci_idmac, cur_idmac,
  167. data ? DIV_ROUND_UP(data->blocks, 8) : 0);
  168. int ret = 0, flags = 0, i;
  169. unsigned int timeout = 500;
  170. u32 retry = 100000;
  171. u32 mask, ctrl;
  172. ulong start = get_timer(0);
  173. struct bounce_buffer bbstate;
  174. while (dwmci_readl(host, DWMCI_STATUS) & DWMCI_BUSY) {
  175. if (get_timer(start) > timeout) {
  176. debug("%s: Timeout on data busy\n", __func__);
  177. return -ETIMEDOUT;
  178. }
  179. }
  180. dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_ALL);
  181. if (data) {
  182. if (host->fifo_mode) {
  183. dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
  184. dwmci_writel(host, DWMCI_BYTCNT,
  185. data->blocksize * data->blocks);
  186. dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
  187. } else {
  188. if (data->flags == MMC_DATA_READ) {
  189. bounce_buffer_start(&bbstate, (void*)data->dest,
  190. data->blocksize *
  191. data->blocks, GEN_BB_WRITE);
  192. } else {
  193. bounce_buffer_start(&bbstate, (void*)data->src,
  194. data->blocksize *
  195. data->blocks, GEN_BB_READ);
  196. }
  197. dwmci_prepare_data(host, data, cur_idmac,
  198. bbstate.bounce_buffer);
  199. }
  200. }
  201. dwmci_writel(host, DWMCI_CMDARG, cmd->cmdarg);
  202. if (data)
  203. flags = dwmci_set_transfer_mode(host, data);
  204. if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
  205. return -1;
  206. if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
  207. flags |= DWMCI_CMD_ABORT_STOP;
  208. else
  209. flags |= DWMCI_CMD_PRV_DAT_WAIT;
  210. if (cmd->resp_type & MMC_RSP_PRESENT) {
  211. flags |= DWMCI_CMD_RESP_EXP;
  212. if (cmd->resp_type & MMC_RSP_136)
  213. flags |= DWMCI_CMD_RESP_LENGTH;
  214. }
  215. if (cmd->resp_type & MMC_RSP_CRC)
  216. flags |= DWMCI_CMD_CHECK_CRC;
  217. flags |= (cmd->cmdidx | DWMCI_CMD_START | DWMCI_CMD_USE_HOLD_REG);
  218. debug("Sending CMD%d\n",cmd->cmdidx);
  219. dwmci_writel(host, DWMCI_CMD, flags);
  220. for (i = 0; i < retry; i++) {
  221. mask = dwmci_readl(host, DWMCI_RINTSTS);
  222. if (mask & DWMCI_INTMSK_CDONE) {
  223. if (!data)
  224. dwmci_writel(host, DWMCI_RINTSTS, mask);
  225. break;
  226. }
  227. }
  228. if (i == retry) {
  229. debug("%s: Timeout.\n", __func__);
  230. return -ETIMEDOUT;
  231. }
  232. if (mask & DWMCI_INTMSK_RTO) {
  233. /*
  234. * Timeout here is not necessarily fatal. (e)MMC cards
  235. * will splat here when they receive CMD55 as they do
  236. * not support this command and that is exactly the way
  237. * to tell them apart from SD cards. Thus, this output
  238. * below shall be debug(). eMMC cards also do not favor
  239. * CMD8, please keep that in mind.
  240. */
  241. debug("%s: Response Timeout.\n", __func__);
  242. return -ETIMEDOUT;
  243. } else if (mask & DWMCI_INTMSK_RE) {
  244. debug("%s: Response Error.\n", __func__);
  245. return -EIO;
  246. }
  247. if (cmd->resp_type & MMC_RSP_PRESENT) {
  248. if (cmd->resp_type & MMC_RSP_136) {
  249. cmd->response[0] = dwmci_readl(host, DWMCI_RESP3);
  250. cmd->response[1] = dwmci_readl(host, DWMCI_RESP2);
  251. cmd->response[2] = dwmci_readl(host, DWMCI_RESP1);
  252. cmd->response[3] = dwmci_readl(host, DWMCI_RESP0);
  253. } else {
  254. cmd->response[0] = dwmci_readl(host, DWMCI_RESP0);
  255. }
  256. }
  257. if (data) {
  258. ret = dwmci_data_transfer(host, data);
  259. /* only dma mode need it */
  260. if (!host->fifo_mode) {
  261. ctrl = dwmci_readl(host, DWMCI_CTRL);
  262. ctrl &= ~(DWMCI_DMA_EN);
  263. dwmci_writel(host, DWMCI_CTRL, ctrl);
  264. bounce_buffer_stop(&bbstate);
  265. }
  266. }
  267. udelay(100);
  268. return ret;
  269. }
  270. static int dwmci_setup_bus(struct dwmci_host *host, u32 freq)
  271. {
  272. u32 div, status;
  273. int timeout = 10000;
  274. unsigned long sclk;
  275. if ((freq == host->clock) || (freq == 0))
  276. return 0;
  277. /*
  278. * If host->get_mmc_clk isn't defined,
  279. * then assume that host->bus_hz is source clock value.
  280. * host->bus_hz should be set by user.
  281. */
  282. if (host->get_mmc_clk)
  283. sclk = host->get_mmc_clk(host, freq);
  284. else if (host->bus_hz)
  285. sclk = host->bus_hz;
  286. else {
  287. debug("%s: Didn't get source clock value.\n", __func__);
  288. return -EINVAL;
  289. }
  290. if (sclk == freq)
  291. div = 0; /* bypass mode */
  292. else
  293. div = DIV_ROUND_UP(sclk, 2 * freq);
  294. dwmci_writel(host, DWMCI_CLKENA, 0);
  295. dwmci_writel(host, DWMCI_CLKSRC, 0);
  296. dwmci_writel(host, DWMCI_CLKDIV, div);
  297. dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
  298. DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
  299. do {
  300. status = dwmci_readl(host, DWMCI_CMD);
  301. if (timeout-- < 0) {
  302. debug("%s: Timeout!\n", __func__);
  303. return -ETIMEDOUT;
  304. }
  305. } while (status & DWMCI_CMD_START);
  306. dwmci_writel(host, DWMCI_CLKENA, DWMCI_CLKEN_ENABLE |
  307. DWMCI_CLKEN_LOW_PWR);
  308. dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
  309. DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
  310. timeout = 10000;
  311. do {
  312. status = dwmci_readl(host, DWMCI_CMD);
  313. if (timeout-- < 0) {
  314. debug("%s: Timeout!\n", __func__);
  315. return -ETIMEDOUT;
  316. }
  317. } while (status & DWMCI_CMD_START);
  318. host->clock = freq;
  319. return 0;
  320. }
  321. #ifdef CONFIG_DM_MMC
  322. static int dwmci_set_ios(struct udevice *dev)
  323. {
  324. struct mmc *mmc = mmc_get_mmc_dev(dev);
  325. #else
  326. static int dwmci_set_ios(struct mmc *mmc)
  327. {
  328. #endif
  329. struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
  330. u32 ctype, regs;
  331. debug("Buswidth = %d, clock: %d\n", mmc->bus_width, mmc->clock);
  332. dwmci_setup_bus(host, mmc->clock);
  333. switch (mmc->bus_width) {
  334. case 8:
  335. ctype = DWMCI_CTYPE_8BIT;
  336. break;
  337. case 4:
  338. ctype = DWMCI_CTYPE_4BIT;
  339. break;
  340. default:
  341. ctype = DWMCI_CTYPE_1BIT;
  342. break;
  343. }
  344. dwmci_writel(host, DWMCI_CTYPE, ctype);
  345. regs = dwmci_readl(host, DWMCI_UHS_REG);
  346. if (mmc->ddr_mode)
  347. regs |= DWMCI_DDR_MODE;
  348. else
  349. regs &= ~DWMCI_DDR_MODE;
  350. dwmci_writel(host, DWMCI_UHS_REG, regs);
  351. if (host->clksel)
  352. host->clksel(host);
  353. return 0;
  354. }
  355. static int dwmci_init(struct mmc *mmc)
  356. {
  357. struct dwmci_host *host = mmc->priv;
  358. if (host->board_init)
  359. host->board_init(host);
  360. dwmci_writel(host, DWMCI_PWREN, 1);
  361. if (!dwmci_wait_reset(host, DWMCI_RESET_ALL)) {
  362. debug("%s[%d] Fail-reset!!\n", __func__, __LINE__);
  363. return -EIO;
  364. }
  365. /* Enumerate at 400KHz */
  366. dwmci_setup_bus(host, mmc->cfg->f_min);
  367. dwmci_writel(host, DWMCI_RINTSTS, 0xFFFFFFFF);
  368. dwmci_writel(host, DWMCI_INTMASK, 0);
  369. dwmci_writel(host, DWMCI_TMOUT, 0xFFFFFFFF);
  370. dwmci_writel(host, DWMCI_IDINTEN, 0);
  371. dwmci_writel(host, DWMCI_BMOD, 1);
  372. if (!host->fifoth_val) {
  373. uint32_t fifo_size;
  374. fifo_size = dwmci_readl(host, DWMCI_FIFOTH);
  375. fifo_size = ((fifo_size & RX_WMARK_MASK) >> RX_WMARK_SHIFT) + 1;
  376. host->fifoth_val = MSIZE(0x2) | RX_WMARK(fifo_size / 2 - 1) |
  377. TX_WMARK(fifo_size / 2);
  378. }
  379. dwmci_writel(host, DWMCI_FIFOTH, host->fifoth_val);
  380. dwmci_writel(host, DWMCI_CLKENA, 0);
  381. dwmci_writel(host, DWMCI_CLKSRC, 0);
  382. return 0;
  383. }
  384. #ifdef CONFIG_DM_MMC
  385. int dwmci_probe(struct udevice *dev)
  386. {
  387. struct mmc *mmc = mmc_get_mmc_dev(dev);
  388. return dwmci_init(mmc);
  389. }
  390. const struct dm_mmc_ops dm_dwmci_ops = {
  391. .send_cmd = dwmci_send_cmd,
  392. .set_ios = dwmci_set_ios,
  393. };
  394. #else
  395. static const struct mmc_ops dwmci_ops = {
  396. .send_cmd = dwmci_send_cmd,
  397. .set_ios = dwmci_set_ios,
  398. .init = dwmci_init,
  399. };
  400. #endif
  401. void dwmci_setup_cfg(struct mmc_config *cfg, struct dwmci_host *host,
  402. u32 max_clk, u32 min_clk)
  403. {
  404. cfg->name = host->name;
  405. #ifndef CONFIG_DM_MMC
  406. cfg->ops = &dwmci_ops;
  407. #endif
  408. cfg->f_min = min_clk;
  409. cfg->f_max = max_clk;
  410. cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
  411. cfg->host_caps = host->caps;
  412. if (host->buswidth == 8) {
  413. cfg->host_caps |= MMC_MODE_8BIT;
  414. cfg->host_caps &= ~MMC_MODE_4BIT;
  415. } else {
  416. cfg->host_caps |= MMC_MODE_4BIT;
  417. cfg->host_caps &= ~MMC_MODE_8BIT;
  418. }
  419. cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz;
  420. cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  421. }
  422. #ifdef CONFIG_BLK
  423. int dwmci_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg)
  424. {
  425. return mmc_bind(dev, mmc, cfg);
  426. }
  427. #else
  428. int add_dwmci(struct dwmci_host *host, u32 max_clk, u32 min_clk)
  429. {
  430. dwmci_setup_cfg(&host->cfg, host, max_clk, min_clk);
  431. host->mmc = mmc_create(&host->cfg, host);
  432. if (host->mmc == NULL)
  433. return -1;
  434. return 0;
  435. }
  436. #endif