Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * txx9wdt: A Hardware Watchdog Driver for TXx9 SoCs
4 *
5 * Copyright (C) 2007 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
6 */
7
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10#include <linux/module.h>
11#include <linux/moduleparam.h>
12#include <linux/types.h>
13#include <linux/watchdog.h>
14#include <linux/init.h>
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
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)
24#define TIMER_MARGIN 60 /* Default is 60 seconds */
25
26static unsigned int timeout = TIMER_MARGIN; /* in seconds */
27module_param(timeout, uint, 0);
28MODULE_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
33static bool nowayout = WATCHDOG_NOWAYOUT;
34module_param(nowayout, bool, 0);
35MODULE_PARM_DESC(nowayout,
36 "Watchdog cannot be stopped once started "
37 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
38
39static struct txx9_tmr_reg __iomem *txx9wdt_reg;
40static struct clk *txx9_imclk;
41static DEFINE_SPINLOCK(txx9_lock);
42
43static int txx9wdt_ping(struct watchdog_device *wdt_dev)
44{
45 spin_lock(&txx9_lock);
46 __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr);
47 spin_unlock(&txx9_lock);
48 return 0;
49}
50
51static int txx9wdt_start(struct watchdog_device *wdt_dev)
52{
53 spin_lock(&txx9_lock);
54 __raw_writel(WD_TIMER_CLK * wdt_dev->timeout, &txx9wdt_reg->cpra);
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);
60 spin_unlock(&txx9_lock);
61 return 0;
62}
63
64static int txx9wdt_stop(struct watchdog_device *wdt_dev)
65{
66 spin_lock(&txx9_lock);
67 __raw_writel(TXx9_TMWTMR_WDIS, &txx9wdt_reg->wtmr);
68 __raw_writel(__raw_readl(&txx9wdt_reg->tcr) & ~TXx9_TMTCR_TCE,
69 &txx9wdt_reg->tcr);
70 spin_unlock(&txx9_lock);
71 return 0;
72}
73
74static int txx9wdt_set_timeout(struct watchdog_device *wdt_dev,
75 unsigned int new_timeout)
76{
77 wdt_dev->timeout = new_timeout;
78 txx9wdt_stop(wdt_dev);
79 txx9wdt_start(wdt_dev);
80 return 0;
81}
82
83static const struct watchdog_info txx9wdt_info = {
84 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
85 .identity = "Hardware Watchdog for TXx9",
86};
87
88static 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,
99};
100
101static int __init txx9wdt_probe(struct platform_device *dev)
102{
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 }
111 ret = clk_prepare_enable(txx9_imclk);
112 if (ret) {
113 clk_put(txx9_imclk);
114 txx9_imclk = NULL;
115 goto exit;
116 }
117
118 txx9wdt_reg = devm_platform_ioremap_resource(dev, 0);
119 if (IS_ERR(txx9wdt_reg)) {
120 ret = PTR_ERR(txx9wdt_reg);
121 goto exit;
122 }
123
124 if (timeout < 1 || timeout > WD_MAX_TIMEOUT)
125 timeout = TIMER_MARGIN;
126 txx9wdt.timeout = timeout;
127 txx9wdt.min_timeout = 1;
128 txx9wdt.max_timeout = WD_MAX_TIMEOUT;
129 txx9wdt.parent = &dev->dev;
130 watchdog_set_nowayout(&txx9wdt, nowayout);
131
132 ret = watchdog_register_device(&txx9wdt);
133 if (ret)
134 goto exit;
135
136 pr_info("Hardware Watchdog Timer: timeout=%d sec (max %ld) (nowayout= %d)\n",
137 timeout, WD_MAX_TIMEOUT, nowayout);
138
139 return 0;
140exit:
141 if (txx9_imclk) {
142 clk_disable_unprepare(txx9_imclk);
143 clk_put(txx9_imclk);
144 }
145 return ret;
146}
147
148static int __exit txx9wdt_remove(struct platform_device *dev)
149{
150 watchdog_unregister_device(&txx9wdt);
151 clk_disable_unprepare(txx9_imclk);
152 clk_put(txx9_imclk);
153 return 0;
154}
155
156static void txx9wdt_shutdown(struct platform_device *dev)
157{
158 txx9wdt_stop(&txx9wdt);
159}
160
161static struct platform_driver txx9wdt_driver = {
162 .remove = __exit_p(txx9wdt_remove),
163 .shutdown = txx9wdt_shutdown,
164 .driver = {
165 .name = "txx9wdt",
166 },
167};
168
169module_platform_driver_probe(txx9wdt_driver, txx9wdt_probe);
170
171MODULE_DESCRIPTION("TXx9 Watchdog Driver");
172MODULE_LICENSE("GPL");
173MODULE_ALIAS("platform:txx9wdt");
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 */
10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/types.h>
16#include <linux/watchdog.h>
17#include <linux/init.h>
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
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)
27#define TIMER_MARGIN 60 /* Default is 60 seconds */
28
29static unsigned int timeout = TIMER_MARGIN; /* in seconds */
30module_param(timeout, uint, 0);
31MODULE_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
36static bool nowayout = WATCHDOG_NOWAYOUT;
37module_param(nowayout, bool, 0);
38MODULE_PARM_DESC(nowayout,
39 "Watchdog cannot be stopped once started "
40 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
41
42static struct txx9_tmr_reg __iomem *txx9wdt_reg;
43static struct clk *txx9_imclk;
44static DEFINE_SPINLOCK(txx9_lock);
45
46static int txx9wdt_ping(struct watchdog_device *wdt_dev)
47{
48 spin_lock(&txx9_lock);
49 __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr);
50 spin_unlock(&txx9_lock);
51 return 0;
52}
53
54static int txx9wdt_start(struct watchdog_device *wdt_dev)
55{
56 spin_lock(&txx9_lock);
57 __raw_writel(WD_TIMER_CLK * wdt_dev->timeout, &txx9wdt_reg->cpra);
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);
63 spin_unlock(&txx9_lock);
64 return 0;
65}
66
67static int txx9wdt_stop(struct watchdog_device *wdt_dev)
68{
69 spin_lock(&txx9_lock);
70 __raw_writel(TXx9_TMWTMR_WDIS, &txx9wdt_reg->wtmr);
71 __raw_writel(__raw_readl(&txx9wdt_reg->tcr) & ~TXx9_TMTCR_TCE,
72 &txx9wdt_reg->tcr);
73 spin_unlock(&txx9_lock);
74 return 0;
75}
76
77static int txx9wdt_set_timeout(struct watchdog_device *wdt_dev,
78 unsigned int new_timeout)
79{
80 wdt_dev->timeout = new_timeout;
81 txx9wdt_stop(wdt_dev);
82 txx9wdt_start(wdt_dev);
83 return 0;
84}
85
86static const struct watchdog_info txx9wdt_info = {
87 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
88 .identity = "Hardware Watchdog for TXx9",
89};
90
91static 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
99static struct watchdog_device txx9wdt = {
100 .info = &txx9wdt_info,
101 .ops = &txx9wdt_ops,
102};
103
104static 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);
123 txx9wdt_reg = devm_ioremap_resource(&dev->dev, res);
124 if (IS_ERR(txx9wdt_reg)) {
125 ret = PTR_ERR(txx9wdt_reg);
126 goto exit;
127 }
128
129 if (timeout < 1 || timeout > WD_MAX_TIMEOUT)
130 timeout = TIMER_MARGIN;
131 txx9wdt.timeout = timeout;
132 txx9wdt.min_timeout = 1;
133 txx9wdt.max_timeout = WD_MAX_TIMEOUT;
134 txx9wdt.parent = &dev->dev;
135 watchdog_set_nowayout(&txx9wdt, nowayout);
136
137 ret = watchdog_register_device(&txx9wdt);
138 if (ret)
139 goto exit;
140
141 pr_info("Hardware Watchdog Timer: timeout=%d sec (max %ld) (nowayout= %d)\n",
142 timeout, WD_MAX_TIMEOUT, nowayout);
143
144 return 0;
145exit:
146 if (txx9_imclk) {
147 clk_disable(txx9_imclk);
148 clk_put(txx9_imclk);
149 }
150 return ret;
151}
152
153static int __exit txx9wdt_remove(struct platform_device *dev)
154{
155 watchdog_unregister_device(&txx9wdt);
156 clk_disable(txx9_imclk);
157 clk_put(txx9_imclk);
158 return 0;
159}
160
161static void txx9wdt_shutdown(struct platform_device *dev)
162{
163 txx9wdt_stop(&txx9wdt);
164}
165
166static struct platform_driver txx9wdt_driver = {
167 .remove = __exit_p(txx9wdt_remove),
168 .shutdown = txx9wdt_shutdown,
169 .driver = {
170 .name = "txx9wdt",
171 },
172};
173
174module_platform_driver_probe(txx9wdt_driver, txx9wdt_probe);
175
176MODULE_DESCRIPTION("TXx9 Watchdog Driver");
177MODULE_LICENSE("GPL");
178MODULE_ALIAS("platform:txx9wdt");