blob: d190d04636a714e87da50a3f8748b17347ca225f [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 Guoabfafc22011-06-30 15:44:44 +080024#include <linux/of.h>
25#include <linux/of_device.h>
26#include <linux/of_gpio.h>
Dong Aishenge62d8b82012-05-11 14:56:01 +080027#include <linux/pinctrl/consumer.h>
Wolfram Sang0c6d49c2011-02-26 14:44:39 +010028#include <mach/esdhc.h>
Wolfram Sang95f25ef2010-10-15 12:21:04 +020029#include "sdhci-pltfm.h"
30#include "sdhci-esdhc.h"
31
Tony Lin0d588642011-08-11 16:45:59 -040032#define SDHCI_CTRL_D3CD 0x08
Richard Zhu58ac8172011-03-21 13:22:16 +080033/* VENDOR SPEC register */
34#define SDHCI_VENDOR_SPEC 0xC0
35#define SDHCI_VENDOR_SPEC_SDIO_QUIRK 0x00000002
Shawn Guof750ba92011-11-10 16:39:32 +080036#define SDHCI_WTMK_LVL 0x44
Shawn Guo95a24822011-09-19 17:32:21 +080037#define SDHCI_MIX_CTRL 0x48
Richard Zhu58ac8172011-03-21 13:22:16 +080038
Richard Zhu58ac8172011-03-21 13:22:16 +080039/*
Richard Zhu97e4ba62011-08-11 16:51:46 -040040 * There is an INT DMA ERR mis-match between eSDHC and STD SDHC SPEC:
41 * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
42 * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
43 * Define this macro DMA error INT for fsl eSDHC
44 */
45#define SDHCI_INT_VENDOR_SPEC_DMA_ERR 0x10000000
46
47/*
Richard Zhu58ac8172011-03-21 13:22:16 +080048 * The CMDTYPE of the CMD register (offset 0xE) should be set to
49 * "11" when the STOP CMD12 is issued on imx53 to abort one
50 * open ended multi-blk IO. Otherwise the TC INT wouldn't
51 * be generated.
52 * In exact block transfer, the controller doesn't complete the
53 * operations automatically as required at the end of the
54 * transfer and remains on hold if the abort command is not sent.
55 * As a result, the TC flag is not asserted and SW received timeout
56 * exeception. Bit1 of Vendor Spec registor is used to fix it.
57 */
58#define ESDHC_FLAG_MULTIBLK_NO_INT (1 << 1)
Richard Zhue1498602011-03-25 09:18:27 -040059
Shawn Guo57ed3312011-06-30 09:24:26 +080060enum imx_esdhc_type {
61 IMX25_ESDHC,
62 IMX35_ESDHC,
63 IMX51_ESDHC,
64 IMX53_ESDHC,
Shawn Guo95a24822011-09-19 17:32:21 +080065 IMX6Q_USDHC,
Shawn Guo57ed3312011-06-30 09:24:26 +080066};
67
Richard Zhue1498602011-03-25 09:18:27 -040068struct pltfm_imx_data {
69 int flags;
70 u32 scratchpad;
Shawn Guo57ed3312011-06-30 09:24:26 +080071 enum imx_esdhc_type devtype;
Dong Aishenge62d8b82012-05-11 14:56:01 +080072 struct pinctrl *pinctrl;
Shawn Guo842afc02011-07-06 22:57:48 +080073 struct esdhc_platform_data boarddata;
Richard Zhue1498602011-03-25 09:18:27 -040074};
75
Shawn Guo57ed3312011-06-30 09:24:26 +080076static struct platform_device_id imx_esdhc_devtype[] = {
77 {
78 .name = "sdhci-esdhc-imx25",
79 .driver_data = IMX25_ESDHC,
80 }, {
81 .name = "sdhci-esdhc-imx35",
82 .driver_data = IMX35_ESDHC,
83 }, {
84 .name = "sdhci-esdhc-imx51",
85 .driver_data = IMX51_ESDHC,
86 }, {
87 .name = "sdhci-esdhc-imx53",
88 .driver_data = IMX53_ESDHC,
89 }, {
Shawn Guo95a24822011-09-19 17:32:21 +080090 .name = "sdhci-usdhc-imx6q",
91 .driver_data = IMX6Q_USDHC,
92 }, {
Shawn Guo57ed3312011-06-30 09:24:26 +080093 /* sentinel */
94 }
95};
96MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
97
Shawn Guoabfafc22011-06-30 15:44:44 +080098static const struct of_device_id imx_esdhc_dt_ids[] = {
99 { .compatible = "fsl,imx25-esdhc", .data = &imx_esdhc_devtype[IMX25_ESDHC], },
100 { .compatible = "fsl,imx35-esdhc", .data = &imx_esdhc_devtype[IMX35_ESDHC], },
101 { .compatible = "fsl,imx51-esdhc", .data = &imx_esdhc_devtype[IMX51_ESDHC], },
102 { .compatible = "fsl,imx53-esdhc", .data = &imx_esdhc_devtype[IMX53_ESDHC], },
Shawn Guo95a24822011-09-19 17:32:21 +0800103 { .compatible = "fsl,imx6q-usdhc", .data = &imx_esdhc_devtype[IMX6Q_USDHC], },
Shawn Guoabfafc22011-06-30 15:44:44 +0800104 { /* sentinel */ }
105};
106MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
107
Shawn Guo57ed3312011-06-30 09:24:26 +0800108static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
109{
110 return data->devtype == IMX25_ESDHC;
111}
112
113static inline int is_imx35_esdhc(struct pltfm_imx_data *data)
114{
115 return data->devtype == IMX35_ESDHC;
116}
117
118static inline int is_imx51_esdhc(struct pltfm_imx_data *data)
119{
120 return data->devtype == IMX51_ESDHC;
121}
122
123static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
124{
125 return data->devtype == IMX53_ESDHC;
126}
127
Shawn Guo95a24822011-09-19 17:32:21 +0800128static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
129{
130 return data->devtype == IMX6Q_USDHC;
131}
132
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200133static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
134{
135 void __iomem *base = host->ioaddr + (reg & ~0x3);
136 u32 shift = (reg & 0x3) * 8;
137
138 writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
139}
140
Wolfram Sang7e29c302011-02-26 14:44:41 +0100141static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
142{
Shawn Guo842afc02011-07-06 22:57:48 +0800143 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
144 struct pltfm_imx_data *imx_data = pltfm_host->priv;
145 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Richard Zhue1498602011-03-25 09:18:27 -0400146
Shawn Guo913413c2011-06-21 22:41:51 +0800147 /* fake CARD_PRESENT flag */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100148 u32 val = readl(host->ioaddr + reg);
149
Richard Zhue1498602011-03-25 09:18:27 -0400150 if (unlikely((reg == SDHCI_PRESENT_STATE)
Shawn Guo913413c2011-06-21 22:41:51 +0800151 && gpio_is_valid(boarddata->cd_gpio))) {
152 if (gpio_get_value(boarddata->cd_gpio))
Wolfram Sang7e29c302011-02-26 14:44:41 +0100153 /* no card, if a valid gpio says so... */
Shawn Guo803862a2011-06-21 22:41:49 +0800154 val &= ~SDHCI_CARD_PRESENT;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100155 else
156 /* ... in all other cases assume card is present */
157 val |= SDHCI_CARD_PRESENT;
158 }
159
Richard Zhu97e4ba62011-08-11 16:51:46 -0400160 if (unlikely(reg == SDHCI_CAPABILITIES)) {
161 /* In FSL esdhc IC module, only bit20 is used to indicate the
162 * ADMA2 capability of esdhc, but this bit is messed up on
163 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
164 * don't actually support ADMA2). So set the BROKEN_ADMA
165 * uirk on MX25/35 platforms.
166 */
167
168 if (val & SDHCI_CAN_DO_ADMA1) {
169 val &= ~SDHCI_CAN_DO_ADMA1;
170 val |= SDHCI_CAN_DO_ADMA2;
171 }
172 }
173
174 if (unlikely(reg == SDHCI_INT_STATUS)) {
175 if (val & SDHCI_INT_VENDOR_SPEC_DMA_ERR) {
176 val &= ~SDHCI_INT_VENDOR_SPEC_DMA_ERR;
177 val |= SDHCI_INT_ADMA_ERROR;
178 }
179 }
180
Wolfram Sang7e29c302011-02-26 14:44:41 +0100181 return val;
182}
183
184static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
185{
Richard Zhue1498602011-03-25 09:18:27 -0400186 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
187 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Shawn Guo842afc02011-07-06 22:57:48 +0800188 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Tony Lin0d588642011-08-11 16:45:59 -0400189 u32 data;
Richard Zhue1498602011-03-25 09:18:27 -0400190
Tony Lin0d588642011-08-11 16:45:59 -0400191 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
192 if (boarddata->cd_type == ESDHC_CD_GPIO)
193 /*
194 * These interrupts won't work with a custom
195 * card_detect gpio (only applied to mx25/35)
196 */
197 val &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
198
199 if (val & SDHCI_INT_CARD_INT) {
200 /*
201 * Clear and then set D3CD bit to avoid missing the
202 * card interrupt. This is a eSDHC controller problem
203 * so we need to apply the following workaround: clear
204 * and set D3CD bit will make eSDHC re-sample the card
205 * interrupt. In case a card interrupt was lost,
206 * re-sample it by the following steps.
207 */
208 data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
209 data &= ~SDHCI_CTRL_D3CD;
210 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
211 data |= SDHCI_CTRL_D3CD;
212 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
213 }
214 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100215
Richard Zhu58ac8172011-03-21 13:22:16 +0800216 if (unlikely((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
217 && (reg == SDHCI_INT_STATUS)
218 && (val & SDHCI_INT_DATA_END))) {
219 u32 v;
220 v = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
221 v &= ~SDHCI_VENDOR_SPEC_SDIO_QUIRK;
222 writel(v, host->ioaddr + SDHCI_VENDOR_SPEC);
223 }
224
Richard Zhu97e4ba62011-08-11 16:51:46 -0400225 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
226 if (val & SDHCI_INT_ADMA_ERROR) {
227 val &= ~SDHCI_INT_ADMA_ERROR;
228 val |= SDHCI_INT_VENDOR_SPEC_DMA_ERR;
229 }
230 }
231
Wolfram Sang7e29c302011-02-26 14:44:41 +0100232 writel(val, host->ioaddr + reg);
233}
234
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200235static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
236{
Shawn Guo95a24822011-09-19 17:32:21 +0800237 if (unlikely(reg == SDHCI_HOST_VERSION)) {
238 u16 val = readw(host->ioaddr + (reg ^ 2));
239 /*
240 * uSDHC supports SDHCI v3.0, but it's encoded as value
241 * 0x3 in host controller version register, which violates
242 * SDHCI_SPEC_300 definition. Work it around here.
243 */
244 if ((val & SDHCI_SPEC_VER_MASK) == 3)
245 return --val;
246 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200247
248 return readw(host->ioaddr + reg);
249}
250
251static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
252{
253 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400254 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200255
256 switch (reg) {
257 case SDHCI_TRANSFER_MODE:
258 /*
259 * Postpone this write, we must do it together with a
260 * command write that is down below.
261 */
Richard Zhu58ac8172011-03-21 13:22:16 +0800262 if ((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
263 && (host->cmd->opcode == SD_IO_RW_EXTENDED)
264 && (host->cmd->data->blocks > 1)
265 && (host->cmd->data->flags & MMC_DATA_READ)) {
266 u32 v;
267 v = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
268 v |= SDHCI_VENDOR_SPEC_SDIO_QUIRK;
269 writel(v, host->ioaddr + SDHCI_VENDOR_SPEC);
270 }
Richard Zhue1498602011-03-25 09:18:27 -0400271 imx_data->scratchpad = val;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200272 return;
273 case SDHCI_COMMAND:
Sascha Hauer5b6b0ad2012-02-17 11:51:49 +0100274 if ((host->cmd->opcode == MMC_STOP_TRANSMISSION ||
275 host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
276 (imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
Richard Zhu58ac8172011-03-21 13:22:16 +0800277 val |= SDHCI_CMD_ABORTCMD;
Shawn Guo95a24822011-09-19 17:32:21 +0800278
279 if (is_imx6q_usdhc(imx_data)) {
280 u32 m = readl(host->ioaddr + SDHCI_MIX_CTRL);
281 m = imx_data->scratchpad | (m & 0xffff0000);
282 writel(m, host->ioaddr + SDHCI_MIX_CTRL);
283 writel(val << 16,
284 host->ioaddr + SDHCI_TRANSFER_MODE);
285 } else {
286 writel(val << 16 | imx_data->scratchpad,
287 host->ioaddr + SDHCI_TRANSFER_MODE);
288 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200289 return;
290 case SDHCI_BLOCK_SIZE:
291 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
292 break;
293 }
294 esdhc_clrset_le(host, 0xffff, val, reg);
295}
296
297static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
298{
299 u32 new_val;
300
301 switch (reg) {
302 case SDHCI_POWER_CONTROL:
303 /*
304 * FSL put some DMA bits here
305 * If your board has a regulator, code should be here
306 */
307 return;
308 case SDHCI_HOST_CONTROL:
Tony Lin0d588642011-08-11 16:45:59 -0400309 /* FSL messed up here, so we can just keep those three */
310 new_val = val & (SDHCI_CTRL_LED | \
311 SDHCI_CTRL_4BITBUS | \
312 SDHCI_CTRL_D3CD);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200313 /* ensure the endianess */
314 new_val |= ESDHC_HOST_CONTROL_LE;
315 /* DMA mode bits are shifted */
316 new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
317
318 esdhc_clrset_le(host, 0xffff, new_val, reg);
319 return;
320 }
321 esdhc_clrset_le(host, 0xff, val, reg);
Shawn Guo913413c2011-06-21 22:41:51 +0800322
323 /*
324 * The esdhc has a design violation to SDHC spec which tells
325 * that software reset should not affect card detection circuit.
326 * But esdhc clears its SYSCTL register bits [0..2] during the
327 * software reset. This will stop those clocks that card detection
328 * circuit relies on. To work around it, we turn the clocks on back
329 * to keep card detection circuit functional.
330 */
331 if ((reg == SDHCI_SOFTWARE_RESET) && (val & 1))
332 esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200333}
334
335static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
336{
337 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
338
339 return clk_get_rate(pltfm_host->clk);
340}
341
342static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
343{
344 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
345
346 return clk_get_rate(pltfm_host->clk) / 256 / 16;
347}
348
Shawn Guo913413c2011-06-21 22:41:51 +0800349static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
350{
Shawn Guo842afc02011-07-06 22:57:48 +0800351 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
352 struct pltfm_imx_data *imx_data = pltfm_host->priv;
353 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Shawn Guo913413c2011-06-21 22:41:51 +0800354
355 switch (boarddata->wp_type) {
356 case ESDHC_WP_GPIO:
357 if (gpio_is_valid(boarddata->wp_gpio))
358 return gpio_get_value(boarddata->wp_gpio);
359 case ESDHC_WP_CONTROLLER:
360 return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
361 SDHCI_WRITE_PROTECT);
362 case ESDHC_WP_NONE:
363 break;
364 }
365
366 return -ENOSYS;
367}
368
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100369static struct sdhci_ops sdhci_esdhc_ops = {
Richard Zhue1498602011-03-25 09:18:27 -0400370 .read_l = esdhc_readl_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100371 .read_w = esdhc_readw_le,
Richard Zhue1498602011-03-25 09:18:27 -0400372 .write_l = esdhc_writel_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100373 .write_w = esdhc_writew_le,
374 .write_b = esdhc_writeb_le,
375 .set_clock = esdhc_set_clock,
376 .get_max_clock = esdhc_pltfm_get_max_clock,
377 .get_min_clock = esdhc_pltfm_get_min_clock,
Shawn Guo913413c2011-06-21 22:41:51 +0800378 .get_ro = esdhc_pltfm_get_ro,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100379};
380
Shawn Guo85d65092011-05-27 23:48:12 +0800381static struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
Richard Zhu97e4ba62011-08-11 16:51:46 -0400382 .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
383 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
384 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
Shawn Guo85d65092011-05-27 23:48:12 +0800385 | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
Shawn Guo85d65092011-05-27 23:48:12 +0800386 .ops = &sdhci_esdhc_ops,
387};
388
Wolfram Sang7e29c302011-02-26 14:44:41 +0100389static irqreturn_t cd_irq(int irq, void *data)
390{
391 struct sdhci_host *sdhost = (struct sdhci_host *)data;
392
393 tasklet_schedule(&sdhost->card_tasklet);
394 return IRQ_HANDLED;
395};
396
Shawn Guoabfafc22011-06-30 15:44:44 +0800397#ifdef CONFIG_OF
398static int __devinit
399sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
400 struct esdhc_platform_data *boarddata)
401{
402 struct device_node *np = pdev->dev.of_node;
403
404 if (!np)
405 return -ENODEV;
406
407 if (of_get_property(np, "fsl,card-wired", NULL))
408 boarddata->cd_type = ESDHC_CD_PERMANENT;
409
410 if (of_get_property(np, "fsl,cd-controller", NULL))
411 boarddata->cd_type = ESDHC_CD_CONTROLLER;
412
413 if (of_get_property(np, "fsl,wp-controller", NULL))
414 boarddata->wp_type = ESDHC_WP_CONTROLLER;
415
416 boarddata->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
417 if (gpio_is_valid(boarddata->cd_gpio))
418 boarddata->cd_type = ESDHC_CD_GPIO;
419
420 boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
421 if (gpio_is_valid(boarddata->wp_gpio))
422 boarddata->wp_type = ESDHC_WP_GPIO;
423
424 return 0;
425}
426#else
427static inline int
428sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
429 struct esdhc_platform_data *boarddata)
430{
431 return -ENODEV;
432}
433#endif
434
Shawn Guo85d65092011-05-27 23:48:12 +0800435static int __devinit sdhci_esdhc_imx_probe(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200436{
Shawn Guoabfafc22011-06-30 15:44:44 +0800437 const struct of_device_id *of_id =
438 of_match_device(imx_esdhc_dt_ids, &pdev->dev);
Shawn Guo85d65092011-05-27 23:48:12 +0800439 struct sdhci_pltfm_host *pltfm_host;
440 struct sdhci_host *host;
441 struct esdhc_platform_data *boarddata;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200442 struct clk *clk;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100443 int err;
Richard Zhue1498602011-03-25 09:18:27 -0400444 struct pltfm_imx_data *imx_data;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200445
Shawn Guo85d65092011-05-27 23:48:12 +0800446 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata);
447 if (IS_ERR(host))
448 return PTR_ERR(host);
449
450 pltfm_host = sdhci_priv(host);
451
452 imx_data = kzalloc(sizeof(struct pltfm_imx_data), GFP_KERNEL);
Shawn Guoabfafc22011-06-30 15:44:44 +0800453 if (!imx_data) {
454 err = -ENOMEM;
455 goto err_imx_data;
456 }
Shawn Guo57ed3312011-06-30 09:24:26 +0800457
Shawn Guoabfafc22011-06-30 15:44:44 +0800458 if (of_id)
459 pdev->id_entry = of_id->data;
Shawn Guo57ed3312011-06-30 09:24:26 +0800460 imx_data->devtype = pdev->id_entry->driver_data;
Shawn Guo85d65092011-05-27 23:48:12 +0800461 pltfm_host->priv = imx_data;
462
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200463 clk = clk_get(mmc_dev(host->mmc), NULL);
464 if (IS_ERR(clk)) {
465 dev_err(mmc_dev(host->mmc), "clk err\n");
Shawn Guo85d65092011-05-27 23:48:12 +0800466 err = PTR_ERR(clk);
467 goto err_clk_get;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200468 }
Richard Zhao5ef97fa2011-11-15 14:48:09 +0800469 clk_prepare_enable(clk);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200470 pltfm_host->clk = clk;
471
Dong Aishenge62d8b82012-05-11 14:56:01 +0800472 imx_data->pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
473 if (IS_ERR(imx_data->pinctrl)) {
474 err = PTR_ERR(imx_data->pinctrl);
475 goto pin_err;
476 }
477
Eric Bénardb89152822012-04-18 02:30:20 +0200478 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
Eric Bénard37865fe2010-10-23 01:57:21 +0200479
Shawn Guo57ed3312011-06-30 09:24:26 +0800480 if (is_imx25_esdhc(imx_data) || is_imx35_esdhc(imx_data))
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100481 /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
Richard Zhu97e4ba62011-08-11 16:51:46 -0400482 host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK
483 | SDHCI_QUIRK_BROKEN_ADMA;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100484
Shawn Guo57ed3312011-06-30 09:24:26 +0800485 if (is_imx53_esdhc(imx_data))
Richard Zhu58ac8172011-03-21 13:22:16 +0800486 imx_data->flags |= ESDHC_FLAG_MULTIBLK_NO_INT;
487
Shawn Guof750ba92011-11-10 16:39:32 +0800488 /*
489 * The imx6q ROM code will change the default watermark level setting
490 * to something insane. Change it back here.
491 */
492 if (is_imx6q_usdhc(imx_data))
493 writel(0x08100810, host->ioaddr + SDHCI_WTMK_LVL);
494
Shawn Guo842afc02011-07-06 22:57:48 +0800495 boarddata = &imx_data->boarddata;
Shawn Guoabfafc22011-06-30 15:44:44 +0800496 if (sdhci_esdhc_imx_probe_dt(pdev, boarddata) < 0) {
497 if (!host->mmc->parent->platform_data) {
498 dev_err(mmc_dev(host->mmc), "no board data!\n");
499 err = -EINVAL;
500 goto no_board_data;
501 }
502 imx_data->boarddata = *((struct esdhc_platform_data *)
503 host->mmc->parent->platform_data);
504 }
Shawn Guo913413c2011-06-21 22:41:51 +0800505
506 /* write_protect */
507 if (boarddata->wp_type == ESDHC_WP_GPIO) {
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100508 err = gpio_request_one(boarddata->wp_gpio, GPIOF_IN, "ESDHC_WP");
509 if (err) {
510 dev_warn(mmc_dev(host->mmc),
Shawn Guo913413c2011-06-21 22:41:51 +0800511 "no write-protect pin available!\n");
512 boarddata->wp_gpio = -EINVAL;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100513 }
Shawn Guo913413c2011-06-21 22:41:51 +0800514 } else {
515 boarddata->wp_gpio = -EINVAL;
516 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100517
Shawn Guo913413c2011-06-21 22:41:51 +0800518 /* card_detect */
519 if (boarddata->cd_type != ESDHC_CD_GPIO)
520 boarddata->cd_gpio = -EINVAL;
521
522 switch (boarddata->cd_type) {
523 case ESDHC_CD_GPIO:
Wolfram Sang7e29c302011-02-26 14:44:41 +0100524 err = gpio_request_one(boarddata->cd_gpio, GPIOF_IN, "ESDHC_CD");
525 if (err) {
Shawn Guo913413c2011-06-21 22:41:51 +0800526 dev_err(mmc_dev(host->mmc),
Wolfram Sang7e29c302011-02-26 14:44:41 +0100527 "no card-detect pin available!\n");
528 goto no_card_detect_pin;
529 }
530
Wolfram Sang7e29c302011-02-26 14:44:41 +0100531 err = request_irq(gpio_to_irq(boarddata->cd_gpio), cd_irq,
532 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
533 mmc_hostname(host->mmc), host);
534 if (err) {
Shawn Guo913413c2011-06-21 22:41:51 +0800535 dev_err(mmc_dev(host->mmc), "request irq error\n");
Wolfram Sang7e29c302011-02-26 14:44:41 +0100536 goto no_card_detect_irq;
537 }
Shawn Guo913413c2011-06-21 22:41:51 +0800538 /* fall through */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100539
Shawn Guo913413c2011-06-21 22:41:51 +0800540 case ESDHC_CD_CONTROLLER:
541 /* we have a working card_detect back */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100542 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
Shawn Guo913413c2011-06-21 22:41:51 +0800543 break;
544
545 case ESDHC_CD_PERMANENT:
546 host->mmc->caps = MMC_CAP_NONREMOVABLE;
547 break;
548
549 case ESDHC_CD_NONE:
550 break;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100551 }
Eric Bénard16a790b2010-10-23 01:57:22 +0200552
Shawn Guo85d65092011-05-27 23:48:12 +0800553 err = sdhci_add_host(host);
554 if (err)
555 goto err_add_host;
556
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200557 return 0;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100558
Shawn Guo913413c2011-06-21 22:41:51 +0800559err_add_host:
560 if (gpio_is_valid(boarddata->cd_gpio))
561 free_irq(gpio_to_irq(boarddata->cd_gpio), host);
562no_card_detect_irq:
563 if (gpio_is_valid(boarddata->cd_gpio))
564 gpio_free(boarddata->cd_gpio);
565 if (gpio_is_valid(boarddata->wp_gpio))
566 gpio_free(boarddata->wp_gpio);
567no_card_detect_pin:
568no_board_data:
Dong Aishenge62d8b82012-05-11 14:56:01 +0800569pin_err:
Richard Zhao5ef97fa2011-11-15 14:48:09 +0800570 clk_disable_unprepare(pltfm_host->clk);
Shawn Guo85d65092011-05-27 23:48:12 +0800571 clk_put(pltfm_host->clk);
Shawn Guo913413c2011-06-21 22:41:51 +0800572err_clk_get:
573 kfree(imx_data);
Shawn Guoabfafc22011-06-30 15:44:44 +0800574err_imx_data:
Shawn Guo85d65092011-05-27 23:48:12 +0800575 sdhci_pltfm_free(pdev);
576 return err;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200577}
578
Shawn Guo85d65092011-05-27 23:48:12 +0800579static int __devexit sdhci_esdhc_imx_remove(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200580{
Shawn Guo85d65092011-05-27 23:48:12 +0800581 struct sdhci_host *host = platform_get_drvdata(pdev);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200582 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400583 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Shawn Guo842afc02011-07-06 22:57:48 +0800584 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Shawn Guo85d65092011-05-27 23:48:12 +0800585 int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
586
587 sdhci_remove_host(host, dead);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100588
Shawn Guo913413c2011-06-21 22:41:51 +0800589 if (gpio_is_valid(boarddata->wp_gpio))
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100590 gpio_free(boarddata->wp_gpio);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200591
Shawn Guo913413c2011-06-21 22:41:51 +0800592 if (gpio_is_valid(boarddata->cd_gpio)) {
593 free_irq(gpio_to_irq(boarddata->cd_gpio), host);
Wolfram Sang7e29c302011-02-26 14:44:41 +0100594 gpio_free(boarddata->cd_gpio);
Wolfram Sang7e29c302011-02-26 14:44:41 +0100595 }
596
Richard Zhao5ef97fa2011-11-15 14:48:09 +0800597 clk_disable_unprepare(pltfm_host->clk);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200598 clk_put(pltfm_host->clk);
Richard Zhue1498602011-03-25 09:18:27 -0400599 kfree(imx_data);
Shawn Guo85d65092011-05-27 23:48:12 +0800600
601 sdhci_pltfm_free(pdev);
602
603 return 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200604}
605
Shawn Guo85d65092011-05-27 23:48:12 +0800606static struct platform_driver sdhci_esdhc_imx_driver = {
607 .driver = {
608 .name = "sdhci-esdhc-imx",
609 .owner = THIS_MODULE,
Shawn Guoabfafc22011-06-30 15:44:44 +0800610 .of_match_table = imx_esdhc_dt_ids,
Manuel Lauss29495aa2011-11-03 11:09:45 +0100611 .pm = SDHCI_PLTFM_PMOPS,
Shawn Guo85d65092011-05-27 23:48:12 +0800612 },
Shawn Guo57ed3312011-06-30 09:24:26 +0800613 .id_table = imx_esdhc_devtype,
Shawn Guo85d65092011-05-27 23:48:12 +0800614 .probe = sdhci_esdhc_imx_probe,
615 .remove = __devexit_p(sdhci_esdhc_imx_remove),
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200616};
Shawn Guo85d65092011-05-27 23:48:12 +0800617
Axel Lind1f81a62011-11-26 12:55:43 +0800618module_platform_driver(sdhci_esdhc_imx_driver);
Shawn Guo85d65092011-05-27 23:48:12 +0800619
620MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
621MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
622MODULE_LICENSE("GPL v2");