Skip to content

Commit 245b826

Browse files
sylvioalvescfriedt
authored andcommitted
soc: esp32c3: update startup code to map IROM and DROM segments
This change updates startup code to setup MMU mappings Signed-off-by: Sylvio Alves <[email protected]>
1 parent 7e39ec9 commit 245b826

File tree

3 files changed

+96
-23
lines changed

3 files changed

+96
-23
lines changed

soc/riscv/esp32c3/loader.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) 2022 Espressif Systems (Shanghai) Co., Ltd.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <esp32c3/rom/cache.h>
8+
#include "soc/cache_memory.h"
9+
#include "soc/extmem_reg.h"
10+
#include <bootloader_flash_priv.h>
11+
12+
#include <zephyr.h>
13+
#include <soc.h>
14+
#include <storage/flash_map.h>
15+
#include <esp_log.h>
16+
#include <stdlib.h>
17+
18+
#ifdef CONFIG_BOOTLOADER_MCUBOOT
19+
#define HDR_ATTR __attribute__((section(".entry_addr"))) __attribute__((used))
20+
21+
extern uint32_t _image_irom_start, _image_irom_size, _image_irom_vaddr;
22+
extern uint32_t _image_drom_start, _image_drom_size, _image_drom_vaddr;
23+
24+
void __start(void);
25+
26+
static HDR_ATTR void (*_entry_point)(void) = &__start;
27+
28+
static int map_rom_segments(void)
29+
{
30+
int rc = 0;
31+
32+
size_t _partition_offset = FLASH_AREA_OFFSET(image_0);
33+
uint32_t _app_irom_start = _partition_offset + (uint32_t)&_image_irom_start;
34+
uint32_t _app_irom_size = (uint32_t)&_image_irom_size;
35+
uint32_t _app_irom_vaddr = (uint32_t)&_image_irom_vaddr;
36+
37+
uint32_t _app_drom_start = _partition_offset + (uint32_t)&_image_drom_start;
38+
uint32_t _app_drom_size = (uint32_t)&_image_drom_size;
39+
uint32_t _app_drom_vaddr = (uint32_t)&_image_drom_vaddr;
40+
41+
uint32_t autoload = esp_rom_Cache_Suspend_ICache();
42+
43+
esp_rom_Cache_Invalidate_ICache_All();
44+
45+
/* Clear the MMU entries that are already set up,
46+
* so the new app only has the mappings it creates.
47+
*/
48+
for (size_t i = 0; i < FLASH_MMU_TABLE_SIZE; i++) {
49+
FLASH_MMU_TABLE[i] = MMU_TABLE_INVALID_VAL;
50+
}
51+
52+
uint32_t drom_page_count = bootloader_cache_pages_to_map(_app_drom_size, _app_drom_vaddr);
53+
54+
rc |= esp_rom_Cache_Dbus_MMU_Set(MMU_ACCESS_FLASH, _app_drom_vaddr & 0xffff0000,
55+
_app_drom_start & 0xffff0000, 64, drom_page_count, 0);
56+
57+
uint32_t irom_page_count = bootloader_cache_pages_to_map(_app_irom_size, _app_irom_vaddr);
58+
59+
rc |= esp_rom_Cache_Ibus_MMU_Set(MMU_ACCESS_FLASH, _app_irom_vaddr & 0xffff0000,
60+
_app_irom_start & 0xffff0000, 64, irom_page_count, 0);
61+
62+
REG_CLR_BIT(EXTMEM_ICACHE_CTRL1_REG, EXTMEM_ICACHE_SHUT_IBUS);
63+
REG_CLR_BIT(EXTMEM_ICACHE_CTRL1_REG, EXTMEM_ICACHE_SHUT_DBUS);
64+
65+
esp_rom_Cache_Resume_ICache(autoload);
66+
67+
return rc;
68+
}
69+
#endif
70+
71+
void __start(void)
72+
{
73+
#ifdef CONFIG_BOOTLOADER_MCUBOOT
74+
int err = map_rom_segments();
75+
76+
if (err != 0) {
77+
ets_printf("Failed to setup XIP, aborting\n");
78+
abort();
79+
}
80+
#endif
81+
__esp_platform_start();
82+
}

soc/riscv/esp32c3/soc.c

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,17 @@
1717
#include <drivers/interrupt_controller/intc_esp32c3.h>
1818

1919
#include <kernel_structs.h>
20+
#include <kernel_internal.h>
2021
#include <string.h>
2122
#include <toolchain/gcc.h>
2223
#include <soc.h>
2324

