1 /* Copyright (c) 2014, The Linux Foundation. All rights reserved.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
13 #include <linux/clk.h>
14 #include <linux/delay.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/watchdog.h>
25 #define WDT_BITE_TIME 0x5C
28 struct watchdog_device wdd
;
35 struct qcom_wdt
*to_qcom_wdt(struct watchdog_device
*wdd
)
37 return container_of(wdd
, struct qcom_wdt
, wdd
);
40 static int qcom_wdt_start(struct watchdog_device
*wdd
)
42 struct qcom_wdt
*wdt
= to_qcom_wdt(wdd
);
44 writel(0, wdt
->base
+ WDT_EN
);
45 writel(1, wdt
->base
+ WDT_RST
);
46 writel(wdd
->timeout
* wdt
->rate
, wdt
->base
+ WDT_BITE_TIME
);
47 writel(1, wdt
->base
+ WDT_EN
);
51 static int qcom_wdt_stop(struct watchdog_device
*wdd
)
53 struct qcom_wdt
*wdt
= to_qcom_wdt(wdd
);
55 writel(0, wdt
->base
+ WDT_EN
);
59 static int qcom_wdt_ping(struct watchdog_device
*wdd
)
61 struct qcom_wdt
*wdt
= to_qcom_wdt(wdd
);
63 writel(1, wdt
->base
+ WDT_RST
);
67 static int qcom_wdt_set_timeout(struct watchdog_device
*wdd
,
70 wdd
->timeout
= timeout
;
71 return qcom_wdt_start(wdd
);
74 static int qcom_wdt_restart(struct watchdog_device
*wdd
, unsigned long action
,
77 struct qcom_wdt
*wdt
= to_qcom_wdt(wdd
);
81 * Trigger watchdog bite:
82 * Setup BITE_TIME to be 128ms, and enable WDT.
84 timeout
= 128 * wdt
->rate
/ 1000;
86 writel(0, wdt
->base
+ WDT_EN
);
87 writel(1, wdt
->base
+ WDT_RST
);
88 writel(timeout
, wdt
->base
+ WDT_BITE_TIME
);
89 writel(1, wdt
->base
+ WDT_EN
);
92 * Actually make sure the above sequence hits hardware before sleeping.
100 static const struct watchdog_ops qcom_wdt_ops
= {
101 .start
= qcom_wdt_start
,
102 .stop
= qcom_wdt_stop
,
103 .ping
= qcom_wdt_ping
,
104 .set_timeout
= qcom_wdt_set_timeout
,
105 .restart
= qcom_wdt_restart
,
106 .owner
= THIS_MODULE
,
109 static const struct watchdog_info qcom_wdt_info
= {
110 .options
= WDIOF_KEEPALIVEPING
114 .identity
= KBUILD_MODNAME
,
117 static int qcom_wdt_probe(struct platform_device
*pdev
)
119 struct qcom_wdt
*wdt
;
120 struct resource
*res
;
121 struct device_node
*np
= pdev
->dev
.of_node
;
125 wdt
= devm_kzalloc(&pdev
->dev
, sizeof(*wdt
), GFP_KERNEL
);
129 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
131 /* We use CPU0's DGT for the watchdog */
132 if (of_property_read_u32(np
, "cpu-offset", &percpu_offset
))
135 res
->start
+= percpu_offset
;
136 res
->end
+= percpu_offset
;
138 wdt
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
139 if (IS_ERR(wdt
->base
))
140 return PTR_ERR(wdt
->base
);
142 wdt
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
143 if (IS_ERR(wdt
->clk
)) {
144 dev_err(&pdev
->dev
, "failed to get input clock\n");
145 return PTR_ERR(wdt
->clk
);
148 ret
= clk_prepare_enable(wdt
->clk
);
150 dev_err(&pdev
->dev
, "failed to setup clock\n");
155 * We use the clock rate to calculate the max timeout, so ensure it's
156 * not zero to avoid a divide-by-zero exception.
158 * WATCHDOG_CORE assumes units of seconds, if the WDT is clocked such
159 * that it would bite before a second elapses it's usefulness is
160 * limited. Bail if this is the case.
162 wdt
->rate
= clk_get_rate(wdt
->clk
);
163 if (wdt
->rate
== 0 ||
164 wdt
->rate
> 0x10000000U
) {
165 dev_err(&pdev
->dev
, "invalid clock rate\n");
167 goto err_clk_unprepare
;
170 wdt
->wdd
.info
= &qcom_wdt_info
;
171 wdt
->wdd
.ops
= &qcom_wdt_ops
;
172 wdt
->wdd
.min_timeout
= 1;
173 wdt
->wdd
.max_timeout
= 0x10000000U
/ wdt
->rate
;
174 wdt
->wdd
.parent
= &pdev
->dev
;
176 if (readl(wdt
->base
+ WDT_STS
) & 1)
177 wdt
->wdd
.bootstatus
= WDIOF_CARDRESET
;
180 * If 'timeout-sec' unspecified in devicetree, assume a 30 second
181 * default, unless the max timeout is less than 30 seconds, then use
184 wdt
->wdd
.timeout
= min(wdt
->wdd
.max_timeout
, 30U);
185 watchdog_init_timeout(&wdt
->wdd
, 0, &pdev
->dev
);
187 ret
= watchdog_register_device(&wdt
->wdd
);
189 dev_err(&pdev
->dev
, "failed to register watchdog\n");
190 goto err_clk_unprepare
;
193 platform_set_drvdata(pdev
, wdt
);
197 clk_disable_unprepare(wdt
->clk
);
201 static int qcom_wdt_remove(struct platform_device
*pdev
)
203 struct qcom_wdt
*wdt
= platform_get_drvdata(pdev
);
205 watchdog_unregister_device(&wdt
->wdd
);
206 clk_disable_unprepare(wdt
->clk
);
210 static const struct of_device_id qcom_wdt_of_table
[] = {
211 { .compatible
= "qcom,kpss-timer" },
212 { .compatible
= "qcom,scss-timer" },
215 MODULE_DEVICE_TABLE(of
, qcom_wdt_of_table
);
217 static struct platform_driver qcom_watchdog_driver
= {
218 .probe
= qcom_wdt_probe
,
219 .remove
= qcom_wdt_remove
,
221 .name
= KBUILD_MODNAME
,
222 .of_match_table
= qcom_wdt_of_table
,
225 module_platform_driver(qcom_watchdog_driver
);
227 MODULE_DESCRIPTION("QCOM KPSS Watchdog Driver");
228 MODULE_LICENSE("GPL v2");