blob: 7c18b3bffcf74ab0845fb8796659d297c618e6ed [file] [log] [blame]
Sylver Bruneau22ac9232008-06-26 10:47:45 +02001/*
Nicolas Pitre3b937a72009-06-01 13:56:02 -04002 * drivers/watchdog/orion_wdt.c
Sylver Bruneau22ac9232008-06-26 10:47:45 +02003 *
Nicolas Pitre3b937a72009-06-01 13:56:02 -04004 * Watchdog driver for Orion/Kirkwood processors
Sylver Bruneau22ac9232008-06-26 10:47:45 +02005 *
6 * Author: Sylver Bruneau <sylver.bruneau@googlemail.com>
7 *
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
11 */
12
Joe Perches27c766a2012-02-15 15:06:19 -080013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Sylver Bruneau22ac9232008-06-26 10:47:45 +020015#include <linux/module.h>
16#include <linux/moduleparam.h>
17#include <linux/types.h>
18#include <linux/kernel.h>
Sylver Bruneau22ac9232008-06-26 10:47:45 +020019#include <linux/miscdevice.h>
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080020#include <linux/platform_device.h>
Sylver Bruneau22ac9232008-06-26 10:47:45 +020021#include <linux/watchdog.h>
22#include <linux/init.h>
Sylver Bruneau22ac9232008-06-26 10:47:45 +020023#include <linux/io.h>
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +000024#include <linux/spinlock.h>
Andrew Lunn4f04be62012-03-04 16:57:31 +010025#include <linux/clk.h>
Axel Lin0dd6e482012-03-26 11:14:29 +080026#include <linux/err.h>
Andrew Lunn1e7bad02012-06-10 15:20:06 +020027#include <linux/of.h>
Nicolas Pitrefdd8b072009-04-22 20:08:17 +010028#include <mach/bridge-regs.h>
Sylver Bruneau22ac9232008-06-26 10:47:45 +020029
30/*
31 * Watchdog timer block registers.
32 */
Jason Coopera855a7c2012-03-15 00:33:26 +000033#define TIMER_CTRL 0x0000
Axel Lin0dd6e482012-03-26 11:14:29 +080034#define WDT_EN 0x0010
Jason Coopera855a7c2012-03-15 00:33:26 +000035#define WDT_VAL 0x0024
Sylver Bruneau22ac9232008-06-26 10:47:45 +020036
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080037#define WDT_MAX_CYCLE_COUNT 0xffffffff
Sylver Bruneau22ac9232008-06-26 10:47:45 +020038#define WDT_IN_USE 0
39#define WDT_OK_TO_CLOSE 1
40
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010041static bool nowayout = WATCHDOG_NOWAYOUT;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080042static int heartbeat = -1; /* module parameter (seconds) */
43static unsigned int wdt_max_duration; /* (seconds) */
Andrew Lunn4f04be62012-03-04 16:57:31 +010044static struct clk *clk;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080045static unsigned int wdt_tclk;
Jason Coopera855a7c2012-03-15 00:33:26 +000046static void __iomem *wdt_reg;
Axel Lin1334f322011-11-29 13:54:01 +080047static DEFINE_SPINLOCK(wdt_lock);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020048
Axel Lin0dd6e482012-03-26 11:14:29 +080049static int orion_wdt_ping(struct watchdog_device *wdt_dev)
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +010050{
51 spin_lock(&wdt_lock);
52
53 /* Reload watchdog duration */
Axel Lin0dd6e482012-03-26 11:14:29 +080054 writel(wdt_tclk * wdt_dev->timeout, wdt_reg + WDT_VAL);
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +010055
56 spin_unlock(&wdt_lock);
Axel Lin0dd6e482012-03-26 11:14:29 +080057 return 0;
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +010058}
59
Axel Lin0dd6e482012-03-26 11:14:29 +080060static int orion_wdt_start(struct watchdog_device *wdt_dev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +020061{
62 u32 reg;
63
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +000064 spin_lock(&wdt_lock);
65
Sylver Bruneau22ac9232008-06-26 10:47:45 +020066 /* Set watchdog duration */
Axel Lin0dd6e482012-03-26 11:14:29 +080067 writel(wdt_tclk * wdt_dev->timeout, wdt_reg + WDT_VAL);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020068
69 /* Clear watchdog timer interrupt */
70 reg = readl(BRIDGE_CAUSE);
71 reg &= ~WDT_INT_REQ;
72 writel(reg, BRIDGE_CAUSE);
73
74 /* Enable watchdog timer */
Jason Coopera855a7c2012-03-15 00:33:26 +000075 reg = readl(wdt_reg + TIMER_CTRL);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020076 reg |= WDT_EN;
Jason Coopera855a7c2012-03-15 00:33:26 +000077 writel(reg, wdt_reg + TIMER_CTRL);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020078
79 /* Enable reset on watchdog */
Thomas Reitmayr6462c612009-06-01 13:38:33 +020080 reg = readl(RSTOUTn_MASK);
81 reg |= WDT_RESET_OUT_EN;
82 writel(reg, RSTOUTn_MASK);
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +000083
84 spin_unlock(&wdt_lock);
Axel Lin0dd6e482012-03-26 11:14:29 +080085 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +020086}
87
Axel Lin0dd6e482012-03-26 11:14:29 +080088static int orion_wdt_stop(struct watchdog_device *wdt_dev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +020089{
90 u32 reg;
91
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +000092 spin_lock(&wdt_lock);
93
Sylver Bruneau22ac9232008-06-26 10:47:45 +020094 /* Disable reset on watchdog */
Thomas Reitmayr6462c612009-06-01 13:38:33 +020095 reg = readl(RSTOUTn_MASK);
96 reg &= ~WDT_RESET_OUT_EN;
97 writel(reg, RSTOUTn_MASK);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020098
99 /* Disable watchdog timer */
Jason Coopera855a7c2012-03-15 00:33:26 +0000100 reg = readl(wdt_reg + TIMER_CTRL);
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200101 reg &= ~WDT_EN;
Jason Coopera855a7c2012-03-15 00:33:26 +0000102 writel(reg, wdt_reg + TIMER_CTRL);
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000103
104 spin_unlock(&wdt_lock);
Axel Lin0dd6e482012-03-26 11:14:29 +0800105 return 0;
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000106}
107
Axel Lin0dd6e482012-03-26 11:14:29 +0800108static unsigned int orion_wdt_get_timeleft(struct watchdog_device *wdt_dev)
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000109{
Axel Lin0dd6e482012-03-26 11:14:29 +0800110 unsigned int time_left;
111
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000112 spin_lock(&wdt_lock);
Axel Lin0dd6e482012-03-26 11:14:29 +0800113 time_left = readl(wdt_reg + WDT_VAL) / wdt_tclk;
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000114 spin_unlock(&wdt_lock);
Axel Lin0dd6e482012-03-26 11:14:29 +0800115
116 return time_left;
117}
118
119static int orion_wdt_set_timeout(struct watchdog_device *wdt_dev,
120 unsigned int timeout)
121{
122 wdt_dev->timeout = timeout;
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000123 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200124}
125
Axel Lin0dd6e482012-03-26 11:14:29 +0800126static const struct watchdog_info orion_wdt_info = {
127 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
128 .identity = "Orion Watchdog",
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200129};
130
Axel Lin0dd6e482012-03-26 11:14:29 +0800131static const struct watchdog_ops orion_wdt_ops = {
132 .owner = THIS_MODULE,
133 .start = orion_wdt_start,
134 .stop = orion_wdt_stop,
135 .ping = orion_wdt_ping,
136 .set_timeout = orion_wdt_set_timeout,
137 .get_timeleft = orion_wdt_get_timeleft,
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200138};
139
Axel Lin0dd6e482012-03-26 11:14:29 +0800140static struct watchdog_device orion_wdt = {
141 .info = &orion_wdt_info,
142 .ops = &orion_wdt_ops,
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200143};
144
Bill Pemberton2d991a12012-11-19 13:21:41 -0500145static int orion_wdt_probe(struct platform_device *pdev)
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800146{
Jason Coopera855a7c2012-03-15 00:33:26 +0000147 struct resource *res;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800148 int ret;
149
Axel Lin0dd6e482012-03-26 11:14:29 +0800150 clk = devm_clk_get(&pdev->dev, NULL);
Andrew Lunn4f04be62012-03-04 16:57:31 +0100151 if (IS_ERR(clk)) {
Axel Lin0dd6e482012-03-26 11:14:29 +0800152 dev_err(&pdev->dev, "Orion Watchdog missing clock\n");
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800153 return -ENODEV;
154 }
Andrew Lunn4f04be62012-03-04 16:57:31 +0100155 clk_prepare_enable(clk);
156 wdt_tclk = clk_get_rate(clk);
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800157
Jason Coopera855a7c2012-03-15 00:33:26 +0000158 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jason Gunthorpe8c4c4192012-12-07 15:44:46 -0700159 if (!res)
160 return -ENODEV;
Axel Lin0dd6e482012-03-26 11:14:29 +0800161 wdt_reg = devm_ioremap(&pdev->dev, res->start, resource_size(res));
162 if (!wdt_reg)
163 return -ENOMEM;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800164
165 wdt_max_duration = WDT_MAX_CYCLE_COUNT / wdt_tclk;
Axel Lin0dd6e482012-03-26 11:14:29 +0800166
167 if ((heartbeat < 1) || (heartbeat > wdt_max_duration))
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800168 heartbeat = wdt_max_duration;
169
Axel Lin0dd6e482012-03-26 11:14:29 +0800170 orion_wdt.timeout = heartbeat;
171 orion_wdt.min_timeout = 1;
172 orion_wdt.max_timeout = wdt_max_duration;
173
174 watchdog_set_nowayout(&orion_wdt, nowayout);
175 ret = watchdog_register_device(&orion_wdt);
176 if (ret) {
177 clk_disable_unprepare(clk);
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800178 return ret;
Axel Lin0dd6e482012-03-26 11:14:29 +0800179 }
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800180
Joe Perches27c766a2012-02-15 15:06:19 -0800181 pr_info("Initial timeout %d sec%s\n",
182 heartbeat, nowayout ? ", nowayout" : "");
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800183 return 0;
184}
185
Bill Pemberton4b12b892012-11-19 13:26:24 -0500186static int orion_wdt_remove(struct platform_device *pdev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200187{
Axel Lin0dd6e482012-03-26 11:14:29 +0800188 watchdog_unregister_device(&orion_wdt);
Andrew Lunn4f04be62012-03-04 16:57:31 +0100189 clk_disable_unprepare(clk);
Axel Lin0dd6e482012-03-26 11:14:29 +0800190 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200191}
192
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400193static void orion_wdt_shutdown(struct platform_device *pdev)
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +0100194{
Axel Lin0dd6e482012-03-26 11:14:29 +0800195 orion_wdt_stop(&orion_wdt);
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +0100196}
197
Bill Pemberton1d131362012-11-19 13:24:05 -0500198static const struct of_device_id orion_wdt_of_match_table[] = {
Andrew Lunn1e7bad02012-06-10 15:20:06 +0200199 { .compatible = "marvell,orion-wdt", },
200 {},
201};
202MODULE_DEVICE_TABLE(of, orion_wdt_of_match_table);
203
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400204static struct platform_driver orion_wdt_driver = {
205 .probe = orion_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500206 .remove = orion_wdt_remove,
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400207 .shutdown = orion_wdt_shutdown,
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800208 .driver = {
209 .owner = THIS_MODULE,
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400210 .name = "orion_wdt",
Andrew Lunn1e7bad02012-06-10 15:20:06 +0200211 .of_match_table = of_match_ptr(orion_wdt_of_match_table),
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800212 },
213};
214
Axel Linb8ec6112011-11-29 13:56:27 +0800215module_platform_driver(orion_wdt_driver);
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200216
217MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400218MODULE_DESCRIPTION("Orion Processor Watchdog");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200219
220module_param(heartbeat, int, 0);
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +0100221MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200222
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100223module_param(nowayout, bool, 0);
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +0100224MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
225 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200226
227MODULE_LICENSE("GPL");
228MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);