sdhci-esdhc-imx.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 u16 esdhc_readw_le(struct sdhci_host *host, int reg)
  32. {
  33. if (unlikely(reg == SDHCI_HOST_VERSION))
  34. reg ^= 2;
  35. return readw(host->ioaddr + reg);
  36. }
  37. static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
  38. {
  39. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  40. switch (reg) {
  41. case SDHCI_TRANSFER_MODE:
  42. /*
  43. * Postpone this write, we must do it together with a
  44. * command write that is down below.
  45. */
  46. pltfm_host->scratchpad = val;
  47. return;
  48. case SDHCI_COMMAND:
  49. writel(val << 16 | pltfm_host->scratchpad,
  50. host->ioaddr + SDHCI_TRANSFER_MODE);
  51. return;
  52. case SDHCI_BLOCK_SIZE:
  53. val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
  54. break;
  55. }
  56. esdhc_clrset_le(host, 0xffff, val, reg);
  57. }
  58. static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
  59. {
  60. u32 new_val;
  61. switch (reg) {
  62. case SDHCI_POWER_CONTROL:
  63. /*
  64. * FSL put some DMA bits here
  65. * If your board has a regulator, code should be here
  66. */
  67. return;
  68. case SDHCI_HOST_CONTROL:
  69. /* FSL messed up here, so we can just keep those two */
  70. new_val = val & (SDHCI_CTRL_LED | SDHCI_CTRL_4BITBUS);
  71. /* ensure the endianess */
  72. new_val |= ESDHC_HOST_CONTROL_LE;
  73. /* DMA mode bits are shifted */
  74. new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
  75. esdhc_clrset_le(host, 0xffff, new_val, reg);
  76. return;
  77. }
  78. esdhc_clrset_le(host, 0xff, val, reg);
  79. }
  80. static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
  81. {
  82. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  83. return clk_get_rate(pltfm_host->clk);
  84. }
  85. static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
  86. {
  87. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  88. return clk_get_rate(pltfm_host->clk) / 256 / 16;
  89. }
  90. static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
  91. {
  92. struct esdhc_platform_data *boarddata = host->mmc->parent->platform_data;
  93. if (boarddata && gpio_is_valid(boarddata->wp_gpio))
  94. return gpio_get_value(boarddata->wp_gpio);
  95. else
  96. return -ENOSYS;
  97. }
  98. static struct sdhci_ops sdhci_esdhc_ops = {
  99. .read_w = esdhc_readw_le,
  100. .write_w = esdhc_writew_le,
  101. .write_b = esdhc_writeb_le,
  102. .set_clock = esdhc_set_clock,
  103. .get_max_clock = esdhc_pltfm_get_max_clock,
  104. .get_min_clock = esdhc_pltfm_get_min_clock,
  105. };
  106. static int esdhc_pltfm_init(struct sdhci_host *host, struct sdhci_pltfm_data *pdata)
  107. {
  108. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  109. struct esdhc_platform_data *boarddata = host->mmc->parent->platform_data;
  110. struct clk *clk;
  111. int err;
  112. clk = clk_get(mmc_dev(host->mmc), NULL);
  113. if (IS_ERR(clk)) {
  114. dev_err(mmc_dev(host->mmc), "clk err\n");
  115. return PTR_ERR(clk);
  116. }
  117. clk_enable(clk);
  118. pltfm_host->clk = clk;
  119. if (cpu_is_mx35() || cpu_is_mx51())
  120. host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
  121. if (cpu_is_mx25() || cpu_is_mx35()) {
  122. /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
  123. host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK;
  124. /* write_protect can't be routed to controller, use gpio */
  125. sdhci_esdhc_ops.get_ro = esdhc_pltfm_get_ro;
  126. }
  127. if (boarddata) {
  128. err = gpio_request_one(boarddata->wp_gpio, GPIOF_IN, "ESDHC_WP");
  129. if (err) {
  130. dev_warn(mmc_dev(host->mmc),
  131. "no write-protect pin available!\n");
  132. boarddata->wp_gpio = err;
  133. }
  134. }
  135. return 0;
  136. }
  137. static void esdhc_pltfm_exit(struct sdhci_host *host)
  138. {
  139. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  140. struct esdhc_platform_data *boarddata = host->mmc->parent->platform_data;
  141. if (boarddata && gpio_is_valid(boarddata->wp_gpio))
  142. gpio_free(boarddata->wp_gpio);
  143. clk_disable(pltfm_host->clk);
  144. clk_put(pltfm_host->clk);
  145. }
  146. struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
  147. .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_BROKEN_ADMA
  148. | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
  149. /* ADMA has issues. Might be fixable */
  150. .ops = &sdhci_esdhc_ops,
  151. .init = esdhc_pltfm_init,
  152. .exit = esdhc_pltfm_exit,
  153. };