blob: ff7c7649a159b9d63ba623a92187c5153d8db1dd [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;
Tero Kristo1a725e22021-06-11 11:45:07 +030021 struct clk *clkp, *parent;
Peng Fanaeeb2e62019-08-21 13:35:14 +000022 u32 rate;
23
24 clkp = dev_get_clk_ptr(dev);
Patrick Delaunayc40251c2022-12-13 14:57:10 +010025 if (clkp) {
Tero Kristo1a725e22021-06-11 11:45:07 +030026 parent = clk_get_parent(clkp);
27 if (!IS_ERR(parent) && depth == -1)
28 return;
Patrick Delaunay689ca8c2020-07-30 14:04:10 +020029 depth++;
Peng Fanaeeb2e62019-08-21 13:35:14 +000030 rate = clk_get_rate(clkp);
31
Patrick Delaunaycc632842020-07-30 14:04:09 +020032 printf(" %-12u %8d ", rate, clkp->enable_count);
Peng Fanaeeb2e62019-08-21 13:35:14 +000033
Patrick Delaunaycc632842020-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 Fanaeeb2e62019-08-21 13:35:14 +000047 }
Peng Fanaeeb2e62019-08-21 13:35:14 +000048
Patrick Delaunaycc632842020-07-30 14:04:09 +020049 printf("%s\n", dev->name);
Peng Fanaeeb2e62019-08-21 13:35:14 +000050 }
51
Patrick Delaunayc40251c2022-12-13 14:57:10 +010052 device_foreach_child_probe(child, dev) {
53 if (device_get_uclass_id(child) != UCLASS_CLK)
54 continue;
Tero Kristo1a725e22021-06-11 11:45:07 +030055 if (child == dev)
56 continue;
Peng Fanaeeb2e62019-08-21 13:35:14 +000057 is_last = list_is_last(&child->sibling_node, &dev->child_head);
Patrick Delaunay689ca8c2020-07-30 14:04:10 +020058 show_clks(child, depth, (last_flag << 1) | is_last);
Peng Fanaeeb2e62019-08-21 13:35:14 +000059 }
60}
61
Michal Simek08d0d6f2013-11-21 13:39:02 -080062int __weak soc_clk_dump(void)
63{
Tero Kristo1a725e22021-06-11 11:45:07 +030064 struct udevice *dev;
Tero Kristo1a725e22021-06-11 11:45:07 +030065
66 printf(" Rate Usecnt Name\n");
67 printf("------------------------------------------\n");
68
Patrick Delaunayc40251c2022-12-13 14:57:10 +010069 uclass_foreach_dev_probe(UCLASS_CLK, dev)
Tero Kristo1a725e22021-06-11 11:45:07 +030070 show_clks(dev, -1, 0);
Marek Vasutff8eee02018-08-08 22:10:44 +020071
72 return 0;
Peng Fanaeeb2e62019-08-21 13:35:14 +000073}
Marek Vasutff8eee02018-08-08 22:10:44 +020074#else
Peng Fanaeeb2e62019-08-21 13:35:14 +000075int __weak soc_clk_dump(void)
76{
Michal Simek08d0d6f2013-11-21 13:39:02 -080077 puts("Not implemented\n");
78 return 1;
79}
Peng Fanaeeb2e62019-08-21 13:35:14 +000080#endif
Michal Simek08d0d6f2013-11-21 13:39:02 -080081
Simon Glass09140112020-05-10 11:40:03 -060082static int do_clk_dump(struct cmd_tbl *cmdtp, int flag, int argc,
Michal Simek08d0d6f2013-11-21 13:39:02 -080083 char *const argv[])
84{
Michal Simekebc675b2018-04-19 15:15:25 +020085 int ret;
86
87 ret = soc_clk_dump();
88 if (ret < 0) {
89 printf("Clock dump error %d\n", ret);
90 ret = CMD_RET_FAILURE;
91 }
92
93 return ret;
Michal Simek08d0d6f2013-11-21 13:39:02 -080094}
95
Tero Kristo7ab418f2021-06-11 11:45:09 +030096#if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(CLK)
Tero Kristo7ab418f2021-06-11 11:45:09 +030097static int do_clk_setfreq(struct cmd_tbl *cmdtp, int flag, int argc,
98 char *const argv[])
99{
100 struct clk *clk = NULL;
101 s32 freq;
102 struct udevice *dev;
103
Patrick Delaunay3386fb12022-01-31 17:21:37 +0100104 if (argc != 3)
105 return CMD_RET_USAGE;
106
Simon Glass0b1284e2021-07-24 09:03:30 -0600107 freq = dectoul(argv[2], NULL);
Tero Kristo7ab418f2021-06-11 11:45:09 +0300108
Patrick Delaunayafcc2612022-01-31 17:21:38 +0100109 if (!uclass_get_device_by_name(UCLASS_CLK, argv[1], &dev))
Tero Kristo7ab418f2021-06-11 11:45:09 +0300110 clk = dev_get_clk_ptr(dev);
111
112 if (!clk) {
113 printf("clock '%s' not found.\n", argv[1]);
Patrick Delaunay534859a2022-01-31 17:21:39 +0100114 return CMD_RET_FAILURE;
Tero Kristo7ab418f2021-06-11 11:45:09 +0300115 }
116
117 freq = clk_set_rate(clk, freq);
118 if (freq < 0) {
119 printf("set_rate failed: %d\n", freq);
120 return CMD_RET_FAILURE;
121 }
122
123 printf("set_rate returns %u\n", freq);
124 return 0;
125}
126#endif
127
Simon Glass09140112020-05-10 11:40:03 -0600128static struct cmd_tbl cmd_clk_sub[] = {
Michal Simek08d0d6f2013-11-21 13:39:02 -0800129 U_BOOT_CMD_MKENT(dump, 1, 1, do_clk_dump, "", ""),
Tero Kristo7ab418f2021-06-11 11:45:09 +0300130#if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(CLK)
131 U_BOOT_CMD_MKENT(setfreq, 3, 1, do_clk_setfreq, "", ""),
132#endif
Michal Simek08d0d6f2013-11-21 13:39:02 -0800133};
134
Simon Glass09140112020-05-10 11:40:03 -0600135static int do_clk(struct cmd_tbl *cmdtp, int flag, int argc,
Michal Simek08d0d6f2013-11-21 13:39:02 -0800136 char *const argv[])
137{
Simon Glass09140112020-05-10 11:40:03 -0600138 struct cmd_tbl *c;
Michal Simek08d0d6f2013-11-21 13:39:02 -0800139
140 if (argc < 2)
141 return CMD_RET_USAGE;
142
143 /* Strip off leading 'clk' command argument */
144 argc--;
145 argv++;
146
147 c = find_cmd_tbl(argv[0], &cmd_clk_sub[0], ARRAY_SIZE(cmd_clk_sub));
148
149 if (c)
150 return c->cmd(cmdtp, flag, argc, argv);
151 else
152 return CMD_RET_USAGE;
153}
154
155#ifdef CONFIG_SYS_LONGHELP
156static char clk_help_text[] =
Tero Kristo7ab418f2021-06-11 11:45:09 +0300157 "dump - Print clock frequencies\n"
Patrick Delaunay92a1bba2022-01-31 17:21:40 +0100158 "clk setfreq [clk] [freq] - Set clock frequency";
Michal Simek08d0d6f2013-11-21 13:39:02 -0800159#endif
160
Tero Kristo7ab418f2021-06-11 11:45:09 +0300161U_BOOT_CMD(clk, 4, 1, do_clk, "CLK sub-system", clk_help_text);