3f81ad81ebdeafba5ee35189a23e317d58ac48d5
[librecmc/librecmc.git] / target / linux / cns3xxx / patches-2.6.31 / 204-cns3xxx_mmc_support.patch
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
4         return 0;
5  }
6  
7 -static struct block_device_operations mmc_bdops = {
8 +static const struct block_device_operations mmc_bdops = {
9         .open                   = mmc_blk_open,
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));
15  
16 -#if 0
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))
21 -                               goto cmd_err;
22 -#endif
23                 }
24  
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
28 @@ -37,6 +37,9 @@
29  #include "sd_ops.h"
30  #include "sdio_ops.h"
31  
32 +/* scott.trace */
33 +//#define MMC_DEBUG
34 +
35  static struct workqueue_struct *workqueue;
36  
37  /*
38 @@ -90,17 +93,30 @@ void mmc_request_done(struct mmc_host *h
39                 cmd->error = 0;
40                 host->ops->request(host, mrq);
41         } else {
42 +#if defined(CONFIG_LEDS_CLASS) || defined(CONFIG_LEDS_CLASS_MODULE)
43                 led_trigger_event(host->led, LED_OFF);
44 +#endif
45  
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]);
50 +#ifdef MMC_DEBUG
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]);
55 +#endif
56  
57                 if (mrq->data) {
58                         pr_debug("%s:     %d bytes transferred: %d\n",
59                                 mmc_hostname(host),
60                                 mrq->data->bytes_xfered, mrq->data->error);
61 +#ifdef MMC_DEBUG
62 +                       printk("[MMC_DEBUG] %s:     %d bytes transferred: %d\n",
63 +                               mmc_hostname(host),
64 +                               mrq->data->bytes_xfered, mrq->data->error);
65 +#endif
66                 }
67  
68                 if (mrq->stop) {
69 @@ -109,6 +125,13 @@ void mmc_request_done(struct mmc_host *h
70                                 mrq->stop->error,
71                                 mrq->stop->resp[0], mrq->stop->resp[1],
72                                 mrq->stop->resp[2], mrq->stop->resp[3]);
73 +#ifdef MMC_DEBUG
74 +                       printk("[MMC_DEBUG] %s:     (CMD%u): %d: %08x %08x %08x %08x\n",
75 +                               mmc_hostname(host), mrq->stop->opcode,
76 +                               mrq->stop->error,
77 +                               mrq->stop->resp[0], mrq->stop->resp[1],
78 +                               mrq->stop->resp[2], mrq->stop->resp[3]);
79 +#endif
80                 }
81  
82                 if (mrq->done)
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);
87 +#ifdef MMC_DEBUG
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);
91 +#endif
92  
93         if (mrq->data) {
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);
99 +#ifdef MMC_DEBUG
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);
106 +#endif
107         }
108  
109         if (mrq->stop) {
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);
113 +#ifdef MMC_DEBUG
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);
117 +#endif
118         }
119  
120         WARN_ON(!host->claimed);
121  
122 +#if defined(CONFIG_LEDS_CLASS) || defined(CONFIG_LEDS_CLASS_MODULE)
123         led_trigger_event(host->led, LED_FULL);
124 +#endif
125  
126         mrq->cmd->error = 0;
127         mrq->cmd->mrq = mrq;
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.
131                          */
132 -                       limit_us = 300000;
133 +                       limit_us = 500000;
134                 else
135 -                       limit_us = 100000;
136 +                       limit_us = 200000;
137  
138                 /*
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);
142  
143  /**
144 + *     mmc_host_enable - enable a host.
145 + *     @host: mmc host to enable
146 + *
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.
150 + */
151 +int mmc_host_enable(struct mmc_host *host)
152 +{
153 +       if (!(host->caps & MMC_CAP_DISABLE))
154 +               return 0;
155 +
156 +       if (host->en_dis_recurs)
157 +               return 0;
158 +
159 +       if (host->nesting_cnt++)
160 +               return 0;
161 +
162 +       cancel_delayed_work_sync(&host->disable);
163 +
164 +       if (host->enabled)
165 +               return 0;
166 +
167 +       if (host->ops->enable) {
168 +               int err;
169 +
170 +               host->en_dis_recurs = 1;
171 +               err = host->ops->enable(host);
172 +               host->en_dis_recurs = 0;
173 +
174 +               if (err) {
175 +                       pr_debug("%s: enable error %d\n",
176 +                                mmc_hostname(host), err);
177 +                       return err;
178 +               }
179 +       }
180 +       host->enabled = 1;
181 +       return 0;
182 +}
183 +EXPORT_SYMBOL(mmc_host_enable);
184 +
185 +static int mmc_host_do_disable(struct mmc_host *host, int lazy)
186 +{
187 +       if (host->ops->disable) {
188 +               int err;
189 +
190 +               host->en_dis_recurs = 1;
191 +               err = host->ops->disable(host, lazy);
192 +               host->en_dis_recurs = 0;
193 +
194 +               if (err < 0) {
195 +                       pr_debug("%s: disable error %d\n",
196 +                                mmc_hostname(host), err);
197 +                       return err;
198 +               }
199 +               if (err > 0) {
200 +                       unsigned long delay = msecs_to_jiffies(err);
201 +
202 +                       mmc_schedule_delayed_work(&host->disable, delay);
203 +               }
204 +       }
205 +       host->enabled = 0;
206 +       return 0;
207 +}
208 +
209 +/**
210 + *     mmc_host_disable - disable a host.
211 + *     @host: mmc host to disable
212 + *
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.
216 + */
217 +int mmc_host_disable(struct mmc_host *host)
218 +{
219 +       int err;
220 +
221 +       if (!(host->caps & MMC_CAP_DISABLE))
222 +               return 0;
223 +
224 +       if (host->en_dis_recurs)
225 +               return 0;
226 +
227 +       if (--host->nesting_cnt)
228 +               return 0;
229 +
230 +       if (!host->enabled)
231 +               return 0;
232 +
233 +       err = mmc_host_do_disable(host, 0);
234 +       return err;
235 +}
236 +EXPORT_SYMBOL(mmc_host_disable);
237 +
238 +/**
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
243         while (1) {
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)
248                         break;
249                 spin_unlock_irqrestore(&host->lock, flags);
250                 schedule();
251                 spin_lock_irqsave(&host->lock, flags);
252         }
253         set_current_state(TASK_RUNNING);
254 -       if (!stop)
255 +       if (!stop) {
256                 host->claimed = 1;
257 -       else
258 +               host->claimer = current;
259 +               host->claim_cnt += 1;
260 +       } else
261                 wake_up(&host->wq);
262         spin_unlock_irqrestore(&host->lock, flags);
263         remove_wait_queue(&host->wq, &wait);
264 +       if (!stop)
265 +               mmc_host_enable(host);
266         return stop;
267  }
268  
269  EXPORT_SYMBOL(__mmc_claim_host);
270  
271  /**
272 + *     mmc_try_claim_host - try exclusively to claim a host
273 + *     @host: mmc host to claim
274 + *
275 + *     Returns %1 if the host is claimed, %0 otherwise.
276 + */
277 +int mmc_try_claim_host(struct mmc_host *host)
278 +{
279 +       int claimed_host = 0;
280 +       unsigned long flags;
281 +
282 +       spin_lock_irqsave(&host->lock, flags);
283 +       if (!host->claimed || host->claimer == current) {
284 +               host->claimed = 1;
285 +               host->claimer = current;
286 +               host->claim_cnt += 1;
287 +               claimed_host = 1;
288 +       }
289 +       spin_unlock_irqrestore(&host->lock, flags);
290 +       return claimed_host;
291 +}
292 +EXPORT_SYMBOL(mmc_try_claim_host);
293 +
294 +static void mmc_do_release_host(struct mmc_host *host)
295 +{
296 +       unsigned long flags;
297 +
298 +       spin_lock_irqsave(&host->lock, flags);
299 +       if (--host->claim_cnt) {
300 +               /* Release for nested claim */
301 +               spin_unlock_irqrestore(&host->lock, flags);
302 +       } else {
303 +               host->claimed = 0;
304 +               host->claimer = NULL;
305 +               spin_unlock_irqrestore(&host->lock, flags);
306 +               wake_up(&host->wq);
307 +       }
308 +}
309 +
310 +void mmc_host_deeper_disable(struct work_struct *work)
311 +{
312 +       struct mmc_host *host =
313 +               container_of(work, struct mmc_host, disable.work);
314 +
315 +       /* If the host is claimed then we do not want to disable it anymore */
316 +       if (!mmc_try_claim_host(host))
317 +               return;
318 +       mmc_host_do_disable(host, 1);
319 +       mmc_do_release_host(host);
320 +}
321 +
322 +/**
323 + *     mmc_host_lazy_disable - lazily disable a host.
324 + *     @host: mmc host to disable
325 + *
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.
329 + */
330 +int mmc_host_lazy_disable(struct mmc_host *host)
331 +{
332 +       if (!(host->caps & MMC_CAP_DISABLE))
333 +               return 0;
334 +
335 +       if (host->en_dis_recurs)
336 +               return 0;
337 +
338 +       if (--host->nesting_cnt)
339 +               return 0;
340 +
341 +       if (!host->enabled)
342 +               return 0;
343 +
344 +       if (host->disable_delay) {
345 +               mmc_schedule_delayed_work(&host->disable,
346 +                               msecs_to_jiffies(host->disable_delay));
347 +               return 0;
348 +       } else
349 +               return mmc_host_do_disable(host, 1);
350 +}
351 +EXPORT_SYMBOL(mmc_host_lazy_disable);
352 +
353 +/**
354   *     mmc_release_host - release a host
355   *     @host: mmc host to release
356   *
357 @@ -393,15 +617,11 @@ EXPORT_SYMBOL(__mmc_claim_host);
358   */
359  void mmc_release_host(struct mmc_host *host)
360  {
361 -       unsigned long flags;
362 -
363         WARN_ON(!host->claimed);
364  
365 -       spin_lock_irqsave(&host->lock, flags);
366 -       host->claimed = 0;
367 -       spin_unlock_irqrestore(&host->lock, flags);
368 +       mmc_host_lazy_disable(host);
369  
370 -       wake_up(&host->wq);
371 +       mmc_do_release_host(host);
372  }
373  
374  EXPORT_SYMBOL(mmc_release_host);
375 @@ -687,7 +907,13 @@ void mmc_set_timing(struct mmc_host *hos
376   */
377  static void mmc_power_up(struct mmc_host *host)
378  {
379 -       int bit = fls(host->ocr_avail) - 1;
380 +       int bit;
381 +
382 +       /* If ocr is set, we use it */
383 +       if (host->ocr)
384 +               bit = ffs(host->ocr) - 1;
385 +       else
386 +               bit = fls(host->ocr_avail) - 1;
387  
388         host->ios.vdd = bit;
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);
392  #endif
393  
394 +       if (host->caps & MMC_CAP_DISABLE)
395 +               cancel_delayed_work(&host->disable);
396         cancel_delayed_work(&host->detect);
397         mmc_flush_scheduled_work();
398  
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);
403 +               mmc_bus_put(host);
404 +               return;
405         }
406         mmc_bus_put(host);
407  
408 @@ -966,6 +1196,80 @@ void mmc_stop_host(struct mmc_host *host
409         mmc_power_off(host);
410  }
411  
412 +void mmc_power_save_host(struct mmc_host *host)
413 +{
414 +       mmc_bus_get(host);
415 +
416 +       if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
417 +               mmc_bus_put(host);
418 +               return;
419 +       }
420 +
421 +       if (host->bus_ops->power_save)
422 +               host->bus_ops->power_save(host);
423 +
424 +       mmc_bus_put(host);
425 +
426 +       mmc_power_off(host);
427 +}
428 +EXPORT_SYMBOL(mmc_power_save_host);
429 +
430 +void mmc_power_restore_host(struct mmc_host *host)
431 +{
432 +       mmc_bus_get(host);
433 +
434 +       if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
435 +               mmc_bus_put(host);
436 +               return;
437 +       }
438 +
439 +       mmc_power_up(host);
440 +       host->bus_ops->power_restore(host);
441 +
442 +       mmc_bus_put(host);
443 +}
444 +EXPORT_SYMBOL(mmc_power_restore_host);
445 +
446 +int mmc_card_awake(struct mmc_host *host)
447 +{
448 +       int err = -ENOSYS;
449 +
450 +       mmc_bus_get(host);
451 +
452 +       if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
453 +               err = host->bus_ops->awake(host);
454 +
455 +       mmc_bus_put(host);
456 +
457 +       return err;
458 +}
459 +EXPORT_SYMBOL(mmc_card_awake);
460 +
461 +int mmc_card_sleep(struct mmc_host *host)
462 +{
463 +       int err = -ENOSYS;
464 +
465 +       mmc_bus_get(host);
466 +
467 +       if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
468 +               err = host->bus_ops->sleep(host);
469 +
470 +       mmc_bus_put(host);
471 +
472 +       return err;
473 +}
474 +EXPORT_SYMBOL(mmc_card_sleep);
475 +
476 +int mmc_card_can_sleep(struct mmc_host *host)
477 +{
478 +       struct mmc_card *card = host->card;
479 +
480 +       if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
481 +               return 1;
482 +       return 0;
483 +}
484 +EXPORT_SYMBOL(mmc_card_can_sleep);
485 +
486  #ifdef CONFIG_PM
487  
488  /**
489 @@ -975,27 +1279,36 @@ void mmc_stop_host(struct mmc_host *host
490   */
491  int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
492  {
493 +       int err = 0;
494 +
495 +       if (host->caps & MMC_CAP_DISABLE)
496 +               cancel_delayed_work(&host->disable);
497         cancel_delayed_work(&host->detect);
498         mmc_flush_scheduled_work();
499  
500         mmc_bus_get(host);
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) {
507 +                       /*
508 +                        * We simply "remove" the card in this case.
509 +                        * It will be redetected on resume.
510 +                        */
511                         if (host->bus_ops->remove)
512                                 host->bus_ops->remove(host);
513 -
514                         mmc_claim_host(host);
515                         mmc_detach_bus(host);
516                         mmc_release_host(host);
517 +                       err = 0;
518                 }
519         }
520         mmc_bus_put(host);
521  
522 -       mmc_power_off(host);
523 +       if (!err)
524 +               mmc_power_off(host);
525  
526 -       return 0;
527 +       return err;
528  }
529  
530  EXPORT_SYMBOL(mmc_suspend_host);
531 @@ -1006,12 +1319,26 @@ EXPORT_SYMBOL(mmc_suspend_host);
532   */
533  int mmc_resume_host(struct mmc_host *host)
534  {
535 +       int err = 0;
536 +
537         mmc_bus_get(host);
538         if (host->bus_ops && !host->bus_dead) {
539                 mmc_power_up(host);
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);
544 +               if (err) {
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 */
554 +                       err = 0;
555 +               }
556         }
557         mmc_bus_put(host);
558  
559 @@ -1021,7 +1348,7 @@ int mmc_resume_host(struct mmc_host *hos
560          */
561         mmc_detect_change(host, 1);
562  
563 -       return 0;
564 +       return err;
565  }
566  
567  EXPORT_SYMBOL(mmc_resume_host);
568 --- a/drivers/mmc/core/core.h
569 +++ b/drivers/mmc/core/core.h
570 @@ -16,10 +16,14 @@
571  #define MMC_CMD_RETRIES        3
572  
573  struct mmc_bus_ops {
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 *);
584  };
585  
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
590         return 0;
591  }
592  
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);
605  
606         /*
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
610 @@ -14,5 +14,7 @@
611  int mmc_register_host_class(void);
612  void mmc_unregister_host_class(void);
613  
614 +void mmc_host_deeper_disable(struct work_struct *work);
615 +
616  #endif
617  
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
621  {
622         int err;
623         u8 *ext_csd;
624 -       unsigned int ext_csd_struct;
625  
626         BUG_ON(!card);
627  
628 @@ -207,16 +206,16 @@ static int mmc_read_ext_csd(struct mmc_c
629                 goto out;
630         }
631  
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),
638 -                       ext_csd_struct);
639 +                       card->ext_csd.rev);
640                 err = -EINVAL;
641                 goto out;
642         }
643  
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
650                 goto out;
651         }
652  
653 +       if (card->ext_csd.rev >= 3) {
654 +               u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
655 +
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];
660 +       }
661 +
662  out:
663         kfree(ext_csd);
664  
665 @@ -276,7 +284,7 @@ static struct attribute_group mmc_std_at
666         .attrs = mmc_std_attrs,
667  };
668  
669 -static struct attribute_group *mmc_attr_groups[] = {
670 +static const struct attribute_group *mmc_attr_groups[] = {
671         &mmc_std_attr_group,
672         NULL,
673  };
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);
678 -               if (err)
679 +               if (err && err != -EBADMSG)
680                         goto free_card;
681  
682 -               mmc_card_set_highspeed(card);
683 -
684 -               mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
685 +               if (err) {
686 +                       printk(KERN_WARNING "%s: switch to highspeed failed\n",
687 +                              mmc_hostname(card->host));
688 +                       err = 0;
689 +               } else {
690 +                       mmc_card_set_highspeed(card);
691 +                       mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
692 +               }
693         }
694  
695         /*
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);
699  
700 -               if (err)
701 +               if (err && err != -EBADMSG)
702                         goto free_card;
703  
704 -               mmc_set_bus_width(card->host, bus_width);
705 +               if (err) {
706 +                       printk(KERN_WARNING "%s: switch to bus width %d "
707 +                              "failed\n", mmc_hostname(card->host),
708 +                              1 << bus_width);
709 +                       err = 0;
710 +               } else {
711 +                       mmc_set_bus_width(card->host, bus_width);
712 +               }
713         }
714  
715         if (!oldcard)
716 @@ -507,12 +527,10 @@ static void mmc_detect(struct mmc_host *
717         }
718  }
719  
720 -#ifdef CONFIG_MMC_UNSAFE_RESUME
721 -
722  /*
723   * Suspend callback from host.
724   */
725 -static void mmc_suspend(struct mmc_host *host)
726 +static int mmc_suspend(struct mmc_host *host)
727  {
728         BUG_ON(!host);
729         BUG_ON(!host->card);
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);
734 +
735 +       return 0;
736  }
737  
738  /*
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.
742   */
743 -static void mmc_resume(struct mmc_host *host)
744 +static int mmc_resume(struct mmc_host *host)
745  {
746         int err;
747  
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);
751  
752 -       if (err) {
753 -               mmc_remove(host);
754 +       return err;
755 +}
756  
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)
761 +{
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);
766 +}
767 +
768 +static int mmc_sleep(struct mmc_host *host)
769 +{
770 +       struct mmc_card *card = host->card;
771 +       int err = -ENOSYS;
772 +
773 +       if (card && card->ext_csd.rev >= 3) {
774 +               err = mmc_card_sleepawake(host, 1);
775 +               if (err < 0)
776 +                       pr_debug("%s: Error %d while putting card into sleep",
777 +                                mmc_hostname(host), err);
778         }
779  
780 +       return err;
781  }
782  
783 -#else
784 +static int mmc_awake(struct mmc_host *host)
785 +{
786 +       struct mmc_card *card = host->card;
787 +       int err = -ENOSYS;
788  
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);
793 +               if (err < 0)
794 +                       pr_debug("%s: Error %d while awaking sleeping card",
795 +                                mmc_hostname(host), err);
796 +       }
797  
798 -#endif
799 +       return err;
800 +}
801 +
802 +#ifdef CONFIG_MMC_UNSAFE_RESUME
803 +
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,
812 +};
813 +
814 +static void mmc_attach_bus_ops(struct mmc_host *host)
815 +{
816 +       mmc_attach_bus(host, &mmc_ops);
817 +}
818 +
819 +#else
820  
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,
826 +       .suspend = NULL,
827 +       .resume = NULL,
828 +       .power_restore = mmc_power_restore,
829 +};
830 +
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,
839  };
840  
841 +static void mmc_attach_bus_ops(struct mmc_host *host)
842 +{
843 +       const struct mmc_bus_ops *bus_ops;
844 +
845 +       if (host->caps & MMC_CAP_NONREMOVABLE)
846 +               bus_ops = &mmc_ops_unsafe;
847 +       else
848 +               bus_ops = &mmc_ops;
849 +       mmc_attach_bus(host, bus_ops);
850 +}
851 +
852 +#endif
853 +
854  /*
855   * Starting point for MMC card init.
856   */
857 @@ -575,7 +664,7 @@ int mmc_attach_mmc(struct mmc_host *host
858         BUG_ON(!host);
859         WARN_ON(!host->claimed);
860  
861 -       mmc_attach_bus(host, &mmc_ops);
862 +       mmc_attach_bus_ops(host);
863  
864         /*
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);
870  }
871  
872 +int mmc_card_sleepawake(struct mmc_host *host, int sleep)
873 +{
874 +       struct mmc_command cmd;
875 +       struct mmc_card *card = host->card;
876 +       int err;
877 +
878 +       if (sleep)
879 +               mmc_deselect_cards(host);
880 +
881 +       memset(&cmd, 0, sizeof(struct mmc_command));
882 +
883 +       cmd.opcode = MMC_SLEEP_AWAKE;
884 +       cmd.arg = card->rca << 16;
885 +       if (sleep)
886 +               cmd.arg |= 1 << 15;
887 +
888 +       cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
889 +       err = mmc_wait_for_cmd(host, &cmd, 0);
890 +       if (err)
891 +               return err;
892 +
893 +       /*
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.
898 +        */
899 +       if (!(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
900 +               mmc_delay(DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000));
901 +
902 +       if (!sleep)
903 +               err = mmc_select_card(card);
904 +
905 +       return err;
906 +}
907 +
908  int mmc_go_idle(struct mmc_host *host)
909  {
910         int err;
911 @@ -354,6 +390,7 @@ int mmc_switch(struct mmc_card *card, u8
912  {
913         int err;
914         struct mmc_command cmd;
915 +       u32 status;
916  
917         BUG_ON(!card);
918         BUG_ON(!card->host);
919 @@ -371,6 +408,28 @@ int mmc_switch(struct mmc_card *card, u8
920         if (err)
921                 return err;
922  
923 +       /* Must check status to be sure of no errors */
924 +       do {
925 +               err = mmc_send_status(card, &status);
926 +               if (err)
927 +                       return err;
928 +               if (card->host->caps & MMC_CAP_WAIT_WHILE_BUSY)
929 +                       break;
930 +               if (mmc_host_is_spi(card->host))
931 +                       break;
932 +       } while (R1_CURRENT_STATE(status) == 7);
933 +
934 +       if (mmc_host_is_spi(card->host)) {
935 +               if (status & R1_SPI_ILLEGAL_COMMAND)
936 +                       return -EBADMSG;
937 +       } else {
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)
942 +                       return -EBADMSG;
943 +       }
944 +
945         return 0;
946  }
947  
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);
955  
956  #endif
957  
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,
962  };
963  
964 -static struct attribute_group *sd_attr_groups[] = {
965 +static const struct attribute_group *sd_attr_groups[] = {
966         &sd_std_attr_group,
967         NULL,
968  };
969 @@ -561,12 +561,10 @@ static void mmc_sd_detect(struct mmc_hos
970         }
971  }
972  
973 -#ifdef CONFIG_MMC_UNSAFE_RESUME
974 -
975  /*
976   * Suspend callback from host.
977   */
978 -static void mmc_sd_suspend(struct mmc_host *host)
979 +static int mmc_sd_suspend(struct mmc_host *host)
980  {
981         BUG_ON(!host);
982         BUG_ON(!host->card);
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);
987 +
988 +       return 0;
989  }
990  
991  /*
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.
995   */
996 -static void mmc_sd_resume(struct mmc_host *host)
997 +static int mmc_sd_resume(struct mmc_host *host)
998  {
999         int err;
1000  
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);
1004  
1005 -       if (err) {
1006 -               mmc_sd_remove(host);
1007 -
1008 -               mmc_claim_host(host);
1009 -               mmc_detach_bus(host);
1010 -               mmc_release_host(host);
1011 -       }
1012 +       return err;
1013 +}
1014  
1015 +static void mmc_sd_power_restore(struct mmc_host *host)
1016 +{
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);
1021  }
1022  
1023 -#else
1024 +#ifdef CONFIG_MMC_UNSAFE_RESUME
1025  
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,
1034 +};
1035  
1036 -#endif
1037 +static void mmc_sd_attach_bus_ops(struct mmc_host *host)
1038 +{
1039 +       mmc_attach_bus(host, &mmc_sd_ops);
1040 +}
1041 +
1042 +#else
1043  
1044  static const struct mmc_bus_ops mmc_sd_ops = {
1045         .remove = mmc_sd_remove,
1046         .detect = mmc_sd_detect,
1047 +       .suspend = NULL,
1048 +       .resume = NULL,
1049 +       .power_restore = mmc_sd_power_restore,
1050 +};
1051 +
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,
1058  };
1059  
1060 +static void mmc_sd_attach_bus_ops(struct mmc_host *host)
1061 +{
1062 +       const struct mmc_bus_ops *bus_ops;
1063 +
1064 +       if (host->caps & MMC_CAP_NONREMOVABLE)
1065 +               bus_ops = &mmc_sd_ops_unsafe;
1066 +       else
1067 +               bus_ops = &mmc_sd_ops;
1068 +       mmc_attach_bus(host, bus_ops);
1069 +}
1070 +
1071 +#endif
1072 +
1073  /*
1074   * Starting point for SD card init.
1075   */
1076 @@ -629,7 +662,7 @@ int mmc_attach_sd(struct mmc_host *host,
1077         BUG_ON(!host);
1078         WARN_ON(!host->claimed);
1079  
1080 -       mmc_attach_bus(host, &mmc_sd_ops);
1081 +       mmc_sd_attach_bus_ops(host);
1082  
1083         /*
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
1087 @@ -20,9 +20,6 @@
1088  #include "sdio_cis.h"
1089  #include "sdio_bus.h"
1090  
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)
1093 -
1094  /* show configuration fields */
1095  #define sdio_config_attr(field, format_string)                         \
1096  static ssize_t                                                         \
1097 @@ -251,12 +248,15 @@ int sdio_add_func(struct sdio_func *func
1098  /*
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).
1103   */
1104  void sdio_remove_func(struct sdio_func *func)
1105  {
1106 -       if (sdio_func_present(func))
1107 -               device_del(&func->dev);
1108 +       if (!sdio_func_present(func))
1109 +               return;
1110  
1111 +       device_del(&func->dev);
1112         put_device(&func->dev);
1113  }
1114  
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
1118  }
1119  
1120  /*
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.
1125 + */
1126 +static int sdio_disable_cd(struct mmc_card *card)
1127 +{
1128 +       int ret;
1129 +       u8 ctrl;
1130 +
1131 +       if (!card->cccr.disable_cd)
1132 +               return 0;
1133 +
1134 +       ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
1135 +       if (ret)
1136 +               return ret;
1137 +
1138 +       ctrl |= SDIO_BUS_CD_DISABLE;
1139 +
1140 +       return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
1141 +}
1142 +
1143 +/*
1144   * Test if the card supports high-speed mode and, if so, switch to it.
1145   */
1146  static int sdio_enable_hs(struct mmc_card *card)
1147 @@ -195,6 +218,135 @@ static int sdio_enable_hs(struct mmc_car
1148  }
1149  
1150  /*
1151 + * Handle the detection and initialisation of a card.
1152 + *
1153 + * In the case of a resume, "oldcard" will contain the card
1154 + * we're trying to reinitialise.
1155 + */
1156 +static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
1157 +                             struct mmc_card *oldcard)
1158 +{
1159 +       struct mmc_card *card;
1160 +       int err;
1161 +
1162 +       BUG_ON(!host);
1163 +       WARN_ON(!host->claimed);
1164 +
1165 +       /*
1166 +        * Inform the card of the voltage
1167 +        */
1168 +       err = mmc_send_io_op_cond(host, host->ocr, &ocr);
1169 +       if (err)
1170 +               goto err;
1171 +
1172 +       /*
1173 +        * For SPI, enable CRC as appropriate.
1174 +        */
1175 +       if (mmc_host_is_spi(host)) {
1176 +               err = mmc_spi_set_crc(host, use_spi_crc);
1177 +               if (err)
1178 +                       goto err;
1179 +       }
1180 +
1181 +       /*
1182 +        * Allocate card structure.
1183 +        */
1184 +       card = mmc_alloc_card(host, NULL);
1185 +       if (IS_ERR(card)) {
1186 +               err = PTR_ERR(card);
1187 +               goto err;
1188 +       }
1189 +
1190 +       card->type = MMC_TYPE_SDIO;
1191 +
1192 +       /*
1193 +        * For native busses:  set card RCA and quit open drain mode.
1194 +        */
1195 +       if (!mmc_host_is_spi(host)) {
1196 +               err = mmc_send_relative_addr(host, &card->rca);
1197 +               if (err)
1198 +                       goto remove;
1199 +
1200 +               mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1201 +       }
1202 +
1203 +       /*
1204 +        * Select card, as all following commands rely on that.
1205 +        */
1206 +       if (!mmc_host_is_spi(host)) {
1207 +               err = mmc_select_card(card);
1208 +               if (err)
1209 +                       goto remove;
1210 +       }
1211 +
1212 +       /*
1213 +        * Read the common registers.
1214 +        */
1215 +       err = sdio_read_cccr(card);
1216 +       if (err)
1217 +               goto remove;
1218 +
1219 +       /*
1220 +        * Read the common CIS tuples.
1221 +        */
1222 +       err = sdio_read_common_cis(card);
1223 +       if (err)
1224 +               goto remove;
1225 +
1226 +       if (oldcard) {
1227 +               int same = (card->cis.vendor == oldcard->cis.vendor &&
1228 +                           card->cis.device == oldcard->cis.device);
1229 +               mmc_remove_card(card);
1230 +               if (!same) {
1231 +                       err = -ENOENT;
1232 +                       goto err;
1233 +               }
1234 +               card = oldcard;
1235 +               return 0;
1236 +       }
1237 +
1238 +       /*
1239 +        * Switch to high-speed (if supported).
1240 +        */
1241 +       err = sdio_enable_hs(card);
1242 +       if (err)
1243 +               goto remove;
1244 +
1245 +       /*
1246 +        * Change to the card's maximum speed.
1247 +        */
1248 +       if (mmc_card_highspeed(card)) {
1249 +               /*
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
1253 +                * mandatory.
1254 +                */
1255 +               mmc_set_clock(host, 50000000);
1256 +       } else {
1257 +               mmc_set_clock(host, card->cis.max_dtr);
1258 +       }
1259 +
1260 +       /*
1261 +        * Switch to wider bus (if supported).
1262 +        */
1263 +       err = sdio_enable_wide(card);
1264 +       if (err)
1265 +               goto remove;
1266 +
1267 +       if (!oldcard)
1268 +               host->card = card;
1269 +       return 0;
1270 +
1271 +remove:
1272 +       if (!oldcard)
1273 +               mmc_remove_card(card);
1274 +
1275 +err:
1276 +       return err;
1277 +}
1278 +
1279 +/*
1280   * Host is being removed. Free up the current card.
1281   */
1282  static void mmc_sdio_remove(struct mmc_host *host)
1283 @@ -243,10 +395,77 @@ static void mmc_sdio_detect(struct mmc_h
1284         }
1285  }
1286  
1287 +/*
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.
1291 + */
1292 +static int mmc_sdio_suspend(struct mmc_host *host)
1293 +{
1294 +       int i, err = 0;
1295 +
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 */
1302 +                               err = -ENOSYS;
1303 +                       } else
1304 +                               err = pmops->suspend(&func->dev);
1305 +                       if (err)
1306 +                               break;
1307 +               }
1308 +       }
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);
1314 +               }
1315 +       }
1316 +
1317 +       return err;
1318 +}
1319 +
1320 +static int mmc_sdio_resume(struct mmc_host *host)
1321 +{
1322 +       int i, err;
1323 +
1324 +       BUG_ON(!host);
1325 +       BUG_ON(!host->card);
1326 +
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);
1331 +
1332 +       /*
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.
1341 +        */
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);
1347 +               }
1348 +       }
1349 +
1350 +       return err;
1351 +}
1352  
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,
1358  };
1359  
1360  
1361 @@ -275,13 +494,6 @@ int mmc_attach_sdio(struct mmc_host *hos
1362                 ocr &= ~0x7F;
1363         }
1364  
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;
1370 -       }
1371 -
1372         host->ocr = mmc_select_voltage(host, ocr);
1373  
1374         /*
1375 @@ -293,108 +505,31 @@ int mmc_attach_sdio(struct mmc_host *hos
1376         }
1377  
1378         /*
1379 -        * Inform the card of the voltage
1380 +        * Detect and init the card.
1381          */
1382 -       err = mmc_send_io_op_cond(host, host->ocr, &ocr);
1383 +       err = mmc_sdio_init_card(host, host->ocr, NULL);
1384         if (err)
1385                 goto err;
1386 -
1387 -       /*
1388 -        * For SPI, enable CRC as appropriate.
1389 -        */
1390 -       if (mmc_host_is_spi(host)) {
1391 -               err = mmc_spi_set_crc(host, use_spi_crc);
1392 -               if (err)
1393 -                       goto err;
1394 -       }
1395 +       card = host->card;
1396  
1397         /*
1398          * The number of functions on the card is encoded inside
1399          * the ocr.
1400          */
1401         funcs = (ocr & 0x70000000) >> 28;
1402 +       card->sdio_funcs = 0;
1403  
1404         /*
1405 -        * Allocate card structure.
1406 -        */
1407 -       card = mmc_alloc_card(host, NULL);
1408 -       if (IS_ERR(card)) {
1409 -               err = PTR_ERR(card);
1410 -               goto err;
1411 -       }
1412 -
1413 -       card->type = MMC_TYPE_SDIO;
1414 -       card->sdio_funcs = funcs;
1415 -
1416 -       host->card = card;
1417 -
1418 -       /*
1419 -        * For native busses:  set card RCA and quit open drain mode.
1420 +        * If needed, disconnect card detection pull-up resistor.
1421          */
1422 -       if (!mmc_host_is_spi(host)) {
1423 -               err = mmc_send_relative_addr(host, &card->rca);
1424 -               if (err)
1425 -                       goto remove;
1426 -
1427 -               mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1428 -       }
1429 -
1430 -       /*
1431 -        * Select card, as all following commands rely on that.
1432 -        */
1433 -       if (!mmc_host_is_spi(host)) {
1434 -               err = mmc_select_card(card);
1435 -               if (err)
1436 -                       goto remove;
1437 -       }
1438 -
1439 -       /*
1440 -        * Read the common registers.
1441 -        */
1442 -       err = sdio_read_cccr(card);
1443 -       if (err)
1444 -               goto remove;
1445 -
1446 -       /*
1447 -        * Read the common CIS tuples.
1448 -        */
1449 -       err = sdio_read_common_cis(card);
1450 -       if (err)
1451 -               goto remove;
1452 -
1453 -       /*
1454 -        * Switch to high-speed (if supported).
1455 -        */
1456 -       err = sdio_enable_hs(card);
1457 -       if (err)
1458 -               goto remove;
1459 -
1460 -       /*
1461 -        * Change to the card's maximum speed.
1462 -        */
1463 -       if (mmc_card_highspeed(card)) {
1464 -               /*
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
1468 -                * mandatory.
1469 -                */
1470 -               mmc_set_clock(host, 50000000);
1471 -       } else {
1472 -               mmc_set_clock(host, card->cis.max_dtr);
1473 -       }
1474 -
1475 -       /*
1476 -        * Switch to wider bus (if supported).
1477 -        */
1478 -       err = sdio_enable_wide(card);
1479 +       err = sdio_disable_cd(card);
1480         if (err)
1481                 goto remove;
1482  
1483         /*
1484          * Initialize (but don't add) all present functions.
1485          */
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);
1489                 if (err)
1490                         goto remove;
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;
1496  
1497 +       /* Find all null-terminated (including zero length) strings in
1498 +          the TPLLV1_INFO field. Trailing garbage is ignored. */
1499         buf += 2;
1500         size -= 2;
1501  
1502 @@ -39,11 +41,8 @@ static int cistpl_vers_1(struct mmc_card
1503                 if (buf[i] == 0)
1504                         nr_strings++;
1505         }
1506 -
1507 -       if (buf[i-1] != '\0') {
1508 -               printk(KERN_WARNING "SDIO: ignoring broken CISTPL_VERS_1\n");
1509 +       if (nr_strings == 0)
1510                 return 0;
1511 -       }
1512  
1513         size = i;
1514  
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 };
1518  
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 */
1522 +};
1523 +
1524 +static int cistpl_funce_whitelisted(unsigned char type)
1525 +{
1526 +       int i;
1527 +
1528 +       for (i = 0; i < ARRAY_SIZE(funce_type_whitelist); i++) {
1529 +               if (funce_type_whitelist[i] == type)
1530 +                       return 1;
1531 +       }
1532 +       return 0;
1533 +}
1534 +
1535  static int cistpl_funce_common(struct mmc_card *card,
1536                                const unsigned char *buf, unsigned size)
1537  {
1538 @@ -120,6 +135,10 @@ static int cistpl_funce_func(struct sdio
1539         unsigned vsn;
1540         unsigned min_size;
1541  
1542 +       /* let SDIO drivers take care of whitelisted FUNCE tuples */
1543 +       if (cistpl_funce_whitelisted(buf[0]))
1544 +               return -EILSEQ;
1545 +
1546         vsn = func->card->cccr.sdio_vsn;
1547         min_size = (vsn == SDIO_SDIO_REV_1_00) ? 28 : 42;
1548  
1549 @@ -154,13 +173,12 @@ static int cistpl_funce(struct mmc_card 
1550         else
1551                 ret = cistpl_funce_common(card, buf, size);
1552  
1553 -       if (ret) {
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]);
1557 -               return ret;
1558         }
1559  
1560 -       return 0;
1561 +       return ret;
1562  }
1563  
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)
1568                                 break;
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;
1574 -                       *prev = this;
1575 -                       prev = &this->next;
1576 -                       printk(KERN_DEBUG
1577 -                              "%s: queuing CIS tuple 0x%02x length %u\n",
1578 -                              mmc_hostname(card->host), tpl_code, tpl_link);
1579 -               } else {
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) {
1583                                 printk(KERN_ERR
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);
1589                                 ret = -EINVAL;
1590 @@ -275,7 +284,30 @@ static int sdio_read_cis(struct mmc_card
1591                                 ret = tpl->parse(card, func,
1592                                                  this->data, tpl_link);
1593                         }
1594 -                       kfree(this);
1595 +                       /*
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.
1599 +                        */
1600 +                       if (!ret || ret != -EILSEQ)
1601 +                               kfree(this);
1602 +               } else {
1603 +                       /* unknown tuple */
1604 +                       ret = -EILSEQ;
1605 +               }
1606 +
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;
1612 +                       *prev = this;
1613 +                       prev = &this->next;
1614 +                       printk(KERN_DEBUG
1615 +                              "%s: queuing CIS tuple 0x%02x length %u\n",
1616 +                              mmc_hostname(card->host), tpl_code, tpl_link);
1617 +                       /* keep on analyzing tuples */
1618 +                       ret = 0;
1619                 }
1620  
1621                 ptr += tpl_link;
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
1625  
1626         BUG_ON(!func);
1627  
1628 -       if (addr < 0xF0 || addr > 0xFF) {
1629 +       if ((addr < 0xF0 || addr > 0xFF) && (!mmc_card_lenient_fn0(func->card))) {
1630                 if (err_ret)
1631                         *err_ret = -EINVAL;
1632                 return;
1633 --- a/drivers/mmc/host/Kconfig
1634 +++ b/drivers/mmc/host/Kconfig
1635 @@ -55,6 +55,17 @@ config MMC_SDHCI_PCI
1636  
1637           If unsure, say N.
1638  
1639 +config MMC_SDHCI_CNS3XXX
1640 +       tristate "SDHCI support on CNS3XXX"
1641 +       depends on MMC_SDHCI && ARCH_CNS3XXX
1642 +       help
1643 +         This selects the Secure Digital Host Controller Interface (SDHCI)
1644 +         in Cavium Networks CNS3XXX SOCs.
1645 +
1646 +         If you have a controller with this interface, say Y or M here.
1647 +
1648 +         If unsure, say N.
1649 +
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
1665 @@ -27,6 +27,15 @@
1666  
1667  #define DRIVER_NAME "sdhci"
1668  
1669 +#define SDHCI_DEBUG
1670 +#undef SDHCI_DEBUG
1671 +
1672 +#ifdef SDHCI_DEBUG
1673 +#define sd_printk(x...)        printk(x)
1674 +#else
1675 +#define sd_printk(x...)        do { } while(0)
1676 +#endif
1677 +
1678  #define DBG(f, x...) \
1679         pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
1680  
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 *);
1684  
1685 +static void sdhci_dumpallregs(struct sdhci_host *host)
1686 +{
1687 +#ifdef SDHCI_DEBUG
1688 +       printk(" _______________________________________________\n");
1689 +
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));
1701 +//#else
1702 +       printk(KERN_DEBUG " _______________________________________________\n");
1703 +
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));
1715 +#endif
1716 +}
1717 +
1718  static void sdhci_dumpregs(struct sdhci_host *host)
1719  {
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;
1724  
1725 +       if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
1726 +               host->timeout_clk = host->clock / 1000;
1727 +
1728         /*
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)
1733  {
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;
1737  
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);
1743  
1744 -       if (host->flags & SDHCI_USE_DMA)
1745 +       if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
1746                 host->flags |= SDHCI_REQ_USE_DMA;
1747  
1748         /*
1749 @@ -736,11 +781,21 @@ static void sdhci_prepare_data(struct sd
1750                 } else {
1751                         int sg_cnt;
1752  
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) ?
1759                                                 DMA_FROM_DEVICE :
1760                                                 DMA_TO_DEVICE);
1761 +                       if (data->sg == NULL) {
1762 +                               sd_printk("[SDHCI_DEBUG] dma_map_sg(), data->sg = (NULL) \n");
1763 +                               return;
1764 +                       }
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);
1767 +
1768                         if (sg_cnt == 0) {
1769                                 /*
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;
1773                         } else {
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),
1777                                         SDHCI_DMA_ADDRESS);
1778                         }
1779 @@ -763,14 +819,32 @@ static void sdhci_prepare_data(struct sd
1780          */
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;
1785 +#else
1786 +               ctrl &= ~SDHCI_CTRL_LED;
1787 +#endif
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;
1792                 else
1793                         ctrl |= SDHCI_CTRL_SDMA;
1794 +
1795                 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1796 -       }
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;
1801 +#else
1802 +               ctrl &= ~SDHCI_CTRL_LED;
1803 +#endif
1804 +               ctrl &= ~SDHCI_CTRL_DMA_MASK;
1805 +               if (host->flags & SDHCI_REQ_USE_DMA)
1806 +                       ctrl |= SDHCI_CTRL_SDMA;
1807 +
1808 +               sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1809 +       }       
1810  
1811         if (!(host->flags & SDHCI_REQ_USE_DMA)) {
1812                 int flags;
1813 @@ -795,15 +869,26 @@ static void sdhci_set_transfer_mode(stru
1814         struct mmc_data *data)
1815  {
1816         u16 mode;
1817 +       u8 bgctrl;
1818  
1819         if (data == NULL)
1820                 return;
1821  
1822         WARN_ON(!host->data);
1823  
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);
1828 +
1829         mode = SDHCI_TRNS_BLK_CNT_EN;
1830 -       if (data->blocks > 1)
1831 +
1832 +       if (data->blocks > 1) {
1833                 mode |= SDHCI_TRNS_MULTI;
1834 +
1835 +               if (host->quirks & SDHCI_QUIRK_AUTO_CMD12)
1836 +                       mode |= SDHCI_TRNS_ACMD12;
1837 +       }
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);
1843  }
1844  
1845 +static void shdci_check_dma_overrun(struct sdhci_host *host, struct mmc_data *data)
1846 +{
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;
1850 +
1851 +       /* Test whether we ended up moving more data than was originally requested. */
1852 +       if (dma_pos <= dma_end)
1853 +               return;
1854 +
1855 +       printk(KERN_ERR "%s: dma overrun, dma %08x, req %08x..%08x\n",
1856 +              mmc_hostname(host->mmc), dma_pos, dma_start, dma_end);
1857 +}
1858 +
1859  static void sdhci_finish_data(struct sdhci_host *host)
1860  {
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);
1865                 else {
1866 +                       shdci_check_dma_overrun(host, data);
1867 +
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
1873  
1874         WARN_ON(host->cmd);
1875  
1876 +       sd_printk("[SDHCI_DEBUG] sdhci_send_command() \n");
1877 +
1878         /* Wait max 10 ms */
1879         timeout = 10;
1880  
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;
1886  
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
1890         if (cmd->data)
1891                 flags |= SDHCI_CMD_DATA;
1892  
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);
1898  }
1899  
1900  static void sdhci_finish_command(struct sdhci_host *host)
1901 @@ -934,6 +1044,8 @@ static void sdhci_finish_command(struct 
1902  
1903         BUG_ON(host->cmd == NULL);
1904  
1905 +       sd_printk("[SDHCI_DEBUG] sdhci_finish_command() \n");
1906 +
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);
1913  
1914 -       /* Wait max 10 ms */
1915 -       timeout = 10;
1916 +       /* Wait max 20 ms */
1917 +       timeout = 20;
1918         while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
1919                 & SDHCI_CLOCK_INT_STABLE)) {
1920                 if (timeout == 0) {
1921 @@ -1154,6 +1266,12 @@ static void sdhci_set_ios(struct mmc_hos
1922         else
1923                 ctrl &= ~SDHCI_CTRL_HISPD;
1924  
1925 +#ifdef SDHCI_USE_LEDS_CLASS
1926 +       ctrl |= SDHCI_CTRL_LED;
1927 +#else
1928 +       ctrl &= ~SDHCI_CTRL_LED;
1929 +#endif
1930 +
1931         sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1932  
1933         /*
1934 @@ -1321,7 +1439,11 @@ static void sdhci_timeout_timer(unsigned
1935         if (host->mrq) {
1936                 printk(KERN_ERR "%s: Timeout waiting for hardware "
1937                         "interrupt.\n", mmc_hostname(host->mmc));
1938 +#ifdef SDHCI_DEBUG
1939 +               sdhci_dumpallregs(host);
1940 +#else
1941                 sdhci_dumpregs(host);
1942 +#endif
1943  
1944                 if (host->data) {
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);
1949  
1950 +#ifdef SDHCI_DEBUG
1951 +       printk("*** %s got interrupt: 0x%08x\n", mmc_hostname(host->mmc), intmask);
1952 +#endif
1953 +
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 
1958  {
1959         int ret;
1960  
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);
1965         }
1966 @@ -1678,23 +1804,20 @@ int sdhci_add_host(struct sdhci_host *ho
1967         caps = sdhci_readl(host, SDHCI_CAPABILITIES);
1968  
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");
1976         else
1977 -               host->flags |= SDHCI_USE_DMA;
1978 +               host->flags |= SDHCI_USE_SDMA;
1979  
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;
1986         }
1987  
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;
1992 -       }
1993 +       if ((host->version >= SDHCI_SPEC_200) && (caps & SDHCI_CAN_DO_ADMA2))
1994 +               host->flags |= SDHCI_USE_ADMA;
1995  
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;
2000         }
2001  
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",
2008                                         mmc_hostname(mmc));
2009 -                               host->flags &= ~(SDHCI_USE_DMA | SDHCI_USE_ADMA);
2010 +                               host->flags &=
2011 +                                       ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
2012                         }
2013                 }
2014         }
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.
2018          */
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;
2023         }
2024 @@ -1757,13 +1881,15 @@ int sdhci_add_host(struct sdhci_host *ho
2025         host->timeout_clk =
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)) {
2033                         printk(KERN_ERR
2034                                "%s: Hardware doesn't specify timeout clock "
2035                                "frequency.\n", mmc_hostname(mmc));
2036                         return -ENODEV;
2037                 }
2038 -               host->timeout_clk = host->ops->get_timeout_clock(host);
2039         }
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.
2044          */
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);
2050         else
2051                 mmc->f_min = host->max_clk / 256;
2052 @@ -1810,7 +1937,7 @@ int sdhci_add_host(struct sdhci_host *ho
2053          */
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;
2059         else /* PIO */
2060                 mmc->max_hw_segs = 128;
2061 @@ -1893,10 +2020,10 @@ int sdhci_add_host(struct sdhci_host *ho
2062  
2063         mmc_add_host(mmc);
2064  
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");
2072  
2073         sdhci_enable_card_detection(host);
2074  
2075 --- /dev/null
2076 +++ b/drivers/mmc/host/sdhci-cns3xxx.c
2077 @@ -0,0 +1,313 @@
2078 +/*******************************************************************************
2079 + *
2080 + *  drivers/mmc/host/sdhci-cns3xxx.c
2081 + *
2082 + *  SDHCI support for the CNS3XXX SOCs
2083 + *
2084 + *  Author: Scott Shu
2085 + *
2086 + *  Copyright (c) 2008 Cavium Networks 
2087 + * 
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. 
2091 + *
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. 
2096 + *
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/. 
2101 + *
2102 + *  This file may also be available under a different license from Cavium. 
2103 + *  Contact Cavium Networks for more information
2104 + *
2105 + ******************************************************************************/
2106 +
2107 +#include <linux/delay.h>
2108 +#include <linux/highmem.h>
2109 +#include <linux/platform_device.h>
2110 +#include <linux/dma-mapping.h>
2111 +
2112 +#include <linux/mmc/host.h>
2113 +
2114 +#include <asm/scatterlist.h>
2115 +#include <asm/io.h>
2116 +#include <linux/interrupt.h>
2117 +
2118 +#include <mach/sdhci.h>
2119 +#include <mach/pm.h>
2120 +
2121 +#include "sdhci.h"
2122 +
2123 +//#define DEBUG
2124 +
2125 +#define MAX_BUS_CLK    (4)
2126 +
2127 +static unsigned __initdata use_dma = 0;
2128 +
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];
2136 +};
2137 +
2138 +static unsigned int sdhci_cns3xxx_get_max_clk(struct sdhci_host *host)
2139 +{
2140 +       int clk = 50000000;
2141 +
2142 +       return clk;
2143 +}
2144 +
2145 +static unsigned int sdhci_cns3xxx_get_timeout_clk(struct sdhci_host *host)
2146 +{
2147 +       return sdhci_cns3xxx_get_max_clk(host) / 100000;
2148 +}
2149 +
2150 +/*
2151 + * sdhci_cns3xxx_set_clock - callback on clock change
2152 + *
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.
2155 + */
2156 +static void sdhci_cns3xxx_set_clock(struct sdhci_host *host, unsigned int clock)
2157 +{
2158 +       u16 clk;
2159 +       unsigned long timeout;
2160 +
2161 +       if (clock == host->clock)
2162 +               return;
2163 +
2164 +       sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
2165 +
2166 +       if (clock == 0)
2167 +               goto out;
2168 +#if 1
2169 +       clk = 0x03 << SDHCI_DIVIDER_SHIFT;      /* base clock divided by 3 */
2170 +#else
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 */
2174 +       } else {
2175 +               clk = 0x02 << SDHCI_DIVIDER_SHIFT;      /* base clock divided by 4 */
2176 +       }
2177 +#endif
2178 +       clk |= SDHCI_CLOCK_INT_EN;
2179 +       sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
2180 +
2181 +       timeout = 10;
2182 +       while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
2183 +               & SDHCI_CLOCK_INT_STABLE)) {
2184 +               if (timeout == 0) {
2185 +                       return;
2186 +               }
2187 +               timeout--;
2188 +               mdelay(1);
2189 +       }
2190 +
2191 +       clk |= SDHCI_CLOCK_CARD_EN;
2192 +       sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
2193 +
2194 +       host->timeout_clk = sdhci_cns3xxx_get_timeout_clk(host);
2195 +out:
2196 +       host->clock = clock;
2197 +}
2198 +
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,
2203 +};
2204 +
2205 +static int __devinit sdhci_cns3xxx_probe(struct platform_device *pdev)
2206 +{
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;
2212 +       int ret, irq;
2213 +
2214 +       if (!pdata) {
2215 +               dev_err(dev, "no device data specified\n");
2216 +               return -ENOENT;
2217 +       }
2218 +
2219 +       irq = platform_get_irq(pdev, 0);
2220 +       if (irq < 0) {
2221 +               dev_err(dev, "no irq specified\n");
2222 +               return irq;
2223 +       }
2224 +
2225 +       res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2226 +       if (!res) {
2227 +               dev_err(dev, "no memory specified\n");
2228 +               return -ENOENT;
2229 +       }
2230 +
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);
2235 +       }
2236 +
2237 +       sc = sdhci_priv(host);
2238 +
2239 +       sc->host = host;
2240 +       sc->pdev = pdev;
2241 +       sc->pdata = pdata;
2242 +
2243 +       platform_set_drvdata(pdev, host);
2244 +
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");
2248 +               ret = -ENXIO;
2249 +               goto err_req_regs;
2250 +       }
2251 +
2252 +       host->ioaddr = ioremap_nocache(res->start, resource_size(res));
2253 +       if (!host->ioaddr) {
2254 +               dev_err(dev, "failed to map registers\n");
2255 +               ret = -ENXIO;
2256 +               goto err_req_regs;
2257 +       }
2258 +
2259 +       host->hw_name = "cns3xxx";
2260 +       host->ops = &sdhci_cns3xxx_ops;
2261 +       host->quirks = 0;
2262 +       host->irq = irq;
2263 +
2264 +       if (use_dma != 1) {
2265 +               host->quirks |= SDHCI_QUIRK_BROKEN_DMA;
2266 +               host->quirks |= SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK;
2267 +       } else {
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;
2277 +       }
2278 +
2279 +       //host->quirks |= SDHCI_QUIRK_INVERTED_WRITE_PROTECT;
2280 +
2281 +       host->quirks |= SDHCI_QUIRK_NONSTANDARD_CLOCK;
2282 +
2283 +       host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
2284 +
2285 +       ret = sdhci_add_host(host);
2286 +       if (ret) {
2287 +               dev_err(dev, "sdhci_add_host() failed (%d)\n", ret);
2288 +               goto err_add_host;
2289 +       }
2290 +
2291 +       return 0;
2292 +
2293 +err_add_host:
2294 +       free_irq(host->irq, host);
2295 +       iounmap(host->ioaddr);
2296 +       release_resource(sc->ioarea);
2297 +       kfree(sc->ioarea);
2298 +
2299 +err_req_regs:
2300 +       sdhci_free_host(host);
2301 +
2302 +       return ret;
2303 +}
2304 +
2305 +static int __devexit sdhci_cns3xxx_remove(struct platform_device *pdev)
2306 +{
2307 +       struct device *dev = &pdev->dev;
2308 +       struct sdhci_host *host = dev_get_drvdata(dev);
2309 +       struct resource *res;
2310 +
2311 +       pr_debug("%s: remove=%p\n", __func__, pdev);
2312 +
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));
2317 +
2318 +       return 0;
2319 +}
2320 +
2321 +#ifdef CONFIG_PM
2322 +
2323 +static int sdhci_cns3xxx_suspend(struct platform_device *dev, pm_message_t state)
2324 +{
2325 +
2326 +       return 0;
2327 +}
2328 +
2329 +static int sdhci_cns3xxx_resume(struct platform_device *dev)
2330 +{
2331 +
2332 +       return 0;
2333 +}
2334 +
2335 +#else
2336 +#define sdhci_cns3xxx_suspend  NULL
2337 +#define sdhci_cns3xxx_resume   NULL
2338 +#endif /* CONFIG_PM */
2339 +
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,
2345 +       .driver         = {
2346 +               .name   = "cns3xxx-sdhci",
2347 +               .owner  = THIS_MODULE,
2348 +       },
2349 +};
2350 +
2351 +static char banner[] __initdata = KERN_INFO "cns3xxx-sdhci, (c) 2009 Cavium Networks\n";
2352 +
2353 +static int __init sdhci_cns3xxx_init(void)
2354 +{
2355 +#ifdef CONFIG_SILICON
2356 +       unsigned long gpioapin = __raw_readl((void __iomem *)(CNS3XXX_MISC_BASE_VIRT + 0x0014));;
2357 +#else
2358 +       unsigned long status = __raw_readl((void __iomem *)(CNS3XXX_MISC_BASE_VIRT + 0x0700));
2359 +#endif
2360 +
2361 +       printk(banner);
2362 +
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));
2368 +#else
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));
2371 +#endif
2372 +       return platform_driver_register(&sdhci_cns3xxx_driver);
2373 +}
2374 +
2375 +static void __exit sdhci_cns3xxx_exit(void)
2376 +{
2377 +       platform_driver_unregister(&sdhci_cns3xxx_driver);
2378 +}
2379 +
2380 +module_init(sdhci_cns3xxx_init);
2381 +module_exit(sdhci_cns3xxx_exit);
2382 +
2383 +module_param(use_dma, uint, 0);
2384 +
2385 +MODULE_AUTHOR("Scott Shu");
2386 +MODULE_DESCRIPTION("Cavium Networks CNS3XXX SDHCI glue");
2387 +MODULE_LICENSE("GPL");
2388 +MODULE_ALIAS("platform:cns3xxx-sdhci");
2389 +
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
2393 @@ -8,6 +8,8 @@
2394   * the Free Software Foundation; either version 2 of the License, or (at
2395   * your option) any later version.
2396   */
2397 +#ifndef __SDHCI_H
2398 +#define __SDHCI_H
2399  
2400  #include <linux/scatterlist.h>
2401  #include <linux/compiler.h>
2402 @@ -78,6 +80,7 @@
2403  #define  SDHCI_POWER_330       0x0E
2404  
2405  #define SDHCI_BLOCK_GAP_CONTROL        0x2A
2406 +#define  SDHCI_READ_WAIT_CTRL  0x04
2407  
2408  #define SDHCI_WAKE_UP_CONTROL  0x2B
2409  
2410 @@ -143,7 +146,7 @@
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)
2429  
2430         int                     irq;            /* Device IRQ */
2431         void __iomem *          ioaddr;         /* Mapped address */
2432 @@ -250,7 +259,7 @@ struct sdhci_host {
2433         spinlock_t              lock;           /* Mutex */
2434  
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);
2444  #endif
2445 +
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 {
2450  };
2451  
2452  struct mmc_ext_csd {
2453 +       u8                      rev;
2454 +       unsigned int            sa_timeout;             /* Units: 100ns */
2455         unsigned int            hs_max_dtr;
2456         unsigned int            sectors;
2457  };
2458 @@ -62,7 +64,8 @@ struct sdio_cccr {
2459                                 low_speed:1,
2460                                 wide_bus:1,
2461                                 high_power:1,
2462 -                               high_speed:1;
2463 +                               high_speed:1,
2464 +                               disable_cd:1;
2465  };
2466  
2467  struct sdio_cis {
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 */
2474  
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)
2480  
2481 +static inline int mmc_card_lenient_fn0(const struct mmc_card *c)
2482 +{
2483 +       return c->quirks & MMC_QUIRK_LENIENT_FN0;
2484 +}
2485 +
2486  #define mmc_card_name(c)       ((c)->cid.prod_name)
2487  #define mmc_card_id(c)         (dev_name(&(c)->dev))
2488  
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(
2492  
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);
2496  
2497  /**
2498   *     mmc_claim_host - exclusively claim a host
2499 --- a/include/linux/mmc/host.h
2500 +++ b/include/linux/mmc/host.h
2501 @@ -11,6 +11,7 @@
2502  #define LINUX_MMC_HOST_H
2503  
2504  #include <linux/leds.h>
2505 +#include <linux/sched.h>
2506  
2507  #include <linux/mmc/core.h>
2508  
2509 @@ -51,6 +52,35 @@ struct mmc_ios {
2510  };
2511  
2512  struct mmc_host_ops {
2513 +       /*
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.
2524 +        *
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.
2530 +        *
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.
2535 +        *
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.
2539 +        */
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);
2543         /*
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 */
2552  
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 */
2557  #endif
2558  
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 */
2565 +
2566         struct mmc_card         *card;          /* device attached to this host */
2567  
2568         wait_queue_head_t       wq;
2569 +       struct task_struct      *claimer;       /* task that has host claimed */
2570 +       int                     claim_cnt;      /* "claim" nesting count */
2571  
2572         struct delayed_work     detect;
2573  
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 *);
2577  
2578 +extern void mmc_power_save_host(struct mmc_host *host);
2579 +extern void mmc_power_restore_host(struct mmc_host *host);
2580 +
2581  extern void mmc_detect_change(struct mmc_host *, unsigned long delay);
2582  extern void mmc_request_done(struct mmc_host *, struct mmc_request *);
2583  
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);
2587  
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);
2591 +
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);
2595 +
2596 +static inline void mmc_set_disable_delay(struct mmc_host *host,
2597 +                                        unsigned int disable_delay)
2598 +{
2599 +       host->disable_delay = disable_delay;
2600 +}
2601 +
2602  #endif
2603  
2604 --- a/include/linux/mmc/mmc.h
2605 +++ b/include/linux/mmc/mmc.h
2606 @@ -31,6 +31,7 @@
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  */
2614 @@ -127,6 +128,7 @@
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 */
2620  
2621  /*
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
2627  
2628  /*
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 {
2633  
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)
2637  
2638  /*
2639   * SDIO function device driver
2640 @@ -81,6 +82,8 @@ struct sdio_driver {
2641         struct device_driver drv;
2642  };
2643  
2644 +#define to_sdio_driver(d)      container_of(d, struct sdio_driver, drv)
2645 +
2646  /**
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
2651 @@ -22,6 +22,12 @@
2652  /*
2653   * Vendors and devices.  Sort key: vendor first, device next.
2654   */
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
2661  
2662  #define SDIO_VENDOR_ID_MARVELL                 0x02df
2663  #define SDIO_DEVICE_ID_MARVELL_LIBERTAS                0x9103