blob: baa27c75b7710b99136dcdad1756db16ef90b849 [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 Aisheng602519b2013-10-18 19:48:47 +080049/* dll control register */
50#define ESDHC_DLL_CTRL 0x60
51#define ESDHC_DLL_OVERRIDE_VAL_SHIFT 9
52#define ESDHC_DLL_OVERRIDE_EN_SHIFT 8
53
Dong Aisheng03221912013-09-13 19:11:34 +080054/* tune control register */
55#define ESDHC_TUNE_CTRL_STATUS 0x68
56#define ESDHC_TUNE_CTRL_STEP 1
57#define ESDHC_TUNE_CTRL_MIN 0
58#define ESDHC_TUNE_CTRL_MAX ((1 << 7) - 1)
59
Dong Aisheng6e9fd282013-10-18 19:48:43 +080060#define ESDHC_TUNING_CTRL 0xcc
61#define ESDHC_STD_TUNING_EN (1 << 24)
62/* NOTE: the minimum valid tuning start tap for mx6sl is 1 */
63#define ESDHC_TUNING_START_TAP 0x1
64
Dong Aisheng03221912013-09-13 19:11:34 +080065#define ESDHC_TUNING_BLOCK_PATTERN_LEN 64
66
Dong Aishengad932202013-09-13 19:11:35 +080067/* pinctrl state */
68#define ESDHC_PINCTRL_STATE_100MHZ "state_100mhz"
69#define ESDHC_PINCTRL_STATE_200MHZ "state_200mhz"
70
Richard Zhu58ac8172011-03-21 13:22:16 +080071/*
Sascha Haueraf510792013-01-21 19:02:28 +080072 * Our interpretation of the SDHCI_HOST_CONTROL register
73 */
74#define ESDHC_CTRL_4BITBUS (0x1 << 1)
75#define ESDHC_CTRL_8BITBUS (0x2 << 1)
76#define ESDHC_CTRL_BUSWIDTH_MASK (0x3 << 1)
77
78/*
Richard Zhu97e4ba62011-08-11 16:51:46 -040079 * There is an INT DMA ERR mis-match between eSDHC and STD SDHC SPEC:
80 * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
81 * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
82 * Define this macro DMA error INT for fsl eSDHC
83 */
Shawn Guo60bf6392013-01-15 23:36:53 +080084#define ESDHC_INT_VENDOR_SPEC_DMA_ERR (1 << 28)
Richard Zhu97e4ba62011-08-11 16:51:46 -040085
86/*
Richard Zhu58ac8172011-03-21 13:22:16 +080087 * The CMDTYPE of the CMD register (offset 0xE) should be set to
88 * "11" when the STOP CMD12 is issued on imx53 to abort one
89 * open ended multi-blk IO. Otherwise the TC INT wouldn't
90 * be generated.
91 * In exact block transfer, the controller doesn't complete the
92 * operations automatically as required at the end of the
93 * transfer and remains on hold if the abort command is not sent.
94 * As a result, the TC flag is not asserted and SW received timeout
95 * exeception. Bit1 of Vendor Spec registor is used to fix it.
96 */
Shawn Guo31fbb302013-10-17 15:19:44 +080097#define ESDHC_FLAG_MULTIBLK_NO_INT BIT(1)
98/*
99 * The flag enables the workaround for ESDHC errata ENGcm07207 which
100 * affects i.MX25 and i.MX35.
101 */
102#define ESDHC_FLAG_ENGCM07207 BIT(2)
Shawn Guo9d61c002013-10-17 15:19:45 +0800103/*
104 * The flag tells that the ESDHC controller is an USDHC block that is
105 * integrated on the i.MX6 series.
106 */
107#define ESDHC_FLAG_USDHC BIT(3)
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800108/* The IP supports manual tuning process */
109#define ESDHC_FLAG_MAN_TUNING BIT(4)
110/* The IP supports standard tuning process */
111#define ESDHC_FLAG_STD_TUNING BIT(5)
112/* The IP has SDHCI_CAPABILITIES_1 register */
113#define ESDHC_FLAG_HAVE_CAP1 BIT(6)
Richard Zhue1498602011-03-25 09:18:27 -0400114
Shawn Guof47c4bb2013-10-17 15:19:47 +0800115struct esdhc_soc_data {
116 u32 flags;
117};
118
119static struct esdhc_soc_data esdhc_imx25_data = {
120 .flags = ESDHC_FLAG_ENGCM07207,
121};
122
123static struct esdhc_soc_data esdhc_imx35_data = {
124 .flags = ESDHC_FLAG_ENGCM07207,
125};
126
127static struct esdhc_soc_data esdhc_imx51_data = {
128 .flags = 0,
129};
130
131static struct esdhc_soc_data esdhc_imx53_data = {
132 .flags = ESDHC_FLAG_MULTIBLK_NO_INT,
133};
134
135static struct esdhc_soc_data usdhc_imx6q_data = {
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800136 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING,
137};
138
139static struct esdhc_soc_data usdhc_imx6sl_data = {
140 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
141 | ESDHC_FLAG_HAVE_CAP1,
Shawn Guo57ed3312011-06-30 09:24:26 +0800142};
143
Richard Zhue1498602011-03-25 09:18:27 -0400144struct pltfm_imx_data {
Richard Zhue1498602011-03-25 09:18:27 -0400145 u32 scratchpad;
Dong Aishenge62d8b82012-05-11 14:56:01 +0800146 struct pinctrl *pinctrl;
Dong Aishengad932202013-09-13 19:11:35 +0800147 struct pinctrl_state *pins_default;
148 struct pinctrl_state *pins_100mhz;
149 struct pinctrl_state *pins_200mhz;
Shawn Guof47c4bb2013-10-17 15:19:47 +0800150 const struct esdhc_soc_data *socdata;
Shawn Guo842afc02011-07-06 22:57:48 +0800151 struct esdhc_platform_data boarddata;
Sascha Hauer52dac612012-03-07 09:31:34 +0100152 struct clk *clk_ipg;
153 struct clk *clk_ahb;
154 struct clk *clk_per;
Lucas Stach361b8482013-03-15 09:49:26 +0100155 enum {
156 NO_CMD_PENDING, /* no multiblock command pending*/
157 MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */
158 WAIT_FOR_INT, /* sent CMD12, waiting for response INT */
159 } multiblock_status;
Dong Aisheng03221912013-09-13 19:11:34 +0800160 u32 uhs_mode;
Dong Aishengde5bdbf2013-10-18 19:48:46 +0800161 u32 is_ddr;
Richard Zhue1498602011-03-25 09:18:27 -0400162};
163
Shawn Guo57ed3312011-06-30 09:24:26 +0800164static struct platform_device_id imx_esdhc_devtype[] = {
165 {
166 .name = "sdhci-esdhc-imx25",
Shawn Guof47c4bb2013-10-17 15:19:47 +0800167 .driver_data = (kernel_ulong_t) &esdhc_imx25_data,
Shawn Guo57ed3312011-06-30 09:24:26 +0800168 }, {
169 .name = "sdhci-esdhc-imx35",
Shawn Guof47c4bb2013-10-17 15:19:47 +0800170 .driver_data = (kernel_ulong_t) &esdhc_imx35_data,
Shawn Guo57ed3312011-06-30 09:24:26 +0800171 }, {
172 .name = "sdhci-esdhc-imx51",
Shawn Guof47c4bb2013-10-17 15:19:47 +0800173 .driver_data = (kernel_ulong_t) &esdhc_imx51_data,
Shawn Guo57ed3312011-06-30 09:24:26 +0800174 }, {
Shawn Guo57ed3312011-06-30 09:24:26 +0800175 /* sentinel */
176 }
177};
178MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
179
Shawn Guoabfafc22011-06-30 15:44:44 +0800180static const struct of_device_id imx_esdhc_dt_ids[] = {
Shawn Guof47c4bb2013-10-17 15:19:47 +0800181 { .compatible = "fsl,imx25-esdhc", .data = &esdhc_imx25_data, },
182 { .compatible = "fsl,imx35-esdhc", .data = &esdhc_imx35_data, },
183 { .compatible = "fsl,imx51-esdhc", .data = &esdhc_imx51_data, },
184 { .compatible = "fsl,imx53-esdhc", .data = &esdhc_imx53_data, },
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800185 { .compatible = "fsl,imx6sl-usdhc", .data = &usdhc_imx6sl_data, },
Shawn Guof47c4bb2013-10-17 15:19:47 +0800186 { .compatible = "fsl,imx6q-usdhc", .data = &usdhc_imx6q_data, },
Shawn Guoabfafc22011-06-30 15:44:44 +0800187 { /* sentinel */ }
188};
189MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
190
Shawn Guo57ed3312011-06-30 09:24:26 +0800191static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
192{
Shawn Guof47c4bb2013-10-17 15:19:47 +0800193 return data->socdata == &esdhc_imx25_data;
Shawn Guo57ed3312011-06-30 09:24:26 +0800194}
195
196static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
197{
Shawn Guof47c4bb2013-10-17 15:19:47 +0800198 return data->socdata == &esdhc_imx53_data;
Shawn Guo57ed3312011-06-30 09:24:26 +0800199}
200
Shawn Guo95a24822011-09-19 17:32:21 +0800201static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
202{
Shawn Guof47c4bb2013-10-17 15:19:47 +0800203 return data->socdata == &usdhc_imx6q_data;
Shawn Guo95a24822011-09-19 17:32:21 +0800204}
205
Shawn Guo9d61c002013-10-17 15:19:45 +0800206static inline int esdhc_is_usdhc(struct pltfm_imx_data *data)
207{
Shawn Guof47c4bb2013-10-17 15:19:47 +0800208 return !!(data->socdata->flags & ESDHC_FLAG_USDHC);
Shawn Guo9d61c002013-10-17 15:19:45 +0800209}
210
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200211static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
212{
213 void __iomem *base = host->ioaddr + (reg & ~0x3);
214 u32 shift = (reg & 0x3) * 8;
215
216 writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
217}
218
Wolfram Sang7e29c302011-02-26 14:44:41 +0100219static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
220{
Lucas Stach361b8482013-03-15 09:49:26 +0100221 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
222 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100223 u32 val = readl(host->ioaddr + reg);
224
Dong Aisheng03221912013-09-13 19:11:34 +0800225 if (unlikely(reg == SDHCI_PRESENT_STATE)) {
226 u32 fsl_prss = val;
227 /* save the least 20 bits */
228 val = fsl_prss & 0x000FFFFF;
229 /* move dat[0-3] bits */
230 val |= (fsl_prss & 0x0F000000) >> 4;
231 /* move cmd line bit */
232 val |= (fsl_prss & 0x00800000) << 1;
233 }
234
Richard Zhu97e4ba62011-08-11 16:51:46 -0400235 if (unlikely(reg == SDHCI_CAPABILITIES)) {
Dong Aisheng6b4fb6712a2013-10-18 19:48:44 +0800236 /* ignore bit[0-15] as it stores cap_1 register val for mx6sl */
237 if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
238 val &= 0xffff0000;
239
Richard Zhu97e4ba62011-08-11 16:51:46 -0400240 /* In FSL esdhc IC module, only bit20 is used to indicate the
241 * ADMA2 capability of esdhc, but this bit is messed up on
242 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
243 * don't actually support ADMA2). So set the BROKEN_ADMA
244 * uirk on MX25/35 platforms.
245 */
246
247 if (val & SDHCI_CAN_DO_ADMA1) {
248 val &= ~SDHCI_CAN_DO_ADMA1;
249 val |= SDHCI_CAN_DO_ADMA2;
250 }
251 }
252
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800253 if (unlikely(reg == SDHCI_CAPABILITIES_1)) {
254 if (esdhc_is_usdhc(imx_data)) {
255 if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
256 val = readl(host->ioaddr + SDHCI_CAPABILITIES) & 0xFFFF;
257 else
258 /* imx6q/dl does not have cap_1 register, fake one */
259 val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104
Dong Aisheng888824b2013-10-18 19:48:48 +0800260 | SDHCI_SUPPORT_SDR50
261 | SDHCI_USE_SDR50_TUNING;
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800262 }
263 }
Dong Aisheng03221912013-09-13 19:11:34 +0800264
Shawn Guo9d61c002013-10-17 15:19:45 +0800265 if (unlikely(reg == SDHCI_MAX_CURRENT) && esdhc_is_usdhc(imx_data)) {
Dong Aisheng03221912013-09-13 19:11:34 +0800266 val = 0;
267 val |= 0xFF << SDHCI_MAX_CURRENT_330_SHIFT;
268 val |= 0xFF << SDHCI_MAX_CURRENT_300_SHIFT;
269 val |= 0xFF << SDHCI_MAX_CURRENT_180_SHIFT;
270 }
271
Richard Zhu97e4ba62011-08-11 16:51:46 -0400272 if (unlikely(reg == SDHCI_INT_STATUS)) {
Shawn Guo60bf6392013-01-15 23:36:53 +0800273 if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
274 val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
Richard Zhu97e4ba62011-08-11 16:51:46 -0400275 val |= SDHCI_INT_ADMA_ERROR;
276 }
Lucas Stach361b8482013-03-15 09:49:26 +0100277
278 /*
279 * mask off the interrupt we get in response to the manually
280 * sent CMD12
281 */
282 if ((imx_data->multiblock_status == WAIT_FOR_INT) &&
283 ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) {
284 val &= ~SDHCI_INT_RESPONSE;
285 writel(SDHCI_INT_RESPONSE, host->ioaddr +
286 SDHCI_INT_STATUS);
287 imx_data->multiblock_status = NO_CMD_PENDING;
288 }
Richard Zhu97e4ba62011-08-11 16:51:46 -0400289 }
290
Wolfram Sang7e29c302011-02-26 14:44:41 +0100291 return val;
292}
293
294static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
295{
Richard Zhue1498602011-03-25 09:18:27 -0400296 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
297 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Tony Lin0d588642011-08-11 16:45:59 -0400298 u32 data;
Richard Zhue1498602011-03-25 09:18:27 -0400299
Tony Lin0d588642011-08-11 16:45:59 -0400300 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
Tony Lin0d588642011-08-11 16:45:59 -0400301 if (val & SDHCI_INT_CARD_INT) {
302 /*
303 * Clear and then set D3CD bit to avoid missing the
304 * card interrupt. This is a eSDHC controller problem
305 * so we need to apply the following workaround: clear
306 * and set D3CD bit will make eSDHC re-sample the card
307 * interrupt. In case a card interrupt was lost,
308 * re-sample it by the following steps.
309 */
310 data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
Shawn Guo60bf6392013-01-15 23:36:53 +0800311 data &= ~ESDHC_CTRL_D3CD;
Tony Lin0d588642011-08-11 16:45:59 -0400312 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
Shawn Guo60bf6392013-01-15 23:36:53 +0800313 data |= ESDHC_CTRL_D3CD;
Tony Lin0d588642011-08-11 16:45:59 -0400314 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
315 }
316 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100317
Shawn Guof47c4bb2013-10-17 15:19:47 +0800318 if (unlikely((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
Richard Zhu58ac8172011-03-21 13:22:16 +0800319 && (reg == SDHCI_INT_STATUS)
320 && (val & SDHCI_INT_DATA_END))) {
321 u32 v;
Shawn Guo60bf6392013-01-15 23:36:53 +0800322 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
323 v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
324 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
Lucas Stach361b8482013-03-15 09:49:26 +0100325
326 if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS)
327 {
328 /* send a manual CMD12 with RESPTYP=none */
329 data = MMC_STOP_TRANSMISSION << 24 |
330 SDHCI_CMD_ABORTCMD << 16;
331 writel(data, host->ioaddr + SDHCI_TRANSFER_MODE);
332 imx_data->multiblock_status = WAIT_FOR_INT;
333 }
Richard Zhu58ac8172011-03-21 13:22:16 +0800334 }
335
Richard Zhu97e4ba62011-08-11 16:51:46 -0400336 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
337 if (val & SDHCI_INT_ADMA_ERROR) {
338 val &= ~SDHCI_INT_ADMA_ERROR;
Shawn Guo60bf6392013-01-15 23:36:53 +0800339 val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
Richard Zhu97e4ba62011-08-11 16:51:46 -0400340 }
341 }
342
Wolfram Sang7e29c302011-02-26 14:44:41 +0100343 writel(val, host->ioaddr + reg);
344}
345
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200346static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
347{
Shawn Guoef4d0882013-01-15 23:30:27 +0800348 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
349 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aisheng03221912013-09-13 19:11:34 +0800350 u16 ret = 0;
351 u32 val;
Shawn Guoef4d0882013-01-15 23:30:27 +0800352
Shawn Guo95a24822011-09-19 17:32:21 +0800353 if (unlikely(reg == SDHCI_HOST_VERSION)) {
Shawn Guoef4d0882013-01-15 23:30:27 +0800354 reg ^= 2;
Shawn Guo9d61c002013-10-17 15:19:45 +0800355 if (esdhc_is_usdhc(imx_data)) {
Shawn Guoef4d0882013-01-15 23:30:27 +0800356 /*
357 * The usdhc register returns a wrong host version.
358 * Correct it here.
359 */
360 return SDHCI_SPEC_300;
361 }
Shawn Guo95a24822011-09-19 17:32:21 +0800362 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200363
Dong Aisheng03221912013-09-13 19:11:34 +0800364 if (unlikely(reg == SDHCI_HOST_CONTROL2)) {
365 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
366 if (val & ESDHC_VENDOR_SPEC_VSELECT)
367 ret |= SDHCI_CTRL_VDD_180;
368
Shawn Guo9d61c002013-10-17 15:19:45 +0800369 if (esdhc_is_usdhc(imx_data)) {
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800370 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
371 val = readl(host->ioaddr + ESDHC_MIX_CTRL);
372 else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING)
373 /* the std tuning bits is in ACMD12_ERR for imx6sl */
374 val = readl(host->ioaddr + SDHCI_ACMD12_ERR);
Dong Aisheng03221912013-09-13 19:11:34 +0800375 }
376
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800377 if (val & ESDHC_MIX_CTRL_EXE_TUNE)
378 ret |= SDHCI_CTRL_EXEC_TUNING;
379 if (val & ESDHC_MIX_CTRL_SMPCLK_SEL)
380 ret |= SDHCI_CTRL_TUNED_CLK;
381
Dong Aisheng03221912013-09-13 19:11:34 +0800382 ret |= (imx_data->uhs_mode & SDHCI_CTRL_UHS_MASK);
383 ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
384
385 return ret;
386 }
387
Dong Aisheng7dd109e2013-10-30 22:09:49 +0800388 if (unlikely(reg == SDHCI_TRANSFER_MODE)) {
389 if (esdhc_is_usdhc(imx_data)) {
390 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
391 ret = m & ESDHC_MIX_CTRL_SDHCI_MASK;
392 /* Swap AC23 bit */
393 if (m & ESDHC_MIX_CTRL_AC23EN) {
394 ret &= ~ESDHC_MIX_CTRL_AC23EN;
395 ret |= SDHCI_TRNS_AUTO_CMD23;
396 }
397 } else {
398 ret = readw(host->ioaddr + SDHCI_TRANSFER_MODE);
399 }
400
401 return ret;
402 }
403
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200404 return readw(host->ioaddr + reg);
405}
406
407static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
408{
409 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400410 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aisheng03221912013-09-13 19:11:34 +0800411 u32 new_val = 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200412
413 switch (reg) {
Dong Aisheng03221912013-09-13 19:11:34 +0800414 case SDHCI_CLOCK_CONTROL:
415 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
416 if (val & SDHCI_CLOCK_CARD_EN)
417 new_val |= ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
418 else
419 new_val &= ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
420 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
421 return;
422 case SDHCI_HOST_CONTROL2:
423 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
424 if (val & SDHCI_CTRL_VDD_180)
425 new_val |= ESDHC_VENDOR_SPEC_VSELECT;
426 else
427 new_val &= ~ESDHC_VENDOR_SPEC_VSELECT;
428 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
429 imx_data->uhs_mode = val & SDHCI_CTRL_UHS_MASK;
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800430 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
431 new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
432 if (val & SDHCI_CTRL_TUNED_CLK)
433 new_val |= ESDHC_MIX_CTRL_SMPCLK_SEL;
434 else
435 new_val &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
436 writel(new_val , host->ioaddr + ESDHC_MIX_CTRL);
437 } else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
438 u32 v = readl(host->ioaddr + SDHCI_ACMD12_ERR);
439 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
440 new_val = readl(host->ioaddr + ESDHC_TUNING_CTRL);
441 if (val & SDHCI_CTRL_EXEC_TUNING) {
442 new_val |= ESDHC_STD_TUNING_EN |
443 ESDHC_TUNING_START_TAP;
444 v |= ESDHC_MIX_CTRL_EXE_TUNE;
445 m |= ESDHC_MIX_CTRL_FBCLK_SEL;
446 } else {
447 new_val &= ~ESDHC_STD_TUNING_EN;
448 v &= ~ESDHC_MIX_CTRL_EXE_TUNE;
449 m &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
450 }
451
452 if (val & SDHCI_CTRL_TUNED_CLK)
453 v |= ESDHC_MIX_CTRL_SMPCLK_SEL;
454 else
455 v &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
456
457 writel(new_val, host->ioaddr + ESDHC_TUNING_CTRL);
458 writel(v, host->ioaddr + SDHCI_ACMD12_ERR);
459 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
460 }
Dong Aisheng03221912013-09-13 19:11:34 +0800461 return;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200462 case SDHCI_TRANSFER_MODE:
Shawn Guof47c4bb2013-10-17 15:19:47 +0800463 if ((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
Richard Zhu58ac8172011-03-21 13:22:16 +0800464 && (host->cmd->opcode == SD_IO_RW_EXTENDED)
465 && (host->cmd->data->blocks > 1)
466 && (host->cmd->data->flags & MMC_DATA_READ)) {
467 u32 v;
Shawn Guo60bf6392013-01-15 23:36:53 +0800468 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
469 v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
470 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
Richard Zhu58ac8172011-03-21 13:22:16 +0800471 }
Shawn Guo69f54692013-01-21 19:02:24 +0800472
Shawn Guo9d61c002013-10-17 15:19:45 +0800473 if (esdhc_is_usdhc(imx_data)) {
Shawn Guo69f54692013-01-21 19:02:24 +0800474 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
Shawn Guo2a15f982013-01-21 19:02:26 +0800475 /* Swap AC23 bit */
476 if (val & SDHCI_TRNS_AUTO_CMD23) {
477 val &= ~SDHCI_TRNS_AUTO_CMD23;
478 val |= ESDHC_MIX_CTRL_AC23EN;
479 }
480 m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
Shawn Guo69f54692013-01-21 19:02:24 +0800481 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
482 } else {
483 /*
484 * Postpone this write, we must do it together with a
485 * command write that is down below.
486 */
487 imx_data->scratchpad = val;
488 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200489 return;
490 case SDHCI_COMMAND:
Lucas Stach361b8482013-03-15 09:49:26 +0100491 if (host->cmd->opcode == MMC_STOP_TRANSMISSION)
Richard Zhu58ac8172011-03-21 13:22:16 +0800492 val |= SDHCI_CMD_ABORTCMD;
Shawn Guo95a24822011-09-19 17:32:21 +0800493
Lucas Stach361b8482013-03-15 09:49:26 +0100494 if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
Shawn Guof47c4bb2013-10-17 15:19:47 +0800495 (imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
Lucas Stach361b8482013-03-15 09:49:26 +0100496 imx_data->multiblock_status = MULTIBLK_IN_PROCESS;
497
Shawn Guo9d61c002013-10-17 15:19:45 +0800498 if (esdhc_is_usdhc(imx_data))
Shawn Guo95a24822011-09-19 17:32:21 +0800499 writel(val << 16,
500 host->ioaddr + SDHCI_TRANSFER_MODE);
Shawn Guo69f54692013-01-21 19:02:24 +0800501 else
Shawn Guo95a24822011-09-19 17:32:21 +0800502 writel(val << 16 | imx_data->scratchpad,
503 host->ioaddr + SDHCI_TRANSFER_MODE);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200504 return;
505 case SDHCI_BLOCK_SIZE:
506 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
507 break;
508 }
509 esdhc_clrset_le(host, 0xffff, val, reg);
510}
511
512static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
513{
Wilson Callan9a0985b2012-07-19 02:49:16 -0400514 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
515 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200516 u32 new_val;
Sascha Haueraf510792013-01-21 19:02:28 +0800517 u32 mask;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200518
519 switch (reg) {
520 case SDHCI_POWER_CONTROL:
521 /*
522 * FSL put some DMA bits here
523 * If your board has a regulator, code should be here
524 */
525 return;
526 case SDHCI_HOST_CONTROL:
Shawn Guo6b40d182013-01-15 23:36:52 +0800527 /* FSL messed up here, so we need to manually compose it. */
Sascha Haueraf510792013-01-21 19:02:28 +0800528 new_val = val & SDHCI_CTRL_LED;
Masanari Iida7122bbb2012-08-05 23:25:40 +0900529 /* ensure the endianness */
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200530 new_val |= ESDHC_HOST_CONTROL_LE;
Wilson Callan9a0985b2012-07-19 02:49:16 -0400531 /* bits 8&9 are reserved on mx25 */
532 if (!is_imx25_esdhc(imx_data)) {
533 /* DMA mode bits are shifted */
534 new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
535 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200536
Sascha Haueraf510792013-01-21 19:02:28 +0800537 /*
538 * Do not touch buswidth bits here. This is done in
539 * esdhc_pltfm_bus_width.
Martin Fuzzeyf6825742013-04-15 17:08:35 +0200540 * Do not touch the D3CD bit either which is used for the
541 * SDIO interrupt errata workaround.
Sascha Haueraf510792013-01-21 19:02:28 +0800542 */
Martin Fuzzeyf6825742013-04-15 17:08:35 +0200543 mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD);
Sascha Haueraf510792013-01-21 19:02:28 +0800544
545 esdhc_clrset_le(host, mask, new_val, reg);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200546 return;
547 }
548 esdhc_clrset_le(host, 0xff, val, reg);
Shawn Guo913413c2011-06-21 22:41:51 +0800549
550 /*
551 * The esdhc has a design violation to SDHC spec which tells
552 * that software reset should not affect card detection circuit.
553 * But esdhc clears its SYSCTL register bits [0..2] during the
554 * software reset. This will stop those clocks that card detection
555 * circuit relies on. To work around it, we turn the clocks on back
556 * to keep card detection circuit functional.
557 */
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800558 if ((reg == SDHCI_SOFTWARE_RESET) && (val & 1)) {
Shawn Guo913413c2011-06-21 22:41:51 +0800559 esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800560 /*
561 * The reset on usdhc fails to clear MIX_CTRL register.
562 * Do it manually here.
563 */
Dong Aishengde5bdbf2013-10-18 19:48:46 +0800564 if (esdhc_is_usdhc(imx_data)) {
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800565 writel(0, host->ioaddr + ESDHC_MIX_CTRL);
Dong Aishengde5bdbf2013-10-18 19:48:46 +0800566 imx_data->is_ddr = 0;
567 }
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800568 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200569}
570
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200571static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
572{
573 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
574 struct pltfm_imx_data *imx_data = pltfm_host->priv;
575 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
576
577 u32 f_host = clk_get_rate(pltfm_host->clk);
578
579 if (boarddata->f_max && (boarddata->f_max < f_host))
580 return boarddata->f_max;
581 else
582 return f_host;
583}
584
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200585static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
586{
587 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
588
589 return clk_get_rate(pltfm_host->clk) / 256 / 16;
590}
591
Lucas Stach8ba95802013-06-05 15:13:25 +0200592static inline void esdhc_pltfm_set_clock(struct sdhci_host *host,
593 unsigned int clock)
594{
595 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800596 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aishengd31fc002013-09-13 19:11:32 +0800597 unsigned int host_clock = clk_get_rate(pltfm_host->clk);
598 int pre_div = 2;
599 int div = 1;
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800600 u32 temp, val;
Lucas Stach8ba95802013-06-05 15:13:25 +0200601
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800602 if (clock == 0) {
Shawn Guo9d61c002013-10-17 15:19:45 +0800603 if (esdhc_is_usdhc(imx_data)) {
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800604 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
605 writel(val & ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
606 host->ioaddr + ESDHC_VENDOR_SPEC);
607 }
Dong Aishengd31fc002013-09-13 19:11:32 +0800608 goto out;
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800609 }
Dong Aishengd31fc002013-09-13 19:11:32 +0800610
Dong Aishengde5bdbf2013-10-18 19:48:46 +0800611 if (esdhc_is_usdhc(imx_data) && !imx_data->is_ddr)
Dong Aisheng5f7886c2013-09-13 19:11:36 +0800612 pre_div = 1;
613
Dong Aishengd31fc002013-09-13 19:11:32 +0800614 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
615 temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
616 | ESDHC_CLOCK_MASK);
617 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
618
619 while (host_clock / pre_div / 16 > clock && pre_div < 256)
620 pre_div *= 2;
621
622 while (host_clock / pre_div / div > clock && div < 16)
623 div++;
624
Dong Aishenge76b8552013-09-13 19:11:37 +0800625 host->mmc->actual_clock = host_clock / pre_div / div;
Dong Aishengd31fc002013-09-13 19:11:32 +0800626 dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
Dong Aishenge76b8552013-09-13 19:11:37 +0800627 clock, host->mmc->actual_clock);
Dong Aishengd31fc002013-09-13 19:11:32 +0800628
Dong Aishengde5bdbf2013-10-18 19:48:46 +0800629 if (imx_data->is_ddr)
630 pre_div >>= 2;
631 else
632 pre_div >>= 1;
Dong Aishengd31fc002013-09-13 19:11:32 +0800633 div--;
634
635 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
636 temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
637 | (div << ESDHC_DIVIDER_SHIFT)
638 | (pre_div << ESDHC_PREDIV_SHIFT));
639 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800640
Shawn Guo9d61c002013-10-17 15:19:45 +0800641 if (esdhc_is_usdhc(imx_data)) {
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800642 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
643 writel(val | ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
644 host->ioaddr + ESDHC_VENDOR_SPEC);
645 }
646
Dong Aishengd31fc002013-09-13 19:11:32 +0800647 mdelay(1);
648out:
649 host->clock = clock;
Lucas Stach8ba95802013-06-05 15:13:25 +0200650}
651
Shawn Guo913413c2011-06-21 22:41:51 +0800652static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
653{
Shawn Guo842afc02011-07-06 22:57:48 +0800654 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
655 struct pltfm_imx_data *imx_data = pltfm_host->priv;
656 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Shawn Guo913413c2011-06-21 22:41:51 +0800657
658 switch (boarddata->wp_type) {
659 case ESDHC_WP_GPIO:
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800660 return mmc_gpio_get_ro(host->mmc);
Shawn Guo913413c2011-06-21 22:41:51 +0800661 case ESDHC_WP_CONTROLLER:
662 return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
663 SDHCI_WRITE_PROTECT);
664 case ESDHC_WP_NONE:
665 break;
666 }
667
668 return -ENOSYS;
669}
670
Sascha Haueraf510792013-01-21 19:02:28 +0800671static int esdhc_pltfm_bus_width(struct sdhci_host *host, int width)
672{
673 u32 ctrl;
674
675 switch (width) {
676 case MMC_BUS_WIDTH_8:
677 ctrl = ESDHC_CTRL_8BITBUS;
678 break;
679 case MMC_BUS_WIDTH_4:
680 ctrl = ESDHC_CTRL_4BITBUS;
681 break;
682 default:
683 ctrl = 0;
684 break;
685 }
686
687 esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
688 SDHCI_HOST_CONTROL);
689
690 return 0;
691}
692
Dong Aisheng03221912013-09-13 19:11:34 +0800693static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val)
694{
695 u32 reg;
696
697 /* FIXME: delay a bit for card to be ready for next tuning due to errors */
698 mdelay(1);
699
700 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
701 reg |= ESDHC_MIX_CTRL_EXE_TUNE | ESDHC_MIX_CTRL_SMPCLK_SEL |
702 ESDHC_MIX_CTRL_FBCLK_SEL;
703 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
704 writel(val << 8, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
705 dev_dbg(mmc_dev(host->mmc),
706 "tunning with delay 0x%x ESDHC_TUNE_CTRL_STATUS 0x%x\n",
707 val, readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS));
708}
709
710static void esdhc_request_done(struct mmc_request *mrq)
711{
712 complete(&mrq->completion);
713}
714
715static int esdhc_send_tuning_cmd(struct sdhci_host *host, u32 opcode)
716{
717 struct mmc_command cmd = {0};
718 struct mmc_request mrq = {0};
719 struct mmc_data data = {0};
720 struct scatterlist sg;
721 char tuning_pattern[ESDHC_TUNING_BLOCK_PATTERN_LEN];
722
723 cmd.opcode = opcode;
724 cmd.arg = 0;
725 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
726
727 data.blksz = ESDHC_TUNING_BLOCK_PATTERN_LEN;
728 data.blocks = 1;
729 data.flags = MMC_DATA_READ;
730 data.sg = &sg;
731 data.sg_len = 1;
732
733 sg_init_one(&sg, tuning_pattern, sizeof(tuning_pattern));
734
735 mrq.cmd = &cmd;
736 mrq.cmd->mrq = &mrq;
737 mrq.data = &data;
738 mrq.data->mrq = &mrq;
739 mrq.cmd->data = mrq.data;
740
741 mrq.done = esdhc_request_done;
742 init_completion(&(mrq.completion));
743
744 disable_irq(host->irq);
745 spin_lock(&host->lock);
746 host->mrq = &mrq;
747
748 sdhci_send_command(host, mrq.cmd);
749
750 spin_unlock(&host->lock);
751 enable_irq(host->irq);
752
753 wait_for_completion(&mrq.completion);
754
755 if (cmd.error)
756 return cmd.error;
757 if (data.error)
758 return data.error;
759
760 return 0;
761}
762
763static void esdhc_post_tuning(struct sdhci_host *host)
764{
765 u32 reg;
766
767 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
768 reg &= ~ESDHC_MIX_CTRL_EXE_TUNE;
769 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
770}
771
772static int esdhc_executing_tuning(struct sdhci_host *host, u32 opcode)
773{
774 int min, max, avg, ret;
775
776 /* find the mininum delay first which can pass tuning */
777 min = ESDHC_TUNE_CTRL_MIN;
778 while (min < ESDHC_TUNE_CTRL_MAX) {
779 esdhc_prepare_tuning(host, min);
780 if (!esdhc_send_tuning_cmd(host, opcode))
781 break;
782 min += ESDHC_TUNE_CTRL_STEP;
783 }
784
785 /* find the maxinum delay which can not pass tuning */
786 max = min + ESDHC_TUNE_CTRL_STEP;
787 while (max < ESDHC_TUNE_CTRL_MAX) {
788 esdhc_prepare_tuning(host, max);
789 if (esdhc_send_tuning_cmd(host, opcode)) {
790 max -= ESDHC_TUNE_CTRL_STEP;
791 break;
792 }
793 max += ESDHC_TUNE_CTRL_STEP;
794 }
795
796 /* use average delay to get the best timing */
797 avg = (min + max) / 2;
798 esdhc_prepare_tuning(host, avg);
799 ret = esdhc_send_tuning_cmd(host, opcode);
800 esdhc_post_tuning(host);
801
802 dev_dbg(mmc_dev(host->mmc), "tunning %s at 0x%x ret %d\n",
803 ret ? "failed" : "passed", avg, ret);
804
805 return ret;
806}
807
Dong Aishengad932202013-09-13 19:11:35 +0800808static int esdhc_change_pinstate(struct sdhci_host *host,
809 unsigned int uhs)
810{
811 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
812 struct pltfm_imx_data *imx_data = pltfm_host->priv;
813 struct pinctrl_state *pinctrl;
814
815 dev_dbg(mmc_dev(host->mmc), "change pinctrl state for uhs %d\n", uhs);
816
817 if (IS_ERR(imx_data->pinctrl) ||
818 IS_ERR(imx_data->pins_default) ||
819 IS_ERR(imx_data->pins_100mhz) ||
820 IS_ERR(imx_data->pins_200mhz))
821 return -EINVAL;
822
823 switch (uhs) {
824 case MMC_TIMING_UHS_SDR50:
825 pinctrl = imx_data->pins_100mhz;
826 break;
827 case MMC_TIMING_UHS_SDR104:
828 pinctrl = imx_data->pins_200mhz;
829 break;
830 default:
831 /* back to default state for other legacy timing */
832 pinctrl = imx_data->pins_default;
833 }
834
835 return pinctrl_select_state(imx_data->pinctrl, pinctrl);
836}
837
838static int esdhc_set_uhs_signaling(struct sdhci_host *host, unsigned int uhs)
839{
840 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
841 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aisheng602519b2013-10-18 19:48:47 +0800842 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Dong Aishengad932202013-09-13 19:11:35 +0800843
844 switch (uhs) {
845 case MMC_TIMING_UHS_SDR12:
846 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR12;
847 break;
848 case MMC_TIMING_UHS_SDR25:
849 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR25;
850 break;
851 case MMC_TIMING_UHS_SDR50:
852 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR50;
853 break;
854 case MMC_TIMING_UHS_SDR104:
855 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR104;
856 break;
857 case MMC_TIMING_UHS_DDR50:
858 imx_data->uhs_mode = SDHCI_CTRL_UHS_DDR50;
Dong Aishengde5bdbf2013-10-18 19:48:46 +0800859 writel(readl(host->ioaddr + ESDHC_MIX_CTRL) |
860 ESDHC_MIX_CTRL_DDREN,
861 host->ioaddr + ESDHC_MIX_CTRL);
862 imx_data->is_ddr = 1;
Dong Aisheng602519b2013-10-18 19:48:47 +0800863 if (boarddata->delay_line) {
864 u32 v;
865 v = boarddata->delay_line <<
866 ESDHC_DLL_OVERRIDE_VAL_SHIFT |
867 (1 << ESDHC_DLL_OVERRIDE_EN_SHIFT);
868 if (is_imx53_esdhc(imx_data))
869 v <<= 1;
870 writel(v, host->ioaddr + ESDHC_DLL_CTRL);
871 }
Dong Aishengad932202013-09-13 19:11:35 +0800872 break;
873 }
874
875 return esdhc_change_pinstate(host, uhs);
876}
877
Dong Aisheng6e9fd282013-10-18 19:48:43 +0800878static struct sdhci_ops sdhci_esdhc_ops = {
Richard Zhue1498602011-03-25 09:18:27 -0400879 .read_l = esdhc_readl_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100880 .read_w = esdhc_readw_le,
Richard Zhue1498602011-03-25 09:18:27 -0400881 .write_l = esdhc_writel_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100882 .write_w = esdhc_writew_le,
883 .write_b = esdhc_writeb_le,
Lucas Stach8ba95802013-06-05 15:13:25 +0200884 .set_clock = esdhc_pltfm_set_clock,
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200885 .get_max_clock = esdhc_pltfm_get_max_clock,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100886 .get_min_clock = esdhc_pltfm_get_min_clock,
Shawn Guo913413c2011-06-21 22:41:51 +0800887 .get_ro = esdhc_pltfm_get_ro,
Sascha Haueraf510792013-01-21 19:02:28 +0800888 .platform_bus_width = esdhc_pltfm_bus_width,
Dong Aishengad932202013-09-13 19:11:35 +0800889 .set_uhs_signaling = esdhc_set_uhs_signaling,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100890};
891
Lars-Peter Clausen1db5eeb2013-03-13 19:26:03 +0100892static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
Richard Zhu97e4ba62011-08-11 16:51:46 -0400893 .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
894 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
895 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
Shawn Guo85d65092011-05-27 23:48:12 +0800896 | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
Shawn Guo85d65092011-05-27 23:48:12 +0800897 .ops = &sdhci_esdhc_ops,
898};
899
Shawn Guoabfafc22011-06-30 15:44:44 +0800900#ifdef CONFIG_OF
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500901static int
Shawn Guoabfafc22011-06-30 15:44:44 +0800902sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
903 struct esdhc_platform_data *boarddata)
904{
905 struct device_node *np = pdev->dev.of_node;
906
907 if (!np)
908 return -ENODEV;
909
Arnd Bergmann7f217792012-05-13 00:14:24 -0400910 if (of_get_property(np, "non-removable", NULL))
Shawn Guoabfafc22011-06-30 15:44:44 +0800911 boarddata->cd_type = ESDHC_CD_PERMANENT;
912
913 if (of_get_property(np, "fsl,cd-controller", NULL))
914 boarddata->cd_type = ESDHC_CD_CONTROLLER;
915
916 if (of_get_property(np, "fsl,wp-controller", NULL))
917 boarddata->wp_type = ESDHC_WP_CONTROLLER;
918
919 boarddata->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
920 if (gpio_is_valid(boarddata->cd_gpio))
921 boarddata->cd_type = ESDHC_CD_GPIO;
922
923 boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
924 if (gpio_is_valid(boarddata->wp_gpio))
925 boarddata->wp_type = ESDHC_WP_GPIO;
926
Sascha Haueraf510792013-01-21 19:02:28 +0800927 of_property_read_u32(np, "bus-width", &boarddata->max_bus_width);
928
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200929 of_property_read_u32(np, "max-frequency", &boarddata->f_max);
930
Dong Aishengad932202013-09-13 19:11:35 +0800931 if (of_find_property(np, "no-1-8-v", NULL))
932 boarddata->support_vsel = false;
933 else
934 boarddata->support_vsel = true;
935
Dong Aisheng602519b2013-10-18 19:48:47 +0800936 if (of_property_read_u32(np, "fsl,delay-line", &boarddata->delay_line))
937 boarddata->delay_line = 0;
938
Shawn Guoabfafc22011-06-30 15:44:44 +0800939 return 0;
940}
941#else
942static inline int
943sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
944 struct esdhc_platform_data *boarddata)
945{
946 return -ENODEV;
947}
948#endif
949
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500950static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200951{
Shawn Guoabfafc22011-06-30 15:44:44 +0800952 const struct of_device_id *of_id =
953 of_match_device(imx_esdhc_dt_ids, &pdev->dev);
Shawn Guo85d65092011-05-27 23:48:12 +0800954 struct sdhci_pltfm_host *pltfm_host;
955 struct sdhci_host *host;
956 struct esdhc_platform_data *boarddata;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100957 int err;
Richard Zhue1498602011-03-25 09:18:27 -0400958 struct pltfm_imx_data *imx_data;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200959
Christian Daudt0e748232013-05-29 13:50:05 -0700960 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata, 0);
Shawn Guo85d65092011-05-27 23:48:12 +0800961 if (IS_ERR(host))
962 return PTR_ERR(host);
963
964 pltfm_host = sdhci_priv(host);
965
Shawn Guoe3af31c2012-11-26 14:39:43 +0800966 imx_data = devm_kzalloc(&pdev->dev, sizeof(*imx_data), GFP_KERNEL);
Shawn Guoabfafc22011-06-30 15:44:44 +0800967 if (!imx_data) {
968 err = -ENOMEM;
Shawn Guoe3af31c2012-11-26 14:39:43 +0800969 goto free_sdhci;
Shawn Guoabfafc22011-06-30 15:44:44 +0800970 }
Shawn Guo57ed3312011-06-30 09:24:26 +0800971
Shawn Guof47c4bb2013-10-17 15:19:47 +0800972 imx_data->socdata = of_id ? of_id->data : (struct esdhc_soc_data *)
973 pdev->id_entry->driver_data;
Shawn Guo85d65092011-05-27 23:48:12 +0800974 pltfm_host->priv = imx_data;
975
Sascha Hauer52dac612012-03-07 09:31:34 +0100976 imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
977 if (IS_ERR(imx_data->clk_ipg)) {
978 err = PTR_ERR(imx_data->clk_ipg);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800979 goto free_sdhci;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200980 }
Sascha Hauer52dac612012-03-07 09:31:34 +0100981
982 imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
983 if (IS_ERR(imx_data->clk_ahb)) {
984 err = PTR_ERR(imx_data->clk_ahb);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800985 goto free_sdhci;
Sascha Hauer52dac612012-03-07 09:31:34 +0100986 }
987
988 imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
989 if (IS_ERR(imx_data->clk_per)) {
990 err = PTR_ERR(imx_data->clk_per);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800991 goto free_sdhci;
Sascha Hauer52dac612012-03-07 09:31:34 +0100992 }
993
994 pltfm_host->clk = imx_data->clk_per;
995
996 clk_prepare_enable(imx_data->clk_per);
997 clk_prepare_enable(imx_data->clk_ipg);
998 clk_prepare_enable(imx_data->clk_ahb);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200999
Dong Aishengad932202013-09-13 19:11:35 +08001000 imx_data->pinctrl = devm_pinctrl_get(&pdev->dev);
Dong Aishenge62d8b82012-05-11 14:56:01 +08001001 if (IS_ERR(imx_data->pinctrl)) {
1002 err = PTR_ERR(imx_data->pinctrl);
Shawn Guoe3af31c2012-11-26 14:39:43 +08001003 goto disable_clk;
Dong Aishenge62d8b82012-05-11 14:56:01 +08001004 }
1005
Dong Aishengad932202013-09-13 19:11:35 +08001006 imx_data->pins_default = pinctrl_lookup_state(imx_data->pinctrl,
1007 PINCTRL_STATE_DEFAULT);
1008 if (IS_ERR(imx_data->pins_default)) {
1009 err = PTR_ERR(imx_data->pins_default);
1010 dev_err(mmc_dev(host->mmc), "could not get default state\n");
1011 goto disable_clk;
1012 }
1013
Eric BĂ©nardb89152822012-04-18 02:30:20 +02001014 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
Eric BĂ©nard37865fe2010-10-23 01:57:21 +02001015
Shawn Guof47c4bb2013-10-17 15:19:47 +08001016 if (imx_data->socdata->flags & ESDHC_FLAG_ENGCM07207)
Wolfram Sang0c6d49c2011-02-26 14:44:39 +01001017 /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
Richard Zhu97e4ba62011-08-11 16:51:46 -04001018 host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK
1019 | SDHCI_QUIRK_BROKEN_ADMA;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +01001020
Shawn Guof750ba92011-11-10 16:39:32 +08001021 /*
1022 * The imx6q ROM code will change the default watermark level setting
1023 * to something insane. Change it back here.
1024 */
Dong Aisheng69ed60e2013-10-18 19:48:49 +08001025 if (esdhc_is_usdhc(imx_data)) {
Shawn Guo60bf6392013-01-15 23:36:53 +08001026 writel(0x08100810, host->ioaddr + ESDHC_WTMK_LVL);
Dong Aisheng69ed60e2013-10-18 19:48:49 +08001027 host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
Dong Aishenge2997c92013-10-30 22:09:52 +08001028 host->mmc->caps |= MMC_CAP_1_8V_DDR;
Dong Aisheng69ed60e2013-10-18 19:48:49 +08001029 }
Shawn Guof750ba92011-11-10 16:39:32 +08001030
Dong Aisheng6e9fd282013-10-18 19:48:43 +08001031 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
1032 sdhci_esdhc_ops.platform_execute_tuning =
1033 esdhc_executing_tuning;
Shawn Guo842afc02011-07-06 22:57:48 +08001034 boarddata = &imx_data->boarddata;
Shawn Guoabfafc22011-06-30 15:44:44 +08001035 if (sdhci_esdhc_imx_probe_dt(pdev, boarddata) < 0) {
1036 if (!host->mmc->parent->platform_data) {
1037 dev_err(mmc_dev(host->mmc), "no board data!\n");
1038 err = -EINVAL;
Shawn Guoe3af31c2012-11-26 14:39:43 +08001039 goto disable_clk;
Shawn Guoabfafc22011-06-30 15:44:44 +08001040 }
1041 imx_data->boarddata = *((struct esdhc_platform_data *)
1042 host->mmc->parent->platform_data);
1043 }
Shawn Guo913413c2011-06-21 22:41:51 +08001044
1045 /* write_protect */
1046 if (boarddata->wp_type == ESDHC_WP_GPIO) {
Shawn Guofbe5fdd2012-12-11 22:32:20 +08001047 err = mmc_gpio_request_ro(host->mmc, boarddata->wp_gpio);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +01001048 if (err) {
Shawn Guofbe5fdd2012-12-11 22:32:20 +08001049 dev_err(mmc_dev(host->mmc),
1050 "failed to request write-protect gpio!\n");
1051 goto disable_clk;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +01001052 }
Shawn Guofbe5fdd2012-12-11 22:32:20 +08001053 host->mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
Shawn Guo913413c2011-06-21 22:41:51 +08001054 }
Wolfram Sang7e29c302011-02-26 14:44:41 +01001055
Shawn Guo913413c2011-06-21 22:41:51 +08001056 /* card_detect */
Shawn Guo913413c2011-06-21 22:41:51 +08001057 switch (boarddata->cd_type) {
1058 case ESDHC_CD_GPIO:
Laurent Pinchart214fc302013-08-08 12:38:31 +02001059 err = mmc_gpio_request_cd(host->mmc, boarddata->cd_gpio, 0);
Wolfram Sang7e29c302011-02-26 14:44:41 +01001060 if (err) {
Shawn Guo913413c2011-06-21 22:41:51 +08001061 dev_err(mmc_dev(host->mmc),
Shawn Guofbe5fdd2012-12-11 22:32:20 +08001062 "failed to request card-detect gpio!\n");
Shawn Guoe3af31c2012-11-26 14:39:43 +08001063 goto disable_clk;
Wolfram Sang7e29c302011-02-26 14:44:41 +01001064 }
Shawn Guo913413c2011-06-21 22:41:51 +08001065 /* fall through */
Wolfram Sang7e29c302011-02-26 14:44:41 +01001066
Shawn Guo913413c2011-06-21 22:41:51 +08001067 case ESDHC_CD_CONTROLLER:
1068 /* we have a working card_detect back */
Wolfram Sang7e29c302011-02-26 14:44:41 +01001069 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
Shawn Guo913413c2011-06-21 22:41:51 +08001070 break;
1071
1072 case ESDHC_CD_PERMANENT:
Dong Aishenge5260032013-10-30 22:09:51 +08001073 host->mmc->caps |= MMC_CAP_NONREMOVABLE;
Shawn Guo913413c2011-06-21 22:41:51 +08001074 break;
1075
1076 case ESDHC_CD_NONE:
1077 break;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +01001078 }
Eric BĂ©nard16a790b2010-10-23 01:57:22 +02001079
Sascha Haueraf510792013-01-21 19:02:28 +08001080 switch (boarddata->max_bus_width) {
1081 case 8:
1082 host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA;
1083 break;
1084 case 4:
1085 host->mmc->caps |= MMC_CAP_4_BIT_DATA;
1086 break;
1087 case 1:
1088 default:
1089 host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
1090 break;
1091 }
1092
Dong Aishengad932202013-09-13 19:11:35 +08001093 /* sdr50 and sdr104 needs work on 1.8v signal voltage */
Shawn Guo9d61c002013-10-17 15:19:45 +08001094 if ((boarddata->support_vsel) && esdhc_is_usdhc(imx_data)) {
Dong Aishengad932202013-09-13 19:11:35 +08001095 imx_data->pins_100mhz = pinctrl_lookup_state(imx_data->pinctrl,
1096 ESDHC_PINCTRL_STATE_100MHZ);
1097 imx_data->pins_200mhz = pinctrl_lookup_state(imx_data->pinctrl,
1098 ESDHC_PINCTRL_STATE_200MHZ);
1099 if (IS_ERR(imx_data->pins_100mhz) ||
1100 IS_ERR(imx_data->pins_200mhz)) {
1101 dev_warn(mmc_dev(host->mmc),
1102 "could not get ultra high speed state, work on normal mode\n");
1103 /* fall back to not support uhs by specify no 1.8v quirk */
1104 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1105 }
1106 } else {
1107 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1108 }
1109
Shawn Guo85d65092011-05-27 23:48:12 +08001110 err = sdhci_add_host(host);
1111 if (err)
Shawn Guoe3af31c2012-11-26 14:39:43 +08001112 goto disable_clk;
Shawn Guo85d65092011-05-27 23:48:12 +08001113
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001114 return 0;
Wolfram Sang7e29c302011-02-26 14:44:41 +01001115
Shawn Guoe3af31c2012-11-26 14:39:43 +08001116disable_clk:
Sascha Hauer52dac612012-03-07 09:31:34 +01001117 clk_disable_unprepare(imx_data->clk_per);
1118 clk_disable_unprepare(imx_data->clk_ipg);
1119 clk_disable_unprepare(imx_data->clk_ahb);
Shawn Guoe3af31c2012-11-26 14:39:43 +08001120free_sdhci:
Shawn Guo85d65092011-05-27 23:48:12 +08001121 sdhci_pltfm_free(pdev);
1122 return err;
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001123}
1124
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001125static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001126{
Shawn Guo85d65092011-05-27 23:48:12 +08001127 struct sdhci_host *host = platform_get_drvdata(pdev);
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001128 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -04001129 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Shawn Guo85d65092011-05-27 23:48:12 +08001130 int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
1131
1132 sdhci_remove_host(host, dead);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +01001133
Sascha Hauer52dac612012-03-07 09:31:34 +01001134 clk_disable_unprepare(imx_data->clk_per);
1135 clk_disable_unprepare(imx_data->clk_ipg);
1136 clk_disable_unprepare(imx_data->clk_ahb);
1137
Shawn Guo85d65092011-05-27 23:48:12 +08001138 sdhci_pltfm_free(pdev);
1139
1140 return 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001141}
1142
Shawn Guo85d65092011-05-27 23:48:12 +08001143static struct platform_driver sdhci_esdhc_imx_driver = {
1144 .driver = {
1145 .name = "sdhci-esdhc-imx",
1146 .owner = THIS_MODULE,
Shawn Guoabfafc22011-06-30 15:44:44 +08001147 .of_match_table = imx_esdhc_dt_ids,
Manuel Lauss29495aa2011-11-03 11:09:45 +01001148 .pm = SDHCI_PLTFM_PMOPS,
Shawn Guo85d65092011-05-27 23:48:12 +08001149 },
Shawn Guo57ed3312011-06-30 09:24:26 +08001150 .id_table = imx_esdhc_devtype,
Shawn Guo85d65092011-05-27 23:48:12 +08001151 .probe = sdhci_esdhc_imx_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05001152 .remove = sdhci_esdhc_imx_remove,
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001153};
Shawn Guo85d65092011-05-27 23:48:12 +08001154
Axel Lind1f81a62011-11-26 12:55:43 +08001155module_platform_driver(sdhci_esdhc_imx_driver);
Shawn Guo85d65092011-05-27 23:48:12 +08001156
1157MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
1158MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
1159MODULE_LICENSE("GPL v2");