sdhci-esdhc-imx.c 33 KB

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