blob: c84c808ab00b277e162a8c6844c72345efeee36b [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 Guof47c4bb2013-10-17 15:19:47 +080098struct esdhc_soc_data {
99 u32 flags;
100};
101
102static struct esdhc_soc_data esdhc_imx25_data = {
103 .flags = ESDHC_FLAG_ENGCM07207,
104};
105
106static struct esdhc_soc_data esdhc_imx35_data = {
107 .flags = ESDHC_FLAG_ENGCM07207,
108};
109
110static struct esdhc_soc_data esdhc_imx51_data = {
111 .flags = 0,
112};
113
114static struct esdhc_soc_data esdhc_imx53_data = {
115 .flags = ESDHC_FLAG_MULTIBLK_NO_INT,
116};
117
118static struct esdhc_soc_data usdhc_imx6q_data = {
119 .flags = ESDHC_FLAG_USDHC,
Shawn Guo57ed3312011-06-30 09:24:26 +0800120};
121
Richard Zhue1498602011-03-25 09:18:27 -0400122struct pltfm_imx_data {
Richard Zhue1498602011-03-25 09:18:27 -0400123 u32 scratchpad;
Dong Aishenge62d8b82012-05-11 14:56:01 +0800124 struct pinctrl *pinctrl;
Dong Aishengad932202013-09-13 19:11:35 +0800125 struct pinctrl_state *pins_default;
126 struct pinctrl_state *pins_100mhz;
127 struct pinctrl_state *pins_200mhz;
Shawn Guof47c4bb2013-10-17 15:19:47 +0800128 const struct esdhc_soc_data *socdata;
Shawn Guo842afc02011-07-06 22:57:48 +0800129 struct esdhc_platform_data boarddata;
Sascha Hauer52dac612012-03-07 09:31:34 +0100130 struct clk *clk_ipg;
131 struct clk *clk_ahb;
132 struct clk *clk_per;
Lucas Stach361b8482013-03-15 09:49:26 +0100133 enum {
134 NO_CMD_PENDING, /* no multiblock command pending*/
135 MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */
136 WAIT_FOR_INT, /* sent CMD12, waiting for response INT */
137 } multiblock_status;
Dong Aisheng03221912013-09-13 19:11:34 +0800138 u32 uhs_mode;
Richard Zhue1498602011-03-25 09:18:27 -0400139};
140
Shawn Guo57ed3312011-06-30 09:24:26 +0800141static struct platform_device_id imx_esdhc_devtype[] = {
142 {
143 .name = "sdhci-esdhc-imx25",
Shawn Guof47c4bb2013-10-17 15:19:47 +0800144 .driver_data = (kernel_ulong_t) &esdhc_imx25_data,
Shawn Guo57ed3312011-06-30 09:24:26 +0800145 }, {
146 .name = "sdhci-esdhc-imx35",
Shawn Guof47c4bb2013-10-17 15:19:47 +0800147 .driver_data = (kernel_ulong_t) &esdhc_imx35_data,
Shawn Guo57ed3312011-06-30 09:24:26 +0800148 }, {
149 .name = "sdhci-esdhc-imx51",
Shawn Guof47c4bb2013-10-17 15:19:47 +0800150 .driver_data = (kernel_ulong_t) &esdhc_imx51_data,
Shawn Guo57ed3312011-06-30 09:24:26 +0800151 }, {
Shawn Guo57ed3312011-06-30 09:24:26 +0800152 /* sentinel */
153 }
154};
155MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
156
Shawn Guoabfafc22011-06-30 15:44:44 +0800157static const struct of_device_id imx_esdhc_dt_ids[] = {
Shawn Guof47c4bb2013-10-17 15:19:47 +0800158 { .compatible = "fsl,imx25-esdhc", .data = &esdhc_imx25_data, },
159 { .compatible = "fsl,imx35-esdhc", .data = &esdhc_imx35_data, },
160 { .compatible = "fsl,imx51-esdhc", .data = &esdhc_imx51_data, },
161 { .compatible = "fsl,imx53-esdhc", .data = &esdhc_imx53_data, },
162 { .compatible = "fsl,imx6q-usdhc", .data = &usdhc_imx6q_data, },
Shawn Guoabfafc22011-06-30 15:44:44 +0800163 { /* sentinel */ }
164};
165MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
166
Shawn Guo57ed3312011-06-30 09:24:26 +0800167static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
168{
Shawn Guof47c4bb2013-10-17 15:19:47 +0800169 return data->socdata == &esdhc_imx25_data;
Shawn Guo57ed3312011-06-30 09:24:26 +0800170}
171
172static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
173{
Shawn Guof47c4bb2013-10-17 15:19:47 +0800174 return data->socdata == &esdhc_imx53_data;
Shawn Guo57ed3312011-06-30 09:24:26 +0800175}
176
Shawn Guo95a24822011-09-19 17:32:21 +0800177static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
178{
Shawn Guof47c4bb2013-10-17 15:19:47 +0800179 return data->socdata == &usdhc_imx6q_data;
Shawn Guo95a24822011-09-19 17:32:21 +0800180}
181
Shawn Guo9d61c002013-10-17 15:19:45 +0800182static inline int esdhc_is_usdhc(struct pltfm_imx_data *data)
183{
Shawn Guof47c4bb2013-10-17 15:19:47 +0800184 return !!(data->socdata->flags & ESDHC_FLAG_USDHC);
Shawn Guo9d61c002013-10-17 15:19:45 +0800185}
186
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200187static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
188{
189 void __iomem *base = host->ioaddr + (reg & ~0x3);
190 u32 shift = (reg & 0x3) * 8;
191
192 writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
193}
194
Wolfram Sang7e29c302011-02-26 14:44:41 +0100195static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
196{
Lucas Stach361b8482013-03-15 09:49:26 +0100197 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
198 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100199 u32 val = readl(host->ioaddr + reg);
200
Dong Aisheng03221912013-09-13 19:11:34 +0800201 if (unlikely(reg == SDHCI_PRESENT_STATE)) {
202 u32 fsl_prss = val;
203 /* save the least 20 bits */
204 val = fsl_prss & 0x000FFFFF;
205 /* move dat[0-3] bits */
206 val |= (fsl_prss & 0x0F000000) >> 4;
207 /* move cmd line bit */
208 val |= (fsl_prss & 0x00800000) << 1;
209 }
210
Richard Zhu97e4ba62011-08-11 16:51:46 -0400211 if (unlikely(reg == SDHCI_CAPABILITIES)) {
212 /* In FSL esdhc IC module, only bit20 is used to indicate the
213 * ADMA2 capability of esdhc, but this bit is messed up on
214 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
215 * don't actually support ADMA2). So set the BROKEN_ADMA
216 * uirk on MX25/35 platforms.
217 */
218
219 if (val & SDHCI_CAN_DO_ADMA1) {
220 val &= ~SDHCI_CAN_DO_ADMA1;
221 val |= SDHCI_CAN_DO_ADMA2;
222 }
223 }
224
Shawn Guo9d61c002013-10-17 15:19:45 +0800225 if (unlikely(reg == SDHCI_CAPABILITIES_1) && esdhc_is_usdhc(imx_data))
Dong Aisheng03221912013-09-13 19:11:34 +0800226 val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104
227 | SDHCI_SUPPORT_SDR50;
228
Shawn Guo9d61c002013-10-17 15:19:45 +0800229 if (unlikely(reg == SDHCI_MAX_CURRENT) && esdhc_is_usdhc(imx_data)) {
Dong Aisheng03221912013-09-13 19:11:34 +0800230 val = 0;
231 val |= 0xFF << SDHCI_MAX_CURRENT_330_SHIFT;
232 val |= 0xFF << SDHCI_MAX_CURRENT_300_SHIFT;
233 val |= 0xFF << SDHCI_MAX_CURRENT_180_SHIFT;
234 }
235
Richard Zhu97e4ba62011-08-11 16:51:46 -0400236 if (unlikely(reg == SDHCI_INT_STATUS)) {
Shawn Guo60bf6392013-01-15 23:36:53 +0800237 if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
238 val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
Richard Zhu97e4ba62011-08-11 16:51:46 -0400239 val |= SDHCI_INT_ADMA_ERROR;
240 }
Lucas Stach361b8482013-03-15 09:49:26 +0100241
242 /*
243 * mask off the interrupt we get in response to the manually
244 * sent CMD12
245 */
246 if ((imx_data->multiblock_status == WAIT_FOR_INT) &&
247 ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) {
248 val &= ~SDHCI_INT_RESPONSE;
249 writel(SDHCI_INT_RESPONSE, host->ioaddr +
250 SDHCI_INT_STATUS);
251 imx_data->multiblock_status = NO_CMD_PENDING;
252 }
Richard Zhu97e4ba62011-08-11 16:51:46 -0400253 }
254
Wolfram Sang7e29c302011-02-26 14:44:41 +0100255 return val;
256}
257
258static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
259{
Richard Zhue1498602011-03-25 09:18:27 -0400260 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
261 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Tony Lin0d588642011-08-11 16:45:59 -0400262 u32 data;
Richard Zhue1498602011-03-25 09:18:27 -0400263
Tony Lin0d588642011-08-11 16:45:59 -0400264 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
Tony Lin0d588642011-08-11 16:45:59 -0400265 if (val & SDHCI_INT_CARD_INT) {
266 /*
267 * Clear and then set D3CD bit to avoid missing the
268 * card interrupt. This is a eSDHC controller problem
269 * so we need to apply the following workaround: clear
270 * and set D3CD bit will make eSDHC re-sample the card
271 * interrupt. In case a card interrupt was lost,
272 * re-sample it by the following steps.
273 */
274 data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
Shawn Guo60bf6392013-01-15 23:36:53 +0800275 data &= ~ESDHC_CTRL_D3CD;
Tony Lin0d588642011-08-11 16:45:59 -0400276 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
Shawn Guo60bf6392013-01-15 23:36:53 +0800277 data |= ESDHC_CTRL_D3CD;
Tony Lin0d588642011-08-11 16:45:59 -0400278 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
279 }
280 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100281
Shawn Guof47c4bb2013-10-17 15:19:47 +0800282 if (unlikely((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
Richard Zhu58ac8172011-03-21 13:22:16 +0800283 && (reg == SDHCI_INT_STATUS)
284 && (val & SDHCI_INT_DATA_END))) {
285 u32 v;
Shawn Guo60bf6392013-01-15 23:36:53 +0800286 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
287 v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
288 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
Lucas Stach361b8482013-03-15 09:49:26 +0100289
290 if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS)
291 {
292 /* send a manual CMD12 with RESPTYP=none */
293 data = MMC_STOP_TRANSMISSION << 24 |
294 SDHCI_CMD_ABORTCMD << 16;
295 writel(data, host->ioaddr + SDHCI_TRANSFER_MODE);
296 imx_data->multiblock_status = WAIT_FOR_INT;
297 }
Richard Zhu58ac8172011-03-21 13:22:16 +0800298 }
299
Richard Zhu97e4ba62011-08-11 16:51:46 -0400300 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
301 if (val & SDHCI_INT_ADMA_ERROR) {
302 val &= ~SDHCI_INT_ADMA_ERROR;
Shawn Guo60bf6392013-01-15 23:36:53 +0800303 val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
Richard Zhu97e4ba62011-08-11 16:51:46 -0400304 }
305 }
306
Wolfram Sang7e29c302011-02-26 14:44:41 +0100307 writel(val, host->ioaddr + reg);
308}
309
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200310static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
311{
Shawn Guoef4d0882013-01-15 23:30:27 +0800312 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
313 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aisheng03221912013-09-13 19:11:34 +0800314 u16 ret = 0;
315 u32 val;
Shawn Guoef4d0882013-01-15 23:30:27 +0800316
Shawn Guo95a24822011-09-19 17:32:21 +0800317 if (unlikely(reg == SDHCI_HOST_VERSION)) {
Shawn Guoef4d0882013-01-15 23:30:27 +0800318 reg ^= 2;
Shawn Guo9d61c002013-10-17 15:19:45 +0800319 if (esdhc_is_usdhc(imx_data)) {
Shawn Guoef4d0882013-01-15 23:30:27 +0800320 /*
321 * The usdhc register returns a wrong host version.
322 * Correct it here.
323 */
324 return SDHCI_SPEC_300;
325 }
Shawn Guo95a24822011-09-19 17:32:21 +0800326 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200327
Dong Aisheng03221912013-09-13 19:11:34 +0800328 if (unlikely(reg == SDHCI_HOST_CONTROL2)) {
329 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
330 if (val & ESDHC_VENDOR_SPEC_VSELECT)
331 ret |= SDHCI_CTRL_VDD_180;
332
Shawn Guo9d61c002013-10-17 15:19:45 +0800333 if (esdhc_is_usdhc(imx_data)) {
Dong Aisheng03221912013-09-13 19:11:34 +0800334 val = readl(host->ioaddr + ESDHC_MIX_CTRL);
335 if (val & ESDHC_MIX_CTRL_EXE_TUNE)
336 ret |= SDHCI_CTRL_EXEC_TUNING;
337 if (val & ESDHC_MIX_CTRL_SMPCLK_SEL)
338 ret |= SDHCI_CTRL_TUNED_CLK;
339 }
340
341 ret |= (imx_data->uhs_mode & SDHCI_CTRL_UHS_MASK);
342 ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
343
344 return ret;
345 }
346
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200347 return readw(host->ioaddr + reg);
348}
349
350static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
351{
352 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400353 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aisheng03221912013-09-13 19:11:34 +0800354 u32 new_val = 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200355
356 switch (reg) {
Dong Aisheng03221912013-09-13 19:11:34 +0800357 case SDHCI_CLOCK_CONTROL:
358 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
359 if (val & SDHCI_CLOCK_CARD_EN)
360 new_val |= ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
361 else
362 new_val &= ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
363 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
364 return;
365 case SDHCI_HOST_CONTROL2:
366 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
367 if (val & SDHCI_CTRL_VDD_180)
368 new_val |= ESDHC_VENDOR_SPEC_VSELECT;
369 else
370 new_val &= ~ESDHC_VENDOR_SPEC_VSELECT;
371 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
372 imx_data->uhs_mode = val & SDHCI_CTRL_UHS_MASK;
373 new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
374 if (val & SDHCI_CTRL_TUNED_CLK)
375 new_val |= ESDHC_MIX_CTRL_SMPCLK_SEL;
376 else
377 new_val &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
378 writel(new_val , host->ioaddr + ESDHC_MIX_CTRL);
379 return;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200380 case SDHCI_TRANSFER_MODE:
Shawn Guof47c4bb2013-10-17 15:19:47 +0800381 if ((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
Richard Zhu58ac8172011-03-21 13:22:16 +0800382 && (host->cmd->opcode == SD_IO_RW_EXTENDED)
383 && (host->cmd->data->blocks > 1)
384 && (host->cmd->data->flags & MMC_DATA_READ)) {
385 u32 v;
Shawn Guo60bf6392013-01-15 23:36:53 +0800386 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
387 v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
388 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
Richard Zhu58ac8172011-03-21 13:22:16 +0800389 }
Shawn Guo69f54692013-01-21 19:02:24 +0800390
Shawn Guo9d61c002013-10-17 15:19:45 +0800391 if (esdhc_is_usdhc(imx_data)) {
Shawn Guo69f54692013-01-21 19:02:24 +0800392 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
Shawn Guo2a15f982013-01-21 19:02:26 +0800393 /* Swap AC23 bit */
394 if (val & SDHCI_TRNS_AUTO_CMD23) {
395 val &= ~SDHCI_TRNS_AUTO_CMD23;
396 val |= ESDHC_MIX_CTRL_AC23EN;
397 }
398 m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
Shawn Guo69f54692013-01-21 19:02:24 +0800399 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
400 } else {
401 /*
402 * Postpone this write, we must do it together with a
403 * command write that is down below.
404 */
405 imx_data->scratchpad = val;
406 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200407 return;
408 case SDHCI_COMMAND:
Lucas Stach361b8482013-03-15 09:49:26 +0100409 if (host->cmd->opcode == MMC_STOP_TRANSMISSION)
Richard Zhu58ac8172011-03-21 13:22:16 +0800410 val |= SDHCI_CMD_ABORTCMD;
Shawn Guo95a24822011-09-19 17:32:21 +0800411
Lucas Stach361b8482013-03-15 09:49:26 +0100412 if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
Shawn Guof47c4bb2013-10-17 15:19:47 +0800413 (imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
Lucas Stach361b8482013-03-15 09:49:26 +0100414 imx_data->multiblock_status = MULTIBLK_IN_PROCESS;
415
Shawn Guo9d61c002013-10-17 15:19:45 +0800416 if (esdhc_is_usdhc(imx_data))
Shawn Guo95a24822011-09-19 17:32:21 +0800417 writel(val << 16,
418 host->ioaddr + SDHCI_TRANSFER_MODE);
Shawn Guo69f54692013-01-21 19:02:24 +0800419 else
Shawn Guo95a24822011-09-19 17:32:21 +0800420 writel(val << 16 | imx_data->scratchpad,
421 host->ioaddr + SDHCI_TRANSFER_MODE);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200422 return;
423 case SDHCI_BLOCK_SIZE:
424 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
425 break;
426 }
427 esdhc_clrset_le(host, 0xffff, val, reg);
428}
429
430static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
431{
Wilson Callan9a0985b2012-07-19 02:49:16 -0400432 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
433 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200434 u32 new_val;
Sascha Haueraf510792013-01-21 19:02:28 +0800435 u32 mask;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200436
437 switch (reg) {
438 case SDHCI_POWER_CONTROL:
439 /*
440 * FSL put some DMA bits here
441 * If your board has a regulator, code should be here
442 */
443 return;
444 case SDHCI_HOST_CONTROL:
Shawn Guo6b40d182013-01-15 23:36:52 +0800445 /* FSL messed up here, so we need to manually compose it. */
Sascha Haueraf510792013-01-21 19:02:28 +0800446 new_val = val & SDHCI_CTRL_LED;
Masanari Iida7122bbb2012-08-05 23:25:40 +0900447 /* ensure the endianness */
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200448 new_val |= ESDHC_HOST_CONTROL_LE;
Wilson Callan9a0985b2012-07-19 02:49:16 -0400449 /* bits 8&9 are reserved on mx25 */
450 if (!is_imx25_esdhc(imx_data)) {
451 /* DMA mode bits are shifted */
452 new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
453 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200454
Sascha Haueraf510792013-01-21 19:02:28 +0800455 /*
456 * Do not touch buswidth bits here. This is done in
457 * esdhc_pltfm_bus_width.
Martin Fuzzeyf6825742013-04-15 17:08:35 +0200458 * Do not touch the D3CD bit either which is used for the
459 * SDIO interrupt errata workaround.
Sascha Haueraf510792013-01-21 19:02:28 +0800460 */
Martin Fuzzeyf6825742013-04-15 17:08:35 +0200461 mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD);
Sascha Haueraf510792013-01-21 19:02:28 +0800462
463 esdhc_clrset_le(host, mask, new_val, reg);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200464 return;
465 }
466 esdhc_clrset_le(host, 0xff, val, reg);
Shawn Guo913413c2011-06-21 22:41:51 +0800467
468 /*
469 * The esdhc has a design violation to SDHC spec which tells
470 * that software reset should not affect card detection circuit.
471 * But esdhc clears its SYSCTL register bits [0..2] during the
472 * software reset. This will stop those clocks that card detection
473 * circuit relies on. To work around it, we turn the clocks on back
474 * to keep card detection circuit functional.
475 */
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800476 if ((reg == SDHCI_SOFTWARE_RESET) && (val & 1)) {
Shawn Guo913413c2011-06-21 22:41:51 +0800477 esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800478 /*
479 * The reset on usdhc fails to clear MIX_CTRL register.
480 * Do it manually here.
481 */
Shawn Guo9d61c002013-10-17 15:19:45 +0800482 if (esdhc_is_usdhc(imx_data))
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800483 writel(0, host->ioaddr + ESDHC_MIX_CTRL);
484 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200485}
486
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200487static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
488{
489 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
490 struct pltfm_imx_data *imx_data = pltfm_host->priv;
491 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
492
493 u32 f_host = clk_get_rate(pltfm_host->clk);
494
495 if (boarddata->f_max && (boarddata->f_max < f_host))
496 return boarddata->f_max;
497 else
498 return f_host;
499}
500
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200501static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
502{
503 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
504
505 return clk_get_rate(pltfm_host->clk) / 256 / 16;
506}
507
Lucas Stach8ba95802013-06-05 15:13:25 +0200508static inline void esdhc_pltfm_set_clock(struct sdhci_host *host,
509 unsigned int clock)
510{
511 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800512 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aishengd31fc002013-09-13 19:11:32 +0800513 unsigned int host_clock = clk_get_rate(pltfm_host->clk);
514 int pre_div = 2;
515 int div = 1;
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800516 u32 temp, val;
Lucas Stach8ba95802013-06-05 15:13:25 +0200517
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800518 if (clock == 0) {
Shawn Guo9d61c002013-10-17 15:19:45 +0800519 if (esdhc_is_usdhc(imx_data)) {
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800520 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
521 writel(val & ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
522 host->ioaddr + ESDHC_VENDOR_SPEC);
523 }
Dong Aishengd31fc002013-09-13 19:11:32 +0800524 goto out;
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800525 }
Dong Aishengd31fc002013-09-13 19:11:32 +0800526
Shawn Guo9d61c002013-10-17 15:19:45 +0800527 if (esdhc_is_usdhc(imx_data))
Dong Aisheng5f7886c2013-09-13 19:11:36 +0800528 pre_div = 1;
529
Dong Aishengd31fc002013-09-13 19:11:32 +0800530 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
531 temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
532 | ESDHC_CLOCK_MASK);
533 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
534
535 while (host_clock / pre_div / 16 > clock && pre_div < 256)
536 pre_div *= 2;
537
538 while (host_clock / pre_div / div > clock && div < 16)
539 div++;
540
Dong Aishenge76b8552013-09-13 19:11:37 +0800541 host->mmc->actual_clock = host_clock / pre_div / div;
Dong Aishengd31fc002013-09-13 19:11:32 +0800542 dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
Dong Aishenge76b8552013-09-13 19:11:37 +0800543 clock, host->mmc->actual_clock);
Dong Aishengd31fc002013-09-13 19:11:32 +0800544
545 pre_div >>= 1;
546 div--;
547
548 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
549 temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
550 | (div << ESDHC_DIVIDER_SHIFT)
551 | (pre_div << ESDHC_PREDIV_SHIFT));
552 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800553
Shawn Guo9d61c002013-10-17 15:19:45 +0800554 if (esdhc_is_usdhc(imx_data)) {
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800555 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
556 writel(val | ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
557 host->ioaddr + ESDHC_VENDOR_SPEC);
558 }
559
Dong Aishengd31fc002013-09-13 19:11:32 +0800560 mdelay(1);
561out:
562 host->clock = clock;
Lucas Stach8ba95802013-06-05 15:13:25 +0200563}
564
Shawn Guo913413c2011-06-21 22:41:51 +0800565static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
566{
Shawn Guo842afc02011-07-06 22:57:48 +0800567 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
568 struct pltfm_imx_data *imx_data = pltfm_host->priv;
569 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Shawn Guo913413c2011-06-21 22:41:51 +0800570
571 switch (boarddata->wp_type) {
572 case ESDHC_WP_GPIO:
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800573 return mmc_gpio_get_ro(host->mmc);
Shawn Guo913413c2011-06-21 22:41:51 +0800574 case ESDHC_WP_CONTROLLER:
575 return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
576 SDHCI_WRITE_PROTECT);
577 case ESDHC_WP_NONE:
578 break;
579 }
580
581 return -ENOSYS;
582}
583
Sascha Haueraf510792013-01-21 19:02:28 +0800584static int esdhc_pltfm_bus_width(struct sdhci_host *host, int width)
585{
586 u32 ctrl;
587
588 switch (width) {
589 case MMC_BUS_WIDTH_8:
590 ctrl = ESDHC_CTRL_8BITBUS;
591 break;
592 case MMC_BUS_WIDTH_4:
593 ctrl = ESDHC_CTRL_4BITBUS;
594 break;
595 default:
596 ctrl = 0;
597 break;
598 }
599
600 esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
601 SDHCI_HOST_CONTROL);
602
603 return 0;
604}
605
Dong Aisheng03221912013-09-13 19:11:34 +0800606static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val)
607{
608 u32 reg;
609
610 /* FIXME: delay a bit for card to be ready for next tuning due to errors */
611 mdelay(1);
612
613 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
614 reg |= ESDHC_MIX_CTRL_EXE_TUNE | ESDHC_MIX_CTRL_SMPCLK_SEL |
615 ESDHC_MIX_CTRL_FBCLK_SEL;
616 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
617 writel(val << 8, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
618 dev_dbg(mmc_dev(host->mmc),
619 "tunning with delay 0x%x ESDHC_TUNE_CTRL_STATUS 0x%x\n",
620 val, readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS));
621}
622
623static void esdhc_request_done(struct mmc_request *mrq)
624{
625 complete(&mrq->completion);
626}
627
628static int esdhc_send_tuning_cmd(struct sdhci_host *host, u32 opcode)
629{
630 struct mmc_command cmd = {0};
631 struct mmc_request mrq = {0};
632 struct mmc_data data = {0};
633 struct scatterlist sg;
634 char tuning_pattern[ESDHC_TUNING_BLOCK_PATTERN_LEN];
635
636 cmd.opcode = opcode;
637 cmd.arg = 0;
638 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
639
640 data.blksz = ESDHC_TUNING_BLOCK_PATTERN_LEN;
641 data.blocks = 1;
642 data.flags = MMC_DATA_READ;
643 data.sg = &sg;
644 data.sg_len = 1;
645
646 sg_init_one(&sg, tuning_pattern, sizeof(tuning_pattern));
647
648 mrq.cmd = &cmd;
649 mrq.cmd->mrq = &mrq;
650 mrq.data = &data;
651 mrq.data->mrq = &mrq;
652 mrq.cmd->data = mrq.data;
653
654 mrq.done = esdhc_request_done;
655 init_completion(&(mrq.completion));
656
657 disable_irq(host->irq);
658 spin_lock(&host->lock);
659 host->mrq = &mrq;
660
661 sdhci_send_command(host, mrq.cmd);
662
663 spin_unlock(&host->lock);
664 enable_irq(host->irq);
665
666 wait_for_completion(&mrq.completion);
667
668 if (cmd.error)
669 return cmd.error;
670 if (data.error)
671 return data.error;
672
673 return 0;
674}
675
676static void esdhc_post_tuning(struct sdhci_host *host)
677{
678 u32 reg;
679
680 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
681 reg &= ~ESDHC_MIX_CTRL_EXE_TUNE;
682 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
683}
684
685static int esdhc_executing_tuning(struct sdhci_host *host, u32 opcode)
686{
687 int min, max, avg, ret;
688
689 /* find the mininum delay first which can pass tuning */
690 min = ESDHC_TUNE_CTRL_MIN;
691 while (min < ESDHC_TUNE_CTRL_MAX) {
692 esdhc_prepare_tuning(host, min);
693 if (!esdhc_send_tuning_cmd(host, opcode))
694 break;
695 min += ESDHC_TUNE_CTRL_STEP;
696 }
697
698 /* find the maxinum delay which can not pass tuning */
699 max = min + ESDHC_TUNE_CTRL_STEP;
700 while (max < ESDHC_TUNE_CTRL_MAX) {
701 esdhc_prepare_tuning(host, max);
702 if (esdhc_send_tuning_cmd(host, opcode)) {
703 max -= ESDHC_TUNE_CTRL_STEP;
704 break;
705 }
706 max += ESDHC_TUNE_CTRL_STEP;
707 }
708
709 /* use average delay to get the best timing */
710 avg = (min + max) / 2;
711 esdhc_prepare_tuning(host, avg);
712 ret = esdhc_send_tuning_cmd(host, opcode);
713 esdhc_post_tuning(host);
714
715 dev_dbg(mmc_dev(host->mmc), "tunning %s at 0x%x ret %d\n",
716 ret ? "failed" : "passed", avg, ret);
717
718 return ret;
719}
720
Dong Aishengad932202013-09-13 19:11:35 +0800721static int esdhc_change_pinstate(struct sdhci_host *host,
722 unsigned int uhs)
723{
724 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
725 struct pltfm_imx_data *imx_data = pltfm_host->priv;
726 struct pinctrl_state *pinctrl;
727
728 dev_dbg(mmc_dev(host->mmc), "change pinctrl state for uhs %d\n", uhs);
729
730 if (IS_ERR(imx_data->pinctrl) ||
731 IS_ERR(imx_data->pins_default) ||
732 IS_ERR(imx_data->pins_100mhz) ||
733 IS_ERR(imx_data->pins_200mhz))
734 return -EINVAL;
735
736 switch (uhs) {
737 case MMC_TIMING_UHS_SDR50:
738 pinctrl = imx_data->pins_100mhz;
739 break;
740 case MMC_TIMING_UHS_SDR104:
741 pinctrl = imx_data->pins_200mhz;
742 break;
743 default:
744 /* back to default state for other legacy timing */
745 pinctrl = imx_data->pins_default;
746 }
747
748 return pinctrl_select_state(imx_data->pinctrl, pinctrl);
749}
750
751static int esdhc_set_uhs_signaling(struct sdhci_host *host, unsigned int uhs)
752{
753 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
754 struct pltfm_imx_data *imx_data = pltfm_host->priv;
755
756 switch (uhs) {
757 case MMC_TIMING_UHS_SDR12:
758 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR12;
759 break;
760 case MMC_TIMING_UHS_SDR25:
761 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR25;
762 break;
763 case MMC_TIMING_UHS_SDR50:
764 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR50;
765 break;
766 case MMC_TIMING_UHS_SDR104:
767 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR104;
768 break;
769 case MMC_TIMING_UHS_DDR50:
770 imx_data->uhs_mode = SDHCI_CTRL_UHS_DDR50;
771 break;
772 }
773
774 return esdhc_change_pinstate(host, uhs);
775}
776
Lars-Peter Clausenc9155682013-03-13 19:26:05 +0100777static const struct sdhci_ops sdhci_esdhc_ops = {
Richard Zhue1498602011-03-25 09:18:27 -0400778 .read_l = esdhc_readl_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100779 .read_w = esdhc_readw_le,
Richard Zhue1498602011-03-25 09:18:27 -0400780 .write_l = esdhc_writel_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100781 .write_w = esdhc_writew_le,
782 .write_b = esdhc_writeb_le,
Lucas Stach8ba95802013-06-05 15:13:25 +0200783 .set_clock = esdhc_pltfm_set_clock,
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200784 .get_max_clock = esdhc_pltfm_get_max_clock,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100785 .get_min_clock = esdhc_pltfm_get_min_clock,
Shawn Guo913413c2011-06-21 22:41:51 +0800786 .get_ro = esdhc_pltfm_get_ro,
Sascha Haueraf510792013-01-21 19:02:28 +0800787 .platform_bus_width = esdhc_pltfm_bus_width,
Dong Aishengad932202013-09-13 19:11:35 +0800788 .set_uhs_signaling = esdhc_set_uhs_signaling,
Dong Aisheng03221912013-09-13 19:11:34 +0800789 .platform_execute_tuning = esdhc_executing_tuning,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100790};
791
Lars-Peter Clausen1db5eeb2013-03-13 19:26:03 +0100792static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
Richard Zhu97e4ba62011-08-11 16:51:46 -0400793 .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
794 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
795 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
Shawn Guo85d65092011-05-27 23:48:12 +0800796 | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
Shawn Guo85d65092011-05-27 23:48:12 +0800797 .ops = &sdhci_esdhc_ops,
798};
799
Shawn Guoabfafc22011-06-30 15:44:44 +0800800#ifdef CONFIG_OF
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500801static int
Shawn Guoabfafc22011-06-30 15:44:44 +0800802sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
803 struct esdhc_platform_data *boarddata)
804{
805 struct device_node *np = pdev->dev.of_node;
806
807 if (!np)
808 return -ENODEV;
809
Arnd Bergmann7f217792012-05-13 00:14:24 -0400810 if (of_get_property(np, "non-removable", NULL))
Shawn Guoabfafc22011-06-30 15:44:44 +0800811 boarddata->cd_type = ESDHC_CD_PERMANENT;
812
813 if (of_get_property(np, "fsl,cd-controller", NULL))
814 boarddata->cd_type = ESDHC_CD_CONTROLLER;
815
816 if (of_get_property(np, "fsl,wp-controller", NULL))
817 boarddata->wp_type = ESDHC_WP_CONTROLLER;
818
819 boarddata->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
820 if (gpio_is_valid(boarddata->cd_gpio))
821 boarddata->cd_type = ESDHC_CD_GPIO;
822
823 boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
824 if (gpio_is_valid(boarddata->wp_gpio))
825 boarddata->wp_type = ESDHC_WP_GPIO;
826
Sascha Haueraf510792013-01-21 19:02:28 +0800827 of_property_read_u32(np, "bus-width", &boarddata->max_bus_width);
828
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200829 of_property_read_u32(np, "max-frequency", &boarddata->f_max);
830
Dong Aishengad932202013-09-13 19:11:35 +0800831 if (of_find_property(np, "no-1-8-v", NULL))
832 boarddata->support_vsel = false;
833 else
834 boarddata->support_vsel = true;
835
Shawn Guoabfafc22011-06-30 15:44:44 +0800836 return 0;
837}
838#else
839static inline int
840sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
841 struct esdhc_platform_data *boarddata)
842{
843 return -ENODEV;
844}
845#endif
846
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500847static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200848{
Shawn Guoabfafc22011-06-30 15:44:44 +0800849 const struct of_device_id *of_id =
850 of_match_device(imx_esdhc_dt_ids, &pdev->dev);
Shawn Guo85d65092011-05-27 23:48:12 +0800851 struct sdhci_pltfm_host *pltfm_host;
852 struct sdhci_host *host;
853 struct esdhc_platform_data *boarddata;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100854 int err;
Richard Zhue1498602011-03-25 09:18:27 -0400855 struct pltfm_imx_data *imx_data;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200856
Christian Daudt0e748232013-05-29 13:50:05 -0700857 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata, 0);
Shawn Guo85d65092011-05-27 23:48:12 +0800858 if (IS_ERR(host))
859 return PTR_ERR(host);
860
861 pltfm_host = sdhci_priv(host);
862
Shawn Guoe3af31c2012-11-26 14:39:43 +0800863 imx_data = devm_kzalloc(&pdev->dev, sizeof(*imx_data), GFP_KERNEL);
Shawn Guoabfafc22011-06-30 15:44:44 +0800864 if (!imx_data) {
865 err = -ENOMEM;
Shawn Guoe3af31c2012-11-26 14:39:43 +0800866 goto free_sdhci;
Shawn Guoabfafc22011-06-30 15:44:44 +0800867 }
Shawn Guo57ed3312011-06-30 09:24:26 +0800868
Shawn Guof47c4bb2013-10-17 15:19:47 +0800869 imx_data->socdata = of_id ? of_id->data : (struct esdhc_soc_data *)
870 pdev->id_entry->driver_data;
Shawn Guo85d65092011-05-27 23:48:12 +0800871 pltfm_host->priv = imx_data;
872
Sascha Hauer52dac612012-03-07 09:31:34 +0100873 imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
874 if (IS_ERR(imx_data->clk_ipg)) {
875 err = PTR_ERR(imx_data->clk_ipg);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800876 goto free_sdhci;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200877 }
Sascha Hauer52dac612012-03-07 09:31:34 +0100878
879 imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
880 if (IS_ERR(imx_data->clk_ahb)) {
881 err = PTR_ERR(imx_data->clk_ahb);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800882 goto free_sdhci;
Sascha Hauer52dac612012-03-07 09:31:34 +0100883 }
884
885 imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
886 if (IS_ERR(imx_data->clk_per)) {
887 err = PTR_ERR(imx_data->clk_per);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800888 goto free_sdhci;
Sascha Hauer52dac612012-03-07 09:31:34 +0100889 }
890
891 pltfm_host->clk = imx_data->clk_per;
892
893 clk_prepare_enable(imx_data->clk_per);
894 clk_prepare_enable(imx_data->clk_ipg);
895 clk_prepare_enable(imx_data->clk_ahb);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200896
Dong Aishengad932202013-09-13 19:11:35 +0800897 imx_data->pinctrl = devm_pinctrl_get(&pdev->dev);
Dong Aishenge62d8b82012-05-11 14:56:01 +0800898 if (IS_ERR(imx_data->pinctrl)) {
899 err = PTR_ERR(imx_data->pinctrl);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800900 goto disable_clk;
Dong Aishenge62d8b82012-05-11 14:56:01 +0800901 }
902
Dong Aishengad932202013-09-13 19:11:35 +0800903 imx_data->pins_default = pinctrl_lookup_state(imx_data->pinctrl,
904 PINCTRL_STATE_DEFAULT);
905 if (IS_ERR(imx_data->pins_default)) {
906 err = PTR_ERR(imx_data->pins_default);
907 dev_err(mmc_dev(host->mmc), "could not get default state\n");
908 goto disable_clk;
909 }
910
Eric Bénardb89152822012-04-18 02:30:20 +0200911 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
Eric Bénard37865fe2010-10-23 01:57:21 +0200912
Shawn Guof47c4bb2013-10-17 15:19:47 +0800913 if (imx_data->socdata->flags & ESDHC_FLAG_ENGCM07207)
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100914 /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
Richard Zhu97e4ba62011-08-11 16:51:46 -0400915 host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK
916 | SDHCI_QUIRK_BROKEN_ADMA;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100917
Shawn Guof750ba92011-11-10 16:39:32 +0800918 /*
919 * The imx6q ROM code will change the default watermark level setting
920 * to something insane. Change it back here.
921 */
Shawn Guo9d61c002013-10-17 15:19:45 +0800922 if (esdhc_is_usdhc(imx_data))
Shawn Guo60bf6392013-01-15 23:36:53 +0800923 writel(0x08100810, host->ioaddr + ESDHC_WTMK_LVL);
Shawn Guof750ba92011-11-10 16:39:32 +0800924
Shawn Guo842afc02011-07-06 22:57:48 +0800925 boarddata = &imx_data->boarddata;
Shawn Guoabfafc22011-06-30 15:44:44 +0800926 if (sdhci_esdhc_imx_probe_dt(pdev, boarddata) < 0) {
927 if (!host->mmc->parent->platform_data) {
928 dev_err(mmc_dev(host->mmc), "no board data!\n");
929 err = -EINVAL;
Shawn Guoe3af31c2012-11-26 14:39:43 +0800930 goto disable_clk;
Shawn Guoabfafc22011-06-30 15:44:44 +0800931 }
932 imx_data->boarddata = *((struct esdhc_platform_data *)
933 host->mmc->parent->platform_data);
934 }
Shawn Guo913413c2011-06-21 22:41:51 +0800935
936 /* write_protect */
937 if (boarddata->wp_type == ESDHC_WP_GPIO) {
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800938 err = mmc_gpio_request_ro(host->mmc, boarddata->wp_gpio);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100939 if (err) {
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800940 dev_err(mmc_dev(host->mmc),
941 "failed to request write-protect gpio!\n");
942 goto disable_clk;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100943 }
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800944 host->mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
Shawn Guo913413c2011-06-21 22:41:51 +0800945 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100946
Shawn Guo913413c2011-06-21 22:41:51 +0800947 /* card_detect */
Shawn Guo913413c2011-06-21 22:41:51 +0800948 switch (boarddata->cd_type) {
949 case ESDHC_CD_GPIO:
Laurent Pinchart214fc302013-08-08 12:38:31 +0200950 err = mmc_gpio_request_cd(host->mmc, boarddata->cd_gpio, 0);
Wolfram Sang7e29c302011-02-26 14:44:41 +0100951 if (err) {
Shawn Guo913413c2011-06-21 22:41:51 +0800952 dev_err(mmc_dev(host->mmc),
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800953 "failed to request card-detect gpio!\n");
Shawn Guoe3af31c2012-11-26 14:39:43 +0800954 goto disable_clk;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100955 }
Shawn Guo913413c2011-06-21 22:41:51 +0800956 /* fall through */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100957
Shawn Guo913413c2011-06-21 22:41:51 +0800958 case ESDHC_CD_CONTROLLER:
959 /* we have a working card_detect back */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100960 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
Shawn Guo913413c2011-06-21 22:41:51 +0800961 break;
962
963 case ESDHC_CD_PERMANENT:
964 host->mmc->caps = MMC_CAP_NONREMOVABLE;
965 break;
966
967 case ESDHC_CD_NONE:
968 break;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100969 }
Eric Bénard16a790b2010-10-23 01:57:22 +0200970
Sascha Haueraf510792013-01-21 19:02:28 +0800971 switch (boarddata->max_bus_width) {
972 case 8:
973 host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA;
974 break;
975 case 4:
976 host->mmc->caps |= MMC_CAP_4_BIT_DATA;
977 break;
978 case 1:
979 default:
980 host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
981 break;
982 }
983
Dong Aishengad932202013-09-13 19:11:35 +0800984 /* sdr50 and sdr104 needs work on 1.8v signal voltage */
Shawn Guo9d61c002013-10-17 15:19:45 +0800985 if ((boarddata->support_vsel) && esdhc_is_usdhc(imx_data)) {
Dong Aishengad932202013-09-13 19:11:35 +0800986 imx_data->pins_100mhz = pinctrl_lookup_state(imx_data->pinctrl,
987 ESDHC_PINCTRL_STATE_100MHZ);
988 imx_data->pins_200mhz = pinctrl_lookup_state(imx_data->pinctrl,
989 ESDHC_PINCTRL_STATE_200MHZ);
990 if (IS_ERR(imx_data->pins_100mhz) ||
991 IS_ERR(imx_data->pins_200mhz)) {
992 dev_warn(mmc_dev(host->mmc),
993 "could not get ultra high speed state, work on normal mode\n");
994 /* fall back to not support uhs by specify no 1.8v quirk */
995 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
996 }
997 } else {
998 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
999 }
1000
Shawn Guo85d65092011-05-27 23:48:12 +08001001 err = sdhci_add_host(host);
1002 if (err)
Shawn Guoe3af31c2012-11-26 14:39:43 +08001003 goto disable_clk;
Shawn Guo85d65092011-05-27 23:48:12 +08001004
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001005 return 0;
Wolfram Sang7e29c302011-02-26 14:44:41 +01001006
Shawn Guoe3af31c2012-11-26 14:39:43 +08001007disable_clk:
Sascha Hauer52dac612012-03-07 09:31:34 +01001008 clk_disable_unprepare(imx_data->clk_per);
1009 clk_disable_unprepare(imx_data->clk_ipg);
1010 clk_disable_unprepare(imx_data->clk_ahb);
Shawn Guoe3af31c2012-11-26 14:39:43 +08001011free_sdhci:
Shawn Guo85d65092011-05-27 23:48:12 +08001012 sdhci_pltfm_free(pdev);
1013 return err;
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001014}
1015
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001016static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001017{
Shawn Guo85d65092011-05-27 23:48:12 +08001018 struct sdhci_host *host = platform_get_drvdata(pdev);
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001019 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -04001020 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Shawn Guo85d65092011-05-27 23:48:12 +08001021 int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
1022
1023 sdhci_remove_host(host, dead);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +01001024
Sascha Hauer52dac612012-03-07 09:31:34 +01001025 clk_disable_unprepare(imx_data->clk_per);
1026 clk_disable_unprepare(imx_data->clk_ipg);
1027 clk_disable_unprepare(imx_data->clk_ahb);
1028
Shawn Guo85d65092011-05-27 23:48:12 +08001029 sdhci_pltfm_free(pdev);
1030
1031 return 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001032}
1033
Shawn Guo85d65092011-05-27 23:48:12 +08001034static struct platform_driver sdhci_esdhc_imx_driver = {
1035 .driver = {
1036 .name = "sdhci-esdhc-imx",
1037 .owner = THIS_MODULE,
Shawn Guoabfafc22011-06-30 15:44:44 +08001038 .of_match_table = imx_esdhc_dt_ids,
Manuel Lauss29495aa2011-11-03 11:09:45 +01001039 .pm = SDHCI_PLTFM_PMOPS,
Shawn Guo85d65092011-05-27 23:48:12 +08001040 },
Shawn Guo57ed3312011-06-30 09:24:26 +08001041 .id_table = imx_esdhc_devtype,
Shawn Guo85d65092011-05-27 23:48:12 +08001042 .probe = sdhci_esdhc_imx_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05001043 .remove = sdhci_esdhc_imx_remove,
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001044};
Shawn Guo85d65092011-05-27 23:48:12 +08001045
Axel Lind1f81a62011-11-26 12:55:43 +08001046module_platform_driver(sdhci_esdhc_imx_driver);
Shawn Guo85d65092011-05-27 23:48:12 +08001047
1048MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
1049MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
1050MODULE_LICENSE("GPL v2");