xref: /openbmc/u-boot/cmd/clk.c (revision c00d0012)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2013 Xilinx, Inc.
4  */
5 #include <common.h>
6 #include <command.h>
7 #include <clk.h>
8 #if defined(CONFIG_DM) && defined(CONFIG_CLK)
9 #include <dm.h>
10 #include <dm/device-internal.h>
11 #endif
12 
13 int __weak soc_clk_dump(void)
14 {
15 #if defined(CONFIG_DM) && defined(CONFIG_CLK)
16 	struct udevice *dev;
17 	struct uclass *uc;
18 	struct clk clk;
19 	int ret;
20 
21 	/* Device addresses start at 1 */
22 	ret = uclass_get(UCLASS_CLK, &uc);
23 	if (ret)
24 		return ret;
25 
26 	uclass_foreach_dev(dev, uc) {
27 		memset(&clk, 0, sizeof(clk));
28 		ret = device_probe(dev);
29 		if (ret) {
30 			printf("%-30.30s : ? Hz\n", dev->name);
31 			continue;
32 		}
33 
34 		ret = clk_request(dev, &clk);
35 		if (ret) {
36 			printf("%-30.30s : ? Hz\n", dev->name);
37 			continue;
38 		}
39 
40 		printf("%-30.30s : %lu Hz\n", dev->name, clk_get_rate(&clk));
41 
42 		clk_free(&clk);
43 	}
44 
45 	return 0;
46 #else
47 	puts("Not implemented\n");
48 	return 1;
49 #endif
50 }
51 
52 static int do_clk_dump(cmd_tbl_t *cmdtp, int flag, int argc,
53 		       char *const argv[])
54 {
55 	int ret;
56 
57 	ret = soc_clk_dump();
58 	if (ret < 0) {
59 		printf("Clock dump error %d\n", ret);
60 		ret = CMD_RET_FAILURE;
61 	}
62 
63 	return ret;
64 }
65 
66 static cmd_tbl_t cmd_clk_sub[] = {
67 	U_BOOT_CMD_MKENT(dump, 1, 1, do_clk_dump, "", ""),
68 };
69 
70 static int do_clk(cmd_tbl_t *cmdtp, int flag, int argc,
71 		  char *const argv[])
72 {
73 	cmd_tbl_t *c;
74 
75 	if (argc < 2)
76 		return CMD_RET_USAGE;
77 
78 	/* Strip off leading 'clk' command argument */
79 	argc--;
80 	argv++;
81 
82 	c = find_cmd_tbl(argv[0], &cmd_clk_sub[0], ARRAY_SIZE(cmd_clk_sub));
83 
84 	if (c)
85 		return c->cmd(cmdtp, flag, argc, argv);
86 	else
87 		return CMD_RET_USAGE;
88 }
89 
90 #ifdef CONFIG_SYS_LONGHELP
91 static char clk_help_text[] =
92 	"dump - Print clock frequencies";
93 #endif
94 
95 U_BOOT_CMD(clk, 2, 1, do_clk, "CLK sub-system", clk_help_text);
96