sdhci-of-esdhc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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/of_address.h>
  19. #include <linux/delay.h>
  20. #include <linux/module.h>
  21. #include <linux/sys_soc.h>
  22. #include <linux/clk.h>
  23. #include <linux/ktime.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/mmc/host.h>
  26. #include "sdhci-pltfm.h"
  27. #include "sdhci-esdhc.h"
  28. #define VENDOR_V_22 0x12
  29. #define VENDOR_V_23 0x13
  30. #define MMC_TIMING_NUM (MMC_TIMING_MMC_HS400 + 1)
  31. struct esdhc_clk_fixup {
  32. const unsigned int sd_dflt_max_clk;
  33. const unsigned int max_clk[MMC_TIMING_NUM];
  34. };
  35. static const struct esdhc_clk_fixup ls1021a_esdhc_clk = {
  36. .sd_dflt_max_clk = 25000000,
  37. .max_clk[MMC_TIMING_MMC_HS] = 46500000,
  38. .max_clk[MMC_TIMING_SD_HS] = 46500000,
  39. };
  40. static const struct esdhc_clk_fixup ls1046a_esdhc_clk = {
  41. .sd_dflt_max_clk = 25000000,
  42. .max_clk[MMC_TIMING_UHS_SDR104] = 167000000,
  43. .max_clk[MMC_TIMING_MMC_HS200] = 167000000,
  44. };
  45. static const struct esdhc_clk_fixup ls1012a_esdhc_clk = {
  46. .sd_dflt_max_clk = 25000000,
  47. .max_clk[MMC_TIMING_UHS_SDR104] = 125000000,
  48. .max_clk[MMC_TIMING_MMC_HS200] = 125000000,
  49. };
  50. static const struct esdhc_clk_fixup p1010_esdhc_clk = {
  51. .sd_dflt_max_clk = 20000000,
  52. .max_clk[MMC_TIMING_LEGACY] = 20000000,
  53. .max_clk[MMC_TIMING_MMC_HS] = 42000000,
  54. .max_clk[MMC_TIMING_SD_HS] = 40000000,
  55. };
  56. static const struct of_device_id sdhci_esdhc_of_match[] = {
  57. { .compatible = "fsl,ls1021a-esdhc", .data = &ls1021a_esdhc_clk},
  58. { .compatible = "fsl,ls1046a-esdhc", .data = &ls1046a_esdhc_clk},
  59. { .compatible = "fsl,ls1012a-esdhc", .data = &ls1012a_esdhc_clk},
  60. { .compatible = "fsl,p1010-esdhc", .data = &p1010_esdhc_clk},
  61. { .compatible = "fsl,mpc8379-esdhc" },
  62. { .compatible = "fsl,mpc8536-esdhc" },
  63. { .compatible = "fsl,esdhc" },
  64. { }
  65. };
  66. MODULE_DEVICE_TABLE(of, sdhci_esdhc_of_match);
  67. struct sdhci_esdhc {
  68. u8 vendor_ver;
  69. u8 spec_ver;
  70. bool quirk_incorrect_hostver;
  71. unsigned int peripheral_clock;
  72. const struct esdhc_clk_fixup *clk_fixup;
  73. };
  74. /**
  75. * esdhc_read*_fixup - Fixup the value read from incompatible eSDHC register
  76. * to make it compatible with SD spec.
  77. *
  78. * @host: pointer to sdhci_host
  79. * @spec_reg: SD spec register address
  80. * @value: 32bit eSDHC register value on spec_reg address
  81. *
  82. * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC
  83. * registers are 32 bits. There are differences in register size, register
  84. * address, register function, bit position and function between eSDHC spec
  85. * and SD spec.
  86. *
  87. * Return a fixed up register value
  88. */
  89. static u32 esdhc_readl_fixup(struct sdhci_host *host,
  90. int spec_reg, u32 value)
  91. {
  92. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  93. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  94. u32 ret;
  95. /*
  96. * The bit of ADMA flag in eSDHC is not compatible with standard
  97. * SDHC register, so set fake flag SDHCI_CAN_DO_ADMA2 when ADMA is
  98. * supported by eSDHC.
  99. * And for many FSL eSDHC controller, the reset value of field
  100. * SDHCI_CAN_DO_ADMA1 is 1, but some of them can't support ADMA,
  101. * only these vendor version is greater than 2.2/0x12 support ADMA.
  102. */
  103. if ((spec_reg == SDHCI_CAPABILITIES) && (value & SDHCI_CAN_DO_ADMA1)) {
  104. if (esdhc->vendor_ver > VENDOR_V_22) {
  105. ret = value | SDHCI_CAN_DO_ADMA2;
  106. return ret;
  107. }
  108. }
  109. /*
  110. * The DAT[3:0] line signal levels and the CMD line signal level are
  111. * not compatible with standard SDHC register. The line signal levels
  112. * DAT[7:0] are at bits 31:24 and the command line signal level is at
  113. * bit 23. All other bits are the same as in the standard SDHC
  114. * register.
  115. */
  116. if (spec_reg == SDHCI_PRESENT_STATE) {
  117. ret = value & 0x000fffff;
  118. ret |= (value >> 4) & SDHCI_DATA_LVL_MASK;
  119. ret |= (value << 1) & SDHCI_CMD_LVL;
  120. return ret;
  121. }
  122. /*
  123. * DTS properties of mmc host are used to enable each speed mode
  124. * according to soc and board capability. So clean up
  125. * SDR50/SDR104/DDR50 support bits here.
  126. */
  127. if (spec_reg == SDHCI_CAPABILITIES_1) {
  128. ret = value & ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_SDR104 |
  129. SDHCI_SUPPORT_DDR50);
  130. return ret;
  131. }
  132. ret = value;
  133. return ret;
  134. }
  135. static u16 esdhc_readw_fixup(struct sdhci_host *host,
  136. int spec_reg, u32 value)
  137. {
  138. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  139. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  140. u16 ret;
  141. int shift = (spec_reg & 0x2) * 8;
  142. if (spec_reg == SDHCI_HOST_VERSION)
  143. ret = value & 0xffff;
  144. else
  145. ret = (value >> shift) & 0xffff;
  146. /* Workaround for T4240-R1.0-R2.0 eSDHC which has incorrect
  147. * vendor version and spec version information.
  148. */
  149. if ((spec_reg == SDHCI_HOST_VERSION) &&
  150. (esdhc->quirk_incorrect_hostver))
  151. ret = (VENDOR_V_23 << SDHCI_VENDOR_VER_SHIFT) | SDHCI_SPEC_200;
  152. return ret;
  153. }
  154. static u8 esdhc_readb_fixup(struct sdhci_host *host,
  155. int spec_reg, u32 value)
  156. {
  157. u8 ret;
  158. u8 dma_bits;
  159. int shift = (spec_reg & 0x3) * 8;
  160. ret = (value >> shift) & 0xff;
  161. /*
  162. * "DMA select" locates at offset 0x28 in SD specification, but on
  163. * P5020 or P3041, it locates at 0x29.
  164. */
  165. if (spec_reg == SDHCI_HOST_CONTROL) {
  166. /* DMA select is 22,23 bits in Protocol Control Register */
  167. dma_bits = (value >> 5) & SDHCI_CTRL_DMA_MASK;
  168. /* fixup the result */
  169. ret &= ~SDHCI_CTRL_DMA_MASK;
  170. ret |= dma_bits;
  171. }
  172. return ret;
  173. }
  174. /**
  175. * esdhc_write*_fixup - Fixup the SD spec register value so that it could be
  176. * written into eSDHC register.
  177. *
  178. * @host: pointer to sdhci_host
  179. * @spec_reg: SD spec register address
  180. * @value: 8/16/32bit SD spec register value that would be written
  181. * @old_value: 32bit eSDHC register value on spec_reg address
  182. *
  183. * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC
  184. * registers are 32 bits. There are differences in register size, register
  185. * address, register function, bit position and function between eSDHC spec
  186. * and SD spec.
  187. *
  188. * Return a fixed up register value
  189. */
  190. static u32 esdhc_writel_fixup(struct sdhci_host *host,
  191. int spec_reg, u32 value, u32 old_value)
  192. {
  193. u32 ret;
  194. /*
  195. * Enabling IRQSTATEN[BGESEN] is just to set IRQSTAT[BGE]
  196. * when SYSCTL[RSTD] is set for some special operations.
  197. * No any impact on other operation.
  198. */
  199. if (spec_reg == SDHCI_INT_ENABLE)
  200. ret = value | SDHCI_INT_BLK_GAP;
  201. else
  202. ret = value;
  203. return ret;
  204. }
  205. static u32 esdhc_writew_fixup(struct sdhci_host *host,
  206. int spec_reg, u16 value, u32 old_value)
  207. {
  208. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  209. int shift = (spec_reg & 0x2) * 8;
  210. u32 ret;
  211. switch (spec_reg) {
  212. case SDHCI_TRANSFER_MODE:
  213. /*
  214. * Postpone this write, we must do it together with a
  215. * command write that is down below. Return old value.
  216. */
  217. pltfm_host->xfer_mode_shadow = value;
  218. return old_value;
  219. case SDHCI_COMMAND:
  220. ret = (value << 16) | pltfm_host->xfer_mode_shadow;
  221. return ret;
  222. }
  223. ret = old_value & (~(0xffff << shift));
  224. ret |= (value << shift);
  225. if (spec_reg == SDHCI_BLOCK_SIZE) {
  226. /*
  227. * Two last DMA bits are reserved, and first one is used for
  228. * non-standard blksz of 4096 bytes that we don't support
  229. * yet. So clear the DMA boundary bits.
  230. */
  231. ret &= (~SDHCI_MAKE_BLKSZ(0x7, 0));
  232. }
  233. return ret;
  234. }
  235. static u32 esdhc_writeb_fixup(struct sdhci_host *host,
  236. int spec_reg, u8 value, u32 old_value)
  237. {
  238. u32 ret;
  239. u32 dma_bits;
  240. u8 tmp;
  241. int shift = (spec_reg & 0x3) * 8;
  242. /*
  243. * eSDHC doesn't have a standard power control register, so we do
  244. * nothing here to avoid incorrect operation.
  245. */
  246. if (spec_reg == SDHCI_POWER_CONTROL)
  247. return old_value;
  248. /*
  249. * "DMA select" location is offset 0x28 in SD specification, but on
  250. * P5020 or P3041, it's located at 0x29.
  251. */
  252. if (spec_reg == SDHCI_HOST_CONTROL) {
  253. /*
  254. * If host control register is not standard, exit
  255. * this function
  256. */
  257. if (host->quirks2 & SDHCI_QUIRK2_BROKEN_HOST_CONTROL)
  258. return old_value;
  259. /* DMA select is 22,23 bits in Protocol Control Register */
  260. dma_bits = (value & SDHCI_CTRL_DMA_MASK) << 5;
  261. ret = (old_value & (~(SDHCI_CTRL_DMA_MASK << 5))) | dma_bits;
  262. tmp = (value & (~SDHCI_CTRL_DMA_MASK)) |
  263. (old_value & SDHCI_CTRL_DMA_MASK);
  264. ret = (ret & (~0xff)) | tmp;
  265. /* Prevent SDHCI core from writing reserved bits (e.g. HISPD) */
  266. ret &= ~ESDHC_HOST_CONTROL_RES;
  267. return ret;
  268. }
  269. ret = (old_value & (~(0xff << shift))) | (value << shift);
  270. return ret;
  271. }
  272. static u32 esdhc_be_readl(struct sdhci_host *host, int reg)
  273. {
  274. u32 ret;
  275. u32 value;
  276. if (reg == SDHCI_CAPABILITIES_1)
  277. value = ioread32be(host->ioaddr + ESDHC_CAPABILITIES_1);
  278. else
  279. value = ioread32be(host->ioaddr + reg);
  280. ret = esdhc_readl_fixup(host, reg, value);
  281. return ret;
  282. }
  283. static u32 esdhc_le_readl(struct sdhci_host *host, int reg)
  284. {
  285. u32 ret;
  286. u32 value;
  287. if (reg == SDHCI_CAPABILITIES_1)
  288. value = ioread32(host->ioaddr + ESDHC_CAPABILITIES_1);
  289. else
  290. value = ioread32(host->ioaddr + reg);
  291. ret = esdhc_readl_fixup(host, reg, value);
  292. return ret;
  293. }
  294. static u16 esdhc_be_readw(struct sdhci_host *host, int reg)
  295. {
  296. u16 ret;
  297. u32 value;
  298. int base = reg & ~0x3;
  299. value = ioread32be(host->ioaddr + base);
  300. ret = esdhc_readw_fixup(host, reg, value);
  301. return ret;
  302. }
  303. static u16 esdhc_le_readw(struct sdhci_host *host, int reg)
  304. {
  305. u16 ret;
  306. u32 value;
  307. int base = reg & ~0x3;
  308. value = ioread32(host->ioaddr + base);
  309. ret = esdhc_readw_fixup(host, reg, value);
  310. return ret;
  311. }
  312. static u8 esdhc_be_readb(struct sdhci_host *host, int reg)
  313. {
  314. u8 ret;
  315. u32 value;
  316. int base = reg & ~0x3;
  317. value = ioread32be(host->ioaddr + base);
  318. ret = esdhc_readb_fixup(host, reg, value);
  319. return ret;
  320. }
  321. static u8 esdhc_le_readb(struct sdhci_host *host, int reg)
  322. {
  323. u8 ret;
  324. u32 value;
  325. int base = reg & ~0x3;
  326. value = ioread32(host->ioaddr + base);
  327. ret = esdhc_readb_fixup(host, reg, value);
  328. return ret;
  329. }
  330. static void esdhc_be_writel(struct sdhci_host *host, u32 val, int reg)
  331. {
  332. u32 value;
  333. value = esdhc_writel_fixup(host, reg, val, 0);
  334. iowrite32be(value, host->ioaddr + reg);
  335. }
  336. static void esdhc_le_writel(struct sdhci_host *host, u32 val, int reg)
  337. {
  338. u32 value;
  339. value = esdhc_writel_fixup(host, reg, val, 0);
  340. iowrite32(value, host->ioaddr + reg);
  341. }
  342. static void esdhc_be_writew(struct sdhci_host *host, u16 val, int reg)
  343. {
  344. int base = reg & ~0x3;
  345. u32 value;
  346. u32 ret;
  347. value = ioread32be(host->ioaddr + base);
  348. ret = esdhc_writew_fixup(host, reg, val, value);
  349. if (reg != SDHCI_TRANSFER_MODE)
  350. iowrite32be(ret, host->ioaddr + base);
  351. }
  352. static void esdhc_le_writew(struct sdhci_host *host, u16 val, int reg)
  353. {
  354. int base = reg & ~0x3;
  355. u32 value;
  356. u32 ret;
  357. value = ioread32(host->ioaddr + base);
  358. ret = esdhc_writew_fixup(host, reg, val, value);
  359. if (reg != SDHCI_TRANSFER_MODE)
  360. iowrite32(ret, host->ioaddr + base);
  361. }
  362. static void esdhc_be_writeb(struct sdhci_host *host, u8 val, int reg)
  363. {
  364. int base = reg & ~0x3;
  365. u32 value;
  366. u32 ret;
  367. value = ioread32be(host->ioaddr + base);
  368. ret = esdhc_writeb_fixup(host, reg, val, value);
  369. iowrite32be(ret, host->ioaddr + base);
  370. }
  371. static void esdhc_le_writeb(struct sdhci_host *host, u8 val, int reg)
  372. {
  373. int base = reg & ~0x3;
  374. u32 value;
  375. u32 ret;
  376. value = ioread32(host->ioaddr + base);
  377. ret = esdhc_writeb_fixup(host, reg, val, value);
  378. iowrite32(ret, host->ioaddr + base);
  379. }
  380. /*
  381. * For Abort or Suspend after Stop at Block Gap, ignore the ADMA
  382. * error(IRQSTAT[ADMAE]) if both Transfer Complete(IRQSTAT[TC])
  383. * and Block Gap Event(IRQSTAT[BGE]) are also set.
  384. * For Continue, apply soft reset for data(SYSCTL[RSTD]);
  385. * and re-issue the entire read transaction from beginning.
  386. */
  387. static void esdhc_of_adma_workaround(struct sdhci_host *host, u32 intmask)
  388. {
  389. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  390. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  391. bool applicable;
  392. dma_addr_t dmastart;
  393. dma_addr_t dmanow;
  394. applicable = (intmask & SDHCI_INT_DATA_END) &&
  395. (intmask & SDHCI_INT_BLK_GAP) &&
  396. (esdhc->vendor_ver == VENDOR_V_23);
  397. if (!applicable)
  398. return;
  399. host->data->error = 0;
  400. dmastart = sg_dma_address(host->data->sg);
  401. dmanow = dmastart + host->data->bytes_xfered;
  402. /*
  403. * Force update to the next DMA block boundary.
  404. */
  405. dmanow = (dmanow & ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
  406. SDHCI_DEFAULT_BOUNDARY_SIZE;
  407. host->data->bytes_xfered = dmanow - dmastart;
  408. sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
  409. }
  410. static int esdhc_of_enable_dma(struct sdhci_host *host)
  411. {
  412. u32 value;
  413. struct device *dev = mmc_dev(host->mmc);
  414. if (of_device_is_compatible(dev->of_node, "fsl,ls1043a-esdhc") ||
  415. of_device_is_compatible(dev->of_node, "fsl,ls1046a-esdhc"))
  416. dma_set_mask_and_coherent(dev, DMA_BIT_MASK(40));
  417. value = sdhci_readl(host, ESDHC_DMA_SYSCTL);
  418. if (of_dma_is_coherent(dev->of_node))
  419. value |= ESDHC_DMA_SNOOP;
  420. else
  421. value &= ~ESDHC_DMA_SNOOP;
  422. sdhci_writel(host, value, ESDHC_DMA_SYSCTL);
  423. return 0;
  424. }
  425. static unsigned int esdhc_of_get_max_clock(struct sdhci_host *host)
  426. {
  427. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  428. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  429. if (esdhc->peripheral_clock)
  430. return esdhc->peripheral_clock;
  431. else
  432. return pltfm_host->clock;
  433. }
  434. static unsigned int esdhc_of_get_min_clock(struct sdhci_host *host)
  435. {
  436. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  437. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  438. unsigned int clock;
  439. if (esdhc->peripheral_clock)
  440. clock = esdhc->peripheral_clock;
  441. else
  442. clock = pltfm_host->clock;
  443. return clock / 256 / 16;
  444. }
  445. static void esdhc_clock_enable(struct sdhci_host *host, bool enable)
  446. {
  447. u32 val;
  448. ktime_t timeout;
  449. val = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
  450. if (enable)
  451. val |= ESDHC_CLOCK_SDCLKEN;
  452. else
  453. val &= ~ESDHC_CLOCK_SDCLKEN;
  454. sdhci_writel(host, val, ESDHC_SYSTEM_CONTROL);
  455. /* Wait max 20 ms */
  456. timeout = ktime_add_ms(ktime_get(), 20);
  457. val = ESDHC_CLOCK_STABLE;
  458. while (1) {
  459. bool timedout = ktime_after(ktime_get(), timeout);
  460. if (sdhci_readl(host, ESDHC_PRSSTAT) & val)
  461. break;
  462. if (timedout) {
  463. pr_err("%s: Internal clock never stabilised.\n",
  464. mmc_hostname(host->mmc));
  465. break;
  466. }
  467. udelay(10);
  468. }
  469. }
  470. static void esdhc_of_set_clock(struct sdhci_host *host, unsigned int clock)
  471. {
  472. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  473. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  474. int pre_div = 1;
  475. int div = 1;
  476. ktime_t timeout;
  477. long fixup = 0;
  478. u32 temp;
  479. host->mmc->actual_clock = 0;
  480. if (clock == 0) {
  481. esdhc_clock_enable(host, false);
  482. return;
  483. }
  484. /* Workaround to start pre_div at 2 for VNN < VENDOR_V_23 */
  485. if (esdhc->vendor_ver < VENDOR_V_23)
  486. pre_div = 2;
  487. if (host->mmc->card && mmc_card_sd(host->mmc->card) &&
  488. esdhc->clk_fixup && host->mmc->ios.timing == MMC_TIMING_LEGACY)
  489. fixup = esdhc->clk_fixup->sd_dflt_max_clk;
  490. else if (esdhc->clk_fixup)
  491. fixup = esdhc->clk_fixup->max_clk[host->mmc->ios.timing];
  492. if (fixup && clock > fixup)
  493. clock = fixup;
  494. temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
  495. temp &= ~(ESDHC_CLOCK_SDCLKEN | ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN |
  496. ESDHC_CLOCK_PEREN | ESDHC_CLOCK_MASK);
  497. sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
  498. while (host->max_clk / pre_div / 16 > clock && pre_div < 256)
  499. pre_div *= 2;
  500. while (host->max_clk / pre_div / div > clock && div < 16)
  501. div++;
  502. dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
  503. clock, host->max_clk / pre_div / div);
  504. host->mmc->actual_clock = host->max_clk / pre_div / div;
  505. pre_div >>= 1;
  506. div--;
  507. temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
  508. temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
  509. | (div << ESDHC_DIVIDER_SHIFT)
  510. | (pre_div << ESDHC_PREDIV_SHIFT));
  511. sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
  512. /* Wait max 20 ms */
  513. timeout = ktime_add_ms(ktime_get(), 20);
  514. while (1) {
  515. bool timedout = ktime_after(ktime_get(), timeout);
  516. if (sdhci_readl(host, ESDHC_PRSSTAT) & ESDHC_CLOCK_STABLE)
  517. break;
  518. if (timedout) {
  519. pr_err("%s: Internal clock never stabilised.\n",
  520. mmc_hostname(host->mmc));
  521. return;
  522. }
  523. udelay(10);
  524. }
  525. temp |= ESDHC_CLOCK_SDCLKEN;
  526. sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
  527. }
  528. static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
  529. {
  530. u32 ctrl;
  531. ctrl = sdhci_readl(host, ESDHC_PROCTL);
  532. ctrl &= (~ESDHC_CTRL_BUSWIDTH_MASK);
  533. switch (width) {
  534. case MMC_BUS_WIDTH_8:
  535. ctrl |= ESDHC_CTRL_8BITBUS;
  536. break;
  537. case MMC_BUS_WIDTH_4:
  538. ctrl |= ESDHC_CTRL_4BITBUS;
  539. break;
  540. default:
  541. break;
  542. }
  543. sdhci_writel(host, ctrl, ESDHC_PROCTL);
  544. }
  545. static void esdhc_reset(struct sdhci_host *host, u8 mask)
  546. {
  547. u32 val;
  548. sdhci_reset(host, mask);
  549. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  550. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  551. if (mask & SDHCI_RESET_ALL) {
  552. val = sdhci_readl(host, ESDHC_TBCTL);
  553. val &= ~ESDHC_TB_EN;
  554. sdhci_writel(host, val, ESDHC_TBCTL);
  555. }
  556. }
  557. /* The SCFG, Supplemental Configuration Unit, provides SoC specific
  558. * configuration and status registers for the device. There is a
  559. * SDHC IO VSEL control register on SCFG for some platforms. It's
  560. * used to support SDHC IO voltage switching.
  561. */
  562. static const struct of_device_id scfg_device_ids[] = {
  563. { .compatible = "fsl,t1040-scfg", },
  564. { .compatible = "fsl,ls1012a-scfg", },
  565. { .compatible = "fsl,ls1046a-scfg", },
  566. {}
  567. };
  568. /* SDHC IO VSEL control register definition */
  569. #define SCFG_SDHCIOVSELCR 0x408
  570. #define SDHCIOVSELCR_TGLEN 0x80000000
  571. #define SDHCIOVSELCR_VSELVAL 0x60000000
  572. #define SDHCIOVSELCR_SDHC_VS 0x00000001
  573. static int esdhc_signal_voltage_switch(struct mmc_host *mmc,
  574. struct mmc_ios *ios)
  575. {
  576. struct sdhci_host *host = mmc_priv(mmc);
  577. struct device_node *scfg_node;
  578. void __iomem *scfg_base = NULL;
  579. u32 sdhciovselcr;
  580. u32 val;
  581. /*
  582. * Signal Voltage Switching is only applicable for Host Controllers
  583. * v3.00 and above.
  584. */
  585. if (host->version < SDHCI_SPEC_300)
  586. return 0;
  587. val = sdhci_readl(host, ESDHC_PROCTL);
  588. switch (ios->signal_voltage) {
  589. case MMC_SIGNAL_VOLTAGE_330:
  590. val &= ~ESDHC_VOLT_SEL;
  591. sdhci_writel(host, val, ESDHC_PROCTL);
  592. return 0;
  593. case MMC_SIGNAL_VOLTAGE_180:
  594. scfg_node = of_find_matching_node(NULL, scfg_device_ids);
  595. if (scfg_node)
  596. scfg_base = of_iomap(scfg_node, 0);
  597. if (scfg_base) {
  598. sdhciovselcr = SDHCIOVSELCR_TGLEN |
  599. SDHCIOVSELCR_VSELVAL;
  600. iowrite32be(sdhciovselcr,
  601. scfg_base + SCFG_SDHCIOVSELCR);
  602. val |= ESDHC_VOLT_SEL;
  603. sdhci_writel(host, val, ESDHC_PROCTL);
  604. mdelay(5);
  605. sdhciovselcr = SDHCIOVSELCR_TGLEN |
  606. SDHCIOVSELCR_SDHC_VS;
  607. iowrite32be(sdhciovselcr,
  608. scfg_base + SCFG_SDHCIOVSELCR);
  609. iounmap(scfg_base);
  610. } else {
  611. val |= ESDHC_VOLT_SEL;
  612. sdhci_writel(host, val, ESDHC_PROCTL);
  613. }
  614. return 0;
  615. default:
  616. return 0;
  617. }
  618. }
  619. static int esdhc_execute_tuning(struct mmc_host *mmc, u32 opcode)
  620. {
  621. struct sdhci_host *host = mmc_priv(mmc);
  622. u32 val;
  623. /* Use tuning block for tuning procedure */
  624. esdhc_clock_enable(host, false);
  625. val = sdhci_readl(host, ESDHC_DMA_SYSCTL);
  626. val |= ESDHC_FLUSH_ASYNC_FIFO;
  627. sdhci_writel(host, val, ESDHC_DMA_SYSCTL);
  628. val = sdhci_readl(host, ESDHC_TBCTL);
  629. val |= ESDHC_TB_EN;
  630. sdhci_writel(host, val, ESDHC_TBCTL);
  631. esdhc_clock_enable(host, true);
  632. return sdhci_execute_tuning(mmc, opcode);
  633. }
  634. #ifdef CONFIG_PM_SLEEP
  635. static u32 esdhc_proctl;
  636. static int esdhc_of_suspend(struct device *dev)
  637. {
  638. struct sdhci_host *host = dev_get_drvdata(dev);
  639. esdhc_proctl = sdhci_readl(host, SDHCI_HOST_CONTROL);
  640. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  641. mmc_retune_needed(host->mmc);
  642. return sdhci_suspend_host(host);
  643. }
  644. static int esdhc_of_resume(struct device *dev)
  645. {
  646. struct sdhci_host *host = dev_get_drvdata(dev);
  647. int ret = sdhci_resume_host(host);
  648. if (ret == 0) {
  649. /* Isn't this already done by sdhci_resume_host() ? --rmk */
  650. esdhc_of_enable_dma(host);
  651. sdhci_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL);
  652. }
  653. return ret;
  654. }
  655. #endif
  656. static SIMPLE_DEV_PM_OPS(esdhc_of_dev_pm_ops,
  657. esdhc_of_suspend,
  658. esdhc_of_resume);
  659. static const struct sdhci_ops sdhci_esdhc_be_ops = {
  660. .read_l = esdhc_be_readl,
  661. .read_w = esdhc_be_readw,
  662. .read_b = esdhc_be_readb,
  663. .write_l = esdhc_be_writel,
  664. .write_w = esdhc_be_writew,
  665. .write_b = esdhc_be_writeb,
  666. .set_clock = esdhc_of_set_clock,
  667. .enable_dma = esdhc_of_enable_dma,
  668. .get_max_clock = esdhc_of_get_max_clock,
  669. .get_min_clock = esdhc_of_get_min_clock,
  670. .adma_workaround = esdhc_of_adma_workaround,
  671. .set_bus_width = esdhc_pltfm_set_bus_width,
  672. .reset = esdhc_reset,
  673. .set_uhs_signaling = sdhci_set_uhs_signaling,
  674. };
  675. static const struct sdhci_ops sdhci_esdhc_le_ops = {
  676. .read_l = esdhc_le_readl,
  677. .read_w = esdhc_le_readw,
  678. .read_b = esdhc_le_readb,
  679. .write_l = esdhc_le_writel,
  680. .write_w = esdhc_le_writew,
  681. .write_b = esdhc_le_writeb,
  682. .set_clock = esdhc_of_set_clock,
  683. .enable_dma = esdhc_of_enable_dma,
  684. .get_max_clock = esdhc_of_get_max_clock,
  685. .get_min_clock = esdhc_of_get_min_clock,
  686. .adma_workaround = esdhc_of_adma_workaround,
  687. .set_bus_width = esdhc_pltfm_set_bus_width,
  688. .reset = esdhc_reset,
  689. .set_uhs_signaling = sdhci_set_uhs_signaling,
  690. };
  691. static const struct sdhci_pltfm_data sdhci_esdhc_be_pdata = {
  692. .quirks = ESDHC_DEFAULT_QUIRKS |
  693. #ifdef CONFIG_PPC
  694. SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  695. #endif
  696. SDHCI_QUIRK_NO_CARD_NO_RESET |
  697. SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  698. .ops = &sdhci_esdhc_be_ops,
  699. };
  700. static const struct sdhci_pltfm_data sdhci_esdhc_le_pdata = {
  701. .quirks = ESDHC_DEFAULT_QUIRKS |
  702. SDHCI_QUIRK_NO_CARD_NO_RESET |
  703. SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  704. .ops = &sdhci_esdhc_le_ops,
  705. };
  706. static struct soc_device_attribute soc_incorrect_hostver[] = {
  707. { .family = "QorIQ T4240", .revision = "1.0", },
  708. { .family = "QorIQ T4240", .revision = "2.0", },
  709. { },
  710. };
  711. static void esdhc_init(struct platform_device *pdev, struct sdhci_host *host)
  712. {
  713. const struct of_device_id *match;
  714. struct sdhci_pltfm_host *pltfm_host;
  715. struct sdhci_esdhc *esdhc;
  716. struct device_node *np;
  717. struct clk *clk;
  718. u32 val;
  719. u16 host_ver;
  720. pltfm_host = sdhci_priv(host);
  721. esdhc = sdhci_pltfm_priv(pltfm_host);
  722. host_ver = sdhci_readw(host, SDHCI_HOST_VERSION);
  723. esdhc->vendor_ver = (host_ver & SDHCI_VENDOR_VER_MASK) >>
  724. SDHCI_VENDOR_VER_SHIFT;
  725. esdhc->spec_ver = host_ver & SDHCI_SPEC_VER_MASK;
  726. if (soc_device_match(soc_incorrect_hostver))
  727. esdhc->quirk_incorrect_hostver = true;
  728. else
  729. esdhc->quirk_incorrect_hostver = false;
  730. match = of_match_node(sdhci_esdhc_of_match, pdev->dev.of_node);
  731. if (match)
  732. esdhc->clk_fixup = match->data;
  733. np = pdev->dev.of_node;
  734. clk = of_clk_get(np, 0);
  735. if (!IS_ERR(clk)) {
  736. /*
  737. * esdhc->peripheral_clock would be assigned with a value
  738. * which is eSDHC base clock when use periperal clock.
  739. * For ls1046a, the clock value got by common clk API is
  740. * peripheral clock while the eSDHC base clock is 1/2
  741. * peripheral clock.
  742. */
  743. if (of_device_is_compatible(np, "fsl,ls1046a-esdhc"))
  744. esdhc->peripheral_clock = clk_get_rate(clk) / 2;
  745. else
  746. esdhc->peripheral_clock = clk_get_rate(clk);
  747. clk_put(clk);
  748. }
  749. if (esdhc->peripheral_clock) {
  750. esdhc_clock_enable(host, false);
  751. val = sdhci_readl(host, ESDHC_DMA_SYSCTL);
  752. val |= ESDHC_PERIPHERAL_CLK_SEL;
  753. sdhci_writel(host, val, ESDHC_DMA_SYSCTL);
  754. esdhc_clock_enable(host, true);
  755. }
  756. }
  757. static int sdhci_esdhc_probe(struct platform_device *pdev)
  758. {
  759. struct sdhci_host *host;
  760. struct device_node *np;
  761. struct sdhci_pltfm_host *pltfm_host;
  762. struct sdhci_esdhc *esdhc;
  763. int ret;
  764. np = pdev->dev.of_node;
  765. if (of_property_read_bool(np, "little-endian"))
  766. host = sdhci_pltfm_init(pdev, &sdhci_esdhc_le_pdata,
  767. sizeof(struct sdhci_esdhc));
  768. else
  769. host = sdhci_pltfm_init(pdev, &sdhci_esdhc_be_pdata,
  770. sizeof(struct sdhci_esdhc));
  771. if (IS_ERR(host))
  772. return PTR_ERR(host);
  773. host->mmc_host_ops.start_signal_voltage_switch =
  774. esdhc_signal_voltage_switch;
  775. host->mmc_host_ops.execute_tuning = esdhc_execute_tuning;
  776. host->tuning_delay = 1;
  777. esdhc_init(pdev, host);
  778. sdhci_get_of_property(pdev);
  779. pltfm_host = sdhci_priv(host);
  780. esdhc = sdhci_pltfm_priv(pltfm_host);
  781. if (esdhc->vendor_ver == VENDOR_V_22)
  782. host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23;
  783. if (esdhc->vendor_ver > VENDOR_V_22)
  784. host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ;
  785. if (of_find_compatible_node(NULL, NULL, "fsl,p2020-esdhc")) {
  786. host->quirks |= SDHCI_QUIRK_RESET_AFTER_REQUEST;
  787. host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
  788. }
  789. if (of_device_is_compatible(np, "fsl,p5040-esdhc") ||
  790. of_device_is_compatible(np, "fsl,p5020-esdhc") ||
  791. of_device_is_compatible(np, "fsl,p4080-esdhc") ||
  792. of_device_is_compatible(np, "fsl,p1020-esdhc") ||
  793. of_device_is_compatible(np, "fsl,t1040-esdhc"))
  794. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  795. if (of_device_is_compatible(np, "fsl,ls1021a-esdhc"))
  796. host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
  797. if (of_device_is_compatible(np, "fsl,p2020-esdhc")) {
  798. /*
  799. * Freescale messed up with P2020 as it has a non-standard
  800. * host control register
  801. */
  802. host->quirks2 |= SDHCI_QUIRK2_BROKEN_HOST_CONTROL;
  803. }
  804. /* call to generic mmc_of_parse to support additional capabilities */
  805. ret = mmc_of_parse(host->mmc);
  806. if (ret)
  807. goto err;
  808. mmc_of_parse_voltage(np, &host->ocr_mask);
  809. ret = sdhci_add_host(host);
  810. if (ret)
  811. goto err;
  812. return 0;
  813. err:
  814. sdhci_pltfm_free(pdev);
  815. return ret;
  816. }
  817. static struct platform_driver sdhci_esdhc_driver = {
  818. .driver = {
  819. .name = "sdhci-esdhc",
  820. .of_match_table = sdhci_esdhc_of_match,
  821. .pm = &esdhc_of_dev_pm_ops,
  822. },
  823. .probe = sdhci_esdhc_probe,
  824. .remove = sdhci_pltfm_unregister,
  825. };
  826. module_platform_driver(sdhci_esdhc_driver);
  827. MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC");
  828. MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, "
  829. "Anton Vorontsov <avorontsov@ru.mvista.com>");
  830. MODULE_LICENSE("GPL v2");