blob: 44f048832343bde96114620b6c08cd2d9dabf383 [file] [log] [blame]
Yuri Tikhonov8f15d4a2008-02-04 14:10:42 +01001/*
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
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +010027/* This test verifies if the reason of last reset was an abnormal voltage
Yuri Tikhonov8f15d4a2008-02-04 14:10:42 +010028 * condition, than it performs watchdog test, measuing time required to
29 * trigger watchdog reset.
30 */
31
Yuri Tikhonov8f15d4a2008-02-04 14:10:42 +010032#include <post.h>
33
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020034#if CONFIG_POST & CONFIG_SYS_POST_WATCHDOG
Yuri Tikhonov8f15d4a2008-02-04 14:10:42 +010035
36#include <watchdog.h>
37#include <asm/gpio.h>
38#include <asm/io.h>
39
40static uint watchdog_magic_read(void)
41{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020042 return in_be32((void *)CONFIG_SYS_WATCHDOG_FLAGS_ADDR) &
43 CONFIG_SYS_WATCHDOG_MAGIC_MASK;
Yuri Tikhonov8f15d4a2008-02-04 14:10:42 +010044}
45
46static void watchdog_magic_write(uint value)
47{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020048 out_be32((void *)CONFIG_SYS_WATCHDOG_FLAGS_ADDR, value |
49 (in_be32((void *)CONFIG_SYS_WATCHDOG_FLAGS_ADDR) &
50 ~CONFIG_SYS_WATCHDOG_MAGIC_MASK));
Yuri Tikhonov8f15d4a2008-02-04 14:10:42 +010051}
52
53int sysmon1_post_test(int flags)
54{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020055 if (gpio_read_in_bit(CONFIG_SYS_GPIO_SYSMON_STATUS) == 0) {
Sascha Laue3e4615a2008-04-01 15:13:03 +020056 /*
57 * 3.1. GPIO62 is low
Yuri Tikhonov8f15d4a2008-02-04 14:10:42 +010058 * Assuming system voltage failure.
59 */
60 post_log("Abnormal voltage detected (GPIO62)\n");
61 return 1;
62 }
63
64 return 0;
65}
66
67int lwmon5_watchdog_post_test(int flags)
68{
69 /* On each reset scratch register 1 should be tested,
70 * but first test GPIO62:
71 */
72 if (!(flags & POST_MANUAL) && sysmon1_post_test(flags)) {
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +010073 /* 3.1. GPIO62 is low
Yuri Tikhonov8f15d4a2008-02-04 14:10:42 +010074 * Assuming system voltage failure.
75 */
76 /* 3.1.1. Set scratch register 1 to 0x0000xxxx */
77 watchdog_magic_write(0);
78 /* 3.1.2. Mark test as failed due to voltage?! */
79 return 1;
80 }
81
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020082 if (watchdog_magic_read() != CONFIG_SYS_WATCHDOG_MAGIC) {
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +010083 /* 3.2. Scratch register 1 differs from magic value 0x1248xxxx
Yuri Tikhonov8f15d4a2008-02-04 14:10:42 +010084 * Assuming PowerOn
85 */
86 int ints;
87 ulong base;
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +010088 ulong time;
Yuri Tikhonov8f15d4a2008-02-04 14:10:42 +010089
90 /* 3.2.1. Set magic value to scratch register */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020091 watchdog_magic_write(CONFIG_SYS_WATCHDOG_MAGIC);
Yuri Tikhonov8f15d4a2008-02-04 14:10:42 +010092
93 ints = disable_interrupts ();
94 /* 3.2.2. strobe watchdog once */
95 WATCHDOG_RESET();
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020096 out_be32((void *)CONFIG_SYS_WATCHDOG_TIME_ADDR, 0);
Yuri Tikhonov8f15d4a2008-02-04 14:10:42 +010097 /* 3.2.3. save time of strobe in scratch register 2 */
98 base = post_time_ms (0);
99
100 /* 3.2.4. Wait for 150 ms (enough for reset to happen) */
101 while ((time = post_time_ms (base)) < 150)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200102 out_be32((void *)CONFIG_SYS_WATCHDOG_TIME_ADDR, time);
Yuri Tikhonov8f15d4a2008-02-04 14:10:42 +0100103 if (ints)
104 enable_interrupts ();
105
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100106 /* 3.2.5. Reset didn't happen. - Set 0x0000xxxx
Yuri Tikhonov8f15d4a2008-02-04 14:10:42 +0100107 * into scratch register 1
108 */
109 watchdog_magic_write(0);
110 /* 3.2.6. Mark test as failed. */
111 post_log("hw watchdog time : %u ms, failed ", time);
112 return 2;
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100113 } else {
114 /* 3.3. Scratch register matches magic value 0x1248xxxx
115 * Assume this is watchdog-initiated reset
116 */
117 ulong time;
118 /* 3.3.1. So, the test succeed, save measured time to syslog. */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200119 time = in_be32((void *)CONFIG_SYS_WATCHDOG_TIME_ADDR);
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100120 post_log("hw watchdog time : %u ms, passed ", time);
121 /* 3.3.2. Set scratch register 1 to 0x0000xxxx */
122 watchdog_magic_write(0);
123 return 0;
Yuri Tikhonov8f15d4a2008-02-04 14:10:42 +0100124 }
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100125 return -1;
Wolfgang Denk81a0ac62008-03-20 22:01:38 +0100126}
Yuri Tikhonov8f15d4a2008-02-04 14:10:42 +0100127
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200128#endif /* CONFIG_POST & CONFIG_SYS_POST_WATCHDOG */