sdhci-esdhc-imx.c 39 KB

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