blob: 0245b97136043bcfe186707938df03d16cd5962d [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);
25 if (device_get_uclass_id(dev) == UCLASS_CLK && 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
Peng Fan51709f72019-08-21 13:35:14 +000052 list_for_each_entry(child, &dev->child_head, sibling_node) {
Tero Kristo1a22eba2021-06-11 11:45:07 +030053 if (child == dev)
54 continue;
55
Peng Fan51709f72019-08-21 13:35:14 +000056 is_last = list_is_last(&child->sibling_node, &dev->child_head);
Patrick Delaunay0ca4cc02020-07-30 14:04:10 +020057 show_clks(child, depth, (last_flag << 1) | is_last);
Peng Fan51709f72019-08-21 13:35:14 +000058 }
59}
Ismael Luceno Cortes8b5d3a42019-03-18 12:27:32 +000060
Peng Fan51709f72019-08-21 13:35:14 +000061int __weak soc_clk_dump(void)
62{
Tero Kristo1a22eba2021-06-11 11:45:07 +030063 struct udevice *dev;
64 struct uclass *uc;
65 int ret;
Peng Fan51709f72019-08-21 13:35:14 +000066
Tero Kristo1a22eba2021-06-11 11:45:07 +030067 ret = uclass_get(UCLASS_CLK, &uc);
68 if (ret)
69 return ret;
70
71 printf(" Rate Usecnt Name\n");
72 printf("------------------------------------------\n");
73
74 uclass_foreach_dev(dev, uc)
75 show_clks(dev, -1, 0);
Marek Vasut9254f992018-08-08 22:10:44 +020076
77 return 0;
Peng Fan51709f72019-08-21 13:35:14 +000078}
Marek Vasut9254f992018-08-08 22:10:44 +020079#else
Peng Fan51709f72019-08-21 13:35:14 +000080int __weak soc_clk_dump(void)
81{
Michal Simeka0d28022013-11-21 13:39:02 -080082 puts("Not implemented\n");
83 return 1;
84}
Peng Fan51709f72019-08-21 13:35:14 +000085#endif
Michal Simeka0d28022013-11-21 13:39:02 -080086
Simon Glassed38aef2020-05-10 11:40:03 -060087static int do_clk_dump(struct cmd_tbl *cmdtp, int flag, int argc,
Michal Simeka0d28022013-11-21 13:39:02 -080088 char *const argv[])
89{
Michal Simek7f6992b2018-04-19 15:15:25 +020090 int ret;
91
92 ret = soc_clk_dump();
93 if (ret < 0) {
94 printf("Clock dump error %d\n", ret);
95 ret = CMD_RET_FAILURE;
96 }
97
98 return ret;
Michal Simeka0d28022013-11-21 13:39:02 -080099}
100
Simon Glassed38aef2020-05-10 11:40:03 -0600101static struct cmd_tbl cmd_clk_sub[] = {
Michal Simeka0d28022013-11-21 13:39:02 -0800102 U_BOOT_CMD_MKENT(dump, 1, 1, do_clk_dump, "", ""),
103};
104
Simon Glassed38aef2020-05-10 11:40:03 -0600105static int do_clk(struct cmd_tbl *cmdtp, int flag, int argc,
Michal Simeka0d28022013-11-21 13:39:02 -0800106 char *const argv[])
107{
Simon Glassed38aef2020-05-10 11:40:03 -0600108 struct cmd_tbl *c;
Michal Simeka0d28022013-11-21 13:39:02 -0800109
110 if (argc < 2)
111 return CMD_RET_USAGE;
112
113 /* Strip off leading 'clk' command argument */
114 argc--;
115 argv++;
116
117 c = find_cmd_tbl(argv[0], &cmd_clk_sub[0], ARRAY_SIZE(cmd_clk_sub));
118
119 if (c)
120 return c->cmd(cmdtp, flag, argc, argv);
121 else
122 return CMD_RET_USAGE;
123}
124
125#ifdef CONFIG_SYS_LONGHELP
126static char clk_help_text[] =
127 "dump - Print clock frequencies";
128#endif
129
130U_BOOT_CMD(clk, 2, 1, do_clk, "CLK sub-system", clk_help_text);