Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 2 | /* |
| 3 | * [origin: Linux kernel drivers/watchdog/at91sam9_wdt.c] |
| 4 | * |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 5 | * Watchdog driver for AT91SAM9x processors. |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 6 | * |
| 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-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 9 | */ |
| 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 | |
Reinhard Meyer | 585273f | 2011-02-04 20:17:33 +0100 | [diff] [blame] | 17 | #include <asm/io.h> |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 18 | #include <asm/arch/at91_wdt.h> |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 19 | #include <common.h> |
Stefan Roese | d052ef8 | 2019-04-02 10:57:19 +0200 | [diff] [blame] | 20 | #include <div64.h> |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 21 | #include <dm.h> |
| 22 | #include <errno.h> |
| 23 | #include <wdt.h> |
| 24 | |
| 25 | DECLARE_GLOBAL_DATA_PTR; |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 26 | |
| 27 | /* |
| 28 | * AT91SAM9 watchdog runs a 12bit counter @ 256Hz, |
| 29 | * use this to convert a watchdog |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 30 | * value from seconds. |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 31 | */ |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 32 | #define WDT_SEC2TICKS(s) (((s) << 8) - 1) |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 33 | |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 34 | /* |
| 35 | * Set the watchdog time interval in 1/256Hz (write-once) |
| 36 | * Counter is 12 bit. |
| 37 | */ |
Stefan Roese | d052ef8 | 2019-04-02 10:57:19 +0200 | [diff] [blame] | 38 | static int at91_wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags) |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 39 | { |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 40 | struct at91_wdt_priv *priv = dev_get_priv(dev); |
Stefan Roese | d052ef8 | 2019-04-02 10:57:19 +0200 | [diff] [blame] | 41 | u64 timeout; |
| 42 | u32 ticks; |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 43 | |
Stefan Roese | d052ef8 | 2019-04-02 10:57:19 +0200 | [diff] [blame] | 44 | /* Calculate timeout in seconds and the resulting ticks */ |
| 45 | timeout = timeout_ms; |
| 46 | do_div(timeout, 1000); |
| 47 | timeout = min_t(u64, timeout, WDT_MAX_TIMEOUT); |
| 48 | ticks = WDT_SEC2TICKS(timeout); |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 49 | |
| 50 | /* Check if disabled */ |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 51 | if (readl(priv->regs + AT91_WDT_MR) & AT91_WDT_MR_WDDIS) { |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 52 | printf("sorry, watchdog is disabled\n"); |
| 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * All counting occurs at SLOW_CLOCK / 128 = 256 Hz |
| 58 | * |
| 59 | * Since WDV is a 12-bit counter, the maximum period is |
| 60 | * 4096 / 256 = 16 seconds. |
| 61 | */ |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 62 | priv->regval = AT91_WDT_MR_WDRSTEN /* causes watchdog reset */ |
Achim Ehrlich | 2a649d6 | 2010-03-17 14:50:29 +0100 | [diff] [blame] | 63 | | AT91_WDT_MR_WDDBGHLT /* disabled in debug mode */ |
| 64 | | AT91_WDT_MR_WDD(0xfff) /* restart at any time */ |
Stefan Roese | d052ef8 | 2019-04-02 10:57:19 +0200 | [diff] [blame] | 65 | | AT91_WDT_MR_WDV(ticks); /* timer value */ |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 66 | writel(priv->regval, priv->regs + AT91_WDT_MR); |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static int at91_wdt_stop(struct udevice *dev) |
| 72 | { |
| 73 | struct at91_wdt_priv *priv = dev_get_priv(dev); |
| 74 | |
| 75 | /* Disable Watchdog Timer */ |
| 76 | priv->regval |= AT91_WDT_MR_WDDIS; |
| 77 | writel(priv->regval, priv->regs + AT91_WDT_MR); |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 82 | static int at91_wdt_reset(struct udevice *dev) |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 83 | { |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 84 | struct at91_wdt_priv *priv = dev_get_priv(dev); |
| 85 | |
| 86 | writel(AT91_WDT_CR_WDRSTT | AT91_WDT_CR_KEY, priv->regs + AT91_WDT_CR); |
| 87 | |
| 88 | return 0; |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 91 | static const struct wdt_ops at91_wdt_ops = { |
| 92 | .start = at91_wdt_start, |
| 93 | .stop = at91_wdt_stop, |
| 94 | .reset = at91_wdt_reset, |
| 95 | }; |
| 96 | |
| 97 | static const struct udevice_id at91_wdt_ids[] = { |
| 98 | { .compatible = "atmel,at91sam9260-wdt" }, |
| 99 | {} |
| 100 | }; |
| 101 | |
| 102 | static int at91_wdt_probe(struct udevice *dev) |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 103 | { |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 104 | struct at91_wdt_priv *priv = dev_get_priv(dev); |
| 105 | |
| 106 | priv->regs = dev_remap_addr(dev); |
| 107 | if (!priv->regs) |
| 108 | return -EINVAL; |
| 109 | |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 110 | debug("%s: Probing wdt%u\n", __func__, dev->seq); |
| 111 | |
| 112 | return 0; |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 113 | } |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 114 | |
| 115 | U_BOOT_DRIVER(at91_wdt) = { |
| 116 | .name = "at91_wdt", |
| 117 | .id = UCLASS_WDT, |
| 118 | .of_match = at91_wdt_ids, |
| 119 | .priv_auto_alloc_size = sizeof(struct at91_wdt_priv), |
| 120 | .ops = &at91_wdt_ops, |
| 121 | .probe = at91_wdt_probe, |
| 122 | }; |