Skip to content

Commit 7692059

Browse files
committed
[SAMA5D3] Added test-app, moved to SP_ASM
1 parent 888d538 commit 7692059

8 files changed

Lines changed: 249 additions & 26 deletions

File tree

arch.mk

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,24 @@ ifeq ($(ARCH),ARM)
178178
ifeq ($(TARGET),sama5d3)
179179
CORTEX_A5=1
180180
UPDATE_OBJS:=src/update_ram.o
181-
CFLAGS+=-DWOLFBOOT_DUALBOOT -DEXT_FLASH -DNAND_FLASH
181+
CFLAGS+=-DWOLFBOOT_DUALBOOT -DEXT_FLASH -DNAND_FLASH -fno-builtin -ffreestanding
182182
#CFLAGS+=-DWOLFBOOT_USE_STDLIBC
183183
endif
184184

185185
## Cortex CPU
186186

187187
ifeq ($(CORTEX_A5),1)
188-
CFLAGS+=-mcpu=cortex-a5 -mtune=cortex-a5 -mfpu=vfpv4-d16 -static -z noexecstack
189-
LDLAGS+=-mcpu=cortex-a5 -mtune=cortex-a5 -mtune=cortex-a5 -mfpu=vfpv4-d16 -static -z noexecstack -Ttext 0x300000
188+
FPU=-mfpu=vfp4-d16
189+
CFLAGS+=-mcpu=cortex-a5 -mtune=cortex-a5 -static -z noexecstack
190+
LDLAGS+=-mcpu=cortex-a5 -mtune=cortex-a5 -mtune=cortex-a5 -static -z noexecstack -Ttext 0x300000
190191
# Cortex-A uses boot_arm32.o
191192
OBJS+=src/boot_arm32.o src/boot_arm32_start.o
192-
MATH_OBJS += ./lib/wolfssl/wolfcrypt/src/sp_c32.o
193+
ifeq ($(NO_ASM),1)
194+
MATH_OBJS+=./lib/wolfssl/wolfcrypt/src/sp_c32.o
195+
else
196+
MATH_OBJS+=./lib/wolfssl/wolfcrypt/src/sp_arm32.o
197+
CFLAGS+=-DWOLFSSL_SP_ARM32_ASM
198+
endif
193199
else
194200
# All others use boot_arm.o
195201
OBJS+=src/boot_arm.o

config/examples/sama5d3.config

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ SPMATH?=1
1414
WOLFBOOT_PARTITION_SIZE?=0x1000000
1515
WOLFBOOT_NO_PARTITIONS=0
1616
WOLFBOOT_SECTOR_SIZE?=0x1000
17-
WOLFBOOT_LOAD_ADDRESS=0x380000
18-
WOLFBOOT_LOAD_DTS_ADDRESS=0x3C0000
19-
WOLFBOOT_PARTITION_BOOT_ADDRESS=0x40000
20-
WOLFBOOT_PARTITION_UPDATE_ADDRESS=0x80000
17+
WOLFBOOT_LOAD_ADDRESS=0x201000
18+
WOLFBOOT_LOAD_DTS_ADDRESS=0x281000
19+
WOLFBOOT_PARTITION_BOOT_ADDRESS=0x400000
20+
WOLFBOOT_PARTITION_UPDATE_ADDRESS=0x800000
2121
WOLFBOOT_PARTITION_SWAP_ADDRESS=0x0
2222
NO_XIP=1
2323
IMAGE_HEADER_SIZE=2048

hal/sama5d3.c

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@ extern void *kernel_addr, *update_addr, *dts_addr;
4848

4949
#define NAND_MASK_ALE (1 << 21)
5050
#define NAND_MASK_CLE (1 << 22)
51-
#define NAND_CMD (*((volatile uint8_t *)(NAND_BASE | NAND_MASK_CLE)))
51+
#define NAND_CMD (*((volatile uint8_t *)(NAND_BASE | NAND_MASK_CLE)))
5252
#define NAND_ADDR (*((volatile uint8_t *)(NAND_BASE | NAND_MASK_ALE)))
5353
#define NAND_DATA (*((volatile uint8_t *)(NAND_BASE)))
54-
#define NAND_DATAW (*((volatile uint32_t *)(NAND_BASE)))
5554

5655
/* Command set */
5756
#define NAND_CMD_STATUS 0x70
@@ -141,6 +140,80 @@ extern void *kernel_addr, *update_addr, *dts_addr;
141140
#define MAX_ECC_BYTES 8
142141

