18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Marvell Armada 39x SoC clocks
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2015 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 * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <linux/kernel.h>
158c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
168c2ecf20Sopenharmony_ci#include <linux/io.h>
178c2ecf20Sopenharmony_ci#include <linux/of.h>
188c2ecf20Sopenharmony_ci#include "common.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/*
218c2ecf20Sopenharmony_ci * SARL[14:10] : Ratios between CPU, NBCLK, HCLK and DCLK.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * SARL[15]    : TCLK frequency
248c2ecf20Sopenharmony_ci *		 0 = 250 MHz
258c2ecf20Sopenharmony_ci *		 1 = 200 MHz
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci * SARH[0]     : Reference clock frequency
288c2ecf20Sopenharmony_ci *               0 = 25 Mhz
298c2ecf20Sopenharmony_ci *               1 = 40 Mhz
308c2ecf20Sopenharmony_ci */
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#define SARL 					0
338c2ecf20Sopenharmony_ci#define  SARL_A390_TCLK_FREQ_OPT		15
348c2ecf20Sopenharmony_ci#define  SARL_A390_TCLK_FREQ_OPT_MASK		0x1
358c2ecf20Sopenharmony_ci#define  SARL_A390_CPU_DDR_L2_FREQ_OPT		10
368c2ecf20Sopenharmony_ci#define  SARL_A390_CPU_DDR_L2_FREQ_OPT_MASK	0x1F
378c2ecf20Sopenharmony_ci#define SARH					4
388c2ecf20Sopenharmony_ci#define  SARH_A390_REFCLK_FREQ			BIT(0)
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic const u32 armada_39x_tclk_frequencies[] __initconst = {
418c2ecf20Sopenharmony_ci	250000000,
428c2ecf20Sopenharmony_ci	200000000,
438c2ecf20Sopenharmony_ci};
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic u32 __init armada_39x_get_tclk_freq(void __iomem *sar)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	u8 tclk_freq_select;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	tclk_freq_select = ((readl(sar + SARL) >> SARL_A390_TCLK_FREQ_OPT) &
508c2ecf20Sopenharmony_ci			    SARL_A390_TCLK_FREQ_OPT_MASK);
518c2ecf20Sopenharmony_ci	return armada_39x_tclk_frequencies[tclk_freq_select];
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic const u32 armada_39x_cpu_frequencies[] __initconst = {
558c2ecf20Sopenharmony_ci	[0x0] = 666 * 1000 * 1000,
568c2ecf20Sopenharmony_ci	[0x2] = 800 * 1000 * 1000,
578c2ecf20Sopenharmony_ci	[0x3] = 800 * 1000 * 1000,
588c2ecf20Sopenharmony_ci	[0x4] = 1066 * 1000 * 1000,
598c2ecf20Sopenharmony_ci	[0x5] = 1066 * 1000 * 1000,
608c2ecf20Sopenharmony_ci	[0x6] = 1200 * 1000 * 1000,
618c2ecf20Sopenharmony_ci	[0x8] = 1332 * 1000 * 1000,
628c2ecf20Sopenharmony_ci	[0xB] = 1600 * 1000 * 1000,
638c2ecf20Sopenharmony_ci	[0xC] = 1600 * 1000 * 1000,
648c2ecf20Sopenharmony_ci	[0x12] = 1800 * 1000 * 1000,
658c2ecf20Sopenharmony_ci	[0x1E] = 1800 * 1000 * 1000,
668c2ecf20Sopenharmony_ci};
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic u32 __init armada_39x_get_cpu_freq(void __iomem *sar)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	u8 cpu_freq_select;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	cpu_freq_select = ((readl(sar + SARL) >> SARL_A390_CPU_DDR_L2_FREQ_OPT) &
738c2ecf20Sopenharmony_ci			   SARL_A390_CPU_DDR_L2_FREQ_OPT_MASK);
748c2ecf20Sopenharmony_ci	if (cpu_freq_select >= ARRAY_SIZE(armada_39x_cpu_frequencies)) {
758c2ecf20Sopenharmony_ci		pr_err("Selected CPU frequency (%d) unsupported\n",
768c2ecf20Sopenharmony_ci			cpu_freq_select);
778c2ecf20Sopenharmony_ci		return 0;
788c2ecf20Sopenharmony_ci	}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	return armada_39x_cpu_frequencies[cpu_freq_select];
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cienum { A390_CPU_TO_NBCLK, A390_CPU_TO_HCLK, A390_CPU_TO_DCLK };
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic const struct coreclk_ratio armada_39x_coreclk_ratios[] __initconst = {
868c2ecf20Sopenharmony_ci	{ .id = A390_CPU_TO_NBCLK, .name = "nbclk" },
878c2ecf20Sopenharmony_ci	{ .id = A390_CPU_TO_HCLK, .name = "hclk" },
888c2ecf20Sopenharmony_ci	{ .id = A390_CPU_TO_DCLK, .name = "dclk" },
898c2ecf20Sopenharmony_ci};
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic void __init armada_39x_get_clk_ratio(
928c2ecf20Sopenharmony_ci	void __iomem *sar, int id, int *mult, int *div)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	switch (id) {
958c2ecf20Sopenharmony_ci	case A390_CPU_TO_NBCLK:
968c2ecf20Sopenharmony_ci		*mult = 1;
978c2ecf20Sopenharmony_ci		*div = 2;
988c2ecf20Sopenharmony_ci		break;
998c2ecf20Sopenharmony_ci	case A390_CPU_TO_HCLK:
1008c2ecf20Sopenharmony_ci		*mult = 1;
1018c2ecf20Sopenharmony_ci		*div = 4;
1028c2ecf20Sopenharmony_ci		break;
1038c2ecf20Sopenharmony_ci	case A390_CPU_TO_DCLK:
1048c2ecf20Sopenharmony_ci		*mult = 1;
1058c2ecf20Sopenharmony_ci		*div = 2;
1068c2ecf20Sopenharmony_ci		break;
1078c2ecf20Sopenharmony_ci	}
1088c2ecf20Sopenharmony_ci}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistatic u32 __init armada_39x_refclk_ratio(void __iomem *sar)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	if (readl(sar + SARH) & SARH_A390_REFCLK_FREQ)
1138c2ecf20Sopenharmony_ci		return 40 * 1000 * 1000;
1148c2ecf20Sopenharmony_ci	else
1158c2ecf20Sopenharmony_ci		return 25 * 1000 * 1000;
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic const struct coreclk_soc_desc armada_39x_coreclks = {
1198c2ecf20Sopenharmony_ci	.get_tclk_freq = armada_39x_get_tclk_freq,
1208c2ecf20Sopenharmony_ci	.get_cpu_freq = armada_39x_get_cpu_freq,
1218c2ecf20Sopenharmony_ci	.get_clk_ratio = armada_39x_get_clk_ratio,
1228c2ecf20Sopenharmony_ci	.get_refclk_freq = armada_39x_refclk_ratio,
1238c2ecf20Sopenharmony_ci	.ratios = armada_39x_coreclk_ratios,
1248c2ecf20Sopenharmony_ci	.num_ratios = ARRAY_SIZE(armada_39x_coreclk_ratios),
1258c2ecf20Sopenharmony_ci};
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_cistatic void __init armada_39x_coreclk_init(struct device_node *np)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	mvebu_coreclk_setup(np, &armada_39x_coreclks);
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ciCLK_OF_DECLARE(armada_39x_core_clk, "marvell,armada-390-core-clock",
1328c2ecf20Sopenharmony_ci	       armada_39x_coreclk_init);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci/*
1358c2ecf20Sopenharmony_ci * Clock Gating Control
1368c2ecf20Sopenharmony_ci */
1378c2ecf20Sopenharmony_cistatic const struct clk_gating_soc_desc armada_39x_gating_desc[] __initconst = {
1388c2ecf20Sopenharmony_ci	{ "pex1", NULL, 5 },
1398c2ecf20Sopenharmony_ci	{ "pex2", NULL, 6 },
1408c2ecf20Sopenharmony_ci	{ "pex3", NULL, 7 },
1418c2ecf20Sopenharmony_ci	{ "pex0", NULL, 8 },
1428c2ecf20Sopenharmony_ci	{ "usb3h0", NULL, 9 },
1438c2ecf20Sopenharmony_ci	{ "usb3h1", NULL, 10 },
1448c2ecf20Sopenharmony_ci	{ "sata0", NULL, 15 },
1458c2ecf20Sopenharmony_ci	{ "sdio", NULL, 17 },
1468c2ecf20Sopenharmony_ci	{ "xor0", NULL, 22 },
1478c2ecf20Sopenharmony_ci	{ "xor1", NULL, 28 },
1488c2ecf20Sopenharmony_ci	{ }
1498c2ecf20Sopenharmony_ci};
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cistatic void __init armada_39x_clk_gating_init(struct device_node *np)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	mvebu_clk_gating_setup(np, armada_39x_gating_desc);
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ciCLK_OF_DECLARE(armada_39x_clk_gating, "marvell,armada-390-gating-clock",
1568c2ecf20Sopenharmony_ci	       armada_39x_clk_gating_init);
157