summaryrefslogtreecommitdiff
path: root/sys/dev/fdt/imxccm.c
blob: b7c386a818c7e0e2ad1a0b9d75bc19bc109aea7b (plain)
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
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
/* $OpenBSD: imxccm.c,v 1.29 2022/06/28 23:43:12 naddy Exp $ */
/*
 * Copyright (c) 2012-2013 Patrick Wildt <patrick@blueri.se>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/queue.h>
#include <sys/malloc.h>
#include <sys/sysctl.h>
#include <sys/device.h>
#include <sys/evcount.h>
#include <sys/socket.h>
#include <sys/timeout.h>

#include <machine/intr.h>
#include <machine/bus.h>
#include <machine/fdt.h>

#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_clock.h>
#include <dev/ofw/ofw_misc.h>
#include <dev/ofw/ofw_regulator.h>
#include <dev/ofw/fdt.h>

#include <dev/fdt/imxanatopvar.h>

/* registers */
#define CCM_CCR		0x00
#define CCM_CCDR	0x04
#define CCM_CSR		0x08
#define CCM_CCSR	0x0c
#define CCM_CACRR	0x10
#define CCM_CBCDR	0x14
#define CCM_CBCMR	0x18
#define CCM_CSCMR1	0x1c
#define CCM_CSCMR2	0x20
#define CCM_CSCDR1	0x24
#define CCM_CS1CDR	0x28
#define CCM_CS2CDR	0x2c
#define CCM_CDCDR	0x30
#define CCM_CHSCCDR	0x34
#define CCM_CSCDR2	0x38
#define CCM_CSCDR3	0x3c
#define CCM_CSCDR4	0x40
#define CCM_CDHIPR	0x48
#define CCM_CDCR	0x4c
#define CCM_CTOR	0x50
#define CCM_CLPCR	0x54
#define CCM_CISR	0x58
#define CCM_CIMR	0x5c
#define CCM_CCOSR	0x60
#define CCM_CGPR	0x64
#define CCM_CCGR0	0x68
#define CCM_CCGR1	0x6c
#define CCM_CCGR2	0x70
#define CCM_CCGR3	0x74
#define CCM_CCGR4	0x78
#define CCM_CCGR5	0x7c
#define CCM_CCGR6	0x80
#define CCM_CCGR7	0x84
#define CCM_CMEOR	0x88

/* bits and bytes */
#define CCM_CCSR_PLL3_SW_CLK_SEL		(1 << 0)
#define CCM_CCSR_PLL2_SW_CLK_SEL		(1 << 1)
#define CCM_CCSR_PLL1_SW_CLK_SEL		(1 << 2)
#define CCM_CCSR_STEP_SEL			(1 << 8)
#define CCM_CBCDR_IPG_PODF_SHIFT		8
#define CCM_CBCDR_IPG_PODF_MASK			0x3
#define CCM_CBCDR_AHB_PODF_SHIFT		10
#define CCM_CBCDR_AHB_PODF_MASK			0x7
#define CCM_CBCDR_PERIPH_CLK_SEL_SHIFT		25
#define CCM_CBCDR_PERIPH_CLK_SEL_MASK		0x1
#define CCM_CBCMR_PERIPH_CLK2_SEL_SHIFT		12
#define CCM_CBCMR_PERIPH_CLK2_SEL_MASK		0x3
#define CCM_CBCMR_PRE_PERIPH_CLK_SEL_SHIFT	18
#define CCM_CBCMR_PRE_PERIPH_CLK_SEL_MASK	0x3
#define CCM_CSCDR1_USDHCx_CLK_SEL_SHIFT(x)	((x) + 15)
#define CCM_CSCDR1_USDHCx_CLK_SEL_MASK		0x1
#define CCM_CSCDR1_USDHCx_PODF_MASK		0x7
#define CCM_CSCDR1_UART_PODF_MASK		0x7
#define CCM_CSCDR2_ECSPI_PODF_SHIFT		19
#define CCM_CSCDR2_ECSPI_PODF_MASK		0x7
#define CCM_CCGR1_ENET				(3 << 10)
#define CCM_CCGR4_125M_PCIE			(3 << 0)
#define CCM_CCGR5_100M_SATA			(3 << 4)
#define CCM_CSCMR1_PERCLK_CLK_PODF_MASK		0x1f
#define CCM_CSCMR1_PERCLK_CLK_SEL_MASK		(1 << 6)

/* Analog registers */
#define CCM_ANALOG_PLL_USB1			0x0010
#define CCM_ANALOG_PLL_USB1_SET			0x0014
#define CCM_ANALOG_PLL_USB1_CLR			0x0018
#define  CCM_ANALOG_PLL_USB1_LOCK		(1U << 31)
#define  CCM_ANALOG_PLL_USB1_BYPASS		(1 << 16)
#define  CCM_ANALOG_PLL_USB1_ENABLE		(1 << 13)
#define  CCM_ANALOG_PLL_USB1_POWER		(1 << 12)
#define  CCM_ANALOG_PLL_USB1_EN_USB_CLKS	(1 << 6)
#define CCM_ANALOG_PLL_USB2			0x0020
#define CCM_ANALOG_PLL_USB2_SET			0x0024
#define CCM_ANALOG_PLL_USB2_CLR			0x0028
#define  CCM_ANALOG_PLL_USB2_LOCK		(1U << 31)
#define  CCM_ANALOG_PLL_USB2_BYPASS		(1 << 16)
#define  CCM_ANALOG_PLL_USB2_ENABLE		(1 << 13)
#define  CCM_ANALOG_PLL_USB2_POWER		(1 << 12)
#define  CCM_ANALOG_PLL_USB2_EN_USB_CLKS	(1 << 6)
#define CCM_ANALOG_PLL_ENET			0x00e0
#define CCM_ANALOG_PLL_ENET_SET			0x00e4
#define CCM_ANALOG_PLL_ENET_CLR			0x00e8
#define  CCM_ANALOG_PLL_ENET_LOCK		(1U << 31)
#define  CCM_ANALOG_PLL_ENET_ENABLE_100M	(1 << 20) /* i.MX6 */
#define  CCM_ANALOG_PLL_ENET_BYPASS		(1 << 16)
#define  CCM_ANALOG_PLL_ENET_ENABLE		(1 << 13) /* i.MX6 */
#define  CCM_ANALOG_PLL_ENET_POWERDOWN		(1 << 12) /* i.MX6 */
#define  CCM_ANALOG_PLL_ENET_ENABLE_CLK_125MHZ	(1 << 10) /* i.MX7 */

/* Int PLL */
#define CCM_14XX_IMX8M_ARM_PLL_GNRL_CTL		0x84
#define CCM_14XX_IMX8M_ARM_PLL_DIV_CTL		0x88
#define CCM_14XX_IMX8M_SYS_PLL1_GNRL_CTL	0x94
#define CCM_14XX_IMX8M_SYS_PLL1_DIV_CTL		0x98
#define  CCM_INT_PLL_LOCK				(1U << 31)
#define  CCM_INT_PLL_LOCK_SEL				(1 << 29)
#define  CCM_INT_PLL_RST				(1 << 9)
#define  CCM_INT_PLL_BYPASS				(1 << 4)
#define  CCM_INT_PLL_MAIN_DIV_SHIFT			12
#define  CCM_INT_PLL_MAIN_DIV_MASK			0x3ff
#define  CCM_INT_PLL_PRE_DIV_SHIFT			4
#define  CCM_INT_PLL_PRE_DIV_MASK			0x3f
#define  CCM_INT_PLL_POST_DIV_SHIFT			0
#define  CCM_INT_PLL_POST_DIV_MASK			0x7

/* Frac PLL */
#define CCM_FRAC_IMX8M_ARM_PLL0			0x28
#define CCM_FRAC_IMX8M_ARM_PLL1			0x2c
#define  CCM_FRAC_PLL_LOCK				(1U << 31)
#define  CCM_FRAC_PLL_ENABLE				(1 << 21)
#define  CCM_FRAC_PLL_POWERDOWN				(1 << 19)
#define  CCM_FRAC_PLL_REFCLK_SEL_SHIFT			16
#define  CCM_FRAC_PLL_REFCLK_SEL_MASK			0x3
#define  CCM_FRAC_PLL_LOCK_SEL				(1 << 15)
#define  CCM_FRAC_PLL_BYPASS				(1 << 14)
#define  CCM_FRAC_PLL_COUNTCLK_SEL			(1 << 13)
#define  CCM_FRAC_PLL_NEWDIV_VAL			(1 << 12)
#define  CCM_FRAC_PLL_NEWDIV_ACK			(1 << 11)
#define  CCM_FRAC_PLL_REFCLK_DIV_VAL_SHIFT		5
#define  CCM_FRAC_PLL_REFCLK_DIV_VAL_MASK		0x3f
#define  CCM_FRAC_PLL_OUTPUT_DIV_VAL_SHIFT		0
#define  CCM_FRAC_PLL_OUTPUT_DIV_VAL_MASK		0x1f
#define  CCM_FRAC_PLL_FRAC_DIV_CTL_SHIFT		7
#define  CCM_FRAC_PLL_FRAC_DIV_CTL_MASK			0x1ffffff
#define  CCM_FRAC_PLL_INT_DIV_CTL_SHIFT			0
#define  CCM_FRAC_PLL_INT_DIV_CTL_MASK			0x7f
#define  CCM_FRAC_PLL_DENOM				(1 << 24)
#define CCM_FRAC_IMX8M_PLLOUT_DIV_CFG		0x78
#define  CCM_FRAC_IMX8M_PLLOUT_DIV_CFG_ARM_SHIFT	20
#define  CCM_FRAC_IMX8M_PLLOUT_DIV_CFG_ARM_MASK		0x7

#define HCLK_FREQ				24000000
#define PLL3_60M				60000000
#define PLL3_80M				80000000

#define HREAD4(sc, reg)							\
	(bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)))
#define HWRITE4(sc, reg, val)						\
	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
#define HSET4(sc, reg, bits)						\
	HWRITE4((sc), (reg), HREAD4((sc), (reg)) | (bits))
#define HCLR4(sc, reg, bits)						\
	HWRITE4((sc), (reg), HREAD4((sc), (reg)) & ~(bits))

struct imxccm_gate {
	uint16_t reg;
	uint8_t pos;
	uint16_t parent;
};

struct imxccm_divider {
	uint16_t reg;
	uint16_t shift;
	uint16_t mask;
	uint16_t parent;
	uint16_t fixed;
};

struct imxccm_mux {
	uint16_t reg;
	uint16_t shift;
	uint16_t mask;
};

#include "imxccm_clocks.h"

struct imxccm_softc {
	struct device		sc_dev;
	bus_space_tag_t		sc_iot;
	bus_space_handle_t	sc_ioh;
	int			sc_node;
	uint32_t		sc_phandle;

	struct regmap		*sc_anatop;

