18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Marvell Armada 37xx SoC Peripheral clocks
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2016 Marvell
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Gregory CLEMENT <gregory.clement@free-electrons.com>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Most of the peripheral clocks can be modelled like this:
108c2ecf20Sopenharmony_ci *             _____    _______    _______
118c2ecf20Sopenharmony_ci * TBG-A-P  --|     |  |       |  |       |   ______
128c2ecf20Sopenharmony_ci * TBG-B-P  --| Mux |--| /div1 |--| /div2 |--| Gate |--> perip_clk
138c2ecf20Sopenharmony_ci * TBG-A-S  --|     |  |       |  |       |  |______|
148c2ecf20Sopenharmony_ci * TBG-B-S  --|_____|  |_______|  |_______|
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * However some clocks may use only one or two block or and use the
178c2ecf20Sopenharmony_ci * xtal clock as parent.
188c2ecf20Sopenharmony_ci */
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
218c2ecf20Sopenharmony_ci#include <linux/io.h>
228c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h>
238c2ecf20Sopenharmony_ci#include <linux/of.h>
248c2ecf20Sopenharmony_ci#include <linux/of_device.h>
258c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
268c2ecf20Sopenharmony_ci#include <linux/regmap.h>
278c2ecf20Sopenharmony_ci#include <linux/slab.h>
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define TBG_SEL		0x0
308c2ecf20Sopenharmony_ci#define DIV_SEL0	0x4
318c2ecf20Sopenharmony_ci#define DIV_SEL1	0x8
328c2ecf20Sopenharmony_ci#define DIV_SEL2	0xC
338c2ecf20Sopenharmony_ci#define CLK_SEL		0x10
348c2ecf20Sopenharmony_ci#define CLK_DIS		0x14
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define  ARMADA_37XX_DVFS_LOAD_1 1
378c2ecf20Sopenharmony_ci#define LOAD_LEVEL_NR	4
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define ARMADA_37XX_NB_L0L1	0x18
408c2ecf20Sopenharmony_ci#define ARMADA_37XX_NB_L2L3	0x1C
418c2ecf20Sopenharmony_ci#define		ARMADA_37XX_NB_TBG_DIV_OFF	13
428c2ecf20Sopenharmony_ci#define		ARMADA_37XX_NB_TBG_DIV_MASK	0x7
438c2ecf20Sopenharmony_ci#define		ARMADA_37XX_NB_CLK_SEL_OFF	11
448c2ecf20Sopenharmony_ci#define		ARMADA_37XX_NB_CLK_SEL_MASK	0x1
458c2ecf20Sopenharmony_ci#define		ARMADA_37XX_NB_TBG_SEL_OFF	9
468c2ecf20Sopenharmony_ci#define		ARMADA_37XX_NB_TBG_SEL_MASK	0x3
478c2ecf20Sopenharmony_ci#define		ARMADA_37XX_NB_CONFIG_SHIFT	16
488c2ecf20Sopenharmony_ci#define ARMADA_37XX_NB_DYN_MOD	0x24
498c2ecf20Sopenharmony_ci#define		ARMADA_37XX_NB_DFS_EN	31
508c2ecf20Sopenharmony_ci#define ARMADA_37XX_NB_CPU_LOAD	0x30
518c2ecf20Sopenharmony_ci#define		ARMADA_37XX_NB_CPU_LOAD_MASK	0x3
528c2ecf20Sopenharmony_ci#define		ARMADA_37XX_DVFS_LOAD_0		0
538c2ecf20Sopenharmony_ci#define		ARMADA_37XX_DVFS_LOAD_1		1
548c2ecf20Sopenharmony_ci#define		ARMADA_37XX_DVFS_LOAD_2		2
558c2ecf20Sopenharmony_ci#define		ARMADA_37XX_DVFS_LOAD_3		3
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistruct clk_periph_driver_data {
588c2ecf20Sopenharmony_ci	struct clk_hw_onecell_data *hw_data;
598c2ecf20Sopenharmony_ci	spinlock_t lock;
608c2ecf20Sopenharmony_ci	void __iomem *reg;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	/* Storage registers for suspend/resume operations */
638c2ecf20Sopenharmony_ci	u32 tbg_sel;
648c2ecf20Sopenharmony_ci	u32 div_sel0;
658c2ecf20Sopenharmony_ci	u32 div_sel1;
668c2ecf20Sopenharmony_ci	u32 div_sel2;
678c2ecf20Sopenharmony_ci	u32 clk_sel;
688c2ecf20Sopenharmony_ci	u32 clk_dis;
698c2ecf20Sopenharmony_ci};
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistruct clk_double_div {
728c2ecf20Sopenharmony_ci	struct clk_hw hw;
738c2ecf20Sopenharmony_ci	void __iomem *reg1;
748c2ecf20Sopenharmony_ci	u8 shift1;
758c2ecf20Sopenharmony_ci	void __iomem *reg2;
768c2ecf20Sopenharmony_ci	u8 shift2;
778c2ecf20Sopenharmony_ci};
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistruct clk_pm_cpu {
808c2ecf20Sopenharmony_ci	struct clk_hw hw;
818c2ecf20Sopenharmony_ci	void __iomem *reg_mux;
828c2ecf20Sopenharmony_ci	u8 shift_mux;
838c2ecf20Sopenharmony_ci	u32 mask_mux;
848c2ecf20Sopenharmony_ci	void __iomem *reg_div;
858c2ecf20Sopenharmony_ci	u8 shift_div;
868c2ecf20Sopenharmony_ci	struct regmap *nb_pm_base;
878c2ecf20Sopenharmony_ci	unsigned long l1_expiration;
888c2ecf20Sopenharmony_ci};
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci#define to_clk_double_div(_hw) container_of(_hw, struct clk_double_div, hw)
918c2ecf20Sopenharmony_ci#define to_clk_pm_cpu(_hw) container_of(_hw, struct clk_pm_cpu, hw)
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistruct clk_periph_data {
948c2ecf20Sopenharmony_ci	const char *name;
958c2ecf20Sopenharmony_ci	const char * const *parent_names;
968c2ecf20Sopenharmony_ci	int num_parents;
978c2ecf20Sopenharmony_ci	struct clk_hw *mux_hw;
988c2ecf20Sopenharmony_ci	struct clk_hw *rate_hw;
998c2ecf20Sopenharmony_ci	struct clk_hw *gate_hw;
1008c2ecf20Sopenharmony_ci	struct clk_hw *muxrate_hw;
1018c2ecf20Sopenharmony_ci	bool is_double_div;
1028c2ecf20Sopenharmony_ci};
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic const struct clk_div_table clk_table6[] = {
1058c2ecf20Sopenharmony_ci	{ .val = 1, .div = 1, },
1068c2ecf20Sopenharmony_ci	{ .val = 2, .div = 2, },
1078c2ecf20Sopenharmony_ci	{ .val = 3, .div = 3, },
1088c2ecf20Sopenharmony_ci	{ .val = 4, .div = 4, },
1098c2ecf20Sopenharmony_ci	{ .val = 5, .div = 5, },
1108c2ecf20Sopenharmony_ci	{ .val = 6, .div = 6, },
1118c2ecf20Sopenharmony_ci	{ .val = 0, .div = 0, }, /* last entry */
1128c2ecf20Sopenharmony_ci};
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic const struct clk_div_table clk_table1[] = {
1158c2ecf20Sopenharmony_ci	{ .val = 0, .div = 1, },
1168c2ecf20Sopenharmony_ci	{ .val = 1, .div = 2, },
1178c2ecf20Sopenharmony_ci	{ .val = 0, .div = 0, }, /* last entry */
1188c2ecf20Sopenharmony_ci};
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic const struct clk_div_table clk_table2[] = {
1218c2ecf20Sopenharmony_ci	{ .val = 0, .div = 2, },
1228c2ecf20Sopenharmony_ci	{ .val = 1, .div = 4, },
1238c2ecf20Sopenharmony_ci	{ .val = 0, .div = 0, }, /* last entry */
1248c2ecf20Sopenharmony_ci};
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic const struct clk_ops clk_double_div_ops;
1278c2ecf20Sopenharmony_cistatic const struct clk_ops clk_pm_cpu_ops;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci#define PERIPH_GATE(_name, _bit)		\
1308c2ecf20Sopenharmony_cistruct clk_gate gate_##_name = {		\
1318c2ecf20Sopenharmony_ci	.reg = (void *)CLK_DIS,			\
1328c2ecf20Sopenharmony_ci	.bit_idx = _bit,			\
1338c2ecf20Sopenharmony_ci	.hw.init = &(struct clk_init_data){	\
1348c2ecf20Sopenharmony_ci		.ops =  &clk_gate_ops,		\
1358c2ecf20Sopenharmony_ci	}					\
1368c2ecf20Sopenharmony_ci};
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci#define PERIPH_MUX(_name, _shift)		\
1398c2ecf20Sopenharmony_cistruct clk_mux mux_##_name = {			\
1408c2ecf20Sopenharmony_ci	.reg = (void *)TBG_SEL,			\
1418c2ecf20Sopenharmony_ci	.shift = _shift,			\
1428c2ecf20Sopenharmony_ci	.mask = 3,				\
1438c2ecf20Sopenharmony_ci	.hw.init = &(struct clk_init_data){	\
1448c2ecf20Sopenharmony_ci		.ops =  &clk_mux_ro_ops,	\
1458c2ecf20Sopenharmony_ci	}					\
1468c2ecf20Sopenharmony_ci};
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci#define PERIPH_DOUBLEDIV(_name, _reg1, _reg2, _shift1, _shift2)	\
1498c2ecf20Sopenharmony_cistruct clk_double_div rate_##_name = {		\
1508c2ecf20Sopenharmony_ci	.reg1 = (void *)_reg1,			\
1518c2ecf20Sopenharmony_ci	.reg2 = (void *)_reg2,			\
1528c2ecf20Sopenharmony_ci	.shift1 = _shift1,			\
1538c2ecf20Sopenharmony_ci	.shift2 = _shift2,			\
1548c2ecf20Sopenharmony_ci	.hw.init = &(struct clk_init_data){	\
1558c2ecf20Sopenharmony_ci		.ops =  &clk_double_div_ops,	\
1568c2ecf20Sopenharmony_ci	}					\
1578c2ecf20Sopenharmony_ci};
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci#define PERIPH_DIV(_name, _reg, _shift, _table)	\
1608c2ecf20Sopenharmony_cistruct clk_divider rate_##_name = {		\
1618c2ecf20Sopenharmony_ci	.reg = (void *)_reg,			\
1628c2ecf20Sopenharmony_ci	.table = _table,			\
1638c2ecf20Sopenharmony_ci	.shift = _shift,			\
1648c2ecf20Sopenharmony_ci	.hw.init = &(struct clk_init_data){	\
1658c2ecf20Sopenharmony_ci		.ops =  &clk_divider_ro_ops,	\
1668c2ecf20Sopenharmony_ci	}					\
1678c2ecf20Sopenharmony_ci};
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci#define PERIPH_PM_CPU(_name, _shift1, _reg, _shift2)	\
1708c2ecf20Sopenharmony_cistruct clk_pm_cpu muxrate_##_name = {		\
1718c2ecf20Sopenharmony_ci	.reg_mux = (void *)TBG_SEL,		\
1728c2ecf20Sopenharmony_ci	.mask_mux = 3,				\
1738c2ecf20Sopenharmony_ci	.shift_mux = _shift1,			\
1748c2ecf20Sopenharmony_ci	.reg_div = (void *)_reg,		\
1758c2ecf20Sopenharmony_ci	.shift_div = _shift2,			\
1768c2ecf20Sopenharmony_ci	.hw.init = &(struct clk_init_data){	\
1778c2ecf20Sopenharmony_ci		.ops =  &clk_pm_cpu_ops,	\
1788c2ecf20Sopenharmony_ci	}					\
1798c2ecf20Sopenharmony_ci};
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci#define PERIPH_CLK_FULL_DD(_name, _bit, _shift, _reg1, _reg2, _shift1, _shift2)\
1828c2ecf20Sopenharmony_cistatic PERIPH_GATE(_name, _bit);			    \
1838c2ecf20Sopenharmony_cistatic PERIPH_MUX(_name, _shift);			    \
1848c2ecf20Sopenharmony_cistatic PERIPH_DOUBLEDIV(_name, _reg1, _reg2, _shift1, _shift2);
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci#define PERIPH_CLK_FULL(_name, _bit, _shift, _reg, _shift1, _table)	\
1878c2ecf20Sopenharmony_cistatic PERIPH_GATE(_name, _bit);			    \
1888c2ecf20Sopenharmony_cistatic PERIPH_MUX(_name, _shift);			    \
1898c2ecf20Sopenharmony_cistatic PERIPH_DIV(_name, _reg, _shift1, _table);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci#define PERIPH_CLK_GATE_DIV(_name, _bit,  _reg, _shift, _table)	\
1928c2ecf20Sopenharmony_cistatic PERIPH_GATE(_name, _bit);			\
1938c2ecf20Sopenharmony_cistatic PERIPH_DIV(_name, _reg, _shift, _table);
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci#define PERIPH_CLK_MUX_DD(_name, _shift, _reg1, _reg2, _shift1, _shift2)\
1968c2ecf20Sopenharmony_cistatic PERIPH_MUX(_name, _shift);			    \
1978c2ecf20Sopenharmony_cistatic PERIPH_DOUBLEDIV(_name, _reg1, _reg2, _shift1, _shift2);
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci#define REF_CLK_FULL(_name)				\
2008c2ecf20Sopenharmony_ci	{ .name = #_name,				\
2018c2ecf20Sopenharmony_ci	  .parent_names = (const char *[]){ "TBG-A-P",	\
2028c2ecf20Sopenharmony_ci	      "TBG-B-P", "TBG-A-S", "TBG-B-S"},		\
2038c2ecf20Sopenharmony_ci	  .num_parents = 4,				\
2048c2ecf20Sopenharmony_ci	  .mux_hw = &mux_##_name.hw,			\
2058c2ecf20Sopenharmony_ci	  .gate_hw = &gate_##_name.hw,			\
2068c2ecf20Sopenharmony_ci	  .rate_hw = &rate_##_name.hw,			\
2078c2ecf20Sopenharmony_ci	}
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci#define REF_CLK_FULL_DD(_name)				\
2108c2ecf20Sopenharmony_ci	{ .name = #_name,				\
2118c2ecf20Sopenharmony_ci	  .parent_names = (const char *[]){ "TBG-A-P",	\
2128c2ecf20Sopenharmony_ci	      "TBG-B-P", "TBG-A-S", "TBG-B-S"},		\
2138c2ecf20Sopenharmony_ci	  .num_parents = 4,				\
2148c2ecf20Sopenharmony_ci	  .mux_hw = &mux_##_name.hw,			\
2158c2ecf20Sopenharmony_ci	  .gate_hw = &gate_##_name.hw,			\
2168c2ecf20Sopenharmony_ci	  .rate_hw = &rate_##_name.hw,			\
2178c2ecf20Sopenharmony_ci	  .is_double_div = true,			\
2188c2ecf20Sopenharmony_ci	}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci#define REF_CLK_GATE(_name, _parent_name)			\
2218c2ecf20Sopenharmony_ci	{ .name = #_name,					\
2228c2ecf20Sopenharmony_ci	  .parent_names = (const char *[]){ _parent_name},	\
2238c2ecf20Sopenharmony_ci	  .num_parents = 1,					\
2248c2ecf20Sopenharmony_ci	  .gate_hw = &gate_##_name.hw,				\
2258c2ecf20Sopenharmony_ci	}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci#define REF_CLK_GATE_DIV(_name, _parent_name)			\
2288c2ecf20Sopenharmony_ci	{ .name = #_name,					\
2298c2ecf20Sopenharmony_ci	  .parent_names = (const char *[]){ _parent_name},	\
2308c2ecf20Sopenharmony_ci	  .num_parents = 1,					\
2318c2ecf20Sopenharmony_ci	  .gate_hw = &gate_##_name.hw,				\
2328c2ecf20Sopenharmony_ci	  .rate_hw = &rate_##_name.hw,				\
2338c2ecf20Sopenharmony_ci	}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci#define REF_CLK_PM_CPU(_name)				\
2368c2ecf20Sopenharmony_ci	{ .name = #_name,				\
2378c2ecf20Sopenharmony_ci	  .parent_names = (const char *[]){ "TBG-A-P",	\
2388c2ecf20Sopenharmony_ci	      "TBG-B-P", "TBG-A-S", "TBG-B-S"},		\
2398c2ecf20Sopenharmony_ci	  .num_parents = 4,				\
2408c2ecf20Sopenharmony_ci	  .muxrate_hw = &muxrate_##_name.hw,		\
2418c2ecf20Sopenharmony_ci	}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci#define REF_CLK_MUX_DD(_name)				\
2448c2ecf20Sopenharmony_ci	{ .name = #_name,				\
2458c2ecf20Sopenharmony_ci	  .parent_names = (const char *[]){ "TBG-A-P",	\
2468c2ecf20Sopenharmony_ci	      "TBG-B-P", "TBG-A-S", "TBG-B-S"},		\
2478c2ecf20Sopenharmony_ci	  .num_parents = 4,				\
2488c2ecf20Sopenharmony_ci	  .mux_hw = &mux_##_name.hw,			\
2498c2ecf20Sopenharmony_ci	  .rate_hw = &rate_##_name.hw,			\
2508c2ecf20Sopenharmony_ci	  .is_double_div = true,			\
2518c2ecf20Sopenharmony_ci	}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci/* NB periph clocks */
2548c2ecf20Sopenharmony_ciPERIPH_CLK_FULL_DD(mmc, 2, 0, DIV_SEL2, DIV_SEL2, 16, 13);
2558c2ecf20Sopenharmony_ciPERIPH_CLK_FULL_DD(sata_host, 3, 2, DIV_SEL2, DIV_SEL2, 10, 7);
2568c2ecf20Sopenharmony_ciPERIPH_CLK_FULL_DD(sec_at, 6, 4, DIV_SEL1, DIV_SEL1, 3, 0);
2578c2ecf20Sopenharmony_ciPERIPH_CLK_FULL_DD(sec_dap, 7, 6, DIV_SEL1, DIV_SEL1, 9, 6);
2588c2ecf20Sopenharmony_ciPERIPH_CLK_FULL_DD(tscem, 8, 8, DIV_SEL1, DIV_SEL1, 15, 12);
2598c2ecf20Sopenharmony_ciPERIPH_CLK_FULL(tscem_tmx, 10, 10, DIV_SEL1, 18, clk_table6);
2608c2ecf20Sopenharmony_cistatic PERIPH_GATE(avs, 11);
2618c2ecf20Sopenharmony_ciPERIPH_CLK_FULL_DD(pwm, 13, 14, DIV_SEL0, DIV_SEL0, 3, 0);
2628c2ecf20Sopenharmony_ciPERIPH_CLK_FULL_DD(sqf, 12, 12, DIV_SEL1, DIV_SEL1, 27, 24);
2638c2ecf20Sopenharmony_cistatic PERIPH_GATE(i2c_2, 16);
2648c2ecf20Sopenharmony_cistatic PERIPH_GATE(i2c_1, 17);
2658c2ecf20Sopenharmony_ciPERIPH_CLK_GATE_DIV(ddr_phy, 19, DIV_SEL0, 18, clk_table2);
2668c2ecf20Sopenharmony_ciPERIPH_CLK_FULL_DD(ddr_fclk, 21, 16, DIV_SEL0, DIV_SEL0, 15, 12);
2678c2ecf20Sopenharmony_ciPERIPH_CLK_FULL(trace, 22, 18, DIV_SEL0, 20, clk_table6);
2688c2ecf20Sopenharmony_ciPERIPH_CLK_FULL(counter, 23, 20, DIV_SEL0, 23, clk_table6);
2698c2ecf20Sopenharmony_ciPERIPH_CLK_FULL_DD(eip97, 24, 24, DIV_SEL2, DIV_SEL2, 22, 19);
2708c2ecf20Sopenharmony_cistatic PERIPH_PM_CPU(cpu, 22, DIV_SEL0, 28);
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_cistatic struct clk_periph_data data_nb[] = {
2738c2ecf20Sopenharmony_ci	REF_CLK_FULL_DD(mmc),
2748c2ecf20Sopenharmony_ci	REF_CLK_FULL_DD(sata_host),
2758c2ecf20Sopenharmony_ci	REF_CLK_FULL_DD(sec_at),
2768c2ecf20Sopenharmony_ci	REF_CLK_FULL_DD(sec_dap),
2778c2ecf20Sopenharmony_ci	REF_CLK_FULL_DD(tscem),
2788c2ecf20Sopenharmony_ci	REF_CLK_FULL(tscem_tmx),
2798c2ecf20Sopenharmony_ci	REF_CLK_GATE(avs, "xtal"),
2808c2ecf20Sopenharmony_ci	REF_CLK_FULL_DD(sqf),
2818c2ecf20Sopenharmony_ci	REF_CLK_FULL_DD(pwm),
2828c2ecf20Sopenharmony_ci	REF_CLK_GATE(i2c_2, "xtal"),
2838c2ecf20Sopenharmony_ci	REF_CLK_GATE(i2c_1, "xtal"),
2848c2ecf20Sopenharmony_ci	REF_CLK_GATE_DIV(ddr_phy, "TBG-A-S"),
2858c2ecf20Sopenharmony_ci	REF_CLK_FULL_DD(ddr_fclk),
2868c2ecf20Sopenharmony_ci	REF_CLK_FULL(trace),
2878c2ecf20Sopenharmony_ci	REF_CLK_FULL(counter),
2888c2ecf20Sopenharmony_ci	REF_CLK_FULL_DD(eip97),
2898c2ecf20Sopenharmony_ci	REF_CLK_PM_CPU(cpu),
2908c2ecf20Sopenharmony_ci	{ },
2918c2ecf20Sopenharmony_ci};
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci/* SB periph clocks */
2948c2ecf20Sopenharmony_ciPERIPH_CLK_MUX_DD(gbe_50, 6, DIV_SEL2, DIV_SEL2, 6, 9);
2958c2ecf20Sopenharmony_ciPERIPH_CLK_MUX_DD(gbe_core, 8, DIV_SEL1, DIV_SEL1, 18, 21);
2968c2ecf20Sopenharmony_ciPERIPH_CLK_MUX_DD(gbe_125, 10, DIV_SEL1, DIV_SEL1, 6, 9);
2978c2ecf20Sopenharmony_cistatic PERIPH_GATE(gbe1_50, 0);
2988c2ecf20Sopenharmony_cistatic PERIPH_GATE(gbe0_50, 1);
2998c2ecf20Sopenharmony_cistatic PERIPH_GATE(gbe1_125, 2);
3008c2ecf20Sopenharmony_cistatic PERIPH_GATE(gbe0_125, 3);
3018c2ecf20Sopenharmony_ciPERIPH_CLK_GATE_DIV(gbe1_core, 4, DIV_SEL1, 13, clk_table1);
3028c2ecf20Sopenharmony_ciPERIPH_CLK_GATE_DIV(gbe0_core, 5, DIV_SEL1, 14, clk_table1);
3038c2ecf20Sopenharmony_ciPERIPH_CLK_GATE_DIV(gbe_bm, 12, DIV_SEL1, 0, clk_table1);
3048c2ecf20Sopenharmony_ciPERIPH_CLK_FULL_DD(sdio, 11, 14, DIV_SEL0, DIV_SEL0, 3, 6);
3058c2ecf20Sopenharmony_ciPERIPH_CLK_FULL_DD(usb32_usb2_sys, 16, 16, DIV_SEL0, DIV_SEL0, 9, 12);
3068c2ecf20Sopenharmony_ciPERIPH_CLK_FULL_DD(usb32_ss_sys, 17, 18, DIV_SEL0, DIV_SEL0, 15, 18);
3078c2ecf20Sopenharmony_cistatic PERIPH_GATE(pcie, 14);
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_cistatic struct clk_periph_data data_sb[] = {
3108c2ecf20Sopenharmony_ci	REF_CLK_MUX_DD(gbe_50),
3118c2ecf20Sopenharmony_ci	REF_CLK_MUX_DD(gbe_core),
3128c2ecf20Sopenharmony_ci	REF_CLK_MUX_DD(gbe_125),
3138c2ecf20Sopenharmony_ci	REF_CLK_GATE(gbe1_50, "gbe_50"),
3148c2ecf20Sopenharmony_ci	REF_CLK_GATE(gbe0_50, "gbe_50"),
3158c2ecf20Sopenharmony_ci	REF_CLK_GATE(gbe1_125, "gbe_125"),
3168c2ecf20Sopenharmony_ci	REF_CLK_GATE(gbe0_125, "gbe_125"),
3178c2ecf20Sopenharmony_ci	REF_CLK_GATE_DIV(gbe1_core, "gbe_core"),
3188c2ecf20Sopenharmony_ci	REF_CLK_GATE_DIV(gbe0_core, "gbe_core"),
3198c2ecf20Sopenharmony_ci	REF_CLK_GATE_DIV(gbe_bm, "gbe_core"),
3208c2ecf20Sopenharmony_ci	REF_CLK_FULL_DD(sdio),
3218c2ecf20Sopenharmony_ci	REF_CLK_FULL_DD(usb32_usb2_sys),
3228c2ecf20Sopenharmony_ci	REF_CLK_FULL_DD(usb32_ss_sys),
3238c2ecf20Sopenharmony_ci	REF_CLK_GATE(pcie, "gbe_core"),
3248c2ecf20Sopenharmony_ci	{ },
3258c2ecf20Sopenharmony_ci};
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_cistatic unsigned int get_div(void __iomem *reg, int shift)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	u32 val;
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	val = (readl(reg) >> shift) & 0x7;
3328c2ecf20Sopenharmony_ci	if (val > 6)
3338c2ecf20Sopenharmony_ci		return 0;
3348c2ecf20Sopenharmony_ci	return val;
3358c2ecf20Sopenharmony_ci}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_cistatic unsigned long clk_double_div_recalc_rate(struct clk_hw *hw,
3388c2ecf20Sopenharmony_ci						unsigned long parent_rate)
3398c2ecf20Sopenharmony_ci{
3408c2ecf20Sopenharmony_ci	struct clk_double_div *double_div = to_clk_double_div(hw);
3418c2ecf20Sopenharmony_ci	unsigned int div;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	div = get_div(double_div->reg1, double_div->shift1);
3448c2ecf20Sopenharmony_ci	div *= get_div(double_div->reg2, double_div->shift2);
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	return DIV_ROUND_UP_ULL((u64)parent_rate, div);
3478c2ecf20Sopenharmony_ci}
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_cistatic const struct clk_ops clk_double_div_ops = {
3508c2ecf20Sopenharmony_ci	.recalc_rate = clk_double_div_recalc_rate,
3518c2ecf20Sopenharmony_ci};
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_cistatic void armada_3700_pm_dvfs_update_regs(unsigned int load_level,
3548c2ecf20Sopenharmony_ci					    unsigned int *reg,
3558c2ecf20Sopenharmony_ci					    unsigned int *offset)
3568c2ecf20Sopenharmony_ci{
3578c2ecf20Sopenharmony_ci	if (load_level <= ARMADA_37XX_DVFS_LOAD_1)
3588c2ecf20Sopenharmony_ci		*reg = ARMADA_37XX_NB_L0L1;
3598c2ecf20Sopenharmony_ci	else
3608c2ecf20Sopenharmony_ci		*reg = ARMADA_37XX_NB_L2L3;
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	if (load_level == ARMADA_37XX_DVFS_LOAD_0 ||
3638c2ecf20Sopenharmony_ci	    load_level ==  ARMADA_37XX_DVFS_LOAD_2)
3648c2ecf20Sopenharmony_ci		*offset += ARMADA_37XX_NB_CONFIG_SHIFT;
3658c2ecf20Sopenharmony_ci}
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_cistatic bool armada_3700_pm_dvfs_is_enabled(struct regmap *base)
3688c2ecf20Sopenharmony_ci{
3698c2ecf20Sopenharmony_ci	unsigned int val, reg = ARMADA_37XX_NB_DYN_MOD;
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	if (IS_ERR(base))
3728c2ecf20Sopenharmony_ci		return false;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	regmap_read(base, reg, &val);
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	return !!(val & BIT(ARMADA_37XX_NB_DFS_EN));
3778c2ecf20Sopenharmony_ci}
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_cistatic unsigned int armada_3700_pm_dvfs_get_cpu_div(struct regmap *base)
3808c2ecf20Sopenharmony_ci{
3818c2ecf20Sopenharmony_ci	unsigned int reg = ARMADA_37XX_NB_CPU_LOAD;
3828c2ecf20Sopenharmony_ci	unsigned int offset = ARMADA_37XX_NB_TBG_DIV_OFF;
3838c2ecf20Sopenharmony_ci	unsigned int load_level, div;
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	/*
3868c2ecf20Sopenharmony_ci	 * This function is always called after the function
3878c2ecf20Sopenharmony_ci	 * armada_3700_pm_dvfs_is_enabled, so no need to check again
3888c2ecf20Sopenharmony_ci	 * if the base is valid.
3898c2ecf20Sopenharmony_ci	 */
3908c2ecf20Sopenharmony_ci	regmap_read(base, reg, &load_level);
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	/*
3938c2ecf20Sopenharmony_ci	 * The register and the offset inside this register accessed to
3948c2ecf20Sopenharmony_ci	 * read the current divider depend on the load level
3958c2ecf20Sopenharmony_ci	 */
3968c2ecf20Sopenharmony_ci	load_level &= ARMADA_37XX_NB_CPU_LOAD_MASK;
3978c2ecf20Sopenharmony_ci	armada_3700_pm_dvfs_update_regs(load_level, &reg, &offset);
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	regmap_read(base, reg, &div);
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	return (div >> offset) & ARMADA_37XX_NB_TBG_DIV_MASK;
4028c2ecf20Sopenharmony_ci}
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_cistatic unsigned int armada_3700_pm_dvfs_get_cpu_parent(struct regmap *base)
4058c2ecf20Sopenharmony_ci{
4068c2ecf20Sopenharmony_ci	unsigned int reg = ARMADA_37XX_NB_CPU_LOAD;
4078c2ecf20Sopenharmony_ci	unsigned int offset = ARMADA_37XX_NB_TBG_SEL_OFF;
4088c2ecf20Sopenharmony_ci	unsigned int load_level, sel;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	/*
4118c2ecf20Sopenharmony_ci	 * This function is always called after the function
4128c2ecf20Sopenharmony_ci	 * armada_3700_pm_dvfs_is_enabled, so no need to check again
4138c2ecf20Sopenharmony_ci	 * if the base is valid
4148c2ecf20Sopenharmony_ci	 */
4158c2ecf20Sopenharmony_ci	regmap_read(base, reg, &load_level);
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	/*
4188c2ecf20Sopenharmony_ci	 * The register and the offset inside this register accessed to
4198c2ecf20Sopenharmony_ci	 * read the current divider depend on the load level
4208c2ecf20Sopenharmony_ci	 */
4218c2ecf20Sopenharmony_ci	load_level &= ARMADA_37XX_NB_CPU_LOAD_MASK;
4228c2ecf20Sopenharmony_ci	armada_3700_pm_dvfs_update_regs(load_level, &reg, &offset);
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	regmap_read(base, reg, &sel);
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	return (sel >> offset) & ARMADA_37XX_NB_TBG_SEL_MASK;
4278c2ecf20Sopenharmony_ci}
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_cistatic u8 clk_pm_cpu_get_parent(struct clk_hw *hw)
4308c2ecf20Sopenharmony_ci{
4318c2ecf20Sopenharmony_ci	struct clk_pm_cpu *pm_cpu = to_clk_pm_cpu(hw);
4328c2ecf20Sopenharmony_ci	u32 val;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	if (armada_3700_pm_dvfs_is_enabled(pm_cpu->nb_pm_base)) {
4358c2ecf20Sopenharmony_ci		val = armada_3700_pm_dvfs_get_cpu_parent(pm_cpu->nb_pm_base);
4368c2ecf20Sopenharmony_ci	} else {
4378c2ecf20Sopenharmony_ci		val = readl(pm_cpu->reg_mux) >> pm_cpu->shift_mux;
4388c2ecf20Sopenharmony_ci		val &= pm_cpu->mask_mux;
4398c2ecf20Sopenharmony_ci	}
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	return val;
4428c2ecf20Sopenharmony_ci}
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_cistatic unsigned long clk_pm_cpu_recalc_rate(struct clk_hw *hw,
4458c2ecf20Sopenharmony_ci					    unsigned long parent_rate)
4468c2ecf20Sopenharmony_ci{
4478c2ecf20Sopenharmony_ci	struct clk_pm_cpu *pm_cpu = to_clk_pm_cpu(hw);
4488c2ecf20Sopenharmony_ci	unsigned int div;
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	if (armada_3700_pm_dvfs_is_enabled(pm_cpu->nb_pm_base))
4518c2ecf20Sopenharmony_ci		div = armada_3700_pm_dvfs_get_cpu_div(pm_cpu->nb_pm_base);
4528c2ecf20Sopenharmony_ci	else
4538c2ecf20Sopenharmony_ci		div = get_div(pm_cpu->reg_div, pm_cpu->shift_div);
4548c2ecf20Sopenharmony_ci	return DIV_ROUND_UP_ULL((u64)parent_rate, div);
4558c2ecf20Sopenharmony_ci}
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_cistatic long clk_pm_cpu_round_rate(struct clk_hw *hw, unsigned long rate,
4588c2ecf20Sopenharmony_ci				  unsigned long *parent_rate)
4598c2ecf20Sopenharmony_ci{
4608c2ecf20Sopenharmony_ci	struct clk_pm_cpu *pm_cpu = to_clk_pm_cpu(hw);
4618c2ecf20Sopenharmony_ci	struct regmap *base = pm_cpu->nb_pm_base;
4628c2ecf20Sopenharmony_ci	unsigned int div = *parent_rate / rate;
4638c2ecf20Sopenharmony_ci	unsigned int load_level;
4648c2ecf20Sopenharmony_ci	/* only available when DVFS is enabled */
4658c2ecf20Sopenharmony_ci	if (!armada_3700_pm_dvfs_is_enabled(base))
4668c2ecf20Sopenharmony_ci		return -EINVAL;
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	for (load_level = 0; load_level < LOAD_LEVEL_NR; load_level++) {
4698c2ecf20Sopenharmony_ci		unsigned int reg, val, offset = ARMADA_37XX_NB_TBG_DIV_OFF;
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci		armada_3700_pm_dvfs_update_regs(load_level, &reg, &offset);
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci		regmap_read(base, reg, &val);
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci		val >>= offset;
4768c2ecf20Sopenharmony_ci		val &= ARMADA_37XX_NB_TBG_DIV_MASK;
4778c2ecf20Sopenharmony_ci		if (val == div)
4788c2ecf20Sopenharmony_ci			/*
4798c2ecf20Sopenharmony_ci			 * We found a load level matching the target
4808c2ecf20Sopenharmony_ci			 * divider, switch to this load level and
4818c2ecf20Sopenharmony_ci			 * return.
4828c2ecf20Sopenharmony_ci			 */
4838c2ecf20Sopenharmony_ci			return *parent_rate / div;
4848c2ecf20Sopenharmony_ci	}
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	/* We didn't find any valid divider */
4878c2ecf20Sopenharmony_ci	return -EINVAL;
4888c2ecf20Sopenharmony_ci}
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci/*
4918c2ecf20Sopenharmony_ci * Workaround when base CPU frequnecy is 1000 or 1200 MHz
4928c2ecf20Sopenharmony_ci *
4938c2ecf20Sopenharmony_ci * Switching the CPU from the L2 or L3 frequencies (250/300 or 200 MHz
4948c2ecf20Sopenharmony_ci * respectively) to L0 frequency (1/1.2 GHz) requires a significant
4958c2ecf20Sopenharmony_ci * amount of time to let VDD stabilize to the appropriate
4968c2ecf20Sopenharmony_ci * voltage. This amount of time is large enough that it cannot be
4978c2ecf20Sopenharmony_ci * covered by the hardware countdown register. Due to this, the CPU
4988c2ecf20Sopenharmony_ci * might start operating at L0 before the voltage is stabilized,
4998c2ecf20Sopenharmony_ci * leading to CPU stalls.
5008c2ecf20Sopenharmony_ci *
5018c2ecf20Sopenharmony_ci * To work around this problem, we prevent switching directly from the
5028c2ecf20Sopenharmony_ci * L2/L3 frequencies to the L0 frequency, and instead switch to the L1
5038c2ecf20Sopenharmony_ci * frequency in-between. The sequence therefore becomes:
5048c2ecf20Sopenharmony_ci * 1. First switch from L2/L3 (200/250/300 MHz) to L1 (500/600 MHz)
5058c2ecf20Sopenharmony_ci * 2. Sleep 20ms for stabling VDD voltage
5068c2ecf20Sopenharmony_ci * 3. Then switch from L1 (500/600 MHz) to L0 (1000/1200 MHz).
5078c2ecf20Sopenharmony_ci */
5088c2ecf20Sopenharmony_cistatic void clk_pm_cpu_set_rate_wa(struct clk_pm_cpu *pm_cpu,
5098c2ecf20Sopenharmony_ci				   unsigned int new_level, unsigned long rate,
5108c2ecf20Sopenharmony_ci				   struct regmap *base)
5118c2ecf20Sopenharmony_ci{
5128c2ecf20Sopenharmony_ci	unsigned int cur_level;
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	regmap_read(base, ARMADA_37XX_NB_CPU_LOAD, &cur_level);
5158c2ecf20Sopenharmony_ci	cur_level &= ARMADA_37XX_NB_CPU_LOAD_MASK;
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	if (cur_level == new_level)
5188c2ecf20Sopenharmony_ci		return;
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	/*
5218c2ecf20Sopenharmony_ci	 * System wants to go to L1 on its own. If we are going from L2/L3,
5228c2ecf20Sopenharmony_ci	 * remember when 20ms will expire. If from L0, set the value so that
5238c2ecf20Sopenharmony_ci	 * next switch to L0 won't have to wait.
5248c2ecf20Sopenharmony_ci	 */
5258c2ecf20Sopenharmony_ci	if (new_level == ARMADA_37XX_DVFS_LOAD_1) {
5268c2ecf20Sopenharmony_ci		if (cur_level == ARMADA_37XX_DVFS_LOAD_0)
5278c2ecf20Sopenharmony_ci			pm_cpu->l1_expiration = jiffies;
5288c2ecf20Sopenharmony_ci		else
5298c2ecf20Sopenharmony_ci			pm_cpu->l1_expiration = jiffies + msecs_to_jiffies(20);
5308c2ecf20Sopenharmony_ci		return;
5318c2ecf20Sopenharmony_ci	}
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	/*
5348c2ecf20Sopenharmony_ci	 * If we are setting to L2/L3, just invalidate L1 expiration time,
5358c2ecf20Sopenharmony_ci	 * sleeping is not needed.
5368c2ecf20Sopenharmony_ci	 */
5378c2ecf20Sopenharmony_ci	if (rate < 1000*1000*1000)
5388c2ecf20Sopenharmony_ci		goto invalidate_l1_exp;
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	/*
5418c2ecf20Sopenharmony_ci	 * We are going to L0 with rate >= 1GHz. Check whether we have been at
5428c2ecf20Sopenharmony_ci	 * L1 for long enough time. If not, go to L1 for 20ms.
5438c2ecf20Sopenharmony_ci	 */
5448c2ecf20Sopenharmony_ci	if (pm_cpu->l1_expiration && jiffies >= pm_cpu->l1_expiration)
5458c2ecf20Sopenharmony_ci		goto invalidate_l1_exp;
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	regmap_update_bits(base, ARMADA_37XX_NB_CPU_LOAD,
5488c2ecf20Sopenharmony_ci			   ARMADA_37XX_NB_CPU_LOAD_MASK,
5498c2ecf20Sopenharmony_ci			   ARMADA_37XX_DVFS_LOAD_1);
5508c2ecf20Sopenharmony_ci	msleep(20);
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ciinvalidate_l1_exp:
5538c2ecf20Sopenharmony_ci	pm_cpu->l1_expiration = 0;
5548c2ecf20Sopenharmony_ci}
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_cistatic int clk_pm_cpu_set_rate(struct clk_hw *hw, unsigned long rate,
5578c2ecf20Sopenharmony_ci			       unsigned long parent_rate)
5588c2ecf20Sopenharmony_ci{
5598c2ecf20Sopenharmony_ci	struct clk_pm_cpu *pm_cpu = to_clk_pm_cpu(hw);
5608c2ecf20Sopenharmony_ci	struct regmap *base = pm_cpu->nb_pm_base;
5618c2ecf20Sopenharmony_ci	unsigned int div = parent_rate / rate;
5628c2ecf20Sopenharmony_ci	unsigned int load_level;
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	/* only available when DVFS is enabled */
5658c2ecf20Sopenharmony_ci	if (!armada_3700_pm_dvfs_is_enabled(base))
5668c2ecf20Sopenharmony_ci		return -EINVAL;
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	for (load_level = 0; load_level < LOAD_LEVEL_NR; load_level++) {
5698c2ecf20Sopenharmony_ci		unsigned int reg, mask, val,
5708c2ecf20Sopenharmony_ci			offset = ARMADA_37XX_NB_TBG_DIV_OFF;
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci		armada_3700_pm_dvfs_update_regs(load_level, &reg, &offset);
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci		regmap_read(base, reg, &val);
5758c2ecf20Sopenharmony_ci		val >>= offset;
5768c2ecf20Sopenharmony_ci		val &= ARMADA_37XX_NB_TBG_DIV_MASK;
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci		if (val == div) {
5798c2ecf20Sopenharmony_ci			/*
5808c2ecf20Sopenharmony_ci			 * We found a load level matching the target
5818c2ecf20Sopenharmony_ci			 * divider, switch to this load level and
5828c2ecf20Sopenharmony_ci			 * return.
5838c2ecf20Sopenharmony_ci			 */
5848c2ecf20Sopenharmony_ci			reg = ARMADA_37XX_NB_CPU_LOAD;
5858c2ecf20Sopenharmony_ci			mask = ARMADA_37XX_NB_CPU_LOAD_MASK;
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci			/* Apply workaround when base CPU frequency is 1000 or 1200 MHz */
5888c2ecf20Sopenharmony_ci			if (parent_rate >= 1000*1000*1000)
5898c2ecf20Sopenharmony_ci				clk_pm_cpu_set_rate_wa(pm_cpu, load_level, rate, base);
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci			regmap_update_bits(base, reg, mask, load_level);
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci			return rate;
5948c2ecf20Sopenharmony_ci		}
5958c2ecf20Sopenharmony_ci	}
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	/* We didn't find any valid divider */
5988c2ecf20Sopenharmony_ci	return -EINVAL;
5998c2ecf20Sopenharmony_ci}
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_cistatic const struct clk_ops clk_pm_cpu_ops = {
6028c2ecf20Sopenharmony_ci	.get_parent = clk_pm_cpu_get_parent,
6038c2ecf20Sopenharmony_ci	.round_rate = clk_pm_cpu_round_rate,
6048c2ecf20Sopenharmony_ci	.set_rate = clk_pm_cpu_set_rate,
6058c2ecf20Sopenharmony_ci	.recalc_rate = clk_pm_cpu_recalc_rate,
6068c2ecf20Sopenharmony_ci};
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_cistatic const struct of_device_id armada_3700_periph_clock_of_match[] = {
6098c2ecf20Sopenharmony_ci	{ .compatible = "marvell,armada-3700-periph-clock-nb",
6108c2ecf20Sopenharmony_ci	  .data = data_nb, },
6118c2ecf20Sopenharmony_ci	{ .compatible = "marvell,armada-3700-periph-clock-sb",
6128c2ecf20Sopenharmony_ci	.data = data_sb, },
6138c2ecf20Sopenharmony_ci	{ }
6148c2ecf20Sopenharmony_ci};
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_cistatic int armada_3700_add_composite_clk(const struct clk_periph_data *data,
6178c2ecf20Sopenharmony_ci					 void __iomem *reg, spinlock_t *lock,
6188c2ecf20Sopenharmony_ci					 struct device *dev, struct clk_hw **hw)
6198c2ecf20Sopenharmony_ci{
6208c2ecf20Sopenharmony_ci	const struct clk_ops *mux_ops = NULL, *gate_ops = NULL,
6218c2ecf20Sopenharmony_ci		*rate_ops = NULL;
6228c2ecf20Sopenharmony_ci	struct clk_hw *mux_hw = NULL, *gate_hw = NULL, *rate_hw = NULL;
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci	if (data->mux_hw) {
6258c2ecf20Sopenharmony_ci		struct clk_mux *mux;
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci		mux_hw = data->mux_hw;
6288c2ecf20Sopenharmony_ci		mux = to_clk_mux(mux_hw);
6298c2ecf20Sopenharmony_ci		mux->lock = lock;
6308c2ecf20Sopenharmony_ci		mux_ops = mux_hw->init->ops;
6318c2ecf20Sopenharmony_ci		mux->reg = reg + (u64)mux->reg;
6328c2ecf20Sopenharmony_ci	}
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	if (data->gate_hw) {
6358c2ecf20Sopenharmony_ci		struct clk_gate *gate;
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci		gate_hw = data->gate_hw;
6388c2ecf20Sopenharmony_ci		gate = to_clk_gate(gate_hw);
6398c2ecf20Sopenharmony_ci		gate->lock = lock;
6408c2ecf20Sopenharmony_ci		gate_ops = gate_hw->init->ops;
6418c2ecf20Sopenharmony_ci		gate->reg = reg + (u64)gate->reg;
6428c2ecf20Sopenharmony_ci		gate->flags = CLK_GATE_SET_TO_DISABLE;
6438c2ecf20Sopenharmony_ci	}
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	if (data->rate_hw) {
6468c2ecf20Sopenharmony_ci		rate_hw = data->rate_hw;
6478c2ecf20Sopenharmony_ci		rate_ops = rate_hw->init->ops;
6488c2ecf20Sopenharmony_ci		if (data->is_double_div) {
6498c2ecf20Sopenharmony_ci			struct clk_double_div *rate;
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci			rate =  to_clk_double_div(rate_hw);
6528c2ecf20Sopenharmony_ci			rate->reg1 = reg + (u64)rate->reg1;
6538c2ecf20Sopenharmony_ci			rate->reg2 = reg + (u64)rate->reg2;
6548c2ecf20Sopenharmony_ci		} else {
6558c2ecf20Sopenharmony_ci			struct clk_divider *rate = to_clk_divider(rate_hw);
6568c2ecf20Sopenharmony_ci			const struct clk_div_table *clkt;
6578c2ecf20Sopenharmony_ci			int table_size = 0;
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci			rate->reg = reg + (u64)rate->reg;
6608c2ecf20Sopenharmony_ci			for (clkt = rate->table; clkt->div; clkt++)
6618c2ecf20Sopenharmony_ci				table_size++;
6628c2ecf20Sopenharmony_ci			rate->width = order_base_2(table_size);
6638c2ecf20Sopenharmony_ci			rate->lock = lock;
6648c2ecf20Sopenharmony_ci		}
6658c2ecf20Sopenharmony_ci	}
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci	if (data->muxrate_hw) {
6688c2ecf20Sopenharmony_ci		struct clk_pm_cpu *pmcpu_clk;
6698c2ecf20Sopenharmony_ci		struct clk_hw *muxrate_hw = data->muxrate_hw;
6708c2ecf20Sopenharmony_ci		struct regmap *map;
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci		pmcpu_clk =  to_clk_pm_cpu(muxrate_hw);
6738c2ecf20Sopenharmony_ci		pmcpu_clk->reg_mux = reg + (u64)pmcpu_clk->reg_mux;
6748c2ecf20Sopenharmony_ci		pmcpu_clk->reg_div = reg + (u64)pmcpu_clk->reg_div;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci		mux_hw = muxrate_hw;
6778c2ecf20Sopenharmony_ci		rate_hw = muxrate_hw;
6788c2ecf20Sopenharmony_ci		mux_ops = muxrate_hw->init->ops;
6798c2ecf20Sopenharmony_ci		rate_ops = muxrate_hw->init->ops;
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci		map = syscon_regmap_lookup_by_compatible(
6828c2ecf20Sopenharmony_ci				"marvell,armada-3700-nb-pm");
6838c2ecf20Sopenharmony_ci		pmcpu_clk->nb_pm_base = map;
6848c2ecf20Sopenharmony_ci	}
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	*hw = clk_hw_register_composite(dev, data->name, data->parent_names,
6878c2ecf20Sopenharmony_ci					data->num_parents, mux_hw,
6888c2ecf20Sopenharmony_ci					mux_ops, rate_hw, rate_ops,
6898c2ecf20Sopenharmony_ci					gate_hw, gate_ops, CLK_IGNORE_UNUSED);
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(*hw);
6928c2ecf20Sopenharmony_ci}
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_cistatic int __maybe_unused armada_3700_periph_clock_suspend(struct device *dev)
6958c2ecf20Sopenharmony_ci{
6968c2ecf20Sopenharmony_ci	struct clk_periph_driver_data *data = dev_get_drvdata(dev);
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci	data->tbg_sel = readl(data->reg + TBG_SEL);
6998c2ecf20Sopenharmony_ci	data->div_sel0 = readl(data->reg + DIV_SEL0);
7008c2ecf20Sopenharmony_ci	data->div_sel1 = readl(data->reg + DIV_SEL1);
7018c2ecf20Sopenharmony_ci	data->div_sel2 = readl(data->reg + DIV_SEL2);
7028c2ecf20Sopenharmony_ci	data->clk_sel = readl(data->reg + CLK_SEL);
7038c2ecf20Sopenharmony_ci	data->clk_dis = readl(data->reg + CLK_DIS);
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	return 0;
7068c2ecf20Sopenharmony_ci}
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_cistatic int __maybe_unused armada_3700_periph_clock_resume(struct device *dev)
7098c2ecf20Sopenharmony_ci{
7108c2ecf20Sopenharmony_ci	struct clk_periph_driver_data *data = dev_get_drvdata(dev);
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	/* Follow the same order than what the Cortex-M3 does (ATF code) */
7138c2ecf20Sopenharmony_ci	writel(data->clk_dis, data->reg + CLK_DIS);
7148c2ecf20Sopenharmony_ci	writel(data->div_sel0, data->reg + DIV_SEL0);
7158c2ecf20Sopenharmony_ci	writel(data->div_sel1, data->reg + DIV_SEL1);
7168c2ecf20Sopenharmony_ci	writel(data->div_sel2, data->reg + DIV_SEL2);
7178c2ecf20Sopenharmony_ci	writel(data->tbg_sel, data->reg + TBG_SEL);
7188c2ecf20Sopenharmony_ci	writel(data->clk_sel, data->reg + CLK_SEL);
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ci	return 0;
7218c2ecf20Sopenharmony_ci}
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_cistatic const struct dev_pm_ops armada_3700_periph_clock_pm_ops = {
7248c2ecf20Sopenharmony_ci	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(armada_3700_periph_clock_suspend,
7258c2ecf20Sopenharmony_ci				      armada_3700_periph_clock_resume)
7268c2ecf20Sopenharmony_ci};
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_cistatic int armada_3700_periph_clock_probe(struct platform_device *pdev)
7298c2ecf20Sopenharmony_ci{
7308c2ecf20Sopenharmony_ci	struct clk_periph_driver_data *driver_data;
7318c2ecf20Sopenharmony_ci	struct device_node *np = pdev->dev.of_node;
7328c2ecf20Sopenharmony_ci	const struct clk_periph_data *data;
7338c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
7348c2ecf20Sopenharmony_ci	int num_periph = 0, i, ret;
7358c2ecf20Sopenharmony_ci	struct resource *res;
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	data = of_device_get_match_data(dev);
7388c2ecf20Sopenharmony_ci	if (!data)
7398c2ecf20Sopenharmony_ci		return -ENODEV;
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_ci	while (data[num_periph].name)
7428c2ecf20Sopenharmony_ci		num_periph++;
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	driver_data = devm_kzalloc(dev, sizeof(*driver_data), GFP_KERNEL);
7458c2ecf20Sopenharmony_ci	if (!driver_data)
7468c2ecf20Sopenharmony_ci		return -ENOMEM;
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci	driver_data->hw_data = devm_kzalloc(dev,
7498c2ecf20Sopenharmony_ci					    struct_size(driver_data->hw_data,
7508c2ecf20Sopenharmony_ci							hws, num_periph),
7518c2ecf20Sopenharmony_ci					    GFP_KERNEL);
7528c2ecf20Sopenharmony_ci	if (!driver_data->hw_data)
7538c2ecf20Sopenharmony_ci		return -ENOMEM;
7548c2ecf20Sopenharmony_ci	driver_data->hw_data->num = num_periph;
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
7578c2ecf20Sopenharmony_ci	driver_data->reg = devm_ioremap_resource(dev, res);
7588c2ecf20Sopenharmony_ci	if (IS_ERR(driver_data->reg))
7598c2ecf20Sopenharmony_ci		return PTR_ERR(driver_data->reg);
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	spin_lock_init(&driver_data->lock);
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci	for (i = 0; i < num_periph; i++) {
7648c2ecf20Sopenharmony_ci		struct clk_hw **hw = &driver_data->hw_data->hws[i];
7658c2ecf20Sopenharmony_ci		if (armada_3700_add_composite_clk(&data[i], driver_data->reg,
7668c2ecf20Sopenharmony_ci						  &driver_data->lock, dev, hw))
7678c2ecf20Sopenharmony_ci			dev_err(dev, "Can't register periph clock %s\n",
7688c2ecf20Sopenharmony_ci				data[i].name);
7698c2ecf20Sopenharmony_ci	}
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci	ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get,
7728c2ecf20Sopenharmony_ci				     driver_data->hw_data);
7738c2ecf20Sopenharmony_ci	if (ret) {
7748c2ecf20Sopenharmony_ci		for (i = 0; i < num_periph; i++)
7758c2ecf20Sopenharmony_ci			clk_hw_unregister(driver_data->hw_data->hws[i]);
7768c2ecf20Sopenharmony_ci		return ret;
7778c2ecf20Sopenharmony_ci	}
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, driver_data);
7808c2ecf20Sopenharmony_ci	return 0;
7818c2ecf20Sopenharmony_ci}
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_cistatic int armada_3700_periph_clock_remove(struct platform_device *pdev)
7848c2ecf20Sopenharmony_ci{
7858c2ecf20Sopenharmony_ci	struct clk_periph_driver_data *data = platform_get_drvdata(pdev);
7868c2ecf20Sopenharmony_ci	struct clk_hw_onecell_data *hw_data = data->hw_data;
7878c2ecf20Sopenharmony_ci	int i;
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	of_clk_del_provider(pdev->dev.of_node);
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci	for (i = 0; i < hw_data->num; i++)
7928c2ecf20Sopenharmony_ci		clk_hw_unregister(hw_data->hws[i]);
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	return 0;
7958c2ecf20Sopenharmony_ci}
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_cistatic struct platform_driver armada_3700_periph_clock_driver = {
7988c2ecf20Sopenharmony_ci	.probe = armada_3700_periph_clock_probe,
7998c2ecf20Sopenharmony_ci	.remove = armada_3700_periph_clock_remove,
8008c2ecf20Sopenharmony_ci	.driver		= {
8018c2ecf20Sopenharmony_ci		.name	= "marvell-armada-3700-periph-clock",
8028c2ecf20Sopenharmony_ci		.of_match_table = armada_3700_periph_clock_of_match,
8038c2ecf20Sopenharmony_ci		.pm	= &armada_3700_periph_clock_pm_ops,
8048c2ecf20Sopenharmony_ci	},
8058c2ecf20Sopenharmony_ci};
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_cibuiltin_platform_driver(armada_3700_periph_clock_driver);
808