Skip to content

Commit 647b01b

Browse files
[nrf fromlist] drivers: flash: nrf_qspi_nor: align to errno codes in nrfx
NRFX drivers now return errno codes, aligned driver. Upstream PR #: 97997 Signed-off-by: Michał Stasiak <[email protected]>
1 parent e546727 commit 647b01b

File tree

1 file changed

+42
-80
lines changed

1 file changed

+42
-80
lines changed

drivers/flash/nrf_qspi_nor.c

Lines changed: 42 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -237,32 +237,6 @@ static int exit_dpd(const struct device *const dev);
237237
#define QSPI_IS_SECTOR_ALIGNED(_ofs) (((_ofs) & (QSPI_SECTOR_SIZE - 1U)) == 0)
238238
#define QSPI_IS_BLOCK_ALIGNED(_ofs) (((_ofs) & (QSPI_BLOCK_SIZE - 1U)) == 0)
239239

240-
/**
241-
* @brief Converts NRFX return codes to the zephyr ones
242-
*/
243-
static inline int qspi_get_zephyr_ret_code(nrfx_err_t res)
244-
{
245-
switch (res) {
246-
case NRFX_SUCCESS:
247-
return 0;
248-
case NRFX_ERROR_INVALID_PARAM:
249-
case NRFX_ERROR_INVALID_ADDR:
250-
return -EINVAL;
251-
case NRFX_ERROR_INVALID_STATE:
252-
return -ECANCELED;
253-
#if NRF_ERRATA_STATIC_CHECK(53, 159)
254-
case NRFX_ERROR_FORBIDDEN:
255-
LOG_ERR("nRF5340 anomaly 159 conditions detected");
256-
LOG_ERR("Set the CPU clock to 64 MHz before starting QSPI operation");
257-
return -ECANCELED;
258-
#endif
259-
case NRFX_ERROR_BUSY:
260-
case NRFX_ERROR_TIMEOUT:
261-
default:
262-
return -EBUSY;
263-
}
264-
}
265-
266240
static inline void qspi_lock(const struct device *dev)
267241
{
268242
#ifdef CONFIG_MULTITHREADING
@@ -374,12 +348,11 @@ static void qspi_release(const struct device *dev)
374348
}
375349
}
376350

