Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Pierre Ossman | 70f1048 | 2007-07-11 20:04:50 +0200 | [diff] [blame] | 2 | * linux/drivers/mmc/host/pxa.c - PXA MMCI driver |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2003 Russell King, All Rights Reserved. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This hardware is really sick: |
| 11 | * - No way to clear interrupts. |
| 12 | * - Have to turn off the clock whenever we touch the device. |
| 13 | * - Doesn't tell you how many data blocks were transferred. |
| 14 | * Yuck! |
| 15 | * |
| 16 | * 1 and 3 byte data transfers not supported |
| 17 | * max block length up to 1023 |
| 18 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/module.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/ioport.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 22 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/delay.h> |
| 24 | #include <linux/interrupt.h> |
| 25 | #include <linux/dma-mapping.h> |
| 26 | #include <linux/mmc/host.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | #include <asm/dma.h> |
| 29 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <asm/scatterlist.h> |
| 31 | #include <asm/sizes.h> |
| 32 | |
| 33 | #include <asm/arch/pxa-regs.h> |
| 34 | #include <asm/arch/mmc.h> |
| 35 | |
| 36 | #include "pxamci.h" |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #define DRIVER_NAME "pxa2xx-mci" |
| 39 | |
| 40 | #define NR_SG 1 |
| 41 | |
| 42 | struct pxamci_host { |
| 43 | struct mmc_host *mmc; |
| 44 | spinlock_t lock; |
| 45 | struct resource *res; |
| 46 | void __iomem *base; |
| 47 | int irq; |
| 48 | int dma; |
| 49 | unsigned int clkrt; |
| 50 | unsigned int cmdat; |
| 51 | unsigned int imask; |
| 52 | unsigned int power_mode; |
| 53 | struct pxamci_platform_data *pdata; |
| 54 | |
| 55 | struct mmc_request *mrq; |
| 56 | struct mmc_command *cmd; |
| 57 | struct mmc_data *data; |
| 58 | |
| 59 | dma_addr_t sg_dma; |
| 60 | struct pxa_dma_desc *sg_cpu; |
| 61 | unsigned int dma_len; |
| 62 | |
| 63 | unsigned int dma_dir; |
| 64 | }; |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | static void pxamci_stop_clock(struct pxamci_host *host) |
| 67 | { |
| 68 | if (readl(host->base + MMC_STAT) & STAT_CLK_EN) { |
| 69 | unsigned long timeout = 10000; |
| 70 | unsigned int v; |
| 71 | |
| 72 | writel(STOP_CLOCK, host->base + MMC_STRPCL); |
| 73 | |
| 74 | do { |
| 75 | v = readl(host->base + MMC_STAT); |
| 76 | if (!(v & STAT_CLK_EN)) |
| 77 | break; |
| 78 | udelay(1); |
| 79 | } while (timeout--); |
| 80 | |
| 81 | if (v & STAT_CLK_EN) |
| 82 | dev_err(mmc_dev(host->mmc), "unable to stop clock\n"); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | static void pxamci_enable_irq(struct pxamci_host *host, unsigned int mask) |
| 87 | { |
| 88 | unsigned long flags; |
| 89 | |
| 90 | spin_lock_irqsave(&host->lock, flags); |
| 91 | host->imask &= ~mask; |
| 92 | writel(host->imask, host->base + MMC_I_MASK); |
| 93 | spin_unlock_irqrestore(&host->lock, flags); |
| 94 | } |
| 95 | |
| 96 | static void pxamci_disable_irq(struct pxamci_host *host, unsigned int mask) |
| 97 | { |
| 98 | unsigned long flags; |
| 99 | |
| 100 | spin_lock_irqsave(&host->lock, flags); |
| 101 | host->imask |= mask; |
| 102 | writel(host->imask, host->base + MMC_I_MASK); |
| 103 | spin_unlock_irqrestore(&host->lock, flags); |
| 104 | } |
| 105 | |
| 106 | static void pxamci_setup_data(struct pxamci_host *host, struct mmc_data *data) |
| 107 | { |
| 108 | unsigned int nob = data->blocks; |
Russell King | 3d63abe | 2006-04-24 11:27:02 +0100 | [diff] [blame] | 109 | unsigned long long clks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | unsigned int timeout; |
| 111 | u32 dcmd; |
| 112 | int i; |
| 113 | |
| 114 | host->data = data; |
| 115 | |
| 116 | if (data->flags & MMC_DATA_STREAM) |
| 117 | nob = 0xffff; |
| 118 | |
| 119 | writel(nob, host->base + MMC_NOB); |
Pavel Pisa | 2c171bf | 2006-05-19 21:48:03 +0100 | [diff] [blame] | 120 | writel(data->blksz, host->base + MMC_BLKLEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
Russell King | 3d63abe | 2006-04-24 11:27:02 +0100 | [diff] [blame] | 122 | clks = (unsigned long long)data->timeout_ns * CLOCKRATE; |
| 123 | do_div(clks, 1000000000UL); |
| 124 | timeout = (unsigned int)clks + (data->timeout_clks << host->clkrt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | writel((timeout + 255) / 256, host->base + MMC_RDTO); |
| 126 | |
| 127 | if (data->flags & MMC_DATA_READ) { |
| 128 | host->dma_dir = DMA_FROM_DEVICE; |
| 129 | dcmd = DCMD_INCTRGADDR | DCMD_FLOWTRG; |
| 130 | DRCMRTXMMC = 0; |
| 131 | DRCMRRXMMC = host->dma | DRCMR_MAPVLD; |
| 132 | } else { |
| 133 | host->dma_dir = DMA_TO_DEVICE; |
| 134 | dcmd = DCMD_INCSRCADDR | DCMD_FLOWSRC; |
| 135 | DRCMRRXMMC = 0; |
| 136 | DRCMRTXMMC = host->dma | DRCMR_MAPVLD; |
| 137 | } |
| 138 | |
| 139 | dcmd |= DCMD_BURST32 | DCMD_WIDTH1; |
| 140 | |
| 141 | host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len, |
| 142 | host->dma_dir); |
| 143 | |
| 144 | for (i = 0; i < host->dma_len; i++) { |
| 145 | if (data->flags & MMC_DATA_READ) { |
| 146 | host->sg_cpu[i].dsadr = host->res->start + MMC_RXFIFO; |
| 147 | host->sg_cpu[i].dtadr = sg_dma_address(&data->sg[i]); |
| 148 | } else { |
| 149 | host->sg_cpu[i].dsadr = sg_dma_address(&data->sg[i]); |
| 150 | host->sg_cpu[i].dtadr = host->res->start + MMC_TXFIFO; |
| 151 | } |
| 152 | host->sg_cpu[i].dcmd = dcmd | sg_dma_len(&data->sg[i]); |
| 153 | host->sg_cpu[i].ddadr = host->sg_dma + (i + 1) * |
| 154 | sizeof(struct pxa_dma_desc); |
| 155 | } |
| 156 | host->sg_cpu[host->dma_len - 1].ddadr = DDADR_STOP; |
| 157 | wmb(); |
| 158 | |
| 159 | DDADR(host->dma) = host->sg_dma; |
| 160 | DCSR(host->dma) = DCSR_RUN; |
| 161 | } |
| 162 | |
| 163 | static void pxamci_start_cmd(struct pxamci_host *host, struct mmc_command *cmd, unsigned int cmdat) |
| 164 | { |
| 165 | WARN_ON(host->cmd != NULL); |
| 166 | host->cmd = cmd; |
| 167 | |
| 168 | if (cmd->flags & MMC_RSP_BUSY) |
| 169 | cmdat |= CMDAT_BUSY; |
| 170 | |
Russell King | e922517 | 2006-02-02 12:23:12 +0000 | [diff] [blame] | 171 | #define RSP_TYPE(x) ((x) & ~(MMC_RSP_BUSY|MMC_RSP_OPCODE)) |
| 172 | switch (RSP_TYPE(mmc_resp_type(cmd))) { |
Philip Langdale | 6f94990 | 2007-01-04 07:04:47 -0800 | [diff] [blame] | 173 | case RSP_TYPE(MMC_RSP_R1): /* r1, r1b, r6, r7 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | cmdat |= CMDAT_RESP_SHORT; |
| 175 | break; |
Russell King | e922517 | 2006-02-02 12:23:12 +0000 | [diff] [blame] | 176 | case RSP_TYPE(MMC_RSP_R3): |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | cmdat |= CMDAT_RESP_R3; |
| 178 | break; |
Russell King | e922517 | 2006-02-02 12:23:12 +0000 | [diff] [blame] | 179 | case RSP_TYPE(MMC_RSP_R2): |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | cmdat |= CMDAT_RESP_R2; |
| 181 | break; |
| 182 | default: |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | writel(cmd->opcode, host->base + MMC_CMD); |
| 187 | writel(cmd->arg >> 16, host->base + MMC_ARGH); |
| 188 | writel(cmd->arg & 0xffff, host->base + MMC_ARGL); |
| 189 | writel(cmdat, host->base + MMC_CMDAT); |
| 190 | writel(host->clkrt, host->base + MMC_CLKRT); |
| 191 | |
| 192 | writel(START_CLOCK, host->base + MMC_STRPCL); |
| 193 | |
| 194 | pxamci_enable_irq(host, END_CMD_RES); |
| 195 | } |
| 196 | |
| 197 | static void pxamci_finish_request(struct pxamci_host *host, struct mmc_request *mrq) |
| 198 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | host->mrq = NULL; |
| 200 | host->cmd = NULL; |
| 201 | host->data = NULL; |
| 202 | mmc_request_done(host->mmc, mrq); |
| 203 | } |
| 204 | |
| 205 | static int pxamci_cmd_done(struct pxamci_host *host, unsigned int stat) |
| 206 | { |
| 207 | struct mmc_command *cmd = host->cmd; |
| 208 | int i; |
| 209 | u32 v; |
| 210 | |
| 211 | if (!cmd) |
| 212 | return 0; |
| 213 | |
| 214 | host->cmd = NULL; |
| 215 | |
| 216 | /* |
| 217 | * Did I mention this is Sick. We always need to |
| 218 | * discard the upper 8 bits of the first 16-bit word. |
| 219 | */ |
| 220 | v = readl(host->base + MMC_RES) & 0xffff; |
| 221 | for (i = 0; i < 4; i++) { |
| 222 | u32 w1 = readl(host->base + MMC_RES) & 0xffff; |
| 223 | u32 w2 = readl(host->base + MMC_RES) & 0xffff; |
| 224 | cmd->resp[i] = v << 24 | w1 << 8 | w2 >> 8; |
| 225 | v = w2; |
| 226 | } |
| 227 | |
| 228 | if (stat & STAT_TIME_OUT_RESPONSE) { |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 229 | cmd->error = -ETIMEDOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | } else if (stat & STAT_RES_CRC_ERR && cmd->flags & MMC_RSP_CRC) { |
| 231 | #ifdef CONFIG_PXA27x |
| 232 | /* |
| 233 | * workaround for erratum #42: |
| 234 | * Intel PXA27x Family Processor Specification Update Rev 001 |
Nicolas Pitre | 90e07d9 | 2007-05-13 18:03:08 +0200 | [diff] [blame] | 235 | * A bogus CRC error can appear if the msb of a 136 bit |
| 236 | * response is a one. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | */ |
Nicolas Pitre | 90e07d9 | 2007-05-13 18:03:08 +0200 | [diff] [blame] | 238 | if (cmd->flags & MMC_RSP_136 && cmd->resp[0] & 0x80000000) { |
| 239 | pr_debug("ignoring CRC from command %d - *risky*\n", cmd->opcode); |
| 240 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | #endif |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 242 | cmd->error = -EILSEQ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | pxamci_disable_irq(host, END_CMD_RES); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 246 | if (host->data && !cmd->error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | pxamci_enable_irq(host, DATA_TRAN_DONE); |
| 248 | } else { |
| 249 | pxamci_finish_request(host, host->mrq); |
| 250 | } |
| 251 | |
| 252 | return 1; |
| 253 | } |
| 254 | |
| 255 | static int pxamci_data_done(struct pxamci_host *host, unsigned int stat) |
| 256 | { |
| 257 | struct mmc_data *data = host->data; |
| 258 | |
| 259 | if (!data) |
| 260 | return 0; |
| 261 | |
| 262 | DCSR(host->dma) = 0; |
| 263 | dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len, |
| 264 | host->dma_dir); |
| 265 | |
| 266 | if (stat & STAT_READ_TIME_OUT) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 267 | data->error = -ETIMEDOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | else if (stat & (STAT_CRC_READ_ERROR|STAT_CRC_WRITE_ERROR)) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 269 | data->error = -EILSEQ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
| 271 | /* |
| 272 | * There appears to be a hardware design bug here. There seems to |
| 273 | * be no way to find out how much data was transferred to the card. |
| 274 | * This means that if there was an error on any block, we mark all |
| 275 | * data blocks as being in error. |
| 276 | */ |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 277 | if (!data->error) |
Pavel Pisa | 2c171bf | 2006-05-19 21:48:03 +0100 | [diff] [blame] | 278 | data->bytes_xfered = data->blocks * data->blksz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | else |
| 280 | data->bytes_xfered = 0; |
| 281 | |
| 282 | pxamci_disable_irq(host, DATA_TRAN_DONE); |
| 283 | |
| 284 | host->data = NULL; |
Russell King | 58741e8 | 2006-05-02 20:02:39 +0100 | [diff] [blame] | 285 | if (host->mrq->stop) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | pxamci_stop_clock(host); |
Bridge Wu | df456f4 | 2007-09-25 19:09:19 +0200 | [diff] [blame] | 287 | pxamci_start_cmd(host, host->mrq->stop, host->cmdat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | } else { |
| 289 | pxamci_finish_request(host, host->mrq); |
| 290 | } |
| 291 | |
| 292 | return 1; |
| 293 | } |
| 294 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 295 | static irqreturn_t pxamci_irq(int irq, void *devid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | { |
| 297 | struct pxamci_host *host = devid; |
| 298 | unsigned int ireg; |
| 299 | int handled = 0; |
| 300 | |
Bridge Wu | 81ab570f | 2007-09-25 18:59:07 +0200 | [diff] [blame] | 301 | ireg = readl(host->base + MMC_I_REG) & ~readl(host->base + MMC_I_MASK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | if (ireg) { |
| 304 | unsigned stat = readl(host->base + MMC_STAT); |
| 305 | |
Russell King | d78e907 | 2006-05-02 20:18:53 +0100 | [diff] [blame] | 306 | pr_debug("PXAMCI: irq %08x stat %08x\n", ireg, stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
| 308 | if (ireg & END_CMD_RES) |
| 309 | handled |= pxamci_cmd_done(host, stat); |
| 310 | if (ireg & DATA_TRAN_DONE) |
| 311 | handled |= pxamci_data_done(host, stat); |
Bridge Wu | 5d3ad4e | 2007-09-25 19:11:00 +0200 | [diff] [blame^] | 312 | if (ireg & SDIO_INT) { |
| 313 | mmc_signal_sdio_irq(host->mmc); |
| 314 | handled = 1; |
| 315 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | return IRQ_RETVAL(handled); |
| 319 | } |
| 320 | |
| 321 | static void pxamci_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 322 | { |
| 323 | struct pxamci_host *host = mmc_priv(mmc); |
| 324 | unsigned int cmdat; |
| 325 | |
| 326 | WARN_ON(host->mrq != NULL); |
| 327 | |
| 328 | host->mrq = mrq; |
| 329 | |
| 330 | pxamci_stop_clock(host); |
| 331 | |
| 332 | cmdat = host->cmdat; |
| 333 | host->cmdat &= ~CMDAT_INIT; |
| 334 | |
| 335 | if (mrq->data) { |
| 336 | pxamci_setup_data(host, mrq->data); |
| 337 | |
| 338 | cmdat &= ~CMDAT_BUSY; |
| 339 | cmdat |= CMDAT_DATAEN | CMDAT_DMAEN; |
| 340 | if (mrq->data->flags & MMC_DATA_WRITE) |
| 341 | cmdat |= CMDAT_WRITE; |
| 342 | |
| 343 | if (mrq->data->flags & MMC_DATA_STREAM) |
| 344 | cmdat |= CMDAT_STREAM; |
| 345 | } |
| 346 | |
| 347 | pxamci_start_cmd(host, mrq->cmd, cmdat); |
| 348 | } |
| 349 | |
Richard Purdie | e619524 | 2005-09-06 15:18:56 -0700 | [diff] [blame] | 350 | static int pxamci_get_ro(struct mmc_host *mmc) |
| 351 | { |
| 352 | struct pxamci_host *host = mmc_priv(mmc); |
| 353 | |
| 354 | if (host->pdata && host->pdata->get_ro) |
Sascha Hauer | 9e86619 | 2006-12-05 07:41:09 +0100 | [diff] [blame] | 355 | return host->pdata->get_ro(mmc_dev(mmc)); |
Richard Purdie | e619524 | 2005-09-06 15:18:56 -0700 | [diff] [blame] | 356 | /* Host doesn't support read only detection so assume writeable */ |
| 357 | return 0; |
| 358 | } |
| 359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | static void pxamci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 361 | { |
| 362 | struct pxamci_host *host = mmc_priv(mmc); |
| 363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | if (ios->clock) { |
| 365 | unsigned int clk = CLOCKRATE / ios->clock; |
| 366 | if (CLOCKRATE / clk > ios->clock) |
| 367 | clk <<= 1; |
| 368 | host->clkrt = fls(clk) - 1; |
Eric Miao | 7053acb | 2007-04-05 04:07:20 +0100 | [diff] [blame] | 369 | pxa_set_cken(CKEN_MMC, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
| 371 | /* |
| 372 | * we write clkrt on the next command |
| 373 | */ |
| 374 | } else { |
| 375 | pxamci_stop_clock(host); |
Eric Miao | 7053acb | 2007-04-05 04:07:20 +0100 | [diff] [blame] | 376 | pxa_set_cken(CKEN_MMC, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | if (host->power_mode != ios->power_mode) { |
| 380 | host->power_mode = ios->power_mode; |
| 381 | |
| 382 | if (host->pdata && host->pdata->setpower) |
Sascha Hauer | 9e86619 | 2006-12-05 07:41:09 +0100 | [diff] [blame] | 383 | host->pdata->setpower(mmc_dev(mmc), ios->vdd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | |
| 385 | if (ios->power_mode == MMC_POWER_ON) |
| 386 | host->cmdat |= CMDAT_INIT; |
| 387 | } |
| 388 | |
Bridge Wu | df456f4 | 2007-09-25 19:09:19 +0200 | [diff] [blame] | 389 | if (ios->bus_width == MMC_BUS_WIDTH_4) |
| 390 | host->cmdat |= CMDAT_SD_4DAT; |
| 391 | else |
| 392 | host->cmdat &= ~CMDAT_SD_4DAT; |
| 393 | |
Russell King | d78e907 | 2006-05-02 20:18:53 +0100 | [diff] [blame] | 394 | pr_debug("PXAMCI: clkrt = %x cmdat = %x\n", |
Russell King | c656317 | 2006-03-29 09:30:20 +0100 | [diff] [blame] | 395 | host->clkrt, host->cmdat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Bridge Wu | 5d3ad4e | 2007-09-25 19:11:00 +0200 | [diff] [blame^] | 398 | static void pxamci_enable_sdio_irq(struct mmc_host *host, int enable) |
| 399 | { |
| 400 | struct pxamci_host *pxa_host = mmc_priv(host); |
| 401 | |
| 402 | if (enable) |
| 403 | pxamci_enable_irq(pxa_host, SDIO_INT); |
| 404 | else |
| 405 | pxamci_disable_irq(pxa_host, SDIO_INT); |
| 406 | } |
| 407 | |
David Brownell | ab7aefd | 2006-11-12 17:55:30 -0800 | [diff] [blame] | 408 | static const struct mmc_host_ops pxamci_ops = { |
Bridge Wu | 5d3ad4e | 2007-09-25 19:11:00 +0200 | [diff] [blame^] | 409 | .request = pxamci_request, |
| 410 | .get_ro = pxamci_get_ro, |
| 411 | .set_ios = pxamci_set_ios, |
| 412 | .enable_sdio_irq = pxamci_enable_sdio_irq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | }; |
| 414 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 415 | static void pxamci_dma_irq(int dma, void *devid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | { |
| 417 | printk(KERN_ERR "DMA%d: IRQ???\n", dma); |
| 418 | DCSR(dma) = DCSR_STARTINTR|DCSR_ENDINTR|DCSR_BUSERR; |
| 419 | } |
| 420 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 421 | static irqreturn_t pxamci_detect_irq(int irq, void *devid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | { |
Richard Purdie | c26971c | 2005-09-08 22:48:16 +0100 | [diff] [blame] | 423 | struct pxamci_host *host = mmc_priv(devid); |
| 424 | |
| 425 | mmc_detect_change(devid, host->pdata->detect_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | return IRQ_HANDLED; |
| 427 | } |
| 428 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 429 | static int pxamci_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | struct mmc_host *mmc; |
| 432 | struct pxamci_host *host = NULL; |
| 433 | struct resource *r; |
| 434 | int ret, irq; |
| 435 | |
| 436 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 437 | irq = platform_get_irq(pdev, 0); |
David Vrabel | 4894473 | 2006-01-19 17:56:29 +0000 | [diff] [blame] | 438 | if (!r || irq < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | return -ENXIO; |
| 440 | |
| 441 | r = request_mem_region(r->start, SZ_4K, DRIVER_NAME); |
| 442 | if (!r) |
| 443 | return -EBUSY; |
| 444 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 445 | mmc = mmc_alloc_host(sizeof(struct pxamci_host), &pdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | if (!mmc) { |
| 447 | ret = -ENOMEM; |
| 448 | goto out; |
| 449 | } |
| 450 | |
| 451 | mmc->ops = &pxamci_ops; |
| 452 | mmc->f_min = CLOCKRATE_MIN; |
| 453 | mmc->f_max = CLOCKRATE_MAX; |
| 454 | |
| 455 | /* |
| 456 | * We can do SG-DMA, but we don't because we never know how much |
| 457 | * data we successfully wrote to the card. |
| 458 | */ |
| 459 | mmc->max_phys_segs = NR_SG; |
| 460 | |
| 461 | /* |
| 462 | * Our hardware DMA can handle a maximum of one page per SG entry. |
| 463 | */ |
| 464 | mmc->max_seg_size = PAGE_SIZE; |
| 465 | |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 466 | /* |
Nicolas Pitre | fe2dc44 | 2007-09-24 15:47:18 -0400 | [diff] [blame] | 467 | * Block length register is only 10 bits before PXA27x. |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 468 | */ |
Nicolas Pitre | fe2dc44 | 2007-09-24 15:47:18 -0400 | [diff] [blame] | 469 | mmc->max_blk_size = (cpu_is_pxa21x() || cpu_is_pxa25x()) ? 1023 : 2048; |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 470 | |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 471 | /* |
| 472 | * Block count register is 16 bits. |
| 473 | */ |
| 474 | mmc->max_blk_count = 65535; |
| 475 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | host = mmc_priv(mmc); |
| 477 | host->mmc = mmc; |
| 478 | host->dma = -1; |
| 479 | host->pdata = pdev->dev.platform_data; |
| 480 | mmc->ocr_avail = host->pdata ? |
| 481 | host->pdata->ocr_mask : |
| 482 | MMC_VDD_32_33|MMC_VDD_33_34; |
Bridge Wu | df456f4 | 2007-09-25 19:09:19 +0200 | [diff] [blame] | 483 | mmc->caps = 0; |
Bridge Wu | 5d3ad4e | 2007-09-25 19:11:00 +0200 | [diff] [blame^] | 484 | host->cmdat = 0; |
| 485 | if (!cpu_is_pxa21x() && !cpu_is_pxa25x()) { |
| 486 | mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ; |
| 487 | host->cmdat |= CMDAT_SDIO_INT_EN; |
| 488 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 490 | host->sg_cpu = dma_alloc_coherent(&pdev->dev, PAGE_SIZE, &host->sg_dma, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | if (!host->sg_cpu) { |
| 492 | ret = -ENOMEM; |
| 493 | goto out; |
| 494 | } |
| 495 | |
| 496 | spin_lock_init(&host->lock); |
| 497 | host->res = r; |
| 498 | host->irq = irq; |
| 499 | host->imask = MMC_I_MASK_ALL; |
| 500 | |
| 501 | host->base = ioremap(r->start, SZ_4K); |
| 502 | if (!host->base) { |
| 503 | ret = -ENOMEM; |
| 504 | goto out; |
| 505 | } |
| 506 | |
| 507 | /* |
| 508 | * Ensure that the host controller is shut down, and setup |
| 509 | * with our defaults. |
| 510 | */ |
| 511 | pxamci_stop_clock(host); |
| 512 | writel(0, host->base + MMC_SPI); |
| 513 | writel(64, host->base + MMC_RESTO); |
| 514 | writel(host->imask, host->base + MMC_I_MASK); |
| 515 | |
| 516 | host->dma = pxa_request_dma(DRIVER_NAME, DMA_PRIO_LOW, |
| 517 | pxamci_dma_irq, host); |
| 518 | if (host->dma < 0) { |
| 519 | ret = -EBUSY; |
| 520 | goto out; |
| 521 | } |
| 522 | |
| 523 | ret = request_irq(host->irq, pxamci_irq, 0, DRIVER_NAME, host); |
| 524 | if (ret) |
| 525 | goto out; |
| 526 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 527 | platform_set_drvdata(pdev, mmc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | |
| 529 | if (host->pdata && host->pdata->init) |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 530 | host->pdata->init(&pdev->dev, pxamci_detect_irq, mmc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | |
| 532 | mmc_add_host(mmc); |
| 533 | |
| 534 | return 0; |
| 535 | |
| 536 | out: |
| 537 | if (host) { |
| 538 | if (host->dma >= 0) |
| 539 | pxa_free_dma(host->dma); |
| 540 | if (host->base) |
| 541 | iounmap(host->base); |
| 542 | if (host->sg_cpu) |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 543 | dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | } |
| 545 | if (mmc) |
| 546 | mmc_free_host(mmc); |
| 547 | release_resource(r); |
| 548 | return ret; |
| 549 | } |
| 550 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 551 | static int pxamci_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 553 | struct mmc_host *mmc = platform_get_drvdata(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 555 | platform_set_drvdata(pdev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
| 557 | if (mmc) { |
| 558 | struct pxamci_host *host = mmc_priv(mmc); |
| 559 | |
| 560 | if (host->pdata && host->pdata->exit) |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 561 | host->pdata->exit(&pdev->dev, mmc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | |
| 563 | mmc_remove_host(mmc); |
| 564 | |
| 565 | pxamci_stop_clock(host); |
| 566 | writel(TXFIFO_WR_REQ|RXFIFO_RD_REQ|CLK_IS_OFF|STOP_CMD| |
| 567 | END_CMD_RES|PRG_DONE|DATA_TRAN_DONE, |
| 568 | host->base + MMC_I_MASK); |
| 569 | |
| 570 | DRCMRRXMMC = 0; |
| 571 | DRCMRTXMMC = 0; |
| 572 | |
| 573 | free_irq(host->irq, host); |
| 574 | pxa_free_dma(host->dma); |
| 575 | iounmap(host->base); |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 576 | dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | |
| 578 | release_resource(host->res); |
| 579 | |
| 580 | mmc_free_host(mmc); |
| 581 | } |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | #ifdef CONFIG_PM |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 586 | static int pxamci_suspend(struct platform_device *dev, pm_message_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 588 | struct mmc_host *mmc = platform_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | int ret = 0; |
| 590 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 591 | if (mmc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | ret = mmc_suspend_host(mmc, state); |
| 593 | |
| 594 | return ret; |
| 595 | } |
| 596 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 597 | static int pxamci_resume(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 599 | struct mmc_host *mmc = platform_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | int ret = 0; |
| 601 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 602 | if (mmc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | ret = mmc_resume_host(mmc); |
| 604 | |
| 605 | return ret; |
| 606 | } |
| 607 | #else |
| 608 | #define pxamci_suspend NULL |
| 609 | #define pxamci_resume NULL |
| 610 | #endif |
| 611 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 612 | static struct platform_driver pxamci_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | .probe = pxamci_probe, |
| 614 | .remove = pxamci_remove, |
| 615 | .suspend = pxamci_suspend, |
| 616 | .resume = pxamci_resume, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 617 | .driver = { |
| 618 | .name = DRIVER_NAME, |
| 619 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | }; |
| 621 | |
| 622 | static int __init pxamci_init(void) |
| 623 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 624 | return platform_driver_register(&pxamci_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | static void __exit pxamci_exit(void) |
| 628 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 629 | platform_driver_unregister(&pxamci_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | module_init(pxamci_init); |
| 633 | module_exit(pxamci_exit); |
| 634 | |
| 635 | MODULE_DESCRIPTION("PXA Multimedia Card Interface Driver"); |
| 636 | MODULE_LICENSE("GPL"); |