Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /* pdt.c: OF PROM device tree support code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Paul Mackerras	August 1996.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 1996-2005 Paul Mackerras.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *    {engebret|bergner}@us.ibm.com
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *  Adapted for sparc by David S. Miller davem@davemloft.net
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *  Adapted for multiple architectures by Andres Salomon <dilinger@queued.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/of_pdt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) static struct of_pdt_ops *of_pdt_prom_ops __initdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #if defined(CONFIG_SPARC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) unsigned int of_pdt_unique_id __initdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define of_pdt_incr_unique_id(p) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	(p)->unique_id = of_pdt_unique_id++; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) static char * __init of_pdt_build_full_name(struct device_node *dp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	return build_path_component(dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #else /* CONFIG_SPARC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) static inline void of_pdt_incr_unique_id(void *p) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) static inline void irq_trans_init(struct device_node *dp) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) static char * __init of_pdt_build_full_name(struct device_node *dp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	static int failsafe_id = 0; /* for generating unique names on failure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	char path[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	if (!of_pdt_prom_ops->pkg2path(dp->phandle, path, sizeof(path), &len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		name = kbasename(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		buf = prom_early_alloc(strlen(name) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		strcpy(buf, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		return buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	name = of_get_property(dp, "name", &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	buf = prom_early_alloc(len + 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	sprintf(buf, "%s@unknown%i", name, failsafe_id++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	pr_err("%s: pkg2path failed; assigning %s\n", __func__, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	return buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) #endif /* !CONFIG_SPARC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) static struct property * __init of_pdt_build_one_prop(phandle node, char *prev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 					       char *special_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 					       void *special_val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 					       int special_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	static struct property *tmp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	struct property *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (tmp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		p = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		memset(p, 0, sizeof(*p) + 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		tmp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		p = prom_early_alloc(sizeof(struct property) + 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		of_pdt_incr_unique_id(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	p->name = (char *) (p + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	if (special_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		strcpy(p->name, special_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		p->length = special_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		p->value = prom_early_alloc(special_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		memcpy(p->value, special_val, special_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		err = of_pdt_prom_ops->nextprop(node, prev, p->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			tmp = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		p->length = of_pdt_prom_ops->getproplen(node, p->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		if (p->length <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			p->length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			p->value = prom_early_alloc(p->length + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			len = of_pdt_prom_ops->getproperty(node, p->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 					p->value, p->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			if (len <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 				p->length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			((unsigned char *)p->value)[p->length] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	return p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static struct property * __init of_pdt_build_prop_list(phandle node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	struct property *head, *tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	head = tail = of_pdt_build_one_prop(node, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 				     ".node", &node, sizeof(node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	tail->next = of_pdt_build_one_prop(node, NULL, NULL, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	tail = tail->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	while(tail) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		tail->next = of_pdt_build_one_prop(node, tail->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 					    NULL, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		tail = tail->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	return head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static char * __init of_pdt_get_one_property(phandle node, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	char *buf = "<NULL>";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	len = of_pdt_prom_ops->getproplen(node, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		buf = prom_early_alloc(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		len = of_pdt_prom_ops->getproperty(node, name, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	return buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static struct device_node * __init of_pdt_create_node(phandle node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 						    struct device_node *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	struct device_node *dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	dp = prom_early_alloc(sizeof(*dp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	of_node_init(dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	of_pdt_incr_unique_id(dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	dp->parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	dp->name = of_pdt_get_one_property(node, "name");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	dp->phandle = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	dp->properties = of_pdt_build_prop_list(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	dp->full_name = of_pdt_build_full_name(dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	irq_trans_init(dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	return dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static struct device_node * __init of_pdt_build_tree(struct device_node *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 						   phandle node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	struct device_node *ret = NULL, *prev_sibling = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	struct device_node *dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		dp = of_pdt_create_node(node, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		if (!dp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		if (prev_sibling)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 			prev_sibling->sibling = dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 			ret = dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		prev_sibling = dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		dp->child = of_pdt_build_tree(dp, of_pdt_prom_ops->getchild(node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		node = of_pdt_prom_ops->getsibling(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static void * __init kernel_tree_alloc(u64 size, u64 align)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	return prom_early_alloc(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) void __init of_pdt_build_devicetree(phandle root_node, struct of_pdt_ops *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	BUG_ON(!ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	of_pdt_prom_ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	of_root = of_pdt_create_node(root_node, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	of_root->full_name = "/";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	of_root->child = of_pdt_build_tree(of_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 				of_pdt_prom_ops->getchild(of_root->phandle));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	/* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	of_alias_scan(kernel_tree_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }