aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mmc/core
diff options
context:
space:
mode:
authorGlenn Elliott <gelliott@cs.unc.edu>2012-03-04 19:47:13 -0500
committerGlenn Elliott <gelliott@cs.unc.edu>2012-03-04 19:47:13 -0500
commitc71c03bda1e86c9d5198c5d83f712e695c4f2a1e (patch)
treeecb166cb3e2b7e2adb3b5e292245fefd23381ac8 /drivers/mmc/core
parentea53c912f8a86a8567697115b6a0d8152beee5c8 (diff)
parent6a00f206debf8a5c8899055726ad127dbeeed098 (diff)
Merge branch 'mpi-master' into wip-k-fmlpwip-k-fmlp
Conflicts: litmus/sched_cedf.c
Diffstat (limited to 'drivers/mmc/core')
-rw-r--r--drivers/mmc/core/Kconfig11
-rw-r--r--drivers/mmc/core/Makefile7
-rw-r--r--drivers/mmc/core/bus.c78
-rw-r--r--drivers/mmc/core/bus.h2
-rw-r--r--drivers/mmc/core/core.c379
-rw-r--r--drivers/mmc/core/core.h17
-rw-r--r--drivers/mmc/core/debugfs.c41
-rw-r--r--drivers/mmc/core/host.c210
-rw-r--r--drivers/mmc/core/host.h21
-rw-r--r--drivers/mmc/core/mmc.c400
-rw-r--r--drivers/mmc/core/mmc_ops.c169
-rw-r--r--drivers/mmc/core/mmc_ops.h2
-rw-r--r--drivers/mmc/core/quirks.c79
-rw-r--r--drivers/mmc/core/sd.c432
-rw-r--r--drivers/mmc/core/sd.h2
-rw-r--r--drivers/mmc/core/sd_ops.c63
-rw-r--r--drivers/mmc/core/sdio.c160
-rw-r--r--drivers/mmc/core/sdio_bus.c66
-rw-r--r--drivers/mmc/core/sdio_irq.c35
-rw-r--r--drivers/mmc/core/sdio_ops.c18
20 files changed, 1844 insertions, 348 deletions
diff --git a/drivers/mmc/core/Kconfig b/drivers/mmc/core/Kconfig
index bb22ffd76ef8..ef103871517f 100644
--- a/drivers/mmc/core/Kconfig
+++ b/drivers/mmc/core/Kconfig
@@ -16,3 +16,14 @@ config MMC_UNSAFE_RESUME
16 16
17 This option sets a default which can be overridden by the 17 This option sets a default which can be overridden by the
18 module parameter "removable=0" or "removable=1". 18 module parameter "removable=0" or "removable=1".
19
20config MMC_CLKGATE
21 bool "MMC host clock gating (EXPERIMENTAL)"
22 depends on EXPERIMENTAL
23 help
24 This will attempt to aggressively gate the clock to the MMC card.
25 This is done to save power due to gating off the logic and bus
26 noise when the MMC card is not in use. Your host driver has to
27 support handling this in order for it to be of any use.
28
29 If unsure, say N.
diff --git a/drivers/mmc/core/Makefile b/drivers/mmc/core/Makefile
index 889e5f898f6f..639501970b41 100644
--- a/drivers/mmc/core/Makefile
+++ b/drivers/mmc/core/Makefile
@@ -2,14 +2,11 @@
2# Makefile for the kernel mmc core. 2# Makefile for the kernel mmc core.
3# 3#
4 4
5ifeq ($(CONFIG_MMC_DEBUG),y)
6 EXTRA_CFLAGS += -DDEBUG
7endif
8
9obj-$(CONFIG_MMC) += mmc_core.o 5obj-$(CONFIG_MMC) += mmc_core.o
10mmc_core-y := core.o bus.o host.o \ 6mmc_core-y := core.o bus.o host.o \
11 mmc.o mmc_ops.o sd.o sd_ops.o \ 7 mmc.o mmc_ops.o sd.o sd_ops.o \
12 sdio.o sdio_ops.o sdio_bus.o \ 8 sdio.o sdio_ops.o sdio_bus.o \
13 sdio_cis.o sdio_io.o sdio_irq.o 9 sdio_cis.o sdio_io.o sdio_irq.o \
10 quirks.o
14 11
15mmc_core-$(CONFIG_DEBUG_FS) += debugfs.o 12mmc_core-$(CONFIG_DEBUG_FS) += debugfs.o
diff --git a/drivers/mmc/core/bus.c b/drivers/mmc/core/bus.c
index 7cd9749dc21d..393d817ed040 100644
--- a/drivers/mmc/core/bus.c
+++ b/drivers/mmc/core/bus.c
@@ -14,6 +14,7 @@
14#include <linux/device.h> 14#include <linux/device.h>
15#include <linux/err.h> 15#include <linux/err.h>
16#include <linux/slab.h> 16#include <linux/slab.h>
17#include <linux/pm_runtime.h>
17 18
18#include <linux/mmc/card.h> 19#include <linux/mmc/card.h>
19#include <linux/mmc/host.h> 20#include <linux/mmc/host.h>
@@ -22,13 +23,12 @@
22#include "sdio_cis.h" 23#include "sdio_cis.h"
23#include "bus.h" 24#include "bus.h"
24 25
25#define dev_to_mmc_card(d) container_of(d, struct mmc_card, dev)
26#define to_mmc_driver(d) container_of(d, struct mmc_driver, drv) 26#define to_mmc_driver(d) container_of(d, struct mmc_driver, drv)
27 27
28static ssize_t mmc_type_show(struct device *dev, 28static ssize_t mmc_type_show(struct device *dev,
29 struct device_attribute *attr, char *buf) 29 struct device_attribute *attr, char *buf)
30{ 30{
31 struct mmc_card *card = dev_to_mmc_card(dev); 31 struct mmc_card *card = mmc_dev_to_card(dev);
32 32
33 switch (card->type) { 33 switch (card->type) {
34 case MMC_TYPE_MMC: 34 case MMC_TYPE_MMC:
@@ -62,7 +62,7 @@ static int mmc_bus_match(struct device *dev, struct device_driver *drv)
62static int 62static int
63mmc_bus_uevent(struct device *dev, struct kobj_uevent_env *env) 63mmc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
64{ 64{
65 struct mmc_card *card = dev_to_mmc_card(dev); 65 struct mmc_card *card = mmc_dev_to_card(dev);
66 const char *type; 66 const char *type;
67 int retval = 0; 67 int retval = 0;
68 68
@@ -105,7 +105,7 @@ mmc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
105static int mmc_bus_probe(struct device *dev) 105static int mmc_bus_probe(struct device *dev)
106{ 106{
107 struct mmc_driver *drv = to_mmc_driver(dev->driver); 107 struct mmc_driver *drv = to_mmc_driver(dev->driver);
108 struct mmc_card *card = dev_to_mmc_card(dev); 108 struct mmc_card *card = mmc_dev_to_card(dev);
109 109
110 return drv->probe(card); 110 return drv->probe(card);
111} 111}
@@ -113,7 +113,7 @@ static int mmc_bus_probe(struct device *dev)
113static int mmc_bus_remove(struct device *dev) 113static int mmc_bus_remove(struct device *dev)
114{ 114{
115 struct mmc_driver *drv = to_mmc_driver(dev->driver); 115 struct mmc_driver *drv = to_mmc_driver(dev->driver);
116 struct mmc_card *card = dev_to_mmc_card(dev); 116 struct mmc_card *card = mmc_dev_to_card(dev);
117 117
118 drv->remove(card); 118 drv->remove(card);
119 119
@@ -123,7 +123,7 @@ static int mmc_bus_remove(struct device *dev)
123static int mmc_bus_suspend(struct device *dev, pm_message_t state) 123static int mmc_bus_suspend(struct device *dev, pm_message_t state)
124{ 124{
125 struct mmc_driver *drv = to_mmc_driver(dev->driver); 125 struct mmc_driver *drv = to_mmc_driver(dev->driver);
126 struct mmc_card *card = dev_to_mmc_card(dev); 126 struct mmc_card *card = mmc_dev_to_card(dev);
127 int ret = 0; 127 int ret = 0;
128 128
129 if (dev->driver && drv->suspend) 129 if (dev->driver && drv->suspend)
@@ -134,7 +134,7 @@ static int mmc_bus_suspend(struct device *dev, pm_message_t state)
134static int mmc_bus_resume(struct device *dev) 134static int mmc_bus_resume(struct device *dev)
135{ 135{
136 struct mmc_driver *drv = to_mmc_driver(dev->driver); 136 struct mmc_driver *drv = to_mmc_driver(dev->driver);
137 struct mmc_card *card = dev_to_mmc_card(dev); 137 struct mmc_card *card = mmc_dev_to_card(dev);
138 int ret = 0; 138 int ret = 0;
139 139
140 if (dev->driver && drv->resume) 140 if (dev->driver && drv->resume)
@@ -142,6 +142,41 @@ static int mmc_bus_resume(struct device *dev)
142 return ret; 142 return ret;
143} 143}
144 144
145#ifdef CONFIG_PM_RUNTIME
146
147static int mmc_runtime_suspend(struct device *dev)
148{
149 struct mmc_card *card = mmc_dev_to_card(dev);
150
151 return mmc_power_save_host(card->host);
152}
153
154static int mmc_runtime_resume(struct device *dev)
155{
156 struct mmc_card *card = mmc_dev_to_card(dev);
157
158 return mmc_power_restore_host(card->host);
159}
160
161static int mmc_runtime_idle(struct device *dev)
162{
163 return pm_runtime_suspend(dev);
164}
165
166static const struct dev_pm_ops mmc_bus_pm_ops = {
167 .runtime_suspend = mmc_runtime_suspend,
168 .runtime_resume = mmc_runtime_resume,
169 .runtime_idle = mmc_runtime_idle,
170};
171
172#define MMC_PM_OPS_PTR (&mmc_bus_pm_ops)
173
174#else /* !CONFIG_PM_RUNTIME */
175
176#define MMC_PM_OPS_PTR NULL
177
178#endif /* !CONFIG_PM_RUNTIME */
179
145static struct bus_type mmc_bus_type = { 180static struct bus_type mmc_bus_type = {
146 .name = "mmc", 181 .name = "mmc",
147 .dev_attrs = mmc_dev_attrs, 182 .dev_attrs = mmc_dev_attrs,
@@ -151,6 +186,7 @@ static struct bus_type mmc_bus_type = {
151 .remove = mmc_bus_remove, 186 .remove = mmc_bus_remove,
152 .suspend = mmc_bus_suspend, 187 .suspend = mmc_bus_suspend,
153 .resume = mmc_bus_resume, 188 .resume = mmc_bus_resume,
189 .pm = MMC_PM_OPS_PTR,
154}; 190};
155 191
156int mmc_register_bus(void) 192int mmc_register_bus(void)
@@ -189,7 +225,7 @@ EXPORT_SYMBOL(mmc_unregister_driver);
189 225
190static void mmc_release_card(struct device *dev) 226static void mmc_release_card(struct device *dev)
191{ 227{
192 struct mmc_card *card = dev_to_mmc_card(dev); 228 struct mmc_card *card = mmc_dev_to_card(dev);
193 229
194 sdio_free_common_cis(card); 230 sdio_free_common_cis(card);
195 231
@@ -238,8 +274,12 @@ int mmc_add_card(struct mmc_card *card)
238 break; 274 break;
239 case MMC_TYPE_SD: 275 case MMC_TYPE_SD:
240 type = "SD"; 276 type = "SD";
241 if (mmc_card_blockaddr(card)) 277 if (mmc_card_blockaddr(card)) {
242 type = "SDHC"; 278 if (mmc_card_ext_capacity(card))
279 type = "SDXC";
280 else
281 type = "SDHC";
282 }
243 break; 283 break;
244 case MMC_TYPE_SDIO: 284 case MMC_TYPE_SDIO:
245 type = "SDIO"; 285 type = "SDIO";
@@ -248,31 +288,35 @@ int mmc_add_card(struct mmc_card *card)
248 type = "SD-combo"; 288 type = "SD-combo";
249 if (mmc_card_blockaddr(card)) 289 if (mmc_card_blockaddr(card))
250 type = "SDHC-combo"; 290 type = "SDHC-combo";
291 break;
251 default: 292 default:
252 type = "?"; 293 type = "?";
253 break; 294 break;
254 } 295 }
255 296
256 if (mmc_host_is_spi(card->host)) { 297 if (mmc_host_is_spi(card->host)) {
257 printk(KERN_INFO "%s: new %s%s card on SPI\n", 298 printk(KERN_INFO "%s: new %s%s%s card on SPI\n",
258 mmc_hostname(card->host), 299 mmc_hostname(card->host),
259 mmc_card_highspeed(card) ? "high speed " : "", 300 mmc_card_highspeed(card) ? "high speed " : "",
301 mmc_card_ddr_mode(card) ? "DDR " : "",
260 type); 302 type);
261 } else { 303 } else {
262 printk(KERN_INFO "%s: new %s%s card at address %04x\n", 304 printk(KERN_INFO "%s: new %s%s%s card at address %04x\n",
263 mmc_hostname(card->host), 305 mmc_hostname(card->host),
264 mmc_card_highspeed(card) ? "high speed " : "", 306 mmc_sd_card_uhs(card) ? "ultra high speed " :
307 (mmc_card_highspeed(card) ? "high speed " : ""),
308 mmc_card_ddr_mode(card) ? "DDR " : "",
265 type, card->rca); 309 type, card->rca);
266 } 310 }
267 311
268 ret = device_add(&card->dev);
269 if (ret)
270 return ret;
271
272#ifdef CONFIG_DEBUG_FS 312#ifdef CONFIG_DEBUG_FS
273 mmc_add_card_debugfs(card); 313 mmc_add_card_debugfs(card);
274#endif 314#endif
275 315
316 ret = device_add(&card->dev);
317 if (ret)
318 return ret;
319
276 mmc_card_set_present(card); 320 mmc_card_set_present(card);
277 321
278 return 0; 322 return 0;
diff --git a/drivers/mmc/core/bus.h b/drivers/mmc/core/bus.h
index 18178766ab46..00a19710b6b4 100644
--- a/drivers/mmc/core/bus.h
+++ b/drivers/mmc/core/bus.h
@@ -14,7 +14,7 @@
14#define MMC_DEV_ATTR(name, fmt, args...) \ 14#define MMC_DEV_ATTR(name, fmt, args...) \
15static ssize_t mmc_##name##_show (struct device *dev, struct device_attribute *attr, char *buf) \ 15static ssize_t mmc_##name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
16{ \ 16{ \
17 struct mmc_card *card = container_of(dev, struct mmc_card, dev); \ 17 struct mmc_card *card = mmc_dev_to_card(dev); \
18 return sprintf(buf, fmt, args); \ 18 return sprintf(buf, fmt, args); \
19} \ 19} \
20static DEVICE_ATTR(name, S_IRUGO, mmc_##name##_show, NULL) 20static DEVICE_ATTR(name, S_IRUGO, mmc_##name##_show, NULL)
diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c
index 09eee6df0653..7843efe22359 100644
--- a/drivers/mmc/core/core.c
+++ b/drivers/mmc/core/core.c
@@ -22,6 +22,7 @@
22#include <linux/scatterlist.h> 22#include <linux/scatterlist.h>
23#include <linux/log2.h> 23#include <linux/log2.h>
24#include <linux/regulator/consumer.h> 24#include <linux/regulator/consumer.h>
25#include <linux/pm_runtime.h>
25 26
26#include <linux/mmc/card.h> 27#include <linux/mmc/card.h>
27#include <linux/mmc/host.h> 28#include <linux/mmc/host.h>
@@ -58,6 +59,7 @@ int mmc_assume_removable;
58#else 59#else
59int mmc_assume_removable = 1; 60int mmc_assume_removable = 1;
60#endif 61#endif
62EXPORT_SYMBOL(mmc_assume_removable);
61module_param_named(removable, mmc_assume_removable, bool, 0644); 63module_param_named(removable, mmc_assume_removable, bool, 0644);
62MODULE_PARM_DESC( 64MODULE_PARM_DESC(
63 removable, 65 removable,
@@ -129,6 +131,8 @@ void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
129 131
130 if (mrq->done) 132 if (mrq->done)
131 mrq->done(mrq); 133 mrq->done(mrq);
134
135 mmc_host_clk_gate(host);
132 } 136 }
133} 137}
134 138
@@ -163,8 +167,6 @@ mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
163 167
164 WARN_ON(!host->claimed); 168 WARN_ON(!host->claimed);
165 169
166 led_trigger_event(host->led, LED_FULL);
167
168 mrq->cmd->error = 0; 170 mrq->cmd->error = 0;
169 mrq->cmd->mrq = mrq; 171 mrq->cmd->mrq = mrq;
170 if (mrq->data) { 172 if (mrq->data) {
@@ -189,6 +191,8 @@ mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
189 mrq->stop->mrq = mrq; 191 mrq->stop->mrq = mrq;
190 } 192 }
191 } 193 }
194 mmc_host_clk_ungate(host);
195 led_trigger_event(host->led, LED_FULL);
192 host->ops->request(host, mrq); 196 host->ops->request(host, mrq);
193} 197}
194 198
@@ -232,12 +236,10 @@ EXPORT_SYMBOL(mmc_wait_for_req);
232 */ 236 */
233int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries) 237int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
234{ 238{
235 struct mmc_request mrq; 239 struct mmc_request mrq = {0};
236 240
237 WARN_ON(!host->claimed); 241 WARN_ON(!host->claimed);
238 242
239 memset(&mrq, 0, sizeof(struct mmc_request));
240
241 memset(cmd->resp, 0, sizeof(cmd->resp)); 243 memset(cmd->resp, 0, sizeof(cmd->resp));
242 cmd->retries = retries; 244 cmd->retries = retries;
243 245
@@ -294,8 +296,9 @@ void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
294 unsigned int timeout_us, limit_us; 296 unsigned int timeout_us, limit_us;
295 297
296 timeout_us = data->timeout_ns / 1000; 298 timeout_us = data->timeout_ns / 1000;
297 timeout_us += data->timeout_clks * 1000 / 299 if (mmc_host_clk_rate(card->host))
298 (card->host->ios.clock / 1000); 300 timeout_us += data->timeout_clks * 1000 /
301 (mmc_host_clk_rate(card->host) / 1000);
299 302
300 if (data->flags & MMC_DATA_WRITE) 303 if (data->flags & MMC_DATA_WRITE)
301 /* 304 /*
@@ -522,7 +525,14 @@ int mmc_try_claim_host(struct mmc_host *host)
522} 525}
523EXPORT_SYMBOL(mmc_try_claim_host); 526EXPORT_SYMBOL(mmc_try_claim_host);
524 527
525static void mmc_do_release_host(struct mmc_host *host) 528/**
529 * mmc_do_release_host - release a claimed host
530 * @host: mmc host to release
531 *
532 * If you successfully claimed a host, this function will
533 * release it again.
534 */
535void mmc_do_release_host(struct mmc_host *host)
526{ 536{
527 unsigned long flags; 537 unsigned long flags;
528 538
@@ -537,6 +547,7 @@ static void mmc_do_release_host(struct mmc_host *host)
537 wake_up(&host->wq); 547 wake_up(&host->wq);
538 } 548 }
539} 549}
550EXPORT_SYMBOL(mmc_do_release_host);
540 551
541void mmc_host_deeper_disable(struct work_struct *work) 552void mmc_host_deeper_disable(struct work_struct *work)
542{ 553{
@@ -613,6 +624,8 @@ static inline void mmc_set_ios(struct mmc_host *host)
613 ios->power_mode, ios->chip_select, ios->vdd, 624 ios->power_mode, ios->chip_select, ios->vdd,
614 ios->bus_width, ios->timing); 625 ios->bus_width, ios->timing);
615 626
627 if (ios->clock > 0)
628 mmc_set_ungated(host);
616 host->ops->set_ios(host, ios); 629 host->ops->set_ios(host, ios);
617} 630}
618 631
@@ -640,6 +653,61 @@ void mmc_set_clock(struct mmc_host *host, unsigned int hz)
640 mmc_set_ios(host); 653 mmc_set_ios(host);
641} 654}
642 655
656#ifdef CONFIG_MMC_CLKGATE
657/*
658 * This gates the clock by setting it to 0 Hz.
659 */
660void mmc_gate_clock(struct mmc_host *host)
661{
662 unsigned long flags;
663
664 spin_lock_irqsave(&host->clk_lock, flags);
665 host->clk_old = host->ios.clock;
666 host->ios.clock = 0;
667 host->clk_gated = true;
668 spin_unlock_irqrestore(&host->clk_lock, flags);
669 mmc_set_ios(host);
670}
671
672/*
673 * This restores the clock from gating by using the cached
674 * clock value.
675 */
676void mmc_ungate_clock(struct mmc_host *host)
677{
678 /*
679 * We should previously have gated the clock, so the clock shall
680 * be 0 here! The clock may however be 0 during initialization,
681 * when some request operations are performed before setting
682 * the frequency. When ungate is requested in that situation
683 * we just ignore the call.
684 */
685 if (host->clk_old) {
686 BUG_ON(host->ios.clock);
687 /* This call will also set host->clk_gated to false */
688 mmc_set_clock(host, host->clk_old);
689 }
690}
691
692void mmc_set_ungated(struct mmc_host *host)
693{
694 unsigned long flags;
695
696 /*
697 * We've been given a new frequency while the clock is gated,
698 * so make sure we regard this as ungating it.
699 */
700 spin_lock_irqsave(&host->clk_lock, flags);
701 host->clk_gated = false;
702 spin_unlock_irqrestore(&host->clk_lock, flags);
703}
704
705#else
706void mmc_set_ungated(struct mmc_host *host)
707{
708}
709#endif
710
643/* 711/*
644 * Change the bus mode (open drain/push-pull) of a host. 712 * Change the bus mode (open drain/push-pull) of a host.
645 */ 713 */
@@ -771,8 +839,9 @@ EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
771 839
772/** 840/**
773 * mmc_regulator_set_ocr - set regulator to match host->ios voltage 841 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
774 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd) 842 * @mmc: the host to regulate
775 * @supply: regulator to use 843 * @supply: regulator to use
844 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
776 * 845 *
777 * Returns zero on success, else negative errno. 846 * Returns zero on success, else negative errno.
778 * 847 *
@@ -780,15 +849,12 @@ EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
780 * a particular supply voltage. This would normally be called from the 849 * a particular supply voltage. This would normally be called from the
781 * set_ios() method. 850 * set_ios() method.
782 */ 851 */
783int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit) 852int mmc_regulator_set_ocr(struct mmc_host *mmc,
853 struct regulator *supply,
854 unsigned short vdd_bit)
784{ 855{
785 int result = 0; 856 int result = 0;
786 int min_uV, max_uV; 857 int min_uV, max_uV;
787 int enabled;
788
789 enabled = regulator_is_enabled(supply);
790 if (enabled < 0)
791 return enabled;
792 858
793 if (vdd_bit) { 859 if (vdd_bit) {
794 int tmp; 860 int tmp;
@@ -819,17 +885,25 @@ int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit)
819 else 885 else
820 result = 0; 886 result = 0;
821 887
822 if (result == 0 && !enabled) 888 if (result == 0 && !mmc->regulator_enabled) {
823 result = regulator_enable(supply); 889 result = regulator_enable(supply);
824 } else if (enabled) { 890 if (!result)
891 mmc->regulator_enabled = true;
892 }
893 } else if (mmc->regulator_enabled) {
825 result = regulator_disable(supply); 894 result = regulator_disable(supply);
895 if (result == 0)
896 mmc->regulator_enabled = false;
826 } 897 }
827 898
899 if (result)
900 dev_err(mmc_dev(mmc),
901 "could not set regulator OCR (%d)\n", result);
828 return result; 902 return result;
829} 903}
830EXPORT_SYMBOL(mmc_regulator_set_ocr); 904EXPORT_SYMBOL(mmc_regulator_set_ocr);
831 905
832#endif 906#endif /* CONFIG_REGULATOR */
833 907
834/* 908/*
835 * Mask off any voltages we don't support and select 909 * Mask off any voltages we don't support and select
@@ -858,6 +932,38 @@ u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
858 return ocr; 932 return ocr;
859} 933}
860 934
935int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, bool cmd11)
936{
937 struct mmc_command cmd = {0};
938 int err = 0;
939
940 BUG_ON(!host);
941
942 /*
943 * Send CMD11 only if the request is to switch the card to
944 * 1.8V signalling.
945 */
946 if ((signal_voltage != MMC_SIGNAL_VOLTAGE_330) && cmd11) {
947 cmd.opcode = SD_SWITCH_VOLTAGE;
948 cmd.arg = 0;
949 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
950
951 err = mmc_wait_for_cmd(host, &cmd, 0);
952 if (err)
953 return err;
954
955 if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
956 return -EIO;
957 }
958
959 host->ios.signal_voltage = signal_voltage;
960
961 if (host->ops->start_signal_voltage_switch)
962 err = host->ops->start_signal_voltage_switch(host, &host->ios);
963
964 return err;
965}
966
861/* 967/*
862 * Select timing parameters for host. 968 * Select timing parameters for host.
863 */ 969 */
@@ -868,6 +974,15 @@ void mmc_set_timing(struct mmc_host *host, unsigned int timing)
868} 974}
869 975
870/* 976/*
977 * Select appropriate driver type for host.
978 */
979void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
980{
981 host->ios.drv_type = drv_type;
982 mmc_set_ios(host);
983}
984
985/*
871 * Apply power to the MMC stack. This is a two-stage process. 986 * Apply power to the MMC stack. This is a two-stage process.
872 * First, we enable power to the card without the clock running. 987 * First, we enable power to the card without the clock running.
873 * We then wait a bit for the power to stabilise. Finally, 988 * We then wait a bit for the power to stabilise. Finally,
@@ -907,12 +1022,7 @@ static void mmc_power_up(struct mmc_host *host)
907 */ 1022 */
908 mmc_delay(10); 1023 mmc_delay(10);
909 1024
910 if (host->f_min > 400000) { 1025 host->ios.clock = host->f_init;
911 pr_warning("%s: Minimum clock frequency too high for "
912 "identification mode\n", mmc_hostname(host));
913 host->ios.clock = host->f_min;
914 } else
915 host->ios.clock = 400000;
916 1026
917 host->ios.power_mode = MMC_POWER_ON; 1027 host->ios.power_mode = MMC_POWER_ON;
918 mmc_set_ios(host); 1028 mmc_set_ios(host);
@@ -928,6 +1038,13 @@ static void mmc_power_off(struct mmc_host *host)
928{ 1038{
929 host->ios.clock = 0; 1039 host->ios.clock = 0;
930 host->ios.vdd = 0; 1040 host->ios.vdd = 0;
1041
1042 /*
1043 * Reset ocr mask to be the highest possible voltage supported for
1044 * this mmc host. This value will be used at next power up.
1045 */
1046 host->ocr = 1 << (fls(host->ocr_avail) - 1);
1047
931 if (!mmc_host_is_spi(host)) { 1048 if (!mmc_host_is_spi(host)) {
932 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; 1049 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
933 host->ios.chip_select = MMC_CS_DONTCARE; 1050 host->ios.chip_select = MMC_CS_DONTCARE;
@@ -1099,9 +1216,8 @@ void mmc_init_erase(struct mmc_card *card)
1099 } 1216 }
1100} 1217}
1101 1218
1102static void mmc_set_mmc_erase_timeout(struct mmc_card *card, 1219static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
1103 struct mmc_command *cmd, 1220 unsigned int arg, unsigned int qty)
1104 unsigned int arg, unsigned int qty)
1105{ 1221{
1106 unsigned int erase_timeout; 1222 unsigned int erase_timeout;
1107 1223
@@ -1129,7 +1245,7 @@ static void mmc_set_mmc_erase_timeout(struct mmc_card *card,
1129 */ 1245 */
1130 timeout_clks <<= 1; 1246 timeout_clks <<= 1;
1131 timeout_us += (timeout_clks * 1000) / 1247 timeout_us += (timeout_clks * 1000) /
1132 (card->host->ios.clock / 1000); 1248 (mmc_host_clk_rate(card->host) / 1000);
1133 1249
1134 erase_timeout = timeout_us / 1000; 1250 erase_timeout = timeout_us / 1000;
1135 1251
@@ -1158,44 +1274,48 @@ static void mmc_set_mmc_erase_timeout(struct mmc_card *card,
1158 if (mmc_host_is_spi(card->host) && erase_timeout < 1000) 1274 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1159 erase_timeout = 1000; 1275 erase_timeout = 1000;
1160 1276
1161 cmd->erase_timeout = erase_timeout; 1277 return erase_timeout;
1162} 1278}
1163 1279
1164static void mmc_set_sd_erase_timeout(struct mmc_card *card, 1280static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
1165 struct mmc_command *cmd, unsigned int arg, 1281 unsigned int arg,
1166 unsigned int qty) 1282 unsigned int qty)
1167{ 1283{
1284 unsigned int erase_timeout;
1285
1168 if (card->ssr.erase_timeout) { 1286 if (card->ssr.erase_timeout) {
1169 /* Erase timeout specified in SD Status Register (SSR) */ 1287 /* Erase timeout specified in SD Status Register (SSR) */
1170 cmd->erase_timeout = card->ssr.erase_timeout * qty + 1288 erase_timeout = card->ssr.erase_timeout * qty +
1171 card->ssr.erase_offset; 1289 card->ssr.erase_offset;
1172 } else { 1290 } else {
1173 /* 1291 /*
1174 * Erase timeout not specified in SD Status Register (SSR) so 1292 * Erase timeout not specified in SD Status Register (SSR) so
1175 * use 250ms per write block. 1293 * use 250ms per write block.
1176 */ 1294 */
1177 cmd->erase_timeout = 250 * qty; 1295 erase_timeout = 250 * qty;
1178 } 1296 }
1179 1297
1180 /* Must not be less than 1 second */ 1298 /* Must not be less than 1 second */
1181 if (cmd->erase_timeout < 1000) 1299 if (erase_timeout < 1000)
1182 cmd->erase_timeout = 1000; 1300 erase_timeout = 1000;
1301
1302 return erase_timeout;
1183} 1303}
1184 1304
1185static void mmc_set_erase_timeout(struct mmc_card *card, 1305static unsigned int mmc_erase_timeout(struct mmc_card *card,
1186 struct mmc_command *cmd, unsigned int arg, 1306 unsigned int arg,
1187 unsigned int qty) 1307 unsigned int qty)
1188{ 1308{
1189 if (mmc_card_sd(card)) 1309 if (mmc_card_sd(card))
1190 mmc_set_sd_erase_timeout(card, cmd, arg, qty); 1310 return mmc_sd_erase_timeout(card, arg, qty);
1191 else 1311 else
1192 mmc_set_mmc_erase_timeout(card, cmd, arg, qty); 1312 return mmc_mmc_erase_timeout(card, arg, qty);
1193} 1313}
1194 1314
1195static int mmc_do_erase(struct mmc_card *card, unsigned int from, 1315static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1196 unsigned int to, unsigned int arg) 1316 unsigned int to, unsigned int arg)
1197{ 1317{
1198 struct mmc_command cmd; 1318 struct mmc_command cmd = {0};
1199 unsigned int qty = 0; 1319 unsigned int qty = 0;
1200 int err; 1320 int err;
1201 1321
@@ -1229,7 +1349,6 @@ static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1229 to <<= 9; 1349 to <<= 9;
1230 } 1350 }
1231 1351
1232 memset(&cmd, 0, sizeof(struct mmc_command));
1233 if (mmc_card_sd(card)) 1352 if (mmc_card_sd(card))
1234 cmd.opcode = SD_ERASE_WR_BLK_START; 1353 cmd.opcode = SD_ERASE_WR_BLK_START;
1235 else 1354 else
@@ -1263,7 +1382,7 @@ static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1263 cmd.opcode = MMC_ERASE; 1382 cmd.opcode = MMC_ERASE;
1264 cmd.arg = arg; 1383 cmd.arg = arg;
1265 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; 1384 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1266 mmc_set_erase_timeout(card, &cmd, arg, qty); 1385 cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty);
1267 err = mmc_wait_for_cmd(card->host, &cmd, 0); 1386 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1268 if (err) { 1387 if (err) {
1269 printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n", 1388 printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n",
@@ -1397,33 +1516,77 @@ int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
1397} 1516}
1398EXPORT_SYMBOL(mmc_erase_group_aligned); 1517EXPORT_SYMBOL(mmc_erase_group_aligned);
1399 1518
1519int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
1520{
1521 struct mmc_command cmd = {0};
1522
1523 if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
1524 return 0;
1525
1526 cmd.opcode = MMC_SET_BLOCKLEN;
1527 cmd.arg = blocklen;
1528 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1529 return mmc_wait_for_cmd(card->host, &cmd, 5);
1530}
1531EXPORT_SYMBOL(mmc_set_blocklen);
1532
1533static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
1534{
1535 host->f_init = freq;
1536
1537#ifdef CONFIG_MMC_DEBUG
1538 pr_info("%s: %s: trying to init card at %u Hz\n",
1539 mmc_hostname(host), __func__, host->f_init);
1540#endif
1541 mmc_power_up(host);
1542
1543 /*
1544 * sdio_reset sends CMD52 to reset card. Since we do not know
1545 * if the card is being re-initialized, just send it. CMD52
1546 * should be ignored by SD/eMMC cards.
1547 */
1548 sdio_reset(host);
1549 mmc_go_idle(host);
1550
1551 mmc_send_if_cond(host, host->ocr_avail);
1552
1553 /* Order's important: probe SDIO, then SD, then MMC */
1554 if (!mmc_attach_sdio(host))
1555 return 0;
1556 if (!mmc_attach_sd(host))
1557 return 0;
1558 if (!mmc_attach_mmc(host))
1559 return 0;
1560
1561 mmc_power_off(host);
1562 return -EIO;
1563}
1564
1400void mmc_rescan(struct work_struct *work) 1565void mmc_rescan(struct work_struct *work)
1401{ 1566{
1567 static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
1402 struct mmc_host *host = 1568 struct mmc_host *host =
1403 container_of(work, struct mmc_host, detect.work); 1569 container_of(work, struct mmc_host, detect.work);
1404 u32 ocr; 1570 int i;
1405 int err;
1406 unsigned long flags;
1407
1408 spin_lock_irqsave(&host->lock, flags);
1409 1571
1410 if (host->rescan_disable) { 1572 if (host->rescan_disable)
1411 spin_unlock_irqrestore(&host->lock, flags);
1412 return; 1573 return;
1413 }
1414
1415 spin_unlock_irqrestore(&host->lock, flags);
1416
1417 1574
1418 mmc_bus_get(host); 1575 mmc_bus_get(host);
1419 1576
1420 /* if there is a card registered, check whether it is still present */ 1577 /*
1421 if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead) 1578 * if there is a _removable_ card registered, check whether it is
1579 * still present
1580 */
1581 if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
1582 && !(host->caps & MMC_CAP_NONREMOVABLE))
1422 host->bus_ops->detect(host); 1583 host->bus_ops->detect(host);
1423 1584
1585 /*
1586 * Let mmc_bus_put() free the bus/bus_ops if we've found that
1587 * the card is no longer present.
1588 */
1424 mmc_bus_put(host); 1589 mmc_bus_put(host);
1425
1426
1427 mmc_bus_get(host); 1590 mmc_bus_get(host);
1428 1591
1429 /* if there still is a card present, stop here */ 1592 /* if there still is a card present, stop here */
@@ -1432,8 +1595,6 @@ void mmc_rescan(struct work_struct *work)
1432 goto out; 1595 goto out;
1433 } 1596 }
1434 1597
1435 /* detect a newly inserted card */
1436
1437 /* 1598 /*
1438 * Only we can add a new handler, so it's safe to 1599 * Only we can add a new handler, so it's safe to
1439 * release the lock here. 1600 * release the lock here.
@@ -1444,55 +1605,15 @@ void mmc_rescan(struct work_struct *work)
1444 goto out; 1605 goto out;
1445 1606
1446 mmc_claim_host(host); 1607 mmc_claim_host(host);
1447 1608 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
1448 mmc_power_up(host); 1609 if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
1449 sdio_reset(host); 1610 break;
1450 mmc_go_idle(host); 1611 if (freqs[i] <= host->f_min)
1451 1612 break;
1452 mmc_send_if_cond(host, host->ocr_avail);
1453
1454 /*
1455 * First we search for SDIO...
1456 */
1457 err = mmc_send_io_op_cond(host, 0, &ocr);
1458 if (!err) {
1459 if (mmc_attach_sdio(host, ocr)) {
1460 mmc_claim_host(host);
1461 /* try SDMEM (but not MMC) even if SDIO is broken */
1462 if (mmc_send_app_op_cond(host, 0, &ocr))
1463 goto out_fail;
1464
1465 if (mmc_attach_sd(host, ocr))
1466 mmc_power_off(host);
1467 }
1468 goto out;
1469 }
1470
1471 /*
1472 * ...then normal SD...
1473 */
1474 err = mmc_send_app_op_cond(host, 0, &ocr);
1475 if (!err) {
1476 if (mmc_attach_sd(host, ocr))
1477 mmc_power_off(host);
1478 goto out;
1479 }
1480
1481 /*
1482 * ...and finally MMC.
1483 */
1484 err = mmc_send_op_cond(host, 0, &ocr);
1485 if (!err) {
1486 if (mmc_attach_mmc(host, ocr))
1487 mmc_power_off(host);
1488 goto out;
1489 } 1613 }
1490
1491out_fail:
1492 mmc_release_host(host); 1614 mmc_release_host(host);
1493 mmc_power_off(host);
1494 1615
1495out: 1616 out:
1496 if (host->caps & MMC_CAP_NEEDS_POLL) 1617 if (host->caps & MMC_CAP_NEEDS_POLL)
1497 mmc_schedule_delayed_work(&host->detect, HZ); 1618 mmc_schedule_delayed_work(&host->detect, HZ);
1498} 1619}
@@ -1514,7 +1635,7 @@ void mmc_stop_host(struct mmc_host *host)
1514 1635
1515 if (host->caps & MMC_CAP_DISABLE) 1636 if (host->caps & MMC_CAP_DISABLE)
1516 cancel_delayed_work(&host->disable); 1637 cancel_delayed_work(&host->disable);
1517 cancel_delayed_work(&host->detect); 1638 cancel_delayed_work_sync(&host->detect);
1518 mmc_flush_scheduled_work(); 1639 mmc_flush_scheduled_work();
1519 1640
1520 /* clear pm flags now and let card drivers set them as needed */ 1641 /* clear pm flags now and let card drivers set them as needed */
@@ -1538,37 +1659,45 @@ void mmc_stop_host(struct mmc_host *host)
1538 mmc_power_off(host); 1659 mmc_power_off(host);
1539} 1660}
1540 1661
1541void mmc_power_save_host(struct mmc_host *host) 1662int mmc_power_save_host(struct mmc_host *host)
1542{ 1663{
1664 int ret = 0;
1665
1543 mmc_bus_get(host); 1666 mmc_bus_get(host);
1544 1667
1545 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) { 1668 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1546 mmc_bus_put(host); 1669 mmc_bus_put(host);
1547 return; 1670 return -EINVAL;
1548 } 1671 }
1549 1672
1550 if (host->bus_ops->power_save) 1673 if (host->bus_ops->power_save)
1551 host->bus_ops->power_save(host); 1674 ret = host->bus_ops->power_save(host);
1552 1675
1553 mmc_bus_put(host); 1676 mmc_bus_put(host);
1554 1677
1555 mmc_power_off(host); 1678 mmc_power_off(host);
1679
1680 return ret;
1556} 1681}
1557EXPORT_SYMBOL(mmc_power_save_host); 1682EXPORT_SYMBOL(mmc_power_save_host);
1558 1683
1559void mmc_power_restore_host(struct mmc_host *host) 1684int mmc_power_restore_host(struct mmc_host *host)
1560{ 1685{
1686 int ret;
1687
1561 mmc_bus_get(host); 1688 mmc_bus_get(host);
1562 1689
1563 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) { 1690 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1564 mmc_bus_put(host); 1691 mmc_bus_put(host);
1565 return; 1692 return -EINVAL;
1566 } 1693 }
1567 1694
1568 mmc_power_up(host); 1695 mmc_power_up(host);
1569 host->bus_ops->power_restore(host); 1696 ret = host->bus_ops->power_restore(host);
1570 1697
1571 mmc_bus_put(host); 1698 mmc_bus_put(host);
1699
1700 return ret;
1572} 1701}
1573EXPORT_SYMBOL(mmc_power_restore_host); 1702EXPORT_SYMBOL(mmc_power_restore_host);
1574 1703
@@ -1647,7 +1776,7 @@ int mmc_suspend_host(struct mmc_host *host)
1647 } 1776 }
1648 mmc_bus_put(host); 1777 mmc_bus_put(host);
1649 1778
1650 if (!err && !(host->pm_flags & MMC_PM_KEEP_POWER)) 1779 if (!err && !mmc_card_keep_power(host))
1651 mmc_power_off(host); 1780 mmc_power_off(host);
1652 1781
1653 return err; 1782 return err;
@@ -1665,9 +1794,21 @@ int mmc_resume_host(struct mmc_host *host)
1665 1794
1666 mmc_bus_get(host); 1795 mmc_bus_get(host);
1667 if (host->bus_ops && !host->bus_dead) { 1796 if (host->bus_ops && !host->bus_dead) {
1668 if (!(host->pm_flags & MMC_PM_KEEP_POWER)) { 1797 if (!mmc_card_keep_power(host)) {
1669 mmc_power_up(host); 1798 mmc_power_up(host);
1670 mmc_select_voltage(host, host->ocr); 1799 mmc_select_voltage(host, host->ocr);
1800 /*
1801 * Tell runtime PM core we just powered up the card,
1802 * since it still believes the card is powered off.
1803 * Note that currently runtime PM is only enabled
1804 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
1805 */
1806 if (mmc_card_sdio(host->card) &&
1807 (host->caps & MMC_CAP_POWER_OFF_CARD)) {
1808 pm_runtime_disable(&host->card->dev);
1809 pm_runtime_set_active(&host->card->dev);
1810 pm_runtime_enable(&host->card->dev);
1811 }
1671 } 1812 }
1672 BUG_ON(!host->bus_ops->resume); 1813 BUG_ON(!host->bus_ops->resume);
1673 err = host->bus_ops->resume(host); 1814 err = host->bus_ops->resume(host);
@@ -1678,6 +1819,7 @@ int mmc_resume_host(struct mmc_host *host)
1678 err = 0; 1819 err = 0;
1679 } 1820 }
1680 } 1821 }
1822 host->pm_flags &= ~MMC_PM_KEEP_POWER;
1681 mmc_bus_put(host); 1823 mmc_bus_put(host);
1682 1824
1683 return err; 1825 return err;
@@ -1720,6 +1862,7 @@ int mmc_pm_notify(struct notifier_block *notify_block,
1720 1862
1721 case PM_POST_SUSPEND: 1863 case PM_POST_SUSPEND:
1722 case PM_POST_HIBERNATION: 1864 case PM_POST_HIBERNATION:
1865 case PM_POST_RESTORE:
1723 1866
1724 spin_lock_irqsave(&host->lock, flags); 1867 spin_lock_irqsave(&host->lock, flags);
1725 host->rescan_disable = 0; 1868 host->rescan_disable = 0;
@@ -1736,7 +1879,7 @@ static int __init mmc_init(void)
1736{ 1879{
1737 int ret; 1880 int ret;
1738 1881
1739 workqueue = create_singlethread_workqueue("kmmcd"); 1882 workqueue = alloc_ordered_workqueue("kmmcd", 0);
1740 if (!workqueue) 1883 if (!workqueue)
1741 return -ENOMEM; 1884 return -ENOMEM;
1742 1885
diff --git a/drivers/mmc/core/core.h b/drivers/mmc/core/core.h
index 9d9eef50e5d1..d9411ed2a39b 100644
--- a/drivers/mmc/core/core.h
+++ b/drivers/mmc/core/core.h
@@ -22,8 +22,8 @@ struct mmc_bus_ops {
22 void (*detect)(struct mmc_host *); 22 void (*detect)(struct mmc_host *);
23 int (*suspend)(struct mmc_host *); 23 int (*suspend)(struct mmc_host *);
24 int (*resume)(struct mmc_host *); 24 int (*resume)(struct mmc_host *);
25 void (*power_save)(struct mmc_host *); 25 int (*power_save)(struct mmc_host *);
26 void (*power_restore)(struct mmc_host *); 26 int (*power_restore)(struct mmc_host *);
27}; 27};
28 28
29void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops); 29void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops);
@@ -33,10 +33,16 @@ void mmc_init_erase(struct mmc_card *card);
33 33
34void mmc_set_chip_select(struct mmc_host *host, int mode); 34void mmc_set_chip_select(struct mmc_host *host, int mode);
35void mmc_set_clock(struct mmc_host *host, unsigned int hz); 35void mmc_set_clock(struct mmc_host *host, unsigned int hz);
36void mmc_gate_clock(struct mmc_host *host);
37void mmc_ungate_clock(struct mmc_host *host);
38void mmc_set_ungated(struct mmc_host *host);
36void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode); 39void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode);
37void mmc_set_bus_width(struct mmc_host *host, unsigned int width); 40void mmc_set_bus_width(struct mmc_host *host, unsigned int width);
38u32 mmc_select_voltage(struct mmc_host *host, u32 ocr); 41u32 mmc_select_voltage(struct mmc_host *host, u32 ocr);
42int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage,
43 bool cmd11);
39void mmc_set_timing(struct mmc_host *host, unsigned int timing); 44void mmc_set_timing(struct mmc_host *host, unsigned int timing);
45void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type);
40 46
41static inline void mmc_delay(unsigned int ms) 47static inline void mmc_delay(unsigned int ms)
42{ 48{
@@ -52,13 +58,12 @@ void mmc_rescan(struct work_struct *work);
52void mmc_start_host(struct mmc_host *host); 58void mmc_start_host(struct mmc_host *host);
53void mmc_stop_host(struct mmc_host *host); 59void mmc_stop_host(struct mmc_host *host);
54 60
55int mmc_attach_mmc(struct mmc_host *host, u32 ocr); 61int mmc_attach_mmc(struct mmc_host *host);
56int mmc_attach_sd(struct mmc_host *host, u32 ocr); 62int mmc_attach_sd(struct mmc_host *host);
57int mmc_attach_sdio(struct mmc_host *host, u32 ocr); 63int mmc_attach_sdio(struct mmc_host *host);
58 64
59/* Module parameters */ 65/* Module parameters */
60extern int use_spi_crc; 66extern int use_spi_crc;
61extern int mmc_assume_removable;
62 67
63/* Debugfs information for hosts and cards */ 68/* Debugfs information for hosts and cards */
64void mmc_add_host_debugfs(struct mmc_host *host); 69void mmc_add_host_debugfs(struct mmc_host *host);
diff --git a/drivers/mmc/core/debugfs.c b/drivers/mmc/core/debugfs.c
index 53cb380c0987..998797ed67a6 100644
--- a/drivers/mmc/core/debugfs.c
+++ b/drivers/mmc/core/debugfs.c
@@ -134,6 +134,33 @@ static const struct file_operations mmc_ios_fops = {
134 .release = single_release, 134 .release = single_release,
135}; 135};
136 136
137static int mmc_clock_opt_get(void *data, u64 *val)
138{
139 struct mmc_host *host = data;
140
141 *val = host->ios.clock;
142
143 return 0;
144}
145
146static int mmc_clock_opt_set(void *data, u64 val)
147{
148 struct mmc_host *host = data;
149
150 /* We need this check due to input value is u64 */
151 if (val > host->f_max)
152 return -EINVAL;
153
154 mmc_claim_host(host);
155 mmc_set_clock(host, (unsigned int) val);
156 mmc_release_host(host);
157
158 return 0;
159}
160
161DEFINE_SIMPLE_ATTRIBUTE(mmc_clock_fops, mmc_clock_opt_get, mmc_clock_opt_set,
162 "%llu\n");
163
137void mmc_add_host_debugfs(struct mmc_host *host) 164void mmc_add_host_debugfs(struct mmc_host *host)
138{ 165{
139 struct dentry *root; 166 struct dentry *root;
@@ -150,11 +177,20 @@ void mmc_add_host_debugfs(struct mmc_host *host)
150 host->debugfs_root = root; 177 host->debugfs_root = root;
151 178
152 if (!debugfs_create_file("ios", S_IRUSR, root, host, &mmc_ios_fops)) 179 if (!debugfs_create_file("ios", S_IRUSR, root, host, &mmc_ios_fops))
153 goto err_ios; 180 goto err_node;
181
182 if (!debugfs_create_file("clock", S_IRUSR | S_IWUSR, root, host,
183 &mmc_clock_fops))
184 goto err_node;
154 185
186#ifdef CONFIG_MMC_CLKGATE
187 if (!debugfs_create_u32("clk_delay", (S_IRUSR | S_IWUSR),
188 root, &host->clk_delay))
189 goto err_node;
190#endif
155 return; 191 return;
156 192
157err_ios: 193err_node:
158 debugfs_remove_recursive(root); 194 debugfs_remove_recursive(root);
159 host->debugfs_root = NULL; 195 host->debugfs_root = NULL;
160err_root: 196err_root:
@@ -245,6 +281,7 @@ static const struct file_operations mmc_dbg_ext_csd_fops = {
245 .open = mmc_ext_csd_open, 281 .open = mmc_ext_csd_open,
246 .read = mmc_ext_csd_read, 282 .read = mmc_ext_csd_read,
247 .release = mmc_ext_csd_release, 283 .release = mmc_ext_csd_release,
284 .llseek = default_llseek,
248}; 285};
249 286
250void mmc_add_card_debugfs(struct mmc_card *card) 287void mmc_add_card_debugfs(struct mmc_card *card)
diff --git a/drivers/mmc/core/host.c b/drivers/mmc/core/host.c
index d80cfdc8edd2..b29d3e8fd3a2 100644
--- a/drivers/mmc/core/host.c
+++ b/drivers/mmc/core/host.c
@@ -3,6 +3,7 @@
3 * 3 *
4 * Copyright (C) 2003 Russell King, All Rights Reserved. 4 * Copyright (C) 2003 Russell King, All Rights Reserved.
5 * Copyright (C) 2007-2008 Pierre Ossman 5 * Copyright (C) 2007-2008 Pierre Ossman
6 * Copyright (C) 2010 Linus Walleij
6 * 7 *
7 * This program is free software; you can redistribute it and/or modify 8 * 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 * it under the terms of the GNU General Public License version 2 as
@@ -20,6 +21,7 @@
20#include <linux/suspend.h> 21#include <linux/suspend.h>
21 22
22#include <linux/mmc/host.h> 23#include <linux/mmc/host.h>
24#include <linux/mmc/card.h>
23 25
24#include "core.h" 26#include "core.h"
25#include "host.h" 27#include "host.h"
@@ -50,6 +52,202 @@ void mmc_unregister_host_class(void)
50static DEFINE_IDR(mmc_host_idr); 52static DEFINE_IDR(mmc_host_idr);
51static DEFINE_SPINLOCK(mmc_host_lock); 53static DEFINE_SPINLOCK(mmc_host_lock);
52 54
55#ifdef CONFIG_MMC_CLKGATE
56
57/*
58 * Enabling clock gating will make the core call out to the host
59 * once up and once down when it performs a request or card operation
60 * intermingled in any fashion. The driver will see this through
61 * set_ios() operations with ios.clock field set to 0 to gate (disable)
62 * the block clock, and to the old frequency to enable it again.
63 */
64static void mmc_host_clk_gate_delayed(struct mmc_host *host)
65{
66 unsigned long tick_ns;
67 unsigned long freq = host->ios.clock;
68 unsigned long flags;
69
70 if (!freq) {
71 pr_debug("%s: frequency set to 0 in disable function, "
72 "this means the clock is already disabled.\n",
73 mmc_hostname(host));
74 return;
75 }
76 /*
77 * New requests may have appeared while we were scheduling,
78 * then there is no reason to delay the check before
79 * clk_disable().
80 */
81 spin_lock_irqsave(&host->clk_lock, flags);
82
83 /*
84 * Delay n bus cycles (at least 8 from MMC spec) before attempting
85 * to disable the MCI block clock. The reference count may have
86 * gone up again after this delay due to rescheduling!
87 */
88 if (!host->clk_requests) {
89 spin_unlock_irqrestore(&host->clk_lock, flags);
90 tick_ns = DIV_ROUND_UP(1000000000, freq);
91 ndelay(host->clk_delay * tick_ns);
92 } else {
93 /* New users appeared while waiting for this work */
94 spin_unlock_irqrestore(&host->clk_lock, flags);
95 return;
96 }
97 mutex_lock(&host->clk_gate_mutex);
98 spin_lock_irqsave(&host->clk_lock, flags);
99 if (!host->clk_requests) {
100 spin_unlock_irqrestore(&host->clk_lock, flags);
101 /* This will set host->ios.clock to 0 */
102 mmc_gate_clock(host);
103 spin_lock_irqsave(&host->clk_lock, flags);
104 pr_debug("%s: gated MCI clock\n", mmc_hostname(host));
105 }
106 spin_unlock_irqrestore(&host->clk_lock, flags);
107 mutex_unlock(&host->clk_gate_mutex);
108}
109
110/*
111 * Internal work. Work to disable the clock at some later point.
112 */
113static void mmc_host_clk_gate_work(struct work_struct *work)
114{
115 struct mmc_host *host = container_of(work, struct mmc_host,
116 clk_gate_work);
117
118 mmc_host_clk_gate_delayed(host);
119}
120
121/**
122 * mmc_host_clk_ungate - ungate hardware MCI clocks
123 * @host: host to ungate.
124 *
125 * Makes sure the host ios.clock is restored to a non-zero value
126 * past this call. Increase clock reference count and ungate clock
127 * if we're the first user.
128 */
129void mmc_host_clk_ungate(struct mmc_host *host)
130{
131 unsigned long flags;
132
133 mutex_lock(&host->clk_gate_mutex);
134 spin_lock_irqsave(&host->clk_lock, flags);
135 if (host->clk_gated) {
136 spin_unlock_irqrestore(&host->clk_lock, flags);
137 mmc_ungate_clock(host);
138 spin_lock_irqsave(&host->clk_lock, flags);
139 pr_debug("%s: ungated MCI clock\n", mmc_hostname(host));
140 }
141 host->clk_requests++;
142 spin_unlock_irqrestore(&host->clk_lock, flags);
143 mutex_unlock(&host->clk_gate_mutex);
144}
145
146/**
147 * mmc_host_may_gate_card - check if this card may be gated
148 * @card: card to check.
149 */
150static bool mmc_host_may_gate_card(struct mmc_card *card)
151{
152 /* If there is no card we may gate it */
153 if (!card)
154 return true;
155 /*
156 * Don't gate SDIO cards! These need to be clocked at all times
157 * since they may be independent systems generating interrupts
158 * and other events. The clock requests counter from the core will
159 * go down to zero since the core does not need it, but we will not
160 * gate the clock, because there is somebody out there that may still
161 * be using it.
162 */
163 return !(card->quirks & MMC_QUIRK_BROKEN_CLK_GATING);
164}
165
166/**
167 * mmc_host_clk_gate - gate off hardware MCI clocks
168 * @host: host to gate.
169 *
170 * Calls the host driver with ios.clock set to zero as often as possible
171 * in order to gate off hardware MCI clocks. Decrease clock reference
172 * count and schedule disabling of clock.
173 */
174void mmc_host_clk_gate(struct mmc_host *host)
175{
176 unsigned long flags;
177
178 spin_lock_irqsave(&host->clk_lock, flags);
179 host->clk_requests--;
180 if (mmc_host_may_gate_card(host->card) &&
181 !host->clk_requests)
182 schedule_work(&host->clk_gate_work);
183 spin_unlock_irqrestore(&host->clk_lock, flags);
184}
185
186/**
187 * mmc_host_clk_rate - get current clock frequency setting
188 * @host: host to get the clock frequency for.
189 *
190 * Returns current clock frequency regardless of gating.
191 */
192unsigned int mmc_host_clk_rate(struct mmc_host *host)
193{
194 unsigned long freq;
195 unsigned long flags;
196
197 spin_lock_irqsave(&host->clk_lock, flags);
198 if (host->clk_gated)
199 freq = host->clk_old;
200 else
201 freq = host->ios.clock;
202 spin_unlock_irqrestore(&host->clk_lock, flags);
203 return freq;
204}
205
206/**
207 * mmc_host_clk_init - set up clock gating code
208 * @host: host with potential clock to control
209 */
210static inline void mmc_host_clk_init(struct mmc_host *host)
211{
212 host->clk_requests = 0;
213 /* Hold MCI clock for 8 cycles by default */
214 host->clk_delay = 8;
215 host->clk_gated = false;
216 INIT_WORK(&host->clk_gate_work, mmc_host_clk_gate_work);
217 spin_lock_init(&host->clk_lock);
218 mutex_init(&host->clk_gate_mutex);
219}
220
221/**
222 * mmc_host_clk_exit - shut down clock gating code
223 * @host: host with potential clock to control
224 */
225static inline void mmc_host_clk_exit(struct mmc_host *host)
226{
227 /*
228 * Wait for any outstanding gate and then make sure we're
229 * ungated before exiting.
230 */
231 if (cancel_work_sync(&host->clk_gate_work))
232 mmc_host_clk_gate_delayed(host);
233 if (host->clk_gated)
234 mmc_host_clk_ungate(host);
235 /* There should be only one user now */
236 WARN_ON(host->clk_requests > 1);
237}
238
239#else
240
241static inline void mmc_host_clk_init(struct mmc_host *host)
242{
243}
244
245static inline void mmc_host_clk_exit(struct mmc_host *host)
246{
247}
248
249#endif
250
53/** 251/**
54 * mmc_alloc_host - initialise the per-host structure. 252 * mmc_alloc_host - initialise the per-host structure.
55 * @extra: sizeof private data structure 253 * @extra: sizeof private data structure
@@ -82,6 +280,8 @@ struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
82 host->class_dev.class = &mmc_host_class; 280 host->class_dev.class = &mmc_host_class;
83 device_initialize(&host->class_dev); 281 device_initialize(&host->class_dev);
84 282
283 mmc_host_clk_init(host);
284
85 spin_lock_init(&host->lock); 285 spin_lock_init(&host->lock);
86 init_waitqueue_head(&host->wq); 286 init_waitqueue_head(&host->wq);
87 INIT_DELAYED_WORK(&host->detect, mmc_rescan); 287 INIT_DELAYED_WORK(&host->detect, mmc_rescan);
@@ -94,8 +294,7 @@ struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
94 * By default, hosts do not support SGIO or large requests. 294 * By default, hosts do not support SGIO or large requests.
95 * They have to set these according to their abilities. 295 * They have to set these according to their abilities.
96 */ 296 */
97 host->max_hw_segs = 1; 297 host->max_segs = 1;
98 host->max_phys_segs = 1;
99 host->max_seg_size = PAGE_CACHE_SIZE; 298 host->max_seg_size = PAGE_CACHE_SIZE;
100 299
101 host->max_req_size = PAGE_CACHE_SIZE; 300 host->max_req_size = PAGE_CACHE_SIZE;
@@ -126,12 +325,12 @@ int mmc_add_host(struct mmc_host *host)
126 WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) && 325 WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) &&
127 !host->ops->enable_sdio_irq); 326 !host->ops->enable_sdio_irq);
128 327
129 led_trigger_register_simple(dev_name(&host->class_dev), &host->led);
130
131 err = device_add(&host->class_dev); 328 err = device_add(&host->class_dev);
132 if (err) 329 if (err)
133 return err; 330 return err;
134 331
332 led_trigger_register_simple(dev_name(&host->class_dev), &host->led);
333
135#ifdef CONFIG_DEBUG_FS 334#ifdef CONFIG_DEBUG_FS
136 mmc_add_host_debugfs(host); 335 mmc_add_host_debugfs(host);
137#endif 336#endif
@@ -164,6 +363,8 @@ void mmc_remove_host(struct mmc_host *host)
164 device_del(&host->class_dev); 363 device_del(&host->class_dev);
165 364
166 led_trigger_unregister_simple(host->led); 365 led_trigger_unregister_simple(host->led);
366
367 mmc_host_clk_exit(host);
167} 368}
168 369
169EXPORT_SYMBOL(mmc_remove_host); 370EXPORT_SYMBOL(mmc_remove_host);
@@ -184,4 +385,3 @@ void mmc_free_host(struct mmc_host *host)
184} 385}
185 386
186EXPORT_SYMBOL(mmc_free_host); 387EXPORT_SYMBOL(mmc_free_host);
187
diff --git a/drivers/mmc/core/host.h b/drivers/mmc/core/host.h
index 8c87e1109a34..de199f911928 100644
--- a/drivers/mmc/core/host.h
+++ b/drivers/mmc/core/host.h
@@ -10,10 +10,31 @@
10 */ 10 */
11#ifndef _MMC_CORE_HOST_H 11#ifndef _MMC_CORE_HOST_H
12#define _MMC_CORE_HOST_H 12#define _MMC_CORE_HOST_H
13#include <linux/mmc/host.h>
13 14
14int mmc_register_host_class(void); 15int mmc_register_host_class(void);
15void mmc_unregister_host_class(void); 16void mmc_unregister_host_class(void);
16 17
18#ifdef CONFIG_MMC_CLKGATE
19void mmc_host_clk_ungate(struct mmc_host *host);
20void mmc_host_clk_gate(struct mmc_host *host);
21unsigned int mmc_host_clk_rate(struct mmc_host *host);
22
23#else
24static inline void mmc_host_clk_ungate(struct mmc_host *host)
25{
26}
27
28static inline void mmc_host_clk_gate(struct mmc_host *host)
29{
30}
31
32static inline unsigned int mmc_host_clk_rate(struct mmc_host *host)
33{
34 return host->ios.clock;
35}
36#endif
37
17void mmc_host_deeper_disable(struct work_struct *work); 38void mmc_host_deeper_disable(struct work_struct *work);
18 39
19#endif 40#endif
diff --git a/drivers/mmc/core/mmc.c b/drivers/mmc/core/mmc.c
index 6909a54c39be..aa7d1d79b8c5 100644
--- a/drivers/mmc/core/mmc.c
+++ b/drivers/mmc/core/mmc.c
@@ -20,6 +20,7 @@
20#include "core.h" 20#include "core.h"
21#include "bus.h" 21#include "bus.h"
22#include "mmc_ops.h" 22#include "mmc_ops.h"
23#include "sd_ops.h"
23 24
24static const unsigned int tran_exp[] = { 25static const unsigned int tran_exp[] = {
25 10000, 100000, 1000000, 10000000, 26 10000, 100000, 1000000, 10000000,
@@ -173,14 +174,17 @@ static int mmc_decode_csd(struct mmc_card *card)
173} 174}
174 175
175/* 176/*
176 * Read and decode extended CSD. 177 * Read extended CSD.
177 */ 178 */
178static int mmc_read_ext_csd(struct mmc_card *card) 179static int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd)
179{ 180{
180 int err; 181 int err;
181 u8 *ext_csd; 182 u8 *ext_csd;
182 183
183 BUG_ON(!card); 184 BUG_ON(!card);
185 BUG_ON(!new_ext_csd);
186
187 *new_ext_csd = NULL;
184 188
185 if (card->csd.mmca_vsn < CSD_SPEC_VER_4) 189 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
186 return 0; 190 return 0;
@@ -198,12 +202,15 @@ static int mmc_read_ext_csd(struct mmc_card *card)
198 202
199 err = mmc_send_ext_csd(card, ext_csd); 203 err = mmc_send_ext_csd(card, ext_csd);
200 if (err) { 204 if (err) {
205 kfree(ext_csd);
206 *new_ext_csd = NULL;
207
201 /* If the host or the card can't do the switch, 208 /* If the host or the card can't do the switch,
202 * fail more gracefully. */ 209 * fail more gracefully. */
203 if ((err != -EINVAL) 210 if ((err != -EINVAL)
204 && (err != -ENOSYS) 211 && (err != -ENOSYS)
205 && (err != -EFAULT)) 212 && (err != -EFAULT))
206 goto out; 213 return err;
207 214
208 /* 215 /*
209 * High capacity cards should have this "magic" size 216 * High capacity cards should have this "magic" size
@@ -221,17 +228,31 @@ static int mmc_read_ext_csd(struct mmc_card *card)
221 mmc_hostname(card->host)); 228 mmc_hostname(card->host));
222 err = 0; 229 err = 0;
223 } 230 }
231 } else
232 *new_ext_csd = ext_csd;
224 233
225 goto out; 234 return err;
226 } 235}
236
237/*
238 * Decode extended CSD.
239 */
240static int mmc_read_ext_csd(struct mmc_card *card, u8 *ext_csd)
241{
242 int err = 0;
243
244 BUG_ON(!card);
245
246 if (!ext_csd)
247 return 0;
227 248
228 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */ 249 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
250 card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
229 if (card->csd.structure == 3) { 251 if (card->csd.structure == 3) {
230 int ext_csd_struct = ext_csd[EXT_CSD_STRUCTURE]; 252 if (card->ext_csd.raw_ext_csd_structure > 2) {
231 if (ext_csd_struct > 2) {
232 printk(KERN_ERR "%s: unrecognised EXT_CSD structure " 253 printk(KERN_ERR "%s: unrecognised EXT_CSD structure "
233 "version %d\n", mmc_hostname(card->host), 254 "version %d\n", mmc_hostname(card->host),
234 ext_csd_struct); 255 card->ext_csd.raw_ext_csd_structure);
235 err = -EINVAL; 256 err = -EINVAL;
236 goto out; 257 goto out;
237 } 258 }
@@ -245,6 +266,10 @@ static int mmc_read_ext_csd(struct mmc_card *card)
245 goto out; 266 goto out;
246 } 267 }
247 268
269 card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
270 card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
271 card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
272 card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
248 if (card->ext_csd.rev >= 2) { 273 if (card->ext_csd.rev >= 2) {
249 card->ext_csd.sectors = 274 card->ext_csd.sectors =
250 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 | 275 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
@@ -256,8 +281,23 @@ static int mmc_read_ext_csd(struct mmc_card *card)
256 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512) 281 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
257 mmc_card_set_blockaddr(card); 282 mmc_card_set_blockaddr(card);
258 } 283 }
259 284 card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
260 switch (ext_csd[EXT_CSD_CARD_TYPE] & EXT_CSD_CARD_TYPE_MASK) { 285 switch (ext_csd[EXT_CSD_CARD_TYPE] & EXT_CSD_CARD_TYPE_MASK) {
286 case EXT_CSD_CARD_TYPE_DDR_52 | EXT_CSD_CARD_TYPE_52 |
287 EXT_CSD_CARD_TYPE_26:
288 card->ext_csd.hs_max_dtr = 52000000;
289 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_52;
290 break;
291 case EXT_CSD_CARD_TYPE_DDR_1_2V | EXT_CSD_CARD_TYPE_52 |
292 EXT_CSD_CARD_TYPE_26:
293 card->ext_csd.hs_max_dtr = 52000000;
294 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_1_2V;
295 break;
296 case EXT_CSD_CARD_TYPE_DDR_1_8V | EXT_CSD_CARD_TYPE_52 |
297 EXT_CSD_CARD_TYPE_26:
298 card->ext_csd.hs_max_dtr = 52000000;
299 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_1_8V;
300 break;
261 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26: 301 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
262 card->ext_csd.hs_max_dtr = 52000000; 302 card->ext_csd.hs_max_dtr = 52000000;
263 break; 303 break;
@@ -271,8 +311,17 @@ static int mmc_read_ext_csd(struct mmc_card *card)
271 mmc_hostname(card->host)); 311 mmc_hostname(card->host));
272 } 312 }
273 313
314 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
315 card->ext_csd.raw_erase_timeout_mult =
316 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
317 card->ext_csd.raw_hc_erase_grp_size =
318 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
274 if (card->ext_csd.rev >= 3) { 319 if (card->ext_csd.rev >= 3) {
275 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT]; 320 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
321 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
322
323 /* EXT_CSD value is in units of 10ms, but we store in ms */
324 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
276 325
277 /* Sleep / awake timeout in 100ns units */ 326 /* Sleep / awake timeout in 100ns units */
278 if (sa_shift > 0 && sa_shift <= 0x17) 327 if (sa_shift > 0 && sa_shift <= 0x17)
@@ -284,9 +333,65 @@ static int mmc_read_ext_csd(struct mmc_card *card)
284 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]; 333 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
285 card->ext_csd.hc_erase_size = 334 card->ext_csd.hc_erase_size =
286 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10; 335 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
336
337 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
338
339 /*
340 * There are two boot regions of equal size, defined in
341 * multiples of 128K.
342 */
343 card->ext_csd.boot_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
287 } 344 }
288 345
346 card->ext_csd.raw_hc_erase_gap_size =
347 ext_csd[EXT_CSD_PARTITION_ATTRIBUTE];
348 card->ext_csd.raw_sec_trim_mult =
349 ext_csd[EXT_CSD_SEC_TRIM_MULT];
350 card->ext_csd.raw_sec_erase_mult =
351 ext_csd[EXT_CSD_SEC_ERASE_MULT];
352 card->ext_csd.raw_sec_feature_support =
353 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
354 card->ext_csd.raw_trim_mult =
355 ext_csd[EXT_CSD_TRIM_MULT];
289 if (card->ext_csd.rev >= 4) { 356 if (card->ext_csd.rev >= 4) {
357 /*
358 * Enhanced area feature support -- check whether the eMMC
359 * card has the Enhanced area enabled. If so, export enhanced
360 * area offset and size to user by adding sysfs interface.
361 */
362 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
363 (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
364 u8 hc_erase_grp_sz =
365 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
366 u8 hc_wp_grp_sz =
367 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
368
369 card->ext_csd.enhanced_area_en = 1;
370 /*
371 * calculate the enhanced data area offset, in bytes
372 */
373 card->ext_csd.enhanced_area_offset =
374 (ext_csd[139] << 24) + (ext_csd[138] << 16) +
375 (ext_csd[137] << 8) + ext_csd[136];
376 if (mmc_card_blockaddr(card))
377 card->ext_csd.enhanced_area_offset <<= 9;
378 /*
379 * calculate the enhanced data area size, in kilobytes
380 */
381 card->ext_csd.enhanced_area_size =
382 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
383 ext_csd[140];
384 card->ext_csd.enhanced_area_size *=
385 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
386 card->ext_csd.enhanced_area_size <<= 9;
387 } else {
388 /*
389 * If the enhanced area is not enabled, disable these
390 * device attributes.
391 */
392 card->ext_csd.enhanced_area_offset = -EINVAL;
393 card->ext_csd.enhanced_area_size = -EINVAL;
394 }
290 card->ext_csd.sec_trim_mult = 395 card->ext_csd.sec_trim_mult =
291 ext_csd[EXT_CSD_SEC_TRIM_MULT]; 396 ext_csd[EXT_CSD_SEC_TRIM_MULT];
292 card->ext_csd.sec_erase_mult = 397 card->ext_csd.sec_erase_mult =
@@ -297,14 +402,83 @@ static int mmc_read_ext_csd(struct mmc_card *card)
297 ext_csd[EXT_CSD_TRIM_MULT]; 402 ext_csd[EXT_CSD_TRIM_MULT];
298 } 403 }
299 404
405 if (card->ext_csd.rev >= 5)
406 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
407
300 if (ext_csd[EXT_CSD_ERASED_MEM_CONT]) 408 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
301 card->erased_byte = 0xFF; 409 card->erased_byte = 0xFF;
302 else 410 else
303 card->erased_byte = 0x0; 411 card->erased_byte = 0x0;
304 412
305out: 413out:
414 return err;
415}
416
417static inline void mmc_free_ext_csd(u8 *ext_csd)
418{
306 kfree(ext_csd); 419 kfree(ext_csd);
420}
421
422
423static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
424{
425 u8 *bw_ext_csd;
426 int err;
307 427
428 if (bus_width == MMC_BUS_WIDTH_1)
429 return 0;
430
431 err = mmc_get_ext_csd(card, &bw_ext_csd);
432
433 if (err || bw_ext_csd == NULL) {
434 if (bus_width != MMC_BUS_WIDTH_1)
435 err = -EINVAL;
436 goto out;
437 }
438
439 if (bus_width == MMC_BUS_WIDTH_1)
440 goto out;
441
442 /* only compare read only fields */
443 err = (!(card->ext_csd.raw_partition_support ==
444 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
445 (card->ext_csd.raw_erased_mem_count ==
446 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
447 (card->ext_csd.rev ==
448 bw_ext_csd[EXT_CSD_REV]) &&
449 (card->ext_csd.raw_ext_csd_structure ==
450 bw_ext_csd[EXT_CSD_STRUCTURE]) &&
451 (card->ext_csd.raw_card_type ==
452 bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
453 (card->ext_csd.raw_s_a_timeout ==
454 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
455 (card->ext_csd.raw_hc_erase_gap_size ==
456 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
457 (card->ext_csd.raw_erase_timeout_mult ==
458 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
459 (card->ext_csd.raw_hc_erase_grp_size ==
460 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
461 (card->ext_csd.raw_sec_trim_mult ==
462 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
463 (card->ext_csd.raw_sec_erase_mult ==
464 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
465 (card->ext_csd.raw_sec_feature_support ==
466 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
467 (card->ext_csd.raw_trim_mult ==
468 bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
469 (card->ext_csd.raw_sectors[0] ==
470 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
471 (card->ext_csd.raw_sectors[1] ==
472 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
473 (card->ext_csd.raw_sectors[2] ==
474 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
475 (card->ext_csd.raw_sectors[3] ==
476 bw_ext_csd[EXT_CSD_SEC_CNT + 3]));
477 if (err)
478 err = -EINVAL;
479
480out:
481 mmc_free_ext_csd(bw_ext_csd);
308 return err; 482 return err;
309} 483}
310 484
@@ -321,6 +495,9 @@ MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
321MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name); 495MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
322MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid); 496MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
323MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial); 497MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
498MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
499 card->ext_csd.enhanced_area_offset);
500MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
324 501
325static struct attribute *mmc_std_attrs[] = { 502static struct attribute *mmc_std_attrs[] = {
326 &dev_attr_cid.attr, 503 &dev_attr_cid.attr,
@@ -334,6 +511,8 @@ static struct attribute *mmc_std_attrs[] = {
334 &dev_attr_name.attr, 511 &dev_attr_name.attr,
335 &dev_attr_oemid.attr, 512 &dev_attr_oemid.attr,
336 &dev_attr_serial.attr, 513 &dev_attr_serial.attr,
514 &dev_attr_enhanced_area_offset.attr,
515 &dev_attr_enhanced_area_size.attr,
337 NULL, 516 NULL,
338}; 517};
339 518
@@ -360,9 +539,11 @@ static int mmc_init_card(struct mmc_host *host, u32 ocr,
360 struct mmc_card *oldcard) 539 struct mmc_card *oldcard)
361{ 540{
362 struct mmc_card *card; 541 struct mmc_card *card;
363 int err; 542 int err, ddr = 0;
364 u32 cid[4]; 543 u32 cid[4];
365 unsigned int max_dtr; 544 unsigned int max_dtr;
545 u32 rocr;
546 u8 *ext_csd = NULL;
366 547
367 BUG_ON(!host); 548 BUG_ON(!host);
368 WARN_ON(!host->claimed); 549 WARN_ON(!host->claimed);
@@ -376,7 +557,7 @@ static int mmc_init_card(struct mmc_host *host, u32 ocr,
376 mmc_go_idle(host); 557 mmc_go_idle(host);
377 558
378 /* The extra bit indicates that we support high capacity */ 559 /* The extra bit indicates that we support high capacity */
379 err = mmc_send_op_cond(host, ocr | (1 << 30), NULL); 560 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
380 if (err) 561 if (err)
381 goto err; 562 goto err;
382 563
@@ -461,20 +642,76 @@ static int mmc_init_card(struct mmc_host *host, u32 ocr,
461 /* 642 /*
462 * Fetch and process extended CSD. 643 * Fetch and process extended CSD.
463 */ 644 */
464 err = mmc_read_ext_csd(card); 645
646 err = mmc_get_ext_csd(card, &ext_csd);
647 if (err)
648 goto free_card;
649 err = mmc_read_ext_csd(card, ext_csd);
465 if (err) 650 if (err)
466 goto free_card; 651 goto free_card;
652
653 /* If doing byte addressing, check if required to do sector
654 * addressing. Handle the case of <2GB cards needing sector
655 * addressing. See section 8.1 JEDEC Standard JED84-A441;
656 * ocr register has bit 30 set for sector addressing.
657 */
658 if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30)))
659 mmc_card_set_blockaddr(card);
660
467 /* Erase size depends on CSD and Extended CSD */ 661 /* Erase size depends on CSD and Extended CSD */
468 mmc_set_erase_size(card); 662 mmc_set_erase_size(card);
469 } 663 }
470 664
471 /* 665 /*
666 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
667 * bit. This bit will be lost every time after a reset or power off.
668 */
669 if (card->ext_csd.enhanced_area_en) {
670 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
671 EXT_CSD_ERASE_GROUP_DEF, 1, 0);
672
673 if (err && err != -EBADMSG)
674 goto free_card;
675
676 if (err) {
677 err = 0;
678 /*
679 * Just disable enhanced area off & sz
680 * will try to enable ERASE_GROUP_DEF
681 * during next time reinit
682 */
683 card->ext_csd.enhanced_area_offset = -EINVAL;
684 card->ext_csd.enhanced_area_size = -EINVAL;
685 } else {
686 card->ext_csd.erase_group_def = 1;
687 /*
688 * enable ERASE_GRP_DEF successfully.
689 * This will affect the erase size, so
690 * here need to reset erase size
691 */
692 mmc_set_erase_size(card);
693 }
694 }
695
696 /*
697 * Ensure eMMC user default partition is enabled
698 */
699 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
700 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
701 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
702 card->ext_csd.part_config,
703 card->ext_csd.part_time);
704 if (err && err != -EBADMSG)
705 goto free_card;
706 }
707
708 /*
472 * Activate high speed (if supported) 709 * Activate high speed (if supported)
473 */ 710 */
474 if ((card->ext_csd.hs_max_dtr != 0) && 711 if ((card->ext_csd.hs_max_dtr != 0) &&
475 (host->caps & MMC_CAP_MMC_HIGHSPEED)) { 712 (host->caps & MMC_CAP_MMC_HIGHSPEED)) {
476 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, 713 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
477 EXT_CSD_HS_TIMING, 1); 714 EXT_CSD_HS_TIMING, 1, 0);
478 if (err && err != -EBADMSG) 715 if (err && err != -EBADMSG)
479 goto free_card; 716 goto free_card;
480 717
@@ -503,32 +740,102 @@ static int mmc_init_card(struct mmc_host *host, u32 ocr,
503 mmc_set_clock(host, max_dtr); 740 mmc_set_clock(host, max_dtr);
504 741
505 /* 742 /*
506 * Activate wide bus (if supported). 743 * Indicate DDR mode (if supported).
744 */
745 if (mmc_card_highspeed(card)) {
746 if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_8V)
747 && ((host->caps & (MMC_CAP_1_8V_DDR |
748 MMC_CAP_UHS_DDR50))
749 == (MMC_CAP_1_8V_DDR | MMC_CAP_UHS_DDR50)))
750 ddr = MMC_1_8V_DDR_MODE;
751 else if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
752 && ((host->caps & (MMC_CAP_1_2V_DDR |
753 MMC_CAP_UHS_DDR50))
754 == (MMC_CAP_1_2V_DDR | MMC_CAP_UHS_DDR50)))
755 ddr = MMC_1_2V_DDR_MODE;
756 }
757
758 /*
759 * Activate wide bus and DDR (if supported).
507 */ 760 */
508 if ((card->csd.mmca_vsn >= CSD_SPEC_VER_4) && 761 if ((card->csd.mmca_vsn >= CSD_SPEC_VER_4) &&
509 (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) { 762 (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) {
510 unsigned ext_csd_bit, bus_width; 763 static unsigned ext_csd_bits[][2] = {
511 764 { EXT_CSD_BUS_WIDTH_8, EXT_CSD_DDR_BUS_WIDTH_8 },
512 if (host->caps & MMC_CAP_8_BIT_DATA) { 765 { EXT_CSD_BUS_WIDTH_4, EXT_CSD_DDR_BUS_WIDTH_4 },
513 ext_csd_bit = EXT_CSD_BUS_WIDTH_8; 766 { EXT_CSD_BUS_WIDTH_1, EXT_CSD_BUS_WIDTH_1 },
514 bus_width = MMC_BUS_WIDTH_8; 767 };
515 } else { 768 static unsigned bus_widths[] = {
516 ext_csd_bit = EXT_CSD_BUS_WIDTH_4; 769 MMC_BUS_WIDTH_8,
517 bus_width = MMC_BUS_WIDTH_4; 770 MMC_BUS_WIDTH_4,
771 MMC_BUS_WIDTH_1
772 };
773 unsigned idx, bus_width = 0;
774
775 if (host->caps & MMC_CAP_8_BIT_DATA)
776 idx = 0;
777 else
778 idx = 1;
779 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
780 bus_width = bus_widths[idx];
781 if (bus_width == MMC_BUS_WIDTH_1)
782 ddr = 0; /* no DDR for 1-bit width */
783 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
784 EXT_CSD_BUS_WIDTH,
785 ext_csd_bits[idx][0],
786 0);
787 if (!err) {
788 mmc_set_bus_width(card->host, bus_width);
789
790 /*
791 * If controller can't handle bus width test,
792 * compare ext_csd previously read in 1 bit mode
793 * against ext_csd at new bus width
794 */
795 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
796 err = mmc_compare_ext_csds(card,
797 bus_width);
798 else
799 err = mmc_bus_test(card, bus_width);
800 if (!err)
801 break;
802 }
518 } 803 }
519 804
520 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, 805 if (!err && ddr) {
521 EXT_CSD_BUS_WIDTH, ext_csd_bit); 806 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
522 807 EXT_CSD_BUS_WIDTH,
523 if (err && err != -EBADMSG) 808 ext_csd_bits[idx][1],
524 goto free_card; 809 0);
525 810 }
526 if (err) { 811 if (err) {
527 printk(KERN_WARNING "%s: switch to bus width %d " 812 printk(KERN_WARNING "%s: switch to bus width %d ddr %d "
528 "failed\n", mmc_hostname(card->host), 813 "failed\n", mmc_hostname(card->host),
529 1 << bus_width); 814 1 << bus_width, ddr);
530 err = 0; 815 goto free_card;
531 } else { 816 } else if (ddr) {
817 /*
818 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
819 * signaling.
820 *
821 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
822 *
823 * 1.8V vccq at 3.3V core voltage (vcc) is not required
824 * in the JEDEC spec for DDR.
825 *
826 * Do not force change in vccq since we are obviously
827 * working and no change to vccq is needed.
828 *
829 * WARNING: eMMC rules are NOT the same as SD DDR
830 */
831 if (ddr == EXT_CSD_CARD_TYPE_DDR_1_2V) {
832 err = mmc_set_signal_voltage(host,
833 MMC_SIGNAL_VOLTAGE_120, 0);
834 if (err)
835 goto err;
836 }
837 mmc_card_set_ddr_mode(card);
838 mmc_set_timing(card->host, MMC_TIMING_UHS_DDR50);
532 mmc_set_bus_width(card->host, bus_width); 839 mmc_set_bus_width(card->host, bus_width);
533 } 840 }
534 } 841 }
@@ -536,12 +843,14 @@ static int mmc_init_card(struct mmc_host *host, u32 ocr,
536 if (!oldcard) 843 if (!oldcard)
537 host->card = card; 844 host->card = card;
538 845
846 mmc_free_ext_csd(ext_csd);
539 return 0; 847 return 0;
540 848
541free_card: 849free_card:
542 if (!oldcard) 850 if (!oldcard)
543 mmc_remove_card(card); 851 mmc_remove_card(card);
544err: 852err:
853 mmc_free_ext_csd(ext_csd);
545 854
546 return err; 855 return err;
547} 856}
@@ -623,12 +932,16 @@ static int mmc_resume(struct mmc_host *host)
623 return err; 932 return err;
624} 933}
625 934
626static void mmc_power_restore(struct mmc_host *host) 935static int mmc_power_restore(struct mmc_host *host)
627{ 936{
937 int ret;
938
628 host->card->state &= ~MMC_STATE_HIGHSPEED; 939 host->card->state &= ~MMC_STATE_HIGHSPEED;
629 mmc_claim_host(host); 940 mmc_claim_host(host);
630 mmc_init_card(host, host->ocr, host->card); 941 ret = mmc_init_card(host, host->ocr, host->card);
631 mmc_release_host(host); 942 mmc_release_host(host);
943
944 return ret;
632} 945}
633 946
634static int mmc_sleep(struct mmc_host *host) 947static int mmc_sleep(struct mmc_host *host)
@@ -685,7 +998,7 @@ static void mmc_attach_bus_ops(struct mmc_host *host)
685{ 998{
686 const struct mmc_bus_ops *bus_ops; 999 const struct mmc_bus_ops *bus_ops;
687 1000
688 if (host->caps & MMC_CAP_NONREMOVABLE || !mmc_assume_removable) 1001 if (!mmc_card_is_removable(host))
689 bus_ops = &mmc_ops_unsafe; 1002 bus_ops = &mmc_ops_unsafe;
690 else 1003 else
691 bus_ops = &mmc_ops; 1004 bus_ops = &mmc_ops;
@@ -695,14 +1008,21 @@ static void mmc_attach_bus_ops(struct mmc_host *host)
695/* 1008/*
696 * Starting point for MMC card init. 1009 * Starting point for MMC card init.
697 */ 1010 */
698int mmc_attach_mmc(struct mmc_host *host, u32 ocr) 1011int mmc_attach_mmc(struct mmc_host *host)
699{ 1012{
700 int err; 1013 int err;
1014 u32 ocr;
701 1015
702 BUG_ON(!host); 1016 BUG_ON(!host);
703 WARN_ON(!host->claimed); 1017 WARN_ON(!host->claimed);
704 1018
1019 err = mmc_send_op_cond(host, 0, &ocr);
1020 if (err)
1021 return err;
1022
705 mmc_attach_bus_ops(host); 1023 mmc_attach_bus_ops(host);
1024 if (host->ocr_avail_mmc)
1025 host->ocr_avail = host->ocr_avail_mmc;
706 1026
707 /* 1027 /*
708 * We need to get OCR a different way for SPI. 1028 * We need to get OCR a different way for SPI.
@@ -742,20 +1062,20 @@ int mmc_attach_mmc(struct mmc_host *host, u32 ocr)
742 goto err; 1062 goto err;
743 1063
744 mmc_release_host(host); 1064 mmc_release_host(host);
745
746 err = mmc_add_card(host->card); 1065 err = mmc_add_card(host->card);
1066 mmc_claim_host(host);
747 if (err) 1067 if (err)
748 goto remove_card; 1068 goto remove_card;
749 1069
750 return 0; 1070 return 0;
751 1071
752remove_card: 1072remove_card:
1073 mmc_release_host(host);
753 mmc_remove_card(host->card); 1074 mmc_remove_card(host->card);
754 host->card = NULL;
755 mmc_claim_host(host); 1075 mmc_claim_host(host);
1076 host->card = NULL;
756err: 1077err:
757 mmc_detach_bus(host); 1078 mmc_detach_bus(host);
758 mmc_release_host(host);
759 1079
760 printk(KERN_ERR "%s: error %d whilst initialising MMC card\n", 1080 printk(KERN_ERR "%s: error %d whilst initialising MMC card\n",
761 mmc_hostname(host), err); 1081 mmc_hostname(host), err);
diff --git a/drivers/mmc/core/mmc_ops.c b/drivers/mmc/core/mmc_ops.c
index 326447c9ede8..845ce7c533b9 100644
--- a/drivers/mmc/core/mmc_ops.c
+++ b/drivers/mmc/core/mmc_ops.c
@@ -23,12 +23,10 @@
23static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card) 23static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card)
24{ 24{
25 int err; 25 int err;
26 struct mmc_command cmd; 26 struct mmc_command cmd = {0};
27 27
28 BUG_ON(!host); 28 BUG_ON(!host);
29 29
30 memset(&cmd, 0, sizeof(struct mmc_command));
31
32 cmd.opcode = MMC_SELECT_CARD; 30 cmd.opcode = MMC_SELECT_CARD;
33 31
34 if (card) { 32 if (card) {
@@ -60,15 +58,13 @@ int mmc_deselect_cards(struct mmc_host *host)
60 58
61int mmc_card_sleepawake(struct mmc_host *host, int sleep) 59int mmc_card_sleepawake(struct mmc_host *host, int sleep)
62{ 60{
63 struct mmc_command cmd; 61 struct mmc_command cmd = {0};
64 struct mmc_card *card = host->card; 62 struct mmc_card *card = host->card;
65 int err; 63 int err;
66 64
67 if (sleep) 65 if (sleep)
68 mmc_deselect_cards(host); 66 mmc_deselect_cards(host);
69 67
70 memset(&cmd, 0, sizeof(struct mmc_command));
71
72 cmd.opcode = MMC_SLEEP_AWAKE; 68 cmd.opcode = MMC_SLEEP_AWAKE;
73 cmd.arg = card->rca << 16; 69 cmd.arg = card->rca << 16;
74 if (sleep) 70 if (sleep)
@@ -97,7 +93,7 @@ int mmc_card_sleepawake(struct mmc_host *host, int sleep)
97int mmc_go_idle(struct mmc_host *host) 93int mmc_go_idle(struct mmc_host *host)
98{ 94{
99 int err; 95 int err;
100 struct mmc_command cmd; 96 struct mmc_command cmd = {0};
101 97
102 /* 98 /*
103 * Non-SPI hosts need to prevent chipselect going active during 99 * Non-SPI hosts need to prevent chipselect going active during
@@ -105,7 +101,7 @@ int mmc_go_idle(struct mmc_host *host)
105 * that in case of hardware that won't pull up DAT3/nCS otherwise. 101 * that in case of hardware that won't pull up DAT3/nCS otherwise.
106 * 102 *
107 * SPI hosts ignore ios.chip_select; it's managed according to 103 * SPI hosts ignore ios.chip_select; it's managed according to
108 * rules that must accomodate non-MMC slaves which this layer 104 * rules that must accommodate non-MMC slaves which this layer
109 * won't even know about. 105 * won't even know about.
110 */ 106 */
111 if (!mmc_host_is_spi(host)) { 107 if (!mmc_host_is_spi(host)) {
@@ -113,8 +109,6 @@ int mmc_go_idle(struct mmc_host *host)
113 mmc_delay(1); 109 mmc_delay(1);
114 } 110 }
115 111
116 memset(&cmd, 0, sizeof(struct mmc_command));
117
118 cmd.opcode = MMC_GO_IDLE_STATE; 112 cmd.opcode = MMC_GO_IDLE_STATE;
119 cmd.arg = 0; 113 cmd.arg = 0;
120 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC; 114 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
@@ -135,13 +129,11 @@ int mmc_go_idle(struct mmc_host *host)
135 129
136int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr) 130int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
137{ 131{
138 struct mmc_command cmd; 132 struct mmc_command cmd = {0};
139 int i, err = 0; 133 int i, err = 0;
140 134
141 BUG_ON(!host); 135 BUG_ON(!host);
142 136
143 memset(&cmd, 0, sizeof(struct mmc_command));
144
145 cmd.opcode = MMC_SEND_OP_COND; 137 cmd.opcode = MMC_SEND_OP_COND;
146 cmd.arg = mmc_host_is_spi(host) ? 0 : ocr; 138 cmd.arg = mmc_host_is_spi(host) ? 0 : ocr;
147 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR; 139 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
@@ -178,13 +170,11 @@ int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
178int mmc_all_send_cid(struct mmc_host *host, u32 *cid) 170int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
179{ 171{
180 int err; 172 int err;
181 struct mmc_command cmd; 173 struct mmc_command cmd = {0};
182 174
183 BUG_ON(!host); 175 BUG_ON(!host);
184 BUG_ON(!cid); 176 BUG_ON(!cid);
185 177
186 memset(&cmd, 0, sizeof(struct mmc_command));
187
188 cmd.opcode = MMC_ALL_SEND_CID; 178 cmd.opcode = MMC_ALL_SEND_CID;
189 cmd.arg = 0; 179 cmd.arg = 0;
190 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; 180 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
@@ -201,13 +191,11 @@ int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
201int mmc_set_relative_addr(struct mmc_card *card) 191int mmc_set_relative_addr(struct mmc_card *card)
202{ 192{
203 int err; 193 int err;
204 struct mmc_command cmd; 194 struct mmc_command cmd = {0};
205 195
206 BUG_ON(!card); 196 BUG_ON(!card);
207 BUG_ON(!card->host); 197 BUG_ON(!card->host);
208 198
209 memset(&cmd, 0, sizeof(struct mmc_command));
210
211 cmd.opcode = MMC_SET_RELATIVE_ADDR; 199 cmd.opcode = MMC_SET_RELATIVE_ADDR;
212 cmd.arg = card->rca << 16; 200 cmd.arg = card->rca << 16;
213 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 201 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
@@ -223,13 +211,11 @@ static int
223mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode) 211mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
224{ 212{
225 int err; 213 int err;
226 struct mmc_command cmd; 214 struct mmc_command cmd = {0};
227 215
228 BUG_ON(!host); 216 BUG_ON(!host);
229 BUG_ON(!cxd); 217 BUG_ON(!cxd);
230 218
231 memset(&cmd, 0, sizeof(struct mmc_command));
232
233 cmd.opcode = opcode; 219 cmd.opcode = opcode;
234 cmd.arg = arg; 220 cmd.arg = arg;
235 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC; 221 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
@@ -247,9 +233,9 @@ static int
247mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host, 233mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
248 u32 opcode, void *buf, unsigned len) 234 u32 opcode, void *buf, unsigned len)
249{ 235{
250 struct mmc_request mrq; 236 struct mmc_request mrq = {0};
251 struct mmc_command cmd; 237 struct mmc_command cmd = {0};
252 struct mmc_data data; 238 struct mmc_data data = {0};
253 struct scatterlist sg; 239 struct scatterlist sg;
254 void *data_buf; 240 void *data_buf;
255 241
@@ -260,10 +246,6 @@ mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
260 if (data_buf == NULL) 246 if (data_buf == NULL)
261 return -ENOMEM; 247 return -ENOMEM;
262 248
263 memset(&mrq, 0, sizeof(struct mmc_request));
264 memset(&cmd, 0, sizeof(struct mmc_command));
265 memset(&data, 0, sizeof(struct mmc_data));
266
267 mrq.cmd = &cmd; 249 mrq.cmd = &cmd;
268 mrq.data = &data; 250 mrq.data = &data;
269 251
@@ -355,11 +337,9 @@ int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
355 337
356int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp) 338int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
357{ 339{
358 struct mmc_command cmd; 340 struct mmc_command cmd = {0};
359 int err; 341 int err;
360 342
361 memset(&cmd, 0, sizeof(struct mmc_command));
362
363 cmd.opcode = MMC_SPI_READ_OCR; 343 cmd.opcode = MMC_SPI_READ_OCR;
364 cmd.arg = highcap ? (1 << 30) : 0; 344 cmd.arg = highcap ? (1 << 30) : 0;
365 cmd.flags = MMC_RSP_SPI_R3; 345 cmd.flags = MMC_RSP_SPI_R3;
@@ -372,11 +352,9 @@ int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
372 352
373int mmc_spi_set_crc(struct mmc_host *host, int use_crc) 353int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
374{ 354{
375 struct mmc_command cmd; 355 struct mmc_command cmd = {0};
376 int err; 356 int err;
377 357
378 memset(&cmd, 0, sizeof(struct mmc_command));
379
380 cmd.opcode = MMC_SPI_CRC_ON_OFF; 358 cmd.opcode = MMC_SPI_CRC_ON_OFF;
381 cmd.flags = MMC_RSP_SPI_R1; 359 cmd.flags = MMC_RSP_SPI_R1;
382 cmd.arg = use_crc; 360 cmd.arg = use_crc;
@@ -387,23 +365,34 @@ int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
387 return err; 365 return err;
388} 366}
389 367
390int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value) 368/**
369 * mmc_switch - modify EXT_CSD register
370 * @card: the MMC card associated with the data transfer
371 * @set: cmd set values
372 * @index: EXT_CSD register index
373 * @value: value to program into EXT_CSD register
374 * @timeout_ms: timeout (ms) for operation performed by register write,
375 * timeout of zero implies maximum possible timeout
376 *
377 * Modifies the EXT_CSD register for selected card.
378 */
379int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
380 unsigned int timeout_ms)
391{ 381{
392 int err; 382 int err;
393 struct mmc_command cmd; 383 struct mmc_command cmd = {0};
394 u32 status; 384 u32 status;
395 385
396 BUG_ON(!card); 386 BUG_ON(!card);
397 BUG_ON(!card->host); 387 BUG_ON(!card->host);
398 388
399 memset(&cmd, 0, sizeof(struct mmc_command));
400
401 cmd.opcode = MMC_SWITCH; 389 cmd.opcode = MMC_SWITCH;
402 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | 390 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
403 (index << 16) | 391 (index << 16) |
404 (value << 8) | 392 (value << 8) |
405 set; 393 set;
406 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; 394 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
395 cmd.cmd_timeout_ms = timeout_ms;
407 396
408 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES); 397 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
409 if (err) 398 if (err)
@@ -433,17 +422,16 @@ int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value)
433 422
434 return 0; 423 return 0;
435} 424}
425EXPORT_SYMBOL_GPL(mmc_switch);
436 426
437int mmc_send_status(struct mmc_card *card, u32 *status) 427int mmc_send_status(struct mmc_card *card, u32 *status)
438{ 428{
439 int err; 429 int err;
440 struct mmc_command cmd; 430 struct mmc_command cmd = {0};
441 431
442 BUG_ON(!card); 432 BUG_ON(!card);
443 BUG_ON(!card->host); 433 BUG_ON(!card->host);
444 434
445 memset(&cmd, 0, sizeof(struct mmc_command));
446
447 cmd.opcode = MMC_SEND_STATUS; 435 cmd.opcode = MMC_SEND_STATUS;
448 if (!mmc_host_is_spi(card->host)) 436 if (!mmc_host_is_spi(card->host))
449 cmd.arg = card->rca << 16; 437 cmd.arg = card->rca << 16;
@@ -462,3 +450,100 @@ int mmc_send_status(struct mmc_card *card, u32 *status)
462 return 0; 450 return 0;
463} 451}
464 452
453static int
454mmc_send_bus_test(struct mmc_card *card, struct mmc_host *host, u8 opcode,
455 u8 len)
456{
457 struct mmc_request mrq = {0};
458 struct mmc_command cmd = {0};
459 struct mmc_data data = {0};
460 struct scatterlist sg;
461 u8 *data_buf;
462 u8 *test_buf;
463 int i, err;
464 static u8 testdata_8bit[8] = { 0x55, 0xaa, 0, 0, 0, 0, 0, 0 };
465 static u8 testdata_4bit[4] = { 0x5a, 0, 0, 0 };
466
467 /* dma onto stack is unsafe/nonportable, but callers to this
468 * routine normally provide temporary on-stack buffers ...
469 */
470 data_buf = kmalloc(len, GFP_KERNEL);
471 if (!data_buf)
472 return -ENOMEM;
473
474 if (len == 8)
475 test_buf = testdata_8bit;
476 else if (len == 4)
477 test_buf = testdata_4bit;
478 else {
479 printk(KERN_ERR "%s: Invalid bus_width %d\n",
480 mmc_hostname(host), len);
481 kfree(data_buf);
482 return -EINVAL;
483 }
484
485 if (opcode == MMC_BUS_TEST_W)
486 memcpy(data_buf, test_buf, len);
487
488 mrq.cmd = &cmd;
489 mrq.data = &data;
490 cmd.opcode = opcode;
491 cmd.arg = 0;
492
493 /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
494 * rely on callers to never use this with "native" calls for reading
495 * CSD or CID. Native versions of those commands use the R2 type,
496 * not R1 plus a data block.
497 */
498 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
499
500 data.blksz = len;
501 data.blocks = 1;
502 if (opcode == MMC_BUS_TEST_R)
503 data.flags = MMC_DATA_READ;
504 else
505 data.flags = MMC_DATA_WRITE;
506
507 data.sg = &sg;
508 data.sg_len = 1;
509 sg_init_one(&sg, data_buf, len);
510 mmc_wait_for_req(host, &mrq);
511 err = 0;
512 if (opcode == MMC_BUS_TEST_R) {
513 for (i = 0; i < len / 4; i++)
514 if ((test_buf[i] ^ data_buf[i]) != 0xff) {
515 err = -EIO;
516 break;
517 }
518 }
519 kfree(data_buf);
520
521 if (cmd.error)
522 return cmd.error;
523 if (data.error)
524 return data.error;
525
526 return err;
527}
528
529int mmc_bus_test(struct mmc_card *card, u8 bus_width)
530{
531 int err, width;
532
533 if (bus_width == MMC_BUS_WIDTH_8)
534 width = 8;
535 else if (bus_width == MMC_BUS_WIDTH_4)
536 width = 4;
537 else if (bus_width == MMC_BUS_WIDTH_1)
538 return 0; /* no need for test */
539 else
540 return -EINVAL;
541
542 /*
543 * Ignore errors from BUS_TEST_W. BUS_TEST_R will fail if there
544 * is a problem. This improves chances that the test will work.
545 */
546 mmc_send_bus_test(card, card->host, MMC_BUS_TEST_W, width);
547 err = mmc_send_bus_test(card, card->host, MMC_BUS_TEST_R, width);
548 return err;
549}
diff --git a/drivers/mmc/core/mmc_ops.h b/drivers/mmc/core/mmc_ops.h
index 653eb8e84178..9276946fa5b7 100644
--- a/drivers/mmc/core/mmc_ops.h
+++ b/drivers/mmc/core/mmc_ops.h
@@ -20,12 +20,12 @@ int mmc_all_send_cid(struct mmc_host *host, u32 *cid);
20int mmc_set_relative_addr(struct mmc_card *card); 20int mmc_set_relative_addr(struct mmc_card *card);
21int mmc_send_csd(struct mmc_card *card, u32 *csd); 21int mmc_send_csd(struct mmc_card *card, u32 *csd);
22int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd); 22int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd);
23int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value);
24int mmc_send_status(struct mmc_card *card, u32 *status); 23int mmc_send_status(struct mmc_card *card, u32 *status);
25int mmc_send_cid(struct mmc_host *host, u32 *cid); 24int mmc_send_cid(struct mmc_host *host, u32 *cid);
26int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp); 25int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp);
27int mmc_spi_set_crc(struct mmc_host *host, int use_crc); 26int mmc_spi_set_crc(struct mmc_host *host, int use_crc);
28int mmc_card_sleepawake(struct mmc_host *host, int sleep); 27int mmc_card_sleepawake(struct mmc_host *host, int sleep);
28int mmc_bus_test(struct mmc_card *card, u8 bus_width);
29 29
30#endif 30#endif
31 31
diff --git a/drivers/mmc/core/quirks.c b/drivers/mmc/core/quirks.c
new file mode 100644
index 000000000000..3a596217029e
--- /dev/null
+++ b/drivers/mmc/core/quirks.c
@@ -0,0 +1,79 @@
1/*
2 * This file contains work-arounds for many known SD/MMC
3 * and SDIO hardware bugs.
4 *
5 * Copyright (c) 2011 Andrei Warkentin <andreiw@motorola.com>
6 * Copyright (c) 2011 Pierre Tardy <tardyp@gmail.com>
7 * Inspired from pci fixup code:
8 * Copyright (c) 1999 Martin Mares <mj@ucw.cz>
9 *
10 */
11
12#include <linux/types.h>
13#include <linux/kernel.h>
14#include <linux/mmc/card.h>
15
16#ifndef SDIO_VENDOR_ID_TI
17#define SDIO_VENDOR_ID_TI 0x0097
18#endif
19
20#ifndef SDIO_DEVICE_ID_TI_WL1271
21#define SDIO_DEVICE_ID_TI_WL1271 0x4076
22#endif
23
24/*
25 * This hook just adds a quirk for all sdio devices
26 */
27static void add_quirk_for_sdio_devices(struct mmc_card *card, int data)
28{
29 if (mmc_card_sdio(card))
30 card->quirks |= data;
31}
32
33static const struct mmc_fixup mmc_fixup_methods[] = {
34 /* by default sdio devices are considered CLK_GATING broken */
35 /* good cards will be whitelisted as they are tested */
36 SDIO_FIXUP(SDIO_ANY_ID, SDIO_ANY_ID,
37 add_quirk_for_sdio_devices,
38 MMC_QUIRK_BROKEN_CLK_GATING),
39
40 SDIO_FIXUP(SDIO_VENDOR_ID_TI, SDIO_DEVICE_ID_TI_WL1271,
41 remove_quirk, MMC_QUIRK_BROKEN_CLK_GATING),
42
43 SDIO_FIXUP(SDIO_VENDOR_ID_TI, SDIO_DEVICE_ID_TI_WL1271,
44 add_quirk, MMC_QUIRK_NONSTD_FUNC_IF),
45
46 SDIO_FIXUP(SDIO_VENDOR_ID_TI, SDIO_DEVICE_ID_TI_WL1271,
47 add_quirk, MMC_QUIRK_DISABLE_CD),
48
49 END_FIXUP
50};
51
52void mmc_fixup_device(struct mmc_card *card, const struct mmc_fixup *table)
53{
54 const struct mmc_fixup *f;
55 u64 rev = cid_rev_card(card);
56
57 /* Non-core specific workarounds. */
58 if (!table)
59 table = mmc_fixup_methods;
60
61 for (f = table; f->vendor_fixup; f++) {
62 if ((f->manfid == CID_MANFID_ANY ||
63 f->manfid == card->cid.manfid) &&
64 (f->oemid == CID_OEMID_ANY ||
65 f->oemid == card->cid.oemid) &&
66 (f->name == CID_NAME_ANY ||
67 !strncmp(f->name, card->cid.prod_name,
68 sizeof(card->cid.prod_name))) &&
69 (f->cis_vendor == card->cis.vendor ||
70 f->cis_vendor == (u16) SDIO_ANY_ID) &&
71 (f->cis_device == card->cis.device ||
72 f->cis_device == (u16) SDIO_ANY_ID) &&
73 rev >= f->rev_start && rev <= f->rev_end) {
74 dev_dbg(&card->dev, "calling %pF\n", f->vendor_fixup);
75 f->vendor_fixup(card, f->data);
76 }
77 }
78}
79EXPORT_SYMBOL(mmc_fixup_device);
diff --git a/drivers/mmc/core/sd.c b/drivers/mmc/core/sd.c
index 0f5241085557..ff2774128aa9 100644
--- a/drivers/mmc/core/sd.c
+++ b/drivers/mmc/core/sd.c
@@ -21,6 +21,7 @@
21#include "core.h" 21#include "core.h"
22#include "bus.h" 22#include "bus.h"
23#include "mmc_ops.h" 23#include "mmc_ops.h"
24#include "sd.h"
24#include "sd_ops.h" 25#include "sd_ops.h"
25 26
26static const unsigned int tran_exp[] = { 27static const unsigned int tran_exp[] = {
@@ -129,7 +130,7 @@ static int mmc_decode_csd(struct mmc_card *card)
129 break; 130 break;
130 case 1: 131 case 1:
131 /* 132 /*
132 * This is a block-addressed SDHC card. Most 133 * This is a block-addressed SDHC or SDXC card. Most
133 * interesting fields are unused and have fixed 134 * interesting fields are unused and have fixed
134 * values. To avoid getting tripped by buggy cards, 135 * values. To avoid getting tripped by buggy cards,
135 * we assume those fixed values ourselves. 136 * we assume those fixed values ourselves.
@@ -143,6 +144,11 @@ static int mmc_decode_csd(struct mmc_card *card)
143 e = UNSTUFF_BITS(resp, 96, 3); 144 e = UNSTUFF_BITS(resp, 96, 3);
144 csd->max_dtr = tran_exp[e] * tran_mant[m]; 145 csd->max_dtr = tran_exp[e] * tran_mant[m];
145 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); 146 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
147 csd->c_size = UNSTUFF_BITS(resp, 48, 22);
148
149 /* SDXC cards have a minimum C_SIZE of 0x00FFFF */
150 if (csd->c_size >= 0xFFFF)
151 mmc_card_set_ext_capacity(card);
146 152
147 m = UNSTUFF_BITS(resp, 48, 22); 153 m = UNSTUFF_BITS(resp, 48, 22);
148 csd->capacity = (1 + m) << 10; 154 csd->capacity = (1 + m) << 10;
@@ -188,12 +194,17 @@ static int mmc_decode_scr(struct mmc_card *card)
188 194
189 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4); 195 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
190 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4); 196 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
197 if (scr->sda_vsn == SCR_SPEC_VER_2)
198 /* Check if Physical Layer Spec v3.0 is supported */
199 scr->sda_spec3 = UNSTUFF_BITS(resp, 47, 1);
191 200
192 if (UNSTUFF_BITS(resp, 55, 1)) 201 if (UNSTUFF_BITS(resp, 55, 1))
193 card->erased_byte = 0xFF; 202 card->erased_byte = 0xFF;
194 else 203 else
195 card->erased_byte = 0x0; 204 card->erased_byte = 0x0;
196 205
206 if (scr->sda_spec3)
207 scr->cmds = UNSTUFF_BITS(resp, 32, 2);
197 return 0; 208 return 0;
198} 209}
199 210
@@ -273,29 +284,74 @@ static int mmc_read_switch(struct mmc_card *card)
273 status = kmalloc(64, GFP_KERNEL); 284 status = kmalloc(64, GFP_KERNEL);
274 if (!status) { 285 if (!status) {
275 printk(KERN_ERR "%s: could not allocate a buffer for " 286 printk(KERN_ERR "%s: could not allocate a buffer for "
276 "switch capabilities.\n", mmc_hostname(card->host)); 287 "switch capabilities.\n",
288 mmc_hostname(card->host));
277 return -ENOMEM; 289 return -ENOMEM;
278 } 290 }
279 291
292 /* Find out the supported Bus Speed Modes. */
280 err = mmc_sd_switch(card, 0, 0, 1, status); 293 err = mmc_sd_switch(card, 0, 0, 1, status);
281 if (err) { 294 if (err) {
282 /* If the host or the card can't do the switch, 295 /*
283 * fail more gracefully. */ 296 * If the host or the card can't do the switch,
284 if ((err != -EINVAL) 297 * fail more gracefully.
285 && (err != -ENOSYS) 298 */
286 && (err != -EFAULT)) 299 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
287 goto out; 300 goto out;
288 301
289 printk(KERN_WARNING "%s: problem reading switch " 302 printk(KERN_WARNING "%s: problem reading Bus Speed modes.\n",
290 "capabilities, performance might suffer.\n",
291 mmc_hostname(card->host)); 303 mmc_hostname(card->host));
292 err = 0; 304 err = 0;
293 305
294 goto out; 306 goto out;
295 } 307 }
296 308
297 if (status[13] & 0x02) 309 if (card->scr.sda_spec3) {
298 card->sw_caps.hs_max_dtr = 50000000; 310 card->sw_caps.sd3_bus_mode = status[13];
311
312 /* Find out Driver Strengths supported by the card */
313 err = mmc_sd_switch(card, 0, 2, 1, status);
314 if (err) {
315 /*
316 * If the host or the card can't do the switch,
317 * fail more gracefully.
318 */
319 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
320 goto out;
321
322 printk(KERN_WARNING "%s: problem reading "
323 "Driver Strength.\n",
324 mmc_hostname(card->host));
325 err = 0;
326
327 goto out;
328 }
329
330 card->sw_caps.sd3_drv_type = status[9];
331
332 /* Find out Current Limits supported by the card */
333 err = mmc_sd_switch(card, 0, 3, 1, status);
334 if (err) {
335 /*
336 * If the host or the card can't do the switch,
337 * fail more gracefully.
338 */
339 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
340 goto out;
341
342 printk(KERN_WARNING "%s: problem reading "
343 "Current Limit.\n",
344 mmc_hostname(card->host));
345 err = 0;
346
347 goto out;
348 }
349
350 card->sw_caps.sd3_curr_limit = status[7];
351 } else {
352 if (status[13] & 0x02)
353 card->sw_caps.hs_max_dtr = 50000000;
354 }
299 355
300out: 356out:
301 kfree(status); 357 kfree(status);
@@ -351,6 +407,232 @@ out:
351 return err; 407 return err;
352} 408}
353 409
410static int sd_select_driver_type(struct mmc_card *card, u8 *status)
411{
412 int host_drv_type = 0, card_drv_type = 0;
413 int err;
414
415 /*
416 * If the host doesn't support any of the Driver Types A,C or D,
417 * default Driver Type B is used.
418 */
419 if (!(card->host->caps & (MMC_CAP_DRIVER_TYPE_A | MMC_CAP_DRIVER_TYPE_C
420 | MMC_CAP_DRIVER_TYPE_D)))
421 return 0;
422
423 if (card->host->caps & MMC_CAP_DRIVER_TYPE_A) {
424 host_drv_type = MMC_SET_DRIVER_TYPE_A;
425 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A)
426 card_drv_type = MMC_SET_DRIVER_TYPE_A;
427 else if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_B)
428 card_drv_type = MMC_SET_DRIVER_TYPE_B;
429 else if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
430 card_drv_type = MMC_SET_DRIVER_TYPE_C;
431 } else if (card->host->caps & MMC_CAP_DRIVER_TYPE_C) {
432 host_drv_type = MMC_SET_DRIVER_TYPE_C;
433 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
434 card_drv_type = MMC_SET_DRIVER_TYPE_C;
435 } else if (!(card->host->caps & MMC_CAP_DRIVER_TYPE_D)) {
436 /*
437 * If we are here, that means only the default driver type
438 * B is supported by the host.
439 */
440 host_drv_type = MMC_SET_DRIVER_TYPE_B;
441 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_B)
442 card_drv_type = MMC_SET_DRIVER_TYPE_B;
443 else if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
444 card_drv_type = MMC_SET_DRIVER_TYPE_C;
445 }
446
447 err = mmc_sd_switch(card, 1, 2, card_drv_type, status);
448 if (err)
449 return err;
450
451 if ((status[15] & 0xF) != card_drv_type) {
452 printk(KERN_WARNING "%s: Problem setting driver strength!\n",
453 mmc_hostname(card->host));
454 return 0;
455 }
456
457 mmc_set_driver_type(card->host, host_drv_type);
458
459 return 0;
460}
461
462static int sd_set_bus_speed_mode(struct mmc_card *card, u8 *status)
463{
464 unsigned int bus_speed = 0, timing = 0;
465 int err;
466
467 /*
468 * If the host doesn't support any of the UHS-I modes, fallback on
469 * default speed.
470 */
471 if (!(card->host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
472 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50)))
473 return 0;
474
475 if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
476 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
477 bus_speed = UHS_SDR104_BUS_SPEED;
478 timing = MMC_TIMING_UHS_SDR104;
479 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
480 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
481 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
482 bus_speed = UHS_DDR50_BUS_SPEED;
483 timing = MMC_TIMING_UHS_DDR50;
484 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
485 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
486 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
487 SD_MODE_UHS_SDR50)) {
488 bus_speed = UHS_SDR50_BUS_SPEED;
489 timing = MMC_TIMING_UHS_SDR50;
490 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
491 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
492 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
493 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
494 bus_speed = UHS_SDR25_BUS_SPEED;
495 timing = MMC_TIMING_UHS_SDR25;
496 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
497 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
498 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
499 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
500 SD_MODE_UHS_SDR12)) {
501 bus_speed = UHS_SDR12_BUS_SPEED;
502 timing = MMC_TIMING_UHS_SDR12;
503 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
504 }
505
506 card->sd_bus_speed = bus_speed;
507 err = mmc_sd_switch(card, 1, 0, bus_speed, status);
508 if (err)
509 return err;
510
511 if ((status[16] & 0xF) != bus_speed)
512 printk(KERN_WARNING "%s: Problem setting bus speed mode!\n",
513 mmc_hostname(card->host));
514 else {
515 mmc_set_timing(card->host, timing);
516 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
517 }
518
519 return 0;
520}
521
522static int sd_set_current_limit(struct mmc_card *card, u8 *status)
523{
524 int current_limit = 0;
525 int err;
526
527 /*
528 * Current limit switch is only defined for SDR50, SDR104, and DDR50
529 * bus speed modes. For other bus speed modes, we set the default
530 * current limit of 200mA.
531 */
532 if ((card->sd_bus_speed == UHS_SDR50_BUS_SPEED) ||
533 (card->sd_bus_speed == UHS_SDR104_BUS_SPEED) ||
534 (card->sd_bus_speed == UHS_DDR50_BUS_SPEED)) {
535 if (card->host->caps & MMC_CAP_MAX_CURRENT_800) {
536 if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_800)
537 current_limit = SD_SET_CURRENT_LIMIT_800;
538 else if (card->sw_caps.sd3_curr_limit &
539 SD_MAX_CURRENT_600)
540 current_limit = SD_SET_CURRENT_LIMIT_600;
541 else if (card->sw_caps.sd3_curr_limit &
542 SD_MAX_CURRENT_400)
543 current_limit = SD_SET_CURRENT_LIMIT_400;
544 else if (card->sw_caps.sd3_curr_limit &
545 SD_MAX_CURRENT_200)
546 current_limit = SD_SET_CURRENT_LIMIT_200;
547 } else if (card->host->caps & MMC_CAP_MAX_CURRENT_600) {
548 if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_600)
549 current_limit = SD_SET_CURRENT_LIMIT_600;
550 else if (card->sw_caps.sd3_curr_limit &
551 SD_MAX_CURRENT_400)
552 current_limit = SD_SET_CURRENT_LIMIT_400;
553 else if (card->sw_caps.sd3_curr_limit &
554 SD_MAX_CURRENT_200)
555 current_limit = SD_SET_CURRENT_LIMIT_200;
556 } else if (card->host->caps & MMC_CAP_MAX_CURRENT_400) {
557 if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_400)
558 current_limit = SD_SET_CURRENT_LIMIT_400;
559 else if (card->sw_caps.sd3_curr_limit &
560 SD_MAX_CURRENT_200)
561 current_limit = SD_SET_CURRENT_LIMIT_200;
562 } else if (card->host->caps & MMC_CAP_MAX_CURRENT_200) {
563 if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_200)
564 current_limit = SD_SET_CURRENT_LIMIT_200;
565 }
566 } else
567 current_limit = SD_SET_CURRENT_LIMIT_200;
568
569 err = mmc_sd_switch(card, 1, 3, current_limit, status);
570 if (err)
571 return err;
572
573 if (((status[15] >> 4) & 0x0F) != current_limit)
574 printk(KERN_WARNING "%s: Problem setting current limit!\n",
575 mmc_hostname(card->host));
576
577 return 0;
578}
579
580/*
581 * UHS-I specific initialization procedure
582 */
583static int mmc_sd_init_uhs_card(struct mmc_card *card)
584{
585 int err;
586 u8 *status;
587
588 if (!card->scr.sda_spec3)
589 return 0;
590
591 if (!(card->csd.cmdclass & CCC_SWITCH))
592 return 0;
593
594 status = kmalloc(64, GFP_KERNEL);
595 if (!status) {
596 printk(KERN_ERR "%s: could not allocate a buffer for "
597 "switch capabilities.\n", mmc_hostname(card->host));
598 return -ENOMEM;
599 }
600
601 /* Set 4-bit bus width */
602 if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
603 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
604 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
605 if (err)
606 goto out;
607
608 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
609 }
610
611 /* Set the driver strength for the card */
612 err = sd_select_driver_type(card, status);
613 if (err)
614 goto out;
615
616 /* Set bus speed mode of the card */
617 err = sd_set_bus_speed_mode(card, status);
618 if (err)
619 goto out;
620
621 /* Set current limit for the card */
622 err = sd_set_current_limit(card, status);
623 if (err)
624 goto out;
625
626 /* SPI mode doesn't define CMD19 */
627 if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning)
628 err = card->host->ops->execute_tuning(card->host);
629
630out:
631 kfree(status);
632
633 return err;
634}
635
354MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1], 636MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
355 card->raw_cid[2], card->raw_cid[3]); 637 card->raw_cid[2], card->raw_cid[3]);
356MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1], 638MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
@@ -399,7 +681,7 @@ struct device_type sd_type = {
399/* 681/*
400 * Fetch CID from card. 682 * Fetch CID from card.
401 */ 683 */
402int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid) 684int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid, u32 *rocr)
403{ 685{
404 int err; 686 int err;
405 687
@@ -419,12 +701,39 @@ int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid)
419 */ 701 */
420 err = mmc_send_if_cond(host, ocr); 702 err = mmc_send_if_cond(host, ocr);
421 if (!err) 703 if (!err)
422 ocr |= 1 << 30; 704 ocr |= SD_OCR_CCS;
705
706 /*
707 * If the host supports one of UHS-I modes, request the card
708 * to switch to 1.8V signaling level.
709 */
710 if (host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
711 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50))
712 ocr |= SD_OCR_S18R;
713
714 /* If the host can supply more than 150mA, XPC should be set to 1. */
715 if (host->caps & (MMC_CAP_SET_XPC_330 | MMC_CAP_SET_XPC_300 |
716 MMC_CAP_SET_XPC_180))
717 ocr |= SD_OCR_XPC;
423 718
424 err = mmc_send_app_op_cond(host, ocr, NULL); 719try_again:
720 err = mmc_send_app_op_cond(host, ocr, rocr);
425 if (err) 721 if (err)
426 return err; 722 return err;
427 723
724 /*
725 * In case CCS and S18A in the response is set, start Signal Voltage
726 * Switch procedure. SPI mode doesn't support CMD11.
727 */
728 if (!mmc_host_is_spi(host) && rocr &&
729 ((*rocr & 0x41000000) == 0x41000000)) {
730 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180, true);
731 if (err) {
732 ocr &= ~SD_OCR_S18R;
733 goto try_again;
734 }
735 }
736
428 if (mmc_host_is_spi(host)) 737 if (mmc_host_is_spi(host))
429 err = mmc_send_cid(host, cid); 738 err = mmc_send_cid(host, cid);
430 else 739 else
@@ -552,11 +861,12 @@ static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
552 struct mmc_card *card; 861 struct mmc_card *card;
553 int err; 862 int err;
554 u32 cid[4]; 863 u32 cid[4];
864 u32 rocr = 0;
555 865
556 BUG_ON(!host); 866 BUG_ON(!host);
557 WARN_ON(!host->claimed); 867 WARN_ON(!host->claimed);
558 868
559 err = mmc_sd_get_cid(host, ocr, cid); 869 err = mmc_sd_get_cid(host, ocr, cid, &rocr);
560 if (err) 870 if (err)
561 return err; 871 return err;
562 872
@@ -609,30 +919,47 @@ static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
609 if (err) 919 if (err)
610 goto free_card; 920 goto free_card;
611 921
612 /* 922 /* Initialization sequence for UHS-I cards */
613 * Attempt to change to high-speed (if supported) 923 if (rocr & SD_ROCR_S18A) {
614 */ 924 err = mmc_sd_init_uhs_card(card);
615 err = mmc_sd_switch_hs(card); 925 if (err)
616 if (err > 0) 926 goto free_card;
617 mmc_sd_go_highspeed(card);
618 else if (err)
619 goto free_card;
620 927
621 /* 928 /* Card is an ultra-high-speed card */
622 * Set bus speed. 929 mmc_sd_card_set_uhs(card);
623 */
624 mmc_set_clock(host, mmc_sd_get_max_clock(card));
625 930
626 /* 931 /*
627 * Switch to wider bus (if supported). 932 * Since initialization is now complete, enable preset
628 */ 933 * value registers for UHS-I cards.
629 if ((host->caps & MMC_CAP_4_BIT_DATA) && 934 */
630 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) { 935 if (host->ops->enable_preset_value)
631 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4); 936 host->ops->enable_preset_value(host, true);
632 if (err) 937 } else {
938 /*
939 * Attempt to change to high-speed (if supported)
940 */
941 err = mmc_sd_switch_hs(card);
942 if (err > 0)
943 mmc_sd_go_highspeed(card);
944 else if (err)
633 goto free_card; 945 goto free_card;
634 946
635 mmc_set_bus_width(host, MMC_BUS_WIDTH_4); 947 /*
948 * Set bus speed.
949 */
950 mmc_set_clock(host, mmc_sd_get_max_clock(card));
951
952 /*
953 * Switch to wider bus (if supported).
954 */
955 if ((host->caps & MMC_CAP_4_BIT_DATA) &&
956 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
957 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
958 if (err)
959 goto free_card;
960
961 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
962 }
636 } 963 }
637 964
638 host->card = card; 965 host->card = card;
@@ -722,12 +1049,16 @@ static int mmc_sd_resume(struct mmc_host *host)
722 return err; 1049 return err;
723} 1050}
724 1051
725static void mmc_sd_power_restore(struct mmc_host *host) 1052static int mmc_sd_power_restore(struct mmc_host *host)
726{ 1053{
1054 int ret;
1055
727 host->card->state &= ~MMC_STATE_HIGHSPEED; 1056 host->card->state &= ~MMC_STATE_HIGHSPEED;
728 mmc_claim_host(host); 1057 mmc_claim_host(host);
729 mmc_sd_init_card(host, host->ocr, host->card); 1058 ret = mmc_sd_init_card(host, host->ocr, host->card);
730 mmc_release_host(host); 1059 mmc_release_host(host);
1060
1061 return ret;
731} 1062}
732 1063
733static const struct mmc_bus_ops mmc_sd_ops = { 1064static const struct mmc_bus_ops mmc_sd_ops = {
@@ -750,7 +1081,7 @@ static void mmc_sd_attach_bus_ops(struct mmc_host *host)
750{ 1081{
751 const struct mmc_bus_ops *bus_ops; 1082 const struct mmc_bus_ops *bus_ops;
752 1083
753 if (host->caps & MMC_CAP_NONREMOVABLE || !mmc_assume_removable) 1084 if (!mmc_card_is_removable(host))
754 bus_ops = &mmc_sd_ops_unsafe; 1085 bus_ops = &mmc_sd_ops_unsafe;
755 else 1086 else
756 bus_ops = &mmc_sd_ops; 1087 bus_ops = &mmc_sd_ops;
@@ -760,14 +1091,30 @@ static void mmc_sd_attach_bus_ops(struct mmc_host *host)
760/* 1091/*
761 * Starting point for SD card init. 1092 * Starting point for SD card init.
762 */ 1093 */
763int mmc_attach_sd(struct mmc_host *host, u32 ocr) 1094int mmc_attach_sd(struct mmc_host *host)
764{ 1095{
765 int err; 1096 int err;
1097 u32 ocr;
766 1098
767 BUG_ON(!host); 1099 BUG_ON(!host);
768 WARN_ON(!host->claimed); 1100 WARN_ON(!host->claimed);
769 1101
1102 /* Make sure we are at 3.3V signalling voltage */
1103 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, false);
1104 if (err)
1105 return err;
1106
1107 /* Disable preset value enable if already set since last time */
1108 if (host->ops->enable_preset_value)
1109 host->ops->enable_preset_value(host, false);
1110
1111 err = mmc_send_app_op_cond(host, 0, &ocr);
1112 if (err)
1113 return err;
1114
770 mmc_sd_attach_bus_ops(host); 1115 mmc_sd_attach_bus_ops(host);
1116 if (host->ocr_avail_sd)
1117 host->ocr_avail = host->ocr_avail_sd;
771 1118
772 /* 1119 /*
773 * We need to get OCR a different way for SPI. 1120 * We need to get OCR a different way for SPI.
@@ -791,7 +1138,8 @@ int mmc_attach_sd(struct mmc_host *host, u32 ocr)
791 ocr &= ~0x7F; 1138 ocr &= ~0x7F;
792 } 1139 }
793 1140
794 if (ocr & MMC_VDD_165_195) { 1141 if ((ocr & MMC_VDD_165_195) &&
1142 !(host->ocr_avail_sd & MMC_VDD_165_195)) {
795 printk(KERN_WARNING "%s: SD card claims to support the " 1143 printk(KERN_WARNING "%s: SD card claims to support the "
796 "incompletely defined 'low voltage range'. This " 1144 "incompletely defined 'low voltage range'. This "
797 "will be ignored.\n", mmc_hostname(host)); 1145 "will be ignored.\n", mmc_hostname(host));
@@ -816,20 +1164,20 @@ int mmc_attach_sd(struct mmc_host *host, u32 ocr)
816 goto err; 1164 goto err;
817 1165
818 mmc_release_host(host); 1166 mmc_release_host(host);
819
820 err = mmc_add_card(host->card); 1167 err = mmc_add_card(host->card);
1168 mmc_claim_host(host);
821 if (err) 1169 if (err)
822 goto remove_card; 1170 goto remove_card;
823 1171
824 return 0; 1172 return 0;
825 1173
826remove_card: 1174remove_card:
1175 mmc_release_host(host);
827 mmc_remove_card(host->card); 1176 mmc_remove_card(host->card);
828 host->card = NULL; 1177 host->card = NULL;
829 mmc_claim_host(host); 1178 mmc_claim_host(host);
830err: 1179err:
831 mmc_detach_bus(host); 1180 mmc_detach_bus(host);
832 mmc_release_host(host);
833 1181
834 printk(KERN_ERR "%s: error %d whilst initialising SD card\n", 1182 printk(KERN_ERR "%s: error %d whilst initialising SD card\n",
835 mmc_hostname(host), err); 1183 mmc_hostname(host), err);
diff --git a/drivers/mmc/core/sd.h b/drivers/mmc/core/sd.h
index 3d8800fa7600..4b34b24f3f76 100644
--- a/drivers/mmc/core/sd.h
+++ b/drivers/mmc/core/sd.h
@@ -5,7 +5,7 @@
5 5
6extern struct device_type sd_type; 6extern struct device_type sd_type;
7 7
8int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid); 8int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid, u32 *rocr);
9int mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card); 9int mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card);
10void mmc_decode_cid(struct mmc_card *card); 10void mmc_decode_cid(struct mmc_card *card);
11int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card, 11int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
diff --git a/drivers/mmc/core/sd_ops.c b/drivers/mmc/core/sd_ops.c
index 797cdb5887fd..021fed153804 100644
--- a/drivers/mmc/core/sd_ops.c
+++ b/drivers/mmc/core/sd_ops.c
@@ -9,6 +9,7 @@
9 * your option) any later version. 9 * your option) any later version.
10 */ 10 */
11 11
12#include <linux/slab.h>
12#include <linux/types.h> 13#include <linux/types.h>
13#include <linux/scatterlist.h> 14#include <linux/scatterlist.h>
14 15
@@ -20,10 +21,10 @@
20#include "core.h" 21#include "core.h"
21#include "sd_ops.h" 22#include "sd_ops.h"
22 23
23static int mmc_app_cmd(struct mmc_host *host, struct mmc_card *card) 24int mmc_app_cmd(struct mmc_host *host, struct mmc_card *card)
24{ 25{
25 int err; 26 int err;
26 struct mmc_command cmd; 27 struct mmc_command cmd = {0};
27 28
28 BUG_ON(!host); 29 BUG_ON(!host);
29 BUG_ON(card && (card->host != host)); 30 BUG_ON(card && (card->host != host));
@@ -48,6 +49,7 @@ static int mmc_app_cmd(struct mmc_host *host, struct mmc_card *card)
48 49
49 return 0; 50 return 0;
50} 51}
52EXPORT_SYMBOL_GPL(mmc_app_cmd);
51 53
52/** 54/**
53 * mmc_wait_for_app_cmd - start an application command and wait for 55 * mmc_wait_for_app_cmd - start an application command and wait for
@@ -65,7 +67,7 @@ static int mmc_app_cmd(struct mmc_host *host, struct mmc_card *card)
65int mmc_wait_for_app_cmd(struct mmc_host *host, struct mmc_card *card, 67int mmc_wait_for_app_cmd(struct mmc_host *host, struct mmc_card *card,
66 struct mmc_command *cmd, int retries) 68 struct mmc_command *cmd, int retries)
67{ 69{
68 struct mmc_request mrq; 70 struct mmc_request mrq = {0};
69 71
70 int i, err; 72 int i, err;
71 73
@@ -118,13 +120,11 @@ EXPORT_SYMBOL(mmc_wait_for_app_cmd);
118int mmc_app_set_bus_width(struct mmc_card *card, int width) 120int mmc_app_set_bus_width(struct mmc_card *card, int width)
119{ 121{
120 int err; 122 int err;
121 struct mmc_command cmd; 123 struct mmc_command cmd = {0};
122 124
123 BUG_ON(!card); 125 BUG_ON(!card);
124 BUG_ON(!card->host); 126 BUG_ON(!card->host);
125 127
126 memset(&cmd, 0, sizeof(struct mmc_command));
127
128 cmd.opcode = SD_APP_SET_BUS_WIDTH; 128 cmd.opcode = SD_APP_SET_BUS_WIDTH;
129 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 129 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
130 130
@@ -148,13 +148,11 @@ int mmc_app_set_bus_width(struct mmc_card *card, int width)
148 148
149int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr) 149int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
150{ 150{
151 struct mmc_command cmd; 151 struct mmc_command cmd = {0};
152 int i, err = 0; 152 int i, err = 0;
153 153
154 BUG_ON(!host); 154 BUG_ON(!host);
155 155
156 memset(&cmd, 0, sizeof(struct mmc_command));
157
158 cmd.opcode = SD_APP_OP_COND; 156 cmd.opcode = SD_APP_OP_COND;
159 if (mmc_host_is_spi(host)) 157 if (mmc_host_is_spi(host))
160 cmd.arg = ocr & (1 << 30); /* SPI only defines one bit */ 158 cmd.arg = ocr & (1 << 30); /* SPI only defines one bit */
@@ -193,7 +191,7 @@ int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
193 191
194int mmc_send_if_cond(struct mmc_host *host, u32 ocr) 192int mmc_send_if_cond(struct mmc_host *host, u32 ocr)
195{ 193{
196 struct mmc_command cmd; 194 struct mmc_command cmd = {0};
197 int err; 195 int err;
198 static const u8 test_pattern = 0xAA; 196 static const u8 test_pattern = 0xAA;
199 u8 result_pattern; 197 u8 result_pattern;
@@ -225,13 +223,11 @@ int mmc_send_if_cond(struct mmc_host *host, u32 ocr)
225int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca) 223int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca)
226{ 224{
227 int err; 225 int err;
228 struct mmc_command cmd; 226 struct mmc_command cmd = {0};
229 227
230 BUG_ON(!host); 228 BUG_ON(!host);
231 BUG_ON(!rca); 229 BUG_ON(!rca);
232 230
233 memset(&cmd, 0, sizeof(struct mmc_command));
234
235 cmd.opcode = SD_SEND_RELATIVE_ADDR; 231 cmd.opcode = SD_SEND_RELATIVE_ADDR;
236 cmd.arg = 0; 232 cmd.arg = 0;
237 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR; 233 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
@@ -248,10 +244,11 @@ int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca)
248int mmc_app_send_scr(struct mmc_card *card, u32 *scr) 244int mmc_app_send_scr(struct mmc_card *card, u32 *scr)
249{ 245{
250 int err; 246 int err;
251 struct mmc_request mrq; 247 struct mmc_request mrq = {0};
252 struct mmc_command cmd; 248 struct mmc_command cmd = {0};
253 struct mmc_data data; 249 struct mmc_data data = {0};
254 struct scatterlist sg; 250 struct scatterlist sg;
251 void *data_buf;
255 252
256 BUG_ON(!card); 253 BUG_ON(!card);
257 BUG_ON(!card->host); 254 BUG_ON(!card->host);
@@ -263,9 +260,12 @@ int mmc_app_send_scr(struct mmc_card *card, u32 *scr)
263 if (err) 260 if (err)
264 return err; 261 return err;
265 262
266 memset(&mrq, 0, sizeof(struct mmc_request)); 263 /* dma onto stack is unsafe/nonportable, but callers to this
267 memset(&cmd, 0, sizeof(struct mmc_command)); 264 * routine normally provide temporary on-stack buffers ...
268 memset(&data, 0, sizeof(struct mmc_data)); 265 */
266 data_buf = kmalloc(sizeof(card->raw_scr), GFP_KERNEL);
267 if (data_buf == NULL)
268 return -ENOMEM;
269 269
270 mrq.cmd = &cmd; 270 mrq.cmd = &cmd;
271 mrq.data = &data; 271 mrq.data = &data;
@@ -280,12 +280,15 @@ int mmc_app_send_scr(struct mmc_card *card, u32 *scr)
280 data.sg = &sg; 280 data.sg = &sg;
281 data.sg_len = 1; 281 data.sg_len = 1;
282 282
283 sg_init_one(&sg, scr, 8); 283 sg_init_one(&sg, data_buf, 8);
284 284
285 mmc_set_data_timeout(&data, card); 285 mmc_set_data_timeout(&data, card);
286 286
287 mmc_wait_for_req(card->host, &mrq); 287 mmc_wait_for_req(card->host, &mrq);
288 288
289 memcpy(scr, data_buf, sizeof(card->raw_scr));
290 kfree(data_buf);
291
289 if (cmd.error) 292 if (cmd.error)
290 return cmd.error; 293 return cmd.error;
291 if (data.error) 294 if (data.error)
@@ -300,9 +303,9 @@ int mmc_app_send_scr(struct mmc_card *card, u32 *scr)
300int mmc_sd_switch(struct mmc_card *card, int mode, int group, 303int mmc_sd_switch(struct mmc_card *card, int mode, int group,
301 u8 value, u8 *resp) 304 u8 value, u8 *resp)
302{ 305{
303 struct mmc_request mrq; 306 struct mmc_request mrq = {0};
304 struct mmc_command cmd; 307 struct mmc_command cmd = {0};
305 struct mmc_data data; 308 struct mmc_data data = {0};
306 struct scatterlist sg; 309 struct scatterlist sg;
307 310
308 BUG_ON(!card); 311 BUG_ON(!card);
@@ -313,10 +316,6 @@ int mmc_sd_switch(struct mmc_card *card, int mode, int group,
313 mode = !!mode; 316 mode = !!mode;
314 value &= 0xF; 317 value &= 0xF;
315 318
316 memset(&mrq, 0, sizeof(struct mmc_request));
317 memset(&cmd, 0, sizeof(struct mmc_command));
318 memset(&data, 0, sizeof(struct mmc_data));
319
320 mrq.cmd = &cmd; 319 mrq.cmd = &cmd;
321 mrq.data = &data; 320 mrq.data = &data;
322 321
@@ -349,9 +348,9 @@ int mmc_sd_switch(struct mmc_card *card, int mode, int group,
349int mmc_app_sd_status(struct mmc_card *card, void *ssr) 348int mmc_app_sd_status(struct mmc_card *card, void *ssr)
350{ 349{
351 int err; 350 int err;
352 struct mmc_request mrq; 351 struct mmc_request mrq = {0};
353 struct mmc_command cmd; 352 struct mmc_command cmd = {0};
354 struct mmc_data data; 353 struct mmc_data data = {0};
355 struct scatterlist sg; 354 struct scatterlist sg;
356 355
357 BUG_ON(!card); 356 BUG_ON(!card);
@@ -364,10 +363,6 @@ int mmc_app_sd_status(struct mmc_card *card, void *ssr)
364 if (err) 363 if (err)
365 return err; 364 return err;
366 365
367 memset(&mrq, 0, sizeof(struct mmc_request));
368 memset(&cmd, 0, sizeof(struct mmc_command));
369 memset(&data, 0, sizeof(struct mmc_data));
370
371 mrq.cmd = &cmd; 366 mrq.cmd = &cmd;
372 mrq.data = &data; 367 mrq.data = &data;
373 368
diff --git a/drivers/mmc/core/sdio.c b/drivers/mmc/core/sdio.c
index f332c52968b7..262fff019177 100644
--- a/drivers/mmc/core/sdio.c
+++ b/drivers/mmc/core/sdio.c
@@ -10,11 +10,13 @@
10 */ 10 */
11 11
12#include <linux/err.h> 12#include <linux/err.h>
13#include <linux/pm_runtime.h>
13 14
14#include <linux/mmc/host.h> 15#include <linux/mmc/host.h>
15#include <linux/mmc/card.h> 16#include <linux/mmc/card.h>
16#include <linux/mmc/sdio.h> 17#include <linux/mmc/sdio.h>
17#include <linux/mmc/sdio_func.h> 18#include <linux/mmc/sdio_func.h>
19#include <linux/mmc/sdio_ids.h>
18 20
19#include "core.h" 21#include "core.h"
20#include "bus.h" 22#include "bus.h"
@@ -30,6 +32,11 @@ static int sdio_read_fbr(struct sdio_func *func)
30 int ret; 32 int ret;
31 unsigned char data; 33 unsigned char data;
32 34
35 if (mmc_card_nonstd_func_interface(func->card)) {
36 func->class = SDIO_CLASS_NONE;
37 return 0;
38 }
39
33 ret = mmc_io_rw_direct(func->card, 0, 0, 40 ret = mmc_io_rw_direct(func->card, 0, 0,
34 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data); 41 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
35 if (ret) 42 if (ret)
@@ -180,7 +187,7 @@ static int sdio_disable_cd(struct mmc_card *card)
180 int ret; 187 int ret;
181 u8 ctrl; 188 u8 ctrl;
182 189
183 if (!card->cccr.disable_cd) 190 if (!mmc_card_disable_cd(card))
184 return 0; 191 return 0;
185 192
186 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl); 193 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
@@ -362,8 +369,8 @@ static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
362 goto err; 369 goto err;
363 } 370 }
364 371
365 if (ocr & R4_MEMORY_PRESENT 372 if ((ocr & R4_MEMORY_PRESENT) &&
366 && mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid) == 0) { 373 mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid, NULL) == 0) {
367 card->type = MMC_TYPE_SD_COMBO; 374 card->type = MMC_TYPE_SD_COMBO;
368 375
369 if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO || 376 if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
@@ -394,6 +401,14 @@ static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
394 if (err) 401 if (err)
395 goto remove; 402 goto remove;
396 403
404 /*
405 * Update oldcard with the new RCA received from the SDIO
406 * device -- we're doing this so that it's updated in the
407 * "card" struct when oldcard overwrites that later.
408 */
409 if (oldcard)
410 oldcard->rca = card->rca;
411
397 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); 412 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
398 } 413 }
399 414
@@ -456,8 +471,8 @@ static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
456 return -ENOENT; 471 return -ENOENT;
457 472
458 card = oldcard; 473 card = oldcard;
459 return 0;
460 } 474 }
475 mmc_fixup_device(card, NULL);
461 476
462 if (card->type == MMC_TYPE_SD_COMBO) { 477 if (card->type == MMC_TYPE_SD_COMBO) {
463 err = mmc_sd_setup_card(host, card, oldcard != NULL); 478 err = mmc_sd_setup_card(host, card, oldcard != NULL);
@@ -546,6 +561,13 @@ static void mmc_sdio_detect(struct mmc_host *host)
546 BUG_ON(!host); 561 BUG_ON(!host);
547 BUG_ON(!host->card); 562 BUG_ON(!host->card);
548 563
564 /* Make sure card is powered before detecting it */
565 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
566 err = pm_runtime_get_sync(&host->card->dev);
567 if (err < 0)
568 goto out;
569 }
570
549 mmc_claim_host(host); 571 mmc_claim_host(host);
550 572
551 /* 573 /*
@@ -555,6 +577,21 @@ static void mmc_sdio_detect(struct mmc_host *host)
555 577
556 mmc_release_host(host); 578 mmc_release_host(host);
557 579
580 /*
581 * Tell PM core it's OK to power off the card now.
582 *
583 * The _sync variant is used in order to ensure that the card
584 * is left powered off in case an error occurred, and the card
585 * is going to be removed.
586 *
587 * Since there is no specific reason to believe a new user
588 * is about to show up at this point, the _sync variant is
589 * desirable anyway.
590 */
591 if (host->caps & MMC_CAP_POWER_OFF_CARD)
592 pm_runtime_put_sync(&host->card->dev);
593
594out:
558 if (err) { 595 if (err) {
559 mmc_sdio_remove(host); 596 mmc_sdio_remove(host);
560 597
@@ -594,7 +631,7 @@ static int mmc_sdio_suspend(struct mmc_host *host)
594 } 631 }
595 } 632 }
596 633
597 if (!err && host->pm_flags & MMC_PM_KEEP_POWER) { 634 if (!err && mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
598 mmc_claim_host(host); 635 mmc_claim_host(host);
599 sdio_disable_wide(host->card); 636 sdio_disable_wide(host->card);
600 mmc_release_host(host); 637 mmc_release_host(host);
@@ -605,23 +642,27 @@ static int mmc_sdio_suspend(struct mmc_host *host)
605 642
606static int mmc_sdio_resume(struct mmc_host *host) 643static int mmc_sdio_resume(struct mmc_host *host)
607{ 644{
608 int i, err; 645 int i, err = 0;
609 646
610 BUG_ON(!host); 647 BUG_ON(!host);
611 BUG_ON(!host->card); 648 BUG_ON(!host->card);
612 649
613 /* Basic card reinitialization. */ 650 /* Basic card reinitialization. */
614 mmc_claim_host(host); 651 mmc_claim_host(host);
615 err = mmc_sdio_init_card(host, host->ocr, host->card, 652
616 (host->pm_flags & MMC_PM_KEEP_POWER)); 653 /* No need to reinitialize powered-resumed nonremovable cards */
617 if (!err) { 654 if (mmc_card_is_removable(host) || !mmc_card_keep_power(host))
618 /* We may have switched to 1-bit mode during suspend. */ 655 err = mmc_sdio_init_card(host, host->ocr, host->card,
656 mmc_card_keep_power(host));
657 else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
658 /* We may have switched to 1-bit mode during suspend */
619 err = sdio_enable_4bit_bus(host->card); 659 err = sdio_enable_4bit_bus(host->card);
620 if (err > 0) { 660 if (err > 0) {
621 mmc_set_bus_width(host, MMC_BUS_WIDTH_4); 661 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
622 err = 0; 662 err = 0;
623 } 663 }
624 } 664 }
665
625 if (!err && host->sdio_irqs) 666 if (!err && host->sdio_irqs)
626 mmc_signal_sdio_irq(host); 667 mmc_signal_sdio_irq(host);
627 mmc_release_host(host); 668 mmc_release_host(host);
@@ -647,27 +688,90 @@ static int mmc_sdio_resume(struct mmc_host *host)
647 return err; 688 return err;
648} 689}
649 690
691static int mmc_sdio_power_restore(struct mmc_host *host)
692{
693 int ret;
694 u32 ocr;
695
696 BUG_ON(!host);
697 BUG_ON(!host->card);
698
699 mmc_claim_host(host);
700
701 /*
702 * Reset the card by performing the same steps that are taken by
703 * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
704 *
705 * sdio_reset() is technically not needed. Having just powered up the
706 * hardware, it should already be in reset state. However, some
707 * platforms (such as SD8686 on OLPC) do not instantly cut power,
708 * meaning that a reset is required when restoring power soon after
709 * powering off. It is harmless in other cases.
710 *
711 * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
712 * is not necessary for non-removable cards. However, it is required
713 * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
714 * harmless in other situations.
715 *
716 * With these steps taken, mmc_select_voltage() is also required to
717 * restore the correct voltage setting of the card.
718 */
719 sdio_reset(host);
720 mmc_go_idle(host);
721 mmc_send_if_cond(host, host->ocr_avail);
722
723 ret = mmc_send_io_op_cond(host, 0, &ocr);
724 if (ret)
725 goto out;
726
727 if (host->ocr_avail_sdio)
728 host->ocr_avail = host->ocr_avail_sdio;
729
730 host->ocr = mmc_select_voltage(host, ocr & ~0x7F);
731 if (!host->ocr) {
732 ret = -EINVAL;
733 goto out;
734 }
735
736 ret = mmc_sdio_init_card(host, host->ocr, host->card,
737 mmc_card_keep_power(host));
738 if (!ret && host->sdio_irqs)
739 mmc_signal_sdio_irq(host);
740
741out:
742 mmc_release_host(host);
743
744 return ret;
745}
746
650static const struct mmc_bus_ops mmc_sdio_ops = { 747static const struct mmc_bus_ops mmc_sdio_ops = {
651 .remove = mmc_sdio_remove, 748 .remove = mmc_sdio_remove,
652 .detect = mmc_sdio_detect, 749 .detect = mmc_sdio_detect,
653 .suspend = mmc_sdio_suspend, 750 .suspend = mmc_sdio_suspend,
654 .resume = mmc_sdio_resume, 751 .resume = mmc_sdio_resume,
752 .power_restore = mmc_sdio_power_restore,
655}; 753};
656 754
657 755
658/* 756/*
659 * Starting point for SDIO card init. 757 * Starting point for SDIO card init.
660 */ 758 */
661int mmc_attach_sdio(struct mmc_host *host, u32 ocr) 759int mmc_attach_sdio(struct mmc_host *host)
662{ 760{
663 int err; 761 int err, i, funcs;
664 int i, funcs; 762 u32 ocr;
665 struct mmc_card *card; 763 struct mmc_card *card;
666 764
667 BUG_ON(!host); 765 BUG_ON(!host);
668 WARN_ON(!host->claimed); 766 WARN_ON(!host->claimed);
669 767
768 err = mmc_send_io_op_cond(host, 0, &ocr);
769 if (err)
770 return err;
771
670 mmc_attach_bus(host, &mmc_sdio_ops); 772 mmc_attach_bus(host, &mmc_sdio_ops);
773 if (host->ocr_avail_sdio)
774 host->ocr_avail = host->ocr_avail_sdio;
671 775
672 /* 776 /*
673 * Sanity check the voltages that the card claims to 777 * Sanity check the voltages that the card claims to
@@ -699,6 +803,23 @@ int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
699 card = host->card; 803 card = host->card;
700 804
701 /* 805 /*
806 * Enable runtime PM only if supported by host+card+board
807 */
808 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
809 /*
810 * Let runtime PM core know our card is active
811 */
812 err = pm_runtime_set_active(&card->dev);
813 if (err)
814 goto remove;
815
816 /*
817 * Enable runtime PM for this card
818 */
819 pm_runtime_enable(&card->dev);
820 }
821
822 /*
702 * The number of functions on the card is encoded inside 823 * The number of functions on the card is encoded inside
703 * the ocr. 824 * the ocr.
704 */ 825 */
@@ -712,13 +833,18 @@ int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
712 err = sdio_init_func(host->card, i + 1); 833 err = sdio_init_func(host->card, i + 1);
713 if (err) 834 if (err)
714 goto remove; 835 goto remove;
715 }
716 836
717 mmc_release_host(host); 837 /*
838 * Enable Runtime PM for this func (if supported)
839 */
840 if (host->caps & MMC_CAP_POWER_OFF_CARD)
841 pm_runtime_enable(&card->sdio_func[i]->dev);
842 }
718 843
719 /* 844 /*
720 * First add the card to the driver model... 845 * First add the card to the driver model...
721 */ 846 */
847 mmc_release_host(host);
722 err = mmc_add_card(host->card); 848 err = mmc_add_card(host->card);
723 if (err) 849 if (err)
724 goto remove_added; 850 goto remove_added;
@@ -732,6 +858,7 @@ int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
732 goto remove_added; 858 goto remove_added;
733 } 859 }
734 860
861 mmc_claim_host(host);
735 return 0; 862 return 0;
736 863
737 864
@@ -741,11 +868,12 @@ remove_added:
741 mmc_claim_host(host); 868 mmc_claim_host(host);
742remove: 869remove:
743 /* And with lock if it hasn't been added. */ 870 /* And with lock if it hasn't been added. */
871 mmc_release_host(host);
744 if (host->card) 872 if (host->card)
745 mmc_sdio_remove(host); 873 mmc_sdio_remove(host);
874 mmc_claim_host(host);
746err: 875err:
747 mmc_detach_bus(host); 876 mmc_detach_bus(host);
748 mmc_release_host(host);
749 877
750 printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n", 878 printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
751 mmc_hostname(host), err); 879 mmc_hostname(host), err);
diff --git a/drivers/mmc/core/sdio_bus.c b/drivers/mmc/core/sdio_bus.c
index 4a890dcb95ab..d2565df8a7fb 100644
--- a/drivers/mmc/core/sdio_bus.c
+++ b/drivers/mmc/core/sdio_bus.c
@@ -14,8 +14,10 @@
14#include <linux/device.h> 14#include <linux/device.h>
15#include <linux/err.h> 15#include <linux/err.h>
16#include <linux/slab.h> 16#include <linux/slab.h>
17#include <linux/pm_runtime.h>
17 18
18#include <linux/mmc/card.h> 19#include <linux/mmc/card.h>
20#include <linux/mmc/host.h>
19#include <linux/mmc/sdio_func.h> 21#include <linux/mmc/sdio_func.h>
20 22
21#include "sdio_cis.h" 23#include "sdio_cis.h"
@@ -125,21 +127,51 @@ static int sdio_bus_probe(struct device *dev)
125 if (!id) 127 if (!id)
126 return -ENODEV; 128 return -ENODEV;
127 129
130 /* Unbound SDIO functions are always suspended.
131 * During probe, the function is set active and the usage count
132 * is incremented. If the driver supports runtime PM,
133 * it should call pm_runtime_put_noidle() in its probe routine and
134 * pm_runtime_get_noresume() in its remove routine.
135 */
136 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD) {
137 ret = pm_runtime_get_sync(dev);
138 if (ret < 0)
139 goto out;
140 }
141
128 /* Set the default block size so the driver is sure it's something 142 /* Set the default block size so the driver is sure it's something
129 * sensible. */ 143 * sensible. */
130 sdio_claim_host(func); 144 sdio_claim_host(func);
131 ret = sdio_set_block_size(func, 0); 145 ret = sdio_set_block_size(func, 0);
132 sdio_release_host(func); 146 sdio_release_host(func);
133 if (ret) 147 if (ret)
134 return ret; 148 goto disable_runtimepm;
149
150 ret = drv->probe(func, id);
151 if (ret)
152 goto disable_runtimepm;
153
154 return 0;
135 155
136 return drv->probe(func, id); 156disable_runtimepm:
157 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
158 pm_runtime_put_noidle(dev);
159out:
160 return ret;
137} 161}
138 162
139static int sdio_bus_remove(struct device *dev) 163static int sdio_bus_remove(struct device *dev)
140{ 164{
141 struct sdio_driver *drv = to_sdio_driver(dev->driver); 165 struct sdio_driver *drv = to_sdio_driver(dev->driver);
142 struct sdio_func *func = dev_to_sdio_func(dev); 166 struct sdio_func *func = dev_to_sdio_func(dev);
167 int ret = 0;
168
169 /* Make sure card is powered before invoking ->remove() */
170 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD) {
171 ret = pm_runtime_get_sync(dev);
172 if (ret < 0)
173 goto out;
174 }
143 175
144 drv->remove(func); 176 drv->remove(func);
145 177
@@ -151,9 +183,36 @@ static int sdio_bus_remove(struct device *dev)
151 sdio_release_host(func); 183 sdio_release_host(func);
152 } 184 }
153 185
154 return 0; 186 /* First, undo the increment made directly above */
187 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
188 pm_runtime_put_noidle(dev);
189
190 /* Then undo the runtime PM settings in sdio_bus_probe() */
191 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
192 pm_runtime_put_sync(dev);
193
194out:
195 return ret;
155} 196}
156 197
198#ifdef CONFIG_PM_RUNTIME
199
200static const struct dev_pm_ops sdio_bus_pm_ops = {
201 SET_RUNTIME_PM_OPS(
202 pm_generic_runtime_suspend,
203 pm_generic_runtime_resume,
204 pm_generic_runtime_idle
205 )
206};
207
208#define SDIO_PM_OPS_PTR (&sdio_bus_pm_ops)
209
210#else /* !CONFIG_PM_RUNTIME */
211
212#define SDIO_PM_OPS_PTR NULL
213
214#endif /* !CONFIG_PM_RUNTIME */
215
157static struct bus_type sdio_bus_type = { 216static struct bus_type sdio_bus_type = {
158 .name = "sdio", 217 .name = "sdio",
159 .dev_attrs = sdio_dev_attrs, 218 .dev_attrs = sdio_dev_attrs,
@@ -161,6 +220,7 @@ static struct bus_type sdio_bus_type = {
161 .uevent = sdio_bus_uevent, 220 .uevent = sdio_bus_uevent,
162 .probe = sdio_bus_probe, 221 .probe = sdio_bus_probe,
163 .remove = sdio_bus_remove, 222 .remove = sdio_bus_remove,
223 .pm = SDIO_PM_OPS_PTR,
164}; 224};
165 225
166int sdio_register_bus(void) 226int sdio_register_bus(void)
diff --git a/drivers/mmc/core/sdio_irq.c b/drivers/mmc/core/sdio_irq.c
index bb192f90e8e9..03ead028d2ce 100644
--- a/drivers/mmc/core/sdio_irq.c
+++ b/drivers/mmc/core/sdio_irq.c
@@ -31,6 +31,17 @@ static int process_sdio_pending_irqs(struct mmc_card *card)
31{ 31{
32 int i, ret, count; 32 int i, ret, count;
33 unsigned char pending; 33 unsigned char pending;
34 struct sdio_func *func;
35
36 /*
37 * Optimization, if there is only 1 function interrupt registered
38 * call irq handler directly
39 */
40 func = card->sdio_single_irq;
41 if (func) {
42 func->irq_handler(func);
43 return 1;
44 }
34 45
35 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_INTx, 0, &pending); 46 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_INTx, 0, &pending);
36 if (ret) { 47 if (ret) {
@@ -42,10 +53,10 @@ static int process_sdio_pending_irqs(struct mmc_card *card)
42 count = 0; 53 count = 0;
43 for (i = 1; i <= 7; i++) { 54 for (i = 1; i <= 7; i++) {
44 if (pending & (1 << i)) { 55 if (pending & (1 << i)) {
45 struct sdio_func *func = card->sdio_func[i - 1]; 56 func = card->sdio_func[i - 1];
46 if (!func) { 57 if (!func) {
47 printk(KERN_WARNING "%s: pending IRQ for " 58 printk(KERN_WARNING "%s: pending IRQ for "
48 "non-existant function\n", 59 "non-existent function\n",
49 mmc_card_id(card)); 60 mmc_card_id(card));
50 ret = -EINVAL; 61 ret = -EINVAL;
51 } else if (func->irq_handler) { 62 } else if (func->irq_handler) {
@@ -186,6 +197,24 @@ static int sdio_card_irq_put(struct mmc_card *card)
186 return 0; 197 return 0;
187} 198}
188 199
200/* If there is only 1 function registered set sdio_single_irq */
201static void sdio_single_irq_set(struct mmc_card *card)
202{
203 struct sdio_func *func;
204 int i;
205
206 card->sdio_single_irq = NULL;
207 if ((card->host->caps & MMC_CAP_SDIO_IRQ) &&
208 card->host->sdio_irqs == 1)
209 for (i = 0; i < card->sdio_funcs; i++) {
210 func = card->sdio_func[i];
211 if (func && func->irq_handler) {
212 card->sdio_single_irq = func;
213 break;
214 }
215 }
216}
217
189/** 218/**
190 * sdio_claim_irq - claim the IRQ for a SDIO function 219 * sdio_claim_irq - claim the IRQ for a SDIO function
191 * @func: SDIO function 220 * @func: SDIO function
@@ -227,6 +256,7 @@ int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler)
227 ret = sdio_card_irq_get(func->card); 256 ret = sdio_card_irq_get(func->card);
228 if (ret) 257 if (ret)
229 func->irq_handler = NULL; 258 func->irq_handler = NULL;
259 sdio_single_irq_set(func->card);
230 260
231 return ret; 261 return ret;
232} 262}
@@ -251,6 +281,7 @@ int sdio_release_irq(struct sdio_func *func)
251 if (func->irq_handler) { 281 if (func->irq_handler) {
252 func->irq_handler = NULL; 282 func->irq_handler = NULL;
253 sdio_card_irq_put(func->card); 283 sdio_card_irq_put(func->card);
284 sdio_single_irq_set(func->card);
254 } 285 }
255 286
256 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg); 287 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
diff --git a/drivers/mmc/core/sdio_ops.c b/drivers/mmc/core/sdio_ops.c
index dea36d9c22e6..f087d876c573 100644
--- a/drivers/mmc/core/sdio_ops.c
+++ b/drivers/mmc/core/sdio_ops.c
@@ -21,13 +21,11 @@
21 21
22int mmc_send_io_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr) 22int mmc_send_io_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
23{ 23{
24 struct mmc_command cmd; 24 struct mmc_command cmd = {0};
25 int i, err = 0; 25 int i, err = 0;
26 26
27 BUG_ON(!host); 27 BUG_ON(!host);
28 28
29 memset(&cmd, 0, sizeof(struct mmc_command));
30
31 cmd.opcode = SD_IO_SEND_OP_COND; 29 cmd.opcode = SD_IO_SEND_OP_COND;
32 cmd.arg = ocr; 30 cmd.arg = ocr;
33 cmd.flags = MMC_RSP_SPI_R4 | MMC_RSP_R4 | MMC_CMD_BCR; 31 cmd.flags = MMC_RSP_SPI_R4 | MMC_RSP_R4 | MMC_CMD_BCR;
@@ -70,7 +68,7 @@ int mmc_send_io_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
70static int mmc_io_rw_direct_host(struct mmc_host *host, int write, unsigned fn, 68static int mmc_io_rw_direct_host(struct mmc_host *host, int write, unsigned fn,
71 unsigned addr, u8 in, u8 *out) 69 unsigned addr, u8 in, u8 *out)
72{ 70{
73 struct mmc_command cmd; 71 struct mmc_command cmd = {0};
74 int err; 72 int err;
75 73
76 BUG_ON(!host); 74 BUG_ON(!host);
@@ -80,8 +78,6 @@ static int mmc_io_rw_direct_host(struct mmc_host *host, int write, unsigned fn,
80 if (addr & ~0x1FFFF) 78 if (addr & ~0x1FFFF)
81 return -EINVAL; 79 return -EINVAL;
82 80
83 memset(&cmd, 0, sizeof(struct mmc_command));
84
85 cmd.opcode = SD_IO_RW_DIRECT; 81 cmd.opcode = SD_IO_RW_DIRECT;
86 cmd.arg = write ? 0x80000000 : 0x00000000; 82 cmd.arg = write ? 0x80000000 : 0x00000000;
87 cmd.arg |= fn << 28; 83 cmd.arg |= fn << 28;
@@ -125,9 +121,9 @@ int mmc_io_rw_direct(struct mmc_card *card, int write, unsigned fn,
125int mmc_io_rw_extended(struct mmc_card *card, int write, unsigned fn, 121int mmc_io_rw_extended(struct mmc_card *card, int write, unsigned fn,
126 unsigned addr, int incr_addr, u8 *buf, unsigned blocks, unsigned blksz) 122 unsigned addr, int incr_addr, u8 *buf, unsigned blocks, unsigned blksz)
127{ 123{
128 struct mmc_request mrq; 124 struct mmc_request mrq = {0};
129 struct mmc_command cmd; 125 struct mmc_command cmd = {0};
130 struct mmc_data data; 126 struct mmc_data data = {0};
131 struct scatterlist sg; 127 struct scatterlist sg;
132 128
133 BUG_ON(!card); 129 BUG_ON(!card);
@@ -140,10 +136,6 @@ int mmc_io_rw_extended(struct mmc_card *card, int write, unsigned fn,
140 if (addr & ~0x1FFFF) 136 if (addr & ~0x1FFFF)
141 return -EINVAL; 137 return -EINVAL;
142 138
143 memset(&mrq, 0, sizeof(struct mmc_request));
144 memset(&cmd, 0, sizeof(struct mmc_command));
145 memset(&data, 0, sizeof(struct mmc_data));
146
147 mrq.cmd = &cmd; 139 mrq.cmd = &cmd;
148 mrq.data = &data; 140 mrq.data = &data;
149 141