sdhci-of-at91.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. MODULE_DEVICE_TABLE(of, sdhci_at91_dt_match);
  88. #ifdef CONFIG_PM
  89. static int sdhci_at91_runtime_suspend(struct device *dev)
  90. {
  91. struct sdhci_host *host = dev_get_drvdata(dev);
  92. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  93. struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
  94. int ret;
  95. ret = sdhci_runtime_suspend_host(host);
  96. clk_disable_unprepare(priv->gck);
  97. clk_disable_unprepare(priv->hclock);
  98. clk_disable_unprepare(priv->mainck);
  99. return ret;
  100. }
  101. static int sdhci_at91_runtime_resume(struct device *dev)
  102. {
  103. struct sdhci_host *host = dev_get_drvdata(dev);
  104. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  105. struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
  106. int ret;
  107. ret = clk_prepare_enable(priv->mainck);
  108. if (ret) {
  109. dev_err(dev, "can't enable mainck\n");
  110. return ret;
  111. }
  112. ret = clk_prepare_enable(priv->hclock);
  113. if (ret) {
  114. dev_err(dev, "can't enable hclock\n");
  115. return ret;
  116. }
  117. ret = clk_prepare_enable(priv->gck);
  118. if (ret) {
  119. dev_err(dev, "can't enable gck\n");
  120. return ret;
  121. }
  122. return sdhci_runtime_resume_host(host);
  123. }
  124. #endif /* CONFIG_PM */
  125. static const struct dev_pm_ops sdhci_at91_dev_pm_ops = {
  126. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  127. pm_runtime_force_resume)
  128. SET_RUNTIME_PM_OPS(sdhci_at91_runtime_suspend,
  129. sdhci_at91_runtime_resume,
  130. NULL)
  131. };
  132. static int sdhci_at91_probe(struct platform_device *pdev)
  133. {
  134. const struct of_device_id *match;
  135. const struct sdhci_pltfm_data *soc_data;
  136. struct sdhci_host *host;
  137. struct sdhci_pltfm_host *pltfm_host;
  138. struct sdhci_at91_priv *priv;
  139. unsigned int caps0, caps1;
  140. unsigned int clk_base, clk_mul;
  141. unsigned int gck_rate, real_gck_rate;
  142. int ret;
  143. unsigned int preset_div;
  144. match = of_match_device(sdhci_at91_dt_match, &pdev->dev);
  145. if (!match)
  146. return -EINVAL;
  147. soc_data = match->data;
  148. host = sdhci_pltfm_init(pdev, soc_data, sizeof(*priv));
  149. if (IS_ERR(host))
  150. return PTR_ERR(host);
  151. pltfm_host = sdhci_priv(host);
  152. priv = sdhci_pltfm_priv(pltfm_host);
  153. priv->mainck = devm_clk_get(&pdev->dev, "baseclk");
  154. if (IS_ERR(priv->mainck)) {
  155. dev_err(&pdev->dev, "failed to get baseclk\n");
  156. return PTR_ERR(priv->mainck);
  157. }
  158. priv->hclock = devm_clk_get(&pdev->dev, "hclock");
  159. if (IS_ERR(priv->hclock)) {
  160. dev_err(&pdev->dev, "failed to get hclock\n");
  161. return PTR_ERR(priv->hclock);
  162. }
  163. priv->gck = devm_clk_get(&pdev->dev, "multclk");
  164. if (IS_ERR(priv->gck)) {
  165. dev_err(&pdev->dev, "failed to get multclk\n");
  166. return PTR_ERR(priv->gck);
  167. }
  168. /*
  169. * The mult clock is provided by as a generated clock by the PMC
  170. * controller. In order to set the rate of gck, we have to get the
  171. * base clock rate and the clock mult from capabilities.
  172. */
  173. clk_prepare_enable(priv->hclock);
  174. caps0 = readl(host->ioaddr + SDHCI_CAPABILITIES);
  175. caps1 = readl(host->ioaddr + SDHCI_CAPABILITIES_1);
  176. clk_base = (caps0 & SDHCI_CLOCK_V3_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
  177. clk_mul = (caps1 & SDHCI_CLOCK_MUL_MASK) >> SDHCI_CLOCK_MUL_SHIFT;
  178. gck_rate = clk_base * 1000000 * (clk_mul + 1);
  179. ret = clk_set_rate(priv->gck, gck_rate);
  180. if (ret < 0) {
  181. dev_err(&pdev->dev, "failed to set gck");
  182. goto hclock_disable_unprepare;
  183. }
  184. /*
  185. * We need to check if we have the requested rate for gck because in
  186. * some cases this rate could be not supported. If it happens, the rate
  187. * is the closest one gck can provide. We have to update the value
  188. * of clk mul.
  189. */
  190. real_gck_rate = clk_get_rate(priv->gck);
  191. if (real_gck_rate != gck_rate) {
  192. clk_mul = real_gck_rate / (clk_base * 1000000) - 1;
  193. caps1 &= (~SDHCI_CLOCK_MUL_MASK);
  194. caps1 |= ((clk_mul << SDHCI_CLOCK_MUL_SHIFT) & SDHCI_CLOCK_MUL_MASK);
  195. /* Set capabilities in r/w mode. */
  196. writel(SDMMC_CACR_KEY | SDMMC_CACR_CAPWREN, host->ioaddr + SDMMC_CACR);
  197. writel(caps1, host->ioaddr + SDHCI_CAPABILITIES_1);
  198. /* Set capabilities in ro mode. */
  199. writel(0, host->ioaddr + SDMMC_CACR);
  200. dev_info(&pdev->dev, "update clk mul to %u as gck rate is %u Hz\n",
  201. clk_mul, real_gck_rate);
  202. }
  203. /*
  204. * We have to set preset values because it depends on the clk_mul
  205. * value. Moreover, SDR104 is supported in a degraded mode since the
  206. * maximum sd clock value is 120 MHz instead of 208 MHz. For that
  207. * reason, we need to use presets to support SDR104.
  208. */
  209. preset_div = DIV_ROUND_UP(real_gck_rate, 24000000) - 1;
  210. writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
  211. host->ioaddr + SDHCI_PRESET_FOR_SDR12);
  212. preset_div = DIV_ROUND_UP(real_gck_rate, 50000000) - 1;
  213. writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
  214. host->ioaddr + SDHCI_PRESET_FOR_SDR25);
  215. preset_div = DIV_ROUND_UP(real_gck_rate, 100000000) - 1;
  216. writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
  217. host->ioaddr + SDHCI_PRESET_FOR_SDR50);
  218. preset_div = DIV_ROUND_UP(real_gck_rate, 120000000) - 1;
  219. writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
  220. host->ioaddr + SDHCI_PRESET_FOR_SDR104);
  221. preset_div = DIV_ROUND_UP(real_gck_rate, 50000000) - 1;
  222. writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
  223. host->ioaddr + SDHCI_PRESET_FOR_DDR50);
  224. clk_prepare_enable(priv->mainck);
  225. clk_prepare_enable(priv->gck);
  226. ret = mmc_of_parse(host->mmc);
  227. if (ret)
  228. goto clocks_disable_unprepare;
  229. sdhci_get_of_property(pdev);
  230. pm_runtime_get_noresume(&pdev->dev);
  231. pm_runtime_set_active(&pdev->dev);
  232. pm_runtime_enable(&pdev->dev);
  233. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  234. pm_runtime_use_autosuspend(&pdev->dev);
  235. ret = sdhci_add_host(host);
  236. if (ret)
  237. goto pm_runtime_disable;
  238. /*
  239. * When calling sdhci_runtime_suspend_host(), the sdhci layer makes
  240. * the assumption that all the clocks of the controller are disabled.
  241. * It means we can't get irq from it when it is runtime suspended.
  242. * For that reason, it is not planned to wake-up on a card detect irq
  243. * from the controller.
  244. * If we want to use runtime PM and to be able to wake-up on card
  245. * insertion, we have to use a GPIO for the card detection or we can
  246. * use polling. Be aware that using polling will resume/suspend the
  247. * controller between each attempt.
  248. * Disable SDHCI_QUIRK_BROKEN_CARD_DETECTION to be sure nobody tries
  249. * to enable polling via device tree with broken-cd property.
  250. */
  251. if (mmc_card_is_removable(host->mmc) &&
  252. mmc_gpio_get_cd(host->mmc) < 0) {
  253. host->mmc->caps |= MMC_CAP_NEEDS_POLL;
  254. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  255. }
  256. pm_runtime_put_autosuspend(&pdev->dev);
  257. return 0;
  258. pm_runtime_disable:
  259. pm_runtime_disable(&pdev->dev);
  260. pm_runtime_set_suspended(&pdev->dev);
  261. pm_runtime_put_noidle(&pdev->dev);
  262. clocks_disable_unprepare:
  263. clk_disable_unprepare(priv->gck);
  264. clk_disable_unprepare(priv->mainck);
  265. hclock_disable_unprepare:
  266. clk_disable_unprepare(priv->hclock);
  267. sdhci_pltfm_free(pdev);
  268. return ret;
  269. }
  270. static int sdhci_at91_remove(struct platform_device *pdev)
  271. {
  272. struct sdhci_host *host = platform_get_drvdata(pdev);
  273. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  274. struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
  275. struct clk *gck = priv->gck;
  276. struct clk *hclock = priv->hclock;
  277. struct clk *mainck = priv->mainck;
  278. pm_runtime_get_sync(&pdev->dev);
  279. pm_runtime_disable(&pdev->dev);
  280. pm_runtime_put_noidle(&pdev->dev);
  281. sdhci_pltfm_unregister(pdev);
  282. clk_disable_unprepare(gck);
  283. clk_disable_unprepare(hclock);
  284. clk_disable_unprepare(mainck);
  285. return 0;
  286. }
  287. static struct platform_driver sdhci_at91_driver = {
  288. .driver = {
  289. .name = "sdhci-at91",
  290. .of_match_table = sdhci_at91_dt_match,
  291. .pm = &sdhci_at91_dev_pm_ops,
  292. },
  293. .probe = sdhci_at91_probe,
  294. .remove = sdhci_at91_remove,
  295. };
  296. module_platform_driver(sdhci_at91_driver);
  297. MODULE_DESCRIPTION("SDHCI driver for at91");
  298. MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>");
  299. MODULE_LICENSE("GPL v2");