mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 01/11] mv64x60_wdt: set up platform_device in platform code
@ 2007-07-24 18:07 Dale Farnsworth
  2007-07-24 18:09 ` [PATCH 02/11] mv64x60_wdt: Get register address from platform data Dale Farnsworth
                   ` (9 more replies)
  0 siblings, 10 replies; 12+ messages in thread
From: Dale Farnsworth @ 2007-07-24 18:07 UTC (permalink / raw)
  To: Wim Van Sebroeck, linux-kernel

The driver previously registered its platform device data in its own
init function--that's bogus.  Move that code to platform-specific
code in arch/ppc.  This is being done so that the platform code can
decide at runtime whether to initialize this driver or not.

Signed-off-by: Dale Farnsworth <dale@farnsworth.org>
---
 arch/ppc/syslib/mv64x60.c           |   29 ++++++++++++++++++++++++++
 drivers/char/watchdog/mv64x60_wdt.c |   26 -----------------------
 include/asm-ppc/mv64x60.h           |    2 -
 3 files changed, 31 insertions(+), 26 deletions(-)

Index: linux-2.6-powerpc-wdt/arch/ppc/syslib/mv64x60.c
===================================================================
--- linux-2.6-powerpc-wdt.orig/arch/ppc/syslib/mv64x60.c	2007-07-20 00:49:16.000000000 +0000
+++ linux-2.6-powerpc-wdt/arch/ppc/syslib/mv64x60.c	2007-07-20 00:52:19.000000000 +0000
@@ -440,6 +440,32 @@ static struct platform_device i2c_device
 };
 #endif
 
+#ifdef CONFIG_WATCHDOG
+static struct mv64x60_wdt_pdata mv64x60_wdt_pdata = {
+	.timeout		= 10,  /* default watchdog expiry in seconds */
+	.bus_clk		= 133, /* default bus clock in MHz */
+};
+
+static struct resource mv64x60_wdt_resources[] = {
+	[0] = {
+		.name	= "mv64x60 wdt base",
+		.start	= MV64x60_WDT_WDC,
+		.end	= MV64x60_WDT_WDC + 8 - 1, /* two 32-bit registers */
+		.flags	= IORESOURCE_MEM,
+	},
+};
+
+static struct platform_device wdt_device = {
+	.name		= MV64x60_WDT_NAME,
+	.id		= 0,
+	.num_resources	= ARRAY_SIZE(mv64x60_wdt_resources),
+	.resource	= mv64x60_wdt_resources,
+	.dev = {
+		.platform_data = &mv64x60_wdt_pdata,
+	},
+};
+#endif
+
 #if defined(CONFIG_SYSFS) && !defined(CONFIG_GT64260)
 static struct mv64xxx_pdata mv64xxx_pdata = {
 	.hs_reg_valid	= 0,
@@ -475,6 +501,9 @@ static struct platform_device *mv64x60_p
 #ifdef	CONFIG_I2C_MV64XXX
 	&i2c_device,
 #endif
+#ifdef	CONFIG_MV64X60_WDT
+	&wdt_device,
+#endif
 #if defined(CONFIG_SYSFS) && !defined(CONFIG_GT64260)
 	&mv64xxx_device,
 #endif
Index: linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c
===================================================================
--- linux-2.6-powerpc-wdt.orig/drivers/char/watchdog/mv64x60_wdt.c	2007-07-20 00:49:16.000000000 +0000
+++ linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c	2007-07-20 00:49:18.000000000 +0000
@@ -219,40 +219,16 @@ static struct platform_driver mv64x60_wd
 	},
 };
 
-static struct platform_device *mv64x60_wdt_dev;
-
 static int __init mv64x60_wdt_init(void)
 {
-	int ret;
-
 	printk(KERN_INFO "MV64x60 watchdog driver\n");
 
-	mv64x60_wdt_dev = platform_device_alloc(MV64x60_WDT_NAME, -1);
-	if (!mv64x60_wdt_dev) {
-		ret = -ENOMEM;
-		goto out;
-	}
-
-	ret = platform_device_add(mv64x60_wdt_dev);
-	if (ret) {
-		platform_device_put(mv64x60_wdt_dev);
-		goto out;
-	}
-
-	ret = platform_driver_register(&mv64x60_wdt_driver);
-	if (ret) {
-		platform_device_unregister(mv64x60_wdt_dev);
-		goto out;
-	}
-
- out:
-	return ret;
+	return platform_driver_register(&mv64x60_wdt_driver);
 }
 
 static void __exit mv64x60_wdt_exit(void)
 {
 	platform_driver_unregister(&mv64x60_wdt_driver);
-	platform_device_unregister(mv64x60_wdt_dev);
 }
 
 module_init(mv64x60_wdt_init);
Index: linux-2.6-powerpc-wdt/include/asm-ppc/mv64x60.h
===================================================================
--- linux-2.6-powerpc-wdt.orig/include/asm-ppc/mv64x60.h	2007-07-20 00:49:16.000000000 +0000
+++ linux-2.6-powerpc-wdt/include/asm-ppc/mv64x60.h	2007-07-20 00:49:18.000000000 +0000
@@ -121,7 +121,7 @@ extern spinlock_t mv64x60_lock;
 #define	MV64x60_64BIT_WIN_COUNT			24
 
 /* Watchdog Platform Device, Driver Data */
