blob: 44edca66d564195a8e20b54f20c8b4cc389eca2c [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
Russell Kingfa142ff2013-06-18 17:19:32 +010041#define WDT_RESET_OUT_EN BIT(1)
42#define WDT_INT_REQ BIT(3)
43
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010044static bool nowayout = WATCHDOG_NOWAYOUT;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080045static int heartbeat = -1; /* module parameter (seconds) */
46static unsigned int wdt_max_duration; /* (seconds) */
Andrew Lunn4f04be62012-03-04 16:57:31 +010047static struct clk *clk;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080048static unsigned int wdt_tclk;
Jason Coopera855a7c2012-03-15 00:33:26 +000049static void __iomem *wdt_reg;
Axel Lin1334f322011-11-29 13:54:01 +080050static DEFINE_SPINLOCK(wdt_lock);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020051
Axel Lin0dd6e482012-03-26 11:14:29 +080052static int orion_wdt_ping(struct watchdog_device *wdt_dev)
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +010053{
54 spin_lock(&wdt_lock);
55
56 /* Reload watchdog duration */
Axel Lin0dd6e482012-03-26 11:14:29 +080057 writel(wdt_tclk * wdt_dev->timeout, wdt_reg + WDT_VAL);
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +010058
59 spin_unlock(&wdt_lock);
Axel Lin0dd6e482012-03-26 11:14:29 +080060 return 0;
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +010061}
62
Axel Lin0dd6e482012-03-26 11:14:29 +080063static int orion_wdt_start(struct watchdog_device *wdt_dev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +020064{
65 u32 reg;
66
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +000067 spin_lock(&wdt_lock);
68
Sylver Bruneau22ac9232008-06-26 10:47:45 +020069 /* Set watchdog duration */
Axel Lin0dd6e482012-03-26 11:14:29 +080070 writel(wdt_tclk * wdt_dev->timeout, wdt_reg + WDT_VAL);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020071
72 /* Clear watchdog timer interrupt */
Russell King6910ceb2013-06-18 17:20:32 +010073 writel(~WDT_INT_REQ, BRIDGE_CAUSE);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020074
75 /* Enable watchdog timer */
Jason Coopera855a7c2012-03-15 00:33:26 +000076 reg = readl(wdt_reg + TIMER_CTRL);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020077 reg |= WDT_EN;
Jason Coopera855a7c2012-03-15 00:33:26 +000078 writel(reg, wdt_reg + TIMER_CTRL);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020079
80 /* Enable reset on watchdog */
Thomas Reitmayr6462c612009-06-01 13:38:33 +020081 reg = readl(RSTOUTn_MASK);
82 reg |= WDT_RESET_OUT_EN;
83 writel(reg, RSTOUTn_MASK);
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +000084
85 spin_unlock(&wdt_lock);
Axel Lin0dd6e482012-03-26 11:14:29 +080086 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +020087}
88
Axel Lin0dd6e482012-03-26 11:14:29 +080089static int orion_wdt_stop(struct watchdog_device *wdt_dev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +020090{
91 u32 reg;
92
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +000093 spin_lock(&wdt_lock);
94
Sylver Bruneau22ac9232008-06-26 10:47:45 +020095 /* Disable reset on watchdog */
Thomas Reitmayr6462c612009-06-01 13:38:33 +020096 reg = readl(RSTOUTn_MASK);
97 reg &= ~WDT_RESET_OUT_EN;
98 writel(reg, RSTOUTn_MASK);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020099
100 /* Disable watchdog timer */
Jason Coopera855a7c2012-03-15 00:33:26 +0000101 reg = readl(wdt_reg + TIMER_CTRL);
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200102 reg &= ~WDT_EN;
Jason Coopera855a7c2012-03-15 00:33:26 +0000103 writel(reg, wdt_reg + TIMER_CTRL);
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000104
105 spin_unlock(&wdt_lock);
Axel Lin0dd6e482012-03-26 11:14:29 +0800106 return 0;
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000107}
108
Axel Lin0dd6e482012-03-26 11:14:29 +0800109static unsigned int orion_wdt_get_timeleft(struct watchdog_device *wdt_dev)
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000110{
Axel Lin0dd6e482012-03-26 11:14:29 +0800111 unsigned int time_left;
112
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000113 spin_lock(&wdt_lock);
Axel Lin0dd6e482012-03-26 11:14:29 +0800114 time_left = readl(wdt_reg + WDT_VAL) / wdt_tclk;
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000115 spin_unlock(&wdt_lock);
Axel Lin0dd6e482012-03-26 11:14:29 +0800116
117 return time_left;
118}
119
120static int orion_wdt_set_timeout(struct watchdog_device *wdt_dev,
121 unsigned int timeout)
122{
123 wdt_dev->timeout = timeout;
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000124 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200125}
126
Axel Lin0dd6e482012-03-26 11:14:29 +0800127static const struct watchdog_info orion_wdt_info = {
128 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
129 .identity = "Orion Watchdog",
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200130};
131
Axel Lin0dd6e482012-03-26 11:14:29 +0800132static const struct watchdog_ops orion_wdt_ops = {
133 .owner = THIS_MODULE,
134 .start = orion_wdt_start,
135 .stop = orion_wdt_stop,
136 .ping = orion_wdt_ping,
137 .set_timeout = orion_wdt_set_timeout,
138 .get_timeleft = orion_wdt_get_timeleft,
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200139};
140
Axel Lin0dd6e482012-03-26 11:14:29 +0800141static struct watchdog_device orion_wdt = {
142 .info = &orion_wdt_info,
143 .ops = &orion_wdt_ops,
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100144 .min_timeout = 1,
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200145};
146
Bill Pemberton2d991a12012-11-19 13:21:41 -0500147static int orion_wdt_probe(struct platform_device *pdev)
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800148{
Jason Coopera855a7c2012-03-15 00:33:26 +0000149 struct resource *res;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800150 int ret;
151
Axel Lin0dd6e482012-03-26 11:14:29 +0800152 clk = devm_clk_get(&pdev->dev, NULL);
Andrew Lunn4f04be62012-03-04 16:57:31 +0100153 if (IS_ERR(clk)) {
Axel Lin0dd6e482012-03-26 11:14:29 +0800154 dev_err(&pdev->dev, "Orion Watchdog missing clock\n");
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800155 return -ENODEV;
156 }
Andrew Lunn4f04be62012-03-04 16:57:31 +0100157 clk_prepare_enable(clk);
158 wdt_tclk = clk_get_rate(clk);
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800159
Jason Coopera855a7c2012-03-15 00:33:26 +0000160 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jason Gunthorpe8c4c4192012-12-07 15:44:46 -0700161 if (!res)
162 return -ENODEV;
Axel Lin0dd6e482012-03-26 11:14:29 +0800163 wdt_reg = devm_ioremap(&pdev->dev, res->start, resource_size(res));
164 if (!wdt_reg)
165 return -ENOMEM;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800166
167 wdt_max_duration = WDT_MAX_CYCLE_COUNT / wdt_tclk;
Axel Lin0dd6e482012-03-26 11:14:29 +0800168
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100169 orion_wdt.timeout = wdt_max_duration;
Axel Lin0dd6e482012-03-26 11:14:29 +0800170 orion_wdt.max_timeout = wdt_max_duration;
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100171 watchdog_init_timeout(&orion_wdt, heartbeat, &pdev->dev);
Axel Lin0dd6e482012-03-26 11:14:29 +0800172
173 watchdog_set_nowayout(&orion_wdt, nowayout);
174 ret = watchdog_register_device(&orion_wdt);
175 if (ret) {
176 clk_disable_unprepare(clk);
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800177 return ret;
Axel Lin0dd6e482012-03-26 11:14:29 +0800178 }
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800179
Joe Perches27c766a2012-02-15 15:06:19 -0800180 pr_info("Initial timeout %d sec%s\n",
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100181 orion_wdt.timeout, nowayout ? ", nowayout" : "");
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800182 return 0;
183}
184
Bill Pemberton4b12b892012-11-19 13:26:24 -0500185static int orion_wdt_remove(struct platform_device *pdev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200186{
Axel Lin0dd6e482012-03-26 11:14:29 +0800187 watchdog_unregister_device(&orion_wdt);
Andrew Lunn4f04be62012-03-04 16:57:31 +0100188 clk_disable_unprepare(clk);
Axel Lin0dd6e482012-03-26 11:14:29 +0800189 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200190}
191
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400192static void orion_wdt_shutdown(struct platform_device *pdev)
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +0100193{
Axel Lin0dd6e482012-03-26 11:14:29 +0800194 orion_wdt_stop(&orion_wdt);
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +0100195}
196
Bill Pemberton1d131362012-11-19 13:24:05 -0500197static const struct of_device_id orion_wdt_of_match_table[] = {
Andrew Lunn1e7bad02012-06-10 15:20:06 +0200198 { .compatible = "marvell,orion-wdt", },
199 {},
200};
201MODULE_DEVICE_TABLE(of, orion_wdt_of_match_table);
202
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400203static struct platform_driver orion_wdt_driver = {
204 .probe = orion_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500205 .remove = orion_wdt_remove,
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400206 .shutdown = orion_wdt_shutdown,
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800207 .driver = {
208 .owner = THIS_MODULE,
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400209 .name = "orion_wdt",
Sachin Kamat85eee812013-09-30 10:12:51 +0530210 .of_match_table = orion_wdt_of_match_table,
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800211 },
212};
213
Axel Linb8ec6112011-11-29 13:56:27 +0800214module_platform_driver(orion_wdt_driver);
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200215
216MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400217MODULE_DESCRIPTION("Orion Processor Watchdog");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200218
219module_param(heartbeat, int, 0);
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +0100220MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200221
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100222module_param(nowayout, bool, 0);
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +0100223MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
224 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200225
226MODULE_LICENSE("GPL");
Lubomir Rintelf3ea7332013-01-04 15:06:28 +0100227MODULE_ALIAS("platform:orion_wdt");