Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / u-boot / post / board / lwmon5 / watchdog.c
1 /*
2  * (C) Copyright 2008 Dmitry Rakhchev, EmCraft Systems, rda@emcraft.com
3  *
4  * Developed for DENX Software Engineering GmbH
5  *
6  * SPDX-License-Identifier:     GPL-2.0+
7  */
8
9 #include <common.h>
10
11 /* This test verifies if the reason of last reset was an abnormal voltage
12  * condition, than it performs watchdog test, measuing time required to
13  * trigger watchdog reset.
14  */
15
16 #include <post.h>
17
18 #if CONFIG_POST & CONFIG_SYS_POST_WATCHDOG
19
20 #include <watchdog.h>
21 #include <asm/ppc4xx-gpio.h>
22 #include <asm/io.h>
23
24 static uint watchdog_magic_read(void)
25 {
26         return in_be32((void *)CONFIG_SYS_WATCHDOG_FLAGS_ADDR) &
27                 CONFIG_SYS_WATCHDOG_MAGIC_MASK;
28 }
29
30 static void watchdog_magic_write(uint value)
31 {
32         out_be32((void *)CONFIG_SYS_WATCHDOG_FLAGS_ADDR, value |
33                 (in_be32((void *)CONFIG_SYS_WATCHDOG_FLAGS_ADDR) &
34                         ~CONFIG_SYS_WATCHDOG_MAGIC_MASK));
35 }
36
37 int sysmon1_post_test(int flags)
38 {
39         if (gpio_read_in_bit(CONFIG_SYS_GPIO_SYSMON_STATUS) == 0) {
40                 /*
41                  * 3.1. GPIO62 is low
42                  * Assuming system voltage failure.
43                  */
44                 post_log("sysmon1 Abnormal voltage detected (GPIO62)\n");
45                 post_log("POST sysmon1 FAILED\n");
46                 return 1;
47         } else {
48                 post_log("sysmon1 PASSED\n");
49         }
50
51         return 0;
52 }
53
54 int lwmon5_watchdog_post_test(int flags)
55 {
56         /* On each reset scratch register 1 should be tested,
57          * but first test GPIO62:
58          */
59         if (!(flags & POST_MANUAL) && sysmon1_post_test(flags)) {
60                 /* 3.1. GPIO62 is low
61                  * Assuming system voltage failure.
62                  */
63                 /* 3.1.1. Set scratch register 1 to 0x0000xxxx */
64                 watchdog_magic_write(0);
65                 /* 3.1.2. Mark test as failed due to voltage?! */
66                 return 1;
67         }
68
69         if (watchdog_magic_read() != CONFIG_SYS_WATCHDOG_MAGIC) {
70                 /* 3.2. Scratch register 1 differs from magic value 0x1248xxxx
71                  * Assuming PowerOn
72                  */
73                 int ints;
74                 ulong base;
75                 ulong time;
76
77                 /* 3.2.1. Set magic value to scratch register */
78                 watchdog_magic_write(CONFIG_SYS_WATCHDOG_MAGIC);
79
80                 ints = disable_interrupts ();
81                 /* 3.2.2. strobe watchdog once */
82                 WATCHDOG_RESET();
83                 out_be32((void *)CONFIG_SYS_WATCHDOG_TIME_ADDR, 0);
84                 /* 3.2.3. save time of strobe in scratch register 2 */
85                 base = post_time_ms (0);
86
87                 /* 3.2.4. Wait for 150 ms (enough for reset to happen) */
88                 while ((time = post_time_ms (base)) < 150)
89                         out_be32((void *)CONFIG_SYS_WATCHDOG_TIME_ADDR, time);
90                 if (ints)
91                         enable_interrupts ();
92
93                 /* 3.2.5. Reset didn't happen. - Set 0x0000xxxx
94                  * into scratch register 1
95                  */
96                 watchdog_magic_write(0);
97                 /* 3.2.6. Mark test as failed. */
98                 post_log("hw watchdog time : %u ms, failed ", time);
99                 return 2;
100         } else {
101                 /* 3.3. Scratch register matches magic value 0x1248xxxx
102                  * Assume this is watchdog-initiated reset
103                  */
104                 ulong time;
105                 /* 3.3.1. So, the test succeed, save measured time to syslog. */
106                 time = in_be32((void *)CONFIG_SYS_WATCHDOG_TIME_ADDR);
107                 if (time > 90 ) { /* ms*/
108                         post_log("hw watchdog time : %u ms, passed ", time);
109                         /* 3.3.2. Set scratch register 1 to 0x0000xxxx */
110                         watchdog_magic_write(0);
111                         return 0;
112                 } else {
113                         /*test minimum watchdogtime */
114                         post_log("hw watchdog time : %u ms, failed ", time);
115                         return 2;
116                 }
117         }
118         return -1;
119 }
120
121 #endif /* CONFIG_POST & CONFIG_SYS_POST_WATCHDOG */