sdhci-tegra.c 8.8 KB

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