sdhci-of-ma35d1.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2024 Nuvoton Technology Corp.
  4. *
  5. * Author: Shan-Chun Hung <shanchun1218@gmail.com>
  6. */
  7. #include <linux/align.h>
  8. #include <linux/array_size.h>
  9. #include <linux/bits.h>
  10. #include <linux/build_bug.h>
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/dev_printk.h>
  14. #include <linux/device.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/err.h>
  17. #include <linux/math.h>
  18. #include <linux/mfd/syscon.h>
  19. #include <linux/minmax.h>
  20. #include <linux/mmc/card.h>
  21. #include <linux/mmc/host.h>
  22. #include <linux/mod_devicetable.h>
  23. #include <linux/module.h>
  24. #include <linux/pinctrl/consumer.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/regmap.h>
  27. #include <linux/reset.h>
  28. #include <linux/sizes.h>
  29. #include <linux/types.h>
  30. #include "sdhci-pltfm.h"
  31. #include "sdhci.h"
  32. #define MA35_SYS_MISCFCR0 0x070
  33. #define MA35_SDHCI_MSHCCTL 0x508
  34. #define MA35_SDHCI_MBIUCTL 0x510
  35. #define MA35_SDHCI_CMD_CONFLICT_CHK BIT(0)
  36. #define MA35_SDHCI_INCR_MSK GENMASK(3, 0)
  37. #define MA35_SDHCI_INCR16 BIT(3)
  38. #define MA35_SDHCI_INCR8 BIT(2)
  39. struct ma35_priv {
  40. struct reset_control *rst;
  41. struct pinctrl *pinctrl;
  42. struct pinctrl_state *pins_uhs;
  43. struct pinctrl_state *pins_default;
  44. };
  45. struct ma35_restore_data {
  46. u32 reg;
  47. u32 width;
  48. };
  49. static const struct ma35_restore_data restore_data[] = {
  50. { SDHCI_CLOCK_CONTROL, sizeof(u32)},
  51. { SDHCI_BLOCK_SIZE, sizeof(u32)},
  52. { SDHCI_INT_ENABLE, sizeof(u32)},
  53. { SDHCI_SIGNAL_ENABLE, sizeof(u32)},
  54. { SDHCI_AUTO_CMD_STATUS, sizeof(u32)},
  55. { SDHCI_HOST_CONTROL, sizeof(u32)},
  56. { SDHCI_TIMEOUT_CONTROL, sizeof(u8) },
  57. { MA35_SDHCI_MSHCCTL, sizeof(u16)},
  58. { MA35_SDHCI_MBIUCTL, sizeof(u16)},
  59. };
  60. /*
  61. * If DMA addr spans 128MB boundary, we split the DMA transfer into two
  62. * so that each DMA transfer doesn't exceed the boundary.
  63. */
  64. static void ma35_adma_write_desc(struct sdhci_host *host, void **desc, dma_addr_t addr, int len,
  65. unsigned int cmd)
  66. {
  67. int tmplen, offset;
  68. if (likely(!len || (ALIGN(addr, SZ_128M) == ALIGN(addr + len - 1, SZ_128M)))) {
  69. sdhci_adma_write_desc(host, desc, addr, len, cmd);
  70. return;
  71. }
  72. offset = addr & (SZ_128M - 1);
  73. tmplen = SZ_128M - offset;
  74. sdhci_adma_write_desc(host, desc, addr, tmplen, cmd);
  75. addr += tmplen;
  76. len -= tmplen;
  77. sdhci_adma_write_desc(host, desc, addr, len, cmd);
  78. }
  79. static void ma35_set_clock(struct sdhci_host *host, unsigned int clock)
  80. {
  81. u32 ctl;
  82. /*
  83. * If the clock frequency exceeds MMC_HIGH_52_MAX_DTR,
  84. * disable command conflict check.
  85. */
  86. ctl = sdhci_readw(host, MA35_SDHCI_MSHCCTL);
  87. if (clock > MMC_HIGH_52_MAX_DTR)
  88. ctl &= ~MA35_SDHCI_CMD_CONFLICT_CHK;
  89. else
  90. ctl |= MA35_SDHCI_CMD_CONFLICT_CHK;
  91. sdhci_writew(host, ctl, MA35_SDHCI_MSHCCTL);
  92. sdhci_set_clock(host, clock);
  93. }
  94. static int ma35_start_signal_voltage_switch(struct mmc_host *mmc, struct mmc_ios *ios)
  95. {
  96. struct sdhci_host *host = mmc_priv(mmc);
  97. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  98. struct ma35_priv *priv = sdhci_pltfm_priv(pltfm_host);
  99. switch (ios->signal_voltage) {
  100. case MMC_SIGNAL_VOLTAGE_180:
  101. if (!IS_ERR(priv->pinctrl) && !IS_ERR(priv->pins_uhs))
  102. pinctrl_select_state(priv->pinctrl, priv->pins_uhs);
  103. break;
  104. case MMC_SIGNAL_VOLTAGE_330:
  105. if (!IS_ERR(priv->pinctrl) && !IS_ERR(priv->pins_default))
  106. pinctrl_select_state(priv->pinctrl, priv->pins_default);
  107. break;
  108. default:
  109. dev_err(mmc_dev(host->mmc), "Unsupported signal voltage!\n");
  110. return -EINVAL;
  111. }
  112. return sdhci_start_signal_voltage_switch(mmc, ios);
  113. }
  114. static void ma35_voltage_switch(struct sdhci_host *host)
  115. {
  116. /* Wait for 5ms after set 1.8V signal enable bit */
  117. fsleep(5000);
  118. }
  119. static int ma35_execute_tuning(struct mmc_host *mmc, u32 opcode)
  120. {
  121. struct sdhci_host *host = mmc_priv(mmc);
  122. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  123. struct ma35_priv *priv = sdhci_pltfm_priv(pltfm_host);
  124. int idx;
  125. u32 regs[ARRAY_SIZE(restore_data)] = {};
  126. /*
  127. * Limitations require a reset of SD/eMMC before tuning and
  128. * saving the registers before resetting, then restoring
  129. * after the reset.
  130. */
  131. for (idx = 0; idx < ARRAY_SIZE(restore_data); idx++) {
  132. if (restore_data[idx].width == sizeof(u32))
  133. regs[idx] = sdhci_readl(host, restore_data[idx].reg);
  134. else if (restore_data[idx].width == sizeof(u16))
  135. regs[idx] = sdhci_readw(host, restore_data[idx].reg);
  136. else if (restore_data[idx].width == sizeof(u8))
  137. regs[idx] = sdhci_readb(host, restore_data[idx].reg);
  138. }
  139. reset_control_assert(priv->rst);
  140. reset_control_deassert(priv->rst);
  141. for (idx = 0; idx < ARRAY_SIZE(restore_data); idx++) {
  142. if (restore_data[idx].width == sizeof(u32))
  143. sdhci_writel(host, regs[idx], restore_data[idx].reg);
  144. else if (restore_data[idx].width == sizeof(u16))
  145. sdhci_writew(host, regs[idx], restore_data[idx].reg);
  146. else if (restore_data[idx].width == sizeof(u8))
  147. sdhci_writeb(host, regs[idx], restore_data[idx].reg);
  148. }
  149. return sdhci_execute_tuning(mmc, opcode);
  150. }
  151. static const struct sdhci_ops sdhci_ma35_ops = {
  152. .set_clock = ma35_set_clock,
  153. .set_bus_width = sdhci_set_bus_width,
  154. .set_uhs_signaling = sdhci_set_uhs_signaling,
  155. .get_max_clock = sdhci_pltfm_clk_get_max_clock,
  156. .reset = sdhci_reset,
  157. .adma_write_desc = ma35_adma_write_desc,
  158. .voltage_switch = ma35_voltage_switch,
  159. };
  160. static const struct sdhci_pltfm_data sdhci_ma35_pdata = {
  161. .ops = &sdhci_ma35_ops,
  162. .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
  163. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | SDHCI_QUIRK2_BROKEN_DDR50 |
  164. SDHCI_QUIRK2_ACMD23_BROKEN,
  165. };
  166. static int ma35_probe(struct platform_device *pdev)
  167. {
  168. struct device *dev = &pdev->dev;
  169. struct sdhci_pltfm_host *pltfm_host;
  170. struct sdhci_host *host;
  171. struct ma35_priv *priv;
  172. int err;
  173. u32 extra, ctl;
  174. host = sdhci_pltfm_init(pdev, &sdhci_ma35_pdata, sizeof(struct ma35_priv));
  175. if (IS_ERR(host))
  176. return PTR_ERR(host);
  177. /* Extra adma table cnt for cross 128M boundary handling. */
  178. extra = DIV_ROUND_UP_ULL(dma_get_required_mask(dev), SZ_128M);
  179. extra = min(extra, SDHCI_MAX_SEGS);
  180. host->adma_table_cnt += extra;
  181. pltfm_host = sdhci_priv(host);
  182. priv = sdhci_pltfm_priv(pltfm_host);
  183. pltfm_host->clk = devm_clk_get_optional_enabled(dev, NULL);
  184. if (IS_ERR(pltfm_host->clk)) {
  185. err = dev_err_probe(dev, PTR_ERR(pltfm_host->clk), "failed to get clk\n");
  186. goto err_sdhci;
  187. }
  188. err = mmc_of_parse(host->mmc);
  189. if (err)
  190. goto err_sdhci;
  191. priv->rst = devm_reset_control_get_exclusive(dev, NULL);
  192. if (IS_ERR(priv->rst)) {
  193. err = dev_err_probe(dev, PTR_ERR(priv->rst), "failed to get reset control\n");
  194. goto err_sdhci;
  195. }
  196. sdhci_get_of_property(pdev);
  197. priv->pinctrl = devm_pinctrl_get(dev);
  198. if (!IS_ERR(priv->pinctrl)) {
  199. priv->pins_default = pinctrl_lookup_state(priv->pinctrl, "default");
  200. priv->pins_uhs = pinctrl_lookup_state(priv->pinctrl, "state_uhs");
  201. pinctrl_select_state(priv->pinctrl, priv->pins_default);
  202. }
  203. if (!(host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)) {
  204. struct regmap *regmap;
  205. u32 reg;
  206. regmap = syscon_regmap_lookup_by_phandle(dev_of_node(dev), "nuvoton,sys");
  207. if (!IS_ERR(regmap)) {
  208. /* Enable SDHCI voltage stable for 1.8V */
  209. regmap_read(regmap, MA35_SYS_MISCFCR0, &reg);
  210. reg |= BIT(17);
  211. regmap_write(regmap, MA35_SYS_MISCFCR0, reg);
  212. }
  213. host->mmc_host_ops.start_signal_voltage_switch =
  214. ma35_start_signal_voltage_switch;
  215. }
  216. host->mmc_host_ops.execute_tuning = ma35_execute_tuning;
  217. err = sdhci_add_host(host);
  218. if (err)
  219. goto err_sdhci;
  220. /*
  221. * Split data into chunks of 16 or 8 bytes for transmission.
  222. * Each chunk transfer is guaranteed to be uninterrupted on the bus.
  223. * This likely corresponds to the AHB bus DMA burst size.
  224. */
  225. ctl = sdhci_readw(host, MA35_SDHCI_MBIUCTL);
  226. ctl &= ~MA35_SDHCI_INCR_MSK;
  227. ctl |= MA35_SDHCI_INCR16 | MA35_SDHCI_INCR8;
  228. sdhci_writew(host, ctl, MA35_SDHCI_MBIUCTL);
  229. return 0;
  230. err_sdhci:
  231. sdhci_pltfm_free(pdev);
  232. return err;
  233. }
  234. static void ma35_disable_card_clk(struct sdhci_host *host)
  235. {
  236. u16 ctrl;
  237. ctrl = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  238. if (ctrl & SDHCI_CLOCK_CARD_EN) {
  239. ctrl &= ~SDHCI_CLOCK_CARD_EN;
  240. sdhci_writew(host, ctrl, SDHCI_CLOCK_CONTROL);
  241. }
  242. }
  243. static void ma35_remove(struct platform_device *pdev)
  244. {
  245. struct sdhci_host *host = platform_get_drvdata(pdev);
  246. sdhci_remove_host(host, 0);
  247. ma35_disable_card_clk(host);
  248. sdhci_pltfm_free(pdev);
  249. }
  250. static const struct of_device_id sdhci_ma35_dt_ids[] = {
  251. { .compatible = "nuvoton,ma35d1-sdhci" },
  252. {}
  253. };
  254. static struct platform_driver sdhci_ma35_driver = {
  255. .driver = {
  256. .name = "sdhci-ma35",
  257. .of_match_table = sdhci_ma35_dt_ids,
  258. },
  259. .probe = ma35_probe,
  260. .remove_new = ma35_remove,
  261. };
  262. module_platform_driver(sdhci_ma35_driver);
  263. MODULE_DESCRIPTION("SDHCI platform driver for Nuvoton MA35");
  264. MODULE_AUTHOR("Shan-Chun Hung <shanchun1218@gmail.com>");
  265. MODULE_LICENSE("GPL");