sdhci-tegra.c 9.4 KB

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