sdhci-esdhc-imx.c 34 KB

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