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