Merge branch 'next-spi' of git://git.secretlab.ca/git/linux-2.6
[safe/jmp/linux-2.6] / 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 and
13  *  Grant Likely.
14  *
15  *      This program is free software; you can redistribute it and/or
16  *      modify it under the terms of the GNU General Public License
17  *      as published by the Free Software Foundation; either version
18  *      2 of the License, or (at your option) any later version.
19  */
20 #include <linux/module.h>
21 #include <linux/of.h>
22 #include <linux/spinlock.h>
23
24 struct device_node *allnodes;
25
26 /* use when traversing tree through the allnext, child, sibling,
27  * or parent members of struct device_node.
28  */
29 DEFINE_RWLOCK(devtree_lock);
30
31 int of_n_addr_cells(struct device_node *np)
32 {
33         const int *ip;
34
35         do {
36                 if (np->parent)
37                         np = np->parent;
38                 ip = of_get_property(np, "#address-cells", NULL);
39                 if (ip)
40                         return *ip;
41         } while (np->parent);
42         /* No #address-cells property for the root node */
43         return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
44 }
45 EXPORT_SYMBOL(of_n_addr_cells);
46
47 int of_n_size_cells(struct device_node *np)
48 {
49         const int *ip;
50
51         do {
52                 if (np->parent)
53                         np = np->parent;
54                 ip = of_get_property(np, "#size-cells", NULL);
55                 if (ip)
56                         return *ip;
57         } while (np->parent);
58         /* No #size-cells property for the root node */
59         return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
60 }
61 EXPORT_SYMBOL(of_n_size_cells);
62
63 struct property *of_find_property(const struct device_node *np,
64                                   const char *name,
65                                   int *lenp)
66 {
67         struct property *pp;
68
69         if (!np)
70                 return NULL;
71
72         read_lock(&devtree_lock);
73         for (pp = np->properties; pp != 0; pp = pp->next) {
74                 if (of_prop_cmp(pp->name, name) == 0) {
75                         if (lenp != 0)
76                                 *lenp = pp->length;
77                         break;
78                 }
79         }
80         read_unlock(&devtree_lock);
81
82         return pp;
83 }
84 EXPORT_SYMBOL(of_find_property);
85
86 /**
87  * of_find_all_nodes - Get next node in global list
88  * @prev:       Previous node or NULL to start iteration
89  *              of_node_put() will be called on it
90  *
91  * Returns a node pointer with refcount incremented, use
92  * of_node_put() on it when done.
93  */
94 struct device_node *of_find_all_nodes(struct device_node *prev)
95 {
96         struct device_node *np;
97
98         read_lock(&devtree_lock);
99         np = prev ? prev->allnext : allnodes;
100         for (; np != NULL; np = np->allnext)
101                 if (of_node_get(np))
102                         break;
103         of_node_put(prev);
104         read_unlock(&devtree_lock);
105         return np;
106 }
107 EXPORT_SYMBOL(of_find_all_nodes);
108
109 /*
110  * Find a property with a given name for a given node
111  * and return the value.
112  */
113 const void *of_get_property(const struct device_node *np, const char *name,
114                          int *lenp)
115 {
116         struct property *pp = of_find_property(np, name, lenp);
117
118         return pp ? pp->value : NULL;
119 }
120 EXPORT_SYMBOL(of_get_property);
121
122 /** Checks if the given "compat" string matches one of the strings in
123  * the device's "compatible" property
124  */
125 int of_device_is_compatible(const struct device_node *device,
126                 const char *compat)
127 {
128         const char* cp;
129         int cplen, l;
130
131         cp = of_get_property(device, "compatible", &cplen);
132         if (cp == NULL)
133                 return 0;
134         while (cplen > 0) {
135                 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
136                         return 1;
137                 l = strlen(cp) + 1;
138                 cp += l;
139                 cplen -= l;
140         }
141
142         return 0;
143 }
144 EXPORT_SYMBOL(of_device_is_compatible);
145
146 /**
147  *  of_device_is_available - check if a device is available for use
148  *
149  *  @device: Node to check for availability
150  *
151  *  Returns 1 if the status property is absent or set to "okay" or "ok",
152  *  0 otherwise
153  */
154 int of_device_is_available(const struct device_node *device)
155 {
156         const char *status;
157         int statlen;
158
159         status = of_get_property(device, "status", &statlen);
160         if (status == NULL)
161                 return 1;
162
163         if (statlen > 0) {
164                 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
165                         return 1;
166         }
167
168         return 0;
169 }
170 EXPORT_SYMBOL(of_device_is_available);
171
172 /**
173  *      of_get_parent - Get a node's parent if any
174  *      @node:  Node to get parent
175  *
176  *      Returns a node pointer with refcount incremented, use
177  *      of_node_put() on it when done.
178  */
179 struct device_node *of_get_parent(const struct device_node *node)
180 {
181         struct device_node *np;
182
183         if (!node)
184                 return NULL;
185
186         read_lock(&devtree_lock);
187         np = of_node_get(node->parent);
188         read_unlock(&devtree_lock);
189         return np;
190 }
191 EXPORT_SYMBOL(of_get_parent);
192
193 /**
194  *      of_get_next_parent - Iterate to a node's parent
195  *      @node:  Node to get parent of
196  *
197  *      This is like of_get_parent() except that it drops the
198  *      refcount on the passed node, making it suitable for iterating
199  *      through a node's parents.
200  *
201  *      Returns a node pointer with refcount incremented, use
202  *      of_node_put() on it when done.
203  */
204 struct device_node *of_get_next_parent(struct device_node *node)
205 {
206         struct device_node *parent;
207
208         if (!node)
209                 return NULL;
210
211         read_lock(&devtree_lock);
212         parent = of_node_get(node->parent);
213         of_node_put(node);
214         read_unlock(&devtree_lock);
215         return parent;
216 }
217
218 /**
219  *      of_get_next_child - Iterate a node childs
220  *      @node:  parent node
221  *      @prev:  previous child of the parent node, or NULL to get first
222  *
223  *      Returns a node pointer with refcount incremented, use
224  *      of_node_put() on it when done.
225  */
226 struct device_node *of_get_next_child(const struct device_node *node,
227         struct device_node *prev)
228 {
229         struct device_node *next;
230
231         read_lock(&devtree_lock);
232         next = prev ? prev->sibling : node->child;
233         for (; next; next = next->sibling)
234                 if (of_node_get(next))
235                         break;
236         of_node_put(prev);
237         read_unlock(&devtree_lock);
238         return next;
239 }
240 EXPORT_SYMBOL(of_get_next_child);
241
242 /**
243  *      of_find_node_by_path - Find a node matching a full OF path
244  *      @path:  The full path to match
245  *
246  *      Returns a node pointer with refcount incremented, use
247  *      of_node_put() on it when done.
248  */
249 struct device_node *of_find_node_by_path(const char *path)
250 {
251         struct device_node *np = allnodes;
252
253         read_lock(&devtree_lock);
254         for (; np; np = np->allnext) {
255                 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
256                     && of_node_get(np))
257                         break;
258         }
259         read_unlock(&devtree_lock);
260         return np;
261 }
262 EXPORT_SYMBOL(of_find_node_by_path);
263
264 /**
265  *      of_find_node_by_name - Find a node by its "name" property
266  *      @from:  The node to start searching from or NULL, the node
267  *              you pass will not be searched, only the next one
268  *              will; typically, you pass what the previous call
269  *              returned. of_node_put() will be called on it
270  *      @name:  The name string to match against
271  *
272  *      Returns a node pointer with refcount incremented, use
273  *      of_node_put() on it when done.
274  */
275 struct device_node *of_find_node_by_name(struct device_node *from,
276         const char *name)
277 {
278         struct device_node *np;
279
280         read_lock(&devtree_lock);
281         np = from ? from->allnext : allnodes;
282         for (; np; np = np->allnext)
283                 if (np->name && (of_node_cmp(np->name, name) == 0)
284                     && of_node_get(np))
285                         break;
286         of_node_put(from);
287         read_unlock(&devtree_lock);
288         return np;
289 }
290 EXPORT_SYMBOL(of_find_node_by_name);
291
292 /**
293  *      of_find_node_by_type - Find a node by its "device_type" property
294  *      @from:  The node to start searching from, or NULL to start searching
295  *              the entire device tree. The node you pass will not be
296  *              searched, only the next one will; typically, you pass
297  *              what the previous call returned. of_node_put() will be
298  *              called on from for you.
299  *      @type:  The type string to match against
300  *
301  *      Returns a node pointer with refcount incremented, use
302  *      of_node_put() on it when done.
303  */
304 struct device_node *of_find_node_by_type(struct device_node *from,
305         const char *type)
306 {
307         struct device_node *np;
308
309         read_lock(&devtree_lock);
310         np = from ? from->allnext : allnodes;
311         for (; np; np = np->allnext)
312                 if (np->type && (of_node_cmp(np->type, type) == 0)
313                     && of_node_get(np))
314                         break;
315         of_node_put(from);
316         read_unlock(&devtree_lock);
317         return np;
318 }
319 EXPORT_SYMBOL(of_find_node_by_type);
320
321 /**
322  *      of_find_compatible_node - Find a node based on type and one of the
323  *                                tokens in its "compatible" property
324  *      @from:          The node to start searching from or NULL, the node
325  *                      you pass will not be searched, only the next one
326  *                      will; typically, you pass what the previous call
327  *                      returned. of_node_put() will be called on it
328  *      @type:          The type string to match "device_type" or NULL to ignore
329  *      @compatible:    The string to match to one of the tokens in the device
330  *                      "compatible" list.
331  *
332  *      Returns a node pointer with refcount incremented, use
333  *      of_node_put() on it when done.
334  */
335 struct device_node *of_find_compatible_node(struct device_node *from,
336         const char *type, const char *compatible)
337 {
338         struct device_node *np;
339
340         read_lock(&devtree_lock);
341         np = from ? from->allnext : allnodes;
342         for (; np; np = np->allnext) {
343                 if (type
344                     && !(np->type && (of_node_cmp(np->type, type) == 0)))
345                         continue;
346                 if (of_device_is_compatible(np, compatible) && of_node_get(np))
347                         break;
348         }
349         of_node_put(from);
350         read_unlock(&devtree_lock);
351         return np;
352 }
353 EXPORT_SYMBOL(of_find_compatible_node);
354
355 /**
356  *      of_find_node_with_property - Find a node which has a property with
357  *                                   the given name.
358  *      @from:          The node to start searching from or NULL, the node
359  *                      you pass will not be searched, only the next one
360  *                      will; typically, you pass what the previous call
361  *                      returned. of_node_put() will be called on it
362  *      @prop_name:     The name of the property to look for.
363  *
364  *      Returns a node pointer with refcount incremented, use
365  *      of_node_put() on it when done.
366  */
367 struct device_node *of_find_node_with_property(struct device_node *from,
368         const char *prop_name)
369 {
370         struct device_node *np;
371         struct property *pp;
372
373         read_lock(&devtree_lock);
374         np = from ? from->allnext : allnodes;
375         for (; np; np = np->allnext) {
376                 for (pp = np->properties; pp != 0; pp = pp->next) {
377                         if (of_prop_cmp(pp->name, prop_name) == 0) {
378                                 of_node_get(np);
379                                 goto out;
380                         }
381                 }
382         }
383 out:
384         of_node_put(from);
385         read_unlock(&devtree_lock);
386         return np;
387 }
388 EXPORT_SYMBOL(of_find_node_with_property);
389
390 /**
391  * of_match_node - Tell if an device_node has a matching of_match structure
392  *      @matches:       array of of device match structures to search in
393  *      @node:          the of device structure to match against
394  *
395  *      Low level utility function used by device matching.
396  */
397 const struct of_device_id *of_match_node(const struct of_device_id *matches,
398                                          const struct device_node *node)
399 {
400         while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
401                 int match = 1;
402                 if (matches->name[0])
403                         match &= node->name
404                                 && !strcmp(matches->name, node->name);
405                 if (matches->type[0])
406                         match &= node->type
407                                 && !strcmp(matches->type, node->type);
408                 if (matches->compatible[0])
409                         match &= of_device_is_compatible(node,
410                                                 matches->compatible);
411                 if (match)
412                         return matches;
413                 matches++;
414         }
415         return NULL;
416 }
417 EXPORT_SYMBOL(of_match_node);
418
419 /**
420  *      of_find_matching_node - Find a node based on an of_device_id match
421  *                              table.
422  *      @from:          The node to start searching from or NULL, the node
423  *                      you pass will not be searched, only the next one
424  *                      will; typically, you pass what the previous call
425  *                      returned. of_node_put() will be called on it
426  *      @matches:       array of of device match structures to search in
427  *
428  *      Returns a node pointer with refcount incremented, use
429  *      of_node_put() on it when done.
430  */
431 struct device_node *of_find_matching_node(struct device_node *from,
432                                           const struct of_device_id *matches)
433 {
434         struct device_node *np;
435
436         read_lock(&devtree_lock);
437         np = from ? from->allnext : allnodes;
438         for (; np; np = np->allnext) {
439                 if (of_match_node(matches, np) && of_node_get(np))
440                         break;
441         }
442         of_node_put(from);
443         read_unlock(&devtree_lock);
444         return np;
445 }
446 EXPORT_SYMBOL(of_find_matching_node);
447
448 /**
449  * of_modalias_table: Table of explicit compatible ==> modalias mappings
450  *
451  * This table allows particulare compatible property values to be mapped
452  * to modalias strings.  This is useful for busses which do not directly
453  * understand the OF device tree but are populated based on data contained
454  * within the device tree.  SPI and I2C are the two current users of this
455  * table.
456  *
457  * In most cases, devices do not need to be listed in this table because
458  * the modalias value can be derived directly from the compatible table.
459  * However, if for any reason a value cannot be derived, then this table
460  * provides a method to override the implicit derivation.
461  *
462  * At the moment, a single table is used for all bus types because it is
463  * assumed that the data size is small and that the compatible values
464  * should already be distinct enough to differentiate between SPI, I2C
465  * and other devices.
466  */
467 struct of_modalias_table {
468         char *of_device;
469         char *modalias;
470 };
471 static struct of_modalias_table of_modalias_table[] = {
472         { "fsl,mcu-mpc8349emitx", "mcu-mpc8349emitx" },
473         { "mmc-spi-slot", "mmc_spi" },
474 };
475
476 /**
477  * of_modalias_node - Lookup appropriate modalias for a device node
478  * @node:       pointer to a device tree node
479  * @modalias:   Pointer to buffer that modalias value will be copied into
480  * @len:        Length of modalias value
481  *
482  * Based on the value of the compatible property, this routine will determine
483  * an appropriate modalias value for a particular device tree node.  Two
484  * separate methods are attempted to derive a modalias value.
485  *
486  * First method is to lookup the compatible value in of_modalias_table.
487  * Second is to strip off the manufacturer prefix from the first
488  * compatible entry and use the remainder as modalias
489  *
490  * This routine returns 0 on success
491  */
492 int of_modalias_node(struct device_node *node, char *modalias, int len)
493 {
494         int i, cplen;
495         const char *compatible;
496         const char *p;
497
498         /* 1. search for exception list entry */
499         for (i = 0; i < ARRAY_SIZE(of_modalias_table); i++) {
500                 compatible = of_modalias_table[i].of_device;
501                 if (!of_device_is_compatible(node, compatible))
502                         continue;
503                 strlcpy(modalias, of_modalias_table[i].modalias, len);
504                 return 0;
505         }
506
507         compatible = of_get_property(node, "compatible", &cplen);
508         if (!compatible)
509                 return -ENODEV;
510
511         /* 2. take first compatible entry and strip manufacturer */
512         p = strchr(compatible, ',');
513         if (!p)
514                 return -ENODEV;
515         p++;
516         strlcpy(modalias, p, len);
517         return 0;
518 }
519 EXPORT_SYMBOL_GPL(of_modalias_node);
520
521 /**
522  * of_parse_phandle - Resolve a phandle property to a device_node pointer
523  * @np: Pointer to device node holding phandle property
524  * @phandle_name: Name of property holding a phandle value
525  * @index: For properties holding a table of phandles, this is the index into
526  *         the table
527  *
528  * Returns the device_node pointer with refcount incremented.  Use
529  * of_node_put() on it when done.
530  */
531 struct device_node *
532 of_parse_phandle(struct device_node *np, const char *phandle_name, int index)
533 {
534         const phandle *phandle;
535         int size;
536
537         phandle = of_get_property(np, phandle_name, &size);
538         if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
539                 return NULL;
540
541         return of_find_node_by_phandle(phandle[index]);
542 }
543 EXPORT_SYMBOL(of_parse_phandle);
544
545 /**
546  * of_parse_phandles_with_args - Find a node pointed by phandle in a list
547  * @np:         pointer to a device tree node containing a list
548  * @list_name:  property name that contains a list
549  * @cells_name: property name that specifies phandles' arguments count
550  * @index:      index of a phandle to parse out
551  * @out_node:   optional pointer to device_node struct pointer (will be filled)
552  * @out_args:   optional pointer to arguments pointer (will be filled)
553  *
554  * This function is useful to parse lists of phandles and their arguments.
555  * Returns 0 on success and fills out_node and out_args, on error returns
556  * appropriate errno value.
557  *
558  * Example:
559  *
560  * phandle1: node1 {
561  *      #list-cells = <2>;
562  * }
563  *
564  * phandle2: node2 {
565  *      #list-cells = <1>;
566  * }
567  *
568  * node3 {
569  *      list = <&phandle1 1 2 &phandle2 3>;
570  * }
571  *
572  * To get a device_node of the `node2' node you may call this:
573  * of_parse_phandles_with_args(node3, "list", "#list-cells", 2, &node2, &args);
574  */
575 int of_parse_phandles_with_args(struct device_node *np, const char *list_name,
576                                 const char *cells_name, int index,
577                                 struct device_node **out_node,
578                                 const void **out_args)
579 {
580         int ret = -EINVAL;
581         const u32 *list;
582         const u32 *list_end;
583         int size;
584         int cur_index = 0;
585         struct device_node *node = NULL;
586         const void *args = NULL;
587
588         list = of_get_property(np, list_name, &size);
589         if (!list) {
590                 ret = -ENOENT;
591                 goto err0;
592         }
593         list_end = list + size / sizeof(*list);
594
595         while (list < list_end) {
596                 const u32 *cells;
597                 const phandle *phandle;
598
599                 phandle = list++;
600                 args = list;
601
602                 /* one cell hole in the list = <>; */
603                 if (!*phandle)
604                         goto next;
605
606                 node = of_find_node_by_phandle(*phandle);
607                 if (!node) {
608                         pr_debug("%s: could not find phandle\n",
609                                  np->full_name);
610                         goto err0;
611                 }
612
613                 cells = of_get_property(node, cells_name, &size);
614                 if (!cells || size != sizeof(*cells)) {
615                         pr_debug("%s: could not get %s for %s\n",
616                                  np->full_name, cells_name, node->full_name);
617                         goto err1;
618                 }
619
620                 list += *cells;
621                 if (list > list_end) {
622                         pr_debug("%s: insufficient arguments length\n",
623                                  np->full_name);
624                         goto err1;
625                 }
626 next:
627                 if (cur_index == index)
628                         break;
629
630                 of_node_put(node);
631                 node = NULL;
632                 args = NULL;
633                 cur_index++;
634         }
635
636         if (!node) {
637                 /*
638                  * args w/o node indicates that the loop above has stopped at
639                  * the 'hole' cell. Report this differently.
640                  */
641                 if (args)
642                         ret = -EEXIST;
643                 else
644                         ret = -ENOENT;
645                 goto err0;
646         }
647
648         if (out_node)
649                 *out_node = node;
650         if (out_args)
651                 *out_args = args;
652
653         return 0;
654 err1:
655         of_node_put(node);
656 err0:
657         pr_debug("%s failed with status %d\n", __func__, ret);
658         return ret;
659 }
660 EXPORT_SYMBOL(of_parse_phandles_with_args);