blob: 2fc834e5549c60770463f6e733ef39d67d810ff3 [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 */
Michal Simeka0d28022013-11-21 13:39:02 -08005#include <command.h>
6#include <clk.h>
Marek Vasut9254f992018-08-08 22:10:44 +02007#include <dm.h>
Peng Fan51709f72019-08-21 13:35:14 +00008#include <dm/device.h>
9#include <dm/root.h>
Marek Vasut9254f992018-08-08 22:10:44 +020010#include <dm/device-internal.h>
Peng Fan51709f72019-08-21 13:35:14 +000011#include <linux/clk-provider.h>
Michal Simeka0d28022013-11-21 13:39:02 -080012
Peng Fan51709f72019-08-21 13:35:14 +000013static void show_clks(struct udevice *dev, int depth, int last_flag)
14{
15 int i, is_last;
16 struct udevice *child;
Tero Kristo1a22eba2021-06-11 11:45:07 +030017 struct clk *clkp, *parent;
Peng Fan51709f72019-08-21 13:35:14 +000018 u32 rate;
Marek Vasut9254f992018-08-08 22:10:44 +020019
Peng Fan51709f72019-08-21 13:35:14 +000020 clkp = dev_get_clk_ptr(dev);
Patrick Delaunayaac5b9a2022-12-13 14:57:10 +010021 if (clkp) {
Tero Kristo1a22eba2021-06-11 11:45:07 +030022 parent = clk_get_parent(clkp);
23 if (!IS_ERR(parent) && depth == -1)
24 return;
Patrick Delaunay0ca4cc02020-07-30 14:04:10 +020025 depth++;
Peng Fan51709f72019-08-21 13:35:14 +000026 rate = clk_get_rate(clkp);
Marek Vasut9254f992018-08-08 22:10:44 +020027
Patrick Delaunay15c5a342020-07-30 14:04:09 +020028 printf(" %-12u %8d ", rate, clkp->enable_count);
Marek Vasut9254f992018-08-08 22:10:44 +020029
Patrick Delaunay15c5a342020-07-30 14:04:09 +020030 for (i = depth; i >= 0; i--) {
31 is_last = (last_flag >> i) & 1;
32 if (i) {
33 if (is_last)
34 printf(" ");
35 else
36 printf("| ");
37 } else {
38 if (is_last)
39 printf("`-- ");
40 else
41 printf("|-- ");
42 }
Peng Fan51709f72019-08-21 13:35:14 +000043 }
Marek Vasut9254f992018-08-08 22:10:44 +020044
Patrick Delaunay15c5a342020-07-30 14:04:09 +020045 printf("%s\n", dev->name);
Peng Fan51709f72019-08-21 13:35:14 +000046 }
Ismael Luceno Cortes8b5d3a42019-03-18 12:27:32 +000047
Patrick Delaunayaac5b9a2022-12-13 14:57:10 +010048 device_foreach_child_probe(child, dev) {
49 if (device_get_uclass_id(child) != UCLASS_CLK)
50 continue;
Tero Kristo1a22eba2021-06-11 11:45:07 +030051 if (child == dev)
52 continue;
Peng Fan51709f72019-08-21 13:35:14 +000053 is_last = list_is_last(&child->sibling_node, &dev->child_head);
Patrick Delaunay0ca4cc02020-07-30 14:04:10 +020054 show_clks(child, depth, (last_flag << 1) | is_last);
Peng Fan51709f72019-08-21 13:35:14 +000055 }
56}
Ismael Luceno Cortes8b5d3a42019-03-18 12:27:32 +000057
Igor Prusov7336e682023-11-09 13:55:16 +030058static int soc_clk_dump(void)
Peng Fan51709f72019-08-21 13:35:14 +000059{
Tero Kristo1a22eba2021-06-11 11:45:07 +030060 struct udevice *dev;
Igor Prusov6b565402023-11-09 13:55:14 +030061 const struct clk_ops *ops;
Tero Kristo1a22eba2021-06-11 11:45:07 +030062
63 printf(" Rate Usecnt Name\n");
64 printf("------------------------------------------\n");
65
Patrick Delaunayaac5b9a2022-12-13 14:57:10 +010066 uclass_foreach_dev_probe(UCLASS_CLK, dev)
Tero Kristo1a22eba2021-06-11 11:45:07 +030067 show_clks(dev, -1, 0);
Marek Vasut9254f992018-08-08 22:10:44 +020068
Igor Prusov6b565402023-11-09 13:55:14 +030069 uclass_foreach_dev_probe(UCLASS_CLK, dev) {
70 ops = dev_get_driver_ops(dev);
71 if (ops && ops->dump) {
72 printf("\n%s %s:\n", dev->driver->name, dev->name);
73 ops->dump(dev);
74 }
75 }
76
Marek Vasut9254f992018-08-08 22:10:44 +020077 return 0;
Peng Fan51709f72019-08-21 13:35:14 +000078}
Michal Simeka0d28022013-11-21 13:39:02 -080079
Simon Glassed38aef2020-05-10 11:40:03 -060080static int do_clk_dump(struct cmd_tbl *cmdtp, int flag, int argc,
Michal Simeka0d28022013-11-21 13:39:02 -080081 char *const argv[])
82{
Michal Simek7f6992b2018-04-19 15:15:25 +020083 int ret;
84
85 ret = soc_clk_dump();
86 if (ret < 0) {
87 printf("Clock dump error %d\n", ret);
88 ret = CMD_RET_FAILURE;
89 }
90
91 return ret;
Michal Simeka0d28022013-11-21 13:39:02 -080092}
93
Tero Kristo4bd4cb42021-06-11 11:45:09 +030094static int do_clk_setfreq(struct cmd_tbl *cmdtp, int flag, int argc,
95 char *const argv[])
96{
97 struct clk *clk = NULL;
98 s32 freq;
99 struct udevice *dev;
100
Patrick Delaunay5c6d32d2022-01-31 17:21:37 +0100101 if (argc != 3)
102 return CMD_RET_USAGE;
103
Simon Glassff9b9032021-07-24 09:03:30 -0600104 freq = dectoul(argv[2], NULL);
Tero Kristo4bd4cb42021-06-11 11:45:09 +0300105
Patrick Delaunay27e14d42022-01-31 17:21:38 +0100106 if (!uclass_get_device_by_name(UCLASS_CLK, argv[1], &dev))
Tero Kristo4bd4cb42021-06-11 11:45:09 +0300107 clk = dev_get_clk_ptr(dev);
108
109 if (!clk) {
110 printf("clock '%s' not found.\n", argv[1]);
Patrick Delaunayfe23c802022-01-31 17:21:39 +0100111 return CMD_RET_FAILURE;
Tero Kristo4bd4cb42021-06-11 11:45:09 +0300112 }
113
114 freq = clk_set_rate(clk, freq);
115 if (freq < 0) {
116 printf("set_rate failed: %d\n", freq);
117 return CMD_RET_FAILURE;
118 }
119
120 printf("set_rate returns %u\n", freq);
121 return 0;
122}
Tero Kristo4bd4cb42021-06-11 11:45:09 +0300123
Simon Glassed38aef2020-05-10 11:40:03 -0600124static struct cmd_tbl cmd_clk_sub[] = {
Michal Simeka0d28022013-11-21 13:39:02 -0800125 U_BOOT_CMD_MKENT(dump, 1, 1, do_clk_dump, "", ""),
Tero Kristo4bd4cb42021-06-11 11:45:09 +0300126 U_BOOT_CMD_MKENT(setfreq, 3, 1, do_clk_setfreq, "", ""),
Michal Simeka0d28022013-11-21 13:39:02 -0800127};
128
Simon Glassed38aef2020-05-10 11:40:03 -0600129static int do_clk(struct cmd_tbl *cmdtp, int flag, int argc,
Michal Simeka0d28022013-11-21 13:39:02 -0800130 char *const argv[])
131{
Simon Glassed38aef2020-05-10 11:40:03 -0600132 struct cmd_tbl *c;
Michal Simeka0d28022013-11-21 13:39:02 -0800133
134 if (argc < 2)
135 return CMD_RET_USAGE;
136
137 /* Strip off leading 'clk' command argument */
138 argc--;
139 argv++;
140
141 c = find_cmd_tbl(argv[0], &cmd_clk_sub[0], ARRAY_SIZE(cmd_clk_sub));
142
143 if (c)
144 return c->cmd(cmdtp, flag, argc, argv);
145 else
146 return CMD_RET_USAGE;
147}
148
Tom Rini03f146c2023-10-07 15:13:08 -0400149U_BOOT_LONGHELP(clk,
Tero Kristo4bd4cb42021-06-11 11:45:09 +0300150 "dump - Print clock frequencies\n"
Tom Rini03f146c2023-10-07 15:13:08 -0400151 "clk setfreq [clk] [freq] - Set clock frequency");
Michal Simeka0d28022013-11-21 13:39:02 -0800152
Tero Kristo4bd4cb42021-06-11 11:45:09 +0300153U_BOOT_CMD(clk, 4, 1, do_clk, "CLK sub-system", clk_help_text);