blob: d844be862cfa3981577543034236fa1a1599d2a1 [file] [log] [blame]
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001/*
2 * Freescale eSDHC i.MX controller driver for the platform bus.
3 *
4 * derived from the OF-version.
5 *
6 * Copyright (c) 2010 Pengutronix e.K.
7 * Author: Wolfram Sang <w.sang@pengutronix.de>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License.
12 */
13
14#include <linux/io.h>
15#include <linux/delay.h>
16#include <linux/err.h>
17#include <linux/clk.h>
Wolfram Sang0c6d49c2011-02-26 14:44:39 +010018#include <linux/gpio.h>
Shawn Guo66506f72011-08-15 10:28:18 +080019#include <linux/module.h>
Richard Zhue1498602011-03-25 09:18:27 -040020#include <linux/slab.h>
Wolfram Sang95f25ef2010-10-15 12:21:04 +020021#include <linux/mmc/host.h>
Richard Zhu58ac8172011-03-21 13:22:16 +080022#include <linux/mmc/mmc.h>
23#include <linux/mmc/sdio.h>
Shawn Guofbe5fdd2012-12-11 22:32:20 +080024#include <linux/mmc/slot-gpio.h>
Shawn Guoabfafc22011-06-30 15:44:44 +080025#include <linux/of.h>
26#include <linux/of_device.h>
27#include <linux/of_gpio.h>
Dong Aishenge62d8b82012-05-11 14:56:01 +080028#include <linux/pinctrl/consumer.h>
Arnd Bergmann82906b12012-08-24 15:14:29 +020029#include <linux/platform_data/mmc-esdhc-imx.h>
Wolfram Sang95f25ef2010-10-15 12:21:04 +020030#include "sdhci-pltfm.h"
31#include "sdhci-esdhc.h"
32
Shawn Guo60bf6392013-01-15 23:36:53 +080033#define ESDHC_CTRL_D3CD 0x08
Richard Zhu58ac8172011-03-21 13:22:16 +080034/* VENDOR SPEC register */
Shawn Guo60bf6392013-01-15 23:36:53 +080035#define ESDHC_VENDOR_SPEC 0xc0
36#define ESDHC_VENDOR_SPEC_SDIO_QUIRK (1 << 1)
Dong Aisheng03221912013-09-13 19:11:34 +080037#define ESDHC_VENDOR_SPEC_VSELECT (1 << 1)
Dong Aishengfed2f6e2013-09-13 19:11:33 +080038#define ESDHC_VENDOR_SPEC_FRC_SDCLK_ON (1 << 8)
Shawn Guo60bf6392013-01-15 23:36:53 +080039#define ESDHC_WTMK_LVL 0x44
40#define ESDHC_MIX_CTRL 0x48
Shawn Guo2a15f982013-01-21 19:02:26 +080041#define ESDHC_MIX_CTRL_AC23EN (1 << 7)
Dong Aisheng03221912013-09-13 19:11:34 +080042#define ESDHC_MIX_CTRL_EXE_TUNE (1 << 22)
43#define ESDHC_MIX_CTRL_SMPCLK_SEL (1 << 23)
44#define ESDHC_MIX_CTRL_FBCLK_SEL (1 << 25)
Shawn Guo2a15f982013-01-21 19:02:26 +080045/* Bits 3 and 6 are not SDHCI standard definitions */
46#define ESDHC_MIX_CTRL_SDHCI_MASK 0xb7
Richard Zhu58ac8172011-03-21 13:22:16 +080047
Dong Aisheng03221912013-09-13 19:11:34 +080048/* tune control register */
49#define ESDHC_TUNE_CTRL_STATUS 0x68
50#define ESDHC_TUNE_CTRL_STEP 1
51#define ESDHC_TUNE_CTRL_MIN 0
52#define ESDHC_TUNE_CTRL_MAX ((1 << 7) - 1)
53
54#define ESDHC_TUNING_BLOCK_PATTERN_LEN 64
55
Dong Aishengad932202013-09-13 19:11:35 +080056/* pinctrl state */
57#define ESDHC_PINCTRL_STATE_100MHZ "state_100mhz"
58#define ESDHC_PINCTRL_STATE_200MHZ "state_200mhz"
59
Richard Zhu58ac8172011-03-21 13:22:16 +080060/*
Sascha Haueraf510792013-01-21 19:02:28 +080061 * Our interpretation of the SDHCI_HOST_CONTROL register
62 */
63#define ESDHC_CTRL_4BITBUS (0x1 << 1)
64#define ESDHC_CTRL_8BITBUS (0x2 << 1)
65#define ESDHC_CTRL_BUSWIDTH_MASK (0x3 << 1)
66
67/*
Richard Zhu97e4ba62011-08-11 16:51:46 -040068 * There is an INT DMA ERR mis-match between eSDHC and STD SDHC SPEC:
69 * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
70 * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
71 * Define this macro DMA error INT for fsl eSDHC
72 */
Shawn Guo60bf6392013-01-15 23:36:53 +080073#define ESDHC_INT_VENDOR_SPEC_DMA_ERR (1 << 28)
Richard Zhu97e4ba62011-08-11 16:51:46 -040074
75/*
Richard Zhu58ac8172011-03-21 13:22:16 +080076 * The CMDTYPE of the CMD register (offset 0xE) should be set to
77 * "11" when the STOP CMD12 is issued on imx53 to abort one
78 * open ended multi-blk IO. Otherwise the TC INT wouldn't
79 * be generated.
80 * In exact block transfer, the controller doesn't complete the
81 * operations automatically as required at the end of the
82 * transfer and remains on hold if the abort command is not sent.
83 * As a result, the TC flag is not asserted and SW received timeout
84 * exeception. Bit1 of Vendor Spec registor is used to fix it.
85 */
Shawn Guo31fbb302013-10-17 15:19:44 +080086#define ESDHC_FLAG_MULTIBLK_NO_INT BIT(1)
87/*
88 * The flag enables the workaround for ESDHC errata ENGcm07207 which
89 * affects i.MX25 and i.MX35.
90 */
91#define ESDHC_FLAG_ENGCM07207 BIT(2)
Richard Zhue1498602011-03-25 09:18:27 -040092
Shawn Guo57ed3312011-06-30 09:24:26 +080093enum imx_esdhc_type {
94 IMX25_ESDHC,
95 IMX35_ESDHC,
96 IMX51_ESDHC,
97 IMX53_ESDHC,
Shawn Guo95a24822011-09-19 17:32:21 +080098 IMX6Q_USDHC,
Shawn Guo57ed3312011-06-30 09:24:26 +080099};
100
Richard Zhue1498602011-03-25 09:18:27 -0400101struct pltfm_imx_data {
102 int flags;
103 u32 scratchpad;
Shawn Guo57ed3312011-06-30 09:24:26 +0800104 enum imx_esdhc_type devtype;
Dong Aishenge62d8b82012-05-11 14:56:01 +0800105 struct pinctrl *pinctrl;
Dong Aishengad932202013-09-13 19:11:35 +0800106 struct pinctrl_state *pins_default;
107 struct pinctrl_state *pins_100mhz;
108 struct pinctrl_state *pins_200mhz;
Shawn Guo842afc02011-07-06 22:57:48 +0800109 struct esdhc_platform_data boarddata;
Sascha Hauer52dac612012-03-07 09:31:34 +0100110 struct clk *clk_ipg;
111 struct clk *clk_ahb;
112 struct clk *clk_per;
Lucas Stach361b8482013-03-15 09:49:26 +0100113 enum {
114 NO_CMD_PENDING, /* no multiblock command pending*/
115 MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */
116 WAIT_FOR_INT, /* sent CMD12, waiting for response INT */
117 } multiblock_status;
Dong Aisheng03221912013-09-13 19:11:34 +0800118 u32 uhs_mode;
Richard Zhue1498602011-03-25 09:18:27 -0400119};
120
Shawn Guo57ed3312011-06-30 09:24:26 +0800121static struct platform_device_id imx_esdhc_devtype[] = {
122 {
123 .name = "sdhci-esdhc-imx25",
124 .driver_data = IMX25_ESDHC,
125 }, {
126 .name = "sdhci-esdhc-imx35",
127 .driver_data = IMX35_ESDHC,
128 }, {
129 .name = "sdhci-esdhc-imx51",
130 .driver_data = IMX51_ESDHC,
131 }, {
132 .name = "sdhci-esdhc-imx53",
133 .driver_data = IMX53_ESDHC,
134 }, {
Shawn Guo95a24822011-09-19 17:32:21 +0800135 .name = "sdhci-usdhc-imx6q",
136 .driver_data = IMX6Q_USDHC,
137 }, {
Shawn Guo57ed3312011-06-30 09:24:26 +0800138 /* sentinel */
139 }
140};
141MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
142
Shawn Guoabfafc22011-06-30 15:44:44 +0800143static const struct of_device_id imx_esdhc_dt_ids[] = {
144 { .compatible = "fsl,imx25-esdhc", .data = &imx_esdhc_devtype[IMX25_ESDHC], },
145 { .compatible = "fsl,imx35-esdhc", .data = &imx_esdhc_devtype[IMX35_ESDHC], },
146 { .compatible = "fsl,imx51-esdhc", .data = &imx_esdhc_devtype[IMX51_ESDHC], },
147 { .compatible = "fsl,imx53-esdhc", .data = &imx_esdhc_devtype[IMX53_ESDHC], },
Shawn Guo95a24822011-09-19 17:32:21 +0800148 { .compatible = "fsl,imx6q-usdhc", .data = &imx_esdhc_devtype[IMX6Q_USDHC], },
Shawn Guoabfafc22011-06-30 15:44:44 +0800149 { /* sentinel */ }
150};
151MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
152
Shawn Guo57ed3312011-06-30 09:24:26 +0800153static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
154{
155 return data->devtype == IMX25_ESDHC;
156}
157
158static inline int is_imx35_esdhc(struct pltfm_imx_data *data)
159{
160 return data->devtype == IMX35_ESDHC;
161}
162
163static inline int is_imx51_esdhc(struct pltfm_imx_data *data)
164{
165 return data->devtype == IMX51_ESDHC;
166}
167
168static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
169{
170 return data->devtype == IMX53_ESDHC;
171}
172
Shawn Guo95a24822011-09-19 17:32:21 +0800173static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
174{
175 return data->devtype == IMX6Q_USDHC;
176}
177
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200178static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
179{
180 void __iomem *base = host->ioaddr + (reg & ~0x3);
181 u32 shift = (reg & 0x3) * 8;
182
183 writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
184}
185
Wolfram Sang7e29c302011-02-26 14:44:41 +0100186static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
187{
Lucas Stach361b8482013-03-15 09:49:26 +0100188 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
189 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100190 u32 val = readl(host->ioaddr + reg);
191
Dong Aisheng03221912013-09-13 19:11:34 +0800192 if (unlikely(reg == SDHCI_PRESENT_STATE)) {
193 u32 fsl_prss = val;
194 /* save the least 20 bits */
195 val = fsl_prss & 0x000FFFFF;
196 /* move dat[0-3] bits */
197 val |= (fsl_prss & 0x0F000000) >> 4;
198 /* move cmd line bit */
199 val |= (fsl_prss & 0x00800000) << 1;
200 }
201
Richard Zhu97e4ba62011-08-11 16:51:46 -0400202 if (unlikely(reg == SDHCI_CAPABILITIES)) {
203 /* In FSL esdhc IC module, only bit20 is used to indicate the
204 * ADMA2 capability of esdhc, but this bit is messed up on
205 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
206 * don't actually support ADMA2). So set the BROKEN_ADMA
207 * uirk on MX25/35 platforms.
208 */
209
210 if (val & SDHCI_CAN_DO_ADMA1) {
211 val &= ~SDHCI_CAN_DO_ADMA1;
212 val |= SDHCI_CAN_DO_ADMA2;
213 }
214 }
215
Dong Aisheng03221912013-09-13 19:11:34 +0800216 if (unlikely(reg == SDHCI_CAPABILITIES_1) && is_imx6q_usdhc(imx_data))
217 val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104
218 | SDHCI_SUPPORT_SDR50;
219
220 if (unlikely(reg == SDHCI_MAX_CURRENT) && is_imx6q_usdhc(imx_data)) {
221 val = 0;
222 val |= 0xFF << SDHCI_MAX_CURRENT_330_SHIFT;
223 val |= 0xFF << SDHCI_MAX_CURRENT_300_SHIFT;
224 val |= 0xFF << SDHCI_MAX_CURRENT_180_SHIFT;
225 }
226
Richard Zhu97e4ba62011-08-11 16:51:46 -0400227 if (unlikely(reg == SDHCI_INT_STATUS)) {
Shawn Guo60bf6392013-01-15 23:36:53 +0800228 if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
229 val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
Richard Zhu97e4ba62011-08-11 16:51:46 -0400230 val |= SDHCI_INT_ADMA_ERROR;
231 }
Lucas Stach361b8482013-03-15 09:49:26 +0100232
233 /*
234 * mask off the interrupt we get in response to the manually
235 * sent CMD12
236 */
237 if ((imx_data->multiblock_status == WAIT_FOR_INT) &&
238 ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) {
239 val &= ~SDHCI_INT_RESPONSE;
240 writel(SDHCI_INT_RESPONSE, host->ioaddr +
241 SDHCI_INT_STATUS);
242 imx_data->multiblock_status = NO_CMD_PENDING;
243 }
Richard Zhu97e4ba62011-08-11 16:51:46 -0400244 }
245
Wolfram Sang7e29c302011-02-26 14:44:41 +0100246 return val;
247}
248
249static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
250{
Richard Zhue1498602011-03-25 09:18:27 -0400251 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
252 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Tony Lin0d588642011-08-11 16:45:59 -0400253 u32 data;
Richard Zhue1498602011-03-25 09:18:27 -0400254
Tony Lin0d588642011-08-11 16:45:59 -0400255 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
Tony Lin0d588642011-08-11 16:45:59 -0400256 if (val & SDHCI_INT_CARD_INT) {
257 /*
258 * Clear and then set D3CD bit to avoid missing the
259 * card interrupt. This is a eSDHC controller problem
260 * so we need to apply the following workaround: clear
261 * and set D3CD bit will make eSDHC re-sample the card
262 * interrupt. In case a card interrupt was lost,
263 * re-sample it by the following steps.
264 */
265 data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
Shawn Guo60bf6392013-01-15 23:36:53 +0800266 data &= ~ESDHC_CTRL_D3CD;
Tony Lin0d588642011-08-11 16:45:59 -0400267 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
Shawn Guo60bf6392013-01-15 23:36:53 +0800268 data |= ESDHC_CTRL_D3CD;
Tony Lin0d588642011-08-11 16:45:59 -0400269 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
270 }
271 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100272
Richard Zhu58ac8172011-03-21 13:22:16 +0800273 if (unlikely((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
274 && (reg == SDHCI_INT_STATUS)
275 && (val & SDHCI_INT_DATA_END))) {
276 u32 v;
Shawn Guo60bf6392013-01-15 23:36:53 +0800277 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
278 v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
279 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
Lucas Stach361b8482013-03-15 09:49:26 +0100280
281 if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS)
282 {
283 /* send a manual CMD12 with RESPTYP=none */
284 data = MMC_STOP_TRANSMISSION << 24 |
285 SDHCI_CMD_ABORTCMD << 16;
286 writel(data, host->ioaddr + SDHCI_TRANSFER_MODE);
287 imx_data->multiblock_status = WAIT_FOR_INT;
288 }
Richard Zhu58ac8172011-03-21 13:22:16 +0800289 }
290
Richard Zhu97e4ba62011-08-11 16:51:46 -0400291 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
292 if (val & SDHCI_INT_ADMA_ERROR) {
293 val &= ~SDHCI_INT_ADMA_ERROR;
Shawn Guo60bf6392013-01-15 23:36:53 +0800294 val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
Richard Zhu97e4ba62011-08-11 16:51:46 -0400295 }
296 }
297
Wolfram Sang7e29c302011-02-26 14:44:41 +0100298 writel(val, host->ioaddr + reg);
299}
300
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200301static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
302{
Shawn Guoef4d0882013-01-15 23:30:27 +0800303 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
304 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aisheng03221912013-09-13 19:11:34 +0800305 u16 ret = 0;
306 u32 val;
Shawn Guoef4d0882013-01-15 23:30:27 +0800307
Shawn Guo95a24822011-09-19 17:32:21 +0800308 if (unlikely(reg == SDHCI_HOST_VERSION)) {
Shawn Guoef4d0882013-01-15 23:30:27 +0800309 reg ^= 2;
310 if (is_imx6q_usdhc(imx_data)) {
311 /*
312 * The usdhc register returns a wrong host version.
313 * Correct it here.
314 */
315 return SDHCI_SPEC_300;
316 }
Shawn Guo95a24822011-09-19 17:32:21 +0800317 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200318
Dong Aisheng03221912013-09-13 19:11:34 +0800319 if (unlikely(reg == SDHCI_HOST_CONTROL2)) {
320 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
321 if (val & ESDHC_VENDOR_SPEC_VSELECT)
322 ret |= SDHCI_CTRL_VDD_180;
323
324 if (is_imx6q_usdhc(imx_data)) {
325 val = readl(host->ioaddr + ESDHC_MIX_CTRL);
326 if (val & ESDHC_MIX_CTRL_EXE_TUNE)
327 ret |= SDHCI_CTRL_EXEC_TUNING;
328 if (val & ESDHC_MIX_CTRL_SMPCLK_SEL)
329 ret |= SDHCI_CTRL_TUNED_CLK;
330 }
331
332 ret |= (imx_data->uhs_mode & SDHCI_CTRL_UHS_MASK);
333 ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
334
335 return ret;
336 }
337
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200338 return readw(host->ioaddr + reg);
339}
340
341static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
342{
343 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400344 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aisheng03221912013-09-13 19:11:34 +0800345 u32 new_val = 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200346
347 switch (reg) {
Dong Aisheng03221912013-09-13 19:11:34 +0800348 case SDHCI_CLOCK_CONTROL:
349 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
350 if (val & SDHCI_CLOCK_CARD_EN)
351 new_val |= ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
352 else
353 new_val &= ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
354 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
355 return;
356 case SDHCI_HOST_CONTROL2:
357 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
358 if (val & SDHCI_CTRL_VDD_180)
359 new_val |= ESDHC_VENDOR_SPEC_VSELECT;
360 else
361 new_val &= ~ESDHC_VENDOR_SPEC_VSELECT;
362 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
363 imx_data->uhs_mode = val & SDHCI_CTRL_UHS_MASK;
364 new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
365 if (val & SDHCI_CTRL_TUNED_CLK)
366 new_val |= ESDHC_MIX_CTRL_SMPCLK_SEL;
367 else
368 new_val &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
369 writel(new_val , host->ioaddr + ESDHC_MIX_CTRL);
370 return;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200371 case SDHCI_TRANSFER_MODE:
Richard Zhu58ac8172011-03-21 13:22:16 +0800372 if ((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
373 && (host->cmd->opcode == SD_IO_RW_EXTENDED)
374 && (host->cmd->data->blocks > 1)
375 && (host->cmd->data->flags & MMC_DATA_READ)) {
376 u32 v;
Shawn Guo60bf6392013-01-15 23:36:53 +0800377 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
378 v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
379 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
Richard Zhu58ac8172011-03-21 13:22:16 +0800380 }
Shawn Guo69f54692013-01-21 19:02:24 +0800381
382 if (is_imx6q_usdhc(imx_data)) {
383 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
Shawn Guo2a15f982013-01-21 19:02:26 +0800384 /* Swap AC23 bit */
385 if (val & SDHCI_TRNS_AUTO_CMD23) {
386 val &= ~SDHCI_TRNS_AUTO_CMD23;
387 val |= ESDHC_MIX_CTRL_AC23EN;
388 }
389 m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
Shawn Guo69f54692013-01-21 19:02:24 +0800390 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
391 } else {
392 /*
393 * Postpone this write, we must do it together with a
394 * command write that is down below.
395 */
396 imx_data->scratchpad = val;
397 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200398 return;
399 case SDHCI_COMMAND:
Lucas Stach361b8482013-03-15 09:49:26 +0100400 if (host->cmd->opcode == MMC_STOP_TRANSMISSION)
Richard Zhu58ac8172011-03-21 13:22:16 +0800401 val |= SDHCI_CMD_ABORTCMD;
Shawn Guo95a24822011-09-19 17:32:21 +0800402
Lucas Stach361b8482013-03-15 09:49:26 +0100403 if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
404 (imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
405 imx_data->multiblock_status = MULTIBLK_IN_PROCESS;
406
Shawn Guo69f54692013-01-21 19:02:24 +0800407 if (is_imx6q_usdhc(imx_data))
Shawn Guo95a24822011-09-19 17:32:21 +0800408 writel(val << 16,
409 host->ioaddr + SDHCI_TRANSFER_MODE);
Shawn Guo69f54692013-01-21 19:02:24 +0800410 else
Shawn Guo95a24822011-09-19 17:32:21 +0800411 writel(val << 16 | imx_data->scratchpad,
412 host->ioaddr + SDHCI_TRANSFER_MODE);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200413 return;
414 case SDHCI_BLOCK_SIZE:
415 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
416 break;
417 }
418 esdhc_clrset_le(host, 0xffff, val, reg);
419}
420
421static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
422{
Wilson Callan9a0985b2012-07-19 02:49:16 -0400423 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
424 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200425 u32 new_val;
Sascha Haueraf510792013-01-21 19:02:28 +0800426 u32 mask;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200427
428 switch (reg) {
429 case SDHCI_POWER_CONTROL:
430 /*
431 * FSL put some DMA bits here
432 * If your board has a regulator, code should be here
433 */
434 return;
435 case SDHCI_HOST_CONTROL:
Shawn Guo6b40d182013-01-15 23:36:52 +0800436 /* FSL messed up here, so we need to manually compose it. */
Sascha Haueraf510792013-01-21 19:02:28 +0800437 new_val = val & SDHCI_CTRL_LED;
Masanari Iida7122bbb2012-08-05 23:25:40 +0900438 /* ensure the endianness */
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200439 new_val |= ESDHC_HOST_CONTROL_LE;
Wilson Callan9a0985b2012-07-19 02:49:16 -0400440 /* bits 8&9 are reserved on mx25 */
441 if (!is_imx25_esdhc(imx_data)) {
442 /* DMA mode bits are shifted */
443 new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
444 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200445
Sascha Haueraf510792013-01-21 19:02:28 +0800446 /*
447 * Do not touch buswidth bits here. This is done in
448 * esdhc_pltfm_bus_width.
Martin Fuzzeyf6825742013-04-15 17:08:35 +0200449 * Do not touch the D3CD bit either which is used for the
450 * SDIO interrupt errata workaround.
Sascha Haueraf510792013-01-21 19:02:28 +0800451 */
Martin Fuzzeyf6825742013-04-15 17:08:35 +0200452 mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD);
Sascha Haueraf510792013-01-21 19:02:28 +0800453
454 esdhc_clrset_le(host, mask, new_val, reg);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200455 return;
456 }
457 esdhc_clrset_le(host, 0xff, val, reg);
Shawn Guo913413c2011-06-21 22:41:51 +0800458
459 /*
460 * The esdhc has a design violation to SDHC spec which tells
461 * that software reset should not affect card detection circuit.
462 * But esdhc clears its SYSCTL register bits [0..2] during the
463 * software reset. This will stop those clocks that card detection
464 * circuit relies on. To work around it, we turn the clocks on back
465 * to keep card detection circuit functional.
466 */
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800467 if ((reg == SDHCI_SOFTWARE_RESET) && (val & 1)) {
Shawn Guo913413c2011-06-21 22:41:51 +0800468 esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800469 /*
470 * The reset on usdhc fails to clear MIX_CTRL register.
471 * Do it manually here.
472 */
473 if (is_imx6q_usdhc(imx_data))
474 writel(0, host->ioaddr + ESDHC_MIX_CTRL);
475 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200476}
477
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200478static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
479{
480 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
481 struct pltfm_imx_data *imx_data = pltfm_host->priv;
482 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
483
484 u32 f_host = clk_get_rate(pltfm_host->clk);
485
486 if (boarddata->f_max && (boarddata->f_max < f_host))
487 return boarddata->f_max;
488 else
489 return f_host;
490}
491
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200492static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
493{
494 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
495
496 return clk_get_rate(pltfm_host->clk) / 256 / 16;
497}
498
Lucas Stach8ba95802013-06-05 15:13:25 +0200499static inline void esdhc_pltfm_set_clock(struct sdhci_host *host,
500 unsigned int clock)
501{
502 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800503 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aishengd31fc002013-09-13 19:11:32 +0800504 unsigned int host_clock = clk_get_rate(pltfm_host->clk);
505 int pre_div = 2;
506 int div = 1;
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800507 u32 temp, val;
Lucas Stach8ba95802013-06-05 15:13:25 +0200508
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800509 if (clock == 0) {
510 if (is_imx6q_usdhc(imx_data)) {
511 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
512 writel(val & ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
513 host->ioaddr + ESDHC_VENDOR_SPEC);
514 }
Dong Aishengd31fc002013-09-13 19:11:32 +0800515 goto out;
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800516 }
Dong Aishengd31fc002013-09-13 19:11:32 +0800517
Dong Aisheng5f7886c2013-09-13 19:11:36 +0800518 if (is_imx6q_usdhc(imx_data))
519 pre_div = 1;
520
Dong Aishengd31fc002013-09-13 19:11:32 +0800521 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
522 temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
523 | ESDHC_CLOCK_MASK);
524 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
525
526 while (host_clock / pre_div / 16 > clock && pre_div < 256)
527 pre_div *= 2;
528
529 while (host_clock / pre_div / div > clock && div < 16)
530 div++;
531
Dong Aishenge76b8552013-09-13 19:11:37 +0800532 host->mmc->actual_clock = host_clock / pre_div / div;
Dong Aishengd31fc002013-09-13 19:11:32 +0800533 dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
Dong Aishenge76b8552013-09-13 19:11:37 +0800534 clock, host->mmc->actual_clock);
Dong Aishengd31fc002013-09-13 19:11:32 +0800535
536 pre_div >>= 1;
537 div--;
538
539 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
540 temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
541 | (div << ESDHC_DIVIDER_SHIFT)
542 | (pre_div << ESDHC_PREDIV_SHIFT));
543 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800544
545 if (is_imx6q_usdhc(imx_data)) {
546 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
547 writel(val | ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
548 host->ioaddr + ESDHC_VENDOR_SPEC);
549 }
550
Dong Aishengd31fc002013-09-13 19:11:32 +0800551 mdelay(1);
552out:
553 host->clock = clock;
Lucas Stach8ba95802013-06-05 15:13:25 +0200554}
555
Shawn Guo913413c2011-06-21 22:41:51 +0800556static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
557{
Shawn Guo842afc02011-07-06 22:57:48 +0800558 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
559 struct pltfm_imx_data *imx_data = pltfm_host->priv;
560 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Shawn Guo913413c2011-06-21 22:41:51 +0800561
562 switch (boarddata->wp_type) {
563 case ESDHC_WP_GPIO:
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800564 return mmc_gpio_get_ro(host->mmc);
Shawn Guo913413c2011-06-21 22:41:51 +0800565 case ESDHC_WP_CONTROLLER:
566 return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
567 SDHCI_WRITE_PROTECT);
568 case ESDHC_WP_NONE:
569 break;
570 }
571
572 return -ENOSYS;
573}
574
Sascha Haueraf510792013-01-21 19:02:28 +0800575static int esdhc_pltfm_bus_width(struct sdhci_host *host, int width)
576{
577 u32 ctrl;
578
579 switch (width) {
580 case MMC_BUS_WIDTH_8:
581 ctrl = ESDHC_CTRL_8BITBUS;
582 break;
583 case MMC_BUS_WIDTH_4:
584 ctrl = ESDHC_CTRL_4BITBUS;
585 break;
586 default:
587 ctrl = 0;
588 break;
589 }
590
591 esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
592 SDHCI_HOST_CONTROL);
593
594 return 0;
595}
596
Dong Aisheng03221912013-09-13 19:11:34 +0800597static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val)
598{
599 u32 reg;
600
601 /* FIXME: delay a bit for card to be ready for next tuning due to errors */
602 mdelay(1);
603
604 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
605 reg |= ESDHC_MIX_CTRL_EXE_TUNE | ESDHC_MIX_CTRL_SMPCLK_SEL |
606 ESDHC_MIX_CTRL_FBCLK_SEL;
607 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
608 writel(val << 8, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
609 dev_dbg(mmc_dev(host->mmc),
610 "tunning with delay 0x%x ESDHC_TUNE_CTRL_STATUS 0x%x\n",
611 val, readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS));
612}
613
614static void esdhc_request_done(struct mmc_request *mrq)
615{
616 complete(&mrq->completion);
617}
618
619static int esdhc_send_tuning_cmd(struct sdhci_host *host, u32 opcode)
620{
621 struct mmc_command cmd = {0};
622 struct mmc_request mrq = {0};
623 struct mmc_data data = {0};
624 struct scatterlist sg;
625 char tuning_pattern[ESDHC_TUNING_BLOCK_PATTERN_LEN];
626
627 cmd.opcode = opcode;
628 cmd.arg = 0;
629 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
630
631 data.blksz = ESDHC_TUNING_BLOCK_PATTERN_LEN;
632 data.blocks = 1;
633 data.flags = MMC_DATA_READ;
634 data.sg = &sg;
635 data.sg_len = 1;
636
637 sg_init_one(&sg, tuning_pattern, sizeof(tuning_pattern));
638
639 mrq.cmd = &cmd;
640 mrq.cmd->mrq = &mrq;
641 mrq.data = &data;
642 mrq.data->mrq = &mrq;
643 mrq.cmd->data = mrq.data;
644
645 mrq.done = esdhc_request_done;
646 init_completion(&(mrq.completion));
647
648 disable_irq(host->irq);
649 spin_lock(&host->lock);
650 host->mrq = &mrq;
651
652 sdhci_send_command(host, mrq.cmd);
653
654 spin_unlock(&host->lock);
655 enable_irq(host->irq);
656
657 wait_for_completion(&mrq.completion);
658
659 if (cmd.error)
660 return cmd.error;
661 if (data.error)
662 return data.error;
663
664 return 0;
665}
666
667static void esdhc_post_tuning(struct sdhci_host *host)
668{
669 u32 reg;
670
671 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
672 reg &= ~ESDHC_MIX_CTRL_EXE_TUNE;
673 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
674}
675
676static int esdhc_executing_tuning(struct sdhci_host *host, u32 opcode)
677{
678 int min, max, avg, ret;
679
680 /* find the mininum delay first which can pass tuning */
681 min = ESDHC_TUNE_CTRL_MIN;
682 while (min < ESDHC_TUNE_CTRL_MAX) {
683 esdhc_prepare_tuning(host, min);
684 if (!esdhc_send_tuning_cmd(host, opcode))
685 break;
686 min += ESDHC_TUNE_CTRL_STEP;
687 }
688
689 /* find the maxinum delay which can not pass tuning */
690 max = min + ESDHC_TUNE_CTRL_STEP;
691 while (max < ESDHC_TUNE_CTRL_MAX) {
692 esdhc_prepare_tuning(host, max);
693 if (esdhc_send_tuning_cmd(host, opcode)) {
694 max -= ESDHC_TUNE_CTRL_STEP;
695 break;
696 }
697 max += ESDHC_TUNE_CTRL_STEP;
698 }
699
700 /* use average delay to get the best timing */
701 avg = (min + max) / 2;
702 esdhc_prepare_tuning(host, avg);
703 ret = esdhc_send_tuning_cmd(host, opcode);
704 esdhc_post_tuning(host);
705
706 dev_dbg(mmc_dev(host->mmc), "tunning %s at 0x%x ret %d\n",
707 ret ? "failed" : "passed", avg, ret);
708
709 return ret;
710}
711
Dong Aishengad932202013-09-13 19:11:35 +0800712static int esdhc_change_pinstate(struct sdhci_host *host,
713 unsigned int uhs)
714{
715 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
716 struct pltfm_imx_data *imx_data = pltfm_host->priv;
717 struct pinctrl_state *pinctrl;
718
719 dev_dbg(mmc_dev(host->mmc), "change pinctrl state for uhs %d\n", uhs);
720
721 if (IS_ERR(imx_data->pinctrl) ||
722 IS_ERR(imx_data->pins_default) ||
723 IS_ERR(imx_data->pins_100mhz) ||
724 IS_ERR(imx_data->pins_200mhz))
725 return -EINVAL;
726
727 switch (uhs) {
728 case MMC_TIMING_UHS_SDR50:
729 pinctrl = imx_data->pins_100mhz;
730 break;
731 case MMC_TIMING_UHS_SDR104:
732 pinctrl = imx_data->pins_200mhz;
733 break;
734 default:
735 /* back to default state for other legacy timing */
736 pinctrl = imx_data->pins_default;
737 }
738
739 return pinctrl_select_state(imx_data->pinctrl, pinctrl);
740}
741
742static int esdhc_set_uhs_signaling(struct sdhci_host *host, unsigned int uhs)
743{
744 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
745 struct pltfm_imx_data *imx_data = pltfm_host->priv;
746
747 switch (uhs) {
748 case MMC_TIMING_UHS_SDR12:
749 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR12;
750 break;
751 case MMC_TIMING_UHS_SDR25:
752 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR25;
753 break;
754 case MMC_TIMING_UHS_SDR50:
755 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR50;
756 break;
757 case MMC_TIMING_UHS_SDR104:
758 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR104;
759 break;
760 case MMC_TIMING_UHS_DDR50:
761 imx_data->uhs_mode = SDHCI_CTRL_UHS_DDR50;
762 break;
763 }
764
765 return esdhc_change_pinstate(host, uhs);
766}
767
Lars-Peter Clausenc9155682013-03-13 19:26:05 +0100768static const struct sdhci_ops sdhci_esdhc_ops = {
Richard Zhue1498602011-03-25 09:18:27 -0400769 .read_l = esdhc_readl_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100770 .read_w = esdhc_readw_le,
Richard Zhue1498602011-03-25 09:18:27 -0400771 .write_l = esdhc_writel_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100772 .write_w = esdhc_writew_le,
773 .write_b = esdhc_writeb_le,
Lucas Stach8ba95802013-06-05 15:13:25 +0200774 .set_clock = esdhc_pltfm_set_clock,
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200775 .get_max_clock = esdhc_pltfm_get_max_clock,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100776 .get_min_clock = esdhc_pltfm_get_min_clock,
Shawn Guo913413c2011-06-21 22:41:51 +0800777 .get_ro = esdhc_pltfm_get_ro,
Sascha Haueraf510792013-01-21 19:02:28 +0800778 .platform_bus_width = esdhc_pltfm_bus_width,
Dong Aishengad932202013-09-13 19:11:35 +0800779 .set_uhs_signaling = esdhc_set_uhs_signaling,
Dong Aisheng03221912013-09-13 19:11:34 +0800780 .platform_execute_tuning = esdhc_executing_tuning,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100781};
782
Lars-Peter Clausen1db5eeb2013-03-13 19:26:03 +0100783static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
Richard Zhu97e4ba62011-08-11 16:51:46 -0400784 .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
785 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
786 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
Shawn Guo85d65092011-05-27 23:48:12 +0800787 | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
Shawn Guo85d65092011-05-27 23:48:12 +0800788 .ops = &sdhci_esdhc_ops,
789};
790
Shawn Guoabfafc22011-06-30 15:44:44 +0800791#ifdef CONFIG_OF
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500792static int
Shawn Guoabfafc22011-06-30 15:44:44 +0800793sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
794 struct esdhc_platform_data *boarddata)
795{
796 struct device_node *np = pdev->dev.of_node;
797
798 if (!np)
799 return -ENODEV;
800
Arnd Bergmann7f217792012-05-13 00:14:24 -0400801 if (of_get_property(np, "non-removable", NULL))
Shawn Guoabfafc22011-06-30 15:44:44 +0800802 boarddata->cd_type = ESDHC_CD_PERMANENT;
803
804 if (of_get_property(np, "fsl,cd-controller", NULL))
805 boarddata->cd_type = ESDHC_CD_CONTROLLER;
806
807 if (of_get_property(np, "fsl,wp-controller", NULL))
808 boarddata->wp_type = ESDHC_WP_CONTROLLER;
809
810 boarddata->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
811 if (gpio_is_valid(boarddata->cd_gpio))
812 boarddata->cd_type = ESDHC_CD_GPIO;
813
814 boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
815 if (gpio_is_valid(boarddata->wp_gpio))
816 boarddata->wp_type = ESDHC_WP_GPIO;
817
Sascha Haueraf510792013-01-21 19:02:28 +0800818 of_property_read_u32(np, "bus-width", &boarddata->max_bus_width);
819
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200820 of_property_read_u32(np, "max-frequency", &boarddata->f_max);
821
Dong Aishengad932202013-09-13 19:11:35 +0800822 if (of_find_property(np, "no-1-8-v", NULL))
823 boarddata->support_vsel = false;
824 else
825 boarddata->support_vsel = true;
826
Shawn Guoabfafc22011-06-30 15:44:44 +0800827 return 0;
828}
829#else
830static inline int
831sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
832 struct esdhc_platform_data *boarddata)
833{
834 return -ENODEV;
835}
836#endif
837
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500838static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200839{
Shawn Guoabfafc22011-06-30 15:44:44 +0800840 const struct of_device_id *of_id =
841 of_match_device(imx_esdhc_dt_ids, &pdev->dev);
Shawn Guo85d65092011-05-27 23:48:12 +0800842 struct sdhci_pltfm_host *pltfm_host;
843 struct sdhci_host *host;
844 struct esdhc_platform_data *boarddata;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100845 int err;
Richard Zhue1498602011-03-25 09:18:27 -0400846 struct pltfm_imx_data *imx_data;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200847
Christian Daudt0e748232013-05-29 13:50:05 -0700848 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata, 0);
Shawn Guo85d65092011-05-27 23:48:12 +0800849 if (IS_ERR(host))
850 return PTR_ERR(host);
851
852 pltfm_host = sdhci_priv(host);
853
Shawn Guoe3af31c2012-11-26 14:39:43 +0800854 imx_data = devm_kzalloc(&pdev->dev, sizeof(*imx_data), GFP_KERNEL);
Shawn Guoabfafc22011-06-30 15:44:44 +0800855 if (!imx_data) {
856 err = -ENOMEM;
Shawn Guoe3af31c2012-11-26 14:39:43 +0800857 goto free_sdhci;
Shawn Guoabfafc22011-06-30 15:44:44 +0800858 }
Shawn Guo57ed3312011-06-30 09:24:26 +0800859
Shawn Guoabfafc22011-06-30 15:44:44 +0800860 if (of_id)
861 pdev->id_entry = of_id->data;
Shawn Guo57ed3312011-06-30 09:24:26 +0800862 imx_data->devtype = pdev->id_entry->driver_data;
Shawn Guo85d65092011-05-27 23:48:12 +0800863 pltfm_host->priv = imx_data;
864
Shawn Guo31fbb302013-10-17 15:19:44 +0800865 if (is_imx25_esdhc(imx_data) || is_imx35_esdhc(imx_data))
866 imx_data->flags |= ESDHC_FLAG_ENGCM07207;
867
Sascha Hauer52dac612012-03-07 09:31:34 +0100868 imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
869 if (IS_ERR(imx_data->clk_ipg)) {
870 err = PTR_ERR(imx_data->clk_ipg);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800871 goto free_sdhci;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200872 }
Sascha Hauer52dac612012-03-07 09:31:34 +0100873
874 imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
875 if (IS_ERR(imx_data->clk_ahb)) {
876 err = PTR_ERR(imx_data->clk_ahb);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800877 goto free_sdhci;
Sascha Hauer52dac612012-03-07 09:31:34 +0100878 }
879
880 imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
881 if (IS_ERR(imx_data->clk_per)) {
882 err = PTR_ERR(imx_data->clk_per);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800883 goto free_sdhci;
Sascha Hauer52dac612012-03-07 09:31:34 +0100884 }
885
886 pltfm_host->clk = imx_data->clk_per;
887
888 clk_prepare_enable(imx_data->clk_per);
889 clk_prepare_enable(imx_data->clk_ipg);
890 clk_prepare_enable(imx_data->clk_ahb);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200891
Dong Aishengad932202013-09-13 19:11:35 +0800892 imx_data->pinctrl = devm_pinctrl_get(&pdev->dev);
Dong Aishenge62d8b82012-05-11 14:56:01 +0800893 if (IS_ERR(imx_data->pinctrl)) {
894 err = PTR_ERR(imx_data->pinctrl);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800895 goto disable_clk;
Dong Aishenge62d8b82012-05-11 14:56:01 +0800896 }
897
Dong Aishengad932202013-09-13 19:11:35 +0800898 imx_data->pins_default = pinctrl_lookup_state(imx_data->pinctrl,
899 PINCTRL_STATE_DEFAULT);
900 if (IS_ERR(imx_data->pins_default)) {
901 err = PTR_ERR(imx_data->pins_default);
902 dev_err(mmc_dev(host->mmc), "could not get default state\n");
903 goto disable_clk;
904 }
905
Eric BĂ©nardb89152822012-04-18 02:30:20 +0200906 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
Eric BĂ©nard37865fe2010-10-23 01:57:21 +0200907
Shawn Guo31fbb302013-10-17 15:19:44 +0800908 if (imx_data->flags & ESDHC_FLAG_ENGCM07207)
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100909 /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
Richard Zhu97e4ba62011-08-11 16:51:46 -0400910 host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK
911 | SDHCI_QUIRK_BROKEN_ADMA;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100912
Shawn Guo57ed3312011-06-30 09:24:26 +0800913 if (is_imx53_esdhc(imx_data))
Richard Zhu58ac8172011-03-21 13:22:16 +0800914 imx_data->flags |= ESDHC_FLAG_MULTIBLK_NO_INT;
915
Shawn Guof750ba92011-11-10 16:39:32 +0800916 /*
917 * The imx6q ROM code will change the default watermark level setting
918 * to something insane. Change it back here.
919 */
920 if (is_imx6q_usdhc(imx_data))
Shawn Guo60bf6392013-01-15 23:36:53 +0800921 writel(0x08100810, host->ioaddr + ESDHC_WTMK_LVL);
Shawn Guof750ba92011-11-10 16:39:32 +0800922
Shawn Guo842afc02011-07-06 22:57:48 +0800923 boarddata = &imx_data->boarddata;
Shawn Guoabfafc22011-06-30 15:44:44 +0800924 if (sdhci_esdhc_imx_probe_dt(pdev, boarddata) < 0) {
925 if (!host->mmc->parent->platform_data) {
926 dev_err(mmc_dev(host->mmc), "no board data!\n");
927 err = -EINVAL;
Shawn Guoe3af31c2012-11-26 14:39:43 +0800928 goto disable_clk;
Shawn Guoabfafc22011-06-30 15:44:44 +0800929 }
930 imx_data->boarddata = *((struct esdhc_platform_data *)
931 host->mmc->parent->platform_data);
932 }
Shawn Guo913413c2011-06-21 22:41:51 +0800933
934 /* write_protect */
935 if (boarddata->wp_type == ESDHC_WP_GPIO) {
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800936 err = mmc_gpio_request_ro(host->mmc, boarddata->wp_gpio);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100937 if (err) {
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800938 dev_err(mmc_dev(host->mmc),
939 "failed to request write-protect gpio!\n");
940 goto disable_clk;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100941 }
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800942 host->mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
Shawn Guo913413c2011-06-21 22:41:51 +0800943 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100944
Shawn Guo913413c2011-06-21 22:41:51 +0800945 /* card_detect */
Shawn Guo913413c2011-06-21 22:41:51 +0800946 switch (boarddata->cd_type) {
947 case ESDHC_CD_GPIO:
Laurent Pinchart214fc302013-08-08 12:38:31 +0200948 err = mmc_gpio_request_cd(host->mmc, boarddata->cd_gpio, 0);
Wolfram Sang7e29c302011-02-26 14:44:41 +0100949 if (err) {
Shawn Guo913413c2011-06-21 22:41:51 +0800950 dev_err(mmc_dev(host->mmc),
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800951 "failed to request card-detect gpio!\n");
Shawn Guoe3af31c2012-11-26 14:39:43 +0800952 goto disable_clk;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100953 }
Shawn Guo913413c2011-06-21 22:41:51 +0800954 /* fall through */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100955
Shawn Guo913413c2011-06-21 22:41:51 +0800956 case ESDHC_CD_CONTROLLER:
957 /* we have a working card_detect back */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100958 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
Shawn Guo913413c2011-06-21 22:41:51 +0800959 break;
960
961 case ESDHC_CD_PERMANENT:
962 host->mmc->caps = MMC_CAP_NONREMOVABLE;
963 break;
964
965 case ESDHC_CD_NONE:
966 break;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100967 }
Eric BĂ©nard16a790b2010-10-23 01:57:22 +0200968
Sascha Haueraf510792013-01-21 19:02:28 +0800969 switch (boarddata->max_bus_width) {
970 case 8:
971 host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA;
972 break;
973 case 4:
974 host->mmc->caps |= MMC_CAP_4_BIT_DATA;
975 break;
976 case 1:
977 default:
978 host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
979 break;
980 }
981
Dong Aishengad932202013-09-13 19:11:35 +0800982 /* sdr50 and sdr104 needs work on 1.8v signal voltage */
983 if ((boarddata->support_vsel) && is_imx6q_usdhc(imx_data)) {
984 imx_data->pins_100mhz = pinctrl_lookup_state(imx_data->pinctrl,
985 ESDHC_PINCTRL_STATE_100MHZ);
986 imx_data->pins_200mhz = pinctrl_lookup_state(imx_data->pinctrl,
987 ESDHC_PINCTRL_STATE_200MHZ);
988 if (IS_ERR(imx_data->pins_100mhz) ||
989 IS_ERR(imx_data->pins_200mhz)) {
990 dev_warn(mmc_dev(host->mmc),
991 "could not get ultra high speed state, work on normal mode\n");
992 /* fall back to not support uhs by specify no 1.8v quirk */
993 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
994 }
995 } else {
996 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
997 }
998
Shawn Guo85d65092011-05-27 23:48:12 +0800999 err = sdhci_add_host(host);
1000 if (err)
Shawn Guoe3af31c2012-11-26 14:39:43 +08001001 goto disable_clk;
Shawn Guo85d65092011-05-27 23:48:12 +08001002
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001003 return 0;
Wolfram Sang7e29c302011-02-26 14:44:41 +01001004
Shawn Guoe3af31c2012-11-26 14:39:43 +08001005disable_clk:
Sascha Hauer52dac612012-03-07 09:31:34 +01001006 clk_disable_unprepare(imx_data->clk_per);
1007 clk_disable_unprepare(imx_data->clk_ipg);
1008 clk_disable_unprepare(imx_data->clk_ahb);
Shawn Guoe3af31c2012-11-26 14:39:43 +08001009free_sdhci:
Shawn Guo85d65092011-05-27 23:48:12 +08001010 sdhci_pltfm_free(pdev);
1011 return err;
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001012}
1013
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001014static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001015{
Shawn Guo85d65092011-05-27 23:48:12 +08001016 struct sdhci_host *host = platform_get_drvdata(pdev);
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001017 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -04001018 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Shawn Guo85d65092011-05-27 23:48:12 +08001019 int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
1020
1021 sdhci_remove_host(host, dead);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +01001022
Sascha Hauer52dac612012-03-07 09:31:34 +01001023 clk_disable_unprepare(imx_data->clk_per);
1024 clk_disable_unprepare(imx_data->clk_ipg);
1025 clk_disable_unprepare(imx_data->clk_ahb);
1026
Shawn Guo85d65092011-05-27 23:48:12 +08001027 sdhci_pltfm_free(pdev);
1028
1029 return 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001030}
1031
Shawn Guo85d65092011-05-27 23:48:12 +08001032static struct platform_driver sdhci_esdhc_imx_driver = {
1033 .driver = {
1034 .name = "sdhci-esdhc-imx",
1035 .owner = THIS_MODULE,
Shawn Guoabfafc22011-06-30 15:44:44 +08001036 .of_match_table = imx_esdhc_dt_ids,
Manuel Lauss29495aa2011-11-03 11:09:45 +01001037 .pm = SDHCI_PLTFM_PMOPS,
Shawn Guo85d65092011-05-27 23:48:12 +08001038 },
Shawn Guo57ed3312011-06-30 09:24:26 +08001039 .id_table = imx_esdhc_devtype,
Shawn Guo85d65092011-05-27 23:48:12 +08001040 .probe = sdhci_esdhc_imx_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05001041 .remove = sdhci_esdhc_imx_remove,
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001042};
Shawn Guo85d65092011-05-27 23:48:12 +08001043
Axel Lind1f81a62011-11-26 12:55:43 +08001044module_platform_driver(sdhci_esdhc_imx_driver);
Shawn Guo85d65092011-05-27 23:48:12 +08001045
1046MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
1047MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
1048MODULE_LICENSE("GPL v2");