UPSTREAM: drm/edid: Extract SADs properly from multiple audio data blocks
[firefly-linux-kernel-4.4.55.git] / drivers / watchdog / rk29_wdt.c
1 /* linux/drivers/watchdog/rk29_wdt.c
2  *
3  * Copyright (C) 2011 ROCKCHIP, Inc.
4  *      hhb@rock-chips.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; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/types.h>
25 #include <linux/timer.h>
26 #include <linux/miscdevice.h>
27 #include <linux/watchdog.h>
28 #include <linux/fs.h>
29 #include <linux/init.h>
30 #include <linux/platform_device.h>
31 #include <linux/interrupt.h>
32 #include <linux/clk.h>
33 #include <linux/uaccess.h>
34 #include <linux/io.h>
35 #ifndef CONFIG_ARM64
36 #include <asm/mach/map.h>
37 #endif
38 #ifdef CONFIG_OF
39 #include <linux/of.h>
40 #endif
41
42
43 /* RK29 registers define */
44 #define RK29_WDT_CR     0X00
45 #define RK29_WDT_TORR   0X04
46 #define RK29_WDT_CCVR   0X08
47 #define RK29_WDT_CRR    0X0C
48 #define RK29_WDT_STAT   0X10
49 #define RK29_WDT_EOI    0X14
50
51 #define RK29_WDT_EN     1
52 #define RK29_RESPONSE_MODE      1
53 #define RK29_RESET_PULSE    4
54
55 //THAT wdt's clock is 24MHZ
56 #define RK29_WDT_2730US         0
57 #define RK29_WDT_5460US         1
58 #define RK29_WDT_10920US        2
59 #define RK29_WDT_21840US        3
60 #define RK29_WDT_43680US        4
61 #define RK29_WDT_87360US        5
62 #define RK29_WDT_174720US       6
63 #define RK29_WDT_349440US       7
64 #define RK29_WDT_698880US       8
65 #define RK29_WDT_1397760US      9
66 #define RK29_WDT_2795520US      10
67 #define RK29_WDT_5591040US      11
68 #define RK29_WDT_11182080US     12
69 #define RK29_WDT_22364160US     13
70 #define RK29_WDT_44728320US     14
71 #define RK29_WDT_89456640US     15
72
73 /*
74 #define CONFIG_RK29_WATCHDOG_ATBOOT             (1)
75 #define CONFIG_RK29_WATCHDOG_DEFAULT_TIME       10      //unit second
76 #define CONFIG_RK29_WATCHDOG_DEBUG      1
77 */
78
79 static int nowayout     = WATCHDOG_NOWAYOUT;
80 static int tmr_margin   = 100;//CONFIG_RK29_WATCHDOG_DEFAULT_TIME;
81 #ifdef CONFIG_RK29_WATCHDOG_ATBOOT
82 static int tmr_atboot   = 1;
83 #else
84 static int tmr_atboot   = 0;
85 #endif
86
87 static int soft_noboot;
88
89 #ifdef CONFIG_RK29_WATCHDOG_DEBUG
90 static int debug        = 1;
91 #else
92 static int debug        = 0;
93 #endif
94
95 module_param(tmr_margin,  int, 0);
96 module_param(tmr_atboot,  int, 0);
97 module_param(nowayout,    int, 0);
98 module_param(soft_noboot, int, 0);
99 module_param(debug,       int, 0);
100
101 MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. default="
102                 __MODULE_STRING(CONFIG_RK29_WATCHDOG_DEFAULT_TIME) ")");
103 MODULE_PARM_DESC(tmr_atboot,
104                 "Watchdog is started at boot time if set to 1, default="
105                         __MODULE_STRING(CONFIG_RK29_WATCHDOG_ATBOOT));
106 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
107                         __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
108 MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, "
109                         "0 to reboot (default depends on ONLY_TESTING)");
110 MODULE_PARM_DESC(debug, "Watchdog debug, set to >1 for debug, (default 0)");
111
112
113 static unsigned long open_lock;
114 static struct device    *wdt_dev;       /* platform device attached to */
115 static struct resource  *wdt_mem;
116 static struct resource  *wdt_irq;
117 static struct clk       *wdt_clock;
118 static void __iomem     *wdt_base;
119 static char              expect_close;
120
121
122 /* watchdog control routines */
123
124 #define DBG(msg...) do { \
125         if (debug) \
126                 printk(KERN_INFO msg); \
127         } while (0)
128
129 #ifdef CONFIG_ARM64
130 #define wdt_writel(v, offset) do { \
131         writel_relaxed(v, wdt_base + offset);\
132         dsb(sy);\
133         } while (0)
134 #else
135 #define wdt_writel(v, offset) do { \
136         writel_relaxed(v, wdt_base + offset);\
137         dsb();\
138         } while (0)
139 #endif
140
141 /* functions */
142 void rk29_wdt_keepalive(void)
143 {
144         if (wdt_base)
145                 wdt_writel(0x76, RK29_WDT_CRR);
146 }
147
148 static void __rk29_wdt_stop(void)
149 {
150         rk29_wdt_keepalive();    //feed dog
151         wdt_writel(0x0a, RK29_WDT_CR);
152 }
153
154 void rk29_wdt_stop(void)
155 {
156         __rk29_wdt_stop();
157         clk_disable_unprepare(wdt_clock);
158 }
159
160 /* timeout unit second */
161 int rk29_wdt_set_heartbeat(int timeout)
162 {
163         unsigned int count = 0;
164         unsigned int torr = 0, acc = 1, maxtime = 0;    
165         unsigned int freq = clk_get_rate(wdt_clock);
166
167         if (timeout < 1)
168                 return -EINVAL;
169         //0x80000000 is the max count of watch dog
170         maxtime = 0x80000000 / freq + 1;
171         if(timeout > maxtime)
172                 timeout = maxtime;
173                 
174         count = timeout * freq;
175         count /= 0x10000;
176
177         while(acc < count){
178                 acc *= 2;
179                 torr++;
180         }
181         if(torr > 15){
182                 torr = 15;
183         }
184         DBG("%s:torr:%d, count:%d, maxtime:%d s\n", __func__, torr, count, maxtime);
185         wdt_writel(torr, RK29_WDT_TORR);
186         return 0;
187 }
188
189 void rk29_wdt_start(void)
190 {
191         unsigned long wtcon;
192         clk_prepare_enable(wdt_clock);
193         rk29_wdt_set_heartbeat(tmr_margin);
194         wtcon = (RK29_WDT_EN << 0) | (RK29_RESPONSE_MODE << 1) | (RK29_RESET_PULSE << 2);
195         wdt_writel(wtcon, RK29_WDT_CR);
196 }
197
198 /*
199  *      /dev/watchdog handling
200  */
201
202 static int rk29_wdt_open(struct inode *inode, struct file *file)
203 {
204         DBG("%s\n", __func__);
205         if (test_and_set_bit(0, &open_lock))
206                 return -EBUSY;
207
208         if (nowayout)
209                 __module_get(THIS_MODULE);
210
211         expect_close = 0;
212
213         /* start the timer */
214         rk29_wdt_start();
215         return nonseekable_open(inode, file);
216 }
217
218 static int rk29_wdt_release(struct inode *inode, struct file *file)
219 {
220         /*
221          *      Shut off the timer.
222          *      Lock it in if it's a module and we set nowayout
223          */
224         DBG("%s\n", __func__);
225         if (expect_close == 42)
226                 rk29_wdt_stop();
227         else {
228                 dev_err(wdt_dev, "Unexpected close, not stopping watchdog\n");
229                 rk29_wdt_keepalive();
230         }
231         expect_close = 0;
232         clear_bit(0, &open_lock);
233         return 0;
234 }
235
236 static ssize_t rk29_wdt_write(struct file *file, const char __user *data,
237                                 size_t len, loff_t *ppos)
238 {
239         /*
240          *      Refresh the timer.
241          */
242         DBG("%s\n", __func__);
243         if (len) {
244                 if (!nowayout) {
245                         size_t i;
246
247                         /* In case it was set long ago */
248                         expect_close = 0;
249
250                         for (i = 0; i != len; i++) {
251                                 char c;
252
253                                 if (get_user(c, data + i))
254                                         return -EFAULT;
255                                 if (c == 'V')
256                                         expect_close = 42;
257                         }
258                 }
259                 rk29_wdt_keepalive();
260         }
261         return len;
262 }
263
264 #define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
265
266 static const struct watchdog_info rk29_wdt_ident = {
267         .options          =     OPTIONS,
268         .firmware_version =     0,
269         .identity         =     "rk29 Watchdog",
270 };
271
272
273 static long rk29_wdt_ioctl(struct file *file,   unsigned int cmd,
274                                                         unsigned long arg)
275 {
276         void __user *argp = (void __user *)arg;
277         int __user *p = argp;
278         int new_margin;
279         DBG("%s\n", __func__);
280         switch (cmd) {
281         case WDIOC_GETSUPPORT:
282                 return copy_to_user(argp, &rk29_wdt_ident,
283                         sizeof(rk29_wdt_ident)) ? -EFAULT : 0;
284         case WDIOC_GETSTATUS:
285         case WDIOC_GETBOOTSTATUS:
286                 return put_user(0, p);
287         case WDIOC_KEEPALIVE:
288                 DBG("%s:rk29_wdt_keepalive\n", __func__);
289                 rk29_wdt_keepalive();
290                 return 0;
291         case WDIOC_SETTIMEOUT:
292                 if (get_user(new_margin, p))
293                         return -EFAULT;
294                 if (rk29_wdt_set_heartbeat(new_margin))
295                         return -EINVAL;
296                 rk29_wdt_keepalive();
297                 return put_user(tmr_margin, p);
298         case WDIOC_GETTIMEOUT:
299                 return put_user(tmr_margin, p);
300         default:
301                 return -ENOTTY;
302         }
303 }
304
305
306
307 /* kernel interface */
308
309 static const struct file_operations rk29_wdt_fops = {
310         .owner          = THIS_MODULE,
311         .llseek         = no_llseek,
312         .write          = rk29_wdt_write,
313         .unlocked_ioctl = rk29_wdt_ioctl,
314         .open           = rk29_wdt_open,
315         .release        = rk29_wdt_release,
316 };
317
318 static struct miscdevice rk29_wdt_miscdev = {
319         .minor          = WATCHDOG_MINOR,
320         .name           = "watchdog",
321         .fops           = &rk29_wdt_fops,
322 };
323
324
325 /* interrupt handler code */
326
327 static irqreturn_t rk29_wdt_irq_handler(int irqno, void *param)
328 {
329         DBG("RK29_wdt:watchdog timer expired (irq)\n");
330         rk29_wdt_keepalive();
331         return IRQ_HANDLED;
332 }
333
334
335 /* device interface */
336
337 static int rk29_wdt_probe(struct platform_device *pdev)
338 {
339         struct resource *res;
340         struct device *dev;
341         int started = 0;
342         int ret, val;
343
344         dev = &pdev->dev;
345         wdt_dev = &pdev->dev;
346
347         /* get the memory region for the watchdog timer */
348         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
349         if (res == NULL) {
350                 dev_err(dev, "no memory resource specified\n");
351                 return -ENOENT;
352         }
353
354         wdt_base = devm_request_and_ioremap(&pdev->dev, res);
355         if (wdt_base == NULL) {
356                 dev_err(dev, "failed to ioremap() region\n");
357                 return -EINVAL;
358         }
359         
360 #ifdef CONFIG_OF
361         if(!of_property_read_u32(pdev->dev.of_node, "rockchip,atboot", &val))
362                 tmr_atboot = val;
363         else
364                 tmr_atboot = 0;
365
366         if(!of_property_read_u32(pdev->dev.of_node, "rockchip,timeout", &val))
367                 tmr_margin = val;
368         else
369                 tmr_margin = 0;
370
371         if(!of_property_read_u32(pdev->dev.of_node, "rockchip,debug", &val))
372                 debug = val;
373         else
374                 debug = 0;
375         DBG("probe: mapped wdt_base=%p\n", wdt_base);
376
377         of_property_read_u32(pdev->dev.of_node, "rockchip,irq", &val);
378 #endif
379
380 #ifdef CONFIG_RK29_FEED_DOG_BY_INTE
381         val = 1;
382 #endif
383         //printk("atboot:%d, timeout:%ds, debug:%d, irq:%d\n", tmr_atboot, tmr_margin, debug, val);
384         
385         if(val == 1) {
386                 wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
387                 if (wdt_irq == NULL) {
388                         dev_err(dev, "no irq resource specified\n");
389                         return -ENOENT;
390                 }
391
392                 ret = request_irq(wdt_irq->start, rk29_wdt_irq_handler, 0, pdev->name, pdev);
393                 if (ret != 0) {
394                         dev_err(dev, "failed to install irq (%d)\n", ret);
395                         return ret;
396                 }
397         }
398
399         wdt_clock = devm_clk_get(&pdev->dev, "pclk_wdt");
400         if (IS_ERR(wdt_clock)) {
401                 dev_err(dev, "failed to find watchdog clock source\n");
402                 ret = PTR_ERR(wdt_clock);
403                 goto err_irq;
404         }
405
406         ret = misc_register(&rk29_wdt_miscdev);
407         if (ret) {
408                 dev_err(dev, "cannot register miscdev on minor=%d (%d)\n",
409                         WATCHDOG_MINOR, ret);
410                 goto err_irq;
411         }
412         if (tmr_atboot && started == 0) {
413                 dev_info(dev, "starting watchdog timer\n");
414                 rk29_wdt_start();
415         } else if (!tmr_atboot) {
416                 /* if we're not enabling the watchdog, then ensure it is
417                  * disabled if it has been left running from the bootloader
418                  * or other source */
419
420                 rk29_wdt_stop();
421         }
422         return 0;
423
424 err_irq:
425         free_irq(wdt_irq->start, pdev);
426         return ret;
427 }
428
429 static int rk29_wdt_remove(struct platform_device *dev)
430 {
431         wdt_mem = NULL;
432         free_irq(wdt_irq->start, dev);
433         wdt_irq = NULL;
434         clk_disable_unprepare(wdt_clock);
435         wdt_clock = NULL;
436         misc_deregister(&rk29_wdt_miscdev);
437         return 0;
438 }
439
440 static void rk29_wdt_shutdown(struct platform_device *dev)
441 {
442         rk29_wdt_stop();
443 }
444
445 #ifdef CONFIG_PM
446
447 static int rk29_wdt_suspend(struct platform_device *dev, pm_message_t state)
448 {
449         rk29_wdt_stop();
450         return 0;
451 }
452
453 static int rk29_wdt_resume(struct platform_device *dev)
454 {
455         rk29_wdt_start();
456         return 0;
457 }
458
459 #else
460 #define rk29_wdt_suspend NULL
461 #define rk29_wdt_resume  NULL
462 #endif /* CONFIG_PM */
463
464 #ifdef CONFIG_OF
465 static const struct of_device_id of_rk29_wdt_match[] = {
466         { .compatible = "rockchip,watch dog" },
467         { /* Sentinel */ }
468 };
469 #endif
470 static struct platform_driver rk29_wdt_driver = {
471         .probe          = rk29_wdt_probe,
472         .remove         = rk29_wdt_remove,
473         .shutdown       = rk29_wdt_shutdown,
474         .suspend        = rk29_wdt_suspend,
475         .resume         = rk29_wdt_resume,
476         .driver         = {
477                 .owner  = THIS_MODULE,
478 #ifdef CONFIG_OF
479                 .of_match_table = of_rk29_wdt_match,
480 #endif
481                 .name   = "rk29-wdt",
482         },
483 };
484
485
486 static char banner[] __initdata =
487         KERN_INFO "RK29 Watchdog Timer, (c) 2011 Rockchip Electronics\n";
488
489 static int __init watchdog_init(void)
490 {
491         printk(banner);
492         return platform_driver_register(&rk29_wdt_driver);
493 }
494
495 static void __exit watchdog_exit(void)
496 {
497         platform_driver_unregister(&rk29_wdt_driver);
498 }
499
500 subsys_initcall(watchdog_init);
501 module_exit(watchdog_exit);
502
503 MODULE_AUTHOR("hhb@rock-chips.com");
504 MODULE_DESCRIPTION("RK29 Watchdog Device Driver");
505 MODULE_LICENSE("GPL");
506 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
507 MODULE_ALIAS("platform:rk29-wdt");