xref: /openbmc/linux/arch/mips/loongson64/env.c (revision b58c6630)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Based on Ocelot Linux port, which is
4  * Copyright 2001 MontaVista Software Inc.
5  * Author: jsun@mvista.com or jsun@junsun.net
6  *
7  * Copyright 2003 ICT CAS
8  * Author: Michael Guo <guoyi@ict.ac.cn>
9  *
10  * Copyright (C) 2007 Lemote Inc. & Institute of Computing Technology
11  * Author: Fuxin Zhang, zhangfx@lemote.com
12  *
13  * Copyright (C) 2009 Lemote Inc.
14  * Author: Wu Zhangjin, wuzhangjin@gmail.com
15  */
16 #include <linux/export.h>
17 #include <asm/bootinfo.h>
18 #include <loongson.h>
19 #include <boot_param.h>
20 #include <builtin_dtbs.h>
21 #include <workarounds.h>
22 
23 u32 cpu_clock_freq;
24 EXPORT_SYMBOL(cpu_clock_freq);
25 struct efi_memory_map_loongson *loongson_memmap;
26 struct loongson_system_configuration loongson_sysconf;
27 
28 u64 loongson_chipcfg[MAX_PACKAGES] = {0xffffffffbfc00180};
29 u64 loongson_chiptemp[MAX_PACKAGES];
30 u64 loongson_freqctrl[MAX_PACKAGES];
31 
32 unsigned long long smp_group[4];
33 
34 const char *get_system_type(void)
35 {
36 	return "Generic Loongson64 System";
37 }
38 
39 void __init prom_init_env(void)
40 {
41 	struct boot_params *boot_p;
42 	struct loongson_params *loongson_p;
43 	struct system_loongson *esys;
44 	struct efi_cpuinfo_loongson *ecpu;
45 	struct irq_source_routing_table *eirq_source;
46 
47 	/* firmware arguments are initialized in head.S */
48 	boot_p = (struct boot_params *)fw_arg2;
49 	loongson_p = &(boot_p->efi.smbios.lp);
50 
51 	esys = (struct system_loongson *)
52 		((u64)loongson_p + loongson_p->system_offset);
53 	ecpu = (struct efi_cpuinfo_loongson *)
54 		((u64)loongson_p + loongson_p->cpu_offset);
55 	eirq_source = (struct irq_source_routing_table *)
56 		((u64)loongson_p + loongson_p->irq_offset);
57 	loongson_memmap = (struct efi_memory_map_loongson *)
58 		((u64)loongson_p + loongson_p->memory_offset);
59 
60 	cpu_clock_freq = ecpu->cpu_clock_freq;
61 	loongson_sysconf.cputype = ecpu->cputype;
62 	switch (ecpu->cputype) {
63 	case Legacy_3A:
64 	case Loongson_3A:
65 		loongson_sysconf.cores_per_node = 4;
66 		loongson_sysconf.cores_per_package = 4;
67 		smp_group[0] = 0x900000003ff01000;
68 		smp_group[1] = 0x900010003ff01000;
69 		smp_group[2] = 0x900020003ff01000;
70 		smp_group[3] = 0x900030003ff01000;
71 		loongson_chipcfg[0] = 0x900000001fe00180;
72 		loongson_chipcfg[1] = 0x900010001fe00180;
73 		loongson_chipcfg[2] = 0x900020001fe00180;
74 		loongson_chipcfg[3] = 0x900030001fe00180;
75 		loongson_chiptemp[0] = 0x900000001fe0019c;
76 		loongson_chiptemp[1] = 0x900010001fe0019c;
77 		loongson_chiptemp[2] = 0x900020001fe0019c;
78 		loongson_chiptemp[3] = 0x900030001fe0019c;
79 		loongson_freqctrl[0] = 0x900000001fe001d0;
80 		loongson_freqctrl[1] = 0x900010001fe001d0;
81 		loongson_freqctrl[2] = 0x900020001fe001d0;
82 		loongson_freqctrl[3] = 0x900030001fe001d0;
83 		loongson_sysconf.ht_control_base = 0x90000EFDFB000000;
84 		loongson_sysconf.workarounds = WORKAROUND_CPUFREQ;
85 		break;
86 	case Legacy_3B:
87 	case Loongson_3B:
88 		loongson_sysconf.cores_per_node = 4; /* One chip has 2 nodes */
89 		loongson_sysconf.cores_per_package = 8;
90 		smp_group[0] = 0x900000003ff01000;
91 		smp_group[1] = 0x900010003ff05000;
92 		smp_group[2] = 0x900020003ff09000;
93 		smp_group[3] = 0x900030003ff0d000;
94 		loongson_chipcfg[0] = 0x900000001fe00180;
95 		loongson_chipcfg[1] = 0x900020001fe00180;
96 		loongson_chipcfg[2] = 0x900040001fe00180;
97 		loongson_chipcfg[3] = 0x900060001fe00180;
98 		loongson_chiptemp[0] = 0x900000001fe0019c;
99 		loongson_chiptemp[1] = 0x900020001fe0019c;
100 		loongson_chiptemp[2] = 0x900040001fe0019c;
101 		loongson_chiptemp[3] = 0x900060001fe0019c;
102 		loongson_freqctrl[0] = 0x900000001fe001d0;
103 		loongson_freqctrl[1] = 0x900020001fe001d0;
104 		loongson_freqctrl[2] = 0x900040001fe001d0;
105 		loongson_freqctrl[3] = 0x900060001fe001d0;
106 		loongson_sysconf.ht_control_base = 0x90001EFDFB000000;
107 		loongson_sysconf.workarounds = WORKAROUND_CPUHOTPLUG;
108 		break;
109 	default:
110 		loongson_sysconf.cores_per_node = 1;
111 		loongson_sysconf.cores_per_package = 1;
112 		loongson_chipcfg[0] = 0x900000001fe00180;
113 	}
114 
115 	loongson_sysconf.nr_cpus = ecpu->nr_cpus;
116 	loongson_sysconf.boot_cpu_id = ecpu->cpu_startup_core_id;
117 	loongson_sysconf.reserved_cpus_mask = ecpu->reserved_cores_mask;
118 	if (ecpu->nr_cpus > NR_CPUS || ecpu->nr_cpus == 0)
119 		loongson_sysconf.nr_cpus = NR_CPUS;
120 	loongson_sysconf.nr_nodes = (loongson_sysconf.nr_cpus +
121 		loongson_sysconf.cores_per_node - 1) /
122 		loongson_sysconf.cores_per_node;
123 
124 	if ((read_c0_prid() & PRID_IMP_MASK) == PRID_IMP_LOONGSON_64C) {
125 		switch (read_c0_prid() & PRID_REV_MASK) {
126 		case PRID_REV_LOONGSON3A_R1:
127 		case PRID_REV_LOONGSON3A_R2_0:
128 		case PRID_REV_LOONGSON3A_R2_1:
129 		case PRID_REV_LOONGSON3A_R3_0:
130 		case PRID_REV_LOONGSON3A_R3_1:
131 			loongson_fdt_blob = __dtb_loongson3_4core_rs780e_begin;
132 			break;
133 		case PRID_REV_LOONGSON3B_R1:
134 		case PRID_REV_LOONGSON3B_R2:
135 			loongson_fdt_blob = __dtb_loongson3_8core_rs780e_begin;
136 			break;
137 		default:
138 			break;
139 		}
140 	}
141 
142 
143 	if (!loongson_fdt_blob)
144 		pr_err("Failed to determine built-in Loongson64 dtb\n");
145 
146 	loongson_sysconf.pci_mem_start_addr = eirq_source->pci_mem_start_addr;
147 	loongson_sysconf.pci_mem_end_addr = eirq_source->pci_mem_end_addr;
148 	loongson_sysconf.pci_io_base = eirq_source->pci_io_start_addr;
149 	loongson_sysconf.dma_mask_bits = eirq_source->dma_mask_bits;
150 	if (loongson_sysconf.dma_mask_bits < 32 ||
151 		loongson_sysconf.dma_mask_bits > 64)
152 		loongson_sysconf.dma_mask_bits = 32;
153 
154 	loongson_sysconf.restart_addr = boot_p->reset_system.ResetWarm;
155 	loongson_sysconf.poweroff_addr = boot_p->reset_system.Shutdown;
156 	loongson_sysconf.suspend_addr = boot_p->reset_system.DoSuspend;
157 
158 	loongson_sysconf.vgabios_addr = boot_p->efi.smbios.vga_bios;
159 	pr_debug("Shutdown Addr: %llx, Restart Addr: %llx, VBIOS Addr: %llx\n",
160 		loongson_sysconf.poweroff_addr, loongson_sysconf.restart_addr,
161 		loongson_sysconf.vgabios_addr);
162 
163 	memset(loongson_sysconf.ecname, 0, 32);
164 	if (esys->has_ec)
165 		memcpy(loongson_sysconf.ecname, esys->ec_name, 32);
166 	loongson_sysconf.workarounds |= esys->workarounds;
167 
168 	loongson_sysconf.nr_uarts = esys->nr_uarts;
169 	if (esys->nr_uarts < 1 || esys->nr_uarts > MAX_UARTS)
170 		loongson_sysconf.nr_uarts = 1;
171 	memcpy(loongson_sysconf.uarts, esys->uarts,
172 		sizeof(struct uart_device) * loongson_sysconf.nr_uarts);
173 
174 	loongson_sysconf.nr_sensors = esys->nr_sensors;
175 	if (loongson_sysconf.nr_sensors > MAX_SENSORS)
176 		loongson_sysconf.nr_sensors = 0;
177 	if (loongson_sysconf.nr_sensors)
178 		memcpy(loongson_sysconf.sensors, esys->sensors,
179 			sizeof(struct sensor_device) * loongson_sysconf.nr_sensors);
180 	pr_info("CpuClock = %u\n", cpu_clock_freq);
181 }
182