Skip to content

Commit 99aba9d

Browse files
committed
Further code cleanups and optimizations
1 parent c27198e commit 99aba9d

6 files changed

Lines changed: 227 additions & 357 deletions

File tree

hal/nxp_p1021.c

Lines changed: 6 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,18 @@
3434
#define ENABLE_BUS_CLK_CALC
3535

3636
#ifndef BUILD_LOADER_STAGE1
37-
/* Tests */
38-
#if 0
39-
#define TEST_DDR
40-
#define TEST_TPM
41-
#endif
4237
#define ENABLE_PCIE
4338
#define ENABLE_CPLD /* Board Configuration and Status Registers (BCSR) */
4439
#define ENABLE_CONF_IO
4540
#define ENABLE_QE /* QUICC Engine */
46-
#if defined(WOLFBOOT_TPM) || defined(TEST_TPM)
41+
#if defined(WOLFBOOT_TPM)
4742
#define ENABLE_ESPI /* SPI for TPM */
4843
#endif
4944
#define ENABLE_MP /* multi-core support */
5045
#define ENABLE_IRQ
5146
/* #define ENABLE_QE_CRC32 */ /* CRC32 check on QE disabled by default */
5247
#endif
5348

54-
/* Foward declarations */
55-
#if defined(ENABLE_DDR) && defined(TEST_DDR)
56-
static int test_ddr(void);
57-
#endif
58-
#if defined(ENABLE_ESPI) && defined(TEST_TPM)
59-
static int test_tpm(void);
60-
#endif
61-
6249
#ifdef ENABLE_ESPI
6350
#include "spi_drv.h" /* for transfer flags and chip select */
6451
#endif
@@ -488,11 +475,7 @@ static uint32_t flash_idx;
488475
int ext_flash_read(uintptr_t address, uint8_t *data, int len);
489476
#endif
490477

491-
/* generic share NXP QorIQ driver code */
492-
#include "nxp_ppc.c"
493-
494-
495-
/* local functions */
478+
/* P1021 bus clock: reads GUTS_PORPLLSR (different from E5500/E6500 CLOCKING regs) */
496479
#ifdef ENABLE_BUS_CLK_CALC
497480
static uint32_t hal_get_bus_clk(void)
498481
{
@@ -508,53 +491,16 @@ static uint32_t hal_get_bus_clk(void)
508491
#define hal_get_bus_clk() (uint32_t)(SYS_CLK * 6)
509492
#endif
510493

494+
/* E500 uses bus-clock based delay (not TIMEBASE like E5500/E6500) */
511495
#define DELAY_US (hal_get_bus_clk() / 1000000)
512496
static void udelay(uint32_t delay_us)
513497
{
514498
wait_ticks(delay_us * DELAY_US);
515499
}
516500

517-
#if 0 /* useful timer code */
518-
519-
uint64_t hal_timer_ms(void)
520-
{
521-
uint64_t val;
522-
/* time base is updated every 8 CCB clocks */
523-
uint64_t cntfrq = hal_get_bus_clk() / 8;
524-
uint64_t cntpct = get_ticks();
525-
val = (cntpct * 1000ULL) / cntfrq;
526-
return val;
527-
}
528-
529-
/* example usage */
530-
//uint64_t start = hal_get_tick_count();
531-
// do some work
532-
//wolfBoot_printf("done (%lu ms)\n", (uint32_t)hal_elapsed_time_ms(start));
533-
534-
/* Calculate elapsed time in milliseconds, handling timer overflow properly */
535-
uint64_t hal_elapsed_time_ms(uint64_t start_ticks)
536-
{
537-
uint64_t current_ticks, elapsed_ticks;
538-
uint64_t cntfrq = hal_get_bus_clk() / 8;
539-
540-
current_ticks = get_ticks();
541-
542-
/* Handle timer overflow using unsigned arithmetic
543-
* This works correctly even if the timer has rolled over,
544-
* as long as the elapsed time is less than the full timer range
545-
*/
546-
elapsed_ticks = current_ticks - start_ticks;
547-
548-
/* Convert elapsed ticks to milliseconds */
549-
return (elapsed_ticks * 1000ULL) / cntfrq;
550-
}
501+
/* generic share NXP QorIQ driver code (uart_init/uart_write use hal_get_bus_clk above) */
502+
#include "nxp_ppc.c"
551503

552-
/* Get current tick count for use with hal_elapsed_time_ms() */
553-
uint64_t hal_get_tick_count(void)
554-
{
555-
return get_ticks();
556-
}
557-
#endif
558504

559505
/* ---- eSPI Driver ---- */
560506
#ifdef ENABLE_ESPI
@@ -673,47 +619,7 @@ void hal_espi_deinit(void)
673619
}
674620
#endif /* ENABLE_ESPI */
675621

