Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Copyright (C) 2016 National Instruments Corp.
 
 
 
 
 
 
 
 
 
 
  4 */
  5
  6#include <linux/acpi.h>
  7#include <linux/bitops.h>
  8#include <linux/device.h>
  9#include <linux/io.h>
 10#include <linux/module.h>
 11#include <linux/platform_device.h>
 12#include <linux/watchdog.h>
 13
 14#define LOCK			0xA5
 15#define UNLOCK			0x5A
 16
 17#define WDT_CTRL_RESET_EN	BIT(7)
 18#define WDT_RELOAD_PORT_EN	BIT(7)
 19
 20#define WDT_CTRL		1
 21#define WDT_RELOAD_CTRL		2
 22#define WDT_PRESET_PRESCALE	4
 23#define WDT_REG_LOCK		5
 24#define WDT_COUNT		6
 25#define WDT_RELOAD_PORT		7
 26
 27#define WDT_MIN_TIMEOUT		1
 28#define WDT_MAX_TIMEOUT		464
 29#define WDT_DEFAULT_TIMEOUT	80
 30
 31#define WDT_MAX_COUNTER		15
 32
 33static unsigned int timeout;
 34module_param(timeout, uint, 0);
 35MODULE_PARM_DESC(timeout,
 36		 "Watchdog timeout in seconds. (default="
 37		 __MODULE_STRING(WDT_DEFAULT_TIMEOUT) ")");
 38
 39static bool nowayout = WATCHDOG_NOWAYOUT;
 40module_param(nowayout, bool, 0);
 41MODULE_PARM_DESC(nowayout,
 42		 "Watchdog cannot be stopped once started. (default="
 43		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
 44
 45struct nic7018_wdt {
 46	u16 io_base;
 47	u32 period;
 48	struct watchdog_device wdd;
 49};
 50
 51struct nic7018_config {
 52	u32 period;
 53	u8 divider;
 54};
 55
 56static const struct nic7018_config nic7018_configs[] = {
 57	{  2, 4 },
 58	{ 32, 5 },
 59};
 60
 61static inline u32 nic7018_timeout(u32 period, u8 counter)
 62{
 63	return period * counter - period / 2;
 64}
 65
 66static const struct nic7018_config *nic7018_get_config(u32 timeout,
 67						       u8 *counter)
 68{
 69	const struct nic7018_config *config;
 70	u8 count;
 71
 72	if (timeout < 30 && timeout != 16) {
 73		config = &nic7018_configs[0];
 74		count = timeout / 2 + 1;
 75	} else {
 76		config = &nic7018_configs[1];
 77		count = DIV_ROUND_UP(timeout + 16, 32);
 78
 79		if (count > WDT_MAX_COUNTER)
 80			count = WDT_MAX_COUNTER;
 81	}
 82	*counter = count;
 83	return config;
 84}
 85
 86static int nic7018_set_timeout(struct watchdog_device *wdd,
 87			       unsigned int timeout)
 88{
 89	struct nic7018_wdt *wdt = watchdog_get_drvdata(wdd);
 90	const struct nic7018_config *config;
 91	u8 counter;
 92
 93	config = nic7018_get_config(timeout, &counter);
 94
 95	outb(counter << 4 | config->divider,
 96	     wdt->io_base + WDT_PRESET_PRESCALE);
 97
 98	wdd->timeout = nic7018_timeout(config->period, counter);
 99	wdt->period = config->period;
100
101	return 0;
102}
103
104static int nic7018_start(struct watchdog_device *wdd)
105{
106	struct nic7018_wdt *wdt = watchdog_get_drvdata(wdd);
107	u8 control;
108
109	nic7018_set_timeout(wdd, wdd->timeout);
110
111	control = inb(wdt->io_base + WDT_RELOAD_CTRL);
112	outb(control | WDT_RELOAD_PORT_EN, wdt->io_base + WDT_RELOAD_CTRL);
113
114	outb(1, wdt->io_base + WDT_RELOAD_PORT);
115
116	control = inb(wdt->io_base + WDT_CTRL);
117	outb(control | WDT_CTRL_RESET_EN, wdt->io_base + WDT_CTRL);
118
119	return 0;
120}
121
122static int nic7018_stop(struct watchdog_device *wdd)
123{
124	struct nic7018_wdt *wdt = watchdog_get_drvdata(wdd);
125
126	outb(0, wdt->io_base + WDT_CTRL);
127	outb(0, wdt->io_base + WDT_RELOAD_CTRL);
128	outb(0xF0, wdt->io_base + WDT_PRESET_PRESCALE);
129
130	return 0;
131}
132
133static int nic7018_ping(struct watchdog_device *wdd)
134{
135	struct nic7018_wdt *wdt = watchdog_get_drvdata(wdd);
136
137	outb(1, wdt->io_base + WDT_RELOAD_PORT);
138
139	return 0;
140}
141
142static unsigned int nic7018_get_timeleft(struct watchdog_device *wdd)
143{
144	struct nic7018_wdt *wdt = watchdog_get_drvdata(wdd);
145	u8 count;
146
147	count = inb(wdt->io_base + WDT_COUNT) & 0xF;
148	if (!count)
149		return 0;
150
151	return nic7018_timeout(wdt->period, count);
152}
153
154static const struct watchdog_info nic7018_wdd_info = {
155	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
156	.identity = "NIC7018 Watchdog",
157};
158
159static const struct watchdog_ops nic7018_wdd_ops = {
160	.owner = THIS_MODULE,
161	.start = nic7018_start,
162	.stop = nic7018_stop,
163	.ping = nic7018_ping,
164	.set_timeout = nic7018_set_timeout,
165	.get_timeleft = nic7018_get_timeleft,
166};
167
168static int nic7018_probe(struct platform_device *pdev)
169{
170	struct device *dev = &pdev->dev;
171	struct watchdog_device *wdd;
172	struct nic7018_wdt *wdt;
173	struct resource *io_rc;
174	int ret;
175
176	wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
177	if (!wdt)
178		return -ENOMEM;
179
180	platform_set_drvdata(pdev, wdt);
181
182	io_rc = platform_get_resource(pdev, IORESOURCE_IO, 0);
183	if (!io_rc) {
184		dev_err(dev, "missing IO resources\n");
185		return -EINVAL;
186	}
187
188	if (!devm_request_region(dev, io_rc->start, resource_size(io_rc),
189				 KBUILD_MODNAME)) {
190		dev_err(dev, "failed to get IO region\n");
191		return -EBUSY;
192	}
193
194	wdt->io_base = io_rc->start;
195	wdd = &wdt->wdd;
196	wdd->info = &nic7018_wdd_info;
197	wdd->ops = &nic7018_wdd_ops;
198	wdd->min_timeout = WDT_MIN_TIMEOUT;
199	wdd->max_timeout = WDT_MAX_TIMEOUT;
200	wdd->timeout = WDT_DEFAULT_TIMEOUT;
201	wdd->parent = dev;
202
203	watchdog_set_drvdata(wdd, wdt);
204	watchdog_set_nowayout(wdd, nowayout);
205	watchdog_init_timeout(wdd, timeout, dev);
 
 
 
206
207	/* Unlock WDT register */
208	outb(UNLOCK, wdt->io_base + WDT_REG_LOCK);
209
210	ret = watchdog_register_device(wdd);
211	if (ret) {
212		outb(LOCK, wdt->io_base + WDT_REG_LOCK);
 
213		return ret;
214	}
215
216	dev_dbg(dev, "io_base=0x%04X, timeout=%d, nowayout=%d\n",
217		wdt->io_base, timeout, nowayout);
218	return 0;
219}
220
221static int nic7018_remove(struct platform_device *pdev)
222{
223	struct nic7018_wdt *wdt = platform_get_drvdata(pdev);
224
225	watchdog_unregister_device(&wdt->wdd);
226
227	/* Lock WDT register */
228	outb(LOCK, wdt->io_base + WDT_REG_LOCK);
229
230	return 0;
231}
232
233static const struct acpi_device_id nic7018_device_ids[] = {
234	{"NIC7018", 0},
235	{"", 0},
236};
237MODULE_DEVICE_TABLE(acpi, nic7018_device_ids);
238
239static struct platform_driver watchdog_driver = {
240	.probe = nic7018_probe,
241	.remove = nic7018_remove,
242	.driver = {
243		.name = KBUILD_MODNAME,
244		.acpi_match_table = ACPI_PTR(nic7018_device_ids),
245	},
246};
247
248module_platform_driver(watchdog_driver);
249
250MODULE_DESCRIPTION("National Instruments NIC7018 Watchdog driver");
251MODULE_AUTHOR("Hui Chun Ong <hui.chun.ong@ni.com>");
252MODULE_LICENSE("GPL");
v4.17
 
  1/*
  2 * Copyright (C) 2016 National Instruments Corp.
  3 *
  4 * This program is free software; you can redistribute it and/or modify
  5 * it under the terms of the GNU General Public License as published by
  6 * the Free Software Foundation; either version 2 of the License, or
  7 * (at your option) any later version.
  8 *
  9 * This program is distributed in the hope that it will be useful,
 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 * GNU General Public License for more details.
 13 */
 14
 15#include <linux/acpi.h>
 16#include <linux/bitops.h>
 17#include <linux/device.h>
 18#include <linux/io.h>
 19#include <linux/module.h>
 20#include <linux/platform_device.h>
 21#include <linux/watchdog.h>
 22
 23#define LOCK			0xA5
 24#define UNLOCK			0x5A
 25
 26#define WDT_CTRL_RESET_EN	BIT(7)
 27#define WDT_RELOAD_PORT_EN	BIT(7)
 28
 29#define WDT_CTRL		1
 30#define WDT_RELOAD_CTRL		2
 31#define WDT_PRESET_PRESCALE	4
 32#define WDT_REG_LOCK		5
 33#define WDT_COUNT		6
 34#define WDT_RELOAD_PORT		7
 35
 36#define WDT_MIN_TIMEOUT		1
 37#define WDT_MAX_TIMEOUT		464
 38#define WDT_DEFAULT_TIMEOUT	80
 39
 40#define WDT_MAX_COUNTER		15
 41
 42static unsigned int timeout;
 43module_param(timeout, uint, 0);
 44MODULE_PARM_DESC(timeout,
 45		 "Watchdog timeout in seconds. (default="
 46		 __MODULE_STRING(WDT_DEFAULT_TIMEOUT) ")");
 47
 48static bool nowayout = WATCHDOG_NOWAYOUT;
 49module_param(nowayout, bool, 0);
 50MODULE_PARM_DESC(nowayout,
 51		 "Watchdog cannot be stopped once started. (default="
 52		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
 53
 54struct nic7018_wdt {
 55	u16 io_base;
 56	u32 period;
 57	struct watchdog_device wdd;
 58};
 59
 60struct nic7018_config {
 61	u32 period;
 62	u8 divider;
 63};
 64
 65static const struct nic7018_config nic7018_configs[] = {
 66	{  2, 4 },
 67	{ 32, 5 },
 68};
 69
 70static inline u32 nic7018_timeout(u32 period, u8 counter)
 71{
 72	return period * counter - period / 2;
 73}
 74
 75static const struct nic7018_config *nic7018_get_config(u32 timeout,
 76						       u8 *counter)
 77{
 78	const struct nic7018_config *config;
 79	u8 count;
 80
 81	if (timeout < 30 && timeout != 16) {
 82		config = &nic7018_configs[0];
 83		count = timeout / 2 + 1;
 84	} else {
 85		config = &nic7018_configs[1];
 86		count = DIV_ROUND_UP(timeout + 16, 32);
 87
 88		if (count > WDT_MAX_COUNTER)
 89			count = WDT_MAX_COUNTER;
 90	}
 91	*counter = count;
 92	return config;
 93}
 94
 95static int nic7018_set_timeout(struct watchdog_device *wdd,
 96			       unsigned int timeout)
 97{
 98	struct nic7018_wdt *wdt = watchdog_get_drvdata(wdd);
 99	const struct nic7018_config *config;
100	u8 counter;
101
102	config = nic7018_get_config(timeout, &counter);
103
104	outb(counter << 4 | config->divider,
105	     wdt->io_base + WDT_PRESET_PRESCALE);
106
107	wdd->timeout = nic7018_timeout(config->period, counter);
108	wdt->period = config->period;
109
110	return 0;
111}
112
113static int nic7018_start(struct watchdog_device *wdd)
114{
115	struct nic7018_wdt *wdt = watchdog_get_drvdata(wdd);
116	u8 control;
117
118	nic7018_set_timeout(wdd, wdd->timeout);
119
120	control = inb(wdt->io_base + WDT_RELOAD_CTRL);
121	outb(control | WDT_RELOAD_PORT_EN, wdt->io_base + WDT_RELOAD_CTRL);
122
123	outb(1, wdt->io_base + WDT_RELOAD_PORT);
124
125	control = inb(wdt->io_base + WDT_CTRL);
126	outb(control | WDT_CTRL_RESET_EN, wdt->io_base + WDT_CTRL);
127
128	return 0;
129}
130
131static int nic7018_stop(struct watchdog_device *wdd)
132{
133	struct nic7018_wdt *wdt = watchdog_get_drvdata(wdd);
134
135	outb(0, wdt->io_base + WDT_CTRL);
136	outb(0, wdt->io_base + WDT_RELOAD_CTRL);
137	outb(0xF0, wdt->io_base + WDT_PRESET_PRESCALE);
138
139	return 0;
140}
141
142static int nic7018_ping(struct watchdog_device *wdd)
143{
144	struct nic7018_wdt *wdt = watchdog_get_drvdata(wdd);
145
146	outb(1, wdt->io_base + WDT_RELOAD_PORT);
147
148	return 0;
149}
150
151static unsigned int nic7018_get_timeleft(struct watchdog_device *wdd)
152{
153	struct nic7018_wdt *wdt = watchdog_get_drvdata(wdd);
154	u8 count;
155
156	count = inb(wdt->io_base + WDT_COUNT) & 0xF;
157	if (!count)
158		return 0;
159
160	return nic7018_timeout(wdt->period, count);
161}
162
163static const struct watchdog_info nic7018_wdd_info = {
164	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
165	.identity = "NIC7018 Watchdog",
166};
167
168static const struct watchdog_ops nic7018_wdd_ops = {
169	.owner = THIS_MODULE,
170	.start = nic7018_start,
171	.stop = nic7018_stop,
172	.ping = nic7018_ping,
173	.set_timeout = nic7018_set_timeout,
174	.get_timeleft = nic7018_get_timeleft,
175};
176
177static int nic7018_probe(struct platform_device *pdev)
178{
179	struct device *dev = &pdev->dev;
180	struct watchdog_device *wdd;
181	struct nic7018_wdt *wdt;
182	struct resource *io_rc;
183	int ret;
184
185	wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
186	if (!wdt)
187		return -ENOMEM;
188
189	platform_set_drvdata(pdev, wdt);
190
191	io_rc = platform_get_resource(pdev, IORESOURCE_IO, 0);
192	if (!io_rc) {
193		dev_err(dev, "missing IO resources\n");
194		return -EINVAL;
195	}
196
197	if (!devm_request_region(dev, io_rc->start, resource_size(io_rc),
198				 KBUILD_MODNAME)) {
199		dev_err(dev, "failed to get IO region\n");
200		return -EBUSY;
201	}
202
203	wdt->io_base = io_rc->start;
204	wdd = &wdt->wdd;
205	wdd->info = &nic7018_wdd_info;
206	wdd->ops = &nic7018_wdd_ops;
207	wdd->min_timeout = WDT_MIN_TIMEOUT;
208	wdd->max_timeout = WDT_MAX_TIMEOUT;
209	wdd->timeout = WDT_DEFAULT_TIMEOUT;
210	wdd->parent = dev;
211
212	watchdog_set_drvdata(wdd, wdt);
213	watchdog_set_nowayout(wdd, nowayout);
214
215	ret = watchdog_init_timeout(wdd, timeout, dev);
216	if (ret)
217		dev_warn(dev, "unable to set timeout value, using default\n");
218
219	/* Unlock WDT register */
220	outb(UNLOCK, wdt->io_base + WDT_REG_LOCK);
221
222	ret = watchdog_register_device(wdd);
223	if (ret) {
224		outb(LOCK, wdt->io_base + WDT_REG_LOCK);
225		dev_err(dev, "failed to register watchdog\n");
226		return ret;
227	}
228
229	dev_dbg(dev, "io_base=0x%04X, timeout=%d, nowayout=%d\n",
230		wdt->io_base, timeout, nowayout);
231	return 0;
232}
233
234static int nic7018_remove(struct platform_device *pdev)
235{
236	struct nic7018_wdt *wdt = platform_get_drvdata(pdev);
237
238	watchdog_unregister_device(&wdt->wdd);
239
240	/* Lock WDT register */
241	outb(LOCK, wdt->io_base + WDT_REG_LOCK);
242
243	return 0;
244}
245
246static const struct acpi_device_id nic7018_device_ids[] = {
247	{"NIC7018", 0},
248	{"", 0},
249};
250MODULE_DEVICE_TABLE(acpi, nic7018_device_ids);
251
252static struct platform_driver watchdog_driver = {
253	.probe = nic7018_probe,
254	.remove = nic7018_remove,
255	.driver = {
256		.name = KBUILD_MODNAME,
257		.acpi_match_table = ACPI_PTR(nic7018_device_ids),
258	},
259};
260
261module_platform_driver(watchdog_driver);
262
263MODULE_DESCRIPTION("National Instruments NIC7018 Watchdog driver");
264MODULE_AUTHOR("Hui Chun Ong <hui.chun.ong@ni.com>");
265MODULE_LICENSE("GPL");