	const struct imxccm_gate *sc_gates;
	int			sc_ngates;
	const struct imxccm_divider *sc_divs;
	int			sc_ndivs;
	const struct imxccm_mux	*sc_muxs;
	int			sc_nmuxs;
	const struct imxccm_divider *sc_predivs;
	int			sc_npredivs;
	struct clock_device	sc_cd;
};

int	imxccm_match(struct device *, void *, void *);
void	imxccm_attach(struct device *parent, struct device *self, void *args);

const struct cfattach	imxccm_ca = {
	sizeof (struct imxccm_softc), imxccm_match, imxccm_attach
};

struct cfdriver imxccm_cd = {
	NULL, "imxccm", DV_DULL
};

uint32_t imxccm_get_armclk(struct imxccm_softc *);
void imxccm_armclk_set_parent(struct imxccm_softc *, enum imxanatop_clocks);
uint32_t imxccm_get_usdhx(struct imxccm_softc *, int x);
uint32_t imxccm_get_periphclk(struct imxccm_softc *);
uint32_t imxccm_get_ahbclk(struct imxccm_softc *);
uint32_t imxccm_get_ipgclk(struct imxccm_softc *);
uint32_t imxccm_get_ipg_perclk(struct imxccm_softc *);
uint32_t imxccm_get_uartclk(struct imxccm_softc *);
uint32_t imxccm_imx8mm_enet(struct imxccm_softc *sc, uint32_t);
uint32_t imxccm_imx8mm_ahb(struct imxccm_softc *sc, uint32_t);
uint32_t imxccm_imx8mm_i2c(struct imxccm_softc *sc, uint32_t);
uint32_t imxccm_imx8mm_uart(struct imxccm_softc *sc, uint32_t);
uint32_t imxccm_imx8mm_usdhc(struct imxccm_softc *sc, uint32_t);
uint32_t imxccm_imx8mp_enet_qos_timer(struct imxccm_softc *sc, uint32_t);
uint32_t imxccm_imx8mp_enet_qos(struct imxccm_softc *sc, uint32_t);
uint32_t imxccm_imx8mp_hsio_axi(struct imxccm_softc *sc, uint32_t);
uint32_t imxccm_imx8mq_ecspi(struct imxccm_softc *sc, uint32_t);
uint32_t imxccm_imx8mq_enet(struct imxccm_softc *sc, uint32_t);
uint32_t imxccm_imx8mq_ahb(struct imxccm_softc *sc, uint32_t);
uint32_t imxccm_imx8mq_i2c(struct imxccm_softc *sc, uint32_t);
uint32_t imxccm_imx8mq_pwm(struct imxccm_softc *sc, uint32_t);
uint32_t imxccm_imx8mq_uart(struct imxccm_softc *sc, uint32_t);
uint32_t imxccm_imx8mq_usdhc(struct imxccm_softc *sc, uint32_t);
uint32_t imxccm_imx8mq_usb(struct imxccm_softc *sc, uint32_t);
int imxccm_imx8m_set_div(struct imxccm_softc *, uint32_t, uint64_t, uint64_t);
void imxccm_enable(void *, uint32_t *, int);
uint32_t imxccm_get_frequency(void *, uint32_t *);
int imxccm_set_frequency(void *, uint32_t *, uint32_t);
int imxccm_set_parent(void *, uint32_t *, uint32_t *);

int
imxccm_match(struct device *parent, void *match, void *aux)
{
	struct fdt_attach_args *faa = aux;

	return (OF_is_compatible(faa->fa_node, "fsl,imx6q-ccm") ||
	    OF_is_compatible(faa->fa_node, "fsl,imx6sl-ccm") ||
	    OF_is_compatible(faa->fa_node, "fsl,imx6sx-ccm") ||
	    OF_is_compatible(faa->fa_node, "fsl,imx6ul-ccm") ||
	    OF_is_compatible(faa->fa_node, "fsl,imx7d-ccm") ||
	    OF_is_compatible(faa->fa_node, "fsl,imx8mm-ccm") ||
	    OF_is_compatible(faa->fa_node, "fsl,imx8mp-ccm") ||
	    OF_is_compatible(faa->fa_node, "fsl,imx8mq-ccm"));
}

void
imxccm_attach(struct device *parent, struct device *self, void *aux)
{
	struct imxccm_softc *sc = (struct imxccm_softc *)self;
	struct fdt_attach_args *faa = aux;

	KASSERT(faa->fa_nreg >= 1);

	sc->sc_node = faa->fa_node;
	sc->sc_iot = faa->fa_iot;
	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
	    faa->fa_reg[0].size, 0, &sc->sc_ioh))
		panic("%s: bus_space_map failed!", __func__);

	sc->sc_phandle = OF_getpropint(sc->sc_node, "phandle", 0);

	if (OF_is_compatible(sc->sc_node, "fsl,imx8mm-ccm")) {
		sc->sc_anatop = regmap_bycompatible("fsl,imx8mm-anatop");
		KASSERT(sc->sc_anatop != NULL);
		sc->sc_gates = imx8mm_gates;
		sc->sc_ngates = nitems(imx8mm_gates);
		sc->sc_divs = imx8mm_divs;
		sc->sc_ndivs = nitems(imx8mm_divs);
		sc->sc_muxs = imx8mm_muxs;
		sc->sc_nmuxs = nitems(imx8mm_muxs);
		sc->sc_predivs = imx8mm_predivs;
		sc->sc_npredivs = nitems(imx8mm_predivs);
	} else if (OF_is_compatible(sc->sc_node, "fsl,imx8mp-ccm")) {
		sc->sc_anatop = regmap_bycompatible("fsl,imx8mp-anatop");
		KASSERT(sc->sc_anatop != NULL);
		sc->sc_gates = imx8mp_gates;
		sc->sc_ngates = nitems(imx8mp_gates);
		sc->sc_divs = imx8mp_divs;
		sc->sc_ndivs = nitems(imx8mp_divs);
		sc->sc_muxs = imx8mp_muxs;
		sc->sc_nmuxs = nitems(imx8mp_muxs);
		sc->sc_predivs = imx8mp_predivs;
		sc->sc_npredivs = nitems(imx8mp_predivs);
	} else if (OF_is_compatible(sc->sc_node, "fsl,imx8mq-ccm")) {
		sc->sc_anatop = regmap_bycompatible("fsl,imx8mq-anatop");
		KASSERT(sc->sc_anatop != NULL);
		sc->sc_gates = imx8mq_gates;
		sc->sc_ngates = nitems(imx8mq_gates);
		sc->sc_divs = imx8mq_divs;
		sc->sc_ndivs = nitems(imx8mq_divs);
		sc->sc_muxs = imx8mq_muxs;
		sc->sc_nmuxs = nitems(imx8mq_muxs);
		sc->sc_predivs = imx8mq_predivs;
		sc->sc_npredivs = nitems(imx8mq_predivs);
	} else if (OF_is_compatible(sc->sc_node, "fsl,imx7d-ccm")) {
		sc->sc_gates = imx7d_gates;
		sc->sc_ngates = nitems(imx7d_gates);
		sc->sc_divs = imx7d_divs;
		sc->sc_ndivs = nitems(imx7d_divs);
		sc->sc_muxs = imx7d_muxs;
		sc->sc_nmuxs = nitems(imx7d_muxs);
	} else if (OF_is_compatible(sc->sc_node, "fsl,imx6ul-ccm")) {
		sc->sc_gates = imx6ul_gates;
		sc->sc_ngates = nitems(imx6ul_gates);
	} else {
		sc->sc_gates = imx6_gates;
		sc->sc_ngates = nitems(imx6_gates);
	}

	printf("\n");

	sc->sc_cd.cd_node = faa->fa_node;
	sc->sc_cd.cd_cookie = sc;
	sc->sc_cd.cd_enable = imxccm_enable;
	sc->sc_cd.cd_get_frequency = imxccm_get_frequency;
	sc->sc_cd.cd_set_frequency = imxccm_set_frequency;
	sc->sc_cd.cd_set_parent = imxccm_set_parent;
	clock_register(&sc->sc_cd);
}

uint32_t
imxccm_get_armclk(struct imxccm_softc *sc)
{
	uint32_t ccsr = HREAD4(sc, CCM_CCSR);

	if (!(ccsr & CCM_CCSR_PLL1_SW_CLK_SEL))
		return imxanatop_decode_pll(ARM_PLL1, HCLK_FREQ);
	else if (ccsr & CCM_CCSR_STEP_SEL)
		return imxanatop_get_pll2_pfd(2);
	else
		return HCLK_FREQ;
}

void
imxccm_armclk_set_parent(struct imxccm_softc *sc, enum imxanatop_clocks clock)
{
	switch (clock)
	{
	case ARM_PLL1:
		/* jump onto pll1 */
		HCLR4(sc, CCM_CCSR, CCM_CCSR_PLL1_SW_CLK_SEL);
		/* put step clk on OSC, power saving */
		HCLR4(sc, CCM_CCSR, CCM_CCSR_STEP_SEL);
		break;
	case OSC:
		/* put step clk on OSC */
		HCLR4(sc, CCM_CCSR, CCM_CCSR_STEP_SEL);
		/* jump onto step clk */
		HSET4(sc, CCM_CCSR, CCM_CCSR_PLL1_SW_CLK_SEL);
		break;
	case SYS_PLL2_PFD2:
		/* put step clk on pll2-pfd2 400 MHz */
		HSET4(sc, CCM_CCSR, CCM_CCSR_STEP_SEL);
		/* jump onto step clk */
		HSET4(sc, CCM_CCSR, CCM_CCSR_PLL1_SW_CLK_SEL);
		break;
	default:
		panic("%s: parent not possible for arm clk", __func__);
	}
}

uint32_t
imxccm_get_ecspiclk(struct imxccm_softc *sc)
{
	uint32_t clkroot = PLL3_60M;
	uint32_t podf = HREAD4(sc, CCM_CSCDR2);

	podf >>= CCM_CSCDR2_ECSPI_PODF_SHIFT;
	podf &= CCM_CSCDR2_ECSPI_PODF_MASK;

	return clkroot / (podf + 1);
}

unsigned int
imxccm_get_usdhx(struct imxccm_softc *sc, int x)
{
	uint32_t cscmr1 = HREAD4(sc, CCM_CSCMR1);
	uint32_t cscdr1 = HREAD4(sc, CCM_CSCDR1);
	uint32_t podf, clkroot;

	// Odd bitsetting. Damn you.
	if (x == 1)
		podf = ((cscdr1 >> 11) & CCM_CSCDR1_USDHCx_PODF_MASK);
	else
		podf = ((cscdr1 >> (10 + 3*x)) & CCM_CSCDR1_USDHCx_PODF_MASK);

	if (cscmr1 & (1 << CCM_CSCDR1_USDHCx_CLK_SEL_SHIFT(x)))
		clkroot = imxanatop_get_pll2_pfd(0); // 352 MHz
	else
		clkroot = imxanatop_get_pll2_pfd(2); // 396 MHz

	return clkroot / (podf + 1);
}

