• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
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>
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 
wdt_command(int sub,u32 * in,int inlen)36 static 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 
wdt_start(struct watchdog_device * wd)41 static int wdt_start(struct watchdog_device *wd)
42 {
43 	struct device *dev = watchdog_get_drvdata(wd);
44 	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);
58 	if (ret)
59 		dev_crit(dev, "error starting watchdog: %d\n", ret);
60 
61 	return ret;
62 }
63 
wdt_ping(struct watchdog_device * wd)64 static int wdt_ping(struct watchdog_device *wd)
65 {
66 	struct device *dev = watchdog_get_drvdata(wd);
67 	int ret;
68 
69 	ret = wdt_command(SCU_WATCHDOG_KEEPALIVE, NULL, 0);
70 	if (ret)
71 		dev_crit(dev, "Error executing keepalive: %d\n", ret);
72 
73 	return ret;
74 }
75 
wdt_stop(struct watchdog_device * wd)76 static int wdt_stop(struct watchdog_device *wd)
77 {
78 	struct device *dev = watchdog_get_drvdata(wd);
79 	int ret;
80 
81 	ret = wdt_command(SCU_WATCHDOG_STOP, NULL, 0);
82 	if (ret)
83 		dev_crit(dev, "Error stopping watchdog: %d\n", ret);
84 
85 	return ret;
86 }
87 
mid_wdt_irq(int irq,void * dev_id)88 static 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 
96 static const struct watchdog_info mid_wdt_info = {
97 	.identity = "Intel MID SCU watchdog",
98 	.options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
99 };
100 
101 static 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 
mid_wdt_probe(struct platform_device * pdev)108 static int mid_wdt_probe(struct platform_device *pdev)
109 {
110 	struct device *dev = &pdev->dev;
111 	struct watchdog_device *wdt_dev;
112 	struct intel_mid_wdt_pdata *pdata = dev->platform_data;
113 	int ret;
114 
115 	if (!pdata) {
116 		dev_err(dev, "missing platform data\n");
117 		return -EINVAL;
118 	}
119 
120 	if (pdata->probe) {
121 		ret = pdata->probe(pdev);
122 		if (ret)
123 			return ret;
124 	}
125 
126 	wdt_dev = devm_kzalloc(dev, sizeof(*wdt_dev), GFP_KERNEL);
127 	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;
135 	wdt_dev->parent = dev;
136 
137 	watchdog_set_drvdata(wdt_dev, dev);
138 
139 	ret = devm_request_irq(dev, pdata->irq, mid_wdt_irq,
140 			       IRQF_SHARED | IRQF_NO_SUSPEND, "watchdog",
141 			       wdt_dev);
142 	if (ret) {
143 		dev_err(dev, "error requesting warning irq %d\n", pdata->irq);
144 		return ret;
145 	}
146 
147 	/*
148 	 * The firmware followed by U-Boot leaves the watchdog running
149 	 * with the default threshold which may vary. When we get here
150 	 * we should make a decision to prevent any side effects before
151 	 * user space daemon will take care of it. The best option,
152 	 * taking into consideration that there is no way to read values
153 	 * back from hardware, is to enforce watchdog being run with
154 	 * deterministic values.
155 	 */
156 	ret = wdt_start(wdt_dev);
157 	if (ret)
158 		return ret;
159 
160 	/* Make sure the watchdog is serviced */
161 	set_bit(WDOG_HW_RUNNING, &wdt_dev->status);
162 
163 	ret = devm_watchdog_register_device(dev, wdt_dev);
164 	if (ret)
165 		return ret;
166 
167 	dev_info(dev, "Intel MID watchdog device probed\n");
168 
169 	return 0;
170 }
171 
172 static struct platform_driver mid_wdt_driver = {
173 	.probe		= mid_wdt_probe,
174 	.driver		= {
175 		.name	= "intel_mid_wdt",
176 	},
177 };
178 
179 module_platform_driver(mid_wdt_driver);
180 
181 MODULE_AUTHOR("David Cohen <david.a.cohen@linux.intel.com>");
182 MODULE_DESCRIPTION("Watchdog Driver for Intel MID platform");
183 MODULE_LICENSE("GPL");
184