sdhci-tegra.c 9.5 KB

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