uint32_t
imxccm_get_uartclk(struct imxccm_softc *sc)
{
	uint32_t clkroot = PLL3_80M;
	uint32_t podf = HREAD4(sc, CCM_CSCDR1) & CCM_CSCDR1_UART_PODF_MASK;

	return clkroot / (podf + 1);
}

uint32_t
imxccm_get_periphclk(struct imxccm_softc *sc)
{
	if ((HREAD4(sc, CCM_CBCDR) >> CCM_CBCDR_PERIPH_CLK_SEL_SHIFT)
		    & CCM_CBCDR_PERIPH_CLK_SEL_MASK) {
		switch((HREAD4(sc, CCM_CBCMR)
		    >> CCM_CBCMR_PERIPH_CLK2_SEL_SHIFT) & CCM_CBCMR_PERIPH_CLK2_SEL_MASK) {
		case 0:
			return imxanatop_decode_pll(USB1_PLL3, HCLK_FREQ);
		case 1:
		case 2:
			return HCLK_FREQ;
		default:
			return 0;
		}
	
	} else {
		switch((HREAD4(sc, CCM_CBCMR)
		    >> CCM_CBCMR_PRE_PERIPH_CLK_SEL_SHIFT) & CCM_CBCMR_PRE_PERIPH_CLK_SEL_MASK) {
		default:
		case 0:
			return imxanatop_decode_pll(SYS_PLL2, HCLK_FREQ);
		case 1:
			return imxanatop_get_pll2_pfd(2); // 396 MHz
		case 2:
			return imxanatop_get_pll2_pfd(0); // 352 MHz
		case 3:
			return imxanatop_get_pll2_pfd(2) / 2; // 198 MHz
		}
	}
}

uint32_t
imxccm_get_ahbclk(struct imxccm_softc *sc)
{
	uint32_t ahb_podf;

	ahb_podf = (HREAD4(sc, CCM_CBCDR) >> CCM_CBCDR_AHB_PODF_SHIFT)
	    & CCM_CBCDR_AHB_PODF_MASK;
	return imxccm_get_periphclk(sc) / (ahb_podf + 1);
}

uint32_t
imxccm_get_ipgclk(struct imxccm_softc *sc)
{
	uint32_t ipg_podf;

	ipg_podf = (HREAD4(sc, CCM_CBCDR) >> CCM_CBCDR_IPG_PODF_SHIFT)
	    & CCM_CBCDR_IPG_PODF_MASK;
	return imxccm_get_ahbclk(sc) / (ipg_podf + 1);
}

uint32_t
imxccm_get_ipg_perclk(struct imxccm_softc *sc)
{
	uint32_t cscmr1 = HREAD4(sc, CCM_CSCMR1);
	uint32_t freq, ipg_podf;

	if (sc->sc_gates == imx6ul_gates &&
	    cscmr1 & CCM_CSCMR1_PERCLK_CLK_SEL_MASK)
		freq = HCLK_FREQ;
	else
		freq = imxccm_get_ipgclk(sc);

	ipg_podf = cscmr1 & CCM_CSCMR1_PERCLK_CLK_PODF_MASK;

	return freq / (ipg_podf + 1);
}

void
imxccm_imx6_enable_pll_enet(struct imxccm_softc *sc, int on)
{
	KASSERT(on);

	regmap_write_4(sc->sc_anatop, CCM_ANALOG_PLL_ENET_CLR,
	    CCM_ANALOG_PLL_ENET_POWERDOWN);

	/* Wait for the PLL to lock. */
	while ((regmap_read_4(sc->sc_anatop,
	    CCM_ANALOG_PLL_ENET) & CCM_ANALOG_PLL_ENET_LOCK) == 0)
		;

	regmap_write_4(sc->sc_anatop, CCM_ANALOG_PLL_ENET_CLR,
	    CCM_ANALOG_PLL_ENET_BYPASS);
}

void
imxccm_imx6_enable_pll_usb1(struct imxccm_softc *sc, int on)
{
	KASSERT(on);

	regmap_write_4(sc->sc_anatop, CCM_ANALOG_PLL_USB1_SET,
	    CCM_ANALOG_PLL_USB1_POWER);

	/* Wait for the PLL to lock. */
	while ((regmap_read_4(sc->sc_anatop,
	    CCM_ANALOG_PLL_USB1) & CCM_ANALOG_PLL_USB1_LOCK) == 0)
		;

	regmap_write_4(sc->sc_anatop, CCM_ANALOG_PLL_USB1_CLR,
	    CCM_ANALOG_PLL_USB1_BYPASS);
}

void
imxccm_imx6_enable_pll_usb2(struct imxccm_softc *sc, int on)
{
	KASSERT(on);

	regmap_write_4(sc->sc_anatop, CCM_ANALOG_PLL_USB2_SET,
	    CCM_ANALOG_PLL_USB2_POWER);

	/* Wait for the PLL to lock. */
	while ((regmap_read_4(sc->sc_anatop,
	    CCM_ANALOG_PLL_USB2) & CCM_ANALOG_PLL_USB2_LOCK) == 0)
		;

	regmap_write_4(sc->sc_anatop, CCM_ANALOG_PLL_USB2_CLR,
	    CCM_ANALOG_PLL_USB2_BYPASS);
}

uint32_t
imxccm_imx7d_enet(struct imxccm_softc *sc, uint32_t idx)
{
	uint32_t mux;

	if (idx >= sc->sc_nmuxs || sc->sc_muxs[idx].reg == 0)
		return 0;

	mux = HREAD4(sc, sc->sc_muxs[idx].reg);
	mux >>= sc->sc_muxs[idx].shift;
	mux &= sc->sc_muxs[idx].mask;

	switch (mux) {
	case 0:
		return clock_get_frequency(sc->sc_node, "osc");
	case 7:
		return 392000000; /* pll_sys_pfd4_clk XXX not fixed */
	default:
		printf("%s: 0x%08x 0x%08x\n", __func__, idx, mux);
		return 0;
	}
}

uint32_t
imxccm_imx7d_i2c(struct imxccm_softc *sc, uint32_t idx)
{
	uint32_t mux;

	if (idx >= sc->sc_nmuxs || sc->sc_muxs[idx].reg == 0)
		return 0;

	mux = HREAD4(sc, sc->sc_muxs[idx].reg);
	mux >>= sc->sc_muxs[idx].shift;
	mux &= sc->sc_muxs[idx].mask;

	switch (mux) {
	case 0:
		return clock_get_frequency(sc->sc_node, "osc");
	case 1:
		return 120000000; /* pll_sys_main_120m_clk */
	default:
		printf("%s: 0x%08x 0x%08x\n", __func__, idx, mux);
		return 0;
	}
}

uint32_t
imxccm_imx7d_uart(struct imxccm_softc *sc, uint32_t idx)
{
	uint32_t mux;

	if (idx >= sc->sc_nmuxs || sc->sc_muxs[idx].reg == 0)
		return 0;

	mux = HREAD4(sc, sc->sc_muxs[idx].reg);
	mux >>= sc->sc_muxs[idx].shift;
	mux &= sc->sc_muxs[idx].mask;

	switch (mux) {
	case 0:
		return clock_get_frequency(sc->sc_node, "osc");
	default:
		printf("%s: 0x%08x 0x%08x\n", __func__, idx, mux);
		return 0;
	}
}

uint32_t
imxccm_imx7d_usdhc(struct imxccm_softc *sc, uint32_t idx)
{
	uint32_t mux;

	if (idx >= sc->sc_nmuxs || sc->sc_muxs[idx].reg == 0)
		return 0;

	mux = HREAD4(sc, sc->sc_muxs[idx].reg);
	mux >>= sc->sc_muxs[idx].shift;
	mux &= sc->sc_muxs[idx].mask;

	switch (mux) {
	case 0:
		return clock_get_frequency(sc->sc_node, "osc");
	case 1:
		return 392000000; /* pll_sys_pfd0_392m_clk */
	default:
		printf("%s: 0x%08x 0x%08x\n", __func__, idx, mux);
		return 0;
	}
}

uint32_t
imxccm_imx8mm_get_pll(struct imxccm_softc *sc, uint32_t idx)
{
	uint64_t main_div, pre_div, post_div, div;
	uint32_t pll0, pll1;
	uint64_t freq;

	switch (idx) {
	case IMX8MM_ARM_PLL:
		pll0 = regmap_read_4(sc->sc_anatop,
		    CCM_14XX_IMX8M_ARM_PLL_GNRL_CTL);
		pll1 = regmap_read_4(sc->sc_anatop,
		    CCM_14XX_IMX8M_ARM_PLL_DIV_CTL);
		div = 1;
		break;
	default:
		printf("%s: 0x%08x\n", __func__, idx);
		return 0;
	}

	freq = clock_get_frequency(sc->sc_node, "osc_24m");
	if (pll0 & CCM_INT_PLL_BYPASS)
		return freq;

	main_div = (pll1 >> CCM_INT_PLL_MAIN_DIV_SHIFT) &
	    CCM_INT_PLL_MAIN_DIV_MASK;
	pre_div = (pll1 >> CCM_INT_PLL_PRE_DIV_SHIFT) &
	    CCM_INT_PLL_PRE_DIV_MASK;
	post_div = (pll1 >> CCM_INT_PLL_POST_DIV_SHIFT) &
	    CCM_INT_PLL_POST_DIV_MASK;

	freq = freq * main_div;
	freq = freq / (pre_div * (1 << post_div) * div);
	return freq;
}

