blob: 260a81fde18da2cc5d9bced3bfdce81ce5e6d729 [file] [log] [blame]
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001/*
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
14#include <linux/io.h>
15#include <linux/delay.h>
16#include <linux/err.h>
17#include <linux/clk.h>
Wolfram Sang0c6d49c2011-02-26 14:44:39 +010018#include <linux/gpio.h>
Shawn Guo66506f72011-08-15 10:28:18 +080019#include <linux/module.h>
Richard Zhue1498602011-03-25 09:18:27 -040020#include <linux/slab.h>
Wolfram Sang95f25ef2010-10-15 12:21:04 +020021#include <linux/mmc/host.h>
Richard Zhu58ac8172011-03-21 13:22:16 +080022#include <linux/mmc/mmc.h>
23#include <linux/mmc/sdio.h>
Shawn Guofbe5fdd2012-12-11 22:32:20 +080024#include <linux/mmc/slot-gpio.h>
Shawn Guoabfafc22011-06-30 15:44:44 +080025#include <linux/of.h>
26#include <linux/of_device.h>
27#include <linux/of_gpio.h>
Dong Aishenge62d8b82012-05-11 14:56:01 +080028#include <linux/pinctrl/consumer.h>
Arnd Bergmann82906b12012-08-24 15:14:29 +020029#include <linux/platform_data/mmc-esdhc-imx.h>
Wolfram Sang95f25ef2010-10-15 12:21:04 +020030#include "sdhci-pltfm.h"
31#include "sdhci-esdhc.h"
32
Shawn Guo60bf6392013-01-15 23:36:53 +080033#define ESDHC_CTRL_D3CD 0x08
Richard Zhu58ac8172011-03-21 13:22:16 +080034/* VENDOR SPEC register */
Shawn Guo60bf6392013-01-15 23:36:53 +080035#define ESDHC_VENDOR_SPEC 0xc0
36#define ESDHC_VENDOR_SPEC_SDIO_QUIRK (1 << 1)
Dong Aisheng03221912013-09-13 19:11:34 +080037#define ESDHC_VENDOR_SPEC_VSELECT (1 << 1)
Dong Aishengfed2f6e2013-09-13 19:11:33 +080038#define ESDHC_VENDOR_SPEC_FRC_SDCLK_ON (1 << 8)
Shawn Guo60bf6392013-01-15 23:36:53 +080039#define ESDHC_WTMK_LVL 0x44
40#define ESDHC_MIX_CTRL 0x48
Dong Aishengde5bdbf2013-10-18 19:48:46 +080041#define ESDHC_MIX_CTRL_DDREN (1 << 3)
Shawn Guo2a15f982013-01-21 19:02:26 +080042#define ESDHC_MIX_CTRL_AC23EN (1 << 7)
Dong Aisheng03221912013-09-13 19:11:34 +080043#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)
Shawn Guo2a15f982013-01-21 19:02:26 +080046/* Bits 3 and 6 are not SDHCI standard definitions */
47#define ESDHC_MIX_CTRL_SDHCI_MASK 0xb7
Richard Zhu58ac8172011-03-21 13:22:16 +080048
Dong Aisheng602519b2013-10-18 19:48:47 +080049/* dll control register */
50#define ESDHC_DLL_CTRL 0x60
51#define ESDHC_DLL_OVERRIDE_VAL_SHIFT 9
52#define ESDHC_DLL_OVERRIDE_EN_SHIFT 8
53
Dong Aisheng03221912013-09-13 19:11:34 +080054/* tune control register */
55#define ESDHC_TUNE_CTRL_STATUS 0x68
56#define ESDHC_TUNE_CTRL_STEP 1
57#define ESDHC_TUNE_CTRL_MIN 0
58#define ESDHC_TUNE_CTRL_MAX ((1 << 7) - 1)
59
Dong Aisheng6e9fd282013-10-18 19:48:43 +080060#define ESDHC_TUNING_CTRL 0xcc
61#define ESDHC_STD_TUNING_EN (1 << 24)
62/* NOTE: the minimum valid tuning start tap for mx6sl is 1 */
63#define ESDHC_TUNING_START_TAP 0x1
64
Dong Aisheng03221912013-09-13 19:11:34 +080065#define ESDHC_TUNING_BLOCK_PATTERN_LEN 64
66
Dong Aishengad932202013-09-13 19:11:35 +080067/* pinctrl state */
68#define ESDHC_PINCTRL_STATE_100MHZ "state_100mhz"
69#define ESDHC_PINCTRL_STATE_200MHZ "state_200mhz"
70
Richard Zhu58ac8172011-03-21 13:22:16 +080071/*
Sascha Haueraf510792013-01-21 19:02:28 +080072 * Our interpretation of the SDHCI_HOST_CONTROL register
73 */
74#define ESDHC_CTRL_4BITBUS (0x1 << 1)
75#define ESDHC_CTRL_8BITBUS (0x2 << 1)
76#define ESDHC_CTRL_BUSWIDTH_MASK (0x3 << 1)
77
78/*
Richard Zhu97e4ba62011-08-11 16:51:46 -040079 * There is an INT DMA ERR mis-match between eSDHC and STD SDHC SPEC:
80 * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
81 * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
82 * Define this macro DMA error INT for fsl eSDHC
83 */
Shawn Guo60bf6392013-01-15 23:36:53 +080084#define ESDHC_INT_VENDOR_SPEC_DMA_ERR (1 << 28)
Richard Zhu97e4ba62011-08-11 16:51:46 -040085
86/*
Richard Zhu58ac8172011-03-21 13:22:16 +080087 * The CMDTYPE of the CMD register (offset 0xE) should be set to
88 * "11" when the STOP CMD12 is issued on imx53 to abort one
89 * open ended multi-blk IO. Otherwise the TC INT wouldn't
90 * be generated.
91 * In exact block transfer, the controller doesn't complete the
92 * operations automatically as required at the end of the
93 * transfer and remains on hold if the abort command is not sent.
94 * As a result, the TC flag is not asserted and SW received timeout
95 * exeception. Bit1 of Vendor Spec registor is used to fix it.
96 */
Shawn Guo31fbb302013-10-17 15:19:44 +080097#define ESDHC_FLAG_MULTIBLK_NO_INT BIT(1)
98/*
99 * The flag enables the workaround for ESDHC errata ENGcm07207 which
100 * affects i.MX25 and i.MX35.
101 */
102#define ESDHC_FLAG_ENGCM07207 BIT(2)
Shawn Guo9d61c002013-10-17 15:19:45 +0800103/*
104 * The flag tells that the ESDHC controller is an USDHC block that is
105 * integrated on the i.MX6 series.
106 */
107#define ESDHC_FLAG_USDHC BIT(3)
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800108/* The IP supports manual tuning process */
109#define ESDHC_FLAG_MAN_TUNING BIT(4)
110/* The IP supports standard tuning process */
111#define ESDHC_FLAG_STD_TUNING BIT(5)
112/* The IP has SDHCI_CAPABILITIES_1 register */
113#define ESDHC_FLAG_HAVE_CAP1 BIT(6)
Richard Zhue1498602011-03-25 09:18:27 -0400114
Shawn Guof47c4bb2013-10-17 15:19:47 +0800115struct esdhc_soc_data {
116 u32 flags;
117};
118
119static struct esdhc_soc_data esdhc_imx25_data = {
120 .flags = ESDHC_FLAG_ENGCM07207,
121};
122
123static struct esdhc_soc_data esdhc_imx35_data = {
124 .flags = ESDHC_FLAG_ENGCM07207,
125};
126
127static struct esdhc_soc_data esdhc_imx51_data = {
128 .flags = 0,
129};
130
131static struct esdhc_soc_data esdhc_imx53_data = {
132 .flags = ESDHC_FLAG_MULTIBLK_NO_INT,
133};
134
135static struct esdhc_soc_data usdhc_imx6q_data = {
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800136 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING,
137};
138
139static struct esdhc_soc_data usdhc_imx6sl_data = {
140 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
141 | ESDHC_FLAG_HAVE_CAP1,
Shawn Guo57ed3312011-06-30 09:24:26 +0800142};
143
Richard Zhue1498602011-03-25 09:18:27 -0400144struct pltfm_imx_data {
Richard Zhue1498602011-03-25 09:18:27 -0400145 u32 scratchpad;
Dong Aishenge62d8b82012-05-11 14:56:01 +0800146 struct pinctrl *pinctrl;
Dong Aishengad932202013-09-13 19:11:35 +0800147 struct pinctrl_state *pins_default;
148 struct pinctrl_state *pins_100mhz;
149 struct pinctrl_state *pins_200mhz;
Shawn Guof47c4bb2013-10-17 15:19:47 +0800150 const struct esdhc_soc_data *socdata;
Shawn Guo842afc02011-07-06 22:57:48 +0800151 struct esdhc_platform_data boarddata;
Sascha Hauer52dac612012-03-07 09:31:34 +0100152 struct clk *clk_ipg;
153 struct clk *clk_ahb;
154 struct clk *clk_per;
Lucas Stach361b8482013-03-15 09:49:26 +0100155 enum {
156 NO_CMD_PENDING, /* no multiblock command pending*/
157 MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */
158 WAIT_FOR_INT, /* sent CMD12, waiting for response INT */
159 } multiblock_status;
Dong Aisheng03221912013-09-13 19:11:34 +0800160 u32 uhs_mode;
Dong Aishengde5bdbf2013-10-18 19:48:46 +0800161 u32 is_ddr;
Richard Zhue1498602011-03-25 09:18:27 -0400162};
163
Shawn Guo57ed3312011-06-30 09:24:26 +0800164static struct platform_device_id imx_esdhc_devtype[] = {
165 {
166 .name = "sdhci-esdhc-imx25",
Shawn Guof47c4bb2013-10-17 15:19:47 +0800167 .driver_data = (kernel_ulong_t) &esdhc_imx25_data,
Shawn Guo57ed3312011-06-30 09:24:26 +0800168 }, {
169 .name = "sdhci-esdhc-imx35",
Shawn Guof47c4bb2013-10-17 15:19:47 +0800170 .driver_data = (kernel_ulong_t) &esdhc_imx35_data,
Shawn Guo57ed3312011-06-30 09:24:26 +0800171 }, {
172 .name = "sdhci-esdhc-imx51",
Shawn Guof47c4bb2013-10-17 15:19:47 +0800173 .driver_data = (kernel_ulong_t) &esdhc_imx51_data,
Shawn Guo57ed3312011-06-30 09:24:26 +0800174 }, {
Shawn Guo57ed3312011-06-30 09:24:26 +0800175 /* sentinel */
176 }
177};
178MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
179
Shawn Guoabfafc22011-06-30 15:44:44 +0800180static const struct of_device_id imx_esdhc_dt_ids[] = {
Shawn Guof47c4bb2013-10-17 15:19:47 +0800181 { .compatible = "fsl,imx25-esdhc", .data = &esdhc_imx25_data, },
182 { .compatible = "fsl,imx35-esdhc", .data = &esdhc_imx35_data, },
183 { .compatible = "fsl,imx51-esdhc", .data = &esdhc_imx51_data, },
184 { .compatible = "fsl,imx53-esdhc", .data = &esdhc_imx53_data, },
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800185 { .compatible = "fsl,imx6sl-usdhc", .data = &usdhc_imx6sl_data, },
Shawn Guof47c4bb2013-10-17 15:19:47 +0800186 { .compatible = "fsl,imx6q-usdhc", .data = &usdhc_imx6q_data, },
Shawn Guoabfafc22011-06-30 15:44:44 +0800187 { /* sentinel */ }
188};
189MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
190
Shawn Guo57ed3312011-06-30 09:24:26 +0800191static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
192{
Shawn Guof47c4bb2013-10-17 15:19:47 +0800193 return data->socdata == &esdhc_imx25_data;
Shawn Guo57ed3312011-06-30 09:24:26 +0800194}
195
196static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
197{
Shawn Guof47c4bb2013-10-17 15:19:47 +0800198 return data->socdata == &esdhc_imx53_data;
Shawn Guo57ed3312011-06-30 09:24:26 +0800199}
200
Shawn Guo95a24822011-09-19 17:32:21 +0800201static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
202{
Shawn Guof47c4bb2013-10-17 15:19:47 +0800203 return data->socdata == &usdhc_imx6q_data;
Shawn Guo95a24822011-09-19 17:32:21 +0800204}
205
Shawn Guo9d61c002013-10-17 15:19:45 +0800206static inline int esdhc_is_usdhc(struct pltfm_imx_data *data)
207{
Shawn Guof47c4bb2013-10-17 15:19:47 +0800208 return !!(data->socdata->flags & ESDHC_FLAG_USDHC);
Shawn Guo9d61c002013-10-17 15:19:45 +0800209}
210
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200211static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
212{
213 void __iomem *base = host->ioaddr + (reg & ~0x3);
214 u32 shift = (reg & 0x3) * 8;
215
216 writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
217}
218
Wolfram Sang7e29c302011-02-26 14:44:41 +0100219static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
220{
Lucas Stach361b8482013-03-15 09:49:26 +0100221 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
222 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100223 u32 val = readl(host->ioaddr + reg);
224
Dong Aisheng03221912013-09-13 19:11:34 +0800225 if (unlikely(reg == SDHCI_PRESENT_STATE)) {
226 u32 fsl_prss = val;
227 /* save the least 20 bits */
228 val = fsl_prss & 0x000FFFFF;
229 /* move dat[0-3] bits */
230 val |= (fsl_prss & 0x0F000000) >> 4;
231 /* move cmd line bit */
232 val |= (fsl_prss & 0x00800000) << 1;
233 }
234
Richard Zhu97e4ba62011-08-11 16:51:46 -0400235 if (unlikely(reg == SDHCI_CAPABILITIES)) {
Dong Aisheng6b4fb6712a2013-10-18 19:48:44 +0800236 /* ignore bit[0-15] as it stores cap_1 register val for mx6sl */
237 if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
238 val &= 0xffff0000;
239
Richard Zhu97e4ba62011-08-11 16:51:46 -0400240 /* In FSL esdhc IC module, only bit20 is used to indicate the
241 * ADMA2 capability of esdhc, but this bit is messed up on
242 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
243 * don't actually support ADMA2). So set the BROKEN_ADMA
244 * uirk on MX25/35 platforms.
245 */
246
247 if (val & SDHCI_CAN_DO_ADMA1) {
248 val &= ~SDHCI_CAN_DO_ADMA1;
249 val |= SDHCI_CAN_DO_ADMA2;
250 }
251 }
252
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800253 if (unlikely(reg == SDHCI_CAPABILITIES_1)) {
254 if (esdhc_is_usdhc(imx_data)) {
255 if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
256 val = readl(host->ioaddr + SDHCI_CAPABILITIES) & 0xFFFF;
257 else
258 /* imx6q/dl does not have cap_1 register, fake one */
259 val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104
260 | SDHCI_SUPPORT_SDR50;
261 }
262 }
Dong Aisheng03221912013-09-13 19:11:34 +0800263
Shawn Guo9d61c002013-10-17 15:19:45 +0800264 if (unlikely(reg == SDHCI_MAX_CURRENT) && esdhc_is_usdhc(imx_data)) {
Dong Aisheng03221912013-09-13 19:11:34 +0800265 val = 0;
266 val |= 0xFF << SDHCI_MAX_CURRENT_330_SHIFT;
267 val |= 0xFF << SDHCI_MAX_CURRENT_300_SHIFT;
268 val |= 0xFF << SDHCI_MAX_CURRENT_180_SHIFT;
269 }
270
Richard Zhu97e4ba62011-08-11 16:51:46 -0400271 if (unlikely(reg == SDHCI_INT_STATUS)) {
Shawn Guo60bf6392013-01-15 23:36:53 +0800272 if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
273 val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
Richard Zhu97e4ba62011-08-11 16:51:46 -0400274 val |= SDHCI_INT_ADMA_ERROR;
275 }
Lucas Stach361b8482013-03-15 09:49:26 +0100276
277 /*
278 * mask off the interrupt we get in response to the manually
279 * sent CMD12
280 */
281 if ((imx_data->multiblock_status == WAIT_FOR_INT) &&
282 ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) {
283 val &= ~SDHCI_INT_RESPONSE;
284 writel(SDHCI_INT_RESPONSE, host->ioaddr +
285 SDHCI_INT_STATUS);
286 imx_data->multiblock_status = NO_CMD_PENDING;
287 }
Richard Zhu97e4ba62011-08-11 16:51:46 -0400288 }
289
Wolfram Sang7e29c302011-02-26 14:44:41 +0100290 return val;
291}
292
293static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
294{
Richard Zhue1498602011-03-25 09:18:27 -0400295 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
296 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Tony Lin0d588642011-08-11 16:45:59 -0400297 u32 data;
Richard Zhue1498602011-03-25 09:18:27 -0400298
Tony Lin0d588642011-08-11 16:45:59 -0400299 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
Tony Lin0d588642011-08-11 16:45:59 -0400300 if (val & SDHCI_INT_CARD_INT) {
301 /*
302 * Clear and then set D3CD bit to avoid missing the
303 * card interrupt. This is a eSDHC controller problem
304 * so we need to apply the following workaround: clear
305 * and set D3CD bit will make eSDHC re-sample the card
306 * interrupt. In case a card interrupt was lost,
307 * re-sample it by the following steps.
308 */
309 data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
Shawn Guo60bf6392013-01-15 23:36:53 +0800310 data &= ~ESDHC_CTRL_D3CD;
Tony Lin0d588642011-08-11 16:45:59 -0400311 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
Shawn Guo60bf6392013-01-15 23:36:53 +0800312 data |= ESDHC_CTRL_D3CD;
Tony Lin0d588642011-08-11 16:45:59 -0400313 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
314 }
315 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100316
Shawn Guof47c4bb2013-10-17 15:19:47 +0800317 if (unlikely((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
Richard Zhu58ac8172011-03-21 13:22:16 +0800318 && (reg == SDHCI_INT_STATUS)
319 && (val & SDHCI_INT_DATA_END))) {
320 u32 v;
Shawn Guo60bf6392013-01-15 23:36:53 +0800321 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
322 v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
323 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
Lucas Stach361b8482013-03-15 09:49:26 +0100324
325 if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS)
326 {
327 /* send a manual CMD12 with RESPTYP=none */
328 data = MMC_STOP_TRANSMISSION << 24 |
329 SDHCI_CMD_ABORTCMD << 16;
330 writel(data, host->ioaddr + SDHCI_TRANSFER_MODE);
331 imx_data->multiblock_status = WAIT_FOR_INT;
332 }
Richard Zhu58ac8172011-03-21 13:22:16 +0800333 }
334
Richard Zhu97e4ba62011-08-11 16:51:46 -0400335 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
336 if (val & SDHCI_INT_ADMA_ERROR) {
337 val &= ~SDHCI_INT_ADMA_ERROR;
Shawn Guo60bf6392013-01-15 23:36:53 +0800338 val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
Richard Zhu97e4ba62011-08-11 16:51:46 -0400339 }
340 }
341
Wolfram Sang7e29c302011-02-26 14:44:41 +0100342 writel(val, host->ioaddr + reg);
343}
344
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200345static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
346{
Shawn Guoef4d0882013-01-15 23:30:27 +0800347 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
348 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aisheng03221912013-09-13 19:11:34 +0800349 u16 ret = 0;
350 u32 val;
Shawn Guoef4d0882013-01-15 23:30:27 +0800351
Shawn Guo95a24822011-09-19 17:32:21 +0800352 if (unlikely(reg == SDHCI_HOST_VERSION)) {
Shawn Guoef4d0882013-01-15 23:30:27 +0800353 reg ^= 2;
Shawn Guo9d61c002013-10-17 15:19:45 +0800354 if (esdhc_is_usdhc(imx_data)) {
Shawn Guoef4d0882013-01-15 23:30:27 +0800355 /*
356 * The usdhc register returns a wrong host version.
357 * Correct it here.
358 */
359 return SDHCI_SPEC_300;
360 }
Shawn Guo95a24822011-09-19 17:32:21 +0800361 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200362
Dong Aisheng03221912013-09-13 19:11:34 +0800363 if (unlikely(reg == SDHCI_HOST_CONTROL2)) {
364 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
365 if (val & ESDHC_VENDOR_SPEC_VSELECT)
366 ret |= SDHCI_CTRL_VDD_180;
367
Shawn Guo9d61c002013-10-17 15:19:45 +0800368 if (esdhc_is_usdhc(imx_data)) {
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800369 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
370 val = readl(host->ioaddr + ESDHC_MIX_CTRL);
371 else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING)
372 /* the std tuning bits is in ACMD12_ERR for imx6sl */
373 val = readl(host->ioaddr + SDHCI_ACMD12_ERR);
Dong Aisheng03221912013-09-13 19:11:34 +0800374 }
375
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800376 if (val & ESDHC_MIX_CTRL_EXE_TUNE)
377 ret |= SDHCI_CTRL_EXEC_TUNING;
378 if (val & ESDHC_MIX_CTRL_SMPCLK_SEL)
379 ret |= SDHCI_CTRL_TUNED_CLK;
380
Dong Aisheng03221912013-09-13 19:11:34 +0800381 ret |= (imx_data->uhs_mode & SDHCI_CTRL_UHS_MASK);
382 ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
383
384 return ret;
385 }
386
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200387 return readw(host->ioaddr + reg);
388}
389
390static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
391{
392 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400393 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aisheng03221912013-09-13 19:11:34 +0800394 u32 new_val = 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200395
396 switch (reg) {
Dong Aisheng03221912013-09-13 19:11:34 +0800397 case SDHCI_CLOCK_CONTROL:
398 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
399 if (val & SDHCI_CLOCK_CARD_EN)
400 new_val |= ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
401 else
402 new_val &= ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
403 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
404 return;
405 case SDHCI_HOST_CONTROL2:
406 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
407 if (val & SDHCI_CTRL_VDD_180)
408 new_val |= ESDHC_VENDOR_SPEC_VSELECT;
409 else
410 new_val &= ~ESDHC_VENDOR_SPEC_VSELECT;
411 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
412 imx_data->uhs_mode = val & SDHCI_CTRL_UHS_MASK;
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800413 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
414 new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
415 if (val & SDHCI_CTRL_TUNED_CLK)
416 new_val |= ESDHC_MIX_CTRL_SMPCLK_SEL;
417 else
418 new_val &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
419 writel(new_val , host->ioaddr + ESDHC_MIX_CTRL);
420 } else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
421 u32 v = readl(host->ioaddr + SDHCI_ACMD12_ERR);
422 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
423 new_val = readl(host->ioaddr + ESDHC_TUNING_CTRL);
424 if (val & SDHCI_CTRL_EXEC_TUNING) {
425 new_val |= ESDHC_STD_TUNING_EN |
426 ESDHC_TUNING_START_TAP;
427 v |= ESDHC_MIX_CTRL_EXE_TUNE;
428 m |= ESDHC_MIX_CTRL_FBCLK_SEL;
429 } else {
430 new_val &= ~ESDHC_STD_TUNING_EN;
431 v &= ~ESDHC_MIX_CTRL_EXE_TUNE;
432 m &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
433 }
434
435 if (val & SDHCI_CTRL_TUNED_CLK)
436 v |= ESDHC_MIX_CTRL_SMPCLK_SEL;
437 else
438 v &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
439
440 writel(new_val, host->ioaddr + ESDHC_TUNING_CTRL);
441 writel(v, host->ioaddr + SDHCI_ACMD12_ERR);
442 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
443 }
Dong Aisheng03221912013-09-13 19:11:34 +0800444 return;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200445 case SDHCI_TRANSFER_MODE:
Shawn Guof47c4bb2013-10-17 15:19:47 +0800446 if ((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
Richard Zhu58ac8172011-03-21 13:22:16 +0800447 && (host->cmd->opcode == SD_IO_RW_EXTENDED)
448 && (host->cmd->data->blocks > 1)
449 && (host->cmd->data->flags & MMC_DATA_READ)) {
450 u32 v;
Shawn Guo60bf6392013-01-15 23:36:53 +0800451 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
452 v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
453 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
Richard Zhu58ac8172011-03-21 13:22:16 +0800454 }
Shawn Guo69f54692013-01-21 19:02:24 +0800455
Shawn Guo9d61c002013-10-17 15:19:45 +0800456 if (esdhc_is_usdhc(imx_data)) {
Shawn Guo69f54692013-01-21 19:02:24 +0800457 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
Shawn Guo2a15f982013-01-21 19:02:26 +0800458 /* 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);
Shawn Guo69f54692013-01-21 19:02:24 +0800464 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 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200472 return;
473 case SDHCI_COMMAND:
Lucas Stach361b8482013-03-15 09:49:26 +0100474 if (host->cmd->opcode == MMC_STOP_TRANSMISSION)
Richard Zhu58ac8172011-03-21 13:22:16 +0800475 val |= SDHCI_CMD_ABORTCMD;
Shawn Guo95a24822011-09-19 17:32:21 +0800476
Lucas Stach361b8482013-03-15 09:49:26 +0100477 if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
Shawn Guof47c4bb2013-10-17 15:19:47 +0800478 (imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
Lucas Stach361b8482013-03-15 09:49:26 +0100479 imx_data->multiblock_status = MULTIBLK_IN_PROCESS;
480
Shawn Guo9d61c002013-10-17 15:19:45 +0800481 if (esdhc_is_usdhc(imx_data))
Shawn Guo95a24822011-09-19 17:32:21 +0800482 writel(val << 16,
483 host->ioaddr + SDHCI_TRANSFER_MODE);
Shawn Guo69f54692013-01-21 19:02:24 +0800484 else
Shawn Guo95a24822011-09-19 17:32:21 +0800485 writel(val << 16 | imx_data->scratchpad,
486 host->ioaddr + SDHCI_TRANSFER_MODE);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200487 return;
488 case SDHCI_BLOCK_SIZE:
489 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
490 break;
491 }
492 esdhc_clrset_le(host, 0xffff, val, reg);
493}
494
495static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
496{
Wilson Callan9a0985b2012-07-19 02:49:16 -0400497 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
498 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200499 u32 new_val;
Sascha Haueraf510792013-01-21 19:02:28 +0800500 u32 mask;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200501
502 switch (reg) {
503 case SDHCI_POWER_CONTROL:
504 /*
505 * FSL put some DMA bits here
506 * If your board has a regulator, code should be here
507 */
508 return;
509 case SDHCI_HOST_CONTROL:
Shawn Guo6b40d182013-01-15 23:36:52 +0800510 /* FSL messed up here, so we need to manually compose it. */
Sascha Haueraf510792013-01-21 19:02:28 +0800511 new_val = val & SDHCI_CTRL_LED;
Masanari Iida7122bbb2012-08-05 23:25:40 +0900512 /* ensure the endianness */
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200513 new_val |= ESDHC_HOST_CONTROL_LE;
Wilson Callan9a0985b2012-07-19 02:49:16 -0400514 /* bits 8&9 are reserved on mx25 */
515 if (!is_imx25_esdhc(imx_data)) {
516 /* DMA mode bits are shifted */
517 new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
518 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200519
Sascha Haueraf510792013-01-21 19:02:28 +0800520 /*
521 * Do not touch buswidth bits here. This is done in
522 * esdhc_pltfm_bus_width.
Martin Fuzzeyf6825742013-04-15 17:08:35 +0200523 * Do not touch the D3CD bit either which is used for the
524 * SDIO interrupt errata workaround.
Sascha Haueraf510792013-01-21 19:02:28 +0800525 */
Martin Fuzzeyf6825742013-04-15 17:08:35 +0200526 mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD);
Sascha Haueraf510792013-01-21 19:02:28 +0800527
528 esdhc_clrset_le(host, mask, new_val, reg);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200529 return;
530 }
531 esdhc_clrset_le(host, 0xff, val, reg);
Shawn Guo913413c2011-06-21 22:41:51 +0800532
533 /*
534 * The esdhc has a design violation to SDHC spec which tells
535 * that software reset should not affect card detection circuit.
536 * But esdhc clears its SYSCTL register bits [0..2] during the
537 * software reset. This will stop those clocks that card detection
538 * circuit relies on. To work around it, we turn the clocks on back
539 * to keep card detection circuit functional.
540 */
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800541 if ((reg == SDHCI_SOFTWARE_RESET) && (val & 1)) {
Shawn Guo913413c2011-06-21 22:41:51 +0800542 esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800543 /*
544 * The reset on usdhc fails to clear MIX_CTRL register.
545 * Do it manually here.
546 */
Dong Aishengde5bdbf2013-10-18 19:48:46 +0800547 if (esdhc_is_usdhc(imx_data)) {
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800548 writel(0, host->ioaddr + ESDHC_MIX_CTRL);
Dong Aishengde5bdbf2013-10-18 19:48:46 +0800549 imx_data->is_ddr = 0;
550 }
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800551 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200552}
553
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200554static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
555{
556 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
557 struct pltfm_imx_data *imx_data = pltfm_host->priv;
558 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
559
560 u32 f_host = clk_get_rate(pltfm_host->clk);
561
562 if (boarddata->f_max && (boarddata->f_max < f_host))
563 return boarddata->f_max;
564 else
565 return f_host;
566}
567
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200568static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
569{
570 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
571
572 return clk_get_rate(pltfm_host->clk) / 256 / 16;
573}
574
Lucas Stach8ba95802013-06-05 15:13:25 +0200575static inline void esdhc_pltfm_set_clock(struct sdhci_host *host,
576 unsigned int clock)
577{
578 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800579 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aishengd31fc002013-09-13 19:11:32 +0800580 unsigned int host_clock = clk_get_rate(pltfm_host->clk);
581 int pre_div = 2;
582 int div = 1;
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800583 u32 temp, val;
Lucas Stach8ba95802013-06-05 15:13:25 +0200584
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800585 if (clock == 0) {
Shawn Guo9d61c002013-10-17 15:19:45 +0800586 if (esdhc_is_usdhc(imx_data)) {
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800587 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
588 writel(val & ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
589 host->ioaddr + ESDHC_VENDOR_SPEC);
590 }
Dong Aishengd31fc002013-09-13 19:11:32 +0800591 goto out;
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800592 }
Dong Aishengd31fc002013-09-13 19:11:32 +0800593
Dong Aishengde5bdbf2013-10-18 19:48:46 +0800594 if (esdhc_is_usdhc(imx_data) && !imx_data->is_ddr)
Dong Aisheng5f7886c2013-09-13 19:11:36 +0800595 pre_div = 1;
596
Dong Aishengd31fc002013-09-13 19:11:32 +0800597 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
598 temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
599 | ESDHC_CLOCK_MASK);
600 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
601
602 while (host_clock / pre_div / 16 > clock && pre_div < 256)
603 pre_div *= 2;
604
605 while (host_clock / pre_div / div > clock && div < 16)
606 div++;
607
Dong Aishenge76b8552013-09-13 19:11:37 +0800608 host->mmc->actual_clock = host_clock / pre_div / div;
Dong Aishengd31fc002013-09-13 19:11:32 +0800609 dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
Dong Aishenge76b8552013-09-13 19:11:37 +0800610 clock, host->mmc->actual_clock);
Dong Aishengd31fc002013-09-13 19:11:32 +0800611
Dong Aishengde5bdbf2013-10-18 19:48:46 +0800612 if (imx_data->is_ddr)
613 pre_div >>= 2;
614 else
615 pre_div >>= 1;
Dong Aishengd31fc002013-09-13 19:11:32 +0800616 div--;
617
618 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
619 temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
620 | (div << ESDHC_DIVIDER_SHIFT)
621 | (pre_div << ESDHC_PREDIV_SHIFT));
622 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800623
Shawn Guo9d61c002013-10-17 15:19:45 +0800624 if (esdhc_is_usdhc(imx_data)) {
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800625 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
626 writel(val | ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
627 host->ioaddr + ESDHC_VENDOR_SPEC);
628 }
629
Dong Aishengd31fc002013-09-13 19:11:32 +0800630 mdelay(1);
631out:
632 host->clock = clock;
Lucas Stach8ba95802013-06-05 15:13:25 +0200633}
634
Shawn Guo913413c2011-06-21 22:41:51 +0800635static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
636{
Shawn Guo842afc02011-07-06 22:57:48 +0800637 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
638 struct pltfm_imx_data *imx_data = pltfm_host->priv;
639 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Shawn Guo913413c2011-06-21 22:41:51 +0800640
641 switch (boarddata->wp_type) {
642 case ESDHC_WP_GPIO:
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800643 return mmc_gpio_get_ro(host->mmc);
Shawn Guo913413c2011-06-21 22:41:51 +0800644 case ESDHC_WP_CONTROLLER:
645 return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
646 SDHCI_WRITE_PROTECT);
647 case ESDHC_WP_NONE:
648 break;
649 }
650
651 return -ENOSYS;
652}
653
Sascha Haueraf510792013-01-21 19:02:28 +0800654static int esdhc_pltfm_bus_width(struct sdhci_host *host, int width)
655{
656 u32 ctrl;
657
658 switch (width) {
659 case MMC_BUS_WIDTH_8:
660 ctrl = ESDHC_CTRL_8BITBUS;
661 break;
662 case MMC_BUS_WIDTH_4:
663 ctrl = ESDHC_CTRL_4BITBUS;
664 break;
665 default:
666 ctrl = 0;
667 break;
668 }
669
670 esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
671 SDHCI_HOST_CONTROL);
672
673 return 0;
674}
675
Dong Aisheng03221912013-09-13 19:11:34 +0800676static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val)
677{
678 u32 reg;
679
680 /* FIXME: delay a bit for card to be ready for next tuning due to errors */
681 mdelay(1);
682
683 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
684 reg |= ESDHC_MIX_CTRL_EXE_TUNE | ESDHC_MIX_CTRL_SMPCLK_SEL |
685 ESDHC_MIX_CTRL_FBCLK_SEL;
686 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
687 writel(val << 8, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
688 dev_dbg(mmc_dev(host->mmc),
689 "tunning with delay 0x%x ESDHC_TUNE_CTRL_STATUS 0x%x\n",
690 val, readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS));
691}
692
693static void esdhc_request_done(struct mmc_request *mrq)
694{
695 complete(&mrq->completion);
696}
697
698static int esdhc_send_tuning_cmd(struct sdhci_host *host, u32 opcode)
699{
700 struct mmc_command cmd = {0};
701 struct mmc_request mrq = {0};
702 struct mmc_data data = {0};
703 struct scatterlist sg;
704 char tuning_pattern[ESDHC_TUNING_BLOCK_PATTERN_LEN];
705
706 cmd.opcode = opcode;
707 cmd.arg = 0;
708 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
709
710 data.blksz = ESDHC_TUNING_BLOCK_PATTERN_LEN;
711 data.blocks = 1;
712 data.flags = MMC_DATA_READ;
713 data.sg = &sg;
714 data.sg_len = 1;
715
716 sg_init_one(&sg, tuning_pattern, sizeof(tuning_pattern));
717
718 mrq.cmd = &cmd;
719 mrq.cmd->mrq = &mrq;
720 mrq.data = &data;
721 mrq.data->mrq = &mrq;
722 mrq.cmd->data = mrq.data;
723
724 mrq.done = esdhc_request_done;
725 init_completion(&(mrq.completion));
726
727 disable_irq(host->irq);
728 spin_lock(&host->lock);
729 host->mrq = &mrq;
730
731 sdhci_send_command(host, mrq.cmd);
732
733 spin_unlock(&host->lock);
734 enable_irq(host->irq);
735
736 wait_for_completion(&mrq.completion);
737
738 if (cmd.error)
739 return cmd.error;
740 if (data.error)
741 return data.error;
742
743 return 0;
744}
745
746static void esdhc_post_tuning(struct sdhci_host *host)
747{
748 u32 reg;
749
750 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
751 reg &= ~ESDHC_MIX_CTRL_EXE_TUNE;
752 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
753}
754
755static int esdhc_executing_tuning(struct sdhci_host *host, u32 opcode)
756{
757 int min, max, avg, ret;
758
759 /* find the mininum delay first which can pass tuning */
760 min = ESDHC_TUNE_CTRL_MIN;
761 while (min < ESDHC_TUNE_CTRL_MAX) {
762 esdhc_prepare_tuning(host, min);
763 if (!esdhc_send_tuning_cmd(host, opcode))
764 break;
765 min += ESDHC_TUNE_CTRL_STEP;
766 }
767
768 /* find the maxinum delay which can not pass tuning */
769 max = min + ESDHC_TUNE_CTRL_STEP;
770 while (max < ESDHC_TUNE_CTRL_MAX) {
771 esdhc_prepare_tuning(host, max);
772 if (esdhc_send_tuning_cmd(host, opcode)) {
773 max -= ESDHC_TUNE_CTRL_STEP;
774 break;
775 }
776 max += ESDHC_TUNE_CTRL_STEP;
777 }
778
779 /* use average delay to get the best timing */
780 avg = (min + max) / 2;
781 esdhc_prepare_tuning(host, avg);
782 ret = esdhc_send_tuning_cmd(host, opcode);
783 esdhc_post_tuning(host);
784
785 dev_dbg(mmc_dev(host->mmc), "tunning %s at 0x%x ret %d\n",
786 ret ? "failed" : "passed", avg, ret);
787
788 return ret;
789}
790
Dong Aishengad932202013-09-13 19:11:35 +0800791static int esdhc_change_pinstate(struct sdhci_host *host,
792 unsigned int uhs)
793{
794 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
795 struct pltfm_imx_data *imx_data = pltfm_host->priv;
796 struct pinctrl_state *pinctrl;
797
798 dev_dbg(mmc_dev(host->mmc), "change pinctrl state for uhs %d\n", uhs);
799
800 if (IS_ERR(imx_data->pinctrl) ||
801 IS_ERR(imx_data->pins_default) ||
802 IS_ERR(imx_data->pins_100mhz) ||
803 IS_ERR(imx_data->pins_200mhz))
804 return -EINVAL;
805
806 switch (uhs) {
807 case MMC_TIMING_UHS_SDR50:
808 pinctrl = imx_data->pins_100mhz;
809 break;
810 case MMC_TIMING_UHS_SDR104:
811 pinctrl = imx_data->pins_200mhz;
812 break;
813 default:
814 /* back to default state for other legacy timing */
815 pinctrl = imx_data->pins_default;
816 }
817
818 return pinctrl_select_state(imx_data->pinctrl, pinctrl);
819}
820
821static int esdhc_set_uhs_signaling(struct sdhci_host *host, unsigned int uhs)
822{
823 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
824 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aisheng602519b2013-10-18 19:48:47 +0800825 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Dong Aishengad932202013-09-13 19:11:35 +0800826
827 switch (uhs) {
828 case MMC_TIMING_UHS_SDR12:
829 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR12;
830 break;
831 case MMC_TIMING_UHS_SDR25:
832 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR25;
833 break;
834 case MMC_TIMING_UHS_SDR50:
835 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR50;
836 break;
837 case MMC_TIMING_UHS_SDR104:
838 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR104;
839 break;
840 case MMC_TIMING_UHS_DDR50:
841 imx_data->uhs_mode = SDHCI_CTRL_UHS_DDR50;
Dong Aishengde5bdbf2013-10-18 19:48:46 +0800842 writel(readl(host->ioaddr + ESDHC_MIX_CTRL) |
843 ESDHC_MIX_CTRL_DDREN,
844 host->ioaddr + ESDHC_MIX_CTRL);
845 imx_data->is_ddr = 1;
Dong Aisheng602519b2013-10-18 19:48:47 +0800846 if (boarddata->delay_line) {
847 u32 v;
848 v = boarddata->delay_line <<
849 ESDHC_DLL_OVERRIDE_VAL_SHIFT |
850 (1 << ESDHC_DLL_OVERRIDE_EN_SHIFT);
851 if (is_imx53_esdhc(imx_data))
852 v <<= 1;
853 writel(v, host->ioaddr + ESDHC_DLL_CTRL);
854 }
Dong Aishengad932202013-09-13 19:11:35 +0800855 break;
856 }
857
858 return esdhc_change_pinstate(host, uhs);
859}
860
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800861static struct sdhci_ops sdhci_esdhc_ops = {
Richard Zhue1498602011-03-25 09:18:27 -0400862 .read_l = esdhc_readl_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100863 .read_w = esdhc_readw_le,
Richard Zhue1498602011-03-25 09:18:27 -0400864 .write_l = esdhc_writel_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100865 .write_w = esdhc_writew_le,
866 .write_b = esdhc_writeb_le,
Lucas Stach8ba95802013-06-05 15:13:25 +0200867 .set_clock = esdhc_pltfm_set_clock,
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200868 .get_max_clock = esdhc_pltfm_get_max_clock,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100869 .get_min_clock = esdhc_pltfm_get_min_clock,
Shawn Guo913413c2011-06-21 22:41:51 +0800870 .get_ro = esdhc_pltfm_get_ro,
Sascha Haueraf510792013-01-21 19:02:28 +0800871 .platform_bus_width = esdhc_pltfm_bus_width,
Dong Aishengad932202013-09-13 19:11:35 +0800872 .set_uhs_signaling = esdhc_set_uhs_signaling,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100873};
874
Lars-Peter Clausen1db5eeb2013-03-13 19:26:03 +0100875static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
Richard Zhu97e4ba62011-08-11 16:51:46 -0400876 .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
877 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
878 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
Shawn Guo85d65092011-05-27 23:48:12 +0800879 | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
Shawn Guo85d65092011-05-27 23:48:12 +0800880 .ops = &sdhci_esdhc_ops,
881};
882
Shawn Guoabfafc22011-06-30 15:44:44 +0800883#ifdef CONFIG_OF
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500884static int
Shawn Guoabfafc22011-06-30 15:44:44 +0800885sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
886 struct esdhc_platform_data *boarddata)
887{
888 struct device_node *np = pdev->dev.of_node;
889
890 if (!np)
891 return -ENODEV;
892
Arnd Bergmann7f217792012-05-13 00:14:24 -0400893 if (of_get_property(np, "non-removable", NULL))
Shawn Guoabfafc22011-06-30 15:44:44 +0800894 boarddata->cd_type = ESDHC_CD_PERMANENT;
895
896 if (of_get_property(np, "fsl,cd-controller", NULL))
897 boarddata->cd_type = ESDHC_CD_CONTROLLER;
898
899 if (of_get_property(np, "fsl,wp-controller", NULL))
900 boarddata->wp_type = ESDHC_WP_CONTROLLER;
901
902 boarddata->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
903 if (gpio_is_valid(boarddata->cd_gpio))
904 boarddata->cd_type = ESDHC_CD_GPIO;
905
906 boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
907 if (gpio_is_valid(boarddata->wp_gpio))
908 boarddata->wp_type = ESDHC_WP_GPIO;
909
Sascha Haueraf510792013-01-21 19:02:28 +0800910 of_property_read_u32(np, "bus-width", &boarddata->max_bus_width);
911
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200912 of_property_read_u32(np, "max-frequency", &boarddata->f_max);
913
Dong Aishengad932202013-09-13 19:11:35 +0800914 if (of_find_property(np, "no-1-8-v", NULL))
915 boarddata->support_vsel = false;
916 else
917 boarddata->support_vsel = true;
918
Dong Aisheng602519b2013-10-18 19:48:47 +0800919 if (of_property_read_u32(np, "fsl,delay-line", &boarddata->delay_line))
920 boarddata->delay_line = 0;
921
Shawn Guoabfafc22011-06-30 15:44:44 +0800922 return 0;
923}
924#else
925static inline int
926sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
927 struct esdhc_platform_data *boarddata)
928{
929 return -ENODEV;
930}
931#endif
932
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500933static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200934{
Shawn Guoabfafc22011-06-30 15:44:44 +0800935 const struct of_device_id *of_id =
936 of_match_device(imx_esdhc_dt_ids, &pdev->dev);
Shawn Guo85d65092011-05-27 23:48:12 +0800937 struct sdhci_pltfm_host *pltfm_host;
938 struct sdhci_host *host;
939 struct esdhc_platform_data *boarddata;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100940 int err;
Richard Zhue1498602011-03-25 09:18:27 -0400941 struct pltfm_imx_data *imx_data;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200942
Christian Daudt0e748232013-05-29 13:50:05 -0700943 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata, 0);
Shawn Guo85d65092011-05-27 23:48:12 +0800944 if (IS_ERR(host))
945 return PTR_ERR(host);
946
947 pltfm_host = sdhci_priv(host);
948
Shawn Guoe3af31c2012-11-26 14:39:43 +0800949 imx_data = devm_kzalloc(&pdev->dev, sizeof(*imx_data), GFP_KERNEL);
Shawn Guoabfafc22011-06-30 15:44:44 +0800950 if (!imx_data) {
951 err = -ENOMEM;
Shawn Guoe3af31c2012-11-26 14:39:43 +0800952 goto free_sdhci;
Shawn Guoabfafc22011-06-30 15:44:44 +0800953 }
Shawn Guo57ed3312011-06-30 09:24:26 +0800954
Shawn Guof47c4bb2013-10-17 15:19:47 +0800955 imx_data->socdata = of_id ? of_id->data : (struct esdhc_soc_data *)
956 pdev->id_entry->driver_data;
Shawn Guo85d65092011-05-27 23:48:12 +0800957 pltfm_host->priv = imx_data;
958
Sascha Hauer52dac612012-03-07 09:31:34 +0100959 imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
960 if (IS_ERR(imx_data->clk_ipg)) {
961 err = PTR_ERR(imx_data->clk_ipg);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800962 goto free_sdhci;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200963 }
Sascha Hauer52dac612012-03-07 09:31:34 +0100964
965 imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
966 if (IS_ERR(imx_data->clk_ahb)) {
967 err = PTR_ERR(imx_data->clk_ahb);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800968 goto free_sdhci;
Sascha Hauer52dac612012-03-07 09:31:34 +0100969 }
970
971 imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
972 if (IS_ERR(imx_data->clk_per)) {
973 err = PTR_ERR(imx_data->clk_per);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800974 goto free_sdhci;
Sascha Hauer52dac612012-03-07 09:31:34 +0100975 }
976
977 pltfm_host->clk = imx_data->clk_per;
978
979 clk_prepare_enable(imx_data->clk_per);
980 clk_prepare_enable(imx_data->clk_ipg);
981 clk_prepare_enable(imx_data->clk_ahb);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200982
Dong Aishengad932202013-09-13 19:11:35 +0800983 imx_data->pinctrl = devm_pinctrl_get(&pdev->dev);
Dong Aishenge62d8b82012-05-11 14:56:01 +0800984 if (IS_ERR(imx_data->pinctrl)) {
985 err = PTR_ERR(imx_data->pinctrl);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800986 goto disable_clk;
Dong Aishenge62d8b82012-05-11 14:56:01 +0800987 }
988
Dong Aishengad932202013-09-13 19:11:35 +0800989 imx_data->pins_default = pinctrl_lookup_state(imx_data->pinctrl,
990 PINCTRL_STATE_DEFAULT);
991 if (IS_ERR(imx_data->pins_default)) {
992 err = PTR_ERR(imx_data->pins_default);
993 dev_err(mmc_dev(host->mmc), "could not get default state\n");
994 goto disable_clk;
995 }
996
Eric Bénardb89152822012-04-18 02:30:20 +0200997 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
Eric Bénard37865fe2010-10-23 01:57:21 +0200998
Shawn Guof47c4bb2013-10-17 15:19:47 +0800999 if (imx_data->socdata->flags & ESDHC_FLAG_ENGCM07207)
Wolfram Sang0c6d49c2011-02-26 14:44:39 +01001000 /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
Richard Zhu97e4ba62011-08-11 16:51:46 -04001001 host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK
1002 | SDHCI_QUIRK_BROKEN_ADMA;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +01001003
Shawn Guof750ba92011-11-10 16:39:32 +08001004 /*
1005 * The imx6q ROM code will change the default watermark level setting
1006 * to something insane. Change it back here.
1007 */
Shawn Guo9d61c002013-10-17 15:19:45 +08001008 if (esdhc_is_usdhc(imx_data))
Shawn Guo60bf6392013-01-15 23:36:53 +08001009 writel(0x08100810, host->ioaddr + ESDHC_WTMK_LVL);
Shawn Guof750ba92011-11-10 16:39:32 +08001010
Dong Aisheng6e9fd282013-10-18 19:48:43 +08001011 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
1012 sdhci_esdhc_ops.platform_execute_tuning =
1013 esdhc_executing_tuning;
Shawn Guo842afc02011-07-06 22:57:48 +08001014 boarddata = &imx_data->boarddata;
Shawn Guoabfafc22011-06-30 15:44:44 +08001015 if (sdhci_esdhc_imx_probe_dt(pdev, boarddata) < 0) {
1016 if (!host->mmc->parent->platform_data) {
1017 dev_err(mmc_dev(host->mmc), "no board data!\n");
1018 err = -EINVAL;
Shawn Guoe3af31c2012-11-26 14:39:43 +08001019 goto disable_clk;
Shawn Guoabfafc22011-06-30 15:44:44 +08001020 }
1021 imx_data->boarddata = *((struct esdhc_platform_data *)
1022 host->mmc->parent->platform_data);
1023 }
Shawn Guo913413c2011-06-21 22:41:51 +08001024
1025 /* write_protect */
1026 if (boarddata->wp_type == ESDHC_WP_GPIO) {
Shawn Guofbe5fdd2012-12-11 22:32:20 +08001027 err = mmc_gpio_request_ro(host->mmc, boarddata->wp_gpio);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +01001028 if (err) {
Shawn Guofbe5fdd2012-12-11 22:32:20 +08001029 dev_err(mmc_dev(host->mmc),
1030 "failed to request write-protect gpio!\n");
1031 goto disable_clk;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +01001032 }
Shawn Guofbe5fdd2012-12-11 22:32:20 +08001033 host->mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
Shawn Guo913413c2011-06-21 22:41:51 +08001034 }
Wolfram Sang7e29c302011-02-26 14:44:41 +01001035
Shawn Guo913413c2011-06-21 22:41:51 +08001036 /* card_detect */
Shawn Guo913413c2011-06-21 22:41:51 +08001037 switch (boarddata->cd_type) {
1038 case ESDHC_CD_GPIO:
Laurent Pinchart214fc302013-08-08 12:38:31 +02001039 err = mmc_gpio_request_cd(host->mmc, boarddata->cd_gpio, 0);
Wolfram Sang7e29c302011-02-26 14:44:41 +01001040 if (err) {
Shawn Guo913413c2011-06-21 22:41:51 +08001041 dev_err(mmc_dev(host->mmc),
Shawn Guofbe5fdd2012-12-11 22:32:20 +08001042 "failed to request card-detect gpio!\n");
Shawn Guoe3af31c2012-11-26 14:39:43 +08001043 goto disable_clk;
Wolfram Sang7e29c302011-02-26 14:44:41 +01001044 }
Shawn Guo913413c2011-06-21 22:41:51 +08001045 /* fall through */
Wolfram Sang7e29c302011-02-26 14:44:41 +01001046
Shawn Guo913413c2011-06-21 22:41:51 +08001047 case ESDHC_CD_CONTROLLER:
1048 /* we have a working card_detect back */
Wolfram Sang7e29c302011-02-26 14:44:41 +01001049 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
Shawn Guo913413c2011-06-21 22:41:51 +08001050 break;
1051
1052 case ESDHC_CD_PERMANENT:
1053 host->mmc->caps = MMC_CAP_NONREMOVABLE;
1054 break;
1055
1056 case ESDHC_CD_NONE:
1057 break;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +01001058 }
Eric Bénard16a790b2010-10-23 01:57:22 +02001059
Sascha Haueraf510792013-01-21 19:02:28 +08001060 switch (boarddata->max_bus_width) {
1061 case 8:
1062 host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA;
1063 break;
1064 case 4:
1065 host->mmc->caps |= MMC_CAP_4_BIT_DATA;
1066 break;
1067 case 1:
1068 default:
1069 host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
1070 break;
1071 }
1072
Dong Aishengad932202013-09-13 19:11:35 +08001073 /* sdr50 and sdr104 needs work on 1.8v signal voltage */
Shawn Guo9d61c002013-10-17 15:19:45 +08001074 if ((boarddata->support_vsel) && esdhc_is_usdhc(imx_data)) {
Dong Aishengad932202013-09-13 19:11:35 +08001075 imx_data->pins_100mhz = pinctrl_lookup_state(imx_data->pinctrl,
1076 ESDHC_PINCTRL_STATE_100MHZ);
1077 imx_data->pins_200mhz = pinctrl_lookup_state(imx_data->pinctrl,
1078 ESDHC_PINCTRL_STATE_200MHZ);
1079 if (IS_ERR(imx_data->pins_100mhz) ||
1080 IS_ERR(imx_data->pins_200mhz)) {
1081 dev_warn(mmc_dev(host->mmc),
1082 "could not get ultra high speed state, work on normal mode\n");
1083 /* fall back to not support uhs by specify no 1.8v quirk */
1084 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1085 }
1086 } else {
1087 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1088 }
1089
Shawn Guo85d65092011-05-27 23:48:12 +08001090 err = sdhci_add_host(host);
1091 if (err)
Shawn Guoe3af31c2012-11-26 14:39:43 +08001092 goto disable_clk;
Shawn Guo85d65092011-05-27 23:48:12 +08001093
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001094 return 0;
Wolfram Sang7e29c302011-02-26 14:44:41 +01001095
Shawn Guoe3af31c2012-11-26 14:39:43 +08001096disable_clk:
Sascha Hauer52dac612012-03-07 09:31:34 +01001097 clk_disable_unprepare(imx_data->clk_per);
1098 clk_disable_unprepare(imx_data->clk_ipg);
1099 clk_disable_unprepare(imx_data->clk_ahb);
Shawn Guoe3af31c2012-11-26 14:39:43 +08001100free_sdhci:
Shawn Guo85d65092011-05-27 23:48:12 +08001101 sdhci_pltfm_free(pdev);
1102 return err;
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001103}
1104
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001105static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001106{
Shawn Guo85d65092011-05-27 23:48:12 +08001107 struct sdhci_host *host = platform_get_drvdata(pdev);
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001108 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -04001109 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Shawn Guo85d65092011-05-27 23:48:12 +08001110 int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
1111
1112 sdhci_remove_host(host, dead);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +01001113
Sascha Hauer52dac612012-03-07 09:31:34 +01001114 clk_disable_unprepare(imx_data->clk_per);
1115 clk_disable_unprepare(imx_data->clk_ipg);
1116 clk_disable_unprepare(imx_data->clk_ahb);
1117
Shawn Guo85d65092011-05-27 23:48:12 +08001118 sdhci_pltfm_free(pdev);
1119
1120 return 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001121}
1122
Shawn Guo85d65092011-05-27 23:48:12 +08001123static struct platform_driver sdhci_esdhc_imx_driver = {
1124 .driver = {
1125 .name = "sdhci-esdhc-imx",
1126 .owner = THIS_MODULE,
Shawn Guoabfafc22011-06-30 15:44:44 +08001127 .of_match_table = imx_esdhc_dt_ids,
Manuel Lauss29495aa2011-11-03 11:09:45 +01001128 .pm = SDHCI_PLTFM_PMOPS,
Shawn Guo85d65092011-05-27 23:48:12 +08001129 },
Shawn Guo57ed3312011-06-30 09:24:26 +08001130 .id_table = imx_esdhc_devtype,
Shawn Guo85d65092011-05-27 23:48:12 +08001131 .probe = sdhci_esdhc_imx_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05001132 .remove = sdhci_esdhc_imx_remove,
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001133};
Shawn Guo85d65092011-05-27 23:48:12 +08001134
Axel Lind1f81a62011-11-26 12:55:43 +08001135module_platform_driver(sdhci_esdhc_imx_driver);
Shawn Guo85d65092011-05-27 23:48:12 +08001136
1137MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
1138MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
1139MODULE_LICENSE("GPL v2");