blob: f7ee5e67516ce8ae46cc4950951e339763f8a997 [file] [log] [blame]
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001/*
2 * Freescale eSDHC i.MX controller driver for the platform bus.
3 *
4 * derived from the OF-version.
5 *
6 * Copyright (c) 2010 Pengutronix e.K.
7 * Author: Wolfram Sang <w.sang@pengutronix.de>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License.
12 */
13
14#include <linux/io.h>
15#include <linux/delay.h>
16#include <linux/err.h>
17#include <linux/clk.h>
Wolfram Sang0c6d49c2011-02-26 14:44:39 +010018#include <linux/gpio.h>
Shawn Guo66506f72011-08-15 10:28:18 +080019#include <linux/module.h>
Richard Zhue1498602011-03-25 09:18:27 -040020#include <linux/slab.h>
Wolfram Sang95f25ef2010-10-15 12:21:04 +020021#include <linux/mmc/host.h>
Richard Zhu58ac8172011-03-21 13:22:16 +080022#include <linux/mmc/mmc.h>
23#include <linux/mmc/sdio.h>
Shawn Guofbe5fdd2012-12-11 22:32:20 +080024#include <linux/mmc/slot-gpio.h>
Shawn Guoabfafc22011-06-30 15:44:44 +080025#include <linux/of.h>
26#include <linux/of_device.h>
27#include <linux/of_gpio.h>
Dong Aishenge62d8b82012-05-11 14:56:01 +080028#include <linux/pinctrl/consumer.h>
Arnd Bergmann82906b12012-08-24 15:14:29 +020029#include <linux/platform_data/mmc-esdhc-imx.h>
Wolfram Sang95f25ef2010-10-15 12:21:04 +020030#include "sdhci-pltfm.h"
31#include "sdhci-esdhc.h"
32
Shawn Guo60bf6392013-01-15 23:36:53 +080033#define ESDHC_CTRL_D3CD 0x08
Richard Zhu58ac8172011-03-21 13:22:16 +080034/* VENDOR SPEC register */
Shawn Guo60bf6392013-01-15 23:36:53 +080035#define ESDHC_VENDOR_SPEC 0xc0
36#define ESDHC_VENDOR_SPEC_SDIO_QUIRK (1 << 1)
37#define ESDHC_WTMK_LVL 0x44
38#define ESDHC_MIX_CTRL 0x48
Shawn Guo2a15f982013-01-21 19:02:26 +080039#define ESDHC_MIX_CTRL_AC23EN (1 << 7)
40/* Bits 3 and 6 are not SDHCI standard definitions */
41#define ESDHC_MIX_CTRL_SDHCI_MASK 0xb7
Richard Zhu58ac8172011-03-21 13:22:16 +080042
Richard Zhu58ac8172011-03-21 13:22:16 +080043/*
Sascha Haueraf510792013-01-21 19:02:28 +080044 * Our interpretation of the SDHCI_HOST_CONTROL register
45 */
46#define ESDHC_CTRL_4BITBUS (0x1 << 1)
47#define ESDHC_CTRL_8BITBUS (0x2 << 1)
48#define ESDHC_CTRL_BUSWIDTH_MASK (0x3 << 1)
49
50/*
Richard Zhu97e4ba62011-08-11 16:51:46 -040051 * There is an INT DMA ERR mis-match between eSDHC and STD SDHC SPEC:
52 * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
53 * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
54 * Define this macro DMA error INT for fsl eSDHC
55 */
Shawn Guo60bf6392013-01-15 23:36:53 +080056#define ESDHC_INT_VENDOR_SPEC_DMA_ERR (1 << 28)
Richard Zhu97e4ba62011-08-11 16:51:46 -040057
58/*
Richard Zhu58ac8172011-03-21 13:22:16 +080059 * The CMDTYPE of the CMD register (offset 0xE) should be set to
60 * "11" when the STOP CMD12 is issued on imx53 to abort one
61 * open ended multi-blk IO. Otherwise the TC INT wouldn't
62 * be generated.
63 * In exact block transfer, the controller doesn't complete the
64 * operations automatically as required at the end of the
65 * transfer and remains on hold if the abort command is not sent.
66 * As a result, the TC flag is not asserted and SW received timeout
67 * exeception. Bit1 of Vendor Spec registor is used to fix it.
68 */
69#define ESDHC_FLAG_MULTIBLK_NO_INT (1 << 1)
Richard Zhue1498602011-03-25 09:18:27 -040070
Shawn Guo57ed3312011-06-30 09:24:26 +080071enum imx_esdhc_type {
72 IMX25_ESDHC,
73 IMX35_ESDHC,
74 IMX51_ESDHC,
75 IMX53_ESDHC,
Shawn Guo95a24822011-09-19 17:32:21 +080076 IMX6Q_USDHC,
Shawn Guo57ed3312011-06-30 09:24:26 +080077};
78
Richard Zhue1498602011-03-25 09:18:27 -040079struct pltfm_imx_data {
80 int flags;
81 u32 scratchpad;
Shawn Guo57ed3312011-06-30 09:24:26 +080082 enum imx_esdhc_type devtype;
Dong Aishenge62d8b82012-05-11 14:56:01 +080083 struct pinctrl *pinctrl;
Shawn Guo842afc02011-07-06 22:57:48 +080084 struct esdhc_platform_data boarddata;
Sascha Hauer52dac612012-03-07 09:31:34 +010085 struct clk *clk_ipg;
86 struct clk *clk_ahb;
87 struct clk *clk_per;
Richard Zhue1498602011-03-25 09:18:27 -040088};
89
Shawn Guo57ed3312011-06-30 09:24:26 +080090static struct platform_device_id imx_esdhc_devtype[] = {
91 {
92 .name = "sdhci-esdhc-imx25",
93 .driver_data = IMX25_ESDHC,
94 }, {
95 .name = "sdhci-esdhc-imx35",
96 .driver_data = IMX35_ESDHC,
97 }, {
98 .name = "sdhci-esdhc-imx51",
99 .driver_data = IMX51_ESDHC,
100 }, {
101 .name = "sdhci-esdhc-imx53",
102 .driver_data = IMX53_ESDHC,
103 }, {
Shawn Guo95a24822011-09-19 17:32:21 +0800104 .name = "sdhci-usdhc-imx6q",
105 .driver_data = IMX6Q_USDHC,
106 }, {
Shawn Guo57ed3312011-06-30 09:24:26 +0800107 /* sentinel */
108 }
109};
110MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
111
Shawn Guoabfafc22011-06-30 15:44:44 +0800112static const struct of_device_id imx_esdhc_dt_ids[] = {
113 { .compatible = "fsl,imx25-esdhc", .data = &imx_esdhc_devtype[IMX25_ESDHC], },
114 { .compatible = "fsl,imx35-esdhc", .data = &imx_esdhc_devtype[IMX35_ESDHC], },
115 { .compatible = "fsl,imx51-esdhc", .data = &imx_esdhc_devtype[IMX51_ESDHC], },
116 { .compatible = "fsl,imx53-esdhc", .data = &imx_esdhc_devtype[IMX53_ESDHC], },
Shawn Guo95a24822011-09-19 17:32:21 +0800117 { .compatible = "fsl,imx6q-usdhc", .data = &imx_esdhc_devtype[IMX6Q_USDHC], },
Shawn Guoabfafc22011-06-30 15:44:44 +0800118 { /* sentinel */ }
119};
120MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
121
Shawn Guo57ed3312011-06-30 09:24:26 +0800122static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
123{
124 return data->devtype == IMX25_ESDHC;
125}
126
127static inline int is_imx35_esdhc(struct pltfm_imx_data *data)
128{
129 return data->devtype == IMX35_ESDHC;
130}
131
132static inline int is_imx51_esdhc(struct pltfm_imx_data *data)
133{
134 return data->devtype == IMX51_ESDHC;
135}
136
137static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
138{
139 return data->devtype == IMX53_ESDHC;
140}
141
Shawn Guo95a24822011-09-19 17:32:21 +0800142static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
143{
144 return data->devtype == IMX6Q_USDHC;
145}
146
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200147static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
148{
149 void __iomem *base = host->ioaddr + (reg & ~0x3);
150 u32 shift = (reg & 0x3) * 8;
151
152 writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
153}
154
Wolfram Sang7e29c302011-02-26 14:44:41 +0100155static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
156{
Wolfram Sang7e29c302011-02-26 14:44:41 +0100157 u32 val = readl(host->ioaddr + reg);
158
Richard Zhu97e4ba62011-08-11 16:51:46 -0400159 if (unlikely(reg == SDHCI_CAPABILITIES)) {
160 /* In FSL esdhc IC module, only bit20 is used to indicate the
161 * ADMA2 capability of esdhc, but this bit is messed up on
162 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
163 * don't actually support ADMA2). So set the BROKEN_ADMA
164 * uirk on MX25/35 platforms.
165 */
166
167 if (val & SDHCI_CAN_DO_ADMA1) {
168 val &= ~SDHCI_CAN_DO_ADMA1;
169 val |= SDHCI_CAN_DO_ADMA2;
170 }
171 }
172
173 if (unlikely(reg == SDHCI_INT_STATUS)) {
Shawn Guo60bf6392013-01-15 23:36:53 +0800174 if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
175 val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
Richard Zhu97e4ba62011-08-11 16:51:46 -0400176 val |= SDHCI_INT_ADMA_ERROR;
177 }
178 }
179
Wolfram Sang7e29c302011-02-26 14:44:41 +0100180 return val;
181}
182
183static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
184{
Richard Zhue1498602011-03-25 09:18:27 -0400185 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
186 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Tony Lin0d588642011-08-11 16:45:59 -0400187 u32 data;
Richard Zhue1498602011-03-25 09:18:27 -0400188
Tony Lin0d588642011-08-11 16:45:59 -0400189 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
Tony Lin0d588642011-08-11 16:45:59 -0400190 if (val & SDHCI_INT_CARD_INT) {
191 /*
192 * Clear and then set D3CD bit to avoid missing the
193 * card interrupt. This is a eSDHC controller problem
194 * so we need to apply the following workaround: clear
195 * and set D3CD bit will make eSDHC re-sample the card
196 * interrupt. In case a card interrupt was lost,
197 * re-sample it by the following steps.
198 */
199 data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
Shawn Guo60bf6392013-01-15 23:36:53 +0800200 data &= ~ESDHC_CTRL_D3CD;
Tony Lin0d588642011-08-11 16:45:59 -0400201 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
Shawn Guo60bf6392013-01-15 23:36:53 +0800202 data |= ESDHC_CTRL_D3CD;
Tony Lin0d588642011-08-11 16:45:59 -0400203 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
204 }
205 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100206
Richard Zhu58ac8172011-03-21 13:22:16 +0800207 if (unlikely((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
208 && (reg == SDHCI_INT_STATUS)
209 && (val & SDHCI_INT_DATA_END))) {
210 u32 v;
Shawn Guo60bf6392013-01-15 23:36:53 +0800211 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
212 v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
213 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
Richard Zhu58ac8172011-03-21 13:22:16 +0800214 }
215
Richard Zhu97e4ba62011-08-11 16:51:46 -0400216 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
217 if (val & SDHCI_INT_ADMA_ERROR) {
218 val &= ~SDHCI_INT_ADMA_ERROR;
Shawn Guo60bf6392013-01-15 23:36:53 +0800219 val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
Richard Zhu97e4ba62011-08-11 16:51:46 -0400220 }
221 }
222
Wolfram Sang7e29c302011-02-26 14:44:41 +0100223 writel(val, host->ioaddr + reg);
224}
225
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200226static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
227{
Shawn Guoef4d0882013-01-15 23:30:27 +0800228 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
229 struct pltfm_imx_data *imx_data = pltfm_host->priv;
230
Shawn Guo95a24822011-09-19 17:32:21 +0800231 if (unlikely(reg == SDHCI_HOST_VERSION)) {
Shawn Guoef4d0882013-01-15 23:30:27 +0800232 reg ^= 2;
233 if (is_imx6q_usdhc(imx_data)) {
234 /*
235 * The usdhc register returns a wrong host version.
236 * Correct it here.
237 */
238 return SDHCI_SPEC_300;
239 }
Shawn Guo95a24822011-09-19 17:32:21 +0800240 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200241
242 return readw(host->ioaddr + reg);
243}
244
245static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
246{
247 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400248 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200249
250 switch (reg) {
251 case SDHCI_TRANSFER_MODE:
Richard Zhu58ac8172011-03-21 13:22:16 +0800252 if ((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
253 && (host->cmd->opcode == SD_IO_RW_EXTENDED)
254 && (host->cmd->data->blocks > 1)
255 && (host->cmd->data->flags & MMC_DATA_READ)) {
256 u32 v;
Shawn Guo60bf6392013-01-15 23:36:53 +0800257 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
258 v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
259 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
Richard Zhu58ac8172011-03-21 13:22:16 +0800260 }
Shawn Guo69f54692013-01-21 19:02:24 +0800261
262 if (is_imx6q_usdhc(imx_data)) {
263 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
Shawn Guo2a15f982013-01-21 19:02:26 +0800264 /* Swap AC23 bit */
265 if (val & SDHCI_TRNS_AUTO_CMD23) {
266 val &= ~SDHCI_TRNS_AUTO_CMD23;
267 val |= ESDHC_MIX_CTRL_AC23EN;
268 }
269 m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
Shawn Guo69f54692013-01-21 19:02:24 +0800270 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
271 } else {
272 /*
273 * Postpone this write, we must do it together with a
274 * command write that is down below.
275 */
276 imx_data->scratchpad = val;
277 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200278 return;
279 case SDHCI_COMMAND:
Sascha Hauer5b6b0ad2012-02-17 11:51:49 +0100280 if ((host->cmd->opcode == MMC_STOP_TRANSMISSION ||
281 host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
282 (imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
Richard Zhu58ac8172011-03-21 13:22:16 +0800283 val |= SDHCI_CMD_ABORTCMD;
Shawn Guo95a24822011-09-19 17:32:21 +0800284
Shawn Guo69f54692013-01-21 19:02:24 +0800285 if (is_imx6q_usdhc(imx_data))
Shawn Guo95a24822011-09-19 17:32:21 +0800286 writel(val << 16,
287 host->ioaddr + SDHCI_TRANSFER_MODE);
Shawn Guo69f54692013-01-21 19:02:24 +0800288 else
Shawn Guo95a24822011-09-19 17:32:21 +0800289 writel(val << 16 | imx_data->scratchpad,
290 host->ioaddr + SDHCI_TRANSFER_MODE);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200291 return;
292 case SDHCI_BLOCK_SIZE:
293 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
294 break;
295 }
296 esdhc_clrset_le(host, 0xffff, val, reg);
297}
298
299static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
300{
Wilson Callan9a0985b2012-07-19 02:49:16 -0400301 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
302 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200303 u32 new_val;
Sascha Haueraf510792013-01-21 19:02:28 +0800304 u32 mask;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200305
306 switch (reg) {
307 case SDHCI_POWER_CONTROL:
308 /*
309 * FSL put some DMA bits here
310 * If your board has a regulator, code should be here
311 */
312 return;
313 case SDHCI_HOST_CONTROL:
Shawn Guo6b40d182013-01-15 23:36:52 +0800314 /* FSL messed up here, so we need to manually compose it. */
Sascha Haueraf510792013-01-21 19:02:28 +0800315 new_val = val & SDHCI_CTRL_LED;
Masanari Iida7122bbb2012-08-05 23:25:40 +0900316 /* ensure the endianness */
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200317 new_val |= ESDHC_HOST_CONTROL_LE;
Wilson Callan9a0985b2012-07-19 02:49:16 -0400318 /* bits 8&9 are reserved on mx25 */
319 if (!is_imx25_esdhc(imx_data)) {
320 /* DMA mode bits are shifted */
321 new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
322 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200323
Sascha Haueraf510792013-01-21 19:02:28 +0800324 /*
325 * Do not touch buswidth bits here. This is done in
326 * esdhc_pltfm_bus_width.
327 */
328 mask = 0xffff & ~ESDHC_CTRL_BUSWIDTH_MASK;
329
330 esdhc_clrset_le(host, mask, new_val, reg);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200331 return;
332 }
333 esdhc_clrset_le(host, 0xff, val, reg);
Shawn Guo913413c2011-06-21 22:41:51 +0800334
335 /*
336 * The esdhc has a design violation to SDHC spec which tells
337 * that software reset should not affect card detection circuit.
338 * But esdhc clears its SYSCTL register bits [0..2] during the
339 * software reset. This will stop those clocks that card detection
340 * circuit relies on. To work around it, we turn the clocks on back
341 * to keep card detection circuit functional.
342 */
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800343 if ((reg == SDHCI_SOFTWARE_RESET) && (val & 1)) {
Shawn Guo913413c2011-06-21 22:41:51 +0800344 esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800345 /*
346 * The reset on usdhc fails to clear MIX_CTRL register.
347 * Do it manually here.
348 */
349 if (is_imx6q_usdhc(imx_data))
350 writel(0, host->ioaddr + ESDHC_MIX_CTRL);
351 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200352}
353
354static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
355{
356 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
357
358 return clk_get_rate(pltfm_host->clk);
359}
360
361static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
362{
363 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
364
365 return clk_get_rate(pltfm_host->clk) / 256 / 16;
366}
367
Shawn Guo913413c2011-06-21 22:41:51 +0800368static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
369{
Shawn Guo842afc02011-07-06 22:57:48 +0800370 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
371 struct pltfm_imx_data *imx_data = pltfm_host->priv;
372 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Shawn Guo913413c2011-06-21 22:41:51 +0800373
374 switch (boarddata->wp_type) {
375 case ESDHC_WP_GPIO:
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800376 return mmc_gpio_get_ro(host->mmc);
Shawn Guo913413c2011-06-21 22:41:51 +0800377 case ESDHC_WP_CONTROLLER:
378 return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
379 SDHCI_WRITE_PROTECT);
380 case ESDHC_WP_NONE:
381 break;
382 }
383
384 return -ENOSYS;
385}
386
Sascha Haueraf510792013-01-21 19:02:28 +0800387static int esdhc_pltfm_bus_width(struct sdhci_host *host, int width)
388{
389 u32 ctrl;
390
391 switch (width) {
392 case MMC_BUS_WIDTH_8:
393 ctrl = ESDHC_CTRL_8BITBUS;
394 break;
395 case MMC_BUS_WIDTH_4:
396 ctrl = ESDHC_CTRL_4BITBUS;
397 break;
398 default:
399 ctrl = 0;
400 break;
401 }
402
403 esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
404 SDHCI_HOST_CONTROL);
405
406 return 0;
407}
408
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100409static struct sdhci_ops sdhci_esdhc_ops = {
Richard Zhue1498602011-03-25 09:18:27 -0400410 .read_l = esdhc_readl_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100411 .read_w = esdhc_readw_le,
Richard Zhue1498602011-03-25 09:18:27 -0400412 .write_l = esdhc_writel_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100413 .write_w = esdhc_writew_le,
414 .write_b = esdhc_writeb_le,
415 .set_clock = esdhc_set_clock,
416 .get_max_clock = esdhc_pltfm_get_max_clock,
417 .get_min_clock = esdhc_pltfm_get_min_clock,
Shawn Guo913413c2011-06-21 22:41:51 +0800418 .get_ro = esdhc_pltfm_get_ro,
Sascha Haueraf510792013-01-21 19:02:28 +0800419 .platform_bus_width = esdhc_pltfm_bus_width,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100420};
421
Shawn Guo85d65092011-05-27 23:48:12 +0800422static struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
Richard Zhu97e4ba62011-08-11 16:51:46 -0400423 .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
424 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
425 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
Shawn Guo85d65092011-05-27 23:48:12 +0800426 | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
Shawn Guo85d65092011-05-27 23:48:12 +0800427 .ops = &sdhci_esdhc_ops,
428};
429
Shawn Guoabfafc22011-06-30 15:44:44 +0800430#ifdef CONFIG_OF
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500431static int
Shawn Guoabfafc22011-06-30 15:44:44 +0800432sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
433 struct esdhc_platform_data *boarddata)
434{
435 struct device_node *np = pdev->dev.of_node;
436
437 if (!np)
438 return -ENODEV;
439
Arnd Bergmann7f217792012-05-13 00:14:24 -0400440 if (of_get_property(np, "non-removable", NULL))
Shawn Guoabfafc22011-06-30 15:44:44 +0800441 boarddata->cd_type = ESDHC_CD_PERMANENT;
442
443 if (of_get_property(np, "fsl,cd-controller", NULL))
444 boarddata->cd_type = ESDHC_CD_CONTROLLER;
445
446 if (of_get_property(np, "fsl,wp-controller", NULL))
447 boarddata->wp_type = ESDHC_WP_CONTROLLER;
448
449 boarddata->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
450 if (gpio_is_valid(boarddata->cd_gpio))
451 boarddata->cd_type = ESDHC_CD_GPIO;
452
453 boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
454 if (gpio_is_valid(boarddata->wp_gpio))
455 boarddata->wp_type = ESDHC_WP_GPIO;
456
Sascha Haueraf510792013-01-21 19:02:28 +0800457 of_property_read_u32(np, "bus-width", &boarddata->max_bus_width);
458
Shawn Guoabfafc22011-06-30 15:44:44 +0800459 return 0;
460}
461#else
462static inline int
463sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
464 struct esdhc_platform_data *boarddata)
465{
466 return -ENODEV;
467}
468#endif
469
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500470static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200471{
Shawn Guoabfafc22011-06-30 15:44:44 +0800472 const struct of_device_id *of_id =
473 of_match_device(imx_esdhc_dt_ids, &pdev->dev);
Shawn Guo85d65092011-05-27 23:48:12 +0800474 struct sdhci_pltfm_host *pltfm_host;
475 struct sdhci_host *host;
476 struct esdhc_platform_data *boarddata;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100477 int err;
Richard Zhue1498602011-03-25 09:18:27 -0400478 struct pltfm_imx_data *imx_data;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200479
Shawn Guo85d65092011-05-27 23:48:12 +0800480 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata);
481 if (IS_ERR(host))
482 return PTR_ERR(host);
483
484 pltfm_host = sdhci_priv(host);
485
Shawn Guoe3af31c2012-11-26 14:39:43 +0800486 imx_data = devm_kzalloc(&pdev->dev, sizeof(*imx_data), GFP_KERNEL);
Shawn Guoabfafc22011-06-30 15:44:44 +0800487 if (!imx_data) {
488 err = -ENOMEM;
Shawn Guoe3af31c2012-11-26 14:39:43 +0800489 goto free_sdhci;
Shawn Guoabfafc22011-06-30 15:44:44 +0800490 }
Shawn Guo57ed3312011-06-30 09:24:26 +0800491
Shawn Guoabfafc22011-06-30 15:44:44 +0800492 if (of_id)
493 pdev->id_entry = of_id->data;
Shawn Guo57ed3312011-06-30 09:24:26 +0800494 imx_data->devtype = pdev->id_entry->driver_data;
Shawn Guo85d65092011-05-27 23:48:12 +0800495 pltfm_host->priv = imx_data;
496
Sascha Hauer52dac612012-03-07 09:31:34 +0100497 imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
498 if (IS_ERR(imx_data->clk_ipg)) {
499 err = PTR_ERR(imx_data->clk_ipg);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800500 goto free_sdhci;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200501 }
Sascha Hauer52dac612012-03-07 09:31:34 +0100502
503 imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
504 if (IS_ERR(imx_data->clk_ahb)) {
505 err = PTR_ERR(imx_data->clk_ahb);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800506 goto free_sdhci;
Sascha Hauer52dac612012-03-07 09:31:34 +0100507 }
508
509 imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
510 if (IS_ERR(imx_data->clk_per)) {
511 err = PTR_ERR(imx_data->clk_per);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800512 goto free_sdhci;
Sascha Hauer52dac612012-03-07 09:31:34 +0100513 }
514
515 pltfm_host->clk = imx_data->clk_per;
516
517 clk_prepare_enable(imx_data->clk_per);
518 clk_prepare_enable(imx_data->clk_ipg);
519 clk_prepare_enable(imx_data->clk_ahb);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200520
Dong Aishenge62d8b82012-05-11 14:56:01 +0800521 imx_data->pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
522 if (IS_ERR(imx_data->pinctrl)) {
523 err = PTR_ERR(imx_data->pinctrl);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800524 goto disable_clk;
Dong Aishenge62d8b82012-05-11 14:56:01 +0800525 }
526
Eric BĂ©nardb89152822012-04-18 02:30:20 +0200527 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
Eric BĂ©nard37865fe2010-10-23 01:57:21 +0200528
Shawn Guo57ed3312011-06-30 09:24:26 +0800529 if (is_imx25_esdhc(imx_data) || is_imx35_esdhc(imx_data))
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100530 /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
Richard Zhu97e4ba62011-08-11 16:51:46 -0400531 host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK
532 | SDHCI_QUIRK_BROKEN_ADMA;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100533
Shawn Guo57ed3312011-06-30 09:24:26 +0800534 if (is_imx53_esdhc(imx_data))
Richard Zhu58ac8172011-03-21 13:22:16 +0800535 imx_data->flags |= ESDHC_FLAG_MULTIBLK_NO_INT;
536
Shawn Guof750ba92011-11-10 16:39:32 +0800537 /*
538 * The imx6q ROM code will change the default watermark level setting
539 * to something insane. Change it back here.
540 */
541 if (is_imx6q_usdhc(imx_data))
Shawn Guo60bf6392013-01-15 23:36:53 +0800542 writel(0x08100810, host->ioaddr + ESDHC_WTMK_LVL);
Shawn Guof750ba92011-11-10 16:39:32 +0800543
Shawn Guo842afc02011-07-06 22:57:48 +0800544 boarddata = &imx_data->boarddata;
Shawn Guoabfafc22011-06-30 15:44:44 +0800545 if (sdhci_esdhc_imx_probe_dt(pdev, boarddata) < 0) {
546 if (!host->mmc->parent->platform_data) {
547 dev_err(mmc_dev(host->mmc), "no board data!\n");
548 err = -EINVAL;
Shawn Guoe3af31c2012-11-26 14:39:43 +0800549 goto disable_clk;
Shawn Guoabfafc22011-06-30 15:44:44 +0800550 }
551 imx_data->boarddata = *((struct esdhc_platform_data *)
552 host->mmc->parent->platform_data);
553 }
Shawn Guo913413c2011-06-21 22:41:51 +0800554
555 /* write_protect */
556 if (boarddata->wp_type == ESDHC_WP_GPIO) {
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800557 err = mmc_gpio_request_ro(host->mmc, boarddata->wp_gpio);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100558 if (err) {
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800559 dev_err(mmc_dev(host->mmc),
560 "failed to request write-protect gpio!\n");
561 goto disable_clk;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100562 }
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800563 host->mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
Shawn Guo913413c2011-06-21 22:41:51 +0800564 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100565
Shawn Guo913413c2011-06-21 22:41:51 +0800566 /* card_detect */
Shawn Guo913413c2011-06-21 22:41:51 +0800567 switch (boarddata->cd_type) {
568 case ESDHC_CD_GPIO:
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800569 err = mmc_gpio_request_cd(host->mmc, boarddata->cd_gpio);
Wolfram Sang7e29c302011-02-26 14:44:41 +0100570 if (err) {
Shawn Guo913413c2011-06-21 22:41:51 +0800571 dev_err(mmc_dev(host->mmc),
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800572 "failed to request card-detect gpio!\n");
Shawn Guoe3af31c2012-11-26 14:39:43 +0800573 goto disable_clk;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100574 }
Shawn Guo913413c2011-06-21 22:41:51 +0800575 /* fall through */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100576
Shawn Guo913413c2011-06-21 22:41:51 +0800577 case ESDHC_CD_CONTROLLER:
578 /* we have a working card_detect back */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100579 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
Shawn Guo913413c2011-06-21 22:41:51 +0800580 break;
581
582 case ESDHC_CD_PERMANENT:
583 host->mmc->caps = MMC_CAP_NONREMOVABLE;
584 break;
585
586 case ESDHC_CD_NONE:
587 break;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100588 }
Eric BĂ©nard16a790b2010-10-23 01:57:22 +0200589
Sascha Haueraf510792013-01-21 19:02:28 +0800590 switch (boarddata->max_bus_width) {
591 case 8:
592 host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA;
593 break;
594 case 4:
595 host->mmc->caps |= MMC_CAP_4_BIT_DATA;
596 break;
597 case 1:
598 default:
599 host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
600 break;
601 }
602
Shawn Guo85d65092011-05-27 23:48:12 +0800603 err = sdhci_add_host(host);
604 if (err)
Shawn Guoe3af31c2012-11-26 14:39:43 +0800605 goto disable_clk;
Shawn Guo85d65092011-05-27 23:48:12 +0800606
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200607 return 0;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100608
Shawn Guoe3af31c2012-11-26 14:39:43 +0800609disable_clk:
Sascha Hauer52dac612012-03-07 09:31:34 +0100610 clk_disable_unprepare(imx_data->clk_per);
611 clk_disable_unprepare(imx_data->clk_ipg);
612 clk_disable_unprepare(imx_data->clk_ahb);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800613free_sdhci:
Shawn Guo85d65092011-05-27 23:48:12 +0800614 sdhci_pltfm_free(pdev);
615 return err;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200616}
617
Bill Pemberton6e0ee712012-11-19 13:26:03 -0500618static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200619{
Shawn Guo85d65092011-05-27 23:48:12 +0800620 struct sdhci_host *host = platform_get_drvdata(pdev);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200621 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400622 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Shawn Guo85d65092011-05-27 23:48:12 +0800623 int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
624
625 sdhci_remove_host(host, dead);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100626
Sascha Hauer52dac612012-03-07 09:31:34 +0100627 clk_disable_unprepare(imx_data->clk_per);
628 clk_disable_unprepare(imx_data->clk_ipg);
629 clk_disable_unprepare(imx_data->clk_ahb);
630
Shawn Guo85d65092011-05-27 23:48:12 +0800631 sdhci_pltfm_free(pdev);
632
633 return 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200634}
635
Shawn Guo85d65092011-05-27 23:48:12 +0800636static struct platform_driver sdhci_esdhc_imx_driver = {
637 .driver = {
638 .name = "sdhci-esdhc-imx",
639 .owner = THIS_MODULE,
Shawn Guoabfafc22011-06-30 15:44:44 +0800640 .of_match_table = imx_esdhc_dt_ids,
Manuel Lauss29495aa2011-11-03 11:09:45 +0100641 .pm = SDHCI_PLTFM_PMOPS,
Shawn Guo85d65092011-05-27 23:48:12 +0800642 },
Shawn Guo57ed3312011-06-30 09:24:26 +0800643 .id_table = imx_esdhc_devtype,
Shawn Guo85d65092011-05-27 23:48:12 +0800644 .probe = sdhci_esdhc_imx_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -0500645 .remove = sdhci_esdhc_imx_remove,
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200646};
Shawn Guo85d65092011-05-27 23:48:12 +0800647
Axel Lind1f81a62011-11-26 12:55:43 +0800648module_platform_driver(sdhci_esdhc_imx_driver);
Shawn Guo85d65092011-05-27 23:48:12 +0800649
650MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
651MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
652MODULE_LICENSE("GPL v2");