int
imxccm_imx8mm_set_pll(struct imxccm_softc *sc, uint32_t idx, uint64_t freq)
{
	uint64_t main_div, pre_div, post_div;
	uint32_t pll0, pll1, reg;
	int i;

	switch (idx) {
	case IMX8MM_ARM_PLL:
		pre_div = 3;
		switch (freq) {
		case 1800000000U:
			main_div = 225;
			post_div = 0;
			break;
		case 1600000000U:
			main_div = 200;
			post_div = 0;
			break;
		case 1200000000U:
			main_div = 300;
			post_div = 1;
			break;
		case 1000000000U:
			main_div = 250;
			post_div = 1;
			break;
		case 800000000U:
			main_div = 200;
			post_div = 1;
			break;
		case 750000000U:
			main_div = 250;
			post_div = 2;
			break;
		case 700000000U:
			main_div = 350;
			post_div = 2;
			break;
		case 600000000U:
			main_div = 300;
			post_div = 2;
			break;
		default:
			printf("%s: 0x%08x\n", __func__, idx);
			return -1;
		}
		pll0 = CCM_14XX_IMX8M_ARM_PLL_GNRL_CTL;
		pll1 = CCM_14XX_IMX8M_ARM_PLL_DIV_CTL;
		break;
	default:
		printf("%s: 0x%08x\n", __func__, idx);
		return -1;
	}

	regmap_write_4(sc->sc_anatop, pll0,
	    regmap_read_4(sc->sc_anatop, pll0) |
	    CCM_INT_PLL_LOCK_SEL);
	regmap_write_4(sc->sc_anatop, pll0,
	    regmap_read_4(sc->sc_anatop, pll0) &
	    ~CCM_INT_PLL_RST);
	regmap_write_4(sc->sc_anatop, pll1,
	    main_div << CCM_INT_PLL_MAIN_DIV_SHIFT |
	    pre_div << CCM_INT_PLL_PRE_DIV_SHIFT |
	    post_div << CCM_INT_PLL_POST_DIV_SHIFT);
	delay(3);
	regmap_write_4(sc->sc_anatop, pll0,
	    regmap_read_4(sc->sc_anatop, pll0) |
	    CCM_INT_PLL_RST);
	for (i = 0; i < 5000; i++) {
		reg = regmap_read_4(sc->sc_anatop, pll0);
		if (reg & CCM_INT_PLL_LOCK)
			break;
		delay(10);
	}
	if (i == 5000)
		printf("%s: timeout\n", __func__);
	regmap_write_4(sc->sc_anatop, pll0,
	    regmap_read_4(sc->sc_anatop, pll0) &
	    ~CCM_INT_PLL_BYPASS);

	return 0;
}

uint32_t
imxccm_imx8mm_enet(struct imxccm_softc *sc, uint32_t idx)
{
	uint32_t mux;

	if (idx >= sc->sc_nmuxs || sc->sc_muxs[idx].reg == 0)
		return 0;

	mux = HREAD4(sc, sc->sc_muxs[idx].reg);
	mux >>= sc->sc_muxs[idx].shift;
	mux &= sc->sc_muxs[idx].mask;

	switch (mux) {
	case 0:
		return clock_get_frequency(sc->sc_node, "osc_24m");
	case 1:
		return 266 * 1000 * 1000; /* sys1_pll_266m */
	default:
		printf("%s: 0x%08x 0x%08x\n", __func__, idx, mux);
		return 0;
	}
}

uint32_t
imxccm_imx8mm_ahb(struct imxccm_softc *sc, uint32_t idx)
{
	uint32_t mux;

	if (idx >= sc->sc_nmuxs || sc->sc_muxs[idx].reg == 0)
		return 0;

	mux = HREAD4(sc, sc->sc_muxs[idx].reg);
	mux >>= sc->sc_muxs[idx].shift;
	mux &= sc->sc_muxs[idx].mask;

	switch (mux) {
	case 0:
		return clock_get_frequency(sc->sc_node, "osc_24m");
	case 1:
		return 133 * 1000 * 1000; /* sys_pll1_133m */
	case 2:
		return 800 * 1000 * 1000; /* sys_pll1_800m */
	case 3:
		return 400 * 1000 * 1000; /* sys_pll1_400m */
	case 4:
		return 125 * 1000 * 1000; /* sys_pll2_125m */
	default:
		printf("%s: 0x%08x 0x%08x\n", __func__, idx, mux);
		return 0;
	}
}

uint32_t
imxccm_imx8mm_i2c(struct imxccm_softc *sc, uint32_t idx)
{
	uint32_t mux;

	if (idx >= sc->sc_nmuxs || sc->sc_muxs[idx].reg == 0)
		return 0;

	mux = HREAD4(sc, sc->sc_muxs[idx].reg);
	mux >>= sc->sc_muxs[idx].shift;
	mux &= sc->sc_muxs[idx].mask;

	switch (mux) {
	case 0:
		return clock_get_frequency(sc->sc_node, "osc_24m");
	default:
		printf("%s: 0x%08x 0x%08x\n", __func__, idx, mux);
		return 0;
	}
}

uint32_t
imxccm_imx8mm_uart(struct imxccm_softc *sc, uint32_t idx)
{
	uint32_t mux;

	if (idx >= sc->sc_nmuxs || sc->sc_muxs[idx].reg == 0)
		return 0;

	mux = HREAD4(sc, sc->sc_muxs[idx].reg);
	mux >>= sc->sc_muxs[idx].shift;
	mux &= sc->sc_muxs[idx].mask;

	switch (mux) {
	case 0:
		return clock_get_frequency(sc->sc_node, "osc_24m");
	default:
		printf("%s: 0x%08x 0x%08x\n", __func__, idx, mux);
		return 0;
	}
}

uint32_t
imxccm_imx8mm_usdhc(struct imxccm_softc *sc, uint32_t idx)
{
	uint32_t mux;

	if (idx >= sc->sc_nmuxs || sc->sc_muxs[idx].reg == 0)
		return 0;

	mux = HREAD4(sc, sc->sc_muxs[idx].reg);
	mux >>= sc->sc_muxs[idx].shift;
	mux &= sc->sc_muxs[idx].mask;

	switch (mux) {
	case 0:
		return clock_get_frequency(sc->sc_node, "osc_24m");
	case 1:
		return 400 * 1000 * 1000; /* sys1_pll_400m */
	default:
		printf("%s: 0x%08x 0x%08x\n", __func__, idx, mux);
		return 0;
	}
}

uint32_t
imxccm_imx8mp_enet_qos(struct imxccm_softc *sc, uint32_t idx)
{
	uint32_t mux;

	if (idx >= sc->sc_nmuxs || sc->sc_muxs[idx].reg == 0)
		return 0;

	mux = HREAD4(sc, sc->sc_muxs[idx].reg);
	mux >>= sc->sc_muxs[idx].shift;
	mux &= sc->sc_muxs[idx].mask;

	switch (mux) {
	case 0:
		return clock_get_frequency(sc->sc_node, "osc_24m");
	case 1:
		return 125 * 1000 * 1000; /* sys2_pll_125m */
	default:
		printf("%s: 0x%08x 0x%08x\n", __func__, idx, mux);
		return 0;
	}
}

uint32_t
imxccm_imx8mp_enet_qos_timer(struct imxccm_softc *sc, uint32_t idx)
{
	uint32_t mux;

	if (idx >= sc->sc_nmuxs || sc->sc_muxs[idx].reg == 0)
		return 0;

	mux = HREAD4(sc, sc->sc_muxs[idx].reg);
	mux >>= sc->sc_muxs[idx].shift;
	mux &= sc->sc_muxs[idx].mask;

	switch (mux) {
	case 0:
		return clock_get_frequency(sc->sc_node, "osc_24m");
	case 1:
		return 100 * 1000 * 1000; /* sys2_pll_100m */
	default:
		printf("%s: 0x%08x 0x%08x\n", __func__, idx, mux);
		return 0;
	}
}

uint32_t
imxccm_imx8mp_hsio_axi(struct imxccm_softc *sc, uint32_t idx)
{
	uint32_t mux;

	if (idx >= sc->sc_nmuxs || sc->sc_muxs[idx].reg == 0)
		return 0;

	mux = HREAD4(sc, sc->sc_muxs[idx].reg);
	mux >>= sc->sc_muxs[idx].shift;
	mux &= sc->sc_muxs[idx].mask;

	switch (mux) {
	case 0:
		return clock_get_frequency(sc->sc_node, "osc_24m");
	case 1:
		return 500 * 1000 * 1000; /* sys2_pll_500m */
	default:
		printf("%s: 0x%08x 0x%08x\n", __func__, idx, mux);
		return 0;
	}
}

uint32_t
imxccm_imx8mq_ecspi(struct imxccm_softc *sc, uint32_t idx)
{
	uint32_t mux;

	if (idx >= sc->sc_nmuxs || sc->sc_muxs[idx].reg == 0)
		return 0;

	mux = HREAD4(sc, sc->sc_muxs[idx].reg);
	mux >>= sc->sc_muxs[idx].shift;
	mux &= sc->sc_muxs[idx].mask;

	switch (mux) {
	case 0:
		return clock_get_frequency(sc->sc_node, "osc_25m");
	case 1:
		return 200 * 1000 * 1000; /* sys2_pll_200m */
	default:
		printf("%s: 0x%08x 0x%08x\n", __func__, idx, mux);
		return 0;
	}
}

uint32_t
imxccm_imx8mq_enet(struct imxccm_softc *sc, uint32_t idx)
{
	uint32_t mux;

	if (idx >= sc->sc_nmuxs || sc->sc_muxs[idx].reg == 0)
		return 0;

	mux = HREAD4(sc, sc->sc_muxs[idx].reg);
	mux >>= sc->sc_muxs[idx].shift;
	mux &= sc->sc_muxs[idx].mask;

	switch (mux) {
	case 0:
		return clock_get_frequency(sc->sc_node, "osc_25m");
	case 1:
		return 266 * 1000 * 1000; /* sys1_pll_266m */
	default:
		printf("%s: 0x%08x 0x%08x\n", __func__, idx, mux);
		return 0;
	}
}

uint32_t
imxccm_imx8mq_ahb(struct imxccm_softc *sc, uint32_t idx)
{
	uint32_t mux;

	if (idx >= sc->sc_nmuxs || sc->sc_muxs[idx].reg == 0)
		return 0;

	mux = HREAD4(sc, sc->sc_muxs[idx].reg);
	mux >>= sc->sc_muxs[idx].shift;
	mux &= sc->sc_muxs[idx].mask;

	switch (mux) {
	case 0:
		return clock_get_frequency(sc->sc_node, "osc_25m");
	case 1:
		return 133 * 1000 * 1000; /* sys1_pll_133m */
	case 2:
		return 800 * 1000 * 1000; /* sys1_pll_800m */
	case 3:
		return 400 * 1000 * 1000; /* sys1_pll_400m */
	case 4:
		return 125 * 1000 * 1000; /* sys2_pll_125m */
	default:
		printf("%s: 0x%08x 0x%08x\n", __func__, idx, mux);
		return 0;
	}
}

uint32_t
imxccm_imx8mq_i2c(struct imxccm_softc *sc, uint32_t idx)
{
	uint32_t mux;

	if (idx >= sc->sc_nmuxs || sc->sc_muxs[idx].reg == 0)
		return 0;

	mux = HREAD4(sc, sc->sc_muxs[idx].reg);
	mux >>= sc->sc_muxs[idx].shift;
	mux &= sc->sc_muxs[idx].mask;

	switch (mux) {
	case 0:
		return clock_get_frequency(sc->sc_node, "osc_25m");
	default:
		printf("%s: 0x%08x 0x%08x\n", __func__, idx, mux);
		return 0;
	}
}

