Prepare v2023.10
[platform/kernel/u-boot.git] / drivers / watchdog / ftwdt010_wdt.c
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 static int ftwdt010_wdt_reset(struct udevice *dev)
29 {
30         struct ftwdt010_wdt_priv *priv = dev_get_priv(dev);
31         struct ftwdt010_wdt *wd = priv->regs;
32
33         debug("Reset WDT..\n");
34
35         /* clear control register */
36         writel(0, &wd->wdcr);
37
38         /* Write Magic number */
39         writel(FTWDT010_WDRESTART_MAGIC, &wd->wdrestart);
40
41         /* Enable WDT */
42         writel(FTWDT010_WDCR_RST | FTWDT010_WDCR_ENABLE, &wd->wdcr);
43
44         return 0;
45 }
46
47 /*
48  * Set the watchdog time interval and start the timer.
49  * Counter is 32 bit.
50  */
51 static int ftwdt010_wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
52 {
53         struct ftwdt010_wdt_priv *priv = dev_get_priv(dev);
54         struct ftwdt010_wdt *wd = priv->regs;
55         unsigned int reg;
56
57         debug("Activating WDT %llu ms\n", timeout_ms);
58
59         /* Check if disabled */
60         if (readl(&wd->wdcr) & ~FTWDT010_WDCR_ENABLE) {
61                 printf("sorry, watchdog is disabled\n");
62                 return -1;
63         }
64
65         /*
66          * In a 66MHz system,
67          * if you set WDLOAD as 0x03EF1480 (66000000)
68          * the reset timer is 1 second.
69          */
70         reg = FTWDT010_WDLOAD(timeout_ms * FTWDT010_TIMEOUT_FACTOR);
71
72         writel(reg, &wd->wdload);
73
74         return ftwdt010_wdt_reset(dev);
75 }
76
77 static int ftwdt010_wdt_stop(struct udevice *dev)
78 {
79         struct ftwdt010_wdt_priv *priv = dev_get_priv(dev);
80         struct ftwdt010_wdt *wd = priv->regs;
81
82         debug("Deactivating WDT..\n");
83
84         /*
85          * It was defined with CONFIG_WATCHDOG_NOWAYOUT in Linux
86          *
87          * Shut off the timer.
88          * Lock it in if it's a module and we defined ...NOWAYOUT
89          */
90         writel(0, &wd->wdcr);
91         return 0;
92 }
93
94 static int ftwdt010_wdt_expire_now(struct udevice *dev, ulong flags)
95 {
96         struct ftwdt010_wdt_priv *priv = dev_get_priv(dev);
97         struct ftwdt010_wdt *wd = priv->regs;
98
99         debug("Expiring WDT..\n");
100         writel(FTWDT010_WDLOAD(0), &wd->wdload);
101         return ftwdt010_wdt_reset(dev);
102 }
103
104 static int ftwdt010_wdt_probe(struct udevice *dev)
105 {
106         struct ftwdt010_wdt_priv *priv = dev_get_priv(dev);
107
108         priv->regs = dev_read_addr_ptr(dev);
109         if (!priv->regs)
110                 return -EINVAL;
111
112         return 0;
113 }
114
115 static const struct wdt_ops ftwdt010_wdt_ops = {
116         .start = ftwdt010_wdt_start,
117         .reset = ftwdt010_wdt_reset,
118         .stop = ftwdt010_wdt_stop,
119         .expire_now = ftwdt010_wdt_expire_now,
120 };
121
122 static const struct udevice_id ftwdt010_wdt_ids[] = {
123         { .compatible = "faraday,ftwdt010" },
124         {}
125 };
126
127 U_BOOT_DRIVER(ftwdt010_wdt) = {
128         .name = "ftwdt010_wdt",
129         .id = UCLASS_WDT,
130         .of_match = ftwdt010_wdt_ids,
131         .ops = &ftwdt010_wdt_ops,
132         .probe = ftwdt010_wdt_probe,
133         .priv_auto = sizeof(struct ftwdt010_wdt_priv),
134 };