1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2016-2020 NVIDIA Corporation
6 #include <linux/clk-provider.h>
7 #include <linux/device.h>
8 #include <linux/seq_buf.h>
9 #include <linux/slab.h>
11 #include <soc/tegra/bpmp.h>
12 #include <soc/tegra/bpmp-abi.h>
14 #define TEGRA_BPMP_DUMP_CLOCK_INFO 0
16 #define TEGRA_BPMP_CLK_HAS_MUX BIT(0)
17 #define TEGRA_BPMP_CLK_HAS_SET_RATE BIT(1)
18 #define TEGRA_BPMP_CLK_IS_ROOT BIT(2)
20 struct tegra_bpmp_clk_info {
22 char name[MRQ_CLK_NAME_MAXLEN];
23 unsigned int parents[MRQ_CLK_MAX_PARENTS];
24 unsigned int num_parents;
28 struct tegra_bpmp_clk {
31 struct tegra_bpmp *bpmp;
34 unsigned int num_parents;
35 unsigned int *parents;
38 static inline struct tegra_bpmp_clk *to_tegra_bpmp_clk(struct clk_hw *hw)
40 return container_of(hw, struct tegra_bpmp_clk, hw);
43 struct tegra_bpmp_clk_message {
59 static int tegra_bpmp_clk_transfer(struct tegra_bpmp *bpmp,
60 const struct tegra_bpmp_clk_message *clk)
62 struct mrq_clk_request request;
63 struct tegra_bpmp_message msg;
67 memset(&request, 0, sizeof(request));
68 request.cmd_and_id = (clk->cmd << 24) | clk->id;
71 * The mrq_clk_request structure has an anonymous union at offset 4
72 * that contains all possible sub-command structures. Copy the data
73 * to that union. Ideally we'd be able to refer to it by name, but
74 * doing so would require changing the ABI header and increase the
77 memcpy(req + 4, clk->tx.data, clk->tx.size);
79 memset(&msg, 0, sizeof(msg));
81 msg.tx.data = &request;
82 msg.tx.size = sizeof(request);
83 msg.rx.data = clk->rx.data;
84 msg.rx.size = clk->rx.size;
86 err = tegra_bpmp_transfer(bpmp, &msg);
89 else if (msg.rx.ret < 0)
95 static int tegra_bpmp_clk_prepare(struct clk_hw *hw)
97 struct tegra_bpmp_clk *clk = to_tegra_bpmp_clk(hw);
98 struct tegra_bpmp_clk_message msg;
100 memset(&msg, 0, sizeof(msg));
101 msg.cmd = CMD_CLK_ENABLE;
104 return tegra_bpmp_clk_transfer(clk->bpmp, &msg);
107 static void tegra_bpmp_clk_unprepare(struct clk_hw *hw)
109 struct tegra_bpmp_clk *clk = to_tegra_bpmp_clk(hw);
110 struct tegra_bpmp_clk_message msg;
113 memset(&msg, 0, sizeof(msg));
114 msg.cmd = CMD_CLK_DISABLE;
117 err = tegra_bpmp_clk_transfer(clk->bpmp, &msg);
119 dev_err(clk->bpmp->dev, "failed to disable clock %s: %d\n",
120 clk_hw_get_name(hw), err);
123 static int tegra_bpmp_clk_is_prepared(struct clk_hw *hw)
125 struct tegra_bpmp_clk *clk = to_tegra_bpmp_clk(hw);
126 struct cmd_clk_is_enabled_response response;
127 struct tegra_bpmp_clk_message msg;
130 memset(&msg, 0, sizeof(msg));
131 msg.cmd = CMD_CLK_IS_ENABLED;
133 msg.rx.data = &response;
134 msg.rx.size = sizeof(response);
136 err = tegra_bpmp_clk_transfer(clk->bpmp, &msg);
140 return response.state;
143 static unsigned long tegra_bpmp_clk_recalc_rate(struct clk_hw *hw,
144 unsigned long parent_rate)
146 struct tegra_bpmp_clk *clk = to_tegra_bpmp_clk(hw);
147 struct cmd_clk_get_rate_response response;
148 struct cmd_clk_get_rate_request request;
149 struct tegra_bpmp_clk_message msg;
152 memset(&msg, 0, sizeof(msg));
153 msg.cmd = CMD_CLK_GET_RATE;
155 msg.tx.data = &request;
156 msg.tx.size = sizeof(request);
157 msg.rx.data = &response;
158 msg.rx.size = sizeof(response);
160 err = tegra_bpmp_clk_transfer(clk->bpmp, &msg);
164 return response.rate;
167 static int tegra_bpmp_clk_determine_rate(struct clk_hw *hw,
168 struct clk_rate_request *rate_req)
170 struct tegra_bpmp_clk *clk = to_tegra_bpmp_clk(hw);
171 struct cmd_clk_round_rate_response response;
172 struct cmd_clk_round_rate_request request;
173 struct tegra_bpmp_clk_message msg;
177 rate = min(max(rate_req->rate, rate_req->min_rate), rate_req->max_rate);
179 memset(&request, 0, sizeof(request));
180 request.rate = min_t(u64, rate, S64_MAX);
182 memset(&msg, 0, sizeof(msg));
183 msg.cmd = CMD_CLK_ROUND_RATE;
185 msg.tx.data = &request;
186 msg.tx.size = sizeof(request);
187 msg.rx.data = &response;
188 msg.rx.size = sizeof(response);
190 err = tegra_bpmp_clk_transfer(clk->bpmp, &msg);
194 rate_req->rate = (unsigned long)response.rate;
199 static int tegra_bpmp_clk_set_parent(struct clk_hw *hw, u8 index)
201 struct tegra_bpmp_clk *clk = to_tegra_bpmp_clk(hw);
202 struct cmd_clk_set_parent_response response;
203 struct cmd_clk_set_parent_request request;
204 struct tegra_bpmp_clk_message msg;
207 memset(&request, 0, sizeof(request));
208 request.parent_id = clk->parents[index];
210 memset(&msg, 0, sizeof(msg));
211 msg.cmd = CMD_CLK_SET_PARENT;
213 msg.tx.data = &request;
214 msg.tx.size = sizeof(request);
215 msg.rx.data = &response;
216 msg.rx.size = sizeof(response);
218 err = tegra_bpmp_clk_transfer(clk->bpmp, &msg);
222 /* XXX check parent ID in response */
227 static u8 tegra_bpmp_clk_get_parent(struct clk_hw *hw)
229 struct tegra_bpmp_clk *clk = to_tegra_bpmp_clk(hw);
230 struct cmd_clk_get_parent_response response;
231 struct tegra_bpmp_clk_message msg;
235 memset(&msg, 0, sizeof(msg));
236 msg.cmd = CMD_CLK_GET_PARENT;
238 msg.rx.data = &response;
239 msg.rx.size = sizeof(response);
241 err = tegra_bpmp_clk_transfer(clk->bpmp, &msg);
243 dev_err(clk->bpmp->dev, "failed to get parent for %s: %d\n",
244 clk_hw_get_name(hw), err);
248 for (i = 0; i < clk->num_parents; i++)
249 if (clk->parents[i] == response.parent_id)
255 static int tegra_bpmp_clk_set_rate(struct clk_hw *hw, unsigned long rate,
256 unsigned long parent_rate)
258 struct tegra_bpmp_clk *clk = to_tegra_bpmp_clk(hw);
259 struct cmd_clk_set_rate_response response;
260 struct cmd_clk_set_rate_request request;
261 struct tegra_bpmp_clk_message msg;
263 memset(&request, 0, sizeof(request));
264 request.rate = min_t(u64, rate, S64_MAX);
266 memset(&msg, 0, sizeof(msg));
267 msg.cmd = CMD_CLK_SET_RATE;
269 msg.tx.data = &request;
270 msg.tx.size = sizeof(request);
271 msg.rx.data = &response;
272 msg.rx.size = sizeof(response);
274 return tegra_bpmp_clk_transfer(clk->bpmp, &msg);
277 static const struct clk_ops tegra_bpmp_clk_gate_ops = {
278 .prepare = tegra_bpmp_clk_prepare,
279 .unprepare = tegra_bpmp_clk_unprepare,
280 .is_prepared = tegra_bpmp_clk_is_prepared,
281 .recalc_rate = tegra_bpmp_clk_recalc_rate,
284 static const struct clk_ops tegra_bpmp_clk_mux_ops = {
285 .prepare = tegra_bpmp_clk_prepare,
286 .unprepare = tegra_bpmp_clk_unprepare,
287 .is_prepared = tegra_bpmp_clk_is_prepared,
288 .recalc_rate = tegra_bpmp_clk_recalc_rate,
289 .set_parent = tegra_bpmp_clk_set_parent,
290 .get_parent = tegra_bpmp_clk_get_parent,
293 static const struct clk_ops tegra_bpmp_clk_rate_ops = {
294 .prepare = tegra_bpmp_clk_prepare,
295 .unprepare = tegra_bpmp_clk_unprepare,
296 .is_prepared = tegra_bpmp_clk_is_prepared,
297 .recalc_rate = tegra_bpmp_clk_recalc_rate,
298 .determine_rate = tegra_bpmp_clk_determine_rate,
299 .set_rate = tegra_bpmp_clk_set_rate,
302 static const struct clk_ops tegra_bpmp_clk_mux_rate_ops = {
303 .prepare = tegra_bpmp_clk_prepare,
304 .unprepare = tegra_bpmp_clk_unprepare,
305 .is_prepared = tegra_bpmp_clk_is_prepared,
306 .recalc_rate = tegra_bpmp_clk_recalc_rate,
307 .determine_rate = tegra_bpmp_clk_determine_rate,
308 .set_parent = tegra_bpmp_clk_set_parent,
309 .get_parent = tegra_bpmp_clk_get_parent,
310 .set_rate = tegra_bpmp_clk_set_rate,
313 static int tegra_bpmp_clk_get_max_id(struct tegra_bpmp *bpmp)
315 struct cmd_clk_get_max_clk_id_response response;
316 struct tegra_bpmp_clk_message msg;
319 memset(&msg, 0, sizeof(msg));
320 msg.cmd = CMD_CLK_GET_MAX_CLK_ID;
321 msg.rx.data = &response;
322 msg.rx.size = sizeof(response);
324 err = tegra_bpmp_clk_transfer(bpmp, &msg);
328 if (response.max_id > INT_MAX)
331 return response.max_id;
334 static int tegra_bpmp_clk_get_info(struct tegra_bpmp *bpmp, unsigned int id,
335 struct tegra_bpmp_clk_info *info)
337 struct cmd_clk_get_all_info_response response;
338 struct tegra_bpmp_clk_message msg;
342 memset(&msg, 0, sizeof(msg));
343 msg.cmd = CMD_CLK_GET_ALL_INFO;
345 msg.rx.data = &response;
346 msg.rx.size = sizeof(response);
348 err = tegra_bpmp_clk_transfer(bpmp, &msg);
352 strlcpy(info->name, response.name, MRQ_CLK_NAME_MAXLEN);
353 info->num_parents = response.num_parents;
355 for (i = 0; i < info->num_parents; i++)
356 info->parents[i] = response.parents[i];
358 info->flags = response.flags;
363 static void tegra_bpmp_clk_info_dump(struct tegra_bpmp *bpmp,
365 const struct tegra_bpmp_clk_info *info)
367 const char *prefix = "";
372 seq_buf_init(&buf, flags, sizeof(flags));
375 seq_buf_printf(&buf, "(");
377 if (info->flags & TEGRA_BPMP_CLK_HAS_MUX) {
378 seq_buf_printf(&buf, "%smux", prefix);
382 if ((info->flags & TEGRA_BPMP_CLK_HAS_SET_RATE) == 0) {
383 seq_buf_printf(&buf, "%sfixed", prefix);
387 if (info->flags & TEGRA_BPMP_CLK_IS_ROOT) {
388 seq_buf_printf(&buf, "%sroot", prefix);
393 seq_buf_printf(&buf, ")");
395 dev_printk(level, bpmp->dev, "%03u: %s\n", info->id, info->name);
396 dev_printk(level, bpmp->dev, " flags: %lx %s\n", info->flags, flags);
397 dev_printk(level, bpmp->dev, " parents: %u\n", info->num_parents);
399 for (i = 0; i < info->num_parents; i++)
400 dev_printk(level, bpmp->dev, " %03u\n", info->parents[i]);
403 static int tegra_bpmp_probe_clocks(struct tegra_bpmp *bpmp,
404 struct tegra_bpmp_clk_info **clocksp)
406 struct tegra_bpmp_clk_info *clocks;
407 unsigned int max_id, id, count = 0;
408 unsigned int holes = 0;
411 err = tegra_bpmp_clk_get_max_id(bpmp);
417 dev_dbg(bpmp->dev, "maximum clock ID: %u\n", max_id);
419 clocks = kcalloc(max_id + 1, sizeof(*clocks), GFP_KERNEL);
423 for (id = 0; id <= max_id; id++) {
424 struct tegra_bpmp_clk_info *info = &clocks[count];
426 err = tegra_bpmp_clk_get_info(bpmp, id, info);
430 if (info->num_parents >= U8_MAX) {
432 "clock %u has too many parents (%u, max: %u)\n",
433 id, info->num_parents, U8_MAX);
437 /* clock not exposed by BPMP */
438 if (info->name[0] == '\0') {
446 if (TEGRA_BPMP_DUMP_CLOCK_INFO)
447 tegra_bpmp_clk_info_dump(bpmp, KERN_DEBUG, info);
450 dev_dbg(bpmp->dev, "holes: %u\n", holes);
457 tegra_bpmp_clk_id_to_index(const struct tegra_bpmp_clk_info *clocks,
458 unsigned int num_clocks, unsigned int id)
462 for (i = 0; i < num_clocks; i++)
463 if (clocks[i].id == id)
469 static const struct tegra_bpmp_clk_info *
470 tegra_bpmp_clk_find(const struct tegra_bpmp_clk_info *clocks,
471 unsigned int num_clocks, unsigned int id)
475 i = tegra_bpmp_clk_id_to_index(clocks, num_clocks, id);
483 static struct tegra_bpmp_clk *
484 tegra_bpmp_clk_register(struct tegra_bpmp *bpmp,
485 const struct tegra_bpmp_clk_info *info,
486 const struct tegra_bpmp_clk_info *clocks,
487 unsigned int num_clocks)
489 struct tegra_bpmp_clk *clk;
490 struct clk_init_data init;
491 const char **parents;
495 clk = devm_kzalloc(bpmp->dev, sizeof(*clk), GFP_KERNEL);
497 return ERR_PTR(-ENOMEM);
502 clk->parents = devm_kcalloc(bpmp->dev, info->num_parents,
503 sizeof(*clk->parents), GFP_KERNEL);
505 return ERR_PTR(-ENOMEM);
507 clk->num_parents = info->num_parents;
509 /* hardware clock initialization */
510 memset(&init, 0, sizeof(init));
511 init.name = info->name;
512 clk->hw.init = &init;
514 if (info->flags & TEGRA_BPMP_CLK_HAS_MUX) {
515 if (info->flags & TEGRA_BPMP_CLK_HAS_SET_RATE)
516 init.ops = &tegra_bpmp_clk_mux_rate_ops;
518 init.ops = &tegra_bpmp_clk_mux_ops;
520 if (info->flags & TEGRA_BPMP_CLK_HAS_SET_RATE)
521 init.ops = &tegra_bpmp_clk_rate_ops;
523 init.ops = &tegra_bpmp_clk_gate_ops;
526 init.num_parents = info->num_parents;
528 parents = kcalloc(info->num_parents, sizeof(*parents), GFP_KERNEL);
530 return ERR_PTR(-ENOMEM);
532 for (i = 0; i < info->num_parents; i++) {
533 const struct tegra_bpmp_clk_info *parent;
535 /* keep a private copy of the ID to parent index map */
536 clk->parents[i] = info->parents[i];
538 parent = tegra_bpmp_clk_find(clocks, num_clocks,
541 dev_err(bpmp->dev, "no parent %u found for %u\n",
542 info->parents[i], info->id);
546 parents[i] = parent->name;
549 init.parent_names = parents;
551 err = devm_clk_hw_register(bpmp->dev, &clk->hw);
561 static void tegra_bpmp_register_clocks_one(struct tegra_bpmp *bpmp,
562 struct tegra_bpmp_clk_info *infos,
567 struct tegra_bpmp_clk_info *info;
568 struct tegra_bpmp_clk *clk;
570 if (bpmp->clocks[i]) {
571 /* already registered */
576 for (j = 0; j < info->num_parents; ++j) {
577 unsigned int p_id = info->parents[j];
578 unsigned int p_i = tegra_bpmp_clk_id_to_index(infos, count,
581 tegra_bpmp_register_clocks_one(bpmp, infos, p_i, count);
584 clk = tegra_bpmp_clk_register(bpmp, info, infos, count);
587 "failed to register clock %u (%s): %ld\n",
588 info->id, info->name, PTR_ERR(clk));
589 /* intentionally store the error pointer to
590 * bpmp->clocks[i] to avoid re-attempting the
595 bpmp->clocks[i] = clk;
598 static int tegra_bpmp_register_clocks(struct tegra_bpmp *bpmp,
599 struct tegra_bpmp_clk_info *infos,
604 bpmp->num_clocks = count;
606 bpmp->clocks = devm_kcalloc(bpmp->dev, count, sizeof(struct tegra_bpmp_clk), GFP_KERNEL);
610 for (i = 0; i < count; i++) {
611 tegra_bpmp_register_clocks_one(bpmp, infos, i, count);
617 static void tegra_bpmp_unregister_clocks(struct tegra_bpmp *bpmp)
621 for (i = 0; i < bpmp->num_clocks; i++)
622 clk_hw_unregister(&bpmp->clocks[i]->hw);
625 static struct clk_hw *tegra_bpmp_clk_of_xlate(struct of_phandle_args *clkspec,
628 unsigned int id = clkspec->args[0], i;
629 struct tegra_bpmp *bpmp = data;
631 for (i = 0; i < bpmp->num_clocks; i++) {
632 struct tegra_bpmp_clk *clk = bpmp->clocks[i];
644 int tegra_bpmp_init_clocks(struct tegra_bpmp *bpmp)
646 struct tegra_bpmp_clk_info *clocks;
650 err = tegra_bpmp_probe_clocks(bpmp, &clocks);
656 dev_dbg(bpmp->dev, "%u clocks probed\n", count);
658 err = tegra_bpmp_register_clocks(bpmp, clocks, count);
662 err = of_clk_add_hw_provider(bpmp->dev->of_node,
663 tegra_bpmp_clk_of_xlate,
666 tegra_bpmp_unregister_clocks(bpmp);