1 // SPDX-License-Identifier: GPL-2.0+
3 * [origin: Linux kernel drivers/watchdog/at91sam9_wdt.c]
5 * Watchdog driver for AT91SAM9x processors.
7 * Copyright (C) 2008 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
8 * Copyright (C) 2008 Renaud CERRATO r.cerrato@til-technologies.fr
12 * The Watchdog Timer Mode Register can be only written to once. If the
13 * timeout need to be set from U-Boot, be sure that the bootstrap doesn't
14 * write to this register. Inform Linux to it too
18 #include <asm/global_data.h>
20 #include <asm/arch/at91_wdt.h>
27 DECLARE_GLOBAL_DATA_PTR;
30 * AT91SAM9 watchdog runs a 12bit counter @ 256Hz,
31 * use this to convert a watchdog
34 #define WDT_SEC2TICKS(s) (((s) << 8) - 1)
37 * Set the watchdog time interval in 1/256Hz (write-once)
40 static int at91_wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
42 struct at91_wdt_priv *priv = dev_get_priv(dev);
46 /* Calculate timeout in seconds and the resulting ticks */
48 do_div(timeout, 1000);
49 timeout = min_t(u64, timeout, WDT_MAX_TIMEOUT);
50 ticks = WDT_SEC2TICKS(timeout);
52 /* Check if disabled */
53 if (readl(priv->regs + AT91_WDT_MR) & AT91_WDT_MR_WDDIS) {
54 printf("sorry, watchdog is disabled\n");
59 * All counting occurs at SLOW_CLOCK / 128 = 256 Hz
61 * Since WDV is a 12-bit counter, the maximum period is
62 * 4096 / 256 = 16 seconds.
64 priv->regval = AT91_WDT_MR_WDRSTEN /* causes watchdog reset */
65 | AT91_WDT_MR_WDDBGHLT /* disabled in debug mode */
66 | AT91_WDT_MR_WDD(0xfff) /* restart at any time */
67 | AT91_WDT_MR_WDV(ticks); /* timer value */
68 writel(priv->regval, priv->regs + AT91_WDT_MR);
73 static int at91_wdt_stop(struct udevice *dev)
75 struct at91_wdt_priv *priv = dev_get_priv(dev);
77 /* Disable Watchdog Timer */
78 priv->regval |= AT91_WDT_MR_WDDIS;
79 writel(priv->regval, priv->regs + AT91_WDT_MR);
84 static int at91_wdt_reset(struct udevice *dev)
86 struct at91_wdt_priv *priv = dev_get_priv(dev);
88 writel(AT91_WDT_CR_WDRSTT | AT91_WDT_CR_KEY, priv->regs + AT91_WDT_CR);
93 static const struct wdt_ops at91_wdt_ops = {
94 .start = at91_wdt_start,
95 .stop = at91_wdt_stop,
96 .reset = at91_wdt_reset,
99 static const struct udevice_id at91_wdt_ids[] = {
100 { .compatible = "atmel,at91sam9260-wdt" },
104 static int at91_wdt_probe(struct udevice *dev)
106 struct at91_wdt_priv *priv = dev_get_priv(dev);
108 priv->regs = dev_remap_addr(dev);
112 debug("%s: Probing wdt%u\n", __func__, dev_seq(dev));
117 U_BOOT_DRIVER(atmel_at91sam9260_wdt) = {
118 .name = "atmel_at91sam9260_wdt",
120 .of_match = at91_wdt_ids,
121 .priv_auto = sizeof(struct at91_wdt_priv),
122 .ops = &at91_wdt_ops,
123 .probe = at91_wdt_probe,