xref: /linux/drivers/clk/mvebu/armada-37xx-periph.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
166c7bb7cSGregory CLEMENT // SPDX-License-Identifier: GPL-2.0+
28ca4746aSGregory CLEMENT /*
38ca4746aSGregory CLEMENT  * Marvell Armada 37xx SoC Peripheral clocks
48ca4746aSGregory CLEMENT  *
58ca4746aSGregory CLEMENT  * Copyright (C) 2016 Marvell
68ca4746aSGregory CLEMENT  *
78ca4746aSGregory CLEMENT  * Gregory CLEMENT <gregory.clement@free-electrons.com>
88ca4746aSGregory CLEMENT  *
98ca4746aSGregory CLEMENT  * Most of the peripheral clocks can be modelled like this:
108ca4746aSGregory CLEMENT  *             _____    _______    _______
118ca4746aSGregory CLEMENT  * TBG-A-P  --|     |  |       |  |       |   ______
128ca4746aSGregory CLEMENT  * TBG-B-P  --| Mux |--| /div1 |--| /div2 |--| Gate |--> perip_clk
138ca4746aSGregory CLEMENT  * TBG-A-S  --|     |  |       |  |       |  |______|
148ca4746aSGregory CLEMENT  * TBG-B-S  --|_____|  |_______|  |_______|
158ca4746aSGregory CLEMENT  *
168ca4746aSGregory CLEMENT  * However some clocks may use only one or two block or and use the
178ca4746aSGregory CLEMENT  * xtal clock as parent.
188ca4746aSGregory CLEMENT  */
198ca4746aSGregory CLEMENT 
208ca4746aSGregory CLEMENT #include <linux/clk-provider.h>
2162e59c4eSStephen Boyd #include <linux/io.h>
222089dc33SGregory CLEMENT #include <linux/mfd/syscon.h>
238ca4746aSGregory CLEMENT #include <linux/of.h>
248ca4746aSGregory CLEMENT #include <linux/platform_device.h>
252089dc33SGregory CLEMENT #include <linux/regmap.h>
268ca4746aSGregory CLEMENT #include <linux/slab.h>
27b191fe39SWang Qing #include <linux/jiffies.h>
288ca4746aSGregory CLEMENT 
298ca4746aSGregory CLEMENT #define TBG_SEL		0x0
308ca4746aSGregory CLEMENT #define DIV_SEL0	0x4
318ca4746aSGregory CLEMENT #define DIV_SEL1	0x8
328ca4746aSGregory CLEMENT #define DIV_SEL2	0xC
338ca4746aSGregory CLEMENT #define CLK_SEL		0x10
348ca4746aSGregory CLEMENT #define CLK_DIS		0x14
358ca4746aSGregory CLEMENT 
3661c40f35SGregory CLEMENT #define  ARMADA_37XX_DVFS_LOAD_1 1
372089dc33SGregory CLEMENT #define LOAD_LEVEL_NR	4
382089dc33SGregory CLEMENT 
392089dc33SGregory CLEMENT #define ARMADA_37XX_NB_L0L1	0x18
402089dc33SGregory CLEMENT #define ARMADA_37XX_NB_L2L3	0x1C
412089dc33SGregory CLEMENT #define		ARMADA_37XX_NB_TBG_DIV_OFF	13
422089dc33SGregory CLEMENT #define		ARMADA_37XX_NB_TBG_DIV_MASK	0x7
432089dc33SGregory CLEMENT #define		ARMADA_37XX_NB_CLK_SEL_OFF	11
442089dc33SGregory CLEMENT #define		ARMADA_37XX_NB_CLK_SEL_MASK	0x1
452089dc33SGregory CLEMENT #define		ARMADA_37XX_NB_TBG_SEL_OFF	9
462089dc33SGregory CLEMENT #define		ARMADA_37XX_NB_TBG_SEL_MASK	0x3
472089dc33SGregory CLEMENT #define		ARMADA_37XX_NB_CONFIG_SHIFT	16
482089dc33SGregory CLEMENT #define ARMADA_37XX_NB_DYN_MOD	0x24
492089dc33SGregory CLEMENT #define		ARMADA_37XX_NB_DFS_EN	31
502089dc33SGregory CLEMENT #define ARMADA_37XX_NB_CPU_LOAD	0x30
512089dc33SGregory CLEMENT #define		ARMADA_37XX_NB_CPU_LOAD_MASK	0x3
522089dc33SGregory CLEMENT #define		ARMADA_37XX_DVFS_LOAD_0		0
532089dc33SGregory CLEMENT #define		ARMADA_37XX_DVFS_LOAD_1		1
542089dc33SGregory CLEMENT #define		ARMADA_37XX_DVFS_LOAD_2		2
552089dc33SGregory CLEMENT #define		ARMADA_37XX_DVFS_LOAD_3		3
562089dc33SGregory CLEMENT 
578ca4746aSGregory CLEMENT struct clk_periph_driver_data {
588ca4746aSGregory CLEMENT 	struct clk_hw_onecell_data *hw_data;
598ca4746aSGregory CLEMENT 	spinlock_t lock;
60d9d95e78SMiquel Raynal 	void __iomem *reg;
615beb1e60SMiquel Raynal 
625beb1e60SMiquel Raynal 	/* Storage registers for suspend/resume operations */
635beb1e60SMiquel Raynal 	u32 tbg_sel;
645beb1e60SMiquel Raynal 	u32 div_sel0;
655beb1e60SMiquel Raynal 	u32 div_sel1;
665beb1e60SMiquel Raynal 	u32 div_sel2;
675beb1e60SMiquel Raynal 	u32 clk_sel;
685beb1e60SMiquel Raynal 	u32 clk_dis;
698ca4746aSGregory CLEMENT };
708ca4746aSGregory CLEMENT 
718ca4746aSGregory CLEMENT struct clk_double_div {
728ca4746aSGregory CLEMENT 	struct clk_hw hw;
738ca4746aSGregory CLEMENT 	void __iomem *reg1;
748ca4746aSGregory CLEMENT 	u8 shift1;
758ca4746aSGregory CLEMENT 	void __iomem *reg2;
768ca4746aSGregory CLEMENT 	u8 shift2;
778ca4746aSGregory CLEMENT };
788ca4746aSGregory CLEMENT 
799818a7a4SGregory CLEMENT struct clk_pm_cpu {
809818a7a4SGregory CLEMENT 	struct clk_hw hw;
819818a7a4SGregory CLEMENT 	void __iomem *reg_mux;
829818a7a4SGregory CLEMENT 	u8 shift_mux;
839818a7a4SGregory CLEMENT 	u32 mask_mux;
849818a7a4SGregory CLEMENT 	void __iomem *reg_div;
859818a7a4SGregory CLEMENT 	u8 shift_div;
862089dc33SGregory CLEMENT 	struct regmap *nb_pm_base;
87e93033afSPali Rohár 	unsigned long l1_expiration;
889818a7a4SGregory CLEMENT };
899818a7a4SGregory CLEMENT 
908ca4746aSGregory CLEMENT #define to_clk_double_div(_hw) container_of(_hw, struct clk_double_div, hw)
919818a7a4SGregory CLEMENT #define to_clk_pm_cpu(_hw) container_of(_hw, struct clk_pm_cpu, hw)
928ca4746aSGregory CLEMENT 
938ca4746aSGregory CLEMENT struct clk_periph_data {
948ca4746aSGregory CLEMENT 	const char *name;
958ca4746aSGregory CLEMENT 	const char * const *parent_names;
968ca4746aSGregory CLEMENT 	int num_parents;
978ca4746aSGregory CLEMENT 	struct clk_hw *mux_hw;
988ca4746aSGregory CLEMENT 	struct clk_hw *rate_hw;
998ca4746aSGregory CLEMENT 	struct clk_hw *gate_hw;
1009818a7a4SGregory CLEMENT 	struct clk_hw *muxrate_hw;
1018ca4746aSGregory CLEMENT 	bool is_double_div;
1028ca4746aSGregory CLEMENT };
1038ca4746aSGregory CLEMENT 
1048ca4746aSGregory CLEMENT static const struct clk_div_table clk_table6[] = {
1058ca4746aSGregory CLEMENT 	{ .val = 1, .div = 1, },
1068ca4746aSGregory CLEMENT 	{ .val = 2, .div = 2, },
1078ca4746aSGregory CLEMENT 	{ .val = 3, .div = 3, },
1088ca4746aSGregory CLEMENT 	{ .val = 4, .div = 4, },
1098ca4746aSGregory CLEMENT 	{ .val = 5, .div = 5, },
1108ca4746aSGregory CLEMENT 	{ .val = 6, .div = 6, },
1118ca4746aSGregory CLEMENT 	{ .val = 0, .div = 0, }, /* last entry */
1128ca4746aSGregory CLEMENT };
1138ca4746aSGregory CLEMENT 
1148ca4746aSGregory CLEMENT static const struct clk_div_table clk_table1[] = {
1158ca4746aSGregory CLEMENT 	{ .val = 0, .div = 1, },
1168ca4746aSGregory CLEMENT 	{ .val = 1, .div = 2, },
1178ca4746aSGregory CLEMENT 	{ .val = 0, .div = 0, }, /* last entry */
1188ca4746aSGregory CLEMENT };
1198ca4746aSGregory CLEMENT 
1208ca4746aSGregory CLEMENT static const struct clk_div_table clk_table2[] = {
1218ca4746aSGregory CLEMENT 	{ .val = 0, .div = 2, },
1228ca4746aSGregory CLEMENT 	{ .val = 1, .div = 4, },
1238ca4746aSGregory CLEMENT 	{ .val = 0, .div = 0, }, /* last entry */
1248ca4746aSGregory CLEMENT };
125adf4e289SGregory CLEMENT 
1268ca4746aSGregory CLEMENT static const struct clk_ops clk_double_div_ops;
1279818a7a4SGregory CLEMENT static const struct clk_ops clk_pm_cpu_ops;
1288ca4746aSGregory CLEMENT 
1298ca4746aSGregory CLEMENT #define PERIPH_GATE(_name, _bit)		\
1308ca4746aSGregory CLEMENT struct clk_gate gate_##_name = {		\
1318ca4746aSGregory CLEMENT 	.reg = (void *)CLK_DIS,			\
1328ca4746aSGregory CLEMENT 	.bit_idx = _bit,			\
1338ca4746aSGregory CLEMENT 	.hw.init = &(struct clk_init_data){	\
1348ca4746aSGregory CLEMENT 		.ops =  &clk_gate_ops,		\
1358ca4746aSGregory CLEMENT 	}					\
1368ca4746aSGregory CLEMENT };
1378ca4746aSGregory CLEMENT 
1388ca4746aSGregory CLEMENT #define PERIPH_MUX(_name, _shift)		\
1398ca4746aSGregory CLEMENT struct clk_mux mux_##_name = {			\
1408ca4746aSGregory CLEMENT 	.reg = (void *)TBG_SEL,			\
1418ca4746aSGregory CLEMENT 	.shift = _shift,			\
1428ca4746aSGregory CLEMENT 	.mask = 3,				\
1438ca4746aSGregory CLEMENT 	.hw.init = &(struct clk_init_data){	\
1448ca4746aSGregory CLEMENT 		.ops =  &clk_mux_ro_ops,	\
1458ca4746aSGregory CLEMENT 	}					\
1468ca4746aSGregory CLEMENT };
1478ca4746aSGregory CLEMENT 
1488ca4746aSGregory CLEMENT #define PERIPH_DOUBLEDIV(_name, _reg1, _reg2, _shift1, _shift2)	\
1498ca4746aSGregory CLEMENT struct clk_double_div rate_##_name = {		\
1508ca4746aSGregory CLEMENT 	.reg1 = (void *)_reg1,			\
1518ca4746aSGregory CLEMENT 	.reg2 = (void *)_reg2,			\
1528ca4746aSGregory CLEMENT 	.shift1 = _shift1,			\
1538ca4746aSGregory CLEMENT 	.shift2 = _shift2,			\
1548ca4746aSGregory CLEMENT 	.hw.init = &(struct clk_init_data){	\
1558ca4746aSGregory CLEMENT 		.ops =  &clk_double_div_ops,	\
1568ca4746aSGregory CLEMENT 	}					\
1578ca4746aSGregory CLEMENT };
1588ca4746aSGregory CLEMENT 
1598ca4746aSGregory CLEMENT #define PERIPH_DIV(_name, _reg, _shift, _table)	\
1608ca4746aSGregory CLEMENT struct clk_divider rate_##_name = {		\
1618ca4746aSGregory CLEMENT 	.reg = (void *)_reg,			\
1628ca4746aSGregory CLEMENT 	.table = _table,			\
1638ca4746aSGregory CLEMENT 	.shift = _shift,			\
1648ca4746aSGregory CLEMENT 	.hw.init = &(struct clk_init_data){	\
1658ca4746aSGregory CLEMENT 		.ops =  &clk_divider_ro_ops,	\
1668ca4746aSGregory CLEMENT 	}					\
1678ca4746aSGregory CLEMENT };
1688ca4746aSGregory CLEMENT 
1699818a7a4SGregory CLEMENT #define PERIPH_PM_CPU(_name, _shift1, _reg, _shift2)	\
1709818a7a4SGregory CLEMENT struct clk_pm_cpu muxrate_##_name = {		\
1719818a7a4SGregory CLEMENT 	.reg_mux = (void *)TBG_SEL,		\
1729818a7a4SGregory CLEMENT 	.mask_mux = 3,				\
1739818a7a4SGregory CLEMENT 	.shift_mux = _shift1,			\
1749818a7a4SGregory CLEMENT 	.reg_div = (void *)_reg,		\
1759818a7a4SGregory CLEMENT 	.shift_div = _shift2,			\
1769818a7a4SGregory CLEMENT 	.hw.init = &(struct clk_init_data){	\
1779818a7a4SGregory CLEMENT 		.ops =  &clk_pm_cpu_ops,	\
1789818a7a4SGregory CLEMENT 	}					\
1799818a7a4SGregory CLEMENT };
1809818a7a4SGregory CLEMENT 
1818ca4746aSGregory CLEMENT #define PERIPH_CLK_FULL_DD(_name, _bit, _shift, _reg1, _reg2, _shift1, _shift2)\
1828ca4746aSGregory CLEMENT static PERIPH_GATE(_name, _bit);			    \
1838ca4746aSGregory CLEMENT static PERIPH_MUX(_name, _shift);			    \
1848ca4746aSGregory CLEMENT static PERIPH_DOUBLEDIV(_name, _reg1, _reg2, _shift1, _shift2);
1858ca4746aSGregory CLEMENT 
1868ca4746aSGregory CLEMENT #define PERIPH_CLK_FULL(_name, _bit, _shift, _reg, _shift1, _table)	\
1878ca4746aSGregory CLEMENT static PERIPH_GATE(_name, _bit);			    \
1888ca4746aSGregory CLEMENT static PERIPH_MUX(_name, _shift);			    \
1898ca4746aSGregory CLEMENT static PERIPH_DIV(_name, _reg, _shift1, _table);
1908ca4746aSGregory CLEMENT 
1918ca4746aSGregory CLEMENT #define PERIPH_CLK_GATE_DIV(_name, _bit,  _reg, _shift, _table)	\
1928ca4746aSGregory CLEMENT static PERIPH_GATE(_name, _bit);			\
1938ca4746aSGregory CLEMENT static PERIPH_DIV(_name, _reg, _shift, _table);
1948ca4746aSGregory CLEMENT 
1958ca4746aSGregory CLEMENT #define PERIPH_CLK_MUX_DD(_name, _shift, _reg1, _reg2, _shift1, _shift2)\
1968ca4746aSGregory CLEMENT static PERIPH_MUX(_name, _shift);			    \
1978ca4746aSGregory CLEMENT static PERIPH_DOUBLEDIV(_name, _reg1, _reg2, _shift1, _shift2);
1988ca4746aSGregory CLEMENT 
1998ca4746aSGregory CLEMENT #define REF_CLK_FULL(_name)				\
2008ca4746aSGregory CLEMENT 	{ .name = #_name,				\
2018ca4746aSGregory CLEMENT 	  .parent_names = (const char *[]){ "TBG-A-P",	\
2028ca4746aSGregory CLEMENT 	      "TBG-B-P", "TBG-A-S", "TBG-B-S"},		\
2038ca4746aSGregory CLEMENT 	  .num_parents = 4,				\
2048ca4746aSGregory CLEMENT 	  .mux_hw = &mux_##_name.hw,			\
2058ca4746aSGregory CLEMENT 	  .gate_hw = &gate_##_name.hw,			\
2068ca4746aSGregory CLEMENT 	  .rate_hw = &rate_##_name.hw,			\
2078ca4746aSGregory CLEMENT 	}
2088ca4746aSGregory CLEMENT 
2098ca4746aSGregory CLEMENT #define REF_CLK_FULL_DD(_name)				\
2108ca4746aSGregory CLEMENT 	{ .name = #_name,				\
2118ca4746aSGregory CLEMENT 	  .parent_names = (const char *[]){ "TBG-A-P",	\
2128ca4746aSGregory CLEMENT 	      "TBG-B-P", "TBG-A-S", "TBG-B-S"},		\
2138ca4746aSGregory CLEMENT 	  .num_parents = 4,				\
2148ca4746aSGregory CLEMENT 	  .mux_hw = &mux_##_name.hw,			\
2158ca4746aSGregory CLEMENT 	  .gate_hw = &gate_##_name.hw,			\
2168ca4746aSGregory CLEMENT 	  .rate_hw = &rate_##_name.hw,			\
2178ca4746aSGregory CLEMENT 	  .is_double_div = true,			\
2188ca4746aSGregory CLEMENT 	}
2198ca4746aSGregory CLEMENT 
2208ca4746aSGregory CLEMENT #define REF_CLK_GATE(_name, _parent_name)			\
2218ca4746aSGregory CLEMENT 	{ .name = #_name,					\
2228ca4746aSGregory CLEMENT 	  .parent_names = (const char *[]){ _parent_name},	\
2238ca4746aSGregory CLEMENT 	  .num_parents = 1,					\
2248ca4746aSGregory CLEMENT 	  .gate_hw = &gate_##_name.hw,				\
2258ca4746aSGregory CLEMENT 	}
2268ca4746aSGregory CLEMENT 
2278ca4746aSGregory CLEMENT #define REF_CLK_GATE_DIV(_name, _parent_name)			\
2288ca4746aSGregory CLEMENT 	{ .name = #_name,					\
2298ca4746aSGregory CLEMENT 	  .parent_names = (const char *[]){ _parent_name},	\
2308ca4746aSGregory CLEMENT 	  .num_parents = 1,					\
2318ca4746aSGregory CLEMENT 	  .gate_hw = &gate_##_name.hw,				\
2328ca4746aSGregory CLEMENT 	  .rate_hw = &rate_##_name.hw,				\
2338ca4746aSGregory CLEMENT 	}
2348ca4746aSGregory CLEMENT 
2359818a7a4SGregory CLEMENT #define REF_CLK_PM_CPU(_name)				\
2368ca4746aSGregory CLEMENT 	{ .name = #_name,				\
2378ca4746aSGregory CLEMENT 	  .parent_names = (const char *[]){ "TBG-A-P",	\
2388ca4746aSGregory CLEMENT 	      "TBG-B-P", "TBG-A-S", "TBG-B-S"},		\
2398ca4746aSGregory CLEMENT 	  .num_parents = 4,				\
2409818a7a4SGregory CLEMENT 	  .muxrate_hw = &muxrate_##_name.hw,		\
2418ca4746aSGregory CLEMENT 	}
2428ca4746aSGregory CLEMENT 
2438ca4746aSGregory CLEMENT #define REF_CLK_MUX_DD(_name)				\
2448ca4746aSGregory CLEMENT 	{ .name = #_name,				\
2458ca4746aSGregory CLEMENT 	  .parent_names = (const char *[]){ "TBG-A-P",	\
2468ca4746aSGregory CLEMENT 	      "TBG-B-P", "TBG-A-S", "TBG-B-S"},		\
2478ca4746aSGregory CLEMENT 	  .num_parents = 4,				\
2488ca4746aSGregory CLEMENT 	  .mux_hw = &mux_##_name.hw,			\
2498ca4746aSGregory CLEMENT 	  .rate_hw = &rate_##_name.hw,			\
2508ca4746aSGregory CLEMENT 	  .is_double_div = true,			\
2518ca4746aSGregory CLEMENT 	}
2528ca4746aSGregory CLEMENT 
2538ca4746aSGregory CLEMENT /* NB periph clocks */
2548ca4746aSGregory CLEMENT PERIPH_CLK_FULL_DD(mmc, 2, 0, DIV_SEL2, DIV_SEL2, 16, 13);
2558ca4746aSGregory CLEMENT PERIPH_CLK_FULL_DD(sata_host, 3, 2, DIV_SEL2, DIV_SEL2, 10, 7);
2568ca4746aSGregory CLEMENT PERIPH_CLK_FULL_DD(sec_at, 6, 4, DIV_SEL1, DIV_SEL1, 3, 0);
2578ca4746aSGregory CLEMENT PERIPH_CLK_FULL_DD(sec_dap, 7, 6, DIV_SEL1, DIV_SEL1, 9, 6);
2588ca4746aSGregory CLEMENT PERIPH_CLK_FULL_DD(tscem, 8, 8, DIV_SEL1, DIV_SEL1, 15, 12);
2598ca4746aSGregory CLEMENT PERIPH_CLK_FULL(tscem_tmx, 10, 10, DIV_SEL1, 18, clk_table6);
2608ca4746aSGregory CLEMENT static PERIPH_GATE(avs, 11);
2618ca4746aSGregory CLEMENT PERIPH_CLK_FULL_DD(pwm, 13, 14, DIV_SEL0, DIV_SEL0, 3, 0);
2628ca4746aSGregory CLEMENT PERIPH_CLK_FULL_DD(sqf, 12, 12, DIV_SEL1, DIV_SEL1, 27, 24);
2638ca4746aSGregory CLEMENT static PERIPH_GATE(i2c_2, 16);
2648ca4746aSGregory CLEMENT static PERIPH_GATE(i2c_1, 17);
2658ca4746aSGregory CLEMENT PERIPH_CLK_GATE_DIV(ddr_phy, 19, DIV_SEL0, 18, clk_table2);
2668ca4746aSGregory CLEMENT PERIPH_CLK_FULL_DD(ddr_fclk, 21, 16, DIV_SEL0, DIV_SEL0, 15, 12);
2678ca4746aSGregory CLEMENT PERIPH_CLK_FULL(trace, 22, 18, DIV_SEL0, 20, clk_table6);
2688ca4746aSGregory CLEMENT PERIPH_CLK_FULL(counter, 23, 20, DIV_SEL0, 23, clk_table6);
2698ca4746aSGregory CLEMENT PERIPH_CLK_FULL_DD(eip97, 24, 24, DIV_SEL2, DIV_SEL2, 22, 19);
2709818a7a4SGregory CLEMENT static PERIPH_PM_CPU(cpu, 22, DIV_SEL0, 28);
2718ca4746aSGregory CLEMENT 
2728ca4746aSGregory CLEMENT static struct clk_periph_data data_nb[] = {
2738ca4746aSGregory CLEMENT 	REF_CLK_FULL_DD(mmc),
2748ca4746aSGregory CLEMENT 	REF_CLK_FULL_DD(sata_host),
2758ca4746aSGregory CLEMENT 	REF_CLK_FULL_DD(sec_at),
2768ca4746aSGregory CLEMENT 	REF_CLK_FULL_DD(sec_dap),
2778ca4746aSGregory CLEMENT 	REF_CLK_FULL_DD(tscem),
2788ca4746aSGregory CLEMENT 	REF_CLK_FULL(tscem_tmx),
2798ca4746aSGregory CLEMENT 	REF_CLK_GATE(avs, "xtal"),
2808ca4746aSGregory CLEMENT 	REF_CLK_FULL_DD(sqf),
2818ca4746aSGregory CLEMENT 	REF_CLK_FULL_DD(pwm),
2828ca4746aSGregory CLEMENT 	REF_CLK_GATE(i2c_2, "xtal"),
2838ca4746aSGregory CLEMENT 	REF_CLK_GATE(i2c_1, "xtal"),
2848ca4746aSGregory CLEMENT 	REF_CLK_GATE_DIV(ddr_phy, "TBG-A-S"),
2858ca4746aSGregory CLEMENT 	REF_CLK_FULL_DD(ddr_fclk),
2868ca4746aSGregory CLEMENT 	REF_CLK_FULL(trace),
2878ca4746aSGregory CLEMENT 	REF_CLK_FULL(counter),
2888ca4746aSGregory CLEMENT 	REF_CLK_FULL_DD(eip97),
2899818a7a4SGregory CLEMENT 	REF_CLK_PM_CPU(cpu),
2908ca4746aSGregory CLEMENT 	{ },
2918ca4746aSGregory CLEMENT };
2928ca4746aSGregory CLEMENT 
2938ca4746aSGregory CLEMENT /* SB periph clocks */
2948ca4746aSGregory CLEMENT PERIPH_CLK_MUX_DD(gbe_50, 6, DIV_SEL2, DIV_SEL2, 6, 9);
2958ca4746aSGregory CLEMENT PERIPH_CLK_MUX_DD(gbe_core, 8, DIV_SEL1, DIV_SEL1, 18, 21);
2968ca4746aSGregory CLEMENT PERIPH_CLK_MUX_DD(gbe_125, 10, DIV_SEL1, DIV_SEL1, 6, 9);
2978ca4746aSGregory CLEMENT static PERIPH_GATE(gbe1_50, 0);
2988ca4746aSGregory CLEMENT static PERIPH_GATE(gbe0_50, 1);
2998ca4746aSGregory CLEMENT static PERIPH_GATE(gbe1_125, 2);
3008ca4746aSGregory CLEMENT static PERIPH_GATE(gbe0_125, 3);
3018ca4746aSGregory CLEMENT PERIPH_CLK_GATE_DIV(gbe1_core, 4, DIV_SEL1, 13, clk_table1);
3028ca4746aSGregory CLEMENT PERIPH_CLK_GATE_DIV(gbe0_core, 5, DIV_SEL1, 14, clk_table1);
3038ca4746aSGregory CLEMENT PERIPH_CLK_GATE_DIV(gbe_bm, 12, DIV_SEL1, 0, clk_table1);
3048ca4746aSGregory CLEMENT PERIPH_CLK_FULL_DD(sdio, 11, 14, DIV_SEL0, DIV_SEL0, 3, 6);
3058ca4746aSGregory CLEMENT PERIPH_CLK_FULL_DD(usb32_usb2_sys, 16, 16, DIV_SEL0, DIV_SEL0, 9, 12);
3068ca4746aSGregory CLEMENT PERIPH_CLK_FULL_DD(usb32_ss_sys, 17, 18, DIV_SEL0, DIV_SEL0, 15, 18);
3074d8fb494SMiquel Raynal static PERIPH_GATE(pcie, 14);
3088ca4746aSGregory CLEMENT 
3098ca4746aSGregory CLEMENT static struct clk_periph_data data_sb[] = {
3108ca4746aSGregory CLEMENT 	REF_CLK_MUX_DD(gbe_50),
3118ca4746aSGregory CLEMENT 	REF_CLK_MUX_DD(gbe_core),
3128ca4746aSGregory CLEMENT 	REF_CLK_MUX_DD(gbe_125),
3138ca4746aSGregory CLEMENT 	REF_CLK_GATE(gbe1_50, "gbe_50"),
3148ca4746aSGregory CLEMENT 	REF_CLK_GATE(gbe0_50, "gbe_50"),
3158ca4746aSGregory CLEMENT 	REF_CLK_GATE(gbe1_125, "gbe_125"),
3168ca4746aSGregory CLEMENT 	REF_CLK_GATE(gbe0_125, "gbe_125"),
3178ca4746aSGregory CLEMENT 	REF_CLK_GATE_DIV(gbe1_core, "gbe_core"),
3188ca4746aSGregory CLEMENT 	REF_CLK_GATE_DIV(gbe0_core, "gbe_core"),
3198ca4746aSGregory CLEMENT 	REF_CLK_GATE_DIV(gbe_bm, "gbe_core"),
3208ca4746aSGregory CLEMENT 	REF_CLK_FULL_DD(sdio),
3218ca4746aSGregory CLEMENT 	REF_CLK_FULL_DD(usb32_usb2_sys),
3228ca4746aSGregory CLEMENT 	REF_CLK_FULL_DD(usb32_ss_sys),
3234d8fb494SMiquel Raynal 	REF_CLK_GATE(pcie, "gbe_core"),
3248ca4746aSGregory CLEMENT 	{ },
3258ca4746aSGregory CLEMENT };
3268ca4746aSGregory CLEMENT 
get_div(void __iomem * reg,int shift)3278ca4746aSGregory CLEMENT static unsigned int get_div(void __iomem *reg, int shift)
3288ca4746aSGregory CLEMENT {
3298ca4746aSGregory CLEMENT 	u32 val;
3308ca4746aSGregory CLEMENT 
3318ca4746aSGregory CLEMENT 	val = (readl(reg) >> shift) & 0x7;
3328ca4746aSGregory CLEMENT 	if (val > 6)
3338ca4746aSGregory CLEMENT 		return 0;
3348ca4746aSGregory CLEMENT 	return val;
3358ca4746aSGregory CLEMENT }
3368ca4746aSGregory CLEMENT 
clk_double_div_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)3378ca4746aSGregory CLEMENT static unsigned long clk_double_div_recalc_rate(struct clk_hw *hw,
3388ca4746aSGregory CLEMENT 						unsigned long parent_rate)
3398ca4746aSGregory CLEMENT {
3408ca4746aSGregory CLEMENT 	struct clk_double_div *double_div = to_clk_double_div(hw);
3418ca4746aSGregory CLEMENT 	unsigned int div;
3428ca4746aSGregory CLEMENT 
3438ca4746aSGregory CLEMENT 	div = get_div(double_div->reg1, double_div->shift1);
3448ca4746aSGregory CLEMENT 	div *= get_div(double_div->reg2, double_div->shift2);
3458ca4746aSGregory CLEMENT 
3468ca4746aSGregory CLEMENT 	return DIV_ROUND_UP_ULL((u64)parent_rate, div);
3478ca4746aSGregory CLEMENT }
3488ca4746aSGregory CLEMENT 
3498ca4746aSGregory CLEMENT static const struct clk_ops clk_double_div_ops = {
3508ca4746aSGregory CLEMENT 	.recalc_rate = clk_double_div_recalc_rate,
3518ca4746aSGregory CLEMENT };
3528ca4746aSGregory CLEMENT 
armada_3700_pm_dvfs_update_regs(unsigned int load_level,unsigned int * reg,unsigned int * offset)3532089dc33SGregory CLEMENT static void armada_3700_pm_dvfs_update_regs(unsigned int load_level,
3542089dc33SGregory CLEMENT 					    unsigned int *reg,
3552089dc33SGregory CLEMENT 					    unsigned int *offset)
3562089dc33SGregory CLEMENT {
3572089dc33SGregory CLEMENT 	if (load_level <= ARMADA_37XX_DVFS_LOAD_1)
3582089dc33SGregory CLEMENT 		*reg = ARMADA_37XX_NB_L0L1;
3592089dc33SGregory CLEMENT 	else
3602089dc33SGregory CLEMENT 		*reg = ARMADA_37XX_NB_L2L3;
3612089dc33SGregory CLEMENT 
3622089dc33SGregory CLEMENT 	if (load_level == ARMADA_37XX_DVFS_LOAD_0 ||
3632089dc33SGregory CLEMENT 	    load_level ==  ARMADA_37XX_DVFS_LOAD_2)
3642089dc33SGregory CLEMENT 		*offset += ARMADA_37XX_NB_CONFIG_SHIFT;
3652089dc33SGregory CLEMENT }
3662089dc33SGregory CLEMENT 
armada_3700_pm_dvfs_is_enabled(struct regmap * base)3672089dc33SGregory CLEMENT static bool armada_3700_pm_dvfs_is_enabled(struct regmap *base)
3682089dc33SGregory CLEMENT {
3692089dc33SGregory CLEMENT 	unsigned int val, reg = ARMADA_37XX_NB_DYN_MOD;
3702089dc33SGregory CLEMENT 
3712089dc33SGregory CLEMENT 	if (IS_ERR(base))
3722089dc33SGregory CLEMENT 		return false;
3732089dc33SGregory CLEMENT 
3742089dc33SGregory CLEMENT 	regmap_read(base, reg, &val);
3752089dc33SGregory CLEMENT 
3762089dc33SGregory CLEMENT 	return !!(val & BIT(ARMADA_37XX_NB_DFS_EN));
3772089dc33SGregory CLEMENT }
3782089dc33SGregory CLEMENT 
armada_3700_pm_dvfs_get_cpu_div(struct regmap * base)3792089dc33SGregory CLEMENT static unsigned int armada_3700_pm_dvfs_get_cpu_div(struct regmap *base)
3802089dc33SGregory CLEMENT {
3812089dc33SGregory CLEMENT 	unsigned int reg = ARMADA_37XX_NB_CPU_LOAD;
3822089dc33SGregory CLEMENT 	unsigned int offset = ARMADA_37XX_NB_TBG_DIV_OFF;
3832089dc33SGregory CLEMENT 	unsigned int load_level, div;
3842089dc33SGregory CLEMENT 
3852089dc33SGregory CLEMENT 	/*
3862089dc33SGregory CLEMENT 	 * This function is always called after the function
3872089dc33SGregory CLEMENT 	 * armada_3700_pm_dvfs_is_enabled, so no need to check again
3882089dc33SGregory CLEMENT 	 * if the base is valid.
3892089dc33SGregory CLEMENT 	 */
3902089dc33SGregory CLEMENT 	regmap_read(base, reg, &load_level);
3912089dc33SGregory CLEMENT 
3922089dc33SGregory CLEMENT 	/*
3932089dc33SGregory CLEMENT 	 * The register and the offset inside this register accessed to
3942089dc33SGregory CLEMENT 	 * read the current divider depend on the load level
3952089dc33SGregory CLEMENT 	 */
3962089dc33SGregory CLEMENT 	load_level &= ARMADA_37XX_NB_CPU_LOAD_MASK;
3972089dc33SGregory CLEMENT 	armada_3700_pm_dvfs_update_regs(load_level, &reg, &offset);
3982089dc33SGregory CLEMENT 
3992089dc33SGregory CLEMENT 	regmap_read(base, reg, &div);
4002089dc33SGregory CLEMENT 
4012089dc33SGregory CLEMENT 	return (div >> offset) & ARMADA_37XX_NB_TBG_DIV_MASK;
4022089dc33SGregory CLEMENT }
4032089dc33SGregory CLEMENT 
armada_3700_pm_dvfs_get_cpu_parent(struct regmap * base)4042089dc33SGregory CLEMENT static unsigned int armada_3700_pm_dvfs_get_cpu_parent(struct regmap *base)
4052089dc33SGregory CLEMENT {
4062089dc33SGregory CLEMENT 	unsigned int reg = ARMADA_37XX_NB_CPU_LOAD;
4072089dc33SGregory CLEMENT 	unsigned int offset = ARMADA_37XX_NB_TBG_SEL_OFF;
4082089dc33SGregory CLEMENT 	unsigned int load_level, sel;
4092089dc33SGregory CLEMENT 
4102089dc33SGregory CLEMENT 	/*
4112089dc33SGregory CLEMENT 	 * This function is always called after the function
4122089dc33SGregory CLEMENT 	 * armada_3700_pm_dvfs_is_enabled, so no need to check again
4132089dc33SGregory CLEMENT 	 * if the base is valid
4142089dc33SGregory CLEMENT 	 */
4152089dc33SGregory CLEMENT 	regmap_read(base, reg, &load_level);
4162089dc33SGregory CLEMENT 
4172089dc33SGregory CLEMENT 	/*
4182089dc33SGregory CLEMENT 	 * The register and the offset inside this register accessed to
4192089dc33SGregory CLEMENT 	 * read the current divider depend on the load level
4202089dc33SGregory CLEMENT 	 */
4212089dc33SGregory CLEMENT 	load_level &= ARMADA_37XX_NB_CPU_LOAD_MASK;
4222089dc33SGregory CLEMENT 	armada_3700_pm_dvfs_update_regs(load_level, &reg, &offset);
4232089dc33SGregory CLEMENT 
4242089dc33SGregory CLEMENT 	regmap_read(base, reg, &sel);
4252089dc33SGregory CLEMENT 
4262089dc33SGregory CLEMENT 	return (sel >> offset) & ARMADA_37XX_NB_TBG_SEL_MASK;
4272089dc33SGregory CLEMENT }
4282089dc33SGregory CLEMENT 
clk_pm_cpu_get_parent(struct clk_hw * hw)4299818a7a4SGregory CLEMENT static u8 clk_pm_cpu_get_parent(struct clk_hw *hw)
4309818a7a4SGregory CLEMENT {
4319818a7a4SGregory CLEMENT 	struct clk_pm_cpu *pm_cpu = to_clk_pm_cpu(hw);
4329818a7a4SGregory CLEMENT 	u32 val;
4339818a7a4SGregory CLEMENT 
4342089dc33SGregory CLEMENT 	if (armada_3700_pm_dvfs_is_enabled(pm_cpu->nb_pm_base)) {
4352089dc33SGregory CLEMENT 		val = armada_3700_pm_dvfs_get_cpu_parent(pm_cpu->nb_pm_base);
4362089dc33SGregory CLEMENT 	} else {
4379818a7a4SGregory CLEMENT 		val = readl(pm_cpu->reg_mux) >> pm_cpu->shift_mux;
4389818a7a4SGregory CLEMENT 		val &= pm_cpu->mask_mux;
4392089dc33SGregory CLEMENT 	}
4409818a7a4SGregory CLEMENT 
4419818a7a4SGregory CLEMENT 	return val;
4429818a7a4SGregory CLEMENT }
4439818a7a4SGregory CLEMENT 
clk_pm_cpu_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)4449818a7a4SGregory CLEMENT static unsigned long clk_pm_cpu_recalc_rate(struct clk_hw *hw,
4459818a7a4SGregory CLEMENT 					    unsigned long parent_rate)
4469818a7a4SGregory CLEMENT {
4479818a7a4SGregory CLEMENT 	struct clk_pm_cpu *pm_cpu = to_clk_pm_cpu(hw);
4489818a7a4SGregory CLEMENT 	unsigned int div;
4499818a7a4SGregory CLEMENT 
4502089dc33SGregory CLEMENT 	if (armada_3700_pm_dvfs_is_enabled(pm_cpu->nb_pm_base))
4512089dc33SGregory CLEMENT 		div = armada_3700_pm_dvfs_get_cpu_div(pm_cpu->nb_pm_base);
4522089dc33SGregory CLEMENT 	else
4539818a7a4SGregory CLEMENT 		div = get_div(pm_cpu->reg_div, pm_cpu->shift_div);
4549818a7a4SGregory CLEMENT 	return DIV_ROUND_UP_ULL((u64)parent_rate, div);
4559818a7a4SGregory CLEMENT }
4569818a7a4SGregory CLEMENT 
clk_pm_cpu_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * parent_rate)4572089dc33SGregory CLEMENT static long clk_pm_cpu_round_rate(struct clk_hw *hw, unsigned long rate,
4582089dc33SGregory CLEMENT 				  unsigned long *parent_rate)
4592089dc33SGregory CLEMENT {
4602089dc33SGregory CLEMENT 	struct clk_pm_cpu *pm_cpu = to_clk_pm_cpu(hw);
4612089dc33SGregory CLEMENT 	struct regmap *base = pm_cpu->nb_pm_base;
4622089dc33SGregory CLEMENT 	unsigned int div = *parent_rate / rate;
4632089dc33SGregory CLEMENT 	unsigned int load_level;
4642089dc33SGregory CLEMENT 	/* only available when DVFS is enabled */
4652089dc33SGregory CLEMENT 	if (!armada_3700_pm_dvfs_is_enabled(base))
4662089dc33SGregory CLEMENT 		return -EINVAL;
4672089dc33SGregory CLEMENT 
4682089dc33SGregory CLEMENT 	for (load_level = 0; load_level < LOAD_LEVEL_NR; load_level++) {
4692089dc33SGregory CLEMENT 		unsigned int reg, val, offset = ARMADA_37XX_NB_TBG_DIV_OFF;
4702089dc33SGregory CLEMENT 
4712089dc33SGregory CLEMENT 		armada_3700_pm_dvfs_update_regs(load_level, &reg, &offset);
4722089dc33SGregory CLEMENT 
4732089dc33SGregory CLEMENT 		regmap_read(base, reg, &val);
4742089dc33SGregory CLEMENT 
4752089dc33SGregory CLEMENT 		val >>= offset;
4762089dc33SGregory CLEMENT 		val &= ARMADA_37XX_NB_TBG_DIV_MASK;
4772089dc33SGregory CLEMENT 		if (val == div)
4782089dc33SGregory CLEMENT 			/*
4792089dc33SGregory CLEMENT 			 * We found a load level matching the target
4802089dc33SGregory CLEMENT 			 * divider, switch to this load level and
4812089dc33SGregory CLEMENT 			 * return.
4822089dc33SGregory CLEMENT 			 */
4832089dc33SGregory CLEMENT 			return *parent_rate / div;
4842089dc33SGregory CLEMENT 	}
4852089dc33SGregory CLEMENT 
4862089dc33SGregory CLEMENT 	/* We didn't find any valid divider */
4872089dc33SGregory CLEMENT 	return -EINVAL;
4882089dc33SGregory CLEMENT }
4892089dc33SGregory CLEMENT 
49061c40f35SGregory CLEMENT /*
4914decb918SPali Rohár  * Workaround when base CPU frequnecy is 1000 or 1200 MHz
4924decb918SPali Rohár  *
4934decb918SPali Rohár  * Switching the CPU from the L2 or L3 frequencies (250/300 or 200 MHz
4944decb918SPali Rohár  * respectively) to L0 frequency (1/1.2 GHz) requires a significant
49561c40f35SGregory CLEMENT  * amount of time to let VDD stabilize to the appropriate
49661c40f35SGregory CLEMENT  * voltage. This amount of time is large enough that it cannot be
49761c40f35SGregory CLEMENT  * covered by the hardware countdown register. Due to this, the CPU
49861c40f35SGregory CLEMENT  * might start operating at L0 before the voltage is stabilized,
49961c40f35SGregory CLEMENT  * leading to CPU stalls.
50061c40f35SGregory CLEMENT  *
50161c40f35SGregory CLEMENT  * To work around this problem, we prevent switching directly from the
50261c40f35SGregory CLEMENT  * L2/L3 frequencies to the L0 frequency, and instead switch to the L1
50361c40f35SGregory CLEMENT  * frequency in-between. The sequence therefore becomes:
5044decb918SPali Rohár  * 1. First switch from L2/L3 (200/250/300 MHz) to L1 (500/600 MHz)
50561c40f35SGregory CLEMENT  * 2. Sleep 20ms for stabling VDD voltage
5064decb918SPali Rohár  * 3. Then switch from L1 (500/600 MHz) to L0 (1000/1200 MHz).
50761c40f35SGregory CLEMENT  */
clk_pm_cpu_set_rate_wa(struct clk_pm_cpu * pm_cpu,unsigned int new_level,unsigned long rate,struct regmap * base)508e93033afSPali Rohár static void clk_pm_cpu_set_rate_wa(struct clk_pm_cpu *pm_cpu,
509e93033afSPali Rohár 				   unsigned int new_level, unsigned long rate,
510e93033afSPali Rohár 				   struct regmap *base)
51161c40f35SGregory CLEMENT {
51261c40f35SGregory CLEMENT 	unsigned int cur_level;
51361c40f35SGregory CLEMENT 
51461c40f35SGregory CLEMENT 	regmap_read(base, ARMADA_37XX_NB_CPU_LOAD, &cur_level);
51561c40f35SGregory CLEMENT 	cur_level &= ARMADA_37XX_NB_CPU_LOAD_MASK;
516e93033afSPali Rohár 
517e93033afSPali Rohár 	if (cur_level == new_level)
51861c40f35SGregory CLEMENT 		return;
51961c40f35SGregory CLEMENT 
520e93033afSPali Rohár 	/*
521e93033afSPali Rohár 	 * System wants to go to L1 on its own. If we are going from L2/L3,
522e93033afSPali Rohár 	 * remember when 20ms will expire. If from L0, set the value so that
523e93033afSPali Rohár 	 * next switch to L0 won't have to wait.
524e93033afSPali Rohár 	 */
525e93033afSPali Rohár 	if (new_level == ARMADA_37XX_DVFS_LOAD_1) {
526e93033afSPali Rohár 		if (cur_level == ARMADA_37XX_DVFS_LOAD_0)
527e93033afSPali Rohár 			pm_cpu->l1_expiration = jiffies;
528e93033afSPali Rohár 		else
529e93033afSPali Rohár 			pm_cpu->l1_expiration = jiffies + msecs_to_jiffies(20);
530e93033afSPali Rohár 		return;
531e93033afSPali Rohár 	}
532e93033afSPali Rohár 
533e93033afSPali Rohár 	/*
534e93033afSPali Rohár 	 * If we are setting to L2/L3, just invalidate L1 expiration time,
535e93033afSPali Rohár 	 * sleeping is not needed.
536e93033afSPali Rohár 	 */
537e93033afSPali Rohár 	if (rate < 1000*1000*1000)
538e93033afSPali Rohár 		goto invalidate_l1_exp;
539e93033afSPali Rohár 
540e93033afSPali Rohár 	/*
541e93033afSPali Rohár 	 * We are going to L0 with rate >= 1GHz. Check whether we have been at
542e93033afSPali Rohár 	 * L1 for long enough time. If not, go to L1 for 20ms.
543e93033afSPali Rohár 	 */
544b191fe39SWang Qing 	if (pm_cpu->l1_expiration && time_is_before_eq_jiffies(pm_cpu->l1_expiration))
545e93033afSPali Rohár 		goto invalidate_l1_exp;
546e93033afSPali Rohár 
54761c40f35SGregory CLEMENT 	regmap_update_bits(base, ARMADA_37XX_NB_CPU_LOAD,
54861c40f35SGregory CLEMENT 			   ARMADA_37XX_NB_CPU_LOAD_MASK,
54961c40f35SGregory CLEMENT 			   ARMADA_37XX_DVFS_LOAD_1);
55061c40f35SGregory CLEMENT 	msleep(20);
551e93033afSPali Rohár 
552e93033afSPali Rohár invalidate_l1_exp:
553e93033afSPali Rohár 	pm_cpu->l1_expiration = 0;
55461c40f35SGregory CLEMENT }
55561c40f35SGregory CLEMENT 
clk_pm_cpu_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)5562089dc33SGregory CLEMENT static int clk_pm_cpu_set_rate(struct clk_hw *hw, unsigned long rate,
5572089dc33SGregory CLEMENT 			       unsigned long parent_rate)
5582089dc33SGregory CLEMENT {
5592089dc33SGregory CLEMENT 	struct clk_pm_cpu *pm_cpu = to_clk_pm_cpu(hw);
5602089dc33SGregory CLEMENT 	struct regmap *base = pm_cpu->nb_pm_base;
5612089dc33SGregory CLEMENT 	unsigned int div = parent_rate / rate;
5622089dc33SGregory CLEMENT 	unsigned int load_level;
5632089dc33SGregory CLEMENT 
5642089dc33SGregory CLEMENT 	/* only available when DVFS is enabled */
5652089dc33SGregory CLEMENT 	if (!armada_3700_pm_dvfs_is_enabled(base))
5662089dc33SGregory CLEMENT 		return -EINVAL;
5672089dc33SGregory CLEMENT 
5682089dc33SGregory CLEMENT 	for (load_level = 0; load_level < LOAD_LEVEL_NR; load_level++) {
5692089dc33SGregory CLEMENT 		unsigned int reg, mask, val,
5702089dc33SGregory CLEMENT 			offset = ARMADA_37XX_NB_TBG_DIV_OFF;
5712089dc33SGregory CLEMENT 
5722089dc33SGregory CLEMENT 		armada_3700_pm_dvfs_update_regs(load_level, &reg, &offset);
5732089dc33SGregory CLEMENT 
5742089dc33SGregory CLEMENT 		regmap_read(base, reg, &val);
5752089dc33SGregory CLEMENT 		val >>= offset;
5762089dc33SGregory CLEMENT 		val &= ARMADA_37XX_NB_TBG_DIV_MASK;
5772089dc33SGregory CLEMENT 
5782089dc33SGregory CLEMENT 		if (val == div) {
5792089dc33SGregory CLEMENT 			/*
5802089dc33SGregory CLEMENT 			 * We found a load level matching the target
5812089dc33SGregory CLEMENT 			 * divider, switch to this load level and
5822089dc33SGregory CLEMENT 			 * return.
5832089dc33SGregory CLEMENT 			 */
5842089dc33SGregory CLEMENT 			reg = ARMADA_37XX_NB_CPU_LOAD;
5852089dc33SGregory CLEMENT 			mask = ARMADA_37XX_NB_CPU_LOAD_MASK;
58661c40f35SGregory CLEMENT 
587e93033afSPali Rohár 			/* Apply workaround when base CPU frequency is 1000 or 1200 MHz */
588e93033afSPali Rohár 			if (parent_rate >= 1000*1000*1000)
589e93033afSPali Rohár 				clk_pm_cpu_set_rate_wa(pm_cpu, load_level, rate, base);
59061c40f35SGregory CLEMENT 
5912089dc33SGregory CLEMENT 			regmap_update_bits(base, reg, mask, load_level);
5922089dc33SGregory CLEMENT 
5932089dc33SGregory CLEMENT 			return rate;
5942089dc33SGregory CLEMENT 		}
5952089dc33SGregory CLEMENT 	}
5962089dc33SGregory CLEMENT 
5972089dc33SGregory CLEMENT 	/* We didn't find any valid divider */
5982089dc33SGregory CLEMENT 	return -EINVAL;
5992089dc33SGregory CLEMENT }
6002089dc33SGregory CLEMENT 
6019818a7a4SGregory CLEMENT static const struct clk_ops clk_pm_cpu_ops = {
6029818a7a4SGregory CLEMENT 	.get_parent = clk_pm_cpu_get_parent,
6032089dc33SGregory CLEMENT 	.round_rate = clk_pm_cpu_round_rate,
6042089dc33SGregory CLEMENT 	.set_rate = clk_pm_cpu_set_rate,
6059818a7a4SGregory CLEMENT 	.recalc_rate = clk_pm_cpu_recalc_rate,
6069818a7a4SGregory CLEMENT };
6079818a7a4SGregory CLEMENT 
6088ca4746aSGregory CLEMENT static const struct of_device_id armada_3700_periph_clock_of_match[] = {
6098ca4746aSGregory CLEMENT 	{ .compatible = "marvell,armada-3700-periph-clock-nb",
6108ca4746aSGregory CLEMENT 	  .data = data_nb, },
6118ca4746aSGregory CLEMENT 	{ .compatible = "marvell,armada-3700-periph-clock-sb",
6128ca4746aSGregory CLEMENT 	.data = data_sb, },
6138ca4746aSGregory CLEMENT 	{ }
6148ca4746aSGregory CLEMENT };
615adf4e289SGregory CLEMENT 
armada_3700_add_composite_clk(const struct clk_periph_data * data,void __iomem * reg,spinlock_t * lock,struct device * dev,struct clk_hw ** hw)6168ca4746aSGregory CLEMENT static int armada_3700_add_composite_clk(const struct clk_periph_data *data,
6178ca4746aSGregory CLEMENT 					 void __iomem *reg, spinlock_t *lock,
618981e1beaSGregory CLEMENT 					 struct device *dev, struct clk_hw **hw)
6198ca4746aSGregory CLEMENT {
6208ca4746aSGregory CLEMENT 	const struct clk_ops *mux_ops = NULL, *gate_ops = NULL,
6218ca4746aSGregory CLEMENT 		*rate_ops = NULL;
6228ca4746aSGregory CLEMENT 	struct clk_hw *mux_hw = NULL, *gate_hw = NULL, *rate_hw = NULL;
6238ca4746aSGregory CLEMENT 
6248ca4746aSGregory CLEMENT 	if (data->mux_hw) {
6258ca4746aSGregory CLEMENT 		struct clk_mux *mux;
6268ca4746aSGregory CLEMENT 
6278ca4746aSGregory CLEMENT 		mux_hw = data->mux_hw;
6288ca4746aSGregory CLEMENT 		mux = to_clk_mux(mux_hw);
6298ca4746aSGregory CLEMENT 		mux->lock = lock;
6308ca4746aSGregory CLEMENT 		mux_ops = mux_hw->init->ops;
6318ca4746aSGregory CLEMENT 		mux->reg = reg + (u64)mux->reg;
6328ca4746aSGregory CLEMENT 	}
6338ca4746aSGregory CLEMENT 
6348ca4746aSGregory CLEMENT 	if (data->gate_hw) {
6358ca4746aSGregory CLEMENT 		struct clk_gate *gate;
6368ca4746aSGregory CLEMENT 
6378ca4746aSGregory CLEMENT 		gate_hw = data->gate_hw;
6388ca4746aSGregory CLEMENT 		gate = to_clk_gate(gate_hw);
6398ca4746aSGregory CLEMENT 		gate->lock = lock;
6408ca4746aSGregory CLEMENT 		gate_ops = gate_hw->init->ops;
6418ca4746aSGregory CLEMENT 		gate->reg = reg + (u64)gate->reg;
6424aa6c99dSGregory CLEMENT 		gate->flags = CLK_GATE_SET_TO_DISABLE;
6438ca4746aSGregory CLEMENT 	}
6448ca4746aSGregory CLEMENT 
6458ca4746aSGregory CLEMENT 	if (data->rate_hw) {
6468ca4746aSGregory CLEMENT 		rate_hw = data->rate_hw;
6478ca4746aSGregory CLEMENT 		rate_ops = rate_hw->init->ops;
6488ca4746aSGregory CLEMENT 		if (data->is_double_div) {
6498ca4746aSGregory CLEMENT 			struct clk_double_div *rate;
6508ca4746aSGregory CLEMENT 
6518ca4746aSGregory CLEMENT 			rate =  to_clk_double_div(rate_hw);
6528ca4746aSGregory CLEMENT 			rate->reg1 = reg + (u64)rate->reg1;
6538ca4746aSGregory CLEMENT 			rate->reg2 = reg + (u64)rate->reg2;
6548ca4746aSGregory CLEMENT 		} else {
6558ca4746aSGregory CLEMENT 			struct clk_divider *rate = to_clk_divider(rate_hw);
6568ca4746aSGregory CLEMENT 			const struct clk_div_table *clkt;
6578ca4746aSGregory CLEMENT 			int table_size = 0;
6588ca4746aSGregory CLEMENT 
6598ca4746aSGregory CLEMENT 			rate->reg = reg + (u64)rate->reg;
6608ca4746aSGregory CLEMENT 			for (clkt = rate->table; clkt->div; clkt++)
6618ca4746aSGregory CLEMENT 				table_size++;
6628ca4746aSGregory CLEMENT 			rate->width = order_base_2(table_size);
6638ca4746aSGregory CLEMENT 			rate->lock = lock;
6648ca4746aSGregory CLEMENT 		}
6658ca4746aSGregory CLEMENT 	}
6668ca4746aSGregory CLEMENT 
6679818a7a4SGregory CLEMENT 	if (data->muxrate_hw) {
6689818a7a4SGregory CLEMENT 		struct clk_pm_cpu *pmcpu_clk;
6699818a7a4SGregory CLEMENT 		struct clk_hw *muxrate_hw = data->muxrate_hw;
6702089dc33SGregory CLEMENT 		struct regmap *map;
6719818a7a4SGregory CLEMENT 
6729818a7a4SGregory CLEMENT 		pmcpu_clk =  to_clk_pm_cpu(muxrate_hw);
6739818a7a4SGregory CLEMENT 		pmcpu_clk->reg_mux = reg + (u64)pmcpu_clk->reg_mux;
6749818a7a4SGregory CLEMENT 		pmcpu_clk->reg_div = reg + (u64)pmcpu_clk->reg_div;
6759818a7a4SGregory CLEMENT 
6769818a7a4SGregory CLEMENT 		mux_hw = muxrate_hw;
6779818a7a4SGregory CLEMENT 		rate_hw = muxrate_hw;
6789818a7a4SGregory CLEMENT 		mux_ops = muxrate_hw->init->ops;
6799818a7a4SGregory CLEMENT 		rate_ops = muxrate_hw->init->ops;
6802089dc33SGregory CLEMENT 
6812089dc33SGregory CLEMENT 		map = syscon_regmap_lookup_by_compatible(
6822089dc33SGregory CLEMENT 				"marvell,armada-3700-nb-pm");
6832089dc33SGregory CLEMENT 		pmcpu_clk->nb_pm_base = map;
6849818a7a4SGregory CLEMENT 	}
6859818a7a4SGregory CLEMENT 
686981e1beaSGregory CLEMENT 	*hw = clk_hw_register_composite(dev, data->name, data->parent_names,
6878ca4746aSGregory CLEMENT 					data->num_parents, mux_hw,
6888ca4746aSGregory CLEMENT 					mux_ops, rate_hw, rate_ops,
6898ca4746aSGregory CLEMENT 					gate_hw, gate_ops, CLK_IGNORE_UNUSED);
6908ca4746aSGregory CLEMENT 
6919713588fSGomonovych, Vasyl 	return PTR_ERR_OR_ZERO(*hw);
6928ca4746aSGregory CLEMENT }
6938ca4746aSGregory CLEMENT 
armada_3700_periph_clock_suspend(struct device * dev)6945beb1e60SMiquel Raynal static int __maybe_unused armada_3700_periph_clock_suspend(struct device *dev)
6955beb1e60SMiquel Raynal {
6965beb1e60SMiquel Raynal 	struct clk_periph_driver_data *data = dev_get_drvdata(dev);
6975beb1e60SMiquel Raynal 
6985beb1e60SMiquel Raynal 	data->tbg_sel = readl(data->reg + TBG_SEL);
6995beb1e60SMiquel Raynal 	data->div_sel0 = readl(data->reg + DIV_SEL0);
7005beb1e60SMiquel Raynal 	data->div_sel1 = readl(data->reg + DIV_SEL1);
7015beb1e60SMiquel Raynal 	data->div_sel2 = readl(data->reg + DIV_SEL2);
7025beb1e60SMiquel Raynal 	data->clk_sel = readl(data->reg + CLK_SEL);
7035beb1e60SMiquel Raynal 	data->clk_dis = readl(data->reg + CLK_DIS);
7045beb1e60SMiquel Raynal 
7055beb1e60SMiquel Raynal 	return 0;
7065beb1e60SMiquel Raynal }
7075beb1e60SMiquel Raynal 
armada_3700_periph_clock_resume(struct device * dev)7085beb1e60SMiquel Raynal static int __maybe_unused armada_3700_periph_clock_resume(struct device *dev)
7095beb1e60SMiquel Raynal {
7105beb1e60SMiquel Raynal 	struct clk_periph_driver_data *data = dev_get_drvdata(dev);
7115beb1e60SMiquel Raynal 
7125beb1e60SMiquel Raynal 	/* Follow the same order than what the Cortex-M3 does (ATF code) */
7135beb1e60SMiquel Raynal 	writel(data->clk_dis, data->reg + CLK_DIS);
7145beb1e60SMiquel Raynal 	writel(data->div_sel0, data->reg + DIV_SEL0);
7155beb1e60SMiquel Raynal 	writel(data->div_sel1, data->reg + DIV_SEL1);
7165beb1e60SMiquel Raynal 	writel(data->div_sel2, data->reg + DIV_SEL2);
7175beb1e60SMiquel Raynal 	writel(data->tbg_sel, data->reg + TBG_SEL);
7185beb1e60SMiquel Raynal 	writel(data->clk_sel, data->reg + CLK_SEL);
7195beb1e60SMiquel Raynal 
7205beb1e60SMiquel Raynal 	return 0;
7215beb1e60SMiquel Raynal }
7225beb1e60SMiquel Raynal 
7235beb1e60SMiquel Raynal static const struct dev_pm_ops armada_3700_periph_clock_pm_ops = {
724e7ea59b6SMiquel Raynal 	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(armada_3700_periph_clock_suspend,
7255beb1e60SMiquel Raynal 				      armada_3700_periph_clock_resume)
7265beb1e60SMiquel Raynal };
7275beb1e60SMiquel Raynal 
armada_3700_periph_clock_probe(struct platform_device * pdev)7288ca4746aSGregory CLEMENT static int armada_3700_periph_clock_probe(struct platform_device *pdev)
7298ca4746aSGregory CLEMENT {
7308ca4746aSGregory CLEMENT 	struct clk_periph_driver_data *driver_data;
7318ca4746aSGregory CLEMENT 	struct device_node *np = pdev->dev.of_node;
7328ca4746aSGregory CLEMENT 	const struct clk_periph_data *data;
7338ca4746aSGregory CLEMENT 	struct device *dev = &pdev->dev;
7348ca4746aSGregory CLEMENT 	int num_periph = 0, i, ret;
7358ca4746aSGregory CLEMENT 
7368ca4746aSGregory CLEMENT 	data = of_device_get_match_data(dev);
7378ca4746aSGregory CLEMENT 	if (!data)
7388ca4746aSGregory CLEMENT 		return -ENODEV;
7398ca4746aSGregory CLEMENT 
7408ca4746aSGregory CLEMENT 	while (data[num_periph].name)
7418ca4746aSGregory CLEMENT 		num_periph++;
7428ca4746aSGregory CLEMENT 
7438ca4746aSGregory CLEMENT 	driver_data = devm_kzalloc(dev, sizeof(*driver_data), GFP_KERNEL);
7448ca4746aSGregory CLEMENT 	if (!driver_data)
7458ca4746aSGregory CLEMENT 		return -ENOMEM;
7468ca4746aSGregory CLEMENT 
7470ed2dd03SKees Cook 	driver_data->hw_data = devm_kzalloc(dev,
7480ed2dd03SKees Cook 					    struct_size(driver_data->hw_data,
7490ed2dd03SKees Cook 							hws, num_periph),
7508ca4746aSGregory CLEMENT 					    GFP_KERNEL);
7518ca4746aSGregory CLEMENT 	if (!driver_data->hw_data)
7528ca4746aSGregory CLEMENT 		return -ENOMEM;
7538ca4746aSGregory CLEMENT 	driver_data->hw_data->num = num_periph;
7548ca4746aSGregory CLEMENT 
7550a26c3f8SYangtao Li 	driver_data->reg = devm_platform_ioremap_resource(pdev, 0);
756d9d95e78SMiquel Raynal 	if (IS_ERR(driver_data->reg))
757d9d95e78SMiquel Raynal 		return PTR_ERR(driver_data->reg);
758d9d95e78SMiquel Raynal 
7598ca4746aSGregory CLEMENT 	spin_lock_init(&driver_data->lock);
7608ca4746aSGregory CLEMENT 
7618ca4746aSGregory CLEMENT 	for (i = 0; i < num_periph; i++) {
762981e1beaSGregory CLEMENT 		struct clk_hw **hw = &driver_data->hw_data->hws[i];
763d9d95e78SMiquel Raynal 		if (armada_3700_add_composite_clk(&data[i], driver_data->reg,
7648ca4746aSGregory CLEMENT 						  &driver_data->lock, dev, hw))
7658ca4746aSGregory CLEMENT 			dev_err(dev, "Can't register periph clock %s\n",
7668ca4746aSGregory CLEMENT 				data[i].name);
7678ca4746aSGregory CLEMENT 	}
7688ca4746aSGregory CLEMENT 
7698ca4746aSGregory CLEMENT 	ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get,
7708ca4746aSGregory CLEMENT 				     driver_data->hw_data);
7718ca4746aSGregory CLEMENT 	if (ret) {
7728ca4746aSGregory CLEMENT 		for (i = 0; i < num_periph; i++)
7738ca4746aSGregory CLEMENT 			clk_hw_unregister(driver_data->hw_data->hws[i]);
7748ca4746aSGregory CLEMENT 		return ret;
7758ca4746aSGregory CLEMENT 	}
7768ca4746aSGregory CLEMENT 
7778ca4746aSGregory CLEMENT 	platform_set_drvdata(pdev, driver_data);
7788ca4746aSGregory CLEMENT 	return 0;
7798ca4746aSGregory CLEMENT }
7808ca4746aSGregory CLEMENT 
armada_3700_periph_clock_remove(struct platform_device * pdev)781d9f139daSUwe Kleine-König static void armada_3700_periph_clock_remove(struct platform_device *pdev)
7828ca4746aSGregory CLEMENT {
7838ca4746aSGregory CLEMENT 	struct clk_periph_driver_data *data = platform_get_drvdata(pdev);
7848ca4746aSGregory CLEMENT 	struct clk_hw_onecell_data *hw_data = data->hw_data;
7858ca4746aSGregory CLEMENT 	int i;
7868ca4746aSGregory CLEMENT 
7878ca4746aSGregory CLEMENT 	of_clk_del_provider(pdev->dev.of_node);
7888ca4746aSGregory CLEMENT 
7898ca4746aSGregory CLEMENT 	for (i = 0; i < hw_data->num; i++)
7908ca4746aSGregory CLEMENT 		clk_hw_unregister(hw_data->hws[i]);
7918ca4746aSGregory CLEMENT }
7928ca4746aSGregory CLEMENT 
7938ca4746aSGregory CLEMENT static struct platform_driver armada_3700_periph_clock_driver = {
7948ca4746aSGregory CLEMENT 	.probe = armada_3700_periph_clock_probe,
795*f00b45dbSUwe Kleine-König 	.remove = armada_3700_periph_clock_remove,
7968ca4746aSGregory CLEMENT 	.driver		= {
7978ca4746aSGregory CLEMENT 		.name	= "marvell-armada-3700-periph-clock",
7988ca4746aSGregory CLEMENT 		.of_match_table = armada_3700_periph_clock_of_match,
7995beb1e60SMiquel Raynal 		.pm	= &armada_3700_periph_clock_pm_ops,
8008ca4746aSGregory CLEMENT 	},
8018ca4746aSGregory CLEMENT };
8028ca4746aSGregory CLEMENT 
8038ca4746aSGregory CLEMENT builtin_platform_driver(armada_3700_periph_clock_driver);
804