[3/3] mtd: spinand: Wait for the erase op to finish before writing a bad block marker
diff mbox series

Message ID 20200211163452.25442-4-frieder.schrempf@kontron.de
State New
Headers show
Series
  • mtd: spinand: Fix reading and writing of bad block markers
Related show

Commit Message

Schrempf Frieder Feb. 11, 2020, 4:35 p.m. UTC
From: Frieder Schrempf <frieder.schrempf@kontron.de>

Currently when marking a block, we use spinand_erase_op() to erase
the block before writing the marker to the OOB area without waiting
for the operation to succeed. This can lead to the marking failing
silently and no bad block marker being written to the flash.

To fix this we reuse the spinand_erase() function, that already does
everything we need to do before actually writing the marker.

Fixes: 7529df465248 ("mtd: nand: Add core infrastructure to support SPI NANDs")
Cc: stable@vger.kernel.org
Signed-off-by: Frieder Schrempf <frieder.schrempf@kontron.de>
---
 drivers/mtd/nand/spi/core.c | 56 ++++++++++++++++++-------------------
 1 file changed, 28 insertions(+), 28 deletions(-)

Comments

Miquel Raynal Feb. 17, 2020, 10:39 a.m. UTC | #1
Hi Frieder,

Schrempf Frieder <frieder.schrempf@kontron.de> wrote on Tue, 11 Feb
2020 16:35:53 +0000:

> From: Frieder Schrempf <frieder.schrempf@kontron.de>
> 
> Currently when marking a block, we use spinand_erase_op() to erase
> the block before writing the marker to the OOB area without waiting
> for the operation to succeed. This can lead to the marking failing
> silently and no bad block marker being written to the flash.
> 
> To fix this we reuse the spinand_erase() function, that already does
> everything we need to do before actually writing the marker.
> 

Thanks a lot for this series!

Yet I don't really understand the point of waiting for the erasure if
it failed: we don't really care as programming (1 -> 0) cells is always
possible. Are you sure this lead to an error?

Also, why just not calling spinand_erase() instead of
spinand_erase_op() from spinand_markbad()?

