Thomas Gleixner | 25763b3 | 2019-05-28 10:10:09 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 2 | /* |
| 3 | * intel-mid_wdt: generic Intel MID SCU watchdog driver |
| 4 | * |
| 5 | * Platforms supported so far: |
| 6 | * - Merrifield only |
| 7 | * |
| 8 | * Copyright (C) 2014 Intel Corporation. All rights reserved. |
| 9 | * Contact: David Cohen <david.a.cohen@linux.intel.com> |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/interrupt.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/nmi.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/watchdog.h> |
| 17 | #include <linux/platform_data/intel-mid_wdt.h> |
| 18 | |
| 19 | #include <asm/intel_scu_ipc.h> |
| 20 | #include <asm/intel-mid.h> |
| 21 | |
| 22 | #define IPC_WATCHDOG 0xf8 |
| 23 | |
| 24 | #define MID_WDT_PRETIMEOUT 15 |
| 25 | #define MID_WDT_TIMEOUT_MIN (1 + MID_WDT_PRETIMEOUT) |
| 26 | #define MID_WDT_TIMEOUT_MAX 170 |
| 27 | #define MID_WDT_DEFAULT_TIMEOUT 90 |
| 28 | |
| 29 | /* SCU watchdog messages */ |
| 30 | enum { |
| 31 | SCU_WATCHDOG_START = 0, |
| 32 | SCU_WATCHDOG_STOP, |
| 33 | SCU_WATCHDOG_KEEPALIVE, |
| 34 | }; |
| 35 | |
Mika Westerberg | 80ae679 | 2020-04-16 11:15:38 +0300 | [diff] [blame] | 36 | struct mid_wdt { |
| 37 | struct watchdog_device wd; |
| 38 | struct device *dev; |
| 39 | struct intel_scu_ipc_dev *scu; |
| 40 | }; |
| 41 | |
| 42 | static inline int |
| 43 | wdt_command(struct mid_wdt *mid, int sub, const void *in, size_t inlen, size_t size) |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 44 | { |
Mika Westerberg | 80ae679 | 2020-04-16 11:15:38 +0300 | [diff] [blame] | 45 | struct intel_scu_ipc_dev *scu = mid->scu; |
| 46 | |
| 47 | return intel_scu_ipc_dev_command_with_size(scu, IPC_WATCHDOG, sub, in, |
| 48 | inlen, size, NULL, 0); |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static int wdt_start(struct watchdog_device *wd) |
| 52 | { |
Mika Westerberg | 80ae679 | 2020-04-16 11:15:38 +0300 | [diff] [blame] | 53 | struct mid_wdt *mid = watchdog_get_drvdata(wd); |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 54 | int ret, in_size; |
| 55 | int timeout = wd->timeout; |
| 56 | struct ipc_wd_start { |
| 57 | u32 pretimeout; |
| 58 | u32 timeout; |
| 59 | } ipc_wd_start = { timeout - MID_WDT_PRETIMEOUT, timeout }; |
| 60 | |
| 61 | /* |
Mika Westerberg | 80ae679 | 2020-04-16 11:15:38 +0300 | [diff] [blame] | 62 | * SCU expects the input size for watchdog IPC to be 2 which is the |
| 63 | * size of the structure in dwords. SCU IPC normally takes bytes |
| 64 | * but this is a special case where we specify size to be different |
| 65 | * than inlen. |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 66 | */ |
| 67 | in_size = DIV_ROUND_UP(sizeof(ipc_wd_start), 4); |
| 68 | |
Mika Westerberg | 80ae679 | 2020-04-16 11:15:38 +0300 | [diff] [blame] | 69 | ret = wdt_command(mid, SCU_WATCHDOG_START, &ipc_wd_start, |
| 70 | sizeof(ipc_wd_start), in_size); |
Andy Shevchenko | bb79036 | 2016-11-18 17:24:41 +0200 | [diff] [blame] | 71 | if (ret) |
Mika Westerberg | 80ae679 | 2020-04-16 11:15:38 +0300 | [diff] [blame] | 72 | dev_crit(mid->dev, "error starting watchdog: %d\n", ret); |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 73 | |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | static int wdt_ping(struct watchdog_device *wd) |
| 78 | { |
Mika Westerberg | 80ae679 | 2020-04-16 11:15:38 +0300 | [diff] [blame] | 79 | struct mid_wdt *mid = watchdog_get_drvdata(wd); |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 80 | int ret; |
| 81 | |
Mika Westerberg | 80ae679 | 2020-04-16 11:15:38 +0300 | [diff] [blame] | 82 | ret = wdt_command(mid, SCU_WATCHDOG_KEEPALIVE, NULL, 0, 0); |
Andy Shevchenko | bb79036 | 2016-11-18 17:24:41 +0200 | [diff] [blame] | 83 | if (ret) |
Mika Westerberg | 80ae679 | 2020-04-16 11:15:38 +0300 | [diff] [blame] | 84 | dev_crit(mid->dev, "Error executing keepalive: %d\n", ret); |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 85 | |
| 86 | return ret; |
| 87 | } |
| 88 | |
| 89 | static int wdt_stop(struct watchdog_device *wd) |
| 90 | { |
Mika Westerberg | 80ae679 | 2020-04-16 11:15:38 +0300 | [diff] [blame] | 91 | struct mid_wdt *mid = watchdog_get_drvdata(wd); |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 92 | int ret; |
| 93 | |
Mika Westerberg | 80ae679 | 2020-04-16 11:15:38 +0300 | [diff] [blame] | 94 | ret = wdt_command(mid, SCU_WATCHDOG_STOP, NULL, 0, 0); |
Andy Shevchenko | bb79036 | 2016-11-18 17:24:41 +0200 | [diff] [blame] | 95 | if (ret) |
Mika Westerberg | 80ae679 | 2020-04-16 11:15:38 +0300 | [diff] [blame] | 96 | dev_crit(mid->dev, "Error stopping watchdog: %d\n", ret); |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 97 | |
| 98 | return ret; |
| 99 | } |
| 100 | |
| 101 | static irqreturn_t mid_wdt_irq(int irq, void *dev_id) |
| 102 | { |
| 103 | panic("Kernel Watchdog"); |
| 104 | |
| 105 | /* This code should not be reached */ |
| 106 | return IRQ_HANDLED; |
| 107 | } |
| 108 | |
| 109 | static const struct watchdog_info mid_wdt_info = { |
| 110 | .identity = "Intel MID SCU watchdog", |
David Cohen | 8cbb97e | 2015-10-05 16:49:58 -0700 | [diff] [blame] | 111 | .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE, |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | static const struct watchdog_ops mid_wdt_ops = { |
| 115 | .owner = THIS_MODULE, |
| 116 | .start = wdt_start, |
| 117 | .stop = wdt_stop, |
| 118 | .ping = wdt_ping, |
| 119 | }; |
| 120 | |
| 121 | static int mid_wdt_probe(struct platform_device *pdev) |
| 122 | { |
Guenter Roeck | b7b6adf | 2019-04-08 12:38:43 -0700 | [diff] [blame] | 123 | struct device *dev = &pdev->dev; |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 124 | struct watchdog_device *wdt_dev; |
Guenter Roeck | b7b6adf | 2019-04-08 12:38:43 -0700 | [diff] [blame] | 125 | struct intel_mid_wdt_pdata *pdata = dev->platform_data; |
Mika Westerberg | 80ae679 | 2020-04-16 11:15:38 +0300 | [diff] [blame] | 126 | struct mid_wdt *mid; |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 127 | int ret; |
| 128 | |
| 129 | if (!pdata) { |
Guenter Roeck | b7b6adf | 2019-04-08 12:38:43 -0700 | [diff] [blame] | 130 | dev_err(dev, "missing platform data\n"); |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 131 | return -EINVAL; |
| 132 | } |
| 133 | |
| 134 | if (pdata->probe) { |
| 135 | ret = pdata->probe(pdev); |
| 136 | if (ret) |
| 137 | return ret; |
| 138 | } |
| 139 | |
Mika Westerberg | 80ae679 | 2020-04-16 11:15:38 +0300 | [diff] [blame] | 140 | mid = devm_kzalloc(dev, sizeof(*mid), GFP_KERNEL); |
| 141 | if (!mid) |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 142 | return -ENOMEM; |
| 143 | |
Mika Westerberg | 80ae679 | 2020-04-16 11:15:38 +0300 | [diff] [blame] | 144 | mid->dev = dev; |
| 145 | wdt_dev = &mid->wd; |
| 146 | |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 147 | wdt_dev->info = &mid_wdt_info; |
| 148 | wdt_dev->ops = &mid_wdt_ops; |
| 149 | wdt_dev->min_timeout = MID_WDT_TIMEOUT_MIN; |
| 150 | wdt_dev->max_timeout = MID_WDT_TIMEOUT_MAX; |
| 151 | wdt_dev->timeout = MID_WDT_DEFAULT_TIMEOUT; |
Guenter Roeck | b7b6adf | 2019-04-08 12:38:43 -0700 | [diff] [blame] | 152 | wdt_dev->parent = dev; |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 153 | |
Andy Shevchenko | ff0aaac | 2019-09-24 17:31:16 +0300 | [diff] [blame] | 154 | watchdog_set_nowayout(wdt_dev, WATCHDOG_NOWAYOUT); |
Mika Westerberg | 80ae679 | 2020-04-16 11:15:38 +0300 | [diff] [blame] | 155 | watchdog_set_drvdata(wdt_dev, mid); |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 156 | |
Andy Shevchenko | f285c95 | 2020-10-23 19:33:02 +0300 | [diff] [blame] | 157 | mid->scu = devm_intel_scu_ipc_dev_get(dev); |
| 158 | if (!mid->scu) |
| 159 | return -EPROBE_DEFER; |
| 160 | |
Guenter Roeck | b7b6adf | 2019-04-08 12:38:43 -0700 | [diff] [blame] | 161 | ret = devm_request_irq(dev, pdata->irq, mid_wdt_irq, |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 162 | IRQF_SHARED | IRQF_NO_SUSPEND, "watchdog", |
| 163 | wdt_dev); |
| 164 | if (ret) { |
Guenter Roeck | b7b6adf | 2019-04-08 12:38:43 -0700 | [diff] [blame] | 165 | dev_err(dev, "error requesting warning irq %d\n", pdata->irq); |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 166 | return ret; |
| 167 | } |
| 168 | |
Andy Shevchenko | 954351e | 2017-03-11 00:22:17 +0200 | [diff] [blame] | 169 | /* |
| 170 | * The firmware followed by U-Boot leaves the watchdog running |
| 171 | * with the default threshold which may vary. When we get here |
| 172 | * we should make a decision to prevent any side effects before |
| 173 | * user space daemon will take care of it. The best option, |
| 174 | * taking into consideration that there is no way to read values |
| 175 | * back from hardware, is to enforce watchdog being run with |
| 176 | * deterministic values. |
| 177 | */ |
| 178 | ret = wdt_start(wdt_dev); |
| 179 | if (ret) |
| 180 | return ret; |
| 181 | |
| 182 | /* Make sure the watchdog is serviced */ |
| 183 | set_bit(WDOG_HW_RUNNING, &wdt_dev->status); |
Andy Shevchenko | 31ecad6 | 2016-11-18 16:50:02 +0200 | [diff] [blame] | 184 | |
Guenter Roeck | b7b6adf | 2019-04-08 12:38:43 -0700 | [diff] [blame] | 185 | ret = devm_watchdog_register_device(dev, wdt_dev); |
Wolfram Sang | ca2d449 | 2019-05-18 23:27:33 +0200 | [diff] [blame] | 186 | if (ret) |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 187 | return ret; |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 188 | |
Guenter Roeck | b7b6adf | 2019-04-08 12:38:43 -0700 | [diff] [blame] | 189 | dev_info(dev, "Intel MID watchdog device probed\n"); |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 194 | static struct platform_driver mid_wdt_driver = { |
| 195 | .probe = mid_wdt_probe, |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 196 | .driver = { |
David Cohen | 87a1ef8 | 2014-04-15 13:06:05 -0700 | [diff] [blame] | 197 | .name = "intel_mid_wdt", |
| 198 | }, |
| 199 | }; |
| 200 | |
| 201 | module_platform_driver(mid_wdt_driver); |
| 202 | |
| 203 | MODULE_AUTHOR("David Cohen <david.a.cohen@linux.intel.com>"); |
| 204 | MODULE_DESCRIPTION("Watchdog Driver for Intel MID platform"); |
| 205 | MODULE_LICENSE("GPL"); |