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
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
|
/*
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2024-5-1 GuEe-GUI first version
*/
#include "dev_sdio_dm.h"
#include "dev_sdhci_dm.h"
#define DBG_TAG "sdhci.k1x"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#define CANDIDATE_WIN_NUM 3
#define SELECT_DELAY_NUM 9
#define WINDOW_1ST 0
#define WINDOW_2ND 1
#define WINDOW_3RD 2
#define RX_TUNING_WINDOW_THRESHOLD 80
#define RX_TUNING_DLINE_REG 0x09
#define TX_TUNING_DLINE_REG 0x00
#define TX_TUNING_DELAYCODE 127
#define BOOTPART_NOACC_DEFAULT 0
/* SDH registers define */
#define SDHC_OP_EXT_REG 0x108
#define OVRRD_CLK_OEN 0x0800
#define FORCE_CLK_ON 0x1000
#define SDHC_LEGACY_CTRL_REG 0x10c
#define GEN_PAD_CLK_ON 0x0040
#define SDHC_MMC_CTRL_REG 0x114
#define MISC_INT_EN 0x0002
#define MISC_INT 0x0004
#define ENHANCE_STROBE_EN 0x0100
#define MMC_HS400 0x0200
#define MMC_HS200 0x0400
#define MMC_CARD_MODE 0x1000
#define SDHC_TX_CFG_REG 0x11c
#define TX_INT_CLK_SEL 0x40000000
#define TX_MUX_SEL 0x80000000
#define SDHC_PHY_CTRL_REG 0x160
#define PHY_FUNC_EN 0x0001
#define PHY_PLL_LOCK 0x0002
#define HOST_LEGACY_MODE 0x80000000
#define SDHC_PHY_FUNC_REG 0x164
#define PHY_TEST_EN 0x0080
#define HS200_USE_RFIFO 0x8000
#define SDHC_PHY_DLLCFG 0x168
#define DLL_PREDLY_NUM 0x04
#define DLL_FULLDLY_RANGE 0x10
#define DLL_VREG_CTRL 0x40
#define DLL_ENABLE 0x80000000
#define DLL_REFRESH_SWEN_SHIFT 0x1c
#define DLL_REFRESH_SW_SHIFT 0x1d
#define SDHC_PHY_DLLCFG1 0x16c
#define DLL_REG2_CTRL 0x0c
#define DLL_REG3_CTRL_MASK 0xff
#define DLL_REG3_CTRL_SHIFT 0x10
#define DLL_REG2_CTRL_MASK 0xff
#define DLL_REG2_CTRL_SHIFT 0x08
#define DLL_REG1_CTRL 0x92
#define DLL_REG1_CTRL_MASK 0xff
#define DLL_REG1_CTRL_SHIFT 0x00
#define SDHC_PHY_DLLSTS 0x170
#define DLL_LOCK_STATE 0x01
#define SDHC_PHY_DLLSTS1 0x174
#define DLL_MASTER_DELAY_MASK 0xff
#define DLL_MASTER_DELAY_SHIFT 0x10
#define SDHC_PHY_PADCFG_REG 0x178
#define RX_BIAS_CTRL_SHIFT 0x5
#define PHY_DRIVE_SEL_SHIFT 0x0
#define PHY_DRIVE_SEL_MASK 0x7
#define PHY_DRIVE_SEL_DEFAULT 0x4
#define RPM_DELAY 50
#define MAX_74CLK_WAIT_COUNT 100
#define MMC1_IO_V18EN 0x04
#define AKEY_ASFAR 0xbaba
#define AKEY_ASSAR 0xeb10
/* Spacemit private quirk, shared with the DT binding. */
#define RT_SDHCI_QUIRK2_SET_AIB_MMC RT_BIT(27)
#define SDHC_RX_CFG_REG 0x118
#define RX_SDCLK_SEL0_MASK 0x03
#define RX_SDCLK_SEL0_SHIFT 0x00
#define RX_SDCLK_SEL0 0x02
#define RX_SDCLK_SEL1_MASK 0x03
#define RX_SDCLK_SEL1_SHIFT 0x02
#define RX_SDCLK_SEL1 0x01
#define SDHC_DLINE_CTRL_REG 0x130
#define DLINE_PU 0x01
#define RX_DLINE_CODE_MASK 0xff
#define RX_DLINE_CODE_SHIFT 0x10
#define TX_DLINE_CODE_MASK 0xff
#define TX_DLINE_CODE_SHIFT 0x18
#define SDHC_DLINE_CFG_REG 0x134
#define RX_DLINE_REG_MASK 0xff
#define RX_DLINE_REG_SHIFT 0x00
#define RX_DLINE_GAIN_MASK 0x1
#define RX_DLINE_GAIN_SHIFT 0x8
#define RX_DLINE_GAIN 0x1
#define TX_DLINE_REG_MASK 0xff
#define TX_DLINE_REG_SHIFT 0x10
#define SDHC_RX_TUNE_DELAY_MIN 0x0
#define SDHC_RX_TUNE_DELAY_MAX 0xff
#define SDHC_RX_TUNE_DELAY_STEP 0x1
#define MMC_CAP2_QUIRK_BREAK_SDR104 RT_BIT(30)
enum window_type
{
LEFT_WINDOW = 0,
MIDDLE_WINDOW = 1,
RIGHT_WINDOW = 2,
};
struct tuning_window
{
rt_uint8_t type;
rt_uint8_t min_delay;
rt_uint8_t max_delay;
};
struct rx_tuning
{
rt_uint8_t rx_dline_reg;
rt_uint8_t select_delay_num;
rt_uint8_t current_delay_index;
/* 0: biggest window, 1: bigger, 2: small */
struct tuning_window windows[CANDIDATE_WIN_NUM];
rt_uint8_t select_delay[SELECT_DELAY_NUM];
rt_uint32_t card_cid[4];
rt_uint8_t window_limit;
rt_uint8_t tuning_fail;
rt_uint8_t window_type;
};
struct k1x_sdhci_data
{
rt_uint32_t host_freq;
rt_uint32_t flags;
rt_uint32_t host_caps;
rt_uint32_t host_caps2;
rt_uint32_t host_caps_disable;
rt_uint32_t host_caps2_disable;
rt_uint32_t quirks;
rt_uint32_t quirks2;
rt_uint32_t pm_caps;
rt_uint32_t aib_mmc1_io_reg;
rt_uint32_t apbc_asfar_reg;
rt_uint32_t apbc_assar_reg;
rt_uint8_t tx_dline_reg;
rt_uint8_t tx_delaycode;
rt_uint8_t tx_delaycode_cnt;
rt_uint8_t tx_delaycode_array[2];
rt_uint8_t phy_driver_sel;
struct rx_tuning rxtuning;
rt_uint8_t need_reset_dllcfg1;
rt_uint32_t prev_dllcfg1;
rt_uint32_t curr_dllcfg1;
rt_uint32_t new_dllcfg1;
rt_uint8_t dllcfg1_odd_reset;
rt_uint32_t rx_tuning_freq;
};
struct k1x_sdhci
{
struct rt_sdhci_host *host;
struct rt_clk *clk_core;
struct rt_clk *clk_io;
struct rt_clk *clk_aib;
struct rt_reset_control *rstc;
unsigned char power_mode;
rt_bool_t pinctrl;
struct k1x_sdhci_data pdata;
};
static const rt_uint32_t tuning_patten4[16] =
{
0x00ff0fff, 0xccc3ccff, 0xffcc3cc3, 0xeffefffe,
0xddffdfff, 0xfbfffbff, 0xff7fffbf, 0xefbdf777,
0xf0fff0ff, 0x3cccfc0f, 0xcfcc33cc, 0xeeffefff,
0xfdfffdff, 0xffbfffdf, 0xfff7ffbb, 0xde7b7ff7,
};
static const rt_uint32_t tuning_patten8[32] =
{
0xff00ffff, 0x0000ffff, 0xccccffff, 0xcccc33cc,
0xcc3333cc, 0xffffcccc, 0xffffeeff, 0xffeeeeff,
0xffddffff, 0xddddffff, 0xbbffffff, 0xbbffffff,
0xffffffbb, 0xffffff77, 0x77ff7777, 0xffeeddbb,
0x00ffffff, 0x00ffffff, 0xccffff00, 0xcc33cccc,
0x3333cccc, 0xffcccccc, 0xffeeffff, 0xeeeeffff,
0xddffffff, 0xddffffff, 0xffffffdd, 0xffffffbb,
0xffffbbbb, 0xffff77ff, 0xff7777ff, 0xeeddbb77,
};
static void k1x_sdhci_set_clk_gate(struct rt_sdhci_host *host, rt_bool_t auto_gate)
{
unsigned int reg;
reg = rt_sdhci_readl(host, SDHC_OP_EXT_REG);
if (auto_gate)
{
reg &= ~(OVRRD_CLK_OEN | FORCE_CLK_ON);
}
else
{
reg |= OVRRD_CLK_OEN | FORCE_CLK_ON;
}
rt_sdhci_writel(host, reg, SDHC_OP_EXT_REG);
}
static rt_bool_t k1x_sdhci_card_busy(struct rt_mmc_host *mmc)
{
rt_bool_t res;
rt_uint32_t cmd, present_state;
struct rt_sdhci_host *host = rt_mmc_priv(mmc);
/* Check whether DAT[0] is 0 */
present_state = rt_sdhci_readl(host, RT_SDHCI_PRESENT_STATE);
res = !(present_state & RT_SDHCI_DATA_0_LVL_MASK);
if (host->mmc->caps2 & MMC_CAP2_NO_MMC)
{
cmd = RT_SDHCI_GET_CMD(rt_sdhci_readw(host, RT_SDHCI_COMMAND));
if (cmd == VOLTAGE_SWITCH && host->mmc->ios.signal_voltage == MMC_SIGNAL_VOLTAGE_180)
{
/* Recover the auto clock */
k1x_sdhci_set_clk_gate(host, RT_TRUE);
}
}
return res;
}
static void k1x_sdhci_enable_sdio_irq_nolock(struct rt_sdhci_host *host, rt_bool_t enable)
{
if (!(host->flags & RT_SDHCI_DEVICE_DEAD))
{
if (enable)
{
host->ier |= RT_SDHCI_INT_CARD_INT;
}
else
{
host->ier &= ~RT_SDHCI_INT_CARD_INT;
}
rt_sdhci_writel(host, host->ier, RT_SDHCI_INT_ENABLE);
rt_sdhci_writel(host, host->ier, RT_SDHCI_SIGNAL_ENABLE);
}
}
static void k1x_sdhci_enable_sdio_irq(struct rt_mmc_host *mmc, rt_bool_t enable)
{
rt_ubase_t level;
struct rt_sdhci_host *host = rt_mmc_priv(mmc);
level = rt_spin_lock_irqsave(&host->lock);
k1x_sdhci_enable_sdio_irq_nolock(host, enable);
rt_spin_unlock_irqrestore(&host->lock, level);
}
static void k1x_enable_sdio_irq(struct rt_mmc_host *mmc, int enable)
{
rt_ubase_t level;
struct rt_sdhci_host *host = rt_mmc_priv(mmc);
k1x_sdhci_enable_sdio_irq(mmc, enable);
/* Avoid to read the SDIO_CCCR_INTx */
level = rt_spin_lock_irqsave(&host->lock);
mmc->sdio_irq_pending = RT_TRUE;
rt_spin_unlock_irqrestore(&host->lock, level);
}
static void k1x_sdhci_set_clock(struct rt_sdhci_host *host, unsigned int clock)
{
rt_uint32_t reg, cmd;
struct rt_sdhci_pltfm_host *pltfm_host = rt_sdhci_priv(host);
struct k1x_sdhci *k1x = rt_sdhci_pltfm_priv(pltfm_host);
struct rt_mmc_host *mmc = host->mmc;
/*
* According to the SDHC_TX_CFG_REG(0x11c<bit>),
* set TX_INT_CLK_SEL to gurantee the hold time
* at default speed mode or HS/SDR12/SDR25/SDR50 mode.
*/
reg = rt_sdhci_readl(host, SDHC_TX_CFG_REG);
if (mmc->ios.timing == MMCSD_TIMING_LEGACY ||
mmc->ios.timing == MMCSD_TIMING_SD_HS ||
mmc->ios.timing == MMCSD_TIMING_UHS_SDR12 ||
mmc->ios.timing == MMCSD_TIMING_UHS_SDR25 ||
mmc->ios.timing == MMCSD_TIMING_UHS_SDR50 ||
mmc->ios.timing == MMCSD_TIMING_MMC_HS)
{
reg |= TX_INT_CLK_SEL;
}
else
{
reg &= ~TX_INT_CLK_SEL;
}
rt_sdhci_writel(host, reg, SDHC_TX_CFG_REG);
/* set pinctrl state */
if (k1x->pinctrl)
{
if (clock >= 200000000)
{
rt_pin_ctrl_confs_apply_by_name(mmc->parent, "fast");
}
else if (clock == 0 || host->mmc->ios.bus_width < MMC_BUS_WIDTH_4)
{
rt_pin_ctrl_confs_apply_by_name(mmc->parent, "debug");
}
else
{
rt_pin_ctrl_confs_apply_by_name(mmc->parent, "default");
}
}
rt_sdhci_set_clock(host, clock);
if (host->mmc->caps2 & MMC_CAP2_NO_MMC)
{
/*
* According to the SD spec, during a signal voltage level switch,
* the clock must be closed for 5 ms.
* then, the host starts providing clk at 1.8 and the host checks whether
* DAT[3:0] is high after 1ms clk.
*
* For the above goal, temporarily disable the auto clk
* and keep clk always on for 1ms.
*/
cmd = RT_SDHCI_GET_CMD(rt_sdhci_readw(host, RT_SDHCI_COMMAND));
if (cmd == VOLTAGE_SWITCH && host->mmc->ios.signal_voltage == MMC_SIGNAL_VOLTAGE_180)
{
/* Disable auto clock */
k1x_sdhci_set_clk_gate(host, 0);
}
}
};
static void k1x_sdhci_phy_dll_init(struct rt_sdhci_host *host)
{
rt_uint32_t reg;
/* Config dll_reg1 & dll_reg2 */
reg = rt_sdhci_readl(host, SDHC_PHY_DLLCFG);
reg |= DLL_PREDLY_NUM | DLL_FULLDLY_RANGE | DLL_VREG_CTRL;
rt_sdhci_writel(host, reg, SDHC_PHY_DLLCFG);
reg = rt_sdhci_readl(host, SDHC_PHY_DLLCFG1);
reg |= DLL_REG1_CTRL & DLL_REG1_CTRL_MASK;
rt_sdhci_writel(host, reg, SDHC_PHY_DLLCFG1);
/* Dll enable */
reg = rt_sdhci_readl(host, SDHC_PHY_DLLCFG);
reg |= DLL_ENABLE;
rt_sdhci_writel(host, reg, SDHC_PHY_DLLCFG);
/* Wait dll lock */
for (int i = 0; i < 100; ++i)
{
if (rt_sdhci_readl(host, SDHC_PHY_DLLSTS) & DLL_LOCK_STATE)
{
break;
}
rt_hw_us_delay(10);
}
}
static void k1x_sdhci_hs400_enhanced_strobe(struct rt_mmc_host *mmc, struct rt_mmcsd_io_cfg *ios)
{
rt_uint32_t reg;
struct rt_sdhci_host *host = rt_mmc_priv(mmc);
reg = rt_sdhci_readl(host, SDHC_MMC_CTRL_REG);
if (ios->enhanced_strobe)
{
reg |= ENHANCE_STROBE_EN;
}
else
{
reg &= ~ENHANCE_STROBE_EN;
}
rt_sdhci_writel(host, reg, SDHC_MMC_CTRL_REG);
if (ios->enhanced_strobe)
{
k1x_sdhci_phy_dll_init(host);
}
}
static int k1x_sdhci_start_signal_voltage_switch(struct rt_mmc_host *mmc, struct rt_mmcsd_io_cfg *ios)
{
return rt_sdhci_start_signal_voltage_switch(mmc, ios);
}
static void k1x_sw_rx_tuning_prepare(struct rt_sdhci_host *host, rt_uint8_t dline_reg)
{
rt_uint32_t reg;
struct rt_mmc_host *mmc = host->mmc;
struct rt_mmcsd_io_cfg ios = mmc->ios;
reg = rt_sdhci_readl(host, SDHC_DLINE_CFG_REG);
reg &= ~(RX_DLINE_REG_MASK << RX_DLINE_REG_SHIFT);
reg |= dline_reg << RX_DLINE_REG_SHIFT;
reg &= ~(RX_DLINE_GAIN_MASK << RX_DLINE_GAIN_SHIFT);
if (ios.timing == MMC_TIMING_UHS_SDR50 && (reg & 0x40))
{
reg |= RX_DLINE_GAIN << RX_DLINE_GAIN_SHIFT;
}
rt_sdhci_writel(host, reg, SDHC_DLINE_CFG_REG);
reg = rt_sdhci_readl(host, SDHC_DLINE_CTRL_REG);
reg |= DLINE_PU;
rt_sdhci_writel(host, reg, SDHC_DLINE_CTRL_REG);
rt_hw_us_delay(5);
reg = rt_sdhci_readl(host, SDHC_RX_CFG_REG);
reg &= ~(RX_SDCLK_SEL1_MASK << RX_SDCLK_SEL1_SHIFT);
reg |= RX_SDCLK_SEL1 << RX_SDCLK_SEL1_SHIFT;
rt_sdhci_writel(host, reg, SDHC_RX_CFG_REG);
if (mmc->ios.timing == MMC_TIMING_MMC_HS200)
{
reg = rt_sdhci_readl(host, SDHC_PHY_FUNC_REG);
reg |= HS200_USE_RFIFO;
rt_sdhci_writel(host, reg, SDHC_PHY_FUNC_REG);
}
}
static void k1x_sw_rx_set_delaycode(struct rt_sdhci_host *host, rt_uint32_t delay)
{
rt_uint32_t reg;
reg = rt_sdhci_readl(host, SDHC_DLINE_CTRL_REG);
reg &= ~(RX_DLINE_CODE_MASK << RX_DLINE_CODE_SHIFT);
reg |= (delay & RX_DLINE_CODE_MASK) << RX_DLINE_CODE_SHIFT;
rt_sdhci_writel(host, reg, SDHC_DLINE_CTRL_REG);
}
static void k1x_sw_tx_tuning_prepare(struct rt_sdhci_host *host)
{
rt_uint32_t reg;
/* Set TX_MUX_SEL */
reg = rt_sdhci_readl(host, SDHC_TX_CFG_REG);
reg |= TX_MUX_SEL;
rt_sdhci_writel(host, reg, SDHC_TX_CFG_REG);
reg = rt_sdhci_readl(host, SDHC_DLINE_CTRL_REG);
reg |= DLINE_PU;
rt_sdhci_writel(host, reg, SDHC_DLINE_CTRL_REG);
rt_hw_us_delay(5);
}
static void k1x_sw_tx_set_dlinereg(struct rt_sdhci_host *host, rt_uint8_t dline_reg)
{
rt_uint32_t reg;
reg = rt_sdhci_readl(host, SDHC_DLINE_CFG_REG);
reg &= ~(TX_DLINE_REG_MASK << TX_DLINE_REG_SHIFT);
reg |= dline_reg << TX_DLINE_REG_SHIFT;
rt_sdhci_writel(host, reg, SDHC_DLINE_CFG_REG);
}
static void k1x_sw_tx_set_delaycode(struct rt_sdhci_host *host, rt_uint32_t delay)
{
rt_uint32_t reg;
reg = rt_sdhci_readl(host, SDHC_DLINE_CTRL_REG);
reg &= ~(TX_DLINE_CODE_MASK << TX_DLINE_CODE_SHIFT);
reg |= (delay & TX_DLINE_CODE_MASK) << TX_DLINE_CODE_SHIFT;
rt_sdhci_writel(host, reg, SDHC_DLINE_CTRL_REG);
}
static void k1x_sdhci_clear_set_irqs(struct rt_sdhci_host *host, rt_uint32_t clr, rt_uint32_t set)
{
rt_uint32_t ier;
ier = rt_sdhci_readl(host, RT_SDHCI_INT_ENABLE);
ier &= ~clr;
ier |= set;
rt_sdhci_writel(host, ier, RT_SDHCI_INT_ENABLE);
rt_sdhci_writel(host, ier, RT_SDHCI_SIGNAL_ENABLE);
}
static int k1x_tuning_patten_check(struct rt_sdhci_host *host, int point)
{
int patten_len, err = 0;
rt_uint32_t read_patten, *tuning_patten;
if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
{
tuning_patten = (rt_uint32_t *)tuning_patten8;
patten_len = RT_ARRAY_SIZE(tuning_patten8);
}
else
{
tuning_patten = (rt_uint32_t *)tuning_patten4;
patten_len = RT_ARRAY_SIZE(tuning_patten4);
}
for (int i = 0; i < patten_len; ++i)
{
read_patten = rt_sdhci_readl(host, RT_SDHCI_BUFFER);
if (read_patten != tuning_patten[i])
{
++err;
}
}
return err;
}
static int k1x_send_tuning_cmd(struct rt_sdhci_host *host, rt_uint32_t opcode,
int point, rt_ubase_t level)
{
int err = 0;
rt_spin_unlock_irqrestore(&host->lock, level);
rt_sdhci_send_tuning(host, opcode);
level = rt_spin_lock_irqsave(&host->lock);
if (!host->tuning_done)
{
LOG_E("Timeout waiting for Buffer Read Ready interrupt "
"during tuning procedure, resetting CMD and DATA");
rt_sdhci_reset(host, RT_SDHCI_RESET_CMD | RT_SDHCI_RESET_DATA);
}
else
{
err = k1x_tuning_patten_check(host, point);
}
host->tuning_done = 0;
return err;
}
static int k1x_sw_rx_select_window(struct rt_sdhci_host *host, rt_uint32_t opcode)
{
int err = 0, min, max;
rt_uint16_t ctrl;
rt_uint32_t ier;
rt_ubase_t level = 0;
struct tuning_window tmp;
struct rt_mmc_host *mmc = host->mmc;
struct rt_sdhci_pltfm_host *pltfm_host = rt_sdhci_priv(host);
struct k1x_sdhci *k1x = rt_sdhci_pltfm_priv(pltfm_host);
struct k1x_sdhci_data *pdata = &k1x->pdata;
struct rx_tuning *rxtuning = &pdata->rxtuning;
/* Change to pio mode during the tuning stage */
level = rt_spin_lock_irqsave(&host->lock);
ier = rt_sdhci_readl(host, RT_SDHCI_INT_ENABLE);
k1x_sdhci_clear_set_irqs(host, ier, RT_SDHCI_INT_DATA_AVAIL);
min = SDHC_RX_TUNE_DELAY_MIN;
do {
/* Find the mininum delay first which can pass tuning */
while (min < SDHC_RX_TUNE_DELAY_MAX)
{
k1x_sw_rx_set_delaycode(host, min);
if (!mmc->ops->get_cd(mmc))
{
rt_spin_unlock_irqrestore(&host->lock, level);
return -RT_EIO;
}
err = k1x_send_tuning_cmd(host, opcode, min, level);
if (err == -RT_EIO)
{
rt_spin_unlock_irqrestore(&host->lock, level);
return -RT_EIO;
}
if (!err)
{
break;
}
ctrl = rt_sdhci_readw(host, RT_SDHCI_HOST_CONTROL2);
ctrl &= ~(RT_SDHCI_CTRL_TUNED_CLK | RT_SDHCI_CTRL_EXEC_TUNING);
rt_sdhci_writew(host, ctrl, RT_SDHCI_HOST_CONTROL2);
min += SDHC_RX_TUNE_DELAY_STEP;
}
/* find the maxinum delay which can not pass tuning */
max = min + SDHC_RX_TUNE_DELAY_STEP;
while (max < SDHC_RX_TUNE_DELAY_MAX)
{
k1x_sw_rx_set_delaycode(host, max);
if (!mmc->ops->get_cd(mmc))
{
rt_spin_unlock_irqrestore(&host->lock, level);
return -RT_EIO;
}
if ((err = k1x_send_tuning_cmd(host, opcode, max, level)))
{
ctrl = rt_sdhci_readw(host, RT_SDHCI_HOST_CONTROL2);
ctrl &= ~(RT_SDHCI_CTRL_TUNED_CLK | RT_SDHCI_CTRL_EXEC_TUNING);
rt_sdhci_writew(host, ctrl, RT_SDHCI_HOST_CONTROL2);
if (err == -RT_EIO)
{
rt_spin_unlock_irqrestore(&host->lock, level);
return -RT_EIO;
}
break;
}
max += SDHC_RX_TUNE_DELAY_STEP;
}
/* Store the top 3 window */
if ((max - min) >= rxtuning->window_limit)
{
tmp.max_delay = max;
tmp.min_delay = min;
tmp.type = pdata->rxtuning.window_type;
for (int i = 0; i < CANDIDATE_WIN_NUM; i++)
{
int len = rxtuning->windows[i].max_delay - rxtuning->windows[i].min_delay;
if ((tmp.max_delay - tmp.min_delay) > len)
{
for (int j = CANDIDATE_WIN_NUM - 1; j > i; j--)
{
rxtuning->windows[j] = rxtuning->windows[j-1];
}
rxtuning->windows[i] = tmp;
break;
}
}
}
min = max + SDHC_RX_TUNE_DELAY_STEP;
} while (min < SDHC_RX_TUNE_DELAY_MAX);
k1x_sdhci_clear_set_irqs(host, RT_SDHCI_INT_DATA_AVAIL, ier);
rt_spin_unlock_irqrestore(&host->lock, level);
return RT_EOK;
}
static int k1x_sw_rx_select_delay(struct rt_sdhci_host *host)
{
int win_len, min, max, mid;
struct tuning_window *window;
struct rt_sdhci_pltfm_host *pltfm_host = rt_sdhci_priv(host);
struct k1x_sdhci *k1x = rt_sdhci_pltfm_priv(pltfm_host);
struct k1x_sdhci_data *pdata = &k1x->pdata;
struct rx_tuning *tuning = &pdata->rxtuning;
for (int i = 0; i < CANDIDATE_WIN_NUM; ++i)
{
window = &tuning->windows[i];
min = window->min_delay;
max = window->max_delay;
mid = (min + max - 1) / 2;
win_len = max - min;
if (win_len < tuning->window_limit)
{
continue;
}
if (window->type == LEFT_WINDOW)
{
tuning->select_delay[tuning->select_delay_num++] = min + win_len / 4;
tuning->select_delay[tuning->select_delay_num++] = min + win_len / 3;
}
else if (window->type == RIGHT_WINDOW)
{
tuning->select_delay[tuning->select_delay_num++] = max - win_len / 4;
tuning->select_delay[tuning->select_delay_num++] = max - win_len / 3;
}
else
{
tuning->select_delay[tuning->select_delay_num++] = mid;
tuning->select_delay[tuning->select_delay_num++] = mid + win_len / 4;
tuning->select_delay[tuning->select_delay_num++] = mid - win_len / 4;
}
}
return tuning->select_delay_num;
}
static unsigned int k1x_sdhci_clk_get_max_clock(struct rt_sdhci_host *host)
{
struct rt_sdhci_pltfm_host *pltfm_host = rt_sdhci_priv(host);
return rt_clk_get_rate(pltfm_host->clk);
}
static unsigned int k1x_get_max_timeout_count(struct rt_sdhci_host *host)
{
/*
* The default sdhci code use the 1 << 27 as the max timeout counter
* to calculate the max_busy_timeout.
* aquilac sdhci support 1 << 29 as the timeout counter.
*/
return 1 << 29;
}
static void k1x_sdhci_reset(struct rt_sdhci_host *host, rt_uint8_t mask)
{
rt_uint32_t reg;
struct rt_sdhci_pltfm_host *pltfm_host = rt_sdhci_priv(host);
struct k1x_sdhci *k1x = rt_sdhci_pltfm_priv(pltfm_host);
struct k1x_sdhci_data *pdata = &k1x->pdata;
rt_sdhci_reset(host, mask);
if (mask != RT_SDHCI_RESET_ALL)
{
return;
}
reg = rt_sdhci_readl(host, SDHC_PHY_CTRL_REG);
reg |= (PHY_FUNC_EN | PHY_PLL_LOCK);
rt_sdhci_writel(host, reg, SDHC_PHY_CTRL_REG);
reg = rt_sdhci_readl(host, SDHC_PHY_PADCFG_REG);
reg |= (1 << RX_BIAS_CTRL_SHIFT);
reg &= ~(PHY_DRIVE_SEL_MASK);
reg |= (pdata->phy_driver_sel & PHY_DRIVE_SEL_MASK) << PHY_DRIVE_SEL_SHIFT;
rt_sdhci_writel(host, reg, SDHC_PHY_PADCFG_REG);
/* For EMMC */
if (!(host->mmc->caps2 & MMC_CAP2_NO_MMC))
{
/* MMC card mode */
reg = rt_sdhci_readl(host, SDHC_MMC_CTRL_REG);
reg |= MMC_CARD_MODE;
rt_sdhci_writel(host, reg, SDHC_MMC_CTRL_REG);
}
}
static void k1x_sdhci_set_uhs_signaling(struct rt_sdhci_host *host, unsigned timing)
{
rt_uint16_t reg;
if (timing == MMC_TIMING_MMC_HS200 || timing == MMC_TIMING_MMC_HS400)
{
reg = rt_sdhci_readw(host, SDHC_MMC_CTRL_REG);
reg |= (timing == MMC_TIMING_MMC_HS200) ? MMC_HS200 : MMC_HS400;
rt_sdhci_writew(host, reg, SDHC_MMC_CTRL_REG);
}
rt_sdhci_set_uhs(host, timing);
if (!(host->mmc->caps2 & MMC_CAP2_NO_SDIO))
{
reg = rt_sdhci_readw(host, RT_SDHCI_HOST_CONTROL2);
rt_sdhci_writew(host, reg | RT_SDHCI_CTRL_VDD_180, RT_SDHCI_HOST_CONTROL2);
}
}
static int k1x_sdhci_execute_sw_tuning(struct rt_sdhci_host *host, rt_uint32_t opcode)
{
struct rt_mmc_host *mmc = host->mmc;
struct rt_mmcsd_io_cfg ios = mmc->ios;
struct rt_sdhci_pltfm_host *pltfm_host = rt_sdhci_priv(host);
struct k1x_sdhci *k1x = rt_sdhci_pltfm_priv(pltfm_host);
struct k1x_sdhci_data *pdata = &k1x->pdata;
struct rx_tuning *rxtuning = &pdata->rxtuning;
/*
* Tuning is required for SDR50/SDR104, HS200/HS400 cards and
* if clock frequency is greater than 100MHz in these modes.
*/
if (host->clock < 100 * 1000 * 1000 ||
!((ios.timing == MMC_TIMING_MMC_HS200) ||
(ios.timing == MMC_TIMING_UHS_SDR50) ||
(ios.timing == MMC_TIMING_UHS_SDR104)))
{
return 0;
}
if (!(mmc->caps2 & MMC_CAP2_NO_SD) && !mmc->ops->get_cd(mmc))
{
return 0;
}
/* TX tuning config */
if (host->mmc->caps2 & MMC_CAP2_NO_MMC)
{
k1x_sw_tx_set_dlinereg(host, pdata->tx_dline_reg);
k1x_sw_tx_set_delaycode(host, pdata->tx_delaycode);
k1x_sw_tx_tuning_prepare(host);
}
rxtuning->select_delay_num = 0;
rxtuning->current_delay_index = 0;
rt_memset(rxtuning->windows, 0, sizeof(rxtuning->windows));
rt_memset(rxtuning->select_delay, 0xFF, sizeof(rxtuning->select_delay));
rt_memset(rxtuning->card_cid, 0, sizeof(rxtuning->card_cid));
/* Get pass window and caculate the select_delay */
k1x_sw_rx_tuning_prepare(host, rxtuning->rx_dline_reg);
if (k1x_sw_rx_select_window(host, opcode))
{
rxtuning->tuning_fail = 1;
return -RT_EIO;
}
if (!k1x_sw_rx_select_delay(host))
{
rxtuning->tuning_fail = 1;
return -RT_EIO;
}
/* Set the delay code and store card cid */
k1x_sw_rx_set_delaycode(host, rxtuning->select_delay[0]);
rxtuning->tuning_fail = 0;
return 0;
}
static rt_uint32_t k1x_handle_interrupt(struct rt_sdhci_host *host, rt_uint32_t intmask)
{
/* Handle sdio RT_SDHCI_INT_CARD_INT */
if ((intmask & RT_SDHCI_INT_CARD_INT) && (host->ier & RT_SDHCI_INT_CARD_INT))
{
if (!(host->flags & RT_SDHCI_DEVICE_DEAD))
{
host->ier &= ~RT_SDHCI_INT_CARD_INT;
rt_sdhci_writel(host, host->ier, RT_SDHCI_INT_ENABLE);
rt_sdhci_writel(host, host->ier, RT_SDHCI_SIGNAL_ENABLE);
}
/* Wakeup ksdioirqd thread */
host->mmc->sdio_irq_pending = RT_TRUE;
if (host->mmc->rthost.sdio_irq_thread)
{
rt_thread_resume(host->mmc->rthost.sdio_irq_thread);
}
}
/* Handle error interrupts */
if (intmask & RT_SDHCI_INT_ERROR)
{
if (intmask & (RT_SDHCI_INT_CRC | RT_SDHCI_INT_DATA_CRC | RT_SDHCI_INT_DATA_END_BIT | RT_SDHCI_INT_AUTO_CMD_ERR))
{
/* Handle crc error for sd device */
if (host->mmc->caps2 & MMC_CAP2_NO_MMC)
{
host->mmc->caps2 |= MMC_CAP2_QUIRK_BREAK_SDR104;
}
}
}
return intmask;
}
static void k1x_set_aib_mmc1_io(struct rt_sdhci_host *host, int signal_voltage)
{
void *aib_mmc1_io = RT_NULL;
void *apbc_asfar = RT_NULL;
void *apbc_assar = RT_NULL;
rt_uint32_t reg;
struct rt_sdhci_pltfm_host *pltfm_host = rt_sdhci_priv(host);
struct k1x_sdhci *k1x = rt_sdhci_pltfm_priv(pltfm_host);
struct k1x_sdhci_data *pdata = &k1x->pdata;
if (!pdata->aib_mmc1_io_reg || !pdata->apbc_asfar_reg ||
!pdata->apbc_assar_reg)
{
return;
}
aib_mmc1_io = rt_ioremap((void *)(rt_ubase_t)pdata->aib_mmc1_io_reg, 4);
apbc_asfar = rt_ioremap((void *)(rt_ubase_t)pdata->apbc_asfar_reg, 4);
apbc_assar = rt_ioremap((void *)(rt_ubase_t)pdata->apbc_assar_reg, 4);
if (!aib_mmc1_io || !apbc_asfar || !apbc_assar)
{
LOG_E("Map AIB voltage control registers failed");
goto _unmap;
}
HWREG32(apbc_asfar) = AKEY_ASFAR;
HWREG32(apbc_assar) = AKEY_ASSAR;
reg = HWREG32(aib_mmc1_io);
if (signal_voltage == MMC_SIGNAL_VOLTAGE_180)
{
reg |= MMC1_IO_V18EN;
}
else
{
reg &= ~MMC1_IO_V18EN;
}
HWREG32(apbc_asfar) = AKEY_ASFAR;
HWREG32(apbc_assar) = AKEY_ASSAR;
HWREG32(aib_mmc1_io) = reg;
rt_hw_dsb();
_unmap:
if (apbc_assar)
{
rt_iounmap(apbc_assar);
}
if (apbc_asfar)
{
rt_iounmap(apbc_asfar);
}
if (aib_mmc1_io)
{
rt_iounmap(aib_mmc1_io);
}
}
static void k1x_sdhci_voltage_switch(struct rt_sdhci_host *host)
{
if (host->quirks2 & RT_SDHCI_QUIRK2_SET_AIB_MMC)
{
k1x_set_aib_mmc1_io(host, host->mmc->rthost.io_cfg.signal_voltage);
}
}
static const struct rt_sdhci_ops k1x_sdhci_ops =
{
.set_clock = k1x_sdhci_set_clock,
.get_max_clock = k1x_sdhci_clk_get_max_clock,
.get_max_timeout_count = k1x_get_max_timeout_count,
.set_bus_width = rt_sdhci_set_bus_width,
.reset = k1x_sdhci_reset,
.set_uhs_signaling = k1x_sdhci_set_uhs_signaling,
.platform_execute_tuning = k1x_sdhci_execute_sw_tuning,
.irq = k1x_handle_interrupt,
.set_power = rt_sdhci_set_power_and_bus_voltage,
.voltage_switch = k1x_sdhci_voltage_switch,
};
static int k1x_sdhci_pre_select_hs400(struct rt_mmc_host *mmc)
{
rt_uint32_t reg;
struct rt_sdhci_host *host = rt_mmc_priv(mmc);
reg = rt_sdhci_readl(host, SDHC_MMC_CTRL_REG);
reg |= MMC_HS400;
rt_sdhci_writel(host, reg, SDHC_MMC_CTRL_REG);
return RT_EOK;
}
static void k1x_sdhci_post_select_hs400(struct rt_mmc_host *mmc)
{
struct rt_sdhci_host *host = rt_mmc_priv(mmc);
k1x_sdhci_phy_dll_init(host);
}
static void k1x_sdhci_pre_hs400_to_hs200(struct rt_mmc_host *mmc)
{
rt_uint32_t reg;
struct rt_sdhci_host *host = rt_mmc_priv(mmc);
reg = rt_sdhci_readl(host, SDHC_PHY_CTRL_REG);
reg &= ~(PHY_FUNC_EN | PHY_PLL_LOCK);
rt_sdhci_writel(host, reg, SDHC_PHY_CTRL_REG);
reg = rt_sdhci_readl(host, SDHC_MMC_CTRL_REG);
reg &= ~(MMC_HS400 | MMC_HS200 | ENHANCE_STROBE_EN);
rt_sdhci_writel(host, reg, SDHC_MMC_CTRL_REG);
reg = rt_sdhci_readl(host, SDHC_PHY_FUNC_REG);
reg &= ~HS200_USE_RFIFO;
rt_sdhci_writel(host, reg, SDHC_PHY_FUNC_REG);
rt_hw_us_delay(5);
reg = rt_sdhci_readl(host, SDHC_PHY_CTRL_REG);
reg |= PHY_FUNC_EN | PHY_PLL_LOCK;
rt_sdhci_writel(host, reg, SDHC_PHY_CTRL_REG);
}
static struct rt_sdhci_pltfm_data sdhci_k1x_pdata =
{
.ops = &k1x_sdhci_ops,
.quirks = RT_SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK
| RT_SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
| RT_SDHCI_QUIRK_32BIT_ADMA_SIZE
| RT_SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
.quirks2 = RT_SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
};
static void k1x_sdhci_request(struct rt_mmc_host *mmc,
struct rt_mmcsd_req *mrq)
{
struct rt_mmcsd_data *data = mrq->data;
void *original, *bounce;
rt_size_t length, alloc_length;
if (!data || !data->buf)
{
rt_sdhci_start_request(mmc, mrq);
return;
}
length = data->blks * data->blksize;
if (!(((rt_ubase_t)data->buf | length) & (RT_CPU_CACHE_LINE_SZ - 1)))
{
rt_sdhci_start_request(mmc, mrq);
return;
}
alloc_length = RT_ALIGN(length, RT_CPU_CACHE_LINE_SZ);
bounce = rt_malloc_align(alloc_length, RT_CPU_CACHE_LINE_SZ);
if (!bounce)
{
mrq->cmd->err = -RT_ENOMEM;
data->err = -RT_ENOMEM;
rt_mmc_request_done(mmc, mrq);
return;
}
original = data->buf;
if (data->flags & DATA_DIR_WRITE)
{
rt_memcpy(bounce, original, length);
}
else
{
rt_memset(bounce, 0, alloc_length);
}
data->buf = bounce;
rt_sdhci_start_request(mmc, mrq);
rt_sem_take(&mmc->rthost.sem_ack, RT_WAITING_FOREVER);
if ((data->flags & DATA_DIR_READ) && !mrq->cmd->err && !data->err)
{
rt_memcpy(original, bounce, length);
}
data->buf = original;
rt_free_align(bounce);
/* Pass the completion consumed above back to mmcsd_send_request(). */
rt_sem_release(&mmc->rthost.sem_ack);
}
#ifdef RT_USING_PM
static rt_err_t k1x_sdhci_pm_suspend(const struct rt_device *device, rt_uint8_t mode)
{
rt_uint32_t reg;
rt_ubase_t level;
struct k1x_sdhci *k1x = device->user_data;
struct rt_sdhci_host *host = k1x->host;
level = rt_spin_lock_irqsave(&host->lock);
host->runtime_suspended = RT_TRUE;
if (!(host->mmc->caps2 & MMC_CAP2_NO_MMC))
{
reg = rt_sdhci_readl(host, SDHC_PHY_CTRL_REG);
reg &= ~PHY_FUNC_EN;
rt_sdhci_writel(host, reg, SDHC_PHY_CTRL_REG);
}
rt_spin_unlock_irqrestore(&host->lock, level);
rt_clk_disable_unprepare(k1x->clk_io);
if (!rt_is_err(k1x->clk_aib))
{
rt_clk_disable_unprepare(k1x->clk_aib);
}
if (!rt_is_err(k1x->clk_core))
{
rt_clk_disable_unprepare(k1x->clk_core);
}
return RT_EOK;
}
static void k1x_sdhci_pm_resume(const struct rt_device *device, rt_uint8_t mode)
{
rt_uint32_t reg;
rt_ubase_t level;
struct k1x_sdhci *k1x = device->user_data;
struct rt_sdhci_host *host = k1x->host;
rt_clk_prepare_enable(k1x->clk_io);
if (!rt_is_err(k1x->clk_aib))
{
rt_clk_prepare_enable(k1x->clk_aib);
}
if (!rt_is_err(k1x->clk_core))
{
rt_clk_prepare_enable(k1x->clk_core);
}
level = rt_spin_lock_irqsave(&host->lock);
if (!(host->mmc->caps2 & MMC_CAP2_NO_MMC))
{
reg = rt_sdhci_readl(host, SDHC_PHY_CTRL_REG);
reg |= PHY_FUNC_EN;
rt_sdhci_writel(host, reg, SDHC_PHY_CTRL_REG);
}
host->runtime_suspended = RT_FALSE;
rt_spin_unlock_irqrestore(&host->lock, level);
}
static const struct rt_device_pm_ops k1x_sdhci_pm_ops =
{
.suspend = k1x_sdhci_pm_suspend,
.resume = k1x_sdhci_pm_resume,
};
#endif /* RT_USING_PM */
static rt_err_t k1x_sdhci_probe(struct rt_platform_device *pdev)
{
rt_err_t err;
rt_uint32_t val;
struct k1x_sdhci *k1x;
struct k1x_sdhci_data *pdata;
struct rt_sdhci_host *host;
struct rt_sdhci_pltfm_host *pltfm_host;
struct rt_device *dev = &pdev->parent;
host = rt_sdhci_pltfm_init(pdev, &sdhci_k1x_pdata, sizeof(*k1x));
if (!host)
{
return -RT_ERROR;
}
pltfm_host = rt_sdhci_priv(host);
k1x = rt_sdhci_pltfm_priv(pltfm_host);
pdata = &k1x->pdata;
k1x->clk_io = rt_clk_get_by_name(dev, "sdh-io");
if (rt_is_err(k1x->clk_io))
{
k1x->clk_io = rt_clk_get_by_index(dev, 0);
}
if (rt_is_err(k1x->clk_io))
{
err = rt_ptr_err(k1x->clk_io);
goto _fail;
}
pltfm_host->clk = k1x->clk_io;
if ((err = rt_clk_prepare_enable(k1x->clk_io)))
{
goto _free_clk_io;
}
k1x->clk_core = rt_clk_get_by_name(dev, "sdh-core");
if (!rt_is_err(k1x->clk_core))
{
if ((err = rt_clk_prepare_enable(k1x->clk_core)))
{
goto _free_clk_core;
}
}
k1x->clk_aib = rt_clk_get_by_name(dev, "aib-clk");
if (!rt_is_err(k1x->clk_aib))
{
if ((err = rt_clk_prepare_enable(k1x->clk_aib)))
{
goto _free_clk_aib;
}
}
k1x->rstc = rt_reset_control_get_by_index(dev, 0);
if (rt_is_err(k1x->rstc))
{
err = rt_ptr_err(k1x->rstc);
goto _free_clk_aib;
}
if ((err = rt_reset_control_deassert(k1x->rstc)))
{
goto _err_rst_get;
}
if ((err = rt_mmc_of_parse(host->mmc)))
{
goto _err_rst_deassert;
}
/* rt_mmc_of_parse() does not currently import this standard property. */
if (rt_dm_dev_prop_read_bool(dev, "no-mmc"))
{
host->mmc->caps2 |= MMC_CAP2_NO_MMC;
}
rt_sdhci_get_property(pdev);
/* SDH io clk */
if (!rt_dm_dev_prop_read_u32(dev, "spacemit,sdh-freq", &val))
{
pdata->host_freq = val;
}
if (!rt_dm_dev_prop_read_u32(dev, "spacemit,sdh-flags", &val))
{
pdata->flags |= val;
}
if (!rt_dm_dev_prop_read_u32(dev, "spacemit,sdh-host-caps", &val))
{
pdata->host_caps |= val;
}
if (!rt_dm_dev_prop_read_u32(dev, "spacemit,sdh-host-caps2", &val))
{
pdata->host_caps2 |= val;
}
if (!rt_dm_dev_prop_read_u32(dev, "spacemit,sdh-host-caps-disable", &val))
{
pdata->host_caps_disable |= val;
}
if (!rt_dm_dev_prop_read_u32(dev, "spacemit,sdh-host-caps2-disable", &val))
{
pdata->host_caps2_disable |= val;
}
if (!rt_dm_dev_prop_read_u32(dev, "spacemit,sdh-quirks", &val))
{
pdata->quirks |= val;
}
if (!rt_dm_dev_prop_read_u32(dev, "spacemit,sdh-quirks2", &val))
{
pdata->quirks2 |= val;
}
if (!rt_dm_dev_prop_read_u32(dev, "spacemit,aib_mmc1_io_reg", &val))
{
pdata->aib_mmc1_io_reg = val;
}
else
{
pdata->aib_mmc1_io_reg = 0x0;
}
if (!rt_dm_dev_prop_read_u32(dev, "spacemit,apbc_asfar_reg", &val))
{
pdata->apbc_asfar_reg = val;
}
else
{
pdata->apbc_asfar_reg = 0x0;
}
if (!rt_dm_dev_prop_read_u32(dev, "spacemit,apbc_assar_reg", &val))
{
pdata->apbc_assar_reg = val;
}
else
{
pdata->apbc_assar_reg = 0x0;
}
/* Read rx tuning dline_reg */
if (!rt_dm_dev_prop_read_u32(dev, "spacemit,rx_dline_reg", &val))
{
pdata->rxtuning.rx_dline_reg = (rt_uint8_t)val;
}
else
{
pdata->rxtuning.rx_dline_reg = RX_TUNING_DLINE_REG;
}
/* Read rx tuning window limit */
if (!rt_dm_dev_prop_read_u32(dev, "spacemit,rx_tuning_limit", &val))
{
pdata->rxtuning.window_limit = (rt_uint8_t)val;
}
else
{
pdata->rxtuning.window_limit = RX_TUNING_WINDOW_THRESHOLD;
}
/* Read rx tuning window type */
if (!rt_dm_dev_prop_read_u32(dev, "spacemit,rx_tuning_type", &val))
{
pdata->rxtuning.window_type = (rt_uint8_t)val;
}
else
{
pdata->rxtuning.window_type = MIDDLE_WINDOW;
}
/* TX tuning dline_reg */
if (!rt_dm_dev_prop_read_u32(dev, "spacemit,tx_dline_reg", &val))
{
pdata->tx_dline_reg = (rt_uint8_t)val;
}
else
{
pdata->tx_dline_reg = TX_TUNING_DLINE_REG;
}
rt_uint32_t tx_delaycode_array[RT_ARRAY_SIZE(pdata->tx_delaycode_array)];
int tx_delaycode_cnt = rt_dm_dev_prop_count_of_size(dev,
"spacemit,tx_delaycode", sizeof(rt_uint32_t));
if (tx_delaycode_cnt > 0)
{
if (tx_delaycode_cnt > RT_ARRAY_SIZE(pdata->tx_delaycode_array))
{
tx_delaycode_cnt = RT_ARRAY_SIZE(pdata->tx_delaycode_array);
}
if (rt_dm_dev_prop_read_u32_array_index(dev, "spacemit,tx_delaycode",
0, tx_delaycode_cnt, tx_delaycode_array) == tx_delaycode_cnt)
{
for (int i = 0; i < tx_delaycode_cnt; ++i)
{
pdata->tx_delaycode_array[i] = tx_delaycode_array[i];
}
pdata->tx_delaycode_cnt = tx_delaycode_cnt;
pdata->tx_delaycode = pdata->tx_delaycode_array[0];
}
else
{
pdata->tx_delaycode_array[0] = TX_TUNING_DELAYCODE;
pdata->tx_delaycode_cnt = 1;
pdata->tx_delaycode = pdata->tx_delaycode_array[0];
}
}
else
{
pdata->tx_delaycode_array[0] = TX_TUNING_DELAYCODE;
pdata->tx_delaycode_cnt = 1;
pdata->tx_delaycode = pdata->tx_delaycode_array[0];
}
/* PHY driver select */
if (!rt_dm_dev_prop_read_u32(dev, "spacemit,phy_driver_sel", &val))
{
pdata->phy_driver_sel = (rt_uint8_t)val;
}
else
{
pdata->phy_driver_sel = PHY_DRIVE_SEL_DEFAULT;
}
/* Read rx tuning cpufreq, unit 1000Hz */
if (!rt_dm_dev_prop_read_u32(dev, "spacemit,rx_tuning_freq", &val))
{
pdata->rx_tuning_freq = val;
}
if (pdata->quirks)
{
host->quirks |= pdata->quirks;
}
if (pdata->quirks2)
{
host->quirks2 |= pdata->quirks2;
}
if (pdata->host_caps)
{
host->mmc->caps |= pdata->host_caps;
}
if (pdata->host_caps2)
{
host->mmc->caps2 |= pdata->host_caps2;
}
if (pdata->pm_caps)
{
host->mmc->pm_caps |= pdata->pm_caps;
}
if (!(host->mmc->caps2 & MMC_CAP2_NO_MMC))
{
host->mmc_host_ops.hs400_prepare_ddr = k1x_sdhci_pre_select_hs400;
host->mmc_host_ops.hs400_complete = k1x_sdhci_post_select_hs400;
host->mmc_host_ops.hs400_downgrade = k1x_sdhci_pre_hs400_to_hs200;
if (host->mmc->caps2 & MMC_CAP2_HS400_ES)
{
host->mmc_host_ops.hs400_enhanced_strobe = k1x_sdhci_hs400_enhanced_strobe;
}
}
if (host->mmc->caps2 & MMC_CAP2_NO_MMC)
{
/* Break sdr104 */
if (host->mmc->caps2 & MMC_CAP2_QUIRK_BREAK_SDR104)
{
host->mmc->caps &= ~MMC_CAP_UHS_SDR104;
host->mmc->caps2 &= ~MMC_CAP2_QUIRK_BREAK_SDR104;
}
else
{
if (pdata->rxtuning.tuning_fail)
{
/* Fallback bus speed */
host->mmc->caps &= ~MMC_CAP_UHS_SDR104;
pdata->rxtuning.tuning_fail = 0;
}
else if (!(pdata->host_caps_disable & MMC_CAP_UHS_SDR104) &&
!(host->quirks2 & RT_SDHCI_QUIRK2_NO_1_8_V))
{
/* Recovery sdr104 capability */
host->mmc->caps |= MMC_CAP_UHS_SDR104;
}
}
}
if (!(host->mmc->caps2 & MMC_CAP2_NO_SDIO))
{
/* Disable MMC_CAP2_SDIO_IRQ_NOTHREAD */
host->mmc->caps2 &= ~MMC_CAP2_SDIO_IRQ_NOTHREAD;
/*
* Use the fake irq pending to avoid to read the SDIO_CCCR_INTx
* which sometimes return an abnormal value.
*/
host->mmc->sdio_irq_pending = RT_TRUE;
}
host->mmc_host_ops.request = k1x_sdhci_request;
host->mmc_host_ops.start_signal_voltage_switch = k1x_sdhci_start_signal_voltage_switch;
host->mmc_host_ops.card_busy = k1x_sdhci_card_busy;
host->mmc_host_ops.enable_sdio_irq = k1x_enable_sdio_irq;
/* Set IO clock rate */
if (pdata->host_freq)
{
if ((err = rt_clk_set_rate(k1x->clk_io, pdata->host_freq)))
{
goto _fail;
}
}
else
{
goto _fail;
}
if ((err = rt_sdhci_set_and_add_host(host)))
{
goto _fail;
}
if (pdata->host_caps_disable)
{
host->mmc->caps &= ~pdata->host_caps_disable;
}
if (pdata->host_caps2_disable)
{
host->mmc->caps2 &= ~pdata->host_caps2_disable;
}
if (host->mmc->caps2 & MMC_CAP2_NO_MMC)
{
k1x->pinctrl = RT_TRUE;
}
dev->user_data = k1x;
#ifdef RT_USING_PM
rt_pm_device_register(dev, &k1x_sdhci_pm_ops);
#endif
return RT_EOK;
_err_rst_deassert:
rt_reset_control_assert(k1x->rstc);
_err_rst_get:
rt_reset_control_put(k1x->rstc);
k1x->rstc = RT_NULL;
if (!rt_is_err(k1x->clk_aib))
{
rt_clk_disable_unprepare(k1x->clk_aib);
}
_free_clk_aib:
if (!rt_is_err(k1x->clk_aib))
{
rt_clk_put(k1x->clk_aib);
}
k1x->clk_aib = RT_NULL;
if (!rt_is_err(k1x->clk_core))
{
rt_clk_disable_unprepare(k1x->clk_core);
}
_free_clk_core:
if (!rt_is_err(k1x->clk_core))
{
rt_clk_put(k1x->clk_core);
}
k1x->clk_core = RT_NULL;
rt_clk_disable_unprepare(k1x->clk_io);
_free_clk_io:
rt_clk_put(k1x->clk_io);
k1x->clk_io = RT_NULL;
_fail:
rt_sdhci_pltfm_free(pdev);
return err;
}
static rt_err_t k1x_sdhci_remove(struct rt_platform_device *pdev)
{
struct rt_sdhci_pltfm_host *pltfm_host;
struct k1x_sdhci *k1x = pdev->parent.user_data;
#ifdef RT_USING_PM
rt_pm_device_unregister(&pdev->parent);
#endif
pltfm_host = rt_sdhci_priv(k1x->host);
pltfm_host->clk = RT_NULL;
rt_reset_control_assert(k1x->rstc);
rt_clk_disable_unprepare(k1x->clk_aib);
rt_clk_disable_unprepare(k1x->clk_io);
rt_clk_disable_unprepare(k1x->clk_core);
rt_reset_control_put(k1x->rstc);
rt_clk_put(k1x->clk_aib);
rt_clk_put(k1x->clk_io);
rt_clk_put(k1x->clk_core);
return rt_sdhci_pltfm_remove(pdev);
}
static const struct rt_ofw_node_id k1x_sdhci_ofw_ids[] =
{
{ .compatible = "spacemit,k1-x-sdhci" },
{ /* sentinel */ }
};
static struct rt_platform_driver k1x_sdhci_driver =
{
.name = "sdhci-iproc",
.ids = k1x_sdhci_ofw_ids,
.probe = k1x_sdhci_probe,
.remove = k1x_sdhci_remove,
};
RT_PLATFORM_DRIVER_EXPORT(k1x_sdhci_driver);
|