Thomas Gleixner | a912e80 | 2019-05-27 08:55:00 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2010, Paul Cercueil <paul@crapouillou.net> |
| 4 | * JZ4740 Watchdog driver |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Paul Cercueil | df04cce | 2019-06-07 18:24:26 +0200 | [diff] [blame] | 7 | #include <linux/mfd/ingenic-tcu.h> |
Paul Cercueil | 6d53214 | 2019-10-23 19:47:13 +0200 | [diff] [blame] | 8 | #include <linux/mfd/syscon.h> |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 9 | #include <linux/module.h> |
| 10 | #include <linux/moduleparam.h> |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/kernel.h> |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 13 | #include <linux/watchdog.h> |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 14 | #include <linux/platform_device.h> |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 15 | #include <linux/io.h> |
| 16 | #include <linux/device.h> |
| 17 | #include <linux/clk.h> |
| 18 | #include <linux/slab.h> |
Axel Lin | 85f6df1 | 2012-01-26 18:10:45 +0800 | [diff] [blame] | 19 | #include <linux/err.h> |
Zubair Lutfullah Kakakhel | 6b96c72 | 2015-02-03 10:25:48 +0000 | [diff] [blame] | 20 | #include <linux/of.h> |
Paul Cercueil | 6d53214 | 2019-10-23 19:47:13 +0200 | [diff] [blame] | 21 | #include <linux/regmap.h> |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 22 | |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 23 | #define DEFAULT_HEARTBEAT 5 |
| 24 | #define MAX_HEARTBEAT 2048 |
| 25 | |
Axel Lin | 85f6df1 | 2012-01-26 18:10:45 +0800 | [diff] [blame] | 26 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 27 | module_param(nowayout, bool, 0); |
| 28 | MODULE_PARM_DESC(nowayout, |
| 29 | "Watchdog cannot be stopped once started (default=" |
| 30 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 31 | |
| 32 | static unsigned int heartbeat = DEFAULT_HEARTBEAT; |
| 33 | module_param(heartbeat, uint, 0); |
| 34 | MODULE_PARM_DESC(heartbeat, |
| 35 | "Watchdog heartbeat period in seconds from 1 to " |
| 36 | __MODULE_STRING(MAX_HEARTBEAT) ", default " |
| 37 | __MODULE_STRING(DEFAULT_HEARTBEAT)); |
| 38 | |
| 39 | struct jz4740_wdt_drvdata { |
| 40 | struct watchdog_device wdt; |
Paul Cercueil | 6d53214 | 2019-10-23 19:47:13 +0200 | [diff] [blame] | 41 | struct regmap *map; |
Paul Cercueil | 1d9c307 | 2019-10-23 19:47:12 +0200 | [diff] [blame] | 42 | struct clk *clk; |
| 43 | unsigned long clk_rate; |
Axel Lin | 85f6df1 | 2012-01-26 18:10:45 +0800 | [diff] [blame] | 44 | }; |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 45 | |
Axel Lin | 85f6df1 | 2012-01-26 18:10:45 +0800 | [diff] [blame] | 46 | static int jz4740_wdt_ping(struct watchdog_device *wdt_dev) |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 47 | { |
Axel Lin | 85f6df1 | 2012-01-26 18:10:45 +0800 | [diff] [blame] | 48 | struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev); |
| 49 | |
Paul Cercueil | 6d53214 | 2019-10-23 19:47:13 +0200 | [diff] [blame] | 50 | regmap_write(drvdata->map, TCU_REG_WDT_TCNT, 0); |
| 51 | |
Axel Lin | 85f6df1 | 2012-01-26 18:10:45 +0800 | [diff] [blame] | 52 | return 0; |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Axel Lin | 85f6df1 | 2012-01-26 18:10:45 +0800 | [diff] [blame] | 55 | static int jz4740_wdt_set_timeout(struct watchdog_device *wdt_dev, |
| 56 | unsigned int new_timeout) |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 57 | { |
Axel Lin | 85f6df1 | 2012-01-26 18:10:45 +0800 | [diff] [blame] | 58 | struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev); |
Paul Cercueil | 1d9c307 | 2019-10-23 19:47:12 +0200 | [diff] [blame] | 59 | u16 timeout_value = (u16)(drvdata->clk_rate * new_timeout); |
Paul Cercueil | 6d53214 | 2019-10-23 19:47:13 +0200 | [diff] [blame] | 60 | unsigned int tcer; |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 61 | |
Paul Cercueil | 6d53214 | 2019-10-23 19:47:13 +0200 | [diff] [blame] | 62 | regmap_read(drvdata->map, TCU_REG_WDT_TCER, &tcer); |
| 63 | regmap_write(drvdata->map, TCU_REG_WDT_TCER, 0); |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 64 | |
Paul Cercueil | 6d53214 | 2019-10-23 19:47:13 +0200 | [diff] [blame] | 65 | regmap_write(drvdata->map, TCU_REG_WDT_TDR, timeout_value); |
| 66 | regmap_write(drvdata->map, TCU_REG_WDT_TCNT, 0); |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 67 | |
Paul Cercueil | 9b34611 | 2019-06-07 18:24:27 +0200 | [diff] [blame] | 68 | if (tcer & TCU_WDT_TCER_TCEN) |
Paul Cercueil | 6d53214 | 2019-10-23 19:47:13 +0200 | [diff] [blame] | 69 | regmap_write(drvdata->map, TCU_REG_WDT_TCER, TCU_WDT_TCER_TCEN); |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 70 | |
Wim Van Sebroeck | 0197c1c | 2012-02-29 20:20:58 +0100 | [diff] [blame] | 71 | wdt_dev->timeout = new_timeout; |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 72 | return 0; |
| 73 | } |
| 74 | |
Axel Lin | 85f6df1 | 2012-01-26 18:10:45 +0800 | [diff] [blame] | 75 | static int jz4740_wdt_start(struct watchdog_device *wdt_dev) |
| 76 | { |
Paul Cercueil | 9b34611 | 2019-06-07 18:24:27 +0200 | [diff] [blame] | 77 | struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev); |
Paul Cercueil | 6d53214 | 2019-10-23 19:47:13 +0200 | [diff] [blame] | 78 | unsigned int tcer; |
Paul Cercueil | 1d9c307 | 2019-10-23 19:47:12 +0200 | [diff] [blame] | 79 | int ret; |
Paul Cercueil | 9b34611 | 2019-06-07 18:24:27 +0200 | [diff] [blame] | 80 | |
Paul Cercueil | 1d9c307 | 2019-10-23 19:47:12 +0200 | [diff] [blame] | 81 | ret = clk_prepare_enable(drvdata->clk); |
| 82 | if (ret) |
| 83 | return ret; |
| 84 | |
Paul Cercueil | 6d53214 | 2019-10-23 19:47:13 +0200 | [diff] [blame] | 85 | regmap_read(drvdata->map, TCU_REG_WDT_TCER, &tcer); |
Paul Cercueil | 9b34611 | 2019-06-07 18:24:27 +0200 | [diff] [blame] | 86 | |
Axel Lin | 85f6df1 | 2012-01-26 18:10:45 +0800 | [diff] [blame] | 87 | jz4740_wdt_set_timeout(wdt_dev, wdt_dev->timeout); |
| 88 | |
Paul Cercueil | 9b34611 | 2019-06-07 18:24:27 +0200 | [diff] [blame] | 89 | /* Start watchdog if it wasn't started already */ |
| 90 | if (!(tcer & TCU_WDT_TCER_TCEN)) |
Paul Cercueil | 6d53214 | 2019-10-23 19:47:13 +0200 | [diff] [blame] | 91 | regmap_write(drvdata->map, TCU_REG_WDT_TCER, TCU_WDT_TCER_TCEN); |
Paul Cercueil | 9b34611 | 2019-06-07 18:24:27 +0200 | [diff] [blame] | 92 | |
Axel Lin | 85f6df1 | 2012-01-26 18:10:45 +0800 | [diff] [blame] | 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | static int jz4740_wdt_stop(struct watchdog_device *wdt_dev) |
| 97 | { |
| 98 | struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev); |
| 99 | |
Paul Cercueil | 6d53214 | 2019-10-23 19:47:13 +0200 | [diff] [blame] | 100 | regmap_write(drvdata->map, TCU_REG_WDT_TCER, 0); |
Paul Cercueil | 1d9c307 | 2019-10-23 19:47:12 +0200 | [diff] [blame] | 101 | clk_disable_unprepare(drvdata->clk); |
Axel Lin | 85f6df1 | 2012-01-26 18:10:45 +0800 | [diff] [blame] | 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
Paul Cercueil | b491805 | 2018-05-10 20:47:46 +0200 | [diff] [blame] | 106 | static int jz4740_wdt_restart(struct watchdog_device *wdt_dev, |
| 107 | unsigned long action, void *data) |
| 108 | { |
| 109 | wdt_dev->timeout = 0; |
| 110 | jz4740_wdt_start(wdt_dev); |
| 111 | return 0; |
| 112 | } |
| 113 | |
Axel Lin | 85f6df1 | 2012-01-26 18:10:45 +0800 | [diff] [blame] | 114 | static const struct watchdog_info jz4740_wdt_info = { |
| 115 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE, |
| 116 | .identity = "jz4740 Watchdog", |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 117 | }; |
| 118 | |
Axel Lin | 85f6df1 | 2012-01-26 18:10:45 +0800 | [diff] [blame] | 119 | static const struct watchdog_ops jz4740_wdt_ops = { |
| 120 | .owner = THIS_MODULE, |
| 121 | .start = jz4740_wdt_start, |
| 122 | .stop = jz4740_wdt_stop, |
| 123 | .ping = jz4740_wdt_ping, |
| 124 | .set_timeout = jz4740_wdt_set_timeout, |
Paul Cercueil | b491805 | 2018-05-10 20:47:46 +0200 | [diff] [blame] | 125 | .restart = jz4740_wdt_restart, |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 126 | }; |
| 127 | |
Zubair Lutfullah Kakakhel | 6b96c72 | 2015-02-03 10:25:48 +0000 | [diff] [blame] | 128 | #ifdef CONFIG_OF |
| 129 | static const struct of_device_id jz4740_wdt_of_matches[] = { |
| 130 | { .compatible = "ingenic,jz4740-watchdog", }, |
Mathieu Malaterre | 71246c3 | 2017-09-15 21:20:19 +0200 | [diff] [blame] | 131 | { .compatible = "ingenic,jz4780-watchdog", }, |
Zubair Lutfullah Kakakhel | 6b96c72 | 2015-02-03 10:25:48 +0000 | [diff] [blame] | 132 | { /* sentinel */ } |
| 133 | }; |
Stephen Boyd | 35ffa96 | 2016-11-10 16:02:20 -0800 | [diff] [blame] | 134 | MODULE_DEVICE_TABLE(of, jz4740_wdt_of_matches); |
Zubair Lutfullah Kakakhel | 6b96c72 | 2015-02-03 10:25:48 +0000 | [diff] [blame] | 135 | #endif |
| 136 | |
Bill Pemberton | 2d991a1 | 2012-11-19 13:21:41 -0500 | [diff] [blame] | 137 | static int jz4740_wdt_probe(struct platform_device *pdev) |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 138 | { |
Guenter Roeck | 02189bb | 2019-04-10 09:28:01 -0700 | [diff] [blame] | 139 | struct device *dev = &pdev->dev; |
Axel Lin | 85f6df1 | 2012-01-26 18:10:45 +0800 | [diff] [blame] | 140 | struct jz4740_wdt_drvdata *drvdata; |
| 141 | struct watchdog_device *jz4740_wdt; |
Paul Cercueil | 1d9c307 | 2019-10-23 19:47:12 +0200 | [diff] [blame] | 142 | long rate; |
| 143 | int ret; |
Axel Lin | 85f6df1 | 2012-01-26 18:10:45 +0800 | [diff] [blame] | 144 | |
Guenter Roeck | 02189bb | 2019-04-10 09:28:01 -0700 | [diff] [blame] | 145 | drvdata = devm_kzalloc(dev, sizeof(struct jz4740_wdt_drvdata), |
Axel Lin | 85f6df1 | 2012-01-26 18:10:45 +0800 | [diff] [blame] | 146 | GFP_KERNEL); |
Colin Ian King | e26e74b | 2016-04-26 18:18:49 +0100 | [diff] [blame] | 147 | if (!drvdata) |
Axel Lin | 85f6df1 | 2012-01-26 18:10:45 +0800 | [diff] [blame] | 148 | return -ENOMEM; |
Axel Lin | 85f6df1 | 2012-01-26 18:10:45 +0800 | [diff] [blame] | 149 | |
Paul Cercueil | 1d9c307 | 2019-10-23 19:47:12 +0200 | [diff] [blame] | 150 | drvdata->clk = devm_clk_get(&pdev->dev, "wdt"); |
| 151 | if (IS_ERR(drvdata->clk)) { |
| 152 | dev_err(&pdev->dev, "cannot find WDT clock\n"); |
| 153 | return PTR_ERR(drvdata->clk); |
| 154 | } |
Axel Lin | 85f6df1 | 2012-01-26 18:10:45 +0800 | [diff] [blame] | 155 | |
Paul Cercueil | 1d9c307 | 2019-10-23 19:47:12 +0200 | [diff] [blame] | 156 | /* Set smallest clock possible */ |
| 157 | rate = clk_round_rate(drvdata->clk, 1); |
| 158 | if (rate < 0) |
| 159 | return rate; |
| 160 | |
| 161 | ret = clk_set_rate(drvdata->clk, rate); |
| 162 | if (ret) |
| 163 | return ret; |
| 164 | |
| 165 | drvdata->clk_rate = rate; |
Axel Lin | 85f6df1 | 2012-01-26 18:10:45 +0800 | [diff] [blame] | 166 | jz4740_wdt = &drvdata->wdt; |
| 167 | jz4740_wdt->info = &jz4740_wdt_info; |
| 168 | jz4740_wdt->ops = &jz4740_wdt_ops; |
Axel Lin | 85f6df1 | 2012-01-26 18:10:45 +0800 | [diff] [blame] | 169 | jz4740_wdt->min_timeout = 1; |
Paul Cercueil | 1d9c307 | 2019-10-23 19:47:12 +0200 | [diff] [blame] | 170 | jz4740_wdt->max_timeout = 0xffff / rate; |
| 171 | jz4740_wdt->timeout = clamp(heartbeat, |
| 172 | jz4740_wdt->min_timeout, |
| 173 | jz4740_wdt->max_timeout); |
Guenter Roeck | 02189bb | 2019-04-10 09:28:01 -0700 | [diff] [blame] | 174 | jz4740_wdt->parent = dev; |
Axel Lin | 85f6df1 | 2012-01-26 18:10:45 +0800 | [diff] [blame] | 175 | watchdog_set_nowayout(jz4740_wdt, nowayout); |
| 176 | watchdog_set_drvdata(jz4740_wdt, drvdata); |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 177 | |
Paul Cercueil | 6d53214 | 2019-10-23 19:47:13 +0200 | [diff] [blame] | 178 | drvdata->map = device_node_to_regmap(dev->parent->of_node); |
| 179 | if (!drvdata->map) { |
| 180 | dev_err(dev, "regmap not found\n"); |
| 181 | return -EINVAL; |
| 182 | } |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 183 | |
Wolfram Sang | 9ee644c | 2019-05-18 23:27:34 +0200 | [diff] [blame] | 184 | return devm_watchdog_register_device(dev, &drvdata->wdt); |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 185 | } |
| 186 | |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 187 | static struct platform_driver jz4740_wdt_driver = { |
| 188 | .probe = jz4740_wdt_probe, |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 189 | .driver = { |
| 190 | .name = "jz4740-wdt", |
Zubair Lutfullah Kakakhel | 6b96c72 | 2015-02-03 10:25:48 +0000 | [diff] [blame] | 191 | .of_match_table = of_match_ptr(jz4740_wdt_of_matches), |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 192 | }, |
| 193 | }; |
| 194 | |
Axel Lin | b8ec611 | 2011-11-29 13:56:27 +0800 | [diff] [blame] | 195 | module_platform_driver(jz4740_wdt_driver); |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 196 | |
| 197 | MODULE_AUTHOR("Paul Cercueil <paul@crapouillou.net>"); |
| 198 | MODULE_DESCRIPTION("jz4740 Watchdog Driver"); |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 199 | MODULE_LICENSE("GPL"); |
Paul Cercueil | f865c35 | 2010-12-05 21:08:22 +0100 | [diff] [blame] | 200 | MODULE_ALIAS("platform:jz4740-wdt"); |