dw_mmc.c 12 KB

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