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