blob: 470213abfd3df65b6f863b664eb7a8aeb96cc2ae [file] [log] [blame]
Thomas Gleixner25763b32019-05-28 10:10:09 -07001// SPDX-License-Identifier: GPL-2.0-only
David Cohen87a1ef82014-04-15 13:06:05 -07002/*
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 Cohen87a1ef82014-04-15 13:06:05 -070010 */
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 */
30enum {
31 SCU_WATCHDOG_START = 0,
32 SCU_WATCHDOG_STOP,
33 SCU_WATCHDOG_KEEPALIVE,
34};
35
36static inline int wdt_command(int sub, u32 *in, int inlen)
37{
38 return intel_scu_ipc_command(IPC_WATCHDOG, sub, in, inlen, NULL, 0);
39}
40
41static int wdt_start(struct watchdog_device *wd)
42{
Andy Shevchenkobb790362016-11-18 17:24:41 +020043 struct device *dev = watchdog_get_drvdata(wd);
David Cohen87a1ef82014-04-15 13:06:05 -070044 int ret, in_size;
45 int timeout = wd->timeout;
46 struct ipc_wd_start {
47 u32 pretimeout;
48 u32 timeout;
49 } ipc_wd_start = { timeout - MID_WDT_PRETIMEOUT, timeout };
50
51 /*
52 * SCU expects the input size for watchdog IPC to
53 * be based on 4 bytes
54 */
55 in_size = DIV_ROUND_UP(sizeof(ipc_wd_start), 4);
56
57 ret = wdt_command(SCU_WATCHDOG_START, (u32 *)&ipc_wd_start, in_size);
Andy Shevchenkobb790362016-11-18 17:24:41 +020058 if (ret)
David Cohen87a1ef82014-04-15 13:06:05 -070059 dev_crit(dev, "error starting watchdog: %d\n", ret);
David Cohen87a1ef82014-04-15 13:06:05 -070060
61 return ret;
62}
63
64static int wdt_ping(struct watchdog_device *wd)
65{
Andy Shevchenkobb790362016-11-18 17:24:41 +020066 struct device *dev = watchdog_get_drvdata(wd);
David Cohen87a1ef82014-04-15 13:06:05 -070067 int ret;
68
69 ret = wdt_command(SCU_WATCHDOG_KEEPALIVE, NULL, 0);
Andy Shevchenkobb790362016-11-18 17:24:41 +020070 if (ret)
71 dev_crit(dev, "Error executing keepalive: %d\n", ret);
David Cohen87a1ef82014-04-15 13:06:05 -070072
73 return ret;
74}
75
76static int wdt_stop(struct watchdog_device *wd)
77{
Andy Shevchenkobb790362016-11-18 17:24:41 +020078 struct device *dev = watchdog_get_drvdata(wd);
David Cohen87a1ef82014-04-15 13:06:05 -070079 int ret;
80
81 ret = wdt_command(SCU_WATCHDOG_STOP, NULL, 0);
Andy Shevchenkobb790362016-11-18 17:24:41 +020082 if (ret)
83 dev_crit(dev, "Error stopping watchdog: %d\n", ret);
David Cohen87a1ef82014-04-15 13:06:05 -070084
85 return ret;
86}
87
88static irqreturn_t mid_wdt_irq(int irq, void *dev_id)
89{
90 panic("Kernel Watchdog");
91
92 /* This code should not be reached */
93 return IRQ_HANDLED;
94}
95
96static const struct watchdog_info mid_wdt_info = {
97 .identity = "Intel MID SCU watchdog",
David Cohen8cbb97e2015-10-05 16:49:58 -070098 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
David Cohen87a1ef82014-04-15 13:06:05 -070099};
100
101static const struct watchdog_ops mid_wdt_ops = {
102 .owner = THIS_MODULE,
103 .start = wdt_start,
104 .stop = wdt_stop,
105 .ping = wdt_ping,
106};
107
108static int mid_wdt_probe(struct platform_device *pdev)
109{
Guenter Roeckb7b6adf2019-04-08 12:38:43 -0700110 struct device *dev = &pdev->dev;
David Cohen87a1ef82014-04-15 13:06:05 -0700111 struct watchdog_device *wdt_dev;
Guenter Roeckb7b6adf2019-04-08 12:38:43 -0700112 struct intel_mid_wdt_pdata *pdata = dev->platform_data;
David Cohen87a1ef82014-04-15 13:06:05 -0700113 int ret;
114
115 if (!pdata) {
Guenter Roeckb7b6adf2019-04-08 12:38:43 -0700116 dev_err(dev, "missing platform data\n");
David Cohen87a1ef82014-04-15 13:06:05 -0700117 return -EINVAL;
118 }
119
120 if (pdata->probe) {
121 ret = pdata->probe(pdev);
122 if (ret)
123 return ret;
124 }
125
Guenter Roeckb7b6adf2019-04-08 12:38:43 -0700126 wdt_dev = devm_kzalloc(dev, sizeof(*wdt_dev), GFP_KERNEL);
David Cohen87a1ef82014-04-15 13:06:05 -0700127 if (!wdt_dev)
128 return -ENOMEM;
129
130 wdt_dev->info = &mid_wdt_info;
131 wdt_dev->ops = &mid_wdt_ops;
132 wdt_dev->min_timeout = MID_WDT_TIMEOUT_MIN;
133 wdt_dev->max_timeout = MID_WDT_TIMEOUT_MAX;
134 wdt_dev->timeout = MID_WDT_DEFAULT_TIMEOUT;
Guenter Roeckb7b6adf2019-04-08 12:38:43 -0700135 wdt_dev->parent = dev;
David Cohen87a1ef82014-04-15 13:06:05 -0700136
Andy Shevchenkoff0aaac2019-09-24 17:31:16 +0300137 watchdog_set_nowayout(wdt_dev, WATCHDOG_NOWAYOUT);
Guenter Roeckb7b6adf2019-04-08 12:38:43 -0700138 watchdog_set_drvdata(wdt_dev, dev);
David Cohen87a1ef82014-04-15 13:06:05 -0700139
Guenter Roeckb7b6adf2019-04-08 12:38:43 -0700140 ret = devm_request_irq(dev, pdata->irq, mid_wdt_irq,
David Cohen87a1ef82014-04-15 13:06:05 -0700141 IRQF_SHARED | IRQF_NO_SUSPEND, "watchdog",
142 wdt_dev);
143 if (ret) {
Guenter Roeckb7b6adf2019-04-08 12:38:43 -0700144 dev_err(dev, "error requesting warning irq %d\n", pdata->irq);
David Cohen87a1ef82014-04-15 13:06:05 -0700145 return ret;
146 }
147
Andy Shevchenko954351e2017-03-11 00:22:17 +0200148 /*
149 * The firmware followed by U-Boot leaves the watchdog running
150 * with the default threshold which may vary. When we get here
151 * we should make a decision to prevent any side effects before
152 * user space daemon will take care of it. The best option,
153 * taking into consideration that there is no way to read values
154 * back from hardware, is to enforce watchdog being run with
155 * deterministic values.
156 */
157 ret = wdt_start(wdt_dev);
158 if (ret)
159 return ret;
160
161 /* Make sure the watchdog is serviced */
162 set_bit(WDOG_HW_RUNNING, &wdt_dev->status);
Andy Shevchenko31ecad62016-11-18 16:50:02 +0200163
Guenter Roeckb7b6adf2019-04-08 12:38:43 -0700164 ret = devm_watchdog_register_device(dev, wdt_dev);
Wolfram Sangca2d4492019-05-18 23:27:33 +0200165 if (ret)
David Cohen87a1ef82014-04-15 13:06:05 -0700166 return ret;
David Cohen87a1ef82014-04-15 13:06:05 -0700167
Guenter Roeckb7b6adf2019-04-08 12:38:43 -0700168 dev_info(dev, "Intel MID watchdog device probed\n");
David Cohen87a1ef82014-04-15 13:06:05 -0700169
170 return 0;
171}
172
David Cohen87a1ef82014-04-15 13:06:05 -0700173static struct platform_driver mid_wdt_driver = {
174 .probe = mid_wdt_probe,
David Cohen87a1ef82014-04-15 13:06:05 -0700175 .driver = {
David Cohen87a1ef82014-04-15 13:06:05 -0700176 .name = "intel_mid_wdt",
177 },
178};
179
180module_platform_driver(mid_wdt_driver);
181
182MODULE_AUTHOR("David Cohen <david.a.cohen@linux.intel.com>");
183MODULE_DESCRIPTION("Watchdog Driver for Intel MID platform");
184MODULE_LICENSE("GPL");