18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Marvell Armada 380/385 SoC clocks
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2014 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 * SAR[14:10] : Ratios between PCLK0, NBCLK, HCLK and DRAM clocks
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * SAR[15]    : TCLK frequency
238c2ecf20Sopenharmony_ci *		 0 = 250 MHz
248c2ecf20Sopenharmony_ci *		 1 = 200 MHz
258c2ecf20Sopenharmony_ci */
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define SAR_A380_TCLK_FREQ_OPT		  15
288c2ecf20Sopenharmony_ci#define SAR_A380_TCLK_FREQ_OPT_MASK	  0x1
298c2ecf20Sopenharmony_ci#define SAR_A380_CPU_DDR_L2_FREQ_OPT	  10
308c2ecf20Sopenharmony_ci#define SAR_A380_CPU_DDR_L2_FREQ_OPT_MASK 0x1F
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic const u32 armada_38x_tclk_frequencies[] __initconst = {
338c2ecf20Sopenharmony_ci	250000000,
348c2ecf20Sopenharmony_ci	200000000,
358c2ecf20Sopenharmony_ci};
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic u32 __init armada_38x_get_tclk_freq(void __iomem *sar)
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	u8 tclk_freq_select;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	tclk_freq_select = ((readl(sar) >> SAR_A380_TCLK_FREQ_OPT) &
428c2ecf20Sopenharmony_ci			    SAR_A380_TCLK_FREQ_OPT_MASK);
438c2ecf20Sopenharmony_ci	return armada_38x_tclk_frequencies[tclk_freq_select];
448c2ecf20Sopenharmony_ci}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic const u32 armada_38x_cpu_frequencies[] __initconst = {
478c2ecf20Sopenharmony_ci	666 * 1000 * 1000,  0, 800 * 1000 * 1000, 0,
488c2ecf20Sopenharmony_ci	1066 * 1000 * 1000, 0, 1200 * 1000 * 1000, 0,
498c2ecf20Sopenharmony_ci	1332 * 1000 * 1000, 0, 0, 0,
508c2ecf20Sopenharmony_ci	1600 * 1000 * 1000, 0, 0, 0,
518c2ecf20Sopenharmony_ci	1866 * 1000 * 1000, 0, 0, 2000 * 1000 * 1000,
528c2ecf20Sopenharmony_ci};
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic u32 __init armada_38x_get_cpu_freq(void __iomem *sar)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	u8 cpu_freq_select;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	cpu_freq_select = ((readl(sar) >> SAR_A380_CPU_DDR_L2_FREQ_OPT) &
598c2ecf20Sopenharmony_ci			   SAR_A380_CPU_DDR_L2_FREQ_OPT_MASK);
608c2ecf20Sopenharmony_ci	if (cpu_freq_select >= ARRAY_SIZE(armada_38x_cpu_frequencies)) {
618c2ecf20Sopenharmony_ci		pr_err("Selected CPU frequency (%d) unsupported\n",
628c2ecf20Sopenharmony_ci			cpu_freq_select);
638c2ecf20Sopenharmony_ci		return 0;
648c2ecf20Sopenharmony_ci	}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	return armada_38x_cpu_frequencies[cpu_freq_select];
678c2ecf20Sopenharmony_ci}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cienum { A380_CPU_TO_DDR, A380_CPU_TO_L2 };
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic const struct coreclk_ratio armada_38x_coreclk_ratios[] __initconst = {
728c2ecf20Sopenharmony_ci	{ .id = A380_CPU_TO_L2,	 .name = "l2clk" },
738c2ecf20Sopenharmony_ci	{ .id = A380_CPU_TO_DDR, .name = "ddrclk" },
748c2ecf20Sopenharmony_ci};
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic const int armada_38x_cpu_l2_ratios[32][2] __initconst = {
778c2ecf20Sopenharmony_ci	{1, 2}, {0, 1}, {1, 2}, {0, 1},
788c2ecf20Sopenharmony_ci	{1, 2}, {0, 1}, {1, 2}, {0, 1},
798c2ecf20Sopenharmony_ci	{1, 2}, {0, 1}, {0, 1}, {0, 1},
808c2ecf20Sopenharmony_ci	{1, 2}, {0, 1}, {0, 1}, {0, 1},
818c2ecf20Sopenharmony_ci	{1, 2}, {0, 1}, {0, 1}, {1, 2},
828c2ecf20Sopenharmony_ci	{0, 1}, {0, 1}, {0, 1}, {0, 1},
838c2ecf20Sopenharmony_ci	{0, 1}, {0, 1}, {0, 1}, {0, 1},
848c2ecf20Sopenharmony_ci	{0, 1}, {0, 1}, {0, 1}, {0, 1},
858c2ecf20Sopenharmony_ci};
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic const int armada_38x_cpu_ddr_ratios[32][2] __initconst = {
888c2ecf20Sopenharmony_ci	{0, 1}, {0, 1}, {0, 1}, {0, 1},
898c2ecf20Sopenharmony_ci	{1, 2}, {0, 1}, {0, 1}, {0, 1},
908c2ecf20Sopenharmony_ci	{1, 2}, {0, 1}, {0, 1}, {0, 1},
918c2ecf20Sopenharmony_ci	{1, 2}, {0, 1}, {0, 1}, {0, 1},
928c2ecf20Sopenharmony_ci	{1, 2}, {0, 1}, {0, 1}, {7, 15},
938c2ecf20Sopenharmony_ci	{0, 1}, {0, 1}, {0, 1}, {0, 1},
948c2ecf20Sopenharmony_ci	{0, 1}, {0, 1}, {0, 1}, {0, 1},
958c2ecf20Sopenharmony_ci	{0, 1}, {0, 1}, {0, 1}, {0, 1},
968c2ecf20Sopenharmony_ci};
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic void __init armada_38x_get_clk_ratio(
998c2ecf20Sopenharmony_ci	void __iomem *sar, int id, int *mult, int *div)
1008c2ecf20Sopenharmony_ci{
1018c2ecf20Sopenharmony_ci	u32 opt = ((readl(sar) >> SAR_A380_CPU_DDR_L2_FREQ_OPT) &
1028c2ecf20Sopenharmony_ci		SAR_A380_CPU_DDR_L2_FREQ_OPT_MASK);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	switch (id) {
1058c2ecf20Sopenharmony_ci	case A380_CPU_TO_L2:
1068c2ecf20Sopenharmony_ci		*mult = armada_38x_cpu_l2_ratios[opt][0];
1078c2ecf20Sopenharmony_ci		*div = armada_38x_cpu_l2_ratios[opt][1];
1088c2ecf20Sopenharmony_ci		break;
1098c2ecf20Sopenharmony_ci	case A380_CPU_TO_DDR:
1108c2ecf20Sopenharmony_ci		*mult = armada_38x_cpu_ddr_ratios[opt][0];
1118c2ecf20Sopenharmony_ci		*div = armada_38x_cpu_ddr_ratios[opt][1];
1128c2ecf20Sopenharmony_ci		break;
1138c2ecf20Sopenharmony_ci	}
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic const struct coreclk_soc_desc armada_38x_coreclks = {
1178c2ecf20Sopenharmony_ci	.get_tclk_freq = armada_38x_get_tclk_freq,
1188c2ecf20Sopenharmony_ci	.get_cpu_freq = armada_38x_get_cpu_freq,
1198c2ecf20Sopenharmony_ci	.get_clk_ratio = armada_38x_get_clk_ratio,
1208c2ecf20Sopenharmony_ci	.ratios = armada_38x_coreclk_ratios,
1218c2ecf20Sopenharmony_ci	.num_ratios = ARRAY_SIZE(armada_38x_coreclk_ratios),
1228c2ecf20Sopenharmony_ci};
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic void __init armada_38x_coreclk_init(struct device_node *np)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	mvebu_coreclk_setup(np, &armada_38x_coreclks);
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ciCLK_OF_DECLARE(armada_38x_core_clk, "marvell,armada-380-core-clock",
1298c2ecf20Sopenharmony_ci	       armada_38x_coreclk_init);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci/*
1328c2ecf20Sopenharmony_ci * Clock Gating Control
1338c2ecf20Sopenharmony_ci */
1348c2ecf20Sopenharmony_cistatic const struct clk_gating_soc_desc armada_38x_gating_desc[] __initconst = {
1358c2ecf20Sopenharmony_ci	{ "audio", NULL, 0 },
1368c2ecf20Sopenharmony_ci	{ "ge2", NULL, 2 },
1378c2ecf20Sopenharmony_ci	{ "ge1", NULL, 3 },
1388c2ecf20Sopenharmony_ci	{ "ge0", NULL, 4 },
1398c2ecf20Sopenharmony_ci	{ "pex1", NULL, 5 },
1408c2ecf20Sopenharmony_ci	{ "pex2", NULL, 6 },
1418c2ecf20Sopenharmony_ci	{ "pex3", NULL, 7 },
1428c2ecf20Sopenharmony_ci	{ "pex0", NULL, 8 },
1438c2ecf20Sopenharmony_ci	{ "usb3h0", NULL, 9 },
1448c2ecf20Sopenharmony_ci	{ "usb3h1", NULL, 10 },
1458c2ecf20Sopenharmony_ci	{ "usb3d", NULL, 11 },
1468c2ecf20Sopenharmony_ci	{ "bm", NULL, 13 },
1478c2ecf20Sopenharmony_ci	{ "crypto0z", NULL, 14 },
1488c2ecf20Sopenharmony_ci	{ "sata0", NULL, 15 },
1498c2ecf20Sopenharmony_ci	{ "crypto1z", NULL, 16 },
1508c2ecf20Sopenharmony_ci	{ "sdio", NULL, 17 },
1518c2ecf20Sopenharmony_ci	{ "usb2", NULL, 18 },
1528c2ecf20Sopenharmony_ci	{ "crypto1", NULL, 21 },
1538c2ecf20Sopenharmony_ci	{ "xor0", NULL, 22 },
1548c2ecf20Sopenharmony_ci	{ "crypto0", NULL, 23 },
1558c2ecf20Sopenharmony_ci	{ "tdm", NULL, 25 },
1568c2ecf20Sopenharmony_ci	{ "xor1", NULL, 28 },
1578c2ecf20Sopenharmony_ci	{ "sata1", NULL, 30 },
1588c2ecf20Sopenharmony_ci	{ }
1598c2ecf20Sopenharmony_ci};
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic void __init armada_38x_clk_gating_init(struct device_node *np)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	mvebu_clk_gating_setup(np, armada_38x_gating_desc);
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ciCLK_OF_DECLARE(armada_38x_clk_gating, "marvell,armada-380-gating-clock",
1668c2ecf20Sopenharmony_ci	       armada_38x_clk_gating_init);
167