@@ -307,6 +307,32 @@ static int spi_nor_access(const struct device *const dev,
307307#define spi_nor_cmd_addr_write (dev , opcode , addr , src , length ) \
308308 spi_nor_access(dev, opcode, true, addr, (void *)src, length, true)
309309
310+ /**
311+ * @brief Wait until the flash is ready
312+ *
313+ * @note The device must be externally acquired before invoking this
314+ * function.
315+ *
316+ * This function should be invoked after every ERASE, PROGRAM, or
317+ * WRITE_STATUS operation before continuing. This allows us to assume
318+ * that the device is ready to accept new commands at any other point
319+ * in the code.
320+ *
321+ * @param dev The device structure
322+ * @return 0 on success, negative errno code otherwise
323+ */
324+ static int spi_nor_wait_until_ready (const struct device * dev )
325+ {
326+ int ret ;
327+ uint8_t reg ;
328+
329+ do {
330+ ret = spi_nor_cmd_read (dev , SPI_NOR_CMD_RDSR , & reg , sizeof (reg ));
331+ } while (!ret && (reg & SPI_NOR_WIP_BIT ));
332+
333+ return ret ;
334+ }
335+
310336#if defined(CONFIG_SPI_NOR_SFDP_RUNTIME ) || defined(CONFIG_FLASH_JESD216_API )
311337/*
312338 * @brief Read content from the SFDP hierarchy
@@ -430,24 +456,6 @@ static void release_device(const struct device *dev)
430456 }
431457}
432458
433- /**
434- * @brief Wait until the flash is ready
435- *
436- * @param dev The device structure
437- * @return 0 on success, negative errno code otherwise
438- */
439- static int spi_nor_wait_until_ready (const struct device * dev )
440- {
441- int ret ;
442- uint8_t reg ;
443-
444- do {
445- ret = spi_nor_cmd_read (dev , SPI_NOR_CMD_RDSR , & reg , 1 );
446- } while (!ret && (reg & SPI_NOR_WIP_BIT ));
447-
448- return ret ;
449- }
450-
451459static int spi_nor_read (const struct device * dev , off_t addr , void * dest ,
452460 size_t size )
453461{
@@ -461,8 +469,6 @@ static int spi_nor_read(const struct device *dev, off_t addr, void *dest,
461469
462470 acquire_device (dev );
463471
464- spi_nor_wait_until_ready (dev );
465-
466472 ret = spi_nor_cmd_addr_read (dev , SPI_NOR_CMD_READ , addr , dest , size );
467473
468474 release_device (dev );
@@ -600,8 +606,6 @@ static int spi_nor_write_protection_set(const struct device *dev,
600606{
601607 int ret ;
602608
603- spi_nor_wait_until_ready (dev );
604-
605609 ret = spi_nor_cmd_write (dev , (write_protect ) ?
606610 SPI_NOR_CMD_WRDI : SPI_NOR_CMD_WREN );
607611
@@ -621,8 +625,6 @@ static int spi_nor_sfdp_read(const struct device *dev, off_t addr,
621625{
622626 acquire_device (dev );
623627
624- spi_nor_wait_until_ready (dev );
625-
626628 int ret = read_sfdp (dev , addr , dest , size );
627629
628630 release_device (dev );
@@ -641,8 +643,6 @@ static int spi_nor_read_jedec_id(const struct device *dev,
641643
642644 acquire_device (dev );
643645
644- spi_nor_wait_until_ready (dev );
645-
646646 int ret = spi_nor_cmd_read (dev , SPI_NOR_CMD_RDID , id , SPI_NOR_MAX_ID_LEN );
647647
648648 release_device (dev );
0 commit comments