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