Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright 2018-2019 NXP.
0004  */
0005 
0006 #include <linux/arm-smccc.h>
0007 #include <linux/firmware/imx/sci.h>
0008 #include <linux/io.h>
0009 #include <linux/kernel.h>
0010 #include <linux/module.h>
0011 #include <linux/moduleparam.h>
0012 #include <linux/of.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/watchdog.h>
0015 
0016 #define DEFAULT_TIMEOUT 60
0017 /*
0018  * Software timer tick implemented in scfw side, support 10ms to 0xffffffff ms
0019  * in theory, but for normal case, 1s~128s is enough, you can change this max
0020  * value in case it's not enough.
0021  */
0022 #define MAX_TIMEOUT 128
0023 
0024 #define IMX_SIP_TIMER           0xC2000002
0025 #define IMX_SIP_TIMER_START_WDOG        0x01
0026 #define IMX_SIP_TIMER_STOP_WDOG     0x02
0027 #define IMX_SIP_TIMER_SET_WDOG_ACT  0x03
0028 #define IMX_SIP_TIMER_PING_WDOG     0x04
0029 #define IMX_SIP_TIMER_SET_TIMEOUT_WDOG  0x05
0030 #define IMX_SIP_TIMER_GET_WDOG_STAT 0x06
0031 #define IMX_SIP_TIMER_SET_PRETIME_WDOG  0x07
0032 
0033 #define SC_TIMER_WDOG_ACTION_PARTITION  0
0034 
0035 #define SC_IRQ_WDOG         1
0036 #define SC_IRQ_GROUP_WDOG       1
0037 
0038 static bool nowayout = WATCHDOG_NOWAYOUT;
0039 module_param(nowayout, bool, 0000);
0040 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
0041          __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
0042 
0043 struct imx_sc_wdt_device {
0044     struct watchdog_device wdd;
0045     struct notifier_block wdt_notifier;
0046 };
0047 
0048 static int imx_sc_wdt_ping(struct watchdog_device *wdog)
0049 {
0050     struct arm_smccc_res res;
0051 
0052     arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_PING_WDOG,
0053               0, 0, 0, 0, 0, 0, &res);
0054 
0055     return 0;
0056 }
0057 
0058 static int imx_sc_wdt_start(struct watchdog_device *wdog)
0059 {
0060     struct arm_smccc_res res;
0061 
0062     arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_START_WDOG,
0063               0, 0, 0, 0, 0, 0, &res);
0064     if (res.a0)
0065         return -EACCES;
0066 
0067     arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_SET_WDOG_ACT,
0068               SC_TIMER_WDOG_ACTION_PARTITION,
0069               0, 0, 0, 0, 0, &res);
0070     return res.a0 ? -EACCES : 0;
0071 }
0072 
0073 static int imx_sc_wdt_stop(struct watchdog_device *wdog)
0074 {
0075     struct arm_smccc_res res;
0076 
0077     arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_STOP_WDOG,
0078               0, 0, 0, 0, 0, 0, &res);
0079 
0080     return res.a0 ? -EACCES : 0;
0081 }
0082 
0083 static int imx_sc_wdt_set_timeout(struct watchdog_device *wdog,
0084                 unsigned int timeout)
0085 {
0086     struct arm_smccc_res res;
0087 
0088     wdog->timeout = timeout;
0089     arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_SET_TIMEOUT_WDOG,
0090               timeout * 1000, 0, 0, 0, 0, 0, &res);
0091 
0092     return res.a0 ? -EACCES : 0;
0093 }
0094 
0095 static int imx_sc_wdt_set_pretimeout(struct watchdog_device *wdog,
0096                      unsigned int pretimeout)
0097 {
0098     struct arm_smccc_res res;
0099 
0100     /*
0101      * SCU firmware calculates pretimeout based on current time
0102      * stamp instead of watchdog timeout stamp, need to convert
0103      * the pretimeout to SCU firmware's timeout value.
0104      */
0105     arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_SET_PRETIME_WDOG,
0106               (wdog->timeout - pretimeout) * 1000, 0, 0, 0,
0107               0, 0, &res);
0108     if (res.a0)
0109         return -EACCES;
0110 
0111     wdog->pretimeout = pretimeout;
0112 
0113     return 0;
0114 }
0115 
0116 static int imx_sc_wdt_notify(struct notifier_block *nb,
0117                  unsigned long event, void *group)
0118 {
0119     struct imx_sc_wdt_device *imx_sc_wdd =
0120                  container_of(nb,
0121                           struct imx_sc_wdt_device,
0122                           wdt_notifier);
0123 
0124     if (event & SC_IRQ_WDOG &&
0125         *(u8 *)group == SC_IRQ_GROUP_WDOG)
0126         watchdog_notify_pretimeout(&imx_sc_wdd->wdd);
0127 
0128     return 0;
0129 }
0130 
0131 static void imx_sc_wdt_action(void *data)
0132 {
0133     struct notifier_block *wdt_notifier = data;
0134 
0135     imx_scu_irq_unregister_notifier(wdt_notifier);
0136     imx_scu_irq_group_enable(SC_IRQ_GROUP_WDOG,
0137                  SC_IRQ_WDOG,
0138                  false);
0139 }
0140 
0141 static const struct watchdog_ops imx_sc_wdt_ops = {
0142     .owner = THIS_MODULE,
0143     .start = imx_sc_wdt_start,
0144     .stop  = imx_sc_wdt_stop,
0145     .ping  = imx_sc_wdt_ping,
0146     .set_timeout = imx_sc_wdt_set_timeout,
0147     .set_pretimeout = imx_sc_wdt_set_pretimeout,
0148 };
0149 
0150 static struct watchdog_info imx_sc_wdt_info = {
0151     .identity   = "i.MX SC watchdog timer",
0152     .options    = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
0153               WDIOF_MAGICCLOSE,
0154 };
0155 
0156 static int imx_sc_wdt_probe(struct platform_device *pdev)
0157 {
0158     struct imx_sc_wdt_device *imx_sc_wdd;
0159     struct watchdog_device *wdog;
0160     struct device *dev = &pdev->dev;
0161     int ret;
0162 
0163     imx_sc_wdd = devm_kzalloc(dev, sizeof(*imx_sc_wdd), GFP_KERNEL);
0164     if (!imx_sc_wdd)
0165         return -ENOMEM;
0166 
0167     platform_set_drvdata(pdev, imx_sc_wdd);
0168 
0169     wdog = &imx_sc_wdd->wdd;
0170     wdog->info = &imx_sc_wdt_info;
0171     wdog->ops = &imx_sc_wdt_ops;
0172     wdog->min_timeout = 1;
0173     wdog->max_timeout = MAX_TIMEOUT;
0174     wdog->parent = dev;
0175     wdog->timeout = DEFAULT_TIMEOUT;
0176 
0177     watchdog_init_timeout(wdog, 0, dev);
0178 
0179     ret = imx_sc_wdt_set_timeout(wdog, wdog->timeout);
0180     if (ret)
0181         return ret;
0182 
0183     watchdog_stop_on_reboot(wdog);
0184     watchdog_stop_on_unregister(wdog);
0185 
0186     ret = imx_scu_irq_group_enable(SC_IRQ_GROUP_WDOG,
0187                        SC_IRQ_WDOG,
0188                        true);
0189     if (ret) {
0190         dev_warn(dev, "Enable irq failed, pretimeout NOT supported\n");
0191         goto register_device;
0192     }
0193 
0194     imx_sc_wdd->wdt_notifier.notifier_call = imx_sc_wdt_notify;
0195     ret = imx_scu_irq_register_notifier(&imx_sc_wdd->wdt_notifier);
0196     if (ret) {
0197         imx_scu_irq_group_enable(SC_IRQ_GROUP_WDOG,
0198                      SC_IRQ_WDOG,
0199                      false);
0200         dev_warn(dev,
0201              "Register irq notifier failed, pretimeout NOT supported\n");
0202         goto register_device;
0203     }
0204 
0205     ret = devm_add_action_or_reset(dev, imx_sc_wdt_action,
0206                        &imx_sc_wdd->wdt_notifier);
0207     if (!ret)
0208         imx_sc_wdt_info.options |= WDIOF_PRETIMEOUT;
0209     else
0210         dev_warn(dev, "Add action failed, pretimeout NOT supported\n");
0211 
0212 register_device:
0213     return devm_watchdog_register_device(dev, wdog);
0214 }
0215 
0216 static int __maybe_unused imx_sc_wdt_suspend(struct device *dev)
0217 {
0218     struct imx_sc_wdt_device *imx_sc_wdd = dev_get_drvdata(dev);
0219 
0220     if (watchdog_active(&imx_sc_wdd->wdd))
0221         imx_sc_wdt_stop(&imx_sc_wdd->wdd);
0222 
0223     return 0;
0224 }
0225 
0226 static int __maybe_unused imx_sc_wdt_resume(struct device *dev)
0227 {
0228     struct imx_sc_wdt_device *imx_sc_wdd = dev_get_drvdata(dev);
0229 
0230     if (watchdog_active(&imx_sc_wdd->wdd))
0231         imx_sc_wdt_start(&imx_sc_wdd->wdd);
0232 
0233     return 0;
0234 }
0235 
0236 static SIMPLE_DEV_PM_OPS(imx_sc_wdt_pm_ops,
0237              imx_sc_wdt_suspend, imx_sc_wdt_resume);
0238 
0239 static const struct of_device_id imx_sc_wdt_dt_ids[] = {
0240     { .compatible = "fsl,imx-sc-wdt", },
0241     { /* sentinel */ }
0242 };
0243 MODULE_DEVICE_TABLE(of, imx_sc_wdt_dt_ids);
0244 
0245 static struct platform_driver imx_sc_wdt_driver = {
0246     .probe      = imx_sc_wdt_probe,
0247     .driver     = {
0248         .name   = "imx-sc-wdt",
0249         .of_match_table = imx_sc_wdt_dt_ids,
0250         .pm = &imx_sc_wdt_pm_ops,
0251     },
0252 };
0253 module_platform_driver(imx_sc_wdt_driver);
0254 
0255 MODULE_AUTHOR("Robin Gong <yibin.gong@nxp.com>");
0256 MODULE_DESCRIPTION("NXP i.MX system controller watchdog driver");
0257 MODULE_LICENSE("GPL v2");