1c3828949SGregory CLEMENT // SPDX-License-Identifier: GPL-2.0
26b72333dSSebastian Hesselbarth /*
36b72333dSSebastian Hesselbarth * Marvell Armada 370 SoC clocks
46b72333dSSebastian Hesselbarth *
56b72333dSSebastian Hesselbarth * Copyright (C) 2012 Marvell
66b72333dSSebastian Hesselbarth *
76b72333dSSebastian Hesselbarth * Gregory CLEMENT <gregory.clement@free-electrons.com>
86b72333dSSebastian Hesselbarth * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
96b72333dSSebastian Hesselbarth * Andrew Lunn <andrew@lunn.ch>
106b72333dSSebastian Hesselbarth *
116b72333dSSebastian Hesselbarth */
126b72333dSSebastian Hesselbarth
136b72333dSSebastian Hesselbarth #include <linux/kernel.h>
146b72333dSSebastian Hesselbarth #include <linux/clk-provider.h>
156b72333dSSebastian Hesselbarth #include <linux/io.h>
166b72333dSSebastian Hesselbarth #include <linux/of.h>
176b72333dSSebastian Hesselbarth #include "common.h"
186b72333dSSebastian Hesselbarth
196b72333dSSebastian Hesselbarth /*
206b72333dSSebastian Hesselbarth * Core Clocks
216b72333dSSebastian Hesselbarth */
226b72333dSSebastian Hesselbarth
236b72333dSSebastian Hesselbarth #define SARL 0 /* Low part [0:31] */
245e1a63f5SGregory CLEMENT #define SARL_A370_SSCG_ENABLE BIT(10)
256b72333dSSebastian Hesselbarth #define SARL_A370_PCLK_FREQ_OPT 11
266b72333dSSebastian Hesselbarth #define SARL_A370_PCLK_FREQ_OPT_MASK 0xF
276b72333dSSebastian Hesselbarth #define SARL_A370_FAB_FREQ_OPT 15
286b72333dSSebastian Hesselbarth #define SARL_A370_FAB_FREQ_OPT_MASK 0x1F
296b72333dSSebastian Hesselbarth #define SARL_A370_TCLK_FREQ_OPT 20
306b72333dSSebastian Hesselbarth #define SARL_A370_TCLK_FREQ_OPT_MASK 0x1
316b72333dSSebastian Hesselbarth
326b72333dSSebastian Hesselbarth enum { A370_CPU_TO_NBCLK, A370_CPU_TO_HCLK, A370_CPU_TO_DRAMCLK };
336b72333dSSebastian Hesselbarth
34682dfdc0SSachin Kamat static const struct coreclk_ratio a370_coreclk_ratios[] __initconst = {
356b72333dSSebastian Hesselbarth { .id = A370_CPU_TO_NBCLK, .name = "nbclk" },
366b72333dSSebastian Hesselbarth { .id = A370_CPU_TO_HCLK, .name = "hclk" },
376b72333dSSebastian Hesselbarth { .id = A370_CPU_TO_DRAMCLK, .name = "dramclk" },
386b72333dSSebastian Hesselbarth };
396b72333dSSebastian Hesselbarth
40682dfdc0SSachin Kamat static const u32 a370_tclk_freqs[] __initconst = {
411022c75fSSimon Guinot 166000000,
421022c75fSSimon Guinot 200000000,
436b72333dSSebastian Hesselbarth };
446b72333dSSebastian Hesselbarth
a370_get_tclk_freq(void __iomem * sar)456b72333dSSebastian Hesselbarth static u32 __init a370_get_tclk_freq(void __iomem *sar)
466b72333dSSebastian Hesselbarth {
476b72333dSSebastian Hesselbarth u8 tclk_freq_select = 0;
486b72333dSSebastian Hesselbarth
496b72333dSSebastian Hesselbarth tclk_freq_select = ((readl(sar) >> SARL_A370_TCLK_FREQ_OPT) &
506b72333dSSebastian Hesselbarth SARL_A370_TCLK_FREQ_OPT_MASK);
516b72333dSSebastian Hesselbarth return a370_tclk_freqs[tclk_freq_select];
526b72333dSSebastian Hesselbarth }
536b72333dSSebastian Hesselbarth
54682dfdc0SSachin Kamat static const u32 a370_cpu_freqs[] __initconst = {
556b72333dSSebastian Hesselbarth 400000000,
566b72333dSSebastian Hesselbarth 533000000,
576b72333dSSebastian Hesselbarth 667000000,
586b72333dSSebastian Hesselbarth 800000000,
596b72333dSSebastian Hesselbarth 1000000000,
606b72333dSSebastian Hesselbarth 1067000000,
616b72333dSSebastian Hesselbarth 1200000000,
626b72333dSSebastian Hesselbarth };
636b72333dSSebastian Hesselbarth
a370_get_cpu_freq(void __iomem * sar)646b72333dSSebastian Hesselbarth static u32 __init a370_get_cpu_freq(void __iomem *sar)
656b72333dSSebastian Hesselbarth {
666b72333dSSebastian Hesselbarth u32 cpu_freq;
676b72333dSSebastian Hesselbarth u8 cpu_freq_select = 0;
686b72333dSSebastian Hesselbarth
696b72333dSSebastian Hesselbarth cpu_freq_select = ((readl(sar) >> SARL_A370_PCLK_FREQ_OPT) &
706b72333dSSebastian Hesselbarth SARL_A370_PCLK_FREQ_OPT_MASK);
716b72333dSSebastian Hesselbarth if (cpu_freq_select >= ARRAY_SIZE(a370_cpu_freqs)) {
726b72333dSSebastian Hesselbarth pr_err("CPU freq select unsupported %d\n", cpu_freq_select);
736b72333dSSebastian Hesselbarth cpu_freq = 0;
746b72333dSSebastian Hesselbarth } else
756b72333dSSebastian Hesselbarth cpu_freq = a370_cpu_freqs[cpu_freq_select];
766b72333dSSebastian Hesselbarth
776b72333dSSebastian Hesselbarth return cpu_freq;
786b72333dSSebastian Hesselbarth }
796b72333dSSebastian Hesselbarth
80682dfdc0SSachin Kamat static const int a370_nbclk_ratios[32][2] __initconst = {
816b72333dSSebastian Hesselbarth {0, 1}, {1, 2}, {2, 2}, {2, 2},
826b72333dSSebastian Hesselbarth {1, 2}, {1, 2}, {1, 1}, {2, 3},
836b72333dSSebastian Hesselbarth {0, 1}, {1, 2}, {2, 4}, {0, 1},
846b72333dSSebastian Hesselbarth {1, 2}, {0, 1}, {0, 1}, {2, 2},
856b72333dSSebastian Hesselbarth {0, 1}, {0, 1}, {0, 1}, {1, 1},
866b72333dSSebastian Hesselbarth {2, 3}, {0, 1}, {0, 1}, {0, 1},
876b72333dSSebastian Hesselbarth {0, 1}, {0, 1}, {0, 1}, {1, 1},
886b72333dSSebastian Hesselbarth {0, 1}, {0, 1}, {0, 1}, {0, 1},
896b72333dSSebastian Hesselbarth };
906b72333dSSebastian Hesselbarth
91682dfdc0SSachin Kamat static const int a370_hclk_ratios[32][2] __initconst = {
926b72333dSSebastian Hesselbarth {0, 1}, {1, 2}, {2, 6}, {2, 3},
936b72333dSSebastian Hesselbarth {1, 3}, {1, 4}, {1, 2}, {2, 6},
946b72333dSSebastian Hesselbarth {0, 1}, {1, 6}, {2, 10}, {0, 1},
956b72333dSSebastian Hesselbarth {1, 4}, {0, 1}, {0, 1}, {2, 5},
966b72333dSSebastian Hesselbarth {0, 1}, {0, 1}, {0, 1}, {1, 2},
976b72333dSSebastian Hesselbarth {2, 6}, {0, 1}, {0, 1}, {0, 1},
986b72333dSSebastian Hesselbarth {0, 1}, {0, 1}, {0, 1}, {1, 1},
996b72333dSSebastian Hesselbarth {0, 1}, {0, 1}, {0, 1}, {0, 1},
1006b72333dSSebastian Hesselbarth };
1016b72333dSSebastian Hesselbarth
102682dfdc0SSachin Kamat static const int a370_dramclk_ratios[32][2] __initconst = {
1036b72333dSSebastian Hesselbarth {0, 1}, {1, 2}, {2, 3}, {2, 3},
1046b72333dSSebastian Hesselbarth {1, 3}, {1, 2}, {1, 2}, {2, 6},
1056b72333dSSebastian Hesselbarth {0, 1}, {1, 3}, {2, 5}, {0, 1},
1066b72333dSSebastian Hesselbarth {1, 4}, {0, 1}, {0, 1}, {2, 5},
1076b72333dSSebastian Hesselbarth {0, 1}, {0, 1}, {0, 1}, {1, 1},
1086b72333dSSebastian Hesselbarth {2, 3}, {0, 1}, {0, 1}, {0, 1},
1096b72333dSSebastian Hesselbarth {0, 1}, {0, 1}, {0, 1}, {1, 1},
1106b72333dSSebastian Hesselbarth {0, 1}, {0, 1}, {0, 1}, {0, 1},
1116b72333dSSebastian Hesselbarth };
1126b72333dSSebastian Hesselbarth
a370_get_clk_ratio(void __iomem * sar,int id,int * mult,int * div)1136b72333dSSebastian Hesselbarth static void __init a370_get_clk_ratio(
1146b72333dSSebastian Hesselbarth void __iomem *sar, int id, int *mult, int *div)
1156b72333dSSebastian Hesselbarth {
1166b72333dSSebastian Hesselbarth u32 opt = ((readl(sar) >> SARL_A370_FAB_FREQ_OPT) &
1176b72333dSSebastian Hesselbarth SARL_A370_FAB_FREQ_OPT_MASK);
1186b72333dSSebastian Hesselbarth
1196b72333dSSebastian Hesselbarth switch (id) {
1206b72333dSSebastian Hesselbarth case A370_CPU_TO_NBCLK:
1216b72333dSSebastian Hesselbarth *mult = a370_nbclk_ratios[opt][0];
1226b72333dSSebastian Hesselbarth *div = a370_nbclk_ratios[opt][1];
1236b72333dSSebastian Hesselbarth break;
1246b72333dSSebastian Hesselbarth case A370_CPU_TO_HCLK:
1256b72333dSSebastian Hesselbarth *mult = a370_hclk_ratios[opt][0];
1266b72333dSSebastian Hesselbarth *div = a370_hclk_ratios[opt][1];
1276b72333dSSebastian Hesselbarth break;
1286b72333dSSebastian Hesselbarth case A370_CPU_TO_DRAMCLK:
1296b72333dSSebastian Hesselbarth *mult = a370_dramclk_ratios[opt][0];
1306b72333dSSebastian Hesselbarth *div = a370_dramclk_ratios[opt][1];
1316b72333dSSebastian Hesselbarth break;
1326b72333dSSebastian Hesselbarth }
1336b72333dSSebastian Hesselbarth }
1346b72333dSSebastian Hesselbarth
a370_is_sscg_enabled(void __iomem * sar)1355e1a63f5SGregory CLEMENT static bool a370_is_sscg_enabled(void __iomem *sar)
1365e1a63f5SGregory CLEMENT {
1375e1a63f5SGregory CLEMENT return !(readl(sar) & SARL_A370_SSCG_ENABLE);
1385e1a63f5SGregory CLEMENT }
1395e1a63f5SGregory CLEMENT
1406b72333dSSebastian Hesselbarth static const struct coreclk_soc_desc a370_coreclks = {
1416b72333dSSebastian Hesselbarth .get_tclk_freq = a370_get_tclk_freq,
1426b72333dSSebastian Hesselbarth .get_cpu_freq = a370_get_cpu_freq,
1436b72333dSSebastian Hesselbarth .get_clk_ratio = a370_get_clk_ratio,
1445e1a63f5SGregory CLEMENT .is_sscg_enabled = a370_is_sscg_enabled,
1455e1a63f5SGregory CLEMENT .fix_sscg_deviation = kirkwood_fix_sscg_deviation,
1466b72333dSSebastian Hesselbarth .ratios = a370_coreclk_ratios,
1476b72333dSSebastian Hesselbarth .num_ratios = ARRAY_SIZE(a370_coreclk_ratios),
1486b72333dSSebastian Hesselbarth };
1496b72333dSSebastian Hesselbarth
1506b72333dSSebastian Hesselbarth /*
1516b72333dSSebastian Hesselbarth * Clock Gating Control
1526b72333dSSebastian Hesselbarth */
1536b72333dSSebastian Hesselbarth
154682dfdc0SSachin Kamat static const struct clk_gating_soc_desc a370_gating_desc[] __initconst = {
1556b72333dSSebastian Hesselbarth { "audio", NULL, 0, 0 },
1566b72333dSSebastian Hesselbarth { "pex0_en", NULL, 1, 0 },
1576b72333dSSebastian Hesselbarth { "pex1_en", NULL, 2, 0 },
1586b72333dSSebastian Hesselbarth { "ge1", NULL, 3, 0 },
1596b72333dSSebastian Hesselbarth { "ge0", NULL, 4, 0 },
1606b72333dSSebastian Hesselbarth { "pex0", "pex0_en", 5, 0 },
1616b72333dSSebastian Hesselbarth { "pex1", "pex1_en", 9, 0 },
1626b72333dSSebastian Hesselbarth { "sata0", NULL, 15, 0 },
1636b72333dSSebastian Hesselbarth { "sdio", NULL, 17, 0 },
164dc627eeaSBoris Brezillon { "crypto", NULL, 23, CLK_IGNORE_UNUSED },
1656b72333dSSebastian Hesselbarth { "tdm", NULL, 25, 0 },
1666b72333dSSebastian Hesselbarth { "ddr", NULL, 28, CLK_IGNORE_UNUSED },
1676b72333dSSebastian Hesselbarth { "sata1", NULL, 30, 0 },
1686b72333dSSebastian Hesselbarth { }
1696b72333dSSebastian Hesselbarth };
1706b72333dSSebastian Hesselbarth
a370_clk_init(struct device_node * np)17107ad6836SSebastian Hesselbarth static void __init a370_clk_init(struct device_node *np)
1726b72333dSSebastian Hesselbarth {
17307ad6836SSebastian Hesselbarth struct device_node *cgnp =
17407ad6836SSebastian Hesselbarth of_find_compatible_node(NULL, NULL, "marvell,armada-370-gating-clock");
17507ad6836SSebastian Hesselbarth
17607ad6836SSebastian Hesselbarth mvebu_coreclk_setup(np, &a370_coreclks);
17707ad6836SSebastian Hesselbarth
178*a3c24050SYangtao Li if (cgnp) {
17907ad6836SSebastian Hesselbarth mvebu_clk_gating_setup(cgnp, a370_gating_desc);
180*a3c24050SYangtao Li of_node_put(cgnp);
181*a3c24050SYangtao Li }
1826b72333dSSebastian Hesselbarth }
18307ad6836SSebastian Hesselbarth CLK_OF_DECLARE(a370_clk, "marvell,armada-370-core-clock", a370_clk_init);
18407ad6836SSebastian Hesselbarth
185