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-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * This file does the necessary interface mapping between the bootwrapper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * device tree operations and the interface provided by shared source
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * files flatdevicetree.[ch].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Copyright 2007 David Gibson, IBM Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <page.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <libfdt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include "ops.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #define DEBUG	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #define BAD_ERROR(err)	(((err) < 0) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 			 && ((err) != -FDT_ERR_NOTFOUND) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 			 && ((err) != -FDT_ERR_EXISTS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define check_err(err) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	({ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 		if (BAD_ERROR(err) || ((err < 0) && DEBUG)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 			printf("%s():%d  %s\n\r", __func__, __LINE__, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 			       fdt_strerror(err)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 		if (BAD_ERROR(err)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 			exit(); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 		(err < 0) ? -1 : 0; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	})
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define offset_devp(off)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	({ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		unsigned long _offset = (off); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		check_err(_offset) ? NULL : (void *)(_offset+1); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	})
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define devp_offset_find(devp)	(((unsigned long)(devp))-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define devp_offset(devp)	(devp ? ((unsigned long)(devp))-1 : 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) static void *fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) static void *buf; /* = NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define EXPAND_GRANULARITY	1024
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static void expand_buf(int minexpand)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	int size = fdt_totalsize(fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	size = _ALIGN(size + minexpand, EXPAND_GRANULARITY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	buf = platform_ops.realloc(buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		fatal("Couldn't find %d bytes to expand device tree\n\r", size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	rc = fdt_open_into(fdt, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		fatal("Couldn't expand fdt into new buffer: %s\n\r",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		      fdt_strerror(rc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	fdt = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static void *fdt_wrapper_finddevice(const char *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	return offset_devp(fdt_path_offset(fdt, path));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) static int fdt_wrapper_getprop(const void *devp, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			       void *buf, const int buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	const void *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	p = fdt_getprop(fdt, devp_offset(devp), name, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		return check_err(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	memcpy(buf, p, min(len, buflen));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) static int fdt_wrapper_setprop(const void *devp, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			       const void *buf, const int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	rc = fdt_setprop(fdt, devp_offset(devp), name, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	if (rc == -FDT_ERR_NOSPACE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		expand_buf(len + 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		rc = fdt_setprop(fdt, devp_offset(devp), name, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	return check_err(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) static int fdt_wrapper_del_node(const void *devp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	return fdt_del_node(fdt, devp_offset(devp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) static void *fdt_wrapper_get_parent(const void *devp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	return offset_devp(fdt_parent_offset(fdt, devp_offset(devp)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static void *fdt_wrapper_create_node(const void *devp, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	int offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	offset = fdt_add_subnode(fdt, devp_offset(devp), name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (offset == -FDT_ERR_NOSPACE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		expand_buf(strlen(name) + 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		offset = fdt_add_subnode(fdt, devp_offset(devp), name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	return offset_devp(offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static void *fdt_wrapper_find_node_by_prop_value(const void *prev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 						 const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 						 const char *val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 						 int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	int offset = fdt_node_offset_by_prop_value(fdt, devp_offset_find(prev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	                                           name, val, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	return offset_devp(offset);
^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) static void *fdt_wrapper_find_node_by_compatible(const void *prev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 						 const char *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	int offset = fdt_node_offset_by_compatible(fdt, devp_offset_find(prev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	                                           val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	return offset_devp(offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static char *fdt_wrapper_get_path(const void *devp, char *buf, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	rc = fdt_get_path(fdt, devp_offset(devp), buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	if (check_err(rc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	return buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static unsigned long fdt_wrapper_finalize(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	rc = fdt_pack(fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		fatal("Couldn't pack flat tree: %s\n\r",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		      fdt_strerror(rc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	return (unsigned long)fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) void fdt_init(void *blob)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	int bufsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	dt_ops.finddevice = fdt_wrapper_finddevice;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	dt_ops.getprop = fdt_wrapper_getprop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	dt_ops.setprop = fdt_wrapper_setprop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	dt_ops.get_parent = fdt_wrapper_get_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	dt_ops.create_node = fdt_wrapper_create_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	dt_ops.find_node_by_prop_value = fdt_wrapper_find_node_by_prop_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	dt_ops.find_node_by_compatible = fdt_wrapper_find_node_by_compatible;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	dt_ops.del_node = fdt_wrapper_del_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	dt_ops.get_path = fdt_wrapper_get_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	dt_ops.finalize = fdt_wrapper_finalize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	/* Make sure the dt blob is the right version and so forth */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	fdt = blob;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	bufsize = fdt_totalsize(fdt) + EXPAND_GRANULARITY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	buf = malloc(bufsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if(!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		fatal("malloc failed. can't relocate the device tree\n\r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	err = fdt_open_into(fdt, buf, bufsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	if (err != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		fatal("fdt_init(): %s\n\r", fdt_strerror(err));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	fdt = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }