sdhci-of-at91.c 11 KB

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