1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * arch/sh/kernel/setup.c
4  *
5  * This file handles the architecture-dependent parts of initialization
6  *
7  *  Copyright (C) 1999  Niibe Yutaka
8  *  Copyright (C) 2002 - 2010 Paul Mundt
9  */
10 #include <linux/ioport.h>
11 #include <linux/init.h>
12 #include <linux/initrd.h>
13 #include <linux/console.h>
14 #include <linux/root_dev.h>
15 #include <linux/utsname.h>
16 #include <linux/nodemask.h>
17 #include <linux/cpu.h>
18 #include <linux/pfn.h>
19 #include <linux/fs.h>
20 #include <linux/mm.h>
21 #include <linux/kexec.h>
22 #include <linux/module.h>
23 #include <linux/smp.h>
24 #include <linux/err.h>
25 #include <linux/crash_dump.h>
26 #include <linux/mmzone.h>
27 #include <linux/clk.h>
28 #include <linux/delay.h>
29 #include <linux/platform_device.h>
30 #include <linux/memblock.h>
31 #include <linux/of.h>
32 #include <linux/of_fdt.h>
33 #include <linux/uaccess.h>
34 #include <uapi/linux/mount.h>
35 #include <asm/io.h>
36 #include <asm/page.h>
37 #include <asm/elf.h>
38 #include <asm/sections.h>
39 #include <asm/irq.h>
40 #include <asm/setup.h>
41 #include <asm/clock.h>
42 #include <asm/smp.h>
43 #include <asm/mmu_context.h>
44 #include <asm/mmzone.h>
45 #include <asm/processor.h>
46 #include <asm/sparsemem.h>
47 #include <asm/platform_early.h>
48 
49 /*
50  * Initialize loops_per_jiffy as 10000000 (1000MIPS).
51  * This value will be used at the very early stage of serial setup.
52  * The bigger value means no problem.
53  */
54 struct sh_cpuinfo cpu_data[NR_CPUS] __read_mostly = {
55 	[0] = {
56 		.type			= CPU_SH_NONE,
57 		.family			= CPU_FAMILY_UNKNOWN,
58 		.loops_per_jiffy	= 10000000,
59 		.phys_bits		= MAX_PHYSMEM_BITS,
60 	},
61 };
62 EXPORT_SYMBOL(cpu_data);
63 
64 /*
65  * The machine vector. First entry in .machvec.init, or clobbered by
66  * sh_mv= on the command line, prior to .machvec.init teardown.
67  */
68 struct sh_machine_vector sh_mv = { .mv_name = "generic", };
69 EXPORT_SYMBOL(sh_mv);
70 
71 extern int root_mountflags;
72 
73 #define RAMDISK_IMAGE_START_MASK	0x07FF
74 #define RAMDISK_PROMPT_FLAG		0x8000
75 #define RAMDISK_LOAD_FLAG		0x4000
76 
77 static char __initdata command_line[COMMAND_LINE_SIZE] = { 0, };
78 
79 static struct resource code_resource = {
80 	.name = "Kernel code",
81 	.flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
82 };
83 
84 static struct resource data_resource = {
85 	.name = "Kernel data",
86 	.flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
87 };
88 
89 static struct resource bss_resource = {
90 	.name	= "Kernel bss",
91 	.flags	= IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
92 };
93 
94 unsigned long memory_start;
95 EXPORT_SYMBOL(memory_start);
96 unsigned long memory_end = 0;
97 EXPORT_SYMBOL(memory_end);
98 unsigned long memory_limit = 0;
99 
100 static struct resource mem_resources[MAX_NUMNODES];
101 
102 int l1i_cache_shape, l1d_cache_shape, l2_cache_shape;
103 
early_parse_mem(char * p)104 static int __init early_parse_mem(char *p)
105 {
106 	if (!p)
107 		return 1;
108 
109 	memory_limit = PAGE_ALIGN(memparse(p, &p));
110 
111 	pr_notice("Memory limited to %ldMB\n", memory_limit >> 20);
112 
113 	return 0;
114 }
115 early_param("mem", early_parse_mem);
116 
check_for_initrd(void)117 void __init check_for_initrd(void)
118 {
119 #ifdef CONFIG_BLK_DEV_INITRD
120 	unsigned long start, end;
121 
122 	/*
123 	 * Check for the rare cases where boot loaders adhere to the boot
124 	 * ABI.
125 	 */
126 	if (!LOADER_TYPE || !INITRD_START || !INITRD_SIZE)
127 		goto disable;
128 
129 	start = INITRD_START + __MEMORY_START;
130 	end = start + INITRD_SIZE;
131 
132 	if (unlikely(end <= start))
133 		goto disable;
134 	if (unlikely(start & ~PAGE_MASK)) {
135 		pr_err("initrd must be page aligned\n");
136 		goto disable;
137 	}
138 
139 	if (unlikely(start < __MEMORY_START)) {
140 		pr_err("initrd start (%08lx) < __MEMORY_START(%x)\n",
141 			start, __MEMORY_START);
142 		goto disable;
143 	}
144 
145 	if (unlikely(end > memblock_end_of_DRAM())) {
146 		pr_err("initrd extends beyond end of memory "
147 		       "(0x%08lx > 0x%08lx)\ndisabling initrd\n",
148 		       end, (unsigned long)memblock_end_of_DRAM());
149 		goto disable;
150 	}
151 
152 	/*
153 	 * If we got this far in spite of the boot loader's best efforts
154 	 * to the contrary, assume we actually have a valid initrd and
155 	 * fix up the root dev.
156 	 */
157 	ROOT_DEV = Root_RAM0;
158 
159 	/*
160 	 * Address sanitization
161 	 */
162 	initrd_start = (unsigned long)__va(start);
163 	initrd_end = initrd_start + INITRD_SIZE;
164 
165 	memblock_reserve(__pa(initrd_start), INITRD_SIZE);
166 
167 	return;
168 
169 disable:
170 	pr_info("initrd disabled\n");
171 	initrd_start = initrd_end = 0;
172 #endif
173 }
174 
175 #ifndef CONFIG_GENERIC_CALIBRATE_DELAY
calibrate_delay(void)176 void calibrate_delay(void)
177 {
178 	struct clk *clk = clk_get(NULL, "cpu_clk");
179 
180 	if (IS_ERR(clk))
181 		panic("Need a sane CPU clock definition!");
182 
183 	loops_per_jiffy = (clk_get_rate(clk) >> 1) / HZ;
184 
185 	printk(KERN_INFO "Calibrating delay loop (skipped)... "
186 			 "%lu.%02lu BogoMIPS PRESET (lpj=%lu)\n",
187 			 loops_per_jiffy/(500000/HZ),
188 			 (loops_per_jiffy/(5000/HZ)) % 100,
189 			 loops_per_jiffy);
190 }
191 #endif
192 
__add_active_range(unsigned int nid,unsigned long start_pfn,unsigned long end_pfn)193 void __init __add_active_range(unsigned int nid, unsigned long start_pfn,
194 						unsigned long end_pfn)
195 {
196 	struct resource *res = &mem_resources[nid];
197 	unsigned long start, end;
198 
199 	WARN_ON(res->name); /* max one active range per node for now */
200 
201 	start = start_pfn << PAGE_SHIFT;
202 	end = end_pfn << PAGE_SHIFT;
203 
204 	res->name = "System RAM";
205 	res->start = start;
206 	res->end = end - 1;
207 	res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
208 
209 	if (request_resource(&iomem_resource, res)) {
210 		pr_err("unable to request memory_resource 0x%lx 0x%lx\n",
211 		       start_pfn, end_pfn);
212 		return;
213 	}
214 
215 	/*
216 	 * We don't know which RAM region contains kernel data or
217 	 * the reserved crashkernel region, so try it repeatedly
218 	 * and let the resource manager test it.
219 	 */
220 	request_resource(res, &code_resource);
221 	request_resource(res, &data_resource);
222 	request_resource(res, &bss_resource);
223 #ifdef CONFIG_CRASH_RESERVE
224 	request_resource(res, &crashk_res);
225 #endif
226 
227 	/*
228 	 * Also make sure that there is a PMB mapping that covers this
229 	 * range before we attempt to activate it, to avoid reset by MMU.
230 	 * We can hit this path with NUMA or memory hot-add.
231 	 */
232 	pmb_bolt_mapping((unsigned long)__va(start), start, end - start,
233 			 PAGE_KERNEL);
234 
235 	memblock_set_node(PFN_PHYS(start_pfn), PFN_PHYS(end_pfn - start_pfn),
236 			  &memblock.memory, nid);
237 }
238 
plat_early_device_setup(void)239 void __init __weak plat_early_device_setup(void)
240 {
241 }
242 
243 #ifdef CONFIG_OF_EARLY_FLATTREE
sh_fdt_init(phys_addr_t dt_phys)244 void __ref sh_fdt_init(phys_addr_t dt_phys)
245 {
246 	static int done = 0;
247 	void *dt_virt;
248 
249 	/* Avoid calling an __init function on secondary cpus. */
250 	if (done) return;
251 
252 #ifdef CONFIG_USE_BUILTIN_DTB
253 	dt_virt = __dtb_start;
254 #else
255 	dt_virt = phys_to_virt(dt_phys);
256 #endif
257 
258 	if (!dt_virt || !early_init_dt_scan(dt_virt)) {
259 		pr_crit("Error: invalid device tree blob"
260 			" at physical address %p\n", (void *)dt_phys);
261 
262 		while (true)
263 			cpu_relax();
264 	}
265 
266 	done = 1;
267 }
268 #endif
269 
setup_arch(char ** cmdline_p)270 void __init setup_arch(char **cmdline_p)
271 {
272 	enable_mmu();
273 
274 	ROOT_DEV = old_decode_dev(ORIG_ROOT_DEV);
275 
276 	printk(KERN_NOTICE "Boot params:\n"
277 			   "... MOUNT_ROOT_RDONLY - %08lx\n"
278 			   "... RAMDISK_FLAGS     - %08lx\n"
279 			   "... ORIG_ROOT_DEV     - %08lx\n"
280 			   "... LOADER_TYPE       - %08lx\n"
281 			   "... INITRD_START      - %08lx\n"
282 			   "... INITRD_SIZE       - %08lx\n",
283 			   MOUNT_ROOT_RDONLY, RAMDISK_FLAGS,
284 			   ORIG_ROOT_DEV, LOADER_TYPE,
285 			   INITRD_START, INITRD_SIZE);
286 
287 #ifdef CONFIG_BLK_DEV_RAM
288 	rd_image_start = RAMDISK_FLAGS & RAMDISK_IMAGE_START_MASK;
289 #endif
290 
291 	if (!MOUNT_ROOT_RDONLY)
292 		root_mountflags &= ~MS_RDONLY;
293 	setup_initial_init_mm(_text, _etext, _edata, _end);
294 
295 	code_resource.start = virt_to_phys(_text);
296 	code_resource.end = virt_to_phys(_etext)-1;
297 	data_resource.start = virt_to_phys(_etext);
298 	data_resource.end = virt_to_phys(_edata)-1;
299 	bss_resource.start = virt_to_phys(__bss_start);
300 	bss_resource.end = virt_to_phys(__bss_stop)-1;
301 
302 #ifdef CONFIG_CMDLINE_OVERWRITE
303 	strscpy(command_line, CONFIG_CMDLINE, sizeof(command_line));
304 #else
305 	strscpy(command_line, COMMAND_LINE, sizeof(command_line));
306 #ifdef CONFIG_CMDLINE_EXTEND
307 	strlcat(command_line, " ", sizeof(command_line));
308 	strlcat(command_line, CONFIG_CMDLINE, sizeof(command_line));
309 #endif
310 #endif
311 
312 	/* Save unparsed command line copy for /proc/cmdline */
313 	memcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
314 	*cmdline_p = command_line;
315 
316 	parse_early_param();
317 
318 	plat_early_device_setup();
319 
320 	sh_mv_setup();
321 
322 	/* Let earlyprintk output early console messages */
323 	sh_early_platform_driver_probe("earlyprintk", 1, 1);
324 
325 #ifdef CONFIG_OF_EARLY_FLATTREE
326 #ifdef CONFIG_USE_BUILTIN_DTB
327 	unflatten_and_copy_device_tree();
328 #else
329 	unflatten_device_tree();
330 #endif
331 #endif
332 
333 	paging_init();
334 
335 	/* Perform the machine specific initialisation */
336 	if (likely(sh_mv.mv_setup))
337 		sh_mv.mv_setup(cmdline_p);
338 
339 	plat_smp_setup();
340 }
341 
342 /* processor boot mode configuration */
generic_mode_pins(void)343 int generic_mode_pins(void)
344 {
345 	pr_warn("generic_mode_pins(): missing mode pin configuration\n");
346 	return 0;
347 }
348 
test_mode_pin(int pin)349 int test_mode_pin(int pin)
350 {
351 	return sh_mv.mv_mode_pins() & pin;
352 }
353 
arch_cpu_finalize_init(void)354 void __init arch_cpu_finalize_init(void)
355 {
356 	char *p = &init_utsname()->machine[2]; /* "sh" */
357 
358 	select_idle_routine();
359 
360 	current_cpu_data.loops_per_jiffy = loops_per_jiffy;
361 
362 	switch (current_cpu_data.family) {
363 	case CPU_FAMILY_SH2:
364 		*p++ = '2';
365 		break;
366 	case CPU_FAMILY_SH2A:
367 		*p++ = '2';
368 		*p++ = 'a';
369 		break;
370 	case CPU_FAMILY_SH3:
371 		*p++ = '3';
372 		break;
373 	case CPU_FAMILY_SH4:
374 		*p++ = '4';
375 		break;
376 	case CPU_FAMILY_SH4A:
377 		*p++ = '4';
378 		*p++ = 'a';
379 		break;
380 	case CPU_FAMILY_SH4AL_DSP:
381 		*p++ = '4';
382 		*p++ = 'a';
383 		*p++ = 'l';
384 		*p++ = '-';
385 		*p++ = 'd';
386 		*p++ = 's';
387 		*p++ = 'p';
388 		break;
389 	case CPU_FAMILY_UNKNOWN:
390 		/*
391 		 * Specifically use CPU_FAMILY_UNKNOWN rather than
392 		 * default:, so we're able to have the compiler whine
393 		 * about unhandled enumerations.
394 		 */
395 		break;
396 	}
397 
398 	pr_info("CPU: %s\n", get_cpu_subtype(&current_cpu_data));
399 
400 #ifndef __LITTLE_ENDIAN__
401 	/* 'eb' means 'Endian Big' */
402 	*p++ = 'e';
403 	*p++ = 'b';
404 #endif
405 	*p = '\0';
406 }
407