143142

143+
/* Manual division operation */
144+
int division(uint32_t dividend,
145+
uint32_t divisor,
146+
uint32_t *quotient,
147+
uint32_t *remainder)
148+
{
149+
uint32_t shift;
150+
uint32_t divisor_shift;
151+
uint32_t factor = 0;
152+
unsigned char end_flag = 0;
153+
154+
if (!divisor)
155+
return 0xffffffff;
156+
157+
if (dividend < divisor) {
158+
*quotient = 0;
159+
*remainder = dividend;
160+
return 0;
161+
}
162+
163+
while (dividend >= divisor) {
164+
for (shift = 0, divisor_shift = divisor;
165+
dividend >= divisor_shift;
166+
divisor_shift <<= 1, shift++) {
167+
if (dividend - divisor_shift < divisor_shift) {
168+
factor += 1 << shift;
169+
dividend -= divisor_shift;
170+
end_flag = 1;
171+
break;
172+
}
173+
}
174+
175+
if (end_flag)
176+
continue;
177+
178+
factor += 1 << (shift - 1);
179+
dividend -= divisor_shift >> 1;
180+
}
181+
182+
if (quotient)
183+
*quotient = factor;
184+
185+
if (remainder)
186+
*remainder = dividend;
187+
188+
return 0;
189+
}
190+
191+
uint32_t div(uint32_t dividend, uint32_t divisor)
192+
{
193+
uint32_t quotient = 0;
194+
uint32_t remainder = 0;
195+
int ret;
196+
197+
ret = division(dividend, divisor, &quotient, &remainder);
198+
if (ret)
199+
return 0xffffffff;
200+
201+
return quotient;
202+
}
203+
204+
uint32_t mod(uint32_t dividend, uint32_t divisor)
205+
{
206+
uint32_t quotient = 0;
207+
uint32_t remainder = 0;
208+
int ret;
209+
210+
ret = division(dividend, divisor, &quotient, &remainder);
211+
if (ret)
212+
return 0xffffffff;
213+
214+
return remainder;
215+
}
216+
144217
/* Static variables to hold nand info */
145218
static uint8_t nand_manif_id;
146219
static uint8_t nand_dev_id;
@@ -245,7 +318,7 @@ static void nand_read_info(void)
245318
nand_flash.bad_block_pos = (*(uint16_t *)(onfi_data + PARAMS_POS_FEATURES)) & 1;
246319
nand_flash.ext_page_len = *(uint16_t *)(onfi_data + PARAMS_POS_EXT_PARAM_PAGE_LEN);
247320
nand_flash.parameter_page = *(uint16_t *)(onfi_data + PARAMS_POS_PARAMETER_PAGE);
248-
nand_flash.pages_per_block = nand_flash.block_size / nand_flash.page_size;
321+
nand_flash.pages_per_block = div(nand_flash.block_size, nand_flash.page_size);
249322
nand_flash.pages_per_device = nand_flash.pages_per_block * nand_flash.block_count;
250323
nand_flash.oob_size = *(uint16_t *)(onfi_data + PARAMS_POS_OOBSIZE);
251324
nand_flash.revision = *(uint16_t *)(onfi_data + PARAMS_POS_REVISION);
@@ -342,14 +415,14 @@ static int nand_check_bad_block(uint32_t block)
342415
}
343416

344417

345-
static uint8_t buffer_page[NAND_FLASH_PAGE_SIZE];
346418

347419
int ext_flash_read(uintptr_t address, uint8_t *data, int len)
348420
{
349-
uint32_t block = address / nand_flash.block_size; /* The block where the address falls in */
350-
uint32_t page = address / nand_flash.page_size; /* The page where the address falls in */
351-
uint32_t start_page_in_block = page % nand_flash.pages_per_block; /* The start page within this block */
352-
uint32_t in_block_offset = address % nand_flash.block_size; /* The offset of the address within the block */
421+
uint8_t buffer_page[NAND_FLASH_PAGE_SIZE];
422+
uint32_t block = div(address, nand_flash.block_size); /* The block where the address falls in */
423+
uint32_t page = div(address, nand_flash.page_size); /* The page where the address falls in */
424+
uint32_t start_page_in_block = mod(page, nand_flash.pages_per_block); /* The start page within this block */
425+
uint32_t in_block_offset = mod(address, nand_flash.block_size); /* The offset of the address within the block */
353426
uint32_t remaining = nand_flash.block_size - in_block_offset; /* How many bytes remaining to read in the first block */
354427
uint32_t len_to_read = len;
355428
uint8_t *buffer = data;
@@ -378,7 +451,7 @@ int ext_flash_read(uintptr_t address, uint8_t *data, int len)
378451
} while (ret < 0);
379452

