blob: 24daaf4e20baf9bab15fa0254774cb510e0d1dde [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)
37#define ESDHC_WTMK_LVL 0x44
38#define ESDHC_MIX_CTRL 0x48
Shawn Guo2a15f982013-01-21 19:02:26 +080039#define ESDHC_MIX_CTRL_AC23EN (1 << 7)
40/* Bits 3 and 6 are not SDHCI standard definitions */
41#define ESDHC_MIX_CTRL_SDHCI_MASK 0xb7
Richard Zhu58ac8172011-03-21 13:22:16 +080042
Richard Zhu58ac8172011-03-21 13:22:16 +080043/*
Richard Zhu97e4ba62011-08-11 16:51:46 -040044 * There is an INT DMA ERR mis-match between eSDHC and STD SDHC SPEC:
45 * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
46 * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
47 * Define this macro DMA error INT for fsl eSDHC
48 */
Shawn Guo60bf6392013-01-15 23:36:53 +080049#define ESDHC_INT_VENDOR_SPEC_DMA_ERR (1 << 28)
Richard Zhu97e4ba62011-08-11 16:51:46 -040050
51/*
Richard Zhu58ac8172011-03-21 13:22:16 +080052 * The CMDTYPE of the CMD register (offset 0xE) should be set to
53 * "11" when the STOP CMD12 is issued on imx53 to abort one
54 * open ended multi-blk IO. Otherwise the TC INT wouldn't
55 * be generated.
56 * In exact block transfer, the controller doesn't complete the
57 * operations automatically as required at the end of the
58 * transfer and remains on hold if the abort command is not sent.
59 * As a result, the TC flag is not asserted and SW received timeout
60 * exeception. Bit1 of Vendor Spec registor is used to fix it.
61 */
62#define ESDHC_FLAG_MULTIBLK_NO_INT (1 << 1)
Richard Zhue1498602011-03-25 09:18:27 -040063
Shawn Guo57ed3312011-06-30 09:24:26 +080064enum imx_esdhc_type {
65 IMX25_ESDHC,
66 IMX35_ESDHC,
67 IMX51_ESDHC,
68 IMX53_ESDHC,
Shawn Guo95a24822011-09-19 17:32:21 +080069 IMX6Q_USDHC,
Shawn Guo57ed3312011-06-30 09:24:26 +080070};
71
Richard Zhue1498602011-03-25 09:18:27 -040072struct pltfm_imx_data {
73 int flags;
74 u32 scratchpad;
Shawn Guo57ed3312011-06-30 09:24:26 +080075 enum imx_esdhc_type devtype;
Dong Aishenge62d8b82012-05-11 14:56:01 +080076 struct pinctrl *pinctrl;
Shawn Guo842afc02011-07-06 22:57:48 +080077 struct esdhc_platform_data boarddata;
Sascha Hauer52dac612012-03-07 09:31:34 +010078 struct clk *clk_ipg;
79 struct clk *clk_ahb;
80 struct clk *clk_per;
Richard Zhue1498602011-03-25 09:18:27 -040081};
82
Shawn Guo57ed3312011-06-30 09:24:26 +080083static struct platform_device_id imx_esdhc_devtype[] = {
84 {
85 .name = "sdhci-esdhc-imx25",
86 .driver_data = IMX25_ESDHC,
87 }, {
88 .name = "sdhci-esdhc-imx35",
89 .driver_data = IMX35_ESDHC,
90 }, {
91 .name = "sdhci-esdhc-imx51",
92 .driver_data = IMX51_ESDHC,
93 }, {
94 .name = "sdhci-esdhc-imx53",
95 .driver_data = IMX53_ESDHC,
96 }, {
Shawn Guo95a24822011-09-19 17:32:21 +080097 .name = "sdhci-usdhc-imx6q",
98 .driver_data = IMX6Q_USDHC,
99 }, {
Shawn Guo57ed3312011-06-30 09:24:26 +0800100 /* sentinel */
101 }
102};
103MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
104
Shawn Guoabfafc22011-06-30 15:44:44 +0800105static const struct of_device_id imx_esdhc_dt_ids[] = {
106 { .compatible = "fsl,imx25-esdhc", .data = &imx_esdhc_devtype[IMX25_ESDHC], },
107 { .compatible = "fsl,imx35-esdhc", .data = &imx_esdhc_devtype[IMX35_ESDHC], },
108 { .compatible = "fsl,imx51-esdhc", .data = &imx_esdhc_devtype[IMX51_ESDHC], },
109 { .compatible = "fsl,imx53-esdhc", .data = &imx_esdhc_devtype[IMX53_ESDHC], },
Shawn Guo95a24822011-09-19 17:32:21 +0800110 { .compatible = "fsl,imx6q-usdhc", .data = &imx_esdhc_devtype[IMX6Q_USDHC], },
Shawn Guoabfafc22011-06-30 15:44:44 +0800111 { /* sentinel */ }
112};
113MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
114
Shawn Guo57ed3312011-06-30 09:24:26 +0800115static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
116{
117 return data->devtype == IMX25_ESDHC;
118}
119
120static inline int is_imx35_esdhc(struct pltfm_imx_data *data)
121{
122 return data->devtype == IMX35_ESDHC;
123}
124
125static inline int is_imx51_esdhc(struct pltfm_imx_data *data)
126{
127 return data->devtype == IMX51_ESDHC;
128}
129
130static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
131{
132 return data->devtype == IMX53_ESDHC;
133}
134
Shawn Guo95a24822011-09-19 17:32:21 +0800135static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
136{
137 return data->devtype == IMX6Q_USDHC;
138}
139
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200140static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
141{
142 void __iomem *base = host->ioaddr + (reg & ~0x3);
143 u32 shift = (reg & 0x3) * 8;
144
145 writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
146}
147
Wolfram Sang7e29c302011-02-26 14:44:41 +0100148static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
149{
Wolfram Sang7e29c302011-02-26 14:44:41 +0100150 u32 val = readl(host->ioaddr + reg);
151
Richard Zhu97e4ba62011-08-11 16:51:46 -0400152 if (unlikely(reg == SDHCI_CAPABILITIES)) {
153 /* In FSL esdhc IC module, only bit20 is used to indicate the
154 * ADMA2 capability of esdhc, but this bit is messed up on
155 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
156 * don't actually support ADMA2). So set the BROKEN_ADMA
157 * uirk on MX25/35 platforms.
158 */
159
160 if (val & SDHCI_CAN_DO_ADMA1) {
161 val &= ~SDHCI_CAN_DO_ADMA1;
162 val |= SDHCI_CAN_DO_ADMA2;
163 }
164 }
165
166 if (unlikely(reg == SDHCI_INT_STATUS)) {
Shawn Guo60bf6392013-01-15 23:36:53 +0800167 if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
168 val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
Richard Zhu97e4ba62011-08-11 16:51:46 -0400169 val |= SDHCI_INT_ADMA_ERROR;
170 }
171 }
172
Wolfram Sang7e29c302011-02-26 14:44:41 +0100173 return val;
174}
175
176static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
177{
Richard Zhue1498602011-03-25 09:18:27 -0400178 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
179 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Tony Lin0d588642011-08-11 16:45:59 -0400180 u32 data;
Richard Zhue1498602011-03-25 09:18:27 -0400181
Tony Lin0d588642011-08-11 16:45:59 -0400182 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
Tony Lin0d588642011-08-11 16:45:59 -0400183 if (val & SDHCI_INT_CARD_INT) {
184 /*
185 * Clear and then set D3CD bit to avoid missing the
186 * card interrupt. This is a eSDHC controller problem
187 * so we need to apply the following workaround: clear
188 * and set D3CD bit will make eSDHC re-sample the card
189 * interrupt. In case a card interrupt was lost,
190 * re-sample it by the following steps.
191 */
192 data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
Shawn Guo60bf6392013-01-15 23:36:53 +0800193 data &= ~ESDHC_CTRL_D3CD;
Tony Lin0d588642011-08-11 16:45:59 -0400194 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
Shawn Guo60bf6392013-01-15 23:36:53 +0800195 data |= ESDHC_CTRL_D3CD;
Tony Lin0d588642011-08-11 16:45:59 -0400196 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
197 }
198 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100199
Richard Zhu58ac8172011-03-21 13:22:16 +0800200 if (unlikely((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
201 && (reg == SDHCI_INT_STATUS)
202 && (val & SDHCI_INT_DATA_END))) {
203 u32 v;
Shawn Guo60bf6392013-01-15 23:36:53 +0800204 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
205 v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
206 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
Richard Zhu58ac8172011-03-21 13:22:16 +0800207 }
208
Richard Zhu97e4ba62011-08-11 16:51:46 -0400209 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
210 if (val & SDHCI_INT_ADMA_ERROR) {
211 val &= ~SDHCI_INT_ADMA_ERROR;
Shawn Guo60bf6392013-01-15 23:36:53 +0800212 val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
Richard Zhu97e4ba62011-08-11 16:51:46 -0400213 }
214 }
215
Wolfram Sang7e29c302011-02-26 14:44:41 +0100216 writel(val, host->ioaddr + reg);
217}
218
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200219static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
220{
Shawn Guoef4d0882013-01-15 23:30:27 +0800221 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
222 struct pltfm_imx_data *imx_data = pltfm_host->priv;
223
Shawn Guo95a24822011-09-19 17:32:21 +0800224 if (unlikely(reg == SDHCI_HOST_VERSION)) {
Shawn Guoef4d0882013-01-15 23:30:27 +0800225 reg ^= 2;
226 if (is_imx6q_usdhc(imx_data)) {
227 /*
228 * The usdhc register returns a wrong host version.
229 * Correct it here.
230 */
231 return SDHCI_SPEC_300;
232 }
Shawn Guo95a24822011-09-19 17:32:21 +0800233 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200234
235 return readw(host->ioaddr + reg);
236}
237
238static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
239{
240 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400241 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200242
243 switch (reg) {
244 case SDHCI_TRANSFER_MODE:
Richard Zhu58ac8172011-03-21 13:22:16 +0800245 if ((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
246 && (host->cmd->opcode == SD_IO_RW_EXTENDED)
247 && (host->cmd->data->blocks > 1)
248 && (host->cmd->data->flags & MMC_DATA_READ)) {
249 u32 v;
Shawn Guo60bf6392013-01-15 23:36:53 +0800250 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
251 v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
252 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
Richard Zhu58ac8172011-03-21 13:22:16 +0800253 }
Shawn Guo69f54692013-01-21 19:02:24 +0800254
255 if (is_imx6q_usdhc(imx_data)) {
256 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
Shawn Guo2a15f982013-01-21 19:02:26 +0800257 /* Swap AC23 bit */
258 if (val & SDHCI_TRNS_AUTO_CMD23) {
259 val &= ~SDHCI_TRNS_AUTO_CMD23;
260 val |= ESDHC_MIX_CTRL_AC23EN;
261 }
262 m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
Shawn Guo69f54692013-01-21 19:02:24 +0800263 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
264 } else {
265 /*
266 * Postpone this write, we must do it together with a
267 * command write that is down below.
268 */
269 imx_data->scratchpad = val;
270 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200271 return;
272 case SDHCI_COMMAND:
Sascha Hauer5b6b0ad2012-02-17 11:51:49 +0100273 if ((host->cmd->opcode == MMC_STOP_TRANSMISSION ||
274 host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
275 (imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
Richard Zhu58ac8172011-03-21 13:22:16 +0800276 val |= SDHCI_CMD_ABORTCMD;
Shawn Guo95a24822011-09-19 17:32:21 +0800277
Shawn Guo69f54692013-01-21 19:02:24 +0800278 if (is_imx6q_usdhc(imx_data))
Shawn Guo95a24822011-09-19 17:32:21 +0800279 writel(val << 16,
280 host->ioaddr + SDHCI_TRANSFER_MODE);
Shawn Guo69f54692013-01-21 19:02:24 +0800281 else
Shawn Guo95a24822011-09-19 17:32:21 +0800282 writel(val << 16 | imx_data->scratchpad,
283 host->ioaddr + SDHCI_TRANSFER_MODE);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200284 return;
285 case SDHCI_BLOCK_SIZE:
286 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
287 break;
288 }
289 esdhc_clrset_le(host, 0xffff, val, reg);
290}
291
292static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
293{
Wilson Callan9a0985b2012-07-19 02:49:16 -0400294 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
295 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200296 u32 new_val;
297
298 switch (reg) {
299 case SDHCI_POWER_CONTROL:
300 /*
301 * FSL put some DMA bits here
302 * If your board has a regulator, code should be here
303 */
304 return;
305 case SDHCI_HOST_CONTROL:
Shawn Guo6b40d182013-01-15 23:36:52 +0800306 /* FSL messed up here, so we need to manually compose it. */
307 new_val = val & (SDHCI_CTRL_LED | SDHCI_CTRL_4BITBUS);
Masanari Iida7122bbb2012-08-05 23:25:40 +0900308 /* ensure the endianness */
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200309 new_val |= ESDHC_HOST_CONTROL_LE;
Wilson Callan9a0985b2012-07-19 02:49:16 -0400310 /* bits 8&9 are reserved on mx25 */
311 if (!is_imx25_esdhc(imx_data)) {
312 /* DMA mode bits are shifted */
313 new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
314 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200315
316 esdhc_clrset_le(host, 0xffff, new_val, reg);
317 return;
318 }
319 esdhc_clrset_le(host, 0xff, val, reg);
Shawn Guo913413c2011-06-21 22:41:51 +0800320
321 /*
322 * The esdhc has a design violation to SDHC spec which tells
323 * that software reset should not affect card detection circuit.
324 * But esdhc clears its SYSCTL register bits [0..2] during the
325 * software reset. This will stop those clocks that card detection
326 * circuit relies on. To work around it, we turn the clocks on back
327 * to keep card detection circuit functional.
328 */
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800329 if ((reg == SDHCI_SOFTWARE_RESET) && (val & 1)) {
Shawn Guo913413c2011-06-21 22:41:51 +0800330 esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800331 /*
332 * The reset on usdhc fails to clear MIX_CTRL register.
333 * Do it manually here.
334 */
335 if (is_imx6q_usdhc(imx_data))
336 writel(0, host->ioaddr + ESDHC_MIX_CTRL);
337 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200338}
339
340static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
341{
342 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
343
344 return clk_get_rate(pltfm_host->clk);
345}
346
347static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
348{
349 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
350
351 return clk_get_rate(pltfm_host->clk) / 256 / 16;
352}
353
Shawn Guo913413c2011-06-21 22:41:51 +0800354static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
355{
Shawn Guo842afc02011-07-06 22:57:48 +0800356 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
357 struct pltfm_imx_data *imx_data = pltfm_host->priv;
358 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Shawn Guo913413c2011-06-21 22:41:51 +0800359
360 switch (boarddata->wp_type) {
361 case ESDHC_WP_GPIO:
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800362 return mmc_gpio_get_ro(host->mmc);
Shawn Guo913413c2011-06-21 22:41:51 +0800363 case ESDHC_WP_CONTROLLER:
364 return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
365 SDHCI_WRITE_PROTECT);
366 case ESDHC_WP_NONE:
367 break;
368 }
369
370 return -ENOSYS;
371}
372
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100373static struct sdhci_ops sdhci_esdhc_ops = {
Richard Zhue1498602011-03-25 09:18:27 -0400374 .read_l = esdhc_readl_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100375 .read_w = esdhc_readw_le,
Richard Zhue1498602011-03-25 09:18:27 -0400376 .write_l = esdhc_writel_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100377 .write_w = esdhc_writew_le,
378 .write_b = esdhc_writeb_le,
379 .set_clock = esdhc_set_clock,
380 .get_max_clock = esdhc_pltfm_get_max_clock,
381 .get_min_clock = esdhc_pltfm_get_min_clock,
Shawn Guo913413c2011-06-21 22:41:51 +0800382 .get_ro = esdhc_pltfm_get_ro,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100383};
384
Shawn Guo85d65092011-05-27 23:48:12 +0800385static struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
Richard Zhu97e4ba62011-08-11 16:51:46 -0400386 .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
387 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
388 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
Shawn Guo85d65092011-05-27 23:48:12 +0800389 | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
Shawn Guo85d65092011-05-27 23:48:12 +0800390 .ops = &sdhci_esdhc_ops,
391};
392
Shawn Guoabfafc22011-06-30 15:44:44 +0800393#ifdef CONFIG_OF
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500394static int
Shawn Guoabfafc22011-06-30 15:44:44 +0800395sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
396 struct esdhc_platform_data *boarddata)
397{
398 struct device_node *np = pdev->dev.of_node;
399
400 if (!np)
401 return -ENODEV;
402
Arnd Bergmann7f217792012-05-13 00:14:24 -0400403 if (of_get_property(np, "non-removable", NULL))
Shawn Guoabfafc22011-06-30 15:44:44 +0800404 boarddata->cd_type = ESDHC_CD_PERMANENT;
405
406 if (of_get_property(np, "fsl,cd-controller", NULL))
407 boarddata->cd_type = ESDHC_CD_CONTROLLER;
408
409 if (of_get_property(np, "fsl,wp-controller", NULL))
410 boarddata->wp_type = ESDHC_WP_CONTROLLER;
411
412 boarddata->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
413 if (gpio_is_valid(boarddata->cd_gpio))
414 boarddata->cd_type = ESDHC_CD_GPIO;
415
416 boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
417 if (gpio_is_valid(boarddata->wp_gpio))
418 boarddata->wp_type = ESDHC_WP_GPIO;
419
420 return 0;
421}
422#else
423static inline int
424sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
425 struct esdhc_platform_data *boarddata)
426{
427 return -ENODEV;
428}
429#endif
430
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500431static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200432{
Shawn Guoabfafc22011-06-30 15:44:44 +0800433 const struct of_device_id *of_id =
434 of_match_device(imx_esdhc_dt_ids, &pdev->dev);
Shawn Guo85d65092011-05-27 23:48:12 +0800435 struct sdhci_pltfm_host *pltfm_host;
436 struct sdhci_host *host;
437 struct esdhc_platform_data *boarddata;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100438 int err;
Richard Zhue1498602011-03-25 09:18:27 -0400439 struct pltfm_imx_data *imx_data;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200440
Shawn Guo85d65092011-05-27 23:48:12 +0800441 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata);
442 if (IS_ERR(host))
443 return PTR_ERR(host);
444
445 pltfm_host = sdhci_priv(host);
446
Shawn Guoe3af31c2012-11-26 14:39:43 +0800447 imx_data = devm_kzalloc(&pdev->dev, sizeof(*imx_data), GFP_KERNEL);
Shawn Guoabfafc22011-06-30 15:44:44 +0800448 if (!imx_data) {
449 err = -ENOMEM;
Shawn Guoe3af31c2012-11-26 14:39:43 +0800450 goto free_sdhci;
Shawn Guoabfafc22011-06-30 15:44:44 +0800451 }
Shawn Guo57ed3312011-06-30 09:24:26 +0800452
Shawn Guoabfafc22011-06-30 15:44:44 +0800453 if (of_id)
454 pdev->id_entry = of_id->data;
Shawn Guo57ed3312011-06-30 09:24:26 +0800455 imx_data->devtype = pdev->id_entry->driver_data;
Shawn Guo85d65092011-05-27 23:48:12 +0800456 pltfm_host->priv = imx_data;
457
Sascha Hauer52dac612012-03-07 09:31:34 +0100458 imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
459 if (IS_ERR(imx_data->clk_ipg)) {
460 err = PTR_ERR(imx_data->clk_ipg);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800461 goto free_sdhci;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200462 }
Sascha Hauer52dac612012-03-07 09:31:34 +0100463
464 imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
465 if (IS_ERR(imx_data->clk_ahb)) {
466 err = PTR_ERR(imx_data->clk_ahb);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800467 goto free_sdhci;
Sascha Hauer52dac612012-03-07 09:31:34 +0100468 }
469
470 imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
471 if (IS_ERR(imx_data->clk_per)) {
472 err = PTR_ERR(imx_data->clk_per);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800473 goto free_sdhci;
Sascha Hauer52dac612012-03-07 09:31:34 +0100474 }
475
476 pltfm_host->clk = imx_data->clk_per;
477
478 clk_prepare_enable(imx_data->clk_per);
479 clk_prepare_enable(imx_data->clk_ipg);
480 clk_prepare_enable(imx_data->clk_ahb);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200481
Dong Aishenge62d8b82012-05-11 14:56:01 +0800482 imx_data->pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
483 if (IS_ERR(imx_data->pinctrl)) {
484 err = PTR_ERR(imx_data->pinctrl);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800485 goto disable_clk;
Dong Aishenge62d8b82012-05-11 14:56:01 +0800486 }
487
Eric Bénardb8915282012-04-18 02:30:20 +0200488 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
Eric Bénard37865fe2010-10-23 01:57:21 +0200489
Shawn Guo57ed3312011-06-30 09:24:26 +0800490 if (is_imx25_esdhc(imx_data) || is_imx35_esdhc(imx_data))
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100491 /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
Richard Zhu97e4ba62011-08-11 16:51:46 -0400492 host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK
493 | SDHCI_QUIRK_BROKEN_ADMA;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100494
Shawn Guo57ed3312011-06-30 09:24:26 +0800495 if (is_imx53_esdhc(imx_data))
Richard Zhu58ac8172011-03-21 13:22:16 +0800496 imx_data->flags |= ESDHC_FLAG_MULTIBLK_NO_INT;
497
Shawn Guof750ba92011-11-10 16:39:32 +0800498 /*
499 * The imx6q ROM code will change the default watermark level setting
500 * to something insane. Change it back here.
501 */
502 if (is_imx6q_usdhc(imx_data))
Shawn Guo60bf6392013-01-15 23:36:53 +0800503 writel(0x08100810, host->ioaddr + ESDHC_WTMK_LVL);
Shawn Guof750ba92011-11-10 16:39:32 +0800504
Shawn Guo842afc02011-07-06 22:57:48 +0800505 boarddata = &imx_data->boarddata;
Shawn Guoabfafc22011-06-30 15:44:44 +0800506 if (sdhci_esdhc_imx_probe_dt(pdev, boarddata) < 0) {
507 if (!host->mmc->parent->platform_data) {
508 dev_err(mmc_dev(host->mmc), "no board data!\n");
509 err = -EINVAL;
Shawn Guoe3af31c2012-11-26 14:39:43 +0800510 goto disable_clk;
Shawn Guoabfafc22011-06-30 15:44:44 +0800511 }
512 imx_data->boarddata = *((struct esdhc_platform_data *)
513 host->mmc->parent->platform_data);
514 }
Shawn Guo913413c2011-06-21 22:41:51 +0800515
516 /* write_protect */
517 if (boarddata->wp_type == ESDHC_WP_GPIO) {
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800518 err = mmc_gpio_request_ro(host->mmc, boarddata->wp_gpio);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100519 if (err) {
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800520 dev_err(mmc_dev(host->mmc),
521 "failed to request write-protect gpio!\n");
522 goto disable_clk;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100523 }
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800524 host->mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
Shawn Guo913413c2011-06-21 22:41:51 +0800525 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100526
Shawn Guo913413c2011-06-21 22:41:51 +0800527 /* card_detect */
Shawn Guo913413c2011-06-21 22:41:51 +0800528 switch (boarddata->cd_type) {
529 case ESDHC_CD_GPIO:
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800530 err = mmc_gpio_request_cd(host->mmc, boarddata->cd_gpio);
Wolfram Sang7e29c302011-02-26 14:44:41 +0100531 if (err) {
Shawn Guo913413c2011-06-21 22:41:51 +0800532 dev_err(mmc_dev(host->mmc),
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800533 "failed to request card-detect gpio!\n");
Shawn Guoe3af31c2012-11-26 14:39:43 +0800534 goto disable_clk;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100535 }
Shawn Guo913413c2011-06-21 22:41:51 +0800536 /* fall through */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100537
Shawn Guo913413c2011-06-21 22:41:51 +0800538 case ESDHC_CD_CONTROLLER:
539 /* we have a working card_detect back */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100540 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
Shawn Guo913413c2011-06-21 22:41:51 +0800541 break;
542
543 case ESDHC_CD_PERMANENT:
544 host->mmc->caps = MMC_CAP_NONREMOVABLE;
545 break;
546
547 case ESDHC_CD_NONE:
548 break;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100549 }
Eric Bénard16a790b2010-10-23 01:57:22 +0200550
Shawn Guo85d65092011-05-27 23:48:12 +0800551 err = sdhci_add_host(host);
552 if (err)
Shawn Guoe3af31c2012-11-26 14:39:43 +0800553 goto disable_clk;
Shawn Guo85d65092011-05-27 23:48:12 +0800554
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200555 return 0;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100556
Shawn Guoe3af31c2012-11-26 14:39:43 +0800557disable_clk:
Sascha Hauer52dac612012-03-07 09:31:34 +0100558 clk_disable_unprepare(imx_data->clk_per);
559 clk_disable_unprepare(imx_data->clk_ipg);
560 clk_disable_unprepare(imx_data->clk_ahb);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800561free_sdhci:
Shawn Guo85d65092011-05-27 23:48:12 +0800562 sdhci_pltfm_free(pdev);
563 return err;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200564}
565
Bill Pemberton6e0ee712012-11-19 13:26:03 -0500566static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200567{
Shawn Guo85d65092011-05-27 23:48:12 +0800568 struct sdhci_host *host = platform_get_drvdata(pdev);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200569 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400570 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Shawn Guo85d65092011-05-27 23:48:12 +0800571 int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
572
573 sdhci_remove_host(host, dead);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100574
Sascha Hauer52dac612012-03-07 09:31:34 +0100575 clk_disable_unprepare(imx_data->clk_per);
576 clk_disable_unprepare(imx_data->clk_ipg);
577 clk_disable_unprepare(imx_data->clk_ahb);
578
Shawn Guo85d65092011-05-27 23:48:12 +0800579 sdhci_pltfm_free(pdev);
580
581 return 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200582}
583
Shawn Guo85d65092011-05-27 23:48:12 +0800584static struct platform_driver sdhci_esdhc_imx_driver = {
585 .driver = {
586 .name = "sdhci-esdhc-imx",
587 .owner = THIS_MODULE,
Shawn Guoabfafc22011-06-30 15:44:44 +0800588 .of_match_table = imx_esdhc_dt_ids,
Manuel Lauss29495aa2011-11-03 11:09:45 +0100589 .pm = SDHCI_PLTFM_PMOPS,
Shawn Guo85d65092011-05-27 23:48:12 +0800590 },
Shawn Guo57ed3312011-06-30 09:24:26 +0800591 .id_table = imx_esdhc_devtype,
Shawn Guo85d65092011-05-27 23:48:12 +0800592 .probe = sdhci_esdhc_imx_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -0500593 .remove = sdhci_esdhc_imx_remove,
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200594};
Shawn Guo85d65092011-05-27 23:48:12 +0800595
Axel Lind1f81a62011-11-26 12:55:43 +0800596module_platform_driver(sdhci_esdhc_imx_driver);
Shawn Guo85d65092011-05-27 23:48:12 +0800597
598MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
599MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
600MODULE_LICENSE("GPL v2");