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