1 // SPDX-License-Identifier: GPL-2.0-only OR MIT
3 * Apple SoC Watchdog driver
5 * Copyright (C) The Asahi Linux Contributors
8 #include <linux/bits.h>
10 #include <linux/delay.h>
12 #include <linux/kernel.h>
13 #include <linux/limits.h>
14 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/watchdog.h>
20 * Apple Watchdog MMIO registers
22 * This HW block has three separate watchdogs. WD0 resets the machine
23 * to recovery mode and is not very useful for us. WD1 and WD2 trigger a normal
24 * machine reset. WD0 additionally supports a configurable interrupt.
25 * This information can be used to implement pretimeout support at a later time.
27 * APPLE_WDT_WDx_CUR_TIME is a simple counter incremented for each tick of the
28 * reference clock. It can also be overwritten to any value.
29 * Whenever APPLE_WDT_CTRL_RESET_EN is set in APPLE_WDT_WDx_CTRL and
30 * APPLE_WDT_WDx_CUR_TIME >= APPLE_WDT_WDx_BITE_TIME the entire machine is
32 * Whenever APPLE_WDT_CTRL_IRQ_EN is set and APPLE_WDTx_WD1_CUR_TIME >=
33 * APPLE_WDTx_WD1_BARK_TIME an interrupt is triggered and
34 * APPLE_WDT_CTRL_IRQ_STATUS is set. The interrupt can be cleared by writing
35 * 1 to APPLE_WDT_CTRL_IRQ_STATUS.
37 #define APPLE_WDT_WD0_CUR_TIME 0x00
38 #define APPLE_WDT_WD0_BITE_TIME 0x04
39 #define APPLE_WDT_WD0_BARK_TIME 0x08
40 #define APPLE_WDT_WD0_CTRL 0x0c
42 #define APPLE_WDT_WD1_CUR_TIME 0x10
43 #define APPLE_WDT_WD1_BITE_TIME 0x14
44 #define APPLE_WDT_WD1_CTRL 0x1c
46 #define APPLE_WDT_WD2_CUR_TIME 0x20
47 #define APPLE_WDT_WD2_BITE_TIME 0x24
48 #define APPLE_WDT_WD2_CTRL 0x2c
50 #define APPLE_WDT_CTRL_IRQ_EN BIT(0)
51 #define APPLE_WDT_CTRL_IRQ_STATUS BIT(1)
52 #define APPLE_WDT_CTRL_RESET_EN BIT(2)
54 #define APPLE_WDT_TIMEOUT_DEFAULT 30
57 struct watchdog_device wdd
;
59 unsigned long clk_rate
;
62 static struct apple_wdt
*to_apple_wdt(struct watchdog_device
*wdd
)
64 return container_of(wdd
, struct apple_wdt
, wdd
);
67 static int apple_wdt_start(struct watchdog_device
*wdd
)
69 struct apple_wdt
*wdt
= to_apple_wdt(wdd
);
71 writel_relaxed(0, wdt
->regs
+ APPLE_WDT_WD1_CUR_TIME
);
72 writel_relaxed(APPLE_WDT_CTRL_RESET_EN
, wdt
->regs
+ APPLE_WDT_WD1_CTRL
);
77 static int apple_wdt_stop(struct watchdog_device
*wdd
)
79 struct apple_wdt
*wdt
= to_apple_wdt(wdd
);
81 writel_relaxed(0, wdt
->regs
+ APPLE_WDT_WD1_CTRL
);
86 static int apple_wdt_ping(struct watchdog_device
*wdd
)
88 struct apple_wdt
*wdt
= to_apple_wdt(wdd
);
90 writel_relaxed(0, wdt
->regs
+ APPLE_WDT_WD1_CUR_TIME
);
95 static int apple_wdt_set_timeout(struct watchdog_device
*wdd
, unsigned int s
)
97 struct apple_wdt
*wdt
= to_apple_wdt(wdd
);
99 writel_relaxed(0, wdt
->regs
+ APPLE_WDT_WD1_CUR_TIME
);
100 writel_relaxed(wdt
->clk_rate
* s
, wdt
->regs
+ APPLE_WDT_WD1_BITE_TIME
);
107 static unsigned int apple_wdt_get_timeleft(struct watchdog_device
*wdd
)
109 struct apple_wdt
*wdt
= to_apple_wdt(wdd
);
110 u32 cur_time
, reset_time
;
112 cur_time
= readl_relaxed(wdt
->regs
+ APPLE_WDT_WD1_CUR_TIME
);
113 reset_time
= readl_relaxed(wdt
->regs
+ APPLE_WDT_WD1_BITE_TIME
);
115 return (reset_time
- cur_time
) / wdt
->clk_rate
;
118 static int apple_wdt_restart(struct watchdog_device
*wdd
, unsigned long mode
,
121 struct apple_wdt
*wdt
= to_apple_wdt(wdd
);
123 writel_relaxed(APPLE_WDT_CTRL_RESET_EN
, wdt
->regs
+ APPLE_WDT_WD1_CTRL
);
124 writel_relaxed(0, wdt
->regs
+ APPLE_WDT_WD1_BITE_TIME
);
125 writel_relaxed(0, wdt
->regs
+ APPLE_WDT_WD1_CUR_TIME
);
128 * Flush writes and then wait for the SoC to reset. Even though the
129 * reset is queued almost immediately experiments have shown that it
130 * can take up to ~20-25ms until the SoC is actually reset. Just wait
131 * 50ms here to be safe.
133 (void)readl_relaxed(wdt
->regs
+ APPLE_WDT_WD1_CUR_TIME
);
139 static struct watchdog_ops apple_wdt_ops
= {
140 .owner
= THIS_MODULE
,
141 .start
= apple_wdt_start
,
142 .stop
= apple_wdt_stop
,
143 .ping
= apple_wdt_ping
,
144 .set_timeout
= apple_wdt_set_timeout
,
145 .get_timeleft
= apple_wdt_get_timeleft
,
146 .restart
= apple_wdt_restart
,
149 static struct watchdog_info apple_wdt_info
= {
150 .identity
= "Apple SoC Watchdog",
151 .options
= WDIOF_MAGICCLOSE
| WDIOF_KEEPALIVEPING
| WDIOF_SETTIMEOUT
,
154 static int apple_wdt_probe(struct platform_device
*pdev
)
156 struct device
*dev
= &pdev
->dev
;
157 struct apple_wdt
*wdt
;
161 wdt
= devm_kzalloc(dev
, sizeof(*wdt
), GFP_KERNEL
);
165 wdt
->regs
= devm_platform_ioremap_resource(pdev
, 0);
166 if (IS_ERR(wdt
->regs
))
167 return PTR_ERR(wdt
->regs
);
169 clk
= devm_clk_get_enabled(dev
, NULL
);
172 wdt
->clk_rate
= clk_get_rate(clk
);
176 platform_set_drvdata(pdev
, wdt
);
178 wdt
->wdd
.ops
= &apple_wdt_ops
;
179 wdt
->wdd
.info
= &apple_wdt_info
;
180 wdt
->wdd
.max_timeout
= U32_MAX
/ wdt
->clk_rate
;
181 wdt
->wdd
.timeout
= APPLE_WDT_TIMEOUT_DEFAULT
;
183 wdt_ctrl
= readl_relaxed(wdt
->regs
+ APPLE_WDT_WD1_CTRL
);
184 if (wdt_ctrl
& APPLE_WDT_CTRL_RESET_EN
)
185 set_bit(WDOG_HW_RUNNING
, &wdt
->wdd
.status
);
187 watchdog_init_timeout(&wdt
->wdd
, 0, dev
);
188 apple_wdt_set_timeout(&wdt
->wdd
, wdt
->wdd
.timeout
);
189 watchdog_stop_on_unregister(&wdt
->wdd
);
190 watchdog_set_restart_priority(&wdt
->wdd
, 128);
192 return devm_watchdog_register_device(dev
, &wdt
->wdd
);
195 static int apple_wdt_resume(struct device
*dev
)
197 struct apple_wdt
*wdt
= dev_get_drvdata(dev
);
199 if (watchdog_active(&wdt
->wdd
) || watchdog_hw_running(&wdt
->wdd
))
200 apple_wdt_start(&wdt
->wdd
);
205 static int apple_wdt_suspend(struct device
*dev
)
207 struct apple_wdt
*wdt
= dev_get_drvdata(dev
);
209 if (watchdog_active(&wdt
->wdd
) || watchdog_hw_running(&wdt
->wdd
))
210 apple_wdt_stop(&wdt
->wdd
);
215 static DEFINE_SIMPLE_DEV_PM_OPS(apple_wdt_pm_ops
, apple_wdt_suspend
, apple_wdt_resume
);
217 static const struct of_device_id apple_wdt_of_match
[] = {
218 { .compatible
= "apple,wdt" },
221 MODULE_DEVICE_TABLE(of
, apple_wdt_of_match
);
223 static struct platform_driver apple_wdt_driver
= {
225 .name
= "apple-watchdog",
226 .of_match_table
= apple_wdt_of_match
,
227 .pm
= pm_sleep_ptr(&apple_wdt_pm_ops
),
229 .probe
= apple_wdt_probe
,
231 module_platform_driver(apple_wdt_driver
);
233 MODULE_DESCRIPTION("Apple SoC watchdog driver");
234 MODULE_AUTHOR("Sven Peter <sven@svenpeter.dev>");
235 MODULE_LICENSE("Dual MIT/GPL");