Skip to content

Commit a512e75

Browse files
pabigotmbolivar-nordic
authored andcommitted
[nrf temphack] drivers: flash: lock around read/write pair
A write of sub-word data requires reading the content currently at the target offset so that a word-sized write doesn't change it. Correctness requires no other thread be given an opportunity to change that data before the subsequent write is performed. Extend the wait API to allow a lock to be retained after a failed operation. Rework the write control flow to allow success in the read to continue to the write without unlocking, while failure in the read bypasses the write. Signed-off-by: Peter A. Bigot <[email protected]> (cherry picked from commit b477654)
1 parent cd30f28 commit a512e75

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

drivers/flash/nrf_qspi_nor.c

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -562,15 +562,6 @@ static int qspi_nor_write(struct device *dev, off_t addr, const void *src,
562562
if (size < 4U) {
563563
src = buf;
564564
size = sizeof(buf);
565-
/* read out the whole word so that unchanged data can be
566-
* written back
567-
*/
568-
nrfx_err_t res = nrfx_qspi_read(buf, size, addr);
569-
570-
if (res != 0) {
571-
return qspi_get_zephyr_ret_code(res);
572-
}
573-
memcpy(buf, sptr, dlen);
574565
} else if ((size % 4U) != 0) {
575566
return -EINVAL;
576567
}
@@ -595,7 +586,20 @@ static int qspi_nor_write(struct device *dev, off_t addr, const void *src,
595586
}
596587

597588

598-
nrfx_err_t res;
589+
nrfx_err_t res = NRFX_SUCCESS;
590+
591+
qspi_lock(dev);
592+
593+
if (sptr != src) {
594+
/* read out the whole word so that unchanged data can be
595+
* written back
596+
*/
597+
res = nrfx_qspi_read(buf, size, addr);
598+
qspi_wait_for_completion(dev, res);
599+
if (res == NRFX_SUCCESS) {
600+
memcpy(buf, sptr, dlen);
601+
}
602+
}
599603

600604
/* if the data smaller than a whole word the function already copies
601605
* the data in ram
@@ -604,20 +608,20 @@ static int qspi_nor_write(struct device *dev, off_t addr, const void *src,
604608
/* pre-read failed */
605609
} else if (IS_ENABLED(CONFIG_NORDIC_QSPI_NOR_FLASH_ALLOW_STACK_USAGE_FOR_DATA_IN_FLASH) &&
606610
((u32_t)sptr < CONFIG_SRAM_BASE_ADDRESS) && !(size < 4U)) {
607-
for (size_t bytes_written = 0; bytes_written < dlen;) {
611+
size_t bytes_written = 0;
612+
613+
while ((res == NRFX_SUCCESS)
614+
&& (bytes_written < dlen)) {
608615
memcpy(buf, ((u8_t *)sptr + bytes_written),
609616
sizeof(buf));
610-
qspi_lock(dev);
611617
res = nrfx_qspi_write(buf, sizeof(buf),
612618
addr + bytes_written);
613619
qspi_wait_for_completion(dev, res);
614-
if (res != NRFX_SUCCESS) {
615-
return qspi_get_zephyr_ret_code(res);
620+
if (res == NRFX_SUCCESS) {
621+
bytes_written += sizeof(buf);
616622
}
617-
bytes_written += sizeof(buf);
618623
}
619624
} else {
620-
qspi_lock(dev);
621625
res = nrfx_qspi_write(src, size, addr);
622626
qspi_wait_for_completion(dev, res);
623627
}

0 commit comments

Comments
 (0)