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 | |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 17 | #include <log.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 18 | #include <asm/global_data.h> |
Reinhard Meyer | 585273f | 2011-02-04 20:17:33 +0100 | [diff] [blame] | 19 | #include <asm/io.h> |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 20 | #include <asm/arch/at91_wdt.h> |
Stefan Roese | d052ef8 | 2019-04-02 10:57:19 +0200 | [diff] [blame] | 21 | #include <div64.h> |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 22 | #include <dm.h> |
| 23 | #include <errno.h> |
| 24 | #include <wdt.h> |
| 25 | |
| 26 | DECLARE_GLOBAL_DATA_PTR; |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * AT91SAM9 watchdog runs a 12bit counter @ 256Hz, |
| 30 | * use this to convert a watchdog |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 31 | * value from seconds. |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 32 | */ |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 33 | #define WDT_SEC2TICKS(s) (((s) << 8) - 1) |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 34 | |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 35 | /* |
| 36 | * Set the watchdog time interval in 1/256Hz (write-once) |
| 37 | * Counter is 12 bit. |
| 38 | */ |
Stefan Roese | d052ef8 | 2019-04-02 10:57:19 +0200 | [diff] [blame] | 39 | 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] | 40 | { |
Zixun LI | 7c480ad | 2025-04-28 11:16:25 +0200 | [diff] [blame] | 41 | struct at91_wdt_priv *wdt = dev_get_priv(dev); |
Stefan Roese | d052ef8 | 2019-04-02 10:57:19 +0200 | [diff] [blame] | 42 | u64 timeout; |
| 43 | u32 ticks; |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 44 | |
Stefan Roese | d052ef8 | 2019-04-02 10:57:19 +0200 | [diff] [blame] | 45 | /* Calculate timeout in seconds and the resulting ticks */ |
| 46 | timeout = timeout_ms; |
| 47 | do_div(timeout, 1000); |
| 48 | timeout = min_t(u64, timeout, WDT_MAX_TIMEOUT); |
| 49 | ticks = WDT_SEC2TICKS(timeout); |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 50 | |
| 51 | /* Check if disabled */ |
Zixun LI | 991fdec | 2025-04-28 11:16:26 +0200 | [diff] [blame] | 52 | if (readl(wdt->regs + AT91_WDT_MR) & wdt->wddis) { |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 53 | printf("sorry, watchdog is disabled\n"); |
| 54 | return -1; |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * All counting occurs at SLOW_CLOCK / 128 = 256 Hz |
| 59 | * |
| 60 | * Since WDV is a 12-bit counter, the maximum period is |
| 61 | * 4096 / 256 = 16 seconds. |
| 62 | */ |
Zixun LI | 991fdec | 2025-04-28 11:16:26 +0200 | [diff] [blame] | 63 | |
| 64 | if (wdt->mode == AT91_WDT_MODE_SAM9260) { |
| 65 | wdt->mr = AT91_WDT_MR_WDRSTEN /* causes watchdog reset */ |
| 66 | | AT91_WDT_MR_WDDBGHLT /* disabled in debug mode */ |
| 67 | | AT91_WDT_MR_WDD(0xfff) /* restart at any time */ |
| 68 | | AT91_WDT_MR_WDV(ticks); /* timer value */ |
| 69 | writel(wdt->mr, wdt->regs + AT91_WDT_MR); |
| 70 | } else if (wdt->mode == AT91_WDT_MODE_SAM9X60) { |
| 71 | writel(AT91_SAM9X60_WLR_COUNTER(ticks), /* timer value */ |
| 72 | wdt->regs + AT91_SAM9X60_WLR); |
| 73 | |
| 74 | wdt->mr = AT91_SAM9X60_MR_PERIODRST /* causes watchdog reset */ |
| 75 | | AT91_SAM9X60_MR_WDDBGHLT; /* disabled in debug mode */ |
| 76 | writel(wdt->mr, wdt->regs + AT91_WDT_MR); |
| 77 | } |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static int at91_wdt_stop(struct udevice *dev) |
| 83 | { |
Zixun LI | 7c480ad | 2025-04-28 11:16:25 +0200 | [diff] [blame] | 84 | struct at91_wdt_priv *wdt = dev_get_priv(dev); |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 85 | |
| 86 | /* Disable Watchdog Timer */ |
Zixun LI | 991fdec | 2025-04-28 11:16:26 +0200 | [diff] [blame] | 87 | wdt->mr |= wdt->wddis; |
Zixun LI | 7c480ad | 2025-04-28 11:16:25 +0200 | [diff] [blame] | 88 | writel(wdt->mr, wdt->regs + AT91_WDT_MR); |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 93 | static int at91_wdt_reset(struct udevice *dev) |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 94 | { |
Zixun LI | 7c480ad | 2025-04-28 11:16:25 +0200 | [diff] [blame] | 95 | struct at91_wdt_priv *wdt = dev_get_priv(dev); |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 96 | |
Zixun LI | 7c480ad | 2025-04-28 11:16:25 +0200 | [diff] [blame] | 97 | writel(AT91_WDT_CR_WDRSTT | AT91_WDT_CR_KEY, wdt->regs + AT91_WDT_CR); |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 98 | |
| 99 | return 0; |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 102 | static const struct wdt_ops at91_wdt_ops = { |
| 103 | .start = at91_wdt_start, |
| 104 | .stop = at91_wdt_stop, |
| 105 | .reset = at91_wdt_reset, |
| 106 | }; |
| 107 | |
| 108 | static const struct udevice_id at91_wdt_ids[] = { |
Zixun LI | 991fdec | 2025-04-28 11:16:26 +0200 | [diff] [blame] | 109 | { .compatible = "atmel,at91sam9260-wdt", |
| 110 | .data = AT91_WDT_MODE_SAM9260 }, |
| 111 | { .compatible = "atmel,sama5d4-wdt", |
| 112 | .data = AT91_WDT_MODE_SAM9260 }, |
| 113 | { .compatible = "microchip,sam9x60-wdt", |
| 114 | .data = AT91_WDT_MODE_SAM9X60 }, |
| 115 | { .compatible = "microchip,sama7g5-wdt", |
| 116 | .data = AT91_WDT_MODE_SAM9X60 }, |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 117 | {} |
| 118 | }; |
| 119 | |
| 120 | static int at91_wdt_probe(struct udevice *dev) |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 121 | { |
Zixun LI | 7c480ad | 2025-04-28 11:16:25 +0200 | [diff] [blame] | 122 | struct at91_wdt_priv *wdt = dev_get_priv(dev); |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 123 | |
Zixun LI | 7c480ad | 2025-04-28 11:16:25 +0200 | [diff] [blame] | 124 | wdt->regs = dev_remap_addr(dev); |
| 125 | if (!wdt->regs) |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 126 | return -EINVAL; |
| 127 | |
Zixun LI | 991fdec | 2025-04-28 11:16:26 +0200 | [diff] [blame] | 128 | wdt->mode = dev_get_driver_data(dev); |
| 129 | if (wdt->mode == AT91_WDT_MODE_SAM9260) |
| 130 | wdt->wddis = AT91_WDT_MR_WDDIS; |
| 131 | else if (wdt->mode == AT91_WDT_MODE_SAM9X60) |
| 132 | wdt->wddis = AT91_SAM9X60_MR_WDDIS; |
| 133 | |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 134 | debug("%s: Probing wdt%u\n", __func__, dev_seq(dev)); |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 135 | |
| 136 | return 0; |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 137 | } |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 138 | |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 139 | U_BOOT_DRIVER(atmel_at91sam9260_wdt) = { |
| 140 | .name = "atmel_at91sam9260_wdt", |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 141 | .id = UCLASS_WDT, |
| 142 | .of_match = at91_wdt_ids, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 143 | .priv_auto = sizeof(struct at91_wdt_priv), |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 144 | .ops = &at91_wdt_ops, |
| 145 | .probe = at91_wdt_probe, |
| 146 | }; |