sdhci-tegra.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright (C) 2010 Google, Inc.
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <linux/err.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/clk.h>
  19. #include <linux/io.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/of_gpio.h>
  23. #include <linux/gpio.h>
  24. #include <linux/mmc/card.h>
  25. #include <linux/mmc/host.h>
  26. #include <linux/mmc/slot-gpio.h>
  27. #include <asm/gpio.h>
  28. #include "sdhci-pltfm.h"
  29. /* Tegra SDHOST controller vendor register definitions */
  30. #define SDHCI_TEGRA_VENDOR_MISC_CTRL 0x120
  31. #define SDHCI_MISC_CTRL_ENABLE_SDR104 0x8
  32. #define SDHCI_MISC_CTRL_ENABLE_SDR50 0x10
  33. #define SDHCI_MISC_CTRL_ENABLE_SDHCI_SPEC_300 0x20
  34. #define SDHCI_MISC_CTRL_ENABLE_DDR50 0x200
  35. #define NVQUIRK_FORCE_SDHCI_SPEC_200 BIT(0)
  36. #define NVQUIRK_ENABLE_BLOCK_GAP_DET BIT(1)
  37. #define NVQUIRK_ENABLE_SDHCI_SPEC_300 BIT(2)
  38. #define NVQUIRK_DISABLE_SDR50 BIT(3)
  39. #define NVQUIRK_DISABLE_SDR104 BIT(4)
  40. #define NVQUIRK_DISABLE_DDR50 BIT(5)
  41. struct sdhci_tegra_soc_data {
  42. const struct sdhci_pltfm_data *pdata;
  43. u32 nvquirks;
  44. };
  45. struct sdhci_tegra {
  46. const struct sdhci_tegra_soc_data *soc_data;
  47. int power_gpio;
  48. };
  49. static u16 tegra_sdhci_readw(struct sdhci_host *host, int reg)
  50. {
  51. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  52. struct sdhci_tegra *tegra_host = pltfm_host->priv;
  53. const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
  54. if (unlikely((soc_data->nvquirks & NVQUIRK_FORCE_SDHCI_SPEC_200) &&
  55. (reg == SDHCI_HOST_VERSION))) {
  56. /* Erratum: Version register is invalid in HW. */
  57. return SDHCI_SPEC_200;
  58. }
  59. return readw(host->ioaddr + reg);
  60. }
  61. static void tegra_sdhci_writel(struct sdhci_host *host, u32 val, int reg)
  62. {
  63. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  64. struct sdhci_tegra *tegra_host = pltfm_host->priv;
  65. const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
  66. /* Seems like we're getting spurious timeout and crc errors, so
  67. * disable signalling of them. In case of real errors software
  68. * timers should take care of eventually detecting them.
  69. */
  70. if (unlikely(reg == SDHCI_SIGNAL_ENABLE))
  71. val &= ~(SDHCI_INT_TIMEOUT|SDHCI_INT_CRC);
  72. writel(val, host->ioaddr + reg);
  73. if (unlikely((soc_data->nvquirks & NVQUIRK_ENABLE_BLOCK_GAP_DET) &&
  74. (reg == SDHCI_INT_ENABLE))) {
  75. /* Erratum: Must enable block gap interrupt detection */
  76. u8 gap_ctrl = readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL);
  77. if (val & SDHCI_INT_CARD_INT)
  78. gap_ctrl |= 0x8;
  79. else
  80. gap_ctrl &= ~0x8;
  81. writeb(gap_ctrl, host->ioaddr + SDHCI_BLOCK_GAP_CONTROL);
  82. }
  83. }
  84. static unsigned int tegra_sdhci_get_ro(struct sdhci_host *host)
  85. {
  86. return mmc_gpio_get_ro(host->mmc);
  87. }
  88. static void tegra_sdhci_reset(struct sdhci_host *host, u8 mask)
  89. {
  90. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  91. struct sdhci_tegra *tegra_host = pltfm_host->priv;
  92. const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
  93. u32 misc_ctrl;
  94. sdhci_reset(host, mask);
  95. if (!(mask & SDHCI_RESET_ALL))
  96. return;
  97. misc_ctrl = sdhci_readw(host, SDHCI_TEGRA_VENDOR_MISC_CTRL);
  98. /* Erratum: Enable SDHCI spec v3.00 support */
  99. if (soc_data->nvquirks & NVQUIRK_ENABLE_SDHCI_SPEC_300)
  100. misc_ctrl |= SDHCI_MISC_CTRL_ENABLE_SDHCI_SPEC_300;
  101. /* Don't advertise UHS modes which aren't supported yet */
  102. if (soc_data->nvquirks & NVQUIRK_DISABLE_SDR50)
  103. misc_ctrl &= ~SDHCI_MISC_CTRL_ENABLE_SDR50;
  104. if (soc_data->nvquirks & NVQUIRK_DISABLE_DDR50)
  105. misc_ctrl &= ~SDHCI_MISC_CTRL_ENABLE_DDR50;
  106. if (soc_data->nvquirks & NVQUIRK_DISABLE_SDR104)
  107. misc_ctrl &= ~SDHCI_MISC_CTRL_ENABLE_SDR104;
  108. sdhci_writew(host, misc_ctrl, SDHCI_TEGRA_VENDOR_MISC_CTRL);
  109. }
  110. static void tegra_sdhci_set_bus_width(struct sdhci_host *host, int bus_width)
  111. {
  112. u32 ctrl;
  113. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  114. if ((host->mmc->caps & MMC_CAP_8_BIT_DATA) &&
  115. (bus_width == MMC_BUS_WIDTH_8)) {
  116. ctrl &= ~SDHCI_CTRL_4BITBUS;
  117. ctrl |= SDHCI_CTRL_8BITBUS;
  118. } else {
  119. ctrl &= ~SDHCI_CTRL_8BITBUS;
  120. if (bus_width == MMC_BUS_WIDTH_4)
  121. ctrl |= SDHCI_CTRL_4BITBUS;
  122. else
  123. ctrl &= ~SDHCI_CTRL_4BITBUS;
  124. }
  125. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  126. }
  127. static const struct sdhci_ops tegra_sdhci_ops = {
  128. .get_ro = tegra_sdhci_get_ro,
  129. .read_w = tegra_sdhci_readw,
  130. .write_l = tegra_sdhci_writel,
  131. .set_clock = sdhci_set_clock,
  132. .set_bus_width = tegra_sdhci_set_bus_width,
  133. .reset = tegra_sdhci_reset,
  134. .set_uhs_signaling = sdhci_set_uhs_signaling,
  135. .get_max_clock = sdhci_pltfm_clk_get_max_clock,
  136. };
  137. static const struct sdhci_pltfm_data sdhci_tegra20_pdata = {
  138. .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
  139. SDHCI_QUIRK_SINGLE_POWER_WRITE |
  140. SDHCI_QUIRK_NO_HISPD_BIT |
  141. SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC |
  142. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
  143. .ops = &tegra_sdhci_ops,
  144. };
  145. static struct sdhci_tegra_soc_data soc_data_tegra20 = {
  146. .pdata = &sdhci_tegra20_pdata,
  147. .nvquirks = NVQUIRK_FORCE_SDHCI_SPEC_200 |
  148. NVQUIRK_ENABLE_BLOCK_GAP_DET,
  149. };
  150. static const struct sdhci_pltfm_data sdhci_tegra30_pdata = {
  151. .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
  152. SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
  153. SDHCI_QUIRK_SINGLE_POWER_WRITE |
  154. SDHCI_QUIRK_NO_HISPD_BIT |
  155. SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC |
  156. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
  157. .ops = &tegra_sdhci_ops,
  158. };
  159. static struct sdhci_tegra_soc_data soc_data_tegra30 = {
  160. .pdata = &sdhci_tegra30_pdata,
  161. .nvquirks = NVQUIRK_ENABLE_SDHCI_SPEC_300 |
  162. NVQUIRK_DISABLE_SDR50 |
  163. NVQUIRK_DISABLE_SDR104,
  164. };
  165. static const struct sdhci_pltfm_data sdhci_tegra114_pdata = {
  166. .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
  167. SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
  168. SDHCI_QUIRK_SINGLE_POWER_WRITE |
  169. SDHCI_QUIRK_NO_HISPD_BIT |
  170. SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC |
  171. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
  172. .ops = &tegra_sdhci_ops,
  173. };
  174. static struct sdhci_tegra_soc_data soc_data_tegra114 = {
  175. .pdata = &sdhci_tegra114_pdata,
  176. .nvquirks = NVQUIRK_DISABLE_SDR50 |
  177. NVQUIRK_DISABLE_DDR50 |
  178. NVQUIRK_DISABLE_SDR104,
  179. };
  180. static const struct of_device_id sdhci_tegra_dt_match[] = {
  181. { .compatible = "nvidia,tegra124-sdhci", .data = &soc_data_tegra114 },
  182. { .compatible = "nvidia,tegra114-sdhci", .data = &soc_data_tegra114 },
  183. { .compatible = "nvidia,tegra30-sdhci", .data = &soc_data_tegra30 },
  184. { .compatible = "nvidia,tegra20-sdhci", .data = &soc_data_tegra20 },
  185. {}
  186. };
  187. MODULE_DEVICE_TABLE(of, sdhci_tegra_dt_match);
  188. static int sdhci_tegra_parse_dt(struct device *dev)
  189. {
  190. struct device_node *np = dev->of_node;
  191. struct sdhci_host *host = dev_get_drvdata(dev);
  192. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  193. struct sdhci_tegra *tegra_host = pltfm_host->priv;
  194. tegra_host->power_gpio = of_get_named_gpio(np, "power-gpios", 0);
  195. return mmc_of_parse(host->mmc);
  196. }
  197. static int sdhci_tegra_probe(struct platform_device *pdev)
  198. {
  199. const struct of_device_id *match;
  200. const struct sdhci_tegra_soc_data *soc_data;
  201. struct sdhci_host *host;
  202. struct sdhci_pltfm_host *pltfm_host;
  203. struct sdhci_tegra *tegra_host;
  204. struct clk *clk;
  205. int rc;
  206. match = of_match_device(sdhci_tegra_dt_match, &pdev->dev);
  207. if (!match)
  208. return -EINVAL;
  209. soc_data = match->data;
  210. host = sdhci_pltfm_init(pdev, soc_data->pdata, 0);
  211. if (IS_ERR(host))
  212. return PTR_ERR(host);
  213. pltfm_host = sdhci_priv(host);
  214. tegra_host = devm_kzalloc(&pdev->dev, sizeof(*tegra_host), GFP_KERNEL);
  215. if (!tegra_host) {
  216. dev_err(mmc_dev(host->mmc), "failed to allocate tegra_host\n");
  217. rc = -ENOMEM;
  218. goto err_alloc_tegra_host;
  219. }
  220. tegra_host->soc_data = soc_data;
  221. pltfm_host->priv = tegra_host;
  222. rc = sdhci_tegra_parse_dt(&pdev->dev);
  223. if (rc)
  224. goto err_parse_dt;
  225. if (gpio_is_valid(tegra_host->power_gpio)) {
  226. rc = gpio_request(tegra_host->power_gpio, "sdhci_power");
  227. if (rc) {
  228. dev_err(mmc_dev(host->mmc),
  229. "failed to allocate power gpio\n");
  230. goto err_power_req;
  231. }
  232. gpio_direction_output(tegra_host->power_gpio, 1);
  233. }
  234. clk = clk_get(mmc_dev(host->mmc), NULL);
  235. if (IS_ERR(clk)) {
  236. dev_err(mmc_dev(host->mmc), "clk err\n");
  237. rc = PTR_ERR(clk);
  238. goto err_clk_get;
  239. }
  240. clk_prepare_enable(clk);
  241. pltfm_host->clk = clk;
  242. rc = sdhci_add_host(host);
  243. if (rc)
  244. goto err_add_host;
  245. return 0;
  246. err_add_host:
  247. clk_disable_unprepare(pltfm_host->clk);
  248. clk_put(pltfm_host->clk);
  249. err_clk_get:
  250. if (gpio_is_valid(tegra_host->power_gpio))
  251. gpio_free(tegra_host->power_gpio);
  252. err_power_req:
  253. err_parse_dt:
  254. err_alloc_tegra_host:
  255. sdhci_pltfm_free(pdev);
  256. return rc;
  257. }
  258. static int sdhci_tegra_remove(struct platform_device *pdev)
  259. {
  260. struct sdhci_host *host = platform_get_drvdata(pdev);
  261. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  262. struct sdhci_tegra *tegra_host = pltfm_host->priv;
  263. int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
  264. sdhci_remove_host(host, dead);
  265. if (gpio_is_valid(tegra_host->power_gpio))
  266. gpio_free(tegra_host->power_gpio);
  267. clk_disable_unprepare(pltfm_host->clk);
  268. clk_put(pltfm_host->clk);
  269. sdhci_pltfm_free(pdev);
  270. return 0;
  271. }
  272. static struct platform_driver sdhci_tegra_driver = {
  273. .driver = {
  274. .name = "sdhci-tegra",
  275. .owner = THIS_MODULE,
  276. .of_match_table = sdhci_tegra_dt_match,
  277. .pm = SDHCI_PLTFM_PMOPS,
  278. },
  279. .probe = sdhci_tegra_probe,
  280. .remove = sdhci_tegra_remove,
  281. };
  282. module_platform_driver(sdhci_tegra_driver);
  283. MODULE_DESCRIPTION("SDHCI driver for Tegra");
  284. MODULE_AUTHOR("Google, Inc.");
  285. MODULE_LICENSE("GPL v2");