blob: 7eda43c66c916198b1c2d8fc5043fcb1edaede7a [file] [log] [blame]
Rob Herringaf6074f2017-12-27 12:55:14 -06001// SPDX-License-Identifier: GPL-2.0+
Andres Salomon3cfc5352010-10-10 21:42:33 -06002/* pdt.c: OF PROM device tree support code.
Andres Salomon9bdf6ba2010-08-30 03:57:28 +00003 *
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 by David S. Miller davem@davemloft.net
Andres Salomon3cfc5352010-10-10 21:42:33 -060011 * Adapted for multiple architectures by Andres Salomon <dilinger@queued.net>
Andres Salomon9bdf6ba2010-08-30 03:57:28 +000012 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/errno.h>
17#include <linux/mutex.h>
18#include <linux/slab.h>
19#include <linux/of.h>
Andres Salomon3cfc5352010-10-10 21:42:33 -060020#include <linux/of_pdt.h>
Andres Salomonf90c34bd2010-10-10 21:49:45 -060021
22static struct of_pdt_ops *of_pdt_prom_ops __initdata;
Andres Salomon9bdf6ba2010-08-30 03:57:28 +000023
Andres Salomon3cfc5352010-10-10 21:42:33 -060024#if defined(CONFIG_SPARC)
25unsigned int of_pdt_unique_id __initdata;
26
27#define of_pdt_incr_unique_id(p) do { \
28 (p)->unique_id = of_pdt_unique_id++; \
29} while (0)
30
Andres Salomona74ea432011-02-23 22:38:22 -080031static char * __init of_pdt_build_full_name(struct device_node *dp)
Andres Salomon3cfc5352010-10-10 21:42:33 -060032{
Rob Herring0c5eaa72018-11-16 15:06:55 -060033 return build_path_component(dp);
Andres Salomon3cfc5352010-10-10 21:42:33 -060034}
35
Andres Salomona74ea432011-02-23 22:38:22 -080036#else /* CONFIG_SPARC */
Andres Salomon3cfc5352010-10-10 21:42:33 -060037
38static inline void of_pdt_incr_unique_id(void *p) { }
39static inline void irq_trans_init(struct device_node *dp) { }
40
Andres Salomona74ea432011-02-23 22:38:22 -080041static char * __init of_pdt_build_full_name(struct device_node *dp)
Andres Salomon3cfc5352010-10-10 21:42:33 -060042{
Andres Salomona74ea432011-02-23 22:38:22 -080043 static int failsafe_id = 0; /* for generating unique names on failure */
Rob Herring0c5eaa72018-11-16 15:06:55 -060044 const char *name;
45 char path[256];
Andres Salomona74ea432011-02-23 22:38:22 -080046 char *buf;
47 int len;
48
Rob Herring0c5eaa72018-11-16 15:06:55 -060049 if (!of_pdt_prom_ops->pkg2path(dp->phandle, path, sizeof(path), &len)) {
50 name = kbasename(path);
51 buf = prom_early_alloc(strlen(name) + 1);
52 strcpy(buf, name);
53 return buf;
54 }
Andres Salomona74ea432011-02-23 22:38:22 -080055
Rob Herring0c5eaa72018-11-16 15:06:55 -060056 name = of_get_property(dp, "name", &len);
57 buf = prom_early_alloc(len + 16);
58 sprintf(buf, "%s@unknown%i", name, failsafe_id++);
Andres Salomona74ea432011-02-23 22:38:22 -080059 pr_err("%s: pkg2path failed; assigning %s\n", __func__, buf);
60 return buf;
Andres Salomon3cfc5352010-10-10 21:42:33 -060061}
62
63#endif /* !CONFIG_SPARC */
Andres Salomon9bdf6ba2010-08-30 03:57:28 +000064
Andres Salomoned418502010-10-10 21:51:25 -060065static struct property * __init of_pdt_build_one_prop(phandle node, char *prev,
Andres Salomon9bdf6ba2010-08-30 03:57:28 +000066 char *special_name,
67 void *special_val,
68 int special_len)
69{
70 static struct property *tmp = NULL;
71 struct property *p;
Andres Salomonf90c34bd2010-10-10 21:49:45 -060072 int err;
Andres Salomon9bdf6ba2010-08-30 03:57:28 +000073
74 if (tmp) {
75 p = tmp;
76 memset(p, 0, sizeof(*p) + 32);
77 tmp = NULL;
78 } else {
79 p = prom_early_alloc(sizeof(struct property) + 32);
Andres Salomon3cfc5352010-10-10 21:42:33 -060080 of_pdt_incr_unique_id(p);
Andres Salomon9bdf6ba2010-08-30 03:57:28 +000081 }
82
83 p->name = (char *) (p + 1);
84 if (special_name) {
85 strcpy(p->name, special_name);
86 p->length = special_len;
87 p->value = prom_early_alloc(special_len);
88 memcpy(p->value, special_val, special_len);
89 } else {
Andres Salomonf90c34bd2010-10-10 21:49:45 -060090 err = of_pdt_prom_ops->nextprop(node, prev, p->name);
91 if (err) {
Andres Salomon9bdf6ba2010-08-30 03:57:28 +000092 tmp = p;
93 return NULL;
94 }
Andres Salomonf90c34bd2010-10-10 21:49:45 -060095 p->length = of_pdt_prom_ops->getproplen(node, p->name);
Andres Salomon9bdf6ba2010-08-30 03:57:28 +000096 if (p->length <= 0) {
97 p->length = 0;
98 } else {
99 int len;
100
101 p->value = prom_early_alloc(p->length + 1);
Andres Salomonf90c34bd2010-10-10 21:49:45 -0600102 len = of_pdt_prom_ops->getproperty(node, p->name,
103 p->value, p->length);
Andres Salomon9bdf6ba2010-08-30 03:57:28 +0000104 if (len <= 0)
105 p->length = 0;
106 ((unsigned char *)p->value)[p->length] = '\0';
107 }
108 }
109 return p;
110}
111
Andres Salomoned418502010-10-10 21:51:25 -0600112static struct property * __init of_pdt_build_prop_list(phandle node)
Andres Salomon9bdf6ba2010-08-30 03:57:28 +0000113{
114 struct property *head, *tail;
115
Andres Salomoned418502010-10-10 21:51:25 -0600116 head = tail = of_pdt_build_one_prop(node, NULL,
Andres Salomon9bdf6ba2010-08-30 03:57:28 +0000117 ".node", &node, sizeof(node));
118
Andres Salomoned418502010-10-10 21:51:25 -0600119 tail->next = of_pdt_build_one_prop(node, NULL, NULL, NULL, 0);
Andres Salomon9bdf6ba2010-08-30 03:57:28 +0000120 tail = tail->next;
121 while(tail) {
Andres Salomoned418502010-10-10 21:51:25 -0600122 tail->next = of_pdt_build_one_prop(node, tail->name,
Andres Salomon9bdf6ba2010-08-30 03:57:28 +0000123 NULL, NULL, 0);
124 tail = tail->next;
125 }
126
127 return head;
128}
129
Andres Salomoned418502010-10-10 21:51:25 -0600130static char * __init of_pdt_get_one_property(phandle node, const char *name)
Andres Salomon9bdf6ba2010-08-30 03:57:28 +0000131{
132 char *buf = "<NULL>";
133 int len;
134
Andres Salomonf90c34bd2010-10-10 21:49:45 -0600135 len = of_pdt_prom_ops->getproplen(node, name);
Andres Salomon9bdf6ba2010-08-30 03:57:28 +0000136 if (len > 0) {
137 buf = prom_early_alloc(len);
Andres Salomonf90c34bd2010-10-10 21:49:45 -0600138 len = of_pdt_prom_ops->getproperty(node, name, buf, len);
Andres Salomon9bdf6ba2010-08-30 03:57:28 +0000139 }
140
141 return buf;
142}
143
Andres Salomoned418502010-10-10 21:51:25 -0600144static struct device_node * __init of_pdt_create_node(phandle node,
Andres Salomon9bdf6ba2010-08-30 03:57:28 +0000145 struct device_node *parent)
146{
147 struct device_node *dp;
148
149 if (!node)
150 return NULL;
151
152 dp = prom_early_alloc(sizeof(*dp));
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200153 of_node_init(dp);
Andres Salomon3cfc5352010-10-10 21:42:33 -0600154 of_pdt_incr_unique_id(dp);
Andres Salomon9bdf6ba2010-08-30 03:57:28 +0000155 dp->parent = parent;
156
Andres Salomona74ea432011-02-23 22:38:22 -0800157 dp->name = of_pdt_get_one_property(node, "name");
Andres Salomon9bdf6ba2010-08-30 03:57:28 +0000158 dp->phandle = node;
159
Andres Salomoned418502010-10-10 21:51:25 -0600160 dp->properties = of_pdt_build_prop_list(node);
Andres Salomon9bdf6ba2010-08-30 03:57:28 +0000161
Rob Herring0c5eaa72018-11-16 15:06:55 -0600162 dp->full_name = of_pdt_build_full_name(dp);
163
Andres Salomon9bdf6ba2010-08-30 03:57:28 +0000164 irq_trans_init(dp);
165
166 return dp;
167}
168
Andres Salomoned418502010-10-10 21:51:25 -0600169static struct device_node * __init of_pdt_build_tree(struct device_node *parent,
Grant Likely5063e252014-10-03 16:28:27 +0100170 phandle node)
Andres Salomon9bdf6ba2010-08-30 03:57:28 +0000171{
172 struct device_node *ret = NULL, *prev_sibling = NULL;
173 struct device_node *dp;
174
175 while (1) {
Andres Salomoned418502010-10-10 21:51:25 -0600176 dp = of_pdt_create_node(node, parent);
Andres Salomon9bdf6ba2010-08-30 03:57:28 +0000177 if (!dp)
178 break;
179
180 if (prev_sibling)
181 prev_sibling->sibling = dp;
182
183 if (!ret)
184 ret = dp;
185 prev_sibling = dp;
186
Grant Likely5063e252014-10-03 16:28:27 +0100187 dp->child = of_pdt_build_tree(dp, of_pdt_prom_ops->getchild(node));
Andres Salomon9bdf6ba2010-08-30 03:57:28 +0000188
Andres Salomonf90c34bd2010-10-10 21:49:45 -0600189 node = of_pdt_prom_ops->getsibling(node);
Andres Salomon9bdf6ba2010-08-30 03:57:28 +0000190 }
191
192 return ret;
193}
194
Sam Ravnborg75c71842011-12-27 22:58:40 +0100195static void * __init kernel_tree_alloc(u64 size, u64 align)
Shawn Guo611cad72011-08-15 15:28:14 +0800196{
197 return prom_early_alloc(size);
198}
199
Andres Salomonf90c34bd2010-10-10 21:49:45 -0600200void __init of_pdt_build_devicetree(phandle root_node, struct of_pdt_ops *ops)
Andres Salomon9bdf6ba2010-08-30 03:57:28 +0000201{
Andres Salomonf90c34bd2010-10-10 21:49:45 -0600202 BUG_ON(!ops);
203 of_pdt_prom_ops = ops;
204
Grant Likely5063e252014-10-03 16:28:27 +0100205 of_root = of_pdt_create_node(root_node, NULL);
Grant Likely5063e252014-10-03 16:28:27 +0100206 of_root->full_name = "/";
Andres Salomon9bdf6ba2010-08-30 03:57:28 +0000207
Grant Likely5063e252014-10-03 16:28:27 +0100208 of_root->child = of_pdt_build_tree(of_root,
209 of_pdt_prom_ops->getchild(of_root->phandle));
Shawn Guo611cad72011-08-15 15:28:14 +0800210
Robert P. J. Day4c7d6362013-05-30 05:38:08 -0400211 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
Shawn Guo611cad72011-08-15 15:28:14 +0800212 of_alias_scan(kernel_tree_alloc);
Andres Salomon9bdf6ba2010-08-30 03:57:28 +0000213}