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
|
// SPDX-License-Identifier: GPL-2.0+
/*
* (C) Copyright 2018 - Beniamino Galvani <b.galvani@gmail.com>
* (C) Copyright 2018 - BayLibre, SAS
* Author: Neil Armstrong <narmstrong@baylibre.com>
*/
#include <common.h>
#include <log.h>
#include <asm/arch/clock-g12a.h>
#include <asm/io.h>
#include <clk-uclass.h>
#include <dm.h>
#include <regmap.h>
#include <syscon.h>
#include <div64.h>
#include <dt-bindings/clock/g12a-clkc.h>
#include <linux/bitops.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/kernel.h>
#include "clk_meson.h"
/* This driver support only basic clock tree operations :
* - Can calculate clock frequency on a limited tree
* - Can Read muxes and basic dividers (0-based only)
* - Can enable/disable gates with limited propagation
* - Can reparent without propagation, only on muxes
* - Can set rates without reparenting
* This driver is adapted to what is actually supported by U-Boot
*/
/* Only the clocks ids we don't want to expose, such as the internal muxes
* and dividers of composite clocks, will remain defined here.
*/
#define CLKID_MPEG_SEL 8
#define CLKID_MPEG_DIV 9
#define CLKID_SD_EMMC_A_CLK0_SEL 63
#define CLKID_SD_EMMC_A_CLK0_DIV 64
#define CLKID_SD_EMMC_B_CLK0_SEL 65
#define CLKID_SD_EMMC_B_CLK0_DIV 66
#define CLKID_SD_EMMC_C_CLK0_SEL 67
#define CLKID_SD_EMMC_C_CLK0_DIV 68
#define CLKID_MPLL0_DIV 69
#define CLKID_MPLL1_DIV 70
#define CLKID_MPLL2_DIV 71
#define CLKID_MPLL3_DIV 72
#define CLKID_MPLL_PREDIV 73
#define CLKID_FCLK_DIV2_DIV 75
#define CLKID_FCLK_DIV3_DIV 76
#define CLKID_FCLK_DIV4_DIV 77
#define CLKID_FCLK_DIV5_DIV 78
#define CLKID_FCLK_DIV7_DIV 79
#define CLKID_FCLK_DIV2P5_DIV 100
#define CLKID_FIXED_PLL_DCO 101
#define CLKID_SYS_PLL_DCO 102
#define CLKID_GP0_PLL_DCO 103
#define CLKID_HIFI_PLL_DCO 104
#define CLKID_VPU_0_DIV 111
#define CLKID_VPU_1_DIV 114
#define CLKID_VAPB_0_DIV 118
#define CLKID_VAPB_1_DIV 121
#define CLKID_HDMI_PLL_DCO 125
#define CLKID_HDMI_PLL_OD 126
#define CLKID_HDMI_PLL_OD2 127
#define CLKID_VID_PLL_SEL 130
#define CLKID_VID_PLL_DIV 131
#define CLKID_VCLK_SEL 132
#define CLKID_VCLK2_SEL 133
#define CLKID_VCLK_INPUT 134
#define CLKID_VCLK2_INPUT 135
#define CLKID_VCLK_DIV 136
#define CLKID_VCLK2_DIV 137
#define CLKID_VCLK_DIV2_EN 140
#define CLKID_VCLK_DIV4_EN 141
#define CLKID_VCLK_DIV6_EN 142
#define CLKID_VCLK_DIV12_EN 143
#define CLKID_VCLK2_DIV2_EN 144
#define CLKID_VCLK2_DIV4_EN 145
#define CLKID_VCLK2_DIV6_EN 146
#define CLKID_VCLK2_DIV12_EN 147
#define CLKID_CTS_ENCI_SEL 158
#define CLKID_CTS_ENCP_SEL 159
#define CLKID_CTS_VDAC_SEL 160
#define CLKID_HDMI_TX_SEL 161
#define CLKID_HDMI_SEL 166
#define CLKID_HDMI_DIV 167
#define CLKID_MALI_0_DIV 170
#define CLKID_MALI_1_DIV 173
#define CLKID_XTAL 0x10000000
#define XTAL_RATE 24000000
struct meson_clk {
struct regmap *map;
};
static ulong meson_div_get_rate(struct clk *clk, unsigned long id);
static ulong meson_div_set_rate(struct clk *clk, unsigned long id, ulong rate,
ulong current_rate);
static ulong meson_mux_set_parent(struct clk *clk, unsigned long id,
unsigned long parent_id);
static ulong meson_mux_get_rate(struct clk *clk, unsigned long id);
static ulong meson_clk_set_rate_by_id(struct clk *clk, unsigned long id,
ulong rate, ulong current_rate);
static ulong meson_mux_get_parent(struct clk *clk, unsigned long id);
static ulong meson_clk_get_rate_by_id(struct clk *clk, unsigned long id);
#define NUM_CLKS 178
static struct meson_gate gates[NUM_CLKS] = {
/* Everything Else (EE) domain gates */
MESON_GATE(CLKID_SPICC0, HHI_GCLK_MPEG0, 8),
MESON_GATE(CLKID_I2C, HHI_GCLK_MPEG0, 9),
MESON_GATE(CLKID_UART0, HHI_GCLK_MPEG0, 13),
MESON_GATE(CLKID_SPICC1, HHI_GCLK_MPEG0, 14),
MESON_GATE(CLKID_SD_EMMC_A, HHI_GCLK_MPEG0, 4),
MESON_GATE(CLKID_SD_EMMC_B, HHI_GCLK_MPEG0, 25),
MESON_GATE(CLKID_SD_EMMC_C, HHI_GCLK_MPEG0, 26),
MESON_GATE(CLKID_ETH, HHI_GCLK_MPEG1, 3),
MESON_GATE(CLKID_UART1, HHI_GCLK_MPEG1, 16),
MESON_GATE(CLKID_USB, HHI_GCLK_MPEG1, 25),
MESON_GATE(CLKID_HTX_PCLK, HHI_GCLK_MPEG2, 4),
MESON_GATE(CLKID_USB1_DDR_BRIDGE, HHI_GCLK_MPEG2, 8),
MESON_GATE(CLKID_VPU_INTR, HHI_GCLK_MPEG2, 25),
/* Peripheral Gates */
MESON_GATE(CLKID_FCLK_DIV2, HHI_FIX_PLL_CNTL1, 24),
MESON_GATE(CLKID_FCLK_DIV3, HHI_FIX_PLL_CNTL1, 20),
MESON_GATE(CLKID_FCLK_DIV4, HHI_FIX_PLL_CNTL1, 21),
MESON_GATE(CLKID_FCLK_DIV5, HHI_FIX_PLL_CNTL1, 22),
MESON_GATE(CLKID_FCLK_DIV7, HHI_FIX_PLL_CNTL1, 23),
MESON_GATE(CLKID_SD_EMMC_A_CLK0, HHI_SD_EMMC_CLK_CNTL, 7),
MESON_GATE(CLKID_SD_EMMC_B_CLK0, HHI_SD_EMMC_CLK_CNTL, 23),
MESON_GATE(CLKID_SD_EMMC_C_CLK0, HHI_NAND_CLK_CNTL, 7),
MESON_GATE(CLKID_VPU_0, HHI_VPU_CLK_CNTL, 8),
MESON_GATE(CLKID_VPU_1, HHI_VPU_CLK_CNTL, 24),
MESON_GATE(CLKID_VAPB_0, HHI_VAPBCLK_CNTL, 8),
MESON_GATE(CLKID_VAPB_1, HHI_VAPBCLK_CNTL, 24),
MESON_GATE(CLKID_VAPB, HHI_VAPBCLK_CNTL, 30),
MESON_GATE(CLKID_HDMI, HHI_HDMI_CLK_CNTL, 8),
};
static int meson_set_gate_by_id(struct clk *clk, unsigned long id, bool on)
{
struct meson_clk *priv = dev_get_priv(clk->dev);
struct meson_gate *gate;
debug("%s: %sabling %ld\n", __func__, on ? "en" : "dis", id);
/* Propagate through muxes */
switch (id) {
case CLKID_VPU:
return meson_set_gate_by_id(clk,
meson_mux_get_parent(clk, CLKID_VPU), on);
case CLKID_VAPB_SEL:
return meson_set_gate_by_id(clk,
meson_mux_get_parent(clk, CLKID_VAPB_SEL), on);
}
if (id >= ARRAY_SIZE(gates))
return -ENOENT;
gate = &gates[id];
if (gate->reg == 0)
return 0;
debug("%s: really %sabling %ld\n", __func__, on ? "en" : "dis", id);
regmap_update_bits(priv->map, gate->reg,
BIT(gate->bit), on ? BIT(gate->bit) : 0);
/* Propagate to next gate(s) */
switch (id) {
case CLKID_VAPB:
return meson_set_gate_by_id(clk, CLKID_VAPB_SEL, on);
case CLKID_VAPB_0:
return meson_set_gate_by_id(clk,
meson_mux_get_parent(clk, CLKID_VAPB_0_SEL), on);
case CLKID_VAPB_1:
return meson_set_gate_by_id(clk,
meson_mux_get_parent(clk, CLKID_VAPB_0_SEL), on);
case CLKID_VPU_0:
return meson_set_gate_by_id(clk,
meson_mux_get_parent(clk, CLKID_VPU_0_SEL), on);
case CLKID_VPU_1:
return meson_set_gate_by_id(clk,
meson_mux_get_parent(clk, CLKID_VPU_1_SEL), on);
}
return 0;
}
static int meson_clk_enable(struct clk *clk)
{
return meson_set_gate_by_id(clk, clk->id, true);
}
static int meson_clk_disable(struct clk *clk)
{
return meson_set_gate_by_id(clk, clk->id, false);
}
static struct parm meson_vpu_0_div_parm = {
HHI_VPU_CLK_CNTL, 0, 7,
};
int meson_vpu_0_div_parent = CLKID_VPU_0_SEL;
static struct parm meson_vpu_1_div_parm = {
HHI_VPU_CLK_CNTL, 16, 7,
};
int meson_vpu_1_div_parent = CLKID_VPU_1_SEL;
static struct parm meson_vapb_0_div_parm = {
HHI_VAPBCLK_CNTL, 0, 7,
};
int meson_vapb_0_div_parent = CLKID_VAPB_0_SEL;
static struct parm meson_vapb_1_div_parm = {
HHI_VAPBCLK_CNTL, 16, 7,
};
int meson_vapb_1_div_parent = CLKID_VAPB_1_SEL;
static struct parm meson_hdmi_div_parm = {
HHI_HDMI_CLK_CNTL, 0, 7,
};
int meson_hdmi_div_parent = CLKID_HDMI_SEL;
static ulong meson_div_get_rate(struct clk *clk, unsigned long id)
{
struct meson_clk *priv = dev_get_priv(clk->dev);
unsigned int rate, parent_rate;
struct parm *parm;
int parent;
uint reg;
switch (id) {
case CLKID_VPU_0_DIV:
parm = &meson_vpu_0_div_parm;
parent = meson_vpu_0_div_parent;
break;
case CLKID_VPU_1_DIV:
parm = &meson_vpu_1_div_parm;
parent = meson_vpu_1_div_parent;
break;
case CLKID_VAPB_0_DIV:
parm = &meson_vapb_0_div_parm;
parent = meson_vapb_0_div_parent;
break;
case CLKID_VAPB_1_DIV:
parm = &meson_vapb_1_div_parm;
parent = meson_vapb_1_div_parent;
break;
case CLKID_HDMI_DIV:
parm = &meson_hdmi_div_parm;
parent = meson_hdmi_div_parent;
break;
default:
return -ENOENT;
}
regmap_read(priv->map, parm->reg_off, ®);
reg = PARM_GET(parm->width, parm->shift, reg);
debug("%s: div of %ld is %d\n", __func__, id, reg + 1);
parent_rate = meson_clk_get_rate_by_id(clk, parent);
if (IS_ERR_VALUE(parent_rate))
return parent_rate;
debug("%s: parent rate of %ld is %d\n", __func__, id, parent_rate);
rate = parent_rate / (reg + 1);
debug("%s: rate of %ld is %d\n", __func__, id, rate);
return rate;
}
static ulong meson_div_set_rate(struct clk *clk, unsigned long id, ulong rate,
ulong current_rate)
{
struct meson_clk *priv = dev_get_priv(clk->dev);
unsigned int new_div = -EINVAL;
unsigned long parent_rate;
struct parm *parm;
int parent;
int ret;
if (current_rate == rate)
return 0;
debug("%s: setting rate of %ld from %ld to %ld\n",
__func__, id, current_rate, rate);
switch (id) {
case CLKID_VPU_0_DIV:
parm = &meson_vpu_0_div_parm;
parent = meson_vpu_0_div_parent;
break;
case CLKID_VPU_1_DIV:
parm = &meson_vpu_1_div_parm;
parent = meson_vpu_1_div_parent;
break;
case CLKID_VAPB_0_DIV:
parm = &meson_vapb_0_div_parm;
parent = meson_vapb_0_div_parent;
break;
case CLKID_VAPB_1_DIV:
parm = &meson_vapb_1_div_parm;
parent = meson_vapb_1_div_parent;
break;
case CLKID_HDMI_DIV:
parm = &meson_hdmi_div_parm;
parent = meson_hdmi_div_parent;
break;
default:
return -ENOENT;
}
parent_rate = meson_clk_get_rate_by_id(clk, parent);
if (IS_ERR_VALUE(parent_rate))
return parent_rate;
debug("%s: parent rate of %ld is %ld\n", __func__, id, parent_rate);
/* If can't divide, set parent instead */
if (!parent_rate || rate > parent_rate)
return meson_clk_set_rate_by_id(clk, parent, rate,
current_rate);
new_div = DIV_ROUND_CLOSEST(parent_rate, rate);
debug("%s: new div of %ld is %d\n", __func__, id, new_div);
/* If overflow, try to set parent rate and retry */
if (!new_div || new_div > (1 << parm->width)) {
ret = meson_clk_set_rate_by_id(clk, parent, rate, current_rate);
if (IS_ERR_VALUE(ret))
return ret;
parent_rate = meson_clk_get_rate_by_id(clk, parent);
if (IS_ERR_VALUE(parent_rate))
return parent_rate;
new_div = DIV_ROUND_CLOSEST(parent_rate, rate);
debug("%s: new new div of %ld is %d\n", __func__, id, new_div);
if (!new_div || new_div > (1 << parm->width))
return -EINVAL;
}
debug("%s: setting div of %ld to %d\n", __func__, id, new_div);
regmap_update_bits(priv->map, parm->reg_off,
SETPMASK(parm->width, parm->shift),
(new_div - 1) << parm->shift);
debug("%s: new rate of %ld is %ld\n",
__func__, id, meson_div_get_rate(clk, id));
return 0;
}
static struct parm meson_vpu_mux_parm = {
HHI_VPU_CLK_CNTL, 31, 1,
};
int meson_vpu_mux_parents[] = {
CLKID_VPU_0,
CLKID_VPU_1,
};
static struct parm meson_vpu_0_mux_parm = {
HHI_VPU_CLK_CNTL, 9, 3,
};
static struct parm meson_vpu_1_mux_parm = {
HHI_VPU_CLK_CNTL, 25, 3,
};
static int meson_vpu_0_1_mux_parents[] = {
CLKID_FCLK_DIV3,
CLKID_FCLK_DIV4,
CLKID_FCLK_DIV5,
CLKID_FCLK_DIV7,
-ENOENT,
-ENOENT,
-ENOENT,
-ENOENT,
};
static struct parm meson_vapb_sel_mux_parm = {
HHI_VAPBCLK_CNTL, 31, 1,
};
int meson_vapb_sel_mux_parents[] = {
CLKID_VAPB_0,
CLKID_VAPB_1,
};
static struct parm meson_vapb_0_mux_parm = {
HHI_VAPBCLK_CNTL, 9, 2,
};
static struct parm meson_vapb_1_mux_parm = {
HHI_VAPBCLK_CNTL, 25, 2,
};
static int meson_vapb_0_1_mux_parents[] = {
CLKID_FCLK_DIV4,
CLKID_FCLK_DIV3,
CLKID_FCLK_DIV5,
CLKID_FCLK_DIV7,
};
static struct parm meson_hdmi_mux_parm = {
HHI_HDMI_CLK_CNTL, 9, 2,
};
static int meson_hdmi_mux_parents[] = {
CLKID_XTAL,
CLKID_FCLK_DIV4,
CLKID_FCLK_DIV3,
CLKID_FCLK_DIV5,
};
static ulong meson_mux_get_parent(struct clk *clk, unsigned long id)
{
struct meson_clk *priv = dev_get_priv(clk->dev);
struct parm *parm;
int *parents;
uint reg;
switch (id) {
case CLKID_VPU:
parm = &meson_vpu_mux_parm;
parents = meson_vpu_mux_parents;
break;
case CLKID_VPU_0_SEL:
parm = &meson_vpu_0_mux_parm;
parents = meson_vpu_0_1_mux_parents;
break;
case CLKID_VPU_1_SEL:
parm = &meson_vpu_1_mux_parm;
parents = meson_vpu_0_1_mux_parents;
break;
case CLKID_VAPB_SEL:
parm = &meson_vapb_sel_mux_parm;
parents = meson_vapb_sel_mux_parents;
break;
case CLKID_VAPB_0_SEL:
parm = &meson_vapb_0_mux_parm;
parents = meson_vapb_0_1_mux_parents;
break;
case CLKID_VAPB_1_SEL:
parm = &meson_vapb_1_mux_parm;
parents = meson_vapb_0_1_mux_parents;
break;
case CLKID_HDMI_SEL:
parm = &meson_hdmi_mux_parm;
parents = meson_hdmi_mux_parents;
break;
default:
return -ENOENT;
}
regmap_read(priv->map, parm->reg_off, ®);
reg = PARM_GET(parm->width, parm->shift, reg);
debug("%s: parent of %ld is %d (%d)\n",
__func__, id, parents[reg], reg);
return parents[reg];
}
static ulong meson_mux_set_parent(struct clk *clk, unsigned long id,
unsigned long parent_id)
{
unsigned long cur_parent = meson_mux_get_parent(clk, id);
struct meson_clk *priv = dev_get_priv(clk->dev);
unsigned int new_index = -EINVAL;
struct parm *parm;
int *parents;
int i;
if (IS_ERR_VALUE(cur_parent))
return cur_parent;
debug("%s: setting parent of %ld from %ld to %ld\n",
__func__, id, cur_parent, parent_id);
if (cur_parent == parent_id)
return 0;
switch (id) {
case CLKID_VPU:
parm = &meson_vpu_mux_parm;
parents = meson_vpu_mux_parents;
break;
case CLKID_VPU_0_SEL:
parm = &meson_vpu_0_mux_parm;
parents = meson_vpu_0_1_mux_parents;
break;
case CLKID_VPU_1_SEL:
parm = &meson_vpu_1_mux_parm;
parents = meson_vpu_0_1_mux_parents;
break;
case CLKID_VAPB_SEL:
parm = &meson_vapb_sel_mux_parm;
parents = meson_vapb_sel_mux_parents;
break;
case CLKID_VAPB_0_SEL:
parm = &meson_vapb_0_mux_parm;
parents = meson_vapb_0_1_mux_parents;
break;
case CLKID_VAPB_1_SEL:
parm = &meson_vapb_1_mux_parm;
parents = meson_vapb_0_1_mux_parents;
break;
case CLKID_HDMI_SEL:
parm = &meson_hdmi_mux_parm;
parents = meson_hdmi_mux_parents;
break;
default:
/* Not a mux */
return -ENOENT;
}
for (i = 0 ; i < (1 << parm->width) ; ++i) {
if (parents[i] == parent_id)
new_index = i;
}
if (IS_ERR_VALUE(new_index))
return new_index;
debug("%s: new index of %ld is %d\n", __func__, id, new_index);
regmap_update_bits(priv->map, parm->reg_off,
SETPMASK(parm->width, parm->shift),
new_index << parm->shift);
debug("%s: new parent of %ld is %ld\n",
__func__, id, meson_mux_get_parent(clk, id));
return 0;
}
static ulong meson_mux_get_rate(struct clk *clk, unsigned long id)
{
int parent = meson_mux_get_parent(clk, id);
if (IS_ERR_VALUE(parent))
return parent;
return meson_clk_get_rate_by_id(clk, parent);
}
static unsigned long meson_clk81_get_rate(struct clk *clk)
{
struct meson_clk *priv = dev_get_priv(clk->dev);
unsigned long parent_rate;
uint reg;
int parents[] = {
CLKID_XTAL,
-1,
CLKID_FCLK_DIV7,
CLKID_MPLL1,
CLKID_MPLL2,
CLKID_FCLK_DIV4,
CLKID_FCLK_DIV3,
CLKID_FCLK_DIV5
};
/* mux */
regmap_read(priv->map, HHI_MPEG_CLK_CNTL, ®);
reg = (reg >> 12) & 7;
switch (reg) {
case 1:
return -ENOENT;
default:
parent_rate = meson_clk_get_rate_by_id(clk, parents[reg]);
}
/* divider */
regmap_read(priv->map, HHI_MPEG_CLK_CNTL, ®);
reg = reg & ((1 << 7) - 1);
return parent_rate / reg;
}
static long mpll_rate_from_params(unsigned long parent_rate,
unsigned long sdm,
unsigned long n2)
{
unsigned long divisor = (SDM_DEN * n2) + sdm;
if (n2 < N2_MIN)
return -EINVAL;
return DIV_ROUND_UP_ULL((u64)parent_rate * SDM_DEN, divisor);
}
static struct parm meson_mpll0_parm[2] = {
{HHI_MPLL_CNTL1, 0, 14}, /* psdm */
{HHI_MPLL_CNTL1, 20, 9}, /* pn2 */
};
static struct parm meson_mpll1_parm[2] = {
{HHI_MPLL_CNTL3, 0, 14}, /* psdm */
{HHI_MPLL_CNTL3, 20, 9}, /* pn2 */
};
static struct parm meson_mpll2_parm[2] = {
{HHI_MPLL_CNTL5, 0, 14}, /* psdm */
{HHI_MPLL_CNTL5, 20, 9}, /* pn2 */
};
/*
* MultiPhase Locked Loops are outputs from a PLL with additional frequency
* scaling capabilities. MPLL rates are calculated as:
*
* f(N2_integer, SDM_IN ) = 2.0G/(N2_integer + SDM_IN/16384)
*/
static ulong meson_mpll_get_rate(struct clk *clk, unsigned long id)
{
struct meson_clk *priv = dev_get_priv(clk->dev);
struct parm *psdm, *pn2;
unsigned long sdm, n2;
unsigned long parent_rate;
uint reg;
switch (id) {
case CLKID_MPLL0:
psdm = &meson_mpll0_parm[0];
pn2 = &meson_mpll0_parm[1];
break;
case CLKID_MPLL1:
psdm = &meson_mpll1_parm[0];
pn2 = &meson_mpll1_parm[1];
break;
case CLKID_MPLL2:
psdm = &meson_mpll2_parm[0];
pn2 = &meson_mpll2_parm[1];
break;
default:
return -ENOENT;
}
parent_rate = meson_clk_get_rate_by_id(clk, CLKID_FIXED_PLL);
if (IS_ERR_VALUE(parent_rate))
return parent_rate;
regmap_read(priv->map, psdm->reg_off, ®);
sdm = PARM_GET(psdm->width, psdm->shift, reg);
regmap_read(priv->map, pn2->reg_off, ®);
n2 = PARM_GET(pn2->width, pn2->shift, reg);
return mpll_rate_from_params(parent_rate, sdm, n2);
}
static struct parm meson_fixed_pll_parm[4] = {
{HHI_FIX_PLL_CNTL0, 0, 9}, /* pm */
{HHI_FIX_PLL_CNTL0, 10, 5}, /* pn */
{HHI_FIX_PLL_CNTL0, 16, 2}, /* pod */
{HHI_FIX_PLL_CNTL1, 0, 17}, /* pfrac */
};
static struct parm meson_sys_pll_parm[3] = {
{HHI_SYS_PLL_CNTL0, 0, 9}, /* pm */
{HHI_SYS_PLL_CNTL0, 10, 5}, /* pn */
{HHI_SYS_PLL_CNTL0, 16, 3}, /* pod */
};
static ulong meson_pll_get_rate(struct clk *clk, unsigned long id)
{
struct meson_clk *priv = dev_get_priv(clk->dev);
struct parm *pm, *pn, *pod, *pfrac = NULL;
unsigned long parent_rate_mhz = XTAL_RATE / 1000000;
u16 n, m, od, frac;
ulong rate;
uint reg;
/*
* FIXME: Between the unit conversion and the missing frac, we know
* rate will be slightly off ...
*/
switch (id) {
case CLKID_FIXED_PLL:
pm = &meson_fixed_pll_parm[0];
pn = &meson_fixed_pll_parm[1];
pod = &meson_fixed_pll_parm[2];
pfrac = &meson_fixed_pll_parm[3];
break;
case CLKID_SYS_PLL:
pm = &meson_sys_pll_parm[0];
pn = &meson_sys_pll_parm[1];
pod = &meson_sys_pll_parm[2];
break;
default:
return -ENOENT;
}
regmap_read(priv->map, pn->reg_off, ®);
n = PARM_GET(pn->width, pn->shift, reg);
regmap_read(priv->map, pm->reg_off, ®);
m = PARM_GET(pm->width, pm->shift, reg);
regmap_read(priv->map, pod->reg_off, ®);
od = PARM_GET(pod->width, pod->shift, reg);
rate = parent_rate_mhz * m;
if (pfrac) {
ulong frac_rate;
regmap_read(priv->map, pfrac->reg_off, ®);
frac = PARM_GET(pfrac->width - 1, pfrac->shift, reg);
frac_rate = DIV_ROUND_UP_ULL((u64)parent_rate_mhz * frac,
1 << (pfrac->width - 2));
if (frac & BIT(pfrac->width - 1))
rate -= frac_rate;
else
rate += frac_rate;
}
return (DIV_ROUND_UP_ULL(rate, n) >> od) * 1000000;
}
static struct parm meson_pcie_pll_parm[3] = {
{HHI_PCIE_PLL_CNTL0, 0, 8}, /* pm */
{HHI_PCIE_PLL_CNTL0, 10, 5}, /* pn */
{HHI_PCIE_PLL_CNTL0, 16, 5}, /* pod */
};
static ulong meson_pcie_pll_get_rate(struct clk *clk)
{
struct meson_clk *priv = dev_get_priv(clk->dev);
struct parm *pm, *pn, *pod;
unsigned long parent_rate_mhz = XTAL_RATE / 1000000;
u16 n, m, od;
uint reg;
pm = &meson_pcie_pll_parm[0];
pn = &meson_pcie_pll_parm[1];
pod = &meson_pcie_pll_parm[2];
regmap_read(priv->map, pn->reg_off, ®);
n = PARM_GET(pn->width, pn->shift, reg);
regmap_read(priv->map, pm->reg_off, ®);
m = PARM_GET(pm->width, pm->shift, reg);
regmap_read(priv->map, pod->reg_off, ®);
od = PARM_GET(pod->width, pod->shift, reg);
return ((parent_rate_mhz * m / n) / 2 / od / 2) * 1000000;
}
static ulong meson_clk_get_rate_by_id(struct clk *clk, unsigned long id)
{
ulong rate;
switch (id) {
case CLKID_XTAL:
rate = XTAL_RATE;
break;
case CLKID_FIXED_PLL:
case CLKID_SYS_PLL:
rate = meson_pll_get_rate(clk, id);
break;
case CLKID_FCLK_DIV2:
rate = meson_pll_get_rate(clk, CLKID_FIXED_PLL) / 2;
break;
case CLKID_FCLK_DIV3:
rate = meson_pll_get_rate(clk, CLKID_FIXED_PLL) / 3;
break;
case CLKID_FCLK_DIV4:
rate = meson_pll_get_rate(clk, CLKID_FIXED_PLL) / 4;
break;
case CLKID_FCLK_DIV5:
rate = meson_pll_get_rate(clk, CLKID_FIXED_PLL) / 5;
break;
case CLKID_FCLK_DIV7:
rate = meson_pll_get_rate(clk, CLKID_FIXED_PLL) / 7;
break;
case CLKID_MPLL0:
case CLKID_MPLL1:
case CLKID_MPLL2:
rate = meson_mpll_get_rate(clk, id);
break;
case CLKID_CLK81:
rate = meson_clk81_get_rate(clk);
break;
case CLKID_PCIE_PLL:
rate = meson_pcie_pll_get_rate(clk);
break;
case CLKID_VPU_0:
rate = meson_div_get_rate(clk, CLKID_VPU_0_DIV);
break;
case CLKID_VPU_1:
rate = meson_div_get_rate(clk, CLKID_VPU_1_DIV);
break;
case CLKID_VAPB:
rate = meson_mux_get_rate(clk, CLKID_VAPB_SEL);
break;
case CLKID_VAPB_0:
rate = meson_div_get_rate(clk, CLKID_VAPB_0_DIV);
break;
case CLKID_VAPB_1:
rate = meson_div_get_rate(clk, CLKID_VAPB_1_DIV);
break;
case CLKID_HDMI:
rate = meson_div_get_rate(clk, CLKID_HDMI_DIV);
break;
case CLKID_VPU_0_DIV:
case CLKID_VPU_1_DIV:
case CLKID_VAPB_0_DIV:
case CLKID_VAPB_1_DIV:
case CLKID_HDMI_DIV:
rate = meson_div_get_rate(clk, id);
break;
case CLKID_VPU:
case CLKID_VPU_0_SEL:
case CLKID_VPU_1_SEL:
case CLKID_VAPB_SEL:
case CLKID_VAPB_0_SEL:
case CLKID_VAPB_1_SEL:
case CLKID_HDMI_SEL:
rate = meson_mux_get_rate(clk, id);
break;
default:
if (gates[id].reg != 0) {
/* a clock gate */
rate = meson_clk81_get_rate(clk);
break;
}
return -ENOENT;
}
debug("clock %lu has rate %lu\n", id, rate);
return rate;
}
static ulong meson_clk_get_rate(struct clk *clk)
{
return meson_clk_get_rate_by_id(clk, clk->id);
}
static ulong meson_pcie_pll_set_rate(struct clk *clk, ulong rate)
{
struct meson_clk *priv = dev_get_priv(clk->dev);
regmap_write(priv->map, HHI_PCIE_PLL_CNTL0, 0x20090496);
regmap_write(priv->map, HHI_PCIE_PLL_CNTL0, 0x30090496);
regmap_write(priv->map, HHI_PCIE_PLL_CNTL1, 0x00000000);
regmap_write(priv->map, HHI_PCIE_PLL_CNTL2, 0x00001100);
regmap_write(priv->map, HHI_PCIE_PLL_CNTL3, 0x10058e00);
regmap_write(priv->map, HHI_PCIE_PLL_CNTL4, 0x000100c0);
regmap_write(priv->map, HHI_PCIE_PLL_CNTL5, 0x68000048);
regmap_write(priv->map, HHI_PCIE_PLL_CNTL5, 0x68000068);
udelay(20);
regmap_write(priv->map, HHI_PCIE_PLL_CNTL4, 0x008100c0);
udelay(10);
regmap_write(priv->map, HHI_PCIE_PLL_CNTL0, 0x34090496);
regmap_write(priv->map, HHI_PCIE_PLL_CNTL0, 0x14090496);
udelay(10);
regmap_write(priv->map, HHI_PCIE_PLL_CNTL2, 0x00001000);
regmap_update_bits(priv->map, HHI_PCIE_PLL_CNTL0,
0x1f << 16, 9 << 16);
return 100000000;
}
static int meson_clk_set_parent(struct clk *clk, struct clk *parent)
{
return meson_mux_set_parent(clk, clk->id, parent->id);
}
static ulong meson_clk_set_rate_by_id(struct clk *clk, unsigned long id,
ulong rate, ulong current_rate)
{
if (current_rate == rate)
return 0;
switch (id) {
/* Fixed clocks */
case CLKID_FIXED_PLL:
case CLKID_SYS_PLL:
case CLKID_FCLK_DIV2:
case CLKID_FCLK_DIV3:
case CLKID_FCLK_DIV4:
case CLKID_FCLK_DIV5:
case CLKID_FCLK_DIV7:
case CLKID_MPLL0:
case CLKID_MPLL1:
case CLKID_MPLL2:
case CLKID_CLK81:
if (current_rate != rate)
return -EINVAL;
case CLKID_PCIE_PLL:
return meson_pcie_pll_set_rate(clk, rate);
return 0;
case CLKID_VPU:
return meson_clk_set_rate_by_id(clk,
meson_mux_get_parent(clk, CLKID_VPU), rate,
current_rate);
case CLKID_VAPB:
case CLKID_VAPB_SEL:
return meson_clk_set_rate_by_id(clk,
meson_mux_get_parent(clk, CLKID_VAPB_SEL),
rate, current_rate);
case CLKID_VPU_0:
return meson_div_set_rate(clk, CLKID_VPU_0_DIV, rate,
current_rate);
case CLKID_VPU_1:
return meson_div_set_rate(clk, CLKID_VPU_1_DIV, rate,
current_rate);
case CLKID_VAPB_0:
return meson_div_set_rate(clk, CLKID_VAPB_0_DIV, rate,
current_rate);
case CLKID_VAPB_1:
return meson_div_set_rate(clk, CLKID_VAPB_1_DIV, rate,
current_rate);
case CLKID_VPU_0_DIV:
case CLKID_VPU_1_DIV:
case CLKID_VAPB_0_DIV:
case CLKID_VAPB_1_DIV:
case CLKID_HDMI_DIV:
return meson_div_set_rate(clk, id, rate, current_rate);
case CLKID_HDMI:
return meson_clk_set_rate_by_id(clk, CLKID_HDMI_DIV,
rate, current_rate);
default:
return -ENOENT;
}
return -EINVAL;
}
static ulong meson_clk_set_rate(struct clk *clk, ulong rate)
{
ulong current_rate = meson_clk_get_rate_by_id(clk, clk->id);
int ret;
if (IS_ERR_VALUE(current_rate))
return current_rate;
debug("%s: setting rate of %ld from %ld to %ld\n",
__func__, clk->id, current_rate, rate);
ret = meson_clk_set_rate_by_id(clk, clk->id, rate, current_rate);
if (IS_ERR_VALUE(ret))
return ret;
debug("clock %lu has new rate %lu\n", clk->id,
meson_clk_get_rate_by_id(clk, clk->id));
return 0;
}
static int meson_clk_probe(struct udevice *dev)
{
struct meson_clk *priv = dev_get_priv(dev);
priv->map = syscon_node_to_regmap(dev_get_parent(dev)->node);
if (IS_ERR(priv->map))
return PTR_ERR(priv->map);
/*
* Depending on the boot src, the state of the MMC clock might
* be different. Reset it to make sure we won't get stuck
*/
regmap_write(priv->map, HHI_NAND_CLK_CNTL, 0);
regmap_write(priv->map, HHI_SD_EMMC_CLK_CNTL, 0);
debug("meson-clk-g12a: probed\n");
return 0;
}
static struct clk_ops meson_clk_ops = {
.disable = meson_clk_disable,
.enable = meson_clk_enable,
.get_rate = meson_clk_get_rate,
.set_parent = meson_clk_set_parent,
.set_rate = meson_clk_set_rate,
};
static const struct udevice_id meson_clk_ids[] = {
{ .compatible = "amlogic,g12a-clkc" },
{ .compatible = "amlogic,g12b-clkc" },
{ .compatible = "amlogic,sm1-clkc" },
{ }
};
U_BOOT_DRIVER(meson_clk_g12a) = {
.name = "meson_clk_g12a",
.id = UCLASS_CLK,
.of_match = meson_clk_ids,
.priv_auto = sizeof(struct meson_clk),
.ops = &meson_clk_ops,
.probe = meson_clk_probe,
};
|