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