| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Driver for Atmel SAMA5D4 Watchdog Timer |
|---|
| 3 | 4 | * |
|---|
| 4 | | - * Copyright (C) 2015 Atmel Corporation |
|---|
| 5 | | - * |
|---|
| 6 | | - * Licensed under GPLv2. |
|---|
| 5 | + * Copyright (C) 2015-2019 Microchip Technology Inc. and its subsidiaries |
|---|
| 7 | 6 | */ |
|---|
| 8 | 7 | |
|---|
| 9 | 8 | #include <linux/delay.h> |
|---|
| .. | .. |
|---|
| 12 | 11 | #include <linux/kernel.h> |
|---|
| 13 | 12 | #include <linux/module.h> |
|---|
| 14 | 13 | #include <linux/of.h> |
|---|
| 14 | +#include <linux/of_device.h> |
|---|
| 15 | 15 | #include <linux/of_irq.h> |
|---|
| 16 | 16 | #include <linux/platform_device.h> |
|---|
| 17 | 17 | #include <linux/reboot.h> |
|---|
| .. | .. |
|---|
| 30 | 30 | struct watchdog_device wdd; |
|---|
| 31 | 31 | void __iomem *reg_base; |
|---|
| 32 | 32 | u32 mr; |
|---|
| 33 | + u32 ir; |
|---|
| 33 | 34 | unsigned long last_ping; |
|---|
| 35 | + bool need_irq; |
|---|
| 36 | + bool sam9x60_support; |
|---|
| 34 | 37 | }; |
|---|
| 35 | 38 | |
|---|
| 36 | 39 | static int wdt_timeout; |
|---|
| .. | .. |
|---|
| 79 | 82 | { |
|---|
| 80 | 83 | struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd); |
|---|
| 81 | 84 | |
|---|
| 82 | | - wdt->mr &= ~AT91_WDT_WDDIS; |
|---|
| 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 | + } |
|---|
| 83 | 91 | wdt_write(wdt, AT91_WDT_MR, wdt->mr); |
|---|
| 84 | 92 | |
|---|
| 85 | 93 | return 0; |
|---|
| .. | .. |
|---|
| 89 | 97 | { |
|---|
| 90 | 98 | struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd); |
|---|
| 91 | 99 | |
|---|
| 92 | | - wdt->mr |= AT91_WDT_WDDIS; |
|---|
| 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 | + } |
|---|
| 93 | 106 | wdt_write(wdt, AT91_WDT_MR, wdt->mr); |
|---|
| 94 | 107 | |
|---|
| 95 | 108 | return 0; |
|---|
| .. | .. |
|---|
| 109 | 122 | { |
|---|
| 110 | 123 | struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd); |
|---|
| 111 | 124 | u32 value = WDT_SEC2TICKS(timeout); |
|---|
| 125 | + |
|---|
| 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 | + } |
|---|
| 112 | 133 | |
|---|
| 113 | 134 | wdt->mr &= ~AT91_WDT_WDV; |
|---|
| 114 | 135 | wdt->mr |= AT91_WDT_SET_WDV(value); |
|---|
| .. | .. |
|---|
| 144 | 165 | static irqreturn_t sama5d4_wdt_irq_handler(int irq, void *dev_id) |
|---|
| 145 | 166 | { |
|---|
| 146 | 167 | struct sama5d4_wdt *wdt = platform_get_drvdata(dev_id); |
|---|
| 168 | + u32 reg; |
|---|
| 147 | 169 | |
|---|
| 148 | | - if (wdt_read(wdt, AT91_WDT_SR)) { |
|---|
| 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) { |
|---|
| 149 | 176 | pr_crit("Atmel Watchdog Software Reset\n"); |
|---|
| 150 | 177 | emergency_restart(); |
|---|
| 151 | 178 | pr_crit("Reboot didn't succeed\n"); |
|---|
| .. | .. |
|---|
| 158 | 185 | { |
|---|
| 159 | 186 | const char *tmp; |
|---|
| 160 | 187 | |
|---|
| 161 | | - wdt->mr = AT91_WDT_WDDIS; |
|---|
| 188 | + if (wdt->sam9x60_support) |
|---|
| 189 | + wdt->mr = AT91_SAM9X60_WDDIS; |
|---|
| 190 | + else |
|---|
| 191 | + wdt->mr = AT91_WDT_WDDIS; |
|---|
| 162 | 192 | |
|---|
| 163 | 193 | if (!of_property_read_string(np, "atmel,watchdog-type", &tmp) && |
|---|
| 164 | 194 | !strcmp(tmp, "software")) |
|---|
| 165 | | - wdt->mr |= AT91_WDT_WDFIEN; |
|---|
| 166 | | - else |
|---|
| 167 | | - wdt->mr |= AT91_WDT_WDRSTEN; |
|---|
| 195 | + wdt->need_irq = true; |
|---|
| 168 | 196 | |
|---|
| 169 | 197 | if (of_property_read_bool(np, "atmel,idle-halt")) |
|---|
| 170 | 198 | wdt->mr |= AT91_WDT_WDIDLEHLT; |
|---|
| .. | .. |
|---|
| 177 | 205 | |
|---|
| 178 | 206 | static int sama5d4_wdt_init(struct sama5d4_wdt *wdt) |
|---|
| 179 | 207 | { |
|---|
| 180 | | - u32 reg; |
|---|
| 208 | + u32 reg, val; |
|---|
| 209 | + |
|---|
| 210 | + val = WDT_SEC2TICKS(WDT_DEFAULT_TIMEOUT); |
|---|
| 181 | 211 | /* |
|---|
| 182 | 212 | * When booting and resuming, the bootloader may have changed the |
|---|
| 183 | 213 | * watchdog configuration. |
|---|
| 184 | 214 | * If the watchdog is already running, we can safely update it. |
|---|
| 185 | 215 | * Else, we have to disable it properly. |
|---|
| 186 | 216 | */ |
|---|
| 187 | | - if (wdt_enabled) { |
|---|
| 188 | | - wdt_write_nosleep(wdt, AT91_WDT_MR, wdt->mr); |
|---|
| 189 | | - } else { |
|---|
| 217 | + if (!wdt_enabled) { |
|---|
| 190 | 218 | reg = wdt_read(wdt, AT91_WDT_MR); |
|---|
| 191 | | - if (!(reg & AT91_WDT_WDDIS)) |
|---|
| 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))) |
|---|
| 192 | 224 | wdt_write_nosleep(wdt, AT91_WDT_MR, |
|---|
| 193 | 225 | reg | AT91_WDT_WDDIS); |
|---|
| 194 | 226 | } |
|---|
| 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 | + |
|---|
| 195 | 248 | return 0; |
|---|
| 196 | 249 | } |
|---|
| 197 | 250 | |
|---|
| 198 | 251 | static int sama5d4_wdt_probe(struct platform_device *pdev) |
|---|
| 199 | 252 | { |
|---|
| 253 | + struct device *dev = &pdev->dev; |
|---|
| 200 | 254 | struct watchdog_device *wdd; |
|---|
| 201 | 255 | struct sama5d4_wdt *wdt; |
|---|
| 202 | | - struct resource *res; |
|---|
| 203 | 256 | void __iomem *regs; |
|---|
| 204 | 257 | u32 irq = 0; |
|---|
| 205 | | - u32 timeout; |
|---|
| 206 | 258 | int ret; |
|---|
| 207 | 259 | |
|---|
| 208 | | - wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL); |
|---|
| 260 | + wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL); |
|---|
| 209 | 261 | if (!wdt) |
|---|
| 210 | 262 | return -ENOMEM; |
|---|
| 211 | 263 | |
|---|
| .. | .. |
|---|
| 216 | 268 | wdd->min_timeout = MIN_WDT_TIMEOUT; |
|---|
| 217 | 269 | wdd->max_timeout = MAX_WDT_TIMEOUT; |
|---|
| 218 | 270 | wdt->last_ping = jiffies; |
|---|
| 271 | + wdt->sam9x60_support = of_device_is_compatible(dev->of_node, |
|---|
| 272 | + "microchip,sam9x60-wdt"); |
|---|
| 219 | 273 | |
|---|
| 220 | 274 | watchdog_set_drvdata(wdd, wdt); |
|---|
| 221 | 275 | |
|---|
| 222 | | - res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
|---|
| 223 | | - regs = devm_ioremap_resource(&pdev->dev, res); |
|---|
| 276 | + regs = devm_platform_ioremap_resource(pdev, 0); |
|---|
| 224 | 277 | if (IS_ERR(regs)) |
|---|
| 225 | 278 | return PTR_ERR(regs); |
|---|
| 226 | 279 | |
|---|
| 227 | 280 | wdt->reg_base = regs; |
|---|
| 228 | 281 | |
|---|
| 229 | | - irq = irq_of_parse_and_map(pdev->dev.of_node, 0); |
|---|
| 230 | | - if (!irq) |
|---|
| 231 | | - dev_warn(&pdev->dev, "failed to get IRQ from DT\n"); |
|---|
| 232 | | - |
|---|
| 233 | | - ret = of_sama5d4_wdt_init(pdev->dev.of_node, wdt); |
|---|
| 282 | + ret = of_sama5d4_wdt_init(dev->of_node, wdt); |
|---|
| 234 | 283 | if (ret) |
|---|
| 235 | 284 | return ret; |
|---|
| 236 | 285 | |
|---|
| 237 | | - if ((wdt->mr & AT91_WDT_WDFIEN) && irq) { |
|---|
| 238 | | - ret = devm_request_irq(&pdev->dev, irq, sama5d4_wdt_irq_handler, |
|---|
| 286 | + if (wdt->need_irq) { |
|---|
| 287 | + irq = irq_of_parse_and_map(dev->of_node, 0); |
|---|
| 288 | + if (!irq) { |
|---|
| 289 | + dev_warn(dev, "failed to get IRQ from DT\n"); |
|---|
| 290 | + wdt->need_irq = false; |
|---|
| 291 | + } |
|---|
| 292 | + } |
|---|
| 293 | + |
|---|
| 294 | + if (wdt->need_irq) { |
|---|
| 295 | + ret = devm_request_irq(dev, irq, sama5d4_wdt_irq_handler, |
|---|
| 239 | 296 | IRQF_SHARED | IRQF_IRQPOLL | |
|---|
| 240 | 297 | IRQF_NO_SUSPEND, pdev->name, pdev); |
|---|
| 241 | 298 | if (ret) { |
|---|
| 242 | | - dev_err(&pdev->dev, |
|---|
| 243 | | - "cannot register interrupt handler\n"); |
|---|
| 299 | + dev_err(dev, "cannot register interrupt handler\n"); |
|---|
| 244 | 300 | return ret; |
|---|
| 245 | 301 | } |
|---|
| 246 | 302 | } |
|---|
| 247 | 303 | |
|---|
| 248 | | - watchdog_init_timeout(wdd, wdt_timeout, &pdev->dev); |
|---|
| 249 | | - |
|---|
| 250 | | - timeout = WDT_SEC2TICKS(wdd->timeout); |
|---|
| 251 | | - |
|---|
| 252 | | - wdt->mr |= AT91_WDT_SET_WDD(WDT_SEC2TICKS(MAX_WDT_TIMEOUT)); |
|---|
| 253 | | - wdt->mr |= AT91_WDT_SET_WDV(timeout); |
|---|
| 304 | + watchdog_init_timeout(wdd, wdt_timeout, dev); |
|---|
| 254 | 305 | |
|---|
| 255 | 306 | ret = sama5d4_wdt_init(wdt); |
|---|
| 256 | 307 | if (ret) |
|---|
| .. | .. |
|---|
| 258 | 309 | |
|---|
| 259 | 310 | watchdog_set_nowayout(wdd, nowayout); |
|---|
| 260 | 311 | |
|---|
| 261 | | - ret = watchdog_register_device(wdd); |
|---|
| 262 | | - if (ret) { |
|---|
| 263 | | - dev_err(&pdev->dev, "failed to register watchdog device\n"); |
|---|
| 312 | + watchdog_stop_on_unregister(wdd); |
|---|
| 313 | + ret = devm_watchdog_register_device(dev, wdd); |
|---|
| 314 | + if (ret) |
|---|
| 264 | 315 | return ret; |
|---|
| 265 | | - } |
|---|
| 266 | 316 | |
|---|
| 267 | 317 | platform_set_drvdata(pdev, wdt); |
|---|
| 268 | 318 | |
|---|
| 269 | | - dev_info(&pdev->dev, "initialized (timeout = %d sec, nowayout = %d)\n", |
|---|
| 319 | + dev_info(dev, "initialized (timeout = %d sec, nowayout = %d)\n", |
|---|
| 270 | 320 | wdd->timeout, nowayout); |
|---|
| 271 | 321 | |
|---|
| 272 | 322 | return 0; |
|---|
| 273 | 323 | } |
|---|
| 274 | 324 | |
|---|
| 275 | | -static int sama5d4_wdt_remove(struct platform_device *pdev) |
|---|
| 276 | | -{ |
|---|
| 277 | | - struct sama5d4_wdt *wdt = platform_get_drvdata(pdev); |
|---|
| 278 | | - |
|---|
| 279 | | - sama5d4_wdt_stop(&wdt->wdd); |
|---|
| 280 | | - |
|---|
| 281 | | - watchdog_unregister_device(&wdt->wdd); |
|---|
| 282 | | - |
|---|
| 283 | | - return 0; |
|---|
| 284 | | -} |
|---|
| 285 | | - |
|---|
| 286 | 325 | static const struct of_device_id sama5d4_wdt_of_match[] = { |
|---|
| 287 | | - { .compatible = "atmel,sama5d4-wdt", }, |
|---|
| 326 | + { |
|---|
| 327 | + .compatible = "atmel,sama5d4-wdt", |
|---|
| 328 | + }, |
|---|
| 329 | + { |
|---|
| 330 | + .compatible = "microchip,sam9x60-wdt", |
|---|
| 331 | + }, |
|---|
| 288 | 332 | { } |
|---|
| 289 | 333 | }; |
|---|
| 290 | 334 | MODULE_DEVICE_TABLE(of, sama5d4_wdt_of_match); |
|---|
| 291 | 335 | |
|---|
| 292 | 336 | #ifdef CONFIG_PM_SLEEP |
|---|
| 293 | | -static int sama5d4_wdt_resume(struct device *dev) |
|---|
| 337 | +static int sama5d4_wdt_suspend_late(struct device *dev) |
|---|
| 338 | +{ |
|---|
| 339 | + struct sama5d4_wdt *wdt = dev_get_drvdata(dev); |
|---|
| 340 | + |
|---|
| 341 | + if (watchdog_active(&wdt->wdd)) |
|---|
| 342 | + sama5d4_wdt_stop(&wdt->wdd); |
|---|
| 343 | + |
|---|
| 344 | + return 0; |
|---|
| 345 | +} |
|---|
| 346 | + |
|---|
| 347 | +static int sama5d4_wdt_resume_early(struct device *dev) |
|---|
| 294 | 348 | { |
|---|
| 295 | 349 | struct sama5d4_wdt *wdt = dev_get_drvdata(dev); |
|---|
| 296 | 350 | |
|---|
| .. | .. |
|---|
| 301 | 355 | */ |
|---|
| 302 | 356 | sama5d4_wdt_init(wdt); |
|---|
| 303 | 357 | |
|---|
| 358 | + if (watchdog_active(&wdt->wdd)) |
|---|
| 359 | + sama5d4_wdt_start(&wdt->wdd); |
|---|
| 360 | + |
|---|
| 304 | 361 | return 0; |
|---|
| 305 | 362 | } |
|---|
| 306 | 363 | #endif |
|---|
| 307 | 364 | |
|---|
| 308 | | -static SIMPLE_DEV_PM_OPS(sama5d4_wdt_pm_ops, NULL, |
|---|
| 309 | | - sama5d4_wdt_resume); |
|---|
| 365 | +static const struct dev_pm_ops sama5d4_wdt_pm_ops = { |
|---|
| 366 | + SET_LATE_SYSTEM_SLEEP_PM_OPS(sama5d4_wdt_suspend_late, |
|---|
| 367 | + sama5d4_wdt_resume_early) |
|---|
| 368 | +}; |
|---|
| 310 | 369 | |
|---|
| 311 | 370 | static struct platform_driver sama5d4_wdt_driver = { |
|---|
| 312 | 371 | .probe = sama5d4_wdt_probe, |
|---|
| 313 | | - .remove = sama5d4_wdt_remove, |
|---|
| 314 | 372 | .driver = { |
|---|
| 315 | 373 | .name = "sama5d4_wdt", |
|---|
| 316 | 374 | .pm = &sama5d4_wdt_pm_ops, |
|---|