Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Copyright (C) 2010, Paul Cercueil <paul@crapouillou.net>
0004  *  JZ4740 Watchdog driver
0005  */
0006 
0007 #include <linux/mfd/ingenic-tcu.h>
0008 #include <linux/mfd/syscon.h>
0009 #include <linux/module.h>
0010 #include <linux/moduleparam.h>
0011 #include <linux/types.h>
0012 #include <linux/kernel.h>
0013 #include <linux/watchdog.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/io.h>
0016 #include <linux/device.h>
0017 #include <linux/clk.h>
0018 #include <linux/slab.h>
0019 #include <linux/err.h>
0020 #include <linux/of.h>
0021 #include <linux/regmap.h>
0022 
0023 #define DEFAULT_HEARTBEAT 5
0024 #define MAX_HEARTBEAT     2048
0025 
0026 static bool nowayout = WATCHDOG_NOWAYOUT;
0027 module_param(nowayout, bool, 0);
0028 MODULE_PARM_DESC(nowayout,
0029          "Watchdog cannot be stopped once started (default="
0030          __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
0031 
0032 static unsigned int heartbeat = DEFAULT_HEARTBEAT;
0033 module_param(heartbeat, uint, 0);
0034 MODULE_PARM_DESC(heartbeat,
0035         "Watchdog heartbeat period in seconds from 1 to "
0036         __MODULE_STRING(MAX_HEARTBEAT) ", default "
0037         __MODULE_STRING(DEFAULT_HEARTBEAT));
0038 
0039 struct jz4740_wdt_drvdata {
0040     struct watchdog_device wdt;
0041     struct regmap *map;
0042     struct clk *clk;
0043     unsigned long clk_rate;
0044 };
0045 
0046 static int jz4740_wdt_ping(struct watchdog_device *wdt_dev)
0047 {
0048     struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
0049 
0050     regmap_write(drvdata->map, TCU_REG_WDT_TCNT, 0);
0051 
0052     return 0;
0053 }
0054 
0055 static int jz4740_wdt_set_timeout(struct watchdog_device *wdt_dev,
0056                     unsigned int new_timeout)
0057 {
0058     struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
0059     u16 timeout_value = (u16)(drvdata->clk_rate * new_timeout);
0060     unsigned int tcer;
0061 
0062     regmap_read(drvdata->map, TCU_REG_WDT_TCER, &tcer);
0063     regmap_write(drvdata->map, TCU_REG_WDT_TCER, 0);
0064 
0065     regmap_write(drvdata->map, TCU_REG_WDT_TDR, timeout_value);
0066     regmap_write(drvdata->map, TCU_REG_WDT_TCNT, 0);
0067 
0068     if (tcer & TCU_WDT_TCER_TCEN)
0069         regmap_write(drvdata->map, TCU_REG_WDT_TCER, TCU_WDT_TCER_TCEN);
0070 
0071     wdt_dev->timeout = new_timeout;
0072     return 0;
0073 }
0074 
0075 static int jz4740_wdt_start(struct watchdog_device *wdt_dev)
0076 {
0077     struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
0078     unsigned int tcer;
0079     int ret;
0080 
0081     ret = clk_prepare_enable(drvdata->clk);
0082     if (ret)
0083         return ret;
0084 
0085     regmap_read(drvdata->map, TCU_REG_WDT_TCER, &tcer);
0086 
0087     jz4740_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
0088 
0089     /* Start watchdog if it wasn't started already */
0090     if (!(tcer & TCU_WDT_TCER_TCEN))
0091         regmap_write(drvdata->map, TCU_REG_WDT_TCER, TCU_WDT_TCER_TCEN);
0092 
0093     return 0;
0094 }
0095 
0096 static int jz4740_wdt_stop(struct watchdog_device *wdt_dev)
0097 {
0098     struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
0099 
0100     regmap_write(drvdata->map, TCU_REG_WDT_TCER, 0);
0101     clk_disable_unprepare(drvdata->clk);
0102 
0103     return 0;
0104 }
0105 
0106 static int jz4740_wdt_restart(struct watchdog_device *wdt_dev,
0107                   unsigned long action, void *data)
0108 {
0109     wdt_dev->timeout = 0;
0110     jz4740_wdt_start(wdt_dev);
0111     return 0;
0112 }
0113 
0114 static const struct watchdog_info jz4740_wdt_info = {
0115     .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
0116     .identity = "jz4740 Watchdog",
0117 };
0118 
0119 static const struct watchdog_ops jz4740_wdt_ops = {
0120     .owner = THIS_MODULE,
0121     .start = jz4740_wdt_start,
0122     .stop = jz4740_wdt_stop,
0123     .ping = jz4740_wdt_ping,
0124     .set_timeout = jz4740_wdt_set_timeout,
0125     .restart = jz4740_wdt_restart,
0126 };
0127 
0128 #ifdef CONFIG_OF
0129 static const struct of_device_id jz4740_wdt_of_matches[] = {
0130     { .compatible = "ingenic,jz4740-watchdog", },
0131     { .compatible = "ingenic,jz4780-watchdog", },
0132     { /* sentinel */ }
0133 };
0134 MODULE_DEVICE_TABLE(of, jz4740_wdt_of_matches);
0135 #endif
0136 
0137 static int jz4740_wdt_probe(struct platform_device *pdev)
0138 {
0139     struct device *dev = &pdev->dev;
0140     struct jz4740_wdt_drvdata *drvdata;
0141     struct watchdog_device *jz4740_wdt;
0142     long rate;
0143     int ret;
0144 
0145     drvdata = devm_kzalloc(dev, sizeof(struct jz4740_wdt_drvdata),
0146                    GFP_KERNEL);
0147     if (!drvdata)
0148         return -ENOMEM;
0149 
0150     drvdata->clk = devm_clk_get(&pdev->dev, "wdt");
0151     if (IS_ERR(drvdata->clk)) {
0152         dev_err(&pdev->dev, "cannot find WDT clock\n");
0153         return PTR_ERR(drvdata->clk);
0154     }
0155 
0156     /* Set smallest clock possible */
0157     rate = clk_round_rate(drvdata->clk, 1);
0158     if (rate < 0)
0159         return rate;
0160 
0161     ret = clk_set_rate(drvdata->clk, rate);
0162     if (ret)
0163         return ret;
0164 
0165     drvdata->clk_rate = rate;
0166     jz4740_wdt = &drvdata->wdt;
0167     jz4740_wdt->info = &jz4740_wdt_info;
0168     jz4740_wdt->ops = &jz4740_wdt_ops;
0169     jz4740_wdt->min_timeout = 1;
0170     jz4740_wdt->max_timeout = 0xffff / rate;
0171     jz4740_wdt->timeout = clamp(heartbeat,
0172                     jz4740_wdt->min_timeout,
0173                     jz4740_wdt->max_timeout);
0174     jz4740_wdt->parent = dev;
0175     watchdog_set_nowayout(jz4740_wdt, nowayout);
0176     watchdog_set_drvdata(jz4740_wdt, drvdata);
0177 
0178     drvdata->map = device_node_to_regmap(dev->parent->of_node);
0179     if (IS_ERR(drvdata->map)) {
0180         dev_err(dev, "regmap not found\n");
0181         return PTR_ERR(drvdata->map);
0182     }
0183 
0184     return devm_watchdog_register_device(dev, &drvdata->wdt);
0185 }
0186 
0187 static struct platform_driver jz4740_wdt_driver = {
0188     .probe = jz4740_wdt_probe,
0189     .driver = {
0190         .name = "jz4740-wdt",
0191         .of_match_table = of_match_ptr(jz4740_wdt_of_matches),
0192     },
0193 };
0194 
0195 module_platform_driver(jz4740_wdt_driver);
0196 
0197 MODULE_AUTHOR("Paul Cercueil <paul@crapouillou.net>");
0198 MODULE_DESCRIPTION("jz4740 Watchdog Driver");
0199 MODULE_LICENSE("GPL");
0200 MODULE_ALIAS("platform:jz4740-wdt");