2 * Moschip MCS814x Watchdog driver
4 * Copyright (C) 2012, Florian Fainelli <florian@openwrt.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/init.h>
24 #include <linux/platform_device.h>
25 #include <linux/miscdevice.h>
26 #include <linux/watchdog.h>
28 #include <linux/err.h>
29 #include <linux/clk.h>
32 #define WDT_COUNT 0x00
34 #define WDT_CTRL_EN 0x1
36 /* watchdog frequency */
37 #define WDT_MAX_VALUE (0xffffffff)
42 struct watchdog_device wdt_dev;
46 static int mcs814x_wdt_start(struct watchdog_device *dev)
48 struct mcs814x_wdt *wdt = watchdog_get_drvdata(dev);
51 spin_lock(&wdt->lock);
52 reg = readl_relaxed(wdt->regs + WDT_CTRL);
54 writel_relaxed(reg, wdt->regs + WDT_CTRL);
55 spin_unlock(&wdt->lock);
60 static int mcs814x_wdt_stop(struct watchdog_device *dev)
62 struct mcs814x_wdt *wdt = watchdog_get_drvdata(dev);
65 spin_lock(&wdt->lock);
66 reg = readl_relaxed(wdt->regs + WDT_CTRL);
68 writel_relaxed(reg, wdt->regs + WDT_CTRL);
69 spin_unlock(&wdt->lock);
74 static int mcs814x_wdt_set_timeout(struct watchdog_device *dev,
75 unsigned int new_timeout)
77 struct mcs814x_wdt *wdt = watchdog_get_drvdata(dev);
79 spin_lock(&wdt->lock);
80 /* watchdog counts upward and rollover (0xfffffff -> 0)
83 writel_relaxed(WDT_MAX_VALUE - (new_timeout * clk_get_rate(wdt->clk)),
84 wdt->regs + WDT_COUNT);
85 spin_unlock(&wdt->lock);
90 static int mcs814x_wdt_ping(struct watchdog_device *dev)
92 /* restart the watchdog */
93 mcs814x_wdt_stop(dev);
94 mcs814x_wdt_set_timeout(dev, dev->timeout);
95 mcs814x_wdt_start(dev);
100 static const struct watchdog_info mcs814x_wdt_ident = {
101 .options = WDIOF_CARDRESET | WDIOF_SETTIMEOUT |
102 WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
103 .identity = "MCS814x Watchdog",
106 static struct watchdog_ops mcs814x_wdt_ops = {
107 .owner = THIS_MODULE,
108 .start = mcs814x_wdt_start,
109 .stop = mcs814x_wdt_stop,
110 .set_timeout = mcs814x_wdt_set_timeout,
111 .ping = mcs814x_wdt_ping,
114 static int mcs814x_wdt_probe(struct platform_device *pdev)
116 struct resource *res;
117 struct mcs814x_wdt *wdt;
121 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
125 clk = clk_get(NULL, "wdt");
126 if (IS_ERR_OR_NULL(clk)) {
127 dev_err(&pdev->dev, "failed to get watchdog clock\n");
131 wdt = kzalloc(sizeof(*wdt), GFP_KERNEL);
137 spin_lock_init(&wdt->lock);
139 wdt->wdt_dev.info = &mcs814x_wdt_ident;
140 wdt->wdt_dev.ops = &mcs814x_wdt_ops;
141 wdt->wdt_dev.min_timeout = 1;
142 /* approximately 10995 secs */
143 wdt->wdt_dev.max_timeout = (WDT_MAX_VALUE / clk_get_rate(clk));
145 platform_set_drvdata(pdev, wdt);
147 /* only ioremap registers, because the register is shared */
148 wdt->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
154 watchdog_set_drvdata(&wdt->wdt_dev, wdt);
156 ret = watchdog_register_device(&wdt->wdt_dev);
158 dev_err(&pdev->dev, "cannot register watchdog: %d\n", ret);
162 dev_info(&pdev->dev, "registered\n");
166 platform_set_drvdata(pdev, NULL);
173 static int mcs814x_wdt_remove(struct platform_device *pdev)
175 struct mcs814x_wdt *wdt = platform_get_drvdata(pdev);
178 watchdog_unregister_device(&wdt->wdt_dev);
179 watchdog_set_drvdata(&wdt->wdt_dev, NULL);
181 platform_set_drvdata(pdev, NULL);
186 static const struct of_device_id mcs814x_wdt_ids[] = {
187 { .compatible = "moschip,mcs814x-wdt", },
191 static struct platform_driver mcs814x_wdt_driver = {
193 .name = "mcs814x-wdt",
194 .owner = THIS_MODULE,
195 .of_match_table = mcs814x_wdt_ids,
197 .probe = mcs814x_wdt_probe,
198 .remove = mcs814x_wdt_remove,
201 module_platform_driver(mcs814x_wdt_driver);
203 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
204 MODULE_DESCRIPTION("Moschip MCS814x Watchdog driver");
205 MODULE_LICENSE("GPL");
206 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
207 MODULE_ALIAS("platform:mcs814x-wdt");