Suneel Garapati | 7072b17 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2019 Marvell International Ltd. |
| 4 | * |
| 5 | * https://spdx.org/licenses |
| 6 | */ |
| 7 | |
Suneel Garapati | baba248 | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 8 | #include <clk.h> |
Suneel Garapati | 7072b17 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <errno.h> |
| 11 | #include <wdt.h> |
| 12 | #include <asm/io.h> |
Suneel Garapati | baba248 | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 13 | #include <linux/bitfield.h> |
Suneel Garapati | 7072b17 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
Suneel Garapati | baba248 | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 17 | #define CORE0_WDOG_OFFSET 0x40000 |
Suneel Garapati | 7072b17 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 18 | #define CORE0_POKE_OFFSET 0x50000 |
| 19 | #define CORE0_POKE_OFFSET_MASK 0xfffffULL |
| 20 | |
Suneel Garapati | baba248 | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 21 | #define WDOG_MODE GENMASK_ULL(1, 0) |
| 22 | #define WDOG_LEN GENMASK_ULL(19, 4) |
| 23 | #define WDOG_CNT GENMASK_ULL(43, 20) |
| 24 | |
Suneel Garapati | 7072b17 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 25 | struct octeontx_wdt { |
| 26 | void __iomem *reg; |
Suneel Garapati | baba248 | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 27 | struct clk clk; |
Suneel Garapati | 7072b17 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 28 | }; |
| 29 | |
Suneel Garapati | baba248 | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 30 | static int octeontx_wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags) |
| 31 | { |
| 32 | struct octeontx_wdt *priv = dev_get_priv(dev); |
| 33 | u64 clk_rate, val; |
| 34 | u64 tout_wdog; |
| 35 | |
| 36 | clk_rate = clk_get_rate(&priv->clk); |
| 37 | if (IS_ERR_VALUE(clk_rate)) |
| 38 | return -EINVAL; |
| 39 | |
| 40 | /* Watchdog counts in 1024 cycle steps */ |
| 41 | tout_wdog = (clk_rate * timeout_ms / 1000) >> 10; |
| 42 | |
| 43 | /* |
| 44 | * We can only specify the upper 16 bits of a 24 bit value. |
| 45 | * Round up |
| 46 | */ |
| 47 | tout_wdog = (tout_wdog + 0xff) >> 8; |
| 48 | |
| 49 | /* If the timeout overflows the hardware limit, set max */ |
| 50 | if (tout_wdog >= 0x10000) |
| 51 | tout_wdog = 0xffff; |
| 52 | |
| 53 | val = FIELD_PREP(WDOG_MODE, 0x3) | |
| 54 | FIELD_PREP(WDOG_LEN, tout_wdog) | |
| 55 | FIELD_PREP(WDOG_CNT, tout_wdog << 8); |
| 56 | writeq(val, priv->reg + CORE0_WDOG_OFFSET); |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static int octeontx_wdt_stop(struct udevice *dev) |
| 62 | { |
| 63 | struct octeontx_wdt *priv = dev_get_priv(dev); |
| 64 | |
| 65 | writeq(0, priv->reg + CORE0_WDOG_OFFSET); |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int octeontx_wdt_expire_now(struct udevice *dev, ulong flags) |
| 71 | { |
| 72 | octeontx_wdt_stop(dev); |
| 73 | |
| 74 | /* Start with 100ms timeout to expire immediately */ |
| 75 | octeontx_wdt_start(dev, 100, flags); |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
Suneel Garapati | 7072b17 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 80 | static int octeontx_wdt_reset(struct udevice *dev) |
| 81 | { |
| 82 | struct octeontx_wdt *priv = dev_get_priv(dev); |
| 83 | |
Suneel Garapati | baba248 | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 84 | writeq(~0ULL, priv->reg + CORE0_POKE_OFFSET); |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static int octeontx_wdt_remove(struct udevice *dev) |
| 90 | { |
| 91 | octeontx_wdt_stop(dev); |
Suneel Garapati | 7072b17 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | static int octeontx_wdt_probe(struct udevice *dev) |
| 97 | { |
| 98 | struct octeontx_wdt *priv = dev_get_priv(dev); |
Suneel Garapati | baba248 | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 99 | int ret; |
Suneel Garapati | 7072b17 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 100 | |
| 101 | priv->reg = dev_remap_addr(dev); |
| 102 | if (!priv->reg) |
| 103 | return -EINVAL; |
| 104 | |
| 105 | /* |
Suneel Garapati | baba248 | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 106 | * Save base register address in reg masking lower 20 bits |
| 107 | * as 0xa0000 appears when extracted from the DT |
Suneel Garapati | 7072b17 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 108 | */ |
| 109 | priv->reg = (void __iomem *)(((u64)priv->reg & |
Suneel Garapati | baba248 | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 110 | ~CORE0_POKE_OFFSET_MASK)); |
| 111 | |
| 112 | ret = clk_get_by_index(dev, 0, &priv->clk); |
| 113 | if (ret < 0) |
| 114 | return ret; |
| 115 | |
| 116 | ret = clk_enable(&priv->clk); |
| 117 | if (ret) |
| 118 | return ret; |
Suneel Garapati | 7072b17 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static const struct wdt_ops octeontx_wdt_ops = { |
| 124 | .reset = octeontx_wdt_reset, |
Suneel Garapati | baba248 | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 125 | .start = octeontx_wdt_start, |
| 126 | .stop = octeontx_wdt_stop, |
| 127 | .expire_now = octeontx_wdt_expire_now, |
Suneel Garapati | 7072b17 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | static const struct udevice_id octeontx_wdt_ids[] = { |
| 131 | { .compatible = "arm,sbsa-gwdt" }, |
| 132 | {} |
| 133 | }; |
| 134 | |
| 135 | U_BOOT_DRIVER(wdt_octeontx) = { |
| 136 | .name = "wdt_octeontx", |
| 137 | .id = UCLASS_WDT, |
| 138 | .of_match = octeontx_wdt_ids, |
| 139 | .ops = &octeontx_wdt_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 140 | .priv_auto = sizeof(struct octeontx_wdt), |
Suneel Garapati | 7072b17 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 141 | .probe = octeontx_wdt_probe, |
Suneel Garapati | baba248 | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 142 | .remove = octeontx_wdt_remove, |
| 143 | .flags = DM_FLAG_OS_PREPARE, |
Suneel Garapati | 7072b17 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 144 | }; |