sdhci-of-esdhc.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. int pre_div = 2;
  176. int div = 1;
  177. u32 temp;
  178. if (clock == 0)
  179. goto out;
  180. /* Workaround to reduce the clock frequency for p1010 esdhc */
  181. if (of_find_compatible_node(NULL, NULL, "fsl,p1010-esdhc")) {
  182. if (clock > 20000000)
  183. clock -= 5000000;
  184. if (clock > 40000000)
  185. clock -= 5000000;
  186. }
  187. temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
  188. temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
  189. | ESDHC_CLOCK_MASK);
  190. sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
  191. while (host->max_clk / pre_div / 16 > clock && pre_div < 256)
  192. pre_div *= 2;
  193. while (host->max_clk / pre_div / div > clock && div < 16)
  194. div++;
  195. dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
  196. clock, host->max_clk / pre_div / div);
  197. pre_div >>= 1;
  198. div--;
  199. temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
  200. temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
  201. | (div << ESDHC_DIVIDER_SHIFT)
  202. | (pre_div << ESDHC_PREDIV_SHIFT));
  203. sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
  204. mdelay(1);
  205. out:
  206. host->clock = clock;
  207. }
  208. #ifdef CONFIG_PM
  209. static u32 esdhc_proctl;
  210. static void esdhc_of_suspend(struct sdhci_host *host)
  211. {
  212. esdhc_proctl = sdhci_be32bs_readl(host, SDHCI_HOST_CONTROL);
  213. }
  214. static void esdhc_of_resume(struct sdhci_host *host)
  215. {
  216. esdhc_of_enable_dma(host);
  217. sdhci_be32bs_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL);
  218. }
  219. #endif
  220. static void esdhc_of_platform_init(struct sdhci_host *host)
  221. {
  222. u32 vvn;
  223. vvn = in_be32(host->ioaddr + SDHCI_SLOT_INT_STATUS);
  224. vvn = (vvn & SDHCI_VENDOR_VER_MASK) >> SDHCI_VENDOR_VER_SHIFT;
  225. if (vvn == VENDOR_V_22)
  226. host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23;
  227. if (vvn > VENDOR_V_22)
  228. host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ;
  229. }
  230. static int esdhc_pltfm_bus_width(struct sdhci_host *host, int width)
  231. {
  232. u32 ctrl;
  233. switch (width) {
  234. case MMC_BUS_WIDTH_8:
  235. ctrl = ESDHC_CTRL_8BITBUS;
  236. break;
  237. case MMC_BUS_WIDTH_4:
  238. ctrl = ESDHC_CTRL_4BITBUS;
  239. break;
  240. default:
  241. ctrl = 0;
  242. break;
  243. }
  244. clrsetbits_be32(host->ioaddr + SDHCI_HOST_CONTROL,
  245. ESDHC_CTRL_BUSWIDTH_MASK, ctrl);
  246. return 0;
  247. }
  248. static const struct sdhci_ops sdhci_esdhc_ops = {
  249. .read_l = esdhc_readl,
  250. .read_w = esdhc_readw,
  251. .read_b = esdhc_readb,
  252. .write_l = esdhc_writel,
  253. .write_w = esdhc_writew,
  254. .write_b = esdhc_writeb,
  255. .set_clock = esdhc_of_set_clock,
  256. .enable_dma = esdhc_of_enable_dma,
  257. .get_max_clock = esdhc_of_get_max_clock,
  258. .get_min_clock = esdhc_of_get_min_clock,
  259. .platform_init = esdhc_of_platform_init,
  260. #ifdef CONFIG_PM
  261. .platform_suspend = esdhc_of_suspend,
  262. .platform_resume = esdhc_of_resume,
  263. #endif
  264. .adma_workaround = esdhci_of_adma_workaround,
  265. .platform_bus_width = esdhc_pltfm_bus_width,
  266. };
  267. static const struct sdhci_pltfm_data sdhci_esdhc_pdata = {
  268. /*
  269. * card detection could be handled via GPIO
  270. * eSDHC cannot support End Attribute in NOP ADMA descriptor
  271. */
  272. .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_BROKEN_CARD_DETECTION
  273. | SDHCI_QUIRK_NO_CARD_NO_RESET
  274. | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  275. .ops = &sdhci_esdhc_ops,
  276. };
  277. static int sdhci_esdhc_probe(struct platform_device *pdev)
  278. {
  279. struct sdhci_host *host;
  280. struct device_node *np;
  281. int ret;
  282. host = sdhci_pltfm_init(pdev, &sdhci_esdhc_pdata, 0);
  283. if (IS_ERR(host))
  284. return PTR_ERR(host);
  285. sdhci_get_of_property(pdev);
  286. np = pdev->dev.of_node;
  287. if (of_device_is_compatible(np, "fsl,p2020-esdhc")) {
  288. /*
  289. * Freescale messed up with P2020 as it has a non-standard
  290. * host control register
  291. */
  292. host->quirks2 |= SDHCI_QUIRK2_BROKEN_HOST_CONTROL;
  293. }
  294. /* call to generic mmc_of_parse to support additional capabilities */
  295. mmc_of_parse(host->mmc);
  296. mmc_of_parse_voltage(np, &host->ocr_mask);
  297. ret = sdhci_add_host(host);
  298. if (ret)
  299. sdhci_pltfm_free(pdev);
  300. return ret;
  301. }
  302. static int sdhci_esdhc_remove(struct platform_device *pdev)
  303. {
  304. return sdhci_pltfm_unregister(pdev);
  305. }
  306. static const struct of_device_id sdhci_esdhc_of_match[] = {
  307. { .compatible = "fsl,mpc8379-esdhc" },
  308. { .compatible = "fsl,mpc8536-esdhc" },
  309. { .compatible = "fsl,esdhc" },
  310. { }
  311. };
  312. MODULE_DEVICE_TABLE(of, sdhci_esdhc_of_match);
  313. static struct platform_driver sdhci_esdhc_driver = {
  314. .driver = {
  315. .name = "sdhci-esdhc",
  316. .owner = THIS_MODULE,
  317. .of_match_table = sdhci_esdhc_of_match,
  318. .pm = SDHCI_PLTFM_PMOPS,
  319. },
  320. .probe = sdhci_esdhc_probe,
  321. .remove = sdhci_esdhc_remove,
  322. };
  323. module_platform_driver(sdhci_esdhc_driver);
  324. MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC");
  325. MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, "
  326. "Anton Vorontsov <avorontsov@ru.mvista.com>");
  327. MODULE_LICENSE("GPL v2");