377-
static inline void qspi_wait_for_completion(const struct device *dev,
378-
nrfx_err_t res)
351+
static inline void qspi_wait_for_completion(const struct device *dev, int res)
379352
{
380353
struct qspi_nor_data *dev_data = dev->data;
381354

382-
if (res == NRFX_SUCCESS) {
355+
if (res == 0) {
383356
#ifdef CONFIG_MULTITHREADING
384357
k_sem_take(&dev_data->sync, K_FOREVER);
385358
#else /* CONFIG_MULTITHREADING */
@@ -475,9 +448,7 @@ static int qspi_send_cmd(const struct device *dev, const struct qspi_cmd *cmd,
475448
.wren = wren,
476449
};
477450

478-
int res = nrfx_qspi_cinstr_xfer(&cinstr_cfg, tx_buf, rx_buf);
479-
480-
return qspi_get_zephyr_ret_code(res);
451+
return nrfx_qspi_cinstr_xfer(&cinstr_cfg, tx_buf, rx_buf);
481452
}
482453

483454
#if !IS_EQUAL(INST_0_QER, JESD216_DW15_QER_VAL_NONE)
@@ -605,7 +576,7 @@ static int qspi_erase(const struct device *dev, uint32_t addr, uint32_t size)
605576
return rc;
606577
}
607578
while (size > 0) {
608-
nrfx_err_t res = !NRFX_SUCCESS;
579+
int res = -1;
609580
uint32_t adj = 0;
610581

611582
if (size == params->size) {
@@ -625,11 +596,11 @@ static int qspi_erase(const struct device *dev, uint32_t addr, uint32_t size)
625596
} else {
626597
/* minimal erase size is at least a sector size */
627598
LOG_ERR("unsupported at 0x%lx size %zu", (long)addr, size);
628-
res = NRFX_ERROR_INVALID_PARAM;
599+
res = -EINVAL;
629600
}
630601

631602
qspi_wait_for_completion(dev, res);
632-
if (res == NRFX_SUCCESS) {
603+
if (res == 0) {
633604
addr += adj;
634605
size -= adj;
635606

@@ -644,7 +615,7 @@ static int qspi_erase(const struct device *dev, uint32_t addr, uint32_t size)
644615
}
645616
} else {
646617
LOG_ERR("erase error at 0x%lx size %zu", (long)addr, size);
647-
rc = qspi_get_zephyr_ret_code(res);
618+
rc = res;
648619
break;
649620
}
650621
}
@@ -779,39 +750,39 @@ static int qspi_sfdp_read(const struct device *dev, off_t offset,
779750
.io2_level = true,
780751
.io3_level = true,
781752
};
782-
nrfx_err_t res;
753+
int res;
783754

784755
qspi_acquire(dev);
785756

786757
res = nrfx_qspi_lfm_start(&cinstr_cfg);
787-
if (res != NRFX_SUCCESS) {
758+
if (res != 0) {
788759
LOG_DBG("lfm_start: %x", res);
789760
goto out;
790761
}
791762

792763
res = nrfx_qspi_lfm_xfer(addr_buf, NULL, sizeof(addr_buf), false);
793-
if (res != NRFX_SUCCESS) {
764+
if (res != 0) {
794765
LOG_DBG("lfm_xfer addr: %x", res);
795766
goto out;
796767
}
797768

798769
res = nrfx_qspi_lfm_xfer(NULL, data, len, true);
799-
if (res != NRFX_SUCCESS) {
770+
if (res != 0) {
800771
LOG_DBG("lfm_xfer read: %x", res);
801772
goto out;
802773
}
803774

804775
out:
805776
qspi_release(dev);
806777

807-
return qspi_get_zephyr_ret_code(res);
778+
return res;
808779
}
809780

810781
#endif /* CONFIG_FLASH_JESD216_API */
811782

812-
static inline nrfx_err_t read_non_aligned(const struct device *dev,
813-
off_t addr,
814-
void *dest, size_t size)
783+
static inline int read_non_aligned(const struct device *dev,
784+
off_t addr,
785+
void *dest, size_t size)
815786
{
816787
uint8_t __aligned(WORD_SIZE) buf[WORD_SIZE * 2];
817788
uint8_t *dptr = dest;
@@ -838,14 +809,14 @@ static inline nrfx_err_t read_non_aligned(const struct device *dev,
838809
flash_suffix = size - flash_prefix - flash_middle;
839810
}
840811

841-
nrfx_err_t res = NRFX_SUCCESS;
812+
int res = 0;
842813

843814
/* read from aligned flash to aligned memory */
844815
if (flash_middle != 0) {
845816
res = nrfx_qspi_read(dptr + dest_prefix, flash_middle,
846817
addr + flash_prefix);
847818
qspi_wait_for_completion(dev, res);
848-
if (res != NRFX_SUCCESS) {
819+
if (res != 0) {
849820
return res;
850821
}
851822

@@ -860,7 +831,7 @@ static inline nrfx_err_t read_non_aligned(const struct device *dev,
860831
res = nrfx_qspi_read(buf, WORD_SIZE, addr -
861832
(WORD_SIZE - flash_prefix));
862833
qspi_wait_for_completion(dev, res);
863-
if (res != NRFX_SUCCESS) {
834+
if (res != 0) {
864835
return res;
865836
}
866837
memcpy(dptr, buf + WORD_SIZE - flash_prefix, flash_prefix);
@@ -871,7 +842,7 @@ static inline nrfx_err_t read_non_aligned(const struct device *dev,
871842
res = nrfx_qspi_read(buf, WORD_SIZE * 2,
872843
addr + flash_prefix + flash_middle);
873844
qspi_wait_for_completion(dev, res);
874-
if (res != NRFX_SUCCESS) {
845+
if (res != 0) {
875846
return res;
876847
}
877848
memcpy(dptr + flash_prefix + flash_middle, buf, flash_suffix);
@@ -884,7 +855,7 @@ static int qspi_nor_read(const struct device *dev, off_t addr, void *dest,
884855
size_t size)
885856
{
886857
const struct qspi_nor_config *params = dev->config;
887-
nrfx_err_t res;
858+
int res;
888859

889860
if (!dest) {
890861
return -EINVAL;
@@ -910,23 +881,23 @@ static int qspi_nor_read(const struct device *dev, off_t addr, void *dest,
910881

911882
qspi_release(dev);
912883

913-
return qspi_get_zephyr_ret_code(res);
884+
return res;
914885
}
915886

916887
/* addr aligned, sptr not null, slen less than 4 */
917-
static inline nrfx_err_t write_sub_word(const struct device *dev, off_t addr,
918-
const void *sptr, size_t slen)
888+
static inline int write_sub_word(const struct device *dev, off_t addr,
889+
const void *sptr, size_t slen)
919890
{
920891
uint8_t __aligned(4) buf[4];
921-
nrfx_err_t res;
892+
int res;
922893

923894
/* read out the whole word so that unchanged data can be
924895
* written back
925896
*/
926897
res = nrfx_qspi_read(buf, sizeof(buf), addr);
927898
qspi_wait_for_completion(dev, res);
928899

929-
if (res == NRFX_SUCCESS) {
900+
if (res == 0) {
930901
memcpy(buf, sptr, slen);
931902
res = nrfx_qspi_write(buf, sizeof(buf), addr);
932903
qspi_wait_for_completion(dev, res);
@@ -943,30 +914,30 @@ BUILD_ASSERT((CONFIG_NORDIC_QSPI_NOR_STACK_WRITE_BUFFER_SIZE % 4) == 0,
943914
*
944915
* If not enabled return the error the peripheral would have produced.
945916
*/
946-
static nrfx_err_t write_through_buffer(const struct device *dev, off_t addr,
917+
static int write_through_buffer(const struct device *dev, off_t addr,
947918
const void *sptr, size_t slen)
948919
{
949-
nrfx_err_t res = NRFX_SUCCESS;
920+
int res = 0;
950921

951922
if (CONFIG_NORDIC_QSPI_NOR_STACK_WRITE_BUFFER_SIZE > 0) {
952923
uint8_t __aligned(4) buf[CONFIG_NORDIC_QSPI_NOR_STACK_WRITE_BUFFER_SIZE];
953924
const uint8_t *sp = sptr;
954925

955-
while ((slen > 0) && (res == NRFX_SUCCESS)) {
926+
while ((slen > 0) && (res == 0)) {
956927
size_t len = MIN(slen, sizeof(buf));
957928

958929
memcpy(buf, sp, len);
959930
res = nrfx_qspi_write(buf, len, addr);
960931
qspi_wait_for_completion(dev, res);
961932

962-
if (res == NRFX_SUCCESS) {
933+
if (res == 0) {
963934
slen -= len;
964935
sp += len;
965936
addr += len;
966937
}
967938
}
968939
} else {
969-
res = NRFX_ERROR_INVALID_ADDR;
940+
res = -EACCES;
970941
}
971942
return res;
972943
}
@@ -1005,19 +976,15 @@ static int qspi_nor_write(const struct device *dev, off_t addr,
1005976

1006977
rc = qspi_nor_write_protection_set(dev, false);
1007978
if (rc == 0) {
1008-
nrfx_err_t res;
1009-
1010979
if (size < 4U) {
1011-
res = write_sub_word(dev, addr, src, size);
980+
rc = write_sub_word(dev, addr, src, size);
1012981
} else if (!nrfx_is_in_ram(src) ||
1013982
!nrfx_is_word_aligned(src)) {
1014-
res = write_through_buffer(dev, addr, src, size);
983+
rc = write_through_buffer(dev, addr, src, size);
1015984
} else {
1016-
res = nrfx_qspi_write(src, size, addr);
1017-
qspi_wait_for_completion(dev, res);
985+
rc = nrfx_qspi_write(src, size, addr);
986+
qspi_wait_for_completion(dev, rc);
1018987
}
1019-
1020-
rc = qspi_get_zephyr_ret_code(res);
1021988
}
1022989

1023990
rc2 = qspi_nor_write_protection_set(dev, true);
@@ -1079,11 +1046,9 @@ static int qspi_init(const struct device *dev)
10791046
{
10801047
const struct qspi_nor_config *dev_config = dev->config;
10811048
uint8_t id[SPI_NOR_MAX_ID_LEN];
1082-
nrfx_err_t res;
10831049
int rc;
10841050

1085-
res = nrfx_qspi_init(&dev_config->nrfx_cfg, qspi_handler, dev->data);
1086-
rc = qspi_get_zephyr_ret_code(res);
1051+
rc = nrfx_qspi_init(&dev_config->nrfx_cfg, qspi_handler, dev->data);
10871052
if (rc < 0) {
10881053
return rc;
10891054
}
@@ -1268,15 +1233,14 @@ static int exit_dpd(const struct device *const dev)
12681233
.op_code = SPI_NOR_CMD_RDPD,
12691234
};
12701235
uint32_t t_exit_dpd = DT_INST_PROP_OR(0, t_exit_dpd, 0);
1271-
nrfx_err_t res;
12721236
int rc;
12731237

12741238
nrf_qspi_pins_get(NRF_QSPI, &pins);
12751239
nrf_qspi_pins_set(NRF_QSPI, &disconnected_pins);
1276-
res = nrfx_qspi_activate(true);
1240+
rc = nrfx_qspi_activate(true);
12771241
nrf_qspi_pins_set(NRF_QSPI, &pins);
12781242

1279-
if (res != NRFX_SUCCESS) {
1243+
if (rc != 0) {
12801244
return -EIO;
12811245
}
12821246

@@ -1300,11 +1264,10 @@ static int exit_dpd(const struct device *const dev)
13001264
static int qspi_suspend(const struct device *dev)
13011265
{
13021266
const struct qspi_nor_config *dev_config = dev->config;
1303-
nrfx_err_t res;
13041267
int rc;
13051268

1306-
res = nrfx_qspi_mem_busy_check();
1307-
if (res != NRFX_SUCCESS) {
1269+
rc = nrfx_qspi_mem_busy_check();
1270+
if (rc != -) {
13081271
return -EBUSY;
13091272
}
13101273

@@ -1321,17 +1284,16 @@ static int qspi_suspend(const struct device *dev)
13211284
static int qspi_resume(const struct device *dev)
13221285
{
13231286
const struct qspi_nor_config *dev_config = dev->config;
1324-
nrfx_err_t res;
13251287
int rc;
13261288

13271289
rc = pinctrl_apply_state(dev_config->pcfg, PINCTRL_STATE_DEFAULT);
13281290
if (rc < 0) {
13291291
return rc;
13301292
}
13311293

1332-
res = nrfx_qspi_init(&dev_config->nrfx_cfg, qspi_handler, dev->data);
1333-
if (res != NRFX_SUCCESS) {
1334-
return -EIO;
1294+
rc = nrfx_qspi_init(&dev_config->nrfx_cfg, qspi_handler, dev->data);
1295+
if (rc < 0) {
1296+
return rc;
13351297
}
13361298

13371299
return exit_dpd(dev);

0 commit comments

Comments
 (0)