Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  2 * Copyright (c) 2009 Nuvoton technology corporation.
  3 *
  4 * Wan ZongShun <mcuos.com@gmail.com>
  5 *
  6 * This program is free software; you can redistribute it and/or modify
  7 * it under the terms of the GNU General Public License as published by
  8 * the Free Software Foundation;version 2 of the License.
  9 *
 10 */
 11
 12#include <linux/bitops.h>
 13#include <linux/errno.h>
 14#include <linux/fs.h>
 15#include <linux/init.h>
 16#include <linux/io.h>
 17#include <linux/clk.h>
 18#include <linux/kernel.h>
 19#include <linux/miscdevice.h>
 20#include <linux/module.h>
 21#include <linux/moduleparam.h>
 22#include <linux/platform_device.h>
 23#include <linux/slab.h>
 24#include <linux/interrupt.h>
 25#include <linux/types.h>
 26#include <linux/watchdog.h>
 27#include <linux/uaccess.h>
 28
 29#define REG_WTCR		0x1c
 30#define WTCLK			(0x01 << 10)
 31#define WTE			(0x01 << 7)	/*wdt enable*/
 32#define WTIS			(0x03 << 4)
 33#define WTIF			(0x01 << 3)
 34#define WTRF			(0x01 << 2)
 35#define WTRE			(0x01 << 1)
 36#define WTR			(0x01 << 0)
 37/*
 38 * The watchdog time interval can be calculated via following formula:
 39 * WTIS		real time interval (formula)
 40 * 0x00		((2^ 14 ) * ((external crystal freq) / 256))seconds
 41 * 0x01		((2^ 16 ) * ((external crystal freq) / 256))seconds
 42 * 0x02		((2^ 18 ) * ((external crystal freq) / 256))seconds
 43 * 0x03		((2^ 20 ) * ((external crystal freq) / 256))seconds
 44 *
 45 * The external crystal freq is 15Mhz in the nuc900 evaluation board.
 46 * So 0x00 = +-0.28 seconds, 0x01 = +-1.12 seconds, 0x02 = +-4.48 seconds,
 47 * 0x03 = +- 16.92 seconds..
 48 */
 49#define WDT_HW_TIMEOUT		0x02
 50#define WDT_TIMEOUT		(HZ/2)
 51#define WDT_HEARTBEAT		15
 52
 53static int heartbeat = WDT_HEARTBEAT;
 54module_param(heartbeat, int, 0);
 55MODULE_PARM_DESC(heartbeat, "Watchdog heartbeats in seconds. "
 56	"(default = " __MODULE_STRING(WDT_HEARTBEAT) ")");
 57
 58static int nowayout = WATCHDOG_NOWAYOUT;
 59module_param(nowayout, int, 0);
 60MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
 61	"(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
 62
 63struct nuc900_wdt {
 64	struct resource  *res;
 65	struct clk	 *wdt_clock;
 66	struct platform_device *pdev;
 67	void __iomem	 *wdt_base;
 68	char		 expect_close;
 69	struct timer_list timer;
 70	spinlock_t       wdt_lock;
 71	unsigned long next_heartbeat;
 72};
 73
 74static unsigned long nuc900wdt_busy;
 75struct nuc900_wdt *nuc900_wdt;
 76
 77static inline void nuc900_wdt_keepalive(void)
 78{
 79	unsigned int val;
 80
 81	spin_lock(&nuc900_wdt->wdt_lock);
 82
 83	val = __raw_readl(nuc900_wdt->wdt_base + REG_WTCR);
 84	val |= (WTR | WTIF);
 85	__raw_writel(val, nuc900_wdt->wdt_base + REG_WTCR);
 86
 87	spin_unlock(&nuc900_wdt->wdt_lock);
 88}
 89
 90static inline void nuc900_wdt_start(void)
 91{
 92	unsigned int val;
 93
 94	spin_lock(&nuc900_wdt->wdt_lock);
 95
 96	val = __raw_readl(nuc900_wdt->wdt_base + REG_WTCR);
 97	val |= (WTRE | WTE | WTR | WTCLK | WTIF);
 98	val &= ~WTIS;
 99	val |= (WDT_HW_TIMEOUT << 0x04);
100	__raw_writel(val, nuc900_wdt->wdt_base + REG_WTCR);
101
102	spin_unlock(&nuc900_wdt->wdt_lock);
103
104	nuc900_wdt->next_heartbeat = jiffies + heartbeat * HZ;
105	mod_timer(&nuc900_wdt->timer, jiffies + WDT_TIMEOUT);
106}
107
108static inline void nuc900_wdt_stop(void)
109{
110	unsigned int val;
111
112	del_timer(&nuc900_wdt->timer);
113
114	spin_lock(&nuc900_wdt->wdt_lock);
115
116	val = __raw_readl(nuc900_wdt->wdt_base + REG_WTCR);
117	val &= ~WTE;
118	__raw_writel(val, nuc900_wdt->wdt_base + REG_WTCR);
119
120	spin_unlock(&nuc900_wdt->wdt_lock);
121}
122
123static inline void nuc900_wdt_ping(void)
124{
125	nuc900_wdt->next_heartbeat = jiffies + heartbeat * HZ;
126}
127
128static int nuc900_wdt_open(struct inode *inode, struct file *file)
129{
130
131	if (test_and_set_bit(0, &nuc900wdt_busy))
132		return -EBUSY;
133
134	nuc900_wdt_start();
135
136	return nonseekable_open(inode, file);
137}
138
139static int nuc900_wdt_close(struct inode *inode, struct file *file)
140{
141	if (nuc900_wdt->expect_close == 42)
142		nuc900_wdt_stop();
143	else {
144		dev_crit(&nuc900_wdt->pdev->dev,
145			"Unexpected close, not stopping watchdog!\n");
146		nuc900_wdt_ping();
147	}
148
149	nuc900_wdt->expect_close = 0;
150	clear_bit(0, &nuc900wdt_busy);
151	return 0;
152}
153
154static const struct watchdog_info nuc900_wdt_info = {
155	.identity	= "nuc900 watchdog",
156	.options	= WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
157						WDIOF_MAGICCLOSE,
158};
159
160static long nuc900_wdt_ioctl(struct file *file,
161					unsigned int cmd, unsigned long arg)
162{
163	void __user *argp = (void __user *)arg;
164	int __user *p = argp;
165	int new_value;
166
167	switch (cmd) {
168	case WDIOC_GETSUPPORT:
169		return copy_to_user(argp, &nuc900_wdt_info,
170				sizeof(nuc900_wdt_info)) ? -EFAULT : 0;
171	case WDIOC_GETSTATUS:
172	case WDIOC_GETBOOTSTATUS:
173		return put_user(0, p);
174
175	case WDIOC_KEEPALIVE:
176		nuc900_wdt_ping();
177		return 0;
178
179	case WDIOC_SETTIMEOUT:
180		if (get_user(new_value, p))
181			return -EFAULT;
182
183		heartbeat = new_value;
184		nuc900_wdt_ping();
185
186		return put_user(new_value, p);
187	case WDIOC_GETTIMEOUT:
188		return put_user(heartbeat, p);
189	default:
190		return -ENOTTY;
191	}
192}
193
194static ssize_t nuc900_wdt_write(struct file *file, const char __user *data,
195						size_t len, loff_t *ppos)
196{
197	if (!len)
198		return 0;
199
200	/* Scan for magic character */
201	if (!nowayout) {
202		size_t i;
203
204		nuc900_wdt->expect_close = 0;
205
206		for (i = 0; i < len; i++) {
207			char c;
208			if (get_user(c, data + i))
209				return -EFAULT;
210			if (c == 'V') {
211				nuc900_wdt->expect_close = 42;
212				break;
213			}
214		}
215	}
216
217	nuc900_wdt_ping();
218	return len;
219}
220
221static void nuc900_wdt_timer_ping(unsigned long data)
222{
223	if (time_before(jiffies, nuc900_wdt->next_heartbeat)) {
224		nuc900_wdt_keepalive();
225		mod_timer(&nuc900_wdt->timer, jiffies + WDT_TIMEOUT);
226	} else
227		dev_warn(&nuc900_wdt->pdev->dev, "Will reset the machine !\n");
228}
229
230static const struct file_operations nuc900wdt_fops = {
231	.owner		= THIS_MODULE,
232	.llseek		= no_llseek,
233	.unlocked_ioctl	= nuc900_wdt_ioctl,
234	.open		= nuc900_wdt_open,
235	.release	= nuc900_wdt_close,
236	.write		= nuc900_wdt_write,
237};
238
239static struct miscdevice nuc900wdt_miscdev = {
240	.minor		= WATCHDOG_MINOR,
241	.name		= "watchdog",
242	.fops		= &nuc900wdt_fops,
243};
244
245static int __devinit nuc900wdt_probe(struct platform_device *pdev)
246{
 
247	int ret = 0;
248
249	nuc900_wdt = kzalloc(sizeof(struct nuc900_wdt), GFP_KERNEL);
 
250	if (!nuc900_wdt)
251		return -ENOMEM;
252
253	nuc900_wdt->pdev = pdev;
254
255	spin_lock_init(&nuc900_wdt->wdt_lock);
256
257	nuc900_wdt->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
258	if (nuc900_wdt->res == NULL) {
259		dev_err(&pdev->dev, "no memory resource specified\n");
260		ret = -ENOENT;
261		goto err_get;
262	}
263
264	if (!request_mem_region(nuc900_wdt->res->start,
265				resource_size(nuc900_wdt->res), pdev->name)) {
266		dev_err(&pdev->dev, "failed to get memory region\n");
267		ret = -ENOENT;
268		goto err_get;
269	}
270
271	nuc900_wdt->wdt_base = ioremap(nuc900_wdt->res->start,
272					resource_size(nuc900_wdt->res));
273	if (nuc900_wdt->wdt_base == NULL) {
274		dev_err(&pdev->dev, "failed to ioremap() region\n");
275		ret = -EINVAL;
276		goto err_req;
277	}
278
279	nuc900_wdt->wdt_clock = clk_get(&pdev->dev, NULL);
280	if (IS_ERR(nuc900_wdt->wdt_clock)) {
281		dev_err(&pdev->dev, "failed to find watchdog clock source\n");
282		ret = PTR_ERR(nuc900_wdt->wdt_clock);
283		goto err_map;
284	}
285
286	clk_enable(nuc900_wdt->wdt_clock);
287
288	setup_timer(&nuc900_wdt->timer, nuc900_wdt_timer_ping, 0);
289
290	if (misc_register(&nuc900wdt_miscdev)) {
 
291		dev_err(&pdev->dev, "err register miscdev on minor=%d (%d)\n",
292			WATCHDOG_MINOR, ret);
293		goto err_clk;
294	}
295
296	return 0;
297
298err_clk:
299	clk_disable(nuc900_wdt->wdt_clock);
300	clk_put(nuc900_wdt->wdt_clock);
301err_map:
302	iounmap(nuc900_wdt->wdt_base);
303err_req:
304	release_mem_region(nuc900_wdt->res->start,
305					resource_size(nuc900_wdt->res));
306err_get:
307	kfree(nuc900_wdt);
308	return ret;
309}
310
311static int __devexit nuc900wdt_remove(struct platform_device *pdev)
312{
313	misc_deregister(&nuc900wdt_miscdev);
314
315	clk_disable(nuc900_wdt->wdt_clock);
316	clk_put(nuc900_wdt->wdt_clock);
317
318	iounmap(nuc900_wdt->wdt_base);
319
320	release_mem_region(nuc900_wdt->res->start,
321					resource_size(nuc900_wdt->res));
322
323	kfree(nuc900_wdt);
324
325	return 0;
326}
327
328static struct platform_driver nuc900wdt_driver = {
329	.probe		= nuc900wdt_probe,
330	.remove		= __devexit_p(nuc900wdt_remove),
331	.driver		= {
332		.name	= "nuc900-wdt",
333		.owner	= THIS_MODULE,
334	},
335};
336
337static int __init nuc900_wdt_init(void)
338{
339	return platform_driver_register(&nuc900wdt_driver);
340}
341
342static void __exit nuc900_wdt_exit(void)
343{
344	platform_driver_unregister(&nuc900wdt_driver);
345}
346
347module_init(nuc900_wdt_init);
348module_exit(nuc900_wdt_exit);
349
350MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
351MODULE_DESCRIPTION("Watchdog driver for NUC900");
352MODULE_LICENSE("GPL");
353MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
354MODULE_ALIAS("platform:nuc900-wdt");
v4.10.11
  1/*
  2 * Copyright (c) 2009 Nuvoton technology corporation.
  3 *
  4 * Wan ZongShun <mcuos.com@gmail.com>
  5 *
  6 * This program is free software; you can redistribute it and/or modify
  7 * it under the terms of the GNU General Public License as published by
  8 * the Free Software Foundation;version 2 of the License.
  9 *
 10 */
 11
 12#include <linux/bitops.h>
 13#include <linux/errno.h>
 14#include <linux/fs.h>
 
 15#include <linux/io.h>
 16#include <linux/clk.h>
 17#include <linux/kernel.h>
 18#include <linux/miscdevice.h>
 19#include <linux/module.h>
 20#include <linux/moduleparam.h>
 21#include <linux/platform_device.h>
 22#include <linux/slab.h>
 23#include <linux/interrupt.h>
 24#include <linux/types.h>
 25#include <linux/watchdog.h>
 26#include <linux/uaccess.h>
 27
 28#define REG_WTCR		0x1c
 29#define WTCLK			(0x01 << 10)
 30#define WTE			(0x01 << 7)	/*wdt enable*/
 31#define WTIS			(0x03 << 4)
 32#define WTIF			(0x01 << 3)
 33#define WTRF			(0x01 << 2)
 34#define WTRE			(0x01 << 1)
 35#define WTR			(0x01 << 0)
 36/*
 37 * The watchdog time interval can be calculated via following formula:
 38 * WTIS		real time interval (formula)
 39 * 0x00		((2^ 14 ) * ((external crystal freq) / 256))seconds
 40 * 0x01		((2^ 16 ) * ((external crystal freq) / 256))seconds
 41 * 0x02		((2^ 18 ) * ((external crystal freq) / 256))seconds
 42 * 0x03		((2^ 20 ) * ((external crystal freq) / 256))seconds
 43 *
 44 * The external crystal freq is 15Mhz in the nuc900 evaluation board.
 45 * So 0x00 = +-0.28 seconds, 0x01 = +-1.12 seconds, 0x02 = +-4.48 seconds,
 46 * 0x03 = +- 16.92 seconds..
 47 */
 48#define WDT_HW_TIMEOUT		0x02
 49#define WDT_TIMEOUT		(HZ/2)
 50#define WDT_HEARTBEAT		15
 51
 52static int heartbeat = WDT_HEARTBEAT;
 53module_param(heartbeat, int, 0);
 54MODULE_PARM_DESC(heartbeat, "Watchdog heartbeats in seconds. "
 55	"(default = " __MODULE_STRING(WDT_HEARTBEAT) ")");
 56
 57static bool nowayout = WATCHDOG_NOWAYOUT;
 58module_param(nowayout, bool, 0);
 59MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
 60	"(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
 61
 62struct nuc900_wdt {
 
 63	struct clk	 *wdt_clock;
 64	struct platform_device *pdev;
 65	void __iomem	 *wdt_base;
 66	char		 expect_close;
 67	struct timer_list timer;
 68	spinlock_t       wdt_lock;
 69	unsigned long next_heartbeat;
 70};
 71
 72static unsigned long nuc900wdt_busy;
 73static struct nuc900_wdt *nuc900_wdt;
 74
 75static inline void nuc900_wdt_keepalive(void)
 76{
 77	unsigned int val;
 78
 79	spin_lock(&nuc900_wdt->wdt_lock);
 80
 81	val = __raw_readl(nuc900_wdt->wdt_base + REG_WTCR);
 82	val |= (WTR | WTIF);
 83	__raw_writel(val, nuc900_wdt->wdt_base + REG_WTCR);
 84
 85	spin_unlock(&nuc900_wdt->wdt_lock);
 86}
 87
 88static inline void nuc900_wdt_start(void)
 89{
 90	unsigned int val;
 91
 92	spin_lock(&nuc900_wdt->wdt_lock);
 93
 94	val = __raw_readl(nuc900_wdt->wdt_base + REG_WTCR);
 95	val |= (WTRE | WTE | WTR | WTCLK | WTIF);
 96	val &= ~WTIS;
 97	val |= (WDT_HW_TIMEOUT << 0x04);
 98	__raw_writel(val, nuc900_wdt->wdt_base + REG_WTCR);
 99
100	spin_unlock(&nuc900_wdt->wdt_lock);
101
102	nuc900_wdt->next_heartbeat = jiffies + heartbeat * HZ;
103	mod_timer(&nuc900_wdt->timer, jiffies + WDT_TIMEOUT);
104}
105
106static inline void nuc900_wdt_stop(void)
107{
108	unsigned int val;
109
110	del_timer(&nuc900_wdt->timer);
111
112	spin_lock(&nuc900_wdt->wdt_lock);
113
114	val = __raw_readl(nuc900_wdt->wdt_base + REG_WTCR);
115	val &= ~WTE;
116	__raw_writel(val, nuc900_wdt->wdt_base + REG_WTCR);
117
118	spin_unlock(&nuc900_wdt->wdt_lock);
119}
120
121static inline void nuc900_wdt_ping(void)
122{
123	nuc900_wdt->next_heartbeat = jiffies + heartbeat * HZ;
124}
125
126static int nuc900_wdt_open(struct inode *inode, struct file *file)
127{
128
129	if (test_and_set_bit(0, &nuc900wdt_busy))
130		return -EBUSY;
131
132	nuc900_wdt_start();
133
134	return nonseekable_open(inode, file);
135}
136
137static int nuc900_wdt_close(struct inode *inode, struct file *file)
138{
139	if (nuc900_wdt->expect_close == 42)
140		nuc900_wdt_stop();
141	else {
142		dev_crit(&nuc900_wdt->pdev->dev,
143			"Unexpected close, not stopping watchdog!\n");
144		nuc900_wdt_ping();
145	}
146
147	nuc900_wdt->expect_close = 0;
148	clear_bit(0, &nuc900wdt_busy);
149	return 0;
150}
151
152static const struct watchdog_info nuc900_wdt_info = {
153	.identity	= "nuc900 watchdog",
154	.options	= WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
155						WDIOF_MAGICCLOSE,
156};
157
158static long nuc900_wdt_ioctl(struct file *file,
159					unsigned int cmd, unsigned long arg)
160{
161	void __user *argp = (void __user *)arg;
162	int __user *p = argp;
163	int new_value;
164
165	switch (cmd) {
166	case WDIOC_GETSUPPORT:
167		return copy_to_user(argp, &nuc900_wdt_info,
168				sizeof(nuc900_wdt_info)) ? -EFAULT : 0;
169	case WDIOC_GETSTATUS:
170	case WDIOC_GETBOOTSTATUS:
171		return put_user(0, p);
172
173	case WDIOC_KEEPALIVE:
174		nuc900_wdt_ping();
175		return 0;
176
177	case WDIOC_SETTIMEOUT:
178		if (get_user(new_value, p))
179			return -EFAULT;
180
181		heartbeat = new_value;
182		nuc900_wdt_ping();
183
184		return put_user(new_value, p);
185	case WDIOC_GETTIMEOUT:
186		return put_user(heartbeat, p);
187	default:
188		return -ENOTTY;
189	}
190}
191
192static ssize_t nuc900_wdt_write(struct file *file, const char __user *data,
193						size_t len, loff_t *ppos)
194{
195	if (!len)
196		return 0;
197
198	/* Scan for magic character */
199	if (!nowayout) {
200		size_t i;
201
202		nuc900_wdt->expect_close = 0;
203
204		for (i = 0; i < len; i++) {
205			char c;
206			if (get_user(c, data + i))
207				return -EFAULT;
208			if (c == 'V') {
209				nuc900_wdt->expect_close = 42;
210				break;
211			}
212		}
213	}
214
215	nuc900_wdt_ping();
216	return len;
217}
218
219static void nuc900_wdt_timer_ping(unsigned long data)
220{
221	if (time_before(jiffies, nuc900_wdt->next_heartbeat)) {
222		nuc900_wdt_keepalive();
223		mod_timer(&nuc900_wdt->timer, jiffies + WDT_TIMEOUT);
224	} else
225		dev_warn(&nuc900_wdt->pdev->dev, "Will reset the machine !\n");
226}
227
228static const struct file_operations nuc900wdt_fops = {
229	.owner		= THIS_MODULE,
230	.llseek		= no_llseek,
231	.unlocked_ioctl	= nuc900_wdt_ioctl,
232	.open		= nuc900_wdt_open,
233	.release	= nuc900_wdt_close,
234	.write		= nuc900_wdt_write,
235};
236
237static struct miscdevice nuc900wdt_miscdev = {
238	.minor		= WATCHDOG_MINOR,
239	.name		= "watchdog",
240	.fops		= &nuc900wdt_fops,
241};
242
243static int nuc900wdt_probe(struct platform_device *pdev)
244{
245	struct resource *res;
246	int ret = 0;
247
248	nuc900_wdt = devm_kzalloc(&pdev->dev, sizeof(*nuc900_wdt),
249				GFP_KERNEL);
250	if (!nuc900_wdt)
251		return -ENOMEM;
252
253	nuc900_wdt->pdev = pdev;
254
255	spin_lock_init(&nuc900_wdt->wdt_lock);
256
257	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
258	nuc900_wdt->wdt_base = devm_ioremap_resource(&pdev->dev, res);
259	if (IS_ERR(nuc900_wdt->wdt_base))
260		return PTR_ERR(nuc900_wdt->wdt_base);
 
 
 
 
 
 
 
 
 
261
262	nuc900_wdt->wdt_clock = devm_clk_get(&pdev->dev, NULL);
 
 
 
 
 
 
 
 
263	if (IS_ERR(nuc900_wdt->wdt_clock)) {
264		dev_err(&pdev->dev, "failed to find watchdog clock source\n");
265		return PTR_ERR(nuc900_wdt->wdt_clock);
 
266	}
267
268	clk_enable(nuc900_wdt->wdt_clock);
269
270	setup_timer(&nuc900_wdt->timer, nuc900_wdt_timer_ping, 0);
271
272	ret = misc_register(&nuc900wdt_miscdev);
273	if (ret) {
274		dev_err(&pdev->dev, "err register miscdev on minor=%d (%d)\n",
275			WATCHDOG_MINOR, ret);
276		goto err_clk;
277	}
278
279	return 0;
280
281err_clk:
282	clk_disable(nuc900_wdt->wdt_clock);
 
 
 
 
 
 
 
 
283	return ret;
284}
285
286static int nuc900wdt_remove(struct platform_device *pdev)
287{
288	misc_deregister(&nuc900wdt_miscdev);
289
290	clk_disable(nuc900_wdt->wdt_clock);
 
 
 
 
 
 
 
 
291
292	return 0;
293}
294
295static struct platform_driver nuc900wdt_driver = {
296	.probe		= nuc900wdt_probe,
297	.remove		= nuc900wdt_remove,
298	.driver		= {
299		.name	= "nuc900-wdt",
 
300	},
301};
302
303module_platform_driver(nuc900wdt_driver);
 
 
 
 
 
 
 
 
 
 
 
304
305MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
306MODULE_DESCRIPTION("Watchdog driver for NUC900");
307MODULE_LICENSE("GPL");
 
308MODULE_ALIAS("platform:nuc900-wdt");