Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Michal Simek | a0d2802 | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Xilinx, Inc. |
Michal Simek | a0d2802 | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 4 | */ |
| 5 | #include <common.h> |
| 6 | #include <command.h> |
| 7 | #include <clk.h> |
Marek Vasut | 9254f99 | 2018-08-08 22:10:44 +0200 | [diff] [blame] | 8 | #if defined(CONFIG_DM) && defined(CONFIG_CLK) |
| 9 | #include <dm.h> |
Peng Fan | 51709f7 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 10 | #include <dm/device.h> |
| 11 | #include <dm/root.h> |
Marek Vasut | 9254f99 | 2018-08-08 22:10:44 +0200 | [diff] [blame] | 12 | #include <dm/device-internal.h> |
Peng Fan | 51709f7 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 13 | #include <linux/clk-provider.h> |
Marek Vasut | 9254f99 | 2018-08-08 22:10:44 +0200 | [diff] [blame] | 14 | #endif |
Michal Simek | a0d2802 | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 15 | |
Marek Vasut | 9254f99 | 2018-08-08 22:10:44 +0200 | [diff] [blame] | 16 | #if defined(CONFIG_DM) && defined(CONFIG_CLK) |
Peng Fan | 51709f7 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 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 | 1a22eba | 2021-06-11 11:45:07 +0300 | [diff] [blame] | 21 | struct clk *clkp, *parent; |
Peng Fan | 51709f7 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 22 | u32 rate; |
Marek Vasut | 9254f99 | 2018-08-08 22:10:44 +0200 | [diff] [blame] | 23 | |
Peng Fan | 51709f7 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 24 | clkp = dev_get_clk_ptr(dev); |
Patrick Delaunay | aac5b9a | 2022-12-13 14:57:10 +0100 | [diff] [blame] | 25 | if (clkp) { |
Tero Kristo | 1a22eba | 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 | 0ca4cc0 | 2020-07-30 14:04:10 +0200 | [diff] [blame] | 29 | depth++; |
Peng Fan | 51709f7 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 30 | rate = clk_get_rate(clkp); |
Marek Vasut | 9254f99 | 2018-08-08 22:10:44 +0200 | [diff] [blame] | 31 | |
Patrick Delaunay | 15c5a34 | 2020-07-30 14:04:09 +0200 | [diff] [blame] | 32 | printf(" %-12u %8d ", rate, clkp->enable_count); |
Marek Vasut | 9254f99 | 2018-08-08 22:10:44 +0200 | [diff] [blame] | 33 | |
Patrick Delaunay | 15c5a34 | 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 | 51709f7 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 47 | } |
Marek Vasut | 9254f99 | 2018-08-08 22:10:44 +0200 | [diff] [blame] | 48 | |
Patrick Delaunay | 15c5a34 | 2020-07-30 14:04:09 +0200 | [diff] [blame] | 49 | printf("%s\n", dev->name); |
Peng Fan | 51709f7 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 50 | } |
Ismael Luceno Cortes | 8b5d3a4 | 2019-03-18 12:27:32 +0000 | [diff] [blame] | 51 | |
Patrick Delaunay | aac5b9a | 2022-12-13 14:57:10 +0100 | [diff] [blame] | 52 | device_foreach_child_probe(child, dev) { |
| 53 | if (device_get_uclass_id(child) != UCLASS_CLK) |
| 54 | continue; |
Tero Kristo | 1a22eba | 2021-06-11 11:45:07 +0300 | [diff] [blame] | 55 | if (child == dev) |
| 56 | continue; |
Peng Fan | 51709f7 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 57 | is_last = list_is_last(&child->sibling_node, &dev->child_head); |
Patrick Delaunay | 0ca4cc0 | 2020-07-30 14:04:10 +0200 | [diff] [blame] | 58 | show_clks(child, depth, (last_flag << 1) | is_last); |
Peng Fan | 51709f7 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 59 | } |
| 60 | } |
Ismael Luceno Cortes | 8b5d3a4 | 2019-03-18 12:27:32 +0000 | [diff] [blame] | 61 | |
Peng Fan | 51709f7 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 62 | int __weak soc_clk_dump(void) |
| 63 | { |
Tero Kristo | 1a22eba | 2021-06-11 11:45:07 +0300 | [diff] [blame] | 64 | struct udevice *dev; |
Tero Kristo | 1a22eba | 2021-06-11 11:45:07 +0300 | [diff] [blame] | 65 | |
| 66 | printf(" Rate Usecnt Name\n"); |
| 67 | printf("------------------------------------------\n"); |
| 68 | |
Patrick Delaunay | aac5b9a | 2022-12-13 14:57:10 +0100 | [diff] [blame] | 69 | uclass_foreach_dev_probe(UCLASS_CLK, dev) |
Tero Kristo | 1a22eba | 2021-06-11 11:45:07 +0300 | [diff] [blame] | 70 | show_clks(dev, -1, 0); |
Marek Vasut | 9254f99 | 2018-08-08 22:10:44 +0200 | [diff] [blame] | 71 | |
| 72 | return 0; |
Peng Fan | 51709f7 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 73 | } |
Marek Vasut | 9254f99 | 2018-08-08 22:10:44 +0200 | [diff] [blame] | 74 | #else |
Peng Fan | 51709f7 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 75 | int __weak soc_clk_dump(void) |
| 76 | { |
Michal Simek | a0d2802 | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 77 | puts("Not implemented\n"); |
| 78 | return 1; |
| 79 | } |
Peng Fan | 51709f7 | 2019-08-21 13:35:14 +0000 | [diff] [blame] | 80 | #endif |
Michal Simek | a0d2802 | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 81 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 82 | static int do_clk_dump(struct cmd_tbl *cmdtp, int flag, int argc, |
Michal Simek | a0d2802 | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 83 | char *const argv[]) |
| 84 | { |
Michal Simek | 7f6992b | 2018-04-19 15:15:25 +0200 | [diff] [blame] | 85 | 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 Simek | a0d2802 | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Tero Kristo | 4bd4cb4 | 2021-06-11 11:45:09 +0300 | [diff] [blame] | 96 | #if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(CLK) |
Tero Kristo | 4bd4cb4 | 2021-06-11 11:45:09 +0300 | [diff] [blame] | 97 | static 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 Delaunay | 5c6d32d | 2022-01-31 17:21:37 +0100 | [diff] [blame] | 104 | if (argc != 3) |
| 105 | return CMD_RET_USAGE; |
| 106 | |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 107 | freq = dectoul(argv[2], NULL); |
Tero Kristo | 4bd4cb4 | 2021-06-11 11:45:09 +0300 | [diff] [blame] | 108 | |
Patrick Delaunay | 27e14d4 | 2022-01-31 17:21:38 +0100 | [diff] [blame] | 109 | if (!uclass_get_device_by_name(UCLASS_CLK, argv[1], &dev)) |
Tero Kristo | 4bd4cb4 | 2021-06-11 11:45:09 +0300 | [diff] [blame] | 110 | clk = dev_get_clk_ptr(dev); |
| 111 | |
| 112 | if (!clk) { |
| 113 | printf("clock '%s' not found.\n", argv[1]); |
Patrick Delaunay | fe23c80 | 2022-01-31 17:21:39 +0100 | [diff] [blame] | 114 | return CMD_RET_FAILURE; |
Tero Kristo | 4bd4cb4 | 2021-06-11 11:45:09 +0300 | [diff] [blame] | 115 | } |
| 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 Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 128 | static struct cmd_tbl cmd_clk_sub[] = { |
Michal Simek | a0d2802 | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 129 | U_BOOT_CMD_MKENT(dump, 1, 1, do_clk_dump, "", ""), |
Tero Kristo | 4bd4cb4 | 2021-06-11 11:45:09 +0300 | [diff] [blame] | 130 | #if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(CLK) |
| 131 | U_BOOT_CMD_MKENT(setfreq, 3, 1, do_clk_setfreq, "", ""), |
| 132 | #endif |
Michal Simek | a0d2802 | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 133 | }; |
| 134 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 135 | static int do_clk(struct cmd_tbl *cmdtp, int flag, int argc, |
Michal Simek | a0d2802 | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 136 | char *const argv[]) |
| 137 | { |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 138 | struct cmd_tbl *c; |
Michal Simek | a0d2802 | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 139 | |
| 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 | |
Tom Rini | 03f146c | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 155 | U_BOOT_LONGHELP(clk, |
Tero Kristo | 4bd4cb4 | 2021-06-11 11:45:09 +0300 | [diff] [blame] | 156 | "dump - Print clock frequencies\n" |
Tom Rini | 03f146c | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 157 | "clk setfreq [clk] [freq] - Set clock frequency"); |
Michal Simek | a0d2802 | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 158 | |
Tero Kristo | 4bd4cb4 | 2021-06-11 11:45:09 +0300 | [diff] [blame] | 159 | U_BOOT_CMD(clk, 4, 1, do_clk, "CLK sub-system", clk_help_text); |