1 --- a/drivers/mmc/card/block.c
2 +++ b/drivers/mmc/card/block.c
3 @@ -130,7 +130,7 @@ mmc_blk_getgeo(struct block_device *bdev
7 -static struct block_device_operations mmc_bdops = {
8 +static const struct block_device_operations mmc_bdops = {
10 .release = mmc_blk_release,
11 .getgeo = mmc_blk_getgeo,
12 @@ -392,13 +392,9 @@ static int mmc_blk_issue_rq(struct mmc_q
13 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
14 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
17 if (cmd.resp[0] & ~0x00000900)
18 printk(KERN_ERR "%s: status = %08x\n",
19 req->rq_disk->disk_name, cmd.resp[0]);
20 - if (mmc_decode_status(cmd.resp))
25 if (brq.cmd.error || brq.stop.error || brq.data.error) {
26 --- a/drivers/mmc/core/core.c
27 +++ b/drivers/mmc/core/core.c
35 static struct workqueue_struct *workqueue;
38 @@ -90,17 +93,30 @@ void mmc_request_done(struct mmc_host *h
40 host->ops->request(host, mrq);
42 +#if defined(CONFIG_LEDS_CLASS) || defined(CONFIG_LEDS_CLASS_MODULE)
43 led_trigger_event(host->led, LED_OFF);
46 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
47 mmc_hostname(host), cmd->opcode, err,
48 cmd->resp[0], cmd->resp[1],
49 cmd->resp[2], cmd->resp[3]);
51 + printk("[MMC_DEBUG] %s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
52 + mmc_hostname(host), cmd->opcode, err,
53 + cmd->resp[0], cmd->resp[1],
54 + cmd->resp[2], cmd->resp[3]);
58 pr_debug("%s: %d bytes transferred: %d\n",
60 mrq->data->bytes_xfered, mrq->data->error);
62 + printk("[MMC_DEBUG] %s: %d bytes transferred: %d\n",
64 + mrq->data->bytes_xfered, mrq->data->error);
69 @@ -109,6 +125,13 @@ void mmc_request_done(struct mmc_host *h
71 mrq->stop->resp[0], mrq->stop->resp[1],
72 mrq->stop->resp[2], mrq->stop->resp[3]);
74 + printk("[MMC_DEBUG] %s: (CMD%u): %d: %08x %08x %08x %08x\n",
75 + mmc_hostname(host), mrq->stop->opcode,
77 + mrq->stop->resp[0], mrq->stop->resp[1],
78 + mrq->stop->resp[2], mrq->stop->resp[3]);
83 @@ -129,6 +152,11 @@ mmc_start_request(struct mmc_host *host,
84 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
85 mmc_hostname(host), mrq->cmd->opcode,
86 mrq->cmd->arg, mrq->cmd->flags);
88 + printk("[MMC_DEBUG] %s: starting CMD%u arg %08x flags %08x\n",
89 + mmc_hostname(host), mrq->cmd->opcode,
90 + mrq->cmd->arg, mrq->cmd->flags);
94 pr_debug("%s: blksz %d blocks %d flags %08x "
95 @@ -137,17 +165,32 @@ mmc_start_request(struct mmc_host *host,
96 mrq->data->blocks, mrq->data->flags,
97 mrq->data->timeout_ns / 1000000,
98 mrq->data->timeout_clks);
100 + printk("[MMC_DEBUG] %s: blksz %d blocks %d flags %08x "
101 + "tsac %d ms nsac %d\n",
102 + mmc_hostname(host), mrq->data->blksz,
103 + mrq->data->blocks, mrq->data->flags,
104 + mrq->data->timeout_ns / 1000000,
105 + mrq->data->timeout_clks);
110 pr_debug("%s: CMD%u arg %08x flags %08x\n",
111 mmc_hostname(host), mrq->stop->opcode,
112 mrq->stop->arg, mrq->stop->flags);
114 + printk("[MMC_DEBUG] %s: CMD%u arg %08x flags %08x\n",
115 + mmc_hostname(host), mrq->stop->opcode,
116 + mrq->stop->arg, mrq->stop->flags);
120 WARN_ON(!host->claimed);
122 +#if defined(CONFIG_LEDS_CLASS) || defined(CONFIG_LEDS_CLASS_MODULE)
123 led_trigger_event(host->led, LED_FULL);
128 @@ -286,9 +329,9 @@ void mmc_set_data_timeout(struct mmc_dat
129 * The limit is really 250 ms, but that is
130 * insufficient for some crappy cards.
139 * SDHC cards always use these fixed values.
140 @@ -344,6 +387,101 @@ unsigned int mmc_align_data_size(struct
141 EXPORT_SYMBOL(mmc_align_data_size);
144 + * mmc_host_enable - enable a host.
145 + * @host: mmc host to enable
147 + * Hosts that support power saving can use the 'enable' and 'disable'
148 + * methods to exit and enter power saving states. For more information
149 + * see comments for struct mmc_host_ops.
151 +int mmc_host_enable(struct mmc_host *host)
153 + if (!(host->caps & MMC_CAP_DISABLE))
156 + if (host->en_dis_recurs)
159 + if (host->nesting_cnt++)
162 + cancel_delayed_work_sync(&host->disable);
167 + if (host->ops->enable) {
170 + host->en_dis_recurs = 1;
171 + err = host->ops->enable(host);
172 + host->en_dis_recurs = 0;
175 + pr_debug("%s: enable error %d\n",
176 + mmc_hostname(host), err);
183 +EXPORT_SYMBOL(mmc_host_enable);
185 +static int mmc_host_do_disable(struct mmc_host *host, int lazy)
187 + if (host->ops->disable) {
190 + host->en_dis_recurs = 1;
191 + err = host->ops->disable(host, lazy);
192 + host->en_dis_recurs = 0;
195 + pr_debug("%s: disable error %d\n",
196 + mmc_hostname(host), err);
200 + unsigned long delay = msecs_to_jiffies(err);
202 + mmc_schedule_delayed_work(&host->disable, delay);
210 + * mmc_host_disable - disable a host.
211 + * @host: mmc host to disable
213 + * Hosts that support power saving can use the 'enable' and 'disable'
214 + * methods to exit and enter power saving states. For more information
215 + * see comments for struct mmc_host_ops.
217 +int mmc_host_disable(struct mmc_host *host)
221 + if (!(host->caps & MMC_CAP_DISABLE))
224 + if (host->en_dis_recurs)
227 + if (--host->nesting_cnt)
230 + if (!host->enabled)
233 + err = mmc_host_do_disable(host, 0);
236 +EXPORT_SYMBOL(mmc_host_disable);
239 * __mmc_claim_host - exclusively claim a host
240 * @host: mmc host to claim
241 * @abort: whether or not the operation should be aborted
242 @@ -366,25 +504,111 @@ int __mmc_claim_host(struct mmc_host *ho
244 set_current_state(TASK_UNINTERRUPTIBLE);
245 stop = abort ? atomic_read(abort) : 0;
246 - if (stop || !host->claimed)
247 + if (stop || !host->claimed || host->claimer == current)
249 spin_unlock_irqrestore(&host->lock, flags);
251 spin_lock_irqsave(&host->lock, flags);
253 set_current_state(TASK_RUNNING);
258 + host->claimer = current;
259 + host->claim_cnt += 1;
262 spin_unlock_irqrestore(&host->lock, flags);
263 remove_wait_queue(&host->wq, &wait);
265 + mmc_host_enable(host);
269 EXPORT_SYMBOL(__mmc_claim_host);
272 + * mmc_try_claim_host - try exclusively to claim a host
273 + * @host: mmc host to claim
275 + * Returns %1 if the host is claimed, %0 otherwise.
277 +int mmc_try_claim_host(struct mmc_host *host)
279 + int claimed_host = 0;
280 + unsigned long flags;
282 + spin_lock_irqsave(&host->lock, flags);
283 + if (!host->claimed || host->claimer == current) {
285 + host->claimer = current;
286 + host->claim_cnt += 1;
289 + spin_unlock_irqrestore(&host->lock, flags);
290 + return claimed_host;
292 +EXPORT_SYMBOL(mmc_try_claim_host);
294 +static void mmc_do_release_host(struct mmc_host *host)
296 + unsigned long flags;
298 + spin_lock_irqsave(&host->lock, flags);
299 + if (--host->claim_cnt) {
300 + /* Release for nested claim */
301 + spin_unlock_irqrestore(&host->lock, flags);
304 + host->claimer = NULL;
305 + spin_unlock_irqrestore(&host->lock, flags);
306 + wake_up(&host->wq);
310 +void mmc_host_deeper_disable(struct work_struct *work)
312 + struct mmc_host *host =
313 + container_of(work, struct mmc_host, disable.work);
315 + /* If the host is claimed then we do not want to disable it anymore */
316 + if (!mmc_try_claim_host(host))
318 + mmc_host_do_disable(host, 1);
319 + mmc_do_release_host(host);
323 + * mmc_host_lazy_disable - lazily disable a host.
324 + * @host: mmc host to disable
326 + * Hosts that support power saving can use the 'enable' and 'disable'
327 + * methods to exit and enter power saving states. For more information
328 + * see comments for struct mmc_host_ops.
330 +int mmc_host_lazy_disable(struct mmc_host *host)
332 + if (!(host->caps & MMC_CAP_DISABLE))
335 + if (host->en_dis_recurs)
338 + if (--host->nesting_cnt)
341 + if (!host->enabled)
344 + if (host->disable_delay) {
345 + mmc_schedule_delayed_work(&host->disable,
346 + msecs_to_jiffies(host->disable_delay));
349 + return mmc_host_do_disable(host, 1);
351 +EXPORT_SYMBOL(mmc_host_lazy_disable);
354 * mmc_release_host - release a host
355 * @host: mmc host to release
357 @@ -393,15 +617,11 @@ EXPORT_SYMBOL(__mmc_claim_host);
359 void mmc_release_host(struct mmc_host *host)
361 - unsigned long flags;
363 WARN_ON(!host->claimed);
365 - spin_lock_irqsave(&host->lock, flags);
367 - spin_unlock_irqrestore(&host->lock, flags);
368 + mmc_host_lazy_disable(host);
370 - wake_up(&host->wq);
371 + mmc_do_release_host(host);
374 EXPORT_SYMBOL(mmc_release_host);
375 @@ -687,7 +907,13 @@ void mmc_set_timing(struct mmc_host *hos
377 static void mmc_power_up(struct mmc_host *host)
379 - int bit = fls(host->ocr_avail) - 1;
382 + /* If ocr is set, we use it */
384 + bit = ffs(host->ocr) - 1;
386 + bit = fls(host->ocr_avail) - 1;
389 if (mmc_host_is_spi(host)) {
390 @@ -947,6 +1173,8 @@ void mmc_stop_host(struct mmc_host *host
391 spin_unlock_irqrestore(&host->lock, flags);
394 + if (host->caps & MMC_CAP_DISABLE)
395 + cancel_delayed_work(&host->disable);
396 cancel_delayed_work(&host->detect);
397 mmc_flush_scheduled_work();
399 @@ -958,6 +1186,8 @@ void mmc_stop_host(struct mmc_host *host
400 mmc_claim_host(host);
401 mmc_detach_bus(host);
402 mmc_release_host(host);
408 @@ -966,6 +1196,80 @@ void mmc_stop_host(struct mmc_host *host
412 +void mmc_power_save_host(struct mmc_host *host)
416 + if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
421 + if (host->bus_ops->power_save)
422 + host->bus_ops->power_save(host);
426 + mmc_power_off(host);
428 +EXPORT_SYMBOL(mmc_power_save_host);
430 +void mmc_power_restore_host(struct mmc_host *host)
434 + if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
439 + mmc_power_up(host);
440 + host->bus_ops->power_restore(host);
444 +EXPORT_SYMBOL(mmc_power_restore_host);
446 +int mmc_card_awake(struct mmc_host *host)
452 + if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
453 + err = host->bus_ops->awake(host);
459 +EXPORT_SYMBOL(mmc_card_awake);
461 +int mmc_card_sleep(struct mmc_host *host)
467 + if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
468 + err = host->bus_ops->sleep(host);
474 +EXPORT_SYMBOL(mmc_card_sleep);
476 +int mmc_card_can_sleep(struct mmc_host *host)
478 + struct mmc_card *card = host->card;
480 + if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
484 +EXPORT_SYMBOL(mmc_card_can_sleep);
489 @@ -975,27 +1279,36 @@ void mmc_stop_host(struct mmc_host *host
491 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
495 + if (host->caps & MMC_CAP_DISABLE)
496 + cancel_delayed_work(&host->disable);
497 cancel_delayed_work(&host->detect);
498 mmc_flush_scheduled_work();
501 if (host->bus_ops && !host->bus_dead) {
502 if (host->bus_ops->suspend)
503 - host->bus_ops->suspend(host);
504 - if (!host->bus_ops->resume) {
505 + err = host->bus_ops->suspend(host);
506 + if (err == -ENOSYS || !host->bus_ops->resume) {
508 + * We simply "remove" the card in this case.
509 + * It will be redetected on resume.
511 if (host->bus_ops->remove)
512 host->bus_ops->remove(host);
514 mmc_claim_host(host);
515 mmc_detach_bus(host);
516 mmc_release_host(host);
522 - mmc_power_off(host);
524 + mmc_power_off(host);
530 EXPORT_SYMBOL(mmc_suspend_host);
531 @@ -1006,12 +1319,26 @@ EXPORT_SYMBOL(mmc_suspend_host);
533 int mmc_resume_host(struct mmc_host *host)
538 if (host->bus_ops && !host->bus_dead) {
540 mmc_select_voltage(host, host->ocr);
541 BUG_ON(!host->bus_ops->resume);
542 - host->bus_ops->resume(host);
543 + err = host->bus_ops->resume(host);
545 + printk(KERN_WARNING "%s: error %d during resume "
546 + "(card was removed?)\n",
547 + mmc_hostname(host), err);
548 + if (host->bus_ops->remove)
549 + host->bus_ops->remove(host);
550 + mmc_claim_host(host);
551 + mmc_detach_bus(host);
552 + mmc_release_host(host);
553 + /* no need to bother upper layers */
559 @@ -1021,7 +1348,7 @@ int mmc_resume_host(struct mmc_host *hos
561 mmc_detect_change(host, 1);
567 EXPORT_SYMBOL(mmc_resume_host);
568 --- a/drivers/mmc/core/core.h
569 +++ b/drivers/mmc/core/core.h
571 #define MMC_CMD_RETRIES 3
574 + int (*awake)(struct mmc_host *);
575 + int (*sleep)(struct mmc_host *);
576 void (*remove)(struct mmc_host *);
577 void (*detect)(struct mmc_host *);
578 - void (*suspend)(struct mmc_host *);
579 - void (*resume)(struct mmc_host *);
580 + int (*suspend)(struct mmc_host *);
581 + int (*resume)(struct mmc_host *);
582 + void (*power_save)(struct mmc_host *);
583 + void (*power_restore)(struct mmc_host *);
586 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops);
587 --- a/drivers/mmc/core/debugfs.c
588 +++ b/drivers/mmc/core/debugfs.c
589 @@ -240,7 +240,7 @@ static int mmc_ext_csd_release(struct in
593 -static struct file_operations mmc_dbg_ext_csd_fops = {
594 +static const struct file_operations mmc_dbg_ext_csd_fops = {
595 .open = mmc_ext_csd_open,
596 .read = mmc_ext_csd_read,
597 .release = mmc_ext_csd_release,
598 --- a/drivers/mmc/core/host.c
599 +++ b/drivers/mmc/core/host.c
600 @@ -83,6 +83,7 @@ struct mmc_host *mmc_alloc_host(int extr
601 spin_lock_init(&host->lock);
602 init_waitqueue_head(&host->wq);
603 INIT_DELAYED_WORK(&host->detect, mmc_rescan);
604 + INIT_DELAYED_WORK_DEFERRABLE(&host->disable, mmc_host_deeper_disable);
607 * By default, hosts do not support SGIO or large requests.
608 --- a/drivers/mmc/core/host.h
609 +++ b/drivers/mmc/core/host.h
611 int mmc_register_host_class(void);
612 void mmc_unregister_host_class(void);
614 +void mmc_host_deeper_disable(struct work_struct *work);
618 --- a/drivers/mmc/core/mmc.c
619 +++ b/drivers/mmc/core/mmc.c
620 @@ -160,7 +160,6 @@ static int mmc_read_ext_csd(struct mmc_c
624 - unsigned int ext_csd_struct;
628 @@ -207,16 +206,16 @@ static int mmc_read_ext_csd(struct mmc_c
632 - ext_csd_struct = ext_csd[EXT_CSD_REV];
633 - if (ext_csd_struct > 3) {
634 + card->ext_csd.rev = ext_csd[EXT_CSD_REV];
635 + if (card->ext_csd.rev > 3) {
636 printk(KERN_ERR "%s: unrecognised EXT_CSD structure "
637 "version %d\n", mmc_hostname(card->host),
639 + card->ext_csd.rev);
644 - if (ext_csd_struct >= 2) {
645 + if (card->ext_csd.rev >= 2) {
646 card->ext_csd.sectors =
647 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
648 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
649 @@ -241,6 +240,15 @@ static int mmc_read_ext_csd(struct mmc_c
653 + if (card->ext_csd.rev >= 3) {
654 + u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
656 + /* Sleep / awake timeout in 100ns units */
657 + if (sa_shift > 0 && sa_shift <= 0x17)
658 + card->ext_csd.sa_timeout =
659 + 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
665 @@ -276,7 +284,7 @@ static struct attribute_group mmc_std_at
666 .attrs = mmc_std_attrs,
669 -static struct attribute_group *mmc_attr_groups[] = {
670 +static const struct attribute_group *mmc_attr_groups[] = {
674 @@ -408,12 +416,17 @@ static int mmc_init_card(struct mmc_host
675 (host->caps & MMC_CAP_MMC_HIGHSPEED)) {
676 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
677 EXT_CSD_HS_TIMING, 1);
679 + if (err && err != -EBADMSG)
682 - mmc_card_set_highspeed(card);
684 - mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
686 + printk(KERN_WARNING "%s: switch to highspeed failed\n",
687 + mmc_hostname(card->host));
690 + mmc_card_set_highspeed(card);
691 + mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
696 @@ -448,10 +461,17 @@ static int mmc_init_card(struct mmc_host
697 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
698 EXT_CSD_BUS_WIDTH, ext_csd_bit);
701 + if (err && err != -EBADMSG)
704 - mmc_set_bus_width(card->host, bus_width);
706 + printk(KERN_WARNING "%s: switch to bus width %d "
707 + "failed\n", mmc_hostname(card->host),
711 + mmc_set_bus_width(card->host, bus_width);
716 @@ -507,12 +527,10 @@ static void mmc_detect(struct mmc_host *
720 -#ifdef CONFIG_MMC_UNSAFE_RESUME
723 * Suspend callback from host.
725 -static void mmc_suspend(struct mmc_host *host)
726 +static int mmc_suspend(struct mmc_host *host)
730 @@ -522,6 +540,8 @@ static void mmc_suspend(struct mmc_host
731 mmc_deselect_cards(host);
732 host->card->state &= ~MMC_STATE_HIGHSPEED;
733 mmc_release_host(host);
739 @@ -530,7 +550,7 @@ static void mmc_suspend(struct mmc_host
740 * This function tries to determine if the same card is still present
741 * and, if so, restore all state to it.
743 -static void mmc_resume(struct mmc_host *host)
744 +static int mmc_resume(struct mmc_host *host)
748 @@ -541,30 +561,99 @@ static void mmc_resume(struct mmc_host *
749 err = mmc_init_card(host, host->ocr, host->card);
750 mmc_release_host(host);
757 - mmc_claim_host(host);
758 - mmc_detach_bus(host);
759 - mmc_release_host(host);
760 +static void mmc_power_restore(struct mmc_host *host)
762 + host->card->state &= ~MMC_STATE_HIGHSPEED;
763 + mmc_claim_host(host);
764 + mmc_init_card(host, host->ocr, host->card);
765 + mmc_release_host(host);
768 +static int mmc_sleep(struct mmc_host *host)
770 + struct mmc_card *card = host->card;
773 + if (card && card->ext_csd.rev >= 3) {
774 + err = mmc_card_sleepawake(host, 1);
776 + pr_debug("%s: Error %d while putting card into sleep",
777 + mmc_hostname(host), err);
784 +static int mmc_awake(struct mmc_host *host)
786 + struct mmc_card *card = host->card;
789 -#define mmc_suspend NULL
790 -#define mmc_resume NULL
791 + if (card && card->ext_csd.rev >= 3) {
792 + err = mmc_card_sleepawake(host, 0);
794 + pr_debug("%s: Error %d while awaking sleeping card",
795 + mmc_hostname(host), err);
802 +#ifdef CONFIG_MMC_UNSAFE_RESUME
804 +static const struct mmc_bus_ops mmc_ops = {
805 + .awake = mmc_awake,
806 + .sleep = mmc_sleep,
807 + .remove = mmc_remove,
808 + .detect = mmc_detect,
809 + .suspend = mmc_suspend,
810 + .resume = mmc_resume,
811 + .power_restore = mmc_power_restore,
814 +static void mmc_attach_bus_ops(struct mmc_host *host)
816 + mmc_attach_bus(host, &mmc_ops);
821 static const struct mmc_bus_ops mmc_ops = {
822 + .awake = mmc_awake,
823 + .sleep = mmc_sleep,
824 + .remove = mmc_remove,
825 + .detect = mmc_detect,
828 + .power_restore = mmc_power_restore,
831 +static const struct mmc_bus_ops mmc_ops_unsafe = {
832 + .awake = mmc_awake,
833 + .sleep = mmc_sleep,
834 .remove = mmc_remove,
835 .detect = mmc_detect,
836 .suspend = mmc_suspend,
837 .resume = mmc_resume,
838 + .power_restore = mmc_power_restore,
841 +static void mmc_attach_bus_ops(struct mmc_host *host)
843 + const struct mmc_bus_ops *bus_ops;
845 + if (host->caps & MMC_CAP_NONREMOVABLE)
846 + bus_ops = &mmc_ops_unsafe;
848 + bus_ops = &mmc_ops;
849 + mmc_attach_bus(host, bus_ops);
855 * Starting point for MMC card init.
857 @@ -575,7 +664,7 @@ int mmc_attach_mmc(struct mmc_host *host
859 WARN_ON(!host->claimed);
861 - mmc_attach_bus(host, &mmc_ops);
862 + mmc_attach_bus_ops(host);
865 * We need to get OCR a different way for SPI.
866 --- a/drivers/mmc/core/mmc_ops.c
867 +++ b/drivers/mmc/core/mmc_ops.c
868 @@ -57,6 +57,42 @@ int mmc_deselect_cards(struct mmc_host *
869 return _mmc_select_card(host, NULL);
872 +int mmc_card_sleepawake(struct mmc_host *host, int sleep)
874 + struct mmc_command cmd;
875 + struct mmc_card *card = host->card;
879 + mmc_deselect_cards(host);
881 + memset(&cmd, 0, sizeof(struct mmc_command));
883 + cmd.opcode = MMC_SLEEP_AWAKE;
884 + cmd.arg = card->rca << 16;
886 + cmd.arg |= 1 << 15;
888 + cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
889 + err = mmc_wait_for_cmd(host, &cmd, 0);
894 + * If the host does not wait while the card signals busy, then we will
895 + * will have to wait the sleep/awake timeout. Note, we cannot use the
896 + * SEND_STATUS command to poll the status because that command (and most
897 + * others) is invalid while the card sleeps.
899 + if (!(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
900 + mmc_delay(DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000));
903 + err = mmc_select_card(card);
908 int mmc_go_idle(struct mmc_host *host)
911 @@ -354,6 +390,7 @@ int mmc_switch(struct mmc_card *card, u8
914 struct mmc_command cmd;
919 @@ -371,6 +408,28 @@ int mmc_switch(struct mmc_card *card, u8
923 + /* Must check status to be sure of no errors */
925 + err = mmc_send_status(card, &status);
928 + if (card->host->caps & MMC_CAP_WAIT_WHILE_BUSY)
930 + if (mmc_host_is_spi(card->host))
932 + } while (R1_CURRENT_STATE(status) == 7);
934 + if (mmc_host_is_spi(card->host)) {
935 + if (status & R1_SPI_ILLEGAL_COMMAND)
938 + if (status & 0xFDFFA000)
939 + printk(KERN_WARNING "%s: unexpected status %#x after "
940 + "switch", mmc_hostname(card->host), status);
941 + if (status & R1_SWITCH_ERROR)
948 --- a/drivers/mmc/core/mmc_ops.h
949 +++ b/drivers/mmc/core/mmc_ops.h
950 @@ -25,6 +25,7 @@ int mmc_send_status(struct mmc_card *car
951 int mmc_send_cid(struct mmc_host *host, u32 *cid);
952 int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp);
953 int mmc_spi_set_crc(struct mmc_host *host, int use_crc);
954 +int mmc_card_sleepawake(struct mmc_host *host, int sleep);
958 --- a/drivers/mmc/core/sd.c
959 +++ b/drivers/mmc/core/sd.c
960 @@ -314,7 +314,7 @@ static struct attribute_group sd_std_att
961 .attrs = sd_std_attrs,
964 -static struct attribute_group *sd_attr_groups[] = {
965 +static const struct attribute_group *sd_attr_groups[] = {
969 @@ -561,12 +561,10 @@ static void mmc_sd_detect(struct mmc_hos
973 -#ifdef CONFIG_MMC_UNSAFE_RESUME
976 * Suspend callback from host.
978 -static void mmc_sd_suspend(struct mmc_host *host)
979 +static int mmc_sd_suspend(struct mmc_host *host)
983 @@ -576,6 +574,8 @@ static void mmc_sd_suspend(struct mmc_ho
984 mmc_deselect_cards(host);
985 host->card->state &= ~MMC_STATE_HIGHSPEED;
986 mmc_release_host(host);
992 @@ -584,7 +584,7 @@ static void mmc_sd_suspend(struct mmc_ho
993 * This function tries to determine if the same card is still present
994 * and, if so, restore all state to it.
996 -static void mmc_sd_resume(struct mmc_host *host)
997 +static int mmc_sd_resume(struct mmc_host *host)
1001 @@ -595,30 +595,63 @@ static void mmc_sd_resume(struct mmc_hos
1002 err = mmc_sd_init_card(host, host->ocr, host->card);
1003 mmc_release_host(host);
1006 - mmc_sd_remove(host);
1008 - mmc_claim_host(host);
1009 - mmc_detach_bus(host);
1010 - mmc_release_host(host);
1015 +static void mmc_sd_power_restore(struct mmc_host *host)
1017 + host->card->state &= ~MMC_STATE_HIGHSPEED;
1018 + mmc_claim_host(host);
1019 + mmc_sd_init_card(host, host->ocr, host->card);
1020 + mmc_release_host(host);
1024 +#ifdef CONFIG_MMC_UNSAFE_RESUME
1026 -#define mmc_sd_suspend NULL
1027 -#define mmc_sd_resume NULL
1028 +static const struct mmc_bus_ops mmc_sd_ops = {
1029 + .remove = mmc_sd_remove,
1030 + .detect = mmc_sd_detect,
1031 + .suspend = mmc_sd_suspend,
1032 + .resume = mmc_sd_resume,
1033 + .power_restore = mmc_sd_power_restore,
1037 +static void mmc_sd_attach_bus_ops(struct mmc_host *host)
1039 + mmc_attach_bus(host, &mmc_sd_ops);
1044 static const struct mmc_bus_ops mmc_sd_ops = {
1045 .remove = mmc_sd_remove,
1046 .detect = mmc_sd_detect,
1049 + .power_restore = mmc_sd_power_restore,
1052 +static const struct mmc_bus_ops mmc_sd_ops_unsafe = {
1053 + .remove = mmc_sd_remove,
1054 + .detect = mmc_sd_detect,
1055 .suspend = mmc_sd_suspend,
1056 .resume = mmc_sd_resume,
1057 + .power_restore = mmc_sd_power_restore,
1060 +static void mmc_sd_attach_bus_ops(struct mmc_host *host)
1062 + const struct mmc_bus_ops *bus_ops;
1064 + if (host->caps & MMC_CAP_NONREMOVABLE)
1065 + bus_ops = &mmc_sd_ops_unsafe;
1067 + bus_ops = &mmc_sd_ops;
1068 + mmc_attach_bus(host, bus_ops);
1074 * Starting point for SD card init.
1076 @@ -629,7 +662,7 @@ int mmc_attach_sd(struct mmc_host *host,
1078 WARN_ON(!host->claimed);
1080 - mmc_attach_bus(host, &mmc_sd_ops);
1081 + mmc_sd_attach_bus_ops(host);
1084 * We need to get OCR a different way for SPI.
1085 --- a/drivers/mmc/core/sdio_bus.c
1086 +++ b/drivers/mmc/core/sdio_bus.c
1088 #include "sdio_cis.h"
1089 #include "sdio_bus.h"
1091 -#define dev_to_sdio_func(d) container_of(d, struct sdio_func, dev)
1092 -#define to_sdio_driver(d) container_of(d, struct sdio_driver, drv)
1094 /* show configuration fields */
1095 #define sdio_config_attr(field, format_string) \
1097 @@ -251,12 +248,15 @@ int sdio_add_func(struct sdio_func *func
1099 * Unregister a SDIO function with the driver model, and
1100 * (eventually) free it.
1101 + * This function can be called through error paths where sdio_add_func() was
1102 + * never executed (because a failure occurred at an earlier point).
1104 void sdio_remove_func(struct sdio_func *func)
1106 - if (sdio_func_present(func))
1107 - device_del(&func->dev);
1108 + if (!sdio_func_present(func))
1111 + device_del(&func->dev);
1112 put_device(&func->dev);
1115 --- a/drivers/mmc/core/sdio.c
1116 +++ b/drivers/mmc/core/sdio.c
1117 @@ -165,6 +165,29 @@ static int sdio_enable_wide(struct mmc_c
1121 + * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
1122 + * of the card. This may be required on certain setups of boards,
1123 + * controllers and embedded sdio device which do not need the card's
1124 + * pull-up. As a result, card detection is disabled and power is saved.
1126 +static int sdio_disable_cd(struct mmc_card *card)
1131 + if (!card->cccr.disable_cd)
1134 + ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
1138 + ctrl |= SDIO_BUS_CD_DISABLE;
1140 + return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
1144 * Test if the card supports high-speed mode and, if so, switch to it.
1146 static int sdio_enable_hs(struct mmc_card *card)
1147 @@ -195,6 +218,135 @@ static int sdio_enable_hs(struct mmc_car
1151 + * Handle the detection and initialisation of a card.
1153 + * In the case of a resume, "oldcard" will contain the card
1154 + * we're trying to reinitialise.
1156 +static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
1157 + struct mmc_card *oldcard)
1159 + struct mmc_card *card;
1163 + WARN_ON(!host->claimed);
1166 + * Inform the card of the voltage
1168 + err = mmc_send_io_op_cond(host, host->ocr, &ocr);
1173 + * For SPI, enable CRC as appropriate.
1175 + if (mmc_host_is_spi(host)) {
1176 + err = mmc_spi_set_crc(host, use_spi_crc);
1182 + * Allocate card structure.
1184 + card = mmc_alloc_card(host, NULL);
1185 + if (IS_ERR(card)) {
1186 + err = PTR_ERR(card);
1190 + card->type = MMC_TYPE_SDIO;
1193 + * For native busses: set card RCA and quit open drain mode.
1195 + if (!mmc_host_is_spi(host)) {
1196 + err = mmc_send_relative_addr(host, &card->rca);
1200 + mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1204 + * Select card, as all following commands rely on that.
1206 + if (!mmc_host_is_spi(host)) {
1207 + err = mmc_select_card(card);
1213 + * Read the common registers.
1215 + err = sdio_read_cccr(card);
1220 + * Read the common CIS tuples.
1222 + err = sdio_read_common_cis(card);
1227 + int same = (card->cis.vendor == oldcard->cis.vendor &&
1228 + card->cis.device == oldcard->cis.device);
1229 + mmc_remove_card(card);
1239 + * Switch to high-speed (if supported).
1241 + err = sdio_enable_hs(card);
1246 + * Change to the card's maximum speed.
1248 + if (mmc_card_highspeed(card)) {
1250 + * The SDIO specification doesn't mention how
1251 + * the CIS transfer speed register relates to
1252 + * high-speed, but it seems that 50 MHz is
1255 + mmc_set_clock(host, 50000000);
1257 + mmc_set_clock(host, card->cis.max_dtr);
1261 + * Switch to wider bus (if supported).
1263 + err = sdio_enable_wide(card);
1268 + host->card = card;
1273 + mmc_remove_card(card);
1280 * Host is being removed. Free up the current card.
1282 static void mmc_sdio_remove(struct mmc_host *host)
1283 @@ -243,10 +395,77 @@ static void mmc_sdio_detect(struct mmc_h
1288 + * SDIO suspend. We need to suspend all functions separately.
1289 + * Therefore all registered functions must have drivers with suspend
1290 + * and resume methods. Failing that we simply remove the whole card.
1292 +static int mmc_sdio_suspend(struct mmc_host *host)
1296 + for (i = 0; i < host->card->sdio_funcs; i++) {
1297 + struct sdio_func *func = host->card->sdio_func[i];
1298 + if (func && sdio_func_present(func) && func->dev.driver) {
1299 + const struct dev_pm_ops *pmops = func->dev.driver->pm;
1300 + if (!pmops || !pmops->suspend || !pmops->resume) {
1301 + /* force removal of entire card in that case */
1304 + err = pmops->suspend(&func->dev);
1309 + while (err && --i >= 0) {
1310 + struct sdio_func *func = host->card->sdio_func[i];
1311 + if (func && sdio_func_present(func) && func->dev.driver) {
1312 + const struct dev_pm_ops *pmops = func->dev.driver->pm;
1313 + pmops->resume(&func->dev);
1320 +static int mmc_sdio_resume(struct mmc_host *host)
1325 + BUG_ON(!host->card);
1327 + /* Basic card reinitialization. */
1328 + mmc_claim_host(host);
1329 + err = mmc_sdio_init_card(host, host->ocr, host->card);
1330 + mmc_release_host(host);
1333 + * If the card looked to be the same as before suspending, then
1334 + * we proceed to resume all card functions. If one of them returns
1335 + * an error then we simply return that error to the core and the
1336 + * card will be redetected as new. It is the responsibility of
1337 + * the function driver to perform further tests with the extra
1338 + * knowledge it has of the card to confirm the card is indeed the
1339 + * same as before suspending (same MAC address for network cards,
1340 + * etc.) and return an error otherwise.
1342 + for (i = 0; !err && i < host->card->sdio_funcs; i++) {
1343 + struct sdio_func *func = host->card->sdio_func[i];
1344 + if (func && sdio_func_present(func) && func->dev.driver) {
1345 + const struct dev_pm_ops *pmops = func->dev.driver->pm;
1346 + err = pmops->resume(&func->dev);
1353 static const struct mmc_bus_ops mmc_sdio_ops = {
1354 .remove = mmc_sdio_remove,
1355 .detect = mmc_sdio_detect,
1356 + .suspend = mmc_sdio_suspend,
1357 + .resume = mmc_sdio_resume,
1361 @@ -275,13 +494,6 @@ int mmc_attach_sdio(struct mmc_host *hos
1365 - if (ocr & MMC_VDD_165_195) {
1366 - printk(KERN_WARNING "%s: SDIO card claims to support the "
1367 - "incompletely defined 'low voltage range'. This "
1368 - "will be ignored.\n", mmc_hostname(host));
1369 - ocr &= ~MMC_VDD_165_195;
1372 host->ocr = mmc_select_voltage(host, ocr);
1375 @@ -293,108 +505,31 @@ int mmc_attach_sdio(struct mmc_host *hos
1379 - * Inform the card of the voltage
1380 + * Detect and init the card.
1382 - err = mmc_send_io_op_cond(host, host->ocr, &ocr);
1383 + err = mmc_sdio_init_card(host, host->ocr, NULL);
1388 - * For SPI, enable CRC as appropriate.
1390 - if (mmc_host_is_spi(host)) {
1391 - err = mmc_spi_set_crc(host, use_spi_crc);
1395 + card = host->card;
1398 * The number of functions on the card is encoded inside
1401 funcs = (ocr & 0x70000000) >> 28;
1402 + card->sdio_funcs = 0;
1405 - * Allocate card structure.
1407 - card = mmc_alloc_card(host, NULL);
1408 - if (IS_ERR(card)) {
1409 - err = PTR_ERR(card);
1413 - card->type = MMC_TYPE_SDIO;
1414 - card->sdio_funcs = funcs;
1416 - host->card = card;
1419 - * For native busses: set card RCA and quit open drain mode.
1420 + * If needed, disconnect card detection pull-up resistor.
1422 - if (!mmc_host_is_spi(host)) {
1423 - err = mmc_send_relative_addr(host, &card->rca);
1427 - mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1431 - * Select card, as all following commands rely on that.
1433 - if (!mmc_host_is_spi(host)) {
1434 - err = mmc_select_card(card);
1440 - * Read the common registers.
1442 - err = sdio_read_cccr(card);
1447 - * Read the common CIS tuples.
1449 - err = sdio_read_common_cis(card);
1454 - * Switch to high-speed (if supported).
1456 - err = sdio_enable_hs(card);
1461 - * Change to the card's maximum speed.
1463 - if (mmc_card_highspeed(card)) {
1465 - * The SDIO specification doesn't mention how
1466 - * the CIS transfer speed register relates to
1467 - * high-speed, but it seems that 50 MHz is
1470 - mmc_set_clock(host, 50000000);
1472 - mmc_set_clock(host, card->cis.max_dtr);
1476 - * Switch to wider bus (if supported).
1478 - err = sdio_enable_wide(card);
1479 + err = sdio_disable_cd(card);
1484 * Initialize (but don't add) all present functions.
1486 - for (i = 0;i < funcs;i++) {
1487 + for (i = 0; i < funcs; i++, card->sdio_funcs++) {
1488 err = sdio_init_func(host->card, i + 1);
1491 --- a/drivers/mmc/core/sdio_cis.c
1492 +++ b/drivers/mmc/core/sdio_cis.c
1493 @@ -29,6 +29,8 @@ static int cistpl_vers_1(struct mmc_card
1494 unsigned i, nr_strings;
1495 char **buffer, *string;
1497 + /* Find all null-terminated (including zero length) strings in
1498 + the TPLLV1_INFO field. Trailing garbage is ignored. */
1502 @@ -39,11 +41,8 @@ static int cistpl_vers_1(struct mmc_card
1507 - if (buf[i-1] != '\0') {
1508 - printk(KERN_WARNING "SDIO: ignoring broken CISTPL_VERS_1\n");
1509 + if (nr_strings == 0)
1515 @@ -98,6 +97,22 @@ static const unsigned char speed_val[16]
1516 static const unsigned int speed_unit[8] =
1517 { 10000, 100000, 1000000, 10000000, 0, 0, 0, 0 };
1519 +/* FUNCE tuples with these types get passed to SDIO drivers */
1520 +static const unsigned char funce_type_whitelist[] = {
1521 + 4 /* CISTPL_FUNCE_LAN_NODE_ID used in Broadcom cards */
1524 +static int cistpl_funce_whitelisted(unsigned char type)
1528 + for (i = 0; i < ARRAY_SIZE(funce_type_whitelist); i++) {
1529 + if (funce_type_whitelist[i] == type)
1535 static int cistpl_funce_common(struct mmc_card *card,
1536 const unsigned char *buf, unsigned size)
1538 @@ -120,6 +135,10 @@ static int cistpl_funce_func(struct sdio
1542 + /* let SDIO drivers take care of whitelisted FUNCE tuples */
1543 + if (cistpl_funce_whitelisted(buf[0]))
1546 vsn = func->card->cccr.sdio_vsn;
1547 min_size = (vsn == SDIO_SDIO_REV_1_00) ? 28 : 42;
1549 @@ -154,13 +173,12 @@ static int cistpl_funce(struct mmc_card
1551 ret = cistpl_funce_common(card, buf, size);
1554 + if (ret && ret != -EILSEQ) {
1555 printk(KERN_ERR "%s: bad CISTPL_FUNCE size %u "
1556 "type %u\n", mmc_hostname(card->host), size, buf[0]);
1564 typedef int (tpl_parse_t)(struct mmc_card *, struct sdio_func *,
1565 @@ -253,21 +271,12 @@ static int sdio_read_cis(struct mmc_card
1566 for (i = 0; i < ARRAY_SIZE(cis_tpl_list); i++)
1567 if (cis_tpl_list[i].code == tpl_code)
1569 - if (i >= ARRAY_SIZE(cis_tpl_list)) {
1570 - /* this tuple is unknown to the core */
1571 - this->next = NULL;
1572 - this->code = tpl_code;
1573 - this->size = tpl_link;
1575 - prev = &this->next;
1577 - "%s: queuing CIS tuple 0x%02x length %u\n",
1578 - mmc_hostname(card->host), tpl_code, tpl_link);
1580 + if (i < ARRAY_SIZE(cis_tpl_list)) {
1581 const struct cis_tpl *tpl = cis_tpl_list + i;
1582 if (tpl_link < tpl->min_size) {
1584 - "%s: bad CIS tuple 0x%02x (length = %u, expected >= %u)\n",
1585 + "%s: bad CIS tuple 0x%02x"
1586 + " (length = %u, expected >= %u)\n",
1587 mmc_hostname(card->host),
1588 tpl_code, tpl_link, tpl->min_size);
1590 @@ -275,7 +284,30 @@ static int sdio_read_cis(struct mmc_card
1591 ret = tpl->parse(card, func,
1592 this->data, tpl_link);
1596 + * We don't need the tuple anymore if it was
1597 + * successfully parsed by the SDIO core or if it is
1598 + * not going to be parsed by SDIO drivers.
1600 + if (!ret || ret != -EILSEQ)
1603 + /* unknown tuple */
1607 + if (ret == -EILSEQ) {
1608 + /* this tuple is unknown to the core or whitelisted */
1609 + this->next = NULL;
1610 + this->code = tpl_code;
1611 + this->size = tpl_link;
1613 + prev = &this->next;
1615 + "%s: queuing CIS tuple 0x%02x length %u\n",
1616 + mmc_hostname(card->host), tpl_code, tpl_link);
1617 + /* keep on analyzing tuples */
1622 --- a/drivers/mmc/core/sdio_io.c
1623 +++ b/drivers/mmc/core/sdio_io.c
1624 @@ -624,7 +624,7 @@ void sdio_f0_writeb(struct sdio_func *fu
1628 - if (addr < 0xF0 || addr > 0xFF) {
1629 + if ((addr < 0xF0 || addr > 0xFF) && (!mmc_card_lenient_fn0(func->card))) {
1633 --- a/drivers/mmc/host/Kconfig
1634 +++ b/drivers/mmc/host/Kconfig
1635 @@ -55,6 +55,17 @@ config MMC_SDHCI_PCI
1639 +config MMC_SDHCI_CNS3XXX
1640 + tristate "SDHCI support on CNS3XXX"
1641 + depends on MMC_SDHCI && ARCH_CNS3XXX
1643 + This selects the Secure Digital Host Controller Interface (SDHCI)
1644 + in Cavium Networks CNS3XXX SOCs.
1646 + If you have a controller with this interface, say Y or M here.
1650 config MMC_RICOH_MMC
1651 tristate "Ricoh MMC Controller Disabler (EXPERIMENTAL)"
1652 depends on MMC_SDHCI_PCI
1653 --- a/drivers/mmc/host/Makefile
1654 +++ b/drivers/mmc/host/Makefile
1655 @@ -12,6 +12,7 @@ obj-$(CONFIG_MMC_IMX) += imxmmc.o
1656 obj-$(CONFIG_MMC_MXC) += mxcmmc.o
1657 obj-$(CONFIG_MMC_SDHCI) += sdhci.o
1658 obj-$(CONFIG_MMC_SDHCI_PCI) += sdhci-pci.o
1659 +obj-$(CONFIG_MMC_SDHCI_CNS3XXX) += sdhci-cns3xxx.o
1660 obj-$(CONFIG_MMC_RICOH_MMC) += ricoh_mmc.o
1661 obj-$(CONFIG_MMC_SDHCI_OF) += sdhci-of.o
1662 obj-$(CONFIG_MMC_SDHCI_PLTFM) += sdhci-pltfm.o
1663 --- a/drivers/mmc/host/sdhci.c
1664 +++ b/drivers/mmc/host/sdhci.c
1667 #define DRIVER_NAME "sdhci"
1669 +#define SDHCI_DEBUG
1673 +#define sd_printk(x...) printk(x)
1675 +#define sd_printk(x...) do { } while(0)
1678 #define DBG(f, x...) \
1679 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
1681 @@ -43,6 +52,39 @@ static void sdhci_finish_data(struct sdh
1682 static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
1683 static void sdhci_finish_command(struct sdhci_host *);
1685 +static void sdhci_dumpallregs(struct sdhci_host *host)
1688 + printk(" _______________________________________________\n");
1690 + printk(" 0x00: 0x%08x | 0x04: 0x%08x\n", sdhci_readl(host, 0x00), sdhci_readl(host, 0x04));
1691 + printk(" 0x08: 0x%08x | 0x0C: 0x%08x\n", sdhci_readl(host, 0x08), sdhci_readl(host, 0x0C));
1692 + printk(" 0x10: 0x%08x | 0x14: 0x%08x\n", sdhci_readl(host, 0x10), sdhci_readl(host, 0x14));
1693 + printk(" 0x18: 0x%08x | 0x1C: 0x%08x\n", sdhci_readl(host, 0x18), sdhci_readl(host, 0x1C));
1694 + printk(" -----------------| 0x24: 0x%08x\n", sdhci_readl(host, 0x24));
1695 + printk(" 0x28: 0x%08x | 0x2C: 0x%08x\n", sdhci_readl(host, 0x28), sdhci_readl(host, 0x2C));
1696 + printk(" 0x30: 0x%08x | 0x34: 0x%08x\n", sdhci_readl(host, 0x30), sdhci_readl(host, 0x34));
1697 + printk(" 0x38: 0x%08x | 0x3C: 0x%08x\n", sdhci_readl(host, 0x38), sdhci_readl(host, 0x3C));
1698 + printk(" 0x40: 0x%08x | 0x44: 0x%08x\n", sdhci_readl(host, 0x40), sdhci_readl(host, 0x44));
1699 + printk(" 0x48: 0x%08x | 0x4C: 0x%08x\n", sdhci_readl(host, 0x48), sdhci_readl(host, 0x4C));
1700 + printk(" 0x50: 0x%08x | 0xFC: 0x%08x\n", sdhci_readl(host, 0x50), sdhci_readl(host, 0xFC));
1702 + printk(KERN_DEBUG " _______________________________________________\n");
1704 + printk(KERN_DEBUG " 0x00: 0x%08x | 0x04: 0x%08x\n", sdhci_readl(host, 0x00), sdhci_readl(host, 0x04));
1705 + printk(KERN_DEBUG " 0x08: 0x%08x | 0x0C: 0x%08x\n", sdhci_readl(host, 0x08), sdhci_readl(host, 0x0C));
1706 + printk(KERN_DEBUG " 0x10: 0x%08x | 0x14: 0x%08x\n", sdhci_readl(host, 0x10), sdhci_readl(host, 0x14));
1707 + printk(KERN_DEBUG " 0x18: 0x%08x | 0x1C: 0x%08x\n", sdhci_readl(host, 0x18), sdhci_readl(host, 0x1C));
1708 + printk(KERN_DEBUG " -----------------| 0x24: 0x%08x\n", sdhci_readl(host, 0x24));
1709 + printk(KERN_DEBUG " 0x28: 0x%08x | 0x2C: 0x%08x\n", sdhci_readl(host, 0x28), sdhci_readl(host, 0x2C));
1710 + printk(KERN_DEBUG " 0x30: 0x%08x | 0x34: 0x%08x\n", sdhci_readl(host, 0x30), sdhci_readl(host, 0x34));
1711 + printk(KERN_DEBUG " 0x38: 0x%08x | 0x3C: 0x%08x\n", sdhci_readl(host, 0x38), sdhci_readl(host, 0x3C));
1712 + printk(KERN_DEBUG " 0x40: 0x%08x | 0x44: 0x%08x\n", sdhci_readl(host, 0x40), sdhci_readl(host, 0x44));
1713 + printk(KERN_DEBUG " 0x48: 0x%08x | 0x4C: 0x%08x\n", sdhci_readl(host, 0x48), sdhci_readl(host, 0x4C));
1714 + printk(KERN_DEBUG " 0x50: 0x%08x | 0xFC: 0x%08x\n", sdhci_readl(host, 0x50), sdhci_readl(host, 0xFC));
1718 static void sdhci_dumpregs(struct sdhci_host *host)
1720 printk(KERN_DEBUG DRIVER_NAME ": ============== REGISTER DUMP ==============\n");
1721 @@ -591,6 +633,9 @@ static u8 sdhci_calc_timeout(struct sdhc
1722 target_timeout = data->timeout_ns / 1000 +
1723 data->timeout_clks / host->clock;
1725 + if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
1726 + host->timeout_clk = host->clock / 1000;
1729 * Figure out needed cycles.
1730 * We do this in steps in order to fit inside a 32 bit int.
1731 @@ -622,7 +667,7 @@ static u8 sdhci_calc_timeout(struct sdhc
1732 static void sdhci_set_transfer_irqs(struct sdhci_host *host)
1734 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
1735 - u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
1736 + u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ACMD12ERR | SDHCI_INT_ADMA_ERROR;
1738 if (host->flags & SDHCI_REQ_USE_DMA)
1739 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
1740 @@ -652,7 +697,7 @@ static void sdhci_prepare_data(struct sd
1741 count = sdhci_calc_timeout(host, data);
1742 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
1744 - if (host->flags & SDHCI_USE_DMA)
1745 + if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
1746 host->flags |= SDHCI_REQ_USE_DMA;
1749 @@ -736,11 +781,21 @@ static void sdhci_prepare_data(struct sd
1753 + sd_printk("[SDHCI_DEBUG] dma_map_sg(), mmc_dev(host->mmc) = %p \n", mmc_dev(host->mmc));
1754 + sd_printk("[SDHCI_DEBUG] dma_map_sg(), data->sg = %p \n", data->sg);
1755 + sd_printk("[SDHCI_DEBUG] dma_map_sg(), data->sg_len = %d \n", data->sg_len);
1756 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
1757 data->sg, data->sg_len,
1758 (data->flags & MMC_DATA_READ) ?
1761 + if (data->sg == NULL) {
1762 + sd_printk("[SDHCI_DEBUG] dma_map_sg(), data->sg = (NULL) \n");
1765 + sd_printk("[SDHCI_DEBUG] dma_map_sg(), data->sg = %p \n", data->sg);
1766 + sd_printk("[SDHCI_DEBUG] dma_map_sg(), sg_cnt = %d \n", sg_cnt);
1770 * This only happens when someone fed
1771 @@ -750,6 +805,7 @@ static void sdhci_prepare_data(struct sd
1772 host->flags &= ~SDHCI_REQ_USE_DMA;
1774 WARN_ON(sg_cnt != 1);
1775 + sd_printk("[SDHCI_DEBUG] sg_dma_address() => %08x \n", sg_dma_address(data->sg));
1776 sdhci_writel(host, sg_dma_address(data->sg),
1779 @@ -763,14 +819,32 @@ static void sdhci_prepare_data(struct sd
1781 if (host->version >= SDHCI_SPEC_200) {
1782 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1783 +#ifdef SDHCI_USE_LEDS_CLASS
1784 + ctrl |= SDHCI_CTRL_LED;
1786 + ctrl &= ~SDHCI_CTRL_LED;
1788 ctrl &= ~SDHCI_CTRL_DMA_MASK;
1789 if ((host->flags & SDHCI_REQ_USE_DMA) &&
1790 (host->flags & SDHCI_USE_ADMA))
1791 ctrl |= SDHCI_CTRL_ADMA32;
1793 ctrl |= SDHCI_CTRL_SDMA;
1795 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1797 + } else if (host->version == SDHCI_SPEC_100) {
1798 + ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1799 +#ifdef SDHCI_USE_LEDS_CLASS
1800 + ctrl |= SDHCI_CTRL_LED;
1802 + ctrl &= ~SDHCI_CTRL_LED;
1804 + ctrl &= ~SDHCI_CTRL_DMA_MASK;
1805 + if (host->flags & SDHCI_REQ_USE_DMA)
1806 + ctrl |= SDHCI_CTRL_SDMA;
1808 + sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1811 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
1813 @@ -795,15 +869,26 @@ static void sdhci_set_transfer_mode(stru
1814 struct mmc_data *data)
1822 WARN_ON(!host->data);
1824 + bgctrl = sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL);
1825 + if (host->quirks & SDHCI_QUIRK_READ_WAIT_CTRL)
1826 + bgctrl |= SDHCI_READ_WAIT_CTRL;
1827 + sdhci_writeb(host, bgctrl, SDHCI_BLOCK_GAP_CONTROL);
1829 mode = SDHCI_TRNS_BLK_CNT_EN;
1830 - if (data->blocks > 1)
1832 + if (data->blocks > 1) {
1833 mode |= SDHCI_TRNS_MULTI;
1835 + if (host->quirks & SDHCI_QUIRK_AUTO_CMD12)
1836 + mode |= SDHCI_TRNS_ACMD12;
1838 if (data->flags & MMC_DATA_READ)
1839 mode |= SDHCI_TRNS_READ;
1840 if (host->flags & SDHCI_REQ_USE_DMA)
1841 @@ -812,6 +897,20 @@ static void sdhci_set_transfer_mode(stru
1842 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
1845 +static void shdci_check_dma_overrun(struct sdhci_host *host, struct mmc_data *data)
1847 + u32 dma_pos = sdhci_readl(host, SDHCI_DMA_ADDRESS);
1848 + u32 dma_start = sg_dma_address(data->sg);
1849 + u32 dma_end = dma_start + data->sg->length;
1851 + /* Test whether we ended up moving more data than was originally requested. */
1852 + if (dma_pos <= dma_end)
1855 + printk(KERN_ERR "%s: dma overrun, dma %08x, req %08x..%08x\n",
1856 + mmc_hostname(host->mmc), dma_pos, dma_start, dma_end);
1859 static void sdhci_finish_data(struct sdhci_host *host)
1861 struct mmc_data *data;
1862 @@ -825,6 +924,9 @@ static void sdhci_finish_data(struct sdh
1863 if (host->flags & SDHCI_USE_ADMA)
1864 sdhci_adma_table_post(host, data);
1866 + shdci_check_dma_overrun(host, data);
1868 + sd_printk("[SDHCI_DEBUG] dma_unmap_sg(), data->sg_len = %d \n", data->sg_len);
1869 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
1870 data->sg_len, (data->flags & MMC_DATA_READ) ?
1871 DMA_FROM_DEVICE : DMA_TO_DEVICE);
1872 @@ -866,12 +968,16 @@ static void sdhci_send_command(struct sd
1876 + sd_printk("[SDHCI_DEBUG] sdhci_send_command() \n");
1878 /* Wait max 10 ms */
1881 mask = SDHCI_CMD_INHIBIT;
1882 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
1883 mask |= SDHCI_DATA_INHIBIT;
1884 + if ((cmd->data != NULL))
1885 + mask |= SDHCI_DATA_INHIBIT;
1887 /* We shouldn't wait for data inihibit for stop commands, even
1888 though they might use busy signaling */
1889 @@ -925,7 +1031,11 @@ static void sdhci_send_command(struct sd
1891 flags |= SDHCI_CMD_DATA;
1893 + sd_printk("[SDHCI_DEBUG] sdhci_send_command() => %08x \n", SDHCI_MAKE_CMD(cmd->opcode, flags));
1894 + sdhci_dumpallregs(host);
1895 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
1896 + sd_printk("[SDHCI_DEBUG] sdhci_send_command(): After issue command \n");
1897 + sdhci_dumpallregs(host);
1900 static void sdhci_finish_command(struct sdhci_host *host)
1901 @@ -934,6 +1044,8 @@ static void sdhci_finish_command(struct
1903 BUG_ON(host->cmd == NULL);
1905 + sd_printk("[SDHCI_DEBUG] sdhci_finish_command() \n");
1907 if (host->cmd->flags & MMC_RSP_PRESENT) {
1908 if (host->cmd->flags & MMC_RSP_136) {
1909 /* CRC is stripped so we need to do some shifting. */
1910 @@ -991,8 +1103,8 @@ static void sdhci_set_clock(struct sdhci
1911 clk |= SDHCI_CLOCK_INT_EN;
1912 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1914 - /* Wait max 10 ms */
1916 + /* Wait max 20 ms */
1918 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
1919 & SDHCI_CLOCK_INT_STABLE)) {
1921 @@ -1154,6 +1266,12 @@ static void sdhci_set_ios(struct mmc_hos
1923 ctrl &= ~SDHCI_CTRL_HISPD;
1925 +#ifdef SDHCI_USE_LEDS_CLASS
1926 + ctrl |= SDHCI_CTRL_LED;
1928 + ctrl &= ~SDHCI_CTRL_LED;
1931 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1934 @@ -1321,7 +1439,11 @@ static void sdhci_timeout_timer(unsigned
1936 printk(KERN_ERR "%s: Timeout waiting for hardware "
1937 "interrupt.\n", mmc_hostname(host->mmc));
1939 + sdhci_dumpallregs(host);
1941 sdhci_dumpregs(host);
1945 host->data->error = -ETIMEDOUT;
1946 @@ -1508,6 +1630,10 @@ static irqreturn_t sdhci_irq(int irq, vo
1947 DBG("*** %s got interrupt: 0x%08x\n",
1948 mmc_hostname(host->mmc), intmask);
1951 + printk("*** %s got interrupt: 0x%08x\n", mmc_hostname(host->mmc), intmask);
1954 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
1955 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
1956 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
1957 @@ -1597,7 +1723,7 @@ int sdhci_resume_host(struct sdhci_host
1961 - if (host->flags & SDHCI_USE_DMA) {
1962 + if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
1963 if (host->ops->enable_dma)
1964 host->ops->enable_dma(host);
1966 @@ -1678,23 +1804,20 @@ int sdhci_add_host(struct sdhci_host *ho
1967 caps = sdhci_readl(host, SDHCI_CAPABILITIES);
1969 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
1970 - host->flags |= SDHCI_USE_DMA;
1971 - else if (!(caps & SDHCI_CAN_DO_DMA))
1972 - DBG("Controller doesn't have DMA capability\n");
1973 + host->flags |= SDHCI_USE_SDMA;
1974 + else if (!(caps & SDHCI_CAN_DO_SDMA))
1975 + DBG("Controller doesn't have SDMA capability\n");
1977 - host->flags |= SDHCI_USE_DMA;
1978 + host->flags |= SDHCI_USE_SDMA;
1980 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
1981 - (host->flags & SDHCI_USE_DMA)) {
1982 + (host->flags & SDHCI_USE_SDMA)) {
1983 DBG("Disabling DMA as it is marked broken\n");
1984 - host->flags &= ~SDHCI_USE_DMA;
1985 + host->flags &= ~SDHCI_USE_SDMA;
1988 - if (host->flags & SDHCI_USE_DMA) {
1989 - if ((host->version >= SDHCI_SPEC_200) &&
1990 - (caps & SDHCI_CAN_DO_ADMA2))
1991 - host->flags |= SDHCI_USE_ADMA;
1993 + if ((host->version >= SDHCI_SPEC_200) && (caps & SDHCI_CAN_DO_ADMA2))
1994 + host->flags |= SDHCI_USE_ADMA;
1996 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
1997 (host->flags & SDHCI_USE_ADMA)) {
1998 @@ -1702,13 +1825,14 @@ int sdhci_add_host(struct sdhci_host *ho
1999 host->flags &= ~SDHCI_USE_ADMA;
2002 - if (host->flags & SDHCI_USE_DMA) {
2003 + if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2004 if (host->ops->enable_dma) {
2005 if (host->ops->enable_dma(host)) {
2006 printk(KERN_WARNING "%s: No suitable DMA "
2007 "available. Falling back to PIO.\n",
2009 - host->flags &= ~(SDHCI_USE_DMA | SDHCI_USE_ADMA);
2011 + ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
2015 @@ -1736,7 +1860,7 @@ int sdhci_add_host(struct sdhci_host *ho
2016 * mask, but PIO does not need the hw shim so we set a new
2017 * mask here in that case.
2019 - if (!(host->flags & SDHCI_USE_DMA)) {
2020 + if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
2021 host->dma_mask = DMA_BIT_MASK(64);
2022 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2024 @@ -1757,13 +1881,15 @@ int sdhci_add_host(struct sdhci_host *ho
2026 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
2027 if (host->timeout_clk == 0) {
2028 - if (!host->ops->get_timeout_clock) {
2029 + if (host->ops->get_timeout_clock) {
2030 + host->timeout_clk = host->ops->get_timeout_clock(host);
2031 + } else if (!(host->quirks &
2032 + SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
2034 "%s: Hardware doesn't specify timeout clock "
2035 "frequency.\n", mmc_hostname(mmc));
2038 - host->timeout_clk = host->ops->get_timeout_clock(host);
2040 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
2041 host->timeout_clk *= 1000;
2042 @@ -1772,7 +1898,8 @@ int sdhci_add_host(struct sdhci_host *ho
2043 * Set host parameters.
2045 mmc->ops = &sdhci_ops;
2046 - if (host->ops->get_min_clock)
2047 + if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK &&
2048 + host->ops->set_clock && host->ops->get_min_clock)
2049 mmc->f_min = host->ops->get_min_clock(host);
2051 mmc->f_min = host->max_clk / 256;
2052 @@ -1810,7 +1937,7 @@ int sdhci_add_host(struct sdhci_host *ho
2054 if (host->flags & SDHCI_USE_ADMA)
2055 mmc->max_hw_segs = 128;
2056 - else if (host->flags & SDHCI_USE_DMA)
2057 + else if (host->flags & SDHCI_USE_SDMA)
2058 mmc->max_hw_segs = 1;
2060 mmc->max_hw_segs = 128;
2061 @@ -1893,10 +2020,10 @@ int sdhci_add_host(struct sdhci_host *ho
2065 - printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s%s\n",
2066 + printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s\n",
2067 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
2068 - (host->flags & SDHCI_USE_ADMA)?"A":"",
2069 - (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
2070 + (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
2071 + (host->flags & SDHCI_USE_SDMA) ? "SDMA" : "PIO");
2073 sdhci_enable_card_detection(host);
2076 +++ b/drivers/mmc/host/sdhci-cns3xxx.c
2078 +/*******************************************************************************
2080 + * drivers/mmc/host/sdhci-cns3xxx.c
2082 + * SDHCI support for the CNS3XXX SOCs
2084 + * Author: Scott Shu
2086 + * Copyright (c) 2008 Cavium Networks
2088 + * This file is free software; you can redistribute it and/or modify
2089 + * it under the terms of the GNU General Public License, Version 2, as
2090 + * published by the Free Software Foundation.
2092 + * This file is distributed in the hope that it will be useful,
2093 + * but AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty of
2094 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
2095 + * NONINFRINGEMENT. See the GNU General Public License for more details.
2097 + * You should have received a copy of the GNU General Public License
2098 + * along with this file; if not, write to the Free Software
2099 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA or
2100 + * visit http://www.gnu.org/licenses/.
2102 + * This file may also be available under a different license from Cavium.
2103 + * Contact Cavium Networks for more information
2105 + ******************************************************************************/
2107 +#include <linux/delay.h>
2108 +#include <linux/highmem.h>
2109 +#include <linux/platform_device.h>
2110 +#include <linux/dma-mapping.h>
2112 +#include <linux/mmc/host.h>
2114 +#include <asm/scatterlist.h>
2115 +#include <asm/io.h>
2116 +#include <linux/interrupt.h>
2118 +#include <mach/sdhci.h>
2119 +#include <mach/pm.h>
2125 +#define MAX_BUS_CLK (4)
2127 +static unsigned __initdata use_dma = 0;
2129 +struct sdhci_cns3xxx {
2130 + struct sdhci_host *host;
2131 + struct platform_device *pdev;
2132 + struct resource *ioarea;
2133 + struct cns3xxx_sdhci_platdata *pdata;
2134 + struct clk *clk_io;
2135 + struct clk *clk_bus[MAX_BUS_CLK];
2138 +static unsigned int sdhci_cns3xxx_get_max_clk(struct sdhci_host *host)
2140 + int clk = 50000000;
2145 +static unsigned int sdhci_cns3xxx_get_timeout_clk(struct sdhci_host *host)
2147 + return sdhci_cns3xxx_get_max_clk(host) / 100000;
2151 + * sdhci_cns3xxx_set_clock - callback on clock change
2153 + * When the card's clock is going to be changed, look at the new frequency
2154 + * and find the best clock source to go with it.
2156 +static void sdhci_cns3xxx_set_clock(struct sdhci_host *host, unsigned int clock)
2159 + unsigned long timeout;
2161 + if (clock == host->clock)
2164 + sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
2169 + clk = 0x03 << SDHCI_DIVIDER_SHIFT; /* base clock divided by 3 */
2171 + /* high speed mode or normal speed mode */
2172 + if (0x4 & sdhci_readw(host, 0x28)) {
2173 + clk = 0x03 << SDHCI_DIVIDER_SHIFT; /* base clock divided by 3 */
2175 + clk = 0x02 << SDHCI_DIVIDER_SHIFT; /* base clock divided by 4 */
2178 + clk |= SDHCI_CLOCK_INT_EN;
2179 + sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
2182 + while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
2183 + & SDHCI_CLOCK_INT_STABLE)) {
2184 + if (timeout == 0) {
2191 + clk |= SDHCI_CLOCK_CARD_EN;
2192 + sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
2194 + host->timeout_clk = sdhci_cns3xxx_get_timeout_clk(host);
2196 + host->clock = clock;
2199 +static struct sdhci_ops sdhci_cns3xxx_ops = {
2200 + .get_max_clock = sdhci_cns3xxx_get_max_clk,
2201 + .get_timeout_clock = sdhci_cns3xxx_get_timeout_clk,
2202 + .set_clock = sdhci_cns3xxx_set_clock,
2205 +static int __devinit sdhci_cns3xxx_probe(struct platform_device *pdev)
2207 + struct cns3xxx_sdhci_platdata *pdata = pdev->dev.platform_data;
2208 + struct device *dev = &pdev->dev;
2209 + struct sdhci_host *host;
2210 + struct sdhci_cns3xxx *sc;
2211 + struct resource *res;
2215 + dev_err(dev, "no device data specified\n");
2219 + irq = platform_get_irq(pdev, 0);
2221 + dev_err(dev, "no irq specified\n");
2225 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2227 + dev_err(dev, "no memory specified\n");
2231 + host = sdhci_alloc_host(dev, sizeof(*sc));
2232 + if (IS_ERR(host)) {
2233 + dev_err(dev, "sdhci_alloc_host() failed\n");
2234 + return PTR_ERR(host);
2237 + sc = sdhci_priv(host);
2241 + sc->pdata = pdata;
2243 + platform_set_drvdata(pdev, host);
2245 + sc->ioarea = request_mem_region(res->start, resource_size(res), mmc_hostname(host->mmc));
2246 + if (!sc->ioarea) {
2247 + dev_err(dev, "failed to reserve register area\n");
2249 + goto err_req_regs;
2252 + host->ioaddr = ioremap_nocache(res->start, resource_size(res));
2253 + if (!host->ioaddr) {
2254 + dev_err(dev, "failed to map registers\n");
2256 + goto err_req_regs;
2259 + host->hw_name = "cns3xxx";
2260 + host->ops = &sdhci_cns3xxx_ops;
2264 + if (use_dma != 1) {
2265 + host->quirks |= SDHCI_QUIRK_BROKEN_DMA;
2266 + host->quirks |= SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK;
2268 + host->quirks |= SDHCI_QUIRK_FORCE_DMA;
2269 + host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
2270 + host->quirks |= (SDHCI_QUIRK_32BIT_DMA_ADDR | SDHCI_QUIRK_32BIT_DMA_SIZE);
2271 + host->quirks |= SDHCI_QUIRK_NO_BUSY_IRQ;
2272 + //host->quirks |= SDHCI_QUIRK_FORCE_BLK_SZ_2048;
2273 + //host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK;
2274 + host->quirks |= SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK;
2275 + host->quirks |= SDHCI_QUIRK_AUTO_CMD12;
2276 + host->quirks |= SDHCI_QUIRK_READ_WAIT_CTRL;
2279 + //host->quirks |= SDHCI_QUIRK_INVERTED_WRITE_PROTECT;
2281 + host->quirks |= SDHCI_QUIRK_NONSTANDARD_CLOCK;
2283 + host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
2285 + ret = sdhci_add_host(host);
2287 + dev_err(dev, "sdhci_add_host() failed (%d)\n", ret);
2288 + goto err_add_host;
2294 + free_irq(host->irq, host);
2295 + iounmap(host->ioaddr);
2296 + release_resource(sc->ioarea);
2297 + kfree(sc->ioarea);
2300 + sdhci_free_host(host);
2305 +static int __devexit sdhci_cns3xxx_remove(struct platform_device *pdev)
2307 + struct device *dev = &pdev->dev;
2308 + struct sdhci_host *host = dev_get_drvdata(dev);
2309 + struct resource *res;
2311 + pr_debug("%s: remove=%p\n", __func__, pdev);
2313 + sdhci_remove_host(host, 0);
2314 + sdhci_free_host(host);
2315 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2316 + release_mem_region(res->start, resource_size(res));
2323 +static int sdhci_cns3xxx_suspend(struct platform_device *dev, pm_message_t state)
2329 +static int sdhci_cns3xxx_resume(struct platform_device *dev)
2336 +#define sdhci_cns3xxx_suspend NULL
2337 +#define sdhci_cns3xxx_resume NULL
2338 +#endif /* CONFIG_PM */
2340 +static struct platform_driver sdhci_cns3xxx_driver = {
2341 + .probe = sdhci_cns3xxx_probe,
2342 + .remove = __devexit_p(sdhci_cns3xxx_remove),
2343 + .suspend = sdhci_cns3xxx_suspend,
2344 + .resume = sdhci_cns3xxx_resume,
2346 + .name = "cns3xxx-sdhci",
2347 + .owner = THIS_MODULE,
2351 +static char banner[] __initdata = KERN_INFO "cns3xxx-sdhci, (c) 2009 Cavium Networks\n";
2353 +static int __init sdhci_cns3xxx_init(void)
2355 +#ifdef CONFIG_SILICON
2356 + unsigned long gpioapin = __raw_readl((void __iomem *)(CNS3XXX_MISC_BASE_VIRT + 0x0014));;
2358 + unsigned long status = __raw_readl((void __iomem *)(CNS3XXX_MISC_BASE_VIRT + 0x0700));
2363 +#ifdef CONFIG_SILICON
2364 + /* MMC/SD pins share with GPIOA */
2365 + __raw_writel(gpioapin | (0x1fff0004), (void __iomem *)(CNS3XXX_MISC_BASE_VIRT + 0x0014));
2366 + cns3xxx_pwr_clk_en(CNS3XXX_PWR_CLK_EN(SDIO));
2367 + cns3xxx_pwr_soft_rst(CNS3XXX_PWR_SOFTWARE_RST(SDIO));
2369 + /* insert a delay on SDIO output interface (only for FPGA mode & high-speed mode) */
2370 + __raw_writel(status | (1 << 4), (void __iomem *)(CNS3XXX_MISC_BASE_VIRT + 0x0700));
2372 + return platform_driver_register(&sdhci_cns3xxx_driver);
2375 +static void __exit sdhci_cns3xxx_exit(void)
2377 + platform_driver_unregister(&sdhci_cns3xxx_driver);
2380 +module_init(sdhci_cns3xxx_init);
2381 +module_exit(sdhci_cns3xxx_exit);
2383 +module_param(use_dma, uint, 0);
2385 +MODULE_AUTHOR("Scott Shu");
2386 +MODULE_DESCRIPTION("Cavium Networks CNS3XXX SDHCI glue");
2387 +MODULE_LICENSE("GPL");
2388 +MODULE_ALIAS("platform:cns3xxx-sdhci");
2390 +MODULE_PARM_DESC(use_dma, "Whether to use DMA or not. Default = 0");
2391 --- a/drivers/mmc/host/sdhci.h
2392 +++ b/drivers/mmc/host/sdhci.h
2394 * the Free Software Foundation; either version 2 of the License, or (at
2395 * your option) any later version.
2400 #include <linux/scatterlist.h>
2401 #include <linux/compiler.h>
2403 #define SDHCI_POWER_330 0x0E
2405 #define SDHCI_BLOCK_GAP_CONTROL 0x2A
2406 +#define SDHCI_READ_WAIT_CTRL 0x04
2408 #define SDHCI_WAKE_UP_CONTROL 0x2B
2411 #define SDHCI_CAN_DO_ADMA2 0x00080000
2412 #define SDHCI_CAN_DO_ADMA1 0x00100000
2413 #define SDHCI_CAN_DO_HISPD 0x00200000
2414 -#define SDHCI_CAN_DO_DMA 0x00400000
2415 +#define SDHCI_CAN_DO_SDMA 0x00400000
2416 #define SDHCI_CAN_VDD_330 0x01000000
2417 #define SDHCI_CAN_VDD_300 0x02000000
2418 #define SDHCI_CAN_VDD_180 0x04000000
2419 @@ -232,6 +235,12 @@ struct sdhci_host {
2420 #define SDHCI_QUIRK_FORCE_1_BIT_DATA (1<<22)
2421 /* Controller needs 10ms delay between applying power and clock */
2422 #define SDHCI_QUIRK_DELAY_AFTER_POWER (1<<23)
2423 +/* Controller uses SDCLK instead of TMCLK for data timeouts */
2424 +#define SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK (1<<24)
2425 +/* Controller uses Auto CMD12 */
2426 +#define SDHCI_QUIRK_AUTO_CMD12 (1<<25)
2427 +/* Controller uses read wait control protocol */
2428 +#define SDHCI_QUIRK_READ_WAIT_CTRL (1<<26)
2430 int irq; /* Device IRQ */
2431 void __iomem * ioaddr; /* Mapped address */
2432 @@ -250,7 +259,7 @@ struct sdhci_host {
2433 spinlock_t lock; /* Mutex */
2435 int flags; /* Host attributes */
2436 -#define SDHCI_USE_DMA (1<<0) /* Host is DMA capable */
2437 +#define SDHCI_USE_SDMA (1<<0) /* Host is SDMA capable */
2438 #define SDHCI_USE_ADMA (1<<1) /* Host is ADMA capable */
2439 #define SDHCI_REQ_USE_DMA (1<<2) /* Use DMA for this req. */
2440 #define SDHCI_DEVICE_DEAD (1<<3) /* Device unresponsive */
2441 @@ -406,3 +415,5 @@ extern void sdhci_remove_host(struct sdh
2442 extern int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state);
2443 extern int sdhci_resume_host(struct sdhci_host *host);
2446 +#endif /* __SDHCI_H */
2447 --- a/include/linux/mmc/card.h
2448 +++ b/include/linux/mmc/card.h
2449 @@ -40,6 +40,8 @@ struct mmc_csd {
2452 struct mmc_ext_csd {
2454 + unsigned int sa_timeout; /* Units: 100ns */
2455 unsigned int hs_max_dtr;
2456 unsigned int sectors;
2458 @@ -62,7 +64,8 @@ struct sdio_cccr {
2468 @@ -94,6 +97,8 @@ struct mmc_card {
2469 #define MMC_STATE_READONLY (1<<1) /* card is read-only */
2470 #define MMC_STATE_HIGHSPEED (1<<2) /* card is in high speed mode */
2471 #define MMC_STATE_BLOCKADDR (1<<3) /* card uses block-addressing */
2472 + unsigned int quirks; /* card quirks */
2473 +#define MMC_QUIRK_LENIENT_FN0 (1<<0) /* allow SDIO FN0 writes outside of the VS CCCR range */
2475 u32 raw_cid[4]; /* raw card CID */
2476 u32 raw_csd[4]; /* raw card CSD */
2477 @@ -129,6 +134,11 @@ struct mmc_card {
2478 #define mmc_card_set_highspeed(c) ((c)->state |= MMC_STATE_HIGHSPEED)
2479 #define mmc_card_set_blockaddr(c) ((c)->state |= MMC_STATE_BLOCKADDR)
2481 +static inline int mmc_card_lenient_fn0(const struct mmc_card *c)
2483 + return c->quirks & MMC_QUIRK_LENIENT_FN0;
2486 #define mmc_card_name(c) ((c)->cid.prod_name)
2487 #define mmc_card_id(c) (dev_name(&(c)->dev))
2489 --- a/include/linux/mmc/core.h
2490 +++ b/include/linux/mmc/core.h
2491 @@ -139,6 +139,7 @@ extern unsigned int mmc_align_data_size(
2493 extern int __mmc_claim_host(struct mmc_host *host, atomic_t *abort);
2494 extern void mmc_release_host(struct mmc_host *host);
2495 +extern int mmc_try_claim_host(struct mmc_host *host);
2498 * mmc_claim_host - exclusively claim a host
2499 --- a/include/linux/mmc/host.h
2500 +++ b/include/linux/mmc/host.h
2502 #define LINUX_MMC_HOST_H
2504 #include <linux/leds.h>
2505 +#include <linux/sched.h>
2507 #include <linux/mmc/core.h>
2509 @@ -51,6 +52,35 @@ struct mmc_ios {
2512 struct mmc_host_ops {
2514 + * Hosts that support power saving can use the 'enable' and 'disable'
2515 + * methods to exit and enter power saving states. 'enable' is called
2516 + * when the host is claimed and 'disable' is called (or scheduled with
2517 + * a delay) when the host is released. The 'disable' is scheduled if
2518 + * the disable delay set by 'mmc_set_disable_delay()' is non-zero,
2519 + * otherwise 'disable' is called immediately. 'disable' may be
2520 + * scheduled repeatedly, to permit ever greater power saving at the
2521 + * expense of ever greater latency to re-enable. Rescheduling is
2522 + * determined by the return value of the 'disable' method. A positive
2523 + * value gives the delay in milliseconds.
2525 + * In the case where a host function (like set_ios) may be called
2526 + * with or without the host claimed, enabling and disabling can be
2527 + * done directly and will nest correctly. Call 'mmc_host_enable()' and
2528 + * 'mmc_host_lazy_disable()' for this purpose, but note that these
2529 + * functions must be paired.
2531 + * Alternatively, 'mmc_host_enable()' may be paired with
2532 + * 'mmc_host_disable()' which calls 'disable' immediately. In this
2533 + * case the 'disable' method will be called with 'lazy' set to 0.
2534 + * This is mainly useful for error paths.
2536 + * Because lazy disable may be called from a work queue, the 'disable'
2537 + * method must claim the host when 'lazy' != 0, which will work
2538 + * correctly because recursion is detected and handled.
2540 + int (*enable)(struct mmc_host *host);
2541 + int (*disable)(struct mmc_host *host, int lazy);
2542 void (*request)(struct mmc_host *host, struct mmc_request *req);
2544 * Avoid calling these three functions too often or in a "fast path",
2545 @@ -118,6 +148,9 @@ struct mmc_host {
2546 #define MMC_CAP_SPI (1 << 4) /* Talks only SPI protocols */
2547 #define MMC_CAP_NEEDS_POLL (1 << 5) /* Needs polling for card-detection */
2548 #define MMC_CAP_8_BIT_DATA (1 << 6) /* Can the host do 8 bit transfers */
2549 +#define MMC_CAP_DISABLE (1 << 7) /* Can the host be disabled */
2550 +#define MMC_CAP_NONREMOVABLE (1 << 8) /* Nonremovable e.g. eMMC */
2551 +#define MMC_CAP_WAIT_WHILE_BUSY (1 << 9) /* Waits while card is busy */
2553 /* host specific block data */
2554 unsigned int max_seg_size; /* see blk_queue_max_segment_size */
2555 @@ -142,9 +175,18 @@ struct mmc_host {
2556 unsigned int removed:1; /* host is being removed */
2559 + /* Only used with MMC_CAP_DISABLE */
2560 + int enabled; /* host is enabled */
2561 + int nesting_cnt; /* "enable" nesting count */
2562 + int en_dis_recurs; /* detect recursion */
2563 + unsigned int disable_delay; /* disable delay in msecs */
2564 + struct delayed_work disable; /* disabling work */
2566 struct mmc_card *card; /* device attached to this host */
2568 wait_queue_head_t wq;
2569 + struct task_struct *claimer; /* task that has host claimed */
2570 + int claim_cnt; /* "claim" nesting count */
2572 struct delayed_work detect;
2574 @@ -183,6 +225,9 @@ static inline void *mmc_priv(struct mmc_
2575 extern int mmc_suspend_host(struct mmc_host *, pm_message_t);
2576 extern int mmc_resume_host(struct mmc_host *);
2578 +extern void mmc_power_save_host(struct mmc_host *host);
2579 +extern void mmc_power_restore_host(struct mmc_host *host);
2581 extern void mmc_detect_change(struct mmc_host *, unsigned long delay);
2582 extern void mmc_request_done(struct mmc_host *, struct mmc_request *);
2584 @@ -197,5 +242,19 @@ struct regulator;
2585 int mmc_regulator_get_ocrmask(struct regulator *supply);
2586 int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit);
2588 +int mmc_card_awake(struct mmc_host *host);
2589 +int mmc_card_sleep(struct mmc_host *host);
2590 +int mmc_card_can_sleep(struct mmc_host *host);
2592 +int mmc_host_enable(struct mmc_host *host);
2593 +int mmc_host_disable(struct mmc_host *host);
2594 +int mmc_host_lazy_disable(struct mmc_host *host);
2596 +static inline void mmc_set_disable_delay(struct mmc_host *host,
2597 + unsigned int disable_delay)
2599 + host->disable_delay = disable_delay;
2604 --- a/include/linux/mmc/mmc.h
2605 +++ b/include/linux/mmc/mmc.h
2607 #define MMC_ALL_SEND_CID 2 /* bcr R2 */
2608 #define MMC_SET_RELATIVE_ADDR 3 /* ac [31:16] RCA R1 */
2609 #define MMC_SET_DSR 4 /* bc [31:16] RCA */
2610 +#define MMC_SLEEP_AWAKE 5 /* ac [31:16] RCA 15:flg R1b */
2611 #define MMC_SWITCH 6 /* ac [31:0] See below R1b */
2612 #define MMC_SELECT_CARD 7 /* ac [31:16] RCA R1 */
2613 #define MMC_SEND_EXT_CSD 8 /* adtc R1 */
2615 #define R1_STATUS(x) (x & 0xFFFFE000)
2616 #define R1_CURRENT_STATE(x) ((x & 0x00001E00) >> 9) /* sx, b (4 bits) */
2617 #define R1_READY_FOR_DATA (1 << 8) /* sx, a */
2618 +#define R1_SWITCH_ERROR (1 << 7) /* sx, c */
2619 #define R1_APP_CMD (1 << 5) /* sr, c */
2622 @@ -254,6 +256,7 @@ struct _mmc_csd {
2623 #define EXT_CSD_CARD_TYPE 196 /* RO */
2624 #define EXT_CSD_REV 192 /* RO */
2625 #define EXT_CSD_SEC_CNT 212 /* RO, 4 bytes */
2626 +#define EXT_CSD_S_A_TIMEOUT 217
2629 * EXT_CSD field definitions
2630 --- a/include/linux/mmc/sdio_func.h
2631 +++ b/include/linux/mmc/sdio_func.h
2632 @@ -67,6 +67,7 @@ struct sdio_func {
2634 #define sdio_get_drvdata(f) dev_get_drvdata(&(f)->dev)
2635 #define sdio_set_drvdata(f,d) dev_set_drvdata(&(f)->dev, d)
2636 +#define dev_to_sdio_func(d) container_of(d, struct sdio_func, dev)
2639 * SDIO function device driver
2640 @@ -81,6 +82,8 @@ struct sdio_driver {
2641 struct device_driver drv;
2644 +#define to_sdio_driver(d) container_of(d, struct sdio_driver, drv)
2647 * SDIO_DEVICE - macro used to describe a specific SDIO device
2648 * @vend: the 16 bit manufacturer code
2649 --- a/include/linux/mmc/sdio_ids.h
2650 +++ b/include/linux/mmc/sdio_ids.h
2653 * Vendors and devices. Sort key: vendor first, device next.
2655 +#define SDIO_VENDOR_ID_INTEL 0x0089
2656 +#define SDIO_DEVICE_ID_INTEL_IWMC3200WIMAX 0x1402
2657 +#define SDIO_DEVICE_ID_INTEL_IWMC3200WIFI 0x1403
2658 +#define SDIO_DEVICE_ID_INTEL_IWMC3200TOP 0x1404
2659 +#define SDIO_DEVICE_ID_INTEL_IWMC3200GPS 0x1405
2660 +#define SDIO_DEVICE_ID_INTEL_IWMC3200BT 0x1406
2662 #define SDIO_VENDOR_ID_MARVELL 0x02df
2663 #define SDIO_DEVICE_ID_MARVELL_LIBERTAS 0x9103