Suneel Garapati | af6ba90 | 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 | 3981cdd | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 8 | #include <clk.h> |
Suneel Garapati | af6ba90 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <errno.h> |
| 11 | #include <wdt.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Suneel Garapati | af6ba90 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 13 | #include <asm/io.h> |
Suneel Garapati | 3981cdd | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 14 | #include <linux/bitfield.h> |
Suneel Garapati | af6ba90 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Suneel Garapati | af6ba90 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 18 | #define CORE0_POKE_OFFSET_MASK 0xfffffULL |
| 19 | |
Suneel Garapati | 3981cdd | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 20 | #define WDOG_MODE GENMASK_ULL(1, 0) |
| 21 | #define WDOG_LEN GENMASK_ULL(19, 4) |
| 22 | #define WDOG_CNT GENMASK_ULL(43, 20) |
| 23 | |
Stefan Roese | 1aa2b85 | 2022-05-11 09:08:47 +0200 | [diff] [blame] | 24 | struct octeontx_wdt_data { |
| 25 | u32 wdog_offset; |
| 26 | u32 poke_offset; |
| 27 | int timer_shift; |
| 28 | bool has_clk; |
| 29 | }; |
| 30 | |
Suneel Garapati | af6ba90 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 31 | struct octeontx_wdt { |
| 32 | void __iomem *reg; |
Stefan Roese | 1aa2b85 | 2022-05-11 09:08:47 +0200 | [diff] [blame] | 33 | const struct octeontx_wdt_data *data; |
Suneel Garapati | 3981cdd | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 34 | struct clk clk; |
Suneel Garapati | af6ba90 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
Suneel Garapati | 3981cdd | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 37 | static int octeontx_wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags) |
| 38 | { |
| 39 | struct octeontx_wdt *priv = dev_get_priv(dev); |
| 40 | u64 clk_rate, val; |
| 41 | u64 tout_wdog; |
| 42 | |
Stefan Roese | 1aa2b85 | 2022-05-11 09:08:47 +0200 | [diff] [blame] | 43 | if (priv->data->has_clk) { |
| 44 | clk_rate = clk_get_rate(&priv->clk); |
| 45 | if (IS_ERR_VALUE(clk_rate)) |
| 46 | return -EINVAL; |
| 47 | } else { |
| 48 | clk_rate = gd->bus_clk; |
| 49 | } |
Suneel Garapati | 3981cdd | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 50 | |
Stefan Roese | 1aa2b85 | 2022-05-11 09:08:47 +0200 | [diff] [blame] | 51 | /* Watchdog counts in configured cycle steps */ |
| 52 | tout_wdog = (clk_rate * timeout_ms / 1000) >> priv->data->timer_shift; |
Suneel Garapati | 3981cdd | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 53 | |
| 54 | /* |
| 55 | * We can only specify the upper 16 bits of a 24 bit value. |
| 56 | * Round up |
| 57 | */ |
| 58 | tout_wdog = (tout_wdog + 0xff) >> 8; |
| 59 | |
| 60 | /* If the timeout overflows the hardware limit, set max */ |
| 61 | if (tout_wdog >= 0x10000) |
| 62 | tout_wdog = 0xffff; |
| 63 | |
| 64 | val = FIELD_PREP(WDOG_MODE, 0x3) | |
| 65 | FIELD_PREP(WDOG_LEN, tout_wdog) | |
| 66 | FIELD_PREP(WDOG_CNT, tout_wdog << 8); |
Stefan Roese | 1aa2b85 | 2022-05-11 09:08:47 +0200 | [diff] [blame] | 67 | writeq(val, priv->reg + priv->data->wdog_offset); |
Suneel Garapati | 3981cdd | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | static int octeontx_wdt_stop(struct udevice *dev) |
| 73 | { |
| 74 | struct octeontx_wdt *priv = dev_get_priv(dev); |
| 75 | |
Stefan Roese | 1aa2b85 | 2022-05-11 09:08:47 +0200 | [diff] [blame] | 76 | writeq(0, priv->reg + priv->data->wdog_offset); |
Suneel Garapati | 3981cdd | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static int octeontx_wdt_expire_now(struct udevice *dev, ulong flags) |
| 82 | { |
| 83 | octeontx_wdt_stop(dev); |
| 84 | |
| 85 | /* Start with 100ms timeout to expire immediately */ |
| 86 | octeontx_wdt_start(dev, 100, flags); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
Suneel Garapati | af6ba90 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 91 | static int octeontx_wdt_reset(struct udevice *dev) |
| 92 | { |
| 93 | struct octeontx_wdt *priv = dev_get_priv(dev); |
| 94 | |
Stefan Roese | 1aa2b85 | 2022-05-11 09:08:47 +0200 | [diff] [blame] | 95 | writeq(~0ULL, priv->reg + priv->data->poke_offset); |
Suneel Garapati | 3981cdd | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static int octeontx_wdt_remove(struct udevice *dev) |
| 101 | { |
| 102 | octeontx_wdt_stop(dev); |
Suneel Garapati | af6ba90 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static int octeontx_wdt_probe(struct udevice *dev) |
| 108 | { |
| 109 | struct octeontx_wdt *priv = dev_get_priv(dev); |
Suneel Garapati | 3981cdd | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 110 | int ret; |
Suneel Garapati | af6ba90 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 111 | |
| 112 | priv->reg = dev_remap_addr(dev); |
| 113 | if (!priv->reg) |
| 114 | return -EINVAL; |
| 115 | |
Stefan Roese | 1aa2b85 | 2022-05-11 09:08:47 +0200 | [diff] [blame] | 116 | priv->data = (void *)dev_get_driver_data(dev); |
| 117 | if (!priv->data) |
| 118 | return -EINVAL; |
| 119 | |
Suneel Garapati | af6ba90 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 120 | /* |
Suneel Garapati | 3981cdd | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 121 | * Save base register address in reg masking lower 20 bits |
| 122 | * as 0xa0000 appears when extracted from the DT |
Suneel Garapati | af6ba90 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 123 | */ |
| 124 | priv->reg = (void __iomem *)(((u64)priv->reg & |
Suneel Garapati | 3981cdd | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 125 | ~CORE0_POKE_OFFSET_MASK)); |
| 126 | |
Stefan Roese | 1aa2b85 | 2022-05-11 09:08:47 +0200 | [diff] [blame] | 127 | if (priv->data->has_clk) { |
| 128 | ret = clk_get_by_index(dev, 0, &priv->clk); |
| 129 | if (ret < 0) |
| 130 | return ret; |
Suneel Garapati | 3981cdd | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 131 | |
Stefan Roese | 1aa2b85 | 2022-05-11 09:08:47 +0200 | [diff] [blame] | 132 | ret = clk_enable(&priv->clk); |
| 133 | if (ret) |
| 134 | return ret; |
| 135 | } |
Suneel Garapati | af6ba90 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static const struct wdt_ops octeontx_wdt_ops = { |
| 141 | .reset = octeontx_wdt_reset, |
Suneel Garapati | 3981cdd | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 142 | .start = octeontx_wdt_start, |
| 143 | .stop = octeontx_wdt_stop, |
| 144 | .expire_now = octeontx_wdt_expire_now, |
Suneel Garapati | af6ba90 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
Stefan Roese | 1aa2b85 | 2022-05-11 09:08:47 +0200 | [diff] [blame] | 147 | static const struct octeontx_wdt_data octeontx_data = { |
| 148 | .wdog_offset = 0x40000, |
| 149 | .poke_offset = 0x50000, |
| 150 | .timer_shift = 10, |
| 151 | .has_clk = true, |
| 152 | }; |
| 153 | |
| 154 | static const struct octeontx_wdt_data octeon_data = { |
| 155 | .wdog_offset = 0x20000, |
| 156 | .poke_offset = 0x30000, |
| 157 | .timer_shift = 10, |
| 158 | .has_clk = false, |
| 159 | }; |
| 160 | |
Suneel Garapati | af6ba90 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 161 | static const struct udevice_id octeontx_wdt_ids[] = { |
Stefan Roese | 1aa2b85 | 2022-05-11 09:08:47 +0200 | [diff] [blame] | 162 | { .compatible = "arm,sbsa-gwdt", .data = (ulong)&octeontx_data }, |
| 163 | { .compatible = "cavium,octeon-7890-ciu3", .data = (ulong)&octeon_data }, |
Suneel Garapati | af6ba90 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 164 | {} |
| 165 | }; |
| 166 | |
| 167 | U_BOOT_DRIVER(wdt_octeontx) = { |
| 168 | .name = "wdt_octeontx", |
| 169 | .id = UCLASS_WDT, |
| 170 | .of_match = octeontx_wdt_ids, |
| 171 | .ops = &octeontx_wdt_ops, |
Stefan Roese | 1aa2b85 | 2022-05-11 09:08:47 +0200 | [diff] [blame] | 172 | .priv_auto = sizeof(struct octeontx_wdt), |
Suneel Garapati | af6ba90 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 173 | .probe = octeontx_wdt_probe, |
Suneel Garapati | 3981cdd | 2020-09-23 11:01:31 +0200 | [diff] [blame] | 174 | .remove = octeontx_wdt_remove, |
| 175 | .flags = DM_FLAG_OS_PREPARE, |
Suneel Garapati | af6ba90 | 2019-10-21 16:09:36 -0700 | [diff] [blame] | 176 | }; |