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/mmci.c - ARM PrimeCell MMCI PL180/1 driver |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2003 Deep Blue Solutions, Ltd, All Rights Reserved. |
Linus Walleij | 64de028 | 2010-02-19 01:09:10 +0100 | [diff] [blame^] | 5 | * Copyright (C) 2010 ST-Ericsson AB. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/moduleparam.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/ioport.h> |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/highmem.h> |
Nicolas Pitre | 019a5f5 | 2007-10-11 01:06:03 -0400 | [diff] [blame] | 20 | #include <linux/log2.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/mmc/host.h> |
Russell King | a62c80e | 2006-01-07 13:52:45 +0000 | [diff] [blame] | 22 | #include <linux/amba/bus.h> |
Russell King | f8ce254 | 2006-01-07 16:15:52 +0000 | [diff] [blame] | 23 | #include <linux/clk.h> |
Jens Axboe | bd6dee6 | 2007-10-24 09:01:09 +0200 | [diff] [blame] | 24 | #include <linux/scatterlist.h> |
Russell King | 8900144 | 2009-07-09 15:16:07 +0100 | [diff] [blame] | 25 | #include <linux/gpio.h> |
Linus Walleij | 6ef297f | 2009-09-22 14:29:36 +0100 | [diff] [blame] | 26 | #include <linux/amba/mmci.h> |
Linus Walleij | 34e84f3 | 2009-09-22 14:41:40 +0100 | [diff] [blame] | 27 | #include <linux/regulator/consumer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Russell King | e9c091b | 2006-01-04 16:24:05 +0000 | [diff] [blame] | 29 | #include <asm/cacheflush.h> |
Russell King | 7b09cda | 2005-07-01 12:02:59 +0100 | [diff] [blame] | 30 | #include <asm/div64.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <asm/io.h> |
Russell King | c6b8fda | 2005-10-28 14:05:16 +0100 | [diff] [blame] | 32 | #include <asm/sizes.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | #include "mmci.h" |
| 35 | |
| 36 | #define DRIVER_NAME "mmci-pl18x" |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | static unsigned int fmax = 515633; |
| 39 | |
Linus Walleij | a6a6464 | 2009-09-14 12:56:14 +0100 | [diff] [blame] | 40 | /* |
| 41 | * This must be called with host->lock held |
| 42 | */ |
| 43 | static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired) |
| 44 | { |
| 45 | u32 clk = 0; |
| 46 | |
| 47 | if (desired) { |
| 48 | if (desired >= host->mclk) { |
| 49 | clk = MCI_CLK_BYPASS; |
| 50 | host->cclk = host->mclk; |
| 51 | } else { |
| 52 | clk = host->mclk / (2 * desired) - 1; |
| 53 | if (clk >= 256) |
| 54 | clk = 255; |
| 55 | host->cclk = host->mclk / (2 * (clk + 1)); |
| 56 | } |
Linus Walleij | b43149c | 2009-11-10 08:33:01 +0100 | [diff] [blame] | 57 | if (host->hw_designer == AMBA_VENDOR_ST) |
Linus Walleij | a6a6464 | 2009-09-14 12:56:14 +0100 | [diff] [blame] | 58 | clk |= MCI_FCEN; /* Bug fix in ST IP block */ |
| 59 | clk |= MCI_CLK_ENABLE; |
| 60 | /* This hasn't proven to be worthwhile */ |
| 61 | /* clk |= MCI_CLK_PWRSAVE; */ |
| 62 | } |
| 63 | |
Linus Walleij | 9e6c82c | 2009-09-14 12:57:11 +0100 | [diff] [blame] | 64 | if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4) |
| 65 | clk |= MCI_WIDE_BUS; |
| 66 | |
Linus Walleij | a6a6464 | 2009-09-14 12:56:14 +0100 | [diff] [blame] | 67 | writel(clk, host->base + MMCICLOCK); |
| 68 | } |
| 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | static void |
| 71 | mmci_request_end(struct mmci_host *host, struct mmc_request *mrq) |
| 72 | { |
| 73 | writel(0, host->base + MMCICOMMAND); |
| 74 | |
Russell King | e47c222 | 2007-01-08 16:42:51 +0000 | [diff] [blame] | 75 | BUG_ON(host->data); |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | host->mrq = NULL; |
| 78 | host->cmd = NULL; |
| 79 | |
| 80 | if (mrq->data) |
| 81 | mrq->data->bytes_xfered = host->data_xfered; |
| 82 | |
| 83 | /* |
| 84 | * Need to drop the host lock here; mmc_request_done may call |
| 85 | * back into the driver... |
| 86 | */ |
| 87 | spin_unlock(&host->lock); |
| 88 | mmc_request_done(host->mmc, mrq); |
| 89 | spin_lock(&host->lock); |
| 90 | } |
| 91 | |
| 92 | static void mmci_stop_data(struct mmci_host *host) |
| 93 | { |
| 94 | writel(0, host->base + MMCIDATACTRL); |
| 95 | writel(0, host->base + MMCIMASK1); |
| 96 | host->data = NULL; |
| 97 | } |
| 98 | |
| 99 | static void mmci_start_data(struct mmci_host *host, struct mmc_data *data) |
| 100 | { |
| 101 | unsigned int datactrl, timeout, irqmask; |
Russell King | 7b09cda | 2005-07-01 12:02:59 +0100 | [diff] [blame] | 102 | unsigned long long clks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | void __iomem *base; |
Russell King | 3bc87f2 | 2006-08-27 13:51:28 +0100 | [diff] [blame] | 104 | int blksz_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Linus Walleij | 64de028 | 2010-02-19 01:09:10 +0100 | [diff] [blame^] | 106 | dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n", |
| 107 | data->blksz, data->blocks, data->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | host->data = data; |
Russell King | 3bc87f2 | 2006-08-27 13:51:28 +0100 | [diff] [blame] | 110 | host->size = data->blksz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | host->data_xfered = 0; |
| 112 | |
| 113 | mmci_init_sg(host, data); |
| 114 | |
Russell King | 7b09cda | 2005-07-01 12:02:59 +0100 | [diff] [blame] | 115 | clks = (unsigned long long)data->timeout_ns * host->cclk; |
| 116 | do_div(clks, 1000000000UL); |
| 117 | |
| 118 | timeout = data->timeout_clks + (unsigned int)clks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
| 120 | base = host->base; |
| 121 | writel(timeout, base + MMCIDATATIMER); |
| 122 | writel(host->size, base + MMCIDATALENGTH); |
| 123 | |
Russell King | 3bc87f2 | 2006-08-27 13:51:28 +0100 | [diff] [blame] | 124 | blksz_bits = ffs(data->blksz) - 1; |
| 125 | BUG_ON(1 << blksz_bits != data->blksz); |
| 126 | |
| 127 | datactrl = MCI_DPSM_ENABLE | blksz_bits << 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | if (data->flags & MMC_DATA_READ) { |
| 129 | datactrl |= MCI_DPSM_DIRECTION; |
| 130 | irqmask = MCI_RXFIFOHALFFULLMASK; |
Russell King | 0425a14 | 2006-02-16 16:48:31 +0000 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | * If we have less than a FIFOSIZE of bytes to transfer, |
| 134 | * trigger a PIO interrupt as soon as any data is available. |
| 135 | */ |
| 136 | if (host->size < MCI_FIFOSIZE) |
| 137 | irqmask |= MCI_RXDATAAVLBLMASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } else { |
| 139 | /* |
| 140 | * We don't actually need to include "FIFO empty" here |
| 141 | * since its implicit in "FIFO half empty". |
| 142 | */ |
| 143 | irqmask = MCI_TXFIFOHALFEMPTYMASK; |
| 144 | } |
| 145 | |
| 146 | writel(datactrl, base + MMCIDATACTRL); |
| 147 | writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0); |
| 148 | writel(irqmask, base + MMCIMASK1); |
| 149 | } |
| 150 | |
| 151 | static void |
| 152 | mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c) |
| 153 | { |
| 154 | void __iomem *base = host->base; |
| 155 | |
Linus Walleij | 64de028 | 2010-02-19 01:09:10 +0100 | [diff] [blame^] | 156 | dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | cmd->opcode, cmd->arg, cmd->flags); |
| 158 | |
| 159 | if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) { |
| 160 | writel(0, base + MMCICOMMAND); |
| 161 | udelay(1); |
| 162 | } |
| 163 | |
| 164 | c |= cmd->opcode | MCI_CPSM_ENABLE; |
Russell King | e922517 | 2006-02-02 12:23:12 +0000 | [diff] [blame] | 165 | if (cmd->flags & MMC_RSP_PRESENT) { |
| 166 | if (cmd->flags & MMC_RSP_136) |
| 167 | c |= MCI_CPSM_LONGRSP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | c |= MCI_CPSM_RESPONSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
| 170 | if (/*interrupt*/0) |
| 171 | c |= MCI_CPSM_INTERRUPT; |
| 172 | |
| 173 | host->cmd = cmd; |
| 174 | |
| 175 | writel(cmd->arg, base + MMCIARGUMENT); |
| 176 | writel(c, base + MMCICOMMAND); |
| 177 | } |
| 178 | |
| 179 | static void |
| 180 | mmci_data_irq(struct mmci_host *host, struct mmc_data *data, |
| 181 | unsigned int status) |
| 182 | { |
| 183 | if (status & MCI_DATABLOCKEND) { |
Russell King | 3bc87f2 | 2006-08-27 13:51:28 +0100 | [diff] [blame] | 184 | host->data_xfered += data->blksz; |
Linus Walleij | f28e8a4 | 2010-01-25 07:14:46 +0100 | [diff] [blame] | 185 | #ifdef CONFIG_ARCH_U300 |
| 186 | /* |
| 187 | * On the U300 some signal or other is |
| 188 | * badly routed so that a data write does |
| 189 | * not properly terminate with a MCI_DATAEND |
| 190 | * status flag. This quirk will make writes |
| 191 | * work again. |
| 192 | */ |
| 193 | if (data->flags & MMC_DATA_WRITE) |
| 194 | status |= MCI_DATAEND; |
| 195 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|MCI_RXOVERRUN)) { |
Linus Walleij | 64de028 | 2010-02-19 01:09:10 +0100 | [diff] [blame^] | 198 | dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ (status %08x)\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | if (status & MCI_DATACRCFAIL) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 200 | data->error = -EILSEQ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | else if (status & MCI_DATATIMEOUT) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 202 | data->error = -ETIMEDOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | else if (status & (MCI_TXUNDERRUN|MCI_RXOVERRUN)) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 204 | data->error = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | status |= MCI_DATAEND; |
Russell King | e9c091b | 2006-01-04 16:24:05 +0000 | [diff] [blame] | 206 | |
| 207 | /* |
| 208 | * We hit an error condition. Ensure that any data |
| 209 | * partially written to a page is properly coherent. |
| 210 | */ |
| 211 | if (host->sg_len && data->flags & MMC_DATA_READ) |
Jens Axboe | bd6dee6 | 2007-10-24 09:01:09 +0200 | [diff] [blame] | 212 | flush_dcache_page(sg_page(host->sg_ptr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | if (status & MCI_DATAEND) { |
| 215 | mmci_stop_data(host); |
| 216 | |
| 217 | if (!data->stop) { |
| 218 | mmci_request_end(host, data->mrq); |
| 219 | } else { |
| 220 | mmci_start_command(host, data->stop, 0); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | static void |
| 226 | mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd, |
| 227 | unsigned int status) |
| 228 | { |
| 229 | void __iomem *base = host->base; |
| 230 | |
| 231 | host->cmd = NULL; |
| 232 | |
| 233 | cmd->resp[0] = readl(base + MMCIRESPONSE0); |
| 234 | cmd->resp[1] = readl(base + MMCIRESPONSE1); |
| 235 | cmd->resp[2] = readl(base + MMCIRESPONSE2); |
| 236 | cmd->resp[3] = readl(base + MMCIRESPONSE3); |
| 237 | |
| 238 | if (status & MCI_CMDTIMEOUT) { |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 239 | cmd->error = -ETIMEDOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) { |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 241 | cmd->error = -EILSEQ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 244 | if (!cmd->data || cmd->error) { |
Russell King | e47c222 | 2007-01-08 16:42:51 +0000 | [diff] [blame] | 245 | if (host->data) |
| 246 | mmci_stop_data(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | mmci_request_end(host, cmd->mrq); |
| 248 | } else if (!(cmd->data->flags & MMC_DATA_READ)) { |
| 249 | mmci_start_data(host, cmd->data); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain) |
| 254 | { |
| 255 | void __iomem *base = host->base; |
| 256 | char *ptr = buffer; |
| 257 | u32 status; |
Linus Walleij | 26eed9a | 2008-04-26 23:39:44 +0100 | [diff] [blame] | 258 | int host_remain = host->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
| 260 | do { |
Linus Walleij | 26eed9a | 2008-04-26 23:39:44 +0100 | [diff] [blame] | 261 | int count = host_remain - (readl(base + MMCIFIFOCNT) << 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
| 263 | if (count > remain) |
| 264 | count = remain; |
| 265 | |
| 266 | if (count <= 0) |
| 267 | break; |
| 268 | |
| 269 | readsl(base + MMCIFIFO, ptr, count >> 2); |
| 270 | |
| 271 | ptr += count; |
| 272 | remain -= count; |
Linus Walleij | 26eed9a | 2008-04-26 23:39:44 +0100 | [diff] [blame] | 273 | host_remain -= count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
| 275 | if (remain == 0) |
| 276 | break; |
| 277 | |
| 278 | status = readl(base + MMCISTATUS); |
| 279 | } while (status & MCI_RXDATAAVLBL); |
| 280 | |
| 281 | return ptr - buffer; |
| 282 | } |
| 283 | |
| 284 | static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status) |
| 285 | { |
| 286 | void __iomem *base = host->base; |
| 287 | char *ptr = buffer; |
| 288 | |
| 289 | do { |
| 290 | unsigned int count, maxcnt; |
| 291 | |
| 292 | maxcnt = status & MCI_TXFIFOEMPTY ? MCI_FIFOSIZE : MCI_FIFOHALFSIZE; |
| 293 | count = min(remain, maxcnt); |
| 294 | |
| 295 | writesl(base + MMCIFIFO, ptr, count >> 2); |
| 296 | |
| 297 | ptr += count; |
| 298 | remain -= count; |
| 299 | |
| 300 | if (remain == 0) |
| 301 | break; |
| 302 | |
| 303 | status = readl(base + MMCISTATUS); |
| 304 | } while (status & MCI_TXFIFOHALFEMPTY); |
| 305 | |
| 306 | return ptr - buffer; |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * PIO data transfer IRQ handler. |
| 311 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 312 | static irqreturn_t mmci_pio_irq(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | { |
| 314 | struct mmci_host *host = dev_id; |
| 315 | void __iomem *base = host->base; |
| 316 | u32 status; |
| 317 | |
| 318 | status = readl(base + MMCISTATUS); |
| 319 | |
Linus Walleij | 64de028 | 2010-02-19 01:09:10 +0100 | [diff] [blame^] | 320 | dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
| 322 | do { |
| 323 | unsigned long flags; |
| 324 | unsigned int remain, len; |
| 325 | char *buffer; |
| 326 | |
| 327 | /* |
| 328 | * For write, we only need to test the half-empty flag |
| 329 | * here - if the FIFO is completely empty, then by |
| 330 | * definition it is more than half empty. |
| 331 | * |
| 332 | * For read, check for data available. |
| 333 | */ |
| 334 | if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL))) |
| 335 | break; |
| 336 | |
| 337 | /* |
| 338 | * Map the current scatter buffer. |
| 339 | */ |
| 340 | buffer = mmci_kmap_atomic(host, &flags) + host->sg_off; |
| 341 | remain = host->sg_ptr->length - host->sg_off; |
| 342 | |
| 343 | len = 0; |
| 344 | if (status & MCI_RXACTIVE) |
| 345 | len = mmci_pio_read(host, buffer, remain); |
| 346 | if (status & MCI_TXACTIVE) |
| 347 | len = mmci_pio_write(host, buffer, remain, status); |
| 348 | |
| 349 | /* |
| 350 | * Unmap the buffer. |
| 351 | */ |
Evgeniy Polyakov | f3e2628 | 2006-01-05 10:31:23 +0000 | [diff] [blame] | 352 | mmci_kunmap_atomic(host, buffer, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
| 354 | host->sg_off += len; |
| 355 | host->size -= len; |
| 356 | remain -= len; |
| 357 | |
| 358 | if (remain) |
| 359 | break; |
| 360 | |
Russell King | e9c091b | 2006-01-04 16:24:05 +0000 | [diff] [blame] | 361 | /* |
| 362 | * If we were reading, and we have completed this |
| 363 | * page, ensure that the data cache is coherent. |
| 364 | */ |
| 365 | if (status & MCI_RXACTIVE) |
Jens Axboe | bd6dee6 | 2007-10-24 09:01:09 +0200 | [diff] [blame] | 366 | flush_dcache_page(sg_page(host->sg_ptr)); |
Russell King | e9c091b | 2006-01-04 16:24:05 +0000 | [diff] [blame] | 367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | if (!mmci_next_sg(host)) |
| 369 | break; |
| 370 | |
| 371 | status = readl(base + MMCISTATUS); |
| 372 | } while (1); |
| 373 | |
| 374 | /* |
| 375 | * If we're nearing the end of the read, switch to |
| 376 | * "any data available" mode. |
| 377 | */ |
| 378 | if (status & MCI_RXACTIVE && host->size < MCI_FIFOSIZE) |
| 379 | writel(MCI_RXDATAAVLBLMASK, base + MMCIMASK1); |
| 380 | |
| 381 | /* |
| 382 | * If we run out of data, disable the data IRQs; this |
| 383 | * prevents a race where the FIFO becomes empty before |
| 384 | * the chip itself has disabled the data path, and |
| 385 | * stops us racing with our data end IRQ. |
| 386 | */ |
| 387 | if (host->size == 0) { |
| 388 | writel(0, base + MMCIMASK1); |
| 389 | writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0); |
| 390 | } |
| 391 | |
| 392 | return IRQ_HANDLED; |
| 393 | } |
| 394 | |
| 395 | /* |
| 396 | * Handle completion of command and data transfers. |
| 397 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 398 | static irqreturn_t mmci_irq(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | { |
| 400 | struct mmci_host *host = dev_id; |
| 401 | u32 status; |
| 402 | int ret = 0; |
| 403 | |
| 404 | spin_lock(&host->lock); |
| 405 | |
| 406 | do { |
| 407 | struct mmc_command *cmd; |
| 408 | struct mmc_data *data; |
| 409 | |
| 410 | status = readl(host->base + MMCISTATUS); |
| 411 | status &= readl(host->base + MMCIMASK0); |
| 412 | writel(status, host->base + MMCICLEAR); |
| 413 | |
Linus Walleij | 64de028 | 2010-02-19 01:09:10 +0100 | [diff] [blame^] | 414 | dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
| 416 | data = host->data; |
| 417 | if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN| |
| 418 | MCI_RXOVERRUN|MCI_DATAEND|MCI_DATABLOCKEND) && data) |
| 419 | mmci_data_irq(host, data, status); |
| 420 | |
| 421 | cmd = host->cmd; |
| 422 | if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd) |
| 423 | mmci_cmd_irq(host, cmd, status); |
| 424 | |
| 425 | ret = 1; |
| 426 | } while (status); |
| 427 | |
| 428 | spin_unlock(&host->lock); |
| 429 | |
| 430 | return IRQ_RETVAL(ret); |
| 431 | } |
| 432 | |
| 433 | static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 434 | { |
| 435 | struct mmci_host *host = mmc_priv(mmc); |
Linus Walleij | 9e94302 | 2008-10-24 21:17:50 +0100 | [diff] [blame] | 436 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | |
| 438 | WARN_ON(host->mrq != NULL); |
| 439 | |
Nicolas Pitre | 019a5f5 | 2007-10-11 01:06:03 -0400 | [diff] [blame] | 440 | if (mrq->data && !is_power_of_2(mrq->data->blksz)) { |
Linus Walleij | 64de028 | 2010-02-19 01:09:10 +0100 | [diff] [blame^] | 441 | dev_err(mmc_dev(mmc), "unsupported block size (%d bytes)\n", |
| 442 | mrq->data->blksz); |
Pierre Ossman | 255d01a | 2007-07-24 20:38:53 +0200 | [diff] [blame] | 443 | mrq->cmd->error = -EINVAL; |
| 444 | mmc_request_done(mmc, mrq); |
| 445 | return; |
| 446 | } |
| 447 | |
Linus Walleij | 9e94302 | 2008-10-24 21:17:50 +0100 | [diff] [blame] | 448 | spin_lock_irqsave(&host->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | |
| 450 | host->mrq = mrq; |
| 451 | |
| 452 | if (mrq->data && mrq->data->flags & MMC_DATA_READ) |
| 453 | mmci_start_data(host, mrq->data); |
| 454 | |
| 455 | mmci_start_command(host, mrq->cmd, 0); |
| 456 | |
Linus Walleij | 9e94302 | 2008-10-24 21:17:50 +0100 | [diff] [blame] | 457 | spin_unlock_irqrestore(&host->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 461 | { |
| 462 | struct mmci_host *host = mmc_priv(mmc); |
Linus Walleij | a6a6464 | 2009-09-14 12:56:14 +0100 | [diff] [blame] | 463 | u32 pwr = 0; |
| 464 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | switch (ios->power_mode) { |
| 467 | case MMC_POWER_OFF: |
Linus Walleij | 34e84f3 | 2009-09-22 14:41:40 +0100 | [diff] [blame] | 468 | if(host->vcc && |
| 469 | regulator_is_enabled(host->vcc)) |
| 470 | regulator_disable(host->vcc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | break; |
| 472 | case MMC_POWER_UP: |
Linus Walleij | 34e84f3 | 2009-09-22 14:41:40 +0100 | [diff] [blame] | 473 | #ifdef CONFIG_REGULATOR |
| 474 | if (host->vcc) |
| 475 | /* This implicitly enables the regulator */ |
| 476 | mmc_regulator_set_ocr(host->vcc, ios->vdd); |
| 477 | #endif |
| 478 | /* |
| 479 | * The translate_vdd function is not used if you have |
| 480 | * an external regulator, or your design is really weird. |
| 481 | * Using it would mean sending in power control BOTH using |
| 482 | * a regulator AND the 4 MMCIPWR bits. If we don't have |
| 483 | * a regulator, we might have some other platform specific |
| 484 | * power control behind this translate function. |
| 485 | */ |
| 486 | if (!host->vcc && host->plat->translate_vdd) |
| 487 | pwr |= host->plat->translate_vdd(mmc_dev(mmc), ios->vdd); |
Linus Walleij | cc30d60 | 2009-01-04 15:18:54 +0100 | [diff] [blame] | 488 | /* The ST version does not have this, fall through to POWER_ON */ |
Linus Walleij | f17a1f0 | 2009-08-04 01:01:02 +0100 | [diff] [blame] | 489 | if (host->hw_designer != AMBA_VENDOR_ST) { |
Linus Walleij | cc30d60 | 2009-01-04 15:18:54 +0100 | [diff] [blame] | 490 | pwr |= MCI_PWR_UP; |
| 491 | break; |
| 492 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | case MMC_POWER_ON: |
| 494 | pwr |= MCI_PWR_ON; |
| 495 | break; |
| 496 | } |
| 497 | |
Linus Walleij | cc30d60 | 2009-01-04 15:18:54 +0100 | [diff] [blame] | 498 | if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) { |
Linus Walleij | f17a1f0 | 2009-08-04 01:01:02 +0100 | [diff] [blame] | 499 | if (host->hw_designer != AMBA_VENDOR_ST) |
Linus Walleij | cc30d60 | 2009-01-04 15:18:54 +0100 | [diff] [blame] | 500 | pwr |= MCI_ROD; |
| 501 | else { |
| 502 | /* |
| 503 | * The ST Micro variant use the ROD bit for something |
| 504 | * else and only has OD (Open Drain). |
| 505 | */ |
| 506 | pwr |= MCI_OD; |
| 507 | } |
| 508 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | |
Linus Walleij | a6a6464 | 2009-09-14 12:56:14 +0100 | [diff] [blame] | 510 | spin_lock_irqsave(&host->lock, flags); |
| 511 | |
| 512 | mmci_set_clkreg(host, ios->clock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
| 514 | if (host->pwr != pwr) { |
| 515 | host->pwr = pwr; |
| 516 | writel(pwr, host->base + MMCIPOWER); |
| 517 | } |
Linus Walleij | a6a6464 | 2009-09-14 12:56:14 +0100 | [diff] [blame] | 518 | |
| 519 | spin_unlock_irqrestore(&host->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Russell King | 8900144 | 2009-07-09 15:16:07 +0100 | [diff] [blame] | 522 | static int mmci_get_ro(struct mmc_host *mmc) |
| 523 | { |
| 524 | struct mmci_host *host = mmc_priv(mmc); |
| 525 | |
| 526 | if (host->gpio_wp == -ENOSYS) |
| 527 | return -ENOSYS; |
| 528 | |
| 529 | return gpio_get_value(host->gpio_wp); |
| 530 | } |
| 531 | |
| 532 | static int mmci_get_cd(struct mmc_host *mmc) |
| 533 | { |
| 534 | struct mmci_host *host = mmc_priv(mmc); |
| 535 | unsigned int status; |
| 536 | |
| 537 | if (host->gpio_cd == -ENOSYS) |
| 538 | status = host->plat->status(mmc_dev(host->mmc)); |
| 539 | else |
| 540 | status = gpio_get_value(host->gpio_cd); |
| 541 | |
| 542 | return !status; |
| 543 | } |
| 544 | |
David Brownell | ab7aefd | 2006-11-12 17:55:30 -0800 | [diff] [blame] | 545 | static const struct mmc_host_ops mmci_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | .request = mmci_request, |
| 547 | .set_ios = mmci_set_ios, |
Russell King | 8900144 | 2009-07-09 15:16:07 +0100 | [diff] [blame] | 548 | .get_ro = mmci_get_ro, |
| 549 | .get_cd = mmci_get_cd, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | }; |
| 551 | |
| 552 | static void mmci_check_status(unsigned long data) |
| 553 | { |
| 554 | struct mmci_host *host = (struct mmci_host *)data; |
Russell King | 8900144 | 2009-07-09 15:16:07 +0100 | [diff] [blame] | 555 | unsigned int status = mmci_get_cd(host->mmc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | if (status ^ host->oldstat) |
Richard Purdie | 8dc0033 | 2005-09-08 17:53:01 +0100 | [diff] [blame] | 558 | mmc_detect_change(host->mmc, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | |
| 560 | host->oldstat = status; |
| 561 | mod_timer(&host->timer, jiffies + HZ); |
| 562 | } |
| 563 | |
Alessandro Rubini | 03fbdb1 | 2009-05-20 22:39:08 +0100 | [diff] [blame] | 564 | static int __devinit mmci_probe(struct amba_device *dev, struct amba_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | { |
Linus Walleij | 6ef297f | 2009-09-22 14:29:36 +0100 | [diff] [blame] | 566 | struct mmci_platform_data *plat = dev->dev.platform_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | struct mmci_host *host; |
| 568 | struct mmc_host *mmc; |
| 569 | int ret; |
| 570 | |
| 571 | /* must have platform data */ |
| 572 | if (!plat) { |
| 573 | ret = -EINVAL; |
| 574 | goto out; |
| 575 | } |
| 576 | |
| 577 | ret = amba_request_regions(dev, DRIVER_NAME); |
| 578 | if (ret) |
| 579 | goto out; |
| 580 | |
| 581 | mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev); |
| 582 | if (!mmc) { |
| 583 | ret = -ENOMEM; |
| 584 | goto rel_regions; |
| 585 | } |
| 586 | |
| 587 | host = mmc_priv(mmc); |
Rabin Vincent | 4ea580f | 2009-04-17 08:44:19 +0530 | [diff] [blame] | 588 | host->mmc = mmc; |
Russell King | 012b7d3 | 2009-07-09 15:13:56 +0100 | [diff] [blame] | 589 | |
Russell King | 8900144 | 2009-07-09 15:16:07 +0100 | [diff] [blame] | 590 | host->gpio_wp = -ENOSYS; |
| 591 | host->gpio_cd = -ENOSYS; |
| 592 | |
Russell King | 012b7d3 | 2009-07-09 15:13:56 +0100 | [diff] [blame] | 593 | host->hw_designer = amba_manf(dev); |
| 594 | host->hw_revision = amba_rev(dev); |
Linus Walleij | 64de028 | 2010-02-19 01:09:10 +0100 | [diff] [blame^] | 595 | dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer); |
| 596 | dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision); |
Russell King | 012b7d3 | 2009-07-09 15:13:56 +0100 | [diff] [blame] | 597 | |
Russell King | ee569c4 | 2008-11-30 17:38:14 +0000 | [diff] [blame] | 598 | host->clk = clk_get(&dev->dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | if (IS_ERR(host->clk)) { |
| 600 | ret = PTR_ERR(host->clk); |
| 601 | host->clk = NULL; |
| 602 | goto host_free; |
| 603 | } |
| 604 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | ret = clk_enable(host->clk); |
| 606 | if (ret) |
Russell King | a8d3584 | 2006-01-03 18:41:37 +0000 | [diff] [blame] | 607 | goto clk_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | |
| 609 | host->plat = plat; |
| 610 | host->mclk = clk_get_rate(host->clk); |
Linus Walleij | c8df9a5 | 2008-04-29 09:34:07 +0100 | [diff] [blame] | 611 | /* |
| 612 | * According to the spec, mclk is max 100 MHz, |
| 613 | * so we try to adjust the clock down to this, |
| 614 | * (if possible). |
| 615 | */ |
| 616 | if (host->mclk > 100000000) { |
| 617 | ret = clk_set_rate(host->clk, 100000000); |
| 618 | if (ret < 0) |
| 619 | goto clk_disable; |
| 620 | host->mclk = clk_get_rate(host->clk); |
Linus Walleij | 64de028 | 2010-02-19 01:09:10 +0100 | [diff] [blame^] | 621 | dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n", |
| 622 | host->mclk); |
Linus Walleij | c8df9a5 | 2008-04-29 09:34:07 +0100 | [diff] [blame] | 623 | } |
Linus Walleij | dc890c2 | 2009-06-07 23:27:31 +0100 | [diff] [blame] | 624 | host->base = ioremap(dev->res.start, resource_size(&dev->res)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | if (!host->base) { |
| 626 | ret = -ENOMEM; |
| 627 | goto clk_disable; |
| 628 | } |
| 629 | |
| 630 | mmc->ops = &mmci_ops; |
| 631 | mmc->f_min = (host->mclk + 511) / 512; |
| 632 | mmc->f_max = min(host->mclk, fmax); |
Linus Walleij | 64de028 | 2010-02-19 01:09:10 +0100 | [diff] [blame^] | 633 | dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max); |
| 634 | |
Linus Walleij | 34e84f3 | 2009-09-22 14:41:40 +0100 | [diff] [blame] | 635 | #ifdef CONFIG_REGULATOR |
| 636 | /* If we're using the regulator framework, try to fetch a regulator */ |
| 637 | host->vcc = regulator_get(&dev->dev, "vmmc"); |
| 638 | if (IS_ERR(host->vcc)) |
| 639 | host->vcc = NULL; |
| 640 | else { |
| 641 | int mask = mmc_regulator_get_ocrmask(host->vcc); |
| 642 | |
| 643 | if (mask < 0) |
| 644 | dev_err(&dev->dev, "error getting OCR mask (%d)\n", |
| 645 | mask); |
| 646 | else { |
| 647 | host->mmc->ocr_avail = (u32) mask; |
| 648 | if (plat->ocr_mask) |
| 649 | dev_warn(&dev->dev, |
| 650 | "Provided ocr_mask/setpower will not be used " |
| 651 | "(using regulator instead)\n"); |
| 652 | } |
| 653 | } |
| 654 | #endif |
| 655 | /* Fall back to platform data if no regulator is found */ |
| 656 | if (host->vcc == NULL) |
| 657 | mmc->ocr_avail = plat->ocr_mask; |
Linus Walleij | 9e6c82c | 2009-09-14 12:57:11 +0100 | [diff] [blame] | 658 | mmc->caps = plat->capabilities; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
| 660 | /* |
| 661 | * We can do SGIO |
| 662 | */ |
| 663 | mmc->max_hw_segs = 16; |
| 664 | mmc->max_phys_segs = NR_SG; |
| 665 | |
| 666 | /* |
| 667 | * Since we only have a 16-bit data length register, we must |
| 668 | * ensure that we don't exceed 2^16-1 bytes in a single request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | */ |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 670 | mmc->max_req_size = 65535; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | |
| 672 | /* |
| 673 | * Set the maximum segment size. Since we aren't doing DMA |
| 674 | * (yet) we are only limited by the data length register. |
| 675 | */ |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 676 | mmc->max_seg_size = mmc->max_req_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 678 | /* |
| 679 | * Block size can be up to 2048 bytes, but must be a power of two. |
| 680 | */ |
| 681 | mmc->max_blk_size = 2048; |
| 682 | |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 683 | /* |
| 684 | * No limit on the number of blocks transferred. |
| 685 | */ |
| 686 | mmc->max_blk_count = mmc->max_req_size; |
| 687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | spin_lock_init(&host->lock); |
| 689 | |
| 690 | writel(0, host->base + MMCIMASK0); |
| 691 | writel(0, host->base + MMCIMASK1); |
| 692 | writel(0xfff, host->base + MMCICLEAR); |
| 693 | |
Russell King | 8900144 | 2009-07-09 15:16:07 +0100 | [diff] [blame] | 694 | if (gpio_is_valid(plat->gpio_cd)) { |
| 695 | ret = gpio_request(plat->gpio_cd, DRIVER_NAME " (cd)"); |
| 696 | if (ret == 0) |
| 697 | ret = gpio_direction_input(plat->gpio_cd); |
| 698 | if (ret == 0) |
| 699 | host->gpio_cd = plat->gpio_cd; |
| 700 | else if (ret != -ENOSYS) |
| 701 | goto err_gpio_cd; |
| 702 | } |
| 703 | if (gpio_is_valid(plat->gpio_wp)) { |
| 704 | ret = gpio_request(plat->gpio_wp, DRIVER_NAME " (wp)"); |
| 705 | if (ret == 0) |
| 706 | ret = gpio_direction_input(plat->gpio_wp); |
| 707 | if (ret == 0) |
| 708 | host->gpio_wp = plat->gpio_wp; |
| 709 | else if (ret != -ENOSYS) |
| 710 | goto err_gpio_wp; |
| 711 | } |
| 712 | |
Thomas Gleixner | dace145 | 2006-07-01 19:29:38 -0700 | [diff] [blame] | 713 | ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | if (ret) |
| 715 | goto unmap; |
| 716 | |
Thomas Gleixner | dace145 | 2006-07-01 19:29:38 -0700 | [diff] [blame] | 717 | ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED, DRIVER_NAME " (pio)", host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | if (ret) |
| 719 | goto irq0_free; |
| 720 | |
| 721 | writel(MCI_IRQENABLE, host->base + MMCIMASK0); |
| 722 | |
| 723 | amba_set_drvdata(dev, mmc); |
Russell King | 8900144 | 2009-07-09 15:16:07 +0100 | [diff] [blame] | 724 | host->oldstat = mmci_get_cd(host->mmc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | |
| 726 | mmc_add_host(mmc); |
| 727 | |
Linus Walleij | 64de028 | 2010-02-19 01:09:10 +0100 | [diff] [blame^] | 728 | dev_info(&dev->dev, "%s: MMCI rev %x cfg %02x at 0x%016llx irq %d,%d\n", |
Russell King | d366b64 | 2005-08-19 09:40:08 +0100 | [diff] [blame] | 729 | mmc_hostname(mmc), amba_rev(dev), amba_config(dev), |
Greg Kroah-Hartman | e29419f | 2006-06-12 15:20:16 -0700 | [diff] [blame] | 730 | (unsigned long long)dev->res.start, dev->irq[0], dev->irq[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | |
| 732 | init_timer(&host->timer); |
| 733 | host->timer.data = (unsigned long)host; |
| 734 | host->timer.function = mmci_check_status; |
| 735 | host->timer.expires = jiffies + HZ; |
| 736 | add_timer(&host->timer); |
| 737 | |
| 738 | return 0; |
| 739 | |
| 740 | irq0_free: |
| 741 | free_irq(dev->irq[0], host); |
| 742 | unmap: |
Russell King | 8900144 | 2009-07-09 15:16:07 +0100 | [diff] [blame] | 743 | if (host->gpio_wp != -ENOSYS) |
| 744 | gpio_free(host->gpio_wp); |
| 745 | err_gpio_wp: |
| 746 | if (host->gpio_cd != -ENOSYS) |
| 747 | gpio_free(host->gpio_cd); |
| 748 | err_gpio_cd: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | iounmap(host->base); |
| 750 | clk_disable: |
| 751 | clk_disable(host->clk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | clk_free: |
| 753 | clk_put(host->clk); |
| 754 | host_free: |
| 755 | mmc_free_host(mmc); |
| 756 | rel_regions: |
| 757 | amba_release_regions(dev); |
| 758 | out: |
| 759 | return ret; |
| 760 | } |
| 761 | |
Linus Walleij | 6dc4a47 | 2009-03-07 00:23:52 +0100 | [diff] [blame] | 762 | static int __devexit mmci_remove(struct amba_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | { |
| 764 | struct mmc_host *mmc = amba_get_drvdata(dev); |
| 765 | |
| 766 | amba_set_drvdata(dev, NULL); |
| 767 | |
| 768 | if (mmc) { |
| 769 | struct mmci_host *host = mmc_priv(mmc); |
| 770 | |
| 771 | del_timer_sync(&host->timer); |
| 772 | |
| 773 | mmc_remove_host(mmc); |
| 774 | |
| 775 | writel(0, host->base + MMCIMASK0); |
| 776 | writel(0, host->base + MMCIMASK1); |
| 777 | |
| 778 | writel(0, host->base + MMCICOMMAND); |
| 779 | writel(0, host->base + MMCIDATACTRL); |
| 780 | |
| 781 | free_irq(dev->irq[0], host); |
| 782 | free_irq(dev->irq[1], host); |
| 783 | |
Russell King | 8900144 | 2009-07-09 15:16:07 +0100 | [diff] [blame] | 784 | if (host->gpio_wp != -ENOSYS) |
| 785 | gpio_free(host->gpio_wp); |
| 786 | if (host->gpio_cd != -ENOSYS) |
| 787 | gpio_free(host->gpio_cd); |
| 788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | iounmap(host->base); |
| 790 | clk_disable(host->clk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | clk_put(host->clk); |
| 792 | |
Linus Walleij | 34e84f3 | 2009-09-22 14:41:40 +0100 | [diff] [blame] | 793 | if (regulator_is_enabled(host->vcc)) |
| 794 | regulator_disable(host->vcc); |
| 795 | regulator_put(host->vcc); |
| 796 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | mmc_free_host(mmc); |
| 798 | |
| 799 | amba_release_regions(dev); |
| 800 | } |
| 801 | |
| 802 | return 0; |
| 803 | } |
| 804 | |
| 805 | #ifdef CONFIG_PM |
Pavel Machek | e5378ca | 2005-04-16 15:25:29 -0700 | [diff] [blame] | 806 | static int mmci_suspend(struct amba_device *dev, pm_message_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | { |
| 808 | struct mmc_host *mmc = amba_get_drvdata(dev); |
| 809 | int ret = 0; |
| 810 | |
| 811 | if (mmc) { |
| 812 | struct mmci_host *host = mmc_priv(mmc); |
| 813 | |
| 814 | ret = mmc_suspend_host(mmc, state); |
| 815 | if (ret == 0) |
| 816 | writel(0, host->base + MMCIMASK0); |
| 817 | } |
| 818 | |
| 819 | return ret; |
| 820 | } |
| 821 | |
| 822 | static int mmci_resume(struct amba_device *dev) |
| 823 | { |
| 824 | struct mmc_host *mmc = amba_get_drvdata(dev); |
| 825 | int ret = 0; |
| 826 | |
| 827 | if (mmc) { |
| 828 | struct mmci_host *host = mmc_priv(mmc); |
| 829 | |
| 830 | writel(MCI_IRQENABLE, host->base + MMCIMASK0); |
| 831 | |
| 832 | ret = mmc_resume_host(mmc); |
| 833 | } |
| 834 | |
| 835 | return ret; |
| 836 | } |
| 837 | #else |
| 838 | #define mmci_suspend NULL |
| 839 | #define mmci_resume NULL |
| 840 | #endif |
| 841 | |
| 842 | static struct amba_id mmci_ids[] = { |
| 843 | { |
| 844 | .id = 0x00041180, |
| 845 | .mask = 0x000fffff, |
| 846 | }, |
| 847 | { |
| 848 | .id = 0x00041181, |
| 849 | .mask = 0x000fffff, |
| 850 | }, |
Linus Walleij | cc30d60 | 2009-01-04 15:18:54 +0100 | [diff] [blame] | 851 | /* ST Micro variants */ |
| 852 | { |
| 853 | .id = 0x00180180, |
| 854 | .mask = 0x00ffffff, |
| 855 | }, |
| 856 | { |
| 857 | .id = 0x00280180, |
| 858 | .mask = 0x00ffffff, |
| 859 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | { 0, 0 }, |
| 861 | }; |
| 862 | |
| 863 | static struct amba_driver mmci_driver = { |
| 864 | .drv = { |
| 865 | .name = DRIVER_NAME, |
| 866 | }, |
| 867 | .probe = mmci_probe, |
Linus Walleij | 6dc4a47 | 2009-03-07 00:23:52 +0100 | [diff] [blame] | 868 | .remove = __devexit_p(mmci_remove), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | .suspend = mmci_suspend, |
| 870 | .resume = mmci_resume, |
| 871 | .id_table = mmci_ids, |
| 872 | }; |
| 873 | |
| 874 | static int __init mmci_init(void) |
| 875 | { |
| 876 | return amba_driver_register(&mmci_driver); |
| 877 | } |
| 878 | |
| 879 | static void __exit mmci_exit(void) |
| 880 | { |
| 881 | amba_driver_unregister(&mmci_driver); |
| 882 | } |
| 883 | |
| 884 | module_init(mmci_init); |
| 885 | module_exit(mmci_exit); |
| 886 | module_param(fmax, uint, 0444); |
| 887 | |
| 888 | MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver"); |
| 889 | MODULE_LICENSE("GPL"); |