sdhci-of-at91.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Atmel SDMMC controller driver.
  3. *
  4. * Copyright (C) 2015 Atmel,
  5. * 2015 Ludovic Desroches <ludovic.desroches@atmel.com>
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/delay.h>
  18. #include <linux/err.h>
  19. #include <linux/io.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mmc/host.h>
  22. #include <linux/mmc/slot-gpio.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #include <linux/pm.h>
  27. #include <linux/pm_runtime.h>
  28. #include "sdhci-pltfm.h"
  29. #define SDMMC_CACR 0x230
  30. #define SDMMC_CACR_CAPWREN BIT(0)
  31. #define SDMMC_CACR_KEY (0x46 << 8)
  32. #define SDHCI_AT91_PRESET_COMMON_CONF 0x400 /* drv type B, programmable clock mode */
  33. struct sdhci_at91_priv {
  34. struct clk *hclock;
  35. struct clk *gck;
  36. struct clk *mainck;
  37. };
  38. static void sdhci_at91_set_clock(struct sdhci_host *host, unsigned int clock)
  39. {
  40. u16 clk;
  41. unsigned long timeout;
  42. host->mmc->actual_clock = 0;
  43. /*
  44. * There is no requirement to disable the internal clock before
  45. * changing the SD clock configuration. Moreover, disabling the
  46. * internal clock, changing the configuration and re-enabling the
  47. * internal clock causes some bugs. It can prevent to get the internal
  48. * clock stable flag ready and an unexpected switch to the base clock
  49. * when using presets.
  50. */
  51. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  52. clk &= SDHCI_CLOCK_INT_EN;
  53. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  54. if (clock == 0)
  55. return;
  56. clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
  57. clk |= SDHCI_CLOCK_INT_EN;
  58. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  59. /* Wait max 20 ms */
  60. timeout = 20;
  61. while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
  62. & SDHCI_CLOCK_INT_STABLE)) {
  63. if (timeout == 0) {
  64. pr_err("%s: Internal clock never stabilised.\n",
  65. mmc_hostname(host->mmc));
  66. return;
  67. }
  68. timeout--;
  69. mdelay(1);
  70. }
  71. clk |= SDHCI_CLOCK_CARD_EN;
  72. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  73. }
  74. static const struct sdhci_ops sdhci_at91_sama5d2_ops = {
  75. .set_clock = sdhci_at91_set_clock,
  76. .set_bus_width = sdhci_set_bus_width,
  77. .reset = sdhci_reset,
  78. .set_uhs_signaling = sdhci_set_uhs_signaling,
  79. };
  80. static const struct sdhci_pltfm_data soc_data_sama5d2 = {
  81. .ops = &sdhci_at91_sama5d2_ops,
  82. };
  83. static const struct of_device_id sdhci_at91_dt_match[] = {
  84. { .compatible = "atmel,sama5d2-sdhci", .data = &soc_data_sama5d2 },
  85. {}
  86. };
  87. #ifdef CONFIG_PM
  88. static int sdhci_at91_runtime_suspend(struct device *dev)
  89. {
  90. struct sdhci_host *host = dev_get_drvdata(dev);
  91. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  92. struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
  93. int ret;
  94. ret = sdhci_runtime_suspend_host(host);
  95. clk_disable_unprepare(priv->gck);
  96. clk_disable_unprepare(priv->hclock);
  97. clk_disable_unprepare(priv->mainck);
  98. return ret;
  99. }
  100. static int sdhci_at91_runtime_resume(struct device *dev)
  101. {
  102. struct sdhci_host *host = dev_get_drvdata(dev);
  103. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  104. struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
  105. int ret;
  106. ret = clk_prepare_enable(priv->mainck);
  107. if (ret) {
  108. dev_err(dev, "can't enable mainck\n");
  109. return ret;
  110. }
  111. ret = clk_prepare_enable(priv->hclock);
  112. if (ret) {
  113. dev_err(dev, "can't enable hclock\n");
  114. return ret;
  115. }
  116. ret = clk_prepare_enable(priv->gck);
  117. if (ret) {
  118. dev_err(dev, "can't enable gck\n");
  119. return ret;
  120. }
  121. return sdhci_runtime_resume_host(host);
  122. }
  123. #endif /* CONFIG_PM */
  124. static const struct dev_pm_ops sdhci_at91_dev_pm_ops = {
  125. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  126. pm_runtime_force_resume)
  127. SET_RUNTIME_PM_OPS(sdhci_at91_runtime_suspend,
  128. sdhci_at91_runtime_resume,
  129. NULL)
  130. };
  131. static int sdhci_at91_probe(struct platform_device *pdev)
  132. {
  133. const struct of_device_id *match;
  134. const struct sdhci_pltfm_data *soc_data;
  135. struct sdhci_host *host;
  136. struct sdhci_pltfm_host *pltfm_host;
  137. struct sdhci_at91_priv *priv;
  138. unsigned int caps0, caps1;
  139. unsigned int clk_base, clk_mul;
  140. unsigned int gck_rate, real_gck_rate;
  141. int ret;
  142. unsigned int preset_div;
  143. match = of_match_device(sdhci_at91_dt_match, &pdev->dev);
  144. if (!match)
  145. return -EINVAL;
  146. soc_data = match->data;
  147. host = sdhci_pltfm_init(pdev, soc_data, sizeof(*priv));
  148. if (IS_ERR(host))
  149. return PTR_ERR(host);
  150. pltfm_host = sdhci_priv(host);
  151. priv = sdhci_pltfm_priv(pltfm_host);
  152. priv->mainck = devm_clk_get(&pdev->dev, "baseclk");
  153. if (IS_ERR(priv->mainck)) {
  154. dev_err(&pdev->dev, "failed to get baseclk\n");
  155. return PTR_ERR(priv->mainck);
  156. }
  157. priv->hclock = devm_clk_get(&pdev->dev, "hclock");
  158. if (IS_ERR(priv->hclock)) {
  159. dev_err(&pdev->dev, "failed to get hclock\n");
  160. return PTR_ERR(priv->hclock);
  161. }
  162. priv->gck = devm_clk_get(&pdev->dev, "multclk");
  163. if (IS_ERR(priv->gck)) {
  164. dev_err(&pdev->dev, "failed to get multclk\n");
  165. return PTR_ERR(priv->gck);
  166. }
  167. /*
  168. * The mult clock is provided by as a generated clock by the PMC
  169. * controller. In order to set the rate of gck, we have to get the
  170. * base clock rate and the clock mult from capabilities.
  171. */
  172. clk_prepare_enable(priv->hclock);
  173. caps0 = readl(host->ioaddr + SDHCI_CAPABILITIES);
  174. caps1 = readl(host->ioaddr + SDHCI_CAPABILITIES_1);
  175. clk_base = (caps0 & SDHCI_CLOCK_V3_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
  176. clk_mul = (caps1 & SDHCI_CLOCK_MUL_MASK) >> SDHCI_CLOCK_MUL_SHIFT;
  177. gck_rate = clk_base * 1000000 * (clk_mul + 1);
  178. ret = clk_set_rate(priv->gck, gck_rate);
  179. if (ret < 0) {
  180. dev_err(&pdev->dev, "failed to set gck");
  181. goto hclock_disable_unprepare;
  182. }
  183. /*
  184. * We need to check if we have the requested rate for gck because in
  185. * some cases this rate could be not supported. If it happens, the rate
  186. * is the closest one gck can provide. We have to update the value
  187. * of clk mul.
  188. */
  189. real_gck_rate = clk_get_rate(priv->gck);
  190. if (real_gck_rate != gck_rate) {
  191. clk_mul = real_gck_rate / (clk_base * 1000000) - 1;
  192. caps1 &= (~SDHCI_CLOCK_MUL_MASK);
  193. caps1 |= ((clk_mul << SDHCI_CLOCK_MUL_SHIFT) & SDHCI_CLOCK_MUL_MASK);
  194. /* Set capabilities in r/w mode. */
  195. writel(SDMMC_CACR_KEY | SDMMC_CACR_CAPWREN, host->ioaddr + SDMMC_CACR);
  196. writel(caps1, host->ioaddr + SDHCI_CAPABILITIES_1);
  197. /* Set capabilities in ro mode. */
  198. writel(0, host->ioaddr + SDMMC_CACR);
  199. dev_info(&pdev->dev, "update clk mul to %u as gck rate is %u Hz\n",
  200. clk_mul, real_gck_rate);
  201. }
  202. /*
  203. * We have to set preset values because it depends on the clk_mul
  204. * value. Moreover, SDR104 is supported in a degraded mode since the
  205. * maximum sd clock value is 120 MHz instead of 208 MHz. For that
  206. * reason, we need to use presets to support SDR104.
  207. */
  208. preset_div = DIV_ROUND_UP(real_gck_rate, 24000000) - 1;
  209. writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
  210. host->ioaddr + SDHCI_PRESET_FOR_SDR12);
  211. preset_div = DIV_ROUND_UP(real_gck_rate, 50000000) - 1;
  212. writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
  213. host->ioaddr + SDHCI_PRESET_FOR_SDR25);
  214. preset_div = DIV_ROUND_UP(real_gck_rate, 100000000) - 1;
  215. writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
  216. host->ioaddr + SDHCI_PRESET_FOR_SDR50);
  217. preset_div = DIV_ROUND_UP(real_gck_rate, 120000000) - 1;
  218. writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
  219. host->ioaddr + SDHCI_PRESET_FOR_SDR104);
  220. preset_div = DIV_ROUND_UP(real_gck_rate, 50000000) - 1;
  221. writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
  222. host->ioaddr + SDHCI_PRESET_FOR_DDR50);
  223. clk_prepare_enable(priv->mainck);
  224. clk_prepare_enable(priv->gck);
  225. ret = mmc_of_parse(host->mmc);
  226. if (ret)
  227. goto clocks_disable_unprepare;
  228. sdhci_get_of_property(pdev);
  229. pm_runtime_get_noresume(&pdev->dev);
  230. pm_runtime_set_active(&pdev->dev);
  231. pm_runtime_enable(&pdev->dev);
  232. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  233. pm_runtime_use_autosuspend(&pdev->dev);
  234. ret = sdhci_add_host(host);
  235. if (ret)
  236. goto pm_runtime_disable;
  237. /*
  238. * When calling sdhci_runtime_suspend_host(), the sdhci layer makes
  239. * the assumption that all the clocks of the controller are disabled.
  240. * It means we can't get irq from it when it is runtime suspended.
  241. * For that reason, it is not planned to wake-up on a card detect irq
  242. * from the controller.
  243. * If we want to use runtime PM and to be able to wake-up on card
  244. * insertion, we have to use a GPIO for the card detection or we can
  245. * use polling. Be aware that using polling will resume/suspend the
  246. * controller between each attempt.
  247. * Disable SDHCI_QUIRK_BROKEN_CARD_DETECTION to be sure nobody tries
  248. * to enable polling via device tree with broken-cd property.
  249. */
  250. if (mmc_card_is_removable(host->mmc) &&
  251. mmc_gpio_get_cd(host->mmc) < 0) {
  252. host->mmc->caps |= MMC_CAP_NEEDS_POLL;
  253. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  254. }
  255. pm_runtime_put_autosuspend(&pdev->dev);
  256. return 0;
  257. pm_runtime_disable:
  258. pm_runtime_disable(&pdev->dev);
  259. pm_runtime_set_suspended(&pdev->dev);
  260. pm_runtime_put_noidle(&pdev->dev);
  261. clocks_disable_unprepare:
  262. clk_disable_unprepare(priv->gck);
  263. clk_disable_unprepare(priv->mainck);
  264. hclock_disable_unprepare:
  265. clk_disable_unprepare(priv->hclock);
  266. sdhci_pltfm_free(pdev);
  267. return ret;
  268. }
  269. static int sdhci_at91_remove(struct platform_device *pdev)
  270. {
  271. struct sdhci_host *host = platform_get_drvdata(pdev);
  272. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  273. struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
  274. struct clk *gck = priv->gck;
  275. struct clk *hclock = priv->hclock;
  276. struct clk *mainck = priv->mainck;
  277. pm_runtime_get_sync(&pdev->dev);
  278. pm_runtime_disable(&pdev->dev);
  279. pm_runtime_put_noidle(&pdev->dev);
  280. sdhci_pltfm_unregister(pdev);
  281. clk_disable_unprepare(gck);
  282. clk_disable_unprepare(hclock);
  283. clk_disable_unprepare(mainck);
  284. return 0;
  285. }
  286. static struct platform_driver sdhci_at91_driver = {
  287. .driver = {
  288. .name = "sdhci-at91",
  289. .of_match_table = sdhci_at91_dt_match,
  290. .pm = &sdhci_at91_dev_pm_ops,
  291. },
  292. .probe = sdhci_at91_probe,
  293. .remove = sdhci_at91_remove,
  294. };
  295. module_platform_driver(sdhci_at91_driver);
  296. MODULE_DESCRIPTION("SDHCI driver for at91");
  297. MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>");
  298. MODULE_LICENSE("GPL v2");