blob: ee77f49a9445011736f0d7be1ef568e4b6c9b1d0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Álvaro Fernández Rojasd8759a52017-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 Rojasd8759a52017-05-16 18:29:09 +02008 */
9
10#include <common.h>
11#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Álvaro Fernández Rojasd8759a52017-05-16 18:29:09 +020013#include <wdt.h>
Philippe Reynes53a81ad2019-05-03 19:43:06 +020014#include <clk.h>
Álvaro Fernández Rojasd8759a52017-05-16 18:29:09 +020015#include <asm/io.h>
16
17/* WDT Value register */
18#define WDT_VAL_REG 0x0
19#define WDT_VAL_MIN 0x00000002
20#define WDT_VAL_MAX 0xfffffffe
21
22/* WDT Control register */
23#define WDT_CTL_REG 0x4
24#define WDT_CTL_START1_MASK 0x0000ff00
25#define WDT_CTL_START2_MASK 0x000000ff
26#define WDT_CTL_STOP1_MASK 0x0000ee00
27#define WDT_CTL_STOP2_MASK 0x000000ee
28
29struct bcm6345_wdt_priv {
30 void __iomem *regs;
Philippe Reynes53a81ad2019-05-03 19:43:06 +020031 unsigned long clk_rate;
Álvaro Fernández Rojasd8759a52017-05-16 18:29:09 +020032};
33
34static int bcm6345_wdt_reset(struct udevice *dev)
35{
36 struct bcm6345_wdt_priv *priv = dev_get_priv(dev);
37
Philippe Reynesd1667252019-01-28 15:37:27 +010038 writel(WDT_CTL_START1_MASK, priv->regs + WDT_CTL_REG);
39 writel(WDT_CTL_START2_MASK, priv->regs + WDT_CTL_REG);
Álvaro Fernández Rojasd8759a52017-05-16 18:29:09 +020040
41 return 0;
42}
43
44static int bcm6345_wdt_start(struct udevice *dev, u64 timeout, ulong flags)
45{
46 struct bcm6345_wdt_priv *priv = dev_get_priv(dev);
Philippe Reynes53a81ad2019-05-03 19:43:06 +020047 u32 val = priv->clk_rate / 1000 * timeout;
Álvaro Fernández Rojasd8759a52017-05-16 18:29:09 +020048
Philippe Reynes53a81ad2019-05-03 19:43:06 +020049 if (val < WDT_VAL_MIN) {
Álvaro Fernández Rojasd8759a52017-05-16 18:29:09 +020050 debug("watchdog won't fire with less than 2 ticks\n");
Philippe Reynes53a81ad2019-05-03 19:43:06 +020051 val = WDT_VAL_MIN;
52 } else if (val > WDT_VAL_MAX) {
Álvaro Fernández Rojasd8759a52017-05-16 18:29:09 +020053 debug("maximum watchdog timeout exceeded\n");
Philippe Reynes53a81ad2019-05-03 19:43:06 +020054 val = WDT_VAL_MAX;
Álvaro Fernández Rojasd8759a52017-05-16 18:29:09 +020055 }
56
Philippe Reynes53a81ad2019-05-03 19:43:06 +020057 writel(val, priv->regs + WDT_VAL_REG);
Álvaro Fernández Rojasd8759a52017-05-16 18:29:09 +020058
59 return bcm6345_wdt_reset(dev);
60}
61
62static int bcm6345_wdt_expire_now(struct udevice *dev, ulong flags)
63{
64 return bcm6345_wdt_start(dev, WDT_VAL_MIN, flags);
65}
66
67static int bcm6345_wdt_stop(struct udevice *dev)
68{
69 struct bcm6345_wdt_priv *priv = dev_get_priv(dev);
70
Philippe Reynesd1667252019-01-28 15:37:27 +010071 writel(WDT_CTL_STOP1_MASK, priv->regs + WDT_CTL_REG);
72 writel(WDT_CTL_STOP2_MASK, priv->regs + WDT_CTL_REG);
Álvaro Fernández Rojasd8759a52017-05-16 18:29:09 +020073
74 return 0;
75}
76
77static const struct wdt_ops bcm6345_wdt_ops = {
78 .expire_now = bcm6345_wdt_expire_now,
79 .reset = bcm6345_wdt_reset,
80 .start = bcm6345_wdt_start,
81 .stop = bcm6345_wdt_stop,
82};
83
84static const struct udevice_id bcm6345_wdt_ids[] = {
85 { .compatible = "brcm,bcm6345-wdt" },
86 { /* sentinel */ }
87};
88
89static int bcm6345_wdt_probe(struct udevice *dev)
90{
91 struct bcm6345_wdt_priv *priv = dev_get_priv(dev);
Philippe Reynes53a81ad2019-05-03 19:43:06 +020092 struct clk clk;
93 int ret;
Álvaro Fernández Rojasd8759a52017-05-16 18:29:09 +020094
Álvaro Fernández Rojasa1a3a4c2018-03-22 19:39:28 +010095 priv->regs = dev_remap_addr(dev);
96 if (!priv->regs)
Álvaro Fernández Rojasd8759a52017-05-16 18:29:09 +020097 return -EINVAL;
98
Philippe Reynes53a81ad2019-05-03 19:43:06 +020099 ret = clk_get_by_index(dev, 0, &clk);
100 if (!ret)
101 priv->clk_rate = clk_get_rate(&clk);
102 else
103 return -EINVAL;
104
Álvaro Fernández Rojasd8759a52017-05-16 18:29:09 +0200105 bcm6345_wdt_stop(dev);
106
107 return 0;
108}
109
110U_BOOT_DRIVER(wdt_bcm6345) = {
111 .name = "wdt_bcm6345",
112 .id = UCLASS_WDT,
113 .of_match = bcm6345_wdt_ids,
114 .ops = &bcm6345_wdt_ops,
115 .priv_auto_alloc_size = sizeof(struct bcm6345_wdt_priv),
116 .probe = bcm6345_wdt_probe,
117};