blob: 3b9c94f74ed07ab91c0b4c80ef7af480ff638684 [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
Shawn Guo2a15f982013-01-21 19:02:26 +080041#define ESDHC_MIX_CTRL_AC23EN (1 << 7)
Dong Aisheng03221912013-09-13 19:11:34 +080042#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)
Shawn Guo2a15f982013-01-21 19:02:26 +080045/* Bits 3 and 6 are not SDHCI standard definitions */
46#define ESDHC_MIX_CTRL_SDHCI_MASK 0xb7
Richard Zhu58ac8172011-03-21 13:22:16 +080047
Dong Aisheng03221912013-09-13 19:11:34 +080048/* 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
Dong Aisheng6e9fd282013-10-18 19:48:43 +080054#define ESDHC_TUNING_CTRL 0xcc
55#define ESDHC_STD_TUNING_EN (1 << 24)
56/* NOTE: the minimum valid tuning start tap for mx6sl is 1 */
57#define ESDHC_TUNING_START_TAP 0x1
58
Dong Aisheng03221912013-09-13 19:11:34 +080059#define ESDHC_TUNING_BLOCK_PATTERN_LEN 64
60
Dong Aishengad932202013-09-13 19:11:35 +080061/* pinctrl state */
62#define ESDHC_PINCTRL_STATE_100MHZ "state_100mhz"
63#define ESDHC_PINCTRL_STATE_200MHZ "state_200mhz"
64
Richard Zhu58ac8172011-03-21 13:22:16 +080065/*
Sascha Haueraf510792013-01-21 19:02:28 +080066 * Our interpretation of the SDHCI_HOST_CONTROL register
67 */
68#define ESDHC_CTRL_4BITBUS (0x1 << 1)
69#define ESDHC_CTRL_8BITBUS (0x2 << 1)
70#define ESDHC_CTRL_BUSWIDTH_MASK (0x3 << 1)
71
72/*
Richard Zhu97e4ba62011-08-11 16:51:46 -040073 * There is an INT DMA ERR mis-match between eSDHC and STD SDHC SPEC:
74 * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
75 * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
76 * Define this macro DMA error INT for fsl eSDHC
77 */
Shawn Guo60bf6392013-01-15 23:36:53 +080078#define ESDHC_INT_VENDOR_SPEC_DMA_ERR (1 << 28)
Richard Zhu97e4ba62011-08-11 16:51:46 -040079
80/*
Richard Zhu58ac8172011-03-21 13:22:16 +080081 * The CMDTYPE of the CMD register (offset 0xE) should be set to
82 * "11" when the STOP CMD12 is issued on imx53 to abort one
83 * open ended multi-blk IO. Otherwise the TC INT wouldn't
84 * be generated.
85 * In exact block transfer, the controller doesn't complete the
86 * operations automatically as required at the end of the
87 * transfer and remains on hold if the abort command is not sent.
88 * As a result, the TC flag is not asserted and SW received timeout
89 * exeception. Bit1 of Vendor Spec registor is used to fix it.
90 */
Shawn Guo31fbb302013-10-17 15:19:44 +080091#define ESDHC_FLAG_MULTIBLK_NO_INT BIT(1)
92/*
93 * The flag enables the workaround for ESDHC errata ENGcm07207 which
94 * affects i.MX25 and i.MX35.
95 */
96#define ESDHC_FLAG_ENGCM07207 BIT(2)
Shawn Guo9d61c002013-10-17 15:19:45 +080097/*
98 * The flag tells that the ESDHC controller is an USDHC block that is
99 * integrated on the i.MX6 series.
100 */
101#define ESDHC_FLAG_USDHC BIT(3)
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800102/* The IP supports manual tuning process */
103#define ESDHC_FLAG_MAN_TUNING BIT(4)
104/* The IP supports standard tuning process */
105#define ESDHC_FLAG_STD_TUNING BIT(5)
106/* The IP has SDHCI_CAPABILITIES_1 register */
107#define ESDHC_FLAG_HAVE_CAP1 BIT(6)
Richard Zhue1498602011-03-25 09:18:27 -0400108
Shawn Guof47c4bb2013-10-17 15:19:47 +0800109struct esdhc_soc_data {
110 u32 flags;
111};
112
113static struct esdhc_soc_data esdhc_imx25_data = {
114 .flags = ESDHC_FLAG_ENGCM07207,
115};
116
117static struct esdhc_soc_data esdhc_imx35_data = {
118 .flags = ESDHC_FLAG_ENGCM07207,
119};
120
121static struct esdhc_soc_data esdhc_imx51_data = {
122 .flags = 0,
123};
124
125static struct esdhc_soc_data esdhc_imx53_data = {
126 .flags = ESDHC_FLAG_MULTIBLK_NO_INT,
127};
128
129static struct esdhc_soc_data usdhc_imx6q_data = {
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800130 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING,
131};
132
133static struct esdhc_soc_data usdhc_imx6sl_data = {
134 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
135 | ESDHC_FLAG_HAVE_CAP1,
Shawn Guo57ed3312011-06-30 09:24:26 +0800136};
137
Richard Zhue1498602011-03-25 09:18:27 -0400138struct pltfm_imx_data {
Richard Zhue1498602011-03-25 09:18:27 -0400139 u32 scratchpad;
Dong Aishenge62d8b82012-05-11 14:56:01 +0800140 struct pinctrl *pinctrl;
Dong Aishengad932202013-09-13 19:11:35 +0800141 struct pinctrl_state *pins_default;
142 struct pinctrl_state *pins_100mhz;
143 struct pinctrl_state *pins_200mhz;
Shawn Guof47c4bb2013-10-17 15:19:47 +0800144 const struct esdhc_soc_data *socdata;
Shawn Guo842afc02011-07-06 22:57:48 +0800145 struct esdhc_platform_data boarddata;
Sascha Hauer52dac612012-03-07 09:31:34 +0100146 struct clk *clk_ipg;
147 struct clk *clk_ahb;
148 struct clk *clk_per;
Lucas Stach361b8482013-03-15 09:49:26 +0100149 enum {
150 NO_CMD_PENDING, /* no multiblock command pending*/
151 MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */
152 WAIT_FOR_INT, /* sent CMD12, waiting for response INT */
153 } multiblock_status;
Dong Aisheng03221912013-09-13 19:11:34 +0800154 u32 uhs_mode;
Richard Zhue1498602011-03-25 09:18:27 -0400155};
156
Shawn Guo57ed3312011-06-30 09:24:26 +0800157static struct platform_device_id imx_esdhc_devtype[] = {
158 {
159 .name = "sdhci-esdhc-imx25",
Shawn Guof47c4bb2013-10-17 15:19:47 +0800160 .driver_data = (kernel_ulong_t) &esdhc_imx25_data,
Shawn Guo57ed3312011-06-30 09:24:26 +0800161 }, {
162 .name = "sdhci-esdhc-imx35",
Shawn Guof47c4bb2013-10-17 15:19:47 +0800163 .driver_data = (kernel_ulong_t) &esdhc_imx35_data,
Shawn Guo57ed3312011-06-30 09:24:26 +0800164 }, {
165 .name = "sdhci-esdhc-imx51",
Shawn Guof47c4bb2013-10-17 15:19:47 +0800166 .driver_data = (kernel_ulong_t) &esdhc_imx51_data,
Shawn Guo57ed3312011-06-30 09:24:26 +0800167 }, {
Shawn Guo57ed3312011-06-30 09:24:26 +0800168 /* sentinel */
169 }
170};
171MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
172
Shawn Guoabfafc22011-06-30 15:44:44 +0800173static const struct of_device_id imx_esdhc_dt_ids[] = {
Shawn Guof47c4bb2013-10-17 15:19:47 +0800174 { .compatible = "fsl,imx25-esdhc", .data = &esdhc_imx25_data, },
175 { .compatible = "fsl,imx35-esdhc", .data = &esdhc_imx35_data, },
176 { .compatible = "fsl,imx51-esdhc", .data = &esdhc_imx51_data, },
177 { .compatible = "fsl,imx53-esdhc", .data = &esdhc_imx53_data, },
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800178 { .compatible = "fsl,imx6sl-usdhc", .data = &usdhc_imx6sl_data, },
Shawn Guof47c4bb2013-10-17 15:19:47 +0800179 { .compatible = "fsl,imx6q-usdhc", .data = &usdhc_imx6q_data, },
Shawn Guoabfafc22011-06-30 15:44:44 +0800180 { /* sentinel */ }
181};
182MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
183
Shawn Guo57ed3312011-06-30 09:24:26 +0800184static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
185{
Shawn Guof47c4bb2013-10-17 15:19:47 +0800186 return data->socdata == &esdhc_imx25_data;
Shawn Guo57ed3312011-06-30 09:24:26 +0800187}
188
189static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
190{
Shawn Guof47c4bb2013-10-17 15:19:47 +0800191 return data->socdata == &esdhc_imx53_data;
Shawn Guo57ed3312011-06-30 09:24:26 +0800192}
193
Shawn Guo95a24822011-09-19 17:32:21 +0800194static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
195{
Shawn Guof47c4bb2013-10-17 15:19:47 +0800196 return data->socdata == &usdhc_imx6q_data;
Shawn Guo95a24822011-09-19 17:32:21 +0800197}
198
Shawn Guo9d61c002013-10-17 15:19:45 +0800199static inline int esdhc_is_usdhc(struct pltfm_imx_data *data)
200{
Shawn Guof47c4bb2013-10-17 15:19:47 +0800201 return !!(data->socdata->flags & ESDHC_FLAG_USDHC);
Shawn Guo9d61c002013-10-17 15:19:45 +0800202}
203
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200204static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
205{
206 void __iomem *base = host->ioaddr + (reg & ~0x3);
207 u32 shift = (reg & 0x3) * 8;
208
209 writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
210}
211
Wolfram Sang7e29c302011-02-26 14:44:41 +0100212static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
213{
Lucas Stach361b8482013-03-15 09:49:26 +0100214 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
215 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100216 u32 val = readl(host->ioaddr + reg);
217
Dong Aisheng03221912013-09-13 19:11:34 +0800218 if (unlikely(reg == SDHCI_PRESENT_STATE)) {
219 u32 fsl_prss = val;
220 /* save the least 20 bits */
221 val = fsl_prss & 0x000FFFFF;
222 /* move dat[0-3] bits */
223 val |= (fsl_prss & 0x0F000000) >> 4;
224 /* move cmd line bit */
225 val |= (fsl_prss & 0x00800000) << 1;
226 }
227
Richard Zhu97e4ba62011-08-11 16:51:46 -0400228 if (unlikely(reg == SDHCI_CAPABILITIES)) {
229 /* In FSL esdhc IC module, only bit20 is used to indicate the
230 * ADMA2 capability of esdhc, but this bit is messed up on
231 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
232 * don't actually support ADMA2). So set the BROKEN_ADMA
233 * uirk on MX25/35 platforms.
234 */
235
236 if (val & SDHCI_CAN_DO_ADMA1) {
237 val &= ~SDHCI_CAN_DO_ADMA1;
238 val |= SDHCI_CAN_DO_ADMA2;
239 }
240 }
241
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800242 if (unlikely(reg == SDHCI_CAPABILITIES_1)) {
243 if (esdhc_is_usdhc(imx_data)) {
244 if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
245 val = readl(host->ioaddr + SDHCI_CAPABILITIES) & 0xFFFF;
246 else
247 /* imx6q/dl does not have cap_1 register, fake one */
248 val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104
249 | SDHCI_SUPPORT_SDR50;
250 }
251 }
Dong Aisheng03221912013-09-13 19:11:34 +0800252
Shawn Guo9d61c002013-10-17 15:19:45 +0800253 if (unlikely(reg == SDHCI_MAX_CURRENT) && esdhc_is_usdhc(imx_data)) {
Dong Aisheng03221912013-09-13 19:11:34 +0800254 val = 0;
255 val |= 0xFF << SDHCI_MAX_CURRENT_330_SHIFT;
256 val |= 0xFF << SDHCI_MAX_CURRENT_300_SHIFT;
257 val |= 0xFF << SDHCI_MAX_CURRENT_180_SHIFT;
258 }
259
Richard Zhu97e4ba62011-08-11 16:51:46 -0400260 if (unlikely(reg == SDHCI_INT_STATUS)) {
Shawn Guo60bf6392013-01-15 23:36:53 +0800261 if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
262 val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
Richard Zhu97e4ba62011-08-11 16:51:46 -0400263 val |= SDHCI_INT_ADMA_ERROR;
264 }
Lucas Stach361b8482013-03-15 09:49:26 +0100265
266 /*
267 * mask off the interrupt we get in response to the manually
268 * sent CMD12
269 */
270 if ((imx_data->multiblock_status == WAIT_FOR_INT) &&
271 ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) {
272 val &= ~SDHCI_INT_RESPONSE;
273 writel(SDHCI_INT_RESPONSE, host->ioaddr +
274 SDHCI_INT_STATUS);
275 imx_data->multiblock_status = NO_CMD_PENDING;
276 }
Richard Zhu97e4ba62011-08-11 16:51:46 -0400277 }
278
Wolfram Sang7e29c302011-02-26 14:44:41 +0100279 return val;
280}
281
282static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
283{
Richard Zhue1498602011-03-25 09:18:27 -0400284 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
285 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Tony Lin0d588642011-08-11 16:45:59 -0400286 u32 data;
Richard Zhue1498602011-03-25 09:18:27 -0400287
Tony Lin0d588642011-08-11 16:45:59 -0400288 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
Tony Lin0d588642011-08-11 16:45:59 -0400289 if (val & SDHCI_INT_CARD_INT) {
290 /*
291 * Clear and then set D3CD bit to avoid missing the
292 * card interrupt. This is a eSDHC controller problem
293 * so we need to apply the following workaround: clear
294 * and set D3CD bit will make eSDHC re-sample the card
295 * interrupt. In case a card interrupt was lost,
296 * re-sample it by the following steps.
297 */
298 data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
Shawn Guo60bf6392013-01-15 23:36:53 +0800299 data &= ~ESDHC_CTRL_D3CD;
Tony Lin0d588642011-08-11 16:45:59 -0400300 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
Shawn Guo60bf6392013-01-15 23:36:53 +0800301 data |= ESDHC_CTRL_D3CD;
Tony Lin0d588642011-08-11 16:45:59 -0400302 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
303 }
304 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100305
Shawn Guof47c4bb2013-10-17 15:19:47 +0800306 if (unlikely((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
Richard Zhu58ac8172011-03-21 13:22:16 +0800307 && (reg == SDHCI_INT_STATUS)
308 && (val & SDHCI_INT_DATA_END))) {
309 u32 v;
Shawn Guo60bf6392013-01-15 23:36:53 +0800310 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
311 v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
312 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
Lucas Stach361b8482013-03-15 09:49:26 +0100313
314 if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS)
315 {
316 /* send a manual CMD12 with RESPTYP=none */
317 data = MMC_STOP_TRANSMISSION << 24 |
318 SDHCI_CMD_ABORTCMD << 16;
319 writel(data, host->ioaddr + SDHCI_TRANSFER_MODE);
320 imx_data->multiblock_status = WAIT_FOR_INT;
321 }
Richard Zhu58ac8172011-03-21 13:22:16 +0800322 }
323
Richard Zhu97e4ba62011-08-11 16:51:46 -0400324 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
325 if (val & SDHCI_INT_ADMA_ERROR) {
326 val &= ~SDHCI_INT_ADMA_ERROR;
Shawn Guo60bf6392013-01-15 23:36:53 +0800327 val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
Richard Zhu97e4ba62011-08-11 16:51:46 -0400328 }
329 }
330
Wolfram Sang7e29c302011-02-26 14:44:41 +0100331 writel(val, host->ioaddr + reg);
332}
333
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200334static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
335{
Shawn Guoef4d0882013-01-15 23:30:27 +0800336 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
337 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aisheng03221912013-09-13 19:11:34 +0800338 u16 ret = 0;
339 u32 val;
Shawn Guoef4d0882013-01-15 23:30:27 +0800340
Shawn Guo95a24822011-09-19 17:32:21 +0800341 if (unlikely(reg == SDHCI_HOST_VERSION)) {
Shawn Guoef4d0882013-01-15 23:30:27 +0800342 reg ^= 2;
Shawn Guo9d61c002013-10-17 15:19:45 +0800343 if (esdhc_is_usdhc(imx_data)) {
Shawn Guoef4d0882013-01-15 23:30:27 +0800344 /*
345 * The usdhc register returns a wrong host version.
346 * Correct it here.
347 */
348 return SDHCI_SPEC_300;
349 }
Shawn Guo95a24822011-09-19 17:32:21 +0800350 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200351
Dong Aisheng03221912013-09-13 19:11:34 +0800352 if (unlikely(reg == SDHCI_HOST_CONTROL2)) {
353 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
354 if (val & ESDHC_VENDOR_SPEC_VSELECT)
355 ret |= SDHCI_CTRL_VDD_180;
356
Shawn Guo9d61c002013-10-17 15:19:45 +0800357 if (esdhc_is_usdhc(imx_data)) {
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800358 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
359 val = readl(host->ioaddr + ESDHC_MIX_CTRL);
360 else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING)
361 /* the std tuning bits is in ACMD12_ERR for imx6sl */
362 val = readl(host->ioaddr + SDHCI_ACMD12_ERR);
Dong Aisheng03221912013-09-13 19:11:34 +0800363 }
364
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800365 if (val & ESDHC_MIX_CTRL_EXE_TUNE)
366 ret |= SDHCI_CTRL_EXEC_TUNING;
367 if (val & ESDHC_MIX_CTRL_SMPCLK_SEL)
368 ret |= SDHCI_CTRL_TUNED_CLK;
369
Dong Aisheng03221912013-09-13 19:11:34 +0800370 ret |= (imx_data->uhs_mode & SDHCI_CTRL_UHS_MASK);
371 ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
372
373 return ret;
374 }
375
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200376 return readw(host->ioaddr + reg);
377}
378
379static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
380{
381 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400382 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aisheng03221912013-09-13 19:11:34 +0800383 u32 new_val = 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200384
385 switch (reg) {
Dong Aisheng03221912013-09-13 19:11:34 +0800386 case SDHCI_CLOCK_CONTROL:
387 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
388 if (val & SDHCI_CLOCK_CARD_EN)
389 new_val |= ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
390 else
391 new_val &= ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
392 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
393 return;
394 case SDHCI_HOST_CONTROL2:
395 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
396 if (val & SDHCI_CTRL_VDD_180)
397 new_val |= ESDHC_VENDOR_SPEC_VSELECT;
398 else
399 new_val &= ~ESDHC_VENDOR_SPEC_VSELECT;
400 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
401 imx_data->uhs_mode = val & SDHCI_CTRL_UHS_MASK;
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800402 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
403 new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
404 if (val & SDHCI_CTRL_TUNED_CLK)
405 new_val |= ESDHC_MIX_CTRL_SMPCLK_SEL;
406 else
407 new_val &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
408 writel(new_val , host->ioaddr + ESDHC_MIX_CTRL);
409 } else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
410 u32 v = readl(host->ioaddr + SDHCI_ACMD12_ERR);
411 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
412 new_val = readl(host->ioaddr + ESDHC_TUNING_CTRL);
413 if (val & SDHCI_CTRL_EXEC_TUNING) {
414 new_val |= ESDHC_STD_TUNING_EN |
415 ESDHC_TUNING_START_TAP;
416 v |= ESDHC_MIX_CTRL_EXE_TUNE;
417 m |= ESDHC_MIX_CTRL_FBCLK_SEL;
418 } else {
419 new_val &= ~ESDHC_STD_TUNING_EN;
420 v &= ~ESDHC_MIX_CTRL_EXE_TUNE;
421 m &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
422 }
423
424 if (val & SDHCI_CTRL_TUNED_CLK)
425 v |= ESDHC_MIX_CTRL_SMPCLK_SEL;
426 else
427 v &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
428
429 writel(new_val, host->ioaddr + ESDHC_TUNING_CTRL);
430 writel(v, host->ioaddr + SDHCI_ACMD12_ERR);
431 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
432 }
Dong Aisheng03221912013-09-13 19:11:34 +0800433 return;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200434 case SDHCI_TRANSFER_MODE:
Shawn Guof47c4bb2013-10-17 15:19:47 +0800435 if ((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
Richard Zhu58ac8172011-03-21 13:22:16 +0800436 && (host->cmd->opcode == SD_IO_RW_EXTENDED)
437 && (host->cmd->data->blocks > 1)
438 && (host->cmd->data->flags & MMC_DATA_READ)) {
439 u32 v;
Shawn Guo60bf6392013-01-15 23:36:53 +0800440 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
441 v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
442 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
Richard Zhu58ac8172011-03-21 13:22:16 +0800443 }
Shawn Guo69f54692013-01-21 19:02:24 +0800444
Shawn Guo9d61c002013-10-17 15:19:45 +0800445 if (esdhc_is_usdhc(imx_data)) {
Shawn Guo69f54692013-01-21 19:02:24 +0800446 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
Shawn Guo2a15f982013-01-21 19:02:26 +0800447 /* Swap AC23 bit */
448 if (val & SDHCI_TRNS_AUTO_CMD23) {
449 val &= ~SDHCI_TRNS_AUTO_CMD23;
450 val |= ESDHC_MIX_CTRL_AC23EN;
451 }
452 m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
Shawn Guo69f54692013-01-21 19:02:24 +0800453 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
454 } else {
455 /*
456 * Postpone this write, we must do it together with a
457 * command write that is down below.
458 */
459 imx_data->scratchpad = val;
460 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200461 return;
462 case SDHCI_COMMAND:
Lucas Stach361b8482013-03-15 09:49:26 +0100463 if (host->cmd->opcode == MMC_STOP_TRANSMISSION)
Richard Zhu58ac8172011-03-21 13:22:16 +0800464 val |= SDHCI_CMD_ABORTCMD;
Shawn Guo95a24822011-09-19 17:32:21 +0800465
Lucas Stach361b8482013-03-15 09:49:26 +0100466 if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
Shawn Guof47c4bb2013-10-17 15:19:47 +0800467 (imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
Lucas Stach361b8482013-03-15 09:49:26 +0100468 imx_data->multiblock_status = MULTIBLK_IN_PROCESS;
469
Shawn Guo9d61c002013-10-17 15:19:45 +0800470 if (esdhc_is_usdhc(imx_data))
Shawn Guo95a24822011-09-19 17:32:21 +0800471 writel(val << 16,
472 host->ioaddr + SDHCI_TRANSFER_MODE);
Shawn Guo69f54692013-01-21 19:02:24 +0800473 else
Shawn Guo95a24822011-09-19 17:32:21 +0800474 writel(val << 16 | imx_data->scratchpad,
475 host->ioaddr + SDHCI_TRANSFER_MODE);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200476 return;
477 case SDHCI_BLOCK_SIZE:
478 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
479 break;
480 }
481 esdhc_clrset_le(host, 0xffff, val, reg);
482}
483
484static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
485{
Wilson Callan9a0985b2012-07-19 02:49:16 -0400486 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
487 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200488 u32 new_val;
Sascha Haueraf510792013-01-21 19:02:28 +0800489 u32 mask;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200490
491 switch (reg) {
492 case SDHCI_POWER_CONTROL:
493 /*
494 * FSL put some DMA bits here
495 * If your board has a regulator, code should be here
496 */
497 return;
498 case SDHCI_HOST_CONTROL:
Shawn Guo6b40d182013-01-15 23:36:52 +0800499 /* FSL messed up here, so we need to manually compose it. */
Sascha Haueraf510792013-01-21 19:02:28 +0800500 new_val = val & SDHCI_CTRL_LED;
Masanari Iida7122bbb2012-08-05 23:25:40 +0900501 /* ensure the endianness */
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200502 new_val |= ESDHC_HOST_CONTROL_LE;
Wilson Callan9a0985b2012-07-19 02:49:16 -0400503 /* bits 8&9 are reserved on mx25 */
504 if (!is_imx25_esdhc(imx_data)) {
505 /* DMA mode bits are shifted */
506 new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
507 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200508
Sascha Haueraf510792013-01-21 19:02:28 +0800509 /*
510 * Do not touch buswidth bits here. This is done in
511 * esdhc_pltfm_bus_width.
Martin Fuzzeyf6825742013-04-15 17:08:35 +0200512 * Do not touch the D3CD bit either which is used for the
513 * SDIO interrupt errata workaround.
Sascha Haueraf510792013-01-21 19:02:28 +0800514 */
Martin Fuzzeyf6825742013-04-15 17:08:35 +0200515 mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD);
Sascha Haueraf510792013-01-21 19:02:28 +0800516
517 esdhc_clrset_le(host, mask, new_val, reg);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200518 return;
519 }
520 esdhc_clrset_le(host, 0xff, val, reg);
Shawn Guo913413c2011-06-21 22:41:51 +0800521
522 /*
523 * The esdhc has a design violation to SDHC spec which tells
524 * that software reset should not affect card detection circuit.
525 * But esdhc clears its SYSCTL register bits [0..2] during the
526 * software reset. This will stop those clocks that card detection
527 * circuit relies on. To work around it, we turn the clocks on back
528 * to keep card detection circuit functional.
529 */
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800530 if ((reg == SDHCI_SOFTWARE_RESET) && (val & 1)) {
Shawn Guo913413c2011-06-21 22:41:51 +0800531 esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800532 /*
533 * The reset on usdhc fails to clear MIX_CTRL register.
534 * Do it manually here.
535 */
Shawn Guo9d61c002013-10-17 15:19:45 +0800536 if (esdhc_is_usdhc(imx_data))
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800537 writel(0, host->ioaddr + ESDHC_MIX_CTRL);
538 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200539}
540
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200541static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
542{
543 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
544 struct pltfm_imx_data *imx_data = pltfm_host->priv;
545 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
546
547 u32 f_host = clk_get_rate(pltfm_host->clk);
548
549 if (boarddata->f_max && (boarddata->f_max < f_host))
550 return boarddata->f_max;
551 else
552 return f_host;
553}
554
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200555static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
556{
557 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
558
559 return clk_get_rate(pltfm_host->clk) / 256 / 16;
560}
561
Lucas Stach8ba95802013-06-05 15:13:25 +0200562static inline void esdhc_pltfm_set_clock(struct sdhci_host *host,
563 unsigned int clock)
564{
565 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800566 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aishengd31fc002013-09-13 19:11:32 +0800567 unsigned int host_clock = clk_get_rate(pltfm_host->clk);
568 int pre_div = 2;
569 int div = 1;
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800570 u32 temp, val;
Lucas Stach8ba95802013-06-05 15:13:25 +0200571
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800572 if (clock == 0) {
Shawn Guo9d61c002013-10-17 15:19:45 +0800573 if (esdhc_is_usdhc(imx_data)) {
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800574 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
575 writel(val & ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
576 host->ioaddr + ESDHC_VENDOR_SPEC);
577 }
Dong Aishengd31fc002013-09-13 19:11:32 +0800578 goto out;
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800579 }
Dong Aishengd31fc002013-09-13 19:11:32 +0800580
Shawn Guo9d61c002013-10-17 15:19:45 +0800581 if (esdhc_is_usdhc(imx_data))
Dong Aisheng5f7886c2013-09-13 19:11:36 +0800582 pre_div = 1;
583
Dong Aishengd31fc002013-09-13 19:11:32 +0800584 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
585 temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
586 | ESDHC_CLOCK_MASK);
587 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
588
589 while (host_clock / pre_div / 16 > clock && pre_div < 256)
590 pre_div *= 2;
591
592 while (host_clock / pre_div / div > clock && div < 16)
593 div++;
594
Dong Aishenge76b8552013-09-13 19:11:37 +0800595 host->mmc->actual_clock = host_clock / pre_div / div;
Dong Aishengd31fc002013-09-13 19:11:32 +0800596 dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
Dong Aishenge76b8552013-09-13 19:11:37 +0800597 clock, host->mmc->actual_clock);
Dong Aishengd31fc002013-09-13 19:11:32 +0800598
599 pre_div >>= 1;
600 div--;
601
602 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
603 temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
604 | (div << ESDHC_DIVIDER_SHIFT)
605 | (pre_div << ESDHC_PREDIV_SHIFT));
606 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800607
Shawn Guo9d61c002013-10-17 15:19:45 +0800608 if (esdhc_is_usdhc(imx_data)) {
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800609 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
610 writel(val | ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
611 host->ioaddr + ESDHC_VENDOR_SPEC);
612 }
613
Dong Aishengd31fc002013-09-13 19:11:32 +0800614 mdelay(1);
615out:
616 host->clock = clock;
Lucas Stach8ba95802013-06-05 15:13:25 +0200617}
618
Shawn Guo913413c2011-06-21 22:41:51 +0800619static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
620{
Shawn Guo842afc02011-07-06 22:57:48 +0800621 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
622 struct pltfm_imx_data *imx_data = pltfm_host->priv;
623 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Shawn Guo913413c2011-06-21 22:41:51 +0800624
625 switch (boarddata->wp_type) {
626 case ESDHC_WP_GPIO:
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800627 return mmc_gpio_get_ro(host->mmc);
Shawn Guo913413c2011-06-21 22:41:51 +0800628 case ESDHC_WP_CONTROLLER:
629 return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
630 SDHCI_WRITE_PROTECT);
631 case ESDHC_WP_NONE:
632 break;
633 }
634
635 return -ENOSYS;
636}
637
Sascha Haueraf510792013-01-21 19:02:28 +0800638static int esdhc_pltfm_bus_width(struct sdhci_host *host, int width)
639{
640 u32 ctrl;
641
642 switch (width) {
643 case MMC_BUS_WIDTH_8:
644 ctrl = ESDHC_CTRL_8BITBUS;
645 break;
646 case MMC_BUS_WIDTH_4:
647 ctrl = ESDHC_CTRL_4BITBUS;
648 break;
649 default:
650 ctrl = 0;
651 break;
652 }
653
654 esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
655 SDHCI_HOST_CONTROL);
656
657 return 0;
658}
659
Dong Aisheng03221912013-09-13 19:11:34 +0800660static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val)
661{
662 u32 reg;
663
664 /* FIXME: delay a bit for card to be ready for next tuning due to errors */
665 mdelay(1);
666
667 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
668 reg |= ESDHC_MIX_CTRL_EXE_TUNE | ESDHC_MIX_CTRL_SMPCLK_SEL |
669 ESDHC_MIX_CTRL_FBCLK_SEL;
670 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
671 writel(val << 8, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
672 dev_dbg(mmc_dev(host->mmc),
673 "tunning with delay 0x%x ESDHC_TUNE_CTRL_STATUS 0x%x\n",
674 val, readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS));
675}
676
677static void esdhc_request_done(struct mmc_request *mrq)
678{
679 complete(&mrq->completion);
680}
681
682static int esdhc_send_tuning_cmd(struct sdhci_host *host, u32 opcode)
683{
684 struct mmc_command cmd = {0};
685 struct mmc_request mrq = {0};
686 struct mmc_data data = {0};
687 struct scatterlist sg;
688 char tuning_pattern[ESDHC_TUNING_BLOCK_PATTERN_LEN];
689
690 cmd.opcode = opcode;
691 cmd.arg = 0;
692 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
693
694 data.blksz = ESDHC_TUNING_BLOCK_PATTERN_LEN;
695 data.blocks = 1;
696 data.flags = MMC_DATA_READ;
697 data.sg = &sg;
698 data.sg_len = 1;
699
700 sg_init_one(&sg, tuning_pattern, sizeof(tuning_pattern));
701
702 mrq.cmd = &cmd;
703 mrq.cmd->mrq = &mrq;
704 mrq.data = &data;
705 mrq.data->mrq = &mrq;
706 mrq.cmd->data = mrq.data;
707
708 mrq.done = esdhc_request_done;
709 init_completion(&(mrq.completion));
710
711 disable_irq(host->irq);
712 spin_lock(&host->lock);
713 host->mrq = &mrq;
714
715 sdhci_send_command(host, mrq.cmd);
716
717 spin_unlock(&host->lock);
718 enable_irq(host->irq);
719
720 wait_for_completion(&mrq.completion);
721
722 if (cmd.error)
723 return cmd.error;
724 if (data.error)
725 return data.error;
726
727 return 0;
728}
729
730static void esdhc_post_tuning(struct sdhci_host *host)
731{
732 u32 reg;
733
734 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
735 reg &= ~ESDHC_MIX_CTRL_EXE_TUNE;
736 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
737}
738
739static int esdhc_executing_tuning(struct sdhci_host *host, u32 opcode)
740{
741 int min, max, avg, ret;
742
743 /* find the mininum delay first which can pass tuning */
744 min = ESDHC_TUNE_CTRL_MIN;
745 while (min < ESDHC_TUNE_CTRL_MAX) {
746 esdhc_prepare_tuning(host, min);
747 if (!esdhc_send_tuning_cmd(host, opcode))
748 break;
749 min += ESDHC_TUNE_CTRL_STEP;
750 }
751
752 /* find the maxinum delay which can not pass tuning */
753 max = min + ESDHC_TUNE_CTRL_STEP;
754 while (max < ESDHC_TUNE_CTRL_MAX) {
755 esdhc_prepare_tuning(host, max);
756 if (esdhc_send_tuning_cmd(host, opcode)) {
757 max -= ESDHC_TUNE_CTRL_STEP;
758 break;
759 }
760 max += ESDHC_TUNE_CTRL_STEP;
761 }
762
763 /* use average delay to get the best timing */
764 avg = (min + max) / 2;
765 esdhc_prepare_tuning(host, avg);
766 ret = esdhc_send_tuning_cmd(host, opcode);
767 esdhc_post_tuning(host);
768
769 dev_dbg(mmc_dev(host->mmc), "tunning %s at 0x%x ret %d\n",
770 ret ? "failed" : "passed", avg, ret);
771
772 return ret;
773}
774
Dong Aishengad932202013-09-13 19:11:35 +0800775static int esdhc_change_pinstate(struct sdhci_host *host,
776 unsigned int uhs)
777{
778 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
779 struct pltfm_imx_data *imx_data = pltfm_host->priv;
780 struct pinctrl_state *pinctrl;
781
782 dev_dbg(mmc_dev(host->mmc), "change pinctrl state for uhs %d\n", uhs);
783
784 if (IS_ERR(imx_data->pinctrl) ||
785 IS_ERR(imx_data->pins_default) ||
786 IS_ERR(imx_data->pins_100mhz) ||
787 IS_ERR(imx_data->pins_200mhz))
788 return -EINVAL;
789
790 switch (uhs) {
791 case MMC_TIMING_UHS_SDR50:
792 pinctrl = imx_data->pins_100mhz;
793 break;
794 case MMC_TIMING_UHS_SDR104:
795 pinctrl = imx_data->pins_200mhz;
796 break;
797 default:
798 /* back to default state for other legacy timing */
799 pinctrl = imx_data->pins_default;
800 }
801
802 return pinctrl_select_state(imx_data->pinctrl, pinctrl);
803}
804
805static int esdhc_set_uhs_signaling(struct sdhci_host *host, unsigned int uhs)
806{
807 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
808 struct pltfm_imx_data *imx_data = pltfm_host->priv;
809
810 switch (uhs) {
811 case MMC_TIMING_UHS_SDR12:
812 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR12;
813 break;
814 case MMC_TIMING_UHS_SDR25:
815 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR25;
816 break;
817 case MMC_TIMING_UHS_SDR50:
818 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR50;
819 break;
820 case MMC_TIMING_UHS_SDR104:
821 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR104;
822 break;
823 case MMC_TIMING_UHS_DDR50:
824 imx_data->uhs_mode = SDHCI_CTRL_UHS_DDR50;
825 break;
826 }
827
828 return esdhc_change_pinstate(host, uhs);
829}
830
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800831static struct sdhci_ops sdhci_esdhc_ops = {
Richard Zhue1498602011-03-25 09:18:27 -0400832 .read_l = esdhc_readl_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100833 .read_w = esdhc_readw_le,
Richard Zhue1498602011-03-25 09:18:27 -0400834 .write_l = esdhc_writel_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100835 .write_w = esdhc_writew_le,
836 .write_b = esdhc_writeb_le,
Lucas Stach8ba95802013-06-05 15:13:25 +0200837 .set_clock = esdhc_pltfm_set_clock,
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200838 .get_max_clock = esdhc_pltfm_get_max_clock,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100839 .get_min_clock = esdhc_pltfm_get_min_clock,
Shawn Guo913413c2011-06-21 22:41:51 +0800840 .get_ro = esdhc_pltfm_get_ro,
Sascha Haueraf510792013-01-21 19:02:28 +0800841 .platform_bus_width = esdhc_pltfm_bus_width,
Dong Aishengad932202013-09-13 19:11:35 +0800842 .set_uhs_signaling = esdhc_set_uhs_signaling,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100843};
844
Lars-Peter Clausen1db5eeb2013-03-13 19:26:03 +0100845static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
Richard Zhu97e4ba62011-08-11 16:51:46 -0400846 .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
847 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
848 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
Shawn Guo85d65092011-05-27 23:48:12 +0800849 | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
Shawn Guo85d65092011-05-27 23:48:12 +0800850 .ops = &sdhci_esdhc_ops,
851};
852
Shawn Guoabfafc22011-06-30 15:44:44 +0800853#ifdef CONFIG_OF
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500854static int
Shawn Guoabfafc22011-06-30 15:44:44 +0800855sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
856 struct esdhc_platform_data *boarddata)
857{
858 struct device_node *np = pdev->dev.of_node;
859
860 if (!np)
861 return -ENODEV;
862
Arnd Bergmann7f217792012-05-13 00:14:24 -0400863 if (of_get_property(np, "non-removable", NULL))
Shawn Guoabfafc22011-06-30 15:44:44 +0800864 boarddata->cd_type = ESDHC_CD_PERMANENT;
865
866 if (of_get_property(np, "fsl,cd-controller", NULL))
867 boarddata->cd_type = ESDHC_CD_CONTROLLER;
868
869 if (of_get_property(np, "fsl,wp-controller", NULL))
870 boarddata->wp_type = ESDHC_WP_CONTROLLER;
871
872 boarddata->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
873 if (gpio_is_valid(boarddata->cd_gpio))
874 boarddata->cd_type = ESDHC_CD_GPIO;
875
876 boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
877 if (gpio_is_valid(boarddata->wp_gpio))
878 boarddata->wp_type = ESDHC_WP_GPIO;
879
Sascha Haueraf510792013-01-21 19:02:28 +0800880 of_property_read_u32(np, "bus-width", &boarddata->max_bus_width);
881
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200882 of_property_read_u32(np, "max-frequency", &boarddata->f_max);
883
Dong Aishengad932202013-09-13 19:11:35 +0800884 if (of_find_property(np, "no-1-8-v", NULL))
885 boarddata->support_vsel = false;
886 else
887 boarddata->support_vsel = true;
888
Shawn Guoabfafc22011-06-30 15:44:44 +0800889 return 0;
890}
891#else
892static inline int
893sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
894 struct esdhc_platform_data *boarddata)
895{
896 return -ENODEV;
897}
898#endif
899
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500900static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200901{
Shawn Guoabfafc22011-06-30 15:44:44 +0800902 const struct of_device_id *of_id =
903 of_match_device(imx_esdhc_dt_ids, &pdev->dev);
Shawn Guo85d65092011-05-27 23:48:12 +0800904 struct sdhci_pltfm_host *pltfm_host;
905 struct sdhci_host *host;
906 struct esdhc_platform_data *boarddata;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100907 int err;
Richard Zhue1498602011-03-25 09:18:27 -0400908 struct pltfm_imx_data *imx_data;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200909
Christian Daudt0e748232013-05-29 13:50:05 -0700910 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata, 0);
Shawn Guo85d65092011-05-27 23:48:12 +0800911 if (IS_ERR(host))
912 return PTR_ERR(host);
913
914 pltfm_host = sdhci_priv(host);
915
Shawn Guoe3af31c2012-11-26 14:39:43 +0800916 imx_data = devm_kzalloc(&pdev->dev, sizeof(*imx_data), GFP_KERNEL);
Shawn Guoabfafc22011-06-30 15:44:44 +0800917 if (!imx_data) {
918 err = -ENOMEM;
Shawn Guoe3af31c2012-11-26 14:39:43 +0800919 goto free_sdhci;
Shawn Guoabfafc22011-06-30 15:44:44 +0800920 }
Shawn Guo57ed3312011-06-30 09:24:26 +0800921
Shawn Guof47c4bb2013-10-17 15:19:47 +0800922 imx_data->socdata = of_id ? of_id->data : (struct esdhc_soc_data *)
923 pdev->id_entry->driver_data;
Shawn Guo85d65092011-05-27 23:48:12 +0800924 pltfm_host->priv = imx_data;
925
Sascha Hauer52dac612012-03-07 09:31:34 +0100926 imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
927 if (IS_ERR(imx_data->clk_ipg)) {
928 err = PTR_ERR(imx_data->clk_ipg);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800929 goto free_sdhci;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200930 }
Sascha Hauer52dac612012-03-07 09:31:34 +0100931
932 imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
933 if (IS_ERR(imx_data->clk_ahb)) {
934 err = PTR_ERR(imx_data->clk_ahb);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800935 goto free_sdhci;
Sascha Hauer52dac612012-03-07 09:31:34 +0100936 }
937
938 imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
939 if (IS_ERR(imx_data->clk_per)) {
940 err = PTR_ERR(imx_data->clk_per);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800941 goto free_sdhci;
Sascha Hauer52dac612012-03-07 09:31:34 +0100942 }
943
944 pltfm_host->clk = imx_data->clk_per;
945
946 clk_prepare_enable(imx_data->clk_per);
947 clk_prepare_enable(imx_data->clk_ipg);
948 clk_prepare_enable(imx_data->clk_ahb);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200949
Dong Aishengad932202013-09-13 19:11:35 +0800950 imx_data->pinctrl = devm_pinctrl_get(&pdev->dev);
Dong Aishenge62d8b82012-05-11 14:56:01 +0800951 if (IS_ERR(imx_data->pinctrl)) {
952 err = PTR_ERR(imx_data->pinctrl);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800953 goto disable_clk;
Dong Aishenge62d8b82012-05-11 14:56:01 +0800954 }
955
Dong Aishengad932202013-09-13 19:11:35 +0800956 imx_data->pins_default = pinctrl_lookup_state(imx_data->pinctrl,
957 PINCTRL_STATE_DEFAULT);
958 if (IS_ERR(imx_data->pins_default)) {
959 err = PTR_ERR(imx_data->pins_default);
960 dev_err(mmc_dev(host->mmc), "could not get default state\n");
961 goto disable_clk;
962 }
963
Eric BĂ©nardb89152822012-04-18 02:30:20 +0200964 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
Eric BĂ©nard37865fe2010-10-23 01:57:21 +0200965
Shawn Guof47c4bb2013-10-17 15:19:47 +0800966 if (imx_data->socdata->flags & ESDHC_FLAG_ENGCM07207)
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100967 /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
Richard Zhu97e4ba62011-08-11 16:51:46 -0400968 host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK
969 | SDHCI_QUIRK_BROKEN_ADMA;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100970
Shawn Guof750ba92011-11-10 16:39:32 +0800971 /*
972 * The imx6q ROM code will change the default watermark level setting
973 * to something insane. Change it back here.
974 */
Shawn Guo9d61c002013-10-17 15:19:45 +0800975 if (esdhc_is_usdhc(imx_data))
Shawn Guo60bf6392013-01-15 23:36:53 +0800976 writel(0x08100810, host->ioaddr + ESDHC_WTMK_LVL);
Shawn Guof750ba92011-11-10 16:39:32 +0800977
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800978 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
979 sdhci_esdhc_ops.platform_execute_tuning =
980 esdhc_executing_tuning;
Shawn Guo842afc02011-07-06 22:57:48 +0800981 boarddata = &imx_data->boarddata;
Shawn Guoabfafc22011-06-30 15:44:44 +0800982 if (sdhci_esdhc_imx_probe_dt(pdev, boarddata) < 0) {
983 if (!host->mmc->parent->platform_data) {
984 dev_err(mmc_dev(host->mmc), "no board data!\n");
985 err = -EINVAL;
Shawn Guoe3af31c2012-11-26 14:39:43 +0800986 goto disable_clk;
Shawn Guoabfafc22011-06-30 15:44:44 +0800987 }
988 imx_data->boarddata = *((struct esdhc_platform_data *)
989 host->mmc->parent->platform_data);
990 }
Shawn Guo913413c2011-06-21 22:41:51 +0800991
992 /* write_protect */
993 if (boarddata->wp_type == ESDHC_WP_GPIO) {
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800994 err = mmc_gpio_request_ro(host->mmc, boarddata->wp_gpio);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100995 if (err) {
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800996 dev_err(mmc_dev(host->mmc),
997 "failed to request write-protect gpio!\n");
998 goto disable_clk;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100999 }
Shawn Guofbe5fdd2012-12-11 22:32:20 +08001000 host->mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
Shawn Guo913413c2011-06-21 22:41:51 +08001001 }
Wolfram Sang7e29c302011-02-26 14:44:41 +01001002
Shawn Guo913413c2011-06-21 22:41:51 +08001003 /* card_detect */
Shawn Guo913413c2011-06-21 22:41:51 +08001004 switch (boarddata->cd_type) {
1005 case ESDHC_CD_GPIO:
Laurent Pinchart214fc302013-08-08 12:38:31 +02001006 err = mmc_gpio_request_cd(host->mmc, boarddata->cd_gpio, 0);
Wolfram Sang7e29c302011-02-26 14:44:41 +01001007 if (err) {
Shawn Guo913413c2011-06-21 22:41:51 +08001008 dev_err(mmc_dev(host->mmc),
Shawn Guofbe5fdd2012-12-11 22:32:20 +08001009 "failed to request card-detect gpio!\n");
Shawn Guoe3af31c2012-11-26 14:39:43 +08001010 goto disable_clk;
Wolfram Sang7e29c302011-02-26 14:44:41 +01001011 }
Shawn Guo913413c2011-06-21 22:41:51 +08001012 /* fall through */
Wolfram Sang7e29c302011-02-26 14:44:41 +01001013
Shawn Guo913413c2011-06-21 22:41:51 +08001014 case ESDHC_CD_CONTROLLER:
1015 /* we have a working card_detect back */
Wolfram Sang7e29c302011-02-26 14:44:41 +01001016 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
Shawn Guo913413c2011-06-21 22:41:51 +08001017 break;
1018
1019 case ESDHC_CD_PERMANENT:
1020 host->mmc->caps = MMC_CAP_NONREMOVABLE;
1021 break;
1022
1023 case ESDHC_CD_NONE:
1024 break;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +01001025 }
Eric BĂ©nard16a790b2010-10-23 01:57:22 +02001026
Sascha Haueraf510792013-01-21 19:02:28 +08001027 switch (boarddata->max_bus_width) {
1028 case 8:
1029 host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA;
1030 break;
1031 case 4:
1032 host->mmc->caps |= MMC_CAP_4_BIT_DATA;
1033 break;
1034 case 1:
1035 default:
1036 host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
1037 break;
1038 }
1039
Dong Aishengad932202013-09-13 19:11:35 +08001040 /* sdr50 and sdr104 needs work on 1.8v signal voltage */
Shawn Guo9d61c002013-10-17 15:19:45 +08001041 if ((boarddata->support_vsel) && esdhc_is_usdhc(imx_data)) {
Dong Aishengad932202013-09-13 19:11:35 +08001042 imx_data->pins_100mhz = pinctrl_lookup_state(imx_data->pinctrl,
1043 ESDHC_PINCTRL_STATE_100MHZ);
1044 imx_data->pins_200mhz = pinctrl_lookup_state(imx_data->pinctrl,
1045 ESDHC_PINCTRL_STATE_200MHZ);
1046 if (IS_ERR(imx_data->pins_100mhz) ||
1047 IS_ERR(imx_data->pins_200mhz)) {
1048 dev_warn(mmc_dev(host->mmc),
1049 "could not get ultra high speed state, work on normal mode\n");
1050 /* fall back to not support uhs by specify no 1.8v quirk */
1051 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1052 }
1053 } else {
1054 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1055 }
1056
Shawn Guo85d65092011-05-27 23:48:12 +08001057 err = sdhci_add_host(host);
1058 if (err)
Shawn Guoe3af31c2012-11-26 14:39:43 +08001059 goto disable_clk;
Shawn Guo85d65092011-05-27 23:48:12 +08001060
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001061 return 0;
Wolfram Sang7e29c302011-02-26 14:44:41 +01001062
Shawn Guoe3af31c2012-11-26 14:39:43 +08001063disable_clk:
Sascha Hauer52dac612012-03-07 09:31:34 +01001064 clk_disable_unprepare(imx_data->clk_per);
1065 clk_disable_unprepare(imx_data->clk_ipg);
1066 clk_disable_unprepare(imx_data->clk_ahb);
Shawn Guoe3af31c2012-11-26 14:39:43 +08001067free_sdhci:
Shawn Guo85d65092011-05-27 23:48:12 +08001068 sdhci_pltfm_free(pdev);
1069 return err;
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001070}
1071
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001072static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001073{
Shawn Guo85d65092011-05-27 23:48:12 +08001074 struct sdhci_host *host = platform_get_drvdata(pdev);
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001075 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -04001076 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Shawn Guo85d65092011-05-27 23:48:12 +08001077 int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
1078
1079 sdhci_remove_host(host, dead);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +01001080
Sascha Hauer52dac612012-03-07 09:31:34 +01001081 clk_disable_unprepare(imx_data->clk_per);
1082 clk_disable_unprepare(imx_data->clk_ipg);
1083 clk_disable_unprepare(imx_data->clk_ahb);
1084
Shawn Guo85d65092011-05-27 23:48:12 +08001085 sdhci_pltfm_free(pdev);
1086
1087 return 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001088}
1089
Shawn Guo85d65092011-05-27 23:48:12 +08001090static struct platform_driver sdhci_esdhc_imx_driver = {
1091 .driver = {
1092 .name = "sdhci-esdhc-imx",
1093 .owner = THIS_MODULE,
Shawn Guoabfafc22011-06-30 15:44:44 +08001094 .of_match_table = imx_esdhc_dt_ids,
Manuel Lauss29495aa2011-11-03 11:09:45 +01001095 .pm = SDHCI_PLTFM_PMOPS,
Shawn Guo85d65092011-05-27 23:48:12 +08001096 },
Shawn Guo57ed3312011-06-30 09:24:26 +08001097 .id_table = imx_esdhc_devtype,
Shawn Guo85d65092011-05-27 23:48:12 +08001098 .probe = sdhci_esdhc_imx_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05001099 .remove = sdhci_esdhc_imx_remove,
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001100};
Shawn Guo85d65092011-05-27 23:48:12 +08001101
Axel Lind1f81a62011-11-26 12:55:43 +08001102module_platform_driver(sdhci_esdhc_imx_driver);
Shawn Guo85d65092011-05-27 23:48:12 +08001103
1104MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
1105MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
1106MODULE_LICENSE("GPL v2");