blob: 3bc648f40e1f248e09170293c22bc183dc8fd269 [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
Michal Simekf06cdfd2014-02-12 14:34:34 +010015#include <linux/err.h>
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010016#include <linux/module.h>
17#include <linux/types.h>
18#include <linux/kernel.h>
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010019#include <linux/ioport.h>
20#include <linux/watchdog.h>
21#include <linux/io.h>
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010022#include <linux/of.h>
23#include <linux/of_device.h>
24#include <linux/of_address.h>
25
26/* Register offsets for the Wdt device */
27#define XWT_TWCSR0_OFFSET 0x0 /* Control/Status Register0 */
28#define XWT_TWCSR1_OFFSET 0x4 /* Control/Status Register1 */
29#define XWT_TBR_OFFSET 0x8 /* Timebase Register Offset */
30
31/* Control/Status Register Masks */
32#define XWT_CSR0_WRS_MASK 0x00000008 /* Reset status */
33#define XWT_CSR0_WDS_MASK 0x00000004 /* Timer state */
34#define XWT_CSR0_EWDT1_MASK 0x00000002 /* Enable bit 1 */
35
36/* Control/Status Register 0/1 bits */
37#define XWT_CSRX_EWDT2_MASK 0x00000001 /* Enable bit 2 */
38
39/* SelfTest constants */
40#define XWT_MAX_SELFTEST_LOOP_COUNT 0x00010000
41#define XWT_TIMER_FAILED 0xFFFFFFFF
42
43#define WATCHDOG_NAME "Xilinx Watchdog"
44#define PFX WATCHDOG_NAME ": "
45
46struct xwdt_device {
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010047 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 +010054
55static DEFINE_SPINLOCK(spinlock);
56
Michal Simekd14fd962014-02-12 14:34:32 +010057static int xilinx_wdt_start(struct watchdog_device *wdd)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010058{
Michal Simek5cf4e692014-02-12 14:34:33 +010059 u32 control_status_reg;
60
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010061 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{
Michal Simek5cf4e692014-02-12 14:34:33 +010079 u32 control_status_reg;
80
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010081 spin_lock(&spinlock);
82
83 control_status_reg = ioread32(xdev.base + XWT_TWCSR0_OFFSET);
84
85 iowrite32((control_status_reg & ~XWT_CSR0_EWDT1_MASK),
86 xdev.base + XWT_TWCSR0_OFFSET);
87
88 iowrite32(0, xdev.base + XWT_TWCSR1_OFFSET);
89
90 spin_unlock(&spinlock);
Joe Perches27c766a2012-02-15 15:06:19 -080091 pr_info("Stopped!\n");
Michal Simekd14fd962014-02-12 14:34:32 +010092
93 return 0;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010094}
95
Michal Simekd14fd962014-02-12 14:34:32 +010096static int xilinx_wdt_keepalive(struct watchdog_device *wdd)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010097{
Michal Simek5cf4e692014-02-12 14:34:33 +010098 u32 control_status_reg;
99
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100100 spin_lock(&spinlock);
101
102 control_status_reg = ioread32(xdev.base + XWT_TWCSR0_OFFSET);
103 control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
104 iowrite32(control_status_reg, xdev.base + XWT_TWCSR0_OFFSET);
105
106 spin_unlock(&spinlock);
Michal Simekd14fd962014-02-12 14:34:32 +0100107
108 return 0;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100109}
110
Michal Simekd14fd962014-02-12 14:34:32 +0100111static const struct watchdog_info xilinx_wdt_ident = {
112 .options = WDIOF_MAGICCLOSE |
113 WDIOF_KEEPALIVEPING,
114 .firmware_version = 1,
115 .identity = WATCHDOG_NAME,
116};
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100117
Michal Simekd14fd962014-02-12 14:34:32 +0100118static const struct watchdog_ops xilinx_wdt_ops = {
119 .owner = THIS_MODULE,
120 .start = xilinx_wdt_start,
121 .stop = xilinx_wdt_stop,
122 .ping = xilinx_wdt_keepalive,
123};
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100124
Michal Simekd14fd962014-02-12 14:34:32 +0100125static struct watchdog_device xilinx_wdt_wdd = {
126 .info = &xilinx_wdt_ident,
127 .ops = &xilinx_wdt_ops,
128};
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100129
130static u32 xwdt_selftest(void)
131{
132 int i;
133 u32 timer_value1;
134 u32 timer_value2;
135
136 spin_lock(&spinlock);
137
138 timer_value1 = ioread32(xdev.base + XWT_TBR_OFFSET);
139 timer_value2 = ioread32(xdev.base + XWT_TBR_OFFSET);
140
141 for (i = 0;
142 ((i <= XWT_MAX_SELFTEST_LOOP_COUNT) &&
143 (timer_value2 == timer_value1)); i++) {
144 timer_value2 = ioread32(xdev.base + XWT_TBR_OFFSET);
145 }
146
147 spin_unlock(&spinlock);
148
149 if (timer_value2 != timer_value1)
150 return ~XWT_TIMER_FAILED;
151 else
152 return XWT_TIMER_FAILED;
153}
154
Bill Pemberton2d991a12012-11-19 13:21:41 -0500155static int xwdt_probe(struct platform_device *pdev)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100156{
157 int rc;
158 u32 *tmptr;
159 u32 *pfreq;
Michal Simekf06cdfd2014-02-12 14:34:34 +0100160 struct resource *res;
Michal Simekffb8eee2014-02-12 14:34:35 +0100161 bool no_timeout = false;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100162
Michal Simekf06cdfd2014-02-12 14:34:34 +0100163 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
164 xdev.base = devm_ioremap_resource(&pdev->dev, res);
165 if (IS_ERR(xdev.base))
166 return PTR_ERR(xdev.base);
167
Michal Simek90fe6c62012-06-21 08:45:40 +0200168 pfreq = (u32 *)of_get_property(pdev->dev.of_node,
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100169 "clock-frequency", NULL);
170
171 if (pfreq == NULL) {
Joe Perches27c766a2012-02-15 15:06:19 -0800172 pr_warn("The watchdog clock frequency cannot be obtained!\n");
Michal Simekffb8eee2014-02-12 14:34:35 +0100173 no_timeout = true;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100174 }
175
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100176 tmptr = (u32 *)of_get_property(pdev->dev.of_node,
177 "xlnx,wdt-interval", NULL);
178 if (tmptr == NULL) {
Joe Perches27c766a2012-02-15 15:06:19 -0800179 pr_warn("Parameter \"xlnx,wdt-interval\" not found in device tree!\n");
Michal Simekffb8eee2014-02-12 14:34:35 +0100180 no_timeout = true;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100181 } else {
182 xdev.wdt_interval = *tmptr;
183 }
184
185 tmptr = (u32 *)of_get_property(pdev->dev.of_node,
186 "xlnx,wdt-enable-once", NULL);
187 if (tmptr == NULL) {
Joe Perches27c766a2012-02-15 15:06:19 -0800188 pr_warn("Parameter \"xlnx,wdt-enable-once\" not found in device tree!\n");
Michal Simekd14fd962014-02-12 14:34:32 +0100189 watchdog_set_nowayout(&xilinx_wdt_wdd, true);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100190 }
191
192/*
193 * Twice of the 2^wdt_interval / freq because the first wdt overflow is
194 * ignored (interrupt), reset is only generated at second wdt overflow
195 */
196 if (!no_timeout)
197 timeout = 2 * ((1<<xdev.wdt_interval) / *pfreq);
198
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100199 rc = xwdt_selftest();
200 if (rc == XWT_TIMER_FAILED) {
Joe Perches27c766a2012-02-15 15:06:19 -0800201 pr_err("SelfTest routine error!\n");
Michal Simekf06cdfd2014-02-12 14:34:34 +0100202 return rc;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100203 }
204
Michal Simekd14fd962014-02-12 14:34:32 +0100205 rc = watchdog_register_device(&xilinx_wdt_wdd);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100206 if (rc) {
Michal Simekd14fd962014-02-12 14:34:32 +0100207 pr_err("cannot register watchdog (err=%d)\n", rc);
Michal Simekf06cdfd2014-02-12 14:34:34 +0100208 return rc;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100209 }
210
Michal Simekd14fd962014-02-12 14:34:32 +0100211 dev_info(&pdev->dev, "Xilinx Watchdog Timer at %p with timeout %ds\n",
212 xdev.base, timeout);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100213
214 return 0;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100215}
216
Bill Pemberton4b12b892012-11-19 13:26:24 -0500217static int xwdt_remove(struct platform_device *dev)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100218{
Michal Simekd14fd962014-02-12 14:34:32 +0100219 watchdog_unregister_device(&xilinx_wdt_wdd);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100220
221 return 0;
222}
223
224/* Match table for of_platform binding */
Bill Pemberton1d131362012-11-19 13:24:05 -0500225static struct of_device_id xwdt_of_match[] = {
Michal Simek8fce9b32013-05-31 07:56:34 +0200226 { .compatible = "xlnx,xps-timebase-wdt-1.00.a", },
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100227 { .compatible = "xlnx,xps-timebase-wdt-1.01.a", },
228 {},
229};
230MODULE_DEVICE_TABLE(of, xwdt_of_match);
231
232static struct platform_driver xwdt_driver = {
233 .probe = xwdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500234 .remove = xwdt_remove,
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100235 .driver = {
236 .owner = THIS_MODULE,
237 .name = WATCHDOG_NAME,
238 .of_match_table = xwdt_of_match,
239 },
240};
241
Axel Linb8ec6112011-11-29 13:56:27 +0800242module_platform_driver(xwdt_driver);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100243
244MODULE_AUTHOR("Alejandro Cabrera <aldaya@gmail.com>");
245MODULE_DESCRIPTION("Xilinx Watchdog driver");
Michal Simek9419c072013-05-31 07:56:33 +0200246MODULE_LICENSE("GPL v2");