]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/watchdog/bcm2835_wdt.c
UBUNTU: Ubuntu-raspi2-4.10.0-1011.14
[mirror_ubuntu-zesty-kernel.git] / drivers / watchdog / bcm2835_wdt.c
1 /*
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
16 #include <linux/delay.h>
17 #include <linux/reboot.h>
18 #include <linux/types.h>
19 #include <linux/module.h>
20 #include <linux/io.h>
21 #include <linux/watchdog.h>
22 #include <linux/platform_device.h>
23 #include <linux/of_address.h>
24 #include <linux/of_platform.h>
25
26 #define PM_RSTC 0x1c
27 #define PM_RSTS 0x20
28 #define PM_WDOG 0x24
29
30 #define PM_PASSWORD 0x5a000000
31
32 #define PM_WDOG_TIME_SET 0x000fffff
33 #define PM_RSTC_WRCFG_CLR 0xffffffcf
34 #define PM_RSTS_HADWRH_SET 0x00000040
35 #define PM_RSTC_WRCFG_SET 0x00000030
36 #define PM_RSTC_WRCFG_FULL_RESET 0x00000020
37 #define PM_RSTC_RESET 0x00000102
38 #define PM_RSTS_PARTITION_CLR 0xfffffaaa
39
40 #define SECS_TO_WDOG_TICKS(x) ((x) << 16)
41 #define WDOG_TICKS_TO_SECS(x) ((x) >> 16)
42
43 struct bcm2835_wdt {
44 void __iomem *base;
45 spinlock_t lock;
46 struct notifier_block restart_handler;
47 };
48
49 static unsigned int heartbeat;
50 static bool nowayout = WATCHDOG_NOWAYOUT;
51
52 static bool bcm2835_wdt_is_running(struct bcm2835_wdt *wdt)
53 {
54 uint32_t cur;
55
56 cur = readl(wdt->base + PM_RSTC);
57
58 return !!(cur & PM_RSTC_WRCFG_FULL_RESET);
59 }
60
61 static int bcm2835_wdt_start(struct watchdog_device *wdog)
62 {
63 struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
64 uint32_t cur;
65 unsigned long flags;
66
67 spin_lock_irqsave(&wdt->lock, flags);
68
69 writel_relaxed(PM_PASSWORD | (SECS_TO_WDOG_TICKS(wdog->timeout) &
70 PM_WDOG_TIME_SET), wdt->base + PM_WDOG);
71 cur = readl_relaxed(wdt->base + PM_RSTC);
72 writel_relaxed(PM_PASSWORD | (cur & PM_RSTC_WRCFG_CLR) |
73 PM_RSTC_WRCFG_FULL_RESET, wdt->base + PM_RSTC);
74
75 spin_unlock_irqrestore(&wdt->lock, flags);
76
77 return 0;
78 }
79
80 static int bcm2835_wdt_stop(struct watchdog_device *wdog)
81 {
82 struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
83
84 writel_relaxed(PM_PASSWORD | PM_RSTC_RESET, wdt->base + PM_RSTC);
85 return 0;
86 }
87
88 static unsigned int bcm2835_wdt_get_timeleft(struct watchdog_device *wdog)
89 {
90 struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
91
92 uint32_t ret = readl_relaxed(wdt->base + PM_WDOG);
93 return WDOG_TICKS_TO_SECS(ret & PM_WDOG_TIME_SET);
94 }
95
96 static const struct watchdog_ops bcm2835_wdt_ops = {
97 .owner = THIS_MODULE,
98 .start = bcm2835_wdt_start,
99 .stop = bcm2835_wdt_stop,
100 .get_timeleft = bcm2835_wdt_get_timeleft,
101 };
102
103 static const struct watchdog_info bcm2835_wdt_info = {
104 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE |
105 WDIOF_KEEPALIVEPING,
106 .identity = "Broadcom BCM2835 Watchdog timer",
107 };
108
109 static struct watchdog_device bcm2835_wdt_wdd = {
110 .info = &bcm2835_wdt_info,
111 .ops = &bcm2835_wdt_ops,
112 .min_timeout = 1,
113 .max_timeout = WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
114 .timeout = WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
115 };
116
117 /*
118 * The Raspberry Pi firmware uses the RSTS register to know which partiton
119 * to boot from. The partiton value is spread into bits 0, 2, 4, 6, 8, 10.
120 * Partiton 63 is a special partition used by the firmware to indicate halt.
121 */
122
123 static void bcm2835_restart(struct bcm2835_wdt *wdt, u8 partition)
124 {
125 u32 val, rsts;
126
127 rsts = (partition & BIT(0)) | ((partition & BIT(1)) << 1) |
128 ((partition & BIT(2)) << 2) | ((partition & BIT(3)) << 3) |
129 ((partition & BIT(4)) << 4) | ((partition & BIT(5)) << 5);
130
131 val = readl_relaxed(wdt->base + PM_RSTS);
132 val &= PM_RSTS_PARTITION_CLR;
133 val |= PM_PASSWORD | rsts;
134 writel_relaxed(val, wdt->base + PM_RSTS);
135
136 /* use a timeout of 10 ticks (~150us) */
137 writel_relaxed(10 | PM_PASSWORD, wdt->base + PM_WDOG);
138
139 val = readl_relaxed(wdt->base + PM_RSTC);
140 val &= PM_RSTC_WRCFG_CLR;
141 val |= PM_PASSWORD | PM_RSTC_WRCFG_FULL_RESET;
142 writel_relaxed(val, wdt->base + PM_RSTC);
143
144 /* No sleeping, possibly atomic. */
145 mdelay(1);
146 }
147
148 static int bcm2835_restart_notifier_call(struct notifier_block *this,
149 unsigned long mode, void *cmd)
150 {
151 struct bcm2835_wdt *wdt = container_of(this, struct bcm2835_wdt,
152 restart_handler);
153 unsigned long long val;
154 u8 partition = 0;
155
156 if (cmd && !kstrtoull(cmd, 0, &val) && val <= 63)
157 partition = val;
158
159 bcm2835_restart(wdt, partition);
160
161 return 0;
162 }
163
164 /*
165 * We can't really power off, but if we do the normal reset scheme, and
166 * indicate to bootcode.bin not to reboot, then most of the chip will be
167 * powered off.
168 */
169 static void bcm2835_power_off(void)
170 {
171 struct device_node *np =
172 of_find_compatible_node(NULL, NULL, "brcm,bcm2835-pm-wdt");
173 struct platform_device *pdev = of_find_device_by_node(np);
174 struct bcm2835_wdt *wdt = platform_get_drvdata(pdev);
175
176 /* Partition 63 tells the firmware that this is a halt */
177 bcm2835_restart(wdt, 63);
178 }
179
180 static int bcm2835_wdt_probe(struct platform_device *pdev)
181 {
182 struct device *dev = &pdev->dev;
183 struct device_node *np = dev->of_node;
184 struct bcm2835_wdt *wdt;
185 int err;
186
187 wdt = devm_kzalloc(dev, sizeof(struct bcm2835_wdt), GFP_KERNEL);
188 if (!wdt)
189 return -ENOMEM;
190 platform_set_drvdata(pdev, wdt);
191
192 spin_lock_init(&wdt->lock);
193
194 wdt->base = of_iomap(np, 0);
195 if (!wdt->base) {
196 dev_err(dev, "Failed to remap watchdog regs");
197 return -ENODEV;
198 }
199
200 watchdog_set_drvdata(&bcm2835_wdt_wdd, wdt);
201 watchdog_init_timeout(&bcm2835_wdt_wdd, heartbeat, dev);
202 watchdog_set_nowayout(&bcm2835_wdt_wdd, nowayout);
203 bcm2835_wdt_wdd.parent = &pdev->dev;
204 if (bcm2835_wdt_is_running(wdt)) {
205 /*
206 * The currently active timeout value (set by the
207 * bootloader) may be different from the module
208 * heartbeat parameter or the value in device
209 * tree. But we just need to set WDOG_HW_RUNNING,
210 * because then the framework will "immediately" ping
211 * the device, updating the timeout.
212 */
213 set_bit(WDOG_HW_RUNNING, &bcm2835_wdt_wdd.status);
214 }
215 err = watchdog_register_device(&bcm2835_wdt_wdd);
216 if (err) {
217 dev_err(dev, "Failed to register watchdog device");
218 iounmap(wdt->base);
219 return err;
220 }
221
222 wdt->restart_handler.notifier_call = bcm2835_restart_notifier_call;
223 wdt->restart_handler.priority = 128;
224 register_restart_handler(&wdt->restart_handler);
225 if (pm_power_off == NULL)
226 pm_power_off = bcm2835_power_off;
227
228 dev_info(dev, "Broadcom BCM2835 watchdog timer");
229 return 0;
230 }
231
232 static int bcm2835_wdt_remove(struct platform_device *pdev)
233 {
234 struct bcm2835_wdt *wdt = platform_get_drvdata(pdev);
235
236 unregister_restart_handler(&wdt->restart_handler);
237 if (pm_power_off == bcm2835_power_off)
238 pm_power_off = NULL;
239 watchdog_unregister_device(&bcm2835_wdt_wdd);
240 iounmap(wdt->base);
241
242 return 0;
243 }
244
245 static void bcm2835_wdt_shutdown(struct platform_device *pdev)
246 {
247 bcm2835_wdt_stop(&bcm2835_wdt_wdd);
248 }
249
250 static const struct of_device_id bcm2835_wdt_of_match[] = {
251 { .compatible = "brcm,bcm2835-pm-wdt", },
252 {},
253 };
254 MODULE_DEVICE_TABLE(of, bcm2835_wdt_of_match);
255
256 static struct platform_driver bcm2835_wdt_driver = {
257 .probe = bcm2835_wdt_probe,
258 .remove = bcm2835_wdt_remove,
259 .shutdown = bcm2835_wdt_shutdown,
260 .driver = {
261 .name = "bcm2835-wdt",
262 .of_match_table = bcm2835_wdt_of_match,
263 },
264 };
265 module_platform_driver(bcm2835_wdt_driver);
266
267 module_param(heartbeat, uint, 0);
268 MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
269
270 module_param(nowayout, bool, 0);
271 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
272 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
273
274 MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
275 MODULE_DESCRIPTION("Driver for Broadcom BCM2835 watchdog timer");
276 MODULE_LICENSE("GPL");