sdhci-of-at91.c 13 KB

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