380453
/* Amount of pages to be read from this block */
381-
pages_to_read = (sz + nand_flash.page_size - 1) / nand_flash.page_size;
454+
pages_to_read = div((sz + nand_flash.page_size - 1), nand_flash.page_size);
382455

383456
if (pages_to_read * nand_flash.page_size > remaining)
384457
pages_to_read--;
@@ -397,7 +470,8 @@ int ext_flash_read(uintptr_t address, uint8_t *data, int len)
397470
if (copy) {
398471
uint32_t *dst = (uint32_t *)data;
399472
uint32_t *src = (uint32_t *)buffer_page;
400-
for (i = 0; i < (len / sizeof(uint32_t)); i++) {
473+
uint32_t tot_len = (uint32_t)len;
474+
for (i = 0; i < (tot_len >> 2); i++) {
401475
dst[i] = src[i];
402476
}
403477
}

hal/sama5d3.ld

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ OUTPUT_ARCH(arm)
33

44
MEMORY
55
{
6-
DDR_MEM(rwx): ORIGIN = 0x00300000, LENGTH = 0x0007F000
6+
DDR_MEM(rwx): ORIGIN = 0x00000000, LENGTH = 0x0000F000
77
}
88

99
ENTRY(reset_vector_entry)
@@ -12,9 +12,9 @@ SECTIONS
1212
.text : {
1313
_start_text = .;
1414
*(.text)
15-
*(.rodata)
16-
*(.rodata*)
17-
. = ALIGN(4);
15+
*(.rodata)
16+
*(.rodata*)
17+
. = ALIGN(4);
1818
*(.glue_7)
1919
. = ALIGN(4);
2020
*(.eh_frame)
@@ -46,8 +46,8 @@ SECTIONS
4646
}
4747
}
4848

49-
kernel_addr = 0x040000;
50-
update_addr = 0x080000;
49+
kernel_addr = 0x0400000;
50+
update_addr = 0x0800000;
5151

5252
_romsize = _end_data - _start_text;
5353
_sramsize = _end_bss - _start_text;

test-app/ARM-sama5d3.ld

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ OUTPUT_ARCH(arm)
33

44
MEMORY
55
{
6-
DDR_MEM(rwx): ORIGIN = 0x00340000, LENGTH = 0x000100000
6+
DDR_MEM(rwx): ORIGIN = 0x00311000, LENGTH = 0x000080000
77
}
88

