blob: ce83d6191f4a3cc398e4dd83b9f43456fb4a90c2 [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>
Wolfram Sang0c6d49c2011-02-26 14:44:39 +010027#include <mach/esdhc.h>
Wolfram Sang95f25ef2010-10-15 12:21:04 +020028#include "sdhci-pltfm.h"
29#include "sdhci-esdhc.h"
30
Tony Lin0d588642011-08-11 16:45:59 -040031#define SDHCI_CTRL_D3CD 0x08
Richard Zhu58ac8172011-03-21 13:22:16 +080032/* VENDOR SPEC register */
33#define SDHCI_VENDOR_SPEC 0xC0
34#define SDHCI_VENDOR_SPEC_SDIO_QUIRK 0x00000002
Shawn Guof750ba92011-11-10 16:39:32 +080035#define SDHCI_WTMK_LVL 0x44
Shawn Guo95a24822011-09-19 17:32:21 +080036#define SDHCI_MIX_CTRL 0x48
Richard Zhu58ac8172011-03-21 13:22:16 +080037
Richard Zhu58ac8172011-03-21 13:22:16 +080038/*
Richard Zhu97e4ba62011-08-11 16:51:46 -040039 * There is an INT DMA ERR mis-match between eSDHC and STD SDHC SPEC:
40 * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
41 * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
42 * Define this macro DMA error INT for fsl eSDHC
43 */
44#define SDHCI_INT_VENDOR_SPEC_DMA_ERR 0x10000000
45
46/*
Richard Zhu58ac8172011-03-21 13:22:16 +080047 * The CMDTYPE of the CMD register (offset 0xE) should be set to
48 * "11" when the STOP CMD12 is issued on imx53 to abort one
49 * open ended multi-blk IO. Otherwise the TC INT wouldn't
50 * be generated.
51 * In exact block transfer, the controller doesn't complete the
52 * operations automatically as required at the end of the
53 * transfer and remains on hold if the abort command is not sent.
54 * As a result, the TC flag is not asserted and SW received timeout
55 * exeception. Bit1 of Vendor Spec registor is used to fix it.
56 */
57#define ESDHC_FLAG_MULTIBLK_NO_INT (1 << 1)
Richard Zhue1498602011-03-25 09:18:27 -040058
Shawn Guo57ed3312011-06-30 09:24:26 +080059enum imx_esdhc_type {
60 IMX25_ESDHC,
61 IMX35_ESDHC,
62 IMX51_ESDHC,
63 IMX53_ESDHC,
Shawn Guo95a24822011-09-19 17:32:21 +080064 IMX6Q_USDHC,
Shawn Guo57ed3312011-06-30 09:24:26 +080065};
66
Richard Zhue1498602011-03-25 09:18:27 -040067struct pltfm_imx_data {
68 int flags;
69 u32 scratchpad;
Shawn Guo57ed3312011-06-30 09:24:26 +080070 enum imx_esdhc_type devtype;
Shawn Guo842afc02011-07-06 22:57:48 +080071 struct esdhc_platform_data boarddata;
Sascha Hauer52dac612012-03-07 09:31:34 +010072 struct clk *clk_ipg;
73 struct clk *clk_ahb;
74 struct clk *clk_per;
Richard Zhue1498602011-03-25 09:18:27 -040075};
76
Shawn Guo57ed3312011-06-30 09:24:26 +080077static struct platform_device_id imx_esdhc_devtype[] = {
78 {
79 .name = "sdhci-esdhc-imx25",
80 .driver_data = IMX25_ESDHC,
81 }, {
82 .name = "sdhci-esdhc-imx35",
83 .driver_data = IMX35_ESDHC,
84 }, {
85 .name = "sdhci-esdhc-imx51",
86 .driver_data = IMX51_ESDHC,
87 }, {
88 .name = "sdhci-esdhc-imx53",
89 .driver_data = IMX53_ESDHC,
90 }, {
Shawn Guo95a24822011-09-19 17:32:21 +080091 .name = "sdhci-usdhc-imx6q",
92 .driver_data = IMX6Q_USDHC,
93 }, {
Shawn Guo57ed3312011-06-30 09:24:26 +080094 /* sentinel */
95 }
96};
97MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
98
Shawn Guoabfafc22011-06-30 15:44:44 +080099static const struct of_device_id imx_esdhc_dt_ids[] = {
100 { .compatible = "fsl,imx25-esdhc", .data = &imx_esdhc_devtype[IMX25_ESDHC], },
101 { .compatible = "fsl,imx35-esdhc", .data = &imx_esdhc_devtype[IMX35_ESDHC], },
102 { .compatible = "fsl,imx51-esdhc", .data = &imx_esdhc_devtype[IMX51_ESDHC], },
103 { .compatible = "fsl,imx53-esdhc", .data = &imx_esdhc_devtype[IMX53_ESDHC], },
Shawn Guo95a24822011-09-19 17:32:21 +0800104 { .compatible = "fsl,imx6q-usdhc", .data = &imx_esdhc_devtype[IMX6Q_USDHC], },
Shawn Guoabfafc22011-06-30 15:44:44 +0800105 { /* sentinel */ }
106};
107MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
108
Shawn Guo57ed3312011-06-30 09:24:26 +0800109static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
110{
111 return data->devtype == IMX25_ESDHC;
112}
113
114static inline int is_imx35_esdhc(struct pltfm_imx_data *data)
115{
116 return data->devtype == IMX35_ESDHC;
117}
118
119static inline int is_imx51_esdhc(struct pltfm_imx_data *data)
120{
121 return data->devtype == IMX51_ESDHC;
122}
123
124static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
125{
126 return data->devtype == IMX53_ESDHC;
127}
128
Shawn Guo95a24822011-09-19 17:32:21 +0800129static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
130{
131 return data->devtype == IMX6Q_USDHC;
132}
133
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200134static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
135{
136 void __iomem *base = host->ioaddr + (reg & ~0x3);
137 u32 shift = (reg & 0x3) * 8;
138
139 writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
140}
141
Wolfram Sang7e29c302011-02-26 14:44:41 +0100142static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
143{
Shawn Guo842afc02011-07-06 22:57:48 +0800144 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
145 struct pltfm_imx_data *imx_data = pltfm_host->priv;
146 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Richard Zhue1498602011-03-25 09:18:27 -0400147
Shawn Guo913413c2011-06-21 22:41:51 +0800148 /* fake CARD_PRESENT flag */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100149 u32 val = readl(host->ioaddr + reg);
150
Richard Zhue1498602011-03-25 09:18:27 -0400151 if (unlikely((reg == SDHCI_PRESENT_STATE)
Shawn Guo913413c2011-06-21 22:41:51 +0800152 && gpio_is_valid(boarddata->cd_gpio))) {
153 if (gpio_get_value(boarddata->cd_gpio))
Wolfram Sang7e29c302011-02-26 14:44:41 +0100154 /* no card, if a valid gpio says so... */
Shawn Guo803862a2011-06-21 22:41:49 +0800155 val &= ~SDHCI_CARD_PRESENT;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100156 else
157 /* ... in all other cases assume card is present */
158 val |= SDHCI_CARD_PRESENT;
159 }
160
Richard Zhu97e4ba62011-08-11 16:51:46 -0400161 if (unlikely(reg == SDHCI_CAPABILITIES)) {
162 /* In FSL esdhc IC module, only bit20 is used to indicate the
163 * ADMA2 capability of esdhc, but this bit is messed up on
164 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
165 * don't actually support ADMA2). So set the BROKEN_ADMA
166 * uirk on MX25/35 platforms.
167 */
168
169 if (val & SDHCI_CAN_DO_ADMA1) {
170 val &= ~SDHCI_CAN_DO_ADMA1;
171 val |= SDHCI_CAN_DO_ADMA2;
172 }
173 }
174
175 if (unlikely(reg == SDHCI_INT_STATUS)) {
176 if (val & SDHCI_INT_VENDOR_SPEC_DMA_ERR) {
177 val &= ~SDHCI_INT_VENDOR_SPEC_DMA_ERR;
178 val |= SDHCI_INT_ADMA_ERROR;
179 }
180 }
181
Wolfram Sang7e29c302011-02-26 14:44:41 +0100182 return val;
183}
184
185static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
186{
Richard Zhue1498602011-03-25 09:18:27 -0400187 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
188 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Shawn Guo842afc02011-07-06 22:57:48 +0800189 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Tony Lin0d588642011-08-11 16:45:59 -0400190 u32 data;
Richard Zhue1498602011-03-25 09:18:27 -0400191
Tony Lin0d588642011-08-11 16:45:59 -0400192 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
193 if (boarddata->cd_type == ESDHC_CD_GPIO)
194 /*
195 * These interrupts won't work with a custom
196 * card_detect gpio (only applied to mx25/35)
197 */
198 val &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
199
200 if (val & SDHCI_INT_CARD_INT) {
201 /*
202 * Clear and then set D3CD bit to avoid missing the
203 * card interrupt. This is a eSDHC controller problem
204 * so we need to apply the following workaround: clear
205 * and set D3CD bit will make eSDHC re-sample the card
206 * interrupt. In case a card interrupt was lost,
207 * re-sample it by the following steps.
208 */
209 data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
210 data &= ~SDHCI_CTRL_D3CD;
211 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
212 data |= SDHCI_CTRL_D3CD;
213 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
214 }
215 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100216
Richard Zhu58ac8172011-03-21 13:22:16 +0800217 if (unlikely((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
218 && (reg == SDHCI_INT_STATUS)
219 && (val & SDHCI_INT_DATA_END))) {
220 u32 v;
221 v = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
222 v &= ~SDHCI_VENDOR_SPEC_SDIO_QUIRK;
223 writel(v, host->ioaddr + SDHCI_VENDOR_SPEC);
224 }
225
Richard Zhu97e4ba62011-08-11 16:51:46 -0400226 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
227 if (val & SDHCI_INT_ADMA_ERROR) {
228 val &= ~SDHCI_INT_ADMA_ERROR;
229 val |= SDHCI_INT_VENDOR_SPEC_DMA_ERR;
230 }
231 }
232
Wolfram Sang7e29c302011-02-26 14:44:41 +0100233 writel(val, host->ioaddr + reg);
234}
235
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200236static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
237{
Shawn Guo95a24822011-09-19 17:32:21 +0800238 if (unlikely(reg == SDHCI_HOST_VERSION)) {
239 u16 val = readw(host->ioaddr + (reg ^ 2));
240 /*
241 * uSDHC supports SDHCI v3.0, but it's encoded as value
242 * 0x3 in host controller version register, which violates
243 * SDHCI_SPEC_300 definition. Work it around here.
244 */
245 if ((val & SDHCI_SPEC_VER_MASK) == 3)
246 return --val;
247 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200248
249 return readw(host->ioaddr + reg);
250}
251
252static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
253{
254 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400255 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200256
257 switch (reg) {
258 case SDHCI_TRANSFER_MODE:
259 /*
260 * Postpone this write, we must do it together with a
261 * command write that is down below.
262 */
Richard Zhu58ac8172011-03-21 13:22:16 +0800263 if ((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
264 && (host->cmd->opcode == SD_IO_RW_EXTENDED)
265 && (host->cmd->data->blocks > 1)
266 && (host->cmd->data->flags & MMC_DATA_READ)) {
267 u32 v;
268 v = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
269 v |= SDHCI_VENDOR_SPEC_SDIO_QUIRK;
270 writel(v, host->ioaddr + SDHCI_VENDOR_SPEC);
271 }
Richard Zhue1498602011-03-25 09:18:27 -0400272 imx_data->scratchpad = val;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200273 return;
274 case SDHCI_COMMAND:
Sascha Hauer5b6b0ad2012-02-17 11:51:49 +0100275 if ((host->cmd->opcode == MMC_STOP_TRANSMISSION ||
276 host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
277 (imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
Richard Zhu58ac8172011-03-21 13:22:16 +0800278 val |= SDHCI_CMD_ABORTCMD;
Shawn Guo95a24822011-09-19 17:32:21 +0800279
280 if (is_imx6q_usdhc(imx_data)) {
281 u32 m = readl(host->ioaddr + SDHCI_MIX_CTRL);
282 m = imx_data->scratchpad | (m & 0xffff0000);
283 writel(m, host->ioaddr + SDHCI_MIX_CTRL);
284 writel(val << 16,
285 host->ioaddr + SDHCI_TRANSFER_MODE);
286 } else {
287 writel(val << 16 | imx_data->scratchpad,
288 host->ioaddr + SDHCI_TRANSFER_MODE);
289 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200290 return;
291 case SDHCI_BLOCK_SIZE:
292 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
293 break;
294 }
295 esdhc_clrset_le(host, 0xffff, val, reg);
296}
297
298static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
299{
300 u32 new_val;
301
302 switch (reg) {
303 case SDHCI_POWER_CONTROL:
304 /*
305 * FSL put some DMA bits here
306 * If your board has a regulator, code should be here
307 */
308 return;
309 case SDHCI_HOST_CONTROL:
Tony Lin0d588642011-08-11 16:45:59 -0400310 /* FSL messed up here, so we can just keep those three */
311 new_val = val & (SDHCI_CTRL_LED | \
312 SDHCI_CTRL_4BITBUS | \
313 SDHCI_CTRL_D3CD);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200314 /* ensure the endianess */
315 new_val |= ESDHC_HOST_CONTROL_LE;
316 /* DMA mode bits are shifted */
317 new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
318
319 esdhc_clrset_le(host, 0xffff, new_val, reg);
320 return;
321 }
322 esdhc_clrset_le(host, 0xff, val, reg);
Shawn Guo913413c2011-06-21 22:41:51 +0800323
324 /*
325 * The esdhc has a design violation to SDHC spec which tells
326 * that software reset should not affect card detection circuit.
327 * But esdhc clears its SYSCTL register bits [0..2] during the
328 * software reset. This will stop those clocks that card detection
329 * circuit relies on. To work around it, we turn the clocks on back
330 * to keep card detection circuit functional.
331 */
332 if ((reg == SDHCI_SOFTWARE_RESET) && (val & 1))
333 esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200334}
335
336static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
337{
338 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
339
340 return clk_get_rate(pltfm_host->clk);
341}
342
343static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
344{
345 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
346
347 return clk_get_rate(pltfm_host->clk) / 256 / 16;
348}
349
Shawn Guo913413c2011-06-21 22:41:51 +0800350static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
351{
Shawn Guo842afc02011-07-06 22:57:48 +0800352 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
353 struct pltfm_imx_data *imx_data = pltfm_host->priv;
354 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Shawn Guo913413c2011-06-21 22:41:51 +0800355
356 switch (boarddata->wp_type) {
357 case ESDHC_WP_GPIO:
358 if (gpio_is_valid(boarddata->wp_gpio))
359 return gpio_get_value(boarddata->wp_gpio);
360 case ESDHC_WP_CONTROLLER:
361 return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
362 SDHCI_WRITE_PROTECT);
363 case ESDHC_WP_NONE:
364 break;
365 }
366
367 return -ENOSYS;
368}
369
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100370static struct sdhci_ops sdhci_esdhc_ops = {
Richard Zhue1498602011-03-25 09:18:27 -0400371 .read_l = esdhc_readl_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100372 .read_w = esdhc_readw_le,
Richard Zhue1498602011-03-25 09:18:27 -0400373 .write_l = esdhc_writel_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100374 .write_w = esdhc_writew_le,
375 .write_b = esdhc_writeb_le,
376 .set_clock = esdhc_set_clock,
377 .get_max_clock = esdhc_pltfm_get_max_clock,
378 .get_min_clock = esdhc_pltfm_get_min_clock,
Shawn Guo913413c2011-06-21 22:41:51 +0800379 .get_ro = esdhc_pltfm_get_ro,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100380};
381
Shawn Guo85d65092011-05-27 23:48:12 +0800382static struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
Richard Zhu97e4ba62011-08-11 16:51:46 -0400383 .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
384 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
385 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
Shawn Guo85d65092011-05-27 23:48:12 +0800386 | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
Shawn Guo85d65092011-05-27 23:48:12 +0800387 .ops = &sdhci_esdhc_ops,
388};
389
Wolfram Sang7e29c302011-02-26 14:44:41 +0100390static irqreturn_t cd_irq(int irq, void *data)
391{
392 struct sdhci_host *sdhost = (struct sdhci_host *)data;
393
394 tasklet_schedule(&sdhost->card_tasklet);
395 return IRQ_HANDLED;
396};
397
Shawn Guoabfafc22011-06-30 15:44:44 +0800398#ifdef CONFIG_OF
399static int __devinit
400sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
401 struct esdhc_platform_data *boarddata)
402{
403 struct device_node *np = pdev->dev.of_node;
404
405 if (!np)
406 return -ENODEV;
407
408 if (of_get_property(np, "fsl,card-wired", NULL))
409 boarddata->cd_type = ESDHC_CD_PERMANENT;
410
411 if (of_get_property(np, "fsl,cd-controller", NULL))
412 boarddata->cd_type = ESDHC_CD_CONTROLLER;
413
414 if (of_get_property(np, "fsl,wp-controller", NULL))
415 boarddata->wp_type = ESDHC_WP_CONTROLLER;
416
417 boarddata->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
418 if (gpio_is_valid(boarddata->cd_gpio))
419 boarddata->cd_type = ESDHC_CD_GPIO;
420
421 boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
422 if (gpio_is_valid(boarddata->wp_gpio))
423 boarddata->wp_type = ESDHC_WP_GPIO;
424
425 return 0;
426}
427#else
428static inline int
429sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
430 struct esdhc_platform_data *boarddata)
431{
432 return -ENODEV;
433}
434#endif
435
Shawn Guo85d65092011-05-27 23:48:12 +0800436static int __devinit sdhci_esdhc_imx_probe(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200437{
Shawn Guoabfafc22011-06-30 15:44:44 +0800438 const struct of_device_id *of_id =
439 of_match_device(imx_esdhc_dt_ids, &pdev->dev);
Shawn Guo85d65092011-05-27 23:48:12 +0800440 struct sdhci_pltfm_host *pltfm_host;
441 struct sdhci_host *host;
442 struct esdhc_platform_data *boarddata;
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
Sascha Hauer52dac612012-03-07 09:31:34 +0100463 imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
464 if (IS_ERR(imx_data->clk_ipg)) {
465 err = PTR_ERR(imx_data->clk_ipg);
Shawn Guo85d65092011-05-27 23:48:12 +0800466 goto err_clk_get;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200467 }
Sascha Hauer52dac612012-03-07 09:31:34 +0100468
469 imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
470 if (IS_ERR(imx_data->clk_ahb)) {
471 err = PTR_ERR(imx_data->clk_ahb);
472 goto err_clk_get;
473 }
474
475 imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
476 if (IS_ERR(imx_data->clk_per)) {
477 err = PTR_ERR(imx_data->clk_per);
478 goto err_clk_get;
479 }
480
481 pltfm_host->clk = imx_data->clk_per;
482
483 clk_prepare_enable(imx_data->clk_per);
484 clk_prepare_enable(imx_data->clk_ipg);
485 clk_prepare_enable(imx_data->clk_ahb);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200486
Eric Bénardb89152822012-04-18 02:30:20 +0200487 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
Eric Bénard37865fe2010-10-23 01:57:21 +0200488
Shawn Guo57ed3312011-06-30 09:24:26 +0800489 if (is_imx25_esdhc(imx_data) || is_imx35_esdhc(imx_data))
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100490 /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
Richard Zhu97e4ba62011-08-11 16:51:46 -0400491 host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK
492 | SDHCI_QUIRK_BROKEN_ADMA;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100493
Shawn Guo57ed3312011-06-30 09:24:26 +0800494 if (is_imx53_esdhc(imx_data))
Richard Zhu58ac8172011-03-21 13:22:16 +0800495 imx_data->flags |= ESDHC_FLAG_MULTIBLK_NO_INT;
496
Shawn Guof750ba92011-11-10 16:39:32 +0800497 /*
498 * The imx6q ROM code will change the default watermark level setting
499 * to something insane. Change it back here.
500 */
501 if (is_imx6q_usdhc(imx_data))
502 writel(0x08100810, host->ioaddr + SDHCI_WTMK_LVL);
503
Shawn Guo842afc02011-07-06 22:57:48 +0800504 boarddata = &imx_data->boarddata;
Shawn Guoabfafc22011-06-30 15:44:44 +0800505 if (sdhci_esdhc_imx_probe_dt(pdev, boarddata) < 0) {
506 if (!host->mmc->parent->platform_data) {
507 dev_err(mmc_dev(host->mmc), "no board data!\n");
508 err = -EINVAL;
509 goto no_board_data;
510 }
511 imx_data->boarddata = *((struct esdhc_platform_data *)
512 host->mmc->parent->platform_data);
513 }
Shawn Guo913413c2011-06-21 22:41:51 +0800514
515 /* write_protect */
516 if (boarddata->wp_type == ESDHC_WP_GPIO) {
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100517 err = gpio_request_one(boarddata->wp_gpio, GPIOF_IN, "ESDHC_WP");
518 if (err) {
519 dev_warn(mmc_dev(host->mmc),
Shawn Guo913413c2011-06-21 22:41:51 +0800520 "no write-protect pin available!\n");
521 boarddata->wp_gpio = -EINVAL;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100522 }
Shawn Guo913413c2011-06-21 22:41:51 +0800523 } else {
524 boarddata->wp_gpio = -EINVAL;
525 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100526
Shawn Guo913413c2011-06-21 22:41:51 +0800527 /* card_detect */
528 if (boarddata->cd_type != ESDHC_CD_GPIO)
529 boarddata->cd_gpio = -EINVAL;
530
531 switch (boarddata->cd_type) {
532 case ESDHC_CD_GPIO:
Wolfram Sang7e29c302011-02-26 14:44:41 +0100533 err = gpio_request_one(boarddata->cd_gpio, GPIOF_IN, "ESDHC_CD");
534 if (err) {
Shawn Guo913413c2011-06-21 22:41:51 +0800535 dev_err(mmc_dev(host->mmc),
Wolfram Sang7e29c302011-02-26 14:44:41 +0100536 "no card-detect pin available!\n");
537 goto no_card_detect_pin;
538 }
539
Wolfram Sang7e29c302011-02-26 14:44:41 +0100540 err = request_irq(gpio_to_irq(boarddata->cd_gpio), cd_irq,
541 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
542 mmc_hostname(host->mmc), host);
543 if (err) {
Shawn Guo913413c2011-06-21 22:41:51 +0800544 dev_err(mmc_dev(host->mmc), "request irq error\n");
Wolfram Sang7e29c302011-02-26 14:44:41 +0100545 goto no_card_detect_irq;
546 }
Shawn Guo913413c2011-06-21 22:41:51 +0800547 /* fall through */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100548
Shawn Guo913413c2011-06-21 22:41:51 +0800549 case ESDHC_CD_CONTROLLER:
550 /* we have a working card_detect back */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100551 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
Shawn Guo913413c2011-06-21 22:41:51 +0800552 break;
553
554 case ESDHC_CD_PERMANENT:
555 host->mmc->caps = MMC_CAP_NONREMOVABLE;
556 break;
557
558 case ESDHC_CD_NONE:
559 break;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100560 }
Eric Bénard16a790b2010-10-23 01:57:22 +0200561
Shawn Guo85d65092011-05-27 23:48:12 +0800562 err = sdhci_add_host(host);
563 if (err)
564 goto err_add_host;
565
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200566 return 0;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100567
Shawn Guo913413c2011-06-21 22:41:51 +0800568err_add_host:
569 if (gpio_is_valid(boarddata->cd_gpio))
570 free_irq(gpio_to_irq(boarddata->cd_gpio), host);
571no_card_detect_irq:
572 if (gpio_is_valid(boarddata->cd_gpio))
573 gpio_free(boarddata->cd_gpio);
574 if (gpio_is_valid(boarddata->wp_gpio))
575 gpio_free(boarddata->wp_gpio);
576no_card_detect_pin:
577no_board_data:
Sascha Hauer52dac612012-03-07 09:31:34 +0100578 clk_disable_unprepare(imx_data->clk_per);
579 clk_disable_unprepare(imx_data->clk_ipg);
580 clk_disable_unprepare(imx_data->clk_ahb);
Shawn Guo913413c2011-06-21 22:41:51 +0800581err_clk_get:
582 kfree(imx_data);
Shawn Guoabfafc22011-06-30 15:44:44 +0800583err_imx_data:
Shawn Guo85d65092011-05-27 23:48:12 +0800584 sdhci_pltfm_free(pdev);
585 return err;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200586}
587
Shawn Guo85d65092011-05-27 23:48:12 +0800588static int __devexit sdhci_esdhc_imx_remove(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200589{
Shawn Guo85d65092011-05-27 23:48:12 +0800590 struct sdhci_host *host = platform_get_drvdata(pdev);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200591 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400592 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Shawn Guo842afc02011-07-06 22:57:48 +0800593 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Shawn Guo85d65092011-05-27 23:48:12 +0800594 int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
595
596 sdhci_remove_host(host, dead);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100597
Shawn Guo913413c2011-06-21 22:41:51 +0800598 if (gpio_is_valid(boarddata->wp_gpio))
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100599 gpio_free(boarddata->wp_gpio);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200600
Shawn Guo913413c2011-06-21 22:41:51 +0800601 if (gpio_is_valid(boarddata->cd_gpio)) {
602 free_irq(gpio_to_irq(boarddata->cd_gpio), host);
Wolfram Sang7e29c302011-02-26 14:44:41 +0100603 gpio_free(boarddata->cd_gpio);
Wolfram Sang7e29c302011-02-26 14:44:41 +0100604 }
605
Sascha Hauer52dac612012-03-07 09:31:34 +0100606 clk_disable_unprepare(imx_data->clk_per);
607 clk_disable_unprepare(imx_data->clk_ipg);
608 clk_disable_unprepare(imx_data->clk_ahb);
609
Richard Zhue1498602011-03-25 09:18:27 -0400610 kfree(imx_data);
Shawn Guo85d65092011-05-27 23:48:12 +0800611
612 sdhci_pltfm_free(pdev);
613
614 return 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200615}
616
Shawn Guo85d65092011-05-27 23:48:12 +0800617static struct platform_driver sdhci_esdhc_imx_driver = {
618 .driver = {
619 .name = "sdhci-esdhc-imx",
620 .owner = THIS_MODULE,
Shawn Guoabfafc22011-06-30 15:44:44 +0800621 .of_match_table = imx_esdhc_dt_ids,
Manuel Lauss29495aa2011-11-03 11:09:45 +0100622 .pm = SDHCI_PLTFM_PMOPS,
Shawn Guo85d65092011-05-27 23:48:12 +0800623 },
Shawn Guo57ed3312011-06-30 09:24:26 +0800624 .id_table = imx_esdhc_devtype,
Shawn Guo85d65092011-05-27 23:48:12 +0800625 .probe = sdhci_esdhc_imx_probe,
626 .remove = __devexit_p(sdhci_esdhc_imx_remove),
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200627};
Shawn Guo85d65092011-05-27 23:48:12 +0800628
Axel Lind1f81a62011-11-26 12:55:43 +0800629module_platform_driver(sdhci_esdhc_imx_driver);
Shawn Guo85d65092011-05-27 23:48:12 +0800630
631MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
632MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
633MODULE_LICENSE("GPL v2");