sdhci-tegra.c 8.0 KB

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