Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Watchdog driver for Atmel AT91SAM9x processors. |
| 3 | * |
| 4 | * Copyright (C) 2008 Renaud CERRATO r.cerrato@til-technologies.fr |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | /* |
| 13 | * The Watchdog Timer Mode Register can be only written to once. If the |
| 14 | * timeout need to be set from Linux, be sure that the bootstrap or the |
| 15 | * bootloader doesn't write to this register. |
| 16 | */ |
| 17 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 18 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 19 | |
Alexandre Belloni | a97a09b | 2015-08-16 11:23:43 +0200 | [diff] [blame] | 20 | #include <linux/clk.h> |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 21 | #include <linux/errno.h> |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 22 | #include <linux/init.h> |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 23 | #include <linux/interrupt.h> |
Andrew Victor | 2af29b7 | 2009-02-11 21:23:10 +0100 | [diff] [blame] | 24 | #include <linux/io.h> |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 25 | #include <linux/kernel.h> |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 26 | #include <linux/module.h> |
| 27 | #include <linux/moduleparam.h> |
| 28 | #include <linux/platform_device.h> |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 29 | #include <linux/reboot.h> |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 30 | #include <linux/types.h> |
| 31 | #include <linux/watchdog.h> |
| 32 | #include <linux/jiffies.h> |
| 33 | #include <linux/timer.h> |
| 34 | #include <linux/bitops.h> |
| 35 | #include <linux/uaccess.h> |
Fabio Porcedda | be49bba | 2012-11-12 09:37:25 +0100 | [diff] [blame] | 36 | #include <linux/of.h> |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 37 | #include <linux/of_irq.h> |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 38 | |
Jean-Christophe Plagniol-Villard | e7b3914 | 2011-07-15 01:52:05 +0200 | [diff] [blame] | 39 | #include "at91sam9_wdt.h" |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 40 | |
| 41 | #define DRV_NAME "AT91SAM9 Watchdog" |
| 42 | |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 43 | #define wdt_read(wdt, field) \ |
Ben Dooks | feccebe | 2015-03-26 14:34:14 +0000 | [diff] [blame] | 44 | readl_relaxed((wdt)->base + (field)) |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 45 | #define wdt_write(wtd, field, val) \ |
Ben Dooks | feccebe | 2015-03-26 14:34:14 +0000 | [diff] [blame] | 46 | writel_relaxed((val), (wdt)->base + (field)) |
Jean-Christophe PLAGNIOL-VILLARD | c1c30a2 | 2011-11-02 01:43:31 +0800 | [diff] [blame] | 47 | |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 48 | /* AT91SAM9 watchdog runs a 12bit counter @ 256Hz, |
| 49 | * use this to convert a watchdog |
| 50 | * value from/to milliseconds. |
| 51 | */ |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 52 | #define ticks_to_hz_rounddown(t) ((((t) + 1) * HZ) >> 8) |
| 53 | #define ticks_to_hz_roundup(t) (((((t) + 1) * HZ) + 255) >> 8) |
| 54 | #define ticks_to_secs(t) (((t) + 1) >> 8) |
Boris BREZILLON | 1444797 | 2013-11-03 18:52:42 +0100 | [diff] [blame] | 55 | #define secs_to_ticks(s) ((s) ? (((s) << 8) - 1) : 0) |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 56 | |
| 57 | #define WDT_MR_RESET 0x3FFF2FFF |
| 58 | |
| 59 | /* Watchdog max counter value in ticks */ |
| 60 | #define WDT_COUNTER_MAX_TICKS 0xFFF |
| 61 | |
| 62 | /* Watchdog max delta/value in secs */ |
| 63 | #define WDT_COUNTER_MAX_SECS ticks_to_secs(WDT_COUNTER_MAX_TICKS) |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 64 | |
| 65 | /* Hardware timeout in seconds */ |
| 66 | #define WDT_HW_TIMEOUT 2 |
| 67 | |
| 68 | /* Timer heartbeat (500ms) */ |
| 69 | #define WDT_TIMEOUT (HZ/2) |
| 70 | |
| 71 | /* User land timeout */ |
| 72 | #define WDT_HEARTBEAT 15 |
Fabio Porcedda | c1fd5f6 | 2013-02-14 09:14:25 +0100 | [diff] [blame] | 73 | static int heartbeat; |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 74 | module_param(heartbeat, int, 0); |
| 75 | MODULE_PARM_DESC(heartbeat, "Watchdog heartbeats in seconds. " |
| 76 | "(default = " __MODULE_STRING(WDT_HEARTBEAT) ")"); |
| 77 | |
Wim Van Sebroeck | 86a1e18 | 2012-03-05 16:51:11 +0100 | [diff] [blame] | 78 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 79 | module_param(nowayout, bool, 0); |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 80 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started " |
| 81 | "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 82 | |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 83 | #define to_wdt(wdd) container_of(wdd, struct at91wdt, wdd) |
| 84 | struct at91wdt { |
| 85 | struct watchdog_device wdd; |
Jean-Christophe PLAGNIOL-VILLARD | c1c30a2 | 2011-11-02 01:43:31 +0800 | [diff] [blame] | 86 | void __iomem *base; |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 87 | unsigned long next_heartbeat; /* the next_heartbeat for the timer */ |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 88 | struct timer_list timer; /* The timer that pings the watchdog */ |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 89 | u32 mr; |
| 90 | u32 mr_mask; |
| 91 | unsigned long heartbeat; /* WDT heartbeat in jiffies */ |
| 92 | bool nowayout; |
| 93 | unsigned int irq; |
Alexandre Belloni | a97a09b | 2015-08-16 11:23:43 +0200 | [diff] [blame] | 94 | struct clk *sclk; |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 95 | }; |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 96 | |
| 97 | /* ......................................................................... */ |
| 98 | |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 99 | static irqreturn_t wdt_interrupt(int irq, void *dev_id) |
| 100 | { |
| 101 | struct at91wdt *wdt = (struct at91wdt *)dev_id; |
| 102 | |
| 103 | if (wdt_read(wdt, AT91_WDT_SR)) { |
| 104 | pr_crit("at91sam9 WDT software reset\n"); |
| 105 | emergency_restart(); |
| 106 | pr_crit("Reboot didn't ?????\n"); |
| 107 | } |
| 108 | |
| 109 | return IRQ_HANDLED; |
| 110 | } |
| 111 | |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 112 | /* |
| 113 | * Reload the watchdog timer. (ie, pat the watchdog) |
| 114 | */ |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 115 | static inline void at91_wdt_reset(struct at91wdt *wdt) |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 116 | { |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 117 | wdt_write(wdt, AT91_WDT_CR, AT91_WDT_KEY | AT91_WDT_WDRSTT); |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* |
| 121 | * Timer tick |
| 122 | */ |
| 123 | static void at91_ping(unsigned long data) |
| 124 | { |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 125 | struct at91wdt *wdt = (struct at91wdt *)data; |
| 126 | if (time_before(jiffies, wdt->next_heartbeat) || |
| 127 | !watchdog_active(&wdt->wdd)) { |
| 128 | at91_wdt_reset(wdt); |
| 129 | mod_timer(&wdt->timer, jiffies + wdt->heartbeat); |
| 130 | } else { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 131 | pr_crit("I will reset your machine !\n"); |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 132 | } |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Wenyou Yang | 490ac7af | 2013-02-01 15:06:21 +0800 | [diff] [blame] | 135 | static int at91_wdt_start(struct watchdog_device *wdd) |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 136 | { |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 137 | struct at91wdt *wdt = to_wdt(wdd); |
| 138 | /* calculate when the next userspace timeout will be */ |
| 139 | wdt->next_heartbeat = jiffies + wdd->timeout * HZ; |
Wenyou Yang | 490ac7af | 2013-02-01 15:06:21 +0800 | [diff] [blame] | 140 | return 0; |
| 141 | } |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 142 | |
Wenyou Yang | 490ac7af | 2013-02-01 15:06:21 +0800 | [diff] [blame] | 143 | static int at91_wdt_stop(struct watchdog_device *wdd) |
| 144 | { |
| 145 | /* The watchdog timer hardware can not be stopped... */ |
| 146 | return 0; |
| 147 | } |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 148 | |
Wenyou Yang | 490ac7af | 2013-02-01 15:06:21 +0800 | [diff] [blame] | 149 | static int at91_wdt_set_timeout(struct watchdog_device *wdd, unsigned int new_timeout) |
| 150 | { |
| 151 | wdd->timeout = new_timeout; |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 152 | return at91_wdt_start(wdd); |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 155 | static int at91_wdt_init(struct platform_device *pdev, struct at91wdt *wdt) |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 156 | { |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 157 | u32 tmp; |
| 158 | u32 delta; |
| 159 | u32 value; |
| 160 | int err; |
| 161 | u32 mask = wdt->mr_mask; |
| 162 | unsigned long min_heartbeat = 1; |
Boris BREZILLON | f72fa00 | 2013-11-03 18:52:44 +0100 | [diff] [blame] | 163 | unsigned long max_heartbeat; |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 164 | struct device *dev = &pdev->dev; |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 165 | |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 166 | tmp = wdt_read(wdt, AT91_WDT_MR); |
| 167 | if ((tmp & mask) != (wdt->mr & mask)) { |
| 168 | if (tmp == WDT_MR_RESET) { |
| 169 | wdt_write(wdt, AT91_WDT_MR, wdt->mr); |
| 170 | tmp = wdt_read(wdt, AT91_WDT_MR); |
| 171 | } |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 172 | } |
| 173 | |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 174 | if (tmp & AT91_WDT_WDDIS) { |
| 175 | if (wdt->mr & AT91_WDT_WDDIS) |
| 176 | return 0; |
| 177 | dev_err(dev, "watchdog is disabled\n"); |
| 178 | return -EINVAL; |
| 179 | } |
| 180 | |
| 181 | value = tmp & AT91_WDT_WDV; |
| 182 | delta = (tmp & AT91_WDT_WDD) >> 16; |
| 183 | |
| 184 | if (delta < value) |
| 185 | min_heartbeat = ticks_to_hz_roundup(value - delta); |
| 186 | |
Boris BREZILLON | f72fa00 | 2013-11-03 18:52:44 +0100 | [diff] [blame] | 187 | max_heartbeat = ticks_to_hz_rounddown(value); |
| 188 | if (!max_heartbeat) { |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 189 | dev_err(dev, |
| 190 | "heartbeat is too small for the system to handle it correctly\n"); |
| 191 | return -EINVAL; |
| 192 | } |
| 193 | |
Boris BREZILLON | f72fa00 | 2013-11-03 18:52:44 +0100 | [diff] [blame] | 194 | /* |
| 195 | * Try to reset the watchdog counter 4 or 2 times more often than |
| 196 | * actually requested, to avoid spurious watchdog reset. |
| 197 | * If this is not possible because of the min_heartbeat value, reset |
| 198 | * it at the min_heartbeat period. |
| 199 | */ |
| 200 | if ((max_heartbeat / 4) >= min_heartbeat) |
| 201 | wdt->heartbeat = max_heartbeat / 4; |
| 202 | else if ((max_heartbeat / 2) >= min_heartbeat) |
| 203 | wdt->heartbeat = max_heartbeat / 2; |
| 204 | else |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 205 | wdt->heartbeat = min_heartbeat; |
Boris BREZILLON | f72fa00 | 2013-11-03 18:52:44 +0100 | [diff] [blame] | 206 | |
| 207 | if (max_heartbeat < min_heartbeat + 4) |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 208 | dev_warn(dev, |
| 209 | "min heartbeat and max heartbeat might be too close for the system to handle it correctly\n"); |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 210 | |
| 211 | if ((tmp & AT91_WDT_WDFIEN) && wdt->irq) { |
| 212 | err = request_irq(wdt->irq, wdt_interrupt, |
Boris BREZILLON | d677772 | 2015-03-02 10:18:17 +0100 | [diff] [blame] | 213 | IRQF_SHARED | IRQF_IRQPOLL | |
| 214 | IRQF_NO_SUSPEND, |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 215 | pdev->name, wdt); |
| 216 | if (err) |
| 217 | return err; |
| 218 | } |
| 219 | |
| 220 | if ((tmp & wdt->mr_mask) != (wdt->mr & wdt->mr_mask)) |
| 221 | dev_warn(dev, |
| 222 | "watchdog already configured differently (mr = %x expecting %x)\n", |
| 223 | tmp & wdt->mr_mask, wdt->mr & wdt->mr_mask); |
| 224 | |
| 225 | setup_timer(&wdt->timer, at91_ping, (unsigned long)wdt); |
Boris BREZILLON | a04c3f0 | 2013-11-03 18:52:43 +0100 | [diff] [blame] | 226 | |
| 227 | /* |
| 228 | * Use min_heartbeat the first time to avoid spurious watchdog reset: |
| 229 | * we don't know for how long the watchdog counter is running, and |
| 230 | * - resetting it right now might trigger a watchdog fault reset |
| 231 | * - waiting for heartbeat time might lead to a watchdog timeout |
| 232 | * reset |
| 233 | */ |
| 234 | mod_timer(&wdt->timer, jiffies + min_heartbeat); |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 235 | |
| 236 | /* Try to set timeout from device tree first */ |
| 237 | if (watchdog_init_timeout(&wdt->wdd, 0, dev)) |
| 238 | watchdog_init_timeout(&wdt->wdd, heartbeat, dev); |
| 239 | watchdog_set_nowayout(&wdt->wdd, wdt->nowayout); |
| 240 | err = watchdog_register_device(&wdt->wdd); |
| 241 | if (err) |
| 242 | goto out_stop_timer; |
| 243 | |
| 244 | wdt->next_heartbeat = jiffies + wdt->wdd.timeout * HZ; |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 245 | |
| 246 | return 0; |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 247 | |
| 248 | out_stop_timer: |
| 249 | del_timer(&wdt->timer); |
| 250 | return err; |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 251 | } |
| 252 | |
Wenyou Yang | 490ac7af | 2013-02-01 15:06:21 +0800 | [diff] [blame] | 253 | /* ......................................................................... */ |
| 254 | |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 255 | static const struct watchdog_info at91_wdt_info = { |
| 256 | .identity = DRV_NAME, |
Wim Van Sebroeck | e73a780 | 2009-05-11 18:33:00 +0000 | [diff] [blame] | 257 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | |
| 258 | WDIOF_MAGICCLOSE, |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 259 | }; |
| 260 | |
Wenyou Yang | 490ac7af | 2013-02-01 15:06:21 +0800 | [diff] [blame] | 261 | static const struct watchdog_ops at91_wdt_ops = { |
| 262 | .owner = THIS_MODULE, |
| 263 | .start = at91_wdt_start, |
| 264 | .stop = at91_wdt_stop, |
Wenyou Yang | 490ac7af | 2013-02-01 15:06:21 +0800 | [diff] [blame] | 265 | .set_timeout = at91_wdt_set_timeout, |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 266 | }; |
| 267 | |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 268 | #if defined(CONFIG_OF) |
| 269 | static int of_at91wdt_init(struct device_node *np, struct at91wdt *wdt) |
| 270 | { |
| 271 | u32 min = 0; |
| 272 | u32 max = WDT_COUNTER_MAX_SECS; |
| 273 | const char *tmp; |
| 274 | |
| 275 | /* Get the interrupts property */ |
| 276 | wdt->irq = irq_of_parse_and_map(np, 0); |
| 277 | if (!wdt->irq) |
| 278 | dev_warn(wdt->wdd.parent, "failed to get IRQ from DT\n"); |
| 279 | |
| 280 | if (!of_property_read_u32_index(np, "atmel,max-heartbeat-sec", 0, |
| 281 | &max)) { |
| 282 | if (!max || max > WDT_COUNTER_MAX_SECS) |
| 283 | max = WDT_COUNTER_MAX_SECS; |
| 284 | |
| 285 | if (!of_property_read_u32_index(np, "atmel,min-heartbeat-sec", |
| 286 | 0, &min)) { |
| 287 | if (min >= max) |
| 288 | min = max - 1; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | min = secs_to_ticks(min); |
| 293 | max = secs_to_ticks(max); |
| 294 | |
| 295 | wdt->mr_mask = 0x3FFFFFFF; |
| 296 | wdt->mr = 0; |
| 297 | if (!of_property_read_string(np, "atmel,watchdog-type", &tmp) && |
| 298 | !strcmp(tmp, "software")) { |
| 299 | wdt->mr |= AT91_WDT_WDFIEN; |
| 300 | wdt->mr_mask &= ~AT91_WDT_WDRPROC; |
| 301 | } else { |
| 302 | wdt->mr |= AT91_WDT_WDRSTEN; |
| 303 | } |
| 304 | |
| 305 | if (!of_property_read_string(np, "atmel,reset-type", &tmp) && |
| 306 | !strcmp(tmp, "proc")) |
| 307 | wdt->mr |= AT91_WDT_WDRPROC; |
| 308 | |
| 309 | if (of_property_read_bool(np, "atmel,disable")) { |
| 310 | wdt->mr |= AT91_WDT_WDDIS; |
| 311 | wdt->mr_mask &= AT91_WDT_WDDIS; |
| 312 | } |
| 313 | |
| 314 | if (of_property_read_bool(np, "atmel,idle-halt")) |
| 315 | wdt->mr |= AT91_WDT_WDIDLEHLT; |
| 316 | |
| 317 | if (of_property_read_bool(np, "atmel,dbg-halt")) |
| 318 | wdt->mr |= AT91_WDT_WDDBGHLT; |
| 319 | |
| 320 | wdt->mr |= max | ((max - min) << 16); |
| 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | #else |
| 325 | static inline int of_at91wdt_init(struct device_node *np, struct at91wdt *wdt) |
| 326 | { |
| 327 | return 0; |
| 328 | } |
| 329 | #endif |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 330 | |
| 331 | static int __init at91wdt_probe(struct platform_device *pdev) |
| 332 | { |
Jean-Christophe PLAGNIOL-VILLARD | c1c30a2 | 2011-11-02 01:43:31 +0800 | [diff] [blame] | 333 | struct resource *r; |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 334 | int err; |
| 335 | struct at91wdt *wdt; |
| 336 | |
| 337 | wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL); |
| 338 | if (!wdt) |
| 339 | return -ENOMEM; |
| 340 | |
| 341 | wdt->mr = (WDT_HW_TIMEOUT * 256) | AT91_WDT_WDRSTEN | AT91_WDT_WDD | |
| 342 | AT91_WDT_WDDBGHLT | AT91_WDT_WDIDLEHLT; |
| 343 | wdt->mr_mask = 0x3FFFFFFF; |
| 344 | wdt->nowayout = nowayout; |
| 345 | wdt->wdd.parent = &pdev->dev; |
| 346 | wdt->wdd.info = &at91_wdt_info; |
| 347 | wdt->wdd.ops = &at91_wdt_ops; |
| 348 | wdt->wdd.timeout = WDT_HEARTBEAT; |
| 349 | wdt->wdd.min_timeout = 1; |
| 350 | wdt->wdd.max_timeout = 0xFFFF; |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 351 | |
Jean-Christophe PLAGNIOL-VILLARD | c1c30a2 | 2011-11-02 01:43:31 +0800 | [diff] [blame] | 352 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 353 | wdt->base = devm_ioremap_resource(&pdev->dev, r); |
| 354 | if (IS_ERR(wdt->base)) |
| 355 | return PTR_ERR(wdt->base); |
| 356 | |
Alexandre Belloni | a97a09b | 2015-08-16 11:23:43 +0200 | [diff] [blame] | 357 | wdt->sclk = devm_clk_get(&pdev->dev, NULL); |
| 358 | if (IS_ERR(wdt->sclk)) |
| 359 | return PTR_ERR(wdt->sclk); |
| 360 | |
| 361 | err = clk_prepare_enable(wdt->sclk); |
| 362 | if (err) { |
| 363 | dev_err(&pdev->dev, "Could not enable slow clock\n"); |
| 364 | return err; |
| 365 | } |
| 366 | |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 367 | if (pdev->dev.of_node) { |
| 368 | err = of_at91wdt_init(pdev->dev.of_node, wdt); |
| 369 | if (err) |
Alexandre Belloni | a97a09b | 2015-08-16 11:23:43 +0200 | [diff] [blame] | 370 | goto err_clk; |
Jean-Christophe PLAGNIOL-VILLARD | c1c30a2 | 2011-11-02 01:43:31 +0800 | [diff] [blame] | 371 | } |
| 372 | |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 373 | err = at91_wdt_init(pdev, wdt); |
| 374 | if (err) |
Alexandre Belloni | a97a09b | 2015-08-16 11:23:43 +0200 | [diff] [blame] | 375 | goto err_clk; |
Wenyou Yang | 490ac7af | 2013-02-01 15:06:21 +0800 | [diff] [blame] | 376 | |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 377 | platform_set_drvdata(pdev, wdt); |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 378 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 379 | pr_info("enabled (heartbeat=%d sec, nowayout=%d)\n", |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 380 | wdt->wdd.timeout, wdt->nowayout); |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 381 | |
| 382 | return 0; |
Alexandre Belloni | a97a09b | 2015-08-16 11:23:43 +0200 | [diff] [blame] | 383 | |
| 384 | err_clk: |
| 385 | clk_disable_unprepare(wdt->sclk); |
| 386 | |
| 387 | return err; |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | static int __exit at91wdt_remove(struct platform_device *pdev) |
| 391 | { |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 392 | struct at91wdt *wdt = platform_get_drvdata(pdev); |
| 393 | watchdog_unregister_device(&wdt->wdd); |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 394 | |
Wenyou Yang | 490ac7af | 2013-02-01 15:06:21 +0800 | [diff] [blame] | 395 | pr_warn("I quit now, hardware will probably reboot!\n"); |
Boris BREZILLON | 5161b31 | 2013-10-04 09:24:12 +0200 | [diff] [blame] | 396 | del_timer(&wdt->timer); |
Alexandre Belloni | a97a09b | 2015-08-16 11:23:43 +0200 | [diff] [blame] | 397 | clk_disable_unprepare(wdt->sclk); |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 398 | |
Wenyou Yang | 490ac7af | 2013-02-01 15:06:21 +0800 | [diff] [blame] | 399 | return 0; |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 400 | } |
| 401 | |
Fabio Porcedda | be49bba | 2012-11-12 09:37:25 +0100 | [diff] [blame] | 402 | #if defined(CONFIG_OF) |
Arnd Bergmann | 6c41e47 | 2013-01-25 14:14:27 +0000 | [diff] [blame] | 403 | static const struct of_device_id at91_wdt_dt_ids[] = { |
Fabio Porcedda | be49bba | 2012-11-12 09:37:25 +0100 | [diff] [blame] | 404 | { .compatible = "atmel,at91sam9260-wdt" }, |
| 405 | { /* sentinel */ } |
| 406 | }; |
| 407 | |
| 408 | MODULE_DEVICE_TABLE(of, at91_wdt_dt_ids); |
| 409 | #endif |
| 410 | |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 411 | static struct platform_driver at91wdt_driver = { |
| 412 | .remove = __exit_p(at91wdt_remove), |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 413 | .driver = { |
| 414 | .name = "at91_wdt", |
Fabio Porcedda | be49bba | 2012-11-12 09:37:25 +0100 | [diff] [blame] | 415 | .of_match_table = of_match_ptr(at91_wdt_dt_ids), |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 416 | }, |
| 417 | }; |
| 418 | |
Fabio Porcedda | 1cb9204 | 2013-01-09 12:15:27 +0100 | [diff] [blame] | 419 | module_platform_driver_probe(at91wdt_driver, at91wdt_probe); |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 420 | |
| 421 | MODULE_AUTHOR("Renaud CERRATO <r.cerrato@til-technologies.fr>"); |
| 422 | MODULE_DESCRIPTION("Watchdog driver for Atmel AT91SAM9x processors"); |
| 423 | MODULE_LICENSE("GPL"); |