Update CHANGELOG
[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 #define POST_MAX_NUMBER         32
36
37 #define BOOTMODE_MAGIC  0xDEAD0000
38
39 int post_init_f(void)
40 {
41         DECLARE_GLOBAL_DATA_PTR;
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         DECLARE_GLOBAL_DATA_PTR;
66         int bootmode = post_bootmode_get(0);
67         int newword;
68
69         if (post_hotkeys_pressed() && !(bootmode & POST_POWERTEST)) {
70                 newword = BOOTMODE_MAGIC | POST_SLOWTEST;
71         } else if (bootmode == 0) {
72                 newword = BOOTMODE_MAGIC | POST_POWERON;
73         } else if (bootmode == POST_POWERON || bootmode == POST_SLOWTEST) {
74                 newword = BOOTMODE_MAGIC | POST_NORMAL;
75         } else {
76                 /* Use old value */
77                 newword = post_word_load() & ~POST_COLDBOOT;
78         }
79
80         if (bootmode == 0) {
81                 /* We are booting after power-on */
82                 newword |= POST_COLDBOOT;
83         }
84
85         post_word_store(newword);
86
87         /* Reset activity record */
88         gd->post_log_word = 0;
89 }
90
91 int post_bootmode_get(unsigned int *last_test)
92 {
93         unsigned long word = post_word_load();
94         int bootmode;
95
96         if ((word & 0xFFFF0000) != BOOTMODE_MAGIC) {
97                 return 0;
98         }
99
100         bootmode = word & 0x7F;
101
102         if (last_test && (bootmode & POST_POWERTEST)) {
103                 *last_test = (word >> 8) & 0xFF;
104         }
105
106         return bootmode;
107 }
108
109 /* POST tests run before relocation only mark status bits .... */
110 static void post_log_mark_start(unsigned long testid)
111 {
112         DECLARE_GLOBAL_DATA_PTR;
113         gd->post_log_word |= (testid) << 16;
114 }
115
116 static void post_log_mark_succ(unsigned long testid)
117 {
118         DECLARE_GLOBAL_DATA_PTR;
119         gd->post_log_word |= testid;
120 }
121
122 /* ... and the messages are output once we are relocated */
123 void post_output_backlog(void)
124 {
125         DECLARE_GLOBAL_DATA_PTR;
126         int j;
127
128         for (j = 0; j < post_list_size; j++) {
129                 if (gd->post_log_word & (post_list[j].testid << 16)) {
130                         post_log("POST %s ", post_list[j].cmd);
131                         if (gd->post_log_word & post_list[j].testid)
132                                 post_log("PASSED\n");
133                         else {
134                                 post_log("FAILED\n");
135 #ifdef CONFIG_SHOW_BOOT_PROGRESS
136                                 show_boot_progress(-31);
137 #endif
138                         }
139                 }
140         }
141 }
142
143 static void post_bootmode_test_on(unsigned int last_test)
144 {
145         unsigned long word = post_word_load();
146
147         word |= POST_POWERTEST;
148
149         word |= (last_test & 0xFF) << 8;
150
151         post_word_store(word);
152 }
153
154 static void post_bootmode_test_off(void)
155 {
156         unsigned long word = post_word_load();
157
158         word &= ~POST_POWERTEST;
159
160         post_word_store(word);
161 }
162
163 static void post_get_flags(int *test_flags)
164 {
165         int flag[] = { POST_POWERON, POST_NORMAL, POST_SLOWTEST };
166         char *var[] = { "post_poweron", "post_normal", "post_slowtest" };
167         int varnum = sizeof(var) / sizeof(var[0]);
168         char list[128];         /* long enough for POST list */
169         char *name;
170         char *s;
171         int last;
172         int i, j;
173
174         for (j = 0; j < post_list_size; j++) {
175                 test_flags[j] = post_list[j].flags;
176         }
177
178         for (i = 0; i < varnum; i++) {
179                 if (getenv_r(var[i], list, sizeof(list)) <= 0)
180                         continue;
181
182                 for (j = 0; j < post_list_size; j++) {
183                         test_flags[j] &= ~flag[i];
184                 }
185
186                 last = 0;
187                 name = list;
188                 while (!last) {
189                         while (*name && *name == ' ')
190                                 name++;
191                         if (*name == 0)
192                                 break;
193                         s = name + 1;
194                         while (*s && *s != ' ')
195                                 s++;
196                         if (*s == 0)
197                                 last = 1;
198                         else
199                                 *s = 0;
200
201                         for (j = 0; j < post_list_size; j++) {
202                                 if (strcmp(post_list[j].cmd, name) == 0) {
203                                         test_flags[j] |= flag[i];
204                                         break;
205                                 }
206                         }
207
208                         if (j == post_list_size) {
209                                 printf("No such test: %s\n", name);
210                         }
211
212                         name = s + 1;
213                 }
214         }
215
216         for (j = 0; j < post_list_size; j++) {
217                 if (test_flags[j] & POST_POWERON) {
218                         test_flags[j] |= POST_SLOWTEST;
219                 }
220         }
221 }
222
223 static int post_run_single(struct post_test *test,
224                            int test_flags, int flags, unsigned int i)
225 {
226         if ((flags & test_flags & POST_ALWAYS) &&
227             (flags & test_flags & POST_MEM)) {
228                 WATCHDOG_RESET();
229
230                 if (!(flags & POST_REBOOT)) {
231                         if ((test_flags & POST_REBOOT)
232                             && !(flags & POST_MANUAL)) {
233                                 post_bootmode_test_on(i);
234                         }
235
236                         if (test_flags & POST_PREREL)
237                                 post_log_mark_start(test->testid);
238                         else
239                                 post_log("POST %s ", test->cmd);
240                 }
241
242                 if (test_flags & POST_PREREL) {
243                         if ((*test->test) (flags) == 0)
244                                 post_log_mark_succ(test->testid);
245                 } else {
246                         if ((*test->test) (flags) != 0) {
247                                 post_log("FAILED\n");
248 #ifdef CONFIG_SHOW_BOOT_PROGRESS
249                                 show_boot_progress(-32);
250 #endif
251                         } else
252                                 post_log("PASSED\n");
253                 }
254
255                 if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
256                         post_bootmode_test_off();
257                 }
258
259                 return 0;
260         } else {
261                 return -1;
262         }
263 }
264
265 int post_run(char *name, int flags)
266 {
267         unsigned int i;
268         int test_flags[POST_MAX_NUMBER];
269
270         post_get_flags(test_flags);
271
272         if (name == NULL) {
273                 unsigned int last;
274
275                 if (post_bootmode_get(&last) & POST_POWERTEST) {
276                         if (last < post_list_size &&
277                             (flags & test_flags[last] & POST_ALWAYS) &&
278                             (flags & test_flags[last] & POST_MEM)) {
279
280                                 post_run_single(post_list + last,
281                                                 test_flags[last],
282                                                 flags | POST_REBOOT, last);
283
284                                 for (i = last + 1; i < post_list_size; i++) {
285                                         post_run_single(post_list + i,
286                                                         test_flags[i],
287                                                         flags, i);
288                                 }
289                         }
290                 } else {
291                         for (i = 0; i < post_list_size; i++) {
292                                 post_run_single(post_list + i,
293                                                 test_flags[i], flags, i);
294                         }
295                 }
296
297                 return 0;
298         } else {
299                 for (i = 0; i < post_list_size; i++) {
300                         if (strcmp(post_list[i].cmd, name) == 0)
301                                 break;
302                 }
303
304                 if (i < post_list_size) {
305                         return post_run_single(post_list + i,
306                                                test_flags[i], flags, i);
307                 } else {
308                         return -1;
309                 }
310         }
311 }
312
313 static int post_info_single(struct post_test *test, int full)
314 {
315         if (test->flags & POST_MANUAL) {
316                 if (full)
317                         printf("%s - %s\n"
318                                "  %s\n", test->cmd, test->name, test->desc);
319                 else
320                         printf("  %-15s - %s\n", test->cmd, test->name);
321
322                 return 0;
323         } else {
324                 return -1;
325         }
326 }
327
328 int post_info(char *name)
329 {
330         unsigned int i;
331
332         if (name == NULL) {
333                 for (i = 0; i < post_list_size; i++) {
334                         post_info_single(post_list + i, 0);
335                 }
336
337                 return 0;
338         } else {
339                 for (i = 0; i < post_list_size; i++) {
340                         if (strcmp(post_list[i].cmd, name) == 0)
341                                 break;
342                 }
343
344                 if (i < post_list_size) {
345                         return post_info_single(post_list + i, 1);
346                 } else {
347                         return -1;
348                 }
349         }
350 }
351
352 int post_log(char *format, ...)
353 {
354         va_list args;
355         uint i;
356         char printbuffer[CFG_PBSIZE];
357
358         va_start(args, format);
359
360         /* For this to work, printbuffer must be larger than
361          * anything we ever want to print.
362          */
363         i = vsprintf(printbuffer, format, args);
364         va_end(args);
365
366 #ifdef CONFIG_LOGBUFFER
367         /* Send to the logbuffer */
368         logbuff_log(printbuffer);
369 #else
370         /* Send to the stdout file */
371         puts(printbuffer);
372 #endif
373
374         return 0;
375 }
376
377 void post_reloc(void)
378 {
379         DECLARE_GLOBAL_DATA_PTR;
380
381         unsigned int i;
382
383         /*
384          * We have to relocate the test table manually
385          */
386         for (i = 0; i < post_list_size; i++) {
387                 ulong addr;
388                 struct post_test *test = post_list + i;
389
390                 if (test->name) {
391                         addr = (ulong) (test->name) + gd->reloc_off;
392                         test->name = (char *)addr;
393                 }
394
395                 if (test->cmd) {
396                         addr = (ulong) (test->cmd) + gd->reloc_off;
397                         test->cmd = (char *)addr;
398                 }
399
400                 if (test->desc) {
401                         addr = (ulong) (test->desc) + gd->reloc_off;
402                         test->desc = (char *)addr;
403                 }
404
405                 if (test->test) {
406                         addr = (ulong) (test->test) + gd->reloc_off;
407                         test->test = (int (*)(int flags))addr;
408                 }
409
410                 if (test->init_f) {
411                         addr = (ulong) (test->init_f) + gd->reloc_off;
412                         test->init_f = (int (*)(void))addr;
413                 }
414
415                 if (test->reloc) {
416                         addr = (ulong) (test->reloc) + gd->reloc_off;
417                         test->reloc = (void (*)(void))addr;
418
419                         test->reloc();
420                 }
421         }
422 }
423
424 /*
425  * Some tests (e.g. SYSMON) need the time when post_init_f started,
426  * but we cannot use get_timer() at this point.
427  *
428  * On PowerPC we implement it using the timebase register.
429  */
430 unsigned long post_time_ms(unsigned long base)
431 {
432         return (unsigned long)get_ticks() / (get_tbclk() / CFG_HZ) - base;
433 }
434
435 #endif                          /* CONFIG_POST */