> Fixes: 7529df465248 ("mtd: nand: Add core infrastructure to support SPI NANDs")
> Cc: stable@vger.kernel.org
> Signed-off-by: Frieder Schrempf <frieder.schrempf@kontron.de>
> ---
>  drivers/mtd/nand/spi/core.c | 56 ++++++++++++++++++-------------------
>  1 file changed, 28 insertions(+), 28 deletions(-)
> 
> diff --git a/drivers/mtd/nand/spi/core.c b/drivers/mtd/nand/spi/core.c
> index 925db6269861..8a69d13639e2 100644
> --- a/drivers/mtd/nand/spi/core.c
> +++ b/drivers/mtd/nand/spi/core.c
> @@ -600,6 +600,32 @@ static int spinand_mtd_block_isbad(struct mtd_info *mtd, loff_t offs)
>  	return ret;
>  }
>  
> +static int __spinand_erase(struct nand_device *nand, const struct nand_pos *pos,
> +			   bool hard_fail)
> +{
> +	struct spinand_device *spinand = nand_to_spinand(nand);
> +	u8 status;
> +	int ret;
> +
> +	ret = spinand_select_target(spinand, pos->target);
> +	if (ret)
> +		return ret;
> +
> +	ret = spinand_write_enable_op(spinand);
> +	if (ret)
> +		return ret;
> +
> +	ret = spinand_erase_op(spinand, pos);
> +	if (ret && hard_fail)
> +		return ret;
> +
> +	ret = spinand_wait(spinand, &status);
> +	if (!ret && (status & STATUS_ERASE_FAILED))
> +		ret = -EIO;
> +
> +	return ret;
> +}
> +
>  static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos)
>  {
>  	struct spinand_device *spinand = nand_to_spinand(nand);
> @@ -614,16 +640,10 @@ static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos)
>  	int ret;
>  
>  	/* Erase block before marking it bad. */
> -	ret = spinand_select_target(spinand, pos->target);
> -	if (ret)
> -		return ret;
> -
> -	ret = spinand_write_enable_op(spinand);
> +	ret = __spinand_erase(nand, pos, false);
>  	if (ret)
>  		return ret;
>  
> -	spinand_erase_op(spinand, pos);
> -
>  	return spinand_write_page(spinand, &req);
>  }
>  
> @@ -644,27 +664,7 @@ static int spinand_mtd_block_markbad(struct mtd_info *mtd, loff_t offs)
>  
>  static int spinand_erase(struct nand_device *nand, const struct nand_pos *pos)
>  {
> -	struct spinand_device *spinand = nand_to_spinand(nand);
> -	u8 status;
> -	int ret;
> -
> -	ret = spinand_select_target(spinand, pos->target);
> -	if (ret)
> -		return ret;
> -
> -	ret = spinand_write_enable_op(spinand);
> -	if (ret)
> -		return ret;
> -
> -	ret = spinand_erase_op(spinand, pos);
> -	if (ret)
> -		return ret;
> -
> -	ret = spinand_wait(spinand, &status);
> -	if (!ret && (status & STATUS_ERASE_FAILED))
> -		ret = -EIO;
> -
> -	return ret;
> +	return __spinand_erase(nand, pos, true);
>  }
>  
>  static int spinand_mtd_erase(struct mtd_info *mtd,

Thanks,
Miquèl
Boris Brezillon Feb. 17, 2020, 11:14 a.m. UTC | #2
On Mon, 17 Feb 2020 11:39:19 +0100
Miquel Raynal <miquel.raynal@bootlin.com> wrote:

> Hi Frieder,
> 
> Schrempf Frieder <frieder.schrempf@kontron.de> wrote on Tue, 11 Feb
> 2020 16:35:53 +0000:
> 
> > From: Frieder Schrempf <frieder.schrempf@kontron.de>
> > 
> > Currently when marking a block, we use spinand_erase_op() to erase
> > the block before writing the marker to the OOB area without waiting
> > for the operation to succeed. This can lead to the marking failing
> > silently and no bad block marker being written to the flash.
> > 
> > To fix this we reuse the spinand_erase() function, that already does
> > everything we need to do before actually writing the marker.
> >   
> 
> Thanks a lot for this series!
> 
> Yet I don't really understand the point of waiting for the erasure if
> it failed: we don't really care as programming (1 -> 0) cells is always
> possible. Are you sure this lead to an error?

Actually, I think I already pointed out that we should probably write
the BBM without erasing the block. IIRC, this logic has been copied
from rawnand where some controllers don't disable the ECC engine when
doing raw accesses, leading to ECC errors if the block is not erased
before BBMs are programmed. Assuming we don't let such drivers being
merged in spinand, this erase operation can be dropped.

> 
> Also, why just not calling spinand_erase() instead of
> spinand_erase_op() from spinand_markbad()?
> 
> > Fixes: 7529df465248 ("mtd: nand: Add core infrastructure to support SPI NANDs")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Frieder Schrempf <frieder.schrempf@kontron.de>
> > ---
> >  drivers/mtd/nand/spi/core.c | 56 ++++++++++++++++++-------------------
> >  1 file changed, 28 insertions(+), 28 deletions(-)
> > 
> > diff --git a/drivers/mtd/nand/spi/core.c b/drivers/mtd/nand/spi/core.c
> > index 925db6269861..8a69d13639e2 100644
> > --- a/drivers/mtd/nand/spi/core.c
> > +++ b/drivers/mtd/nand/spi/core.c
> > @@ -600,6 +600,32 @@ static int spinand_mtd_block_isbad(struct mtd_info *mtd, loff_t offs)
> >  	return ret;
> >  }
> >  
> > +static int __spinand_erase(struct nand_device *nand, const struct nand_pos *pos,
> > +			   bool hard_fail)

I hate those __ prefix. Please find a more descriptive name
(spinand_erase_block() or spinand_erase_and_wait()?)

