blob: 9cab3fd4f14c286c5fb521781d7fca4787d36f5e [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;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010054static u8 no_timeout;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010055
56static DEFINE_SPINLOCK(spinlock);
57
Michal Simekd14fd962014-02-12 14:34:32 +010058static int xilinx_wdt_start(struct watchdog_device *wdd)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010059{
Michal Simek5cf4e692014-02-12 14:34:33 +010060 u32 control_status_reg;
61
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010062 spin_lock(&spinlock);
63
64 /* Clean previous status and enable the watchdog timer */
65 control_status_reg = ioread32(xdev.base + XWT_TWCSR0_OFFSET);
66 control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
67
68 iowrite32((control_status_reg | XWT_CSR0_EWDT1_MASK),
69 xdev.base + XWT_TWCSR0_OFFSET);
70
71 iowrite32(XWT_CSRX_EWDT2_MASK, xdev.base + XWT_TWCSR1_OFFSET);
72
73 spin_unlock(&spinlock);
Michal Simekd14fd962014-02-12 14:34:32 +010074
75 return 0;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010076}
77
Michal Simekd14fd962014-02-12 14:34:32 +010078static int xilinx_wdt_stop(struct watchdog_device *wdd)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010079{
Michal Simek5cf4e692014-02-12 14:34:33 +010080 u32 control_status_reg;
81
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010082 spin_lock(&spinlock);
83
84 control_status_reg = ioread32(xdev.base + XWT_TWCSR0_OFFSET);
85
86 iowrite32((control_status_reg & ~XWT_CSR0_EWDT1_MASK),
87 xdev.base + XWT_TWCSR0_OFFSET);
88
89 iowrite32(0, xdev.base + XWT_TWCSR1_OFFSET);
90
91 spin_unlock(&spinlock);
Joe Perches27c766a2012-02-15 15:06:19 -080092 pr_info("Stopped!\n");
Michal Simekd14fd962014-02-12 14:34:32 +010093
94 return 0;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010095}
96
Michal Simekd14fd962014-02-12 14:34:32 +010097static int xilinx_wdt_keepalive(struct watchdog_device *wdd)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010098{
Michal Simek5cf4e692014-02-12 14:34:33 +010099 u32 control_status_reg;
100
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100101 spin_lock(&spinlock);
102
103 control_status_reg = ioread32(xdev.base + XWT_TWCSR0_OFFSET);
104 control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
105 iowrite32(control_status_reg, xdev.base + XWT_TWCSR0_OFFSET);
106
107 spin_unlock(&spinlock);
Michal Simekd14fd962014-02-12 14:34:32 +0100108
109 return 0;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100110}
111
Michal Simekd14fd962014-02-12 14:34:32 +0100112static const struct watchdog_info xilinx_wdt_ident = {
113 .options = WDIOF_MAGICCLOSE |
114 WDIOF_KEEPALIVEPING,
115 .firmware_version = 1,
116 .identity = WATCHDOG_NAME,
117};
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100118
Michal Simekd14fd962014-02-12 14:34:32 +0100119static const struct watchdog_ops xilinx_wdt_ops = {
120 .owner = THIS_MODULE,
121 .start = xilinx_wdt_start,
122 .stop = xilinx_wdt_stop,
123 .ping = xilinx_wdt_keepalive,
124};
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100125
Michal Simekd14fd962014-02-12 14:34:32 +0100126static struct watchdog_device xilinx_wdt_wdd = {
127 .info = &xilinx_wdt_ident,
128 .ops = &xilinx_wdt_ops,
129};
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100130
131static u32 xwdt_selftest(void)
132{
133 int i;
134 u32 timer_value1;
135 u32 timer_value2;
136
137 spin_lock(&spinlock);
138
139 timer_value1 = ioread32(xdev.base + XWT_TBR_OFFSET);
140 timer_value2 = ioread32(xdev.base + XWT_TBR_OFFSET);
141
142 for (i = 0;
143 ((i <= XWT_MAX_SELFTEST_LOOP_COUNT) &&
144 (timer_value2 == timer_value1)); i++) {
145 timer_value2 = ioread32(xdev.base + XWT_TBR_OFFSET);
146 }
147
148 spin_unlock(&spinlock);
149
150 if (timer_value2 != timer_value1)
151 return ~XWT_TIMER_FAILED;
152 else
153 return XWT_TIMER_FAILED;
154}
155
Bill Pemberton2d991a12012-11-19 13:21:41 -0500156static int xwdt_probe(struct platform_device *pdev)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100157{
158 int rc;
159 u32 *tmptr;
160 u32 *pfreq;
161
162 no_timeout = 0;
163
Michal Simek90fe6c62012-06-21 08:45:40 +0200164 pfreq = (u32 *)of_get_property(pdev->dev.of_node,
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100165 "clock-frequency", NULL);
166
167 if (pfreq == NULL) {
Joe Perches27c766a2012-02-15 15:06:19 -0800168 pr_warn("The watchdog clock frequency cannot be obtained!\n");
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100169 no_timeout = 1;
170 }
171
172 rc = of_address_to_resource(pdev->dev.of_node, 0, &xdev.res);
173 if (rc) {
Joe Perches27c766a2012-02-15 15:06:19 -0800174 pr_warn("invalid address!\n");
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100175 return rc;
176 }
177
178 tmptr = (u32 *)of_get_property(pdev->dev.of_node,
179 "xlnx,wdt-interval", NULL);
180 if (tmptr == NULL) {
Joe Perches27c766a2012-02-15 15:06:19 -0800181 pr_warn("Parameter \"xlnx,wdt-interval\" not found in device tree!\n");
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100182 no_timeout = 1;
183 } else {
184 xdev.wdt_interval = *tmptr;
185 }
186
187 tmptr = (u32 *)of_get_property(pdev->dev.of_node,
188 "xlnx,wdt-enable-once", NULL);
189 if (tmptr == NULL) {
Joe Perches27c766a2012-02-15 15:06:19 -0800190 pr_warn("Parameter \"xlnx,wdt-enable-once\" not found in device tree!\n");
Michal Simekd14fd962014-02-12 14:34:32 +0100191 watchdog_set_nowayout(&xilinx_wdt_wdd, true);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100192 }
193
194/*
195 * Twice of the 2^wdt_interval / freq because the first wdt overflow is
196 * ignored (interrupt), reset is only generated at second wdt overflow
197 */
198 if (!no_timeout)
199 timeout = 2 * ((1<<xdev.wdt_interval) / *pfreq);
200
201 if (!request_mem_region(xdev.res.start,
202 xdev.res.end - xdev.res.start + 1, WATCHDOG_NAME)) {
203 rc = -ENXIO;
Joe Perches27c766a2012-02-15 15:06:19 -0800204 pr_err("memory request failure!\n");
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100205 goto err_out;
206 }
207
208 xdev.base = ioremap(xdev.res.start, xdev.res.end - xdev.res.start + 1);
209 if (xdev.base == NULL) {
210 rc = -ENOMEM;
Joe Perches27c766a2012-02-15 15:06:19 -0800211 pr_err("ioremap failure!\n");
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100212 goto release_mem;
213 }
214
215 rc = xwdt_selftest();
216 if (rc == XWT_TIMER_FAILED) {
Joe Perches27c766a2012-02-15 15:06:19 -0800217 pr_err("SelfTest routine error!\n");
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100218 goto unmap_io;
219 }
220
Michal Simekd14fd962014-02-12 14:34:32 +0100221 rc = watchdog_register_device(&xilinx_wdt_wdd);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100222 if (rc) {
Michal Simekd14fd962014-02-12 14:34:32 +0100223 pr_err("cannot register watchdog (err=%d)\n", rc);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100224 goto unmap_io;
225 }
226
Michal Simekd14fd962014-02-12 14:34:32 +0100227 dev_info(&pdev->dev, "Xilinx Watchdog Timer at %p with timeout %ds\n",
228 xdev.base, timeout);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100229
230 return 0;
231
232unmap_io:
233 iounmap(xdev.base);
234release_mem:
235 release_mem_region(xdev.res.start, resource_size(&xdev.res));
236err_out:
237 return rc;
238}
239
Bill Pemberton4b12b892012-11-19 13:26:24 -0500240static int xwdt_remove(struct platform_device *dev)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100241{
Michal Simekd14fd962014-02-12 14:34:32 +0100242 watchdog_unregister_device(&xilinx_wdt_wdd);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100243 iounmap(xdev.base);
244 release_mem_region(xdev.res.start, resource_size(&xdev.res));
245
246 return 0;
247}
248
249/* Match table for of_platform binding */
Bill Pemberton1d131362012-11-19 13:24:05 -0500250static struct of_device_id xwdt_of_match[] = {
Michal Simek8fce9b32013-05-31 07:56:34 +0200251 { .compatible = "xlnx,xps-timebase-wdt-1.00.a", },
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100252 { .compatible = "xlnx,xps-timebase-wdt-1.01.a", },
253 {},
254};
255MODULE_DEVICE_TABLE(of, xwdt_of_match);
256
257static struct platform_driver xwdt_driver = {
258 .probe = xwdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500259 .remove = xwdt_remove,
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100260 .driver = {
261 .owner = THIS_MODULE,
262 .name = WATCHDOG_NAME,
263 .of_match_table = xwdt_of_match,
264 },
265};
266
Axel Linb8ec6112011-11-29 13:56:27 +0800267module_platform_driver(xwdt_driver);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100268
269MODULE_AUTHOR("Alejandro Cabrera <aldaya@gmail.com>");
270MODULE_DESCRIPTION("Xilinx Watchdog driver");
Michal Simek9419c072013-05-31 07:56:33 +0200271MODULE_LICENSE("GPL v2");