-
Notifications
You must be signed in to change notification settings - Fork 233
/
mci_media.c
1289 lines (1043 loc) · 27.9 KB
/
mci_media.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2012 Microchip Technology Inc. and its subsidiaries
//
// SPDX-License-Identifier: MIT
#include "common.h"
#include "string.h"
#include "mci_media.h"
#include "timer.h"
#include "atmel_mci.h"
#include "sdhc.h"
#include "debug.h"
#define DEFAULT_SD_BLOCK_LEN 512
static struct sdcard_register sdcard_register;
static struct sd_command sdcard_command;
static struct sd_data sdcard_data;
static struct sd_card atmel_sdcard;
static int sd_cmd_set_blocklen(struct sd_card *sdcard,
unsigned int block_len);
static int sd_cmd_go_idle_state(struct sd_card *sdcard)
{
struct sd_host *host = sdcard->host;
struct sd_command *command = sdcard->command;
int ret;
command->cmd = SD_CMD_GO_IDLE_STATE;
command->resp_type = SD_RESP_TYPE_NO_RESP;
command->argu = 0;
ret = host->ops->send_command(command, 0);
if (ret)
return ret;
return 0;
}
static int sd_cmd_send_if_cond(struct sd_card *sdcard)
{
struct sd_host *host = sdcard->host;
struct sd_command *command = sdcard->command;
int ret;
command->cmd = SD_CMD_SEND_IF_COND;
command->resp_type = SD_RESP_TYPE_R1;
command->argu = CHECK_PATTERN;
command->argu |= (host->caps_voltages
& OCR_VOLTAGE_27_36_MASK) ? (0x01 << 8) : 0;
ret = host->ops->send_command(command, 0);
if (ret)
return ret;
if (((command->resp[0] & CHECK_PATTERN) != CHECK_PATTERN)
|| (((command->resp[0] >> 8) & 0x0f) != 0x01))
return ERROR_UNUSABLE_CARD;
else
return 0;
}
static int sd_cmd_send_app_cmd(struct sd_card *sdcard)
{
struct sd_host *host = sdcard->host;
struct sd_command *command = sdcard->command;
int ret;
command->cmd = SD_CMD_APP_CMD;
command->resp_type = SD_RESP_TYPE_R1;
command->argu = sdcard->reg->rca << 16;
ret = host->ops->send_command(command, 0);
if (ret)
return ret;
return 0;
}
/* Host Capacity Support / Card Capacity Status */
#define OCR_HCR_CCS (0x01 << 30)
#define OCR_BUSY_STATUS (0x01 << 31)
static int sd_cmd_app_sd_send_op_cmd(struct sd_card *sdcard,
unsigned int capacity_support,
unsigned int *reponse)
{
struct sd_host *host = sdcard->host;
struct sd_command *command = sdcard->command;
int ret;
command->cmd = SD_CMD_APP_SD_SEND_OP_COND;
command->resp_type = SD_RESP_TYPE_R3;
command->argu = host->caps_voltages
& OCR_VOLTAGE_27_36_MASK;
if (capacity_support)
command->argu |= OCR_HCR_CCS;
ret = host->ops->send_command(command, 0);
if (ret)
return ret;
*reponse = command->resp[0];
return 0;
}
static int sd_check_operational_condition(struct sd_card *sdcard,
unsigned int capacity_support)
{
unsigned int response = 0;
unsigned int retries = 1000;
unsigned int i;
int ret;
/*
* The host repeatedly issues ACMD41 for at least 1 second
* or until the busy bit are set to 1.
*/
for (i = 0; i < retries; i++) {
ret = sd_cmd_send_app_cmd(sdcard);
if (ret)
return ret;
ret = sd_cmd_app_sd_send_op_cmd(sdcard,
capacity_support, &response);
if (ret)
return ret;
if (response & OCR_BUSY_STATUS)
break;
udelay(1000);
};
if (i == retries)
return ERROR_UNUSABLE_CARD;
sdcard->reg->ocr = response;
return 0;
}
static int sd_cmd_all_send_cid(struct sd_card *sdcard)
{
struct sd_host *host = sdcard->host;
struct sd_command *command = sdcard->command;
unsigned int i;
int ret;
unsigned int resp;
command->cmd = SD_CMD_ALL_SEND_CID;
command->resp_type = SD_RESP_TYPE_R2;
command->argu = 0;
ret = host->ops->send_command(command, 0);
if (ret)
return ret;
/* we need to shift the answer with 8 bits, because
* the registers provide R[128:8] in RR3[23:0],
* RR2[31:0], RR1[31:0] and RR0[31:0]
*/
sdcard->reg->cid[3] = 0x000000ff;
for (i = 0; i < 4; i++) {
resp = command->resp[i];
if (i < 3)
sdcard->reg->cid[2 - i] = resp >> 24 & 0xff;
sdcard->reg->cid[3 - i] |= resp << 8 & 0xffffff00;
}
return 0;
}
static int sd_cmd_send_relative_addr(struct sd_card *sdcard)
{
struct sd_host *host = sdcard->host;
struct sd_command *command = sdcard->command;
int ret;
sdcard->reg->rca = 1;
command->cmd = SD_CMD_SEND_RELATIVE_ADDR;
command->resp_type = SD_RESP_TYPE_R6;
command->argu = sdcard->reg->rca << 16;
ret = host->ops->send_command(command, 0);
if (ret)
return ret;
sdcard->reg->rca = (sdcard->card_type == CARD_TYPE_SD) ?
(command->resp[0] >> 16) & 0xffff : 1;
return 0;
}
static int sd_cmd_send_status(struct sd_card *sdcard, unsigned int retries)
{
struct sd_host *host = sdcard->host;
struct sd_command *command = sdcard->command;
unsigned int i;
int ret;
command->cmd = SD_CMD_SEND_STATUS;
command->resp_type = SD_RESP_TYPE_R1;
command->argu = sdcard->reg->rca << 16;
for (i = 0; i < retries; i++) {
ret = host->ops->send_command(command, 0);
if (ret)
return ret;
if ((command->resp[0] >> 8) & 0x01)
break;
udelay(1000);
};
if (i == retries) {
dbg_info("Timeout, wait for card ready\n");
return ERROR_TIMEOUT;
}
return 0;
}
static int sd_cmd_select_card(struct sd_card *sdcard)
{
struct sd_host *host = sdcard->host;
struct sd_command *command = sdcard->command;
int ret;
command->cmd = SD_CMD_SELECT_CARD;
command->resp_type = SD_RESP_TYPE_R1;
command->argu = sdcard->reg->rca << 16;
ret = host->ops->send_command(command, 0);
if (ret)
return ret;
return 0;
}
static int sd_cmd_send_csd(struct sd_card *sdcard)
{
struct sd_host *host = sdcard->host;
struct sd_command *command = sdcard->command;
unsigned int i;
int ret;
unsigned int resp;
command->cmd = SD_CMD_SEND_CSD;
command->resp_type = SD_RESP_TYPE_R2;
command->argu = sdcard->reg->rca << 16;
ret = host->ops->send_command(command, 0);
if (ret)
return ret;
/* we need to shift the answer with 8 bits, because
* the registers provide R[128:8] in RR3[23:0],
* RR2[31:0], RR1[31:0] and RR0[31:0]
*/
sdcard->reg->csd[3] = 0x000000ff;
for (i = 0; i < 4; i++) {
resp = command->resp[i];
if (i < 3)
sdcard->reg->csd[2 - i] = resp >> 24 & 0xff;
sdcard->reg->csd[3 - i] |= resp << 8 & 0xffffff00;
}
return 0;
}
static int sd_cmd_app_send_scr(struct sd_card *sdcard)
{
struct sd_host *host = sdcard->host;
struct sd_command *command = sdcard->command;
struct sd_data *data = sdcard->data;
unsigned int scr_data[2];
int ret;
ret = sd_cmd_send_app_cmd(sdcard);
if (ret)
return ret;
command->cmd = SD_CMD_APP_SEND_SCR;
command->resp_type = SD_RESP_TYPE_R1;
command->argu = sdcard->reg->rca << 16;
data->buff = (unsigned char *)scr_data;
data->direction = SD_DATA_DIR_RD;
data->blocksize = 8;
data->blocks = 1;
ret = host->ops->send_command(command, data);
if (ret)
return ret;
sdcard->reg->scr[0] = swap_uint32(scr_data[0]);
sdcard->reg->scr[1] = swap_uint32(scr_data[1]);
return 0;
}
static int sd_cmd_app_set_bus_width(struct sd_card *sdcard,
unsigned int bus_width)
{
struct sd_host *host = sdcard->host;
struct sd_command *command = sdcard->command;
int ret;
command->cmd = SD_CMD_APP_SET_BUS_WIDTH;
command->resp_type = SD_RESP_TYPE_R1;
command->argu = (bus_width == 4) ? 0x02 : 0;
ret = host->ops->send_command(command, 0);
if (ret)
return ret;
return 0;
}
static int sd_set_bus_width(struct sd_card *sdcard,
unsigned int bus_width)
{
int ret;
ret = sd_cmd_send_app_cmd(sdcard);
if (ret)
return ret;
ret = sd_cmd_app_set_bus_width(sdcard, bus_width);
if (ret)
return ret;
return 0;
}
/* SD SWITCH */
#define SD_SWITCH_MODE_CHECK 0
#define SD_SWITCH_MODE_SET 1
#define SD_SWITCH_GRP_ACCESS_MODE 1
#define SD_SWITCH_GRP_CMD_SYS 2
#define SD_SWITCH_GRP_DRV_STRENGTH 3
#define SD_SWITCH_GRP_CUR_LIMIT 4
#define SD_SWITCH_FUNC_SDR12 0x00
#define SD_SWITCH_FUNC_HS_SDR25 0x01
#define SD_SWITCH_FUNC_SDR50 0x02
#define SD_SWITCH_FUNC_SDR104 0x03
#define SD_SWITCH_FUNC_DDR50 0x04
static int sd_cmd_switch_fun(struct sd_card *sdcard,
unsigned int mode,
unsigned int group,
unsigned int func,
unsigned int *status)
{
struct sd_host *host = sdcard->host;
struct sd_command *command = sdcard->command;
struct sd_data *data = sdcard->data;
int ret;
command->cmd = SD_CMD_SWITCH_FUN;
command->resp_type = SD_RESP_TYPE_R1;
command->argu = (mode << 31) | 0xffffff;
command->argu &= ~(0xf << ((group - 1) * 4));
command->argu |= func << ((group - 1) * 4);
data->buff = (unsigned char *)status;
data->direction = SD_DATA_DIR_RD;
data->blocksize = 64;
data->blocks = 1;
ret = host->ops->send_command(command, data);
if (ret)
return ret;
return 0;
}
/*
* Refer to Physical Layer Specification Version 3.1
* 4.3.10.6 Switch Function Flow example
* 4.3.10.7 Example of Checking
*/
static int switch_check_hs_busy_status_supported(struct sd_card *sdcard,
unsigned char *support)
{
unsigned int switch_func_status[16];
unsigned int status;
unsigned int version;
unsigned int retries = 6;
unsigned int i;
int ret;
for (i = 0; i < retries; i++) {
/* Mode 0 operation: check function */
ret = sd_cmd_switch_fun(sdcard,
SD_SWITCH_MODE_CHECK,
SD_SWITCH_GRP_ACCESS_MODE,
SD_SWITCH_FUNC_HS_SDR25,
switch_func_status);
if (ret)
return ret;
/* Check Data Structure version
* 0x00 - bits 511:376 are defined
* 0x01 - bits 511:272 are defined
*/
version = swap_uint32(switch_func_status[4]);
if (((version >> 16) & 0xff) == 0x00)
break;
/* Check Busy Status of function */
status = swap_uint32(switch_func_status[7]);
if (!((status >> 17) & 0x01))
break;
};
if (i == retries)
return -1;
/* Check function supported */
status = swap_uint32(switch_func_status[3]);
*support = ((status >> 17) & 0x01) ? 1 : 0;
return 0;
}
static int sd_switch_func_high_speed(struct sd_card *sdcard)
{
unsigned int switch_func_status[16];
unsigned int status;
unsigned char support = 0;
int ret;
ret = switch_check_hs_busy_status_supported(sdcard, &support);
if (ret)
return ret;
if (!support) {
dbg_info("SD: Not support hs function switch\n");
return 0;
}
/* Mode 1 operation: set functioin */
ret = sd_cmd_switch_fun(sdcard,
SD_SWITCH_MODE_SET,
SD_SWITCH_GRP_ACCESS_MODE,
SD_SWITCH_FUNC_HS_SDR25,
switch_func_status);
if (ret)
return ret;
/* Check Switched function */
status = swap_uint32(switch_func_status[4]);
if ((status >> 24) & 0x01) {
sdcard->highspeed_card = 1;
return 0;
} else
return -1;
}
static int sd_card_set_bus_width(struct sd_card *sdcard)
{
struct sd_host *host = sdcard->host;
unsigned int bus_width;
int ret;
bus_width = (sdcard->bus_width_support & 0x04) ? 4 : 1;
ret = sd_set_bus_width(sdcard, bus_width);
if (ret)
return ret;
if (host->ops->set_bus_width) {
ret = host->ops->set_bus_width(sdcard, bus_width);
if (ret)
return ret;
} else {
return -1;
}
return 0;
}
/*-----------------------------------------------------------------*/
#define OCR_VOLTAGE_WIN_27_36 0x00FF8000
#define OCR_ACCESS_MODE 0x60000000
#define OCR_ACCESS_MODE_BYTE (0x00 << 30)
#define OCR_ACCESS_MODE_SECTOR (0x01 << 30)
static int mmc_cmd_send_op_cond(struct sd_card *sdcard,
unsigned int ocr)
{
struct sd_host *host = sdcard->host;
struct sd_command *command = sdcard->command;
int ret;
command->cmd = MMC_CMD_SEND_OP_COND;
command->resp_type = SD_RESP_TYPE_R3;
command->argu = (ocr & OCR_VOLTAGE_WIN_27_36)
| (ocr & OCR_ACCESS_MODE);
ret = host->ops->send_command(command, 0);
if (ret)
return ret;
return 0;
}
static int mmc_verify_operating_condition(struct sd_card *sdcard)
{
struct sd_command *command = sdcard->command;
unsigned int ocr;
unsigned int retries = 1000;
unsigned int i;
int ret;
dbg_very_loud("mmc_verify_operating_condition\n");
/* Query the card and determine the voltage type of the card */
ret = mmc_cmd_send_op_cond(sdcard, 0);
if (ret)
return ret;
ocr = command->resp[0] | OCR_ACCESS_MODE_SECTOR;
for (i = 0; i < retries; i++) {
ret = mmc_cmd_send_op_cond(sdcard, ocr);
if (ret)
return ret;
if (command->resp[0] & (0x01 << 31))
break;
udelay(1000);
};
if (i == retries)
return ERROR_UNUSABLE_CARD;
sdcard->reg->ocr = command->resp[0];
dbg_very_loud("mmc_verify_operating_condition success OCR = %x\n",
sdcard->reg->ocr);
return 0;
}
static int mmc_cmd_switch_fun(struct sd_card *sdcard,
unsigned char access_mode,
unsigned char index,
unsigned char value)
{
struct sd_host *host = sdcard->host;
struct sd_command *command = sdcard->command;
unsigned int retries = 1000;
int ret;
command->cmd = MMC_CMD_SWITCH_FUN;
command->resp_type = SD_RESP_TYPE_R1B;
command->argu = (access_mode << 24)
| (index << 16)
| (value << 8);
ret = host->ops->send_command(command, 0);
sd_cmd_send_status(sdcard, retries);
if (ret)
return ret;
return 0;
}
static int mmc_cmd_send_ext_csd(struct sd_card *sdcard, char *ext_csd)
{
struct sd_host *host = sdcard->host;
struct sd_command *command = sdcard->command;
struct sd_data *data = sdcard->data;
unsigned short block_len = DEFAULT_SD_BLOCK_LEN;
int ret;
command->cmd = MMC_CMD_SEND_EXT_CSD;
command->resp_type = SD_RESP_TYPE_R1;
command->argu = 0;
data->buff = (unsigned char *)ext_csd;
data->direction = SD_DATA_DIR_RD;
data->blocksize = block_len;
data->blocks = 1;
ret = host->ops->send_command(command, data);
if (ret)
return ret;
return 0;
}
/* EXT_CSD access mode */
#define MMC_EXT_CSD_ACCESS_CMD_SET 0x00
#define MMC_EXT_CSD_ACCESS_SET_BITS 0x01
#define MMC_EXT_CSD_ACCESS_CLEAR_BITS 0x02
#define MMC_EXT_CSD_ACCESS_WRITE_BYTE 0x03
#define EXT_CSD_BYTE_BUS_WIDTH 183
#define EXT_CSD_BYTE_HS_TIMING 185
#define EXT_CSD_BYTE_POWER_CLASS 187
#define EXT_CSD_BYTE_CMD_SET_REV 189
#define EXT_CSD_BYTE_CMD_SET 191
#define EXT_CSD_BYTE_EXT_CSD_REV 192
#define EXT_CSD_BYTE_CSD_STRUCTURE 194
#define EXT_CSD_BYTE_CARD_TYPE 196
static int mmc_card_identify(struct sd_card *sdcard)
{
char ext_csd[DEFAULT_SD_BLOCK_LEN];
char cardtype;
int ret;
ret = mmc_cmd_send_ext_csd(sdcard, ext_csd);
if (ret)
return ret;
cardtype = ext_csd[EXT_CSD_BYTE_CARD_TYPE] & 0x07;
switch(ext_csd[EXT_CSD_BYTE_EXT_CSD_REV]) {
case 0:
dbg_printf("MMC: v4.0 detected\n");
break;
case 1:
dbg_printf("MMC: v4.1 detected\n");
break;
case 2:
dbg_printf("MMC: v4.2 detected\n");
break;
case 3:
dbg_printf("MMC: v4.3 detected\n");
break;
case 4:
dbg_printf("MMC: v4.4 detected\n");
break;
case 5:
dbg_printf("MMC: v4.41 detected\n");
break;
case 6:
dbg_printf("MMC: v4.5/4.51 detected\n");
break;
case 7:
dbg_printf("MMC: v5.0/5.01 detected\n");
break;
case 8:
dbg_printf("MMC: v5.1 detected\n");
break;
default:
dbg_printf("MMC: unknown revision\n");
};
sdcard->highspeed_card = !!(cardtype & 0x02);
sdcard->ddr_support = !!(cardtype & 0x04);
if (sdcard->highspeed_card)
dbg_printf("MMC: highspeed supported\n");
if (sdcard->ddr_support)
dbg_printf("MMC: Dual Data Rate supported\n");
return 0;
}
static int mmc_switch_high_speed(struct sd_card *sdcard)
{
char ext_csd[DEFAULT_SD_BLOCK_LEN];
int ret;
ret = mmc_cmd_switch_fun(sdcard,
MMC_EXT_CSD_ACCESS_WRITE_BYTE,
EXT_CSD_BYTE_HS_TIMING,
1);
if (ret)
return ret;
ret = mmc_cmd_send_ext_csd(sdcard, ext_csd);
if (ret)
return ret;
if (!ext_csd[EXT_CSD_BYTE_HS_TIMING])
return -1;
return 0;
}
static int mmc_cmd_bustest_w(struct sd_card *sdcard,
unsigned int buswidth,
unsigned char *buffer)
{
struct sd_host *host = sdcard->host;
struct sd_command *command = sdcard->command;
struct sd_data *data = sdcard->data;
unsigned short bytes_to_write;
int ret;
bytes_to_write = (buswidth == 8) ? 8 : 4;
command->cmd = MMC_CMD_BUSTEST_W;
command->resp_type = SD_RESP_TYPE_R1;
command->argu = 0;
data->buff = buffer;
data->direction = SD_DATA_DIR_WR;
data->blocksize = bytes_to_write;
data->blocks = 1;
ret = host->ops->send_command(command, data);
if (ret)
return ret;
return 0;
}
static int mmc_cmd_bustest_r(struct sd_card *sdcard,
unsigned int buswidth,
unsigned char *buffer)
{
struct sd_host *host = sdcard->host;
struct sd_command *command = sdcard->command;
struct sd_data *data = sdcard->data;
unsigned short bytes_to_read;
int ret;
bytes_to_read = (buswidth == 8) ? 8 : 4;
command->cmd = MMC_CMD_BUSTEST_R;
command->resp_type = SD_RESP_TYPE_R1;
command->argu = 0;
data->buff = buffer;
data->direction = SD_DATA_DIR_RD;
data->blocksize = bytes_to_read;
data->blocks = 1;
ret = host->ops->send_command(command, data);
if (ret)
return ret;
return 0;
}
#define MMC_BUS_WIDTH_8_DDR 6
#define MMC_BUS_WIDTH_4_DDR 5
#define MMC_BUS_WIDTH_8 2
#define MMC_BUS_WIDTH_4 1
#define MMC_BUS_WIDTH_1 0
static int mmc_bus_width_select(struct sd_card *sdcard, unsigned int buswidth,
int ddr)
{
struct sd_host *host = sdcard->host;
unsigned char busw;
int ret;
if (!ddr)
busw = (buswidth == 8) ? MMC_BUS_WIDTH_8 : MMC_BUS_WIDTH_4;
else
busw = (buswidth == 8) ? MMC_BUS_WIDTH_8_DDR : MMC_BUS_WIDTH_4_DDR;
ret = mmc_cmd_switch_fun(sdcard,
MMC_EXT_CSD_ACCESS_WRITE_BYTE,
EXT_CSD_BYTE_BUS_WIDTH,
busw);
if (ret)
return ret;
if (host->ops->set_bus_width) {
ret = host->ops->set_bus_width(sdcard, buswidth);
if (ret)
return ret;
}
sd_cmd_send_status(sdcard, 1000);
if (ret)
return ret;
if (ddr && host->ops->set_ddr) {
ret = host->ops->set_ddr(sdcard);
if (ret)
return ret;
}
return 0;
}
static int mmc_detect_buswidth(struct sd_card *sdcard)
{
unsigned char data_8bits[8] = {0x55, 0xaa, 0, 0, 0, 0, 0, 0};
unsigned char data_4bits[4] = {0x5a, 0, 0, 0};
unsigned char read_data[8];
unsigned char *pdata_w;
unsigned int busw;
unsigned int len;
unsigned int i;
int ret;
console_printf("MMC: detecting buswidth...\n");
for (busw = 8, len = 2; busw != 0; busw -= 4, len--) {
pdata_w = (busw == 8) ? data_8bits : data_4bits;
ret = mmc_bus_width_select(sdcard, busw, 0);
if (ret)
continue;
ret = mmc_cmd_bustest_w(sdcard, busw, pdata_w);
if (ret)
continue;
ret = mmc_cmd_bustest_r(sdcard, busw, read_data);
if (ret)
continue;
for (i = 0; i < len; i++) {
if ((pdata_w[i] ^ read_data[i]) != 0xff)
break;
}
if (i == len) {
dbg_info("MMC: %d-bit bus width detected\n", busw);
break;
}
}
if (!busw && !len)
dbg_info("MMC: falling back to 1 bit bus width\n");
return 0;
}
/*-----------------------------------------------------------------*/
/*
* Refer to Physical Layer Specification Version 3.1
* Figure 4-1: SD Memory Card State Diagram (card identification mode)
* Figure 4-2: Card Initialization and Indentification Flow (SD mode)
*/
static int sdcard_identification(struct sd_card *sdcard)
{
int ret;
udelay(3000);
ret = sd_cmd_go_idle_state(sdcard);
if (ret)
return ret;
udelay(2000);
ret = mmc_verify_operating_condition(sdcard);
if (ret == 0) {
sdcard->card_type = CARD_TYPE_MMC;
dbg_very_loud("Card type is MMC\n");
} else if (ret == ERROR_TIMEOUT) {
ret = sd_cmd_send_if_cond(sdcard);
if (ret == 0) {
/* Ver 2.00 or later SD Memory Card */
ret = sd_check_operational_condition(sdcard, 1);
if (ret == ERROR_UNUSABLE_CARD) {
dbg_info("Unusable Card\n");
return -1;
} else if (ret)
return ret;
} else if (ret == ERROR_TIMEOUT) {
ret = sd_check_operational_condition(sdcard, 0);
if (ret == ERROR_UNUSABLE_CARD) {
dbg_info("Unusable Card\n");
return -1;
} else if (ret)
return ret;
}
sdcard->card_type = CARD_TYPE_SD;
} else if (ret == ERROR_UNUSABLE_CARD) {
/*
* Non-compatible voltage range
* or checkpattern not correct
*/
dbg_info("Unusable Card\n");
return -1;
} else
return ret;
sdcard->highcapacity_card = (sdcard->reg->ocr & OCR_HCR_CCS) ? 1 : 0;
if (sdcard->card_type == CARD_TYPE_SD) {
dbg_info("SD: Card Capacity: ");
if (sdcard->highcapacity_card)
dbg_info("High or Extended\n");
else
dbg_info("Standard\n");
}
/*
* Card that is unidentified (which is in Ready State)
* sends its CID number
*/
ret = sd_cmd_all_send_cid(sdcard);
if (ret) {
dbg_very_loud("sd_cmd_all_send_cid failed\n");
return ret;
}
dbg_very_loud("sd card identified with CID = %x %x %x %x\n",
sdcard->reg->cid[0], sdcard->reg->cid[1],
sdcard->reg->cid[2], sdcard->reg->cid[3]);
/* Asks the card to publish a new relative card address (RCA) */
ret = sd_cmd_send_relative_addr(sdcard);
if (ret) {
dbg_very_loud("sd_cmd_send_relative_addr failed\n");
return ret;
}
/*
* The host issues SEND_CSD(CMD9) to obtain
* the Card Specific Data (CSD Register),
*/
ret = sd_cmd_send_csd(sdcard);
if (ret) {
dbg_very_loud("sd_cmd_send_csd failed\n");
return ret;
}
sdcard->read_bl_len = DEFAULT_SD_BLOCK_LEN;
dbg_very_loud("sdcard_identification success\n");
return 0;
}
static int sd_initialization(struct sd_card *sdcard)
{
struct sd_host *host = sdcard->host;
int ret;
/*
* CMD7 is used to select one card and put it into
* the Transfer State
*/
ret = sd_cmd_select_card(sdcard);
if (ret)
return ret;
/*
* In Transfer State, send SEND_SCR(ACMD51)
* to read the SD Configuration Register (SCR)
*/
ret = sd_cmd_app_send_scr(sdcard);
if (ret)
return ret;
sdcard->bus_width_support = (sdcard->reg->scr[0] >> 16) & 0x0f;
unsigned int version;
version = (sdcard->reg->scr[0] >> 24) & 0x0f;
dbg_info("SD: Specification Version ");
if (version == 0) {
sdcard->sd_spec_version = SD_VERSION_1_0;
dbg_info("1.0 and 1.01\n");
} else if (version == 1) {
sdcard->sd_spec_version = SD_VERSION_1_10;
dbg_info("1.10\n");
} else if (version == 2) {
if ((sdcard->reg->scr[0] >> 15) & 0x01) {
sdcard->sd_spec_version = SD_VERSION_3;
dbg_info("3.0X\n");
} else {
sdcard->sd_spec_version = SD_VERSION_2;
dbg_info("2.00\n");
}
} else {
sdcard->sd_spec_version = SD_VERSION_1_0;
dbg_info("1.0 and 1.01\n");
}
if (host->caps_high_speed) {
if (sdcard->sd_spec_version != SD_VERSION_1_0) {
ret = sd_switch_func_high_speed(sdcard);
if (ret)
return ret;
}
}
if (host->ops->set_clock) {
if (sdcard->highspeed_card)
host->ops->set_clock(sdcard, 50000000);
else
host->ops->set_clock(sdcard, 25000000);