blob: ac6f971b625f2345616e2140bb74268714512d5e [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
Tony Lin0d588642011-08-11 16:45:59 -040033#define SDHCI_CTRL_D3CD 0x08
Richard Zhu58ac8172011-03-21 13:22:16 +080034/* VENDOR SPEC register */
35#define SDHCI_VENDOR_SPEC 0xC0
36#define SDHCI_VENDOR_SPEC_SDIO_QUIRK 0x00000002
Shawn Guof750ba92011-11-10 16:39:32 +080037#define SDHCI_WTMK_LVL 0x44
Shawn Guo95a24822011-09-19 17:32:21 +080038#define SDHCI_MIX_CTRL 0x48
Richard Zhu58ac8172011-03-21 13:22:16 +080039
Richard Zhu58ac8172011-03-21 13:22:16 +080040/*
Richard Zhu97e4ba62011-08-11 16:51:46 -040041 * There is an INT DMA ERR mis-match between eSDHC and STD SDHC SPEC:
42 * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
43 * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
44 * Define this macro DMA error INT for fsl eSDHC
45 */
46#define SDHCI_INT_VENDOR_SPEC_DMA_ERR 0x10000000
47
48/*
Richard Zhu58ac8172011-03-21 13:22:16 +080049 * The CMDTYPE of the CMD register (offset 0xE) should be set to
50 * "11" when the STOP CMD12 is issued on imx53 to abort one
51 * open ended multi-blk IO. Otherwise the TC INT wouldn't
52 * be generated.
53 * In exact block transfer, the controller doesn't complete the
54 * operations automatically as required at the end of the
55 * transfer and remains on hold if the abort command is not sent.
56 * As a result, the TC flag is not asserted and SW received timeout
57 * exeception. Bit1 of Vendor Spec registor is used to fix it.
58 */
59#define ESDHC_FLAG_MULTIBLK_NO_INT (1 << 1)
Richard Zhue1498602011-03-25 09:18:27 -040060
Shawn Guo57ed3312011-06-30 09:24:26 +080061enum imx_esdhc_type {
62 IMX25_ESDHC,
63 IMX35_ESDHC,
64 IMX51_ESDHC,
65 IMX53_ESDHC,
Shawn Guo95a24822011-09-19 17:32:21 +080066 IMX6Q_USDHC,
Shawn Guo57ed3312011-06-30 09:24:26 +080067};
68
Richard Zhue1498602011-03-25 09:18:27 -040069struct pltfm_imx_data {
70 int flags;
71 u32 scratchpad;
Shawn Guo57ed3312011-06-30 09:24:26 +080072 enum imx_esdhc_type devtype;
Dong Aishenge62d8b82012-05-11 14:56:01 +080073 struct pinctrl *pinctrl;
Shawn Guo842afc02011-07-06 22:57:48 +080074 struct esdhc_platform_data boarddata;
Sascha Hauer52dac612012-03-07 09:31:34 +010075 struct clk *clk_ipg;
76 struct clk *clk_ahb;
77 struct clk *clk_per;
Richard Zhue1498602011-03-25 09:18:27 -040078};
79
Shawn Guo57ed3312011-06-30 09:24:26 +080080static struct platform_device_id imx_esdhc_devtype[] = {
81 {
82 .name = "sdhci-esdhc-imx25",
83 .driver_data = IMX25_ESDHC,
84 }, {
85 .name = "sdhci-esdhc-imx35",
86 .driver_data = IMX35_ESDHC,
87 }, {
88 .name = "sdhci-esdhc-imx51",
89 .driver_data = IMX51_ESDHC,
90 }, {
91 .name = "sdhci-esdhc-imx53",
92 .driver_data = IMX53_ESDHC,
93 }, {
Shawn Guo95a24822011-09-19 17:32:21 +080094 .name = "sdhci-usdhc-imx6q",
95 .driver_data = IMX6Q_USDHC,
96 }, {
Shawn Guo57ed3312011-06-30 09:24:26 +080097 /* sentinel */
98 }
99};
100MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
101
Shawn Guoabfafc22011-06-30 15:44:44 +0800102static const struct of_device_id imx_esdhc_dt_ids[] = {
103 { .compatible = "fsl,imx25-esdhc", .data = &imx_esdhc_devtype[IMX25_ESDHC], },
104 { .compatible = "fsl,imx35-esdhc", .data = &imx_esdhc_devtype[IMX35_ESDHC], },
105 { .compatible = "fsl,imx51-esdhc", .data = &imx_esdhc_devtype[IMX51_ESDHC], },
106 { .compatible = "fsl,imx53-esdhc", .data = &imx_esdhc_devtype[IMX53_ESDHC], },
Shawn Guo95a24822011-09-19 17:32:21 +0800107 { .compatible = "fsl,imx6q-usdhc", .data = &imx_esdhc_devtype[IMX6Q_USDHC], },
Shawn Guoabfafc22011-06-30 15:44:44 +0800108 { /* sentinel */ }
109};
110MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
111
Shawn Guo57ed3312011-06-30 09:24:26 +0800112static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
113{
114 return data->devtype == IMX25_ESDHC;
115}
116
117static inline int is_imx35_esdhc(struct pltfm_imx_data *data)
118{
119 return data->devtype == IMX35_ESDHC;
120}
121
122static inline int is_imx51_esdhc(struct pltfm_imx_data *data)
123{
124 return data->devtype == IMX51_ESDHC;
125}
126
127static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
128{
129 return data->devtype == IMX53_ESDHC;
130}
131
Shawn Guo95a24822011-09-19 17:32:21 +0800132static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
133{
134 return data->devtype == IMX6Q_USDHC;
135}
136
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200137static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
138{
139 void __iomem *base = host->ioaddr + (reg & ~0x3);
140 u32 shift = (reg & 0x3) * 8;
141
142 writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
143}
144
Wolfram Sang7e29c302011-02-26 14:44:41 +0100145static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
146{
Shawn Guo842afc02011-07-06 22:57:48 +0800147 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
148 struct pltfm_imx_data *imx_data = pltfm_host->priv;
149 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Richard Zhue1498602011-03-25 09:18:27 -0400150
Wolfram Sang7e29c302011-02-26 14:44:41 +0100151 u32 val = readl(host->ioaddr + reg);
152
Richard Zhu97e4ba62011-08-11 16:51:46 -0400153 if (unlikely(reg == SDHCI_CAPABILITIES)) {
154 /* In FSL esdhc IC module, only bit20 is used to indicate the
155 * ADMA2 capability of esdhc, but this bit is messed up on
156 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
157 * don't actually support ADMA2). So set the BROKEN_ADMA
158 * uirk on MX25/35 platforms.
159 */
160
161 if (val & SDHCI_CAN_DO_ADMA1) {
162 val &= ~SDHCI_CAN_DO_ADMA1;
163 val |= SDHCI_CAN_DO_ADMA2;
164 }
165 }
166
167 if (unlikely(reg == SDHCI_INT_STATUS)) {
168 if (val & SDHCI_INT_VENDOR_SPEC_DMA_ERR) {
169 val &= ~SDHCI_INT_VENDOR_SPEC_DMA_ERR;
170 val |= SDHCI_INT_ADMA_ERROR;
171 }
172 }
173
Wolfram Sang7e29c302011-02-26 14:44:41 +0100174 return val;
175}
176
177static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
178{
Richard Zhue1498602011-03-25 09:18:27 -0400179 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
180 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Shawn Guo842afc02011-07-06 22:57:48 +0800181 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Tony Lin0d588642011-08-11 16:45:59 -0400182 u32 data;
Richard Zhue1498602011-03-25 09:18:27 -0400183
Tony Lin0d588642011-08-11 16:45:59 -0400184 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
Tony Lin0d588642011-08-11 16:45:59 -0400185 if (val & SDHCI_INT_CARD_INT) {
186 /*
187 * Clear and then set D3CD bit to avoid missing the
188 * card interrupt. This is a eSDHC controller problem
189 * so we need to apply the following workaround: clear
190 * and set D3CD bit will make eSDHC re-sample the card
191 * interrupt. In case a card interrupt was lost,
192 * re-sample it by the following steps.
193 */
194 data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
195 data &= ~SDHCI_CTRL_D3CD;
196 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
197 data |= SDHCI_CTRL_D3CD;
198 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
199 }
200 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100201
Richard Zhu58ac8172011-03-21 13:22:16 +0800202 if (unlikely((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
203 && (reg == SDHCI_INT_STATUS)
204 && (val & SDHCI_INT_DATA_END))) {
205 u32 v;
206 v = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
207 v &= ~SDHCI_VENDOR_SPEC_SDIO_QUIRK;
208 writel(v, host->ioaddr + SDHCI_VENDOR_SPEC);
209 }
210
Richard Zhu97e4ba62011-08-11 16:51:46 -0400211 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
212 if (val & SDHCI_INT_ADMA_ERROR) {
213 val &= ~SDHCI_INT_ADMA_ERROR;
214 val |= SDHCI_INT_VENDOR_SPEC_DMA_ERR;
215 }
216 }
217
Wolfram Sang7e29c302011-02-26 14:44:41 +0100218 writel(val, host->ioaddr + reg);
219}
220
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200221static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
222{
Shawn Guoef4d0882013-01-15 23:30:27 +0800223 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
224 struct pltfm_imx_data *imx_data = pltfm_host->priv;
225
Shawn Guo95a24822011-09-19 17:32:21 +0800226 if (unlikely(reg == SDHCI_HOST_VERSION)) {
Shawn Guoef4d0882013-01-15 23:30:27 +0800227 reg ^= 2;
228 if (is_imx6q_usdhc(imx_data)) {
229 /*
230 * The usdhc register returns a wrong host version.
231 * Correct it here.
232 */
233 return SDHCI_SPEC_300;
234 }
Shawn Guo95a24822011-09-19 17:32:21 +0800235 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200236
237 return readw(host->ioaddr + reg);
238}
239
240static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
241{
242 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400243 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200244
245 switch (reg) {
246 case SDHCI_TRANSFER_MODE:
247 /*
248 * Postpone this write, we must do it together with a
249 * command write that is down below.
250 */
Richard Zhu58ac8172011-03-21 13:22:16 +0800251 if ((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
252 && (host->cmd->opcode == SD_IO_RW_EXTENDED)
253 && (host->cmd->data->blocks > 1)
254 && (host->cmd->data->flags & MMC_DATA_READ)) {
255 u32 v;
256 v = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
257 v |= SDHCI_VENDOR_SPEC_SDIO_QUIRK;
258 writel(v, host->ioaddr + SDHCI_VENDOR_SPEC);
259 }
Richard Zhue1498602011-03-25 09:18:27 -0400260 imx_data->scratchpad = val;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200261 return;
262 case SDHCI_COMMAND:
Sascha Hauer5b6b0ad2012-02-17 11:51:49 +0100263 if ((host->cmd->opcode == MMC_STOP_TRANSMISSION ||
264 host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
265 (imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
Richard Zhu58ac8172011-03-21 13:22:16 +0800266 val |= SDHCI_CMD_ABORTCMD;
Shawn Guo95a24822011-09-19 17:32:21 +0800267
268 if (is_imx6q_usdhc(imx_data)) {
269 u32 m = readl(host->ioaddr + SDHCI_MIX_CTRL);
270 m = imx_data->scratchpad | (m & 0xffff0000);
271 writel(m, host->ioaddr + SDHCI_MIX_CTRL);
272 writel(val << 16,
273 host->ioaddr + SDHCI_TRANSFER_MODE);
274 } else {
275 writel(val << 16 | imx_data->scratchpad,
276 host->ioaddr + SDHCI_TRANSFER_MODE);
277 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200278 return;
279 case SDHCI_BLOCK_SIZE:
280 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
281 break;
282 }
283 esdhc_clrset_le(host, 0xffff, val, reg);
284}
285
286static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
287{
Wilson Callan9a0985b2012-07-19 02:49:16 -0400288 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
289 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200290 u32 new_val;
291
292 switch (reg) {
293 case SDHCI_POWER_CONTROL:
294 /*
295 * FSL put some DMA bits here
296 * If your board has a regulator, code should be here
297 */
298 return;
299 case SDHCI_HOST_CONTROL:
Tony Lin0d588642011-08-11 16:45:59 -0400300 /* FSL messed up here, so we can just keep those three */
301 new_val = val & (SDHCI_CTRL_LED | \
302 SDHCI_CTRL_4BITBUS | \
303 SDHCI_CTRL_D3CD);
Masanari Iida7122bbb2012-08-05 23:25:40 +0900304 /* ensure the endianness */
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200305 new_val |= ESDHC_HOST_CONTROL_LE;
Wilson Callan9a0985b2012-07-19 02:49:16 -0400306 /* bits 8&9 are reserved on mx25 */
307 if (!is_imx25_esdhc(imx_data)) {
308 /* DMA mode bits are shifted */
309 new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
310 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200311
312 esdhc_clrset_le(host, 0xffff, new_val, reg);
313 return;
314 }
315 esdhc_clrset_le(host, 0xff, val, reg);
Shawn Guo913413c2011-06-21 22:41:51 +0800316
317 /*
318 * The esdhc has a design violation to SDHC spec which tells
319 * that software reset should not affect card detection circuit.
320 * But esdhc clears its SYSCTL register bits [0..2] during the
321 * software reset. This will stop those clocks that card detection
322 * circuit relies on. To work around it, we turn the clocks on back
323 * to keep card detection circuit functional.
324 */
325 if ((reg == SDHCI_SOFTWARE_RESET) && (val & 1))
326 esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200327}
328
329static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
330{
331 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
332
333 return clk_get_rate(pltfm_host->clk);
334}
335
336static unsigned int esdhc_pltfm_get_min_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) / 256 / 16;
341}
342
Shawn Guo913413c2011-06-21 22:41:51 +0800343static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
344{
Shawn Guo842afc02011-07-06 22:57:48 +0800345 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
346 struct pltfm_imx_data *imx_data = pltfm_host->priv;
347 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Shawn Guo913413c2011-06-21 22:41:51 +0800348
349 switch (boarddata->wp_type) {
350 case ESDHC_WP_GPIO:
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800351 return mmc_gpio_get_ro(host->mmc);
Shawn Guo913413c2011-06-21 22:41:51 +0800352 case ESDHC_WP_CONTROLLER:
353 return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
354 SDHCI_WRITE_PROTECT);
355 case ESDHC_WP_NONE:
356 break;
357 }
358
359 return -ENOSYS;
360}
361
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100362static struct sdhci_ops sdhci_esdhc_ops = {
Richard Zhue1498602011-03-25 09:18:27 -0400363 .read_l = esdhc_readl_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100364 .read_w = esdhc_readw_le,
Richard Zhue1498602011-03-25 09:18:27 -0400365 .write_l = esdhc_writel_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100366 .write_w = esdhc_writew_le,
367 .write_b = esdhc_writeb_le,
368 .set_clock = esdhc_set_clock,
369 .get_max_clock = esdhc_pltfm_get_max_clock,
370 .get_min_clock = esdhc_pltfm_get_min_clock,
Shawn Guo913413c2011-06-21 22:41:51 +0800371 .get_ro = esdhc_pltfm_get_ro,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100372};
373
Shawn Guo85d65092011-05-27 23:48:12 +0800374static struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
Richard Zhu97e4ba62011-08-11 16:51:46 -0400375 .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
376 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
377 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
Shawn Guo85d65092011-05-27 23:48:12 +0800378 | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
Shawn Guo85d65092011-05-27 23:48:12 +0800379 .ops = &sdhci_esdhc_ops,
380};
381
Shawn Guoabfafc22011-06-30 15:44:44 +0800382#ifdef CONFIG_OF
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500383static int
Shawn Guoabfafc22011-06-30 15:44:44 +0800384sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
385 struct esdhc_platform_data *boarddata)
386{
387 struct device_node *np = pdev->dev.of_node;
388
389 if (!np)
390 return -ENODEV;
391
Arnd Bergmann7f217792012-05-13 00:14:24 -0400392 if (of_get_property(np, "non-removable", NULL))
Shawn Guoabfafc22011-06-30 15:44:44 +0800393 boarddata->cd_type = ESDHC_CD_PERMANENT;
394
395 if (of_get_property(np, "fsl,cd-controller", NULL))
396 boarddata->cd_type = ESDHC_CD_CONTROLLER;
397
398 if (of_get_property(np, "fsl,wp-controller", NULL))
399 boarddata->wp_type = ESDHC_WP_CONTROLLER;
400
401 boarddata->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
402 if (gpio_is_valid(boarddata->cd_gpio))
403 boarddata->cd_type = ESDHC_CD_GPIO;
404
405 boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
406 if (gpio_is_valid(boarddata->wp_gpio))
407 boarddata->wp_type = ESDHC_WP_GPIO;
408
409 return 0;
410}
411#else
412static inline int
413sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
414 struct esdhc_platform_data *boarddata)
415{
416 return -ENODEV;
417}
418#endif
419
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500420static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200421{
Shawn Guoabfafc22011-06-30 15:44:44 +0800422 const struct of_device_id *of_id =
423 of_match_device(imx_esdhc_dt_ids, &pdev->dev);
Shawn Guo85d65092011-05-27 23:48:12 +0800424 struct sdhci_pltfm_host *pltfm_host;
425 struct sdhci_host *host;
426 struct esdhc_platform_data *boarddata;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100427 int err;
Richard Zhue1498602011-03-25 09:18:27 -0400428 struct pltfm_imx_data *imx_data;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200429
Shawn Guo85d65092011-05-27 23:48:12 +0800430 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata);
431 if (IS_ERR(host))
432 return PTR_ERR(host);
433
434 pltfm_host = sdhci_priv(host);
435
Shawn Guoe3af31c2012-11-26 14:39:43 +0800436 imx_data = devm_kzalloc(&pdev->dev, sizeof(*imx_data), GFP_KERNEL);
Shawn Guoabfafc22011-06-30 15:44:44 +0800437 if (!imx_data) {
438 err = -ENOMEM;
Shawn Guoe3af31c2012-11-26 14:39:43 +0800439 goto free_sdhci;
Shawn Guoabfafc22011-06-30 15:44:44 +0800440 }
Shawn Guo57ed3312011-06-30 09:24:26 +0800441
Shawn Guoabfafc22011-06-30 15:44:44 +0800442 if (of_id)
443 pdev->id_entry = of_id->data;
Shawn Guo57ed3312011-06-30 09:24:26 +0800444 imx_data->devtype = pdev->id_entry->driver_data;
Shawn Guo85d65092011-05-27 23:48:12 +0800445 pltfm_host->priv = imx_data;
446
Sascha Hauer52dac612012-03-07 09:31:34 +0100447 imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
448 if (IS_ERR(imx_data->clk_ipg)) {
449 err = PTR_ERR(imx_data->clk_ipg);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800450 goto free_sdhci;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200451 }
Sascha Hauer52dac612012-03-07 09:31:34 +0100452
453 imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
454 if (IS_ERR(imx_data->clk_ahb)) {
455 err = PTR_ERR(imx_data->clk_ahb);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800456 goto free_sdhci;
Sascha Hauer52dac612012-03-07 09:31:34 +0100457 }
458
459 imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
460 if (IS_ERR(imx_data->clk_per)) {
461 err = PTR_ERR(imx_data->clk_per);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800462 goto free_sdhci;
Sascha Hauer52dac612012-03-07 09:31:34 +0100463 }
464
465 pltfm_host->clk = imx_data->clk_per;
466
467 clk_prepare_enable(imx_data->clk_per);
468 clk_prepare_enable(imx_data->clk_ipg);
469 clk_prepare_enable(imx_data->clk_ahb);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200470
Dong Aishenge62d8b82012-05-11 14:56:01 +0800471 imx_data->pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
472 if (IS_ERR(imx_data->pinctrl)) {
473 err = PTR_ERR(imx_data->pinctrl);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800474 goto disable_clk;
Dong Aishenge62d8b82012-05-11 14:56:01 +0800475 }
476
Eric Bénardb89152822012-04-18 02:30:20 +0200477 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
Eric Bénard37865fe2010-10-23 01:57:21 +0200478
Shawn Guo57ed3312011-06-30 09:24:26 +0800479 if (is_imx25_esdhc(imx_data) || is_imx35_esdhc(imx_data))
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100480 /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
Richard Zhu97e4ba62011-08-11 16:51:46 -0400481 host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK
482 | SDHCI_QUIRK_BROKEN_ADMA;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100483
Shawn Guo57ed3312011-06-30 09:24:26 +0800484 if (is_imx53_esdhc(imx_data))
Richard Zhu58ac8172011-03-21 13:22:16 +0800485 imx_data->flags |= ESDHC_FLAG_MULTIBLK_NO_INT;
486
Shawn Guof750ba92011-11-10 16:39:32 +0800487 /*
488 * The imx6q ROM code will change the default watermark level setting
489 * to something insane. Change it back here.
490 */
491 if (is_imx6q_usdhc(imx_data))
492 writel(0x08100810, host->ioaddr + SDHCI_WTMK_LVL);
493
Shawn Guo842afc02011-07-06 22:57:48 +0800494 boarddata = &imx_data->boarddata;
Shawn Guoabfafc22011-06-30 15:44:44 +0800495 if (sdhci_esdhc_imx_probe_dt(pdev, boarddata) < 0) {
496 if (!host->mmc->parent->platform_data) {
497 dev_err(mmc_dev(host->mmc), "no board data!\n");
498 err = -EINVAL;
Shawn Guoe3af31c2012-11-26 14:39:43 +0800499 goto disable_clk;
Shawn Guoabfafc22011-06-30 15:44:44 +0800500 }
501 imx_data->boarddata = *((struct esdhc_platform_data *)
502 host->mmc->parent->platform_data);
503 }
Shawn Guo913413c2011-06-21 22:41:51 +0800504
505 /* write_protect */
506 if (boarddata->wp_type == ESDHC_WP_GPIO) {
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800507 err = mmc_gpio_request_ro(host->mmc, boarddata->wp_gpio);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100508 if (err) {
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800509 dev_err(mmc_dev(host->mmc),
510 "failed to request write-protect gpio!\n");
511 goto disable_clk;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100512 }
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800513 host->mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
Shawn Guo913413c2011-06-21 22:41:51 +0800514 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100515
Shawn Guo913413c2011-06-21 22:41:51 +0800516 /* card_detect */
Shawn Guo913413c2011-06-21 22:41:51 +0800517 switch (boarddata->cd_type) {
518 case ESDHC_CD_GPIO:
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800519 err = mmc_gpio_request_cd(host->mmc, boarddata->cd_gpio);
Wolfram Sang7e29c302011-02-26 14:44:41 +0100520 if (err) {
Shawn Guo913413c2011-06-21 22:41:51 +0800521 dev_err(mmc_dev(host->mmc),
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800522 "failed to request card-detect gpio!\n");
Shawn Guoe3af31c2012-11-26 14:39:43 +0800523 goto disable_clk;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100524 }
Shawn Guo913413c2011-06-21 22:41:51 +0800525 /* fall through */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100526
Shawn Guo913413c2011-06-21 22:41:51 +0800527 case ESDHC_CD_CONTROLLER:
528 /* we have a working card_detect back */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100529 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
Shawn Guo913413c2011-06-21 22:41:51 +0800530 break;
531
532 case ESDHC_CD_PERMANENT:
533 host->mmc->caps = MMC_CAP_NONREMOVABLE;
534 break;
535
536 case ESDHC_CD_NONE:
537 break;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100538 }
Eric Bénard16a790b2010-10-23 01:57:22 +0200539
Shawn Guo85d65092011-05-27 23:48:12 +0800540 err = sdhci_add_host(host);
541 if (err)
Shawn Guoe3af31c2012-11-26 14:39:43 +0800542 goto disable_clk;
Shawn Guo85d65092011-05-27 23:48:12 +0800543
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200544 return 0;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100545
Shawn Guoe3af31c2012-11-26 14:39:43 +0800546disable_clk:
Sascha Hauer52dac612012-03-07 09:31:34 +0100547 clk_disable_unprepare(imx_data->clk_per);
548 clk_disable_unprepare(imx_data->clk_ipg);
549 clk_disable_unprepare(imx_data->clk_ahb);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800550free_sdhci:
Shawn Guo85d65092011-05-27 23:48:12 +0800551 sdhci_pltfm_free(pdev);
552 return err;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200553}
554
Bill Pemberton6e0ee712012-11-19 13:26:03 -0500555static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200556{
Shawn Guo85d65092011-05-27 23:48:12 +0800557 struct sdhci_host *host = platform_get_drvdata(pdev);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200558 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400559 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Shawn Guo85d65092011-05-27 23:48:12 +0800560 int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
561
562 sdhci_remove_host(host, dead);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100563
Sascha Hauer52dac612012-03-07 09:31:34 +0100564 clk_disable_unprepare(imx_data->clk_per);
565 clk_disable_unprepare(imx_data->clk_ipg);
566 clk_disable_unprepare(imx_data->clk_ahb);
567
Shawn Guo85d65092011-05-27 23:48:12 +0800568 sdhci_pltfm_free(pdev);
569
570 return 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200571}
572
Shawn Guo85d65092011-05-27 23:48:12 +0800573static struct platform_driver sdhci_esdhc_imx_driver = {
574 .driver = {
575 .name = "sdhci-esdhc-imx",
576 .owner = THIS_MODULE,
Shawn Guoabfafc22011-06-30 15:44:44 +0800577 .of_match_table = imx_esdhc_dt_ids,
Manuel Lauss29495aa2011-11-03 11:09:45 +0100578 .pm = SDHCI_PLTFM_PMOPS,
Shawn Guo85d65092011-05-27 23:48:12 +0800579 },
Shawn Guo57ed3312011-06-30 09:24:26 +0800580 .id_table = imx_esdhc_devtype,
Shawn Guo85d65092011-05-27 23:48:12 +0800581 .probe = sdhci_esdhc_imx_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -0500582 .remove = sdhci_esdhc_imx_remove,
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200583};
Shawn Guo85d65092011-05-27 23:48:12 +0800584
Axel Lind1f81a62011-11-26 12:55:43 +0800585module_platform_driver(sdhci_esdhc_imx_driver);
Shawn Guo85d65092011-05-27 23:48:12 +0800586
587MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
588MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
589MODULE_LICENSE("GPL v2");