]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/lwmon/lwmon.c
3ec9fa551133b08ae5332868a5452e382ccd1c3e
[karo-tx-uboot.git] / board / lwmon / lwmon.c
1 /***********************************************************************
2  *
3 M* Modul:         lwmon.c
4 M*
5 M* Content:       LWMON specific U-Boot commands.
6  *
7  * (C) Copyright 2001, 2002
8  * DENX Software Engineering
9  * Wolfgang Denk, wd@denx.de
10  * All rights reserved.
11  *
12 D* Design:        wd@denx.de
13 C* Coding:        wd@denx.de
14 V* Verification:  dzu@denx.de
15  *
16  * See file CREDITS for list of people who contributed to this
17  * project.
18  *
19  * This program is free software; you can redistribute it and/or
20  * modify it under the terms of the GNU General Public License as
21  * published by the Free Software Foundation; either version 2 of
22  * the License, or (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program; if not, write to the Free Software
31  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32  * MA 02111-1307 USA
33  ***********************************************************************/
34
35 /*---------------------------- Headerfiles ----------------------------*/
36 #include <common.h>
37 #include <mpc8xx.h>
38 #include <commproc.h>
39 #include <i2c.h>
40 #include <command.h>
41 #include <malloc.h>
42 #include <post.h>
43
44 #include <linux/types.h>
45 #include <linux/string.h>       /* for strdup */
46
47 /*------------------------ Local prototypes ---------------------------*/
48 static long int dram_size (long int, long int *, long int);
49 static void kbd_init (void);
50 static int compare_magic (uchar *kbd_data, uchar *str);
51
52
53 /*--------------------- Local macros and constants --------------------*/
54 #define _NOT_USED_      0xFFFFFFFF
55
56 #ifdef CONFIG_MODEM_SUPPORT
57 static int key_pressed(void);
58 extern void disable_putc(void);
59 #endif /* CONFIG_MODEM_SUPPORT */
60
61 /*
62  * 66 MHz SDRAM access using UPM A
63  */
64 const uint sdram_table[] =
65 {
66 #if defined(CFG_MEMORY_75) || defined(CFG_MEMORY_8E)
67         /*
68          * Single Read. (Offset 0 in UPM RAM)
69          */
70         0x1F0DFC04, 0xEEAFBC04, 0x11AF7C04, 0xEFBAFC00,
71         0x1FF5FC47, /* last */
72         /*
73          * SDRAM Initialization (offset 5 in UPM RAM)
74          *
75          * This is no UPM entry point. The following definition uses
76          * the remaining space to establish an initialization
77          * sequence, which is executed by a RUN command.
78          *
79          */
80                     0x1FF5FC34, 0xEFEABC34, 0x1FB57C35, /* last */
81         /*
82          * Burst Read. (Offset 8 in UPM RAM)
83          */
84         0x1F0DFC04, 0xEEAFBC04, 0x10AF7C04, 0xF0AFFC00,
85         0xF0AFFC00, 0xF1AFFC00, 0xEFBAFC00, 0x1FF5FC47, /* last */
86         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
87         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
88         /*
89          * Single Write. (Offset 18 in UPM RAM)
90          */
91         0x1F2DFC04, 0xEEABBC00, 0x01B27C04, 0x1FF5FC47, /* last */
92         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
93         /*
94          * Burst Write. (Offset 20 in UPM RAM)
95          */
96         0x1F0DFC04, 0xEEABBC00, 0x10A77C00, 0xF0AFFC00,
97         0xF0AFFC00, 0xE1BAFC04, 0x01FF5FC47, /* last */
98                                             _NOT_USED_,
99         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
100         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
101         /*
102          * Refresh  (Offset 30 in UPM RAM)
103          */
104         0x1FFD7C84, 0xFFFFFC04, 0xFFFFFC04, 0xFFFFFC04,
105         0xFFFFFC84, 0xFFFFFC07, /* last */
106                                 _NOT_USED_, _NOT_USED_,
107         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
108         /*
109          * Exception. (Offset 3c in UPM RAM)
110          */
111         0x7FFFFC07, /* last */
112                     0xFFFFFCFF, 0xFFFFFCFF, 0xFFFFFCFF,
113 #endif
114 #ifdef CFG_MEMORY_7E
115         /*
116          * Single Read. (Offset 0 in UPM RAM)
117          */
118         0x0E2DBC04, 0x11AF7C04, 0xEFBAFC00, 0x1FF5FC47, /* last */
119         _NOT_USED_,
120         /*
121          * SDRAM Initialization (offset 5 in UPM RAM)
122          *
123          * This is no UPM entry point. The following definition uses
124          * the remaining space to establish an initialization
125          * sequence, which is executed by a RUN command.
126          *
127          */
128                     0x1FF5FC34, 0xEFEABC34, 0x1FB57C35, /* last */
129         /*
130          * Burst Read. (Offset 8 in UPM RAM)
131          */
132         0x0E2DBC04, 0x10AF7C04, 0xF0AFFC00, 0xF0AFFC00,
133         0xF1AFFC00, 0xEFBAFC00, 0x1FF5FC47, /* last */
134                                             _NOT_USED_,
135         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
136         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
137         /*
138          * Single Write. (Offset 18 in UPM RAM)
139          */
140         0x0E29BC04, 0x01B27C04, 0x1FF5FC47, /* last */
141                                             _NOT_USED_,
142         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
143         /*
144          * Burst Write. (Offset 20 in UPM RAM)
145          */
146         0x0E29BC04, 0x10A77C00, 0xF0AFFC00, 0xF0AFFC00,
147         0xE1BAFC04, 0x1FF5FC47, /* last */
148                                 _NOT_USED_, _NOT_USED_,
149         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
150         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
151         /*
152          * Refresh  (Offset 30 in UPM RAM)
153          */
154         0x1FFD7C84, 0xFFFFFC04, 0xFFFFFC04, 0xFFFFFC04,
155         0xFFFFFC84, 0xFFFFFC07, /* last */
156                                 _NOT_USED_, _NOT_USED_,
157         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
158         /*
159          * Exception. (Offset 3c in UPM RAM)
160          */
161         0x7FFFFC07, /* last */
162                     0xFFFFFCFF, 0xFFFFFCFF, 0xFFFFFCFF,
163 #endif
164 };
165
166 /*
167  * Check Board Identity:
168  *
169  */
170
171 /***********************************************************************
172 F* Function:     int checkboard (void) P*A*Z*
173  *
174 P* Parameters:   none
175 P*
176 P* Returnvalue:  int - 0 is always returned
177  *
178 Z* Intention:    This function is the checkboard() method implementation
179 Z*               for the lwmon board.  Only a standard message is printed.
180  *
181 D* Design:       wd@denx.de
182 C* Coding:       wd@denx.de
183 V* Verification: dzu@denx.de
184  ***********************************************************************/
185 int checkboard (void)
186 {
187         puts ("Board: LICCON Konsole LCD2\n");
188         return (0);
189 }
190
191 /***********************************************************************
192 F* Function:     long int initdram (int board_type) P*A*Z*
193  *
194 P* Parameters:   int board_type
195 P*                - Usually type of the board - ignored here.
196 P*
197 P* Returnvalue:  long int
198 P*                - Size of initialized memory
199  *
200 Z* Intention:    This function is the initdram() method implementation
201 Z*               for the lwmon board.
202 Z*               The memory controller is initialized to access the
203 Z*               DRAM.
204  *
205 D* Design:       wd@denx.de
206 C* Coding:       wd@denx.de
207 V* Verification: dzu@denx.de
208  ***********************************************************************/
209 long int initdram (int board_type)
210 {
211         volatile immap_t *immr = (immap_t *) CFG_IMMR;
212         volatile memctl8xx_t *memctl = &immr->im_memctl;
213         long int size_b0;
214         long int size8, size9;
215         int i;
216
217         /*
218          * Configure UPMA for SDRAM
219          */
220         upmconfig (UPMA, (uint *)sdram_table, sizeof(sdram_table)/sizeof(uint));
221
222         memctl->memc_mptpr = CFG_MPTPR;
223
224         /* burst length=4, burst type=sequential, CAS latency=2 */
225         memctl->memc_mar = CFG_MAR;
226
227         /*
228          * Map controller bank 3 to the SDRAM bank at preliminary address.
229          */
230         memctl->memc_or3 = CFG_OR3_PRELIM;
231         memctl->memc_br3 = CFG_BR3_PRELIM;
232
233         /* initialize memory address register */
234         memctl->memc_mamr = CFG_MAMR_8COL;      /* refresh not enabled yet */
235
236         /* mode initialization (offset 5) */
237         udelay (200);                           /* 0x80006105 */
238         memctl->memc_mcr = MCR_OP_RUN | MCR_MB_CS3 | MCR_MLCF (1) | MCR_MAD (0x05);
239
240         /* run 2 refresh sequence with 4-beat refresh burst (offset 0x30) */
241         udelay (1);                             /* 0x80006130 */
242         memctl->memc_mcr = MCR_OP_RUN | MCR_MB_CS3 | MCR_MLCF (1) | MCR_MAD (0x30);
243         udelay (1);                             /* 0x80006130 */
244         memctl->memc_mcr = MCR_OP_RUN | MCR_MB_CS3 | MCR_MLCF (1) | MCR_MAD (0x30);
245
246         udelay (1);                             /* 0x80006106 */
247         memctl->memc_mcr = MCR_OP_RUN | MCR_MB_CS3 | MCR_MLCF (1) | MCR_MAD (0x06);
248
249         memctl->memc_mamr |= MAMR_PTAE; /* refresh enabled */
250
251         udelay (200);
252
253         /* Need at least 10 DRAM accesses to stabilize */
254         for (i = 0; i < 10; ++i) {
255                 volatile unsigned long *addr =
256                         (volatile unsigned long *) SDRAM_BASE3_PRELIM;
257                 unsigned long val;
258
259                 val = *(addr + i);
260                 *(addr + i) = val;
261         }
262
263         /*
264          * Check Bank 0 Memory Size for re-configuration
265          *
266          * try 8 column mode
267          */
268         size8 = dram_size (CFG_MAMR_8COL, (ulong *)SDRAM_BASE3_PRELIM, SDRAM_MAX_SIZE);
269
270         udelay (1000);
271
272         /*
273          * try 9 column mode
274          */
275         size9 = dram_size (CFG_MAMR_9COL, (ulong *)SDRAM_BASE3_PRELIM, SDRAM_MAX_SIZE);
276
277         if (size8 < size9) {            /* leave configuration at 9 columns */
278                 size_b0 = size9;
279                 memctl->memc_mamr = CFG_MAMR_9COL | MAMR_PTAE;
280                 udelay (500);
281         } else {                        /* back to 8 columns            */
282                 size_b0 = size8;
283                 memctl->memc_mamr = CFG_MAMR_8COL | MAMR_PTAE;
284                 udelay (500);
285         }
286
287         /*
288          * Final mapping:
289          */
290
291         memctl->memc_or3 = ((-size_b0) & 0xFFFF0000) |
292                         OR_CSNT_SAM | OR_G5LS | SDRAM_TIMING;
293         memctl->memc_br3 = (CFG_SDRAM_BASE & BR_BA_MSK) | BR_MS_UPMA | BR_V;
294         udelay (1000);
295
296         return (size_b0);
297 }
298
299 /***********************************************************************
300 F* Function:     static long int dram_size (long int mamr_value,
301 F*                                          long int *base,
302 F*                                          long int maxsize) P*A*Z*
303  *
304 P* Parameters:   long int mamr_value
305 P*                - Value for MAMR for the test
306 P*               long int *base
307 P*                - Base address for the test
308 P*               long int maxsize
309 P*                - Maximum size to test for
310 P*
311 P* Returnvalue:  long int
312 P*                - Size of probed memory
313  *
314 Z* Intention:    Check memory range for valid RAM. A simple memory test
315 Z*               determines the actually available RAM size between
316 Z*               addresses `base' and `base + maxsize'. Some (not all)
317 Z*               hardware errors are detected:
318 Z*                - short between address lines
319 Z*                - short between data lines
320  *
321 D* Design:       wd@denx.de
322 C* Coding:       wd@denx.de
323 V* Verification: dzu@denx.de
324  ***********************************************************************/
325 static long int dram_size (long int mamr_value, long int *base, long int maxsize)
326 {
327         volatile immap_t *immr = (immap_t *) CFG_IMMR;
328         volatile memctl8xx_t *memctl = &immr->im_memctl;
329
330         memctl->memc_mamr = mamr_value;
331
332         return (get_ram_size(base, maxsize));
333 }
334
335 /* ------------------------------------------------------------------------- */
336
337 #ifndef PB_ENET_TENA
338 # define PB_ENET_TENA   ((uint)0x00002000)      /* PB 18 */
339 #endif
340
341 /***********************************************************************
342 F* Function:     int board_pre_init (void) P*A*Z*
343  *
344 P* Parameters:   none
345 P*
346 P* Returnvalue:  int
347 P*                - 0 is always returned.
348  *
349 Z* Intention:    This function is the board_pre_init() method implementation
350 Z*               for the lwmon board.
351 Z*               Disable Ethernet TENA on Port B.
352  *
353 D* Design:       wd@denx.de
354 C* Coding:       wd@denx.de
355 V* Verification: dzu@denx.de
356  ***********************************************************************/
357 int board_pre_init (void)
358 {
359         volatile immap_t *immr = (immap_t *) CFG_IMMR;
360
361         /* Disable Ethernet TENA on Port B
362          * Necessary because of pull up in COM3 port.
363          *
364          * This is just a preliminary fix, intended to turn off TENA
365          * as soon as possible to avoid noise on the network. Once
366          * I²C is running we will make sure the interface is
367          * correctly initialized.
368          */
369         immr->im_cpm.cp_pbpar &= ~PB_ENET_TENA;
370         immr->im_cpm.cp_pbodr &= ~PB_ENET_TENA;
371         immr->im_cpm.cp_pbdat &= ~PB_ENET_TENA; /* set to 0 = disabled */
372         immr->im_cpm.cp_pbdir |= PB_ENET_TENA;
373
374         return (0);
375 }
376
377 /* ------------------------------------------------------------------------- */
378
379 /***********************************************************************
380 F* Function:     void reset_phy (void) P*A*Z*
381  *
382 P* Parameters:   none
383 P*
384 P* Returnvalue:  none
385  *
386 Z* Intention:    Reset the PHY.  In the lwmon case we do this by the
387 Z*               signaling the PIC I/O expander.
388  *
389 D* Design:       wd@denx.de
390 C* Coding:       wd@denx.de
391 V* Verification: dzu@denx.de
392  ***********************************************************************/
393 void reset_phy (void)
394 {
395         uchar c;
396
397 #ifdef DEBUG
398         printf ("### Switch on Ethernet for SCC2 ###\n");
399 #endif
400         c = pic_read (0x61);
401 #ifdef DEBUG
402         printf ("Old PIC read: reg_61 = 0x%02x\n", c);
403 #endif
404         c |= 0x40;                                      /* disable COM3 */
405         c &= ~0x80;                                     /* enable Ethernet */
406         pic_write (0x61, c);
407 #ifdef DEBUG
408         c = pic_read (0x61);
409         printf ("New PIC read: reg_61 = 0x%02x\n", c);
410 #endif
411         udelay (1000);
412 }
413
414
415 /*------------------------- Keyboard controller -----------------------*/
416 /* command codes */
417 #define KEYBD_CMD_READ_KEYS     0x01
418 #define KEYBD_CMD_READ_VERSION  0x02
419 #define KEYBD_CMD_READ_STATUS   0x03
420 #define KEYBD_CMD_RESET_ERRORS  0x10
421
422 /* status codes */
423 #define KEYBD_STATUS_MASK       0x3F
424 #define KEYBD_STATUS_H_RESET    0x20
425 #define KEYBD_STATUS_BROWNOUT   0x10
426 #define KEYBD_STATUS_WD_RESET   0x08
427 #define KEYBD_STATUS_OVERLOAD   0x04
428 #define KEYBD_STATUS_ILLEGAL_WR 0x02
429 #define KEYBD_STATUS_ILLEGAL_RD 0x01
430
431 /* Number of bytes returned from Keyboard Controller */
432 #define KEYBD_VERSIONLEN        2       /* version information */
433 #define KEYBD_DATALEN           9       /* normal key scan data */
434
435 /* maximum number of "magic" key codes that can be assigned */
436
437 static uchar kbd_addr = CFG_I2C_KEYBD_ADDR;
438
439 static uchar *key_match (uchar *);
440
441 #define KEYBD_SET_DEBUGMODE     '#'     /* Magic key to enable debug output */
442
443 /***********************************************************************
444 F* Function:     int board_postclk_init (void) P*A*Z*
445  *
446 P* Parameters:   none
447 P*
448 P* Returnvalue:  int
449 P*                - 0 is always returned.
450  *
451 Z* Intention:    This function is the board_postclk_init() method implementation
452 Z*               for the lwmon board.
453  *
454  ***********************************************************************/
455 int board_postclk_init (void)
456 {
457         DECLARE_GLOBAL_DATA_PTR;
458
459         kbd_init();
460
461 #ifdef CONFIG_MODEM_SUPPORT
462         if (key_pressed()) {
463                 disable_putc(); /* modem doesn't understand banner etc */
464                 gd->do_mdm_init = 1;
465         }
466 #endif
467
468         return (0);
469 }
470
471 static void kbd_init (void)
472 {
473         DECLARE_GLOBAL_DATA_PTR;
474
475         uchar kbd_data[KEYBD_DATALEN];
476         uchar tmp_data[KEYBD_DATALEN];
477         uchar val, errcd;
478         int i;
479
480         i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
481
482         gd->kbd_status = 0;
483
484         /* Forced by PIC. Delays <= 175us loose */
485         udelay(1000);
486
487         /* Read initial keyboard error code */
488         val = KEYBD_CMD_READ_STATUS;
489         i2c_write (kbd_addr, 0, 0, &val, 1);
490         i2c_read (kbd_addr, 0, 0, &errcd, 1);
491         /* clear unused bits */
492         errcd &= KEYBD_STATUS_MASK;
493         /* clear "irrelevant" bits. Recommended by Martin Rajek, LWN */
494         errcd &= ~(KEYBD_STATUS_H_RESET|KEYBD_STATUS_BROWNOUT);
495         if (errcd) {
496                 gd->kbd_status |= errcd << 8;
497         }
498         /* Reset error code and verify */
499         val = KEYBD_CMD_RESET_ERRORS;
500         i2c_write (kbd_addr, 0, 0, &val, 1);
501         udelay(1000);   /* delay NEEDED by keyboard PIC !!! */
502
503         val = KEYBD_CMD_READ_STATUS;
504         i2c_write (kbd_addr, 0, 0, &val, 1);
505         i2c_read (kbd_addr, 0, 0, &val, 1);
506
507         val &= KEYBD_STATUS_MASK;       /* clear unused bits */
508         if (val) {                      /* permanent error, report it */
509                 gd->kbd_status |= val;
510                 return;
511         }
512
513         /*
514          * Read current keyboard state.
515          *
516          * After the error reset it may take some time before the
517          * keyboard PIC picks up a valid keyboard scan - the total
518          * scan time is approx. 1.6 ms (information by Martin Rajek,
519          * 28 Sep 2002). We read a couple of times for the keyboard
520          * to stabilize, using a big enough delay.
521          * 10 times should be enough. If the data is still changing,
522          * we use what we get :-(
523          */
524
525         memset (tmp_data, 0xFF, KEYBD_DATALEN); /* impossible value */
526         for (i=0; i<10; ++i) {
527                 val = KEYBD_CMD_READ_KEYS;
528                 i2c_write (kbd_addr, 0, 0, &val, 1);
529                 i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
530
531                 if (memcmp(kbd_data, tmp_data, KEYBD_DATALEN) == 0) {
532                         /* consistent state, done */
533                         break;
534                 }
535                 /* remeber last state, delay, and retry */
536                 memcpy (tmp_data, kbd_data, KEYBD_DATALEN);
537                 udelay (5000);
538         }
539 }
540
541 /***********************************************************************
542 F* Function:     int misc_init_r (void) P*A*Z*
543  *
544 P* Parameters:   none
545 P*
546 P* Returnvalue:  int
547 P*                - 0 is always returned, even in the case of a keyboard
548 P*                    error.
549  *
550 Z* Intention:    This function is the misc_init_r() method implementation
551 Z*               for the lwmon board.
552 Z*               The keyboard controller is initialized and the result
553 Z*               of a read copied to the environment variable "keybd".
554 Z*               If KEYBD_SET_DEBUGMODE is defined, a check is made for
555 Z*               this key, and if found display to the LCD will be enabled.
556 Z*               The keys in "keybd" are checked against the magic
557 Z*               keycommands defined in the environment.
558 Z*               See also key_match().
559  *
560 D* Design:       wd@denx.de
561 C* Coding:       wd@denx.de
562 V* Verification: dzu@denx.de
563  ***********************************************************************/
564 int misc_init_r (void)
565 {
566         DECLARE_GLOBAL_DATA_PTR;
567
568         uchar kbd_data[KEYBD_DATALEN];
569         uchar keybd_env[2 * KEYBD_DATALEN + 1];
570         uchar kbd_init_status = gd->kbd_status >> 8;
571         uchar kbd_status = gd->kbd_status;
572         uchar val;
573         uchar *str;
574         int i;
575
576         if (kbd_init_status) {
577                 printf ("KEYBD: Error %02X\n", kbd_init_status);
578         }
579         if (kbd_status) {               /* permanent error, report it */
580                 printf ("*** Keyboard error code %02X ***\n", kbd_status);
581                 sprintf (keybd_env, "%02X", kbd_status);
582                 setenv ("keybd", keybd_env);
583                 return 0;
584         }
585
586         /*
587          * Now we know that we have a working  keyboard,  so  disable
588          * all output to the LCD except when a key press is detected.
589          */
590
591         if ((console_assign (stdout, "serial") < 0) ||
592                 (console_assign (stderr, "serial") < 0)) {
593                 printf ("Can't assign serial port as output device\n");
594         }
595
596         /* Read Version */
597         val = KEYBD_CMD_READ_VERSION;
598         i2c_write (kbd_addr, 0, 0, &val, 1);
599         i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_VERSIONLEN);
600         printf ("KEYBD: Version %d.%d\n", kbd_data[0], kbd_data[1]);
601
602         /* Read current keyboard state */
603         val = KEYBD_CMD_READ_KEYS;
604         i2c_write (kbd_addr, 0, 0, &val, 1);
605         i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
606
607         for (i = 0; i < KEYBD_DATALEN; ++i) {
608                 sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
609         }
610         setenv ("keybd", keybd_env);
611
612         str = strdup (key_match (kbd_data));    /* decode keys */
613 #ifdef KEYBD_SET_DEBUGMODE
614         if (kbd_data[0] == KEYBD_SET_DEBUGMODE) {       /* set debug mode */
615                 if ((console_assign (stdout, "lcd") < 0) ||
616                         (console_assign (stderr, "lcd") < 0)) {
617                         printf ("Can't assign LCD display as output device\n");
618                 }
619         }
620 #endif /* KEYBD_SET_DEBUGMODE */
621 #ifdef CONFIG_PREBOOT   /* automatically configure "preboot" command on key match */
622         setenv ("preboot", str);        /* set or delete definition */
623 #endif /* CONFIG_PREBOOT */
624         if (str != NULL) {
625                 free (str);
626         }
627         return (0);
628 }
629
630 #ifdef CONFIG_PREBOOT
631
632 static uchar kbd_magic_prefix[] = "key_magic";
633 static uchar kbd_command_prefix[] = "key_cmd";
634
635 static int compare_magic (uchar *kbd_data, uchar *str)
636 {
637         uchar compare[KEYBD_DATALEN-1];
638         uchar *nxt;
639         int i;
640
641         /* Don't include modifier byte */
642         memcpy (compare, kbd_data+1, KEYBD_DATALEN-1);
643
644         for (; str != NULL; str = (*nxt) ? nxt+1 : nxt) {
645                 uchar c;
646                 int k;
647
648                 c = (uchar) simple_strtoul (str, (char **) (&nxt), 16);
649
650                 if (str == nxt) {       /* invalid character */
651                         break;
652                 }
653
654                 /*
655                  * Check if this key matches the input.
656                  * Set matches to zero, so they match only once
657                  * and we can find duplicates or extra keys
658                  */
659                 for (k = 0; k < sizeof(compare); ++k) {
660                         if (compare[k] == '\0') /* only non-zero entries */
661                                 continue;
662                         if (c == compare[k]) {  /* found matching key */
663                                 compare[k] = '\0';
664                                 break;
665                         }
666                 }
667                 if (k == sizeof(compare)) {
668                         return -1;              /* unmatched key */
669                 }
670         }
671
672         /*
673          * A full match leaves no keys in the `compare' array,
674          */
675         for (i = 0; i < sizeof(compare); ++i) {
676                 if (compare[i])
677                 {
678                         return -1;
679                 }
680         }
681
682         return 0;
683 }
684
685 /***********************************************************************
686 F* Function:     static uchar *key_match (uchar *kbd_data) P*A*Z*
687  *
688 P* Parameters:   uchar *kbd_data
689 P*                - The keys to match against our magic definitions
690 P*
691 P* Returnvalue:  uchar *
692 P*                - != NULL: Pointer to the corresponding command(s)
693 P*                     NULL: No magic is about to happen
694  *
695 Z* Intention:    Check if pressed key(s) match magic sequence,
696 Z*               and return the command string associated with that key(s).
697 Z*
698 Z*               If no key press was decoded, NULL is returned.
699 Z*
700 Z*               Note: the first character of the argument will be
701 Z*                     overwritten with the "magic charcter code" of the
702 Z*                     decoded key(s), or '\0'.
703 Z*
704 Z*               Note: the string points to static environment data
705 Z*                     and must be saved before you call any function that
706 Z*                     modifies the environment.
707  *
708 D* Design:       wd@denx.de
709 C* Coding:       wd@denx.de
710 V* Verification: dzu@denx.de
711  ***********************************************************************/
712 static uchar *key_match (uchar *kbd_data)
713 {
714         uchar magic[sizeof (kbd_magic_prefix) + 1];
715         uchar *suffix;
716         uchar *kbd_magic_keys;
717
718         /*
719          * The following string defines the characters that can pe appended
720          * to "key_magic" to form the names of environment variables that
721          * hold "magic" key codes, i. e. such key codes that can cause
722          * pre-boot actions. If the string is empty (""), then only
723          * "key_magic" is checked (old behaviour); the string "125" causes
724          * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
725          */
726         if ((kbd_magic_keys = getenv ("magic_keys")) == NULL)
727                 kbd_magic_keys = "";
728
729         /* loop over all magic keys;
730          * use '\0' suffix in case of empty string
731          */
732         for (suffix=kbd_magic_keys; *suffix || suffix==kbd_magic_keys; ++suffix) {
733                 sprintf (magic, "%s%c", kbd_magic_prefix, *suffix);
734 #if 0
735                 printf ("### Check magic \"%s\"\n", magic);
736 #endif
737                 if (compare_magic(kbd_data, getenv(magic)) == 0) {
738                         uchar cmd_name[sizeof (kbd_command_prefix) + 1];
739                         char *cmd;
740
741                         sprintf (cmd_name, "%s%c", kbd_command_prefix, *suffix);
742
743                         cmd = getenv (cmd_name);
744 #if 0
745                         printf ("### Set PREBOOT to $(%s): \"%s\"\n",
746                                         cmd_name, cmd ? cmd : "<<NULL>>");
747 #endif
748                         *kbd_data = *suffix;
749                         return (cmd);
750                 }
751         }
752 #if 0
753         printf ("### Delete PREBOOT\n");
754 #endif
755         *kbd_data = '\0';
756         return (NULL);
757 }
758 #endif /* CONFIG_PREBOOT */
759
760 /*---------------Board Special Commands: PIC read/write ---------------*/
761
762 #if (CONFIG_COMMANDS & CFG_CMD_BSP)
763 /***********************************************************************
764 F* Function:     int do_pic (cmd_tbl_t *cmdtp, int flag,
765 F*                           int argc, char *argv[]) P*A*Z*
766  *
767 P* Parameters:   cmd_tbl_t *cmdtp
768 P*                - Pointer to our command table entry
769 P*               int flag
770 P*                - If the CMD_FLAG_REPEAT bit is set, then this call is
771 P*                  a repetition
772 P*               int argc
773 P*                - Argument count
774 P*               char *argv[]
775 P*                - Array of the actual arguments
776 P*
777 P* Returnvalue:  int
778 P*                - 0  The command was handled successfully
779 P*                  1  An error occurred
780  *
781 Z* Intention:    Implement the "pic [read|write]" commands.
782 Z*               The read subcommand takes one argument, the register,
783 Z*               whereas the write command takes two, the register and
784 Z*               the new value.
785  *
786 D* Design:       wd@denx.de
787 C* Coding:       wd@denx.de
788 V* Verification: dzu@denx.de
789  ***********************************************************************/
790 int do_pic (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
791 {
792         uchar reg, val;
793
794         switch (argc) {
795         case 3:                                 /* PIC read reg */
796                 if (strcmp (argv[1], "read") != 0)
797                         break;
798
799                 reg = simple_strtoul (argv[2], NULL, 16);
800
801                 printf ("PIC read: reg %02x: %02x\n\n", reg, pic_read (reg));
802
803                 return 0;
804         case 4:                                 /* PIC write reg val */
805                 if (strcmp (argv[1], "write") != 0)
806                         break;
807
808                 reg = simple_strtoul (argv[2], NULL, 16);
809                 val = simple_strtoul (argv[3], NULL, 16);
810
811                 printf ("PIC write: reg %02x val 0x%02x: %02x => ",
812                                 reg, val, pic_read (reg));
813                 pic_write (reg, val);
814                 printf ("%02x\n\n", pic_read (reg));
815                 return 0;
816         default:
817                 break;
818         }
819         printf ("Usage:\n%s\n", cmdtp->usage);
820         return 1;
821 }
822 U_BOOT_CMD(
823         pic,    4,      1,      do_pic,
824         "pic     - read and write PIC registers\n",
825         "read  reg      - read PIC register `reg'\n"
826         "pic write reg val  - write value `val' to PIC register `reg'\n"
827 );
828
829 /***********************************************************************
830 F* Function:     int do_kbd (cmd_tbl_t *cmdtp, int flag,
831 F*                           int argc, char *argv[]) P*A*Z*
832  *
833 P* Parameters:   cmd_tbl_t *cmdtp
834 P*                - Pointer to our command table entry
835 P*               int flag
836 P*                - If the CMD_FLAG_REPEAT bit is set, then this call is
837 P*                  a repetition
838 P*               int argc
839 P*                - Argument count
840 P*               char *argv[]
841 P*                - Array of the actual arguments
842 P*
843 P* Returnvalue:  int
844 P*                - 0 is always returned.
845  *
846 Z* Intention:    Implement the "kbd" command.
847 Z*               The keyboard status is read.  The result is printed on
848 Z*               the console and written into the "keybd" environment
849 Z*               variable.
850  *
851 D* Design:       wd@denx.de
852 C* Coding:       wd@denx.de
853 V* Verification: dzu@denx.de
854  ***********************************************************************/
855 int do_kbd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
856 {
857         uchar kbd_data[KEYBD_DATALEN];
858         uchar keybd_env[2 * KEYBD_DATALEN + 1];
859         uchar val;
860         int i;
861
862 #if 0 /* Done in kbd_init */
863         i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
864 #endif
865
866         /* Read keys */
867         val = KEYBD_CMD_READ_KEYS;
868         i2c_write (kbd_addr, 0, 0, &val, 1);
869         i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
870
871         puts ("Keys:");
872         for (i = 0; i < KEYBD_DATALEN; ++i) {
873                 sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
874                 printf (" %02x", kbd_data[i]);
875         }
876         putc ('\n');
877         setenv ("keybd", keybd_env);
878         return 0;
879 }
880
881 U_BOOT_CMD(
882         kbd,    1,      1,      do_kbd,
883         "kbd     - read keyboard status\n",
884         NULL
885 );
886
887 /* Read and set LSB switch */
888 #define CFG_PC_TXD1_ENA         0x0008          /* PC.12 */
889
890 /***********************************************************************
891 F* Function:     int do_lsb (cmd_tbl_t *cmdtp, int flag,
892 F*                           int argc, char *argv[]) P*A*Z*
893  *
894 P* Parameters:   cmd_tbl_t *cmdtp
895 P*                - Pointer to our command table entry
896 P*               int flag
897 P*                - If the CMD_FLAG_REPEAT bit is set, then this call is
898 P*                  a repetition
899 P*               int argc
900 P*                - Argument count
901 P*               char *argv[]
902 P*                - Array of the actual arguments
903 P*
904 P* Returnvalue:  int
905 P*                - 0  The command was handled successfully
906 P*                  1  An error occurred
907  *
908 Z* Intention:    Implement the "lsb [on|off]" commands.
909 Z*               The lsb is switched according to the first parameter by
910 Z*               by signaling the PIC I/O expander.
911 Z*               Called with no arguments, the current setting is
912 Z*               printed.
913  *
914 D* Design:       wd@denx.de
915 C* Coding:       wd@denx.de
916 V* Verification: dzu@denx.de
917  ***********************************************************************/
918 int do_lsb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
919 {
920         uchar val;
921         immap_t *immr = (immap_t *) CFG_IMMR;
922
923         switch (argc) {
924         case 1:                                 /* lsb - print setting */
925                 val = pic_read (0x60);
926                 printf ("LSB is o%s\n", (val & 0x20) ? "n" : "ff");
927                 return 0;
928         case 2:                                 /* lsb on or lsb off - set switch */
929                 val = pic_read (0x60);
930
931                 if (strcmp (argv[1], "on") == 0) {
932                         val |= 0x20;
933                         immr->im_ioport.iop_pcpar &= ~(CFG_PC_TXD1_ENA);
934                         immr->im_ioport.iop_pcdat |= CFG_PC_TXD1_ENA;
935                         immr->im_ioport.iop_pcdir |= CFG_PC_TXD1_ENA;
936                 } else if (strcmp (argv[1], "off") == 0) {
937                         val &= ~0x20;
938                         immr->im_ioport.iop_pcpar &= ~(CFG_PC_TXD1_ENA);
939                         immr->im_ioport.iop_pcdat &= ~(CFG_PC_TXD1_ENA);
940                         immr->im_ioport.iop_pcdir |= CFG_PC_TXD1_ENA;
941                 } else {
942                         break;
943                 }
944                 pic_write (0x60, val);
945                 return 0;
946         default:
947                 break;
948         }
949         printf ("Usage:\n%s\n", cmdtp->usage);
950         return 1;
951 }
952
953 U_BOOT_CMD(
954         lsb,    2,      1,      do_lsb,
955         "lsb     - check and set LSB switch\n",
956         "on  - switch LSB on\n"
957         "lsb off - switch LSB off\n"
958         "lsb     - print current setting\n"
959 );
960
961 #endif /* CFG_CMD_BSP */
962
963 /*----------------------------- Utilities -----------------------------*/
964 /***********************************************************************
965 F* Function:     uchar pic_read (uchar reg) P*A*Z*
966  *
967 P* Parameters:   uchar reg
968 P*                - Register to read
969 P*
970 P* Returnvalue:  uchar
971 P*                - Value read from register
972  *
973 Z* Intention:    Read a register from the PIC I/O expander.
974  *
975 D* Design:       wd@denx.de
976 C* Coding:       wd@denx.de
977 V* Verification: dzu@denx.de
978  ***********************************************************************/
979 uchar pic_read (uchar reg)
980 {
981         return (i2c_reg_read (CFG_I2C_PICIO_ADDR, reg));
982 }
983
984 /***********************************************************************
985 F* Function:     void pic_write (uchar reg, uchar val) P*A*Z*
986  *
987 P* Parameters:   uchar reg
988 P*                - Register to read
989 P*               uchar val
990 P*                - Value to write
991 P*
992 P* Returnvalue:  none
993  *
994 Z* Intention:    Write to a register on the PIC I/O expander.
995  *
996 D* Design:       wd@denx.de
997 C* Coding:       wd@denx.de
998 V* Verification: dzu@denx.de
999  ***********************************************************************/
1000 void pic_write (uchar reg, uchar val)
1001 {
1002         i2c_reg_write (CFG_I2C_PICIO_ADDR, reg, val);
1003 }
1004
1005 /*---------------------- Board Control Functions ----------------------*/
1006 /***********************************************************************
1007 F* Function:     void board_poweroff (void) P*A*Z*
1008  *
1009 P* Parameters:   none
1010 P*
1011 P* Returnvalue:  none
1012  *
1013 Z* Intention:    Turn off the battery power and loop endless, so this
1014 Z*               should better be the last function you call...
1015  *
1016 D* Design:       wd@denx.de
1017 C* Coding:       wd@denx.de
1018 V* Verification: dzu@denx.de
1019  ***********************************************************************/
1020 void board_poweroff (void)
1021 {
1022     /* Turn battery off */
1023     ((volatile immap_t *)CFG_IMMR)->im_ioport.iop_pcdat &= ~(1 << (31 - 13));
1024
1025     while (1);
1026 }
1027
1028 #ifdef CONFIG_MODEM_SUPPORT
1029 static int key_pressed(void)
1030 {
1031         uchar kbd_data[KEYBD_DATALEN];
1032         uchar val;
1033
1034         /* Read keys */
1035         val = KEYBD_CMD_READ_KEYS;
1036         i2c_write (kbd_addr, 0, 0, &val, 1);
1037         i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
1038
1039         return (compare_magic(kbd_data, CONFIG_MODEM_KEY_MAGIC) == 0);
1040 }
1041 #endif  /* CONFIG_MODEM_SUPPORT */
1042
1043 #ifdef CONFIG_POST
1044 /*
1045  * Returns 1 if keys pressed to start the power-on long-running tests
1046  * Called from board_init_f().
1047  */
1048 int post_hotkeys_pressed(void)
1049 {
1050         uchar kbd_data[KEYBD_DATALEN];
1051         uchar val;
1052
1053         /* Read keys */
1054         val = KEYBD_CMD_READ_KEYS;
1055         i2c_write (kbd_addr, 0, 0, &val, 1);
1056         i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
1057
1058         return (compare_magic(kbd_data, CONFIG_POST_KEY_MAGIC) == 0);
1059 }
1060 #endif