> > +{
> > +	struct spinand_device *spinand = nand_to_spinand(nand);
> > +	u8 status;
> > +	int ret;
> > +
> > +	ret = spinand_select_target(spinand, pos->target);
> > +	if (ret)
> > +		return ret;
> > +
> > +	ret = spinand_write_enable_op(spinand);
> > +	if (ret)
> > +		return ret;
> > +
> > +	ret = spinand_erase_op(spinand, pos);
> > +	if (ret && hard_fail)
> > +		return ret;
> > +
> > +	ret = spinand_wait(spinand, &status);
> > +	if (!ret && (status & STATUS_ERASE_FAILED))
> > +		ret = -EIO;
> > +
> > +	return ret;
> > +}
> > +
> >  static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos)
> >  {
> >  	struct spinand_device *spinand = nand_to_spinand(nand);
> > @@ -614,16 +640,10 @@ static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos)
> >  	int ret;
> >  
> >  	/* Erase block before marking it bad. */
> > -	ret = spinand_select_target(spinand, pos->target);
> > -	if (ret)
> > -		return ret;
> > -
> > -	ret = spinand_write_enable_op(spinand);
> > +	ret = __spinand_erase(nand, pos, false);
> >  	if (ret)
> >  		return ret;
> >  
> > -	spinand_erase_op(spinand, pos);
> > -
> >  	return spinand_write_page(spinand, &req);
> >  }
> >  
> > @@ -644,27 +664,7 @@ static int spinand_mtd_block_markbad(struct mtd_info *mtd, loff_t offs)
> >  
> >  static int spinand_erase(struct nand_device *nand, const struct nand_pos *pos)
> >  {
> > -	struct spinand_device *spinand = nand_to_spinand(nand);
> > -	u8 status;
> > -	int ret;
> > -
> > -	ret = spinand_select_target(spinand, pos->target);
> > -	if (ret)
> > -		return ret;
> > -
> > -	ret = spinand_write_enable_op(spinand);
> > -	if (ret)
> > -		return ret;
> > -
> > -	ret = spinand_erase_op(spinand, pos);
> > -	if (ret)
> > -		return ret;
> > -
> > -	ret = spinand_wait(spinand, &status);
> > -	if (!ret && (status & STATUS_ERASE_FAILED))
> > -		ret = -EIO;
> > -
> > -	return ret;
> > +	return __spinand_erase(nand, pos, true);
> >  }
> >  
> >  static int spinand_mtd_erase(struct mtd_info *mtd,  
> 
> Thanks,
> Miquèl
Schrempf Frieder Feb. 17, 2020, 11:19 a.m. UTC | #3
Hi Miquel,

On 17.02.20 11:39, Miquel Raynal wrote:
> Hi Frieder,
> 
> Schrempf Frieder <frieder.schrempf@kontron.de> wrote on Tue, 11 Feb
> 2020 16:35:53 +0000:
> 
>> From: Frieder Schrempf <frieder.schrempf@kontron.de>
>>
>> Currently when marking a block, we use spinand_erase_op() to erase
>> the block before writing the marker to the OOB area without waiting
>> for the operation to succeed. This can lead to the marking failing
>> silently and no bad block marker being written to the flash.
>>
>> To fix this we reuse the spinand_erase() function, that already does
>> everything we need to do before actually writing the marker.
>>
> 
> Thanks a lot for this series!
> 
> Yet I don't really understand the point of waiting for the erasure if
> it failed: we don't really care as programming (1 -> 0) cells is always
> possible. Are you sure this lead to an error?

We don't care about the result of the erase operation, but I think we 
still need to wait for it to be done and the STATUS_BUSY bit to be 
cleared. Otherwise it seems like the program operation to set the marker 
can get ignored by the chip. At least that's my explanation for the 
behavior I was observing.

> 
> Also, why just not calling spinand_erase() instead of
> spinand_erase_op() from spinand_markbad()?

Yeah, that's wrong. I was thinking, that spinand_erase_op() would return 
an error if the erase failed on the flash level (which should have been 
ignored in this case). The current code suggested this as it doesn't 
handle the return value of spinand_erase_op().

But in fact the success of the erase operation is only checked in 
spinand_erase() after waiting for the flash to be ready. So I think we 
can use spinand_erase(), but instead of ignoring the return value of 
spinand_erase_op(), we need to ignore the 'status & STATUS_ERASE_FAILED' 
check.

Thanks,
Frieder

> 
>> Fixes: 7529df465248 ("mtd: nand: Add core infrastructure to support SPI NANDs")
>> Cc: stable@vger.kernel.org
>> Signed-off-by: Frieder Schrempf <frieder.schrempf@kontron.de>
>> ---
>>   drivers/mtd/nand/spi/core.c | 56 ++++++++++++++++++-------------------
>>   1 file changed, 28 insertions(+), 28 deletions(-)
>>
>> diff --git a/drivers/mtd/nand/spi/core.c b/drivers/mtd/nand/spi/core.c
>> index 925db6269861..8a69d13639e2 100644
>> --- a/drivers/mtd/nand/spi/core.c
>> +++ b/drivers/mtd/nand/spi/core.c
>> @@ -600,6 +600,32 @@ static int spinand_mtd_block_isbad(struct mtd_info *mtd, loff_t offs)
>>   	return ret;
>>   }
>>   
>> +static int __spinand_erase(struct nand_device *nand, const struct nand_pos *pos,
>> +			   bool hard_fail)
>> +{
>> +	struct spinand_device *spinand = nand_to_spinand(nand);
>> +	u8 status;
>> +	int ret;
>> +
>> +	ret = spinand_select_target(spinand, pos->target);
>> +	if (ret)
>> +		return ret;
>> +
>> +	ret = spinand_write_enable_op(spinand);
>> +	if (ret)
>> +		return ret;
>> +
>> +	ret = spinand_erase_op(spinand, pos);
>> +	if (ret && hard_fail)
>> +		return ret;
>> +
>> +	ret = spinand_wait(spinand, &status);
>> +	if (!ret && (status & STATUS_ERASE_FAILED))
>> +		ret = -EIO;
>> +
>> +	return ret;
>> +}
>> +
>>   static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos)
>>   {
>>   	struct spinand_device *spinand = nand_to_spinand(nand);
>> @@ -614,16 +640,10 @@ static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos)
>>   	int ret;
>>   
>>   	/* Erase block before marking it bad. */
>> -	ret = spinand_select_target(spinand, pos->target);
>> -	if (ret)
>> -		return ret;
>> -
>> -	ret = spinand_write_enable_op(spinand);
>> +	ret = __spinand_erase(nand, pos, false);
>>   	if (ret)
>>   		return ret;
>>   
>> -	spinand_erase_op(spinand, pos);
>> -
>>   	return spinand_write_page(spinand, &req);
>>   }
>>   
>> @@ -644,27 +664,7 @@ static int spinand_mtd_block_markbad(struct mtd_info *mtd, loff_t offs)
>>   
>>   static int spinand_erase(struct nand_device *nand, const struct nand_pos *pos)
>>   {
>> -	struct spinand_device *spinand = nand_to_spinand(nand);
>> -	u8 status;
>> -	int ret;
>> -
>> -	ret = spinand_select_target(spinand, pos->target);
>> -	if (ret)
>> -		return ret;
>> -
>> -	ret = spinand_write_enable_op(spinand);
>> -	if (ret)
>> -		return ret;
>> -
>> -	ret = spinand_erase_op(spinand, pos);
>> -	if (ret)
>> -		return ret;
>> -
>> -	ret = spinand_wait(spinand, &status);
>> -	if (!ret && (status & STATUS_ERASE_FAILED))
>> -		ret = -EIO;
>> -
>> -	return ret;
>> +	return __spinand_erase(nand, pos, true);
>>   }
>>   
>>   static int spinand_mtd_erase(struct mtd_info *mtd,
> 
> Thanks,
> Miquèl
>
Schrempf Frieder Feb. 17, 2020, 11:29 a.m. UTC | #4
Hi Boris,

On 17.02.20 12:14, Boris Brezillon wrote:
> On Mon, 17 Feb 2020 11:39:19 +0100
> Miquel Raynal <miquel.raynal@bootlin.com> wrote:
> 
>> Hi Frieder,
>>
>> Schrempf Frieder <frieder.schrempf@kontron.de> wrote on Tue, 11 Feb
>> 2020 16:35:53 +0000:
>>
>>> From: Frieder Schrempf <frieder.schrempf@kontron.de>
>>>
>>> Currently when marking a block, we use spinand_erase_op() to erase
>>> the block before writing the marker to the OOB area without waiting
>>> for the operation to succeed. This can lead to the marking failing
>>> silently and no bad block marker being written to the flash.
>>>
>>> To fix this we reuse the spinand_erase() function, that already does
>>> everything we need to do before actually writing the marker.
>>>    
>>
>> Thanks a lot for this series!
>>
>> Yet I don't really understand the point of waiting for the erasure if
>> it failed: we don't really care as programming (1 -> 0) cells is always
>> possible. Are you sure this lead to an error?
> 
> Actually, I think I already pointed out that we should probably write
> the BBM without erasing the block. IIRC, this logic has been copied
> from rawnand where some controllers don't disable the ECC engine when
> doing raw accesses, leading to ECC errors if the block is not erased
> before BBMs are programmed. Assuming we don't let such drivers being
> merged in spinand, this erase operation can be dropped.

You're probably right, we could also just write the BBM without erasing 
the block. I will try if this works in my setup and update the patch.

> 
>>
>> Also, why just not calling spinand_erase() instead of
>> spinand_erase_op() from spinand_markbad()?
>>
>>> Fixes: 7529df465248 ("mtd: nand: Add core infrastructure to support SPI NANDs")
>>> Cc: stable@vger.kernel.org
>>> Signed-off-by: Frieder Schrempf <frieder.schrempf@kontron.de>
>>> ---
>>>   drivers/mtd/nand/spi/core.c | 56 ++++++++++++++++++-------------------
>>>   1 file changed, 28 insertions(+), 28 deletions(-)
>>>
>>> diff --git a/drivers/mtd/nand/spi/core.c b/drivers/mtd/nand/spi/core.c
>>> index 925db6269861..8a69d13639e2 100644
>>> --- a/drivers/mtd/nand/spi/core.c
>>> +++ b/drivers/mtd/nand/spi/core.c
>>> @@ -600,6 +600,32 @@ static int spinand_mtd_block_isbad(struct mtd_info *mtd, loff_t offs)
>>>   	return ret;
>>>   }
>>>   
>>> +static int __spinand_erase(struct nand_device *nand, const struct nand_pos *pos,
>>> +			   bool hard_fail)
> 
> I hate those __ prefix. Please find a more descriptive name
> (spinand_erase_block() or spinand_erase_and_wait()?)

