[PATCH v2] mtd: rawnand: r852: preserve card changes during detection work
James Hilliard
james.hilliard1 at gmail.com
Sun Nov 8 15:27:48 PST 2026
r852_update_card_detect() reads and writes the card interrupt enable
register without holding irqlock. A card interrupt can mask detection
interrupts and mark the card unstable between those accesses, only for
the worker to overwrite the mask with its saved value.
The worker also calls this helper before and after registering or
unregistering a card. An interrupt between these calls starts a new
debounce interval, but the second call clears card_unstable and enables
interrupts again before that interval has elapsed. The interrupt can
also change card_detected between the worker's registration checks.
Hold irqlock across the interrupt mask update and leave the mask alone
while the card is unstable. Move the transition to stable state into
the locked card sample, and return that sample for both of the worker's
registration decisions.
Keep registration failure from overwriting a newer interrupt's card
state, so the next delayed work invocation can process that change.
Fixes: 67e054e91924 ("mtd: nand: Add driver for Ricoh xD/SmartMedia reader")
Cc: stable at vger.kernel.org
Closes: https://lore.kernel.org/r/sashiko-outbox-204068@kernel.org/
Closes: https://lore.kernel.org/r/sashiko-outbox-204135@kernel.org/
Signed-off-by: James Hilliard <james.hilliard1 at gmail.com>
---
Changes in v2:
- Keep new debounce events pending until the next card-state sample.
- Use one presence sample for the worker's registration decisions.
- Protect the registration-failure update from newer IRQ events.
- Link to v1: https://patch.msgid.link/20261108-submit-r852-card-detect-lock-v1-v1-1-5514e7543209@gmail.com
To: Maxim Levitsky <maximlevitsky at gmail.com>
To: Miquel Raynal <miquel.raynal at bootlin.com>
To: Richard Weinberger <richard at nod.at>
To: Vignesh Raghavendra <vigneshr at ti.com>
Cc: linux-mtd at lists.infradead.org
Cc: linux-kernel at vger.kernel.org
---
drivers/mtd/nand/raw/r852.c | 35 ++++++++++++++++++++++++++---------
1 file changed, 26 insertions(+), 9 deletions(-)
diff --git a/drivers/mtd/nand/raw/r852.c b/drivers/mtd/nand/raw/r852.c
index 92c47d351c27..4c094648673b 100644
--- a/drivers/mtd/nand/raw/r852.c
+++ b/drivers/mtd/nand/raw/r852.c
@@ -557,15 +557,20 @@ static void r852_engine_disable(struct r852_device *dev)
* Test if card is present
*/
-static void r852_card_update_present(struct r852_device *dev)
+static bool r852_card_update_present(struct r852_device *dev)
{
unsigned long flags;
uint8_t reg;
+ bool present;
spin_lock_irqsave(&dev->irqlock, flags);
reg = r852_read_reg(dev, R852_CARD_STA);
- dev->card_detected = !!(reg & R852_CARD_STA_PRESENT);
+ present = !!(reg & R852_CARD_STA_PRESENT);
+ dev->card_detected = present;
+ dev->card_unstable = 0;
spin_unlock_irqrestore(&dev->irqlock, flags);
+
+ return present;
}
/*
@@ -574,8 +579,14 @@ static void r852_card_update_present(struct r852_device *dev)
*/
static void r852_update_card_detect(struct r852_device *dev)
{
- int card_detect_reg = r852_read_reg(dev, R852_CARD_IRQ_ENABLE);
- dev->card_unstable = 0;
+ unsigned long flags;
+ int card_detect_reg;
+
+ spin_lock_irqsave(&dev->irqlock, flags);
+ if (dev->card_unstable)
+ goto out;
+
+ card_detect_reg = r852_read_reg(dev, R852_CARD_IRQ_ENABLE);
card_detect_reg &= ~(R852_CARD_IRQ_REMOVE | R852_CARD_IRQ_INSERT);
card_detect_reg |= R852_CARD_IRQ_GENABLE;
@@ -584,6 +595,8 @@ static void r852_update_card_detect(struct r852_device *dev)
R852_CARD_IRQ_REMOVE : R852_CARD_IRQ_INSERT;
r852_write_reg(dev, R852_CARD_IRQ_ENABLE, card_detect_reg);
+out:
+ spin_unlock_irqrestore(&dev->irqlock, flags);
}
static ssize_t media_type_show(struct device *sys_dev,
@@ -632,6 +645,7 @@ static void r852_update_media_status(struct r852_device *dev)
static int r852_register_nand_device(struct r852_device *dev)
{
struct mtd_info *mtd = nand_to_mtd(dev->chip);
+ unsigned long flags;
WARN_ON(dev->card_registered);
@@ -657,7 +671,10 @@ static int r852_register_nand_device(struct r852_device *dev)
nand_cleanup(dev->chip);
error1:
/* Force card redetect */
- dev->card_detected = 0;
+ spin_lock_irqsave(&dev->irqlock, flags);
+ if (!dev->card_unstable)
+ dev->card_detected = 0;
+ spin_unlock_irqrestore(&dev->irqlock, flags);
return -1;
}
@@ -684,20 +701,20 @@ static void r852_card_detect_work(struct work_struct *work)
{
struct r852_device *dev =
container_of(work, struct r852_device, card_detect_work.work);
+ bool present;
- r852_card_update_present(dev);
+ present = r852_card_update_present(dev);
r852_update_card_detect(dev);
- dev->card_unstable = 0;
/* False alarm */
- if (dev->card_detected == dev->card_registered)
+ if (present == dev->card_registered)
goto exit;
/* Read media properties */
r852_update_media_status(dev);
/* Register the card */
- if (dev->card_detected)
+ if (present)
r852_register_nand_device(dev);
else
r852_unregister_nand_device(dev);
---
base-commit: 30daaf3e7897b4a8d9b72e5b0c86dd6e2a4d7d85
change-id: 20261108-submit-r852-card-detect-lock-v1-713bf5cd926b
Best regards,
--
James Hilliard <james.hilliard1 at gmail.com>
More information about the linux-mtd
mailing list