Merge tag 'for-linus-4.15-rc5-tag' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.git] / arch / sh / kernel / head_64.S
1 /*
2  * arch/sh/kernel/head_64.S
3  *
4  * Copyright (C) 2000, 2001  Paolo Alberelli
5  * Copyright (C) 2003, 2004  Paul Mundt
6  *
7  * This file is subject to the terms and conditions of the GNU General Public
8  * License.  See the file "COPYING" in the main directory of this archive
9  * for more details.
10  */
11
12 #include <linux/init.h>
13
14 #include <asm/page.h>
15 #include <asm/cache.h>
16 #include <asm/tlb.h>
17 #include <cpu/registers.h>
18 #include <cpu/mmu_context.h>
19 #include <asm/thread_info.h>
20
21 /*
22  * MMU defines: TLB boundaries.
23  */
24
25 #define MMUIR_FIRST     ITLB_FIXED
26 #define MMUIR_END       ITLB_LAST_VAR_UNRESTRICTED+TLB_STEP
27 #define MMUIR_STEP      TLB_STEP
28
29 #define MMUDR_FIRST     DTLB_FIXED
30 #define MMUDR_END       DTLB_LAST_VAR_UNRESTRICTED+TLB_STEP
31 #define MMUDR_STEP      TLB_STEP
32
33 /* Safety check : CONFIG_PAGE_OFFSET has to be a multiple of 512Mb */
34 #if (CONFIG_PAGE_OFFSET & ((1UL<<29)-1))
35 #error "CONFIG_PAGE_OFFSET must be a multiple of 512Mb"
36 #endif
37
38 /*
39  * MMU defines: Fixed TLBs.
40  */
41 /* Deal safely with the case where the base of RAM is not 512Mb aligned */
42
43 #define ALIGN_512M_MASK (0xffffffffe0000000)
44 #define ALIGNED_EFFECTIVE ((CONFIG_PAGE_OFFSET + CONFIG_MEMORY_START) & ALIGN_512M_MASK)
45 #define ALIGNED_PHYSICAL (CONFIG_MEMORY_START & ALIGN_512M_MASK)
46
47 #define MMUIR_TEXT_H    (0x0000000000000003 | ALIGNED_EFFECTIVE)
48                         /* Enabled, Shared, ASID 0, Eff. Add. 0xA0000000 */
49
50 #define MMUIR_TEXT_L    (0x000000000000009a | ALIGNED_PHYSICAL)
51                         /* 512 Mb, Cacheable, Write-back, execute, Not User, Ph. Add. */
52
53 #define MMUDR_CACHED_H  0x0000000000000003 | ALIGNED_EFFECTIVE
54                         /* Enabled, Shared, ASID 0, Eff. Add. 0xA0000000 */
55 #define MMUDR_CACHED_L  0x000000000000015a | ALIGNED_PHYSICAL
56                         /* 512 Mb, Cacheable, Write-back, read/write, Not User, Ph. Add. */
57
58 #ifdef CONFIG_CACHE_OFF
59 #define ICCR0_INIT_VAL  ICCR0_OFF                       /* ICACHE off */
60 #else
61 #define ICCR0_INIT_VAL  ICCR0_ON | ICCR0_ICI            /* ICE + ICI */
62 #endif
63 #define ICCR1_INIT_VAL  ICCR1_NOLOCK                    /* No locking */
64
65 #if defined (CONFIG_CACHE_OFF)
66 #define OCCR0_INIT_VAL  OCCR0_OFF                          /* D-cache: off  */
67 #elif defined (CONFIG_CACHE_WRITETHROUGH)
68 #define OCCR0_INIT_VAL  OCCR0_ON | OCCR0_OCI | OCCR0_WT    /* D-cache: on,   */
69                                                            /* WT, invalidate */
70 #elif defined (CONFIG_CACHE_WRITEBACK)
71 #define OCCR0_INIT_VAL  OCCR0_ON | OCCR0_OCI | OCCR0_WB    /* D-cache: on,   */
72                                                            /* WB, invalidate */
73 #else
74 #error preprocessor flag CONFIG_CACHE_... not recognized!
75 #endif
76
77 #define OCCR1_INIT_VAL  OCCR1_NOLOCK                       /* No locking     */
78
79         .section        .empty_zero_page, "aw"
80         .global empty_zero_page
81
82 empty_zero_page:
83         .long   1               /* MOUNT_ROOT_RDONLY */
84         .long   0               /* RAMDISK_FLAGS */
85         .long   0x0200          /* ORIG_ROOT_DEV */
86         .long   1               /* LOADER_TYPE */
87         .long   0x00800000      /* INITRD_START */
88         .long   0x00800000      /* INITRD_SIZE */
89         .long   0
90
91         .text
92         .balign 4096,0,4096
93
94         .section        .data, "aw"
95         .balign PAGE_SIZE
96
97         .section        .data, "aw"
98         .balign PAGE_SIZE
99
100         .global mmu_pdtp_cache
101 mmu_pdtp_cache:
102         .space PAGE_SIZE, 0
103
104         .global fpu_in_use
105 fpu_in_use:     .quad   0
106
107
108         __HEAD
109         .balign L1_CACHE_BYTES
110 /*
111  * Condition at the entry of __stext:
112  * . Reset state:
113  *   . SR.FD    = 1             (FPU disabled)
114  *   . SR.BL    = 1             (Exceptions disabled)
115  *   . SR.MD    = 1             (Privileged Mode)
116  *   . SR.MMU   = 0             (MMU Disabled)
117  *   . SR.CD    = 0             (CTC User Visible)
118  *   . SR.IMASK = Undefined     (Interrupt Mask)
119  *
120  * Operations supposed to be performed by __stext:
121  * . prevent speculative fetch onto device memory while MMU is off
122  * . reflect as much as possible SH5 ABI (r15, r26, r27, r18)
123  * . first, save CPU state and set it to something harmless
124  * . any CPU detection and/or endianness settings (?)
125  * . initialize EMI/LMI (but not TMU/RTC/INTC/SCIF): TBD
126  * . set initial TLB entries for cached and uncached regions
127  *   (no fine granularity paging)
128  * . set initial cache state
129  * . enable MMU and caches
130  * . set CPU to a consistent state
131  *   . registers (including stack pointer and current/KCR0)
132  *   . NOT expecting to set Exception handling nor VBR/RESVEC/DCR
133  *     at this stage. This is all to later Linux initialization steps.
134  *   . initialize FPU
135  * . clear BSS
136  * . jump into start_kernel()
137  * . be prepared to hopeless start_kernel() returns.
138  *
139  */
140         .global _stext
141 _stext:
142         /*
143          * Prevent speculative fetch on device memory due to
144          * uninitialized target registers.
145          */
146         ptabs/u ZERO, tr0
147         ptabs/u ZERO, tr1
148         ptabs/u ZERO, tr2
149         ptabs/u ZERO, tr3
150         ptabs/u ZERO, tr4
151         ptabs/u ZERO, tr5
152         ptabs/u ZERO, tr6
153         ptabs/u ZERO, tr7
154         synci
155
156         /*
157          * Read/Set CPU state. After this block:
158          * r29 = Initial SR
159          */
160         getcon  SR, r29
161         movi    SR_HARMLESS, r20
162         putcon  r20, SR
163
164         /*
165          * Initialize EMI/LMI. To Be Done.
166          */
167
168         /*
169          * CPU detection and/or endianness settings (?). To Be Done.
170          * Pure PIC code here, please ! Just save state into r30.
171          * After this block:
172          * r30 = CPU type/Platform Endianness
173          */
174
175         /*
176          * Set initial TLB entries for cached and uncached regions.
177          * Note: PTA/BLINK is PIC code, PTABS/BLINK isn't !
178          */
179         /* Clear ITLBs */
180         pta     clear_ITLB, tr1
181         movi    MMUIR_FIRST, r21
182         movi    MMUIR_END, r22
183 clear_ITLB:
184         putcfg  r21, 0, ZERO            /* Clear MMUIR[n].PTEH.V */
185         addi    r21, MMUIR_STEP, r21
186         bne     r21, r22, tr1
187
188         /* Clear DTLBs */
189         pta     clear_DTLB, tr1
190         movi    MMUDR_FIRST, r21
191         movi    MMUDR_END, r22
192 clear_DTLB:
193         putcfg  r21, 0, ZERO            /* Clear MMUDR[n].PTEH.V */
194         addi    r21, MMUDR_STEP, r21
195         bne     r21, r22, tr1
196
197         /* Map one big (512Mb) page for ITLB */
198         movi    MMUIR_FIRST, r21
199         movi    MMUIR_TEXT_L, r22       /* PTEL first */
200         add.l   r22, r63, r22           /* Sign extend */
201         putcfg  r21, 1, r22             /* Set MMUIR[0].PTEL */
202         movi    MMUIR_TEXT_H, r22       /* PTEH last */
203         add.l   r22, r63, r22           /* Sign extend */
204         putcfg  r21, 0, r22             /* Set MMUIR[0].PTEH */
205
206         /* Map one big CACHED (512Mb) page for DTLB */
207         movi    MMUDR_FIRST, r21
208         movi    MMUDR_CACHED_L, r22     /* PTEL first */
209         add.l   r22, r63, r22           /* Sign extend */
210         putcfg  r21, 1, r22             /* Set MMUDR[0].PTEL */
211         movi    MMUDR_CACHED_H, r22     /* PTEH last */
212         add.l   r22, r63, r22           /* Sign extend */
213         putcfg  r21, 0, r22             /* Set MMUDR[0].PTEH */
214
215         /*
216          * Setup a DTLB translation for SCIF phys.
217          */
218         addi    r21, MMUDR_STEP, r21
219         movi    0x0a03, r22     /* SCIF phys */
220         shori   0x0148, r22
221         putcfg  r21, 1, r22     /* PTEL first */
222         movi    0xfa03, r22     /* 0xfa030000, fixed SCIF virt */
223         shori   0x0003, r22
224         putcfg  r21, 0, r22     /* PTEH last */
225
226         /*
227          * Set cache behaviours.
228          */
229         /* ICache */
230         movi    ICCR_BASE, r21
231         movi    ICCR0_INIT_VAL, r22
232         movi    ICCR1_INIT_VAL, r23
233         putcfg  r21, ICCR_REG0, r22
234         putcfg  r21, ICCR_REG1, r23
235
236         /* OCache */
237         movi    OCCR_BASE, r21
238         movi    OCCR0_INIT_VAL, r22
239         movi    OCCR1_INIT_VAL, r23
240         putcfg  r21, OCCR_REG0, r22
241         putcfg  r21, OCCR_REG1, r23
242
243
244         /*
245          * Enable Caches and MMU. Do the first non-PIC jump.
246          * Now head.S global variables, constants and externs
247          * can be used.
248          */
249         getcon  SR, r21
250         movi    SR_ENABLE_MMU, r22
251         or      r21, r22, r21
252         putcon  r21, SSR
253         movi    hyperspace, r22
254         ori     r22, 1, r22         /* Make it SHmedia, not required but..*/
255         putcon  r22, SPC
256         synco
257         rte                         /* And now go into the hyperspace ... */
258 hyperspace:                         /* ... that's the next instruction !  */
259
260         /*
261          * Set CPU to a consistent state.
262          * r31 = FPU support flag
263          * tr0/tr7 in use. Others give a chance to loop somewhere safe
264          */
265         movi    start_kernel, r32
266         ori     r32, 1, r32
267
268         ptabs   r32, tr0                    /* r32 = _start_kernel address        */
269         pta/u   hopeless, tr1
270         pta/u   hopeless, tr2
271         pta/u   hopeless, tr3
272         pta/u   hopeless, tr4
273         pta/u   hopeless, tr5
274         pta/u   hopeless, tr6
275         pta/u   hopeless, tr7
276         gettr   tr1, r28                        /* r28 = hopeless address */
277
278         /* Set initial stack pointer */
279         movi    init_thread_union, SP
280         putcon  SP, KCR0                /* Set current to init_task */
281         movi    THREAD_SIZE, r22        /* Point to the end */
282         add     SP, r22, SP
283
284         /*
285          * Initialize FPU.
286          * Keep FPU flag in r31. After this block:
287          * r31 = FPU flag
288          */
289         movi fpu_in_use, r31    /* Temporary */
290
291 #ifdef CONFIG_SH_FPU
292         getcon  SR, r21
293         movi    SR_ENABLE_FPU, r22
294         and     r21, r22, r22
295         putcon  r22, SR                 /* Try to enable */
296         getcon  SR, r22
297         xor     r21, r22, r21
298         shlri   r21, 15, r21            /* Supposedly 0/1 */
299         st.q    r31, 0 , r21            /* Set fpu_in_use */
300 #else
301         movi    0, r21
302         st.q    r31, 0 , r21            /* Set fpu_in_use */
303 #endif
304         or      r21, ZERO, r31          /* Set FPU flag at last */
305
306 #ifndef CONFIG_SH_NO_BSS_INIT
307 /* Don't clear BSS if running on slow platforms such as an RTL simulation,
308    remote memory via SHdebug link, etc.  For these the memory can be guaranteed
309    to be all zero on boot anyway. */
310         /*
311          * Clear bss
312          */
313         pta     clear_quad, tr1
314         movi    __bss_start, r22
315         movi    _end, r23
316 clear_quad:
317         st.q    r22, 0, ZERO
318         addi    r22, 8, r22
319         bne     r22, r23, tr1           /* Both quad aligned, see vmlinux.lds.S */
320 #endif
321         pta/u   hopeless, tr1
322
323         /* Say bye to head.S but be prepared to wrongly get back ... */
324         blink   tr0, LINK
325
326         /* If we ever get back here through LINK/tr1-tr7 */
327         pta/u   hopeless, tr7
328
329 hopeless:
330         /*
331          * Something's badly wrong here. Loop endlessly,
332          * there's nothing more we can do about it.
333          *
334          * Note on hopeless: it can be jumped into invariably
335          * before or after jumping into hyperspace. The only
336          * requirement is to be PIC called (PTA) before and
337          * any way (PTA/PTABS) after. According to Virtual
338          * to Physical mapping a simulator/emulator can easily
339          * tell where we came here from just looking at hopeless
340          * (PC) address.
341          *
342          * For debugging purposes:
343          * (r28) hopeless/loop address
344          * (r29) Original SR
345          * (r30) CPU type/Platform endianness
346          * (r31) FPU Support
347          * (r32) _start_kernel address
348          */
349         blink   tr7, ZERO