blob: e1bd73dfd4dad0db6043fdbd2cbd1233d3187bb1 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Álvaro Fernández Rojas77331932017-05-16 18:29:09 +02002/*
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>
Álvaro Fernández Rojas77331932017-05-16 18:29:09 +02008 */
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
27struct bcm6345_wdt_priv {
28 void __iomem *regs;
29};
30
31static int bcm6345_wdt_reset(struct udevice *dev)
32{
33 struct bcm6345_wdt_priv *priv = dev_get_priv(dev);
34
35 writel_be(WDT_CTL_START1_MASK, priv->regs + WDT_CTL_REG);
36 writel_be(WDT_CTL_START2_MASK, priv->regs + WDT_CTL_REG);
37
38 return 0;
39}
40
41static 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_be(timeout, priv->regs + WDT_VAL_REG);
54
55 return bcm6345_wdt_reset(dev);
56}
57
58static int bcm6345_wdt_expire_now(struct udevice *dev, ulong flags)
59{
60 return bcm6345_wdt_start(dev, WDT_VAL_MIN, flags);
61}
62
63static int bcm6345_wdt_stop(struct udevice *dev)
64{
65 struct bcm6345_wdt_priv *priv = dev_get_priv(dev);
66
67 writel_be(WDT_CTL_STOP1_MASK, priv->regs + WDT_CTL_REG);
68 writel_be(WDT_CTL_STOP2_MASK, priv->regs + WDT_CTL_REG);
69
70 return 0;
71}
72
73static 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
80static const struct udevice_id bcm6345_wdt_ids[] = {
81 { .compatible = "brcm,bcm6345-wdt" },
82 { /* sentinel */ }
83};
84
85static int bcm6345_wdt_probe(struct udevice *dev)
86{
87 struct bcm6345_wdt_priv *priv = dev_get_priv(dev);
Álvaro Fernández Rojas77331932017-05-16 18:29:09 +020088
Álvaro Fernández Rojasaa8f8252018-03-22 19:39:28 +010089 priv->regs = dev_remap_addr(dev);
90 if (!priv->regs)
Álvaro Fernández Rojas77331932017-05-16 18:29:09 +020091 return -EINVAL;
92
Álvaro Fernández Rojas77331932017-05-16 18:29:09 +020093 bcm6345_wdt_stop(dev);
94
95 return 0;
96}
97
98U_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};