uint32_t
imxccm_imx8mq_pwm(struct imxccm_softc *sc, uint32_t idx)
{
	uint32_t mux;

	if (idx >= sc->sc_nmuxs || sc->sc_muxs[idx].reg == 0)
		return 0;

	mux = HREAD4(sc, sc->sc_muxs[idx].reg);
	mux >>= sc->sc_muxs[idx].shift;
	mux &= sc->sc_muxs[idx].mask;

	switch (mux) {
	case 0:
		return clock_get_frequency(sc->sc_node, "osc_25m");
	case 1:
		return 100 * 1000 * 1000; /* sys1_pll_100m */
	case 2:
		return 160 * 1000 * 1000; /* sys1_pll_160m */
	case 3:
		return 40 * 1000 * 1000; /* sys1_pll_40m */
	default:
		printf("%s: 0x%08x 0x%08x\n", __func__, idx, mux);
		return 0;
	}
}

uint32_t
imxccm_imx8mq_uart(struct imxccm_softc *sc, uint32_t idx)
{
	uint32_t mux;

	if (idx >= sc->sc_nmuxs || sc->sc_muxs[idx].reg == 0)
		return 0;

	mux = HREAD4(sc, sc->sc_muxs[idx].reg);
	mux >>= sc->sc_muxs[idx].shift;
	mux &= sc->sc_muxs[idx].mask;

	switch (mux) {
	case 0:
		return clock_get_frequency(sc->sc_node, "osc_25m");
	default:
		printf("%s: 0x%08x 0x%08x\n", __func__, idx, mux);
		return 0;
	}
}

uint32_t
imxccm_imx8mq_usdhc(struct imxccm_softc *sc, uint32_t idx)
{
	uint32_t mux;

	if (idx >= sc->sc_nmuxs || sc->sc_muxs[idx].reg == 0)
		return 0;

	mux = HREAD4(sc, sc->sc_muxs[idx].reg);
	mux >>= sc->sc_muxs[idx].shift;
	mux &= sc->sc_muxs[idx].mask;

	switch (mux) {
	case 0:
		return clock_get_frequency(sc->sc_node, "osc_25m");
	case 1:
		return 400 * 1000 * 1000; /* sys1_pll_400m */
	default:
		printf("%s: 0x%08x 0x%08x\n", __func__, idx, mux);
		return 0;
	}
}

uint32_t
imxccm_imx8mq_usb(struct imxccm_softc *sc, uint32_t idx)
{
	uint32_t mux;

	if (idx >= sc->sc_nmuxs || sc->sc_muxs[idx].reg == 0)
		return 0;

	mux = HREAD4(sc, sc->sc_muxs[idx].reg);
	mux >>= sc->sc_muxs[idx].shift;
	mux &= sc->sc_muxs[idx].mask;

	switch (mux) {
	case 0:
		return clock_get_frequency(sc->sc_node, "osc_25m");
	case 1:
		if (idx == IMX8MQ_CLK_USB_CORE_REF ||
		    idx == IMX8MQ_CLK_USB_PHY_REF)
			return 100 * 1000 * 1000; /* sys1_pll_100m */
		if (idx == IMX8MQ_CLK_USB_BUS)
			return 500 * 1000 * 1000; /* sys2_pll_500m */
		printf("%s: 0x%08x 0x%08x\n", __func__, idx, mux);
		return 0;
	default:
		printf("%s: 0x%08x 0x%08x\n", __func__, idx, mux);
		return 0;
	}
}

uint32_t
imxccm_imx8mq_get_pll(struct imxccm_softc *sc, uint32_t idx)
{
	uint32_t divr_val, divq_val, divf_val;
	uint32_t divff, divfi;
	uint32_t pllout_div;
	uint32_t pll0, pll1;
	uint32_t freq;
	uint32_t mux;

	pllout_div = regmap_read_4(sc->sc_anatop, CCM_FRAC_IMX8M_PLLOUT_DIV_CFG);

	switch (idx) {
	case IMX8MQ_ARM_PLL:
		pll0 = regmap_read_4(sc->sc_anatop, CCM_FRAC_IMX8M_ARM_PLL0);
		pll1 = regmap_read_4(sc->sc_anatop, CCM_FRAC_IMX8M_ARM_PLL1);
		pllout_div >>= CCM_FRAC_IMX8M_PLLOUT_DIV_CFG_ARM_SHIFT;
		pllout_div &= CCM_FRAC_IMX8M_PLLOUT_DIV_CFG_ARM_MASK;
		break;
	default:
		printf("%s: 0x%08x\n", __func__, idx);
		return 0;
	}

	if (pll0 & CCM_FRAC_PLL_POWERDOWN)
		return 0;

	if ((pll0 & CCM_FRAC_PLL_ENABLE) == 0)
		return 0;

	mux = (pll0 >> CCM_FRAC_PLL_REFCLK_SEL_SHIFT) &
	    CCM_FRAC_PLL_REFCLK_SEL_MASK;
	switch (mux) {
	case 0:
		freq = clock_get_frequency(sc->sc_node, "osc_25m");
		break;
	case 1:
	case 2:
		freq = clock_get_frequency(sc->sc_node, "osc_27m");
		break;
	default:
		printf("%s: 0x%08x 0x%08x\n", __func__, idx, mux);
		return 0;
	}

	if (pll0 & CCM_FRAC_PLL_BYPASS)
		return freq;

	divr_val = (pll0 >> CCM_FRAC_PLL_REFCLK_DIV_VAL_SHIFT) &
	    CCM_FRAC_PLL_REFCLK_DIV_VAL_MASK;
	divq_val = pll0 & CCM_FRAC_PLL_OUTPUT_DIV_VAL_MASK;
	divff = (pll1 >> CCM_FRAC_PLL_FRAC_DIV_CTL_SHIFT) &
	    CCM_FRAC_PLL_FRAC_DIV_CTL_MASK;
	divfi = pll1 & CCM_FRAC_PLL_INT_DIV_CTL_MASK;
	divf_val = 1 + divfi + divff / CCM_FRAC_PLL_DENOM;

	freq = freq / (divr_val + 1) * 8 * divf_val / ((divq_val + 1) * 2);
	return freq / (pllout_div + 1);
}

int
imxccm_imx8mq_set_pll(struct imxccm_softc *sc, uint32_t idx, uint64_t freq)
{
	uint64_t divff, divfi, divr;
	uint32_t pllout_div;
	uint32_t pll0, pll1;
	uint32_t mux, reg;
	uint64_t pfreq;
	int i;

	pllout_div = regmap_read_4(sc->sc_anatop, CCM_FRAC_IMX8M_PLLOUT_DIV_CFG);

	switch (idx) {
	case IMX8MQ_ARM_PLL:
		pll0 = CCM_FRAC_IMX8M_ARM_PLL0;
		pll1 = CCM_FRAC_IMX8M_ARM_PLL1;
		pllout_div >>= CCM_FRAC_IMX8M_PLLOUT_DIV_CFG_ARM_SHIFT;
		pllout_div &= CCM_FRAC_IMX8M_PLLOUT_DIV_CFG_ARM_MASK;
		/* XXX: Assume fixed divider to ease math. */
		KASSERT(pllout_div == 0);
		divr = 5;
		break;
	default:
		printf("%s: 0x%08x\n", __func__, idx);
		return -1;
	}

	reg = regmap_read_4(sc->sc_anatop, pll0);
	mux = (reg >> CCM_FRAC_PLL_REFCLK_SEL_SHIFT) &
	    CCM_FRAC_PLL_REFCLK_SEL_MASK;
	switch (mux) {
	case 0:
		pfreq = clock_get_frequency(sc->sc_node, "osc_25m");
		break;
	case 1:
	case 2:
		pfreq = clock_get_frequency(sc->sc_node, "osc_27m");
		break;
	default:
		printf("%s: 0x%08x 0x%08x\n", __func__, idx, mux);
		return -1;
	}

	/* Frac divider follows the PLL */
	freq *= divr;

	/* PLL calculation */
	freq *= 2;
	pfreq *= 8;
	divfi = freq / pfreq;
	divff = (uint64_t)(freq - divfi * pfreq);
	divff = (divff * CCM_FRAC_PLL_DENOM) / pfreq;

	reg = regmap_read_4(sc->sc_anatop, pll1);
	reg &= ~(CCM_FRAC_PLL_FRAC_DIV_CTL_MASK << CCM_FRAC_PLL_FRAC_DIV_CTL_SHIFT);
	reg |= divff << CCM_FRAC_PLL_FRAC_DIV_CTL_SHIFT;
	reg &= ~(CCM_FRAC_PLL_INT_DIV_CTL_MASK << CCM_FRAC_PLL_INT_DIV_CTL_SHIFT);
	reg |= (divfi - 1) << CCM_FRAC_PLL_INT_DIV_CTL_SHIFT;
	regmap_write_4(sc->sc_anatop, pll1, reg);

	reg = regmap_read_4(sc->sc_anatop, pll0);
	reg &= ~CCM_FRAC_PLL_OUTPUT_DIV_VAL_MASK;
	reg &= ~(CCM_FRAC_PLL_REFCLK_DIV_VAL_MASK << CCM_FRAC_PLL_REFCLK_DIV_VAL_SHIFT);
	reg |= (divr - 1) << CCM_FRAC_PLL_REFCLK_DIV_VAL_SHIFT;
	regmap_write_4(sc->sc_anatop, pll0, reg);

	reg = regmap_read_4(sc->sc_anatop, pll0);
	reg |= CCM_FRAC_PLL_NEWDIV_VAL;
	regmap_write_4(sc->sc_anatop, pll0, reg);

	for (i = 0; i < 5000; i++) {
		reg = regmap_read_4(sc->sc_anatop, pll0);
		if (reg & CCM_FRAC_PLL_BYPASS)
			break;
		if (reg & CCM_FRAC_PLL_POWERDOWN)
			break;
		if (reg & CCM_FRAC_PLL_NEWDIV_ACK)
			break;
		delay(10);
	}
	if (i == 5000)
		printf("%s: timeout\n", __func__);

	reg = regmap_read_4(sc->sc_anatop, pll0);
	reg &= ~CCM_FRAC_PLL_NEWDIV_VAL;
	regmap_write_4(sc->sc_anatop, pll0, reg);

	return 0;
}

int
imxccm_imx8m_set_div(struct imxccm_softc *sc, uint32_t idx, uint64_t freq,
    uint64_t parent_freq)
{
	uint64_t div;
	uint32_t reg;

	if (parent_freq < freq) {
		printf("%s: parent frequency too low (0x%08x)\n",
		    __func__, idx);
		return -1;
	}

	/* divisor can only be changed if enabled */
	imxccm_enable(sc, &idx, 1);

	div = 0;
	while (parent_freq / (div + 1) > freq)
		div++;
	reg = HREAD4(sc, sc->sc_divs[idx].reg);
	reg &= ~(sc->sc_divs[idx].mask << sc->sc_divs[idx].shift);
	reg |= (div << sc->sc_divs[idx].shift);
	HWRITE4(sc, sc->sc_divs[idx].reg, reg);
	HCLR4(sc, sc->sc_predivs[idx].reg,
	    sc->sc_predivs[idx].mask << sc->sc_predivs[idx].shift);
	return 0;
}