676-
/* ---- DUART Driver ---- */
677-
#ifdef DEBUG_UART
678-
679-
void uart_init(void)
680-
{
681-
/* calc divisor for UART
682-
* baud rate = CCSRBAR frequency ÷ (16 x [UDMB||UDLB])
683-
*/
684-
/* compute UART divisor - round up */
685-
uint32_t div = (hal_get_bus_clk() + (16/2 * BAUD_RATE)) / (16 * BAUD_RATE);
686-
687-
while (!(get8(UART_LSR(UART_SEL)) & UART_LSR_TEMT))
688-
;
689-
690-
/* set ier, fcr, mcr */
691-
set8(UART_IER(UART_SEL), 0);
692-
set8(UART_FCR(UART_SEL), (UART_FCR_TFR | UART_FCR_RFR | UART_FCR_FEN));
693-
694-
/* enable baud rate access (DLAB=1) - divisor latch access bit*/
695-
set8(UART_LCR(UART_SEL), (UART_LCR_DLAB | UART_LCR_WLS));
696-
/* set divisor */
697-
set8(UART_DLB(UART_SEL), (div & 0xff));
698-
set8(UART_DMB(UART_SEL), ((div>>8) & 0xff));
699-
/* disable rate access (DLAB=0) */
700-
set8(UART_LCR(UART_SEL), (UART_LCR_WLS));
701-
}
702-
703-
void uart_write(const char* buf, uint32_t sz)
704-
{
705-
uint32_t pos = 0;
706-
while (sz-- > 0) {
707-
char c = buf[pos++];
708-
if (c == '\n') { /* handle CRLF */
709-
while ((get8(UART_LSR(UART_SEL)) & UART_LSR_THRE) == 0);
710-
set8(UART_THR(UART_SEL), '\r');
711-
}
712-
while ((get8(UART_LSR(UART_SEL)) & UART_LSR_THRE) == 0);
713-
set8(UART_THR(UART_SEL), c);
714-
}
715-
}
716-
#endif /* DEBUG_UART */
622+
/* uart_init and uart_write are provided by nxp_ppc.c shared code */
717623

718624
/* ---- eLBC Driver ---- */
719625
#ifdef ENABLE_ELBC

hal/nxp_ppc.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,89 @@
2121

