Merge tag 'v5.15.57' into rpi-5.15.y
[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
40 struct bcm2835_wdt {
41         void __iomem            *base;
42         spinlock_t              lock;
43 };
44
45 static struct bcm2835_wdt *bcm2835_power_off_wdt;
46
47 static unsigned int heartbeat;
48 static bool nowayout = WATCHDOG_NOWAYOUT;
49
50 static bool bcm2835_wdt_is_running(struct bcm2835_wdt *wdt)
51 {
52         uint32_t cur;
53
54         cur = readl(wdt->base + PM_RSTC);
55
56         return !!(cur & PM_RSTC_WRCFG_FULL_RESET);
57 }
58
59 static int bcm2835_wdt_start(struct watchdog_device *wdog)
60 {
61         struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
62         uint32_t cur;
63         unsigned long flags;
64
65         spin_lock_irqsave(&wdt->lock, flags);
66
67         writel_relaxed(PM_PASSWORD | (SECS_TO_WDOG_TICKS(wdog->timeout) &
68                                 PM_WDOG_TIME_SET), wdt->base + PM_WDOG);
69         cur = readl_relaxed(wdt->base + PM_RSTC);
70         writel_relaxed(PM_PASSWORD | (cur & PM_RSTC_WRCFG_CLR) |
71                   PM_RSTC_WRCFG_FULL_RESET, wdt->base + PM_RSTC);
72
73         spin_unlock_irqrestore(&wdt->lock, flags);
74
75         return 0;
76 }
77
78 static int bcm2835_wdt_stop(struct watchdog_device *wdog)
79 {
80         struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
81
82         writel_relaxed(PM_PASSWORD | PM_RSTC_RESET, wdt->base + PM_RSTC);
83         return 0;
84 }
85
86 static unsigned int bcm2835_wdt_get_timeleft(struct watchdog_device *wdog)
87 {
88         struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
89
90         uint32_t ret = readl_relaxed(wdt->base + PM_WDOG);
91         return WDOG_TICKS_TO_SECS(ret & PM_WDOG_TIME_SET);
92 }
93
94 /*
95  * The Raspberry Pi firmware uses the RSTS register to know which partiton
96  * to boot from. The partiton value is spread into bits 0, 2, 4, 6, 8, 10.
97  * Partiton 63 is a special partition used by the firmware to indicate halt.
98  */
99
100 static void __bcm2835_restart(struct bcm2835_wdt *wdt, u8 partition)
101 {
102         u32 val, rsts;
103
104         rsts = (partition & BIT(0)) | ((partition & BIT(1)) << 1) |
105                ((partition & BIT(2)) << 2) | ((partition & BIT(3)) << 3) |
106                ((partition & BIT(4)) << 4) | ((partition & BIT(5)) << 5);
107
108         val = readl_relaxed(wdt->base + PM_RSTS);
109         val &= PM_RSTS_PARTITION_CLR;
110         val |= PM_PASSWORD | rsts;
111         writel_relaxed(val, wdt->base + PM_RSTS);
112
113         /* use a timeout of 10 ticks (~150us) */
114         writel_relaxed(10 | PM_PASSWORD, wdt->base + PM_WDOG);
115         val = readl_relaxed(wdt->base + PM_RSTC);
116         val &= PM_RSTC_WRCFG_CLR;
117         val |= PM_PASSWORD | PM_RSTC_WRCFG_FULL_RESET;
118         writel_relaxed(val, wdt->base + PM_RSTC);
119
120         /* No sleeping, possibly atomic. */
121         mdelay(1);
122 }
123
124 static int bcm2835_restart(struct watchdog_device *wdog,
125                            unsigned long action, void *data)
126 {
127         struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
128
129         unsigned long val;
130         u8 partition = 0;
131
132         // Allow extra arguments separated by spaces after
133         // the partition number.
134         if (data && sscanf(data, "%lu", &val) && val < 63)
135                 partition = val;
136
137         __bcm2835_restart(wdt, partition);
138
139         return 0;
140 }
141
142 static const struct watchdog_ops bcm2835_wdt_ops = {
143         .owner =        THIS_MODULE,
144         .start =        bcm2835_wdt_start,
145         .stop =         bcm2835_wdt_stop,
146         .get_timeleft = bcm2835_wdt_get_timeleft,
147         .restart =      bcm2835_restart,
148 };
149
150 static const struct watchdog_info bcm2835_wdt_info = {
151         .options =      WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE |
152                         WDIOF_KEEPALIVEPING,
153         .identity =     "Broadcom BCM2835 Watchdog timer",
154 };
155
156 static struct watchdog_device bcm2835_wdt_wdd = {
157         .info =         &bcm2835_wdt_info,
158         .ops =          &bcm2835_wdt_ops,
159         .min_timeout =  1,
160         .max_timeout =  WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
161         .timeout =      WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
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 bcm2835_wdt *wdt = bcm2835_power_off_wdt;
172
173         /* Partition 63 tells the firmware that this is a halt */
174         __bcm2835_restart(wdt, 63);
175 }
176
177 static int bcm2835_wdt_probe(struct platform_device *pdev)
178 {
179         struct bcm2835_pm *pm = dev_get_drvdata(pdev->dev.parent);
180         struct device *dev = &pdev->dev;
181         struct bcm2835_wdt *wdt;
182         int err;
183
184         wdt = devm_kzalloc(dev, sizeof(struct bcm2835_wdt), GFP_KERNEL);
185         if (!wdt)
186                 return -ENOMEM;
187
188         spin_lock_init(&wdt->lock);
189
190         wdt->base = pm->base;
191
192         watchdog_set_drvdata(&bcm2835_wdt_wdd, wdt);
193         watchdog_init_timeout(&bcm2835_wdt_wdd, heartbeat, dev);
194         watchdog_set_nowayout(&bcm2835_wdt_wdd, nowayout);
195         bcm2835_wdt_wdd.parent = dev;
196         if (bcm2835_wdt_is_running(wdt)) {
197                 /*
198                  * The currently active timeout value (set by the
199                  * bootloader) may be different from the module
200                  * heartbeat parameter or the value in device
201                  * tree. But we just need to set WDOG_HW_RUNNING,
202                  * because then the framework will "immediately" ping
203                  * the device, updating the timeout.
204                  */
205                 set_bit(WDOG_HW_RUNNING, &bcm2835_wdt_wdd.status);
206         }
207
208         watchdog_set_restart_priority(&bcm2835_wdt_wdd, 128);
209
210         watchdog_stop_on_reboot(&bcm2835_wdt_wdd);
211         err = devm_watchdog_register_device(dev, &bcm2835_wdt_wdd);
212         if (err)
213                 return err;
214
215         if (of_device_is_system_power_controller(pdev->dev.parent->of_node)) {
216                 if (!pm_power_off) {
217                         pm_power_off = bcm2835_power_off;
218                         bcm2835_power_off_wdt = wdt;
219                 } else {
220                         dev_info(dev, "Poweroff handler already present!\n");
221                 }
222         }
223
224         dev_info(dev, "Broadcom BCM2835 watchdog timer");
225         return 0;
226 }
227
228 static int bcm2835_wdt_remove(struct platform_device *pdev)
229 {
230         if (pm_power_off == bcm2835_power_off)
231                 pm_power_off = NULL;
232
233         return 0;
234 }
235
236 static struct platform_driver bcm2835_wdt_driver = {
237         .probe          = bcm2835_wdt_probe,
238         .remove         = bcm2835_wdt_remove,
239         .driver = {
240                 .name =         "bcm2835-wdt",
241         },
242 };
243 module_platform_driver(bcm2835_wdt_driver);
244
245 module_param(heartbeat, uint, 0);
246 MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
247
248 module_param(nowayout, bool, 0);
249 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
250                                 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
251
252 MODULE_ALIAS("platform:bcm2835-wdt");
253 MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
254 MODULE_DESCRIPTION("Driver for Broadcom BCM2835 watchdog timer");
255 MODULE_LICENSE("GPL");