blob: 4269bb498ff0305b20c5e8c922cb400addacf56d [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>
Richard Zhue1498602011-03-25 09:18:27 -040019#include <linux/slab.h>
Wolfram Sang95f25ef2010-10-15 12:21:04 +020020#include <linux/mmc/host.h>
Richard Zhu58ac8172011-03-21 13:22:16 +080021#include <linux/mmc/mmc.h>
22#include <linux/mmc/sdio.h>
Eric Bénard37865fe2010-10-23 01:57:21 +020023#include <mach/hardware.h>
Wolfram Sang0c6d49c2011-02-26 14:44:39 +010024#include <mach/esdhc.h>
Wolfram Sang95f25ef2010-10-15 12:21:04 +020025#include "sdhci-pltfm.h"
26#include "sdhci-esdhc.h"
27
Richard Zhu58ac8172011-03-21 13:22:16 +080028/* VENDOR SPEC register */
29#define SDHCI_VENDOR_SPEC 0xC0
30#define SDHCI_VENDOR_SPEC_SDIO_QUIRK 0x00000002
31
Richard Zhu58ac8172011-03-21 13:22:16 +080032/*
33 * The CMDTYPE of the CMD register (offset 0xE) should be set to
34 * "11" when the STOP CMD12 is issued on imx53 to abort one
35 * open ended multi-blk IO. Otherwise the TC INT wouldn't
36 * be generated.
37 * In exact block transfer, the controller doesn't complete the
38 * operations automatically as required at the end of the
39 * transfer and remains on hold if the abort command is not sent.
40 * As a result, the TC flag is not asserted and SW received timeout
41 * exeception. Bit1 of Vendor Spec registor is used to fix it.
42 */
43#define ESDHC_FLAG_MULTIBLK_NO_INT (1 << 1)
Richard Zhue1498602011-03-25 09:18:27 -040044
45struct pltfm_imx_data {
46 int flags;
47 u32 scratchpad;
48};
49
Wolfram Sang95f25ef2010-10-15 12:21:04 +020050static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
51{
52 void __iomem *base = host->ioaddr + (reg & ~0x3);
53 u32 shift = (reg & 0x3) * 8;
54
55 writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
56}
57
Wolfram Sang7e29c302011-02-26 14:44:41 +010058static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
59{
Shawn Guo913413c2011-06-21 22:41:51 +080060 struct esdhc_platform_data *boarddata =
61 host->mmc->parent->platform_data;
Richard Zhue1498602011-03-25 09:18:27 -040062
Shawn Guo913413c2011-06-21 22:41:51 +080063 /* fake CARD_PRESENT flag */
Wolfram Sang7e29c302011-02-26 14:44:41 +010064 u32 val = readl(host->ioaddr + reg);
65
Richard Zhue1498602011-03-25 09:18:27 -040066 if (unlikely((reg == SDHCI_PRESENT_STATE)
Shawn Guo913413c2011-06-21 22:41:51 +080067 && gpio_is_valid(boarddata->cd_gpio))) {
68 if (gpio_get_value(boarddata->cd_gpio))
Wolfram Sang7e29c302011-02-26 14:44:41 +010069 /* no card, if a valid gpio says so... */
Shawn Guo803862a2011-06-21 22:41:49 +080070 val &= ~SDHCI_CARD_PRESENT;
Wolfram Sang7e29c302011-02-26 14:44:41 +010071 else
72 /* ... in all other cases assume card is present */
73 val |= SDHCI_CARD_PRESENT;
74 }
75
76 return val;
77}
78
79static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
80{
Richard Zhue1498602011-03-25 09:18:27 -040081 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
82 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Shawn Guo913413c2011-06-21 22:41:51 +080083 struct esdhc_platform_data *boarddata =
84 host->mmc->parent->platform_data;
Richard Zhue1498602011-03-25 09:18:27 -040085
86 if (unlikely((reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)
Shawn Guo913413c2011-06-21 22:41:51 +080087 && (boarddata->cd_type == ESDHC_CD_GPIO)))
Wolfram Sang7e29c302011-02-26 14:44:41 +010088 /*
89 * these interrupts won't work with a custom card_detect gpio
Wolfram Sang7e29c302011-02-26 14:44:41 +010090 */
91 val &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
92
Richard Zhu58ac8172011-03-21 13:22:16 +080093 if (unlikely((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
94 && (reg == SDHCI_INT_STATUS)
95 && (val & SDHCI_INT_DATA_END))) {
96 u32 v;
97 v = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
98 v &= ~SDHCI_VENDOR_SPEC_SDIO_QUIRK;
99 writel(v, host->ioaddr + SDHCI_VENDOR_SPEC);
100 }
101
Wolfram Sang7e29c302011-02-26 14:44:41 +0100102 writel(val, host->ioaddr + reg);
103}
104
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200105static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
106{
107 if (unlikely(reg == SDHCI_HOST_VERSION))
108 reg ^= 2;
109
110 return readw(host->ioaddr + reg);
111}
112
113static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
114{
115 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400116 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200117
118 switch (reg) {
119 case SDHCI_TRANSFER_MODE:
120 /*
121 * Postpone this write, we must do it together with a
122 * command write that is down below.
123 */
Richard Zhu58ac8172011-03-21 13:22:16 +0800124 if ((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
125 && (host->cmd->opcode == SD_IO_RW_EXTENDED)
126 && (host->cmd->data->blocks > 1)
127 && (host->cmd->data->flags & MMC_DATA_READ)) {
128 u32 v;
129 v = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
130 v |= SDHCI_VENDOR_SPEC_SDIO_QUIRK;
131 writel(v, host->ioaddr + SDHCI_VENDOR_SPEC);
132 }
Richard Zhue1498602011-03-25 09:18:27 -0400133 imx_data->scratchpad = val;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200134 return;
135 case SDHCI_COMMAND:
Richard Zhu58ac8172011-03-21 13:22:16 +0800136 if ((host->cmd->opcode == MMC_STOP_TRANSMISSION)
137 && (imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
138 val |= SDHCI_CMD_ABORTCMD;
Richard Zhue1498602011-03-25 09:18:27 -0400139 writel(val << 16 | imx_data->scratchpad,
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200140 host->ioaddr + SDHCI_TRANSFER_MODE);
141 return;
142 case SDHCI_BLOCK_SIZE:
143 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
144 break;
145 }
146 esdhc_clrset_le(host, 0xffff, val, reg);
147}
148
149static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
150{
151 u32 new_val;
152
153 switch (reg) {
154 case SDHCI_POWER_CONTROL:
155 /*
156 * FSL put some DMA bits here
157 * If your board has a regulator, code should be here
158 */
159 return;
160 case SDHCI_HOST_CONTROL:
161 /* FSL messed up here, so we can just keep those two */
162 new_val = val & (SDHCI_CTRL_LED | SDHCI_CTRL_4BITBUS);
163 /* ensure the endianess */
164 new_val |= ESDHC_HOST_CONTROL_LE;
165 /* DMA mode bits are shifted */
166 new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
167
168 esdhc_clrset_le(host, 0xffff, new_val, reg);
169 return;
170 }
171 esdhc_clrset_le(host, 0xff, val, reg);
Shawn Guo913413c2011-06-21 22:41:51 +0800172
173 /*
174 * The esdhc has a design violation to SDHC spec which tells
175 * that software reset should not affect card detection circuit.
176 * But esdhc clears its SYSCTL register bits [0..2] during the
177 * software reset. This will stop those clocks that card detection
178 * circuit relies on. To work around it, we turn the clocks on back
179 * to keep card detection circuit functional.
180 */
181 if ((reg == SDHCI_SOFTWARE_RESET) && (val & 1))
182 esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200183}
184
185static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
186{
187 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
188
189 return clk_get_rate(pltfm_host->clk);
190}
191
192static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
193{
194 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
195
196 return clk_get_rate(pltfm_host->clk) / 256 / 16;
197}
198
Shawn Guo913413c2011-06-21 22:41:51 +0800199static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
200{
201 struct esdhc_platform_data *boarddata =
202 host->mmc->parent->platform_data;
203
204 switch (boarddata->wp_type) {
205 case ESDHC_WP_GPIO:
206 if (gpio_is_valid(boarddata->wp_gpio))
207 return gpio_get_value(boarddata->wp_gpio);
208 case ESDHC_WP_CONTROLLER:
209 return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
210 SDHCI_WRITE_PROTECT);
211 case ESDHC_WP_NONE:
212 break;
213 }
214
215 return -ENOSYS;
216}
217
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100218static struct sdhci_ops sdhci_esdhc_ops = {
Richard Zhue1498602011-03-25 09:18:27 -0400219 .read_l = esdhc_readl_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100220 .read_w = esdhc_readw_le,
Richard Zhue1498602011-03-25 09:18:27 -0400221 .write_l = esdhc_writel_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100222 .write_w = esdhc_writew_le,
223 .write_b = esdhc_writeb_le,
224 .set_clock = esdhc_set_clock,
225 .get_max_clock = esdhc_pltfm_get_max_clock,
226 .get_min_clock = esdhc_pltfm_get_min_clock,
Shawn Guo913413c2011-06-21 22:41:51 +0800227 .get_ro = esdhc_pltfm_get_ro,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100228};
229
Shawn Guo85d65092011-05-27 23:48:12 +0800230static struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
231 .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_BROKEN_ADMA
232 | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
233 /* ADMA has issues. Might be fixable */
234 .ops = &sdhci_esdhc_ops,
235};
236
Wolfram Sang7e29c302011-02-26 14:44:41 +0100237static irqreturn_t cd_irq(int irq, void *data)
238{
239 struct sdhci_host *sdhost = (struct sdhci_host *)data;
240
241 tasklet_schedule(&sdhost->card_tasklet);
242 return IRQ_HANDLED;
243};
244
Shawn Guo85d65092011-05-27 23:48:12 +0800245static int __devinit sdhci_esdhc_imx_probe(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200246{
Shawn Guo85d65092011-05-27 23:48:12 +0800247 struct sdhci_pltfm_host *pltfm_host;
248 struct sdhci_host *host;
249 struct esdhc_platform_data *boarddata;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200250 struct clk *clk;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100251 int err;
Richard Zhue1498602011-03-25 09:18:27 -0400252 struct pltfm_imx_data *imx_data;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200253
Shawn Guo85d65092011-05-27 23:48:12 +0800254 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata);
255 if (IS_ERR(host))
256 return PTR_ERR(host);
257
258 pltfm_host = sdhci_priv(host);
259
260 imx_data = kzalloc(sizeof(struct pltfm_imx_data), GFP_KERNEL);
261 if (!imx_data)
262 return -ENOMEM;
263 pltfm_host->priv = imx_data;
264
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200265 clk = clk_get(mmc_dev(host->mmc), NULL);
266 if (IS_ERR(clk)) {
267 dev_err(mmc_dev(host->mmc), "clk err\n");
Shawn Guo85d65092011-05-27 23:48:12 +0800268 err = PTR_ERR(clk);
269 goto err_clk_get;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200270 }
271 clk_enable(clk);
272 pltfm_host->clk = clk;
273
Richard Zhue1498602011-03-25 09:18:27 -0400274 if (!cpu_is_mx25())
Eric Bénard37865fe2010-10-23 01:57:21 +0200275 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
276
Shawn Guo913413c2011-06-21 22:41:51 +0800277 if (cpu_is_mx25() || cpu_is_mx35())
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100278 /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
Eric Bénard16a790b2010-10-23 01:57:22 +0200279 host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100280
Richard Zhu58ac8172011-03-21 13:22:16 +0800281 if (!(cpu_is_mx25() || cpu_is_mx35() || cpu_is_mx51()))
282 imx_data->flags |= ESDHC_FLAG_MULTIBLK_NO_INT;
283
Shawn Guo85d65092011-05-27 23:48:12 +0800284 boarddata = host->mmc->parent->platform_data;
Shawn Guo913413c2011-06-21 22:41:51 +0800285 if (!boarddata) {
286 dev_err(mmc_dev(host->mmc), "no board data!\n");
287 err = -EINVAL;
288 goto no_board_data;
289 }
290
291 /* write_protect */
292 if (boarddata->wp_type == ESDHC_WP_GPIO) {
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100293 err = gpio_request_one(boarddata->wp_gpio, GPIOF_IN, "ESDHC_WP");
294 if (err) {
295 dev_warn(mmc_dev(host->mmc),
Shawn Guo913413c2011-06-21 22:41:51 +0800296 "no write-protect pin available!\n");
297 boarddata->wp_gpio = -EINVAL;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100298 }
Shawn Guo913413c2011-06-21 22:41:51 +0800299 } else {
300 boarddata->wp_gpio = -EINVAL;
301 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100302
Shawn Guo913413c2011-06-21 22:41:51 +0800303 /* card_detect */
304 if (boarddata->cd_type != ESDHC_CD_GPIO)
305 boarddata->cd_gpio = -EINVAL;
306
307 switch (boarddata->cd_type) {
308 case ESDHC_CD_GPIO:
Wolfram Sang7e29c302011-02-26 14:44:41 +0100309 err = gpio_request_one(boarddata->cd_gpio, GPIOF_IN, "ESDHC_CD");
310 if (err) {
Shawn Guo913413c2011-06-21 22:41:51 +0800311 dev_err(mmc_dev(host->mmc),
Wolfram Sang7e29c302011-02-26 14:44:41 +0100312 "no card-detect pin available!\n");
313 goto no_card_detect_pin;
314 }
315
Wolfram Sang7e29c302011-02-26 14:44:41 +0100316 err = request_irq(gpio_to_irq(boarddata->cd_gpio), cd_irq,
317 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
318 mmc_hostname(host->mmc), host);
319 if (err) {
Shawn Guo913413c2011-06-21 22:41:51 +0800320 dev_err(mmc_dev(host->mmc), "request irq error\n");
Wolfram Sang7e29c302011-02-26 14:44:41 +0100321 goto no_card_detect_irq;
322 }
Shawn Guo913413c2011-06-21 22:41:51 +0800323 /* fall through */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100324
Shawn Guo913413c2011-06-21 22:41:51 +0800325 case ESDHC_CD_CONTROLLER:
326 /* we have a working card_detect back */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100327 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
Shawn Guo913413c2011-06-21 22:41:51 +0800328 break;
329
330 case ESDHC_CD_PERMANENT:
331 host->mmc->caps = MMC_CAP_NONREMOVABLE;
332 break;
333
334 case ESDHC_CD_NONE:
335 break;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100336 }
Eric Bénard16a790b2010-10-23 01:57:22 +0200337
Shawn Guo85d65092011-05-27 23:48:12 +0800338 err = sdhci_add_host(host);
339 if (err)
340 goto err_add_host;
341
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200342 return 0;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100343
Shawn Guo913413c2011-06-21 22:41:51 +0800344err_add_host:
345 if (gpio_is_valid(boarddata->cd_gpio))
346 free_irq(gpio_to_irq(boarddata->cd_gpio), host);
347no_card_detect_irq:
348 if (gpio_is_valid(boarddata->cd_gpio))
349 gpio_free(boarddata->cd_gpio);
350 if (gpio_is_valid(boarddata->wp_gpio))
351 gpio_free(boarddata->wp_gpio);
352no_card_detect_pin:
353no_board_data:
Shawn Guo85d65092011-05-27 23:48:12 +0800354 clk_disable(pltfm_host->clk);
355 clk_put(pltfm_host->clk);
Shawn Guo913413c2011-06-21 22:41:51 +0800356err_clk_get:
357 kfree(imx_data);
Shawn Guo85d65092011-05-27 23:48:12 +0800358 sdhci_pltfm_free(pdev);
359 return err;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200360}
361
Shawn Guo85d65092011-05-27 23:48:12 +0800362static int __devexit sdhci_esdhc_imx_remove(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200363{
Shawn Guo85d65092011-05-27 23:48:12 +0800364 struct sdhci_host *host = platform_get_drvdata(pdev);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200365 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100366 struct esdhc_platform_data *boarddata = host->mmc->parent->platform_data;
Richard Zhue1498602011-03-25 09:18:27 -0400367 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Shawn Guo85d65092011-05-27 23:48:12 +0800368 int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
369
370 sdhci_remove_host(host, dead);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100371
Shawn Guo913413c2011-06-21 22:41:51 +0800372 if (gpio_is_valid(boarddata->wp_gpio))
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100373 gpio_free(boarddata->wp_gpio);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200374
Shawn Guo913413c2011-06-21 22:41:51 +0800375 if (gpio_is_valid(boarddata->cd_gpio)) {
376 free_irq(gpio_to_irq(boarddata->cd_gpio), host);
Wolfram Sang7e29c302011-02-26 14:44:41 +0100377 gpio_free(boarddata->cd_gpio);
Wolfram Sang7e29c302011-02-26 14:44:41 +0100378 }
379
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200380 clk_disable(pltfm_host->clk);
381 clk_put(pltfm_host->clk);
Richard Zhue1498602011-03-25 09:18:27 -0400382 kfree(imx_data);
Shawn Guo85d65092011-05-27 23:48:12 +0800383
384 sdhci_pltfm_free(pdev);
385
386 return 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200387}
388
Shawn Guo85d65092011-05-27 23:48:12 +0800389static struct platform_driver sdhci_esdhc_imx_driver = {
390 .driver = {
391 .name = "sdhci-esdhc-imx",
392 .owner = THIS_MODULE,
393 },
394 .probe = sdhci_esdhc_imx_probe,
395 .remove = __devexit_p(sdhci_esdhc_imx_remove),
396#ifdef CONFIG_PM
397 .suspend = sdhci_pltfm_suspend,
398 .resume = sdhci_pltfm_resume,
399#endif
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200400};
Shawn Guo85d65092011-05-27 23:48:12 +0800401
402static int __init sdhci_esdhc_imx_init(void)
403{
404 return platform_driver_register(&sdhci_esdhc_imx_driver);
405}
406module_init(sdhci_esdhc_imx_init);
407
408static void __exit sdhci_esdhc_imx_exit(void)
409{
410 platform_driver_unregister(&sdhci_esdhc_imx_driver);
411}
412module_exit(sdhci_esdhc_imx_exit);
413
414MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
415MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
416MODULE_LICENSE("GPL v2");