1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/interrupt.h>
16#include <linux/completion.h>
17#include <linux/device.h>
18#include <linux/delay.h>
19#include <linux/pagemap.h>
20#include <linux/err.h>
21#include <linux/leds.h>
22#include <linux/scatterlist.h>
23#include <linux/log2.h>
24#include <linux/regulator/consumer.h>
25
26#include <linux/mmc/card.h>
27#include <linux/mmc/host.h>
28#include <linux/mmc/mmc.h>
29#include <linux/mmc/sd.h>
30
31#include "core.h"
32#include "bus.h"
33#include "host.h"
34#include "sdio_bus.h"
35
36#include "mmc_ops.h"
37#include "sd_ops.h"
38#include "sdio_ops.h"
39
40static struct workqueue_struct *workqueue;
41
42
43
44
45
46
47int use_spi_crc = 1;
48module_param(use_spi_crc, bool, 0);
49
50
51
52
53static int mmc_schedule_delayed_work(struct delayed_work *work,
54 unsigned long delay)
55{
56 return queue_delayed_work(workqueue, work, delay);
57}
58
59
60
61
62static void mmc_flush_scheduled_work(void)
63{
64 flush_workqueue(workqueue);
65}
66
67
68
69
70
71
72
73
74
75void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
76{
77 struct mmc_command *cmd = mrq->cmd;
78 int err = cmd->error;
79
80 if (err && cmd->retries && mmc_host_is_spi(host)) {
81 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
82 cmd->retries = 0;
83 }
84
85 if (err && cmd->retries) {
86 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
87 mmc_hostname(host), cmd->opcode, err);
88
89 cmd->retries--;
90 cmd->error = 0;
91 host->ops->request(host, mrq);
92 } else {
93 led_trigger_event(host->led, LED_OFF);
94
95 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
96 mmc_hostname(host), cmd->opcode, err,
97 cmd->resp[0], cmd->resp[1],
98 cmd->resp[2], cmd->resp[3]);
99
100 if (mrq->data) {
101 pr_debug("%s: %d bytes transferred: %d\n",
102 mmc_hostname(host),
103 mrq->data->bytes_xfered, mrq->data->error);
104 }
105
106 if (mrq->stop) {
107 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
108 mmc_hostname(host), mrq->stop->opcode,
109 mrq->stop->error,
110 mrq->stop->resp[0], mrq->stop->resp[1],
111 mrq->stop->resp[2], mrq->stop->resp[3]);
112 }
113
114 if (mrq->done)
115 mrq->done(mrq);
116 }
117}
118
119EXPORT_SYMBOL(mmc_request_done);
120
121static void
122mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
123{
124#ifdef CONFIG_MMC_DEBUG
125 unsigned int i, sz;
126 struct scatterlist *sg;
127#endif
128
129 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
130 mmc_hostname(host), mrq->cmd->opcode,
131 mrq->cmd->arg, mrq->cmd->flags);
132
133 if (mrq->data) {
134 pr_debug("%s: blksz %d blocks %d flags %08x "
135 "tsac %d ms nsac %d\n",
136 mmc_hostname(host), mrq->data->blksz,
137 mrq->data->blocks, mrq->data->flags,
138 mrq->data->timeout_ns / 1000000,
139 mrq->data->timeout_clks);
140 }
141
142 if (mrq->stop) {
143 pr_debug("%s: CMD%u arg %08x flags %08x\n",
144 mmc_hostname(host), mrq->stop->opcode,
145 mrq->stop->arg, mrq->stop->flags);
146 }
147
148 WARN_ON(!host->claimed);
149
150 led_trigger_event(host->led, LED_FULL);
151
152 mrq->cmd->error = 0;
153 mrq->cmd->mrq = mrq;
154 if (mrq->data) {
155 BUG_ON(mrq->data->blksz > host->max_blk_size);
156 BUG_ON(mrq->data->blocks > host->max_blk_count);
157 BUG_ON(mrq->data->blocks * mrq->data->blksz >
158 host->max_req_size);
159
160#ifdef CONFIG_MMC_DEBUG
161 sz = 0;
162 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
163 sz += sg->length;
164 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
165#endif
166
167 mrq->cmd->data = mrq->data;
168 mrq->data->error = 0;
169 mrq->data->mrq = mrq;
170 if (mrq->stop) {
171 mrq->data->stop = mrq->stop;
172 mrq->stop->error = 0;
173 mrq->stop->mrq = mrq;
174 }
175 }
176 host->ops->request(host, mrq);
177}
178
179static void mmc_wait_done(struct mmc_request *mrq)
180{
181 complete(mrq->done_data);
182}
183
184
185
186
187
188
189
190
191
192
193void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
194{
195 DECLARE_COMPLETION_ONSTACK(complete);
196
197 mrq->done_data = &complete;
198 mrq->done = mmc_wait_done;
199
200 mmc_start_request(host, mrq);
201
202 wait_for_completion(&complete);
203}
204
205EXPORT_SYMBOL(mmc_wait_for_req);
206
207
208
209
210
211
212
213
214
215
216
217int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
218{
219 struct mmc_request mrq;
220
221 WARN_ON(!host->claimed);
222
223 memset(&mrq, 0, sizeof(struct mmc_request));
224
225 memset(cmd->resp, 0, sizeof(cmd->resp));
226 cmd->retries = retries;
227
228 mrq.cmd = cmd;
229 cmd->data = NULL;
230
231 mmc_wait_for_req(host, &mrq);
232
233 return cmd->error;
234}
235
236EXPORT_SYMBOL(mmc_wait_for_cmd);
237
238
239
240
241
242
243
244
245
246void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
247{
248 unsigned int mult;
249
250
251
252
253 if (mmc_card_sdio(card)) {
254 data->timeout_ns = 1000000000;
255 data->timeout_clks = 0;
256 return;
257 }
258
259
260
261
262 mult = mmc_card_sd(card) ? 100 : 10;
263
264
265
266
267
268 if (data->flags & MMC_DATA_WRITE)
269 mult <<= card->csd.r2w_factor;
270
271 data->timeout_ns = card->csd.tacc_ns * mult;
272 data->timeout_clks = card->csd.tacc_clks * mult;
273
274
275
276
277 if (mmc_card_sd(card)) {
278 unsigned int timeout_us, limit_us;
279
280 timeout_us = data->timeout_ns / 1000;
281 timeout_us += data->timeout_clks * 1000 /
282 (card->host->ios.clock / 1000);
283
284 if (data->flags & MMC_DATA_WRITE)
285
286
287
288
289 limit_us = 300000;
290 else
291 limit_us = 100000;
292
293
294
295
296 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
297 data->timeout_ns = limit_us * 1000;
298 data->timeout_clks = 0;
299 }
300 }
301
302
303
304
305
306
307 if (mmc_host_is_spi(card->host)) {
308 if (data->flags & MMC_DATA_WRITE) {
309 if (data->timeout_ns < 1000000000)
310 data->timeout_ns = 1000000000;
311 } else {
312 if (data->timeout_ns < 100000000)
313 data->timeout_ns = 100000000;
314 }
315 }
316}
317EXPORT_SYMBOL(mmc_set_data_timeout);
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
334{
335
336
337
338
339
340 sz = ((sz + 3) / 4) * 4;
341
342 return sz;
343}
344EXPORT_SYMBOL(mmc_align_data_size);
345
346
347
348
349
350
351
352
353
354int mmc_host_enable(struct mmc_host *host)
355{
356 if (!(host->caps & MMC_CAP_DISABLE))
357 return 0;
358
359 if (host->en_dis_recurs)
360 return 0;
361
362 if (host->nesting_cnt++)
363 return 0;
364
365 cancel_delayed_work_sync(&host->disable);
366
367 if (host->enabled)
368 return 0;
369
370 if (host->ops->enable) {
371 int err;
372
373 host->en_dis_recurs = 1;
374 err = host->ops->enable(host);
375 host->en_dis_recurs = 0;
376
377 if (err) {
378 pr_debug("%s: enable error %d\n",
379 mmc_hostname(host), err);
380 return err;
381 }
382 }
383 host->enabled = 1;
384 return 0;
385}
386EXPORT_SYMBOL(mmc_host_enable);
387
388static int mmc_host_do_disable(struct mmc_host *host, int lazy)
389{
390 if (host->ops->disable) {
391 int err;
392
393 host->en_dis_recurs = 1;
394 err = host->ops->disable(host, lazy);
395 host->en_dis_recurs = 0;
396
397 if (err < 0) {
398 pr_debug("%s: disable error %d\n",
399 mmc_hostname(host), err);
400 return err;
401 }
402 if (err > 0) {
403 unsigned long delay = msecs_to_jiffies(err);
404
405 mmc_schedule_delayed_work(&host->disable, delay);
406 }
407 }
408 host->enabled = 0;
409 return 0;
410}
411
412
413
414
415
416
417
418
419
420int mmc_host_disable(struct mmc_host *host)
421{
422 int err;
423
424 if (!(host->caps & MMC_CAP_DISABLE))
425 return 0;
426
427 if (host->en_dis_recurs)
428 return 0;
429
430 if (--host->nesting_cnt)
431 return 0;
432
433 if (!host->enabled)
434 return 0;
435
436 err = mmc_host_do_disable(host, 0);
437 return err;
438}
439EXPORT_SYMBOL(mmc_host_disable);
440
441
442
443
444
445
446
447
448
449
450
451int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
452{
453 DECLARE_WAITQUEUE(wait, current);
454 unsigned long flags;
455 int stop;
456
457 might_sleep();
458
459 add_wait_queue(&host->wq, &wait);
460 spin_lock_irqsave(&host->lock, flags);
461 while (1) {
462 set_current_state(TASK_UNINTERRUPTIBLE);
463 stop = abort ? atomic_read(abort) : 0;
464 if (stop || !host->claimed || host->claimer == current)
465 break;
466 spin_unlock_irqrestore(&host->lock, flags);
467 schedule();
468 spin_lock_irqsave(&host->lock, flags);
469 }
470 set_current_state(TASK_RUNNING);
471 if (!stop) {
472 host->claimed = 1;
473 host->claimer = current;
474 host->claim_cnt += 1;
475 } else
476 wake_up(&host->wq);
477 spin_unlock_irqrestore(&host->lock, flags);
478 remove_wait_queue(&host->wq, &wait);
479 if (!stop)
480 mmc_host_enable(host);
481 return stop;
482}
483
484EXPORT_SYMBOL(__mmc_claim_host);
485
486
487
488
489
490
491
492int mmc_try_claim_host(struct mmc_host *host)
493{
494 int claimed_host = 0;
495 unsigned long flags;
496
497 spin_lock_irqsave(&host->lock, flags);
498 if (!host->claimed || host->claimer == current) {
499 host->claimed = 1;
500 host->claimer = current;
501 host->claim_cnt += 1;
502 claimed_host = 1;
503 }
504 spin_unlock_irqrestore(&host->lock, flags);
505 return claimed_host;
506}
507EXPORT_SYMBOL(mmc_try_claim_host);
508
509static void mmc_do_release_host(struct mmc_host *host)
510{
511 unsigned long flags;
512
513 spin_lock_irqsave(&host->lock, flags);
514 if (--host->claim_cnt) {
515
516 spin_unlock_irqrestore(&host->lock, flags);
517 } else {
518 host->claimed = 0;
519 host->claimer = NULL;
520 spin_unlock_irqrestore(&host->lock, flags);
521 wake_up(&host->wq);
522 }
523}
524
525void mmc_host_deeper_disable(struct work_struct *work)
526{
527 struct mmc_host *host =
528 container_of(work, struct mmc_host, disable.work);
529
530
531 if (!mmc_try_claim_host(host))
532 return;
533 mmc_host_do_disable(host, 1);
534 mmc_do_release_host(host);
535}
536
537
538
539
540
541
542
543
544
545int mmc_host_lazy_disable(struct mmc_host *host)
546{
547 if (!(host->caps & MMC_CAP_DISABLE))
548 return 0;
549
550 if (host->en_dis_recurs)
551 return 0;
552
553 if (--host->nesting_cnt)
554 return 0;
555
556 if (!host->enabled)
557 return 0;
558
559 if (host->disable_delay) {
560 mmc_schedule_delayed_work(&host->disable,
561 msecs_to_jiffies(host->disable_delay));
562 return 0;
563 } else
564 return mmc_host_do_disable(host, 1);
565}
566EXPORT_SYMBOL(mmc_host_lazy_disable);
567
568
569
570
571
572
573
574
575void mmc_release_host(struct mmc_host *host)
576{
577 WARN_ON(!host->claimed);
578
579 mmc_host_lazy_disable(host);
580
581 mmc_do_release_host(host);
582}
583
584EXPORT_SYMBOL(mmc_release_host);
585
586
587
588
589
590static inline void mmc_set_ios(struct mmc_host *host)
591{
592 struct mmc_ios *ios = &host->ios;
593
594 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
595 "width %u timing %u\n",
596 mmc_hostname(host), ios->clock, ios->bus_mode,
597 ios->power_mode, ios->chip_select, ios->vdd,
598 ios->bus_width, ios->timing);
599
600 host->ops->set_ios(host, ios);
601}
602
603
604
605
606void mmc_set_chip_select(struct mmc_host *host, int mode)
607{
608 host->ios.chip_select = mode;
609 mmc_set_ios(host);
610}
611
612
613
614
615
616void mmc_set_clock(struct mmc_host *host, unsigned int hz)
617{
618 WARN_ON(hz < host->f_min);
619
620 if (hz > host->f_max)
621 hz = host->f_max;
622
623 host->ios.clock = hz;
624 mmc_set_ios(host);
625}
626
627
628
629
630void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
631{
632 host->ios.bus_mode = mode;
633 mmc_set_ios(host);
634}
635
636
637
638
639void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
640{
641 host->ios.bus_width = width;
642 mmc_set_ios(host);
643}
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
661{
662 const int max_bit = ilog2(MMC_VDD_35_36);
663 int bit;
664
665 if (vdd < 1650 || vdd > 3600)
666 return -EINVAL;
667
668 if (vdd >= 1650 && vdd <= 1950)
669 return ilog2(MMC_VDD_165_195);
670
671 if (low_bits)
672 vdd -= 1;
673
674
675 bit = (vdd - 2000) / 100 + 8;
676 if (bit > max_bit)
677 return max_bit;
678 return bit;
679}
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
695{
696 u32 mask = 0;
697
698 if (vdd_max < vdd_min)
699 return 0;
700
701
702 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
703 if (vdd_max < 0)
704 return 0;
705
706
707 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
708 if (vdd_min < 0)
709 return 0;
710
711
712 while (vdd_max >= vdd_min)
713 mask |= 1 << vdd_max--;
714
715 return mask;
716}
717EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
718
719#ifdef CONFIG_REGULATOR
720
721
722
723
724
725
726
727
728
729
730int mmc_regulator_get_ocrmask(struct regulator *supply)
731{
732 int result = 0;
733 int count;
734 int i;
735
736 count = regulator_count_voltages(supply);
737 if (count < 0)
738 return count;
739
740 for (i = 0; i < count; i++) {
741 int vdd_uV;
742 int vdd_mV;
743
744 vdd_uV = regulator_list_voltage(supply, i);
745 if (vdd_uV <= 0)
746 continue;
747
748 vdd_mV = vdd_uV / 1000;
749 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
750 }
751
752 return result;
753}
754EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
755
756
757
758
759
760
761
762
763
764
765
766
767int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit)
768{
769 int result = 0;
770 int min_uV, max_uV;
771 int enabled;
772
773 enabled = regulator_is_enabled(supply);
774 if (enabled < 0)
775 return enabled;
776
777 if (vdd_bit) {
778 int tmp;
779 int voltage;
780
781
782
783
784
785
786 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
787 if (tmp == 0) {
788 min_uV = 1650 * 1000;
789 max_uV = 1950 * 1000;
790 } else {
791 min_uV = 1900 * 1000 + tmp * 100 * 1000;
792 max_uV = min_uV + 100 * 1000;
793 }
794
795
796
797
798 voltage = regulator_get_voltage(supply);
799 if (voltage < 0)
800 result = voltage;
801 else if (voltage < min_uV || voltage > max_uV)
802 result = regulator_set_voltage(supply, min_uV, max_uV);
803 else
804 result = 0;
805
806 if (result == 0 && !enabled)
807 result = regulator_enable(supply);
808 } else if (enabled) {
809 result = regulator_disable(supply);
810 }
811
812 return result;
813}
814EXPORT_SYMBOL(mmc_regulator_set_ocr);
815
816#endif
817
818
819
820
821
822u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
823{
824 int bit;
825
826 ocr &= host->ocr_avail;
827
828 bit = ffs(ocr);
829 if (bit) {
830 bit -= 1;
831
832 ocr &= 3 << bit;
833
834 host->ios.vdd = bit;
835 mmc_set_ios(host);
836 } else {
837 pr_warning("%s: host doesn't support card's voltages\n",
838 mmc_hostname(host));
839 ocr = 0;
840 }
841
842 return ocr;
843}
844
845
846
847
848void mmc_set_timing(struct mmc_host *host, unsigned int timing)
849{
850 host->ios.timing = timing;
851 mmc_set_ios(host);
852}
853
854
855
856
857
858
859
860
861
862
863
864
865static void mmc_power_up(struct mmc_host *host)
866{
867 int bit;
868
869
870 if (host->ocr)
871 bit = ffs(host->ocr) - 1;
872 else
873 bit = fls(host->ocr_avail) - 1;
874
875 host->ios.vdd = bit;
876 if (mmc_host_is_spi(host)) {
877 host->ios.chip_select = MMC_CS_HIGH;
878 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
879 } else {
880 host->ios.chip_select = MMC_CS_DONTCARE;
881 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
882 }
883 host->ios.power_mode = MMC_POWER_UP;
884 host->ios.bus_width = MMC_BUS_WIDTH_1;
885 host->ios.timing = MMC_TIMING_LEGACY;
886 mmc_set_ios(host);
887
888
889
890
891
892 mmc_delay(10);
893
894 if (host->f_min > 400000) {
895 pr_warning("%s: Minimum clock frequency too high for "
896 "identification mode\n", mmc_hostname(host));
897 host->ios.clock = host->f_min;
898 } else
899 host->ios.clock = 400000;
900
901 host->ios.power_mode = MMC_POWER_ON;
902 mmc_set_ios(host);
903
904
905
906
907
908 mmc_delay(10);
909}
910
911static void mmc_power_off(struct mmc_host *host)
912{
913 host->ios.clock = 0;
914 host->ios.vdd = 0;
915 if (!mmc_host_is_spi(host)) {
916 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
917 host->ios.chip_select = MMC_CS_DONTCARE;
918 }
919 host->ios.power_mode = MMC_POWER_OFF;
920 host->ios.bus_width = MMC_BUS_WIDTH_1;
921 host->ios.timing = MMC_TIMING_LEGACY;
922 mmc_set_ios(host);
923}
924
925
926
927
928static void __mmc_release_bus(struct mmc_host *host)
929{
930 BUG_ON(!host);
931 BUG_ON(host->bus_refs);
932 BUG_ON(!host->bus_dead);
933
934 host->bus_ops = NULL;
935}
936
937
938
939
940static inline void mmc_bus_get(struct mmc_host *host)
941{
942 unsigned long flags;
943
944 spin_lock_irqsave(&host->lock, flags);
945 host->bus_refs++;
946 spin_unlock_irqrestore(&host->lock, flags);
947}
948
949
950
951
952
953static inline void mmc_bus_put(struct mmc_host *host)
954{
955 unsigned long flags;
956
957 spin_lock_irqsave(&host->lock, flags);
958 host->bus_refs--;
959 if ((host->bus_refs == 0) && host->bus_ops)
960 __mmc_release_bus(host);
961 spin_unlock_irqrestore(&host->lock, flags);
962}
963
964
965
966
967
968void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
969{
970 unsigned long flags;
971
972 BUG_ON(!host);
973 BUG_ON(!ops);
974
975 WARN_ON(!host->claimed);
976
977 spin_lock_irqsave(&host->lock, flags);
978
979 BUG_ON(host->bus_ops);
980 BUG_ON(host->bus_refs);
981
982 host->bus_ops = ops;
983 host->bus_refs = 1;
984 host->bus_dead = 0;
985
986 spin_unlock_irqrestore(&host->lock, flags);
987}
988
989
990
991
992
993void mmc_detach_bus(struct mmc_host *host)
994{
995 unsigned long flags;
996
997 BUG_ON(!host);
998
999 WARN_ON(!host->claimed);
1000 WARN_ON(!host->bus_ops);
1001
1002 spin_lock_irqsave(&host->lock, flags);
1003
1004 host->bus_dead = 1;
1005
1006 spin_unlock_irqrestore(&host->lock, flags);
1007
1008 mmc_power_off(host);
1009
1010 mmc_bus_put(host);
1011}
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1024{
1025#ifdef CONFIG_MMC_DEBUG
1026 unsigned long flags;
1027 spin_lock_irqsave(&host->lock, flags);
1028 WARN_ON(host->removed);
1029 spin_unlock_irqrestore(&host->lock, flags);
1030#endif
1031
1032 mmc_schedule_delayed_work(&host->detect, delay);
1033}
1034
1035EXPORT_SYMBOL(mmc_detect_change);
1036
1037
1038void mmc_rescan(struct work_struct *work)
1039{
1040 struct mmc_host *host =
1041 container_of(work, struct mmc_host, detect.work);
1042 u32 ocr;
1043 int err;
1044
1045 mmc_bus_get(host);
1046
1047
1048 if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead)
1049 host->bus_ops->detect(host);
1050
1051 mmc_bus_put(host);
1052
1053
1054 mmc_bus_get(host);
1055
1056
1057 if (host->bus_ops != NULL) {
1058 mmc_bus_put(host);
1059 goto out;
1060 }
1061
1062
1063
1064
1065
1066
1067
1068 mmc_bus_put(host);
1069
1070 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1071 goto out;
1072
1073 mmc_claim_host(host);
1074
1075 mmc_power_up(host);
1076 mmc_go_idle(host);
1077
1078 mmc_send_if_cond(host, host->ocr_avail);
1079
1080
1081
1082
1083 err = mmc_send_io_op_cond(host, 0, &ocr);
1084 if (!err) {
1085 if (mmc_attach_sdio(host, ocr))
1086 mmc_power_off(host);
1087 goto out;
1088 }
1089
1090
1091
1092
1093 err = mmc_send_app_op_cond(host, 0, &ocr);
1094 if (!err) {
1095 if (mmc_attach_sd(host, ocr))
1096 mmc_power_off(host);
1097 goto out;
1098 }
1099
1100
1101
1102
1103 err = mmc_send_op_cond(host, 0, &ocr);
1104 if (!err) {
1105 if (mmc_attach_mmc(host, ocr))
1106 mmc_power_off(host);
1107 goto out;
1108 }
1109
1110 mmc_release_host(host);
1111 mmc_power_off(host);
1112
1113out:
1114 if (host->caps & MMC_CAP_NEEDS_POLL)
1115 mmc_schedule_delayed_work(&host->detect, HZ);
1116}
1117
1118void mmc_start_host(struct mmc_host *host)
1119{
1120 mmc_power_off(host);
1121 mmc_detect_change(host, 0);
1122}
1123
1124void mmc_stop_host(struct mmc_host *host)
1125{
1126#ifdef CONFIG_MMC_DEBUG
1127 unsigned long flags;
1128 spin_lock_irqsave(&host->lock, flags);
1129 host->removed = 1;
1130 spin_unlock_irqrestore(&host->lock, flags);
1131#endif
1132
1133 if (host->caps & MMC_CAP_DISABLE)
1134 cancel_delayed_work(&host->disable);
1135 cancel_delayed_work(&host->detect);
1136 mmc_flush_scheduled_work();
1137
1138 mmc_bus_get(host);
1139 if (host->bus_ops && !host->bus_dead) {
1140 if (host->bus_ops->remove)
1141 host->bus_ops->remove(host);
1142
1143 mmc_claim_host(host);
1144 mmc_detach_bus(host);
1145 mmc_release_host(host);
1146 mmc_bus_put(host);
1147 return;
1148 }
1149 mmc_bus_put(host);
1150
1151 BUG_ON(host->card);
1152
1153 mmc_power_off(host);
1154}
1155
1156void mmc_power_save_host(struct mmc_host *host)
1157{
1158 mmc_bus_get(host);
1159
1160 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1161 mmc_bus_put(host);
1162 return;
1163 }
1164
1165 if (host->bus_ops->power_save)
1166 host->bus_ops->power_save(host);
1167
1168 mmc_bus_put(host);
1169
1170 mmc_power_off(host);
1171}
1172EXPORT_SYMBOL(mmc_power_save_host);
1173
1174void mmc_power_restore_host(struct mmc_host *host)
1175{
1176 mmc_bus_get(host);
1177
1178 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1179 mmc_bus_put(host);
1180 return;
1181 }
1182
1183 mmc_power_up(host);
1184 host->bus_ops->power_restore(host);
1185
1186 mmc_bus_put(host);
1187}
1188EXPORT_SYMBOL(mmc_power_restore_host);
1189
1190int mmc_card_awake(struct mmc_host *host)
1191{
1192 int err = -ENOSYS;
1193
1194 mmc_bus_get(host);
1195
1196 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1197 err = host->bus_ops->awake(host);
1198
1199 mmc_bus_put(host);
1200
1201 return err;
1202}
1203EXPORT_SYMBOL(mmc_card_awake);
1204
1205int mmc_card_sleep(struct mmc_host *host)
1206{
1207 int err = -ENOSYS;
1208
1209 mmc_bus_get(host);
1210
1211 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1212 err = host->bus_ops->sleep(host);
1213
1214 mmc_bus_put(host);
1215
1216 return err;
1217}
1218EXPORT_SYMBOL(mmc_card_sleep);
1219
1220int mmc_card_can_sleep(struct mmc_host *host)
1221{
1222 struct mmc_card *card = host->card;
1223
1224 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
1225 return 1;
1226 return 0;
1227}
1228EXPORT_SYMBOL(mmc_card_can_sleep);
1229
1230#ifdef CONFIG_PM
1231
1232
1233
1234
1235
1236
1237int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1238{
1239 int err = 0;
1240
1241 if (host->caps & MMC_CAP_DISABLE)
1242 cancel_delayed_work(&host->disable);
1243 cancel_delayed_work(&host->detect);
1244 mmc_flush_scheduled_work();
1245
1246 mmc_bus_get(host);
1247 if (host->bus_ops && !host->bus_dead) {
1248 if (host->bus_ops->suspend)
1249 err = host->bus_ops->suspend(host);
1250 if (err == -ENOSYS || !host->bus_ops->resume) {
1251
1252
1253
1254
1255 if (host->bus_ops->remove)
1256 host->bus_ops->remove(host);
1257 mmc_claim_host(host);
1258 mmc_detach_bus(host);
1259 mmc_release_host(host);
1260 err = 0;
1261 }
1262 }
1263 mmc_bus_put(host);
1264
1265 if (!err)
1266 mmc_power_off(host);
1267
1268 return err;
1269}
1270
1271EXPORT_SYMBOL(mmc_suspend_host);
1272
1273
1274
1275
1276
1277int mmc_resume_host(struct mmc_host *host)
1278{
1279 int err = 0;
1280
1281 mmc_bus_get(host);
1282 if (host->bus_ops && !host->bus_dead) {
1283 mmc_power_up(host);
1284 mmc_select_voltage(host, host->ocr);
1285 BUG_ON(!host->bus_ops->resume);
1286 err = host->bus_ops->resume(host);
1287 if (err) {
1288 printk(KERN_WARNING "%s: error %d during resume "
1289 "(card was removed?)\n",
1290 mmc_hostname(host), err);
1291 if (host->bus_ops->remove)
1292 host->bus_ops->remove(host);
1293 mmc_claim_host(host);
1294 mmc_detach_bus(host);
1295 mmc_release_host(host);
1296
1297 err = 0;
1298 }
1299 }
1300 mmc_bus_put(host);
1301
1302
1303
1304
1305
1306 mmc_detect_change(host, 1);
1307
1308 return err;
1309}
1310
1311EXPORT_SYMBOL(mmc_resume_host);
1312
1313#endif
1314
1315static int __init mmc_init(void)
1316{
1317 int ret;
1318
1319 workqueue = create_singlethread_workqueue("kmmcd");
1320 if (!workqueue)
1321 return -ENOMEM;
1322
1323 ret = mmc_register_bus();
1324 if (ret)
1325 goto destroy_workqueue;
1326
1327 ret = mmc_register_host_class();
1328 if (ret)
1329 goto unregister_bus;
1330
1331 ret = sdio_register_bus();
1332 if (ret)
1333 goto unregister_host_class;
1334
1335 return 0;
1336
1337unregister_host_class:
1338 mmc_unregister_host_class();
1339unregister_bus:
1340 mmc_unregister_bus();
1341destroy_workqueue:
1342 destroy_workqueue(workqueue);
1343
1344 return ret;
1345}
1346
1347static void __exit mmc_exit(void)
1348{
1349 sdio_unregister_bus();
1350 mmc_unregister_host_class();
1351 mmc_unregister_bus();
1352 destroy_workqueue(workqueue);
1353}
1354
1355subsys_initcall(mmc_init);
1356module_exit(mmc_exit);
1357
1358MODULE_LICENSE("GPL");
1359