sdhci-tegra.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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/init.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/clk.h>
  18. #include <linux/io.h>
  19. #include <linux/of.h>
  20. #include <linux/of_gpio.h>
  21. #include <linux/gpio.h>
  22. #include <linux/mmc/card.h>
  23. #include <linux/mmc/host.h>
  24. #include <mach/gpio.h>
  25. #include <mach/sdhci.h>
  26. #include "sdhci-pltfm.h"
  27. static u32 tegra_sdhci_readl(struct sdhci_host *host, int reg)
  28. {
  29. u32 val;
  30. if (unlikely(reg == SDHCI_PRESENT_STATE)) {
  31. /* Use wp_gpio here instead? */
  32. val = readl(host->ioaddr + reg);
  33. return val | SDHCI_WRITE_PROTECT;
  34. }
  35. return readl(host->ioaddr + reg);
  36. }
  37. static u16 tegra_sdhci_readw(struct sdhci_host *host, int reg)
  38. {
  39. if (unlikely(reg == SDHCI_HOST_VERSION)) {
  40. /* Erratum: Version register is invalid in HW. */
  41. return SDHCI_SPEC_200;
  42. }
  43. return readw(host->ioaddr + reg);
  44. }
  45. static void tegra_sdhci_writel(struct sdhci_host *host, u32 val, int reg)
  46. {
  47. /* Seems like we're getting spurious timeout and crc errors, so
  48. * disable signalling of them. In case of real errors software
  49. * timers should take care of eventually detecting them.
  50. */
  51. if (unlikely(reg == SDHCI_SIGNAL_ENABLE))
  52. val &= ~(SDHCI_INT_TIMEOUT|SDHCI_INT_CRC);
  53. writel(val, host->ioaddr + reg);
  54. if (unlikely(reg == SDHCI_INT_ENABLE)) {
  55. /* Erratum: Must enable block gap interrupt detection */
  56. u8 gap_ctrl = readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL);
  57. if (val & SDHCI_INT_CARD_INT)
  58. gap_ctrl |= 0x8;
  59. else
  60. gap_ctrl &= ~0x8;
  61. writeb(gap_ctrl, host->ioaddr + SDHCI_BLOCK_GAP_CONTROL);
  62. }
  63. }
  64. static unsigned int tegra_sdhci_get_ro(struct sdhci_host *sdhci)
  65. {
  66. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(sdhci);
  67. struct tegra_sdhci_platform_data *plat = pltfm_host->priv;
  68. if (!gpio_is_valid(plat->wp_gpio))
  69. return -1;
  70. return gpio_get_value(plat->wp_gpio);
  71. }
  72. static irqreturn_t carddetect_irq(int irq, void *data)
  73. {
  74. struct sdhci_host *sdhost = (struct sdhci_host *)data;
  75. tasklet_schedule(&sdhost->card_tasklet);
  76. return IRQ_HANDLED;
  77. };
  78. static int tegra_sdhci_8bit(struct sdhci_host *host, int bus_width)
  79. {
  80. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  81. struct tegra_sdhci_platform_data *plat = pltfm_host->priv;
  82. u32 ctrl;
  83. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  84. if (plat->is_8bit && bus_width == MMC_BUS_WIDTH_8) {
  85. ctrl &= ~SDHCI_CTRL_4BITBUS;
  86. ctrl |= SDHCI_CTRL_8BITBUS;
  87. } else {
  88. ctrl &= ~SDHCI_CTRL_8BITBUS;
  89. if (bus_width == MMC_BUS_WIDTH_4)
  90. ctrl |= SDHCI_CTRL_4BITBUS;
  91. else
  92. ctrl &= ~SDHCI_CTRL_4BITBUS;
  93. }
  94. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  95. return 0;
  96. }
  97. static struct sdhci_ops tegra_sdhci_ops = {
  98. .get_ro = tegra_sdhci_get_ro,
  99. .read_l = tegra_sdhci_readl,
  100. .read_w = tegra_sdhci_readw,
  101. .write_l = tegra_sdhci_writel,
  102. .platform_8bit_width = tegra_sdhci_8bit,
  103. };
  104. static struct sdhci_pltfm_data sdhci_tegra_pdata = {
  105. .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
  106. SDHCI_QUIRK_SINGLE_POWER_WRITE |
  107. SDHCI_QUIRK_NO_HISPD_BIT |
  108. SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC,
  109. .ops = &tegra_sdhci_ops,
  110. };
  111. static const struct of_device_id sdhci_tegra_dt_match[] __devinitdata = {
  112. { .compatible = "nvidia,tegra20-sdhci", },
  113. {}
  114. };
  115. MODULE_DEVICE_TABLE(of, sdhci_dt_ids);
  116. static struct tegra_sdhci_platform_data * __devinit sdhci_tegra_dt_parse_pdata(
  117. struct platform_device *pdev)
  118. {
  119. struct tegra_sdhci_platform_data *plat;
  120. struct device_node *np = pdev->dev.of_node;
  121. if (!np)
  122. return NULL;
  123. plat = devm_kzalloc(&pdev->dev, sizeof(*plat), GFP_KERNEL);
  124. if (!plat) {
  125. dev_err(&pdev->dev, "Can't allocate platform data\n");
  126. return NULL;
  127. }
  128. plat->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
  129. plat->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
  130. plat->power_gpio = of_get_named_gpio(np, "power-gpios", 0);
  131. if (of_find_property(np, "support-8bit", NULL))
  132. plat->is_8bit = 1;
  133. return plat;
  134. }
  135. static int __devinit sdhci_tegra_probe(struct platform_device *pdev)
  136. {
  137. struct sdhci_pltfm_host *pltfm_host;
  138. struct tegra_sdhci_platform_data *plat;
  139. struct sdhci_host *host;
  140. struct clk *clk;
  141. int rc;
  142. host = sdhci_pltfm_init(pdev, &sdhci_tegra_pdata);
  143. if (IS_ERR(host))
  144. return PTR_ERR(host);
  145. pltfm_host = sdhci_priv(host);
  146. plat = pdev->dev.platform_data;
  147. if (plat == NULL)
  148. plat = sdhci_tegra_dt_parse_pdata(pdev);
  149. if (plat == NULL) {
  150. dev_err(mmc_dev(host->mmc), "missing platform data\n");
  151. rc = -ENXIO;
  152. goto err_no_plat;
  153. }
  154. pltfm_host->priv = plat;
  155. if (gpio_is_valid(plat->power_gpio)) {
  156. rc = gpio_request(plat->power_gpio, "sdhci_power");
  157. if (rc) {
  158. dev_err(mmc_dev(host->mmc),
  159. "failed to allocate power gpio\n");
  160. goto err_power_req;
  161. }
  162. tegra_gpio_enable(plat->power_gpio);
  163. gpio_direction_output(plat->power_gpio, 1);
  164. }
  165. if (gpio_is_valid(plat->cd_gpio)) {
  166. rc = gpio_request(plat->cd_gpio, "sdhci_cd");
  167. if (rc) {
  168. dev_err(mmc_dev(host->mmc),
  169. "failed to allocate cd gpio\n");
  170. goto err_cd_req;
  171. }
  172. tegra_gpio_enable(plat->cd_gpio);
  173. gpio_direction_input(plat->cd_gpio);
  174. rc = request_irq(gpio_to_irq(plat->cd_gpio), carddetect_irq,
  175. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  176. mmc_hostname(host->mmc), host);
  177. if (rc) {
  178. dev_err(mmc_dev(host->mmc), "request irq error\n");
  179. goto err_cd_irq_req;
  180. }
  181. }
  182. if (gpio_is_valid(plat->wp_gpio)) {
  183. rc = gpio_request(plat->wp_gpio, "sdhci_wp");
  184. if (rc) {
  185. dev_err(mmc_dev(host->mmc),
  186. "failed to allocate wp gpio\n");
  187. goto err_wp_req;
  188. }
  189. tegra_gpio_enable(plat->wp_gpio);
  190. gpio_direction_input(plat->wp_gpio);
  191. }
  192. clk = clk_get(mmc_dev(host->mmc), NULL);
  193. if (IS_ERR(clk)) {
  194. dev_err(mmc_dev(host->mmc), "clk err\n");
  195. rc = PTR_ERR(clk);
  196. goto err_clk_get;
  197. }
  198. clk_enable(clk);
  199. pltfm_host->clk = clk;
  200. host->mmc->pm_caps = plat->pm_flags;
  201. if (plat->is_8bit)
  202. host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  203. rc = sdhci_add_host(host);
  204. if (rc)
  205. goto err_add_host;
  206. return 0;
  207. err_add_host:
  208. clk_disable(pltfm_host->clk);
  209. clk_put(pltfm_host->clk);
  210. err_clk_get:
  211. if (gpio_is_valid(plat->wp_gpio)) {
  212. tegra_gpio_disable(plat->wp_gpio);
  213. gpio_free(plat->wp_gpio);
  214. }
  215. err_wp_req:
  216. if (gpio_is_valid(plat->cd_gpio))
  217. free_irq(gpio_to_irq(plat->cd_gpio), host);
  218. err_cd_irq_req:
  219. if (gpio_is_valid(plat->cd_gpio)) {
  220. tegra_gpio_disable(plat->cd_gpio);
  221. gpio_free(plat->cd_gpio);
  222. }
  223. err_cd_req:
  224. if (gpio_is_valid(plat->power_gpio)) {
  225. tegra_gpio_disable(plat->power_gpio);
  226. gpio_free(plat->power_gpio);
  227. }
  228. err_power_req:
  229. err_no_plat:
  230. sdhci_pltfm_free(pdev);
  231. return rc;
  232. }
  233. static int __devexit sdhci_tegra_remove(struct platform_device *pdev)
  234. {
  235. struct sdhci_host *host = platform_get_drvdata(pdev);
  236. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  237. struct tegra_sdhci_platform_data *plat = pltfm_host->priv;
  238. int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
  239. sdhci_remove_host(host, dead);
  240. if (gpio_is_valid(plat->wp_gpio)) {
  241. tegra_gpio_disable(plat->wp_gpio);
  242. gpio_free(plat->wp_gpio);
  243. }
  244. if (gpio_is_valid(plat->cd_gpio)) {
  245. free_irq(gpio_to_irq(plat->cd_gpio), host);
  246. tegra_gpio_disable(plat->cd_gpio);
  247. gpio_free(plat->cd_gpio);
  248. }
  249. if (gpio_is_valid(plat->power_gpio)) {
  250. tegra_gpio_disable(plat->power_gpio);
  251. gpio_free(plat->power_gpio);
  252. }
  253. clk_disable(pltfm_host->clk);
  254. clk_put(pltfm_host->clk);
  255. sdhci_pltfm_free(pdev);
  256. return 0;
  257. }
  258. static struct platform_driver sdhci_tegra_driver = {
  259. .driver = {
  260. .name = "sdhci-tegra",
  261. .owner = THIS_MODULE,
  262. .of_match_table = sdhci_tegra_dt_match,
  263. },
  264. .probe = sdhci_tegra_probe,
  265. .remove = __devexit_p(sdhci_tegra_remove),
  266. #ifdef CONFIG_PM
  267. .suspend = sdhci_pltfm_suspend,
  268. .resume = sdhci_pltfm_resume,
  269. #endif
  270. };
  271. static int __init sdhci_tegra_init(void)
  272. {
  273. return platform_driver_register(&sdhci_tegra_driver);
  274. }
  275. module_init(sdhci_tegra_init);
  276. static void __exit sdhci_tegra_exit(void)
  277. {
  278. platform_driver_unregister(&sdhci_tegra_driver);
  279. }
  280. module_exit(sdhci_tegra_exit);
  281. MODULE_DESCRIPTION("SDHCI driver for Tegra");
  282. MODULE_AUTHOR(" Google, Inc.");
  283. MODULE_LICENSE("GPL v2");