1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com>
4  *
5  * Derived from linux/drivers/watchdog/bcm63xx_wdt.c:
6  *	Copyright (C) 2007 Miguel Gaio <miguel.gaio@efixo.com>
7  *	Copyright (C) 2008 Florian Fainelli <florian@openwrt.org>
8  */
9 
10 #include <common.h>
11 #include <dm.h>
12 #include <wdt.h>
13 #include <asm/io.h>
14 
15 /* WDT Value register */
16 #define WDT_VAL_REG		0x0
17 #define WDT_VAL_MIN		0x00000002
18 #define WDT_VAL_MAX		0xfffffffe
19 
20 /* WDT Control register */
21 #define WDT_CTL_REG		0x4
22 #define WDT_CTL_START1_MASK	0x0000ff00
23 #define WDT_CTL_START2_MASK	0x000000ff
24 #define WDT_CTL_STOP1_MASK	0x0000ee00
25 #define WDT_CTL_STOP2_MASK	0x000000ee
26 
27 struct bcm6345_wdt_priv {
28 	void __iomem *regs;
29 };
30 
bcm6345_wdt_reset(struct udevice * dev)31 static int bcm6345_wdt_reset(struct udevice *dev)
32 {
33 	struct bcm6345_wdt_priv *priv = dev_get_priv(dev);
34 
35 	writel(WDT_CTL_START1_MASK, priv->regs + WDT_CTL_REG);
36 	writel(WDT_CTL_START2_MASK, priv->regs + WDT_CTL_REG);
37 
38 	return 0;
39 }
40 
bcm6345_wdt_start(struct udevice * dev,u64 timeout,ulong flags)41 static int bcm6345_wdt_start(struct udevice *dev, u64 timeout, ulong flags)
42 {
43 	struct bcm6345_wdt_priv *priv = dev_get_priv(dev);
44 
45 	if (timeout < WDT_VAL_MIN) {
46 		debug("watchdog won't fire with less than 2 ticks\n");
47 		timeout = WDT_VAL_MIN;
48 	} else if (timeout > WDT_VAL_MAX) {
49 		debug("maximum watchdog timeout exceeded\n");
50 		timeout = WDT_VAL_MAX;
51 	}
52 
53 	writel(timeout, priv->regs + WDT_VAL_REG);
54 
55 	return bcm6345_wdt_reset(dev);
56 }
57 
bcm6345_wdt_expire_now(struct udevice * dev,ulong flags)58 static int bcm6345_wdt_expire_now(struct udevice *dev, ulong flags)
59 {
60 	return bcm6345_wdt_start(dev, WDT_VAL_MIN, flags);
61 }
62 
bcm6345_wdt_stop(struct udevice * dev)63 static int bcm6345_wdt_stop(struct udevice *dev)
64 {
65 	struct bcm6345_wdt_priv *priv = dev_get_priv(dev);
66 
67 	writel(WDT_CTL_STOP1_MASK, priv->regs + WDT_CTL_REG);
68 	writel(WDT_CTL_STOP2_MASK, priv->regs + WDT_CTL_REG);
69 
70 	return 0;
71 }
72 
73 static const struct wdt_ops bcm6345_wdt_ops = {
74 	.expire_now = bcm6345_wdt_expire_now,
75 	.reset = bcm6345_wdt_reset,
76 	.start = bcm6345_wdt_start,
77 	.stop = bcm6345_wdt_stop,
78 };
79 
80 static const struct udevice_id bcm6345_wdt_ids[] = {
81 	{ .compatible = "brcm,bcm6345-wdt" },
82 	{ /* sentinel */ }
83 };
84 
bcm6345_wdt_probe(struct udevice * dev)85 static int bcm6345_wdt_probe(struct udevice *dev)
86 {
87 	struct bcm6345_wdt_priv *priv = dev_get_priv(dev);
88 
89 	priv->regs = dev_remap_addr(dev);
90 	if (!priv->regs)
91 		return -EINVAL;
92 
93 	bcm6345_wdt_stop(dev);
94 
95 	return 0;
96 }
97 
98 U_BOOT_DRIVER(wdt_bcm6345) = {
99 	.name = "wdt_bcm6345",
100 	.id = UCLASS_WDT,
101 	.of_match = bcm6345_wdt_ids,
102 	.ops = &bcm6345_wdt_ops,
103 	.priv_auto_alloc_size = sizeof(struct bcm6345_wdt_priv),
104 	.probe = bcm6345_wdt_probe,
105 };
106