void
imxccm_enable_parent(struct imxccm_softc *sc, uint32_t parent, int on)
{
	if (on)
		imxccm_enable(sc, &parent, on);
}

void
imxccm_enable(void *cookie, uint32_t *cells, int on)
{
	struct imxccm_softc *sc = cookie;
	uint32_t idx = cells[0], parent;
	uint32_t pcells[2];
	uint16_t reg;
	uint8_t pos;

	/* Dummy clock. */
	if (idx == 0)
		return;

	if (sc->sc_gates == imx8mm_gates) {
		switch (idx) {
		case IMX8MM_CLK_PCIE1_CTRL:
		case IMX8MM_CLK_PCIE2_CTRL:
			pcells[0] = sc->sc_phandle;
			pcells[1] = IMX8MM_SYS_PLL2_250M;
			imxccm_set_parent(cookie, &idx, pcells);
			break;
		case IMX8MM_CLK_PCIE1_PHY:
		case IMX8MM_CLK_PCIE2_PHY:
			pcells[0] = sc->sc_phandle;
			pcells[1] = IMX8MM_SYS_PLL2_100M;
			imxccm_set_parent(cookie, &idx, pcells);
			break;
		}
	} else if (sc->sc_gates == imx8mq_gates) {
		switch (idx) {
		case IMX8MQ_CLK_32K:
			/* always on */
			return;
		case IMX8MQ_CLK_PCIE1_CTRL:
		case IMX8MQ_CLK_PCIE2_CTRL:
			pcells[0] = sc->sc_phandle;
			pcells[1] = IMX8MQ_SYS2_PLL_250M;
			imxccm_set_parent(cookie, &idx, pcells);
			break;
		case IMX8MQ_CLK_PCIE1_PHY:
		case IMX8MQ_CLK_PCIE2_PHY:
			pcells[0] = sc->sc_phandle;
			pcells[1] = IMX8MQ_SYS2_PLL_100M;
			imxccm_set_parent(cookie, &idx, pcells);
			break;
		}
	} else if (sc->sc_gates == imx7d_gates) {
		if (sc->sc_anatop == NULL) {
			sc->sc_anatop = regmap_bycompatible("fsl,imx7d-anatop");
			KASSERT(sc->sc_anatop);
		}

		switch (idx) {
		case IMX7D_PLL_ENET_MAIN_125M_CLK:
			regmap_write_4(sc->sc_anatop, CCM_ANALOG_PLL_ENET_SET,
			    CCM_ANALOG_PLL_ENET_ENABLE_CLK_125MHZ);
			return;
		default:
			break;
		}
	} else if (sc->sc_gates == imx6_gates) {
		if (sc->sc_anatop == NULL) {
			sc->sc_anatop = regmap_bycompatible("fsl,imx6q-anatop");
			KASSERT(sc->sc_anatop);
		}

		switch (idx) {
		case IMX6_CLK_PLL3:
			imxccm_imx6_enable_pll_usb1(sc, on);
			return;
		case IMX6_CLK_PLL6:
			imxccm_imx6_enable_pll_enet(sc, on);
			return;
		case IMX6_CLK_PLL7:
			imxccm_imx6_enable_pll_usb2(sc, on);
			return;
		case IMX6_CLK_PLL3_USB_OTG:
			imxccm_enable_parent(sc, IMX6_CLK_PLL3, on);
			regmap_write_4(sc->sc_anatop,
			    on ? CCM_ANALOG_PLL_USB1_SET : CCM_ANALOG_PLL_USB1_CLR,
			    CCM_ANALOG_PLL_USB1_ENABLE);
			return;
		case IMX6_CLK_PLL6_ENET:
			imxccm_enable_parent(sc, IMX6_CLK_PLL6, on);
			regmap_write_4(sc->sc_anatop,
			    on ? CCM_ANALOG_PLL_ENET_SET : CCM_ANALOG_PLL_ENET_CLR,
			    CCM_ANALOG_PLL_ENET_ENABLE);
			return;
		case IMX6_CLK_PLL7_USB_HOST:
			imxccm_enable_parent(sc, IMX6_CLK_PLL7, on);
			regmap_write_4(sc->sc_anatop,
			    on ? CCM_ANALOG_PLL_USB2_SET : CCM_ANALOG_PLL_USB2_CLR,
			    CCM_ANALOG_PLL_USB2_ENABLE);
			return;
		case IMX6_CLK_USBPHY1:
			/* PLL outputs should always be on. */
			regmap_write_4(sc->sc_anatop, CCM_ANALOG_PLL_USB1_SET,
			    CCM_ANALOG_PLL_USB1_EN_USB_CLKS);
			imxccm_enable_parent(sc, IMX6_CLK_PLL3_USB_OTG, on);
			return;
		case IMX6_CLK_USBPHY2:
			/* PLL outputs should always be on. */
			regmap_write_4(sc->sc_anatop, CCM_ANALOG_PLL_USB2_SET,
			    CCM_ANALOG_PLL_USB2_EN_USB_CLKS);
			imxccm_enable_parent(sc, IMX6_CLK_PLL7_USB_HOST, on);
			return;
		case IMX6_CLK_SATA_REF_100:
			imxccm_enable_parent(sc, IMX6_CLK_PLL6_ENET, on);
			regmap_write_4(sc->sc_anatop,
			   on ? CCM_ANALOG_PLL_ENET_SET : CCM_ANALOG_PLL_ENET_CLR,
			   CCM_ANALOG_PLL_ENET_ENABLE_100M);
			return;
		case IMX6_CLK_ENET_REF:
			imxccm_enable_parent(sc, IMX6_CLK_PLL6_ENET, on);
			return;
		case IMX6_CLK_IPG:
		case IMX6_CLK_IPG_PER:
		case IMX6_CLK_ECSPI_ROOT:
			/* always on */
			return;
		default:
			break;
		}
	}

	if (on) {
		if (idx < sc->sc_ngates && sc->sc_gates[idx].parent) {
			parent = sc->sc_gates[idx].parent;
			imxccm_enable(sc, &parent, on);
		}

		if (idx < sc->sc_ndivs && sc->sc_divs[idx].parent) {
			parent = sc->sc_divs[idx].parent;
			imxccm_enable(sc, &parent, on);
		}
	}

	if (idx >= sc->sc_ngates || sc->sc_gates[idx].reg == 0) {
		if ((idx < sc->sc_ndivs && sc->sc_divs[idx].reg != 0) ||
		    (idx < sc->sc_nmuxs && sc->sc_muxs[idx].reg != 0))
			return;
		printf("%s: 0x%08x\n", __func__, idx);
		return;
	}

	reg = sc->sc_gates[idx].reg;
	pos = sc->sc_gates[idx].pos;

	if (on)
		HSET4(sc, reg, 0x3 << (2 * pos));
	else
		HCLR4(sc, reg, 0x3 << (2 * pos));
}

