sdhci-esdhc-imx.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Freescale eSDHC i.MX controller driver for the platform bus.
  4. *
  5. * derived from the OF-version.
  6. *
  7. * Copyright (c) 2010 Pengutronix e.K.
  8. * Author: Wolfram Sang <[email protected]>
  9. */
  10. #include <linux/bitfield.h>
  11. #include <linux/io.h>
  12. #include <linux/iopoll.h>
  13. #include <linux/delay.h>
  14. #include <linux/err.h>
  15. #include <linux/clk.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/pm_qos.h>
  19. #include <linux/mmc/host.h>
  20. #include <linux/mmc/mmc.h>
  21. #include <linux/mmc/sdio.h>
  22. #include <linux/mmc/slot-gpio.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/pinctrl/consumer.h>
  26. #include <linux/pm_runtime.h>
  27. #include "sdhci-cqhci.h"
  28. #include "sdhci-pltfm.h"
  29. #include "sdhci-esdhc.h"
  30. #include "cqhci.h"
  31. #define ESDHC_SYS_CTRL_DTOCV_MASK 0x0f
  32. #define ESDHC_CTRL_D3CD 0x08
  33. #define ESDHC_BURST_LEN_EN_INCR (1 << 27)
  34. /* VENDOR SPEC register */
  35. #define ESDHC_VENDOR_SPEC 0xc0
  36. #define ESDHC_VENDOR_SPEC_SDIO_QUIRK (1 << 1)
  37. #define ESDHC_VENDOR_SPEC_VSELECT (1 << 1)
  38. #define ESDHC_VENDOR_SPEC_FRC_SDCLK_ON (1 << 8)
  39. #define ESDHC_DEBUG_SEL_AND_STATUS_REG 0xc2
  40. #define ESDHC_DEBUG_SEL_REG 0xc3
  41. #define ESDHC_DEBUG_SEL_MASK 0xf
  42. #define ESDHC_DEBUG_SEL_CMD_STATE 1
  43. #define ESDHC_DEBUG_SEL_DATA_STATE 2
  44. #define ESDHC_DEBUG_SEL_TRANS_STATE 3
  45. #define ESDHC_DEBUG_SEL_DMA_STATE 4
  46. #define ESDHC_DEBUG_SEL_ADMA_STATE 5
  47. #define ESDHC_DEBUG_SEL_FIFO_STATE 6
  48. #define ESDHC_DEBUG_SEL_ASYNC_FIFO_STATE 7
  49. #define ESDHC_WTMK_LVL 0x44
  50. #define ESDHC_WTMK_DEFAULT_VAL 0x10401040
  51. #define ESDHC_WTMK_LVL_RD_WML_MASK 0x000000FF
  52. #define ESDHC_WTMK_LVL_RD_WML_SHIFT 0
  53. #define ESDHC_WTMK_LVL_WR_WML_MASK 0x00FF0000
  54. #define ESDHC_WTMK_LVL_WR_WML_SHIFT 16
  55. #define ESDHC_WTMK_LVL_WML_VAL_DEF 64
  56. #define ESDHC_WTMK_LVL_WML_VAL_MAX 128
  57. #define ESDHC_MIX_CTRL 0x48
  58. #define ESDHC_MIX_CTRL_DDREN (1 << 3)
  59. #define ESDHC_MIX_CTRL_AC23EN (1 << 7)
  60. #define ESDHC_MIX_CTRL_EXE_TUNE (1 << 22)
  61. #define ESDHC_MIX_CTRL_SMPCLK_SEL (1 << 23)
  62. #define ESDHC_MIX_CTRL_AUTO_TUNE_EN (1 << 24)
  63. #define ESDHC_MIX_CTRL_FBCLK_SEL (1 << 25)
  64. #define ESDHC_MIX_CTRL_HS400_EN (1 << 26)
  65. #define ESDHC_MIX_CTRL_HS400_ES_EN (1 << 27)
  66. /* Bits 3 and 6 are not SDHCI standard definitions */
  67. #define ESDHC_MIX_CTRL_SDHCI_MASK 0xb7
  68. /* Tuning bits */
  69. #define ESDHC_MIX_CTRL_TUNING_MASK 0x03c00000
  70. /* dll control register */
  71. #define ESDHC_DLL_CTRL 0x60
  72. #define ESDHC_DLL_OVERRIDE_VAL_SHIFT 9
  73. #define ESDHC_DLL_OVERRIDE_EN_SHIFT 8
  74. /* tune control register */
  75. #define ESDHC_TUNE_CTRL_STATUS 0x68
  76. #define ESDHC_TUNE_CTRL_STEP 1
  77. #define ESDHC_TUNE_CTRL_MIN 0
  78. #define ESDHC_TUNE_CTRL_MAX ((1 << 7) - 1)
  79. /* strobe dll register */
  80. #define ESDHC_STROBE_DLL_CTRL 0x70
  81. #define ESDHC_STROBE_DLL_CTRL_ENABLE (1 << 0)
  82. #define ESDHC_STROBE_DLL_CTRL_RESET (1 << 1)
  83. #define ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT 0x7
  84. #define ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT 3
  85. #define ESDHC_STROBE_DLL_CTRL_SLV_UPDATE_INT_DEFAULT (4 << 20)
  86. #define ESDHC_STROBE_DLL_STATUS 0x74
  87. #define ESDHC_STROBE_DLL_STS_REF_LOCK (1 << 1)
  88. #define ESDHC_STROBE_DLL_STS_SLV_LOCK 0x1
  89. #define ESDHC_VEND_SPEC2 0xc8
  90. #define ESDHC_VEND_SPEC2_EN_BUSY_IRQ (1 << 8)
  91. #define ESDHC_VEND_SPEC2_AUTO_TUNE_8BIT_EN (1 << 4)
  92. #define ESDHC_VEND_SPEC2_AUTO_TUNE_4BIT_EN (0 << 4)
  93. #define ESDHC_VEND_SPEC2_AUTO_TUNE_1BIT_EN (2 << 4)
  94. #define ESDHC_VEND_SPEC2_AUTO_TUNE_CMD_EN (1 << 6)
  95. #define ESDHC_VEND_SPEC2_AUTO_TUNE_MODE_MASK (7 << 4)
  96. #define ESDHC_TUNING_CTRL 0xcc
  97. #define ESDHC_STD_TUNING_EN (1 << 24)
  98. /* NOTE: the minimum valid tuning start tap for mx6sl is 1 */
  99. #define ESDHC_TUNING_START_TAP_DEFAULT 0x1
  100. #define ESDHC_TUNING_START_TAP_MASK 0x7f
  101. #define ESDHC_TUNING_CMD_CRC_CHECK_DISABLE (1 << 7)
  102. #define ESDHC_TUNING_STEP_DEFAULT 0x1
  103. #define ESDHC_TUNING_STEP_MASK 0x00070000
  104. #define ESDHC_TUNING_STEP_SHIFT 16
  105. /* pinctrl state */
  106. #define ESDHC_PINCTRL_STATE_100MHZ "state_100mhz"
  107. #define ESDHC_PINCTRL_STATE_200MHZ "state_200mhz"
  108. /*
  109. * Our interpretation of the SDHCI_HOST_CONTROL register
  110. */
  111. #define ESDHC_CTRL_4BITBUS (0x1 << 1)
  112. #define ESDHC_CTRL_8BITBUS (0x2 << 1)
  113. #define ESDHC_CTRL_BUSWIDTH_MASK (0x3 << 1)
  114. #define USDHC_GET_BUSWIDTH(c) (c & ESDHC_CTRL_BUSWIDTH_MASK)
  115. /*
  116. * There is an INT DMA ERR mismatch between eSDHC and STD SDHC SPEC:
  117. * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
  118. * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
  119. * Define this macro DMA error INT for fsl eSDHC
  120. */
  121. #define ESDHC_INT_VENDOR_SPEC_DMA_ERR (1 << 28)
  122. /* the address offset of CQHCI */
  123. #define ESDHC_CQHCI_ADDR_OFFSET 0x100
  124. /*
  125. * The CMDTYPE of the CMD register (offset 0xE) should be set to
  126. * "11" when the STOP CMD12 is issued on imx53 to abort one
  127. * open ended multi-blk IO. Otherwise the TC INT wouldn't
  128. * be generated.
  129. * In exact block transfer, the controller doesn't complete the
  130. * operations automatically as required at the end of the
  131. * transfer and remains on hold if the abort command is not sent.
  132. * As a result, the TC flag is not asserted and SW received timeout
  133. * exception. Bit1 of Vendor Spec register is used to fix it.
  134. */
  135. #define ESDHC_FLAG_MULTIBLK_NO_INT BIT(1)
  136. /*
  137. * The flag tells that the ESDHC controller is an USDHC block that is
  138. * integrated on the i.MX6 series.
  139. */
  140. #define ESDHC_FLAG_USDHC BIT(3)
  141. /* The IP supports manual tuning process */
  142. #define ESDHC_FLAG_MAN_TUNING BIT(4)
  143. /* The IP supports standard tuning process */
  144. #define ESDHC_FLAG_STD_TUNING BIT(5)
  145. /* The IP has SDHCI_CAPABILITIES_1 register */
  146. #define ESDHC_FLAG_HAVE_CAP1 BIT(6)
  147. /*
  148. * The IP has erratum ERR004536
  149. * uSDHC: ADMA Length Mismatch Error occurs if the AHB read access is slow,
  150. * when reading data from the card
  151. * This flag is also set for i.MX25 and i.MX35 in order to get
  152. * SDHCI_QUIRK_BROKEN_ADMA, but for different reasons (ADMA capability bits).
  153. */
  154. #define ESDHC_FLAG_ERR004536 BIT(7)
  155. /* The IP supports HS200 mode */
  156. #define ESDHC_FLAG_HS200 BIT(8)
  157. /* The IP supports HS400 mode */
  158. #define ESDHC_FLAG_HS400 BIT(9)
  159. /*
  160. * The IP has errata ERR010450
  161. * uSDHC: At 1.8V due to the I/O timing limit, for SDR mode, SD card
  162. * clock can't exceed 150MHz, for DDR mode, SD card clock can't exceed 45MHz.
  163. */
  164. #define ESDHC_FLAG_ERR010450 BIT(10)
  165. /* The IP supports HS400ES mode */
  166. #define ESDHC_FLAG_HS400_ES BIT(11)
  167. /* The IP has Host Controller Interface for Command Queuing */
  168. #define ESDHC_FLAG_CQHCI BIT(12)
  169. /* need request pmqos during low power */
  170. #define ESDHC_FLAG_PMQOS BIT(13)
  171. /* The IP state got lost in low power mode */
  172. #define ESDHC_FLAG_STATE_LOST_IN_LPMODE BIT(14)
  173. /* The IP lost clock rate in PM_RUNTIME */
  174. #define ESDHC_FLAG_CLK_RATE_LOST_IN_PM_RUNTIME BIT(15)
  175. /*
  176. * The IP do not support the ACMD23 feature completely when use ADMA mode.
  177. * In ADMA mode, it only use the 16 bit block count of the register 0x4
  178. * (BLOCK_ATT) as the CMD23's argument for ACMD23 mode, which means it will
  179. * ignore the upper 16 bit of the CMD23's argument. This will block the reliable
  180. * write operation in RPMB, because RPMB reliable write need to set the bit31
  181. * of the CMD23's argument.
  182. * imx6qpdl/imx6sx/imx6sl/imx7d has this limitation only for ADMA mode, SDMA
  183. * do not has this limitation. so when these SoC use ADMA mode, it need to
  184. * disable the ACMD23 feature.
  185. */
  186. #define ESDHC_FLAG_BROKEN_AUTO_CMD23 BIT(16)
  187. /* ERR004536 is not applicable for the IP */
  188. #define ESDHC_FLAG_SKIP_ERR004536 BIT(17)
  189. enum wp_types {
  190. ESDHC_WP_NONE, /* no WP, neither controller nor gpio */
  191. ESDHC_WP_CONTROLLER, /* mmc controller internal WP */
  192. ESDHC_WP_GPIO, /* external gpio pin for WP */
  193. };
  194. enum cd_types {
  195. ESDHC_CD_NONE, /* no CD, neither controller nor gpio */
  196. ESDHC_CD_CONTROLLER, /* mmc controller internal CD */
  197. ESDHC_CD_GPIO, /* external gpio pin for CD */
  198. ESDHC_CD_PERMANENT, /* no CD, card permanently wired to host */
  199. };
  200. /*
  201. * struct esdhc_platform_data - platform data for esdhc on i.MX
  202. *
  203. * ESDHC_WP(CD)_CONTROLLER type is not available on i.MX25/35.
  204. *
  205. * @wp_type: type of write_protect method (see wp_types enum above)
  206. * @cd_type: type of card_detect method (see cd_types enum above)
  207. */
  208. struct esdhc_platform_data {
  209. enum wp_types wp_type;
  210. enum cd_types cd_type;
  211. int max_bus_width;
  212. unsigned int delay_line;
  213. unsigned int tuning_step; /* The delay cell steps in tuning procedure */
  214. unsigned int tuning_start_tap; /* The start delay cell point in tuning procedure */
  215. unsigned int strobe_dll_delay_target; /* The delay cell for strobe pad (read clock) */
  216. };
  217. struct esdhc_soc_data {
  218. u32 flags;
  219. };
  220. static const struct esdhc_soc_data esdhc_imx25_data = {
  221. .flags = ESDHC_FLAG_ERR004536,
  222. };
  223. static const struct esdhc_soc_data esdhc_imx35_data = {
  224. .flags = ESDHC_FLAG_ERR004536,
  225. };
  226. static const struct esdhc_soc_data esdhc_imx51_data = {
  227. .flags = 0,
  228. };
  229. static const struct esdhc_soc_data esdhc_imx53_data = {
  230. .flags = ESDHC_FLAG_MULTIBLK_NO_INT,
  231. };
  232. static const struct esdhc_soc_data usdhc_imx6q_data = {
  233. .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING
  234. | ESDHC_FLAG_BROKEN_AUTO_CMD23,
  235. };
  236. static const struct esdhc_soc_data usdhc_imx6sl_data = {
  237. .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
  238. | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_ERR004536
  239. | ESDHC_FLAG_HS200
  240. | ESDHC_FLAG_BROKEN_AUTO_CMD23,
  241. };
  242. static const struct esdhc_soc_data usdhc_imx6sll_data = {
  243. .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
  244. | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
  245. | ESDHC_FLAG_HS400
  246. | ESDHC_FLAG_STATE_LOST_IN_LPMODE,
  247. };
  248. static const struct esdhc_soc_data usdhc_imx6sx_data = {
  249. .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
  250. | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
  251. | ESDHC_FLAG_STATE_LOST_IN_LPMODE
  252. | ESDHC_FLAG_BROKEN_AUTO_CMD23,
  253. };
  254. static const struct esdhc_soc_data usdhc_imx6ull_data = {
  255. .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
  256. | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
  257. | ESDHC_FLAG_ERR010450
  258. | ESDHC_FLAG_STATE_LOST_IN_LPMODE,
  259. };
  260. static const struct esdhc_soc_data usdhc_imx7d_data = {
  261. .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
  262. | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
  263. | ESDHC_FLAG_HS400
  264. | ESDHC_FLAG_STATE_LOST_IN_LPMODE
  265. | ESDHC_FLAG_BROKEN_AUTO_CMD23,
  266. };
  267. static struct esdhc_soc_data usdhc_s32g2_data = {
  268. .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING
  269. | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
  270. | ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES
  271. | ESDHC_FLAG_SKIP_ERR004536,
  272. };
  273. static struct esdhc_soc_data usdhc_imx7ulp_data = {
  274. .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
  275. | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
  276. | ESDHC_FLAG_PMQOS | ESDHC_FLAG_HS400
  277. | ESDHC_FLAG_STATE_LOST_IN_LPMODE,
  278. };
  279. static struct esdhc_soc_data usdhc_imxrt1050_data = {
  280. .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_HS200 | ESDHC_FLAG_ERR004536,
  281. };
  282. static struct esdhc_soc_data usdhc_imx8qxp_data = {
  283. .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
  284. | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
  285. | ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES
  286. | ESDHC_FLAG_STATE_LOST_IN_LPMODE
  287. | ESDHC_FLAG_CLK_RATE_LOST_IN_PM_RUNTIME,
  288. };
  289. static struct esdhc_soc_data usdhc_imx8mm_data = {
  290. .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
  291. | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
  292. | ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES
  293. | ESDHC_FLAG_STATE_LOST_IN_LPMODE,
  294. };
  295. struct pltfm_imx_data {
  296. u32 scratchpad;
  297. struct pinctrl *pinctrl;
  298. struct pinctrl_state *pins_100mhz;
  299. struct pinctrl_state *pins_200mhz;
  300. const struct esdhc_soc_data *socdata;
  301. struct esdhc_platform_data boarddata;
  302. struct clk *clk_ipg;
  303. struct clk *clk_ahb;
  304. struct clk *clk_per;
  305. unsigned int actual_clock;
  306. enum {
  307. NO_CMD_PENDING, /* no multiblock command pending */
  308. MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */
  309. WAIT_FOR_INT, /* sent CMD12, waiting for response INT */
  310. } multiblock_status;
  311. u32 is_ddr;
  312. struct pm_qos_request pm_qos_req;
  313. };
  314. static const struct of_device_id imx_esdhc_dt_ids[] = {
  315. { .compatible = "fsl,imx25-esdhc", .data = &esdhc_imx25_data, },
  316. { .compatible = "fsl,imx35-esdhc", .data = &esdhc_imx35_data, },
  317. { .compatible = "fsl,imx51-esdhc", .data = &esdhc_imx51_data, },
  318. { .compatible = "fsl,imx53-esdhc", .data = &esdhc_imx53_data, },
  319. { .compatible = "fsl,imx6sx-usdhc", .data = &usdhc_imx6sx_data, },
  320. { .compatible = "fsl,imx6sl-usdhc", .data = &usdhc_imx6sl_data, },
  321. { .compatible = "fsl,imx6sll-usdhc", .data = &usdhc_imx6sll_data, },
  322. { .compatible = "fsl,imx6q-usdhc", .data = &usdhc_imx6q_data, },
  323. { .compatible = "fsl,imx6ull-usdhc", .data = &usdhc_imx6ull_data, },
  324. { .compatible = "fsl,imx7d-usdhc", .data = &usdhc_imx7d_data, },
  325. { .compatible = "fsl,imx7ulp-usdhc", .data = &usdhc_imx7ulp_data, },
  326. { .compatible = "fsl,imx8qxp-usdhc", .data = &usdhc_imx8qxp_data, },
  327. { .compatible = "fsl,imx8mm-usdhc", .data = &usdhc_imx8mm_data, },
  328. { .compatible = "fsl,imxrt1050-usdhc", .data = &usdhc_imxrt1050_data, },
  329. { .compatible = "nxp,s32g2-usdhc", .data = &usdhc_s32g2_data, },
  330. { /* sentinel */ }
  331. };
  332. MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
  333. static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
  334. {
  335. return data->socdata == &esdhc_imx25_data;
  336. }
  337. static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
  338. {
  339. return data->socdata == &esdhc_imx53_data;
  340. }
  341. static inline int esdhc_is_usdhc(struct pltfm_imx_data *data)
  342. {
  343. return !!(data->socdata->flags & ESDHC_FLAG_USDHC);
  344. }
  345. static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
  346. {
  347. void __iomem *base = host->ioaddr + (reg & ~0x3);
  348. u32 shift = (reg & 0x3) * 8;
  349. writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
  350. }
  351. #define DRIVER_NAME "sdhci-esdhc-imx"
  352. #define ESDHC_IMX_DUMP(f, x...) \
  353. pr_err("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
  354. static void esdhc_dump_debug_regs(struct sdhci_host *host)
  355. {
  356. int i;
  357. char *debug_status[7] = {
  358. "cmd debug status",
  359. "data debug status",
  360. "trans debug status",
  361. "dma debug status",
  362. "adma debug status",
  363. "fifo debug status",
  364. "async fifo debug status"
  365. };
  366. ESDHC_IMX_DUMP("========= ESDHC IMX DEBUG STATUS DUMP =========\n");
  367. for (i = 0; i < 7; i++) {
  368. esdhc_clrset_le(host, ESDHC_DEBUG_SEL_MASK,
  369. ESDHC_DEBUG_SEL_CMD_STATE + i, ESDHC_DEBUG_SEL_REG);
  370. ESDHC_IMX_DUMP("%s: 0x%04x\n", debug_status[i],
  371. readw(host->ioaddr + ESDHC_DEBUG_SEL_AND_STATUS_REG));
  372. }
  373. esdhc_clrset_le(host, ESDHC_DEBUG_SEL_MASK, 0, ESDHC_DEBUG_SEL_REG);
  374. }
  375. static inline void esdhc_wait_for_card_clock_gate_off(struct sdhci_host *host)
  376. {
  377. u32 present_state;
  378. int ret;
  379. ret = readl_poll_timeout(host->ioaddr + ESDHC_PRSSTAT, present_state,
  380. (present_state & ESDHC_CLOCK_GATE_OFF), 2, 100);
  381. if (ret == -ETIMEDOUT)
  382. dev_warn(mmc_dev(host->mmc), "%s: card clock still not gate off in 100us!.\n", __func__);
  383. }
  384. /* Enable the auto tuning circuit to check the CMD line and BUS line */
  385. static inline void usdhc_auto_tuning_mode_sel(struct sdhci_host *host)
  386. {
  387. u32 buswidth, auto_tune_buswidth;
  388. buswidth = USDHC_GET_BUSWIDTH(readl(host->ioaddr + SDHCI_HOST_CONTROL));
  389. switch (buswidth) {
  390. case ESDHC_CTRL_8BITBUS:
  391. auto_tune_buswidth = ESDHC_VEND_SPEC2_AUTO_TUNE_8BIT_EN;
  392. break;
  393. case ESDHC_CTRL_4BITBUS:
  394. auto_tune_buswidth = ESDHC_VEND_SPEC2_AUTO_TUNE_4BIT_EN;
  395. break;
  396. default: /* 1BITBUS */
  397. auto_tune_buswidth = ESDHC_VEND_SPEC2_AUTO_TUNE_1BIT_EN;
  398. break;
  399. }
  400. esdhc_clrset_le(host, ESDHC_VEND_SPEC2_AUTO_TUNE_MODE_MASK,
  401. auto_tune_buswidth | ESDHC_VEND_SPEC2_AUTO_TUNE_CMD_EN,
  402. ESDHC_VEND_SPEC2);
  403. }
  404. static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
  405. {
  406. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  407. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  408. u32 val = readl(host->ioaddr + reg);
  409. if (unlikely(reg == SDHCI_PRESENT_STATE)) {
  410. u32 fsl_prss = val;
  411. /* save the least 20 bits */
  412. val = fsl_prss & 0x000FFFFF;
  413. /* move dat[0-3] bits */
  414. val |= (fsl_prss & 0x0F000000) >> 4;
  415. /* move cmd line bit */
  416. val |= (fsl_prss & 0x00800000) << 1;
  417. }
  418. if (unlikely(reg == SDHCI_CAPABILITIES)) {
  419. /* ignore bit[0-15] as it stores cap_1 register val for mx6sl */
  420. if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
  421. val &= 0xffff0000;
  422. /* In FSL esdhc IC module, only bit20 is used to indicate the
  423. * ADMA2 capability of esdhc, but this bit is messed up on
  424. * some SOCs (e.g. on MX25, MX35 this bit is set, but they
  425. * don't actually support ADMA2). So set the BROKEN_ADMA
  426. * quirk on MX25/35 platforms.
  427. */
  428. if (val & SDHCI_CAN_DO_ADMA1) {
  429. val &= ~SDHCI_CAN_DO_ADMA1;
  430. val |= SDHCI_CAN_DO_ADMA2;
  431. }
  432. }
  433. if (unlikely(reg == SDHCI_CAPABILITIES_1)) {
  434. if (esdhc_is_usdhc(imx_data)) {
  435. if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
  436. val = readl(host->ioaddr + SDHCI_CAPABILITIES) & 0xFFFF;
  437. else
  438. /* imx6q/dl does not have cap_1 register, fake one */
  439. val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104
  440. | SDHCI_SUPPORT_SDR50
  441. | SDHCI_USE_SDR50_TUNING
  442. | FIELD_PREP(SDHCI_RETUNING_MODE_MASK,
  443. SDHCI_TUNING_MODE_3);
  444. /*
  445. * Do not advertise faster UHS modes if there are no
  446. * pinctrl states for 100MHz/200MHz.
  447. */
  448. if (IS_ERR_OR_NULL(imx_data->pins_100mhz))
  449. val &= ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_DDR50);
  450. if (IS_ERR_OR_NULL(imx_data->pins_200mhz))
  451. val &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_HS400);
  452. }
  453. }
  454. if (unlikely(reg == SDHCI_MAX_CURRENT) && esdhc_is_usdhc(imx_data)) {
  455. val = 0;
  456. val |= FIELD_PREP(SDHCI_MAX_CURRENT_330_MASK, 0xFF);
  457. val |= FIELD_PREP(SDHCI_MAX_CURRENT_300_MASK, 0xFF);
  458. val |= FIELD_PREP(SDHCI_MAX_CURRENT_180_MASK, 0xFF);
  459. }
  460. if (unlikely(reg == SDHCI_INT_STATUS)) {
  461. if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
  462. val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
  463. val |= SDHCI_INT_ADMA_ERROR;
  464. }
  465. /*
  466. * mask off the interrupt we get in response to the manually
  467. * sent CMD12
  468. */
  469. if ((imx_data->multiblock_status == WAIT_FOR_INT) &&
  470. ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) {
  471. val &= ~SDHCI_INT_RESPONSE;
  472. writel(SDHCI_INT_RESPONSE, host->ioaddr +
  473. SDHCI_INT_STATUS);
  474. imx_data->multiblock_status = NO_CMD_PENDING;
  475. }
  476. }
  477. return val;
  478. }
  479. static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
  480. {
  481. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  482. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  483. u32 data;
  484. if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE ||
  485. reg == SDHCI_INT_STATUS)) {
  486. if ((val & SDHCI_INT_CARD_INT) && !esdhc_is_usdhc(imx_data)) {
  487. /*
  488. * Clear and then set D3CD bit to avoid missing the
  489. * card interrupt. This is an eSDHC controller problem
  490. * so we need to apply the following workaround: clear
  491. * and set D3CD bit will make eSDHC re-sample the card
  492. * interrupt. In case a card interrupt was lost,
  493. * re-sample it by the following steps.
  494. */
  495. data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
  496. data &= ~ESDHC_CTRL_D3CD;
  497. writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
  498. data |= ESDHC_CTRL_D3CD;
  499. writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
  500. }
  501. if (val & SDHCI_INT_ADMA_ERROR) {
  502. val &= ~SDHCI_INT_ADMA_ERROR;
  503. val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
  504. }
  505. }
  506. if (unlikely((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
  507. && (reg == SDHCI_INT_STATUS)
  508. && (val & SDHCI_INT_DATA_END))) {
  509. u32 v;
  510. v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  511. v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
  512. writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
  513. if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS)
  514. {
  515. /* send a manual CMD12 with RESPTYP=none */
  516. data = MMC_STOP_TRANSMISSION << 24 |
  517. SDHCI_CMD_ABORTCMD << 16;
  518. writel(data, host->ioaddr + SDHCI_TRANSFER_MODE);
  519. imx_data->multiblock_status = WAIT_FOR_INT;
  520. }
  521. }
  522. writel(val, host->ioaddr + reg);
  523. }
  524. static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
  525. {
  526. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  527. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  528. u16 ret = 0;
  529. u32 val;
  530. if (unlikely(reg == SDHCI_HOST_VERSION)) {
  531. reg ^= 2;
  532. if (esdhc_is_usdhc(imx_data)) {
  533. /*
  534. * The usdhc register returns a wrong host version.
  535. * Correct it here.
  536. */
  537. return SDHCI_SPEC_300;
  538. }
  539. }
  540. if (unlikely(reg == SDHCI_HOST_CONTROL2)) {
  541. val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  542. if (val & ESDHC_VENDOR_SPEC_VSELECT)
  543. ret |= SDHCI_CTRL_VDD_180;
  544. if (esdhc_is_usdhc(imx_data)) {
  545. if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
  546. val = readl(host->ioaddr + ESDHC_MIX_CTRL);
  547. else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING)
  548. /* the std tuning bits is in ACMD12_ERR for imx6sl */
  549. val = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
  550. }
  551. if (val & ESDHC_MIX_CTRL_EXE_TUNE)
  552. ret |= SDHCI_CTRL_EXEC_TUNING;
  553. if (val & ESDHC_MIX_CTRL_SMPCLK_SEL)
  554. ret |= SDHCI_CTRL_TUNED_CLK;
  555. ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
  556. return ret;
  557. }
  558. if (unlikely(reg == SDHCI_TRANSFER_MODE)) {
  559. if (esdhc_is_usdhc(imx_data)) {
  560. u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
  561. ret = m & ESDHC_MIX_CTRL_SDHCI_MASK;
  562. /* Swap AC23 bit */
  563. if (m & ESDHC_MIX_CTRL_AC23EN) {
  564. ret &= ~ESDHC_MIX_CTRL_AC23EN;
  565. ret |= SDHCI_TRNS_AUTO_CMD23;
  566. }
  567. } else {
  568. ret = readw(host->ioaddr + SDHCI_TRANSFER_MODE);
  569. }
  570. return ret;
  571. }
  572. return readw(host->ioaddr + reg);
  573. }
  574. static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
  575. {
  576. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  577. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  578. u32 new_val = 0;
  579. switch (reg) {
  580. case SDHCI_CLOCK_CONTROL:
  581. new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  582. if (val & SDHCI_CLOCK_CARD_EN)
  583. new_val |= ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
  584. else
  585. new_val &= ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
  586. writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
  587. if (!(new_val & ESDHC_VENDOR_SPEC_FRC_SDCLK_ON))
  588. esdhc_wait_for_card_clock_gate_off(host);
  589. return;
  590. case SDHCI_HOST_CONTROL2:
  591. new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  592. if (val & SDHCI_CTRL_VDD_180)
  593. new_val |= ESDHC_VENDOR_SPEC_VSELECT;
  594. else
  595. new_val &= ~ESDHC_VENDOR_SPEC_VSELECT;
  596. writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
  597. if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
  598. u32 v = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
  599. u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
  600. if (val & SDHCI_CTRL_TUNED_CLK) {
  601. v |= ESDHC_MIX_CTRL_SMPCLK_SEL;
  602. } else {
  603. v &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
  604. m &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
  605. m &= ~ESDHC_MIX_CTRL_AUTO_TUNE_EN;
  606. }
  607. if (val & SDHCI_CTRL_EXEC_TUNING) {
  608. v |= ESDHC_MIX_CTRL_EXE_TUNE;
  609. m |= ESDHC_MIX_CTRL_FBCLK_SEL;
  610. m |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
  611. usdhc_auto_tuning_mode_sel(host);
  612. } else {
  613. v &= ~ESDHC_MIX_CTRL_EXE_TUNE;
  614. }
  615. writel(v, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
  616. writel(m, host->ioaddr + ESDHC_MIX_CTRL);
  617. }
  618. return;
  619. case SDHCI_TRANSFER_MODE:
  620. if ((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
  621. && (host->cmd->opcode == SD_IO_RW_EXTENDED)
  622. && (host->cmd->data->blocks > 1)
  623. && (host->cmd->data->flags & MMC_DATA_READ)) {
  624. u32 v;
  625. v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  626. v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
  627. writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
  628. }
  629. if (esdhc_is_usdhc(imx_data)) {
  630. u32 wml;
  631. u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
  632. /* Swap AC23 bit */
  633. if (val & SDHCI_TRNS_AUTO_CMD23) {
  634. val &= ~SDHCI_TRNS_AUTO_CMD23;
  635. val |= ESDHC_MIX_CTRL_AC23EN;
  636. }
  637. m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
  638. writel(m, host->ioaddr + ESDHC_MIX_CTRL);
  639. /* Set watermark levels for PIO access to maximum value
  640. * (128 words) to accommodate full 512 bytes buffer.
  641. * For DMA access restore the levels to default value.
  642. */
  643. m = readl(host->ioaddr + ESDHC_WTMK_LVL);
  644. if (val & SDHCI_TRNS_DMA) {
  645. wml = ESDHC_WTMK_LVL_WML_VAL_DEF;
  646. } else {
  647. u8 ctrl;
  648. wml = ESDHC_WTMK_LVL_WML_VAL_MAX;
  649. /*
  650. * Since already disable DMA mode, so also need
  651. * to clear the DMASEL. Otherwise, for standard
  652. * tuning, when send tuning command, usdhc will
  653. * still prefetch the ADMA script from wrong
  654. * DMA address, then we will see IOMMU report
  655. * some error which show lack of TLB mapping.
  656. */
  657. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  658. ctrl &= ~SDHCI_CTRL_DMA_MASK;
  659. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  660. }
  661. m &= ~(ESDHC_WTMK_LVL_RD_WML_MASK |
  662. ESDHC_WTMK_LVL_WR_WML_MASK);
  663. m |= (wml << ESDHC_WTMK_LVL_RD_WML_SHIFT) |
  664. (wml << ESDHC_WTMK_LVL_WR_WML_SHIFT);
  665. writel(m, host->ioaddr + ESDHC_WTMK_LVL);
  666. } else {
  667. /*
  668. * Postpone this write, we must do it together with a
  669. * command write that is down below.
  670. */
  671. imx_data->scratchpad = val;
  672. }
  673. return;
  674. case SDHCI_COMMAND:
  675. if (host->cmd->opcode == MMC_STOP_TRANSMISSION)
  676. val |= SDHCI_CMD_ABORTCMD;
  677. if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
  678. (imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
  679. imx_data->multiblock_status = MULTIBLK_IN_PROCESS;
  680. if (esdhc_is_usdhc(imx_data))
  681. writel(val << 16,
  682. host->ioaddr + SDHCI_TRANSFER_MODE);
  683. else
  684. writel(val << 16 | imx_data->scratchpad,
  685. host->ioaddr + SDHCI_TRANSFER_MODE);
  686. return;
  687. case SDHCI_BLOCK_SIZE:
  688. val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
  689. break;
  690. }
  691. esdhc_clrset_le(host, 0xffff, val, reg);
  692. }
  693. static u8 esdhc_readb_le(struct sdhci_host *host, int reg)
  694. {
  695. u8 ret;
  696. u32 val;
  697. switch (reg) {
  698. case SDHCI_HOST_CONTROL:
  699. val = readl(host->ioaddr + reg);
  700. ret = val & SDHCI_CTRL_LED;
  701. ret |= (val >> 5) & SDHCI_CTRL_DMA_MASK;
  702. ret |= (val & ESDHC_CTRL_4BITBUS);
  703. ret |= (val & ESDHC_CTRL_8BITBUS) << 3;
  704. return ret;
  705. }
  706. return readb(host->ioaddr + reg);
  707. }
  708. static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
  709. {
  710. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  711. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  712. u32 new_val = 0;
  713. u32 mask;
  714. switch (reg) {
  715. case SDHCI_POWER_CONTROL:
  716. /*
  717. * FSL put some DMA bits here
  718. * If your board has a regulator, code should be here
  719. */
  720. return;
  721. case SDHCI_HOST_CONTROL:
  722. /* FSL messed up here, so we need to manually compose it. */
  723. new_val = val & SDHCI_CTRL_LED;
  724. /* ensure the endianness */
  725. new_val |= ESDHC_HOST_CONTROL_LE;
  726. /* bits 8&9 are reserved on mx25 */
  727. if (!is_imx25_esdhc(imx_data)) {
  728. /* DMA mode bits are shifted */
  729. new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
  730. }
  731. /*
  732. * Do not touch buswidth bits here. This is done in
  733. * esdhc_pltfm_bus_width.
  734. * Do not touch the D3CD bit either which is used for the
  735. * SDIO interrupt erratum workaround.
  736. */
  737. mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD);
  738. esdhc_clrset_le(host, mask, new_val, reg);
  739. return;
  740. case SDHCI_SOFTWARE_RESET:
  741. if (val & SDHCI_RESET_DATA)
  742. new_val = readl(host->ioaddr + SDHCI_HOST_CONTROL);
  743. break;
  744. }
  745. esdhc_clrset_le(host, 0xff, val, reg);
  746. if (reg == SDHCI_SOFTWARE_RESET) {
  747. if (val & SDHCI_RESET_ALL) {
  748. /*
  749. * The esdhc has a design violation to SDHC spec which
  750. * tells that software reset should not affect card
  751. * detection circuit. But esdhc clears its SYSCTL
  752. * register bits [0..2] during the software reset. This
  753. * will stop those clocks that card detection circuit
  754. * relies on. To work around it, we turn the clocks on
  755. * back to keep card detection circuit functional.
  756. */
  757. esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
  758. /*
  759. * The reset on usdhc fails to clear MIX_CTRL register.
  760. * Do it manually here.
  761. */
  762. if (esdhc_is_usdhc(imx_data)) {
  763. /*
  764. * the tuning bits should be kept during reset
  765. */
  766. new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
  767. writel(new_val & ESDHC_MIX_CTRL_TUNING_MASK,
  768. host->ioaddr + ESDHC_MIX_CTRL);
  769. imx_data->is_ddr = 0;
  770. }
  771. } else if (val & SDHCI_RESET_DATA) {
  772. /*
  773. * The eSDHC DAT line software reset clears at least the
  774. * data transfer width on i.MX25, so make sure that the
  775. * Host Control register is unaffected.
  776. */
  777. esdhc_clrset_le(host, 0xff, new_val,
  778. SDHCI_HOST_CONTROL);
  779. }
  780. }
  781. }
  782. static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
  783. {
  784. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  785. return pltfm_host->clock;
  786. }
  787. static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
  788. {
  789. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  790. return pltfm_host->clock / 256 / 16;
  791. }
  792. static inline void esdhc_pltfm_set_clock(struct sdhci_host *host,
  793. unsigned int clock)
  794. {
  795. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  796. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  797. unsigned int host_clock = pltfm_host->clock;
  798. int ddr_pre_div = imx_data->is_ddr ? 2 : 1;
  799. int pre_div = 1;
  800. int div = 1;
  801. int ret;
  802. u32 temp, val;
  803. if (esdhc_is_usdhc(imx_data)) {
  804. val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  805. writel(val & ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
  806. host->ioaddr + ESDHC_VENDOR_SPEC);
  807. esdhc_wait_for_card_clock_gate_off(host);
  808. }
  809. if (clock == 0) {
  810. host->mmc->actual_clock = 0;
  811. return;
  812. }
  813. /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
  814. if (is_imx53_esdhc(imx_data)) {
  815. /*
  816. * According to the i.MX53 reference manual, if DLLCTRL[10] can
  817. * be set, then the controller is eSDHCv3, else it is eSDHCv2.
  818. */
  819. val = readl(host->ioaddr + ESDHC_DLL_CTRL);
  820. writel(val | BIT(10), host->ioaddr + ESDHC_DLL_CTRL);
  821. temp = readl(host->ioaddr + ESDHC_DLL_CTRL);
  822. writel(val, host->ioaddr + ESDHC_DLL_CTRL);
  823. if (temp & BIT(10))
  824. pre_div = 2;
  825. }
  826. temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
  827. temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
  828. | ESDHC_CLOCK_MASK);
  829. sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
  830. if ((imx_data->socdata->flags & ESDHC_FLAG_ERR010450) &&
  831. (!(host->quirks2 & SDHCI_QUIRK2_NO_1_8_V))) {
  832. unsigned int max_clock;
  833. max_clock = imx_data->is_ddr ? 45000000 : 150000000;
  834. clock = min(clock, max_clock);
  835. }
  836. while (host_clock / (16 * pre_div * ddr_pre_div) > clock &&
  837. pre_div < 256)
  838. pre_div *= 2;
  839. while (host_clock / (div * pre_div * ddr_pre_div) > clock && div < 16)
  840. div++;
  841. host->mmc->actual_clock = host_clock / (div * pre_div * ddr_pre_div);
  842. dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
  843. clock, host->mmc->actual_clock);
  844. pre_div >>= 1;
  845. div--;
  846. temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
  847. temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
  848. | (div << ESDHC_DIVIDER_SHIFT)
  849. | (pre_div << ESDHC_PREDIV_SHIFT));
  850. sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
  851. /* need to wait the bit 3 of the PRSSTAT to be set, make sure card clock is stable */
  852. ret = readl_poll_timeout(host->ioaddr + ESDHC_PRSSTAT, temp,
  853. (temp & ESDHC_CLOCK_STABLE), 2, 100);
  854. if (ret == -ETIMEDOUT)
  855. dev_warn(mmc_dev(host->mmc), "card clock still not stable in 100us!.\n");
  856. if (esdhc_is_usdhc(imx_data)) {
  857. val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  858. writel(val | ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
  859. host->ioaddr + ESDHC_VENDOR_SPEC);
  860. }
  861. }
  862. static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
  863. {
  864. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  865. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  866. struct esdhc_platform_data *boarddata = &imx_data->boarddata;
  867. switch (boarddata->wp_type) {
  868. case ESDHC_WP_GPIO:
  869. return mmc_gpio_get_ro(host->mmc);
  870. case ESDHC_WP_CONTROLLER:
  871. return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
  872. SDHCI_WRITE_PROTECT);
  873. case ESDHC_WP_NONE:
  874. break;
  875. }
  876. return -ENOSYS;
  877. }
  878. static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
  879. {
  880. u32 ctrl;
  881. switch (width) {
  882. case MMC_BUS_WIDTH_8:
  883. ctrl = ESDHC_CTRL_8BITBUS;
  884. break;
  885. case MMC_BUS_WIDTH_4:
  886. ctrl = ESDHC_CTRL_4BITBUS;
  887. break;
  888. default:
  889. ctrl = 0;
  890. break;
  891. }
  892. esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
  893. SDHCI_HOST_CONTROL);
  894. }
  895. static int usdhc_execute_tuning(struct mmc_host *mmc, u32 opcode)
  896. {
  897. struct sdhci_host *host = mmc_priv(mmc);
  898. /*
  899. * i.MX uSDHC internally already uses a fixed optimized timing for
  900. * DDR50, normally does not require tuning for DDR50 mode.
  901. */
  902. if (host->timing == MMC_TIMING_UHS_DDR50)
  903. return 0;
  904. return sdhci_execute_tuning(mmc, opcode);
  905. }
  906. static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val)
  907. {
  908. u32 reg;
  909. u8 sw_rst;
  910. int ret;
  911. /* FIXME: delay a bit for card to be ready for next tuning due to errors */
  912. mdelay(1);
  913. /* IC suggest to reset USDHC before every tuning command */
  914. esdhc_clrset_le(host, 0xff, SDHCI_RESET_ALL, SDHCI_SOFTWARE_RESET);
  915. ret = readb_poll_timeout(host->ioaddr + SDHCI_SOFTWARE_RESET, sw_rst,
  916. !(sw_rst & SDHCI_RESET_ALL), 10, 100);
  917. if (ret == -ETIMEDOUT)
  918. dev_warn(mmc_dev(host->mmc),
  919. "warning! RESET_ALL never complete before sending tuning command\n");
  920. reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
  921. reg |= ESDHC_MIX_CTRL_EXE_TUNE | ESDHC_MIX_CTRL_SMPCLK_SEL |
  922. ESDHC_MIX_CTRL_FBCLK_SEL;
  923. writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
  924. writel(val << 8, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
  925. dev_dbg(mmc_dev(host->mmc),
  926. "tuning with delay 0x%x ESDHC_TUNE_CTRL_STATUS 0x%x\n",
  927. val, readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS));
  928. }
  929. static void esdhc_post_tuning(struct sdhci_host *host)
  930. {
  931. u32 reg;
  932. usdhc_auto_tuning_mode_sel(host);
  933. reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
  934. reg &= ~ESDHC_MIX_CTRL_EXE_TUNE;
  935. reg |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
  936. writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
  937. }
  938. static int esdhc_executing_tuning(struct sdhci_host *host, u32 opcode)
  939. {
  940. int min, max, avg, ret;
  941. /* find the mininum delay first which can pass tuning */
  942. min = ESDHC_TUNE_CTRL_MIN;
  943. while (min < ESDHC_TUNE_CTRL_MAX) {
  944. esdhc_prepare_tuning(host, min);
  945. if (!mmc_send_tuning(host->mmc, opcode, NULL))
  946. break;
  947. min += ESDHC_TUNE_CTRL_STEP;
  948. }
  949. /* find the maxinum delay which can not pass tuning */
  950. max = min + ESDHC_TUNE_CTRL_STEP;
  951. while (max < ESDHC_TUNE_CTRL_MAX) {
  952. esdhc_prepare_tuning(host, max);
  953. if (mmc_send_tuning(host->mmc, opcode, NULL)) {
  954. max -= ESDHC_TUNE_CTRL_STEP;
  955. break;
  956. }
  957. max += ESDHC_TUNE_CTRL_STEP;
  958. }
  959. /* use average delay to get the best timing */
  960. avg = (min + max) / 2;
  961. esdhc_prepare_tuning(host, avg);
  962. ret = mmc_send_tuning(host->mmc, opcode, NULL);
  963. esdhc_post_tuning(host);
  964. dev_dbg(mmc_dev(host->mmc), "tuning %s at 0x%x ret %d\n",
  965. ret ? "failed" : "passed", avg, ret);
  966. return ret;
  967. }
  968. static void esdhc_hs400_enhanced_strobe(struct mmc_host *mmc, struct mmc_ios *ios)
  969. {
  970. struct sdhci_host *host = mmc_priv(mmc);
  971. u32 m;
  972. m = readl(host->ioaddr + ESDHC_MIX_CTRL);
  973. if (ios->enhanced_strobe)
  974. m |= ESDHC_MIX_CTRL_HS400_ES_EN;
  975. else
  976. m &= ~ESDHC_MIX_CTRL_HS400_ES_EN;
  977. writel(m, host->ioaddr + ESDHC_MIX_CTRL);
  978. }
  979. static int esdhc_change_pinstate(struct sdhci_host *host,
  980. unsigned int uhs)
  981. {
  982. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  983. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  984. struct pinctrl_state *pinctrl;
  985. dev_dbg(mmc_dev(host->mmc), "change pinctrl state for uhs %d\n", uhs);
  986. if (IS_ERR(imx_data->pinctrl) ||
  987. IS_ERR(imx_data->pins_100mhz) ||
  988. IS_ERR(imx_data->pins_200mhz))
  989. return -EINVAL;
  990. switch (uhs) {
  991. case MMC_TIMING_UHS_SDR50:
  992. case MMC_TIMING_UHS_DDR50:
  993. pinctrl = imx_data->pins_100mhz;
  994. break;
  995. case MMC_TIMING_UHS_SDR104:
  996. case MMC_TIMING_MMC_HS200:
  997. case MMC_TIMING_MMC_HS400:
  998. pinctrl = imx_data->pins_200mhz;
  999. break;
  1000. default:
  1001. /* back to default state for other legacy timing */
  1002. return pinctrl_select_default_state(mmc_dev(host->mmc));
  1003. }
  1004. return pinctrl_select_state(imx_data->pinctrl, pinctrl);
  1005. }
  1006. /*
  1007. * For HS400 eMMC, there is a data_strobe line. This signal is generated
  1008. * by the device and used for data output and CRC status response output
  1009. * in HS400 mode. The frequency of this signal follows the frequency of
  1010. * CLK generated by host. The host receives the data which is aligned to the
  1011. * edge of data_strobe line. Due to the time delay between CLK line and
  1012. * data_strobe line, if the delay time is larger than one clock cycle,
  1013. * then CLK and data_strobe line will be misaligned, read error shows up.
  1014. */
  1015. static void esdhc_set_strobe_dll(struct sdhci_host *host)
  1016. {
  1017. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1018. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  1019. u32 strobe_delay;
  1020. u32 v;
  1021. int ret;
  1022. /* disable clock before enabling strobe dll */
  1023. writel(readl(host->ioaddr + ESDHC_VENDOR_SPEC) &
  1024. ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
  1025. host->ioaddr + ESDHC_VENDOR_SPEC);
  1026. esdhc_wait_for_card_clock_gate_off(host);
  1027. /* force a reset on strobe dll */
  1028. writel(ESDHC_STROBE_DLL_CTRL_RESET,
  1029. host->ioaddr + ESDHC_STROBE_DLL_CTRL);
  1030. /* clear the reset bit on strobe dll before any setting */
  1031. writel(0, host->ioaddr + ESDHC_STROBE_DLL_CTRL);
  1032. /*
  1033. * enable strobe dll ctrl and adjust the delay target
  1034. * for the uSDHC loopback read clock
  1035. */
  1036. if (imx_data->boarddata.strobe_dll_delay_target)
  1037. strobe_delay = imx_data->boarddata.strobe_dll_delay_target;
  1038. else
  1039. strobe_delay = ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT;
  1040. v = ESDHC_STROBE_DLL_CTRL_ENABLE |
  1041. ESDHC_STROBE_DLL_CTRL_SLV_UPDATE_INT_DEFAULT |
  1042. (strobe_delay << ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
  1043. writel(v, host->ioaddr + ESDHC_STROBE_DLL_CTRL);
  1044. /* wait max 50us to get the REF/SLV lock */
  1045. ret = readl_poll_timeout(host->ioaddr + ESDHC_STROBE_DLL_STATUS, v,
  1046. ((v & ESDHC_STROBE_DLL_STS_REF_LOCK) && (v & ESDHC_STROBE_DLL_STS_SLV_LOCK)), 1, 50);
  1047. if (ret == -ETIMEDOUT)
  1048. dev_warn(mmc_dev(host->mmc),
  1049. "warning! HS400 strobe DLL status REF/SLV not lock in 50us, STROBE DLL status is %x!\n", v);
  1050. }
  1051. static void esdhc_reset_tuning(struct sdhci_host *host)
  1052. {
  1053. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1054. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  1055. u32 ctrl;
  1056. int ret;
  1057. /* Reset the tuning circuit */
  1058. if (esdhc_is_usdhc(imx_data)) {
  1059. if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
  1060. ctrl = readl(host->ioaddr + ESDHC_MIX_CTRL);
  1061. ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
  1062. ctrl &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
  1063. writel(ctrl, host->ioaddr + ESDHC_MIX_CTRL);
  1064. writel(0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
  1065. } else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
  1066. ctrl = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
  1067. ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
  1068. ctrl &= ~ESDHC_MIX_CTRL_EXE_TUNE;
  1069. writel(ctrl, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
  1070. /* Make sure ESDHC_MIX_CTRL_EXE_TUNE cleared */
  1071. ret = readl_poll_timeout(host->ioaddr + SDHCI_AUTO_CMD_STATUS,
  1072. ctrl, !(ctrl & ESDHC_MIX_CTRL_EXE_TUNE), 1, 50);
  1073. if (ret == -ETIMEDOUT)
  1074. dev_warn(mmc_dev(host->mmc),
  1075. "Warning! clear execute tuning bit failed\n");
  1076. /*
  1077. * SDHCI_INT_DATA_AVAIL is W1C bit, set this bit will clear the
  1078. * usdhc IP internal logic flag execute_tuning_with_clr_buf, which
  1079. * will finally make sure the normal data transfer logic correct.
  1080. */
  1081. ctrl = readl(host->ioaddr + SDHCI_INT_STATUS);
  1082. ctrl |= SDHCI_INT_DATA_AVAIL;
  1083. writel(ctrl, host->ioaddr + SDHCI_INT_STATUS);
  1084. }
  1085. }
  1086. }
  1087. static void esdhc_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
  1088. {
  1089. u32 m;
  1090. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1091. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  1092. struct esdhc_platform_data *boarddata = &imx_data->boarddata;
  1093. /* disable ddr mode and disable HS400 mode */
  1094. m = readl(host->ioaddr + ESDHC_MIX_CTRL);
  1095. m &= ~(ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN);
  1096. imx_data->is_ddr = 0;
  1097. switch (timing) {
  1098. case MMC_TIMING_UHS_SDR12:
  1099. case MMC_TIMING_UHS_SDR25:
  1100. case MMC_TIMING_UHS_SDR50:
  1101. case MMC_TIMING_UHS_SDR104:
  1102. case MMC_TIMING_MMC_HS:
  1103. case MMC_TIMING_MMC_HS200:
  1104. writel(m, host->ioaddr + ESDHC_MIX_CTRL);
  1105. break;
  1106. case MMC_TIMING_UHS_DDR50:
  1107. case MMC_TIMING_MMC_DDR52:
  1108. m |= ESDHC_MIX_CTRL_DDREN;
  1109. writel(m, host->ioaddr + ESDHC_MIX_CTRL);
  1110. imx_data->is_ddr = 1;
  1111. if (boarddata->delay_line) {
  1112. u32 v;
  1113. v = boarddata->delay_line <<
  1114. ESDHC_DLL_OVERRIDE_VAL_SHIFT |
  1115. (1 << ESDHC_DLL_OVERRIDE_EN_SHIFT);
  1116. if (is_imx53_esdhc(imx_data))
  1117. v <<= 1;
  1118. writel(v, host->ioaddr + ESDHC_DLL_CTRL);
  1119. }
  1120. break;
  1121. case MMC_TIMING_MMC_HS400:
  1122. m |= ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN;
  1123. writel(m, host->ioaddr + ESDHC_MIX_CTRL);
  1124. imx_data->is_ddr = 1;
  1125. /* update clock after enable DDR for strobe DLL lock */
  1126. host->ops->set_clock(host, host->clock);
  1127. esdhc_set_strobe_dll(host);
  1128. break;
  1129. case MMC_TIMING_LEGACY:
  1130. default:
  1131. esdhc_reset_tuning(host);
  1132. break;
  1133. }
  1134. esdhc_change_pinstate(host, timing);
  1135. }
  1136. static void esdhc_reset(struct sdhci_host *host, u8 mask)
  1137. {
  1138. sdhci_and_cqhci_reset(host, mask);
  1139. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  1140. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  1141. }
  1142. static unsigned int esdhc_get_max_timeout_count(struct sdhci_host *host)
  1143. {
  1144. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1145. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  1146. /* Doc Erratum: the uSDHC actual maximum timeout count is 1 << 29 */
  1147. return esdhc_is_usdhc(imx_data) ? 1 << 29 : 1 << 27;
  1148. }
  1149. static void esdhc_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
  1150. {
  1151. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1152. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  1153. /* use maximum timeout counter */
  1154. esdhc_clrset_le(host, ESDHC_SYS_CTRL_DTOCV_MASK,
  1155. esdhc_is_usdhc(imx_data) ? 0xF : 0xE,
  1156. SDHCI_TIMEOUT_CONTROL);
  1157. }
  1158. static u32 esdhc_cqhci_irq(struct sdhci_host *host, u32 intmask)
  1159. {
  1160. int cmd_error = 0;
  1161. int data_error = 0;
  1162. if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
  1163. return intmask;
  1164. cqhci_irq(host->mmc, intmask, cmd_error, data_error);
  1165. return 0;
  1166. }
  1167. static struct sdhci_ops sdhci_esdhc_ops = {
  1168. .read_l = esdhc_readl_le,
  1169. .read_w = esdhc_readw_le,
  1170. .read_b = esdhc_readb_le,
  1171. .write_l = esdhc_writel_le,
  1172. .write_w = esdhc_writew_le,
  1173. .write_b = esdhc_writeb_le,
  1174. .set_clock = esdhc_pltfm_set_clock,
  1175. .get_max_clock = esdhc_pltfm_get_max_clock,
  1176. .get_min_clock = esdhc_pltfm_get_min_clock,
  1177. .get_max_timeout_count = esdhc_get_max_timeout_count,
  1178. .get_ro = esdhc_pltfm_get_ro,
  1179. .set_timeout = esdhc_set_timeout,
  1180. .set_bus_width = esdhc_pltfm_set_bus_width,
  1181. .set_uhs_signaling = esdhc_set_uhs_signaling,
  1182. .reset = esdhc_reset,
  1183. .irq = esdhc_cqhci_irq,
  1184. .dump_vendor_regs = esdhc_dump_debug_regs,
  1185. };
  1186. static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
  1187. .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
  1188. | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
  1189. | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
  1190. | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
  1191. .ops = &sdhci_esdhc_ops,
  1192. };
  1193. static void sdhci_esdhc_imx_hwinit(struct sdhci_host *host)
  1194. {
  1195. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1196. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  1197. struct cqhci_host *cq_host = host->mmc->cqe_private;
  1198. u32 tmp;
  1199. if (esdhc_is_usdhc(imx_data)) {
  1200. /*
  1201. * The imx6q ROM code will change the default watermark
  1202. * level setting to something insane. Change it back here.
  1203. */
  1204. writel(ESDHC_WTMK_DEFAULT_VAL, host->ioaddr + ESDHC_WTMK_LVL);
  1205. /*
  1206. * ROM code will change the bit burst_length_enable setting
  1207. * to zero if this usdhc is chosen to boot system. Change
  1208. * it back here, otherwise it will impact the performance a
  1209. * lot. This bit is used to enable/disable the burst length
  1210. * for the external AHB2AXI bridge. It's useful especially
  1211. * for INCR transfer because without burst length indicator,
  1212. * the AHB2AXI bridge does not know the burst length in
  1213. * advance. And without burst length indicator, AHB INCR
  1214. * transfer can only be converted to singles on the AXI side.
  1215. */
  1216. writel(readl(host->ioaddr + SDHCI_HOST_CONTROL)
  1217. | ESDHC_BURST_LEN_EN_INCR,
  1218. host->ioaddr + SDHCI_HOST_CONTROL);
  1219. /*
  1220. * erratum ESDHC_FLAG_ERR004536 fix for MX6Q TO1.2 and MX6DL
  1221. * TO1.1, it's harmless for MX6SL
  1222. */
  1223. if (!(imx_data->socdata->flags & ESDHC_FLAG_SKIP_ERR004536)) {
  1224. writel(readl(host->ioaddr + 0x6c) & ~BIT(7),
  1225. host->ioaddr + 0x6c);
  1226. }
  1227. /* disable DLL_CTRL delay line settings */
  1228. writel(0x0, host->ioaddr + ESDHC_DLL_CTRL);
  1229. /*
  1230. * For the case of command with busy, if set the bit
  1231. * ESDHC_VEND_SPEC2_EN_BUSY_IRQ, USDHC will generate a
  1232. * transfer complete interrupt when busy is deasserted.
  1233. * When CQHCI use DCMD to send a CMD need R1b respons,
  1234. * CQHCI require to set ESDHC_VEND_SPEC2_EN_BUSY_IRQ,
  1235. * otherwise DCMD will always meet timeout waiting for
  1236. * hardware interrupt issue.
  1237. */
  1238. if (imx_data->socdata->flags & ESDHC_FLAG_CQHCI) {
  1239. tmp = readl(host->ioaddr + ESDHC_VEND_SPEC2);
  1240. tmp |= ESDHC_VEND_SPEC2_EN_BUSY_IRQ;
  1241. writel(tmp, host->ioaddr + ESDHC_VEND_SPEC2);
  1242. host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ;
  1243. }
  1244. if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
  1245. tmp = readl(host->ioaddr + ESDHC_TUNING_CTRL);
  1246. tmp |= ESDHC_STD_TUNING_EN;
  1247. /*
  1248. * ROM code or bootloader may config the start tap
  1249. * and step, unmask them first.
  1250. */
  1251. tmp &= ~(ESDHC_TUNING_START_TAP_MASK | ESDHC_TUNING_STEP_MASK);
  1252. if (imx_data->boarddata.tuning_start_tap)
  1253. tmp |= imx_data->boarddata.tuning_start_tap;
  1254. else
  1255. tmp |= ESDHC_TUNING_START_TAP_DEFAULT;
  1256. if (imx_data->boarddata.tuning_step) {
  1257. tmp |= imx_data->boarddata.tuning_step
  1258. << ESDHC_TUNING_STEP_SHIFT;
  1259. } else {
  1260. tmp |= ESDHC_TUNING_STEP_DEFAULT
  1261. << ESDHC_TUNING_STEP_SHIFT;
  1262. }
  1263. /* Disable the CMD CRC check for tuning, if not, need to
  1264. * add some delay after every tuning command, because
  1265. * hardware standard tuning logic will directly go to next
  1266. * step once it detect the CMD CRC error, will not wait for
  1267. * the card side to finally send out the tuning data, trigger
  1268. * the buffer read ready interrupt immediately. If usdhc send
  1269. * the next tuning command some eMMC card will stuck, can't
  1270. * response, block the tuning procedure or the first command
  1271. * after the whole tuning procedure always can't get any response.
  1272. */
  1273. tmp |= ESDHC_TUNING_CMD_CRC_CHECK_DISABLE;
  1274. writel(tmp, host->ioaddr + ESDHC_TUNING_CTRL);
  1275. } else if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
  1276. /*
  1277. * ESDHC_STD_TUNING_EN may be configed in bootloader
  1278. * or ROM code, so clear this bit here to make sure
  1279. * the manual tuning can work.
  1280. */
  1281. tmp = readl(host->ioaddr + ESDHC_TUNING_CTRL);
  1282. tmp &= ~ESDHC_STD_TUNING_EN;
  1283. writel(tmp, host->ioaddr + ESDHC_TUNING_CTRL);
  1284. }
  1285. /*
  1286. * On i.MX8MM, we are running Dual Linux OS, with 1st Linux using SD Card
  1287. * as rootfs storage, 2nd Linux using eMMC as rootfs storage. We let the
  1288. * the 1st linux configure power/clock for the 2nd Linux.
  1289. *
  1290. * When the 2nd Linux is booting into rootfs stage, we let the 1st Linux
  1291. * to destroy the 2nd linux, then restart the 2nd linux, we met SDHCI dump.
  1292. * After we clear the pending interrupt and halt CQCTL, issue gone.
  1293. */
  1294. if (cq_host) {
  1295. tmp = cqhci_readl(cq_host, CQHCI_IS);
  1296. cqhci_writel(cq_host, tmp, CQHCI_IS);
  1297. cqhci_writel(cq_host, CQHCI_HALT, CQHCI_CTL);
  1298. }
  1299. }
  1300. }
  1301. static void esdhc_cqe_enable(struct mmc_host *mmc)
  1302. {
  1303. struct sdhci_host *host = mmc_priv(mmc);
  1304. struct cqhci_host *cq_host = mmc->cqe_private;
  1305. u32 reg;
  1306. u16 mode;
  1307. int count = 10;
  1308. /*
  1309. * CQE gets stuck if it sees Buffer Read Enable bit set, which can be
  1310. * the case after tuning, so ensure the buffer is drained.
  1311. */
  1312. reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
  1313. while (reg & SDHCI_DATA_AVAILABLE) {
  1314. sdhci_readl(host, SDHCI_BUFFER);
  1315. reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
  1316. if (count-- == 0) {
  1317. dev_warn(mmc_dev(host->mmc),
  1318. "CQE may get stuck because the Buffer Read Enable bit is set\n");
  1319. break;
  1320. }
  1321. mdelay(1);
  1322. }
  1323. /*
  1324. * Runtime resume will reset the entire host controller, which
  1325. * will also clear the DMAEN/BCEN of register ESDHC_MIX_CTRL.
  1326. * Here set DMAEN and BCEN when enable CMDQ.
  1327. */
  1328. mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
  1329. if (host->flags & SDHCI_REQ_USE_DMA)
  1330. mode |= SDHCI_TRNS_DMA;
  1331. if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
  1332. mode |= SDHCI_TRNS_BLK_CNT_EN;
  1333. sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
  1334. /*
  1335. * Though Runtime resume reset the entire host controller,
  1336. * but do not impact the CQHCI side, need to clear the
  1337. * HALT bit, avoid CQHCI stuck in the first request when
  1338. * system resume back.
  1339. */
  1340. cqhci_writel(cq_host, 0, CQHCI_CTL);
  1341. if (cqhci_readl(cq_host, CQHCI_CTL) & CQHCI_HALT)
  1342. dev_err(mmc_dev(host->mmc),
  1343. "failed to exit halt state when enable CQE\n");
  1344. sdhci_cqe_enable(mmc);
  1345. }
  1346. static void esdhc_sdhci_dumpregs(struct mmc_host *mmc)
  1347. {
  1348. sdhci_dumpregs(mmc_priv(mmc));
  1349. }
  1350. static const struct cqhci_host_ops esdhc_cqhci_ops = {
  1351. .enable = esdhc_cqe_enable,
  1352. .disable = sdhci_cqe_disable,
  1353. .dumpregs = esdhc_sdhci_dumpregs,
  1354. };
  1355. static int
  1356. sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
  1357. struct sdhci_host *host,
  1358. struct pltfm_imx_data *imx_data)
  1359. {
  1360. struct device_node *np = pdev->dev.of_node;
  1361. struct esdhc_platform_data *boarddata = &imx_data->boarddata;
  1362. int ret;
  1363. if (of_get_property(np, "fsl,wp-controller", NULL))
  1364. boarddata->wp_type = ESDHC_WP_CONTROLLER;
  1365. /*
  1366. * If we have this property, then activate WP check.
  1367. * Retrieveing and requesting the actual WP GPIO will happen
  1368. * in the call to mmc_of_parse().
  1369. */
  1370. if (of_property_read_bool(np, "wp-gpios"))
  1371. boarddata->wp_type = ESDHC_WP_GPIO;
  1372. of_property_read_u32(np, "fsl,tuning-step", &boarddata->tuning_step);
  1373. of_property_read_u32(np, "fsl,tuning-start-tap",
  1374. &boarddata->tuning_start_tap);
  1375. of_property_read_u32(np, "fsl,strobe-dll-delay-target",
  1376. &boarddata->strobe_dll_delay_target);
  1377. if (of_find_property(np, "no-1-8-v", NULL))
  1378. host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
  1379. if (of_property_read_u32(np, "fsl,delay-line", &boarddata->delay_line))
  1380. boarddata->delay_line = 0;
  1381. mmc_of_parse_voltage(host->mmc, &host->ocr_mask);
  1382. if (esdhc_is_usdhc(imx_data) && !IS_ERR(imx_data->pinctrl)) {
  1383. imx_data->pins_100mhz = pinctrl_lookup_state(imx_data->pinctrl,
  1384. ESDHC_PINCTRL_STATE_100MHZ);
  1385. imx_data->pins_200mhz = pinctrl_lookup_state(imx_data->pinctrl,
  1386. ESDHC_PINCTRL_STATE_200MHZ);
  1387. }
  1388. /* call to generic mmc_of_parse to support additional capabilities */
  1389. ret = mmc_of_parse(host->mmc);
  1390. if (ret)
  1391. return ret;
  1392. /* HS400/HS400ES require 8 bit bus */
  1393. if (!(host->mmc->caps & MMC_CAP_8_BIT_DATA))
  1394. host->mmc->caps2 &= ~(MMC_CAP2_HS400 | MMC_CAP2_HS400_ES);
  1395. if (mmc_gpio_get_cd(host->mmc) >= 0)
  1396. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  1397. return 0;
  1398. }
  1399. static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
  1400. {
  1401. struct sdhci_pltfm_host *pltfm_host;
  1402. struct sdhci_host *host;
  1403. struct cqhci_host *cq_host;
  1404. int err;
  1405. struct pltfm_imx_data *imx_data;
  1406. host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata,
  1407. sizeof(*imx_data));
  1408. if (IS_ERR(host))
  1409. return PTR_ERR(host);
  1410. pltfm_host = sdhci_priv(host);
  1411. imx_data = sdhci_pltfm_priv(pltfm_host);
  1412. imx_data->socdata = device_get_match_data(&pdev->dev);
  1413. if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
  1414. cpu_latency_qos_add_request(&imx_data->pm_qos_req, 0);
  1415. imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  1416. if (IS_ERR(imx_data->clk_ipg)) {
  1417. err = PTR_ERR(imx_data->clk_ipg);
  1418. goto free_sdhci;
  1419. }
  1420. imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  1421. if (IS_ERR(imx_data->clk_ahb)) {
  1422. err = PTR_ERR(imx_data->clk_ahb);
  1423. goto free_sdhci;
  1424. }
  1425. imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
  1426. if (IS_ERR(imx_data->clk_per)) {
  1427. err = PTR_ERR(imx_data->clk_per);
  1428. goto free_sdhci;
  1429. }
  1430. pltfm_host->clk = imx_data->clk_per;
  1431. pltfm_host->clock = clk_get_rate(pltfm_host->clk);
  1432. err = clk_prepare_enable(imx_data->clk_per);
  1433. if (err)
  1434. goto free_sdhci;
  1435. err = clk_prepare_enable(imx_data->clk_ipg);
  1436. if (err)
  1437. goto disable_per_clk;
  1438. err = clk_prepare_enable(imx_data->clk_ahb);
  1439. if (err)
  1440. goto disable_ipg_clk;
  1441. imx_data->pinctrl = devm_pinctrl_get(&pdev->dev);
  1442. if (IS_ERR(imx_data->pinctrl))
  1443. dev_warn(mmc_dev(host->mmc), "could not get pinctrl\n");
  1444. if (esdhc_is_usdhc(imx_data)) {
  1445. host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
  1446. host->mmc->caps |= MMC_CAP_1_8V_DDR | MMC_CAP_3_3V_DDR;
  1447. /* GPIO CD can be set as a wakeup source */
  1448. host->mmc->caps |= MMC_CAP_CD_WAKE;
  1449. if (!(imx_data->socdata->flags & ESDHC_FLAG_HS200))
  1450. host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
  1451. /* clear tuning bits in case ROM has set it already */
  1452. writel(0x0, host->ioaddr + ESDHC_MIX_CTRL);
  1453. writel(0x0, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
  1454. writel(0x0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
  1455. /*
  1456. * Link usdhc specific mmc_host_ops execute_tuning function,
  1457. * to replace the standard one in sdhci_ops.
  1458. */
  1459. host->mmc_host_ops.execute_tuning = usdhc_execute_tuning;
  1460. }
  1461. if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
  1462. sdhci_esdhc_ops.platform_execute_tuning =
  1463. esdhc_executing_tuning;
  1464. if (imx_data->socdata->flags & ESDHC_FLAG_ERR004536)
  1465. host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
  1466. if (imx_data->socdata->flags & ESDHC_FLAG_HS400)
  1467. host->mmc->caps2 |= MMC_CAP2_HS400;
  1468. if (imx_data->socdata->flags & ESDHC_FLAG_BROKEN_AUTO_CMD23)
  1469. host->quirks2 |= SDHCI_QUIRK2_ACMD23_BROKEN;
  1470. if (imx_data->socdata->flags & ESDHC_FLAG_HS400_ES) {
  1471. host->mmc->caps2 |= MMC_CAP2_HS400_ES;
  1472. host->mmc_host_ops.hs400_enhanced_strobe =
  1473. esdhc_hs400_enhanced_strobe;
  1474. }
  1475. if (imx_data->socdata->flags & ESDHC_FLAG_CQHCI) {
  1476. host->mmc->caps2 |= MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD;
  1477. cq_host = devm_kzalloc(&pdev->dev, sizeof(*cq_host), GFP_KERNEL);
  1478. if (!cq_host) {
  1479. err = -ENOMEM;
  1480. goto disable_ahb_clk;
  1481. }
  1482. cq_host->mmio = host->ioaddr + ESDHC_CQHCI_ADDR_OFFSET;
  1483. cq_host->ops = &esdhc_cqhci_ops;
  1484. err = cqhci_init(cq_host, host->mmc, false);
  1485. if (err)
  1486. goto disable_ahb_clk;
  1487. }
  1488. err = sdhci_esdhc_imx_probe_dt(pdev, host, imx_data);
  1489. if (err)
  1490. goto disable_ahb_clk;
  1491. sdhci_esdhc_imx_hwinit(host);
  1492. err = sdhci_add_host(host);
  1493. if (err)
  1494. goto disable_ahb_clk;
  1495. /*
  1496. * Setup the wakeup capability here, let user to decide
  1497. * whether need to enable this wakeup through sysfs interface.
  1498. */
  1499. if ((host->mmc->pm_caps & MMC_PM_KEEP_POWER) &&
  1500. (host->mmc->pm_caps & MMC_PM_WAKE_SDIO_IRQ))
  1501. device_set_wakeup_capable(&pdev->dev, true);
  1502. pm_runtime_set_active(&pdev->dev);
  1503. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  1504. pm_runtime_use_autosuspend(&pdev->dev);
  1505. pm_suspend_ignore_children(&pdev->dev, 1);
  1506. pm_runtime_enable(&pdev->dev);
  1507. return 0;
  1508. disable_ahb_clk:
  1509. clk_disable_unprepare(imx_data->clk_ahb);
  1510. disable_ipg_clk:
  1511. clk_disable_unprepare(imx_data->clk_ipg);
  1512. disable_per_clk:
  1513. clk_disable_unprepare(imx_data->clk_per);
  1514. free_sdhci:
  1515. if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
  1516. cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
  1517. sdhci_pltfm_free(pdev);
  1518. return err;
  1519. }
  1520. static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
  1521. {
  1522. struct sdhci_host *host = platform_get_drvdata(pdev);
  1523. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1524. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  1525. int dead;
  1526. pm_runtime_get_sync(&pdev->dev);
  1527. dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
  1528. pm_runtime_disable(&pdev->dev);
  1529. pm_runtime_put_noidle(&pdev->dev);
  1530. sdhci_remove_host(host, dead);
  1531. clk_disable_unprepare(imx_data->clk_per);
  1532. clk_disable_unprepare(imx_data->clk_ipg);
  1533. clk_disable_unprepare(imx_data->clk_ahb);
  1534. if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
  1535. cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
  1536. sdhci_pltfm_free(pdev);
  1537. return 0;
  1538. }
  1539. #ifdef CONFIG_PM_SLEEP
  1540. static int sdhci_esdhc_suspend(struct device *dev)
  1541. {
  1542. struct sdhci_host *host = dev_get_drvdata(dev);
  1543. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1544. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  1545. int ret;
  1546. if (host->mmc->caps2 & MMC_CAP2_CQE) {
  1547. ret = cqhci_suspend(host->mmc);
  1548. if (ret)
  1549. return ret;
  1550. }
  1551. if ((imx_data->socdata->flags & ESDHC_FLAG_STATE_LOST_IN_LPMODE) &&
  1552. (host->tuning_mode != SDHCI_TUNING_MODE_1)) {
  1553. mmc_retune_timer_stop(host->mmc);
  1554. mmc_retune_needed(host->mmc);
  1555. }
  1556. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  1557. mmc_retune_needed(host->mmc);
  1558. ret = sdhci_suspend_host(host);
  1559. if (ret)
  1560. return ret;
  1561. ret = pinctrl_pm_select_sleep_state(dev);
  1562. if (ret)
  1563. return ret;
  1564. ret = mmc_gpio_set_cd_wake(host->mmc, true);
  1565. return ret;
  1566. }
  1567. static int sdhci_esdhc_resume(struct device *dev)
  1568. {
  1569. struct sdhci_host *host = dev_get_drvdata(dev);
  1570. int ret;
  1571. ret = pinctrl_pm_select_default_state(dev);
  1572. if (ret)
  1573. return ret;
  1574. /* re-initialize hw state in case it's lost in low power mode */
  1575. sdhci_esdhc_imx_hwinit(host);
  1576. ret = sdhci_resume_host(host);
  1577. if (ret)
  1578. return ret;
  1579. if (host->mmc->caps2 & MMC_CAP2_CQE)
  1580. ret = cqhci_resume(host->mmc);
  1581. if (!ret)
  1582. ret = mmc_gpio_set_cd_wake(host->mmc, false);
  1583. return ret;
  1584. }
  1585. #endif
  1586. #ifdef CONFIG_PM
  1587. static int sdhci_esdhc_runtime_suspend(struct device *dev)
  1588. {
  1589. struct sdhci_host *host = dev_get_drvdata(dev);
  1590. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1591. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  1592. int ret;
  1593. if (host->mmc->caps2 & MMC_CAP2_CQE) {
  1594. ret = cqhci_suspend(host->mmc);
  1595. if (ret)
  1596. return ret;
  1597. }
  1598. ret = sdhci_runtime_suspend_host(host);
  1599. if (ret)
  1600. return ret;
  1601. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  1602. mmc_retune_needed(host->mmc);
  1603. imx_data->actual_clock = host->mmc->actual_clock;
  1604. esdhc_pltfm_set_clock(host, 0);
  1605. clk_disable_unprepare(imx_data->clk_per);
  1606. clk_disable_unprepare(imx_data->clk_ipg);
  1607. clk_disable_unprepare(imx_data->clk_ahb);
  1608. if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
  1609. cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
  1610. return ret;
  1611. }
  1612. static int sdhci_esdhc_runtime_resume(struct device *dev)
  1613. {
  1614. struct sdhci_host *host = dev_get_drvdata(dev);
  1615. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1616. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  1617. int err;
  1618. if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
  1619. cpu_latency_qos_add_request(&imx_data->pm_qos_req, 0);
  1620. if (imx_data->socdata->flags & ESDHC_FLAG_CLK_RATE_LOST_IN_PM_RUNTIME)
  1621. clk_set_rate(imx_data->clk_per, pltfm_host->clock);
  1622. err = clk_prepare_enable(imx_data->clk_ahb);
  1623. if (err)
  1624. goto remove_pm_qos_request;
  1625. err = clk_prepare_enable(imx_data->clk_per);
  1626. if (err)
  1627. goto disable_ahb_clk;
  1628. err = clk_prepare_enable(imx_data->clk_ipg);
  1629. if (err)
  1630. goto disable_per_clk;
  1631. esdhc_pltfm_set_clock(host, imx_data->actual_clock);
  1632. err = sdhci_runtime_resume_host(host, 0);
  1633. if (err)
  1634. goto disable_ipg_clk;
  1635. if (host->mmc->caps2 & MMC_CAP2_CQE)
  1636. err = cqhci_resume(host->mmc);
  1637. return err;
  1638. disable_ipg_clk:
  1639. clk_disable_unprepare(imx_data->clk_ipg);
  1640. disable_per_clk:
  1641. clk_disable_unprepare(imx_data->clk_per);
  1642. disable_ahb_clk:
  1643. clk_disable_unprepare(imx_data->clk_ahb);
  1644. remove_pm_qos_request:
  1645. if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
  1646. cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
  1647. return err;
  1648. }
  1649. #endif
  1650. static const struct dev_pm_ops sdhci_esdhc_pmops = {
  1651. SET_SYSTEM_SLEEP_PM_OPS(sdhci_esdhc_suspend, sdhci_esdhc_resume)
  1652. SET_RUNTIME_PM_OPS(sdhci_esdhc_runtime_suspend,
  1653. sdhci_esdhc_runtime_resume, NULL)
  1654. };
  1655. static struct platform_driver sdhci_esdhc_imx_driver = {
  1656. .driver = {
  1657. .name = "sdhci-esdhc-imx",
  1658. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1659. .of_match_table = imx_esdhc_dt_ids,
  1660. .pm = &sdhci_esdhc_pmops,
  1661. },
  1662. .probe = sdhci_esdhc_imx_probe,
  1663. .remove = sdhci_esdhc_imx_remove,
  1664. };
  1665. module_platform_driver(sdhci_esdhc_imx_driver);
  1666. MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
  1667. MODULE_AUTHOR("Wolfram Sang <[email protected]>");
  1668. MODULE_LICENSE("GPL v2");