Merge branch 'master' of git://www.denx.de/git/u-boot-nand-flash
[platform/kernel/u-boot.git] / 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  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22  * MA 02111-1307 USA
23  */
24
25 #include <common.h>
26
27 /*
28  * This test verifies if the reason of last reset was an abnormal voltage
29  * condition, than it performs watchdog test, measuing time required to
30  * trigger watchdog reset.
31  */
32
33 #ifdef CONFIG_POST
34
35 #include <post.h>
36
37 #if CONFIG_POST & CFG_POST_WATCHDOG
38
39 #include <watchdog.h>
40 #include <asm/gpio.h>
41 #include <asm/io.h>
42
43 static uint watchdog_magic_read(void)
44 {
45         return in_be32((void *)CFG_WATCHDOG_FLAGS_ADDR) &
46                 CFG_WATCHDOG_MAGIC_MASK;
47 }
48
49 static void watchdog_magic_write(uint value)
50 {
51         out_be32((void *)CFG_WATCHDOG_FLAGS_ADDR, value |
52                 (in_be32((void *)CFG_WATCHDOG_FLAGS_ADDR) &
53                         ~CFG_WATCHDOG_MAGIC_MASK));
54 }
55
56 int sysmon1_post_test(int flags)
57 {
58         if (gpio_read_in_bit(CFG_GPIO_SYSMON_STATUS) == 0) {
59                 /*
60                  * 3.1. GPIO62 is low
61                  * Assuming system voltage failure.
62                  */
63                 post_log("Abnormal voltage detected (GPIO62)\n");
64                 return 1;
65         }
66
67         return 0;
68 }
69
70 int lwmon5_watchdog_post_test(int flags)
71 {
72         ulong time;
73
74         /* On each reset scratch register 1 should be tested,
75          * but first test GPIO62:
76          */
77         if (!(flags & POST_MANUAL) && sysmon1_post_test(flags)) {
78                 /*
79                  * 3.1. GPIO62 is low
80                  * Assuming system voltage failure.
81                  */
82                 /* 3.1.1. Set scratch register 1 to 0x0000xxxx */
83                 watchdog_magic_write(0);
84                 /* 3.1.2. Mark test as failed due to voltage?! */
85                 return 1;
86         }
87
88         if (watchdog_magic_read() != CFG_WATCHDOG_MAGIC) {
89                 /*
90                  * 3.2. Scratch register 1 differs from magic value 0x1248xxxx
91                  * Assuming PowerOn
92                  */
93                 int ints;
94                 ulong base;
95
96                 /* 3.2.1. Set magic value to scratch register */
97                 watchdog_magic_write(CFG_WATCHDOG_MAGIC);
98
99                 ints = disable_interrupts ();
100                 /* 3.2.2. strobe watchdog once */
101                 WATCHDOG_RESET();
102                 out_be32((void *)CFG_WATCHDOG_TIME_ADDR, 0);
103                 /* 3.2.3. save time of strobe in scratch register 2 */
104                 base = post_time_ms (0);
105
106                 /* 3.2.4. Wait for 150 ms (enough for reset to happen) */
107                 while ((time = post_time_ms (base)) < 150)
108                         out_be32((void *)CFG_WATCHDOG_TIME_ADDR, time);
109                 if (ints)
110                         enable_interrupts ();
111
112                 /*
113                  * 3.2.5. Reset didn't happen. - Set 0x0000xxxx
114                  * into scratch register 1
115                  */
116                 watchdog_magic_write(0);
117                 /* 3.2.6. Mark test as failed. */
118                 post_log("hw watchdog time : %u ms, failed ", time);
119                 return 2;
120         }
121
122         /*
123          * 3.3. Scratch register matches magic value 0x1248xxxx
124          * Assume this is watchdog-initiated reset
125          */
126         /* 3.3.1. So, the test succeed, save measured time to syslog. */
127         time = in_be32((void *)CFG_WATCHDOG_TIME_ADDR);
128         post_log("hw watchdog time : %u ms, passed ", time);
129         /* 3.3.2. Set scratch register 1 to 0x0000xxxx */
130         watchdog_magic_write(0);
131
132         return 0;
133 }
134
135 #endif /* CONFIG_POST & CFG_POST_WATCHDOG */
136 #endif /* CONFIG_POST */