sdhci-of-esdhc.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Freescale eSDHC controller driver.
  3. *
  4. * Copyright (c) 2007, 2010, 2012 Freescale Semiconductor, Inc.
  5. * Copyright (c) 2009 MontaVista Software, Inc.
  6. *
  7. * Authors: Xiaobo Xie <X.Xie@freescale.com>
  8. * Anton Vorontsov <avorontsov@ru.mvista.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or (at
  13. * your option) any later version.
  14. */
  15. #include <linux/io.h>
  16. #include <linux/of.h>
  17. #include <linux/delay.h>
  18. #include <linux/module.h>
  19. #include <linux/mmc/host.h>
  20. #include "sdhci-pltfm.h"
  21. #include "sdhci-esdhc.h"
  22. #define VENDOR_V_22 0x12
  23. static u32 esdhc_readl(struct sdhci_host *host, int reg)
  24. {
  25. u32 ret;
  26. ret = in_be32(host->ioaddr + reg);
  27. /*
  28. * The bit of ADMA flag in eSDHC is not compatible with standard
  29. * SDHC register, so set fake flag SDHCI_CAN_DO_ADMA2 when ADMA is
  30. * supported by eSDHC.
  31. * And for many FSL eSDHC controller, the reset value of field
  32. * SDHCI_CAN_DO_ADMA1 is one, but some of them can't support ADMA,
  33. * only these vendor version is greater than 2.2/0x12 support ADMA.
  34. * For FSL eSDHC, must aligned 4-byte, so use 0xFC to read the
  35. * the verdor version number, oxFE is SDHCI_HOST_VERSION.
  36. */
  37. if ((reg == SDHCI_CAPABILITIES) && (ret & SDHCI_CAN_DO_ADMA1)) {
  38. u32 tmp = in_be32(host->ioaddr + SDHCI_SLOT_INT_STATUS);
  39. tmp = (tmp & SDHCI_VENDOR_VER_MASK) >> SDHCI_VENDOR_VER_SHIFT;
  40. if (tmp > VENDOR_V_22)
  41. ret |= SDHCI_CAN_DO_ADMA2;
  42. }
  43. return ret;
  44. }
  45. static u16 esdhc_readw(struct sdhci_host *host, int reg)
  46. {
  47. u16 ret;
  48. int base = reg & ~0x3;
  49. int shift = (reg & 0x2) * 8;
  50. if (unlikely(reg == SDHCI_HOST_VERSION))
  51. ret = in_be32(host->ioaddr + base) & 0xffff;
  52. else
  53. ret = (in_be32(host->ioaddr + base) >> shift) & 0xffff;
  54. return ret;
  55. }
  56. static u8 esdhc_readb(struct sdhci_host *host, int reg)
  57. {
  58. int base = reg & ~0x3;
  59. int shift = (reg & 0x3) * 8;
  60. u8 ret = (in_be32(host->ioaddr + base) >> shift) & 0xff;
  61. /*
  62. * "DMA select" locates at offset 0x28 in SD specification, but on
  63. * P5020 or P3041, it locates at 0x29.
  64. */
  65. if (reg == SDHCI_HOST_CONTROL) {
  66. u32 dma_bits;
  67. dma_bits = in_be32(host->ioaddr + reg);
  68. /* DMA select is 22,23 bits in Protocol Control Register */
  69. dma_bits = (dma_bits >> 5) & SDHCI_CTRL_DMA_MASK;
  70. /* fixup the result */
  71. ret &= ~SDHCI_CTRL_DMA_MASK;
  72. ret |= dma_bits;
  73. }
  74. return ret;
  75. }
  76. static void esdhc_writew(struct sdhci_host *host, u16 val, int reg)
  77. {
  78. if (reg == SDHCI_BLOCK_SIZE) {
  79. /*
  80. * Two last DMA bits are reserved, and first one is used for
  81. * non-standard blksz of 4096 bytes that we don't support
  82. * yet. So clear the DMA boundary bits.
  83. */
  84. val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
  85. }
  86. sdhci_be32bs_writew(host, val, reg);
  87. }
  88. static void esdhc_writeb(struct sdhci_host *host, u8 val, int reg)
  89. {
  90. /*
  91. * "DMA select" location is offset 0x28 in SD specification, but on
  92. * P5020 or P3041, it's located at 0x29.
  93. */
  94. if (reg == SDHCI_HOST_CONTROL) {
  95. u32 dma_bits;
  96. /* DMA select is 22,23 bits in Protocol Control Register */
  97. dma_bits = (val & SDHCI_CTRL_DMA_MASK) << 5;
  98. clrsetbits_be32(host->ioaddr + reg , SDHCI_CTRL_DMA_MASK << 5,
  99. dma_bits);
  100. val &= ~SDHCI_CTRL_DMA_MASK;
  101. val |= in_be32(host->ioaddr + reg) & SDHCI_CTRL_DMA_MASK;
  102. }
  103. /* Prevent SDHCI core from writing reserved bits (e.g. HISPD). */
  104. if (reg == SDHCI_HOST_CONTROL)
  105. val &= ~ESDHC_HOST_CONTROL_RES;
  106. sdhci_be32bs_writeb(host, val, reg);
  107. }
  108. static int esdhc_of_enable_dma(struct sdhci_host *host)
  109. {
  110. setbits32(host->ioaddr + ESDHC_DMA_SYSCTL, ESDHC_DMA_SNOOP);
  111. return 0;
  112. }
  113. static unsigned int esdhc_of_get_max_clock(struct sdhci_host *host)
  114. {
  115. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  116. return pltfm_host->clock;
  117. }
  118. static unsigned int esdhc_of_get_min_clock(struct sdhci_host *host)
  119. {
  120. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  121. return pltfm_host->clock / 256 / 16;
  122. }
  123. static void esdhc_of_set_clock(struct sdhci_host *host, unsigned int clock)
  124. {
  125. /* Workaround to reduce the clock frequency for p1010 esdhc */
  126. if (of_find_compatible_node(NULL, NULL, "fsl,p1010-esdhc")) {
  127. if (clock > 20000000)
  128. clock -= 5000000;
  129. if (clock > 40000000)
  130. clock -= 5000000;
  131. }
  132. /* Set the clock */
  133. esdhc_set_clock(host, clock);
  134. }
  135. #ifdef CONFIG_PM
  136. static u32 esdhc_proctl;
  137. static void esdhc_of_suspend(struct sdhci_host *host)
  138. {
  139. esdhc_proctl = sdhci_be32bs_readl(host, SDHCI_HOST_CONTROL);
  140. }
  141. static void esdhc_of_resume(struct sdhci_host *host)
  142. {
  143. esdhc_of_enable_dma(host);
  144. sdhci_be32bs_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL);
  145. }
  146. #endif
  147. static void esdhc_of_platform_init(struct sdhci_host *host)
  148. {
  149. u32 vvn;
  150. vvn = in_be32(host->ioaddr + SDHCI_SLOT_INT_STATUS);
  151. vvn = (vvn & SDHCI_VENDOR_VER_MASK) >> SDHCI_VENDOR_VER_SHIFT;
  152. if (vvn == VENDOR_V_22)
  153. host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23;
  154. }
  155. static struct sdhci_ops sdhci_esdhc_ops = {
  156. .read_l = esdhc_readl,
  157. .read_w = esdhc_readw,
  158. .read_b = esdhc_readb,
  159. .write_l = sdhci_be32bs_writel,
  160. .write_w = esdhc_writew,
  161. .write_b = esdhc_writeb,
  162. .set_clock = esdhc_of_set_clock,
  163. .enable_dma = esdhc_of_enable_dma,
  164. .get_max_clock = esdhc_of_get_max_clock,
  165. .get_min_clock = esdhc_of_get_min_clock,
  166. .platform_init = esdhc_of_platform_init,
  167. #ifdef CONFIG_PM
  168. .platform_suspend = esdhc_of_suspend,
  169. .platform_resume = esdhc_of_resume,
  170. #endif
  171. };
  172. static struct sdhci_pltfm_data sdhci_esdhc_pdata = {
  173. /*
  174. * card detection could be handled via GPIO
  175. * eSDHC cannot support End Attribute in NOP ADMA descriptor
  176. */
  177. .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_BROKEN_CARD_DETECTION
  178. | SDHCI_QUIRK_NO_CARD_NO_RESET
  179. | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  180. .ops = &sdhci_esdhc_ops,
  181. };
  182. static int __devinit sdhci_esdhc_probe(struct platform_device *pdev)
  183. {
  184. return sdhci_pltfm_register(pdev, &sdhci_esdhc_pdata);
  185. }
  186. static int __devexit sdhci_esdhc_remove(struct platform_device *pdev)
  187. {
  188. return sdhci_pltfm_unregister(pdev);
  189. }
  190. static const struct of_device_id sdhci_esdhc_of_match[] = {
  191. { .compatible = "fsl,mpc8379-esdhc" },
  192. { .compatible = "fsl,mpc8536-esdhc" },
  193. { .compatible = "fsl,esdhc" },
  194. { }
  195. };
  196. MODULE_DEVICE_TABLE(of, sdhci_esdhc_of_match);
  197. static struct platform_driver sdhci_esdhc_driver = {
  198. .driver = {
  199. .name = "sdhci-esdhc",
  200. .owner = THIS_MODULE,
  201. .of_match_table = sdhci_esdhc_of_match,
  202. .pm = SDHCI_PLTFM_PMOPS,
  203. },
  204. .probe = sdhci_esdhc_probe,
  205. .remove = __devexit_p(sdhci_esdhc_remove),
  206. };
  207. module_platform_driver(sdhci_esdhc_driver);
  208. MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC");
  209. MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, "
  210. "Anton Vorontsov <avorontsov@ru.mvista.com>");
  211. MODULE_LICENSE("GPL v2");