sdhci-tegra.c 8.3 KB

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