blob: 2421ac7fa6bf684093de1c13f1198794f952c2fd [file] [log] [blame]
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001/*
2 * Freescale eSDHC i.MX controller driver for the platform bus.
3 *
4 * derived from the OF-version.
5 *
6 * Copyright (c) 2010 Pengutronix e.K.
7 * Author: Wolfram Sang <w.sang@pengutronix.de>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License.
12 */
13
14#include <linux/io.h>
15#include <linux/delay.h>
16#include <linux/err.h>
17#include <linux/clk.h>
Wolfram Sang0c6d49c2011-02-26 14:44:39 +010018#include <linux/gpio.h>
Shawn Guo66506f72011-08-15 10:28:18 +080019#include <linux/module.h>
Richard Zhue1498602011-03-25 09:18:27 -040020#include <linux/slab.h>
Wolfram Sang95f25ef2010-10-15 12:21:04 +020021#include <linux/mmc/host.h>
Richard Zhu58ac8172011-03-21 13:22:16 +080022#include <linux/mmc/mmc.h>
23#include <linux/mmc/sdio.h>
Shawn Guofbe5fdd2012-12-11 22:32:20 +080024#include <linux/mmc/slot-gpio.h>
Shawn Guoabfafc22011-06-30 15:44:44 +080025#include <linux/of.h>
26#include <linux/of_device.h>
27#include <linux/of_gpio.h>
Dong Aishenge62d8b82012-05-11 14:56:01 +080028#include <linux/pinctrl/consumer.h>
Arnd Bergmann82906b12012-08-24 15:14:29 +020029#include <linux/platform_data/mmc-esdhc-imx.h>
Wolfram Sang95f25ef2010-10-15 12:21:04 +020030#include "sdhci-pltfm.h"
31#include "sdhci-esdhc.h"
32
Shawn Guo60bf6392013-01-15 23:36:53 +080033#define ESDHC_CTRL_D3CD 0x08
Richard Zhu58ac8172011-03-21 13:22:16 +080034/* VENDOR SPEC register */
Shawn Guo60bf6392013-01-15 23:36:53 +080035#define ESDHC_VENDOR_SPEC 0xc0
36#define ESDHC_VENDOR_SPEC_SDIO_QUIRK (1 << 1)
Dong Aisheng03221912013-09-13 19:11:34 +080037#define ESDHC_VENDOR_SPEC_VSELECT (1 << 1)
Dong Aishengfed2f6e2013-09-13 19:11:33 +080038#define ESDHC_VENDOR_SPEC_FRC_SDCLK_ON (1 << 8)
Shawn Guo60bf6392013-01-15 23:36:53 +080039#define ESDHC_WTMK_LVL 0x44
40#define ESDHC_MIX_CTRL 0x48
Shawn Guo2a15f982013-01-21 19:02:26 +080041#define ESDHC_MIX_CTRL_AC23EN (1 << 7)
Dong Aisheng03221912013-09-13 19:11:34 +080042#define ESDHC_MIX_CTRL_EXE_TUNE (1 << 22)
43#define ESDHC_MIX_CTRL_SMPCLK_SEL (1 << 23)
44#define ESDHC_MIX_CTRL_FBCLK_SEL (1 << 25)
Shawn Guo2a15f982013-01-21 19:02:26 +080045/* Bits 3 and 6 are not SDHCI standard definitions */
46#define ESDHC_MIX_CTRL_SDHCI_MASK 0xb7
Richard Zhu58ac8172011-03-21 13:22:16 +080047
Dong Aisheng03221912013-09-13 19:11:34 +080048/* tune control register */
49#define ESDHC_TUNE_CTRL_STATUS 0x68
50#define ESDHC_TUNE_CTRL_STEP 1
51#define ESDHC_TUNE_CTRL_MIN 0
52#define ESDHC_TUNE_CTRL_MAX ((1 << 7) - 1)
53
54#define ESDHC_TUNING_BLOCK_PATTERN_LEN 64
55
Dong Aishengad932202013-09-13 19:11:35 +080056/* pinctrl state */
57#define ESDHC_PINCTRL_STATE_100MHZ "state_100mhz"
58#define ESDHC_PINCTRL_STATE_200MHZ "state_200mhz"
59
Richard Zhu58ac8172011-03-21 13:22:16 +080060/*
Sascha Haueraf510792013-01-21 19:02:28 +080061 * Our interpretation of the SDHCI_HOST_CONTROL register
62 */
63#define ESDHC_CTRL_4BITBUS (0x1 << 1)
64#define ESDHC_CTRL_8BITBUS (0x2 << 1)
65#define ESDHC_CTRL_BUSWIDTH_MASK (0x3 << 1)
66
67/*
Richard Zhu97e4ba62011-08-11 16:51:46 -040068 * There is an INT DMA ERR mis-match between eSDHC and STD SDHC SPEC:
69 * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
70 * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
71 * Define this macro DMA error INT for fsl eSDHC
72 */
Shawn Guo60bf6392013-01-15 23:36:53 +080073#define ESDHC_INT_VENDOR_SPEC_DMA_ERR (1 << 28)
Richard Zhu97e4ba62011-08-11 16:51:46 -040074
75/*
Richard Zhu58ac8172011-03-21 13:22:16 +080076 * The CMDTYPE of the CMD register (offset 0xE) should be set to
77 * "11" when the STOP CMD12 is issued on imx53 to abort one
78 * open ended multi-blk IO. Otherwise the TC INT wouldn't
79 * be generated.
80 * In exact block transfer, the controller doesn't complete the
81 * operations automatically as required at the end of the
82 * transfer and remains on hold if the abort command is not sent.
83 * As a result, the TC flag is not asserted and SW received timeout
84 * exeception. Bit1 of Vendor Spec registor is used to fix it.
85 */
Shawn Guo31fbb302013-10-17 15:19:44 +080086#define ESDHC_FLAG_MULTIBLK_NO_INT BIT(1)
87/*
88 * The flag enables the workaround for ESDHC errata ENGcm07207 which
89 * affects i.MX25 and i.MX35.
90 */
91#define ESDHC_FLAG_ENGCM07207 BIT(2)
Shawn Guo9d61c002013-10-17 15:19:45 +080092/*
93 * The flag tells that the ESDHC controller is an USDHC block that is
94 * integrated on the i.MX6 series.
95 */
96#define ESDHC_FLAG_USDHC BIT(3)
Richard Zhue1498602011-03-25 09:18:27 -040097
Shawn Guo57ed3312011-06-30 09:24:26 +080098enum imx_esdhc_type {
99 IMX25_ESDHC,
100 IMX35_ESDHC,
101 IMX51_ESDHC,
102 IMX53_ESDHC,
Shawn Guo95a24822011-09-19 17:32:21 +0800103 IMX6Q_USDHC,
Shawn Guo57ed3312011-06-30 09:24:26 +0800104};
105
Richard Zhue1498602011-03-25 09:18:27 -0400106struct pltfm_imx_data {
107 int flags;
108 u32 scratchpad;
Shawn Guo57ed3312011-06-30 09:24:26 +0800109 enum imx_esdhc_type devtype;
Dong Aishenge62d8b82012-05-11 14:56:01 +0800110 struct pinctrl *pinctrl;
Dong Aishengad932202013-09-13 19:11:35 +0800111 struct pinctrl_state *pins_default;
112 struct pinctrl_state *pins_100mhz;
113 struct pinctrl_state *pins_200mhz;
Shawn Guo842afc02011-07-06 22:57:48 +0800114 struct esdhc_platform_data boarddata;
Sascha Hauer52dac612012-03-07 09:31:34 +0100115 struct clk *clk_ipg;
116 struct clk *clk_ahb;
117 struct clk *clk_per;
Lucas Stach361b8482013-03-15 09:49:26 +0100118 enum {
119 NO_CMD_PENDING, /* no multiblock command pending*/
120 MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */
121 WAIT_FOR_INT, /* sent CMD12, waiting for response INT */
122 } multiblock_status;
Dong Aisheng03221912013-09-13 19:11:34 +0800123 u32 uhs_mode;
Richard Zhue1498602011-03-25 09:18:27 -0400124};
125
Shawn Guo57ed3312011-06-30 09:24:26 +0800126static struct platform_device_id imx_esdhc_devtype[] = {
127 {
128 .name = "sdhci-esdhc-imx25",
129 .driver_data = IMX25_ESDHC,
130 }, {
131 .name = "sdhci-esdhc-imx35",
132 .driver_data = IMX35_ESDHC,
133 }, {
134 .name = "sdhci-esdhc-imx51",
135 .driver_data = IMX51_ESDHC,
136 }, {
137 .name = "sdhci-esdhc-imx53",
138 .driver_data = IMX53_ESDHC,
139 }, {
Shawn Guo95a24822011-09-19 17:32:21 +0800140 .name = "sdhci-usdhc-imx6q",
141 .driver_data = IMX6Q_USDHC,
142 }, {
Shawn Guo57ed3312011-06-30 09:24:26 +0800143 /* sentinel */
144 }
145};
146MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
147
Shawn Guoabfafc22011-06-30 15:44:44 +0800148static const struct of_device_id imx_esdhc_dt_ids[] = {
149 { .compatible = "fsl,imx25-esdhc", .data = &imx_esdhc_devtype[IMX25_ESDHC], },
150 { .compatible = "fsl,imx35-esdhc", .data = &imx_esdhc_devtype[IMX35_ESDHC], },
151 { .compatible = "fsl,imx51-esdhc", .data = &imx_esdhc_devtype[IMX51_ESDHC], },
152 { .compatible = "fsl,imx53-esdhc", .data = &imx_esdhc_devtype[IMX53_ESDHC], },
Shawn Guo95a24822011-09-19 17:32:21 +0800153 { .compatible = "fsl,imx6q-usdhc", .data = &imx_esdhc_devtype[IMX6Q_USDHC], },
Shawn Guoabfafc22011-06-30 15:44:44 +0800154 { /* sentinel */ }
155};
156MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
157
Shawn Guo57ed3312011-06-30 09:24:26 +0800158static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
159{
160 return data->devtype == IMX25_ESDHC;
161}
162
163static inline int is_imx35_esdhc(struct pltfm_imx_data *data)
164{
165 return data->devtype == IMX35_ESDHC;
166}
167
168static inline int is_imx51_esdhc(struct pltfm_imx_data *data)
169{
170 return data->devtype == IMX51_ESDHC;
171}
172
173static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
174{
175 return data->devtype == IMX53_ESDHC;
176}
177
Shawn Guo95a24822011-09-19 17:32:21 +0800178static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
179{
180 return data->devtype == IMX6Q_USDHC;
181}
182
Shawn Guo9d61c002013-10-17 15:19:45 +0800183static inline int esdhc_is_usdhc(struct pltfm_imx_data *data)
184{
185 return !!(data->flags & ESDHC_FLAG_USDHC);
186}
187
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200188static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
189{
190 void __iomem *base = host->ioaddr + (reg & ~0x3);
191 u32 shift = (reg & 0x3) * 8;
192
193 writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
194}
195
Wolfram Sang7e29c302011-02-26 14:44:41 +0100196static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
197{
Lucas Stach361b8482013-03-15 09:49:26 +0100198 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
199 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100200 u32 val = readl(host->ioaddr + reg);
201
Dong Aisheng03221912013-09-13 19:11:34 +0800202 if (unlikely(reg == SDHCI_PRESENT_STATE)) {
203 u32 fsl_prss = val;
204 /* save the least 20 bits */
205 val = fsl_prss & 0x000FFFFF;
206 /* move dat[0-3] bits */
207 val |= (fsl_prss & 0x0F000000) >> 4;
208 /* move cmd line bit */
209 val |= (fsl_prss & 0x00800000) << 1;
210 }
211
Richard Zhu97e4ba62011-08-11 16:51:46 -0400212 if (unlikely(reg == SDHCI_CAPABILITIES)) {
213 /* In FSL esdhc IC module, only bit20 is used to indicate the
214 * ADMA2 capability of esdhc, but this bit is messed up on
215 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
216 * don't actually support ADMA2). So set the BROKEN_ADMA
217 * uirk on MX25/35 platforms.
218 */
219
220 if (val & SDHCI_CAN_DO_ADMA1) {
221 val &= ~SDHCI_CAN_DO_ADMA1;
222 val |= SDHCI_CAN_DO_ADMA2;
223 }
224 }
225
Shawn Guo9d61c002013-10-17 15:19:45 +0800226 if (unlikely(reg == SDHCI_CAPABILITIES_1) && esdhc_is_usdhc(imx_data))
Dong Aisheng03221912013-09-13 19:11:34 +0800227 val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104
228 | SDHCI_SUPPORT_SDR50;
229
Shawn Guo9d61c002013-10-17 15:19:45 +0800230 if (unlikely(reg == SDHCI_MAX_CURRENT) && esdhc_is_usdhc(imx_data)) {
Dong Aisheng03221912013-09-13 19:11:34 +0800231 val = 0;
232 val |= 0xFF << SDHCI_MAX_CURRENT_330_SHIFT;
233 val |= 0xFF << SDHCI_MAX_CURRENT_300_SHIFT;
234 val |= 0xFF << SDHCI_MAX_CURRENT_180_SHIFT;
235 }
236
Richard Zhu97e4ba62011-08-11 16:51:46 -0400237 if (unlikely(reg == SDHCI_INT_STATUS)) {
Shawn Guo60bf6392013-01-15 23:36:53 +0800238 if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
239 val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
Richard Zhu97e4ba62011-08-11 16:51:46 -0400240 val |= SDHCI_INT_ADMA_ERROR;
241 }
Lucas Stach361b8482013-03-15 09:49:26 +0100242
243 /*
244 * mask off the interrupt we get in response to the manually
245 * sent CMD12
246 */
247 if ((imx_data->multiblock_status == WAIT_FOR_INT) &&
248 ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) {
249 val &= ~SDHCI_INT_RESPONSE;
250 writel(SDHCI_INT_RESPONSE, host->ioaddr +
251 SDHCI_INT_STATUS);
252 imx_data->multiblock_status = NO_CMD_PENDING;
253 }
Richard Zhu97e4ba62011-08-11 16:51:46 -0400254 }
255
Wolfram Sang7e29c302011-02-26 14:44:41 +0100256 return val;
257}
258
259static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
260{
Richard Zhue1498602011-03-25 09:18:27 -0400261 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
262 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Tony Lin0d588642011-08-11 16:45:59 -0400263 u32 data;
Richard Zhue1498602011-03-25 09:18:27 -0400264
Tony Lin0d588642011-08-11 16:45:59 -0400265 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
Tony Lin0d588642011-08-11 16:45:59 -0400266 if (val & SDHCI_INT_CARD_INT) {
267 /*
268 * Clear and then set D3CD bit to avoid missing the
269 * card interrupt. This is a eSDHC controller problem
270 * so we need to apply the following workaround: clear
271 * and set D3CD bit will make eSDHC re-sample the card
272 * interrupt. In case a card interrupt was lost,
273 * re-sample it by the following steps.
274 */
275 data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
Shawn Guo60bf6392013-01-15 23:36:53 +0800276 data &= ~ESDHC_CTRL_D3CD;
Tony Lin0d588642011-08-11 16:45:59 -0400277 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
Shawn Guo60bf6392013-01-15 23:36:53 +0800278 data |= ESDHC_CTRL_D3CD;
Tony Lin0d588642011-08-11 16:45:59 -0400279 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
280 }
281 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100282
Richard Zhu58ac8172011-03-21 13:22:16 +0800283 if (unlikely((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
284 && (reg == SDHCI_INT_STATUS)
285 && (val & SDHCI_INT_DATA_END))) {
286 u32 v;
Shawn Guo60bf6392013-01-15 23:36:53 +0800287 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
288 v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
289 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
Lucas Stach361b8482013-03-15 09:49:26 +0100290
291 if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS)
292 {
293 /* send a manual CMD12 with RESPTYP=none */
294 data = MMC_STOP_TRANSMISSION << 24 |
295 SDHCI_CMD_ABORTCMD << 16;
296 writel(data, host->ioaddr + SDHCI_TRANSFER_MODE);
297 imx_data->multiblock_status = WAIT_FOR_INT;
298 }
Richard Zhu58ac8172011-03-21 13:22:16 +0800299 }
300
Richard Zhu97e4ba62011-08-11 16:51:46 -0400301 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
302 if (val & SDHCI_INT_ADMA_ERROR) {
303 val &= ~SDHCI_INT_ADMA_ERROR;
Shawn Guo60bf6392013-01-15 23:36:53 +0800304 val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
Richard Zhu97e4ba62011-08-11 16:51:46 -0400305 }
306 }
307
Wolfram Sang7e29c302011-02-26 14:44:41 +0100308 writel(val, host->ioaddr + reg);
309}
310
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200311static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
312{
Shawn Guoef4d0882013-01-15 23:30:27 +0800313 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
314 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aisheng03221912013-09-13 19:11:34 +0800315 u16 ret = 0;
316 u32 val;
Shawn Guoef4d0882013-01-15 23:30:27 +0800317
Shawn Guo95a24822011-09-19 17:32:21 +0800318 if (unlikely(reg == SDHCI_HOST_VERSION)) {
Shawn Guoef4d0882013-01-15 23:30:27 +0800319 reg ^= 2;
Shawn Guo9d61c002013-10-17 15:19:45 +0800320 if (esdhc_is_usdhc(imx_data)) {
Shawn Guoef4d0882013-01-15 23:30:27 +0800321 /*
322 * The usdhc register returns a wrong host version.
323 * Correct it here.
324 */
325 return SDHCI_SPEC_300;
326 }
Shawn Guo95a24822011-09-19 17:32:21 +0800327 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200328
Dong Aisheng03221912013-09-13 19:11:34 +0800329 if (unlikely(reg == SDHCI_HOST_CONTROL2)) {
330 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
331 if (val & ESDHC_VENDOR_SPEC_VSELECT)
332 ret |= SDHCI_CTRL_VDD_180;
333
Shawn Guo9d61c002013-10-17 15:19:45 +0800334 if (esdhc_is_usdhc(imx_data)) {
Dong Aisheng03221912013-09-13 19:11:34 +0800335 val = readl(host->ioaddr + ESDHC_MIX_CTRL);
336 if (val & ESDHC_MIX_CTRL_EXE_TUNE)
337 ret |= SDHCI_CTRL_EXEC_TUNING;
338 if (val & ESDHC_MIX_CTRL_SMPCLK_SEL)
339 ret |= SDHCI_CTRL_TUNED_CLK;
340 }
341
342 ret |= (imx_data->uhs_mode & SDHCI_CTRL_UHS_MASK);
343 ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
344
345 return ret;
346 }
347
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200348 return readw(host->ioaddr + reg);
349}
350
351static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
352{
353 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400354 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aisheng03221912013-09-13 19:11:34 +0800355 u32 new_val = 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200356
357 switch (reg) {
Dong Aisheng03221912013-09-13 19:11:34 +0800358 case SDHCI_CLOCK_CONTROL:
359 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
360 if (val & SDHCI_CLOCK_CARD_EN)
361 new_val |= ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
362 else
363 new_val &= ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
364 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
365 return;
366 case SDHCI_HOST_CONTROL2:
367 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
368 if (val & SDHCI_CTRL_VDD_180)
369 new_val |= ESDHC_VENDOR_SPEC_VSELECT;
370 else
371 new_val &= ~ESDHC_VENDOR_SPEC_VSELECT;
372 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
373 imx_data->uhs_mode = val & SDHCI_CTRL_UHS_MASK;
374 new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
375 if (val & SDHCI_CTRL_TUNED_CLK)
376 new_val |= ESDHC_MIX_CTRL_SMPCLK_SEL;
377 else
378 new_val &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
379 writel(new_val , host->ioaddr + ESDHC_MIX_CTRL);
380 return;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200381 case SDHCI_TRANSFER_MODE:
Richard Zhu58ac8172011-03-21 13:22:16 +0800382 if ((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
383 && (host->cmd->opcode == SD_IO_RW_EXTENDED)
384 && (host->cmd->data->blocks > 1)
385 && (host->cmd->data->flags & MMC_DATA_READ)) {
386 u32 v;
Shawn Guo60bf6392013-01-15 23:36:53 +0800387 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
388 v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
389 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
Richard Zhu58ac8172011-03-21 13:22:16 +0800390 }
Shawn Guo69f54692013-01-21 19:02:24 +0800391
Shawn Guo9d61c002013-10-17 15:19:45 +0800392 if (esdhc_is_usdhc(imx_data)) {
Shawn Guo69f54692013-01-21 19:02:24 +0800393 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
Shawn Guo2a15f982013-01-21 19:02:26 +0800394 /* Swap AC23 bit */
395 if (val & SDHCI_TRNS_AUTO_CMD23) {
396 val &= ~SDHCI_TRNS_AUTO_CMD23;
397 val |= ESDHC_MIX_CTRL_AC23EN;
398 }
399 m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
Shawn Guo69f54692013-01-21 19:02:24 +0800400 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
401 } else {
402 /*
403 * Postpone this write, we must do it together with a
404 * command write that is down below.
405 */
406 imx_data->scratchpad = val;
407 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200408 return;
409 case SDHCI_COMMAND:
Lucas Stach361b8482013-03-15 09:49:26 +0100410 if (host->cmd->opcode == MMC_STOP_TRANSMISSION)
Richard Zhu58ac8172011-03-21 13:22:16 +0800411 val |= SDHCI_CMD_ABORTCMD;
Shawn Guo95a24822011-09-19 17:32:21 +0800412
Lucas Stach361b8482013-03-15 09:49:26 +0100413 if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
414 (imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
415 imx_data->multiblock_status = MULTIBLK_IN_PROCESS;
416
Shawn Guo9d61c002013-10-17 15:19:45 +0800417 if (esdhc_is_usdhc(imx_data))
Shawn Guo95a24822011-09-19 17:32:21 +0800418 writel(val << 16,
419 host->ioaddr + SDHCI_TRANSFER_MODE);
Shawn Guo69f54692013-01-21 19:02:24 +0800420 else
Shawn Guo95a24822011-09-19 17:32:21 +0800421 writel(val << 16 | imx_data->scratchpad,
422 host->ioaddr + SDHCI_TRANSFER_MODE);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200423 return;
424 case SDHCI_BLOCK_SIZE:
425 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
426 break;
427 }
428 esdhc_clrset_le(host, 0xffff, val, reg);
429}
430
431static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
432{
Wilson Callan9a0985b2012-07-19 02:49:16 -0400433 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
434 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200435 u32 new_val;
Sascha Haueraf510792013-01-21 19:02:28 +0800436 u32 mask;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200437
438 switch (reg) {
439 case SDHCI_POWER_CONTROL:
440 /*
441 * FSL put some DMA bits here
442 * If your board has a regulator, code should be here
443 */
444 return;
445 case SDHCI_HOST_CONTROL:
Shawn Guo6b40d182013-01-15 23:36:52 +0800446 /* FSL messed up here, so we need to manually compose it. */
Sascha Haueraf510792013-01-21 19:02:28 +0800447 new_val = val & SDHCI_CTRL_LED;
Masanari Iida7122bbb2012-08-05 23:25:40 +0900448 /* ensure the endianness */
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200449 new_val |= ESDHC_HOST_CONTROL_LE;
Wilson Callan9a0985b2012-07-19 02:49:16 -0400450 /* bits 8&9 are reserved on mx25 */
451 if (!is_imx25_esdhc(imx_data)) {
452 /* DMA mode bits are shifted */
453 new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
454 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200455
Sascha Haueraf510792013-01-21 19:02:28 +0800456 /*
457 * Do not touch buswidth bits here. This is done in
458 * esdhc_pltfm_bus_width.
Martin Fuzzeyf6825742013-04-15 17:08:35 +0200459 * Do not touch the D3CD bit either which is used for the
460 * SDIO interrupt errata workaround.
Sascha Haueraf510792013-01-21 19:02:28 +0800461 */
Martin Fuzzeyf6825742013-04-15 17:08:35 +0200462 mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD);
Sascha Haueraf510792013-01-21 19:02:28 +0800463
464 esdhc_clrset_le(host, mask, new_val, reg);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200465 return;
466 }
467 esdhc_clrset_le(host, 0xff, val, reg);
Shawn Guo913413c2011-06-21 22:41:51 +0800468
469 /*
470 * The esdhc has a design violation to SDHC spec which tells
471 * that software reset should not affect card detection circuit.
472 * But esdhc clears its SYSCTL register bits [0..2] during the
473 * software reset. This will stop those clocks that card detection
474 * circuit relies on. To work around it, we turn the clocks on back
475 * to keep card detection circuit functional.
476 */
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800477 if ((reg == SDHCI_SOFTWARE_RESET) && (val & 1)) {
Shawn Guo913413c2011-06-21 22:41:51 +0800478 esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800479 /*
480 * The reset on usdhc fails to clear MIX_CTRL register.
481 * Do it manually here.
482 */
Shawn Guo9d61c002013-10-17 15:19:45 +0800483 if (esdhc_is_usdhc(imx_data))
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800484 writel(0, host->ioaddr + ESDHC_MIX_CTRL);
485 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200486}
487
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200488static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
489{
490 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
491 struct pltfm_imx_data *imx_data = pltfm_host->priv;
492 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
493
494 u32 f_host = clk_get_rate(pltfm_host->clk);
495
496 if (boarddata->f_max && (boarddata->f_max < f_host))
497 return boarddata->f_max;
498 else
499 return f_host;
500}
501
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200502static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
503{
504 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
505
506 return clk_get_rate(pltfm_host->clk) / 256 / 16;
507}
508
Lucas Stach8ba95802013-06-05 15:13:25 +0200509static inline void esdhc_pltfm_set_clock(struct sdhci_host *host,
510 unsigned int clock)
511{
512 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800513 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aishengd31fc002013-09-13 19:11:32 +0800514 unsigned int host_clock = clk_get_rate(pltfm_host->clk);
515 int pre_div = 2;
516 int div = 1;
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800517 u32 temp, val;
Lucas Stach8ba95802013-06-05 15:13:25 +0200518
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800519 if (clock == 0) {
Shawn Guo9d61c002013-10-17 15:19:45 +0800520 if (esdhc_is_usdhc(imx_data)) {
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800521 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
522 writel(val & ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
523 host->ioaddr + ESDHC_VENDOR_SPEC);
524 }
Dong Aishengd31fc002013-09-13 19:11:32 +0800525 goto out;
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800526 }
Dong Aishengd31fc002013-09-13 19:11:32 +0800527
Shawn Guo9d61c002013-10-17 15:19:45 +0800528 if (esdhc_is_usdhc(imx_data))
Dong Aisheng5f7886c2013-09-13 19:11:36 +0800529 pre_div = 1;
530
Dong Aishengd31fc002013-09-13 19:11:32 +0800531 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
532 temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
533 | ESDHC_CLOCK_MASK);
534 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
535
536 while (host_clock / pre_div / 16 > clock && pre_div < 256)
537 pre_div *= 2;
538
539 while (host_clock / pre_div / div > clock && div < 16)
540 div++;
541
Dong Aishenge76b8552013-09-13 19:11:37 +0800542 host->mmc->actual_clock = host_clock / pre_div / div;
Dong Aishengd31fc002013-09-13 19:11:32 +0800543 dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
Dong Aishenge76b8552013-09-13 19:11:37 +0800544 clock, host->mmc->actual_clock);
Dong Aishengd31fc002013-09-13 19:11:32 +0800545
546 pre_div >>= 1;
547 div--;
548
549 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
550 temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
551 | (div << ESDHC_DIVIDER_SHIFT)
552 | (pre_div << ESDHC_PREDIV_SHIFT));
553 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800554
Shawn Guo9d61c002013-10-17 15:19:45 +0800555 if (esdhc_is_usdhc(imx_data)) {
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800556 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
557 writel(val | ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
558 host->ioaddr + ESDHC_VENDOR_SPEC);
559 }
560
Dong Aishengd31fc002013-09-13 19:11:32 +0800561 mdelay(1);
562out:
563 host->clock = clock;
Lucas Stach8ba95802013-06-05 15:13:25 +0200564}
565
Shawn Guo913413c2011-06-21 22:41:51 +0800566static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
567{
Shawn Guo842afc02011-07-06 22:57:48 +0800568 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
569 struct pltfm_imx_data *imx_data = pltfm_host->priv;
570 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Shawn Guo913413c2011-06-21 22:41:51 +0800571
572 switch (boarddata->wp_type) {
573 case ESDHC_WP_GPIO:
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800574 return mmc_gpio_get_ro(host->mmc);
Shawn Guo913413c2011-06-21 22:41:51 +0800575 case ESDHC_WP_CONTROLLER:
576 return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
577 SDHCI_WRITE_PROTECT);
578 case ESDHC_WP_NONE:
579 break;
580 }
581
582 return -ENOSYS;
583}
584
Sascha Haueraf510792013-01-21 19:02:28 +0800585static int esdhc_pltfm_bus_width(struct sdhci_host *host, int width)
586{
587 u32 ctrl;
588
589 switch (width) {
590 case MMC_BUS_WIDTH_8:
591 ctrl = ESDHC_CTRL_8BITBUS;
592 break;
593 case MMC_BUS_WIDTH_4:
594 ctrl = ESDHC_CTRL_4BITBUS;
595 break;
596 default:
597 ctrl = 0;
598 break;
599 }
600
601 esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
602 SDHCI_HOST_CONTROL);
603
604 return 0;
605}
606
Dong Aisheng03221912013-09-13 19:11:34 +0800607static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val)
608{
609 u32 reg;
610
611 /* FIXME: delay a bit for card to be ready for next tuning due to errors */
612 mdelay(1);
613
614 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
615 reg |= ESDHC_MIX_CTRL_EXE_TUNE | ESDHC_MIX_CTRL_SMPCLK_SEL |
616 ESDHC_MIX_CTRL_FBCLK_SEL;
617 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
618 writel(val << 8, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
619 dev_dbg(mmc_dev(host->mmc),
620 "tunning with delay 0x%x ESDHC_TUNE_CTRL_STATUS 0x%x\n",
621 val, readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS));
622}
623
624static void esdhc_request_done(struct mmc_request *mrq)
625{
626 complete(&mrq->completion);
627}
628
629static int esdhc_send_tuning_cmd(struct sdhci_host *host, u32 opcode)
630{
631 struct mmc_command cmd = {0};
632 struct mmc_request mrq = {0};
633 struct mmc_data data = {0};
634 struct scatterlist sg;
635 char tuning_pattern[ESDHC_TUNING_BLOCK_PATTERN_LEN];
636
637 cmd.opcode = opcode;
638 cmd.arg = 0;
639 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
640
641 data.blksz = ESDHC_TUNING_BLOCK_PATTERN_LEN;
642 data.blocks = 1;
643 data.flags = MMC_DATA_READ;
644 data.sg = &sg;
645 data.sg_len = 1;
646
647 sg_init_one(&sg, tuning_pattern, sizeof(tuning_pattern));
648
649 mrq.cmd = &cmd;
650 mrq.cmd->mrq = &mrq;
651 mrq.data = &data;
652 mrq.data->mrq = &mrq;
653 mrq.cmd->data = mrq.data;
654
655 mrq.done = esdhc_request_done;
656 init_completion(&(mrq.completion));
657
658 disable_irq(host->irq);
659 spin_lock(&host->lock);
660 host->mrq = &mrq;
661
662 sdhci_send_command(host, mrq.cmd);
663
664 spin_unlock(&host->lock);
665 enable_irq(host->irq);
666
667 wait_for_completion(&mrq.completion);
668
669 if (cmd.error)
670 return cmd.error;
671 if (data.error)
672 return data.error;
673
674 return 0;
675}
676
677static void esdhc_post_tuning(struct sdhci_host *host)
678{
679 u32 reg;
680
681 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
682 reg &= ~ESDHC_MIX_CTRL_EXE_TUNE;
683 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
684}
685
686static int esdhc_executing_tuning(struct sdhci_host *host, u32 opcode)
687{
688 int min, max, avg, ret;
689
690 /* find the mininum delay first which can pass tuning */
691 min = ESDHC_TUNE_CTRL_MIN;
692 while (min < ESDHC_TUNE_CTRL_MAX) {
693 esdhc_prepare_tuning(host, min);
694 if (!esdhc_send_tuning_cmd(host, opcode))
695 break;
696 min += ESDHC_TUNE_CTRL_STEP;
697 }
698
699 /* find the maxinum delay which can not pass tuning */
700 max = min + ESDHC_TUNE_CTRL_STEP;
701 while (max < ESDHC_TUNE_CTRL_MAX) {
702 esdhc_prepare_tuning(host, max);
703 if (esdhc_send_tuning_cmd(host, opcode)) {
704 max -= ESDHC_TUNE_CTRL_STEP;
705 break;
706 }
707 max += ESDHC_TUNE_CTRL_STEP;
708 }
709
710 /* use average delay to get the best timing */
711 avg = (min + max) / 2;
712 esdhc_prepare_tuning(host, avg);
713 ret = esdhc_send_tuning_cmd(host, opcode);
714 esdhc_post_tuning(host);
715
716 dev_dbg(mmc_dev(host->mmc), "tunning %s at 0x%x ret %d\n",
717 ret ? "failed" : "passed", avg, ret);
718
719 return ret;
720}
721
Dong Aishengad932202013-09-13 19:11:35 +0800722static int esdhc_change_pinstate(struct sdhci_host *host,
723 unsigned int uhs)
724{
725 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
726 struct pltfm_imx_data *imx_data = pltfm_host->priv;
727 struct pinctrl_state *pinctrl;
728
729 dev_dbg(mmc_dev(host->mmc), "change pinctrl state for uhs %d\n", uhs);
730
731 if (IS_ERR(imx_data->pinctrl) ||
732 IS_ERR(imx_data->pins_default) ||
733 IS_ERR(imx_data->pins_100mhz) ||
734 IS_ERR(imx_data->pins_200mhz))
735 return -EINVAL;
736
737 switch (uhs) {
738 case MMC_TIMING_UHS_SDR50:
739 pinctrl = imx_data->pins_100mhz;
740 break;
741 case MMC_TIMING_UHS_SDR104:
742 pinctrl = imx_data->pins_200mhz;
743 break;
744 default:
745 /* back to default state for other legacy timing */
746 pinctrl = imx_data->pins_default;
747 }
748
749 return pinctrl_select_state(imx_data->pinctrl, pinctrl);
750}
751
752static int esdhc_set_uhs_signaling(struct sdhci_host *host, unsigned int uhs)
753{
754 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
755 struct pltfm_imx_data *imx_data = pltfm_host->priv;
756
757 switch (uhs) {
758 case MMC_TIMING_UHS_SDR12:
759 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR12;
760 break;
761 case MMC_TIMING_UHS_SDR25:
762 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR25;
763 break;
764 case MMC_TIMING_UHS_SDR50:
765 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR50;
766 break;
767 case MMC_TIMING_UHS_SDR104:
768 imx_data->uhs_mode = SDHCI_CTRL_UHS_SDR104;
769 break;
770 case MMC_TIMING_UHS_DDR50:
771 imx_data->uhs_mode = SDHCI_CTRL_UHS_DDR50;
772 break;
773 }
774
775 return esdhc_change_pinstate(host, uhs);
776}
777
Lars-Peter Clausenc9155682013-03-13 19:26:05 +0100778static const struct sdhci_ops sdhci_esdhc_ops = {
Richard Zhue1498602011-03-25 09:18:27 -0400779 .read_l = esdhc_readl_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100780 .read_w = esdhc_readw_le,
Richard Zhue1498602011-03-25 09:18:27 -0400781 .write_l = esdhc_writel_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100782 .write_w = esdhc_writew_le,
783 .write_b = esdhc_writeb_le,
Lucas Stach8ba95802013-06-05 15:13:25 +0200784 .set_clock = esdhc_pltfm_set_clock,
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200785 .get_max_clock = esdhc_pltfm_get_max_clock,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100786 .get_min_clock = esdhc_pltfm_get_min_clock,
Shawn Guo913413c2011-06-21 22:41:51 +0800787 .get_ro = esdhc_pltfm_get_ro,
Sascha Haueraf510792013-01-21 19:02:28 +0800788 .platform_bus_width = esdhc_pltfm_bus_width,
Dong Aishengad932202013-09-13 19:11:35 +0800789 .set_uhs_signaling = esdhc_set_uhs_signaling,
Dong Aisheng03221912013-09-13 19:11:34 +0800790 .platform_execute_tuning = esdhc_executing_tuning,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100791};
792
Lars-Peter Clausen1db5eeb2013-03-13 19:26:03 +0100793static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
Richard Zhu97e4ba62011-08-11 16:51:46 -0400794 .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
795 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
796 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
Shawn Guo85d65092011-05-27 23:48:12 +0800797 | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
Shawn Guo85d65092011-05-27 23:48:12 +0800798 .ops = &sdhci_esdhc_ops,
799};
800
Shawn Guoabfafc22011-06-30 15:44:44 +0800801#ifdef CONFIG_OF
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500802static int
Shawn Guoabfafc22011-06-30 15:44:44 +0800803sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
804 struct esdhc_platform_data *boarddata)
805{
806 struct device_node *np = pdev->dev.of_node;
807
808 if (!np)
809 return -ENODEV;
810
Arnd Bergmann7f217792012-05-13 00:14:24 -0400811 if (of_get_property(np, "non-removable", NULL))
Shawn Guoabfafc22011-06-30 15:44:44 +0800812 boarddata->cd_type = ESDHC_CD_PERMANENT;
813
814 if (of_get_property(np, "fsl,cd-controller", NULL))
815 boarddata->cd_type = ESDHC_CD_CONTROLLER;
816
817 if (of_get_property(np, "fsl,wp-controller", NULL))
818 boarddata->wp_type = ESDHC_WP_CONTROLLER;
819
820 boarddata->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
821 if (gpio_is_valid(boarddata->cd_gpio))
822 boarddata->cd_type = ESDHC_CD_GPIO;
823
824 boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
825 if (gpio_is_valid(boarddata->wp_gpio))
826 boarddata->wp_type = ESDHC_WP_GPIO;
827
Sascha Haueraf510792013-01-21 19:02:28 +0800828 of_property_read_u32(np, "bus-width", &boarddata->max_bus_width);
829
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200830 of_property_read_u32(np, "max-frequency", &boarddata->f_max);
831
Dong Aishengad932202013-09-13 19:11:35 +0800832 if (of_find_property(np, "no-1-8-v", NULL))
833 boarddata->support_vsel = false;
834 else
835 boarddata->support_vsel = true;
836
Shawn Guoabfafc22011-06-30 15:44:44 +0800837 return 0;
838}
839#else
840static inline int
841sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
842 struct esdhc_platform_data *boarddata)
843{
844 return -ENODEV;
845}
846#endif
847
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500848static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200849{
Shawn Guoabfafc22011-06-30 15:44:44 +0800850 const struct of_device_id *of_id =
851 of_match_device(imx_esdhc_dt_ids, &pdev->dev);
Shawn Guo85d65092011-05-27 23:48:12 +0800852 struct sdhci_pltfm_host *pltfm_host;
853 struct sdhci_host *host;
854 struct esdhc_platform_data *boarddata;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100855 int err;
Richard Zhue1498602011-03-25 09:18:27 -0400856 struct pltfm_imx_data *imx_data;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200857
Christian Daudt0e748232013-05-29 13:50:05 -0700858 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata, 0);
Shawn Guo85d65092011-05-27 23:48:12 +0800859 if (IS_ERR(host))
860 return PTR_ERR(host);
861
862 pltfm_host = sdhci_priv(host);
863
Shawn Guoe3af31c2012-11-26 14:39:43 +0800864 imx_data = devm_kzalloc(&pdev->dev, sizeof(*imx_data), GFP_KERNEL);
Shawn Guoabfafc22011-06-30 15:44:44 +0800865 if (!imx_data) {
866 err = -ENOMEM;
Shawn Guoe3af31c2012-11-26 14:39:43 +0800867 goto free_sdhci;
Shawn Guoabfafc22011-06-30 15:44:44 +0800868 }
Shawn Guo57ed3312011-06-30 09:24:26 +0800869
Shawn Guoabfafc22011-06-30 15:44:44 +0800870 if (of_id)
871 pdev->id_entry = of_id->data;
Shawn Guo57ed3312011-06-30 09:24:26 +0800872 imx_data->devtype = pdev->id_entry->driver_data;
Shawn Guo85d65092011-05-27 23:48:12 +0800873 pltfm_host->priv = imx_data;
874
Shawn Guo31fbb302013-10-17 15:19:44 +0800875 if (is_imx25_esdhc(imx_data) || is_imx35_esdhc(imx_data))
876 imx_data->flags |= ESDHC_FLAG_ENGCM07207;
877
Shawn Guo9d61c002013-10-17 15:19:45 +0800878 if (is_imx6q_usdhc(imx_data))
879 imx_data->flags |= ESDHC_FLAG_USDHC;
880
Sascha Hauer52dac612012-03-07 09:31:34 +0100881 imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
882 if (IS_ERR(imx_data->clk_ipg)) {
883 err = PTR_ERR(imx_data->clk_ipg);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800884 goto free_sdhci;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200885 }
Sascha Hauer52dac612012-03-07 09:31:34 +0100886
887 imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
888 if (IS_ERR(imx_data->clk_ahb)) {
889 err = PTR_ERR(imx_data->clk_ahb);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800890 goto free_sdhci;
Sascha Hauer52dac612012-03-07 09:31:34 +0100891 }
892
893 imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
894 if (IS_ERR(imx_data->clk_per)) {
895 err = PTR_ERR(imx_data->clk_per);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800896 goto free_sdhci;
Sascha Hauer52dac612012-03-07 09:31:34 +0100897 }
898
899 pltfm_host->clk = imx_data->clk_per;
900
901 clk_prepare_enable(imx_data->clk_per);
902 clk_prepare_enable(imx_data->clk_ipg);
903 clk_prepare_enable(imx_data->clk_ahb);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200904
Dong Aishengad932202013-09-13 19:11:35 +0800905 imx_data->pinctrl = devm_pinctrl_get(&pdev->dev);
Dong Aishenge62d8b82012-05-11 14:56:01 +0800906 if (IS_ERR(imx_data->pinctrl)) {
907 err = PTR_ERR(imx_data->pinctrl);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800908 goto disable_clk;
Dong Aishenge62d8b82012-05-11 14:56:01 +0800909 }
910
Dong Aishengad932202013-09-13 19:11:35 +0800911 imx_data->pins_default = pinctrl_lookup_state(imx_data->pinctrl,
912 PINCTRL_STATE_DEFAULT);
913 if (IS_ERR(imx_data->pins_default)) {
914 err = PTR_ERR(imx_data->pins_default);
915 dev_err(mmc_dev(host->mmc), "could not get default state\n");
916 goto disable_clk;
917 }
918
Eric Bénardb89152822012-04-18 02:30:20 +0200919 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
Eric Bénard37865fe2010-10-23 01:57:21 +0200920
Shawn Guo31fbb302013-10-17 15:19:44 +0800921 if (imx_data->flags & ESDHC_FLAG_ENGCM07207)
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100922 /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
Richard Zhu97e4ba62011-08-11 16:51:46 -0400923 host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK
924 | SDHCI_QUIRK_BROKEN_ADMA;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100925
Shawn Guo57ed3312011-06-30 09:24:26 +0800926 if (is_imx53_esdhc(imx_data))
Richard Zhu58ac8172011-03-21 13:22:16 +0800927 imx_data->flags |= ESDHC_FLAG_MULTIBLK_NO_INT;
928
Shawn Guof750ba92011-11-10 16:39:32 +0800929 /*
930 * The imx6q ROM code will change the default watermark level setting
931 * to something insane. Change it back here.
932 */
Shawn Guo9d61c002013-10-17 15:19:45 +0800933 if (esdhc_is_usdhc(imx_data))
Shawn Guo60bf6392013-01-15 23:36:53 +0800934 writel(0x08100810, host->ioaddr + ESDHC_WTMK_LVL);
Shawn Guof750ba92011-11-10 16:39:32 +0800935
Shawn Guo842afc02011-07-06 22:57:48 +0800936 boarddata = &imx_data->boarddata;
Shawn Guoabfafc22011-06-30 15:44:44 +0800937 if (sdhci_esdhc_imx_probe_dt(pdev, boarddata) < 0) {
938 if (!host->mmc->parent->platform_data) {
939 dev_err(mmc_dev(host->mmc), "no board data!\n");
940 err = -EINVAL;
Shawn Guoe3af31c2012-11-26 14:39:43 +0800941 goto disable_clk;
Shawn Guoabfafc22011-06-30 15:44:44 +0800942 }
943 imx_data->boarddata = *((struct esdhc_platform_data *)
944 host->mmc->parent->platform_data);
945 }
Shawn Guo913413c2011-06-21 22:41:51 +0800946
947 /* write_protect */
948 if (boarddata->wp_type == ESDHC_WP_GPIO) {
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800949 err = mmc_gpio_request_ro(host->mmc, boarddata->wp_gpio);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100950 if (err) {
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800951 dev_err(mmc_dev(host->mmc),
952 "failed to request write-protect gpio!\n");
953 goto disable_clk;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100954 }
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800955 host->mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
Shawn Guo913413c2011-06-21 22:41:51 +0800956 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100957
Shawn Guo913413c2011-06-21 22:41:51 +0800958 /* card_detect */
Shawn Guo913413c2011-06-21 22:41:51 +0800959 switch (boarddata->cd_type) {
960 case ESDHC_CD_GPIO:
Laurent Pinchart214fc302013-08-08 12:38:31 +0200961 err = mmc_gpio_request_cd(host->mmc, boarddata->cd_gpio, 0);
Wolfram Sang7e29c302011-02-26 14:44:41 +0100962 if (err) {
Shawn Guo913413c2011-06-21 22:41:51 +0800963 dev_err(mmc_dev(host->mmc),
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800964 "failed to request card-detect gpio!\n");
Shawn Guoe3af31c2012-11-26 14:39:43 +0800965 goto disable_clk;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100966 }
Shawn Guo913413c2011-06-21 22:41:51 +0800967 /* fall through */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100968
Shawn Guo913413c2011-06-21 22:41:51 +0800969 case ESDHC_CD_CONTROLLER:
970 /* we have a working card_detect back */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100971 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
Shawn Guo913413c2011-06-21 22:41:51 +0800972 break;
973
974 case ESDHC_CD_PERMANENT:
975 host->mmc->caps = MMC_CAP_NONREMOVABLE;
976 break;
977
978 case ESDHC_CD_NONE:
979 break;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100980 }
Eric Bénard16a790b2010-10-23 01:57:22 +0200981
Sascha Haueraf510792013-01-21 19:02:28 +0800982 switch (boarddata->max_bus_width) {
983 case 8:
984 host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA;
985 break;
986 case 4:
987 host->mmc->caps |= MMC_CAP_4_BIT_DATA;
988 break;
989 case 1:
990 default:
991 host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
992 break;
993 }
994
Dong Aishengad932202013-09-13 19:11:35 +0800995 /* sdr50 and sdr104 needs work on 1.8v signal voltage */
Shawn Guo9d61c002013-10-17 15:19:45 +0800996 if ((boarddata->support_vsel) && esdhc_is_usdhc(imx_data)) {
Dong Aishengad932202013-09-13 19:11:35 +0800997 imx_data->pins_100mhz = pinctrl_lookup_state(imx_data->pinctrl,
998 ESDHC_PINCTRL_STATE_100MHZ);
999 imx_data->pins_200mhz = pinctrl_lookup_state(imx_data->pinctrl,
1000 ESDHC_PINCTRL_STATE_200MHZ);
1001 if (IS_ERR(imx_data->pins_100mhz) ||
1002 IS_ERR(imx_data->pins_200mhz)) {
1003 dev_warn(mmc_dev(host->mmc),
1004 "could not get ultra high speed state, work on normal mode\n");
1005 /* fall back to not support uhs by specify no 1.8v quirk */
1006 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1007 }
1008 } else {
1009 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1010 }
1011
Shawn Guo85d65092011-05-27 23:48:12 +08001012 err = sdhci_add_host(host);
1013 if (err)
Shawn Guoe3af31c2012-11-26 14:39:43 +08001014 goto disable_clk;
Shawn Guo85d65092011-05-27 23:48:12 +08001015
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001016 return 0;
Wolfram Sang7e29c302011-02-26 14:44:41 +01001017
Shawn Guoe3af31c2012-11-26 14:39:43 +08001018disable_clk:
Sascha Hauer52dac612012-03-07 09:31:34 +01001019 clk_disable_unprepare(imx_data->clk_per);
1020 clk_disable_unprepare(imx_data->clk_ipg);
1021 clk_disable_unprepare(imx_data->clk_ahb);
Shawn Guoe3af31c2012-11-26 14:39:43 +08001022free_sdhci:
Shawn Guo85d65092011-05-27 23:48:12 +08001023 sdhci_pltfm_free(pdev);
1024 return err;
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001025}
1026
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001027static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001028{
Shawn Guo85d65092011-05-27 23:48:12 +08001029 struct sdhci_host *host = platform_get_drvdata(pdev);
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001030 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -04001031 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Shawn Guo85d65092011-05-27 23:48:12 +08001032 int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
1033
1034 sdhci_remove_host(host, dead);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +01001035
Sascha Hauer52dac612012-03-07 09:31:34 +01001036 clk_disable_unprepare(imx_data->clk_per);
1037 clk_disable_unprepare(imx_data->clk_ipg);
1038 clk_disable_unprepare(imx_data->clk_ahb);
1039
Shawn Guo85d65092011-05-27 23:48:12 +08001040 sdhci_pltfm_free(pdev);
1041
1042 return 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001043}
1044
Shawn Guo85d65092011-05-27 23:48:12 +08001045static struct platform_driver sdhci_esdhc_imx_driver = {
1046 .driver = {
1047 .name = "sdhci-esdhc-imx",
1048 .owner = THIS_MODULE,
Shawn Guoabfafc22011-06-30 15:44:44 +08001049 .of_match_table = imx_esdhc_dt_ids,
Manuel Lauss29495aa2011-11-03 11:09:45 +01001050 .pm = SDHCI_PLTFM_PMOPS,
Shawn Guo85d65092011-05-27 23:48:12 +08001051 },
Shawn Guo57ed3312011-06-30 09:24:26 +08001052 .id_table = imx_esdhc_devtype,
Shawn Guo85d65092011-05-27 23:48:12 +08001053 .probe = sdhci_esdhc_imx_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05001054 .remove = sdhci_esdhc_imx_remove,
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001055};
Shawn Guo85d65092011-05-27 23:48:12 +08001056
Axel Lind1f81a62011-11-26 12:55:43 +08001057module_platform_driver(sdhci_esdhc_imx_driver);
Shawn Guo85d65092011-05-27 23:48:12 +08001058
1059MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
1060MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
1061MODULE_LICENSE("GPL v2");