Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Álvaro Fernández Rojas | d8759a5 | 2017-05-16 18:29:09 +0200 | [diff] [blame] | 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> |
Álvaro Fernández Rojas | d8759a5 | 2017-05-16 18:29:09 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Álvaro Fernández Rojas | d8759a5 | 2017-05-16 18:29:09 +0200 | [diff] [blame] | 13 | #include <wdt.h> |
Philippe Reynes | 53a81ad | 2019-05-03 19:43:06 +0200 | [diff] [blame] | 14 | #include <clk.h> |
Álvaro Fernández Rojas | d8759a5 | 2017-05-16 18:29:09 +0200 | [diff] [blame] | 15 | #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 | |
| 29 | struct bcm6345_wdt_priv { |
| 30 | void __iomem *regs; |
Philippe Reynes | 53a81ad | 2019-05-03 19:43:06 +0200 | [diff] [blame] | 31 | unsigned long clk_rate; |
Álvaro Fernández Rojas | d8759a5 | 2017-05-16 18:29:09 +0200 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | static int bcm6345_wdt_reset(struct udevice *dev) |
| 35 | { |
| 36 | struct bcm6345_wdt_priv *priv = dev_get_priv(dev); |
| 37 | |
Philippe Reynes | d166725 | 2019-01-28 15:37:27 +0100 | [diff] [blame] | 38 | writel(WDT_CTL_START1_MASK, priv->regs + WDT_CTL_REG); |
| 39 | writel(WDT_CTL_START2_MASK, priv->regs + WDT_CTL_REG); |
Álvaro Fernández Rojas | d8759a5 | 2017-05-16 18:29:09 +0200 | [diff] [blame] | 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | static int bcm6345_wdt_start(struct udevice *dev, u64 timeout, ulong flags) |
| 45 | { |
| 46 | struct bcm6345_wdt_priv *priv = dev_get_priv(dev); |
Philippe Reynes | 53a81ad | 2019-05-03 19:43:06 +0200 | [diff] [blame] | 47 | u32 val = priv->clk_rate / 1000 * timeout; |
Álvaro Fernández Rojas | d8759a5 | 2017-05-16 18:29:09 +0200 | [diff] [blame] | 48 | |
Philippe Reynes | 53a81ad | 2019-05-03 19:43:06 +0200 | [diff] [blame] | 49 | if (val < WDT_VAL_MIN) { |
Álvaro Fernández Rojas | d8759a5 | 2017-05-16 18:29:09 +0200 | [diff] [blame] | 50 | debug("watchdog won't fire with less than 2 ticks\n"); |
Philippe Reynes | 53a81ad | 2019-05-03 19:43:06 +0200 | [diff] [blame] | 51 | val = WDT_VAL_MIN; |
| 52 | } else if (val > WDT_VAL_MAX) { |
Álvaro Fernández Rojas | d8759a5 | 2017-05-16 18:29:09 +0200 | [diff] [blame] | 53 | debug("maximum watchdog timeout exceeded\n"); |
Philippe Reynes | 53a81ad | 2019-05-03 19:43:06 +0200 | [diff] [blame] | 54 | val = WDT_VAL_MAX; |
Álvaro Fernández Rojas | d8759a5 | 2017-05-16 18:29:09 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Philippe Reynes | 53a81ad | 2019-05-03 19:43:06 +0200 | [diff] [blame] | 57 | writel(val, priv->regs + WDT_VAL_REG); |
Álvaro Fernández Rojas | d8759a5 | 2017-05-16 18:29:09 +0200 | [diff] [blame] | 58 | |
| 59 | return bcm6345_wdt_reset(dev); |
| 60 | } |
| 61 | |
| 62 | static int bcm6345_wdt_expire_now(struct udevice *dev, ulong flags) |
| 63 | { |
| 64 | return bcm6345_wdt_start(dev, WDT_VAL_MIN, flags); |
| 65 | } |
| 66 | |
| 67 | static int bcm6345_wdt_stop(struct udevice *dev) |
| 68 | { |
| 69 | struct bcm6345_wdt_priv *priv = dev_get_priv(dev); |
| 70 | |
Philippe Reynes | d166725 | 2019-01-28 15:37:27 +0100 | [diff] [blame] | 71 | writel(WDT_CTL_STOP1_MASK, priv->regs + WDT_CTL_REG); |
| 72 | writel(WDT_CTL_STOP2_MASK, priv->regs + WDT_CTL_REG); |
Álvaro Fernández Rojas | d8759a5 | 2017-05-16 18:29:09 +0200 | [diff] [blame] | 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static 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 | |
| 84 | static const struct udevice_id bcm6345_wdt_ids[] = { |
| 85 | { .compatible = "brcm,bcm6345-wdt" }, |
| 86 | { /* sentinel */ } |
| 87 | }; |
| 88 | |
| 89 | static int bcm6345_wdt_probe(struct udevice *dev) |
| 90 | { |
| 91 | struct bcm6345_wdt_priv *priv = dev_get_priv(dev); |
Philippe Reynes | 53a81ad | 2019-05-03 19:43:06 +0200 | [diff] [blame] | 92 | struct clk clk; |
| 93 | int ret; |
Álvaro Fernández Rojas | d8759a5 | 2017-05-16 18:29:09 +0200 | [diff] [blame] | 94 | |
Álvaro Fernández Rojas | a1a3a4c | 2018-03-22 19:39:28 +0100 | [diff] [blame] | 95 | priv->regs = dev_remap_addr(dev); |
| 96 | if (!priv->regs) |
Álvaro Fernández Rojas | d8759a5 | 2017-05-16 18:29:09 +0200 | [diff] [blame] | 97 | return -EINVAL; |
| 98 | |
Philippe Reynes | 53a81ad | 2019-05-03 19:43:06 +0200 | [diff] [blame] | 99 | 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 Rojas | d8759a5 | 2017-05-16 18:29:09 +0200 | [diff] [blame] | 105 | bcm6345_wdt_stop(dev); |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | U_BOOT_DRIVER(wdt_bcm6345) = { |
| 111 | .name = "wdt_bcm6345", |
| 112 | .id = UCLASS_WDT, |
| 113 | .of_match = bcm6345_wdt_ids, |
| 114 | .ops = &bcm6345_wdt_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 115 | .priv_auto = sizeof(struct bcm6345_wdt_priv), |
Álvaro Fernández Rojas | d8759a5 | 2017-05-16 18:29:09 +0200 | [diff] [blame] | 116 | .probe = bcm6345_wdt_probe, |
| 117 | }; |