xref: /openbmc/linux/arch/mips/generic/init.c (revision 18c7e034)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2eed0eabdSPaul Burton /*
3eed0eabdSPaul Burton  * Copyright (C) 2016 Imagination Technologies
448c834beSPaul Burton  * Author: Paul Burton <paul.burton@mips.com>
5eed0eabdSPaul Burton  */
6eed0eabdSPaul Burton 
7eed0eabdSPaul Burton #include <linux/clk.h>
8eed0eabdSPaul Burton #include <linux/clocksource.h>
9eed0eabdSPaul Burton #include <linux/init.h>
10eed0eabdSPaul Burton #include <linux/irqchip.h>
11089a792cSGeert Uytterhoeven #include <linux/of_clk.h>
12eed0eabdSPaul Burton #include <linux/of_fdt.h>
13eed0eabdSPaul Burton 
14b47e9c62SPaul Burton #include <asm/bootinfo.h>
15eed0eabdSPaul Burton #include <asm/fw/fw.h>
16eed0eabdSPaul Burton #include <asm/irq_cpu.h>
17eed0eabdSPaul Burton #include <asm/machine.h>
187f005f11SMatt Redfearn #include <asm/mips-cps.h>
19eed0eabdSPaul Burton #include <asm/prom.h>
20eed0eabdSPaul Burton #include <asm/smp-ops.h>
21eed0eabdSPaul Burton #include <asm/time.h>
22eed0eabdSPaul Burton 
23a14bf1dcSTiezhu Yang static __initconst const void *fdt;
24a14bf1dcSTiezhu Yang static __initconst const struct mips_machine *mach;
25a14bf1dcSTiezhu Yang static __initconst const void *mach_match_data;
26eed0eabdSPaul Burton 
prom_init(void)27eed0eabdSPaul Burton void __init prom_init(void)
28eed0eabdSPaul Burton {
299a59061cSMatt Redfearn 	plat_get_fdt();
309a59061cSMatt Redfearn 	BUG_ON(!fdt);
319a59061cSMatt Redfearn }
329a59061cSMatt Redfearn 
plat_get_fdt(void)339a59061cSMatt Redfearn void __init *plat_get_fdt(void)
349a59061cSMatt Redfearn {
35eed0eabdSPaul Burton 	const struct mips_machine *check_mach;
36eed0eabdSPaul Burton 	const struct of_device_id *match;
37eed0eabdSPaul Burton 
389a59061cSMatt Redfearn 	if (fdt)
399a59061cSMatt Redfearn 		/* Already set up */
409a59061cSMatt Redfearn 		return (void *)fdt;
419a59061cSMatt Redfearn 
42b83ba0b9SThomas Bogendoerfer 	fdt = (void *)get_fdt();
43b83ba0b9SThomas Bogendoerfer 	if (fdt && !fdt_check_header(fdt)) {
44eed0eabdSPaul Burton 		/*
45323690d2SPaul Cercueil 		 * We have been provided with the appropriate device tree for
46323690d2SPaul Cercueil 		 * the board. Make use of it & search for any machine struct
47323690d2SPaul Cercueil 		 * based upon the root compatible string.
48eed0eabdSPaul Burton 		 */
49eed0eabdSPaul Burton 		for_each_mips_machine(check_mach) {
50eed0eabdSPaul Burton 			match = mips_machine_is_compatible(check_mach, fdt);
51eed0eabdSPaul Burton 			if (match) {
52eed0eabdSPaul Burton 				mach = check_mach;
53eed0eabdSPaul Burton 				mach_match_data = match->data;
54eed0eabdSPaul Burton 				break;
55eed0eabdSPaul Burton 			}
56eed0eabdSPaul Burton 		}
57eed0eabdSPaul Burton 	} else if (IS_ENABLED(CONFIG_LEGACY_BOARDS)) {
58eed0eabdSPaul Burton 		/*
59eed0eabdSPaul Burton 		 * We weren't booted using the UHI boot protocol, but do
60eed0eabdSPaul Burton 		 * support some number of boards with legacy boot protocols.
61eed0eabdSPaul Burton 		 * Attempt to find the right one.
62eed0eabdSPaul Burton 		 */
63eed0eabdSPaul Burton 		for_each_mips_machine(check_mach) {
64eed0eabdSPaul Burton 			if (!check_mach->detect)
65eed0eabdSPaul Burton 				continue;
66eed0eabdSPaul Burton 
67eed0eabdSPaul Burton 			if (!check_mach->detect())
68eed0eabdSPaul Burton 				continue;
69eed0eabdSPaul Burton 
70eed0eabdSPaul Burton 			mach = check_mach;
71eed0eabdSPaul Burton 		}
72eed0eabdSPaul Burton 
73eed0eabdSPaul Burton 		/*
74eed0eabdSPaul Burton 		 * If we don't recognise the machine then we can't continue, so
75eed0eabdSPaul Burton 		 * die here.
76eed0eabdSPaul Burton 		 */
77eed0eabdSPaul Burton 		BUG_ON(!mach);
78eed0eabdSPaul Burton 
79eed0eabdSPaul Burton 		/* Retrieve the machine's FDT */
80eed0eabdSPaul Burton 		fdt = mach->fdt;
81eed0eabdSPaul Burton 	}
82eed0eabdSPaul Burton 	return (void *)fdt;
83eed0eabdSPaul Burton }
84eed0eabdSPaul Burton 
85b47e9c62SPaul Burton #ifdef CONFIG_RELOCATABLE
86b47e9c62SPaul Burton 
plat_fdt_relocated(void * new_location)870063fdedSMarcin Nowakowski void __init plat_fdt_relocated(void *new_location)
880063fdedSMarcin Nowakowski {
890063fdedSMarcin Nowakowski 	/*
900063fdedSMarcin Nowakowski 	 * reset fdt as the cached value would point to the location
910063fdedSMarcin Nowakowski 	 * before relocations happened and update the location argument
920063fdedSMarcin Nowakowski 	 * if it was passed using UHI
930063fdedSMarcin Nowakowski 	 */
940063fdedSMarcin Nowakowski 	fdt = NULL;
950063fdedSMarcin Nowakowski 
960063fdedSMarcin Nowakowski 	if (fw_arg0 == -2)
970063fdedSMarcin Nowakowski 		fw_arg1 = (unsigned long)new_location;
980063fdedSMarcin Nowakowski }
990063fdedSMarcin Nowakowski 
100b47e9c62SPaul Burton #endif /* CONFIG_RELOCATABLE */
101b47e9c62SPaul Burton 
plat_mem_setup(void)102eed0eabdSPaul Burton void __init plat_mem_setup(void)
103eed0eabdSPaul Burton {
104eed0eabdSPaul Burton 	if (mach && mach->fixup_fdt)
105eed0eabdSPaul Burton 		fdt = mach->fixup_fdt(fdt, mach_match_data);
106eed0eabdSPaul Burton 
10713a0ea28SPaul Cercueil 	fw_init_cmdline();
108eed0eabdSPaul Burton 	__dt_setup_arch((void *)fdt);
109eed0eabdSPaul Burton }
110eed0eabdSPaul Burton 
device_tree_init(void)111eed0eabdSPaul Burton void __init device_tree_init(void)
112eed0eabdSPaul Burton {
113eed0eabdSPaul Burton 	unflatten_and_copy_device_tree();
114eed0eabdSPaul Burton 	mips_cpc_probe();
115eed0eabdSPaul Burton 
116*18c7e034SSander Vanheule 	if (!register_cps_smp_ops())
117*18c7e034SSander Vanheule 		return;
118*18c7e034SSander Vanheule 	if (!register_vsmp_smp_ops())
119*18c7e034SSander Vanheule 		return;
120*18c7e034SSander Vanheule 
121*18c7e034SSander Vanheule 	register_up_smp_ops();
122eed0eabdSPaul Burton }
123eed0eabdSPaul Burton 
apply_mips_fdt_fixups(void * fdt_out,size_t fdt_out_size,const void * fdt_in,const struct mips_fdt_fixup * fixups)124e889dfcaSPaul Burton int __init apply_mips_fdt_fixups(void *fdt_out, size_t fdt_out_size,
125e889dfcaSPaul Burton 				 const void *fdt_in,
126e889dfcaSPaul Burton 				 const struct mips_fdt_fixup *fixups)
127e889dfcaSPaul Burton {
128e889dfcaSPaul Burton 	int err;
129e889dfcaSPaul Burton 
130e889dfcaSPaul Burton 	err = fdt_open_into(fdt_in, fdt_out, fdt_out_size);
131e889dfcaSPaul Burton 	if (err) {
132e889dfcaSPaul Burton 		pr_err("Failed to open FDT\n");
133e889dfcaSPaul Burton 		return err;
134e889dfcaSPaul Burton 	}
135e889dfcaSPaul Burton 
136e889dfcaSPaul Burton 	for (; fixups->apply; fixups++) {
137e889dfcaSPaul Burton 		err = fixups->apply(fdt_out);
138e889dfcaSPaul Burton 		if (err) {
139e889dfcaSPaul Burton 			pr_err("Failed to apply FDT fixup \"%s\"\n",
140e889dfcaSPaul Burton 			       fixups->description);
141e889dfcaSPaul Burton 			return err;
142e889dfcaSPaul Burton 		}
143e889dfcaSPaul Burton 	}
144e889dfcaSPaul Burton 
145e889dfcaSPaul Burton 	err = fdt_pack(fdt_out);
146e889dfcaSPaul Burton 	if (err)
147e889dfcaSPaul Burton 		pr_err("Failed to pack FDT\n");
148e889dfcaSPaul Burton 	return err;
149e889dfcaSPaul Burton }
150e889dfcaSPaul Burton 
plat_time_init(void)151eed0eabdSPaul Burton void __init plat_time_init(void)
152eed0eabdSPaul Burton {
153eed0eabdSPaul Burton 	struct device_node *np;
154eed0eabdSPaul Burton 	struct clk *clk;
155eed0eabdSPaul Burton 
156eed0eabdSPaul Burton 	of_clk_init(NULL);
157eed0eabdSPaul Burton 
158eed0eabdSPaul Burton 	if (!cpu_has_counter) {
159eed0eabdSPaul Burton 		mips_hpt_frequency = 0;
160eed0eabdSPaul Burton 	} else if (mach && mach->measure_hpt_freq) {
161eed0eabdSPaul Burton 		mips_hpt_frequency = mach->measure_hpt_freq();
162eed0eabdSPaul Burton 	} else {
163eed0eabdSPaul Burton 		np = of_get_cpu_node(0, NULL);
164eed0eabdSPaul Burton 		if (!np) {
165eed0eabdSPaul Burton 			pr_err("Failed to get CPU node\n");
166eed0eabdSPaul Burton 			return;
167eed0eabdSPaul Burton 		}
168eed0eabdSPaul Burton 
169eed0eabdSPaul Burton 		clk = of_clk_get(np, 0);
170eed0eabdSPaul Burton 		if (IS_ERR(clk)) {
171eed0eabdSPaul Burton 			pr_err("Failed to get CPU clock: %ld\n", PTR_ERR(clk));
172eed0eabdSPaul Burton 			return;
173eed0eabdSPaul Burton 		}
174eed0eabdSPaul Burton 
175eed0eabdSPaul Burton 		mips_hpt_frequency = clk_get_rate(clk);
176eed0eabdSPaul Burton 		clk_put(clk);
177eed0eabdSPaul Burton 
178eed0eabdSPaul Burton 		switch (boot_cpu_type()) {
179eed0eabdSPaul Burton 		case CPU_20KC:
180eed0eabdSPaul Burton 		case CPU_25KF:
181eed0eabdSPaul Burton 			/* The counter runs at the CPU clock rate */
182eed0eabdSPaul Burton 			break;
183eed0eabdSPaul Burton 		default:
184eed0eabdSPaul Burton 			/* The counter runs at half the CPU clock rate */
185eed0eabdSPaul Burton 			mips_hpt_frequency /= 2;
186eed0eabdSPaul Burton 			break;
187eed0eabdSPaul Burton 		}
188eed0eabdSPaul Burton 	}
189eed0eabdSPaul Burton 
190ba5d08c0SDaniel Lezcano 	timer_probe();
191eed0eabdSPaul Burton }
192eed0eabdSPaul Burton 
arch_init_irq(void)193eed0eabdSPaul Burton void __init arch_init_irq(void)
194eed0eabdSPaul Burton {
195eed0eabdSPaul Burton 	struct device_node *intc_node;
196eed0eabdSPaul Burton 
197eed0eabdSPaul Burton 	intc_node = of_find_compatible_node(NULL, NULL,
198eed0eabdSPaul Burton 					    "mti,cpu-interrupt-controller");
199eed0eabdSPaul Burton 	if (!cpu_has_veic && !intc_node)
200eed0eabdSPaul Burton 		mips_cpu_irq_init();
20128ec2238SNicholas Mc Guire 	of_node_put(intc_node);
202eed0eabdSPaul Burton 
203eed0eabdSPaul Burton 	irqchip_init();
204eed0eabdSPaul Burton }
205