1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2020 - Texas Instruments Incorporated - https://www.ti.com/
4 *	Dave Gerlach <d-gerlach@ti.com>
5 */
6
7#define LOG_CATEGORY UCLASS_SOC
8
9#include <common.h>
10#include <soc.h>
11#include <dm.h>
12#include <errno.h>
13#include <dm/lists.h>
14#include <dm/root.h>
15
16int soc_get(struct udevice **devp)
17{
18	return uclass_first_device_err(UCLASS_SOC, devp);
19}
20
21int soc_get_machine(struct udevice *dev, char *buf, int size)
22{
23	struct soc_ops *ops = soc_get_ops(dev);
24
25	if (!ops->get_machine)
26		return -ENOSYS;
27
28	return ops->get_machine(dev, buf, size);
29}
30
31int soc_get_family(struct udevice *dev, char *buf, int size)
32{
33	struct soc_ops *ops = soc_get_ops(dev);
34
35	if (!ops->get_family)
36		return -ENOSYS;
37
38	return ops->get_family(dev, buf, size);
39}
40
41int soc_get_revision(struct udevice *dev, char *buf, int size)
42{
43	struct soc_ops *ops = soc_get_ops(dev);
44
45	if (!ops->get_revision)
46		return -ENOSYS;
47
48	return ops->get_revision(dev, buf, size);
49}
50
51const struct soc_attr *
52soc_device_match(const struct soc_attr *matches)
53{
54	bool match;
55	struct udevice *soc;
56	char str[SOC_MAX_STR_SIZE];
57
58	if (!matches)
59		return NULL;
60
61	if (soc_get(&soc))
62		return NULL;
63
64	while (1) {
65		if (!(matches->machine || matches->family ||
66		      matches->revision))
67			break;
68
69		match = true;
70
71		if (matches->machine) {
72			if (!soc_get_machine(soc, str, SOC_MAX_STR_SIZE)) {
73				if (strcmp(matches->machine, str))
74					match = false;
75			}
76		}
77
78		if (matches->family) {
79			if (!soc_get_family(soc, str, SOC_MAX_STR_SIZE)) {
80				if (strcmp(matches->family, str))
81					match = false;
82			}
83		}
84
85		if (matches->revision) {
86			if (!soc_get_revision(soc, str, SOC_MAX_STR_SIZE)) {
87				if (strcmp(matches->revision, str))
88					match = false;
89			}
90		}
91
92		if (match)
93			return matches;
94
95		matches++;
96	}
97
98	return NULL;
99}
100
101UCLASS_DRIVER(soc) = {
102	.id		= UCLASS_SOC,
103	.name		= "soc",
104};
105