2222
/* This file gets directly included from nxp_ targets.
2323
* This file contains shared driver code for all NXP QorIQ platforms */
24+
25+
/* RAMFUNCTION is defined by image.h (included by targets that need RAM_CODE).
26+
* Provide an empty fallback for targets that do not use RAM_CODE (e.g. T1024/P1021). */
27+
#ifndef RAMFUNCTION
28+
#define RAMFUNCTION
29+
#endif
30+
31+
/* ---- E5500/E6500 clock helpers and udelay ----
32+
* CLOCKING_PLLCNGSR and CLOCKING_PLLPGSR must be defined by the including
33+
* target before this file is reached (e.g. via nxp_t2080.h / nxp_t1024.c).
34+
* SYS_CLK must be the oscillator input frequency (e.g. 100 MHz). */
35+
#if defined(CORE_E5500) || defined(CORE_E6500)
36+
#ifdef ENABLE_BUS_CLK_CALC
37+
static uint32_t hal_get_core_clk(void)
38+
{
39+
/* compute core clock: system_input * (CGA_PLL1_RAT / 2) */
40+
uint32_t core_clk;
41+
uint32_t core_ratio = get32(CLOCKING_PLLCNGSR(0));
42+
core_ratio = ((core_ratio >> 1) & 0x3F);
43+
core_clk = SYS_CLK * core_ratio;
44+
return core_clk;
45+
}
46+
static uint32_t RAMFUNCTION hal_get_plat_clk(void)
47+
{
48+
/* compute platform clock: system_input * (SYS_PLL_RAT / 2) */
49+
uint32_t plat_clk;
50+
uint32_t plat_ratio = get32(CLOCKING_PLLPGSR);
51+
plat_ratio = ((plat_ratio >> 1) & 0x1F);
52+
plat_clk = SYS_CLK * plat_ratio;
53+
return plat_clk;
54+
}
55+
static uint32_t hal_get_bus_clk(void)
56+
{
57+
return hal_get_plat_clk() / 2;
58+
}
59+
#endif /* ENABLE_BUS_CLK_CALC */
60+
61+
#define TIMEBASE_CLK_DIV 16
62+
#define TIMEBASE_HZ (hal_get_plat_clk() / TIMEBASE_CLK_DIV)
63+
#define DELAY_US (TIMEBASE_HZ / 1000000)
64+
static void RAMFUNCTION udelay(uint32_t delay_us)
65+
{
66+
wait_ticks((unsigned long long)delay_us * DELAY_US);
67+
}
68+
#endif /* CORE_E5500 || CORE_E6500 */
69+
70+
/* ---- Shared PC16552D-compatible DUART driver ----
71+
* Each target must define before including this file:
72+
* UART_SEL, BAUD_RATE, UART_THR(n), UART_IER(n), UART_FCR(n),
73+
* UART_LCR(n), UART_DLB(n), UART_DMB(n), UART_LSR(n),
74+
* UART_FCR_TFR, UART_FCR_RFR, UART_FCR_FEN,
75+
* UART_LCR_DLAB, UART_LCR_WLS, UART_LSR_TEMT, UART_LSR_THRE */
76+
#ifdef DEBUG_UART
77+
void uart_init(void)
78+
{
79+
/* baud rate = bus_clk / (16 * div); round up */
80+
uint32_t div = (hal_get_bus_clk() + (8 * BAUD_RATE)) / (16 * BAUD_RATE);
81+
82+
while (!(get8(UART_LSR(UART_SEL)) & UART_LSR_TEMT))
83+
;
84+
85+
set8(UART_IER(UART_SEL), 0);
86+
set8(UART_FCR(UART_SEL), (UART_FCR_TFR | UART_FCR_RFR | UART_FCR_FEN));
87+
88+
/* enable baud rate access (DLAB=1) */
89+
set8(UART_LCR(UART_SEL), (UART_LCR_DLAB | UART_LCR_WLS));
90+
set8(UART_DLB(UART_SEL), (div & 0xff));
91+
set8(UART_DMB(UART_SEL), ((div >> 8) & 0xff));
92+
/* disable baud rate access (DLAB=0) */
93+
set8(UART_LCR(UART_SEL), (UART_LCR_WLS));
94+
}
95+
96+
void uart_write(const char* buf, uint32_t sz)
97+
{
98+
uint32_t pos = 0;
99+
while (sz-- > 0) {
100+
char c = buf[pos++];
101+
if (c == '\n') { /* handle CRLF */
102+
while ((get8(UART_LSR(UART_SEL)) & UART_LSR_THRE) == 0);
103+
set8(UART_THR(UART_SEL), '\r');
104+
}
105+
while ((get8(UART_LSR(UART_SEL)) & UART_LSR_THRE) == 0);
106+
set8(UART_THR(UART_SEL), c);
107+
}
108+
}
109+
#endif /* DEBUG_UART */

hal/nxp_ppc.h

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,6 @@
111111
#define CCSRBAR_DEF (0xFE000000UL) /* T2080RM 4.3.1 default base */
112112
#define CCSRBAR_SIZE BOOKE_PAGESZ_16M
113113

114-
/* relocate to 64-bit 0xE_ */
115-
//#define CCSRBAR_PHYS_HIGH 0xEULL
116-
//#define CCSRBAR_PHYS (CCSRBAR_PHYS_HIGH + CCSRBAR_DEF)
117-
118114
#define ENABLE_L1_CACHE
119115
#define ENABLE_L2_CACHE
120116

@@ -143,7 +139,11 @@
143139

