Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 1 | /* |
Michal Simek | 9419c07 | 2013-05-31 07:56:33 +0200 | [diff] [blame] | 2 | * Watchdog Device Driver for Xilinx axi/xps_timebase_wdt |
| 3 | * |
Michal Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame] | 4 | * (C) Copyright 2013 - 2014 Xilinx, Inc. |
Michal Simek | 9419c07 | 2013-05-31 07:56:33 +0200 | [diff] [blame] | 5 | * (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 Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 12 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 14 | |
Michal Simek | f06cdfd | 2014-02-12 14:34:34 +0100 | [diff] [blame^] | 15 | #include <linux/err.h> |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/types.h> |
| 18 | #include <linux/kernel.h> |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 19 | #include <linux/ioport.h> |
| 20 | #include <linux/watchdog.h> |
| 21 | #include <linux/io.h> |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 22 | #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 | |
| 46 | struct xwdt_device { |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 47 | void __iomem *base; |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 48 | u32 wdt_interval; |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | static struct xwdt_device xdev; |
| 52 | |
| 53 | static u32 timeout; |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 54 | static u8 no_timeout; |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 55 | |
| 56 | static DEFINE_SPINLOCK(spinlock); |
| 57 | |
Michal Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame] | 58 | static int xilinx_wdt_start(struct watchdog_device *wdd) |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 59 | { |
Michal Simek | 5cf4e69 | 2014-02-12 14:34:33 +0100 | [diff] [blame] | 60 | u32 control_status_reg; |
| 61 | |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 62 | 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 Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame] | 74 | |
| 75 | return 0; |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Michal Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame] | 78 | static int xilinx_wdt_stop(struct watchdog_device *wdd) |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 79 | { |
Michal Simek | 5cf4e69 | 2014-02-12 14:34:33 +0100 | [diff] [blame] | 80 | u32 control_status_reg; |
| 81 | |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 82 | 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 Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 92 | pr_info("Stopped!\n"); |
Michal Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame] | 93 | |
| 94 | return 0; |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Michal Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame] | 97 | static int xilinx_wdt_keepalive(struct watchdog_device *wdd) |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 98 | { |
Michal Simek | 5cf4e69 | 2014-02-12 14:34:33 +0100 | [diff] [blame] | 99 | u32 control_status_reg; |
| 100 | |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 101 | 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 Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame] | 108 | |
| 109 | return 0; |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Michal Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame] | 112 | static const struct watchdog_info xilinx_wdt_ident = { |
| 113 | .options = WDIOF_MAGICCLOSE | |
| 114 | WDIOF_KEEPALIVEPING, |
| 115 | .firmware_version = 1, |
| 116 | .identity = WATCHDOG_NAME, |
| 117 | }; |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 118 | |
Michal Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame] | 119 | static 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 Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 125 | |
Michal Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame] | 126 | static struct watchdog_device xilinx_wdt_wdd = { |
| 127 | .info = &xilinx_wdt_ident, |
| 128 | .ops = &xilinx_wdt_ops, |
| 129 | }; |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 130 | |
| 131 | static 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 Pemberton | 2d991a1 | 2012-11-19 13:21:41 -0500 | [diff] [blame] | 156 | static int xwdt_probe(struct platform_device *pdev) |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 157 | { |
| 158 | int rc; |
| 159 | u32 *tmptr; |
| 160 | u32 *pfreq; |
Michal Simek | f06cdfd | 2014-02-12 14:34:34 +0100 | [diff] [blame^] | 161 | struct resource *res; |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 162 | |
| 163 | no_timeout = 0; |
| 164 | |
Michal Simek | f06cdfd | 2014-02-12 14:34:34 +0100 | [diff] [blame^] | 165 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 166 | xdev.base = devm_ioremap_resource(&pdev->dev, res); |
| 167 | if (IS_ERR(xdev.base)) |
| 168 | return PTR_ERR(xdev.base); |
| 169 | |
Michal Simek | 90fe6c6 | 2012-06-21 08:45:40 +0200 | [diff] [blame] | 170 | pfreq = (u32 *)of_get_property(pdev->dev.of_node, |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 171 | "clock-frequency", NULL); |
| 172 | |
| 173 | if (pfreq == NULL) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 174 | pr_warn("The watchdog clock frequency cannot be obtained!\n"); |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 175 | no_timeout = 1; |
| 176 | } |
| 177 | |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 178 | tmptr = (u32 *)of_get_property(pdev->dev.of_node, |
| 179 | "xlnx,wdt-interval", NULL); |
| 180 | if (tmptr == NULL) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 181 | pr_warn("Parameter \"xlnx,wdt-interval\" not found in device tree!\n"); |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 182 | 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 Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 190 | pr_warn("Parameter \"xlnx,wdt-enable-once\" not found in device tree!\n"); |
Michal Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame] | 191 | watchdog_set_nowayout(&xilinx_wdt_wdd, true); |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 192 | } |
| 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 | |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 201 | rc = xwdt_selftest(); |
| 202 | if (rc == XWT_TIMER_FAILED) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 203 | pr_err("SelfTest routine error!\n"); |
Michal Simek | f06cdfd | 2014-02-12 14:34:34 +0100 | [diff] [blame^] | 204 | return rc; |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 205 | } |
| 206 | |
Michal Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame] | 207 | rc = watchdog_register_device(&xilinx_wdt_wdd); |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 208 | if (rc) { |
Michal Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame] | 209 | pr_err("cannot register watchdog (err=%d)\n", rc); |
Michal Simek | f06cdfd | 2014-02-12 14:34:34 +0100 | [diff] [blame^] | 210 | return rc; |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Michal Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame] | 213 | dev_info(&pdev->dev, "Xilinx Watchdog Timer at %p with timeout %ds\n", |
| 214 | xdev.base, timeout); |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 215 | |
| 216 | return 0; |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 217 | } |
| 218 | |
Bill Pemberton | 4b12b89 | 2012-11-19 13:26:24 -0500 | [diff] [blame] | 219 | static int xwdt_remove(struct platform_device *dev) |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 220 | { |
Michal Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame] | 221 | watchdog_unregister_device(&xilinx_wdt_wdd); |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | /* Match table for of_platform binding */ |
Bill Pemberton | 1d13136 | 2012-11-19 13:24:05 -0500 | [diff] [blame] | 227 | static struct of_device_id xwdt_of_match[] = { |
Michal Simek | 8fce9b3 | 2013-05-31 07:56:34 +0200 | [diff] [blame] | 228 | { .compatible = "xlnx,xps-timebase-wdt-1.00.a", }, |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 229 | { .compatible = "xlnx,xps-timebase-wdt-1.01.a", }, |
| 230 | {}, |
| 231 | }; |
| 232 | MODULE_DEVICE_TABLE(of, xwdt_of_match); |
| 233 | |
| 234 | static struct platform_driver xwdt_driver = { |
| 235 | .probe = xwdt_probe, |
Bill Pemberton | 8226871 | 2012-11-19 13:21:12 -0500 | [diff] [blame] | 236 | .remove = xwdt_remove, |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 237 | .driver = { |
| 238 | .owner = THIS_MODULE, |
| 239 | .name = WATCHDOG_NAME, |
| 240 | .of_match_table = xwdt_of_match, |
| 241 | }, |
| 242 | }; |
| 243 | |
Axel Lin | b8ec611 | 2011-11-29 13:56:27 +0800 | [diff] [blame] | 244 | module_platform_driver(xwdt_driver); |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 245 | |
| 246 | MODULE_AUTHOR("Alejandro Cabrera <aldaya@gmail.com>"); |
| 247 | MODULE_DESCRIPTION("Xilinx Watchdog driver"); |
Michal Simek | 9419c07 | 2013-05-31 07:56:33 +0200 | [diff] [blame] | 248 | MODULE_LICENSE("GPL v2"); |