blob: 45e4d02221b5849e1a3f61fa83d85b16bb8382d6 [file] [log] [blame]
David Cohen87a1ef82014-04-15 13:06:05 -07001/*
2 * intel-mid_wdt: generic Intel MID SCU watchdog driver
3 *
4 * Platforms supported so far:
5 * - Merrifield only
6 *
7 * Copyright (C) 2014 Intel Corporation. All rights reserved.
8 * Contact: David Cohen <david.a.cohen@linux.intel.com>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General
12 * Public License as published by the Free Software Foundation.
13 */
14
15#include <linux/interrupt.h>
16#include <linux/module.h>
17#include <linux/nmi.h>
18#include <linux/platform_device.h>
19#include <linux/watchdog.h>
20#include <linux/platform_data/intel-mid_wdt.h>
21
22#include <asm/intel_scu_ipc.h>
23#include <asm/intel-mid.h>
24
25#define IPC_WATCHDOG 0xf8
26
27#define MID_WDT_PRETIMEOUT 15
28#define MID_WDT_TIMEOUT_MIN (1 + MID_WDT_PRETIMEOUT)
29#define MID_WDT_TIMEOUT_MAX 170
30#define MID_WDT_DEFAULT_TIMEOUT 90
31
32/* SCU watchdog messages */
33enum {
34 SCU_WATCHDOG_START = 0,
35 SCU_WATCHDOG_STOP,
36 SCU_WATCHDOG_KEEPALIVE,
37};
38
39static inline int wdt_command(int sub, u32 *in, int inlen)
40{
41 return intel_scu_ipc_command(IPC_WATCHDOG, sub, in, inlen, NULL, 0);
42}
43
44static int wdt_start(struct watchdog_device *wd)
45{
Andy Shevchenkobb790362016-11-18 17:24:41 +020046 struct device *dev = watchdog_get_drvdata(wd);
David Cohen87a1ef82014-04-15 13:06:05 -070047 int ret, in_size;
48 int timeout = wd->timeout;
49 struct ipc_wd_start {
50 u32 pretimeout;
51 u32 timeout;
52 } ipc_wd_start = { timeout - MID_WDT_PRETIMEOUT, timeout };
53
54 /*
55 * SCU expects the input size for watchdog IPC to
56 * be based on 4 bytes
57 */
58 in_size = DIV_ROUND_UP(sizeof(ipc_wd_start), 4);
59
60 ret = wdt_command(SCU_WATCHDOG_START, (u32 *)&ipc_wd_start, in_size);
Andy Shevchenkobb790362016-11-18 17:24:41 +020061 if (ret)
David Cohen87a1ef82014-04-15 13:06:05 -070062 dev_crit(dev, "error starting watchdog: %d\n", ret);
David Cohen87a1ef82014-04-15 13:06:05 -070063
64 return ret;
65}
66
67static int wdt_ping(struct watchdog_device *wd)
68{
Andy Shevchenkobb790362016-11-18 17:24:41 +020069 struct device *dev = watchdog_get_drvdata(wd);
David Cohen87a1ef82014-04-15 13:06:05 -070070 int ret;
71
72 ret = wdt_command(SCU_WATCHDOG_KEEPALIVE, NULL, 0);
Andy Shevchenkobb790362016-11-18 17:24:41 +020073 if (ret)
74 dev_crit(dev, "Error executing keepalive: %d\n", ret);
David Cohen87a1ef82014-04-15 13:06:05 -070075
76 return ret;
77}
78
79static int wdt_stop(struct watchdog_device *wd)
80{
Andy Shevchenkobb790362016-11-18 17:24:41 +020081 struct device *dev = watchdog_get_drvdata(wd);
David Cohen87a1ef82014-04-15 13:06:05 -070082 int ret;
83
84 ret = wdt_command(SCU_WATCHDOG_STOP, NULL, 0);
Andy Shevchenkobb790362016-11-18 17:24:41 +020085 if (ret)
86 dev_crit(dev, "Error stopping watchdog: %d\n", ret);
David Cohen87a1ef82014-04-15 13:06:05 -070087
88 return ret;
89}
90
91static irqreturn_t mid_wdt_irq(int irq, void *dev_id)
92{
93 panic("Kernel Watchdog");
94
95 /* This code should not be reached */
96 return IRQ_HANDLED;
97}
98
99static const struct watchdog_info mid_wdt_info = {
100 .identity = "Intel MID SCU watchdog",
David Cohen8cbb97e2015-10-05 16:49:58 -0700101 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
David Cohen87a1ef82014-04-15 13:06:05 -0700102};
103
104static const struct watchdog_ops mid_wdt_ops = {
105 .owner = THIS_MODULE,
106 .start = wdt_start,
107 .stop = wdt_stop,
108 .ping = wdt_ping,
109};
110
111static int mid_wdt_probe(struct platform_device *pdev)
112{
113 struct watchdog_device *wdt_dev;
114 struct intel_mid_wdt_pdata *pdata = pdev->dev.platform_data;
115 int ret;
116
117 if (!pdata) {
118 dev_err(&pdev->dev, "missing platform data\n");
119 return -EINVAL;
120 }
121
122 if (pdata->probe) {
123 ret = pdata->probe(pdev);
124 if (ret)
125 return ret;
126 }
127
128 wdt_dev = devm_kzalloc(&pdev->dev, sizeof(*wdt_dev), GFP_KERNEL);
129 if (!wdt_dev)
130 return -ENOMEM;
131
132 wdt_dev->info = &mid_wdt_info;
133 wdt_dev->ops = &mid_wdt_ops;
134 wdt_dev->min_timeout = MID_WDT_TIMEOUT_MIN;
135 wdt_dev->max_timeout = MID_WDT_TIMEOUT_MAX;
136 wdt_dev->timeout = MID_WDT_DEFAULT_TIMEOUT;
Pratyush Anand65518812015-08-20 14:05:01 +0530137 wdt_dev->parent = &pdev->dev;
David Cohen87a1ef82014-04-15 13:06:05 -0700138
139 watchdog_set_drvdata(wdt_dev, &pdev->dev);
David Cohen87a1ef82014-04-15 13:06:05 -0700140
141 ret = devm_request_irq(&pdev->dev, pdata->irq, mid_wdt_irq,
142 IRQF_SHARED | IRQF_NO_SUSPEND, "watchdog",
143 wdt_dev);
144 if (ret) {
145 dev_err(&pdev->dev, "error requesting warning irq %d\n",
146 pdata->irq);
147 return ret;
148 }
149
Andy Shevchenko31ecad62016-11-18 16:50:02 +0200150 /* Make sure the watchdog is not running */
151 wdt_stop(wdt_dev);
152
Guenter Roeck396164b2017-01-10 15:21:50 -0800153 ret = devm_watchdog_register_device(&pdev->dev, wdt_dev);
David Cohen87a1ef82014-04-15 13:06:05 -0700154 if (ret) {
155 dev_err(&pdev->dev, "error registering watchdog device\n");
156 return ret;
157 }
158
159 dev_info(&pdev->dev, "Intel MID watchdog device probed\n");
160
161 return 0;
162}
163
David Cohen87a1ef82014-04-15 13:06:05 -0700164static struct platform_driver mid_wdt_driver = {
165 .probe = mid_wdt_probe,
David Cohen87a1ef82014-04-15 13:06:05 -0700166 .driver = {
David Cohen87a1ef82014-04-15 13:06:05 -0700167 .name = "intel_mid_wdt",
168 },
169};
170
171module_platform_driver(mid_wdt_driver);
172
173MODULE_AUTHOR("David Cohen <david.a.cohen@linux.intel.com>");
174MODULE_DESCRIPTION("Watchdog Driver for Intel MID platform");
175MODULE_LICENSE("GPL");