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
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
|
// SPDX-License-Identifier: GPL-2.0
/*
* (C) Copyright 2017 Rockchip Electronics Co., Ltd
*/
#include <common.h>
#include <bitfield.h>
#include <clk-uclass.h>
#include <dm.h>
#include <errno.h>
#include <log.h>
#include <malloc.h>
#include <syscon.h>
#include <asm/arch-rockchip/clock.h>
#include <asm/arch-rockchip/cru_px30.h>
#include <asm/arch-rockchip/hardware.h>
#include <asm/global_data.h>
#include <asm/io.h>
#include <dm/device-internal.h>
#include <dm/lists.h>
#include <dt-bindings/clock/px30-cru.h>
#include <linux/bitops.h>
#include <linux/delay.h>
DECLARE_GLOBAL_DATA_PTR;
enum {
VCO_MAX_HZ = 3200U * 1000000,
VCO_MIN_HZ = 800 * 1000000,
OUTPUT_MAX_HZ = 3200U * 1000000,
OUTPUT_MIN_HZ = 24 * 1000000,
};
#define PX30_VOP_PLL_LIMIT 600000000
#define PX30_PLL_RATE(_rate, _refdiv, _fbdiv, _postdiv1, \
_postdiv2, _dsmpd, _frac) \
{ \
.rate = _rate##U, \
.fbdiv = _fbdiv, \
.postdiv1 = _postdiv1, \
.refdiv = _refdiv, \
.postdiv2 = _postdiv2, \
.dsmpd = _dsmpd, \
.frac = _frac, \
}
#define PX30_CPUCLK_RATE(_rate, _aclk_div, _pclk_div) \
{ \
.rate = _rate##U, \
.aclk_div = _aclk_div, \
.pclk_div = _pclk_div, \
}
#define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1))
#define PX30_CLK_DUMP(_id, _name, _iscru) \
{ \
.id = _id, \
.name = _name, \
.is_cru = _iscru, \
}
static struct pll_rate_table px30_pll_rates[] = {
/* _mhz, _refdiv, _fbdiv, _postdiv1, _postdiv2, _dsmpd, _frac */
PX30_PLL_RATE(1200000000, 1, 50, 1, 1, 1, 0),
PX30_PLL_RATE(1188000000, 2, 99, 1, 1, 1, 0),
PX30_PLL_RATE(1100000000, 12, 550, 1, 1, 1, 0),
PX30_PLL_RATE(1008000000, 1, 84, 2, 1, 1, 0),
PX30_PLL_RATE(1000000000, 6, 500, 2, 1, 1, 0),
PX30_PLL_RATE(816000000, 1, 68, 2, 1, 1, 0),
PX30_PLL_RATE(600000000, 1, 75, 3, 1, 1, 0),
};
static struct cpu_rate_table px30_cpu_rates[] = {
PX30_CPUCLK_RATE(1200000000, 1, 5),
PX30_CPUCLK_RATE(1008000000, 1, 5),
PX30_CPUCLK_RATE(816000000, 1, 3),
PX30_CPUCLK_RATE(600000000, 1, 3),
PX30_CPUCLK_RATE(408000000, 1, 1),
};
static u8 pll_mode_shift[PLL_COUNT] = {
APLL_MODE_SHIFT, DPLL_MODE_SHIFT, CPLL_MODE_SHIFT,
NPLL_MODE_SHIFT, GPLL_MODE_SHIFT
};
static u32 pll_mode_mask[PLL_COUNT] = {
APLL_MODE_MASK, DPLL_MODE_MASK, CPLL_MODE_MASK,
NPLL_MODE_MASK, GPLL_MODE_MASK
};
static struct pll_rate_table auto_table;
static ulong px30_clk_get_pll_rate(struct px30_clk_priv *priv,
enum px30_pll_id pll_id);
static struct pll_rate_table *pll_clk_set_by_auto(u32 drate)
{
struct pll_rate_table *rate = &auto_table;
u32 ref_khz = OSC_HZ / KHz, refdiv, fbdiv = 0;
u32 postdiv1, postdiv2 = 1;
u32 fref_khz;
u32 diff_khz, best_diff_khz;
const u32 max_refdiv = 63, max_fbdiv = 3200, min_fbdiv = 16;
const u32 max_postdiv1 = 7, max_postdiv2 = 7;
u32 vco_khz;
u32 rate_khz = drate / KHz;
if (!drate) {
printf("%s: the frequency can't be 0 Hz\n", __func__);
return NULL;
}
postdiv1 = DIV_ROUND_UP(VCO_MIN_HZ / 1000, rate_khz);
if (postdiv1 > max_postdiv1) {
postdiv2 = DIV_ROUND_UP(postdiv1, max_postdiv1);
postdiv1 = DIV_ROUND_UP(postdiv1, postdiv2);
}
vco_khz = rate_khz * postdiv1 * postdiv2;
if (vco_khz < (VCO_MIN_HZ / KHz) || vco_khz > (VCO_MAX_HZ / KHz) ||
postdiv2 > max_postdiv2) {
printf("%s: Cannot find out a supported VCO for Freq (%uHz)\n",
__func__, rate_khz);
return NULL;
}
rate->postdiv1 = postdiv1;
rate->postdiv2 = postdiv2;
best_diff_khz = vco_khz;
for (refdiv = 1; refdiv < max_refdiv && best_diff_khz; refdiv++) {
fref_khz = ref_khz / refdiv;
fbdiv = vco_khz / fref_khz;
if (fbdiv >= max_fbdiv || fbdiv <= min_fbdiv)
continue;
diff_khz = vco_khz - fbdiv * fref_khz;
if (fbdiv + 1 < max_fbdiv && diff_khz > fref_khz / 2) {
fbdiv++;
diff_khz = fref_khz - diff_khz;
}
if (diff_khz >= best_diff_khz)
continue;
best_diff_khz = diff_khz;
rate->refdiv = refdiv;
rate->fbdiv = fbdiv;
}
if (best_diff_khz > 4 * (MHz / KHz)) {
printf("%s: Failed to match output frequency %u bestis %u Hz\n",
__func__, rate_khz,
best_diff_khz * KHz);
return NULL;
}
return rate;
}
static const struct pll_rate_table *get_pll_settings(unsigned long rate)
{
unsigned int rate_count = ARRAY_SIZE(px30_pll_rates);
int i;
for (i = 0; i < rate_count; i++) {
if (rate == px30_pll_rates[i].rate)
return &px30_pll_rates[i];
}
return pll_clk_set_by_auto(rate);
}
static const struct cpu_rate_table *get_cpu_settings(unsigned long rate)
{
unsigned int rate_count = ARRAY_SIZE(px30_cpu_rates);
int i;
for (i = 0; i < rate_count; i++) {
if (rate == px30_cpu_rates[i].rate)
return &px30_cpu_rates[i];
}
return NULL;
}
/*
* How to calculate the PLL(from TRM V0.3 Part 1 Page 63):
* Formulas also embedded within the Fractional PLL Verilog model:
* If DSMPD = 1 (DSM is disabled, "integer mode")
* FOUTVCO = FREF / REFDIV * FBDIV
* FOUTPOSTDIV = FOUTVCO / POSTDIV1 / POSTDIV2
* Where:
* FOUTVCO = Fractional PLL non-divided output frequency
* FOUTPOSTDIV = Fractional PLL divided output frequency
* (output of second post divider)
* FREF = Fractional PLL input reference frequency, (the OSC_HZ 24MHz input)
* REFDIV = Fractional PLL input reference clock divider
* FBDIV = Integer value programmed into feedback divide
*
*/
static int rkclk_set_pll(struct px30_pll *pll, unsigned int *mode,
enum px30_pll_id pll_id,
unsigned long drate)
{
const struct pll_rate_table *rate;
uint vco_hz, output_hz;
rate = get_pll_settings(drate);
if (!rate) {
printf("%s unsupport rate\n", __func__);
return -EINVAL;
}
/* All PLLs have same VCO and output frequency range restrictions. */
vco_hz = OSC_HZ / 1000 * rate->fbdiv / rate->refdiv * 1000;
output_hz = vco_hz / rate->postdiv1 / rate->postdiv2;
debug("PLL at %p: fb=%d, ref=%d, pst1=%d, pst2=%d, vco=%u Hz, output=%u Hz\n",
pll, rate->fbdiv, rate->refdiv, rate->postdiv1,
rate->postdiv2, vco_hz, output_hz);
assert(vco_hz >= VCO_MIN_HZ && vco_hz <= VCO_MAX_HZ &&
output_hz >= OUTPUT_MIN_HZ && output_hz <= OUTPUT_MAX_HZ);
/*
* When power on or changing PLL setting,
* we must force PLL into slow mode to ensure output stable clock.
*/
rk_clrsetreg(mode, pll_mode_mask[pll_id],
PLLMUX_FROM_XIN24M << pll_mode_shift[pll_id]);
/* use integer mode */
rk_setreg(&pll->con1, 1 << PLL_DSMPD_SHIFT);
/* Power down */
rk_setreg(&pll->con1, 1 << PLL_PD_SHIFT);
rk_clrsetreg(&pll->con0,
PLL_POSTDIV1_MASK | PLL_FBDIV_MASK,
(rate->postdiv1 << PLL_POSTDIV1_SHIFT) | rate->fbdiv);
rk_clrsetreg(&pll->con1, PLL_POSTDIV2_MASK | PLL_REFDIV_MASK,
(rate->postdiv2 << PLL_POSTDIV2_SHIFT |
rate->refdiv << PLL_REFDIV_SHIFT));
/* Power Up */
rk_clrreg(&pll->con1, 1 << PLL_PD_SHIFT);
/* waiting for pll lock */
while (!(readl(&pll->con1) & (1 << PLL_LOCK_STATUS_SHIFT)))
udelay(1);
rk_clrsetreg(mode, pll_mode_mask[pll_id],
PLLMUX_FROM_PLL << pll_mode_shift[pll_id]);
return 0;
}
static uint32_t rkclk_pll_get_rate(struct px30_pll *pll, unsigned int *mode,
enum px30_pll_id pll_id)
{
u32 refdiv, fbdiv, postdiv1, postdiv2;
u32 con, shift, mask;
con = readl(mode);
shift = pll_mode_shift[pll_id];
mask = pll_mode_mask[pll_id];
switch ((con & mask) >> shift) {
case PLLMUX_FROM_XIN24M:
return OSC_HZ;
case PLLMUX_FROM_PLL:
/* normal mode */
con = readl(&pll->con0);
postdiv1 = (con & PLL_POSTDIV1_MASK) >> PLL_POSTDIV1_SHIFT;
fbdiv = (con & PLL_FBDIV_MASK) >> PLL_FBDIV_SHIFT;
con = readl(&pll->con1);
postdiv2 = (con & PLL_POSTDIV2_MASK) >> PLL_POSTDIV2_SHIFT;
refdiv = (con & PLL_REFDIV_MASK) >> PLL_REFDIV_SHIFT;
return (24 * fbdiv / (refdiv * postdiv1 * postdiv2)) * 1000000;
case PLLMUX_FROM_RTC32K:
default:
return 32768;
}
}
static ulong px30_i2c_get_clk(struct px30_clk_priv *priv, ulong clk_id)
{
struct px30_cru *cru = priv->cru;
u32 div, con;
switch (clk_id) {
case SCLK_I2C0:
con = readl(&cru->clksel_con[49]);
div = con >> CLK_I2C0_DIV_CON_SHIFT & CLK_I2C_DIV_CON_MASK;
break;
case SCLK_I2C1:
con = readl(&cru->clksel_con[49]);
div = con >> CLK_I2C1_DIV_CON_SHIFT & CLK_I2C_DIV_CON_MASK;
break;
case SCLK_I2C2:
con = readl(&cru->clksel_con[50]);
div = con >> CLK_I2C2_DIV_CON_SHIFT & CLK_I2C_DIV_CON_MASK;
break;
case SCLK_I2C3:
con = readl(&cru->clksel_con[50]);
div = con >> CLK_I2C3_DIV_CON_SHIFT & CLK_I2C_DIV_CON_MASK;
break;
default:
printf("do not support this i2c bus\n");
return -EINVAL;
}
return DIV_TO_RATE(priv->gpll_hz, div);
}
static ulong px30_i2c_set_clk(struct px30_clk_priv *priv, ulong clk_id, uint hz)
{
struct px30_cru *cru = priv->cru;
int src_clk_div;
src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz);
assert(src_clk_div - 1 <= 127);
switch (clk_id) {
case SCLK_I2C0:
rk_clrsetreg(&cru->clksel_con[49],
CLK_I2C_DIV_CON_MASK << CLK_I2C0_DIV_CON_SHIFT |
CLK_I2C_PLL_SEL_MASK << CLK_I2C0_PLL_SEL_SHIFT,
(src_clk_div - 1) << CLK_I2C0_DIV_CON_SHIFT |
CLK_I2C_PLL_SEL_GPLL << CLK_I2C0_PLL_SEL_SHIFT);
break;
case SCLK_I2C1:
rk_clrsetreg(&cru->clksel_con[49],
CLK_I2C_DIV_CON_MASK << CLK_I2C1_DIV_CON_SHIFT |
CLK_I2C_PLL_SEL_MASK << CLK_I2C1_PLL_SEL_SHIFT,
(src_clk_div - 1) << CLK_I2C1_DIV_CON_SHIFT |
CLK_I2C_PLL_SEL_GPLL << CLK_I2C1_PLL_SEL_SHIFT);
break;
case SCLK_I2C2:
rk_clrsetreg(&cru->clksel_con[50],
CLK_I2C_DIV_CON_MASK << CLK_I2C2_DIV_CON_SHIFT |
CLK_I2C_PLL_SEL_MASK << CLK_I2C2_PLL_SEL_SHIFT,
(src_clk_div - 1) << CLK_I2C2_DIV_CON_SHIFT |
CLK_I2C_PLL_SEL_GPLL << CLK_I2C2_PLL_SEL_SHIFT);
break;
case SCLK_I2C3:
rk_clrsetreg(&cru->clksel_con[50],
CLK_I2C_DIV_CON_MASK << CLK_I2C3_DIV_CON_SHIFT |
CLK_I2C_PLL_SEL_MASK << CLK_I2C3_PLL_SEL_SHIFT,
(src_clk_div - 1) << CLK_I2C3_DIV_CON_SHIFT |
CLK_I2C_PLL_SEL_GPLL << CLK_I2C3_PLL_SEL_SHIFT);
break;
default:
printf("do not support this i2c bus\n");
return -EINVAL;
}
return px30_i2c_get_clk(priv, clk_id);
}
/*
* calculate best rational approximation for a given fraction
* taking into account restricted register size, e.g. to find
* appropriate values for a pll with 5 bit denominator and
* 8 bit numerator register fields, trying to set up with a
* frequency ratio of 3.1415, one would say:
*
* rational_best_approximation(31415, 10000,
* (1 << 8) - 1, (1 << 5) - 1, &n, &d);
*
* you may look at given_numerator as a fixed point number,
* with the fractional part size described in given_denominator.
*
* for theoretical background, see:
* http://en.wikipedia.org/wiki/Continued_fraction
*/
static void rational_best_approximation(unsigned long given_numerator,
unsigned long given_denominator,
unsigned long max_numerator,
unsigned long max_denominator,
unsigned long *best_numerator,
unsigned long *best_denominator)
{
unsigned long n, d, n0, d0, n1, d1;
n = given_numerator;
d = given_denominator;
n0 = 0;
d1 = 0;
n1 = 1;
d0 = 1;
for (;;) {
unsigned long t, a;
if (n1 > max_numerator || d1 > max_denominator) {
n1 = n0;
d1 = d0;
break;
}
if (d == 0)
break;
t = d;
a = n / d;
d = n % d;
n = t;
t = n0 + a * n1;
n0 = n1;
n1 = t;
t = d0 + a * d1;
d0 = d1;
d1 = t;
}
*best_numerator = n1;
*best_denominator = d1;
}
static ulong px30_i2s_get_clk(struct px30_clk_priv *priv, ulong clk_id)
{
u32 con, fracdiv, gate;
u32 clk_src = priv->gpll_hz / 2;
unsigned long m, n;
struct px30_cru *cru = priv->cru;
switch (clk_id) {
case SCLK_I2S1:
con = readl(&cru->clksel_con[30]);
fracdiv = readl(&cru->clksel_con[31]);
gate = readl(&cru->clkgate_con[10]);
m = fracdiv & CLK_I2S1_FRAC_NUMERATOR_MASK;
m >>= CLK_I2S1_FRAC_NUMERATOR_SHIFT;
n = fracdiv & CLK_I2S1_FRAC_DENOMINATOR_MASK;
n >>= CLK_I2S1_FRAC_DENOMINATOR_SHIFT;
debug("con30: 0x%x, gate: 0x%x, frac: 0x%x\n",
con, gate, fracdiv);
break;
default:
printf("do not support this i2s bus\n");
return -EINVAL;
}
return clk_src * n / m;
}
static ulong px30_i2s_set_clk(struct px30_clk_priv *priv, ulong clk_id, uint hz)
{
u32 clk_src;
unsigned long m, n, val;
struct px30_cru *cru = priv->cru;
clk_src = priv->gpll_hz / 2;
rational_best_approximation(hz, clk_src,
GENMASK(16 - 1, 0),
GENMASK(16 - 1, 0),
&m, &n);
switch (clk_id) {
case SCLK_I2S1:
rk_clrsetreg(&cru->clksel_con[30],
CLK_I2S1_PLL_SEL_MASK, CLK_I2S1_PLL_SEL_GPLL);
rk_clrsetreg(&cru->clksel_con[30],
CLK_I2S1_DIV_CON_MASK, 0x1);
rk_clrsetreg(&cru->clksel_con[30],
CLK_I2S1_SEL_MASK, CLK_I2S1_SEL_FRAC);
val = m << CLK_I2S1_FRAC_NUMERATOR_SHIFT | n;
writel(val, &cru->clksel_con[31]);
rk_clrsetreg(&cru->clkgate_con[10],
CLK_I2S1_OUT_MCLK_PAD_MASK,
CLK_I2S1_OUT_MCLK_PAD_ENABLE);
break;
default:
printf("do not support this i2s bus\n");
return -EINVAL;
}
return px30_i2s_get_clk(priv, clk_id);
}
static ulong px30_nandc_get_clk(struct px30_clk_priv *priv)
{
struct px30_cru *cru = priv->cru;
u32 div, con;
con = readl(&cru->clksel_con[15]);
div = (con & NANDC_DIV_MASK) >> NANDC_DIV_SHIFT;
return DIV_TO_RATE(priv->gpll_hz, div);
}
static ulong px30_nandc_set_clk(struct px30_clk_priv *priv,
ulong set_rate)
{
struct px30_cru *cru = priv->cru;
int src_clk_div;
/* Select nandc source from GPLL by default */
/* nandc clock defaulg div 2 internal, need provide double in cru */
src_clk_div = DIV_ROUND_UP(priv->gpll_hz, set_rate);
assert(src_clk_div - 1 <= 31);
rk_clrsetreg(&cru->clksel_con[15],
NANDC_CLK_SEL_MASK | NANDC_PLL_MASK |
NANDC_DIV_MASK,
NANDC_CLK_SEL_NANDC << NANDC_CLK_SEL_SHIFT |
NANDC_SEL_GPLL << NANDC_PLL_SHIFT |
(src_clk_div - 1) << NANDC_DIV_SHIFT);
return px30_nandc_get_clk(priv);
}
static ulong px30_mmc_get_clk(struct px30_clk_priv *priv, uint clk_id)
{
struct px30_cru *cru = priv->cru;
u32 div, con, con_id;
switch (clk_id) {
case HCLK_SDMMC:
case SCLK_SDMMC:
con_id = 16;
break;
case HCLK_EMMC:
case SCLK_EMMC:
case SCLK_EMMC_SAMPLE:
con_id = 20;
break;
default:
return -EINVAL;
}
con = readl(&cru->clksel_con[con_id]);
div = (con & EMMC_DIV_MASK) >> EMMC_DIV_SHIFT;
if ((con & EMMC_PLL_MASK) >> EMMC_PLL_SHIFT
== EMMC_SEL_24M)
return DIV_TO_RATE(OSC_HZ, div) / 2;
else
return DIV_TO_RATE(priv->gpll_hz, div) / 2;
}
static ulong px30_mmc_set_clk(struct px30_clk_priv *priv,
ulong clk_id, ulong set_rate)
{
struct px30_cru *cru = priv->cru;
int src_clk_div;
u32 con_id;
switch (clk_id) {
case HCLK_SDMMC:
case SCLK_SDMMC:
con_id = 16;
break;
case HCLK_EMMC:
case SCLK_EMMC:
con_id = 20;
break;
default:
return -EINVAL;
}
/* Select clk_sdmmc/emmc source from GPLL by default */
/* mmc clock defaulg div 2 internal, need provide double in cru */
src_clk_div = DIV_ROUND_UP(priv->gpll_hz / 2, set_rate);
if (src_clk_div > 127) {
/* use 24MHz source for 400KHz clock */
src_clk_div = DIV_ROUND_UP(OSC_HZ / 2, set_rate);
rk_clrsetreg(&cru->clksel_con[con_id],
EMMC_PLL_MASK | EMMC_DIV_MASK,
EMMC_SEL_24M << EMMC_PLL_SHIFT |
(src_clk_div - 1) << EMMC_DIV_SHIFT);
} else {
rk_clrsetreg(&cru->clksel_con[con_id],
EMMC_PLL_MASK | EMMC_DIV_MASK,
EMMC_SEL_GPLL << EMMC_PLL_SHIFT |
(src_clk_div - 1) << EMMC_DIV_SHIFT);
}
rk_clrsetreg(&cru->clksel_con[con_id + 1], EMMC_CLK_SEL_MASK,
EMMC_CLK_SEL_EMMC);
return px30_mmc_get_clk(priv, clk_id);
}
static ulong px30_sfc_get_clk(struct px30_clk_priv *priv, uint clk_id)
{
struct px30_cru *cru = priv->cru;
u32 div, con;
con = readl(&cru->clksel_con[22]);
div = (con & SFC_DIV_CON_MASK) >> SFC_DIV_CON_SHIFT;
return DIV_TO_RATE(priv->gpll_hz, div);
}
static ulong px30_sfc_set_clk(struct px30_clk_priv *priv,
ulong clk_id, ulong set_rate)
{
struct px30_cru *cru = priv->cru;
int src_clk_div;
src_clk_div = DIV_ROUND_UP(priv->gpll_hz, set_rate);
rk_clrsetreg(&cru->clksel_con[22],
SFC_PLL_SEL_MASK | SFC_DIV_CON_MASK,
0 << SFC_PLL_SEL_SHIFT |
(src_clk_div - 1) << SFC_DIV_CON_SHIFT);
return px30_sfc_get_clk(priv, clk_id);
}
static ulong px30_pwm_get_clk(struct px30_clk_priv *priv, ulong clk_id)
{
struct px30_cru *cru = priv->cru;
u32 div, con;
switch (clk_id) {
case SCLK_PWM0:
con = readl(&cru->clksel_con[52]);
div = con >> CLK_PWM0_DIV_CON_SHIFT & CLK_PWM_DIV_CON_MASK;
break;
case SCLK_PWM1:
con = readl(&cru->clksel_con[52]);
div = con >> CLK_PWM1_DIV_CON_SHIFT & CLK_PWM_DIV_CON_MASK;
break;
default:
printf("do not support this pwm bus\n");
return -EINVAL;
}
return DIV_TO_RATE(priv->gpll_hz, div);
}
static ulong px30_pwm_set_clk(struct px30_clk_priv *priv, ulong clk_id, uint hz)
{
struct px30_cru *cru = priv->cru;
int src_clk_div;
src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz);
assert(src_clk_div - 1 <= 127);
switch (clk_id) {
case SCLK_PWM0:
rk_clrsetreg(&cru->clksel_con[52],
CLK_PWM_DIV_CON_MASK << CLK_PWM0_DIV_CON_SHIFT |
CLK_PWM_PLL_SEL_MASK << CLK_PWM0_PLL_SEL_SHIFT,
(src_clk_div - 1) << CLK_PWM0_DIV_CON_SHIFT |
CLK_PWM_PLL_SEL_GPLL << CLK_PWM0_PLL_SEL_SHIFT);
break;
case SCLK_PWM1:
rk_clrsetreg(&cru->clksel_con[52],
CLK_PWM_DIV_CON_MASK << CLK_PWM1_DIV_CON_SHIFT |
CLK_PWM_PLL_SEL_MASK << CLK_PWM1_PLL_SEL_SHIFT,
(src_clk_div - 1) << CLK_PWM1_DIV_CON_SHIFT |
CLK_PWM_PLL_SEL_GPLL << CLK_PWM1_PLL_SEL_SHIFT);
break;
default:
printf("do not support this pwm bus\n");
return -EINVAL;
}
return px30_pwm_get_clk(priv, clk_id);
}
static ulong px30_saradc_get_clk(struct px30_clk_priv *priv)
{
struct px30_cru *cru = priv->cru;
u32 div, con;
con = readl(&cru->clksel_con[55]);
div = con >> CLK_SARADC_DIV_CON_SHIFT & CLK_SARADC_DIV_CON_MASK;
return DIV_TO_RATE(OSC_HZ, div);
}
static ulong px30_saradc_set_clk(struct px30_clk_priv *priv, uint hz)
{
struct px30_cru *cru = priv->cru;
int src_clk_div;
src_clk_div = DIV_ROUND_UP(OSC_HZ, hz);
assert(src_clk_div - 1 <= 2047);
rk_clrsetreg(&cru->clksel_con[55],
CLK_SARADC_DIV_CON_MASK,
(src_clk_div - 1) << CLK_SARADC_DIV_CON_SHIFT);
return px30_saradc_get_clk(priv);
}
static ulong px30_tsadc_get_clk(struct px30_clk_priv *priv)
{
struct px30_cru *cru = priv->cru;
u32 div, con;
con = readl(&cru->clksel_con[54]);
div = con >> CLK_SARADC_DIV_CON_SHIFT & CLK_SARADC_DIV_CON_MASK;
return DIV_TO_RATE(OSC_HZ, div);
}
static ulong px30_tsadc_set_clk(struct px30_clk_priv *priv, uint hz)
{
struct px30_cru *cru = priv->cru;
int src_clk_div;
src_clk_div = DIV_ROUND_UP(OSC_HZ, hz);
assert(src_clk_div - 1 <= 2047);
rk_clrsetreg(&cru->clksel_con[54],
CLK_SARADC_DIV_CON_MASK,
(src_clk_div - 1) << CLK_SARADC_DIV_CON_SHIFT);
return px30_tsadc_get_clk(priv);
}
static ulong px30_spi_get_clk(struct px30_clk_priv *priv, ulong clk_id)
{
struct px30_cru *cru = priv->cru;
u32 div, con;
switch (clk_id) {
case SCLK_SPI0:
con = readl(&cru->clksel_con[53]);
div = con >> CLK_SPI0_DIV_CON_SHIFT & CLK_SPI_DIV_CON_MASK;
break;
case SCLK_SPI1:
con = readl(&cru->clksel_con[53]);
div = con >> CLK_SPI1_DIV_CON_SHIFT & CLK_SPI_DIV_CON_MASK;
break;
default:
printf("do not support this pwm bus\n");
return -EINVAL;
}
return DIV_TO_RATE(priv->gpll_hz, div);
}
static ulong px30_spi_set_clk(struct px30_clk_priv *priv, ulong clk_id, uint hz)
{
struct px30_cru *cru = priv->cru;
int src_clk_div;
src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz);
assert(src_clk_div - 1 <= 127);
switch (clk_id) {
case SCLK_SPI0:
rk_clrsetreg(&cru->clksel_con[53],
CLK_SPI_DIV_CON_MASK << CLK_SPI0_DIV_CON_SHIFT |
CLK_SPI_PLL_SEL_MASK << CLK_SPI0_PLL_SEL_SHIFT,
(src_clk_div - 1) << CLK_SPI0_DIV_CON_SHIFT |
CLK_SPI_PLL_SEL_GPLL << CLK_SPI0_PLL_SEL_SHIFT);
break;
case SCLK_SPI1:
rk_clrsetreg(&cru->clksel_con[53],
CLK_SPI_DIV_CON_MASK << CLK_SPI1_DIV_CON_SHIFT |
CLK_SPI_PLL_SEL_MASK << CLK_SPI1_PLL_SEL_SHIFT,
(src_clk_div - 1) << CLK_SPI1_DIV_CON_SHIFT |
CLK_SPI_PLL_SEL_GPLL << CLK_SPI1_PLL_SEL_SHIFT);
break;
default:
printf("do not support this pwm bus\n");
return -EINVAL;
}
return px30_spi_get_clk(priv, clk_id);
}
static ulong px30_vop_get_clk(struct px30_clk_priv *priv, ulong clk_id)
{
struct px30_cru *cru = priv->cru;
u32 div, con, parent;
switch (clk_id) {
case ACLK_VOPB:
case ACLK_VOPL:
con = readl(&cru->clksel_con[3]);
div = con & ACLK_VO_DIV_MASK;
parent = priv->gpll_hz;
break;
case DCLK_VOPB:
con = readl(&cru->clksel_con[5]);
div = con & DCLK_VOPB_DIV_MASK;
parent = rkclk_pll_get_rate(&cru->pll[CPLL], &cru->mode, CPLL);
break;
case DCLK_VOPL:
con = readl(&cru->clksel_con[8]);
div = con & DCLK_VOPL_DIV_MASK;
parent = rkclk_pll_get_rate(&cru->pll[NPLL], &cru->mode, NPLL);
break;
default:
return -ENOENT;
}
return DIV_TO_RATE(parent, div);
}
static ulong px30_vop_set_clk(struct px30_clk_priv *priv, ulong clk_id, uint hz)
{
struct px30_cru *cru = priv->cru;
ulong npll_hz;
int src_clk_div;
switch (clk_id) {
case ACLK_VOPB:
case ACLK_VOPL:
src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz);
assert(src_clk_div - 1 <= 31);
rk_clrsetreg(&cru->clksel_con[3],
ACLK_VO_PLL_MASK | ACLK_VO_DIV_MASK,
ACLK_VO_SEL_GPLL << ACLK_VO_PLL_SHIFT |
(src_clk_div - 1) << ACLK_VO_DIV_SHIFT);
break;
case DCLK_VOPB:
if (hz < PX30_VOP_PLL_LIMIT) {
src_clk_div = DIV_ROUND_UP(PX30_VOP_PLL_LIMIT, hz);
if (src_clk_div % 2)
src_clk_div = src_clk_div - 1;
} else {
src_clk_div = 1;
}
assert(src_clk_div - 1 <= 255);
rkclk_set_pll(&cru->pll[CPLL], &cru->mode,
CPLL, hz * src_clk_div);
rk_clrsetreg(&cru->clksel_con[5],
DCLK_VOPB_SEL_MASK | DCLK_VOPB_PLL_SEL_MASK |
DCLK_VOPB_DIV_MASK,
DCLK_VOPB_SEL_DIVOUT << DCLK_VOPB_SEL_SHIFT |
DCLK_VOPB_PLL_SEL_CPLL << DCLK_VOPB_PLL_SEL_SHIFT |
(src_clk_div - 1) << DCLK_VOPB_DIV_SHIFT);
break;
case DCLK_VOPL:
npll_hz = px30_clk_get_pll_rate(priv, NPLL);
if (npll_hz >= PX30_VOP_PLL_LIMIT && npll_hz >= hz &&
npll_hz % hz == 0) {
src_clk_div = npll_hz / hz;
assert(src_clk_div - 1 <= 255);
} else {
if (hz < PX30_VOP_PLL_LIMIT) {
src_clk_div = DIV_ROUND_UP(PX30_VOP_PLL_LIMIT,
hz);
if (src_clk_div % 2)
src_clk_div = src_clk_div - 1;
} else {
src_clk_div = 1;
}
assert(src_clk_div - 1 <= 255);
rkclk_set_pll(&cru->pll[NPLL], &cru->mode, NPLL,
hz * src_clk_div);
}
rk_clrsetreg(&cru->clksel_con[8],
DCLK_VOPL_SEL_MASK | DCLK_VOPL_PLL_SEL_MASK |
DCLK_VOPL_DIV_MASK,
DCLK_VOPL_SEL_DIVOUT << DCLK_VOPL_SEL_SHIFT |
DCLK_VOPL_PLL_SEL_NPLL << DCLK_VOPL_PLL_SEL_SHIFT |
(src_clk_div - 1) << DCLK_VOPL_DIV_SHIFT);
break;
default:
printf("do not support this vop freq\n");
return -EINVAL;
}
return px30_vop_get_clk(priv, clk_id);
}
static ulong px30_bus_get_clk(struct px30_clk_priv *priv, ulong clk_id)
{
struct px30_cru *cru = priv->cru;
u32 div, con, parent;
switch (clk_id) {
case ACLK_BUS_PRE:
con = readl(&cru->clksel_con[23]);
div = (con & BUS_ACLK_DIV_MASK) >> BUS_ACLK_DIV_SHIFT;
parent = priv->gpll_hz;
break;
case HCLK_BUS_PRE:
con = readl(&cru->clksel_con[24]);
div = (con & BUS_HCLK_DIV_MASK) >> BUS_HCLK_DIV_SHIFT;
parent = priv->gpll_hz;
break;
case PCLK_BUS_PRE:
case PCLK_WDT_NS:
parent = px30_bus_get_clk(priv, ACLK_BUS_PRE);
con = readl(&cru->clksel_con[24]);
div = (con & BUS_PCLK_DIV_MASK) >> BUS_PCLK_DIV_SHIFT;
break;
default:
return -ENOENT;
}
return DIV_TO_RATE(parent, div);
}
static ulong px30_bus_set_clk(struct px30_clk_priv *priv, ulong clk_id,
ulong hz)
{
struct px30_cru *cru = priv->cru;
int src_clk_div;
/*
* select gpll as pd_bus bus clock source and
* set up dependent divisors for PCLK/HCLK and ACLK clocks.
*/
switch (clk_id) {
case ACLK_BUS_PRE:
src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz);
assert(src_clk_div - 1 <= 31);
rk_clrsetreg(&cru->clksel_con[23],
BUS_PLL_SEL_MASK | BUS_ACLK_DIV_MASK,
BUS_PLL_SEL_GPLL << BUS_PLL_SEL_SHIFT |
(src_clk_div - 1) << BUS_ACLK_DIV_SHIFT);
break;
case HCLK_BUS_PRE:
src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz);
assert(src_clk_div - 1 <= 31);
rk_clrsetreg(&cru->clksel_con[24],
BUS_PLL_SEL_MASK | BUS_HCLK_DIV_MASK,
BUS_PLL_SEL_GPLL << BUS_PLL_SEL_SHIFT |
(src_clk_div - 1) << BUS_HCLK_DIV_SHIFT);
break;
case PCLK_BUS_PRE:
src_clk_div =
DIV_ROUND_UP(px30_bus_get_clk(priv, ACLK_BUS_PRE), hz);
assert(src_clk_div - 1 <= 3);
rk_clrsetreg(&cru->clksel_con[24],
BUS_PCLK_DIV_MASK,
(src_clk_div - 1) << BUS_PCLK_DIV_SHIFT);
break;
default:
printf("do not support this bus freq\n");
return -EINVAL;
}
return px30_bus_get_clk(priv, clk_id);
}
static ulong px30_peri_get_clk(struct px30_clk_priv *priv, ulong clk_id)
{
struct px30_cru *cru = priv->cru;
u32 div, con, parent;
switch (clk_id) {
case ACLK_PERI_PRE:
con = readl(&cru->clksel_con[14]);
div = (con & PERI_ACLK_DIV_MASK) >> PERI_ACLK_DIV_SHIFT;
parent = priv->gpll_hz;
break;
case HCLK_PERI_PRE:
con = readl(&cru->clksel_con[14]);
div = (con & PERI_HCLK_DIV_MASK) >> PERI_HCLK_DIV_SHIFT;
parent = priv->gpll_hz;
break;
default:
return -ENOENT;
}
return DIV_TO_RATE(parent, div);
}
static ulong px30_peri_set_clk(struct px30_clk_priv *priv, ulong clk_id,
ulong hz)
{
struct px30_cru *cru = priv->cru;
int src_clk_div;
src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz);
assert(src_clk_div - 1 <= 31);
/*
* select gpll as pd_peri bus clock source and
* set up dependent divisors for HCLK and ACLK clocks.
*/
switch (clk_id) {
case ACLK_PERI_PRE:
rk_clrsetreg(&cru->clksel_con[14],
PERI_PLL_SEL_MASK | PERI_ACLK_DIV_MASK,
PERI_PLL_GPLL << PERI_PLL_SEL_SHIFT |
(src_clk_div - 1) << PERI_ACLK_DIV_SHIFT);
break;
case HCLK_PERI_PRE:
rk_clrsetreg(&cru->clksel_con[14],
PERI_PLL_SEL_MASK | PERI_HCLK_DIV_MASK,
PERI_PLL_GPLL << PERI_PLL_SEL_SHIFT |
(src_clk_div - 1) << PERI_HCLK_DIV_SHIFT);
break;
default:
printf("do not support this peri freq\n");
return -EINVAL;
}
return px30_peri_get_clk(priv, clk_id);
}
#ifndef CONFIG_SPL_BUILD
static ulong px30_crypto_get_clk(struct px30_clk_priv *priv, ulong clk_id)
{
struct px30_cru *cru = priv->cru;
u32 div, con, parent;
switch (clk_id) {
case SCLK_CRYPTO:
con = readl(&cru->clksel_con[25]);
div = (con & CRYPTO_DIV_MASK) >> CRYPTO_DIV_SHIFT;
parent = priv->gpll_hz;
break;
case SCLK_CRYPTO_APK:
con = readl(&cru->clksel_con[25]);
div = (con & CRYPTO_APK_DIV_MASK) >> CRYPTO_APK_DIV_SHIFT;
parent = priv->gpll_hz;
break;
default:
return -ENOENT;
}
return DIV_TO_RATE(parent, div);
}
static ulong px30_crypto_set_clk(struct px30_clk_priv *priv, ulong clk_id,
ulong hz)
{
struct px30_cru *cru = priv->cru;
int src_clk_div;
src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz);
assert(src_clk_div - 1 <= 31);
/*
* select gpll as crypto clock source and
* set up dependent divisors for crypto clocks.
*/
switch (clk_id) {
case SCLK_CRYPTO:
rk_clrsetreg(&cru->clksel_con[25],
CRYPTO_PLL_SEL_MASK | CRYPTO_DIV_MASK,
CRYPTO_PLL_SEL_GPLL << CRYPTO_PLL_SEL_SHIFT |
(src_clk_div - 1) << CRYPTO_DIV_SHIFT);
break;
case SCLK_CRYPTO_APK:
rk_clrsetreg(&cru->clksel_con[25],
CRYPTO_APK_PLL_SEL_MASK | CRYPTO_APK_DIV_MASK,
CRYPTO_PLL_SEL_GPLL << CRYPTO_APK_SEL_SHIFT |
(src_clk_div - 1) << CRYPTO_APK_DIV_SHIFT);
break;
default:
printf("do not support this peri freq\n");
return -EINVAL;
}
return px30_crypto_get_clk(priv, clk_id);
}
static ulong px30_i2s1_mclk_get_clk(struct px30_clk_priv *priv, ulong clk_id)
{
struct px30_cru *cru = priv->cru;
u32 con;
con = readl(&cru->clksel_con[30]);
if (!(con & CLK_I2S1_OUT_SEL_MASK))
return -ENOENT;
return 12000000;
}
static ulong px30_i2s1_mclk_set_clk(struct px30_clk_priv *priv, ulong clk_id,
ulong hz)
{
struct px30_cru *cru = priv->cru;
if (hz != 12000000) {
printf("do not support this i2s1_mclk freq\n");
return -EINVAL;
}
rk_clrsetreg(&cru->clksel_con[30], CLK_I2S1_OUT_SEL_MASK,
CLK_I2S1_OUT_SEL_OSC);
rk_clrsetreg(&cru->clkgate_con[10], CLK_I2S1_OUT_MCLK_PAD_MASK,
CLK_I2S1_OUT_MCLK_PAD_ENABLE);
return px30_i2s1_mclk_get_clk(priv, clk_id);
}
static ulong px30_mac_set_clk(struct px30_clk_priv *priv, uint hz)
{
struct px30_cru *cru = priv->cru;
u32 con = readl(&cru->clksel_con[22]);
ulong pll_rate;
u8 div;
if ((con >> GMAC_PLL_SEL_SHIFT) & GMAC_PLL_SEL_CPLL)
pll_rate = px30_clk_get_pll_rate(priv, CPLL);
else if ((con >> GMAC_PLL_SEL_SHIFT) & GMAC_PLL_SEL_NPLL)
pll_rate = px30_clk_get_pll_rate(priv, NPLL);
else
pll_rate = priv->gpll_hz;
/*default set 50MHZ for gmac*/
if (!hz)
hz = 50000000;
div = DIV_ROUND_UP(pll_rate, hz) - 1;
assert(div < 32);
rk_clrsetreg(&cru->clksel_con[22], CLK_GMAC_DIV_MASK,
div << CLK_GMAC_DIV_SHIFT);
return DIV_TO_RATE(pll_rate, div);
}
static int px30_mac_set_speed_clk(struct px30_clk_priv *priv, uint hz)
{
struct px30_cru *cru = priv->cru;
if (hz != 2500000 && hz != 25000000) {
debug("Unsupported mac speed:%d\n", hz);
return -EINVAL;
}
rk_clrsetreg(&cru->clksel_con[23], RMII_CLK_SEL_MASK,
((hz == 2500000) ? 0 : 1) << RMII_CLK_SEL_SHIFT);
return 0;
}
#endif
static ulong px30_clk_get_pll_rate(struct px30_clk_priv *priv,
enum px30_pll_id pll_id)
{
struct px30_cru *cru = priv->cru;
return rkclk_pll_get_rate(&cru->pll[pll_id], &cru->mode, pll_id);
}
static ulong px30_clk_set_pll_rate(struct px30_clk_priv *priv,
enum px30_pll_id pll_id, ulong hz)
{
struct px30_cru *cru = priv->cru;
if (rkclk_set_pll(&cru->pll[pll_id], &cru->mode, pll_id, hz))
return -EINVAL;
return rkclk_pll_get_rate(&cru->pll[pll_id], &cru->mode, pll_id);
}
static ulong px30_armclk_set_clk(struct px30_clk_priv *priv, ulong hz)
{
struct px30_cru *cru = priv->cru;
const struct cpu_rate_table *rate;
ulong old_rate;
rate = get_cpu_settings(hz);
if (!rate) {
printf("%s unsupport rate\n", __func__);
return -EINVAL;
}
/*
* select apll as cpu/core clock pll source and
* set up dependent divisors for PERI and ACLK clocks.
* core hz : apll = 1:1
*/
old_rate = px30_clk_get_pll_rate(priv, APLL);
if (old_rate > hz) {
if (rkclk_set_pll(&cru->pll[APLL], &cru->mode, APLL, hz))
return -EINVAL;
rk_clrsetreg(&cru->clksel_con[0],
CORE_CLK_PLL_SEL_MASK | CORE_DIV_CON_MASK |
CORE_ACLK_DIV_MASK | CORE_DBG_DIV_MASK,
rate->aclk_div << CORE_ACLK_DIV_SHIFT |
rate->pclk_div << CORE_DBG_DIV_SHIFT |
CORE_CLK_PLL_SEL_APLL << CORE_CLK_PLL_SEL_SHIFT |
0 << CORE_DIV_CON_SHIFT);
} else if (old_rate < hz) {
rk_clrsetreg(&cru->clksel_con[0],
CORE_CLK_PLL_SEL_MASK | CORE_DIV_CON_MASK |
CORE_ACLK_DIV_MASK | CORE_DBG_DIV_MASK,
rate->aclk_div << CORE_ACLK_DIV_SHIFT |
rate->pclk_div << CORE_DBG_DIV_SHIFT |
CORE_CLK_PLL_SEL_APLL << CORE_CLK_PLL_SEL_SHIFT |
0 << CORE_DIV_CON_SHIFT);
if (rkclk_set_pll(&cru->pll[APLL], &cru->mode, APLL, hz))
return -EINVAL;
}
return px30_clk_get_pll_rate(priv, APLL);
}
static ulong px30_clk_get_rate(struct clk *clk)
{
struct px30_clk_priv *priv = dev_get_priv(clk->dev);
ulong rate = 0;
if (!priv->gpll_hz && clk->id > ARMCLK) {
printf("%s gpll=%lu\n", __func__, priv->gpll_hz);
return -ENOENT;
}
debug("%s %ld\n", __func__, clk->id);
switch (clk->id) {
case PLL_APLL:
rate = px30_clk_get_pll_rate(priv, APLL);
break;
case PLL_DPLL:
rate = px30_clk_get_pll_rate(priv, DPLL);
break;
case PLL_CPLL:
rate = px30_clk_get_pll_rate(priv, CPLL);
break;
case PLL_NPLL:
rate = px30_clk_get_pll_rate(priv, NPLL);
break;
case ARMCLK:
rate = px30_clk_get_pll_rate(priv, APLL);
break;
case HCLK_SDMMC:
case HCLK_EMMC:
case SCLK_SDMMC:
case SCLK_EMMC:
case SCLK_EMMC_SAMPLE:
rate = px30_mmc_get_clk(priv, clk->id);
break;
case SCLK_SFC:
rate = px30_sfc_get_clk(priv, clk->id);
break;
case SCLK_I2C0:
case SCLK_I2C1:
case SCLK_I2C2:
case SCLK_I2C3:
rate = px30_i2c_get_clk(priv, clk->id);
break;
case SCLK_I2S1:
rate = px30_i2s_get_clk(priv, clk->id);
break;
case SCLK_NANDC:
rate = px30_nandc_get_clk(priv);
break;
case SCLK_PWM0:
case SCLK_PWM1:
rate = px30_pwm_get_clk(priv, clk->id);
break;
case SCLK_SARADC:
rate = px30_saradc_get_clk(priv);
break;
case SCLK_TSADC:
rate = px30_tsadc_get_clk(priv);
break;
case SCLK_SPI0:
case SCLK_SPI1:
rate = px30_spi_get_clk(priv, clk->id);
break;
case ACLK_VOPB:
case ACLK_VOPL:
case DCLK_VOPB:
case DCLK_VOPL:
rate = px30_vop_get_clk(priv, clk->id);
break;
case ACLK_BUS_PRE:
case HCLK_BUS_PRE:
case PCLK_BUS_PRE:
case PCLK_WDT_NS:
rate = px30_bus_get_clk(priv, clk->id);
break;
case ACLK_PERI_PRE:
case HCLK_PERI_PRE:
rate = px30_peri_get_clk(priv, clk->id);
break;
#ifndef CONFIG_SPL_BUILD
case SCLK_CRYPTO:
case SCLK_CRYPTO_APK:
rate = px30_crypto_get_clk(priv, clk->id);
break;
#endif
default:
return -ENOENT;
}
return rate;
}
static ulong px30_clk_set_rate(struct clk *clk, ulong rate)
{
struct px30_clk_priv *priv = dev_get_priv(clk->dev);
ulong ret = 0;
if (!priv->gpll_hz && clk->id > ARMCLK) {
printf("%s gpll=%lu\n", __func__, priv->gpll_hz);
return -ENOENT;
}
debug("%s %ld %ld\n", __func__, clk->id, rate);
switch (clk->id) {
case PLL_NPLL:
ret = px30_clk_set_pll_rate(priv, NPLL, rate);
break;
case PLL_CPLL:
ret = px30_clk_set_pll_rate(priv, CPLL, rate);
break;
case ARMCLK:
ret = px30_armclk_set_clk(priv, rate);
break;
case HCLK_SDMMC:
case HCLK_EMMC:
case SCLK_SDMMC:
case SCLK_EMMC:
ret = px30_mmc_set_clk(priv, clk->id, rate);
break;
case SCLK_SFC:
ret = px30_sfc_set_clk(priv, clk->id, rate);
break;
case SCLK_I2C0:
case SCLK_I2C1:
case SCLK_I2C2:
case SCLK_I2C3:
ret = px30_i2c_set_clk(priv, clk->id, rate);
break;
case SCLK_I2S1:
ret = px30_i2s_set_clk(priv, clk->id, rate);
break;
case SCLK_NANDC:
ret = px30_nandc_set_clk(priv, rate);
break;
case SCLK_PWM0:
case SCLK_PWM1:
ret = px30_pwm_set_clk(priv, clk->id, rate);
break;
case SCLK_SARADC:
ret = px30_saradc_set_clk(priv, rate);
break;
case SCLK_TSADC:
ret = px30_tsadc_set_clk(priv, rate);
break;
case SCLK_SPI0:
case SCLK_SPI1:
ret = px30_spi_set_clk(priv, clk->id, rate);
break;
case ACLK_VOPB:
case ACLK_VOPL:
case DCLK_VOPB:
case DCLK_VOPL:
ret = px30_vop_set_clk(priv, clk->id, rate);
break;
case ACLK_BUS_PRE:
case HCLK_BUS_PRE:
case PCLK_BUS_PRE:
ret = px30_bus_set_clk(priv, clk->id, rate);
break;
case ACLK_PERI_PRE:
case HCLK_PERI_PRE:
ret = px30_peri_set_clk(priv, clk->id, rate);
break;
#ifndef CONFIG_SPL_BUILD
case SCLK_CRYPTO:
case SCLK_CRYPTO_APK:
ret = px30_crypto_set_clk(priv, clk->id, rate);
break;
case SCLK_I2S1_OUT:
ret = px30_i2s1_mclk_set_clk(priv, clk->id, rate);
break;
case SCLK_GMAC:
case SCLK_GMAC_SRC:
ret = px30_mac_set_clk(priv, rate);
break;
case SCLK_GMAC_RMII:
ret = px30_mac_set_speed_clk(priv, rate);
break;
#endif
default:
return -ENOENT;
}
return ret;
}
#if CONFIG_IS_ENABLED(OF_REAL)
static int px30_gmac_set_parent(struct clk *clk, struct clk *parent)
{
struct px30_clk_priv *priv = dev_get_priv(clk->dev);
struct px30_cru *cru = priv->cru;
if (parent->id == SCLK_GMAC_SRC) {
debug("%s: switching GAMC to SCLK_GMAC_SRC\n", __func__);
rk_clrsetreg(&cru->clksel_con[23], RMII_EXTCLK_SEL_MASK,
RMII_EXTCLK_SEL_INT << RMII_EXTCLK_SEL_SHIFT);
} else {
debug("%s: switching GMAC to external clock\n", __func__);
rk_clrsetreg(&cru->clksel_con[23], RMII_EXTCLK_SEL_MASK,
RMII_EXTCLK_SEL_EXT << RMII_EXTCLK_SEL_SHIFT);
}
return 0;
}
static int px30_clk_set_parent(struct clk *clk, struct clk *parent)
{
switch (clk->id) {
case SCLK_GMAC:
return px30_gmac_set_parent(clk, parent);
default:
return -ENOENT;
}
}
#endif
static int px30_clk_enable(struct clk *clk)
{
switch (clk->id) {
case HCLK_HOST:
case HCLK_OTG:
case HCLK_SFC:
case SCLK_GMAC:
case SCLK_GMAC_RX_TX:
case SCLK_MAC_REF:
case SCLK_MAC_REFOUT:
case SCLK_SFC:
case ACLK_GMAC:
case PCLK_GMAC:
case SCLK_GMAC_RMII:
/* Required to successfully probe the Designware GMAC driver */
return 0;
case PCLK_WDT_NS:
/* Required to successfully probe the Designware watchdog driver */
return 0;
}
debug("%s: unsupported clk %ld\n", __func__, clk->id);
return -ENOENT;
}
static struct clk_ops px30_clk_ops = {
.get_rate = px30_clk_get_rate,
.set_rate = px30_clk_set_rate,
#if CONFIG_IS_ENABLED(OF_REAL)
.set_parent = px30_clk_set_parent,
#endif
.enable = px30_clk_enable,
};
static void px30_clk_init(struct px30_clk_priv *priv)
{
ulong npll_hz;
int ret;
npll_hz = px30_clk_get_pll_rate(priv, NPLL);
if (npll_hz != NPLL_HZ) {
ret = px30_clk_set_pll_rate(priv, NPLL, NPLL_HZ);
if (ret < 0)
printf("%s failed to set npll rate\n", __func__);
}
px30_bus_set_clk(priv, ACLK_BUS_PRE, ACLK_BUS_HZ);
px30_bus_set_clk(priv, HCLK_BUS_PRE, HCLK_BUS_HZ);
px30_bus_set_clk(priv, PCLK_BUS_PRE, PCLK_BUS_HZ);
px30_peri_set_clk(priv, ACLK_PERI_PRE, ACLK_PERI_HZ);
px30_peri_set_clk(priv, HCLK_PERI_PRE, HCLK_PERI_HZ);
}
static int px30_clk_probe(struct udevice *dev)
{
struct px30_clk_priv *priv = dev_get_priv(dev);
struct clk clk_gpll;
int ret;
if (px30_clk_get_pll_rate(priv, APLL) != APLL_HZ)
px30_armclk_set_clk(priv, APLL_HZ);
/* get the GPLL rate from the pmucru */
ret = clk_get_by_name(dev, "gpll", &clk_gpll);
if (ret) {
printf("%s: failed to get gpll clk from pmucru\n", __func__);
return ret;
}
priv->gpll_hz = clk_get_rate(&clk_gpll);
px30_clk_init(priv);
return 0;
}
static int px30_clk_of_to_plat(struct udevice *dev)
{
struct px30_clk_priv *priv = dev_get_priv(dev);
priv->cru = dev_read_addr_ptr(dev);
return 0;
}
static int px30_clk_bind(struct udevice *dev)
{
int ret;
struct udevice *sys_child;
struct sysreset_reg *priv;
/* The reset driver does not have a device node, so bind it here */
ret = device_bind_driver(dev, "rockchip_sysreset", "sysreset",
&sys_child);
if (ret) {
debug("Warning: No sysreset driver: ret=%d\n", ret);
} else {
priv = malloc(sizeof(struct sysreset_reg));
priv->glb_srst_fst_value = offsetof(struct px30_cru,
glb_srst_fst);
priv->glb_srst_snd_value = offsetof(struct px30_cru,
glb_srst_snd);
dev_set_priv(sys_child, priv);
}
#if CONFIG_IS_ENABLED(RESET_ROCKCHIP)
ret = offsetof(struct px30_cru, softrst_con[0]);
ret = rockchip_reset_bind(dev, ret, 12);
if (ret)
debug("Warning: software reset driver bind failed\n");
#endif
return 0;
}
static const struct udevice_id px30_clk_ids[] = {
{ .compatible = "rockchip,px30-cru" },
{ }
};
U_BOOT_DRIVER(rockchip_px30_cru) = {
.name = "rockchip_px30_cru",
.id = UCLASS_CLK,
.of_match = px30_clk_ids,
.priv_auto = sizeof(struct px30_clk_priv),
.of_to_plat = px30_clk_of_to_plat,
.ops = &px30_clk_ops,
.bind = px30_clk_bind,
.probe = px30_clk_probe,
};
static ulong px30_pclk_pmu_get_pmuclk(struct px30_pmuclk_priv *priv)
{
struct px30_pmucru *pmucru = priv->pmucru;
u32 div, con;
con = readl(&pmucru->pmu_clksel_con[0]);
div = (con & CLK_PMU_PCLK_DIV_MASK) >> CLK_PMU_PCLK_DIV_SHIFT;
return DIV_TO_RATE(priv->gpll_hz, div);
}
static ulong px30_pclk_pmu_set_pmuclk(struct px30_pmuclk_priv *priv, ulong hz)
{
struct px30_pmucru *pmucru = priv->pmucru;
int src_clk_div;
src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz);
assert(src_clk_div - 1 <= 31);
rk_clrsetreg(&pmucru->pmu_clksel_con[0],
CLK_PMU_PCLK_DIV_MASK,
(src_clk_div - 1) << CLK_PMU_PCLK_DIV_SHIFT);
return px30_pclk_pmu_get_pmuclk(priv);
}
static ulong px30_pmuclk_get_gpll_rate(struct px30_pmuclk_priv *priv)
{
struct px30_pmucru *pmucru = priv->pmucru;
return rkclk_pll_get_rate(&pmucru->pll, &pmucru->pmu_mode, GPLL);
}
static ulong px30_pmuclk_set_gpll_rate(struct px30_pmuclk_priv *priv, ulong hz)
{
struct px30_pmucru *pmucru = priv->pmucru;
ulong pclk_pmu_rate;
u32 div;
if (priv->gpll_hz == hz)
return priv->gpll_hz;
div = DIV_ROUND_UP(hz, priv->gpll_hz);
/* save clock rate */
pclk_pmu_rate = px30_pclk_pmu_get_pmuclk(priv);
/* avoid rate too large, reduce rate first */
px30_pclk_pmu_set_pmuclk(priv, pclk_pmu_rate / div);
/* change gpll rate */
rkclk_set_pll(&pmucru->pll, &pmucru->pmu_mode, GPLL, hz);
priv->gpll_hz = px30_pmuclk_get_gpll_rate(priv);
/* restore clock rate */
px30_pclk_pmu_set_pmuclk(priv, pclk_pmu_rate);
return priv->gpll_hz;
}
static ulong px30_pmuclk_get_rate(struct clk *clk)
{
struct px30_pmuclk_priv *priv = dev_get_priv(clk->dev);
ulong rate = 0;
debug("%s %ld\n", __func__, clk->id);
switch (clk->id) {
case PLL_GPLL:
rate = px30_pmuclk_get_gpll_rate(priv);
break;
case PCLK_PMU_PRE:
rate = px30_pclk_pmu_get_pmuclk(priv);
break;
default:
return -ENOENT;
}
return rate;
}
static ulong px30_pmuclk_set_rate(struct clk *clk, ulong rate)
{
struct px30_pmuclk_priv *priv = dev_get_priv(clk->dev);
ulong ret = 0;
debug("%s %ld %ld\n", __func__, clk->id, rate);
switch (clk->id) {
case PLL_GPLL:
ret = px30_pmuclk_set_gpll_rate(priv, rate);
break;
case PCLK_PMU_PRE:
ret = px30_pclk_pmu_set_pmuclk(priv, rate);
break;
default:
return -ENOENT;
}
return ret;
}
static struct clk_ops px30_pmuclk_ops = {
.get_rate = px30_pmuclk_get_rate,
.set_rate = px30_pmuclk_set_rate,
};
static void px30_pmuclk_init(struct px30_pmuclk_priv *priv)
{
priv->gpll_hz = px30_pmuclk_get_gpll_rate(priv);
px30_pmuclk_set_gpll_rate(priv, GPLL_HZ);
px30_pclk_pmu_set_pmuclk(priv, PCLK_PMU_HZ);
}
static int px30_pmuclk_probe(struct udevice *dev)
{
struct px30_pmuclk_priv *priv = dev_get_priv(dev);
px30_pmuclk_init(priv);
return 0;
}
static int px30_pmuclk_of_to_plat(struct udevice *dev)
{
struct px30_pmuclk_priv *priv = dev_get_priv(dev);
priv->pmucru = dev_read_addr_ptr(dev);
return 0;
}
static const struct udevice_id px30_pmuclk_ids[] = {
{ .compatible = "rockchip,px30-pmucru" },
{ }
};
U_BOOT_DRIVER(rockchip_px30_pmucru) = {
.name = "rockchip_px30_pmucru",
.id = UCLASS_CLK,
.of_match = px30_pmuclk_ids,
.priv_auto = sizeof(struct px30_pmuclk_priv),
.of_to_plat = px30_pmuclk_of_to_plat,
.ops = &px30_pmuclk_ops,
.probe = px30_pmuclk_probe,
};
|