Merge tag 'for-v2022.10' of https://source.denx.de/u-boot/custodians/u-boot-i2c
[platform/kernel/u-boot.git] / cmd / cpu.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2015 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  * Copyright (c) 2017 Álvaro Fernández Rojas <noltari@gmail.com>
6  */
7
8 #include <common.h>
9 #include <command.h>
10 #include <cpu.h>
11 #include <dm.h>
12 #include <errno.h>
13
14 static const char *cpu_feature_name[CPU_FEAT_COUNT] = {
15         "L1 cache",
16         "MMU",
17         "Microcode",
18         "Device ID",
19 };
20
21 static int print_cpu_list(bool detail)
22 {
23         struct udevice *dev;
24         char buf[100];
25
26         for (uclass_first_device(UCLASS_CPU, &dev);
27                      dev;
28                      uclass_next_device(&dev)) {
29                 struct cpu_plat *plat = dev_get_parent_plat(dev);
30                 struct cpu_info info;
31                 bool first = true;
32                 int ret, i;
33
34                 ret = cpu_get_desc(dev, buf, sizeof(buf));
35                 printf("%3d: %-10s %s\n", dev_seq(dev), dev->name,
36                        ret ? "<no description>" : buf);
37                 if (!detail)
38                         continue;
39                 ret = cpu_get_info(dev, &info);
40                 if (ret) {
41                         printf("\t(no detail available");
42                         if (ret != -ENOSYS)
43                                 printf(": err=%d", ret);
44                         printf(")\n");
45                         continue;
46                 }
47                 printf("\tID = %d, freq = ", plat->cpu_id);
48                 print_freq(info.cpu_freq, "");
49                 for (i = 0; i < CPU_FEAT_COUNT; i++) {
50                         if (info.features & (1 << i)) {
51                                 printf("%s%s", first ? ": " : ", ",
52                                        cpu_feature_name[i]);
53                                 first = false;
54                         }
55                 }
56                 printf("\n");
57                 if (info.features & (1 << CPU_FEAT_UCODE))
58                         printf("\tMicrocode version %#x\n",
59                                plat->ucode_version);
60                 if (info.features & (1 << CPU_FEAT_DEVICE_ID))
61                         printf("\tDevice ID %#lx\n", plat->device_id);
62         }
63
64         return 0;
65 }
66
67 static int do_cpu_list(struct cmd_tbl *cmdtp, int flag, int argc,
68                        char *const argv[])
69 {
70         if (print_cpu_list(false))
71                 return CMD_RET_FAILURE;
72
73         return 0;
74 }
75
76 static int do_cpu_detail(struct cmd_tbl *cmdtp, int flag, int argc,
77                          char *const argv[])
78 {
79         if (print_cpu_list(true))
80                 return CMD_RET_FAILURE;
81
82         return 0;
83 }
84
85 #if CONFIG_IS_ENABLED(SYS_LONGHELP)
86 static char cpu_help_text[] =
87         "list   - list available CPUs\n"
88         "cpu detail     - show CPU detail"
89         ;
90 #endif
91
92 U_BOOT_CMD_WITH_SUBCMDS(cpu, "display information about CPUs", cpu_help_text,
93         U_BOOT_SUBCMD_MKENT(list, 1, 1, do_cpu_list),
94         U_BOOT_SUBCMD_MKENT(detail, 1, 0, do_cpu_detail));