blob: 01d816251302c2491c24a70e8f7c542b61c2f15a [file] [log] [blame]
Viresh KUMAR4a370272010-08-04 11:44:14 +05301/*
2 * drivers/char/watchdog/sp805-wdt.c
3 *
4 * Watchdog driver for ARM SP805 watchdog module
5 *
6 * Copyright (C) 2010 ST Microelectronics
Viresh Kumarda899472015-07-17 16:23:50 -07007 * Viresh Kumar <vireshk@kernel.org>
Viresh KUMAR4a370272010-08-04 11:44:14 +05308 *
9 * This file is licensed under the terms of the GNU General Public
10 * License version 2 or later. This program is licensed "as is" without any
11 * warranty of any kind, whether express or implied.
12 */
13
14#include <linux/device.h>
15#include <linux/resource.h>
16#include <linux/amba/bus.h>
17#include <linux/bitops.h>
18#include <linux/clk.h>
Viresh KUMAR4a370272010-08-04 11:44:14 +053019#include <linux/io.h>
20#include <linux/ioport.h>
21#include <linux/kernel.h>
22#include <linux/math64.h>
Viresh KUMAR4a370272010-08-04 11:44:14 +053023#include <linux/module.h>
24#include <linux/moduleparam.h>
Viresh Kumar16ac4ab2012-02-24 15:12:37 +053025#include <linux/pm.h>
Viresh KUMAR4a370272010-08-04 11:44:14 +053026#include <linux/slab.h>
27#include <linux/spinlock.h>
28#include <linux/types.h>
Viresh KUMAR4a370272010-08-04 11:44:14 +053029#include <linux/watchdog.h>
30
31/* default timeout in seconds */
32#define DEFAULT_TIMEOUT 60
33
34#define MODULE_NAME "sp805-wdt"
35
36/* watchdog register offsets and masks */
37#define WDTLOAD 0x000
38 #define LOAD_MIN 0x00000001
39 #define LOAD_MAX 0xFFFFFFFF
40#define WDTVALUE 0x004
41#define WDTCONTROL 0x008
42 /* control register masks */
43 #define INT_ENABLE (1 << 0)
44 #define RESET_ENABLE (1 << 1)
45#define WDTINTCLR 0x00C
46#define WDTRIS 0x010
47#define WDTMIS 0x014
48 #define INT_MASK (1 << 0)
49#define WDTLOCK 0xC00
50 #define UNLOCK 0x1ACCE551
51 #define LOCK 0x00000001
52
53/**
54 * struct sp805_wdt: sp805 wdt device structure
Viresh Kumar4a516532012-03-12 09:52:16 +053055 * @wdd: instance of struct watchdog_device
Viresh Kumarbfae14b2012-03-12 09:52:13 +053056 * @lock: spin lock protecting dev structure and io access
57 * @base: base address of wdt
58 * @clk: clock structure of wdt
59 * @adev: amba device structure of wdt
60 * @status: current status of wdt
61 * @load_val: load value to be set for current timeout
Viresh KUMAR4a370272010-08-04 11:44:14 +053062 */
63struct sp805_wdt {
Viresh Kumar4a516532012-03-12 09:52:16 +053064 struct watchdog_device wdd;
Viresh KUMAR4a370272010-08-04 11:44:14 +053065 spinlock_t lock;
66 void __iomem *base;
67 struct clk *clk;
68 struct amba_device *adev;
Viresh KUMAR4a370272010-08-04 11:44:14 +053069 unsigned int load_val;
Viresh KUMAR4a370272010-08-04 11:44:14 +053070};
71
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010072static bool nowayout = WATCHDOG_NOWAYOUT;
Viresh Kumar4a516532012-03-12 09:52:16 +053073module_param(nowayout, bool, 0);
74MODULE_PARM_DESC(nowayout,
75 "Set to 1 to keep watchdog running after device release");
Viresh KUMAR4a370272010-08-04 11:44:14 +053076
77/* This routine finds load value that will reset system in required timout */
Viresh Kumar4a516532012-03-12 09:52:16 +053078static int wdt_setload(struct watchdog_device *wdd, unsigned int timeout)
Viresh KUMAR4a370272010-08-04 11:44:14 +053079{
Viresh Kumar4a516532012-03-12 09:52:16 +053080 struct sp805_wdt *wdt = watchdog_get_drvdata(wdd);
Viresh KUMAR4a370272010-08-04 11:44:14 +053081 u64 load, rate;
82
83 rate = clk_get_rate(wdt->clk);
84
85 /*
86 * sp805 runs counter with given value twice, after the end of first
87 * counter it gives an interrupt and then starts counter again. If
Lucas De Marchi25985ed2011-03-30 22:57:33 -030088 * interrupt already occurred then it resets the system. This is why
Viresh KUMAR4a370272010-08-04 11:44:14 +053089 * load is half of what should be required.
90 */
91 load = div_u64(rate, 2) * timeout - 1;
92
93 load = (load > LOAD_MAX) ? LOAD_MAX : load;
94 load = (load < LOAD_MIN) ? LOAD_MIN : load;
95
96 spin_lock(&wdt->lock);
97 wdt->load_val = load;
98 /* roundup timeout to closest positive integer value */
Viresh Kumar938626d2014-05-15 10:01:59 +053099 wdd->timeout = div_u64((load + 1) * 2 + (rate / 2), rate);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530100 spin_unlock(&wdt->lock);
Viresh Kumar4a516532012-03-12 09:52:16 +0530101
102 return 0;
Viresh KUMAR4a370272010-08-04 11:44:14 +0530103}
104
105/* returns number of seconds left for reset to occur */
Viresh Kumar4a516532012-03-12 09:52:16 +0530106static unsigned int wdt_timeleft(struct watchdog_device *wdd)
Viresh KUMAR4a370272010-08-04 11:44:14 +0530107{
Viresh Kumar4a516532012-03-12 09:52:16 +0530108 struct sp805_wdt *wdt = watchdog_get_drvdata(wdd);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530109 u64 load, rate;
110
111 rate = clk_get_rate(wdt->clk);
112
113 spin_lock(&wdt->lock);
Viresh Kumard2e89192012-03-12 09:52:14 +0530114 load = readl_relaxed(wdt->base + WDTVALUE);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530115
116 /*If the interrupt is inactive then time left is WDTValue + WDTLoad. */
Viresh Kumard2e89192012-03-12 09:52:14 +0530117 if (!(readl_relaxed(wdt->base + WDTRIS) & INT_MASK))
Viresh KUMAR4a370272010-08-04 11:44:14 +0530118 load += wdt->load_val + 1;
119 spin_unlock(&wdt->lock);
120
121 return div_u64(load, rate);
122}
123
Viresh Kumar4a516532012-03-12 09:52:16 +0530124static int wdt_config(struct watchdog_device *wdd, bool ping)
Viresh KUMAR4a370272010-08-04 11:44:14 +0530125{
Viresh Kumar4a516532012-03-12 09:52:16 +0530126 struct sp805_wdt *wdt = watchdog_get_drvdata(wdd);
127 int ret;
128
129 if (!ping) {
Viresh Kumard9df0ef2012-03-12 09:52:17 +0530130
Julia Lawall63fbbc12012-08-26 18:01:02 +0200131 ret = clk_prepare_enable(wdt->clk);
Viresh Kumar4a516532012-03-12 09:52:16 +0530132 if (ret) {
133 dev_err(&wdt->adev->dev, "clock enable fail");
134 return ret;
135 }
136 }
137
Viresh KUMAR4a370272010-08-04 11:44:14 +0530138 spin_lock(&wdt->lock);
139
Viresh Kumard2e89192012-03-12 09:52:14 +0530140 writel_relaxed(UNLOCK, wdt->base + WDTLOCK);
141 writel_relaxed(wdt->load_val, wdt->base + WDTLOAD);
Viresh Kumar4a516532012-03-12 09:52:16 +0530142
143 if (!ping) {
144 writel_relaxed(INT_MASK, wdt->base + WDTINTCLR);
145 writel_relaxed(INT_ENABLE | RESET_ENABLE, wdt->base +
146 WDTCONTROL);
147 }
148
Viresh Kumard2e89192012-03-12 09:52:14 +0530149 writel_relaxed(LOCK, wdt->base + WDTLOCK);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530150
Nick Bowler081d83a2011-07-15 11:04:02 -0400151 /* Flush posted writes. */
Viresh Kumard2e89192012-03-12 09:52:14 +0530152 readl_relaxed(wdt->base + WDTLOCK);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530153 spin_unlock(&wdt->lock);
Viresh Kumar4a516532012-03-12 09:52:16 +0530154
155 return 0;
156}
157
158static int wdt_ping(struct watchdog_device *wdd)
159{
160 return wdt_config(wdd, true);
161}
162
163/* enables watchdog timers reset */
164static int wdt_enable(struct watchdog_device *wdd)
165{
166 return wdt_config(wdd, false);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530167}
168
169/* disables watchdog timers reset */
Viresh Kumar4a516532012-03-12 09:52:16 +0530170static int wdt_disable(struct watchdog_device *wdd)
Viresh KUMAR4a370272010-08-04 11:44:14 +0530171{
Viresh Kumar4a516532012-03-12 09:52:16 +0530172 struct sp805_wdt *wdt = watchdog_get_drvdata(wdd);
173
Viresh KUMAR4a370272010-08-04 11:44:14 +0530174 spin_lock(&wdt->lock);
175
Viresh Kumard2e89192012-03-12 09:52:14 +0530176 writel_relaxed(UNLOCK, wdt->base + WDTLOCK);
177 writel_relaxed(0, wdt->base + WDTCONTROL);
178 writel_relaxed(LOCK, wdt->base + WDTLOCK);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530179
Nick Bowler081d83a2011-07-15 11:04:02 -0400180 /* Flush posted writes. */
Viresh Kumard2e89192012-03-12 09:52:14 +0530181 readl_relaxed(wdt->base + WDTLOCK);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530182 spin_unlock(&wdt->lock);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530183
Julia Lawall63fbbc12012-08-26 18:01:02 +0200184 clk_disable_unprepare(wdt->clk);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530185
186 return 0;
187}
188
Viresh Kumar4a516532012-03-12 09:52:16 +0530189static const struct watchdog_info wdt_info = {
190 .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
191 .identity = MODULE_NAME,
Viresh KUMAR4a370272010-08-04 11:44:14 +0530192};
193
Viresh Kumar4a516532012-03-12 09:52:16 +0530194static const struct watchdog_ops wdt_ops = {
195 .owner = THIS_MODULE,
196 .start = wdt_enable,
197 .stop = wdt_disable,
198 .ping = wdt_ping,
199 .set_timeout = wdt_setload,
200 .get_timeleft = wdt_timeleft,
Viresh KUMAR4a370272010-08-04 11:44:14 +0530201};
202
Bill Pemberton2d991a12012-11-19 13:21:41 -0500203static int
Russell Kingaa25afa2011-02-19 15:55:00 +0000204sp805_wdt_probe(struct amba_device *adev, const struct amba_id *id)
Viresh KUMAR4a370272010-08-04 11:44:14 +0530205{
Viresh Kumar4a516532012-03-12 09:52:16 +0530206 struct sp805_wdt *wdt;
Viresh KUMAR4a370272010-08-04 11:44:14 +0530207 int ret = 0;
208
Viresh Kumarfb35a5a2012-03-12 09:52:15 +0530209 wdt = devm_kzalloc(&adev->dev, sizeof(*wdt), GFP_KERNEL);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530210 if (!wdt) {
Viresh KUMAR4a370272010-08-04 11:44:14 +0530211 ret = -ENOMEM;
Viresh Kumarfb35a5a2012-03-12 09:52:15 +0530212 goto err;
213 }
214
Jingoo Han9d11e4f2014-02-11 21:45:56 +0900215 wdt->base = devm_ioremap_resource(&adev->dev, &adev->res);
216 if (IS_ERR(wdt->base))
217 return PTR_ERR(wdt->base);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530218
Jingoo Han07bf9712013-04-29 18:16:49 +0900219 wdt->clk = devm_clk_get(&adev->dev, NULL);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530220 if (IS_ERR(wdt->clk)) {
221 dev_warn(&adev->dev, "Clock not found\n");
222 ret = PTR_ERR(wdt->clk);
Viresh Kumarfb35a5a2012-03-12 09:52:15 +0530223 goto err;
Viresh KUMAR4a370272010-08-04 11:44:14 +0530224 }
225
226 wdt->adev = adev;
Viresh Kumar4a516532012-03-12 09:52:16 +0530227 wdt->wdd.info = &wdt_info;
228 wdt->wdd.ops = &wdt_ops;
Pratyush Anand65518812015-08-20 14:05:01 +0530229 wdt->wdd.parent = &adev->dev;
Viresh KUMAR4a370272010-08-04 11:44:14 +0530230
Viresh Kumar4a516532012-03-12 09:52:16 +0530231 spin_lock_init(&wdt->lock);
232 watchdog_set_nowayout(&wdt->wdd, nowayout);
233 watchdog_set_drvdata(&wdt->wdd, wdt);
234 wdt_setload(&wdt->wdd, DEFAULT_TIMEOUT);
235
236 ret = watchdog_register_device(&wdt->wdd);
237 if (ret) {
238 dev_err(&adev->dev, "watchdog_register_device() failed: %d\n",
239 ret);
Jingoo Han07bf9712013-04-29 18:16:49 +0900240 goto err;
Viresh KUMAR4a370272010-08-04 11:44:14 +0530241 }
Viresh Kumar4a516532012-03-12 09:52:16 +0530242 amba_set_drvdata(adev, wdt);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530243
244 dev_info(&adev->dev, "registration successful\n");
245 return 0;
246
Viresh KUMAR4a370272010-08-04 11:44:14 +0530247err:
248 dev_err(&adev->dev, "Probe Failed!!!\n");
249 return ret;
250}
251
Bill Pemberton4b12b892012-11-19 13:26:24 -0500252static int sp805_wdt_remove(struct amba_device *adev)
Viresh KUMAR4a370272010-08-04 11:44:14 +0530253{
Viresh Kumar4a516532012-03-12 09:52:16 +0530254 struct sp805_wdt *wdt = amba_get_drvdata(adev);
255
256 watchdog_unregister_device(&wdt->wdd);
Viresh Kumar4a516532012-03-12 09:52:16 +0530257 watchdog_set_drvdata(&wdt->wdd, NULL);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530258
259 return 0;
260}
261
Russell King60d6dd52012-11-08 10:51:03 +0000262static int __maybe_unused sp805_wdt_suspend(struct device *dev)
Viresh Kumar16ac4ab2012-02-24 15:12:37 +0530263{
Viresh Kumar4a516532012-03-12 09:52:16 +0530264 struct sp805_wdt *wdt = dev_get_drvdata(dev);
265
266 if (watchdog_active(&wdt->wdd))
267 return wdt_disable(&wdt->wdd);
Viresh Kumar16ac4ab2012-02-24 15:12:37 +0530268
269 return 0;
270}
271
Russell King60d6dd52012-11-08 10:51:03 +0000272static int __maybe_unused sp805_wdt_resume(struct device *dev)
Viresh Kumar16ac4ab2012-02-24 15:12:37 +0530273{
Viresh Kumar4a516532012-03-12 09:52:16 +0530274 struct sp805_wdt *wdt = dev_get_drvdata(dev);
Viresh Kumar16ac4ab2012-02-24 15:12:37 +0530275
Viresh Kumar4a516532012-03-12 09:52:16 +0530276 if (watchdog_active(&wdt->wdd))
277 return wdt_enable(&wdt->wdd);
Viresh Kumar16ac4ab2012-02-24 15:12:37 +0530278
Viresh Kumar4a516532012-03-12 09:52:16 +0530279 return 0;
Viresh Kumar16ac4ab2012-02-24 15:12:37 +0530280}
Viresh Kumar16ac4ab2012-02-24 15:12:37 +0530281
282static SIMPLE_DEV_PM_OPS(sp805_wdt_dev_pm_ops, sp805_wdt_suspend,
283 sp805_wdt_resume);
284
Nick Bowlerbb558da2011-12-19 11:22:36 -0500285static struct amba_id sp805_wdt_ids[] = {
Viresh KUMAR4a370272010-08-04 11:44:14 +0530286 {
287 .id = 0x00141805,
288 .mask = 0x00ffffff,
289 },
290 { 0, 0 },
291};
292
Dave Martin17885b02011-10-05 15:15:23 +0100293MODULE_DEVICE_TABLE(amba, sp805_wdt_ids);
294
Viresh KUMAR4a370272010-08-04 11:44:14 +0530295static struct amba_driver sp805_wdt_driver = {
296 .drv = {
297 .name = MODULE_NAME,
Viresh Kumar16ac4ab2012-02-24 15:12:37 +0530298 .pm = &sp805_wdt_dev_pm_ops,
Viresh KUMAR4a370272010-08-04 11:44:14 +0530299 },
300 .id_table = sp805_wdt_ids,
301 .probe = sp805_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500302 .remove = sp805_wdt_remove,
Viresh KUMAR4a370272010-08-04 11:44:14 +0530303};
304
viresh kumar9e5ed092012-03-15 10:40:38 +0100305module_amba_driver(sp805_wdt_driver);
Viresh KUMAR4a370272010-08-04 11:44:14 +0530306
Viresh Kumarda899472015-07-17 16:23:50 -0700307MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>");
Viresh KUMAR4a370272010-08-04 11:44:14 +0530308MODULE_DESCRIPTION("ARM SP805 Watchdog Driver");
309MODULE_LICENSE("GPL");