blob: ac3f6ebd0c23e811fc1f94f577af12e66c157461 [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;
Michal Simek90663172014-02-12 14:41:19 +010049 spinlock_t spinlock;
50 struct watchdog_device xilinx_wdt_wdd;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010051};
52
Michal Simekd14fd962014-02-12 14:34:32 +010053static int xilinx_wdt_start(struct watchdog_device *wdd)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010054{
Michal Simek5cf4e692014-02-12 14:34:33 +010055 u32 control_status_reg;
Michal Simek90663172014-02-12 14:41:19 +010056 struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
Michal Simek5cf4e692014-02-12 14:34:33 +010057
Michal Simek90663172014-02-12 14:41:19 +010058 spin_lock(&xdev->spinlock);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010059
60 /* Clean previous status and enable the watchdog timer */
Michal Simek90663172014-02-12 14:41:19 +010061 control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010062 control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
63
64 iowrite32((control_status_reg | XWT_CSR0_EWDT1_MASK),
Michal Simek90663172014-02-12 14:41:19 +010065 xdev->base + XWT_TWCSR0_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010066
Michal Simek90663172014-02-12 14:41:19 +010067 iowrite32(XWT_CSRX_EWDT2_MASK, xdev->base + XWT_TWCSR1_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010068
Michal Simek90663172014-02-12 14:41:19 +010069 spin_unlock(&xdev->spinlock);
Michal Simekd14fd962014-02-12 14:34:32 +010070
71 return 0;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010072}
73
Michal Simekd14fd962014-02-12 14:34:32 +010074static int xilinx_wdt_stop(struct watchdog_device *wdd)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010075{
Michal Simek5cf4e692014-02-12 14:34:33 +010076 u32 control_status_reg;
Michal Simek90663172014-02-12 14:41:19 +010077 struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
Michal Simek5cf4e692014-02-12 14:34:33 +010078
Michal Simek90663172014-02-12 14:41:19 +010079 spin_lock(&xdev->spinlock);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010080
Michal Simek90663172014-02-12 14:41:19 +010081 control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010082
83 iowrite32((control_status_reg & ~XWT_CSR0_EWDT1_MASK),
Michal Simek90663172014-02-12 14:41:19 +010084 xdev->base + XWT_TWCSR0_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010085
Michal Simek90663172014-02-12 14:41:19 +010086 iowrite32(0, xdev->base + XWT_TWCSR1_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010087
Michal Simek90663172014-02-12 14:41:19 +010088 spin_unlock(&xdev->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{
Michal Simek5cf4e692014-02-12 14:34:33 +010096 u32 control_status_reg;
Michal Simek90663172014-02-12 14:41:19 +010097 struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
Michal Simek5cf4e692014-02-12 14:34:33 +010098
Michal Simek90663172014-02-12 14:41:19 +010099 spin_lock(&xdev->spinlock);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100100
Michal Simek90663172014-02-12 14:41:19 +0100101 control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100102 control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
Michal Simek90663172014-02-12 14:41:19 +0100103 iowrite32(control_status_reg, xdev->base + XWT_TWCSR0_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100104
Michal Simek90663172014-02-12 14:41:19 +0100105 spin_unlock(&xdev->spinlock);
Michal Simekd14fd962014-02-12 14:34:32 +0100106
107 return 0;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100108}
109
Michal Simekd14fd962014-02-12 14:34:32 +0100110static const struct watchdog_info xilinx_wdt_ident = {
111 .options = WDIOF_MAGICCLOSE |
112 WDIOF_KEEPALIVEPING,
113 .firmware_version = 1,
114 .identity = WATCHDOG_NAME,
115};
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100116
Michal Simekd14fd962014-02-12 14:34:32 +0100117static const struct watchdog_ops xilinx_wdt_ops = {
118 .owner = THIS_MODULE,
119 .start = xilinx_wdt_start,
120 .stop = xilinx_wdt_stop,
121 .ping = xilinx_wdt_keepalive,
122};
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100123
Michal Simek90663172014-02-12 14:41:19 +0100124static u32 xwdt_selftest(struct xwdt_device *xdev)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100125{
126 int i;
127 u32 timer_value1;
128 u32 timer_value2;
129
Michal Simek90663172014-02-12 14:41:19 +0100130 spin_lock(&xdev->spinlock);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100131
Michal Simek90663172014-02-12 14:41:19 +0100132 timer_value1 = ioread32(xdev->base + XWT_TBR_OFFSET);
133 timer_value2 = ioread32(xdev->base + XWT_TBR_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100134
135 for (i = 0;
136 ((i <= XWT_MAX_SELFTEST_LOOP_COUNT) &&
137 (timer_value2 == timer_value1)); i++) {
Michal Simek90663172014-02-12 14:41:19 +0100138 timer_value2 = ioread32(xdev->base + XWT_TBR_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100139 }
140
Michal Simek90663172014-02-12 14:41:19 +0100141 spin_unlock(&xdev->spinlock);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100142
143 if (timer_value2 != timer_value1)
144 return ~XWT_TIMER_FAILED;
145 else
146 return XWT_TIMER_FAILED;
147}
148
Bill Pemberton2d991a12012-11-19 13:21:41 -0500149static int xwdt_probe(struct platform_device *pdev)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100150{
151 int rc;
152 u32 *tmptr;
153 u32 *pfreq;
Michal Simekf06cdfd2014-02-12 14:34:34 +0100154 struct resource *res;
Michal Simek90663172014-02-12 14:41:19 +0100155 struct xwdt_device *xdev;
Michal Simekffb8eee2014-02-12 14:34:35 +0100156 bool no_timeout = false;
Michal Simek90663172014-02-12 14:41:19 +0100157 struct watchdog_device *xilinx_wdt_wdd;
158
159 xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
160 if (!xdev)
161 return -ENOMEM;
162
163 xilinx_wdt_wdd = &xdev->xilinx_wdt_wdd;
164 xilinx_wdt_wdd->info = &xilinx_wdt_ident;
165 xilinx_wdt_wdd->ops = &xilinx_wdt_ops;
166 xilinx_wdt_wdd->parent = &pdev->dev;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100167
Michal Simekf06cdfd2014-02-12 14:34:34 +0100168 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Michal Simek90663172014-02-12 14:41:19 +0100169 xdev->base = devm_ioremap_resource(&pdev->dev, res);
170 if (IS_ERR(xdev->base))
171 return PTR_ERR(xdev->base);
Michal Simekf06cdfd2014-02-12 14:34:34 +0100172
Michal Simek90fe6c62012-06-21 08:45:40 +0200173 pfreq = (u32 *)of_get_property(pdev->dev.of_node,
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100174 "clock-frequency", NULL);
175
176 if (pfreq == NULL) {
Joe Perches27c766a2012-02-15 15:06:19 -0800177 pr_warn("The watchdog clock frequency cannot be obtained!\n");
Michal Simekffb8eee2014-02-12 14:34:35 +0100178 no_timeout = true;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100179 }
180
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100181 tmptr = (u32 *)of_get_property(pdev->dev.of_node,
182 "xlnx,wdt-interval", NULL);
183 if (tmptr == NULL) {
Joe Perches27c766a2012-02-15 15:06:19 -0800184 pr_warn("Parameter \"xlnx,wdt-interval\" not found in device tree!\n");
Michal Simekffb8eee2014-02-12 14:34:35 +0100185 no_timeout = true;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100186 } else {
Michal Simek90663172014-02-12 14:41:19 +0100187 xdev->wdt_interval = *tmptr;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100188 }
189
190 tmptr = (u32 *)of_get_property(pdev->dev.of_node,
191 "xlnx,wdt-enable-once", NULL);
192 if (tmptr == NULL) {
Joe Perches27c766a2012-02-15 15:06:19 -0800193 pr_warn("Parameter \"xlnx,wdt-enable-once\" not found in device tree!\n");
Michal Simek90663172014-02-12 14:41:19 +0100194 watchdog_set_nowayout(xilinx_wdt_wdd, true);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100195 }
196
197/*
198 * Twice of the 2^wdt_interval / freq because the first wdt overflow is
199 * ignored (interrupt), reset is only generated at second wdt overflow
200 */
201 if (!no_timeout)
Michal Simek90663172014-02-12 14:41:19 +0100202 xilinx_wdt_wdd->timeout = 2 * ((1 << xdev->wdt_interval) /
203 *pfreq);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100204
Michal Simek90663172014-02-12 14:41:19 +0100205 spin_lock_init(&xdev->spinlock);
206 watchdog_set_drvdata(xilinx_wdt_wdd, xdev);
207
208 rc = xwdt_selftest(xdev);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100209 if (rc == XWT_TIMER_FAILED) {
Joe Perches27c766a2012-02-15 15:06:19 -0800210 pr_err("SelfTest routine error!\n");
Michal Simekf06cdfd2014-02-12 14:34:34 +0100211 return rc;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100212 }
213
Michal Simek90663172014-02-12 14:41:19 +0100214 rc = watchdog_register_device(xilinx_wdt_wdd);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100215 if (rc) {
Michal Simekd14fd962014-02-12 14:34:32 +0100216 pr_err("cannot register watchdog (err=%d)\n", rc);
Michal Simekf06cdfd2014-02-12 14:34:34 +0100217 return rc;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100218 }
219
Michal Simekd14fd962014-02-12 14:34:32 +0100220 dev_info(&pdev->dev, "Xilinx Watchdog Timer at %p with timeout %ds\n",
Michal Simek90663172014-02-12 14:41:19 +0100221 xdev->base, xilinx_wdt_wdd->timeout);
222
223 platform_set_drvdata(pdev, xdev);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100224
225 return 0;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100226}
227
Michal Simek90663172014-02-12 14:41:19 +0100228static int xwdt_remove(struct platform_device *pdev)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100229{
Michal Simek90663172014-02-12 14:41:19 +0100230 struct xwdt_device *xdev = platform_get_drvdata(pdev);
231
232 watchdog_unregister_device(&xdev->xilinx_wdt_wdd);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100233
234 return 0;
235}
236
237/* Match table for of_platform binding */
Bill Pemberton1d131362012-11-19 13:24:05 -0500238static struct of_device_id xwdt_of_match[] = {
Michal Simek8fce9b32013-05-31 07:56:34 +0200239 { .compatible = "xlnx,xps-timebase-wdt-1.00.a", },
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100240 { .compatible = "xlnx,xps-timebase-wdt-1.01.a", },
241 {},
242};
243MODULE_DEVICE_TABLE(of, xwdt_of_match);
244
245static struct platform_driver xwdt_driver = {
246 .probe = xwdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500247 .remove = xwdt_remove,
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100248 .driver = {
249 .owner = THIS_MODULE,
250 .name = WATCHDOG_NAME,
251 .of_match_table = xwdt_of_match,
252 },
253};
254
Axel Linb8ec6112011-11-29 13:56:27 +0800255module_platform_driver(xwdt_driver);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100256
257MODULE_AUTHOR("Alejandro Cabrera <aldaya@gmail.com>");
258MODULE_DESCRIPTION("Xilinx Watchdog driver");
Michal Simek9419c072013-05-31 07:56:33 +0200259MODULE_LICENSE("GPL v2");