86c2722f2a093bb0d4bed5d066204d07fb556bba
[linux-2.6-block.git] / drivers / watchdog / imx_sc_wdt.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2018-2019 NXP.
4  */
5
6 #include <linux/arm-smccc.h>
7 #include <linux/io.h>
8 #include <linux/init.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/of.h>
13 #include <linux/platform_device.h>
14 #include <linux/reboot.h>
15 #include <linux/watchdog.h>
16
17 #define DEFAULT_TIMEOUT 60
18 /*
19  * Software timer tick implemented in scfw side, support 10ms to 0xffffffff ms
20  * in theory, but for normal case, 1s~128s is enough, you can change this max
21  * value in case it's not enough.
22  */
23 #define MAX_TIMEOUT 128
24
25 #define IMX_SIP_TIMER                   0xC2000002
26 #define IMX_SIP_TIMER_START_WDOG                0x01
27 #define IMX_SIP_TIMER_STOP_WDOG         0x02
28 #define IMX_SIP_TIMER_SET_WDOG_ACT      0x03
29 #define IMX_SIP_TIMER_PING_WDOG         0x04
30 #define IMX_SIP_TIMER_SET_TIMEOUT_WDOG  0x05
31 #define IMX_SIP_TIMER_GET_WDOG_STAT     0x06
32 #define IMX_SIP_TIMER_SET_PRETIME_WDOG  0x07
33
34 #define SC_TIMER_WDOG_ACTION_PARTITION  0
35
36 static bool nowayout = WATCHDOG_NOWAYOUT;
37 module_param(nowayout, bool, 0000);
38 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
39                  __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
40
41 static int imx_sc_wdt_ping(struct watchdog_device *wdog)
42 {
43         struct arm_smccc_res res;
44
45         arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_PING_WDOG,
46                       0, 0, 0, 0, 0, 0, &res);
47
48         return 0;
49 }
50
51 static int imx_sc_wdt_start(struct watchdog_device *wdog)
52 {
53         struct arm_smccc_res res;
54
55         arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_START_WDOG,
56                       0, 0, 0, 0, 0, 0, &res);
57         if (res.a0)
58                 return -EACCES;
59
60         arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_SET_WDOG_ACT,
61                       SC_TIMER_WDOG_ACTION_PARTITION,
62                       0, 0, 0, 0, 0, &res);
63         return res.a0 ? -EACCES : 0;
64 }
65
66 static int imx_sc_wdt_stop(struct watchdog_device *wdog)
67 {
68         struct arm_smccc_res res;
69
70         arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_STOP_WDOG,
71                       0, 0, 0, 0, 0, 0, &res);
72
73         return res.a0 ? -EACCES : 0;
74 }
75
76 static int imx_sc_wdt_set_timeout(struct watchdog_device *wdog,
77                                 unsigned int timeout)
78 {
79         struct arm_smccc_res res;
80
81         wdog->timeout = timeout;
82         arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_SET_TIMEOUT_WDOG,
83                       timeout * 1000, 0, 0, 0, 0, 0, &res);
84
85         return res.a0 ? -EACCES : 0;
86 }
87
88 static const struct watchdog_ops imx_sc_wdt_ops = {
89         .owner = THIS_MODULE,
90         .start = imx_sc_wdt_start,
91         .stop  = imx_sc_wdt_stop,
92         .ping  = imx_sc_wdt_ping,
93         .set_timeout = imx_sc_wdt_set_timeout,
94 };
95
96 static const struct watchdog_info imx_sc_wdt_info = {
97         .identity       = "i.MX SC watchdog timer",
98         .options        = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
99                           WDIOF_MAGICCLOSE | WDIOF_PRETIMEOUT,
100 };
101
102 static int imx_sc_wdt_probe(struct platform_device *pdev)
103 {
104         struct watchdog_device *imx_sc_wdd;
105         int ret;
106
107         imx_sc_wdd = devm_kzalloc(&pdev->dev, sizeof(*imx_sc_wdd), GFP_KERNEL);
108         if (!imx_sc_wdd)
109                 return -ENOMEM;
110
111         platform_set_drvdata(pdev, imx_sc_wdd);
112
113         imx_sc_wdd->info = &imx_sc_wdt_info;
114         imx_sc_wdd->ops = &imx_sc_wdt_ops;
115         imx_sc_wdd->min_timeout = 1;
116         imx_sc_wdd->max_timeout = MAX_TIMEOUT;
117         imx_sc_wdd->parent = &pdev->dev;
118         imx_sc_wdd->timeout = DEFAULT_TIMEOUT;
119
120         ret = watchdog_init_timeout(imx_sc_wdd, 0, &pdev->dev);
121         if (ret)
122                 dev_warn(&pdev->dev, "Failed to set timeout value, using default\n");
123
124         watchdog_stop_on_reboot(imx_sc_wdd);
125         watchdog_stop_on_unregister(imx_sc_wdd);
126
127         ret = devm_watchdog_register_device(&pdev->dev, imx_sc_wdd);
128         if (ret) {
129                 dev_err(&pdev->dev, "Failed to register watchdog device\n");
130                 return ret;
131         }
132
133         return 0;
134 }
135
136 static int __maybe_unused imx_sc_wdt_suspend(struct device *dev)
137 {
138         struct watchdog_device *imx_sc_wdd = dev_get_drvdata(dev);
139
140         if (watchdog_active(imx_sc_wdd))
141                 imx_sc_wdt_stop(imx_sc_wdd);
142
143         return 0;
144 }
145
146 static int __maybe_unused imx_sc_wdt_resume(struct device *dev)
147 {
148         struct watchdog_device *imx_sc_wdd = dev_get_drvdata(dev);
149
150         if (watchdog_active(imx_sc_wdd))
151                 imx_sc_wdt_start(imx_sc_wdd);
152
153         return 0;
154 }
155
156 static SIMPLE_DEV_PM_OPS(imx_sc_wdt_pm_ops,
157                          imx_sc_wdt_suspend, imx_sc_wdt_resume);
158
159 static const struct of_device_id imx_sc_wdt_dt_ids[] = {
160         { .compatible = "fsl,imx-sc-wdt", },
161         { /* sentinel */ }
162 };
163 MODULE_DEVICE_TABLE(of, imx_sc_wdt_dt_ids);
164
165 static struct platform_driver imx_sc_wdt_driver = {
166         .probe          = imx_sc_wdt_probe,
167         .driver         = {
168                 .name   = "imx-sc-wdt",
169                 .of_match_table = imx_sc_wdt_dt_ids,
170                 .pm     = &imx_sc_wdt_pm_ops,
171         },
172 };
173 module_platform_driver(imx_sc_wdt_driver);
174
175 MODULE_AUTHOR("Robin Gong <yibin.gong@nxp.com>");
176 MODULE_DESCRIPTION("NXP i.MX system controller watchdog driver");
177 MODULE_LICENSE("GPL v2");