sdhci-of-esdhc.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. #define VENDOR_V_23 0x13
  24. static u32 esdhc_readl(struct sdhci_host *host, int reg)
  25. {
  26. u32 ret;
  27. ret = in_be32(host->ioaddr + reg);
  28. /*
  29. * The bit of ADMA flag in eSDHC is not compatible with standard
  30. * SDHC register, so set fake flag SDHCI_CAN_DO_ADMA2 when ADMA is
  31. * supported by eSDHC.
  32. * And for many FSL eSDHC controller, the reset value of field
  33. * SDHCI_CAN_DO_ADMA1 is one, but some of them can't support ADMA,
  34. * only these vendor version is greater than 2.2/0x12 support ADMA.
  35. * For FSL eSDHC, must aligned 4-byte, so use 0xFC to read the
  36. * the verdor version number, oxFE is SDHCI_HOST_VERSION.
  37. */
  38. if ((reg == SDHCI_CAPABILITIES) && (ret & SDHCI_CAN_DO_ADMA1)) {
  39. u32 tmp = in_be32(host->ioaddr + SDHCI_SLOT_INT_STATUS);
  40. tmp = (tmp & SDHCI_VENDOR_VER_MASK) >> SDHCI_VENDOR_VER_SHIFT;
  41. if (tmp > VENDOR_V_22)
  42. ret |= SDHCI_CAN_DO_ADMA2;
  43. }
  44. return ret;
  45. }
  46. static u16 esdhc_readw(struct sdhci_host *host, int reg)
  47. {
  48. u16 ret;
  49. int base = reg & ~0x3;
  50. int shift = (reg & 0x2) * 8;
  51. if (unlikely(reg == SDHCI_HOST_VERSION))
  52. ret = in_be32(host->ioaddr + base) & 0xffff;
  53. else
  54. ret = (in_be32(host->ioaddr + base) >> shift) & 0xffff;
  55. return ret;
  56. }
  57. static u8 esdhc_readb(struct sdhci_host *host, int reg)
  58. {
  59. int base = reg & ~0x3;
  60. int shift = (reg & 0x3) * 8;
  61. u8 ret = (in_be32(host->ioaddr + base) >> shift) & 0xff;
  62. /*
  63. * "DMA select" locates at offset 0x28 in SD specification, but on
  64. * P5020 or P3041, it locates at 0x29.
  65. */
  66. if (reg == SDHCI_HOST_CONTROL) {
  67. u32 dma_bits;
  68. dma_bits = in_be32(host->ioaddr + reg);
  69. /* DMA select is 22,23 bits in Protocol Control Register */
  70. dma_bits = (dma_bits >> 5) & SDHCI_CTRL_DMA_MASK;
  71. /* fixup the result */
  72. ret &= ~SDHCI_CTRL_DMA_MASK;
  73. ret |= dma_bits;
  74. }
  75. return ret;
  76. }
  77. static void esdhc_writel(struct sdhci_host *host, u32 val, int reg)
  78. {
  79. /*
  80. * Enable IRQSTATEN[BGESEN] is just to set IRQSTAT[BGE]
  81. * when SYSCTL[RSTD]) is set for some special operations.
  82. * No any impact other operation.
  83. */
  84. if (reg == SDHCI_INT_ENABLE)
  85. val |= SDHCI_INT_BLK_GAP;
  86. sdhci_be32bs_writel(host, val, reg);
  87. }
  88. static void esdhc_writew(struct sdhci_host *host, u16 val, int reg)
  89. {
  90. if (reg == SDHCI_BLOCK_SIZE) {
  91. /*
  92. * Two last DMA bits are reserved, and first one is used for
  93. * non-standard blksz of 4096 bytes that we don't support
  94. * yet. So clear the DMA boundary bits.
  95. */
  96. val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
  97. }
  98. sdhci_be32bs_writew(host, val, reg);
  99. }
  100. static void esdhc_writeb(struct sdhci_host *host, u8 val, int reg)
  101. {
  102. /*
  103. * "DMA select" location is offset 0x28 in SD specification, but on
  104. * P5020 or P3041, it's located at 0x29.
  105. */
  106. if (reg == SDHCI_HOST_CONTROL) {
  107. u32 dma_bits;
  108. /* DMA select is 22,23 bits in Protocol Control Register */
  109. dma_bits = (val & SDHCI_CTRL_DMA_MASK) << 5;
  110. clrsetbits_be32(host->ioaddr + reg , SDHCI_CTRL_DMA_MASK << 5,
  111. dma_bits);
  112. val &= ~SDHCI_CTRL_DMA_MASK;
  113. val |= in_be32(host->ioaddr + reg) & SDHCI_CTRL_DMA_MASK;
  114. }
  115. /* Prevent SDHCI core from writing reserved bits (e.g. HISPD). */
  116. if (reg == SDHCI_HOST_CONTROL)
  117. val &= ~ESDHC_HOST_CONTROL_RES;
  118. sdhci_be32bs_writeb(host, val, reg);
  119. }
  120. /*
  121. * For Abort or Suspend after Stop at Block Gap, ignore the ADMA
  122. * error(IRQSTAT[ADMAE]) if both Transfer Complete(IRQSTAT[TC])
  123. * and Block Gap Event(IRQSTAT[BGE]) are also set.
  124. * For Continue, apply soft reset for data(SYSCTL[RSTD]);
  125. * and re-issue the entire read transaction from beginning.
  126. */
  127. static void esdhci_of_adma_workaround(struct sdhci_host *host, u32 intmask)
  128. {
  129. u32 tmp;
  130. bool applicable;
  131. dma_addr_t dmastart;
  132. dma_addr_t dmanow;
  133. tmp = in_be32(host->ioaddr + SDHCI_SLOT_INT_STATUS);
  134. tmp = (tmp & SDHCI_VENDOR_VER_MASK) >> SDHCI_VENDOR_VER_SHIFT;
  135. applicable = (intmask & SDHCI_INT_DATA_END) &&
  136. (intmask & SDHCI_INT_BLK_GAP) &&
  137. (tmp == VENDOR_V_23);
  138. if (!applicable)
  139. return;
  140. host->data->error = 0;
  141. dmastart = sg_dma_address(host->data->sg);
  142. dmanow = dmastart + host->data->bytes_xfered;
  143. /*
  144. * Force update to the next DMA block boundary.
  145. */
  146. dmanow = (dmanow & ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
  147. SDHCI_DEFAULT_BOUNDARY_SIZE;
  148. host->data->bytes_xfered = dmanow - dmastart;
  149. sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
  150. }
  151. static int esdhc_of_enable_dma(struct sdhci_host *host)
  152. {
  153. setbits32(host->ioaddr + ESDHC_DMA_SYSCTL, ESDHC_DMA_SNOOP);
  154. return 0;
  155. }
  156. static unsigned int esdhc_of_get_max_clock(struct sdhci_host *host)
  157. {
  158. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  159. return pltfm_host->clock;
  160. }
  161. static unsigned int esdhc_of_get_min_clock(struct sdhci_host *host)
  162. {
  163. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  164. return pltfm_host->clock / 256 / 16;
  165. }
  166. static void esdhc_of_set_clock(struct sdhci_host *host, unsigned int clock)
  167. {
  168. /* Workaround to reduce the clock frequency for p1010 esdhc */
  169. if (of_find_compatible_node(NULL, NULL, "fsl,p1010-esdhc")) {
  170. if (clock > 20000000)
  171. clock -= 5000000;
  172. if (clock > 40000000)
  173. clock -= 5000000;
  174. }
  175. /* Set the clock */
  176. esdhc_set_clock(host, clock);
  177. }
  178. #ifdef CONFIG_PM
  179. static u32 esdhc_proctl;
  180. static void esdhc_of_suspend(struct sdhci_host *host)
  181. {
  182. esdhc_proctl = sdhci_be32bs_readl(host, SDHCI_HOST_CONTROL);
  183. }
  184. static void esdhc_of_resume(struct sdhci_host *host)
  185. {
  186. esdhc_of_enable_dma(host);
  187. sdhci_be32bs_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL);
  188. }
  189. #endif
  190. static void esdhc_of_platform_init(struct sdhci_host *host)
  191. {
  192. u32 vvn;
  193. vvn = in_be32(host->ioaddr + SDHCI_SLOT_INT_STATUS);
  194. vvn = (vvn & SDHCI_VENDOR_VER_MASK) >> SDHCI_VENDOR_VER_SHIFT;
  195. if (vvn == VENDOR_V_22)
  196. host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23;
  197. if (vvn > VENDOR_V_22)
  198. host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ;
  199. }
  200. static struct sdhci_ops sdhci_esdhc_ops = {
  201. .read_l = esdhc_readl,
  202. .read_w = esdhc_readw,
  203. .read_b = esdhc_readb,
  204. .write_l = esdhc_writel,
  205. .write_w = esdhc_writew,
  206. .write_b = esdhc_writeb,
  207. .set_clock = esdhc_of_set_clock,
  208. .enable_dma = esdhc_of_enable_dma,
  209. .get_max_clock = esdhc_of_get_max_clock,
  210. .get_min_clock = esdhc_of_get_min_clock,
  211. .platform_init = esdhc_of_platform_init,
  212. #ifdef CONFIG_PM
  213. .platform_suspend = esdhc_of_suspend,
  214. .platform_resume = esdhc_of_resume,
  215. #endif
  216. .adma_workaround = esdhci_of_adma_workaround,
  217. };
  218. static struct sdhci_pltfm_data sdhci_esdhc_pdata = {
  219. /*
  220. * card detection could be handled via GPIO
  221. * eSDHC cannot support End Attribute in NOP ADMA descriptor
  222. */
  223. .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_BROKEN_CARD_DETECTION
  224. | SDHCI_QUIRK_NO_CARD_NO_RESET
  225. | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  226. .ops = &sdhci_esdhc_ops,
  227. };
  228. static int sdhci_esdhc_probe(struct platform_device *pdev)
  229. {
  230. return sdhci_pltfm_register(pdev, &sdhci_esdhc_pdata);
  231. }
  232. static int sdhci_esdhc_remove(struct platform_device *pdev)
  233. {
  234. return sdhci_pltfm_unregister(pdev);
  235. }
  236. static const struct of_device_id sdhci_esdhc_of_match[] = {
  237. { .compatible = "fsl,mpc8379-esdhc" },
  238. { .compatible = "fsl,mpc8536-esdhc" },
  239. { .compatible = "fsl,esdhc" },
  240. { }
  241. };
  242. MODULE_DEVICE_TABLE(of, sdhci_esdhc_of_match);
  243. static struct platform_driver sdhci_esdhc_driver = {
  244. .driver = {
  245. .name = "sdhci-esdhc",
  246. .owner = THIS_MODULE,
  247. .of_match_table = sdhci_esdhc_of_match,
  248. .pm = SDHCI_PLTFM_PMOPS,
  249. },
  250. .probe = sdhci_esdhc_probe,
  251. .remove = sdhci_esdhc_remove,
  252. };
  253. module_platform_driver(sdhci_esdhc_driver);
  254. MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC");
  255. MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, "
  256. "Anton Vorontsov <avorontsov@ru.mvista.com>");
  257. MODULE_LICENSE("GPL v2");