Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Driver for Atmel SAMA5D4 Watchdog Timer |
| 3 | * |
| 4 | * Copyright (C) 2015 Atmel Corporation |
| 5 | * |
| 6 | * Licensed under GPLv2. |
| 7 | */ |
| 8 | |
Alexandre Belloni | ddd6d24 | 2017-03-02 18:31:12 +0100 | [diff] [blame] | 9 | #include <linux/delay.h> |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 10 | #include <linux/interrupt.h> |
| 11 | #include <linux/io.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/of.h> |
| 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; |
Alexandre Belloni | ddd6d24 | 2017-03-02 18:31:12 +0100 | [diff] [blame] | 33 | unsigned long last_ping; |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | static int wdt_timeout = WDT_DEFAULT_TIMEOUT; |
| 37 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 38 | |
| 39 | module_param(wdt_timeout, int, 0); |
| 40 | MODULE_PARM_DESC(wdt_timeout, |
| 41 | "Watchdog timeout in seconds. (default = " |
| 42 | __MODULE_STRING(WDT_DEFAULT_TIMEOUT) ")"); |
| 43 | |
| 44 | module_param(nowayout, bool, 0); |
| 45 | MODULE_PARM_DESC(nowayout, |
| 46 | "Watchdog cannot be stopped once started (default=" |
| 47 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 48 | |
Alexandre Belloni | 015b528 | 2017-03-02 18:31:11 +0100 | [diff] [blame] | 49 | #define wdt_enabled (!(wdt->mr & AT91_WDT_WDDIS)) |
| 50 | |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 51 | #define wdt_read(wdt, field) \ |
| 52 | readl_relaxed((wdt)->reg_base + (field)) |
| 53 | |
Alexandre Belloni | ddd6d24 | 2017-03-02 18:31:12 +0100 | [diff] [blame] | 54 | /* 4 slow clock periods is 4/32768 = 122.07µs*/ |
| 55 | #define WDT_DELAY usecs_to_jiffies(123) |
| 56 | |
| 57 | static void wdt_write(struct sama5d4_wdt *wdt, u32 field, u32 val) |
| 58 | { |
| 59 | /* |
| 60 | * WDT_CR and WDT_MR must not be modified within three slow clock |
| 61 | * periods following a restart of the watchdog performed by a write |
| 62 | * access in WDT_CR. |
| 63 | */ |
| 64 | while (time_before(jiffies, wdt->last_ping + WDT_DELAY)) |
| 65 | usleep_range(30, 125); |
| 66 | writel_relaxed(val, wdt->reg_base + field); |
| 67 | wdt->last_ping = jiffies; |
| 68 | } |
| 69 | |
| 70 | static void wdt_write_nosleep(struct sama5d4_wdt *wdt, u32 field, u32 val) |
| 71 | { |
| 72 | if (time_before(jiffies, wdt->last_ping + WDT_DELAY)) |
| 73 | udelay(123); |
| 74 | writel_relaxed(val, wdt->reg_base + field); |
| 75 | wdt->last_ping = jiffies; |
| 76 | } |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 77 | |
| 78 | static int sama5d4_wdt_start(struct watchdog_device *wdd) |
| 79 | { |
| 80 | struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd); |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 81 | |
Alexandre Belloni | 722ce63 | 2017-01-30 18:18:47 +0100 | [diff] [blame] | 82 | wdt->mr &= ~AT91_WDT_WDDIS; |
| 83 | wdt_write(wdt, AT91_WDT_MR, wdt->mr); |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static int sama5d4_wdt_stop(struct watchdog_device *wdd) |
| 89 | { |
| 90 | struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd); |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 91 | |
Alexandre Belloni | 722ce63 | 2017-01-30 18:18:47 +0100 | [diff] [blame] | 92 | wdt->mr |= AT91_WDT_WDDIS; |
| 93 | wdt_write(wdt, AT91_WDT_MR, wdt->mr); |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static int sama5d4_wdt_ping(struct watchdog_device *wdd) |
| 99 | { |
| 100 | struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd); |
| 101 | |
| 102 | wdt_write(wdt, AT91_WDT_CR, AT91_WDT_KEY | AT91_WDT_WDRSTT); |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static int sama5d4_wdt_set_timeout(struct watchdog_device *wdd, |
| 108 | unsigned int timeout) |
| 109 | { |
| 110 | struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd); |
| 111 | u32 value = WDT_SEC2TICKS(timeout); |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 112 | |
Alexandre Belloni | 722ce63 | 2017-01-30 18:18:47 +0100 | [diff] [blame] | 113 | wdt->mr &= ~AT91_WDT_WDV; |
| 114 | wdt->mr &= ~AT91_WDT_WDD; |
| 115 | wdt->mr |= AT91_WDT_SET_WDV(value); |
| 116 | wdt->mr |= AT91_WDT_SET_WDD(value); |
Alexandre Belloni | 015b528 | 2017-03-02 18:31:11 +0100 | [diff] [blame] | 117 | |
| 118 | /* |
| 119 | * WDDIS has to be 0 when updating WDD/WDV. The datasheet states: When |
| 120 | * setting the WDDIS bit, and while it is set, the fields WDV and WDD |
| 121 | * must not be modified. |
| 122 | * If the watchdog is enabled, then the timeout can be updated. Else, |
| 123 | * wait that the user enables it. |
| 124 | */ |
| 125 | if (wdt_enabled) |
| 126 | wdt_write(wdt, AT91_WDT_MR, wdt->mr & ~AT91_WDT_WDDIS); |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 127 | |
| 128 | wdd->timeout = timeout; |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static const struct watchdog_info sama5d4_wdt_info = { |
| 134 | .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING, |
| 135 | .identity = "Atmel SAMA5D4 Watchdog", |
| 136 | }; |
| 137 | |
Bhumika Goyal | b893e34 | 2017-01-28 13:11:17 +0530 | [diff] [blame] | 138 | static const struct watchdog_ops sama5d4_wdt_ops = { |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 139 | .owner = THIS_MODULE, |
| 140 | .start = sama5d4_wdt_start, |
| 141 | .stop = sama5d4_wdt_stop, |
| 142 | .ping = sama5d4_wdt_ping, |
| 143 | .set_timeout = sama5d4_wdt_set_timeout, |
| 144 | }; |
| 145 | |
| 146 | static irqreturn_t sama5d4_wdt_irq_handler(int irq, void *dev_id) |
| 147 | { |
| 148 | struct sama5d4_wdt *wdt = platform_get_drvdata(dev_id); |
| 149 | |
| 150 | if (wdt_read(wdt, AT91_WDT_SR)) { |
| 151 | pr_crit("Atmel Watchdog Software Reset\n"); |
| 152 | emergency_restart(); |
| 153 | pr_crit("Reboot didn't succeed\n"); |
| 154 | } |
| 155 | |
| 156 | return IRQ_HANDLED; |
| 157 | } |
| 158 | |
| 159 | static int of_sama5d4_wdt_init(struct device_node *np, struct sama5d4_wdt *wdt) |
| 160 | { |
| 161 | const char *tmp; |
| 162 | |
Alexandre Belloni | 722ce63 | 2017-01-30 18:18:47 +0100 | [diff] [blame] | 163 | wdt->mr = AT91_WDT_WDDIS; |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 164 | |
| 165 | if (!of_property_read_string(np, "atmel,watchdog-type", &tmp) && |
| 166 | !strcmp(tmp, "software")) |
Alexandre Belloni | 722ce63 | 2017-01-30 18:18:47 +0100 | [diff] [blame] | 167 | wdt->mr |= AT91_WDT_WDFIEN; |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 168 | else |
Alexandre Belloni | 722ce63 | 2017-01-30 18:18:47 +0100 | [diff] [blame] | 169 | wdt->mr |= AT91_WDT_WDRSTEN; |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 170 | |
| 171 | if (of_property_read_bool(np, "atmel,idle-halt")) |
Alexandre Belloni | 722ce63 | 2017-01-30 18:18:47 +0100 | [diff] [blame] | 172 | wdt->mr |= AT91_WDT_WDIDLEHLT; |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 173 | |
| 174 | if (of_property_read_bool(np, "atmel,dbg-halt")) |
Alexandre Belloni | 722ce63 | 2017-01-30 18:18:47 +0100 | [diff] [blame] | 175 | wdt->mr |= AT91_WDT_WDDBGHLT; |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int sama5d4_wdt_init(struct sama5d4_wdt *wdt) |
| 181 | { |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 182 | u32 reg; |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 183 | /* |
Alexandre Belloni | 015b528 | 2017-03-02 18:31:11 +0100 | [diff] [blame] | 184 | * When booting and resuming, the bootloader may have changed the |
| 185 | * watchdog configuration. |
| 186 | * If the watchdog is already running, we can safely update it. |
| 187 | * Else, we have to disable it properly. |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 188 | */ |
Alexandre Belloni | 015b528 | 2017-03-02 18:31:11 +0100 | [diff] [blame] | 189 | if (wdt_enabled) { |
Alexandre Belloni | ddd6d24 | 2017-03-02 18:31:12 +0100 | [diff] [blame] | 190 | wdt_write_nosleep(wdt, AT91_WDT_MR, wdt->mr); |
Alexandre Belloni | 015b528 | 2017-03-02 18:31:11 +0100 | [diff] [blame] | 191 | } else { |
| 192 | reg = wdt_read(wdt, AT91_WDT_MR); |
| 193 | if (!(reg & AT91_WDT_WDDIS)) |
Alexandre Belloni | ddd6d24 | 2017-03-02 18:31:12 +0100 | [diff] [blame] | 194 | wdt_write_nosleep(wdt, AT91_WDT_MR, |
| 195 | reg | AT91_WDT_WDDIS); |
Alexandre Belloni | 015b528 | 2017-03-02 18:31:11 +0100 | [diff] [blame] | 196 | } |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | static int sama5d4_wdt_probe(struct platform_device *pdev) |
| 201 | { |
| 202 | struct watchdog_device *wdd; |
| 203 | struct sama5d4_wdt *wdt; |
| 204 | struct resource *res; |
| 205 | void __iomem *regs; |
| 206 | u32 irq = 0; |
Alexandre Belloni | 015b528 | 2017-03-02 18:31:11 +0100 | [diff] [blame] | 207 | u32 timeout; |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 208 | int ret; |
| 209 | |
| 210 | wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL); |
| 211 | if (!wdt) |
| 212 | return -ENOMEM; |
| 213 | |
| 214 | wdd = &wdt->wdd; |
| 215 | wdd->timeout = wdt_timeout; |
| 216 | wdd->info = &sama5d4_wdt_info; |
| 217 | wdd->ops = &sama5d4_wdt_ops; |
| 218 | wdd->min_timeout = MIN_WDT_TIMEOUT; |
| 219 | wdd->max_timeout = MAX_WDT_TIMEOUT; |
Alexandre Belloni | ddd6d24 | 2017-03-02 18:31:12 +0100 | [diff] [blame] | 220 | wdt->last_ping = jiffies; |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 221 | |
| 222 | watchdog_set_drvdata(wdd, wdt); |
| 223 | |
| 224 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 225 | regs = devm_ioremap_resource(&pdev->dev, res); |
| 226 | if (IS_ERR(regs)) |
| 227 | return PTR_ERR(regs); |
| 228 | |
| 229 | wdt->reg_base = regs; |
| 230 | |
| 231 | if (pdev->dev.of_node) { |
| 232 | irq = irq_of_parse_and_map(pdev->dev.of_node, 0); |
| 233 | if (!irq) |
| 234 | dev_warn(&pdev->dev, "failed to get IRQ from DT\n"); |
| 235 | |
| 236 | ret = of_sama5d4_wdt_init(pdev->dev.of_node, wdt); |
| 237 | if (ret) |
| 238 | return ret; |
| 239 | } |
| 240 | |
Alexandre Belloni | 722ce63 | 2017-01-30 18:18:47 +0100 | [diff] [blame] | 241 | if ((wdt->mr & AT91_WDT_WDFIEN) && irq) { |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 242 | ret = devm_request_irq(&pdev->dev, irq, sama5d4_wdt_irq_handler, |
| 243 | IRQF_SHARED | IRQF_IRQPOLL | |
| 244 | IRQF_NO_SUSPEND, pdev->name, pdev); |
| 245 | if (ret) { |
| 246 | dev_err(&pdev->dev, |
| 247 | "cannot register interrupt handler\n"); |
| 248 | return ret; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | ret = watchdog_init_timeout(wdd, wdt_timeout, &pdev->dev); |
| 253 | if (ret) { |
| 254 | dev_err(&pdev->dev, "unable to set timeout value\n"); |
| 255 | return ret; |
| 256 | } |
| 257 | |
Alexandre Belloni | 015b528 | 2017-03-02 18:31:11 +0100 | [diff] [blame] | 258 | timeout = WDT_SEC2TICKS(wdd->timeout); |
| 259 | |
| 260 | wdt->mr |= AT91_WDT_SET_WDD(timeout); |
| 261 | wdt->mr |= AT91_WDT_SET_WDV(timeout); |
| 262 | |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 263 | ret = sama5d4_wdt_init(wdt); |
| 264 | if (ret) |
| 265 | return ret; |
| 266 | |
| 267 | watchdog_set_nowayout(wdd, nowayout); |
| 268 | |
| 269 | ret = watchdog_register_device(wdd); |
| 270 | if (ret) { |
| 271 | dev_err(&pdev->dev, "failed to register watchdog device\n"); |
| 272 | return ret; |
| 273 | } |
| 274 | |
| 275 | platform_set_drvdata(pdev, wdt); |
| 276 | |
| 277 | dev_info(&pdev->dev, "initialized (timeout = %d sec, nowayout = %d)\n", |
| 278 | wdt_timeout, nowayout); |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | static int sama5d4_wdt_remove(struct platform_device *pdev) |
| 284 | { |
| 285 | struct sama5d4_wdt *wdt = platform_get_drvdata(pdev); |
| 286 | |
| 287 | sama5d4_wdt_stop(&wdt->wdd); |
| 288 | |
| 289 | watchdog_unregister_device(&wdt->wdd); |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static const struct of_device_id sama5d4_wdt_of_match[] = { |
| 295 | { .compatible = "atmel,sama5d4-wdt", }, |
| 296 | { } |
| 297 | }; |
| 298 | MODULE_DEVICE_TABLE(of, sama5d4_wdt_of_match); |
| 299 | |
Alexandre Belloni | f201353 | 2017-01-30 18:18:48 +0100 | [diff] [blame] | 300 | #ifdef CONFIG_PM_SLEEP |
| 301 | static int sama5d4_wdt_resume(struct device *dev) |
| 302 | { |
| 303 | struct sama5d4_wdt *wdt = dev_get_drvdata(dev); |
| 304 | |
Alexandre Belloni | 015b528 | 2017-03-02 18:31:11 +0100 | [diff] [blame] | 305 | sama5d4_wdt_init(wdt); |
Alexandre Belloni | f201353 | 2017-01-30 18:18:48 +0100 | [diff] [blame] | 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | #endif |
| 310 | |
| 311 | static SIMPLE_DEV_PM_OPS(sama5d4_wdt_pm_ops, NULL, |
| 312 | sama5d4_wdt_resume); |
| 313 | |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 314 | static struct platform_driver sama5d4_wdt_driver = { |
| 315 | .probe = sama5d4_wdt_probe, |
| 316 | .remove = sama5d4_wdt_remove, |
| 317 | .driver = { |
| 318 | .name = "sama5d4_wdt", |
Alexandre Belloni | f201353 | 2017-01-30 18:18:48 +0100 | [diff] [blame] | 319 | .pm = &sama5d4_wdt_pm_ops, |
Wenyou Yang | 7653486 | 2015-08-06 18:16:46 +0800 | [diff] [blame] | 320 | .of_match_table = sama5d4_wdt_of_match, |
| 321 | } |
| 322 | }; |
| 323 | module_platform_driver(sama5d4_wdt_driver); |
| 324 | |
| 325 | MODULE_AUTHOR("Atmel Corporation"); |
| 326 | MODULE_DESCRIPTION("Atmel SAMA5D4 Watchdog Timer driver"); |
| 327 | MODULE_LICENSE("GPL v2"); |