blob: 74ad8685002461bb431c13dc32e41ef61c976b77 [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;
21 struct clk *clkp;
22 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) {
26 rate = clk_get_rate(clkp);
Marek Vasut9254f992018-08-08 22:10:44 +020027
Peng Fan51709f72019-08-21 13:35:14 +000028 printf(" %-12u %8d ", rate, clkp->enable_count);
Marek Vasut9254f992018-08-08 22:10:44 +020029
Peng Fan51709f72019-08-21 13:35:14 +000030 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 }
43 }
Marek Vasut9254f992018-08-08 22:10:44 +020044
Peng Fan51709f72019-08-21 13:35:14 +000045 printf("%s\n", dev->name);
46 }
Ismael Luceno Cortes8b5d3a42019-03-18 12:27:32 +000047
Peng Fan51709f72019-08-21 13:35:14 +000048 list_for_each_entry(child, &dev->child_head, sibling_node) {
49 is_last = list_is_last(&child->sibling_node, &dev->child_head);
50 show_clks(child, depth + 1, (last_flag << 1) | is_last);
51 }
52}
Ismael Luceno Cortes8b5d3a42019-03-18 12:27:32 +000053
Peng Fan51709f72019-08-21 13:35:14 +000054int __weak soc_clk_dump(void)
55{
56 struct udevice *root;
57
58 root = dm_root();
59 if (root) {
60 printf(" Rate Usecnt Name\n");
61 printf("------------------------------------------\n");
62 show_clks(root, -1, 0);
Marek Vasut9254f992018-08-08 22:10:44 +020063 }
64
65 return 0;
Peng Fan51709f72019-08-21 13:35:14 +000066}
Marek Vasut9254f992018-08-08 22:10:44 +020067#else
Peng Fan51709f72019-08-21 13:35:14 +000068int __weak soc_clk_dump(void)
69{
Michal Simeka0d28022013-11-21 13:39:02 -080070 puts("Not implemented\n");
71 return 1;
72}
Peng Fan51709f72019-08-21 13:35:14 +000073#endif
Michal Simeka0d28022013-11-21 13:39:02 -080074
75static int do_clk_dump(cmd_tbl_t *cmdtp, int flag, int argc,
76 char *const argv[])
77{
Michal Simek7f6992b2018-04-19 15:15:25 +020078 int ret;
79
80 ret = soc_clk_dump();
81 if (ret < 0) {
82 printf("Clock dump error %d\n", ret);
83 ret = CMD_RET_FAILURE;
84 }
85
86 return ret;
Michal Simeka0d28022013-11-21 13:39:02 -080087}
88
89static cmd_tbl_t cmd_clk_sub[] = {
90 U_BOOT_CMD_MKENT(dump, 1, 1, do_clk_dump, "", ""),
91};
92
93static int do_clk(cmd_tbl_t *cmdtp, int flag, int argc,
94 char *const argv[])
95{
96 cmd_tbl_t *c;
97
98 if (argc < 2)
99 return CMD_RET_USAGE;
100
101 /* Strip off leading 'clk' command argument */
102 argc--;
103 argv++;
104
105 c = find_cmd_tbl(argv[0], &cmd_clk_sub[0], ARRAY_SIZE(cmd_clk_sub));
106
107 if (c)
108 return c->cmd(cmdtp, flag, argc, argv);
109 else
110 return CMD_RET_USAGE;
111}
112
113#ifdef CONFIG_SYS_LONGHELP
114static char clk_help_text[] =
115 "dump - Print clock frequencies";
116#endif
117
118U_BOOT_CMD(clk, 2, 1, do_clk, "CLK sub-system", clk_help_text);