sdhci-of-esdhc.c 10 KB

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