Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 2 | /* |
| 3 | * txx9wdt: A Hardware Watchdog Driver for TXx9 SoCs |
| 4 | * |
| 5 | * Copyright (C) 2007 Atsushi Nemoto <anemo@mba.ocn.ne.jp> |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 6 | */ |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 7 | |
| 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 9 | |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 10 | #include <linux/module.h> |
| 11 | #include <linux/moduleparam.h> |
| 12 | #include <linux/types.h> |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 13 | #include <linux/watchdog.h> |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 14 | #include <linux/init.h> |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/clk.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <asm/txx9tmr.h> |
| 20 | |
Wim Van Sebroeck | b92c803 | 2012-03-23 11:48:22 +0100 | [diff] [blame] | 21 | #define WD_TIMER_CCD 7 /* 1/256 */ |
| 22 | #define WD_TIMER_CLK (clk_get_rate(txx9_imclk) / (2 << WD_TIMER_CCD)) |
| 23 | #define WD_MAX_TIMEOUT ((0xffffffff >> (32 - TXX9_TIMER_BITS)) / WD_TIMER_CLK) |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 24 | #define TIMER_MARGIN 60 /* Default is 60 seconds */ |
| 25 | |
Wim Van Sebroeck | b92c803 | 2012-03-23 11:48:22 +0100 | [diff] [blame] | 26 | static unsigned int timeout = TIMER_MARGIN; /* in seconds */ |
| 27 | module_param(timeout, uint, 0); |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 28 | MODULE_PARM_DESC(timeout, |
| 29 | "Watchdog timeout in seconds. " |
| 30 | "(0<timeout<((2^" __MODULE_STRING(TXX9_TIMER_BITS) ")/(IMCLK/256)), " |
| 31 | "default=" __MODULE_STRING(TIMER_MARGIN) ")"); |
| 32 | |
Wim Van Sebroeck | 86a1e18 | 2012-03-05 16:51:11 +0100 | [diff] [blame] | 33 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 34 | module_param(nowayout, bool, 0); |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 35 | MODULE_PARM_DESC(nowayout, |
| 36 | "Watchdog cannot be stopped once started " |
| 37 | "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 38 | |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 39 | static struct txx9_tmr_reg __iomem *txx9wdt_reg; |
| 40 | static struct clk *txx9_imclk; |
Adrian Bunk | f8494e0 | 2008-08-08 18:18:46 +0300 | [diff] [blame] | 41 | static DEFINE_SPINLOCK(txx9_lock); |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 42 | |
Axel Lin | d624584 | 2012-03-16 11:53:53 +0800 | [diff] [blame] | 43 | static int txx9wdt_ping(struct watchdog_device *wdt_dev) |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 44 | { |
Alan Cox | 8dc244f | 2008-05-19 14:09:12 +0100 | [diff] [blame] | 45 | spin_lock(&txx9_lock); |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 46 | __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr); |
Alan Cox | 8dc244f | 2008-05-19 14:09:12 +0100 | [diff] [blame] | 47 | spin_unlock(&txx9_lock); |
Axel Lin | d624584 | 2012-03-16 11:53:53 +0800 | [diff] [blame] | 48 | return 0; |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 49 | } |
| 50 | |
Axel Lin | d624584 | 2012-03-16 11:53:53 +0800 | [diff] [blame] | 51 | static int txx9wdt_start(struct watchdog_device *wdt_dev) |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 52 | { |
Alan Cox | 8dc244f | 2008-05-19 14:09:12 +0100 | [diff] [blame] | 53 | spin_lock(&txx9_lock); |
Wim Van Sebroeck | b92c803 | 2012-03-23 11:48:22 +0100 | [diff] [blame] | 54 | __raw_writel(WD_TIMER_CLK * wdt_dev->timeout, &txx9wdt_reg->cpra); |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 55 | __raw_writel(WD_TIMER_CCD, &txx9wdt_reg->ccdr); |
| 56 | __raw_writel(0, &txx9wdt_reg->tisr); /* clear pending interrupt */ |
| 57 | __raw_writel(TXx9_TMTCR_TCE | TXx9_TMTCR_CCDE | TXx9_TMTCR_TMODE_WDOG, |
| 58 | &txx9wdt_reg->tcr); |
| 59 | __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr); |
Alan Cox | 8dc244f | 2008-05-19 14:09:12 +0100 | [diff] [blame] | 60 | spin_unlock(&txx9_lock); |
Axel Lin | d624584 | 2012-03-16 11:53:53 +0800 | [diff] [blame] | 61 | return 0; |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 62 | } |
| 63 | |
Axel Lin | d624584 | 2012-03-16 11:53:53 +0800 | [diff] [blame] | 64 | static int txx9wdt_stop(struct watchdog_device *wdt_dev) |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 65 | { |
Alan Cox | 8dc244f | 2008-05-19 14:09:12 +0100 | [diff] [blame] | 66 | spin_lock(&txx9_lock); |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 67 | __raw_writel(TXx9_TMWTMR_WDIS, &txx9wdt_reg->wtmr); |
| 68 | __raw_writel(__raw_readl(&txx9wdt_reg->tcr) & ~TXx9_TMTCR_TCE, |
| 69 | &txx9wdt_reg->tcr); |
Alan Cox | 8dc244f | 2008-05-19 14:09:12 +0100 | [diff] [blame] | 70 | spin_unlock(&txx9_lock); |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 71 | return 0; |
| 72 | } |
| 73 | |
Axel Lin | d624584 | 2012-03-16 11:53:53 +0800 | [diff] [blame] | 74 | static int txx9wdt_set_timeout(struct watchdog_device *wdt_dev, |
| 75 | unsigned int new_timeout) |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 76 | { |
Wim Van Sebroeck | b92c803 | 2012-03-23 11:48:22 +0100 | [diff] [blame] | 77 | wdt_dev->timeout = new_timeout; |
Axel Lin | d624584 | 2012-03-16 11:53:53 +0800 | [diff] [blame] | 78 | txx9wdt_stop(wdt_dev); |
| 79 | txx9wdt_start(wdt_dev); |
| 80 | return 0; |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 81 | } |
| 82 | |
Axel Lin | d624584 | 2012-03-16 11:53:53 +0800 | [diff] [blame] | 83 | static const struct watchdog_info txx9wdt_info = { |
| 84 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE, |
| 85 | .identity = "Hardware Watchdog for TXx9", |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 86 | }; |
| 87 | |
Axel Lin | d624584 | 2012-03-16 11:53:53 +0800 | [diff] [blame] | 88 | static const struct watchdog_ops txx9wdt_ops = { |
| 89 | .owner = THIS_MODULE, |
| 90 | .start = txx9wdt_start, |
| 91 | .stop = txx9wdt_stop, |
| 92 | .ping = txx9wdt_ping, |
| 93 | .set_timeout = txx9wdt_set_timeout, |
| 94 | }; |
| 95 | |
| 96 | static struct watchdog_device txx9wdt = { |
| 97 | .info = &txx9wdt_info, |
| 98 | .ops = &txx9wdt_ops, |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 99 | }; |
| 100 | |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 101 | static int __init txx9wdt_probe(struct platform_device *dev) |
| 102 | { |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 103 | int ret; |
| 104 | |
| 105 | txx9_imclk = clk_get(NULL, "imbus_clk"); |
| 106 | if (IS_ERR(txx9_imclk)) { |
| 107 | ret = PTR_ERR(txx9_imclk); |
| 108 | txx9_imclk = NULL; |
| 109 | goto exit; |
| 110 | } |
Geert Uytterhoeven | bfb1f46 | 2016-09-11 10:59:57 +0200 | [diff] [blame] | 111 | ret = clk_prepare_enable(txx9_imclk); |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 112 | if (ret) { |
| 113 | clk_put(txx9_imclk); |
| 114 | txx9_imclk = NULL; |
| 115 | goto exit; |
| 116 | } |
| 117 | |
Guenter Roeck | 0f0a6a2 | 2019-04-02 12:01:53 -0700 | [diff] [blame] | 118 | txx9wdt_reg = devm_platform_ioremap_resource(dev, 0); |
Thierry Reding | 4c271bb | 2013-01-21 11:09:25 +0100 | [diff] [blame] | 119 | if (IS_ERR(txx9wdt_reg)) { |
| 120 | ret = PTR_ERR(txx9wdt_reg); |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 121 | goto exit; |
| 122 | } |
| 123 | |
Wim Van Sebroeck | b92c803 | 2012-03-23 11:48:22 +0100 | [diff] [blame] | 124 | if (timeout < 1 || timeout > WD_MAX_TIMEOUT) |
| 125 | timeout = TIMER_MARGIN; |
| 126 | txx9wdt.timeout = timeout; |
Axel Lin | d624584 | 2012-03-16 11:53:53 +0800 | [diff] [blame] | 127 | txx9wdt.min_timeout = 1; |
| 128 | txx9wdt.max_timeout = WD_MAX_TIMEOUT; |
Pratyush Anand | 6551881 | 2015-08-20 14:05:01 +0530 | [diff] [blame] | 129 | txx9wdt.parent = &dev->dev; |
Axel Lin | d624584 | 2012-03-16 11:53:53 +0800 | [diff] [blame] | 130 | watchdog_set_nowayout(&txx9wdt, nowayout); |
| 131 | |
| 132 | ret = watchdog_register_device(&txx9wdt); |
| 133 | if (ret) |
| 134 | goto exit; |
| 135 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 136 | pr_info("Hardware Watchdog Timer: timeout=%d sec (max %ld) (nowayout= %d)\n", |
| 137 | timeout, WD_MAX_TIMEOUT, nowayout); |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 138 | |
| 139 | return 0; |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 140 | exit: |
| 141 | if (txx9_imclk) { |
Geert Uytterhoeven | bfb1f46 | 2016-09-11 10:59:57 +0200 | [diff] [blame] | 142 | clk_disable_unprepare(txx9_imclk); |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 143 | clk_put(txx9_imclk); |
| 144 | } |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | static int __exit txx9wdt_remove(struct platform_device *dev) |
| 149 | { |
Axel Lin | d624584 | 2012-03-16 11:53:53 +0800 | [diff] [blame] | 150 | watchdog_unregister_device(&txx9wdt); |
Geert Uytterhoeven | bfb1f46 | 2016-09-11 10:59:57 +0200 | [diff] [blame] | 151 | clk_disable_unprepare(txx9_imclk); |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 152 | clk_put(txx9_imclk); |
| 153 | return 0; |
| 154 | } |
| 155 | |
Wim Van Sebroeck | 168b525 | 2009-12-26 19:13:00 +0000 | [diff] [blame] | 156 | static void txx9wdt_shutdown(struct platform_device *dev) |
| 157 | { |
Axel Lin | d624584 | 2012-03-16 11:53:53 +0800 | [diff] [blame] | 158 | txx9wdt_stop(&txx9wdt); |
Wim Van Sebroeck | 168b525 | 2009-12-26 19:13:00 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 161 | static struct platform_driver txx9wdt_driver = { |
| 162 | .remove = __exit_p(txx9wdt_remove), |
Wim Van Sebroeck | 168b525 | 2009-12-26 19:13:00 +0000 | [diff] [blame] | 163 | .shutdown = txx9wdt_shutdown, |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 164 | .driver = { |
| 165 | .name = "txx9wdt", |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 166 | }, |
| 167 | }; |
| 168 | |
Fabio Porcedda | 1cb9204 | 2013-01-09 12:15:27 +0100 | [diff] [blame] | 169 | module_platform_driver_probe(txx9wdt_driver, txx9wdt_probe); |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 170 | |
| 171 | MODULE_DESCRIPTION("TXx9 Watchdog Driver"); |
| 172 | MODULE_LICENSE("GPL"); |
Kay Sievers | f37d193 | 2008-04-10 21:29:23 -0700 | [diff] [blame] | 173 | MODULE_ALIAS("platform:txx9wdt"); |