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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
|
#include <generated/autoconf.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/spinlock.h>
#include <linux/timer.h>
#include <linux/ioport.h>
#include <linux/device.h>
#include <linux/miscdevice.h>
#include <linux/platform_device.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/blkdev.h>
#include <linux/mmc/host.h>
#include <linux/mmc/card.h>
#include <linux/mmc/core.h>
#include <linux/mmc/mmc.h>
#include <linux/mmc/sd.h>
#include <linux/mmc/sdio.h>
#include <linux/dma-mapping.h>
#include <mach/dma.h>
#include <mach/board.h> /* FIXME */
/* #include <asm/tcm.h> */
#include "mt_sd.h"
#include <linux/mmc/sd_misc.h>
#include "board-custom.h"
#include "drivers/mmc/card/queue.h"
#ifndef FPGA_PLATFORM
#include <mach/mt_clkmgr.h>
#endif
#include <linux/proc_fs.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#ifdef CONFIG_MTK_EMMC_SUPPORT
/* extern struct excel_info PartInfoEmmc[PART_NUM]; */
extern u32 g_emmc_mode_switch;
#endif
#define PARTITION_NAME_LENGTH (64)
#define DRV_NAME_MISC "misc-sd"
#define DEBUG_MMC_IOCTL 0
#define DEBUG_MSDC_SSC 1
/*
* For simple_sd_ioctl
*/
#define FORCE_IN_DMA (0x11)
#define FORCE_IN_PIO (0x10)
#define FORCE_NOTHING (0x0)
static int dma_force[HOST_MAX_NUM] = /* used for sd ioctrol */
{
FORCE_NOTHING,
FORCE_NOTHING,
FORCE_NOTHING,
FORCE_NOTHING,
};
#define dma_is_forced(host_id) (dma_force[host_id] & 0x10)
#define get_forced_transfer_mode(host_id) (dma_force[host_id] & 0x01)
static u32* sg_msdc_multi_buffer = NULL;
static int simple_sd_open(struct inode *inode, struct file *file)
{
return 0;
}
extern int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
unsigned int timeout_ms);
extern int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd);
extern struct msdc_host *mtk_msdc_host[];
extern struct msdc_host *msdc_get_host(int host_function,bool boot,bool secondary);
extern int msdc_reinit(struct msdc_host *host);
extern int msdc_get_reserve(void);
extern u32 msdc_get_capacity(int get_emmc_total);
extern struct gendisk *mmc_get_disk(struct mmc_card *card);
#ifndef FPGA_PLATFORM
extern void msdc_get_driving(struct msdc_host *host, struct msdc_ioctl *msdc_ctl);
#endif
static int sd_ioctl_reinit(struct msdc_ioctl *msdc_ctl)
{
struct msdc_host *host = msdc_get_host(MSDC_SD, 0, 0);
if (NULL != host)
return msdc_reinit(host);
else
return -EINVAL;
}
static int sd_ioctl_cd_pin_en(struct msdc_ioctl *msdc_ctl)
{
struct msdc_host *host = msdc_get_host(MSDC_SD, 0, 0);
if (NULL != host) {
if (host->hw->host_function == MSDC_SD) {
return (host->hw->flags & MSDC_CD_PIN_EN == MSDC_CD_PIN_EN);
}
else {
return -EINVAL;
}
}
else {
return -EINVAL;
}
}
void msdc_check_init_done(void)
{
struct msdc_host *host = NULL;
host = msdc_get_host(MSDC_EMMC, 1, 0);
BUG_ON(!host);
BUG_ON(!host->mmc);
host->mmc->card_init_wait(host->mmc);
BUG_ON(!host->mmc->card);
pr_err("[%s]: get the emmc init done signal\n", __func__);
return;
}
static int simple_sd_ioctl_multi_rw(struct msdc_ioctl *msdc_ctl)
{
char l_buf[512];
struct scatterlist msdc_sg;
struct mmc_data msdc_data;
struct mmc_command msdc_cmd;
struct mmc_command msdc_stop;
int ret = 0;
#ifdef MTK_MSDC_USE_CMD23
struct mmc_command msdc_sbc;
#endif
struct mmc_request msdc_mrq;
struct msdc_host *host_ctl;
if(!msdc_ctl)
return -EINVAL;
if(msdc_ctl->total_size <= 0)
return -EINVAL;
host_ctl = mtk_msdc_host[msdc_ctl->host_num];
BUG_ON(!host_ctl);
BUG_ON(!host_ctl->mmc);
BUG_ON(!host_ctl->mmc->card);
mmc_claim_host(host_ctl->mmc);
#if DEBUG_MMC_IOCTL
pr_debug("user want access %d partition\n", msdc_ctl->partition);
#endif
ret = mmc_send_ext_csd(host_ctl->mmc->card, l_buf);
if (ret) {
pr_debug("mmc_send_ext_csd error, multi rw\n");
goto multi_end;
}
#ifdef CONFIG_MTK_EMMC_SUPPORT
switch (msdc_ctl->partition) {
case EMMC_PART_BOOT1:
if (0x1 != (l_buf[179] & 0x7)) {
/* change to access boot partition 1 */
l_buf[179] &= ~0x7;
l_buf[179] |= 0x1;
mmc_switch(host_ctl->mmc->card, 0, 179, l_buf[179], 1000);
}
break;
case EMMC_PART_BOOT2:
if (0x2 != (l_buf[179] & 0x7)) {
/* change to access boot partition 2 */
l_buf[179] &= ~0x7;
l_buf[179] |= 0x2;
mmc_switch(host_ctl->mmc->card, 0, 179, l_buf[179], 1000);
}
break;
default:
/* make sure access partition is user data area */
if (0 != (l_buf[179] & 0x7)) {
/* set back to access user area */
l_buf[179] &= ~0x7;
l_buf[179] |= 0x0;
mmc_switch(host_ctl->mmc->card, 0, 179, l_buf[179], 1000);
}
break;
}
#endif
if(msdc_ctl->total_size > host_ctl->mmc->max_req_size) { //64*1024){
msdc_ctl->result = -1;
goto multi_end;
}
memset(&msdc_data, 0, sizeof(struct mmc_data));
memset(&msdc_mrq, 0, sizeof(struct mmc_request));
memset(&msdc_cmd, 0, sizeof(struct mmc_command));
memset(&msdc_stop, 0, sizeof(struct mmc_command));
#ifdef MTK_MSDC_USE_CMD23
memset(&msdc_sbc, 0, sizeof(struct mmc_command));
#endif
msdc_mrq.cmd = &msdc_cmd;
msdc_mrq.data = &msdc_data;
if (msdc_ctl->trans_type)
dma_force[host_ctl->id] = FORCE_IN_DMA;
else
dma_force[host_ctl->id] = FORCE_IN_PIO;
if (msdc_ctl->iswrite) {
msdc_data.flags = MMC_DATA_WRITE;
msdc_cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
msdc_data.blocks = msdc_ctl->total_size / 512;
if (MSDC_CARD_DUNM_FUNC != msdc_ctl->opcode) {
if (copy_from_user(sg_msdc_multi_buffer, msdc_ctl->buffer, msdc_ctl->total_size)) {
dma_force[host_ctl->id] = FORCE_NOTHING;
ret = -EFAULT;
goto multi_end;
}
} else {
/* called from other kernel module */
memcpy(sg_msdc_multi_buffer, msdc_ctl->buffer, msdc_ctl->total_size);
}
} else {
msdc_data.flags = MMC_DATA_READ;
msdc_cmd.opcode = MMC_READ_MULTIPLE_BLOCK;
msdc_data.blocks = msdc_ctl->total_size / 512;
memset(sg_msdc_multi_buffer, 0, msdc_ctl->total_size);
}
#ifdef MTK_MSDC_USE_CMD23
if ((mmc_card_mmc(host_ctl->mmc->card)
|| (mmc_card_sd(host_ctl->mmc->card)
&& host_ctl->mmc->card->scr.cmds & SD_SCR_CMD23_SUPPORT))
&& !(host_ctl->mmc->card->quirks & MMC_QUIRK_BLK_NO_CMD23)) {
msdc_mrq.sbc = &msdc_sbc;
msdc_mrq.sbc->opcode = MMC_SET_BLOCK_COUNT;
#ifdef MTK_MSDC_USE_CACHE
/* if ioctl access cacheable partition data, there is on flush mechanism in msdc driver
* so do reliable write .*/
if (mmc_card_mmc(host_ctl->mmc->card)
&& (host_ctl->mmc->card->ext_csd.cache_ctrl & 0x1)
&& (msdc_cmd.opcode == MMC_WRITE_MULTIPLE_BLOCK))
msdc_mrq.sbc->arg = msdc_data.blocks | (1 << 31);
else
msdc_mrq.sbc->arg = msdc_data.blocks;
#else
msdc_mrq.sbc->arg = msdc_data.blocks;
#endif
msdc_mrq.sbc->flags = MMC_RSP_R1 | MMC_CMD_AC;
}
#endif
msdc_cmd.arg = msdc_ctl->address;
if (!mmc_card_blockaddr(host_ctl->mmc->card)) {
pr_debug("this device use byte address!!\n");
msdc_cmd.arg <<= 9;
}
msdc_cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
msdc_stop.opcode = MMC_STOP_TRANSMISSION;
msdc_stop.arg = 0;
msdc_stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
msdc_data.stop = &msdc_stop;
msdc_data.blksz = 512;
msdc_data.sg = &msdc_sg;
msdc_data.sg_len = 1;
#if DEBUG_MMC_IOCTL
pr_debug("total size is %d\n", msdc_ctl->total_size);
#endif
sg_init_one(&msdc_sg, sg_msdc_multi_buffer, msdc_ctl->total_size);
mmc_set_data_timeout(&msdc_data, host_ctl->mmc->card);
mmc_wait_for_req(host_ctl->mmc, &msdc_mrq);
if (!msdc_ctl->iswrite) {
if (MSDC_CARD_DUNM_FUNC != msdc_ctl->opcode) {
if (copy_to_user(msdc_ctl->buffer, sg_msdc_multi_buffer, msdc_ctl->total_size)) {
dma_force[host_ctl->id] = FORCE_NOTHING;
ret = -EFAULT;
goto multi_end;
}
} else {
/* called from other kernel module */
memcpy(msdc_ctl->buffer, sg_msdc_multi_buffer, msdc_ctl->total_size);
}
}
/* clear the global buffer of R/W IOCTL */
memset(sg_msdc_multi_buffer, 0 , msdc_ctl->total_size);
if (msdc_ctl->partition) {
ret = mmc_send_ext_csd(host_ctl->mmc->card, l_buf);
if (ret) {
pr_debug("mmc_send_ext_csd error, multi rw2\n");
goto multi_end;
}
if (l_buf[179] & 0x7) {
/* set back to access user area */
l_buf[179] &= ~0x7;
l_buf[179] |= 0x0;
mmc_switch(host_ctl->mmc->card, 0, 179, l_buf[179], 1000);
}
}
multi_end:
mmc_release_host(host_ctl->mmc);
if (ret)
msdc_ctl->result = ret;
if (msdc_cmd.error)
msdc_ctl->result = msdc_cmd.error;
if (msdc_data.error) {
msdc_ctl->result = msdc_data.error;
} else {
msdc_ctl->result = 0;
}
dma_force[host_ctl->id] = FORCE_NOTHING;
return msdc_ctl->result;
}
static int simple_sd_ioctl_single_rw(struct msdc_ioctl *msdc_ctl)
{
char l_buf[512];
struct scatterlist msdc_sg;
struct mmc_data msdc_data;
struct mmc_command msdc_cmd;
struct mmc_request msdc_mrq;
struct msdc_host *host_ctl;
int ret = 0;
if(!msdc_ctl)
return -EINVAL;
if(msdc_ctl->total_size <= 0)
return -EINVAL;
host_ctl = mtk_msdc_host[msdc_ctl->host_num];
BUG_ON(!host_ctl);
BUG_ON(!host_ctl->mmc);
BUG_ON(!host_ctl->mmc->card);
#ifdef MTK_MSDC_USE_CACHE
if (msdc_ctl->iswrite && mmc_card_mmc(host_ctl->mmc->card)
&& (host_ctl->mmc->card->ext_csd.cache_ctrl & 0x1))
return simple_sd_ioctl_multi_rw(msdc_ctl);
#endif
mmc_claim_host(host_ctl->mmc);
#if DEBUG_MMC_IOCTL
pr_debug("user want access %d partition\n", msdc_ctl->partition);
#endif
ret = mmc_send_ext_csd(host_ctl->mmc->card, l_buf);
if (ret) {
pr_debug("mmc_send_ext_csd error, single rw\n");
goto single_end;
}
#ifdef CONFIG_MTK_EMMC_SUPPORT
switch (msdc_ctl->partition) {
case EMMC_PART_BOOT1:
if (0x1 != (l_buf[179] & 0x7)) {
/* change to access boot partition 1 */
l_buf[179] &= ~0x7;
l_buf[179] |= 0x1;
mmc_switch(host_ctl->mmc->card, 0, 179, l_buf[179], 1000);
}
break;
case EMMC_PART_BOOT2:
if (0x2 != (l_buf[179] & 0x7)) {
/* change to access boot partition 2 */
l_buf[179] &= ~0x7;
l_buf[179] |= 0x2;
mmc_switch(host_ctl->mmc->card, 0, 179, l_buf[179], 1000);
}
break;
default:
/* make sure access partition is user data area */
if (0 != (l_buf[179] & 0x7)) {
/* set back to access user area */
l_buf[179] &= ~0x7;
l_buf[179] |= 0x0;
mmc_switch(host_ctl->mmc->card, 0, 179, l_buf[179], 1000);
}
break;
}
#endif
if (msdc_ctl->total_size > 512) {
msdc_ctl->result = -1;
goto single_end;
}
#if DEBUG_MMC_IOCTL
pr_debug("start MSDC_SINGLE_READ_WRITE !!\n");
#endif
memset(&msdc_data, 0, sizeof(struct mmc_data));
memset(&msdc_mrq, 0, sizeof(struct mmc_request));
memset(&msdc_cmd, 0, sizeof(struct mmc_command));
msdc_mrq.cmd = &msdc_cmd;
msdc_mrq.data = &msdc_data;
if (msdc_ctl->trans_type)
dma_force[host_ctl->id] = FORCE_IN_DMA;
else
dma_force[host_ctl->id] = FORCE_IN_PIO;
if (msdc_ctl->iswrite) {
msdc_data.flags = MMC_DATA_WRITE;
msdc_cmd.opcode = MMC_WRITE_BLOCK;
msdc_data.blocks = msdc_ctl->total_size / 512;
if (MSDC_CARD_DUNM_FUNC != msdc_ctl->opcode) {
if (copy_from_user(sg_msdc_multi_buffer, msdc_ctl->buffer, 512)) {
dma_force[host_ctl->id] = FORCE_NOTHING;
ret = -EFAULT;
goto single_end;
}
} else {
/* called from other kernel module */
memcpy(sg_msdc_multi_buffer, msdc_ctl->buffer, 512);
}
} else {
msdc_data.flags = MMC_DATA_READ;
msdc_cmd.opcode = MMC_READ_SINGLE_BLOCK;
msdc_data.blocks = msdc_ctl->total_size / 512;
memset(sg_msdc_multi_buffer, 0, 512);
}
msdc_cmd.arg = msdc_ctl->address;
if (!mmc_card_blockaddr(host_ctl->mmc->card)) {
pr_debug("the device is used byte address!\n");
msdc_cmd.arg <<= 9;
}
msdc_cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
msdc_data.stop = NULL;
msdc_data.blksz = 512;
msdc_data.sg = &msdc_sg;
msdc_data.sg_len = 1;
#if DEBUG_MMC_IOCTL
pr_debug("single block: ueser buf address is 0x%p!\n", msdc_ctl->buffer);
#endif
sg_init_one(&msdc_sg, sg_msdc_multi_buffer, msdc_ctl->total_size);
mmc_set_data_timeout(&msdc_data, host_ctl->mmc->card);
mmc_wait_for_req(host_ctl->mmc, &msdc_mrq);
if (!msdc_ctl->iswrite) {
if (MSDC_CARD_DUNM_FUNC != msdc_ctl->opcode) {
if (copy_to_user(msdc_ctl->buffer, sg_msdc_multi_buffer, 512)) {
dma_force[host_ctl->id] = FORCE_NOTHING;
ret = -EFAULT;
goto single_end;
}
} else {
/* called from other kernel module */
memcpy(msdc_ctl->buffer, sg_msdc_multi_buffer, 512);
}
}
/* clear the global buffer of R/W IOCTL */
memset(sg_msdc_multi_buffer, 0 , 512);
if (msdc_ctl->partition) {
ret = mmc_send_ext_csd(host_ctl->mmc->card, l_buf);
if (ret) {
pr_debug("mmc_send_ext_csd error, single rw2\n");
goto single_end;
}
if (l_buf[179] & 0x7) {
/* set back to access user area */
l_buf[179] &= ~0x7;
l_buf[179] |= 0x0;
mmc_switch(host_ctl->mmc->card, 0, 179, l_buf[179], 1000);
}
}
single_end:
mmc_release_host(host_ctl->mmc);
if (ret)
msdc_ctl->result = ret;
if (msdc_cmd.error)
msdc_ctl->result = msdc_cmd.error;
if (msdc_data.error)
msdc_ctl->result = msdc_data.error;
else
msdc_ctl->result = 0;
dma_force[host_ctl->id] = FORCE_NOTHING;
return msdc_ctl->result;
}
int simple_sd_ioctl_rw(struct msdc_ioctl *msdc_ctl)
{
if((msdc_ctl->total_size > 512) && (msdc_ctl->total_size <= MAX_REQ_SZ))
return simple_sd_ioctl_multi_rw(msdc_ctl);
else
return simple_sd_ioctl_single_rw(msdc_ctl);
}
static int simple_sd_ioctl_get_cid(struct msdc_ioctl *msdc_ctl)
{
struct msdc_host *host_ctl;
if(!msdc_ctl)
return -EINVAL;
host_ctl = mtk_msdc_host[msdc_ctl->host_num];
BUG_ON(!host_ctl);
BUG_ON(!host_ctl->mmc);
BUG_ON(!host_ctl->mmc->card);
#if DEBUG_MMC_IOCTL
pr_debug("user want the cid in msdc slot%d\n", msdc_ctl->host_num);
#endif
if (copy_to_user(msdc_ctl->buffer, &host_ctl->mmc->card->raw_cid, 16))
return -EFAULT;
#if DEBUG_MMC_IOCTL
pr_debug("cid:0x%x,0x%x,0x%x,0x%x\n",
host_ctl->mmc->card->raw_cid[0], host_ctl->mmc->card->raw_cid[1],
host_ctl->mmc->card->raw_cid[2], host_ctl->mmc->card->raw_cid[3]);
#endif
return 0;
}
static int simple_sd_ioctl_get_csd(struct msdc_ioctl *msdc_ctl)
{
struct msdc_host *host_ctl;
if(!msdc_ctl)
return -EINVAL;
host_ctl = mtk_msdc_host[msdc_ctl->host_num];
BUG_ON(!host_ctl);
BUG_ON(!host_ctl->mmc);
BUG_ON(!host_ctl->mmc->card);
#if DEBUG_MMC_IOCTL
pr_debug("user want the csd in msdc slot%d\n", msdc_ctl->host_num);
#endif
if (copy_to_user(msdc_ctl->buffer, &host_ctl->mmc->card->raw_csd, 16))
return -EFAULT;
#if DEBUG_MMC_IOCTL
pr_debug("csd:0x%x,0x%x,0x%x,0x%x\n",
host_ctl->mmc->card->raw_csd[0], host_ctl->mmc->card->raw_csd[1],
host_ctl->mmc->card->raw_csd[2], host_ctl->mmc->card->raw_csd[3]);
#endif
return 0;
}
static int simple_sd_ioctl_get_excsd(struct msdc_ioctl *msdc_ctl)
{
char l_buf[512];
struct msdc_host *host_ctl;
#if DEBUG_MMC_IOCTL
int i;
#endif
if(!msdc_ctl)
return -EINVAL;
host_ctl = mtk_msdc_host[msdc_ctl->host_num];
BUG_ON(!host_ctl);
BUG_ON(!host_ctl->mmc);
BUG_ON(!host_ctl->mmc->card);
mmc_claim_host(host_ctl->mmc);
#if DEBUG_MMC_IOCTL
pr_debug("user want the extend csd in msdc slot%d\n", msdc_ctl->host_num);
#endif
mmc_send_ext_csd(host_ctl->mmc->card, l_buf);
if (copy_to_user(msdc_ctl->buffer, l_buf, 512))
return -EFAULT;
#if DEBUG_MMC_IOCTL
for (i = 0; i < 512; i++) {
pr_debug("%x", l_buf[i]);
if (0 == ((i + 1) % 16)) {
pr_debug("\n");
}
}
#endif
if (copy_to_user(msdc_ctl->buffer, l_buf, 512))
return -EFAULT;
mmc_release_host(host_ctl->mmc);
return 0;
}
#ifndef FPGA_PLATFORM
extern void msdc_set_driving(struct msdc_host *host, struct msdc_hw *hw, bool sd_18);
#endif
extern void msdc_dump_padctl(struct msdc_host *host, u32 pin);
extern u32 msdc_host_mode[HOST_MAX_NUM];
extern u32 msdc_host_mode2[HOST_MAX_NUM];
static int simple_sd_ioctl_set_driving(struct msdc_ioctl *msdc_ctl)
{
void __iomem *base;
struct msdc_host *host;
#if DEBUG_MMC_IOCTL
unsigned int l_value;
#endif
if(!msdc_ctl)
return -EINVAL;
if((msdc_ctl->host_num < 0) || (msdc_ctl->host_num >= HOST_MAX_NUM)){
pr_err("invalid host num: %d\n", msdc_ctl->host_num);
return -EINVAL;
}else if (msdc_ctl->host_num == 0){
#ifndef CFG_DEV_MSDC0
pr_err("host%d is not config\n", msdc_ctl->host_num);
return -EINVAL;
#endif
} else if (msdc_ctl->host_num == 1) {
#ifndef CFG_DEV_MSDC1
pr_err("host%d is not config\n", msdc_ctl->host_num);
return -EINVAL;
#endif
} else if (msdc_ctl->host_num == 2) {
#ifndef CFG_DEV_MSDC2
pr_err("host%d is not config\n", msdc_ctl->host_num);
return -EINVAL;
#endif
} else if (msdc_ctl->host_num == 3) {
#ifndef CFG_DEV_MSDC3
pr_err("host%d is not config\n", msdc_ctl->host_num);
return -EINVAL;
#endif
} else if (msdc_ctl->host_num == 4) {
#ifndef CFG_DEV_MSDC4
pr_err("host%d is not config\n", msdc_ctl->host_num);
return -EINVAL;
#endif
}else {
pr_err("invalid host num: %d\n", msdc_ctl->host_num);
return -EINVAL;
}
host = mtk_msdc_host[msdc_ctl->host_num];
BUG_ON(!host);
base = host->base;
#ifndef FPGA_PLATFORM
enable_clock(MT_CG_PERI_MSDC30_0 + host->id, "SD");
#if DEBUG_MMC_IOCTL
pr_debug("set: clk driving is 0x%x\n", msdc_ctl->clk_pu_driving);
pr_debug("set: cmd driving is 0x%x\n", msdc_ctl->cmd_pu_driving);
pr_debug("set: dat driving is 0x%x\n", msdc_ctl->dat_pu_driving);
pr_debug("set: rst driving is 0x%x\n", msdc_ctl->rst_pu_driving);
pr_debug("set: ds driving is 0x%x\n", msdc_ctl->ds_pu_driving);
#endif
host->hw->clk_drv = msdc_ctl->clk_pu_driving;
host->hw->cmd_drv = msdc_ctl->cmd_pu_driving;
host->hw->dat_drv = msdc_ctl->dat_pu_driving;
host->hw->rst_drv = msdc_ctl->rst_pu_driving;
host->hw->ds_drv = msdc_ctl->ds_pu_driving;
host->hw->clk_drv_sd_18 = msdc_ctl->clk_pu_driving;
host->hw->cmd_drv_sd_18 = msdc_ctl->cmd_pu_driving;
host->hw->dat_drv_sd_18 = msdc_ctl->dat_pu_driving;
msdc_set_driving(host, host->hw, 0);
#if DEBUG_MMC_IOCTL
#if 0
msdc_dump_padctl(host);
#endif
#endif
#endif
return 0;
}
static int simple_sd_ioctl_get_driving(struct msdc_ioctl *msdc_ctl)
{
void __iomem *base;
/* unsigned int l_value; */
struct msdc_host *host;
if(!msdc_ctl)
return -EINVAL;
if((msdc_ctl->host_num < 0) || (msdc_ctl->host_num >= HOST_MAX_NUM)){
pr_err("invalid host num: %d\n", msdc_ctl->host_num);
return -EINVAL;
}else if (msdc_ctl->host_num == 0){
#ifndef CFG_DEV_MSDC0
pr_err("host%d is not config\n", msdc_ctl->host_num);
return -EINVAL;
#endif
} else if (msdc_ctl->host_num == 1) {
#ifndef CFG_DEV_MSDC1
pr_err("host%d is not config\n", msdc_ctl->host_num);
return -EINVAL;
#endif
} else if (msdc_ctl->host_num == 2) {
#ifndef CFG_DEV_MSDC2
pr_err("host%d is not config\n", msdc_ctl->host_num);
return -EINVAL;
#endif
} else if (msdc_ctl->host_num == 3) {
#ifndef CFG_DEV_MSDC3
pr_err("host%d is not config\n", msdc_ctl->host_num);
return -EINVAL;
#endif
} else if (msdc_ctl->host_num == 4) {
#ifndef CFG_DEV_MSDC4
pr_err("host%d is not config\n", msdc_ctl->host_num);
return -EINVAL;
#endif
}else {
pr_err("invalid host num: %d\n", msdc_ctl->host_num);
return -EINVAL;
}
host = mtk_msdc_host[msdc_ctl->host_num];
BUG_ON(!host);
base = host->base;
#ifndef FPGA_PLATFORM
enable_clock(MT_CG_PERI_MSDC30_0 + host->id, "SD");
msdc_get_driving(mtk_msdc_host[msdc_ctl->host_num], msdc_ctl);
#if DEBUG_MMC_IOCTL
pr_debug("read: clk driving is 0x%x\n", msdc_ctl->clk_pu_driving);
pr_debug("read: cmd driving is 0x%x\n", msdc_ctl->cmd_pu_driving);
pr_debug("read: dat driving is 0x%x\n", msdc_ctl->dat_pu_driving);
pr_debug("read: rst driving is 0x%x\n", msdc_ctl->rst_pu_driving);
pr_debug("read: ds driving is 0x%x\n", msdc_ctl->ds_pu_driving);
#endif
#endif
return 0;
}
static int simple_sd_ioctl_sd30_mode_switch(struct msdc_ioctl *msdc_ctl)
{
/* u32 base; */
/* struct msdc_hw hw; */
int id;
#if DEBUG_MMC_IOCTL
unsigned int l_value;
#endif
if(!msdc_ctl)
return -EINVAL;
id = msdc_ctl->host_num;
if((msdc_ctl->host_num < 0) || (msdc_ctl->host_num >= HOST_MAX_NUM)){
pr_err("invalid host num: %d\n", msdc_ctl->host_num);
return -EINVAL;
}else if (msdc_ctl->host_num == 0){
#ifndef CFG_DEV_MSDC0
pr_err("host%d is not config\n", msdc_ctl->host_num);
return -EINVAL;
#endif
} else if (msdc_ctl->host_num == 1) {
#ifndef CFG_DEV_MSDC1
pr_err("host%d is not config\n", msdc_ctl->host_num);
return -EINVAL;
#endif
} else if (msdc_ctl->host_num == 2) {
#ifndef CFG_DEV_MSDC2
pr_err("host%d is not config\n", msdc_ctl->host_num);
return -EINVAL;
#endif
} else if (msdc_ctl->host_num == 3) {
#ifndef CFG_DEV_MSDC3
pr_err("host%d is not config\n", msdc_ctl->host_num);
return -EINVAL;
#endif
} else if (msdc_ctl->host_num == 4) {
#ifndef CFG_DEV_MSDC4
pr_err("host%d is not config\n", msdc_ctl->host_num);
return -EINVAL;
#endif
}else {
pr_err("invalid host num: %d\n", msdc_ctl->host_num);
return -EINVAL;
}
switch (msdc_ctl->sd30_mode) {
case SDHC_HIGHSPEED:
msdc_host_mode[id] |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED;
msdc_host_mode[id] &= (~MMC_CAP_UHS_SDR12) & (~MMC_CAP_UHS_SDR25) &
(~MMC_CAP_UHS_SDR50) & (~MMC_CAP_UHS_DDR50) &
(~MMC_CAP_1_8V_DDR) & (~MMC_CAP_UHS_SDR104);
#if 1 /* CONFIG_EMMC_50_FEATURE */
msdc_host_mode2[id] &= (~MMC_CAP2_HS200_1_8V_SDR) & (~MMC_CAP2_HS400_1_8V_DDR);
#else
msdc_host_mode2[id] &= (~MMC_CAP2_HS200_1_8V_SDR);
#endif
pr_debug("[****SD_Debug****]host will support Highspeed\n");
break;
case UHS_SDR12:
msdc_host_mode[id] |= MMC_CAP_UHS_SDR12;
msdc_host_mode[id] &= (~MMC_CAP_UHS_SDR25) & (~MMC_CAP_UHS_SDR50) &
(~MMC_CAP_UHS_DDR50) & (~MMC_CAP_1_8V_DDR) & (~MMC_CAP_UHS_SDR104);
#if 1 /* CONFIG_EMMC_50_FEATURE */
msdc_host_mode2[id] &= (~MMC_CAP2_HS200_1_8V_SDR) & (~MMC_CAP2_HS400_1_8V_DDR);
#else
msdc_host_mode2[id] &= (~MMC_CAP2_HS200_1_8V_SDR);
#endif
pr_debug("[****SD_Debug****]host will support UHS-SDR12\n");
break;
case UHS_SDR25:
msdc_host_mode[id] |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
msdc_host_mode[id] &= (~MMC_CAP_UHS_SDR50) & (~MMC_CAP_UHS_DDR50) &
(~MMC_CAP_1_8V_DDR) & (~MMC_CAP_UHS_SDR104);
#if 1 /* CONFIG_EMMC_50_FEATURE */
msdc_host_mode2[id] &= (~MMC_CAP2_HS200_1_8V_SDR) & (~MMC_CAP2_HS400_1_8V_DDR);
#else
msdc_host_mode2[id] &= (~MMC_CAP2_HS200_1_8V_SDR);
#endif
pr_debug("[****SD_Debug****]host will support UHS-SDR25\n");
break;
case UHS_SDR50:
msdc_host_mode[id] |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | MMC_CAP_UHS_SDR50;
msdc_host_mode[id] &= (~MMC_CAP_UHS_DDR50) & (~MMC_CAP_1_8V_DDR) &
(~MMC_CAP_UHS_SDR104);
#if 1 /* CONFIG_EMMC_50_FEATURE */
msdc_host_mode2[id] &= (~MMC_CAP2_HS200_1_8V_SDR) & (~MMC_CAP2_HS400_1_8V_DDR);
#else
msdc_host_mode2[id] &= (~MMC_CAP2_HS200_1_8V_SDR);
#endif
pr_debug("[****SD_Debug****]host will support UHS-SDR50\n");
break;
case UHS_SDR104:
msdc_host_mode[id] |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 |
MMC_CAP_1_8V_DDR | MMC_CAP_UHS_SDR104;
msdc_host_mode2[id] |= MMC_CAP2_HS200_1_8V_SDR;
#if 1 /* CONFIG_EMMC_50_FEATURE */
msdc_host_mode2[id] &= (~MMC_CAP2_HS400_1_8V_DDR);
#endif
pr_debug("[****SD_Debug****]host will support UHS-SDR104\n");
break;
case UHS_DDR50:
msdc_host_mode[id] |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR;
msdc_host_mode[id] &= (~MMC_CAP_UHS_SDR50) & (~MMC_CAP_UHS_SDR104);
#if 1 /* CONFIG_EMMC_50_FEATURE */
msdc_host_mode2[id] &= (~MMC_CAP2_HS200_1_8V_SDR) & (~MMC_CAP2_HS400_1_8V_DDR);
#else
msdc_host_mode2[id] &= (~MMC_CAP2_HS200_1_8V_SDR);
#endif
pr_debug("[****SD_Debug****]host will support UHS-DDR50\n");
break;
#if 1 /* CONFIG_EMMC_50_FEATURE */
case 6: /* EMMC_HS400: */
msdc_host_mode[id] |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 |
MMC_CAP_1_8V_DDR | MMC_CAP_UHS_SDR104;
msdc_host_mode2[id] |= MMC_CAP2_HS200_1_8V_SDR | MMC_CAP2_HS400_1_8V_DDR;
pr_debug("[****SD_Debug****]host will support EMMC_HS400\n");
break;
#endif
default:
pr_debug("[****SD_Debug****]invalid sd30_mode:%d\n", msdc_ctl->sd30_mode);
break;
}
pr_debug(KERN_ERR "\n [%s]: msdc%d, line:%d, msdc_host_mode2=%d\n",
__func__, id, __LINE__, msdc_host_mode2[id]);
switch (msdc_ctl->sd30_drive) {
case DRIVER_TYPE_A:
msdc_host_mode[id] |= MMC_CAP_DRIVER_TYPE_A;
msdc_host_mode[id] &= (~MMC_CAP_DRIVER_TYPE_C) & (~MMC_CAP_DRIVER_TYPE_D);
pr_debug("[****SD_Debug****]host will support DRIVING TYPE A\n");
break;
case DRIVER_TYPE_B:
msdc_host_mode[id] &= (~MMC_CAP_DRIVER_TYPE_A) &
(~MMC_CAP_DRIVER_TYPE_C) & (~MMC_CAP_DRIVER_TYPE_D);
pr_debug("[****SD_Debug****]host will support DRIVING TYPE B\n");
break;
case DRIVER_TYPE_C:
msdc_host_mode[id] |= MMC_CAP_DRIVER_TYPE_C;
msdc_host_mode[id] &= (~MMC_CAP_DRIVER_TYPE_A) & (~MMC_CAP_DRIVER_TYPE_D);
pr_debug("[****SD_Debug****]host will support DRIVING TYPE C\n");
break;
case DRIVER_TYPE_D:
msdc_host_mode[id] |= MMC_CAP_DRIVER_TYPE_D;
msdc_host_mode[id] &= (~MMC_CAP_DRIVER_TYPE_A) & (~MMC_CAP_DRIVER_TYPE_C);
pr_debug("[****SD_Debug****]host will support DRIVING TYPE D\n");
break;
default:
pr_debug("[****SD_Debug****]invalid sd30_drive:%d\n", msdc_ctl->sd30_drive);
break;
}
#if 0
switch (msdc_ctl->sd30_max_current) {
case MAX_CURRENT_200:
msdc_host_mode[id] |= MMC_CAP_MAX_CURRENT_200;
msdc_host_mode[id] &= (~MMC_CAP_MAX_CURRENT_400)&
(~MMC_CAP_MAX_CURRENT_600) & (~MMC_CAP_MAX_CURRENT_800);
pr_debug("[****SD_Debug****]host will support MAX_CURRENT_200\n");
break;
case MAX_CURRENT_400:
msdc_host_mode[id] |= MMC_CAP_MAX_CURRENT_200 | MMC_CAP_MAX_CURRENT_400;
msdc_host_mode[id] &= (~MMC_CAP_MAX_CURRENT_600) & (~MMC_CAP_MAX_CURRENT_800);
pr_debug("[****SD_Debug****]host will support MAX_CURRENT_400\n");
break;
case MAX_CURRENT_600:
msdc_host_mode[id] |= MMC_CAP_MAX_CURRENT_200 |
MMC_CAP_MAX_CURRENT_400 | MMC_CAP_MAX_CURRENT_600;
msdc_host_mode[id] &= (~MMC_CAP_MAX_CURRENT_800);
pr_debug("[****SD_Debug****]host will support MAX_CURRENT_600\n");
break;
case MAX_CURRENT_800:
msdc_host_mode[id] |= MMC_CAP_MAX_CURRENT_200 |
MMC_CAP_MAX_CURRENT_400 | MMC_CAP_MAX_CURRENT_600 |
MMC_CAP_MAX_CURRENT_800;
pr_debug("[****SD_Debug****]host will support MAX_CURRENT_800\n");
break;
default:
pr_debug("[****SD_Debug****]invalid sd30_max_current:%d\n",
msdc_ctl->sd30_max_current);
break;
}
switch (msdc_ctl->sd30_power_control) {
case SDXC_NO_POWER_CONTROL:
msdc_host_mode[id] &= (~MMC_CAP_SET_XPC_330) &
(~MMC_CAP_SET_XPC_300) & (~MMC_CAP_SET_XPC_180);
pr_debug("[****SD_Debug****]host will not support SDXC power control\n");
break;
case SDXC_POWER_CONTROL:
msdc_host_mode[id] |= MMC_CAP_SET_XPC_330 |
MMC_CAP_SET_XPC_300 | MMC_CAP_SET_XPC_180;
pr_debug("[****SD_Debug****]host will support SDXC power control\n");
break;
default:
pr_debug("[****SD_Debug****]invalid sd30_power_control:%d\n",
msdc_ctl->sd30_power_control);
break;
}
#endif
#ifdef CONFIG_MTK_EMMC_SUPPORT
/* just for emmc slot */
if (msdc_ctl->host_num == 0) {
g_emmc_mode_switch = 1;
} else {
g_emmc_mode_switch = 0;
}
#endif
return 0;
}
/* to ensure format operate is clean the emmc device fully(partition erase) */
typedef struct mbr_part_info {
unsigned int start_sector;
unsigned int nr_sects;
unsigned int part_no;
unsigned char *part_name;
} MBR_PART_INFO_T;
#define MBR_PART_NUM 6
#define __MMC_ERASE_ARG 0x00000000
#define __MMC_TRIM_ARG 0x00000001
#define __MMC_DISCARD_ARG 0x00000003
struct __mmc_blk_data {
spinlock_t lock;
struct gendisk *disk;
struct mmc_queue queue;
unsigned int usage;
unsigned int read_only;
};
int msdc_get_info(STORAGE_TPYE storage_type, GET_STORAGE_INFO info_type, struct storage_info *info)
{
struct msdc_host *host = NULL;
int host_function = 0;
bool boot = 0;
if(!info)
return -EINVAL;
switch (storage_type) {
case EMMC_CARD_BOOT:
host_function = MSDC_EMMC;
boot = MSDC_BOOT_EN;
break;
case EMMC_CARD:
host_function = MSDC_EMMC;
break;
case SD_CARD_BOOT:
host_function = MSDC_SD;
boot = MSDC_BOOT_EN;
break;
case SD_CARD:
host_function = MSDC_SD;
break;
default:
pr_err(KERN_ERR "No supported storage type!");
return 0;
break;
}
host = msdc_get_host(host_function, boot, 0);
BUG_ON(!host);
BUG_ON(!host->mmc);
BUG_ON(!host->mmc->card);
switch (info_type) {
case CARD_INFO:
if (host->mmc && host->mmc->card)
info->card = host->mmc->card;
else {
pr_err(KERN_ERR "CARD was not ready<get card>!");
return 0;
}
break;
case DISK_INFO:
if (host->mmc && host->mmc->card)
info->disk = mmc_get_disk(host->mmc->card);
else {
pr_err(KERN_ERR "CARD was not ready<get disk>!");
return 0;
}
break;
case EMMC_USER_CAPACITY:
info->emmc_user_capacity = msdc_get_capacity(0);
break;
case EMMC_CAPACITY:
info->emmc_capacity = msdc_get_capacity(1);
break;
case EMMC_RESERVE:
#ifdef CONFIG_MTK_EMMC_SUPPORT
info->emmc_reserve = msdc_get_reserve();
#endif
break;
default:
pr_err(KERN_ERR "Please check INFO_TYPE");
return 0;
}
return 1;
}
#ifdef CONFIG_MTK_EMMC_SUPPORT
#ifdef CONFIG_MTK_GPT_SCHEME_SUPPORT
static int simple_mmc_get_disk_info(struct mbr_part_info* mpi, unsigned char* name)
{
char* no_partition_name = "n/a";
struct disk_part_iter piter;
struct hd_struct *part;
struct msdc_host *host;
struct gendisk *disk;
struct __mmc_blk_data *md;
/* emmc always in slot0 */
host = msdc_get_host(MSDC_EMMC,MSDC_BOOT_EN,0);
BUG_ON(!host);
BUG_ON(!host->mmc);
BUG_ON(!host->mmc->card);
md = mmc_get_drvdata(host->mmc->card);
BUG_ON(!md);
BUG_ON(!md->disk);
disk = md->disk;
/* use this way to find partition info is to avoid handle addr transfer in scatter file
* and 64bit address calculate */
disk_part_iter_init(&piter, disk, 0);
while ((part = disk_part_iter_next(&piter))){
#if DEBUG_MMC_IOCTL
pr_debug("part_name = %s name = %s\n", part->info->volname, name);
#endif
if (!strncmp(part->info->volname, name, PARTITION_NAME_LENGTH)){
mpi->start_sector = part->start_sect;
mpi->nr_sects = part->nr_sects;
mpi->part_no = part->partno;
if (part->info){
mpi->part_name = part->info->volname;
} else {
mpi->part_name = no_partition_name;
}
disk_part_iter_exit(&piter);
return 0;
}
}
disk_part_iter_exit(&piter);
return 1;
}
#else
static int simple_mmc_get_disk_info(struct mbr_part_info *mpi, unsigned char *name)
{
int i = 0;
char* no_partition_name = "n/a";
struct disk_part_iter piter;
struct hd_struct *part;
struct msdc_host *host;
struct gendisk *disk;
struct __mmc_blk_data *md;
if(!name || !mpi)
return -EINVAL;
/* emmc always in slot0 */
host = msdc_get_host(MSDC_EMMC,MSDC_BOOT_EN,0);
BUG_ON(!host);
BUG_ON(!host->mmc);
BUG_ON(!host->mmc->card);
md = mmc_get_drvdata(host->mmc->card);
BUG_ON(!md);
BUG_ON(!md->disk);
disk = md->disk;
/* use this way to find partition info is to avoid handle addr transfer in scatter file
* and 64bit address calculate */
disk_part_iter_init(&piter, disk, 0);
while ((part = disk_part_iter_next(&piter))){
for (i = 0; i < PART_NUM; i++) {
if ((PartInfo[i].partition_idx != 0)
&& (PartInfo[i].partition_idx == part->partno)) {
#if DEBUG_MMC_IOCTL
pr_debug("part_name = %s name = %s\n", PartInfo[i].name, name);
#endif
if (!strncmp(PartInfo[i].name, name, PARTITION_NAME_LENGTH)) {
mpi->start_sector = part->start_sect;
mpi->nr_sects = part->nr_sects;
mpi->part_no = part->partno;
if (i < PART_NUM) {
mpi->part_name = PartInfo[i].name;
} else {
mpi->part_name = no_partition_name;
}
disk_part_iter_exit(&piter);
return 0;
}
break;
}
}
}
disk_part_iter_exit(&piter);
return 1;
}
#endif
/* call mmc block layer interface for userspace to do erase operate */
static int simple_mmc_erase_func(unsigned int start, unsigned int size)
{
struct msdc_host *host;
unsigned int arg;
/* emmc always in slot0 */
host = msdc_get_host(MSDC_EMMC,MSDC_BOOT_EN,0);
BUG_ON(!host);
BUG_ON(!host->mmc);
BUG_ON(!host->mmc->card);
mmc_claim_host(host->mmc);
if (mmc_can_discard(host->mmc->card)) {
arg = __MMC_DISCARD_ARG;
} else if (mmc_can_trim(host->mmc->card)) {
arg = __MMC_TRIM_ARG;
} else if (mmc_can_erase(host->mmc->card)) {
/* mmc_erase() will remove the erase group un-aligned part,
* msdc_command_start() will do trim for old combo erase un-aligned issue
*/
arg = __MMC_ERASE_ARG;
} else {
pr_err("[%s]: emmc card can't support trim / discard / erase\n", __func__);
goto end;
}
pr_debug("[%s]: start=0x%x, size=%d, arg=0x%x, can_trim=(0x%x),EXT_CSD_SEC_GB_CL_EN=0x%lx\n",
__func__, start, size, arg, host->mmc->card->ext_csd.sec_feature_support,
EXT_CSD_SEC_GB_CL_EN);
mmc_erase(host->mmc->card, start, size, arg);
#if DEBUG_MMC_IOCTL
pr_debug("[%s]: erase done....arg=0x%x\n", __func__, arg);
#endif
end:
mmc_release_host(host->mmc);
return 0;
}
#endif
static int simple_mmc_erase_partition(unsigned char *name)
{
#ifdef CONFIG_MTK_EMMC_SUPPORT
struct mbr_part_info mbr_part;
int l_ret = -1;
BUG_ON(!name);
/* just support erase cache & data partition now */
if (('u' == *name && 's' == *(name + 1) && 'r' == *(name + 2) && 'd' == *(name + 3) &&
'a' == *(name + 4) && 't' == *(name + 5) && 'a' == *(name + 6)) ||
('c' == *name && 'a' == *(name + 1) && 'c' == *(name + 2) && 'h' == *(name + 3)
&& 'e' == *(name + 4))) {
/* find erase start address and erase size, just support high capacity emmc card now */
l_ret = simple_mmc_get_disk_info(&mbr_part, name);
if (l_ret == 0) {
/* do erase */
pr_debug("erase %s start sector: 0x%x size: 0x%x\n", mbr_part.part_name,
mbr_part.start_sector, mbr_part.nr_sects);
simple_mmc_erase_func(mbr_part.start_sector, mbr_part.nr_sects);
}
}
return 0;
#else
return 0;
#endif
}
static int simple_mmc_erase_partition_wrap(struct msdc_ioctl *msdc_ctl)
{
unsigned char name[PARTITION_NAME_LENGTH];
if(!msdc_ctl)
return -EINVAL;
if(msdc_ctl->total_size > PARTITION_NAME_LENGTH)
return -EFAULT;
if (copy_from_user(name, (unsigned char *)msdc_ctl->buffer, msdc_ctl->total_size))
return -EFAULT;
return simple_mmc_erase_partition(name);
}
static long simple_sd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
struct msdc_ioctl msdc_ctl;
int ret;
if((struct msdc_ioctl*)arg == NULL){
switch(cmd){
case MSDC_REINIT_SDCARD:
ret = sd_ioctl_reinit((struct msdc_ioctl*)arg);
break;
case MSDC_CD_PIN_EN_SDCARD:
ret = sd_ioctl_cd_pin_en((struct msdc_ioctl*)arg);
break;
default:
pr_err("mt_sd_ioctl:this opcode value is illegal!!\n");
return -EINVAL;
}
return ret;
}
else{
if (copy_from_user(&msdc_ctl, (struct msdc_ioctl*)arg, sizeof(struct msdc_ioctl))){
return -EFAULT;
}
switch (msdc_ctl.opcode){
case MSDC_SINGLE_READ_WRITE:
msdc_ctl.result = simple_sd_ioctl_single_rw(&msdc_ctl);
break;
case MSDC_MULTIPLE_READ_WRITE:
msdc_ctl.result = simple_sd_ioctl_multi_rw(&msdc_ctl);
break;
case MSDC_GET_CID:
msdc_ctl.result = simple_sd_ioctl_get_cid(&msdc_ctl);
break;
case MSDC_GET_CSD:
msdc_ctl.result = simple_sd_ioctl_get_csd(&msdc_ctl);
break;
case MSDC_GET_EXCSD:
msdc_ctl.result = simple_sd_ioctl_get_excsd(&msdc_ctl);
break;
case MSDC_DRIVING_SETTING:
pr_debug("in ioctl to change driving\n");
if (1 == msdc_ctl.iswrite){
msdc_ctl.result = simple_sd_ioctl_set_driving(&msdc_ctl);
} else {
msdc_ctl.result = simple_sd_ioctl_get_driving(&msdc_ctl);
}
break;
case MSDC_ERASE_PARTITION:
msdc_ctl.result = simple_mmc_erase_partition_wrap(&msdc_ctl);
break;
case MSDC_SD30_MODE_SWITCH:
msdc_ctl.result = simple_sd_ioctl_sd30_mode_switch(&msdc_ctl);
break;
default:
pr_err("simple_sd_ioctl:this opcode value is illegal!!\n");
return -EINVAL;
}
if (copy_to_user((struct msdc_ioctl*)arg, &msdc_ctl, sizeof(struct msdc_ioctl))) {
return -EFAULT;
}
return msdc_ctl.result;
}
}
static struct file_operations simple_msdc_em_fops = {
.owner = THIS_MODULE,
.unlocked_ioctl = simple_sd_ioctl,
.open = simple_sd_open,
};
static struct miscdevice simple_msdc_em_dev[] = {
{
.minor = MISC_DYNAMIC_MINOR,
.name = "misc-sd",
.fops = &simple_msdc_em_fops,
}
};
static int simple_sd_probe(struct platform_device *pdev)
{
int ret = 0;
pr_debug("in misc_sd_probe function\n");
return ret;
}
static int simple_sd_remove(struct platform_device *pdev)
{
return 0;
}
static struct platform_driver simple_sd_driver = {
.probe = simple_sd_probe,
.remove = simple_sd_remove,
.driver = {
.name = DRV_NAME_MISC,
.owner = THIS_MODULE,
},
};
static int __init simple_sd_init(void)
{
int ret;
sg_msdc_multi_buffer = (u32 *) kmalloc(64 * 1024, GFP_KERNEL);
if (sg_msdc_multi_buffer == NULL)
pr_err("allock 64KB memory failed\n");
ret = platform_driver_register(&simple_sd_driver);
if (ret) {
pr_err(DRV_NAME_MISC ": Can't register driver\n");
return ret;
}
pr_debug(DRV_NAME_MISC ": MediaTek simple SD/MMC Card Driver\n");
/*msdc0 is for emmc only, just for emmc */
/* ret = misc_register(&simple_msdc_em_dev[host->id]); */
ret = misc_register(&simple_msdc_em_dev[0]);
if (ret) {
pr_err("register MSDC Slot[0] misc driver failed (%d)\n", ret);
return ret;
}
return 0;
}
static void __exit simple_sd_exit(void)
{
if (sg_msdc_multi_buffer != NULL) {
kfree(sg_msdc_multi_buffer);
sg_msdc_multi_buffer = NULL;
}
misc_deregister(&simple_msdc_em_dev[0]);
platform_driver_unregister(&simple_sd_driver);
}
module_init(simple_sd_init);
module_exit(simple_sd_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("simple MediaTek SD/MMC Card Driver");
MODULE_AUTHOR("feifei.wang <feifei.wang@mediatek.com>");
|