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> |
Russell King | ebebd9b | 2007-08-20 10:20:03 +0100 | [diff] [blame] | 26 | #include <linux/clk.h> |
| 27 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/mmc/host.h> |
Russell King | 05678a9 | 2008-11-28 16:04:54 +0000 | [diff] [blame] | 29 | #include <linux/io.h> |
Daniel Ribeiro | 8385f9c | 2009-05-21 08:54:18 -0300 | [diff] [blame] | 30 | #include <linux/regulator/consumer.h> |
Robert Jarzmik | b405db6 | 2009-06-23 23:21:03 +0200 | [diff] [blame] | 31 | #include <linux/gpio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <asm/sizes.h> |
| 34 | |
Russell King | 05678a9 | 2008-11-28 16:04:54 +0000 | [diff] [blame] | 35 | #include <mach/hardware.h> |
Eric Miao | 7ebc8d5 | 2009-01-02 19:38:42 +0800 | [diff] [blame] | 36 | #include <mach/dma.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 37 | #include <mach/mmc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | #include "pxamci.h" |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #define DRIVER_NAME "pxa2xx-mci" |
| 42 | |
| 43 | #define NR_SG 1 |
Russell King | d8cb70d | 2007-10-26 17:56:40 +0100 | [diff] [blame] | 44 | #define CLKRT_OFF (~0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Haojian Zhuang | fa3f993 | 2009-08-31 21:52:53 +0800 | [diff] [blame] | 46 | #define mmc_has_26MHz() (cpu_is_pxa300() || cpu_is_pxa310() \ |
| 47 | || cpu_is_pxa935()) |
| 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | struct pxamci_host { |
| 50 | struct mmc_host *mmc; |
| 51 | spinlock_t lock; |
| 52 | struct resource *res; |
| 53 | void __iomem *base; |
Russell King | ebebd9b | 2007-08-20 10:20:03 +0100 | [diff] [blame] | 54 | struct clk *clk; |
| 55 | unsigned long clkrate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | int irq; |
| 57 | int dma; |
| 58 | unsigned int clkrt; |
| 59 | unsigned int cmdat; |
| 60 | unsigned int imask; |
| 61 | unsigned int power_mode; |
| 62 | struct pxamci_platform_data *pdata; |
| 63 | |
| 64 | struct mmc_request *mrq; |
| 65 | struct mmc_command *cmd; |
| 66 | struct mmc_data *data; |
| 67 | |
| 68 | dma_addr_t sg_dma; |
| 69 | struct pxa_dma_desc *sg_cpu; |
| 70 | unsigned int dma_len; |
| 71 | |
| 72 | unsigned int dma_dir; |
Bridge Wu | 9a788c6 | 2007-12-14 10:40:25 +0100 | [diff] [blame] | 73 | unsigned int dma_drcmrrx; |
| 74 | unsigned int dma_drcmrtx; |
Daniel Ribeiro | 8385f9c | 2009-05-21 08:54:18 -0300 | [diff] [blame] | 75 | |
| 76 | struct regulator *vcc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Daniel Ribeiro | 8385f9c | 2009-05-21 08:54:18 -0300 | [diff] [blame] | 79 | static inline void pxamci_init_ocr(struct pxamci_host *host) |
| 80 | { |
| 81 | #ifdef CONFIG_REGULATOR |
| 82 | host->vcc = regulator_get(mmc_dev(host->mmc), "vmmc"); |
| 83 | |
| 84 | if (IS_ERR(host->vcc)) |
| 85 | host->vcc = NULL; |
| 86 | else { |
| 87 | host->mmc->ocr_avail = mmc_regulator_get_ocrmask(host->vcc); |
| 88 | if (host->pdata && host->pdata->ocr_mask) |
| 89 | dev_warn(mmc_dev(host->mmc), |
| 90 | "ocr_mask/setpower will not be used\n"); |
| 91 | } |
| 92 | #endif |
| 93 | if (host->vcc == NULL) { |
| 94 | /* fall-back to platform data */ |
| 95 | host->mmc->ocr_avail = host->pdata ? |
| 96 | host->pdata->ocr_mask : |
| 97 | MMC_VDD_32_33 | MMC_VDD_33_34; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | static inline void pxamci_set_power(struct pxamci_host *host, unsigned int vdd) |
| 102 | { |
Robert Jarzmik | b405db6 | 2009-06-23 23:21:03 +0200 | [diff] [blame] | 103 | int on; |
| 104 | |
Daniel Ribeiro | 8385f9c | 2009-05-21 08:54:18 -0300 | [diff] [blame] | 105 | #ifdef CONFIG_REGULATOR |
| 106 | if (host->vcc) |
| 107 | mmc_regulator_set_ocr(host->vcc, vdd); |
| 108 | #endif |
Robert Jarzmik | b405db6 | 2009-06-23 23:21:03 +0200 | [diff] [blame] | 109 | if (!host->vcc && host->pdata && |
| 110 | gpio_is_valid(host->pdata->gpio_power)) { |
| 111 | on = ((1 << vdd) & host->pdata->ocr_mask); |
| 112 | gpio_set_value(host->pdata->gpio_power, |
| 113 | !!on ^ host->pdata->gpio_power_invert); |
| 114 | } |
Daniel Ribeiro | 8385f9c | 2009-05-21 08:54:18 -0300 | [diff] [blame] | 115 | if (!host->vcc && host->pdata && host->pdata->setpower) |
| 116 | host->pdata->setpower(mmc_dev(host->mmc), vdd); |
| 117 | } |
| 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | static void pxamci_stop_clock(struct pxamci_host *host) |
| 120 | { |
| 121 | if (readl(host->base + MMC_STAT) & STAT_CLK_EN) { |
| 122 | unsigned long timeout = 10000; |
| 123 | unsigned int v; |
| 124 | |
| 125 | writel(STOP_CLOCK, host->base + MMC_STRPCL); |
| 126 | |
| 127 | do { |
| 128 | v = readl(host->base + MMC_STAT); |
| 129 | if (!(v & STAT_CLK_EN)) |
| 130 | break; |
| 131 | udelay(1); |
| 132 | } while (timeout--); |
| 133 | |
| 134 | if (v & STAT_CLK_EN) |
| 135 | dev_err(mmc_dev(host->mmc), "unable to stop clock\n"); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | static void pxamci_enable_irq(struct pxamci_host *host, unsigned int mask) |
| 140 | { |
| 141 | unsigned long flags; |
| 142 | |
| 143 | spin_lock_irqsave(&host->lock, flags); |
| 144 | host->imask &= ~mask; |
| 145 | writel(host->imask, host->base + MMC_I_MASK); |
| 146 | spin_unlock_irqrestore(&host->lock, flags); |
| 147 | } |
| 148 | |
| 149 | static void pxamci_disable_irq(struct pxamci_host *host, unsigned int mask) |
| 150 | { |
| 151 | unsigned long flags; |
| 152 | |
| 153 | spin_lock_irqsave(&host->lock, flags); |
| 154 | host->imask |= mask; |
| 155 | writel(host->imask, host->base + MMC_I_MASK); |
| 156 | spin_unlock_irqrestore(&host->lock, flags); |
| 157 | } |
| 158 | |
| 159 | static void pxamci_setup_data(struct pxamci_host *host, struct mmc_data *data) |
| 160 | { |
| 161 | unsigned int nob = data->blocks; |
Russell King | 3d63abe | 2006-04-24 11:27:02 +0100 | [diff] [blame] | 162 | unsigned long long clks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | unsigned int timeout; |
Philipp Zabel | 97f8571 | 2008-07-06 01:15:34 +0200 | [diff] [blame] | 164 | bool dalgn = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | u32 dcmd; |
| 166 | int i; |
| 167 | |
| 168 | host->data = data; |
| 169 | |
| 170 | if (data->flags & MMC_DATA_STREAM) |
| 171 | nob = 0xffff; |
| 172 | |
| 173 | writel(nob, host->base + MMC_NOB); |
Pavel Pisa | 2c171bf | 2006-05-19 21:48:03 +0100 | [diff] [blame] | 174 | writel(data->blksz, host->base + MMC_BLKLEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
Russell King | ebebd9b | 2007-08-20 10:20:03 +0100 | [diff] [blame] | 176 | clks = (unsigned long long)data->timeout_ns * host->clkrate; |
Russell King | 3d63abe | 2006-04-24 11:27:02 +0100 | [diff] [blame] | 177 | do_div(clks, 1000000000UL); |
| 178 | timeout = (unsigned int)clks + (data->timeout_clks << host->clkrt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | writel((timeout + 255) / 256, host->base + MMC_RDTO); |
| 180 | |
| 181 | if (data->flags & MMC_DATA_READ) { |
| 182 | host->dma_dir = DMA_FROM_DEVICE; |
Matt Reimer | 7eeff48 | 2009-06-15 13:21:25 -0700 | [diff] [blame] | 183 | dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC; |
Bridge Wu | 9a788c6 | 2007-12-14 10:40:25 +0100 | [diff] [blame] | 184 | DRCMR(host->dma_drcmrtx) = 0; |
| 185 | DRCMR(host->dma_drcmrrx) = host->dma | DRCMR_MAPVLD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } else { |
| 187 | host->dma_dir = DMA_TO_DEVICE; |
Matt Reimer | 7eeff48 | 2009-06-15 13:21:25 -0700 | [diff] [blame] | 188 | dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG; |
Bridge Wu | 9a788c6 | 2007-12-14 10:40:25 +0100 | [diff] [blame] | 189 | DRCMR(host->dma_drcmrrx) = 0; |
| 190 | DRCMR(host->dma_drcmrtx) = host->dma | DRCMR_MAPVLD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | dcmd |= DCMD_BURST32 | DCMD_WIDTH1; |
| 194 | |
| 195 | host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len, |
| 196 | host->dma_dir); |
| 197 | |
| 198 | for (i = 0; i < host->dma_len; i++) { |
Nicolas Pitre | c783837 | 2007-10-09 17:07:58 -0400 | [diff] [blame] | 199 | unsigned int length = sg_dma_len(&data->sg[i]); |
| 200 | host->sg_cpu[i].dcmd = dcmd | length; |
| 201 | if (length & 31 && !(data->flags & MMC_DATA_READ)) |
| 202 | host->sg_cpu[i].dcmd |= DCMD_ENDIRQEN; |
Philipp Zabel | 97f8571 | 2008-07-06 01:15:34 +0200 | [diff] [blame] | 203 | /* Not aligned to 8-byte boundary? */ |
| 204 | if (sg_dma_address(&data->sg[i]) & 0x7) |
| 205 | dalgn = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | if (data->flags & MMC_DATA_READ) { |
| 207 | host->sg_cpu[i].dsadr = host->res->start + MMC_RXFIFO; |
| 208 | host->sg_cpu[i].dtadr = sg_dma_address(&data->sg[i]); |
| 209 | } else { |
| 210 | host->sg_cpu[i].dsadr = sg_dma_address(&data->sg[i]); |
| 211 | host->sg_cpu[i].dtadr = host->res->start + MMC_TXFIFO; |
| 212 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | host->sg_cpu[i].ddadr = host->sg_dma + (i + 1) * |
| 214 | sizeof(struct pxa_dma_desc); |
| 215 | } |
| 216 | host->sg_cpu[host->dma_len - 1].ddadr = DDADR_STOP; |
| 217 | wmb(); |
| 218 | |
Philipp Zabel | 97f8571 | 2008-07-06 01:15:34 +0200 | [diff] [blame] | 219 | /* |
| 220 | * The PXA27x DMA controller encounters overhead when working with |
| 221 | * unaligned (to 8-byte boundaries) data, so switch on byte alignment |
| 222 | * mode only if we have unaligned data. |
| 223 | */ |
| 224 | if (dalgn) |
| 225 | DALGN |= (1 << host->dma); |
| 226 | else |
Karl Beldan | 4fe1689 | 2008-07-16 18:29:11 +0200 | [diff] [blame] | 227 | DALGN &= ~(1 << host->dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | DDADR(host->dma) = host->sg_dma; |
Cliff Brake | b601895 | 2009-01-22 17:07:03 -0500 | [diff] [blame] | 229 | |
| 230 | /* |
| 231 | * workaround for erratum #91: |
| 232 | * only start DMA now if we are doing a read, |
| 233 | * otherwise we wait until CMD/RESP has finished |
| 234 | * before starting DMA. |
| 235 | */ |
| 236 | if (!cpu_is_pxa27x() || data->flags & MMC_DATA_READ) |
| 237 | DCSR(host->dma) = DCSR_RUN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static void pxamci_start_cmd(struct pxamci_host *host, struct mmc_command *cmd, unsigned int cmdat) |
| 241 | { |
| 242 | WARN_ON(host->cmd != NULL); |
| 243 | host->cmd = cmd; |
| 244 | |
| 245 | if (cmd->flags & MMC_RSP_BUSY) |
| 246 | cmdat |= CMDAT_BUSY; |
| 247 | |
Russell King | e922517 | 2006-02-02 12:23:12 +0000 | [diff] [blame] | 248 | #define RSP_TYPE(x) ((x) & ~(MMC_RSP_BUSY|MMC_RSP_OPCODE)) |
| 249 | switch (RSP_TYPE(mmc_resp_type(cmd))) { |
Philip Langdale | 6f94990 | 2007-01-04 07:04:47 -0800 | [diff] [blame] | 250 | case RSP_TYPE(MMC_RSP_R1): /* r1, r1b, r6, r7 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | cmdat |= CMDAT_RESP_SHORT; |
| 252 | break; |
Russell King | e922517 | 2006-02-02 12:23:12 +0000 | [diff] [blame] | 253 | case RSP_TYPE(MMC_RSP_R3): |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | cmdat |= CMDAT_RESP_R3; |
| 255 | break; |
Russell King | e922517 | 2006-02-02 12:23:12 +0000 | [diff] [blame] | 256 | case RSP_TYPE(MMC_RSP_R2): |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | cmdat |= CMDAT_RESP_R2; |
| 258 | break; |
| 259 | default: |
| 260 | break; |
| 261 | } |
| 262 | |
| 263 | writel(cmd->opcode, host->base + MMC_CMD); |
| 264 | writel(cmd->arg >> 16, host->base + MMC_ARGH); |
| 265 | writel(cmd->arg & 0xffff, host->base + MMC_ARGL); |
| 266 | writel(cmdat, host->base + MMC_CMDAT); |
| 267 | writel(host->clkrt, host->base + MMC_CLKRT); |
| 268 | |
| 269 | writel(START_CLOCK, host->base + MMC_STRPCL); |
| 270 | |
| 271 | pxamci_enable_irq(host, END_CMD_RES); |
| 272 | } |
| 273 | |
| 274 | static void pxamci_finish_request(struct pxamci_host *host, struct mmc_request *mrq) |
| 275 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | host->mrq = NULL; |
| 277 | host->cmd = NULL; |
| 278 | host->data = NULL; |
| 279 | mmc_request_done(host->mmc, mrq); |
| 280 | } |
| 281 | |
| 282 | static int pxamci_cmd_done(struct pxamci_host *host, unsigned int stat) |
| 283 | { |
| 284 | struct mmc_command *cmd = host->cmd; |
| 285 | int i; |
| 286 | u32 v; |
| 287 | |
| 288 | if (!cmd) |
| 289 | return 0; |
| 290 | |
| 291 | host->cmd = NULL; |
| 292 | |
| 293 | /* |
| 294 | * Did I mention this is Sick. We always need to |
| 295 | * discard the upper 8 bits of the first 16-bit word. |
| 296 | */ |
| 297 | v = readl(host->base + MMC_RES) & 0xffff; |
| 298 | for (i = 0; i < 4; i++) { |
| 299 | u32 w1 = readl(host->base + MMC_RES) & 0xffff; |
| 300 | u32 w2 = readl(host->base + MMC_RES) & 0xffff; |
| 301 | cmd->resp[i] = v << 24 | w1 << 8 | w2 >> 8; |
| 302 | v = w2; |
| 303 | } |
| 304 | |
| 305 | if (stat & STAT_TIME_OUT_RESPONSE) { |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 306 | cmd->error = -ETIMEDOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } else if (stat & STAT_RES_CRC_ERR && cmd->flags & MMC_RSP_CRC) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | /* |
| 309 | * workaround for erratum #42: |
| 310 | * Intel PXA27x Family Processor Specification Update Rev 001 |
Nicolas Pitre | 90e07d9 | 2007-05-13 18:03:08 +0200 | [diff] [blame] | 311 | * A bogus CRC error can appear if the msb of a 136 bit |
| 312 | * response is a one. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | */ |
Cliff Brake | e10a854 | 2009-01-22 16:58:58 -0500 | [diff] [blame] | 314 | if (cpu_is_pxa27x() && |
| 315 | (cmd->flags & MMC_RSP_136 && cmd->resp[0] & 0x80000000)) |
Nicolas Pitre | 90e07d9 | 2007-05-13 18:03:08 +0200 | [diff] [blame] | 316 | pr_debug("ignoring CRC from command %d - *risky*\n", cmd->opcode); |
Cliff Brake | e10a854 | 2009-01-22 16:58:58 -0500 | [diff] [blame] | 317 | else |
| 318 | cmd->error = -EILSEQ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | pxamci_disable_irq(host, END_CMD_RES); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 322 | if (host->data && !cmd->error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | pxamci_enable_irq(host, DATA_TRAN_DONE); |
Cliff Brake | b601895 | 2009-01-22 17:07:03 -0500 | [diff] [blame] | 324 | /* |
| 325 | * workaround for erratum #91, if doing write |
| 326 | * enable DMA late |
| 327 | */ |
| 328 | if (cpu_is_pxa27x() && host->data->flags & MMC_DATA_WRITE) |
| 329 | DCSR(host->dma) = DCSR_RUN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } else { |
| 331 | pxamci_finish_request(host, host->mrq); |
| 332 | } |
| 333 | |
| 334 | return 1; |
| 335 | } |
| 336 | |
| 337 | static int pxamci_data_done(struct pxamci_host *host, unsigned int stat) |
| 338 | { |
| 339 | struct mmc_data *data = host->data; |
| 340 | |
| 341 | if (!data) |
| 342 | return 0; |
| 343 | |
| 344 | DCSR(host->dma) = 0; |
Vernon Sauder | c00a46a | 2008-12-29 19:21:28 -0500 | [diff] [blame] | 345 | dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | host->dma_dir); |
| 347 | |
| 348 | if (stat & STAT_READ_TIME_OUT) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 349 | data->error = -ETIMEDOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | else if (stat & (STAT_CRC_READ_ERROR|STAT_CRC_WRITE_ERROR)) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 351 | data->error = -EILSEQ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | |
| 353 | /* |
| 354 | * There appears to be a hardware design bug here. There seems to |
| 355 | * be no way to find out how much data was transferred to the card. |
| 356 | * This means that if there was an error on any block, we mark all |
| 357 | * data blocks as being in error. |
| 358 | */ |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 359 | if (!data->error) |
Pavel Pisa | 2c171bf | 2006-05-19 21:48:03 +0100 | [diff] [blame] | 360 | data->bytes_xfered = data->blocks * data->blksz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | else |
| 362 | data->bytes_xfered = 0; |
| 363 | |
| 364 | pxamci_disable_irq(host, DATA_TRAN_DONE); |
| 365 | |
| 366 | host->data = NULL; |
Russell King | 58741e8 | 2006-05-02 20:02:39 +0100 | [diff] [blame] | 367 | if (host->mrq->stop) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | pxamci_stop_clock(host); |
Bridge Wu | df456f4 | 2007-09-25 19:09:19 +0200 | [diff] [blame] | 369 | pxamci_start_cmd(host, host->mrq->stop, host->cmdat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | } else { |
| 371 | pxamci_finish_request(host, host->mrq); |
| 372 | } |
| 373 | |
| 374 | return 1; |
| 375 | } |
| 376 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 377 | static irqreturn_t pxamci_irq(int irq, void *devid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | { |
| 379 | struct pxamci_host *host = devid; |
| 380 | unsigned int ireg; |
| 381 | int handled = 0; |
| 382 | |
Bridge Wu | 81ab570f | 2007-09-25 18:59:07 +0200 | [diff] [blame] | 383 | 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] | 384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | if (ireg) { |
| 386 | unsigned stat = readl(host->base + MMC_STAT); |
| 387 | |
Russell King | d78e907 | 2006-05-02 20:18:53 +0100 | [diff] [blame] | 388 | pr_debug("PXAMCI: irq %08x stat %08x\n", ireg, stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
| 390 | if (ireg & END_CMD_RES) |
| 391 | handled |= pxamci_cmd_done(host, stat); |
| 392 | if (ireg & DATA_TRAN_DONE) |
| 393 | handled |= pxamci_data_done(host, stat); |
Bridge Wu | 5d3ad4e | 2007-09-25 19:11:00 +0200 | [diff] [blame] | 394 | if (ireg & SDIO_INT) { |
| 395 | mmc_signal_sdio_irq(host->mmc); |
| 396 | handled = 1; |
| 397 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | return IRQ_RETVAL(handled); |
| 401 | } |
| 402 | |
| 403 | static void pxamci_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 404 | { |
| 405 | struct pxamci_host *host = mmc_priv(mmc); |
| 406 | unsigned int cmdat; |
| 407 | |
| 408 | WARN_ON(host->mrq != NULL); |
| 409 | |
| 410 | host->mrq = mrq; |
| 411 | |
| 412 | pxamci_stop_clock(host); |
| 413 | |
| 414 | cmdat = host->cmdat; |
| 415 | host->cmdat &= ~CMDAT_INIT; |
| 416 | |
| 417 | if (mrq->data) { |
| 418 | pxamci_setup_data(host, mrq->data); |
| 419 | |
| 420 | cmdat &= ~CMDAT_BUSY; |
| 421 | cmdat |= CMDAT_DATAEN | CMDAT_DMAEN; |
| 422 | if (mrq->data->flags & MMC_DATA_WRITE) |
| 423 | cmdat |= CMDAT_WRITE; |
| 424 | |
| 425 | if (mrq->data->flags & MMC_DATA_STREAM) |
| 426 | cmdat |= CMDAT_STREAM; |
| 427 | } |
| 428 | |
| 429 | pxamci_start_cmd(host, mrq->cmd, cmdat); |
| 430 | } |
| 431 | |
Richard Purdie | e619524 | 2005-09-06 15:18:56 -0700 | [diff] [blame] | 432 | static int pxamci_get_ro(struct mmc_host *mmc) |
| 433 | { |
| 434 | struct pxamci_host *host = mmc_priv(mmc); |
| 435 | |
Robert Jarzmik | b405db6 | 2009-06-23 23:21:03 +0200 | [diff] [blame] | 436 | if (host->pdata && gpio_is_valid(host->pdata->gpio_card_ro)) { |
| 437 | if (host->pdata->gpio_card_ro_invert) |
| 438 | return !gpio_get_value(host->pdata->gpio_card_ro); |
| 439 | else |
| 440 | return gpio_get_value(host->pdata->gpio_card_ro); |
| 441 | } |
Richard Purdie | e619524 | 2005-09-06 15:18:56 -0700 | [diff] [blame] | 442 | if (host->pdata && host->pdata->get_ro) |
Anton Vorontsov | 08f80bb | 2008-06-17 18:17:39 +0400 | [diff] [blame] | 443 | return !!host->pdata->get_ro(mmc_dev(mmc)); |
| 444 | /* |
| 445 | * Board doesn't support read only detection; let the mmc core |
| 446 | * decide what to do. |
| 447 | */ |
| 448 | return -ENOSYS; |
Richard Purdie | e619524 | 2005-09-06 15:18:56 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | static void pxamci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 452 | { |
| 453 | struct pxamci_host *host = mmc_priv(mmc); |
| 454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | if (ios->clock) { |
Russell King | ebebd9b | 2007-08-20 10:20:03 +0100 | [diff] [blame] | 456 | unsigned long rate = host->clkrate; |
| 457 | unsigned int clk = rate / ios->clock; |
| 458 | |
Russell King | d8cb70d | 2007-10-26 17:56:40 +0100 | [diff] [blame] | 459 | if (host->clkrt == CLKRT_OFF) |
| 460 | clk_enable(host->clk); |
| 461 | |
Bridge Wu | 64eb036 | 2007-12-13 07:24:30 +0100 | [diff] [blame] | 462 | if (ios->clock == 26000000) { |
Haojian Zhuang | fa3f993 | 2009-08-31 21:52:53 +0800 | [diff] [blame] | 463 | /* to support 26MHz */ |
Bridge Wu | 64eb036 | 2007-12-13 07:24:30 +0100 | [diff] [blame] | 464 | host->clkrt = 7; |
| 465 | } else { |
| 466 | /* to handle (19.5MHz, 26MHz) */ |
| 467 | if (!clk) |
| 468 | clk = 1; |
| 469 | |
| 470 | /* |
| 471 | * clk might result in a lower divisor than we |
| 472 | * desire. check for that condition and adjust |
| 473 | * as appropriate. |
| 474 | */ |
| 475 | if (rate / clk > ios->clock) |
| 476 | clk <<= 1; |
| 477 | host->clkrt = fls(clk) - 1; |
| 478 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
| 480 | /* |
| 481 | * we write clkrt on the next command |
| 482 | */ |
| 483 | } else { |
| 484 | pxamci_stop_clock(host); |
Russell King | d8cb70d | 2007-10-26 17:56:40 +0100 | [diff] [blame] | 485 | if (host->clkrt != CLKRT_OFF) { |
| 486 | host->clkrt = CLKRT_OFF; |
| 487 | clk_disable(host->clk); |
| 488 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | if (host->power_mode != ios->power_mode) { |
| 492 | host->power_mode = ios->power_mode; |
| 493 | |
Daniel Ribeiro | 8385f9c | 2009-05-21 08:54:18 -0300 | [diff] [blame] | 494 | pxamci_set_power(host, ios->vdd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
| 496 | if (ios->power_mode == MMC_POWER_ON) |
| 497 | host->cmdat |= CMDAT_INIT; |
| 498 | } |
| 499 | |
Bridge Wu | df456f4 | 2007-09-25 19:09:19 +0200 | [diff] [blame] | 500 | if (ios->bus_width == MMC_BUS_WIDTH_4) |
| 501 | host->cmdat |= CMDAT_SD_4DAT; |
| 502 | else |
| 503 | host->cmdat &= ~CMDAT_SD_4DAT; |
| 504 | |
Russell King | d78e907 | 2006-05-02 20:18:53 +0100 | [diff] [blame] | 505 | pr_debug("PXAMCI: clkrt = %x cmdat = %x\n", |
Russell King | c656317 | 2006-03-29 09:30:20 +0100 | [diff] [blame] | 506 | host->clkrt, host->cmdat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Bridge Wu | 5d3ad4e | 2007-09-25 19:11:00 +0200 | [diff] [blame] | 509 | static void pxamci_enable_sdio_irq(struct mmc_host *host, int enable) |
| 510 | { |
| 511 | struct pxamci_host *pxa_host = mmc_priv(host); |
| 512 | |
| 513 | if (enable) |
| 514 | pxamci_enable_irq(pxa_host, SDIO_INT); |
| 515 | else |
| 516 | pxamci_disable_irq(pxa_host, SDIO_INT); |
| 517 | } |
| 518 | |
David Brownell | ab7aefd | 2006-11-12 17:55:30 -0800 | [diff] [blame] | 519 | static const struct mmc_host_ops pxamci_ops = { |
Bridge Wu | 5d3ad4e | 2007-09-25 19:11:00 +0200 | [diff] [blame] | 520 | .request = pxamci_request, |
| 521 | .get_ro = pxamci_get_ro, |
| 522 | .set_ios = pxamci_set_ios, |
| 523 | .enable_sdio_irq = pxamci_enable_sdio_irq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | }; |
| 525 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 526 | static void pxamci_dma_irq(int dma, void *devid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | { |
Nicolas Pitre | c783837 | 2007-10-09 17:07:58 -0400 | [diff] [blame] | 528 | struct pxamci_host *host = devid; |
| 529 | int dcsr = DCSR(dma); |
| 530 | DCSR(dma) = dcsr & ~DCSR_STOPIRQEN; |
| 531 | |
| 532 | if (dcsr & DCSR_ENDINTR) { |
| 533 | writel(BUF_PART_FULL, host->base + MMC_PRTBUF); |
| 534 | } else { |
| 535 | printk(KERN_ERR "%s: DMA error on channel %d (DCSR=%#x)\n", |
| 536 | mmc_hostname(host->mmc), dma, dcsr); |
| 537 | host->data->error = -EIO; |
| 538 | pxamci_data_done(host, 0); |
| 539 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | } |
| 541 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 542 | static irqreturn_t pxamci_detect_irq(int irq, void *devid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | { |
Richard Purdie | c26971c | 2005-09-08 22:48:16 +0100 | [diff] [blame] | 544 | struct pxamci_host *host = mmc_priv(devid); |
| 545 | |
| 546 | mmc_detect_change(devid, host->pdata->detect_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | return IRQ_HANDLED; |
| 548 | } |
| 549 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 550 | static int pxamci_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | struct mmc_host *mmc; |
| 553 | struct pxamci_host *host = NULL; |
Bridge Wu | 9a788c6 | 2007-12-14 10:40:25 +0100 | [diff] [blame] | 554 | struct resource *r, *dmarx, *dmatx; |
Robert Jarzmik | b405db6 | 2009-06-23 23:21:03 +0200 | [diff] [blame] | 555 | int ret, irq, gpio_cd = -1, gpio_ro = -1, gpio_power = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
| 557 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 558 | irq = platform_get_irq(pdev, 0); |
David Vrabel | 4894473 | 2006-01-19 17:56:29 +0000 | [diff] [blame] | 559 | if (!r || irq < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | return -ENXIO; |
| 561 | |
| 562 | r = request_mem_region(r->start, SZ_4K, DRIVER_NAME); |
| 563 | if (!r) |
| 564 | return -EBUSY; |
| 565 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 566 | mmc = mmc_alloc_host(sizeof(struct pxamci_host), &pdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | if (!mmc) { |
| 568 | ret = -ENOMEM; |
| 569 | goto out; |
| 570 | } |
| 571 | |
| 572 | mmc->ops = &pxamci_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | |
| 574 | /* |
| 575 | * We can do SG-DMA, but we don't because we never know how much |
| 576 | * data we successfully wrote to the card. |
| 577 | */ |
| 578 | mmc->max_phys_segs = NR_SG; |
| 579 | |
| 580 | /* |
| 581 | * Our hardware DMA can handle a maximum of one page per SG entry. |
| 582 | */ |
| 583 | mmc->max_seg_size = PAGE_SIZE; |
| 584 | |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 585 | /* |
Nicolas Pitre | fe2dc44 | 2007-09-24 15:47:18 -0400 | [diff] [blame] | 586 | * Block length register is only 10 bits before PXA27x. |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 587 | */ |
Eric Miao | 0ffcbfd | 2008-09-11 10:27:30 +0800 | [diff] [blame] | 588 | mmc->max_blk_size = cpu_is_pxa25x() ? 1023 : 2048; |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 589 | |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 590 | /* |
| 591 | * Block count register is 16 bits. |
| 592 | */ |
| 593 | mmc->max_blk_count = 65535; |
| 594 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | host = mmc_priv(mmc); |
| 596 | host->mmc = mmc; |
| 597 | host->dma = -1; |
| 598 | host->pdata = pdev->dev.platform_data; |
Russell King | d8cb70d | 2007-10-26 17:56:40 +0100 | [diff] [blame] | 599 | host->clkrt = CLKRT_OFF; |
Russell King | ebebd9b | 2007-08-20 10:20:03 +0100 | [diff] [blame] | 600 | |
Russell King | e0d8b13 | 2008-11-11 17:52:32 +0000 | [diff] [blame] | 601 | host->clk = clk_get(&pdev->dev, NULL); |
Russell King | ebebd9b | 2007-08-20 10:20:03 +0100 | [diff] [blame] | 602 | if (IS_ERR(host->clk)) { |
| 603 | ret = PTR_ERR(host->clk); |
| 604 | host->clk = NULL; |
| 605 | goto out; |
| 606 | } |
| 607 | |
| 608 | host->clkrate = clk_get_rate(host->clk); |
| 609 | |
| 610 | /* |
| 611 | * Calculate minimum clock rate, rounding up. |
| 612 | */ |
| 613 | mmc->f_min = (host->clkrate + 63) / 64; |
Haojian Zhuang | fa3f993 | 2009-08-31 21:52:53 +0800 | [diff] [blame] | 614 | mmc->f_max = (mmc_has_26MHz()) ? 26000000 : host->clkrate; |
Russell King | ebebd9b | 2007-08-20 10:20:03 +0100 | [diff] [blame] | 615 | |
Daniel Ribeiro | 8385f9c | 2009-05-21 08:54:18 -0300 | [diff] [blame] | 616 | pxamci_init_ocr(host); |
| 617 | |
Bridge Wu | df456f4 | 2007-09-25 19:09:19 +0200 | [diff] [blame] | 618 | mmc->caps = 0; |
Bridge Wu | 5d3ad4e | 2007-09-25 19:11:00 +0200 | [diff] [blame] | 619 | host->cmdat = 0; |
Eric Miao | 0ffcbfd | 2008-09-11 10:27:30 +0800 | [diff] [blame] | 620 | if (!cpu_is_pxa25x()) { |
Bridge Wu | 5d3ad4e | 2007-09-25 19:11:00 +0200 | [diff] [blame] | 621 | mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ; |
| 622 | host->cmdat |= CMDAT_SDIO_INT_EN; |
Haojian Zhuang | fa3f993 | 2009-08-31 21:52:53 +0800 | [diff] [blame] | 623 | if (mmc_has_26MHz()) |
Bridge Wu | 64eb036 | 2007-12-13 07:24:30 +0100 | [diff] [blame] | 624 | mmc->caps |= MMC_CAP_MMC_HIGHSPEED | |
| 625 | MMC_CAP_SD_HIGHSPEED; |
Bridge Wu | 5d3ad4e | 2007-09-25 19:11:00 +0200 | [diff] [blame] | 626 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 628 | 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] | 629 | if (!host->sg_cpu) { |
| 630 | ret = -ENOMEM; |
| 631 | goto out; |
| 632 | } |
| 633 | |
| 634 | spin_lock_init(&host->lock); |
| 635 | host->res = r; |
| 636 | host->irq = irq; |
| 637 | host->imask = MMC_I_MASK_ALL; |
| 638 | |
| 639 | host->base = ioremap(r->start, SZ_4K); |
| 640 | if (!host->base) { |
| 641 | ret = -ENOMEM; |
| 642 | goto out; |
| 643 | } |
| 644 | |
| 645 | /* |
| 646 | * Ensure that the host controller is shut down, and setup |
| 647 | * with our defaults. |
| 648 | */ |
| 649 | pxamci_stop_clock(host); |
| 650 | writel(0, host->base + MMC_SPI); |
| 651 | writel(64, host->base + MMC_RESTO); |
| 652 | writel(host->imask, host->base + MMC_I_MASK); |
| 653 | |
| 654 | host->dma = pxa_request_dma(DRIVER_NAME, DMA_PRIO_LOW, |
| 655 | pxamci_dma_irq, host); |
| 656 | if (host->dma < 0) { |
| 657 | ret = -EBUSY; |
| 658 | goto out; |
| 659 | } |
| 660 | |
| 661 | ret = request_irq(host->irq, pxamci_irq, 0, DRIVER_NAME, host); |
| 662 | if (ret) |
| 663 | goto out; |
| 664 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 665 | platform_set_drvdata(pdev, mmc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | |
Bridge Wu | 9a788c6 | 2007-12-14 10:40:25 +0100 | [diff] [blame] | 667 | dmarx = platform_get_resource(pdev, IORESOURCE_DMA, 0); |
| 668 | if (!dmarx) { |
| 669 | ret = -ENXIO; |
| 670 | goto out; |
| 671 | } |
| 672 | host->dma_drcmrrx = dmarx->start; |
| 673 | |
| 674 | dmatx = platform_get_resource(pdev, IORESOURCE_DMA, 1); |
| 675 | if (!dmatx) { |
| 676 | ret = -ENXIO; |
| 677 | goto out; |
| 678 | } |
| 679 | host->dma_drcmrtx = dmatx->start; |
| 680 | |
Robert Jarzmik | b405db6 | 2009-06-23 23:21:03 +0200 | [diff] [blame] | 681 | if (host->pdata) { |
| 682 | gpio_cd = host->pdata->gpio_card_detect; |
| 683 | gpio_ro = host->pdata->gpio_card_ro; |
| 684 | gpio_power = host->pdata->gpio_power; |
| 685 | } |
| 686 | if (gpio_is_valid(gpio_power)) { |
| 687 | ret = gpio_request(gpio_power, "mmc card power"); |
| 688 | if (ret) { |
| 689 | dev_err(&pdev->dev, "Failed requesting gpio_power %d\n", gpio_power); |
| 690 | goto out; |
| 691 | } |
| 692 | gpio_direction_output(gpio_power, |
| 693 | host->pdata->gpio_power_invert); |
| 694 | } |
| 695 | if (gpio_is_valid(gpio_ro)) { |
| 696 | ret = gpio_request(gpio_ro, "mmc card read only"); |
| 697 | if (ret) { |
Antonio Ospite | 48f0295 | 2009-10-02 16:24:02 +0200 | [diff] [blame] | 698 | dev_err(&pdev->dev, "Failed requesting gpio_ro %d\n", gpio_ro); |
Robert Jarzmik | b405db6 | 2009-06-23 23:21:03 +0200 | [diff] [blame] | 699 | goto err_gpio_ro; |
| 700 | } |
| 701 | gpio_direction_input(gpio_ro); |
| 702 | } |
| 703 | if (gpio_is_valid(gpio_cd)) { |
| 704 | ret = gpio_request(gpio_cd, "mmc card detect"); |
| 705 | if (ret) { |
Antonio Ospite | 48f0295 | 2009-10-02 16:24:02 +0200 | [diff] [blame] | 706 | dev_err(&pdev->dev, "Failed requesting gpio_cd %d\n", gpio_cd); |
Robert Jarzmik | b405db6 | 2009-06-23 23:21:03 +0200 | [diff] [blame] | 707 | goto err_gpio_cd; |
| 708 | } |
| 709 | gpio_direction_input(gpio_cd); |
| 710 | |
| 711 | ret = request_irq(gpio_to_irq(gpio_cd), pxamci_detect_irq, |
| 712 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 713 | "mmc card detect", mmc); |
| 714 | if (ret) { |
| 715 | dev_err(&pdev->dev, "failed to request card detect IRQ\n"); |
| 716 | goto err_request_irq; |
| 717 | } |
| 718 | } |
| 719 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | if (host->pdata && host->pdata->init) |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 721 | host->pdata->init(&pdev->dev, pxamci_detect_irq, mmc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | |
Robert Jarzmik | b405db6 | 2009-06-23 23:21:03 +0200 | [diff] [blame] | 723 | if (gpio_is_valid(gpio_power) && host->pdata->setpower) |
| 724 | dev_warn(&pdev->dev, "gpio_power and setpower() both defined\n"); |
| 725 | if (gpio_is_valid(gpio_ro) && host->pdata->get_ro) |
| 726 | dev_warn(&pdev->dev, "gpio_ro and get_ro() both defined\n"); |
| 727 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | mmc_add_host(mmc); |
| 729 | |
| 730 | return 0; |
| 731 | |
Robert Jarzmik | b405db6 | 2009-06-23 23:21:03 +0200 | [diff] [blame] | 732 | err_request_irq: |
| 733 | gpio_free(gpio_cd); |
| 734 | err_gpio_cd: |
| 735 | gpio_free(gpio_ro); |
| 736 | err_gpio_ro: |
| 737 | gpio_free(gpio_power); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | out: |
| 739 | if (host) { |
| 740 | if (host->dma >= 0) |
| 741 | pxa_free_dma(host->dma); |
| 742 | if (host->base) |
| 743 | iounmap(host->base); |
| 744 | if (host->sg_cpu) |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 745 | dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma); |
Russell King | ebebd9b | 2007-08-20 10:20:03 +0100 | [diff] [blame] | 746 | if (host->clk) |
| 747 | clk_put(host->clk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | } |
| 749 | if (mmc) |
| 750 | mmc_free_host(mmc); |
| 751 | release_resource(r); |
| 752 | return ret; |
| 753 | } |
| 754 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 755 | static int pxamci_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 757 | struct mmc_host *mmc = platform_get_drvdata(pdev); |
Robert Jarzmik | b405db6 | 2009-06-23 23:21:03 +0200 | [diff] [blame] | 758 | int gpio_cd = -1, gpio_ro = -1, gpio_power = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 760 | platform_set_drvdata(pdev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | |
| 762 | if (mmc) { |
| 763 | struct pxamci_host *host = mmc_priv(mmc); |
| 764 | |
Daniel Mack | 5d6b1ed | 2009-12-01 18:17:18 +0100 | [diff] [blame] | 765 | mmc_remove_host(mmc); |
| 766 | |
Robert Jarzmik | b405db6 | 2009-06-23 23:21:03 +0200 | [diff] [blame] | 767 | if (host->pdata) { |
| 768 | gpio_cd = host->pdata->gpio_card_detect; |
| 769 | gpio_ro = host->pdata->gpio_card_ro; |
| 770 | gpio_power = host->pdata->gpio_power; |
| 771 | } |
| 772 | if (gpio_is_valid(gpio_cd)) { |
| 773 | free_irq(gpio_to_irq(gpio_cd), mmc); |
| 774 | gpio_free(gpio_cd); |
| 775 | } |
| 776 | if (gpio_is_valid(gpio_ro)) |
| 777 | gpio_free(gpio_ro); |
| 778 | if (gpio_is_valid(gpio_power)) |
| 779 | gpio_free(gpio_power); |
Daniel Ribeiro | 8385f9c | 2009-05-21 08:54:18 -0300 | [diff] [blame] | 780 | if (host->vcc) |
| 781 | regulator_put(host->vcc); |
| 782 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | if (host->pdata && host->pdata->exit) |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 784 | host->pdata->exit(&pdev->dev, mmc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | pxamci_stop_clock(host); |
| 787 | writel(TXFIFO_WR_REQ|RXFIFO_RD_REQ|CLK_IS_OFF|STOP_CMD| |
| 788 | END_CMD_RES|PRG_DONE|DATA_TRAN_DONE, |
| 789 | host->base + MMC_I_MASK); |
| 790 | |
Bridge Wu | 9a788c6 | 2007-12-14 10:40:25 +0100 | [diff] [blame] | 791 | DRCMR(host->dma_drcmrrx) = 0; |
| 792 | DRCMR(host->dma_drcmrtx) = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | |
| 794 | free_irq(host->irq, host); |
| 795 | pxa_free_dma(host->dma); |
| 796 | iounmap(host->base); |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 797 | 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] | 798 | |
Russell King | ebebd9b | 2007-08-20 10:20:03 +0100 | [diff] [blame] | 799 | clk_put(host->clk); |
| 800 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | release_resource(host->res); |
| 802 | |
| 803 | mmc_free_host(mmc); |
| 804 | } |
| 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | #ifdef CONFIG_PM |
Mike Rapoport | 33264f9 | 2009-07-29 11:59:24 +0300 | [diff] [blame] | 809 | static int pxamci_suspend(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | { |
Mike Rapoport | 33264f9 | 2009-07-29 11:59:24 +0300 | [diff] [blame] | 811 | struct mmc_host *mmc = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | int ret = 0; |
| 813 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 814 | if (mmc) |
Mike Rapoport | 33264f9 | 2009-07-29 11:59:24 +0300 | [diff] [blame] | 815 | ret = mmc_suspend_host(mmc, PMSG_SUSPEND); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | |
| 817 | return ret; |
| 818 | } |
| 819 | |
Mike Rapoport | 33264f9 | 2009-07-29 11:59:24 +0300 | [diff] [blame] | 820 | static int pxamci_resume(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | { |
Mike Rapoport | 33264f9 | 2009-07-29 11:59:24 +0300 | [diff] [blame] | 822 | struct mmc_host *mmc = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | int ret = 0; |
| 824 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 825 | if (mmc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | ret = mmc_resume_host(mmc); |
| 827 | |
| 828 | return ret; |
| 829 | } |
Mike Rapoport | 33264f9 | 2009-07-29 11:59:24 +0300 | [diff] [blame] | 830 | |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 831 | static const struct dev_pm_ops pxamci_pm_ops = { |
Mike Rapoport | 33264f9 | 2009-07-29 11:59:24 +0300 | [diff] [blame] | 832 | .suspend = pxamci_suspend, |
| 833 | .resume = pxamci_resume, |
| 834 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | #endif |
| 836 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 837 | static struct platform_driver pxamci_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | .probe = pxamci_probe, |
| 839 | .remove = pxamci_remove, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 840 | .driver = { |
| 841 | .name = DRIVER_NAME, |
Kay Sievers | bc65c72 | 2008-04-15 14:34:28 -0700 | [diff] [blame] | 842 | .owner = THIS_MODULE, |
Mike Rapoport | 33264f9 | 2009-07-29 11:59:24 +0300 | [diff] [blame] | 843 | #ifdef CONFIG_PM |
| 844 | .pm = &pxamci_pm_ops, |
| 845 | #endif |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 846 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | }; |
| 848 | |
| 849 | static int __init pxamci_init(void) |
| 850 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 851 | return platform_driver_register(&pxamci_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | static void __exit pxamci_exit(void) |
| 855 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 856 | platform_driver_unregister(&pxamci_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | module_init(pxamci_init); |
| 860 | module_exit(pxamci_exit); |
| 861 | |
| 862 | MODULE_DESCRIPTION("PXA Multimedia Card Interface Driver"); |
| 863 | MODULE_LICENSE("GPL"); |
Kay Sievers | bc65c72 | 2008-04-15 14:34:28 -0700 | [diff] [blame] | 864 | MODULE_ALIAS("platform:pxa2xx-mci"); |