18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Marvell Orion SoC clocks
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2014 Thomas Petazzoni
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/kernel.h>
128c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
138c2ecf20Sopenharmony_ci#include <linux/io.h>
148c2ecf20Sopenharmony_ci#include <linux/of.h>
158c2ecf20Sopenharmony_ci#include "common.h"
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cistatic const struct coreclk_ratio orion_coreclk_ratios[] __initconst = {
188c2ecf20Sopenharmony_ci	{ .id = 0, .name = "ddrclk", }
198c2ecf20Sopenharmony_ci};
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/*
228c2ecf20Sopenharmony_ci * Orion 5181
238c2ecf20Sopenharmony_ci */
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#define SAR_MV88F5181_TCLK_FREQ      8
268c2ecf20Sopenharmony_ci#define SAR_MV88F5181_TCLK_FREQ_MASK 0x3
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic u32 __init mv88f5181_get_tclk_freq(void __iomem *sar)
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	u32 opt = (readl(sar) >> SAR_MV88F5181_TCLK_FREQ) &
318c2ecf20Sopenharmony_ci		SAR_MV88F5181_TCLK_FREQ_MASK;
328c2ecf20Sopenharmony_ci	if (opt == 0)
338c2ecf20Sopenharmony_ci		return 133333333;
348c2ecf20Sopenharmony_ci	else if (opt == 1)
358c2ecf20Sopenharmony_ci		return 150000000;
368c2ecf20Sopenharmony_ci	else if (opt == 2)
378c2ecf20Sopenharmony_ci		return 166666667;
388c2ecf20Sopenharmony_ci	else
398c2ecf20Sopenharmony_ci		return 0;
408c2ecf20Sopenharmony_ci}
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define SAR_MV88F5181_CPU_FREQ       4
438c2ecf20Sopenharmony_ci#define SAR_MV88F5181_CPU_FREQ_MASK  0xf
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic u32 __init mv88f5181_get_cpu_freq(void __iomem *sar)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	u32 opt = (readl(sar) >> SAR_MV88F5181_CPU_FREQ) &
488c2ecf20Sopenharmony_ci		SAR_MV88F5181_CPU_FREQ_MASK;
498c2ecf20Sopenharmony_ci	if (opt == 0)
508c2ecf20Sopenharmony_ci		return 333333333;
518c2ecf20Sopenharmony_ci	else if (opt == 1 || opt == 2)
528c2ecf20Sopenharmony_ci		return 400000000;
538c2ecf20Sopenharmony_ci	else if (opt == 3)
548c2ecf20Sopenharmony_ci		return 500000000;
558c2ecf20Sopenharmony_ci	else
568c2ecf20Sopenharmony_ci		return 0;
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic void __init mv88f5181_get_clk_ratio(void __iomem *sar, int id,
608c2ecf20Sopenharmony_ci					   int *mult, int *div)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	u32 opt = (readl(sar) >> SAR_MV88F5181_CPU_FREQ) &
638c2ecf20Sopenharmony_ci		SAR_MV88F5181_CPU_FREQ_MASK;
648c2ecf20Sopenharmony_ci	if (opt == 0 || opt == 1) {
658c2ecf20Sopenharmony_ci		*mult = 1;
668c2ecf20Sopenharmony_ci		*div  = 2;
678c2ecf20Sopenharmony_ci	} else if (opt == 2 || opt == 3) {
688c2ecf20Sopenharmony_ci		*mult = 1;
698c2ecf20Sopenharmony_ci		*div  = 3;
708c2ecf20Sopenharmony_ci	} else {
718c2ecf20Sopenharmony_ci		*mult = 0;
728c2ecf20Sopenharmony_ci		*div  = 1;
738c2ecf20Sopenharmony_ci	}
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic const struct coreclk_soc_desc mv88f5181_coreclks = {
778c2ecf20Sopenharmony_ci	.get_tclk_freq = mv88f5181_get_tclk_freq,
788c2ecf20Sopenharmony_ci	.get_cpu_freq = mv88f5181_get_cpu_freq,
798c2ecf20Sopenharmony_ci	.get_clk_ratio = mv88f5181_get_clk_ratio,
808c2ecf20Sopenharmony_ci	.ratios = orion_coreclk_ratios,
818c2ecf20Sopenharmony_ci	.num_ratios = ARRAY_SIZE(orion_coreclk_ratios),
828c2ecf20Sopenharmony_ci};
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistatic void __init mv88f5181_clk_init(struct device_node *np)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	return mvebu_coreclk_setup(np, &mv88f5181_coreclks);
878c2ecf20Sopenharmony_ci}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ciCLK_OF_DECLARE(mv88f5181_clk, "marvell,mv88f5181-core-clock", mv88f5181_clk_init);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci/*
928c2ecf20Sopenharmony_ci * Orion 5182
938c2ecf20Sopenharmony_ci */
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci#define SAR_MV88F5182_TCLK_FREQ      8
968c2ecf20Sopenharmony_ci#define SAR_MV88F5182_TCLK_FREQ_MASK 0x3
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic u32 __init mv88f5182_get_tclk_freq(void __iomem *sar)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	u32 opt = (readl(sar) >> SAR_MV88F5182_TCLK_FREQ) &
1018c2ecf20Sopenharmony_ci		SAR_MV88F5182_TCLK_FREQ_MASK;
1028c2ecf20Sopenharmony_ci	if (opt == 1)
1038c2ecf20Sopenharmony_ci		return 150000000;
1048c2ecf20Sopenharmony_ci	else if (opt == 2)
1058c2ecf20Sopenharmony_ci		return 166666667;
1068c2ecf20Sopenharmony_ci	else
1078c2ecf20Sopenharmony_ci		return 0;
1088c2ecf20Sopenharmony_ci}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci#define SAR_MV88F5182_CPU_FREQ       4
1118c2ecf20Sopenharmony_ci#define SAR_MV88F5182_CPU_FREQ_MASK  0xf
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic u32 __init mv88f5182_get_cpu_freq(void __iomem *sar)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	u32 opt = (readl(sar) >> SAR_MV88F5182_CPU_FREQ) &
1168c2ecf20Sopenharmony_ci		SAR_MV88F5182_CPU_FREQ_MASK;
1178c2ecf20Sopenharmony_ci	if (opt == 0)
1188c2ecf20Sopenharmony_ci		return 333333333;
1198c2ecf20Sopenharmony_ci	else if (opt == 1 || opt == 2)
1208c2ecf20Sopenharmony_ci		return 400000000;
1218c2ecf20Sopenharmony_ci	else if (opt == 3)
1228c2ecf20Sopenharmony_ci		return 500000000;
1238c2ecf20Sopenharmony_ci	else
1248c2ecf20Sopenharmony_ci		return 0;
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_cistatic void __init mv88f5182_get_clk_ratio(void __iomem *sar, int id,
1288c2ecf20Sopenharmony_ci					   int *mult, int *div)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	u32 opt = (readl(sar) >> SAR_MV88F5182_CPU_FREQ) &
1318c2ecf20Sopenharmony_ci		SAR_MV88F5182_CPU_FREQ_MASK;
1328c2ecf20Sopenharmony_ci	if (opt == 0 || opt == 1) {
1338c2ecf20Sopenharmony_ci		*mult = 1;
1348c2ecf20Sopenharmony_ci		*div  = 2;
1358c2ecf20Sopenharmony_ci	} else if (opt == 2 || opt == 3) {
1368c2ecf20Sopenharmony_ci		*mult = 1;
1378c2ecf20Sopenharmony_ci		*div  = 3;
1388c2ecf20Sopenharmony_ci	} else {
1398c2ecf20Sopenharmony_ci		*mult = 0;
1408c2ecf20Sopenharmony_ci		*div  = 1;
1418c2ecf20Sopenharmony_ci	}
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistatic const struct coreclk_soc_desc mv88f5182_coreclks = {
1458c2ecf20Sopenharmony_ci	.get_tclk_freq = mv88f5182_get_tclk_freq,
1468c2ecf20Sopenharmony_ci	.get_cpu_freq = mv88f5182_get_cpu_freq,
1478c2ecf20Sopenharmony_ci	.get_clk_ratio = mv88f5182_get_clk_ratio,
1488c2ecf20Sopenharmony_ci	.ratios = orion_coreclk_ratios,
1498c2ecf20Sopenharmony_ci	.num_ratios = ARRAY_SIZE(orion_coreclk_ratios),
1508c2ecf20Sopenharmony_ci};
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic void __init mv88f5182_clk_init(struct device_node *np)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	return mvebu_coreclk_setup(np, &mv88f5182_coreclks);
1558c2ecf20Sopenharmony_ci}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ciCLK_OF_DECLARE(mv88f5182_clk, "marvell,mv88f5182-core-clock", mv88f5182_clk_init);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci/*
1608c2ecf20Sopenharmony_ci * Orion 5281
1618c2ecf20Sopenharmony_ci */
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_cistatic u32 __init mv88f5281_get_tclk_freq(void __iomem *sar)
1648c2ecf20Sopenharmony_ci{
1658c2ecf20Sopenharmony_ci	/* On 5281, tclk is always 166 Mhz */
1668c2ecf20Sopenharmony_ci	return 166666667;
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci#define SAR_MV88F5281_CPU_FREQ       4
1708c2ecf20Sopenharmony_ci#define SAR_MV88F5281_CPU_FREQ_MASK  0xf
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic u32 __init mv88f5281_get_cpu_freq(void __iomem *sar)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	u32 opt = (readl(sar) >> SAR_MV88F5281_CPU_FREQ) &
1758c2ecf20Sopenharmony_ci		SAR_MV88F5281_CPU_FREQ_MASK;
1768c2ecf20Sopenharmony_ci	if (opt == 1 || opt == 2)
1778c2ecf20Sopenharmony_ci		return 400000000;
1788c2ecf20Sopenharmony_ci	else if (opt == 3)
1798c2ecf20Sopenharmony_ci		return 500000000;
1808c2ecf20Sopenharmony_ci	else
1818c2ecf20Sopenharmony_ci		return 0;
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic void __init mv88f5281_get_clk_ratio(void __iomem *sar, int id,
1858c2ecf20Sopenharmony_ci					   int *mult, int *div)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	u32 opt = (readl(sar) >> SAR_MV88F5281_CPU_FREQ) &
1888c2ecf20Sopenharmony_ci		SAR_MV88F5281_CPU_FREQ_MASK;
1898c2ecf20Sopenharmony_ci	if (opt == 1) {
1908c2ecf20Sopenharmony_ci		*mult = 1;
1918c2ecf20Sopenharmony_ci		*div = 2;
1928c2ecf20Sopenharmony_ci	} else if (opt == 2 || opt == 3) {
1938c2ecf20Sopenharmony_ci		*mult = 1;
1948c2ecf20Sopenharmony_ci		*div = 3;
1958c2ecf20Sopenharmony_ci	} else {
1968c2ecf20Sopenharmony_ci		*mult = 0;
1978c2ecf20Sopenharmony_ci		*div = 1;
1988c2ecf20Sopenharmony_ci	}
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistatic const struct coreclk_soc_desc mv88f5281_coreclks = {
2028c2ecf20Sopenharmony_ci	.get_tclk_freq = mv88f5281_get_tclk_freq,
2038c2ecf20Sopenharmony_ci	.get_cpu_freq = mv88f5281_get_cpu_freq,
2048c2ecf20Sopenharmony_ci	.get_clk_ratio = mv88f5281_get_clk_ratio,
2058c2ecf20Sopenharmony_ci	.ratios = orion_coreclk_ratios,
2068c2ecf20Sopenharmony_ci	.num_ratios = ARRAY_SIZE(orion_coreclk_ratios),
2078c2ecf20Sopenharmony_ci};
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_cistatic void __init mv88f5281_clk_init(struct device_node *np)
2108c2ecf20Sopenharmony_ci{
2118c2ecf20Sopenharmony_ci	return mvebu_coreclk_setup(np, &mv88f5281_coreclks);
2128c2ecf20Sopenharmony_ci}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ciCLK_OF_DECLARE(mv88f5281_clk, "marvell,mv88f5281-core-clock", mv88f5281_clk_init);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci/*
2178c2ecf20Sopenharmony_ci * Orion 6183
2188c2ecf20Sopenharmony_ci */
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci#define SAR_MV88F6183_TCLK_FREQ      9
2218c2ecf20Sopenharmony_ci#define SAR_MV88F6183_TCLK_FREQ_MASK 0x1
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_cistatic u32 __init mv88f6183_get_tclk_freq(void __iomem *sar)
2248c2ecf20Sopenharmony_ci{
2258c2ecf20Sopenharmony_ci	u32 opt = (readl(sar) >> SAR_MV88F6183_TCLK_FREQ) &
2268c2ecf20Sopenharmony_ci		SAR_MV88F6183_TCLK_FREQ_MASK;
2278c2ecf20Sopenharmony_ci	if (opt == 0)
2288c2ecf20Sopenharmony_ci		return 133333333;
2298c2ecf20Sopenharmony_ci	else if (opt == 1)
2308c2ecf20Sopenharmony_ci		return 166666667;
2318c2ecf20Sopenharmony_ci	else
2328c2ecf20Sopenharmony_ci		return 0;
2338c2ecf20Sopenharmony_ci}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci#define SAR_MV88F6183_CPU_FREQ       1
2368c2ecf20Sopenharmony_ci#define SAR_MV88F6183_CPU_FREQ_MASK  0x3f
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_cistatic u32 __init mv88f6183_get_cpu_freq(void __iomem *sar)
2398c2ecf20Sopenharmony_ci{
2408c2ecf20Sopenharmony_ci	u32 opt = (readl(sar) >> SAR_MV88F6183_CPU_FREQ) &
2418c2ecf20Sopenharmony_ci		SAR_MV88F6183_CPU_FREQ_MASK;
2428c2ecf20Sopenharmony_ci	if (opt == 9)
2438c2ecf20Sopenharmony_ci		return 333333333;
2448c2ecf20Sopenharmony_ci	else if (opt == 17)
2458c2ecf20Sopenharmony_ci		return 400000000;
2468c2ecf20Sopenharmony_ci	else
2478c2ecf20Sopenharmony_ci		return 0;
2488c2ecf20Sopenharmony_ci}
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_cistatic void __init mv88f6183_get_clk_ratio(void __iomem *sar, int id,
2518c2ecf20Sopenharmony_ci					   int *mult, int *div)
2528c2ecf20Sopenharmony_ci{
2538c2ecf20Sopenharmony_ci	u32 opt = (readl(sar) >> SAR_MV88F6183_CPU_FREQ) &
2548c2ecf20Sopenharmony_ci		SAR_MV88F6183_CPU_FREQ_MASK;
2558c2ecf20Sopenharmony_ci	if (opt == 9 || opt == 17) {
2568c2ecf20Sopenharmony_ci		*mult = 1;
2578c2ecf20Sopenharmony_ci		*div  = 2;
2588c2ecf20Sopenharmony_ci	} else {
2598c2ecf20Sopenharmony_ci		*mult = 0;
2608c2ecf20Sopenharmony_ci		*div  = 1;
2618c2ecf20Sopenharmony_ci	}
2628c2ecf20Sopenharmony_ci}
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_cistatic const struct coreclk_soc_desc mv88f6183_coreclks = {
2658c2ecf20Sopenharmony_ci	.get_tclk_freq = mv88f6183_get_tclk_freq,
2668c2ecf20Sopenharmony_ci	.get_cpu_freq = mv88f6183_get_cpu_freq,
2678c2ecf20Sopenharmony_ci	.get_clk_ratio = mv88f6183_get_clk_ratio,
2688c2ecf20Sopenharmony_ci	.ratios = orion_coreclk_ratios,
2698c2ecf20Sopenharmony_ci	.num_ratios = ARRAY_SIZE(orion_coreclk_ratios),
2708c2ecf20Sopenharmony_ci};
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_cistatic void __init mv88f6183_clk_init(struct device_node *np)
2748c2ecf20Sopenharmony_ci{
2758c2ecf20Sopenharmony_ci	return mvebu_coreclk_setup(np, &mv88f6183_coreclks);
2768c2ecf20Sopenharmony_ci}
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ciCLK_OF_DECLARE(mv88f6183_clk, "marvell,mv88f6183-core-clock", mv88f6183_clk_init);
279