sdhci-of-esdhc.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Freescale eSDHC controller driver.
  4. *
  5. * Copyright (c) 2007, 2010, 2012 Freescale Semiconductor, Inc.
  6. * Copyright (c) 2009 MontaVista Software, Inc.
  7. * Copyright 2020 NXP
  8. *
  9. * Authors: Xiaobo Xie <[email protected]>
  10. * Anton Vorontsov <[email protected]>
  11. */
  12. #include <linux/err.h>
  13. #include <linux/io.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/delay.h>
  17. #include <linux/module.h>
  18. #include <linux/sys_soc.h>
  19. #include <linux/clk.h>
  20. #include <linux/ktime.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/iopoll.h>
  23. #include <linux/mmc/host.h>
  24. #include <linux/mmc/mmc.h>
  25. #include "sdhci-pltfm.h"
  26. #include "sdhci-esdhc.h"
  27. #define VENDOR_V_22 0x12
  28. #define VENDOR_V_23 0x13
  29. #define MMC_TIMING_NUM (MMC_TIMING_MMC_HS400 + 1)
  30. struct esdhc_clk_fixup {
  31. const unsigned int sd_dflt_max_clk;
  32. const unsigned int max_clk[MMC_TIMING_NUM];
  33. };
  34. static const struct esdhc_clk_fixup ls1021a_esdhc_clk = {
  35. .sd_dflt_max_clk = 25000000,
  36. .max_clk[MMC_TIMING_MMC_HS] = 46500000,
  37. .max_clk[MMC_TIMING_SD_HS] = 46500000,
  38. };
  39. static const struct esdhc_clk_fixup ls1046a_esdhc_clk = {
  40. .sd_dflt_max_clk = 25000000,
  41. .max_clk[MMC_TIMING_UHS_SDR104] = 167000000,
  42. .max_clk[MMC_TIMING_MMC_HS200] = 167000000,
  43. };
  44. static const struct esdhc_clk_fixup ls1012a_esdhc_clk = {
  45. .sd_dflt_max_clk = 25000000,
  46. .max_clk[MMC_TIMING_UHS_SDR104] = 125000000,
  47. .max_clk[MMC_TIMING_MMC_HS200] = 125000000,
  48. };
  49. static const struct esdhc_clk_fixup p1010_esdhc_clk = {
  50. .sd_dflt_max_clk = 20000000,
  51. .max_clk[MMC_TIMING_LEGACY] = 20000000,
  52. .max_clk[MMC_TIMING_MMC_HS] = 42000000,
  53. .max_clk[MMC_TIMING_SD_HS] = 40000000,
  54. };
  55. static const struct of_device_id sdhci_esdhc_of_match[] = {
  56. { .compatible = "fsl,ls1021a-esdhc", .data = &ls1021a_esdhc_clk},
  57. { .compatible = "fsl,ls1046a-esdhc", .data = &ls1046a_esdhc_clk},
  58. { .compatible = "fsl,ls1012a-esdhc", .data = &ls1012a_esdhc_clk},
  59. { .compatible = "fsl,p1010-esdhc", .data = &p1010_esdhc_clk},
  60. { .compatible = "fsl,mpc8379-esdhc" },
  61. { .compatible = "fsl,mpc8536-esdhc" },
  62. { .compatible = "fsl,esdhc" },
  63. { }
  64. };
  65. MODULE_DEVICE_TABLE(of, sdhci_esdhc_of_match);
  66. struct sdhci_esdhc {
  67. u8 vendor_ver;
  68. u8 spec_ver;
  69. bool quirk_incorrect_hostver;
  70. bool quirk_limited_clk_division;
  71. bool quirk_unreliable_pulse_detection;
  72. bool quirk_tuning_erratum_type1;
  73. bool quirk_tuning_erratum_type2;
  74. bool quirk_ignore_data_inhibit;
  75. bool quirk_delay_before_data_reset;
  76. bool quirk_trans_complete_erratum;
  77. bool in_sw_tuning;
  78. unsigned int peripheral_clock;
  79. const struct esdhc_clk_fixup *clk_fixup;
  80. u32 div_ratio;
  81. };
  82. /**
  83. * esdhc_read*_fixup - Fixup the value read from incompatible eSDHC register
  84. * to make it compatible with SD spec.
  85. *
  86. * @host: pointer to sdhci_host
  87. * @spec_reg: SD spec register address
  88. * @value: 32bit eSDHC register value on spec_reg address
  89. *
  90. * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC
  91. * registers are 32 bits. There are differences in register size, register
  92. * address, register function, bit position and function between eSDHC spec
  93. * and SD spec.
  94. *
  95. * Return a fixed up register value
  96. */
  97. static u32 esdhc_readl_fixup(struct sdhci_host *host,
  98. int spec_reg, u32 value)
  99. {
  100. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  101. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  102. u32 ret;
  103. /*
  104. * The bit of ADMA flag in eSDHC is not compatible with standard
  105. * SDHC register, so set fake flag SDHCI_CAN_DO_ADMA2 when ADMA is
  106. * supported by eSDHC.
  107. * And for many FSL eSDHC controller, the reset value of field
  108. * SDHCI_CAN_DO_ADMA1 is 1, but some of them can't support ADMA,
  109. * only these vendor version is greater than 2.2/0x12 support ADMA.
  110. */
  111. if ((spec_reg == SDHCI_CAPABILITIES) && (value & SDHCI_CAN_DO_ADMA1)) {
  112. if (esdhc->vendor_ver > VENDOR_V_22) {
  113. ret = value | SDHCI_CAN_DO_ADMA2;
  114. return ret;
  115. }
  116. }
  117. /*
  118. * The DAT[3:0] line signal levels and the CMD line signal level are
  119. * not compatible with standard SDHC register. The line signal levels
  120. * DAT[7:0] are at bits 31:24 and the command line signal level is at
  121. * bit 23. All other bits are the same as in the standard SDHC
  122. * register.
  123. */
  124. if (spec_reg == SDHCI_PRESENT_STATE) {
  125. ret = value & 0x000fffff;
  126. ret |= (value >> 4) & SDHCI_DATA_LVL_MASK;
  127. ret |= (value << 1) & SDHCI_CMD_LVL;
  128. /*
  129. * Some controllers have unreliable Data Line Active
  130. * bit for commands with busy signal. This affects
  131. * Command Inhibit (data) bit. Just ignore it since
  132. * MMC core driver has already polled card status
  133. * with CMD13 after any command with busy siganl.
  134. */
  135. if (esdhc->quirk_ignore_data_inhibit)
  136. ret &= ~SDHCI_DATA_INHIBIT;
  137. return ret;
  138. }
  139. /*
  140. * DTS properties of mmc host are used to enable each speed mode
  141. * according to soc and board capability. So clean up
  142. * SDR50/SDR104/DDR50 support bits here.
  143. */
  144. if (spec_reg == SDHCI_CAPABILITIES_1) {
  145. ret = value & ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_SDR104 |
  146. SDHCI_SUPPORT_DDR50);
  147. return ret;
  148. }
  149. ret = value;
  150. return ret;
  151. }
  152. static u16 esdhc_readw_fixup(struct sdhci_host *host,
  153. int spec_reg, u32 value)
  154. {
  155. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  156. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  157. u16 ret;
  158. int shift = (spec_reg & 0x2) * 8;
  159. if (spec_reg == SDHCI_TRANSFER_MODE)
  160. return pltfm_host->xfer_mode_shadow;
  161. if (spec_reg == SDHCI_HOST_VERSION)
  162. ret = value & 0xffff;
  163. else
  164. ret = (value >> shift) & 0xffff;
  165. /* Workaround for T4240-R1.0-R2.0 eSDHC which has incorrect
  166. * vendor version and spec version information.
  167. */
  168. if ((spec_reg == SDHCI_HOST_VERSION) &&
  169. (esdhc->quirk_incorrect_hostver))
  170. ret = (VENDOR_V_23 << SDHCI_VENDOR_VER_SHIFT) | SDHCI_SPEC_200;
  171. return ret;
  172. }
  173. static u8 esdhc_readb_fixup(struct sdhci_host *host,
  174. int spec_reg, u32 value)
  175. {
  176. u8 ret;
  177. u8 dma_bits;
  178. int shift = (spec_reg & 0x3) * 8;
  179. ret = (value >> shift) & 0xff;
  180. /*
  181. * "DMA select" locates at offset 0x28 in SD specification, but on
  182. * P5020 or P3041, it locates at 0x29.
  183. */
  184. if (spec_reg == SDHCI_HOST_CONTROL) {
  185. /* DMA select is 22,23 bits in Protocol Control Register */
  186. dma_bits = (value >> 5) & SDHCI_CTRL_DMA_MASK;
  187. /* fixup the result */
  188. ret &= ~SDHCI_CTRL_DMA_MASK;
  189. ret |= dma_bits;
  190. }
  191. return ret;
  192. }
  193. /**
  194. * esdhc_write*_fixup - Fixup the SD spec register value so that it could be
  195. * written into eSDHC register.
  196. *
  197. * @host: pointer to sdhci_host
  198. * @spec_reg: SD spec register address
  199. * @value: 8/16/32bit SD spec register value that would be written
  200. * @old_value: 32bit eSDHC register value on spec_reg address
  201. *
  202. * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC
  203. * registers are 32 bits. There are differences in register size, register
  204. * address, register function, bit position and function between eSDHC spec
  205. * and SD spec.
  206. *
  207. * Return a fixed up register value
  208. */
  209. static u32 esdhc_writel_fixup(struct sdhci_host *host,
  210. int spec_reg, u32 value, u32 old_value)
  211. {
  212. u32 ret;
  213. /*
  214. * Enabling IRQSTATEN[BGESEN] is just to set IRQSTAT[BGE]
  215. * when SYSCTL[RSTD] is set for some special operations.
  216. * No any impact on other operation.
  217. */
  218. if (spec_reg == SDHCI_INT_ENABLE)
  219. ret = value | SDHCI_INT_BLK_GAP;
  220. else
  221. ret = value;
  222. return ret;
  223. }
  224. static u32 esdhc_writew_fixup(struct sdhci_host *host,
  225. int spec_reg, u16 value, u32 old_value)
  226. {
  227. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  228. int shift = (spec_reg & 0x2) * 8;
  229. u32 ret;
  230. switch (spec_reg) {
  231. case SDHCI_TRANSFER_MODE:
  232. /*
  233. * Postpone this write, we must do it together with a
  234. * command write that is down below. Return old value.
  235. */
  236. pltfm_host->xfer_mode_shadow = value;
  237. return old_value;
  238. case SDHCI_COMMAND:
  239. ret = (value << 16) | pltfm_host->xfer_mode_shadow;
  240. return ret;
  241. }
  242. ret = old_value & (~(0xffff << shift));
  243. ret |= (value << shift);
  244. if (spec_reg == SDHCI_BLOCK_SIZE) {
  245. /*
  246. * Two last DMA bits are reserved, and first one is used for
  247. * non-standard blksz of 4096 bytes that we don't support
  248. * yet. So clear the DMA boundary bits.
  249. */
  250. ret &= (~SDHCI_MAKE_BLKSZ(0x7, 0));
  251. }
  252. return ret;
  253. }
  254. static u32 esdhc_writeb_fixup(struct sdhci_host *host,
  255. int spec_reg, u8 value, u32 old_value)
  256. {
  257. u32 ret;
  258. u32 dma_bits;
  259. u8 tmp;
  260. int shift = (spec_reg & 0x3) * 8;
  261. /*
  262. * eSDHC doesn't have a standard power control register, so we do
  263. * nothing here to avoid incorrect operation.
  264. */
  265. if (spec_reg == SDHCI_POWER_CONTROL)
  266. return old_value;
  267. /*
  268. * "DMA select" location is offset 0x28 in SD specification, but on
  269. * P5020 or P3041, it's located at 0x29.
  270. */
  271. if (spec_reg == SDHCI_HOST_CONTROL) {
  272. /*
  273. * If host control register is not standard, exit
  274. * this function
  275. */
  276. if (host->quirks2 & SDHCI_QUIRK2_BROKEN_HOST_CONTROL)
  277. return old_value;
  278. /* DMA select is 22,23 bits in Protocol Control Register */
  279. dma_bits = (value & SDHCI_CTRL_DMA_MASK) << 5;
  280. ret = (old_value & (~(SDHCI_CTRL_DMA_MASK << 5))) | dma_bits;
  281. tmp = (value & (~SDHCI_CTRL_DMA_MASK)) |
  282. (old_value & SDHCI_CTRL_DMA_MASK);
  283. ret = (ret & (~0xff)) | tmp;
  284. /* Prevent SDHCI core from writing reserved bits (e.g. HISPD) */
  285. ret &= ~ESDHC_HOST_CONTROL_RES;
  286. return ret;
  287. }
  288. ret = (old_value & (~(0xff << shift))) | (value << shift);
  289. return ret;
  290. }
  291. static u32 esdhc_be_readl(struct sdhci_host *host, int reg)
  292. {
  293. u32 ret;
  294. u32 value;
  295. if (reg == SDHCI_CAPABILITIES_1)
  296. value = ioread32be(host->ioaddr + ESDHC_CAPABILITIES_1);
  297. else
  298. value = ioread32be(host->ioaddr + reg);
  299. ret = esdhc_readl_fixup(host, reg, value);
  300. return ret;
  301. }
  302. static u32 esdhc_le_readl(struct sdhci_host *host, int reg)
  303. {
  304. u32 ret;
  305. u32 value;
  306. if (reg == SDHCI_CAPABILITIES_1)
  307. value = ioread32(host->ioaddr + ESDHC_CAPABILITIES_1);
  308. else
  309. value = ioread32(host->ioaddr + reg);
  310. ret = esdhc_readl_fixup(host, reg, value);
  311. return ret;
  312. }
  313. static u16 esdhc_be_readw(struct sdhci_host *host, int reg)
  314. {
  315. u16 ret;
  316. u32 value;
  317. int base = reg & ~0x3;
  318. value = ioread32be(host->ioaddr + base);
  319. ret = esdhc_readw_fixup(host, reg, value);
  320. return ret;
  321. }
  322. static u16 esdhc_le_readw(struct sdhci_host *host, int reg)
  323. {
  324. u16 ret;
  325. u32 value;
  326. int base = reg & ~0x3;
  327. value = ioread32(host->ioaddr + base);
  328. ret = esdhc_readw_fixup(host, reg, value);
  329. return ret;
  330. }
  331. static u8 esdhc_be_readb(struct sdhci_host *host, int reg)
  332. {
  333. u8 ret;
  334. u32 value;
  335. int base = reg & ~0x3;
  336. value = ioread32be(host->ioaddr + base);
  337. ret = esdhc_readb_fixup(host, reg, value);
  338. return ret;
  339. }
  340. static u8 esdhc_le_readb(struct sdhci_host *host, int reg)
  341. {
  342. u8 ret;
  343. u32 value;
  344. int base = reg & ~0x3;
  345. value = ioread32(host->ioaddr + base);
  346. ret = esdhc_readb_fixup(host, reg, value);
  347. return ret;
  348. }
  349. static void esdhc_be_writel(struct sdhci_host *host, u32 val, int reg)
  350. {
  351. u32 value;
  352. value = esdhc_writel_fixup(host, reg, val, 0);
  353. iowrite32be(value, host->ioaddr + reg);
  354. }
  355. static void esdhc_le_writel(struct sdhci_host *host, u32 val, int reg)
  356. {
  357. u32 value;
  358. value = esdhc_writel_fixup(host, reg, val, 0);
  359. iowrite32(value, host->ioaddr + reg);
  360. }
  361. static void esdhc_be_writew(struct sdhci_host *host, u16 val, int reg)
  362. {
  363. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  364. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  365. int base = reg & ~0x3;
  366. u32 value;
  367. u32 ret;
  368. value = ioread32be(host->ioaddr + base);
  369. ret = esdhc_writew_fixup(host, reg, val, value);
  370. if (reg != SDHCI_TRANSFER_MODE)
  371. iowrite32be(ret, host->ioaddr + base);
  372. /* Starting SW tuning requires ESDHC_SMPCLKSEL to be set
  373. * 1us later after ESDHC_EXTN is set.
  374. */
  375. if (base == ESDHC_SYSTEM_CONTROL_2) {
  376. if (!(value & ESDHC_EXTN) && (ret & ESDHC_EXTN) &&
  377. esdhc->in_sw_tuning) {
  378. udelay(1);
  379. ret |= ESDHC_SMPCLKSEL;
  380. iowrite32be(ret, host->ioaddr + base);
  381. }
  382. }
  383. }
  384. static void esdhc_le_writew(struct sdhci_host *host, u16 val, int reg)
  385. {
  386. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  387. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  388. int base = reg & ~0x3;
  389. u32 value;
  390. u32 ret;
  391. value = ioread32(host->ioaddr + base);
  392. ret = esdhc_writew_fixup(host, reg, val, value);
  393. if (reg != SDHCI_TRANSFER_MODE)
  394. iowrite32(ret, host->ioaddr + base);
  395. /* Starting SW tuning requires ESDHC_SMPCLKSEL to be set
  396. * 1us later after ESDHC_EXTN is set.
  397. */
  398. if (base == ESDHC_SYSTEM_CONTROL_2) {
  399. if (!(value & ESDHC_EXTN) && (ret & ESDHC_EXTN) &&
  400. esdhc->in_sw_tuning) {
  401. udelay(1);
  402. ret |= ESDHC_SMPCLKSEL;
  403. iowrite32(ret, host->ioaddr + base);
  404. }
  405. }
  406. }
  407. static void esdhc_be_writeb(struct sdhci_host *host, u8 val, int reg)
  408. {
  409. int base = reg & ~0x3;
  410. u32 value;
  411. u32 ret;
  412. value = ioread32be(host->ioaddr + base);
  413. ret = esdhc_writeb_fixup(host, reg, val, value);
  414. iowrite32be(ret, host->ioaddr + base);
  415. }
  416. static void esdhc_le_writeb(struct sdhci_host *host, u8 val, int reg)
  417. {
  418. int base = reg & ~0x3;
  419. u32 value;
  420. u32 ret;
  421. value = ioread32(host->ioaddr + base);
  422. ret = esdhc_writeb_fixup(host, reg, val, value);
  423. iowrite32(ret, host->ioaddr + base);
  424. }
  425. /*
  426. * For Abort or Suspend after Stop at Block Gap, ignore the ADMA
  427. * error(IRQSTAT[ADMAE]) if both Transfer Complete(IRQSTAT[TC])
  428. * and Block Gap Event(IRQSTAT[BGE]) are also set.
  429. * For Continue, apply soft reset for data(SYSCTL[RSTD]);
  430. * and re-issue the entire read transaction from beginning.
  431. */
  432. static void esdhc_of_adma_workaround(struct sdhci_host *host, u32 intmask)
  433. {
  434. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  435. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  436. bool applicable;
  437. dma_addr_t dmastart;
  438. dma_addr_t dmanow;
  439. applicable = (intmask & SDHCI_INT_DATA_END) &&
  440. (intmask & SDHCI_INT_BLK_GAP) &&
  441. (esdhc->vendor_ver == VENDOR_V_23);
  442. if (!applicable)
  443. return;
  444. host->data->error = 0;
  445. dmastart = sg_dma_address(host->data->sg);
  446. dmanow = dmastart + host->data->bytes_xfered;
  447. /*
  448. * Force update to the next DMA block boundary.
  449. */
  450. dmanow = (dmanow & ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
  451. SDHCI_DEFAULT_BOUNDARY_SIZE;
  452. host->data->bytes_xfered = dmanow - dmastart;
  453. sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
  454. }
  455. static int esdhc_of_enable_dma(struct sdhci_host *host)
  456. {
  457. int ret;
  458. u32 value;
  459. struct device *dev = mmc_dev(host->mmc);
  460. if (of_device_is_compatible(dev->of_node, "fsl,ls1043a-esdhc") ||
  461. of_device_is_compatible(dev->of_node, "fsl,ls1046a-esdhc")) {
  462. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(40));
  463. if (ret)
  464. return ret;
  465. }
  466. value = sdhci_readl(host, ESDHC_DMA_SYSCTL);
  467. if (of_dma_is_coherent(dev->of_node))
  468. value |= ESDHC_DMA_SNOOP;
  469. else
  470. value &= ~ESDHC_DMA_SNOOP;
  471. sdhci_writel(host, value, ESDHC_DMA_SYSCTL);
  472. return 0;
  473. }
  474. static unsigned int esdhc_of_get_max_clock(struct sdhci_host *host)
  475. {
  476. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  477. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  478. if (esdhc->peripheral_clock)
  479. return esdhc->peripheral_clock;
  480. else
  481. return pltfm_host->clock;
  482. }
  483. static unsigned int esdhc_of_get_min_clock(struct sdhci_host *host)
  484. {
  485. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  486. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  487. unsigned int clock;
  488. if (esdhc->peripheral_clock)
  489. clock = esdhc->peripheral_clock;
  490. else
  491. clock = pltfm_host->clock;
  492. return clock / 256 / 16;
  493. }
  494. static void esdhc_clock_enable(struct sdhci_host *host, bool enable)
  495. {
  496. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  497. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  498. ktime_t timeout;
  499. u32 val, clk_en;
  500. clk_en = ESDHC_CLOCK_SDCLKEN;
  501. /*
  502. * IPGEN/HCKEN/PEREN bits exist on eSDHC whose vendor version
  503. * is 2.2 or lower.
  504. */
  505. if (esdhc->vendor_ver <= VENDOR_V_22)
  506. clk_en |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN |
  507. ESDHC_CLOCK_PEREN);
  508. val = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
  509. if (enable)
  510. val |= clk_en;
  511. else
  512. val &= ~clk_en;
  513. sdhci_writel(host, val, ESDHC_SYSTEM_CONTROL);
  514. /*
  515. * Wait max 20 ms. If vendor version is 2.2 or lower, do not
  516. * wait clock stable bit which does not exist.
  517. */
  518. timeout = ktime_add_ms(ktime_get(), 20);
  519. while (esdhc->vendor_ver > VENDOR_V_22) {
  520. bool timedout = ktime_after(ktime_get(), timeout);
  521. if (sdhci_readl(host, ESDHC_PRSSTAT) & ESDHC_CLOCK_STABLE)
  522. break;
  523. if (timedout) {
  524. pr_err("%s: Internal clock never stabilised.\n",
  525. mmc_hostname(host->mmc));
  526. break;
  527. }
  528. usleep_range(10, 20);
  529. }
  530. }
  531. static void esdhc_flush_async_fifo(struct sdhci_host *host)
  532. {
  533. ktime_t timeout;
  534. u32 val;
  535. val = sdhci_readl(host, ESDHC_DMA_SYSCTL);
  536. val |= ESDHC_FLUSH_ASYNC_FIFO;
  537. sdhci_writel(host, val, ESDHC_DMA_SYSCTL);
  538. /* Wait max 20 ms */
  539. timeout = ktime_add_ms(ktime_get(), 20);
  540. while (1) {
  541. bool timedout = ktime_after(ktime_get(), timeout);
  542. if (!(sdhci_readl(host, ESDHC_DMA_SYSCTL) &
  543. ESDHC_FLUSH_ASYNC_FIFO))
  544. break;
  545. if (timedout) {
  546. pr_err("%s: flushing asynchronous FIFO timeout.\n",
  547. mmc_hostname(host->mmc));
  548. break;
  549. }
  550. usleep_range(10, 20);
  551. }
  552. }
  553. static void esdhc_of_set_clock(struct sdhci_host *host, unsigned int clock)
  554. {
  555. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  556. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  557. unsigned int pre_div = 1, div = 1;
  558. unsigned int clock_fixup = 0;
  559. ktime_t timeout;
  560. u32 temp;
  561. if (clock == 0) {
  562. host->mmc->actual_clock = 0;
  563. esdhc_clock_enable(host, false);
  564. return;
  565. }
  566. /* Start pre_div at 2 for vendor version < 2.3. */
  567. if (esdhc->vendor_ver < VENDOR_V_23)
  568. pre_div = 2;
  569. /* Fix clock value. */
  570. if (host->mmc->card && mmc_card_sd(host->mmc->card) &&
  571. esdhc->clk_fixup && host->mmc->ios.timing == MMC_TIMING_LEGACY)
  572. clock_fixup = esdhc->clk_fixup->sd_dflt_max_clk;
  573. else if (esdhc->clk_fixup)
  574. clock_fixup = esdhc->clk_fixup->max_clk[host->mmc->ios.timing];
  575. if (clock_fixup == 0 || clock < clock_fixup)
  576. clock_fixup = clock;
  577. /* Calculate pre_div and div. */
  578. while (host->max_clk / pre_div / 16 > clock_fixup && pre_div < 256)
  579. pre_div *= 2;
  580. while (host->max_clk / pre_div / div > clock_fixup && div < 16)
  581. div++;
  582. esdhc->div_ratio = pre_div * div;
  583. /* Limit clock division for HS400 200MHz clock for quirk. */
  584. if (esdhc->quirk_limited_clk_division &&
  585. clock == MMC_HS200_MAX_DTR &&
  586. (host->mmc->ios.timing == MMC_TIMING_MMC_HS400 ||
  587. host->flags & SDHCI_HS400_TUNING)) {
  588. if (esdhc->div_ratio <= 4) {
  589. pre_div = 4;
  590. div = 1;
  591. } else if (esdhc->div_ratio <= 8) {
  592. pre_div = 4;
  593. div = 2;
  594. } else if (esdhc->div_ratio <= 12) {
  595. pre_div = 4;
  596. div = 3;
  597. } else {
  598. pr_warn("%s: using unsupported clock division.\n",
  599. mmc_hostname(host->mmc));
  600. }
  601. esdhc->div_ratio = pre_div * div;
  602. }
  603. host->mmc->actual_clock = host->max_clk / esdhc->div_ratio;
  604. dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
  605. clock, host->mmc->actual_clock);
  606. /* Set clock division into register. */
  607. pre_div >>= 1;
  608. div--;
  609. esdhc_clock_enable(host, false);
  610. temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
  611. temp &= ~ESDHC_CLOCK_MASK;
  612. temp |= ((div << ESDHC_DIVIDER_SHIFT) |
  613. (pre_div << ESDHC_PREDIV_SHIFT));
  614. sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
  615. /*
  616. * Wait max 20 ms. If vendor version is 2.2 or lower, do not
  617. * wait clock stable bit which does not exist.
  618. */
  619. timeout = ktime_add_ms(ktime_get(), 20);
  620. while (esdhc->vendor_ver > VENDOR_V_22) {
  621. bool timedout = ktime_after(ktime_get(), timeout);
  622. if (sdhci_readl(host, ESDHC_PRSSTAT) & ESDHC_CLOCK_STABLE)
  623. break;
  624. if (timedout) {
  625. pr_err("%s: Internal clock never stabilised.\n",
  626. mmc_hostname(host->mmc));
  627. break;
  628. }
  629. usleep_range(10, 20);
  630. }
  631. /* Additional setting for HS400. */
  632. if (host->mmc->ios.timing == MMC_TIMING_MMC_HS400 &&
  633. clock == MMC_HS200_MAX_DTR) {
  634. temp = sdhci_readl(host, ESDHC_TBCTL);
  635. sdhci_writel(host, temp | ESDHC_HS400_MODE, ESDHC_TBCTL);
  636. temp = sdhci_readl(host, ESDHC_SDCLKCTL);
  637. sdhci_writel(host, temp | ESDHC_CMD_CLK_CTL, ESDHC_SDCLKCTL);
  638. esdhc_clock_enable(host, true);
  639. temp = sdhci_readl(host, ESDHC_DLLCFG0);
  640. temp |= ESDHC_DLL_ENABLE;
  641. if (host->mmc->actual_clock == MMC_HS200_MAX_DTR)
  642. temp |= ESDHC_DLL_FREQ_SEL;
  643. sdhci_writel(host, temp, ESDHC_DLLCFG0);
  644. temp |= ESDHC_DLL_RESET;
  645. sdhci_writel(host, temp, ESDHC_DLLCFG0);
  646. udelay(1);
  647. temp &= ~ESDHC_DLL_RESET;
  648. sdhci_writel(host, temp, ESDHC_DLLCFG0);
  649. /* Wait max 20 ms */
  650. if (read_poll_timeout(sdhci_readl, temp,
  651. temp & ESDHC_DLL_STS_SLV_LOCK,
  652. 10, 20000, false,
  653. host, ESDHC_DLLSTAT0))
  654. pr_err("%s: timeout for delay chain lock.\n",
  655. mmc_hostname(host->mmc));
  656. temp = sdhci_readl(host, ESDHC_TBCTL);
  657. sdhci_writel(host, temp | ESDHC_HS400_WNDW_ADJUST, ESDHC_TBCTL);
  658. esdhc_clock_enable(host, false);
  659. esdhc_flush_async_fifo(host);
  660. }
  661. esdhc_clock_enable(host, true);
  662. }
  663. static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
  664. {
  665. u32 ctrl;
  666. ctrl = sdhci_readl(host, ESDHC_PROCTL);
  667. ctrl &= (~ESDHC_CTRL_BUSWIDTH_MASK);
  668. switch (width) {
  669. case MMC_BUS_WIDTH_8:
  670. ctrl |= ESDHC_CTRL_8BITBUS;
  671. break;
  672. case MMC_BUS_WIDTH_4:
  673. ctrl |= ESDHC_CTRL_4BITBUS;
  674. break;
  675. default:
  676. break;
  677. }
  678. sdhci_writel(host, ctrl, ESDHC_PROCTL);
  679. }
  680. static void esdhc_reset(struct sdhci_host *host, u8 mask)
  681. {
  682. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  683. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  684. u32 val, bus_width = 0;
  685. /*
  686. * Add delay to make sure all the DMA transfers are finished
  687. * for quirk.
  688. */
  689. if (esdhc->quirk_delay_before_data_reset &&
  690. (mask & SDHCI_RESET_DATA) &&
  691. (host->flags & SDHCI_REQ_USE_DMA))
  692. mdelay(5);
  693. /*
  694. * Save bus-width for eSDHC whose vendor version is 2.2
  695. * or lower for data reset.
  696. */
  697. if ((mask & SDHCI_RESET_DATA) &&
  698. (esdhc->vendor_ver <= VENDOR_V_22)) {
  699. val = sdhci_readl(host, ESDHC_PROCTL);
  700. bus_width = val & ESDHC_CTRL_BUSWIDTH_MASK;
  701. }
  702. sdhci_reset(host, mask);
  703. /*
  704. * Restore bus-width setting and interrupt registers for eSDHC
  705. * whose vendor version is 2.2 or lower for data reset.
  706. */
  707. if ((mask & SDHCI_RESET_DATA) &&
  708. (esdhc->vendor_ver <= VENDOR_V_22)) {
  709. val = sdhci_readl(host, ESDHC_PROCTL);
  710. val &= ~ESDHC_CTRL_BUSWIDTH_MASK;
  711. val |= bus_width;
  712. sdhci_writel(host, val, ESDHC_PROCTL);
  713. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  714. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  715. }
  716. /*
  717. * Some bits have to be cleaned manually for eSDHC whose spec
  718. * version is higher than 3.0 for all reset.
  719. */
  720. if ((mask & SDHCI_RESET_ALL) &&
  721. (esdhc->spec_ver >= SDHCI_SPEC_300)) {
  722. val = sdhci_readl(host, ESDHC_TBCTL);
  723. val &= ~ESDHC_TB_EN;
  724. sdhci_writel(host, val, ESDHC_TBCTL);
  725. /*
  726. * Initialize eSDHC_DLLCFG1[DLL_PD_PULSE_STRETCH_SEL] to
  727. * 0 for quirk.
  728. */
  729. if (esdhc->quirk_unreliable_pulse_detection) {
  730. val = sdhci_readl(host, ESDHC_DLLCFG1);
  731. val &= ~ESDHC_DLL_PD_PULSE_STRETCH_SEL;
  732. sdhci_writel(host, val, ESDHC_DLLCFG1);
  733. }
  734. }
  735. }
  736. /* The SCFG, Supplemental Configuration Unit, provides SoC specific
  737. * configuration and status registers for the device. There is a
  738. * SDHC IO VSEL control register on SCFG for some platforms. It's
  739. * used to support SDHC IO voltage switching.
  740. */
  741. static const struct of_device_id scfg_device_ids[] = {
  742. { .compatible = "fsl,t1040-scfg", },
  743. { .compatible = "fsl,ls1012a-scfg", },
  744. { .compatible = "fsl,ls1046a-scfg", },
  745. {}
  746. };
  747. /* SDHC IO VSEL control register definition */
  748. #define SCFG_SDHCIOVSELCR 0x408
  749. #define SDHCIOVSELCR_TGLEN 0x80000000
  750. #define SDHCIOVSELCR_VSELVAL 0x60000000
  751. #define SDHCIOVSELCR_SDHC_VS 0x00000001
  752. static int esdhc_signal_voltage_switch(struct mmc_host *mmc,
  753. struct mmc_ios *ios)
  754. {
  755. struct sdhci_host *host = mmc_priv(mmc);
  756. struct device_node *scfg_node;
  757. void __iomem *scfg_base = NULL;
  758. u32 sdhciovselcr;
  759. u32 val;
  760. /*
  761. * Signal Voltage Switching is only applicable for Host Controllers
  762. * v3.00 and above.
  763. */
  764. if (host->version < SDHCI_SPEC_300)
  765. return 0;
  766. val = sdhci_readl(host, ESDHC_PROCTL);
  767. switch (ios->signal_voltage) {
  768. case MMC_SIGNAL_VOLTAGE_330:
  769. val &= ~ESDHC_VOLT_SEL;
  770. sdhci_writel(host, val, ESDHC_PROCTL);
  771. return 0;
  772. case MMC_SIGNAL_VOLTAGE_180:
  773. scfg_node = of_find_matching_node(NULL, scfg_device_ids);
  774. if (scfg_node)
  775. scfg_base = of_iomap(scfg_node, 0);
  776. of_node_put(scfg_node);
  777. if (scfg_base) {
  778. sdhciovselcr = SDHCIOVSELCR_TGLEN |
  779. SDHCIOVSELCR_VSELVAL;
  780. iowrite32be(sdhciovselcr,
  781. scfg_base + SCFG_SDHCIOVSELCR);
  782. val |= ESDHC_VOLT_SEL;
  783. sdhci_writel(host, val, ESDHC_PROCTL);
  784. mdelay(5);
  785. sdhciovselcr = SDHCIOVSELCR_TGLEN |
  786. SDHCIOVSELCR_SDHC_VS;
  787. iowrite32be(sdhciovselcr,
  788. scfg_base + SCFG_SDHCIOVSELCR);
  789. iounmap(scfg_base);
  790. } else {
  791. val |= ESDHC_VOLT_SEL;
  792. sdhci_writel(host, val, ESDHC_PROCTL);
  793. }
  794. return 0;
  795. default:
  796. return 0;
  797. }
  798. }
  799. static struct soc_device_attribute soc_tuning_erratum_type1[] = {
  800. { .family = "QorIQ T1023", },
  801. { .family = "QorIQ T1040", },
  802. { .family = "QorIQ T2080", },
  803. { .family = "QorIQ LS1021A", },
  804. { /* sentinel */ }
  805. };
  806. static struct soc_device_attribute soc_tuning_erratum_type2[] = {
  807. { .family = "QorIQ LS1012A", },
  808. { .family = "QorIQ LS1043A", },
  809. { .family = "QorIQ LS1046A", },
  810. { .family = "QorIQ LS1080A", },
  811. { .family = "QorIQ LS2080A", },
  812. { .family = "QorIQ LA1575A", },
  813. { /* sentinel */ }
  814. };
  815. static void esdhc_tuning_block_enable(struct sdhci_host *host, bool enable)
  816. {
  817. u32 val;
  818. esdhc_clock_enable(host, false);
  819. esdhc_flush_async_fifo(host);
  820. val = sdhci_readl(host, ESDHC_TBCTL);
  821. if (enable)
  822. val |= ESDHC_TB_EN;
  823. else
  824. val &= ~ESDHC_TB_EN;
  825. sdhci_writel(host, val, ESDHC_TBCTL);
  826. esdhc_clock_enable(host, true);
  827. }
  828. static void esdhc_tuning_window_ptr(struct sdhci_host *host, u8 *window_start,
  829. u8 *window_end)
  830. {
  831. u32 val;
  832. /* Write TBCTL[11:8]=4'h8 */
  833. val = sdhci_readl(host, ESDHC_TBCTL);
  834. val &= ~(0xf << 8);
  835. val |= 8 << 8;
  836. sdhci_writel(host, val, ESDHC_TBCTL);
  837. mdelay(1);
  838. /* Read TBCTL[31:0] register and rewrite again */
  839. val = sdhci_readl(host, ESDHC_TBCTL);
  840. sdhci_writel(host, val, ESDHC_TBCTL);
  841. mdelay(1);
  842. /* Read the TBSTAT[31:0] register twice */
  843. val = sdhci_readl(host, ESDHC_TBSTAT);
  844. val = sdhci_readl(host, ESDHC_TBSTAT);
  845. *window_end = val & 0xff;
  846. *window_start = (val >> 8) & 0xff;
  847. }
  848. static void esdhc_prepare_sw_tuning(struct sdhci_host *host, u8 *window_start,
  849. u8 *window_end)
  850. {
  851. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  852. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  853. u8 start_ptr, end_ptr;
  854. if (esdhc->quirk_tuning_erratum_type1) {
  855. *window_start = 5 * esdhc->div_ratio;
  856. *window_end = 3 * esdhc->div_ratio;
  857. return;
  858. }
  859. esdhc_tuning_window_ptr(host, &start_ptr, &end_ptr);
  860. /* Reset data lines by setting ESDHCCTL[RSTD] */
  861. sdhci_reset(host, SDHCI_RESET_DATA);
  862. /* Write 32'hFFFF_FFFF to IRQSTAT register */
  863. sdhci_writel(host, 0xFFFFFFFF, SDHCI_INT_STATUS);
  864. /* If TBSTAT[15:8]-TBSTAT[7:0] > (4 * div_ratio) + 2
  865. * or TBSTAT[7:0]-TBSTAT[15:8] > (4 * div_ratio) + 2,
  866. * then program TBPTR[TB_WNDW_END_PTR] = 4 * div_ratio
  867. * and program TBPTR[TB_WNDW_START_PTR] = 8 * div_ratio.
  868. */
  869. if (abs(start_ptr - end_ptr) > (4 * esdhc->div_ratio + 2)) {
  870. *window_start = 8 * esdhc->div_ratio;
  871. *window_end = 4 * esdhc->div_ratio;
  872. } else {
  873. *window_start = 5 * esdhc->div_ratio;
  874. *window_end = 3 * esdhc->div_ratio;
  875. }
  876. }
  877. static int esdhc_execute_sw_tuning(struct mmc_host *mmc, u32 opcode,
  878. u8 window_start, u8 window_end)
  879. {
  880. struct sdhci_host *host = mmc_priv(mmc);
  881. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  882. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  883. u32 val;
  884. int ret;
  885. /* Program TBPTR[TB_WNDW_END_PTR] and TBPTR[TB_WNDW_START_PTR] */
  886. val = ((u32)window_start << ESDHC_WNDW_STRT_PTR_SHIFT) &
  887. ESDHC_WNDW_STRT_PTR_MASK;
  888. val |= window_end & ESDHC_WNDW_END_PTR_MASK;
  889. sdhci_writel(host, val, ESDHC_TBPTR);
  890. /* Program the software tuning mode by setting TBCTL[TB_MODE]=2'h3 */
  891. val = sdhci_readl(host, ESDHC_TBCTL);
  892. val &= ~ESDHC_TB_MODE_MASK;
  893. val |= ESDHC_TB_MODE_SW;
  894. sdhci_writel(host, val, ESDHC_TBCTL);
  895. esdhc->in_sw_tuning = true;
  896. ret = sdhci_execute_tuning(mmc, opcode);
  897. esdhc->in_sw_tuning = false;
  898. return ret;
  899. }
  900. static int esdhc_execute_tuning(struct mmc_host *mmc, u32 opcode)
  901. {
  902. struct sdhci_host *host = mmc_priv(mmc);
  903. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  904. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  905. u8 window_start, window_end;
  906. int ret, retries = 1;
  907. bool hs400_tuning;
  908. unsigned int clk;
  909. u32 val;
  910. /* For tuning mode, the sd clock divisor value
  911. * must be larger than 3 according to reference manual.
  912. */
  913. clk = esdhc->peripheral_clock / 3;
  914. if (host->clock > clk)
  915. esdhc_of_set_clock(host, clk);
  916. esdhc_tuning_block_enable(host, true);
  917. /*
  918. * The eSDHC controller takes the data timeout value into account
  919. * during tuning. If the SD card is too slow sending the response, the
  920. * timer will expire and a "Buffer Read Ready" interrupt without data
  921. * is triggered. This leads to tuning errors.
  922. *
  923. * Just set the timeout to the maximum value because the core will
  924. * already take care of it in sdhci_send_tuning().
  925. */
  926. sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL);
  927. hs400_tuning = host->flags & SDHCI_HS400_TUNING;
  928. do {
  929. if (esdhc->quirk_limited_clk_division &&
  930. hs400_tuning)
  931. esdhc_of_set_clock(host, host->clock);
  932. /* Do HW tuning */
  933. val = sdhci_readl(host, ESDHC_TBCTL);
  934. val &= ~ESDHC_TB_MODE_MASK;
  935. val |= ESDHC_TB_MODE_3;
  936. sdhci_writel(host, val, ESDHC_TBCTL);
  937. ret = sdhci_execute_tuning(mmc, opcode);
  938. if (ret)
  939. break;
  940. /* For type2 affected platforms of the tuning erratum,
  941. * tuning may succeed although eSDHC might not have
  942. * tuned properly. Need to check tuning window.
  943. */
  944. if (esdhc->quirk_tuning_erratum_type2 &&
  945. !host->tuning_err) {
  946. esdhc_tuning_window_ptr(host, &window_start,
  947. &window_end);
  948. if (abs(window_start - window_end) >
  949. (4 * esdhc->div_ratio + 2))
  950. host->tuning_err = -EAGAIN;
  951. }
  952. /* If HW tuning fails and triggers erratum,
  953. * try workaround.
  954. */
  955. ret = host->tuning_err;
  956. if (ret == -EAGAIN &&
  957. (esdhc->quirk_tuning_erratum_type1 ||
  958. esdhc->quirk_tuning_erratum_type2)) {
  959. /* Recover HS400 tuning flag */
  960. if (hs400_tuning)
  961. host->flags |= SDHCI_HS400_TUNING;
  962. pr_info("%s: Hold on to use fixed sampling clock. Try SW tuning!\n",
  963. mmc_hostname(mmc));
  964. /* Do SW tuning */
  965. esdhc_prepare_sw_tuning(host, &window_start,
  966. &window_end);
  967. ret = esdhc_execute_sw_tuning(mmc, opcode,
  968. window_start,
  969. window_end);
  970. if (ret)
  971. break;
  972. /* Retry both HW/SW tuning with reduced clock. */
  973. ret = host->tuning_err;
  974. if (ret == -EAGAIN && retries) {
  975. /* Recover HS400 tuning flag */
  976. if (hs400_tuning)
  977. host->flags |= SDHCI_HS400_TUNING;
  978. clk = host->max_clk / (esdhc->div_ratio + 1);
  979. esdhc_of_set_clock(host, clk);
  980. pr_info("%s: Hold on to use fixed sampling clock. Try tuning with reduced clock!\n",
  981. mmc_hostname(mmc));
  982. } else {
  983. break;
  984. }
  985. } else {
  986. break;
  987. }
  988. } while (retries--);
  989. if (ret) {
  990. esdhc_tuning_block_enable(host, false);
  991. } else if (hs400_tuning) {
  992. val = sdhci_readl(host, ESDHC_SDTIMNGCTL);
  993. val |= ESDHC_FLW_CTL_BG;
  994. sdhci_writel(host, val, ESDHC_SDTIMNGCTL);
  995. }
  996. return ret;
  997. }
  998. static void esdhc_set_uhs_signaling(struct sdhci_host *host,
  999. unsigned int timing)
  1000. {
  1001. u32 val;
  1002. /*
  1003. * There are specific registers setting for HS400 mode.
  1004. * Clean all of them if controller is in HS400 mode to
  1005. * exit HS400 mode before re-setting any speed mode.
  1006. */
  1007. val = sdhci_readl(host, ESDHC_TBCTL);
  1008. if (val & ESDHC_HS400_MODE) {
  1009. val = sdhci_readl(host, ESDHC_SDTIMNGCTL);
  1010. val &= ~ESDHC_FLW_CTL_BG;
  1011. sdhci_writel(host, val, ESDHC_SDTIMNGCTL);
  1012. val = sdhci_readl(host, ESDHC_SDCLKCTL);
  1013. val &= ~ESDHC_CMD_CLK_CTL;
  1014. sdhci_writel(host, val, ESDHC_SDCLKCTL);
  1015. esdhc_clock_enable(host, false);
  1016. val = sdhci_readl(host, ESDHC_TBCTL);
  1017. val &= ~ESDHC_HS400_MODE;
  1018. sdhci_writel(host, val, ESDHC_TBCTL);
  1019. esdhc_clock_enable(host, true);
  1020. val = sdhci_readl(host, ESDHC_DLLCFG0);
  1021. val &= ~(ESDHC_DLL_ENABLE | ESDHC_DLL_FREQ_SEL);
  1022. sdhci_writel(host, val, ESDHC_DLLCFG0);
  1023. val = sdhci_readl(host, ESDHC_TBCTL);
  1024. val &= ~ESDHC_HS400_WNDW_ADJUST;
  1025. sdhci_writel(host, val, ESDHC_TBCTL);
  1026. esdhc_tuning_block_enable(host, false);
  1027. }
  1028. if (timing == MMC_TIMING_MMC_HS400)
  1029. esdhc_tuning_block_enable(host, true);
  1030. else
  1031. sdhci_set_uhs_signaling(host, timing);
  1032. }
  1033. static u32 esdhc_irq(struct sdhci_host *host, u32 intmask)
  1034. {
  1035. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1036. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  1037. u32 command;
  1038. if (esdhc->quirk_trans_complete_erratum) {
  1039. command = SDHCI_GET_CMD(sdhci_readw(host,
  1040. SDHCI_COMMAND));
  1041. if (command == MMC_WRITE_MULTIPLE_BLOCK &&
  1042. sdhci_readw(host, SDHCI_BLOCK_COUNT) &&
  1043. intmask & SDHCI_INT_DATA_END) {
  1044. intmask &= ~SDHCI_INT_DATA_END;
  1045. sdhci_writel(host, SDHCI_INT_DATA_END,
  1046. SDHCI_INT_STATUS);
  1047. }
  1048. }
  1049. return intmask;
  1050. }
  1051. #ifdef CONFIG_PM_SLEEP
  1052. static u32 esdhc_proctl;
  1053. static int esdhc_of_suspend(struct device *dev)
  1054. {
  1055. struct sdhci_host *host = dev_get_drvdata(dev);
  1056. esdhc_proctl = sdhci_readl(host, SDHCI_HOST_CONTROL);
  1057. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  1058. mmc_retune_needed(host->mmc);
  1059. return sdhci_suspend_host(host);
  1060. }
  1061. static int esdhc_of_resume(struct device *dev)
  1062. {
  1063. struct sdhci_host *host = dev_get_drvdata(dev);
  1064. int ret = sdhci_resume_host(host);
  1065. if (ret == 0) {
  1066. /* Isn't this already done by sdhci_resume_host() ? --rmk */
  1067. esdhc_of_enable_dma(host);
  1068. sdhci_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL);
  1069. }
  1070. return ret;
  1071. }
  1072. #endif
  1073. static SIMPLE_DEV_PM_OPS(esdhc_of_dev_pm_ops,
  1074. esdhc_of_suspend,
  1075. esdhc_of_resume);
  1076. static const struct sdhci_ops sdhci_esdhc_be_ops = {
  1077. .read_l = esdhc_be_readl,
  1078. .read_w = esdhc_be_readw,
  1079. .read_b = esdhc_be_readb,
  1080. .write_l = esdhc_be_writel,
  1081. .write_w = esdhc_be_writew,
  1082. .write_b = esdhc_be_writeb,
  1083. .set_clock = esdhc_of_set_clock,
  1084. .enable_dma = esdhc_of_enable_dma,
  1085. .get_max_clock = esdhc_of_get_max_clock,
  1086. .get_min_clock = esdhc_of_get_min_clock,
  1087. .adma_workaround = esdhc_of_adma_workaround,
  1088. .set_bus_width = esdhc_pltfm_set_bus_width,
  1089. .reset = esdhc_reset,
  1090. .set_uhs_signaling = esdhc_set_uhs_signaling,
  1091. .irq = esdhc_irq,
  1092. };
  1093. static const struct sdhci_ops sdhci_esdhc_le_ops = {
  1094. .read_l = esdhc_le_readl,
  1095. .read_w = esdhc_le_readw,
  1096. .read_b = esdhc_le_readb,
  1097. .write_l = esdhc_le_writel,
  1098. .write_w = esdhc_le_writew,
  1099. .write_b = esdhc_le_writeb,
  1100. .set_clock = esdhc_of_set_clock,
  1101. .enable_dma = esdhc_of_enable_dma,
  1102. .get_max_clock = esdhc_of_get_max_clock,
  1103. .get_min_clock = esdhc_of_get_min_clock,
  1104. .adma_workaround = esdhc_of_adma_workaround,
  1105. .set_bus_width = esdhc_pltfm_set_bus_width,
  1106. .reset = esdhc_reset,
  1107. .set_uhs_signaling = esdhc_set_uhs_signaling,
  1108. .irq = esdhc_irq,
  1109. };
  1110. static const struct sdhci_pltfm_data sdhci_esdhc_be_pdata = {
  1111. .quirks = ESDHC_DEFAULT_QUIRKS |
  1112. #ifdef CONFIG_PPC
  1113. SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  1114. #endif
  1115. SDHCI_QUIRK_NO_CARD_NO_RESET |
  1116. SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  1117. .ops = &sdhci_esdhc_be_ops,
  1118. };
  1119. static const struct sdhci_pltfm_data sdhci_esdhc_le_pdata = {
  1120. .quirks = ESDHC_DEFAULT_QUIRKS |
  1121. SDHCI_QUIRK_NO_CARD_NO_RESET |
  1122. SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  1123. .ops = &sdhci_esdhc_le_ops,
  1124. };
  1125. static struct soc_device_attribute soc_incorrect_hostver[] = {
  1126. { .family = "QorIQ T4240", .revision = "1.0", },
  1127. { .family = "QorIQ T4240", .revision = "2.0", },
  1128. { /* sentinel */ }
  1129. };
  1130. static struct soc_device_attribute soc_fixup_sdhc_clkdivs[] = {
  1131. { .family = "QorIQ LX2160A", .revision = "1.0", },
  1132. { .family = "QorIQ LX2160A", .revision = "2.0", },
  1133. { .family = "QorIQ LS1028A", .revision = "1.0", },
  1134. { /* sentinel */ }
  1135. };
  1136. static struct soc_device_attribute soc_unreliable_pulse_detection[] = {
  1137. { .family = "QorIQ LX2160A", .revision = "1.0", },
  1138. { .family = "QorIQ LX2160A", .revision = "2.0", },
  1139. { .family = "QorIQ LS1028A", .revision = "1.0", },
  1140. { /* sentinel */ }
  1141. };
  1142. static void esdhc_init(struct platform_device *pdev, struct sdhci_host *host)
  1143. {
  1144. const struct of_device_id *match;
  1145. struct sdhci_pltfm_host *pltfm_host;
  1146. struct sdhci_esdhc *esdhc;
  1147. struct device_node *np;
  1148. struct clk *clk;
  1149. u32 val;
  1150. u16 host_ver;
  1151. pltfm_host = sdhci_priv(host);
  1152. esdhc = sdhci_pltfm_priv(pltfm_host);
  1153. host_ver = sdhci_readw(host, SDHCI_HOST_VERSION);
  1154. esdhc->vendor_ver = (host_ver & SDHCI_VENDOR_VER_MASK) >>
  1155. SDHCI_VENDOR_VER_SHIFT;
  1156. esdhc->spec_ver = host_ver & SDHCI_SPEC_VER_MASK;
  1157. if (soc_device_match(soc_incorrect_hostver))
  1158. esdhc->quirk_incorrect_hostver = true;
  1159. else
  1160. esdhc->quirk_incorrect_hostver = false;
  1161. if (soc_device_match(soc_fixup_sdhc_clkdivs))
  1162. esdhc->quirk_limited_clk_division = true;
  1163. else
  1164. esdhc->quirk_limited_clk_division = false;
  1165. if (soc_device_match(soc_unreliable_pulse_detection))
  1166. esdhc->quirk_unreliable_pulse_detection = true;
  1167. else
  1168. esdhc->quirk_unreliable_pulse_detection = false;
  1169. match = of_match_node(sdhci_esdhc_of_match, pdev->dev.of_node);
  1170. if (match)
  1171. esdhc->clk_fixup = match->data;
  1172. np = pdev->dev.of_node;
  1173. if (of_device_is_compatible(np, "fsl,p2020-esdhc")) {
  1174. esdhc->quirk_delay_before_data_reset = true;
  1175. esdhc->quirk_trans_complete_erratum = true;
  1176. }
  1177. clk = of_clk_get(np, 0);
  1178. if (!IS_ERR(clk)) {
  1179. /*
  1180. * esdhc->peripheral_clock would be assigned with a value
  1181. * which is eSDHC base clock when use periperal clock.
  1182. * For some platforms, the clock value got by common clk
  1183. * API is peripheral clock while the eSDHC base clock is
  1184. * 1/2 peripheral clock.
  1185. */
  1186. if (of_device_is_compatible(np, "fsl,ls1046a-esdhc") ||
  1187. of_device_is_compatible(np, "fsl,ls1028a-esdhc") ||
  1188. of_device_is_compatible(np, "fsl,ls1088a-esdhc"))
  1189. esdhc->peripheral_clock = clk_get_rate(clk) / 2;
  1190. else
  1191. esdhc->peripheral_clock = clk_get_rate(clk);
  1192. clk_put(clk);
  1193. }
  1194. esdhc_clock_enable(host, false);
  1195. val = sdhci_readl(host, ESDHC_DMA_SYSCTL);
  1196. /*
  1197. * This bit is not able to be reset by SDHCI_RESET_ALL. Need to
  1198. * initialize it as 1 or 0 once, to override the different value
  1199. * which may be configured in bootloader.
  1200. */
  1201. if (esdhc->peripheral_clock)
  1202. val |= ESDHC_PERIPHERAL_CLK_SEL;
  1203. else
  1204. val &= ~ESDHC_PERIPHERAL_CLK_SEL;
  1205. sdhci_writel(host, val, ESDHC_DMA_SYSCTL);
  1206. esdhc_clock_enable(host, true);
  1207. }
  1208. static int esdhc_hs400_prepare_ddr(struct mmc_host *mmc)
  1209. {
  1210. esdhc_tuning_block_enable(mmc_priv(mmc), false);
  1211. return 0;
  1212. }
  1213. static int sdhci_esdhc_probe(struct platform_device *pdev)
  1214. {
  1215. struct sdhci_host *host;
  1216. struct device_node *np, *tp;
  1217. struct sdhci_pltfm_host *pltfm_host;
  1218. struct sdhci_esdhc *esdhc;
  1219. int ret;
  1220. np = pdev->dev.of_node;
  1221. if (of_property_read_bool(np, "little-endian"))
  1222. host = sdhci_pltfm_init(pdev, &sdhci_esdhc_le_pdata,
  1223. sizeof(struct sdhci_esdhc));
  1224. else
  1225. host = sdhci_pltfm_init(pdev, &sdhci_esdhc_be_pdata,
  1226. sizeof(struct sdhci_esdhc));
  1227. if (IS_ERR(host))
  1228. return PTR_ERR(host);
  1229. host->mmc_host_ops.start_signal_voltage_switch =
  1230. esdhc_signal_voltage_switch;
  1231. host->mmc_host_ops.execute_tuning = esdhc_execute_tuning;
  1232. host->mmc_host_ops.hs400_prepare_ddr = esdhc_hs400_prepare_ddr;
  1233. host->tuning_delay = 1;
  1234. esdhc_init(pdev, host);
  1235. sdhci_get_of_property(pdev);
  1236. pltfm_host = sdhci_priv(host);
  1237. esdhc = sdhci_pltfm_priv(pltfm_host);
  1238. if (soc_device_match(soc_tuning_erratum_type1))
  1239. esdhc->quirk_tuning_erratum_type1 = true;
  1240. else
  1241. esdhc->quirk_tuning_erratum_type1 = false;
  1242. if (soc_device_match(soc_tuning_erratum_type2))
  1243. esdhc->quirk_tuning_erratum_type2 = true;
  1244. else
  1245. esdhc->quirk_tuning_erratum_type2 = false;
  1246. if (esdhc->vendor_ver == VENDOR_V_22)
  1247. host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23;
  1248. if (esdhc->vendor_ver > VENDOR_V_22)
  1249. host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ;
  1250. tp = of_find_compatible_node(NULL, NULL, "fsl,p2020-esdhc");
  1251. if (tp) {
  1252. of_node_put(tp);
  1253. host->quirks |= SDHCI_QUIRK_RESET_AFTER_REQUEST;
  1254. host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
  1255. }
  1256. if (of_device_is_compatible(np, "fsl,p5040-esdhc") ||
  1257. of_device_is_compatible(np, "fsl,p5020-esdhc") ||
  1258. of_device_is_compatible(np, "fsl,p4080-esdhc") ||
  1259. of_device_is_compatible(np, "fsl,p1020-esdhc") ||
  1260. of_device_is_compatible(np, "fsl,t1040-esdhc"))
  1261. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  1262. if (of_device_is_compatible(np, "fsl,ls1021a-esdhc"))
  1263. host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
  1264. esdhc->quirk_ignore_data_inhibit = false;
  1265. if (of_device_is_compatible(np, "fsl,p2020-esdhc")) {
  1266. /*
  1267. * Freescale messed up with P2020 as it has a non-standard
  1268. * host control register
  1269. */
  1270. host->quirks2 |= SDHCI_QUIRK2_BROKEN_HOST_CONTROL;
  1271. esdhc->quirk_ignore_data_inhibit = true;
  1272. }
  1273. /* call to generic mmc_of_parse to support additional capabilities */
  1274. ret = mmc_of_parse(host->mmc);
  1275. if (ret)
  1276. goto err;
  1277. mmc_of_parse_voltage(host->mmc, &host->ocr_mask);
  1278. ret = sdhci_add_host(host);
  1279. if (ret)
  1280. goto err;
  1281. return 0;
  1282. err:
  1283. sdhci_pltfm_free(pdev);
  1284. return ret;
  1285. }
  1286. static struct platform_driver sdhci_esdhc_driver = {
  1287. .driver = {
  1288. .name = "sdhci-esdhc",
  1289. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1290. .of_match_table = sdhci_esdhc_of_match,
  1291. .pm = &esdhc_of_dev_pm_ops,
  1292. },
  1293. .probe = sdhci_esdhc_probe,
  1294. .remove = sdhci_pltfm_unregister,
  1295. };
  1296. module_platform_driver(sdhci_esdhc_driver);
  1297. MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC");
  1298. MODULE_AUTHOR("Xiaobo Xie <[email protected]>, "
  1299. "Anton Vorontsov <[email protected]>");
  1300. MODULE_LICENSE("GPL v2");