tmio-common.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. /*
  2. * Copyright (C) 2016 Socionext Inc.
  3. * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <clk.h>
  9. #include <fdtdec.h>
  10. #include <mmc.h>
  11. #include <dm.h>
  12. #include <dm/pinctrl.h>
  13. #include <linux/compat.h>
  14. #include <linux/dma-direction.h>
  15. #include <linux/io.h>
  16. #include <linux/sizes.h>
  17. #include <power/regulator.h>
  18. #include <asm/unaligned.h>
  19. #include "tmio-common.h"
  20. DECLARE_GLOBAL_DATA_PTR;
  21. static u64 tmio_sd_readq(struct tmio_sd_priv *priv, unsigned int reg)
  22. {
  23. return readq(priv->regbase + (reg << 1));
  24. }
  25. static void tmio_sd_writeq(struct tmio_sd_priv *priv,
  26. u64 val, unsigned int reg)
  27. {
  28. writeq(val, priv->regbase + (reg << 1));
  29. }
  30. static u16 tmio_sd_readw(struct tmio_sd_priv *priv, unsigned int reg)
  31. {
  32. return readw(priv->regbase + (reg >> 1));
  33. }
  34. static void tmio_sd_writew(struct tmio_sd_priv *priv,
  35. u16 val, unsigned int reg)
  36. {
  37. writew(val, priv->regbase + (reg >> 1));
  38. }
  39. u32 tmio_sd_readl(struct tmio_sd_priv *priv, unsigned int reg)
  40. {
  41. u32 val;
  42. if (priv->caps & TMIO_SD_CAP_64BIT)
  43. return readl(priv->regbase + (reg << 1));
  44. else if (priv->caps & TMIO_SD_CAP_16BIT) {
  45. val = readw(priv->regbase + (reg >> 1)) & 0xffff;
  46. if ((reg == TMIO_SD_RSP10) || (reg == TMIO_SD_RSP32) ||
  47. (reg == TMIO_SD_RSP54) || (reg == TMIO_SD_RSP76)) {
  48. val |= readw(priv->regbase + (reg >> 1) + 2) << 16;
  49. }
  50. return val;
  51. } else
  52. return readl(priv->regbase + reg);
  53. }
  54. void tmio_sd_writel(struct tmio_sd_priv *priv,
  55. u32 val, unsigned int reg)
  56. {
  57. if (priv->caps & TMIO_SD_CAP_64BIT)
  58. writel(val, priv->regbase + (reg << 1));
  59. else if (priv->caps & TMIO_SD_CAP_16BIT) {
  60. writew(val & 0xffff, priv->regbase + (reg >> 1));
  61. if (reg == TMIO_SD_INFO1 || reg == TMIO_SD_INFO1_MASK ||
  62. reg == TMIO_SD_INFO2 || reg == TMIO_SD_INFO2_MASK ||
  63. reg == TMIO_SD_ARG)
  64. writew(val >> 16, priv->regbase + (reg >> 1) + 2);
  65. } else
  66. writel(val, priv->regbase + reg);
  67. }
  68. static dma_addr_t __dma_map_single(void *ptr, size_t size,
  69. enum dma_data_direction dir)
  70. {
  71. unsigned long addr = (unsigned long)ptr;
  72. if (dir == DMA_FROM_DEVICE)
  73. invalidate_dcache_range(addr, addr + size);
  74. else
  75. flush_dcache_range(addr, addr + size);
  76. return addr;
  77. }
  78. static void __dma_unmap_single(dma_addr_t addr, size_t size,
  79. enum dma_data_direction dir)
  80. {
  81. if (dir != DMA_TO_DEVICE)
  82. invalidate_dcache_range(addr, addr + size);
  83. }
  84. static int tmio_sd_check_error(struct udevice *dev)
  85. {
  86. struct tmio_sd_priv *priv = dev_get_priv(dev);
  87. u32 info2 = tmio_sd_readl(priv, TMIO_SD_INFO2);
  88. if (info2 & TMIO_SD_INFO2_ERR_RTO) {
  89. /*
  90. * TIMEOUT must be returned for unsupported command. Do not
  91. * display error log since this might be a part of sequence to
  92. * distinguish between SD and MMC.
  93. */
  94. return -ETIMEDOUT;
  95. }
  96. if (info2 & TMIO_SD_INFO2_ERR_TO) {
  97. dev_err(dev, "timeout error\n");
  98. return -ETIMEDOUT;
  99. }
  100. if (info2 & (TMIO_SD_INFO2_ERR_END | TMIO_SD_INFO2_ERR_CRC |
  101. TMIO_SD_INFO2_ERR_IDX)) {
  102. dev_err(dev, "communication out of sync\n");
  103. return -EILSEQ;
  104. }
  105. if (info2 & (TMIO_SD_INFO2_ERR_ILA | TMIO_SD_INFO2_ERR_ILR |
  106. TMIO_SD_INFO2_ERR_ILW)) {
  107. dev_err(dev, "illegal access\n");
  108. return -EIO;
  109. }
  110. return 0;
  111. }
  112. static int tmio_sd_wait_for_irq(struct udevice *dev, unsigned int reg,
  113. u32 flag)
  114. {
  115. struct tmio_sd_priv *priv = dev_get_priv(dev);
  116. long wait = 1000000;
  117. int ret;
  118. while (!(tmio_sd_readl(priv, reg) & flag)) {
  119. if (wait-- < 0) {
  120. dev_err(dev, "timeout\n");
  121. return -ETIMEDOUT;
  122. }
  123. ret = tmio_sd_check_error(dev);
  124. if (ret)
  125. return ret;
  126. udelay(1);
  127. }
  128. return 0;
  129. }
  130. #define tmio_pio_read_fifo(__width, __suffix) \
  131. static void tmio_pio_read_fifo_##__width(struct tmio_sd_priv *priv, \
  132. char *pbuf, uint blksz) \
  133. { \
  134. u##__width *buf = (u##__width *)pbuf; \
  135. int i; \
  136. \
  137. if (likely(IS_ALIGNED((uintptr_t)buf, ((__width) / 8)))) { \
  138. for (i = 0; i < blksz / ((__width) / 8); i++) { \
  139. *buf++ = tmio_sd_read##__suffix(priv, \
  140. TMIO_SD_BUF); \
  141. } \
  142. } else { \
  143. for (i = 0; i < blksz / ((__width) / 8); i++) { \
  144. u##__width data; \
  145. data = tmio_sd_read##__suffix(priv, \
  146. TMIO_SD_BUF); \
  147. put_unaligned(data, buf++); \
  148. } \
  149. } \
  150. }
  151. tmio_pio_read_fifo(64, q)
  152. tmio_pio_read_fifo(32, l)
  153. tmio_pio_read_fifo(16, w)
  154. static int tmio_sd_pio_read_one_block(struct udevice *dev, char *pbuf,
  155. uint blocksize)
  156. {
  157. struct tmio_sd_priv *priv = dev_get_priv(dev);
  158. int ret;
  159. /* wait until the buffer is filled with data */
  160. ret = tmio_sd_wait_for_irq(dev, TMIO_SD_INFO2,
  161. TMIO_SD_INFO2_BRE);
  162. if (ret)
  163. return ret;
  164. /*
  165. * Clear the status flag _before_ read the buffer out because
  166. * TMIO_SD_INFO2_BRE is edge-triggered, not level-triggered.
  167. */
  168. tmio_sd_writel(priv, 0, TMIO_SD_INFO2);
  169. if (priv->caps & TMIO_SD_CAP_64BIT)
  170. tmio_pio_read_fifo_64(priv, pbuf, blocksize);
  171. else if (priv->caps & TMIO_SD_CAP_16BIT)
  172. tmio_pio_read_fifo_16(priv, pbuf, blocksize);
  173. else
  174. tmio_pio_read_fifo_32(priv, pbuf, blocksize);
  175. return 0;
  176. }
  177. #define tmio_pio_write_fifo(__width, __suffix) \
  178. static void tmio_pio_write_fifo_##__width(struct tmio_sd_priv *priv, \
  179. const char *pbuf, uint blksz)\
  180. { \
  181. const u##__width *buf = (const u##__width *)pbuf; \
  182. int i; \
  183. \
  184. if (likely(IS_ALIGNED((uintptr_t)buf, ((__width) / 8)))) { \
  185. for (i = 0; i < blksz / ((__width) / 8); i++) { \
  186. tmio_sd_write##__suffix(priv, *buf++, \
  187. TMIO_SD_BUF); \
  188. } \
  189. } else { \
  190. for (i = 0; i < blksz / ((__width) / 8); i++) { \
  191. u##__width data = get_unaligned(buf++); \
  192. tmio_sd_write##__suffix(priv, data, \
  193. TMIO_SD_BUF); \
  194. } \
  195. } \
  196. }
  197. tmio_pio_write_fifo(64, q)
  198. tmio_pio_write_fifo(32, l)
  199. tmio_pio_write_fifo(16, w)
  200. static int tmio_sd_pio_write_one_block(struct udevice *dev,
  201. const char *pbuf, uint blocksize)
  202. {
  203. struct tmio_sd_priv *priv = dev_get_priv(dev);
  204. int ret;
  205. /* wait until the buffer becomes empty */
  206. ret = tmio_sd_wait_for_irq(dev, TMIO_SD_INFO2,
  207. TMIO_SD_INFO2_BWE);
  208. if (ret)
  209. return ret;
  210. tmio_sd_writel(priv, 0, TMIO_SD_INFO2);
  211. if (priv->caps & TMIO_SD_CAP_64BIT)
  212. tmio_pio_write_fifo_64(priv, pbuf, blocksize);
  213. else if (priv->caps & TMIO_SD_CAP_16BIT)
  214. tmio_pio_write_fifo_16(priv, pbuf, blocksize);
  215. else
  216. tmio_pio_write_fifo_32(priv, pbuf, blocksize);
  217. return 0;
  218. }
  219. static int tmio_sd_pio_xfer(struct udevice *dev, struct mmc_data *data)
  220. {
  221. const char *src = data->src;
  222. char *dest = data->dest;
  223. int i, ret;
  224. for (i = 0; i < data->blocks; i++) {
  225. if (data->flags & MMC_DATA_READ)
  226. ret = tmio_sd_pio_read_one_block(dev, dest,
  227. data->blocksize);
  228. else
  229. ret = tmio_sd_pio_write_one_block(dev, src,
  230. data->blocksize);
  231. if (ret)
  232. return ret;
  233. if (data->flags & MMC_DATA_READ)
  234. dest += data->blocksize;
  235. else
  236. src += data->blocksize;
  237. }
  238. return 0;
  239. }
  240. static void tmio_sd_dma_start(struct tmio_sd_priv *priv,
  241. dma_addr_t dma_addr)
  242. {
  243. u32 tmp;
  244. tmio_sd_writel(priv, 0, TMIO_SD_DMA_INFO1);
  245. tmio_sd_writel(priv, 0, TMIO_SD_DMA_INFO2);
  246. /* enable DMA */
  247. tmp = tmio_sd_readl(priv, TMIO_SD_EXTMODE);
  248. tmp |= TMIO_SD_EXTMODE_DMA_EN;
  249. tmio_sd_writel(priv, tmp, TMIO_SD_EXTMODE);
  250. tmio_sd_writel(priv, dma_addr & U32_MAX, TMIO_SD_DMA_ADDR_L);
  251. /* suppress the warning "right shift count >= width of type" */
  252. dma_addr >>= min_t(int, 32, 8 * sizeof(dma_addr));
  253. tmio_sd_writel(priv, dma_addr & U32_MAX, TMIO_SD_DMA_ADDR_H);
  254. tmio_sd_writel(priv, TMIO_SD_DMA_CTL_START, TMIO_SD_DMA_CTL);
  255. }
  256. static int tmio_sd_dma_wait_for_irq(struct udevice *dev, u32 flag,
  257. unsigned int blocks)
  258. {
  259. struct tmio_sd_priv *priv = dev_get_priv(dev);
  260. long wait = 1000000 + 10 * blocks;
  261. while (!(tmio_sd_readl(priv, TMIO_SD_DMA_INFO1) & flag)) {
  262. if (wait-- < 0) {
  263. dev_err(dev, "timeout during DMA\n");
  264. return -ETIMEDOUT;
  265. }
  266. udelay(10);
  267. }
  268. if (tmio_sd_readl(priv, TMIO_SD_DMA_INFO2)) {
  269. dev_err(dev, "error during DMA\n");
  270. return -EIO;
  271. }
  272. return 0;
  273. }
  274. static int tmio_sd_dma_xfer(struct udevice *dev, struct mmc_data *data)
  275. {
  276. struct tmio_sd_priv *priv = dev_get_priv(dev);
  277. size_t len = data->blocks * data->blocksize;
  278. void *buf;
  279. enum dma_data_direction dir;
  280. dma_addr_t dma_addr;
  281. u32 poll_flag, tmp;
  282. int ret;
  283. tmp = tmio_sd_readl(priv, TMIO_SD_DMA_MODE);
  284. if (data->flags & MMC_DATA_READ) {
  285. buf = data->dest;
  286. dir = DMA_FROM_DEVICE;
  287. /*
  288. * The DMA READ completion flag position differs on Socionext
  289. * and Renesas SoCs. It is bit 20 on Socionext SoCs and using
  290. * bit 17 is a hardware bug and forbidden. It is bit 17 on
  291. * Renesas SoCs and bit 20 does not work on them.
  292. */
  293. poll_flag = (priv->caps & TMIO_SD_CAP_RCAR) ?
  294. TMIO_SD_DMA_INFO1_END_RD :
  295. TMIO_SD_DMA_INFO1_END_RD2;
  296. tmp |= TMIO_SD_DMA_MODE_DIR_RD;
  297. } else {
  298. buf = (void *)data->src;
  299. dir = DMA_TO_DEVICE;
  300. poll_flag = TMIO_SD_DMA_INFO1_END_WR;
  301. tmp &= ~TMIO_SD_DMA_MODE_DIR_RD;
  302. }
  303. tmio_sd_writel(priv, tmp, TMIO_SD_DMA_MODE);
  304. dma_addr = __dma_map_single(buf, len, dir);
  305. tmio_sd_dma_start(priv, dma_addr);
  306. ret = tmio_sd_dma_wait_for_irq(dev, poll_flag, data->blocks);
  307. __dma_unmap_single(dma_addr, len, dir);
  308. return ret;
  309. }
  310. /* check if the address is DMA'able */
  311. static bool tmio_sd_addr_is_dmaable(unsigned long addr)
  312. {
  313. if (!IS_ALIGNED(addr, TMIO_SD_DMA_MINALIGN))
  314. return false;
  315. #if defined(CONFIG_ARCH_UNIPHIER) && !defined(CONFIG_ARM64) && \
  316. defined(CONFIG_SPL_BUILD)
  317. /*
  318. * For UniPhier ARMv7 SoCs, the stack is allocated in the locked ways
  319. * of L2, which is unreachable from the DMA engine.
  320. */
  321. if (addr < CONFIG_SPL_STACK)
  322. return false;
  323. #endif
  324. return true;
  325. }
  326. int tmio_sd_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
  327. struct mmc_data *data)
  328. {
  329. struct tmio_sd_priv *priv = dev_get_priv(dev);
  330. int ret;
  331. u32 tmp;
  332. if (tmio_sd_readl(priv, TMIO_SD_INFO2) & TMIO_SD_INFO2_CBSY) {
  333. dev_err(dev, "command busy\n");
  334. return -EBUSY;
  335. }
  336. /* clear all status flags */
  337. tmio_sd_writel(priv, 0, TMIO_SD_INFO1);
  338. tmio_sd_writel(priv, 0, TMIO_SD_INFO2);
  339. /* disable DMA once */
  340. tmp = tmio_sd_readl(priv, TMIO_SD_EXTMODE);
  341. tmp &= ~TMIO_SD_EXTMODE_DMA_EN;
  342. tmio_sd_writel(priv, tmp, TMIO_SD_EXTMODE);
  343. tmio_sd_writel(priv, cmd->cmdarg, TMIO_SD_ARG);
  344. tmp = cmd->cmdidx;
  345. if (data) {
  346. tmio_sd_writel(priv, data->blocksize, TMIO_SD_SIZE);
  347. tmio_sd_writel(priv, data->blocks, TMIO_SD_SECCNT);
  348. /* Do not send CMD12 automatically */
  349. tmp |= TMIO_SD_CMD_NOSTOP | TMIO_SD_CMD_DATA;
  350. if (data->blocks > 1)
  351. tmp |= TMIO_SD_CMD_MULTI;
  352. if (data->flags & MMC_DATA_READ)
  353. tmp |= TMIO_SD_CMD_RD;
  354. }
  355. /*
  356. * Do not use the response type auto-detection on this hardware.
  357. * CMD8, for example, has different response types on SD and eMMC,
  358. * while this controller always assumes the response type for SD.
  359. * Set the response type manually.
  360. */
  361. switch (cmd->resp_type) {
  362. case MMC_RSP_NONE:
  363. tmp |= TMIO_SD_CMD_RSP_NONE;
  364. break;
  365. case MMC_RSP_R1:
  366. tmp |= TMIO_SD_CMD_RSP_R1;
  367. break;
  368. case MMC_RSP_R1b:
  369. tmp |= TMIO_SD_CMD_RSP_R1B;
  370. break;
  371. case MMC_RSP_R2:
  372. tmp |= TMIO_SD_CMD_RSP_R2;
  373. break;
  374. case MMC_RSP_R3:
  375. tmp |= TMIO_SD_CMD_RSP_R3;
  376. break;
  377. default:
  378. dev_err(dev, "unknown response type\n");
  379. return -EINVAL;
  380. }
  381. dev_dbg(dev, "sending CMD%d (SD_CMD=%08x, SD_ARG=%08x)\n",
  382. cmd->cmdidx, tmp, cmd->cmdarg);
  383. tmio_sd_writel(priv, tmp, TMIO_SD_CMD);
  384. ret = tmio_sd_wait_for_irq(dev, TMIO_SD_INFO1,
  385. TMIO_SD_INFO1_RSP);
  386. if (ret)
  387. return ret;
  388. if (cmd->resp_type & MMC_RSP_136) {
  389. u32 rsp_127_104 = tmio_sd_readl(priv, TMIO_SD_RSP76);
  390. u32 rsp_103_72 = tmio_sd_readl(priv, TMIO_SD_RSP54);
  391. u32 rsp_71_40 = tmio_sd_readl(priv, TMIO_SD_RSP32);
  392. u32 rsp_39_8 = tmio_sd_readl(priv, TMIO_SD_RSP10);
  393. cmd->response[0] = ((rsp_127_104 & 0x00ffffff) << 8) |
  394. ((rsp_103_72 & 0xff000000) >> 24);
  395. cmd->response[1] = ((rsp_103_72 & 0x00ffffff) << 8) |
  396. ((rsp_71_40 & 0xff000000) >> 24);
  397. cmd->response[2] = ((rsp_71_40 & 0x00ffffff) << 8) |
  398. ((rsp_39_8 & 0xff000000) >> 24);
  399. cmd->response[3] = (rsp_39_8 & 0xffffff) << 8;
  400. } else {
  401. /* bit 39-8 */
  402. cmd->response[0] = tmio_sd_readl(priv, TMIO_SD_RSP10);
  403. }
  404. if (data) {
  405. /* use DMA if the HW supports it and the buffer is aligned */
  406. if (priv->caps & TMIO_SD_CAP_DMA_INTERNAL &&
  407. tmio_sd_addr_is_dmaable((long)data->src))
  408. ret = tmio_sd_dma_xfer(dev, data);
  409. else
  410. ret = tmio_sd_pio_xfer(dev, data);
  411. ret = tmio_sd_wait_for_irq(dev, TMIO_SD_INFO1,
  412. TMIO_SD_INFO1_CMP);
  413. if (ret)
  414. return ret;
  415. }
  416. tmio_sd_wait_for_irq(dev, TMIO_SD_INFO2, TMIO_SD_INFO2_SCLKDIVEN);
  417. return ret;
  418. }
  419. static int tmio_sd_set_bus_width(struct tmio_sd_priv *priv,
  420. struct mmc *mmc)
  421. {
  422. u32 val, tmp;
  423. switch (mmc->bus_width) {
  424. case 0:
  425. case 1:
  426. val = TMIO_SD_OPTION_WIDTH_1;
  427. break;
  428. case 4:
  429. val = TMIO_SD_OPTION_WIDTH_4;
  430. break;
  431. case 8:
  432. val = TMIO_SD_OPTION_WIDTH_8;
  433. break;
  434. default:
  435. return -EINVAL;
  436. }
  437. tmp = tmio_sd_readl(priv, TMIO_SD_OPTION);
  438. tmp &= ~TMIO_SD_OPTION_WIDTH_MASK;
  439. tmp |= val;
  440. tmio_sd_writel(priv, tmp, TMIO_SD_OPTION);
  441. return 0;
  442. }
  443. static void tmio_sd_set_ddr_mode(struct tmio_sd_priv *priv,
  444. struct mmc *mmc)
  445. {
  446. u32 tmp;
  447. tmp = tmio_sd_readl(priv, TMIO_SD_IF_MODE);
  448. if (mmc->ddr_mode)
  449. tmp |= TMIO_SD_IF_MODE_DDR;
  450. else
  451. tmp &= ~TMIO_SD_IF_MODE_DDR;
  452. tmio_sd_writel(priv, tmp, TMIO_SD_IF_MODE);
  453. }
  454. static void tmio_sd_set_clk_rate(struct tmio_sd_priv *priv,
  455. struct mmc *mmc)
  456. {
  457. unsigned int divisor;
  458. u32 val, tmp;
  459. if (!mmc->clock)
  460. return;
  461. divisor = DIV_ROUND_UP(priv->mclk, mmc->clock);
  462. if (divisor <= 1)
  463. val = (priv->caps & TMIO_SD_CAP_RCAR) ?
  464. TMIO_SD_CLKCTL_RCAR_DIV1 : TMIO_SD_CLKCTL_DIV1;
  465. else if (divisor <= 2)
  466. val = TMIO_SD_CLKCTL_DIV2;
  467. else if (divisor <= 4)
  468. val = TMIO_SD_CLKCTL_DIV4;
  469. else if (divisor <= 8)
  470. val = TMIO_SD_CLKCTL_DIV8;
  471. else if (divisor <= 16)
  472. val = TMIO_SD_CLKCTL_DIV16;
  473. else if (divisor <= 32)
  474. val = TMIO_SD_CLKCTL_DIV32;
  475. else if (divisor <= 64)
  476. val = TMIO_SD_CLKCTL_DIV64;
  477. else if (divisor <= 128)
  478. val = TMIO_SD_CLKCTL_DIV128;
  479. else if (divisor <= 256)
  480. val = TMIO_SD_CLKCTL_DIV256;
  481. else if (divisor <= 512 || !(priv->caps & TMIO_SD_CAP_DIV1024))
  482. val = TMIO_SD_CLKCTL_DIV512;
  483. else
  484. val = TMIO_SD_CLKCTL_DIV1024;
  485. tmp = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
  486. if (tmp & TMIO_SD_CLKCTL_SCLKEN &&
  487. (tmp & TMIO_SD_CLKCTL_DIV_MASK) == val)
  488. return;
  489. /* stop the clock before changing its rate to avoid a glitch signal */
  490. tmp &= ~TMIO_SD_CLKCTL_SCLKEN;
  491. tmio_sd_writel(priv, tmp, TMIO_SD_CLKCTL);
  492. tmp &= ~TMIO_SD_CLKCTL_DIV_MASK;
  493. tmp |= val | TMIO_SD_CLKCTL_OFFEN;
  494. tmio_sd_writel(priv, tmp, TMIO_SD_CLKCTL);
  495. tmp |= TMIO_SD_CLKCTL_SCLKEN;
  496. tmio_sd_writel(priv, tmp, TMIO_SD_CLKCTL);
  497. udelay(1000);
  498. }
  499. static void tmio_sd_set_pins(struct udevice *dev)
  500. {
  501. __maybe_unused struct mmc *mmc = mmc_get_mmc_dev(dev);
  502. #ifdef CONFIG_DM_REGULATOR
  503. struct tmio_sd_priv *priv = dev_get_priv(dev);
  504. if (priv->vqmmc_dev) {
  505. if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
  506. regulator_set_value(priv->vqmmc_dev, 1800000);
  507. else
  508. regulator_set_value(priv->vqmmc_dev, 3300000);
  509. regulator_set_enable(priv->vqmmc_dev, true);
  510. }
  511. #endif
  512. #ifdef CONFIG_PINCTRL
  513. switch (mmc->selected_mode) {
  514. case MMC_LEGACY:
  515. case SD_LEGACY:
  516. case MMC_HS:
  517. case SD_HS:
  518. case MMC_HS_52:
  519. case MMC_DDR_52:
  520. pinctrl_select_state(dev, "default");
  521. break;
  522. case UHS_SDR12:
  523. case UHS_SDR25:
  524. case UHS_SDR50:
  525. case UHS_DDR50:
  526. case UHS_SDR104:
  527. case MMC_HS_200:
  528. pinctrl_select_state(dev, "state_uhs");
  529. break;
  530. default:
  531. break;
  532. }
  533. #endif
  534. }
  535. int tmio_sd_set_ios(struct udevice *dev)
  536. {
  537. struct tmio_sd_priv *priv = dev_get_priv(dev);
  538. struct mmc *mmc = mmc_get_mmc_dev(dev);
  539. int ret;
  540. dev_dbg(dev, "clock %uHz, DDRmode %d, width %u\n",
  541. mmc->clock, mmc->ddr_mode, mmc->bus_width);
  542. ret = tmio_sd_set_bus_width(priv, mmc);
  543. if (ret)
  544. return ret;
  545. tmio_sd_set_ddr_mode(priv, mmc);
  546. tmio_sd_set_clk_rate(priv, mmc);
  547. tmio_sd_set_pins(dev);
  548. return 0;
  549. }
  550. int tmio_sd_get_cd(struct udevice *dev)
  551. {
  552. struct tmio_sd_priv *priv = dev_get_priv(dev);
  553. if (priv->caps & TMIO_SD_CAP_NONREMOVABLE)
  554. return 1;
  555. return !!(tmio_sd_readl(priv, TMIO_SD_INFO1) &
  556. TMIO_SD_INFO1_CD);
  557. }
  558. static void tmio_sd_host_init(struct tmio_sd_priv *priv)
  559. {
  560. u32 tmp;
  561. /* soft reset of the host */
  562. tmp = tmio_sd_readl(priv, TMIO_SD_SOFT_RST);
  563. tmp &= ~TMIO_SD_SOFT_RST_RSTX;
  564. tmio_sd_writel(priv, tmp, TMIO_SD_SOFT_RST);
  565. tmp |= TMIO_SD_SOFT_RST_RSTX;
  566. tmio_sd_writel(priv, tmp, TMIO_SD_SOFT_RST);
  567. /* FIXME: implement eMMC hw_reset */
  568. tmio_sd_writel(priv, TMIO_SD_STOP_SEC, TMIO_SD_STOP);
  569. /*
  570. * Connected to 32bit AXI.
  571. * This register dropped backward compatibility at version 0x10.
  572. * Write an appropriate value depending on the IP version.
  573. */
  574. if (priv->version >= 0x10)
  575. tmio_sd_writel(priv, 0x101, TMIO_SD_HOST_MODE);
  576. else
  577. tmio_sd_writel(priv, 0x0, TMIO_SD_HOST_MODE);
  578. if (priv->caps & TMIO_SD_CAP_DMA_INTERNAL) {
  579. tmp = tmio_sd_readl(priv, TMIO_SD_DMA_MODE);
  580. tmp |= TMIO_SD_DMA_MODE_ADDR_INC;
  581. tmio_sd_writel(priv, tmp, TMIO_SD_DMA_MODE);
  582. }
  583. }
  584. int tmio_sd_bind(struct udevice *dev)
  585. {
  586. struct tmio_sd_plat *plat = dev_get_platdata(dev);
  587. return mmc_bind(dev, &plat->mmc, &plat->cfg);
  588. }
  589. int tmio_sd_probe(struct udevice *dev, u32 quirks)
  590. {
  591. struct tmio_sd_plat *plat = dev_get_platdata(dev);
  592. struct tmio_sd_priv *priv = dev_get_priv(dev);
  593. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  594. fdt_addr_t base;
  595. int ret;
  596. base = devfdt_get_addr(dev);
  597. if (base == FDT_ADDR_T_NONE)
  598. return -EINVAL;
  599. priv->regbase = devm_ioremap(dev, base, SZ_2K);
  600. if (!priv->regbase)
  601. return -ENOMEM;
  602. #ifdef CONFIG_DM_REGULATOR
  603. device_get_supply_regulator(dev, "vqmmc-supply", &priv->vqmmc_dev);
  604. #endif
  605. ret = mmc_of_parse(dev, &plat->cfg);
  606. if (ret < 0) {
  607. dev_err(dev, "failed to parse host caps\n");
  608. return ret;
  609. }
  610. plat->cfg.name = dev->name;
  611. plat->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
  612. if (quirks)
  613. priv->caps = quirks;
  614. priv->version = tmio_sd_readl(priv, TMIO_SD_VERSION) &
  615. TMIO_SD_VERSION_IP;
  616. dev_dbg(dev, "version %x\n", priv->version);
  617. if (priv->version >= 0x10) {
  618. priv->caps |= TMIO_SD_CAP_DMA_INTERNAL;
  619. priv->caps |= TMIO_SD_CAP_DIV1024;
  620. }
  621. if (fdt_get_property(gd->fdt_blob, dev_of_offset(dev), "non-removable",
  622. NULL))
  623. priv->caps |= TMIO_SD_CAP_NONREMOVABLE;
  624. tmio_sd_host_init(priv);
  625. plat->cfg.voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34;
  626. plat->cfg.f_min = priv->mclk /
  627. (priv->caps & TMIO_SD_CAP_DIV1024 ? 1024 : 512);
  628. plat->cfg.f_max = priv->mclk;
  629. plat->cfg.b_max = U32_MAX; /* max value of TMIO_SD_SECCNT */
  630. upriv->mmc = &plat->mmc;
  631. return 0;
  632. }