Skip to contentSkip to conversation

mv64x60_wdt: set up platform_device in platform code

PATCH New Dale Farnsworth 1 participant 12 messages Jul 24, 2007

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/11 mv64x60_wdt: set up platform_device in platform code

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.

---
 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(-)
arch/ppc/syslib/mv64x60.c +29 -0
Index: linux-2.6-powerpc-wdt/arch/ppc/syslib/mv64x60.c
===================================================================
@@ -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
drivers/char/watchdog/mv64x60_wdt.c +1 -25
Index: linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c
===================================================================
@@ -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);
include/asm-ppc/mv64x60.h +1 -1
Index: linux-2.6-powerpc-wdt/include/asm-ppc/mv64x60.h
===================================================================
@@ -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 */
#1 lore raw

PATCH 2/11 mv64x60_wdt: Get register address from platform data

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.

---
 drivers/char/watchdog/mv64x60_wdt.c |   21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
drivers/char/watchdog/mv64x60_wdt.c +16 -5
Index: linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c
===================================================================
@@ -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;
 }
 
#2 lore raw

PATCH 3/11 mv64x60_wdt: Add arch/powerpc platform support

In reply to Dale Farnsworth

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

---
 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(-)
arch/powerpc/boot/dts/prpmc2800.dts +6 -0
Index: linux-2.6-powerpc-wdt/arch/powerpc/boot/dts/prpmc2800.dts
===================================================================
@@ -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";
arch/powerpc/sysdev/mv64x60_dev.c +64 -0

76 lines not on this page · raw message

drivers/char/watchdog/mv64x60_wdt.c +1 -1
Index: linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c
===================================================================
@@ -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>
 
include/asm-ppc/mv64x60.h +0 -8
Index: linux-2.6-powerpc-wdt/include/asm-ppc/mv64x60.h
===================================================================
@@ -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.
include/linux/mv643xx.h +8 -0
Index: linux-2.6-powerpc-wdt/include/linux/mv643xx.h
===================================================================
@@ -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 */
#3 lore raw

PATCH 4/11 mv64x60_wdt: Check return value of nonseekable_open

In reply to Dale Farnsworth
---
 drivers/char/watchdog/mv64x60_wdt.c |    4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/char/watchdog/mv64x60_wdt.c +1 -3
Index: linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c
===================================================================
@@ -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)
#4 lore raw

PATCH 5/11 mv64x60_wdt: Fix WDIOC_GETTIMEOUT return value

In reply to Dale Farnsworth

WDIOC_GETTIMEOUT returns seconds, not jiffies.

---
 drivers/char/watchdog/mv64x60_wdt.c |    4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/char/watchdog/mv64x60_wdt.c +1 -3
Index: linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c
===================================================================
@@ -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;
 
#5 lore raw

PATCH 6/11 mv64x60_wdt: Support for WDIOC_SETTIMEOUT ioctl

In reply to Dale Farnsworth

Add the ability to modify the watchdog timer timeout interval.

---
 drivers/char/watchdog/mv64x60_wdt.c |   38 +++++++++++++++++++++-----
 1 file changed, 31 insertions(+), 7 deletions(-)
drivers/char/watchdog/mv64x60_wdt.c +31 -7
Index: linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c
===================================================================
@@ -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);
 }
#6 lore raw

PATCH 7/11 mv64x60_wdt: Add WDIOC_SETOPTIONS ioctl support

In reply to Dale Farnsworth

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

---
 drivers/char/watchdog/mv64x60_wdt.c |   11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/char/watchdog/mv64x60_wdt.c +10 -1
Index: linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c
===================================================================
@@ -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();
#7 lore raw

PATCH 8/11 mv64x60_wdt: Add a module parameter to change nowayout setting

In reply to Dale Farnsworth

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

---
 drivers/char/watchdog/mv64x60_wdt.c |   12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/char/watchdog/mv64x60_wdt.c +9 -3
Index: linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c
===================================================================
@@ -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);
 
#8 lore raw

PATCH 9/11 mv64x60_wdt: Support the WDIOF_MAGICCLOSE feature

In reply to Dale Farnsworth

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

---
 drivers/char/watchdog/mv64x60_wdt.c |   28 ++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)
drivers/char/watchdog/mv64x60_wdt.c +24 -4
Index: linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c
===================================================================
@@ -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",
#9 lore raw

PATCH 10/11 mv64x60_wdt: disable watchdog timer when driver is probed

In reply to Dale Farnsworth
---
 drivers/char/watchdog/mv64x60_wdt.c |    2 ++
 1 file changed, 2 insertions(+)
drivers/char/watchdog/mv64x60_wdt.c +2 -0
Index: linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c
===================================================================
@@ -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);
 }
 
#10 lore raw

PATCH 11/11 mv64x60_wdt_cleanup_low_level_wdt_code.patch

In reply to Dale Farnsworth

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.

---
 drivers/char/watchdog/mv64x60_wdt.c |   85 +++++++++++++++-----------
 1 file changed, 51 insertions(+), 34 deletions(-)
drivers/char/watchdog/mv64x60_wdt.c +51 -34
Index: linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c
===================================================================
@@ -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);
#11 lore raw

PATCH 11/11 mv64x60_wdt: Rework the timeout register manipulation

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.

---
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(-)
drivers/char/watchdog/mv64x60_wdt.c +54 -36
Index: linux-2.6-powerpc-wdt/drivers/char/watchdog/mv64x60_wdt.c
===================================================================
@@ -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);