xref: /openbmc/linux/arch/powerpc/platforms/pseries/of_helpers.c (revision ead5d1f4d877e92c051e1a1ade623d0d30e71619)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2948ad1acSAndy Shevchenko #include <linux/string.h>
3948ad1acSAndy Shevchenko #include <linux/err.h>
4948ad1acSAndy Shevchenko #include <linux/slab.h>
5948ad1acSAndy Shevchenko #include <linux/of.h>
6e83636acSMichael Bringmann #include <asm/prom.h>
7948ad1acSAndy Shevchenko 
8948ad1acSAndy Shevchenko #include "of_helpers.h"
9948ad1acSAndy Shevchenko 
10948ad1acSAndy Shevchenko /**
11948ad1acSAndy Shevchenko  * pseries_of_derive_parent - basically like dirname(1)
12948ad1acSAndy Shevchenko  * @path:  the full_name of a node to be added to the tree
13948ad1acSAndy Shevchenko  *
14948ad1acSAndy Shevchenko  * Returns the node which should be the parent of the node
15948ad1acSAndy Shevchenko  * described by path.  E.g., for path = "/foo/bar", returns
16948ad1acSAndy Shevchenko  * the node with full_name = "/foo".
17948ad1acSAndy Shevchenko  */
pseries_of_derive_parent(const char * path)18948ad1acSAndy Shevchenko struct device_node *pseries_of_derive_parent(const char *path)
19948ad1acSAndy Shevchenko {
20dc85aaedSAndy Shevchenko 	struct device_node *parent;
21948ad1acSAndy Shevchenko 	char *parent_path = "/";
22f755ecfbSNathan Fontenot 	const char *tail;
23f755ecfbSNathan Fontenot 
24f755ecfbSNathan Fontenot 	/* We do not want the trailing '/' character */
25f755ecfbSNathan Fontenot 	tail = kbasename(path) - 1;
26948ad1acSAndy Shevchenko 
27948ad1acSAndy Shevchenko 	/* reject if path is "/" */
28948ad1acSAndy Shevchenko 	if (!strcmp(path, "/"))
29948ad1acSAndy Shevchenko 		return ERR_PTR(-EINVAL);
30948ad1acSAndy Shevchenko 
31f755ecfbSNathan Fontenot 	if (tail > path) {
32a46d9884SAndy Shevchenko 		parent_path = kstrndup(path, tail - path, GFP_KERNEL);
33948ad1acSAndy Shevchenko 		if (!parent_path)
34948ad1acSAndy Shevchenko 			return ERR_PTR(-ENOMEM);
35948ad1acSAndy Shevchenko 	}
36948ad1acSAndy Shevchenko 	parent = of_find_node_by_path(parent_path);
37948ad1acSAndy Shevchenko 	if (strcmp(parent_path, "/"))
38948ad1acSAndy Shevchenko 		kfree(parent_path);
39dc85aaedSAndy Shevchenko 	return parent ? parent : ERR_PTR(-EINVAL);
40948ad1acSAndy Shevchenko }
41e83636acSMichael Bringmann 
42e83636acSMichael Bringmann 
43e83636acSMichael Bringmann /* Helper Routines to convert between drc_index to cpu numbers */
44e83636acSMichael Bringmann 
of_read_drc_info_cell(struct property ** prop,const __be32 ** curval,struct of_drc_info * data)45e83636acSMichael Bringmann int of_read_drc_info_cell(struct property **prop, const __be32 **curval,
46e83636acSMichael Bringmann 			struct of_drc_info *data)
47e83636acSMichael Bringmann {
4857409d4fSTyrel Datwyler 	const char *p = (char *)(*curval);
49e83636acSMichael Bringmann 	const __be32 *p2;
50e83636acSMichael Bringmann 
51e83636acSMichael Bringmann 	if (!data)
52e83636acSMichael Bringmann 		return -EINVAL;
53e83636acSMichael Bringmann 
54e83636acSMichael Bringmann 	/* Get drc-type:encode-string */
5557409d4fSTyrel Datwyler 	data->drc_type = (char *)p;
56e83636acSMichael Bringmann 	p = of_prop_next_string(*prop, p);
57e83636acSMichael Bringmann 	if (!p)
58e83636acSMichael Bringmann 		return -EINVAL;
59e83636acSMichael Bringmann 
60e83636acSMichael Bringmann 	/* Get drc-name-prefix:encode-string */
61e83636acSMichael Bringmann 	data->drc_name_prefix = (char *)p;
62e83636acSMichael Bringmann 	p = of_prop_next_string(*prop, p);
63e83636acSMichael Bringmann 	if (!p)
64e83636acSMichael Bringmann 		return -EINVAL;
65e83636acSMichael Bringmann 
66e83636acSMichael Bringmann 	/* Get drc-index-start:encode-int */
67e83636acSMichael Bringmann 	p2 = (const __be32 *)p;
6857409d4fSTyrel Datwyler 	data->drc_index_start = be32_to_cpu(*p2);
69e83636acSMichael Bringmann 
70e83636acSMichael Bringmann 	/* Get drc-name-suffix-start:encode-int */
71e83636acSMichael Bringmann 	p2 = of_prop_next_u32(*prop, p2, &data->drc_name_suffix_start);
72e83636acSMichael Bringmann 	if (!p2)
73e83636acSMichael Bringmann 		return -EINVAL;
74e83636acSMichael Bringmann 
75e83636acSMichael Bringmann 	/* Get number-sequential-elements:encode-int */
76e83636acSMichael Bringmann 	p2 = of_prop_next_u32(*prop, p2, &data->num_sequential_elems);
77e83636acSMichael Bringmann 	if (!p2)
78e83636acSMichael Bringmann 		return -EINVAL;
79e83636acSMichael Bringmann 
80e83636acSMichael Bringmann 	/* Get sequential-increment:encode-int */
81e83636acSMichael Bringmann 	p2 = of_prop_next_u32(*prop, p2, &data->sequential_inc);
82e83636acSMichael Bringmann 	if (!p2)
83e83636acSMichael Bringmann 		return -EINVAL;
84e83636acSMichael Bringmann 
85e83636acSMichael Bringmann 	/* Get drc-power-domain:encode-int */
86e83636acSMichael Bringmann 	p2 = of_prop_next_u32(*prop, p2, &data->drc_power_domain);
87e83636acSMichael Bringmann 	if (!p2)
88e83636acSMichael Bringmann 		return -EINVAL;
89e83636acSMichael Bringmann 
90e83636acSMichael Bringmann 	/* Should now know end of current entry */
91*c5e76fa0STyrel Datwyler 	(*curval) = (void *)(++p2);
92e83636acSMichael Bringmann 	data->last_drc_index = data->drc_index_start +
93e83636acSMichael Bringmann 		((data->num_sequential_elems - 1) * data->sequential_inc);
94e83636acSMichael Bringmann 
95e83636acSMichael Bringmann 	return 0;
96e83636acSMichael Bringmann }
97e83636acSMichael Bringmann EXPORT_SYMBOL(of_read_drc_info_cell);
98