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#include <linux/pm_runtime.h>
26#include <linux/suspend.h>
27#include <linux/fault-inject.h>
28#include <linux/random.h>
29#include <linux/slab.h>
30#include <linux/of.h>
31
32#include <linux/mmc/card.h>
33#include <linux/mmc/host.h>
34#include <linux/mmc/mmc.h>
35#include <linux/mmc/sd.h>
36
37#include "core.h"
38#include "bus.h"
39#include "host.h"
40#include "sdio_bus.h"
41
42#include "mmc_ops.h"
43#include "sd_ops.h"
44#include "sdio_ops.h"
45
46
47#define MMC_CORE_TIMEOUT_MS (10 * 60 * 1000)
48
49
50
51
52
53#define MMC_BKOPS_MAX_TIMEOUT (4 * 60 * 1000)
54
55static struct workqueue_struct *workqueue;
56static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
57
58
59
60
61
62
63bool use_spi_crc = 1;
64module_param(use_spi_crc, bool, 0);
65
66
67
68
69
70
71
72#ifdef CONFIG_MMC_UNSAFE_RESUME
73bool mmc_assume_removable;
74#else
75bool mmc_assume_removable = 1;
76#endif
77EXPORT_SYMBOL(mmc_assume_removable);
78module_param_named(removable, mmc_assume_removable, bool, 0644);
79MODULE_PARM_DESC(
80 removable,
81 "MMC/SD cards are removable and may be removed during suspend");
82
83
84
85
86static int mmc_schedule_delayed_work(struct delayed_work *work,
87 unsigned long delay)
88{
89 return queue_delayed_work(workqueue, work, delay);
90}
91
92
93
94
95static void mmc_flush_scheduled_work(void)
96{
97 flush_workqueue(workqueue);
98}
99
100#ifdef CONFIG_FAIL_MMC_REQUEST
101
102
103
104
105
106static void mmc_should_fail_request(struct mmc_host *host,
107 struct mmc_request *mrq)
108{
109 struct mmc_command *cmd = mrq->cmd;
110 struct mmc_data *data = mrq->data;
111 static const int data_errors[] = {
112 -ETIMEDOUT,
113 -EILSEQ,
114 -EIO,
115 };
116
117 if (!data)
118 return;
119
120 if (cmd->error || data->error ||
121 !should_fail(&host->fail_mmc_request, data->blksz * data->blocks))
122 return;
123
124 data->error = data_errors[prandom_u32() % ARRAY_SIZE(data_errors)];
125 data->bytes_xfered = (prandom_u32() % (data->bytes_xfered >> 9)) << 9;
126}
127
128#else
129
130static inline void mmc_should_fail_request(struct mmc_host *host,
131 struct mmc_request *mrq)
132{
133}
134
135#endif
136
137
138
139
140
141
142
143
144
145void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
146{
147 struct mmc_command *cmd = mrq->cmd;
148 int err = cmd->error;
149
150 if (err && cmd->retries && mmc_host_is_spi(host)) {
151 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
152 cmd->retries = 0;
153 }
154
155 if (err && cmd->retries && !mmc_card_removed(host->card)) {
156
157
158
159
160 if (mrq->done)
161 mrq->done(mrq);
162 } else {
163 mmc_should_fail_request(host, mrq);
164
165 led_trigger_event(host->led, LED_OFF);
166
167 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
168 mmc_hostname(host), cmd->opcode, err,
169 cmd->resp[0], cmd->resp[1],
170 cmd->resp[2], cmd->resp[3]);
171
172 if (mrq->data) {
173 pr_debug("%s: %d bytes transferred: %d\n",
174 mmc_hostname(host),
175 mrq->data->bytes_xfered, mrq->data->error);
176 }
177
178 if (mrq->stop) {
179 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
180 mmc_hostname(host), mrq->stop->opcode,
181 mrq->stop->error,
182 mrq->stop->resp[0], mrq->stop->resp[1],
183 mrq->stop->resp[2], mrq->stop->resp[3]);
184 }
185
186 if (mrq->done)
187 mrq->done(mrq);
188
189 mmc_host_clk_release(host);
190 }
191}
192
193EXPORT_SYMBOL(mmc_request_done);
194
195static void
196mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
197{
198#ifdef CONFIG_MMC_DEBUG
199 unsigned int i, sz;
200 struct scatterlist *sg;
201#endif
202
203 if (mrq->sbc) {
204 pr_debug("<%s: starting CMD%u arg %08x flags %08x>\n",
205 mmc_hostname(host), mrq->sbc->opcode,
206 mrq->sbc->arg, mrq->sbc->flags);
207 }
208
209 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
210 mmc_hostname(host), mrq->cmd->opcode,
211 mrq->cmd->arg, mrq->cmd->flags);
212
213 if (mrq->data) {
214 pr_debug("%s: blksz %d blocks %d flags %08x "
215 "tsac %d ms nsac %d\n",
216 mmc_hostname(host), mrq->data->blksz,
217 mrq->data->blocks, mrq->data->flags,
218 mrq->data->timeout_ns / 1000000,
219 mrq->data->timeout_clks);
220 }
221
222 if (mrq->stop) {
223 pr_debug("%s: CMD%u arg %08x flags %08x\n",
224 mmc_hostname(host), mrq->stop->opcode,
225 mrq->stop->arg, mrq->stop->flags);
226 }
227
228 WARN_ON(!host->claimed);
229
230 mrq->cmd->error = 0;
231 mrq->cmd->mrq = mrq;
232 if (mrq->data) {
233 BUG_ON(mrq->data->blksz > host->max_blk_size);
234 BUG_ON(mrq->data->blocks > host->max_blk_count);
235 BUG_ON(mrq->data->blocks * mrq->data->blksz >
236 host->max_req_size);
237
238#ifdef CONFIG_MMC_DEBUG
239 sz = 0;
240 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
241 sz += sg->length;
242 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
243#endif
244
245 mrq->cmd->data = mrq->data;
246 mrq->data->error = 0;
247 mrq->data->mrq = mrq;
248 if (mrq->stop) {
249 mrq->data->stop = mrq->stop;
250 mrq->stop->error = 0;
251 mrq->stop->mrq = mrq;
252 }
253 }
254 mmc_host_clk_hold(host);
255 led_trigger_event(host->led, LED_FULL);
256 host->ops->request(host, mrq);
257}
258
259
260
261
262
263
264
265
266
267
268
269void mmc_start_bkops(struct mmc_card *card, bool from_exception)
270{
271 int err;
272 int timeout;
273 bool use_busy_signal;
274
275 BUG_ON(!card);
276
277 if (!card->ext_csd.bkops_en || mmc_card_doing_bkops(card))
278 return;
279
280 err = mmc_read_bkops_status(card);
281 if (err) {
282 pr_err("%s: Failed to read bkops status: %d\n",
283 mmc_hostname(card->host), err);
284 return;
285 }
286
287 if (!card->ext_csd.raw_bkops_status)
288 return;
289
290 if (card->ext_csd.raw_bkops_status < EXT_CSD_BKOPS_LEVEL_2 &&
291 from_exception)
292 return;
293
294 mmc_claim_host(card->host);
295 if (card->ext_csd.raw_bkops_status >= EXT_CSD_BKOPS_LEVEL_2) {
296 timeout = MMC_BKOPS_MAX_TIMEOUT;
297 use_busy_signal = true;
298 } else {
299 timeout = 0;
300 use_busy_signal = false;
301 }
302
303 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
304 EXT_CSD_BKOPS_START, 1, timeout, use_busy_signal);
305 if (err) {
306 pr_warn("%s: Error %d starting bkops\n",
307 mmc_hostname(card->host), err);
308 goto out;
309 }
310
311
312
313
314
315
316 if (!use_busy_signal)
317 mmc_card_set_doing_bkops(card);
318out:
319 mmc_release_host(card->host);
320}
321EXPORT_SYMBOL(mmc_start_bkops);
322
323
324
325
326
327
328
329static void mmc_wait_data_done(struct mmc_request *mrq)
330{
331 mrq->host->context_info.is_done_rcv = true;
332 wake_up_interruptible(&mrq->host->context_info.wait);
333}
334
335static void mmc_wait_done(struct mmc_request *mrq)
336{
337 complete(&mrq->completion);
338}
339
340
341
342
343
344
345
346
347
348static int __mmc_start_data_req(struct mmc_host *host, struct mmc_request *mrq)
349{
350 mrq->done = mmc_wait_data_done;
351 mrq->host = host;
352 if (mmc_card_removed(host->card)) {
353 mrq->cmd->error = -ENOMEDIUM;
354 mmc_wait_data_done(mrq);
355 return -ENOMEDIUM;
356 }
357 mmc_start_request(host, mrq);
358
359 return 0;
360}
361
362static int __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq)
363{
364 init_completion(&mrq->completion);
365 mrq->done = mmc_wait_done;
366 if (mmc_card_removed(host->card)) {
367 mrq->cmd->error = -ENOMEDIUM;
368 complete(&mrq->completion);
369 return -ENOMEDIUM;
370 }
371 mmc_start_request(host, mrq);
372 return 0;
373}
374
375
376
377
378
379
380
381
382
383
384
385
386static int mmc_wait_for_data_req_done(struct mmc_host *host,
387 struct mmc_request *mrq,
388 struct mmc_async_req *next_req)
389{
390 struct mmc_command *cmd;
391 struct mmc_context_info *context_info = &host->context_info;
392 int err;
393 unsigned long flags;
394
395 while (1) {
396 wait_event_interruptible(context_info->wait,
397 (context_info->is_done_rcv ||
398 context_info->is_new_req));
399 spin_lock_irqsave(&context_info->lock, flags);
400 context_info->is_waiting_last_req = false;
401 spin_unlock_irqrestore(&context_info->lock, flags);
402 if (context_info->is_done_rcv) {
403 context_info->is_done_rcv = false;
404 context_info->is_new_req = false;
405 cmd = mrq->cmd;
406
407 if (!cmd->error || !cmd->retries ||
408 mmc_card_removed(host->card)) {
409 err = host->areq->err_check(host->card,
410 host->areq);
411 break;
412 } else {
413 pr_info("%s: req failed (CMD%u): %d, retrying...\n",
414 mmc_hostname(host),
415 cmd->opcode, cmd->error);
416 cmd->retries--;
417 cmd->error = 0;
418 host->ops->request(host, mrq);
419 continue;
420 }
421 } else if (context_info->is_new_req) {
422 context_info->is_new_req = false;
423 if (!next_req) {
424 err = MMC_BLK_NEW_REQUEST;
425 break;
426 }
427 }
428 }
429 return err;
430}
431
432static void mmc_wait_for_req_done(struct mmc_host *host,
433 struct mmc_request *mrq)
434{
435 struct mmc_command *cmd;
436
437 while (1) {
438 wait_for_completion(&mrq->completion);
439
440 cmd = mrq->cmd;
441
442
443
444
445
446
447
448 if (cmd->sanitize_busy && cmd->error == -ETIMEDOUT) {
449 if (!mmc_interrupt_hpi(host->card)) {
450 pr_warning("%s: %s: Interrupted sanitize\n",
451 mmc_hostname(host), __func__);
452 cmd->error = 0;
453 break;
454 } else {
455 pr_err("%s: %s: Failed to interrupt sanitize\n",
456 mmc_hostname(host), __func__);
457 }
458 }
459 if (!cmd->error || !cmd->retries ||
460 mmc_card_removed(host->card))
461 break;
462
463 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
464 mmc_hostname(host), cmd->opcode, cmd->error);
465 cmd->retries--;
466 cmd->error = 0;
467 host->ops->request(host, mrq);
468 }
469}
470
471
472
473
474
475
476
477
478
479
480
481
482static void mmc_pre_req(struct mmc_host *host, struct mmc_request *mrq,
483 bool is_first_req)
484{
485 if (host->ops->pre_req) {
486 mmc_host_clk_hold(host);
487 host->ops->pre_req(host, mrq, is_first_req);
488 mmc_host_clk_release(host);
489 }
490}
491
492
493
494
495
496
497
498
499
500
501static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq,
502 int err)
503{
504 if (host->ops->post_req) {
505 mmc_host_clk_hold(host);
506 host->ops->post_req(host, mrq, err);
507 mmc_host_clk_release(host);
508 }
509}
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527struct mmc_async_req *mmc_start_req(struct mmc_host *host,
528 struct mmc_async_req *areq, int *error)
529{
530 int err = 0;
531 int start_err = 0;
532 struct mmc_async_req *data = host->areq;
533
534
535 if (areq)
536 mmc_pre_req(host, areq->mrq, !host->areq);
537
538 if (host->areq) {
539 err = mmc_wait_for_data_req_done(host, host->areq->mrq, areq);
540 if (err == MMC_BLK_NEW_REQUEST) {
541 if (error)
542 *error = err;
543
544
545
546
547 return NULL;
548 }
549
550
551
552 if (host->card && mmc_card_mmc(host->card) &&
553 ((mmc_resp_type(host->areq->mrq->cmd) == MMC_RSP_R1) ||
554 (mmc_resp_type(host->areq->mrq->cmd) == MMC_RSP_R1B)) &&
555 (host->areq->mrq->cmd->resp[0] & R1_EXCEPTION_EVENT))
556 mmc_start_bkops(host->card, true);
557 }
558
559 if (!err && areq)
560 start_err = __mmc_start_data_req(host, areq->mrq);
561
562 if (host->areq)
563 mmc_post_req(host, host->areq->mrq, 0);
564
565
566 if ((err || start_err) && areq)
567 mmc_post_req(host, areq->mrq, -EINVAL);
568
569 if (err)
570 host->areq = NULL;
571 else
572 host->areq = areq;
573
574 if (error)
575 *error = err;
576 return data;
577}
578EXPORT_SYMBOL(mmc_start_req);
579
580
581
582
583
584
585
586
587
588
589void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
590{
591 __mmc_start_req(host, mrq);
592 mmc_wait_for_req_done(host, mrq);
593}
594EXPORT_SYMBOL(mmc_wait_for_req);
595
596
597
598
599
600
601
602
603int mmc_interrupt_hpi(struct mmc_card *card)
604{
605 int err;
606 u32 status;
607 unsigned long prg_wait;
608
609 BUG_ON(!card);
610
611 if (!card->ext_csd.hpi_en) {
612 pr_info("%s: HPI enable bit unset\n", mmc_hostname(card->host));
613 return 1;
614 }
615
616 mmc_claim_host(card->host);
617 err = mmc_send_status(card, &status);
618 if (err) {
619 pr_err("%s: Get card status fail\n", mmc_hostname(card->host));
620 goto out;
621 }
622
623 switch (R1_CURRENT_STATE(status)) {
624 case R1_STATE_IDLE:
625 case R1_STATE_READY:
626 case R1_STATE_STBY:
627 case R1_STATE_TRAN:
628
629
630
631
632 goto out;
633 case R1_STATE_PRG:
634 break;
635 default:
636
637 pr_debug("%s: HPI cannot be sent. Card state=%d\n",
638 mmc_hostname(card->host), R1_CURRENT_STATE(status));
639 err = -EINVAL;
640 goto out;
641 }
642
643 err = mmc_send_hpi_cmd(card, &status);
644 if (err)
645 goto out;
646
647 prg_wait = jiffies + msecs_to_jiffies(card->ext_csd.out_of_int_time);
648 do {
649 err = mmc_send_status(card, &status);
650
651 if (!err && R1_CURRENT_STATE(status) == R1_STATE_TRAN)
652 break;
653 if (time_after(jiffies, prg_wait))
654 err = -ETIMEDOUT;
655 } while (!err);
656
657out:
658 mmc_release_host(card->host);
659 return err;
660}
661EXPORT_SYMBOL(mmc_interrupt_hpi);
662
663
664
665
666
667
668
669
670
671
672
673int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
674{
675 struct mmc_request mrq = {NULL};
676
677 WARN_ON(!host->claimed);
678
679 memset(cmd->resp, 0, sizeof(cmd->resp));
680 cmd->retries = retries;
681
682 mrq.cmd = cmd;
683 cmd->data = NULL;
684
685 mmc_wait_for_req(host, &mrq);
686
687 return cmd->error;
688}
689
690EXPORT_SYMBOL(mmc_wait_for_cmd);
691
692
693
694
695
696
697
698
699
700
701int mmc_stop_bkops(struct mmc_card *card)
702{
703 int err = 0;
704
705 BUG_ON(!card);
706 err = mmc_interrupt_hpi(card);
707
708
709
710
711
712 if (!err || (err == -EINVAL)) {
713 mmc_card_clr_doing_bkops(card);
714 err = 0;
715 }
716
717 return err;
718}
719EXPORT_SYMBOL(mmc_stop_bkops);
720
721int mmc_read_bkops_status(struct mmc_card *card)
722{
723 int err;
724 u8 *ext_csd;
725
726
727
728
729 ext_csd = kmalloc(512, GFP_KERNEL);
730 if (!ext_csd) {
731 pr_err("%s: could not allocate buffer to receive the ext_csd.\n",
732 mmc_hostname(card->host));
733 return -ENOMEM;
734 }
735
736 mmc_claim_host(card->host);
737 err = mmc_send_ext_csd(card, ext_csd);
738 mmc_release_host(card->host);
739 if (err)
740 goto out;
741
742 card->ext_csd.raw_bkops_status = ext_csd[EXT_CSD_BKOPS_STATUS];
743 card->ext_csd.raw_exception_status = ext_csd[EXT_CSD_EXP_EVENTS_STATUS];
744out:
745 kfree(ext_csd);
746 return err;
747}
748EXPORT_SYMBOL(mmc_read_bkops_status);
749
750
751
752
753
754
755
756
757
758void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
759{
760 unsigned int mult;
761
762
763
764
765 if (mmc_card_sdio(card)) {
766 data->timeout_ns = 1000000000;
767 data->timeout_clks = 0;
768 return;
769 }
770
771
772
773
774 mult = mmc_card_sd(card) ? 100 : 10;
775
776
777
778
779
780 if (data->flags & MMC_DATA_WRITE)
781 mult <<= card->csd.r2w_factor;
782
783 data->timeout_ns = card->csd.tacc_ns * mult;
784 data->timeout_clks = card->csd.tacc_clks * mult;
785
786
787
788
789 if (mmc_card_sd(card)) {
790 unsigned int timeout_us, limit_us;
791
792 timeout_us = data->timeout_ns / 1000;
793 if (mmc_host_clk_rate(card->host))
794 timeout_us += data->timeout_clks * 1000 /
795 (mmc_host_clk_rate(card->host) / 1000);
796
797 if (data->flags & MMC_DATA_WRITE)
798
799
800
801
802
803
804
805
806 limit_us = 3000000;
807 else
808 limit_us = 100000;
809
810
811
812
813 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
814 data->timeout_ns = limit_us * 1000;
815 data->timeout_clks = 0;
816 }
817 }
818
819
820
821
822
823
824
825 if (mmc_card_long_read_time(card) && data->flags & MMC_DATA_READ) {
826 data->timeout_ns = 300000000;
827 data->timeout_clks = 0;
828 }
829
830
831
832
833
834
835
836 if (mmc_host_is_spi(card->host)) {
837 if (data->flags & MMC_DATA_WRITE) {
838 if (data->timeout_ns < 1000000000)
839 data->timeout_ns = 1000000000;
840 } else {
841 if (data->timeout_ns < 100000000)
842 data->timeout_ns = 100000000;
843 }
844 }
845}
846EXPORT_SYMBOL(mmc_set_data_timeout);
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
863{
864
865
866
867
868
869 sz = ((sz + 3) / 4) * 4;
870
871 return sz;
872}
873EXPORT_SYMBOL(mmc_align_data_size);
874
875
876
877
878
879
880
881
882
883
884
885int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
886{
887 DECLARE_WAITQUEUE(wait, current);
888 unsigned long flags;
889 int stop;
890
891 might_sleep();
892
893 add_wait_queue(&host->wq, &wait);
894 spin_lock_irqsave(&host->lock, flags);
895 while (1) {
896 set_current_state(TASK_UNINTERRUPTIBLE);
897 stop = abort ? atomic_read(abort) : 0;
898 if (stop || !host->claimed || host->claimer == current)
899 break;
900 spin_unlock_irqrestore(&host->lock, flags);
901 schedule();
902 spin_lock_irqsave(&host->lock, flags);
903 }
904 set_current_state(TASK_RUNNING);
905 if (!stop) {
906 host->claimed = 1;
907 host->claimer = current;
908 host->claim_cnt += 1;
909 } else
910 wake_up(&host->wq);
911 spin_unlock_irqrestore(&host->lock, flags);
912 remove_wait_queue(&host->wq, &wait);
913 if (host->ops->enable && !stop && host->claim_cnt == 1)
914 host->ops->enable(host);
915 return stop;
916}
917
918EXPORT_SYMBOL(__mmc_claim_host);
919
920
921
922
923
924
925
926int mmc_try_claim_host(struct mmc_host *host)
927{
928 int claimed_host = 0;
929 unsigned long flags;
930
931 spin_lock_irqsave(&host->lock, flags);
932 if (!host->claimed || host->claimer == current) {
933 host->claimed = 1;
934 host->claimer = current;
935 host->claim_cnt += 1;
936 claimed_host = 1;
937 }
938 spin_unlock_irqrestore(&host->lock, flags);
939 if (host->ops->enable && claimed_host && host->claim_cnt == 1)
940 host->ops->enable(host);
941 return claimed_host;
942}
943EXPORT_SYMBOL(mmc_try_claim_host);
944
945
946
947
948
949
950
951
952void mmc_release_host(struct mmc_host *host)
953{
954 unsigned long flags;
955
956 WARN_ON(!host->claimed);
957
958 if (host->ops->disable && host->claim_cnt == 1)
959 host->ops->disable(host);
960
961 spin_lock_irqsave(&host->lock, flags);
962 if (--host->claim_cnt) {
963
964 spin_unlock_irqrestore(&host->lock, flags);
965 } else {
966 host->claimed = 0;
967 host->claimer = NULL;
968 spin_unlock_irqrestore(&host->lock, flags);
969 wake_up(&host->wq);
970 }
971}
972EXPORT_SYMBOL(mmc_release_host);
973
974
975
976
977
978void mmc_get_card(struct mmc_card *card)
979{
980 pm_runtime_get_sync(&card->dev);
981 mmc_claim_host(card->host);
982}
983EXPORT_SYMBOL(mmc_get_card);
984
985
986
987
988
989void mmc_put_card(struct mmc_card *card)
990{
991 mmc_release_host(card->host);
992 pm_runtime_mark_last_busy(&card->dev);
993 pm_runtime_put_autosuspend(&card->dev);
994}
995EXPORT_SYMBOL(mmc_put_card);
996
997
998
999
1000
1001static inline void mmc_set_ios(struct mmc_host *host)
1002{
1003 struct mmc_ios *ios = &host->ios;
1004
1005 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
1006 "width %u timing %u\n",
1007 mmc_hostname(host), ios->clock, ios->bus_mode,
1008 ios->power_mode, ios->chip_select, ios->vdd,
1009 ios->bus_width, ios->timing);
1010
1011 if (ios->clock > 0)
1012 mmc_set_ungated(host);
1013 host->ops->set_ios(host, ios);
1014}
1015
1016
1017
1018
1019void mmc_set_chip_select(struct mmc_host *host, int mode)
1020{
1021 mmc_host_clk_hold(host);
1022 host->ios.chip_select = mode;
1023 mmc_set_ios(host);
1024 mmc_host_clk_release(host);
1025}
1026
1027
1028
1029
1030
1031static void __mmc_set_clock(struct mmc_host *host, unsigned int hz)
1032{
1033 WARN_ON(hz < host->f_min);
1034
1035 if (hz > host->f_max)
1036 hz = host->f_max;
1037
1038 host->ios.clock = hz;
1039 mmc_set_ios(host);
1040}
1041
1042void mmc_set_clock(struct mmc_host *host, unsigned int hz)
1043{
1044 mmc_host_clk_hold(host);
1045 __mmc_set_clock(host, hz);
1046 mmc_host_clk_release(host);
1047}
1048
1049#ifdef CONFIG_MMC_CLKGATE
1050
1051
1052
1053void mmc_gate_clock(struct mmc_host *host)
1054{
1055 unsigned long flags;
1056
1057 spin_lock_irqsave(&host->clk_lock, flags);
1058 host->clk_old = host->ios.clock;
1059 host->ios.clock = 0;
1060 host->clk_gated = true;
1061 spin_unlock_irqrestore(&host->clk_lock, flags);
1062 mmc_set_ios(host);
1063}
1064
1065
1066
1067
1068
1069void mmc_ungate_clock(struct mmc_host *host)
1070{
1071
1072
1073
1074
1075
1076
1077
1078 if (host->clk_old) {
1079 BUG_ON(host->ios.clock);
1080
1081 __mmc_set_clock(host, host->clk_old);
1082 }
1083}
1084
1085void mmc_set_ungated(struct mmc_host *host)
1086{
1087 unsigned long flags;
1088
1089
1090
1091
1092
1093 spin_lock_irqsave(&host->clk_lock, flags);
1094 host->clk_gated = false;
1095 spin_unlock_irqrestore(&host->clk_lock, flags);
1096}
1097
1098#else
1099void mmc_set_ungated(struct mmc_host *host)
1100{
1101}
1102#endif
1103
1104
1105
1106
1107void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
1108{
1109 mmc_host_clk_hold(host);
1110 host->ios.bus_mode = mode;
1111 mmc_set_ios(host);
1112 mmc_host_clk_release(host);
1113}
1114
1115
1116
1117
1118void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
1119{
1120 mmc_host_clk_hold(host);
1121 host->ios.bus_width = width;
1122 mmc_set_ios(host);
1123 mmc_host_clk_release(host);
1124}
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
1142{
1143 const int max_bit = ilog2(MMC_VDD_35_36);
1144 int bit;
1145
1146 if (vdd < 1650 || vdd > 3600)
1147 return -EINVAL;
1148
1149 if (vdd >= 1650 && vdd <= 1950)
1150 return ilog2(MMC_VDD_165_195);
1151
1152 if (low_bits)
1153 vdd -= 1;
1154
1155
1156 bit = (vdd - 2000) / 100 + 8;
1157 if (bit > max_bit)
1158 return max_bit;
1159 return bit;
1160}
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
1176{
1177 u32 mask = 0;
1178
1179 if (vdd_max < vdd_min)
1180 return 0;
1181
1182
1183 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
1184 if (vdd_max < 0)
1185 return 0;
1186
1187
1188 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
1189 if (vdd_min < 0)
1190 return 0;
1191
1192
1193 while (vdd_max >= vdd_min)
1194 mask |= 1 << vdd_max--;
1195
1196 return mask;
1197}
1198EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
1199
1200#ifdef CONFIG_OF
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210int mmc_of_parse_voltage(struct device_node *np, u32 *mask)
1211{
1212 const u32 *voltage_ranges;
1213 int num_ranges, i;
1214
1215 voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges);
1216 num_ranges = num_ranges / sizeof(*voltage_ranges) / 2;
1217 if (!voltage_ranges || !num_ranges) {
1218 pr_info("%s: voltage-ranges unspecified\n", np->full_name);
1219 return -EINVAL;
1220 }
1221
1222 for (i = 0; i < num_ranges; i++) {
1223 const int j = i * 2;
1224 u32 ocr_mask;
1225
1226 ocr_mask = mmc_vddrange_to_ocrmask(
1227 be32_to_cpu(voltage_ranges[j]),
1228 be32_to_cpu(voltage_ranges[j + 1]));
1229 if (!ocr_mask) {
1230 pr_err("%s: voltage-range #%d is invalid\n",
1231 np->full_name, i);
1232 return -EINVAL;
1233 }
1234 *mask |= ocr_mask;
1235 }
1236
1237 return 0;
1238}
1239EXPORT_SYMBOL(mmc_of_parse_voltage);
1240
1241#endif
1242
1243#ifdef CONFIG_REGULATOR
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254int mmc_regulator_get_ocrmask(struct regulator *supply)
1255{
1256 int result = 0;
1257 int count;
1258 int i;
1259
1260 count = regulator_count_voltages(supply);
1261 if (count < 0)
1262 return count;
1263
1264 for (i = 0; i < count; i++) {
1265 int vdd_uV;
1266 int vdd_mV;
1267
1268 vdd_uV = regulator_list_voltage(supply, i);
1269 if (vdd_uV <= 0)
1270 continue;
1271
1272 vdd_mV = vdd_uV / 1000;
1273 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
1274 }
1275
1276 return result;
1277}
1278EXPORT_SYMBOL_GPL(mmc_regulator_get_ocrmask);
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292int mmc_regulator_set_ocr(struct mmc_host *mmc,
1293 struct regulator *supply,
1294 unsigned short vdd_bit)
1295{
1296 int result = 0;
1297 int min_uV, max_uV;
1298
1299 if (vdd_bit) {
1300 int tmp;
1301 int voltage;
1302
1303
1304
1305
1306
1307
1308
1309 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
1310 if (tmp == 0) {
1311 min_uV = 1650 * 1000;
1312 max_uV = 1950 * 1000;
1313 } else {
1314 min_uV = 1900 * 1000 + tmp * 100 * 1000;
1315 max_uV = min_uV + 100 * 1000;
1316 }
1317
1318
1319
1320
1321
1322 voltage = regulator_get_voltage(supply);
1323
1324 if (!regulator_can_change_voltage(supply))
1325 min_uV = max_uV = voltage;
1326
1327 if (voltage < 0)
1328 result = voltage;
1329 else if (voltage < min_uV || voltage > max_uV)
1330 result = regulator_set_voltage(supply, min_uV, max_uV);
1331 else
1332 result = 0;
1333
1334 if (result == 0 && !mmc->regulator_enabled) {
1335 result = regulator_enable(supply);
1336 if (!result)
1337 mmc->regulator_enabled = true;
1338 }
1339 } else if (mmc->regulator_enabled) {
1340 result = regulator_disable(supply);
1341 if (result == 0)
1342 mmc->regulator_enabled = false;
1343 }
1344
1345 if (result)
1346 dev_err(mmc_dev(mmc),
1347 "could not set regulator OCR (%d)\n", result);
1348 return result;
1349}
1350EXPORT_SYMBOL_GPL(mmc_regulator_set_ocr);
1351
1352int mmc_regulator_get_supply(struct mmc_host *mmc)
1353{
1354 struct device *dev = mmc_dev(mmc);
1355 struct regulator *supply;
1356 int ret;
1357
1358 supply = devm_regulator_get(dev, "vmmc");
1359 mmc->supply.vmmc = supply;
1360 mmc->supply.vqmmc = devm_regulator_get_optional(dev, "vqmmc");
1361
1362 if (IS_ERR(supply))
1363 return PTR_ERR(supply);
1364
1365 ret = mmc_regulator_get_ocrmask(supply);
1366 if (ret > 0)
1367 mmc->ocr_avail = ret;
1368 else
1369 dev_warn(mmc_dev(mmc), "Failed getting OCR mask: %d\n", ret);
1370
1371 return 0;
1372}
1373EXPORT_SYMBOL_GPL(mmc_regulator_get_supply);
1374
1375#endif
1376
1377
1378
1379
1380
1381u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
1382{
1383 int bit;
1384
1385 ocr &= host->ocr_avail;
1386
1387 bit = ffs(ocr);
1388 if (bit) {
1389 bit -= 1;
1390
1391 ocr &= 3 << bit;
1392
1393 mmc_host_clk_hold(host);
1394 host->ios.vdd = bit;
1395 mmc_set_ios(host);
1396 mmc_host_clk_release(host);
1397 } else {
1398 pr_warning("%s: host doesn't support card's voltages\n",
1399 mmc_hostname(host));
1400 ocr = 0;
1401 }
1402
1403 return ocr;
1404}
1405
1406int __mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage)
1407{
1408 int err = 0;
1409 int old_signal_voltage = host->ios.signal_voltage;
1410
1411 host->ios.signal_voltage = signal_voltage;
1412 if (host->ops->start_signal_voltage_switch) {
1413 mmc_host_clk_hold(host);
1414 err = host->ops->start_signal_voltage_switch(host, &host->ios);
1415 mmc_host_clk_release(host);
1416 }
1417
1418 if (err)
1419 host->ios.signal_voltage = old_signal_voltage;
1420
1421 return err;
1422
1423}
1424
1425int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage)
1426{
1427 struct mmc_command cmd = {0};
1428 int err = 0;
1429 u32 clock;
1430
1431 BUG_ON(!host);
1432
1433
1434
1435
1436
1437 if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
1438 return __mmc_set_signal_voltage(host, signal_voltage);
1439
1440
1441
1442
1443
1444 if (!host->ops->start_signal_voltage_switch)
1445 return -EPERM;
1446 if (!host->ops->card_busy)
1447 pr_warning("%s: cannot verify signal voltage switch\n",
1448 mmc_hostname(host));
1449
1450 cmd.opcode = SD_SWITCH_VOLTAGE;
1451 cmd.arg = 0;
1452 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1453
1454 err = mmc_wait_for_cmd(host, &cmd, 0);
1455 if (err)
1456 return err;
1457
1458 if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
1459 return -EIO;
1460
1461 mmc_host_clk_hold(host);
1462
1463
1464
1465
1466 mmc_delay(1);
1467 if (host->ops->card_busy && !host->ops->card_busy(host)) {
1468 err = -EAGAIN;
1469 goto power_cycle;
1470 }
1471
1472
1473
1474
1475 clock = host->ios.clock;
1476 host->ios.clock = 0;
1477 mmc_set_ios(host);
1478
1479 if (__mmc_set_signal_voltage(host, signal_voltage)) {
1480
1481
1482
1483
1484 err = -EAGAIN;
1485 goto power_cycle;
1486 }
1487
1488
1489 mmc_delay(5);
1490 host->ios.clock = clock;
1491 mmc_set_ios(host);
1492
1493
1494 mmc_delay(1);
1495
1496
1497
1498
1499
1500 if (host->ops->card_busy && host->ops->card_busy(host))
1501 err = -EAGAIN;
1502
1503power_cycle:
1504 if (err) {
1505 pr_debug("%s: Signal voltage switch failed, "
1506 "power cycling card\n", mmc_hostname(host));
1507 mmc_power_cycle(host);
1508 }
1509
1510 mmc_host_clk_release(host);
1511
1512 return err;
1513}
1514
1515
1516
1517
1518void mmc_set_timing(struct mmc_host *host, unsigned int timing)
1519{
1520 mmc_host_clk_hold(host);
1521 host->ios.timing = timing;
1522 mmc_set_ios(host);
1523 mmc_host_clk_release(host);
1524}
1525
1526
1527
1528
1529void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
1530{
1531 mmc_host_clk_hold(host);
1532 host->ios.drv_type = drv_type;
1533 mmc_set_ios(host);
1534 mmc_host_clk_release(host);
1535}
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548void mmc_power_up(struct mmc_host *host)
1549{
1550 int bit;
1551
1552 if (host->ios.power_mode == MMC_POWER_ON)
1553 return;
1554
1555 mmc_host_clk_hold(host);
1556
1557
1558 if (host->ocr)
1559 bit = ffs(host->ocr) - 1;
1560 else
1561 bit = fls(host->ocr_avail) - 1;
1562
1563 host->ios.vdd = bit;
1564 if (mmc_host_is_spi(host))
1565 host->ios.chip_select = MMC_CS_HIGH;
1566 else
1567 host->ios.chip_select = MMC_CS_DONTCARE;
1568 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1569 host->ios.power_mode = MMC_POWER_UP;
1570 host->ios.bus_width = MMC_BUS_WIDTH_1;
1571 host->ios.timing = MMC_TIMING_LEGACY;
1572 mmc_set_ios(host);
1573
1574
1575 __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330);
1576
1577
1578
1579
1580
1581 mmc_delay(10);
1582
1583 host->ios.clock = host->f_init;
1584
1585 host->ios.power_mode = MMC_POWER_ON;
1586 mmc_set_ios(host);
1587
1588
1589
1590
1591
1592 mmc_delay(10);
1593
1594 mmc_host_clk_release(host);
1595}
1596
1597void mmc_power_off(struct mmc_host *host)
1598{
1599 if (host->ios.power_mode == MMC_POWER_OFF)
1600 return;
1601
1602 mmc_host_clk_hold(host);
1603
1604 host->ios.clock = 0;
1605 host->ios.vdd = 0;
1606
1607
1608
1609
1610
1611
1612 host->ocr = 1 << (fls(host->ocr_avail) - 1);
1613
1614 if (!mmc_host_is_spi(host)) {
1615 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1616 host->ios.chip_select = MMC_CS_DONTCARE;
1617 }
1618 host->ios.power_mode = MMC_POWER_OFF;
1619 host->ios.bus_width = MMC_BUS_WIDTH_1;
1620 host->ios.timing = MMC_TIMING_LEGACY;
1621 mmc_set_ios(host);
1622
1623
1624
1625
1626
1627
1628 mmc_delay(1);
1629
1630 mmc_host_clk_release(host);
1631}
1632
1633void mmc_power_cycle(struct mmc_host *host)
1634{
1635 mmc_power_off(host);
1636
1637 mmc_delay(1);
1638 mmc_power_up(host);
1639}
1640
1641
1642
1643
1644static void __mmc_release_bus(struct mmc_host *host)
1645{
1646 BUG_ON(!host);
1647 BUG_ON(host->bus_refs);
1648 BUG_ON(!host->bus_dead);
1649
1650 host->bus_ops = NULL;
1651}
1652
1653
1654
1655
1656static inline void mmc_bus_get(struct mmc_host *host)
1657{
1658 unsigned long flags;
1659
1660 spin_lock_irqsave(&host->lock, flags);
1661 host->bus_refs++;
1662 spin_unlock_irqrestore(&host->lock, flags);
1663}
1664
1665
1666
1667
1668
1669static inline void mmc_bus_put(struct mmc_host *host)
1670{
1671 unsigned long flags;
1672
1673 spin_lock_irqsave(&host->lock, flags);
1674 host->bus_refs--;
1675 if ((host->bus_refs == 0) && host->bus_ops)
1676 __mmc_release_bus(host);
1677 spin_unlock_irqrestore(&host->lock, flags);
1678}
1679
1680
1681
1682
1683
1684void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1685{
1686 unsigned long flags;
1687
1688 BUG_ON(!host);
1689 BUG_ON(!ops);
1690
1691 WARN_ON(!host->claimed);
1692
1693 spin_lock_irqsave(&host->lock, flags);
1694
1695 BUG_ON(host->bus_ops);
1696 BUG_ON(host->bus_refs);
1697
1698 host->bus_ops = ops;
1699 host->bus_refs = 1;
1700 host->bus_dead = 0;
1701
1702 spin_unlock_irqrestore(&host->lock, flags);
1703}
1704
1705
1706
1707
1708void mmc_detach_bus(struct mmc_host *host)
1709{
1710 unsigned long flags;
1711
1712 BUG_ON(!host);
1713
1714 WARN_ON(!host->claimed);
1715 WARN_ON(!host->bus_ops);
1716
1717 spin_lock_irqsave(&host->lock, flags);
1718
1719 host->bus_dead = 1;
1720
1721 spin_unlock_irqrestore(&host->lock, flags);
1722
1723 mmc_bus_put(host);
1724}
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1737{
1738#ifdef CONFIG_MMC_DEBUG
1739 unsigned long flags;
1740 spin_lock_irqsave(&host->lock, flags);
1741 WARN_ON(host->removed);
1742 spin_unlock_irqrestore(&host->lock, flags);
1743#endif
1744 host->detect_change = 1;
1745 mmc_schedule_delayed_work(&host->detect, delay);
1746}
1747
1748EXPORT_SYMBOL(mmc_detect_change);
1749
1750void mmc_init_erase(struct mmc_card *card)
1751{
1752 unsigned int sz;
1753
1754 if (is_power_of_2(card->erase_size))
1755 card->erase_shift = ffs(card->erase_size) - 1;
1756 else
1757 card->erase_shift = 0;
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774 if (mmc_card_sd(card) && card->ssr.au) {
1775 card->pref_erase = card->ssr.au;
1776 card->erase_shift = ffs(card->ssr.au) - 1;
1777 } else if (card->ext_csd.hc_erase_size) {
1778 card->pref_erase = card->ext_csd.hc_erase_size;
1779 } else {
1780 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1781 if (sz < 128)
1782 card->pref_erase = 512 * 1024 / 512;
1783 else if (sz < 512)
1784 card->pref_erase = 1024 * 1024 / 512;
1785 else if (sz < 1024)
1786 card->pref_erase = 2 * 1024 * 1024 / 512;
1787 else
1788 card->pref_erase = 4 * 1024 * 1024 / 512;
1789 if (card->pref_erase < card->erase_size)
1790 card->pref_erase = card->erase_size;
1791 else {
1792 sz = card->pref_erase % card->erase_size;
1793 if (sz)
1794 card->pref_erase += card->erase_size - sz;
1795 }
1796 }
1797}
1798
1799static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
1800 unsigned int arg, unsigned int qty)
1801{
1802 unsigned int erase_timeout;
1803
1804 if (arg == MMC_DISCARD_ARG ||
1805 (arg == MMC_TRIM_ARG && card->ext_csd.rev >= 6)) {
1806 erase_timeout = card->ext_csd.trim_timeout;
1807 } else if (card->ext_csd.erase_group_def & 1) {
1808
1809 if (arg == MMC_TRIM_ARG)
1810 erase_timeout = card->ext_csd.trim_timeout;
1811 else
1812 erase_timeout = card->ext_csd.hc_erase_timeout;
1813 } else {
1814
1815 unsigned int mult = (10 << card->csd.r2w_factor);
1816 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1817 unsigned int timeout_us;
1818
1819
1820 if (card->csd.tacc_ns < 1000000)
1821 timeout_us = (card->csd.tacc_ns * mult) / 1000;
1822 else
1823 timeout_us = (card->csd.tacc_ns / 1000) * mult;
1824
1825
1826
1827
1828
1829 timeout_clks <<= 1;
1830 timeout_us += (timeout_clks * 1000) /
1831 (mmc_host_clk_rate(card->host) / 1000);
1832
1833 erase_timeout = timeout_us / 1000;
1834
1835
1836
1837
1838
1839 if (!erase_timeout)
1840 erase_timeout = 1;
1841 }
1842
1843
1844 if (arg & MMC_SECURE_ARGS) {
1845 if (arg == MMC_SECURE_ERASE_ARG)
1846 erase_timeout *= card->ext_csd.sec_erase_mult;
1847 else
1848 erase_timeout *= card->ext_csd.sec_trim_mult;
1849 }
1850
1851 erase_timeout *= qty;
1852
1853
1854
1855
1856
1857 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1858 erase_timeout = 1000;
1859
1860 return erase_timeout;
1861}
1862
1863static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
1864 unsigned int arg,
1865 unsigned int qty)
1866{
1867 unsigned int erase_timeout;
1868
1869 if (card->ssr.erase_timeout) {
1870
1871 erase_timeout = card->ssr.erase_timeout * qty +
1872 card->ssr.erase_offset;
1873 } else {
1874
1875
1876
1877
1878 erase_timeout = 250 * qty;
1879 }
1880
1881
1882 if (erase_timeout < 1000)
1883 erase_timeout = 1000;
1884
1885 return erase_timeout;
1886}
1887
1888static unsigned int mmc_erase_timeout(struct mmc_card *card,
1889 unsigned int arg,
1890 unsigned int qty)
1891{
1892 if (mmc_card_sd(card))
1893 return mmc_sd_erase_timeout(card, arg, qty);
1894 else
1895 return mmc_mmc_erase_timeout(card, arg, qty);
1896}
1897
1898static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1899 unsigned int to, unsigned int arg)
1900{
1901 struct mmc_command cmd = {0};
1902 unsigned int qty = 0;
1903 unsigned long timeout;
1904 int err;
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922 if (card->erase_shift)
1923 qty += ((to >> card->erase_shift) -
1924 (from >> card->erase_shift)) + 1;
1925 else if (mmc_card_sd(card))
1926 qty += to - from + 1;
1927 else
1928 qty += ((to / card->erase_size) -
1929 (from / card->erase_size)) + 1;
1930
1931 if (!mmc_card_blockaddr(card)) {
1932 from <<= 9;
1933 to <<= 9;
1934 }
1935
1936 if (mmc_card_sd(card))
1937 cmd.opcode = SD_ERASE_WR_BLK_START;
1938 else
1939 cmd.opcode = MMC_ERASE_GROUP_START;
1940 cmd.arg = from;
1941 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1942 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1943 if (err) {
1944 pr_err("mmc_erase: group start error %d, "
1945 "status %#x\n", err, cmd.resp[0]);
1946 err = -EIO;
1947 goto out;
1948 }
1949
1950 memset(&cmd, 0, sizeof(struct mmc_command));
1951 if (mmc_card_sd(card))
1952 cmd.opcode = SD_ERASE_WR_BLK_END;
1953 else
1954 cmd.opcode = MMC_ERASE_GROUP_END;
1955 cmd.arg = to;
1956 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1957 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1958 if (err) {
1959 pr_err("mmc_erase: group end error %d, status %#x\n",
1960 err, cmd.resp[0]);
1961 err = -EIO;
1962 goto out;
1963 }
1964
1965 memset(&cmd, 0, sizeof(struct mmc_command));
1966 cmd.opcode = MMC_ERASE;
1967 cmd.arg = arg;
1968 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1969 cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty);
1970 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1971 if (err) {
1972 pr_err("mmc_erase: erase error %d, status %#x\n",
1973 err, cmd.resp[0]);
1974 err = -EIO;
1975 goto out;
1976 }
1977
1978 if (mmc_host_is_spi(card->host))
1979 goto out;
1980
1981 timeout = jiffies + msecs_to_jiffies(MMC_CORE_TIMEOUT_MS);
1982 do {
1983 memset(&cmd, 0, sizeof(struct mmc_command));
1984 cmd.opcode = MMC_SEND_STATUS;
1985 cmd.arg = card->rca << 16;
1986 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1987
1988 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1989 if (err || (cmd.resp[0] & 0xFDF92000)) {
1990 pr_err("error %d requesting status %#x\n",
1991 err, cmd.resp[0]);
1992 err = -EIO;
1993 goto out;
1994 }
1995
1996
1997
1998
1999 if (time_after(jiffies, timeout)) {
2000 pr_err("%s: Card stuck in programming state! %s\n",
2001 mmc_hostname(card->host), __func__);
2002 err = -EIO;
2003 goto out;
2004 }
2005
2006 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
2007 (R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG));
2008out:
2009 return err;
2010}
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
2022 unsigned int arg)
2023{
2024 unsigned int rem, to = from + nr;
2025
2026 if (!(card->host->caps & MMC_CAP_ERASE) ||
2027 !(card->csd.cmdclass & CCC_ERASE))
2028 return -EOPNOTSUPP;
2029
2030 if (!card->erase_size)
2031 return -EOPNOTSUPP;
2032
2033 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
2034 return -EOPNOTSUPP;
2035
2036 if ((arg & MMC_SECURE_ARGS) &&
2037 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
2038 return -EOPNOTSUPP;
2039
2040 if ((arg & MMC_TRIM_ARGS) &&
2041 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
2042 return -EOPNOTSUPP;
2043
2044 if (arg == MMC_SECURE_ERASE_ARG) {
2045 if (from % card->erase_size || nr % card->erase_size)
2046 return -EINVAL;
2047 }
2048
2049 if (arg == MMC_ERASE_ARG) {
2050 rem = from % card->erase_size;
2051 if (rem) {
2052 rem = card->erase_size - rem;
2053 from += rem;
2054 if (nr > rem)
2055 nr -= rem;
2056 else
2057 return 0;
2058 }
2059 rem = nr % card->erase_size;
2060 if (rem)
2061 nr -= rem;
2062 }
2063
2064 if (nr == 0)
2065 return 0;
2066
2067 to = from + nr;
2068
2069 if (to <= from)
2070 return -EINVAL;
2071
2072
2073 to -= 1;
2074
2075 return mmc_do_erase(card, from, to, arg);
2076}
2077EXPORT_SYMBOL(mmc_erase);
2078
2079int mmc_can_erase(struct mmc_card *card)
2080{
2081 if ((card->host->caps & MMC_CAP_ERASE) &&
2082 (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
2083 return 1;
2084 return 0;
2085}
2086EXPORT_SYMBOL(mmc_can_erase);
2087
2088int mmc_can_trim(struct mmc_card *card)
2089{
2090 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
2091 return 1;
2092 return 0;
2093}
2094EXPORT_SYMBOL(mmc_can_trim);
2095
2096int mmc_can_discard(struct mmc_card *card)
2097{
2098
2099
2100
2101
2102 if (card->ext_csd.feature_support & MMC_DISCARD_FEATURE)
2103 return 1;
2104 return 0;
2105}
2106EXPORT_SYMBOL(mmc_can_discard);
2107
2108int mmc_can_sanitize(struct mmc_card *card)
2109{
2110 if (!mmc_can_trim(card) && !mmc_can_erase(card))
2111 return 0;
2112 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_SANITIZE)
2113 return 1;
2114 return 0;
2115}
2116EXPORT_SYMBOL(mmc_can_sanitize);
2117
2118int mmc_can_secure_erase_trim(struct mmc_card *card)
2119{
2120 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
2121 return 1;
2122 return 0;
2123}
2124EXPORT_SYMBOL(mmc_can_secure_erase_trim);
2125
2126int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
2127 unsigned int nr)
2128{
2129 if (!card->erase_size)
2130 return 0;
2131 if (from % card->erase_size || nr % card->erase_size)
2132 return 0;
2133 return 1;
2134}
2135EXPORT_SYMBOL(mmc_erase_group_aligned);
2136
2137static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
2138 unsigned int arg)
2139{
2140 struct mmc_host *host = card->host;
2141 unsigned int max_discard, x, y, qty = 0, max_qty, timeout;
2142 unsigned int last_timeout = 0;
2143
2144 if (card->erase_shift)
2145 max_qty = UINT_MAX >> card->erase_shift;
2146 else if (mmc_card_sd(card))
2147 max_qty = UINT_MAX;
2148 else
2149 max_qty = UINT_MAX / card->erase_size;
2150
2151
2152 do {
2153 y = 0;
2154 for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
2155 timeout = mmc_erase_timeout(card, arg, qty + x);
2156 if (timeout > host->max_discard_to)
2157 break;
2158 if (timeout < last_timeout)
2159 break;
2160 last_timeout = timeout;
2161 y = x;
2162 }
2163 qty += y;
2164 } while (y);
2165
2166 if (!qty)
2167 return 0;
2168
2169 if (qty == 1)
2170 return 1;
2171
2172
2173 if (card->erase_shift)
2174 max_discard = --qty << card->erase_shift;
2175 else if (mmc_card_sd(card))
2176 max_discard = qty;
2177 else
2178 max_discard = --qty * card->erase_size;
2179
2180 return max_discard;
2181}
2182
2183unsigned int mmc_calc_max_discard(struct mmc_card *card)
2184{
2185 struct mmc_host *host = card->host;
2186 unsigned int max_discard, max_trim;
2187
2188 if (!host->max_discard_to)
2189 return UINT_MAX;
2190
2191
2192
2193
2194
2195
2196 if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
2197 return card->pref_erase;
2198
2199 max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
2200 if (mmc_can_trim(card)) {
2201 max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
2202 if (max_trim < max_discard)
2203 max_discard = max_trim;
2204 } else if (max_discard < card->erase_size) {
2205 max_discard = 0;
2206 }
2207 pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
2208 mmc_hostname(host), max_discard, host->max_discard_to);
2209 return max_discard;
2210}
2211EXPORT_SYMBOL(mmc_calc_max_discard);
2212
2213int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
2214{
2215 struct mmc_command cmd = {0};
2216
2217 if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
2218 return 0;
2219
2220 cmd.opcode = MMC_SET_BLOCKLEN;
2221 cmd.arg = blocklen;
2222 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
2223 return mmc_wait_for_cmd(card->host, &cmd, 5);
2224}
2225EXPORT_SYMBOL(mmc_set_blocklen);
2226
2227int mmc_set_blockcount(struct mmc_card *card, unsigned int blockcount,
2228 bool is_rel_write)
2229{
2230 struct mmc_command cmd = {0};
2231
2232 cmd.opcode = MMC_SET_BLOCK_COUNT;
2233 cmd.arg = blockcount & 0x0000FFFF;
2234 if (is_rel_write)
2235 cmd.arg |= 1 << 31;
2236 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
2237 return mmc_wait_for_cmd(card->host, &cmd, 5);
2238}
2239EXPORT_SYMBOL(mmc_set_blockcount);
2240
2241static void mmc_hw_reset_for_init(struct mmc_host *host)
2242{
2243 if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
2244 return;
2245 mmc_host_clk_hold(host);
2246 host->ops->hw_reset(host);
2247 mmc_host_clk_release(host);
2248}
2249
2250int mmc_can_reset(struct mmc_card *card)
2251{
2252 u8 rst_n_function;
2253
2254 if (!mmc_card_mmc(card))
2255 return 0;
2256 rst_n_function = card->ext_csd.rst_n_function;
2257 if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
2258 return 0;
2259 return 1;
2260}
2261EXPORT_SYMBOL(mmc_can_reset);
2262
2263static int mmc_do_hw_reset(struct mmc_host *host, int check)
2264{
2265 struct mmc_card *card = host->card;
2266
2267 if (!host->bus_ops->power_restore)
2268 return -EOPNOTSUPP;
2269
2270 if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
2271 return -EOPNOTSUPP;
2272
2273 if (!card)
2274 return -EINVAL;
2275
2276 if (!mmc_can_reset(card))
2277 return -EOPNOTSUPP;
2278
2279 mmc_host_clk_hold(host);
2280 mmc_set_clock(host, host->f_init);
2281
2282 host->ops->hw_reset(host);
2283
2284
2285 if (check) {
2286 struct mmc_command cmd = {0};
2287 int err;
2288
2289 cmd.opcode = MMC_SEND_STATUS;
2290 if (!mmc_host_is_spi(card->host))
2291 cmd.arg = card->rca << 16;
2292 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
2293 err = mmc_wait_for_cmd(card->host, &cmd, 0);
2294 if (!err) {
2295 mmc_host_clk_release(host);
2296 return -ENOSYS;
2297 }
2298 }
2299
2300 host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_DDR);
2301 if (mmc_host_is_spi(host)) {
2302 host->ios.chip_select = MMC_CS_HIGH;
2303 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
2304 } else {
2305 host->ios.chip_select = MMC_CS_DONTCARE;
2306 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
2307 }
2308 host->ios.bus_width = MMC_BUS_WIDTH_1;
2309 host->ios.timing = MMC_TIMING_LEGACY;
2310 mmc_set_ios(host);
2311
2312 mmc_host_clk_release(host);
2313
2314 return host->bus_ops->power_restore(host);
2315}
2316
2317int mmc_hw_reset(struct mmc_host *host)
2318{
2319 return mmc_do_hw_reset(host, 0);
2320}
2321EXPORT_SYMBOL(mmc_hw_reset);
2322
2323int mmc_hw_reset_check(struct mmc_host *host)
2324{
2325 return mmc_do_hw_reset(host, 1);
2326}
2327EXPORT_SYMBOL(mmc_hw_reset_check);
2328
2329static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
2330{
2331 host->f_init = freq;
2332
2333#ifdef CONFIG_MMC_DEBUG
2334 pr_info("%s: %s: trying to init card at %u Hz\n",
2335 mmc_hostname(host), __func__, host->f_init);
2336#endif
2337 mmc_power_up(host);
2338
2339
2340
2341
2342
2343 mmc_hw_reset_for_init(host);
2344
2345
2346
2347
2348
2349
2350 sdio_reset(host);
2351 mmc_go_idle(host);
2352
2353 mmc_send_if_cond(host, host->ocr_avail);
2354
2355
2356 if (!mmc_attach_sdio(host))
2357 return 0;
2358 if (!mmc_attach_sd(host))
2359 return 0;
2360 if (!mmc_attach_mmc(host))
2361 return 0;
2362
2363 mmc_power_off(host);
2364 return -EIO;
2365}
2366
2367int _mmc_detect_card_removed(struct mmc_host *host)
2368{
2369 int ret;
2370
2371 if ((host->caps & MMC_CAP_NONREMOVABLE) || !host->bus_ops->alive)
2372 return 0;
2373
2374 if (!host->card || mmc_card_removed(host->card))
2375 return 1;
2376
2377 ret = host->bus_ops->alive(host);
2378
2379
2380
2381
2382
2383
2384
2385
2386 if (!ret && host->ops->get_cd && !host->ops->get_cd(host)) {
2387 mmc_detect_change(host, msecs_to_jiffies(200));
2388 pr_debug("%s: card removed too slowly\n", mmc_hostname(host));
2389 }
2390
2391 if (ret) {
2392 mmc_card_set_removed(host->card);
2393 pr_debug("%s: card remove detected\n", mmc_hostname(host));
2394 }
2395
2396 return ret;
2397}
2398
2399int mmc_detect_card_removed(struct mmc_host *host)
2400{
2401 struct mmc_card *card = host->card;
2402 int ret;
2403
2404 WARN_ON(!host->claimed);
2405
2406 if (!card)
2407 return 1;
2408
2409 ret = mmc_card_removed(card);
2410
2411
2412
2413
2414 if (!host->detect_change && !(host->caps & MMC_CAP_NEEDS_POLL))
2415 return ret;
2416
2417 host->detect_change = 0;
2418 if (!ret) {
2419 ret = _mmc_detect_card_removed(host);
2420 if (ret && (host->caps & MMC_CAP_NEEDS_POLL)) {
2421
2422
2423
2424
2425 cancel_delayed_work(&host->detect);
2426 mmc_detect_change(host, 0);
2427 }
2428 }
2429
2430 return ret;
2431}
2432EXPORT_SYMBOL(mmc_detect_card_removed);
2433
2434void mmc_rescan(struct work_struct *work)
2435{
2436 struct mmc_host *host =
2437 container_of(work, struct mmc_host, detect.work);
2438 int i;
2439
2440 if (host->rescan_disable)
2441 return;
2442
2443
2444 if ((host->caps & MMC_CAP_NONREMOVABLE) && host->rescan_entered)
2445 return;
2446 host->rescan_entered = 1;
2447
2448 mmc_bus_get(host);
2449
2450
2451
2452
2453
2454 if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
2455 && !(host->caps & MMC_CAP_NONREMOVABLE))
2456 host->bus_ops->detect(host);
2457
2458 host->detect_change = 0;
2459
2460
2461
2462
2463
2464 mmc_bus_put(host);
2465 mmc_bus_get(host);
2466
2467
2468 if (host->bus_ops != NULL) {
2469 mmc_bus_put(host);
2470 goto out;
2471 }
2472
2473
2474
2475
2476
2477 mmc_bus_put(host);
2478
2479 if (host->ops->get_cd && host->ops->get_cd(host) == 0) {
2480 mmc_claim_host(host);
2481 mmc_power_off(host);
2482 mmc_release_host(host);
2483 goto out;
2484 }
2485
2486 mmc_claim_host(host);
2487 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
2488 if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
2489 break;
2490 if (freqs[i] <= host->f_min)
2491 break;
2492 }
2493 mmc_release_host(host);
2494
2495 out:
2496 if (host->caps & MMC_CAP_NEEDS_POLL)
2497 mmc_schedule_delayed_work(&host->detect, HZ);
2498}
2499
2500void mmc_start_host(struct mmc_host *host)
2501{
2502 host->f_init = max(freqs[0], host->f_min);
2503 host->rescan_disable = 0;
2504 if (host->caps2 & MMC_CAP2_NO_PRESCAN_POWERUP)
2505 mmc_power_off(host);
2506 else
2507 mmc_power_up(host);
2508 mmc_detect_change(host, 0);
2509}
2510
2511void mmc_stop_host(struct mmc_host *host)
2512{
2513#ifdef CONFIG_MMC_DEBUG
2514 unsigned long flags;
2515 spin_lock_irqsave(&host->lock, flags);
2516 host->removed = 1;
2517 spin_unlock_irqrestore(&host->lock, flags);
2518#endif
2519
2520 host->rescan_disable = 1;
2521 cancel_delayed_work_sync(&host->detect);
2522 mmc_flush_scheduled_work();
2523
2524
2525 host->pm_flags = 0;
2526
2527 mmc_bus_get(host);
2528 if (host->bus_ops && !host->bus_dead) {
2529
2530 host->bus_ops->remove(host);
2531 mmc_claim_host(host);
2532 mmc_detach_bus(host);
2533 mmc_power_off(host);
2534 mmc_release_host(host);
2535 mmc_bus_put(host);
2536 return;
2537 }
2538 mmc_bus_put(host);
2539
2540 BUG_ON(host->card);
2541
2542 mmc_power_off(host);
2543}
2544
2545int mmc_power_save_host(struct mmc_host *host)
2546{
2547 int ret = 0;
2548
2549#ifdef CONFIG_MMC_DEBUG
2550 pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__);
2551#endif
2552
2553 mmc_bus_get(host);
2554
2555 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
2556 mmc_bus_put(host);
2557 return -EINVAL;
2558 }
2559
2560 if (host->bus_ops->power_save)
2561 ret = host->bus_ops->power_save(host);
2562
2563 mmc_bus_put(host);
2564
2565 mmc_power_off(host);
2566
2567 return ret;
2568}
2569EXPORT_SYMBOL(mmc_power_save_host);
2570
2571int mmc_power_restore_host(struct mmc_host *host)
2572{
2573 int ret;
2574
2575#ifdef CONFIG_MMC_DEBUG
2576 pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__);
2577#endif
2578
2579 mmc_bus_get(host);
2580
2581 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
2582 mmc_bus_put(host);
2583 return -EINVAL;
2584 }
2585
2586 mmc_power_up(host);
2587 ret = host->bus_ops->power_restore(host);
2588
2589 mmc_bus_put(host);
2590
2591 return ret;
2592}
2593EXPORT_SYMBOL(mmc_power_restore_host);
2594
2595
2596
2597
2598int mmc_flush_cache(struct mmc_card *card)
2599{
2600 struct mmc_host *host = card->host;
2601 int err = 0;
2602
2603 if (!(host->caps2 & MMC_CAP2_CACHE_CTRL))
2604 return err;
2605
2606 if (mmc_card_mmc(card) &&
2607 (card->ext_csd.cache_size > 0) &&
2608 (card->ext_csd.cache_ctrl & 1)) {
2609 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2610 EXT_CSD_FLUSH_CACHE, 1, 0);
2611 if (err)
2612 pr_err("%s: cache flush error %d\n",
2613 mmc_hostname(card->host), err);
2614 }
2615
2616 return err;
2617}
2618EXPORT_SYMBOL(mmc_flush_cache);
2619
2620
2621
2622
2623
2624
2625
2626int mmc_cache_ctrl(struct mmc_host *host, u8 enable)
2627{
2628 struct mmc_card *card = host->card;
2629 unsigned int timeout;
2630 int err = 0;
2631
2632 if (!(host->caps2 & MMC_CAP2_CACHE_CTRL) ||
2633 mmc_card_is_removable(host))
2634 return err;
2635
2636 if (card && mmc_card_mmc(card) &&
2637 (card->ext_csd.cache_size > 0)) {
2638 enable = !!enable;
2639
2640 if (card->ext_csd.cache_ctrl ^ enable) {
2641 timeout = enable ? card->ext_csd.generic_cmd6_time : 0;
2642 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2643 EXT_CSD_CACHE_CTRL, enable, timeout);
2644 if (err)
2645 pr_err("%s: cache %s error %d\n",
2646 mmc_hostname(card->host),
2647 enable ? "on" : "off",
2648 err);
2649 else
2650 card->ext_csd.cache_ctrl = enable;
2651 }
2652 }
2653
2654 return err;
2655}
2656EXPORT_SYMBOL(mmc_cache_ctrl);
2657
2658#ifdef CONFIG_PM
2659
2660
2661
2662
2663
2664int mmc_suspend_host(struct mmc_host *host)
2665{
2666
2667 return 0;
2668}
2669EXPORT_SYMBOL(mmc_suspend_host);
2670
2671
2672
2673
2674
2675int mmc_resume_host(struct mmc_host *host)
2676{
2677
2678 return 0;
2679}
2680EXPORT_SYMBOL(mmc_resume_host);
2681
2682
2683
2684
2685
2686int mmc_pm_notify(struct notifier_block *notify_block,
2687 unsigned long mode, void *unused)
2688{
2689 struct mmc_host *host = container_of(
2690 notify_block, struct mmc_host, pm_notify);
2691 unsigned long flags;
2692 int err = 0;
2693
2694 switch (mode) {
2695 case PM_HIBERNATION_PREPARE:
2696 case PM_SUSPEND_PREPARE:
2697 spin_lock_irqsave(&host->lock, flags);
2698 host->rescan_disable = 1;
2699 spin_unlock_irqrestore(&host->lock, flags);
2700 cancel_delayed_work_sync(&host->detect);
2701
2702 if (!host->bus_ops)
2703 break;
2704
2705
2706 if (host->bus_ops->pre_suspend)
2707 err = host->bus_ops->pre_suspend(host);
2708 if (!err && host->bus_ops->suspend)
2709 break;
2710
2711
2712 host->bus_ops->remove(host);
2713 mmc_claim_host(host);
2714 mmc_detach_bus(host);
2715 mmc_power_off(host);
2716 mmc_release_host(host);
2717 host->pm_flags = 0;
2718 break;
2719
2720 case PM_POST_SUSPEND:
2721 case PM_POST_HIBERNATION:
2722 case PM_POST_RESTORE:
2723
2724 spin_lock_irqsave(&host->lock, flags);
2725 host->rescan_disable = 0;
2726 spin_unlock_irqrestore(&host->lock, flags);
2727 mmc_detect_change(host, 0);
2728
2729 }
2730
2731 return 0;
2732}
2733#endif
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743void mmc_init_context_info(struct mmc_host *host)
2744{
2745 spin_lock_init(&host->context_info.lock);
2746 host->context_info.is_new_req = false;
2747 host->context_info.is_done_rcv = false;
2748 host->context_info.is_waiting_last_req = false;
2749 init_waitqueue_head(&host->context_info.wait);
2750}
2751
2752static int __init mmc_init(void)
2753{
2754 int ret;
2755
2756 workqueue = alloc_ordered_workqueue("kmmcd", 0);
2757 if (!workqueue)
2758 return -ENOMEM;
2759
2760 ret = mmc_register_bus();
2761 if (ret)
2762 goto destroy_workqueue;
2763
2764 ret = mmc_register_host_class();
2765 if (ret)
2766 goto unregister_bus;
2767
2768 ret = sdio_register_bus();
2769 if (ret)
2770 goto unregister_host_class;
2771
2772 return 0;
2773
2774unregister_host_class:
2775 mmc_unregister_host_class();
2776unregister_bus:
2777 mmc_unregister_bus();
2778destroy_workqueue:
2779 destroy_workqueue(workqueue);
2780
2781 return ret;
2782}
2783
2784static void __exit mmc_exit(void)
2785{
2786 sdio_unregister_bus();
2787 mmc_unregister_host_class();
2788 mmc_unregister_bus();
2789 destroy_workqueue(workqueue);
2790}
2791
2792subsys_initcall(mmc_init);
2793module_exit(mmc_exit);
2794
2795MODULE_LICENSE("GPL");
2796