Anson Huang | 986857ac | 2019-03-21 02:26:47 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright 2018-2019 NXP. |
| 4 | */ |
| 5 | |
| 6 | #include <linux/arm-smccc.h> |
Anson Huang | 15f7d7f | 2019-05-27 15:03:17 +0800 | [diff] [blame] | 7 | #include <linux/firmware/imx/sci.h> |
Anson Huang | 986857ac | 2019-03-21 02:26:47 +0000 | [diff] [blame] | 8 | #include <linux/io.h> |
Anson Huang | 986857ac | 2019-03-21 02:26:47 +0000 | [diff] [blame] | 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> |
Anson Huang | 986857ac | 2019-03-21 02:26:47 +0000 | [diff] [blame] | 14 | #include <linux/watchdog.h> |
| 15 | |
| 16 | #define DEFAULT_TIMEOUT 60 |
| 17 | /* |
| 18 | * Software timer tick implemented in scfw side, support 10ms to 0xffffffff ms |
| 19 | * in theory, but for normal case, 1s~128s is enough, you can change this max |
| 20 | * value in case it's not enough. |
| 21 | */ |
| 22 | #define MAX_TIMEOUT 128 |
| 23 | |
| 24 | #define IMX_SIP_TIMER 0xC2000002 |
| 25 | #define IMX_SIP_TIMER_START_WDOG 0x01 |
| 26 | #define IMX_SIP_TIMER_STOP_WDOG 0x02 |
| 27 | #define IMX_SIP_TIMER_SET_WDOG_ACT 0x03 |
| 28 | #define IMX_SIP_TIMER_PING_WDOG 0x04 |
| 29 | #define IMX_SIP_TIMER_SET_TIMEOUT_WDOG 0x05 |
| 30 | #define IMX_SIP_TIMER_GET_WDOG_STAT 0x06 |
| 31 | #define IMX_SIP_TIMER_SET_PRETIME_WDOG 0x07 |
| 32 | |
| 33 | #define SC_TIMER_WDOG_ACTION_PARTITION 0 |
| 34 | |
Anson Huang | 15f7d7f | 2019-05-27 15:03:17 +0800 | [diff] [blame] | 35 | #define SC_IRQ_WDOG 1 |
| 36 | #define SC_IRQ_GROUP_WDOG 1 |
| 37 | |
Anson Huang | 986857ac | 2019-03-21 02:26:47 +0000 | [diff] [blame] | 38 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 39 | module_param(nowayout, bool, 0000); |
| 40 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" |
| 41 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 42 | |
Anson Huang | 15f7d7f | 2019-05-27 15:03:17 +0800 | [diff] [blame] | 43 | struct imx_sc_wdt_device { |
| 44 | struct watchdog_device wdd; |
| 45 | struct notifier_block wdt_notifier; |
| 46 | }; |
| 47 | |
Anson Huang | 986857ac | 2019-03-21 02:26:47 +0000 | [diff] [blame] | 48 | static int imx_sc_wdt_ping(struct watchdog_device *wdog) |
| 49 | { |
| 50 | struct arm_smccc_res res; |
| 51 | |
| 52 | arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_PING_WDOG, |
| 53 | 0, 0, 0, 0, 0, 0, &res); |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static int imx_sc_wdt_start(struct watchdog_device *wdog) |
| 59 | { |
| 60 | struct arm_smccc_res res; |
| 61 | |
| 62 | arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_START_WDOG, |
| 63 | 0, 0, 0, 0, 0, 0, &res); |
| 64 | if (res.a0) |
| 65 | return -EACCES; |
| 66 | |
| 67 | arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_SET_WDOG_ACT, |
| 68 | SC_TIMER_WDOG_ACTION_PARTITION, |
| 69 | 0, 0, 0, 0, 0, &res); |
| 70 | return res.a0 ? -EACCES : 0; |
| 71 | } |
| 72 | |
| 73 | static int imx_sc_wdt_stop(struct watchdog_device *wdog) |
| 74 | { |
| 75 | struct arm_smccc_res res; |
| 76 | |
| 77 | arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_STOP_WDOG, |
| 78 | 0, 0, 0, 0, 0, 0, &res); |
| 79 | |
| 80 | return res.a0 ? -EACCES : 0; |
| 81 | } |
| 82 | |
| 83 | static int imx_sc_wdt_set_timeout(struct watchdog_device *wdog, |
| 84 | unsigned int timeout) |
| 85 | { |
| 86 | struct arm_smccc_res res; |
| 87 | |
| 88 | wdog->timeout = timeout; |
| 89 | arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_SET_TIMEOUT_WDOG, |
| 90 | timeout * 1000, 0, 0, 0, 0, 0, &res); |
| 91 | |
| 92 | return res.a0 ? -EACCES : 0; |
| 93 | } |
| 94 | |
Anson Huang | 15f7d7f | 2019-05-27 15:03:17 +0800 | [diff] [blame] | 95 | static int imx_sc_wdt_set_pretimeout(struct watchdog_device *wdog, |
| 96 | unsigned int pretimeout) |
| 97 | { |
| 98 | struct arm_smccc_res res; |
| 99 | |
Anson Huang | 2c50a6b | 2019-10-09 15:37:47 +0800 | [diff] [blame] | 100 | /* |
| 101 | * SCU firmware calculates pretimeout based on current time |
| 102 | * stamp instead of watchdog timeout stamp, need to convert |
| 103 | * the pretimeout to SCU firmware's timeout value. |
| 104 | */ |
Anson Huang | 15f7d7f | 2019-05-27 15:03:17 +0800 | [diff] [blame] | 105 | arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_SET_PRETIME_WDOG, |
Anson Huang | 2c50a6b | 2019-10-09 15:37:47 +0800 | [diff] [blame] | 106 | (wdog->timeout - pretimeout) * 1000, 0, 0, 0, |
| 107 | 0, 0, &res); |
Anson Huang | 15f7d7f | 2019-05-27 15:03:17 +0800 | [diff] [blame] | 108 | if (res.a0) |
| 109 | return -EACCES; |
| 110 | |
| 111 | wdog->pretimeout = pretimeout; |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | static int imx_sc_wdt_notify(struct notifier_block *nb, |
| 117 | unsigned long event, void *group) |
| 118 | { |
| 119 | struct imx_sc_wdt_device *imx_sc_wdd = |
| 120 | container_of(nb, |
| 121 | struct imx_sc_wdt_device, |
| 122 | wdt_notifier); |
| 123 | |
| 124 | if (event & SC_IRQ_WDOG && |
| 125 | *(u8 *)group == SC_IRQ_GROUP_WDOG) |
| 126 | watchdog_notify_pretimeout(&imx_sc_wdd->wdd); |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static void imx_sc_wdt_action(void *data) |
| 132 | { |
| 133 | struct notifier_block *wdt_notifier = data; |
| 134 | |
| 135 | imx_scu_irq_unregister_notifier(wdt_notifier); |
| 136 | imx_scu_irq_group_enable(SC_IRQ_GROUP_WDOG, |
| 137 | SC_IRQ_WDOG, |
| 138 | false); |
| 139 | } |
| 140 | |
Anson Huang | 986857ac | 2019-03-21 02:26:47 +0000 | [diff] [blame] | 141 | static const struct watchdog_ops imx_sc_wdt_ops = { |
| 142 | .owner = THIS_MODULE, |
| 143 | .start = imx_sc_wdt_start, |
| 144 | .stop = imx_sc_wdt_stop, |
| 145 | .ping = imx_sc_wdt_ping, |
| 146 | .set_timeout = imx_sc_wdt_set_timeout, |
Anson Huang | 15f7d7f | 2019-05-27 15:03:17 +0800 | [diff] [blame] | 147 | .set_pretimeout = imx_sc_wdt_set_pretimeout, |
Anson Huang | 986857ac | 2019-03-21 02:26:47 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
Anson Huang | 15f7d7f | 2019-05-27 15:03:17 +0800 | [diff] [blame] | 150 | static struct watchdog_info imx_sc_wdt_info = { |
Anson Huang | 986857ac | 2019-03-21 02:26:47 +0000 | [diff] [blame] | 151 | .identity = "i.MX SC watchdog timer", |
| 152 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | |
Anson Huang | 15f7d7f | 2019-05-27 15:03:17 +0800 | [diff] [blame] | 153 | WDIOF_MAGICCLOSE, |
Anson Huang | 986857ac | 2019-03-21 02:26:47 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | static int imx_sc_wdt_probe(struct platform_device *pdev) |
| 157 | { |
Anson Huang | 15f7d7f | 2019-05-27 15:03:17 +0800 | [diff] [blame] | 158 | struct imx_sc_wdt_device *imx_sc_wdd; |
| 159 | struct watchdog_device *wdog; |
Guenter Roeck | 01022e3 | 2019-04-10 09:27:51 -0700 | [diff] [blame] | 160 | struct device *dev = &pdev->dev; |
Anson Huang | 986857ac | 2019-03-21 02:26:47 +0000 | [diff] [blame] | 161 | int ret; |
| 162 | |
Guenter Roeck | 01022e3 | 2019-04-10 09:27:51 -0700 | [diff] [blame] | 163 | imx_sc_wdd = devm_kzalloc(dev, sizeof(*imx_sc_wdd), GFP_KERNEL); |
Anson Huang | 986857ac | 2019-03-21 02:26:47 +0000 | [diff] [blame] | 164 | if (!imx_sc_wdd) |
| 165 | return -ENOMEM; |
| 166 | |
| 167 | platform_set_drvdata(pdev, imx_sc_wdd); |
| 168 | |
Anson Huang | 15f7d7f | 2019-05-27 15:03:17 +0800 | [diff] [blame] | 169 | wdog = &imx_sc_wdd->wdd; |
| 170 | wdog->info = &imx_sc_wdt_info; |
| 171 | wdog->ops = &imx_sc_wdt_ops; |
| 172 | wdog->min_timeout = 1; |
| 173 | wdog->max_timeout = MAX_TIMEOUT; |
| 174 | wdog->parent = dev; |
| 175 | wdog->timeout = DEFAULT_TIMEOUT; |
Anson Huang | 986857ac | 2019-03-21 02:26:47 +0000 | [diff] [blame] | 176 | |
Anson Huang | 15f7d7f | 2019-05-27 15:03:17 +0800 | [diff] [blame] | 177 | watchdog_init_timeout(wdog, 0, dev); |
Fabio Estevam | e56d48e | 2020-04-12 20:01:22 -0300 | [diff] [blame] | 178 | |
| 179 | ret = imx_sc_wdt_set_timeout(wdog, wdog->timeout); |
| 180 | if (ret) |
| 181 | return ret; |
| 182 | |
Anson Huang | 15f7d7f | 2019-05-27 15:03:17 +0800 | [diff] [blame] | 183 | watchdog_stop_on_reboot(wdog); |
| 184 | watchdog_stop_on_unregister(wdog); |
Anson Huang | 986857ac | 2019-03-21 02:26:47 +0000 | [diff] [blame] | 185 | |
Anson Huang | 15f7d7f | 2019-05-27 15:03:17 +0800 | [diff] [blame] | 186 | ret = devm_watchdog_register_device(dev, wdog); |
Anson Huang | 30520ee | 2019-08-12 16:44:34 +0800 | [diff] [blame] | 187 | if (ret) |
Oliver Graute | 3b7c09f | 2019-09-05 14:36:49 +0000 | [diff] [blame] | 188 | return ret; |
| 189 | |
Anson Huang | 15f7d7f | 2019-05-27 15:03:17 +0800 | [diff] [blame] | 190 | ret = imx_scu_irq_group_enable(SC_IRQ_GROUP_WDOG, |
| 191 | SC_IRQ_WDOG, |
| 192 | true); |
| 193 | if (ret) { |
| 194 | dev_warn(dev, "Enable irq failed, pretimeout NOT supported\n"); |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | imx_sc_wdd->wdt_notifier.notifier_call = imx_sc_wdt_notify; |
| 199 | ret = imx_scu_irq_register_notifier(&imx_sc_wdd->wdt_notifier); |
| 200 | if (ret) { |
| 201 | imx_scu_irq_group_enable(SC_IRQ_GROUP_WDOG, |
| 202 | SC_IRQ_WDOG, |
| 203 | false); |
| 204 | dev_warn(dev, |
| 205 | "Register irq notifier failed, pretimeout NOT supported\n"); |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | ret = devm_add_action_or_reset(dev, imx_sc_wdt_action, |
| 210 | &imx_sc_wdd->wdt_notifier); |
| 211 | if (!ret) |
| 212 | imx_sc_wdt_info.options |= WDIOF_PRETIMEOUT; |
| 213 | else |
| 214 | dev_warn(dev, "Add action failed, pretimeout NOT supported\n"); |
| 215 | |
| 216 | return 0; |
Anson Huang | 986857ac | 2019-03-21 02:26:47 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static int __maybe_unused imx_sc_wdt_suspend(struct device *dev) |
| 220 | { |
Anson Huang | 15f7d7f | 2019-05-27 15:03:17 +0800 | [diff] [blame] | 221 | struct imx_sc_wdt_device *imx_sc_wdd = dev_get_drvdata(dev); |
Anson Huang | 986857ac | 2019-03-21 02:26:47 +0000 | [diff] [blame] | 222 | |
Anson Huang | 15f7d7f | 2019-05-27 15:03:17 +0800 | [diff] [blame] | 223 | if (watchdog_active(&imx_sc_wdd->wdd)) |
| 224 | imx_sc_wdt_stop(&imx_sc_wdd->wdd); |
Anson Huang | 986857ac | 2019-03-21 02:26:47 +0000 | [diff] [blame] | 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | static int __maybe_unused imx_sc_wdt_resume(struct device *dev) |
| 230 | { |
Anson Huang | 15f7d7f | 2019-05-27 15:03:17 +0800 | [diff] [blame] | 231 | struct imx_sc_wdt_device *imx_sc_wdd = dev_get_drvdata(dev); |
Anson Huang | 986857ac | 2019-03-21 02:26:47 +0000 | [diff] [blame] | 232 | |
Anson Huang | 15f7d7f | 2019-05-27 15:03:17 +0800 | [diff] [blame] | 233 | if (watchdog_active(&imx_sc_wdd->wdd)) |
| 234 | imx_sc_wdt_start(&imx_sc_wdd->wdd); |
Anson Huang | 986857ac | 2019-03-21 02:26:47 +0000 | [diff] [blame] | 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | static SIMPLE_DEV_PM_OPS(imx_sc_wdt_pm_ops, |
| 240 | imx_sc_wdt_suspend, imx_sc_wdt_resume); |
| 241 | |
| 242 | static const struct of_device_id imx_sc_wdt_dt_ids[] = { |
| 243 | { .compatible = "fsl,imx-sc-wdt", }, |
| 244 | { /* sentinel */ } |
| 245 | }; |
| 246 | MODULE_DEVICE_TABLE(of, imx_sc_wdt_dt_ids); |
| 247 | |
| 248 | static struct platform_driver imx_sc_wdt_driver = { |
| 249 | .probe = imx_sc_wdt_probe, |
| 250 | .driver = { |
| 251 | .name = "imx-sc-wdt", |
| 252 | .of_match_table = imx_sc_wdt_dt_ids, |
| 253 | .pm = &imx_sc_wdt_pm_ops, |
| 254 | }, |
| 255 | }; |
| 256 | module_platform_driver(imx_sc_wdt_driver); |
| 257 | |
| 258 | MODULE_AUTHOR("Robin Gong <yibin.gong@nxp.com>"); |
| 259 | MODULE_DESCRIPTION("NXP i.MX system controller watchdog driver"); |
| 260 | MODULE_LICENSE("GPL v2"); |