xref: /linux/arch/mips/generic/init.c (revision e2be04c7f9958dde770eeb8b30e829ca969b37bb)
1 /*
2  * Copyright (C) 2016 Imagination Technologies
3  * Author: Paul Burton <paul.burton@imgtec.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation;  either version 2 of the  License, or (at your
8  * option) any later version.
9  */
10 
11 #include <linux/clk.h>
12 #include <linux/clk-provider.h>
13 #include <linux/clocksource.h>
14 #include <linux/init.h>
15 #include <linux/irqchip.h>
16 #include <linux/of_fdt.h>
17 #include <linux/of_platform.h>
18 
19 #include <asm/bootinfo.h>
20 #include <asm/fw/fw.h>
21 #include <asm/irq_cpu.h>
22 #include <asm/machine.h>
23 #include <asm/mips-cpc.h>
24 #include <asm/prom.h>
25 #include <asm/smp-ops.h>
26 #include <asm/time.h>
27 
28 static __initdata const void *fdt;
29 static __initdata const struct mips_machine *mach;
30 static __initdata const void *mach_match_data;
31 
32 void __init prom_init(void)
33 {
34 	plat_get_fdt();
35 	BUG_ON(!fdt);
36 }
37 
38 void __init *plat_get_fdt(void)
39 {
40 	const struct mips_machine *check_mach;
41 	const struct of_device_id *match;
42 
43 	if (fdt)
44 		/* Already set up */
45 		return (void *)fdt;
46 
47 	if ((fw_arg0 == -2) && !fdt_check_header((void *)fw_arg1)) {
48 		/*
49 		 * We booted using the UHI boot protocol, so we have been
50 		 * provided with the appropriate device tree for the board.
51 		 * Make use of it & search for any machine struct based upon
52 		 * the root compatible string.
53 		 */
54 		fdt = (void *)fw_arg1;
55 
56 		for_each_mips_machine(check_mach) {
57 			match = mips_machine_is_compatible(check_mach, fdt);
58 			if (match) {
59 				mach = check_mach;
60 				mach_match_data = match->data;
61 				break;
62 			}
63 		}
64 	} else if (IS_ENABLED(CONFIG_LEGACY_BOARDS)) {
65 		/*
66 		 * We weren't booted using the UHI boot protocol, but do
67 		 * support some number of boards with legacy boot protocols.
68 		 * Attempt to find the right one.
69 		 */
70 		for_each_mips_machine(check_mach) {
71 			if (!check_mach->detect)
72 				continue;
73 
74 			if (!check_mach->detect())
75 				continue;
76 
77 			mach = check_mach;
78 		}
79 
80 		/*
81 		 * If we don't recognise the machine then we can't continue, so
82 		 * die here.
83 		 */
84 		BUG_ON(!mach);
85 
86 		/* Retrieve the machine's FDT */
87 		fdt = mach->fdt;
88 	}
89 	return (void *)fdt;
90 }
91 
92 #ifdef CONFIG_RELOCATABLE
93 
94 void __init plat_fdt_relocated(void *new_location)
95 {
96 	/*
97 	 * reset fdt as the cached value would point to the location
98 	 * before relocations happened and update the location argument
99 	 * if it was passed using UHI
100 	 */
101 	fdt = NULL;
102 
103 	if (fw_arg0 == -2)
104 		fw_arg1 = (unsigned long)new_location;
105 }
106 
107 #endif /* CONFIG_RELOCATABLE */
108 
109 void __init plat_mem_setup(void)
110 {
111 	if (mach && mach->fixup_fdt)
112 		fdt = mach->fixup_fdt(fdt, mach_match_data);
113 
114 	strlcpy(arcs_cmdline, boot_command_line, COMMAND_LINE_SIZE);
115 	__dt_setup_arch((void *)fdt);
116 }
117 
118 void __init device_tree_init(void)
119 {
120 	int err;
121 
122 	unflatten_and_copy_device_tree();
123 	mips_cpc_probe();
124 
125 	err = register_cps_smp_ops();
126 	if (err)
127 		err = register_up_smp_ops();
128 }
129 
130 int __init apply_mips_fdt_fixups(void *fdt_out, size_t fdt_out_size,
131 				 const void *fdt_in,
132 				 const struct mips_fdt_fixup *fixups)
133 {
134 	int err;
135 
136 	err = fdt_open_into(fdt_in, fdt_out, fdt_out_size);
137 	if (err) {
138 		pr_err("Failed to open FDT\n");
139 		return err;
140 	}
141 
142 	for (; fixups->apply; fixups++) {
143 		err = fixups->apply(fdt_out);
144 		if (err) {
145 			pr_err("Failed to apply FDT fixup \"%s\"\n",
146 			       fixups->description);
147 			return err;
148 		}
149 	}
150 
151 	err = fdt_pack(fdt_out);
152 	if (err)
153 		pr_err("Failed to pack FDT\n");
154 	return err;
155 }
156 
157 void __init plat_time_init(void)
158 {
159 	struct device_node *np;
160 	struct clk *clk;
161 
162 	of_clk_init(NULL);
163 
164 	if (!cpu_has_counter) {
165 		mips_hpt_frequency = 0;
166 	} else if (mach && mach->measure_hpt_freq) {
167 		mips_hpt_frequency = mach->measure_hpt_freq();
168 	} else {
169 		np = of_get_cpu_node(0, NULL);
170 		if (!np) {
171 			pr_err("Failed to get CPU node\n");
172 			return;
173 		}
174 
175 		clk = of_clk_get(np, 0);
176 		if (IS_ERR(clk)) {
177 			pr_err("Failed to get CPU clock: %ld\n", PTR_ERR(clk));
178 			return;
179 		}
180 
181 		mips_hpt_frequency = clk_get_rate(clk);
182 		clk_put(clk);
183 
184 		switch (boot_cpu_type()) {
185 		case CPU_20KC:
186 		case CPU_25KF:
187 			/* The counter runs at the CPU clock rate */
188 			break;
189 		default:
190 			/* The counter runs at half the CPU clock rate */
191 			mips_hpt_frequency /= 2;
192 			break;
193 		}
194 	}
195 
196 	timer_probe();
197 }
198 
199 void __init arch_init_irq(void)
200 {
201 	struct device_node *intc_node;
202 
203 	intc_node = of_find_compatible_node(NULL, NULL,
204 					    "mti,cpu-interrupt-controller");
205 	if (!cpu_has_veic && !intc_node)
206 		mips_cpu_irq_init();
207 
208 	irqchip_init();
209 }
210 
211 static int __init publish_devices(void)
212 {
213 	if (!of_have_populated_dt())
214 		panic("Device-tree not present");
215 
216 	if (of_platform_populate(NULL, of_default_bus_match_table, NULL, NULL))
217 		panic("Failed to populate DT");
218 
219 	return 0;
220 }
221 arch_initcall(publish_devices);
222 
223 void __init prom_free_prom_memory(void)
224 {
225 }
226