Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Michal Simek | 08d0d6f | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Xilinx, Inc. |
Michal Simek | 08d0d6f | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 4 | */ |
| 5 | #include <common.h> |
| 6 | #include <command.h> |
| 7 | #include <clk.h> |
Marek Vasut | ff8eee0 | 2018-08-08 22:10:44 +0200 | [diff] [blame] | 8 | #if defined(CONFIG_DM) && defined(CONFIG_CLK) |
| 9 | #include <dm.h> |
Peng Fan | aeeb2e6 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 10 | #include <dm/device.h> |
| 11 | #include <dm/root.h> |
Marek Vasut | ff8eee0 | 2018-08-08 22:10:44 +0200 | [diff] [blame] | 12 | #include <dm/device-internal.h> |
Peng Fan | aeeb2e6 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 13 | #include <linux/clk-provider.h> |
Marek Vasut | ff8eee0 | 2018-08-08 22:10:44 +0200 | [diff] [blame] | 14 | #endif |
Michal Simek | 08d0d6f | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 15 | |
Peng Fan | aeeb2e6 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 16 | #if defined(CONFIG_DM) && defined(CONFIG_CLK) |
| 17 | static void show_clks(struct udevice *dev, int depth, int last_flag) |
| 18 | { |
| 19 | int i, is_last; |
| 20 | struct udevice *child; |
Tero Kristo | 1a725e2 | 2021-06-11 11:45:07 +0300 | [diff] [blame] | 21 | struct clk *clkp, *parent; |
Peng Fan | aeeb2e6 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 22 | u32 rate; |
| 23 | |
| 24 | clkp = dev_get_clk_ptr(dev); |
| 25 | if (device_get_uclass_id(dev) == UCLASS_CLK && clkp) { |
Tero Kristo | 1a725e2 | 2021-06-11 11:45:07 +0300 | [diff] [blame] | 26 | parent = clk_get_parent(clkp); |
| 27 | if (!IS_ERR(parent) && depth == -1) |
| 28 | return; |
Patrick Delaunay | 689ca8c | 2020-07-30 14:04:10 +0200 | [diff] [blame] | 29 | depth++; |
Peng Fan | aeeb2e6 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 30 | rate = clk_get_rate(clkp); |
| 31 | |
Patrick Delaunay | cc63284 | 2020-07-30 14:04:09 +0200 | [diff] [blame] | 32 | printf(" %-12u %8d ", rate, clkp->enable_count); |
Peng Fan | aeeb2e6 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 33 | |
Patrick Delaunay | cc63284 | 2020-07-30 14:04:09 +0200 | [diff] [blame] | 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 | } |
Peng Fan | aeeb2e6 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 47 | } |
Peng Fan | aeeb2e6 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 48 | |
Patrick Delaunay | cc63284 | 2020-07-30 14:04:09 +0200 | [diff] [blame] | 49 | printf("%s\n", dev->name); |
Peng Fan | aeeb2e6 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | list_for_each_entry(child, &dev->child_head, sibling_node) { |
Tero Kristo | 1a725e2 | 2021-06-11 11:45:07 +0300 | [diff] [blame] | 53 | if (child == dev) |
| 54 | continue; |
| 55 | |
Peng Fan | aeeb2e6 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 56 | is_last = list_is_last(&child->sibling_node, &dev->child_head); |
Patrick Delaunay | 689ca8c | 2020-07-30 14:04:10 +0200 | [diff] [blame] | 57 | show_clks(child, depth, (last_flag << 1) | is_last); |
Peng Fan | aeeb2e6 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
Michal Simek | 08d0d6f | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 61 | int __weak soc_clk_dump(void) |
| 62 | { |
Tero Kristo | 1a725e2 | 2021-06-11 11:45:07 +0300 | [diff] [blame] | 63 | struct udevice *dev; |
| 64 | struct uclass *uc; |
| 65 | int ret; |
Marek Vasut | ff8eee0 | 2018-08-08 22:10:44 +0200 | [diff] [blame] | 66 | |
Tero Kristo | 1a725e2 | 2021-06-11 11:45:07 +0300 | [diff] [blame] | 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); |
Marek Vasut | ff8eee0 | 2018-08-08 22:10:44 +0200 | [diff] [blame] | 76 | |
| 77 | return 0; |
Peng Fan | aeeb2e6 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 78 | } |
Marek Vasut | ff8eee0 | 2018-08-08 22:10:44 +0200 | [diff] [blame] | 79 | #else |
Peng Fan | aeeb2e6 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 80 | int __weak soc_clk_dump(void) |
| 81 | { |
Michal Simek | 08d0d6f | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 82 | puts("Not implemented\n"); |
| 83 | return 1; |
| 84 | } |
Peng Fan | aeeb2e6 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 85 | #endif |
Michal Simek | 08d0d6f | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 86 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 87 | static int do_clk_dump(struct cmd_tbl *cmdtp, int flag, int argc, |
Michal Simek | 08d0d6f | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 88 | char *const argv[]) |
| 89 | { |
Michal Simek | ebc675b | 2018-04-19 15:15:25 +0200 | [diff] [blame] | 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; |
Michal Simek | 08d0d6f | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Tero Kristo | 7ab418f | 2021-06-11 11:45:09 +0300 | [diff] [blame] | 101 | #if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(CLK) |
| 102 | struct udevice *clk_lookup(const char *name) |
| 103 | { |
| 104 | int i = 0; |
| 105 | struct udevice *dev; |
| 106 | |
| 107 | do { |
| 108 | uclass_get_device(UCLASS_CLK, i++, &dev); |
| 109 | if (!strcmp(name, dev->name)) |
| 110 | return dev; |
| 111 | } while (dev); |
| 112 | |
| 113 | return NULL; |
| 114 | } |
| 115 | |
| 116 | static int do_clk_setfreq(struct cmd_tbl *cmdtp, int flag, int argc, |
| 117 | char *const argv[]) |
| 118 | { |
| 119 | struct clk *clk = NULL; |
| 120 | s32 freq; |
| 121 | struct udevice *dev; |
| 122 | |
Simon Glass | 0b1284e | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 123 | freq = dectoul(argv[2], NULL); |
Tero Kristo | 7ab418f | 2021-06-11 11:45:09 +0300 | [diff] [blame] | 124 | |
| 125 | dev = clk_lookup(argv[1]); |
| 126 | |
| 127 | if (dev) |
| 128 | clk = dev_get_clk_ptr(dev); |
| 129 | |
| 130 | if (!clk) { |
| 131 | printf("clock '%s' not found.\n", argv[1]); |
| 132 | return -EINVAL; |
| 133 | } |
| 134 | |
| 135 | freq = clk_set_rate(clk, freq); |
| 136 | if (freq < 0) { |
| 137 | printf("set_rate failed: %d\n", freq); |
| 138 | return CMD_RET_FAILURE; |
| 139 | } |
| 140 | |
| 141 | printf("set_rate returns %u\n", freq); |
| 142 | return 0; |
| 143 | } |
| 144 | #endif |
| 145 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 146 | static struct cmd_tbl cmd_clk_sub[] = { |
Michal Simek | 08d0d6f | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 147 | U_BOOT_CMD_MKENT(dump, 1, 1, do_clk_dump, "", ""), |
Tero Kristo | 7ab418f | 2021-06-11 11:45:09 +0300 | [diff] [blame] | 148 | #if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(CLK) |
| 149 | U_BOOT_CMD_MKENT(setfreq, 3, 1, do_clk_setfreq, "", ""), |
| 150 | #endif |
Michal Simek | 08d0d6f | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 151 | }; |
| 152 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 153 | static int do_clk(struct cmd_tbl *cmdtp, int flag, int argc, |
Michal Simek | 08d0d6f | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 154 | char *const argv[]) |
| 155 | { |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 156 | struct cmd_tbl *c; |
Michal Simek | 08d0d6f | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 157 | |
| 158 | if (argc < 2) |
| 159 | return CMD_RET_USAGE; |
| 160 | |
| 161 | /* Strip off leading 'clk' command argument */ |
| 162 | argc--; |
| 163 | argv++; |
| 164 | |
| 165 | c = find_cmd_tbl(argv[0], &cmd_clk_sub[0], ARRAY_SIZE(cmd_clk_sub)); |
| 166 | |
| 167 | if (c) |
| 168 | return c->cmd(cmdtp, flag, argc, argv); |
| 169 | else |
| 170 | return CMD_RET_USAGE; |
| 171 | } |
| 172 | |
| 173 | #ifdef CONFIG_SYS_LONGHELP |
| 174 | static char clk_help_text[] = |
Tero Kristo | 7ab418f | 2021-06-11 11:45:09 +0300 | [diff] [blame] | 175 | "dump - Print clock frequencies\n" |
| 176 | "setfreq [clk] [freq] - Set clock frequency"; |
Michal Simek | 08d0d6f | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 177 | #endif |
| 178 | |
Tero Kristo | 7ab418f | 2021-06-11 11:45:09 +0300 | [diff] [blame] | 179 | U_BOOT_CMD(clk, 4, 1, do_clk, "CLK sub-system", clk_help_text); |