sdhci-tegra.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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/gpio.h>
  20. #include <linux/mmc/card.h>
  21. #include <linux/mmc/host.h>
  22. #include <mach/gpio.h>
  23. #include <mach/sdhci.h>
  24. #include "sdhci-pltfm.h"
  25. static u32 tegra_sdhci_readl(struct sdhci_host *host, int reg)
  26. {
  27. u32 val;
  28. if (unlikely(reg == SDHCI_PRESENT_STATE)) {
  29. /* Use wp_gpio here instead? */
  30. val = readl(host->ioaddr + reg);
  31. return val | SDHCI_WRITE_PROTECT;
  32. }
  33. return readl(host->ioaddr + reg);
  34. }
  35. static u16 tegra_sdhci_readw(struct sdhci_host *host, int reg)
  36. {
  37. if (unlikely(reg == SDHCI_HOST_VERSION)) {
  38. /* Erratum: Version register is invalid in HW. */
  39. return SDHCI_SPEC_200;
  40. }
  41. return readw(host->ioaddr + reg);
  42. }
  43. static void tegra_sdhci_writel(struct sdhci_host *host, u32 val, int reg)
  44. {
  45. /* Seems like we're getting spurious timeout and crc errors, so
  46. * disable signalling of them. In case of real errors software
  47. * timers should take care of eventually detecting them.
  48. */
  49. if (unlikely(reg == SDHCI_SIGNAL_ENABLE))
  50. val &= ~(SDHCI_INT_TIMEOUT|SDHCI_INT_CRC);
  51. writel(val, host->ioaddr + reg);
  52. if (unlikely(reg == SDHCI_INT_ENABLE)) {
  53. /* Erratum: Must enable block gap interrupt detection */
  54. u8 gap_ctrl = readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL);
  55. if (val & SDHCI_INT_CARD_INT)
  56. gap_ctrl |= 0x8;
  57. else
  58. gap_ctrl &= ~0x8;
  59. writeb(gap_ctrl, host->ioaddr + SDHCI_BLOCK_GAP_CONTROL);
  60. }
  61. }
  62. static unsigned int tegra_sdhci_get_ro(struct sdhci_host *sdhci)
  63. {
  64. struct platform_device *pdev = to_platform_device(mmc_dev(sdhci->mmc));
  65. struct tegra_sdhci_platform_data *plat;
  66. plat = pdev->dev.platform_data;
  67. if (!gpio_is_valid(plat->wp_gpio))
  68. return -1;
  69. return gpio_get_value(plat->wp_gpio);
  70. }
  71. static irqreturn_t carddetect_irq(int irq, void *data)
  72. {
  73. struct sdhci_host *sdhost = (struct sdhci_host *)data;
  74. tasklet_schedule(&sdhost->card_tasklet);
  75. return IRQ_HANDLED;
  76. };
  77. static int tegra_sdhci_8bit(struct sdhci_host *host, int bus_width)
  78. {
  79. struct platform_device *pdev = to_platform_device(mmc_dev(host->mmc));
  80. struct tegra_sdhci_platform_data *plat;
  81. u32 ctrl;
  82. plat = pdev->dev.platform_data;
  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 int __devinit sdhci_tegra_probe(struct platform_device *pdev)
  112. {
  113. struct sdhci_pltfm_host *pltfm_host;
  114. struct tegra_sdhci_platform_data *plat;
  115. struct sdhci_host *host;
  116. struct clk *clk;
  117. int rc;
  118. host = sdhci_pltfm_init(pdev, &sdhci_tegra_pdata);
  119. if (IS_ERR(host))
  120. return PTR_ERR(host);
  121. pltfm_host = sdhci_priv(host);
  122. plat = pdev->dev.platform_data;
  123. if (plat == NULL) {
  124. dev_err(mmc_dev(host->mmc), "missing platform data\n");
  125. rc = -ENXIO;
  126. goto err_no_plat;
  127. }
  128. if (gpio_is_valid(plat->power_gpio)) {
  129. rc = gpio_request(plat->power_gpio, "sdhci_power");
  130. if (rc) {
  131. dev_err(mmc_dev(host->mmc),
  132. "failed to allocate power gpio\n");
  133. goto err_power_req;
  134. }
  135. tegra_gpio_enable(plat->power_gpio);
  136. gpio_direction_output(plat->power_gpio, 1);
  137. }
  138. if (gpio_is_valid(plat->cd_gpio)) {
  139. rc = gpio_request(plat->cd_gpio, "sdhci_cd");
  140. if (rc) {
  141. dev_err(mmc_dev(host->mmc),
  142. "failed to allocate cd gpio\n");
  143. goto err_cd_req;
  144. }
  145. tegra_gpio_enable(plat->cd_gpio);
  146. gpio_direction_input(plat->cd_gpio);
  147. rc = request_irq(gpio_to_irq(plat->cd_gpio), carddetect_irq,
  148. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  149. mmc_hostname(host->mmc), host);
  150. if (rc) {
  151. dev_err(mmc_dev(host->mmc), "request irq error\n");
  152. goto err_cd_irq_req;
  153. }
  154. }
  155. if (gpio_is_valid(plat->wp_gpio)) {
  156. rc = gpio_request(plat->wp_gpio, "sdhci_wp");
  157. if (rc) {
  158. dev_err(mmc_dev(host->mmc),
  159. "failed to allocate wp gpio\n");
  160. goto err_wp_req;
  161. }
  162. tegra_gpio_enable(plat->wp_gpio);
  163. gpio_direction_input(plat->wp_gpio);
  164. }
  165. clk = clk_get(mmc_dev(host->mmc), NULL);
  166. if (IS_ERR(clk)) {
  167. dev_err(mmc_dev(host->mmc), "clk err\n");
  168. rc = PTR_ERR(clk);
  169. goto err_clk_get;
  170. }
  171. clk_enable(clk);
  172. pltfm_host->clk = clk;
  173. host->mmc->pm_caps = plat->pm_flags;
  174. if (plat->is_8bit)
  175. host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  176. rc = sdhci_add_host(host);
  177. if (rc)
  178. goto err_add_host;
  179. return 0;
  180. err_add_host:
  181. clk_disable(pltfm_host->clk);
  182. clk_put(pltfm_host->clk);
  183. err_clk_get:
  184. if (gpio_is_valid(plat->wp_gpio)) {
  185. tegra_gpio_disable(plat->wp_gpio);
  186. gpio_free(plat->wp_gpio);
  187. }
  188. err_wp_req:
  189. if (gpio_is_valid(plat->cd_gpio))
  190. free_irq(gpio_to_irq(plat->cd_gpio), host);
  191. err_cd_irq_req:
  192. if (gpio_is_valid(plat->cd_gpio)) {
  193. tegra_gpio_disable(plat->cd_gpio);
  194. gpio_free(plat->cd_gpio);
  195. }
  196. err_cd_req:
  197. if (gpio_is_valid(plat->power_gpio)) {
  198. tegra_gpio_disable(plat->power_gpio);
  199. gpio_free(plat->power_gpio);
  200. }
  201. err_power_req:
  202. err_no_plat:
  203. sdhci_pltfm_free(pdev);
  204. return rc;
  205. }
  206. static int __devexit sdhci_tegra_remove(struct platform_device *pdev)
  207. {
  208. struct sdhci_host *host = platform_get_drvdata(pdev);
  209. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  210. struct tegra_sdhci_platform_data *plat;
  211. int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
  212. sdhci_remove_host(host, dead);
  213. plat = pdev->dev.platform_data;
  214. if (gpio_is_valid(plat->wp_gpio)) {
  215. tegra_gpio_disable(plat->wp_gpio);
  216. gpio_free(plat->wp_gpio);
  217. }
  218. if (gpio_is_valid(plat->cd_gpio)) {
  219. free_irq(gpio_to_irq(plat->cd_gpio), host);
  220. tegra_gpio_disable(plat->cd_gpio);
  221. gpio_free(plat->cd_gpio);
  222. }
  223. if (gpio_is_valid(plat->power_gpio)) {
  224. tegra_gpio_disable(plat->power_gpio);
  225. gpio_free(plat->power_gpio);
  226. }
  227. clk_disable(pltfm_host->clk);
  228. clk_put(pltfm_host->clk);
  229. sdhci_pltfm_free(pdev);
  230. return 0;
  231. }
  232. static struct platform_driver sdhci_tegra_driver = {
  233. .driver = {
  234. .name = "sdhci-tegra",
  235. .owner = THIS_MODULE,
  236. },
  237. .probe = sdhci_tegra_probe,
  238. .remove = __devexit_p(sdhci_tegra_remove),
  239. #ifdef CONFIG_PM
  240. .suspend = sdhci_pltfm_suspend,
  241. .resume = sdhci_pltfm_resume,
  242. #endif
  243. };
  244. static int __init sdhci_tegra_init(void)
  245. {
  246. return platform_driver_register(&sdhci_tegra_driver);
  247. }
  248. module_init(sdhci_tegra_init);
  249. static void __exit sdhci_tegra_exit(void)
  250. {
  251. platform_driver_unregister(&sdhci_tegra_driver);
  252. }
  253. module_exit(sdhci_tegra_exit);
  254. MODULE_DESCRIPTION("SDHCI driver for Tegra");
  255. MODULE_AUTHOR(" Google, Inc.");
  256. MODULE_LICENSE("GPL v2");