Blame view

drivers/watchdog/of_xilinx_wdt.c 6.83 KB
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
1
  /*
9419c07cc   Michal Simek   watchdog: xilinx:...
2
3
   * Watchdog Device Driver for Xilinx axi/xps_timebase_wdt
   *
d14fd9645   Michal Simek   watchdog: xilinx:...
4
   * (C) Copyright 2013 - 2014 Xilinx, Inc.
9419c07cc   Michal Simek   watchdog: xilinx:...
5
6
7
8
9
10
11
   * (C) Copyright 2011 (Alejandro Cabrera <aldaya@gmail.com>)
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License
   * as published by the Free Software Foundation; either version
   * 2 of the License, or (at your option) any later version.
   */
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
12

9d6b4efc1   Shubhrajyoti Datta   watchdog: xilinx:...
13
  #include <linux/clk.h>
f06cdfd18   Michal Simek   watchdog: xilinx:...
14
  #include <linux/err.h>
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
15
16
17
  #include <linux/module.h>
  #include <linux/types.h>
  #include <linux/kernel.h>
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
18
19
20
  #include <linux/ioport.h>
  #include <linux/watchdog.h>
  #include <linux/io.h>
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  #include <linux/of.h>
  #include <linux/of_device.h>
  #include <linux/of_address.h>
  
  /* Register offsets for the Wdt device */
  #define XWT_TWCSR0_OFFSET   0x0 /* Control/Status Register0 */
  #define XWT_TWCSR1_OFFSET   0x4 /* Control/Status Register1 */
  #define XWT_TBR_OFFSET      0x8 /* Timebase Register Offset */
  
  /* Control/Status Register Masks  */
  #define XWT_CSR0_WRS_MASK   0x00000008 /* Reset status */
  #define XWT_CSR0_WDS_MASK   0x00000004 /* Timer state  */
  #define XWT_CSR0_EWDT1_MASK 0x00000002 /* Enable bit 1 */
  
  /* Control/Status Register 0/1 bits  */
  #define XWT_CSRX_EWDT2_MASK 0x00000001 /* Enable bit 2 */
  
  /* SelfTest constants */
  #define XWT_MAX_SELFTEST_LOOP_COUNT 0x00010000
  #define XWT_TIMER_FAILED            0xFFFFFFFF
  
  #define WATCHDOG_NAME     "Xilinx Watchdog"
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
43
44
  
  struct xwdt_device {
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
45
  	void __iomem *base;
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
46
  	u32 wdt_interval;
906631717   Michal Simek   watchdog: xilinx:...
47
48
  	spinlock_t spinlock;
  	struct watchdog_device xilinx_wdt_wdd;
9d6b4efc1   Shubhrajyoti Datta   watchdog: xilinx:...
49
  	struct clk		*clk;
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
50
  };
d14fd9645   Michal Simek   watchdog: xilinx:...
51
  static int xilinx_wdt_start(struct watchdog_device *wdd)
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
52
  {
5cf4e69d3   Michal Simek   watchdog: xilinx:...
53
  	u32 control_status_reg;
906631717   Michal Simek   watchdog: xilinx:...
54
  	struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
5cf4e69d3   Michal Simek   watchdog: xilinx:...
55

906631717   Michal Simek   watchdog: xilinx:...
56
  	spin_lock(&xdev->spinlock);
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
57
58
  
  	/* Clean previous status and enable the watchdog timer */
906631717   Michal Simek   watchdog: xilinx:...
59
  	control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
60
61
62
  	control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
  
  	iowrite32((control_status_reg | XWT_CSR0_EWDT1_MASK),
906631717   Michal Simek   watchdog: xilinx:...
63
  		  xdev->base + XWT_TWCSR0_OFFSET);
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
64

906631717   Michal Simek   watchdog: xilinx:...
65
  	iowrite32(XWT_CSRX_EWDT2_MASK, xdev->base + XWT_TWCSR1_OFFSET);
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
66

906631717   Michal Simek   watchdog: xilinx:...
67
  	spin_unlock(&xdev->spinlock);
d14fd9645   Michal Simek   watchdog: xilinx:...
68
69
  
  	return 0;
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
70
  }
