/* $Id: head.S,v 1.7 2003/09/01 17:58:19 lethal Exp $1*2* arch/sh/kernel/head.S3*4* Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima5* Copyright (C) 2010 Matt Fleming6*7* This file is subject to the terms and conditions of the GNU General Public8* License. See the file "COPYING" in the main directory of this archive9* for more details.10*11* Head.S contains the SH exception handlers and startup code.12*/13#include <linux/init.h>14#include <linux/linkage.h>15#include <asm/thread_info.h>16#include <asm/mmu.h>17#include <cpu/mmu_context.h>1819#ifdef CONFIG_CPU_SH4A20#define SYNCO() synco2122#define PREFI(label, reg) \23mov.l label, reg; \24prefi @reg25#else26#define SYNCO()27#define PREFI(label, reg)28#endif2930.section .empty_zero_page, "aw"31ENTRY(empty_zero_page)32.long 1 /* MOUNT_ROOT_RDONLY */33.long 0 /* RAMDISK_FLAGS */34.long 0x0200 /* ORIG_ROOT_DEV */35.long 1 /* LOADER_TYPE */36.long 0x00000000 /* INITRD_START */37.long 0x00000000 /* INITRD_SIZE */38#ifdef CONFIG_32BIT39.long 0x53453f00 + 32 /* "SE?" = 32 bit */40#else41.long 0x53453f00 + 29 /* "SE?" = 29 bit */42#endif431:44.skip PAGE_SIZE - empty_zero_page - 1b4546__HEAD4748/*49* Condition at the entry of _stext:50*51* BSC has already been initialized.52* INTC may or may not be initialized.53* VBR may or may not be initialized.54* MMU may or may not be initialized.55* Cache may or may not be initialized.56* Hardware (including on-chip modules) may or may not be initialized.57*58*/59ENTRY(_stext)60! Initialize Status Register61mov.l 1f, r0 ! MD=1, RB=0, BL=0, IMASK=0xF62ldc r0, sr63! Initialize global interrupt mask64#ifdef CONFIG_CPU_HAS_SR_RB65mov #0, r066ldc r0, r6_bank67#endif6869/*70* Prefetch if possible to reduce cache miss penalty.71*72* We do this early on for SH-4A as a micro-optimization,73* as later on we will have speculative execution enabled74* and this will become less of an issue.75*/76PREFI(5f, r0)77PREFI(6f, r0)7879!80mov.l 2f, r081mov r0, r15 ! Set initial r15 (stack pointer)82#ifdef CONFIG_CPU_HAS_SR_RB83mov.l 7f, r084ldc r0, r7_bank ! ... and initial thread_info85#endif8687#ifdef CONFIG_PMB88/*89* Reconfigure the initial PMB mappings setup by the hardware.90*91* When we boot in 32-bit MMU mode there are 2 PMB entries already92* setup for us.93*94* Entry VPN PPN V SZ C UB WT95* ---------------------------------------------------------------96* 0 0x80000000 0x00000000 1 512MB 1 0 197* 1 0xA0000000 0x00000000 1 512MB 0 0 098*99* But we reprogram them here because we want complete control over100* our address space and the initial mappings may not map PAGE_OFFSET101* to __MEMORY_START (or even map all of our RAM).102*103* Once we've setup cached and uncached mappings we clear the rest of the104* PMB entries. This clearing also deals with the fact that PMB entries105* can persist across reboots. The PMB could have been left in any state106* when the reboot occurred, so to be safe we clear all entries and start107* with with a clean slate.108*109* The uncached mapping is constructed using the smallest possible110* mapping with a single unbufferable page. Only the kernel text needs to111* be covered via the uncached mapping so that certain functions can be112* run uncached.113*114* Drivers and the like that have previously abused the 1:1 identity115* mapping are unsupported in 32-bit mode and must specify their caching116* preference when page tables are constructed.117*118* This frees up the P2 space for more nefarious purposes.119*120* Register utilization is as follows:121*122* r0 = PMB_DATA data field123* r1 = PMB_DATA address field124* r2 = PMB_ADDR data field125* r3 = PMB_ADDR address field126* r4 = PMB_E_SHIFT127* r5 = remaining amount of RAM to map128* r6 = PMB mapping size we're trying to use129* r7 = cached_to_uncached130* r8 = scratch register131* r9 = scratch register132* r10 = number of PMB entries we've setup133* r11 = scratch register134*/135136mov.l .LMMUCR, r1 /* Flush the TLB */137mov.l @r1, r0138or #MMUCR_TI, r0139mov.l r0, @r1140141mov.l .LMEMORY_SIZE, r5142143mov #PMB_E_SHIFT, r0144mov #0x1, r4145shld r0, r4146147mov.l .LFIRST_DATA_ENTRY, r0148mov.l .LPMB_DATA, r1149mov.l .LFIRST_ADDR_ENTRY, r2150mov.l .LPMB_ADDR, r3151152/*153* First we need to walk the PMB and figure out if there are any154* existing mappings that match the initial mappings VPN/PPN.155* If these have already been established by the bootloader, we156* don't bother setting up new entries here, and let the late PMB157* initialization take care of things instead.158*159* Note that we may need to coalesce and merge entries in order160* to reclaim more available PMB slots, which is much more than161* we want to do at this early stage.162*/163mov #0, r10164mov #NR_PMB_ENTRIES, r9165166mov r1, r7 /* temporary PMB_DATA iter */167168.Lvalidate_existing_mappings:169170mov.l .LPMB_DATA_MASK, r11171mov.l @r7, r8172and r11, r8173cmp/eq r0, r8 /* Check for valid __MEMORY_START mappings */174bt .Lpmb_done175176add #1, r10 /* Increment the loop counter */177cmp/eq r9, r10178bf/s .Lvalidate_existing_mappings179add r4, r7 /* Increment to the next PMB_DATA entry */180181/*182* If we've fallen through, continue with setting up the initial183* mappings.184*/185186mov r5, r7 /* cached_to_uncached */187mov #0, r10188189#ifdef CONFIG_UNCACHED_MAPPING190/*191* Uncached mapping192*/193mov #(PMB_SZ_16M >> 2), r9194shll2 r9195196mov #(PMB_UB >> 8), r8197shll8 r8198199or r0, r8200or r9, r8201mov.l r8, @r1202mov r2, r8203add r7, r8204mov.l r8, @r3205206add r4, r1207add r4, r3208add #1, r10209#endif210211/*212* Iterate over all of the available sizes from largest to213* smallest for constructing the cached mapping.214*/215#define __PMB_ITER_BY_SIZE(size) \216.L##size: \217mov #(size >> 4), r6; \218shll16 r6; \219shll8 r6; \220\221cmp/hi r5, r6; \222bt 9999f; \223\224mov #(PMB_SZ_##size##M >> 2), r9; \225shll2 r9; \226\227/* \228* Cached mapping \229*/ \230mov #PMB_C, r8; \231or r0, r8; \232or r9, r8; \233mov.l r8, @r1; \234mov.l r2, @r3; \235\236/* Increment to the next PMB_DATA entry */ \237add r4, r1; \238/* Increment to the next PMB_ADDR entry */ \239add r4, r3; \240/* Increment number of PMB entries */ \241add #1, r10; \242\243sub r6, r5; \244add r6, r0; \245add r6, r2; \246\247bra .L##size; \2489999:249250__PMB_ITER_BY_SIZE(512)251__PMB_ITER_BY_SIZE(128)252__PMB_ITER_BY_SIZE(64)253__PMB_ITER_BY_SIZE(16)254255#ifdef CONFIG_UNCACHED_MAPPING256/*257* Now that we can access it, update cached_to_uncached and258* uncached_size.259*/260mov.l .Lcached_to_uncached, r0261mov.l r7, @r0262263mov.l .Luncached_size, r0264mov #1, r7265shll16 r7266shll8 r7267mov.l r7, @r0268#endif269270/*271* Clear the remaining PMB entries.272*273* r3 = entry to begin clearing from274* r10 = number of entries we've setup so far275*/276mov #0, r1277mov #NR_PMB_ENTRIES, r0278279.Lagain:280mov.l r1, @r3 /* Clear PMB_ADDR entry */281add #1, r10 /* Increment the loop counter */282cmp/eq r0, r10283bf/s .Lagain284add r4, r3 /* Increment to the next PMB_ADDR entry */285286mov.l 6f, r0287icbi @r0288289.Lpmb_done:290#endif /* CONFIG_PMB */291292#ifndef CONFIG_SH_NO_BSS_INIT293/*294* Don't clear BSS if running on slow platforms such as an RTL simulation,295* remote memory via SHdebug link, etc. For these the memory can be guaranteed296* to be all zero on boot anyway.297*/298! Clear BSS area299#ifdef CONFIG_SMP300mov.l 3f, r0301cmp/eq #0, r0 ! skip clear if set to zero302bt 10f303#endif304305mov.l 3f, r1306add #4, r1307mov.l 4f, r2308mov #0, r03099: cmp/hs r2, r1310bf/s 9b ! while (r1 < r2)311mov.l r0,@-r231231310:314#endif315316! Additional CPU initialization317mov.l 6f, r0318jsr @r0319nop320321SYNCO() ! Wait for pending instructions..322323! Start kernel324mov.l 5f, r0325jmp @r0326nop327328.balign 4329#if defined(CONFIG_CPU_SH2)3301: .long 0x000000F0 ! IMASK=0xF331#else3321: .long 0x500080F0 ! MD=1, RB=0, BL=1, FD=1, IMASK=0xF333#endif334ENTRY(stack_start)3352: .long init_thread_union+THREAD_SIZE3363: .long __bss_start3374: .long _end3385: .long start_kernel3396: .long cpu_init3407: .long init_thread_union341342#ifdef CONFIG_PMB343.LPMB_ADDR: .long PMB_ADDR344.LPMB_DATA: .long PMB_DATA345.LPMB_DATA_MASK: .long PMB_PFN_MASK | PMB_V346.LFIRST_ADDR_ENTRY: .long PAGE_OFFSET | PMB_V347.LFIRST_DATA_ENTRY: .long __MEMORY_START | PMB_V348.LMMUCR: .long MMUCR349.LMEMORY_SIZE: .long __MEMORY_SIZE350#ifdef CONFIG_UNCACHED_MAPPING351.Lcached_to_uncached: .long cached_to_uncached352.Luncached_size: .long uncached_size353#endif354#endif355356357