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 | */ |
Michal Simek | 08d0d6f | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 5 | #include <command.h> |
| 6 | #include <clk.h> |
Marek Vasut | ff8eee0 | 2018-08-08 22:10:44 +0200 | [diff] [blame] | 7 | #if defined(CONFIG_DM) && defined(CONFIG_CLK) |
| 8 | #include <dm.h> |
Peng Fan | aeeb2e6 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 9 | #include <dm/device.h> |
| 10 | #include <dm/root.h> |
Marek Vasut | ff8eee0 | 2018-08-08 22:10:44 +0200 | [diff] [blame] | 11 | #include <dm/device-internal.h> |
Peng Fan | aeeb2e6 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 12 | #include <linux/clk-provider.h> |
Marek Vasut | ff8eee0 | 2018-08-08 22:10:44 +0200 | [diff] [blame] | 13 | #endif |
Michal Simek | 08d0d6f | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 14 | |
Peng Fan | aeeb2e6 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 15 | #if defined(CONFIG_DM) && defined(CONFIG_CLK) |
| 16 | static void show_clks(struct udevice *dev, int depth, int last_flag) |
| 17 | { |
| 18 | int i, is_last; |
| 19 | struct udevice *child; |
Tero Kristo | 1a725e2 | 2021-06-11 11:45:07 +0300 | [diff] [blame] | 20 | struct clk *clkp, *parent; |
Peng Fan | aeeb2e6 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 21 | u32 rate; |
| 22 | |
| 23 | clkp = dev_get_clk_ptr(dev); |
Patrick Delaunay | c40251c | 2022-12-13 14:57:10 +0100 | [diff] [blame] | 24 | if (clkp) { |
Tero Kristo | 1a725e2 | 2021-06-11 11:45:07 +0300 | [diff] [blame] | 25 | parent = clk_get_parent(clkp); |
| 26 | if (!IS_ERR(parent) && depth == -1) |
| 27 | return; |
Patrick Delaunay | 689ca8c | 2020-07-30 14:04:10 +0200 | [diff] [blame] | 28 | depth++; |
Peng Fan | aeeb2e6 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 29 | rate = clk_get_rate(clkp); |
| 30 | |
Patrick Delaunay | cc63284 | 2020-07-30 14:04:09 +0200 | [diff] [blame] | 31 | printf(" %-12u %8d ", rate, clkp->enable_count); |
Peng Fan | aeeb2e6 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 32 | |
Patrick Delaunay | cc63284 | 2020-07-30 14:04:09 +0200 | [diff] [blame] | 33 | for (i = depth; i >= 0; i--) { |
| 34 | is_last = (last_flag >> i) & 1; |
| 35 | if (i) { |
| 36 | if (is_last) |
| 37 | printf(" "); |
| 38 | else |
| 39 | printf("| "); |
| 40 | } else { |
| 41 | if (is_last) |
| 42 | printf("`-- "); |
| 43 | else |
| 44 | printf("|-- "); |
| 45 | } |
Peng Fan | aeeb2e6 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 46 | } |
Peng Fan | aeeb2e6 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 47 | |
Patrick Delaunay | cc63284 | 2020-07-30 14:04:09 +0200 | [diff] [blame] | 48 | printf("%s\n", dev->name); |
Peng Fan | aeeb2e6 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Patrick Delaunay | c40251c | 2022-12-13 14:57:10 +0100 | [diff] [blame] | 51 | device_foreach_child_probe(child, dev) { |
| 52 | if (device_get_uclass_id(child) != UCLASS_CLK) |
| 53 | continue; |
Tero Kristo | 1a725e2 | 2021-06-11 11:45:07 +0300 | [diff] [blame] | 54 | if (child == dev) |
| 55 | continue; |
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 | |
Igor Prusov | 5666558 | 2023-11-09 13:55:16 +0300 | [diff] [blame] | 61 | static int soc_clk_dump(void) |
Michal Simek | 08d0d6f | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 62 | { |
Tero Kristo | 1a725e2 | 2021-06-11 11:45:07 +0300 | [diff] [blame] | 63 | struct udevice *dev; |
Igor Prusov | 258c100 | 2023-11-09 13:55:14 +0300 | [diff] [blame] | 64 | const struct clk_ops *ops; |
Tero Kristo | 1a725e2 | 2021-06-11 11:45:07 +0300 | [diff] [blame] | 65 | |
| 66 | printf(" Rate Usecnt Name\n"); |
| 67 | printf("------------------------------------------\n"); |
| 68 | |
Patrick Delaunay | c40251c | 2022-12-13 14:57:10 +0100 | [diff] [blame] | 69 | uclass_foreach_dev_probe(UCLASS_CLK, dev) |
Tero Kristo | 1a725e2 | 2021-06-11 11:45:07 +0300 | [diff] [blame] | 70 | show_clks(dev, -1, 0); |
Marek Vasut | ff8eee0 | 2018-08-08 22:10:44 +0200 | [diff] [blame] | 71 | |
Igor Prusov | 258c100 | 2023-11-09 13:55:14 +0300 | [diff] [blame] | 72 | uclass_foreach_dev_probe(UCLASS_CLK, dev) { |
| 73 | ops = dev_get_driver_ops(dev); |
| 74 | if (ops && ops->dump) { |
| 75 | printf("\n%s %s:\n", dev->driver->name, dev->name); |
| 76 | ops->dump(dev); |
| 77 | } |
| 78 | } |
| 79 | |
Marek Vasut | ff8eee0 | 2018-08-08 22:10:44 +0200 | [diff] [blame] | 80 | return 0; |
Peng Fan | aeeb2e6 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 81 | } |
Marek Vasut | ff8eee0 | 2018-08-08 22:10:44 +0200 | [diff] [blame] | 82 | #else |
Igor Prusov | 5666558 | 2023-11-09 13:55:16 +0300 | [diff] [blame] | 83 | static int soc_clk_dump(void) |
Peng Fan | aeeb2e6 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 84 | { |
Michal Simek | 08d0d6f | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 85 | puts("Not implemented\n"); |
| 86 | return 1; |
| 87 | } |
Peng Fan | aeeb2e6 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 88 | #endif |
Michal Simek | 08d0d6f | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 89 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 90 | 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] | 91 | char *const argv[]) |
| 92 | { |
Michal Simek | ebc675b | 2018-04-19 15:15:25 +0200 | [diff] [blame] | 93 | int ret; |
| 94 | |
| 95 | ret = soc_clk_dump(); |
| 96 | if (ret < 0) { |
| 97 | printf("Clock dump error %d\n", ret); |
| 98 | ret = CMD_RET_FAILURE; |
| 99 | } |
| 100 | |
| 101 | return ret; |
Michal Simek | 08d0d6f | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Tero Kristo | 7ab418f | 2021-06-11 11:45:09 +0300 | [diff] [blame] | 104 | #if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(CLK) |
Tero Kristo | 7ab418f | 2021-06-11 11:45:09 +0300 | [diff] [blame] | 105 | static int do_clk_setfreq(struct cmd_tbl *cmdtp, int flag, int argc, |
| 106 | char *const argv[]) |
| 107 | { |
| 108 | struct clk *clk = NULL; |
| 109 | s32 freq; |
| 110 | struct udevice *dev; |
| 111 | |
Patrick Delaunay | 3386fb1 | 2022-01-31 17:21:37 +0100 | [diff] [blame] | 112 | if (argc != 3) |
| 113 | return CMD_RET_USAGE; |
| 114 | |
Simon Glass | 0b1284e | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 115 | freq = dectoul(argv[2], NULL); |
Tero Kristo | 7ab418f | 2021-06-11 11:45:09 +0300 | [diff] [blame] | 116 | |
Patrick Delaunay | afcc261 | 2022-01-31 17:21:38 +0100 | [diff] [blame] | 117 | if (!uclass_get_device_by_name(UCLASS_CLK, argv[1], &dev)) |
Tero Kristo | 7ab418f | 2021-06-11 11:45:09 +0300 | [diff] [blame] | 118 | clk = dev_get_clk_ptr(dev); |
| 119 | |
| 120 | if (!clk) { |
| 121 | printf("clock '%s' not found.\n", argv[1]); |
Patrick Delaunay | 534859a | 2022-01-31 17:21:39 +0100 | [diff] [blame] | 122 | return CMD_RET_FAILURE; |
Tero Kristo | 7ab418f | 2021-06-11 11:45:09 +0300 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | freq = clk_set_rate(clk, freq); |
| 126 | if (freq < 0) { |
| 127 | printf("set_rate failed: %d\n", freq); |
| 128 | return CMD_RET_FAILURE; |
| 129 | } |
| 130 | |
| 131 | printf("set_rate returns %u\n", freq); |
| 132 | return 0; |
| 133 | } |
| 134 | #endif |
| 135 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 136 | static struct cmd_tbl cmd_clk_sub[] = { |
Michal Simek | 08d0d6f | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 137 | U_BOOT_CMD_MKENT(dump, 1, 1, do_clk_dump, "", ""), |
Tero Kristo | 7ab418f | 2021-06-11 11:45:09 +0300 | [diff] [blame] | 138 | #if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(CLK) |
| 139 | U_BOOT_CMD_MKENT(setfreq, 3, 1, do_clk_setfreq, "", ""), |
| 140 | #endif |
Michal Simek | 08d0d6f | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 141 | }; |
| 142 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 143 | static int do_clk(struct cmd_tbl *cmdtp, int flag, int argc, |
Michal Simek | 08d0d6f | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 144 | char *const argv[]) |
| 145 | { |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 146 | struct cmd_tbl *c; |
Michal Simek | 08d0d6f | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 147 | |
| 148 | if (argc < 2) |
| 149 | return CMD_RET_USAGE; |
| 150 | |
| 151 | /* Strip off leading 'clk' command argument */ |
| 152 | argc--; |
| 153 | argv++; |
| 154 | |
| 155 | c = find_cmd_tbl(argv[0], &cmd_clk_sub[0], ARRAY_SIZE(cmd_clk_sub)); |
| 156 | |
| 157 | if (c) |
| 158 | return c->cmd(cmdtp, flag, argc, argv); |
| 159 | else |
| 160 | return CMD_RET_USAGE; |
| 161 | } |
| 162 | |
Tom Rini | 3616218 | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 163 | U_BOOT_LONGHELP(clk, |
Tero Kristo | 7ab418f | 2021-06-11 11:45:09 +0300 | [diff] [blame] | 164 | "dump - Print clock frequencies\n" |
Tom Rini | 3616218 | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 165 | "clk setfreq [clk] [freq] - Set clock frequency"); |
Michal Simek | 08d0d6f | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 166 | |
Tero Kristo | 7ab418f | 2021-06-11 11:45:09 +0300 | [diff] [blame] | 167 | U_BOOT_CMD(clk, 4, 1, do_clk, "CLK sub-system", clk_help_text); |