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