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> |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 36 | |
| 37 | #include <asm/dma.h> |
| 38 | #include <asm/irq.h> |
| 39 | #include <asm/sizes.h> |
| 40 | #include <mach/mmc.h> |
| 41 | |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 42 | #include <mach/dma.h> |
Shawn Guo | edae899 | 2011-10-17 14:15:10 +0800 | [diff] [blame] | 43 | #include <mach/hardware.h> |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 44 | |
Sascha Hauer | 9563b1d | 2009-02-23 13:08:06 +0100 | [diff] [blame] | 45 | #define DRIVER_NAME "mxc-mmc" |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 46 | |
| 47 | #define MMC_REG_STR_STP_CLK 0x00 |
| 48 | #define MMC_REG_STATUS 0x04 |
| 49 | #define MMC_REG_CLK_RATE 0x08 |
| 50 | #define MMC_REG_CMD_DAT_CONT 0x0C |
| 51 | #define MMC_REG_RES_TO 0x10 |
| 52 | #define MMC_REG_READ_TO 0x14 |
| 53 | #define MMC_REG_BLK_LEN 0x18 |
| 54 | #define MMC_REG_NOB 0x1C |
| 55 | #define MMC_REG_REV_NO 0x20 |
| 56 | #define MMC_REG_INT_CNTR 0x24 |
| 57 | #define MMC_REG_CMD 0x28 |
| 58 | #define MMC_REG_ARG 0x2C |
| 59 | #define MMC_REG_RES_FIFO 0x34 |
| 60 | #define MMC_REG_BUFFER_ACCESS 0x38 |
| 61 | |
| 62 | #define STR_STP_CLK_RESET (1 << 3) |
| 63 | #define STR_STP_CLK_START_CLK (1 << 1) |
| 64 | #define STR_STP_CLK_STOP_CLK (1 << 0) |
| 65 | |
| 66 | #define STATUS_CARD_INSERTION (1 << 31) |
| 67 | #define STATUS_CARD_REMOVAL (1 << 30) |
| 68 | #define STATUS_YBUF_EMPTY (1 << 29) |
| 69 | #define STATUS_XBUF_EMPTY (1 << 28) |
| 70 | #define STATUS_YBUF_FULL (1 << 27) |
| 71 | #define STATUS_XBUF_FULL (1 << 26) |
| 72 | #define STATUS_BUF_UND_RUN (1 << 25) |
| 73 | #define STATUS_BUF_OVFL (1 << 24) |
| 74 | #define STATUS_SDIO_INT_ACTIVE (1 << 14) |
| 75 | #define STATUS_END_CMD_RESP (1 << 13) |
| 76 | #define STATUS_WRITE_OP_DONE (1 << 12) |
| 77 | #define STATUS_DATA_TRANS_DONE (1 << 11) |
| 78 | #define STATUS_READ_OP_DONE (1 << 11) |
| 79 | #define STATUS_WR_CRC_ERROR_CODE_MASK (3 << 10) |
| 80 | #define STATUS_CARD_BUS_CLK_RUN (1 << 8) |
| 81 | #define STATUS_BUF_READ_RDY (1 << 7) |
| 82 | #define STATUS_BUF_WRITE_RDY (1 << 6) |
| 83 | #define STATUS_RESP_CRC_ERR (1 << 5) |
| 84 | #define STATUS_CRC_READ_ERR (1 << 3) |
| 85 | #define STATUS_CRC_WRITE_ERR (1 << 2) |
| 86 | #define STATUS_TIME_OUT_RESP (1 << 1) |
| 87 | #define STATUS_TIME_OUT_READ (1 << 0) |
| 88 | #define STATUS_ERR_MASK 0x2f |
| 89 | |
| 90 | #define CMD_DAT_CONT_CMD_RESP_LONG_OFF (1 << 12) |
| 91 | #define CMD_DAT_CONT_STOP_READWAIT (1 << 11) |
| 92 | #define CMD_DAT_CONT_START_READWAIT (1 << 10) |
| 93 | #define CMD_DAT_CONT_BUS_WIDTH_4 (2 << 8) |
| 94 | #define CMD_DAT_CONT_INIT (1 << 7) |
| 95 | #define CMD_DAT_CONT_WRITE (1 << 4) |
| 96 | #define CMD_DAT_CONT_DATA_ENABLE (1 << 3) |
| 97 | #define CMD_DAT_CONT_RESPONSE_48BIT_CRC (1 << 0) |
| 98 | #define CMD_DAT_CONT_RESPONSE_136BIT (2 << 0) |
| 99 | #define CMD_DAT_CONT_RESPONSE_48BIT (3 << 0) |
| 100 | |
| 101 | #define INT_SDIO_INT_WKP_EN (1 << 18) |
| 102 | #define INT_CARD_INSERTION_WKP_EN (1 << 17) |
| 103 | #define INT_CARD_REMOVAL_WKP_EN (1 << 16) |
| 104 | #define INT_CARD_INSERTION_EN (1 << 15) |
| 105 | #define INT_CARD_REMOVAL_EN (1 << 14) |
| 106 | #define INT_SDIO_IRQ_EN (1 << 13) |
| 107 | #define INT_DAT0_EN (1 << 12) |
| 108 | #define INT_BUF_READ_EN (1 << 4) |
| 109 | #define INT_BUF_WRITE_EN (1 << 3) |
| 110 | #define INT_END_CMD_RES_EN (1 << 2) |
| 111 | #define INT_WRITE_OP_DONE_EN (1 << 1) |
| 112 | #define INT_READ_OP_EN (1 << 0) |
| 113 | |
| 114 | struct mxcmci_host { |
| 115 | struct mmc_host *mmc; |
| 116 | struct resource *res; |
| 117 | void __iomem *base; |
| 118 | int irq; |
| 119 | int detect_irq; |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 120 | struct dma_chan *dma; |
| 121 | struct dma_async_tx_descriptor *desc; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 122 | int do_dma; |
Eric Bénard | 16b3bf8 | 2010-05-19 18:46:04 +0200 | [diff] [blame] | 123 | int default_irq_mask; |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 124 | int use_sdio; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 125 | unsigned int power_mode; |
| 126 | struct imxmmc_platform_data *pdata; |
| 127 | |
| 128 | struct mmc_request *req; |
| 129 | struct mmc_command *cmd; |
| 130 | struct mmc_data *data; |
| 131 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 132 | unsigned int datasize; |
| 133 | unsigned int dma_dir; |
| 134 | |
| 135 | u16 rev_no; |
| 136 | unsigned int cmdat; |
| 137 | |
| 138 | struct clk *clk; |
| 139 | |
| 140 | int clock; |
| 141 | |
| 142 | struct work_struct datawork; |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 143 | spinlock_t lock; |
Alberto Panizzo | 74b6695 | 2010-11-02 01:05:37 +0100 | [diff] [blame] | 144 | |
| 145 | struct regulator *vcc; |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 146 | |
| 147 | int burstlen; |
| 148 | int dmareq; |
| 149 | struct dma_slave_config dma_slave_config; |
| 150 | struct imx_dma_data dma_data; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 151 | }; |
| 152 | |
Martin Fuzzey | 18489fa | 2009-04-16 22:00:36 +0200 | [diff] [blame] | 153 | static void mxcmci_set_clk_rate(struct mxcmci_host *host, unsigned int clk_ios); |
| 154 | |
Alberto Panizzo | 74b6695 | 2010-11-02 01:05:37 +0100 | [diff] [blame] | 155 | static inline void mxcmci_init_ocr(struct mxcmci_host *host) |
| 156 | { |
Alberto Panizzo | 74b6695 | 2010-11-02 01:05:37 +0100 | [diff] [blame] | 157 | host->vcc = regulator_get(mmc_dev(host->mmc), "vmmc"); |
| 158 | |
| 159 | if (IS_ERR(host->vcc)) { |
| 160 | host->vcc = NULL; |
| 161 | } else { |
| 162 | host->mmc->ocr_avail = mmc_regulator_get_ocrmask(host->vcc); |
| 163 | if (host->pdata && host->pdata->ocr_avail) |
| 164 | dev_warn(mmc_dev(host->mmc), |
| 165 | "pdata->ocr_avail will not be used\n"); |
| 166 | } |
Alberto Panizzo | d078d24 | 2010-11-09 11:35:37 +0100 | [diff] [blame] | 167 | |
Alberto Panizzo | 74b6695 | 2010-11-02 01:05:37 +0100 | [diff] [blame] | 168 | if (host->vcc == NULL) { |
| 169 | /* fall-back to platform data */ |
| 170 | if (host->pdata && host->pdata->ocr_avail) |
| 171 | host->mmc->ocr_avail = host->pdata->ocr_avail; |
| 172 | else |
| 173 | host->mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; |
| 174 | } |
| 175 | } |
| 176 | |
Alberto Panizzo | d078d24 | 2010-11-09 11:35:37 +0100 | [diff] [blame] | 177 | static inline void mxcmci_set_power(struct mxcmci_host *host, |
| 178 | unsigned char power_mode, |
| 179 | unsigned int vdd) |
Alberto Panizzo | 74b6695 | 2010-11-02 01:05:37 +0100 | [diff] [blame] | 180 | { |
Alberto Panizzo | d078d24 | 2010-11-09 11:35:37 +0100 | [diff] [blame] | 181 | if (host->vcc) { |
| 182 | if (power_mode == MMC_POWER_UP) |
| 183 | mmc_regulator_set_ocr(host->mmc, host->vcc, vdd); |
| 184 | else if (power_mode == MMC_POWER_OFF) |
| 185 | mmc_regulator_set_ocr(host->mmc, host->vcc, 0); |
| 186 | } |
| 187 | |
Alberto Panizzo | 74b6695 | 2010-11-02 01:05:37 +0100 | [diff] [blame] | 188 | if (host->pdata && host->pdata->setpower) |
| 189 | host->pdata->setpower(mmc_dev(host->mmc), vdd); |
| 190 | } |
| 191 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 192 | static inline int mxcmci_use_dma(struct mxcmci_host *host) |
| 193 | { |
| 194 | return host->do_dma; |
| 195 | } |
| 196 | |
| 197 | static void mxcmci_softreset(struct mxcmci_host *host) |
| 198 | { |
| 199 | int i; |
| 200 | |
Daniel Mack | 4725f6f | 2010-04-01 10:03:23 +0200 | [diff] [blame] | 201 | dev_dbg(mmc_dev(host->mmc), "mxcmci_softreset\n"); |
| 202 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 203 | /* reset sequence */ |
| 204 | writew(STR_STP_CLK_RESET, host->base + MMC_REG_STR_STP_CLK); |
| 205 | writew(STR_STP_CLK_RESET | STR_STP_CLK_START_CLK, |
| 206 | host->base + MMC_REG_STR_STP_CLK); |
| 207 | |
| 208 | for (i = 0; i < 8; i++) |
| 209 | writew(STR_STP_CLK_START_CLK, host->base + MMC_REG_STR_STP_CLK); |
| 210 | |
| 211 | writew(0xff, host->base + MMC_REG_RES_TO); |
| 212 | } |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 213 | static int mxcmci_setup_dma(struct mmc_host *mmc); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 214 | |
Martin Fuzzey | 656217d | 2009-04-16 22:00:41 +0200 | [diff] [blame] | 215 | 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] | 216 | { |
| 217 | unsigned int nob = data->blocks; |
| 218 | unsigned int blksz = data->blksz; |
| 219 | unsigned int datasize = nob * blksz; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 220 | struct scatterlist *sg; |
Vinod Koul | 05f5799 | 2011-10-14 10:45:11 +0530 | [diff] [blame] | 221 | enum dma_transfer_direction slave_dirn; |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 222 | int i, nents; |
| 223 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 224 | if (data->flags & MMC_DATA_STREAM) |
| 225 | nob = 0xffff; |
| 226 | |
| 227 | host->data = data; |
| 228 | data->bytes_xfered = 0; |
| 229 | |
| 230 | writew(nob, host->base + MMC_REG_NOB); |
| 231 | writew(blksz, host->base + MMC_REG_BLK_LEN); |
| 232 | host->datasize = datasize; |
| 233 | |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 234 | if (!mxcmci_use_dma(host)) |
| 235 | return 0; |
| 236 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 237 | for_each_sg(data->sg, sg, data->sg_len, i) { |
| 238 | if (sg->offset & 3 || sg->length & 3) { |
| 239 | host->do_dma = 0; |
Martin Fuzzey | 656217d | 2009-04-16 22:00:41 +0200 | [diff] [blame] | 240 | return 0; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
Vinod Koul | 05f5799 | 2011-10-14 10:45:11 +0530 | [diff] [blame] | 244 | if (data->flags & MMC_DATA_READ) { |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 245 | host->dma_dir = DMA_FROM_DEVICE; |
Vinod Koul | 05f5799 | 2011-10-14 10:45:11 +0530 | [diff] [blame] | 246 | slave_dirn = DMA_DEV_TO_MEM; |
| 247 | } else { |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 248 | host->dma_dir = DMA_TO_DEVICE; |
Vinod Koul | 05f5799 | 2011-10-14 10:45:11 +0530 | [diff] [blame] | 249 | slave_dirn = DMA_MEM_TO_DEV; |
| 250 | } |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 251 | |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 252 | nents = dma_map_sg(host->dma->device->dev, data->sg, |
| 253 | data->sg_len, host->dma_dir); |
| 254 | if (nents != data->sg_len) |
| 255 | return -EINVAL; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 256 | |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 257 | host->desc = host->dma->device->device_prep_slave_sg(host->dma, |
Vinod Koul | 05f5799 | 2011-10-14 10:45:11 +0530 | [diff] [blame] | 258 | data->sg, data->sg_len, slave_dirn, |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 259 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 260 | |
| 261 | if (!host->desc) { |
| 262 | dma_unmap_sg(host->dma->device->dev, data->sg, data->sg_len, |
| 263 | host->dma_dir); |
| 264 | host->do_dma = 0; |
| 265 | return 0; /* Fall back to PIO */ |
Martin Fuzzey | 656217d | 2009-04-16 22:00:41 +0200 | [diff] [blame] | 266 | } |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 267 | wmb(); |
| 268 | |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 269 | dmaengine_submit(host->desc); |
Sascha Hauer | 439aa0e | 2012-01-09 10:32:47 +0100 | [diff] [blame^] | 270 | dma_async_issue_pending(host->dma); |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 271 | |
Martin Fuzzey | 656217d | 2009-04-16 22:00:41 +0200 | [diff] [blame] | 272 | return 0; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static int mxcmci_start_cmd(struct mxcmci_host *host, struct mmc_command *cmd, |
| 276 | unsigned int cmdat) |
| 277 | { |
Eric Bénard | 16b3bf8 | 2010-05-19 18:46:04 +0200 | [diff] [blame] | 278 | u32 int_cntr = host->default_irq_mask; |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 279 | unsigned long flags; |
| 280 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 281 | WARN_ON(host->cmd != NULL); |
| 282 | host->cmd = cmd; |
| 283 | |
| 284 | switch (mmc_resp_type(cmd)) { |
| 285 | case MMC_RSP_R1: /* short CRC, OPCODE */ |
| 286 | case MMC_RSP_R1B:/* short CRC, OPCODE, BUSY */ |
| 287 | cmdat |= CMD_DAT_CONT_RESPONSE_48BIT_CRC; |
| 288 | break; |
| 289 | case MMC_RSP_R2: /* long 136 bit + CRC */ |
| 290 | cmdat |= CMD_DAT_CONT_RESPONSE_136BIT; |
| 291 | break; |
| 292 | case MMC_RSP_R3: /* short */ |
| 293 | cmdat |= CMD_DAT_CONT_RESPONSE_48BIT; |
| 294 | break; |
| 295 | case MMC_RSP_NONE: |
| 296 | break; |
| 297 | default: |
| 298 | dev_err(mmc_dev(host->mmc), "unhandled response type 0x%x\n", |
| 299 | mmc_resp_type(cmd)); |
| 300 | cmd->error = -EINVAL; |
| 301 | return -EINVAL; |
| 302 | } |
| 303 | |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 304 | int_cntr = INT_END_CMD_RES_EN; |
| 305 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 306 | if (mxcmci_use_dma(host)) |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 307 | int_cntr |= INT_READ_OP_EN | INT_WRITE_OP_DONE_EN; |
| 308 | |
| 309 | spin_lock_irqsave(&host->lock, flags); |
| 310 | if (host->use_sdio) |
| 311 | int_cntr |= INT_SDIO_IRQ_EN; |
| 312 | writel(int_cntr, host->base + MMC_REG_INT_CNTR); |
| 313 | spin_unlock_irqrestore(&host->lock, flags); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 314 | |
| 315 | writew(cmd->opcode, host->base + MMC_REG_CMD); |
| 316 | writel(cmd->arg, host->base + MMC_REG_ARG); |
| 317 | writew(cmdat, host->base + MMC_REG_CMD_DAT_CONT); |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | static void mxcmci_finish_request(struct mxcmci_host *host, |
| 323 | struct mmc_request *req) |
| 324 | { |
Eric Bénard | 16b3bf8 | 2010-05-19 18:46:04 +0200 | [diff] [blame] | 325 | u32 int_cntr = host->default_irq_mask; |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 326 | unsigned long flags; |
| 327 | |
| 328 | spin_lock_irqsave(&host->lock, flags); |
| 329 | if (host->use_sdio) |
| 330 | int_cntr |= INT_SDIO_IRQ_EN; |
| 331 | writel(int_cntr, host->base + MMC_REG_INT_CNTR); |
| 332 | spin_unlock_irqrestore(&host->lock, flags); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 333 | |
| 334 | host->req = NULL; |
| 335 | host->cmd = NULL; |
| 336 | host->data = NULL; |
| 337 | |
| 338 | mmc_request_done(host->mmc, req); |
| 339 | } |
| 340 | |
| 341 | static int mxcmci_finish_data(struct mxcmci_host *host, unsigned int stat) |
| 342 | { |
| 343 | struct mmc_data *data = host->data; |
| 344 | int data_error; |
| 345 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 346 | if (mxcmci_use_dma(host)) { |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 347 | dmaengine_terminate_all(host->dma); |
| 348 | dma_unmap_sg(host->dma->device->dev, data->sg, data->sg_len, |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 349 | host->dma_dir); |
| 350 | } |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 351 | |
| 352 | if (stat & STATUS_ERR_MASK) { |
| 353 | dev_dbg(mmc_dev(host->mmc), "request failed. status: 0x%08x\n", |
| 354 | stat); |
| 355 | if (stat & STATUS_CRC_READ_ERR) { |
Daniel Mack | 4725f6f | 2010-04-01 10:03:23 +0200 | [diff] [blame] | 356 | dev_err(mmc_dev(host->mmc), "%s: -EILSEQ\n", __func__); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 357 | data->error = -EILSEQ; |
| 358 | } else if (stat & STATUS_CRC_WRITE_ERR) { |
| 359 | u32 err_code = (stat >> 9) & 0x3; |
Daniel Mack | 4725f6f | 2010-04-01 10:03:23 +0200 | [diff] [blame] | 360 | if (err_code == 2) { /* No CRC response */ |
| 361 | dev_err(mmc_dev(host->mmc), |
| 362 | "%s: No CRC -ETIMEDOUT\n", __func__); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 363 | data->error = -ETIMEDOUT; |
Daniel Mack | 4725f6f | 2010-04-01 10:03:23 +0200 | [diff] [blame] | 364 | } else { |
| 365 | dev_err(mmc_dev(host->mmc), |
| 366 | "%s: -EILSEQ\n", __func__); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 367 | data->error = -EILSEQ; |
Daniel Mack | 4725f6f | 2010-04-01 10:03:23 +0200 | [diff] [blame] | 368 | } |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 369 | } else if (stat & STATUS_TIME_OUT_READ) { |
Daniel Mack | 4725f6f | 2010-04-01 10:03:23 +0200 | [diff] [blame] | 370 | dev_err(mmc_dev(host->mmc), |
| 371 | "%s: read -ETIMEDOUT\n", __func__); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 372 | data->error = -ETIMEDOUT; |
| 373 | } else { |
Daniel Mack | 4725f6f | 2010-04-01 10:03:23 +0200 | [diff] [blame] | 374 | dev_err(mmc_dev(host->mmc), "%s: -EIO\n", __func__); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 375 | data->error = -EIO; |
| 376 | } |
| 377 | } else { |
| 378 | data->bytes_xfered = host->datasize; |
| 379 | } |
| 380 | |
| 381 | data_error = data->error; |
| 382 | |
| 383 | host->data = NULL; |
| 384 | |
| 385 | return data_error; |
| 386 | } |
| 387 | |
| 388 | static void mxcmci_read_response(struct mxcmci_host *host, unsigned int stat) |
| 389 | { |
| 390 | struct mmc_command *cmd = host->cmd; |
| 391 | int i; |
| 392 | u32 a, b, c; |
| 393 | |
| 394 | if (!cmd) |
| 395 | return; |
| 396 | |
| 397 | if (stat & STATUS_TIME_OUT_RESP) { |
| 398 | dev_dbg(mmc_dev(host->mmc), "CMD TIMEOUT\n"); |
| 399 | cmd->error = -ETIMEDOUT; |
| 400 | } else if (stat & STATUS_RESP_CRC_ERR && cmd->flags & MMC_RSP_CRC) { |
| 401 | dev_dbg(mmc_dev(host->mmc), "cmd crc error\n"); |
| 402 | cmd->error = -EILSEQ; |
| 403 | } |
| 404 | |
| 405 | if (cmd->flags & MMC_RSP_PRESENT) { |
| 406 | if (cmd->flags & MMC_RSP_136) { |
| 407 | for (i = 0; i < 4; i++) { |
| 408 | a = readw(host->base + MMC_REG_RES_FIFO); |
| 409 | b = readw(host->base + MMC_REG_RES_FIFO); |
| 410 | cmd->resp[i] = a << 16 | b; |
| 411 | } |
| 412 | } else { |
| 413 | a = readw(host->base + MMC_REG_RES_FIFO); |
| 414 | b = readw(host->base + MMC_REG_RES_FIFO); |
| 415 | c = readw(host->base + MMC_REG_RES_FIFO); |
| 416 | cmd->resp[0] = a << 24 | b << 8 | c >> 8; |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | static int mxcmci_poll_status(struct mxcmci_host *host, u32 mask) |
| 422 | { |
| 423 | u32 stat; |
| 424 | unsigned long timeout = jiffies + HZ; |
| 425 | |
| 426 | do { |
| 427 | stat = readl(host->base + MMC_REG_STATUS); |
| 428 | if (stat & STATUS_ERR_MASK) |
| 429 | return stat; |
Martin Fuzzey | 18489fa | 2009-04-16 22:00:36 +0200 | [diff] [blame] | 430 | if (time_after(jiffies, timeout)) { |
| 431 | mxcmci_softreset(host); |
| 432 | mxcmci_set_clk_rate(host, host->clock); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 433 | return STATUS_TIME_OUT_READ; |
Martin Fuzzey | 18489fa | 2009-04-16 22:00:36 +0200 | [diff] [blame] | 434 | } |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 435 | if (stat & mask) |
| 436 | return 0; |
| 437 | cpu_relax(); |
| 438 | } while (1); |
| 439 | } |
| 440 | |
| 441 | static int mxcmci_pull(struct mxcmci_host *host, void *_buf, int bytes) |
| 442 | { |
| 443 | unsigned int stat; |
| 444 | u32 *buf = _buf; |
| 445 | |
| 446 | while (bytes > 3) { |
| 447 | stat = mxcmci_poll_status(host, |
| 448 | STATUS_BUF_READ_RDY | STATUS_READ_OP_DONE); |
| 449 | if (stat) |
| 450 | return stat; |
| 451 | *buf++ = readl(host->base + MMC_REG_BUFFER_ACCESS); |
| 452 | bytes -= 4; |
| 453 | } |
| 454 | |
| 455 | if (bytes) { |
| 456 | u8 *b = (u8 *)buf; |
| 457 | u32 tmp; |
| 458 | |
| 459 | stat = mxcmci_poll_status(host, |
| 460 | STATUS_BUF_READ_RDY | STATUS_READ_OP_DONE); |
| 461 | if (stat) |
| 462 | return stat; |
| 463 | tmp = readl(host->base + MMC_REG_BUFFER_ACCESS); |
| 464 | memcpy(b, &tmp, bytes); |
| 465 | } |
| 466 | |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | static int mxcmci_push(struct mxcmci_host *host, void *_buf, int bytes) |
| 471 | { |
| 472 | unsigned int stat; |
| 473 | u32 *buf = _buf; |
| 474 | |
| 475 | while (bytes > 3) { |
| 476 | stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY); |
| 477 | if (stat) |
| 478 | return stat; |
| 479 | writel(*buf++, host->base + MMC_REG_BUFFER_ACCESS); |
| 480 | bytes -= 4; |
| 481 | } |
| 482 | |
| 483 | if (bytes) { |
| 484 | u8 *b = (u8 *)buf; |
| 485 | u32 tmp; |
| 486 | |
| 487 | stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY); |
| 488 | if (stat) |
| 489 | return stat; |
| 490 | |
| 491 | memcpy(&tmp, b, bytes); |
| 492 | writel(tmp, host->base + MMC_REG_BUFFER_ACCESS); |
| 493 | } |
| 494 | |
| 495 | stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY); |
| 496 | if (stat) |
| 497 | return stat; |
| 498 | |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | static int mxcmci_transfer_data(struct mxcmci_host *host) |
| 503 | { |
| 504 | struct mmc_data *data = host->req->data; |
| 505 | struct scatterlist *sg; |
| 506 | int stat, i; |
| 507 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 508 | host->data = data; |
| 509 | host->datasize = 0; |
| 510 | |
| 511 | if (data->flags & MMC_DATA_READ) { |
| 512 | for_each_sg(data->sg, sg, data->sg_len, i) { |
| 513 | stat = mxcmci_pull(host, sg_virt(sg), sg->length); |
| 514 | if (stat) |
| 515 | return stat; |
| 516 | host->datasize += sg->length; |
| 517 | } |
| 518 | } else { |
| 519 | for_each_sg(data->sg, sg, data->sg_len, i) { |
| 520 | stat = mxcmci_push(host, sg_virt(sg), sg->length); |
| 521 | if (stat) |
| 522 | return stat; |
| 523 | host->datasize += sg->length; |
| 524 | } |
| 525 | stat = mxcmci_poll_status(host, STATUS_WRITE_OP_DONE); |
| 526 | if (stat) |
| 527 | return stat; |
| 528 | } |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | static void mxcmci_datawork(struct work_struct *work) |
| 533 | { |
| 534 | struct mxcmci_host *host = container_of(work, struct mxcmci_host, |
| 535 | datawork); |
| 536 | int datastat = mxcmci_transfer_data(host); |
Daniel Mack | 4a31f2e | 2010-04-27 12:24:42 +0200 | [diff] [blame] | 537 | |
| 538 | writel(STATUS_READ_OP_DONE | STATUS_WRITE_OP_DONE, |
| 539 | host->base + MMC_REG_STATUS); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 540 | mxcmci_finish_data(host, datastat); |
| 541 | |
| 542 | if (host->req->stop) { |
| 543 | if (mxcmci_start_cmd(host, host->req->stop, 0)) { |
| 544 | mxcmci_finish_request(host, host->req); |
| 545 | return; |
| 546 | } |
| 547 | } else { |
| 548 | mxcmci_finish_request(host, host->req); |
| 549 | } |
| 550 | } |
| 551 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 552 | static void mxcmci_data_done(struct mxcmci_host *host, unsigned int stat) |
| 553 | { |
| 554 | struct mmc_data *data = host->data; |
| 555 | int data_error; |
| 556 | |
| 557 | if (!data) |
| 558 | return; |
| 559 | |
| 560 | data_error = mxcmci_finish_data(host, stat); |
| 561 | |
| 562 | mxcmci_read_response(host, stat); |
| 563 | host->cmd = NULL; |
| 564 | |
| 565 | if (host->req->stop) { |
| 566 | if (mxcmci_start_cmd(host, host->req->stop, 0)) { |
| 567 | mxcmci_finish_request(host, host->req); |
| 568 | return; |
| 569 | } |
| 570 | } else { |
| 571 | mxcmci_finish_request(host, host->req); |
| 572 | } |
| 573 | } |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 574 | |
| 575 | static void mxcmci_cmd_done(struct mxcmci_host *host, unsigned int stat) |
| 576 | { |
| 577 | mxcmci_read_response(host, stat); |
| 578 | host->cmd = NULL; |
| 579 | |
| 580 | if (!host->data && host->req) { |
| 581 | mxcmci_finish_request(host, host->req); |
| 582 | return; |
| 583 | } |
| 584 | |
| 585 | /* For the DMA case the DMA engine handles the data transfer |
Anand Gadiyar | fd589a8 | 2009-07-16 17:13:03 +0200 | [diff] [blame] | 586 | * automatically. For non DMA we have to do it ourselves. |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 587 | * Don't do it in interrupt context though. |
| 588 | */ |
| 589 | if (!mxcmci_use_dma(host) && host->data) |
| 590 | schedule_work(&host->datawork); |
| 591 | |
| 592 | } |
| 593 | |
| 594 | static irqreturn_t mxcmci_irq(int irq, void *devid) |
| 595 | { |
| 596 | struct mxcmci_host *host = devid; |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 597 | unsigned long flags; |
| 598 | bool sdio_irq; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 599 | u32 stat; |
| 600 | |
| 601 | stat = readl(host->base + MMC_REG_STATUS); |
Daniel Mack | 4a31f2e | 2010-04-27 12:24:42 +0200 | [diff] [blame] | 602 | writel(stat & ~(STATUS_SDIO_INT_ACTIVE | STATUS_DATA_TRANS_DONE | |
| 603 | STATUS_WRITE_OP_DONE), host->base + MMC_REG_STATUS); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 604 | |
| 605 | dev_dbg(mmc_dev(host->mmc), "%s: 0x%08x\n", __func__, stat); |
| 606 | |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 607 | spin_lock_irqsave(&host->lock, flags); |
| 608 | sdio_irq = (stat & STATUS_SDIO_INT_ACTIVE) && host->use_sdio; |
| 609 | spin_unlock_irqrestore(&host->lock, flags); |
| 610 | |
Daniel Mack | 4a31f2e | 2010-04-27 12:24:42 +0200 | [diff] [blame] | 611 | if (mxcmci_use_dma(host) && |
| 612 | (stat & (STATUS_READ_OP_DONE | STATUS_WRITE_OP_DONE))) |
| 613 | writel(STATUS_READ_OP_DONE | STATUS_WRITE_OP_DONE, |
| 614 | host->base + MMC_REG_STATUS); |
Daniel Mack | 4a31f2e | 2010-04-27 12:24:42 +0200 | [diff] [blame] | 615 | |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 616 | if (sdio_irq) { |
| 617 | writel(STATUS_SDIO_INT_ACTIVE, host->base + MMC_REG_STATUS); |
| 618 | mmc_signal_sdio_irq(host->mmc); |
| 619 | } |
| 620 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 621 | if (stat & STATUS_END_CMD_RESP) |
| 622 | mxcmci_cmd_done(host, stat); |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 623 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 624 | if (mxcmci_use_dma(host) && |
| 625 | (stat & (STATUS_DATA_TRANS_DONE | STATUS_WRITE_OP_DONE))) |
| 626 | mxcmci_data_done(host, stat); |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 627 | |
Eric Bénard | 16b3bf8 | 2010-05-19 18:46:04 +0200 | [diff] [blame] | 628 | if (host->default_irq_mask && |
| 629 | (stat & (STATUS_CARD_INSERTION | STATUS_CARD_REMOVAL))) |
| 630 | mmc_detect_change(host->mmc, msecs_to_jiffies(200)); |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 631 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 632 | return IRQ_HANDLED; |
| 633 | } |
| 634 | |
| 635 | static void mxcmci_request(struct mmc_host *mmc, struct mmc_request *req) |
| 636 | { |
| 637 | struct mxcmci_host *host = mmc_priv(mmc); |
| 638 | unsigned int cmdat = host->cmdat; |
Martin Fuzzey | 656217d | 2009-04-16 22:00:41 +0200 | [diff] [blame] | 639 | int error; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 640 | |
| 641 | WARN_ON(host->req != NULL); |
| 642 | |
| 643 | host->req = req; |
| 644 | host->cmdat &= ~CMD_DAT_CONT_INIT; |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 645 | |
| 646 | if (host->dma) |
| 647 | host->do_dma = 1; |
| 648 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 649 | if (req->data) { |
Martin Fuzzey | 656217d | 2009-04-16 22:00:41 +0200 | [diff] [blame] | 650 | error = mxcmci_setup_data(host, req->data); |
| 651 | if (error) { |
| 652 | req->cmd->error = error; |
| 653 | goto out; |
| 654 | } |
| 655 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 656 | |
| 657 | cmdat |= CMD_DAT_CONT_DATA_ENABLE; |
| 658 | |
| 659 | if (req->data->flags & MMC_DATA_WRITE) |
| 660 | cmdat |= CMD_DAT_CONT_WRITE; |
| 661 | } |
| 662 | |
Martin Fuzzey | 656217d | 2009-04-16 22:00:41 +0200 | [diff] [blame] | 663 | error = mxcmci_start_cmd(host, req->cmd, cmdat); |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 664 | |
Martin Fuzzey | 656217d | 2009-04-16 22:00:41 +0200 | [diff] [blame] | 665 | out: |
| 666 | if (error) |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 667 | mxcmci_finish_request(host, req); |
| 668 | } |
| 669 | |
| 670 | static void mxcmci_set_clk_rate(struct mxcmci_host *host, unsigned int clk_ios) |
| 671 | { |
| 672 | unsigned int divider; |
| 673 | int prescaler = 0; |
| 674 | unsigned int clk_in = clk_get_rate(host->clk); |
| 675 | |
| 676 | while (prescaler <= 0x800) { |
| 677 | for (divider = 1; divider <= 0xF; divider++) { |
| 678 | int x; |
| 679 | |
| 680 | x = (clk_in / (divider + 1)); |
| 681 | |
| 682 | if (prescaler) |
| 683 | x /= (prescaler * 2); |
| 684 | |
| 685 | if (x <= clk_ios) |
| 686 | break; |
| 687 | } |
| 688 | if (divider < 0x10) |
| 689 | break; |
| 690 | |
| 691 | if (prescaler == 0) |
| 692 | prescaler = 1; |
| 693 | else |
| 694 | prescaler <<= 1; |
| 695 | } |
| 696 | |
| 697 | writew((prescaler << 4) | divider, host->base + MMC_REG_CLK_RATE); |
| 698 | |
| 699 | dev_dbg(mmc_dev(host->mmc), "scaler: %d divider: %d in: %d out: %d\n", |
| 700 | prescaler, divider, clk_in, clk_ios); |
| 701 | } |
| 702 | |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 703 | static int mxcmci_setup_dma(struct mmc_host *mmc) |
| 704 | { |
| 705 | struct mxcmci_host *host = mmc_priv(mmc); |
| 706 | struct dma_slave_config *config = &host->dma_slave_config; |
| 707 | |
| 708 | config->dst_addr = host->res->start + MMC_REG_BUFFER_ACCESS; |
| 709 | config->src_addr = host->res->start + MMC_REG_BUFFER_ACCESS; |
| 710 | config->dst_addr_width = 4; |
| 711 | config->src_addr_width = 4; |
| 712 | config->dst_maxburst = host->burstlen; |
| 713 | config->src_maxburst = host->burstlen; |
| 714 | |
| 715 | return dmaengine_slave_config(host->dma, config); |
| 716 | } |
| 717 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 718 | static void mxcmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 719 | { |
| 720 | struct mxcmci_host *host = mmc_priv(mmc); |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 721 | int burstlen, ret; |
| 722 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 723 | /* |
Sascha Hauer | 6584cb8 | 2011-07-06 11:18:33 +0200 | [diff] [blame] | 724 | * use burstlen of 64 (16 words) in 4 bit mode (--> reg value 0) |
| 725 | * 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] | 726 | */ |
| 727 | if (ios->bus_width == MMC_BUS_WIDTH_4) |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 728 | burstlen = 16; |
Sascha Hauer | 6584cb8 | 2011-07-06 11:18:33 +0200 | [diff] [blame] | 729 | else |
| 730 | burstlen = 4; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 731 | |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 732 | if (mxcmci_use_dma(host) && burstlen != host->burstlen) { |
| 733 | host->burstlen = burstlen; |
| 734 | ret = mxcmci_setup_dma(mmc); |
| 735 | if (ret) { |
| 736 | dev_err(mmc_dev(host->mmc), |
| 737 | "failed to config DMA channel. Falling back to PIO\n"); |
| 738 | dma_release_channel(host->dma); |
| 739 | host->do_dma = 0; |
Sascha Hauer | e58f516 | 2011-11-11 16:28:05 +0100 | [diff] [blame] | 740 | host->dma = NULL; |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 744 | if (ios->bus_width == MMC_BUS_WIDTH_4) |
| 745 | host->cmdat |= CMD_DAT_CONT_BUS_WIDTH_4; |
| 746 | else |
| 747 | host->cmdat &= ~CMD_DAT_CONT_BUS_WIDTH_4; |
| 748 | |
| 749 | if (host->power_mode != ios->power_mode) { |
Alberto Panizzo | d078d24 | 2010-11-09 11:35:37 +0100 | [diff] [blame] | 750 | mxcmci_set_power(host, ios->power_mode, ios->vdd); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 751 | host->power_mode = ios->power_mode; |
Alberto Panizzo | 74b6695 | 2010-11-02 01:05:37 +0100 | [diff] [blame] | 752 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 753 | if (ios->power_mode == MMC_POWER_ON) |
| 754 | host->cmdat |= CMD_DAT_CONT_INIT; |
| 755 | } |
| 756 | |
| 757 | if (ios->clock) { |
| 758 | mxcmci_set_clk_rate(host, ios->clock); |
| 759 | writew(STR_STP_CLK_START_CLK, host->base + MMC_REG_STR_STP_CLK); |
| 760 | } else { |
| 761 | writew(STR_STP_CLK_STOP_CLK, host->base + MMC_REG_STR_STP_CLK); |
| 762 | } |
| 763 | |
| 764 | host->clock = ios->clock; |
| 765 | } |
| 766 | |
| 767 | static irqreturn_t mxcmci_detect_irq(int irq, void *data) |
| 768 | { |
| 769 | struct mmc_host *mmc = data; |
| 770 | |
| 771 | dev_dbg(mmc_dev(mmc), "%s\n", __func__); |
| 772 | |
| 773 | mmc_detect_change(mmc, msecs_to_jiffies(250)); |
| 774 | return IRQ_HANDLED; |
| 775 | } |
| 776 | |
| 777 | static int mxcmci_get_ro(struct mmc_host *mmc) |
| 778 | { |
| 779 | struct mxcmci_host *host = mmc_priv(mmc); |
| 780 | |
| 781 | if (host->pdata && host->pdata->get_ro) |
| 782 | return !!host->pdata->get_ro(mmc_dev(mmc)); |
| 783 | /* |
| 784 | * Board doesn't support read only detection; let the mmc core |
| 785 | * decide what to do. |
| 786 | */ |
| 787 | return -ENOSYS; |
| 788 | } |
| 789 | |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 790 | static void mxcmci_enable_sdio_irq(struct mmc_host *mmc, int enable) |
| 791 | { |
| 792 | struct mxcmci_host *host = mmc_priv(mmc); |
| 793 | unsigned long flags; |
| 794 | u32 int_cntr; |
| 795 | |
| 796 | spin_lock_irqsave(&host->lock, flags); |
| 797 | host->use_sdio = enable; |
| 798 | int_cntr = readl(host->base + MMC_REG_INT_CNTR); |
| 799 | |
| 800 | if (enable) |
| 801 | int_cntr |= INT_SDIO_IRQ_EN; |
| 802 | else |
| 803 | int_cntr &= ~INT_SDIO_IRQ_EN; |
| 804 | |
| 805 | writel(int_cntr, host->base + MMC_REG_INT_CNTR); |
| 806 | spin_unlock_irqrestore(&host->lock, flags); |
| 807 | } |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 808 | |
Daniel Mack | 3fcb027 | 2010-04-01 10:03:25 +0200 | [diff] [blame] | 809 | static void mxcmci_init_card(struct mmc_host *host, struct mmc_card *card) |
| 810 | { |
| 811 | /* |
| 812 | * MX3 SoCs have a silicon bug which corrupts CRC calculation of |
| 813 | * multi-block transfers when connected SDIO peripheral doesn't |
| 814 | * drive the BUSY line as required by the specs. |
| 815 | * One way to prevent this is to only allow 1-bit transfers. |
| 816 | */ |
| 817 | |
| 818 | if (cpu_is_mx3() && card->type == MMC_TYPE_SDIO) |
| 819 | host->caps &= ~MMC_CAP_4_BIT_DATA; |
| 820 | else |
| 821 | host->caps |= MMC_CAP_4_BIT_DATA; |
| 822 | } |
| 823 | |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 824 | static bool filter(struct dma_chan *chan, void *param) |
| 825 | { |
| 826 | struct mxcmci_host *host = param; |
| 827 | |
| 828 | if (!imx_dma_is_general_purpose(chan)) |
| 829 | return false; |
| 830 | |
| 831 | chan->private = &host->dma_data; |
| 832 | |
| 833 | return true; |
| 834 | } |
| 835 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 836 | static const struct mmc_host_ops mxcmci_ops = { |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 837 | .request = mxcmci_request, |
| 838 | .set_ios = mxcmci_set_ios, |
| 839 | .get_ro = mxcmci_get_ro, |
| 840 | .enable_sdio_irq = mxcmci_enable_sdio_irq, |
Daniel Mack | 3fcb027 | 2010-04-01 10:03:25 +0200 | [diff] [blame] | 841 | .init_card = mxcmci_init_card, |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 842 | }; |
| 843 | |
| 844 | static int mxcmci_probe(struct platform_device *pdev) |
| 845 | { |
| 846 | struct mmc_host *mmc; |
| 847 | struct mxcmci_host *host = NULL; |
Uwe Kleine-König | c0521ba | 2009-12-14 18:01:17 -0800 | [diff] [blame] | 848 | struct resource *iores, *r; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 849 | int ret = 0, irq; |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 850 | dma_cap_mask_t mask; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 851 | |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 852 | pr_info("i.MX SDHC driver\n"); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 853 | |
Uwe Kleine-König | c0521ba | 2009-12-14 18:01:17 -0800 | [diff] [blame] | 854 | iores = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 855 | irq = platform_get_irq(pdev, 0); |
Uwe Kleine-König | c0521ba | 2009-12-14 18:01:17 -0800 | [diff] [blame] | 856 | if (!iores || irq < 0) |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 857 | return -EINVAL; |
| 858 | |
Uwe Kleine-König | c0521ba | 2009-12-14 18:01:17 -0800 | [diff] [blame] | 859 | r = request_mem_region(iores->start, resource_size(iores), pdev->name); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 860 | if (!r) |
| 861 | return -EBUSY; |
| 862 | |
| 863 | mmc = mmc_alloc_host(sizeof(struct mxcmci_host), &pdev->dev); |
| 864 | if (!mmc) { |
| 865 | ret = -ENOMEM; |
| 866 | goto out_release_mem; |
| 867 | } |
| 868 | |
| 869 | mmc->ops = &mxcmci_ops; |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 870 | mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 871 | |
| 872 | /* MMC core transfer sizes tunable parameters */ |
Martin K. Petersen | a36274e | 2010-09-10 01:33:59 -0400 | [diff] [blame] | 873 | mmc->max_segs = 64; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 874 | mmc->max_blk_size = 2048; |
| 875 | mmc->max_blk_count = 65535; |
| 876 | mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count; |
Vladimir Zapolskiy | d759c37 | 2010-03-10 15:23:57 -0800 | [diff] [blame] | 877 | mmc->max_seg_size = mmc->max_req_size; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 878 | |
| 879 | host = mmc_priv(mmc); |
| 880 | host->base = ioremap(r->start, resource_size(r)); |
| 881 | if (!host->base) { |
| 882 | ret = -ENOMEM; |
| 883 | goto out_free; |
| 884 | } |
| 885 | |
| 886 | host->mmc = mmc; |
| 887 | host->pdata = pdev->dev.platform_data; |
Daniel Mack | f441b99 | 2010-04-01 10:03:24 +0200 | [diff] [blame] | 888 | spin_lock_init(&host->lock); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 889 | |
Alberto Panizzo | 74b6695 | 2010-11-02 01:05:37 +0100 | [diff] [blame] | 890 | mxcmci_init_ocr(host); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 891 | |
Eric Bénard | 16b3bf8 | 2010-05-19 18:46:04 +0200 | [diff] [blame] | 892 | if (host->pdata && host->pdata->dat3_card_detect) |
| 893 | host->default_irq_mask = |
| 894 | INT_CARD_INSERTION_EN | INT_CARD_REMOVAL_EN; |
| 895 | else |
| 896 | host->default_irq_mask = 0; |
| 897 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 898 | host->res = r; |
| 899 | host->irq = irq; |
| 900 | |
Sascha Hauer | 06277b5 | 2009-02-23 13:33:31 +0100 | [diff] [blame] | 901 | host->clk = clk_get(&pdev->dev, NULL); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 902 | if (IS_ERR(host->clk)) { |
| 903 | ret = PTR_ERR(host->clk); |
| 904 | goto out_iounmap; |
| 905 | } |
| 906 | clk_enable(host->clk); |
| 907 | |
| 908 | mxcmci_softreset(host); |
| 909 | |
| 910 | host->rev_no = readw(host->base + MMC_REG_REV_NO); |
| 911 | if (host->rev_no != 0x400) { |
| 912 | ret = -ENODEV; |
| 913 | dev_err(mmc_dev(host->mmc), "wrong rev.no. 0x%08x. aborting.\n", |
| 914 | host->rev_no); |
| 915 | goto out_clk_put; |
| 916 | } |
| 917 | |
Sascha Hauer | c499b06 | 2009-04-03 14:41:56 +0200 | [diff] [blame] | 918 | mmc->f_min = clk_get_rate(host->clk) >> 16; |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 919 | mmc->f_max = clk_get_rate(host->clk) >> 1; |
| 920 | |
| 921 | /* recommended in data sheet */ |
| 922 | writew(0x2db4, host->base + MMC_REG_READ_TO); |
| 923 | |
Eric Bénard | 16b3bf8 | 2010-05-19 18:46:04 +0200 | [diff] [blame] | 924 | writel(host->default_irq_mask, host->base + MMC_REG_INT_CNTR); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 925 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 926 | r = platform_get_resource(pdev, IORESOURCE_DMA, 0); |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 927 | if (r) { |
| 928 | host->dmareq = r->start; |
| 929 | host->dma_data.peripheral_type = IMX_DMATYPE_SDHC; |
| 930 | host->dma_data.priority = DMA_PRIO_LOW; |
| 931 | host->dma_data.dma_request = host->dmareq; |
| 932 | dma_cap_zero(mask); |
| 933 | dma_cap_set(DMA_SLAVE, mask); |
| 934 | host->dma = dma_request_channel(mask, filter, host); |
| 935 | if (host->dma) |
| 936 | mmc->max_seg_size = dma_get_max_seg_size( |
| 937 | host->dma->device->dev); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 938 | } |
| 939 | |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 940 | if (!host->dma) |
| 941 | dev_info(mmc_dev(host->mmc), "dma not available. Using PIO\n"); |
| 942 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 943 | INIT_WORK(&host->datawork, mxcmci_datawork); |
| 944 | |
| 945 | ret = request_irq(host->irq, mxcmci_irq, 0, DRIVER_NAME, host); |
| 946 | if (ret) |
| 947 | goto out_free_dma; |
| 948 | |
| 949 | platform_set_drvdata(pdev, mmc); |
| 950 | |
| 951 | if (host->pdata && host->pdata->init) { |
| 952 | ret = host->pdata->init(&pdev->dev, mxcmci_detect_irq, |
| 953 | host->mmc); |
| 954 | if (ret) |
| 955 | goto out_free_irq; |
| 956 | } |
| 957 | |
| 958 | mmc_add_host(mmc); |
| 959 | |
| 960 | return 0; |
| 961 | |
| 962 | out_free_irq: |
| 963 | free_irq(host->irq, host); |
| 964 | out_free_dma: |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 965 | if (host->dma) |
| 966 | dma_release_channel(host->dma); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 967 | out_clk_put: |
| 968 | clk_disable(host->clk); |
| 969 | clk_put(host->clk); |
| 970 | out_iounmap: |
| 971 | iounmap(host->base); |
| 972 | out_free: |
| 973 | mmc_free_host(mmc); |
| 974 | out_release_mem: |
Uwe Kleine-König | c0521ba | 2009-12-14 18:01:17 -0800 | [diff] [blame] | 975 | release_mem_region(iores->start, resource_size(iores)); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 976 | return ret; |
| 977 | } |
| 978 | |
| 979 | static int mxcmci_remove(struct platform_device *pdev) |
| 980 | { |
| 981 | struct mmc_host *mmc = platform_get_drvdata(pdev); |
| 982 | struct mxcmci_host *host = mmc_priv(mmc); |
| 983 | |
| 984 | platform_set_drvdata(pdev, NULL); |
| 985 | |
| 986 | mmc_remove_host(mmc); |
| 987 | |
Alberto Panizzo | 74b6695 | 2010-11-02 01:05:37 +0100 | [diff] [blame] | 988 | if (host->vcc) |
| 989 | regulator_put(host->vcc); |
| 990 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 991 | if (host->pdata && host->pdata->exit) |
| 992 | host->pdata->exit(&pdev->dev, mmc); |
| 993 | |
| 994 | free_irq(host->irq, host); |
| 995 | iounmap(host->base); |
Sascha Hauer | f53fbde | 2011-02-18 10:21:09 +0100 | [diff] [blame] | 996 | |
| 997 | if (host->dma) |
| 998 | dma_release_channel(host->dma); |
| 999 | |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1000 | clk_disable(host->clk); |
| 1001 | clk_put(host->clk); |
| 1002 | |
| 1003 | release_mem_region(host->res->start, resource_size(host->res)); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1004 | |
| 1005 | mmc_free_host(mmc); |
| 1006 | |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
| 1010 | #ifdef CONFIG_PM |
Eric Bénard | a7d403c | 2010-06-17 20:59:07 +0200 | [diff] [blame] | 1011 | static int mxcmci_suspend(struct device *dev) |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1012 | { |
Eric Bénard | a7d403c | 2010-06-17 20:59:07 +0200 | [diff] [blame] | 1013 | struct mmc_host *mmc = dev_get_drvdata(dev); |
| 1014 | struct mxcmci_host *host = mmc_priv(mmc); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1015 | int ret = 0; |
| 1016 | |
| 1017 | if (mmc) |
Matt Fleming | 1a13f8f | 2010-05-26 14:42:08 -0700 | [diff] [blame] | 1018 | ret = mmc_suspend_host(mmc); |
Eric Bénard | a7d403c | 2010-06-17 20:59:07 +0200 | [diff] [blame] | 1019 | clk_disable(host->clk); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1020 | |
| 1021 | return ret; |
| 1022 | } |
| 1023 | |
Eric Bénard | a7d403c | 2010-06-17 20:59:07 +0200 | [diff] [blame] | 1024 | static int mxcmci_resume(struct device *dev) |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1025 | { |
Eric Bénard | a7d403c | 2010-06-17 20:59:07 +0200 | [diff] [blame] | 1026 | struct mmc_host *mmc = dev_get_drvdata(dev); |
| 1027 | struct mxcmci_host *host = mmc_priv(mmc); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1028 | int ret = 0; |
| 1029 | |
Eric Bénard | a7d403c | 2010-06-17 20:59:07 +0200 | [diff] [blame] | 1030 | clk_enable(host->clk); |
| 1031 | if (mmc) |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1032 | ret = mmc_resume_host(mmc); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1033 | |
| 1034 | return ret; |
| 1035 | } |
Eric Bénard | a7d403c | 2010-06-17 20:59:07 +0200 | [diff] [blame] | 1036 | |
| 1037 | static const struct dev_pm_ops mxcmci_pm_ops = { |
| 1038 | .suspend = mxcmci_suspend, |
| 1039 | .resume = mxcmci_resume, |
| 1040 | }; |
| 1041 | #endif |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1042 | |
| 1043 | static struct platform_driver mxcmci_driver = { |
| 1044 | .probe = mxcmci_probe, |
| 1045 | .remove = mxcmci_remove, |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1046 | .driver = { |
| 1047 | .name = DRIVER_NAME, |
| 1048 | .owner = THIS_MODULE, |
Eric Bénard | a7d403c | 2010-06-17 20:59:07 +0200 | [diff] [blame] | 1049 | #ifdef CONFIG_PM |
| 1050 | .pm = &mxcmci_pm_ops, |
| 1051 | #endif |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1052 | } |
| 1053 | }; |
| 1054 | |
Axel Lin | d1f81a6 | 2011-11-26 12:55:43 +0800 | [diff] [blame] | 1055 | module_platform_driver(mxcmci_driver); |
Sascha Hauer | d96be87 | 2009-01-06 17:04:14 +0100 | [diff] [blame] | 1056 | |
| 1057 | MODULE_DESCRIPTION("i.MX Multimedia Card Interface Driver"); |
| 1058 | MODULE_AUTHOR("Sascha Hauer, Pengutronix"); |
| 1059 | MODULE_LICENSE("GPL"); |
| 1060 | MODULE_ALIAS("platform:imx-mmc"); |