Merge branch 'mkp-fixes' into fixes
[deliverable/linux.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
39 /*
40 * The Raspberry Pi firmware uses the RSTS register to know which partiton
41 * to boot from. The partiton value is spread into bits 0, 2, 4, 6, 8, 10.
42 * Partiton 63 is a special partition used by the firmware to indicate halt.
43 */
44 #define PM_RSTS_RASPBERRYPI_HALT 0x555
45
46 #define SECS_TO_WDOG_TICKS(x) ((x) << 16)
47 #define WDOG_TICKS_TO_SECS(x) ((x) >> 16)
48
49 struct bcm2835_wdt {
50 void __iomem *base;
51 spinlock_t lock;
52 struct notifier_block restart_handler;
53 };
54
55 static unsigned int heartbeat;
56 static bool nowayout = WATCHDOG_NOWAYOUT;
57
58 static int bcm2835_wdt_start(struct watchdog_device *wdog)
59 {
60 struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
61 uint32_t cur;
62 unsigned long flags;
63
64 spin_lock_irqsave(&wdt->lock, flags);
65
66 writel_relaxed(PM_PASSWORD | (SECS_TO_WDOG_TICKS(wdog->timeout) &
67 PM_WDOG_TIME_SET), wdt->base + PM_WDOG);
68 cur = readl_relaxed(wdt->base + PM_RSTC);
69 writel_relaxed(PM_PASSWORD | (cur & PM_RSTC_WRCFG_CLR) |
70 PM_RSTC_WRCFG_FULL_RESET, wdt->base + PM_RSTC);
71
72 spin_unlock_irqrestore(&wdt->lock, flags);
73
74 return 0;
75 }
76
77 static int bcm2835_wdt_stop(struct watchdog_device *wdog)
78 {
79 struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
80
81 writel_relaxed(PM_PASSWORD | PM_RSTC_RESET, wdt->base + PM_RSTC);
82 dev_info(wdog->dev, "Watchdog timer stopped");
83 return 0;
84 }
85
86 static int bcm2835_wdt_set_timeout(struct watchdog_device *wdog, unsigned int t)
87 {
88 wdog->timeout = t;
89 return 0;
90 }
91
92 static 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
100 static struct watchdog_ops bcm2835_wdt_ops = {
101 .owner = THIS_MODULE,
102 .start = bcm2835_wdt_start,
103 .stop = bcm2835_wdt_stop,
104 .set_timeout = bcm2835_wdt_set_timeout,
105 .get_timeleft = bcm2835_wdt_get_timeleft,
106 };
107
108 static struct watchdog_info bcm2835_wdt_info = {
109 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE |
110 WDIOF_KEEPALIVEPING,
111 .identity = "Broadcom BCM2835 Watchdog timer",
112 };
113
114 static struct watchdog_device bcm2835_wdt_wdd = {
115 .info = &bcm2835_wdt_info,
116 .ops = &bcm2835_wdt_ops,
117 .min_timeout = 1,
118 .max_timeout = WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
119 .timeout = WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
120 };
121
122 static int
123 bcm2835_restart(struct notifier_block *this, unsigned long mode, void *cmd)
124 {
125 struct bcm2835_wdt *wdt = container_of(this, struct bcm2835_wdt,
126 restart_handler);
127 u32 val;
128
129 /* use a timeout of 10 ticks (~150us) */
130 writel_relaxed(10 | PM_PASSWORD, wdt->base + PM_WDOG);
131 val = readl_relaxed(wdt->base + PM_RSTC);
132 val &= PM_RSTC_WRCFG_CLR;
133 val |= PM_PASSWORD | PM_RSTC_WRCFG_FULL_RESET;
134 writel_relaxed(val, wdt->base + PM_RSTC);
135
136 /* No sleeping, possibly atomic. */
137 mdelay(1);
138
139 return 0;
140 }
141
142 /*
143 * We can't really power off, but if we do the normal reset scheme, and
144 * indicate to bootcode.bin not to reboot, then most of the chip will be
145 * powered off.
146 */
147 static void bcm2835_power_off(void)
148 {
149 struct device_node *np =
150 of_find_compatible_node(NULL, NULL, "brcm,bcm2835-pm-wdt");
151 struct platform_device *pdev = of_find_device_by_node(np);
152 struct bcm2835_wdt *wdt = platform_get_drvdata(pdev);
153 u32 val;
154
155 /*
156 * We set the watchdog hard reset bit here to distinguish this reset
157 * from the normal (full) reset. bootcode.bin will not reboot after a
158 * hard reset.
159 */
160 val = readl_relaxed(wdt->base + PM_RSTS);
161 val |= PM_PASSWORD | PM_RSTS_RASPBERRYPI_HALT;
162 writel_relaxed(val, wdt->base + PM_RSTS);
163
164 /* Continue with normal reset mechanism */
165 bcm2835_restart(&wdt->restart_handler, REBOOT_HARD, NULL);
166 }
167
168 static int bcm2835_wdt_probe(struct platform_device *pdev)
169 {
170 struct device *dev = &pdev->dev;
171 struct device_node *np = dev->of_node;
172 struct bcm2835_wdt *wdt;
173 int err;
174
175 wdt = devm_kzalloc(dev, sizeof(struct bcm2835_wdt), GFP_KERNEL);
176 if (!wdt)
177 return -ENOMEM;
178 platform_set_drvdata(pdev, wdt);
179
180 spin_lock_init(&wdt->lock);
181
182 wdt->base = of_iomap(np, 0);
183 if (!wdt->base) {
184 dev_err(dev, "Failed to remap watchdog regs");
185 return -ENODEV;
186 }
187
188 watchdog_set_drvdata(&bcm2835_wdt_wdd, wdt);
189 watchdog_init_timeout(&bcm2835_wdt_wdd, heartbeat, dev);
190 watchdog_set_nowayout(&bcm2835_wdt_wdd, nowayout);
191 bcm2835_wdt_wdd.parent = &pdev->dev;
192 err = watchdog_register_device(&bcm2835_wdt_wdd);
193 if (err) {
194 dev_err(dev, "Failed to register watchdog device");
195 iounmap(wdt->base);
196 return err;
197 }
198
199 wdt->restart_handler.notifier_call = bcm2835_restart;
200 wdt->restart_handler.priority = 128;
201 register_restart_handler(&wdt->restart_handler);
202 if (pm_power_off == NULL)
203 pm_power_off = bcm2835_power_off;
204
205 dev_info(dev, "Broadcom BCM2835 watchdog timer");
206 return 0;
207 }
208
209 static int bcm2835_wdt_remove(struct platform_device *pdev)
210 {
211 struct bcm2835_wdt *wdt = platform_get_drvdata(pdev);
212
213 unregister_restart_handler(&wdt->restart_handler);
214 if (pm_power_off == bcm2835_power_off)
215 pm_power_off = NULL;
216 watchdog_unregister_device(&bcm2835_wdt_wdd);
217 iounmap(wdt->base);
218
219 return 0;
220 }
221
222 static void bcm2835_wdt_shutdown(struct platform_device *pdev)
223 {
224 bcm2835_wdt_stop(&bcm2835_wdt_wdd);
225 }
226
227 static const struct of_device_id bcm2835_wdt_of_match[] = {
228 { .compatible = "brcm,bcm2835-pm-wdt", },
229 {},
230 };
231 MODULE_DEVICE_TABLE(of, bcm2835_wdt_of_match);
232
233 static struct platform_driver bcm2835_wdt_driver = {
234 .probe = bcm2835_wdt_probe,
235 .remove = bcm2835_wdt_remove,
236 .shutdown = bcm2835_wdt_shutdown,
237 .driver = {
238 .name = "bcm2835-wdt",
239 .of_match_table = bcm2835_wdt_of_match,
240 },
241 };
242 module_platform_driver(bcm2835_wdt_driver);
243
244 module_param(heartbeat, uint, 0);
245 MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
246
247 module_param(nowayout, bool, 0);
248 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
249 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
250
251 MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
252 MODULE_DESCRIPTION("Driver for Broadcom BCM2835 watchdog timer");
253 MODULE_LICENSE("GPL");
This page took 0.037111 seconds and 6 git commands to generate.