sdhci-of-at91.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Atmel SDMMC controller driver.
  4. *
  5. * Copyright (C) 2015 Atmel,
  6. * 2015 Ludovic Desroches <[email protected]>
  7. */
  8. #include <linux/bitfield.h>
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/err.h>
  12. #include <linux/io.h>
  13. #include <linux/iopoll.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mmc/host.h>
  16. #include <linux/mmc/slot-gpio.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_device.h>
  20. #include <linux/pm.h>
  21. #include <linux/pm_runtime.h>
  22. #include "sdhci-pltfm.h"
  23. #define SDMMC_MC1R 0x204
  24. #define SDMMC_MC1R_DDR BIT(3)
  25. #define SDMMC_MC1R_FCD BIT(7)
  26. #define SDMMC_CACR 0x230
  27. #define SDMMC_CACR_CAPWREN BIT(0)
  28. #define SDMMC_CACR_KEY (0x46 << 8)
  29. #define SDMMC_CALCR 0x240
  30. #define SDMMC_CALCR_EN BIT(0)
  31. #define SDMMC_CALCR_ALWYSON BIT(4)
  32. #define SDHCI_AT91_PRESET_COMMON_CONF 0x400 /* drv type B, programmable clock mode */
  33. struct sdhci_at91_soc_data {
  34. const struct sdhci_pltfm_data *pdata;
  35. bool baseclk_is_generated_internally;
  36. unsigned int divider_for_baseclk;
  37. };
  38. struct sdhci_at91_priv {
  39. const struct sdhci_at91_soc_data *soc_data;
  40. struct clk *hclock;
  41. struct clk *gck;
  42. struct clk *mainck;
  43. bool restore_needed;
  44. bool cal_always_on;
  45. };
  46. static void sdhci_at91_set_force_card_detect(struct sdhci_host *host)
  47. {
  48. u8 mc1r;
  49. mc1r = readb(host->ioaddr + SDMMC_MC1R);
  50. mc1r |= SDMMC_MC1R_FCD;
  51. writeb(mc1r, host->ioaddr + SDMMC_MC1R);
  52. }
  53. static void sdhci_at91_set_clock(struct sdhci_host *host, unsigned int clock)
  54. {
  55. u16 clk;
  56. host->mmc->actual_clock = 0;
  57. /*
  58. * There is no requirement to disable the internal clock before
  59. * changing the SD clock configuration. Moreover, disabling the
  60. * internal clock, changing the configuration and re-enabling the
  61. * internal clock causes some bugs. It can prevent to get the internal
  62. * clock stable flag ready and an unexpected switch to the base clock
  63. * when using presets.
  64. */
  65. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  66. clk &= SDHCI_CLOCK_INT_EN;
  67. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  68. if (clock == 0)
  69. return;
  70. clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
  71. clk |= SDHCI_CLOCK_INT_EN;
  72. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  73. /* Wait max 20 ms */
  74. if (read_poll_timeout(sdhci_readw, clk, (clk & SDHCI_CLOCK_INT_STABLE),
  75. 1000, 20000, false, host, SDHCI_CLOCK_CONTROL)) {
  76. pr_err("%s: Internal clock never stabilised.\n",
  77. mmc_hostname(host->mmc));
  78. return;
  79. }
  80. clk |= SDHCI_CLOCK_CARD_EN;
  81. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  82. }
  83. static void sdhci_at91_set_uhs_signaling(struct sdhci_host *host,
  84. unsigned int timing)
  85. {
  86. u8 mc1r;
  87. if (timing == MMC_TIMING_MMC_DDR52) {
  88. mc1r = sdhci_readb(host, SDMMC_MC1R);
  89. mc1r |= SDMMC_MC1R_DDR;
  90. sdhci_writeb(host, mc1r, SDMMC_MC1R);
  91. }
  92. sdhci_set_uhs_signaling(host, timing);
  93. }
  94. static void sdhci_at91_reset(struct sdhci_host *host, u8 mask)
  95. {
  96. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  97. struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
  98. unsigned int tmp;
  99. sdhci_reset(host, mask);
  100. if ((host->mmc->caps & MMC_CAP_NONREMOVABLE)
  101. || mmc_gpio_get_cd(host->mmc) >= 0)
  102. sdhci_at91_set_force_card_detect(host);
  103. if (priv->cal_always_on && (mask & SDHCI_RESET_ALL)) {
  104. u32 calcr = sdhci_readl(host, SDMMC_CALCR);
  105. sdhci_writel(host, calcr | SDMMC_CALCR_ALWYSON | SDMMC_CALCR_EN,
  106. SDMMC_CALCR);
  107. if (read_poll_timeout(sdhci_readl, tmp, !(tmp & SDMMC_CALCR_EN),
  108. 10, 20000, false, host, SDMMC_CALCR))
  109. dev_err(mmc_dev(host->mmc), "Failed to calibrate\n");
  110. }
  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_set_power_and_bus_voltage,
  118. };
  119. static const struct sdhci_pltfm_data sdhci_sama5d2_pdata = {
  120. .ops = &sdhci_at91_sama5d2_ops,
  121. };
  122. static const struct sdhci_at91_soc_data soc_data_sama5d2 = {
  123. .pdata = &sdhci_sama5d2_pdata,
  124. .baseclk_is_generated_internally = false,
  125. };
  126. static const struct sdhci_at91_soc_data soc_data_sam9x60 = {
  127. .pdata = &sdhci_sama5d2_pdata,
  128. .baseclk_is_generated_internally = true,
  129. .divider_for_baseclk = 2,
  130. };
  131. static const struct of_device_id sdhci_at91_dt_match[] = {
  132. { .compatible = "atmel,sama5d2-sdhci", .data = &soc_data_sama5d2 },
  133. { .compatible = "microchip,sam9x60-sdhci", .data = &soc_data_sam9x60 },
  134. {}
  135. };
  136. MODULE_DEVICE_TABLE(of, sdhci_at91_dt_match);
  137. static int sdhci_at91_set_clks_presets(struct device *dev)
  138. {
  139. struct sdhci_host *host = dev_get_drvdata(dev);
  140. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  141. struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
  142. unsigned int caps0, caps1;
  143. unsigned int clk_base, clk_mul;
  144. unsigned int gck_rate, clk_base_rate;
  145. unsigned int preset_div;
  146. clk_prepare_enable(priv->hclock);
  147. caps0 = readl(host->ioaddr + SDHCI_CAPABILITIES);
  148. caps1 = readl(host->ioaddr + SDHCI_CAPABILITIES_1);
  149. gck_rate = clk_get_rate(priv->gck);
  150. if (priv->soc_data->baseclk_is_generated_internally)
  151. clk_base_rate = gck_rate / priv->soc_data->divider_for_baseclk;
  152. else
  153. clk_base_rate = clk_get_rate(priv->mainck);
  154. clk_base = clk_base_rate / 1000000;
  155. clk_mul = gck_rate / clk_base_rate - 1;
  156. caps0 &= ~SDHCI_CLOCK_V3_BASE_MASK;
  157. caps0 |= FIELD_PREP(SDHCI_CLOCK_V3_BASE_MASK, clk_base);
  158. caps1 &= ~SDHCI_CLOCK_MUL_MASK;
  159. caps1 |= FIELD_PREP(SDHCI_CLOCK_MUL_MASK, clk_mul);
  160. /* Set capabilities in r/w mode. */
  161. writel(SDMMC_CACR_KEY | SDMMC_CACR_CAPWREN, host->ioaddr + SDMMC_CACR);
  162. writel(caps0, host->ioaddr + SDHCI_CAPABILITIES);
  163. writel(caps1, host->ioaddr + SDHCI_CAPABILITIES_1);
  164. /* Set capabilities in ro mode. */
  165. writel(0, host->ioaddr + SDMMC_CACR);
  166. dev_dbg(dev, "update clk mul to %u as gck rate is %u Hz and clk base is %u Hz\n",
  167. clk_mul, gck_rate, clk_base_rate);
  168. /*
  169. * We have to set preset values because it depends on the clk_mul
  170. * value. Moreover, SDR104 is supported in a degraded mode since the
  171. * maximum sd clock value is 120 MHz instead of 208 MHz. For that
  172. * reason, we need to use presets to support SDR104.
  173. */
  174. preset_div = DIV_ROUND_UP(gck_rate, 24000000) - 1;
  175. writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
  176. host->ioaddr + SDHCI_PRESET_FOR_SDR12);
  177. preset_div = DIV_ROUND_UP(gck_rate, 50000000) - 1;
  178. writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
  179. host->ioaddr + SDHCI_PRESET_FOR_SDR25);
  180. preset_div = DIV_ROUND_UP(gck_rate, 100000000) - 1;
  181. writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
  182. host->ioaddr + SDHCI_PRESET_FOR_SDR50);
  183. preset_div = DIV_ROUND_UP(gck_rate, 120000000) - 1;
  184. writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
  185. host->ioaddr + SDHCI_PRESET_FOR_SDR104);
  186. preset_div = DIV_ROUND_UP(gck_rate, 50000000) - 1;
  187. writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
  188. host->ioaddr + SDHCI_PRESET_FOR_DDR50);
  189. clk_prepare_enable(priv->mainck);
  190. clk_prepare_enable(priv->gck);
  191. return 0;
  192. }
  193. #ifdef CONFIG_PM_SLEEP
  194. static int sdhci_at91_suspend(struct device *dev)
  195. {
  196. struct sdhci_host *host = dev_get_drvdata(dev);
  197. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  198. struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
  199. int ret;
  200. ret = pm_runtime_force_suspend(dev);
  201. priv->restore_needed = true;
  202. return ret;
  203. }
  204. #endif /* CONFIG_PM_SLEEP */
  205. #ifdef CONFIG_PM
  206. static int sdhci_at91_runtime_suspend(struct device *dev)
  207. {
  208. struct sdhci_host *host = dev_get_drvdata(dev);
  209. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  210. struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
  211. int ret;
  212. ret = sdhci_runtime_suspend_host(host);
  213. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  214. mmc_retune_needed(host->mmc);
  215. clk_disable_unprepare(priv->gck);
  216. clk_disable_unprepare(priv->hclock);
  217. clk_disable_unprepare(priv->mainck);
  218. return ret;
  219. }
  220. static int sdhci_at91_runtime_resume(struct device *dev)
  221. {
  222. struct sdhci_host *host = dev_get_drvdata(dev);
  223. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  224. struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
  225. int ret;
  226. if (priv->restore_needed) {
  227. ret = sdhci_at91_set_clks_presets(dev);
  228. if (ret)
  229. return ret;
  230. priv->restore_needed = false;
  231. goto out;
  232. }
  233. ret = clk_prepare_enable(priv->mainck);
  234. if (ret) {
  235. dev_err(dev, "can't enable mainck\n");
  236. return ret;
  237. }
  238. ret = clk_prepare_enable(priv->hclock);
  239. if (ret) {
  240. dev_err(dev, "can't enable hclock\n");
  241. return ret;
  242. }
  243. ret = clk_prepare_enable(priv->gck);
  244. if (ret) {
  245. dev_err(dev, "can't enable gck\n");
  246. return ret;
  247. }
  248. out:
  249. return sdhci_runtime_resume_host(host, 0);
  250. }
  251. #endif /* CONFIG_PM */
  252. static const struct dev_pm_ops sdhci_at91_dev_pm_ops = {
  253. SET_SYSTEM_SLEEP_PM_OPS(sdhci_at91_suspend, pm_runtime_force_resume)
  254. SET_RUNTIME_PM_OPS(sdhci_at91_runtime_suspend,
  255. sdhci_at91_runtime_resume,
  256. NULL)
  257. };
  258. static int sdhci_at91_probe(struct platform_device *pdev)
  259. {
  260. const struct sdhci_at91_soc_data *soc_data;
  261. struct sdhci_host *host;
  262. struct sdhci_pltfm_host *pltfm_host;
  263. struct sdhci_at91_priv *priv;
  264. int ret;
  265. soc_data = of_device_get_match_data(&pdev->dev);
  266. if (!soc_data)
  267. return -EINVAL;
  268. host = sdhci_pltfm_init(pdev, soc_data->pdata, sizeof(*priv));
  269. if (IS_ERR(host))
  270. return PTR_ERR(host);
  271. pltfm_host = sdhci_priv(host);
  272. priv = sdhci_pltfm_priv(pltfm_host);
  273. priv->soc_data = soc_data;
  274. priv->mainck = devm_clk_get(&pdev->dev, "baseclk");
  275. if (IS_ERR(priv->mainck)) {
  276. if (soc_data->baseclk_is_generated_internally) {
  277. priv->mainck = NULL;
  278. } else {
  279. dev_err(&pdev->dev, "failed to get baseclk\n");
  280. ret = PTR_ERR(priv->mainck);
  281. goto sdhci_pltfm_free;
  282. }
  283. }
  284. priv->hclock = devm_clk_get(&pdev->dev, "hclock");
  285. if (IS_ERR(priv->hclock)) {
  286. dev_err(&pdev->dev, "failed to get hclock\n");
  287. ret = PTR_ERR(priv->hclock);
  288. goto sdhci_pltfm_free;
  289. }
  290. priv->gck = devm_clk_get(&pdev->dev, "multclk");
  291. if (IS_ERR(priv->gck)) {
  292. dev_err(&pdev->dev, "failed to get multclk\n");
  293. ret = PTR_ERR(priv->gck);
  294. goto sdhci_pltfm_free;
  295. }
  296. ret = sdhci_at91_set_clks_presets(&pdev->dev);
  297. if (ret)
  298. goto sdhci_pltfm_free;
  299. priv->restore_needed = false;
  300. /*
  301. * if SDCAL pin is wrongly connected, we must enable
  302. * the analog calibration cell permanently.
  303. */
  304. priv->cal_always_on =
  305. device_property_read_bool(&pdev->dev,
  306. "microchip,sdcal-inverted");
  307. ret = mmc_of_parse(host->mmc);
  308. if (ret)
  309. goto clocks_disable_unprepare;
  310. sdhci_get_of_property(pdev);
  311. pm_runtime_get_noresume(&pdev->dev);
  312. pm_runtime_set_active(&pdev->dev);
  313. pm_runtime_enable(&pdev->dev);
  314. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  315. pm_runtime_use_autosuspend(&pdev->dev);
  316. /* HS200 is broken at this moment */
  317. host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
  318. ret = sdhci_add_host(host);
  319. if (ret)
  320. goto pm_runtime_disable;
  321. /*
  322. * When calling sdhci_runtime_suspend_host(), the sdhci layer makes
  323. * the assumption that all the clocks of the controller are disabled.
  324. * It means we can't get irq from it when it is runtime suspended.
  325. * For that reason, it is not planned to wake-up on a card detect irq
  326. * from the controller.
  327. * If we want to use runtime PM and to be able to wake-up on card
  328. * insertion, we have to use a GPIO for the card detection or we can
  329. * use polling. Be aware that using polling will resume/suspend the
  330. * controller between each attempt.
  331. * Disable SDHCI_QUIRK_BROKEN_CARD_DETECTION to be sure nobody tries
  332. * to enable polling via device tree with broken-cd property.
  333. */
  334. if (mmc_card_is_removable(host->mmc) &&
  335. mmc_gpio_get_cd(host->mmc) < 0) {
  336. host->mmc->caps |= MMC_CAP_NEEDS_POLL;
  337. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  338. }
  339. /*
  340. * If the device attached to the MMC bus is not removable, it is safer
  341. * to set the Force Card Detect bit. People often don't connect the
  342. * card detect signal and use this pin for another purpose. If the card
  343. * detect pin is not muxed to SDHCI controller, a default value is
  344. * used. This value can be different from a SoC revision to another
  345. * one. Problems come when this default value is not card present. To
  346. * avoid this case, if the device is non removable then the card
  347. * detection procedure using the SDMCC_CD signal is bypassed.
  348. * This bit is reset when a software reset for all command is performed
  349. * so we need to implement our own reset function to set back this bit.
  350. *
  351. * WA: SAMA5D2 doesn't drive CMD if using CD GPIO line.
  352. */
  353. if ((host->mmc->caps & MMC_CAP_NONREMOVABLE)
  354. || mmc_gpio_get_cd(host->mmc) >= 0)
  355. sdhci_at91_set_force_card_detect(host);
  356. pm_runtime_put_autosuspend(&pdev->dev);
  357. return 0;
  358. pm_runtime_disable:
  359. pm_runtime_disable(&pdev->dev);
  360. pm_runtime_set_suspended(&pdev->dev);
  361. pm_runtime_put_noidle(&pdev->dev);
  362. clocks_disable_unprepare:
  363. clk_disable_unprepare(priv->gck);
  364. clk_disable_unprepare(priv->mainck);
  365. clk_disable_unprepare(priv->hclock);
  366. sdhci_pltfm_free:
  367. sdhci_pltfm_free(pdev);
  368. return ret;
  369. }
  370. static int sdhci_at91_remove(struct platform_device *pdev)
  371. {
  372. struct sdhci_host *host = platform_get_drvdata(pdev);
  373. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  374. struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
  375. struct clk *gck = priv->gck;
  376. struct clk *hclock = priv->hclock;
  377. struct clk *mainck = priv->mainck;
  378. pm_runtime_get_sync(&pdev->dev);
  379. pm_runtime_disable(&pdev->dev);
  380. pm_runtime_put_noidle(&pdev->dev);
  381. sdhci_pltfm_unregister(pdev);
  382. clk_disable_unprepare(gck);
  383. clk_disable_unprepare(hclock);
  384. clk_disable_unprepare(mainck);
  385. return 0;
  386. }
  387. static struct platform_driver sdhci_at91_driver = {
  388. .driver = {
  389. .name = "sdhci-at91",
  390. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  391. .of_match_table = sdhci_at91_dt_match,
  392. .pm = &sdhci_at91_dev_pm_ops,
  393. },
  394. .probe = sdhci_at91_probe,
  395. .remove = sdhci_at91_remove,
  396. };
  397. module_platform_driver(sdhci_at91_driver);
  398. MODULE_DESCRIPTION("SDHCI driver for at91");
  399. MODULE_AUTHOR("Ludovic Desroches <[email protected]>");
  400. MODULE_LICENSE("GPL v2");