blob: 915fa68e5122ee5612c2bd27b67029226aebc6af [file] [log] [blame]
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001/*
2 * Freescale eSDHC i.MX controller driver for the platform bus.
3 *
4 * derived from the OF-version.
5 *
6 * Copyright (c) 2010 Pengutronix e.K.
7 * Author: Wolfram Sang <w.sang@pengutronix.de>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License.
12 */
13
14#include <linux/io.h>
15#include <linux/delay.h>
16#include <linux/err.h>
17#include <linux/clk.h>
Wolfram Sang0c6d49c2011-02-26 14:44:39 +010018#include <linux/gpio.h>
Shawn Guo66506f72011-08-15 10:28:18 +080019#include <linux/module.h>
Richard Zhue1498602011-03-25 09:18:27 -040020#include <linux/slab.h>
Wolfram Sang95f25ef2010-10-15 12:21:04 +020021#include <linux/mmc/host.h>
Richard Zhu58ac8172011-03-21 13:22:16 +080022#include <linux/mmc/mmc.h>
23#include <linux/mmc/sdio.h>
Shawn Guofbe5fdd2012-12-11 22:32:20 +080024#include <linux/mmc/slot-gpio.h>
Shawn Guoabfafc22011-06-30 15:44:44 +080025#include <linux/of.h>
26#include <linux/of_device.h>
27#include <linux/of_gpio.h>
Dong Aishenge62d8b82012-05-11 14:56:01 +080028#include <linux/pinctrl/consumer.h>
Arnd Bergmann82906b12012-08-24 15:14:29 +020029#include <linux/platform_data/mmc-esdhc-imx.h>
Wolfram Sang95f25ef2010-10-15 12:21:04 +020030#include "sdhci-pltfm.h"
31#include "sdhci-esdhc.h"
32
Shawn Guo60bf6392013-01-15 23:36:53 +080033#define ESDHC_CTRL_D3CD 0x08
Richard Zhu58ac8172011-03-21 13:22:16 +080034/* VENDOR SPEC register */
Shawn Guo60bf6392013-01-15 23:36:53 +080035#define ESDHC_VENDOR_SPEC 0xc0
36#define ESDHC_VENDOR_SPEC_SDIO_QUIRK (1 << 1)
Dong Aisheng03221912013-09-13 19:11:34 +080037#define ESDHC_VENDOR_SPEC_VSELECT (1 << 1)
Dong Aishengfed2f6e2013-09-13 19:11:33 +080038#define ESDHC_VENDOR_SPEC_FRC_SDCLK_ON (1 << 8)
Shawn Guo60bf6392013-01-15 23:36:53 +080039#define ESDHC_WTMK_LVL 0x44
40#define ESDHC_MIX_CTRL 0x48
Dong Aishengde5bdbf2013-10-18 19:48:46 +080041#define ESDHC_MIX_CTRL_DDREN (1 << 3)
Shawn Guo2a15f982013-01-21 19:02:26 +080042#define ESDHC_MIX_CTRL_AC23EN (1 << 7)
Dong Aisheng03221912013-09-13 19:11:34 +080043#define ESDHC_MIX_CTRL_EXE_TUNE (1 << 22)
44#define ESDHC_MIX_CTRL_SMPCLK_SEL (1 << 23)
45#define ESDHC_MIX_CTRL_FBCLK_SEL (1 << 25)
Shawn Guo2a15f982013-01-21 19:02:26 +080046/* Bits 3 and 6 are not SDHCI standard definitions */
47#define ESDHC_MIX_CTRL_SDHCI_MASK 0xb7
Richard Zhu58ac8172011-03-21 13:22:16 +080048
Dong Aisheng03221912013-09-13 19:11:34 +080049/* tune control register */
50#define ESDHC_TUNE_CTRL_STATUS 0x68
51#define ESDHC_TUNE_CTRL_STEP 1
52#define ESDHC_TUNE_CTRL_MIN 0
53#define ESDHC_TUNE_CTRL_MAX ((1 << 7) - 1)
54
Dong Aisheng6e9fd282013-10-18 19:48:43 +080055#define ESDHC_TUNING_CTRL 0xcc
56#define ESDHC_STD_TUNING_EN (1 << 24)
57/* NOTE: the minimum valid tuning start tap for mx6sl is 1 */
58#define ESDHC_TUNING_START_TAP 0x1
59
Dong Aisheng03221912013-09-13 19:11:34 +080060#define ESDHC_TUNING_BLOCK_PATTERN_LEN 64
61
Dong Aishengad932202013-09-13 19:11:35 +080062/* pinctrl state */
63#define ESDHC_PINCTRL_STATE_100MHZ "state_100mhz"
64#define ESDHC_PINCTRL_STATE_200MHZ "state_200mhz"
65
Richard Zhu58ac8172011-03-21 13:22:16 +080066/*
Sascha Haueraf510792013-01-21 19:02:28 +080067 * Our interpretation of the SDHCI_HOST_CONTROL register
68 */
69#define ESDHC_CTRL_4BITBUS (0x1 << 1)
70#define ESDHC_CTRL_8BITBUS (0x2 << 1)
71#define ESDHC_CTRL_BUSWIDTH_MASK (0x3 << 1)
72
73/*
Richard Zhu97e4ba62011-08-11 16:51:46 -040074 * There is an INT DMA ERR mis-match between eSDHC and STD SDHC SPEC:
75 * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
76 * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
77 * Define this macro DMA error INT for fsl eSDHC
78 */
Shawn Guo60bf6392013-01-15 23:36:53 +080079#define ESDHC_INT_VENDOR_SPEC_DMA_ERR (1 << 28)
Richard Zhu97e4ba62011-08-11 16:51:46 -040080
81/*
Richard Zhu58ac8172011-03-21 13:22:16 +080082 * The CMDTYPE of the CMD register (offset 0xE) should be set to
83 * "11" when the STOP CMD12 is issued on imx53 to abort one
84 * open ended multi-blk IO. Otherwise the TC INT wouldn't
85 * be generated.
86 * In exact block transfer, the controller doesn't complete the
87 * operations automatically as required at the end of the
88 * transfer and remains on hold if the abort command is not sent.
89 * As a result, the TC flag is not asserted and SW received timeout
90 * exeception. Bit1 of Vendor Spec registor is used to fix it.
91 */
Shawn Guo31fbb302013-10-17 15:19:44 +080092#define ESDHC_FLAG_MULTIBLK_NO_INT BIT(1)
93/*
94 * The flag enables the workaround for ESDHC errata ENGcm07207 which
95 * affects i.MX25 and i.MX35.
96 */
97#define ESDHC_FLAG_ENGCM07207 BIT(2)
Shawn Guo9d61c002013-10-17 15:19:45 +080098/*
99 * The flag tells that the ESDHC controller is an USDHC block that is
100 * integrated on the i.MX6 series.
101 */
102#define ESDHC_FLAG_USDHC BIT(3)
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800103/* The IP supports manual tuning process */
104#define ESDHC_FLAG_MAN_TUNING BIT(4)
105/* The IP supports standard tuning process */
106#define ESDHC_FLAG_STD_TUNING BIT(5)
107/* The IP has SDHCI_CAPABILITIES_1 register */
108#define ESDHC_FLAG_HAVE_CAP1 BIT(6)
Richard Zhue1498602011-03-25 09:18:27 -0400109
Shawn Guof47c4bb2013-10-17 15:19:47 +0800110struct esdhc_soc_data {
111 u32 flags;
112};
113
114static struct esdhc_soc_data esdhc_imx25_data = {
115 .flags = ESDHC_FLAG_ENGCM07207,
116};
117
118static struct esdhc_soc_data esdhc_imx35_data = {
119 .flags = ESDHC_FLAG_ENGCM07207,
120};
121
122static struct esdhc_soc_data esdhc_imx51_data = {
123 .flags = 0,
124};
125
126static struct esdhc_soc_data esdhc_imx53_data = {
127 .flags = ESDHC_FLAG_MULTIBLK_NO_INT,
128};
129
130static struct esdhc_soc_data usdhc_imx6q_data = {
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800131 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING,
132};
133
134static struct esdhc_soc_data usdhc_imx6sl_data = {
135 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
136 | ESDHC_FLAG_HAVE_CAP1,
Shawn Guo57ed3312011-06-30 09:24:26 +0800137};
138
Richard Zhue1498602011-03-25 09:18:27 -0400139struct pltfm_imx_data {
Richard Zhue1498602011-03-25 09:18:27 -0400140 u32 scratchpad;
Dong Aishenge62d8b82012-05-11 14:56:01 +0800141 struct pinctrl *pinctrl;
Dong Aishengad932202013-09-13 19:11:35 +0800142 struct pinctrl_state *pins_default;
143 struct pinctrl_state *pins_100mhz;
144 struct pinctrl_state *pins_200mhz;
Shawn Guof47c4bb2013-10-17 15:19:47 +0800145 const struct esdhc_soc_data *socdata;
Shawn Guo842afc02011-07-06 22:57:48 +0800146 struct esdhc_platform_data boarddata;
Sascha Hauer52dac612012-03-07 09:31:34 +0100147 struct clk *clk_ipg;
148 struct clk *clk_ahb;
149 struct clk *clk_per;
Lucas Stach361b8482013-03-15 09:49:26 +0100150 enum {
151 NO_CMD_PENDING, /* no multiblock command pending*/
152 MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */
153 WAIT_FOR_INT, /* sent CMD12, waiting for response INT */
154 } multiblock_status;
Dong Aisheng03221912013-09-13 19:11:34 +0800155 u32 uhs_mode;
Dong Aishengde5bdbf2013-10-18 19:48:46 +0800156 u32 is_ddr;
Richard Zhue1498602011-03-25 09:18:27 -0400157};
158
Shawn Guo57ed3312011-06-30 09:24:26 +0800159static struct platform_device_id imx_esdhc_devtype[] = {
160 {
161 .name = "sdhci-esdhc-imx25",
Shawn Guof47c4bb2013-10-17 15:19:47 +0800162 .driver_data = (kernel_ulong_t) &esdhc_imx25_data,
Shawn Guo57ed3312011-06-30 09:24:26 +0800163 }, {
164 .name = "sdhci-esdhc-imx35",
Shawn Guof47c4bb2013-10-17 15:19:47 +0800165 .driver_data = (kernel_ulong_t) &esdhc_imx35_data,
Shawn Guo57ed3312011-06-30 09:24:26 +0800166 }, {
167 .name = "sdhci-esdhc-imx51",
Shawn Guof47c4bb2013-10-17 15:19:47 +0800168 .driver_data = (kernel_ulong_t) &esdhc_imx51_data,
Shawn Guo57ed3312011-06-30 09:24:26 +0800169 }, {
Shawn Guo57ed3312011-06-30 09:24:26 +0800170 /* sentinel */
171 }
172};
173MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
174
Shawn Guoabfafc22011-06-30 15:44:44 +0800175static const struct of_device_id imx_esdhc_dt_ids[] = {
Shawn Guof47c4bb2013-10-17 15:19:47 +0800176 { .compatible = "fsl,imx25-esdhc", .data = &esdhc_imx25_data, },
177 { .compatible = "fsl,imx35-esdhc", .data = &esdhc_imx35_data, },
178 { .compatible = "fsl,imx51-esdhc", .data = &esdhc_imx51_data, },
179 { .compatible = "fsl,imx53-esdhc", .data = &esdhc_imx53_data, },
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800180 { .compatible = "fsl,imx6sl-usdhc", .data = &usdhc_imx6sl_data, },
Shawn Guof47c4bb2013-10-17 15:19:47 +0800181 { .compatible = "fsl,imx6q-usdhc", .data = &usdhc_imx6q_data, },
Shawn Guoabfafc22011-06-30 15:44:44 +0800182 { /* sentinel */ }
183};
184MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
185
Shawn Guo57ed3312011-06-30 09:24:26 +0800186static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
187{
Shawn Guof47c4bb2013-10-17 15:19:47 +0800188 return data->socdata == &esdhc_imx25_data;
Shawn Guo57ed3312011-06-30 09:24:26 +0800189}
190
191static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
192{
Shawn Guof47c4bb2013-10-17 15:19:47 +0800193 return data->socdata == &esdhc_imx53_data;
Shawn Guo57ed3312011-06-30 09:24:26 +0800194}
195
Shawn Guo95a24822011-09-19 17:32:21 +0800196static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
197{
Shawn Guof47c4bb2013-10-17 15:19:47 +0800198 return data->socdata == &usdhc_imx6q_data;
Shawn Guo95a24822011-09-19 17:32:21 +0800199}
200
Shawn Guo9d61c002013-10-17 15:19:45 +0800201static inline int esdhc_is_usdhc(struct pltfm_imx_data *data)
202{
Shawn Guof47c4bb2013-10-17 15:19:47 +0800203 return !!(data->socdata->flags & ESDHC_FLAG_USDHC);
Shawn Guo9d61c002013-10-17 15:19:45 +0800204}
205
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200206static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
207{
208 void __iomem *base = host->ioaddr + (reg & ~0x3);
209 u32 shift = (reg & 0x3) * 8;
210
211 writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
212}
213
Wolfram Sang7e29c302011-02-26 14:44:41 +0100214static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
215{
Lucas Stach361b8482013-03-15 09:49:26 +0100216 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
217 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100218 u32 val = readl(host->ioaddr + reg);
219
Dong Aisheng03221912013-09-13 19:11:34 +0800220 if (unlikely(reg == SDHCI_PRESENT_STATE)) {
221 u32 fsl_prss = val;
222 /* save the least 20 bits */
223 val = fsl_prss & 0x000FFFFF;
224 /* move dat[0-3] bits */
225 val |= (fsl_prss & 0x0F000000) >> 4;
226 /* move cmd line bit */
227 val |= (fsl_prss & 0x00800000) << 1;
228 }
229
Richard Zhu97e4ba62011-08-11 16:51:46 -0400230 if (unlikely(reg == SDHCI_CAPABILITIES)) {
Dong Aisheng6b4fb6712a2013-10-18 19:48:44 +0800231 /* ignore bit[0-15] as it stores cap_1 register val for mx6sl */
232 if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
233 val &= 0xffff0000;
234
Richard Zhu97e4ba62011-08-11 16:51:46 -0400235 /* In FSL esdhc IC module, only bit20 is used to indicate the
236 * ADMA2 capability of esdhc, but this bit is messed up on
237 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
238 * don't actually support ADMA2). So set the BROKEN_ADMA
239 * uirk on MX25/35 platforms.
240 */
241
242 if (val & SDHCI_CAN_DO_ADMA1) {
243 val &= ~SDHCI_CAN_DO_ADMA1;
244 val |= SDHCI_CAN_DO_ADMA2;
245 }
246 }
247
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800248 if (unlikely(reg == SDHCI_CAPABILITIES_1)) {
249 if (esdhc_is_usdhc(imx_data)) {
250 if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
251 val = readl(host->ioaddr + SDHCI_CAPABILITIES) & 0xFFFF;
252 else
253 /* imx6q/dl does not have cap_1 register, fake one */
254 val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104
255 | SDHCI_SUPPORT_SDR50;
256 }
257 }
Dong Aisheng03221912013-09-13 19:11:34 +0800258
Shawn Guo9d61c002013-10-17 15:19:45 +0800259 if (unlikely(reg == SDHCI_MAX_CURRENT) && esdhc_is_usdhc(imx_data)) {
Dong Aisheng03221912013-09-13 19:11:34 +0800260 val = 0;
261 val |= 0xFF << SDHCI_MAX_CURRENT_330_SHIFT;
262 val |= 0xFF << SDHCI_MAX_CURRENT_300_SHIFT;
263 val |= 0xFF << SDHCI_MAX_CURRENT_180_SHIFT;
264 }
265
Richard Zhu97e4ba62011-08-11 16:51:46 -0400266 if (unlikely(reg == SDHCI_INT_STATUS)) {
Shawn Guo60bf6392013-01-15 23:36:53 +0800267 if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
268 val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
Richard Zhu97e4ba62011-08-11 16:51:46 -0400269 val |= SDHCI_INT_ADMA_ERROR;
270 }
Lucas Stach361b8482013-03-15 09:49:26 +0100271
272 /*
273 * mask off the interrupt we get in response to the manually
274 * sent CMD12
275 */
276 if ((imx_data->multiblock_status == WAIT_FOR_INT) &&
277 ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) {
278 val &= ~SDHCI_INT_RESPONSE;
279 writel(SDHCI_INT_RESPONSE, host->ioaddr +
280 SDHCI_INT_STATUS);
281 imx_data->multiblock_status = NO_CMD_PENDING;
282 }
Richard Zhu97e4ba62011-08-11 16:51:46 -0400283 }
284
Wolfram Sang7e29c302011-02-26 14:44:41 +0100285 return val;
286}
287
288static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
289{
Richard Zhue1498602011-03-25 09:18:27 -0400290 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
291 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Tony Lin0d588642011-08-11 16:45:59 -0400292 u32 data;
Richard Zhue1498602011-03-25 09:18:27 -0400293
Tony Lin0d588642011-08-11 16:45:59 -0400294 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
Tony Lin0d588642011-08-11 16:45:59 -0400295 if (val & SDHCI_INT_CARD_INT) {
296 /*
297 * Clear and then set D3CD bit to avoid missing the
298 * card interrupt. This is a eSDHC controller problem
299 * so we need to apply the following workaround: clear
300 * and set D3CD bit will make eSDHC re-sample the card
301 * interrupt. In case a card interrupt was lost,
302 * re-sample it by the following steps.
303 */
304 data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
Shawn Guo60bf6392013-01-15 23:36:53 +0800305 data &= ~ESDHC_CTRL_D3CD;
Tony Lin0d588642011-08-11 16:45:59 -0400306 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
Shawn Guo60bf6392013-01-15 23:36:53 +0800307 data |= ESDHC_CTRL_D3CD;
Tony Lin0d588642011-08-11 16:45:59 -0400308 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
309 }
310 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100311
Shawn Guof47c4bb2013-10-17 15:19:47 +0800312 if (unlikely((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
Richard Zhu58ac8172011-03-21 13:22:16 +0800313 && (reg == SDHCI_INT_STATUS)
314 && (val & SDHCI_INT_DATA_END))) {
315 u32 v;
Shawn Guo60bf6392013-01-15 23:36:53 +0800316 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
317 v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
318 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
Lucas Stach361b8482013-03-15 09:49:26 +0100319
320 if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS)
321 {
322 /* send a manual CMD12 with RESPTYP=none */
323 data = MMC_STOP_TRANSMISSION << 24 |
324 SDHCI_CMD_ABORTCMD << 16;
325 writel(data, host->ioaddr + SDHCI_TRANSFER_MODE);
326 imx_data->multiblock_status = WAIT_FOR_INT;
327 }
Richard Zhu58ac8172011-03-21 13:22:16 +0800328 }
329
Richard Zhu97e4ba62011-08-11 16:51:46 -0400330 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
331 if (val & SDHCI_INT_ADMA_ERROR) {
332 val &= ~SDHCI_INT_ADMA_ERROR;
Shawn Guo60bf6392013-01-15 23:36:53 +0800333 val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
Richard Zhu97e4ba62011-08-11 16:51:46 -0400334 }
335 }
336
Wolfram Sang7e29c302011-02-26 14:44:41 +0100337 writel(val, host->ioaddr + reg);
338}
339
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200340static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
341{
Shawn Guoef4d0882013-01-15 23:30:27 +0800342 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
343 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aisheng03221912013-09-13 19:11:34 +0800344 u16 ret = 0;
345 u32 val;
Shawn Guoef4d0882013-01-15 23:30:27 +0800346
Shawn Guo95a24822011-09-19 17:32:21 +0800347 if (unlikely(reg == SDHCI_HOST_VERSION)) {
Shawn Guoef4d0882013-01-15 23:30:27 +0800348 reg ^= 2;
Shawn Guo9d61c002013-10-17 15:19:45 +0800349 if (esdhc_is_usdhc(imx_data)) {
Shawn Guoef4d0882013-01-15 23:30:27 +0800350 /*
351 * The usdhc register returns a wrong host version.
352 * Correct it here.
353 */
354 return SDHCI_SPEC_300;
355 }
Shawn Guo95a24822011-09-19 17:32:21 +0800356 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200357
Dong Aisheng03221912013-09-13 19:11:34 +0800358 if (unlikely(reg == SDHCI_HOST_CONTROL2)) {
359 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
360 if (val & ESDHC_VENDOR_SPEC_VSELECT)
361 ret |= SDHCI_CTRL_VDD_180;
362
Shawn Guo9d61c002013-10-17 15:19:45 +0800363 if (esdhc_is_usdhc(imx_data)) {
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800364 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
365 val = readl(host->ioaddr + ESDHC_MIX_CTRL);
366 else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING)
367 /* the std tuning bits is in ACMD12_ERR for imx6sl */
368 val = readl(host->ioaddr + SDHCI_ACMD12_ERR);
Dong Aisheng03221912013-09-13 19:11:34 +0800369 }
370
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800371 if (val & ESDHC_MIX_CTRL_EXE_TUNE)
372 ret |= SDHCI_CTRL_EXEC_TUNING;
373 if (val & ESDHC_MIX_CTRL_SMPCLK_SEL)
374 ret |= SDHCI_CTRL_TUNED_CLK;
375
Dong Aisheng03221912013-09-13 19:11:34 +0800376 ret |= (imx_data->uhs_mode & SDHCI_CTRL_UHS_MASK);
377 ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
378
379 return ret;
380 }
381
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200382 return readw(host->ioaddr + reg);
383}
384
385static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
386{
387 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400388 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aisheng03221912013-09-13 19:11:34 +0800389 u32 new_val = 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200390
391 switch (reg) {
Dong Aisheng03221912013-09-13 19:11:34 +0800392 case SDHCI_CLOCK_CONTROL:
393 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
394 if (val & SDHCI_CLOCK_CARD_EN)
395 new_val |= ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
396 else
397 new_val &= ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
398 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
399 return;
400 case SDHCI_HOST_CONTROL2:
401 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
402 if (val & SDHCI_CTRL_VDD_180)
403 new_val |= ESDHC_VENDOR_SPEC_VSELECT;
404 else
405 new_val &= ~ESDHC_VENDOR_SPEC_VSELECT;
406 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
407 imx_data->uhs_mode = val & SDHCI_CTRL_UHS_MASK;
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800408 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
409 new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
410 if (val & SDHCI_CTRL_TUNED_CLK)
411 new_val |= ESDHC_MIX_CTRL_SMPCLK_SEL;
412 else
413 new_val &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
414 writel(new_val , host->ioaddr + ESDHC_MIX_CTRL);
415 } else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
416 u32 v = readl(host->ioaddr + SDHCI_ACMD12_ERR);
417 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
418 new_val = readl(host->ioaddr + ESDHC_TUNING_CTRL);
419 if (val & SDHCI_CTRL_EXEC_TUNING) {
420 new_val |= ESDHC_STD_TUNING_EN |
421 ESDHC_TUNING_START_TAP;
422 v |= ESDHC_MIX_CTRL_EXE_TUNE;
423 m |= ESDHC_MIX_CTRL_FBCLK_SEL;
424 } else {
425 new_val &= ~ESDHC_STD_TUNING_EN;
426 v &= ~ESDHC_MIX_CTRL_EXE_TUNE;
427 m &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
428 }
429
430 if (val & SDHCI_CTRL_TUNED_CLK)
431 v |= ESDHC_MIX_CTRL_SMPCLK_SEL;
432 else
433 v &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
434
435 writel(new_val, host->ioaddr + ESDHC_TUNING_CTRL);
436 writel(v, host->ioaddr + SDHCI_ACMD12_ERR);
437 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
438 }
Dong Aisheng03221912013-09-13 19:11:34 +0800439 return;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200440 case SDHCI_TRANSFER_MODE:
Shawn Guof47c4bb2013-10-17 15:19:47 +0800441 if ((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
Richard Zhu58ac8172011-03-21 13:22:16 +0800442 && (host->cmd->opcode == SD_IO_RW_EXTENDED)
443 && (host->cmd->data->blocks > 1)
444 && (host->cmd->data->flags & MMC_DATA_READ)) {
445 u32 v;
Shawn Guo60bf6392013-01-15 23:36:53 +0800446 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
447 v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
448 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
Richard Zhu58ac8172011-03-21 13:22:16 +0800449 }
Shawn Guo69f54692013-01-21 19:02:24 +0800450
Shawn Guo9d61c002013-10-17 15:19:45 +0800451 if (esdhc_is_usdhc(imx_data)) {
Shawn Guo69f54692013-01-21 19:02:24 +0800452 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
Shawn Guo2a15f982013-01-21 19:02:26 +0800453 /* Swap AC23 bit */
454 if (val & SDHCI_TRNS_AUTO_CMD23) {
455 val &= ~SDHCI_TRNS_AUTO_CMD23;
456 val |= ESDHC_MIX_CTRL_AC23EN;
457 }
458 m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
Shawn Guo69f54692013-01-21 19:02:24 +0800459 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
460 } else {
461 /*
462 * Postpone this write, we must do it together with a
463 * command write that is down below.
464 */
465 imx_data->scratchpad = val;
466 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200467 return;
468 case SDHCI_COMMAND:
Lucas Stach361b8482013-03-15 09:49:26 +0100469 if (host->cmd->opcode == MMC_STOP_TRANSMISSION)
Richard Zhu58ac8172011-03-21 13:22:16 +0800470 val |= SDHCI_CMD_ABORTCMD;
Shawn Guo95a24822011-09-19 17:32:21 +0800471
Lucas Stach361b8482013-03-15 09:49:26 +0100472 if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
Shawn Guof47c4bb2013-10-17 15:19:47 +0800473 (imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
Lucas Stach361b8482013-03-15 09:49:26 +0100474 imx_data->multiblock_status = MULTIBLK_IN_PROCESS;
475
Shawn Guo9d61c002013-10-17 15:19:45 +0800476 if (esdhc_is_usdhc(imx_data))
Shawn Guo95a24822011-09-19 17:32:21 +0800477 writel(val << 16,
478 host->ioaddr + SDHCI_TRANSFER_MODE);
Shawn Guo69f54692013-01-21 19:02:24 +0800479 else
Shawn Guo95a24822011-09-19 17:32:21 +0800480 writel(val << 16 | imx_data->scratchpad,
481 host->ioaddr + SDHCI_TRANSFER_MODE);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200482 return;
483 case SDHCI_BLOCK_SIZE:
484 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
485 break;
486 }
487 esdhc_clrset_le(host, 0xffff, val, reg);
488}
489
490static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
491{
Wilson Callan9a0985b2012-07-19 02:49:16 -0400492 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
493 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200494 u32 new_val;
Sascha Haueraf510792013-01-21 19:02:28 +0800495 u32 mask;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200496
497 switch (reg) {
498 case SDHCI_POWER_CONTROL:
499 /*
500 * FSL put some DMA bits here
501 * If your board has a regulator, code should be here
502 */
503 return;
504 case SDHCI_HOST_CONTROL:
Shawn Guo6b40d182013-01-15 23:36:52 +0800505 /* FSL messed up here, so we need to manually compose it. */
Sascha Haueraf510792013-01-21 19:02:28 +0800506 new_val = val & SDHCI_CTRL_LED;
Masanari Iida7122bbb2012-08-05 23:25:40 +0900507 /* ensure the endianness */
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200508 new_val |= ESDHC_HOST_CONTROL_LE;
Wilson Callan9a0985b2012-07-19 02:49:16 -0400509 /* bits 8&9 are reserved on mx25 */
510 if (!is_imx25_esdhc(imx_data)) {
511 /* DMA mode bits are shifted */
512 new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
513 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200514
Sascha Haueraf510792013-01-21 19:02:28 +0800515 /*
516 * Do not touch buswidth bits here. This is done in
517 * esdhc_pltfm_bus_width.
Martin Fuzzeyf6825742013-04-15 17:08:35 +0200518 * Do not touch the D3CD bit either which is used for the
519 * SDIO interrupt errata workaround.
Sascha Haueraf510792013-01-21 19:02:28 +0800520 */
Martin Fuzzeyf6825742013-04-15 17:08:35 +0200521 mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD);
Sascha Haueraf510792013-01-21 19:02:28 +0800522
523 esdhc_clrset_le(host, mask, new_val, reg);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200524 return;
525 }
526 esdhc_clrset_le(host, 0xff, val, reg);
Shawn Guo913413c2011-06-21 22:41:51 +0800527
528 /*
529 * The esdhc has a design violation to SDHC spec which tells
530 * that software reset should not affect card detection circuit.
531 * But esdhc clears its SYSCTL register bits [0..2] during the
532 * software reset. This will stop those clocks that card detection
533 * circuit relies on. To work around it, we turn the clocks on back
534 * to keep card detection circuit functional.
535 */
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800536 if ((reg == SDHCI_SOFTWARE_RESET) && (val & 1)) {
Shawn Guo913413c2011-06-21 22:41:51 +0800537 esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800538 /*
539 * The reset on usdhc fails to clear MIX_CTRL register.
540 * Do it manually here.
541 */
Dong Aishengde5bdbf2013-10-18 19:48:46 +0800542 if (esdhc_is_usdhc(imx_data)) {
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800543 writel(0, host->ioaddr + ESDHC_MIX_CTRL);
Dong Aishengde5bdbf2013-10-18 19:48:46 +0800544 imx_data->is_ddr = 0;
545 }
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800546 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200547}
548
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200549static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
550{
551 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
552 struct pltfm_imx_data *imx_data = pltfm_host->priv;
553 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
554
555 u32 f_host = clk_get_rate(pltfm_host->clk);
556
557 if (boarddata->f_max && (boarddata->f_max < f_host))
558 return boarddata->f_max;
559 else
560 return f_host;
561}
562
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200563static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
564{
565 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
566
567 return clk_get_rate(pltfm_host->clk) / 256 / 16;
568}
569
Lucas Stach8ba95802013-06-05 15:13:25 +0200570static inline void esdhc_pltfm_set_clock(struct sdhci_host *host,
571 unsigned int clock)
572{
573 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800574 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aishengd31fc002013-09-13 19:11:32 +0800575 unsigned int host_clock = clk_get_rate(pltfm_host->clk);
576 int pre_div = 2;
577 int div = 1;
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800578 u32 temp, val;
Lucas Stach8ba95802013-06-05 15:13:25 +0200579
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800580 if (clock == 0) {
Shawn Guo9d61c002013-10-17 15:19:45 +0800581 if (esdhc_is_usdhc(imx_data)) {
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800582 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
583 writel(val & ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
584 host->ioaddr + ESDHC_VENDOR_SPEC);
585 }
Dong Aishengd31fc002013-09-13 19:11:32 +0800586 goto out;
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800587 }
Dong Aishengd31fc002013-09-13 19:11:32 +0800588
Dong Aishengde5bdbf2013-10-18 19:48:46 +0800589 if (esdhc_is_usdhc(imx_data) && !imx_data->is_ddr)
Dong Aisheng5f7886c2013-09-13 19:11:36 +0800590 pre_div = 1;
591
Dong Aishengd31fc002013-09-13 19:11:32 +0800592 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
593 temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
594 | ESDHC_CLOCK_MASK);
595 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
596
597 while (host_clock / pre_div / 16 > clock && pre_div < 256)
598 pre_div *= 2;
599
600 while (host_clock / pre_div / div > clock && div < 16)
601 div++;
602
Dong Aishenge76b8552013-09-13 19:11:37 +0800603 host->mmc->actual_clock = host_clock / pre_div / div;
Dong Aishengd31fc002013-09-13 19:11:32 +0800604 dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
Dong Aishenge76b8552013-09-13 19:11:37 +0800605 clock, host->mmc->actual_clock);
Dong Aishengd31fc002013-09-13 19:11:32 +0800606
Dong Aishengde5bdbf2013-10-18 19:48:46 +0800607 if (imx_data->is_ddr)
608 pre_div >>= 2;
609 else
610 pre_div >>= 1;
Dong Aishengd31fc002013-09-13 19:11:32 +0800611 div--;
612
613 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
614 temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
615 | (div << ESDHC_DIVIDER_SHIFT)
616 | (pre_div << ESDHC_PREDIV_SHIFT));
617 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800618
Shawn Guo9d61c002013-10-17 15:19:45 +0800619 if (esdhc_is_usdhc(imx_data)) {
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800620 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
621 writel(val | ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
622 host->ioaddr + ESDHC_VENDOR_SPEC);
623 }
624
Dong Aishengd31fc002013-09-13 19:11:32 +0800625 mdelay(1);
626out:
627 host->clock = clock;
Lucas Stach8ba95802013-06-05 15:13:25 +0200628}
629
Shawn Guo913413c2011-06-21 22:41:51 +0800630static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
631{
Shawn Guo842afc02011-07-06 22:57:48 +0800632 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
633 struct pltfm_imx_data *imx_data = pltfm_host->priv;
634 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Shawn Guo913413c2011-06-21 22:41:51 +0800635
636 switch (boarddata->wp_type) {
637 case ESDHC_WP_GPIO:
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800638 return mmc_gpio_get_ro(host->mmc);
Shawn Guo913413c2011-06-21 22:41:51 +0800639 case ESDHC_WP_CONTROLLER:
640 return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
641 SDHCI_WRITE_PROTECT);
642 case ESDHC_WP_NONE:
643 break;
644 }
645
646 return -ENOSYS;
647}
648
Sascha Haueraf510792013-01-21 19:02:28 +0800649static int esdhc_pltfm_bus_width(struct sdhci_host *host, int width)
650{
651 u32 ctrl;
652
653 switch (width) {
654 case MMC_BUS_WIDTH_8:
655 ctrl = ESDHC_CTRL_8BITBUS;
656 break;
657 case MMC_BUS_WIDTH_4:
658 ctrl = ESDHC_CTRL_4BITBUS;
659 break;
660 default:
661 ctrl = 0;
662 break;
663 }
664
665 esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
666 SDHCI_HOST_CONTROL);
667
668 return 0;
669}
670
Dong Aisheng03221912013-09-13 19:11:34 +0800671static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val)
672{
673 u32 reg;
674
675 /* FIXME: delay a bit for card to be ready for next tuning due to errors */
676 mdelay(1);
677
678 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
679 reg |= ESDHC_MIX_CTRL_EXE_TUNE | ESDHC_MIX_CTRL_SMPCLK_SEL |
680 ESDHC_MIX_CTRL_FBCLK_SEL;
681 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
682 writel(val << 8, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
683 dev_dbg(mmc_dev(host->mmc),
684 "tunning with delay 0x%x ESDHC_TUNE_CTRL_STATUS 0x%x\n",
685 val, readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS));
686}
687
688static void esdhc_request_done(struct mmc_request *mrq)
689{
690 complete(&mrq->completion);
691}
692
693static int esdhc_send_tuning_cmd(struct sdhci_host *host, u32 opcode)
694{
695 struct mmc_command cmd = {0};
696 struct mmc_request mrq = {0};
697 struct mmc_data data = {0};
698 struct scatterlist sg;
699 char tuning_pattern[ESDHC_TUNING_BLOCK_PATTERN_LEN];
700
701 cmd.opcode = opcode;
702 cmd.arg = 0;
703 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
704
705 data.blksz = ESDHC_TUNING_BLOCK_PATTERN_LEN;
706 data.blocks = 1;
707 data.flags = MMC_DATA_READ;
708 data.sg = &sg;
709 data.sg_len = 1;
710
711 sg_init_one(&sg, tuning_pattern, sizeof(tuning_pattern));
712
713 mrq.cmd = &cmd;
714 mrq.cmd->mrq = &mrq;
715 mrq.data = &data;
716 mrq.data->mrq = &mrq;
717 mrq.cmd->data = mrq.data;
718
719 mrq.done = esdhc_request_done;
720 init_completion(&(mrq.completion));
721
722 disable_irq(host->irq);
723 spin_lock(&host->lock);
724 host->mrq = &mrq;
725
726 sdhci_send_command(host, mrq.cmd);
727
728 spin_unlock(&host->lock);
729 enable_irq(host->irq);
730
731 wait_for_completion(&mrq.completion);
732
733 if (cmd.error)
734 return cmd.error;
735 if (data.error)
736 return data.error;
737
738 return 0;
739}
740
741static void esdhc_post_tuning(struct sdhci_host *host)
742{
743 u32 reg;
744
745 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
746 reg &= ~ESDHC_MIX_CTRL_EXE_TUNE;
747 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
748}
749
750static int esdhc_executing_tuning(struct sdhci_host *host, u32 opcode)
751{
752 int min, max, avg, ret;
753
754 /* find the mininum delay first which can pass tuning */
755 min = ESDHC_TUNE_CTRL_MIN;
756 while (min < ESDHC_TUNE_CTRL_MAX) {
757 esdhc_prepare_tuning(host, min);
758 if (!esdhc_send_tuning_cmd(host, opcode))
759 break;
760 min += ESDHC_TUNE_CTRL_STEP;
761 }
762
763 /* find the maxinum delay which can not pass tuning */
764 max = min + ESDHC_TUNE_CTRL_STEP;
765 while (max < ESDHC_TUNE_CTRL_MAX) {
766 esdhc_prepare_tuning(host, max);
767 if (esdhc_send_tuning_cmd(host, opcode)) {
768 max -= ESDHC_TUNE_CTRL_STEP;
769 break;
770 }
771 max += ESDHC_TUNE_CTRL_STEP;
772 }
773
774 /* use average delay to get the best timing */
775 avg = (min + max) / 2;
776 esdhc_prepare_tuning(host, avg);
777 ret = esdhc_send_tuning_cmd(host, opcode);
778 esdhc_post_tuning(host);
779
780 dev_dbg(mmc_dev(host->mmc), "tunning %s at 0x%x ret %d\n",
781 ret ? "failed" : "passed", avg, ret);
782
783 return ret;
784}
785
Dong Aishengad932202013-09-13 19:11:35 +0800786static int esdhc_change_pinstate(struct sdhci_host *host,
787 unsigned int uhs)
788{
789 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
790 struct pltfm_imx_data *imx_data = pltfm_host->priv;
791 struct pinctrl_state *pinctrl;
792
793 dev_dbg(mmc_dev(host->mmc), "change pinctrl state for uhs %d\n", uhs);
794
795 if (IS_ERR(imx_data->pinctrl) ||
796 IS_ERR(imx_data->pins_default) ||
797 IS_ERR(imx_data->pins_100mhz) ||
798 IS_ERR(imx_data->pins_200mhz))
799 return -EINVAL;
800
801 switch (uhs) {
802 case MMC_TIMING_UHS_SDR50:
803 pinctrl = imx_data->pins_100mhz;
804 break;
805 case MMC_TIMING_UHS_SDR104:
806 pinctrl = imx_data->pins_200mhz;
807 break;
808 default:
809 /* back to default state for other legacy timing */
810 pinctrl = imx_data->pins_default;
811 }
812
813 return pinctrl_select_state(imx_data->pinctrl, pinctrl);
814}
815
816static int esdhc_set_uhs_signaling(struct sdhci_host *host, unsigned int uhs)
817{
818 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
819 struct pltfm_imx_data *imx_data = pltfm_host->priv;
820
821 switch (uhs) {
822 case MMC_TIMING_UHS_SDR12:
823 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR12;
824 break;
825 case MMC_TIMING_UHS_SDR25:
826 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR25;
827 break;
828 case MMC_TIMING_UHS_SDR50:
829 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR50;
830 break;
831 case MMC_TIMING_UHS_SDR104:
832 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR104;
833 break;
834 case MMC_TIMING_UHS_DDR50:
835 imx_data->uhs_mode = SDHCI_CTRL_UHS_DDR50;
Dong Aishengde5bdbf2013-10-18 19:48:46 +0800836 writel(readl(host->ioaddr + ESDHC_MIX_CTRL) |
837 ESDHC_MIX_CTRL_DDREN,
838 host->ioaddr + ESDHC_MIX_CTRL);
839 imx_data->is_ddr = 1;
Dong Aishengad932202013-09-13 19:11:35 +0800840 break;
841 }
842
843 return esdhc_change_pinstate(host, uhs);
844}
845
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800846static struct sdhci_ops sdhci_esdhc_ops = {
Richard Zhue1498602011-03-25 09:18:27 -0400847 .read_l = esdhc_readl_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100848 .read_w = esdhc_readw_le,
Richard Zhue1498602011-03-25 09:18:27 -0400849 .write_l = esdhc_writel_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100850 .write_w = esdhc_writew_le,
851 .write_b = esdhc_writeb_le,
Lucas Stach8ba95802013-06-05 15:13:25 +0200852 .set_clock = esdhc_pltfm_set_clock,
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200853 .get_max_clock = esdhc_pltfm_get_max_clock,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100854 .get_min_clock = esdhc_pltfm_get_min_clock,
Shawn Guo913413c2011-06-21 22:41:51 +0800855 .get_ro = esdhc_pltfm_get_ro,
Sascha Haueraf510792013-01-21 19:02:28 +0800856 .platform_bus_width = esdhc_pltfm_bus_width,
Dong Aishengad932202013-09-13 19:11:35 +0800857 .set_uhs_signaling = esdhc_set_uhs_signaling,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100858};
859
Lars-Peter Clausen1db5eeb2013-03-13 19:26:03 +0100860static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
Richard Zhu97e4ba62011-08-11 16:51:46 -0400861 .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
862 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
863 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
Shawn Guo85d65092011-05-27 23:48:12 +0800864 | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
Shawn Guo85d65092011-05-27 23:48:12 +0800865 .ops = &sdhci_esdhc_ops,
866};
867
Shawn Guoabfafc22011-06-30 15:44:44 +0800868#ifdef CONFIG_OF
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500869static int
Shawn Guoabfafc22011-06-30 15:44:44 +0800870sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
871 struct esdhc_platform_data *boarddata)
872{
873 struct device_node *np = pdev->dev.of_node;
874
875 if (!np)
876 return -ENODEV;
877
Arnd Bergmann7f217792012-05-13 00:14:24 -0400878 if (of_get_property(np, "non-removable", NULL))
Shawn Guoabfafc22011-06-30 15:44:44 +0800879 boarddata->cd_type = ESDHC_CD_PERMANENT;
880
881 if (of_get_property(np, "fsl,cd-controller", NULL))
882 boarddata->cd_type = ESDHC_CD_CONTROLLER;
883
884 if (of_get_property(np, "fsl,wp-controller", NULL))
885 boarddata->wp_type = ESDHC_WP_CONTROLLER;
886
887 boarddata->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
888 if (gpio_is_valid(boarddata->cd_gpio))
889 boarddata->cd_type = ESDHC_CD_GPIO;
890
891 boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
892 if (gpio_is_valid(boarddata->wp_gpio))
893 boarddata->wp_type = ESDHC_WP_GPIO;
894
Sascha Haueraf510792013-01-21 19:02:28 +0800895 of_property_read_u32(np, "bus-width", &boarddata->max_bus_width);
896
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200897 of_property_read_u32(np, "max-frequency", &boarddata->f_max);
898
Dong Aishengad932202013-09-13 19:11:35 +0800899 if (of_find_property(np, "no-1-8-v", NULL))
900 boarddata->support_vsel = false;
901 else
902 boarddata->support_vsel = true;
903
Shawn Guoabfafc22011-06-30 15:44:44 +0800904 return 0;
905}
906#else
907static inline int
908sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
909 struct esdhc_platform_data *boarddata)
910{
911 return -ENODEV;
912}
913#endif
914
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500915static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200916{
Shawn Guoabfafc22011-06-30 15:44:44 +0800917 const struct of_device_id *of_id =
918 of_match_device(imx_esdhc_dt_ids, &pdev->dev);
Shawn Guo85d65092011-05-27 23:48:12 +0800919 struct sdhci_pltfm_host *pltfm_host;
920 struct sdhci_host *host;
921 struct esdhc_platform_data *boarddata;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100922 int err;
Richard Zhue1498602011-03-25 09:18:27 -0400923 struct pltfm_imx_data *imx_data;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200924
Christian Daudt0e748232013-05-29 13:50:05 -0700925 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata, 0);
Shawn Guo85d65092011-05-27 23:48:12 +0800926 if (IS_ERR(host))
927 return PTR_ERR(host);
928
929 pltfm_host = sdhci_priv(host);
930
Shawn Guoe3af31c2012-11-26 14:39:43 +0800931 imx_data = devm_kzalloc(&pdev->dev, sizeof(*imx_data), GFP_KERNEL);
Shawn Guoabfafc22011-06-30 15:44:44 +0800932 if (!imx_data) {
933 err = -ENOMEM;
Shawn Guoe3af31c2012-11-26 14:39:43 +0800934 goto free_sdhci;
Shawn Guoabfafc22011-06-30 15:44:44 +0800935 }
Shawn Guo57ed3312011-06-30 09:24:26 +0800936
Shawn Guof47c4bb2013-10-17 15:19:47 +0800937 imx_data->socdata = of_id ? of_id->data : (struct esdhc_soc_data *)
938 pdev->id_entry->driver_data;
Shawn Guo85d65092011-05-27 23:48:12 +0800939 pltfm_host->priv = imx_data;
940
Sascha Hauer52dac612012-03-07 09:31:34 +0100941 imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
942 if (IS_ERR(imx_data->clk_ipg)) {
943 err = PTR_ERR(imx_data->clk_ipg);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800944 goto free_sdhci;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200945 }
Sascha Hauer52dac612012-03-07 09:31:34 +0100946
947 imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
948 if (IS_ERR(imx_data->clk_ahb)) {
949 err = PTR_ERR(imx_data->clk_ahb);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800950 goto free_sdhci;
Sascha Hauer52dac612012-03-07 09:31:34 +0100951 }
952
953 imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
954 if (IS_ERR(imx_data->clk_per)) {
955 err = PTR_ERR(imx_data->clk_per);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800956 goto free_sdhci;
Sascha Hauer52dac612012-03-07 09:31:34 +0100957 }
958
959 pltfm_host->clk = imx_data->clk_per;
960
961 clk_prepare_enable(imx_data->clk_per);
962 clk_prepare_enable(imx_data->clk_ipg);
963 clk_prepare_enable(imx_data->clk_ahb);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200964
Dong Aishengad932202013-09-13 19:11:35 +0800965 imx_data->pinctrl = devm_pinctrl_get(&pdev->dev);
Dong Aishenge62d8b82012-05-11 14:56:01 +0800966 if (IS_ERR(imx_data->pinctrl)) {
967 err = PTR_ERR(imx_data->pinctrl);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800968 goto disable_clk;
Dong Aishenge62d8b82012-05-11 14:56:01 +0800969 }
970
Dong Aishengad932202013-09-13 19:11:35 +0800971 imx_data->pins_default = pinctrl_lookup_state(imx_data->pinctrl,
972 PINCTRL_STATE_DEFAULT);
973 if (IS_ERR(imx_data->pins_default)) {
974 err = PTR_ERR(imx_data->pins_default);
975 dev_err(mmc_dev(host->mmc), "could not get default state\n");
976 goto disable_clk;
977 }
978
Eric Bénardb89152822012-04-18 02:30:20 +0200979 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
Eric Bénard37865fe2010-10-23 01:57:21 +0200980
Shawn Guof47c4bb2013-10-17 15:19:47 +0800981 if (imx_data->socdata->flags & ESDHC_FLAG_ENGCM07207)
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100982 /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
Richard Zhu97e4ba62011-08-11 16:51:46 -0400983 host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK
984 | SDHCI_QUIRK_BROKEN_ADMA;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100985
Shawn Guof750ba92011-11-10 16:39:32 +0800986 /*
987 * The imx6q ROM code will change the default watermark level setting
988 * to something insane. Change it back here.
989 */
Shawn Guo9d61c002013-10-17 15:19:45 +0800990 if (esdhc_is_usdhc(imx_data))
Shawn Guo60bf6392013-01-15 23:36:53 +0800991 writel(0x08100810, host->ioaddr + ESDHC_WTMK_LVL);
Shawn Guof750ba92011-11-10 16:39:32 +0800992
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800993 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
994 sdhci_esdhc_ops.platform_execute_tuning =
995 esdhc_executing_tuning;
Shawn Guo842afc02011-07-06 22:57:48 +0800996 boarddata = &imx_data->boarddata;
Shawn Guoabfafc22011-06-30 15:44:44 +0800997 if (sdhci_esdhc_imx_probe_dt(pdev, boarddata) < 0) {
998 if (!host->mmc->parent->platform_data) {
999 dev_err(mmc_dev(host->mmc), "no board data!\n");
1000 err = -EINVAL;
Shawn Guoe3af31c2012-11-26 14:39:43 +08001001 goto disable_clk;
Shawn Guoabfafc22011-06-30 15:44:44 +08001002 }
1003 imx_data->boarddata = *((struct esdhc_platform_data *)
1004 host->mmc->parent->platform_data);
1005 }
Shawn Guo913413c2011-06-21 22:41:51 +08001006
1007 /* write_protect */
1008 if (boarddata->wp_type == ESDHC_WP_GPIO) {
Shawn Guofbe5fdd2012-12-11 22:32:20 +08001009 err = mmc_gpio_request_ro(host->mmc, boarddata->wp_gpio);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +01001010 if (err) {
Shawn Guofbe5fdd2012-12-11 22:32:20 +08001011 dev_err(mmc_dev(host->mmc),
1012 "failed to request write-protect gpio!\n");
1013 goto disable_clk;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +01001014 }
Shawn Guofbe5fdd2012-12-11 22:32:20 +08001015 host->mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
Shawn Guo913413c2011-06-21 22:41:51 +08001016 }
Wolfram Sang7e29c302011-02-26 14:44:41 +01001017
Shawn Guo913413c2011-06-21 22:41:51 +08001018 /* card_detect */
Shawn Guo913413c2011-06-21 22:41:51 +08001019 switch (boarddata->cd_type) {
1020 case ESDHC_CD_GPIO:
Laurent Pinchart214fc302013-08-08 12:38:31 +02001021 err = mmc_gpio_request_cd(host->mmc, boarddata->cd_gpio, 0);
Wolfram Sang7e29c302011-02-26 14:44:41 +01001022 if (err) {
Shawn Guo913413c2011-06-21 22:41:51 +08001023 dev_err(mmc_dev(host->mmc),
Shawn Guofbe5fdd2012-12-11 22:32:20 +08001024 "failed to request card-detect gpio!\n");
Shawn Guoe3af31c2012-11-26 14:39:43 +08001025 goto disable_clk;
Wolfram Sang7e29c302011-02-26 14:44:41 +01001026 }
Shawn Guo913413c2011-06-21 22:41:51 +08001027 /* fall through */
Wolfram Sang7e29c302011-02-26 14:44:41 +01001028
Shawn Guo913413c2011-06-21 22:41:51 +08001029 case ESDHC_CD_CONTROLLER:
1030 /* we have a working card_detect back */
Wolfram Sang7e29c302011-02-26 14:44:41 +01001031 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
Shawn Guo913413c2011-06-21 22:41:51 +08001032 break;
1033
1034 case ESDHC_CD_PERMANENT:
1035 host->mmc->caps = MMC_CAP_NONREMOVABLE;
1036 break;
1037
1038 case ESDHC_CD_NONE:
1039 break;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +01001040 }
Eric Bénard16a790b2010-10-23 01:57:22 +02001041
Sascha Haueraf510792013-01-21 19:02:28 +08001042 switch (boarddata->max_bus_width) {
1043 case 8:
1044 host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA;
1045 break;
1046 case 4:
1047 host->mmc->caps |= MMC_CAP_4_BIT_DATA;
1048 break;
1049 case 1:
1050 default:
1051 host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
1052 break;
1053 }
1054
Dong Aishengad932202013-09-13 19:11:35 +08001055 /* sdr50 and sdr104 needs work on 1.8v signal voltage */
Shawn Guo9d61c002013-10-17 15:19:45 +08001056 if ((boarddata->support_vsel) && esdhc_is_usdhc(imx_data)) {
Dong Aishengad932202013-09-13 19:11:35 +08001057 imx_data->pins_100mhz = pinctrl_lookup_state(imx_data->pinctrl,
1058 ESDHC_PINCTRL_STATE_100MHZ);
1059 imx_data->pins_200mhz = pinctrl_lookup_state(imx_data->pinctrl,
1060 ESDHC_PINCTRL_STATE_200MHZ);
1061 if (IS_ERR(imx_data->pins_100mhz) ||
1062 IS_ERR(imx_data->pins_200mhz)) {
1063 dev_warn(mmc_dev(host->mmc),
1064 "could not get ultra high speed state, work on normal mode\n");
1065 /* fall back to not support uhs by specify no 1.8v quirk */
1066 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1067 }
1068 } else {
1069 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1070 }
1071
Shawn Guo85d65092011-05-27 23:48:12 +08001072 err = sdhci_add_host(host);
1073 if (err)
Shawn Guoe3af31c2012-11-26 14:39:43 +08001074 goto disable_clk;
Shawn Guo85d65092011-05-27 23:48:12 +08001075
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001076 return 0;
Wolfram Sang7e29c302011-02-26 14:44:41 +01001077
Shawn Guoe3af31c2012-11-26 14:39:43 +08001078disable_clk:
Sascha Hauer52dac612012-03-07 09:31:34 +01001079 clk_disable_unprepare(imx_data->clk_per);
1080 clk_disable_unprepare(imx_data->clk_ipg);
1081 clk_disable_unprepare(imx_data->clk_ahb);
Shawn Guoe3af31c2012-11-26 14:39:43 +08001082free_sdhci:
Shawn Guo85d65092011-05-27 23:48:12 +08001083 sdhci_pltfm_free(pdev);
1084 return err;
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001085}
1086
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001087static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001088{
Shawn Guo85d65092011-05-27 23:48:12 +08001089 struct sdhci_host *host = platform_get_drvdata(pdev);
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001090 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -04001091 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Shawn Guo85d65092011-05-27 23:48:12 +08001092 int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
1093
1094 sdhci_remove_host(host, dead);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +01001095
Sascha Hauer52dac612012-03-07 09:31:34 +01001096 clk_disable_unprepare(imx_data->clk_per);
1097 clk_disable_unprepare(imx_data->clk_ipg);
1098 clk_disable_unprepare(imx_data->clk_ahb);
1099
Shawn Guo85d65092011-05-27 23:48:12 +08001100 sdhci_pltfm_free(pdev);
1101
1102 return 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001103}
1104
Shawn Guo85d65092011-05-27 23:48:12 +08001105static struct platform_driver sdhci_esdhc_imx_driver = {
1106 .driver = {
1107 .name = "sdhci-esdhc-imx",
1108 .owner = THIS_MODULE,
Shawn Guoabfafc22011-06-30 15:44:44 +08001109 .of_match_table = imx_esdhc_dt_ids,
Manuel Lauss29495aa2011-11-03 11:09:45 +01001110 .pm = SDHCI_PLTFM_PMOPS,
Shawn Guo85d65092011-05-27 23:48:12 +08001111 },
Shawn Guo57ed3312011-06-30 09:24:26 +08001112 .id_table = imx_esdhc_devtype,
Shawn Guo85d65092011-05-27 23:48:12 +08001113 .probe = sdhci_esdhc_imx_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05001114 .remove = sdhci_esdhc_imx_remove,
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001115};
Shawn Guo85d65092011-05-27 23:48:12 +08001116
Axel Lind1f81a62011-11-26 12:55:43 +08001117module_platform_driver(sdhci_esdhc_imx_driver);
Shawn Guo85d65092011-05-27 23:48:12 +08001118
1119MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
1120MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
1121MODULE_LICENSE("GPL v2");