blob: 89a54b6645bd04a848549168fad1a0a99c94cb8f [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +09002/*
3 * txx9wdt: A Hardware Watchdog Driver for TXx9 SoCs
4 *
5 * Copyright (C) 2007 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +09006 */
Joe Perches27c766a2012-02-15 15:06:19 -08007
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090010#include <linux/module.h>
11#include <linux/moduleparam.h>
12#include <linux/types.h>
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090013#include <linux/watchdog.h>
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090014#include <linux/init.h>
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090015#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 Sebroeckb92c8032012-03-23 11:48:22 +010021#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 Nemoto6f702fc2007-11-12 01:32:17 +090024#define TIMER_MARGIN 60 /* Default is 60 seconds */
25
Wim Van Sebroeckb92c8032012-03-23 11:48:22 +010026static unsigned int timeout = TIMER_MARGIN; /* in seconds */
27module_param(timeout, uint, 0);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090028MODULE_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 Sebroeck86a1e182012-03-05 16:51:11 +010033static bool nowayout = WATCHDOG_NOWAYOUT;
34module_param(nowayout, bool, 0);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090035MODULE_PARM_DESC(nowayout,
36 "Watchdog cannot be stopped once started "
37 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
38
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090039static struct txx9_tmr_reg __iomem *txx9wdt_reg;
40static struct clk *txx9_imclk;
Adrian Bunkf8494e02008-08-08 18:18:46 +030041static DEFINE_SPINLOCK(txx9_lock);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090042
Axel Lind6245842012-03-16 11:53:53 +080043static int txx9wdt_ping(struct watchdog_device *wdt_dev)
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090044{
Alan Cox8dc244f2008-05-19 14:09:12 +010045 spin_lock(&txx9_lock);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090046 __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr);
Alan Cox8dc244f2008-05-19 14:09:12 +010047 spin_unlock(&txx9_lock);
Axel Lind6245842012-03-16 11:53:53 +080048 return 0;
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090049}
50
Axel Lind6245842012-03-16 11:53:53 +080051static int txx9wdt_start(struct watchdog_device *wdt_dev)
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090052{
Alan Cox8dc244f2008-05-19 14:09:12 +010053 spin_lock(&txx9_lock);
Wim Van Sebroeckb92c8032012-03-23 11:48:22 +010054 __raw_writel(WD_TIMER_CLK * wdt_dev->timeout, &txx9wdt_reg->cpra);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090055 __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 Cox8dc244f2008-05-19 14:09:12 +010060 spin_unlock(&txx9_lock);
Axel Lind6245842012-03-16 11:53:53 +080061 return 0;
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090062}
63
Axel Lind6245842012-03-16 11:53:53 +080064static int txx9wdt_stop(struct watchdog_device *wdt_dev)
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090065{
Alan Cox8dc244f2008-05-19 14:09:12 +010066 spin_lock(&txx9_lock);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090067 __raw_writel(TXx9_TMWTMR_WDIS, &txx9wdt_reg->wtmr);
68 __raw_writel(__raw_readl(&txx9wdt_reg->tcr) & ~TXx9_TMTCR_TCE,
69 &txx9wdt_reg->tcr);
Alan Cox8dc244f2008-05-19 14:09:12 +010070 spin_unlock(&txx9_lock);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090071 return 0;
72}
73
Axel Lind6245842012-03-16 11:53:53 +080074static int txx9wdt_set_timeout(struct watchdog_device *wdt_dev,
75 unsigned int new_timeout)
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090076{
Wim Van Sebroeckb92c8032012-03-23 11:48:22 +010077 wdt_dev->timeout = new_timeout;
Axel Lind6245842012-03-16 11:53:53 +080078 txx9wdt_stop(wdt_dev);
79 txx9wdt_start(wdt_dev);
80 return 0;
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090081}
82
Axel Lind6245842012-03-16 11:53:53 +080083static const struct watchdog_info txx9wdt_info = {
84 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
85 .identity = "Hardware Watchdog for TXx9",
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090086};
87
Axel Lind6245842012-03-16 11:53:53 +080088static 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
96static struct watchdog_device txx9wdt = {
97 .info = &txx9wdt_info,
98 .ops = &txx9wdt_ops,
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090099};
100
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900101static int __init txx9wdt_probe(struct platform_device *dev)
102{
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900103 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 Uytterhoevenbfb1f462016-09-11 10:59:57 +0200111 ret = clk_prepare_enable(txx9_imclk);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900112 if (ret) {
113 clk_put(txx9_imclk);
114 txx9_imclk = NULL;
115 goto exit;
116 }
117
Guenter Roeck0f0a6a22019-04-02 12:01:53 -0700118 txx9wdt_reg = devm_platform_ioremap_resource(dev, 0);
Thierry Reding4c271bb2013-01-21 11:09:25 +0100119 if (IS_ERR(txx9wdt_reg)) {
120 ret = PTR_ERR(txx9wdt_reg);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900121 goto exit;
122 }
123
Wim Van Sebroeckb92c8032012-03-23 11:48:22 +0100124 if (timeout < 1 || timeout > WD_MAX_TIMEOUT)
125 timeout = TIMER_MARGIN;
126 txx9wdt.timeout = timeout;
Axel Lind6245842012-03-16 11:53:53 +0800127 txx9wdt.min_timeout = 1;
128 txx9wdt.max_timeout = WD_MAX_TIMEOUT;
Pratyush Anand65518812015-08-20 14:05:01 +0530129 txx9wdt.parent = &dev->dev;
Axel Lind6245842012-03-16 11:53:53 +0800130 watchdog_set_nowayout(&txx9wdt, nowayout);
131
132 ret = watchdog_register_device(&txx9wdt);
133 if (ret)
134 goto exit;
135
Joe Perches27c766a2012-02-15 15:06:19 -0800136 pr_info("Hardware Watchdog Timer: timeout=%d sec (max %ld) (nowayout= %d)\n",
137 timeout, WD_MAX_TIMEOUT, nowayout);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900138
139 return 0;
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900140exit:
141 if (txx9_imclk) {
Geert Uytterhoevenbfb1f462016-09-11 10:59:57 +0200142 clk_disable_unprepare(txx9_imclk);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900143 clk_put(txx9_imclk);
144 }
145 return ret;
146}
147
148static int __exit txx9wdt_remove(struct platform_device *dev)
149{
Axel Lind6245842012-03-16 11:53:53 +0800150 watchdog_unregister_device(&txx9wdt);
Geert Uytterhoevenbfb1f462016-09-11 10:59:57 +0200151 clk_disable_unprepare(txx9_imclk);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900152 clk_put(txx9_imclk);
153 return 0;
154}
155
Wim Van Sebroeck168b5252009-12-26 19:13:00 +0000156static void txx9wdt_shutdown(struct platform_device *dev)
157{
Axel Lind6245842012-03-16 11:53:53 +0800158 txx9wdt_stop(&txx9wdt);
Wim Van Sebroeck168b5252009-12-26 19:13:00 +0000159}
160
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900161static struct platform_driver txx9wdt_driver = {
162 .remove = __exit_p(txx9wdt_remove),
Wim Van Sebroeck168b5252009-12-26 19:13:00 +0000163 .shutdown = txx9wdt_shutdown,
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900164 .driver = {
165 .name = "txx9wdt",
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900166 },
167};
168
Fabio Porcedda1cb92042013-01-09 12:15:27 +0100169module_platform_driver_probe(txx9wdt_driver, txx9wdt_probe);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900170
171MODULE_DESCRIPTION("TXx9 Watchdog Driver");
172MODULE_LICENSE("GPL");
Kay Sieversf37d1932008-04-10 21:29:23 -0700173MODULE_ALIAS("platform:txx9wdt");