-#define	MV64x60_WDT_NAME			"wdt"
+#define	MV64x60_WDT_NAME			"mv64x60_wdt"
 
 struct mv64x60_wdt_pdata {
 	int	timeout;	/* watchdog expiry in seconds, default 10 */

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

* [PATCH 02/11] mv64x60_wdt: Get register address from platform data
  2007-07-24 18:07 [PATCH 01/11] mv64x60_wdt: set up platform_device in platform code Dale Farnsworth
@ 2007-07-24 18:09 ` Dale Farnsworth
  2007-07-24 18:12 ` [PATCH 03/11] mv64x60_wdt: Add arch/powerpc platform support Dale Farnsworth
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 12+ messages in thread
From: Dale Farnsworth @ 2007-07-24 18:09 UTC (permalink / raw)
  To: Wim Van Sebroeck, linux-kernel

Previously, the address of the watchdog timer registers was
retrieved by calling a global function, mv64x60_get_bridge_vbase().
That function doesn't exist in arch/powerpc.  Instead, we now get
the register address from a platform data resource and ioremap
the registers within the driver.

Signed-off-by: Dale Farnsworth <dale@farnsworth.org>
---
 drivers/char/watchdog/mv64x60_wdt.c |   21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

Index: linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c
===================================================================
--- linux-2.6-powerpc-wdt.orig/drivers/char/watchdog/mv64x60_wdt.c	2007-07-18 23:12:48.000000000 +0000
+++ linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c	2007-07-19 00:05:37.000000000 +0000
@@ -27,6 +27,8 @@
 #include <asm/uaccess.h>
 #include <asm/io.h>
 
+#define MV64x60_WDT_WDC_OFFSET	0
+
 /* MV64x60 WDC (config) register access definitions */
 #define MV64x60_WDC_CTL1_MASK	(3 << 24)
 #define MV64x60_WDC_CTL1(val)	((val & 3) << 24)
@@ -39,7 +41,7 @@
 
 static unsigned long wdt_flags;
 static int wdt_status;
-static void __iomem *mv64x60_regs;
+static void __iomem *mv64x60_wdt_regs;
 static int mv64x60_wdt_timeout;
 
 static void mv64x60_wdt_reg_write(u32 val)
@@ -47,10 +49,10 @@ static void mv64x60_wdt_reg_write(u32 va
 	/* Allow write only to CTL1 / CTL2 fields, retaining values in
 	 * other fields.
 	 */
-	u32 data = readl(mv64x60_regs + MV64x60_WDT_WDC);
+	u32 data = readl(mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
 	data &= ~(MV64x60_WDC_CTL1_MASK | MV64x60_WDC_CTL2_MASK);
 	data |= val;
-	writel(data, mv64x60_regs + MV64x60_WDT_WDC);
+	writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
 }
 
 static void mv64x60_wdt_service(void)
@@ -185,6 +187,7 @@ static int __devinit mv64x60_wdt_probe(s
 {
 	struct mv64x60_wdt_pdata *pdata = dev->dev.platform_data;
 	int bus_clk = 133;
+	struct resource *r;
 
 	mv64x60_wdt_timeout = 10;
 	if (pdata) {
@@ -192,10 +195,16 @@ static int __devinit mv64x60_wdt_probe(s
 		bus_clk = pdata->bus_clk;
 	}
 
-	mv64x60_regs = mv64x60_get_bridge_vbase();
+	r = platform_get_resource(dev, IORESOURCE_MEM, 0);
+	if (!r)
+		return -ENODEV;
+
+	mv64x60_wdt_regs = ioremap(r->start, r->end - r->start + 1);
+	if (mv64x60_wdt_regs == NULL)
+		return -ENOMEM;
 
 	writel((mv64x60_wdt_timeout * (bus_clk * 1000000)) >> 8,
-	       mv64x60_regs + MV64x60_WDT_WDC);
+	       mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
 
 	return misc_register(&mv64x60_wdt_miscdev);
 }
@@ -207,6 +216,8 @@ static int __devexit mv64x60_wdt_remove(
 	mv64x60_wdt_service();
 	mv64x60_wdt_handler_disable();
 
+	iounmap(mv64x60_wdt_regs);
+
 	return 0;
 }
 


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

* [PATCH 03/11] mv64x60_wdt: Add arch/powerpc platform support
  2007-07-24 18:07 [PATCH 01/11] mv64x60_wdt: set up platform_device in platform code Dale Farnsworth
  2007-07-24 18:09 ` [PATCH 02/11] mv64x60_wdt: Get register address from platform data Dale Farnsworth
@ 2007-07-24 18:12 ` Dale Farnsworth
  2007-07-24 18:13 ` [PATCH 04/11] mv64x60_wdt: Check return value of nonseekable_open Dale Farnsworth
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 12+ messages in thread
From: Dale Farnsworth @ 2007-07-24 18:12 UTC (permalink / raw)
  To: Wim Van Sebroeck, linux-kernel

Add support for arch/powerpc, specifically for  the prpmc2800 platform.

Signed-off-by: Dale Farnsworth <dale@farnsworth.org>
---
 arch/powerpc/boot/dts/prpmc2800.dts |    6 ++
 arch/powerpc/sysdev/mv64x60_dev.c   |   64 ++++++++++++++++++++++++++
 drivers/char/watchdog/mv64x60_wdt.c |    2 
 include/asm-ppc/mv64x60.h           |    8 ---
 include/linux/mv643xx.h             |    8 +++
 5 files changed, 79 insertions(+), 9 deletions(-)

Index: linux-2.6-powerpc-wdt/arch/powerpc/boot/dts/prpmc2800.dts
===================================================================
--- linux-2.6-powerpc-wdt.orig/arch/powerpc/boot/dts/prpmc2800.dts	2007-07-20 16:41:08.000000000 +0000
+++ linux-2.6-powerpc-wdt/arch/powerpc/boot/dts/prpmc2800.dts	2007-07-20 16:42:09.000000000 +0000
@@ -207,6 +207,12 @@
 			interrupt-parent = <&/mv64x60/pic>;
 		};
 
+		wdt@b410 {			/* watchdog timer */
+			compatible = "marvell,mv64x60-wdt";
+			reg = <b410 8>;
+			timeout = <a>;		/* wdt timeout in seconds */
+		};
+
 		i2c@c000 {
 			device_type = "i2c";
 			compatible = "marvell,mv64x60-i2c";
Index: linux-2.6-powerpc-wdt/arch/powerpc/sysdev/mv64x60_dev.c
===================================================================
--- linux-2.6-powerpc-wdt.orig/arch/powerpc/sysdev/mv64x60_dev.c	2007-07-20 16:41:08.000000000 +0000
+++ linux-2.6-powerpc-wdt/arch/powerpc/sysdev/mv64x60_dev.c	2007-07-20 16:45:26.000000000 +0000
@@ -390,6 +390,61 @@ error:
 	return err;
 }
 
+/*
+ * Create mv64x60_wdt platform devices
+ */
+static int __init mv64x60_wdt_device_setup(struct device_node *np, int id)
+{
+	struct resource r;
+	struct platform_device *pdev;
+	struct mv64x60_wdt_pdata pdata;
+	const unsigned int *prop;
+	int err;
+
+	err = of_address_to_resource(np, 0, &r);
+	if (err)
+		return err;
+
+	memset(&pdata, 0, sizeof(pdata));
+
+	prop = of_get_property(np, "timeout", NULL);
+	if (!prop)
+		return -ENODEV;
+	pdata.timeout = *prop;
+
+	np = of_get_parent(np);
+	if (!np)
+		return -ENODEV;
+
+	prop = of_get_property(np, "clock-frequency", NULL);
+	of_node_put(np);
+	if (!prop)
+		return -ENODEV;
+	pdata.bus_clk = *prop / 1000000; /* wdt driver wants freq in MHz */
+
+	pdev = platform_device_alloc(MV64x60_WDT_NAME, id);
+	if (!pdev)
+		return -ENOMEM;
+
+	err = platform_device_add_resources(pdev, &r, 1);
+	if (err)
+		goto error;
+
+	err = platform_device_add_data(pdev, &pdata, sizeof(pdata));
+	if (err)
+		goto error;
+
+	err = platform_device_add(pdev);
+	if (err)
+		goto error;
+
+	return 0;
+
+error:
+	platform_device_put(pdev);
+	return err;
+}
+
 static int __init mv64x60_device_setup(void)
 {
 	struct device_node *np = NULL;
@@ -414,6 +469,15 @@ static int __init mv64x60_device_setup(v
 		if ((err = mv64x60_i2c_device_setup(np, id)))
 			goto error;
 
+	/* support up to one watchdog timer */
+	np = of_find_compatible_node(np, NULL, "marvell,mv64x60-wdt");
+	if (np) {
+		if ((err = mv64x60_wdt_device_setup(np, id)))
+			goto error;
+		of_node_put(np);
+	}
+
+
 	return 0;
 
 error:
Index: linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c
===================================================================
--- linux-2.6-powerpc-wdt.orig/drivers/char/watchdog/mv64x60_wdt.c	2007-07-20 16:41:37.000000000 +0000
+++ linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c	2007-07-20 16:48:00.000000000 +0000
@@ -23,7 +23,7 @@
 #include <linux/watchdog.h>
 #include <linux/platform_device.h>
 
-#include <asm/mv64x60.h>
+#include <linux/mv643xx.h>
 #include <asm/uaccess.h>
 #include <asm/io.h>
 
Index: linux-2.6-powerpc-wdt/include/asm-ppc/mv64x60.h
===================================================================
--- linux-2.6-powerpc-wdt.orig/include/asm-ppc/mv64x60.h	2007-07-20 16:41:11.000000000 +0000
+++ linux-2.6-powerpc-wdt/include/asm-ppc/mv64x60.h	2007-07-20 16:42:09.000000000 +0000
@@ -120,14 +120,6 @@ extern spinlock_t mv64x60_lock;
 
 #define	MV64x60_64BIT_WIN_COUNT			24
 
-/* Watchdog Platform Device, Driver Data */
-#define	MV64x60_WDT_NAME			"mv64x60_wdt"
-
-struct mv64x60_wdt_pdata {
-	int	timeout;	/* watchdog expiry in seconds, default 10 */
-	int	bus_clk;	/* bus clock in MHz, default 133 */
-};
-
 /*
  * Define a structure that's used to pass in config information to the
  * core routines.
Index: linux-2.6-powerpc-wdt/include/linux/mv643xx.h
===================================================================
--- linux-2.6-powerpc-wdt.orig/include/linux/mv643xx.h	2007-07-20 16:41:08.000000000 +0000
+++ linux-2.6-powerpc-wdt/include/linux/mv643xx.h	2007-07-20 16:42:09.000000000 +0000
@@ -1306,4 +1306,12 @@ struct mv643xx_eth_platform_data {
 	u8		mac_addr[6];	/* mac address if non-zero*/
 };
 
+/* Watchdog Platform Device, Driver Data */
+#define	MV64x60_WDT_NAME			"mv64x60_wdt"
+
+struct mv64x60_wdt_pdata {
+	int	timeout;	/* watchdog expiry in seconds, default 10 */
+	int	bus_clk;	/* bus clock in MHz, default 133 */
+};
+
 #endif /* __ASM_MV643XX_H */


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

* [PATCH 04/11] mv64x60_wdt: Check return value of nonseekable_open
  2007-07-24 18:07 [PATCH 01/11] mv64x60_wdt: set up platform_device in platform code Dale Farnsworth
  2007-07-24 18:09 ` [PATCH 02/11] mv64x60_wdt: Get register address from platform data Dale Farnsworth
  2007-07-24 18:12 ` [PATCH 03/11] mv64x60_wdt: Add arch/powerpc platform support Dale Farnsworth
@ 2007-07-24 18:13 ` Dale Farnsworth
  2007-07-24 18:14 ` [PATCH 05/11] mv64x60_wdt: Fix WDIOC_GETTIMEOUT return value Dale Farnsworth
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 12+ messages in thread
From: Dale Farnsworth @ 2007-07-24 18:13 UTC (permalink / raw)
  To: Wim Van Sebroeck, linux-kernel

Signed-off-by: Dale Farnsworth <dale@farnsworth.org>
---
 drivers/char/watchdog/mv64x60_wdt.c |    4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

Index: linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c
===================================================================
--- linux-2.6-powerpc-wdt.orig/drivers/char/watchdog/mv64x60_wdt.c	2007-07-19 21:39:15.000000000 +0000
+++ linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c	2007-07-19 21:40:13.000000000 +0000
@@ -90,9 +90,7 @@ static int mv64x60_wdt_open(struct inode
 	mv64x60_wdt_service();
 	mv64x60_wdt_handler_enable();
 
-	nonseekable_open(inode, file);
-
-	return 0;
+	return nonseekable_open(inode, file);
 }
 
 static int mv64x60_wdt_release(struct inode *inode, struct file *file)


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

* [PATCH 05/11] mv64x60_wdt: Fix WDIOC_GETTIMEOUT return value
  2007-07-24 18:07 [PATCH 01/11] mv64x60_wdt: set up platform_device in platform code Dale Farnsworth
                   ` (2 preceding siblings ...)
  2007-07-24 18:13 ` [PATCH 04/11] mv64x60_wdt: Check return value of nonseekable_open Dale Farnsworth
@ 2007-07-24 18:14 ` Dale Farnsworth
  2007-07-24 18:15 ` [PATCH 06/11] mv64x60_wdt: Support for WDIOC_SETTIMEOUT ioctl Dale Farnsworth
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 12+ messages in thread
From: Dale Farnsworth @ 2007-07-24 18:14 UTC (permalink / raw)
  To: Wim Van Sebroeck, linux-kernel

WDIOC_GETTIMEOUT returns seconds, not jiffies.

Signed-off-by: Dale Farnsworth <dale@farnsworth.org>
---
 drivers/char/watchdog/mv64x60_wdt.c |    4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

Index: linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c
===================================================================
--- linux-2.6-powerpc-wdt.orig/drivers/char/watchdog/mv64x60_wdt.c	2007-07-20 19:17:17.000000000 +0000
+++ linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c	2007-07-20 19:23:23.000000000 +0000
@@ -118,7 +118,6 @@ static ssize_t mv64x60_wdt_write(struct 
 static int mv64x60_wdt_ioctl(struct inode *inode, struct file *file,
 			     unsigned int cmd, unsigned long arg)
 {
-	int timeout;
 	void __user *argp = (void __user *)arg;
 	static struct watchdog_info info = {
 		.options = WDIOF_KEEPALIVEPING,
@@ -154,8 +153,7 @@ static int mv64x60_wdt_ioctl(struct inod
 		return -EOPNOTSUPP;
 
 	case WDIOC_GETTIMEOUT:
-		timeout = mv64x60_wdt_timeout * HZ;
-		if (put_user(timeout, (int __user *)argp))
+		if (put_user(mv64x60_wdt_timeout, (int __user *)argp))
 			return -EFAULT;
 		break;
 


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

* [PATCH 06/11] mv64x60_wdt: Support for WDIOC_SETTIMEOUT ioctl
  2007-07-24 18:07 [PATCH 01/11] mv64x60_wdt: set up platform_device in platform code Dale Farnsworth
                   ` (3 preceding siblings ...)
  2007-07-24 18:14 ` [PATCH 05/11] mv64x60_wdt: Fix WDIOC_GETTIMEOUT return value Dale Farnsworth
@ 2007-07-24 18:15 ` Dale Farnsworth
  2007-07-24 18:16 ` [PATCH 07/11] mv64x60_wdt: Add WDIOC_SETOPTIONS ioctl support Dale Farnsworth
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 12+ messages in thread
From: Dale Farnsworth @ 2007-07-24 18:15 UTC (permalink / raw)
  To: Wim Van Sebroeck, linux-kernel

Add the ability to modify the watchdog timer timeout interval.

Signed-off-by: Dale Farnsworth <dale@farnsworth.org>
---
 drivers/char/watchdog/mv64x60_wdt.c |   38 +++++++++++++++++++++-----
 1 file changed, 31 insertions(+), 7 deletions(-)

Index: linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c
===================================================================
--- linux-2.6-powerpc-wdt.orig/drivers/char/watchdog/mv64x60_wdt.c	2007-07-20 19:32:08.000000000 +0000
+++ linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c	2007-07-20 19:42:01.000000000 +0000
@@ -43,6 +43,7 @@ static unsigned long wdt_flags;
 static int wdt_status;
 static void __iomem *mv64x60_wdt_regs;
 static int mv64x60_wdt_timeout;
+static unsigned int bus_clk;
 
 static void mv64x60_wdt_reg_write(u32 val)
 {
@@ -82,6 +83,18 @@ static void mv64x60_wdt_handler_enable(v
 	}
 }
 
+static void mv64x60_wdt_set_timeout(int timeout)
+{
+	/* maximum bus cycle count is 0xFFFFFFFF */
+	if (timeout > 0xFFFFFFFF / bus_clk)
+		timeout = 0xFFFFFFFF / bus_clk;
+
+	mv64x60_wdt_timeout = timeout;
+	writel((timeout * bus_clk) >> 8,
+	       mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
+	mv64x60_wdt_service();
+}
+
 static int mv64x60_wdt_open(struct inode *inode, struct file *file)
 {
 	if (test_and_set_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags))
@@ -118,9 +131,11 @@ static ssize_t mv64x60_wdt_write(struct 
 static int mv64x60_wdt_ioctl(struct inode *inode, struct file *file,
 			     unsigned int cmd, unsigned long arg)
 {
+	int timeout;
 	void __user *argp = (void __user *)arg;
 	static struct watchdog_info info = {
-		.options = WDIOF_KEEPALIVEPING,
+		.options =	WDIOF_SETTIMEOUT	|
+				WDIOF_KEEPALIVEPING,
 		.firmware_version = 0,
 		.identity = "MV64x60 watchdog",
 	};
@@ -150,7 +165,10 @@ static int mv64x60_wdt_ioctl(struct inod
 		break;
 
 	case WDIOC_SETTIMEOUT:
-		return -EOPNOTSUPP;
+		if (get_user(timeout, (int __user *)argp))
+			return -EFAULT;
+		mv64x60_wdt_set_timeout(timeout);
+		/* Fall through */
 
 	case WDIOC_GETTIMEOUT:
 		if (put_user(mv64x60_wdt_timeout, (int __user *)argp))
@@ -182,15 +200,22 @@ static struct miscdevice mv64x60_wdt_mis
 static int __devinit mv64x60_wdt_probe(struct platform_device *dev)
 {
 	struct mv64x60_wdt_pdata *pdata = dev->dev.platform_data;
-	int bus_clk = 133;
 	struct resource *r;
+	int timeout = 10;
 
-	mv64x60_wdt_timeout = 10;
+	bus_clk = 133;			/* in MHz */
 	if (pdata) {
-		mv64x60_wdt_timeout = pdata->timeout;
+		timeout = pdata->timeout;
 		bus_clk = pdata->bus_clk;
 	}
 
+	/* Since bus_clk is truncated MHz, actual frequency could be
+	 * up to 1MHz higher.  Round up, since it's better to time out
+	 * too late than too soon.
+	 */
+	bus_clk++;
+	bus_clk *= 1000000;		/* convert to Hz */
+
 	r = platform_get_resource(dev, IORESOURCE_MEM, 0);
 	if (!r)
 		return -ENODEV;
@@ -199,8 +224,7 @@ static int __devinit mv64x60_wdt_probe(s
 	if (mv64x60_wdt_regs == NULL)
 		return -ENOMEM;
 
-	writel((mv64x60_wdt_timeout * (bus_clk * 1000000)) >> 8,
-	       mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
+	mv64x60_wdt_set_timeout(timeout);
 
 	return misc_register(&mv64x60_wdt_miscdev);
 }


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

* [PATCH 07/11] mv64x60_wdt: Add WDIOC_SETOPTIONS ioctl support
  2007-07-24 18:07 [PATCH 01/11] mv64x60_wdt: set up platform_device in platform code Dale Farnsworth
                   ` (4 preceding siblings ...)
  2007-07-24 18:15 ` [PATCH 06/11] mv64x60_wdt: Support for WDIOC_SETTIMEOUT ioctl Dale Farnsworth
@ 2007-07-24 18:16 ` Dale Farnsworth
  2007-07-24 18:17 ` [PATCH 08/11] mv64x60_wdt: Add a module parameter to change nowayout setting Dale Farnsworth
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 12+ messages in thread
From: Dale Farnsworth @ 2007-07-24 18:16 UTC (permalink / raw)
  To: Wim Van Sebroeck, linux-kernel

Allow the watchdog timer to be enabled or disabled via the
WDIOC_SETOPTIONS ioctl.

Signed-off-by: Dale Farnsworth <dale@farnsworth.org>
---
 drivers/char/watchdog/mv64x60_wdt.c |   11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

Index: linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c
===================================================================
--- linux-2.6-powerpc-wdt.orig/drivers/char/watchdog/mv64x60_wdt.c	2007-07-20 19:29:15.000000000 +0000
+++ linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c	2007-07-20 19:29:34.000000000 +0000
@@ -132,6 +132,7 @@ static int mv64x60_wdt_ioctl(struct inod
 			     unsigned int cmd, unsigned long arg)
 {
 	int timeout;
+	int options;
 	void __user *argp = (void __user *)arg;
 	static struct watchdog_info info = {
 		.options =	WDIOF_SETTIMEOUT	|
@@ -157,7 +158,15 @@ static int mv64x60_wdt_ioctl(struct inod
 		return -EOPNOTSUPP;
 
 	case WDIOC_SETOPTIONS:
-		return -EOPNOTSUPP;
+		if (get_user(options, (int __user *)argp))
+			return -EFAULT;
+
+		if (options & WDIOS_DISABLECARD)
+			mv64x60_wdt_handler_disable();
+
+		if (options & WDIOS_ENABLECARD)
+			mv64x60_wdt_handler_enable();
+		break;
 
 	case WDIOC_KEEPALIVE:
 		mv64x60_wdt_service();


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

* [PATCH 08/11] mv64x60_wdt: Add a module parameter to change nowayout setting
  2007-07-24 18:07 [PATCH 01/11] mv64x60_wdt: set up platform_device in platform code Dale Farnsworth
                   ` (5 preceding siblings ...)
  2007-07-24 18:16 ` [PATCH 07/11] mv64x60_wdt: Add WDIOC_SETOPTIONS ioctl support Dale Farnsworth
@ 2007-07-24 18:17 ` Dale Farnsworth
  2007-07-24 18:18 ` [PATCH 09/11] mv64x60_wdt: Support the WDIOF_MAGICCLOSE feature Dale Farnsworth
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 12+ messages in thread
From: Dale Farnsworth @ 2007-07-24 18:17 UTC (permalink / raw)
  To: Wim Van Sebroeck, linux-kernel

Also, use the WATCHDOG_NOWAYOUT macro, rather than #ifdefs,
and use __module_get to prevent module unloading if WATCHDOG_NOWAYOUT
is set.

Signed-off-by: Dale Farnsworth <dale@farnsworth.org>
---
 drivers/char/watchdog/mv64x60_wdt.c |   12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

Index: linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c
===================================================================
--- linux-2.6-powerpc-wdt.orig/drivers/char/watchdog/mv64x60_wdt.c	2007-07-19 22:11:42.000000000 +0000
+++ linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c	2007-07-19 22:34:01.000000000 +0000
@@ -45,6 +45,10 @@ static void __iomem *mv64x60_wdt_regs;
 static int mv64x60_wdt_timeout;
 static unsigned int bus_clk;
 
+static int nowayout = WATCHDOG_NOWAYOUT;
+module_param(nowayout, int, 0);
+MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
+
 static void mv64x60_wdt_reg_write(u32 val)
 {
 	/* Allow write only to CTL1 / CTL2 fields, retaining values in
@@ -99,6 +103,9 @@ static int mv64x60_wdt_open(struct inode
 	if (test_and_set_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags))
 		return -EBUSY;
 
+	if (nowayout)
+		__module_get(THIS_MODULE);
+
 	mv64x60_wdt_service();
 	mv64x60_wdt_handler_enable();
 
@@ -109,9 +116,8 @@ static int mv64x60_wdt_release(struct in
 {
 	mv64x60_wdt_service();
 
-#if !defined(CONFIG_WATCHDOG_NOWAYOUT)
-	mv64x60_wdt_handler_disable();
-#endif
+	if (!nowayout)
+		mv64x60_wdt_handler_disable();
 
 	clear_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags);
 


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

* [PATCH 09/11] mv64x60_wdt: Support the WDIOF_MAGICCLOSE feature
  2007-07-24 18:07 [PATCH 01/11] mv64x60_wdt: set up platform_device in platform code Dale Farnsworth
                   ` (6 preceding siblings ...)
  2007-07-24 18:17 ` [PATCH 08/11] mv64x60_wdt: Add a module parameter to change nowayout setting Dale Farnsworth
@ 2007-07-24 18:18 ` Dale Farnsworth
  2007-07-24 18:19 ` [PATCH 10/11] mv64x60_wdt: disable watchdog timer when driver is probed Dale Farnsworth
  2007-07-24 18:20 ` [PATCH 11/11] mv64x60_wdt_cleanup_low_level_wdt_code.patch Dale Farnsworth
  9 siblings, 0 replies; 12+ messages in thread
From: Dale Farnsworth @ 2007-07-24 18:18 UTC (permalink / raw)
  To: Wim Van Sebroeck, linux-kernel

Disallow disabling of the watchdog timer unless a particular
character ('V') was recently written to the watchdog device.

Signed-off-by: Dale Farnsworth <dale@farnsworth.org>
---
 drivers/char/watchdog/mv64x60_wdt.c |   28 ++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

Index: linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c
===================================================================
--- linux-2.6-powerpc-wdt.orig/drivers/char/watchdog/mv64x60_wdt.c	2007-07-20 19:32:41.000000000 +0000
+++ linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c	2007-07-20 19:33:30.000000000 +0000
@@ -44,6 +44,7 @@ static int wdt_status;
 static void __iomem *mv64x60_wdt_regs;
 static int mv64x60_wdt_timeout;
 static unsigned int bus_clk;
+static char expect_close;
 
 static int nowayout = WATCHDOG_NOWAYOUT;
 module_param(nowayout, int, 0);
@@ -115,10 +116,14 @@ static int mv64x60_wdt_open(struct inode
 
 static int mv64x60_wdt_release(struct inode *inode, struct file *file)
 {
-	mv64x60_wdt_service();
-
-	if (!nowayout)
+	if (expect_close == 42)
 		mv64x60_wdt_handler_disable();
+	else {
+		printk(KERN_CRIT
+		       "mv64x60_wdt: unexpected close, not stopping timer!\n");
+		mv64x60_wdt_service();
+	}
+	expect_close = 0;
 
 	clear_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags);
 
@@ -128,8 +133,22 @@ static int mv64x60_wdt_release(struct in
 static ssize_t mv64x60_wdt_write(struct file *file, const char __user *data,
 				 size_t len, loff_t * ppos)
 {
-	if (len)
+	if (len) {
+		if (!nowayout) {
+			size_t i;
+
+			expect_close = 0;
+
+			for (i = 0; i != len; i++) {
+				char c;
+				if(get_user(c, data + i))
+					return -EFAULT;
+				if (c == 'V')
+					expect_close = 42;
+			}
+		}
 		mv64x60_wdt_service();
+	}
 
 	return len;
 }
@@ -142,6 +161,7 @@ static int mv64x60_wdt_ioctl(struct inod
 	void __user *argp = (void __user *)arg;
 	static struct watchdog_info info = {
 		.options =	WDIOF_SETTIMEOUT	|
+				WDIOF_MAGICCLOSE	|
 				WDIOF_KEEPALIVEPING,
 		.firmware_version = 0,
 		.identity = "MV64x60 watchdog",


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

* [PATCH 10/11] mv64x60_wdt: disable watchdog timer when driver is probed
  2007-07-24 18:07 [PATCH 01/11] mv64x60_wdt: set up platform_device in platform code Dale Farnsworth
                   ` (7 preceding siblings ...)
  2007-07-24 18:18 ` [PATCH 09/11] mv64x60_wdt: Support the WDIOF_MAGICCLOSE feature Dale Farnsworth
@ 2007-07-24 18:19 ` Dale Farnsworth
  2007-07-24 18:20 ` [PATCH 11/11] mv64x60_wdt_cleanup_low_level_wdt_code.patch Dale Farnsworth
  9 siblings, 0 replies; 12+ messages in thread
From: Dale Farnsworth @ 2007-07-24 18:19 UTC (permalink / raw)
  To: Wim Van Sebroeck, linux-kernel

Signed-off-by: Dale Farnsworth <dale@farnsworth.org>
---
 drivers/char/watchdog/mv64x60_wdt.c |    2 ++
 1 file changed, 2 insertions(+)

Index: linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c
===================================================================
--- linux-2.6-powerpc-wdt.orig/drivers/char/watchdog/mv64x60_wdt.c	2007-07-20 19:33:30.000000000 +0000
+++ linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c	2007-07-20 19:34:23.000000000 +0000
@@ -262,6 +262,8 @@ static int __devinit mv64x60_wdt_probe(s
 
 	mv64x60_wdt_set_timeout(timeout);
 
+	mv64x60_wdt_handler_disable();	/* in case timer was already running */
+
 	return misc_register(&mv64x60_wdt_miscdev);
 }
 


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

* [PATCH 11/11] mv64x60_wdt_cleanup_low_level_wdt_code.patch
  2007-07-24 18:07 [PATCH 01/11] mv64x60_wdt: set up platform_device in platform code Dale Farnsworth
                   ` (8 preceding siblings ...)
  2007-07-24 18:19 ` [PATCH 10/11] mv64x60_wdt: disable watchdog timer when driver is probed Dale Farnsworth
@ 2007-07-24 18:20 ` Dale Farnsworth
  2007-07-24 18:31   ` [PATCH 11/11, corrected] mv64x60_wdt: Rework the timeout register manipulation Dale Farnsworth
  9 siblings, 1 reply; 12+ messages in thread
From: Dale Farnsworth @ 2007-07-24 18:20 UTC (permalink / raw)
  To: Wim Van Sebroeck, linux-kernel

Consolidate the timeout config register modification into a single
function.  Also, use the enabled flag in the config register to
determine whether the timer is enabled instead of a separately
maintained flag, MV64x60_WDOG_FLAG_ENABLED.

Add spinlock protection around enabling/disabling the watchdog timer.

Signed-off-by: Dale Farnsworth <dale@farnsworth.org>
---
 drivers/char/watchdog/mv64x60_wdt.c |   85 +++++++++++++++-----------
 1 file changed, 51 insertions(+), 34 deletions(-)

Index: linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c
===================================================================
--- linux-2.6-powerpc-wdt.orig/drivers/char/watchdog/mv64x60_wdt.c	2007-07-20 20:05:04.000000000 +0000
+++ linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c	2007-07-21 12:51:59.000000000 +0000
@@ -26,78 +26,97 @@
 #include <linux/mv643xx.h>
 #include <asm/uaccess.h>
 #include <asm/io.h>
+#include <linux/delay.h>
 
 #define MV64x60_WDT_WDC_OFFSET	0
 
-/* MV64x60 WDC (config) register access definitions */
-#define MV64x60_WDC_CTL1_MASK	(3 << 24)
-#define MV64x60_WDC_CTL1(val)	((val & 3) << 24)
-#define MV64x60_WDC_CTL2_MASK	(3 << 26)
-#define MV64x60_WDC_CTL2(val)	((val & 3) << 26)
+/*
+ * The watchdog configuration register contains a pair of 2-bit fields,
+ *   1.  a reload field, bits 27-26, which triggers a reload of
+ *       the countdown register, and
+ *   2.  an enable field, bits 25-24, which toggles between
+ *       enabling and disabling the watchdog timer.
+ * Bit 31 is a read-only field which indicates whether the
+ * watchdog timer is currently enabled.
+ *
+ * The low 24 bits contain the timer reload value.
+ */
+#define MV64x60_WDC_ENABLE_SHIFT	24
+#define MV64x60_WDC_LOAD_SHIFT		26
+#define MV64x60_WDC_ENABLED_SHIFT	31
 
 /* Flags bits */
 #define MV64x60_WDOG_FLAG_OPENED	0
-#define MV64x60_WDOG_FLAG_ENABLED	1
 
 static unsigned long wdt_flags;
 static int wdt_status;
 static void __iomem *mv64x60_wdt_regs;
 static int mv64x60_wdt_timeout;
+static int mv64x60_wdt_count;
 static unsigned int bus_clk;
 static char expect_close;
+static DEFINE_SPINLOCK(mv64x60_wdt_spinlock);
 
 static int nowayout = WATCHDOG_NOWAYOUT;
 module_param(nowayout, int, 0);
 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
 
-static void mv64x60_wdt_reg_write(u32 val)
+static void mv64x60_wdt_update_wdc(int field_shift)
 {
-	/* Allow write only to CTL1 / CTL2 fields, retaining values in
-	 * other fields.
-	 */
-	u32 data = readl(mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
-	data &= ~(MV64x60_WDC_CTL1_MASK | MV64x60_WDC_CTL2_MASK);
-	data |= val;
+	u32 data;
+
+	/* We write a 1, then a 2 -- to the appropriate field */
+	data = (1 << field_shift) | mv64x60_wdt_count;
+	writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
+
+	data = (2 << field_shift) | mv64x60_wdt_count;
 	writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
 }
 
 static void mv64x60_wdt_service(void)
 {
-	/* Write 01 followed by 10 to CTL2 */
-	mv64x60_wdt_reg_write(MV64x60_WDC_CTL2(0x01));
-	mv64x60_wdt_reg_write(MV64x60_WDC_CTL2(0x02));
+	spin_lock(&mv64x60_wdt_spinlock);
+	mv64x60_wdt_update_wdc(MV64x60_WDC_LOAD_SHIFT);
+	spin_unlock(&mv64x60_wdt_spinlock);
 }
 
-static void mv64x60_wdt_handler_disable(void)
+static void mv64x60_wdt_handler_enable(void)
 {
-	if (test_and_clear_bit(MV64x60_WDOG_FLAG_ENABLED, &wdt_flags)) {
-		/* Write 01 followed by 10 to CTL1 */
-		mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x01));
-		mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x02));
-		printk(KERN_NOTICE "mv64x60_wdt: watchdog deactivated\n");
+	u32 data;
+
+	spin_lock(&mv64x60_wdt_spinlock);
+	data = readl(mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
+
+	if (!(data & (1 << MV64x60_WDC_ENABLED_SHIFT))) {
+		mv64x60_wdt_update_wdc(MV64x60_WDC_ENABLE_SHIFT);
+		mv64x60_wdt_service();
+		printk(KERN_NOTICE "mv64x60_wdt: watchdog activated\n");
 	}
+	spin_unlock(&mv64x60_wdt_spinlock);
 }
 
-static void mv64x60_wdt_handler_enable(void)
+static void mv64x60_wdt_handler_disable(void)
 {
-	if (!test_and_set_bit(MV64x60_WDOG_FLAG_ENABLED, &wdt_flags)) {
-		/* Write 01 followed by 10 to CTL1 */
-		mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x01));
-		mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x02));
-		printk(KERN_NOTICE "mv64x60_wdt: watchdog activated\n");
+	u32 data;
+
+	spin_lock(&mv64x60_wdt_spinlock);
+	data = readl(mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
+
+	if (data & (1 << MV64x60_WDC_ENABLED_SHIFT)) {
+		mv64x60_wdt_update_wdc(MV64x60_WDC_ENABLE_SHIFT);
+		printk(KERN_NOTICE "mv64x60_wdt: watchdog deactivated\n");
 	}
+	spin_unlock(&mv64x60_wdt_spinlock);
 }
 
-static void mv64x60_wdt_set_timeout(int timeout)
+static void mv64x60_wdt_set_timeout(unsigned int timeout)
 {
 	/* maximum bus cycle count is 0xFFFFFFFF */
 	if (timeout > 0xFFFFFFFF / bus_clk)
 		timeout = 0xFFFFFFFF / bus_clk;
 
+	mv64x60_wdt_count = timeout * bus_clk >> 8;
 	mv64x60_wdt_timeout = timeout;
-	writel((timeout * bus_clk) >> 8,
-	       mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
-	mv64x60_wdt_service();
 }
 
 static int mv64x60_wdt_open(struct inode *inode, struct file *file)
@@ -108,7 +127,6 @@ static int mv64x60_wdt_open(struct inode
 	if (nowayout)
 		__module_get(THIS_MODULE);
 
-	mv64x60_wdt_service();
 	mv64x60_wdt_handler_enable();
 
 	return nonseekable_open(inode, file);
@@ -270,7 +288,6 @@ static int __devexit mv64x60_wdt_remove(
 {
 	misc_deregister(&mv64x60_wdt_miscdev);
 
-	mv64x60_wdt_service();
 	mv64x60_wdt_handler_disable();
 
 	iounmap(mv64x60_wdt_regs);


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

* [PATCH 11/11, corrected] mv64x60_wdt: Rework the timeout register manipulation
  2007-07-24 18:20 ` [PATCH 11/11] mv64x60_wdt_cleanup_low_level_wdt_code.patch Dale Farnsworth
@ 2007-07-24 18:31   ` Dale Farnsworth
  0 siblings, 0 replies; 12+ messages in thread
From: Dale Farnsworth @ 2007-07-24 18:31 UTC (permalink / raw)
  To: Wim Van Sebroeck, linux-kernel

mv64x60_wdt: Rework the timeout register manipulation

Consolidate the timeout config register modification into a single
function.  Also, use the enabled flag in the config register to
determine whether the timer is enabled instead of a separately
maintained flag, MV64x60_WDOG_FLAG_ENABLED.

Add spinlock protection around enabling/disabling the watchdog timer.

Signed-off-by: Dale Farnsworth <dale@farnsworth.org>
---
Oops!  I mistakenly sent an earlier version of this patch.  Please
disregard it and replace it with this one.

 drivers/char/watchdog/mv64x60_wdt.c |   90 +++++++++++++++-----------
 1 file changed, 54 insertions(+), 36 deletions(-)

Index: linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c
===================================================================
--- linux-2.6-powerpc-wdt.orig/drivers/char/watchdog/mv64x60_wdt.c	2007-07-20 20:05:04.000000000 +0000
+++ linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c	2007-07-24 18:28:58.000000000 +0000
@@ -29,75 +29,95 @@
 
 #define MV64x60_WDT_WDC_OFFSET	0
 
-/* MV64x60 WDC (config) register access definitions */
-#define MV64x60_WDC_CTL1_MASK	(3 << 24)
-#define MV64x60_WDC_CTL1(val)	((val & 3) << 24)
-#define MV64x60_WDC_CTL2_MASK	(3 << 26)
-#define MV64x60_WDC_CTL2(val)	((val & 3) << 26)
+/*
+ * The watchdog configuration register contains a pair of 2-bit fields,
+ *   1.  a reload field, bits 27-26, which triggers a reload of
+ *       the countdown register, and
+ *   2.  an enable field, bits 25-24, which toggles between
+ *       enabling and disabling the watchdog timer.
+ * Bit 31 is a read-only field which indicates whether the
+ * watchdog timer is currently enabled.
+ *
+ * The low 24 bits contain the timer reload value.
+ */
+#define MV64x60_WDC_ENABLE_SHIFT	24
+#define MV64x60_WDC_SERVICE_SHIFT	26
+#define MV64x60_WDC_ENABLED_SHIFT	31
+
+#define MV64x60_WDC_ENABLED_TRUE	1
+#define MV64x60_WDC_ENABLED_FALSE	0
 
 /* Flags bits */
 #define MV64x60_WDOG_FLAG_OPENED	0
-#define MV64x60_WDOG_FLAG_ENABLED	1
 
 static unsigned long wdt_flags;
 static int wdt_status;
 static void __iomem *mv64x60_wdt_regs;
 static int mv64x60_wdt_timeout;
+static int mv64x60_wdt_count;
 static unsigned int bus_clk;
 static char expect_close;
+static DEFINE_SPINLOCK(mv64x60_wdt_spinlock);
 
 static int nowayout = WATCHDOG_NOWAYOUT;
 module_param(nowayout, int, 0);
 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
 
-static void mv64x60_wdt_reg_write(u32 val)
+static int mv64x60_wdt_toggle_wdc(int enabled_predicate, int field_shift)
 {
-	/* Allow write only to CTL1 / CTL2 fields, retaining values in
-	 * other fields.
-	 */
-	u32 data = readl(mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
-	data &= ~(MV64x60_WDC_CTL1_MASK | MV64x60_WDC_CTL2_MASK);
-	data |= val;
-	writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
+	u32 data;
+	u32 enabled;
+	int ret = 0;
+
+	spin_lock(&mv64x60_wdt_spinlock);
+	data = readl(mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
+	enabled = (data >> MV64x60_WDC_ENABLED_SHIFT) & 1;
+
+	/* only toggle the requested field if enabled state matches predicate */
+	if ((enabled ^ enabled_predicate) == 0) {
+		/* We write a 1, then a 2 -- to the appropriate field */
+		data = (1 << field_shift) | mv64x60_wdt_count;
+		writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
+
+		data = (2 << field_shift) | mv64x60_wdt_count;
+		writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
+		ret = 1;
+	}
+	spin_unlock(&mv64x60_wdt_spinlock);
+
+	return ret;
 }
 
 static void mv64x60_wdt_service(void)
 {
-	/* Write 01 followed by 10 to CTL2 */
-	mv64x60_wdt_reg_write(MV64x60_WDC_CTL2(0x01));
-	mv64x60_wdt_reg_write(MV64x60_WDC_CTL2(0x02));
+	mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_TRUE,
+			       MV64x60_WDC_SERVICE_SHIFT);
 }
 
-static void mv64x60_wdt_handler_disable(void)
+static void mv64x60_wdt_handler_enable(void)
 {
-	if (test_and_clear_bit(MV64x60_WDOG_FLAG_ENABLED, &wdt_flags)) {
-		/* Write 01 followed by 10 to CTL1 */
-		mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x01));
-		mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x02));
-		printk(KERN_NOTICE "mv64x60_wdt: watchdog deactivated\n");
+	if (mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_FALSE,
+				   MV64x60_WDC_ENABLE_SHIFT)) {
+		mv64x60_wdt_service();
+		printk(KERN_NOTICE "mv64x60_wdt: watchdog activated\n");
 	}
 }
 
-static void mv64x60_wdt_handler_enable(void)
+static void mv64x60_wdt_handler_disable(void)
 {
-	if (!test_and_set_bit(MV64x60_WDOG_FLAG_ENABLED, &wdt_flags)) {
-		/* Write 01 followed by 10 to CTL1 */
-		mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x01));
-		mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x02));
-		printk(KERN_NOTICE "mv64x60_wdt: watchdog activated\n");
-	}
+	if (mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_TRUE,
+				   MV64x60_WDC_ENABLE_SHIFT))
+		printk(KERN_NOTICE "mv64x60_wdt: watchdog deactivated\n");
 }
 
-static void mv64x60_wdt_set_timeout(int timeout)
+static void mv64x60_wdt_set_timeout(unsigned int timeout)
 {
 	/* maximum bus cycle count is 0xFFFFFFFF */
 	if (timeout > 0xFFFFFFFF / bus_clk)
 		timeout = 0xFFFFFFFF / bus_clk;
 
+	mv64x60_wdt_count = timeout * bus_clk >> 8;
 	mv64x60_wdt_timeout = timeout;
-	writel((timeout * bus_clk) >> 8,
-	       mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
-	mv64x60_wdt_service();
 }
 
 static int mv64x60_wdt_open(struct inode *inode, struct file *file)
@@ -108,7 +128,6 @@ static int mv64x60_wdt_open(struct inode
 	if (nowayout)
 		__module_get(THIS_MODULE);
 
-	mv64x60_wdt_service();
 	mv64x60_wdt_handler_enable();
 
 	return nonseekable_open(inode, file);
@@ -270,7 +289,6 @@ static int __devexit mv64x60_wdt_remove(
 {
 	misc_deregister(&mv64x60_wdt_miscdev);
 
-	mv64x60_wdt_service();
 	mv64x60_wdt_handler_disable();
 
 	iounmap(mv64x60_wdt_regs);


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

end of thread, other threads:[~2007-07-24 18:31 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-07-24 18:07 [PATCH 01/11] mv64x60_wdt: set up platform_device in platform code Dale Farnsworth
2007-07-24 18:09 ` [PATCH 02/11] mv64x60_wdt: Get register address from platform data Dale Farnsworth
2007-07-24 18:12 ` [PATCH 03/11] mv64x60_wdt: Add arch/powerpc platform support Dale Farnsworth
2007-07-24 18:13 ` [PATCH 04/11] mv64x60_wdt: Check return value of nonseekable_open Dale Farnsworth
2007-07-24 18:14 ` [PATCH 05/11] mv64x60_wdt: Fix WDIOC_GETTIMEOUT return value Dale Farnsworth
2007-07-24 18:15 ` [PATCH 06/11] mv64x60_wdt: Support for WDIOC_SETTIMEOUT ioctl Dale Farnsworth
2007-07-24 18:16 ` [PATCH 07/11] mv64x60_wdt: Add WDIOC_SETOPTIONS ioctl support Dale Farnsworth
2007-07-24 18:17 ` [PATCH 08/11] mv64x60_wdt: Add a module parameter to change nowayout setting Dale Farnsworth
2007-07-24 18:18 ` [PATCH 09/11] mv64x60_wdt: Support the WDIOF_MAGICCLOSE feature Dale Farnsworth
2007-07-24 18:19 ` [PATCH 10/11] mv64x60_wdt: disable watchdog timer when driver is probed Dale Farnsworth
2007-07-24 18:20 ` [PATCH 11/11] mv64x60_wdt_cleanup_low_level_wdt_code.patch Dale Farnsworth
2007-07-24 18:31   ` [PATCH 11/11, corrected] mv64x60_wdt: Rework the timeout register manipulation Dale Farnsworth

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

Powered by JetHome