blob: 7bbcbfeda332d65446cdab5fe79c59a4b27f24c3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michal Simeka0d28022013-11-21 13:39:02 -08002/*
3 * Copyright (C) 2013 Xilinx, Inc.
Michal Simeka0d28022013-11-21 13:39:02 -08004 */
5#include <common.h>
6#include <command.h>
7#include <clk.h>
Marek Vasut9254f992018-08-08 22:10:44 +02008#if defined(CONFIG_DM) && defined(CONFIG_CLK)
9#include <dm.h>
Peng Fan51709f72019-08-21 13:35:14 +000010#include <dm/device.h>
11#include <dm/root.h>
Marek Vasut9254f992018-08-08 22:10:44 +020012#include <dm/device-internal.h>
Peng Fan51709f72019-08-21 13:35:14 +000013#include <linux/clk-provider.h>
Marek Vasut9254f992018-08-08 22:10:44 +020014#endif
Michal Simeka0d28022013-11-21 13:39:02 -080015
Marek Vasut9254f992018-08-08 22:10:44 +020016#if defined(CONFIG_DM) && defined(CONFIG_CLK)
Peng Fan51709f72019-08-21 13:35:14 +000017static void show_clks(struct udevice *dev, int depth, int last_flag)
18{
19 int i, is_last;
20 struct udevice *child;
Tero Kristo1a22eba2021-06-11 11:45:07 +030021 struct clk *clkp, *parent;
Peng Fan51709f72019-08-21 13:35:14 +000022 u32 rate;
Marek Vasut9254f992018-08-08 22:10:44 +020023
Peng Fan51709f72019-08-21 13:35:14 +000024 clkp = dev_get_clk_ptr(dev);
Patrick Delaunayaac5b9a2022-12-13 14:57:10 +010025 if (clkp) {
Tero Kristo1a22eba2021-06-11 11:45:07 +030026 parent = clk_get_parent(clkp);
27 if (!IS_ERR(parent) && depth == -1)
28 return;
Patrick Delaunay0ca4cc02020-07-30 14:04:10 +020029 depth++;
Peng Fan51709f72019-08-21 13:35:14 +000030 rate = clk_get_rate(clkp);
Marek Vasut9254f992018-08-08 22:10:44 +020031
Patrick Delaunay15c5a342020-07-30 14:04:09 +020032 printf(" %-12u %8d ", rate, clkp->enable_count);
Marek Vasut9254f992018-08-08 22:10:44 +020033
Patrick Delaunay15c5a342020-07-30 14:04:09 +020034 for (i = depth; i >= 0; i--) {
35 is_last = (last_flag >> i) & 1;
36 if (i) {
37 if (is_last)
38 printf(" ");
39 else
40 printf("| ");
41 } else {
42 if (is_last)
43 printf("`-- ");
44 else
45 printf("|-- ");
46 }
Peng Fan51709f72019-08-21 13:35:14 +000047 }
Marek Vasut9254f992018-08-08 22:10:44 +020048
Patrick Delaunay15c5a342020-07-30 14:04:09 +020049 printf("%s\n", dev->name);
Peng Fan51709f72019-08-21 13:35:14 +000050 }
Ismael Luceno Cortes8b5d3a42019-03-18 12:27:32 +000051
Patrick Delaunayaac5b9a2022-12-13 14:57:10 +010052 device_foreach_child_probe(child, dev) {
53 if (device_get_uclass_id(child) != UCLASS_CLK)
54 continue;
Tero Kristo1a22eba2021-06-11 11:45:07 +030055 if (child == dev)
56 continue;
Peng Fan51709f72019-08-21 13:35:14 +000057 is_last = list_is_last(&child->sibling_node, &dev->child_head);
Patrick Delaunay0ca4cc02020-07-30 14:04:10 +020058 show_clks(child, depth, (last_flag << 1) | is_last);
Peng Fan51709f72019-08-21 13:35:14 +000059 }
60}
Ismael Luceno Cortes8b5d3a42019-03-18 12:27:32 +000061
Igor Prusov7336e682023-11-09 13:55:16 +030062static int soc_clk_dump(void)
Peng Fan51709f72019-08-21 13:35:14 +000063{
Tero Kristo1a22eba2021-06-11 11:45:07 +030064 struct udevice *dev;
Igor Prusov6b565402023-11-09 13:55:14 +030065 const struct clk_ops *ops;
Tero Kristo1a22eba2021-06-11 11:45:07 +030066
67 printf(" Rate Usecnt Name\n");
68 printf("------------------------------------------\n");
69
Patrick Delaunayaac5b9a2022-12-13 14:57:10 +010070 uclass_foreach_dev_probe(UCLASS_CLK, dev)
Tero Kristo1a22eba2021-06-11 11:45:07 +030071 show_clks(dev, -1, 0);
Marek Vasut9254f992018-08-08 22:10:44 +020072
Igor Prusov6b565402023-11-09 13:55:14 +030073 uclass_foreach_dev_probe(UCLASS_CLK, dev) {
74 ops = dev_get_driver_ops(dev);
75 if (ops && ops->dump) {
76 printf("\n%s %s:\n", dev->driver->name, dev->name);
77 ops->dump(dev);
78 }
79 }
80
Marek Vasut9254f992018-08-08 22:10:44 +020081 return 0;
Peng Fan51709f72019-08-21 13:35:14 +000082}
Marek Vasut9254f992018-08-08 22:10:44 +020083#else
Igor Prusov7336e682023-11-09 13:55:16 +030084static int soc_clk_dump(void)
Peng Fan51709f72019-08-21 13:35:14 +000085{
Michal Simeka0d28022013-11-21 13:39:02 -080086 puts("Not implemented\n");
87 return 1;
88}
Peng Fan51709f72019-08-21 13:35:14 +000089#endif
Michal Simeka0d28022013-11-21 13:39:02 -080090
Simon Glassed38aef2020-05-10 11:40:03 -060091static int do_clk_dump(struct cmd_tbl *cmdtp, int flag, int argc,
Michal Simeka0d28022013-11-21 13:39:02 -080092 char *const argv[])
93{
Michal Simek7f6992b2018-04-19 15:15:25 +020094 int ret;
95
96 ret = soc_clk_dump();
97 if (ret < 0) {
98 printf("Clock dump error %d\n", ret);
99 ret = CMD_RET_FAILURE;
100 }
101
102 return ret;
Michal Simeka0d28022013-11-21 13:39:02 -0800103}
104
Tero Kristo4bd4cb42021-06-11 11:45:09 +0300105#if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(CLK)
Tero Kristo4bd4cb42021-06-11 11:45:09 +0300106static int do_clk_setfreq(struct cmd_tbl *cmdtp, int flag, int argc,
107 char *const argv[])
108{
109 struct clk *clk = NULL;
110 s32 freq;
111 struct udevice *dev;
112
Patrick Delaunay5c6d32d2022-01-31 17:21:37 +0100113 if (argc != 3)
114 return CMD_RET_USAGE;
115
Simon Glassff9b9032021-07-24 09:03:30 -0600116 freq = dectoul(argv[2], NULL);
Tero Kristo4bd4cb42021-06-11 11:45:09 +0300117
Patrick Delaunay27e14d42022-01-31 17:21:38 +0100118 if (!uclass_get_device_by_name(UCLASS_CLK, argv[1], &dev))
Tero Kristo4bd4cb42021-06-11 11:45:09 +0300119 clk = dev_get_clk_ptr(dev);
120
121 if (!clk) {
122 printf("clock '%s' not found.\n", argv[1]);
Patrick Delaunayfe23c802022-01-31 17:21:39 +0100123 return CMD_RET_FAILURE;
Tero Kristo4bd4cb42021-06-11 11:45:09 +0300124 }
125
126 freq = clk_set_rate(clk, freq);
127 if (freq < 0) {
128 printf("set_rate failed: %d\n", freq);
129 return CMD_RET_FAILURE;
130 }
131
132 printf("set_rate returns %u\n", freq);
133 return 0;
134}
135#endif
136
Simon Glassed38aef2020-05-10 11:40:03 -0600137static struct cmd_tbl cmd_clk_sub[] = {
Michal Simeka0d28022013-11-21 13:39:02 -0800138 U_BOOT_CMD_MKENT(dump, 1, 1, do_clk_dump, "", ""),
Tero Kristo4bd4cb42021-06-11 11:45:09 +0300139#if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(CLK)
140 U_BOOT_CMD_MKENT(setfreq, 3, 1, do_clk_setfreq, "", ""),
141#endif
Michal Simeka0d28022013-11-21 13:39:02 -0800142};
143
Simon Glassed38aef2020-05-10 11:40:03 -0600144static int do_clk(struct cmd_tbl *cmdtp, int flag, int argc,
Michal Simeka0d28022013-11-21 13:39:02 -0800145 char *const argv[])
146{
Simon Glassed38aef2020-05-10 11:40:03 -0600147 struct cmd_tbl *c;
Michal Simeka0d28022013-11-21 13:39:02 -0800148
149 if (argc < 2)
150 return CMD_RET_USAGE;
151
152 /* Strip off leading 'clk' command argument */
153 argc--;
154 argv++;
155
156 c = find_cmd_tbl(argv[0], &cmd_clk_sub[0], ARRAY_SIZE(cmd_clk_sub));
157
158 if (c)
159 return c->cmd(cmdtp, flag, argc, argv);
160 else
161 return CMD_RET_USAGE;
162}
163
Tom Rini03f146c2023-10-07 15:13:08 -0400164U_BOOT_LONGHELP(clk,
Tero Kristo4bd4cb42021-06-11 11:45:09 +0300165 "dump - Print clock frequencies\n"
Tom Rini03f146c2023-10-07 15:13:08 -0400166 "clk setfreq [clk] [freq] - Set clock frequency");
Michal Simeka0d28022013-11-21 13:39:02 -0800167
Tero Kristo4bd4cb42021-06-11 11:45:09 +0300168U_BOOT_CMD(clk, 4, 1, do_clk, "CLK sub-system", clk_help_text);