sdhci-esdhc-imx.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Freescale eSDHC i.MX controller driver for the platform bus.
  3. *
  4. * derived from the OF-version.
  5. *
  6. * Copyright (c) 2010 Pengutronix e.K.
  7. * Author: Wolfram Sang <w.sang@pengutronix.de>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License.
  12. */
  13. #include <linux/io.h>
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/clk.h>
  17. #include <linux/gpio.h>
  18. #include <linux/mmc/host.h>
  19. #include <linux/mmc/sdhci-pltfm.h>
  20. #include <mach/hardware.h>
  21. #include <mach/esdhc.h>
  22. #include "sdhci.h"
  23. #include "sdhci-pltfm.h"
  24. #include "sdhci-esdhc.h"
  25. static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
  26. {
  27. void __iomem *base = host->ioaddr + (reg & ~0x3);
  28. u32 shift = (reg & 0x3) * 8;
  29. writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
  30. }
  31. static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
  32. {
  33. /* fake CARD_PRESENT flag on mx25/35 */
  34. u32 val = readl(host->ioaddr + reg);
  35. if (unlikely(reg == SDHCI_PRESENT_STATE)) {
  36. struct esdhc_platform_data *boarddata =
  37. host->mmc->parent->platform_data;
  38. if (boarddata && gpio_is_valid(boarddata->cd_gpio)
  39. && gpio_get_value(boarddata->cd_gpio))
  40. /* no card, if a valid gpio says so... */
  41. val &= SDHCI_CARD_PRESENT;
  42. else
  43. /* ... in all other cases assume card is present */
  44. val |= SDHCI_CARD_PRESENT;
  45. }
  46. return val;
  47. }
  48. static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
  49. {
  50. if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE))
  51. /*
  52. * these interrupts won't work with a custom card_detect gpio
  53. * (only applied to mx25/35)
  54. */
  55. val &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
  56. writel(val, host->ioaddr + reg);
  57. }
  58. static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
  59. {
  60. if (unlikely(reg == SDHCI_HOST_VERSION))
  61. reg ^= 2;
  62. return readw(host->ioaddr + reg);
  63. }
  64. static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
  65. {
  66. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  67. switch (reg) {
  68. case SDHCI_TRANSFER_MODE:
  69. /*
  70. * Postpone this write, we must do it together with a
  71. * command write that is down below.
  72. */
  73. pltfm_host->scratchpad = val;
  74. return;
  75. case SDHCI_COMMAND:
  76. writel(val << 16 | pltfm_host->scratchpad,
  77. host->ioaddr + SDHCI_TRANSFER_MODE);
  78. return;
  79. case SDHCI_BLOCK_SIZE:
  80. val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
  81. break;
  82. }
  83. esdhc_clrset_le(host, 0xffff, val, reg);
  84. }
  85. static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
  86. {
  87. u32 new_val;
  88. switch (reg) {
  89. case SDHCI_POWER_CONTROL:
  90. /*
  91. * FSL put some DMA bits here
  92. * If your board has a regulator, code should be here
  93. */
  94. return;
  95. case SDHCI_HOST_CONTROL:
  96. /* FSL messed up here, so we can just keep those two */
  97. new_val = val & (SDHCI_CTRL_LED | SDHCI_CTRL_4BITBUS);
  98. /* ensure the endianess */
  99. new_val |= ESDHC_HOST_CONTROL_LE;
  100. /* DMA mode bits are shifted */
  101. new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
  102. esdhc_clrset_le(host, 0xffff, new_val, reg);
  103. return;
  104. }
  105. esdhc_clrset_le(host, 0xff, val, reg);
  106. }
  107. static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
  108. {
  109. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  110. return clk_get_rate(pltfm_host->clk);
  111. }
  112. static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
  113. {
  114. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  115. return clk_get_rate(pltfm_host->clk) / 256 / 16;
  116. }
  117. static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
  118. {
  119. struct esdhc_platform_data *boarddata = host->mmc->parent->platform_data;
  120. if (boarddata && gpio_is_valid(boarddata->wp_gpio))
  121. return gpio_get_value(boarddata->wp_gpio);
  122. else
  123. return -ENOSYS;
  124. }
  125. static struct sdhci_ops sdhci_esdhc_ops = {
  126. .read_w = esdhc_readw_le,
  127. .write_w = esdhc_writew_le,
  128. .write_b = esdhc_writeb_le,
  129. .set_clock = esdhc_set_clock,
  130. .get_max_clock = esdhc_pltfm_get_max_clock,
  131. .get_min_clock = esdhc_pltfm_get_min_clock,
  132. };
  133. static irqreturn_t cd_irq(int irq, void *data)
  134. {
  135. struct sdhci_host *sdhost = (struct sdhci_host *)data;
  136. tasklet_schedule(&sdhost->card_tasklet);
  137. return IRQ_HANDLED;
  138. };
  139. static int esdhc_pltfm_init(struct sdhci_host *host, struct sdhci_pltfm_data *pdata)
  140. {
  141. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  142. struct esdhc_platform_data *boarddata = host->mmc->parent->platform_data;
  143. struct clk *clk;
  144. int err;
  145. clk = clk_get(mmc_dev(host->mmc), NULL);
  146. if (IS_ERR(clk)) {
  147. dev_err(mmc_dev(host->mmc), "clk err\n");
  148. return PTR_ERR(clk);
  149. }
  150. clk_enable(clk);
  151. pltfm_host->clk = clk;
  152. if (cpu_is_mx35() || cpu_is_mx51())
  153. host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
  154. if (cpu_is_mx25() || cpu_is_mx35()) {
  155. /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
  156. host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK;
  157. /* write_protect can't be routed to controller, use gpio */
  158. sdhci_esdhc_ops.get_ro = esdhc_pltfm_get_ro;
  159. }
  160. if (boarddata) {
  161. err = gpio_request_one(boarddata->wp_gpio, GPIOF_IN, "ESDHC_WP");
  162. if (err) {
  163. dev_warn(mmc_dev(host->mmc),
  164. "no write-protect pin available!\n");
  165. boarddata->wp_gpio = err;
  166. }
  167. err = gpio_request_one(boarddata->cd_gpio, GPIOF_IN, "ESDHC_CD");
  168. if (err) {
  169. dev_warn(mmc_dev(host->mmc),
  170. "no card-detect pin available!\n");
  171. goto no_card_detect_pin;
  172. }
  173. /* i.MX5x has issues to be researched */
  174. if (!cpu_is_mx25() && !cpu_is_mx35())
  175. goto not_supported;
  176. err = request_irq(gpio_to_irq(boarddata->cd_gpio), cd_irq,
  177. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  178. mmc_hostname(host->mmc), host);
  179. if (err) {
  180. dev_warn(mmc_dev(host->mmc), "request irq error\n");
  181. goto no_card_detect_irq;
  182. }
  183. sdhci_esdhc_ops.write_l = esdhc_writel_le;
  184. sdhci_esdhc_ops.read_l = esdhc_readl_le;
  185. /* Now we have a working card_detect again */
  186. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  187. }
  188. return 0;
  189. no_card_detect_irq:
  190. gpio_free(boarddata->cd_gpio);
  191. no_card_detect_pin:
  192. boarddata->cd_gpio = err;
  193. not_supported:
  194. return 0;
  195. }
  196. static void esdhc_pltfm_exit(struct sdhci_host *host)
  197. {
  198. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  199. struct esdhc_platform_data *boarddata = host->mmc->parent->platform_data;
  200. if (boarddata && gpio_is_valid(boarddata->wp_gpio))
  201. gpio_free(boarddata->wp_gpio);
  202. if (boarddata && gpio_is_valid(boarddata->cd_gpio)) {
  203. gpio_free(boarddata->cd_gpio);
  204. if (!(host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION))
  205. free_irq(gpio_to_irq(boarddata->cd_gpio), host);
  206. }
  207. clk_disable(pltfm_host->clk);
  208. clk_put(pltfm_host->clk);
  209. }
  210. struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
  211. .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_BROKEN_ADMA
  212. | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
  213. /* ADMA has issues. Might be fixable */
  214. .ops = &sdhci_esdhc_ops,
  215. .init = esdhc_pltfm_init,
  216. .exit = esdhc_pltfm_exit,
  217. };