Thomas Gleixner | f50a7f3 | 2019-05-28 09:57:18 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Driver for Atmel SAMA5D4 Watchdog Timer |
| 4 | * |
Eugen Hristev | bb44aa0 | 2019-11-18 08:50:36 +0000 | [diff] [blame] | 5 | * Copyright (C) 2015-2019 Microchip Technology Inc. and its subsidiaries |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
Alexandre Belloni | ddd6d24 | 2017-03-02 18:31:12 +0100 | [diff] [blame] | 8 | #include <linux/delay.h> |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 9 | #include <linux/interrupt.h> |
| 10 | #include <linux/io.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/of.h> |
Eugen Hristev | bb44aa0 | 2019-11-18 08:50:36 +0000 | [diff] [blame] | 14 | #include <linux/of_device.h> |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 15 | #include <linux/of_irq.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/reboot.h> |
| 18 | #include <linux/watchdog.h> |
| 19 | |
| 20 | #include "at91sam9_wdt.h" |
| 21 | |
| 22 | /* minimum and maximum watchdog timeout, in seconds */ |
| 23 | #define MIN_WDT_TIMEOUT 1 |
| 24 | #define MAX_WDT_TIMEOUT 16 |
| 25 | #define WDT_DEFAULT_TIMEOUT MAX_WDT_TIMEOUT |
| 26 | |
| 27 | #define WDT_SEC2TICKS(s) ((s) ? (((s) << 8) - 1) : 0) |
| 28 | |
| 29 | struct sama5d4_wdt { |
| 30 | struct watchdog_device wdd; |
| 31 | void __iomem *reg_base; |
Alexandre Belloni | 722ce63 | 2017-01-30 18:18:47 +0100 | [diff] [blame] | 32 | u32 mr; |
Eugen Hristev | bb44aa0 | 2019-11-18 08:50:36 +0000 | [diff] [blame] | 33 | u32 ir; |
Alexandre Belloni | ddd6d24 | 2017-03-02 18:31:12 +0100 | [diff] [blame] | 34 | unsigned long last_ping; |
Eugen Hristev | bb44aa0 | 2019-11-18 08:50:36 +0000 | [diff] [blame] | 35 | bool need_irq; |
| 36 | bool sam9x60_support; |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 37 | }; |
| 38 | |
Marcus Folkesson | 976932e | 2018-02-11 21:08:41 +0100 | [diff] [blame] | 39 | static int wdt_timeout; |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 40 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 41 | |
| 42 | module_param(wdt_timeout, int, 0); |
| 43 | MODULE_PARM_DESC(wdt_timeout, |
| 44 | "Watchdog timeout in seconds. (default = " |
| 45 | __MODULE_STRING(WDT_DEFAULT_TIMEOUT) ")"); |
| 46 | |
| 47 | module_param(nowayout, bool, 0); |
| 48 | MODULE_PARM_DESC(nowayout, |
| 49 | "Watchdog cannot be stopped once started (default=" |
| 50 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 51 | |
Alexandre Belloni | 015b528 | 2017-03-02 18:31:11 +0100 | [diff] [blame] | 52 | #define wdt_enabled (!(wdt->mr & AT91_WDT_WDDIS)) |
| 53 | |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 54 | #define wdt_read(wdt, field) \ |
| 55 | readl_relaxed((wdt)->reg_base + (field)) |
| 56 | |
Alexandre Belloni | ddd6d24 | 2017-03-02 18:31:12 +0100 | [diff] [blame] | 57 | /* 4 slow clock periods is 4/32768 = 122.07µs*/ |
| 58 | #define WDT_DELAY usecs_to_jiffies(123) |
| 59 | |
| 60 | static void wdt_write(struct sama5d4_wdt *wdt, u32 field, u32 val) |
| 61 | { |
| 62 | /* |
| 63 | * WDT_CR and WDT_MR must not be modified within three slow clock |
| 64 | * periods following a restart of the watchdog performed by a write |
| 65 | * access in WDT_CR. |
| 66 | */ |
| 67 | while (time_before(jiffies, wdt->last_ping + WDT_DELAY)) |
| 68 | usleep_range(30, 125); |
| 69 | writel_relaxed(val, wdt->reg_base + field); |
| 70 | wdt->last_ping = jiffies; |
| 71 | } |
| 72 | |
| 73 | static void wdt_write_nosleep(struct sama5d4_wdt *wdt, u32 field, u32 val) |
| 74 | { |
| 75 | if (time_before(jiffies, wdt->last_ping + WDT_DELAY)) |
| 76 | udelay(123); |
| 77 | writel_relaxed(val, wdt->reg_base + field); |
| 78 | wdt->last_ping = jiffies; |
| 79 | } |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 80 | |
| 81 | static int sama5d4_wdt_start(struct watchdog_device *wdd) |
| 82 | { |
| 83 | struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd); |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 84 | |
Eugen Hristev | bb44aa0 | 2019-11-18 08:50:36 +0000 | [diff] [blame] | 85 | if (wdt->sam9x60_support) { |
| 86 | writel_relaxed(wdt->ir, wdt->reg_base + AT91_SAM9X60_IER); |
| 87 | wdt->mr &= ~AT91_SAM9X60_WDDIS; |
| 88 | } else { |
| 89 | wdt->mr &= ~AT91_WDT_WDDIS; |
| 90 | } |
Alexandre Belloni | 722ce63 | 2017-01-30 18:18:47 +0100 | [diff] [blame] | 91 | wdt_write(wdt, AT91_WDT_MR, wdt->mr); |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | static int sama5d4_wdt_stop(struct watchdog_device *wdd) |
| 97 | { |
| 98 | struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd); |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 99 | |
Eugen Hristev | bb44aa0 | 2019-11-18 08:50:36 +0000 | [diff] [blame] | 100 | if (wdt->sam9x60_support) { |
| 101 | writel_relaxed(wdt->ir, wdt->reg_base + AT91_SAM9X60_IDR); |
| 102 | wdt->mr |= AT91_SAM9X60_WDDIS; |
| 103 | } else { |
| 104 | wdt->mr |= AT91_WDT_WDDIS; |
| 105 | } |
Alexandre Belloni | 722ce63 | 2017-01-30 18:18:47 +0100 | [diff] [blame] | 106 | wdt_write(wdt, AT91_WDT_MR, wdt->mr); |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static int sama5d4_wdt_ping(struct watchdog_device *wdd) |
| 112 | { |
| 113 | struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd); |
| 114 | |
| 115 | wdt_write(wdt, AT91_WDT_CR, AT91_WDT_KEY | AT91_WDT_WDRSTT); |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static int sama5d4_wdt_set_timeout(struct watchdog_device *wdd, |
| 121 | unsigned int timeout) |
| 122 | { |
| 123 | struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd); |
| 124 | u32 value = WDT_SEC2TICKS(timeout); |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 125 | |
Eugen Hristev | bb44aa0 | 2019-11-18 08:50:36 +0000 | [diff] [blame] | 126 | if (wdt->sam9x60_support) { |
| 127 | wdt_write(wdt, AT91_SAM9X60_WLR, |
| 128 | AT91_SAM9X60_SET_COUNTER(value)); |
| 129 | |
| 130 | wdd->timeout = timeout; |
| 131 | return 0; |
| 132 | } |
| 133 | |
Alexandre Belloni | 722ce63 | 2017-01-30 18:18:47 +0100 | [diff] [blame] | 134 | wdt->mr &= ~AT91_WDT_WDV; |
Alexandre Belloni | 722ce63 | 2017-01-30 18:18:47 +0100 | [diff] [blame] | 135 | wdt->mr |= AT91_WDT_SET_WDV(value); |
Alexandre Belloni | 015b528 | 2017-03-02 18:31:11 +0100 | [diff] [blame] | 136 | |
| 137 | /* |
| 138 | * WDDIS has to be 0 when updating WDD/WDV. The datasheet states: When |
| 139 | * setting the WDDIS bit, and while it is set, the fields WDV and WDD |
| 140 | * must not be modified. |
| 141 | * If the watchdog is enabled, then the timeout can be updated. Else, |
| 142 | * wait that the user enables it. |
| 143 | */ |
| 144 | if (wdt_enabled) |
| 145 | wdt_write(wdt, AT91_WDT_MR, wdt->mr & ~AT91_WDT_WDDIS); |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 146 | |
| 147 | wdd->timeout = timeout; |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static const struct watchdog_info sama5d4_wdt_info = { |
| 153 | .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING, |
| 154 | .identity = "Atmel SAMA5D4 Watchdog", |
| 155 | }; |
| 156 | |
Bhumika Goyal | b893e34 | 2017-01-28 13:11:17 +0530 | [diff] [blame] | 157 | static const struct watchdog_ops sama5d4_wdt_ops = { |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 158 | .owner = THIS_MODULE, |
| 159 | .start = sama5d4_wdt_start, |
| 160 | .stop = sama5d4_wdt_stop, |
| 161 | .ping = sama5d4_wdt_ping, |
| 162 | .set_timeout = sama5d4_wdt_set_timeout, |
| 163 | }; |
| 164 | |
| 165 | static irqreturn_t sama5d4_wdt_irq_handler(int irq, void *dev_id) |
| 166 | { |
| 167 | struct sama5d4_wdt *wdt = platform_get_drvdata(dev_id); |
Eugen Hristev | bb44aa0 | 2019-11-18 08:50:36 +0000 | [diff] [blame] | 168 | u32 reg; |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 169 | |
Eugen Hristev | bb44aa0 | 2019-11-18 08:50:36 +0000 | [diff] [blame] | 170 | if (wdt->sam9x60_support) |
| 171 | reg = wdt_read(wdt, AT91_SAM9X60_ISR); |
| 172 | else |
| 173 | reg = wdt_read(wdt, AT91_WDT_SR); |
| 174 | |
| 175 | if (reg) { |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 176 | pr_crit("Atmel Watchdog Software Reset\n"); |
| 177 | emergency_restart(); |
| 178 | pr_crit("Reboot didn't succeed\n"); |
| 179 | } |
| 180 | |
| 181 | return IRQ_HANDLED; |
| 182 | } |
| 183 | |
| 184 | static int of_sama5d4_wdt_init(struct device_node *np, struct sama5d4_wdt *wdt) |
| 185 | { |
| 186 | const char *tmp; |
| 187 | |
Eugen Hristev | bb44aa0 | 2019-11-18 08:50:36 +0000 | [diff] [blame] | 188 | if (wdt->sam9x60_support) |
| 189 | wdt->mr = AT91_SAM9X60_WDDIS; |
| 190 | else |
| 191 | wdt->mr = AT91_WDT_WDDIS; |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 192 | |
| 193 | if (!of_property_read_string(np, "atmel,watchdog-type", &tmp) && |
| 194 | !strcmp(tmp, "software")) |
Eugen Hristev | bb44aa0 | 2019-11-18 08:50:36 +0000 | [diff] [blame] | 195 | wdt->need_irq = true; |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 196 | |
| 197 | if (of_property_read_bool(np, "atmel,idle-halt")) |
Alexandre Belloni | 722ce63 | 2017-01-30 18:18:47 +0100 | [diff] [blame] | 198 | wdt->mr |= AT91_WDT_WDIDLEHLT; |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 199 | |
| 200 | if (of_property_read_bool(np, "atmel,dbg-halt")) |
Alexandre Belloni | 722ce63 | 2017-01-30 18:18:47 +0100 | [diff] [blame] | 201 | wdt->mr |= AT91_WDT_WDDBGHLT; |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static int sama5d4_wdt_init(struct sama5d4_wdt *wdt) |
| 207 | { |
Eugen Hristev | bb44aa0 | 2019-11-18 08:50:36 +0000 | [diff] [blame] | 208 | u32 reg, val; |
| 209 | |
| 210 | val = WDT_SEC2TICKS(WDT_DEFAULT_TIMEOUT); |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 211 | /* |
Alexandre Belloni | 015b528 | 2017-03-02 18:31:11 +0100 | [diff] [blame] | 212 | * When booting and resuming, the bootloader may have changed the |
| 213 | * watchdog configuration. |
| 214 | * If the watchdog is already running, we can safely update it. |
| 215 | * Else, we have to disable it properly. |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 216 | */ |
Eugen Hristev | bb44aa0 | 2019-11-18 08:50:36 +0000 | [diff] [blame] | 217 | if (!wdt_enabled) { |
Alexandre Belloni | 015b528 | 2017-03-02 18:31:11 +0100 | [diff] [blame] | 218 | reg = wdt_read(wdt, AT91_WDT_MR); |
Eugen Hristev | bb44aa0 | 2019-11-18 08:50:36 +0000 | [diff] [blame] | 219 | if (wdt->sam9x60_support && (!(reg & AT91_SAM9X60_WDDIS))) |
| 220 | wdt_write_nosleep(wdt, AT91_WDT_MR, |
| 221 | reg | AT91_SAM9X60_WDDIS); |
| 222 | else if (!wdt->sam9x60_support && |
| 223 | (!(reg & AT91_WDT_WDDIS))) |
Alexandre Belloni | ddd6d24 | 2017-03-02 18:31:12 +0100 | [diff] [blame] | 224 | wdt_write_nosleep(wdt, AT91_WDT_MR, |
| 225 | reg | AT91_WDT_WDDIS); |
Alexandre Belloni | 015b528 | 2017-03-02 18:31:11 +0100 | [diff] [blame] | 226 | } |
Eugen Hristev | bb44aa0 | 2019-11-18 08:50:36 +0000 | [diff] [blame] | 227 | |
| 228 | if (wdt->sam9x60_support) { |
| 229 | if (wdt->need_irq) |
| 230 | wdt->ir = AT91_SAM9X60_PERINT; |
| 231 | else |
| 232 | wdt->mr |= AT91_SAM9X60_PERIODRST; |
| 233 | |
| 234 | wdt_write(wdt, AT91_SAM9X60_IER, wdt->ir); |
| 235 | wdt_write(wdt, AT91_SAM9X60_WLR, AT91_SAM9X60_SET_COUNTER(val)); |
| 236 | } else { |
| 237 | wdt->mr |= AT91_WDT_SET_WDD(WDT_SEC2TICKS(MAX_WDT_TIMEOUT)); |
| 238 | wdt->mr |= AT91_WDT_SET_WDV(val); |
| 239 | |
| 240 | if (wdt->need_irq) |
| 241 | wdt->mr |= AT91_WDT_WDFIEN; |
| 242 | else |
| 243 | wdt->mr |= AT91_WDT_WDRSTEN; |
| 244 | } |
| 245 | |
| 246 | wdt_write_nosleep(wdt, AT91_WDT_MR, wdt->mr); |
| 247 | |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | static int sama5d4_wdt_probe(struct platform_device *pdev) |
| 252 | { |
Guenter Roeck | dcc3ce0 | 2019-04-09 10:23:54 -0700 | [diff] [blame] | 253 | struct device *dev = &pdev->dev; |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 254 | struct watchdog_device *wdd; |
| 255 | struct sama5d4_wdt *wdt; |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 256 | void __iomem *regs; |
| 257 | u32 irq = 0; |
| 258 | int ret; |
| 259 | |
Guenter Roeck | dcc3ce0 | 2019-04-09 10:23:54 -0700 | [diff] [blame] | 260 | wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL); |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 261 | if (!wdt) |
| 262 | return -ENOMEM; |
| 263 | |
| 264 | wdd = &wdt->wdd; |
Marcus Folkesson | 976932e | 2018-02-11 21:08:41 +0100 | [diff] [blame] | 265 | wdd->timeout = WDT_DEFAULT_TIMEOUT; |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 266 | wdd->info = &sama5d4_wdt_info; |
| 267 | wdd->ops = &sama5d4_wdt_ops; |
| 268 | wdd->min_timeout = MIN_WDT_TIMEOUT; |
| 269 | wdd->max_timeout = MAX_WDT_TIMEOUT; |
Alexandre Belloni | ddd6d24 | 2017-03-02 18:31:12 +0100 | [diff] [blame] | 270 | wdt->last_ping = jiffies; |
Eugen Hristev | 5ae233f | 2021-05-27 13:01:19 +0300 | [diff] [blame] | 271 | |
| 272 | if (of_device_is_compatible(dev->of_node, "microchip,sam9x60-wdt") || |
| 273 | of_device_is_compatible(dev->of_node, "microchip,sama7g5-wdt")) |
| 274 | wdt->sam9x60_support = true; |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 275 | |
| 276 | watchdog_set_drvdata(wdd, wdt); |
| 277 | |
Guenter Roeck | 0f0a6a2 | 2019-04-02 12:01:53 -0700 | [diff] [blame] | 278 | regs = devm_platform_ioremap_resource(pdev, 0); |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 279 | if (IS_ERR(regs)) |
| 280 | return PTR_ERR(regs); |
| 281 | |
| 282 | wdt->reg_base = regs; |
| 283 | |
Guenter Roeck | dcc3ce0 | 2019-04-09 10:23:54 -0700 | [diff] [blame] | 284 | ret = of_sama5d4_wdt_init(dev->of_node, wdt); |
Alexandre Belloni | 39bd56d | 2017-03-02 18:31:13 +0100 | [diff] [blame] | 285 | if (ret) |
| 286 | return ret; |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 287 | |
Eugen Hristev | bb44aa0 | 2019-11-18 08:50:36 +0000 | [diff] [blame] | 288 | if (wdt->need_irq) { |
| 289 | irq = irq_of_parse_and_map(dev->of_node, 0); |
| 290 | if (!irq) { |
| 291 | dev_warn(dev, "failed to get IRQ from DT\n"); |
| 292 | wdt->need_irq = false; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | if (wdt->need_irq) { |
Guenter Roeck | dcc3ce0 | 2019-04-09 10:23:54 -0700 | [diff] [blame] | 297 | ret = devm_request_irq(dev, irq, sama5d4_wdt_irq_handler, |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 298 | IRQF_SHARED | IRQF_IRQPOLL | |
| 299 | IRQF_NO_SUSPEND, pdev->name, pdev); |
| 300 | if (ret) { |
Guenter Roeck | dcc3ce0 | 2019-04-09 10:23:54 -0700 | [diff] [blame] | 301 | dev_err(dev, "cannot register interrupt handler\n"); |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 302 | return ret; |
| 303 | } |
| 304 | } |
| 305 | |
Guenter Roeck | dcc3ce0 | 2019-04-09 10:23:54 -0700 | [diff] [blame] | 306 | watchdog_init_timeout(wdd, wdt_timeout, dev); |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 307 | |
| 308 | ret = sama5d4_wdt_init(wdt); |
| 309 | if (ret) |
| 310 | return ret; |
| 311 | |
| 312 | watchdog_set_nowayout(wdd, nowayout); |
| 313 | |
Guenter Roeck | dcc3ce0 | 2019-04-09 10:23:54 -0700 | [diff] [blame] | 314 | watchdog_stop_on_unregister(wdd); |
| 315 | ret = devm_watchdog_register_device(dev, wdd); |
Wolfram Sang | 24b8eb7 | 2019-05-18 23:27:51 +0200 | [diff] [blame] | 316 | if (ret) |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 317 | return ret; |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 318 | |
| 319 | platform_set_drvdata(pdev, wdt); |
| 320 | |
Guenter Roeck | dcc3ce0 | 2019-04-09 10:23:54 -0700 | [diff] [blame] | 321 | dev_info(dev, "initialized (timeout = %d sec, nowayout = %d)\n", |
Marcus Folkesson | 976932e | 2018-02-11 21:08:41 +0100 | [diff] [blame] | 322 | wdd->timeout, nowayout); |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 327 | static const struct of_device_id sama5d4_wdt_of_match[] = { |
Eugen Hristev | bb44aa0 | 2019-11-18 08:50:36 +0000 | [diff] [blame] | 328 | { |
| 329 | .compatible = "atmel,sama5d4-wdt", |
| 330 | }, |
| 331 | { |
| 332 | .compatible = "microchip,sam9x60-wdt", |
| 333 | }, |
Eugen Hristev | 5ae233f | 2021-05-27 13:01:19 +0300 | [diff] [blame] | 334 | { |
| 335 | .compatible = "microchip,sama7g5-wdt", |
| 336 | }, |
| 337 | |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 338 | { } |
| 339 | }; |
| 340 | MODULE_DEVICE_TABLE(of, sama5d4_wdt_of_match); |
| 341 | |
Alexandre Belloni | f201353 | 2017-01-30 18:18:48 +0100 | [diff] [blame] | 342 | #ifdef CONFIG_PM_SLEEP |
Ken Sloat | 8d209eb | 2019-06-14 12:53:22 +0000 | [diff] [blame] | 343 | static int sama5d4_wdt_suspend_late(struct device *dev) |
| 344 | { |
| 345 | struct sama5d4_wdt *wdt = dev_get_drvdata(dev); |
| 346 | |
| 347 | if (watchdog_active(&wdt->wdd)) |
| 348 | sama5d4_wdt_stop(&wdt->wdd); |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | static int sama5d4_wdt_resume_early(struct device *dev) |
Alexandre Belloni | f201353 | 2017-01-30 18:18:48 +0100 | [diff] [blame] | 354 | { |
| 355 | struct sama5d4_wdt *wdt = dev_get_drvdata(dev); |
| 356 | |
Alexandre Belloni | 5dca80f6 | 2017-03-02 18:31:14 +0100 | [diff] [blame] | 357 | /* |
| 358 | * FIXME: writing MR also pings the watchdog which may not be desired. |
| 359 | * This should only be done when the registers are lost on suspend but |
| 360 | * there is no way to get this information right now. |
| 361 | */ |
Alexandre Belloni | 015b528 | 2017-03-02 18:31:11 +0100 | [diff] [blame] | 362 | sama5d4_wdt_init(wdt); |
Alexandre Belloni | f201353 | 2017-01-30 18:18:48 +0100 | [diff] [blame] | 363 | |
Ken Sloat | 8d209eb | 2019-06-14 12:53:22 +0000 | [diff] [blame] | 364 | if (watchdog_active(&wdt->wdd)) |
| 365 | sama5d4_wdt_start(&wdt->wdd); |
| 366 | |
Alexandre Belloni | f201353 | 2017-01-30 18:18:48 +0100 | [diff] [blame] | 367 | return 0; |
| 368 | } |
| 369 | #endif |
| 370 | |
Ken Sloat | 8d209eb | 2019-06-14 12:53:22 +0000 | [diff] [blame] | 371 | static const struct dev_pm_ops sama5d4_wdt_pm_ops = { |
| 372 | SET_LATE_SYSTEM_SLEEP_PM_OPS(sama5d4_wdt_suspend_late, |
| 373 | sama5d4_wdt_resume_early) |
| 374 | }; |
Alexandre Belloni | f201353 | 2017-01-30 18:18:48 +0100 | [diff] [blame] | 375 | |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 376 | static struct platform_driver sama5d4_wdt_driver = { |
| 377 | .probe = sama5d4_wdt_probe, |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 378 | .driver = { |
| 379 | .name = "sama5d4_wdt", |
Alexandre Belloni | f201353 | 2017-01-30 18:18:48 +0100 | [diff] [blame] | 380 | .pm = &sama5d4_wdt_pm_ops, |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 381 | .of_match_table = sama5d4_wdt_of_match, |
| 382 | } |
| 383 | }; |
| 384 | module_platform_driver(sama5d4_wdt_driver); |
| 385 | |
| 386 | MODULE_AUTHOR("Atmel Corporation"); |
| 387 | MODULE_DESCRIPTION("Atmel SAMA5D4 Watchdog Timer driver"); |
| 388 | MODULE_LICENSE("GPL v2"); |