Actually I was expecting this comment ;)
And I totally agree. I was just lazy to come up with a name.
If we follow the approach without erase, I can get rid of this anyway.

Thanks,
Frieder

> 
>>> +{
>>> +	struct spinand_device *spinand = nand_to_spinand(nand);
>>> +	u8 status;
>>> +	int ret;
>>> +
>>> +	ret = spinand_select_target(spinand, pos->target);
>>> +	if (ret)
>>> +		return ret;
>>> +
>>> +	ret = spinand_write_enable_op(spinand);
>>> +	if (ret)
>>> +		return ret;
>>> +
>>> +	ret = spinand_erase_op(spinand, pos);
>>> +	if (ret && hard_fail)
>>> +		return ret;
>>> +
>>> +	ret = spinand_wait(spinand, &status);
>>> +	if (!ret && (status & STATUS_ERASE_FAILED))
>>> +		ret = -EIO;
>>> +
>>> +	return ret;
>>> +}
>>> +
>>>   static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos)
>>>   {
>>>   	struct spinand_device *spinand = nand_to_spinand(nand);
>>> @@ -614,16 +640,10 @@ static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos)
>>>   	int ret;
>>>   
>>>   	/* Erase block before marking it bad. */
>>> -	ret = spinand_select_target(spinand, pos->target);
>>> -	if (ret)
>>> -		return ret;
>>> -
>>> -	ret = spinand_write_enable_op(spinand);
>>> +	ret = __spinand_erase(nand, pos, false);
>>>   	if (ret)
>>>   		return ret;
>>>   
>>> -	spinand_erase_op(spinand, pos);
>>> -
>>>   	return spinand_write_page(spinand, &req);
>>>   }
>>>   
>>> @@ -644,27 +664,7 @@ static int spinand_mtd_block_markbad(struct mtd_info *mtd, loff_t offs)
>>>   
>>>   static int spinand_erase(struct nand_device *nand, const struct nand_pos *pos)
>>>   {
>>> -	struct spinand_device *spinand = nand_to_spinand(nand);
>>> -	u8 status;
>>> -	int ret;
>>> -
>>> -	ret = spinand_select_target(spinand, pos->target);
>>> -	if (ret)
>>> -		return ret;
>>> -
>>> -	ret = spinand_write_enable_op(spinand);
>>> -	if (ret)
>>> -		return ret;
>>> -
>>> -	ret = spinand_erase_op(spinand, pos);
>>> -	if (ret)
>>> -		return ret;
>>> -
>>> -	ret = spinand_wait(spinand, &status);
>>> -	if (!ret && (status & STATUS_ERASE_FAILED))
>>> -		ret = -EIO;
>>> -
>>> -	return ret;
>>> +	return __spinand_erase(nand, pos, true);
>>>   }
>>>   
>>>   static int spinand_mtd_erase(struct mtd_info *mtd,
>>
>> Thanks,
>> Miquèl
>
David Laight Feb. 17, 2020, 11:30 a.m. UTC | #5
From: Schrempf Frieder
> Sent: 17 February 2020 11:19
> > Schrempf Frieder <frieder.schrempf@kontron.de> wrote on Tue, 11 Feb
> > 2020 16:35:53 +0000:
> >
> >> From: Frieder Schrempf <frieder.schrempf@kontron.de>
> >>
> >> Currently when marking a block, we use spinand_erase_op() to erase
> >> the block before writing the marker to the OOB area without waiting
> >> for the operation to succeed. This can lead to the marking failing
> >> silently and no bad block marker being written to the flash.
> >>
> >> To fix this we reuse the spinand_erase() function, that already does
> >> everything we need to do before actually writing the marker.
> >>
> >
> > Thanks a lot for this series!
> >
> > Yet I don't really understand the point of waiting for the erasure if
> > it failed: we don't really care as programming (1 -> 0) cells is always
> > possible. Are you sure this lead to an error?
> 
> We don't care about the result of the erase operation, but I think we
> still need to wait for it to be done and the STATUS_BUSY bit to be
> cleared. Otherwise it seems like the program operation to set the marker
> can get ignored by the chip. At least that's my explanation for the
> behavior I was observing.

Serial flash devices won't allow any accesses while an erase or write
in in progress.
So while you don't need to wait for either to finish, you do need
to remember that one is 'pending' and wait for it to finish
before any further accesses (apart from reads of the status register).

How many writes you can do to an area (that clear 1s) and the size
of the area will be device dependant.
IIRC one device I've used allows 2 writes to each 16bit word.
This allows either two separate byte writes or one write of
a 16bit (or 32bit) value followed by a second write of all 0s
the 'erase' the value without doing a erase-rewrite cycle.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

Patch
diff mbox series

diff --git a/drivers/mtd/nand/spi/core.c b/drivers/mtd/nand/spi/core.c
index 925db6269861..8a69d13639e2 100644
--- a/drivers/mtd/nand/spi/core.c
+++ b/drivers/mtd/nand/spi/core.c
@@ -600,6 +600,32 @@  static int spinand_mtd_block_isbad(struct mtd_info *mtd, loff_t offs)
 	return ret;
 }
 
