Blame view

drivers/watchdog/meson_wdt.c 5.84 KB
2874c5fd2   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
22e1b8f60   Carlo Caione   ARM: meson: add w...
2
3
4
5
  /*
   *      Meson Watchdog Driver
   *
   *      Copyright (c) 2014 Carlo Caione
22e1b8f60   Carlo Caione   ARM: meson: add w...
6
7
8
9
10
11
12
13
14
15
   */
  
  #include <linux/clk.h>
  #include <linux/delay.h>
  #include <linux/err.h>
  #include <linux/init.h>
  #include <linux/io.h>
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/moduleparam.h>
22e1b8f60   Carlo Caione   ARM: meson: add w...
16
  #include <linux/of.h>
943bf1f64   Carlo Caione   watchdog: meson: ...
17
  #include <linux/of_device.h>
22e1b8f60   Carlo Caione   ARM: meson: add w...
18
  #include <linux/platform_device.h>
22e1b8f60   Carlo Caione   ARM: meson: add w...
19
20
21
22
23
24
  #include <linux/types.h>
  #include <linux/watchdog.h>
  
  #define DRV_NAME		"meson_wdt"
  
  #define MESON_WDT_TC		0x00
22e1b8f60   Carlo Caione   ARM: meson: add w...
25
26
27
28
29
30
  #define MESON_WDT_DC_RESET	(3 << 24)
  
  #define MESON_WDT_RESET		0x04
  
  #define MESON_WDT_TIMEOUT	30
  #define MESON_WDT_MIN_TIMEOUT	1
22e1b8f60   Carlo Caione   ARM: meson: add w...
31

943bf1f64   Carlo Caione   watchdog: meson: ...
32
  #define MESON_SEC_TO_TC(s, c)	((s) * (c))
22e1b8f60   Carlo Caione   ARM: meson: add w...
33
34
  
  static bool nowayout = WATCHDOG_NOWAYOUT;
4590d62cb   Marcus Folkesson   watchdog: meson: ...
35
  static unsigned int timeout;
22e1b8f60   Carlo Caione   ARM: meson: add w...
36

943bf1f64   Carlo Caione   watchdog: meson: ...
37
38
39
40
41
42
43
44
45
46
47
  struct meson_wdt_data {
  	unsigned int enable;
  	unsigned int terminal_count_mask;
  	unsigned int count_unit;
  };
  
  static struct meson_wdt_data meson6_wdt_data = {
  	.enable			= BIT(22),
  	.terminal_count_mask	= 0x3fffff,
  	.count_unit		= 100000, /* 10 us */
  };
71388840e   Carlo Caione   watchdog: meson: ...
48
49
50
51
52
  static struct meson_wdt_data meson8b_wdt_data = {
  	.enable			= BIT(19),
  	.terminal_count_mask	= 0xffff,
  	.count_unit		= 7812, /* 128 us */
  };
22e1b8f60   Carlo Caione   ARM: meson: add w...
53
54
55
  struct meson_wdt_dev {
  	struct watchdog_device wdt_dev;
  	void __iomem *wdt_base;
943bf1f64   Carlo Caione   watchdog: meson: ...
56
  	const struct meson_wdt_data *data;
22e1b8f60   Carlo Caione   ARM: meson: add w...
57
  };
4d8b229d5   Guenter Roeck   watchdog: Add 'ac...
58
59
  static int meson_wdt_restart(struct watchdog_device *wdt_dev,
  			     unsigned long action, void *data)
