18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Marvell Armada XP 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 * Armada XP Sample At Reset is a 64 bit bitfiled split in two
238c2ecf20Sopenharmony_ci * register of 32 bits
248c2ecf20Sopenharmony_ci */
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define SARL				0	/* Low part [0:31] */
278c2ecf20Sopenharmony_ci#define	 SARL_AXP_PCLK_FREQ_OPT		21
288c2ecf20Sopenharmony_ci#define	 SARL_AXP_PCLK_FREQ_OPT_MASK	0x7
298c2ecf20Sopenharmony_ci#define	 SARL_AXP_FAB_FREQ_OPT		24
308c2ecf20Sopenharmony_ci#define	 SARL_AXP_FAB_FREQ_OPT_MASK	0xF
318c2ecf20Sopenharmony_ci#define SARH				4	/* High part [32:63] */
328c2ecf20Sopenharmony_ci#define	 SARH_AXP_PCLK_FREQ_OPT		(52-32)
338c2ecf20Sopenharmony_ci#define	 SARH_AXP_PCLK_FREQ_OPT_MASK	0x1
348c2ecf20Sopenharmony_ci#define	 SARH_AXP_PCLK_FREQ_OPT_SHIFT	3
358c2ecf20Sopenharmony_ci#define	 SARH_AXP_FAB_FREQ_OPT		(51-32)
368c2ecf20Sopenharmony_ci#define	 SARH_AXP_FAB_FREQ_OPT_MASK	0x1
378c2ecf20Sopenharmony_ci#define	 SARH_AXP_FAB_FREQ_OPT_SHIFT	4
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cienum { AXP_CPU_TO_NBCLK, AXP_CPU_TO_HCLK, AXP_CPU_TO_DRAMCLK };
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistatic const struct coreclk_ratio axp_coreclk_ratios[] __initconst = {
428c2ecf20Sopenharmony_ci	{ .id = AXP_CPU_TO_NBCLK, .name = "nbclk" },
438c2ecf20Sopenharmony_ci	{ .id = AXP_CPU_TO_HCLK, .name = "hclk" },
448c2ecf20Sopenharmony_ci	{ .id = AXP_CPU_TO_DRAMCLK, .name = "dramclk" },
458c2ecf20Sopenharmony_ci};
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci/* Armada XP TCLK frequency is fixed to 250MHz */
488c2ecf20Sopenharmony_cistatic u32 __init axp_get_tclk_freq(void __iomem *sar)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	return 250000000;
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic const u32 axp_cpu_freqs[] __initconst = {
548c2ecf20Sopenharmony_ci	1000000000,
558c2ecf20Sopenharmony_ci	1066000000,
568c2ecf20Sopenharmony_ci	1200000000,
578c2ecf20Sopenharmony_ci	1333000000,
588c2ecf20Sopenharmony_ci	1500000000,
598c2ecf20Sopenharmony_ci	1666000000,
608c2ecf20Sopenharmony_ci	1800000000,
618c2ecf20Sopenharmony_ci	2000000000,
628c2ecf20Sopenharmony_ci	667000000,
638c2ecf20Sopenharmony_ci	0,
648c2ecf20Sopenharmony_ci	800000000,
658c2ecf20Sopenharmony_ci	1600000000,
668c2ecf20Sopenharmony_ci};
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic u32 __init axp_get_cpu_freq(void __iomem *sar)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	u32 cpu_freq;
718c2ecf20Sopenharmony_ci	u8 cpu_freq_select = 0;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	cpu_freq_select = ((readl(sar + SARL) >> SARL_AXP_PCLK_FREQ_OPT) &
748c2ecf20Sopenharmony_ci			   SARL_AXP_PCLK_FREQ_OPT_MASK);
758c2ecf20Sopenharmony_ci	/*
768c2ecf20Sopenharmony_ci	 * The upper bit is not contiguous to the other ones and
778c2ecf20Sopenharmony_ci	 * located in the high part of the SAR registers
788c2ecf20Sopenharmony_ci	 */
798c2ecf20Sopenharmony_ci	cpu_freq_select |= (((readl(sar + SARH) >> SARH_AXP_PCLK_FREQ_OPT) &
808c2ecf20Sopenharmony_ci	     SARH_AXP_PCLK_FREQ_OPT_MASK) << SARH_AXP_PCLK_FREQ_OPT_SHIFT);
818c2ecf20Sopenharmony_ci	if (cpu_freq_select >= ARRAY_SIZE(axp_cpu_freqs)) {
828c2ecf20Sopenharmony_ci		pr_err("CPU freq select unsupported: %d\n", cpu_freq_select);
838c2ecf20Sopenharmony_ci		cpu_freq = 0;
848c2ecf20Sopenharmony_ci	} else
858c2ecf20Sopenharmony_ci		cpu_freq = axp_cpu_freqs[cpu_freq_select];
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	return cpu_freq;
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistatic const int axp_nbclk_ratios[32][2] __initconst = {
918c2ecf20Sopenharmony_ci	{0, 1}, {1, 2}, {2, 2}, {2, 2},
928c2ecf20Sopenharmony_ci	{1, 2}, {1, 2}, {1, 1}, {2, 3},
938c2ecf20Sopenharmony_ci	{0, 1}, {1, 2}, {2, 4}, {0, 1},
948c2ecf20Sopenharmony_ci	{1, 2}, {0, 1}, {0, 1}, {2, 2},
958c2ecf20Sopenharmony_ci	{0, 1}, {0, 1}, {0, 1}, {1, 1},
968c2ecf20Sopenharmony_ci	{2, 3}, {0, 1}, {0, 1}, {0, 1},
978c2ecf20Sopenharmony_ci	{0, 1}, {0, 1}, {0, 1}, {1, 1},
988c2ecf20Sopenharmony_ci	{0, 1}, {0, 1}, {0, 1}, {0, 1},
998c2ecf20Sopenharmony_ci};
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistatic const int axp_hclk_ratios[32][2] __initconst = {
1028c2ecf20Sopenharmony_ci	{0, 1}, {1, 2}, {2, 6}, {2, 3},
1038c2ecf20Sopenharmony_ci	{1, 3}, {1, 4}, {1, 2}, {2, 6},
1048c2ecf20Sopenharmony_ci	{0, 1}, {1, 6}, {2, 10}, {0, 1},
1058c2ecf20Sopenharmony_ci	{1, 4}, {0, 1}, {0, 1}, {2, 5},
1068c2ecf20Sopenharmony_ci	{0, 1}, {0, 1}, {0, 1}, {1, 2},
1078c2ecf20Sopenharmony_ci	{2, 6}, {0, 1}, {0, 1}, {0, 1},
1088c2ecf20Sopenharmony_ci	{0, 1}, {0, 1}, {0, 1}, {1, 1},
1098c2ecf20Sopenharmony_ci	{0, 1}, {0, 1}, {0, 1}, {0, 1},
1108c2ecf20Sopenharmony_ci};
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistatic const int axp_dramclk_ratios[32][2] __initconst = {
1138c2ecf20Sopenharmony_ci	{0, 1}, {1, 2}, {2, 3}, {2, 3},
1148c2ecf20Sopenharmony_ci	{1, 3}, {1, 2}, {1, 2}, {2, 6},
1158c2ecf20Sopenharmony_ci	{0, 1}, {1, 3}, {2, 5}, {0, 1},
1168c2ecf20Sopenharmony_ci	{1, 4}, {0, 1}, {0, 1}, {2, 5},
1178c2ecf20Sopenharmony_ci	{0, 1}, {0, 1}, {0, 1}, {1, 1},
1188c2ecf20Sopenharmony_ci	{2, 3}, {0, 1}, {0, 1}, {0, 1},
1198c2ecf20Sopenharmony_ci	{0, 1}, {0, 1}, {0, 1}, {1, 1},
1208c2ecf20Sopenharmony_ci	{0, 1}, {0, 1}, {0, 1}, {0, 1},
1218c2ecf20Sopenharmony_ci};
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic void __init axp_get_clk_ratio(
1248c2ecf20Sopenharmony_ci	void __iomem *sar, int id, int *mult, int *div)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	u32 opt = ((readl(sar + SARL) >> SARL_AXP_FAB_FREQ_OPT) &
1278c2ecf20Sopenharmony_ci	      SARL_AXP_FAB_FREQ_OPT_MASK);
1288c2ecf20Sopenharmony_ci	/*
1298c2ecf20Sopenharmony_ci	 * The upper bit is not contiguous to the other ones and
1308c2ecf20Sopenharmony_ci	 * located in the high part of the SAR registers
1318c2ecf20Sopenharmony_ci	 */
1328c2ecf20Sopenharmony_ci	opt |= (((readl(sar + SARH) >> SARH_AXP_FAB_FREQ_OPT) &
1338c2ecf20Sopenharmony_ci		 SARH_AXP_FAB_FREQ_OPT_MASK) << SARH_AXP_FAB_FREQ_OPT_SHIFT);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	switch (id) {
1368c2ecf20Sopenharmony_ci	case AXP_CPU_TO_NBCLK:
1378c2ecf20Sopenharmony_ci		*mult = axp_nbclk_ratios[opt][0];
1388c2ecf20Sopenharmony_ci		*div = axp_nbclk_ratios[opt][1];
1398c2ecf20Sopenharmony_ci		break;
1408c2ecf20Sopenharmony_ci	case AXP_CPU_TO_HCLK:
1418c2ecf20Sopenharmony_ci		*mult = axp_hclk_ratios[opt][0];
1428c2ecf20Sopenharmony_ci		*div = axp_hclk_ratios[opt][1];
1438c2ecf20Sopenharmony_ci		break;
1448c2ecf20Sopenharmony_ci	case AXP_CPU_TO_DRAMCLK:
1458c2ecf20Sopenharmony_ci		*mult = axp_dramclk_ratios[opt][0];
1468c2ecf20Sopenharmony_ci		*div = axp_dramclk_ratios[opt][1];
1478c2ecf20Sopenharmony_ci		break;
1488c2ecf20Sopenharmony_ci	}
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cistatic const struct coreclk_soc_desc axp_coreclks = {
1528c2ecf20Sopenharmony_ci	.get_tclk_freq = axp_get_tclk_freq,
1538c2ecf20Sopenharmony_ci	.get_cpu_freq = axp_get_cpu_freq,
1548c2ecf20Sopenharmony_ci	.get_clk_ratio = axp_get_clk_ratio,
1558c2ecf20Sopenharmony_ci	.ratios = axp_coreclk_ratios,
1568c2ecf20Sopenharmony_ci	.num_ratios = ARRAY_SIZE(axp_coreclk_ratios),
1578c2ecf20Sopenharmony_ci};
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci/*
1608c2ecf20Sopenharmony_ci * Clock Gating Control
1618c2ecf20Sopenharmony_ci */
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_cistatic const struct clk_gating_soc_desc axp_gating_desc[] __initconst = {
1648c2ecf20Sopenharmony_ci	{ "audio", NULL, 0, 0 },
1658c2ecf20Sopenharmony_ci	{ "ge3", NULL, 1, 0 },
1668c2ecf20Sopenharmony_ci	{ "ge2", NULL,  2, 0 },
1678c2ecf20Sopenharmony_ci	{ "ge1", NULL, 3, 0 },
1688c2ecf20Sopenharmony_ci	{ "ge0", NULL, 4, 0 },
1698c2ecf20Sopenharmony_ci	{ "pex00", NULL, 5, 0 },
1708c2ecf20Sopenharmony_ci	{ "pex01", NULL, 6, 0 },
1718c2ecf20Sopenharmony_ci	{ "pex02", NULL, 7, 0 },
1728c2ecf20Sopenharmony_ci	{ "pex03", NULL, 8, 0 },
1738c2ecf20Sopenharmony_ci	{ "pex10", NULL, 9, 0 },
1748c2ecf20Sopenharmony_ci	{ "pex11", NULL, 10, 0 },
1758c2ecf20Sopenharmony_ci	{ "pex12", NULL, 11, 0 },
1768c2ecf20Sopenharmony_ci	{ "pex13", NULL, 12, 0 },
1778c2ecf20Sopenharmony_ci	{ "bp", NULL, 13, 0 },
1788c2ecf20Sopenharmony_ci	{ "sata0lnk", NULL, 14, 0 },
1798c2ecf20Sopenharmony_ci	{ "sata0", "sata0lnk", 15, 0 },
1808c2ecf20Sopenharmony_ci	{ "lcd", NULL, 16, 0 },
1818c2ecf20Sopenharmony_ci	{ "sdio", NULL, 17, 0 },
1828c2ecf20Sopenharmony_ci	{ "usb0", NULL, 18, 0 },
1838c2ecf20Sopenharmony_ci	{ "usb1", NULL, 19, 0 },
1848c2ecf20Sopenharmony_ci	{ "usb2", NULL, 20, 0 },
1858c2ecf20Sopenharmony_ci	{ "xor0", NULL, 22, 0 },
1868c2ecf20Sopenharmony_ci	{ "crypto", NULL, 23, 0 },
1878c2ecf20Sopenharmony_ci	{ "tdm", NULL, 25, 0 },
1888c2ecf20Sopenharmony_ci	{ "pex20", NULL, 26, 0 },
1898c2ecf20Sopenharmony_ci	{ "pex30", NULL, 27, 0 },
1908c2ecf20Sopenharmony_ci	{ "xor1", NULL, 28, 0 },
1918c2ecf20Sopenharmony_ci	{ "sata1lnk", NULL, 29, 0 },
1928c2ecf20Sopenharmony_ci	{ "sata1", "sata1lnk", 30, 0 },
1938c2ecf20Sopenharmony_ci	{ }
1948c2ecf20Sopenharmony_ci};
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_cistatic void __init axp_clk_init(struct device_node *np)
1978c2ecf20Sopenharmony_ci{
1988c2ecf20Sopenharmony_ci	struct device_node *cgnp =
1998c2ecf20Sopenharmony_ci		of_find_compatible_node(NULL, NULL, "marvell,armada-xp-gating-clock");
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	mvebu_coreclk_setup(np, &axp_coreclks);
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	if (cgnp) {
2048c2ecf20Sopenharmony_ci		mvebu_clk_gating_setup(cgnp, axp_gating_desc);
2058c2ecf20Sopenharmony_ci		of_node_put(cgnp);
2068c2ecf20Sopenharmony_ci	}
2078c2ecf20Sopenharmony_ci}
2088c2ecf20Sopenharmony_ciCLK_OF_DECLARE(axp_clk, "marvell,armada-xp-core-clock", axp_clk_init);
209