blob: f906c206901d11b8a9ec127620dc4c0838234144 [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)
Dong Aisheng03221912013-09-13 19:11:34 +080037#define ESDHC_VENDOR_SPEC_VSELECT (1 << 1)
Dong Aishengfed2f6e2013-09-13 19:11:33 +080038#define ESDHC_VENDOR_SPEC_FRC_SDCLK_ON (1 << 8)
Shawn Guo60bf6392013-01-15 23:36:53 +080039#define ESDHC_WTMK_LVL 0x44
40#define ESDHC_MIX_CTRL 0x48
Shawn Guo2a15f982013-01-21 19:02:26 +080041#define ESDHC_MIX_CTRL_AC23EN (1 << 7)
Dong Aisheng03221912013-09-13 19:11:34 +080042#define ESDHC_MIX_CTRL_EXE_TUNE (1 << 22)
43#define ESDHC_MIX_CTRL_SMPCLK_SEL (1 << 23)
44#define ESDHC_MIX_CTRL_FBCLK_SEL (1 << 25)
Shawn Guo2a15f982013-01-21 19:02:26 +080045/* Bits 3 and 6 are not SDHCI standard definitions */
46#define ESDHC_MIX_CTRL_SDHCI_MASK 0xb7
Richard Zhu58ac8172011-03-21 13:22:16 +080047
Dong Aisheng03221912013-09-13 19:11:34 +080048/* tune control register */
49#define ESDHC_TUNE_CTRL_STATUS 0x68
50#define ESDHC_TUNE_CTRL_STEP 1
51#define ESDHC_TUNE_CTRL_MIN 0
52#define ESDHC_TUNE_CTRL_MAX ((1 << 7) - 1)
53
54#define ESDHC_TUNING_BLOCK_PATTERN_LEN 64
55
Richard Zhu58ac8172011-03-21 13:22:16 +080056/*
Sascha Haueraf510792013-01-21 19:02:28 +080057 * Our interpretation of the SDHCI_HOST_CONTROL register
58 */
59#define ESDHC_CTRL_4BITBUS (0x1 << 1)
60#define ESDHC_CTRL_8BITBUS (0x2 << 1)
61#define ESDHC_CTRL_BUSWIDTH_MASK (0x3 << 1)
62
63/*
Richard Zhu97e4ba62011-08-11 16:51:46 -040064 * There is an INT DMA ERR mis-match between eSDHC and STD SDHC SPEC:
65 * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
66 * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
67 * Define this macro DMA error INT for fsl eSDHC
68 */
Shawn Guo60bf6392013-01-15 23:36:53 +080069#define ESDHC_INT_VENDOR_SPEC_DMA_ERR (1 << 28)
Richard Zhu97e4ba62011-08-11 16:51:46 -040070
71/*
Richard Zhu58ac8172011-03-21 13:22:16 +080072 * The CMDTYPE of the CMD register (offset 0xE) should be set to
73 * "11" when the STOP CMD12 is issued on imx53 to abort one
74 * open ended multi-blk IO. Otherwise the TC INT wouldn't
75 * be generated.
76 * In exact block transfer, the controller doesn't complete the
77 * operations automatically as required at the end of the
78 * transfer and remains on hold if the abort command is not sent.
79 * As a result, the TC flag is not asserted and SW received timeout
80 * exeception. Bit1 of Vendor Spec registor is used to fix it.
81 */
82#define ESDHC_FLAG_MULTIBLK_NO_INT (1 << 1)
Richard Zhue1498602011-03-25 09:18:27 -040083
Shawn Guo57ed3312011-06-30 09:24:26 +080084enum imx_esdhc_type {
85 IMX25_ESDHC,
86 IMX35_ESDHC,
87 IMX51_ESDHC,
88 IMX53_ESDHC,
Shawn Guo95a24822011-09-19 17:32:21 +080089 IMX6Q_USDHC,
Shawn Guo57ed3312011-06-30 09:24:26 +080090};
91
Richard Zhue1498602011-03-25 09:18:27 -040092struct pltfm_imx_data {
93 int flags;
94 u32 scratchpad;
Shawn Guo57ed3312011-06-30 09:24:26 +080095 enum imx_esdhc_type devtype;
Dong Aishenge62d8b82012-05-11 14:56:01 +080096 struct pinctrl *pinctrl;
Shawn Guo842afc02011-07-06 22:57:48 +080097 struct esdhc_platform_data boarddata;
Sascha Hauer52dac612012-03-07 09:31:34 +010098 struct clk *clk_ipg;
99 struct clk *clk_ahb;
100 struct clk *clk_per;
Lucas Stach361b8482013-03-15 09:49:26 +0100101 enum {
102 NO_CMD_PENDING, /* no multiblock command pending*/
103 MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */
104 WAIT_FOR_INT, /* sent CMD12, waiting for response INT */
105 } multiblock_status;
Dong Aisheng03221912013-09-13 19:11:34 +0800106 u32 uhs_mode;
Richard Zhue1498602011-03-25 09:18:27 -0400107};
108
Shawn Guo57ed3312011-06-30 09:24:26 +0800109static struct platform_device_id imx_esdhc_devtype[] = {
110 {
111 .name = "sdhci-esdhc-imx25",
112 .driver_data = IMX25_ESDHC,
113 }, {
114 .name = "sdhci-esdhc-imx35",
115 .driver_data = IMX35_ESDHC,
116 }, {
117 .name = "sdhci-esdhc-imx51",
118 .driver_data = IMX51_ESDHC,
119 }, {
120 .name = "sdhci-esdhc-imx53",
121 .driver_data = IMX53_ESDHC,
122 }, {
Shawn Guo95a24822011-09-19 17:32:21 +0800123 .name = "sdhci-usdhc-imx6q",
124 .driver_data = IMX6Q_USDHC,
125 }, {
Shawn Guo57ed3312011-06-30 09:24:26 +0800126 /* sentinel */
127 }
128};
129MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
130
Shawn Guoabfafc22011-06-30 15:44:44 +0800131static const struct of_device_id imx_esdhc_dt_ids[] = {
132 { .compatible = "fsl,imx25-esdhc", .data = &imx_esdhc_devtype[IMX25_ESDHC], },
133 { .compatible = "fsl,imx35-esdhc", .data = &imx_esdhc_devtype[IMX35_ESDHC], },
134 { .compatible = "fsl,imx51-esdhc", .data = &imx_esdhc_devtype[IMX51_ESDHC], },
135 { .compatible = "fsl,imx53-esdhc", .data = &imx_esdhc_devtype[IMX53_ESDHC], },
Shawn Guo95a24822011-09-19 17:32:21 +0800136 { .compatible = "fsl,imx6q-usdhc", .data = &imx_esdhc_devtype[IMX6Q_USDHC], },
Shawn Guoabfafc22011-06-30 15:44:44 +0800137 { /* sentinel */ }
138};
139MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
140
Shawn Guo57ed3312011-06-30 09:24:26 +0800141static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
142{
143 return data->devtype == IMX25_ESDHC;
144}
145
146static inline int is_imx35_esdhc(struct pltfm_imx_data *data)
147{
148 return data->devtype == IMX35_ESDHC;
149}
150
151static inline int is_imx51_esdhc(struct pltfm_imx_data *data)
152{
153 return data->devtype == IMX51_ESDHC;
154}
155
156static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
157{
158 return data->devtype == IMX53_ESDHC;
159}
160
Shawn Guo95a24822011-09-19 17:32:21 +0800161static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
162{
163 return data->devtype == IMX6Q_USDHC;
164}
165
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200166static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
167{
168 void __iomem *base = host->ioaddr + (reg & ~0x3);
169 u32 shift = (reg & 0x3) * 8;
170
171 writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
172}
173
Wolfram Sang7e29c302011-02-26 14:44:41 +0100174static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
175{
Lucas Stach361b8482013-03-15 09:49:26 +0100176 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
177 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100178 u32 val = readl(host->ioaddr + reg);
179
Dong Aisheng03221912013-09-13 19:11:34 +0800180 if (unlikely(reg == SDHCI_PRESENT_STATE)) {
181 u32 fsl_prss = val;
182 /* save the least 20 bits */
183 val = fsl_prss & 0x000FFFFF;
184 /* move dat[0-3] bits */
185 val |= (fsl_prss & 0x0F000000) >> 4;
186 /* move cmd line bit */
187 val |= (fsl_prss & 0x00800000) << 1;
188 }
189
Richard Zhu97e4ba62011-08-11 16:51:46 -0400190 if (unlikely(reg == SDHCI_CAPABILITIES)) {
191 /* In FSL esdhc IC module, only bit20 is used to indicate the
192 * ADMA2 capability of esdhc, but this bit is messed up on
193 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
194 * don't actually support ADMA2). So set the BROKEN_ADMA
195 * uirk on MX25/35 platforms.
196 */
197
198 if (val & SDHCI_CAN_DO_ADMA1) {
199 val &= ~SDHCI_CAN_DO_ADMA1;
200 val |= SDHCI_CAN_DO_ADMA2;
201 }
202 }
203
Dong Aisheng03221912013-09-13 19:11:34 +0800204 if (unlikely(reg == SDHCI_CAPABILITIES_1) && is_imx6q_usdhc(imx_data))
205 val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104
206 | SDHCI_SUPPORT_SDR50;
207
208 if (unlikely(reg == SDHCI_MAX_CURRENT) && is_imx6q_usdhc(imx_data)) {
209 val = 0;
210 val |= 0xFF << SDHCI_MAX_CURRENT_330_SHIFT;
211 val |= 0xFF << SDHCI_MAX_CURRENT_300_SHIFT;
212 val |= 0xFF << SDHCI_MAX_CURRENT_180_SHIFT;
213 }
214
Richard Zhu97e4ba62011-08-11 16:51:46 -0400215 if (unlikely(reg == SDHCI_INT_STATUS)) {
Shawn Guo60bf6392013-01-15 23:36:53 +0800216 if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
217 val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
Richard Zhu97e4ba62011-08-11 16:51:46 -0400218 val |= SDHCI_INT_ADMA_ERROR;
219 }
Lucas Stach361b8482013-03-15 09:49:26 +0100220
221 /*
222 * mask off the interrupt we get in response to the manually
223 * sent CMD12
224 */
225 if ((imx_data->multiblock_status == WAIT_FOR_INT) &&
226 ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) {
227 val &= ~SDHCI_INT_RESPONSE;
228 writel(SDHCI_INT_RESPONSE, host->ioaddr +
229 SDHCI_INT_STATUS);
230 imx_data->multiblock_status = NO_CMD_PENDING;
231 }
Richard Zhu97e4ba62011-08-11 16:51:46 -0400232 }
233
Wolfram Sang7e29c302011-02-26 14:44:41 +0100234 return val;
235}
236
237static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
238{
Richard Zhue1498602011-03-25 09:18:27 -0400239 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
240 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Tony Lin0d588642011-08-11 16:45:59 -0400241 u32 data;
Richard Zhue1498602011-03-25 09:18:27 -0400242
Tony Lin0d588642011-08-11 16:45:59 -0400243 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
Tony Lin0d588642011-08-11 16:45:59 -0400244 if (val & SDHCI_INT_CARD_INT) {
245 /*
246 * Clear and then set D3CD bit to avoid missing the
247 * card interrupt. This is a eSDHC controller problem
248 * so we need to apply the following workaround: clear
249 * and set D3CD bit will make eSDHC re-sample the card
250 * interrupt. In case a card interrupt was lost,
251 * re-sample it by the following steps.
252 */
253 data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
Shawn Guo60bf6392013-01-15 23:36:53 +0800254 data &= ~ESDHC_CTRL_D3CD;
Tony Lin0d588642011-08-11 16:45:59 -0400255 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
Shawn Guo60bf6392013-01-15 23:36:53 +0800256 data |= ESDHC_CTRL_D3CD;
Tony Lin0d588642011-08-11 16:45:59 -0400257 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
258 }
259 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100260
Richard Zhu58ac8172011-03-21 13:22:16 +0800261 if (unlikely((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
262 && (reg == SDHCI_INT_STATUS)
263 && (val & SDHCI_INT_DATA_END))) {
264 u32 v;
Shawn Guo60bf6392013-01-15 23:36:53 +0800265 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
266 v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
267 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
Lucas Stach361b8482013-03-15 09:49:26 +0100268
269 if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS)
270 {
271 /* send a manual CMD12 with RESPTYP=none */
272 data = MMC_STOP_TRANSMISSION << 24 |
273 SDHCI_CMD_ABORTCMD << 16;
274 writel(data, host->ioaddr + SDHCI_TRANSFER_MODE);
275 imx_data->multiblock_status = WAIT_FOR_INT;
276 }
Richard Zhu58ac8172011-03-21 13:22:16 +0800277 }
278
Richard Zhu97e4ba62011-08-11 16:51:46 -0400279 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
280 if (val & SDHCI_INT_ADMA_ERROR) {
281 val &= ~SDHCI_INT_ADMA_ERROR;
Shawn Guo60bf6392013-01-15 23:36:53 +0800282 val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
Richard Zhu97e4ba62011-08-11 16:51:46 -0400283 }
284 }
285
Wolfram Sang7e29c302011-02-26 14:44:41 +0100286 writel(val, host->ioaddr + reg);
287}
288
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200289static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
290{
Shawn Guoef4d0882013-01-15 23:30:27 +0800291 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
292 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aisheng03221912013-09-13 19:11:34 +0800293 u16 ret = 0;
294 u32 val;
Shawn Guoef4d0882013-01-15 23:30:27 +0800295
Shawn Guo95a24822011-09-19 17:32:21 +0800296 if (unlikely(reg == SDHCI_HOST_VERSION)) {
Shawn Guoef4d0882013-01-15 23:30:27 +0800297 reg ^= 2;
298 if (is_imx6q_usdhc(imx_data)) {
299 /*
300 * The usdhc register returns a wrong host version.
301 * Correct it here.
302 */
303 return SDHCI_SPEC_300;
304 }
Shawn Guo95a24822011-09-19 17:32:21 +0800305 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200306
Dong Aisheng03221912013-09-13 19:11:34 +0800307 if (unlikely(reg == SDHCI_HOST_CONTROL2)) {
308 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
309 if (val & ESDHC_VENDOR_SPEC_VSELECT)
310 ret |= SDHCI_CTRL_VDD_180;
311
312 if (is_imx6q_usdhc(imx_data)) {
313 val = readl(host->ioaddr + ESDHC_MIX_CTRL);
314 if (val & ESDHC_MIX_CTRL_EXE_TUNE)
315 ret |= SDHCI_CTRL_EXEC_TUNING;
316 if (val & ESDHC_MIX_CTRL_SMPCLK_SEL)
317 ret |= SDHCI_CTRL_TUNED_CLK;
318 }
319
320 ret |= (imx_data->uhs_mode & SDHCI_CTRL_UHS_MASK);
321 ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
322
323 return ret;
324 }
325
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200326 return readw(host->ioaddr + reg);
327}
328
329static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
330{
331 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400332 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aisheng03221912013-09-13 19:11:34 +0800333 u32 new_val = 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200334
335 switch (reg) {
Dong Aisheng03221912013-09-13 19:11:34 +0800336 case SDHCI_CLOCK_CONTROL:
337 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
338 if (val & SDHCI_CLOCK_CARD_EN)
339 new_val |= ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
340 else
341 new_val &= ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
342 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
343 return;
344 case SDHCI_HOST_CONTROL2:
345 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
346 if (val & SDHCI_CTRL_VDD_180)
347 new_val |= ESDHC_VENDOR_SPEC_VSELECT;
348 else
349 new_val &= ~ESDHC_VENDOR_SPEC_VSELECT;
350 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
351 imx_data->uhs_mode = val & SDHCI_CTRL_UHS_MASK;
352 new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
353 if (val & SDHCI_CTRL_TUNED_CLK)
354 new_val |= ESDHC_MIX_CTRL_SMPCLK_SEL;
355 else
356 new_val &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
357 writel(new_val , host->ioaddr + ESDHC_MIX_CTRL);
358 return;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200359 case SDHCI_TRANSFER_MODE:
Richard Zhu58ac8172011-03-21 13:22:16 +0800360 if ((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
361 && (host->cmd->opcode == SD_IO_RW_EXTENDED)
362 && (host->cmd->data->blocks > 1)
363 && (host->cmd->data->flags & MMC_DATA_READ)) {
364 u32 v;
Shawn Guo60bf6392013-01-15 23:36:53 +0800365 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
366 v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
367 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
Richard Zhu58ac8172011-03-21 13:22:16 +0800368 }
Shawn Guo69f54692013-01-21 19:02:24 +0800369
370 if (is_imx6q_usdhc(imx_data)) {
371 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
Shawn Guo2a15f982013-01-21 19:02:26 +0800372 /* Swap AC23 bit */
373 if (val & SDHCI_TRNS_AUTO_CMD23) {
374 val &= ~SDHCI_TRNS_AUTO_CMD23;
375 val |= ESDHC_MIX_CTRL_AC23EN;
376 }
377 m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
Shawn Guo69f54692013-01-21 19:02:24 +0800378 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
379 } else {
380 /*
381 * Postpone this write, we must do it together with a
382 * command write that is down below.
383 */
384 imx_data->scratchpad = val;
385 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200386 return;
387 case SDHCI_COMMAND:
Lucas Stach361b8482013-03-15 09:49:26 +0100388 if (host->cmd->opcode == MMC_STOP_TRANSMISSION)
Richard Zhu58ac8172011-03-21 13:22:16 +0800389 val |= SDHCI_CMD_ABORTCMD;
Shawn Guo95a24822011-09-19 17:32:21 +0800390
Lucas Stach361b8482013-03-15 09:49:26 +0100391 if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
392 (imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
393 imx_data->multiblock_status = MULTIBLK_IN_PROCESS;
394
Shawn Guo69f54692013-01-21 19:02:24 +0800395 if (is_imx6q_usdhc(imx_data))
Shawn Guo95a24822011-09-19 17:32:21 +0800396 writel(val << 16,
397 host->ioaddr + SDHCI_TRANSFER_MODE);
Shawn Guo69f54692013-01-21 19:02:24 +0800398 else
Shawn Guo95a24822011-09-19 17:32:21 +0800399 writel(val << 16 | imx_data->scratchpad,
400 host->ioaddr + SDHCI_TRANSFER_MODE);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200401 return;
402 case SDHCI_BLOCK_SIZE:
403 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
404 break;
405 }
406 esdhc_clrset_le(host, 0xffff, val, reg);
407}
408
409static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
410{
Wilson Callan9a0985b2012-07-19 02:49:16 -0400411 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
412 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200413 u32 new_val;
Sascha Haueraf510792013-01-21 19:02:28 +0800414 u32 mask;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200415
416 switch (reg) {
417 case SDHCI_POWER_CONTROL:
418 /*
419 * FSL put some DMA bits here
420 * If your board has a regulator, code should be here
421 */
422 return;
423 case SDHCI_HOST_CONTROL:
Shawn Guo6b40d182013-01-15 23:36:52 +0800424 /* FSL messed up here, so we need to manually compose it. */
Sascha Haueraf510792013-01-21 19:02:28 +0800425 new_val = val & SDHCI_CTRL_LED;
Masanari Iida7122bbb2012-08-05 23:25:40 +0900426 /* ensure the endianness */
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200427 new_val |= ESDHC_HOST_CONTROL_LE;
Wilson Callan9a0985b2012-07-19 02:49:16 -0400428 /* bits 8&9 are reserved on mx25 */
429 if (!is_imx25_esdhc(imx_data)) {
430 /* DMA mode bits are shifted */
431 new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
432 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200433
Sascha Haueraf510792013-01-21 19:02:28 +0800434 /*
435 * Do not touch buswidth bits here. This is done in
436 * esdhc_pltfm_bus_width.
Martin Fuzzeyf6825742013-04-15 17:08:35 +0200437 * Do not touch the D3CD bit either which is used for the
438 * SDIO interrupt errata workaround.
Sascha Haueraf510792013-01-21 19:02:28 +0800439 */
Martin Fuzzeyf6825742013-04-15 17:08:35 +0200440 mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD);
Sascha Haueraf510792013-01-21 19:02:28 +0800441
442 esdhc_clrset_le(host, mask, new_val, reg);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200443 return;
444 }
445 esdhc_clrset_le(host, 0xff, val, reg);
Shawn Guo913413c2011-06-21 22:41:51 +0800446
447 /*
448 * The esdhc has a design violation to SDHC spec which tells
449 * that software reset should not affect card detection circuit.
450 * But esdhc clears its SYSCTL register bits [0..2] during the
451 * software reset. This will stop those clocks that card detection
452 * circuit relies on. To work around it, we turn the clocks on back
453 * to keep card detection circuit functional.
454 */
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800455 if ((reg == SDHCI_SOFTWARE_RESET) && (val & 1)) {
Shawn Guo913413c2011-06-21 22:41:51 +0800456 esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
Shawn Guo58c8c4f2013-01-21 19:02:25 +0800457 /*
458 * The reset on usdhc fails to clear MIX_CTRL register.
459 * Do it manually here.
460 */
461 if (is_imx6q_usdhc(imx_data))
462 writel(0, host->ioaddr + ESDHC_MIX_CTRL);
463 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200464}
465
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200466static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
467{
468 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
469 struct pltfm_imx_data *imx_data = pltfm_host->priv;
470 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
471
472 u32 f_host = clk_get_rate(pltfm_host->clk);
473
474 if (boarddata->f_max && (boarddata->f_max < f_host))
475 return boarddata->f_max;
476 else
477 return f_host;
478}
479
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200480static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
481{
482 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
483
484 return clk_get_rate(pltfm_host->clk) / 256 / 16;
485}
486
Lucas Stach8ba95802013-06-05 15:13:25 +0200487static inline void esdhc_pltfm_set_clock(struct sdhci_host *host,
488 unsigned int clock)
489{
490 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800491 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Dong Aishengd31fc002013-09-13 19:11:32 +0800492 unsigned int host_clock = clk_get_rate(pltfm_host->clk);
493 int pre_div = 2;
494 int div = 1;
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800495 u32 temp, val;
Lucas Stach8ba95802013-06-05 15:13:25 +0200496
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800497 if (clock == 0) {
498 if (is_imx6q_usdhc(imx_data)) {
499 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
500 writel(val & ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
501 host->ioaddr + ESDHC_VENDOR_SPEC);
502 }
Dong Aishengd31fc002013-09-13 19:11:32 +0800503 goto out;
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800504 }
Dong Aishengd31fc002013-09-13 19:11:32 +0800505
506 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
507 temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
508 | ESDHC_CLOCK_MASK);
509 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
510
511 while (host_clock / pre_div / 16 > clock && pre_div < 256)
512 pre_div *= 2;
513
514 while (host_clock / pre_div / div > clock && div < 16)
515 div++;
516
517 dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
518 clock, host_clock / pre_div / div);
519
520 pre_div >>= 1;
521 div--;
522
523 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
524 temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
525 | (div << ESDHC_DIVIDER_SHIFT)
526 | (pre_div << ESDHC_PREDIV_SHIFT));
527 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
Dong Aishengfed2f6e2013-09-13 19:11:33 +0800528
529 if (is_imx6q_usdhc(imx_data)) {
530 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
531 writel(val | ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
532 host->ioaddr + ESDHC_VENDOR_SPEC);
533 }
534
Dong Aishengd31fc002013-09-13 19:11:32 +0800535 mdelay(1);
536out:
537 host->clock = clock;
Lucas Stach8ba95802013-06-05 15:13:25 +0200538}
539
Shawn Guo913413c2011-06-21 22:41:51 +0800540static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
541{
Shawn Guo842afc02011-07-06 22:57:48 +0800542 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
543 struct pltfm_imx_data *imx_data = pltfm_host->priv;
544 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Shawn Guo913413c2011-06-21 22:41:51 +0800545
546 switch (boarddata->wp_type) {
547 case ESDHC_WP_GPIO:
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800548 return mmc_gpio_get_ro(host->mmc);
Shawn Guo913413c2011-06-21 22:41:51 +0800549 case ESDHC_WP_CONTROLLER:
550 return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
551 SDHCI_WRITE_PROTECT);
552 case ESDHC_WP_NONE:
553 break;
554 }
555
556 return -ENOSYS;
557}
558
Sascha Haueraf510792013-01-21 19:02:28 +0800559static int esdhc_pltfm_bus_width(struct sdhci_host *host, int width)
560{
561 u32 ctrl;
562
563 switch (width) {
564 case MMC_BUS_WIDTH_8:
565 ctrl = ESDHC_CTRL_8BITBUS;
566 break;
567 case MMC_BUS_WIDTH_4:
568 ctrl = ESDHC_CTRL_4BITBUS;
569 break;
570 default:
571 ctrl = 0;
572 break;
573 }
574
575 esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
576 SDHCI_HOST_CONTROL);
577
578 return 0;
579}
580
Dong Aisheng03221912013-09-13 19:11:34 +0800581static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val)
582{
583 u32 reg;
584
585 /* FIXME: delay a bit for card to be ready for next tuning due to errors */
586 mdelay(1);
587
588 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
589 reg |= ESDHC_MIX_CTRL_EXE_TUNE | ESDHC_MIX_CTRL_SMPCLK_SEL |
590 ESDHC_MIX_CTRL_FBCLK_SEL;
591 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
592 writel(val << 8, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
593 dev_dbg(mmc_dev(host->mmc),
594 "tunning with delay 0x%x ESDHC_TUNE_CTRL_STATUS 0x%x\n",
595 val, readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS));
596}
597
598static void esdhc_request_done(struct mmc_request *mrq)
599{
600 complete(&mrq->completion);
601}
602
603static int esdhc_send_tuning_cmd(struct sdhci_host *host, u32 opcode)
604{
605 struct mmc_command cmd = {0};
606 struct mmc_request mrq = {0};
607 struct mmc_data data = {0};
608 struct scatterlist sg;
609 char tuning_pattern[ESDHC_TUNING_BLOCK_PATTERN_LEN];
610
611 cmd.opcode = opcode;
612 cmd.arg = 0;
613 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
614
615 data.blksz = ESDHC_TUNING_BLOCK_PATTERN_LEN;
616 data.blocks = 1;
617 data.flags = MMC_DATA_READ;
618 data.sg = &sg;
619 data.sg_len = 1;
620
621 sg_init_one(&sg, tuning_pattern, sizeof(tuning_pattern));
622
623 mrq.cmd = &cmd;
624 mrq.cmd->mrq = &mrq;
625 mrq.data = &data;
626 mrq.data->mrq = &mrq;
627 mrq.cmd->data = mrq.data;
628
629 mrq.done = esdhc_request_done;
630 init_completion(&(mrq.completion));
631
632 disable_irq(host->irq);
633 spin_lock(&host->lock);
634 host->mrq = &mrq;
635
636 sdhci_send_command(host, mrq.cmd);
637
638 spin_unlock(&host->lock);
639 enable_irq(host->irq);
640
641 wait_for_completion(&mrq.completion);
642
643 if (cmd.error)
644 return cmd.error;
645 if (data.error)
646 return data.error;
647
648 return 0;
649}
650
651static void esdhc_post_tuning(struct sdhci_host *host)
652{
653 u32 reg;
654
655 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
656 reg &= ~ESDHC_MIX_CTRL_EXE_TUNE;
657 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
658}
659
660static int esdhc_executing_tuning(struct sdhci_host *host, u32 opcode)
661{
662 int min, max, avg, ret;
663
664 /* find the mininum delay first which can pass tuning */
665 min = ESDHC_TUNE_CTRL_MIN;
666 while (min < ESDHC_TUNE_CTRL_MAX) {
667 esdhc_prepare_tuning(host, min);
668 if (!esdhc_send_tuning_cmd(host, opcode))
669 break;
670 min += ESDHC_TUNE_CTRL_STEP;
671 }
672
673 /* find the maxinum delay which can not pass tuning */
674 max = min + ESDHC_TUNE_CTRL_STEP;
675 while (max < ESDHC_TUNE_CTRL_MAX) {
676 esdhc_prepare_tuning(host, max);
677 if (esdhc_send_tuning_cmd(host, opcode)) {
678 max -= ESDHC_TUNE_CTRL_STEP;
679 break;
680 }
681 max += ESDHC_TUNE_CTRL_STEP;
682 }
683
684 /* use average delay to get the best timing */
685 avg = (min + max) / 2;
686 esdhc_prepare_tuning(host, avg);
687 ret = esdhc_send_tuning_cmd(host, opcode);
688 esdhc_post_tuning(host);
689
690 dev_dbg(mmc_dev(host->mmc), "tunning %s at 0x%x ret %d\n",
691 ret ? "failed" : "passed", avg, ret);
692
693 return ret;
694}
695
Lars-Peter Clausenc9155682013-03-13 19:26:05 +0100696static const struct sdhci_ops sdhci_esdhc_ops = {
Richard Zhue1498602011-03-25 09:18:27 -0400697 .read_l = esdhc_readl_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100698 .read_w = esdhc_readw_le,
Richard Zhue1498602011-03-25 09:18:27 -0400699 .write_l = esdhc_writel_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100700 .write_w = esdhc_writew_le,
701 .write_b = esdhc_writeb_le,
Lucas Stach8ba95802013-06-05 15:13:25 +0200702 .set_clock = esdhc_pltfm_set_clock,
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200703 .get_max_clock = esdhc_pltfm_get_max_clock,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100704 .get_min_clock = esdhc_pltfm_get_min_clock,
Shawn Guo913413c2011-06-21 22:41:51 +0800705 .get_ro = esdhc_pltfm_get_ro,
Sascha Haueraf510792013-01-21 19:02:28 +0800706 .platform_bus_width = esdhc_pltfm_bus_width,
Dong Aisheng03221912013-09-13 19:11:34 +0800707 .platform_execute_tuning = esdhc_executing_tuning,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100708};
709
Lars-Peter Clausen1db5eeb2013-03-13 19:26:03 +0100710static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
Richard Zhu97e4ba62011-08-11 16:51:46 -0400711 .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
712 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
713 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
Shawn Guo85d65092011-05-27 23:48:12 +0800714 | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
Shawn Guo85d65092011-05-27 23:48:12 +0800715 .ops = &sdhci_esdhc_ops,
716};
717
Shawn Guoabfafc22011-06-30 15:44:44 +0800718#ifdef CONFIG_OF
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500719static int
Shawn Guoabfafc22011-06-30 15:44:44 +0800720sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
721 struct esdhc_platform_data *boarddata)
722{
723 struct device_node *np = pdev->dev.of_node;
724
725 if (!np)
726 return -ENODEV;
727
Arnd Bergmann7f217792012-05-13 00:14:24 -0400728 if (of_get_property(np, "non-removable", NULL))
Shawn Guoabfafc22011-06-30 15:44:44 +0800729 boarddata->cd_type = ESDHC_CD_PERMANENT;
730
731 if (of_get_property(np, "fsl,cd-controller", NULL))
732 boarddata->cd_type = ESDHC_CD_CONTROLLER;
733
734 if (of_get_property(np, "fsl,wp-controller", NULL))
735 boarddata->wp_type = ESDHC_WP_CONTROLLER;
736
737 boarddata->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
738 if (gpio_is_valid(boarddata->cd_gpio))
739 boarddata->cd_type = ESDHC_CD_GPIO;
740
741 boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
742 if (gpio_is_valid(boarddata->wp_gpio))
743 boarddata->wp_type = ESDHC_WP_GPIO;
744
Sascha Haueraf510792013-01-21 19:02:28 +0800745 of_property_read_u32(np, "bus-width", &boarddata->max_bus_width);
746
Lucas Stach0ddf03c2013-06-05 15:13:26 +0200747 of_property_read_u32(np, "max-frequency", &boarddata->f_max);
748
Shawn Guoabfafc22011-06-30 15:44:44 +0800749 return 0;
750}
751#else
752static inline int
753sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
754 struct esdhc_platform_data *boarddata)
755{
756 return -ENODEV;
757}
758#endif
759
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500760static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200761{
Shawn Guoabfafc22011-06-30 15:44:44 +0800762 const struct of_device_id *of_id =
763 of_match_device(imx_esdhc_dt_ids, &pdev->dev);
Shawn Guo85d65092011-05-27 23:48:12 +0800764 struct sdhci_pltfm_host *pltfm_host;
765 struct sdhci_host *host;
766 struct esdhc_platform_data *boarddata;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100767 int err;
Richard Zhue1498602011-03-25 09:18:27 -0400768 struct pltfm_imx_data *imx_data;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200769
Christian Daudt0e748232013-05-29 13:50:05 -0700770 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata, 0);
Shawn Guo85d65092011-05-27 23:48:12 +0800771 if (IS_ERR(host))
772 return PTR_ERR(host);
773
774 pltfm_host = sdhci_priv(host);
775
Shawn Guoe3af31c2012-11-26 14:39:43 +0800776 imx_data = devm_kzalloc(&pdev->dev, sizeof(*imx_data), GFP_KERNEL);
Shawn Guoabfafc22011-06-30 15:44:44 +0800777 if (!imx_data) {
778 err = -ENOMEM;
Shawn Guoe3af31c2012-11-26 14:39:43 +0800779 goto free_sdhci;
Shawn Guoabfafc22011-06-30 15:44:44 +0800780 }
Shawn Guo57ed3312011-06-30 09:24:26 +0800781
Shawn Guoabfafc22011-06-30 15:44:44 +0800782 if (of_id)
783 pdev->id_entry = of_id->data;
Shawn Guo57ed3312011-06-30 09:24:26 +0800784 imx_data->devtype = pdev->id_entry->driver_data;
Shawn Guo85d65092011-05-27 23:48:12 +0800785 pltfm_host->priv = imx_data;
786
Sascha Hauer52dac612012-03-07 09:31:34 +0100787 imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
788 if (IS_ERR(imx_data->clk_ipg)) {
789 err = PTR_ERR(imx_data->clk_ipg);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800790 goto free_sdhci;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200791 }
Sascha Hauer52dac612012-03-07 09:31:34 +0100792
793 imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
794 if (IS_ERR(imx_data->clk_ahb)) {
795 err = PTR_ERR(imx_data->clk_ahb);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800796 goto free_sdhci;
Sascha Hauer52dac612012-03-07 09:31:34 +0100797 }
798
799 imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
800 if (IS_ERR(imx_data->clk_per)) {
801 err = PTR_ERR(imx_data->clk_per);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800802 goto free_sdhci;
Sascha Hauer52dac612012-03-07 09:31:34 +0100803 }
804
805 pltfm_host->clk = imx_data->clk_per;
806
807 clk_prepare_enable(imx_data->clk_per);
808 clk_prepare_enable(imx_data->clk_ipg);
809 clk_prepare_enable(imx_data->clk_ahb);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200810
Dong Aishenge62d8b82012-05-11 14:56:01 +0800811 imx_data->pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
812 if (IS_ERR(imx_data->pinctrl)) {
813 err = PTR_ERR(imx_data->pinctrl);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800814 goto disable_clk;
Dong Aishenge62d8b82012-05-11 14:56:01 +0800815 }
816
Eric BĂ©nardb89152822012-04-18 02:30:20 +0200817 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
Eric BĂ©nard37865fe2010-10-23 01:57:21 +0200818
Shawn Guo57ed3312011-06-30 09:24:26 +0800819 if (is_imx25_esdhc(imx_data) || is_imx35_esdhc(imx_data))
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100820 /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
Richard Zhu97e4ba62011-08-11 16:51:46 -0400821 host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK
822 | SDHCI_QUIRK_BROKEN_ADMA;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100823
Shawn Guo57ed3312011-06-30 09:24:26 +0800824 if (is_imx53_esdhc(imx_data))
Richard Zhu58ac8172011-03-21 13:22:16 +0800825 imx_data->flags |= ESDHC_FLAG_MULTIBLK_NO_INT;
826
Shawn Guof750ba92011-11-10 16:39:32 +0800827 /*
828 * The imx6q ROM code will change the default watermark level setting
829 * to something insane. Change it back here.
830 */
831 if (is_imx6q_usdhc(imx_data))
Shawn Guo60bf6392013-01-15 23:36:53 +0800832 writel(0x08100810, host->ioaddr + ESDHC_WTMK_LVL);
Shawn Guof750ba92011-11-10 16:39:32 +0800833
Shawn Guo842afc02011-07-06 22:57:48 +0800834 boarddata = &imx_data->boarddata;
Shawn Guoabfafc22011-06-30 15:44:44 +0800835 if (sdhci_esdhc_imx_probe_dt(pdev, boarddata) < 0) {
836 if (!host->mmc->parent->platform_data) {
837 dev_err(mmc_dev(host->mmc), "no board data!\n");
838 err = -EINVAL;
Shawn Guoe3af31c2012-11-26 14:39:43 +0800839 goto disable_clk;
Shawn Guoabfafc22011-06-30 15:44:44 +0800840 }
841 imx_data->boarddata = *((struct esdhc_platform_data *)
842 host->mmc->parent->platform_data);
843 }
Shawn Guo913413c2011-06-21 22:41:51 +0800844
845 /* write_protect */
846 if (boarddata->wp_type == ESDHC_WP_GPIO) {
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800847 err = mmc_gpio_request_ro(host->mmc, boarddata->wp_gpio);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100848 if (err) {
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800849 dev_err(mmc_dev(host->mmc),
850 "failed to request write-protect gpio!\n");
851 goto disable_clk;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100852 }
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800853 host->mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
Shawn Guo913413c2011-06-21 22:41:51 +0800854 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100855
Shawn Guo913413c2011-06-21 22:41:51 +0800856 /* card_detect */
Shawn Guo913413c2011-06-21 22:41:51 +0800857 switch (boarddata->cd_type) {
858 case ESDHC_CD_GPIO:
Laurent Pinchart214fc302013-08-08 12:38:31 +0200859 err = mmc_gpio_request_cd(host->mmc, boarddata->cd_gpio, 0);
Wolfram Sang7e29c302011-02-26 14:44:41 +0100860 if (err) {
Shawn Guo913413c2011-06-21 22:41:51 +0800861 dev_err(mmc_dev(host->mmc),
Shawn Guofbe5fdd2012-12-11 22:32:20 +0800862 "failed to request card-detect gpio!\n");
Shawn Guoe3af31c2012-11-26 14:39:43 +0800863 goto disable_clk;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100864 }
Shawn Guo913413c2011-06-21 22:41:51 +0800865 /* fall through */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100866
Shawn Guo913413c2011-06-21 22:41:51 +0800867 case ESDHC_CD_CONTROLLER:
868 /* we have a working card_detect back */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100869 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
Shawn Guo913413c2011-06-21 22:41:51 +0800870 break;
871
872 case ESDHC_CD_PERMANENT:
873 host->mmc->caps = MMC_CAP_NONREMOVABLE;
874 break;
875
876 case ESDHC_CD_NONE:
877 break;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100878 }
Eric BĂ©nard16a790b2010-10-23 01:57:22 +0200879
Sascha Haueraf510792013-01-21 19:02:28 +0800880 switch (boarddata->max_bus_width) {
881 case 8:
882 host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA;
883 break;
884 case 4:
885 host->mmc->caps |= MMC_CAP_4_BIT_DATA;
886 break;
887 case 1:
888 default:
889 host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
890 break;
891 }
892
Shawn Guo85d65092011-05-27 23:48:12 +0800893 err = sdhci_add_host(host);
894 if (err)
Shawn Guoe3af31c2012-11-26 14:39:43 +0800895 goto disable_clk;
Shawn Guo85d65092011-05-27 23:48:12 +0800896
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200897 return 0;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100898
Shawn Guoe3af31c2012-11-26 14:39:43 +0800899disable_clk:
Sascha Hauer52dac612012-03-07 09:31:34 +0100900 clk_disable_unprepare(imx_data->clk_per);
901 clk_disable_unprepare(imx_data->clk_ipg);
902 clk_disable_unprepare(imx_data->clk_ahb);
Shawn Guoe3af31c2012-11-26 14:39:43 +0800903free_sdhci:
Shawn Guo85d65092011-05-27 23:48:12 +0800904 sdhci_pltfm_free(pdev);
905 return err;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200906}
907
Bill Pemberton6e0ee712012-11-19 13:26:03 -0500908static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200909{
Shawn Guo85d65092011-05-27 23:48:12 +0800910 struct sdhci_host *host = platform_get_drvdata(pdev);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200911 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400912 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Shawn Guo85d65092011-05-27 23:48:12 +0800913 int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
914
915 sdhci_remove_host(host, dead);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100916
Sascha Hauer52dac612012-03-07 09:31:34 +0100917 clk_disable_unprepare(imx_data->clk_per);
918 clk_disable_unprepare(imx_data->clk_ipg);
919 clk_disable_unprepare(imx_data->clk_ahb);
920
Shawn Guo85d65092011-05-27 23:48:12 +0800921 sdhci_pltfm_free(pdev);
922
923 return 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200924}
925
Shawn Guo85d65092011-05-27 23:48:12 +0800926static struct platform_driver sdhci_esdhc_imx_driver = {
927 .driver = {
928 .name = "sdhci-esdhc-imx",
929 .owner = THIS_MODULE,
Shawn Guoabfafc22011-06-30 15:44:44 +0800930 .of_match_table = imx_esdhc_dt_ids,
Manuel Lauss29495aa2011-11-03 11:09:45 +0100931 .pm = SDHCI_PLTFM_PMOPS,
Shawn Guo85d65092011-05-27 23:48:12 +0800932 },
Shawn Guo57ed3312011-06-30 09:24:26 +0800933 .id_table = imx_esdhc_devtype,
Shawn Guo85d65092011-05-27 23:48:12 +0800934 .probe = sdhci_esdhc_imx_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -0500935 .remove = sdhci_esdhc_imx_remove,
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200936};
Shawn Guo85d65092011-05-27 23:48:12 +0800937
Axel Lind1f81a62011-11-26 12:55:43 +0800938module_platform_driver(sdhci_esdhc_imx_driver);
Shawn Guo85d65092011-05-27 23:48:12 +0800939
940MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
941MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
942MODULE_LICENSE("GPL v2");