22e1b8f60   Carlo Caione   ARM: meson: add w...
60
  {
1b6fd59ad   Damien Riegel   watchdog: meson_w...
61
  	struct meson_wdt_dev *meson_wdt = watchdog_get_drvdata(wdt_dev);
943bf1f64   Carlo Caione   watchdog: meson: ...
62
63
64
  	u32 tc_reboot = MESON_WDT_DC_RESET;
  
  	tc_reboot |= meson_wdt->data->enable;
22e1b8f60   Carlo Caione   ARM: meson: add w...
65
66
67
68
69
  
  	while (1) {
  		writel(tc_reboot, meson_wdt->wdt_base + MESON_WDT_TC);
  		mdelay(5);
  	}
1b6fd59ad   Damien Riegel   watchdog: meson_w...
70
  	return 0;
22e1b8f60   Carlo Caione   ARM: meson: add w...
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  }
  
  static int meson_wdt_ping(struct watchdog_device *wdt_dev)
  {
  	struct meson_wdt_dev *meson_wdt = watchdog_get_drvdata(wdt_dev);
  
  	writel(0, meson_wdt->wdt_base + MESON_WDT_RESET);
  
  	return 0;
  }
  
  static void meson_wdt_change_timeout(struct watchdog_device *wdt_dev,
  				     unsigned int timeout)
  {
  	struct meson_wdt_dev *meson_wdt = watchdog_get_drvdata(wdt_dev);
  	u32 reg;
  
  	reg = readl(meson_wdt->wdt_base + MESON_WDT_TC);
943bf1f64   Carlo Caione   watchdog: meson: ...
89
90
  	reg &= ~meson_wdt->data->terminal_count_mask;
  	reg |= MESON_SEC_TO_TC(timeout, meson_wdt->data->count_unit);
22e1b8f60   Carlo Caione   ARM: meson: add w...
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  	writel(reg, meson_wdt->wdt_base + MESON_WDT_TC);
  }
  
  static int meson_wdt_set_timeout(struct watchdog_device *wdt_dev,
  				 unsigned int timeout)
  {
  	wdt_dev->timeout = timeout;
  
  	meson_wdt_change_timeout(wdt_dev, timeout);
  	meson_wdt_ping(wdt_dev);
  
  	return 0;
  }
  
  static int meson_wdt_stop(struct watchdog_device *wdt_dev)
  {
  	struct meson_wdt_dev *meson_wdt = watchdog_get_drvdata(wdt_dev);
  	u32 reg;
  
  	reg = readl(meson_wdt->wdt_base + MESON_WDT_TC);
943bf1f64   Carlo Caione   watchdog: meson: ...
111
  	reg &= ~meson_wdt->data->enable;
22e1b8f60   Carlo Caione   ARM: meson: add w...
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  	writel(reg, meson_wdt->wdt_base + MESON_WDT_TC);
  
  	return 0;
  }
  
  static int meson_wdt_start(struct watchdog_device *wdt_dev)
  {
  	struct meson_wdt_dev *meson_wdt = watchdog_get_drvdata(wdt_dev);
  	u32 reg;
  
  	meson_wdt_change_timeout(wdt_dev, meson_wdt->wdt_dev.timeout);
  	meson_wdt_ping(wdt_dev);
  
  	reg = readl(meson_wdt->wdt_base + MESON_WDT_TC);
943bf1f64   Carlo Caione   watchdog: meson: ...
126
  	reg |= meson_wdt->data->enable;
22e1b8f60   Carlo Caione   ARM: meson: add w...
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  	writel(reg, meson_wdt->wdt_base + MESON_WDT_TC);
  
  	return 0;
  }
  
  static const struct watchdog_info meson_wdt_info = {
  	.identity	= DRV_NAME,
  	.options	= WDIOF_SETTIMEOUT |
  			  WDIOF_KEEPALIVEPING |
  			  WDIOF_MAGICCLOSE,
  };
  
  static const struct watchdog_ops meson_wdt_ops = {
  	.owner		= THIS_MODULE,
  	.start		= meson_wdt_start,
  	.stop		= meson_wdt_stop,
  	.ping		= meson_wdt_ping,
  	.set_timeout	= meson_wdt_set_timeout,
1b6fd59ad   Damien Riegel   watchdog: meson_w...
145
  	.restart        = meson_wdt_restart,
22e1b8f60   Carlo Caione   ARM: meson: add w...
146
  };
943bf1f64   Carlo Caione   watchdog: meson: ...
147
148
  static const struct of_device_id meson_wdt_dt_ids[] = {
  	{ .compatible = "amlogic,meson6-wdt", .data = &meson6_wdt_data },
43a64e81b   Martin Blumenstingl   watchdog: meson-w...
149
  	{ .compatible = "amlogic,meson8-wdt", .data = &meson6_wdt_data },
71388840e   Carlo Caione   watchdog: meson: ...
150
  	{ .compatible = "amlogic,meson8b-wdt", .data = &meson8b_wdt_data },
43a64e81b   Martin Blumenstingl   watchdog: meson-w...
151
  	{ .compatible = "amlogic,meson8m2-wdt", .data = &meson8b_wdt_data },
943bf1f64   Carlo Caione   watchdog: meson: ...
152
153
154
  	{ /* sentinel */ }
  };
  MODULE_DEVICE_TABLE(of, meson_wdt_dt_ids);
