sdhci-of-at91.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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/err.h>
  18. #include <linux/io.h>
  19. #include <linux/mmc/host.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #include "sdhci-pltfm.h"
  24. #define SDMMC_CACR 0x230
  25. #define SDMMC_CACR_CAPWREN BIT(0)
  26. #define SDMMC_CACR_KEY (0x46 << 8)
  27. struct sdhci_at91_priv {
  28. struct clk *hclock;
  29. struct clk *gck;
  30. struct clk *mainck;
  31. };
  32. static const struct sdhci_ops sdhci_at91_sama5d2_ops = {
  33. .set_clock = sdhci_set_clock,
  34. .set_bus_width = sdhci_set_bus_width,
  35. .reset = sdhci_reset,
  36. .set_uhs_signaling = sdhci_set_uhs_signaling,
  37. };
  38. static const struct sdhci_pltfm_data soc_data_sama5d2 = {
  39. .ops = &sdhci_at91_sama5d2_ops,
  40. .quirks2 = SDHCI_QUIRK2_NEED_DELAY_AFTER_INT_CLK_RST,
  41. };
  42. static const struct of_device_id sdhci_at91_dt_match[] = {
  43. { .compatible = "atmel,sama5d2-sdhci", .data = &soc_data_sama5d2 },
  44. {}
  45. };
  46. static int sdhci_at91_probe(struct platform_device *pdev)
  47. {
  48. const struct of_device_id *match;
  49. const struct sdhci_pltfm_data *soc_data;
  50. struct sdhci_host *host;
  51. struct sdhci_pltfm_host *pltfm_host;
  52. struct sdhci_at91_priv *priv;
  53. unsigned int caps0, caps1;
  54. unsigned int clk_base, clk_mul;
  55. unsigned int gck_rate, real_gck_rate;
  56. int ret;
  57. match = of_match_device(sdhci_at91_dt_match, &pdev->dev);
  58. if (!match)
  59. return -EINVAL;
  60. soc_data = match->data;
  61. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  62. if (!priv) {
  63. dev_err(&pdev->dev, "unable to allocate private data\n");
  64. return -ENOMEM;
  65. }
  66. priv->mainck = devm_clk_get(&pdev->dev, "baseclk");
  67. if (IS_ERR(priv->mainck)) {
  68. dev_err(&pdev->dev, "failed to get baseclk\n");
  69. return PTR_ERR(priv->mainck);
  70. }
  71. priv->hclock = devm_clk_get(&pdev->dev, "hclock");
  72. if (IS_ERR(priv->hclock)) {
  73. dev_err(&pdev->dev, "failed to get hclock\n");
  74. return PTR_ERR(priv->hclock);
  75. }
  76. priv->gck = devm_clk_get(&pdev->dev, "multclk");
  77. if (IS_ERR(priv->gck)) {
  78. dev_err(&pdev->dev, "failed to get multclk\n");
  79. return PTR_ERR(priv->gck);
  80. }
  81. host = sdhci_pltfm_init(pdev, soc_data, 0);
  82. if (IS_ERR(host))
  83. return PTR_ERR(host);
  84. /*
  85. * The mult clock is provided by as a generated clock by the PMC
  86. * controller. In order to set the rate of gck, we have to get the
  87. * base clock rate and the clock mult from capabilities.
  88. */
  89. clk_prepare_enable(priv->hclock);
  90. caps0 = readl(host->ioaddr + SDHCI_CAPABILITIES);
  91. caps1 = readl(host->ioaddr + SDHCI_CAPABILITIES_1);
  92. clk_base = (caps0 & SDHCI_CLOCK_V3_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
  93. clk_mul = (caps1 & SDHCI_CLOCK_MUL_MASK) >> SDHCI_CLOCK_MUL_SHIFT;
  94. gck_rate = clk_base * 1000000 * (clk_mul + 1);
  95. ret = clk_set_rate(priv->gck, gck_rate);
  96. if (ret < 0) {
  97. dev_err(&pdev->dev, "failed to set gck");
  98. goto hclock_disable_unprepare;
  99. return -EINVAL;
  100. }
  101. /*
  102. * We need to check if we have the requested rate for gck because in
  103. * some cases this rate could be not supported. If it happens, the rate
  104. * is the closest one gck can provide. We have to update the value
  105. * of clk mul.
  106. */
  107. real_gck_rate = clk_get_rate(priv->gck);
  108. if (real_gck_rate != gck_rate) {
  109. clk_mul = real_gck_rate / (clk_base * 1000000) - 1;
  110. caps1 &= (~SDHCI_CLOCK_MUL_MASK);
  111. caps1 |= ((clk_mul << SDHCI_CLOCK_MUL_SHIFT) & SDHCI_CLOCK_MUL_MASK);
  112. /* Set capabilities in r/w mode. */
  113. writel(SDMMC_CACR_KEY | SDMMC_CACR_CAPWREN, host->ioaddr + SDMMC_CACR);
  114. writel(caps1, host->ioaddr + SDHCI_CAPABILITIES_1);
  115. /* Set capabilities in ro mode. */
  116. writel(0, host->ioaddr + SDMMC_CACR);
  117. dev_info(&pdev->dev, "update clk mul to %u as gck rate is %u Hz\n",
  118. clk_mul, real_gck_rate);
  119. }
  120. clk_prepare_enable(priv->mainck);
  121. clk_prepare_enable(priv->gck);
  122. pltfm_host = sdhci_priv(host);
  123. pltfm_host->priv = priv;
  124. ret = mmc_of_parse(host->mmc);
  125. if (ret)
  126. goto clocks_disable_unprepare;
  127. sdhci_get_of_property(pdev);
  128. ret = sdhci_add_host(host);
  129. if (ret)
  130. goto clocks_disable_unprepare;
  131. return 0;
  132. clocks_disable_unprepare:
  133. clk_disable_unprepare(priv->gck);
  134. clk_disable_unprepare(priv->mainck);
  135. hclock_disable_unprepare:
  136. clk_disable_unprepare(priv->hclock);
  137. sdhci_pltfm_free(pdev);
  138. return ret;
  139. }
  140. static int sdhci_at91_remove(struct platform_device *pdev)
  141. {
  142. struct sdhci_host *host = platform_get_drvdata(pdev);
  143. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  144. struct sdhci_at91_priv *priv = pltfm_host->priv;
  145. sdhci_pltfm_unregister(pdev);
  146. clk_disable_unprepare(priv->gck);
  147. clk_disable_unprepare(priv->hclock);
  148. clk_disable_unprepare(priv->mainck);
  149. return 0;
  150. }
  151. static struct platform_driver sdhci_at91_driver = {
  152. .driver = {
  153. .name = "sdhci-at91",
  154. .of_match_table = sdhci_at91_dt_match,
  155. .pm = SDHCI_PLTFM_PMOPS,
  156. },
  157. .probe = sdhci_at91_probe,
  158. .remove = sdhci_at91_remove,
  159. };
  160. module_platform_driver(sdhci_at91_driver);
  161. MODULE_DESCRIPTION("SDHCI driver for at91");
  162. MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>");
  163. MODULE_LICENSE("GPL v2");