1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <boot_device.h>
6 #include <device/mmio.h>
7 #include <soc/addressmap.h>
10 #include <console/console.h>
11 #include <spi_sdcard.h>
13 /* follow is the FSBL boot device defined by ZSBL of sifive
14 * FSBL replaced by bootblock of coreboot
15 * MSEL_SPInx1 -> test if boot from memory-mapped on SPIn
16 * MSEL_SPInx4 -> test if boot from memory-mapped on QPIn
17 * MSEL_SPInSD -> test if boot from sdcard mount on SPIn */
18 #define MSEL_SPI0x1(m) (((m) == 5) || ((m) == 14))
19 #define MSEL_SPI0x4(m) (((m) == 6) || ((m) == 10) || ((m) == 15))
20 #define MSEL_SPI1x1(m) ((m) == 12)
21 #define MSEL_SPI1x4(m) (((m) == 7) || ((m) == 13))
22 #define MSEL_SPI1SD(m) ((m) == 8)
23 #define MSEL_SPI2x1(m) ((m) == 9)
24 #define MSEL_SPI2SD(m) ((m) == 11)
26 static struct spi_sdcard card
;
28 /* At 0x20000000: A 256MiB long memory-mapped view of the flash at QSPI0 */
29 static struct mem_region_device spi_mdev
=
30 MEM_REGION_DEV_RO_INIT((void *)0x20000000, CONFIG_ROM_SIZE
);
32 static ssize_t
unleashed_sd_readat(const struct region_device
*rdev
, void *dest
,
33 size_t offset
, size_t count
)
35 spi_sdcard_read(&card
, dest
, offset
, count
);
39 static const struct region_device_ops unleashed_sd_ops
= {
40 .mmap
= mmap_helper_rdev_mmap
,
41 .munmap
= mmap_helper_rdev_munmap
,
42 .readat
= unleashed_sd_readat
,
45 static struct mmap_helper_region_device sd_mdev
=
46 MMAP_HELPER_DEV_INIT(&unleashed_sd_ops
, 0, CONFIG_ROM_SIZE
, &cbfs_cache
);
48 const struct region_device
*boot_device_ro(void)
50 uint32_t m
= read32((uint32_t *)FU540_MSEL
);
51 if (MSEL_SPI0x1(m
) || MSEL_SPI0x4(m
))
52 return &spi_mdev
.rdev
;
55 die("Wrong configuration of MSEL\n");
59 static const struct fu540_spi_mmap_config spi_mmap_config
= {
63 .cmd_proto
= FU540_SPI_PROTO_S
,
64 .addr_proto
= FU540_SPI_PROTO_Q
,
65 .data_proto
= FU540_SPI_PROTO_Q
,
70 void boot_device_init(void)
72 uint32_t m
= read32((uint32_t *)FU540_MSEL
);
73 if (MSEL_SPI0x1(m
) || MSEL_SPI0x4(m
)) {
74 struct spi_slave slave
;
75 /* initialize spi controller */
76 spi_setup_slave(0, 0, &slave
);
77 /* map flash to memory space */
78 fu540_spi_mmap(&slave
, &spi_mmap_config
);
82 spi_sdcard_init(&card
, 2, 0);
85 die("Wrong configuration of MSEL\n");