MPC5674F Software Initialization
MPC5674F Software Initialization
Rev. 0, 04/2010
Application Note
1 Introduction Contents
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
This application note describes a recommended software 2 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3 Startup Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
initialization procedure for the MPC5674F 32-bit Power 3.1 Reset Configuration and Watchdog. . . . . . . . . . . . . 2
Architecture® automotive microcontroller. This covers 3.2 Instruction Cache and PLL . . . . . . . . . . . . . . . . . . . 4
the Power Architecture core, memory management unit 3.3 C Runtime Register Setup. . . . . . . . . . . . . . . . . . . . 7
3.4 SRAM Initialization . . . . . . . . . . . . . . . . . . . . . . . . . 8
(MMU), clock frequency (PLL), watchdog timers, flash 3.5 Copy Initialized Data . . . . . . . . . . . . . . . . . . . . . . . . 8
memory controller, and internal static RAM. 3.6 C Code Execution . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4 MCU Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Recommended configuration settings for these modules 4.1 Flash Optimization. . . . . . . . . . . . . . . . . . . . . . . . . 10
will be given for the purpose of optimizing system 4.2 Data Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
performance. 4.3 Branch Target Buffer . . . . . . . . . . . . . . . . . . . . . . . 11
4.4 Crossbar Switch . . . . . . . . . . . . . . . . . . . . . . . . . . 12
5 Conclusion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2 Overview
There are several options to consider when discussing the structure of our embedded software application.
The first is how it will execute. The application can be stored in internal flash memory or it can be
downloaded from an external device such as a debugger or via a serial communications link. This affects
certain steps in the initialization process and where applicable, this will be noted. Another option is
choosing Variable Length Encoding instructions (VLE) vs. PowerPC BookE Instructions. The assembly
code examples shown in this application note will be using VLE mnemonics and syntax but can easily be
translated into the BookE variant.
3 Startup Code
The first part of our initialization procedure executes from the reset vector or program entry point and
performs the minimal setup needed to prepare for C code execution later on. Another goal of this stage is
to optimize the startup procedure’s execution time. This involves taking certain initialization steps in a
particular order:
1. Reset Configuration and Watchdog
2. Enable Cache
3. Program PLL
4. Initialize SRAM
5. Initialize C Runtime Environment
Block Address
0 0x0000_0000
1 0x0000_4000
2 0x0001_0000
3 0x0001_C000
4 0x0002_0000
5 0x0003_0000
The RCHW is a collection of control bits that specify a minimal MCU configuration after reset. If a valid
RCHW is not found, the BAM will attempt a serial boot. Here is the format for the RCHW:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
The RCHW occupies the most significant 16 bits of the first 32-bit internal memory word at the boot
location. The word immediately following this contains the boot vector address. After applying the
RCHW, the BAM will branch to this address. During software initialization we can reserve space for both
of these 32-bit locations in the linker directive file as follows:
MEMORY
{
flash_rcw : org = FLASH_BASE_ADDR, len = 0x8
…
}
SECTIONS
{
.rcw : {} > flash_rcw
…
}
In the initialization code file, these two locations is generated with a valid RCHW encoding and the start
address symbol for code entry point.
.section .rcw
.LONG 0x015A0000 # RCHW
.LONG _start # Code entry point
In the above example, the core and software watchdog timers are both disabled. These can both operate
independently, but it is typical to use just one or the other in an application. When debugging, the RCHW
is not applied as the BAM does not execute, so it is up to the debugger to disable these timers so that they
do not interfere with application debug sessions. This is necessary for the core watchdog as software
cannot be disabled it once it is enabled. The software watchdog starts out in an unlocked state, so the
control register is still writable. If desired, the enable bit can be cleared, to prevent watchdog operation
during a debug session if the debug tool does not handle this with its own configuration scripts.
NOTE
If either watchdog timer is enabled there may be points within the
initialization procedure that require watchdog service depending on the
timeout period of the watchdog.
e_lis r3,0x1001
mtspr mas0,r3
e_lis r4,0xC000
e_or2i r4,0x0600
mtspr mas1,r4
e_lis r5,0x0000
e_or2i r5,0x0028
mtspr mas2,r5
e_lis r6,0x0000
e_or2i r6,0x003f
mtspr mas3,r6
Note that in this example, tlbwe is preceded by msync and followed by isync. These synchronization steps
are taken when we are executing code from the region being modified.
#--------------------------------------------#
# Invalidate Instruction Cache - Set ICINV #
# bit in L1CSR1 Register #
#--------------------------------------------#
e_lis r5, 0x0000
e_or2i r5, 0x0002
mtspr l1csr1,r5
#--------------------------------------------#
# Mask out ICINV and ICABT to see if #
# invalidation is complete (i.e. ICINV=0, #
# ICABT=0) #
#--------------------------------------------#
label_ICINV_check:
#--------------------------------------------#
# Load Registers with Masks: #
# Load ICINV mask into R4 #
# Load ICABT mask into R6 #
# Load ICABT clear mask into R7 #
#--------------------------------------------#
e_lis r4, 0x0000
e_or2i r4, 0x0002
e_lis r6, 0x0000
e_or2i r6, 0x0004
e_lis r7, 0xFFFF
e_or2i r7, 0xFFFB
CHECK_ICINV:
#--------------------------------------------#
# Read L1CSR1 register, store in r3 #
#--------------------------------------------#
mfspr r3, l1csr1
#--------------------------------------------#
# check for an ABORT of the cache invalidate #
# operation #
#--------------------------------------------#
se_and. r6, r3
e_beq NO_ABORT
#--------------------------------------------#
# If abort detected, clear ICABT bit and #
# re-run invalidation #
#--------------------------------------------#
se_and. r7, r3
mtspr l1csr1, r10
se_b cfg_ICACHE
NO_ABORT:
#--------------------------------------------#
# Check that invalidation has completed - #
# (ICINV=0). Branch if invalidation not #
# complete. #
#--------------------------------------------#
se_and. r4, r3
e_bne CHECK_ICINV
#--------------------------------------------#
# Enable cache the ICache by performing a #
# read/modify/write of the ICE bit in the #
# L1CSR1 register #
#--------------------------------------------#
mfspr r5, l1csr1
e_or2is r5, 0x0000
e_or2i r5, 0x0001 # Store L1CSR1 value to R5 (ICE=1)
se_isync
msync
mtspr l1csr1, r5 # Write R5 to L1CSR1 register
se_blr
The following example sets up the PLL to produce a 264MHz system clock assuming a 40MHz reference
crystal.
# ESYNCR1
e_lis r3, 0xC3F8
e_lis r4, 0x0004 # EPREDIV
# ESYNCR2
e_lis r4, 0x0000
e_or2i r4, 0x0001 # ERFD
e_stw r4, 12(r3)
wait_for_lock:
e_lwz r5, 4(r3) # load SYNSR
e_andi. r5, r5, 0x8
beq wait_for_lock
Now that we’ve locked on our new clock rate, we can enable caching for the flash region.
# Enable caching of this region
# MAS0 : ESEL=1
# MAS1 : TSIZ=4Mbytes
# MAS2 : EPN=0x000000020, VLE=1, W=0, I=0, M=0, G=0, E=big
# MAS3 : RPN=0x000000000, PERMIS=all
e_lis r3,0x1001
mtspr mas0,r3
e_lis r4,0xC000
e_or2i r4,0x0600
mtspr mas1,r4
e_lis r5,0x0000
e_or2i r5,0x0020
mtspr mas2,r5
e_lis r6,0x0000
e_or2i r6,0x003f
mtspr mas3,r6
As noted in the comments above, these values are defined in the linker command file for our project.
__DATA_SRAM_ADDR = ADDR(.data);
__SDATA_SRAM_ADDR = ADDR(.sdata);
__DATA_SIZE = SIZEOF(.data);
__SDATA_SIZE = SIZEOF(.sdata);
__DATA_ROM_ADDR = ADDR(.ROM.data);
__SDATA_ROM_ADDR = ADDR(.ROM.sdata);
The values in the internal flash boot case will be used to copy initialized data from flash to SRAM, but
first the SRAM must be initialized.
e_lis r10, __DATA_ROM_ADDR@h # Load address of first SRAM load into R10
e_or2i r10, __DATA_ROM_ADDR@l # Load lower address of SRAM load into R10
e_subi r10,r10, 1 # Decrement address
DATACPYLOOP:
e_lbzu r4, 1(r10) # Load data byte at R10 into R4
e_stbu r4, 1(r5) # Store R4 data byte into SRAM at R5
e_bdnz DATACPYLOOP # Branch if more bytes to load from ROM
e_lis r10, __SDATA_ROM_ADDR@h # Load address of first SRAM load into R10
e_or2i r10, __SDATA_ROM_ADDR@l # Load lower address of SRAM load into R10
e_subi r10,r10, 1 # Decrement address
4 MCU Optimization
In this section, the following areas for potential optimization will be discussed:
• Wait states, prefetch, and BIU settings for the flash controller
• Data Cache
• Branch Target Buffer
• Crossbar Switch
Since in this example we are executing from flash memory, we need to load instructions to perform the
update of BIUCR into SRAM and then execute from there temporarily.
If desired, SIU_SYSDIV[IPCLKDIV] can also be changed here to affect the platform/peripheral
frequency at which the flash array operates. This should be done before changing BIUCR settings. The
resulting flash clock rate should be checked against the MPC5674F data sheet to determine appropriate
BIUCR values. Here, the example assumes the default divider of 2, so the system clock is 264MHz and
the platform clock is 132MHz.
SRAMLOAD:
#--------------------------------------------#
# Load BIUCR write instruction into R8, R9 & #
# R10 #
#--------------------------------------------#
e_lis r8, 0x54E6
#--------------------------------------------#
# Load RAM address into R11 #
#--------------------------------------------#
e_lis r11, _BIUCR_RAM_ADDR@h
e_add16i r11,r11, _BIUCR_RAM_ADDR@l
#--------------------------------------------#
# Store Instructions in RAM, then branch and #
# execute instructions to setup BIUCR #
#--------------------------------------------#
e_stw r8, 0x0(r11);
e_stw r9, 0x4(r11);
e_stw r10, 0x8(r11);
mtlr r11
se_blrl
NOTE
These settings are currently preliminary and subject to change pending
characterization of the device.
#--------------------------------------------#
# Flush and Enable BTB - Set BBFI and BPEN #
#--------------------------------------------#
e_lis r3, 0x0
e_or2i r3, 0x0201
mtspr 1013, r3
NOTE
If the application modifies instruction code in memory after this
initialization procedure, the Branch Target Buffer may need to be flushed
and re-initialized as it may contain branch prediction for the code that
previously existed at the modified locations.
5 Conclusion
This application note has presented some specific recommendations for initializing this device and
optimizing some of the settings from their reset defaults. This is a starting point only. Other areas to look
at include compiler optimization and efficient use of system resources such as DMA and cache. Consult
the MPC5674F reference manual for additional information.
Web Support:
http://www.freescale.com/support Freescale Semiconductor reserves the right to make changes without further notice to
any products herein. Freescale Semiconductor makes no warranty, representation or
USA/Europe or Locations Not Listed: guarantee regarding the suitability of its products for any particular purpose, nor does
Freescale Semiconductor, Inc. Freescale Semiconductor assume any liability arising out of the application or use of any
Technical Information Center, EL516 product or circuit, and specifically disclaims any and all liability, including without
2100 East Elliot Road
Tempe, Arizona 85284 limitation consequential or incidental damages. “Typical” parameters that may be
+1-800-521-6274 or +1-480-768-2130 provided in Freescale Semiconductor data sheets and/or specifications can and do vary
www.freescale.com/support in different applications and actual performance may vary over time. All operating
parameters, including “Typicals”, must be validated for each customer application by
Europe, Middle East, and Africa: customer’s technical experts. Freescale Semiconductor does not convey any license
Freescale Halbleiter Deutschland GmbH under its patent rights nor the rights of others. Freescale Semiconductor products are
Technical Information Center not designed, intended, or authorized for use as components in systems intended for
Schatzbogen 7 surgical implant into the body, or other applications intended to support or sustain life,
81829 Muenchen, Germany
+44 1296 380 456 (English) or for any other application in which the failure of the Freescale Semiconductor product
+46 8 52200080 (English) could create a situation where personal injury or death may occur. Should Buyer
+49 89 92103 559 (German) purchase or use Freescale Semiconductor products for any such unintended or
+33 1 69 35 48 48 (French) unauthorized application, Buyer shall indemnify and hold Freescale Semiconductor and
www.freescale.com/support its officers, employees, subsidiaries, affiliates, and distributors harmless against all
claims, costs, damages, and expenses, and reasonable attorney fees arising out of,
Japan: directly or indirectly, any claim of personal injury or death associated with such
Freescale Semiconductor Japan Ltd. unintended or unauthorized use, even if such claim alleges that Freescale
Headquarters
ARCO Tower 15F Semiconductor was negligent regarding the design or manufacture of the part.
1-8-1, Shimo-Meguro, Meguro-ku,
Tokyo 153-0064 RoHS-compliant and/or Pb-free versions of Freescale products have the functionality
Japan and electrical characteristics as their non-RoHS-compliant and/or non-Pb-free
0120 191014 or +81 3 5437 9125
[email protected] counterparts. For further information, see http://www.freescale.com or contact your
Freescale sales representative.
Asia/Pacific:
Freescale Semiconductor China Ltd. For information on Freescale’s Environmental Products program, go to
Exchange Building 23F
No. 118 Jianguo Road http://www.freescale.com/epp.
Chaoyang District
Beijing 100022 Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc.
China All other product or service names are the property of their respective owners.
+86 10 5879 8000
[email protected] © Freescale Semiconductor, Inc. 2010. All rights reserved.