+static int __spinand_erase(struct nand_device *nand, const struct nand_pos *pos,
+			   bool hard_fail)
+{
+	struct spinand_device *spinand = nand_to_spinand(nand);
+	u8 status;
+	int ret;
+
+	ret = spinand_select_target(spinand, pos->target);
+	if (ret)
+		return ret;
+
+	ret = spinand_write_enable_op(spinand);
+	if (ret)
+		return ret;
+
+	ret = spinand_erase_op(spinand, pos);
+	if (ret && hard_fail)
+		return ret;
+
+	ret = spinand_wait(spinand, &status);
+	if (!ret && (status & STATUS_ERASE_FAILED))
+		ret = -EIO;
+
+	return ret;
+}
+
 static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos)
 {
 	struct spinand_device *spinand = nand_to_spinand(nand);
@@ -614,16 +640,10 @@  static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos)
 	int ret;
 
 	/* Erase block before marking it bad. */
-	ret = spinand_select_target(spinand, pos->target);
-	if (ret)
-		return ret;
-
-	ret = spinand_write_enable_op(spinand);
+	ret = __spinand_erase(nand, pos, false);
 	if (ret)
 		return ret;
 
-	spinand_erase_op(spinand, pos);
-
 	return spinand_write_page(spinand, &req);
 }
 
@@ -644,27 +664,7 @@  static int spinand_mtd_block_markbad(struct mtd_info *mtd, loff_t offs)
 
 static int spinand_erase(struct nand_device *nand, const struct nand_pos *pos)
 {
-	struct spinand_device *spinand = nand_to_spinand(nand);
-	u8 status;
-	int ret;
-
-	ret = spinand_select_target(spinand, pos->target);
-	if (ret)
-		return ret;
-
-	ret = spinand_write_enable_op(spinand);
-	if (ret)
-		return ret;
-
-	ret = spinand_erase_op(spinand, pos);
-	if (ret)
-		return ret;
-
-	ret = spinand_wait(spinand, &status);
-	if (!ret && (status & STATUS_ERASE_FAILED))
-		ret = -EIO;
-
-	return ret;
+	return __spinand_erase(nand, pos, true);
 }
 
 static int spinand_mtd_erase(struct mtd_info *mtd,