Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/mmc/host/mxcmmc.c - Freescale i.MX MMCI driver |
| 3 | * |
| 4 | * This is a driver for the SDHC controller found in Freescale MX2/MX3 |
| 5 | * SoCs. It is basically the same hardware as found on MX1 (imxmmc.c). |
| 6 | * Unlike the hardware found on MX1, this hardware just works and does |
Daniel Mack | 3ad2f3fb | 2010-02-03 08:01:28 +0800 | [diff] [blame] | 7 | * not need all the quirks found in imxmmc.c, hence the separate driver. |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 8 | * |
| 9 | * Copyright (C) 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de> |
| 10 | * Copyright (C) 2006 Pavel Pisa, PiKRON <ppisa@pikron.com> |
| 11 | * |
| 12 | * derived from pxamci.c by Russell King |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License version 2 as |
| 16 | * published by the Free Software Foundation. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/ioport.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/interrupt.h> |
| 25 | #include <linux/irq.h> |
| 26 | #include <linux/blkdev.h> |
| 27 | #include <linux/dma-mapping.h> |
| 28 | #include <linux/mmc/host.h> |
| 29 | #include <linux/mmc/card.h> |
| 30 | #include <linux/delay.h> |
| 31 | #include <linux/clk.h> |
| 32 | #include <linux/io.h> |
| 33 | #include <linux/gpio.h> |
Alberto Panizzo | 74b6695 | 2010-11-02 01:05:37 +0100 | [diff] [blame] | 34 | #include <linux/regulator/consumer.h> |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 35 | #include <linux/dmaengine.h> |
Viresh Kumar | 258aea7 | 2012-02-01 16:12:19 +0530 | [diff] [blame] | 36 | #include <linux/types.h> |
Markus Pargmann | 7ff747c | 2013-03-31 20:07:38 +0200 | [diff] [blame] | 37 | #include <linux/of.h> |
| 38 | #include <linux/of_device.h> |
| 39 | #include <linux/of_dma.h> |
| 40 | #include <linux/of_gpio.h> |
Anatolij Gustschin | bcf5352 | 2013-04-08 23:28:07 +0200 | [diff] [blame] | 41 | #include <linux/mmc/slot-gpio.h> |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 42 | |
| 43 | #include <asm/dma.h> |
| 44 | #include <asm/irq.h> |
Arnd Bergmann | 82906b1 | 2012-08-24 15:14:29 +0200 | [diff] [blame] | 45 | #include <linux/platform_data/mmc-mxcmmc.h> |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 46 | |
Arnd Bergmann | 82906b1 | 2012-08-24 15:14:29 +0200 | [diff] [blame] | 47 | #include <linux/platform_data/dma-imx.h> |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 48 | |
Sascha Hauer | 9563b1d | 2009-02-23 13:08:06 +0100 | [diff] [blame] | 49 | #define DRIVER_NAME "mxc-mmc" |
Javier Martin | f6ad0a4 | 2012-09-07 10:43:37 +0000 | [diff] [blame] | 50 | #define MXCMCI_TIMEOUT_MS 10000 |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 51 | |
| 52 | #define MMC_REG_STR_STP_CLK 0x00 |
| 53 | #define MMC_REG_STATUS 0x04 |
| 54 | #define MMC_REG_CLK_RATE 0x08 |
| 55 | #define MMC_REG_CMD_DAT_CONT 0x0C |
| 56 | #define MMC_REG_RES_TO 0x10 |
| 57 | #define MMC_REG_READ_TO 0x14 |
| 58 | #define MMC_REG_BLK_LEN 0x18 |
| 59 | #define MMC_REG_NOB 0x1C |
| 60 | #define MMC_REG_REV_NO 0x20 |
| 61 | #define MMC_REG_INT_CNTR 0x24 |
| 62 | #define MMC_REG_CMD 0x28 |
| 63 | #define MMC_REG_ARG 0x2C |
| 64 | #define MMC_REG_RES_FIFO 0x34 |
| 65 | #define MMC_REG_BUFFER_ACCESS 0x38 |
| 66 | |
| 67 | #define STR_STP_CLK_RESET (1 << 3) |
| 68 | #define STR_STP_CLK_START_CLK (1 << 1) |
| 69 | #define STR_STP_CLK_STOP_CLK (1 << 0) |
| 70 | |
| 71 | #define STATUS_CARD_INSERTION (1 << 31) |
| 72 | #define STATUS_CARD_REMOVAL (1 << 30) |
| 73 | #define STATUS_YBUF_EMPTY (1 << 29) |
| 74 | #define STATUS_XBUF_EMPTY (1 << 28) |
| 75 | #define STATUS_YBUF_FULL (1 << 27) |
| 76 | #define STATUS_XBUF_FULL (1 << 26) |
| 77 | #define STATUS_BUF_UND_RUN (1 << 25) |
| 78 | #define STATUS_BUF_OVFL (1 << 24) |
| 79 | #define STATUS_SDIO_INT_ACTIVE (1 << 14) |
| 80 | #define STATUS_END_CMD_RESP (1 << 13) |
| 81 | #define STATUS_WRITE_OP_DONE (1 << 12) |
| 82 | #define STATUS_DATA_TRANS_DONE (1 << 11) |
| 83 | #define STATUS_READ_OP_DONE (1 << 11) |
| 84 | #define STATUS_WR_CRC_ERROR_CODE_MASK (3 << 10) |
| 85 | #define STATUS_CARD_BUS_CLK_RUN (1 << 8) |
| 86 | #define STATUS_BUF_READ_RDY (1 << 7) |
| 87 | #define STATUS_BUF_WRITE_RDY (1 << 6) |
| 88 | #define STATUS_RESP_CRC_ERR (1 << 5) |
| 89 | #define STATUS_CRC_READ_ERR (1 << 3) |
| 90 | #define STATUS_CRC_WRITE_ERR (1 << 2) |
| 91 | #define STATUS_TIME_OUT_RESP (1 << 1) |
| 92 | #define STATUS_TIME_OUT_READ (1 << 0) |
| 93 | #define STATUS_ERR_MASK 0x2f |
| 94 | |
| 95 | #define CMD_DAT_CONT_CMD_RESP_LONG_OFF (1 << 12) |
| 96 | #define CMD_DAT_CONT_STOP_READWAIT (1 << 11) |
| 97 | #define CMD_DAT_CONT_START_READWAIT (1 << 10) |
| 98 | #define CMD_DAT_CONT_BUS_WIDTH_4 (2 << 8) |
| 99 | #define CMD_DAT_CONT_INIT (1 << 7) |
| 100 | #define CMD_DAT_CONT_WRITE (1 << 4) |
| 101 | #define CMD_DAT_CONT_DATA_ENABLE (1 << 3) |
| 102 | #define CMD_DAT_CONT_RESPONSE_48BIT_CRC (1 << 0) |
| 103 | #define CMD_DAT_CONT_RESPONSE_136BIT (2 << 0) |
| 104 | #define CMD_DAT_CONT_RESPONSE_48BIT (3 << 0) |
| 105 | |
| 106 | #define INT_SDIO_INT_WKP_EN (1 << 18) |
| 107 | #define INT_CARD_INSERTION_WKP_EN (1 << 17) |
| 108 | #define INT_CARD_REMOVAL_WKP_EN (1 << 16) |
| 109 | #define INT_CARD_INSERTION_EN (1 << 15) |
| 110 | #define INT_CARD_REMOVAL_EN (1 << 14) |
| 111 | #define INT_SDIO_IRQ_EN (1 << 13) |
| 112 | #define INT_DAT0_EN (1 << 12) |
| 113 | #define INT_BUF_READ_EN (1 << 4) |
| 114 | #define INT_BUF_WRITE_EN (1 << 3) |
| 115 | #define INT_END_CMD_RES_EN (1 << 2) |
| 116 | #define INT_WRITE_OP_DONE_EN (1 << 1) |
| 117 | #define INT_READ_OP_EN (1 << 0) |
| 118 | |
Shawn Guo | 7f917a8 | 2012-09-16 16:54:30 +0800 | [diff] [blame] | 119 | enum mxcmci_type { |
| 120 | IMX21_MMC, |
| 121 | IMX31_MMC, |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 122 | MPC512X_MMC, |
Shawn Guo | 7f917a8 | 2012-09-16 16:54:30 +0800 | [diff] [blame] | 123 | }; |
| 124 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 125 | struct mxcmci_host { |
| 126 | struct mmc_host *mmc; |
| 127 | struct resource *res; |
| 128 | void __iomem *base; |
| 129 | int irq; |
| 130 | int detect_irq; |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 131 | struct dma_chan *dma; |
| 132 | struct dma_async_tx_descriptor *desc; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 133 | int do_dma; |
Eric Bénard | 16b3bf8 | 2010-05-19 18:46:04 +0200 | [diff] [blame] | 134 | int default_irq_mask; |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 135 | int use_sdio; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 136 | unsigned int power_mode; |
| 137 | struct imxmmc_platform_data *pdata; |
| 138 | |
| 139 | struct mmc_request *req; |
| 140 | struct mmc_command *cmd; |
| 141 | struct mmc_data *data; |
| 142 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 143 | unsigned int datasize; |
| 144 | unsigned int dma_dir; |
| 145 | |
| 146 | u16 rev_no; |
| 147 | unsigned int cmdat; |
| 148 | |
Sascha Hauer | 529aa29 | 2012-03-07 09:31:14 +0100 | [diff] [blame] | 149 | struct clk *clk_ipg; |
| 150 | struct clk *clk_per; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 151 | |
| 152 | int clock; |
| 153 | |
| 154 | struct work_struct datawork; |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 155 | spinlock_t lock; |
Alberto Panizzo | 74b6695 | 2010-11-02 01:05:37 +0100 | [diff] [blame] | 156 | |
| 157 | struct regulator *vcc; |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 158 | |
| 159 | int burstlen; |
| 160 | int dmareq; |
| 161 | struct dma_slave_config dma_slave_config; |
| 162 | struct imx_dma_data dma_data; |
Javier Martin | f6ad0a4 | 2012-09-07 10:43:37 +0000 | [diff] [blame] | 163 | |
| 164 | struct timer_list watchdog; |
Shawn Guo | 7f917a8 | 2012-09-16 16:54:30 +0800 | [diff] [blame] | 165 | enum mxcmci_type devtype; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 166 | }; |
| 167 | |
Anatolij Gustschin | 4247705 | 2013-04-08 23:28:08 +0200 | [diff] [blame^] | 168 | static const struct platform_device_id mxcmci_devtype[] = { |
Shawn Guo | 7f917a8 | 2012-09-16 16:54:30 +0800 | [diff] [blame] | 169 | { |
| 170 | .name = "imx21-mmc", |
| 171 | .driver_data = IMX21_MMC, |
| 172 | }, { |
| 173 | .name = "imx31-mmc", |
| 174 | .driver_data = IMX31_MMC, |
| 175 | }, { |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 176 | .name = "mpc512x-sdhc", |
| 177 | .driver_data = MPC512X_MMC, |
| 178 | }, { |
Shawn Guo | 7f917a8 | 2012-09-16 16:54:30 +0800 | [diff] [blame] | 179 | /* sentinel */ |
| 180 | } |
| 181 | }; |
| 182 | MODULE_DEVICE_TABLE(platform, mxcmci_devtype); |
| 183 | |
Markus Pargmann | 7ff747c | 2013-03-31 20:07:38 +0200 | [diff] [blame] | 184 | static const struct of_device_id mxcmci_of_match[] = { |
| 185 | { |
| 186 | .compatible = "fsl,imx21-mmc", |
| 187 | .data = &mxcmci_devtype[IMX21_MMC], |
| 188 | }, { |
| 189 | .compatible = "fsl,imx31-mmc", |
| 190 | .data = &mxcmci_devtype[IMX31_MMC], |
| 191 | }, { |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 192 | .compatible = "fsl,mpc5121-sdhc", |
| 193 | .data = &mxcmci_devtype[MPC512X_MMC], |
| 194 | }, { |
Markus Pargmann | 7ff747c | 2013-03-31 20:07:38 +0200 | [diff] [blame] | 195 | /* sentinel */ |
| 196 | } |
| 197 | }; |
| 198 | MODULE_DEVICE_TABLE(of, mxcmci_of_match); |
| 199 | |
Shawn Guo | 7f917a8 | 2012-09-16 16:54:30 +0800 | [diff] [blame] | 200 | static inline int is_imx31_mmc(struct mxcmci_host *host) |
| 201 | { |
| 202 | return host->devtype == IMX31_MMC; |
| 203 | } |
| 204 | |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 205 | static inline int is_mpc512x_mmc(struct mxcmci_host *host) |
| 206 | { |
| 207 | return host->devtype == MPC512X_MMC; |
| 208 | } |
| 209 | |
| 210 | static inline u32 mxcmci_readl(struct mxcmci_host *host, int reg) |
| 211 | { |
| 212 | if (IS_ENABLED(CONFIG_PPC_MPC512x)) |
| 213 | return ioread32be(host->base + reg); |
| 214 | else |
| 215 | return readl(host->base + reg); |
| 216 | } |
| 217 | |
| 218 | static inline void mxcmci_writel(struct mxcmci_host *host, u32 val, int reg) |
| 219 | { |
| 220 | if (IS_ENABLED(CONFIG_PPC_MPC512x)) |
| 221 | iowrite32be(val, host->base + reg); |
| 222 | else |
| 223 | writel(val, host->base + reg); |
| 224 | } |
| 225 | |
| 226 | static inline u16 mxcmci_readw(struct mxcmci_host *host, int reg) |
| 227 | { |
| 228 | if (IS_ENABLED(CONFIG_PPC_MPC512x)) |
| 229 | return ioread32be(host->base + reg); |
| 230 | else |
| 231 | return readw(host->base + reg); |
| 232 | } |
| 233 | |
| 234 | static inline void mxcmci_writew(struct mxcmci_host *host, u16 val, int reg) |
| 235 | { |
| 236 | if (IS_ENABLED(CONFIG_PPC_MPC512x)) |
| 237 | iowrite32be(val, host->base + reg); |
| 238 | else |
| 239 | writew(val, host->base + reg); |
| 240 | } |
| 241 | |
Martin Fuzzey | 18489fa | 2009-04-16 22:00:36 +0200 | [diff] [blame] | 242 | static void mxcmci_set_clk_rate(struct mxcmci_host *host, unsigned int clk_ios); |
| 243 | |
Alberto Panizzo | 74b6695 | 2010-11-02 01:05:37 +0100 | [diff] [blame] | 244 | static inline void mxcmci_init_ocr(struct mxcmci_host *host) |
| 245 | { |
Alberto Panizzo | 74b6695 | 2010-11-02 01:05:37 +0100 | [diff] [blame] | 246 | host->vcc = regulator_get(mmc_dev(host->mmc), "vmmc"); |
| 247 | |
| 248 | if (IS_ERR(host->vcc)) { |
| 249 | host->vcc = NULL; |
| 250 | } else { |
| 251 | host->mmc->ocr_avail = mmc_regulator_get_ocrmask(host->vcc); |
| 252 | if (host->pdata && host->pdata->ocr_avail) |
| 253 | dev_warn(mmc_dev(host->mmc), |
| 254 | "pdata->ocr_avail will not be used\n"); |
| 255 | } |
Alberto Panizzo | d078d24 | 2010-11-09 11:35:37 +0100 | [diff] [blame] | 256 | |
Alberto Panizzo | 74b6695 | 2010-11-02 01:05:37 +0100 | [diff] [blame] | 257 | if (host->vcc == NULL) { |
| 258 | /* fall-back to platform data */ |
| 259 | if (host->pdata && host->pdata->ocr_avail) |
| 260 | host->mmc->ocr_avail = host->pdata->ocr_avail; |
| 261 | else |
| 262 | host->mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; |
| 263 | } |
| 264 | } |
| 265 | |
Alberto Panizzo | d078d24 | 2010-11-09 11:35:37 +0100 | [diff] [blame] | 266 | static inline void mxcmci_set_power(struct mxcmci_host *host, |
| 267 | unsigned char power_mode, |
| 268 | unsigned int vdd) |
Alberto Panizzo | 74b6695 | 2010-11-02 01:05:37 +0100 | [diff] [blame] | 269 | { |
Alberto Panizzo | d078d24 | 2010-11-09 11:35:37 +0100 | [diff] [blame] | 270 | if (host->vcc) { |
| 271 | if (power_mode == MMC_POWER_UP) |
| 272 | mmc_regulator_set_ocr(host->mmc, host->vcc, vdd); |
| 273 | else if (power_mode == MMC_POWER_OFF) |
| 274 | mmc_regulator_set_ocr(host->mmc, host->vcc, 0); |
| 275 | } |
| 276 | |
Alberto Panizzo | 74b6695 | 2010-11-02 01:05:37 +0100 | [diff] [blame] | 277 | if (host->pdata && host->pdata->setpower) |
| 278 | host->pdata->setpower(mmc_dev(host->mmc), vdd); |
| 279 | } |
| 280 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 281 | static inline int mxcmci_use_dma(struct mxcmci_host *host) |
| 282 | { |
| 283 | return host->do_dma; |
| 284 | } |
| 285 | |
| 286 | static void mxcmci_softreset(struct mxcmci_host *host) |
| 287 | { |
| 288 | int i; |
| 289 | |
Daniel Mack | 4725f6f | 2010-04-01 10:03:23 +0200 | [diff] [blame] | 290 | dev_dbg(mmc_dev(host->mmc), "mxcmci_softreset\n"); |
| 291 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 292 | /* reset sequence */ |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 293 | mxcmci_writew(host, STR_STP_CLK_RESET, MMC_REG_STR_STP_CLK); |
| 294 | mxcmci_writew(host, STR_STP_CLK_RESET | STR_STP_CLK_START_CLK, |
| 295 | MMC_REG_STR_STP_CLK); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 296 | |
| 297 | for (i = 0; i < 8; i++) |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 298 | mxcmci_writew(host, STR_STP_CLK_START_CLK, MMC_REG_STR_STP_CLK); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 299 | |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 300 | mxcmci_writew(host, 0xff, MMC_REG_RES_TO); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 301 | } |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 302 | static int mxcmci_setup_dma(struct mmc_host *mmc); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 303 | |
Martin Fuzzey | 656217d | 2009-04-16 22:00:41 +0200 | [diff] [blame] | 304 | static int mxcmci_setup_data(struct mxcmci_host *host, struct mmc_data *data) |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 305 | { |
| 306 | unsigned int nob = data->blocks; |
| 307 | unsigned int blksz = data->blksz; |
| 308 | unsigned int datasize = nob * blksz; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 309 | struct scatterlist *sg; |
Vinod Koul | 05f5799 | 2011-10-14 10:45:11 +0530 | [diff] [blame] | 310 | enum dma_transfer_direction slave_dirn; |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 311 | int i, nents; |
| 312 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 313 | if (data->flags & MMC_DATA_STREAM) |
| 314 | nob = 0xffff; |
| 315 | |
| 316 | host->data = data; |
| 317 | data->bytes_xfered = 0; |
| 318 | |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 319 | mxcmci_writew(host, nob, MMC_REG_NOB); |
| 320 | mxcmci_writew(host, blksz, MMC_REG_BLK_LEN); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 321 | host->datasize = datasize; |
| 322 | |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 323 | if (!mxcmci_use_dma(host)) |
| 324 | return 0; |
| 325 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 326 | for_each_sg(data->sg, sg, data->sg_len, i) { |
Javier Martin | 2cb5355 | 2012-10-30 16:54:07 +0100 | [diff] [blame] | 327 | if (sg->offset & 3 || sg->length & 3 || sg->length < 512) { |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 328 | host->do_dma = 0; |
Martin Fuzzey | 656217d | 2009-04-16 22:00:41 +0200 | [diff] [blame] | 329 | return 0; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
Vinod Koul | 05f5799 | 2011-10-14 10:45:11 +0530 | [diff] [blame] | 333 | if (data->flags & MMC_DATA_READ) { |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 334 | host->dma_dir = DMA_FROM_DEVICE; |
Vinod Koul | 05f5799 | 2011-10-14 10:45:11 +0530 | [diff] [blame] | 335 | slave_dirn = DMA_DEV_TO_MEM; |
| 336 | } else { |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 337 | host->dma_dir = DMA_TO_DEVICE; |
Vinod Koul | 05f5799 | 2011-10-14 10:45:11 +0530 | [diff] [blame] | 338 | slave_dirn = DMA_MEM_TO_DEV; |
| 339 | } |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 340 | |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 341 | nents = dma_map_sg(host->dma->device->dev, data->sg, |
| 342 | data->sg_len, host->dma_dir); |
| 343 | if (nents != data->sg_len) |
| 344 | return -EINVAL; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 345 | |
Alexandre Bounine | 1605282 | 2012-03-08 16:11:18 -0500 | [diff] [blame] | 346 | host->desc = dmaengine_prep_slave_sg(host->dma, |
Vinod Koul | 05f5799 | 2011-10-14 10:45:11 +0530 | [diff] [blame] | 347 | data->sg, data->sg_len, slave_dirn, |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 348 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 349 | |
| 350 | if (!host->desc) { |
| 351 | dma_unmap_sg(host->dma->device->dev, data->sg, data->sg_len, |
| 352 | host->dma_dir); |
| 353 | host->do_dma = 0; |
| 354 | return 0; /* Fall back to PIO */ |
Martin Fuzzey | 656217d | 2009-04-16 22:00:41 +0200 | [diff] [blame] | 355 | } |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 356 | wmb(); |
| 357 | |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 358 | dmaengine_submit(host->desc); |
Sascha Hauer | 439aa0e | 2012-01-09 10:32:47 +0100 | [diff] [blame] | 359 | dma_async_issue_pending(host->dma); |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 360 | |
Javier Martin | f6ad0a4 | 2012-09-07 10:43:37 +0000 | [diff] [blame] | 361 | mod_timer(&host->watchdog, jiffies + msecs_to_jiffies(MXCMCI_TIMEOUT_MS)); |
| 362 | |
Martin Fuzzey | 656217d | 2009-04-16 22:00:41 +0200 | [diff] [blame] | 363 | return 0; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 364 | } |
| 365 | |
Javier Martin | f6ad0a4 | 2012-09-07 10:43:37 +0000 | [diff] [blame] | 366 | static void mxcmci_cmd_done(struct mxcmci_host *host, unsigned int stat); |
| 367 | static void mxcmci_data_done(struct mxcmci_host *host, unsigned int stat); |
| 368 | |
| 369 | static void mxcmci_dma_callback(void *data) |
| 370 | { |
| 371 | struct mxcmci_host *host = data; |
| 372 | u32 stat; |
| 373 | |
| 374 | del_timer(&host->watchdog); |
| 375 | |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 376 | stat = mxcmci_readl(host, MMC_REG_STATUS); |
| 377 | mxcmci_writel(host, stat & ~STATUS_DATA_TRANS_DONE, MMC_REG_STATUS); |
Javier Martin | f6ad0a4 | 2012-09-07 10:43:37 +0000 | [diff] [blame] | 378 | |
| 379 | dev_dbg(mmc_dev(host->mmc), "%s: 0x%08x\n", __func__, stat); |
| 380 | |
| 381 | if (stat & STATUS_READ_OP_DONE) |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 382 | mxcmci_writel(host, STATUS_READ_OP_DONE, MMC_REG_STATUS); |
Javier Martin | f6ad0a4 | 2012-09-07 10:43:37 +0000 | [diff] [blame] | 383 | |
| 384 | mxcmci_data_done(host, stat); |
| 385 | } |
| 386 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 387 | static int mxcmci_start_cmd(struct mxcmci_host *host, struct mmc_command *cmd, |
| 388 | unsigned int cmdat) |
| 389 | { |
Eric Bénard | 16b3bf8 | 2010-05-19 18:46:04 +0200 | [diff] [blame] | 390 | u32 int_cntr = host->default_irq_mask; |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 391 | unsigned long flags; |
| 392 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 393 | WARN_ON(host->cmd != NULL); |
| 394 | host->cmd = cmd; |
| 395 | |
| 396 | switch (mmc_resp_type(cmd)) { |
| 397 | case MMC_RSP_R1: /* short CRC, OPCODE */ |
| 398 | case MMC_RSP_R1B:/* short CRC, OPCODE, BUSY */ |
| 399 | cmdat |= CMD_DAT_CONT_RESPONSE_48BIT_CRC; |
| 400 | break; |
| 401 | case MMC_RSP_R2: /* long 136 bit + CRC */ |
| 402 | cmdat |= CMD_DAT_CONT_RESPONSE_136BIT; |
| 403 | break; |
| 404 | case MMC_RSP_R3: /* short */ |
| 405 | cmdat |= CMD_DAT_CONT_RESPONSE_48BIT; |
| 406 | break; |
| 407 | case MMC_RSP_NONE: |
| 408 | break; |
| 409 | default: |
| 410 | dev_err(mmc_dev(host->mmc), "unhandled response type 0x%x\n", |
| 411 | mmc_resp_type(cmd)); |
| 412 | cmd->error = -EINVAL; |
| 413 | return -EINVAL; |
| 414 | } |
| 415 | |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 416 | int_cntr = INT_END_CMD_RES_EN; |
| 417 | |
Javier Martin | f6ad0a4 | 2012-09-07 10:43:37 +0000 | [diff] [blame] | 418 | if (mxcmci_use_dma(host)) { |
| 419 | if (host->dma_dir == DMA_FROM_DEVICE) { |
| 420 | host->desc->callback = mxcmci_dma_callback; |
| 421 | host->desc->callback_param = host; |
| 422 | } else { |
| 423 | int_cntr |= INT_WRITE_OP_DONE_EN; |
| 424 | } |
| 425 | } |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 426 | |
| 427 | spin_lock_irqsave(&host->lock, flags); |
| 428 | if (host->use_sdio) |
| 429 | int_cntr |= INT_SDIO_IRQ_EN; |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 430 | mxcmci_writel(host, int_cntr, MMC_REG_INT_CNTR); |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 431 | spin_unlock_irqrestore(&host->lock, flags); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 432 | |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 433 | mxcmci_writew(host, cmd->opcode, MMC_REG_CMD); |
| 434 | mxcmci_writel(host, cmd->arg, MMC_REG_ARG); |
| 435 | mxcmci_writew(host, cmdat, MMC_REG_CMD_DAT_CONT); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | static void mxcmci_finish_request(struct mxcmci_host *host, |
| 441 | struct mmc_request *req) |
| 442 | { |
Eric Bénard | 16b3bf8 | 2010-05-19 18:46:04 +0200 | [diff] [blame] | 443 | u32 int_cntr = host->default_irq_mask; |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 444 | unsigned long flags; |
| 445 | |
| 446 | spin_lock_irqsave(&host->lock, flags); |
| 447 | if (host->use_sdio) |
| 448 | int_cntr |= INT_SDIO_IRQ_EN; |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 449 | mxcmci_writel(host, int_cntr, MMC_REG_INT_CNTR); |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 450 | spin_unlock_irqrestore(&host->lock, flags); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 451 | |
| 452 | host->req = NULL; |
| 453 | host->cmd = NULL; |
| 454 | host->data = NULL; |
| 455 | |
| 456 | mmc_request_done(host->mmc, req); |
| 457 | } |
| 458 | |
| 459 | static int mxcmci_finish_data(struct mxcmci_host *host, unsigned int stat) |
| 460 | { |
| 461 | struct mmc_data *data = host->data; |
| 462 | int data_error; |
| 463 | |
Javier Martin | f6ad0a4 | 2012-09-07 10:43:37 +0000 | [diff] [blame] | 464 | if (mxcmci_use_dma(host)) |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 465 | dma_unmap_sg(host->dma->device->dev, data->sg, data->sg_len, |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 466 | host->dma_dir); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 467 | |
| 468 | if (stat & STATUS_ERR_MASK) { |
| 469 | dev_dbg(mmc_dev(host->mmc), "request failed. status: 0x%08x\n", |
| 470 | stat); |
| 471 | if (stat & STATUS_CRC_READ_ERR) { |
Daniel Mack | 4725f6f | 2010-04-01 10:03:23 +0200 | [diff] [blame] | 472 | dev_err(mmc_dev(host->mmc), "%s: -EILSEQ\n", __func__); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 473 | data->error = -EILSEQ; |
| 474 | } else if (stat & STATUS_CRC_WRITE_ERR) { |
| 475 | u32 err_code = (stat >> 9) & 0x3; |
Daniel Mack | 4725f6f | 2010-04-01 10:03:23 +0200 | [diff] [blame] | 476 | if (err_code == 2) { /* No CRC response */ |
| 477 | dev_err(mmc_dev(host->mmc), |
| 478 | "%s: No CRC -ETIMEDOUT\n", __func__); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 479 | data->error = -ETIMEDOUT; |
Daniel Mack | 4725f6f | 2010-04-01 10:03:23 +0200 | [diff] [blame] | 480 | } else { |
| 481 | dev_err(mmc_dev(host->mmc), |
| 482 | "%s: -EILSEQ\n", __func__); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 483 | data->error = -EILSEQ; |
Daniel Mack | 4725f6f | 2010-04-01 10:03:23 +0200 | [diff] [blame] | 484 | } |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 485 | } else if (stat & STATUS_TIME_OUT_READ) { |
Daniel Mack | 4725f6f | 2010-04-01 10:03:23 +0200 | [diff] [blame] | 486 | dev_err(mmc_dev(host->mmc), |
| 487 | "%s: read -ETIMEDOUT\n", __func__); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 488 | data->error = -ETIMEDOUT; |
| 489 | } else { |
Daniel Mack | 4725f6f | 2010-04-01 10:03:23 +0200 | [diff] [blame] | 490 | dev_err(mmc_dev(host->mmc), "%s: -EIO\n", __func__); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 491 | data->error = -EIO; |
| 492 | } |
| 493 | } else { |
| 494 | data->bytes_xfered = host->datasize; |
| 495 | } |
| 496 | |
| 497 | data_error = data->error; |
| 498 | |
| 499 | host->data = NULL; |
| 500 | |
| 501 | return data_error; |
| 502 | } |
| 503 | |
| 504 | static void mxcmci_read_response(struct mxcmci_host *host, unsigned int stat) |
| 505 | { |
| 506 | struct mmc_command *cmd = host->cmd; |
| 507 | int i; |
| 508 | u32 a, b, c; |
| 509 | |
| 510 | if (!cmd) |
| 511 | return; |
| 512 | |
| 513 | if (stat & STATUS_TIME_OUT_RESP) { |
| 514 | dev_dbg(mmc_dev(host->mmc), "CMD TIMEOUT\n"); |
| 515 | cmd->error = -ETIMEDOUT; |
| 516 | } else if (stat & STATUS_RESP_CRC_ERR && cmd->flags & MMC_RSP_CRC) { |
| 517 | dev_dbg(mmc_dev(host->mmc), "cmd crc error\n"); |
| 518 | cmd->error = -EILSEQ; |
| 519 | } |
| 520 | |
| 521 | if (cmd->flags & MMC_RSP_PRESENT) { |
| 522 | if (cmd->flags & MMC_RSP_136) { |
| 523 | for (i = 0; i < 4; i++) { |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 524 | a = mxcmci_readw(host, MMC_REG_RES_FIFO); |
| 525 | b = mxcmci_readw(host, MMC_REG_RES_FIFO); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 526 | cmd->resp[i] = a << 16 | b; |
| 527 | } |
| 528 | } else { |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 529 | a = mxcmci_readw(host, MMC_REG_RES_FIFO); |
| 530 | b = mxcmci_readw(host, MMC_REG_RES_FIFO); |
| 531 | c = mxcmci_readw(host, MMC_REG_RES_FIFO); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 532 | cmd->resp[0] = a << 24 | b << 8 | c >> 8; |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | static int mxcmci_poll_status(struct mxcmci_host *host, u32 mask) |
| 538 | { |
| 539 | u32 stat; |
| 540 | unsigned long timeout = jiffies + HZ; |
| 541 | |
| 542 | do { |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 543 | stat = mxcmci_readl(host, MMC_REG_STATUS); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 544 | if (stat & STATUS_ERR_MASK) |
| 545 | return stat; |
Martin Fuzzey | 18489fa | 2009-04-16 22:00:36 +0200 | [diff] [blame] | 546 | if (time_after(jiffies, timeout)) { |
| 547 | mxcmci_softreset(host); |
| 548 | mxcmci_set_clk_rate(host, host->clock); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 549 | return STATUS_TIME_OUT_READ; |
Martin Fuzzey | 18489fa | 2009-04-16 22:00:36 +0200 | [diff] [blame] | 550 | } |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 551 | if (stat & mask) |
| 552 | return 0; |
| 553 | cpu_relax(); |
| 554 | } while (1); |
| 555 | } |
| 556 | |
| 557 | static int mxcmci_pull(struct mxcmci_host *host, void *_buf, int bytes) |
| 558 | { |
| 559 | unsigned int stat; |
| 560 | u32 *buf = _buf; |
| 561 | |
| 562 | while (bytes > 3) { |
| 563 | stat = mxcmci_poll_status(host, |
| 564 | STATUS_BUF_READ_RDY | STATUS_READ_OP_DONE); |
| 565 | if (stat) |
| 566 | return stat; |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 567 | *buf++ = cpu_to_le32(mxcmci_readl(host, MMC_REG_BUFFER_ACCESS)); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 568 | bytes -= 4; |
| 569 | } |
| 570 | |
| 571 | if (bytes) { |
| 572 | u8 *b = (u8 *)buf; |
| 573 | u32 tmp; |
| 574 | |
| 575 | stat = mxcmci_poll_status(host, |
| 576 | STATUS_BUF_READ_RDY | STATUS_READ_OP_DONE); |
| 577 | if (stat) |
| 578 | return stat; |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 579 | tmp = cpu_to_le32(mxcmci_readl(host, MMC_REG_BUFFER_ACCESS)); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 580 | memcpy(b, &tmp, bytes); |
| 581 | } |
| 582 | |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | static int mxcmci_push(struct mxcmci_host *host, void *_buf, int bytes) |
| 587 | { |
| 588 | unsigned int stat; |
| 589 | u32 *buf = _buf; |
| 590 | |
| 591 | while (bytes > 3) { |
| 592 | stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY); |
| 593 | if (stat) |
| 594 | return stat; |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 595 | mxcmci_writel(host, cpu_to_le32(*buf++), MMC_REG_BUFFER_ACCESS); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 596 | bytes -= 4; |
| 597 | } |
| 598 | |
| 599 | if (bytes) { |
| 600 | u8 *b = (u8 *)buf; |
| 601 | u32 tmp; |
| 602 | |
| 603 | stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY); |
| 604 | if (stat) |
| 605 | return stat; |
| 606 | |
| 607 | memcpy(&tmp, b, bytes); |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 608 | mxcmci_writel(host, cpu_to_le32(tmp), MMC_REG_BUFFER_ACCESS); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY); |
| 612 | if (stat) |
| 613 | return stat; |
| 614 | |
| 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | static int mxcmci_transfer_data(struct mxcmci_host *host) |
| 619 | { |
| 620 | struct mmc_data *data = host->req->data; |
| 621 | struct scatterlist *sg; |
| 622 | int stat, i; |
| 623 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 624 | host->data = data; |
| 625 | host->datasize = 0; |
| 626 | |
| 627 | if (data->flags & MMC_DATA_READ) { |
| 628 | for_each_sg(data->sg, sg, data->sg_len, i) { |
| 629 | stat = mxcmci_pull(host, sg_virt(sg), sg->length); |
| 630 | if (stat) |
| 631 | return stat; |
| 632 | host->datasize += sg->length; |
| 633 | } |
| 634 | } else { |
| 635 | for_each_sg(data->sg, sg, data->sg_len, i) { |
| 636 | stat = mxcmci_push(host, sg_virt(sg), sg->length); |
| 637 | if (stat) |
| 638 | return stat; |
| 639 | host->datasize += sg->length; |
| 640 | } |
| 641 | stat = mxcmci_poll_status(host, STATUS_WRITE_OP_DONE); |
| 642 | if (stat) |
| 643 | return stat; |
| 644 | } |
| 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | static void mxcmci_datawork(struct work_struct *work) |
| 649 | { |
| 650 | struct mxcmci_host *host = container_of(work, struct mxcmci_host, |
| 651 | datawork); |
| 652 | int datastat = mxcmci_transfer_data(host); |
Daniel Mack | 4a31f2e | 2010-04-27 12:24:42 +0200 | [diff] [blame] | 653 | |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 654 | mxcmci_writel(host, STATUS_READ_OP_DONE | STATUS_WRITE_OP_DONE, |
| 655 | MMC_REG_STATUS); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 656 | mxcmci_finish_data(host, datastat); |
| 657 | |
| 658 | if (host->req->stop) { |
| 659 | if (mxcmci_start_cmd(host, host->req->stop, 0)) { |
| 660 | mxcmci_finish_request(host, host->req); |
| 661 | return; |
| 662 | } |
| 663 | } else { |
| 664 | mxcmci_finish_request(host, host->req); |
| 665 | } |
| 666 | } |
| 667 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 668 | static void mxcmci_data_done(struct mxcmci_host *host, unsigned int stat) |
| 669 | { |
Anatolij Gustschin | 70aa610 | 2013-04-08 23:28:05 +0200 | [diff] [blame] | 670 | struct mmc_request *req; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 671 | int data_error; |
Anatolij Gustschin | 70aa610 | 2013-04-08 23:28:05 +0200 | [diff] [blame] | 672 | unsigned long flags; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 673 | |
Anatolij Gustschin | 70aa610 | 2013-04-08 23:28:05 +0200 | [diff] [blame] | 674 | spin_lock_irqsave(&host->lock, flags); |
| 675 | |
| 676 | if (!host->data) { |
| 677 | spin_unlock_irqrestore(&host->lock, flags); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 678 | return; |
Anatolij Gustschin | 70aa610 | 2013-04-08 23:28:05 +0200 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | if (!host->req) { |
| 682 | spin_unlock_irqrestore(&host->lock, flags); |
| 683 | return; |
| 684 | } |
| 685 | |
| 686 | req = host->req; |
| 687 | if (!req->stop) |
| 688 | host->req = NULL; /* we will handle finish req below */ |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 689 | |
| 690 | data_error = mxcmci_finish_data(host, stat); |
| 691 | |
Anatolij Gustschin | 70aa610 | 2013-04-08 23:28:05 +0200 | [diff] [blame] | 692 | spin_unlock_irqrestore(&host->lock, flags); |
| 693 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 694 | mxcmci_read_response(host, stat); |
| 695 | host->cmd = NULL; |
| 696 | |
Anatolij Gustschin | 70aa610 | 2013-04-08 23:28:05 +0200 | [diff] [blame] | 697 | if (req->stop) { |
| 698 | if (mxcmci_start_cmd(host, req->stop, 0)) { |
| 699 | mxcmci_finish_request(host, req); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 700 | return; |
| 701 | } |
| 702 | } else { |
Anatolij Gustschin | 70aa610 | 2013-04-08 23:28:05 +0200 | [diff] [blame] | 703 | mxcmci_finish_request(host, req); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 704 | } |
| 705 | } |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 706 | |
| 707 | static void mxcmci_cmd_done(struct mxcmci_host *host, unsigned int stat) |
| 708 | { |
| 709 | mxcmci_read_response(host, stat); |
| 710 | host->cmd = NULL; |
| 711 | |
| 712 | if (!host->data && host->req) { |
| 713 | mxcmci_finish_request(host, host->req); |
| 714 | return; |
| 715 | } |
| 716 | |
| 717 | /* For the DMA case the DMA engine handles the data transfer |
Anand Gadiyar | fd589a8 | 2009-07-16 17:13:03 +0200 | [diff] [blame] | 718 | * automatically. For non DMA we have to do it ourselves. |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 719 | * Don't do it in interrupt context though. |
| 720 | */ |
| 721 | if (!mxcmci_use_dma(host) && host->data) |
| 722 | schedule_work(&host->datawork); |
| 723 | |
| 724 | } |
| 725 | |
| 726 | static irqreturn_t mxcmci_irq(int irq, void *devid) |
| 727 | { |
| 728 | struct mxcmci_host *host = devid; |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 729 | unsigned long flags; |
| 730 | bool sdio_irq; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 731 | u32 stat; |
| 732 | |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 733 | stat = mxcmci_readl(host, MMC_REG_STATUS); |
| 734 | mxcmci_writel(host, |
| 735 | stat & ~(STATUS_SDIO_INT_ACTIVE | STATUS_DATA_TRANS_DONE | |
| 736 | STATUS_WRITE_OP_DONE), |
| 737 | MMC_REG_STATUS); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 738 | |
| 739 | dev_dbg(mmc_dev(host->mmc), "%s: 0x%08x\n", __func__, stat); |
| 740 | |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 741 | spin_lock_irqsave(&host->lock, flags); |
| 742 | sdio_irq = (stat & STATUS_SDIO_INT_ACTIVE) && host->use_sdio; |
| 743 | spin_unlock_irqrestore(&host->lock, flags); |
| 744 | |
Daniel Mack | 4a31f2e | 2010-04-27 12:24:42 +0200 | [diff] [blame] | 745 | if (mxcmci_use_dma(host) && |
| 746 | (stat & (STATUS_READ_OP_DONE | STATUS_WRITE_OP_DONE))) |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 747 | mxcmci_writel(host, STATUS_READ_OP_DONE | STATUS_WRITE_OP_DONE, |
| 748 | MMC_REG_STATUS); |
Daniel Mack | 4a31f2e | 2010-04-27 12:24:42 +0200 | [diff] [blame] | 749 | |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 750 | if (sdio_irq) { |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 751 | mxcmci_writel(host, STATUS_SDIO_INT_ACTIVE, MMC_REG_STATUS); |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 752 | mmc_signal_sdio_irq(host->mmc); |
| 753 | } |
| 754 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 755 | if (stat & STATUS_END_CMD_RESP) |
| 756 | mxcmci_cmd_done(host, stat); |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 757 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 758 | if (mxcmci_use_dma(host) && |
Javier Martin | f6ad0a4 | 2012-09-07 10:43:37 +0000 | [diff] [blame] | 759 | (stat & (STATUS_DATA_TRANS_DONE | STATUS_WRITE_OP_DONE))) { |
| 760 | del_timer(&host->watchdog); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 761 | mxcmci_data_done(host, stat); |
Javier Martin | f6ad0a4 | 2012-09-07 10:43:37 +0000 | [diff] [blame] | 762 | } |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 763 | |
Eric Bénard | 16b3bf8 | 2010-05-19 18:46:04 +0200 | [diff] [blame] | 764 | if (host->default_irq_mask && |
| 765 | (stat & (STATUS_CARD_INSERTION | STATUS_CARD_REMOVAL))) |
| 766 | mmc_detect_change(host->mmc, msecs_to_jiffies(200)); |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 767 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 768 | return IRQ_HANDLED; |
| 769 | } |
| 770 | |
| 771 | static void mxcmci_request(struct mmc_host *mmc, struct mmc_request *req) |
| 772 | { |
| 773 | struct mxcmci_host *host = mmc_priv(mmc); |
| 774 | unsigned int cmdat = host->cmdat; |
Martin Fuzzey | 656217d | 2009-04-16 22:00:41 +0200 | [diff] [blame] | 775 | int error; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 776 | |
| 777 | WARN_ON(host->req != NULL); |
| 778 | |
| 779 | host->req = req; |
| 780 | host->cmdat &= ~CMD_DAT_CONT_INIT; |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 781 | |
| 782 | if (host->dma) |
| 783 | host->do_dma = 1; |
| 784 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 785 | if (req->data) { |
Martin Fuzzey | 656217d | 2009-04-16 22:00:41 +0200 | [diff] [blame] | 786 | error = mxcmci_setup_data(host, req->data); |
| 787 | if (error) { |
| 788 | req->cmd->error = error; |
| 789 | goto out; |
| 790 | } |
| 791 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 792 | |
| 793 | cmdat |= CMD_DAT_CONT_DATA_ENABLE; |
| 794 | |
| 795 | if (req->data->flags & MMC_DATA_WRITE) |
| 796 | cmdat |= CMD_DAT_CONT_WRITE; |
| 797 | } |
| 798 | |
Martin Fuzzey | 656217d | 2009-04-16 22:00:41 +0200 | [diff] [blame] | 799 | error = mxcmci_start_cmd(host, req->cmd, cmdat); |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 800 | |
Martin Fuzzey | 656217d | 2009-04-16 22:00:41 +0200 | [diff] [blame] | 801 | out: |
| 802 | if (error) |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 803 | mxcmci_finish_request(host, req); |
| 804 | } |
| 805 | |
| 806 | static void mxcmci_set_clk_rate(struct mxcmci_host *host, unsigned int clk_ios) |
| 807 | { |
| 808 | unsigned int divider; |
| 809 | int prescaler = 0; |
Sascha Hauer | 529aa29 | 2012-03-07 09:31:14 +0100 | [diff] [blame] | 810 | unsigned int clk_in = clk_get_rate(host->clk_per); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 811 | |
| 812 | while (prescaler <= 0x800) { |
| 813 | for (divider = 1; divider <= 0xF; divider++) { |
| 814 | int x; |
| 815 | |
| 816 | x = (clk_in / (divider + 1)); |
| 817 | |
| 818 | if (prescaler) |
| 819 | x /= (prescaler * 2); |
| 820 | |
| 821 | if (x <= clk_ios) |
| 822 | break; |
| 823 | } |
| 824 | if (divider < 0x10) |
| 825 | break; |
| 826 | |
| 827 | if (prescaler == 0) |
| 828 | prescaler = 1; |
| 829 | else |
| 830 | prescaler <<= 1; |
| 831 | } |
| 832 | |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 833 | mxcmci_writew(host, (prescaler << 4) | divider, MMC_REG_CLK_RATE); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 834 | |
| 835 | dev_dbg(mmc_dev(host->mmc), "scaler: %d divider: %d in: %d out: %d\n", |
| 836 | prescaler, divider, clk_in, clk_ios); |
| 837 | } |
| 838 | |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 839 | static int mxcmci_setup_dma(struct mmc_host *mmc) |
| 840 | { |
| 841 | struct mxcmci_host *host = mmc_priv(mmc); |
| 842 | struct dma_slave_config *config = &host->dma_slave_config; |
| 843 | |
| 844 | config->dst_addr = host->res->start + MMC_REG_BUFFER_ACCESS; |
| 845 | config->src_addr = host->res->start + MMC_REG_BUFFER_ACCESS; |
| 846 | config->dst_addr_width = 4; |
| 847 | config->src_addr_width = 4; |
| 848 | config->dst_maxburst = host->burstlen; |
| 849 | config->src_maxburst = host->burstlen; |
Viresh Kumar | 258aea7 | 2012-02-01 16:12:19 +0530 | [diff] [blame] | 850 | config->device_fc = false; |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 851 | |
| 852 | return dmaengine_slave_config(host->dma, config); |
| 853 | } |
| 854 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 855 | static void mxcmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 856 | { |
| 857 | struct mxcmci_host *host = mmc_priv(mmc); |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 858 | int burstlen, ret; |
| 859 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 860 | /* |
Sascha Hauer | 6584cb8 | 2011-07-06 11:18:33 +0200 | [diff] [blame] | 861 | * use burstlen of 64 (16 words) in 4 bit mode (--> reg value 0) |
| 862 | * use burstlen of 16 (4 words) in 1 bit mode (--> reg value 16) |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 863 | */ |
| 864 | if (ios->bus_width == MMC_BUS_WIDTH_4) |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 865 | burstlen = 16; |
Sascha Hauer | 6584cb8 | 2011-07-06 11:18:33 +0200 | [diff] [blame] | 866 | else |
| 867 | burstlen = 4; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 868 | |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 869 | if (mxcmci_use_dma(host) && burstlen != host->burstlen) { |
| 870 | host->burstlen = burstlen; |
| 871 | ret = mxcmci_setup_dma(mmc); |
| 872 | if (ret) { |
| 873 | dev_err(mmc_dev(host->mmc), |
| 874 | "failed to config DMA channel. Falling back to PIO\n"); |
| 875 | dma_release_channel(host->dma); |
| 876 | host->do_dma = 0; |
Sascha Hauer | e58f516 | 2011-11-11 16:28:05 +0100 | [diff] [blame] | 877 | host->dma = NULL; |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 878 | } |
| 879 | } |
| 880 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 881 | if (ios->bus_width == MMC_BUS_WIDTH_4) |
| 882 | host->cmdat |= CMD_DAT_CONT_BUS_WIDTH_4; |
| 883 | else |
| 884 | host->cmdat &= ~CMD_DAT_CONT_BUS_WIDTH_4; |
| 885 | |
| 886 | if (host->power_mode != ios->power_mode) { |
Alberto Panizzo | d078d24 | 2010-11-09 11:35:37 +0100 | [diff] [blame] | 887 | mxcmci_set_power(host, ios->power_mode, ios->vdd); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 888 | host->power_mode = ios->power_mode; |
Alberto Panizzo | 74b6695 | 2010-11-02 01:05:37 +0100 | [diff] [blame] | 889 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 890 | if (ios->power_mode == MMC_POWER_ON) |
| 891 | host->cmdat |= CMD_DAT_CONT_INIT; |
| 892 | } |
| 893 | |
| 894 | if (ios->clock) { |
| 895 | mxcmci_set_clk_rate(host, ios->clock); |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 896 | mxcmci_writew(host, STR_STP_CLK_START_CLK, MMC_REG_STR_STP_CLK); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 897 | } else { |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 898 | mxcmci_writew(host, STR_STP_CLK_STOP_CLK, MMC_REG_STR_STP_CLK); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | host->clock = ios->clock; |
| 902 | } |
| 903 | |
| 904 | static irqreturn_t mxcmci_detect_irq(int irq, void *data) |
| 905 | { |
| 906 | struct mmc_host *mmc = data; |
| 907 | |
| 908 | dev_dbg(mmc_dev(mmc), "%s\n", __func__); |
| 909 | |
| 910 | mmc_detect_change(mmc, msecs_to_jiffies(250)); |
| 911 | return IRQ_HANDLED; |
| 912 | } |
| 913 | |
| 914 | static int mxcmci_get_ro(struct mmc_host *mmc) |
| 915 | { |
| 916 | struct mxcmci_host *host = mmc_priv(mmc); |
| 917 | |
| 918 | if (host->pdata && host->pdata->get_ro) |
| 919 | return !!host->pdata->get_ro(mmc_dev(mmc)); |
| 920 | /* |
Anatolij Gustschin | bcf5352 | 2013-04-08 23:28:07 +0200 | [diff] [blame] | 921 | * If board doesn't support read only detection (no mmc_gpio |
| 922 | * context or gpio is invalid), then let the mmc core decide |
| 923 | * what to do. |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 924 | */ |
Anatolij Gustschin | bcf5352 | 2013-04-08 23:28:07 +0200 | [diff] [blame] | 925 | return mmc_gpio_get_ro(mmc); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 926 | } |
| 927 | |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 928 | static void mxcmci_enable_sdio_irq(struct mmc_host *mmc, int enable) |
| 929 | { |
| 930 | struct mxcmci_host *host = mmc_priv(mmc); |
| 931 | unsigned long flags; |
| 932 | u32 int_cntr; |
| 933 | |
| 934 | spin_lock_irqsave(&host->lock, flags); |
| 935 | host->use_sdio = enable; |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 936 | int_cntr = mxcmci_readl(host, MMC_REG_INT_CNTR); |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 937 | |
| 938 | if (enable) |
| 939 | int_cntr |= INT_SDIO_IRQ_EN; |
| 940 | else |
| 941 | int_cntr &= ~INT_SDIO_IRQ_EN; |
| 942 | |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 943 | mxcmci_writel(host, int_cntr, MMC_REG_INT_CNTR); |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 944 | spin_unlock_irqrestore(&host->lock, flags); |
| 945 | } |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 946 | |
Daniel Mack | 3fcb027 | 2010-04-01 10:03:25 +0200 | [diff] [blame] | 947 | static void mxcmci_init_card(struct mmc_host *host, struct mmc_card *card) |
| 948 | { |
Shawn Guo | 7f917a8 | 2012-09-16 16:54:30 +0800 | [diff] [blame] | 949 | struct mxcmci_host *mxcmci = mmc_priv(host); |
| 950 | |
Daniel Mack | 3fcb027 | 2010-04-01 10:03:25 +0200 | [diff] [blame] | 951 | /* |
| 952 | * MX3 SoCs have a silicon bug which corrupts CRC calculation of |
| 953 | * multi-block transfers when connected SDIO peripheral doesn't |
| 954 | * drive the BUSY line as required by the specs. |
| 955 | * One way to prevent this is to only allow 1-bit transfers. |
| 956 | */ |
| 957 | |
Shawn Guo | 7f917a8 | 2012-09-16 16:54:30 +0800 | [diff] [blame] | 958 | if (is_imx31_mmc(mxcmci) && card->type == MMC_TYPE_SDIO) |
Daniel Mack | 3fcb027 | 2010-04-01 10:03:25 +0200 | [diff] [blame] | 959 | host->caps &= ~MMC_CAP_4_BIT_DATA; |
| 960 | else |
| 961 | host->caps |= MMC_CAP_4_BIT_DATA; |
| 962 | } |
| 963 | |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 964 | static bool filter(struct dma_chan *chan, void *param) |
| 965 | { |
| 966 | struct mxcmci_host *host = param; |
| 967 | |
| 968 | if (!imx_dma_is_general_purpose(chan)) |
| 969 | return false; |
| 970 | |
| 971 | chan->private = &host->dma_data; |
| 972 | |
| 973 | return true; |
| 974 | } |
| 975 | |
Javier Martin | f6ad0a4 | 2012-09-07 10:43:37 +0000 | [diff] [blame] | 976 | static void mxcmci_watchdog(unsigned long data) |
| 977 | { |
| 978 | struct mmc_host *mmc = (struct mmc_host *)data; |
| 979 | struct mxcmci_host *host = mmc_priv(mmc); |
| 980 | struct mmc_request *req = host->req; |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 981 | unsigned int stat = mxcmci_readl(host, MMC_REG_STATUS); |
Javier Martin | f6ad0a4 | 2012-09-07 10:43:37 +0000 | [diff] [blame] | 982 | |
| 983 | if (host->dma_dir == DMA_FROM_DEVICE) { |
| 984 | dmaengine_terminate_all(host->dma); |
| 985 | dev_err(mmc_dev(host->mmc), |
| 986 | "%s: read time out (status = 0x%08x)\n", |
| 987 | __func__, stat); |
| 988 | } else { |
| 989 | dev_err(mmc_dev(host->mmc), |
| 990 | "%s: write time out (status = 0x%08x)\n", |
| 991 | __func__, stat); |
| 992 | mxcmci_softreset(host); |
| 993 | } |
| 994 | |
| 995 | /* Mark transfer as erroneus and inform the upper layers */ |
| 996 | |
Anatolij Gustschin | 70aa610 | 2013-04-08 23:28:05 +0200 | [diff] [blame] | 997 | if (host->data) |
| 998 | host->data->error = -ETIMEDOUT; |
Javier Martin | f6ad0a4 | 2012-09-07 10:43:37 +0000 | [diff] [blame] | 999 | host->req = NULL; |
| 1000 | host->cmd = NULL; |
| 1001 | host->data = NULL; |
| 1002 | mmc_request_done(host->mmc, req); |
| 1003 | } |
| 1004 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1005 | static const struct mmc_host_ops mxcmci_ops = { |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 1006 | .request = mxcmci_request, |
| 1007 | .set_ios = mxcmci_set_ios, |
| 1008 | .get_ro = mxcmci_get_ro, |
| 1009 | .enable_sdio_irq = mxcmci_enable_sdio_irq, |
Daniel Mack | 3fcb027 | 2010-04-01 10:03:25 +0200 | [diff] [blame] | 1010 | .init_card = mxcmci_init_card, |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1011 | }; |
| 1012 | |
| 1013 | static int mxcmci_probe(struct platform_device *pdev) |
| 1014 | { |
| 1015 | struct mmc_host *mmc; |
| 1016 | struct mxcmci_host *host = NULL; |
Uwe Kleine-König | c0521ba | 2009-12-14 18:01:17 -0800 | [diff] [blame] | 1017 | struct resource *iores, *r; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1018 | int ret = 0, irq; |
Markus Pargmann | 7ff747c | 2013-03-31 20:07:38 +0200 | [diff] [blame] | 1019 | bool dat3_card_detect = false; |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 1020 | dma_cap_mask_t mask; |
Markus Pargmann | 7ff747c | 2013-03-31 20:07:38 +0200 | [diff] [blame] | 1021 | const struct of_device_id *of_id; |
| 1022 | struct imxmmc_platform_data *pdata = pdev->dev.platform_data; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1023 | |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 1024 | pr_info("i.MX/MPC512x SDHC driver\n"); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1025 | |
Markus Pargmann | 7ff747c | 2013-03-31 20:07:38 +0200 | [diff] [blame] | 1026 | of_id = of_match_device(mxcmci_of_match, &pdev->dev); |
| 1027 | |
Uwe Kleine-König | c0521ba | 2009-12-14 18:01:17 -0800 | [diff] [blame] | 1028 | iores = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1029 | irq = platform_get_irq(pdev, 0); |
Uwe Kleine-König | c0521ba | 2009-12-14 18:01:17 -0800 | [diff] [blame] | 1030 | if (!iores || irq < 0) |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1031 | return -EINVAL; |
| 1032 | |
Uwe Kleine-König | c0521ba | 2009-12-14 18:01:17 -0800 | [diff] [blame] | 1033 | r = request_mem_region(iores->start, resource_size(iores), pdev->name); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1034 | if (!r) |
| 1035 | return -EBUSY; |
| 1036 | |
| 1037 | mmc = mmc_alloc_host(sizeof(struct mxcmci_host), &pdev->dev); |
| 1038 | if (!mmc) { |
| 1039 | ret = -ENOMEM; |
| 1040 | goto out_release_mem; |
| 1041 | } |
| 1042 | |
Markus Pargmann | 7ff747c | 2013-03-31 20:07:38 +0200 | [diff] [blame] | 1043 | mmc_of_parse(mmc); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1044 | mmc->ops = &mxcmci_ops; |
Markus Pargmann | 7ff747c | 2013-03-31 20:07:38 +0200 | [diff] [blame] | 1045 | |
| 1046 | /* For devicetree parsing, the bus width is read from devicetree */ |
| 1047 | if (pdata) |
| 1048 | mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ; |
| 1049 | else |
| 1050 | mmc->caps |= MMC_CAP_SDIO_IRQ; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1051 | |
| 1052 | /* MMC core transfer sizes tunable parameters */ |
Martin K. Petersen | a36274e | 2010-09-10 01:33:59 -0400 | [diff] [blame] | 1053 | mmc->max_segs = 64; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1054 | mmc->max_blk_size = 2048; |
| 1055 | mmc->max_blk_count = 65535; |
| 1056 | mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count; |
Vladimir Zapolskiy | d759c37 | 2010-03-10 15:23:57 -0800 | [diff] [blame] | 1057 | mmc->max_seg_size = mmc->max_req_size; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1058 | |
| 1059 | host = mmc_priv(mmc); |
| 1060 | host->base = ioremap(r->start, resource_size(r)); |
| 1061 | if (!host->base) { |
| 1062 | ret = -ENOMEM; |
| 1063 | goto out_free; |
| 1064 | } |
| 1065 | |
Markus Pargmann | 7ff747c | 2013-03-31 20:07:38 +0200 | [diff] [blame] | 1066 | if (of_id) { |
| 1067 | const struct platform_device_id *id_entry = of_id->data; |
| 1068 | host->devtype = id_entry->driver_data; |
| 1069 | } else { |
| 1070 | host->devtype = pdev->id_entry->driver_data; |
| 1071 | } |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1072 | host->mmc = mmc; |
Markus Pargmann | 7ff747c | 2013-03-31 20:07:38 +0200 | [diff] [blame] | 1073 | host->pdata = pdata; |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 1074 | spin_lock_init(&host->lock); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1075 | |
Markus Pargmann | 7ff747c | 2013-03-31 20:07:38 +0200 | [diff] [blame] | 1076 | if (pdata) |
| 1077 | dat3_card_detect = pdata->dat3_card_detect; |
| 1078 | else if (!(mmc->caps & MMC_CAP_NONREMOVABLE) |
| 1079 | && !of_property_read_bool(pdev->dev.of_node, "cd-gpios")) |
| 1080 | dat3_card_detect = true; |
| 1081 | |
Alberto Panizzo | 74b6695 | 2010-11-02 01:05:37 +0100 | [diff] [blame] | 1082 | mxcmci_init_ocr(host); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1083 | |
Markus Pargmann | 7ff747c | 2013-03-31 20:07:38 +0200 | [diff] [blame] | 1084 | if (dat3_card_detect) |
Eric Bénard | 16b3bf8 | 2010-05-19 18:46:04 +0200 | [diff] [blame] | 1085 | host->default_irq_mask = |
| 1086 | INT_CARD_INSERTION_EN | INT_CARD_REMOVAL_EN; |
| 1087 | else |
| 1088 | host->default_irq_mask = 0; |
| 1089 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1090 | host->res = r; |
| 1091 | host->irq = irq; |
| 1092 | |
Sascha Hauer | 529aa29 | 2012-03-07 09:31:14 +0100 | [diff] [blame] | 1093 | host->clk_ipg = devm_clk_get(&pdev->dev, "ipg"); |
| 1094 | if (IS_ERR(host->clk_ipg)) { |
| 1095 | ret = PTR_ERR(host->clk_ipg); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1096 | goto out_iounmap; |
| 1097 | } |
Sascha Hauer | 529aa29 | 2012-03-07 09:31:14 +0100 | [diff] [blame] | 1098 | |
| 1099 | host->clk_per = devm_clk_get(&pdev->dev, "per"); |
| 1100 | if (IS_ERR(host->clk_per)) { |
| 1101 | ret = PTR_ERR(host->clk_per); |
| 1102 | goto out_iounmap; |
| 1103 | } |
| 1104 | |
| 1105 | clk_prepare_enable(host->clk_per); |
| 1106 | clk_prepare_enable(host->clk_ipg); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1107 | |
| 1108 | mxcmci_softreset(host); |
| 1109 | |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 1110 | host->rev_no = mxcmci_readw(host, MMC_REG_REV_NO); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1111 | if (host->rev_no != 0x400) { |
| 1112 | ret = -ENODEV; |
| 1113 | dev_err(mmc_dev(host->mmc), "wrong rev.no. 0x%08x. aborting.\n", |
| 1114 | host->rev_no); |
| 1115 | goto out_clk_put; |
| 1116 | } |
| 1117 | |
Sascha Hauer | 529aa29 | 2012-03-07 09:31:14 +0100 | [diff] [blame] | 1118 | mmc->f_min = clk_get_rate(host->clk_per) >> 16; |
| 1119 | mmc->f_max = clk_get_rate(host->clk_per) >> 1; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1120 | |
| 1121 | /* recommended in data sheet */ |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 1122 | mxcmci_writew(host, 0x2db4, MMC_REG_READ_TO); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1123 | |
Anatolij Gustschin | c7ceab0 | 2013-04-08 23:28:06 +0200 | [diff] [blame] | 1124 | mxcmci_writel(host, host->default_irq_mask, MMC_REG_INT_CNTR); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1125 | |
Markus Pargmann | 7ff747c | 2013-03-31 20:07:38 +0200 | [diff] [blame] | 1126 | if (!host->pdata) { |
| 1127 | host->dma = dma_request_slave_channel(&pdev->dev, "rx-tx"); |
| 1128 | } else { |
| 1129 | r = platform_get_resource(pdev, IORESOURCE_DMA, 0); |
| 1130 | if (r) { |
| 1131 | host->dmareq = r->start; |
| 1132 | host->dma_data.peripheral_type = IMX_DMATYPE_SDHC; |
| 1133 | host->dma_data.priority = DMA_PRIO_LOW; |
| 1134 | host->dma_data.dma_request = host->dmareq; |
| 1135 | dma_cap_zero(mask); |
| 1136 | dma_cap_set(DMA_SLAVE, mask); |
| 1137 | host->dma = dma_request_channel(mask, filter, host); |
| 1138 | } |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1139 | } |
Markus Pargmann | 7ff747c | 2013-03-31 20:07:38 +0200 | [diff] [blame] | 1140 | if (host->dma) |
| 1141 | mmc->max_seg_size = dma_get_max_seg_size( |
| 1142 | host->dma->device->dev); |
| 1143 | else |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 1144 | dev_info(mmc_dev(host->mmc), "dma not available. Using PIO\n"); |
| 1145 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1146 | INIT_WORK(&host->datawork, mxcmci_datawork); |
| 1147 | |
| 1148 | ret = request_irq(host->irq, mxcmci_irq, 0, DRIVER_NAME, host); |
| 1149 | if (ret) |
| 1150 | goto out_free_dma; |
| 1151 | |
| 1152 | platform_set_drvdata(pdev, mmc); |
| 1153 | |
| 1154 | if (host->pdata && host->pdata->init) { |
| 1155 | ret = host->pdata->init(&pdev->dev, mxcmci_detect_irq, |
| 1156 | host->mmc); |
| 1157 | if (ret) |
| 1158 | goto out_free_irq; |
| 1159 | } |
| 1160 | |
Javier Martin | f6ad0a4 | 2012-09-07 10:43:37 +0000 | [diff] [blame] | 1161 | init_timer(&host->watchdog); |
| 1162 | host->watchdog.function = &mxcmci_watchdog; |
| 1163 | host->watchdog.data = (unsigned long)mmc; |
| 1164 | |
Alexander Shiyan | abd4190 | 2013-04-06 10:13:31 +0400 | [diff] [blame] | 1165 | mmc_add_host(mmc); |
| 1166 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1167 | return 0; |
| 1168 | |
| 1169 | out_free_irq: |
| 1170 | free_irq(host->irq, host); |
| 1171 | out_free_dma: |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 1172 | if (host->dma) |
| 1173 | dma_release_channel(host->dma); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1174 | out_clk_put: |
Sascha Hauer | 529aa29 | 2012-03-07 09:31:14 +0100 | [diff] [blame] | 1175 | clk_disable_unprepare(host->clk_per); |
| 1176 | clk_disable_unprepare(host->clk_ipg); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1177 | out_iounmap: |
| 1178 | iounmap(host->base); |
| 1179 | out_free: |
| 1180 | mmc_free_host(mmc); |
| 1181 | out_release_mem: |
Uwe Kleine-König | c0521ba | 2009-12-14 18:01:17 -0800 | [diff] [blame] | 1182 | release_mem_region(iores->start, resource_size(iores)); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1183 | return ret; |
| 1184 | } |
| 1185 | |
| 1186 | static int mxcmci_remove(struct platform_device *pdev) |
| 1187 | { |
| 1188 | struct mmc_host *mmc = platform_get_drvdata(pdev); |
| 1189 | struct mxcmci_host *host = mmc_priv(mmc); |
| 1190 | |
| 1191 | platform_set_drvdata(pdev, NULL); |
| 1192 | |
| 1193 | mmc_remove_host(mmc); |
| 1194 | |
Alberto Panizzo | 74b6695 | 2010-11-02 01:05:37 +0100 | [diff] [blame] | 1195 | if (host->vcc) |
| 1196 | regulator_put(host->vcc); |
| 1197 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1198 | if (host->pdata && host->pdata->exit) |
| 1199 | host->pdata->exit(&pdev->dev, mmc); |
| 1200 | |
| 1201 | free_irq(host->irq, host); |
| 1202 | iounmap(host->base); |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 1203 | |
| 1204 | if (host->dma) |
| 1205 | dma_release_channel(host->dma); |
| 1206 | |
Sascha Hauer | 529aa29 | 2012-03-07 09:31:14 +0100 | [diff] [blame] | 1207 | clk_disable_unprepare(host->clk_per); |
| 1208 | clk_disable_unprepare(host->clk_ipg); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1209 | |
| 1210 | release_mem_region(host->res->start, resource_size(host->res)); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1211 | |
| 1212 | mmc_free_host(mmc); |
| 1213 | |
| 1214 | return 0; |
| 1215 | } |
| 1216 | |
| 1217 | #ifdef CONFIG_PM |
Eric Bénard | a7d403c | 2010-06-17 20:59:07 +0200 | [diff] [blame] | 1218 | static int mxcmci_suspend(struct device *dev) |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1219 | { |
Eric Bénard | a7d403c | 2010-06-17 20:59:07 +0200 | [diff] [blame] | 1220 | struct mmc_host *mmc = dev_get_drvdata(dev); |
| 1221 | struct mxcmci_host *host = mmc_priv(mmc); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1222 | int ret = 0; |
| 1223 | |
| 1224 | if (mmc) |
Matt Fleming | 1a13f8f | 2010-05-26 14:42:08 -0700 | [diff] [blame] | 1225 | ret = mmc_suspend_host(mmc); |
Sascha Hauer | 529aa29 | 2012-03-07 09:31:14 +0100 | [diff] [blame] | 1226 | clk_disable_unprepare(host->clk_per); |
| 1227 | clk_disable_unprepare(host->clk_ipg); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1228 | |
| 1229 | return ret; |
| 1230 | } |
| 1231 | |
Eric Bénard | a7d403c | 2010-06-17 20:59:07 +0200 | [diff] [blame] | 1232 | static int mxcmci_resume(struct device *dev) |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1233 | { |
Eric Bénard | a7d403c | 2010-06-17 20:59:07 +0200 | [diff] [blame] | 1234 | struct mmc_host *mmc = dev_get_drvdata(dev); |
| 1235 | struct mxcmci_host *host = mmc_priv(mmc); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1236 | int ret = 0; |
| 1237 | |
Sascha Hauer | 529aa29 | 2012-03-07 09:31:14 +0100 | [diff] [blame] | 1238 | clk_prepare_enable(host->clk_per); |
| 1239 | clk_prepare_enable(host->clk_ipg); |
Eric Bénard | a7d403c | 2010-06-17 20:59:07 +0200 | [diff] [blame] | 1240 | if (mmc) |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1241 | ret = mmc_resume_host(mmc); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1242 | |
| 1243 | return ret; |
| 1244 | } |
Eric Bénard | a7d403c | 2010-06-17 20:59:07 +0200 | [diff] [blame] | 1245 | |
| 1246 | static const struct dev_pm_ops mxcmci_pm_ops = { |
| 1247 | .suspend = mxcmci_suspend, |
| 1248 | .resume = mxcmci_resume, |
| 1249 | }; |
| 1250 | #endif |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1251 | |
| 1252 | static struct platform_driver mxcmci_driver = { |
| 1253 | .probe = mxcmci_probe, |
| 1254 | .remove = mxcmci_remove, |
Shawn Guo | 7f917a8 | 2012-09-16 16:54:30 +0800 | [diff] [blame] | 1255 | .id_table = mxcmci_devtype, |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1256 | .driver = { |
| 1257 | .name = DRIVER_NAME, |
| 1258 | .owner = THIS_MODULE, |
Eric Bénard | a7d403c | 2010-06-17 20:59:07 +0200 | [diff] [blame] | 1259 | #ifdef CONFIG_PM |
| 1260 | .pm = &mxcmci_pm_ops, |
| 1261 | #endif |
Markus Pargmann | 7ff747c | 2013-03-31 20:07:38 +0200 | [diff] [blame] | 1262 | .of_match_table = mxcmci_of_match, |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1263 | } |
| 1264 | }; |
| 1265 | |
Axel Lin | d1f81a6 | 2011-11-26 12:55:43 +0800 | [diff] [blame] | 1266 | module_platform_driver(mxcmci_driver); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1267 | |
| 1268 | MODULE_DESCRIPTION("i.MX Multimedia Card Interface Driver"); |
| 1269 | MODULE_AUTHOR("Sascha Hauer, Pengutronix"); |
| 1270 | MODULE_LICENSE("GPL"); |
Fabio Estevam | 6eb30ad | 2012-10-09 16:26:05 -0300 | [diff] [blame] | 1271 | MODULE_ALIAS("platform:mxc-mmc"); |