Blame view

drivers/watchdog/txx9wdt.c 4.35 KB
d2912cb15   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
2
3
4
5
  /*
   * txx9wdt: A Hardware Watchdog Driver for TXx9 SoCs
   *
   * Copyright (C) 2007 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
6
   */
27c766aaa   Joe Perches   watchdog: Use pr_...
7
8
  
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
9
10
11
  #include <linux/module.h>
  #include <linux/moduleparam.h>
  #include <linux/types.h>
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
12
  #include <linux/watchdog.h>
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
13
  #include <linux/init.h>
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
14
15
16
17
18
  #include <linux/platform_device.h>
  #include <linux/clk.h>
  #include <linux/err.h>
  #include <linux/io.h>
  #include <asm/txx9tmr.h>
b92c803ec   Wim Van Sebroeck   watchdog: txx9wdt...
19
20
21
  #define WD_TIMER_CCD	7		/* 1/256 */
  #define WD_TIMER_CLK	(clk_get_rate(txx9_imclk) / (2 << WD_TIMER_CCD))
  #define WD_MAX_TIMEOUT	((0xffffffff >> (32 - TXX9_TIMER_BITS)) / WD_TIMER_CLK)
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
22
  #define TIMER_MARGIN	60		/* Default is 60 seconds */
b92c803ec   Wim Van Sebroeck   watchdog: txx9wdt...
23
24
  static unsigned int timeout = TIMER_MARGIN;	/* in seconds */
  module_param(timeout, uint, 0);
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
25
26
27
28
  MODULE_PARM_DESC(timeout,
  	"Watchdog timeout in seconds. "
  	"(0<timeout<((2^" __MODULE_STRING(TXX9_TIMER_BITS) ")/(IMCLK/256)), "
  	"default=" __MODULE_STRING(TIMER_MARGIN) ")");
86a1e1896   Wim Van Sebroeck   watchdog: nowayou...
29
30
  static bool nowayout = WATCHDOG_NOWAYOUT;
  module_param(nowayout, bool, 0);
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
31
32
33
  MODULE_PARM_DESC(nowayout,
  	"Watchdog cannot be stopped once started "
  	"(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
34
35
  static struct txx9_tmr_reg __iomem *txx9wdt_reg;
  static struct clk *txx9_imclk;
f8494e061   Adrian Bunk   [WATCHDOG] fix wa...
36
  static DEFINE_SPINLOCK(txx9_lock);
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
37

d62458423   Axel Lin   watchdog: Convert...
38
  static int txx9wdt_ping(struct watchdog_device *wdt_dev)
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
39
  {
8dc244f7d   Alan Cox   [WATCHDOG 48/57] ...
40
  	spin_lock(&txx9_lock);
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
41
  	__raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr);
8dc244f7d   Alan Cox   [WATCHDOG 48/57] ...
42
  	spin_unlock(&txx9_lock);
d62458423   Axel Lin   watchdog: Convert...
43
  	return 0;
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
44
  }
d62458423   Axel Lin   watchdog: Convert...
45
  static int txx9wdt_start(struct watchdog_device *wdt_dev)
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
46
  {
8dc244f7d   Alan Cox   [WATCHDOG 48/57] ...
47
  	spin_lock(&txx9_lock);
b92c803ec   Wim Van Sebroeck   watchdog: txx9wdt...
48
  	__raw_writel(WD_TIMER_CLK * wdt_dev->timeout, &txx9wdt_reg->cpra);
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
49
50
51
52
53
  	__raw_writel(WD_TIMER_CCD, &txx9wdt_reg->ccdr);
  	__raw_writel(0, &txx9wdt_reg->tisr);	/* clear pending interrupt */
  	__raw_writel(TXx9_TMTCR_TCE | TXx9_TMTCR_CCDE | TXx9_TMTCR_TMODE_WDOG,
  		     &txx9wdt_reg->tcr);
  	__raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr);
8dc244f7d   Alan Cox   [WATCHDOG 48/57] ...
54
  	spin_unlock(&txx9_lock);
d62458423   Axel Lin   watchdog: Convert...
55
  	return 0;
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
56
  }
d62458423   Axel Lin   watchdog: Convert...
57
  static int txx9wdt_stop(struct watchdog_device *wdt_dev)
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
58
  {
8dc244f7d   Alan Cox   [WATCHDOG 48/57] ...
59
  	spin_lock(&txx9_lock);
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
60
61
62
  	__raw_writel(TXx9_TMWTMR_WDIS, &txx9wdt_reg->wtmr);
  	__raw_writel(__raw_readl(&txx9wdt_reg->tcr) & ~TXx9_TMTCR_TCE,
  		     &txx9wdt_reg->tcr);
8dc244f7d   Alan Cox   [WATCHDOG 48/57] ...
63
  	spin_unlock(&txx9_lock);
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
64
65
  	return 0;
  }
d62458423   Axel Lin   watchdog: Convert...
66
67
  static int txx9wdt_set_timeout(struct watchdog_device *wdt_dev,
  			       unsigned int new_timeout)
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
68
  {
b92c803ec   Wim Van Sebroeck   watchdog: txx9wdt...
69
  	wdt_dev->timeout = new_timeout;
d62458423   Axel Lin   watchdog: Convert...
70
71
72
  	txx9wdt_stop(wdt_dev);
  	txx9wdt_start(wdt_dev);
  	return 0;
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
73
  }
