Re: [PATCH] Add watchdog driver for w90p910

From: Wan ZongShun
Date: Thu Jul 30 2009 - 01:03:47 EST


Hi Wim ,

Okay, got it, thanks a lot for your help!

2009/7/30 Wim Van Sebroeck <wim@xxxxxxxxx>:
> Hi Wan,
>
> will be reviewed still this week.
>
> Kind regards,
> Wim.
>
>> Dear Wim,
>>
>> How about this watchdog patch?
>> Would you mind applying?
>>
>> Thanks for your help.
>>
>> 2009/7/24, Wan ZongShun <mcuos.com@xxxxxxxxx>:
>> > Dear Wim Van Sebroeck,
>> >
>> > This is a watchdog driver for my w90p910 platform.
>> >
>> > Thanks!
>> >
>> >
>> >
>> > Add watchdog driver for w90p910 platform.
>> >
>> > Signed-off-by: Wan ZongShun <mcuos.com@xxxxxxxxx>
>> >
>> > ---
>> > Âdrivers/watchdog/Kconfig    |  Â9 +
>> > Âdrivers/watchdog/Makefile   Â|  Â1 +
>> > Âdrivers/watchdog/w90p910_wdt.c | Â340 ++++++++++++++++++++++++++++++++++++++++
>> > Â3 files changed, 350 insertions(+), 0 deletions(-)
>> > Âcreate mode 100644 drivers/watchdog/w90p910_wdt.c
>> >
>> > diff --git a/drivers/watchdog/Kconfig b/drivers/watchdog/Kconfig
>> > index b1ccc04..fba4b91 100644
>> > --- a/drivers/watchdog/Kconfig
>> > +++ b/drivers/watchdog/Kconfig
>> > @@ -266,6 +266,15 @@ config STMP3XXX_WATCHDOG
>> > Â Â Â Â ÂTo compile this driver as a module, choose M here: the
>> > Â Â Â Â Âmodule will be called stmp3xxx_wdt.
>> >
>> > +config W90P910_WATCHDOG
>> > + Â Â Â tristate "Nuvoton W90P910 watchdog"
>> > + Â Â Â depends on ARCH_W90X900
>> > + Â Â Â help
>> > + Â Â Â Â Say Y here if to include support for the watchdog timer
>> > + Â Â Â Â for the Nuvoton W90P910 SoC.
>> > + Â Â Â Â To compile this driver as a module, choose M here: the
>> > + Â Â Â Â module will be called w90p910_wdt.
>> > +
>> > Â# AVR32 Architecture
>> >
>> > Âconfig AT32AP700X_WDT
>> > diff --git a/drivers/watchdog/Makefile b/drivers/watchdog/Makefile
>> > index 3d77429..ecc05b4 100644
>> > --- a/drivers/watchdog/Makefile
>> > +++ b/drivers/watchdog/Makefile
>> > @@ -44,6 +44,7 @@ obj-$(CONFIG_DAVINCI_WATCHDOG) += davinci_wdt.o
>> > Âobj-$(CONFIG_ORION_WATCHDOG) += orion_wdt.o
>> > Âobj-$(CONFIG_COH901327_WATCHDOG) += coh901327_wdt.o
>> > Âobj-$(CONFIG_STMP3XXX_WATCHDOG) += stmp3xxx_wdt.o
>> > +obj-$(CONFIG_W90P910_WATCHDOG) += w90p910_wdt.o
>> >
>> > Â# AVR32 Architecture
>> > Âobj-$(CONFIG_AT32AP700X_WDT) += at32ap700x_wdt.o
>> > diff --git a/drivers/watchdog/w90p910_wdt.c b/drivers/watchdog/w90p910_wdt.c
>> > new file mode 100644
>> > index 0000000..a73f85a
>> > --- /dev/null
>> > +++ b/drivers/watchdog/w90p910_wdt.c
>> > @@ -0,0 +1,340 @@
>> > +/*
>> > + * Copyright (c) 2009 Nuvoton technology corporation.
>> > + *
>> > + * Wan ZongShun <mcuos.com@xxxxxxxxx>
>> > + *
>> > + * This program is free software; you can redistribute it and/or modify
>> > + * it under the terms of the GNU General Public License as published by
>> > + * the Free Software Foundation;version 2 of the License.
>> > + *
>> > + */
>> > +
>> > +#include <linux/bitops.h>
>> > +#include <linux/errno.h>
>> > +#include <linux/fs.h>
>> > +#include <linux/init.h>
>> > +#include <linux/io.h>
>> > +#include <linux/clk.h>
>> > +#include <linux/kernel.h>
>> > +#include <linux/miscdevice.h>
>> > +#include <linux/module.h>
>> > +#include <linux/moduleparam.h>
>> > +#include <linux/platform_device.h>
>> > +#include <linux/interrupt.h>
>> > +#include <linux/types.h>
>> > +#include <linux/watchdog.h>
>> > +#include <linux/uaccess.h>
>> > +
>> > +#define WDT_DEFAULT_TIME_LEVEL 0 Â Â Â /* if 0, time = 2^14 clocks */
>> > +#define WDT_MAX_TIME_LEVEL Â Â 3 Â Â Â /* if 3, time = 2^20 clocks */
>> > +#define REG_WTCR Â Â Â Â Â Â Â 0x1c
>> > +#define WTCLK Â Â Â Â Â Â Â Â Â(0x01 << 10)
>> > +#define WTE Â Â Â Â Â Â Â Â Â Â(0x01 << 7) Â Â /*wdt enable*/
>> > +#define WTIE Â Â Â Â Â Â Â Â Â (0x01 << 6) Â Â /*wdt interrupt enable*/
>> > +#define WTISBIT Â Â Â Â Â Â Â Â Â Â Â Â(0x04)
>> > +#define WTIS Â Â Â Â Â Â Â Â Â (0x03 << 4)
>> > +#define WTIF Â Â Â Â Â Â Â Â Â (0x01 << 3)
>> > +#define WTRF Â Â Â Â Â Â Â Â Â (0x01 << 2)
>> > +#define WTRE Â Â Â Â Â Â Â Â Â (0x01 << 1)
>> > +#define WTR Â Â Â Â Â Â Â Â Â Â0x01
>> > +
>> > +static int wdt_time = WDT_DEFAULT_TIME_LEVEL;
>> > +static int nowayout = WATCHDOG_NOWAYOUT;
>> > +
>> > +module_param(wdt_time, int, 0);
>> > +MODULE_PARM_DESC(wdt_time, "Watchdog time in seconds. (default="
>> > + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â __MODULE_STRING(WDT_DEFAULT_TIME) ")");
>> > +
>> > +#ifdef CONFIG_WATCHDOG_NOWAYOUT
>> > +module_param(nowayout, int, 0);
>> > +MODULE_PARM_DESC(nowayout,
>> > + Â Â Â Â Â Â Â "Watchdog cannot be stopped once started (default="
>> > + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
>> > +#endif
>> > +
>> > +struct w90p910_wdt {
>> > + Â Â Â struct resource Â*res;
>> > +    struct clk    *wdt_clock;
>> > + Â Â Â struct platform_device *pdev;
>> > +    unsigned int   open_lock;
>> > +    unsigned int   wdt_irq;
>> > +    void __iomem   *wdt_base;
>> > +    char       expect_close;
>> > +    spinlock_t    wdt_lock;
>> > +};
>> > +
>> > +static unsigned long w90p910wdt_busy;
>> > +struct w90p910_wdt *w90p910_wdt;
>> > +
>> > +static void w90p910_wdt_keepalive(void)
>> > +{
>> > + Â Â Â unsigned int val;
>> > +
>> > + Â Â Â spin_lock(&w90p910_wdt->wdt_lock);
>> > +
>> > + Â Â Â val = __raw_readl(w90p910_wdt->wdt_base + REG_WTCR);
>> > + Â Â Â val |= (WTR | WTIF);
>> > + Â Â Â __raw_writel(val, w90p910_wdt->wdt_base + REG_WTCR);
>> > +
>> > + Â Â Â spin_unlock(&w90p910_wdt->wdt_lock);
>> > +}
>> > +
>> > +static irqreturn_t w90p910_wdt_irq(int irq, void *dev_id)
>> > +{
>> > + Â Â Â w90p910_wdt_keepalive();
>> > + Â Â Â return IRQ_HANDLED;
>> > +}
>> > +
>> > +static inline void w90p910_wdt_stop(void)
>> > +{
>> > + Â Â Â unsigned int val;
>> > +
>> > + Â Â Â spin_lock(&w90p910_wdt->wdt_lock);
>> > +
>> > + Â Â Â val = __raw_readl(w90p910_wdt->wdt_base + REG_WTCR);
>> > + Â Â Â val &= ~WTE;
>> > + Â Â Â __raw_writel(val, w90p910_wdt->wdt_base + REG_WTCR);
>> > +
>> > + Â Â Â spin_unlock(&w90p910_wdt->wdt_lock);
>> > +}
>> > +
>> > +static inline void w90p910_wdt_start(void)
>> > +{
>> > + Â Â Â unsigned int val;
>> > +
>> > + Â Â Â spin_lock(&w90p910_wdt->wdt_lock);
>> > +
>> > + Â Â Â val = __raw_readl(w90p910_wdt->wdt_base + REG_WTCR);
>> > + Â Â Â val |= (WTRE | WTIE | WTE | WTR | WTCLK | WTIF);
>> > + Â Â Â val |= (wdt_time << 0x04);
>> > + Â Â Â __raw_writel(val, w90p910_wdt->wdt_base + REG_WTCR);
>> > +
>> > + Â Â Â spin_unlock(&w90p910_wdt->wdt_lock);
>> > +}
>> > +
>> > +static int w90p910_wdt_open(struct inode *inode, struct file *file)
>> > +{
>> > +
>> > + Â Â Â if (test_and_set_bit(0, &w90p910wdt_busy))
>> > + Â Â Â Â Â Â Â return -EBUSY;
>> > +
>> > + Â Â Â w90p910_wdt_start();
>> > + Â Â Â return nonseekable_open(inode, file);
>> > +}
>> > +
>> > +static int w90p910_wdt_close(struct inode *inode, struct file *file)
>> > +{
>> > + Â Â Â if (!nowayout)
>> > + Â Â Â Â Â Â Â w90p910_wdt_stop();
>> > +
>> > + Â Â Â clear_bit(0, &w90p910wdt_busy);
>> > + Â Â Â return 0;
>> > +}
>> > +
>> > +static int w90p910_wdt_settimeout(int new_time_level)
>> > +{
>> > + Â Â Â unsigned int val;
>> > +
>> > + Â Â Â if ((new_time_level < 0) || (new_time_level > WDT_MAX_TIME_LEVEL))
>> > + Â Â Â Â Â Â Â return -EINVAL;
>> > +
>> > + Â Â Â val = __raw_readl(w90p910_wdt->wdt_base + REG_WTCR);
>> > + Â Â Â val &= ~WTIS;
>> > + Â Â Â val |= new_time_level;
>> > + Â Â Â __raw_writel(val, w90p910_wdt->wdt_base + REG_WTCR);
>> > +
>> > + Â Â Â wdt_time = new_time_level;
>> > + Â Â Â return 0;
>> > +}
>> > +
>> > +static struct watchdog_info w90p910_wdt_info = {
>> > +    .identity    = "w90p910 watchdog",
>> > +    .options    Â= WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
>> > +};
>> > +
>> > +static long w90p910_wdt_ioctl(struct file *file,
>> > + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â unsigned int cmd, unsigned long arg)
>> > +{
>> > + Â Â Â void __user *argp = (void __user *)arg;
>> > + Â Â Â int __user *p = argp;
>> > + Â Â Â int new_value;
>> > +
>> > + Â Â Â switch (cmd) {
>> > + Â Â Â case WDIOC_GETSUPPORT:
>> > + Â Â Â Â Â Â Â return copy_to_user(argp, &w90p910_wdt_info,
>> > + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â sizeof(w90p910_wdt_info)) ? -EFAULT : 0;
>> > + Â Â Â case WDIOC_GETSTATUS:
>> > + Â Â Â case WDIOC_GETBOOTSTATUS:
>> > + Â Â Â Â Â Â Â return put_user(0, p);
>> > + Â Â Â case WDIOC_SETOPTIONS:
>> > + Â Â Â Â Â Â Â if (get_user(new_value, p))
>> > + Â Â Â Â Â Â Â Â Â Â Â return -EFAULT;
>> > + Â Â Â Â Â Â Â if (new_value & WDIOS_DISABLECARD)
>> > + Â Â Â Â Â Â Â Â Â Â Â w90p910_wdt_stop();
>> > + Â Â Â Â Â Â Â if (new_value & WDIOS_ENABLECARD)
>> > + Â Â Â Â Â Â Â Â Â Â Â w90p910_wdt_start();
>> > + Â Â Â Â Â Â Â return 0;
>> > + Â Â Â case WDIOC_KEEPALIVE:
>> > + Â Â Â Â Â Â Â w90p910_wdt_keepalive();
>> > + Â Â Â Â Â Â Â return 0;
>> > + Â Â Â case WDIOC_SETTIMEOUT:
>> > + Â Â Â Â Â Â Â if (get_user(new_value, p))
>> > + Â Â Â Â Â Â Â Â Â Â Â return -EFAULT;
>> > + Â Â Â Â Â Â Â if (w90p910_wdt_settimeout(new_value))
>> > + Â Â Â Â Â Â Â Â Â Â Â return -EINVAL;
>> > +
>> > + Â Â Â Â Â Â Â w90p910_wdt_start();
>> > +
>> > + Â Â Â Â Â Â Â return put_user(wdt_time, p);
>> > + Â Â Â case WDIOC_GETTIMEOUT:
>> > + Â Â Â Â Â Â Â return put_user(wdt_time, p);
>> > + Â Â Â default:
>> > + Â Â Â Â Â Â Â return -ENOTTY;
>> > + Â Â Â }
>> > +}
>> > +
>> > +static ssize_t w90p910_wdt_write(struct file *file, const char *data,
>> > + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â size_t len, loff_t *ppos)
>> > +{
>> > + Â Â Â w90p910_wdt_keepalive();
>> > + Â Â Â return len;
>> > +}
>> > +
>> > +static const struct file_operations w90p910wdt_fops = {
>> > +    .owner     Â= THIS_MODULE,
>> > +    .llseek     = no_llseek,
>> > + Â Â Â .unlocked_ioctl = w90p910_wdt_ioctl,
>> > +    .open      = w90p910_wdt_open,
>> > +    .release    Â= w90p910_wdt_close,
>> > +    .write     Â= w90p910_wdt_write,
>> > +};
>> > +
>> > +static struct miscdevice w90p910wdt_miscdev = {
>> > +    .minor     Â= WATCHDOG_MINOR,
>> > +    .name      = "watchdog",
>> > +    .fops      = &w90p910wdt_fops,
>> > +};
>> > +
>> > +static int __devinit w90p910wdt_probe(struct platform_device *pdev)
>> > +{
>> > + Â Â Â int ret;
>> > +
>> > + Â Â Â w90p910_wdt = kzalloc(sizeof(struct w90p910_wdt), GFP_KERNEL);
>> > + Â Â Â if (!w90p910_wdt)
>> > + Â Â Â Â Â Â Â return -ENOMEM;
>> > +
>> > + Â Â Â w90p910_wdt->pdev = pdev;
>> > +
>> > + Â Â Â w90p910_wdt->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
>> > + Â Â Â if (w90p910_wdt->res == NULL) {
>> > + Â Â Â Â Â Â Â dev_err(&pdev->dev, "no memory resource specified\n");
>> > + Â Â Â Â Â Â Â ret = -ENOENT;
>> > + Â Â Â Â Â Â Â goto err_get;
>> > + Â Â Â }
>> > +
>> > + Â Â Â if (!request_mem_region(w90p910_wdt->res->start,
>> > + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â resource_size(w90p910_wdt->res), pdev->name)) {
>> > + Â Â Â Â Â Â Â dev_err(&pdev->dev, "failed to get memory region\n");
>> > + Â Â Â Â Â Â Â ret = -ENOENT;
>> > + Â Â Â Â Â Â Â goto err_req;
>> > + Â Â Â }
>> > +
>> > + Â Â Â w90p910_wdt->wdt_base = ioremap(w90p910_wdt->res->start,
>> > + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â resource_size(w90p910_wdt->res));
>> > + Â Â Â if (w90p910_wdt->wdt_base == NULL) {
>> > + Â Â Â Â Â Â Â dev_err(&pdev->dev, "failed to ioremap() region\n");
>> > + Â Â Â Â Â Â Â ret = -EINVAL;
>> > + Â Â Â Â Â Â Â goto err_req;
>> > + Â Â Â }
>> > +
>> > + Â Â Â w90p910_wdt->wdt_irq = platform_get_irq(pdev, 0);
>> > + Â Â Â ret = request_irq(w90p910_wdt->wdt_irq,
>> > + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â w90p910_wdt_irq, 0, pdev->name, NULL);
>> > + Â Â Â if (ret != 0) {
>> > + Â Â Â Â Â Â Â dev_err(&pdev->dev, "failed to install irq (%d)\n", ret);
>> > + Â Â Â Â Â Â Â goto err_map;
>> > + Â Â Â }
>> > +
>> > + Â Â Â w90p910_wdt->wdt_clock = clk_get(&pdev->dev, NULL);
>> > + Â Â Â if (IS_ERR(w90p910_wdt->wdt_clock)) {
>> > + Â Â Â Â Â Â Â dev_err(&pdev->dev, "failed to find watchdog clock source\n");
>> > + Â Â Â Â Â Â Â ret = PTR_ERR(w90p910_wdt->wdt_clock);
>> > + Â Â Â Â Â Â Â goto err_irq;
>> > + Â Â Â }
>> > +
>> > + Â Â Â clk_enable(w90p910_wdt->wdt_clock);
>> > +
>> > + Â Â Â if (misc_register(&w90p910wdt_miscdev)) {
>> > + Â Â Â Â Â Â Â dev_err(&pdev->dev, "err register miscdev on minor=%d (%d)\n",
>> > + Â Â Â Â Â Â Â Â Â Â Â WATCHDOG_MINOR, ret);
>> > + Â Â Â Â Â Â Â goto err_clk;
>> > + Â Â Â }
>> > +
>> > + Â Â Â return 0;
>> > +
>> > +err_clk:
>> > + Â Â Â clk_disable(w90p910_wdt->wdt_clock);
>> > + Â Â Â clk_put(w90p910_wdt->wdt_clock);
>> > +err_irq:
>> > + Â Â Â free_irq(w90p910_wdt->wdt_irq, NULL);
>> > +err_map:
>> > + Â Â Â iounmap(w90p910_wdt->wdt_base);
>> > +err_req:
>> > + Â Â Â release_mem_region(w90p910_wdt->res->start,
>> > + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â resource_size(w90p910_wdt->res));
>> > +err_get:
>> > + Â Â Â kfree(w90p910_wdt);
>> > + Â Â Â return ret;
>> > +}
>> > +
>> > +static int __devexit w90p910wdt_remove(struct platform_device *pdev)
>> > +{
>> > + Â Â Â free_irq(w90p910_wdt->wdt_irq, NULL);
>> > +
>> > + Â Â Â clk_disable(w90p910_wdt->wdt_clock);
>> > + Â Â Â clk_put(w90p910_wdt->wdt_clock);
>> > +
>> > + Â Â Â iounmap(w90p910_wdt->wdt_base);
>> > +
>> > + Â Â Â release_mem_region(w90p910_wdt->res->start,
>> > + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â resource_size(w90p910_wdt->res));
>> > +
>> > + Â Â Â kfree(w90p910_wdt);
>> > +
>> > + Â Â Â misc_deregister(&w90p910wdt_miscdev);
>> > + Â Â Â return 0;
>> > +}
>> > +
>> > +static void w90p910wdt_shutdown(struct platform_device *pdev)
>> > +{
>> > + Â Â Â w90p910_wdt_stop();
>> > +}
>> > +
>> > +static struct platform_driver w90p910wdt_driver = {
>> > +    .probe     Â= w90p910wdt_probe,
>> > +    .remove     = __devexit_p(w90p910wdt_remove),
>> > +    .shutdown    = w90p910wdt_shutdown,
>> > +    .driver     = {
>> > +        .name  = "w90p910-wdt",
>> > + Â Â Â Â Â Â Â .owner Â= THIS_MODULE,
>> > + Â Â Â },
>> > +};
>> > +
>> > +static int __init w90p910_wdt_init(void)
>> > +{
>> > + Â Â Â return platform_driver_register(&w90p910wdt_driver);
>> > +}
>> > +
>> > +static void __exit w90p910_wdt_exit(void)
>> > +{
>> > + Â Â Â platform_driver_unregister(&w90p910wdt_driver);
>> > +}
>> > +
>> > +module_init(w90p910_wdt_init);
>> > +module_exit(w90p910_wdt_exit);
>> > +
>> > +MODULE_AUTHOR("Wan ZongShun <mcuos.com@xxxxxxxxx>");
>> > +MODULE_DESCRIPTION("Watchdog driver for W90P910");
>> > +MODULE_LICENSE("GPL");
>> > +MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
>> > +MODULE_ALIAS("platform:w90p910-wdt");
>> > --
>> > 1.5.6.3
>> >
>>
>>
>> --
>> Wan z.s
>



--
Wan z.s
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/