Merge branch 'tqm5200_default_env' of /home/tur/git/u-boot
[platform/kernel/u-boot.git] / lib_blackfin / post.c
1 /*
2  * (C) Copyright 2002
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 #include <common.h>
25 #include <console.h>
26 #include <watchdog.h>
27 #include <post.h>
28
29 #ifdef CONFIG_LOGBUFFER
30 #include <logbuff.h>
31 #endif
32
33 #ifdef CONFIG_POST
34
35 DECLARE_GLOBAL_DATA_PTR;
36
37 #define POST_MAX_NUMBER         32
38
39 #define BOOTMODE_MAGIC  0xDEAD0000
40
41 int post_init_f(void)
42 {
43         int res = 0;
44         unsigned int i;
45
46         for (i = 0; i < post_list_size; i++) {
47                 struct post_test *test = post_list + i;
48
49                 if (test->init_f && test->init_f()) {
50                         res = -1;
51                 }
52         }
53
54         gd->post_init_f_time = post_time_ms(0);
55         if (!gd->post_init_f_time) {
56                 printf
57                     ("post/post.c: post_time_ms seems not to be implemented\n");
58         }
59
60         return res;
61 }
62
63 void post_bootmode_init(void)
64 {
65         int bootmode = post_bootmode_get(0);
66         int newword;
67
68         if (post_hotkeys_pressed() && !(bootmode & POST_POWERTEST)) {
69                 newword = BOOTMODE_MAGIC | POST_SLOWTEST;
70         } else if (bootmode == 0) {
71                 newword = BOOTMODE_MAGIC | POST_POWERON;
72         } else if (bootmode == POST_POWERON || bootmode == POST_SLOWTEST) {
73                 newword = BOOTMODE_MAGIC | POST_NORMAL;
74         } else {
75                 /* Use old value */
76                 newword = post_word_load() & ~POST_COLDBOOT;
77         }
78
79         if (bootmode == 0) {
80                 /* We are booting after power-on */
81                 newword |= POST_COLDBOOT;
82         }
83
84         post_word_store(newword);
85
86         /* Reset activity record */
87         gd->post_log_word = 0;
88 }
89
90 int post_bootmode_get(unsigned int *last_test)
91 {
92         unsigned long word = post_word_load();
93         int bootmode;
94
95         if ((word & 0xFFFF0000) != BOOTMODE_MAGIC) {
96                 return 0;
97         }
98
99         bootmode = word & 0x7F;
100
101         if (last_test && (bootmode & POST_POWERTEST)) {
102                 *last_test = (word >> 8) & 0xFF;
103         }
104
105         return bootmode;
106 }
107
108 /* POST tests run before relocation only mark status bits .... */
109 static void post_log_mark_start(unsigned long testid)
110 {
111         gd->post_log_word |= (testid) << 16;
112 }
113
114 static void post_log_mark_succ(unsigned long testid)
115 {
116         gd->post_log_word |= testid;
117 }
118
119 /* ... and the messages are output once we are relocated */
120 void post_output_backlog(void)
121 {
122         int j;
123
124         for (j = 0; j < post_list_size; j++) {
125                 if (gd->post_log_word & (post_list[j].testid << 16)) {
126                         post_log("POST %s ", post_list[j].cmd);
127                         if (gd->post_log_word & post_list[j].testid)
128                                 post_log("PASSED\n");
129                         else {
130                                 post_log("FAILED\n");
131                                 show_boot_progress (-31);
132                         }
133                 }
134         }
135 }
136
137 static void post_bootmode_test_on(unsigned int last_test)
138 {
139         unsigned long word = post_word_load();
140
141         word |= POST_POWERTEST;
142
143         word |= (last_test & 0xFF) << 8;
144
145         post_word_store(word);
146 }
147
148 static void post_bootmode_test_off(void)
149 {
150         unsigned long word = post_word_load();
151
152         word &= ~POST_POWERTEST;
153
154         post_word_store(word);
155 }
156
157 static void post_get_flags(int *test_flags)
158 {
159         int flag[] = { POST_POWERON, POST_NORMAL, POST_SLOWTEST };
160         char *var[] = { "post_poweron", "post_normal", "post_slowtest" };
161         int varnum = sizeof(var) / sizeof(var[0]);
162         char list[128];         /* long enough for POST list */
163         char *name;
164         char *s;
165         int last;
166         int i, j;
167
168         for (j = 0; j < post_list_size; j++) {
169                 test_flags[j] = post_list[j].flags;
170         }
171
172         for (i = 0; i < varnum; i++) {
173                 if (getenv_r(var[i], list, sizeof(list)) <= 0)
174                         continue;
175
176                 for (j = 0; j < post_list_size; j++) {
177                         test_flags[j] &= ~flag[i];
178                 }
179
180                 last = 0;
181                 name = list;
182                 while (!last) {
183                         while (*name && *name == ' ')
184                                 name++;
185                         if (*name == 0)
186                                 break;
187                         s = name + 1;
188                         while (*s && *s != ' ')
189                                 s++;
190                         if (*s == 0)
191                                 last = 1;
192                         else
193                                 *s = 0;
194
195                         for (j = 0; j < post_list_size; j++) {
196                                 if (strcmp(post_list[j].cmd, name) == 0) {
197                                         test_flags[j] |= flag[i];
198                                         break;
199                                 }
200                         }
201
202                         if (j == post_list_size) {
203                                 printf("No such test: %s\n", name);
204                         }
205
206                         name = s + 1;
207                 }
208         }
209
210         for (j = 0; j < post_list_size; j++) {
211                 if (test_flags[j] & POST_POWERON) {
212                         test_flags[j] |= POST_SLOWTEST;
213                 }
214         }
215 }
216
217 static int post_run_single(struct post_test *test,
218                            int test_flags, int flags, unsigned int i)
219 {
220         if ((flags & test_flags & POST_ALWAYS) &&
221             (flags & test_flags & POST_MEM)) {
222                 WATCHDOG_RESET();
223
224                 if (!(flags & POST_REBOOT)) {
225                         if ((test_flags & POST_REBOOT)
226                             && !(flags & POST_MANUAL)) {
227                                 post_bootmode_test_on(i);
228                         }
229
230                         if (test_flags & POST_PREREL)
231                                 post_log_mark_start(test->testid);
232                         else
233                                 post_log("POST %s ", test->cmd);
234                 }
235
236                 if (test_flags & POST_PREREL) {
237                         if ((*test->test) (flags) == 0)
238                                 post_log_mark_succ(test->testid);
239                 } else {
240                         if ((*test->test) (flags) != 0) {
241                                 post_log("FAILED\n");
242                                 show_boot_progress (-32);
243                         } else
244                                 post_log("PASSED\n");
245                 }
246
247                 if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
248                         post_bootmode_test_off();
249                 }
250
251                 return 0;
252         } else {
253                 return -1;
254         }
255 }
256
257 int post_run(char *name, int flags)
258 {
259         unsigned int i;
260         int test_flags[POST_MAX_NUMBER];
261
262         post_get_flags(test_flags);
263
264         if (name == NULL) {
265                 unsigned int last;
266
267                 if (post_bootmode_get(&last) & POST_POWERTEST) {
268                         if (last < post_list_size &&
269                             (flags & test_flags[last] & POST_ALWAYS) &&
270                             (flags & test_flags[last] & POST_MEM)) {
271
272                                 post_run_single(post_list + last,
273                                                 test_flags[last],
274                                                 flags | POST_REBOOT, last);
275
276                                 for (i = last + 1; i < post_list_size; i++) {
277                                         post_run_single(post_list + i,
278                                                         test_flags[i],
279                                                         flags, i);
280                                 }
281                         }
282                 } else {
283                         for (i = 0; i < post_list_size; i++) {
284                                 post_run_single(post_list + i,
285                                                 test_flags[i], flags, i);
286                         }
287                 }
288
289                 return 0;
290         } else {
291                 for (i = 0; i < post_list_size; i++) {
292                         if (strcmp(post_list[i].cmd, name) == 0)
293                                 break;
294                 }
295
296                 if (i < post_list_size) {
297                         return post_run_single(post_list + i,
298                                                test_flags[i], flags, i);
299                 } else {
300                         return -1;
301                 }
302         }
303 }
304
305 static int post_info_single(struct post_test *test, int full)
306 {
307         if (test->flags & POST_MANUAL) {
308                 if (full)
309                         printf("%s - %s\n"
310                                "  %s\n", test->cmd, test->name, test->desc);
311                 else
312                         printf("  %-15s - %s\n", test->cmd, test->name);
313
314                 return 0;
315         } else {
316                 return -1;
317         }
318 }
319
320 int post_info(char *name)
321 {
322         unsigned int i;
323
324         if (name == NULL) {
325                 for (i = 0; i < post_list_size; i++) {
326                         post_info_single(post_list + i, 0);
327                 }
328
329                 return 0;
330         } else {
331                 for (i = 0; i < post_list_size; i++) {
332                         if (strcmp(post_list[i].cmd, name) == 0)
333                                 break;
334                 }
335
336                 if (i < post_list_size) {
337                         return post_info_single(post_list + i, 1);
338                 } else {
339                         return -1;
340                 }
341         }
342 }
343
344 int post_log(char *format, ...)
345 {
346         va_list args;
347         uint i;
348         char printbuffer[CFG_PBSIZE];
349
350         va_start(args, format);
351
352         /* For this to work, printbuffer must be larger than
353          * anything we ever want to print.
354          */
355         i = vsprintf(printbuffer, format, args);
356         va_end(args);
357
358 #ifdef CONFIG_LOGBUFFER
359         /* Send to the logbuffer */
360         logbuff_log(printbuffer);
361 #else
362         /* Send to the stdout file */
363         puts(printbuffer);
364 #endif
365
366         return 0;
367 }
368
369 void post_reloc(void)
370 {
371         unsigned int i;
372
373         /*
374          * We have to relocate the test table manually
375          */
376         for (i = 0; i < post_list_size; i++) {
377                 ulong addr;
378                 struct post_test *test = post_list + i;
379
380                 if (test->name) {
381                         addr = (ulong) (test->name) + gd->reloc_off;
382                         test->name = (char *)addr;
383                 }
384
385                 if (test->cmd) {
386                         addr = (ulong) (test->cmd) + gd->reloc_off;
387                         test->cmd = (char *)addr;
388                 }
389
390                 if (test->desc) {
391                         addr = (ulong) (test->desc) + gd->reloc_off;
392                         test->desc = (char *)addr;
393                 }
394
395                 if (test->test) {
396                         addr = (ulong) (test->test) + gd->reloc_off;
397                         test->test = (int (*)(int flags))addr;
398                 }
399
400                 if (test->init_f) {
401                         addr = (ulong) (test->init_f) + gd->reloc_off;
402                         test->init_f = (int (*)(void))addr;
403                 }
404
405                 if (test->reloc) {
406                         addr = (ulong) (test->reloc) + gd->reloc_off;
407                         test->reloc = (void (*)(void))addr;
408
409                         test->reloc();
410                 }
411         }
412 }
413
414 /*
415  * Some tests (e.g. SYSMON) need the time when post_init_f started,
416  * but we cannot use get_timer() at this point.
417  *
418  * On PowerPC we implement it using the timebase register.
419  */
420 unsigned long post_time_ms(unsigned long base)
421 {
422         return (unsigned long)get_ticks() / (get_tbclk() / CFG_HZ) - base;
423 }
424
425 #endif                          /* CONFIG_POST */