99
ENTRY(reset_vector_entry)
1010
SECTIONS
1111
{
1212
.text : {
1313
_start_text = .;
14+
*(.iv)
1415
*(.text)
1516
*(.rodata)
1617
*(.rodata*)

test-app/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ ifeq ($(TARGET),stm32c0)
146146
endif
147147

148148
ifeq ($(TARGET),sama5d3)
149-
APP_OBJS+=../src/boot_arm32_start.o
149+
APP_OBJS+=./boot_arm32_start.o
150150
LSCRIPT_TEMPLATE:=$(ARCH)-$(TARGET).ld
151151
endif
152152

test-app/app_sama5d3.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,54 @@
2828
#include "wolfboot/wolfboot.h"
2929

3030
#ifdef TARGET_sama5d3
31+
/* Blue LED is PE23, Red LED is PE24 */
32+
33+
#define GPIOE_BASE 0xFFFFFA00
34+
35+
#define GPIOE_PER *(volatile uint32_t *)(GPIOE_BASE + 0x00)
36+
#define GPIOE_PDR *(volatile uint32_t *)(GPIOE_BASE + 0x04)
37+
#define GPIOE_PSR *(volatile uint32_t *)(GPIOE_BASE + 0x08)
38+
#define GPIOE_OER *(volatile uint32_t *)(GPIOE_BASE + 0x10)
39+
#define GPIOE_ODR *(volatile uint32_t *)(GPIOE_BASE + 0x14)
40+
#define GPIOE_OSR *(volatile uint32_t *)(GPIOE_BASE + 0x18)
41+
#define GPIOE_SODR *(volatile uint32_t *)(GPIOE_BASE + 0x30)
42+
#define GPIOE_CODR *(volatile uint32_t *)(GPIOE_BASE + 0x34)
43+
#define GPIOE_IER *(volatile uint32_t *)(GPIOE_BASE + 0x40)
44+
#define GPIOE_IDR *(volatile uint32_t *)(GPIOE_BASE + 0x44)
45+
#define GPIOE_MDER *(volatile uint32_t *)(GPIOE_BASE + 0x50)
46+
#define GPIOE_MDDR *(volatile uint32_t *)(GPIOE_BASE + 0x54)
47+
#define GPIOE_PPUDR *(volatile uint32_t *)(GPIOE_BASE + 0x60)
48+
#define GPIOE_PPUER *(volatile uint32_t *)(GPIOE_BASE + 0x64)
49+
50+
#define BLUE_LED_PIN 23
51+
#define RED_LED_PIN 24
52+
53+
void led_init(uint32_t pin)
54+
{
55+
uint32_t mask = 1U << pin;
56+
GPIOE_MDDR |= mask;
57+
GPIOE_PER |= mask;
58+
GPIOE_IDR |= mask;
59+
GPIOE_PPUDR |= mask;
60+
GPIOE_CODR |= mask;
61+
}
62+
63+
void led_put(uint32_t pin, int val)
64+
{
65+
uint32_t mask = 1U << pin;
66+
if (val)
67+
GPIOE_SODR |= mask;
68+
else
69+
GPIOE_CODR |= mask;
70+
}
3171

3272
volatile uint32_t time_elapsed = 0;
3373
void main(void) {
3474

3575
/* Wait for reboot */
76+
led_init(RED_LED_PIN);
77+
led_put(RED_LED_PIN, 1);
78+
3679
while(1)
3780
;
3881
}

test-app/boot_arm32_start.S

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* Arm32 (32bit Cortex-A) boot up
3+
* Copyright (C) 2024 wolfSSL Inc.
4+
*
5+
* This file is part of wolfBoot.
6+
*
7+
* wolfBoot is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfBoot is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*/
21+
.section start
22+
.text
23+
24+
/* startup entry point */
25+
.globl reset_vector_entry
26+
.align 4
27+
.section .iv
28+
reset_vector_entry:
29+
/* Exception vectors (should be a branch to be detected as a valid code by the rom */
30+
_exception_vectors:
31+
b isr_reset /* reset */
32+
b isr_empty /* Undefined Instruction */
33+
b isr_swi /* Software Interrupt */
34+
b isr_pabt /* Prefetch Abort */
35+
b dabt_vector /* Data Abort */
36+
.word _romsize /* Size of the binary for ROMCode loading */
37+
b isr_irq /* IRQ : read the AIC */
38+
b isr_fiq /* FIQ */
39+
40+
isr_empty:
41+
b isr_empty
42+
isr_swi:
43+
b isr_swi
44+
isr_pabt:
45+
b isr_pabt
46+
dabt_vector:
47+
subs pc, r14, #4 /* return */
48+
nop
49+
isr_rsvd:
50+
b isr_rsvd
51+
isr_irq:
52+
b isr_irq
53+
isr_fiq:
54+
b isr_fiq
55+
56+
57+
/* Reset handler procedure. Prepare the memory and call main() */
58+
isr_reset:
59+
/* Initialize the stack pointer */
60+
ldr sp,=_stack_top
61+
/* Save BootROM supplied boot source information to stack */
62+
push {r4}
63+
64+
/* Copy the data section */
65+
ldr r2, =_lp_data
66+
ldmia r2, {r1, r3, r4}
67+
1:
68+
cmp r3, r4
69+
ldrcc r2, [r1], #4
70+
strcc r2, [r3], #4
71+
bcc 1b
72+
73+
/* Zero bss area */
74+
adr r2, _lp_bss
75+
ldmia r2, {r3, r4}
76+
mov r2, #0
77+
1:
78+
cmp r3, r4
79+
strcc r2, [r3], #4
80+
bcc 1b
81+
82+
/* Jump to main() */
83+
ldr r4, = main
84+
mov lr, pc
85+
bx r4
86+
87+
/* main() should never return */
88+
_panic:
89+
b _panic
90+
91+
.align
92+
_lp_data:
93+
.word _start_data
94+
.word _end_data
95+
96+
_lp_bss:
97+
.word _start_bss
98+
.word _end_bss
99+

0 commit comments

Comments
 (0)