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