uint32_t
imxccm_get_frequency(void *cookie, uint32_t *cells)
{
	struct imxccm_softc *sc = cookie;
	uint32_t idx = cells[0];
	uint32_t div, pre, reg, parent;
	uint32_t freq;

	/* Dummy clock. */
	if (idx == 0)
		return 0;

	if (idx < sc->sc_ngates && sc->sc_gates[idx].parent) {
		parent = sc->sc_gates[idx].parent;
		return imxccm_get_frequency(sc, &parent);
	}

	if (idx < sc->sc_ndivs && sc->sc_divs[idx].parent) {
		div = HREAD4(sc, sc->sc_divs[idx].reg);
		div = div >> sc->sc_divs[idx].shift;
		div = div & sc->sc_divs[idx].mask;
		parent = sc->sc_divs[idx].parent;
		return imxccm_get_frequency(sc, &parent) / (div + 1);
	}

	if (sc->sc_gates == imx8mm_gates) {
		switch (idx) {
		case IMX8MM_CLK_ARM:
			parent = IMX8MM_ARM_PLL;
			return imxccm_get_frequency(sc, &parent);
		case IMX8MM_ARM_PLL:
			return imxccm_imx8mm_get_pll(sc, idx);
		}

		/* These are composite clocks. */
		if (idx < sc->sc_ngates && sc->sc_gates[idx].reg &&
		    idx < sc->sc_ndivs && sc->sc_divs[idx].reg &&
		    idx < sc->sc_npredivs && sc->sc_predivs[idx].reg) {
			switch (idx) {
			case IMX8MM_CLK_ENET_AXI:
				freq = imxccm_imx8mm_enet(sc, idx);
				break;
			case IMX8MM_CLK_AHB:
				freq = imxccm_imx8mm_ahb(sc, idx);
				break;
			case IMX8MM_CLK_I2C1:
			case IMX8MM_CLK_I2C2:
			case IMX8MM_CLK_I2C3:
			case IMX8MM_CLK_I2C4:
				freq = imxccm_imx8mm_i2c(sc, idx);
				break;
			case IMX8MM_CLK_UART1:
			case IMX8MM_CLK_UART2:
			case IMX8MM_CLK_UART3:
			case IMX8MM_CLK_UART4:
				freq = imxccm_imx8mm_uart(sc, idx);
				break;
			case IMX8MM_CLK_USDHC1:
			case IMX8MM_CLK_USDHC2:
			case IMX8MM_CLK_USDHC3:
				freq = imxccm_imx8mm_usdhc(sc, idx);
				break;
			default:
				printf("%s: 0x%08x\n", __func__, idx);
				return 0;
			}

			reg = HREAD4(sc, sc->sc_divs[idx].reg);
			div = reg >> sc->sc_divs[idx].shift;
			div = div & sc->sc_divs[idx].mask;
			pre = reg >> sc->sc_predivs[idx].shift;
			pre = pre & sc->sc_predivs[idx].mask;
			return ((freq / (pre + 1)) / (div + 1));
		}
	} else if (sc->sc_gates == imx8mp_gates) {
		/* These are composite clocks. */
		if (idx < sc->sc_ngates && sc->sc_gates[idx].reg &&
		    idx < sc->sc_ndivs && sc->sc_divs[idx].reg &&
		    idx < sc->sc_npredivs && sc->sc_predivs[idx].reg) {
			switch (idx) {
			case IMX8MP_CLK_ENET_AXI:
				freq = imxccm_imx8mm_enet(sc, idx);
				break;
			case IMX8MP_CLK_AHB:
				freq = imxccm_imx8mm_ahb(sc, idx);
				break;
			case IMX8MP_CLK_I2C1:
			case IMX8MP_CLK_I2C2:
			case IMX8MP_CLK_I2C3:
			case IMX8MP_CLK_I2C4:
			case IMX8MP_CLK_I2C5:
			case IMX8MP_CLK_I2C6:
				freq = imxccm_imx8mm_i2c(sc, idx);
				break;
			case IMX8MP_CLK_UART1:
			case IMX8MP_CLK_UART2:
			case IMX8MP_CLK_UART3:
			case IMX8MP_CLK_UART4:
				freq = imxccm_imx8mm_uart(sc, idx);
				break;
			case IMX8MP_CLK_USDHC1:
			case IMX8MP_CLK_USDHC2:
			case IMX8MP_CLK_USDHC3:
				freq = imxccm_imx8mm_usdhc(sc, idx);
				break;
			case IMX8MP_CLK_ENET_QOS:
				freq = imxccm_imx8mp_enet_qos(sc, idx);
				break;
			case IMX8MP_CLK_ENET_QOS_TIMER:
				freq = imxccm_imx8mp_enet_qos_timer(sc, idx);
				break;
			case IMX8MP_CLK_HSIO_AXI:
				freq = imxccm_imx8mp_hsio_axi(sc, idx);
				break;
			default:
				printf("%s: 0x%08x\n", __func__, idx);
				return 0;
			}

			reg = HREAD4(sc, sc->sc_divs[idx].reg);
			div = reg >> sc->sc_divs[idx].shift;
			div = div & sc->sc_divs[idx].mask;
			pre = reg >> sc->sc_predivs[idx].shift;
			pre = pre & sc->sc_predivs[idx].mask;
			return ((freq / (pre + 1)) / (div + 1));
		}
	} else if (sc->sc_gates == imx8mq_gates) {
		switch (idx) {
		case IMX8MQ_CLK_ARM:
			parent = IMX8MQ_ARM_PLL;
			return imxccm_get_frequency(sc, &parent);
		case IMX8MQ_ARM_PLL:
			return imxccm_imx8mq_get_pll(sc, idx);
		}

		/* These are composite clocks. */
		if (idx < sc->sc_ngates && sc->sc_gates[idx].reg &&
		    idx < sc->sc_ndivs && sc->sc_divs[idx].reg &&
		    idx < sc->sc_npredivs && sc->sc_predivs[idx].reg) {
			switch (idx) {
			case IMX8MQ_CLK_ENET_AXI:
				freq = imxccm_imx8mq_enet(sc, idx);
				break;
			case IMX8MQ_CLK_AHB:
				freq = imxccm_imx8mq_ahb(sc, idx);
				break;
			case IMX8MQ_CLK_I2C1:
			case IMX8MQ_CLK_I2C2:
			case IMX8MQ_CLK_I2C3:
			case IMX8MQ_CLK_I2C4:
				freq = imxccm_imx8mq_i2c(sc, idx);
				break;
			case IMX8MQ_CLK_UART1:
			case IMX8MQ_CLK_UART2:
			case IMX8MQ_CLK_UART3:
			case IMX8MQ_CLK_UART4:
				freq = imxccm_imx8mq_uart(sc, idx);
				break;
			case IMX8MQ_CLK_USDHC1:
			case IMX8MQ_CLK_USDHC2:
				freq = imxccm_imx8mq_usdhc(sc, idx);
				break;
			case IMX8MQ_CLK_USB_BUS:
			case IMX8MQ_CLK_USB_CORE_REF:
			case IMX8MQ_CLK_USB_PHY_REF:
				freq = imxccm_imx8mq_usb(sc, idx);
				break;
			case IMX8MQ_CLK_ECSPI1:
			case IMX8MQ_CLK_ECSPI2:
			case IMX8MQ_CLK_ECSPI3:
				freq = imxccm_imx8mq_ecspi(sc, idx);
				break;
			case IMX8MQ_CLK_PWM1:
			case IMX8MQ_CLK_PWM2:
			case IMX8MQ_CLK_PWM3:
			case IMX8MQ_CLK_PWM4:
				freq = imxccm_imx8mq_pwm(sc, idx);
				break;
			default:
				printf("%s: 0x%08x\n", __func__, idx);
				return 0;
			}

			reg = HREAD4(sc, sc->sc_divs[idx].reg);
			div = reg >> sc->sc_divs[idx].shift;
			div = div & sc->sc_divs[idx].mask;
			pre = reg >> sc->sc_predivs[idx].shift;
			pre = pre & sc->sc_predivs[idx].mask;
			return ((freq / (pre + 1)) / (div + 1));
		}

	} else if (sc->sc_gates == imx7d_gates) {
		switch (idx) {
		case IMX7D_ENET_AXI_ROOT_SRC:
			return imxccm_imx7d_enet(sc, idx);
		case IMX7D_I2C1_ROOT_SRC:
		case IMX7D_I2C2_ROOT_SRC:
		case IMX7D_I2C3_ROOT_SRC:
		case IMX7D_I2C4_ROOT_SRC:
			return imxccm_imx7d_i2c(sc, idx);
		case IMX7D_UART1_ROOT_SRC:
		case IMX7D_UART2_ROOT_SRC:
		case IMX7D_UART3_ROOT_SRC:
		case IMX7D_UART4_ROOT_SRC:
		case IMX7D_UART5_ROOT_SRC:
		case IMX7D_UART6_ROOT_SRC:
		case IMX7D_UART7_ROOT_SRC:
			return imxccm_imx7d_uart(sc, idx);
		case IMX7D_USDHC1_ROOT_SRC:
		case IMX7D_USDHC2_ROOT_SRC:
		case IMX7D_USDHC3_ROOT_SRC:
			return imxccm_imx7d_usdhc(sc, idx);
		}
	} else if (sc->sc_gates == imx6ul_gates) {
		switch (idx) {
		case IMX6UL_CLK_ARM:
			return imxccm_get_armclk(sc);
		case IMX6UL_CLK_IPG:
			return imxccm_get_ipgclk(sc);
		case IMX6UL_CLK_PERCLK:
			return imxccm_get_ipg_perclk(sc);
		case IMX6UL_CLK_UART1_SERIAL:
			return imxccm_get_uartclk(sc);
		case IMX6UL_CLK_USDHC1:
		case IMX6UL_CLK_USDHC2:
			return imxccm_get_usdhx(sc, idx - IMX6UL_CLK_USDHC1 + 1);
		}
	} else if (sc->sc_gates == imx6_gates) {
		switch (idx) {
		case IMX6_CLK_AHB:
			return imxccm_get_ahbclk(sc);
		case IMX6_CLK_ARM:
			return imxccm_get_armclk(sc);
		case IMX6_CLK_IPG:
			return imxccm_get_ipgclk(sc);
		case IMX6_CLK_IPG_PER:
			return imxccm_get_ipg_perclk(sc);
		case IMX6_CLK_ECSPI_ROOT:
			return imxccm_get_ecspiclk(sc);
		case IMX6_CLK_UART_SERIAL:
			return imxccm_get_uartclk(sc);
		case IMX6_CLK_USDHC1:
		case IMX6_CLK_USDHC2:
		case IMX6_CLK_USDHC3:
		case IMX6_CLK_USDHC4:
			return imxccm_get_usdhx(sc, idx - IMX6_CLK_USDHC1 + 1);
		}
	}

	printf("%s: 0x%08x\n", __func__, idx);
	return 0;
}

int
imxccm_set_frequency(void *cookie, uint32_t *cells, uint32_t freq)
{
	struct imxccm_softc *sc = cookie;
	uint32_t idx = cells[0];
	uint32_t reg, div, parent, parent_freq;
	uint32_t pcells[2];
	int ret;

	if (sc->sc_divs == imx8mm_divs) {
		switch (idx) {
		case IMX8MM_CLK_ARM:
			parent = IMX8MM_CLK_A53_SRC;
			return imxccm_set_frequency(cookie, &parent, freq);
		case IMX8MM_CLK_A53_SRC:
			pcells[0] = sc->sc_phandle;
			pcells[1] = IMX8MM_SYS_PLL1_800M;
			ret = imxccm_set_parent(cookie, &idx, pcells);
			if (ret)
				return ret;
			ret = imxccm_imx8mm_set_pll(sc, IMX8MM_ARM_PLL, freq);
			pcells[0] = sc->sc_phandle;
			pcells[1] = IMX8MM_ARM_PLL_OUT;
			imxccm_set_parent(cookie, &idx, pcells);
			return ret;
		case IMX8MM_CLK_USDHC1_ROOT:
		case IMX8MM_CLK_USDHC2_ROOT:
		case IMX8MM_CLK_USDHC3_ROOT:
			parent = sc->sc_gates[idx].parent;
			return imxccm_set_frequency(sc, &parent, freq);
		case IMX8MM_CLK_USDHC1:
		case IMX8MM_CLK_USDHC2:
		case IMX8MM_CLK_USDHC3:
			parent_freq = imxccm_imx8mm_usdhc(sc, idx);
			return imxccm_imx8m_set_div(sc, idx, freq, parent_freq);
		}
	} else if (sc->sc_divs == imx8mp_divs) {
		switch (idx) {
		case IMX8MP_CLK_ENET_QOS:
			parent_freq = imxccm_imx8mp_enet_qos(sc, idx);
			return imxccm_imx8m_set_div(sc, idx, freq, parent_freq);
		case IMX8MP_CLK_ENET_QOS_TIMER:
			parent_freq = imxccm_imx8mp_enet_qos_timer(sc, idx);
			return imxccm_imx8m_set_div(sc, idx, freq, parent_freq);
		case IMX8MP_CLK_HSIO_AXI:
			parent_freq = imxccm_imx8mp_hsio_axi(sc, idx);
			return imxccm_imx8m_set_div(sc, idx, freq, parent_freq);
		}
	} else if (sc->sc_divs == imx8mq_divs) {
		switch (idx) {
		case IMX8MQ_CLK_ARM:
			parent = IMX8MQ_CLK_A53_SRC;
			return imxccm_set_frequency(cookie, &parent, freq);
		case IMX8MQ_CLK_A53_SRC:
			pcells[0] = sc->sc_phandle;
			pcells[1] = IMX8MQ_SYS1_PLL_800M;
			ret = imxccm_set_parent(cookie, &idx, pcells);
			if (ret)
				return ret;
			ret = imxccm_imx8mq_set_pll(sc, IMX8MQ_ARM_PLL, freq);
			pcells[0] = sc->sc_phandle;
			pcells[1] = IMX8MQ_ARM_PLL_OUT;
			imxccm_set_parent(cookie, &idx, pcells);
			return ret;
		case IMX8MQ_CLK_USB_BUS:
		case IMX8MQ_CLK_USB_CORE_REF:
		case IMX8MQ_CLK_USB_PHY_REF:
			if (imxccm_get_frequency(sc, cells) != freq)
				break;
			return 0;
		case IMX8MQ_CLK_USDHC1:
		case IMX8MQ_CLK_USDHC2:
			parent_freq = imxccm_imx8mq_usdhc(sc, idx);
			return imxccm_imx8m_set_div(sc, idx, freq, parent_freq);
		}
	} else if (sc->sc_divs == imx7d_divs) {
		switch (idx) {
		case IMX7D_USDHC1_ROOT_CLK:
		case IMX7D_USDHC2_ROOT_CLK:
		case IMX7D_USDHC3_ROOT_CLK:
			parent = sc->sc_gates[idx].parent;
			return imxccm_set_frequency(sc, &parent, freq);
		case IMX7D_USDHC1_ROOT_DIV:
		case IMX7D_USDHC2_ROOT_DIV:
		case IMX7D_USDHC3_ROOT_DIV:
			parent = sc->sc_divs[idx].parent;
			parent_freq = imxccm_get_frequency(sc, &parent);
			div = 0;
			while (parent_freq / (div + 1) > freq)
				div++;
			reg = HREAD4(sc, sc->sc_divs[idx].reg);
			reg &= ~(sc->sc_divs[idx].mask << sc->sc_divs[idx].shift);
			reg |= (div << sc->sc_divs[idx].shift);
			HWRITE4(sc, sc->sc_divs[idx].reg, reg);
			return 0;
		}
	}

	printf("%s: 0x%08x %x\n", __func__, idx, freq);
	return -1;
}

