]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
08d0d6f3 MS |
2 | /* |
3 | * Copyright (C) 2013 Xilinx, Inc. | |
08d0d6f3 MS |
4 | */ |
5 | #include <common.h> | |
6 | #include <command.h> | |
7 | #include <clk.h> | |
ff8eee03 MV |
8 | #if defined(CONFIG_DM) && defined(CONFIG_CLK) |
9 | #include <dm.h> | |
aeeb2e6d PF |
10 | #include <dm/device.h> |
11 | #include <dm/root.h> | |
ff8eee03 | 12 | #include <dm/device-internal.h> |
aeeb2e6d | 13 | #include <linux/clk-provider.h> |
ff8eee03 | 14 | #endif |
08d0d6f3 | 15 | |
ff8eee03 | 16 | #if defined(CONFIG_DM) && defined(CONFIG_CLK) |
aeeb2e6d PF |
17 | static void show_clks(struct udevice *dev, int depth, int last_flag) |
18 | { | |
19 | int i, is_last; | |
20 | struct udevice *child; | |
1a725e22 | 21 | struct clk *clkp, *parent; |
aeeb2e6d PF |
22 | u32 rate; |
23 | ||
24 | clkp = dev_get_clk_ptr(dev); | |
25 | if (device_get_uclass_id(dev) == UCLASS_CLK && clkp) { | |
1a725e22 TK |
26 | parent = clk_get_parent(clkp); |
27 | if (!IS_ERR(parent) && depth == -1) | |
28 | return; | |
689ca8c5 | 29 | depth++; |
aeeb2e6d PF |
30 | rate = clk_get_rate(clkp); |
31 | ||
cc632841 PD |
32 | printf(" %-12u %8d ", rate, clkp->enable_count); |
33 | ||
34 | 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 | } | |
aeeb2e6d | 47 | } |
ff8eee03 | 48 | |
cc632841 | 49 | printf("%s\n", dev->name); |
aeeb2e6d | 50 | } |
ff8eee03 | 51 | |
aeeb2e6d | 52 | list_for_each_entry(child, &dev->child_head, sibling_node) { |
1a725e22 TK |
53 | if (child == dev) |
54 | continue; | |
55 | ||
aeeb2e6d | 56 | is_last = list_is_last(&child->sibling_node, &dev->child_head); |
689ca8c5 | 57 | show_clks(child, depth, (last_flag << 1) | is_last); |
aeeb2e6d PF |
58 | } |
59 | } | |
80b44fb3 | 60 | |
aeeb2e6d PF |
61 | int __weak soc_clk_dump(void) |
62 | { | |
1a725e22 TK |
63 | struct udevice *dev; |
64 | struct uclass *uc; | |
65 | int ret; | |
80b44fb3 | 66 | |
1a725e22 TK |
67 | 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); | |
ff8eee03 MV |
76 | |
77 | return 0; | |
aeeb2e6d | 78 | } |
ff8eee03 | 79 | #else |
aeeb2e6d PF |
80 | int __weak soc_clk_dump(void) |
81 | { | |
08d0d6f3 MS |
82 | puts("Not implemented\n"); |
83 | return 1; | |
84 | } | |
aeeb2e6d | 85 | #endif |
08d0d6f3 | 86 | |
09140113 | 87 | static int do_clk_dump(struct cmd_tbl *cmdtp, int flag, int argc, |
08d0d6f3 MS |
88 | char *const argv[]) |
89 | { | |
ebc675b9 MS |
90 | 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; | |
08d0d6f3 MS |
99 | } |
100 | ||
7ab418fb | 101 | #if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(CLK) |
7ab418fb TK |
102 | static int do_clk_setfreq(struct cmd_tbl *cmdtp, int flag, int argc, |
103 | char *const argv[]) | |
104 | { | |
105 | struct clk *clk = NULL; | |
106 | s32 freq; | |
107 | struct udevice *dev; | |
108 | ||
3386fb1e PD |
109 | if (argc != 3) |
110 | return CMD_RET_USAGE; | |
111 | ||
0b1284eb | 112 | freq = dectoul(argv[2], NULL); |
7ab418fb | 113 | |
afcc2614 | 114 | if (!uclass_get_device_by_name(UCLASS_CLK, argv[1], &dev)) |
7ab418fb TK |
115 | clk = dev_get_clk_ptr(dev); |
116 | ||
117 | if (!clk) { | |
118 | printf("clock '%s' not found.\n", argv[1]); | |
534859ac | 119 | return CMD_RET_FAILURE; |
7ab418fb TK |
120 | } |
121 | ||
122 | freq = clk_set_rate(clk, freq); | |
123 | if (freq < 0) { | |
124 | printf("set_rate failed: %d\n", freq); | |
125 | return CMD_RET_FAILURE; | |
126 | } | |
127 | ||
128 | printf("set_rate returns %u\n", freq); | |
129 | return 0; | |
130 | } | |
131 | #endif | |
132 | ||
09140113 | 133 | static struct cmd_tbl cmd_clk_sub[] = { |
08d0d6f3 | 134 | U_BOOT_CMD_MKENT(dump, 1, 1, do_clk_dump, "", ""), |
7ab418fb TK |
135 | #if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(CLK) |
136 | U_BOOT_CMD_MKENT(setfreq, 3, 1, do_clk_setfreq, "", ""), | |
137 | #endif | |
08d0d6f3 MS |
138 | }; |
139 | ||
09140113 | 140 | static int do_clk(struct cmd_tbl *cmdtp, int flag, int argc, |
08d0d6f3 MS |
141 | char *const argv[]) |
142 | { | |
09140113 | 143 | struct cmd_tbl *c; |
08d0d6f3 MS |
144 | |
145 | if (argc < 2) | |
146 | return CMD_RET_USAGE; | |
147 | ||
148 | /* Strip off leading 'clk' command argument */ | |
149 | argc--; | |
150 | argv++; | |
151 | ||
152 | c = find_cmd_tbl(argv[0], &cmd_clk_sub[0], ARRAY_SIZE(cmd_clk_sub)); | |
153 | ||
154 | if (c) | |
155 | return c->cmd(cmdtp, flag, argc, argv); | |
156 | else | |
157 | return CMD_RET_USAGE; | |
158 | } | |
159 | ||
160 | #ifdef CONFIG_SYS_LONGHELP | |
161 | static char clk_help_text[] = | |
7ab418fb | 162 | "dump - Print clock frequencies\n" |
92a1bba8 | 163 | "clk setfreq [clk] [freq] - Set clock frequency"; |
08d0d6f3 MS |
164 | #endif |
165 | ||
7ab418fb | 166 | U_BOOT_CMD(clk, 4, 1, do_clk, "CLK sub-system", clk_help_text); |