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