1 /*
2  * Copyright (C) 2007-2009 Michal Simek <monstr@monstr.eu>
3  * Copyright (C) 2007-2009 PetaLogix
4  * Copyright (C) 2006 Atmark Techno, Inc.
5  *
6  * This file is subject to the terms and conditions of the GNU General Public
7  * License. See the file "COPYING" in the main directory of this archive
8  * for more details.
9  */
10 
11 #include <linux/init.h>
12 #include <linux/clk-provider.h>
13 #include <linux/clocksource.h>
14 #include <linux/string.h>
15 #include <linux/seq_file.h>
16 #include <linux/cpu.h>
17 #include <linux/initrd.h>
18 #include <linux/console.h>
19 #include <linux/debugfs.h>
20 #include <linux/of_fdt.h>
21 
22 #include <asm/setup.h>
23 #include <asm/sections.h>
24 #include <asm/page.h>
25 #include <linux/io.h>
26 #include <linux/bug.h>
27 #include <linux/param.h>
28 #include <linux/pci.h>
29 #include <linux/cache.h>
30 #include <linux/of.h>
31 #include <linux/dma-mapping.h>
32 #include <asm/cacheflush.h>
33 #include <asm/entry.h>
34 #include <asm/cpuinfo.h>
35 
36 #include <asm/pgtable.h>
37 
38 DEFINE_PER_CPU(unsigned int, KSP);	/* Saved kernel stack pointer */
39 DEFINE_PER_CPU(unsigned int, KM);	/* Kernel/user mode */
40 DEFINE_PER_CPU(unsigned int, ENTRY_SP);	/* Saved SP on kernel entry */
41 DEFINE_PER_CPU(unsigned int, R11_SAVE);	/* Temp variable for entry */
42 DEFINE_PER_CPU(unsigned int, CURRENT_SAVE);	/* Saved current pointer */
43 
44 unsigned int boot_cpuid;
45 /*
46  * Placed cmd_line to .data section because can be initialized from
47  * ASM code. Default position is BSS section which is cleared
48  * in machine_early_init().
49  */
50 char cmd_line[COMMAND_LINE_SIZE] __attribute__ ((section(".data")));
51 
setup_arch(char ** cmdline_p)52 void __init setup_arch(char **cmdline_p)
53 {
54 	*cmdline_p = boot_command_line;
55 
56 	setup_memory();
57 	parse_early_param();
58 
59 	console_verbose();
60 
61 	unflatten_device_tree();
62 
63 	setup_cpuinfo();
64 
65 	microblaze_cache_init();
66 
67 	xilinx_pci_init();
68 
69 #if defined(CONFIG_DUMMY_CONSOLE)
70 	conswitchp = &dummy_con;
71 #endif
72 }
73 
74 #ifdef CONFIG_MTD_UCLINUX
75 /* Handle both romfs and cramfs types, without generating unnecessary
76  code (ie no point checking for CRAMFS if it's not even enabled) */
get_romfs_len(unsigned * addr)77 inline unsigned get_romfs_len(unsigned *addr)
78 {
79 #ifdef CONFIG_ROMFS_FS
80 	if (memcmp(&addr[0], "-rom1fs-", 8) == 0) /* romfs */
81 		return be32_to_cpu(addr[2]);
82 #endif
83 
84 #ifdef CONFIG_CRAMFS
85 	if (addr[0] == le32_to_cpu(0x28cd3d45)) /* cramfs */
86 		return le32_to_cpu(addr[1]);
87 #endif
88 	return 0;
89 }
90 #endif	/* CONFIG_MTD_UCLINUX_EBSS */
91 
92 unsigned long kernel_tlb;
93 
machine_early_init(const char * cmdline,unsigned int ram,unsigned int fdt,unsigned int msr,unsigned int tlb0,unsigned int tlb1)94 void __init machine_early_init(const char *cmdline, unsigned int ram,
95 		unsigned int fdt, unsigned int msr, unsigned int tlb0,
96 		unsigned int tlb1)
97 {
98 	unsigned long *src, *dst;
99 	unsigned int offset = 0;
100 
101 	/* If CONFIG_MTD_UCLINUX is defined, assume ROMFS is at the
102 	 * end of kernel. There are two position which we want to check.
103 	 * The first is __init_end and the second __bss_start.
104 	 */
105 #ifdef CONFIG_MTD_UCLINUX
106 	int romfs_size;
107 	unsigned int romfs_base;
108 	char *old_klimit = klimit;
109 
110 	romfs_base = (ram ? ram : (unsigned int)&__init_end);
111 	romfs_size = PAGE_ALIGN(get_romfs_len((unsigned *)romfs_base));
112 	if (!romfs_size) {
113 		romfs_base = (unsigned int)&__bss_start;
114 		romfs_size = PAGE_ALIGN(get_romfs_len((unsigned *)romfs_base));
115 	}
116 
117 	/* Move ROMFS out of BSS before clearing it */
118 	if (romfs_size > 0) {
119 		memmove(&__bss_stop, (int *)romfs_base, romfs_size);
120 		klimit += romfs_size;
121 	}
122 #endif
123 
124 /* clearing bss section */
125 	memset(__bss_start, 0, __bss_stop-__bss_start);
126 	memset(_ssbss, 0, _esbss-_ssbss);
127 
128 /* initialize device tree for usage in early_printk */
129 	early_init_devtree(_fdt_start);
130 
131 	/* setup kernel_tlb after BSS cleaning
132 	 * Maybe worth to move to asm code */
133 	kernel_tlb = tlb0 + tlb1;
134 	/* printk("TLB1 0x%08x, TLB0 0x%08x, tlb 0x%x\n", tlb0,
135 							tlb1, kernel_tlb); */
136 
137 	pr_info("Ramdisk addr 0x%08x, ", ram);
138 	if (fdt)
139 		pr_info("FDT at 0x%08x\n", fdt);
140 	else
141 		pr_info("Compiled-in FDT at %p\n", _fdt_start);
142 
143 #ifdef CONFIG_MTD_UCLINUX
144 	pr_info("Found romfs @ 0x%08x (0x%08x)\n",
145 			romfs_base, romfs_size);
146 	pr_info("#### klimit %p ####\n", old_klimit);
147 	BUG_ON(romfs_size < 0); /* What else can we do? */
148 
149 	pr_info("Moved 0x%08x bytes from 0x%08x to 0x%08x\n",
150 			romfs_size, romfs_base, (unsigned)&__bss_stop);
151 
152 	pr_info("New klimit: 0x%08x\n", (unsigned)klimit);
153 #endif
154 
155 #if CONFIG_XILINX_MICROBLAZE0_USE_MSR_INSTR
156 	if (msr) {
157 		pr_info("!!!Your kernel has setup MSR instruction but ");
158 		pr_cont("CPU don't have it %x\n", msr);
159 	}
160 #else
161 	if (!msr) {
162 		pr_info("!!!Your kernel not setup MSR instruction but ");
163 		pr_cont("CPU have it %x\n", msr);
164 	}
165 #endif
166 
167 	/* Do not copy reset vectors. offset = 0x2 means skip the first
168 	 * two instructions. dst is pointer to MB vectors which are placed
169 	 * in block ram. If you want to copy reset vector setup offset to 0x0 */
170 #if !CONFIG_MANUAL_RESET_VECTOR
171 	offset = 0x2;
172 #endif
173 	dst = (unsigned long *) (offset * sizeof(u32));
174 	for (src = __ivt_start + offset; src < __ivt_end; src++, dst++)
175 		*dst = *src;
176 
177 	/* Initialize global data */
178 	per_cpu(KM, 0) = 0x1;	/* We start in kernel mode */
179 	per_cpu(CURRENT_SAVE, 0) = (unsigned long)current;
180 }
181 
time_init(void)182 void __init time_init(void)
183 {
184 	of_clk_init(NULL);
185 	setup_cpuinfo_clk();
186 	timer_probe();
187 }
188 
189 #ifdef CONFIG_DEBUG_FS
190 struct dentry *of_debugfs_root;
191 
microblaze_debugfs_init(void)192 static int microblaze_debugfs_init(void)
193 {
194 	of_debugfs_root = debugfs_create_dir("microblaze", NULL);
195 
196 	return of_debugfs_root == NULL;
197 }
198 arch_initcall(microblaze_debugfs_init);
199 
200 # ifdef CONFIG_MMU
debugfs_tlb(void)201 static int __init debugfs_tlb(void)
202 {
203 	struct dentry *d;
204 
205 	if (!of_debugfs_root)
206 		return -ENODEV;
207 
208 	d = debugfs_create_u32("tlb_skip", S_IRUGO, of_debugfs_root, &tlb_skip);
209 	if (!d)
210 		return -ENOMEM;
211 
212 	return 0;
213 }
214 device_initcall(debugfs_tlb);
215 # endif
216 #endif
217