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