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)  * Copyright (C) Paul Mackerras 1997.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include "types.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include "elf.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include "string.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include "stdio.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include "page.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include "ops.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "of.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) typedef u32 prom_arg_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) /* The following structure is used to communicate with open firmware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * All arguments in and out are in big endian format. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) struct prom_args {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	__be32 service;	/* Address of service name string. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	__be32 nargs;	/* Number of input arguments. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	__be32 nret;	/* Number of output arguments. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	__be32 args[10];	/* Input/output arguments. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #ifdef __powerpc64__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) extern int prom(void *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) static int (*prom) (void *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) void of_init(void *promptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #ifndef __powerpc64__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	prom = (int (*)(void *))promptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define ADDR(x)		(u32)(unsigned long)(x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) int of_call_prom(const char *service, int nargs, int nret, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	struct prom_args args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	va_list list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	args.service = cpu_to_be32(ADDR(service));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	args.nargs = cpu_to_be32(nargs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	args.nret = cpu_to_be32(nret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	va_start(list, nret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	for (i = 0; i < nargs; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	va_end(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	for (i = 0; i < nret; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		args.args[nargs+i] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	if (prom(&args) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		return PROM_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) static int of_call_prom_ret(const char *service, int nargs, int nret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			    prom_arg_t *rets, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	struct prom_args args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	va_list list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	args.service = cpu_to_be32(ADDR(service));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	args.nargs = cpu_to_be32(nargs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	args.nret = cpu_to_be32(nret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	va_start(list, rets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	for (i = 0; i < nargs; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	va_end(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	for (i = 0; i < nret; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		args.args[nargs+i] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	if (prom(&args) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		return PROM_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	if (rets != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		for (i = 1; i < nret; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			rets[i-1] = be32_to_cpu(args.args[nargs+i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
^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) /* returns true if s2 is a prefix of s1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) static int string_match(const char *s1, const char *s2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	for (; *s2; ++s2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		if (*s1++ != *s2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^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)  * Older OF's require that when claiming a specific range of addresses,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)  * we claim the physical space in the /memory node and the virtual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)  * space in the chosen mmu node, and then do a map operation to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)  * map virtual to physical.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static int need_map = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static ihandle chosen_mmu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static ihandle memory;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static int check_of_version(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	phandle oprom, chosen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	char version[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	oprom = of_finddevice("/openprom");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	if (oprom == (phandle) -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (of_getprop(oprom, "model", version, sizeof(version)) <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	version[sizeof(version)-1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	printf("OF version = '%s'\r\n", version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	if (!string_match(version, "Open Firmware, 1.")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	    && !string_match(version, "FirmWorks,3."))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	chosen = of_finddevice("/chosen");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	if (chosen == (phandle) -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		chosen = of_finddevice("/chosen@0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		if (chosen == (phandle) -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			printf("no chosen\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (of_getprop(chosen, "mmu", &chosen_mmu, sizeof(chosen_mmu)) <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		printf("no mmu\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	memory = of_call_prom("open", 1, 1, "/memory");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	if (memory == PROM_ERROR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		memory = of_call_prom("open", 1, 1, "/memory@0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		if (memory == PROM_ERROR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			printf("no memory node\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	printf("old OF detected\r\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) unsigned int of_claim(unsigned long virt, unsigned long size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		      unsigned long align)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	prom_arg_t result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (need_map < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		need_map = check_of_version();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	if (align || !need_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		return of_call_prom("claim", 3, 1, virt, size, align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	ret = of_call_prom_ret("call-method", 5, 2, &result, "claim", memory,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			       align, size, virt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	if (ret != 0 || result == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		return  -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	ret = of_call_prom_ret("call-method", 5, 2, &result, "claim", chosen_mmu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			       align, size, virt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	/* 0x12 == coherent + read/write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	ret = of_call_prom("call-method", 6, 1, "map", chosen_mmu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 			   0x12, size, virt, virt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	return virt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) void *of_vmlinux_alloc(unsigned long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	unsigned long start = (unsigned long)_start, end = (unsigned long)_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	unsigned long addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	void *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	/* With some older POWER4 firmware we need to claim the area the kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	 * will reside in.  Newer firmwares don't need this so we just ignore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	 * the return value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	addr = (unsigned long) of_claim(start, end - start, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	printf("Trying to claim from 0x%lx to 0x%lx (0x%lx) got %lx\r\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	       start, end, end - start, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	p = malloc(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		fatal("Can't allocate memory for kernel image!\n\r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	return p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) void of_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	of_call_prom("exit", 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)  * OF device tree routines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) void *of_finddevice(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	return (void *) (unsigned long) of_call_prom("finddevice", 1, 1, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) int of_getprop(const void *phandle, const char *name, void *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	       const int buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	return of_call_prom("getprop", 4, 1, phandle, name, buf, buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) int of_setprop(const void *phandle, const char *name, const void *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	       const int buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	return of_call_prom("setprop", 4, 1, phandle, name, buf, buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }