xref: /linux/drivers/clk/microchip/clk-mpfs.c (revision 05d27090b6dc88bce71a608d1271536e582b73d1)
1635e5e73SDaire McNamara // SPDX-License-Identifier: GPL-2.0-only
2635e5e73SDaire McNamara /*
3635e5e73SDaire McNamara  * Daire McNamara,<daire.mcnamara@microchip.com>
4635e5e73SDaire McNamara  * Copyright (C) 2020 Microchip Technology Inc.  All rights reserved.
5635e5e73SDaire McNamara  */
6635e5e73SDaire McNamara #include <linux/clk-provider.h>
7635e5e73SDaire McNamara #include <linux/io.h>
8635e5e73SDaire McNamara #include <linux/module.h>
9635e5e73SDaire McNamara #include <linux/platform_device.h>
10635e5e73SDaire McNamara #include <linux/slab.h>
11635e5e73SDaire McNamara #include <dt-bindings/clock/microchip,mpfs-clock.h>
12635e5e73SDaire McNamara 
13635e5e73SDaire McNamara /* address offset of control registers */
14445c2da8SConor Dooley #define REG_MSSPLL_REF_CR	0x08u
15445c2da8SConor Dooley #define REG_MSSPLL_POSTDIV_CR	0x10u
16445c2da8SConor Dooley #define REG_MSSPLL_SSCG_2_CR	0x2Cu
17635e5e73SDaire McNamara #define REG_CLOCK_CONFIG_CR	0x08u
181c6a7ea3SConor Dooley #define REG_RTC_CLOCK_CR	0x0Cu
19635e5e73SDaire McNamara #define REG_SUBBLK_CLOCK_CR	0x84u
20635e5e73SDaire McNamara #define REG_SUBBLK_RESET_CR	0x88u
21635e5e73SDaire McNamara 
22445c2da8SConor Dooley #define MSSPLL_FBDIV_SHIFT	0x00u
23445c2da8SConor Dooley #define MSSPLL_FBDIV_WIDTH	0x0Cu
24445c2da8SConor Dooley #define MSSPLL_REFDIV_SHIFT	0x08u
25445c2da8SConor Dooley #define MSSPLL_REFDIV_WIDTH	0x06u
26445c2da8SConor Dooley #define MSSPLL_POSTDIV_SHIFT	0x08u
27445c2da8SConor Dooley #define MSSPLL_POSTDIV_WIDTH	0x07u
28445c2da8SConor Dooley #define MSSPLL_FIXED_DIV	4u
29445c2da8SConor Dooley 
30635e5e73SDaire McNamara struct mpfs_clock_data {
31635e5e73SDaire McNamara 	void __iomem *base;
32445c2da8SConor Dooley 	void __iomem *msspll_base;
33635e5e73SDaire McNamara 	struct clk_hw_onecell_data hw_data;
34635e5e73SDaire McNamara };
35635e5e73SDaire McNamara 
36445c2da8SConor Dooley struct mpfs_msspll_hw_clock {
37445c2da8SConor Dooley 	void __iomem *base;
38445c2da8SConor Dooley 	unsigned int id;
39445c2da8SConor Dooley 	u32 reg_offset;
40445c2da8SConor Dooley 	u32 shift;
41445c2da8SConor Dooley 	u32 width;
42445c2da8SConor Dooley 	u32 flags;
43445c2da8SConor Dooley 	struct clk_hw hw;
44445c2da8SConor Dooley 	struct clk_init_data init;
45445c2da8SConor Dooley };
46445c2da8SConor Dooley 
47445c2da8SConor Dooley #define to_mpfs_msspll_clk(_hw) container_of(_hw, struct mpfs_msspll_hw_clock, hw)
48445c2da8SConor Dooley 
49635e5e73SDaire McNamara struct mpfs_cfg_clock {
50635e5e73SDaire McNamara 	const struct clk_div_table *table;
51635e5e73SDaire McNamara 	unsigned int id;
52445c2da8SConor Dooley 	u32 reg_offset;
53635e5e73SDaire McNamara 	u8 shift;
54635e5e73SDaire McNamara 	u8 width;
55445c2da8SConor Dooley 	u8 flags;
56635e5e73SDaire McNamara };
57635e5e73SDaire McNamara 
58635e5e73SDaire McNamara struct mpfs_cfg_hw_clock {
59635e5e73SDaire McNamara 	struct mpfs_cfg_clock cfg;
60635e5e73SDaire McNamara 	void __iomem *sys_base;
61635e5e73SDaire McNamara 	struct clk_hw hw;
62635e5e73SDaire McNamara 	struct clk_init_data init;
63635e5e73SDaire McNamara };
64635e5e73SDaire McNamara 
65635e5e73SDaire McNamara #define to_mpfs_cfg_clk(_hw) container_of(_hw, struct mpfs_cfg_hw_clock, hw)
66635e5e73SDaire McNamara 
67635e5e73SDaire McNamara struct mpfs_periph_clock {
68635e5e73SDaire McNamara 	unsigned int id;
69635e5e73SDaire McNamara 	u8 shift;
70635e5e73SDaire McNamara };
71635e5e73SDaire McNamara 
72635e5e73SDaire McNamara struct mpfs_periph_hw_clock {
73635e5e73SDaire McNamara 	struct mpfs_periph_clock periph;
74635e5e73SDaire McNamara 	void __iomem *sys_base;
75635e5e73SDaire McNamara 	struct clk_hw hw;
76635e5e73SDaire McNamara };
77635e5e73SDaire McNamara 
78635e5e73SDaire McNamara #define to_mpfs_periph_clk(_hw) container_of(_hw, struct mpfs_periph_hw_clock, hw)
79635e5e73SDaire McNamara 
80635e5e73SDaire McNamara /*
81635e5e73SDaire McNamara  * mpfs_clk_lock prevents anything else from writing to the
82635e5e73SDaire McNamara  * mpfs clk block while a software locked register is being written.
83635e5e73SDaire McNamara  */
84635e5e73SDaire McNamara static DEFINE_SPINLOCK(mpfs_clk_lock);
85635e5e73SDaire McNamara 
86445c2da8SConor Dooley static const struct clk_parent_data mpfs_ext_ref[] = {
87635e5e73SDaire McNamara 	{ .index = 0 },
88635e5e73SDaire McNamara };
89635e5e73SDaire McNamara 
90635e5e73SDaire McNamara static const struct clk_div_table mpfs_div_cpu_axi_table[] = {
91635e5e73SDaire McNamara 	{ 0, 1 }, { 1, 2 }, { 2, 4 }, { 3, 8 },
92635e5e73SDaire McNamara 	{ 0, 0 }
93635e5e73SDaire McNamara };
94635e5e73SDaire McNamara 
95635e5e73SDaire McNamara static const struct clk_div_table mpfs_div_ahb_table[] = {
96635e5e73SDaire McNamara 	{ 1, 2 }, { 2, 4}, { 3, 8 },
97635e5e73SDaire McNamara 	{ 0, 0 }
98635e5e73SDaire McNamara };
99635e5e73SDaire McNamara 
1001c6a7ea3SConor Dooley /*
1011c6a7ea3SConor Dooley  * The only two supported reference clock frequencies for the PolarFire SoC are
1021c6a7ea3SConor Dooley  * 100 and 125 MHz, as the rtc reference is required to be 1 MHz.
1031c6a7ea3SConor Dooley  * It therefore only needs to have divider table entries corresponding to
1041c6a7ea3SConor Dooley  * divide by 100 and 125.
1051c6a7ea3SConor Dooley  */
1061c6a7ea3SConor Dooley static const struct clk_div_table mpfs_div_rtcref_table[] = {
1071c6a7ea3SConor Dooley 	{ 100, 100 }, { 125, 125 },
1081c6a7ea3SConor Dooley 	{ 0, 0 }
1091c6a7ea3SConor Dooley };
1101c6a7ea3SConor Dooley 
111445c2da8SConor Dooley static unsigned long mpfs_clk_msspll_recalc_rate(struct clk_hw *hw, unsigned long prate)
112445c2da8SConor Dooley {
113445c2da8SConor Dooley 	struct mpfs_msspll_hw_clock *msspll_hw = to_mpfs_msspll_clk(hw);
114445c2da8SConor Dooley 	void __iomem *mult_addr = msspll_hw->base + msspll_hw->reg_offset;
115445c2da8SConor Dooley 	void __iomem *ref_div_addr = msspll_hw->base + REG_MSSPLL_REF_CR;
116445c2da8SConor Dooley 	void __iomem *postdiv_addr = msspll_hw->base + REG_MSSPLL_POSTDIV_CR;
117445c2da8SConor Dooley 	u32 mult, ref_div, postdiv;
118445c2da8SConor Dooley 
119445c2da8SConor Dooley 	mult = readl_relaxed(mult_addr) >> MSSPLL_FBDIV_SHIFT;
120445c2da8SConor Dooley 	mult &= clk_div_mask(MSSPLL_FBDIV_WIDTH);
121445c2da8SConor Dooley 	ref_div = readl_relaxed(ref_div_addr) >> MSSPLL_REFDIV_SHIFT;
122445c2da8SConor Dooley 	ref_div &= clk_div_mask(MSSPLL_REFDIV_WIDTH);
123445c2da8SConor Dooley 	postdiv = readl_relaxed(postdiv_addr) >> MSSPLL_POSTDIV_SHIFT;
124445c2da8SConor Dooley 	postdiv &= clk_div_mask(MSSPLL_POSTDIV_WIDTH);
125445c2da8SConor Dooley 
126445c2da8SConor Dooley 	return prate * mult / (ref_div * MSSPLL_FIXED_DIV * postdiv);
127445c2da8SConor Dooley }
128445c2da8SConor Dooley 
129445c2da8SConor Dooley static const struct clk_ops mpfs_clk_msspll_ops = {
130445c2da8SConor Dooley 	.recalc_rate = mpfs_clk_msspll_recalc_rate,
131445c2da8SConor Dooley };
132445c2da8SConor Dooley 
133445c2da8SConor Dooley #define CLK_PLL(_id, _name, _parent, _shift, _width, _flags, _offset) {			\
134445c2da8SConor Dooley 	.id = _id,									\
135445c2da8SConor Dooley 	.shift = _shift,								\
136445c2da8SConor Dooley 	.width = _width,								\
137445c2da8SConor Dooley 	.reg_offset = _offset,								\
138445c2da8SConor Dooley 	.flags = _flags,								\
139445c2da8SConor Dooley 	.hw.init = CLK_HW_INIT_PARENTS_DATA(_name, _parent, &mpfs_clk_msspll_ops, 0),	\
140445c2da8SConor Dooley }
141445c2da8SConor Dooley 
142445c2da8SConor Dooley static struct mpfs_msspll_hw_clock mpfs_msspll_clks[] = {
143445c2da8SConor Dooley 	CLK_PLL(CLK_MSSPLL, "clk_msspll", mpfs_ext_ref, MSSPLL_FBDIV_SHIFT,
144445c2da8SConor Dooley 		MSSPLL_FBDIV_WIDTH, 0, REG_MSSPLL_SSCG_2_CR),
145445c2da8SConor Dooley };
146445c2da8SConor Dooley 
147445c2da8SConor Dooley static int mpfs_clk_register_msspll(struct device *dev, struct mpfs_msspll_hw_clock *msspll_hw,
148445c2da8SConor Dooley 				    void __iomem *base)
149445c2da8SConor Dooley {
150445c2da8SConor Dooley 	msspll_hw->base = base;
151445c2da8SConor Dooley 
152445c2da8SConor Dooley 	return devm_clk_hw_register(dev, &msspll_hw->hw);
153445c2da8SConor Dooley }
154445c2da8SConor Dooley 
155445c2da8SConor Dooley static int mpfs_clk_register_mssplls(struct device *dev, struct mpfs_msspll_hw_clock *msspll_hws,
156445c2da8SConor Dooley 				     unsigned int num_clks, struct mpfs_clock_data *data)
157445c2da8SConor Dooley {
158445c2da8SConor Dooley 	void __iomem *base = data->msspll_base;
159445c2da8SConor Dooley 	unsigned int i;
160445c2da8SConor Dooley 	int ret;
161445c2da8SConor Dooley 
162445c2da8SConor Dooley 	for (i = 0; i < num_clks; i++) {
163445c2da8SConor Dooley 		struct mpfs_msspll_hw_clock *msspll_hw = &msspll_hws[i];
164445c2da8SConor Dooley 
165445c2da8SConor Dooley 		ret = mpfs_clk_register_msspll(dev, msspll_hw, base);
166445c2da8SConor Dooley 		if (ret)
167445c2da8SConor Dooley 			return dev_err_probe(dev, ret, "failed to register msspll id: %d\n",
168445c2da8SConor Dooley 					     CLK_MSSPLL);
169445c2da8SConor Dooley 
170445c2da8SConor Dooley 		data->hw_data.hws[msspll_hw->id] = &msspll_hw->hw;
171445c2da8SConor Dooley 	}
172445c2da8SConor Dooley 
173445c2da8SConor Dooley 	return 0;
174445c2da8SConor Dooley }
175445c2da8SConor Dooley 
176445c2da8SConor Dooley /*
177445c2da8SConor Dooley  * "CFG" clocks
178445c2da8SConor Dooley  */
179445c2da8SConor Dooley 
180635e5e73SDaire McNamara static unsigned long mpfs_cfg_clk_recalc_rate(struct clk_hw *hw, unsigned long prate)
181635e5e73SDaire McNamara {
182635e5e73SDaire McNamara 	struct mpfs_cfg_hw_clock *cfg_hw = to_mpfs_cfg_clk(hw);
183635e5e73SDaire McNamara 	struct mpfs_cfg_clock *cfg = &cfg_hw->cfg;
184635e5e73SDaire McNamara 	void __iomem *base_addr = cfg_hw->sys_base;
185635e5e73SDaire McNamara 	u32 val;
186635e5e73SDaire McNamara 
187445c2da8SConor Dooley 	val = readl_relaxed(base_addr + cfg->reg_offset) >> cfg->shift;
188635e5e73SDaire McNamara 	val &= clk_div_mask(cfg->width);
189635e5e73SDaire McNamara 
190445c2da8SConor Dooley 	return divider_recalc_rate(hw, prate, val, cfg->table, cfg->flags, cfg->width);
191635e5e73SDaire McNamara }
192635e5e73SDaire McNamara 
193635e5e73SDaire McNamara static long mpfs_cfg_clk_round_rate(struct clk_hw *hw, unsigned long rate, unsigned long *prate)
194635e5e73SDaire McNamara {
195635e5e73SDaire McNamara 	struct mpfs_cfg_hw_clock *cfg_hw = to_mpfs_cfg_clk(hw);
196635e5e73SDaire McNamara 	struct mpfs_cfg_clock *cfg = &cfg_hw->cfg;
197635e5e73SDaire McNamara 
198635e5e73SDaire McNamara 	return divider_round_rate(hw, rate, prate, cfg->table, cfg->width, 0);
199635e5e73SDaire McNamara }
200635e5e73SDaire McNamara 
201635e5e73SDaire McNamara static int mpfs_cfg_clk_set_rate(struct clk_hw *hw, unsigned long rate, unsigned long prate)
202635e5e73SDaire McNamara {
203635e5e73SDaire McNamara 	struct mpfs_cfg_hw_clock *cfg_hw = to_mpfs_cfg_clk(hw);
204635e5e73SDaire McNamara 	struct mpfs_cfg_clock *cfg = &cfg_hw->cfg;
205635e5e73SDaire McNamara 	void __iomem *base_addr = cfg_hw->sys_base;
206635e5e73SDaire McNamara 	unsigned long flags;
207635e5e73SDaire McNamara 	u32 val;
208635e5e73SDaire McNamara 	int divider_setting;
209635e5e73SDaire McNamara 
210635e5e73SDaire McNamara 	divider_setting = divider_get_val(rate, prate, cfg->table, cfg->width, 0);
211635e5e73SDaire McNamara 
212635e5e73SDaire McNamara 	if (divider_setting < 0)
213635e5e73SDaire McNamara 		return divider_setting;
214635e5e73SDaire McNamara 
215635e5e73SDaire McNamara 	spin_lock_irqsave(&mpfs_clk_lock, flags);
216445c2da8SConor Dooley 	val = readl_relaxed(base_addr + cfg->reg_offset);
217635e5e73SDaire McNamara 	val &= ~(clk_div_mask(cfg->width) << cfg_hw->cfg.shift);
218635e5e73SDaire McNamara 	val |= divider_setting << cfg->shift;
219445c2da8SConor Dooley 	writel_relaxed(val, base_addr + cfg->reg_offset);
220635e5e73SDaire McNamara 
221635e5e73SDaire McNamara 	spin_unlock_irqrestore(&mpfs_clk_lock, flags);
222635e5e73SDaire McNamara 
223635e5e73SDaire McNamara 	return 0;
224635e5e73SDaire McNamara }
225635e5e73SDaire McNamara 
226635e5e73SDaire McNamara static const struct clk_ops mpfs_clk_cfg_ops = {
227635e5e73SDaire McNamara 	.recalc_rate = mpfs_cfg_clk_recalc_rate,
228635e5e73SDaire McNamara 	.round_rate = mpfs_cfg_clk_round_rate,
229635e5e73SDaire McNamara 	.set_rate = mpfs_cfg_clk_set_rate,
230635e5e73SDaire McNamara };
231635e5e73SDaire McNamara 
232445c2da8SConor Dooley #define CLK_CFG(_id, _name, _parent, _shift, _width, _table, _flags, _offset) {		\
233635e5e73SDaire McNamara 	.cfg.id = _id,									\
234635e5e73SDaire McNamara 	.cfg.shift = _shift,								\
235635e5e73SDaire McNamara 	.cfg.width = _width,								\
236635e5e73SDaire McNamara 	.cfg.table = _table,								\
237445c2da8SConor Dooley 	.cfg.reg_offset = _offset,							\
238445c2da8SConor Dooley 	.cfg.flags = _flags,								\
239445c2da8SConor Dooley 	.hw.init = CLK_HW_INIT(_name, _parent, &mpfs_clk_cfg_ops, 0),			\
240635e5e73SDaire McNamara }
241635e5e73SDaire McNamara 
2425da39ac5SConor Dooley #define CLK_CPU_OFFSET		0u
2435da39ac5SConor Dooley #define CLK_AXI_OFFSET		1u
2445da39ac5SConor Dooley #define CLK_AHB_OFFSET		2u
2455da39ac5SConor Dooley #define CLK_RTCREF_OFFSET	3u
2465da39ac5SConor Dooley 
247635e5e73SDaire McNamara static struct mpfs_cfg_hw_clock mpfs_cfg_clks[] = {
248445c2da8SConor Dooley 	CLK_CFG(CLK_CPU, "clk_cpu", "clk_msspll", 0, 2, mpfs_div_cpu_axi_table, 0,
249445c2da8SConor Dooley 		REG_CLOCK_CONFIG_CR),
250445c2da8SConor Dooley 	CLK_CFG(CLK_AXI, "clk_axi", "clk_msspll", 2, 2, mpfs_div_cpu_axi_table, 0,
251445c2da8SConor Dooley 		REG_CLOCK_CONFIG_CR),
252445c2da8SConor Dooley 	CLK_CFG(CLK_AHB, "clk_ahb", "clk_msspll", 4, 2, mpfs_div_ahb_table, 0,
253445c2da8SConor Dooley 		REG_CLOCK_CONFIG_CR),
2541c6a7ea3SConor Dooley 	{
2551c6a7ea3SConor Dooley 		.cfg.id = CLK_RTCREF,
2561c6a7ea3SConor Dooley 		.cfg.shift = 0,
2571c6a7ea3SConor Dooley 		.cfg.width = 12,
2581c6a7ea3SConor Dooley 		.cfg.table = mpfs_div_rtcref_table,
2591c6a7ea3SConor Dooley 		.cfg.reg_offset = REG_RTC_CLOCK_CR,
2601c6a7ea3SConor Dooley 		.cfg.flags = CLK_DIVIDER_ONE_BASED,
2611c6a7ea3SConor Dooley 		.hw.init =
2621c6a7ea3SConor Dooley 			CLK_HW_INIT_PARENTS_DATA("clk_rtcref", mpfs_ext_ref, &mpfs_clk_cfg_ops, 0),
2631c6a7ea3SConor Dooley 	}
264635e5e73SDaire McNamara };
265635e5e73SDaire McNamara 
266635e5e73SDaire McNamara static int mpfs_clk_register_cfg(struct device *dev, struct mpfs_cfg_hw_clock *cfg_hw,
267635e5e73SDaire McNamara 				 void __iomem *sys_base)
268635e5e73SDaire McNamara {
269635e5e73SDaire McNamara 	cfg_hw->sys_base = sys_base;
270635e5e73SDaire McNamara 
271635e5e73SDaire McNamara 	return devm_clk_hw_register(dev, &cfg_hw->hw);
272635e5e73SDaire McNamara }
273635e5e73SDaire McNamara 
274635e5e73SDaire McNamara static int mpfs_clk_register_cfgs(struct device *dev, struct mpfs_cfg_hw_clock *cfg_hws,
275635e5e73SDaire McNamara 				  unsigned int num_clks, struct mpfs_clock_data *data)
276635e5e73SDaire McNamara {
277635e5e73SDaire McNamara 	void __iomem *sys_base = data->base;
278635e5e73SDaire McNamara 	unsigned int i, id;
279635e5e73SDaire McNamara 	int ret;
280635e5e73SDaire McNamara 
281635e5e73SDaire McNamara 	for (i = 0; i < num_clks; i++) {
282635e5e73SDaire McNamara 		struct mpfs_cfg_hw_clock *cfg_hw = &cfg_hws[i];
283635e5e73SDaire McNamara 
284635e5e73SDaire McNamara 		ret = mpfs_clk_register_cfg(dev, cfg_hw, sys_base);
285635e5e73SDaire McNamara 		if (ret)
286635e5e73SDaire McNamara 			return dev_err_probe(dev, ret, "failed to register clock id: %d\n",
287635e5e73SDaire McNamara 					     cfg_hw->cfg.id);
288635e5e73SDaire McNamara 
289445c2da8SConor Dooley 		id = cfg_hw->cfg.id;
290635e5e73SDaire McNamara 		data->hw_data.hws[id] = &cfg_hw->hw;
291635e5e73SDaire McNamara 	}
292635e5e73SDaire McNamara 
293635e5e73SDaire McNamara 	return 0;
294635e5e73SDaire McNamara }
295635e5e73SDaire McNamara 
296445c2da8SConor Dooley /*
297445c2da8SConor Dooley  * peripheral clocks - devices connected to axi or ahb buses.
298445c2da8SConor Dooley  */
299445c2da8SConor Dooley 
300635e5e73SDaire McNamara static int mpfs_periph_clk_enable(struct clk_hw *hw)
301635e5e73SDaire McNamara {
302635e5e73SDaire McNamara 	struct mpfs_periph_hw_clock *periph_hw = to_mpfs_periph_clk(hw);
303635e5e73SDaire McNamara 	struct mpfs_periph_clock *periph = &periph_hw->periph;
304635e5e73SDaire McNamara 	void __iomem *base_addr = periph_hw->sys_base;
305635e5e73SDaire McNamara 	u32 reg, val;
306635e5e73SDaire McNamara 	unsigned long flags;
307635e5e73SDaire McNamara 
308635e5e73SDaire McNamara 	spin_lock_irqsave(&mpfs_clk_lock, flags);
309635e5e73SDaire McNamara 
310635e5e73SDaire McNamara 	reg = readl_relaxed(base_addr + REG_SUBBLK_RESET_CR);
311635e5e73SDaire McNamara 	val = reg & ~(1u << periph->shift);
312635e5e73SDaire McNamara 	writel_relaxed(val, base_addr + REG_SUBBLK_RESET_CR);
313635e5e73SDaire McNamara 
314635e5e73SDaire McNamara 	reg = readl_relaxed(base_addr + REG_SUBBLK_CLOCK_CR);
315635e5e73SDaire McNamara 	val = reg | (1u << periph->shift);
316635e5e73SDaire McNamara 	writel_relaxed(val, base_addr + REG_SUBBLK_CLOCK_CR);
317635e5e73SDaire McNamara 
318635e5e73SDaire McNamara 	spin_unlock_irqrestore(&mpfs_clk_lock, flags);
319635e5e73SDaire McNamara 
320635e5e73SDaire McNamara 	return 0;
321635e5e73SDaire McNamara }
322635e5e73SDaire McNamara 
323635e5e73SDaire McNamara static void mpfs_periph_clk_disable(struct clk_hw *hw)
324635e5e73SDaire McNamara {
325635e5e73SDaire McNamara 	struct mpfs_periph_hw_clock *periph_hw = to_mpfs_periph_clk(hw);
326635e5e73SDaire McNamara 	struct mpfs_periph_clock *periph = &periph_hw->periph;
327635e5e73SDaire McNamara 	void __iomem *base_addr = periph_hw->sys_base;
328635e5e73SDaire McNamara 	u32 reg, val;
329635e5e73SDaire McNamara 	unsigned long flags;
330635e5e73SDaire McNamara 
331635e5e73SDaire McNamara 	spin_lock_irqsave(&mpfs_clk_lock, flags);
332635e5e73SDaire McNamara 
333635e5e73SDaire McNamara 	reg = readl_relaxed(base_addr + REG_SUBBLK_CLOCK_CR);
334635e5e73SDaire McNamara 	val = reg & ~(1u << periph->shift);
335635e5e73SDaire McNamara 	writel_relaxed(val, base_addr + REG_SUBBLK_CLOCK_CR);
336635e5e73SDaire McNamara 
337635e5e73SDaire McNamara 	spin_unlock_irqrestore(&mpfs_clk_lock, flags);
338635e5e73SDaire McNamara }
339635e5e73SDaire McNamara 
340635e5e73SDaire McNamara static int mpfs_periph_clk_is_enabled(struct clk_hw *hw)
341635e5e73SDaire McNamara {
342635e5e73SDaire McNamara 	struct mpfs_periph_hw_clock *periph_hw = to_mpfs_periph_clk(hw);
343635e5e73SDaire McNamara 	struct mpfs_periph_clock *periph = &periph_hw->periph;
344635e5e73SDaire McNamara 	void __iomem *base_addr = periph_hw->sys_base;
345635e5e73SDaire McNamara 	u32 reg;
346635e5e73SDaire McNamara 
347635e5e73SDaire McNamara 	reg = readl_relaxed(base_addr + REG_SUBBLK_RESET_CR);
348635e5e73SDaire McNamara 	if ((reg & (1u << periph->shift)) == 0u) {
349635e5e73SDaire McNamara 		reg = readl_relaxed(base_addr + REG_SUBBLK_CLOCK_CR);
350635e5e73SDaire McNamara 		if (reg & (1u << periph->shift))
351635e5e73SDaire McNamara 			return 1;
352635e5e73SDaire McNamara 	}
353635e5e73SDaire McNamara 
354635e5e73SDaire McNamara 	return 0;
355635e5e73SDaire McNamara }
356635e5e73SDaire McNamara 
357635e5e73SDaire McNamara static const struct clk_ops mpfs_periph_clk_ops = {
358635e5e73SDaire McNamara 	.enable = mpfs_periph_clk_enable,
359635e5e73SDaire McNamara 	.disable = mpfs_periph_clk_disable,
360635e5e73SDaire McNamara 	.is_enabled = mpfs_periph_clk_is_enabled,
361635e5e73SDaire McNamara };
362635e5e73SDaire McNamara 
363635e5e73SDaire McNamara #define CLK_PERIPH(_id, _name, _parent, _shift, _flags) {			\
364635e5e73SDaire McNamara 	.periph.id = _id,							\
365635e5e73SDaire McNamara 	.periph.shift = _shift,							\
366635e5e73SDaire McNamara 	.hw.init = CLK_HW_INIT_HW(_name, _parent, &mpfs_periph_clk_ops,		\
367635e5e73SDaire McNamara 				  _flags),					\
368635e5e73SDaire McNamara }
369635e5e73SDaire McNamara 
3705da39ac5SConor Dooley #define PARENT_CLK(PARENT) (&mpfs_cfg_clks[CLK_##PARENT##_OFFSET].hw)
371635e5e73SDaire McNamara 
372635e5e73SDaire McNamara /*
373635e5e73SDaire McNamara  * Critical clocks:
374635e5e73SDaire McNamara  * - CLK_ENVM: reserved by hart software services (hss) superloop monitor/m mode interrupt
375635e5e73SDaire McNamara  *   trap handler
376635e5e73SDaire McNamara  * - CLK_MMUART0: reserved by the hss
377635e5e73SDaire McNamara  * - CLK_DDRC: provides clock to the ddr subsystem
378*05d27090SConor Dooley  * - CLK_RTC: the onboard RTC's AHB bus clock must be kept running as the rtc will stop
379*05d27090SConor Dooley  *   if the AHB interface clock is disabled
380a2438f82SConor Dooley  * - CLK_FICx: these provide the processor side clocks to the "FIC" (Fabric InterConnect)
381a2438f82SConor Dooley  *   clock domain crossers which provide the interface to the FPGA fabric. Disabling them
382a2438f82SConor Dooley  *   causes the FPGA fabric to go into reset.
383a2438f82SConor Dooley  * - CLK_ATHENA: The athena clock is FIC4, which is reserved for the Athena TeraFire.
384635e5e73SDaire McNamara  */
385635e5e73SDaire McNamara 
386635e5e73SDaire McNamara static struct mpfs_periph_hw_clock mpfs_periph_clks[] = {
387635e5e73SDaire McNamara 	CLK_PERIPH(CLK_ENVM, "clk_periph_envm", PARENT_CLK(AHB), 0, CLK_IS_CRITICAL),
388635e5e73SDaire McNamara 	CLK_PERIPH(CLK_MAC0, "clk_periph_mac0", PARENT_CLK(AHB), 1, 0),
389635e5e73SDaire McNamara 	CLK_PERIPH(CLK_MAC1, "clk_periph_mac1", PARENT_CLK(AHB), 2, 0),
390635e5e73SDaire McNamara 	CLK_PERIPH(CLK_MMC, "clk_periph_mmc", PARENT_CLK(AHB), 3, 0),
3911c6a7ea3SConor Dooley 	CLK_PERIPH(CLK_TIMER, "clk_periph_timer", PARENT_CLK(RTCREF), 4, 0),
392635e5e73SDaire McNamara 	CLK_PERIPH(CLK_MMUART0, "clk_periph_mmuart0", PARENT_CLK(AHB), 5, CLK_IS_CRITICAL),
393635e5e73SDaire McNamara 	CLK_PERIPH(CLK_MMUART1, "clk_periph_mmuart1", PARENT_CLK(AHB), 6, 0),
394635e5e73SDaire McNamara 	CLK_PERIPH(CLK_MMUART2, "clk_periph_mmuart2", PARENT_CLK(AHB), 7, 0),
395635e5e73SDaire McNamara 	CLK_PERIPH(CLK_MMUART3, "clk_periph_mmuart3", PARENT_CLK(AHB), 8, 0),
396635e5e73SDaire McNamara 	CLK_PERIPH(CLK_MMUART4, "clk_periph_mmuart4", PARENT_CLK(AHB), 9, 0),
397635e5e73SDaire McNamara 	CLK_PERIPH(CLK_SPI0, "clk_periph_spi0", PARENT_CLK(AHB), 10, 0),
398635e5e73SDaire McNamara 	CLK_PERIPH(CLK_SPI1, "clk_periph_spi1", PARENT_CLK(AHB), 11, 0),
399635e5e73SDaire McNamara 	CLK_PERIPH(CLK_I2C0, "clk_periph_i2c0", PARENT_CLK(AHB), 12, 0),
400635e5e73SDaire McNamara 	CLK_PERIPH(CLK_I2C1, "clk_periph_i2c1", PARENT_CLK(AHB), 13, 0),
401635e5e73SDaire McNamara 	CLK_PERIPH(CLK_CAN0, "clk_periph_can0", PARENT_CLK(AHB), 14, 0),
402635e5e73SDaire McNamara 	CLK_PERIPH(CLK_CAN1, "clk_periph_can1", PARENT_CLK(AHB), 15, 0),
403635e5e73SDaire McNamara 	CLK_PERIPH(CLK_USB, "clk_periph_usb", PARENT_CLK(AHB), 16, 0),
404*05d27090SConor Dooley 	CLK_PERIPH(CLK_RTC, "clk_periph_rtc", PARENT_CLK(AHB), 18, CLK_IS_CRITICAL),
405635e5e73SDaire McNamara 	CLK_PERIPH(CLK_QSPI, "clk_periph_qspi", PARENT_CLK(AHB), 19, 0),
406635e5e73SDaire McNamara 	CLK_PERIPH(CLK_GPIO0, "clk_periph_gpio0", PARENT_CLK(AHB), 20, 0),
407635e5e73SDaire McNamara 	CLK_PERIPH(CLK_GPIO1, "clk_periph_gpio1", PARENT_CLK(AHB), 21, 0),
408635e5e73SDaire McNamara 	CLK_PERIPH(CLK_GPIO2, "clk_periph_gpio2", PARENT_CLK(AHB), 22, 0),
409635e5e73SDaire McNamara 	CLK_PERIPH(CLK_DDRC, "clk_periph_ddrc", PARENT_CLK(AHB), 23, CLK_IS_CRITICAL),
4108f9fb2abSConor Dooley 	CLK_PERIPH(CLK_FIC0, "clk_periph_fic0", PARENT_CLK(AXI), 24, CLK_IS_CRITICAL),
4118f9fb2abSConor Dooley 	CLK_PERIPH(CLK_FIC1, "clk_periph_fic1", PARENT_CLK(AXI), 25, CLK_IS_CRITICAL),
4128f9fb2abSConor Dooley 	CLK_PERIPH(CLK_FIC2, "clk_periph_fic2", PARENT_CLK(AXI), 26, CLK_IS_CRITICAL),
4138f9fb2abSConor Dooley 	CLK_PERIPH(CLK_FIC3, "clk_periph_fic3", PARENT_CLK(AXI), 27, CLK_IS_CRITICAL),
414a2438f82SConor Dooley 	CLK_PERIPH(CLK_ATHENA, "clk_periph_athena", PARENT_CLK(AXI), 28, CLK_IS_CRITICAL),
415635e5e73SDaire McNamara 	CLK_PERIPH(CLK_CFM, "clk_periph_cfm", PARENT_CLK(AHB), 29, 0),
416635e5e73SDaire McNamara };
417635e5e73SDaire McNamara 
418635e5e73SDaire McNamara static int mpfs_clk_register_periph(struct device *dev, struct mpfs_periph_hw_clock *periph_hw,
419635e5e73SDaire McNamara 				    void __iomem *sys_base)
420635e5e73SDaire McNamara {
421635e5e73SDaire McNamara 	periph_hw->sys_base = sys_base;
422635e5e73SDaire McNamara 
423635e5e73SDaire McNamara 	return devm_clk_hw_register(dev, &periph_hw->hw);
424635e5e73SDaire McNamara }
425635e5e73SDaire McNamara 
426635e5e73SDaire McNamara static int mpfs_clk_register_periphs(struct device *dev, struct mpfs_periph_hw_clock *periph_hws,
427635e5e73SDaire McNamara 				     int num_clks, struct mpfs_clock_data *data)
428635e5e73SDaire McNamara {
429635e5e73SDaire McNamara 	void __iomem *sys_base = data->base;
430635e5e73SDaire McNamara 	unsigned int i, id;
431635e5e73SDaire McNamara 	int ret;
432635e5e73SDaire McNamara 
433635e5e73SDaire McNamara 	for (i = 0; i < num_clks; i++) {
434635e5e73SDaire McNamara 		struct mpfs_periph_hw_clock *periph_hw = &periph_hws[i];
435635e5e73SDaire McNamara 
436635e5e73SDaire McNamara 		ret = mpfs_clk_register_periph(dev, periph_hw, sys_base);
437635e5e73SDaire McNamara 		if (ret)
438635e5e73SDaire McNamara 			return dev_err_probe(dev, ret, "failed to register clock id: %d\n",
439635e5e73SDaire McNamara 					     periph_hw->periph.id);
440635e5e73SDaire McNamara 
441635e5e73SDaire McNamara 		id = periph_hws[i].periph.id;
442635e5e73SDaire McNamara 		data->hw_data.hws[id] = &periph_hw->hw;
443635e5e73SDaire McNamara 	}
444635e5e73SDaire McNamara 
445635e5e73SDaire McNamara 	return 0;
446635e5e73SDaire McNamara }
447635e5e73SDaire McNamara 
448635e5e73SDaire McNamara static int mpfs_clk_probe(struct platform_device *pdev)
449635e5e73SDaire McNamara {
450635e5e73SDaire McNamara 	struct device *dev = &pdev->dev;
451635e5e73SDaire McNamara 	struct mpfs_clock_data *clk_data;
452635e5e73SDaire McNamara 	unsigned int num_clks;
453635e5e73SDaire McNamara 	int ret;
454635e5e73SDaire McNamara 
455445c2da8SConor Dooley 	/* CLK_RESERVED is not part of clock arrays, so add 1 */
456445c2da8SConor Dooley 	num_clks = ARRAY_SIZE(mpfs_msspll_clks) + ARRAY_SIZE(mpfs_cfg_clks)
457445c2da8SConor Dooley 		   + ARRAY_SIZE(mpfs_periph_clks) + 1;
458635e5e73SDaire McNamara 
459635e5e73SDaire McNamara 	clk_data = devm_kzalloc(dev, struct_size(clk_data, hw_data.hws, num_clks), GFP_KERNEL);
460635e5e73SDaire McNamara 	if (!clk_data)
461635e5e73SDaire McNamara 		return -ENOMEM;
462635e5e73SDaire McNamara 
463635e5e73SDaire McNamara 	clk_data->base = devm_platform_ioremap_resource(pdev, 0);
464635e5e73SDaire McNamara 	if (IS_ERR(clk_data->base))
465635e5e73SDaire McNamara 		return PTR_ERR(clk_data->base);
466635e5e73SDaire McNamara 
467445c2da8SConor Dooley 	clk_data->msspll_base = devm_platform_ioremap_resource(pdev, 1);
468445c2da8SConor Dooley 	if (IS_ERR(clk_data->msspll_base))
469445c2da8SConor Dooley 		return PTR_ERR(clk_data->msspll_base);
470445c2da8SConor Dooley 
471635e5e73SDaire McNamara 	clk_data->hw_data.num = num_clks;
472635e5e73SDaire McNamara 
473445c2da8SConor Dooley 	ret = mpfs_clk_register_mssplls(dev, mpfs_msspll_clks, ARRAY_SIZE(mpfs_msspll_clks),
474445c2da8SConor Dooley 					clk_data);
475445c2da8SConor Dooley 	if (ret)
476445c2da8SConor Dooley 		return ret;
477445c2da8SConor Dooley 
478635e5e73SDaire McNamara 	ret = mpfs_clk_register_cfgs(dev, mpfs_cfg_clks, ARRAY_SIZE(mpfs_cfg_clks), clk_data);
479635e5e73SDaire McNamara 	if (ret)
480635e5e73SDaire McNamara 		return ret;
481635e5e73SDaire McNamara 
482635e5e73SDaire McNamara 	ret = mpfs_clk_register_periphs(dev, mpfs_periph_clks, ARRAY_SIZE(mpfs_periph_clks),
483635e5e73SDaire McNamara 					clk_data);
484635e5e73SDaire McNamara 	if (ret)
485635e5e73SDaire McNamara 		return ret;
486635e5e73SDaire McNamara 
487635e5e73SDaire McNamara 	ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, &clk_data->hw_data);
488635e5e73SDaire McNamara 	if (ret)
489635e5e73SDaire McNamara 		return ret;
490635e5e73SDaire McNamara 
491635e5e73SDaire McNamara 	return ret;
492635e5e73SDaire McNamara }
493635e5e73SDaire McNamara 
494635e5e73SDaire McNamara static const struct of_device_id mpfs_clk_of_match_table[] = {
495635e5e73SDaire McNamara 	{ .compatible = "microchip,mpfs-clkcfg", },
496635e5e73SDaire McNamara 	{}
497635e5e73SDaire McNamara };
498635e5e73SDaire McNamara MODULE_DEVICE_TABLE(of, mpfs_clk_match_table);
499635e5e73SDaire McNamara 
500635e5e73SDaire McNamara static struct platform_driver mpfs_clk_driver = {
501635e5e73SDaire McNamara 	.probe = mpfs_clk_probe,
502635e5e73SDaire McNamara 	.driver	= {
503635e5e73SDaire McNamara 		.name = "microchip-mpfs-clkcfg",
504635e5e73SDaire McNamara 		.of_match_table = mpfs_clk_of_match_table,
505635e5e73SDaire McNamara 	},
506635e5e73SDaire McNamara };
507635e5e73SDaire McNamara 
508635e5e73SDaire McNamara static int __init clk_mpfs_init(void)
509635e5e73SDaire McNamara {
510635e5e73SDaire McNamara 	return platform_driver_register(&mpfs_clk_driver);
511635e5e73SDaire McNamara }
512635e5e73SDaire McNamara core_initcall(clk_mpfs_init);
513635e5e73SDaire McNamara 
514635e5e73SDaire McNamara static void __exit clk_mpfs_exit(void)
515635e5e73SDaire McNamara {
516635e5e73SDaire McNamara 	platform_driver_unregister(&mpfs_clk_driver);
517635e5e73SDaire McNamara }
518635e5e73SDaire McNamara module_exit(clk_mpfs_exit);
519635e5e73SDaire McNamara 
520635e5e73SDaire McNamara MODULE_DESCRIPTION("Microchip PolarFire SoC Clock Driver");
521635e5e73SDaire McNamara MODULE_LICENSE("GPL v2");
522