24-
extern void _PrepC(void);
25-
2625
/*
2726
* This is written in C rather than assembly since, during the port bring up,
2827
* Zephyr is being booted by the Espressif bootloader. With it, the C stack
2928
* is already set up.
3029
*/
31-
void __attribute__((section(".iram1"))) __start(void)
30+
void __attribute__((section(".iram1"))) __esp_platform_start(void)
3231
{
3332
volatile uint32_t *wdt_rtc_protect = (uint32_t *)RTC_CNTL_WDTWPROTECT_REG;
3433
volatile uint32_t *wdt_rtc_reg = (uint32_t *)RTC_CNTL_WDTCONFIG0_REG;
@@ -47,35 +46,18 @@ void __attribute__((section(".iram1"))) __start(void)
4746
__asm__ __volatile__("la t0, _esp32c3_vector_table\n"
4847
"csrw mtvec, t0\n");
4948

49+
z_bss_zero();
50+
5051
/* Disable normal interrupts. */
5152
csr_read_clear(mstatus, MSTATUS_MIE);
5253

53-
#if !CONFIG_BOOTLOADER_ESP_IDF
54-
/* The watchdog timer is enabled in the 1st stage (ROM) bootloader.
55-
* We're done booting, so disable it.
56-
* If 2nd stage bootloader from IDF is enabled, then that will take
57-
* care of this.
58-
*/
59-
volatile uint32_t *wdt_timg_protect = (uint32_t *)TIMG_WDTWPROTECT_REG(0);
60-
volatile uint32_t *wdt_timg_reg = (uint32_t *)TIMG_WDTCONFIG0_REG(0);
61-
62-
*wdt_rtc_protect = RTC_CNTL_WDT_WKEY_VALUE;
63-
*wdt_rtc_reg &= ~RTC_CNTL_WDT_FLASHBOOT_MOD_EN;
64-
*wdt_rtc_protect = 0;
65-
*wdt_timg_protect = TIMG_WDT_WKEY_VALUE;
66-
*wdt_timg_reg &= ~TIMG_WDT_FLASHBOOT_MOD_EN;
67-
*wdt_timg_protect = 0;
68-
#endif
69-
70-
#if CONFIG_BOOTLOADER_ESP_IDF
7154
/* ESP-IDF 2nd stage bootloader enables RTC WDT to check on startup sequence
7255
* related issues in application. Hence disable that as we are about to start
7356
* Zephyr environment.
7457
*/
7558
*wdt_rtc_protect = RTC_CNTL_WDT_WKEY_VALUE;
7659
*wdt_rtc_reg &= ~RTC_CNTL_WDT_EN;
7760
*wdt_rtc_protect = 0;
78-
#endif
7961

8062
/* Configure the Cache MMU size for instruction and rodata in flash. */
8163
extern uint32_t esp_rom_cache_set_idrom_mmu_size(uint32_t irom_size,
@@ -105,7 +87,7 @@ void __attribute__((section(".iram1"))) __start(void)
10587
esp_intr_initialize();
10688

10789
/* Start Zephyr */
108-
_PrepC();
90+
z_cstart();
10991

11092
CODE_UNREACHABLE;
11193
}

soc/riscv/esp32c3/soc.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
#ifndef _ASMLANGUAGE
3737

38+
void __esp_platform_start(void);
39+
3840
extern void esp_rom_intr_matrix_set(int cpu_no, uint32_t model_num, uint32_t intr_num);
3941
extern void esp_rom_uart_attach(void);
4042
extern void esp_rom_uart_tx_wait_idle(uint8_t uart_no);
@@ -48,6 +50,13 @@ extern void esprv_intc_int_set_threshold(int priority_threshold);
4850
uint32_t soc_intr_get_next_source(void);
4951
extern void esp_rom_Cache_Resume_ICache(uint32_t autoload);
5052
extern int esp_rom_Cache_Invalidate_Addr(uint32_t addr, uint32_t size);
53+
extern uint32_t esp_rom_Cache_Suspend_ICache(void);
54+
extern void esp_rom_Cache_Invalidate_ICache_All(void);
55+
extern int esp_rom_Cache_Dbus_MMU_Set(uint32_t ext_ram, uint32_t vaddr, uint32_t paddr,
56+
uint32_t psize, uint32_t num, uint32_t fixed);
57+
extern int esp_rom_Cache_Ibus_MMU_Set(uint32_t ext_ram, uint32_t vaddr, uint32_t paddr,
58+
uint32_t psize, uint32_t num, uint32_t fixed);
59+
extern void esp_rom_Cache_Resume_ICache(uint32_t autoload);
5160
extern spiflash_legacy_data_t esp_rom_spiflash_legacy_data;
5261
extern int esp_rom_gpio_matrix_in(uint32_t gpio, uint32_t signal_index,
5362
bool inverted);

0 commit comments

Comments
 (0)