@@ -560,70 +560,116 @@ flash_tlb:
560560 #endif
561561#endif /* ENABLE_DDR */
562562
563+ /* =========================================================================
564+ * CPC SRAM Initialization
565+ * Order: 1) CPC invalidate, 2) CPCSRCR config, 3) LAW, 4) TLB, 5) CPC enable
566+ * Note: TLB must be created BEFORE CPC enable (original working sequence)
567+ * ========================================================================= */
568+ #if defined(ENABLE_L2_CACHE) && defined(L2SRAM_ADDR) && (defined(CORE_E5500) || defined(CORE_E6500))
569+ cpc_setup_sram:
570+ /* T2080RM: 8.4.2.2 - CPC initialization sequence:
571+ * Step 1: Flash invalidate CPC and clear locks (CPCFI | CPCLFC)
572+ * Step 2: Poll until invalidate completes
573+ * Step 3: Configure SRAM control registers (CPCSRCR1, CPCSRCR0)
574+ * Step 4: Configure LAW for SRAM routing (done after this block)
575+ * Step 5: Enable CPC with parity (CPCE | CPCPE)
576+ * Step 6: Create TLB for SRAM access
577+ * The LAW (DDR_1) provides CoreNet routing; CPC intercepts before DDR. */
578+
579+ /* R1 = CPC base - preserve across LAW setup */
580+ LOAD_ADDR32(r1, CPC_BASE)
581+
582+ /* Step 1: Flash invalidate CPC and clear all locks */
583+ lis r0, (CPCCSR0_CPCFI | CPCCSR0_CPCLFC)@h
584+ ori r0, r0, (CPCCSR0_CPCFI | CPCCSR0_CPCLFC)@l
585+ stw r0, CPCCSR0(r1)
586+
587+ /* Step 2: Poll until CPCFI and CPCLFC clear */
588+ cpc_poll_invalidate:
589+ lwz r2, CPCCSR0(r1)
590+ and . r2, r2, r0
591+ bne cpc_poll_invalidate
592+ isync
593+
594+ /* Step 3: Configure CPC SRAM control registers */
595+ li r0, 0
596+ stw r0, CPCSRCR1(r1) /* SRAM high address = 0 */
597+ /* SRAM low address - use LOAD_ADDR32 on e6500 to avoid sign extension */
598+ LOAD_ADDR32(r0, L2SRAM_ADDR)
599+ /* Enable SRAM and set size (must match L2SRAM_SIZE = 512KB) */
600+ ori r0, r0, (CPCSRCR0_SRAMSZ_512 | CPCSRCR0_SRAMEN)
601+ stw r0, CPCSRCR0(r1)
602+ mbar
603+ isync
604+ #endif /* ENABLE_L2_CACHE && L2SRAM_ADDR */
605+
606+ /* Step 3: Configure LAW for SRAM */
563607#ifdef INITIAL_SRAM_ADDR
564608#ifndef INITIAL_SRAM_NO_LAW
565609init_sram_law:
566- /* Intial SRAM LAW 2 */
610+ /* CPC SRAM uses LAW 2 - DO NOT reuse this LAW index elsewhere!
611+ * The stack resides in CPC SRAM; overwriting this LAW causes crashes. */
567612 #define INITIAL_SRAM_LAW (LAWAR_ENABLE | \
568613 LAWAR_TRGT_ID(INITIAL_SRAM_LAW_TRGT) | \
569614 INITIAL_SRAM_LAW_SZ)
570615 LOAD_ADDR32(r9, CCSRBAR + LAWBAR_BASE(2 ))
571616 li r0, 0 /* UPPER=0 */
572617 /* Use LOAD_ADDR32 on e6500 to avoid sign-extension for addresses >= 0x80000000 */
573- LOAD_ADDR32(r1 , INITIAL_SRAM_ADDR)
618+ LOAD_ADDR32(r3 , INITIAL_SRAM_ADDR)
574619 LOAD_ADDR32(r2, INITIAL_SRAM_LAW)
575620 stw r0, 0 (r9) /* LAWBARH */
576- stw r1 , 4 (r9) /* LAWBARL */
621+ stw r3 , 4 (r9) /* LAWBARL */
577622 sync
578623 stw r2, 8 (r9) /* LAWAR */
579624 /* read back LAWAR (per 2.3.2 Configuring Local Access Windows) */
580625 lwz r2, 8 (r9)
581626 isync
582627#endif /* !INITIAL_SRAM_NO_LAW */
628+ #endif /* INITIAL_SRAM_ADDR */
583629
630+ /* Step 4: Create TLB for SRAM - BEFORE CPC enable (original working order)
631+ * This is for e5500/e6500 CPC SRAM only. e500 has its own init_sram_tlb below. */
632+ #if defined(INITIAL_SRAM_ADDR) && (defined(CORE_E5500) || defined(CORE_E6500))
584633init_sram_tlb:
585634 /* Initial SRAM: TLB 1, Entry 9, Supervisor X/R/W, M, TS=0, IPROT
586- * CPC SRAM uses cacheable memory-coherent (M) access.
587- * TLB is created BEFORE l2_setup_sram per old working code. */
635+ * Original working T2080 code (commit 11f46a51) used MAS2_M. */
588636 set_tlb(1 , 9 ,
589637 INITIAL_SRAM_ADDR, INITIAL_SRAM_ADDR, 0 ,
590638 MAS3_SX | MAS3_SW | MAS3_SR, MAS2_M, 0 ,
591639 INITIAL_SRAM_BOOKE_SZ, 1 , r3);
592- #endif
593-
594- #ifdef ENABLE_L2_CACHE
640+ #endif /* INITIAL_SRAM_ADDR && (CORE_E5500 || CORE_E6500) */
595641
596- #if defined(CORE_E5500) || defined(CORE_E6500) /* --- L2 E5500/E6500 --- */
597- #ifdef L2SRAM_ADDR
598- l2_setup_sram:
599- /* T2080RM: 8.4.2.2 - CPC initialization
600- * Configure SRAM control registers, then enable CPC with parity.
601- * The LAW (DDR_1) provides CoreNet routing; CPC intercepts before DDR.
602- * SRAM is zeroed later via dcbz through cacheable TLB (MAS2_M). */
642+ /* Step 5: Enable CPC after TLB is configured */
643+ #if defined(ENABLE_L2_CACHE) && defined(L2SRAM_ADDR) && (defined(CORE_E5500) || defined(CORE_E6500))
644+ cpc_enable:
603645 /* R1 = CPC base */
604646 LOAD_ADDR32(r1, CPC_BASE)
605647
606- /* Configure CPC SRAM control registers */
607- li r0, 0
608- stw r0, CPCSRCR1(r1) /* SRAM high address = 0 */
609- /* SRAM low address - use LOAD_ADDR32 on e6500 to avoid sign extension */
610- LOAD_ADDR32(r0, L2SRAM_ADDR)
611- /* Enable SRAM and set size (must match L2SRAM_SIZE) */
612- ori r0, r0, (CPCSRCR0_SRAMSZ_1024 | CPCSRCR0_SRAMEN)
613- stw r0, CPCSRCR0(r1)
614-
615648 /* Enable CPC with parity */
616649 lis r0, (CPCCSR0_CPCE | CPCCSR0_CPCPE)@h
617650 mbar
618651 isync
619652 stw r0, CPCCSR0(r1)
620653 mbar
621654
655+ /* Verify CPC is enabled by reading back CPCCSR0 */
656+ cpc_poll_enable:
657+ lwz r2, CPCCSR0(r1)
658+ andis. r2, r2, CPCCSR0_CPCE@h /* check CPCE bit */
659+ beq cpc_poll_enable
660+ isync
661+
622662 /* Disable speculation (Errata A-006593) */
623663 lwz r0, CPCHDBCR0(r1)
624664 oris r0, r0, CPCHDBCR0_SPEC_DIS@h
625665 stw r0, CPCHDBCR0(r1)
626- #endif /* L2SRAM_ADDR */
666+ mbar
667+ isync
668+ #endif /* ENABLE_L2_CACHE && L2SRAM_ADDR */
669+
670+ #ifdef ENABLE_L2_CACHE
671+ #if defined(CORE_E5500) || defined(CORE_E6500) /* --- L2 E5500/E6500 --- */
672+ /* Note: CPC SRAM setup moved above for correct T2080RM sequence */
627673
628674#if defined(CORE_E6500) /* --- L2 E6500 --- */
629675l2_setup_cache:
@@ -653,6 +699,13 @@ l2_poll_invclear:
653699 isync
654700 LOAD_ADDR32(r4, (L2CSR0_L2E | L2CSR0_L2PE))
655701 stw r4, L2CSR0(r5)
702+ mbar
703+
704+ /* Verify L2 is enabled by reading back L2CSR0 */
705+ l2_poll_enable:
706+ lwz r3, L2CSR0(r5)
707+ andis. r3, r3, L2CSR0_L2E@h /* check bit 31 (L2E) */
708+ beq l2_poll_enable /* loop until enabled */
656709 isync
657710
658711#elif defined(CORE_E5500) /* --- L2 E5500 --- */
@@ -714,6 +767,17 @@ l2_setup_sram:
714767 stw r1, L2SRBAR0(r5)
715768 mbar
716769#endif /* L2SRAM_ADDR */
770+
771+ #ifdef INITIAL_SRAM_ADDR
772+ init_sram_tlb:
773+ /* Initial SRAM: TLB 1, Entry 9, Supervisor X/R/W, M, TS=0, IPROT
774+ * For e500, L2 SRAM uses cacheable memory-coherent (M) access.
775+ * TLB is created AFTER l2_setup_sram configures L2 as SRAM. */
776+ set_tlb(1 , 9 ,
777+ INITIAL_SRAM_ADDR, INITIAL_SRAM_ADDR, 0 ,
778+ MAS3_SX | MAS3_SW | MAS3_SR, MAS2_M, 0 ,
779+ INITIAL_SRAM_BOOKE_SZ, 1 , r3);
780+ #endif /* INITIAL_SRAM_ADDR */
717781#endif /* CORE_E500 */
718782#endif /* ENABLE_L2_CACHE */
719783
@@ -776,9 +840,9 @@ cache_sram_init_loop:
776840 bdnz cache_sram_init_loop
777841#elif defined(L2SRAM_ADDR)
778842cache_sram_init:
779- /* Zero CPC SRAM via cache (MAS2_M = cacheable, memory coherent) .
780- * dcbz allocates zeroed cache lines without reading from CPC,
781- * avoiding ECC/parity issues from uninitialized SRAM . */
843+ /* Zero CPC SRAM via dcbz .
844+ * With MAS2_M (memory-coherent), dcbz allocates zeroed cache lines.
845+ * This initializes SRAM and avoids ECC/parity issues from uninitialized data . */
782846 LOAD_ADDR32(r3, L2SRAM_ADDR)
783847 li r0, 0
784848 LOAD_ADDR32(r2, (L2SRAM_SIZE / CACHE_LINE_SIZE))
0 commit comments