1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <boot_device.h>
5 #include <spi-generic.h>
8 static struct spi_flash sfg
;
9 static bool sfg_init_done
;
11 static ssize_t
spi_readat(const struct region_device
*rd
, void *b
,
12 size_t offset
, size_t size
)
14 if (spi_flash_read(&sfg
, offset
, size
, b
))
20 static ssize_t
spi_writeat(const struct region_device
*rd
, const void *b
,
21 size_t offset
, size_t size
)
23 if (spi_flash_write(&sfg
, offset
, size
, b
))
29 static ssize_t
spi_eraseat(const struct region_device
*rd
,
30 size_t offset
, size_t size
)
32 if (spi_flash_erase(&sfg
, offset
, size
))
38 static const struct region_device_ops spi_ops
= {
40 .writeat
= spi_writeat
,
41 .eraseat
= spi_eraseat
,
44 static const struct region_device spi_rw
=
45 REGION_DEV_INIT(&spi_ops
, 0, CONFIG_ROM_SIZE
);
47 static void boot_device_rw_init(void)
49 const int bus
= CONFIG_BOOT_DEVICE_SPI_FLASH_BUS
;
52 if (sfg_init_done
== true)
55 /* Ensure any necessary setup is performed by the drivers. */
58 if (!spi_flash_probe(bus
, cs
, &sfg
))
62 const struct region_device
*boot_device_rw(void)
64 /* Probe for the SPI flash device if not already done. */
65 boot_device_rw_init();
67 if (sfg_init_done
!= true)
73 const struct spi_flash
*boot_device_spi_flash(void)
75 boot_device_rw_init();
77 if (sfg_init_done
!= true)
83 int boot_device_wp_region(const struct region_device
*rd
,
84 const enum bootdev_prot_type type
)
88 /* Ensure boot device has been initialized at least once. */
91 const struct spi_flash
*boot_dev
= boot_device_spi_flash();
96 if (type
== MEDIA_WP
) {
97 if (spi_flash_is_write_protected(boot_dev
,
98 region_device_region(rd
)) != 1) {
99 enum spi_flash_status_reg_lockdown lock
=
100 SPI_WRITE_PROTECTION_REBOOT
;
101 if (CONFIG(BOOTMEDIA_SPI_LOCK_REBOOT
))
102 lock
= SPI_WRITE_PROTECTION_REBOOT
;
103 else if (CONFIG(BOOTMEDIA_SPI_LOCK_PIN
))
104 lock
= SPI_WRITE_PROTECTION_PIN
;
105 else if (CONFIG(BOOTMEDIA_SPI_LOCK_PERMANENT
))
106 lock
= SPI_WRITE_PROTECTION_PERMANENT
;
108 return spi_flash_set_write_protected(boot_dev
,
109 region_device_region(rd
), lock
);
112 /* Already write protected */
118 ctrlr_pr
= WRITE_PROTECT
;
121 ctrlr_pr
= READ_PROTECT
;
124 ctrlr_pr
= READ_WRITE_PROTECT
;
130 return spi_flash_ctrlr_protect_region(boot_dev
,
131 region_device_region(rd
), ctrlr_pr
);