public inbox for linux-mtd@lists.infradead.org
 help / color / mirror / Atom feed
* [PATCH] Fix erase timeout in M25P80 driver
@ 2009-04-04  7:31 Peter Horton
  2009-04-04  8:10 ` Mike Frysinger
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Horton @ 2009-04-04  7:31 UTC (permalink / raw)
  To: linux-mtd; +Cc: tbm

The M25P80 driver erase operations timeout when using a M25P128 part with a
Marvell Kirkwood (ARM) processor. Change the timeout from a simple loop count
to a time based timeout. Also added a conditional schedule() in the loop.

Signed-off-by: Peter Horton <zero@colonel-panic.org>
Tested-by: Martin Michlmayr <tbm@cyrius.com>

--- linux-2.6.29-git8.orig/drivers/mtd/devices/m25p80.c	2009-03-23 23:12:14.000000000 +0000
+++ linux-2.6.29-git8/drivers/mtd/devices/m25p80.c	2009-04-02 20:37:47.000000000 +0100
@@ -54,7 +54,7 @@
 #define	SR_SRWD			0x80	/* SR write protect */
 
 /* Define max times to check status register before we give up. */
-#define	MAX_READY_WAIT_COUNT	100000
+#define	MAX_READY_WAIT_TIMEOUT	7000	/* ms - M25P128 max is 6s */
 #define	CMD_SIZE		4
 
 #ifdef CONFIG_M25PXX_USE_FAST_READ
@@ -145,19 +145,25 @@
  */
 static int wait_till_ready(struct m25p *flash)
 {
-	int count;
+#	define DIV_U(n,d)		(((n)+(d)-1)/(d))
+
+	unsigned long deadline;
 	int sr;
 
-	/* one chip guarantees max 5 msec wait here after page writes,
-	 * but potentially three seconds (!) after page erase.
+	deadline = jiffies + DIV_U(MAX_READY_WAIT_TIMEOUT * HZ, 1000);
+
+	/* this can take a long time for sector erase.
+	 * we should probably have a separate timeout
+	 * for program and erase and we should check
+	 * for signals ...
 	 */
-	for (count = 0; count < MAX_READY_WAIT_COUNT; count++) {
+	while (!time_after_eq(jiffies, deadline)) {
 		if ((sr = read_sr(flash)) < 0)
 			break;
 		else if (!(sr & SR_WIP))
 			return 0;
 
-		/* REVISIT sometimes sleeping would be best */
+		cond_resched();
 	}
 
 	return 1;

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Fix erase timeout in M25P80 driver
  2009-04-04  7:31 Peter Horton
@ 2009-04-04  8:10 ` Mike Frysinger
  0 siblings, 0 replies; 5+ messages in thread
From: Mike Frysinger @ 2009-04-04  8:10 UTC (permalink / raw)
  To: Peter Horton; +Cc: linux-mtd, tbm

