Skip to content

Commit e5d14c6

Browse files
57300carlescufi
authored andcommitted
riscv: linker: Fallback to Kconfig when defining ROM region
With `CONFIG_XIP=y`, this linker script would derive the ROM region from the chosen `zephyr,flash` DT node with "soc-nv-flash" or "jedec,spi-nor" as its compatible. If the node was absent or had a different compatible, then linking would fail with: undefined symbol `ROM_BASE' referenced in expression Fix this by using `CONFIG_FLASH_BASE_ADDRESS` and `CONFIG_FLASH_SIZE` for ROM base and size respectively. The existing DT logic is preserved for compatibility with out-of-tree boards, so the flash Kconfigs serve as a mere fallback. In addition, use `CONFIG_FLASH_LOAD_OFFSET` and `CONFIG_FLASH_LOAD_SIZE` if defined, to align with some other architectures' linker scripts. For the existing in-tree RISC-V boards, this should not make a difference. The alternative would've been making sure that all boards and SoCs have the relevant Kconfigs set, and only using those in the linker script. The downside is that `CONFIG_FLASH_SIZE` is given in units of 1 KiB, while some existing boards - hifive1_revb, sparkfun_red_v_things_plus - have more granular flash sizes, which would've been rounded down. Signed-off-by: Grzegorz Swiderski <grzegorz.swiderski@nordicsemi.no>
1 parent 5903c7a commit e5d14c6

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

include/zephyr/arch/riscv/common/linker.ld

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,43 @@
3535
#define ROM_END_OFFSET 0
3636
#endif
3737

38+
#if defined(CONFIG_FLASH_LOAD_OFFSET)
39+
#define FLASH_LOAD_OFFSET CONFIG_FLASH_LOAD_OFFSET
40+
#else
41+
#define FLASH_LOAD_OFFSET 0
42+
#endif
43+
3844
#ifdef CONFIG_XIP
45+
46+
#if CONFIG_FLASH_LOAD_SIZE > 0
47+
#define ROM_SIZE (CONFIG_FLASH_LOAD_SIZE - ROM_END_OFFSET)
48+
#endif
49+
3950
#if DT_NODE_HAS_COMPAT_STATUS(DT_CHOSEN(zephyr_flash), soc_nv_flash, okay)
40-
#ifdef CONFIG_FLASH_LOAD_OFFSET
41-
#define ROM_BASE (DT_REG_ADDR(DT_CHOSEN(zephyr_flash)) + \
42-
CONFIG_FLASH_LOAD_OFFSET)
43-
#else /* !CONFIG_FLASH_LOAD_OFFSET */
44-
#define ROM_BASE DT_REG_ADDR(DT_CHOSEN(zephyr_flash))
45-
#endif /* CONFIG_FLASH_LOAD_OFFSET */
51+
#define ROM_BASE (DT_REG_ADDR(DT_CHOSEN(zephyr_flash)) + FLASH_LOAD_OFFSET)
52+
#ifndef ROM_SIZE
4653
#define ROM_SIZE (DT_REG_SIZE(DT_CHOSEN(zephyr_flash)) - ROM_END_OFFSET)
54+
#endif
55+
4756
#elif DT_NODE_HAS_COMPAT(DT_CHOSEN(zephyr_flash), jedec_spi_nor)
4857
/* For jedec,spi-nor we expect the spi controller to memory map the flash
4958
* and for that mapping to be the second register property of the spi
5059
* controller.
5160
*/
5261
#define SPI_CTRL DT_PARENT(DT_CHOSEN(zephyr_flash))
53-
#define ROM_BASE DT_REG_ADDR_BY_IDX(SPI_CTRL, 1)
62+
#define ROM_BASE (DT_REG_ADDR_BY_IDX(SPI_CTRL, 1) + FLASH_LOAD_OFFSET)
63+
#ifndef ROM_SIZE
5464
#define ROM_SIZE (DT_REG_SIZE_BY_IDX(SPI_CTRL, 1) - ROM_END_OFFSET)
5565
#endif
66+
67+
#else /* Use Kconfig to cover the remaining cases */
68+
#define ROM_BASE (CONFIG_FLASH_BASE_ADDRESS + FLASH_LOAD_OFFSET)
69+
#ifndef ROM_SIZE
70+
#define ROM_SIZE (CONFIG_FLASH_SIZE * 1024 - FLASH_LOAD_OFFSET - ROM_END_OFFSET)
71+
#endif
72+
73+
#endif /* DT_NODE_HAS_COMPAT_STATUS */
74+
5675
#else /* CONFIG_XIP */
5776
#define ROM_BASE CONFIG_SRAM_BASE_ADDRESS
5877
#define ROM_SIZE (KB(CONFIG_SRAM_SIZE) - ROM_END_OFFSET)

0 commit comments

Comments
 (0)