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