blob: 647ae325e9aecf04d6d1ccaf8f3a141911d7baee [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jean-Christophe PLAGNIOL-VILLARDd5ee38e2009-03-27 23:26:42 +01002/*
3 * [origin: Linux kernel drivers/watchdog/at91sam9_wdt.c]
4 *
Prasanthi Chellakumar0509c4e2018-10-09 11:46:40 -07005 * Watchdog driver for AT91SAM9x processors.
Jean-Christophe PLAGNIOL-VILLARDd5ee38e2009-03-27 23:26:42 +01006 *
7 * Copyright (C) 2008 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
8 * Copyright (C) 2008 Renaud CERRATO r.cerrato@til-technologies.fr
Jean-Christophe PLAGNIOL-VILLARDd5ee38e2009-03-27 23:26:42 +01009 */
10
11/*
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
15 */
16
Simon Glass0f2af882020-05-10 11:40:05 -060017#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060018#include <asm/global_data.h>
Reinhard Meyer585273f2011-02-04 20:17:33 +010019#include <asm/io.h>
Jean-Christophe PLAGNIOL-VILLARDd5ee38e2009-03-27 23:26:42 +010020#include <asm/arch/at91_wdt.h>
Prasanthi Chellakumar0509c4e2018-10-09 11:46:40 -070021#include <common.h>
Stefan Roesed052ef82019-04-02 10:57:19 +020022#include <div64.h>
Prasanthi Chellakumar0509c4e2018-10-09 11:46:40 -070023#include <dm.h>
24#include <errno.h>
25#include <wdt.h>
26
27DECLARE_GLOBAL_DATA_PTR;
Jean-Christophe PLAGNIOL-VILLARDd5ee38e2009-03-27 23:26:42 +010028
29/*
30 * AT91SAM9 watchdog runs a 12bit counter @ 256Hz,
31 * use this to convert a watchdog
Prasanthi Chellakumar0509c4e2018-10-09 11:46:40 -070032 * value from seconds.
Jean-Christophe PLAGNIOL-VILLARDd5ee38e2009-03-27 23:26:42 +010033 */
Prasanthi Chellakumar0509c4e2018-10-09 11:46:40 -070034#define WDT_SEC2TICKS(s) (((s) << 8) - 1)
Jean-Christophe PLAGNIOL-VILLARDd5ee38e2009-03-27 23:26:42 +010035
Jean-Christophe PLAGNIOL-VILLARDd5ee38e2009-03-27 23:26:42 +010036/*
37 * Set the watchdog time interval in 1/256Hz (write-once)
38 * Counter is 12 bit.
39 */
Stefan Roesed052ef82019-04-02 10:57:19 +020040static int at91_wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
Jean-Christophe PLAGNIOL-VILLARDd5ee38e2009-03-27 23:26:42 +010041{
Prasanthi Chellakumar0509c4e2018-10-09 11:46:40 -070042 struct at91_wdt_priv *priv = dev_get_priv(dev);
Stefan Roesed052ef82019-04-02 10:57:19 +020043 u64 timeout;
44 u32 ticks;
Prasanthi Chellakumar0509c4e2018-10-09 11:46:40 -070045
Stefan Roesed052ef82019-04-02 10:57:19 +020046 /* Calculate timeout in seconds and the resulting ticks */
47 timeout = timeout_ms;
48 do_div(timeout, 1000);
49 timeout = min_t(u64, timeout, WDT_MAX_TIMEOUT);
50 ticks = WDT_SEC2TICKS(timeout);
Jean-Christophe PLAGNIOL-VILLARDd5ee38e2009-03-27 23:26:42 +010051
52 /* Check if disabled */
Prasanthi Chellakumar0509c4e2018-10-09 11:46:40 -070053 if (readl(priv->regs + AT91_WDT_MR) & AT91_WDT_MR_WDDIS) {
Jean-Christophe PLAGNIOL-VILLARDd5ee38e2009-03-27 23:26:42 +010054 printf("sorry, watchdog is disabled\n");
55 return -1;
56 }
57
58 /*
59 * All counting occurs at SLOW_CLOCK / 128 = 256 Hz
60 *
61 * Since WDV is a 12-bit counter, the maximum period is
62 * 4096 / 256 = 16 seconds.
63 */
Prasanthi Chellakumar0509c4e2018-10-09 11:46:40 -070064 priv->regval = AT91_WDT_MR_WDRSTEN /* causes watchdog reset */
Achim Ehrlich2a649d62010-03-17 14:50:29 +010065 | AT91_WDT_MR_WDDBGHLT /* disabled in debug mode */
66 | AT91_WDT_MR_WDD(0xfff) /* restart at any time */
Stefan Roesed052ef82019-04-02 10:57:19 +020067 | AT91_WDT_MR_WDV(ticks); /* timer value */
Prasanthi Chellakumar0509c4e2018-10-09 11:46:40 -070068 writel(priv->regval, priv->regs + AT91_WDT_MR);
69
70 return 0;
71}
72
73static int at91_wdt_stop(struct udevice *dev)
74{
75 struct at91_wdt_priv *priv = dev_get_priv(dev);
76
77 /* Disable Watchdog Timer */
78 priv->regval |= AT91_WDT_MR_WDDIS;
79 writel(priv->regval, priv->regs + AT91_WDT_MR);
Jean-Christophe PLAGNIOL-VILLARDd5ee38e2009-03-27 23:26:42 +010080
81 return 0;
82}
83
Prasanthi Chellakumar0509c4e2018-10-09 11:46:40 -070084static int at91_wdt_reset(struct udevice *dev)
Jean-Christophe PLAGNIOL-VILLARDd5ee38e2009-03-27 23:26:42 +010085{
Prasanthi Chellakumar0509c4e2018-10-09 11:46:40 -070086 struct at91_wdt_priv *priv = dev_get_priv(dev);
87
88 writel(AT91_WDT_CR_WDRSTT | AT91_WDT_CR_KEY, priv->regs + AT91_WDT_CR);
89
90 return 0;
Jean-Christophe PLAGNIOL-VILLARDd5ee38e2009-03-27 23:26:42 +010091}
92
Prasanthi Chellakumar0509c4e2018-10-09 11:46:40 -070093static const struct wdt_ops at91_wdt_ops = {
94 .start = at91_wdt_start,
95 .stop = at91_wdt_stop,
96 .reset = at91_wdt_reset,
97};
98
99static const struct udevice_id at91_wdt_ids[] = {
100 { .compatible = "atmel,at91sam9260-wdt" },
101 {}
102};
103
104static int at91_wdt_probe(struct udevice *dev)
Jean-Christophe PLAGNIOL-VILLARDd5ee38e2009-03-27 23:26:42 +0100105{
Prasanthi Chellakumar0509c4e2018-10-09 11:46:40 -0700106 struct at91_wdt_priv *priv = dev_get_priv(dev);
107
108 priv->regs = dev_remap_addr(dev);
109 if (!priv->regs)
110 return -EINVAL;
111
Simon Glass75e534b2020-12-16 21:20:07 -0700112 debug("%s: Probing wdt%u\n", __func__, dev_seq(dev));
Prasanthi Chellakumar0509c4e2018-10-09 11:46:40 -0700113
114 return 0;
Jean-Christophe PLAGNIOL-VILLARDd5ee38e2009-03-27 23:26:42 +0100115}
Prasanthi Chellakumar0509c4e2018-10-09 11:46:40 -0700116
Walter Lozano2901ac62020-06-25 01:10:04 -0300117U_BOOT_DRIVER(atmel_at91sam9260_wdt) = {
118 .name = "atmel_at91sam9260_wdt",
Prasanthi Chellakumar0509c4e2018-10-09 11:46:40 -0700119 .id = UCLASS_WDT,
120 .of_match = at91_wdt_ids,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700121 .priv_auto = sizeof(struct at91_wdt_priv),
Prasanthi Chellakumar0509c4e2018-10-09 11:46:40 -0700122 .ops = &at91_wdt_ops,
123 .probe = at91_wdt_probe,
124};