Blame view

drivers/watchdog/cadence_wdt.c 11.7 KB
2e62c4988   Marcus Folkesson   watchdog: add SPD...
1
  // SPDX-License-Identifier: GPL-2.0+
58bf01642   Harini Katakam   watchdog: Add Cad...
2
3
4
5
6
  /*
   * Cadence WDT driver - Used by Xilinx Zynq
   *
   * Copyright (C) 2010 - 2014 Xilinx, Inc.
   *
58bf01642   Harini Katakam   watchdog: Add Cad...
7
8
9
10
11
12
13
14
15
16
17
   */
  
  #include <linux/clk.h>
  #include <linux/init.h>
  #include <linux/interrupt.h>
  #include <linux/io.h>
  #include <linux/irq.h>
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/of.h>
  #include <linux/platform_device.h>
58bf01642   Harini Katakam   watchdog: Add Cad...
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  #include <linux/watchdog.h>
  
  #define CDNS_WDT_DEFAULT_TIMEOUT	10
  /* Supports 1 - 516 sec */
  #define CDNS_WDT_MIN_TIMEOUT	1
  #define CDNS_WDT_MAX_TIMEOUT	516
  
  /* Restart key */
  #define CDNS_WDT_RESTART_KEY 0x00001999
  
  /* Counter register access key */
  #define CDNS_WDT_REGISTER_ACCESS_KEY 0x00920000
  
  /* Counter value divisor */
  #define CDNS_WDT_COUNTER_VALUE_DIVISOR 0x1000
  
  /* Clock prescaler value and selection */
  #define CDNS_WDT_PRESCALE_64	64
  #define CDNS_WDT_PRESCALE_512	512
  #define CDNS_WDT_PRESCALE_4096	4096
  #define CDNS_WDT_PRESCALE_SELECT_64	1
  #define CDNS_WDT_PRESCALE_SELECT_512	2
  #define CDNS_WDT_PRESCALE_SELECT_4096	3
  
  /* Input clock frequency */
  #define CDNS_WDT_CLK_10MHZ	10000000
  #define CDNS_WDT_CLK_75MHZ	75000000
  
  /* Counter maximum value */
  #define CDNS_WDT_COUNTER_MAX 0xFFF
0ddad77b9   Tomas Melin   watchdog: cadence...
48
  static int wdt_timeout;
58bf01642   Harini Katakam   watchdog: Add Cad...
49
  static int nowayout = WATCHDOG_NOWAYOUT;
db202f4d5   Michal Simek   watchdog: cadence...
50
  module_param(wdt_timeout, int, 0644);
58bf01642   Harini Katakam   watchdog: Add Cad...
51
52
53
  MODULE_PARM_DESC(wdt_timeout,
  		 "Watchdog time in seconds. (default="
  		 __MODULE_STRING(CDNS_WDT_DEFAULT_TIMEOUT) ")");
db202f4d5   Michal Simek   watchdog: cadence...
54
  module_param(nowayout, int, 0644);
