blob: b465b14a948ed715b170baecfedf4b34fa4b312d [file] [log] [blame]
Bin Meng12d41052015-06-12 14:52:20 +08001/*
2 * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
8#include <cpu.h>
9#include <dm.h>
10#include <errno.h>
11#include <asm/cpu.h>
12
Bin Mengf967f9a2015-06-17 11:15:36 +080013DECLARE_GLOBAL_DATA_PTR;
14
Bin Meng12d41052015-06-12 14:52:20 +080015int cpu_x86_bind(struct udevice *dev)
16{
17 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
Alexander Graf00b4fc22016-08-19 01:23:26 +020018 struct cpuid_result res;
Bin Meng12d41052015-06-12 14:52:20 +080019
Simon Glassdd79d6e2017-01-17 16:52:55 -070020 plat->cpu_id = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Bin Meng12d41052015-06-12 14:52:20 +080021 "intel,apic-id", -1);
Alexander Graf00b4fc22016-08-19 01:23:26 +020022 plat->family = gd->arch.x86;
23 res = cpuid(1);
24 plat->id[0] = res.eax;
25 plat->id[1] = res.edx;
Bin Meng12d41052015-06-12 14:52:20 +080026
27 return 0;
28}
29
Alexander Graf67f66bd2016-08-19 01:23:27 +020030int cpu_x86_get_vendor(struct udevice *dev, char *buf, int size)
31{
32 const char *vendor = cpu_vendor_name(gd->arch.x86_vendor);
33
34 if (size < (strlen(vendor) + 1))
35 return -ENOSPC;
36
37 strcpy(buf, vendor);
38
39 return 0;
40}
41
Bin Meng12d41052015-06-12 14:52:20 +080042int cpu_x86_get_desc(struct udevice *dev, char *buf, int size)
43{
Simon Glass0bd50cd2017-03-19 12:59:20 -060044 char *ptr;
45
Bin Meng12d41052015-06-12 14:52:20 +080046 if (size < CPU_MAX_NAME_LEN)
47 return -ENOSPC;
48
Simon Glass0bd50cd2017-03-19 12:59:20 -060049 ptr = cpu_get_name(buf);
50 if (ptr != buf)
51 strcpy(buf, ptr);
Bin Meng12d41052015-06-12 14:52:20 +080052
53 return 0;
54}
55
Bin Mengf967f9a2015-06-17 11:15:36 +080056static int cpu_x86_get_count(struct udevice *dev)
57{
58 int node, cpu;
59 int num = 0;
60
61 node = fdt_path_offset(gd->fdt_blob, "/cpus");
62 if (node < 0)
63 return -ENOENT;
64
65 for (cpu = fdt_first_subnode(gd->fdt_blob, node);
66 cpu >= 0;
67 cpu = fdt_next_subnode(gd->fdt_blob, cpu)) {
68 const char *device_type;
69
70 device_type = fdt_getprop(gd->fdt_blob, cpu,
71 "device_type", NULL);
72 if (!device_type)
73 continue;
74 if (strcmp(device_type, "cpu") == 0)
75 num++;
76 }
77
78 return num;
79}
80
Bin Meng12d41052015-06-12 14:52:20 +080081static const struct cpu_ops cpu_x86_ops = {
82 .get_desc = cpu_x86_get_desc,
Bin Mengf967f9a2015-06-17 11:15:36 +080083 .get_count = cpu_x86_get_count,
Alexander Graf67f66bd2016-08-19 01:23:27 +020084 .get_vendor = cpu_x86_get_vendor,
Bin Meng12d41052015-06-12 14:52:20 +080085};
86
87static const struct udevice_id cpu_x86_ids[] = {
88 { .compatible = "cpu-x86" },
89 { }
90};
91
92U_BOOT_DRIVER(cpu_x86_drv) = {
93 .name = "cpu_x86",
94 .id = UCLASS_CPU,
95 .of_match = cpu_x86_ids,
96 .bind = cpu_x86_bind,
97 .ops = &cpu_x86_ops,
98};