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