blob: e3c3d2f9bb224a2871047a64c90c100afc8f5a4c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michal Simek08d0d6f2013-11-21 13:39:02 -08002/*
3 * Copyright (C) 2013 Xilinx, Inc.
Michal Simek08d0d6f2013-11-21 13:39:02 -08004 */
5#include <common.h>
6#include <command.h>
7#include <clk.h>
Marek Vasutff8eee02018-08-08 22:10:44 +02008#if defined(CONFIG_DM) && defined(CONFIG_CLK)
9#include <dm.h>
Peng Fanaeeb2e62019-08-21 13:35:14 +000010#include <dm/device.h>
11#include <dm/root.h>
Marek Vasutff8eee02018-08-08 22:10:44 +020012#include <dm/device-internal.h>
Peng Fanaeeb2e62019-08-21 13:35:14 +000013#include <linux/clk-provider.h>
Marek Vasutff8eee02018-08-08 22:10:44 +020014#endif
Michal Simek08d0d6f2013-11-21 13:39:02 -080015
Peng Fanaeeb2e62019-08-21 13:35:14 +000016#if defined(CONFIG_DM) && defined(CONFIG_CLK)
17static 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;
23
24 clkp = dev_get_clk_ptr(dev);
25 if (device_get_uclass_id(dev) == UCLASS_CLK && clkp) {
Patrick Delaunay689ca8c2020-07-30 14:04:10 +020026 depth++;
Peng Fanaeeb2e62019-08-21 13:35:14 +000027 rate = clk_get_rate(clkp);
28
Patrick Delaunaycc632842020-07-30 14:04:09 +020029 printf(" %-12u %8d ", rate, clkp->enable_count);
Peng Fanaeeb2e62019-08-21 13:35:14 +000030
Patrick Delaunaycc632842020-07-30 14:04:09 +020031 for (i = depth; i >= 0; i--) {
32 is_last = (last_flag >> i) & 1;
33 if (i) {
34 if (is_last)
35 printf(" ");
36 else
37 printf("| ");
38 } else {
39 if (is_last)
40 printf("`-- ");
41 else
42 printf("|-- ");
43 }
Peng Fanaeeb2e62019-08-21 13:35:14 +000044 }
Peng Fanaeeb2e62019-08-21 13:35:14 +000045
Patrick Delaunaycc632842020-07-30 14:04:09 +020046 printf("%s\n", dev->name);
Peng Fanaeeb2e62019-08-21 13:35:14 +000047 }
48
49 list_for_each_entry(child, &dev->child_head, sibling_node) {
50 is_last = list_is_last(&child->sibling_node, &dev->child_head);
Patrick Delaunay689ca8c2020-07-30 14:04:10 +020051 show_clks(child, depth, (last_flag << 1) | is_last);
Peng Fanaeeb2e62019-08-21 13:35:14 +000052 }
53}
54
Michal Simek08d0d6f2013-11-21 13:39:02 -080055int __weak soc_clk_dump(void)
56{
Peng Fanaeeb2e62019-08-21 13:35:14 +000057 struct udevice *root;
Marek Vasutff8eee02018-08-08 22:10:44 +020058
Peng Fanaeeb2e62019-08-21 13:35:14 +000059 root = dm_root();
60 if (root) {
61 printf(" Rate Usecnt Name\n");
62 printf("------------------------------------------\n");
63 show_clks(root, -1, 0);
Marek Vasutff8eee02018-08-08 22:10:44 +020064 }
65
66 return 0;
Peng Fanaeeb2e62019-08-21 13:35:14 +000067}
Marek Vasutff8eee02018-08-08 22:10:44 +020068#else
Peng Fanaeeb2e62019-08-21 13:35:14 +000069int __weak soc_clk_dump(void)
70{
Michal Simek08d0d6f2013-11-21 13:39:02 -080071 puts("Not implemented\n");
72 return 1;
73}
Peng Fanaeeb2e62019-08-21 13:35:14 +000074#endif
Michal Simek08d0d6f2013-11-21 13:39:02 -080075
Simon Glass09140112020-05-10 11:40:03 -060076static int do_clk_dump(struct cmd_tbl *cmdtp, int flag, int argc,
Michal Simek08d0d6f2013-11-21 13:39:02 -080077 char *const argv[])
78{
Michal Simekebc675b2018-04-19 15:15:25 +020079 int ret;
80
81 ret = soc_clk_dump();
82 if (ret < 0) {
83 printf("Clock dump error %d\n", ret);
84 ret = CMD_RET_FAILURE;
85 }
86
87 return ret;
Michal Simek08d0d6f2013-11-21 13:39:02 -080088}
89
Simon Glass09140112020-05-10 11:40:03 -060090static struct cmd_tbl cmd_clk_sub[] = {
Michal Simek08d0d6f2013-11-21 13:39:02 -080091 U_BOOT_CMD_MKENT(dump, 1, 1, do_clk_dump, "", ""),
92};
93
Simon Glass09140112020-05-10 11:40:03 -060094static int do_clk(struct cmd_tbl *cmdtp, int flag, int argc,
Michal Simek08d0d6f2013-11-21 13:39:02 -080095 char *const argv[])
96{
Simon Glass09140112020-05-10 11:40:03 -060097 struct cmd_tbl *c;
Michal Simek08d0d6f2013-11-21 13:39:02 -080098
99 if (argc < 2)
100 return CMD_RET_USAGE;
101
102 /* Strip off leading 'clk' command argument */
103 argc--;
104 argv++;
105
106 c = find_cmd_tbl(argv[0], &cmd_clk_sub[0], ARRAY_SIZE(cmd_clk_sub));
107
108 if (c)
109 return c->cmd(cmdtp, flag, argc, argv);
110 else
111 return CMD_RET_USAGE;
112}
113
114#ifdef CONFIG_SYS_LONGHELP
115static char clk_help_text[] =
116 "dump - Print clock frequencies";
117#endif
118
119U_BOOT_CMD(clk, 2, 1, do_clk, "CLK sub-system", clk_help_text);