sdhci-esdhc-imx.c 28 KB

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