sdhci-of-esdhc.c 9.2 KB

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