xref: /openbmc/linux/drivers/clk/hisilicon/clk-hi6220-stub.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2c1628a2cSLeo Yan /*
3c1628a2cSLeo Yan  * Hi6220 stub clock driver
4c1628a2cSLeo Yan  *
5c1628a2cSLeo Yan  * Copyright (c) 2015 Hisilicon Limited.
6c1628a2cSLeo Yan  * Copyright (c) 2015 Linaro Limited.
7c1628a2cSLeo Yan  *
8c1628a2cSLeo Yan  * Author: Leo Yan <leo.yan@linaro.org>
9c1628a2cSLeo Yan  */
10c1628a2cSLeo Yan 
11c1628a2cSLeo Yan #include <linux/clk-provider.h>
12c1628a2cSLeo Yan #include <linux/err.h>
13c1628a2cSLeo Yan #include <linux/kernel.h>
14c1628a2cSLeo Yan #include <linux/mfd/syscon.h>
15c1628a2cSLeo Yan #include <linux/mailbox_client.h>
16c1628a2cSLeo Yan #include <linux/of.h>
17*a96cbb14SRob Herring #include <linux/platform_device.h>
18c1628a2cSLeo Yan #include <linux/regmap.h>
19c1628a2cSLeo Yan 
20c1628a2cSLeo Yan /* Stub clocks id */
21c1628a2cSLeo Yan #define HI6220_STUB_ACPU0		0
22c1628a2cSLeo Yan #define HI6220_STUB_ACPU1		1
23c1628a2cSLeo Yan #define HI6220_STUB_GPU			2
24c1628a2cSLeo Yan #define HI6220_STUB_DDR			5
25c1628a2cSLeo Yan 
26c1628a2cSLeo Yan /* Mailbox message */
27c1628a2cSLeo Yan #define HI6220_MBOX_MSG_LEN		8
28c1628a2cSLeo Yan 
29c1628a2cSLeo Yan #define HI6220_MBOX_FREQ		0xA
30c1628a2cSLeo Yan #define HI6220_MBOX_CMD_SET		0x3
31c1628a2cSLeo Yan #define HI6220_MBOX_OBJ_AP		0x0
32c1628a2cSLeo Yan 
33c1628a2cSLeo Yan /* CPU dynamic frequency scaling */
34c1628a2cSLeo Yan #define ACPU_DFS_FREQ_MAX		0x1724
35c1628a2cSLeo Yan #define ACPU_DFS_CUR_FREQ		0x17CC
36c1628a2cSLeo Yan #define ACPU_DFS_FLAG			0x1B30
37c1628a2cSLeo Yan #define ACPU_DFS_FREQ_REQ		0x1B34
38c1628a2cSLeo Yan #define ACPU_DFS_FREQ_LMT		0x1B38
39c1628a2cSLeo Yan #define ACPU_DFS_LOCK_FLAG		0xAEAEAEAE
40c1628a2cSLeo Yan 
41c1628a2cSLeo Yan #define to_stub_clk(hw) container_of(hw, struct hi6220_stub_clk, hw)
42c1628a2cSLeo Yan 
43c1628a2cSLeo Yan struct hi6220_stub_clk {
44c1628a2cSLeo Yan 	u32 id;
45c1628a2cSLeo Yan 
46c1628a2cSLeo Yan 	struct device *dev;
47c1628a2cSLeo Yan 	struct clk_hw hw;
48c1628a2cSLeo Yan 
49c1628a2cSLeo Yan 	struct regmap *dfs_map;
50c1628a2cSLeo Yan 	struct mbox_client cl;
51c1628a2cSLeo Yan 	struct mbox_chan *mbox;
52c1628a2cSLeo Yan };
53c1628a2cSLeo Yan 
54c1628a2cSLeo Yan struct hi6220_mbox_msg {
55c1628a2cSLeo Yan 	unsigned char type;
56c1628a2cSLeo Yan 	unsigned char cmd;
57c1628a2cSLeo Yan 	unsigned char obj;
58c1628a2cSLeo Yan 	unsigned char src;
59c1628a2cSLeo Yan 	unsigned char para[4];
60c1628a2cSLeo Yan };
61c1628a2cSLeo Yan 
62c1628a2cSLeo Yan union hi6220_mbox_data {
63c1628a2cSLeo Yan 	unsigned int data[HI6220_MBOX_MSG_LEN];
64c1628a2cSLeo Yan 	struct hi6220_mbox_msg msg;
65c1628a2cSLeo Yan };
66c1628a2cSLeo Yan 
hi6220_acpu_get_freq(struct hi6220_stub_clk * stub_clk)67c1628a2cSLeo Yan static unsigned int hi6220_acpu_get_freq(struct hi6220_stub_clk *stub_clk)
68c1628a2cSLeo Yan {
69c1628a2cSLeo Yan 	unsigned int freq;
70c1628a2cSLeo Yan 
71c1628a2cSLeo Yan 	regmap_read(stub_clk->dfs_map, ACPU_DFS_CUR_FREQ, &freq);
72c1628a2cSLeo Yan 	return freq;
73c1628a2cSLeo Yan }
74c1628a2cSLeo Yan 
hi6220_acpu_set_freq(struct hi6220_stub_clk * stub_clk,unsigned int freq)75c1628a2cSLeo Yan static int hi6220_acpu_set_freq(struct hi6220_stub_clk *stub_clk,
76c1628a2cSLeo Yan 				unsigned int freq)
77c1628a2cSLeo Yan {
78c1628a2cSLeo Yan 	union hi6220_mbox_data data;
79c1628a2cSLeo Yan 
80c1628a2cSLeo Yan 	/* set the frequency in sram */
81c1628a2cSLeo Yan 	regmap_write(stub_clk->dfs_map, ACPU_DFS_FREQ_REQ, freq);
82c1628a2cSLeo Yan 
83c1628a2cSLeo Yan 	/* compound mailbox message */
84c1628a2cSLeo Yan 	data.msg.type = HI6220_MBOX_FREQ;
85c1628a2cSLeo Yan 	data.msg.cmd  = HI6220_MBOX_CMD_SET;
86c1628a2cSLeo Yan 	data.msg.obj  = HI6220_MBOX_OBJ_AP;
87c1628a2cSLeo Yan 	data.msg.src  = HI6220_MBOX_OBJ_AP;
88c1628a2cSLeo Yan 
89c1628a2cSLeo Yan 	mbox_send_message(stub_clk->mbox, &data);
90c1628a2cSLeo Yan 	return 0;
91c1628a2cSLeo Yan }
92c1628a2cSLeo Yan 
hi6220_acpu_round_freq(struct hi6220_stub_clk * stub_clk,unsigned int freq)93c1628a2cSLeo Yan static int hi6220_acpu_round_freq(struct hi6220_stub_clk *stub_clk,
94c1628a2cSLeo Yan 				  unsigned int freq)
95c1628a2cSLeo Yan {
96c1628a2cSLeo Yan 	unsigned int limit_flag, limit_freq = UINT_MAX;
97c1628a2cSLeo Yan 	unsigned int max_freq;
98c1628a2cSLeo Yan 
99c1628a2cSLeo Yan 	/* check the constrained frequency */
100c1628a2cSLeo Yan 	regmap_read(stub_clk->dfs_map, ACPU_DFS_FLAG, &limit_flag);
101c1628a2cSLeo Yan 	if (limit_flag == ACPU_DFS_LOCK_FLAG)
102c1628a2cSLeo Yan 		regmap_read(stub_clk->dfs_map, ACPU_DFS_FREQ_LMT, &limit_freq);
103c1628a2cSLeo Yan 
104c1628a2cSLeo Yan 	/* check the supported maximum frequency */
105c1628a2cSLeo Yan 	regmap_read(stub_clk->dfs_map, ACPU_DFS_FREQ_MAX, &max_freq);
106c1628a2cSLeo Yan 
107c1628a2cSLeo Yan 	/* calculate the real maximum frequency */
108c1628a2cSLeo Yan 	max_freq = min(max_freq, limit_freq);
109c1628a2cSLeo Yan 
110c1628a2cSLeo Yan 	if (WARN_ON(freq > max_freq))
111c1628a2cSLeo Yan 		freq = max_freq;
112c1628a2cSLeo Yan 
113c1628a2cSLeo Yan 	return freq;
114c1628a2cSLeo Yan }
115c1628a2cSLeo Yan 
hi6220_stub_clk_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)116c1628a2cSLeo Yan static unsigned long hi6220_stub_clk_recalc_rate(struct clk_hw *hw,
117c1628a2cSLeo Yan 		unsigned long parent_rate)
118c1628a2cSLeo Yan {
119c1628a2cSLeo Yan 	u32 rate = 0;
120c1628a2cSLeo Yan 	struct hi6220_stub_clk *stub_clk = to_stub_clk(hw);
121c1628a2cSLeo Yan 
122c1628a2cSLeo Yan 	switch (stub_clk->id) {
123c1628a2cSLeo Yan 	case HI6220_STUB_ACPU0:
124c1628a2cSLeo Yan 		rate = hi6220_acpu_get_freq(stub_clk);
125c1628a2cSLeo Yan 
126c1628a2cSLeo Yan 		/* convert from kHz to Hz */
127c1628a2cSLeo Yan 		rate *= 1000;
128c1628a2cSLeo Yan 		break;
129c1628a2cSLeo Yan 
130c1628a2cSLeo Yan 	default:
131c1628a2cSLeo Yan 		dev_err(stub_clk->dev, "%s: un-supported clock id %d\n",
132c1628a2cSLeo Yan 			__func__, stub_clk->id);
133c1628a2cSLeo Yan 		break;
134c1628a2cSLeo Yan 	}
135c1628a2cSLeo Yan 
136c1628a2cSLeo Yan 	return rate;
137c1628a2cSLeo Yan }
138c1628a2cSLeo Yan 
hi6220_stub_clk_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)139c1628a2cSLeo Yan static int hi6220_stub_clk_set_rate(struct clk_hw *hw, unsigned long rate,
140c1628a2cSLeo Yan 		unsigned long parent_rate)
141c1628a2cSLeo Yan {
142c1628a2cSLeo Yan 	struct hi6220_stub_clk *stub_clk = to_stub_clk(hw);
143c1628a2cSLeo Yan 	unsigned long new_rate = rate / 1000;  /* kHz */
144c1628a2cSLeo Yan 	int ret = 0;
145c1628a2cSLeo Yan 
146c1628a2cSLeo Yan 	switch (stub_clk->id) {
147c1628a2cSLeo Yan 	case HI6220_STUB_ACPU0:
148c1628a2cSLeo Yan 		ret = hi6220_acpu_set_freq(stub_clk, new_rate);
149c1628a2cSLeo Yan 		if (ret < 0)
150c1628a2cSLeo Yan 			return ret;
151c1628a2cSLeo Yan 
152c1628a2cSLeo Yan 		break;
153c1628a2cSLeo Yan 
154c1628a2cSLeo Yan 	default:
155c1628a2cSLeo Yan 		dev_err(stub_clk->dev, "%s: un-supported clock id %d\n",
156c1628a2cSLeo Yan 			__func__, stub_clk->id);
157c1628a2cSLeo Yan 		break;
158c1628a2cSLeo Yan 	}
159c1628a2cSLeo Yan 
160c1628a2cSLeo Yan 	pr_debug("%s: set rate=%ldkHz\n", __func__, new_rate);
161c1628a2cSLeo Yan 	return ret;
162c1628a2cSLeo Yan }
163c1628a2cSLeo Yan 
hi6220_stub_clk_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * parent_rate)164c1628a2cSLeo Yan static long hi6220_stub_clk_round_rate(struct clk_hw *hw, unsigned long rate,
165c1628a2cSLeo Yan 		unsigned long *parent_rate)
166c1628a2cSLeo Yan {
167c1628a2cSLeo Yan 	struct hi6220_stub_clk *stub_clk = to_stub_clk(hw);
168c1628a2cSLeo Yan 	unsigned long new_rate = rate / 1000;  /* kHz */
169c1628a2cSLeo Yan 
170c1628a2cSLeo Yan 	switch (stub_clk->id) {
171c1628a2cSLeo Yan 	case HI6220_STUB_ACPU0:
172c1628a2cSLeo Yan 		new_rate = hi6220_acpu_round_freq(stub_clk, new_rate);
173c1628a2cSLeo Yan 
174c1628a2cSLeo Yan 		/* convert from kHz to Hz */
175c1628a2cSLeo Yan 		new_rate *= 1000;
176c1628a2cSLeo Yan 		break;
177c1628a2cSLeo Yan 
178c1628a2cSLeo Yan 	default:
179c1628a2cSLeo Yan 		dev_err(stub_clk->dev, "%s: un-supported clock id %d\n",
180c1628a2cSLeo Yan 			__func__, stub_clk->id);
181c1628a2cSLeo Yan 		break;
182c1628a2cSLeo Yan 	}
183c1628a2cSLeo Yan 
184c1628a2cSLeo Yan 	return new_rate;
185c1628a2cSLeo Yan }
186c1628a2cSLeo Yan 
187c1628a2cSLeo Yan static const struct clk_ops hi6220_stub_clk_ops = {
188c1628a2cSLeo Yan 	.recalc_rate	= hi6220_stub_clk_recalc_rate,
189c1628a2cSLeo Yan 	.round_rate	= hi6220_stub_clk_round_rate,
190c1628a2cSLeo Yan 	.set_rate	= hi6220_stub_clk_set_rate,
191c1628a2cSLeo Yan };
192c1628a2cSLeo Yan 
hi6220_stub_clk_probe(struct platform_device * pdev)193c1628a2cSLeo Yan static int hi6220_stub_clk_probe(struct platform_device *pdev)
194c1628a2cSLeo Yan {
195c1628a2cSLeo Yan 	struct device *dev = &pdev->dev;
196c1628a2cSLeo Yan 	struct clk_init_data init;
197c1628a2cSLeo Yan 	struct hi6220_stub_clk *stub_clk;
198c1628a2cSLeo Yan 	struct clk *clk;
199c1628a2cSLeo Yan 	struct device_node *np = pdev->dev.of_node;
200c1628a2cSLeo Yan 	int ret;
201c1628a2cSLeo Yan 
202c1628a2cSLeo Yan 	stub_clk = devm_kzalloc(dev, sizeof(*stub_clk), GFP_KERNEL);
203c1628a2cSLeo Yan 	if (!stub_clk)
204c1628a2cSLeo Yan 		return -ENOMEM;
205c1628a2cSLeo Yan 
206c1628a2cSLeo Yan 	stub_clk->dfs_map = syscon_regmap_lookup_by_phandle(np,
207c1628a2cSLeo Yan 				"hisilicon,hi6220-clk-sram");
208c1628a2cSLeo Yan 	if (IS_ERR(stub_clk->dfs_map)) {
209c1628a2cSLeo Yan 		dev_err(dev, "failed to get sram regmap\n");
210c1628a2cSLeo Yan 		return PTR_ERR(stub_clk->dfs_map);
211c1628a2cSLeo Yan 	}
212c1628a2cSLeo Yan 
213c1628a2cSLeo Yan 	stub_clk->hw.init = &init;
214c1628a2cSLeo Yan 	stub_clk->dev = dev;
215c1628a2cSLeo Yan 	stub_clk->id = HI6220_STUB_ACPU0;
216c1628a2cSLeo Yan 
217c1628a2cSLeo Yan 	/* Use mailbox client with blocking mode */
218c1628a2cSLeo Yan 	stub_clk->cl.dev = dev;
219c1628a2cSLeo Yan 	stub_clk->cl.tx_done = NULL;
220c1628a2cSLeo Yan 	stub_clk->cl.tx_block = true;
221c1628a2cSLeo Yan 	stub_clk->cl.tx_tout = 500;
222c1628a2cSLeo Yan 	stub_clk->cl.knows_txdone = false;
223c1628a2cSLeo Yan 
224c1628a2cSLeo Yan 	/* Allocate mailbox channel */
225c1628a2cSLeo Yan 	stub_clk->mbox = mbox_request_channel(&stub_clk->cl, 0);
226c1628a2cSLeo Yan 	if (IS_ERR(stub_clk->mbox)) {
227c1628a2cSLeo Yan 		dev_err(dev, "failed get mailbox channel\n");
228c1628a2cSLeo Yan 		return PTR_ERR(stub_clk->mbox);
22990c53547SJavier Martinez Canillas 	}
230c1628a2cSLeo Yan 
231c1628a2cSLeo Yan 	init.name = "acpu0";
232c1628a2cSLeo Yan 	init.ops = &hi6220_stub_clk_ops;
233c1628a2cSLeo Yan 	init.num_parents = 0;
234f61990f3SStephen Boyd 	init.flags = 0;
235c1628a2cSLeo Yan 
236c1628a2cSLeo Yan 	clk = devm_clk_register(dev, &stub_clk->hw);
237c1628a2cSLeo Yan 	if (IS_ERR(clk))
238c1628a2cSLeo Yan 		return PTR_ERR(clk);
239c1628a2cSLeo Yan 
240c1628a2cSLeo Yan 	ret = of_clk_add_provider(np, of_clk_src_simple_get, clk);
241c1628a2cSLeo Yan 	if (ret) {
242c1628a2cSLeo Yan 		dev_err(dev, "failed to register OF clock provider\n");
243c1628a2cSLeo Yan 		return ret;
244c1628a2cSLeo Yan 	}
245c1628a2cSLeo Yan 
246c1628a2cSLeo Yan 	/* initialize buffer to zero */
247c1628a2cSLeo Yan 	regmap_write(stub_clk->dfs_map, ACPU_DFS_FLAG, 0x0);
248c1628a2cSLeo Yan 	regmap_write(stub_clk->dfs_map, ACPU_DFS_FREQ_REQ, 0x0);
249c1628a2cSLeo Yan 	regmap_write(stub_clk->dfs_map, ACPU_DFS_FREQ_LMT, 0x0);
250c1628a2cSLeo Yan 
251c1628a2cSLeo Yan 	dev_dbg(dev, "Registered clock '%s'\n", init.name);
252c1628a2cSLeo Yan 	return 0;
253c1628a2cSLeo Yan }
254c1628a2cSLeo Yan 
255c1628a2cSLeo Yan static const struct of_device_id hi6220_stub_clk_of_match[] = {
256c1628a2cSLeo Yan 	{ .compatible = "hisilicon,hi6220-stub-clk", },
257c1628a2cSLeo Yan 	{}
258c1628a2cSLeo Yan };
259c1628a2cSLeo Yan 
260c1628a2cSLeo Yan static struct platform_driver hi6220_stub_clk_driver = {
261c1628a2cSLeo Yan 	.driver	= {
262c1628a2cSLeo Yan 		.name = "hi6220-stub-clk",
263c1628a2cSLeo Yan 		.of_match_table = hi6220_stub_clk_of_match,
264c1628a2cSLeo Yan 	},
265c1628a2cSLeo Yan 	.probe = hi6220_stub_clk_probe,
266c1628a2cSLeo Yan };
267c1628a2cSLeo Yan 
hi6220_stub_clk_init(void)268c1628a2cSLeo Yan static int __init hi6220_stub_clk_init(void)
269c1628a2cSLeo Yan {
270c1628a2cSLeo Yan 	return platform_driver_register(&hi6220_stub_clk_driver);
271c1628a2cSLeo Yan }
272c1628a2cSLeo Yan subsys_initcall(hi6220_stub_clk_init);
273