David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* Copyright (c) 2014, The Linux Foundation. All rights reserved. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4 | #include <linux/bits.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5 | #include <linux/clk.h> |
| 6 | #include <linux/delay.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7 | #include <linux/interrupt.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8 | #include <linux/io.h> |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/of.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/watchdog.h> |
| 14 | #include <linux/of_device.h> |
| 15 | |
| 16 | enum wdt_reg { |
| 17 | WDT_RST, |
| 18 | WDT_EN, |
| 19 | WDT_STS, |
| 20 | WDT_BARK_TIME, |
| 21 | WDT_BITE_TIME, |
| 22 | }; |
| 23 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 24 | #define QCOM_WDT_ENABLE BIT(0) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 25 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 26 | static const u32 reg_offset_data_apcs_tmr[] = { |
| 27 | [WDT_RST] = 0x38, |
| 28 | [WDT_EN] = 0x40, |
| 29 | [WDT_STS] = 0x44, |
| 30 | [WDT_BARK_TIME] = 0x4C, |
| 31 | [WDT_BITE_TIME] = 0x5C, |
| 32 | }; |
| 33 | |
| 34 | static const u32 reg_offset_data_kpss[] = { |
| 35 | [WDT_RST] = 0x4, |
| 36 | [WDT_EN] = 0x8, |
| 37 | [WDT_STS] = 0xC, |
| 38 | [WDT_BARK_TIME] = 0x10, |
| 39 | [WDT_BITE_TIME] = 0x14, |
| 40 | }; |
| 41 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 42 | struct qcom_wdt_match_data { |
| 43 | const u32 *offset; |
| 44 | bool pretimeout; |
| 45 | }; |
| 46 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 47 | struct qcom_wdt { |
| 48 | struct watchdog_device wdd; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 49 | unsigned long rate; |
| 50 | void __iomem *base; |
| 51 | const u32 *layout; |
| 52 | }; |
| 53 | |
| 54 | static void __iomem *wdt_addr(struct qcom_wdt *wdt, enum wdt_reg reg) |
| 55 | { |
| 56 | return wdt->base + wdt->layout[reg]; |
| 57 | } |
| 58 | |
| 59 | static inline |
| 60 | struct qcom_wdt *to_qcom_wdt(struct watchdog_device *wdd) |
| 61 | { |
| 62 | return container_of(wdd, struct qcom_wdt, wdd); |
| 63 | } |
| 64 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 65 | static irqreturn_t qcom_wdt_isr(int irq, void *arg) |
| 66 | { |
| 67 | struct watchdog_device *wdd = arg; |
| 68 | |
| 69 | watchdog_notify_pretimeout(wdd); |
| 70 | |
| 71 | return IRQ_HANDLED; |
| 72 | } |
| 73 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 74 | static int qcom_wdt_start(struct watchdog_device *wdd) |
| 75 | { |
| 76 | struct qcom_wdt *wdt = to_qcom_wdt(wdd); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 77 | unsigned int bark = wdd->timeout - wdd->pretimeout; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 78 | |
| 79 | writel(0, wdt_addr(wdt, WDT_EN)); |
| 80 | writel(1, wdt_addr(wdt, WDT_RST)); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 81 | writel(bark * wdt->rate, wdt_addr(wdt, WDT_BARK_TIME)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 82 | writel(wdd->timeout * wdt->rate, wdt_addr(wdt, WDT_BITE_TIME)); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 83 | writel(QCOM_WDT_ENABLE, wdt_addr(wdt, WDT_EN)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static int qcom_wdt_stop(struct watchdog_device *wdd) |
| 88 | { |
| 89 | struct qcom_wdt *wdt = to_qcom_wdt(wdd); |
| 90 | |
| 91 | writel(0, wdt_addr(wdt, WDT_EN)); |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static int qcom_wdt_ping(struct watchdog_device *wdd) |
| 96 | { |
| 97 | struct qcom_wdt *wdt = to_qcom_wdt(wdd); |
| 98 | |
| 99 | writel(1, wdt_addr(wdt, WDT_RST)); |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static int qcom_wdt_set_timeout(struct watchdog_device *wdd, |
| 104 | unsigned int timeout) |
| 105 | { |
| 106 | wdd->timeout = timeout; |
| 107 | return qcom_wdt_start(wdd); |
| 108 | } |
| 109 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 110 | static int qcom_wdt_set_pretimeout(struct watchdog_device *wdd, |
| 111 | unsigned int timeout) |
| 112 | { |
| 113 | wdd->pretimeout = timeout; |
| 114 | return qcom_wdt_start(wdd); |
| 115 | } |
| 116 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 117 | static int qcom_wdt_restart(struct watchdog_device *wdd, unsigned long action, |
| 118 | void *data) |
| 119 | { |
| 120 | struct qcom_wdt *wdt = to_qcom_wdt(wdd); |
| 121 | u32 timeout; |
| 122 | |
| 123 | /* |
| 124 | * Trigger watchdog bite: |
| 125 | * Setup BITE_TIME to be 128ms, and enable WDT. |
| 126 | */ |
| 127 | timeout = 128 * wdt->rate / 1000; |
| 128 | |
| 129 | writel(0, wdt_addr(wdt, WDT_EN)); |
| 130 | writel(1, wdt_addr(wdt, WDT_RST)); |
| 131 | writel(timeout, wdt_addr(wdt, WDT_BARK_TIME)); |
| 132 | writel(timeout, wdt_addr(wdt, WDT_BITE_TIME)); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 133 | writel(QCOM_WDT_ENABLE, wdt_addr(wdt, WDT_EN)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 134 | |
| 135 | /* |
| 136 | * Actually make sure the above sequence hits hardware before sleeping. |
| 137 | */ |
| 138 | wmb(); |
| 139 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 140 | mdelay(150); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | static const struct watchdog_ops qcom_wdt_ops = { |
| 145 | .start = qcom_wdt_start, |
| 146 | .stop = qcom_wdt_stop, |
| 147 | .ping = qcom_wdt_ping, |
| 148 | .set_timeout = qcom_wdt_set_timeout, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 149 | .set_pretimeout = qcom_wdt_set_pretimeout, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 150 | .restart = qcom_wdt_restart, |
| 151 | .owner = THIS_MODULE, |
| 152 | }; |
| 153 | |
| 154 | static const struct watchdog_info qcom_wdt_info = { |
| 155 | .options = WDIOF_KEEPALIVEPING |
| 156 | | WDIOF_MAGICCLOSE |
| 157 | | WDIOF_SETTIMEOUT |
| 158 | | WDIOF_CARDRESET, |
| 159 | .identity = KBUILD_MODNAME, |
| 160 | }; |
| 161 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 162 | static const struct watchdog_info qcom_wdt_pt_info = { |
| 163 | .options = WDIOF_KEEPALIVEPING |
| 164 | | WDIOF_MAGICCLOSE |
| 165 | | WDIOF_SETTIMEOUT |
| 166 | | WDIOF_PRETIMEOUT |
| 167 | | WDIOF_CARDRESET, |
| 168 | .identity = KBUILD_MODNAME, |
| 169 | }; |
| 170 | |
| 171 | static void qcom_clk_disable_unprepare(void *data) |
| 172 | { |
| 173 | clk_disable_unprepare(data); |
| 174 | } |
| 175 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 176 | static const struct qcom_wdt_match_data match_data_apcs_tmr = { |
| 177 | .offset = reg_offset_data_apcs_tmr, |
| 178 | .pretimeout = false, |
| 179 | }; |
| 180 | |
| 181 | static const struct qcom_wdt_match_data match_data_kpss = { |
| 182 | .offset = reg_offset_data_kpss, |
| 183 | .pretimeout = true, |
| 184 | }; |
| 185 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 186 | static int qcom_wdt_probe(struct platform_device *pdev) |
| 187 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 188 | struct device *dev = &pdev->dev; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 189 | struct qcom_wdt *wdt; |
| 190 | struct resource *res; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 191 | struct device_node *np = dev->of_node; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 192 | const struct qcom_wdt_match_data *data; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 193 | u32 percpu_offset; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 194 | int irq, ret; |
| 195 | struct clk *clk; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 196 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 197 | data = of_device_get_match_data(dev); |
| 198 | if (!data) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 199 | dev_err(dev, "Unsupported QCOM WDT module\n"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 200 | return -ENODEV; |
| 201 | } |
| 202 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 203 | wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 204 | if (!wdt) |
| 205 | return -ENOMEM; |
| 206 | |
| 207 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 208 | if (!res) |
| 209 | return -ENOMEM; |
| 210 | |
| 211 | /* We use CPU0's DGT for the watchdog */ |
| 212 | if (of_property_read_u32(np, "cpu-offset", &percpu_offset)) |
| 213 | percpu_offset = 0; |
| 214 | |
| 215 | res->start += percpu_offset; |
| 216 | res->end += percpu_offset; |
| 217 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 218 | wdt->base = devm_ioremap_resource(dev, res); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 219 | if (IS_ERR(wdt->base)) |
| 220 | return PTR_ERR(wdt->base); |
| 221 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 222 | clk = devm_clk_get(dev, NULL); |
| 223 | if (IS_ERR(clk)) { |
| 224 | dev_err(dev, "failed to get input clock\n"); |
| 225 | return PTR_ERR(clk); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 226 | } |
| 227 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 228 | ret = clk_prepare_enable(clk); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 229 | if (ret) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 230 | dev_err(dev, "failed to setup clock\n"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 231 | return ret; |
| 232 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 233 | ret = devm_add_action_or_reset(dev, qcom_clk_disable_unprepare, clk); |
| 234 | if (ret) |
| 235 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 236 | |
| 237 | /* |
| 238 | * We use the clock rate to calculate the max timeout, so ensure it's |
| 239 | * not zero to avoid a divide-by-zero exception. |
| 240 | * |
| 241 | * WATCHDOG_CORE assumes units of seconds, if the WDT is clocked such |
| 242 | * that it would bite before a second elapses it's usefulness is |
| 243 | * limited. Bail if this is the case. |
| 244 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 245 | wdt->rate = clk_get_rate(clk); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 246 | if (wdt->rate == 0 || |
| 247 | wdt->rate > 0x10000000U) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 248 | dev_err(dev, "invalid clock rate\n"); |
| 249 | return -EINVAL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 250 | } |
| 251 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 252 | /* check if there is pretimeout support */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 253 | irq = platform_get_irq_optional(pdev, 0); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 254 | if (data->pretimeout && irq > 0) { |
| 255 | ret = devm_request_irq(dev, irq, qcom_wdt_isr, 0, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 256 | "wdt_bark", &wdt->wdd); |
| 257 | if (ret) |
| 258 | return ret; |
| 259 | |
| 260 | wdt->wdd.info = &qcom_wdt_pt_info; |
| 261 | wdt->wdd.pretimeout = 1; |
| 262 | } else { |
| 263 | if (irq == -EPROBE_DEFER) |
| 264 | return -EPROBE_DEFER; |
| 265 | |
| 266 | wdt->wdd.info = &qcom_wdt_info; |
| 267 | } |
| 268 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 269 | wdt->wdd.ops = &qcom_wdt_ops; |
| 270 | wdt->wdd.min_timeout = 1; |
| 271 | wdt->wdd.max_timeout = 0x10000000U / wdt->rate; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 272 | wdt->wdd.parent = dev; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 273 | wdt->layout = data->offset; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 274 | |
| 275 | if (readl(wdt_addr(wdt, WDT_STS)) & 1) |
| 276 | wdt->wdd.bootstatus = WDIOF_CARDRESET; |
| 277 | |
| 278 | /* |
| 279 | * If 'timeout-sec' unspecified in devicetree, assume a 30 second |
| 280 | * default, unless the max timeout is less than 30 seconds, then use |
| 281 | * the max instead. |
| 282 | */ |
| 283 | wdt->wdd.timeout = min(wdt->wdd.max_timeout, 30U); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 284 | watchdog_init_timeout(&wdt->wdd, 0, dev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 285 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 286 | ret = devm_watchdog_register_device(dev, &wdt->wdd); |
| 287 | if (ret) |
| 288 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 289 | |
| 290 | platform_set_drvdata(pdev, wdt); |
| 291 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 292 | } |
| 293 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 294 | static int __maybe_unused qcom_wdt_suspend(struct device *dev) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 295 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 296 | struct qcom_wdt *wdt = dev_get_drvdata(dev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 297 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 298 | if (watchdog_active(&wdt->wdd)) |
| 299 | qcom_wdt_stop(&wdt->wdd); |
| 300 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 301 | return 0; |
| 302 | } |
| 303 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 304 | static int __maybe_unused qcom_wdt_resume(struct device *dev) |
| 305 | { |
| 306 | struct qcom_wdt *wdt = dev_get_drvdata(dev); |
| 307 | |
| 308 | if (watchdog_active(&wdt->wdd)) |
| 309 | qcom_wdt_start(&wdt->wdd); |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | static SIMPLE_DEV_PM_OPS(qcom_wdt_pm_ops, qcom_wdt_suspend, qcom_wdt_resume); |
| 315 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 316 | static const struct of_device_id qcom_wdt_of_table[] = { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 317 | { .compatible = "qcom,kpss-timer", .data = &match_data_apcs_tmr }, |
| 318 | { .compatible = "qcom,scss-timer", .data = &match_data_apcs_tmr }, |
| 319 | { .compatible = "qcom,kpss-wdt", .data = &match_data_kpss }, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 320 | { }, |
| 321 | }; |
| 322 | MODULE_DEVICE_TABLE(of, qcom_wdt_of_table); |
| 323 | |
| 324 | static struct platform_driver qcom_watchdog_driver = { |
| 325 | .probe = qcom_wdt_probe, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 326 | .driver = { |
| 327 | .name = KBUILD_MODNAME, |
| 328 | .of_match_table = qcom_wdt_of_table, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 329 | .pm = &qcom_wdt_pm_ops, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 330 | }, |
| 331 | }; |
| 332 | module_platform_driver(qcom_watchdog_driver); |
| 333 | |
| 334 | MODULE_DESCRIPTION("QCOM KPSS Watchdog Driver"); |
| 335 | MODULE_LICENSE("GPL v2"); |