bnx2: Refine remote PHY locking.
[deliverable/linux.git] / drivers / of / base.c
1 /*
2 * Procedures for creating, accessing and interpreting the device tree.
3 *
4 * Paul Mackerras August 1996.
5 * Copyright (C) 1996-2005 Paul Mackerras.
6 *
7 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8 * {engebret|bergner}@us.ibm.com
9 *
10 * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
11 *
12 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell.
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
18 */
19 #include <linux/module.h>
20 #include <linux/of.h>
21 #include <linux/spinlock.h>
22
23 struct device_node *allnodes;
24
25 /* use when traversing tree through the allnext, child, sibling,
26 * or parent members of struct device_node.
27 */
28 DEFINE_RWLOCK(devtree_lock);
29
30 int of_n_addr_cells(struct device_node *np)
31 {
32 const int *ip;
33
34 do {
35 if (np->parent)
36 np = np->parent;
37 ip = of_get_property(np, "#address-cells", NULL);
38 if (ip)
39 return *ip;
40 } while (np->parent);
41 /* No #address-cells property for the root node */
42 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
43 }
44 EXPORT_SYMBOL(of_n_addr_cells);
45
46 int of_n_size_cells(struct device_node *np)
47 {
48 const int *ip;
49
50 do {
51 if (np->parent)
52 np = np->parent;
53 ip = of_get_property(np, "#size-cells", NULL);
54 if (ip)
55 return *ip;
56 } while (np->parent);
57 /* No #size-cells property for the root node */
58 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
59 }
60 EXPORT_SYMBOL(of_n_size_cells);
61
62 struct property *of_find_property(const struct device_node *np,
63 const char *name,
64 int *lenp)
65 {
66 struct property *pp;
67
68 read_lock(&devtree_lock);
69 for (pp = np->properties; pp != 0; pp = pp->next) {
70 if (of_prop_cmp(pp->name, name) == 0) {
71 if (lenp != 0)
72 *lenp = pp->length;
73 break;
74 }
75 }
76 read_unlock(&devtree_lock);
77
78 return pp;
79 }
80 EXPORT_SYMBOL(of_find_property);
81
82 /*
83 * Find a property with a given name for a given node
84 * and return the value.
85 */
86 const void *of_get_property(const struct device_node *np, const char *name,
87 int *lenp)
88 {
89 struct property *pp = of_find_property(np, name, lenp);
90
91 return pp ? pp->value : NULL;
92 }
93 EXPORT_SYMBOL(of_get_property);
94
95 /** Checks if the given "compat" string matches one of the strings in
96 * the device's "compatible" property
97 */
98 int of_device_is_compatible(const struct device_node *device,
99 const char *compat)
100 {
101 const char* cp;
102 int cplen, l;
103
104 cp = of_get_property(device, "compatible", &cplen);
105 if (cp == NULL)
106 return 0;
107 while (cplen > 0) {
108 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
109 return 1;
110 l = strlen(cp) + 1;
111 cp += l;
112 cplen -= l;
113 }
114
115 return 0;
116 }
117 EXPORT_SYMBOL(of_device_is_compatible);
118
119 /**
120 * of_device_is_available - check if a device is available for use
121 *
122 * @device: Node to check for availability
123 *
124 * Returns 1 if the status property is absent or set to "okay" or "ok",
125 * 0 otherwise
126 */
127 int of_device_is_available(const struct device_node *device)
128 {
129 const char *status;
130 int statlen;
131
132 status = of_get_property(device, "status", &statlen);
133 if (status == NULL)
134 return 1;
135
136 if (statlen > 0) {
137 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
138 return 1;
139 }
140
141 return 0;
142 }
143 EXPORT_SYMBOL(of_device_is_available);
144
145 /**
146 * of_get_parent - Get a node's parent if any
147 * @node: Node to get parent
148 *
149 * Returns a node pointer with refcount incremented, use
150 * of_node_put() on it when done.
151 */
152 struct device_node *of_get_parent(const struct device_node *node)
153 {
154 struct device_node *np;
155
156 if (!node)
157 return NULL;
158
159 read_lock(&devtree_lock);
160 np = of_node_get(node->parent);
161 read_unlock(&devtree_lock);
162 return np;
163 }
164 EXPORT_SYMBOL(of_get_parent);
165
166 /**
167 * of_get_next_parent - Iterate to a node's parent
168 * @node: Node to get parent of
169 *
170 * This is like of_get_parent() except that it drops the
171 * refcount on the passed node, making it suitable for iterating
172 * through a node's parents.
173 *
174 * Returns a node pointer with refcount incremented, use
175 * of_node_put() on it when done.
176 */
177 struct device_node *of_get_next_parent(struct device_node *node)
178 {
179 struct device_node *parent;
180
181 if (!node)
182 return NULL;
183
184 read_lock(&devtree_lock);
185 parent = of_node_get(node->parent);
186 of_node_put(node);
187 read_unlock(&devtree_lock);
188 return parent;
189 }
190
191 /**
192 * of_get_next_child - Iterate a node childs
193 * @node: parent node
194 * @prev: previous child of the parent node, or NULL to get first
195 *
196 * Returns a node pointer with refcount incremented, use
197 * of_node_put() on it when done.
198 */
199 struct device_node *of_get_next_child(const struct device_node *node,
200 struct device_node *prev)
201 {
202 struct device_node *next;
203
204 read_lock(&devtree_lock);
205 next = prev ? prev->sibling : node->child;
206 for (; next; next = next->sibling)
207 if (of_node_get(next))
208 break;
209 of_node_put(prev);
210 read_unlock(&devtree_lock);
211 return next;
212 }
213 EXPORT_SYMBOL(of_get_next_child);
214
215 /**
216 * of_find_node_by_path - Find a node matching a full OF path
217 * @path: The full path to match
218 *
219 * Returns a node pointer with refcount incremented, use
220 * of_node_put() on it when done.
221 */
222 struct device_node *of_find_node_by_path(const char *path)
223 {
224 struct device_node *np = allnodes;
225
226 read_lock(&devtree_lock);
227 for (; np; np = np->allnext) {
228 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
229 && of_node_get(np))
230 break;
231 }
232 read_unlock(&devtree_lock);
233 return np;
234 }
235 EXPORT_SYMBOL(of_find_node_by_path);
236
237 /**
238 * of_find_node_by_name - Find a node by its "name" property
239 * @from: The node to start searching from or NULL, the node
240 * you pass will not be searched, only the next one
241 * will; typically, you pass what the previous call
242 * returned. of_node_put() will be called on it
243 * @name: The name string to match against
244 *
245 * Returns a node pointer with refcount incremented, use
246 * of_node_put() on it when done.
247 */
248 struct device_node *of_find_node_by_name(struct device_node *from,
249 const char *name)
250 {
251 struct device_node *np;
252
253 read_lock(&devtree_lock);
254 np = from ? from->allnext : allnodes;
255 for (; np; np = np->allnext)
256 if (np->name && (of_node_cmp(np->name, name) == 0)
257 && of_node_get(np))
258 break;
259 of_node_put(from);
260 read_unlock(&devtree_lock);
261 return np;
262 }
263 EXPORT_SYMBOL(of_find_node_by_name);
264
265 /**
266 * of_find_node_by_type - Find a node by its "device_type" property
267 * @from: The node to start searching from, or NULL to start searching
268 * the entire device tree. The node you pass will not be
269 * searched, only the next one will; typically, you pass
270 * what the previous call returned. of_node_put() will be
271 * called on from for you.
272 * @type: The type string to match against
273 *
274 * Returns a node pointer with refcount incremented, use
275 * of_node_put() on it when done.
276 */
277 struct device_node *of_find_node_by_type(struct device_node *from,
278 const char *type)
279 {
280 struct device_node *np;
281
282 read_lock(&devtree_lock);
283 np = from ? from->allnext : allnodes;
284 for (; np; np = np->allnext)
285 if (np->type && (of_node_cmp(np->type, type) == 0)
286 && of_node_get(np))
287 break;
288 of_node_put(from);
289 read_unlock(&devtree_lock);
290 return np;
291 }
292 EXPORT_SYMBOL(of_find_node_by_type);
293
294 /**
295 * of_find_compatible_node - Find a node based on type and one of the
296 * tokens in its "compatible" property
297 * @from: The node to start searching from or NULL, the node
298 * you pass will not be searched, only the next one
299 * will; typically, you pass what the previous call
300 * returned. of_node_put() will be called on it
301 * @type: The type string to match "device_type" or NULL to ignore
302 * @compatible: The string to match to one of the tokens in the device
303 * "compatible" list.
304 *
305 * Returns a node pointer with refcount incremented, use
306 * of_node_put() on it when done.
307 */
308 struct device_node *of_find_compatible_node(struct device_node *from,
309 const char *type, const char *compatible)
310 {
311 struct device_node *np;
312
313 read_lock(&devtree_lock);
314 np = from ? from->allnext : allnodes;
315 for (; np; np = np->allnext) {
316 if (type
317 && !(np->type && (of_node_cmp(np->type, type) == 0)))
318 continue;
319 if (of_device_is_compatible(np, compatible) && of_node_get(np))
320 break;
321 }
322 of_node_put(from);
323 read_unlock(&devtree_lock);
324 return np;
325 }
326 EXPORT_SYMBOL(of_find_compatible_node);
327
328 /**
329 * of_match_node - Tell if an device_node has a matching of_match structure
330 * @matches: array of of device match structures to search in
331 * @node: the of device structure to match against
332 *
333 * Low level utility function used by device matching.
334 */
335 const struct of_device_id *of_match_node(const struct of_device_id *matches,
336 const struct device_node *node)
337 {
338 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
339 int match = 1;
340 if (matches->name[0])
341 match &= node->name
342 && !strcmp(matches->name, node->name);
343 if (matches->type[0])
344 match &= node->type
345 && !strcmp(matches->type, node->type);
346 if (matches->compatible[0])
347 match &= of_device_is_compatible(node,
348 matches->compatible);
349 if (match)
350 return matches;
351 matches++;
352 }
353 return NULL;
354 }
355 EXPORT_SYMBOL(of_match_node);
356
357 /**
358 * of_find_matching_node - Find a node based on an of_device_id match
359 * table.
360 * @from: The node to start searching from or NULL, the node
361 * you pass will not be searched, only the next one
362 * will; typically, you pass what the previous call
363 * returned. of_node_put() will be called on it
364 * @matches: array of of device match structures to search in
365 *
366 * Returns a node pointer with refcount incremented, use
367 * of_node_put() on it when done.
368 */
369 struct device_node *of_find_matching_node(struct device_node *from,
370 const struct of_device_id *matches)
371 {
372 struct device_node *np;
373
374 read_lock(&devtree_lock);
375 np = from ? from->allnext : allnodes;
376 for (; np; np = np->allnext) {
377 if (of_match_node(matches, np) && of_node_get(np))
378 break;
379 }
380 of_node_put(from);
381 read_unlock(&devtree_lock);
382 return np;
383 }
384 EXPORT_SYMBOL(of_find_matching_node);
This page took 0.051103 seconds and 5 git commands to generate.