sdhci-of-at91.c 12 KB

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