1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
// SPDX-License-Identifier: GPL-2.0
/*
* of_path.c
*
* Copyright (c) 2013 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
*/
#include <common.h>
#include <malloc.h>
#include <of.h>
#include <fs.h>
#include <linux/mtd/mtd.h>
struct device_d *of_find_device_by_node_path(const char *path)
{
struct device_d *dev;
for_each_device(dev) {
if (!dev->device_node)
continue;
if (!strcmp(path, dev->device_node->full_name))
return dev;
}
return NULL;
}
/**
* __of_find_path
*
* @node: The node to find the cdev for, can be the device or a
* partition in the device
* @part: Optionally, a description of a parition of @node. See of_find_path
* @outpath: if this function returns 0 outpath will contain the path belonging
* to the input path description. Must be freed with free().
* @flags: use OF_FIND_PATH_FLAGS_BB to return the .bb device if available
*
*/
static int __of_find_path(struct device_node *node, const char *part, char **outpath, unsigned flags)
{
struct device_d *dev;
struct cdev *cdev;
bool add_bb = false;
dev = of_find_device_by_node_path(node->full_name);
if (!dev) {
int ret;
const char *uuid;
struct device_node *devnode = node->parent;
if (of_device_is_compatible(devnode, "fixed-partitions")) {
devnode = devnode->parent;
/* when partuuid is specified short-circuit the search for the cdev */
ret = of_property_read_string(node, "partuuid", &uuid);
if (!ret) {
cdev = cdev_by_partuuid(uuid);
if (!cdev)
return -ENODEV;
*outpath = basprintf("/dev/%s", cdev->name);
return 0;
}
}
dev = of_find_device_by_node_path(devnode->full_name);
if (!dev)
return -ENODEV;
}
if (dev->bus && !dev->driver)
return -EPROBE_DEFER;
device_detect(dev);
if (part)
cdev = device_find_partition(dev, part);
else
cdev = cdev_by_device_node(node);
if (!cdev)
return -ENOENT;
if ((flags & OF_FIND_PATH_FLAGS_BB) && cdev->mtd &&
mtd_can_have_bb(cdev->mtd))
add_bb = true;
*outpath = basprintf("/dev/%s%s", cdev->name, add_bb ? ".bb" : "");
return 0;
}
/**
* of_find_path_by_node - translate a node in the devicetree to a
* barebox device path
*
* @node: the node we're interested in
* @outpath: if this function returns 0 outpath will contain the path belonging
* to the input path description. Must be freed with free().
* @flags: use OF_FIND_PATH_FLAGS_BB to return the .bb device if available
*
*/
int of_find_path_by_node(struct device_node *node, char **outpath, unsigned flags)
{
return __of_find_path(node, NULL, outpath, flags);
}
/**
* of_find_node_by_devpath - translate a device path to a device tree node
*
* @root: The device tree root. Can be NULL, in this case the internal tree is used
* @path: The path to look the node up for. Can be "/dev/cdevname" or "cdevname" directly.
*
* This is the counterpart of of_find_path_by_node(). Given a path this function tries
* to find the corresponding node in the given device tree.
*
* We first have to find the hardware device in the tree we are passed and then find
* a partition matching offset/size in this tree. This is necessary because the
* passed tree may use another partition binding (legacy vs. fixed-partitions). Also
* the node names may differ (some device trees have partition@<num> instead of
* partition@<offset>.
*/
struct device_node *of_find_node_by_devpath(struct device_node *root, const char *path)
{
struct cdev *cdev;
bool is_partition = false;
struct device_node *np, *partnode, *rnp;
loff_t part_offset = 0, part_size = 0;
pr_debug("%s: looking for path %s\n", __func__, path);
cdev = cdev_by_name(devpath_to_name(path));
if (!cdev) {
pr_debug("%s: cdev %s not found\n", __func__, path);
return NULL;
}
/*
* Look for the device node of the master device (the one of_parse_partitions() has
* been called with
*/
if (cdev->master) {
is_partition = true;
if (cdev->mtd)
part_offset = cdev->mtd->master_offset;
else
part_offset = cdev->offset;
part_size = cdev->size;
pr_debug("%s path %s: is a partition with offset 0x%08llx, size 0x%08llx\n",
__func__, path, part_offset, part_size);
np = cdev->master->device_node;
} else {
np = cdev->device_node;
}
/*
* Now find the device node of the master device in the device tree we have
* been passed.
*/
rnp = of_find_node_by_path_from(root, np->full_name);
if (!rnp) {
pr_debug("%s path %s: %s not found in passed tree\n", __func__, path,
np->full_name);
return NULL;
}
if (!is_partition) {
pr_debug("%s path %s: returning full device node %s\n", __func__, path,
rnp->full_name);
return rnp;
}
/*
* Look for a partition with matching offset/size in the device node of
* the tree we have been passed.
*/
partnode = of_get_child_by_name(rnp, "partitions");
if (!partnode) {
pr_debug("%s path %s: using legacy partition binding\n", __func__, path);
partnode = rnp;
}
for_each_child_of_node(partnode, np) {
const __be32 *reg;
int na, ns, len;
loff_t offset, size;
reg = of_get_property(np, "reg", &len);
if (!reg)
return NULL;
na = of_n_addr_cells(np);
ns = of_n_size_cells(np);
if (len < (na + ns) * sizeof(__be32)) {
pr_err("reg property too small in %s\n", np->full_name);
continue;
}
offset = of_read_number(reg, na);
size = of_read_number(reg + na, ns);
if (part_offset == offset && part_size == size) {
pr_debug("%s path %s: found matching partition in %s\n", __func__, path,
np->full_name);
return np;
}
}
pr_debug("%s path %s: no matching node found\n", __func__, path);
return NULL;
}
/**
* of_find_path - translate a path description in the devicetree to a barebox
* path
*
* @node: the node containing the property with the path description
* @propname: the property name of the path description
* @outpath: if this function returns 0 outpath will contain the path belonging
* to the input path description. Must be freed with free().
* @flags: use OF_FIND_PATH_FLAGS_BB to return the .bb device if available
*
* paths in the devicetree have the form of a multistring property. The first
* string contains the full path to the physical device containing the path or
* a full path to a partition described by the OF partition binding.
* The remaining strings have the form "<type>:<options>". Currently supported
* for <type> are:
*
* partname:<partname> - find a partition by its partition name. For mtd
* partitions this is the label. For DOS partitions
* this is the number beginning with 0.
*
* examples:
*
* device-path = &mmc0, "partname:0";
* device-path = &norflash, "partname:barebox-environment";
* device-path = &environment_nor;
*/
int of_find_path(struct device_node *node, const char *propname, char **outpath, unsigned flags)
{
struct device_node *rnode;
const char *path;
const char *part = NULL;
const char partnamestr[] = "partname:";
path = of_get_property(node, propname, NULL);
if (!path)
return -EINVAL;
rnode = of_find_node_by_path(path);
if (!rnode)
return -ENODEV;
of_property_read_string_index(node, propname, 1, &part);
if (part) {
if (!strncmp(part, partnamestr, sizeof(partnamestr) - 1)) {
part += sizeof(partnamestr) - 1;
} else {
pr_err("Invalid device-path: %s\n", part);
return -EINVAL;
}
}
return __of_find_path(rnode, part, outpath, flags);
}
|