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