d62458423   Axel Lin   watchdog: Convert...
74
75
76
77
  static const struct watchdog_info txx9wdt_info = {
  	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  	.identity = "Hardware Watchdog for TXx9",
  };
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
78

d62458423   Axel Lin   watchdog: Convert...
79
80
81
82
83
84
  static const struct watchdog_ops txx9wdt_ops = {
  	.owner = THIS_MODULE,
  	.start = txx9wdt_start,
  	.stop = txx9wdt_stop,
  	.ping = txx9wdt_ping,
  	.set_timeout = txx9wdt_set_timeout,
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
85
  };
d62458423   Axel Lin   watchdog: Convert...
86
87
88
  static struct watchdog_device txx9wdt = {
  	.info = &txx9wdt_info,
  	.ops = &txx9wdt_ops,
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
89
  };
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
90
91
  static int __init txx9wdt_probe(struct platform_device *dev)
  {
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
92
93
94
95
96
97
98
99
  	int ret;
  
  	txx9_imclk = clk_get(NULL, "imbus_clk");
  	if (IS_ERR(txx9_imclk)) {
  		ret = PTR_ERR(txx9_imclk);
  		txx9_imclk = NULL;
  		goto exit;
  	}
bfb1f46f6   Geert Uytterhoeven   watchdog: txx9wdt...
100
  	ret = clk_prepare_enable(txx9_imclk);
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
101
102
103
104
105
  	if (ret) {
  		clk_put(txx9_imclk);
  		txx9_imclk = NULL;
  		goto exit;
  	}
0f0a6a285   Guenter Roeck   watchdog: Convert...
106
  	txx9wdt_reg = devm_platform_ioremap_resource(dev, 0);
4c271bb67   Thierry Reding   watchdog: Convert...
107
108
  	if (IS_ERR(txx9wdt_reg)) {
  		ret = PTR_ERR(txx9wdt_reg);
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
109
110
  		goto exit;
  	}
b92c803ec   Wim Van Sebroeck   watchdog: txx9wdt...
111
112
113
  	if (timeout < 1 || timeout > WD_MAX_TIMEOUT)
  		timeout = TIMER_MARGIN;
  	txx9wdt.timeout = timeout;
d62458423   Axel Lin   watchdog: Convert...
114
115
  	txx9wdt.min_timeout = 1;
  	txx9wdt.max_timeout = WD_MAX_TIMEOUT;
6551881c8   Pratyush Anand   Watchdog: Fix par...
116
  	txx9wdt.parent = &dev->dev;
d62458423   Axel Lin   watchdog: Convert...
117
118
119
120
121
  	watchdog_set_nowayout(&txx9wdt, nowayout);
  
  	ret = watchdog_register_device(&txx9wdt);
  	if (ret)
  		goto exit;
27c766aaa   Joe Perches   watchdog: Use pr_...
122
123
124
  	pr_info("Hardware Watchdog Timer: timeout=%d sec (max %ld) (nowayout= %d)
  ",
  		timeout, WD_MAX_TIMEOUT, nowayout);
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
125
126
  
  	return 0;
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
127
128
  exit:
  	if (txx9_imclk) {
bfb1f46f6   Geert Uytterhoeven   watchdog: txx9wdt...
129
  		clk_disable_unprepare(txx9_imclk);
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
130
131
132
133
134
135
136
  		clk_put(txx9_imclk);
  	}
  	return ret;
  }
  
  static int __exit txx9wdt_remove(struct platform_device *dev)
  {
d62458423   Axel Lin   watchdog: Convert...
137
  	watchdog_unregister_device(&txx9wdt);
bfb1f46f6   Geert Uytterhoeven   watchdog: txx9wdt...
138
  	clk_disable_unprepare(txx9_imclk);
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
139
140
141
  	clk_put(txx9_imclk);
  	return 0;
  }
168b5251a   Wim Van Sebroeck   [WATCHDOG] change...
142
143
  static void txx9wdt_shutdown(struct platform_device *dev)
  {
d62458423   Axel Lin   watchdog: Convert...
144
  	txx9wdt_stop(&txx9wdt);
168b5251a   Wim Van Sebroeck   [WATCHDOG] change...
145
  }
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
146
147
  static struct platform_driver txx9wdt_driver = {
  	.remove = __exit_p(txx9wdt_remove),
168b5251a   Wim Van Sebroeck   [WATCHDOG] change...
148
  	.shutdown = txx9wdt_shutdown,
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
149
150
  	.driver = {
  		.name = "txx9wdt",
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
151
152
  	},
  };
1cb9204cc   Fabio Porcedda   watchdog: convert...
153
  module_platform_driver_probe(txx9wdt_driver, txx9wdt_probe);
6f702fce3   Atsushi Nemoto   [WATCHDOG] TXx9 w...
154
155
156
  
  MODULE_DESCRIPTION("TXx9 Watchdog Driver");
  MODULE_LICENSE("GPL");
f37d193c7   Kay Sievers   watchdog: fix pla...
157
  MODULE_ALIAS("platform:txx9wdt");