int
imxccm_set_parent(void *cookie, uint32_t *cells, uint32_t *pcells)
{
	struct imxccm_softc *sc = cookie;
	uint32_t idx = cells[0];
	uint32_t pidx;
	uint32_t mux;

	if (pcells[0] != sc->sc_phandle) {
		printf("%s: 0x%08x parent 0x%08x\n", __func__, idx, pcells[0]);
		return -1;
	}

	pidx = pcells[1];

	if (sc->sc_muxs == imx8mm_muxs) {
		switch (idx) {
		case IMX8MM_CLK_A53_SRC:
			if (pidx != IMX8MM_ARM_PLL_OUT &&
			    pidx != IMX8MM_SYS_PLL1_800M)
				break;
			mux = HREAD4(sc, sc->sc_muxs[idx].reg);
			mux &= ~(sc->sc_muxs[idx].mask << sc->sc_muxs[idx].shift);
			if (pidx == IMX8MM_ARM_PLL_OUT)
				mux |= (0x1 << sc->sc_muxs[idx].shift);
			if (pidx == IMX8MM_SYS_PLL1_800M)
				mux |= (0x4 << sc->sc_muxs[idx].shift);
			HWRITE4(sc, sc->sc_muxs[idx].reg, mux);
			return 0;
		case IMX8MM_CLK_USB_BUS:
			if (pidx != IMX8MM_SYS_PLL2_500M)
				break;
			mux = HREAD4(sc, sc->sc_muxs[idx].reg);
			mux &= ~(sc->sc_muxs[idx].mask << sc->sc_muxs[idx].shift);
			mux |= (0x1 << sc->sc_muxs[idx].shift);
			HWRITE4(sc, sc->sc_muxs[idx].reg, mux);
			return 0;
		case IMX8MM_CLK_USB_CORE_REF:
		case IMX8MM_CLK_USB_PHY_REF:
			if (pidx != IMX8MM_SYS_PLL1_100M)
				break;
			mux = HREAD4(sc, sc->sc_muxs[idx].reg);
			mux &= ~(sc->sc_muxs[idx].mask << sc->sc_muxs[idx].shift);
			mux |= (0x1 << sc->sc_muxs[idx].shift);
			HWRITE4(sc, sc->sc_muxs[idx].reg, mux);
			return 0;
		case IMX8MM_CLK_PCIE1_CTRL:
		case IMX8MM_CLK_PCIE2_CTRL:
			if (pidx != IMX8MM_SYS_PLL2_250M)
				break;
			mux = HREAD4(sc, sc->sc_muxs[idx].reg);
			mux &= ~(sc->sc_muxs[idx].mask << sc->sc_muxs[idx].shift);
			mux |= (0x1 << sc->sc_muxs[idx].shift);
			HWRITE4(sc, sc->sc_muxs[idx].reg, mux);
			return 0;
		case IMX8MM_CLK_PCIE1_PHY:
		case IMX8MM_CLK_PCIE2_PHY:
			if (pidx != IMX8MM_SYS_PLL2_100M)
				break;
			mux = HREAD4(sc, sc->sc_muxs[idx].reg);
			mux &= ~(sc->sc_muxs[idx].mask << sc->sc_muxs[idx].shift);
			mux |= (0x1 << sc->sc_muxs[idx].shift);
			HWRITE4(sc, sc->sc_muxs[idx].reg, mux);
			return 0;
		}
	} else if (sc->sc_muxs == imx8mp_muxs) {
		switch (idx) {
		case IMX8MP_CLK_ENET_AXI:
			if (pidx != IMX8MP_SYS_PLL1_266M)
				break;
			mux = HREAD4(sc, sc->sc_muxs[idx].reg);
			mux &= ~(sc->sc_muxs[idx].mask << sc->sc_muxs[idx].shift);
			mux |= (0x1 << sc->sc_muxs[idx].shift);
			HWRITE4(sc, sc->sc_muxs[idx].reg, mux);
			return 0;
		case IMX8MP_CLK_PCIE_PHY:
			if (pidx != IMX8MP_CLK_24M)
				break;
			mux = HREAD4(sc, sc->sc_muxs[idx].reg);
			mux &= ~(sc->sc_muxs[idx].mask << sc->sc_muxs[idx].shift);
			mux |= (0x0 << sc->sc_muxs[idx].shift);
			HWRITE4(sc, sc->sc_muxs[idx].reg, mux);
			return 0;
		case IMX8MP_CLK_PCIE_AUX:
			if (pidx != IMX8MP_SYS_PLL2_50M)
				break;
			mux = HREAD4(sc, sc->sc_muxs[idx].reg);
			mux &= ~(sc->sc_muxs[idx].mask << sc->sc_muxs[idx].shift);
			mux |= (0x2 << sc->sc_muxs[idx].shift);
			HWRITE4(sc, sc->sc_muxs[idx].reg, mux);
			return 0;
		case IMX8MP_CLK_ENET_QOS:
			if (pidx != IMX8MP_SYS_PLL2_125M)
				break;
			mux = HREAD4(sc, sc->sc_muxs[idx].reg);
			mux &= ~(sc->sc_muxs[idx].mask << sc->sc_muxs[idx].shift);
			mux |= (0x1 << sc->sc_muxs[idx].shift);
			HWRITE4(sc, sc->sc_muxs[idx].reg, mux);
			return 0;
		case IMX8MP_CLK_ENET_QOS_TIMER:
			if (pidx != IMX8MP_SYS_PLL2_100M)
				break;
			mux = HREAD4(sc, sc->sc_muxs[idx].reg);
			mux &= ~(sc->sc_muxs[idx].mask << sc->sc_muxs[idx].shift);
			mux |= (0x1 << sc->sc_muxs[idx].shift);
			HWRITE4(sc, sc->sc_muxs[idx].reg, mux);
			return 0;
		case IMX8MP_CLK_USB_PHY_REF:
			if (pidx != IMX8MP_CLK_24M)
				break;
			mux = HREAD4(sc, sc->sc_muxs[idx].reg);
			mux &= ~(sc->sc_muxs[idx].mask << sc->sc_muxs[idx].shift);
			mux |= (0x0 << sc->sc_muxs[idx].shift);
			HWRITE4(sc, sc->sc_muxs[idx].reg, mux);
			return 0;
		case IMX8MP_CLK_HSIO_AXI:
			if (pidx != IMX8MP_SYS_PLL2_500M)
				break;
			mux = HREAD4(sc, sc->sc_muxs[idx].reg);
			mux &= ~(sc->sc_muxs[idx].mask << sc->sc_muxs[idx].shift);
			mux |= (0x1 << sc->sc_muxs[idx].shift);
			HWRITE4(sc, sc->sc_muxs[idx].reg, mux);
			return 0;
		}
	} else if (sc->sc_muxs == imx8mq_muxs) {
		switch (idx) {
		case IMX8MQ_CLK_A53_SRC:
			if (pidx != IMX8MQ_ARM_PLL_OUT &&
			    pidx != IMX8MQ_SYS1_PLL_800M)
				break;
			mux = HREAD4(sc, sc->sc_muxs[idx].reg);
			mux &= ~(sc->sc_muxs[idx].mask << sc->sc_muxs[idx].shift);
			if (pidx == IMX8MQ_ARM_PLL_OUT)
				mux |= (0x1 << sc->sc_muxs[idx].shift);
			if (pidx == IMX8MQ_SYS1_PLL_800M)
				mux |= (0x4 << sc->sc_muxs[idx].shift);
			HWRITE4(sc, sc->sc_muxs[idx].reg, mux);
			return 0;
		case IMX8MQ_CLK_USB_BUS:
			if (pidx != IMX8MQ_SYS2_PLL_500M)
				break;
			mux = HREAD4(sc, sc->sc_muxs[idx].reg);
			mux &= ~(sc->sc_muxs[idx].mask << sc->sc_muxs[idx].shift);
			mux |= (0x1 << sc->sc_muxs[idx].shift);
			HWRITE4(sc, sc->sc_muxs[idx].reg, mux);
			return 0;
		case IMX8MQ_CLK_USB_CORE_REF:
		case IMX8MQ_CLK_USB_PHY_REF:
			if (pidx != IMX8MQ_SYS1_PLL_100M)
				break;
			mux = HREAD4(sc, sc->sc_muxs[idx].reg);
			mux &= ~(sc->sc_muxs[idx].mask << sc->sc_muxs[idx].shift);
			mux |= (0x1 << sc->sc_muxs[idx].shift);
			HWRITE4(sc, sc->sc_muxs[idx].reg, mux);
			return 0;
		case IMX8MQ_CLK_PCIE1_CTRL:
		case IMX8MQ_CLK_PCIE2_CTRL:
			if (pidx != IMX8MQ_SYS2_PLL_250M)
				break;
			mux = HREAD4(sc, sc->sc_muxs[idx].reg);
			mux &= ~(sc->sc_muxs[idx].mask << sc->sc_muxs[idx].shift);
			mux |= (0x1 << sc->sc_muxs[idx].shift);
			HWRITE4(sc, sc->sc_muxs[idx].reg, mux);
			return 0;
		case IMX8MQ_CLK_PCIE1_PHY:
		case IMX8MQ_CLK_PCIE2_PHY:
			if (pidx != IMX8MQ_SYS2_PLL_100M)
				break;
			mux = HREAD4(sc, sc->sc_muxs[idx].reg);
			mux &= ~(sc->sc_muxs[idx].mask << sc->sc_muxs[idx].shift);
			mux |= (0x1 << sc->sc_muxs[idx].shift);
			HWRITE4(sc, sc->sc_muxs[idx].reg, mux);
			return 0;
		}
	}

	printf("%s: 0x%08x 0x%08x\n", __func__, idx, pidx);
	return -1;
}