sdhci-esdhc-imx.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  1. /*
  2. * Freescale eSDHC i.MX controller driver for the platform bus.
  3. *
  4. * derived from the OF-version.
  5. *
  6. * Copyright (c) 2010 Pengutronix e.K.
  7. * Author: Wolfram Sang <kernel@pengutronix.de>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License.
  12. */
  13. #include <linux/io.h>
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/clk.h>
  17. #include <linux/gpio.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/mmc/host.h>
  21. #include <linux/mmc/mmc.h>
  22. #include <linux/mmc/sdio.h>
  23. #include <linux/mmc/slot-gpio.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #include <linux/of_gpio.h>
  27. #include <linux/pinctrl/consumer.h>
  28. #include <linux/platform_data/mmc-esdhc-imx.h>
  29. #include <linux/pm_runtime.h>
  30. #include "sdhci-pltfm.h"
  31. #include "sdhci-esdhc.h"
  32. #define ESDHC_SYS_CTRL_DTOCV_MASK 0x0f
  33. #define ESDHC_CTRL_D3CD 0x08
  34. #define ESDHC_BURST_LEN_EN_INCR (1 << 27)
  35. /* VENDOR SPEC register */
  36. #define ESDHC_VENDOR_SPEC 0xc0
  37. #define ESDHC_VENDOR_SPEC_SDIO_QUIRK (1 << 1)
  38. #define ESDHC_VENDOR_SPEC_VSELECT (1 << 1)
  39. #define ESDHC_VENDOR_SPEC_FRC_SDCLK_ON (1 << 8)
  40. #define ESDHC_WTMK_LVL 0x44
  41. #define ESDHC_WTMK_DEFAULT_VAL 0x10401040
  42. #define ESDHC_MIX_CTRL 0x48
  43. #define ESDHC_MIX_CTRL_DDREN (1 << 3)
  44. #define ESDHC_MIX_CTRL_AC23EN (1 << 7)
  45. #define ESDHC_MIX_CTRL_EXE_TUNE (1 << 22)
  46. #define ESDHC_MIX_CTRL_SMPCLK_SEL (1 << 23)
  47. #define ESDHC_MIX_CTRL_AUTO_TUNE_EN (1 << 24)
  48. #define ESDHC_MIX_CTRL_FBCLK_SEL (1 << 25)
  49. #define ESDHC_MIX_CTRL_HS400_EN (1 << 26)
  50. /* Bits 3 and 6 are not SDHCI standard definitions */
  51. #define ESDHC_MIX_CTRL_SDHCI_MASK 0xb7
  52. /* Tuning bits */
  53. #define ESDHC_MIX_CTRL_TUNING_MASK 0x03c00000
  54. /* dll control register */
  55. #define ESDHC_DLL_CTRL 0x60
  56. #define ESDHC_DLL_OVERRIDE_VAL_SHIFT 9
  57. #define ESDHC_DLL_OVERRIDE_EN_SHIFT 8
  58. /* tune control register */
  59. #define ESDHC_TUNE_CTRL_STATUS 0x68
  60. #define ESDHC_TUNE_CTRL_STEP 1
  61. #define ESDHC_TUNE_CTRL_MIN 0
  62. #define ESDHC_TUNE_CTRL_MAX ((1 << 7) - 1)
  63. /* strobe dll register */
  64. #define ESDHC_STROBE_DLL_CTRL 0x70
  65. #define ESDHC_STROBE_DLL_CTRL_ENABLE (1 << 0)
  66. #define ESDHC_STROBE_DLL_CTRL_RESET (1 << 1)
  67. #define ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT 3
  68. #define ESDHC_STROBE_DLL_STATUS 0x74
  69. #define ESDHC_STROBE_DLL_STS_REF_LOCK (1 << 1)
  70. #define ESDHC_STROBE_DLL_STS_SLV_LOCK 0x1
  71. #define ESDHC_TUNING_CTRL 0xcc
  72. #define ESDHC_STD_TUNING_EN (1 << 24)
  73. /* NOTE: the minimum valid tuning start tap for mx6sl is 1 */
  74. #define ESDHC_TUNING_START_TAP_DEFAULT 0x1
  75. #define ESDHC_TUNING_START_TAP_MASK 0xff
  76. #define ESDHC_TUNING_STEP_MASK 0x00070000
  77. #define ESDHC_TUNING_STEP_SHIFT 16
  78. /* pinctrl state */
  79. #define ESDHC_PINCTRL_STATE_100MHZ "state_100mhz"
  80. #define ESDHC_PINCTRL_STATE_200MHZ "state_200mhz"
  81. /*
  82. * Our interpretation of the SDHCI_HOST_CONTROL register
  83. */
  84. #define ESDHC_CTRL_4BITBUS (0x1 << 1)
  85. #define ESDHC_CTRL_8BITBUS (0x2 << 1)
  86. #define ESDHC_CTRL_BUSWIDTH_MASK (0x3 << 1)
  87. /*
  88. * There is an INT DMA ERR mismatch between eSDHC and STD SDHC SPEC:
  89. * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
  90. * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
  91. * Define this macro DMA error INT for fsl eSDHC
  92. */
  93. #define ESDHC_INT_VENDOR_SPEC_DMA_ERR (1 << 28)
  94. /*
  95. * The CMDTYPE of the CMD register (offset 0xE) should be set to
  96. * "11" when the STOP CMD12 is issued on imx53 to abort one
  97. * open ended multi-blk IO. Otherwise the TC INT wouldn't
  98. * be generated.
  99. * In exact block transfer, the controller doesn't complete the
  100. * operations automatically as required at the end of the
  101. * transfer and remains on hold if the abort command is not sent.
  102. * As a result, the TC flag is not asserted and SW received timeout
  103. * exception. Bit1 of Vendor Spec register is used to fix it.
  104. */
  105. #define ESDHC_FLAG_MULTIBLK_NO_INT BIT(1)
  106. /*
  107. * The flag tells that the ESDHC controller is an USDHC block that is
  108. * integrated on the i.MX6 series.
  109. */
  110. #define ESDHC_FLAG_USDHC BIT(3)
  111. /* The IP supports manual tuning process */
  112. #define ESDHC_FLAG_MAN_TUNING BIT(4)
  113. /* The IP supports standard tuning process */
  114. #define ESDHC_FLAG_STD_TUNING BIT(5)
  115. /* The IP has SDHCI_CAPABILITIES_1 register */
  116. #define ESDHC_FLAG_HAVE_CAP1 BIT(6)
  117. /*
  118. * The IP has erratum ERR004536
  119. * uSDHC: ADMA Length Mismatch Error occurs if the AHB read access is slow,
  120. * when reading data from the card
  121. * This flag is also set for i.MX25 and i.MX35 in order to get
  122. * SDHCI_QUIRK_BROKEN_ADMA, but for different reasons (ADMA capability bits).
  123. */
  124. #define ESDHC_FLAG_ERR004536 BIT(7)
  125. /* The IP supports HS200 mode */
  126. #define ESDHC_FLAG_HS200 BIT(8)
  127. /* The IP supports HS400 mode */
  128. #define ESDHC_FLAG_HS400 BIT(9)
  129. /* A clock frequency higher than this rate requires strobe dll control */
  130. #define ESDHC_STROBE_DLL_CLK_FREQ 100000000
  131. struct esdhc_soc_data {
  132. u32 flags;
  133. };
  134. static struct esdhc_soc_data esdhc_imx25_data = {
  135. .flags = ESDHC_FLAG_ERR004536,
  136. };
  137. static struct esdhc_soc_data esdhc_imx35_data = {
  138. .flags = ESDHC_FLAG_ERR004536,
  139. };
  140. static struct esdhc_soc_data esdhc_imx51_data = {
  141. .flags = 0,
  142. };
  143. static struct esdhc_soc_data esdhc_imx53_data = {
  144. .flags = ESDHC_FLAG_MULTIBLK_NO_INT,
  145. };
  146. static struct esdhc_soc_data usdhc_imx6q_data = {
  147. .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING,
  148. };
  149. static struct esdhc_soc_data usdhc_imx6sl_data = {
  150. .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
  151. | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_ERR004536
  152. | ESDHC_FLAG_HS200,
  153. };
  154. static struct esdhc_soc_data usdhc_imx6sx_data = {
  155. .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
  156. | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200,
  157. };
  158. static struct esdhc_soc_data usdhc_imx7d_data = {
  159. .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
  160. | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
  161. | ESDHC_FLAG_HS400,
  162. };
  163. struct pltfm_imx_data {
  164. u32 scratchpad;
  165. struct pinctrl *pinctrl;
  166. struct pinctrl_state *pins_default;
  167. struct pinctrl_state *pins_100mhz;
  168. struct pinctrl_state *pins_200mhz;
  169. const struct esdhc_soc_data *socdata;
  170. struct esdhc_platform_data boarddata;
  171. struct clk *clk_ipg;
  172. struct clk *clk_ahb;
  173. struct clk *clk_per;
  174. enum {
  175. NO_CMD_PENDING, /* no multiblock command pending */
  176. MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */
  177. WAIT_FOR_INT, /* sent CMD12, waiting for response INT */
  178. } multiblock_status;
  179. u32 is_ddr;
  180. };
  181. static const struct platform_device_id imx_esdhc_devtype[] = {
  182. {
  183. .name = "sdhci-esdhc-imx25",
  184. .driver_data = (kernel_ulong_t) &esdhc_imx25_data,
  185. }, {
  186. .name = "sdhci-esdhc-imx35",
  187. .driver_data = (kernel_ulong_t) &esdhc_imx35_data,
  188. }, {
  189. .name = "sdhci-esdhc-imx51",
  190. .driver_data = (kernel_ulong_t) &esdhc_imx51_data,
  191. }, {
  192. /* sentinel */
  193. }
  194. };
  195. MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
  196. static const struct of_device_id imx_esdhc_dt_ids[] = {
  197. { .compatible = "fsl,imx25-esdhc", .data = &esdhc_imx25_data, },
  198. { .compatible = "fsl,imx35-esdhc", .data = &esdhc_imx35_data, },
  199. { .compatible = "fsl,imx51-esdhc", .data = &esdhc_imx51_data, },
  200. { .compatible = "fsl,imx53-esdhc", .data = &esdhc_imx53_data, },
  201. { .compatible = "fsl,imx6sx-usdhc", .data = &usdhc_imx6sx_data, },
  202. { .compatible = "fsl,imx6sl-usdhc", .data = &usdhc_imx6sl_data, },
  203. { .compatible = "fsl,imx6q-usdhc", .data = &usdhc_imx6q_data, },
  204. { .compatible = "fsl,imx7d-usdhc", .data = &usdhc_imx7d_data, },
  205. { /* sentinel */ }
  206. };
  207. MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
  208. static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
  209. {
  210. return data->socdata == &esdhc_imx25_data;
  211. }
  212. static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
  213. {
  214. return data->socdata == &esdhc_imx53_data;
  215. }
  216. static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
  217. {
  218. return data->socdata == &usdhc_imx6q_data;
  219. }
  220. static inline int esdhc_is_usdhc(struct pltfm_imx_data *data)
  221. {
  222. return !!(data->socdata->flags & ESDHC_FLAG_USDHC);
  223. }
  224. static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
  225. {
  226. void __iomem *base = host->ioaddr + (reg & ~0x3);
  227. u32 shift = (reg & 0x3) * 8;
  228. writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
  229. }
  230. static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
  231. {
  232. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  233. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  234. u32 val = readl(host->ioaddr + reg);
  235. if (unlikely(reg == SDHCI_PRESENT_STATE)) {
  236. u32 fsl_prss = val;
  237. /* save the least 20 bits */
  238. val = fsl_prss & 0x000FFFFF;
  239. /* move dat[0-3] bits */
  240. val |= (fsl_prss & 0x0F000000) >> 4;
  241. /* move cmd line bit */
  242. val |= (fsl_prss & 0x00800000) << 1;
  243. }
  244. if (unlikely(reg == SDHCI_CAPABILITIES)) {
  245. /* ignore bit[0-15] as it stores cap_1 register val for mx6sl */
  246. if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
  247. val &= 0xffff0000;
  248. /* In FSL esdhc IC module, only bit20 is used to indicate the
  249. * ADMA2 capability of esdhc, but this bit is messed up on
  250. * some SOCs (e.g. on MX25, MX35 this bit is set, but they
  251. * don't actually support ADMA2). So set the BROKEN_ADMA
  252. * quirk on MX25/35 platforms.
  253. */
  254. if (val & SDHCI_CAN_DO_ADMA1) {
  255. val &= ~SDHCI_CAN_DO_ADMA1;
  256. val |= SDHCI_CAN_DO_ADMA2;
  257. }
  258. }
  259. if (unlikely(reg == SDHCI_CAPABILITIES_1)) {
  260. if (esdhc_is_usdhc(imx_data)) {
  261. if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
  262. val = readl(host->ioaddr + SDHCI_CAPABILITIES) & 0xFFFF;
  263. else
  264. /* imx6q/dl does not have cap_1 register, fake one */
  265. val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104
  266. | SDHCI_SUPPORT_SDR50
  267. | SDHCI_USE_SDR50_TUNING
  268. | (SDHCI_TUNING_MODE_3 << SDHCI_RETUNING_MODE_SHIFT);
  269. if (imx_data->socdata->flags & ESDHC_FLAG_HS400)
  270. val |= SDHCI_SUPPORT_HS400;
  271. }
  272. }
  273. if (unlikely(reg == SDHCI_MAX_CURRENT) && esdhc_is_usdhc(imx_data)) {
  274. val = 0;
  275. val |= 0xFF << SDHCI_MAX_CURRENT_330_SHIFT;
  276. val |= 0xFF << SDHCI_MAX_CURRENT_300_SHIFT;
  277. val |= 0xFF << SDHCI_MAX_CURRENT_180_SHIFT;
  278. }
  279. if (unlikely(reg == SDHCI_INT_STATUS)) {
  280. if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
  281. val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
  282. val |= SDHCI_INT_ADMA_ERROR;
  283. }
  284. /*
  285. * mask off the interrupt we get in response to the manually
  286. * sent CMD12
  287. */
  288. if ((imx_data->multiblock_status == WAIT_FOR_INT) &&
  289. ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) {
  290. val &= ~SDHCI_INT_RESPONSE;
  291. writel(SDHCI_INT_RESPONSE, host->ioaddr +
  292. SDHCI_INT_STATUS);
  293. imx_data->multiblock_status = NO_CMD_PENDING;
  294. }
  295. }
  296. return val;
  297. }
  298. static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
  299. {
  300. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  301. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  302. u32 data;
  303. if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE ||
  304. reg == SDHCI_INT_STATUS)) {
  305. if ((val & SDHCI_INT_CARD_INT) && !esdhc_is_usdhc(imx_data)) {
  306. /*
  307. * Clear and then set D3CD bit to avoid missing the
  308. * card interrupt. This is an eSDHC controller problem
  309. * so we need to apply the following workaround: clear
  310. * and set D3CD bit will make eSDHC re-sample the card
  311. * interrupt. In case a card interrupt was lost,
  312. * re-sample it by the following steps.
  313. */
  314. data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
  315. data &= ~ESDHC_CTRL_D3CD;
  316. writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
  317. data |= ESDHC_CTRL_D3CD;
  318. writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
  319. }
  320. if (val & SDHCI_INT_ADMA_ERROR) {
  321. val &= ~SDHCI_INT_ADMA_ERROR;
  322. val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
  323. }
  324. }
  325. if (unlikely((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
  326. && (reg == SDHCI_INT_STATUS)
  327. && (val & SDHCI_INT_DATA_END))) {
  328. u32 v;
  329. v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  330. v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
  331. writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
  332. if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS)
  333. {
  334. /* send a manual CMD12 with RESPTYP=none */
  335. data = MMC_STOP_TRANSMISSION << 24 |
  336. SDHCI_CMD_ABORTCMD << 16;
  337. writel(data, host->ioaddr + SDHCI_TRANSFER_MODE);
  338. imx_data->multiblock_status = WAIT_FOR_INT;
  339. }
  340. }
  341. writel(val, host->ioaddr + reg);
  342. }
  343. static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
  344. {
  345. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  346. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  347. u16 ret = 0;
  348. u32 val;
  349. if (unlikely(reg == SDHCI_HOST_VERSION)) {
  350. reg ^= 2;
  351. if (esdhc_is_usdhc(imx_data)) {
  352. /*
  353. * The usdhc register returns a wrong host version.
  354. * Correct it here.
  355. */
  356. return SDHCI_SPEC_300;
  357. }
  358. }
  359. if (unlikely(reg == SDHCI_HOST_CONTROL2)) {
  360. val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  361. if (val & ESDHC_VENDOR_SPEC_VSELECT)
  362. ret |= SDHCI_CTRL_VDD_180;
  363. if (esdhc_is_usdhc(imx_data)) {
  364. if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
  365. val = readl(host->ioaddr + ESDHC_MIX_CTRL);
  366. else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING)
  367. /* the std tuning bits is in ACMD12_ERR for imx6sl */
  368. val = readl(host->ioaddr + SDHCI_ACMD12_ERR);
  369. }
  370. if (val & ESDHC_MIX_CTRL_EXE_TUNE)
  371. ret |= SDHCI_CTRL_EXEC_TUNING;
  372. if (val & ESDHC_MIX_CTRL_SMPCLK_SEL)
  373. ret |= SDHCI_CTRL_TUNED_CLK;
  374. ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
  375. return ret;
  376. }
  377. if (unlikely(reg == SDHCI_TRANSFER_MODE)) {
  378. if (esdhc_is_usdhc(imx_data)) {
  379. u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
  380. ret = m & ESDHC_MIX_CTRL_SDHCI_MASK;
  381. /* Swap AC23 bit */
  382. if (m & ESDHC_MIX_CTRL_AC23EN) {
  383. ret &= ~ESDHC_MIX_CTRL_AC23EN;
  384. ret |= SDHCI_TRNS_AUTO_CMD23;
  385. }
  386. } else {
  387. ret = readw(host->ioaddr + SDHCI_TRANSFER_MODE);
  388. }
  389. return ret;
  390. }
  391. return readw(host->ioaddr + reg);
  392. }
  393. static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
  394. {
  395. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  396. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  397. u32 new_val = 0;
  398. switch (reg) {
  399. case SDHCI_CLOCK_CONTROL:
  400. new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  401. if (val & SDHCI_CLOCK_CARD_EN)
  402. new_val |= ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
  403. else
  404. new_val &= ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
  405. writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
  406. return;
  407. case SDHCI_HOST_CONTROL2:
  408. new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  409. if (val & SDHCI_CTRL_VDD_180)
  410. new_val |= ESDHC_VENDOR_SPEC_VSELECT;
  411. else
  412. new_val &= ~ESDHC_VENDOR_SPEC_VSELECT;
  413. writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
  414. if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
  415. new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
  416. if (val & SDHCI_CTRL_TUNED_CLK) {
  417. new_val |= ESDHC_MIX_CTRL_SMPCLK_SEL;
  418. new_val |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
  419. } else {
  420. new_val &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
  421. new_val &= ~ESDHC_MIX_CTRL_AUTO_TUNE_EN;
  422. }
  423. writel(new_val , host->ioaddr + ESDHC_MIX_CTRL);
  424. } else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
  425. u32 v = readl(host->ioaddr + SDHCI_ACMD12_ERR);
  426. u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
  427. if (val & SDHCI_CTRL_TUNED_CLK) {
  428. v |= ESDHC_MIX_CTRL_SMPCLK_SEL;
  429. } else {
  430. v &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
  431. m &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
  432. m &= ~ESDHC_MIX_CTRL_AUTO_TUNE_EN;
  433. }
  434. if (val & SDHCI_CTRL_EXEC_TUNING) {
  435. v |= ESDHC_MIX_CTRL_EXE_TUNE;
  436. m |= ESDHC_MIX_CTRL_FBCLK_SEL;
  437. m |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
  438. } else {
  439. v &= ~ESDHC_MIX_CTRL_EXE_TUNE;
  440. }
  441. writel(v, host->ioaddr + SDHCI_ACMD12_ERR);
  442. writel(m, host->ioaddr + ESDHC_MIX_CTRL);
  443. }
  444. return;
  445. case SDHCI_TRANSFER_MODE:
  446. if ((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
  447. && (host->cmd->opcode == SD_IO_RW_EXTENDED)
  448. && (host->cmd->data->blocks > 1)
  449. && (host->cmd->data->flags & MMC_DATA_READ)) {
  450. u32 v;
  451. v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  452. v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
  453. writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
  454. }
  455. if (esdhc_is_usdhc(imx_data)) {
  456. u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
  457. /* Swap AC23 bit */
  458. if (val & SDHCI_TRNS_AUTO_CMD23) {
  459. val &= ~SDHCI_TRNS_AUTO_CMD23;
  460. val |= ESDHC_MIX_CTRL_AC23EN;
  461. }
  462. m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
  463. writel(m, host->ioaddr + ESDHC_MIX_CTRL);
  464. } else {
  465. /*
  466. * Postpone this write, we must do it together with a
  467. * command write that is down below.
  468. */
  469. imx_data->scratchpad = val;
  470. }
  471. return;
  472. case SDHCI_COMMAND:
  473. if (host->cmd->opcode == MMC_STOP_TRANSMISSION)
  474. val |= SDHCI_CMD_ABORTCMD;
  475. if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
  476. (imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
  477. imx_data->multiblock_status = MULTIBLK_IN_PROCESS;
  478. if (esdhc_is_usdhc(imx_data))
  479. writel(val << 16,
  480. host->ioaddr + SDHCI_TRANSFER_MODE);
  481. else
  482. writel(val << 16 | imx_data->scratchpad,
  483. host->ioaddr + SDHCI_TRANSFER_MODE);
  484. return;
  485. case SDHCI_BLOCK_SIZE:
  486. val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
  487. break;
  488. }
  489. esdhc_clrset_le(host, 0xffff, val, reg);
  490. }
  491. static u8 esdhc_readb_le(struct sdhci_host *host, int reg)
  492. {
  493. u8 ret;
  494. u32 val;
  495. switch (reg) {
  496. case SDHCI_HOST_CONTROL:
  497. val = readl(host->ioaddr + reg);
  498. ret = val & SDHCI_CTRL_LED;
  499. ret |= (val >> 5) & SDHCI_CTRL_DMA_MASK;
  500. ret |= (val & ESDHC_CTRL_4BITBUS);
  501. ret |= (val & ESDHC_CTRL_8BITBUS) << 3;
  502. return ret;
  503. }
  504. return readb(host->ioaddr + reg);
  505. }
  506. static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
  507. {
  508. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  509. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  510. u32 new_val = 0;
  511. u32 mask;
  512. switch (reg) {
  513. case SDHCI_POWER_CONTROL:
  514. /*
  515. * FSL put some DMA bits here
  516. * If your board has a regulator, code should be here
  517. */
  518. return;
  519. case SDHCI_HOST_CONTROL:
  520. /* FSL messed up here, so we need to manually compose it. */
  521. new_val = val & SDHCI_CTRL_LED;
  522. /* ensure the endianness */
  523. new_val |= ESDHC_HOST_CONTROL_LE;
  524. /* bits 8&9 are reserved on mx25 */
  525. if (!is_imx25_esdhc(imx_data)) {
  526. /* DMA mode bits are shifted */
  527. new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
  528. }
  529. /*
  530. * Do not touch buswidth bits here. This is done in
  531. * esdhc_pltfm_bus_width.
  532. * Do not touch the D3CD bit either which is used for the
  533. * SDIO interrupt erratum workaround.
  534. */
  535. mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD);
  536. esdhc_clrset_le(host, mask, new_val, reg);
  537. return;
  538. case SDHCI_SOFTWARE_RESET:
  539. if (val & SDHCI_RESET_DATA)
  540. new_val = readl(host->ioaddr + SDHCI_HOST_CONTROL);
  541. break;
  542. }
  543. esdhc_clrset_le(host, 0xff, val, reg);
  544. if (reg == SDHCI_SOFTWARE_RESET) {
  545. if (val & SDHCI_RESET_ALL) {
  546. /*
  547. * The esdhc has a design violation to SDHC spec which
  548. * tells that software reset should not affect card
  549. * detection circuit. But esdhc clears its SYSCTL
  550. * register bits [0..2] during the software reset. This
  551. * will stop those clocks that card detection circuit
  552. * relies on. To work around it, we turn the clocks on
  553. * back to keep card detection circuit functional.
  554. */
  555. esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
  556. /*
  557. * The reset on usdhc fails to clear MIX_CTRL register.
  558. * Do it manually here.
  559. */
  560. if (esdhc_is_usdhc(imx_data)) {
  561. /*
  562. * the tuning bits should be kept during reset
  563. */
  564. new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
  565. writel(new_val & ESDHC_MIX_CTRL_TUNING_MASK,
  566. host->ioaddr + ESDHC_MIX_CTRL);
  567. imx_data->is_ddr = 0;
  568. }
  569. } else if (val & SDHCI_RESET_DATA) {
  570. /*
  571. * The eSDHC DAT line software reset clears at least the
  572. * data transfer width on i.MX25, so make sure that the
  573. * Host Control register is unaffected.
  574. */
  575. esdhc_clrset_le(host, 0xff, new_val,
  576. SDHCI_HOST_CONTROL);
  577. }
  578. }
  579. }
  580. static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
  581. {
  582. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  583. return pltfm_host->clock;
  584. }
  585. static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
  586. {
  587. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  588. return pltfm_host->clock / 256 / 16;
  589. }
  590. static inline void esdhc_pltfm_set_clock(struct sdhci_host *host,
  591. unsigned int clock)
  592. {
  593. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  594. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  595. unsigned int host_clock = pltfm_host->clock;
  596. int ddr_pre_div = imx_data->is_ddr ? 2 : 1;
  597. int pre_div = 1;
  598. int div = 1;
  599. u32 temp, val;
  600. if (clock == 0) {
  601. host->mmc->actual_clock = 0;
  602. if (esdhc_is_usdhc(imx_data)) {
  603. val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  604. writel(val & ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
  605. host->ioaddr + ESDHC_VENDOR_SPEC);
  606. }
  607. return;
  608. }
  609. temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
  610. temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
  611. | ESDHC_CLOCK_MASK);
  612. sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
  613. while (host_clock / (16 * pre_div * ddr_pre_div) > clock &&
  614. pre_div < 256)
  615. pre_div *= 2;
  616. while (host_clock / (div * pre_div * ddr_pre_div) > clock && div < 16)
  617. div++;
  618. host->mmc->actual_clock = host_clock / (div * pre_div * ddr_pre_div);
  619. dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
  620. clock, host->mmc->actual_clock);
  621. pre_div >>= 1;
  622. div--;
  623. temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
  624. temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
  625. | (div << ESDHC_DIVIDER_SHIFT)
  626. | (pre_div << ESDHC_PREDIV_SHIFT));
  627. sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
  628. if (esdhc_is_usdhc(imx_data)) {
  629. val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  630. writel(val | ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
  631. host->ioaddr + ESDHC_VENDOR_SPEC);
  632. }
  633. mdelay(1);
  634. }
  635. static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
  636. {
  637. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  638. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  639. struct esdhc_platform_data *boarddata = &imx_data->boarddata;
  640. switch (boarddata->wp_type) {
  641. case ESDHC_WP_GPIO:
  642. return mmc_gpio_get_ro(host->mmc);
  643. case ESDHC_WP_CONTROLLER:
  644. return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
  645. SDHCI_WRITE_PROTECT);
  646. case ESDHC_WP_NONE:
  647. break;
  648. }
  649. return -ENOSYS;
  650. }
  651. static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
  652. {
  653. u32 ctrl;
  654. switch (width) {
  655. case MMC_BUS_WIDTH_8:
  656. ctrl = ESDHC_CTRL_8BITBUS;
  657. break;
  658. case MMC_BUS_WIDTH_4:
  659. ctrl = ESDHC_CTRL_4BITBUS;
  660. break;
  661. default:
  662. ctrl = 0;
  663. break;
  664. }
  665. esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
  666. SDHCI_HOST_CONTROL);
  667. }
  668. static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val)
  669. {
  670. u32 reg;
  671. /* FIXME: delay a bit for card to be ready for next tuning due to errors */
  672. mdelay(1);
  673. reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
  674. reg |= ESDHC_MIX_CTRL_EXE_TUNE | ESDHC_MIX_CTRL_SMPCLK_SEL |
  675. ESDHC_MIX_CTRL_FBCLK_SEL;
  676. writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
  677. writel(val << 8, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
  678. dev_dbg(mmc_dev(host->mmc),
  679. "tuning with delay 0x%x ESDHC_TUNE_CTRL_STATUS 0x%x\n",
  680. val, readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS));
  681. }
  682. static void esdhc_post_tuning(struct sdhci_host *host)
  683. {
  684. u32 reg;
  685. reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
  686. reg &= ~ESDHC_MIX_CTRL_EXE_TUNE;
  687. reg |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
  688. writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
  689. }
  690. static int esdhc_executing_tuning(struct sdhci_host *host, u32 opcode)
  691. {
  692. int min, max, avg, ret;
  693. /* find the mininum delay first which can pass tuning */
  694. min = ESDHC_TUNE_CTRL_MIN;
  695. while (min < ESDHC_TUNE_CTRL_MAX) {
  696. esdhc_prepare_tuning(host, min);
  697. if (!mmc_send_tuning(host->mmc, opcode, NULL))
  698. break;
  699. min += ESDHC_TUNE_CTRL_STEP;
  700. }
  701. /* find the maxinum delay which can not pass tuning */
  702. max = min + ESDHC_TUNE_CTRL_STEP;
  703. while (max < ESDHC_TUNE_CTRL_MAX) {
  704. esdhc_prepare_tuning(host, max);
  705. if (mmc_send_tuning(host->mmc, opcode, NULL)) {
  706. max -= ESDHC_TUNE_CTRL_STEP;
  707. break;
  708. }
  709. max += ESDHC_TUNE_CTRL_STEP;
  710. }
  711. /* use average delay to get the best timing */
  712. avg = (min + max) / 2;
  713. esdhc_prepare_tuning(host, avg);
  714. ret = mmc_send_tuning(host->mmc, opcode, NULL);
  715. esdhc_post_tuning(host);
  716. dev_dbg(mmc_dev(host->mmc), "tuning %s at 0x%x ret %d\n",
  717. ret ? "failed" : "passed", avg, ret);
  718. return ret;
  719. }
  720. static int esdhc_change_pinstate(struct sdhci_host *host,
  721. unsigned int uhs)
  722. {
  723. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  724. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  725. struct pinctrl_state *pinctrl;
  726. dev_dbg(mmc_dev(host->mmc), "change pinctrl state for uhs %d\n", uhs);
  727. if (IS_ERR(imx_data->pinctrl) ||
  728. IS_ERR(imx_data->pins_default) ||
  729. IS_ERR(imx_data->pins_100mhz) ||
  730. IS_ERR(imx_data->pins_200mhz))
  731. return -EINVAL;
  732. switch (uhs) {
  733. case MMC_TIMING_UHS_SDR50:
  734. case MMC_TIMING_UHS_DDR50:
  735. pinctrl = imx_data->pins_100mhz;
  736. break;
  737. case MMC_TIMING_UHS_SDR104:
  738. case MMC_TIMING_MMC_HS200:
  739. case MMC_TIMING_MMC_HS400:
  740. pinctrl = imx_data->pins_200mhz;
  741. break;
  742. default:
  743. /* back to default state for other legacy timing */
  744. pinctrl = imx_data->pins_default;
  745. }
  746. return pinctrl_select_state(imx_data->pinctrl, pinctrl);
  747. }
  748. /*
  749. * For HS400 eMMC, there is a data_strobe line. This signal is generated
  750. * by the device and used for data output and CRC status response output
  751. * in HS400 mode. The frequency of this signal follows the frequency of
  752. * CLK generated by host. The host receives the data which is aligned to the
  753. * edge of data_strobe line. Due to the time delay between CLK line and
  754. * data_strobe line, if the delay time is larger than one clock cycle,
  755. * then CLK and data_strobe line will be misaligned, read error shows up.
  756. * So when the CLK is higher than 100MHz, each clock cycle is short enough,
  757. * host should configure the delay target.
  758. */
  759. static void esdhc_set_strobe_dll(struct sdhci_host *host)
  760. {
  761. u32 v;
  762. if (host->mmc->actual_clock > ESDHC_STROBE_DLL_CLK_FREQ) {
  763. /* disable clock before enabling strobe dll */
  764. writel(readl(host->ioaddr + ESDHC_VENDOR_SPEC) &
  765. ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
  766. host->ioaddr + ESDHC_VENDOR_SPEC);
  767. /* force a reset on strobe dll */
  768. writel(ESDHC_STROBE_DLL_CTRL_RESET,
  769. host->ioaddr + ESDHC_STROBE_DLL_CTRL);
  770. /*
  771. * enable strobe dll ctrl and adjust the delay target
  772. * for the uSDHC loopback read clock
  773. */
  774. v = ESDHC_STROBE_DLL_CTRL_ENABLE |
  775. (7 << ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
  776. writel(v, host->ioaddr + ESDHC_STROBE_DLL_CTRL);
  777. /* wait 1us to make sure strobe dll status register stable */
  778. udelay(1);
  779. v = readl(host->ioaddr + ESDHC_STROBE_DLL_STATUS);
  780. if (!(v & ESDHC_STROBE_DLL_STS_REF_LOCK))
  781. dev_warn(mmc_dev(host->mmc),
  782. "warning! HS400 strobe DLL status REF not lock!\n");
  783. if (!(v & ESDHC_STROBE_DLL_STS_SLV_LOCK))
  784. dev_warn(mmc_dev(host->mmc),
  785. "warning! HS400 strobe DLL status SLV not lock!\n");
  786. }
  787. }
  788. static void esdhc_reset_tuning(struct sdhci_host *host)
  789. {
  790. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  791. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  792. u32 ctrl;
  793. /* Reset the tuning circuit */
  794. if (esdhc_is_usdhc(imx_data)) {
  795. if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
  796. ctrl = readl(host->ioaddr + ESDHC_MIX_CTRL);
  797. ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
  798. ctrl &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
  799. writel(ctrl, host->ioaddr + ESDHC_MIX_CTRL);
  800. writel(0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
  801. } else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
  802. ctrl = readl(host->ioaddr + SDHCI_ACMD12_ERR);
  803. ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
  804. writel(ctrl, host->ioaddr + SDHCI_ACMD12_ERR);
  805. }
  806. }
  807. }
  808. static void esdhc_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
  809. {
  810. u32 m;
  811. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  812. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  813. struct esdhc_platform_data *boarddata = &imx_data->boarddata;
  814. /* disable ddr mode and disable HS400 mode */
  815. m = readl(host->ioaddr + ESDHC_MIX_CTRL);
  816. m &= ~(ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN);
  817. imx_data->is_ddr = 0;
  818. switch (timing) {
  819. case MMC_TIMING_UHS_SDR12:
  820. case MMC_TIMING_UHS_SDR25:
  821. case MMC_TIMING_UHS_SDR50:
  822. case MMC_TIMING_UHS_SDR104:
  823. case MMC_TIMING_MMC_HS200:
  824. writel(m, host->ioaddr + ESDHC_MIX_CTRL);
  825. break;
  826. case MMC_TIMING_UHS_DDR50:
  827. case MMC_TIMING_MMC_DDR52:
  828. m |= ESDHC_MIX_CTRL_DDREN;
  829. writel(m, host->ioaddr + ESDHC_MIX_CTRL);
  830. imx_data->is_ddr = 1;
  831. if (boarddata->delay_line) {
  832. u32 v;
  833. v = boarddata->delay_line <<
  834. ESDHC_DLL_OVERRIDE_VAL_SHIFT |
  835. (1 << ESDHC_DLL_OVERRIDE_EN_SHIFT);
  836. if (is_imx53_esdhc(imx_data))
  837. v <<= 1;
  838. writel(v, host->ioaddr + ESDHC_DLL_CTRL);
  839. }
  840. break;
  841. case MMC_TIMING_MMC_HS400:
  842. m |= ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN;
  843. writel(m, host->ioaddr + ESDHC_MIX_CTRL);
  844. imx_data->is_ddr = 1;
  845. /* update clock after enable DDR for strobe DLL lock */
  846. host->ops->set_clock(host, host->clock);
  847. esdhc_set_strobe_dll(host);
  848. break;
  849. case MMC_TIMING_LEGACY:
  850. default:
  851. esdhc_reset_tuning(host);
  852. break;
  853. }
  854. esdhc_change_pinstate(host, timing);
  855. }
  856. static void esdhc_reset(struct sdhci_host *host, u8 mask)
  857. {
  858. sdhci_reset(host, mask);
  859. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  860. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  861. }
  862. static unsigned int esdhc_get_max_timeout_count(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. /* Doc Erratum: the uSDHC actual maximum timeout count is 1 << 29 */
  867. return esdhc_is_usdhc(imx_data) ? 1 << 29 : 1 << 27;
  868. }
  869. static void esdhc_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
  870. {
  871. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  872. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  873. /* use maximum timeout counter */
  874. esdhc_clrset_le(host, ESDHC_SYS_CTRL_DTOCV_MASK,
  875. esdhc_is_usdhc(imx_data) ? 0xF : 0xE,
  876. SDHCI_TIMEOUT_CONTROL);
  877. }
  878. static struct sdhci_ops sdhci_esdhc_ops = {
  879. .read_l = esdhc_readl_le,
  880. .read_w = esdhc_readw_le,
  881. .read_b = esdhc_readb_le,
  882. .write_l = esdhc_writel_le,
  883. .write_w = esdhc_writew_le,
  884. .write_b = esdhc_writeb_le,
  885. .set_clock = esdhc_pltfm_set_clock,
  886. .get_max_clock = esdhc_pltfm_get_max_clock,
  887. .get_min_clock = esdhc_pltfm_get_min_clock,
  888. .get_max_timeout_count = esdhc_get_max_timeout_count,
  889. .get_ro = esdhc_pltfm_get_ro,
  890. .set_timeout = esdhc_set_timeout,
  891. .set_bus_width = esdhc_pltfm_set_bus_width,
  892. .set_uhs_signaling = esdhc_set_uhs_signaling,
  893. .reset = esdhc_reset,
  894. };
  895. static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
  896. .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
  897. | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
  898. | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
  899. | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
  900. .ops = &sdhci_esdhc_ops,
  901. };
  902. static void sdhci_esdhc_imx_hwinit(struct sdhci_host *host)
  903. {
  904. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  905. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  906. int tmp;
  907. if (esdhc_is_usdhc(imx_data)) {
  908. /*
  909. * The imx6q ROM code will change the default watermark
  910. * level setting to something insane. Change it back here.
  911. */
  912. writel(ESDHC_WTMK_DEFAULT_VAL, host->ioaddr + ESDHC_WTMK_LVL);
  913. /*
  914. * ROM code will change the bit burst_length_enable setting
  915. * to zero if this usdhc is chosen to boot system. Change
  916. * it back here, otherwise it will impact the performance a
  917. * lot. This bit is used to enable/disable the burst length
  918. * for the external AHB2AXI bridge. It's useful especially
  919. * for INCR transfer because without burst length indicator,
  920. * the AHB2AXI bridge does not know the burst length in
  921. * advance. And without burst length indicator, AHB INCR
  922. * transfer can only be converted to singles on the AXI side.
  923. */
  924. writel(readl(host->ioaddr + SDHCI_HOST_CONTROL)
  925. | ESDHC_BURST_LEN_EN_INCR,
  926. host->ioaddr + SDHCI_HOST_CONTROL);
  927. /*
  928. * erratum ESDHC_FLAG_ERR004536 fix for MX6Q TO1.2 and MX6DL
  929. * TO1.1, it's harmless for MX6SL
  930. */
  931. writel(readl(host->ioaddr + 0x6c) | BIT(7),
  932. host->ioaddr + 0x6c);
  933. /* disable DLL_CTRL delay line settings */
  934. writel(0x0, host->ioaddr + ESDHC_DLL_CTRL);
  935. if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
  936. tmp = readl(host->ioaddr + ESDHC_TUNING_CTRL);
  937. tmp |= ESDHC_STD_TUNING_EN |
  938. ESDHC_TUNING_START_TAP_DEFAULT;
  939. if (imx_data->boarddata.tuning_start_tap) {
  940. tmp &= ~ESDHC_TUNING_START_TAP_MASK;
  941. tmp |= imx_data->boarddata.tuning_start_tap;
  942. }
  943. if (imx_data->boarddata.tuning_step) {
  944. tmp &= ~ESDHC_TUNING_STEP_MASK;
  945. tmp |= imx_data->boarddata.tuning_step
  946. << ESDHC_TUNING_STEP_SHIFT;
  947. }
  948. writel(tmp, host->ioaddr + ESDHC_TUNING_CTRL);
  949. }
  950. }
  951. }
  952. #ifdef CONFIG_OF
  953. static int
  954. sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
  955. struct sdhci_host *host,
  956. struct pltfm_imx_data *imx_data)
  957. {
  958. struct device_node *np = pdev->dev.of_node;
  959. struct esdhc_platform_data *boarddata = &imx_data->boarddata;
  960. int ret;
  961. if (of_get_property(np, "fsl,wp-controller", NULL))
  962. boarddata->wp_type = ESDHC_WP_CONTROLLER;
  963. boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
  964. if (gpio_is_valid(boarddata->wp_gpio))
  965. boarddata->wp_type = ESDHC_WP_GPIO;
  966. of_property_read_u32(np, "fsl,tuning-step", &boarddata->tuning_step);
  967. of_property_read_u32(np, "fsl,tuning-start-tap",
  968. &boarddata->tuning_start_tap);
  969. if (of_find_property(np, "no-1-8-v", NULL))
  970. boarddata->support_vsel = false;
  971. else
  972. boarddata->support_vsel = true;
  973. if (of_property_read_u32(np, "fsl,delay-line", &boarddata->delay_line))
  974. boarddata->delay_line = 0;
  975. mmc_of_parse_voltage(np, &host->ocr_mask);
  976. /* sdr50 and sdr104 need work on 1.8v signal voltage */
  977. if ((boarddata->support_vsel) && esdhc_is_usdhc(imx_data) &&
  978. !IS_ERR(imx_data->pins_default)) {
  979. imx_data->pins_100mhz = pinctrl_lookup_state(imx_data->pinctrl,
  980. ESDHC_PINCTRL_STATE_100MHZ);
  981. imx_data->pins_200mhz = pinctrl_lookup_state(imx_data->pinctrl,
  982. ESDHC_PINCTRL_STATE_200MHZ);
  983. if (IS_ERR(imx_data->pins_100mhz) ||
  984. IS_ERR(imx_data->pins_200mhz)) {
  985. dev_warn(mmc_dev(host->mmc),
  986. "could not get ultra high speed state, work on normal mode\n");
  987. /*
  988. * fall back to not supporting uhs by specifying no
  989. * 1.8v quirk
  990. */
  991. host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
  992. }
  993. } else {
  994. host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
  995. }
  996. /* call to generic mmc_of_parse to support additional capabilities */
  997. ret = mmc_of_parse(host->mmc);
  998. if (ret)
  999. return ret;
  1000. if (mmc_gpio_get_cd(host->mmc) >= 0)
  1001. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  1002. return 0;
  1003. }
  1004. #else
  1005. static inline int
  1006. sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
  1007. struct sdhci_host *host,
  1008. struct pltfm_imx_data *imx_data)
  1009. {
  1010. return -ENODEV;
  1011. }
  1012. #endif
  1013. static int sdhci_esdhc_imx_probe_nondt(struct platform_device *pdev,
  1014. struct sdhci_host *host,
  1015. struct pltfm_imx_data *imx_data)
  1016. {
  1017. struct esdhc_platform_data *boarddata = &imx_data->boarddata;
  1018. int err;
  1019. if (!host->mmc->parent->platform_data) {
  1020. dev_err(mmc_dev(host->mmc), "no board data!\n");
  1021. return -EINVAL;
  1022. }
  1023. imx_data->boarddata = *((struct esdhc_platform_data *)
  1024. host->mmc->parent->platform_data);
  1025. /* write_protect */
  1026. if (boarddata->wp_type == ESDHC_WP_GPIO) {
  1027. err = mmc_gpio_request_ro(host->mmc, boarddata->wp_gpio);
  1028. if (err) {
  1029. dev_err(mmc_dev(host->mmc),
  1030. "failed to request write-protect gpio!\n");
  1031. return err;
  1032. }
  1033. host->mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
  1034. }
  1035. /* card_detect */
  1036. switch (boarddata->cd_type) {
  1037. case ESDHC_CD_GPIO:
  1038. err = mmc_gpio_request_cd(host->mmc, boarddata->cd_gpio, 0);
  1039. if (err) {
  1040. dev_err(mmc_dev(host->mmc),
  1041. "failed to request card-detect gpio!\n");
  1042. return err;
  1043. }
  1044. /* fall through */
  1045. case ESDHC_CD_CONTROLLER:
  1046. /* we have a working card_detect back */
  1047. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  1048. break;
  1049. case ESDHC_CD_PERMANENT:
  1050. host->mmc->caps |= MMC_CAP_NONREMOVABLE;
  1051. break;
  1052. case ESDHC_CD_NONE:
  1053. break;
  1054. }
  1055. switch (boarddata->max_bus_width) {
  1056. case 8:
  1057. host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA;
  1058. break;
  1059. case 4:
  1060. host->mmc->caps |= MMC_CAP_4_BIT_DATA;
  1061. break;
  1062. case 1:
  1063. default:
  1064. host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
  1065. break;
  1066. }
  1067. return 0;
  1068. }
  1069. static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
  1070. {
  1071. const struct of_device_id *of_id =
  1072. of_match_device(imx_esdhc_dt_ids, &pdev->dev);
  1073. struct sdhci_pltfm_host *pltfm_host;
  1074. struct sdhci_host *host;
  1075. int err;
  1076. struct pltfm_imx_data *imx_data;
  1077. host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata,
  1078. sizeof(*imx_data));
  1079. if (IS_ERR(host))
  1080. return PTR_ERR(host);
  1081. pltfm_host = sdhci_priv(host);
  1082. imx_data = sdhci_pltfm_priv(pltfm_host);
  1083. imx_data->socdata = of_id ? of_id->data : (struct esdhc_soc_data *)
  1084. pdev->id_entry->driver_data;
  1085. imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  1086. if (IS_ERR(imx_data->clk_ipg)) {
  1087. err = PTR_ERR(imx_data->clk_ipg);
  1088. goto free_sdhci;
  1089. }
  1090. imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  1091. if (IS_ERR(imx_data->clk_ahb)) {
  1092. err = PTR_ERR(imx_data->clk_ahb);
  1093. goto free_sdhci;
  1094. }
  1095. imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
  1096. if (IS_ERR(imx_data->clk_per)) {
  1097. err = PTR_ERR(imx_data->clk_per);
  1098. goto free_sdhci;
  1099. }
  1100. pltfm_host->clk = imx_data->clk_per;
  1101. pltfm_host->clock = clk_get_rate(pltfm_host->clk);
  1102. err = clk_prepare_enable(imx_data->clk_per);
  1103. if (err)
  1104. goto free_sdhci;
  1105. err = clk_prepare_enable(imx_data->clk_ipg);
  1106. if (err)
  1107. goto disable_per_clk;
  1108. err = clk_prepare_enable(imx_data->clk_ahb);
  1109. if (err)
  1110. goto disable_ipg_clk;
  1111. imx_data->pinctrl = devm_pinctrl_get(&pdev->dev);
  1112. if (IS_ERR(imx_data->pinctrl)) {
  1113. err = PTR_ERR(imx_data->pinctrl);
  1114. goto disable_ahb_clk;
  1115. }
  1116. imx_data->pins_default = pinctrl_lookup_state(imx_data->pinctrl,
  1117. PINCTRL_STATE_DEFAULT);
  1118. if (IS_ERR(imx_data->pins_default))
  1119. dev_warn(mmc_dev(host->mmc), "could not get default state\n");
  1120. if (esdhc_is_usdhc(imx_data)) {
  1121. host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
  1122. host->mmc->caps |= MMC_CAP_1_8V_DDR;
  1123. if (!(imx_data->socdata->flags & ESDHC_FLAG_HS200))
  1124. host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
  1125. /* clear tuning bits in case ROM has set it already */
  1126. writel(0x0, host->ioaddr + ESDHC_MIX_CTRL);
  1127. writel(0x0, host->ioaddr + SDHCI_ACMD12_ERR);
  1128. writel(0x0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
  1129. }
  1130. if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
  1131. sdhci_esdhc_ops.platform_execute_tuning =
  1132. esdhc_executing_tuning;
  1133. if (imx_data->socdata->flags & ESDHC_FLAG_ERR004536)
  1134. host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
  1135. if (imx_data->socdata->flags & ESDHC_FLAG_HS400)
  1136. host->quirks2 |= SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400;
  1137. if (of_id)
  1138. err = sdhci_esdhc_imx_probe_dt(pdev, host, imx_data);
  1139. else
  1140. err = sdhci_esdhc_imx_probe_nondt(pdev, host, imx_data);
  1141. if (err)
  1142. goto disable_ahb_clk;
  1143. sdhci_esdhc_imx_hwinit(host);
  1144. err = sdhci_add_host(host);
  1145. if (err)
  1146. goto disable_ahb_clk;
  1147. pm_runtime_set_active(&pdev->dev);
  1148. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  1149. pm_runtime_use_autosuspend(&pdev->dev);
  1150. pm_suspend_ignore_children(&pdev->dev, 1);
  1151. pm_runtime_enable(&pdev->dev);
  1152. return 0;
  1153. disable_ahb_clk:
  1154. clk_disable_unprepare(imx_data->clk_ahb);
  1155. disable_ipg_clk:
  1156. clk_disable_unprepare(imx_data->clk_ipg);
  1157. disable_per_clk:
  1158. clk_disable_unprepare(imx_data->clk_per);
  1159. free_sdhci:
  1160. sdhci_pltfm_free(pdev);
  1161. return err;
  1162. }
  1163. static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
  1164. {
  1165. struct sdhci_host *host = platform_get_drvdata(pdev);
  1166. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1167. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  1168. int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
  1169. pm_runtime_get_sync(&pdev->dev);
  1170. pm_runtime_disable(&pdev->dev);
  1171. pm_runtime_put_noidle(&pdev->dev);
  1172. sdhci_remove_host(host, dead);
  1173. clk_disable_unprepare(imx_data->clk_per);
  1174. clk_disable_unprepare(imx_data->clk_ipg);
  1175. clk_disable_unprepare(imx_data->clk_ahb);
  1176. sdhci_pltfm_free(pdev);
  1177. return 0;
  1178. }
  1179. #ifdef CONFIG_PM_SLEEP
  1180. static int sdhci_esdhc_suspend(struct device *dev)
  1181. {
  1182. struct sdhci_host *host = dev_get_drvdata(dev);
  1183. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  1184. mmc_retune_needed(host->mmc);
  1185. return sdhci_suspend_host(host);
  1186. }
  1187. static int sdhci_esdhc_resume(struct device *dev)
  1188. {
  1189. struct sdhci_host *host = dev_get_drvdata(dev);
  1190. /* re-initialize hw state in case it's lost in low power mode */
  1191. sdhci_esdhc_imx_hwinit(host);
  1192. return sdhci_resume_host(host);
  1193. }
  1194. #endif
  1195. #ifdef CONFIG_PM
  1196. static int sdhci_esdhc_runtime_suspend(struct device *dev)
  1197. {
  1198. struct sdhci_host *host = dev_get_drvdata(dev);
  1199. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1200. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  1201. int ret;
  1202. ret = sdhci_runtime_suspend_host(host);
  1203. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  1204. mmc_retune_needed(host->mmc);
  1205. if (!sdhci_sdio_irq_enabled(host)) {
  1206. clk_disable_unprepare(imx_data->clk_per);
  1207. clk_disable_unprepare(imx_data->clk_ipg);
  1208. }
  1209. clk_disable_unprepare(imx_data->clk_ahb);
  1210. return ret;
  1211. }
  1212. static int sdhci_esdhc_runtime_resume(struct device *dev)
  1213. {
  1214. struct sdhci_host *host = dev_get_drvdata(dev);
  1215. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1216. struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
  1217. int err;
  1218. if (!sdhci_sdio_irq_enabled(host)) {
  1219. err = clk_prepare_enable(imx_data->clk_per);
  1220. if (err)
  1221. return err;
  1222. err = clk_prepare_enable(imx_data->clk_ipg);
  1223. if (err)
  1224. goto disable_per_clk;
  1225. }
  1226. err = clk_prepare_enable(imx_data->clk_ahb);
  1227. if (err)
  1228. goto disable_ipg_clk;
  1229. err = sdhci_runtime_resume_host(host);
  1230. if (err)
  1231. goto disable_ahb_clk;
  1232. return 0;
  1233. disable_ahb_clk:
  1234. clk_disable_unprepare(imx_data->clk_ahb);
  1235. disable_ipg_clk:
  1236. if (!sdhci_sdio_irq_enabled(host))
  1237. clk_disable_unprepare(imx_data->clk_ipg);
  1238. disable_per_clk:
  1239. if (!sdhci_sdio_irq_enabled(host))
  1240. clk_disable_unprepare(imx_data->clk_per);
  1241. return err;
  1242. }
  1243. #endif
  1244. static const struct dev_pm_ops sdhci_esdhc_pmops = {
  1245. SET_SYSTEM_SLEEP_PM_OPS(sdhci_esdhc_suspend, sdhci_esdhc_resume)
  1246. SET_RUNTIME_PM_OPS(sdhci_esdhc_runtime_suspend,
  1247. sdhci_esdhc_runtime_resume, NULL)
  1248. };
  1249. static struct platform_driver sdhci_esdhc_imx_driver = {
  1250. .driver = {
  1251. .name = "sdhci-esdhc-imx",
  1252. .of_match_table = imx_esdhc_dt_ids,
  1253. .pm = &sdhci_esdhc_pmops,
  1254. },
  1255. .id_table = imx_esdhc_devtype,
  1256. .probe = sdhci_esdhc_imx_probe,
  1257. .remove = sdhci_esdhc_imx_remove,
  1258. };
  1259. module_platform_driver(sdhci_esdhc_imx_driver);
  1260. MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
  1261. MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
  1262. MODULE_LICENSE("GPL v2");