58bf01642   Harini Katakam   watchdog: Add Cad...
55
56
57
58
59
60
61
62
63
64
65
66
67
  MODULE_PARM_DESC(nowayout,
  		 "Watchdog cannot be stopped once started (default="
  		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  
  /**
   * struct cdns_wdt - Watchdog device structure
   * @regs: baseaddress of device
   * @rst: reset flag
   * @clk: struct clk * of a clock source
   * @prescaler: for saving prescaler value
   * @ctrl_clksel: counter clock prescaler selection
   * @io_lock: spinlock for IO register access
   * @cdns_wdt_device: watchdog device structure
58bf01642   Harini Katakam   watchdog: Add Cad...
68
69
70
71
72
73
74
75
76
77
78
   *
   * Structure containing parameters specific to cadence watchdog.
   */
  struct cdns_wdt {
  	void __iomem		*regs;
  	bool			rst;
  	struct clk		*clk;
  	u32			prescaler;
  	u32			ctrl_clksel;
  	spinlock_t		io_lock;
  	struct watchdog_device	cdns_wdt_device;
58bf01642   Harini Katakam   watchdog: Add Cad...
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
  };
  
  /* Write access to Registers */
  static inline void cdns_wdt_writereg(struct cdns_wdt *wdt, u32 offset, u32 val)
  {
  	writel_relaxed(val, wdt->regs + offset);
  }
  
  /*************************Register Map**************************************/
  
  /* Register Offsets for the WDT */
  #define CDNS_WDT_ZMR_OFFSET	0x0	/* Zero Mode Register */
  #define CDNS_WDT_CCR_OFFSET	0x4	/* Counter Control Register */
  #define CDNS_WDT_RESTART_OFFSET	0x8	/* Restart Register */
  #define CDNS_WDT_SR_OFFSET	0xC	/* Status Register */
  
  /*
   * Zero Mode Register - This register controls how the time out is indicated
   * and also contains the access code to allow writes to the register (0xABC).
   */
  #define CDNS_WDT_ZMR_WDEN_MASK	0x00000001 /* Enable the WDT */
  #define CDNS_WDT_ZMR_RSTEN_MASK	0x00000002 /* Enable the reset output */
  #define CDNS_WDT_ZMR_IRQEN_MASK	0x00000004 /* Enable IRQ output */
  #define CDNS_WDT_ZMR_RSTLEN_16	0x00000030 /* Reset pulse of 16 pclk cycles */
  #define CDNS_WDT_ZMR_ZKEY_VAL	0x00ABC000 /* Access key, 0xABC << 12 */
  /*
   * Counter Control register - This register controls how fast the timer runs
   * and the reset value and also contains the access code to allow writes to
   * the register.
   */
  #define CDNS_WDT_CCR_CRV_MASK	0x00003FFC /* Counter reset value */
  
  /**
   * cdns_wdt_stop - Stop the watchdog.
   *
   * @wdd: watchdog device
   *
   * Read the contents of the ZMR register, clear the WDEN bit
   * in the register and set the access key for successful write.
   *
   * Return: always 0
   */
  static int cdns_wdt_stop(struct watchdog_device *wdd)
  {
  	struct cdns_wdt *wdt = watchdog_get_drvdata(wdd);
  
  	spin_lock(&wdt->io_lock);
  	cdns_wdt_writereg(wdt, CDNS_WDT_ZMR_OFFSET,
  			  CDNS_WDT_ZMR_ZKEY_VAL & (~CDNS_WDT_ZMR_WDEN_MASK));
  	spin_unlock(&wdt->io_lock);
  
  	return 0;
  }
  
  /**
   * cdns_wdt_reload - Reload the watchdog timer (i.e. pat the watchdog).
   *
   * @wdd: watchdog device
   *
   * Write the restart key value (0x00001999) to the restart register.
   *
   * Return: always 0
   */
  static int cdns_wdt_reload(struct watchdog_device *wdd)
  {
  	struct cdns_wdt *wdt = watchdog_get_drvdata(wdd);
  
  	spin_lock(&wdt->io_lock);
  	cdns_wdt_writereg(wdt, CDNS_WDT_RESTART_OFFSET,
  			  CDNS_WDT_RESTART_KEY);
  	spin_unlock(&wdt->io_lock);
  
  	return 0;
  }
  
  /**
   * cdns_wdt_start - Enable and start the watchdog.
   *
   * @wdd: watchdog device
   *
   * The counter value is calculated according to the formula:
   *		calculated count = (timeout * clock) / prescaler + 1.
   * The calculated count is divided by 0x1000 to obtain the field value
   * to write to counter control register.
   * Clears the contents of prescaler and counter reset value. Sets the
   * prescaler to 4096 and the calculated count and access key
   * to write to CCR Register.
   * Sets the WDT (WDEN bit) and either the Reset signal(RSTEN bit)
   * or Interrupt signal(IRQEN) with a specified cycles and the access
   * key to write to ZMR Register.
   *
   * Return: always 0
   */
  static int cdns_wdt_start(struct watchdog_device *wdd)
  {
  	struct cdns_wdt *wdt = watchdog_get_drvdata(wdd);
  	unsigned int data = 0;
  	unsigned short count;
  	unsigned long clock_f = clk_get_rate(wdt->clk);
  
  	/*
  	 * Counter value divisor to obtain the value of
  	 * counter reset to be written to control register.
  	 */
  	count = (wdd->timeout * (clock_f / wdt->prescaler)) /
  		 CDNS_WDT_COUNTER_VALUE_DIVISOR + 1;
  
  	if (count > CDNS_WDT_COUNTER_MAX)
  		count = CDNS_WDT_COUNTER_MAX;
  
  	spin_lock(&wdt->io_lock);
  	cdns_wdt_writereg(wdt, CDNS_WDT_ZMR_OFFSET,
  			  CDNS_WDT_ZMR_ZKEY_VAL);
  
  	count = (count << 2) & CDNS_WDT_CCR_CRV_MASK;
  
  	/* Write counter access key first to be able write to register */
  	data = count | CDNS_WDT_REGISTER_ACCESS_KEY | wdt->ctrl_clksel;
  	cdns_wdt_writereg(wdt, CDNS_WDT_CCR_OFFSET, data);
  	data = CDNS_WDT_ZMR_WDEN_MASK | CDNS_WDT_ZMR_RSTLEN_16 |
  	       CDNS_WDT_ZMR_ZKEY_VAL;
  
  	/* Reset on timeout if specified in device tree. */
  	if (wdt->rst) {
  		data |= CDNS_WDT_ZMR_RSTEN_MASK;
  		data &= ~CDNS_WDT_ZMR_IRQEN_MASK;
  	} else {
  		data &= ~CDNS_WDT_ZMR_RSTEN_MASK;
  		data |= CDNS_WDT_ZMR_IRQEN_MASK;
  	}
  	cdns_wdt_writereg(wdt, CDNS_WDT_ZMR_OFFSET, data);
  	cdns_wdt_writereg(wdt, CDNS_WDT_RESTART_OFFSET,
  			  CDNS_WDT_RESTART_KEY);
  	spin_unlock(&wdt->io_lock);
  
  	return 0;
  }
  
  /**
   * cdns_wdt_settimeout - Set a new timeout value for the watchdog device.
   *
   * @wdd: watchdog device
   * @new_time: new timeout value that needs to be set
   * Return: 0 on success
   *
   * Update the watchdog_device timeout with new value which is used when
   * cdns_wdt_start is called.
   */
  static int cdns_wdt_settimeout(struct watchdog_device *wdd,
  			       unsigned int new_time)
  {
  	wdd->timeout = new_time;
  
  	return cdns_wdt_start(wdd);
  }
  
  /**
   * cdns_wdt_irq_handler - Notifies of watchdog timeout.
   *
   * @irq: interrupt number
   * @dev_id: pointer to a platform device structure
   * Return: IRQ_HANDLED
   *
   * The handler is invoked when the watchdog times out and a
   * reset on timeout has not been enabled.
   */
  static irqreturn_t cdns_wdt_irq_handler(int irq, void *dev_id)
  {
  	struct platform_device *pdev = dev_id;
  
  	dev_info(&pdev->dev,
  		 "Watchdog timed out. Internal reset not enabled
  ");
  
  	return IRQ_HANDLED;
  }
  
  /*
   * Info structure used to indicate the features supported by the device
   * to the upper layers. This is defined in watchdog.h header file.
   */
6c368932f   Bhumika Goyal   watchdog: constif...
260
  static const struct watchdog_info cdns_wdt_info = {
58bf01642   Harini Katakam   watchdog: Add Cad...
261
262
263
264
265
266
  	.identity	= "cdns_wdt watchdog",
  	.options	= WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
  			  WDIOF_MAGICCLOSE,
  };
  
  /* Watchdog Core Ops */
85f15cfc2   Julia Lawall   watchdog: constif...
267
  static const struct watchdog_ops cdns_wdt_ops = {
58bf01642   Harini Katakam   watchdog: Add Cad...
268
269
270
271
272
273
  	.owner = THIS_MODULE,
  	.start = cdns_wdt_start,
  	.stop = cdns_wdt_stop,
  	.ping = cdns_wdt_reload,
  	.set_timeout = cdns_wdt_settimeout,
  };
605d8c4f3   Guenter Roeck   watchdog: cadence...
274
275
276
277
  static void cdns_clk_disable_unprepare(void *data)
  {
  	clk_disable_unprepare(data);
  }
58bf01642   Harini Katakam   watchdog: Add Cad...
278
279
280
281
282
283
284
285
286
287
288
  /************************Platform Operations*****************************/
  /**
   * cdns_wdt_probe - Probe call for the device.
   *
   * @pdev: handle to the platform device structure.
   * Return: 0 on success, negative error otherwise.
   *
   * It does all the memory allocation and registration for the device.
   */
  static int cdns_wdt_probe(struct platform_device *pdev)
  {
605d8c4f3   Guenter Roeck   watchdog: cadence...
289
  	struct device *dev = &pdev->dev;
58bf01642   Harini Katakam   watchdog: Add Cad...
290
291
292
293
  	int ret, irq;
  	unsigned long clock_f;
  	struct cdns_wdt *wdt;
  	struct watchdog_device *cdns_wdt_device;
605d8c4f3   Guenter Roeck   watchdog: cadence...
294
  	wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
58bf01642   Harini Katakam   watchdog: Add Cad...
295
296
297
298
299
300
301
302
303
  	if (!wdt)
  		return -ENOMEM;
  
  	cdns_wdt_device = &wdt->cdns_wdt_device;
  	cdns_wdt_device->info = &cdns_wdt_info;
  	cdns_wdt_device->ops = &cdns_wdt_ops;
  	cdns_wdt_device->timeout = CDNS_WDT_DEFAULT_TIMEOUT;
  	cdns_wdt_device->min_timeout = CDNS_WDT_MIN_TIMEOUT;
  	cdns_wdt_device->max_timeout = CDNS_WDT_MAX_TIMEOUT;
0f0a6a285   Guenter Roeck   watchdog: Convert...
304
  	wdt->regs = devm_platform_ioremap_resource(pdev, 0);
58bf01642   Harini Katakam   watchdog: Add Cad...
305
306
307
308
  	if (IS_ERR(wdt->regs))
  		return PTR_ERR(wdt->regs);
  
  	/* Register the interrupt */
605d8c4f3   Guenter Roeck   watchdog: cadence...
309
  	wdt->rst = of_property_read_bool(dev->of_node, "reset-on-timeout");
58bf01642   Harini Katakam   watchdog: Add Cad...
310
311
  	irq = platform_get_irq(pdev, 0);
  	if (!wdt->rst && irq >= 0) {
605d8c4f3   Guenter Roeck   watchdog: cadence...
312
  		ret = devm_request_irq(dev, irq, cdns_wdt_irq_handler, 0,
58bf01642   Harini Katakam   watchdog: Add Cad...
313
314
  				       pdev->name, pdev);
  		if (ret) {
605d8c4f3   Guenter Roeck   watchdog: cadence...
315
  			dev_err(dev,
58bf01642   Harini Katakam   watchdog: Add Cad...
316
317
318
319
320
321
322
323
  				"cannot register interrupt handler err=%d
  ",
  				ret);
  			return ret;
  		}
  	}
  
  	/* Initialize the members of cdns_wdt structure */
605d8c4f3   Guenter Roeck   watchdog: cadence...
324
  	cdns_wdt_device->parent = dev;
58bf01642   Harini Katakam   watchdog: Add Cad...
325

c7d30d42f   Wolfram Sang   watchdog: cadence...
326
  	watchdog_init_timeout(cdns_wdt_device, wdt_timeout, dev);
58bf01642   Harini Katakam   watchdog: Add Cad...
327
  	watchdog_set_nowayout(cdns_wdt_device, nowayout);
5e1f976f4   Damien Riegel   watchdog: cadence...
328
  	watchdog_stop_on_reboot(cdns_wdt_device);
58bf01642   Harini Katakam   watchdog: Add Cad...
329
  	watchdog_set_drvdata(cdns_wdt_device, wdt);
605d8c4f3   Guenter Roeck   watchdog: cadence...
330
  	wdt->clk = devm_clk_get(dev, NULL);
dab112215   Krzysztof Kozlowski   watchdog: cadence...
331
332
333
334
  	if (IS_ERR(wdt->clk))
  		return dev_err_probe(dev, PTR_ERR(wdt->clk),
  				     "input clock not found
  ");
58bf01642   Harini Katakam   watchdog: Add Cad...
335
336
337
  
  	ret = clk_prepare_enable(wdt->clk);
  	if (ret) {
605d8c4f3   Guenter Roeck   watchdog: cadence...
338
339
  		dev_err(dev, "unable to enable clock
  ");
58bf01642   Harini Katakam   watchdog: Add Cad...
340
341
  		return ret;
  	}
605d8c4f3   Guenter Roeck   watchdog: cadence...
342
343
344
345
  	ret = devm_add_action_or_reset(dev, cdns_clk_disable_unprepare,
  				       wdt->clk);
  	if (ret)
  		return ret;
58bf01642   Harini Katakam   watchdog: Add Cad...
346
347
348
349
350
351
352
353
354
355
356
  
  	clock_f = clk_get_rate(wdt->clk);
  	if (clock_f <= CDNS_WDT_CLK_75MHZ) {
  		wdt->prescaler = CDNS_WDT_PRESCALE_512;
  		wdt->ctrl_clksel = CDNS_WDT_PRESCALE_SELECT_512;
  	} else {
  		wdt->prescaler = CDNS_WDT_PRESCALE_4096;
  		wdt->ctrl_clksel = CDNS_WDT_PRESCALE_SELECT_4096;
  	}
  
  	spin_lock_init(&wdt->io_lock);
605d8c4f3   Guenter Roeck   watchdog: cadence...
357
358
359
  	watchdog_stop_on_reboot(cdns_wdt_device);
  	watchdog_stop_on_unregister(cdns_wdt_device);
  	ret = devm_watchdog_register_device(dev, cdns_wdt_device);
76ed828b8   Wolfram Sang   watchdog: cadence...
360
  	if (ret)
605d8c4f3   Guenter Roeck   watchdog: cadence...
361
  		return ret;
58bf01642   Harini Katakam   watchdog: Add Cad...
362
  	platform_set_drvdata(pdev, wdt);
b1301b902   Srinivas Neeli   watchdog: cadence...
363
364
365
  	dev_info(dev, "Xilinx Watchdog Timer with timeout %ds%s
  ",
  		 cdns_wdt_device->timeout, nowayout ? ", nowayout" : "");
58bf01642   Harini Katakam   watchdog: Add Cad...
366
367
  
  	return 0;
58bf01642   Harini Katakam   watchdog: Add Cad...
368
369
370
371
372
373
374
375
376
377
  }
  
  /**
   * cdns_wdt_suspend - Stop the device.
   *
   * @dev: handle to the device structure.
   * Return: 0 always.
   */
  static int __maybe_unused cdns_wdt_suspend(struct device *dev)
  {
20745634c   Wolfram Sang   watchdog: simplif...
378
  	struct cdns_wdt *wdt = dev_get_drvdata(dev);
58bf01642   Harini Katakam   watchdog: Add Cad...
379

eadc4fe17   Shubhrajyoti Datta   watchdog: cadence...
380
381
382
383
  	if (watchdog_active(&wdt->cdns_wdt_device)) {
  		cdns_wdt_stop(&wdt->cdns_wdt_device);
  		clk_disable_unprepare(wdt->clk);
  	}
58bf01642   Harini Katakam   watchdog: Add Cad...
384
385
386
387
388
389
390
391
392
393
394
395
396
  
  	return 0;
  }
  
  /**
   * cdns_wdt_resume - Resume the device.
   *
   * @dev: handle to the device structure.
   * Return: 0 on success, errno otherwise.
   */
  static int __maybe_unused cdns_wdt_resume(struct device *dev)
  {
  	int ret;
20745634c   Wolfram Sang   watchdog: simplif...
397
  	struct cdns_wdt *wdt = dev_get_drvdata(dev);
58bf01642   Harini Katakam   watchdog: Add Cad...
398

eadc4fe17   Shubhrajyoti Datta   watchdog: cadence...
399
400
401
402
403
404
405
406
  	if (watchdog_active(&wdt->cdns_wdt_device)) {
  		ret = clk_prepare_enable(wdt->clk);
  		if (ret) {
  			dev_err(dev, "unable to enable clock
  ");
  			return ret;
  		}
  		cdns_wdt_start(&wdt->cdns_wdt_device);
58bf01642   Harini Katakam   watchdog: Add Cad...
407
  	}
58bf01642   Harini Katakam   watchdog: Add Cad...
408
409
410
411
412
  
  	return 0;
  }
  
  static SIMPLE_DEV_PM_OPS(cdns_wdt_pm_ops, cdns_wdt_suspend, cdns_wdt_resume);
011e29e7d   Arvind Yadav   watchdog: cadence...
413
  static const struct of_device_id cdns_wdt_of_match[] = {
58bf01642   Harini Katakam   watchdog: Add Cad...
414
415
416
417
418
419
420
421
  	{ .compatible = "cdns,wdt-r1p2", },
  	{ /* end of table */ }
  };
  MODULE_DEVICE_TABLE(of, cdns_wdt_of_match);
  
  /* Driver Structure */
  static struct platform_driver cdns_wdt_driver = {
  	.probe		= cdns_wdt_probe,
58bf01642   Harini Katakam   watchdog: Add Cad...
422
423
  	.driver		= {
  		.name	= "cdns-wdt",
58bf01642   Harini Katakam   watchdog: Add Cad...
424
425
426
427
428
429
430
431
432
433
  		.of_match_table = cdns_wdt_of_match,
  		.pm	= &cdns_wdt_pm_ops,
  	},
  };
  
  module_platform_driver(cdns_wdt_driver);
  
  MODULE_AUTHOR("Xilinx, Inc.");
  MODULE_DESCRIPTION("Watchdog driver for Cadence WDT");
  MODULE_LICENSE("GPL");