Skip to content

Commit 72d7a0f

Browse files
Kevin Wangkartben
authored andcommitted
drivers: flash: andes_qspi: Fix the bug when rx length exceeds 512 bytes
ATCSPI hardware limits single transfer to 512 bytes, so when reading data over 512 bytes, it needs to be split into multiple transfers Signed-off-by: Kevin Wang <[email protected]>
1 parent 3d372c0 commit 72d7a0f

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

drivers/flash/flash_andes_qspi.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
LOG_MODULE_REGISTER(flash_andes, CONFIG_FLASH_LOG_LEVEL);
2323

24+
/* ATCSPI200 transfer count is limited to 512 bytes. */
25+
#define MAX_TRANSFER_CNT (512)
26+
2427
/* Indicates that an access command includes bytes for the address.
2528
* If not provided the opcode is not followed by address bytes.
2629
*/
@@ -304,6 +307,8 @@ static int flash_andes_qspi_read(const struct device *dev,
304307
off_t addr, void *dest, size_t size)
305308
{
306309
const size_t flash_size = dev_flash_size(dev);
310+
size_t to_read = size;
311+
307312
int ret;
308313

309314
/* should be between 0 and flash size */
@@ -317,8 +322,23 @@ static int flash_andes_qspi_read(const struct device *dev,
317322

318323
acquire_device(dev);
319324

320-
ret = flash_andes_qspi_cmd_addr_read(dev,
321-
FLASH_ANDES_CMD_4READ, addr, dest, size);
325+
do {
326+
/* Get the adequate size to receive */
327+
to_read = MIN(MAX_TRANSFER_CNT, size);
328+
329+
ret = flash_andes_qspi_cmd_addr_read(dev,
330+
FLASH_ANDES_CMD_4READ, addr, dest, to_read);
331+
332+
if (ret != 0) {
333+
break;
334+
}
335+
336+
size -= to_read;
337+
dest = (uint8_t *)dest + to_read;
338+
addr += to_read;
339+
340+
flash_andes_qspi_wait_until_ready(dev);
341+
} while (size > 0);
322342

323343
release_device(dev);
324344
return ret;
@@ -353,6 +373,8 @@ static int flash_andes_qspi_write(const struct device *dev, off_t addr,
353373
/* Get the adequate size to send*/
354374
to_write = MIN(page_size - (addr % page_size), size);
355375

376+
flash_andes_qspi_cmd_write(dev, FLASH_ANDES_CMD_WREN);
377+
356378
ret = flash_andes_qspi_cmd_addr_write(dev,
357379
FLASH_ANDES_CMD_4PP, addr, src, to_write);
358380

@@ -880,7 +902,7 @@ flash_andes_qspi_get_parameters(const struct device *dev)
880902
return &config->parameters;
881903
}
882904

883-
static DEVICE_API(flash, flash_andes_qspi_api) = {
905+
static const struct flash_driver_api flash_andes_qspi_api = {
884906
.read = flash_andes_qspi_read,
885907
.write = flash_andes_qspi_write,
886908
.erase = flash_andes_qspi_erase,

0 commit comments

Comments
 (0)