tmio-common.c 18 KB

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