drivers: flash: npcx: Fix offset alignment check bug

Removed single offset alignment check mechanism and replaced it with
offset alignment check for each iteration

Signed-off-by: Madhurima Paruchuri <mparuchuri@google.com>
This commit is contained in:
Madhurima Paruchuri 2023-08-03 09:28:22 +00:00 committed by Carles Cufí
commit 3bc694dd8d

View file

@ -258,8 +258,6 @@ static int flash_npcx_nor_erase(const struct device *dev, off_t addr, size_t siz
{ {
const struct flash_npcx_nor_config *config = dev->config; const struct flash_npcx_nor_config *config = dev->config;
int ret = 0; int ret = 0;
bool offset_4k_aligned = SPI_NOR_IS_SECTOR_ALIGNED(addr);
bool offset_64k_aligned = SPI_NOR_IS_64K_ALIGNED(addr);
/* Out of the region of nor flash device? */ /* Out of the region of nor flash device? */
if (!is_within_region(addr, size, 0, config->flash_size)) { if (!is_within_region(addr, size, 0, config->flash_size)) {
@ -268,7 +266,7 @@ static int flash_npcx_nor_erase(const struct device *dev, off_t addr, size_t siz
} }
/* address must be sector-aligned */ /* address must be sector-aligned */
if (!offset_4k_aligned) { if (!SPI_NOR_IS_SECTOR_ALIGNED(addr)) {
LOG_ERR("Addr %ld is not sector-aligned", addr); LOG_ERR("Addr %ld is not sector-aligned", addr);
return -EINVAL; return -EINVAL;
} }
@ -290,16 +288,14 @@ static int flash_npcx_nor_erase(const struct device *dev, off_t addr, size_t siz
while (size > 0) { while (size > 0) {
flash_npcx_uma_cmd_only(dev, SPI_NOR_CMD_WREN); flash_npcx_uma_cmd_only(dev, SPI_NOR_CMD_WREN);
/* Send page/block erase command with addr */ /* Send page/block erase command with addr */
if ((size >= BLOCK_64K_SIZE) && offset_64k_aligned) { if ((size >= BLOCK_64K_SIZE) && SPI_NOR_IS_64K_ALIGNED(addr)) {
flash_npcx_uma_cmd_by_addr(dev, SPI_NOR_CMD_BE, addr); flash_npcx_uma_cmd_by_addr(dev, SPI_NOR_CMD_BE, addr);
addr += BLOCK_64K_SIZE; addr += BLOCK_64K_SIZE;
size -= BLOCK_64K_SIZE; size -= BLOCK_64K_SIZE;
} else if ((size >= BLOCK_4K_SIZE) && offset_4k_aligned) { } else {
flash_npcx_uma_cmd_by_addr(dev, SPI_NOR_CMD_SE, addr); flash_npcx_uma_cmd_by_addr(dev, SPI_NOR_CMD_SE, addr);
addr += BLOCK_4K_SIZE; addr += BLOCK_4K_SIZE;
size -= BLOCK_4K_SIZE; size -= BLOCK_4K_SIZE;
} else {
return -EINVAL;
} }
ret = flash_npcx_nor_wait_until_ready(dev); ret = flash_npcx_nor_wait_until_ready(dev);
if (ret != 0) { if (ret != 0) {