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