Sergei Antonov | a0d9860 | 2023-04-12 14:01:58 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Watchdog driver for the FTWDT010 Watch Dog Driver |
| 4 | * |
| 5 | * (c) Copyright 2004 Faraday Technology Corp. (www.faraday-tech.com) |
| 6 | * Based on sa1100_wdt.c by Oleg Drokin <green@crimea.edu> |
| 7 | * Based on SoftDog driver by Alan Cox <alan@redhat.com> |
| 8 | * |
| 9 | * Copyright (C) 2011 Andes Technology Corporation |
| 10 | * Macpaul Lin, Andes Technology Corporation <macpaul@andestech.com> |
| 11 | * |
| 12 | * 27/11/2004 Initial release, Faraday. |
| 13 | * 12/01/2011 Port to u-boot, Macpaul Lin. |
| 14 | * 22/08/2022 Port to DM |
| 15 | */ |
| 16 | |
| 17 | #include <common.h> |
| 18 | #include <dm.h> |
| 19 | #include <wdt.h> |
| 20 | #include <log.h> |
| 21 | #include <asm/io.h> |
| 22 | #include <faraday/ftwdt010_wdt.h> |
| 23 | |
| 24 | struct ftwdt010_wdt_priv { |
| 25 | struct ftwdt010_wdt __iomem *regs; |
| 26 | }; |
| 27 | |
| 28 | /* |
| 29 | * Set the watchdog time interval. |
| 30 | * Counter is 32 bit. |
| 31 | */ |
| 32 | static int ftwdt010_wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags) |
| 33 | { |
| 34 | struct ftwdt010_wdt_priv *priv = dev_get_priv(dev); |
| 35 | struct ftwdt010_wdt *wd = priv->regs; |
| 36 | unsigned int reg; |
| 37 | |
| 38 | debug("Activating WDT %llu ms\n", timeout_ms); |
| 39 | |
| 40 | /* Check if disabled */ |
| 41 | if (readl(&wd->wdcr) & ~FTWDT010_WDCR_ENABLE) { |
| 42 | printf("sorry, watchdog is disabled\n"); |
| 43 | return -1; |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | * In a 66MHz system, |
| 48 | * if you set WDLOAD as 0x03EF1480 (66000000) |
| 49 | * the reset timer is 1 second. |
| 50 | */ |
| 51 | reg = FTWDT010_WDLOAD(timeout_ms * FTWDT010_TIMEOUT_FACTOR); |
| 52 | |
| 53 | writel(reg, &wd->wdload); |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static int ftwdt010_wdt_reset(struct udevice *dev) |
| 59 | { |
| 60 | struct ftwdt010_wdt_priv *priv = dev_get_priv(dev); |
| 61 | struct ftwdt010_wdt *wd = priv->regs; |
| 62 | |
| 63 | /* clear control register */ |
| 64 | writel(0, &wd->wdcr); |
| 65 | |
| 66 | /* Write Magic number */ |
| 67 | writel(FTWDT010_WDRESTART_MAGIC, &wd->wdrestart); |
| 68 | |
| 69 | /* Enable WDT */ |
| 70 | writel((FTWDT010_WDCR_RST | FTWDT010_WDCR_ENABLE), &wd->wdcr); |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int ftwdt010_wdt_stop(struct udevice *dev) |
| 76 | { |
| 77 | struct ftwdt010_wdt_priv *priv = dev_get_priv(dev); |
| 78 | struct ftwdt010_wdt *wd = priv->regs; |
| 79 | |
| 80 | debug("Deactivating WDT..\n"); |
| 81 | |
| 82 | /* |
| 83 | * It was defined with CONFIG_WATCHDOG_NOWAYOUT in Linux |
| 84 | * |
| 85 | * Shut off the timer. |
| 86 | * Lock it in if it's a module and we defined ...NOWAYOUT |
| 87 | */ |
| 88 | writel(0, &wd->wdcr); |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static int ftwdt010_wdt_expire_now(struct udevice *dev, ulong flags) |
| 93 | { |
| 94 | struct ftwdt010_wdt_priv *priv = dev_get_priv(dev); |
| 95 | struct ftwdt010_wdt *wd = priv->regs; |
| 96 | |
| 97 | debug("Expiring WDT..\n"); |
| 98 | writel(FTWDT010_WDLOAD(0), &wd->wdload); |
| 99 | return ftwdt010_wdt_reset(dev); |
| 100 | } |
| 101 | |
| 102 | static int ftwdt010_wdt_probe(struct udevice *dev) |
| 103 | { |
| 104 | struct ftwdt010_wdt_priv *priv = dev_get_priv(dev); |
| 105 | |
| 106 | priv->regs = dev_read_addr_ptr(dev); |
| 107 | if (!priv->regs) |
| 108 | return -EINVAL; |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static const struct wdt_ops ftwdt010_wdt_ops = { |
| 114 | .start = ftwdt010_wdt_start, |
| 115 | .reset = ftwdt010_wdt_reset, |
| 116 | .stop = ftwdt010_wdt_stop, |
| 117 | .expire_now = ftwdt010_wdt_expire_now, |
| 118 | }; |
| 119 | |
| 120 | static const struct udevice_id ftwdt010_wdt_ids[] = { |
| 121 | { .compatible = "faraday,ftwdt010" }, |
| 122 | {} |
| 123 | }; |
| 124 | |
| 125 | U_BOOT_DRIVER(ftwdt010_wdt) = { |
| 126 | .name = "ftwdt010_wdt", |
| 127 | .id = UCLASS_WDT, |
| 128 | .of_match = ftwdt010_wdt_ids, |
| 129 | .ops = &ftwdt010_wdt_ops, |
| 130 | .probe = ftwdt010_wdt_probe, |
| 131 | .priv_auto = sizeof(struct ftwdt010_wdt_priv), |
| 132 | }; |