Merge branch 'master' of git://git.denx.de/u-boot
[oweals/u-boot.git] / drivers / cpu / cpu-uclass.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6
7 #include <common.h>
8 #include <cpu.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <dm/lists.h>
12 #include <dm/root.h>
13 #include <linux/err.h>
14
15 int cpu_probe_all(void)
16 {
17         struct udevice *cpu;
18         int ret;
19
20         ret = uclass_first_device(UCLASS_CPU, &cpu);
21         if (ret) {
22                 debug("%s: No CPU found (err = %d)\n", __func__, ret);
23                 return ret;
24         }
25
26         while (cpu) {
27                 ret = uclass_next_device(&cpu);
28                 if (ret) {
29                         debug("%s: Error while probing CPU (err = %d)\n",
30                               __func__, ret);
31                         return ret;
32                 }
33         }
34
35         return 0;
36 }
37
38 int cpu_is_current(struct udevice *cpu)
39 {
40         struct cpu_ops *ops = cpu_get_ops(cpu);
41
42         if (ops->is_current) {
43                 if (ops->is_current(cpu))
44                         return 1;
45         }
46
47         return -ENOSYS;
48 }
49
50 struct udevice *cpu_get_current_dev(void)
51 {
52         struct udevice *cpu;
53         int ret;
54
55         uclass_foreach_dev_probe(UCLASS_CPU, cpu) {
56                 if (cpu_is_current(cpu) > 0)
57                         return cpu;
58         }
59
60         /* If can't find current cpu device, use the first dev instead */
61         ret = uclass_first_device_err(UCLASS_CPU, &cpu);
62         if (ret) {
63                 debug("%s: Could not get CPU device (err = %d)\n",
64                       __func__, ret);
65                 return NULL;
66         }
67
68         return cpu;
69 }
70
71 int cpu_get_desc(struct udevice *dev, char *buf, int size)
72 {
73         struct cpu_ops *ops = cpu_get_ops(dev);
74
75         if (!ops->get_desc)
76                 return -ENOSYS;
77
78         return ops->get_desc(dev, buf, size);
79 }
80
81 int cpu_get_info(struct udevice *dev, struct cpu_info *info)
82 {
83         struct cpu_ops *ops = cpu_get_ops(dev);
84
85         if (!ops->get_info)
86                 return -ENOSYS;
87
88         return ops->get_info(dev, info);
89 }
90
91 int cpu_get_count(struct udevice *dev)
92 {
93         struct cpu_ops *ops = cpu_get_ops(dev);
94
95         if (!ops->get_count)
96                 return -ENOSYS;
97
98         return ops->get_count(dev);
99 }
100
101 int cpu_get_vendor(struct udevice *dev, char *buf, int size)
102 {
103         struct cpu_ops *ops = cpu_get_ops(dev);
104
105         if (!ops->get_vendor)
106                 return -ENOSYS;
107
108         return ops->get_vendor(dev, buf, size);
109 }
110
111 U_BOOT_DRIVER(cpu_bus) = {
112         .name   = "cpu_bus",
113         .id     = UCLASS_SIMPLE_BUS,
114         .per_child_platdata_auto_alloc_size = sizeof(struct cpu_platdata),
115 };
116
117 static int uclass_cpu_init(struct uclass *uc)
118 {
119         struct udevice *dev;
120         ofnode node;
121         int ret;
122
123         node = ofnode_path("/cpus");
124         if (!ofnode_valid(node))
125                 return 0;
126
127         ret = device_bind_driver_to_node(dm_root(), "cpu_bus", "cpus", node,
128                                          &dev);
129
130         return ret;
131 }
132
133 UCLASS_DRIVER(cpu) = {
134         .id             = UCLASS_CPU,
135         .name           = "cpu",
136         .flags          = DM_UC_FLAG_SEQ_ALIAS,
137         .init           = uclass_cpu_init,
138 };