/*1* Copyright (C) 2007-2009 Michal Simek <[email protected]>2* Copyright (C) 2007-2009 PetaLogix3* Copyright (C) 2006 Atmark Techno, Inc.4*5* MMU code derived from arch/ppc/kernel/head_4xx.S:6* Copyright (c) 1995-1996 Gary Thomas <[email protected]>7* Initial PowerPC version.8* Copyright (c) 1996 Cort Dougan <[email protected]>9* Rewritten for PReP10* Copyright (c) 1996 Paul Mackerras <[email protected]>11* Low-level exception handers, MMU support, and rewrite.12* Copyright (c) 1997 Dan Malek <[email protected]>13* PowerPC 8xx modifications.14* Copyright (c) 1998-1999 TiVo, Inc.15* PowerPC 403GCX modifications.16* Copyright (c) 1999 Grant Erickson <[email protected]>17* PowerPC 403GCX/405GP modifications.18* Copyright 2000 MontaVista Software Inc.19* PPC405 modifications20* PowerPC 403GCX/405GP modifications.21* Author: MontaVista Software, Inc.22* [email protected] or [email protected]23* [email protected]24*25* This file is subject to the terms and conditions of the GNU General Public26* License. See the file "COPYING" in the main directory of this archive27* for more details.28*/2930#include <linux/init.h>31#include <linux/linkage.h>32#include <asm/thread_info.h>33#include <asm/page.h>34#include <linux/of_fdt.h> /* for OF_DT_HEADER */3536#include <asm/setup.h> /* COMMAND_LINE_SIZE */37#include <asm/mmu.h>38#include <asm/processor.h>3940.section .data41.global empty_zero_page42.align 1243empty_zero_page:44.space PAGE_SIZE45.global swapper_pg_dir46swapper_pg_dir:47.space PAGE_SIZE4849.section .rodata50.align 451endian_check:52.word 15354__HEAD55ENTRY(_start)56#if CONFIG_KERNEL_BASE_ADDR == 057brai TOPHYS(real_start)58.org 0x10059real_start:60#endif6162mts rmsr, r063/* Disable stack protection from bootloader */64mts rslr, r065addi r8, r0, 0xFFFFFFFF66mts rshr, r867/*68* According to Xilinx, msrclr instruction behaves like 'mfs rX,rpc'69* if the msrclr instruction is not enabled. We use this to detect70* if the opcode is available, by issuing msrclr and then testing the result.71* r8 == 0 - msr instructions are implemented72* r8 != 0 - msr instructions are not implemented73*/74mfs r1, rmsr75msrclr r8, 0 /* clear nothing - just read msr for test */76cmpu r8, r8, r1 /* r1 must contain msr reg content */7778/* r7 may point to an FDT, or there may be one linked in.79if it's in r7, we've got to save it away ASAP.80We ensure r7 points to a valid FDT, just in case the bootloader81is broken or non-existent */82beqi r7, no_fdt_arg /* NULL pointer? don't copy */83/* Does r7 point to a valid FDT? Load HEADER magic number */84/* Run time Big/Little endian platform */85/* Save 1 as word and load byte - 0 - BIG, 1 - LITTLE */86lbui r11, r0, TOPHYS(endian_check)87beqid r11, big_endian /* DO NOT break delay stop dependency */88lw r11, r0, r7 /* Big endian load in delay slot */89lwr r11, r0, r7 /* Little endian load */90big_endian:91rsubi r11, r11, OF_DT_HEADER /* Check FDT header */92beqi r11, _prepare_copy_fdt93or r7, r0, r0 /* clear R7 when not valid DTB */94bnei r11, no_fdt_arg /* No - get out of here */95_prepare_copy_fdt:96or r11, r0, r0 /* incremment */97ori r4, r0, TOPHYS(_fdt_start)98ori r3, r0, (0x10000 - 4)99_copy_fdt:100lw r12, r7, r11 /* r12 = r7 + r11 */101sw r12, r4, r11 /* addr[r4 + r11] = r12 */102addik r11, r11, 4 /* increment counting */103bgtid r3, _copy_fdt /* loop for all entries */104addik r3, r3, -4 /* descrement loop */105no_fdt_arg:106107#ifndef CONFIG_CMDLINE_BOOL108/*109* handling command line110* copy command line directly to cmd_line placed in data section.111*/112beqid r5, skip /* Skip if NULL pointer */113or r11, r0, r0 /* incremment */114ori r4, r0, cmd_line /* load address of command line */115tophys(r4,r4) /* convert to phys address */116ori r3, r0, COMMAND_LINE_SIZE - 1 /* number of loops */117_copy_command_line:118/* r2=r5+r11 - r5 contain pointer to command line */119lbu r2, r5, r11120beqid r2, skip /* Skip if no data */121sb r2, r4, r11 /* addr[r4+r11]= r2 */122addik r11, r11, 1 /* increment counting */123bgtid r3, _copy_command_line /* loop for all entries */124addik r3, r3, -1 /* decrement loop */125addik r5, r4, 0 /* add new space for command line */126tovirt(r5,r5)127skip:128#endif /* CONFIG_CMDLINE_BOOL */129130#ifdef NOT_COMPILE131/* save bram context */132or r11, r0, r0 /* incremment */133ori r4, r0, TOPHYS(_bram_load_start) /* save bram context */134ori r3, r0, (LMB_SIZE - 4)135_copy_bram:136lw r7, r0, r11 /* r7 = r0 + r11 */137sw r7, r4, r11 /* addr[r4 + r11] = r7 */138addik r11, r11, 4 /* increment counting */139bgtid r3, _copy_bram /* loop for all entries */140addik r3, r3, -4 /* descrement loop */141#endif142/* We have to turn on the MMU right away. */143144/*145* Set up the initial MMU state so we can do the first level of146* kernel initialization. This maps the first 16 MBytes of memory 1:1147* virtual to physical.148*/149nop150addik r3, r0, MICROBLAZE_TLB_SIZE -1 /* Invalidate all TLB entries */151_invalidate:152mts rtlbx, r3153mts rtlbhi, r0 /* flush: ensure V is clear */154mts rtlblo, r0155bgtid r3, _invalidate /* loop for all entries */156addik r3, r3, -1157/* sync */158159/* Setup the kernel PID */160mts rpid,r0 /* Load the kernel PID */161nop162bri 4163164/*165* We should still be executing code at physical address area166* RAM_BASEADDR at this point. However, kernel code is at167* a virtual address. So, set up a TLB mapping to cover this once168* translation is enabled.169*/170171addik r3,r0, CONFIG_KERNEL_START /* Load the kernel virtual address */172tophys(r4,r3) /* Load the kernel physical address */173174/* start to do TLB calculation */175addik r12, r0, _end176rsub r12, r3, r12177addik r12, r12, CONFIG_LOWMEM_SIZE >> PTE_SHIFT /* that's the pad */178179or r9, r0, r0 /* TLB0 = 0 */180or r10, r0, r0 /* TLB1 = 0 */181182addik r11, r12, -0x1000000183bgei r11, GT16 /* size is greater than 16MB */184addik r11, r12, -0x0800000185bgei r11, GT8 /* size is greater than 8MB */186addik r11, r12, -0x0400000187bgei r11, GT4 /* size is greater than 4MB */188/* size is less than 4MB */189addik r11, r12, -0x0200000190bgei r11, GT2 /* size is greater than 2MB */191addik r9, r0, 0x0100000 /* TLB0 must be 1MB */192addik r11, r12, -0x0100000193bgei r11, GT1 /* size is greater than 1MB */194/* TLB1 is 0 which is setup above */195bri tlb_end196GT4: /* r11 contains the rest - will be either 1 or 4 */197ori r9, r0, 0x400000 /* TLB0 is 4MB */198bri TLB1199GT16: /* TLB0 is 16MB */200addik r9, r0, 0x1000000 /* means TLB0 is 16MB */201TLB1:202/* must be used r2 because of subtract if failed */203addik r2, r11, -0x0400000204bgei r2, GT20 /* size is greater than 16MB */205/* size is >16MB and <20MB */206addik r11, r11, -0x0100000207bgei r11, GT17 /* size is greater than 17MB */208/* kernel is >16MB and < 17MB */209GT1:210addik r10, r0, 0x0100000 /* means TLB1 is 1MB */211bri tlb_end212GT2: /* TLB0 is 0 and TLB1 will be 4MB */213GT17: /* TLB1 is 4MB - kernel size <20MB */214addik r10, r0, 0x0400000 /* means TLB1 is 4MB */215bri tlb_end216GT8: /* TLB0 is still zero that's why I can use only TLB1 */217GT20: /* TLB1 is 16MB - kernel size >20MB */218addik r10, r0, 0x1000000 /* means TLB1 is 16MB */219tlb_end:220221/*222* Configure and load two entries into TLB slots 0 and 1.223* In case we are pinning TLBs, these are reserved in by the224* other TLB functions. If not reserving, then it doesn't225* matter where they are loaded.226*/227andi r4,r4,0xfffffc00 /* Mask off the real page number */228ori r4,r4,(TLB_WR | TLB_EX) /* Set the write and execute bits */229230/*231* TLB0 is always used - check if is not zero (r9 stores TLB0 value)232* if is use TLB1 value and clear it (r10 stores TLB1 value)233*/234bnei r9, tlb0_not_zero235add r9, r10, r0236add r10, r0, r0237tlb0_not_zero:238239/* look at the code below */240ori r30, r0, 0x200241andi r29, r9, 0x100000242bneid r29, 1f243addik r30, r30, 0x80244andi r29, r9, 0x400000245bneid r29, 1f246addik r30, r30, 0x80247andi r29, r9, 0x1000000248bneid r29, 1f249addik r30, r30, 0x802501:251andi r3,r3,0xfffffc00 /* Mask off the effective page number */252ori r3,r3,(TLB_VALID)253or r3, r3, r30254255/* Load tlb_skip size value which is index to first unused TLB entry */256lwi r11, r0, TOPHYS(tlb_skip)257mts rtlbx,r11 /* TLB slow 0 */258259mts rtlblo,r4 /* Load the data portion of the entry */260mts rtlbhi,r3 /* Load the tag portion of the entry */261262/* Increase tlb_skip size */263addik r11, r11, 1264swi r11, r0, TOPHYS(tlb_skip)265266/* TLB1 can be zeroes that's why we not setup it */267beqi r10, jump_over2268269/* look at the code below */270ori r30, r0, 0x200271andi r29, r10, 0x100000272bneid r29, 1f273addik r30, r30, 0x80274andi r29, r10, 0x400000275bneid r29, 1f276addik r30, r30, 0x80277andi r29, r10, 0x1000000278bneid r29, 1f279addik r30, r30, 0x802801:281addk r4, r4, r9 /* previous addr + TLB0 size */282addk r3, r3, r9283284andi r3,r3,0xfffffc00 /* Mask off the effective page number */285ori r3,r3,(TLB_VALID)286or r3, r3, r30287288lwi r11, r0, TOPHYS(tlb_skip)289mts rtlbx, r11 /* r11 is used from TLB0 */290291mts rtlblo,r4 /* Load the data portion of the entry */292mts rtlbhi,r3 /* Load the tag portion of the entry */293294/* Increase tlb_skip size */295addik r11, r11, 1296swi r11, r0, TOPHYS(tlb_skip)297298jump_over2:299/*300* Load a TLB entry for LMB, since we need access to301* the exception vectors, using a 4k real==virtual mapping.302*/303/* Use temporary TLB_ID for LMB - clear this temporary mapping later */304ori r11, r0, MICROBLAZE_LMB_TLB_ID305mts rtlbx,r11306307ori r4,r0,(TLB_WR | TLB_EX)308ori r3,r0,(TLB_VALID | TLB_PAGESZ(PAGESZ_4K))309310mts rtlblo,r4 /* Load the data portion of the entry */311mts rtlbhi,r3 /* Load the tag portion of the entry */312313/*314* We now have the lower 16 Meg of RAM mapped into TLB entries, and the315* caches ready to work.316*/317turn_on_mmu:318ori r15,r0,start_here319ori r4,r0,MSR_KERNEL_VMS320mts rmsr,r4321nop322rted r15,0 /* enables MMU */323nop324325start_here:326327/* Initialize small data anchors */328addik r13, r0, _KERNEL_SDA_BASE_329addik r2, r0, _KERNEL_SDA2_BASE_330331/* Initialize stack pointer */332addik r1, r0, init_thread_union + THREAD_SIZE - 4333334/* Initialize r31 with current task address */335addik r31, r0, init_task336337addik r11, r0, machine_early_init338brald r15, r11339nop340341/*342* Initialize the MMU.343*/344bralid r15, mmu_init345nop346347/* Go back to running unmapped so we can load up new values348* and change to using our exception vectors.349* On the MicroBlaze, all we invalidate the used TLB entries to clear350* the old 16M byte TLB mappings.351*/352ori r15,r0,TOPHYS(kernel_load_context)353ori r4,r0,MSR_KERNEL354mts rmsr,r4355nop356bri 4357rted r15,0358nop359360/* Load up the kernel context */361kernel_load_context:362ori r5, r0, MICROBLAZE_LMB_TLB_ID363mts rtlbx,r5364nop365mts rtlbhi,r0366nop367addi r15, r0, machine_halt368ori r17, r0, start_kernel369ori r4, r0, MSR_KERNEL_VMS370mts rmsr, r4371nop372rted r17, 0 /* enable MMU and jump to start_kernel */373nop374375376