blob: 4074244c71834f2b2648395437b95a95a7c9fe9b [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 Reitmayrdf6707b2009-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 Reitmayrdf6707b2009-02-20 19:44:59 +010058
59 spin_unlock(&wdt_lock);
Axel Lin0dd6e482012-03-26 11:14:29 +080060 return 0;
Thomas Reitmayrdf6707b2009-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 */
73 reg = readl(BRIDGE_CAUSE);
74 reg &= ~WDT_INT_REQ;
75 writel(reg, BRIDGE_CAUSE);
76
77 /* Enable watchdog timer */
Jason Coopera855a7c2012-03-15 00:33:26 +000078 reg = readl(wdt_reg + TIMER_CTRL);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020079 reg |= WDT_EN;
Jason Coopera855a7c2012-03-15 00:33:26 +000080 writel(reg, wdt_reg + TIMER_CTRL);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020081
82 /* Enable reset on watchdog */
Thomas Reitmayr6462c612009-06-01 13:38:33 +020083 reg = readl(RSTOUTn_MASK);
84 reg |= WDT_RESET_OUT_EN;
85 writel(reg, RSTOUTn_MASK);
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +000086
87 spin_unlock(&wdt_lock);
Axel Lin0dd6e482012-03-26 11:14:29 +080088 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +020089}
90
Axel Lin0dd6e482012-03-26 11:14:29 +080091static int orion_wdt_stop(struct watchdog_device *wdt_dev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +020092{
93 u32 reg;
94
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +000095 spin_lock(&wdt_lock);
96
Sylver Bruneau22ac9232008-06-26 10:47:45 +020097 /* Disable reset on watchdog */
Thomas Reitmayr6462c612009-06-01 13:38:33 +020098 reg = readl(RSTOUTn_MASK);
99 reg &= ~WDT_RESET_OUT_EN;
100 writel(reg, RSTOUTn_MASK);
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200101
102 /* Disable watchdog timer */
Jason Coopera855a7c2012-03-15 00:33:26 +0000103 reg = readl(wdt_reg + TIMER_CTRL);
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200104 reg &= ~WDT_EN;
Jason Coopera855a7c2012-03-15 00:33:26 +0000105 writel(reg, wdt_reg + TIMER_CTRL);
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000106
107 spin_unlock(&wdt_lock);
Axel Lin0dd6e482012-03-26 11:14:29 +0800108 return 0;
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000109}
110
Axel Lin0dd6e482012-03-26 11:14:29 +0800111static unsigned int orion_wdt_get_timeleft(struct watchdog_device *wdt_dev)
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000112{
Axel Lin0dd6e482012-03-26 11:14:29 +0800113 unsigned int time_left;
114
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000115 spin_lock(&wdt_lock);
Axel Lin0dd6e482012-03-26 11:14:29 +0800116 time_left = readl(wdt_reg + WDT_VAL) / wdt_tclk;
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000117 spin_unlock(&wdt_lock);
Axel Lin0dd6e482012-03-26 11:14:29 +0800118
119 return time_left;
120}
121
122static int orion_wdt_set_timeout(struct watchdog_device *wdt_dev,
123 unsigned int timeout)
124{
125 wdt_dev->timeout = timeout;
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000126 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200127}
128
Axel Lin0dd6e482012-03-26 11:14:29 +0800129static const struct watchdog_info orion_wdt_info = {
130 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
131 .identity = "Orion Watchdog",
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200132};
133
Axel Lin0dd6e482012-03-26 11:14:29 +0800134static const struct watchdog_ops orion_wdt_ops = {
135 .owner = THIS_MODULE,
136 .start = orion_wdt_start,
137 .stop = orion_wdt_stop,
138 .ping = orion_wdt_ping,
139 .set_timeout = orion_wdt_set_timeout,
140 .get_timeleft = orion_wdt_get_timeleft,
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200141};
142
Axel Lin0dd6e482012-03-26 11:14:29 +0800143static struct watchdog_device orion_wdt = {
144 .info = &orion_wdt_info,
145 .ops = &orion_wdt_ops,
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100146 .min_timeout = 1,
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200147};
148
Bill Pemberton2d991a12012-11-19 13:21:41 -0500149static int orion_wdt_probe(struct platform_device *pdev)
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800150{
Jason Coopera855a7c2012-03-15 00:33:26 +0000151 struct resource *res;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800152 int ret;
153
Axel Lin0dd6e482012-03-26 11:14:29 +0800154 clk = devm_clk_get(&pdev->dev, NULL);
Andrew Lunn4f04be62012-03-04 16:57:31 +0100155 if (IS_ERR(clk)) {
Axel Lin0dd6e482012-03-26 11:14:29 +0800156 dev_err(&pdev->dev, "Orion Watchdog missing clock\n");
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800157 return -ENODEV;
158 }
Andrew Lunn4f04be62012-03-04 16:57:31 +0100159 clk_prepare_enable(clk);
160 wdt_tclk = clk_get_rate(clk);
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800161
Jason Coopera855a7c2012-03-15 00:33:26 +0000162 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jason Gunthorpe8c4c4192012-12-07 15:44:46 -0700163 if (!res)
164 return -ENODEV;
Axel Lin0dd6e482012-03-26 11:14:29 +0800165 wdt_reg = devm_ioremap(&pdev->dev, res->start, resource_size(res));
166 if (!wdt_reg)
167 return -ENOMEM;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800168
169 wdt_max_duration = WDT_MAX_CYCLE_COUNT / wdt_tclk;
Axel Lin0dd6e482012-03-26 11:14:29 +0800170
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100171 orion_wdt.timeout = wdt_max_duration;
Axel Lin0dd6e482012-03-26 11:14:29 +0800172 orion_wdt.max_timeout = wdt_max_duration;
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100173 watchdog_init_timeout(&orion_wdt, heartbeat, &pdev->dev);
Axel Lin0dd6e482012-03-26 11:14:29 +0800174
175 watchdog_set_nowayout(&orion_wdt, nowayout);
176 ret = watchdog_register_device(&orion_wdt);
177 if (ret) {
178 clk_disable_unprepare(clk);
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800179 return ret;
Axel Lin0dd6e482012-03-26 11:14:29 +0800180 }
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800181
Joe Perches27c766a2012-02-15 15:06:19 -0800182 pr_info("Initial timeout %d sec%s\n",
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100183 orion_wdt.timeout, nowayout ? ", nowayout" : "");
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800184 return 0;
185}
186
Bill Pemberton4b12b892012-11-19 13:26:24 -0500187static int orion_wdt_remove(struct platform_device *pdev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200188{
Axel Lin0dd6e482012-03-26 11:14:29 +0800189 watchdog_unregister_device(&orion_wdt);
Andrew Lunn4f04be62012-03-04 16:57:31 +0100190 clk_disable_unprepare(clk);
Axel Lin0dd6e482012-03-26 11:14:29 +0800191 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200192}
193
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400194static void orion_wdt_shutdown(struct platform_device *pdev)
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +0100195{
Axel Lin0dd6e482012-03-26 11:14:29 +0800196 orion_wdt_stop(&orion_wdt);
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +0100197}
198
Bill Pemberton1d131362012-11-19 13:24:05 -0500199static const struct of_device_id orion_wdt_of_match_table[] = {
Andrew Lunn1e7bad02012-06-10 15:20:06 +0200200 { .compatible = "marvell,orion-wdt", },
201 {},
202};
203MODULE_DEVICE_TABLE(of, orion_wdt_of_match_table);
204
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400205static struct platform_driver orion_wdt_driver = {
206 .probe = orion_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500207 .remove = orion_wdt_remove,
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400208 .shutdown = orion_wdt_shutdown,
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800209 .driver = {
210 .owner = THIS_MODULE,
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400211 .name = "orion_wdt",
Andrew Lunn1e7bad02012-06-10 15:20:06 +0200212 .of_match_table = of_match_ptr(orion_wdt_of_match_table),
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800213 },
214};
215
Axel Linb8ec6112011-11-29 13:56:27 +0800216module_platform_driver(orion_wdt_driver);
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200217
218MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400219MODULE_DESCRIPTION("Orion Processor Watchdog");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200220
221module_param(heartbeat, int, 0);
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +0100222MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200223
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100224module_param(nowayout, bool, 0);
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +0100225MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
226 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200227
228MODULE_LICENSE("GPL");
Lubomir Rintelf3ea7332013-01-04 15:06:28 +0100229MODULE_ALIAS("platform:orion_wdt");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200230MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);