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 © 2013 Tony Breeds IBM Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright © 2013 Alistair Popple IBM Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Based on earlier code:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *   Copyright (C) Paul Mackerras 1997.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *   Matt Porter <mporter@kernel.crashing.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *   Copyright 2002-2005 MontaVista Software Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *   Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *   Copyright (c) 2003, 2004 Zultys Technologies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *    Copyright 2007 David Gibson, IBM Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *    Copyright 2010 Ben. Herrenschmidt, IBM Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  *    Copyright © 2011 David Kleikamp IBM Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <stdarg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include "types.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include "elf.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include "string.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include "stdlib.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include "stdio.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include "page.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include "ops.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include "reg.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include "io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include "dcr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include "4xx.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include "44x.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include "libfdt.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) BSS_STACK(4096);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define SPRN_PIR	0x11E	/* Processor Identification Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define USERDATA_LEN	256	/* Length of userdata passed in by PIBS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define MAX_RANKS	0x4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define DDR3_MR0CF	0x80010011U
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define CCTL0_MCO2	0x8000080FU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define CCTL0_MCO3	0x80000810U
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define CCTL0_MCO4	0x80000811U
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define CCTL0_MCO5	0x80000812U
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define CCTL0_MCO6	0x80000813U
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static unsigned long long ibm_akebono_memsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static long long unsigned mac_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static unsigned long long ibm_akebono_detect_memsize(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	u32 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	unsigned i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	unsigned long long memsize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	for (i = 0; i < MAX_RANKS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		reg = mfdcrx(DDR3_MR0CF + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		if (!(reg & 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		reg &= 0x0000f000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		reg >>= 12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		memsize += (0x800000ULL << reg);
^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) 	return memsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static void ibm_akebono_fixups(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	void *emac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	u32 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	dt_fixup_memory(0x0ULL,  ibm_akebono_memsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	/* Fixup the SD timeout frequency */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	mtdcrx(CCTL0_MCO4, 0x1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	/* Disable SD high-speed mode (which seems to be broken) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	reg = mfdcrx(CCTL0_MCO2) & ~0x2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	mtdcrx(CCTL0_MCO2, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	/* Set the MAC address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	emac = finddevice("/plb/opb/ethernet");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	if (emac > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		if (mac_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 			setprop(emac, "local-mac-address",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 				((u8 *) &mac_addr) + 2 , 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) void platform_init(char *userdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	unsigned long end_of_ram, avail_ram;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	u32 pir_reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	int node, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	const u32 *timebase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	int len, i, userdata_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	char *end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	userdata[USERDATA_LEN - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	userdata_len = strlen(userdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	for (i = 0; i < userdata_len - 15; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		if (strncmp(&userdata[i], "local-mac-addr=", 15) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			if (i > 0 && userdata[i - 1] != ' ') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 				/* We've only found a substring ending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 				 * with local-mac-addr so this isn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 				 * our mac address. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			mac_addr = strtoull(&userdata[i + 15], &end, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			/* Remove the "local-mac-addr=<...>" from the kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			 * command line, including the tailing space if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			 * present. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 			if (*end == ' ')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 				end++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 			len = ((int) end) - ((int) &userdata[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 			memmove(&userdata[i], end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 				userdata_len - (len + i) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			break;
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	loader_info.cmdline = userdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	loader_info.cmdline_len = 256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	ibm_akebono_memsize = ibm_akebono_detect_memsize();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	if (ibm_akebono_memsize >> 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		end_of_ram = ~0UL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		end_of_ram = ibm_akebono_memsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	avail_ram = end_of_ram - (unsigned long)_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	simple_alloc_init(_end, avail_ram, 128, 64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	platform_ops.fixups = ibm_akebono_fixups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	platform_ops.exit = ibm44x_dbcr_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	pir_reg = mfspr(SPRN_PIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	/* Make sure FDT blob is sane */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	if (fdt_check_header(_dtb_start) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		fatal("Invalid device tree blob\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	node = fdt_node_offset_by_prop_value(_dtb_start, -1, "device_type",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 					     "cpu", sizeof("cpu"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		fatal("Cannot find cpu node\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	timebase = fdt_getprop(_dtb_start, node, "timebase-frequency", &size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	if (timebase && (size == 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		timebase_period_ns = 1000000000 / *timebase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	fdt_set_boot_cpuid_phys(_dtb_start, pir_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	fdt_init(_dtb_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	serial_console_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }