blob: 4e0adb6c88f0de496ec462c9f70edb0bf0e43e76 [file] [log] [blame]
Lubomir Rintel938d0a82013-06-18 19:44:48 +02001/*
2 * Watchdog driver for Broadcom BCM2835
3 *
4 * "bcm2708_wdog" driver written by Luke Diamand that was obtained from
5 * branch "rpi-3.6.y" of git://github.com/raspberrypi/linux.git was used
6 * as a hardware reference for the Broadcom BCM2835 watchdog timer.
7 *
8 * Copyright (C) 2013 Lubomir Rintel <lkundrak@v3.sk>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 */
15
Eric Anholt33a9f5b2015-04-24 12:08:54 -070016#include <linux/delay.h>
Lubomir Rintel938d0a82013-06-18 19:44:48 +020017#include <linux/types.h>
18#include <linux/module.h>
19#include <linux/io.h>
20#include <linux/watchdog.h>
21#include <linux/platform_device.h>
22#include <linux/of_address.h>
Eric Anholt33a9f5b2015-04-24 12:08:54 -070023#include <linux/of_platform.h>
Lubomir Rintel938d0a82013-06-18 19:44:48 +020024
25#define PM_RSTC 0x1c
Eric Anholt33a9f5b2015-04-24 12:08:54 -070026#define PM_RSTS 0x20
Lubomir Rintel938d0a82013-06-18 19:44:48 +020027#define PM_WDOG 0x24
28
29#define PM_PASSWORD 0x5a000000
30
31#define PM_WDOG_TIME_SET 0x000fffff
32#define PM_RSTC_WRCFG_CLR 0xffffffcf
Eric Anholt33a9f5b2015-04-24 12:08:54 -070033#define PM_RSTS_HADWRH_SET 0x00000040
Lubomir Rintel938d0a82013-06-18 19:44:48 +020034#define PM_RSTC_WRCFG_SET 0x00000030
35#define PM_RSTC_WRCFG_FULL_RESET 0x00000020
36#define PM_RSTC_RESET 0x00000102
37
Noralf Trønnes898e6862015-06-17 16:04:04 +020038/*
39 * The Raspberry Pi firmware uses the RSTS register to know which partiton
40 * to boot from. The partiton value is spread into bits 0, 2, 4, 6, 8, 10.
41 * Partiton 63 is a special partition used by the firmware to indicate halt.
42 */
43#define PM_RSTS_RASPBERRYPI_HALT 0x555
44
Lubomir Rintel938d0a82013-06-18 19:44:48 +020045#define SECS_TO_WDOG_TICKS(x) ((x) << 16)
46#define WDOG_TICKS_TO_SECS(x) ((x) >> 16)
47
48struct bcm2835_wdt {
49 void __iomem *base;
50 spinlock_t lock;
51};
52
53static unsigned int heartbeat;
54static bool nowayout = WATCHDOG_NOWAYOUT;
55
Rasmus Villemoes054ae192016-12-12 10:48:43 +010056static bool bcm2835_wdt_is_running(struct bcm2835_wdt *wdt)
57{
58 uint32_t cur;
59
60 cur = readl(wdt->base + PM_RSTC);
61
62 return !!(cur & PM_RSTC_WRCFG_FULL_RESET);
63}
64
Lubomir Rintel938d0a82013-06-18 19:44:48 +020065static int bcm2835_wdt_start(struct watchdog_device *wdog)
66{
67 struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
68 uint32_t cur;
69 unsigned long flags;
70
71 spin_lock_irqsave(&wdt->lock, flags);
72
73 writel_relaxed(PM_PASSWORD | (SECS_TO_WDOG_TICKS(wdog->timeout) &
74 PM_WDOG_TIME_SET), wdt->base + PM_WDOG);
75 cur = readl_relaxed(wdt->base + PM_RSTC);
76 writel_relaxed(PM_PASSWORD | (cur & PM_RSTC_WRCFG_CLR) |
77 PM_RSTC_WRCFG_FULL_RESET, wdt->base + PM_RSTC);
78
79 spin_unlock_irqrestore(&wdt->lock, flags);
80
81 return 0;
82}
83
84static int bcm2835_wdt_stop(struct watchdog_device *wdog)
85{
86 struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
87
88 writel_relaxed(PM_PASSWORD | PM_RSTC_RESET, wdt->base + PM_RSTC);
Lubomir Rintel938d0a82013-06-18 19:44:48 +020089 return 0;
90}
91
Lubomir Rintel938d0a82013-06-18 19:44:48 +020092static unsigned int bcm2835_wdt_get_timeleft(struct watchdog_device *wdog)
93{
94 struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
95
96 uint32_t ret = readl_relaxed(wdt->base + PM_WDOG);
97 return WDOG_TICKS_TO_SECS(ret & PM_WDOG_TIME_SET);
98}
99
Guenter Roeck71e9b2f2017-01-04 12:26:45 -0800100static void __bcm2835_restart(struct bcm2835_wdt *wdt)
101{
102 u32 val;
103
104 /* use a timeout of 10 ticks (~150us) */
105 writel_relaxed(10 | PM_PASSWORD, wdt->base + PM_WDOG);
106 val = readl_relaxed(wdt->base + PM_RSTC);
107 val &= PM_RSTC_WRCFG_CLR;
108 val |= PM_PASSWORD | PM_RSTC_WRCFG_FULL_RESET;
109 writel_relaxed(val, wdt->base + PM_RSTC);
110
111 /* No sleeping, possibly atomic. */
112 mdelay(1);
113}
114
115static int bcm2835_restart(struct watchdog_device *wdog,
116 unsigned long action, void *data)
117{
118 struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
119
120 __bcm2835_restart(wdt);
121
122 return 0;
123}
124
Rasmus Villemoesaea4b472016-07-15 10:15:21 +0200125static const struct watchdog_ops bcm2835_wdt_ops = {
Lubomir Rintel938d0a82013-06-18 19:44:48 +0200126 .owner = THIS_MODULE,
127 .start = bcm2835_wdt_start,
128 .stop = bcm2835_wdt_stop,
Lubomir Rintel938d0a82013-06-18 19:44:48 +0200129 .get_timeleft = bcm2835_wdt_get_timeleft,
Guenter Roeck71e9b2f2017-01-04 12:26:45 -0800130 .restart = bcm2835_restart,
Lubomir Rintel938d0a82013-06-18 19:44:48 +0200131};
132
Rasmus Villemoesaea4b472016-07-15 10:15:21 +0200133static const struct watchdog_info bcm2835_wdt_info = {
Lubomir Rintel938d0a82013-06-18 19:44:48 +0200134 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE |
135 WDIOF_KEEPALIVEPING,
136 .identity = "Broadcom BCM2835 Watchdog timer",
137};
138
139static struct watchdog_device bcm2835_wdt_wdd = {
140 .info = &bcm2835_wdt_info,
141 .ops = &bcm2835_wdt_ops,
142 .min_timeout = 1,
143 .max_timeout = WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
144 .timeout = WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
145};
146
Eric Anholt33a9f5b2015-04-24 12:08:54 -0700147/*
148 * We can't really power off, but if we do the normal reset scheme, and
149 * indicate to bootcode.bin not to reboot, then most of the chip will be
150 * powered off.
151 */
152static void bcm2835_power_off(void)
153{
154 struct device_node *np =
155 of_find_compatible_node(NULL, NULL, "brcm,bcm2835-pm-wdt");
156 struct platform_device *pdev = of_find_device_by_node(np);
157 struct bcm2835_wdt *wdt = platform_get_drvdata(pdev);
158 u32 val;
159
160 /*
161 * We set the watchdog hard reset bit here to distinguish this reset
162 * from the normal (full) reset. bootcode.bin will not reboot after a
163 * hard reset.
164 */
165 val = readl_relaxed(wdt->base + PM_RSTS);
Noralf Trønnes898e6862015-06-17 16:04:04 +0200166 val |= PM_PASSWORD | PM_RSTS_RASPBERRYPI_HALT;
Eric Anholt33a9f5b2015-04-24 12:08:54 -0700167 writel_relaxed(val, wdt->base + PM_RSTS);
168
169 /* Continue with normal reset mechanism */
Guenter Roeck71e9b2f2017-01-04 12:26:45 -0800170 __bcm2835_restart(wdt);
Eric Anholt33a9f5b2015-04-24 12:08:54 -0700171}
172
Lubomir Rintel938d0a82013-06-18 19:44:48 +0200173static int bcm2835_wdt_probe(struct platform_device *pdev)
174{
175 struct device *dev = &pdev->dev;
176 struct device_node *np = dev->of_node;
177 struct bcm2835_wdt *wdt;
178 int err;
179
180 wdt = devm_kzalloc(dev, sizeof(struct bcm2835_wdt), GFP_KERNEL);
Jingoo Han8deea832014-02-11 15:46:43 +0900181 if (!wdt)
Lubomir Rintel938d0a82013-06-18 19:44:48 +0200182 return -ENOMEM;
Lubomir Rintel938d0a82013-06-18 19:44:48 +0200183 platform_set_drvdata(pdev, wdt);
184
185 spin_lock_init(&wdt->lock);
186
187 wdt->base = of_iomap(np, 0);
188 if (!wdt->base) {
189 dev_err(dev, "Failed to remap watchdog regs");
190 return -ENODEV;
191 }
192
193 watchdog_set_drvdata(&bcm2835_wdt_wdd, wdt);
194 watchdog_init_timeout(&bcm2835_wdt_wdd, heartbeat, dev);
195 watchdog_set_nowayout(&bcm2835_wdt_wdd, nowayout);
Pratyush Anand65518812015-08-20 14:05:01 +0530196 bcm2835_wdt_wdd.parent = &pdev->dev;
Rasmus Villemoes054ae192016-12-12 10:48:43 +0100197 if (bcm2835_wdt_is_running(wdt)) {
198 /*
199 * The currently active timeout value (set by the
200 * bootloader) may be different from the module
201 * heartbeat parameter or the value in device
202 * tree. But we just need to set WDOG_HW_RUNNING,
203 * because then the framework will "immediately" ping
204 * the device, updating the timeout.
205 */
206 set_bit(WDOG_HW_RUNNING, &bcm2835_wdt_wdd.status);
207 }
Guenter Roeck71e9b2f2017-01-04 12:26:45 -0800208
209 watchdog_set_restart_priority(&bcm2835_wdt_wdd, 128);
210
Lubomir Rintel938d0a82013-06-18 19:44:48 +0200211 err = watchdog_register_device(&bcm2835_wdt_wdd);
212 if (err) {
213 dev_err(dev, "Failed to register watchdog device");
214 iounmap(wdt->base);
215 return err;
216 }
217
Eric Anholt33a9f5b2015-04-24 12:08:54 -0700218 if (pm_power_off == NULL)
219 pm_power_off = bcm2835_power_off;
220
Lubomir Rintel938d0a82013-06-18 19:44:48 +0200221 dev_info(dev, "Broadcom BCM2835 watchdog timer");
222 return 0;
223}
224
225static int bcm2835_wdt_remove(struct platform_device *pdev)
226{
227 struct bcm2835_wdt *wdt = platform_get_drvdata(pdev);
228
Eric Anholt33a9f5b2015-04-24 12:08:54 -0700229 if (pm_power_off == bcm2835_power_off)
230 pm_power_off = NULL;
Lubomir Rintel938d0a82013-06-18 19:44:48 +0200231 watchdog_unregister_device(&bcm2835_wdt_wdd);
232 iounmap(wdt->base);
233
234 return 0;
235}
236
237static void bcm2835_wdt_shutdown(struct platform_device *pdev)
238{
239 bcm2835_wdt_stop(&bcm2835_wdt_wdd);
240}
241
242static const struct of_device_id bcm2835_wdt_of_match[] = {
243 { .compatible = "brcm,bcm2835-pm-wdt", },
244 {},
245};
246MODULE_DEVICE_TABLE(of, bcm2835_wdt_of_match);
247
248static struct platform_driver bcm2835_wdt_driver = {
249 .probe = bcm2835_wdt_probe,
250 .remove = bcm2835_wdt_remove,
251 .shutdown = bcm2835_wdt_shutdown,
252 .driver = {
253 .name = "bcm2835-wdt",
Lubomir Rintel938d0a82013-06-18 19:44:48 +0200254 .of_match_table = bcm2835_wdt_of_match,
255 },
256};
257module_platform_driver(bcm2835_wdt_driver);
258
259module_param(heartbeat, uint, 0);
260MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
261
262module_param(nowayout, bool, 0);
263MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
264 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
265
266MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
267MODULE_DESCRIPTION("Driver for Broadcom BCM2835 watchdog timer");
268MODULE_LICENSE("GPL");