MPC5646C_HWInit.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. *
  3. * FILE : MPC564xBC_HWInit.c
  4. *
  5. * DESCRIPTION:
  6. * This file contains all MPC564xBC derivative needed initializations,
  7. * and all initializations for the MPC564xBC boards which are supported.
  8. * This includes setting up the External Bus Interface to allow access
  9. * to memory on the external bus, and ensuring there is a valid entry in
  10. * the MMU for the external memory access.
  11. */
  12. /*----------------------------------------------------------------------------*/
  13. /* Includes */
  14. /*----------------------------------------------------------------------------*/
  15. #include "MPC5646C.h" /* MPC55xx platform development header */
  16. #include "MPC5646C_HWInit.h"
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /*******************************************************/
  21. /* MPC564xBC derivative specific hardware initialization */
  22. /*******************************************************/
  23. /*----------------------------------------------------------------------------*/
  24. /* Function declarations */
  25. /*----------------------------------------------------------------------------*/
  26. /* All these functions must be located in the initial 4KB memory window (.init)
  27. and implemented "nofralloc" so as to not use the stack */
  28. /* Memory initialization */
  29. __declspec(section ".init") __asm void INIT_Derivative(void);
  30. /* Write one MMU Table Entry */
  31. __declspec(section ".init") __asm void WriteMMUTableEntry( void );
  32. /* Initialize the needed MMU Table entries */
  33. __declspec(section ".init") __asm void __initMMU(void);
  34. /* Configures the SWT */
  35. __declspec(section ".init") __asm void cfg_WATCHDOG(void);
  36. /* Configures the platform flash register. */
  37. __declspec(section ".init") __asm void __FlashConfig(void);
  38. /* This macro allows to use C defined address with the inline assembler */
  39. #define MAKE_HLI_COMPATIBLE(hli_name, c_expr) enum { hli_name=/*lint -e30*/((int)(c_expr)) };
  40. /*----------------------------------------------------------------------------*/
  41. /* Function implementations */
  42. /*----------------------------------------------------------------------------*/
  43. /* Symbol L2SRAM_LOCATION is defined in the application linker command file (.lcf)
  44. It is defined to the start of the L2SRAM of the MPC564xBC.
  45. */
  46. /*lint -esym(752, L2SRAM_LOCATION) */
  47. extern long L2SRAM_LOCATION;
  48. /* Symbol L2SRAM_CNT is defined in the application linker command file (.lcf)
  49. It represents the how many writes with stmw,128 bytes each, are needed to cover
  50. the whole L2SRAM.
  51. */
  52. extern long L2SRAM_CNT;
  53. /**************************************************************************/
  54. /* FUNCTION : cfg_WATCHDOG */
  55. /* PURPOSE : This function configures the WATCHDOG */
  56. /* SEQUENCE: */
  57. /* - Disable watchdog, */
  58. /**************************************************************************/
  59. MAKE_HLI_COMPATIBLE(SR_WSC_1, 50464)
  60. MAKE_HLI_COMPATIBLE(SR_WSC_2, 55592)
  61. MAKE_HLI_COMPATIBLE(CR_VALUE, 0x8000010A)
  62. /** Address of the SWT SR */
  63. MAKE_HLI_COMPATIBLE(SWT_SR, &SWT.SR.R)
  64. /** Address of the SWT CR */
  65. MAKE_HLI_COMPATIBLE(SWT_CR, &SWT.CR.R)
  66. __asm void cfg_WATCHDOG(void)
  67. {
  68. nofralloc
  69. /* Clear the soft lock bit SWT_CR.SLKSWT_CR: */
  70. /* SR --> 0x0000c520 */
  71. lis r4, 0
  72. ori r4, r4, SR_WSC_1@l
  73. lis r3, SWT_SR@ha
  74. stw r4, SWT_SR@l(r3)
  75. /* SR --> 0x0000d928 */
  76. lis r4, 0
  77. ori r4, r4, SR_WSC_2@l
  78. stw r4, SWT_SR@l(r3)
  79. /* Disable watchdog, SWT.CR.WEN = 0*/
  80. lis r4, CR_VALUE@h
  81. ori r4, r4, CR_VALUE@l
  82. lis r3, SWT_CR@ha
  83. stw r4, SWT_CR@l(r3)
  84. blr
  85. }
  86. /** PFCR0 */
  87. MAKE_HLI_COMPATIBLE(PFCR0, &CFLASH_0.PFCR0.R)
  88. //bit 0 -> bit 31
  89. //B02_APC = 0b00000, Accesses may be initiated on consecutive (back-to-back) cycles
  90. //B02_WWSC = 0b00001, Write: 1 additional wait-state is added
  91. //B02_RWSC = 0b00101, >100 MHz - 120 MHz, APC =RWSC=5 (default reset)
  92. //B02_RWWC[2:1] = 0b11, Generate a bus stall for a read while write/erase, disable the stall notification interrupt,disable the abort + abort notification interrupt (reset default)
  93. //B02_P1_BCFG = 0b11,The buffers are partitioned into two groups with buffers 0,1,2 allocated for instruction fetches and buffer 3 for data accesses.(reset default)
  94. //B02_P1_DPFE = 0b0 (reset default)
  95. //B02_P1_IPFE = 0b1 (reset default)
  96. //B02_P1_PFLM = 0b10 (reset default)
  97. //B02_P1_BFE = 0b1 (reset default)
  98. //B02_RWWC0 = 0b1 (reset default)
  99. //B02_P0_BCFG = 0b11 (reset default)
  100. //B02_P0_DPFE = 0b0 (reset default)
  101. //B02_P0_IPFE = 0b1 (reset default)
  102. //B02_P0_PFLM = 0b10 (reset default)
  103. //B02_P0_BFE = 0b1 (reset default)
  104. MAKE_HLI_COMPATIBLE(FLASH_DATA0, 0x4BEDED)
  105. // reset value 0x294BEDED
  106. /** PFCR1 */
  107. MAKE_HLI_COMPATIBLE(PFCR1, &CFLASH_0.PFCR1.R)
  108. //bit 0 -> bit 31
  109. //B1_APC = 0b00000 Accesses may be initiated on consecutive (back-to-back) cycles
  110. //B1_WWSC = 0b00001 write: 1 additional wait-state is added
  111. //B1_RWSC = 0b00101 >100 MHz - 120 MHz, APC =RWSC=5
  112. //B1_RWWC2:1= 0b11 Generate a bus stall for a read while write/erase, disable the stall notification interrupt,disable the abort + abort notification interrupt (reset default)
  113. //0b000000
  114. //B1_P1_BFE = 0b1 Bank1, Port 1 Buffer Enable (reset default)
  115. //B1_RWWC0 = 0b1 Generate a bus stall for a read while write/erase, disable the stall notification interrupt,disable the abort + abort notification interrupt (reset default)
  116. //0b000000
  117. //B1_P0_BFE = 0b1 Bank1, Port 0 Buffer Enable (reset default)
  118. MAKE_HLI_COMPATIBLE(FLASH_DATA1, 0x4B8181)
  119. // reset value 0x6b5b8181
  120. MAKE_HLI_COMPATIBLE(__FlashConfigInstrCount, 16)
  121. __asm void __FlashConfig(void)
  122. {
  123. nofralloc
  124. /* configure code flash PFCR0 */
  125. lis r4, PFCR0@h
  126. ori r4, r4, PFCR0@l
  127. lis r3, FLASH_DATA0@h
  128. ori r3, r3, FLASH_DATA0@l
  129. stw r3, 0(r4) /* stw r3,(0)r4 machine code: writes r3 contents to addr in r4 */
  130. mbar 0 /* msync machine code: ensure prior store completed */
  131. /* configure code flash PFCR1 */
  132. lis r4, PFCR1@h
  133. ori r4, r4, PFCR1@l
  134. lis r3, FLASH_DATA1@h
  135. ori r3, r3, FLASH_DATA1@l
  136. stw r3, 0(r4) /* stw r3,(0)r4 machine code: writes r3 contents to addr in r4 */
  137. mbar 0 /* mbar machine code: ensure prior store completed */
  138. blr
  139. nop
  140. nop
  141. nop
  142. }
  143. /**
  144. * TLB_Entry_15: default page for running the initialization code, TS=1, 4GB, cache inhibited,
  145. * not guarded, big endian.
  146. */
  147. MAKE_HLI_COMPATIBLE(TLB_Entry_15_MAS0, MAS0_VALUE(15))
  148. MAKE_HLI_COMPATIBLE(TLB_Entry_15_MAS1, MAS1_VALUE(V_VALID, IPROT_NOTPROTECTED, TID_GLOBAL, TS_ON, TSIZE_4GB))
  149. MAKE_HLI_COMPATIBLE(TLB_Entry_15_MAS1_INVALID, MAS1_VALUE(V_INVALID, IPROT_NOTPROTECTED, TID_GLOBAL, TS_ON, TSIZE_4GB))
  150. #if __option(vle)
  151. MAKE_HLI_COMPATIBLE(TLB_Entry_15_MAS2, MAS2_VALUE(0, VLE_MODE, WRITE_BACK, CACHE_INHIBIT, MEM_COHERENCE_NREQ, NOT_GUARDED, BIG_ENDIAN))
  152. #else
  153. MAKE_HLI_COMPATIBLE(TLB_Entry_15_MAS2, MAS2_VALUE(0, BOOK_E_MODE, WRITE_BACK, CACHE_INHIBIT, MEM_COHERENCE_NREQ, NOT_GUARDED, BIG_ENDIAN))
  154. #endif
  155. MAKE_HLI_COMPATIBLE(TLB_Entry_15_MAS3, MAS3_VALUE(0, READ_WRITE_EXECUTE))
  156. __asm void INIT_Derivative(void)
  157. {
  158. nofralloc
  159. /* Create initialization memory space required in order to create
  160. TLB entries for FLASH or RAM. */
  161. lis r3, TLB_Entry_15_MAS0@h
  162. ori r3, r3, TLB_Entry_15_MAS0@l
  163. lis r4, TLB_Entry_15_MAS1@h
  164. ori r4, r4, TLB_Entry_15_MAS1@l
  165. xor r5, r5, r5
  166. mr r6, r5
  167. ori r5, r5, TLB_Entry_15_MAS2@l
  168. ori r6, r6, TLB_Entry_15_MAS3@l
  169. mtspr 624, r3
  170. mtspr 625, r4
  171. mtspr 626, r5
  172. mtspr 627, r6
  173. /* prefetch instruction to this point. */
  174. isync
  175. tlbwe
  176. /* make sure isntructions and data are fetched from the new context. */
  177. msync
  178. isync
  179. /* force this TLB entry to be used for translation */
  180. mfmsr r10
  181. /* save state */
  182. mr r3, r10
  183. /* set IS=1, DS=1 */
  184. ori r3, r3, 0x20
  185. isync
  186. /* mtmsr does execution synchronization.*/
  187. mtmsr r3
  188. /* Required after changing MSR.IS, and MSR.DS so the prefetched instructions
  189. will be discarded and all subsequent instructions will use the TLB 15 context.
  190. */
  191. isync
  192. /* create device specific MMU entries */
  193. mflr r26
  194. bl __initMMU
  195. mtlr r26
  196. /* Disable the software watch dog. */
  197. mflr r26
  198. bl cfg_WATCHDOG
  199. mtlr r26
  200. /* MPC564xBC L2SRAM initialization code */
  201. #if defined(ROM_VERSION)
  202. /* SRAM initialization code*/
  203. lis r11,L2SRAM_LOCATION@h
  204. ori r11,r11,L2SRAM_LOCATION@l
  205. /* Loops to cover L2SRAM, stmw allows 128 bytes (32 GPRS x 4 bytes) writes */
  206. lis r12,L2SRAM_CNT@h
  207. ori r12,r12,L2SRAM_CNT@l
  208. mtctr r12
  209. init_l2sram_loop:
  210. stmw r0, 0(r11) /* Write 32 GPRs to SRAM*/
  211. addi r11,r11,128 /* Inc the ram ptr; 32 GPRs * 4 bytes = 128B */
  212. bdnz init_l2sram_loop /* Loop for 48k of SRAM */
  213. // init platform flash registers from RAM - please see reference manual
  214. lis r3,__FlashConfig@h
  215. ori r3,r3,__FlashConfig@l
  216. lis r4, __FlashConfigInstrCount@h
  217. ori r4, r4, __FlashConfigInstrCount@l
  218. mtctr r4
  219. lis r5, L2SRAM_LOCATION@h
  220. mflr r26 /* save LR */
  221. mtlr r5 /* set LR <- __FlashConfig*/
  222. /* copy function code to RAM */
  223. copy:
  224. lwz r6, 0(r3)
  225. stw r6, 0(r5)
  226. addi r3, r3, 4
  227. addi r5, r5, 4
  228. bdnz copy /* decrement CTR */
  229. blrl /* goto copyied __FlashConfig in RAM */
  230. mtlr r26 /* restore LR */
  231. #endif
  232. /* restore msr */
  233. mtmsr r10
  234. /* execute all instructions and discard the prefetched instructions */
  235. isync
  236. /* invalidated initialization TLB entry 15 */
  237. lis r3, TLB_Entry_15_MAS0@h
  238. ori r3, r3, TLB_Entry_15_MAS0@l
  239. lis r4, TLB_Entry_15_MAS1_INVALID@h
  240. ori r4, r4, TLB_Entry_15_MAS1_INVALID@l
  241. mtspr 624, r3
  242. mtspr 625, r4
  243. tlbwe
  244. /* make sure isntructions and data are fetched from the new context. */
  245. isync
  246. msync
  247. blr
  248. }
  249. /* Initialize the needed MMU Table entries */
  250. /* FLASH: TLB entry 0, Base address = 0x0000_0000, 16 MB, protected, guarded, cache on, big-endian, all access */
  251. MAKE_HLI_COMPATIBLE(TLB_Entry_0_MAS0, MAS0_VALUE(0))
  252. MAKE_HLI_COMPATIBLE(TLB_Entry_0_MAS1, MAS1_VALUE(V_VALID, IPROT_PROTECTED, TID_GLOBAL, TS_OFF, TSIZE_16MB))
  253. #if __option(vle)
  254. MAKE_HLI_COMPATIBLE(TLB_Entry_0_MAS2, MAS2_VALUE(0, VLE_MODE, WRITE_BACK, CACHEABLE, MEM_COHERENCE_NREQ, GUARDED, BIG_ENDIAN))
  255. #else
  256. MAKE_HLI_COMPATIBLE(TLB_Entry_0_MAS2, MAS2_VALUE(0, BOOK_E_MODE, WRITE_BACK, CACHEABLE, MEM_COHERENCE_NREQ, GUARDED, BIG_ENDIAN))
  257. #endif
  258. MAKE_HLI_COMPATIBLE(TLB_Entry_0_MAS3, MAS3_VALUE(0, READ_WRITE_EXECUTE))
  259. /* SRAM: TLB entry 1, Base address = 0x4000_0000, 256K, not protected, not guarded, cache off, big-endian, all access */
  260. MAKE_HLI_COMPATIBLE(TLB_Entry_1_MAS0, MAS0_VALUE(1))
  261. MAKE_HLI_COMPATIBLE(TLB_Entry_1_MAS1, MAS1_VALUE(V_VALID, IPROT_NOTPROTECTED, TID_GLOBAL, TS_OFF, TSIZE_256KB))
  262. #if __option(vle)
  263. MAKE_HLI_COMPATIBLE(TLB_Entry_1_MAS2, MAS2_VALUE(0x40000, VLE_MODE, WRITE_BACK, CACHE_INHIBIT, MEM_COHERENCE_NREQ, NOT_GUARDED, BIG_ENDIAN))
  264. #else
  265. MAKE_HLI_COMPATIBLE(TLB_Entry_1_MAS2, MAS2_VALUE(0x40000, BOOK_E_MODE, WRITE_BACK, CACHE_INHIBIT, MEM_COHERENCE_NREQ, NOT_GUARDED, BIG_ENDIAN))
  266. #endif
  267. MAKE_HLI_COMPATIBLE(TLB_Entry_1_MAS3, MAS3_VALUE(0x40000, READ_WRITE_EXECUTE))
  268. /* Peripheral bridge 1: TLB entry 2 Base address = 0xC000_0000, 64M, not protected, not guarded, cache off, big-endian, all access */
  269. MAKE_HLI_COMPATIBLE(TLB_Entry_2_MAS0, MAS0_VALUE(2))
  270. MAKE_HLI_COMPATIBLE(TLB_Entry_2_MAS1, MAS1_VALUE(V_VALID, IPROT_NOTPROTECTED, TID_GLOBAL, TS_OFF, TSIZE_64MB))
  271. #if __option(vle)
  272. MAKE_HLI_COMPATIBLE(TLB_Entry_2_MAS2, MAS2_VALUE(0xC0000, VLE_MODE, WRITE_BACK, CACHE_INHIBIT, MEM_COHERENCE_NREQ, NOT_GUARDED, BIG_ENDIAN))
  273. #else
  274. MAKE_HLI_COMPATIBLE(TLB_Entry_2_MAS2, MAS2_VALUE(0xC0000, BOOK_E_MODE, WRITE_BACK, CACHE_INHIBIT, MEM_COHERENCE_NREQ, NOT_GUARDED, BIG_ENDIAN))
  275. #endif
  276. MAKE_HLI_COMPATIBLE(TLB_Entry_2_MAS3, MAS3_VALUE(0xC0000, READ_WRITE_EXECUTE))
  277. /* Peripheral bridge 0 + BAM: TLB entry 3 Base address = 0xFFE0_0000, 2M, not protected, not guarded, cache off, big-endian, all access */
  278. MAKE_HLI_COMPATIBLE(TLB_Entry_3_MAS0, MAS0_VALUE(3))
  279. MAKE_HLI_COMPATIBLE(TLB_Entry_3_MAS1, MAS1_VALUE(V_VALID, IPROT_NOTPROTECTED, TID_GLOBAL, TS_OFF, TSIZE_2MB))
  280. #if __option(vle)
  281. MAKE_HLI_COMPATIBLE(TLB_Entry_3_MAS2, MAS2_VALUE(0xFFE00, VLE_MODE, WRITE_BACK, CACHE_INHIBIT, MEM_COHERENCE_NREQ, NOT_GUARDED, BIG_ENDIAN))
  282. #else
  283. MAKE_HLI_COMPATIBLE(TLB_Entry_3_MAS2, MAS2_VALUE(0xFFE00, BOOK_E_MODE, WRITE_BACK, CACHE_INHIBIT, MEM_COHERENCE_NREQ, NOT_GUARDED, BIG_ENDIAN))
  284. #endif
  285. MAKE_HLI_COMPATIBLE(TLB_Entry_3_MAS3, MAS3_VALUE(0xFFE00, READ_WRITE_EXECUTE))
  286. __asm void __initMMU(void)
  287. {
  288. nofralloc
  289. mflr r27
  290. /* Configure FLASH page on TLB entry 0 */
  291. lis r3, TLB_Entry_0_MAS0@h
  292. ori r3, r3, TLB_Entry_0_MAS0@l
  293. lis r4, TLB_Entry_0_MAS1@h
  294. ori r4, r4, TLB_Entry_0_MAS1@l
  295. lis r5, TLB_Entry_0_MAS2@h
  296. ori r5, r5, TLB_Entry_0_MAS2@l
  297. lis r6, TLB_Entry_0_MAS3@h
  298. ori r6, r6, TLB_Entry_0_MAS3@l
  299. bl WriteMMUTableEntry
  300. /* Configure RAM page on TLB entry 1 */
  301. lis r3, TLB_Entry_1_MAS0@h
  302. ori r3, r3, TLB_Entry_1_MAS0@l
  303. lis r4, TLB_Entry_1_MAS1@h
  304. ori r4, r4, TLB_Entry_1_MAS1@l
  305. lis r5, TLB_Entry_1_MAS2@h
  306. ori r5, r5, TLB_Entry_1_MAS2@l
  307. lis r6, TLB_Entry_1_MAS3@h
  308. ori r6, r6, TLB_Entry_1_MAS3@l
  309. bl WriteMMUTableEntry
  310. /* Peripheral bridge 0 on TLB entry 2 */
  311. lis r3, TLB_Entry_2_MAS0@h
  312. ori r3, r3, TLB_Entry_2_MAS0@l
  313. lis r4, TLB_Entry_2_MAS1@h
  314. ori r4, r4, TLB_Entry_2_MAS1@l
  315. lis r5, TLB_Entry_2_MAS2@h
  316. ori r5, r5, TLB_Entry_2_MAS2@l
  317. lis r6, TLB_Entry_2_MAS3@h
  318. ori r6, r6, TLB_Entry_2_MAS3@l
  319. bl WriteMMUTableEntry
  320. /* Peripheral bridge 0 on TLB entry 3 */
  321. lis r3, TLB_Entry_3_MAS0@h
  322. ori r3, r3, TLB_Entry_3_MAS0@l
  323. lis r4, TLB_Entry_3_MAS1@h
  324. ori r4, r4, TLB_Entry_3_MAS1@l
  325. lis r5, TLB_Entry_3_MAS2@h
  326. ori r5, r5, TLB_Entry_3_MAS2@l
  327. lis r6, TLB_Entry_3_MAS3@h
  328. ori r6, r6, TLB_Entry_3_MAS3@l
  329. bl WriteMMUTableEntry
  330. mtlr r27
  331. blr
  332. }
  333. /* Write one MMU Table Entry: */
  334. /* r3, r4, r5 and r6 must hold */
  335. /* the values of MAS0, MAS1, MAS2 and MAS3 */
  336. __asm void WriteMMUTableEntry( void )
  337. {
  338. nofralloc
  339. /* Write MMU Assist Register 0 (MAS0); SPR 624 */
  340. mtspr 624, r3
  341. /* Write MMU Assist Register 1 (MAS1); SPR 625 */
  342. mtspr 625, r4
  343. /* Write MMU Assist Register 2 (MAS2); SPR 626 */
  344. mtspr 626, r5
  345. /* Write MMU Assist Register 3 (MAS3); SPR 627 */
  346. mtspr 627, r6
  347. /* Write the table entry */
  348. /* All instruction will complete here in current context. */
  349. //isync
  350. tlbwe
  351. /* synchronize instruction fetches and data accesses in respect
  352. with the new created TLB entry. */
  353. msync
  354. isync
  355. blr
  356. }
  357. #ifdef __cplusplus
  358. }
  359. #endif