blob: 0e6c188f55ae7c867034217c022ac384ff8e485f [file] [log] [blame]
Alejandro Cabrerae9659e62011-06-02 22:13:11 +01001/*
Michal Simek9419c072013-05-31 07:56:33 +02002 * Watchdog Device Driver for Xilinx axi/xps_timebase_wdt
3 *
Michal Simekd14fd962014-02-12 14:34:32 +01004 * (C) Copyright 2013 - 2014 Xilinx, Inc.
Michal Simek9419c072013-05-31 07:56:33 +02005 * (C) Copyright 2011 (Alejandro Cabrera <aldaya@gmail.com>)
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010012
Joe Perches27c766a2012-02-15 15:06:19 -080013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010015#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/kernel.h>
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010018#include <linux/ioport.h>
19#include <linux/watchdog.h>
20#include <linux/io.h>
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010021#include <linux/of.h>
22#include <linux/of_device.h>
23#include <linux/of_address.h>
24
25/* Register offsets for the Wdt device */
26#define XWT_TWCSR0_OFFSET 0x0 /* Control/Status Register0 */
27#define XWT_TWCSR1_OFFSET 0x4 /* Control/Status Register1 */
28#define XWT_TBR_OFFSET 0x8 /* Timebase Register Offset */
29
30/* Control/Status Register Masks */
31#define XWT_CSR0_WRS_MASK 0x00000008 /* Reset status */
32#define XWT_CSR0_WDS_MASK 0x00000004 /* Timer state */
33#define XWT_CSR0_EWDT1_MASK 0x00000002 /* Enable bit 1 */
34
35/* Control/Status Register 0/1 bits */
36#define XWT_CSRX_EWDT2_MASK 0x00000001 /* Enable bit 2 */
37
38/* SelfTest constants */
39#define XWT_MAX_SELFTEST_LOOP_COUNT 0x00010000
40#define XWT_TIMER_FAILED 0xFFFFFFFF
41
42#define WATCHDOG_NAME "Xilinx Watchdog"
43#define PFX WATCHDOG_NAME ": "
44
45struct xwdt_device {
46 struct resource res;
47 void __iomem *base;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010048 u32 wdt_interval;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010049};
50
51static struct xwdt_device xdev;
52
53static u32 timeout;
54static u32 control_status_reg;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010055static u8 no_timeout;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010056
57static DEFINE_SPINLOCK(spinlock);
58
Michal Simekd14fd962014-02-12 14:34:32 +010059static int xilinx_wdt_start(struct watchdog_device *wdd)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010060{
61 spin_lock(&spinlock);
62
63 /* Clean previous status and enable the watchdog timer */
64 control_status_reg = ioread32(xdev.base + XWT_TWCSR0_OFFSET);
65 control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
66
67 iowrite32((control_status_reg | XWT_CSR0_EWDT1_MASK),
68 xdev.base + XWT_TWCSR0_OFFSET);
69
70 iowrite32(XWT_CSRX_EWDT2_MASK, xdev.base + XWT_TWCSR1_OFFSET);
71
72 spin_unlock(&spinlock);
Michal Simekd14fd962014-02-12 14:34:32 +010073
74 return 0;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010075}
76
Michal Simekd14fd962014-02-12 14:34:32 +010077static int xilinx_wdt_stop(struct watchdog_device *wdd)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010078{
79 spin_lock(&spinlock);
80
81 control_status_reg = ioread32(xdev.base + XWT_TWCSR0_OFFSET);
82
83 iowrite32((control_status_reg & ~XWT_CSR0_EWDT1_MASK),
84 xdev.base + XWT_TWCSR0_OFFSET);
85
86 iowrite32(0, xdev.base + XWT_TWCSR1_OFFSET);
87
88 spin_unlock(&spinlock);
Joe Perches27c766a2012-02-15 15:06:19 -080089 pr_info("Stopped!\n");
Michal Simekd14fd962014-02-12 14:34:32 +010090
91 return 0;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010092}
93
Michal Simekd14fd962014-02-12 14:34:32 +010094static int xilinx_wdt_keepalive(struct watchdog_device *wdd)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010095{
96 spin_lock(&spinlock);
97
98 control_status_reg = ioread32(xdev.base + XWT_TWCSR0_OFFSET);
99 control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
100 iowrite32(control_status_reg, xdev.base + XWT_TWCSR0_OFFSET);
101
102 spin_unlock(&spinlock);
Michal Simekd14fd962014-02-12 14:34:32 +0100103
104 return 0;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100105}
106
Michal Simekd14fd962014-02-12 14:34:32 +0100107static const struct watchdog_info xilinx_wdt_ident = {
108 .options = WDIOF_MAGICCLOSE |
109 WDIOF_KEEPALIVEPING,
110 .firmware_version = 1,
111 .identity = WATCHDOG_NAME,
112};
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100113
Michal Simekd14fd962014-02-12 14:34:32 +0100114static const struct watchdog_ops xilinx_wdt_ops = {
115 .owner = THIS_MODULE,
116 .start = xilinx_wdt_start,
117 .stop = xilinx_wdt_stop,
118 .ping = xilinx_wdt_keepalive,
119};
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100120
Michal Simekd14fd962014-02-12 14:34:32 +0100121static struct watchdog_device xilinx_wdt_wdd = {
122 .info = &xilinx_wdt_ident,
123 .ops = &xilinx_wdt_ops,
124};
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100125
126static u32 xwdt_selftest(void)
127{
128 int i;
129 u32 timer_value1;
130 u32 timer_value2;
131
132 spin_lock(&spinlock);
133
134 timer_value1 = ioread32(xdev.base + XWT_TBR_OFFSET);
135 timer_value2 = ioread32(xdev.base + XWT_TBR_OFFSET);
136
137 for (i = 0;
138 ((i <= XWT_MAX_SELFTEST_LOOP_COUNT) &&
139 (timer_value2 == timer_value1)); i++) {
140 timer_value2 = ioread32(xdev.base + XWT_TBR_OFFSET);
141 }
142
143 spin_unlock(&spinlock);
144
145 if (timer_value2 != timer_value1)
146 return ~XWT_TIMER_FAILED;
147 else
148 return XWT_TIMER_FAILED;
149}
150
Bill Pemberton2d991a12012-11-19 13:21:41 -0500151static int xwdt_probe(struct platform_device *pdev)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100152{
153 int rc;
154 u32 *tmptr;
155 u32 *pfreq;
156
157 no_timeout = 0;
158
Michal Simek90fe6c62012-06-21 08:45:40 +0200159 pfreq = (u32 *)of_get_property(pdev->dev.of_node,
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100160 "clock-frequency", NULL);
161
162 if (pfreq == NULL) {
Joe Perches27c766a2012-02-15 15:06:19 -0800163 pr_warn("The watchdog clock frequency cannot be obtained!\n");
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100164 no_timeout = 1;
165 }
166
167 rc = of_address_to_resource(pdev->dev.of_node, 0, &xdev.res);
168 if (rc) {
Joe Perches27c766a2012-02-15 15:06:19 -0800169 pr_warn("invalid address!\n");
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100170 return rc;
171 }
172
173 tmptr = (u32 *)of_get_property(pdev->dev.of_node,
174 "xlnx,wdt-interval", NULL);
175 if (tmptr == NULL) {
Joe Perches27c766a2012-02-15 15:06:19 -0800176 pr_warn("Parameter \"xlnx,wdt-interval\" not found in device tree!\n");
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100177 no_timeout = 1;
178 } else {
179 xdev.wdt_interval = *tmptr;
180 }
181
182 tmptr = (u32 *)of_get_property(pdev->dev.of_node,
183 "xlnx,wdt-enable-once", NULL);
184 if (tmptr == NULL) {
Joe Perches27c766a2012-02-15 15:06:19 -0800185 pr_warn("Parameter \"xlnx,wdt-enable-once\" not found in device tree!\n");
Michal Simekd14fd962014-02-12 14:34:32 +0100186 watchdog_set_nowayout(&xilinx_wdt_wdd, true);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100187 }
188
189/*
190 * Twice of the 2^wdt_interval / freq because the first wdt overflow is
191 * ignored (interrupt), reset is only generated at second wdt overflow
192 */
193 if (!no_timeout)
194 timeout = 2 * ((1<<xdev.wdt_interval) / *pfreq);
195
196 if (!request_mem_region(xdev.res.start,
197 xdev.res.end - xdev.res.start + 1, WATCHDOG_NAME)) {
198 rc = -ENXIO;
Joe Perches27c766a2012-02-15 15:06:19 -0800199 pr_err("memory request failure!\n");
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100200 goto err_out;
201 }
202
203 xdev.base = ioremap(xdev.res.start, xdev.res.end - xdev.res.start + 1);
204 if (xdev.base == NULL) {
205 rc = -ENOMEM;
Joe Perches27c766a2012-02-15 15:06:19 -0800206 pr_err("ioremap failure!\n");
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100207 goto release_mem;
208 }
209
210 rc = xwdt_selftest();
211 if (rc == XWT_TIMER_FAILED) {
Joe Perches27c766a2012-02-15 15:06:19 -0800212 pr_err("SelfTest routine error!\n");
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100213 goto unmap_io;
214 }
215
Michal Simekd14fd962014-02-12 14:34:32 +0100216 rc = watchdog_register_device(&xilinx_wdt_wdd);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100217 if (rc) {
Michal Simekd14fd962014-02-12 14:34:32 +0100218 pr_err("cannot register watchdog (err=%d)\n", rc);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100219 goto unmap_io;
220 }
221
Michal Simekd14fd962014-02-12 14:34:32 +0100222 dev_info(&pdev->dev, "Xilinx Watchdog Timer at %p with timeout %ds\n",
223 xdev.base, timeout);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100224
225 return 0;
226
227unmap_io:
228 iounmap(xdev.base);
229release_mem:
230 release_mem_region(xdev.res.start, resource_size(&xdev.res));
231err_out:
232 return rc;
233}
234
Bill Pemberton4b12b892012-11-19 13:26:24 -0500235static int xwdt_remove(struct platform_device *dev)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100236{
Michal Simekd14fd962014-02-12 14:34:32 +0100237 watchdog_unregister_device(&xilinx_wdt_wdd);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100238 iounmap(xdev.base);
239 release_mem_region(xdev.res.start, resource_size(&xdev.res));
240
241 return 0;
242}
243
244/* Match table for of_platform binding */
Bill Pemberton1d131362012-11-19 13:24:05 -0500245static struct of_device_id xwdt_of_match[] = {
Michal Simek8fce9b32013-05-31 07:56:34 +0200246 { .compatible = "xlnx,xps-timebase-wdt-1.00.a", },
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100247 { .compatible = "xlnx,xps-timebase-wdt-1.01.a", },
248 {},
249};
250MODULE_DEVICE_TABLE(of, xwdt_of_match);
251
252static struct platform_driver xwdt_driver = {
253 .probe = xwdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500254 .remove = xwdt_remove,
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100255 .driver = {
256 .owner = THIS_MODULE,
257 .name = WATCHDOG_NAME,
258 .of_match_table = xwdt_of_match,
259 },
260};
261
Axel Linb8ec6112011-11-29 13:56:27 +0800262module_platform_driver(xwdt_driver);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100263
264MODULE_AUTHOR("Alejandro Cabrera <aldaya@gmail.com>");
265MODULE_DESCRIPTION("Xilinx Watchdog driver");
Michal Simek9419c072013-05-31 07:56:33 +0200266MODULE_LICENSE("GPL v2");