d14fd9645   Michal Simek   watchdog: xilinx:...
71
  static int xilinx_wdt_stop(struct watchdog_device *wdd)
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
72
  {
5cf4e69d3   Michal Simek   watchdog: xilinx:...
73
  	u32 control_status_reg;
906631717   Michal Simek   watchdog: xilinx:...
74
  	struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
5cf4e69d3   Michal Simek   watchdog: xilinx:...
75

906631717   Michal Simek   watchdog: xilinx:...
76
  	spin_lock(&xdev->spinlock);
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
77

906631717   Michal Simek   watchdog: xilinx:...
78
  	control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
79
80
  
  	iowrite32((control_status_reg & ~XWT_CSR0_EWDT1_MASK),
906631717   Michal Simek   watchdog: xilinx:...
81
  		  xdev->base + XWT_TWCSR0_OFFSET);
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
82

906631717   Michal Simek   watchdog: xilinx:...
83
  	iowrite32(0, xdev->base + XWT_TWCSR1_OFFSET);
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
84

906631717   Michal Simek   watchdog: xilinx:...
85
  	spin_unlock(&xdev->spinlock);
27c766aaa   Joe Perches   watchdog: Use pr_...
86
87
  	pr_info("Stopped!
  ");
d14fd9645   Michal Simek   watchdog: xilinx:...
88
89
  
  	return 0;
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
90
  }
d14fd9645   Michal Simek   watchdog: xilinx:...
91
  static int xilinx_wdt_keepalive(struct watchdog_device *wdd)
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
92
  {
5cf4e69d3   Michal Simek   watchdog: xilinx:...
93
  	u32 control_status_reg;
906631717   Michal Simek   watchdog: xilinx:...
94
  	struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
5cf4e69d3   Michal Simek   watchdog: xilinx:...
95

906631717   Michal Simek   watchdog: xilinx:...
96
  	spin_lock(&xdev->spinlock);
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
97

906631717   Michal Simek   watchdog: xilinx:...
98
  	control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
99
  	control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
906631717   Michal Simek   watchdog: xilinx:...
100
  	iowrite32(control_status_reg, xdev->base + XWT_TWCSR0_OFFSET);
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
101

906631717   Michal Simek   watchdog: xilinx:...
102
  	spin_unlock(&xdev->spinlock);
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
103

d14fd9645   Michal Simek   watchdog: xilinx:...
104
105
  	return 0;
  }
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
106

d14fd9645   Michal Simek   watchdog: xilinx:...
107
108
109
110
111
112
  static const struct watchdog_info xilinx_wdt_ident = {
  	.options =  WDIOF_MAGICCLOSE |
  		    WDIOF_KEEPALIVEPING,
  	.firmware_version =	1,
  	.identity =	WATCHDOG_NAME,
  };
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
113

d14fd9645   Michal Simek   watchdog: xilinx:...
114
115
116
117
118
119
  static const struct watchdog_ops xilinx_wdt_ops = {
  	.owner = THIS_MODULE,
  	.start = xilinx_wdt_start,
  	.stop = xilinx_wdt_stop,
  	.ping = xilinx_wdt_keepalive,
  };
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
120

906631717   Michal Simek   watchdog: xilinx:...
121
  static u32 xwdt_selftest(struct xwdt_device *xdev)
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
122
123
124
125
  {
  	int i;
  	u32 timer_value1;
  	u32 timer_value2;
906631717   Michal Simek   watchdog: xilinx:...
126
  	spin_lock(&xdev->spinlock);
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
127

906631717   Michal Simek   watchdog: xilinx:...
128
129
  	timer_value1 = ioread32(xdev->base + XWT_TBR_OFFSET);
  	timer_value2 = ioread32(xdev->base + XWT_TBR_OFFSET);
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
130
131
132
133
  
  	for (i = 0;
  		((i <= XWT_MAX_SELFTEST_LOOP_COUNT) &&
  			(timer_value2 == timer_value1)); i++) {
906631717   Michal Simek   watchdog: xilinx:...
134
  		timer_value2 = ioread32(xdev->base + XWT_TBR_OFFSET);
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
135
  	}
906631717   Michal Simek   watchdog: xilinx:...
136
  	spin_unlock(&xdev->spinlock);
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
137
138
139
140
141
142
  
  	if (timer_value2 != timer_value1)
  		return ~XWT_TIMER_FAILED;
  	else
  		return XWT_TIMER_FAILED;
  }
2d991a164   Bill Pemberton   watchdog: remove ...
143
  static int xwdt_probe(struct platform_device *pdev)
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
144
145
  {
  	int rc;
8d6a140b5   Michal Simek   watchdog: xilinx:...
146
  	u32 pfreq = 0, enable_once = 0;
f06cdfd18   Michal Simek   watchdog: xilinx:...
147
  	struct resource *res;
906631717   Michal Simek   watchdog: xilinx:...
148
  	struct xwdt_device *xdev;
906631717   Michal Simek   watchdog: xilinx:...
149
150
151
152
153
154
155
156
157
158
  	struct watchdog_device *xilinx_wdt_wdd;
  
  	xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
  	if (!xdev)
  		return -ENOMEM;
  
  	xilinx_wdt_wdd = &xdev->xilinx_wdt_wdd;
  	xilinx_wdt_wdd->info = &xilinx_wdt_ident;
  	xilinx_wdt_wdd->ops = &xilinx_wdt_ops;
  	xilinx_wdt_wdd->parent = &pdev->dev;
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
159

f06cdfd18   Michal Simek   watchdog: xilinx:...
160
  	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
906631717   Michal Simek   watchdog: xilinx:...
161
162
163
  	xdev->base = devm_ioremap_resource(&pdev->dev, res);
  	if (IS_ERR(xdev->base))
  		return PTR_ERR(xdev->base);
f06cdfd18   Michal Simek   watchdog: xilinx:...
164

2e79a3684   Michal Simek   watchdog: xilinx:...
165
  	rc = of_property_read_u32(pdev->dev.of_node, "clock-frequency", &pfreq);
8d6a140b5   Michal Simek   watchdog: xilinx:...
166
  	if (rc)
4c7fbbc4a   Michal Simek   watchdog: xilinx:...
167
168
169
  		dev_warn(&pdev->dev,
  			 "The watchdog clock frequency cannot be obtained
  ");
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
170

2e79a3684   Michal Simek   watchdog: xilinx:...
171
172
  	rc = of_property_read_u32(pdev->dev.of_node, "xlnx,wdt-interval",
  				  &xdev->wdt_interval);
8d6a140b5   Michal Simek   watchdog: xilinx:...
173
  	if (rc)
4c7fbbc4a   Michal Simek   watchdog: xilinx:...
174
175
176
  		dev_warn(&pdev->dev,
  			 "Parameter \"xlnx,wdt-interval\" not found
  ");
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
177

2e79a3684   Michal Simek   watchdog: xilinx:...
178
179
180
  	rc = of_property_read_u32(pdev->dev.of_node, "xlnx,wdt-enable-once",
  				  &enable_once);
  	if (rc)
4c7fbbc4a   Michal Simek   watchdog: xilinx:...
181
182
183
  		dev_warn(&pdev->dev,
  			 "Parameter \"xlnx,wdt-enable-once\" not found
  ");
2e79a3684   Michal Simek   watchdog: xilinx:...
184
185
  
  	watchdog_set_nowayout(xilinx_wdt_wdd, enable_once);
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
186

75b3c5a82   Michal Simek   watchdog: xilinx:...
187
188
189
190
  	/*
  	 * Twice of the 2^wdt_interval / freq  because the first wdt overflow is
  	 * ignored (interrupt), reset is only generated at second wdt overflow
  	 */
8d6a140b5   Michal Simek   watchdog: xilinx:...
191
  	if (pfreq && xdev->wdt_interval)
906631717   Michal Simek   watchdog: xilinx:...
192
  		xilinx_wdt_wdd->timeout = 2 * ((1 << xdev->wdt_interval) /
2e79a3684   Michal Simek   watchdog: xilinx:...
193
  					  pfreq);
906631717   Michal Simek   watchdog: xilinx:...
194
195
196
  
  	spin_lock_init(&xdev->spinlock);
  	watchdog_set_drvdata(xilinx_wdt_wdd, xdev);
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
197

9d6b4efc1   Shubhrajyoti Datta   watchdog: xilinx:...
198
199
200
201
202
203
204
205
206
207
208
209
210
211
  	xdev->clk = devm_clk_get(&pdev->dev, NULL);
  	if (IS_ERR(xdev->clk)) {
  		if (PTR_ERR(xdev->clk) == -ENOENT)
  			xdev->clk = NULL;
  		else
  			return PTR_ERR(xdev->clk);
  	}
  
  	rc = clk_prepare_enable(xdev->clk);
  	if (rc) {
  		dev_err(&pdev->dev, "unable to enable clock
  ");
  		return rc;
  	}
906631717   Michal Simek   watchdog: xilinx:...
212
  	rc = xwdt_selftest(xdev);
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
213
  	if (rc == XWT_TIMER_FAILED) {
4c7fbbc4a   Michal Simek   watchdog: xilinx:...
214
215
  		dev_err(&pdev->dev, "SelfTest routine error
  ");
9d6b4efc1   Shubhrajyoti Datta   watchdog: xilinx:...
216
  		goto err_clk_disable;
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
217
  	}
906631717   Michal Simek   watchdog: xilinx:...
218
  	rc = watchdog_register_device(xilinx_wdt_wdd);
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
219
  	if (rc) {
4c7fbbc4a   Michal Simek   watchdog: xilinx:...
220
221
  		dev_err(&pdev->dev, "Cannot register watchdog (err=%d)
  ", rc);
9d6b4efc1   Shubhrajyoti Datta   watchdog: xilinx:...
222
  		goto err_clk_disable;
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
223
  	}
d14fd9645   Michal Simek   watchdog: xilinx:...
224
225
  	dev_info(&pdev->dev, "Xilinx Watchdog Timer at %p with timeout %ds
  ",
906631717   Michal Simek   watchdog: xilinx:...
226
227
228
  		 xdev->base, xilinx_wdt_wdd->timeout);
  
  	platform_set_drvdata(pdev, xdev);
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
229
230
  
  	return 0;
9d6b4efc1   Shubhrajyoti Datta   watchdog: xilinx:...
231
232
233
234
  err_clk_disable:
  	clk_disable_unprepare(xdev->clk);
  
  	return rc;
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
235
  }
906631717   Michal Simek   watchdog: xilinx:...
236
  static int xwdt_remove(struct platform_device *pdev)
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
237
  {
906631717   Michal Simek   watchdog: xilinx:...
238
239
240
  	struct xwdt_device *xdev = platform_get_drvdata(pdev);
  
  	watchdog_unregister_device(&xdev->xilinx_wdt_wdd);
9d6b4efc1   Shubhrajyoti Datta   watchdog: xilinx:...
241
  	clk_disable_unprepare(xdev->clk);
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
242
243
244
245
246
  
  	return 0;
  }
  
  /* Match table for of_platform binding */
9ebf1855d   Jingoo Han   watchdog: xilinx:...
247
  static const struct of_device_id xwdt_of_match[] = {
8fce9b367   Michal Simek   watchdog: xilinx:...
248
  	{ .compatible = "xlnx,xps-timebase-wdt-1.00.a", },
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
249
250
251
252
253
254
255
  	{ .compatible = "xlnx,xps-timebase-wdt-1.01.a", },
  	{},
  };
  MODULE_DEVICE_TABLE(of, xwdt_of_match);
  
  static struct platform_driver xwdt_driver = {
  	.probe       = xwdt_probe,
82268714b   Bill Pemberton   watchdog: remove ...
256
  	.remove      = xwdt_remove,
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
257
  	.driver = {
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
258
259
260
261
  		.name  = WATCHDOG_NAME,
  		.of_match_table = xwdt_of_match,
  	},
  };
b8ec61189   Axel Lin   watchdog: convert...
262
  module_platform_driver(xwdt_driver);
e9659e69b   Alejandro Cabrera   watchdog: Add Xil...
263
264
265
  
  MODULE_AUTHOR("Alejandro Cabrera <aldaya@gmail.com>");
  MODULE_DESCRIPTION("Xilinx Watchdog driver");
9419c07cc   Michal Simek   watchdog: xilinx:...
266
  MODULE_LICENSE("GPL v2");