blob: 83023fa6fa34a004e2b135def834d53fc09fbaa8 [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
54#define ESDHC_TUNING_BLOCK_PATTERN_LEN 64
55
Dong Aishengad932202013-09-13 19:11:35 +080056/* pinctrl state */
57#define ESDHC_PINCTRL_STATE_100MHZ "state_100mhz"
58#define ESDHC_PINCTRL_STATE_200MHZ "state_200mhz"
59
Richard Zhu58ac8172011-03-21 13:22:16 +080060/*
Sascha Haueraf510792013-01-21 19:02:28 +080061 * 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
67/*
Richard Zhu97e4ba62011-08-11 16:51:46 -040068 * 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 */
Shawn Guo60bf6392013-01-15 23:36:53 +080073#define ESDHC_INT_VENDOR_SPEC_DMA_ERR (1 << 28)
Richard Zhu97e4ba62011-08-11 16:51:46 -040074
75/*
Richard Zhu58ac8172011-03-21 13:22:16 +080076 * 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 */
Shawn Guo31fbb302013-10-17 15:19:44 +080086#define ESDHC_FLAG_MULTIBLK_NO_INT BIT(1)
87/*
88 * The flag enables the workaround for ESDHC errata ENGcm07207 which
89 * affects i.MX25 and i.MX35.
90 */
91#define ESDHC_FLAG_ENGCM07207 BIT(2)
Shawn Guo9d61c002013-10-17 15:19:45 +080092/*
93 * The flag tells that the ESDHC controller is an USDHC block that is
94 * integrated on the i.MX6 series.
95 */
96#define ESDHC_FLAG_USDHC BIT(3)
Richard Zhue1498602011-03-25 09:18:27 -040097
Shawn Guo57ed3312011-06-30 09:24:26 +080098enum imx_esdhc_type {
99 IMX25_ESDHC,
100 IMX35_ESDHC,
101 IMX51_ESDHC,
102 IMX53_ESDHC,
Shawn Guo95a24822011-09-19 17:32:21 +0800103 IMX6Q_USDHC,
Shawn Guo57ed3312011-06-30 09:24:26 +0800104};
105
Richard Zhue1498602011-03-25 09:18:27 -0400106struct pltfm_imx_data {
107 int flags;
108 u32 scratchpad;
Shawn Guo57ed3312011-06-30 09:24:26 +0800109 enum imx_esdhc_type devtype;
Dong Aishenge62d8b82012-05-11 14:56:01 +0800110 struct pinctrl *pinctrl;
Dong Aishengad932202013-09-13 19:11:35 +0800111 struct pinctrl_state *pins_default;
112 struct pinctrl_state *pins_100mhz;
113 struct pinctrl_state *pins_200mhz;
Shawn Guo842afc02011-07-06 22:57:48 +0800114 struct esdhc_platform_data boarddata;
Sascha Hauer52dac612012-03-07 09:31:34 +0100115 struct clk *clk_ipg;
116 struct clk *clk_ahb;
117 struct clk *clk_per;
Lucas Stach361b8482013-03-15 09:49:26 +0100118 enum {
119 NO_CMD_PENDING, /* no multiblock command pending*/
120 MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */
121 WAIT_FOR_INT, /* sent CMD12, waiting for response INT */
122 } multiblock_status;
Dong Aisheng03221912013-09-13 19:11:34 +0800123 u32 uhs_mode;
Richard Zhue1498602011-03-25 09:18:27 -0400124};
125
Shawn Guo57ed3312011-06-30 09:24:26 +0800126static struct platform_device_id imx_esdhc_devtype[] = {
127 {
128 .name = "sdhci-esdhc-imx25",
129 .driver_data = IMX25_ESDHC,
130 }, {
131 .name = "sdhci-esdhc-imx35",
132 .driver_data = IMX35_ESDHC,
133 }, {
134 .name = "sdhci-esdhc-imx51",
135 .driver_data = IMX51_ESDHC,
136 }, {
Shawn Guo57ed3312011-06-30 09:24:26 +0800137 /* sentinel */
138 }
139};
140MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
141
Shawn Guoabfafc22011-06-30 15:44:44 +0800142static const struct of_device_id imx_esdhc_dt_ids[] = {
Shawn Guo3770ee82013-10-17 15:19:46 +0800143 { .compatible = "fsl,imx25-esdhc", .data = (void *) IMX25_ESDHC, },
144 { .compatible = "fsl,imx35-esdhc", .data = (void *) IMX35_ESDHC, },
145 { .compatible = "fsl,imx51-esdhc", .data = (void *) IMX51_ESDHC, },
146 { .compatible = "fsl,imx53-esdhc", .data = (void *) IMX53_ESDHC, },
147 { .compatible = "fsl,imx6q-usdhc", .data = (void *) IMX6Q_USDHC, },
Shawn Guoabfafc22011-06-30 15:44:44 +0800148 { /* sentinel */ }
149};
150MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
151
Shawn Guo57ed3312011-06-30 09:24:26 +0800152static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
153{
154 return data->devtype == IMX25_ESDHC;
155}
156
157static inline int is_imx35_esdhc(struct pltfm_imx_data *data)
158{
159 return data->devtype == IMX35_ESDHC;
160}
161
162static inline int is_imx51_esdhc(struct pltfm_imx_data *data)
163{
164 return data->devtype == IMX51_ESDHC;
165}
166
167static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
168{
169 return data->devtype == IMX53_ESDHC;
170}
171
Shawn Guo95a24822011-09-19 17:32:21 +0800172static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
173{
174 return data->devtype == IMX6Q_USDHC;
175}
176
Shawn Guo9d61c002013-10-17 15:19:45 +0800177static inline int esdhc_is_usdhc(struct pltfm_imx_data *data)
178{
179 return !!(data->flags & ESDHC_FLAG_USDHC);
180}
181
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200182static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
183{
184 void __iomem *base = host->ioaddr + (reg & ~0x3);
185 u32 shift = (reg & 0x3) * 8;
186
187 writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
188}
189
Wolfram Sang7e29c302011-02-26 14:44:41 +0100190static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
191{
Lucas Stach361b8482013-03-15 09:49:26 +0100192 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
193 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100194 u32 val = readl(host->ioaddr + reg);
195
Dong Aisheng03221912013-09-13 19:11:34 +0800196 if (unlikely(reg == SDHCI_PRESENT_STATE)) {
197 u32 fsl_prss = val;
198 /* save the least 20 bits */
199 val = fsl_prss & 0x000FFFFF;
200 /* move dat[0-3] bits */
201 val |= (fsl_prss & 0x0F000000) >> 4;
202 /* move cmd line bit */
203 val |= (fsl_prss & 0x00800000) << 1;
204 }
205
Richard Zhu97e4ba62011-08-11 16:51:46 -0400206 if (unlikely(reg == SDHCI_CAPABILITIES)) {
207 /* In FSL esdhc IC module, only bit20 is used to indicate the
208 * ADMA2 capability of esdhc, but this bit is messed up on
209 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
210 * don't actually support ADMA2). So set the BROKEN_ADMA
211 * uirk on MX25/35 platforms.
212 */
213
214 if (val & SDHCI_CAN_DO_ADMA1) {
215 val &= ~SDHCI_CAN_DO_ADMA1;
216 val |= SDHCI_CAN_DO_ADMA2;
217 }
218 }
219
Shawn Guo9d61c002013-10-17 15:19:45 +0800220 if (unlikely(reg == SDHCI_CAPABILITIES_1) && esdhc_is_usdhc(imx_data))
Dong Aisheng03221912013-09-13 19:11:34 +0800221 val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104
222 | SDHCI_SUPPORT_SDR50;
223
Shawn Guo9d61c002013-10-17 15:19:45 +0800224 if (unlikely(reg == SDHCI_MAX_CURRENT) && esdhc_is_usdhc(imx_data)) {
Dong Aisheng03221912013-09-13 19:11:34 +0800225 val = 0;
226 val |= 0xFF << SDHCI_MAX_CURRENT_330_SHIFT;
227 val |= 0xFF << SDHCI_MAX_CURRENT_300_SHIFT;
228 val |= 0xFF << SDHCI_MAX_CURRENT_180_SHIFT;
229 }
230
Richard Zhu97e4ba62011-08-11 16:51:46 -0400231 if (unlikely(reg == SDHCI_INT_STATUS)) {
Shawn Guo60bf6392013-01-15 23:36:53 +0800232 if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
233 val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
Richard Zhu97e4ba62011-08-11 16:51:46 -0400234 val |= SDHCI_INT_ADMA_ERROR;
235 }
Lucas Stach361b8482013-03-15 09:49:26 +0100236
237 /*
238 * mask off the interrupt we get in response to the manually
239 * sent CMD12
240 */
241 if ((imx_data->multiblock_status == WAIT_FOR_INT) &&
242 ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) {
243 val &= ~SDHCI_INT_RESPONSE;
244 writel(SDHCI_INT_RESPONSE, host->ioaddr +
245 SDHCI_INT_STATUS);
246 imx_data->multiblock_status = NO_CMD_PENDING;
247 }
Richard Zhu97e4ba62011-08-11 16:51:46 -0400248 }
249
Wolfram Sang7e29c302011-02-26 14:44:41 +0100250 return val;
251}
252
253static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
254{
Richard Zhue1498602011-03-25 09:18:27 -0400255 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
256 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Tony Lin0d588642011-08-11 16:45:59 -0400257 u32 data;
Richard Zhue1498602011-03-25 09:18:27 -0400258
Tony Lin0d588642011-08-11 16:45:59 -0400259 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
Tony Lin0d588642011-08-11 16:45:59 -0400260 if (val & SDHCI_INT_CARD_INT) {
261 /*
262 * Clear and then set D3CD bit to avoid missing the
263 * card interrupt. This is a eSDHC controller problem
264 * so we need to apply the following workaround: clear
265 * and set D3CD bit will make eSDHC re-sample the card
266 * interrupt. In case a card interrupt was lost,
267 * re-sample it by the following steps.
268 */
269 data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
Shawn Guo60bf6392013-01-15 23:36:53 +0800270 data &= ~ESDHC_CTRL_D3CD;
Tony Lin0d588642011-08-11 16:45:59 -0400271 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
Shawn Guo60bf6392013-01-15 23:36:53 +0800272 data |= ESDHC_CTRL_D3CD;
Tony Lin0d588642011-08-11 16:45:59 -0400273 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
274 }
275 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100276
Richard Zhu58ac8172011-03-21 13:22:16 +0800277 if (unlikely((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
278 && (reg == SDHCI_INT_STATUS)
279 && (val & SDHCI_INT_DATA_END))) {
280 u32 v;
Shawn Guo60bf6392013-01-15 23:36:53 +0800281 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
282 v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
283 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
Lucas Stach361b8482013-03-15 09:49:26 +0100284
285 if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS)
286 {
287 /* send a manual CMD12 with RESPTYP=none */
288 data = MMC_STOP_TRANSMISSION << 24 |
289 SDHCI_CMD_ABORTCMD << 16;
290 writel(data, host->ioaddr + SDHCI_TRANSFER_MODE);
291 imx_data->multiblock_status = WAIT_FOR_INT;
292 }
Richard Zhu58ac8172011-03-21 13:22:16 +0800293 }
294
Richard Zhu97e4ba62011-08-11 16:51:46 -0400295 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
296 if (val & SDHCI_INT_ADMA_ERROR) {
297 val &= ~SDHCI_INT_ADMA_ERROR;
Shawn Guo60bf6392013-01-15 23:36:53 +0800298 val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
Richard Zhu97e4ba62011-08-11 16:51:46 -0400299 }
300 }
301
Wolfram Sang7e29c302011-02-26 14:44:41 +0100302 writel(val, host->ioaddr + reg);
303}
304
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200305static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
306{
Shawn Guoef4d0882013-01-15 23:30:27 +0800307 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
308 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aisheng03221912013-09-13 19:11:34 +0800309 u16 ret = 0;
310 u32 val;
Shawn Guoef4d0882013-01-15 23:30:27 +0800311
Shawn Guo95a24822011-09-19 17:32:21 +0800312 if (unlikely(reg == SDHCI_HOST_VERSION)) {
Shawn Guoef4d0882013-01-15 23:30:27 +0800313 reg ^= 2;
Shawn Guo9d61c002013-10-17 15:19:45 +0800314 if (esdhc_is_usdhc(imx_data)) {
Shawn Guoef4d0882013-01-15 23:30:27 +0800315 /*
316 * The usdhc register returns a wrong host version.
317 * Correct it here.
318 */
319 return SDHCI_SPEC_300;
320 }
Shawn Guo95a24822011-09-19 17:32:21 +0800321 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200322
Dong Aisheng03221912013-09-13 19:11:34 +0800323 if (unlikely(reg == SDHCI_HOST_CONTROL2)) {
324 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
325 if (val & ESDHC_VENDOR_SPEC_VSELECT)
326 ret |= SDHCI_CTRL_VDD_180;
327
Shawn Guo9d61c002013-10-17 15:19:45 +0800328 if (esdhc_is_usdhc(imx_data)) {
Dong Aisheng03221912013-09-13 19:11:34 +0800329 val = readl(host->ioaddr + ESDHC_MIX_CTRL);
330 if (val & ESDHC_MIX_CTRL_EXE_TUNE)
331 ret |= SDHCI_CTRL_EXEC_TUNING;
332 if (val & ESDHC_MIX_CTRL_SMPCLK_SEL)
333 ret |= SDHCI_CTRL_TUNED_CLK;
334 }
335
336 ret |= (imx_data->uhs_mode & SDHCI_CTRL_UHS_MASK);
337 ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
338
339 return ret;
340 }
341
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200342 return readw(host->ioaddr + reg);
343}
344
345static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
346{
347 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400348 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aisheng03221912013-09-13 19:11:34 +0800349 u32 new_val = 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200350
351 switch (reg) {
Dong Aisheng03221912013-09-13 19:11:34 +0800352 case SDHCI_CLOCK_CONTROL:
353 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
354 if (val & SDHCI_CLOCK_CARD_EN)
355 new_val |= ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
356 else
357 new_val &= ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
358 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
359 return;
360 case SDHCI_HOST_CONTROL2:
361 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
362 if (val & SDHCI_CTRL_VDD_180)
363 new_val |= ESDHC_VENDOR_SPEC_VSELECT;
364 else
365 new_val &= ~ESDHC_VENDOR_SPEC_VSELECT;
366 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
367 imx_data->uhs_mode = val & SDHCI_CTRL_UHS_MASK;
368 new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
369 if (val & SDHCI_CTRL_TUNED_CLK)
370 new_val |= ESDHC_MIX_CTRL_SMPCLK_SEL;
371 else
372 new_val &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
373 writel(new_val , host->ioaddr + ESDHC_MIX_CTRL);
374 return;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200375 case SDHCI_TRANSFER_MODE:
Richard Zhu58ac8172011-03-21 13:22:16 +0800376 if ((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
377 && (host->cmd->opcode == SD_IO_RW_EXTENDED)
378 && (host->cmd->data->blocks > 1)
379 && (host->cmd->data->flags & MMC_DATA_READ)) {
380 u32 v;
Shawn Guo60bf6392013-01-15 23:36:53 +0800381 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
382 v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
383 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
Richard Zhu58ac8172011-03-21 13:22:16 +0800384 }
Shawn Guo69f54692013-01-21 19:02:24 +0800385
Shawn Guo9d61c002013-10-17 15:19:45 +0800386 if (esdhc_is_usdhc(imx_data)) {
Shawn Guo69f54692013-01-21 19:02:24 +0800387 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
Shawn Guo2a15f982013-01-21 19:02:26 +0800388 /* Swap AC23 bit */
389 if (val & SDHCI_TRNS_AUTO_CMD23) {
390 val &= ~SDHCI_TRNS_AUTO_CMD23;
391 val |= ESDHC_MIX_CTRL_AC23EN;
392 }
393 m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
Shawn Guo69f54692013-01-21 19:02:24 +0800394 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
395 } else {
396 /*
397 * Postpone this write, we must do it together with a
398 * command write that is down below.
399 */
400 imx_data->scratchpad = val;
401 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200402 return;
403 case SDHCI_COMMAND:
Lucas Stach361b8482013-03-15 09:49:26 +0100404 if (host->cmd->opcode == MMC_STOP_TRANSMISSION)
Richard Zhu58ac8172011-03-21 13:22:16 +0800405 val |= SDHCI_CMD_ABORTCMD;
Shawn Guo95a24822011-09-19 17:32:21 +0800406
Lucas Stach361b8482013-03-15 09:49:26 +0100407 if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
408 (imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
409 imx_data->multiblock_status = MULTIBLK_IN_PROCESS;
410
Shawn Guo9d61c002013-10-17 15:19:45 +0800411 if (esdhc_is_usdhc(imx_data))
Shawn Guo95a24822011-09-19 17:32:21 +0800412 writel(val << 16,
413 host->ioaddr + SDHCI_TRANSFER_MODE);
Shawn Guo69f54692013-01-21 19:02:24 +0800414 else
Shawn Guo95a24822011-09-19 17:32:21 +0800415 writel(val << 16 | imx_data->scratchpad,
416 host->ioaddr + SDHCI_TRANSFER_MODE);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200417 return;
418 case SDHCI_BLOCK_SIZE:
419 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
420 break;
421 }
422 esdhc_clrset_le(host, 0xffff, val, reg);
423}
424
425static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
426{
Wilson Callan9a0985b2012-07-19 02:49:16 -0400427 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
428 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200429 u32 new_val;
Sascha Haueraf510792013-01-21 19:02:28 +0800430 u32 mask;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200431
432 switch (reg) {
433 case SDHCI_POWER_CONTROL:
434 /*
435 * FSL put some DMA bits here
436 * If your board has a regulator, code should be here
437 */
438 return;
439 case SDHCI_HOST_CONTROL:
Shawn Guo6b40d182013-01-15 23:36:52 +0800440 /* FSL messed up here, so we need to manually compose it. */
Sascha Haueraf510792013-01-21 19:02:28 +0800441 new_val = val & SDHCI_CTRL_LED;
Masanari Iida7122bbb2012-08-05 23:25:40 +0900442 /* ensure the endianness */
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200443 new_val |= ESDHC_HOST_CONTROL_LE;
Wilson Callan9a0985b2012-07-19 02:49:16 -0400444 /* bits 8&9 are reserved on mx25 */
445 if (!is_imx25_esdhc(imx_data)) {
446 /* DMA mode bits are shifted */
447 new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
448 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200449
Sascha Haueraf510792013-01-21 19:02:28 +0800450 /*
451 * Do not touch buswidth bits here. This is done in
452 * esdhc_pltfm_bus_width.
Martin Fuzzeyf6825742013-04-15 17:08:35 +0200453 * Do not touch the D3CD bit either which is used for the
454 * SDIO interrupt errata workaround.
Sascha Haueraf510792013-01-21 19:02:28 +0800455 */
Martin Fuzzeyf6825742013-04-15 17:08:35 +0200456 mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD);
Sascha Haueraf510792013-01-21 19:02:28 +0800457
458 esdhc_clrset_le(host, mask, new_val, reg);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200459 return;
460 }
461 esdhc_clrset_le(host, 0xff, val, reg);
Shawn Guo913413c2011-06-21 22:41:51 +0800462
463 /*
464 * The esdhc has a design violation to SDHC spec which tells
465 * that software reset should not affect card detection circuit.
466 * But esdhc clears its SYSCTL register bits [0..2] during the
467 * software reset. This will stop those clocks that card detection
468 * circuit relies on. To work around it, we turn the clocks on back
469 * to keep card detection circuit functional.
470 */
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800471 if ((reg == SDHCI_SOFTWARE_RESET) && (val & 1)) {
Shawn Guo913413c2011-06-21 22:41:51 +0800472 esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800473 /*
474 * The reset on usdhc fails to clear MIX_CTRL register.
475 * Do it manually here.
476 */
Shawn Guo9d61c002013-10-17 15:19:45 +0800477 if (esdhc_is_usdhc(imx_data))
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800478 writel(0, host->ioaddr + ESDHC_MIX_CTRL);
479 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200480}
481
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200482static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
483{
484 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
485 struct pltfm_imx_data *imx_data = pltfm_host->priv;
486 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
487
488 u32 f_host = clk_get_rate(pltfm_host->clk);
489
490 if (boarddata->f_max && (boarddata->f_max < f_host))
491 return boarddata->f_max;
492 else
493 return f_host;
494}
495
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200496static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
497{
498 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
499
500 return clk_get_rate(pltfm_host->clk) / 256 / 16;
501}
502
Lucas Stach8ba95802013-06-05 15:13:25 +0200503static inline void esdhc_pltfm_set_clock(struct sdhci_host *host,
504 unsigned int clock)
505{
506 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800507 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aishengd31fc002013-09-13 19:11:32 +0800508 unsigned int host_clock = clk_get_rate(pltfm_host->clk);
509 int pre_div = 2;
510 int div = 1;
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800511 u32 temp, val;
Lucas Stach8ba95802013-06-05 15:13:25 +0200512
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800513 if (clock == 0) {
Shawn Guo9d61c002013-10-17 15:19:45 +0800514 if (esdhc_is_usdhc(imx_data)) {
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800515 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
516 writel(val & ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
517 host->ioaddr + ESDHC_VENDOR_SPEC);
518 }
Dong Aishengd31fc002013-09-13 19:11:32 +0800519 goto out;
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800520 }
Dong Aishengd31fc002013-09-13 19:11:32 +0800521
Shawn Guo9d61c002013-10-17 15:19:45 +0800522 if (esdhc_is_usdhc(imx_data))
Dong Aisheng5f7886c2013-09-13 19:11:36 +0800523 pre_div = 1;
524
Dong Aishengd31fc002013-09-13 19:11:32 +0800525 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
526 temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
527 | ESDHC_CLOCK_MASK);
528 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
529
530 while (host_clock / pre_div / 16 > clock && pre_div < 256)
531 pre_div *= 2;
532
533 while (host_clock / pre_div / div > clock && div < 16)
534 div++;
535
Dong Aishenge76b8552013-09-13 19:11:37 +0800536 host->mmc->actual_clock = host_clock / pre_div / div;
Dong Aishengd31fc002013-09-13 19:11:32 +0800537 dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
Dong Aishenge76b8552013-09-13 19:11:37 +0800538 clock, host->mmc->actual_clock);
Dong Aishengd31fc002013-09-13 19:11:32 +0800539
540 pre_div >>= 1;
541 div--;
542
543 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
544 temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
545 | (div << ESDHC_DIVIDER_SHIFT)
546 | (pre_div << ESDHC_PREDIV_SHIFT));
547 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800548
Shawn Guo9d61c002013-10-17 15:19:45 +0800549 if (esdhc_is_usdhc(imx_data)) {
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800550 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
551 writel(val | ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
552 host->ioaddr + ESDHC_VENDOR_SPEC);
553 }
554
Dong Aishengd31fc002013-09-13 19:11:32 +0800555 mdelay(1);
556out:
557 host->clock = clock;
Lucas Stach8ba95802013-06-05 15:13:25 +0200558}
559
Shawn Guo913413c2011-06-21 22:41:51 +0800560static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
561{
Shawn Guo842afc02011-07-06 22:57:48 +0800562 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
563 struct pltfm_imx_data *imx_data = pltfm_host->priv;
564 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Shawn Guo913413c2011-06-21 22:41:51 +0800565
566 switch (boarddata->wp_type) {
567 case ESDHC_WP_GPIO:
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800568 return mmc_gpio_get_ro(host->mmc);
Shawn Guo913413c2011-06-21 22:41:51 +0800569 case ESDHC_WP_CONTROLLER:
570 return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
571 SDHCI_WRITE_PROTECT);
572 case ESDHC_WP_NONE:
573 break;
574 }
575
576 return -ENOSYS;
577}
578
Sascha Haueraf510792013-01-21 19:02:28 +0800579static int esdhc_pltfm_bus_width(struct sdhci_host *host, int width)
580{
581 u32 ctrl;
582
583 switch (width) {
584 case MMC_BUS_WIDTH_8:
585 ctrl = ESDHC_CTRL_8BITBUS;
586 break;
587 case MMC_BUS_WIDTH_4:
588 ctrl = ESDHC_CTRL_4BITBUS;
589 break;
590 default:
591 ctrl = 0;
592 break;
593 }
594
595 esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
596 SDHCI_HOST_CONTROL);
597
598 return 0;
599}
600
Dong Aisheng03221912013-09-13 19:11:34 +0800601static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val)
602{
603 u32 reg;
604
605 /* FIXME: delay a bit for card to be ready for next tuning due to errors */
606 mdelay(1);
607
608 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
609 reg |= ESDHC_MIX_CTRL_EXE_TUNE | ESDHC_MIX_CTRL_SMPCLK_SEL |
610 ESDHC_MIX_CTRL_FBCLK_SEL;
611 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
612 writel(val << 8, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
613 dev_dbg(mmc_dev(host->mmc),
614 "tunning with delay 0x%x ESDHC_TUNE_CTRL_STATUS 0x%x\n",
615 val, readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS));
616}
617
618static void esdhc_request_done(struct mmc_request *mrq)
619{
620 complete(&mrq->completion);
621}
622
623static int esdhc_send_tuning_cmd(struct sdhci_host *host, u32 opcode)
624{
625 struct mmc_command cmd = {0};
626 struct mmc_request mrq = {0};
627 struct mmc_data data = {0};
628 struct scatterlist sg;
629 char tuning_pattern[ESDHC_TUNING_BLOCK_PATTERN_LEN];
630
631 cmd.opcode = opcode;
632 cmd.arg = 0;
633 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
634
635 data.blksz = ESDHC_TUNING_BLOCK_PATTERN_LEN;
636 data.blocks = 1;
637 data.flags = MMC_DATA_READ;
638 data.sg = &sg;
639 data.sg_len = 1;
640
641 sg_init_one(&sg, tuning_pattern, sizeof(tuning_pattern));
642
643 mrq.cmd = &cmd;
644 mrq.cmd->mrq = &mrq;
645 mrq.data = &data;
646 mrq.data->mrq = &mrq;
647 mrq.cmd->data = mrq.data;
648
649 mrq.done = esdhc_request_done;
650 init_completion(&(mrq.completion));
651
652 disable_irq(host->irq);
653 spin_lock(&host->lock);
654 host->mrq = &mrq;
655
656 sdhci_send_command(host, mrq.cmd);
657
658 spin_unlock(&host->lock);
659 enable_irq(host->irq);
660
661 wait_for_completion(&mrq.completion);
662
663 if (cmd.error)
664 return cmd.error;
665 if (data.error)
666 return data.error;
667
668 return 0;
669}
670
671static void esdhc_post_tuning(struct sdhci_host *host)
672{
673 u32 reg;
674
675 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
676 reg &= ~ESDHC_MIX_CTRL_EXE_TUNE;
677 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
678}
679
680static int esdhc_executing_tuning(struct sdhci_host *host, u32 opcode)
681{
682 int min, max, avg, ret;
683
684 /* find the mininum delay first which can pass tuning */
685 min = ESDHC_TUNE_CTRL_MIN;
686 while (min < ESDHC_TUNE_CTRL_MAX) {
687 esdhc_prepare_tuning(host, min);
688 if (!esdhc_send_tuning_cmd(host, opcode))
689 break;
690 min += ESDHC_TUNE_CTRL_STEP;
691 }
692
693 /* find the maxinum delay which can not pass tuning */
694 max = min + ESDHC_TUNE_CTRL_STEP;
695 while (max < ESDHC_TUNE_CTRL_MAX) {
696 esdhc_prepare_tuning(host, max);
697 if (esdhc_send_tuning_cmd(host, opcode)) {
698 max -= ESDHC_TUNE_CTRL_STEP;
699 break;
700 }
701 max += ESDHC_TUNE_CTRL_STEP;
702 }
703
704 /* use average delay to get the best timing */
705 avg = (min + max) / 2;
706 esdhc_prepare_tuning(host, avg);
707 ret = esdhc_send_tuning_cmd(host, opcode);
708 esdhc_post_tuning(host);
709
710 dev_dbg(mmc_dev(host->mmc), "tunning %s at 0x%x ret %d\n",
711 ret ? "failed" : "passed", avg, ret);
712
713 return ret;
714}
715
Dong Aishengad932202013-09-13 19:11:35 +0800716static int esdhc_change_pinstate(struct sdhci_host *host,
717 unsigned int uhs)
718{
719 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
720 struct pltfm_imx_data *imx_data = pltfm_host->priv;
721 struct pinctrl_state *pinctrl;
722
723 dev_dbg(mmc_dev(host->mmc), "change pinctrl state for uhs %d\n", uhs);
724
725 if (IS_ERR(imx_data->pinctrl) ||
726 IS_ERR(imx_data->pins_default) ||
727 IS_ERR(imx_data->pins_100mhz) ||
728 IS_ERR(imx_data->pins_200mhz))
729 return -EINVAL;
730
731 switch (uhs) {
732 case MMC_TIMING_UHS_SDR50:
733 pinctrl = imx_data->pins_100mhz;
734 break;
735 case MMC_TIMING_UHS_SDR104:
736 pinctrl = imx_data->pins_200mhz;
737 break;
738 default:
739 /* back to default state for other legacy timing */
740 pinctrl = imx_data->pins_default;
741 }
742
743 return pinctrl_select_state(imx_data->pinctrl, pinctrl);
744}
745
746static int esdhc_set_uhs_signaling(struct sdhci_host *host, unsigned int uhs)
747{
748 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
749 struct pltfm_imx_data *imx_data = pltfm_host->priv;
750
751 switch (uhs) {
752 case MMC_TIMING_UHS_SDR12:
753 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR12;
754 break;
755 case MMC_TIMING_UHS_SDR25:
756 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR25;
757 break;
758 case MMC_TIMING_UHS_SDR50:
759 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR50;
760 break;
761 case MMC_TIMING_UHS_SDR104:
762 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR104;
763 break;
764 case MMC_TIMING_UHS_DDR50:
765 imx_data->uhs_mode = SDHCI_CTRL_UHS_DDR50;
766 break;
767 }
768
769 return esdhc_change_pinstate(host, uhs);
770}
771
Lars-Peter Clausenc9155682013-03-13 19:26:05 +0100772static const struct sdhci_ops sdhci_esdhc_ops = {
Richard Zhue1498602011-03-25 09:18:27 -0400773 .read_l = esdhc_readl_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100774 .read_w = esdhc_readw_le,
Richard Zhue1498602011-03-25 09:18:27 -0400775 .write_l = esdhc_writel_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100776 .write_w = esdhc_writew_le,
777 .write_b = esdhc_writeb_le,
Lucas Stach8ba95802013-06-05 15:13:25 +0200778 .set_clock = esdhc_pltfm_set_clock,
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200779 .get_max_clock = esdhc_pltfm_get_max_clock,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100780 .get_min_clock = esdhc_pltfm_get_min_clock,
Shawn Guo913413c2011-06-21 22:41:51 +0800781 .get_ro = esdhc_pltfm_get_ro,
Sascha Haueraf510792013-01-21 19:02:28 +0800782 .platform_bus_width = esdhc_pltfm_bus_width,
Dong Aishengad932202013-09-13 19:11:35 +0800783 .set_uhs_signaling = esdhc_set_uhs_signaling,
Dong Aisheng03221912013-09-13 19:11:34 +0800784 .platform_execute_tuning = esdhc_executing_tuning,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100785};
786
Lars-Peter Clausen1db5eeb2013-03-13 19:26:03 +0100787static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
Richard Zhu97e4ba62011-08-11 16:51:46 -0400788 .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
789 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
790 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
Shawn Guo85d65092011-05-27 23:48:12 +0800791 | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
Shawn Guo85d65092011-05-27 23:48:12 +0800792 .ops = &sdhci_esdhc_ops,
793};
794
Shawn Guoabfafc22011-06-30 15:44:44 +0800795#ifdef CONFIG_OF
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500796static int
Shawn Guoabfafc22011-06-30 15:44:44 +0800797sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
798 struct esdhc_platform_data *boarddata)
799{
800 struct device_node *np = pdev->dev.of_node;
801
802 if (!np)
803 return -ENODEV;
804
Arnd Bergmann7f217792012-05-13 00:14:24 -0400805 if (of_get_property(np, "non-removable", NULL))
Shawn Guoabfafc22011-06-30 15:44:44 +0800806 boarddata->cd_type = ESDHC_CD_PERMANENT;
807
808 if (of_get_property(np, "fsl,cd-controller", NULL))
809 boarddata->cd_type = ESDHC_CD_CONTROLLER;
810
811 if (of_get_property(np, "fsl,wp-controller", NULL))
812 boarddata->wp_type = ESDHC_WP_CONTROLLER;
813
814 boarddata->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
815 if (gpio_is_valid(boarddata->cd_gpio))
816 boarddata->cd_type = ESDHC_CD_GPIO;
817
818 boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
819 if (gpio_is_valid(boarddata->wp_gpio))
820 boarddata->wp_type = ESDHC_WP_GPIO;
821
Sascha Haueraf510792013-01-21 19:02:28 +0800822 of_property_read_u32(np, "bus-width", &boarddata->max_bus_width);
823
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200824 of_property_read_u32(np, "max-frequency", &boarddata->f_max);
825
Dong Aishengad932202013-09-13 19:11:35 +0800826 if (of_find_property(np, "no-1-8-v", NULL))
827 boarddata->support_vsel = false;
828 else
829 boarddata->support_vsel = true;
830
Shawn Guoabfafc22011-06-30 15:44:44 +0800831 return 0;
832}
833#else
834static inline int
835sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
836 struct esdhc_platform_data *boarddata)
837{
838 return -ENODEV;
839}
840#endif
841
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500842static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200843{
Shawn Guoabfafc22011-06-30 15:44:44 +0800844 const struct of_device_id *of_id =
845 of_match_device(imx_esdhc_dt_ids, &pdev->dev);
Shawn Guo85d65092011-05-27 23:48:12 +0800846 struct sdhci_pltfm_host *pltfm_host;
847 struct sdhci_host *host;
848 struct esdhc_platform_data *boarddata;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100849 int err;
Richard Zhue1498602011-03-25 09:18:27 -0400850 struct pltfm_imx_data *imx_data;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200851
Christian Daudt0e748232013-05-29 13:50:05 -0700852 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata, 0);
Shawn Guo85d65092011-05-27 23:48:12 +0800853 if (IS_ERR(host))
854 return PTR_ERR(host);
855
856 pltfm_host = sdhci_priv(host);
857
Shawn Guoe3af31c2012-11-26 14:39:43 +0800858 imx_data = devm_kzalloc(&pdev->dev, sizeof(*imx_data), GFP_KERNEL);
Shawn Guoabfafc22011-06-30 15:44:44 +0800859 if (!imx_data) {
860 err = -ENOMEM;
Shawn Guoe3af31c2012-11-26 14:39:43 +0800861 goto free_sdhci;
Shawn Guoabfafc22011-06-30 15:44:44 +0800862 }
Shawn Guo57ed3312011-06-30 09:24:26 +0800863
Shawn Guo3770ee82013-10-17 15:19:46 +0800864 imx_data->devtype = of_id ? (enum imx_esdhc_type) of_id->data :
865 pdev->id_entry->driver_data;
Shawn Guo85d65092011-05-27 23:48:12 +0800866 pltfm_host->priv = imx_data;
867
Shawn Guo31fbb302013-10-17 15:19:44 +0800868 if (is_imx25_esdhc(imx_data) || is_imx35_esdhc(imx_data))
869 imx_data->flags |= ESDHC_FLAG_ENGCM07207;
870
Shawn Guo9d61c002013-10-17 15:19:45 +0800871 if (is_imx6q_usdhc(imx_data))
872 imx_data->flags |= ESDHC_FLAG_USDHC;
873
Sascha Hauer52dac612012-03-07 09:31:34 +0100874 imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
875 if (IS_ERR(imx_data->clk_ipg)) {
876 err = PTR_ERR(imx_data->clk_ipg);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800877 goto free_sdhci;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200878 }
Sascha Hauer52dac612012-03-07 09:31:34 +0100879
880 imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
881 if (IS_ERR(imx_data->clk_ahb)) {
882 err = PTR_ERR(imx_data->clk_ahb);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800883 goto free_sdhci;
Sascha Hauer52dac612012-03-07 09:31:34 +0100884 }
885
886 imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
887 if (IS_ERR(imx_data->clk_per)) {
888 err = PTR_ERR(imx_data->clk_per);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800889 goto free_sdhci;
Sascha Hauer52dac612012-03-07 09:31:34 +0100890 }
891
892 pltfm_host->clk = imx_data->clk_per;
893
894 clk_prepare_enable(imx_data->clk_per);
895 clk_prepare_enable(imx_data->clk_ipg);
896 clk_prepare_enable(imx_data->clk_ahb);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200897
Dong Aishengad932202013-09-13 19:11:35 +0800898 imx_data->pinctrl = devm_pinctrl_get(&pdev->dev);
Dong Aishenge62d8b82012-05-11 14:56:01 +0800899 if (IS_ERR(imx_data->pinctrl)) {
900 err = PTR_ERR(imx_data->pinctrl);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800901 goto disable_clk;
Dong Aishenge62d8b82012-05-11 14:56:01 +0800902 }
903
Dong Aishengad932202013-09-13 19:11:35 +0800904 imx_data->pins_default = pinctrl_lookup_state(imx_data->pinctrl,
905 PINCTRL_STATE_DEFAULT);
906 if (IS_ERR(imx_data->pins_default)) {
907 err = PTR_ERR(imx_data->pins_default);
908 dev_err(mmc_dev(host->mmc), "could not get default state\n");
909 goto disable_clk;
910 }
911
Eric Bénardb89152822012-04-18 02:30:20 +0200912 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
Eric Bénard37865fe2010-10-23 01:57:21 +0200913
Shawn Guo31fbb302013-10-17 15:19:44 +0800914 if (imx_data->flags & ESDHC_FLAG_ENGCM07207)
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100915 /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
Richard Zhu97e4ba62011-08-11 16:51:46 -0400916 host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK
917 | SDHCI_QUIRK_BROKEN_ADMA;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100918
Shawn Guo57ed3312011-06-30 09:24:26 +0800919 if (is_imx53_esdhc(imx_data))
Richard Zhu58ac8172011-03-21 13:22:16 +0800920 imx_data->flags |= ESDHC_FLAG_MULTIBLK_NO_INT;
921
Shawn Guof750ba92011-11-10 16:39:32 +0800922 /*
923 * The imx6q ROM code will change the default watermark level setting
924 * to something insane. Change it back here.
925 */
Shawn Guo9d61c002013-10-17 15:19:45 +0800926 if (esdhc_is_usdhc(imx_data))
Shawn Guo60bf6392013-01-15 23:36:53 +0800927 writel(0x08100810, host->ioaddr + ESDHC_WTMK_LVL);
Shawn Guof750ba92011-11-10 16:39:32 +0800928
Shawn Guo842afc02011-07-06 22:57:48 +0800929 boarddata = &imx_data->boarddata;
Shawn Guoabfafc22011-06-30 15:44:44 +0800930 if (sdhci_esdhc_imx_probe_dt(pdev, boarddata) < 0) {
931 if (!host->mmc->parent->platform_data) {
932 dev_err(mmc_dev(host->mmc), "no board data!\n");
933 err = -EINVAL;
Shawn Guoe3af31c2012-11-26 14:39:43 +0800934 goto disable_clk;
Shawn Guoabfafc22011-06-30 15:44:44 +0800935 }
936 imx_data->boarddata = *((struct esdhc_platform_data *)
937 host->mmc->parent->platform_data);
938 }
Shawn Guo913413c2011-06-21 22:41:51 +0800939
940 /* write_protect */
941 if (boarddata->wp_type == ESDHC_WP_GPIO) {
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800942 err = mmc_gpio_request_ro(host->mmc, boarddata->wp_gpio);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100943 if (err) {
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800944 dev_err(mmc_dev(host->mmc),
945 "failed to request write-protect gpio!\n");
946 goto disable_clk;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100947 }
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800948 host->mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
Shawn Guo913413c2011-06-21 22:41:51 +0800949 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100950
Shawn Guo913413c2011-06-21 22:41:51 +0800951 /* card_detect */
Shawn Guo913413c2011-06-21 22:41:51 +0800952 switch (boarddata->cd_type) {
953 case ESDHC_CD_GPIO:
Laurent Pinchart214fc302013-08-08 12:38:31 +0200954 err = mmc_gpio_request_cd(host->mmc, boarddata->cd_gpio, 0);
Wolfram Sang7e29c302011-02-26 14:44:41 +0100955 if (err) {
Shawn Guo913413c2011-06-21 22:41:51 +0800956 dev_err(mmc_dev(host->mmc),
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800957 "failed to request card-detect gpio!\n");
Shawn Guoe3af31c2012-11-26 14:39:43 +0800958 goto disable_clk;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100959 }
Shawn Guo913413c2011-06-21 22:41:51 +0800960 /* fall through */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100961
Shawn Guo913413c2011-06-21 22:41:51 +0800962 case ESDHC_CD_CONTROLLER:
963 /* we have a working card_detect back */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100964 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
Shawn Guo913413c2011-06-21 22:41:51 +0800965 break;
966
967 case ESDHC_CD_PERMANENT:
968 host->mmc->caps = MMC_CAP_NONREMOVABLE;
969 break;
970
971 case ESDHC_CD_NONE:
972 break;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100973 }
Eric Bénard16a790b2010-10-23 01:57:22 +0200974
Sascha Haueraf510792013-01-21 19:02:28 +0800975 switch (boarddata->max_bus_width) {
976 case 8:
977 host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA;
978 break;
979 case 4:
980 host->mmc->caps |= MMC_CAP_4_BIT_DATA;
981 break;
982 case 1:
983 default:
984 host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
985 break;
986 }
987
Dong Aishengad932202013-09-13 19:11:35 +0800988 /* sdr50 and sdr104 needs work on 1.8v signal voltage */
Shawn Guo9d61c002013-10-17 15:19:45 +0800989 if ((boarddata->support_vsel) && esdhc_is_usdhc(imx_data)) {
Dong Aishengad932202013-09-13 19:11:35 +0800990 imx_data->pins_100mhz = pinctrl_lookup_state(imx_data->pinctrl,
991 ESDHC_PINCTRL_STATE_100MHZ);
992 imx_data->pins_200mhz = pinctrl_lookup_state(imx_data->pinctrl,
993 ESDHC_PINCTRL_STATE_200MHZ);
994 if (IS_ERR(imx_data->pins_100mhz) ||
995 IS_ERR(imx_data->pins_200mhz)) {
996 dev_warn(mmc_dev(host->mmc),
997 "could not get ultra high speed state, work on normal mode\n");
998 /* fall back to not support uhs by specify no 1.8v quirk */
999 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1000 }
1001 } else {
1002 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1003 }
1004
Shawn Guo85d65092011-05-27 23:48:12 +08001005 err = sdhci_add_host(host);
1006 if (err)
Shawn Guoe3af31c2012-11-26 14:39:43 +08001007 goto disable_clk;
Shawn Guo85d65092011-05-27 23:48:12 +08001008
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001009 return 0;
Wolfram Sang7e29c302011-02-26 14:44:41 +01001010
Shawn Guoe3af31c2012-11-26 14:39:43 +08001011disable_clk:
Sascha Hauer52dac612012-03-07 09:31:34 +01001012 clk_disable_unprepare(imx_data->clk_per);
1013 clk_disable_unprepare(imx_data->clk_ipg);
1014 clk_disable_unprepare(imx_data->clk_ahb);
Shawn Guoe3af31c2012-11-26 14:39:43 +08001015free_sdhci:
Shawn Guo85d65092011-05-27 23:48:12 +08001016 sdhci_pltfm_free(pdev);
1017 return err;
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001018}
1019
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001020static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001021{
Shawn Guo85d65092011-05-27 23:48:12 +08001022 struct sdhci_host *host = platform_get_drvdata(pdev);
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001023 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -04001024 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Shawn Guo85d65092011-05-27 23:48:12 +08001025 int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
1026
1027 sdhci_remove_host(host, dead);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +01001028
Sascha Hauer52dac612012-03-07 09:31:34 +01001029 clk_disable_unprepare(imx_data->clk_per);
1030 clk_disable_unprepare(imx_data->clk_ipg);
1031 clk_disable_unprepare(imx_data->clk_ahb);
1032
Shawn Guo85d65092011-05-27 23:48:12 +08001033 sdhci_pltfm_free(pdev);
1034
1035 return 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001036}
1037
Shawn Guo85d65092011-05-27 23:48:12 +08001038static struct platform_driver sdhci_esdhc_imx_driver = {
1039 .driver = {
1040 .name = "sdhci-esdhc-imx",
1041 .owner = THIS_MODULE,
Shawn Guoabfafc22011-06-30 15:44:44 +08001042 .of_match_table = imx_esdhc_dt_ids,
Manuel Lauss29495aa2011-11-03 11:09:45 +01001043 .pm = SDHCI_PLTFM_PMOPS,
Shawn Guo85d65092011-05-27 23:48:12 +08001044 },
Shawn Guo57ed3312011-06-30 09:24:26 +08001045 .id_table = imx_esdhc_devtype,
Shawn Guo85d65092011-05-27 23:48:12 +08001046 .probe = sdhci_esdhc_imx_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05001047 .remove = sdhci_esdhc_imx_remove,
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001048};
Shawn Guo85d65092011-05-27 23:48:12 +08001049
Axel Lind1f81a62011-11-26 12:55:43 +08001050module_platform_driver(sdhci_esdhc_imx_driver);
Shawn Guo85d65092011-05-27 23:48:12 +08001051
1052MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
1053MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
1054MODULE_LICENSE("GPL v2");