1 // SPDX-License-Identifier: GPL-2.0
3 * Airoha Watchdog Driver
5 * Copyright (c) 2024, AIROHA All rights reserved.
7 * Mayur Kumar <mayur.kumar@airoha.com>
8 * Christian Marangi <ansuelsmth@gmail.com>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/types.h>
16 #include <linux/bitfield.h>
17 #include <linux/clk.h>
19 #include <linux/math.h>
21 #include <linux/platform_device.h>
22 #include <linux/watchdog.h>
24 /* Base address of timer and watchdog registers */
25 #define TIMER_CTRL 0x0
26 #define WDT_ENABLE BIT(25)
27 #define WDT_TIMER_INTERRUPT BIT(21)
28 /* Timer3 is used as Watchdog Timer */
29 #define WDT_TIMER_ENABLE BIT(5)
30 #define WDT_TIMER_LOAD_VALUE 0x2c
31 #define WDT_TIMER_CUR_VALUE 0x30
32 #define WDT_TIMER_VAL GENMASK(31, 0)
33 #define WDT_RELOAD 0x38
34 #define WDT_RLD BIT(0)
36 /* Airoha watchdog structure description */
37 struct airoha_wdt_desc
{
38 struct watchdog_device wdog_dev
;
39 unsigned int wdt_freq
;
43 #define WDT_HEARTBEAT 24
44 static int heartbeat
= WDT_HEARTBEAT
;
45 module_param(heartbeat
, int, 0);
46 MODULE_PARM_DESC(heartbeat
, "Watchdog heartbeats in seconds. (default="
47 __MODULE_STRING(WDT_HEARTBEAT
) ")");
49 static bool nowayout
= WATCHDOG_NOWAYOUT
;
50 module_param(nowayout
, bool, 0);
51 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started (default="
52 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
54 static int airoha_wdt_start(struct watchdog_device
*wdog_dev
)
56 struct airoha_wdt_desc
*airoha_wdt
= watchdog_get_drvdata(wdog_dev
);
59 val
= readl(airoha_wdt
->base
+ TIMER_CTRL
);
60 val
|= (WDT_TIMER_ENABLE
| WDT_ENABLE
| WDT_TIMER_INTERRUPT
);
61 writel(val
, airoha_wdt
->base
+ TIMER_CTRL
);
62 val
= wdog_dev
->timeout
* airoha_wdt
->wdt_freq
;
63 writel(val
, airoha_wdt
->base
+ WDT_TIMER_LOAD_VALUE
);
68 static int airoha_wdt_stop(struct watchdog_device
*wdog_dev
)
70 struct airoha_wdt_desc
*airoha_wdt
= watchdog_get_drvdata(wdog_dev
);
73 val
= readl(airoha_wdt
->base
+ TIMER_CTRL
);
74 val
&= (~WDT_ENABLE
& ~WDT_TIMER_ENABLE
);
75 writel(val
, airoha_wdt
->base
+ TIMER_CTRL
);
80 static int airoha_wdt_ping(struct watchdog_device
*wdog_dev
)
82 struct airoha_wdt_desc
*airoha_wdt
= watchdog_get_drvdata(wdog_dev
);
85 val
= readl(airoha_wdt
->base
+ WDT_RELOAD
);
87 writel(val
, airoha_wdt
->base
+ WDT_RELOAD
);
92 static int airoha_wdt_set_timeout(struct watchdog_device
*wdog_dev
, unsigned int timeout
)
94 wdog_dev
->timeout
= timeout
;
96 if (watchdog_active(wdog_dev
)) {
97 airoha_wdt_stop(wdog_dev
);
98 return airoha_wdt_start(wdog_dev
);
104 static unsigned int airoha_wdt_get_timeleft(struct watchdog_device
*wdog_dev
)
106 struct airoha_wdt_desc
*airoha_wdt
= watchdog_get_drvdata(wdog_dev
);
109 val
= readl(airoha_wdt
->base
+ WDT_TIMER_CUR_VALUE
);
110 return DIV_ROUND_UP(val
, airoha_wdt
->wdt_freq
);
113 static const struct watchdog_info airoha_wdt_info
= {
114 .options
= WDIOF_SETTIMEOUT
| WDIOF_MAGICCLOSE
| WDIOF_KEEPALIVEPING
,
115 .identity
= "Airoha Watchdog",
118 static const struct watchdog_ops airoha_wdt_ops
= {
119 .owner
= THIS_MODULE
,
120 .start
= airoha_wdt_start
,
121 .stop
= airoha_wdt_stop
,
122 .ping
= airoha_wdt_ping
,
123 .set_timeout
= airoha_wdt_set_timeout
,
124 .get_timeleft
= airoha_wdt_get_timeleft
,
127 static int airoha_wdt_probe(struct platform_device
*pdev
)
129 struct airoha_wdt_desc
*airoha_wdt
;
130 struct watchdog_device
*wdog_dev
;
131 struct device
*dev
= &pdev
->dev
;
135 airoha_wdt
= devm_kzalloc(dev
, sizeof(*airoha_wdt
), GFP_KERNEL
);
139 airoha_wdt
->base
= devm_platform_ioremap_resource(pdev
, 0);
140 if (IS_ERR(airoha_wdt
->base
))
141 return PTR_ERR(airoha_wdt
->base
);
143 bus_clk
= devm_clk_get_enabled(dev
, "bus");
145 return dev_err_probe(dev
, PTR_ERR(bus_clk
),
146 "failed to enable bus clock\n");
148 /* Watchdog ticks at half the bus rate */
149 airoha_wdt
->wdt_freq
= clk_get_rate(bus_clk
) / 2;
151 /* Initialize struct watchdog device */
152 wdog_dev
= &airoha_wdt
->wdog_dev
;
153 wdog_dev
->timeout
= heartbeat
;
154 wdog_dev
->info
= &airoha_wdt_info
;
155 wdog_dev
->ops
= &airoha_wdt_ops
;
156 /* Bus 300MHz, watchdog 150MHz, 28 seconds */
157 wdog_dev
->max_timeout
= FIELD_MAX(WDT_TIMER_VAL
) / airoha_wdt
->wdt_freq
;
158 wdog_dev
->parent
= dev
;
160 watchdog_set_drvdata(wdog_dev
, airoha_wdt
);
161 watchdog_set_nowayout(wdog_dev
, nowayout
);
162 watchdog_stop_on_unregister(wdog_dev
);
164 ret
= devm_watchdog_register_device(dev
, wdog_dev
);
168 platform_set_drvdata(pdev
, airoha_wdt
);
172 static int airoha_wdt_suspend(struct device
*dev
)
174 struct airoha_wdt_desc
*airoha_wdt
= dev_get_drvdata(dev
);
176 if (watchdog_active(&airoha_wdt
->wdog_dev
))
177 airoha_wdt_stop(&airoha_wdt
->wdog_dev
);
182 static int airoha_wdt_resume(struct device
*dev
)
184 struct airoha_wdt_desc
*airoha_wdt
= dev_get_drvdata(dev
);
186 if (watchdog_active(&airoha_wdt
->wdog_dev
)) {
187 airoha_wdt_start(&airoha_wdt
->wdog_dev
);
188 airoha_wdt_ping(&airoha_wdt
->wdog_dev
);
193 static const struct of_device_id airoha_wdt_of_match
[] = {
194 { .compatible
= "airoha,en7581-wdt", },
198 MODULE_DEVICE_TABLE(of
, airoha_wdt_of_match
);
200 static DEFINE_SIMPLE_DEV_PM_OPS(airoha_wdt_pm_ops
, airoha_wdt_suspend
, airoha_wdt_resume
);
202 static struct platform_driver airoha_wdt_driver
= {
203 .probe
= airoha_wdt_probe
,
205 .name
= "airoha-wdt",
206 .pm
= pm_sleep_ptr(&airoha_wdt_pm_ops
),
207 .of_match_table
= airoha_wdt_of_match
,
211 module_platform_driver(airoha_wdt_driver
);
213 MODULE_AUTHOR("Mayur Kumar <mayur.kumar@airoha.com>");
214 MODULE_AUTHOR("Christian Marangi <ansuelsmth@gmail.com>");
215 MODULE_DESCRIPTION("Airoha EN7581 Watchdog Driver");
216 MODULE_LICENSE("GPL");