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
354
355
356int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
357{
358 DECLARE_WAITQUEUE(wait, current);
359 unsigned long flags;
360 int stop;
361
362 might_sleep();
363
364 add_wait_queue(&host->wq, &wait);
365 spin_lock_irqsave(&host->lock, flags);
366 while (1) {
367 set_current_state(TASK_UNINTERRUPTIBLE);
368 stop = abort ? atomic_read(abort) : 0;
369 if (stop || !host->claimed)
370 break;
371 spin_unlock_irqrestore(&host->lock, flags);
372 schedule();
373 spin_lock_irqsave(&host->lock, flags);
374 }
375 set_current_state(TASK_RUNNING);
376 if (!stop)
377 host->claimed = 1;
378 else
379 wake_up(&host->wq);
380 spin_unlock_irqrestore(&host->lock, flags);
381 remove_wait_queue(&host->wq, &wait);
382 return stop;
383}
384
385EXPORT_SYMBOL(__mmc_claim_host);
386
387
388
389
390
391
392
393
394void mmc_release_host(struct mmc_host *host)
395{
396 unsigned long flags;
397
398 WARN_ON(!host->claimed);
399
400 spin_lock_irqsave(&host->lock, flags);
401 host->claimed = 0;
402 spin_unlock_irqrestore(&host->lock, flags);
403
404 wake_up(&host->wq);
405}
406
407EXPORT_SYMBOL(mmc_release_host);
408
409
410
411
412
413static inline void mmc_set_ios(struct mmc_host *host)
414{
415 struct mmc_ios *ios = &host->ios;
416
417 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
418 "width %u timing %u\n",
419 mmc_hostname(host), ios->clock, ios->bus_mode,
420 ios->power_mode, ios->chip_select, ios->vdd,
421 ios->bus_width, ios->timing);
422
423 host->ops->set_ios(host, ios);
424}
425
426
427
428
429void mmc_set_chip_select(struct mmc_host *host, int mode)
430{
431 host->ios.chip_select = mode;
432 mmc_set_ios(host);
433}
434
435
436
437
438
439void mmc_set_clock(struct mmc_host *host, unsigned int hz)
440{
441 WARN_ON(hz < host->f_min);
442
443 if (hz > host->f_max)
444 hz = host->f_max;
445
446 host->ios.clock = hz;
447 mmc_set_ios(host);
448}
449
450
451
452
453void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
454{
455 host->ios.bus_mode = mode;
456 mmc_set_ios(host);
457}
458
459
460
461
462void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
463{
464 host->ios.bus_width = width;
465 mmc_set_ios(host);
466}
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
484{
485 const int max_bit = ilog2(MMC_VDD_35_36);
486 int bit;
487
488 if (vdd < 1650 || vdd > 3600)
489 return -EINVAL;
490
491 if (vdd >= 1650 && vdd <= 1950)
492 return ilog2(MMC_VDD_165_195);
493
494 if (low_bits)
495 vdd -= 1;
496
497
498 bit = (vdd - 2000) / 100 + 8;
499 if (bit > max_bit)
500 return max_bit;
501 return bit;
502}
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
518{
519 u32 mask = 0;
520
521 if (vdd_max < vdd_min)
522 return 0;
523
524
525 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
526 if (vdd_max < 0)
527 return 0;
528
529
530 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
531 if (vdd_min < 0)
532 return 0;
533
534
535 while (vdd_max >= vdd_min)
536 mask |= 1 << vdd_max--;
537
538 return mask;
539}
540EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
541
542#ifdef CONFIG_REGULATOR
543
544
545
546
547
548
549
550
551
552
553int mmc_regulator_get_ocrmask(struct regulator *supply)
554{
555 int result = 0;
556 int count;
557 int i;
558
559 count = regulator_count_voltages(supply);
560 if (count < 0)
561 return count;
562
563 for (i = 0; i < count; i++) {
564 int vdd_uV;
565 int vdd_mV;
566
567 vdd_uV = regulator_list_voltage(supply, i);
568 if (vdd_uV <= 0)
569 continue;
570
571 vdd_mV = vdd_uV / 1000;
572 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
573 }
574
575 return result;
576}
577EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
578
579
580
581
582
583
584
585
586
587
588
589
590int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit)
591{
592 int result = 0;
593 int min_uV, max_uV;
594 int enabled;
595
596 enabled = regulator_is_enabled(supply);
597 if (enabled < 0)
598 return enabled;
599
600 if (vdd_bit) {
601 int tmp;
602 int voltage;
603
604
605
606
607
608
609 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
610 if (tmp == 0) {
611 min_uV = 1650 * 1000;
612 max_uV = 1950 * 1000;
613 } else {
614 min_uV = 1900 * 1000 + tmp * 100 * 1000;
615 max_uV = min_uV + 100 * 1000;
616 }
617
618
619
620
621 voltage = regulator_get_voltage(supply);
622 if (voltage < 0)
623 result = voltage;
624 else if (voltage < min_uV || voltage > max_uV)
625 result = regulator_set_voltage(supply, min_uV, max_uV);
626 else
627 result = 0;
628
629 if (result == 0 && !enabled)
630 result = regulator_enable(supply);
631 } else if (enabled) {
632 result = regulator_disable(supply);
633 }
634
635 return result;
636}
637EXPORT_SYMBOL(mmc_regulator_set_ocr);
638
639#endif
640
641
642
643
644
645u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
646{
647 int bit;
648
649 ocr &= host->ocr_avail;
650
651 bit = ffs(ocr);
652 if (bit) {
653 bit -= 1;
654
655 ocr &= 3 << bit;
656
657 host->ios.vdd = bit;
658 mmc_set_ios(host);
659 } else {
660 pr_warning("%s: host doesn't support card's voltages\n",
661 mmc_hostname(host));
662 ocr = 0;
663 }
664
665 return ocr;
666}
667
668
669
670
671void mmc_set_timing(struct mmc_host *host, unsigned int timing)
672{
673 host->ios.timing = timing;
674 mmc_set_ios(host);
675}
676
677
678
679
680
681
682
683
684
685
686
687
688static void mmc_power_up(struct mmc_host *host)
689{
690 int bit = fls(host->ocr_avail) - 1;
691
692 host->ios.vdd = bit;
693 if (mmc_host_is_spi(host)) {
694 host->ios.chip_select = MMC_CS_HIGH;
695 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
696 } else {
697 host->ios.chip_select = MMC_CS_DONTCARE;
698 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
699 }
700 host->ios.power_mode = MMC_POWER_UP;
701 host->ios.bus_width = MMC_BUS_WIDTH_1;
702 host->ios.timing = MMC_TIMING_LEGACY;
703 mmc_set_ios(host);
704
705
706
707
708
709 mmc_delay(10);
710
711 host->ios.clock = host->f_min;
712 host->ios.power_mode = MMC_POWER_ON;
713 mmc_set_ios(host);
714
715
716
717
718
719 mmc_delay(10);
720}
721
722static void mmc_power_off(struct mmc_host *host)
723{
724 host->ios.clock = 0;
725 host->ios.vdd = 0;
726 if (!mmc_host_is_spi(host)) {
727 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
728 host->ios.chip_select = MMC_CS_DONTCARE;
729 }
730 host->ios.power_mode = MMC_POWER_OFF;
731 host->ios.bus_width = MMC_BUS_WIDTH_1;
732 host->ios.timing = MMC_TIMING_LEGACY;
733 mmc_set_ios(host);
734}
735
736
737
738
739static void __mmc_release_bus(struct mmc_host *host)
740{
741 BUG_ON(!host);
742 BUG_ON(host->bus_refs);
743 BUG_ON(!host->bus_dead);
744
745 host->bus_ops = NULL;
746}
747
748
749
750
751static inline void mmc_bus_get(struct mmc_host *host)
752{
753 unsigned long flags;
754
755 spin_lock_irqsave(&host->lock, flags);
756 host->bus_refs++;
757 spin_unlock_irqrestore(&host->lock, flags);
758}
759
760
761
762
763
764static inline void mmc_bus_put(struct mmc_host *host)
765{
766 unsigned long flags;
767
768 spin_lock_irqsave(&host->lock, flags);
769 host->bus_refs--;
770 if ((host->bus_refs == 0) && host->bus_ops)
771 __mmc_release_bus(host);
772 spin_unlock_irqrestore(&host->lock, flags);
773}
774
775
776
777
778
779void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
780{
781 unsigned long flags;
782
783 BUG_ON(!host);
784 BUG_ON(!ops);
785
786 WARN_ON(!host->claimed);
787
788 spin_lock_irqsave(&host->lock, flags);
789
790 BUG_ON(host->bus_ops);
791 BUG_ON(host->bus_refs);
792
793 host->bus_ops = ops;
794 host->bus_refs = 1;
795 host->bus_dead = 0;
796
797 spin_unlock_irqrestore(&host->lock, flags);
798}
799
800
801
802
803
804void mmc_detach_bus(struct mmc_host *host)
805{
806 unsigned long flags;
807
808 BUG_ON(!host);
809
810 WARN_ON(!host->claimed);
811 WARN_ON(!host->bus_ops);
812
813 spin_lock_irqsave(&host->lock, flags);
814
815 host->bus_dead = 1;
816
817 spin_unlock_irqrestore(&host->lock, flags);
818
819 mmc_power_off(host);
820
821 mmc_bus_put(host);
822}
823
824
825
826
827
828
829
830
831
832
833
834void mmc_detect_change(struct mmc_host *host, unsigned long delay)
835{
836#ifdef CONFIG_MMC_DEBUG
837 unsigned long flags;
838 spin_lock_irqsave(&host->lock, flags);
839 WARN_ON(host->removed);
840 spin_unlock_irqrestore(&host->lock, flags);
841#endif
842
843 mmc_schedule_delayed_work(&host->detect, delay);
844}
845
846EXPORT_SYMBOL(mmc_detect_change);
847
848
849void mmc_rescan(struct work_struct *work)
850{
851 struct mmc_host *host =
852 container_of(work, struct mmc_host, detect.work);
853 u32 ocr;
854 int err;
855
856 mmc_bus_get(host);
857
858 if (host->bus_ops == NULL) {
859
860
861
862
863 mmc_bus_put(host);
864
865 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
866 goto out;
867
868 mmc_claim_host(host);
869
870 mmc_power_up(host);
871 mmc_go_idle(host);
872
873 mmc_send_if_cond(host, host->ocr_avail);
874
875
876
877
878 err = mmc_send_io_op_cond(host, 0, &ocr);
879 if (!err) {
880 if (mmc_attach_sdio(host, ocr))
881 mmc_power_off(host);
882 goto out;
883 }
884
885
886
887
888 err = mmc_send_app_op_cond(host, 0, &ocr);
889 if (!err) {
890 if (mmc_attach_sd(host, ocr))
891 mmc_power_off(host);
892 goto out;
893 }
894
895
896
897
898 err = mmc_send_op_cond(host, 0, &ocr);
899 if (!err) {
900 if (mmc_attach_mmc(host, ocr))
901 mmc_power_off(host);
902 goto out;
903 }
904
905 mmc_release_host(host);
906 mmc_power_off(host);
907 } else {
908 if (host->bus_ops->detect && !host->bus_dead)
909 host->bus_ops->detect(host);
910
911 mmc_bus_put(host);
912 }
913out:
914 if (host->caps & MMC_CAP_NEEDS_POLL)
915 mmc_schedule_delayed_work(&host->detect, HZ);
916}
917
918void mmc_start_host(struct mmc_host *host)
919{
920 mmc_power_off(host);
921 mmc_detect_change(host, 0);
922}
923
924void mmc_stop_host(struct mmc_host *host)
925{
926#ifdef CONFIG_MMC_DEBUG
927 unsigned long flags;
928 spin_lock_irqsave(&host->lock, flags);
929 host->removed = 1;
930 spin_unlock_irqrestore(&host->lock, flags);
931#endif
932
933 cancel_delayed_work(&host->detect);
934 mmc_flush_scheduled_work();
935
936 mmc_bus_get(host);
937 if (host->bus_ops && !host->bus_dead) {
938 if (host->bus_ops->remove)
939 host->bus_ops->remove(host);
940
941 mmc_claim_host(host);
942 mmc_detach_bus(host);
943 mmc_release_host(host);
944 }
945 mmc_bus_put(host);
946
947 BUG_ON(host->card);
948
949 mmc_power_off(host);
950}
951
952#ifdef CONFIG_PM
953
954
955
956
957
958
959int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
960{
961 cancel_delayed_work(&host->detect);
962 mmc_flush_scheduled_work();
963
964 mmc_bus_get(host);
965 if (host->bus_ops && !host->bus_dead) {
966 if (host->bus_ops->suspend)
967 host->bus_ops->suspend(host);
968 if (!host->bus_ops->resume) {
969 if (host->bus_ops->remove)
970 host->bus_ops->remove(host);
971
972 mmc_claim_host(host);
973 mmc_detach_bus(host);
974 mmc_release_host(host);
975 }
976 }
977 mmc_bus_put(host);
978
979 mmc_power_off(host);
980
981 return 0;
982}
983
984EXPORT_SYMBOL(mmc_suspend_host);
985
986
987
988
989
990int mmc_resume_host(struct mmc_host *host)
991{
992 mmc_bus_get(host);
993 if (host->bus_ops && !host->bus_dead) {
994 mmc_power_up(host);
995 mmc_select_voltage(host, host->ocr);
996 BUG_ON(!host->bus_ops->resume);
997 host->bus_ops->resume(host);
998 }
999 mmc_bus_put(host);
1000
1001
1002
1003
1004
1005 mmc_detect_change(host, 1);
1006
1007 return 0;
1008}
1009
1010EXPORT_SYMBOL(mmc_resume_host);
1011
1012#endif
1013
1014static int __init mmc_init(void)
1015{
1016 int ret;
1017
1018 workqueue = create_singlethread_workqueue("kmmcd");
1019 if (!workqueue)
1020 return -ENOMEM;
1021
1022 ret = mmc_register_bus();
1023 if (ret)
1024 goto destroy_workqueue;
1025
1026 ret = mmc_register_host_class();
1027 if (ret)
1028 goto unregister_bus;
1029
1030 ret = sdio_register_bus();
1031 if (ret)
1032 goto unregister_host_class;
1033
1034 return 0;
1035
1036unregister_host_class:
1037 mmc_unregister_host_class();
1038unregister_bus:
1039 mmc_unregister_bus();
1040destroy_workqueue:
1041 destroy_workqueue(workqueue);
1042
1043 return ret;
1044}
1045
1046static void __exit mmc_exit(void)
1047{
1048 sdio_unregister_bus();
1049 mmc_unregister_host_class();
1050 mmc_unregister_bus();
1051 destroy_workqueue(workqueue);
1052}
1053
1054subsys_initcall(mmc_init);
1055module_exit(mmc_exit);
1056
1057MODULE_LICENSE("GPL");
1058