sdhci-of-at91.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. || mmc_gpio_get_cd(host->mmc) >= 0)
  111. sdhci_at91_set_force_card_detect(host);
  112. }
  113. static const struct sdhci_ops sdhci_at91_sama5d2_ops = {
  114. .set_clock = sdhci_at91_set_clock,
  115. .set_bus_width = sdhci_set_bus_width,
  116. .reset = sdhci_at91_reset,
  117. .set_uhs_signaling = sdhci_at91_set_uhs_signaling,
  118. .set_power = sdhci_at91_set_power,
  119. };
  120. static const struct sdhci_pltfm_data soc_data_sama5d2 = {
  121. .ops = &sdhci_at91_sama5d2_ops,
  122. };
  123. static const struct of_device_id sdhci_at91_dt_match[] = {
  124. { .compatible = "atmel,sama5d2-sdhci", .data = &soc_data_sama5d2 },
  125. {}
  126. };
  127. MODULE_DEVICE_TABLE(of, sdhci_at91_dt_match);
  128. static int sdhci_at91_set_clks_presets(struct device *dev)
  129. {
  130. struct sdhci_host *host = dev_get_drvdata(dev);
  131. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  132. struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
  133. int ret;
  134. unsigned int caps0, caps1;
  135. unsigned int clk_base, clk_mul;
  136. unsigned int gck_rate, real_gck_rate;
  137. unsigned int preset_div;
  138. /*
  139. * The mult clock is provided by as a generated clock by the PMC
  140. * controller. In order to set the rate of gck, we have to get the
  141. * base clock rate and the clock mult from capabilities.
  142. */
  143. clk_prepare_enable(priv->hclock);
  144. caps0 = readl(host->ioaddr + SDHCI_CAPABILITIES);
  145. caps1 = readl(host->ioaddr + SDHCI_CAPABILITIES_1);
  146. clk_base = (caps0 & SDHCI_CLOCK_V3_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
  147. clk_mul = (caps1 & SDHCI_CLOCK_MUL_MASK) >> SDHCI_CLOCK_MUL_SHIFT;
  148. gck_rate = clk_base * 1000000 * (clk_mul + 1);
  149. ret = clk_set_rate(priv->gck, gck_rate);
  150. if (ret < 0) {
  151. dev_err(dev, "failed to set gck");
  152. clk_disable_unprepare(priv->hclock);
  153. return ret;
  154. }
  155. /*
  156. * We need to check if we have the requested rate for gck because in
  157. * some cases this rate could be not supported. If it happens, the rate
  158. * is the closest one gck can provide. We have to update the value
  159. * of clk mul.
  160. */
  161. real_gck_rate = clk_get_rate(priv->gck);
  162. if (real_gck_rate != gck_rate) {
  163. clk_mul = real_gck_rate / (clk_base * 1000000) - 1;
  164. caps1 &= (~SDHCI_CLOCK_MUL_MASK);
  165. caps1 |= ((clk_mul << SDHCI_CLOCK_MUL_SHIFT) &
  166. SDHCI_CLOCK_MUL_MASK);
  167. /* Set capabilities in r/w mode. */
  168. writel(SDMMC_CACR_KEY | SDMMC_CACR_CAPWREN,
  169. host->ioaddr + SDMMC_CACR);
  170. writel(caps1, host->ioaddr + SDHCI_CAPABILITIES_1);
  171. /* Set capabilities in ro mode. */
  172. writel(0, host->ioaddr + SDMMC_CACR);
  173. dev_info(dev, "update clk mul to %u as gck rate is %u Hz\n",
  174. clk_mul, real_gck_rate);
  175. }
  176. /*
  177. * We have to set preset values because it depends on the clk_mul
  178. * value. Moreover, SDR104 is supported in a degraded mode since the
  179. * maximum sd clock value is 120 MHz instead of 208 MHz. For that
  180. * reason, we need to use presets to support SDR104.
  181. */
  182. preset_div = DIV_ROUND_UP(real_gck_rate, 24000000) - 1;
  183. writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
  184. host->ioaddr + SDHCI_PRESET_FOR_SDR12);
  185. preset_div = DIV_ROUND_UP(real_gck_rate, 50000000) - 1;
  186. writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
  187. host->ioaddr + SDHCI_PRESET_FOR_SDR25);
  188. preset_div = DIV_ROUND_UP(real_gck_rate, 100000000) - 1;
  189. writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
  190. host->ioaddr + SDHCI_PRESET_FOR_SDR50);
  191. preset_div = DIV_ROUND_UP(real_gck_rate, 120000000) - 1;
  192. writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
  193. host->ioaddr + SDHCI_PRESET_FOR_SDR104);
  194. preset_div = DIV_ROUND_UP(real_gck_rate, 50000000) - 1;
  195. writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
  196. host->ioaddr + SDHCI_PRESET_FOR_DDR50);
  197. clk_prepare_enable(priv->mainck);
  198. clk_prepare_enable(priv->gck);
  199. return 0;
  200. }
  201. #ifdef CONFIG_PM_SLEEP
  202. static int sdhci_at91_suspend(struct device *dev)
  203. {
  204. struct sdhci_host *host = dev_get_drvdata(dev);
  205. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  206. struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
  207. int ret;
  208. ret = pm_runtime_force_suspend(dev);
  209. priv->restore_needed = true;
  210. return ret;
  211. }
  212. #endif /* CONFIG_PM_SLEEP */
  213. #ifdef CONFIG_PM
  214. static int sdhci_at91_runtime_suspend(struct device *dev)
  215. {
  216. struct sdhci_host *host = dev_get_drvdata(dev);
  217. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  218. struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
  219. int ret;
  220. ret = sdhci_runtime_suspend_host(host);
  221. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  222. mmc_retune_needed(host->mmc);
  223. clk_disable_unprepare(priv->gck);
  224. clk_disable_unprepare(priv->hclock);
  225. clk_disable_unprepare(priv->mainck);
  226. return ret;
  227. }
  228. static int sdhci_at91_runtime_resume(struct device *dev)
  229. {
  230. struct sdhci_host *host = dev_get_drvdata(dev);
  231. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  232. struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
  233. int ret;
  234. if (priv->restore_needed) {
  235. ret = sdhci_at91_set_clks_presets(dev);
  236. if (ret)
  237. return ret;
  238. priv->restore_needed = false;
  239. goto out;
  240. }
  241. ret = clk_prepare_enable(priv->mainck);
  242. if (ret) {
  243. dev_err(dev, "can't enable mainck\n");
  244. return ret;
  245. }
  246. ret = clk_prepare_enable(priv->hclock);
  247. if (ret) {
  248. dev_err(dev, "can't enable hclock\n");
  249. return ret;
  250. }
  251. ret = clk_prepare_enable(priv->gck);
  252. if (ret) {
  253. dev_err(dev, "can't enable gck\n");
  254. return ret;
  255. }
  256. out:
  257. return sdhci_runtime_resume_host(host);
  258. }
  259. #endif /* CONFIG_PM */
  260. static const struct dev_pm_ops sdhci_at91_dev_pm_ops = {
  261. SET_SYSTEM_SLEEP_PM_OPS(sdhci_at91_suspend, pm_runtime_force_resume)
  262. SET_RUNTIME_PM_OPS(sdhci_at91_runtime_suspend,
  263. sdhci_at91_runtime_resume,
  264. NULL)
  265. };
  266. static int sdhci_at91_probe(struct platform_device *pdev)
  267. {
  268. const struct of_device_id *match;
  269. const struct sdhci_pltfm_data *soc_data;
  270. struct sdhci_host *host;
  271. struct sdhci_pltfm_host *pltfm_host;
  272. struct sdhci_at91_priv *priv;
  273. int ret;
  274. match = of_match_device(sdhci_at91_dt_match, &pdev->dev);
  275. if (!match)
  276. return -EINVAL;
  277. soc_data = match->data;
  278. host = sdhci_pltfm_init(pdev, soc_data, sizeof(*priv));
  279. if (IS_ERR(host))
  280. return PTR_ERR(host);
  281. pltfm_host = sdhci_priv(host);
  282. priv = sdhci_pltfm_priv(pltfm_host);
  283. priv->mainck = devm_clk_get(&pdev->dev, "baseclk");
  284. if (IS_ERR(priv->mainck)) {
  285. dev_err(&pdev->dev, "failed to get baseclk\n");
  286. ret = PTR_ERR(priv->mainck);
  287. goto sdhci_pltfm_free;
  288. }
  289. priv->hclock = devm_clk_get(&pdev->dev, "hclock");
  290. if (IS_ERR(priv->hclock)) {
  291. dev_err(&pdev->dev, "failed to get hclock\n");
  292. ret = PTR_ERR(priv->hclock);
  293. goto sdhci_pltfm_free;
  294. }
  295. priv->gck = devm_clk_get(&pdev->dev, "multclk");
  296. if (IS_ERR(priv->gck)) {
  297. dev_err(&pdev->dev, "failed to get multclk\n");
  298. ret = PTR_ERR(priv->gck);
  299. goto sdhci_pltfm_free;
  300. }
  301. ret = sdhci_at91_set_clks_presets(&pdev->dev);
  302. if (ret)
  303. goto sdhci_pltfm_free;
  304. priv->restore_needed = false;
  305. ret = mmc_of_parse(host->mmc);
  306. if (ret)
  307. goto clocks_disable_unprepare;
  308. sdhci_get_of_property(pdev);
  309. pm_runtime_get_noresume(&pdev->dev);
  310. pm_runtime_set_active(&pdev->dev);
  311. pm_runtime_enable(&pdev->dev);
  312. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  313. pm_runtime_use_autosuspend(&pdev->dev);
  314. /* HS200 is broken at this moment */
  315. host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
  316. ret = sdhci_add_host(host);
  317. if (ret)
  318. goto pm_runtime_disable;
  319. /*
  320. * When calling sdhci_runtime_suspend_host(), the sdhci layer makes
  321. * the assumption that all the clocks of the controller are disabled.
  322. * It means we can't get irq from it when it is runtime suspended.
  323. * For that reason, it is not planned to wake-up on a card detect irq
  324. * from the controller.
  325. * If we want to use runtime PM and to be able to wake-up on card
  326. * insertion, we have to use a GPIO for the card detection or we can
  327. * use polling. Be aware that using polling will resume/suspend the
  328. * controller between each attempt.
  329. * Disable SDHCI_QUIRK_BROKEN_CARD_DETECTION to be sure nobody tries
  330. * to enable polling via device tree with broken-cd property.
  331. */
  332. if (mmc_card_is_removable(host->mmc) &&
  333. mmc_gpio_get_cd(host->mmc) < 0) {
  334. host->mmc->caps |= MMC_CAP_NEEDS_POLL;
  335. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  336. }
  337. /*
  338. * If the device attached to the MMC bus is not removable, it is safer
  339. * to set the Force Card Detect bit. People often don't connect the
  340. * card detect signal and use this pin for another purpose. If the card
  341. * detect pin is not muxed to SDHCI controller, a default value is
  342. * used. This value can be different from a SoC revision to another
  343. * one. Problems come when this default value is not card present. To
  344. * avoid this case, if the device is non removable then the card
  345. * detection procedure using the SDMCC_CD signal is bypassed.
  346. * This bit is reset when a software reset for all command is performed
  347. * so we need to implement our own reset function to set back this bit.
  348. *
  349. * WA: SAMA5D2 doesn't drive CMD if using CD GPIO line.
  350. */
  351. if ((host->mmc->caps & MMC_CAP_NONREMOVABLE)
  352. || mmc_gpio_get_cd(host->mmc) >= 0)
  353. sdhci_at91_set_force_card_detect(host);
  354. pm_runtime_put_autosuspend(&pdev->dev);
  355. return 0;
  356. pm_runtime_disable:
  357. pm_runtime_disable(&pdev->dev);
  358. pm_runtime_set_suspended(&pdev->dev);
  359. pm_runtime_put_noidle(&pdev->dev);
  360. clocks_disable_unprepare:
  361. clk_disable_unprepare(priv->gck);
  362. clk_disable_unprepare(priv->mainck);
  363. clk_disable_unprepare(priv->hclock);
  364. sdhci_pltfm_free:
  365. sdhci_pltfm_free(pdev);
  366. return ret;
  367. }
  368. static int sdhci_at91_remove(struct platform_device *pdev)
  369. {
  370. struct sdhci_host *host = platform_get_drvdata(pdev);
  371. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  372. struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
  373. struct clk *gck = priv->gck;
  374. struct clk *hclock = priv->hclock;
  375. struct clk *mainck = priv->mainck;
  376. pm_runtime_get_sync(&pdev->dev);
  377. pm_runtime_disable(&pdev->dev);
  378. pm_runtime_put_noidle(&pdev->dev);
  379. sdhci_pltfm_unregister(pdev);
  380. clk_disable_unprepare(gck);
  381. clk_disable_unprepare(hclock);
  382. clk_disable_unprepare(mainck);
  383. return 0;
  384. }
  385. static struct platform_driver sdhci_at91_driver = {
  386. .driver = {
  387. .name = "sdhci-at91",
  388. .of_match_table = sdhci_at91_dt_match,
  389. .pm = &sdhci_at91_dev_pm_ops,
  390. },
  391. .probe = sdhci_at91_probe,
  392. .remove = sdhci_at91_remove,
  393. };
  394. module_platform_driver(sdhci_at91_driver);
  395. MODULE_DESCRIPTION("SDHCI driver for at91");
  396. MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>");
  397. MODULE_LICENSE("GPL v2");