sdhci-esdhc-imx.c 38 KB

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