blob: ceb548678946ffdfe698c64fc6ec106e27c298f2 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Eran Liberty9095d4a2005-07-28 10:08:46 -05002/*
3 * Copyright (C) 1998 Dan Malek <dmalek@jlc.net>
4 * Copyright (C) 1999 Magnus Damm <kieraypc01.p.y.kie.era.ericsson.se>
5 * Copyright (C) 2000, 2001,2002 Wolfgang Denk <wd@denx.de>
Scott Woodb71689b2008-06-30 14:13:28 -05006 * Copyright Freescale Semiconductor, Inc. 2004, 2006, 2008.
Eran Liberty9095d4a2005-07-28 10:08:46 -05007 */
8
9/*
10 * U-Boot - Startup Code for MPC83xx PowerPC based Embedded Boards
11 */
12
Wolfgang Denk0191e472010-10-26 14:34:52 +020013#include <asm-offsets.h>
Eran Liberty9095d4a2005-07-28 10:08:46 -050014#include <config.h>
Jon Loeligerebc72242005-08-01 13:20:47 -050015#include <mpc83xx.h>
Tom Rini4ddbade2022-05-25 12:16:03 -040016#include <system-constants.h>
Eran Liberty9095d4a2005-07-28 10:08:46 -050017
Eran Liberty9095d4a2005-07-28 10:08:46 -050018#include <ppc_asm.tmpl>
19#include <ppc_defs.h>
20
21#include <asm/cache.h>
22#include <asm/mmu.h>
Tom Rini7a213552023-11-01 12:28:11 -040023#include <asm/fsl_lbc.h>
Peter Tyser3a1362d2010-10-14 23:33:24 -050024#include <asm/u-boot.h>
Eran Liberty9095d4a2005-07-28 10:08:46 -050025
Mario Six94867102019-01-21 09:17:54 +010026#include "hrcw/hrcw.h"
Mario Sixa861ea62019-01-21 09:17:57 +010027#include "bats/bats.h"
Mario Six8b2141c2019-01-21 09:18:09 +010028#include "hid/hid.h"
Mario Six94867102019-01-21 09:17:54 +010029
Eran Liberty9095d4a2005-07-28 10:08:46 -050030/* We don't want the MMU yet.
31 */
32#undef MSR_KERNEL
33
34/*
35 * Floating Point enable, Machine Check and Recoverable Interr.
36 */
37#ifdef DEBUG
38#define MSR_KERNEL (MSR_FP|MSR_RI)
39#else
40#define MSR_KERNEL (MSR_FP|MSR_ME|MSR_RI)
41#endif
42
Scott Wood2b36fbb2012-12-06 13:33:17 +000043#if defined(CONFIG_NAND_SPL) || \
Tom Rini6b15c162022-05-13 12:26:35 -040044 (defined(CONFIG_SPL_BUILD) && CONFIG_IS_ENABLED(INIT_MINIMAL))
Scott Wood2b36fbb2012-12-06 13:33:17 +000045#define MINIMAL_SPL
46#endif
47
48#if !defined(CONFIG_SPL_BUILD) && !defined(CONFIG_NAND_SPL) && \
49 !defined(CONFIG_SYS_RAMBOOT)
Tom Rini364d0022023-01-10 11:19:45 -050050#define CFG_SYS_FLASHBOOT
Scott Woodb71689b2008-06-30 14:13:28 -050051#endif
52
Eran Liberty9095d4a2005-07-28 10:08:46 -050053/*
54 * Set up GOT: Global Offset Table
55 *
Joakim Tjernlund3fbaa4d2010-01-19 14:41:56 +010056 * Use r12 to access the GOT
Eran Liberty9095d4a2005-07-28 10:08:46 -050057 */
58 START_GOT
59 GOT_ENTRY(_GOT2_TABLE_)
Scott Woodb71689b2008-06-30 14:13:28 -050060 GOT_ENTRY(__bss_start)
Simon Glassed70c8f2013-03-14 06:54:53 +000061 GOT_ENTRY(__bss_end)
Eran Liberty9095d4a2005-07-28 10:08:46 -050062
Scott Wood2b36fbb2012-12-06 13:33:17 +000063#ifndef MINIMAL_SPL
Scott Woodb71689b2008-06-30 14:13:28 -050064 GOT_ENTRY(_FIXUP_TABLE_)
Eran Liberty9095d4a2005-07-28 10:08:46 -050065 GOT_ENTRY(_start)
66 GOT_ENTRY(_start_of_vectors)
67 GOT_ENTRY(_end_of_vectors)
68 GOT_ENTRY(transfer_to_handler)
Scott Woodb71689b2008-06-30 14:13:28 -050069#endif
Eran Liberty9095d4a2005-07-28 10:08:46 -050070 END_GOT
71
72/*
Jerry Van Baren93eb9312006-12-06 21:23:55 -050073 * The Hard Reset Configuration Word (HRCW) table is in the first 64
74 * (0x40) bytes of flash. It has 8 bytes, but each byte is repeated 8
75 * times so the processor can fetch it out of flash whether the flash
76 * is 8, 16, 32, or 64 bits wide (hardware trickery).
Eran Liberty9095d4a2005-07-28 10:08:46 -050077 */
Eran Liberty9095d4a2005-07-28 10:08:46 -050078 .text
79#define _HRCW_TABLE_ENTRY(w) \
80 .fill 8,1,(((w)>>24)&0xff); \
81 .fill 8,1,(((w)>>16)&0xff); \
82 .fill 8,1,(((w)>> 8)&0xff); \
83 .fill 8,1,(((w) )&0xff)
84
Tom Rini364d0022023-01-10 11:19:45 -050085 _HRCW_TABLE_ENTRY(CFG_SYS_HRCW_LOW)
86 _HRCW_TABLE_ENTRY(CFG_SYS_HRCW_HIGH)
Eran Liberty9095d4a2005-07-28 10:08:46 -050087
Jerry Van Baren93eb9312006-12-06 21:23:55 -050088/*
89 * Magic number and version string - put it after the HRCW since it
90 * cannot be first in flash like it is in many other processors.
91 */
92 .long 0x27051956 /* U-Boot Magic Number */
93
Ron Madrid787b61d2008-12-12 13:12:45 -080094 .globl enable_addr_trans
95enable_addr_trans:
96 /* enable address translation */
97 mfmsr r5
98 ori r5, r5, (MSR_IR | MSR_DR)
99 mtmsr r5
100 isync
101 blr
102
103 .globl disable_addr_trans
104disable_addr_trans:
105 /* disable address translation */
106 mflr r4
107 mfmsr r3
108 andi. r0, r3, (MSR_IR | MSR_DR)
109 beqlr
110 andc r3, r3, r0
111 mtspr SRR0, r4
112 mtspr SRR1, r3
113 rfi
114
Eran Liberty9095d4a2005-07-28 10:08:46 -0500115#ifndef CONFIG_DEFAULT_IMMR
116#error CONFIG_DEFAULT_IMMR must be defined
Heiko Schocher71cb3e92017-06-07 17:33:10 +0200117#endif /* CONFIG_DEFAULT_IMMR */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500118
119/*
120 * After configuration, a system reset exception is executed using the
121 * vector at offset 0x100 relative to the base set by MSR[IP]. If
122 * MSR[IP] is 0, the base address is 0x00000000. If MSR[IP] is 1, the
123 * base address is 0xfff00000. In the case of a Power On Reset or Hard
124 * Reset, the value of MSR[IP] is determined by the CIP field in the
125 * HRCW.
126 *
127 * Other bits in the HRCW set up the Base Address and Port Size in BR0.
128 * This determines the location of the boot ROM (flash or EPROM) in the
129 * processor's address space at boot time. As long as the HRCW is set up
130 * so that we eventually end up executing the code below when the
131 * processor executes the reset exception, the actual values used should
132 * not matter.
133 *
134 * Once we have got here, the address mask in OR0 is cleared so that the
135 * bottom 32K of the boot ROM is effectively repeated all throughout the
136 * processor's address space, after which we can jump to the absolute
137 * address at which the boot ROM was linked at compile time, and proceed
138 * to initialise the memory controller without worrying if the rug will
139 * be pulled out from under us, so to speak (it will be fine as long as
140 * we configure BR0 with the same boot ROM link address).
141 */
142 . = EXC_OFF_SYS_RESET
143
144 .globl _start
145_start: /* time t 0 */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500146 lis r4, CONFIG_DEFAULT_IMMR@h
147 nop
Peter Tyser0c44caf2010-09-14 19:13:53 -0500148
Eran Liberty9095d4a2005-07-28 10:08:46 -0500149 mfmsr r5 /* save msr contents */
Scott Wood838450e2009-01-20 11:56:11 -0600150
151 /* 83xx manuals prescribe a specific sequence for updating IMMRBAR. */
152 bl 1f
1531: mflr r7
154
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200155 lis r3, CONFIG_SYS_IMMR@h
156 ori r3, r3, CONFIG_SYS_IMMR@l
Scott Wood838450e2009-01-20 11:56:11 -0600157
158 lwz r6, IMMRBAR(r4)
159 isync
160
Eran Liberty9095d4a2005-07-28 10:08:46 -0500161 stw r3, IMMRBAR(r4)
Scott Wood838450e2009-01-20 11:56:11 -0600162 lwz r6, 0(r7) /* Arbitrary external load */
163 isync
164
165 lwz r6, IMMRBAR(r3)
166 isync
Jon Loeligerebc72242005-08-01 13:20:47 -0500167
Eran Liberty9095d4a2005-07-28 10:08:46 -0500168 /* Initialise the E300 processor core */
169 /*------------------------------------------*/
Jon Loeligerebc72242005-08-01 13:20:47 -0500170
Scott Wood2b36fbb2012-12-06 13:33:17 +0000171#if (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_MPC83XX_WAIT_FOR_NAND)) || \
172 defined(CONFIG_NAND_SPL)
Lepcha Suchit7ed421f2008-10-16 13:38:00 -0500173 /* The FCM begins execution after only the first page
174 * is loaded. Wait for the rest before branching
175 * to another flash page.
176 */
Scott Wood838450e2009-01-20 11:56:11 -06001771: lwz r6, 0x50b0(r3)
Lepcha Suchit7ed421f2008-10-16 13:38:00 -0500178 andi. r6, r6, 1
179 beq 1b
180#endif
181
Eran Liberty9095d4a2005-07-28 10:08:46 -0500182 bl init_e300_core
Jon Loeligerebc72242005-08-01 13:20:47 -0500183
Tom Rini364d0022023-01-10 11:19:45 -0500184#ifdef CFG_SYS_FLASHBOOT
Eran Liberty9095d4a2005-07-28 10:08:46 -0500185
186 /* Inflate flash location so it appears everywhere, calculate */
187 /* the absolute address in final location of the FLASH, jump */
188 /* there and deflate the flash size back to minimal size */
189 /*------------------------------------------------------------*/
190 bl map_flash_by_law1
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200191 lis r4, (CONFIG_SYS_MONITOR_BASE)@h
192 ori r4, r4, (CONFIG_SYS_MONITOR_BASE)@l
Eran Liberty9095d4a2005-07-28 10:08:46 -0500193 addi r5, r4, in_flash - _start + EXC_OFF_SYS_RESET
194 mtlr r5
195 blr
196in_flash:
197#if 1 /* Remapping flash with LAW0. */
198 bl remap_flash_by_law0
199#endif
Tom Rini364d0022023-01-10 11:19:45 -0500200#endif /* CFG_SYS_FLASHBOOT */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500201
Kumar Galad5d94d62006-02-10 15:40:06 -0600202 /* setup the bats */
203 bl setup_bats
204 sync
205
206 /*
207 * Cache must be enabled here for stack-in-cache trick.
208 * This means we need to enable the BATS.
209 * This means:
210 * 1) for the EVB, original gt regs need to be mapped
211 * 2) need to have an IBAT for the 0xf region,
212 * we are running there!
213 * Cache should be turned on after BATs, since by default
214 * everything is write-through.
215 * The init-mem BAT can be reused after reloc. The old
216 * gt-regs BAT can be reused after board_init_f calls
217 * board_early_init_f (EVB only).
218 */
Christophe Leroycc460252023-01-18 20:52:38 +0100219#ifdef CONFIG_SYS_INIT_RAM_LOCK
Kumar Galad5d94d62006-02-10 15:40:06 -0600220 /* enable address translation */
221 bl enable_addr_trans
222 sync
223
Nick Spence7c20aef2008-08-28 14:09:25 -0700224 /* enable the data cache */
Kumar Galad5d94d62006-02-10 15:40:06 -0600225 bl dcache_enable
226 sync
Kumar Galad5d94d62006-02-10 15:40:06 -0600227 bl lock_ram_in_cache
228 sync
229#endif
230
231 /* set up the stack pointer in our newly created
mario.six@gdsys.ccd4f4a542017-01-17 08:33:47 +0100232 * cache-ram; use r3 to keep the new SP for now to
233 * avoid overiding the SP it uselessly */
Tom Rini4ddbade2022-05-25 12:16:03 -0400234 lis r3, SYS_INIT_SP_ADDR@h
235 ori r3, r3, SYS_INIT_SP_ADDR@l
Kumar Galad5d94d62006-02-10 15:40:06 -0600236
mario.six@gdsys.cc85df7b42017-01-17 08:33:48 +0100237 /* r4 = end of GD area */
238 addi r4, r3, GENERATED_GBL_DATA_SIZE
239
240 /* Zero GD area */
241 li r0, 0
2421:
243 subi r4, r4, 1
244 stb r0, 0(r4)
245 cmplw r3, r4
246 bne 1b
247
Simon Glassadad2d02023-09-26 08:14:27 -0600248#if CONFIG_IS_ENABLED(SYS_MALLOC_F)
mario.six@gdsys.cc85df7b42017-01-17 08:33:48 +0100249
Tom Rini6a5dccc2022-11-16 13:10:41 -0500250#if CONFIG_VAL(SYS_MALLOC_F_LEN) + GENERATED_GBL_DATA_SIZE > CFG_SYS_INIT_RAM_SIZE
Andy Yanad0ac4b2017-07-24 17:47:27 +0800251#error "SYS_MALLOC_F_LEN too large to fit into initial RAM."
mario.six@gdsys.cc85df7b42017-01-17 08:33:48 +0100252#endif
253
254 /* r3 = new stack pointer / pre-reloc malloc area */
Andy Yanad0ac4b2017-07-24 17:47:27 +0800255 subi r3, r3, CONFIG_VAL(SYS_MALLOC_F_LEN)
mario.six@gdsys.cc85df7b42017-01-17 08:33:48 +0100256
257 /* Set pointer to pre-reloc malloc area in GD */
258 stw r3, GD_MALLOC_BASE(r4)
259#endif
Kumar Galad5d94d62006-02-10 15:40:06 -0600260 li r0, 0 /* Make room for stack frame header and */
mario.six@gdsys.ccd4f4a542017-01-17 08:33:47 +0100261 stwu r0, -4(r3) /* clear final stack frame so that */
262 stwu r0, -4(r3) /* stack backtraces terminate cleanly */
Kumar Galad5d94d62006-02-10 15:40:06 -0600263
mario.six@gdsys.ccd4f4a542017-01-17 08:33:47 +0100264 /* Finally, actually set SP */
265 mr r1, r3
Eran Liberty9095d4a2005-07-28 10:08:46 -0500266
267 /* let the C-code set up the rest */
Kumar Galad5d94d62006-02-10 15:40:06 -0600268 /* */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500269 /* Be careful to keep code relocatable & stack humble */
270 /*------------------------------------------------------*/
271
272 GET_GOT /* initialize GOT access */
Joakim Tjernlundf14554d2018-11-28 10:59:55 +0100273 /* Needed for -msingle-pic-base */
274 bl _GLOBAL_OFFSET_TABLE_@local-4
275 mflr r30
Wolfgang Denkb2d36ea2011-04-20 22:11:21 +0200276
Eran Liberty9095d4a2005-07-28 10:08:46 -0500277 /* r3: IMMR */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200278 lis r3, CONFIG_SYS_IMMR@h
Eran Liberty9095d4a2005-07-28 10:08:46 -0500279 /* run low-level CPU init code (in Flash)*/
280 bl cpu_init_f
281
Eran Liberty9095d4a2005-07-28 10:08:46 -0500282 /* run 1st part of board init code (in Flash)*/
Valentin Longchampe91e10a2014-10-03 11:45:23 +0200283 li r3, 0 /* clear boot_flag for calling board_init_f */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500284 bl board_init_f
285
Peter Tyser0c44caf2010-09-14 19:13:53 -0500286 /* NOTREACHED - board_init_f() does not return */
287
Scott Wood2b36fbb2012-12-06 13:33:17 +0000288#ifndef MINIMAL_SPL
Eran Liberty9095d4a2005-07-28 10:08:46 -0500289/*
290 * Vector Table
291 */
292
293 .globl _start_of_vectors
294_start_of_vectors:
295
296/* Machine check */
297 STD_EXCEPTION(0x200, MachineCheck, MachineCheckException)
298
299/* Data Storage exception. */
300 STD_EXCEPTION(0x300, DataStorage, UnknownException)
301
302/* Instruction Storage exception. */
303 STD_EXCEPTION(0x400, InstStorage, UnknownException)
304
305/* External Interrupt exception. */
306#ifndef FIXME
307 STD_EXCEPTION(0x500, ExtInterrupt, external_interrupt)
Jon Loeligerebc72242005-08-01 13:20:47 -0500308#endif
Eran Liberty9095d4a2005-07-28 10:08:46 -0500309
310/* Alignment exception. */
311 . = 0x600
312Alignment:
Rafal Jaworowski06244e42007-06-22 14:58:04 +0200313 EXCEPTION_PROLOG(SRR0, SRR1)
Eran Liberty9095d4a2005-07-28 10:08:46 -0500314 mfspr r4,DAR
315 stw r4,_DAR(r21)
316 mfspr r5,DSISR
317 stw r5,_DSISR(r21)
318 addi r3,r1,STACK_FRAME_OVERHEAD
Joakim Tjernlund4ff6bc02010-01-19 14:41:55 +0100319 EXC_XFER_TEMPLATE(Alignment, AlignmentException, MSR_KERNEL, COPY_EE)
Eran Liberty9095d4a2005-07-28 10:08:46 -0500320
321/* Program check exception */
322 . = 0x700
323ProgramCheck:
Rafal Jaworowski06244e42007-06-22 14:58:04 +0200324 EXCEPTION_PROLOG(SRR0, SRR1)
Eran Liberty9095d4a2005-07-28 10:08:46 -0500325 addi r3,r1,STACK_FRAME_OVERHEAD
Joakim Tjernlund4ff6bc02010-01-19 14:41:55 +0100326 EXC_XFER_TEMPLATE(ProgramCheck, ProgramCheckException,
327 MSR_KERNEL, COPY_EE)
Eran Liberty9095d4a2005-07-28 10:08:46 -0500328
329 STD_EXCEPTION(0x800, FPUnavailable, UnknownException)
330
331 /* I guess we could implement decrementer, and may have
332 * to someday for timekeeping.
333 */
334 STD_EXCEPTION(0x900, Decrementer, timer_interrupt)
335
336 STD_EXCEPTION(0xa00, Trap_0a, UnknownException)
337 STD_EXCEPTION(0xb00, Trap_0b, UnknownException)
338 STD_EXCEPTION(0xc00, SystemCall, UnknownException)
339 STD_EXCEPTION(0xd00, SingleStep, UnknownException)
340
341 STD_EXCEPTION(0xe00, Trap_0e, UnknownException)
342 STD_EXCEPTION(0xf00, Trap_0f, UnknownException)
343
344 STD_EXCEPTION(0x1000, InstructionTLBMiss, UnknownException)
345 STD_EXCEPTION(0x1100, DataLoadTLBMiss, UnknownException)
346 STD_EXCEPTION(0x1200, DataStoreTLBMiss, UnknownException)
347#ifdef DEBUG
348 . = 0x1300
349 /*
350 * This exception occurs when the program counter matches the
351 * Instruction Address Breakpoint Register (IABR).
352 *
353 * I want the cpu to halt if this occurs so I can hunt around
354 * with the debugger and look at things.
355 *
356 * When DEBUG is defined, both machine check enable (in the MSR)
357 * and checkstop reset enable (in the reset mode register) are
358 * turned off and so a checkstop condition will result in the cpu
359 * halting.
360 *
361 * I force the cpu into a checkstop condition by putting an illegal
362 * instruction here (at least this is the theory).
363 *
364 * well - that didnt work, so just do an infinite loop!
365 */
3661: b 1b
367#else
368 STD_EXCEPTION(0x1300, InstructionBreakpoint, DebugException)
369#endif
370 STD_EXCEPTION(0x1400, SMI, UnknownException)
371
372 STD_EXCEPTION(0x1500, Trap_15, UnknownException)
373 STD_EXCEPTION(0x1600, Trap_16, UnknownException)
374 STD_EXCEPTION(0x1700, Trap_17, UnknownException)
375 STD_EXCEPTION(0x1800, Trap_18, UnknownException)
376 STD_EXCEPTION(0x1900, Trap_19, UnknownException)
377 STD_EXCEPTION(0x1a00, Trap_1a, UnknownException)
378 STD_EXCEPTION(0x1b00, Trap_1b, UnknownException)
379 STD_EXCEPTION(0x1c00, Trap_1c, UnknownException)
380 STD_EXCEPTION(0x1d00, Trap_1d, UnknownException)
381 STD_EXCEPTION(0x1e00, Trap_1e, UnknownException)
382 STD_EXCEPTION(0x1f00, Trap_1f, UnknownException)
383 STD_EXCEPTION(0x2000, Trap_20, UnknownException)
384 STD_EXCEPTION(0x2100, Trap_21, UnknownException)
385 STD_EXCEPTION(0x2200, Trap_22, UnknownException)
386 STD_EXCEPTION(0x2300, Trap_23, UnknownException)
387 STD_EXCEPTION(0x2400, Trap_24, UnknownException)
388 STD_EXCEPTION(0x2500, Trap_25, UnknownException)
389 STD_EXCEPTION(0x2600, Trap_26, UnknownException)
390 STD_EXCEPTION(0x2700, Trap_27, UnknownException)
391 STD_EXCEPTION(0x2800, Trap_28, UnknownException)
392 STD_EXCEPTION(0x2900, Trap_29, UnknownException)
393 STD_EXCEPTION(0x2a00, Trap_2a, UnknownException)
394 STD_EXCEPTION(0x2b00, Trap_2b, UnknownException)
395 STD_EXCEPTION(0x2c00, Trap_2c, UnknownException)
396 STD_EXCEPTION(0x2d00, Trap_2d, UnknownException)
397 STD_EXCEPTION(0x2e00, Trap_2e, UnknownException)
398 STD_EXCEPTION(0x2f00, Trap_2f, UnknownException)
399
400
401 .globl _end_of_vectors
402_end_of_vectors:
403
404 . = 0x3000
405
406/*
407 * This code finishes saving the registers to the exception frame
408 * and jumps to the appropriate handler for the exception.
409 * Register r21 is pointer into trap frame, r1 has new stack pointer.
410 */
411 .globl transfer_to_handler
412transfer_to_handler:
413 stw r22,_NIP(r21)
414 lis r22,MSR_POW@h
415 andc r23,r23,r22
416 stw r23,_MSR(r21)
417 SAVE_GPR(7, r21)
418 SAVE_4GPRS(8, r21)
419 SAVE_8GPRS(12, r21)
420 SAVE_8GPRS(24, r21)
421 mflr r23
422 andi. r24,r23,0x3f00 /* get vector offset */
423 stw r24,TRAP(r21)
424 li r22,0
425 stw r22,RESULT(r21)
426 lwz r24,0(r23) /* virtual address of handler */
427 lwz r23,4(r23) /* where to go when done */
428 mtspr SRR0,r24
429 mtspr SRR1,r20
430 mtlr r23
431 SYNC
432 rfi /* jump to handler, enable MMU */
433
434int_return:
435 mfmsr r28 /* Disable interrupts */
436 li r4,0
437 ori r4,r4,MSR_EE
438 andc r28,r28,r4
439 SYNC /* Some chip revs need this... */
440 mtmsr r28
441 SYNC
442 lwz r2,_CTR(r1)
443 lwz r0,_LINK(r1)
444 mtctr r2
445 mtlr r0
446 lwz r2,_XER(r1)
447 lwz r0,_CCR(r1)
448 mtspr XER,r2
449 mtcrf 0xFF,r0
450 REST_10GPRS(3, r1)
451 REST_10GPRS(13, r1)
452 REST_8GPRS(23, r1)
453 REST_GPR(31, r1)
454 lwz r2,_NIP(r1) /* Restore environment */
455 lwz r0,_MSR(r1)
456 mtspr SRR0,r2
457 mtspr SRR1,r0
458 lwz r0,GPR0(r1)
459 lwz r2,GPR2(r1)
460 lwz r1,GPR1(r1)
461 SYNC
462 rfi
Scott Wood2b36fbb2012-12-06 13:33:17 +0000463#endif /* !MINIMAL_SPL */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500464
465/*
466 * This code initialises the E300 processor core
467 * (conforms to PowerPC 603e spec)
468 * Note: expects original MSR contents to be in r5.
469 */
470 .globl init_e300_core
471init_e300_core: /* time t 10 */
472 /* Initialize machine status; enable machine check interrupt */
473 /*-----------------------------------------------------------*/
474
475 li r3, MSR_KERNEL /* Set ME and RI flags */
476 rlwimi r3, r5, 0, 25, 25 /* preserve IP bit set by HRCW */
477#ifdef DEBUG
478 rlwimi r3, r5, 0, 21, 22 /* debugger might set SE & BE bits */
479#endif
480 SYNC /* Some chip revs need this... */
481 mtmsr r3
482 SYNC
483 mtspr SRR1, r3 /* Make SRR1 match MSR */
484
485
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200486 lis r3, CONFIG_SYS_IMMR@h
Christophe Leroy39d37952023-04-03 10:39:59 +0200487#ifndef CONFIG_WDT_MPC8xxx
Eran Liberty9095d4a2005-07-28 10:08:46 -0500488#if defined(CONFIG_WATCHDOG)
Horst Kronstorfer4565e042010-05-18 10:37:05 +0200489 /* Initialise the Watchdog values and reset it (if req) */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500490 /*------------------------------------------------------*/
Tom Rini6a5dccc2022-11-16 13:10:41 -0500491 lis r4, CFG_SYS_WATCHDOG_VALUE
Eran Liberty9095d4a2005-07-28 10:08:46 -0500492 ori r4, r4, (SWCRR_SWEN | SWCRR_SWRI | SWCRR_SWPR)
493 stw r4, SWCRR(r3)
Jon Loeligerebc72242005-08-01 13:20:47 -0500494
Eran Liberty9095d4a2005-07-28 10:08:46 -0500495 /* and reset it */
Jon Loeligerebc72242005-08-01 13:20:47 -0500496
Eran Liberty9095d4a2005-07-28 10:08:46 -0500497 li r4, 0x556C
498 sth r4, SWSRR@l(r3)
Heiko Schocher6dfb2e52008-01-11 15:15:17 +0100499 li r4, -0x55C7
Eran Liberty9095d4a2005-07-28 10:08:46 -0500500 sth r4, SWSRR@l(r3)
501#else
Horst Kronstorfer4565e042010-05-18 10:37:05 +0200502 /* Disable Watchdog */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500503 /*-------------------*/
Kumar Galab42751c2006-01-11 11:23:01 -0600504 lwz r4, SWCRR(r3)
505 /* Check to see if its enabled for disabling
506 once disabled by SW you can't re-enable */
507 andi. r4, r4, 0x4
508 beq 1f
Eran Liberty9095d4a2005-07-28 10:08:46 -0500509 xor r4, r4, r4
510 stw r4, SWCRR(r3)
Kumar Galab42751c2006-01-11 11:23:01 -06005111:
Eran Liberty9095d4a2005-07-28 10:08:46 -0500512#endif /* CONFIG_WATCHDOG */
Christophe Leroy39d37952023-04-03 10:39:59 +0200513#endif
Eran Liberty9095d4a2005-07-28 10:08:46 -0500514
Nick Spence56fd3c22008-08-28 14:09:19 -0700515#if defined(CONFIG_MASK_AER_AO)
516 /* Write the Arbiter Event Enable to mask Address Only traps. */
517 /* This prevents the dcbz instruction from being trapped when */
518 /* HID0_ABE Address Broadcast Enable is set and the MEMORY */
519 /* COHERENCY bit is set in the WIMG bits, which is often */
520 /* needed for PCI operation. */
521 lwz r4, 0x0808(r3)
522 rlwinm r0, r4, 0, ~AER_AO
523 stw r0, 0x0808(r3)
524#endif /* CONFIG_MASK_AER_AO */
525
Eran Liberty9095d4a2005-07-28 10:08:46 -0500526 /* Initialize the Hardware Implementation-dependent Registers */
527 /* HID0 also contains cache control */
Nick Spence7c20aef2008-08-28 14:09:25 -0700528 /* - force invalidation of data and instruction caches */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500529 /*------------------------------------------------------*/
530
Tom Rini364d0022023-01-10 11:19:45 -0500531 lis r3, CFG_SYS_HID0_INIT@h
532 ori r3, r3, (CFG_SYS_HID0_INIT | HID0_ICFI | HID0_DCFI)@l
Eran Liberty9095d4a2005-07-28 10:08:46 -0500533 SYNC
534 mtspr HID0, r3
535
Tom Rini364d0022023-01-10 11:19:45 -0500536 lis r3, CFG_SYS_HID0_FINAL@h
537 ori r3, r3, (CFG_SYS_HID0_FINAL & ~(HID0_ICFI | HID0_DCFI))@l
Eran Liberty9095d4a2005-07-28 10:08:46 -0500538 SYNC
539 mtspr HID0, r3
540
Tom Rini364d0022023-01-10 11:19:45 -0500541 lis r3, CFG_SYS_HID2@h
542 ori r3, r3, CFG_SYS_HID2@l
Eran Liberty9095d4a2005-07-28 10:08:46 -0500543 SYNC
544 mtspr HID2, r3
545
Eran Liberty9095d4a2005-07-28 10:08:46 -0500546 /* Done! */
547 /*------------------------------*/
Jon Loeligerebc72242005-08-01 13:20:47 -0500548 blr
Eran Liberty9095d4a2005-07-28 10:08:46 -0500549
Kumar Galad5d94d62006-02-10 15:40:06 -0600550 /* setup_bats - set them up to some initial state */
551 .globl setup_bats
552setup_bats:
553 addis r0, r0, 0x0000
554
555 /* IBAT 0 */
Tom Rini364d0022023-01-10 11:19:45 -0500556 addis r4, r0, CFG_SYS_IBAT0L@h
557 ori r4, r4, CFG_SYS_IBAT0L@l
558 addis r3, r0, CFG_SYS_IBAT0U@h
559 ori r3, r3, CFG_SYS_IBAT0U@l
Kumar Galad5d94d62006-02-10 15:40:06 -0600560 mtspr IBAT0L, r4
561 mtspr IBAT0U, r3
Kumar Galad5d94d62006-02-10 15:40:06 -0600562
563 /* DBAT 0 */
Tom Rini364d0022023-01-10 11:19:45 -0500564 addis r4, r0, CFG_SYS_DBAT0L@h
565 ori r4, r4, CFG_SYS_DBAT0L@l
566 addis r3, r0, CFG_SYS_DBAT0U@h
567 ori r3, r3, CFG_SYS_DBAT0U@l
Kumar Galad5d94d62006-02-10 15:40:06 -0600568 mtspr DBAT0L, r4
569 mtspr DBAT0U, r3
Kumar Galad5d94d62006-02-10 15:40:06 -0600570
571 /* IBAT 1 */
Tom Rini364d0022023-01-10 11:19:45 -0500572 addis r4, r0, CFG_SYS_IBAT1L@h
573 ori r4, r4, CFG_SYS_IBAT1L@l
574 addis r3, r0, CFG_SYS_IBAT1U@h
575 ori r3, r3, CFG_SYS_IBAT1U@l
Kumar Galad5d94d62006-02-10 15:40:06 -0600576 mtspr IBAT1L, r4
577 mtspr IBAT1U, r3
Kumar Galad5d94d62006-02-10 15:40:06 -0600578
579 /* DBAT 1 */
Tom Rini364d0022023-01-10 11:19:45 -0500580 addis r4, r0, CFG_SYS_DBAT1L@h
581 ori r4, r4, CFG_SYS_DBAT1L@l
582 addis r3, r0, CFG_SYS_DBAT1U@h
583 ori r3, r3, CFG_SYS_DBAT1U@l
Kumar Galad5d94d62006-02-10 15:40:06 -0600584 mtspr DBAT1L, r4
585 mtspr DBAT1U, r3
Kumar Galad5d94d62006-02-10 15:40:06 -0600586
587 /* IBAT 2 */
Tom Rini364d0022023-01-10 11:19:45 -0500588 addis r4, r0, CFG_SYS_IBAT2L@h
589 ori r4, r4, CFG_SYS_IBAT2L@l
590 addis r3, r0, CFG_SYS_IBAT2U@h
591 ori r3, r3, CFG_SYS_IBAT2U@l
Kumar Galad5d94d62006-02-10 15:40:06 -0600592 mtspr IBAT2L, r4
593 mtspr IBAT2U, r3
Kumar Galad5d94d62006-02-10 15:40:06 -0600594
595 /* DBAT 2 */
Tom Rini364d0022023-01-10 11:19:45 -0500596 addis r4, r0, CFG_SYS_DBAT2L@h
597 ori r4, r4, CFG_SYS_DBAT2L@l
598 addis r3, r0, CFG_SYS_DBAT2U@h
599 ori r3, r3, CFG_SYS_DBAT2U@l
Kumar Galad5d94d62006-02-10 15:40:06 -0600600 mtspr DBAT2L, r4
601 mtspr DBAT2U, r3
Kumar Galad5d94d62006-02-10 15:40:06 -0600602
603 /* IBAT 3 */
Tom Rini364d0022023-01-10 11:19:45 -0500604 addis r4, r0, CFG_SYS_IBAT3L@h
605 ori r4, r4, CFG_SYS_IBAT3L@l
606 addis r3, r0, CFG_SYS_IBAT3U@h
607 ori r3, r3, CFG_SYS_IBAT3U@l
Kumar Galad5d94d62006-02-10 15:40:06 -0600608 mtspr IBAT3L, r4
609 mtspr IBAT3U, r3
Kumar Galad5d94d62006-02-10 15:40:06 -0600610
611 /* DBAT 3 */
Tom Rini364d0022023-01-10 11:19:45 -0500612 addis r4, r0, CFG_SYS_DBAT3L@h
613 ori r4, r4, CFG_SYS_DBAT3L@l
614 addis r3, r0, CFG_SYS_DBAT3U@h
615 ori r3, r3, CFG_SYS_DBAT3U@l
Kumar Galad5d94d62006-02-10 15:40:06 -0600616 mtspr DBAT3L, r4
617 mtspr DBAT3U, r3
Kumar Galad5d94d62006-02-10 15:40:06 -0600618
Becky Bruce03ea1be2008-05-08 19:02:12 -0500619#ifdef CONFIG_HIGH_BATS
Kumar Galad5d94d62006-02-10 15:40:06 -0600620 /* IBAT 4 */
Tom Rini364d0022023-01-10 11:19:45 -0500621 addis r4, r0, CFG_SYS_IBAT4L@h
622 ori r4, r4, CFG_SYS_IBAT4L@l
623 addis r3, r0, CFG_SYS_IBAT4U@h
624 ori r3, r3, CFG_SYS_IBAT4U@l
Kumar Galad5d94d62006-02-10 15:40:06 -0600625 mtspr IBAT4L, r4
626 mtspr IBAT4U, r3
Kumar Galad5d94d62006-02-10 15:40:06 -0600627
628 /* DBAT 4 */
Tom Rini364d0022023-01-10 11:19:45 -0500629 addis r4, r0, CFG_SYS_DBAT4L@h
630 ori r4, r4, CFG_SYS_DBAT4L@l
631 addis r3, r0, CFG_SYS_DBAT4U@h
632 ori r3, r3, CFG_SYS_DBAT4U@l
Kumar Galad5d94d62006-02-10 15:40:06 -0600633 mtspr DBAT4L, r4
634 mtspr DBAT4U, r3
Kumar Galad5d94d62006-02-10 15:40:06 -0600635
636 /* IBAT 5 */
Tom Rini364d0022023-01-10 11:19:45 -0500637 addis r4, r0, CFG_SYS_IBAT5L@h
638 ori r4, r4, CFG_SYS_IBAT5L@l
639 addis r3, r0, CFG_SYS_IBAT5U@h
640 ori r3, r3, CFG_SYS_IBAT5U@l
Kumar Galad5d94d62006-02-10 15:40:06 -0600641 mtspr IBAT5L, r4
642 mtspr IBAT5U, r3
Kumar Galad5d94d62006-02-10 15:40:06 -0600643
644 /* DBAT 5 */
Tom Rini364d0022023-01-10 11:19:45 -0500645 addis r4, r0, CFG_SYS_DBAT5L@h
646 ori r4, r4, CFG_SYS_DBAT5L@l
647 addis r3, r0, CFG_SYS_DBAT5U@h
648 ori r3, r3, CFG_SYS_DBAT5U@l
Kumar Galad5d94d62006-02-10 15:40:06 -0600649 mtspr DBAT5L, r4
650 mtspr DBAT5U, r3
Kumar Galad5d94d62006-02-10 15:40:06 -0600651
652 /* IBAT 6 */
Tom Rini364d0022023-01-10 11:19:45 -0500653 addis r4, r0, CFG_SYS_IBAT6L@h
654 ori r4, r4, CFG_SYS_IBAT6L@l
655 addis r3, r0, CFG_SYS_IBAT6U@h
656 ori r3, r3, CFG_SYS_IBAT6U@l
Kumar Galad5d94d62006-02-10 15:40:06 -0600657 mtspr IBAT6L, r4
658 mtspr IBAT6U, r3
Kumar Galad5d94d62006-02-10 15:40:06 -0600659
660 /* DBAT 6 */
Tom Rini364d0022023-01-10 11:19:45 -0500661 addis r4, r0, CFG_SYS_DBAT6L@h
662 ori r4, r4, CFG_SYS_DBAT6L@l
663 addis r3, r0, CFG_SYS_DBAT6U@h
664 ori r3, r3, CFG_SYS_DBAT6U@l
Kumar Galad5d94d62006-02-10 15:40:06 -0600665 mtspr DBAT6L, r4
666 mtspr DBAT6U, r3
Kumar Galad5d94d62006-02-10 15:40:06 -0600667
668 /* IBAT 7 */
Tom Rini364d0022023-01-10 11:19:45 -0500669 addis r4, r0, CFG_SYS_IBAT7L@h
670 ori r4, r4, CFG_SYS_IBAT7L@l
671 addis r3, r0, CFG_SYS_IBAT7U@h
672 ori r3, r3, CFG_SYS_IBAT7U@l
Kumar Galad5d94d62006-02-10 15:40:06 -0600673 mtspr IBAT7L, r4
674 mtspr IBAT7U, r3
Kumar Galad5d94d62006-02-10 15:40:06 -0600675
676 /* DBAT 7 */
Tom Rini364d0022023-01-10 11:19:45 -0500677 addis r4, r0, CFG_SYS_DBAT7L@h
678 ori r4, r4, CFG_SYS_DBAT7L@l
679 addis r3, r0, CFG_SYS_DBAT7U@h
680 ori r3, r3, CFG_SYS_DBAT7U@l
Kumar Galad5d94d62006-02-10 15:40:06 -0600681 mtspr DBAT7L, r4
682 mtspr DBAT7U, r3
Kumar Galad5d94d62006-02-10 15:40:06 -0600683#endif
684
Scott Woodb71689b2008-06-30 14:13:28 -0500685 isync
686
687 /* invalidate all tlb's
688 *
689 * From the 603e User Manual: "The 603e provides the ability to
690 * invalidate a TLB entry. The TLB Invalidate Entry (tlbie)
691 * instruction invalidates the TLB entry indexed by the EA, and
692 * operates on both the instruction and data TLBs simultaneously
693 * invalidating four TLB entries (both sets in each TLB). The
694 * index corresponds to bits 15-19 of the EA. To invalidate all
695 * entries within both TLBs, 32 tlbie instructions should be
696 * issued, incrementing this field by one each time."
697 *
698 * "Note that the tlbia instruction is not implemented on the
699 * 603e."
700 *
701 * bits 15-19 correspond to addresses 0x00000000 to 0x0001F000
702 * incrementing by 0x1000 each time. The code below is sort of
Stefan Roese88fbf932010-04-15 16:07:28 +0200703 * based on code in "flush_tlbs" from arch/powerpc/kernel/head.S
Scott Woodb71689b2008-06-30 14:13:28 -0500704 *
Kumar Galad5d94d62006-02-10 15:40:06 -0600705 */
706 lis r3, 0
707 lis r5, 2
708
7091:
710 tlbie r3
711 addi r3, r3, 0x1000
712 cmp 0, 0, r3, r5
713 blt 1b
714
715 blr
716
Eran Liberty9095d4a2005-07-28 10:08:46 -0500717/* Cache functions.
718 *
719 * Note: requires that all cache bits in
720 * HID0 are in the low half word.
721 */
Scott Wood2b36fbb2012-12-06 13:33:17 +0000722#ifndef MINIMAL_SPL
Eran Liberty9095d4a2005-07-28 10:08:46 -0500723 .globl icache_enable
724icache_enable:
725 mfspr r3, HID0
726 ori r3, r3, HID0_ICE
Nick Spence7c20aef2008-08-28 14:09:25 -0700727 li r4, HID0_ICFI|HID0_ILOCK
Eran Liberty9095d4a2005-07-28 10:08:46 -0500728 andc r3, r3, r4
729 ori r4, r3, HID0_ICFI
730 isync
731 mtspr HID0, r4 /* sets enable and invalidate, clears lock */
732 isync
733 mtspr HID0, r3 /* clears invalidate */
734 blr
735
736 .globl icache_disable
737icache_disable:
738 mfspr r3, HID0
739 lis r4, 0
Nick Spence7c20aef2008-08-28 14:09:25 -0700740 ori r4, r4, HID0_ICE|HID0_ICFI|HID0_ILOCK
Eran Liberty9095d4a2005-07-28 10:08:46 -0500741 andc r3, r3, r4
Eran Liberty9095d4a2005-07-28 10:08:46 -0500742 isync
Nick Spence7c20aef2008-08-28 14:09:25 -0700743 mtspr HID0, r3 /* clears invalidate, enable and lock */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500744 blr
745
746 .globl icache_status
747icache_status:
748 mfspr r3, HID0
Marian Balakowicz758e5d32006-03-14 16:01:25 +0100749 rlwinm r3, r3, (31 - HID0_ICE_SHIFT + 1), 31, 31
Eran Liberty9095d4a2005-07-28 10:08:46 -0500750 blr
Scott Wood2b36fbb2012-12-06 13:33:17 +0000751#endif /* !MINIMAL_SPL */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500752
753 .globl dcache_enable
754dcache_enable:
755 mfspr r3, HID0
Kumar Galad5d94d62006-02-10 15:40:06 -0600756 li r5, HID0_DCFI|HID0_DLOCK
757 andc r3, r3, r5
Kumar Galad5d94d62006-02-10 15:40:06 -0600758 ori r3, r3, HID0_DCE
Eran Liberty9095d4a2005-07-28 10:08:46 -0500759 sync
Nick Spence7c20aef2008-08-28 14:09:25 -0700760 mtspr HID0, r3 /* enable, no invalidate */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500761 blr
762
763 .globl dcache_disable
764dcache_disable:
Nick Spence7c20aef2008-08-28 14:09:25 -0700765 mflr r4
766 bl flush_dcache /* uses r3 and r5 */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500767 mfspr r3, HID0
Nick Spence7c20aef2008-08-28 14:09:25 -0700768 li r5, HID0_DCE|HID0_DLOCK
769 andc r3, r3, r5
770 ori r5, r3, HID0_DCFI
Eran Liberty9095d4a2005-07-28 10:08:46 -0500771 sync
Nick Spence7c20aef2008-08-28 14:09:25 -0700772 mtspr HID0, r5 /* sets invalidate, clears enable and lock */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500773 sync
774 mtspr HID0, r3 /* clears invalidate */
Nick Spence7c20aef2008-08-28 14:09:25 -0700775 mtlr r4
Eran Liberty9095d4a2005-07-28 10:08:46 -0500776 blr
777
778 .globl dcache_status
779dcache_status:
780 mfspr r3, HID0
Marian Balakowicz758e5d32006-03-14 16:01:25 +0100781 rlwinm r3, r3, (31 - HID0_DCE_SHIFT + 1), 31, 31
Eran Liberty9095d4a2005-07-28 10:08:46 -0500782 blr
783
Nick Spence7c20aef2008-08-28 14:09:25 -0700784 .globl flush_dcache
785flush_dcache:
786 lis r3, 0
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200787 lis r5, CONFIG_SYS_CACHELINE_SIZE
Nick Spence7c20aef2008-08-28 14:09:25 -07007881: cmp 0, 1, r3, r5
789 bge 2f
790 lwz r5, 0(r3)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200791 lis r5, CONFIG_SYS_CACHELINE_SIZE
Nick Spence7c20aef2008-08-28 14:09:25 -0700792 addi r3, r3, 0x4
793 b 1b
7942: blr
795
Eran Liberty9095d4a2005-07-28 10:08:46 -0500796/*-------------------------------------------------------------------*/
797
798/*
Simon Glass284f71b2019-12-28 10:44:45 -0700799 * void relocate_code(addr_sp, gd, addr_moni)
Eran Liberty9095d4a2005-07-28 10:08:46 -0500800 *
801 * This "function" does not return, instead it continues in RAM
802 * after relocating the monitor code.
803 *
804 * r3 = dest
805 * r4 = src
806 * r5 = length in bytes
807 * r6 = cachelinesize
808 */
809 .globl relocate_code
810relocate_code:
811 mr r1, r3 /* Set new stack pointer */
812 mr r9, r4 /* Save copy of Global Data pointer */
813 mr r10, r5 /* Save copy of Destination Address */
814
Joakim Tjernlund3fbaa4d2010-01-19 14:41:56 +0100815 GET_GOT
Eran Liberty9095d4a2005-07-28 10:08:46 -0500816 mr r3, r5 /* Destination Address */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200817 lis r4, CONFIG_SYS_MONITOR_BASE@h /* Source Address */
818 ori r4, r4, CONFIG_SYS_MONITOR_BASE@l
Scott Woodb71689b2008-06-30 14:13:28 -0500819 lwz r5, GOT(__bss_start)
Eran Liberty9095d4a2005-07-28 10:08:46 -0500820 sub r5, r5, r4
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200821 li r6, CONFIG_SYS_CACHELINE_SIZE /* Cache Line Size */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500822
823 /*
824 * Fix GOT pointer:
825 *
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200826 * New GOT-PTR = (old GOT-PTR - CONFIG_SYS_MONITOR_BASE)
Eran Liberty9095d4a2005-07-28 10:08:46 -0500827 * + Destination Address
828 *
829 * Offset:
830 */
831 sub r15, r10, r4
832
833 /* First our own GOT */
Joakim Tjernlund3fbaa4d2010-01-19 14:41:56 +0100834 add r12, r12, r15
Eran Liberty9095d4a2005-07-28 10:08:46 -0500835 /* then the one used by the C code */
836 add r30, r30, r15
837
838 /*
839 * Now relocate code
840 */
841
842 cmplw cr1,r3,r4
843 addi r0,r5,3
844 srwi. r0,r0,2
845 beq cr1,4f /* In place copy is not necessary */
846 beq 7f /* Protect against 0 count */
847 mtctr r0
848 bge cr1,2f
849 la r8,-4(r4)
850 la r7,-4(r3)
851
852 /* copy */
8531: lwzu r0,4(r8)
854 stwu r0,4(r7)
855 bdnz 1b
856
857 addi r0,r5,3
858 srwi. r0,r0,2
859 mtctr r0
860 la r8,-4(r4)
861 la r7,-4(r3)
Jon Loeligerebc72242005-08-01 13:20:47 -0500862
863 /* and compare */
Eran Liberty9095d4a2005-07-28 10:08:46 -050086420: lwzu r20,4(r8)
865 lwzu r21,4(r7)
866 xor. r22, r20, r21
867 bne 30f
868 bdnz 20b
869 b 4f
870
871 /* compare failed */
87230: li r3, 0
873 blr
874
8752: slwi r0,r0,2 /* re copy in reverse order ... y do we needed it? */
876 add r8,r4,r0
877 add r7,r3,r0
8783: lwzu r0,-4(r8)
879 stwu r0,-4(r7)
880 bdnz 3b
Eran Liberty9095d4a2005-07-28 10:08:46 -0500881
882/*
883 * Now flush the cache: note that we must start from a cache aligned
884 * address. Otherwise we might miss one cache line.
885 */
Kumar Galad5d94d62006-02-10 15:40:06 -06008864: cmpwi r6,0
Eran Liberty9095d4a2005-07-28 10:08:46 -0500887 add r5,r3,r5
Kumar Galad5d94d62006-02-10 15:40:06 -0600888 beq 7f /* Always flush prefetch queue in any case */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500889 subi r0,r6,1
890 andc r3,r3,r0
Eran Liberty9095d4a2005-07-28 10:08:46 -0500891 mr r4,r3
8925: dcbst 0,r4
893 add r4,r4,r6
894 cmplw r4,r5
895 blt 5b
Kumar Galad5d94d62006-02-10 15:40:06 -0600896 sync /* Wait for all dcbst to complete on bus */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500897 mr r4,r3
8986: icbi 0,r4
899 add r4,r4,r6
900 cmplw r4,r5
901 blt 6b
Kumar Galad5d94d62006-02-10 15:40:06 -06009027: sync /* Wait for all icbi to complete on bus */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500903 isync
904
905/*
906 * We are done. Do not return, instead branch to second part of board
907 * initialization, now running from RAM.
908 */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500909 addi r0, r10, in_ram - _start + EXC_OFF_SYS_RESET
910 mtlr r0
911 blr
912
913in_ram:
914
915 /*
Joakim Tjernlund3fbaa4d2010-01-19 14:41:56 +0100916 * Relocation Function, r12 point to got2+0x8000
Eran Liberty9095d4a2005-07-28 10:08:46 -0500917 *
918 * Adjust got2 pointers, no need to check for 0, this code
919 * already puts a few entries in the table.
920 */
921 li r0,__got2_entries@sectoff@l
922 la r3,GOT(_GOT2_TABLE_)
923 lwz r11,GOT(_GOT2_TABLE_)
924 mtctr r0
925 sub r11,r3,r11
926 addi r3,r3,-4
9271: lwzu r0,4(r3)
Joakim Tjernlund4f2fdac2009-10-08 02:03:51 +0200928 cmpwi r0,0
929 beq- 2f
Eran Liberty9095d4a2005-07-28 10:08:46 -0500930 add r0,r0,r11
931 stw r0,0(r3)
Joakim Tjernlund4f2fdac2009-10-08 02:03:51 +02009322: bdnz 1b
Eran Liberty9095d4a2005-07-28 10:08:46 -0500933
Scott Wood2b36fbb2012-12-06 13:33:17 +0000934#ifndef MINIMAL_SPL
Eran Liberty9095d4a2005-07-28 10:08:46 -0500935 /*
936 * Now adjust the fixups and the pointers to the fixups
937 * in case we need to move ourselves again.
938 */
Joakim Tjernlund4f2fdac2009-10-08 02:03:51 +0200939 li r0,__fixup_entries@sectoff@l
Eran Liberty9095d4a2005-07-28 10:08:46 -0500940 lwz r3,GOT(_FIXUP_TABLE_)
941 cmpwi r0,0
942 mtctr r0
943 addi r3,r3,-4
944 beq 4f
9453: lwzu r4,4(r3)
946 lwzux r0,r4,r11
Joakim Tjernlundc61b25a2010-10-14 11:51:44 +0200947 cmpwi r0,0
Eran Liberty9095d4a2005-07-28 10:08:46 -0500948 add r0,r0,r11
Joakim Tjernlund401b5922010-11-04 19:02:00 +0100949 stw r4,0(r3)
Joakim Tjernlundc61b25a2010-10-14 11:51:44 +0200950 beq- 5f
Eran Liberty9095d4a2005-07-28 10:08:46 -0500951 stw r0,0(r4)
Joakim Tjernlundc61b25a2010-10-14 11:51:44 +02009525: bdnz 3b
Eran Liberty9095d4a2005-07-28 10:08:46 -05009534:
Scott Woodb71689b2008-06-30 14:13:28 -0500954#endif
955
Eran Liberty9095d4a2005-07-28 10:08:46 -0500956clear_bss:
957 /*
958 * Now clear BSS segment
959 */
960 lwz r3,GOT(__bss_start)
Simon Glassed70c8f2013-03-14 06:54:53 +0000961 lwz r4,GOT(__bss_end)
Eran Liberty9095d4a2005-07-28 10:08:46 -0500962
963 cmplw 0, r3, r4
964 beq 6f
965
966 li r0, 0
9675:
968 stw r0, 0(r3)
969 addi r3, r3, 4
970 cmplw 0, r3, r4
971 bne 5b
9726:
973
974 mr r3, r9 /* Global Data pointer */
975 mr r4, r10 /* Destination Address */
976 bl board_init_r
977
Scott Wood2b36fbb2012-12-06 13:33:17 +0000978#ifndef MINIMAL_SPL
Eran Liberty9095d4a2005-07-28 10:08:46 -0500979 /*
980 * Copy exception vector code to low memory
981 *
982 * r3: dest_addr
983 * r7: source address, r8: end address, r9: target address
984 */
985 .globl trap_init
986trap_init:
Joakim Tjernlund3fbaa4d2010-01-19 14:41:56 +0100987 mflr r4 /* save link register */
988 GET_GOT
Eran Liberty9095d4a2005-07-28 10:08:46 -0500989 lwz r7, GOT(_start)
990 lwz r8, GOT(_end_of_vectors)
991
992 li r9, 0x100 /* reset vector always at 0x100 */
993
994 cmplw 0, r7, r8
995 bgelr /* return if r7>=r8 - just in case */
Eran Liberty9095d4a2005-07-28 10:08:46 -05009961:
997 lwz r0, 0(r7)
998 stw r0, 0(r9)
999 addi r7, r7, 4
1000 addi r9, r9, 4
1001 cmplw 0, r7, r8
1002 bne 1b
1003
1004 /*
1005 * relocate `hdlr' and `int_return' entries
1006 */
1007 li r7, .L_MachineCheck - _start + EXC_OFF_SYS_RESET
1008 li r8, Alignment - _start + EXC_OFF_SYS_RESET
10092:
1010 bl trap_reloc
1011 addi r7, r7, 0x100 /* next exception vector */
1012 cmplw 0, r7, r8
1013 blt 2b
1014
1015 li r7, .L_Alignment - _start + EXC_OFF_SYS_RESET
1016 bl trap_reloc
1017
1018 li r7, .L_ProgramCheck - _start + EXC_OFF_SYS_RESET
1019 bl trap_reloc
1020
1021 li r7, .L_FPUnavailable - _start + EXC_OFF_SYS_RESET
1022 li r8, SystemCall - _start + EXC_OFF_SYS_RESET
10233:
1024 bl trap_reloc
1025 addi r7, r7, 0x100 /* next exception vector */
1026 cmplw 0, r7, r8
1027 blt 3b
1028
1029 li r7, .L_SingleStep - _start + EXC_OFF_SYS_RESET
1030 li r8, _end_of_vectors - _start + EXC_OFF_SYS_RESET
10314:
1032 bl trap_reloc
1033 addi r7, r7, 0x100 /* next exception vector */
1034 cmplw 0, r7, r8
1035 blt 4b
1036
1037 mfmsr r3 /* now that the vectors have */
1038 lis r7, MSR_IP@h /* relocated into low memory */
1039 ori r7, r7, MSR_IP@l /* MSR[IP] can be turned off */
1040 andc r3, r3, r7 /* (if it was on) */
1041 SYNC /* Some chip revs need this... */
1042 mtmsr r3
1043 SYNC
1044
1045 mtlr r4 /* restore link register */
1046 blr
1047
Scott Wood2b36fbb2012-12-06 13:33:17 +00001048#endif /* !MINIMAL_SPL */
Eran Liberty9095d4a2005-07-28 10:08:46 -05001049
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001050#ifdef CONFIG_SYS_INIT_RAM_LOCK
Kumar Galad5d94d62006-02-10 15:40:06 -06001051lock_ram_in_cache:
1052 /* Allocate Initial RAM in data cache.
1053 */
Tom Rini6a5dccc2022-11-16 13:10:41 -05001054 lis r3, (CFG_SYS_INIT_RAM_ADDR & ~31)@h
1055 ori r3, r3, (CFG_SYS_INIT_RAM_ADDR & ~31)@l
1056 li r4, ((CFG_SYS_INIT_RAM_SIZE & ~31) + \
1057 (CFG_SYS_INIT_RAM_ADDR & 31) + 31) / 32
Nick Spenceb89d6fb2008-08-28 14:09:11 -07001058 mtctr r4
Kumar Galad5d94d62006-02-10 15:40:06 -060010591:
1060 dcbz r0, r3
1061 addi r3, r3, 32
1062 bdnz 1b
1063
1064 /* Lock the data cache */
1065 mfspr r0, HID0
Nick Spence7c20aef2008-08-28 14:09:25 -07001066 ori r0, r0, HID0_DLOCK
Kumar Galad5d94d62006-02-10 15:40:06 -06001067 sync
1068 mtspr HID0, r0
1069 sync
1070 blr
1071
Scott Wood2b36fbb2012-12-06 13:33:17 +00001072#ifndef MINIMAL_SPL
Eran Liberty9095d4a2005-07-28 10:08:46 -05001073.globl unlock_ram_in_cache
1074unlock_ram_in_cache:
1075 /* invalidate the INIT_RAM section */
Tom Rini6a5dccc2022-11-16 13:10:41 -05001076 lis r3, (CFG_SYS_INIT_RAM_ADDR & ~31)@h
1077 ori r3, r3, (CFG_SYS_INIT_RAM_ADDR & ~31)@l
1078 li r4, ((CFG_SYS_INIT_RAM_SIZE & ~31) + \
1079 (CFG_SYS_INIT_RAM_ADDR & 31) + 31) / 32
Nick Spenceb89d6fb2008-08-28 14:09:11 -07001080 mtctr r4
Eran Liberty9095d4a2005-07-28 10:08:46 -050010811: icbi r0, r3
1082 dcbi r0, r3
1083 addi r3, r3, 32
1084 bdnz 1b
1085 sync /* Wait for all icbi to complete on bus */
1086 isync
Kumar Galad5d94d62006-02-10 15:40:06 -06001087
1088 /* Unlock the data cache and invalidate it */
1089 mfspr r3, HID0
1090 li r5, HID0_DLOCK|HID0_DCFI
1091 andc r3, r3, r5 /* no invalidate, unlock */
1092 ori r5, r3, HID0_DCFI /* invalidate, unlock */
Nick Spence7c20aef2008-08-28 14:09:25 -07001093 sync
Kumar Galad5d94d62006-02-10 15:40:06 -06001094 mtspr HID0, r5 /* invalidate, unlock */
Kumar Galad5d94d62006-02-10 15:40:06 -06001095 sync
Nick Spence7c20aef2008-08-28 14:09:25 -07001096 mtspr HID0, r3 /* no invalidate, unlock */
Eran Liberty9095d4a2005-07-28 10:08:46 -05001097 blr
Scott Wood2b36fbb2012-12-06 13:33:17 +00001098#endif /* !MINIMAL_SPL */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001099#endif /* CONFIG_SYS_INIT_RAM_LOCK */
Eran Liberty9095d4a2005-07-28 10:08:46 -05001100
Tom Rini364d0022023-01-10 11:19:45 -05001101#ifdef CFG_SYS_FLASHBOOT
Eran Liberty9095d4a2005-07-28 10:08:46 -05001102map_flash_by_law1:
1103 /* When booting from ROM (Flash or EPROM), clear the */
1104 /* Address Mask in OR0 so ROM appears everywhere */
1105 /*----------------------------------------------------*/
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001106 lis r3, (CONFIG_SYS_IMMR)@h /* r3 <= CONFIG_SYS_IMMR */
Jon Loeligerebc72242005-08-01 13:20:47 -05001107 lwz r4, OR0@l(r3)
Eran Liberty9095d4a2005-07-28 10:08:46 -05001108 li r5, 0x7fff /* r5 <= 0x00007FFFF */
Jon Loeligerebc72242005-08-01 13:20:47 -05001109 and r4, r4, r5
Eran Liberty9095d4a2005-07-28 10:08:46 -05001110 stw r4, OR0@l(r3) /* OR0 <= OR0 & 0x00007FFFF */
1111
1112 /* As MPC8349E User's Manual presented, when RCW[BMS] is set to 0,
1113 * system will boot from 0x0000_0100, and the LBLAWBAR0[BASE_ADDR]
1114 * reset value is 0x00000; when RCW[BMS] is set to 1, system will boot
1115 * from 0xFFF0_0100, and the LBLAWBAR0[BASE_ADDR] reset value is
1116 * 0xFF800. From the hard resetting to here, the processor fetched and
1117 * executed the instructions one by one. There is not absolutely
1118 * jumping happened. Laterly, the u-boot code has to do an absolutely
1119 * jumping to tell the CPU instruction fetching component what the
1120 * u-boot TEXT base address is. Because the TEXT base resides in the
1121 * boot ROM memory space, to garantee the code can run smoothly after
1122 * that jumping, we must map in the entire boot ROM by Local Access
1123 * Window. Sometimes, we desire an non-0x00000 or non-0xFF800 starting
1124 * address for boot ROM, such as 0xFE000000. In this case, the default
1125 * LBIU Local Access Widow 0 will not cover this memory space. So, we
1126 * need another window to map in it.
1127 */
Tom Rini6a5dccc2022-11-16 13:10:41 -05001128 lis r4, (CFG_SYS_FLASH_BASE)@h
1129 ori r4, r4, (CFG_SYS_FLASH_BASE)@l
1130 stw r4, LBLAWBAR1(r3) /* LBLAWBAR1 <= CFG_SYS_FLASH_BASE */
Timur Tabi53b46172006-08-22 17:07:00 -05001131
Tom Rini6a5dccc2022-11-16 13:10:41 -05001132 /* Store 0x80000012 + log2(CFG_SYS_FLASH_SIZE) into LBLAWAR1 */
Timur Tabi53b46172006-08-22 17:07:00 -05001133 lis r4, (0x80000012)@h
1134 ori r4, r4, (0x80000012)@l
Tom Rini6a5dccc2022-11-16 13:10:41 -05001135 li r5, CFG_SYS_FLASH_SIZE
Timur Tabi53b46172006-08-22 17:07:00 -050011361: srawi. r5, r5, 1 /* r5 = r5 >> 1 */
1137 addi r4, r4, 1
1138 bne 1b
1139
Eran Liberty9095d4a2005-07-28 10:08:46 -05001140 stw r4, LBLAWAR1(r3) /* LBLAWAR1 <= 8MB Flash Size */
Joakim Tjernlundb168d632010-11-19 14:15:33 +01001141 /* Wait for HW to catch up */
1142 lwz r4, LBLAWAR1(r3)
1143 twi 0,r4,0
1144 isync
Eran Liberty9095d4a2005-07-28 10:08:46 -05001145 blr
1146
1147 /* Though all the LBIU Local Access Windows and LBC Banks will be
1148 * initialized in the C code, we'd better configure boot ROM's
1149 * window 0 and bank 0 correctly at here.
1150 */
1151remap_flash_by_law0:
1152 /* Initialize the BR0 with the boot ROM starting address. */
1153 lwz r4, BR0(r3)
1154 li r5, 0x7FFF
Jon Loeligerebc72242005-08-01 13:20:47 -05001155 and r4, r4, r5
Tom Rini6a5dccc2022-11-16 13:10:41 -05001156 lis r5, (CFG_SYS_FLASH_BASE & 0xFFFF8000)@h
1157 ori r5, r5, (CFG_SYS_FLASH_BASE & 0xFFFF8000)@l
Eran Liberty9095d4a2005-07-28 10:08:46 -05001158 or r5, r5, r4
Tom Rini6a5dccc2022-11-16 13:10:41 -05001159 stw r5, BR0(r3) /* r5 <= (CFG_SYS_FLASH_BASE & 0xFFFF8000) | (BR0 & 0x00007FFF) */
Eran Liberty9095d4a2005-07-28 10:08:46 -05001160
1161 lwz r4, OR0(r3)
Tom Rini6a5dccc2022-11-16 13:10:41 -05001162 lis r5, ~((CFG_SYS_FLASH_SIZE << 4) - 1)
Eran Liberty9095d4a2005-07-28 10:08:46 -05001163 or r4, r4, r5
Timur Tabi53b46172006-08-22 17:07:00 -05001164 stw r4, OR0(r3)
Eran Liberty9095d4a2005-07-28 10:08:46 -05001165
Tom Rini6a5dccc2022-11-16 13:10:41 -05001166 lis r4, (CFG_SYS_FLASH_BASE)@h
1167 ori r4, r4, (CFG_SYS_FLASH_BASE)@l
1168 stw r4, LBLAWBAR0(r3) /* LBLAWBAR0 <= CFG_SYS_FLASH_BASE */
Eran Liberty9095d4a2005-07-28 10:08:46 -05001169
Tom Rini6a5dccc2022-11-16 13:10:41 -05001170 /* Store 0x80000012 + log2(CFG_SYS_FLASH_SIZE) into LBLAWAR0 */
Timur Tabi53b46172006-08-22 17:07:00 -05001171 lis r4, (0x80000012)@h
1172 ori r4, r4, (0x80000012)@l
Tom Rini6a5dccc2022-11-16 13:10:41 -05001173 li r5, CFG_SYS_FLASH_SIZE
Timur Tabi53b46172006-08-22 17:07:00 -050011741: srawi. r5, r5, 1 /* r5 = r5 >> 1 */
1175 addi r4, r4, 1
1176 bne 1b
1177 stw r4, LBLAWAR0(r3) /* LBLAWAR0 <= Flash Size */
1178
Eran Liberty9095d4a2005-07-28 10:08:46 -05001179
1180 xor r4, r4, r4
1181 stw r4, LBLAWBAR1(r3)
1182 stw r4, LBLAWAR1(r3) /* Off LBIU LAW1 */
Joakim Tjernlundb168d632010-11-19 14:15:33 +01001183 /* Wait for HW to catch up */
1184 lwz r4, LBLAWAR1(r3)
1185 twi 0,r4,0
1186 isync
Eran Liberty9095d4a2005-07-28 10:08:46 -05001187 blr
Tom Rini364d0022023-01-10 11:19:45 -05001188#endif /* CFG_SYS_FLASHBOOT */