xref: /linux/arch/nios2/kernel/setup.c (revision c4c14c3bd177ea769fee938674f73a8ec0cdd47a)
1 /*
2  * Nios2-specific parts of system setup
3  *
4  * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
5  * Copyright (C) 2004 Microtronix Datacom Ltd.
6  * Copyright (C) 2001 Vic Phillips <vic@microtronix.com>
7  *
8  * This file is subject to the terms and conditions of the GNU General Public
9  * License. See the file "COPYING" in the main directory of this archive
10  * for more details.
11  */
12 
13 #include <linux/export.h>
14 #include <linux/kernel.h>
15 #include <linux/mm.h>
16 #include <linux/sched.h>
17 #include <linux/sched/task.h>
18 #include <linux/console.h>
19 #include <linux/bootmem.h>
20 #include <linux/memblock.h>
21 #include <linux/initrd.h>
22 #include <linux/of_fdt.h>
23 #include <linux/screen_info.h>
24 
25 #include <asm/mmu_context.h>
26 #include <asm/sections.h>
27 #include <asm/setup.h>
28 #include <asm/cpuinfo.h>
29 
30 unsigned long memory_start;
31 EXPORT_SYMBOL(memory_start);
32 
33 unsigned long memory_end;
34 EXPORT_SYMBOL(memory_end);
35 
36 unsigned long memory_size;
37 
38 static struct pt_regs fake_regs = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
39 					0, 0, 0, 0, 0, 0,
40 					0};
41 
42 #ifdef CONFIG_VT
43 struct screen_info screen_info;
44 #endif
45 
46 /* Copy a short hook instruction sequence to the exception address */
47 static inline void copy_exception_handler(unsigned int addr)
48 {
49 	unsigned int start = (unsigned int) exception_handler_hook;
50 	volatile unsigned int tmp = 0;
51 
52 	if (start == addr) {
53 		/* The CPU exception address already points to the handler. */
54 		return;
55 	}
56 
57 	__asm__ __volatile__ (
58 		"ldw	%2,0(%0)\n"
59 		"stw	%2,0(%1)\n"
60 		"ldw	%2,4(%0)\n"
61 		"stw	%2,4(%1)\n"
62 		"ldw	%2,8(%0)\n"
63 		"stw	%2,8(%1)\n"
64 		"flushd	0(%1)\n"
65 		"flushd	4(%1)\n"
66 		"flushd	8(%1)\n"
67 		"flushi %1\n"
68 		"addi	%1,%1,4\n"
69 		"flushi %1\n"
70 		"addi	%1,%1,4\n"
71 		"flushi %1\n"
72 		"flushp\n"
73 		: /* no output registers */
74 		: "r" (start), "r" (addr), "r" (tmp)
75 		: "memory"
76 	);
77 }
78 
79 /* Copy the fast TLB miss handler */
80 static inline void copy_fast_tlb_miss_handler(unsigned int addr)
81 {
82 	unsigned int start = (unsigned int) fast_handler;
83 	unsigned int end = (unsigned int) fast_handler_end;
84 	volatile unsigned int tmp = 0;
85 
86 	__asm__ __volatile__ (
87 		"1:\n"
88 		"	ldw	%3,0(%0)\n"
89 		"	stw	%3,0(%1)\n"
90 		"	flushd	0(%1)\n"
91 		"	flushi	%1\n"
92 		"	flushp\n"
93 		"	addi	%0,%0,4\n"
94 		"	addi	%1,%1,4\n"
95 		"	bne	%0,%2,1b\n"
96 		: /* no output registers */
97 		: "r" (start), "r" (addr), "r" (end), "r" (tmp)
98 		: "memory"
99 	);
100 }
101 
102 /*
103  * save args passed from u-boot, called from head.S
104  *
105  * @r4: NIOS magic
106  * @r5: initrd start
107  * @r6: initrd end or fdt
108  * @r7: kernel command line
109  */
110 asmlinkage void __init nios2_boot_init(unsigned r4, unsigned r5, unsigned r6,
111 				       unsigned r7)
112 {
113 	unsigned dtb_passed = 0;
114 	char cmdline_passed[COMMAND_LINE_SIZE] __maybe_unused = { 0, };
115 
116 #if defined(CONFIG_NIOS2_PASS_CMDLINE)
117 	if (r4 == 0x534f494e) { /* r4 is magic NIOS */
118 #if defined(CONFIG_BLK_DEV_INITRD)
119 		if (r5) { /* initramfs */
120 			initrd_start = r5;
121 			initrd_end = r6;
122 		}
123 #endif /* CONFIG_BLK_DEV_INITRD */
124 		dtb_passed = r6;
125 
126 		if (r7)
127 			strncpy(cmdline_passed, (char *)r7, COMMAND_LINE_SIZE);
128 	}
129 #endif
130 
131 	early_init_devtree((void *)dtb_passed);
132 
133 #ifndef CONFIG_CMDLINE_FORCE
134 	if (cmdline_passed[0])
135 		strncpy(boot_command_line, cmdline_passed, COMMAND_LINE_SIZE);
136 #ifdef CONFIG_NIOS2_CMDLINE_IGNORE_DTB
137 	else
138 		strncpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
139 #endif
140 #endif
141 
142 	parse_early_param();
143 }
144 
145 void __init setup_arch(char **cmdline_p)
146 {
147 	int dram_start;
148 
149 	console_verbose();
150 
151 	dram_start = memblock_start_of_DRAM();
152 	memory_size = memblock_phys_mem_size();
153 	memory_start = PAGE_ALIGN((unsigned long)__pa(_end));
154 	memory_end = (unsigned long) CONFIG_NIOS2_MEM_BASE + memory_size;
155 
156 	init_mm.start_code = (unsigned long) _stext;
157 	init_mm.end_code = (unsigned long) _etext;
158 	init_mm.end_data = (unsigned long) _edata;
159 	init_mm.brk = (unsigned long) _end;
160 	init_task.thread.kregs = &fake_regs;
161 
162 	/* Keep a copy of command line */
163 	*cmdline_p = boot_command_line;
164 
165 	min_low_pfn = PFN_UP(memory_start);
166 	max_low_pfn = PFN_DOWN(memory_end);
167 	max_mapnr = max_low_pfn;
168 
169 	memblock_reserve(dram_start, memory_start - dram_start);
170 #ifdef CONFIG_BLK_DEV_INITRD
171 	if (initrd_start) {
172 		memblock_reserve(virt_to_phys((void *)initrd_start),
173 				initrd_end - initrd_start);
174 	}
175 #endif /* CONFIG_BLK_DEV_INITRD */
176 
177 	early_init_fdt_reserve_self();
178 	early_init_fdt_scan_reserved_mem();
179 
180 	unflatten_and_copy_device_tree();
181 
182 	setup_cpuinfo();
183 
184 	copy_exception_handler(cpuinfo.exception_addr);
185 
186 	mmu_init();
187 
188 	copy_fast_tlb_miss_handler(cpuinfo.fast_tlb_miss_exc_addr);
189 
190 	/*
191 	 * Initialize MMU context handling here because data from cpuinfo is
192 	 * needed for this.
193 	 */
194 	mmu_context_init();
195 
196 	/*
197 	 * get kmalloc into gear
198 	 */
199 	paging_init();
200 
201 #if defined(CONFIG_VT) && defined(CONFIG_DUMMY_CONSOLE)
202 	conswitchp = &dummy_con;
203 #endif
204 }
205