blob: fae7fe929ea38c299352cac82db0b161167f58f4 [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
Shubhrajyoti Datta9d6b4ef2016-08-12 12:17:01 +053013#include <linux/clk.h>
Michal Simekf06cdfd2014-02-12 14:34:34 +010014#include <linux/err.h>
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"
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010043
44struct xwdt_device {
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010045 void __iomem *base;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010046 u32 wdt_interval;
Michal Simek90663172014-02-12 14:41:19 +010047 spinlock_t spinlock;
48 struct watchdog_device xilinx_wdt_wdd;
Shubhrajyoti Datta9d6b4ef2016-08-12 12:17:01 +053049 struct clk *clk;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010050};
51
Michal Simekd14fd962014-02-12 14:34:32 +010052static int xilinx_wdt_start(struct watchdog_device *wdd)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010053{
Michal Simek5cf4e692014-02-12 14:34:33 +010054 u32 control_status_reg;
Michal Simek90663172014-02-12 14:41:19 +010055 struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
Michal Simek5cf4e692014-02-12 14:34:33 +010056
Michal Simek90663172014-02-12 14:41:19 +010057 spin_lock(&xdev->spinlock);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010058
59 /* Clean previous status and enable the watchdog timer */
Michal Simek90663172014-02-12 14:41:19 +010060 control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010061 control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
62
63 iowrite32((control_status_reg | XWT_CSR0_EWDT1_MASK),
Michal Simek90663172014-02-12 14:41:19 +010064 xdev->base + XWT_TWCSR0_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010065
Michal Simek90663172014-02-12 14:41:19 +010066 iowrite32(XWT_CSRX_EWDT2_MASK, xdev->base + XWT_TWCSR1_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010067
Michal Simek90663172014-02-12 14:41:19 +010068 spin_unlock(&xdev->spinlock);
Michal Simekd14fd962014-02-12 14:34:32 +010069
70 return 0;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010071}
72
Michal Simekd14fd962014-02-12 14:34:32 +010073static int xilinx_wdt_stop(struct watchdog_device *wdd)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010074{
Michal Simek5cf4e692014-02-12 14:34:33 +010075 u32 control_status_reg;
Michal Simek90663172014-02-12 14:41:19 +010076 struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
Michal Simek5cf4e692014-02-12 14:34:33 +010077
Michal Simek90663172014-02-12 14:41:19 +010078 spin_lock(&xdev->spinlock);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010079
Michal Simek90663172014-02-12 14:41:19 +010080 control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010081
82 iowrite32((control_status_reg & ~XWT_CSR0_EWDT1_MASK),
Michal Simek90663172014-02-12 14:41:19 +010083 xdev->base + XWT_TWCSR0_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010084
Michal Simek90663172014-02-12 14:41:19 +010085 iowrite32(0, xdev->base + XWT_TWCSR1_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010086
Michal Simek90663172014-02-12 14:41:19 +010087 spin_unlock(&xdev->spinlock);
Joe Perches27c766a2012-02-15 15:06:19 -080088 pr_info("Stopped!\n");
Michal Simekd14fd962014-02-12 14:34:32 +010089
90 return 0;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010091}
92
Michal Simekd14fd962014-02-12 14:34:32 +010093static int xilinx_wdt_keepalive(struct watchdog_device *wdd)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010094{
Michal Simek5cf4e692014-02-12 14:34:33 +010095 u32 control_status_reg;
Michal Simek90663172014-02-12 14:41:19 +010096 struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
Michal Simek5cf4e692014-02-12 14:34:33 +010097
Michal Simek90663172014-02-12 14:41:19 +010098 spin_lock(&xdev->spinlock);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010099
Michal Simek90663172014-02-12 14:41:19 +0100100 control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100101 control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
Michal Simek90663172014-02-12 14:41:19 +0100102 iowrite32(control_status_reg, xdev->base + XWT_TWCSR0_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100103
Michal Simek90663172014-02-12 14:41:19 +0100104 spin_unlock(&xdev->spinlock);
Michal Simekd14fd962014-02-12 14:34:32 +0100105
106 return 0;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100107}
108
Michal Simekd14fd962014-02-12 14:34:32 +0100109static const struct watchdog_info xilinx_wdt_ident = {
110 .options = WDIOF_MAGICCLOSE |
111 WDIOF_KEEPALIVEPING,
112 .firmware_version = 1,
113 .identity = WATCHDOG_NAME,
114};
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100115
Michal Simekd14fd962014-02-12 14:34:32 +0100116static const struct watchdog_ops xilinx_wdt_ops = {
117 .owner = THIS_MODULE,
118 .start = xilinx_wdt_start,
119 .stop = xilinx_wdt_stop,
120 .ping = xilinx_wdt_keepalive,
121};
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100122
Michal Simek90663172014-02-12 14:41:19 +0100123static u32 xwdt_selftest(struct xwdt_device *xdev)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100124{
125 int i;
126 u32 timer_value1;
127 u32 timer_value2;
128
Michal Simek90663172014-02-12 14:41:19 +0100129 spin_lock(&xdev->spinlock);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100130
Michal Simek90663172014-02-12 14:41:19 +0100131 timer_value1 = ioread32(xdev->base + XWT_TBR_OFFSET);
132 timer_value2 = ioread32(xdev->base + XWT_TBR_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100133
134 for (i = 0;
135 ((i <= XWT_MAX_SELFTEST_LOOP_COUNT) &&
136 (timer_value2 == timer_value1)); i++) {
Michal Simek90663172014-02-12 14:41:19 +0100137 timer_value2 = ioread32(xdev->base + XWT_TBR_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100138 }
139
Michal Simek90663172014-02-12 14:41:19 +0100140 spin_unlock(&xdev->spinlock);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100141
142 if (timer_value2 != timer_value1)
143 return ~XWT_TIMER_FAILED;
144 else
145 return XWT_TIMER_FAILED;
146}
147
Bill Pemberton2d991a12012-11-19 13:21:41 -0500148static int xwdt_probe(struct platform_device *pdev)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100149{
150 int rc;
Michal Simek8d6a1402014-02-12 14:41:25 +0100151 u32 pfreq = 0, enable_once = 0;
Michal Simekf06cdfd2014-02-12 14:34:34 +0100152 struct resource *res;
Michal Simek90663172014-02-12 14:41:19 +0100153 struct xwdt_device *xdev;
Michal Simek90663172014-02-12 14:41:19 +0100154 struct watchdog_device *xilinx_wdt_wdd;
155
156 xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
157 if (!xdev)
158 return -ENOMEM;
159
160 xilinx_wdt_wdd = &xdev->xilinx_wdt_wdd;
161 xilinx_wdt_wdd->info = &xilinx_wdt_ident;
162 xilinx_wdt_wdd->ops = &xilinx_wdt_ops;
163 xilinx_wdt_wdd->parent = &pdev->dev;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100164
Michal Simekf06cdfd2014-02-12 14:34:34 +0100165 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Michal Simek90663172014-02-12 14:41:19 +0100166 xdev->base = devm_ioremap_resource(&pdev->dev, res);
167 if (IS_ERR(xdev->base))
168 return PTR_ERR(xdev->base);
Michal Simekf06cdfd2014-02-12 14:34:34 +0100169
Michal Simek2e79a362014-02-12 14:41:21 +0100170 rc = of_property_read_u32(pdev->dev.of_node, "clock-frequency", &pfreq);
Michal Simek8d6a1402014-02-12 14:41:25 +0100171 if (rc)
Michal Simek4c7fbbc2014-02-12 14:41:20 +0100172 dev_warn(&pdev->dev,
173 "The watchdog clock frequency cannot be obtained\n");
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100174
Michal Simek2e79a362014-02-12 14:41:21 +0100175 rc = of_property_read_u32(pdev->dev.of_node, "xlnx,wdt-interval",
176 &xdev->wdt_interval);
Michal Simek8d6a1402014-02-12 14:41:25 +0100177 if (rc)
Michal Simek4c7fbbc2014-02-12 14:41:20 +0100178 dev_warn(&pdev->dev,
179 "Parameter \"xlnx,wdt-interval\" not found\n");
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100180
Michal Simek2e79a362014-02-12 14:41:21 +0100181 rc = of_property_read_u32(pdev->dev.of_node, "xlnx,wdt-enable-once",
182 &enable_once);
183 if (rc)
Michal Simek4c7fbbc2014-02-12 14:41:20 +0100184 dev_warn(&pdev->dev,
185 "Parameter \"xlnx,wdt-enable-once\" not found\n");
Michal Simek2e79a362014-02-12 14:41:21 +0100186
187 watchdog_set_nowayout(xilinx_wdt_wdd, enable_once);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100188
Michal Simek75b3c5a2014-02-12 14:41:22 +0100189 /*
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 */
Michal Simek8d6a1402014-02-12 14:41:25 +0100193 if (pfreq && xdev->wdt_interval)
Michal Simek90663172014-02-12 14:41:19 +0100194 xilinx_wdt_wdd->timeout = 2 * ((1 << xdev->wdt_interval) /
Michal Simek2e79a362014-02-12 14:41:21 +0100195 pfreq);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100196
Michal Simek90663172014-02-12 14:41:19 +0100197 spin_lock_init(&xdev->spinlock);
198 watchdog_set_drvdata(xilinx_wdt_wdd, xdev);
199
Shubhrajyoti Datta9d6b4ef2016-08-12 12:17:01 +0530200 xdev->clk = devm_clk_get(&pdev->dev, NULL);
201 if (IS_ERR(xdev->clk)) {
202 if (PTR_ERR(xdev->clk) == -ENOENT)
203 xdev->clk = NULL;
204 else
205 return PTR_ERR(xdev->clk);
206 }
207
208 rc = clk_prepare_enable(xdev->clk);
209 if (rc) {
210 dev_err(&pdev->dev, "unable to enable clock\n");
211 return rc;
212 }
213
Michal Simek90663172014-02-12 14:41:19 +0100214 rc = xwdt_selftest(xdev);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100215 if (rc == XWT_TIMER_FAILED) {
Michal Simek4c7fbbc2014-02-12 14:41:20 +0100216 dev_err(&pdev->dev, "SelfTest routine error\n");
Shubhrajyoti Datta9d6b4ef2016-08-12 12:17:01 +0530217 goto err_clk_disable;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100218 }
219
Michal Simek90663172014-02-12 14:41:19 +0100220 rc = watchdog_register_device(xilinx_wdt_wdd);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100221 if (rc) {
Michal Simek4c7fbbc2014-02-12 14:41:20 +0100222 dev_err(&pdev->dev, "Cannot register watchdog (err=%d)\n", rc);
Shubhrajyoti Datta9d6b4ef2016-08-12 12:17:01 +0530223 goto err_clk_disable;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100224 }
225
Michal Simekd14fd962014-02-12 14:34:32 +0100226 dev_info(&pdev->dev, "Xilinx Watchdog Timer at %p with timeout %ds\n",
Michal Simek90663172014-02-12 14:41:19 +0100227 xdev->base, xilinx_wdt_wdd->timeout);
228
229 platform_set_drvdata(pdev, xdev);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100230
231 return 0;
Shubhrajyoti Datta9d6b4ef2016-08-12 12:17:01 +0530232err_clk_disable:
233 clk_disable_unprepare(xdev->clk);
234
235 return rc;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100236}
237
Michal Simek90663172014-02-12 14:41:19 +0100238static int xwdt_remove(struct platform_device *pdev)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100239{
Michal Simek90663172014-02-12 14:41:19 +0100240 struct xwdt_device *xdev = platform_get_drvdata(pdev);
241
242 watchdog_unregister_device(&xdev->xilinx_wdt_wdd);
Shubhrajyoti Datta9d6b4ef2016-08-12 12:17:01 +0530243 clk_disable_unprepare(xdev->clk);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100244
245 return 0;
246}
247
248/* Match table for of_platform binding */
Jingoo Han9ebf1852014-05-07 17:42:22 +0900249static const struct of_device_id xwdt_of_match[] = {
Michal Simek8fce9b32013-05-31 07:56:34 +0200250 { .compatible = "xlnx,xps-timebase-wdt-1.00.a", },
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100251 { .compatible = "xlnx,xps-timebase-wdt-1.01.a", },
252 {},
253};
254MODULE_DEVICE_TABLE(of, xwdt_of_match);
255
256static struct platform_driver xwdt_driver = {
257 .probe = xwdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500258 .remove = xwdt_remove,
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100259 .driver = {
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100260 .name = WATCHDOG_NAME,
261 .of_match_table = xwdt_of_match,
262 },
263};
264
Axel Linb8ec6112011-11-29 13:56:27 +0800265module_platform_driver(xwdt_driver);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100266
267MODULE_AUTHOR("Alejandro Cabrera <aldaya@gmail.com>");
268MODULE_DESCRIPTION("Xilinx Watchdog driver");
Michal Simek9419c072013-05-31 07:56:33 +0200269MODULE_LICENSE("GPL v2");