1 // SPDX-License-Identifier: GPL-2.0
3 * System Control and Management Interface (SCMI) Clock Protocol
5 * Copyright (C) 2018-2021 ARM Ltd.
8 #include <linux/module.h>
9 #include <linux/sort.h>
13 enum scmi_clock_protocol_cmd {
14 CLOCK_ATTRIBUTES = 0x3,
15 CLOCK_DESCRIBE_RATES = 0x4,
18 CLOCK_CONFIG_SET = 0x7,
21 struct scmi_msg_resp_clock_protocol_attributes {
27 struct scmi_msg_resp_clock_attributes {
29 #define CLOCK_ENABLE BIT(0)
30 u8 name[SCMI_MAX_STR_SIZE];
31 __le32 clock_enable_latency;
34 struct scmi_clock_set_config {
39 struct scmi_msg_clock_describe_rates {
44 struct scmi_msg_resp_clock_describe_rates {
45 __le32 num_rates_flags;
46 #define NUM_RETURNED(x) ((x) & 0xfff)
47 #define RATE_DISCRETE(x) !((x) & BIT(12))
48 #define NUM_REMAINING(x) ((x) >> 16)
53 #define RATE_TO_U64(X) \
56 le32_to_cpu((x).value_low) | (u64)le32_to_cpu((x).value_high) << 32; \
60 struct scmi_clock_set_rate {
62 #define CLOCK_SET_ASYNC BIT(0)
63 #define CLOCK_SET_IGNORE_RESP BIT(1)
64 #define CLOCK_SET_ROUND_UP BIT(2)
65 #define CLOCK_SET_ROUND_AUTO BIT(3)
75 atomic_t cur_async_req;
76 struct scmi_clock_info *clk;
80 scmi_clock_protocol_attributes_get(const struct scmi_protocol_handle *ph,
81 struct clock_info *ci)
85 struct scmi_msg_resp_clock_protocol_attributes *attr;
87 ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES,
88 0, sizeof(*attr), &t);
94 ret = ph->xops->do_xfer(ph, t);
96 ci->num_clocks = le16_to_cpu(attr->num_clocks);
97 ci->max_async_req = attr->max_async_req;
100 ph->xops->xfer_put(ph, t);
104 static int scmi_clock_attributes_get(const struct scmi_protocol_handle *ph,
105 u32 clk_id, struct scmi_clock_info *clk)
109 struct scmi_msg_resp_clock_attributes *attr;
111 ret = ph->xops->xfer_get_init(ph, CLOCK_ATTRIBUTES,
112 sizeof(clk_id), sizeof(*attr), &t);
116 put_unaligned_le32(clk_id, t->tx.buf);
119 ret = ph->xops->do_xfer(ph, t);
121 strlcpy(clk->name, attr->name, SCMI_MAX_STR_SIZE);
122 /* Is optional field clock_enable_latency provided ? */
123 if (t->rx.len == sizeof(*attr))
124 clk->enable_latency =
125 le32_to_cpu(attr->clock_enable_latency);
130 ph->xops->xfer_put(ph, t);
134 static int rate_cmp_func(const void *_r1, const void *_r2)
136 const u64 *r1 = _r1, *r2 = _r2;
147 scmi_clock_describe_rates_get(const struct scmi_protocol_handle *ph, u32 clk_id,
148 struct scmi_clock_info *clk)
152 bool rate_discrete = false;
153 u32 tot_rate_cnt = 0, rates_flag;
154 u16 num_returned, num_remaining;
156 struct scmi_msg_clock_describe_rates *clk_desc;
157 struct scmi_msg_resp_clock_describe_rates *rlist;
159 ret = ph->xops->xfer_get_init(ph, CLOCK_DESCRIBE_RATES,
160 sizeof(*clk_desc), 0, &t);
164 clk_desc = t->tx.buf;
168 clk_desc->id = cpu_to_le32(clk_id);
169 /* Set the number of rates to be skipped/already read */
170 clk_desc->rate_index = cpu_to_le32(tot_rate_cnt);
172 ret = ph->xops->do_xfer(ph, t);
176 rates_flag = le32_to_cpu(rlist->num_rates_flags);
177 num_remaining = NUM_REMAINING(rates_flag);
178 rate_discrete = RATE_DISCRETE(rates_flag);
179 num_returned = NUM_RETURNED(rates_flag);
181 if (tot_rate_cnt + num_returned > SCMI_MAX_NUM_RATES) {
182 dev_err(ph->dev, "No. of rates > MAX_NUM_RATES");
186 if (!rate_discrete) {
187 clk->range.min_rate = RATE_TO_U64(rlist->rate[0]);
188 clk->range.max_rate = RATE_TO_U64(rlist->rate[1]);
189 clk->range.step_size = RATE_TO_U64(rlist->rate[2]);
190 dev_dbg(ph->dev, "Min %llu Max %llu Step %llu Hz\n",
191 clk->range.min_rate, clk->range.max_rate,
192 clk->range.step_size);
196 rate = &clk->list.rates[tot_rate_cnt];
197 for (cnt = 0; cnt < num_returned; cnt++, rate++) {
198 *rate = RATE_TO_U64(rlist->rate[cnt]);
199 dev_dbg(ph->dev, "Rate %llu Hz\n", *rate);
202 tot_rate_cnt += num_returned;
204 ph->xops->reset_rx_to_maxsz(ph, t);
206 * check for both returned and remaining to avoid infinite
207 * loop due to buggy firmware
209 } while (num_returned && num_remaining);
211 if (rate_discrete && rate) {
212 clk->list.num_rates = tot_rate_cnt;
213 sort(clk->list.rates, tot_rate_cnt, sizeof(*rate),
214 rate_cmp_func, NULL);
217 clk->rate_discrete = rate_discrete;
220 ph->xops->xfer_put(ph, t);
225 scmi_clock_rate_get(const struct scmi_protocol_handle *ph,
226 u32 clk_id, u64 *value)
231 ret = ph->xops->xfer_get_init(ph, CLOCK_RATE_GET,
232 sizeof(__le32), sizeof(u64), &t);
236 put_unaligned_le32(clk_id, t->tx.buf);
238 ret = ph->xops->do_xfer(ph, t);
240 *value = get_unaligned_le64(t->rx.buf);
242 ph->xops->xfer_put(ph, t);
246 static int scmi_clock_rate_set(const struct scmi_protocol_handle *ph,
247 u32 clk_id, u64 rate)
252 struct scmi_clock_set_rate *cfg;
253 struct clock_info *ci = ph->get_priv(ph);
255 ret = ph->xops->xfer_get_init(ph, CLOCK_RATE_SET, sizeof(*cfg), 0, &t);
259 if (ci->max_async_req &&
260 atomic_inc_return(&ci->cur_async_req) < ci->max_async_req)
261 flags |= CLOCK_SET_ASYNC;
264 cfg->flags = cpu_to_le32(flags);
265 cfg->id = cpu_to_le32(clk_id);
266 cfg->value_low = cpu_to_le32(rate & 0xffffffff);
267 cfg->value_high = cpu_to_le32(rate >> 32);
269 if (flags & CLOCK_SET_ASYNC)
270 ret = ph->xops->do_xfer_with_response(ph, t);
272 ret = ph->xops->do_xfer(ph, t);
274 if (ci->max_async_req)
275 atomic_dec(&ci->cur_async_req);
277 ph->xops->xfer_put(ph, t);
282 scmi_clock_config_set(const struct scmi_protocol_handle *ph, u32 clk_id,
283 u32 config, bool atomic)
287 struct scmi_clock_set_config *cfg;
289 ret = ph->xops->xfer_get_init(ph, CLOCK_CONFIG_SET,
290 sizeof(*cfg), 0, &t);
294 t->hdr.poll_completion = atomic;
297 cfg->id = cpu_to_le32(clk_id);
298 cfg->attributes = cpu_to_le32(config);
300 ret = ph->xops->do_xfer(ph, t);
302 ph->xops->xfer_put(ph, t);
306 static int scmi_clock_enable(const struct scmi_protocol_handle *ph, u32 clk_id)
308 return scmi_clock_config_set(ph, clk_id, CLOCK_ENABLE, false);
311 static int scmi_clock_disable(const struct scmi_protocol_handle *ph, u32 clk_id)
313 return scmi_clock_config_set(ph, clk_id, 0, false);
316 static int scmi_clock_enable_atomic(const struct scmi_protocol_handle *ph,
319 return scmi_clock_config_set(ph, clk_id, CLOCK_ENABLE, true);
322 static int scmi_clock_disable_atomic(const struct scmi_protocol_handle *ph,
325 return scmi_clock_config_set(ph, clk_id, 0, true);
328 static int scmi_clock_count_get(const struct scmi_protocol_handle *ph)
330 struct clock_info *ci = ph->get_priv(ph);
332 return ci->num_clocks;
335 static const struct scmi_clock_info *
336 scmi_clock_info_get(const struct scmi_protocol_handle *ph, u32 clk_id)
338 struct clock_info *ci = ph->get_priv(ph);
339 struct scmi_clock_info *clk = ci->clk + clk_id;
347 static const struct scmi_clk_proto_ops clk_proto_ops = {
348 .count_get = scmi_clock_count_get,
349 .info_get = scmi_clock_info_get,
350 .rate_get = scmi_clock_rate_get,
351 .rate_set = scmi_clock_rate_set,
352 .enable = scmi_clock_enable,
353 .disable = scmi_clock_disable,
354 .enable_atomic = scmi_clock_enable_atomic,
355 .disable_atomic = scmi_clock_disable_atomic,
358 static int scmi_clock_protocol_init(const struct scmi_protocol_handle *ph)
362 struct clock_info *cinfo;
364 ret = ph->xops->version_get(ph, &version);
368 dev_dbg(ph->dev, "Clock Version %d.%d\n",
369 PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
371 cinfo = devm_kzalloc(ph->dev, sizeof(*cinfo), GFP_KERNEL);
375 ret = scmi_clock_protocol_attributes_get(ph, cinfo);
379 cinfo->clk = devm_kcalloc(ph->dev, cinfo->num_clocks,
380 sizeof(*cinfo->clk), GFP_KERNEL);
384 for (clkid = 0; clkid < cinfo->num_clocks; clkid++) {
385 struct scmi_clock_info *clk = cinfo->clk + clkid;
387 ret = scmi_clock_attributes_get(ph, clkid, clk);
389 scmi_clock_describe_rates_get(ph, clkid, clk);
392 cinfo->version = version;
393 return ph->set_priv(ph, cinfo);
396 static const struct scmi_protocol scmi_clock = {
397 .id = SCMI_PROTOCOL_CLOCK,
398 .owner = THIS_MODULE,
399 .instance_init = &scmi_clock_protocol_init,
400 .ops = &clk_proto_ops,
403 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(clock, scmi_clock)