sdhci-esdhc-imx.c 32 KB

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