sdhci-esdhc-imx.c 37 KB

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