Commit 5f6a68a0 authored by Lukas Gehreke's avatar Lukas Gehreke Committed by Mahesh Mahadevan
Browse files

drivers: flash: Added buffered write for addresses in flash on rp2040



It is impossible to perform flash reads during a flash write. When the
data to write to flash lies in the flash itself, it is buffered in ram
before writing.

Signed-off-by: default avatarLukas Gehreke <lk.gehreke@gmail.com>
parent 69e0b03c
Loading
Loading
Loading
Loading
+9 −5
Original line number Diff line number Diff line
@@ -61,6 +61,7 @@ enum outover {
static ssi_hw_t *const ssi = (ssi_hw_t *)SSI_BASE_ADDRESS;
static uint32_t boot2_copyout[BOOT2_SIZE_WORDS];
static bool boot2_copyout_valid;
static uint8_t flash_ram_buffer[PAGE_SIZE];

static void __no_inline_not_in_flash_func(flash_init_boot2_copyout)(void)
{
@@ -223,22 +224,25 @@ static int flash_rpi_write(const struct device *dev, off_t offset, const void *d

	if ((offset & (PAGE_SIZE - 1)) > 0) {
		bytes_to_write = MIN(PAGE_SIZE - (offset & (PAGE_SIZE - 1)), size);
		flash_write_partial(offset, data_pointer, bytes_to_write);
		memcpy(flash_ram_buffer, data_pointer, bytes_to_write);
		flash_write_partial(offset, flash_ram_buffer, bytes_to_write);
		data_pointer += bytes_to_write;
		size -= bytes_to_write;
		offset += bytes_to_write;
	}

	if (size >= PAGE_SIZE) {
		bytes_to_write = size & ~(PAGE_SIZE - 1);
		flash_range_program(offset, data_pointer, bytes_to_write);
	while (size >= PAGE_SIZE) {
		bytes_to_write = PAGE_SIZE;
		memcpy(flash_ram_buffer, data_pointer, bytes_to_write);
		flash_range_program(offset, flash_ram_buffer, bytes_to_write);
		data_pointer += bytes_to_write;
		size -= bytes_to_write;
		offset += bytes_to_write;
	}

	if (size > 0) {
		flash_write_partial(offset, data_pointer, size);
		memcpy(flash_ram_buffer, data_pointer, size);
		flash_write_partial(offset, flash_ram_buffer, size);
	}

	irq_unlock(key);