/src/barebox/common/tlv/bus.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-only |
2 | | |
3 | | #include <common.h> |
4 | | #include <driver.h> |
5 | | #include <init.h> |
6 | | #include <tlv/tlv.h> |
7 | | #include <linux/err.h> |
8 | | #include <of.h> |
9 | | |
10 | | static void tlv_devinfo(struct device *dev) |
11 | 0 | { |
12 | 0 | struct tlv_device *tlvdev = to_tlv_device(dev); |
13 | |
|
14 | 0 | printf("Magic: %08x\n", tlvdev->magic); |
15 | 0 | } |
16 | | |
17 | | static int tlv_header_check(struct tlv_header *header, size_t size) |
18 | 705 | { |
19 | 705 | if (size < sizeof(*header)) |
20 | 0 | return -ENODATA; |
21 | | |
22 | 705 | if (header->reserved != 0) |
23 | 26 | return -EINVAL; |
24 | | |
25 | 679 | if (size < tlv_total_len(header)) |
26 | 0 | return -ENODATA; |
27 | | |
28 | 679 | return 0; |
29 | 679 | } |
30 | | |
31 | | static struct device_node *tlv_parent_node; |
32 | | |
33 | | struct tlv_device *tlv_register_device(struct tlv_header *header, size_t size, |
34 | | struct device *parent) |
35 | 705 | { |
36 | 705 | struct tlv_device *tlvdev; |
37 | 705 | const char *name = NULL; |
38 | 705 | struct device *dev; |
39 | 705 | static int id = 0; |
40 | 705 | int ret; |
41 | | |
42 | 705 | ret = tlv_header_check(header, size); |
43 | 705 | if (ret) |
44 | 26 | return ERR_PTR(ret); |
45 | | |
46 | 679 | tlvdev = xzalloc(sizeof(*tlvdev)); |
47 | | |
48 | 679 | dev = &tlvdev->dev; |
49 | | |
50 | 679 | dev->bus = &tlv_bus; |
51 | 679 | devinfo_add(dev, tlv_devinfo); |
52 | 679 | dev->platform_data = header; |
53 | 679 | tlvdev->magic = be32_to_cpu(header->magic); |
54 | 679 | dev->parent = parent ?: &tlv_bus.dev; |
55 | 679 | dev->id = DEVICE_ID_SINGLE; |
56 | | |
57 | 679 | if (parent) { |
58 | 0 | name = of_alias_get(parent->device_node); |
59 | 0 | if (name) |
60 | 0 | dev_set_name(dev, "%s", name); |
61 | 0 | } |
62 | | |
63 | 679 | if (!name) |
64 | 679 | dev_set_name(dev, "tlv%u", id++); |
65 | | |
66 | 679 | dev->device_node = of_new_node(tlv_parent_node, dev_name(dev)); |
67 | 679 | dev->device_node->dev = dev; |
68 | 679 | ret = register_device(dev); |
69 | 679 | if (ret) { |
70 | 0 | free(tlvdev); |
71 | 0 | return ERR_PTR(ret); |
72 | 0 | } |
73 | | |
74 | 679 | return tlvdev; |
75 | 679 | } |
76 | | |
77 | | void tlv_free_device(struct tlv_device *tlvdev) |
78 | 679 | { |
79 | 679 | tlv_of_unregister_fixup(tlvdev); |
80 | | |
81 | 679 | devinfo_del(&tlvdev->dev, tlv_devinfo); |
82 | 679 | unregister_device(&tlvdev->dev); |
83 | | |
84 | 679 | free(tlvdev->dev.platform_data); |
85 | 679 | of_delete_node(tlvdev->dev.device_node); |
86 | | |
87 | 679 | free(tlvdev); |
88 | 679 | } |
89 | | |
90 | | static int tlv_bus_match(struct device *dev, const struct driver *drv) |
91 | 679 | { |
92 | 679 | const struct tlv_decoder *decoder = to_tlv_decoder(drv); |
93 | 679 | struct tlv_device *tlvdev = to_tlv_device(dev); |
94 | | |
95 | 679 | return decoder->magic == tlvdev->magic; |
96 | 679 | } |
97 | | |
98 | | static int tlv_bus_probe(struct device *dev) |
99 | 679 | { |
100 | 679 | return dev->driver->probe(dev); |
101 | 679 | } |
102 | | |
103 | | static void tlv_bus_remove(struct device *dev) |
104 | 410 | { |
105 | 410 | if (dev->driver->remove) |
106 | 0 | dev->driver->remove(dev); |
107 | 410 | } |
108 | | |
109 | | struct bus_type tlv_bus = { |
110 | | .name = "barebox-tlv", |
111 | | .match = tlv_bus_match, |
112 | | .probe = tlv_bus_probe, |
113 | | .remove = tlv_bus_remove, |
114 | | }; |
115 | | |
116 | | struct tlv_device *tlv_ensure_probed_by_alias(const char *alias) |
117 | 0 | { |
118 | 0 | struct device_node *np; |
119 | 0 | struct device *dev; |
120 | |
|
121 | 0 | np = of_find_node_by_alias(NULL, alias); |
122 | 0 | if (!np) |
123 | 0 | return ERR_PTR(-EINVAL); |
124 | | |
125 | 0 | of_partition_ensure_probed(np); |
126 | |
|
127 | 0 | bus_for_each_device(&tlv_bus, dev) { |
128 | 0 | if (dev->parent && dev->parent->device_node == np) |
129 | 0 | return to_tlv_device(dev); |
130 | 0 | } |
131 | | |
132 | 0 | return ERR_PTR(-EPROBE_DEFER); |
133 | 0 | } |
134 | | |
135 | | static void tlv_bus_info(struct device *dev) |
136 | 0 | { |
137 | 0 | struct driver *drv; |
138 | |
|
139 | 0 | puts("Registered Magic:\n"); |
140 | 0 | bus_for_each_driver(&tlv_bus, drv) { |
141 | 0 | struct tlv_decoder *tlvdrv = to_tlv_decoder(drv); |
142 | |
|
143 | 0 | printf(" %08x (%s)\n", tlvdrv->magic, tlvdrv->driver.name); |
144 | 0 | } |
145 | 0 | } |
146 | | |
147 | | static int tlv_bus_register(void) |
148 | 30 | { |
149 | 30 | int ret; |
150 | | |
151 | 30 | tlv_parent_node = of_new_node(NULL, NULL); |
152 | | |
153 | 30 | ret = bus_register(&tlv_bus); |
154 | 30 | if (ret) |
155 | 0 | return ret; |
156 | | |
157 | 30 | devinfo_add(&tlv_bus.dev, tlv_bus_info); |
158 | | |
159 | 30 | return 0; |
160 | 30 | } |
161 | | postcore_initcall(tlv_bus_register); |