Skip to content

Commit 8614671

Browse files
committed
[nrf fromtree] drivers: nrf_qspi_nor: Fix and refactor driver initialization
So far the driver first changed the configuration of the flash chip and after that checked the signature of that chip. This could lead to improper change of the chip configuration if the actually found one was different than that specified in devicetree. This commit reverses the order of these two initialization steps and also restructures a bit the initialization code. Signed-off-by: Andrzej Głąbek <[email protected]> (cherry picked from commit ea1be7f)
1 parent 9a0aca0 commit 8614671

File tree

1 file changed

+45
-80
lines changed

1 file changed

+45
-80
lines changed

drivers/flash/nrf_qspi_nor.c

Lines changed: 45 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -599,41 +599,10 @@ static int qspi_erase(const struct device *dev, uint32_t addr, uint32_t size)
599599
return rc != 0 ? rc : rc2;
600600
}
601601

602-
/* Configures QSPI memory for the transfer */
603-
static int qspi_nrfx_configure(const struct device *dev)
602+
static int configure_chip(const struct device *dev)
604603
{
605-
struct qspi_nor_data *dev_data = dev->data;
606604
const struct qspi_nor_config *dev_config = dev->config;
607-
nrfx_err_t res;
608-
int rc;
609-
610-
res = nrfx_qspi_init(&dev_config->nrfx_cfg, qspi_handler, dev_data);
611-
rc = qspi_get_zephyr_ret_code(res);
612-
if (rc < 0) {
613-
return rc;
614-
}
615-
616-
#if DT_INST_NODE_HAS_PROP(0, rx_delay)
617-
if (!nrf53_errata_121()) {
618-
nrf_qspi_iftiming_set(NRF_QSPI, DT_INST_PROP(0, rx_delay));
619-
}
620-
#endif
621-
622-
/* It may happen that after the flash chip was previously put into
623-
* the DPD mode, the system was reset but the flash chip was not.
624-
* Consequently, the flash chip can be in the DPD mode at this point.
625-
* Some flash chips will just exit the DPD mode on the first CS pulse,
626-
* but some need to receive the dedicated command to do it, so send it.
627-
* This can be the case even if the current image does not have
628-
* CONFIG_PM_DEVICE set to enter DPD mode, as a previously executing image
629-
* (for example the main image if the currently executing image is the
630-
* bootloader) might have set DPD mode before reboot. As a result,
631-
* attempt to exit DPD mode regardless of whether CONFIG_PM_DEVICE is set.
632-
*/
633-
rc = exit_dpd(dev);
634-
if (rc < 0) {
635-
return rc;
636-
}
605+
int rc = 0;
637606

638607
/* Set QE to match transfer mode. If not using quad
639608
* it's OK to leave QE set, but doing so prevents use
@@ -784,33 +753,6 @@ static int qspi_sfdp_read(const struct device *dev, off_t offset,
784753

785754
#endif /* CONFIG_FLASH_JESD216_API */
786755

787-
/**
788-
* @brief Retrieve the Flash JEDEC ID and compare it with the one expected
789-
*
790-
* @param dev The device structure
791-
* @return 0 on success, negative errno code otherwise
792-
*/
793-
static inline int qspi_nor_read_id(const struct device *dev)
794-
{
795-
uint8_t id[SPI_NOR_MAX_ID_LEN];
796-
int rc = qspi_rdid(dev, id);
797-
798-
if (rc != 0) {
799-
return -EIO;
800-
}
801-
802-
const struct qspi_nor_config *qnc = dev->config;
803-
804-
if (memcmp(qnc->id, id, SPI_NOR_MAX_ID_LEN) != 0) {
805-
LOG_ERR("JEDEC id [%02x %02x %02x] expect [%02x %02x %02x]",
806-
id[0], id[1], id[2],
807-
qnc->id[0], qnc->id[1], qnc->id[2]);
808-
return -ENODEV;
809-
}
810-
811-
return 0;
812-
}
813-
814756
static inline nrfx_err_t read_non_aligned(const struct device *dev,
815757
off_t addr,
816758
void *dest, size_t size)
@@ -1077,35 +1019,58 @@ static int qspi_nor_write_protection_set(const struct device *dev,
10771019
return rc;
10781020
}
10791021

1080-
/**
1081-
* @brief Configure the flash
1082-
*
1083-
* @param dev The flash device structure
1084-
* @param info The flash info structure
1085-
* @return 0 on success, negative errno code otherwise
1086-
*/
1087-
static int qspi_nor_configure(const struct device *dev)
1022+
static int qspi_init(const struct device *dev)
10881023
{
1089-
int rc = qspi_nrfx_configure(dev);
1024+
const struct qspi_nor_config *dev_config = dev->config;
1025+
uint8_t id[SPI_NOR_MAX_ID_LEN];
1026+
nrfx_err_t res;
1027+
int rc;
10901028

1091-
if (rc != 0) {
1029+
res = nrfx_qspi_init(&dev_config->nrfx_cfg, qspi_handler, dev->data);
1030+
rc = qspi_get_zephyr_ret_code(res);
1031+
if (rc < 0) {
1032+
return rc;
1033+
}
1034+
1035+
#if DT_INST_NODE_HAS_PROP(0, rx_delay)
1036+
if (!nrf53_errata_121()) {
1037+
nrf_qspi_iftiming_set(NRF_QSPI, DT_INST_PROP(0, rx_delay));
1038+
}
1039+
#endif
1040+
1041+
/* It may happen that after the flash chip was previously put into
1042+
* the DPD mode, the system was reset but the flash chip was not.
1043+
* Consequently, the flash chip can be in the DPD mode at this point.
1044+
* Some flash chips will just exit the DPD mode on the first CS pulse,
1045+
* but some need to receive the dedicated command to do it, so send it.
1046+
* This can be the case even if the current image does not have
1047+
* CONFIG_PM_DEVICE set to enter DPD mode, as a previously executing image
1048+
* (for example the main image if the currently executing image is the
1049+
* bootloader) might have set DPD mode before reboot. As a result,
1050+
* attempt to exit DPD mode regardless of whether CONFIG_PM_DEVICE is set.
1051+
*/
1052+
rc = exit_dpd(dev);
1053+
if (rc < 0) {
1054+
return rc;
1055+
}
1056+
1057+
/* Retrieve the Flash JEDEC ID and compare it with the one expected. */
1058+
rc = qspi_rdid(dev, id);
1059+
if (rc < 0) {
10921060
return rc;
10931061
}
10941062

1095-
/* now the spi bus is configured, we can verify the flash id */
1096-
if (qspi_nor_read_id(dev) != 0) {
1063+
if (memcmp(dev_config->id, id, SPI_NOR_MAX_ID_LEN) != 0) {
1064+
LOG_ERR("JEDEC id [%02x %02x %02x] expect [%02x %02x %02x]",
1065+
id[0], id[1], id[2], dev_config->id[0],
1066+
dev_config->id[1], dev_config->id[2]);
10971067
return -ENODEV;
10981068
}
10991069

1100-
return 0;
1070+
/* The chip is correct, it can be configured now. */
1071+
return configure_chip(dev);
11011072
}
11021073

1103-
/**
1104-
* @brief Initialize and configure the flash
1105-
*
1106-
* @param name The flash name
1107-
* @return 0 on success, negative errno code otherwise
1108-
*/
11091074
static int qspi_nor_init(const struct device *dev)
11101075
{
11111076
const struct qspi_nor_config *dev_config = dev->config;
@@ -1121,7 +1086,7 @@ static int qspi_nor_init(const struct device *dev)
11211086

11221087
qspi_clock_div_change();
11231088

1124-
rc = qspi_nor_configure(dev);
1089+
rc = qspi_init(dev);
11251090

11261091
qspi_clock_div_restore();
11271092

0 commit comments

Comments
 (0)