144140
#define ENABLE_DDR
145141
#ifndef DDR_SIZE
146-
#define DDR_SIZE (8192ULL * 1024ULL * 1024ULL)
142+
#ifdef BOARD_VPX3152
143+
#define DDR_SIZE (8192ULL * 1024ULL * 1024ULL) /* TODO: confirm from CS_BNDS dump (4/8/16 GB) */
144+
#else
145+
#define DDR_SIZE (8192ULL * 1024ULL * 1024ULL) /* NAII 68PPC2: 8 GB */
146+
#endif
147147
#endif
148148

149149
/* DDR stack configuration - relocate from CPC SRAM after DDR init
@@ -158,14 +158,25 @@
158158
* RAMFUNCTION code continues to work after CPC becomes L2 cache. */
159159
#define DDR_RAMCODE_ADDR 0x03000000UL /* 48MB into DDR */
160160

161+
/* Flash base address and size — may differ between board variants.
162+
* TODO: Confirm VPX3-152 flash mapping from IFC CSPR(0)/AMASK(0) dump.
163+
* If the new board uses a different base address (e.g. 0xF0000000 for
164+
* 256 MB flash), update the BOARD_VPX3152 values and uncomment. */
165+
#if 0 && defined(BOARD_VPX3152)
166+
#define FLASH_BASE_ADDR 0xF0000000UL /* TODO: from IFC dump */
167+
#define FLASH_BASE_PHYS_HIGH 0x0ULL
168+
#define FLASH_LAW_SIZE LAW_SIZE_256MB
169+
#define FLASH_TLB_PAGESZ BOOKE_PAGESZ_256M
170+
#else
161171
#define FLASH_BASE_ADDR 0xE8000000UL
162172
#define FLASH_BASE_PHYS_HIGH 0x0ULL
163173
#define FLASH_LAW_SIZE LAW_SIZE_128MB
164174
#define FLASH_TLB_PAGESZ BOOKE_PAGESZ_128M
175+
#endif
165176

166177
#define USE_LONG_JUMP
167178
#else
168-
#error Please define platform PowerPC core version and CCSRBAR
179+
#error Please define TARGET (nxp_t2080, nxp_t1024, or nxp_p1021)
169180
#endif
170181

171182

@@ -511,18 +522,16 @@ static inline void uart_putc_early(char c) { (void)c; }
511522
#define L2CSR1 0x3FA /* L2 Data Cache Control and Status Register 1 */
512523
#endif
513524

514-
#define L2CSR0_L2FI 0x00200000 /* L2 Cache Flash Invalidate */
515-
#define L2CSR0_L2FL 0x00000800 /* L2 Cache Flush */
516-
#define L2CSR0_L2LFC 0x00000400 /* L2 Cache Lock Flash Clear */
517-
#define L2CSR0_L2PE 0x40000000 /* L2 Cache Parity/ECC Enable */
518525
#define L2CSR0_L2E 0x80000000 /* L2 Cache Enable */
519-
526+
#define L2CSR0_L2PE 0x40000000 /* L2 Cache Parity/ECC Enable */
520527
#define L2CSR0_L2WP 0x1c000000 /* L2 I/D Way Partioning */
521528
#define L2CSR0_L2CM 0x03000000 /* L2 Cache Coherency Mode */
522529
#define L2CSR0_L2FI 0x00200000 /* L2 Cache Flash Invalidate */
523530
#define L2CSR0_L2IO 0x00100000 /* L2 Cache Instruction Only */
524531
#define L2CSR0_L2DO 0x00010000 /* L2 Cache Data Only */
525532
#define L2CSR0_L2REP 0x00003000 /* L2 Line Replacement Algo */
533+
#define L2CSR0_L2FL 0x00000800 /* L2 Cache Flush */
534+
#define L2CSR0_L2LFC 0x00000400 /* L2 Cache Lock Flash Clear */
526535

527536

528537
#define SCCSRBAR 0x3FE /* Shifted CCSRBAR */
@@ -846,11 +855,6 @@ extern void dcache_disable(void);
846855
#define ENTRY_RESV 16
847856
#define ENTRY_PIR 20
848857

849-
/* not used for ePAPR 1.1 */
850-
#define ENTRY_R6_UPPER 24
851-
#define ENTRY_R6_LOWER 28
852-
853-
854858
#define ENTRY_SIZE 64
855859

856860
#endif /* !_NXP_PPC_H_ */

0 commit comments

Comments
 (0)