drivers: w1-gpio: add flag to force read-polling while delaying
[platform/kernel/linux-rpi.git] / drivers / watchdog / bcm2835_wdt.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Watchdog driver for Broadcom BCM2835
4  *
5  * "bcm2708_wdog" driver written by Luke Diamand that was obtained from
6  * branch "rpi-3.6.y" of git://github.com/raspberrypi/linux.git was used
7  * as a hardware reference for the Broadcom BCM2835 watchdog timer.
8  *
9  * Copyright (C) 2013 Lubomir Rintel <lkundrak@v3.sk>
10  *
11  */
12
13 #include <linux/delay.h>
14 #include <linux/types.h>
15 #include <linux/mfd/bcm2835-pm.h>
16 #include <linux/module.h>
17 #include <linux/io.h>
18 #include <linux/watchdog.h>
19 #include <linux/platform_device.h>
20 #include <linux/of_address.h>
21 #include <linux/of_platform.h>
22
23 #define PM_RSTC                         0x1c
24 #define PM_RSTS                         0x20
25 #define PM_WDOG                         0x24
26
27 #define PM_PASSWORD                     0x5a000000
28
29 #define PM_WDOG_TIME_SET                0x000fffff
30 #define PM_RSTC_WRCFG_CLR               0xffffffcf
31 #define PM_RSTS_HADWRH_SET              0x00000040
32 #define PM_RSTC_WRCFG_SET               0x00000030
33 #define PM_RSTC_WRCFG_FULL_RESET        0x00000020
34 #define PM_RSTC_RESET                   0x00000102
35 #define PM_RSTS_PARTITION_CLR          0xfffffaaa
36
37 #define SECS_TO_WDOG_TICKS(x) ((x) << 16)
38 #define WDOG_TICKS_TO_SECS(x) ((x) >> 16)
39 #define WDOG_TICKS_TO_MSECS(x) ((x) * 1000 >> 16)
40
41 struct bcm2835_wdt {
42         void __iomem            *base;
43         spinlock_t              lock;
44 };
45
46 static struct bcm2835_wdt *bcm2835_power_off_wdt;
47
48 static unsigned int heartbeat;
49 static bool nowayout = WATCHDOG_NOWAYOUT;
50
51 static bool bcm2835_wdt_is_running(struct bcm2835_wdt *wdt)
52 {
53         uint32_t cur;
54
55         cur = readl(wdt->base + PM_RSTC);
56
57         return !!(cur & PM_RSTC_WRCFG_FULL_RESET);
58 }
59
60 static int bcm2835_wdt_start(struct watchdog_device *wdog)
61 {
62         struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
63         uint32_t cur;
64         unsigned long flags;
65
66         spin_lock_irqsave(&wdt->lock, flags);
67
68         writel_relaxed(PM_PASSWORD | (SECS_TO_WDOG_TICKS(wdog->timeout) &
69                                 PM_WDOG_TIME_SET), wdt->base + PM_WDOG);
70         cur = readl_relaxed(wdt->base + PM_RSTC);
71         writel_relaxed(PM_PASSWORD | (cur & PM_RSTC_WRCFG_CLR) |
72                   PM_RSTC_WRCFG_FULL_RESET, wdt->base + PM_RSTC);
73
74         spin_unlock_irqrestore(&wdt->lock, flags);
75
76         return 0;
77 }
78
79 static int bcm2835_wdt_stop(struct watchdog_device *wdog)
80 {
81         struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
82
83         writel_relaxed(PM_PASSWORD | PM_RSTC_RESET, wdt->base + PM_RSTC);
84         return 0;
85 }
86
87 static unsigned int bcm2835_wdt_get_timeleft(struct watchdog_device *wdog)
88 {
89         struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
90
91         uint32_t ret = readl_relaxed(wdt->base + PM_WDOG);
92         return WDOG_TICKS_TO_SECS(ret & PM_WDOG_TIME_SET);
93 }
94
95 /*
96  * The Raspberry Pi firmware uses the RSTS register to know which partiton
97  * to boot from. The partiton value is spread into bits 0, 2, 4, 6, 8, 10.
98  * Partiton 63 is a special partition used by the firmware to indicate halt.
99  */
100
101 static void __bcm2835_restart(struct bcm2835_wdt *wdt, u8 partition)
102 {
103         u32 val, rsts;
104
105         rsts = (partition & BIT(0)) | ((partition & BIT(1)) << 1) |
106                ((partition & BIT(2)) << 2) | ((partition & BIT(3)) << 3) |
107                ((partition & BIT(4)) << 4) | ((partition & BIT(5)) << 5);
108
109         val = readl_relaxed(wdt->base + PM_RSTS);
110         val &= PM_RSTS_PARTITION_CLR;
111         val |= PM_PASSWORD | rsts;
112         writel_relaxed(val, wdt->base + PM_RSTS);
113
114         /* use a timeout of 10 ticks (~150us) */
115         writel_relaxed(10 | PM_PASSWORD, wdt->base + PM_WDOG);
116         val = readl_relaxed(wdt->base + PM_RSTC);
117         val &= PM_RSTC_WRCFG_CLR;
118         val |= PM_PASSWORD | PM_RSTC_WRCFG_FULL_RESET;
119         writel_relaxed(val, wdt->base + PM_RSTC);
120
121         /* No sleeping, possibly atomic. */
122         mdelay(1);
123 }
124
125 static int bcm2835_restart(struct watchdog_device *wdog,
126                            unsigned long action, void *data)
127 {
128         struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
129
130         unsigned long val;
131         u8 partition = 0;
132
133         // Allow extra arguments separated by spaces after
134         // the partition number.
135         if (data && sscanf(data, "%lu", &val) && val < 63)
136                 partition = val;
137
138         __bcm2835_restart(wdt, partition);
139
140         return 0;
141 }
142
143 static const struct watchdog_ops bcm2835_wdt_ops = {
144         .owner =        THIS_MODULE,
145         .start =        bcm2835_wdt_start,
146         .stop =         bcm2835_wdt_stop,
147         .get_timeleft = bcm2835_wdt_get_timeleft,
148         .restart =      bcm2835_restart,
149 };
150
151 static const struct watchdog_info bcm2835_wdt_info = {
152         .options =      WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE |
153                         WDIOF_KEEPALIVEPING,
154         .identity =     "Broadcom BCM2835 Watchdog timer",
155 };
156
157 static struct watchdog_device bcm2835_wdt_wdd = {
158         .info =         &bcm2835_wdt_info,
159         .ops =          &bcm2835_wdt_ops,
160         .min_timeout =  1,
161         .max_hw_heartbeat_ms =  WDOG_TICKS_TO_MSECS(PM_WDOG_TIME_SET),
162         .timeout =      WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
163 };
164
165 /*
166  * We can't really power off, but if we do the normal reset scheme, and
167  * indicate to bootcode.bin not to reboot, then most of the chip will be
168  * powered off.
169  */
170 static void bcm2835_power_off(void)
171 {
172         struct bcm2835_wdt *wdt = bcm2835_power_off_wdt;
173
174         /* Partition 63 tells the firmware that this is a halt */
175         __bcm2835_restart(wdt, 63);
176 }
177
178 static int bcm2835_wdt_probe(struct platform_device *pdev)
179 {
180         struct bcm2835_pm *pm = dev_get_drvdata(pdev->dev.parent);
181         struct device *dev = &pdev->dev;
182         struct bcm2835_wdt *wdt;
183         int err;
184
185         wdt = devm_kzalloc(dev, sizeof(struct bcm2835_wdt), GFP_KERNEL);
186         if (!wdt)
187                 return -ENOMEM;
188
189         spin_lock_init(&wdt->lock);
190
191         wdt->base = pm->base;
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);
196         bcm2835_wdt_wdd.parent = dev;
197         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         }
208
209         watchdog_set_restart_priority(&bcm2835_wdt_wdd, 128);
210
211         watchdog_stop_on_reboot(&bcm2835_wdt_wdd);
212         err = devm_watchdog_register_device(dev, &bcm2835_wdt_wdd);
213         if (err)
214                 return err;
215
216         if (of_device_is_system_power_controller(pdev->dev.parent->of_node)) {
217                 if (!pm_power_off) {
218                         pm_power_off = bcm2835_power_off;
219                         bcm2835_power_off_wdt = wdt;
220                 } else {
221                         dev_info(dev, "Poweroff handler already present!\n");
222                 }
223         }
224
225         dev_info(dev, "Broadcom BCM2835 watchdog timer");
226         return 0;
227 }
228
229 static void bcm2835_wdt_remove(struct platform_device *pdev)
230 {
231         if (pm_power_off == bcm2835_power_off)
232                 pm_power_off = NULL;
233 }
234
235 static struct platform_driver bcm2835_wdt_driver = {
236         .probe          = bcm2835_wdt_probe,
237         .remove_new     = bcm2835_wdt_remove,
238         .driver = {
239                 .name =         "bcm2835-wdt",
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_ALIAS("platform:bcm2835-wdt");
252 MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
253 MODULE_DESCRIPTION("Driver for Broadcom BCM2835 watchdog timer");
254 MODULE_LICENSE("GPL");