18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Marvell Armada 370 SoC clocks
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2012 Marvell
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Gregory CLEMENT <gregory.clement@free-electrons.com>
88c2ecf20Sopenharmony_ci * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
98c2ecf20Sopenharmony_ci * Andrew Lunn <andrew@lunn.ch>
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/kernel.h>
148c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
158c2ecf20Sopenharmony_ci#include <linux/io.h>
168c2ecf20Sopenharmony_ci#include <linux/of.h>
178c2ecf20Sopenharmony_ci#include "common.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/*
208c2ecf20Sopenharmony_ci * Core Clocks
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define SARL				0	/* Low part [0:31] */
248c2ecf20Sopenharmony_ci#define	 SARL_A370_SSCG_ENABLE		BIT(10)
258c2ecf20Sopenharmony_ci#define	 SARL_A370_PCLK_FREQ_OPT	11
268c2ecf20Sopenharmony_ci#define	 SARL_A370_PCLK_FREQ_OPT_MASK	0xF
278c2ecf20Sopenharmony_ci#define	 SARL_A370_FAB_FREQ_OPT		15
288c2ecf20Sopenharmony_ci#define	 SARL_A370_FAB_FREQ_OPT_MASK	0x1F
298c2ecf20Sopenharmony_ci#define	 SARL_A370_TCLK_FREQ_OPT	20
308c2ecf20Sopenharmony_ci#define	 SARL_A370_TCLK_FREQ_OPT_MASK	0x1
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cienum { A370_CPU_TO_NBCLK, A370_CPU_TO_HCLK, A370_CPU_TO_DRAMCLK };
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic const struct coreclk_ratio a370_coreclk_ratios[] __initconst = {
358c2ecf20Sopenharmony_ci	{ .id = A370_CPU_TO_NBCLK, .name = "nbclk" },
368c2ecf20Sopenharmony_ci	{ .id = A370_CPU_TO_HCLK, .name = "hclk" },
378c2ecf20Sopenharmony_ci	{ .id = A370_CPU_TO_DRAMCLK, .name = "dramclk" },
388c2ecf20Sopenharmony_ci};
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic const u32 a370_tclk_freqs[] __initconst = {
418c2ecf20Sopenharmony_ci	166000000,
428c2ecf20Sopenharmony_ci	200000000,
438c2ecf20Sopenharmony_ci};
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic u32 __init a370_get_tclk_freq(void __iomem *sar)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	u8 tclk_freq_select = 0;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	tclk_freq_select = ((readl(sar) >> SARL_A370_TCLK_FREQ_OPT) &
508c2ecf20Sopenharmony_ci			    SARL_A370_TCLK_FREQ_OPT_MASK);
518c2ecf20Sopenharmony_ci	return a370_tclk_freqs[tclk_freq_select];
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic const u32 a370_cpu_freqs[] __initconst = {
558c2ecf20Sopenharmony_ci	400000000,
568c2ecf20Sopenharmony_ci	533000000,
578c2ecf20Sopenharmony_ci	667000000,
588c2ecf20Sopenharmony_ci	800000000,
598c2ecf20Sopenharmony_ci	1000000000,
608c2ecf20Sopenharmony_ci	1067000000,
618c2ecf20Sopenharmony_ci	1200000000,
628c2ecf20Sopenharmony_ci};
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic u32 __init a370_get_cpu_freq(void __iomem *sar)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	u32 cpu_freq;
678c2ecf20Sopenharmony_ci	u8 cpu_freq_select = 0;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	cpu_freq_select = ((readl(sar) >> SARL_A370_PCLK_FREQ_OPT) &
708c2ecf20Sopenharmony_ci			   SARL_A370_PCLK_FREQ_OPT_MASK);
718c2ecf20Sopenharmony_ci	if (cpu_freq_select >= ARRAY_SIZE(a370_cpu_freqs)) {
728c2ecf20Sopenharmony_ci		pr_err("CPU freq select unsupported %d\n", cpu_freq_select);
738c2ecf20Sopenharmony_ci		cpu_freq = 0;
748c2ecf20Sopenharmony_ci	} else
758c2ecf20Sopenharmony_ci		cpu_freq = a370_cpu_freqs[cpu_freq_select];
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	return cpu_freq;
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic const int a370_nbclk_ratios[32][2] __initconst = {
818c2ecf20Sopenharmony_ci	{0, 1}, {1, 2}, {2, 2}, {2, 2},
828c2ecf20Sopenharmony_ci	{1, 2}, {1, 2}, {1, 1}, {2, 3},
838c2ecf20Sopenharmony_ci	{0, 1}, {1, 2}, {2, 4}, {0, 1},
848c2ecf20Sopenharmony_ci	{1, 2}, {0, 1}, {0, 1}, {2, 2},
858c2ecf20Sopenharmony_ci	{0, 1}, {0, 1}, {0, 1}, {1, 1},
868c2ecf20Sopenharmony_ci	{2, 3}, {0, 1}, {0, 1}, {0, 1},
878c2ecf20Sopenharmony_ci	{0, 1}, {0, 1}, {0, 1}, {1, 1},
888c2ecf20Sopenharmony_ci	{0, 1}, {0, 1}, {0, 1}, {0, 1},
898c2ecf20Sopenharmony_ci};
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic const int a370_hclk_ratios[32][2] __initconst = {
928c2ecf20Sopenharmony_ci	{0, 1}, {1, 2}, {2, 6}, {2, 3},
938c2ecf20Sopenharmony_ci	{1, 3}, {1, 4}, {1, 2}, {2, 6},
948c2ecf20Sopenharmony_ci	{0, 1}, {1, 6}, {2, 10}, {0, 1},
958c2ecf20Sopenharmony_ci	{1, 4}, {0, 1}, {0, 1}, {2, 5},
968c2ecf20Sopenharmony_ci	{0, 1}, {0, 1}, {0, 1}, {1, 2},
978c2ecf20Sopenharmony_ci	{2, 6}, {0, 1}, {0, 1}, {0, 1},
988c2ecf20Sopenharmony_ci	{0, 1}, {0, 1}, {0, 1}, {1, 1},
998c2ecf20Sopenharmony_ci	{0, 1}, {0, 1}, {0, 1}, {0, 1},
1008c2ecf20Sopenharmony_ci};
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic const int a370_dramclk_ratios[32][2] __initconst = {
1038c2ecf20Sopenharmony_ci	{0, 1}, {1, 2}, {2, 3}, {2, 3},
1048c2ecf20Sopenharmony_ci	{1, 3}, {1, 2}, {1, 2}, {2, 6},
1058c2ecf20Sopenharmony_ci	{0, 1}, {1, 3}, {2, 5}, {0, 1},
1068c2ecf20Sopenharmony_ci	{1, 4}, {0, 1}, {0, 1}, {2, 5},
1078c2ecf20Sopenharmony_ci	{0, 1}, {0, 1}, {0, 1}, {1, 1},
1088c2ecf20Sopenharmony_ci	{2, 3}, {0, 1}, {0, 1}, {0, 1},
1098c2ecf20Sopenharmony_ci	{0, 1}, {0, 1}, {0, 1}, {1, 1},
1108c2ecf20Sopenharmony_ci	{0, 1}, {0, 1}, {0, 1}, {0, 1},
1118c2ecf20Sopenharmony_ci};
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic void __init a370_get_clk_ratio(
1148c2ecf20Sopenharmony_ci	void __iomem *sar, int id, int *mult, int *div)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	u32 opt = ((readl(sar) >> SARL_A370_FAB_FREQ_OPT) &
1178c2ecf20Sopenharmony_ci		SARL_A370_FAB_FREQ_OPT_MASK);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	switch (id) {
1208c2ecf20Sopenharmony_ci	case A370_CPU_TO_NBCLK:
1218c2ecf20Sopenharmony_ci		*mult = a370_nbclk_ratios[opt][0];
1228c2ecf20Sopenharmony_ci		*div = a370_nbclk_ratios[opt][1];
1238c2ecf20Sopenharmony_ci		break;
1248c2ecf20Sopenharmony_ci	case A370_CPU_TO_HCLK:
1258c2ecf20Sopenharmony_ci		*mult = a370_hclk_ratios[opt][0];
1268c2ecf20Sopenharmony_ci		*div = a370_hclk_ratios[opt][1];
1278c2ecf20Sopenharmony_ci		break;
1288c2ecf20Sopenharmony_ci	case A370_CPU_TO_DRAMCLK:
1298c2ecf20Sopenharmony_ci		*mult = a370_dramclk_ratios[opt][0];
1308c2ecf20Sopenharmony_ci		*div = a370_dramclk_ratios[opt][1];
1318c2ecf20Sopenharmony_ci		break;
1328c2ecf20Sopenharmony_ci	}
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic bool a370_is_sscg_enabled(void __iomem *sar)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	return !(readl(sar) & SARL_A370_SSCG_ENABLE);
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistatic const struct coreclk_soc_desc a370_coreclks = {
1418c2ecf20Sopenharmony_ci	.get_tclk_freq = a370_get_tclk_freq,
1428c2ecf20Sopenharmony_ci	.get_cpu_freq = a370_get_cpu_freq,
1438c2ecf20Sopenharmony_ci	.get_clk_ratio = a370_get_clk_ratio,
1448c2ecf20Sopenharmony_ci	.is_sscg_enabled = a370_is_sscg_enabled,
1458c2ecf20Sopenharmony_ci	.fix_sscg_deviation = kirkwood_fix_sscg_deviation,
1468c2ecf20Sopenharmony_ci	.ratios = a370_coreclk_ratios,
1478c2ecf20Sopenharmony_ci	.num_ratios = ARRAY_SIZE(a370_coreclk_ratios),
1488c2ecf20Sopenharmony_ci};
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci/*
1518c2ecf20Sopenharmony_ci * Clock Gating Control
1528c2ecf20Sopenharmony_ci */
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic const struct clk_gating_soc_desc a370_gating_desc[] __initconst = {
1558c2ecf20Sopenharmony_ci	{ "audio", NULL, 0, 0 },
1568c2ecf20Sopenharmony_ci	{ "pex0_en", NULL, 1, 0 },
1578c2ecf20Sopenharmony_ci	{ "pex1_en", NULL,  2, 0 },
1588c2ecf20Sopenharmony_ci	{ "ge1", NULL, 3, 0 },
1598c2ecf20Sopenharmony_ci	{ "ge0", NULL, 4, 0 },
1608c2ecf20Sopenharmony_ci	{ "pex0", "pex0_en", 5, 0 },
1618c2ecf20Sopenharmony_ci	{ "pex1", "pex1_en", 9, 0 },
1628c2ecf20Sopenharmony_ci	{ "sata0", NULL, 15, 0 },
1638c2ecf20Sopenharmony_ci	{ "sdio", NULL, 17, 0 },
1648c2ecf20Sopenharmony_ci	{ "crypto", NULL, 23, CLK_IGNORE_UNUSED },
1658c2ecf20Sopenharmony_ci	{ "tdm", NULL, 25, 0 },
1668c2ecf20Sopenharmony_ci	{ "ddr", NULL, 28, CLK_IGNORE_UNUSED },
1678c2ecf20Sopenharmony_ci	{ "sata1", NULL, 30, 0 },
1688c2ecf20Sopenharmony_ci	{ }
1698c2ecf20Sopenharmony_ci};
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic void __init a370_clk_init(struct device_node *np)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	struct device_node *cgnp =
1748c2ecf20Sopenharmony_ci		of_find_compatible_node(NULL, NULL, "marvell,armada-370-gating-clock");
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	mvebu_coreclk_setup(np, &a370_coreclks);
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	if (cgnp) {
1798c2ecf20Sopenharmony_ci		mvebu_clk_gating_setup(cgnp, a370_gating_desc);
1808c2ecf20Sopenharmony_ci		of_node_put(cgnp);
1818c2ecf20Sopenharmony_ci	}
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ciCLK_OF_DECLARE(a370_clk, "marvell,armada-370-core-clock", a370_clk_init);
1848c2ecf20Sopenharmony_ci
185