|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include "soc/reg_base.h" |
| 8 | + |
| 9 | +#define HP_SYSTEM_CORE_ERR_RESP_DIS_REG (DR_REG_HP_SYS_BASE + 0x1a4) |
| 10 | + |
| 11 | +/* Clock related */ |
| 12 | +#define DR_REG_LP_CLKRST_BASE (DR_REG_LPAON_BASE + 0x1000) |
| 13 | +#define LP_CLKRST_HPCPU_RESET_CTRL0_REG (DR_REG_LP_CLKRST_BASE + 0x14) |
| 14 | +#define LP_CLKRST_HPCORE0_STAT_VECTOR_SEL (1 << 15) |
| 15 | + |
| 16 | +#define HP_SYS_CLKRST_HP_RST_EN0_REG (DR_REG_HP_SYS_CLKRST_BASE + 0xc0) |
| 17 | +#define HP_SYS_CLKRST_REG_RST_EN_MSPI_AXI (1 << 22) |
| 18 | +#define HP_SYS_CLKRST_REG_RST_EN_MSPI_APB (1 << 24) |
| 19 | + |
| 20 | + |
| 21 | +/* SPIMEM related */ |
| 22 | +#define DR_REG_FLASH_SPI0_BASE (DR_REG_HPPERIPH0_BASE + 0x8C000) |
| 23 | +#define SPI_MEM_C_CACHE_FCTRL_REG (DR_REG_FLASH_SPI0_BASE + 0x3c) |
| 24 | +#define SPI_MEM_C_CLOSE_AXI_INF_EN (1 << 31) |
| 25 | +#define SPI_MEM_C_AXI_REQ_EN (1 << 0) |
| 26 | + |
| 27 | +#define SPI_MEM_C_MMU_ITEM_INDEX_REG (DR_REG_FLASH_SPI0_BASE + 0x380) |
| 28 | +#define SPI_MEM_C_MMU_ITEM_CONTENT_REG (DR_REG_FLASH_SPI0_BASE + 0x37c) |
| 29 | + |
| 30 | +.macro REG_SET_BIT addr, value |
| 31 | + li a0, \addr |
| 32 | + li a1, \value |
| 33 | + lw a2, (a0) |
| 34 | + or a2, a2, a1 |
| 35 | + sw a2, (a0) |
| 36 | +.endm |
| 37 | + |
| 38 | +.macro REG_CLR_BIT addr, value |
| 39 | + li a0, \addr |
| 40 | + /* Since all our parameters will be constants, we can pre-calculate it at assemble time */ |
| 41 | + li a1, ~\value |
| 42 | + lw a2, (a0) |
| 43 | + and a2, a2, a1 |
| 44 | + sw a2, (a0) |
| 45 | +.endm |
| 46 | + |
| 47 | +.macro REG_WRITE addr, value |
| 48 | + li a0, \addr |
| 49 | + li a1, \value |
| 50 | + sw a1, (a0) |
| 51 | +.endm |
| 52 | + |
| 53 | +.macro REG_READ addr |
| 54 | + li a0, \addr |
| 55 | + lw a1, (a0) |
| 56 | +.endm |
| 57 | + |
| 58 | +.macro DELAY_US us |
| 59 | + li t3, (40 * \us) /* CPU @40MHz after reset */ |
| 60 | + csrr t0, cycle |
| 61 | + add t1, t0, t3 |
| 62 | +1: csrr t2, cycle |
| 63 | + blt t2, t1, 1b |
| 64 | +.endm |
| 65 | + |
| 66 | +/** |
| 67 | + * @brief Workaround for MSPI issues on ESP32-P4 revision 3 |
| 68 | + * |
| 69 | + * This function implements a workaround for MSPI-related issues on ESP32-P4 revision 3. |
| 70 | + * It performs 2 flash dummy reads to stabilize the MSPI functionality before jumping to |
| 71 | + * ROM code after deepsleep wakeup. |
| 72 | + */ |
| 73 | +.global p4_rev3_mspi_workaround |
| 74 | +.section .p4_rev3_mspi_workaround.rtc_text,"ax" |
| 75 | + |
| 76 | +p4_rev3_mspi_workaround: |
| 77 | + # Recover the reset vector to HP ROM |
| 78 | + REG_SET_BIT LP_CLKRST_HPCPU_RESET_CTRL0_REG, LP_CLKRST_HPCORE0_STAT_VECTOR_SEL |
| 79 | + |
| 80 | + # Clear the bit to close AXI interface and then set the AXI request enable bit |
| 81 | + REG_CLR_BIT SPI_MEM_C_CACHE_FCTRL_REG, SPI_MEM_C_CLOSE_AXI_INF_EN |
| 82 | + REG_SET_BIT SPI_MEM_C_CACHE_FCTRL_REG, SPI_MEM_C_AXI_REQ_EN |
| 83 | + |
| 84 | + # Set 1 mspi mmu entry for axi addr to flash addr |
| 85 | + REG_WRITE SPI_MEM_C_MMU_ITEM_INDEX_REG, 0 |
| 86 | + REG_WRITE SPI_MEM_C_MMU_ITEM_CONTENT_REG, 0x1000 |
| 87 | + |
| 88 | + # Disable cpu get error response |
| 89 | + REG_WRITE HP_SYSTEM_CORE_ERR_RESP_DIS_REG, 0x7 |
| 90 | + |
| 91 | + # Perform dummy reads |
| 92 | + REG_READ 0x80000000 |
| 93 | + # Perform dummy reads again |
| 94 | + REG_READ 0x80000080 |
| 95 | + |
| 96 | + # Delay 1us to wait MSPI read transmission done |
| 97 | + DELAY_US 1 |
| 98 | + |
| 99 | + # Enable cpu get error response |
| 100 | + REG_WRITE HP_SYSTEM_CORE_ERR_RESP_DIS_REG, 0 |
| 101 | + |
| 102 | + # Reset MSPI AXI and APB interfaces |
| 103 | + REG_SET_BIT HP_SYS_CLKRST_HP_RST_EN0_REG, HP_SYS_CLKRST_REG_RST_EN_MSPI_AXI |
| 104 | + REG_SET_BIT HP_SYS_CLKRST_HP_RST_EN0_REG, HP_SYS_CLKRST_REG_RST_EN_MSPI_APB |
| 105 | + REG_CLR_BIT HP_SYS_CLKRST_HP_RST_EN0_REG, HP_SYS_CLKRST_REG_RST_EN_MSPI_AXI |
| 106 | + REG_CLR_BIT HP_SYS_CLKRST_HP_RST_EN0_REG, HP_SYS_CLKRST_REG_RST_EN_MSPI_APB |
| 107 | + |
| 108 | + # Jump to HP ROM first stage boot code |
| 109 | + li a5, 0x4fc00000 |
| 110 | + jr a5 |
0 commit comments