127d22413SLey Foon Tan /*
227d22413SLey Foon Tan * Nios2-specific parts of system setup
327d22413SLey Foon Tan *
427d22413SLey Foon Tan * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
527d22413SLey Foon Tan * Copyright (C) 2004 Microtronix Datacom Ltd.
627d22413SLey Foon Tan * Copyright (C) 2001 Vic Phillips <vic@microtronix.com>
727d22413SLey Foon Tan *
827d22413SLey Foon Tan * This file is subject to the terms and conditions of the GNU General Public
927d22413SLey Foon Tan * License. See the file "COPYING" in the main directory of this archive
1027d22413SLey Foon Tan * for more details.
1127d22413SLey Foon Tan */
1227d22413SLey Foon Tan
1327d22413SLey Foon Tan #include <linux/export.h>
1427d22413SLey Foon Tan #include <linux/kernel.h>
1527d22413SLey Foon Tan #include <linux/mm.h>
1627d22413SLey Foon Tan #include <linux/sched.h>
179164bb4aSIngo Molnar #include <linux/sched/task.h>
1827d22413SLey Foon Tan #include <linux/console.h>
19a811c05cSMike Rapoport #include <linux/memblock.h>
2027d22413SLey Foon Tan #include <linux/initrd.h>
2127d22413SLey Foon Tan #include <linux/of_fdt.h>
2227d22413SLey Foon Tan
2327d22413SLey Foon Tan #include <asm/mmu_context.h>
2427d22413SLey Foon Tan #include <asm/sections.h>
2527d22413SLey Foon Tan #include <asm/setup.h>
2627d22413SLey Foon Tan #include <asm/cpuinfo.h>
2727d22413SLey Foon Tan
2827d22413SLey Foon Tan unsigned long memory_start;
2927d22413SLey Foon Tan EXPORT_SYMBOL(memory_start);
3027d22413SLey Foon Tan
3127d22413SLey Foon Tan unsigned long memory_end;
3227d22413SLey Foon Tan EXPORT_SYMBOL(memory_end);
3327d22413SLey Foon Tan
3427d22413SLey Foon Tan static struct pt_regs fake_regs = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3527d22413SLey Foon Tan 0, 0, 0, 0, 0, 0,
3627d22413SLey Foon Tan 0};
3727d22413SLey Foon Tan
3827d22413SLey Foon Tan /* Copy a short hook instruction sequence to the exception address */
copy_exception_handler(unsigned int addr)3927d22413SLey Foon Tan static inline void copy_exception_handler(unsigned int addr)
4027d22413SLey Foon Tan {
4127d22413SLey Foon Tan unsigned int start = (unsigned int) exception_handler_hook;
4227d22413SLey Foon Tan volatile unsigned int tmp = 0;
4327d22413SLey Foon Tan
4427d22413SLey Foon Tan if (start == addr) {
4527d22413SLey Foon Tan /* The CPU exception address already points to the handler. */
4627d22413SLey Foon Tan return;
4727d22413SLey Foon Tan }
4827d22413SLey Foon Tan
4927d22413SLey Foon Tan __asm__ __volatile__ (
5027d22413SLey Foon Tan "ldw %2,0(%0)\n"
5127d22413SLey Foon Tan "stw %2,0(%1)\n"
5227d22413SLey Foon Tan "ldw %2,4(%0)\n"
5327d22413SLey Foon Tan "stw %2,4(%1)\n"
5427d22413SLey Foon Tan "ldw %2,8(%0)\n"
5527d22413SLey Foon Tan "stw %2,8(%1)\n"
5627d22413SLey Foon Tan "flushd 0(%1)\n"
5727d22413SLey Foon Tan "flushd 4(%1)\n"
5827d22413SLey Foon Tan "flushd 8(%1)\n"
5927d22413SLey Foon Tan "flushi %1\n"
6027d22413SLey Foon Tan "addi %1,%1,4\n"
6127d22413SLey Foon Tan "flushi %1\n"
6227d22413SLey Foon Tan "addi %1,%1,4\n"
6327d22413SLey Foon Tan "flushi %1\n"
6427d22413SLey Foon Tan "flushp\n"
6527d22413SLey Foon Tan : /* no output registers */
6627d22413SLey Foon Tan : "r" (start), "r" (addr), "r" (tmp)
6727d22413SLey Foon Tan : "memory"
6827d22413SLey Foon Tan );
6927d22413SLey Foon Tan }
7027d22413SLey Foon Tan
7127d22413SLey Foon Tan /* Copy the fast TLB miss handler */
copy_fast_tlb_miss_handler(unsigned int addr)7227d22413SLey Foon Tan static inline void copy_fast_tlb_miss_handler(unsigned int addr)
7327d22413SLey Foon Tan {
7427d22413SLey Foon Tan unsigned int start = (unsigned int) fast_handler;
7527d22413SLey Foon Tan unsigned int end = (unsigned int) fast_handler_end;
7627d22413SLey Foon Tan volatile unsigned int tmp = 0;
7727d22413SLey Foon Tan
7827d22413SLey Foon Tan __asm__ __volatile__ (
7927d22413SLey Foon Tan "1:\n"
8027d22413SLey Foon Tan " ldw %3,0(%0)\n"
8127d22413SLey Foon Tan " stw %3,0(%1)\n"
8227d22413SLey Foon Tan " flushd 0(%1)\n"
8327d22413SLey Foon Tan " flushi %1\n"
8427d22413SLey Foon Tan " flushp\n"
8527d22413SLey Foon Tan " addi %0,%0,4\n"
8627d22413SLey Foon Tan " addi %1,%1,4\n"
8727d22413SLey Foon Tan " bne %0,%2,1b\n"
8827d22413SLey Foon Tan : /* no output registers */
8927d22413SLey Foon Tan : "r" (start), "r" (addr), "r" (end), "r" (tmp)
9027d22413SLey Foon Tan : "memory"
9127d22413SLey Foon Tan );
9227d22413SLey Foon Tan }
9327d22413SLey Foon Tan
9427d22413SLey Foon Tan /*
9527d22413SLey Foon Tan * save args passed from u-boot, called from head.S
9627d22413SLey Foon Tan *
9727d22413SLey Foon Tan * @r4: NIOS magic
9827d22413SLey Foon Tan * @r5: initrd start
9927d22413SLey Foon Tan * @r6: initrd end or fdt
10027d22413SLey Foon Tan * @r7: kernel command line
10127d22413SLey Foon Tan */
nios2_boot_init(unsigned r4,unsigned r5,unsigned r6,unsigned r7)10227d22413SLey Foon Tan asmlinkage void __init nios2_boot_init(unsigned r4, unsigned r5, unsigned r6,
10327d22413SLey Foon Tan unsigned r7)
10427d22413SLey Foon Tan {
10527d22413SLey Foon Tan unsigned dtb_passed = 0;
1064db2196dSMarek Vasut char cmdline_passed[COMMAND_LINE_SIZE] __maybe_unused = { 0, };
10727d22413SLey Foon Tan
10827d22413SLey Foon Tan #if defined(CONFIG_NIOS2_PASS_CMDLINE)
10927d22413SLey Foon Tan if (r4 == 0x534f494e) { /* r4 is magic NIOS */
11027d22413SLey Foon Tan #if defined(CONFIG_BLK_DEV_INITRD)
11127d22413SLey Foon Tan if (r5) { /* initramfs */
11227d22413SLey Foon Tan initrd_start = r5;
11327d22413SLey Foon Tan initrd_end = r6;
11427d22413SLey Foon Tan }
11527d22413SLey Foon Tan #endif /* CONFIG_BLK_DEV_INITRD */
11627d22413SLey Foon Tan dtb_passed = r6;
11727d22413SLey Foon Tan
11827d22413SLey Foon Tan if (r7)
119*6a22e017SAzeem Shaikh strscpy(cmdline_passed, (char *)r7, COMMAND_LINE_SIZE);
12027d22413SLey Foon Tan }
12127d22413SLey Foon Tan #endif
12227d22413SLey Foon Tan
12327d22413SLey Foon Tan early_init_devtree((void *)dtb_passed);
12427d22413SLey Foon Tan
12527d22413SLey Foon Tan #ifndef CONFIG_CMDLINE_FORCE
12627d22413SLey Foon Tan if (cmdline_passed[0])
127*6a22e017SAzeem Shaikh strscpy(boot_command_line, cmdline_passed, COMMAND_LINE_SIZE);
12827d22413SLey Foon Tan #ifdef CONFIG_NIOS2_CMDLINE_IGNORE_DTB
12927d22413SLey Foon Tan else
130*6a22e017SAzeem Shaikh strscpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
13127d22413SLey Foon Tan #endif
13227d22413SLey Foon Tan #endif
13344a4ed42STobias Klauser
13444a4ed42STobias Klauser parse_early_param();
13527d22413SLey Foon Tan }
13627d22413SLey Foon Tan
find_limits(unsigned long * min,unsigned long * max_low,unsigned long * max_high)1377f7bc20bSAndreas Oetken static void __init find_limits(unsigned long *min, unsigned long *max_low,
1387f7bc20bSAndreas Oetken unsigned long *max_high)
1397f7bc20bSAndreas Oetken {
1407f7bc20bSAndreas Oetken *max_low = PFN_DOWN(memblock_get_current_limit());
1417f7bc20bSAndreas Oetken *min = PFN_UP(memblock_start_of_DRAM());
1427f7bc20bSAndreas Oetken *max_high = PFN_DOWN(memblock_end_of_DRAM());
1437f7bc20bSAndreas Oetken }
1447f7bc20bSAndreas Oetken
setup_arch(char ** cmdline_p)14527d22413SLey Foon Tan void __init setup_arch(char **cmdline_p)
14627d22413SLey Foon Tan {
14727d22413SLey Foon Tan console_verbose();
14827d22413SLey Foon Tan
1497f7bc20bSAndreas Oetken memory_start = memblock_start_of_DRAM();
1507f7bc20bSAndreas Oetken memory_end = memblock_end_of_DRAM();
15127d22413SLey Foon Tan
1524154267aSKefeng Wang setup_initial_init_mm(_stext, _etext, _edata, _end);
15327d22413SLey Foon Tan init_task.thread.kregs = &fake_regs;
15427d22413SLey Foon Tan
15527d22413SLey Foon Tan /* Keep a copy of command line */
15627d22413SLey Foon Tan *cmdline_p = boot_command_line;
15727d22413SLey Foon Tan
1587f7bc20bSAndreas Oetken find_limits(&min_low_pfn, &max_low_pfn, &max_pfn);
15927d22413SLey Foon Tan max_mapnr = max_low_pfn;
16027d22413SLey Foon Tan
1617f7bc20bSAndreas Oetken memblock_reserve(__pa_symbol(_stext), _end - _stext);
16227d22413SLey Foon Tan #ifdef CONFIG_BLK_DEV_INITRD
16327d22413SLey Foon Tan if (initrd_start) {
16400423792SMike Rapoport memblock_reserve(virt_to_phys((void *)initrd_start),
16500423792SMike Rapoport initrd_end - initrd_start);
16627d22413SLey Foon Tan }
16727d22413SLey Foon Tan #endif /* CONFIG_BLK_DEV_INITRD */
16827d22413SLey Foon Tan
169921d701eSTobias Klauser early_init_fdt_reserve_self();
170921d701eSTobias Klauser early_init_fdt_scan_reserved_mem();
171921d701eSTobias Klauser
17227d22413SLey Foon Tan unflatten_and_copy_device_tree();
17327d22413SLey Foon Tan
17427d22413SLey Foon Tan setup_cpuinfo();
17527d22413SLey Foon Tan
17627d22413SLey Foon Tan copy_exception_handler(cpuinfo.exception_addr);
17727d22413SLey Foon Tan
17827d22413SLey Foon Tan mmu_init();
17927d22413SLey Foon Tan
18027d22413SLey Foon Tan copy_fast_tlb_miss_handler(cpuinfo.fast_tlb_miss_exc_addr);
18127d22413SLey Foon Tan
18227d22413SLey Foon Tan /*
18327d22413SLey Foon Tan * Initialize MMU context handling here because data from cpuinfo is
18427d22413SLey Foon Tan * needed for this.
18527d22413SLey Foon Tan */
18627d22413SLey Foon Tan mmu_context_init();
18727d22413SLey Foon Tan
18827d22413SLey Foon Tan /*
18927d22413SLey Foon Tan * get kmalloc into gear
19027d22413SLey Foon Tan */
19127d22413SLey Foon Tan paging_init();
19227d22413SLey Foon Tan }
193