22e1b8f60   Carlo Caione   ARM: meson: add w...
155
156
  static int meson_wdt_probe(struct platform_device *pdev)
  {
dd1c66e22   Guenter Roeck   watchdog: meson_w...
157
  	struct device *dev = &pdev->dev;
22e1b8f60   Carlo Caione   ARM: meson: add w...
158
  	struct meson_wdt_dev *meson_wdt;
943bf1f64   Carlo Caione   watchdog: meson: ...
159
  	const struct of_device_id *of_id;
22e1b8f60   Carlo Caione   ARM: meson: add w...
160
  	int err;
dd1c66e22   Guenter Roeck   watchdog: meson_w...
161
  	meson_wdt = devm_kzalloc(dev, sizeof(*meson_wdt), GFP_KERNEL);
22e1b8f60   Carlo Caione   ARM: meson: add w...
162
163
  	if (!meson_wdt)
  		return -ENOMEM;
0f0a6a285   Guenter Roeck   watchdog: Convert...
164
  	meson_wdt->wdt_base = devm_platform_ioremap_resource(pdev, 0);
22e1b8f60   Carlo Caione   ARM: meson: add w...
165
166
  	if (IS_ERR(meson_wdt->wdt_base))
  		return PTR_ERR(meson_wdt->wdt_base);
dd1c66e22   Guenter Roeck   watchdog: meson_w...
167
  	of_id = of_match_device(meson_wdt_dt_ids, dev);
943bf1f64   Carlo Caione   watchdog: meson: ...
168
  	if (!of_id) {
dd1c66e22   Guenter Roeck   watchdog: meson_w...
169
170
  		dev_err(dev, "Unable to initialize WDT data
  ");
943bf1f64   Carlo Caione   watchdog: meson: ...
171
172
173
  		return -ENODEV;
  	}
  	meson_wdt->data = of_id->data;
dd1c66e22   Guenter Roeck   watchdog: meson_w...
174
  	meson_wdt->wdt_dev.parent = dev;
22e1b8f60   Carlo Caione   ARM: meson: add w...
175
176
  	meson_wdt->wdt_dev.info = &meson_wdt_info;
  	meson_wdt->wdt_dev.ops = &meson_wdt_ops;
943bf1f64   Carlo Caione   watchdog: meson: ...
177
178
  	meson_wdt->wdt_dev.max_timeout =
  		meson_wdt->data->terminal_count_mask / meson_wdt->data->count_unit;
22e1b8f60   Carlo Caione   ARM: meson: add w...
179
  	meson_wdt->wdt_dev.min_timeout = MESON_WDT_MIN_TIMEOUT;
943bf1f64   Carlo Caione   watchdog: meson: ...
180
181
182
  	meson_wdt->wdt_dev.timeout = min_t(unsigned int,
  					   MESON_WDT_TIMEOUT,
  					   meson_wdt->wdt_dev.max_timeout);
22e1b8f60   Carlo Caione   ARM: meson: add w...
183
184
  
  	watchdog_set_drvdata(&meson_wdt->wdt_dev, meson_wdt);
dd1c66e22   Guenter Roeck   watchdog: meson_w...
185
  	watchdog_init_timeout(&meson_wdt->wdt_dev, timeout, dev);
22e1b8f60   Carlo Caione   ARM: meson: add w...
186
  	watchdog_set_nowayout(&meson_wdt->wdt_dev, nowayout);
1b6fd59ad   Damien Riegel   watchdog: meson_w...
187
  	watchdog_set_restart_priority(&meson_wdt->wdt_dev, 128);
22e1b8f60   Carlo Caione   ARM: meson: add w...
188
189
  
  	meson_wdt_stop(&meson_wdt->wdt_dev);
c8841a606   Guenter Roeck   watchdog: meson_w...
190
  	watchdog_stop_on_reboot(&meson_wdt->wdt_dev);
dd1c66e22   Guenter Roeck   watchdog: meson_w...
191
  	err = devm_watchdog_register_device(dev, &meson_wdt->wdt_dev);
22e1b8f60   Carlo Caione   ARM: meson: add w...
192
193
  	if (err)
  		return err;
dd1c66e22   Guenter Roeck   watchdog: meson_w...
194
  	dev_info(dev, "Watchdog enabled (timeout=%d sec, nowayout=%d)",
22e1b8f60   Carlo Caione   ARM: meson: add w...
195
196
197
198
  		 meson_wdt->wdt_dev.timeout, nowayout);
  
  	return 0;
  }
22e1b8f60   Carlo Caione   ARM: meson: add w...
199
200
  static struct platform_driver meson_wdt_driver = {
  	.probe		= meson_wdt_probe,
22e1b8f60   Carlo Caione   ARM: meson: add w...
201
  	.driver		= {
22e1b8f60   Carlo Caione   ARM: meson: add w...
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
  		.name		= DRV_NAME,
  		.of_match_table	= meson_wdt_dt_ids,
  	},
  };
  
  module_platform_driver(meson_wdt_driver);
  
  module_param(timeout, uint, 0);
  MODULE_PARM_DESC(timeout, "Watchdog heartbeat in seconds");
  
  module_param(nowayout, bool, 0);
  MODULE_PARM_DESC(nowayout,
  		 "Watchdog cannot be stopped once started (default="
  		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  
  MODULE_LICENSE("GPL");
  MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
  MODULE_DESCRIPTION("Meson Watchdog Timer Driver");