Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mtd/w25q: ensure the correct behavior if erase sector fails #15812

Merged
merged 1 commit into from
Feb 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions drivers/mtd/w25qxxxjv.c
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,7 @@ static int w25qxxxjv_erase_sector(FAR struct w25qxxxjv_dev_s *priv,
{
off_t address;
uint8_t status;
uint16_t nloops = priv->nsectors;

finfo("sector: %08" PRIxOFF "\n", sector);

Expand All @@ -976,10 +977,16 @@ static int w25qxxxjv_erase_sector(FAR struct w25qxxxjv_dev_s *priv,
}

status = w25qxxxjv_read_status(priv);
if ((status & STATUS_BUSY_MASK) != STATUS_READY)
while ((status & STATUS_BUSY_MASK) != STATUS_READY)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@michallenc should be nice to have a timeout here. Could be just a counter, it will avoid the system get blocked forever case something bad happens

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@acassis Probably yes. The advantage is we know the longest possible delay (entire flash erase time), so I put a counter there with this time.

{
ferr("ERROR: Flash busy: %02x", status);
return -EBUSY;
if (nloops-- == 0)
{
ferr("ERROR: Flash busy: %02x", status);
return -EBUSY;
}

nxsig_usleep(priv->erasetime * 1000);
status = w25qxxxjv_read_status(priv);
}

if ((status & priv->protectmask) != 0 &&
Expand Down Expand Up @@ -1352,9 +1359,7 @@ static int w25qxxxjv_erase(FAR struct mtd_dev_s *dev, off_t startblock,
{
FAR struct w25qxxxjv_dev_s *priv = (FAR struct w25qxxxjv_dev_s *)dev;
size_t blocksleft = nblocks;
#ifdef CONFIG_W25QXXXJV_SECTOR512
int ret;
#endif

finfo("startblock: %08" PRIxOFF " nblocks: %d\n",
startblock, (int)nblocks);
Expand All @@ -1370,7 +1375,13 @@ static int w25qxxxjv_erase(FAR struct mtd_dev_s *dev, off_t startblock,
#ifdef CONFIG_W25QXXXJV_SECTOR512
w25qxxxjv_erase_cache(priv, startblock);
#else
w25qxxxjv_erase_sector(priv, startblock);
ret = w25qxxxjv_erase_sector(priv, startblock);
if (ret < 0)
{
w25qxxxjv_unlock(priv->qspi);
return ret;
}

#endif
startblock++;
}
Expand Down
Loading