tmio-common.c 19 KB

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