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 | |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/kernel.h> |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 18 | #include <linux/ioport.h> |
| 19 | #include <linux/watchdog.h> |
| 20 | #include <linux/io.h> |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 21 | #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 | |
| 45 | struct xwdt_device { |
| 46 | struct resource res; |
| 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; |
| 54 | static u32 control_status_reg; |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 55 | static u8 no_timeout; |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 56 | |
| 57 | static DEFINE_SPINLOCK(spinlock); |
| 58 | |
Michal Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame^] | 59 | static int xilinx_wdt_start(struct watchdog_device *wdd) |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 60 | { |
| 61 | 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 Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame^] | 73 | |
| 74 | return 0; |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Michal Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame^] | 77 | static int xilinx_wdt_stop(struct watchdog_device *wdd) |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 78 | { |
| 79 | spin_lock(&spinlock); |
| 80 | |
| 81 | control_status_reg = ioread32(xdev.base + XWT_TWCSR0_OFFSET); |
| 82 | |
| 83 | iowrite32((control_status_reg & ~XWT_CSR0_EWDT1_MASK), |
| 84 | xdev.base + XWT_TWCSR0_OFFSET); |
| 85 | |
| 86 | iowrite32(0, xdev.base + XWT_TWCSR1_OFFSET); |
| 87 | |
| 88 | spin_unlock(&spinlock); |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 89 | pr_info("Stopped!\n"); |
Michal Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame^] | 90 | |
| 91 | return 0; |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Michal Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame^] | 94 | static int xilinx_wdt_keepalive(struct watchdog_device *wdd) |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 95 | { |
| 96 | spin_lock(&spinlock); |
| 97 | |
| 98 | control_status_reg = ioread32(xdev.base + XWT_TWCSR0_OFFSET); |
| 99 | control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK); |
| 100 | iowrite32(control_status_reg, xdev.base + XWT_TWCSR0_OFFSET); |
| 101 | |
| 102 | spin_unlock(&spinlock); |
Michal Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame^] | 103 | |
| 104 | return 0; |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Michal Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame^] | 107 | static const struct watchdog_info xilinx_wdt_ident = { |
| 108 | .options = WDIOF_MAGICCLOSE | |
| 109 | WDIOF_KEEPALIVEPING, |
| 110 | .firmware_version = 1, |
| 111 | .identity = WATCHDOG_NAME, |
| 112 | }; |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 113 | |
Michal Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame^] | 114 | static const struct watchdog_ops xilinx_wdt_ops = { |
| 115 | .owner = THIS_MODULE, |
| 116 | .start = xilinx_wdt_start, |
| 117 | .stop = xilinx_wdt_stop, |
| 118 | .ping = xilinx_wdt_keepalive, |
| 119 | }; |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 120 | |
Michal Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame^] | 121 | static struct watchdog_device xilinx_wdt_wdd = { |
| 122 | .info = &xilinx_wdt_ident, |
| 123 | .ops = &xilinx_wdt_ops, |
| 124 | }; |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 125 | |
| 126 | static u32 xwdt_selftest(void) |
| 127 | { |
| 128 | int i; |
| 129 | u32 timer_value1; |
| 130 | u32 timer_value2; |
| 131 | |
| 132 | spin_lock(&spinlock); |
| 133 | |
| 134 | timer_value1 = ioread32(xdev.base + XWT_TBR_OFFSET); |
| 135 | timer_value2 = ioread32(xdev.base + XWT_TBR_OFFSET); |
| 136 | |
| 137 | for (i = 0; |
| 138 | ((i <= XWT_MAX_SELFTEST_LOOP_COUNT) && |
| 139 | (timer_value2 == timer_value1)); i++) { |
| 140 | timer_value2 = ioread32(xdev.base + XWT_TBR_OFFSET); |
| 141 | } |
| 142 | |
| 143 | spin_unlock(&spinlock); |
| 144 | |
| 145 | if (timer_value2 != timer_value1) |
| 146 | return ~XWT_TIMER_FAILED; |
| 147 | else |
| 148 | return XWT_TIMER_FAILED; |
| 149 | } |
| 150 | |
Bill Pemberton | 2d991a1 | 2012-11-19 13:21:41 -0500 | [diff] [blame] | 151 | static int xwdt_probe(struct platform_device *pdev) |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 152 | { |
| 153 | int rc; |
| 154 | u32 *tmptr; |
| 155 | u32 *pfreq; |
| 156 | |
| 157 | no_timeout = 0; |
| 158 | |
Michal Simek | 90fe6c6 | 2012-06-21 08:45:40 +0200 | [diff] [blame] | 159 | pfreq = (u32 *)of_get_property(pdev->dev.of_node, |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 160 | "clock-frequency", NULL); |
| 161 | |
| 162 | if (pfreq == NULL) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 163 | pr_warn("The watchdog clock frequency cannot be obtained!\n"); |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 164 | no_timeout = 1; |
| 165 | } |
| 166 | |
| 167 | rc = of_address_to_resource(pdev->dev.of_node, 0, &xdev.res); |
| 168 | if (rc) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 169 | pr_warn("invalid address!\n"); |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 170 | return rc; |
| 171 | } |
| 172 | |
| 173 | tmptr = (u32 *)of_get_property(pdev->dev.of_node, |
| 174 | "xlnx,wdt-interval", NULL); |
| 175 | if (tmptr == NULL) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 176 | pr_warn("Parameter \"xlnx,wdt-interval\" not found in device tree!\n"); |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 177 | no_timeout = 1; |
| 178 | } else { |
| 179 | xdev.wdt_interval = *tmptr; |
| 180 | } |
| 181 | |
| 182 | tmptr = (u32 *)of_get_property(pdev->dev.of_node, |
| 183 | "xlnx,wdt-enable-once", NULL); |
| 184 | if (tmptr == NULL) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 185 | 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^] | 186 | watchdog_set_nowayout(&xilinx_wdt_wdd, true); |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* |
| 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 | */ |
| 193 | if (!no_timeout) |
| 194 | timeout = 2 * ((1<<xdev.wdt_interval) / *pfreq); |
| 195 | |
| 196 | if (!request_mem_region(xdev.res.start, |
| 197 | xdev.res.end - xdev.res.start + 1, WATCHDOG_NAME)) { |
| 198 | rc = -ENXIO; |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 199 | pr_err("memory request failure!\n"); |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 200 | goto err_out; |
| 201 | } |
| 202 | |
| 203 | xdev.base = ioremap(xdev.res.start, xdev.res.end - xdev.res.start + 1); |
| 204 | if (xdev.base == NULL) { |
| 205 | rc = -ENOMEM; |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 206 | pr_err("ioremap failure!\n"); |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 207 | goto release_mem; |
| 208 | } |
| 209 | |
| 210 | rc = xwdt_selftest(); |
| 211 | if (rc == XWT_TIMER_FAILED) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 212 | pr_err("SelfTest routine error!\n"); |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 213 | goto unmap_io; |
| 214 | } |
| 215 | |
Michal Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame^] | 216 | rc = watchdog_register_device(&xilinx_wdt_wdd); |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 217 | if (rc) { |
Michal Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame^] | 218 | pr_err("cannot register watchdog (err=%d)\n", rc); |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 219 | goto unmap_io; |
| 220 | } |
| 221 | |
Michal Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame^] | 222 | dev_info(&pdev->dev, "Xilinx Watchdog Timer at %p with timeout %ds\n", |
| 223 | xdev.base, timeout); |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 224 | |
| 225 | return 0; |
| 226 | |
| 227 | unmap_io: |
| 228 | iounmap(xdev.base); |
| 229 | release_mem: |
| 230 | release_mem_region(xdev.res.start, resource_size(&xdev.res)); |
| 231 | err_out: |
| 232 | return rc; |
| 233 | } |
| 234 | |
Bill Pemberton | 4b12b89 | 2012-11-19 13:26:24 -0500 | [diff] [blame] | 235 | static int xwdt_remove(struct platform_device *dev) |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 236 | { |
Michal Simek | d14fd96 | 2014-02-12 14:34:32 +0100 | [diff] [blame^] | 237 | watchdog_unregister_device(&xilinx_wdt_wdd); |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 238 | iounmap(xdev.base); |
| 239 | release_mem_region(xdev.res.start, resource_size(&xdev.res)); |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | /* Match table for of_platform binding */ |
Bill Pemberton | 1d13136 | 2012-11-19 13:24:05 -0500 | [diff] [blame] | 245 | static struct of_device_id xwdt_of_match[] = { |
Michal Simek | 8fce9b3 | 2013-05-31 07:56:34 +0200 | [diff] [blame] | 246 | { .compatible = "xlnx,xps-timebase-wdt-1.00.a", }, |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 247 | { .compatible = "xlnx,xps-timebase-wdt-1.01.a", }, |
| 248 | {}, |
| 249 | }; |
| 250 | MODULE_DEVICE_TABLE(of, xwdt_of_match); |
| 251 | |
| 252 | static struct platform_driver xwdt_driver = { |
| 253 | .probe = xwdt_probe, |
Bill Pemberton | 8226871 | 2012-11-19 13:21:12 -0500 | [diff] [blame] | 254 | .remove = xwdt_remove, |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 255 | .driver = { |
| 256 | .owner = THIS_MODULE, |
| 257 | .name = WATCHDOG_NAME, |
| 258 | .of_match_table = xwdt_of_match, |
| 259 | }, |
| 260 | }; |
| 261 | |
Axel Lin | b8ec611 | 2011-11-29 13:56:27 +0800 | [diff] [blame] | 262 | module_platform_driver(xwdt_driver); |
Alejandro Cabrera | e9659e6 | 2011-06-02 22:13:11 +0100 | [diff] [blame] | 263 | |
| 264 | MODULE_AUTHOR("Alejandro Cabrera <aldaya@gmail.com>"); |
| 265 | MODULE_DESCRIPTION("Xilinx Watchdog driver"); |
Michal Simek | 9419c07 | 2013-05-31 07:56:33 +0200 | [diff] [blame] | 266 | MODULE_LICENSE("GPL v2"); |