sdhci-of-at91.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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/err.h>
  18. #include <linux/io.h>
  19. #include <linux/mmc/host.h>
  20. #include <linux/mmc/slot-gpio.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/pm.h>
  25. #include <linux/pm_runtime.h>
  26. #include "sdhci-pltfm.h"
  27. #define SDMMC_CACR 0x230
  28. #define SDMMC_CACR_CAPWREN BIT(0)
  29. #define SDMMC_CACR_KEY (0x46 << 8)
  30. struct sdhci_at91_priv {
  31. struct clk *hclock;
  32. struct clk *gck;
  33. struct clk *mainck;
  34. };
  35. static const struct sdhci_ops sdhci_at91_sama5d2_ops = {
  36. .set_clock = sdhci_set_clock,
  37. .set_bus_width = sdhci_set_bus_width,
  38. .reset = sdhci_reset,
  39. .set_uhs_signaling = sdhci_set_uhs_signaling,
  40. };
  41. static const struct sdhci_pltfm_data soc_data_sama5d2 = {
  42. .ops = &sdhci_at91_sama5d2_ops,
  43. .quirks2 = SDHCI_QUIRK2_NEED_DELAY_AFTER_INT_CLK_RST,
  44. };
  45. static const struct of_device_id sdhci_at91_dt_match[] = {
  46. { .compatible = "atmel,sama5d2-sdhci", .data = &soc_data_sama5d2 },
  47. {}
  48. };
  49. #ifdef CONFIG_PM
  50. static int sdhci_at91_runtime_suspend(struct device *dev)
  51. {
  52. struct sdhci_host *host = dev_get_drvdata(dev);
  53. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  54. struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
  55. int ret;
  56. ret = sdhci_runtime_suspend_host(host);
  57. clk_disable_unprepare(priv->gck);
  58. clk_disable_unprepare(priv->hclock);
  59. clk_disable_unprepare(priv->mainck);
  60. return ret;
  61. }
  62. static int sdhci_at91_runtime_resume(struct device *dev)
  63. {
  64. struct sdhci_host *host = dev_get_drvdata(dev);
  65. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  66. struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
  67. int ret;
  68. ret = clk_prepare_enable(priv->mainck);
  69. if (ret) {
  70. dev_err(dev, "can't enable mainck\n");
  71. return ret;
  72. }
  73. ret = clk_prepare_enable(priv->hclock);
  74. if (ret) {
  75. dev_err(dev, "can't enable hclock\n");
  76. return ret;
  77. }
  78. ret = clk_prepare_enable(priv->gck);
  79. if (ret) {
  80. dev_err(dev, "can't enable gck\n");
  81. return ret;
  82. }
  83. return sdhci_runtime_resume_host(host);
  84. }
  85. #endif /* CONFIG_PM */
  86. static const struct dev_pm_ops sdhci_at91_dev_pm_ops = {
  87. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  88. pm_runtime_force_resume)
  89. SET_RUNTIME_PM_OPS(sdhci_at91_runtime_suspend,
  90. sdhci_at91_runtime_resume,
  91. NULL)
  92. };
  93. static int sdhci_at91_probe(struct platform_device *pdev)
  94. {
  95. const struct of_device_id *match;
  96. const struct sdhci_pltfm_data *soc_data;
  97. struct sdhci_host *host;
  98. struct sdhci_pltfm_host *pltfm_host;
  99. struct sdhci_at91_priv *priv;
  100. unsigned int caps0, caps1;
  101. unsigned int clk_base, clk_mul;
  102. unsigned int gck_rate, real_gck_rate;
  103. int ret;
  104. match = of_match_device(sdhci_at91_dt_match, &pdev->dev);
  105. if (!match)
  106. return -EINVAL;
  107. soc_data = match->data;
  108. host = sdhci_pltfm_init(pdev, soc_data, sizeof(*priv));
  109. if (IS_ERR(host))
  110. return PTR_ERR(host);
  111. pltfm_host = sdhci_priv(host);
  112. priv = sdhci_pltfm_priv(pltfm_host);
  113. priv->mainck = devm_clk_get(&pdev->dev, "baseclk");
  114. if (IS_ERR(priv->mainck)) {
  115. dev_err(&pdev->dev, "failed to get baseclk\n");
  116. return PTR_ERR(priv->mainck);
  117. }
  118. priv->hclock = devm_clk_get(&pdev->dev, "hclock");
  119. if (IS_ERR(priv->hclock)) {
  120. dev_err(&pdev->dev, "failed to get hclock\n");
  121. return PTR_ERR(priv->hclock);
  122. }
  123. priv->gck = devm_clk_get(&pdev->dev, "multclk");
  124. if (IS_ERR(priv->gck)) {
  125. dev_err(&pdev->dev, "failed to get multclk\n");
  126. return PTR_ERR(priv->gck);
  127. }
  128. /*
  129. * The mult clock is provided by as a generated clock by the PMC
  130. * controller. In order to set the rate of gck, we have to get the
  131. * base clock rate and the clock mult from capabilities.
  132. */
  133. clk_prepare_enable(priv->hclock);
  134. caps0 = readl(host->ioaddr + SDHCI_CAPABILITIES);
  135. caps1 = readl(host->ioaddr + SDHCI_CAPABILITIES_1);
  136. clk_base = (caps0 & SDHCI_CLOCK_V3_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
  137. clk_mul = (caps1 & SDHCI_CLOCK_MUL_MASK) >> SDHCI_CLOCK_MUL_SHIFT;
  138. gck_rate = clk_base * 1000000 * (clk_mul + 1);
  139. ret = clk_set_rate(priv->gck, gck_rate);
  140. if (ret < 0) {
  141. dev_err(&pdev->dev, "failed to set gck");
  142. goto hclock_disable_unprepare;
  143. }
  144. /*
  145. * We need to check if we have the requested rate for gck because in
  146. * some cases this rate could be not supported. If it happens, the rate
  147. * is the closest one gck can provide. We have to update the value
  148. * of clk mul.
  149. */
  150. real_gck_rate = clk_get_rate(priv->gck);
  151. if (real_gck_rate != gck_rate) {
  152. clk_mul = real_gck_rate / (clk_base * 1000000) - 1;
  153. caps1 &= (~SDHCI_CLOCK_MUL_MASK);
  154. caps1 |= ((clk_mul << SDHCI_CLOCK_MUL_SHIFT) & SDHCI_CLOCK_MUL_MASK);
  155. /* Set capabilities in r/w mode. */
  156. writel(SDMMC_CACR_KEY | SDMMC_CACR_CAPWREN, host->ioaddr + SDMMC_CACR);
  157. writel(caps1, host->ioaddr + SDHCI_CAPABILITIES_1);
  158. /* Set capabilities in ro mode. */
  159. writel(0, host->ioaddr + SDMMC_CACR);
  160. dev_info(&pdev->dev, "update clk mul to %u as gck rate is %u Hz\n",
  161. clk_mul, real_gck_rate);
  162. }
  163. clk_prepare_enable(priv->mainck);
  164. clk_prepare_enable(priv->gck);
  165. ret = mmc_of_parse(host->mmc);
  166. if (ret)
  167. goto clocks_disable_unprepare;
  168. sdhci_get_of_property(pdev);
  169. pm_runtime_get_noresume(&pdev->dev);
  170. pm_runtime_set_active(&pdev->dev);
  171. pm_runtime_enable(&pdev->dev);
  172. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  173. pm_runtime_use_autosuspend(&pdev->dev);
  174. ret = sdhci_add_host(host);
  175. if (ret)
  176. goto pm_runtime_disable;
  177. /*
  178. * When calling sdhci_runtime_suspend_host(), the sdhci layer makes
  179. * the assumption that all the clocks of the controller are disabled.
  180. * It means we can't get irq from it when it is runtime suspended.
  181. * For that reason, it is not planned to wake-up on a card detect irq
  182. * from the controller.
  183. * If we want to use runtime PM and to be able to wake-up on card
  184. * insertion, we have to use a GPIO for the card detection or we can
  185. * use polling. Be aware that using polling will resume/suspend the
  186. * controller between each attempt.
  187. * Disable SDHCI_QUIRK_BROKEN_CARD_DETECTION to be sure nobody tries
  188. * to enable polling via device tree with broken-cd property.
  189. */
  190. if (!(host->mmc->caps & MMC_CAP_NONREMOVABLE) &&
  191. IS_ERR_VALUE(mmc_gpio_get_cd(host->mmc))) {
  192. host->mmc->caps |= MMC_CAP_NEEDS_POLL;
  193. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  194. }
  195. pm_runtime_put_autosuspend(&pdev->dev);
  196. return 0;
  197. pm_runtime_disable:
  198. pm_runtime_disable(&pdev->dev);
  199. pm_runtime_set_suspended(&pdev->dev);
  200. pm_runtime_put_noidle(&pdev->dev);
  201. clocks_disable_unprepare:
  202. clk_disable_unprepare(priv->gck);
  203. clk_disable_unprepare(priv->mainck);
  204. hclock_disable_unprepare:
  205. clk_disable_unprepare(priv->hclock);
  206. sdhci_pltfm_free(pdev);
  207. return ret;
  208. }
  209. static int sdhci_at91_remove(struct platform_device *pdev)
  210. {
  211. struct sdhci_host *host = platform_get_drvdata(pdev);
  212. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  213. struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
  214. struct clk *gck = priv->gck;
  215. struct clk *hclock = priv->hclock;
  216. struct clk *mainck = priv->mainck;
  217. pm_runtime_get_sync(&pdev->dev);
  218. pm_runtime_disable(&pdev->dev);
  219. pm_runtime_put_noidle(&pdev->dev);
  220. sdhci_pltfm_unregister(pdev);
  221. clk_disable_unprepare(gck);
  222. clk_disable_unprepare(hclock);
  223. clk_disable_unprepare(mainck);
  224. return 0;
  225. }
  226. static struct platform_driver sdhci_at91_driver = {
  227. .driver = {
  228. .name = "sdhci-at91",
  229. .of_match_table = sdhci_at91_dt_match,
  230. .pm = &sdhci_at91_dev_pm_ops,
  231. },
  232. .probe = sdhci_at91_probe,
  233. .remove = sdhci_at91_remove,
  234. };
  235. module_platform_driver(sdhci_at91_driver);
  236. MODULE_DESCRIPTION("SDHCI driver for at91");
  237. MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>");
  238. MODULE_LICENSE("GPL v2");