On Sat, Apr 4, 2009 at 03:31, Peter Horton wrote:
>  static int wait_till_ready(struct m25p *flash)
>  {
> -       int count;
> +#      define DIV_U(n,d)               (((n)+(d)-1)/(d))
> +
> +       unsigned long deadline;
>        int sr;
>
> -       /* one chip guarantees max 5 msec wait here after page writes,
> -        * but potentially three seconds (!) after page erase.
> +       deadline = jiffies + DIV_U(MAX_READY_WAIT_TIMEOUT * HZ, 1000);

there's already a macro for working with jiffies and time.  dont start
writing yet another one with its own ugly magic.  just look in
linux/jiffies.h.

> +       /* this can take a long time for sector erase.
> +        * we should probably have a separate timeout
> +        * for program and erase and we should check
> +        * for signals ...

timeouts only occur when there's a problem.  a problematic system is
not normal, so having longer (and common) timeouts is fine because
they should never be reached.
-mike

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH] Fix erase timeout in M25P80 driver
@ 2009-04-04 14:38 Peter Horton
  2009-04-04 15:34 ` Martin Michlmayr
  2009-04-04 18:47 ` Mike Frysinger
  0 siblings, 2 replies; 5+ messages in thread
From: Peter Horton @ 2009-04-04 14:38 UTC (permalink / raw)
  To: linux-mtd; +Cc: tbm

Extend erase timeout in M25P80 SPI Flash driver.

The M25P80 drivers fails erasing sectors on a M25P128 because the ready
wait timeout is too short. Change the timeout from a simple loop count to a
suitable number of seconds.

Signed-off-by: Peter Horton <zero@colonel-panic.org>
---
Index: linux-2.6.29-git12/drivers/mtd/devices/m25p80.c
===================================================================
--- linux-2.6.29-git12.orig/drivers/mtd/devices/m25p80.c	2009-04-04 21:05:44.000000000 +0000
+++ linux-2.6.29-git12/drivers/mtd/devices/m25p80.c	2009-04-04 21:40:06.000000000 +0000
@@ -54,7 +54,7 @@
 #define	SR_SRWD			0x80	/* SR write protect */
 
 /* Define max times to check status register before we give up. */
-#define	MAX_READY_WAIT_COUNT	100000
+#define	MAX_READY_WAIT_JIFFIES	(10 * HZ)	/* eg. M25P128 specs 6s max sector erase */
 #define	CMD_SIZE		4
 
 #ifdef CONFIG_M25PXX_USE_FAST_READ
@@ -145,19 +145,16 @@
  */
 static int wait_till_ready(struct m25p *flash)
 {
-	int count;
+	unsigned long deadline;
 	int sr;
 
-	/* one chip guarantees max 5 msec wait here after page writes,
-	 * but potentially three seconds (!) after page erase.
-	 */
-	for (count = 0; count < MAX_READY_WAIT_COUNT; count++) {
+	for (deadline = jiffies + MAX_READY_WAIT_JIFFIES; !time_after_eq(jiffies, deadline);) {
 		if ((sr = read_sr(flash)) < 0)
 			break;
 		else if (!(sr & SR_WIP))
 			return 0;
 
-		/* REVISIT sometimes sleeping would be best */
+		cond_resched();
 	}
 
 	return 1;

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Fix erase timeout in M25P80 driver
  2009-04-04 14:38 [PATCH] Fix erase timeout in M25P80 driver Peter Horton
@ 2009-04-04 15:34 ` Martin Michlmayr
  2009-04-04 18:47 ` Mike Frysinger
  1 sibling, 0 replies; 5+ messages in thread
From: Martin Michlmayr @ 2009-04-04 15:34 UTC (permalink / raw)
  To: Peter Horton; +Cc: linux-mtd

* Peter Horton <zero@colonel-panic.org> [2009-04-04 15:38]:
> Extend erase timeout in M25P80 SPI Flash driver.
> 
> The M25P80 drivers fails erasing sectors on a M25P128 because the ready
> wait timeout is too short. Change the timeout from a simple loop count to a
> suitable number of seconds.
> 
> Signed-off-by: Peter Horton <zero@colonel-panic.org>

Tested-by: Martin Michlmayr <tbm@cyrius.com>

This solves the problem I've seen on the QNAP TS-219.
-- 
Martin Michlmayr
http://www.cyrius.com/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Fix erase timeout in M25P80 driver
  2009-04-04 14:38 [PATCH] Fix erase timeout in M25P80 driver Peter Horton
  2009-04-04 15:34 ` Martin Michlmayr
@ 2009-04-04 18:47 ` Mike Frysinger
  1 sibling, 0 replies; 5+ messages in thread
From: Mike Frysinger @ 2009-04-04 18:47 UTC (permalink / raw)
  To: Peter Horton; +Cc: linux-mtd, tbm

On Sat, Apr 4, 2009 at 10:38, Peter Horton wrote:
> +       for (deadline = jiffies + MAX_READY_WAIT_JIFFIES; !time_after_eq(jiffies, deadline);) {

this would make more sense as a while loop:
  deadline = jiffies + MAX_READY_WAIT_JIFFIES;
  while (!time_after_eq(jiffies, deadline)) {
maybe even more sense as a do { ... } while (...) ...

a for loop just looks weird ... but it should do what you want, so whatever
-mike

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-04-04 18:47 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-04-04 14:38 [PATCH] Fix erase timeout in M25P80 driver Peter Horton
2009-04-04 15:34 ` Martin Michlmayr
2009-04-04 18:47 ` Mike Frysinger
  -- strict thread matches above, loose matches on Subject: below --
2009-04-04  7:31 Peter Horton
2009-04-04  8:10 ` Mike Frysinger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox