sdhci-tegra.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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.h"
  25. #include "sdhci-pltfm.h"
  26. static u32 tegra_sdhci_readl(struct sdhci_host *host, int reg)
  27. {
  28. u32 val;
  29. if (unlikely(reg == SDHCI_PRESENT_STATE)) {
  30. /* Use wp_gpio here instead? */
  31. val = readl(host->ioaddr + reg);
  32. return val | SDHCI_WRITE_PROTECT;
  33. }
  34. return readl(host->ioaddr + reg);
  35. }
  36. static u16 tegra_sdhci_readw(struct sdhci_host *host, int reg)
  37. {
  38. if (unlikely(reg == SDHCI_HOST_VERSION)) {
  39. /* Erratum: Version register is invalid in HW. */
  40. return SDHCI_SPEC_200;
  41. }
  42. return readw(host->ioaddr + reg);
  43. }
  44. static void tegra_sdhci_writel(struct sdhci_host *host, u32 val, int reg)
  45. {
  46. /* Seems like we're getting spurious timeout and crc errors, so
  47. * disable signalling of them. In case of real errors software
  48. * timers should take care of eventually detecting them.
  49. */
  50. if (unlikely(reg == SDHCI_SIGNAL_ENABLE))
  51. val &= ~(SDHCI_INT_TIMEOUT|SDHCI_INT_CRC);
  52. writel(val, host->ioaddr + reg);
  53. if (unlikely(reg == SDHCI_INT_ENABLE)) {
  54. /* Erratum: Must enable block gap interrupt detection */
  55. u8 gap_ctrl = readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL);
  56. if (val & SDHCI_INT_CARD_INT)
  57. gap_ctrl |= 0x8;
  58. else
  59. gap_ctrl &= ~0x8;
  60. writeb(gap_ctrl, host->ioaddr + SDHCI_BLOCK_GAP_CONTROL);
  61. }
  62. }
  63. static unsigned int tegra_sdhci_get_ro(struct sdhci_host *sdhci)
  64. {
  65. struct platform_device *pdev = to_platform_device(mmc_dev(sdhci->mmc));
  66. struct tegra_sdhci_platform_data *plat;
  67. plat = pdev->dev.platform_data;
  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 platform_device *pdev = to_platform_device(mmc_dev(host->mmc));
  81. struct tegra_sdhci_platform_data *plat;
  82. u32 ctrl;
  83. plat = pdev->dev.platform_data;
  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 int tegra_sdhci_pltfm_init(struct sdhci_host *host,
  99. struct sdhci_pltfm_data *pdata)
  100. {
  101. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  102. struct platform_device *pdev = to_platform_device(mmc_dev(host->mmc));
  103. struct tegra_sdhci_platform_data *plat;
  104. struct clk *clk;
  105. int rc;
  106. plat = pdev->dev.platform_data;
  107. if (plat == NULL) {
  108. dev_err(mmc_dev(host->mmc), "missing platform data\n");
  109. return -ENXIO;
  110. }
  111. if (gpio_is_valid(plat->power_gpio)) {
  112. rc = gpio_request(plat->power_gpio, "sdhci_power");
  113. if (rc) {
  114. dev_err(mmc_dev(host->mmc),
  115. "failed to allocate power gpio\n");
  116. goto out;
  117. }
  118. tegra_gpio_enable(plat->power_gpio);
  119. gpio_direction_output(plat->power_gpio, 1);
  120. }
  121. if (gpio_is_valid(plat->cd_gpio)) {
  122. rc = gpio_request(plat->cd_gpio, "sdhci_cd");
  123. if (rc) {
  124. dev_err(mmc_dev(host->mmc),
  125. "failed to allocate cd gpio\n");
  126. goto out_power;
  127. }
  128. tegra_gpio_enable(plat->cd_gpio);
  129. gpio_direction_input(plat->cd_gpio);
  130. rc = request_irq(gpio_to_irq(plat->cd_gpio), carddetect_irq,
  131. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  132. mmc_hostname(host->mmc), host);
  133. if (rc) {
  134. dev_err(mmc_dev(host->mmc), "request irq error\n");
  135. goto out_cd;
  136. }
  137. }
  138. if (gpio_is_valid(plat->wp_gpio)) {
  139. rc = gpio_request(plat->wp_gpio, "sdhci_wp");
  140. if (rc) {
  141. dev_err(mmc_dev(host->mmc),
  142. "failed to allocate wp gpio\n");
  143. goto out_irq;
  144. }
  145. tegra_gpio_enable(plat->wp_gpio);
  146. gpio_direction_input(plat->wp_gpio);
  147. }
  148. clk = clk_get(mmc_dev(host->mmc), NULL);
  149. if (IS_ERR(clk)) {
  150. dev_err(mmc_dev(host->mmc), "clk err\n");
  151. rc = PTR_ERR(clk);
  152. goto out_wp;
  153. }
  154. clk_enable(clk);
  155. pltfm_host->clk = clk;
  156. host->mmc->pm_caps = plat->pm_flags;
  157. if (plat->is_8bit)
  158. host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  159. return 0;
  160. out_wp:
  161. if (gpio_is_valid(plat->wp_gpio)) {
  162. tegra_gpio_disable(plat->wp_gpio);
  163. gpio_free(plat->wp_gpio);
  164. }
  165. out_irq:
  166. if (gpio_is_valid(plat->cd_gpio))
  167. free_irq(gpio_to_irq(plat->cd_gpio), host);
  168. out_cd:
  169. if (gpio_is_valid(plat->cd_gpio)) {
  170. tegra_gpio_disable(plat->cd_gpio);
  171. gpio_free(plat->cd_gpio);
  172. }
  173. out_power:
  174. if (gpio_is_valid(plat->power_gpio)) {
  175. tegra_gpio_disable(plat->power_gpio);
  176. gpio_free(plat->power_gpio);
  177. }
  178. out:
  179. return rc;
  180. }
  181. static void tegra_sdhci_pltfm_exit(struct sdhci_host *host)
  182. {
  183. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  184. struct platform_device *pdev = to_platform_device(mmc_dev(host->mmc));
  185. struct tegra_sdhci_platform_data *plat;
  186. plat = pdev->dev.platform_data;
  187. if (gpio_is_valid(plat->wp_gpio)) {
  188. tegra_gpio_disable(plat->wp_gpio);
  189. gpio_free(plat->wp_gpio);
  190. }
  191. if (gpio_is_valid(plat->cd_gpio)) {
  192. free_irq(gpio_to_irq(plat->cd_gpio), host);
  193. tegra_gpio_disable(plat->cd_gpio);
  194. gpio_free(plat->cd_gpio);
  195. }
  196. if (gpio_is_valid(plat->power_gpio)) {
  197. tegra_gpio_disable(plat->power_gpio);
  198. gpio_free(plat->power_gpio);
  199. }
  200. clk_disable(pltfm_host->clk);
  201. clk_put(pltfm_host->clk);
  202. }
  203. static struct sdhci_ops tegra_sdhci_ops = {
  204. .get_ro = tegra_sdhci_get_ro,
  205. .read_l = tegra_sdhci_readl,
  206. .read_w = tegra_sdhci_readw,
  207. .write_l = tegra_sdhci_writel,
  208. .platform_8bit_width = tegra_sdhci_8bit,
  209. };
  210. struct sdhci_pltfm_data sdhci_tegra_pdata = {
  211. .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
  212. SDHCI_QUIRK_SINGLE_POWER_WRITE |
  213. SDHCI_QUIRK_NO_HISPD_BIT |
  214. SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC,
  215. .ops = &tegra_sdhci_ops,
  216. .init = tegra_sdhci_pltfm_init,
  217. .exit = tegra_sdhci_pltfm_exit,
  218. };