Merge from "stable branch", tag LABEL_2003_06_28_1800-stable:
[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
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: Litronic Monitor IV\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_PTBE; /* 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_PTBE;
280                 udelay (500);
281         } else {                        /* back to 8 columns            */
282                 size_b0 = size8;
283                 memctl->memc_mamr = CFG_MAMR_8COL | MAMR_PTBE;
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         volatile long int *addr;
330         ulong cnt, val;
331         ulong save[32];                         /* to make test non-destructive */
332         unsigned char i = 0;
333
334         memctl->memc_mamr = mamr_value;
335
336         for (cnt = maxsize / sizeof (long); cnt > 0; cnt >>= 1) {
337                 addr = base + cnt;              /* pointer arith! */
338
339                 save[i++] = *addr;
340                 *addr = ~cnt;
341         }
342
343         /* write 0 to base address */
344         addr = base;
345         save[i] = *addr;
346         *addr = 0;
347
348         /* check at base address */
349         if ((val = *addr) != 0) {
350                 *addr = save[i];
351                 return (0);
352         }
353
354         for (cnt = 1; cnt <= maxsize / sizeof (long); cnt <<= 1) {
355                 addr = base + cnt;              /* pointer arith! */
356
357                 val = *addr;
358                 *addr = save[--i];
359
360                 if (val != (~cnt)) {
361                         return (cnt * sizeof (long));
362                 }
363         }
364         return (maxsize);
365 }
366
367 /* ------------------------------------------------------------------------- */
368
369 #ifndef PB_ENET_TENA
370 # define PB_ENET_TENA   ((uint)0x00002000)      /* PB 18 */
371 #endif
372
373 /***********************************************************************
374 F* Function:     int board_pre_init (void) P*A*Z*
375  *
376 P* Parameters:   none
377 P*
378 P* Returnvalue:  int
379 P*                - 0 is always returned.
380  *
381 Z* Intention:    This function is the board_pre_init() method implementation
382 Z*               for the lwmon board.
383 Z*               Disable Ethernet TENA on Port B.
384  *
385 D* Design:       wd@denx.de
386 C* Coding:       wd@denx.de
387 V* Verification: dzu@denx.de
388  ***********************************************************************/
389 int board_pre_init (void)
390 {
391         volatile immap_t *immr = (immap_t *) CFG_IMMR;
392
393         /* Disable Ethernet TENA on Port B
394          * Necessary because of pull up in COM3 port.
395          *
396          * This is just a preliminary fix, intended to turn off TENA
397          * as soon as possible to avoid noise on the network. Once
398          * I²C is running we will make sure the interface is
399          * correctly initialized.
400          */
401         immr->im_cpm.cp_pbpar &= ~PB_ENET_TENA;
402         immr->im_cpm.cp_pbodr &= ~PB_ENET_TENA;
403         immr->im_cpm.cp_pbdat &= ~PB_ENET_TENA; /* set to 0 = disabled */
404         immr->im_cpm.cp_pbdir |= PB_ENET_TENA;
405
406         return (0);
407 }
408
409 /* ------------------------------------------------------------------------- */
410
411 /***********************************************************************
412 F* Function:     void reset_phy (void) P*A*Z*
413  *
414 P* Parameters:   none
415 P*
416 P* Returnvalue:  none
417  *
418 Z* Intention:    Reset the PHY.  In the lwmon case we do this by the
419 Z*               signaling the PIC I/O expander.
420  *
421 D* Design:       wd@denx.de
422 C* Coding:       wd@denx.de
423 V* Verification: dzu@denx.de
424  ***********************************************************************/
425 void reset_phy (void)
426 {
427         uchar c;
428
429 #ifdef DEBUG
430         printf ("### Switch on Ethernet for SCC2 ###\n");
431 #endif
432         c = pic_read (0x61);
433 #ifdef DEBUG
434         printf ("Old PIC read: reg_61 = 0x%02x\n", c);
435 #endif
436         c |= 0x40;                                      /* disable COM3 */
437         c &= ~0x80;                                     /* enable Ethernet */
438         pic_write (0x61, c);
439 #ifdef DEBUG
440         c = pic_read (0x61);
441         printf ("New PIC read: reg_61 = 0x%02x\n", c);
442 #endif
443         udelay (1000);
444 }
445
446
447 /*------------------------- Keyboard controller -----------------------*/
448 /* command codes */
449 #define KEYBD_CMD_READ_KEYS     0x01
450 #define KEYBD_CMD_READ_VERSION  0x02
451 #define KEYBD_CMD_READ_STATUS   0x03
452 #define KEYBD_CMD_RESET_ERRORS  0x10
453
454 /* status codes */
455 #define KEYBD_STATUS_MASK       0x3F
456 #define KEYBD_STATUS_H_RESET    0x20
457 #define KEYBD_STATUS_BROWNOUT   0x10
458 #define KEYBD_STATUS_WD_RESET   0x08
459 #define KEYBD_STATUS_OVERLOAD   0x04
460 #define KEYBD_STATUS_ILLEGAL_WR 0x02
461 #define KEYBD_STATUS_ILLEGAL_RD 0x01
462
463 /* Number of bytes returned from Keyboard Controller */
464 #define KEYBD_VERSIONLEN        2       /* version information */
465 #define KEYBD_DATALEN           9       /* normal key scan data */
466
467 /* maximum number of "magic" key codes that can be assigned */
468
469 static uchar kbd_addr = CFG_I2C_KEYBD_ADDR;
470
471 static uchar *key_match (uchar *);
472
473 #define KEYBD_SET_DEBUGMODE     '#'     /* Magic key to enable debug output */
474
475 /***********************************************************************
476 F* Function:     int board_postclk_init (void) P*A*Z*
477  *
478 P* Parameters:   none
479 P*
480 P* Returnvalue:  int
481 P*                - 0 is always returned.
482  *
483 Z* Intention:    This function is the board_postclk_init() method implementation
484 Z*               for the lwmon board.
485  *
486  ***********************************************************************/
487 int board_postclk_init (void)
488 {
489         DECLARE_GLOBAL_DATA_PTR;
490
491         kbd_init();
492
493 #ifdef CONFIG_MODEM_SUPPORT
494         if (key_pressed()) {
495                 disable_putc(); /* modem doesn't understand banner etc */
496                 gd->do_mdm_init = 1;
497         }
498 #endif
499
500         return (0);
501 }
502
503 static void kbd_init (void)
504 {
505         DECLARE_GLOBAL_DATA_PTR;
506
507         uchar kbd_data[KEYBD_DATALEN];
508         uchar tmp_data[KEYBD_DATALEN];
509         uchar val, errcd;
510         int i;
511
512         i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
513
514         gd->kbd_status = 0;
515
516         /* Forced by PIC. Delays <= 175us loose */
517         udelay(1000);
518
519         /* Read initial keyboard error code */
520         val = KEYBD_CMD_READ_STATUS;
521         i2c_write (kbd_addr, 0, 0, &val, 1);
522         i2c_read (kbd_addr, 0, 0, &errcd, 1);
523         /* clear unused bits */
524         errcd &= KEYBD_STATUS_MASK;
525         /* clear "irrelevant" bits. Recommended by Martin Rajek, LWN */
526         errcd &= ~(KEYBD_STATUS_H_RESET|KEYBD_STATUS_BROWNOUT);
527         if (errcd) {
528                 gd->kbd_status |= errcd << 8;
529         }
530         /* Reset error code and verify */
531         val = KEYBD_CMD_RESET_ERRORS;
532         i2c_write (kbd_addr, 0, 0, &val, 1);
533         udelay(1000);   /* delay NEEDED by keyboard PIC !!! */
534
535         val = KEYBD_CMD_READ_STATUS;
536         i2c_write (kbd_addr, 0, 0, &val, 1);
537         i2c_read (kbd_addr, 0, 0, &val, 1);
538
539         val &= KEYBD_STATUS_MASK;       /* clear unused bits */
540         if (val) {                      /* permanent error, report it */
541                 gd->kbd_status |= val;
542                 return;
543         }
544
545         /*
546          * Read current keyboard state.
547          *
548          * After the error reset it may take some time before the
549          * keyboard PIC picks up a valid keyboard scan - the total
550          * scan time is approx. 1.6 ms (information by Martin Rajek,
551          * 28 Sep 2002). We read a couple of times for the keyboard
552          * to stabilize, using a big enough delay.
553          * 10 times should be enough. If the data is still changing,
554          * we use what we get :-(
555          */
556
557         memset (tmp_data, 0xFF, KEYBD_DATALEN); /* impossible value */
558         for (i=0; i<10; ++i) {
559                 val = KEYBD_CMD_READ_KEYS;
560                 i2c_write (kbd_addr, 0, 0, &val, 1);
561                 i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
562
563                 if (memcmp(kbd_data, tmp_data, KEYBD_DATALEN) == 0) {
564                         /* consistent state, done */
565                         break;
566                 }
567                 /* remeber last state, delay, and retry */
568                 memcpy (tmp_data, kbd_data, KEYBD_DATALEN);
569                 udelay (5000);
570         }
571 }
572
573 /***********************************************************************
574 F* Function:     int misc_init_r (void) P*A*Z*
575  *
576 P* Parameters:   none
577 P*
578 P* Returnvalue:  int
579 P*                - 0 is always returned, even in the case of a keyboard
580 P*                    error.
581  *
582 Z* Intention:    This function is the misc_init_r() method implementation
583 Z*               for the lwmon board.
584 Z*               The keyboard controller is initialized and the result
585 Z*               of a read copied to the environment variable "keybd".
586 Z*               If KEYBD_SET_DEBUGMODE is defined, a check is made for
587 Z*               this key, and if found display to the LCD will be enabled.
588 Z*               The keys in "keybd" are checked against the magic
589 Z*               keycommands defined in the environment.
590 Z*               See also key_match().
591  *
592 D* Design:       wd@denx.de
593 C* Coding:       wd@denx.de
594 V* Verification: dzu@denx.de
595  ***********************************************************************/
596 int misc_init_r (void)
597 {
598         DECLARE_GLOBAL_DATA_PTR;
599
600         uchar kbd_data[KEYBD_DATALEN];
601         uchar keybd_env[2 * KEYBD_DATALEN + 1];
602         uchar kbd_init_status = gd->kbd_status >> 8;
603         uchar kbd_status = gd->kbd_status;
604         uchar val;
605         uchar *str;
606         int i;
607
608         if (kbd_init_status) {
609                 printf ("KEYBD: Error %02X\n", kbd_init_status);
610         }
611         if (kbd_status) {               /* permanent error, report it */
612                 printf ("*** Keyboard error code %02X ***\n", kbd_status);
613                 sprintf (keybd_env, "%02X", kbd_status);
614                 setenv ("keybd", keybd_env);
615                 return 0;
616         }
617
618         /*
619          * Now we know that we have a working  keyboard,  so  disable
620          * all output to the LCD except when a key press is detected.
621          */
622
623         if ((console_assign (stdout, "serial") < 0) ||
624                 (console_assign (stderr, "serial") < 0)) {
625                 printf ("Can't assign serial port as output device\n");
626         }
627
628         /* Read Version */
629         val = KEYBD_CMD_READ_VERSION;
630         i2c_write (kbd_addr, 0, 0, &val, 1);
631         i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_VERSIONLEN);
632         printf ("KEYBD: Version %d.%d\n", kbd_data[0], kbd_data[1]);
633
634         /* Read current keyboard state */
635         val = KEYBD_CMD_READ_KEYS;
636         i2c_write (kbd_addr, 0, 0, &val, 1);
637         i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
638
639         for (i = 0; i < KEYBD_DATALEN; ++i) {
640                 sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
641         }
642         setenv ("keybd", keybd_env);
643
644         str = strdup (key_match (kbd_data));    /* decode keys */
645 #ifdef KEYBD_SET_DEBUGMODE
646         if (kbd_data[0] == KEYBD_SET_DEBUGMODE) {       /* set debug mode */
647                 if ((console_assign (stdout, "lcd") < 0) ||
648                         (console_assign (stderr, "lcd") < 0)) {
649                         printf ("Can't assign LCD display as output device\n");
650                 }
651         }
652 #endif /* KEYBD_SET_DEBUGMODE */
653 #ifdef CONFIG_PREBOOT   /* automatically configure "preboot" command on key match */
654         setenv ("preboot", str);        /* set or delete definition */
655 #endif /* CONFIG_PREBOOT */
656         if (str != NULL) {
657                 free (str);
658         }
659         return (0);
660 }
661
662 #ifdef CONFIG_PREBOOT
663
664 static uchar kbd_magic_prefix[] = "key_magic";
665 static uchar kbd_command_prefix[] = "key_cmd";
666
667 static int compare_magic (uchar *kbd_data, uchar *str)
668 {
669         uchar compare[KEYBD_DATALEN-1];
670         uchar *nxt;
671         int i;
672
673         /* Don't include modifier byte */
674         memcpy (compare, kbd_data+1, KEYBD_DATALEN-1);
675
676         for (; str != NULL; str = (*nxt) ? nxt+1 : nxt) {
677                 uchar c;
678                 int k;
679
680                 c = (uchar) simple_strtoul (str, (char **) (&nxt), 16);
681
682                 if (str == nxt) {       /* invalid character */
683                         break;
684                 }
685
686                 /*
687                  * Check if this key matches the input.
688                  * Set matches to zero, so they match only once
689                  * and we can find duplicates or extra keys
690                  */
691                 for (k = 0; k < sizeof(compare); ++k) {
692                         if (compare[k] == '\0') /* only non-zero entries */
693                                 continue;
694                         if (c == compare[k]) {  /* found matching key */
695                                 compare[k] = '\0';
696                                 break;
697                         }
698                 }
699                 if (k == sizeof(compare)) {
700                         return -1;              /* unmatched key */
701                 }
702         }
703
704         /*
705          * A full match leaves no keys in the `compare' array,
706          */
707         for (i = 0; i < sizeof(compare); ++i) {
708                 if (compare[i])
709                 {
710                         return -1;
711                 }
712         }
713
714         return 0;
715 }
716
717 /***********************************************************************
718 F* Function:     static uchar *key_match (uchar *kbd_data) P*A*Z*
719  *
720 P* Parameters:   uchar *kbd_data
721 P*                - The keys to match against our magic definitions
722 P*
723 P* Returnvalue:  uchar *
724 P*                - != NULL: Pointer to the corresponding command(s)
725 P*                     NULL: No magic is about to happen
726  *
727 Z* Intention:    Check if pressed key(s) match magic sequence,
728 Z*               and return the command string associated with that key(s).
729 Z*
730 Z*               If no key press was decoded, NULL is returned.
731 Z*
732 Z*               Note: the first character of the argument will be
733 Z*                     overwritten with the "magic charcter code" of the
734 Z*                     decoded key(s), or '\0'.
735 Z*
736 Z*               Note: the string points to static environment data
737 Z*                     and must be saved before you call any function that
738 Z*                     modifies the environment.
739  *
740 D* Design:       wd@denx.de
741 C* Coding:       wd@denx.de
742 V* Verification: dzu@denx.de
743  ***********************************************************************/
744 static uchar *key_match (uchar *kbd_data)
745 {
746         uchar magic[sizeof (kbd_magic_prefix) + 1];
747         uchar *suffix;
748         uchar *kbd_magic_keys;
749
750         /*
751          * The following string defines the characters that can pe appended
752          * to "key_magic" to form the names of environment variables that
753          * hold "magic" key codes, i. e. such key codes that can cause
754          * pre-boot actions. If the string is empty (""), then only
755          * "key_magic" is checked (old behaviour); the string "125" causes
756          * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
757          */
758         if ((kbd_magic_keys = getenv ("magic_keys")) == NULL)
759                 kbd_magic_keys = "";
760
761         /* loop over all magic keys;
762          * use '\0' suffix in case of empty string
763          */
764         for (suffix=kbd_magic_keys; *suffix || suffix==kbd_magic_keys; ++suffix) {
765                 sprintf (magic, "%s%c", kbd_magic_prefix, *suffix);
766 #if 0
767                 printf ("### Check magic \"%s\"\n", magic);
768 #endif
769                 if (compare_magic(kbd_data, getenv(magic)) == 0) {
770                         uchar cmd_name[sizeof (kbd_command_prefix) + 1];
771                         char *cmd;
772
773                         sprintf (cmd_name, "%s%c", kbd_command_prefix, *suffix);
774
775                         cmd = getenv (cmd_name);
776 #if 0
777                         printf ("### Set PREBOOT to $(%s): \"%s\"\n",
778                                         cmd_name, cmd ? cmd : "<<NULL>>");
779 #endif
780                         *kbd_data = *suffix;
781                         return (cmd);
782                 }
783         }
784 #if 0
785         printf ("### Delete PREBOOT\n");
786 #endif
787         *kbd_data = '\0';
788         return (NULL);
789 }
790 #endif /* CONFIG_PREBOOT */
791
792 /*---------------Board Special Commands: PIC read/write ---------------*/
793
794 #if (CONFIG_COMMANDS & CFG_CMD_BSP)
795 /***********************************************************************
796 F* Function:     int do_pic (cmd_tbl_t *cmdtp, int flag,
797 F*                           int argc, char *argv[]) P*A*Z*
798  *
799 P* Parameters:   cmd_tbl_t *cmdtp
800 P*                - Pointer to our command table entry
801 P*               int flag
802 P*                - If the CMD_FLAG_REPEAT bit is set, then this call is
803 P*                  a repetition
804 P*               int argc
805 P*                - Argument count
806 P*               char *argv[]
807 P*                - Array of the actual arguments
808 P*
809 P* Returnvalue:  int
810 P*                - 0  The command was handled successfully
811 P*                  1  An error occurred
812  *
813 Z* Intention:    Implement the "pic [read|write]" commands.
814 Z*               The read subcommand takes one argument, the register,
815 Z*               whereas the write command takes two, the register and
816 Z*               the new value.
817  *
818 D* Design:       wd@denx.de
819 C* Coding:       wd@denx.de
820 V* Verification: dzu@denx.de
821  ***********************************************************************/
822 int do_pic (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
823 {
824         uchar reg, val;
825
826         switch (argc) {
827         case 3:                                 /* PIC read reg */
828                 if (strcmp (argv[1], "read") != 0)
829                         break;
830
831                 reg = simple_strtoul (argv[2], NULL, 16);
832
833                 printf ("PIC read: reg %02x: %02x\n\n", reg, pic_read (reg));
834
835                 return 0;
836         case 4:                                 /* PIC write reg val */
837                 if (strcmp (argv[1], "write") != 0)
838                         break;
839
840                 reg = simple_strtoul (argv[2], NULL, 16);
841                 val = simple_strtoul (argv[3], NULL, 16);
842
843                 printf ("PIC write: reg %02x val 0x%02x: %02x => ",
844                                 reg, val, pic_read (reg));
845                 pic_write (reg, val);
846                 printf ("%02x\n\n", pic_read (reg));
847                 return 0;
848         default:
849                 break;
850         }
851         printf ("Usage:\n%s\n", cmdtp->usage);
852         return 1;
853 }
854 cmd_tbl_t U_BOOT_CMD(pic) = MK_CMD_ENTRY(
855         "pic",  4,      1,      do_pic,
856         "pic     - read and write PIC registers\n",
857         "read  reg      - read PIC register `reg'\n"
858         "pic write reg val  - write value `val' to PIC register `reg'\n"
859 );
860
861 /***********************************************************************
862 F* Function:     int do_kbd (cmd_tbl_t *cmdtp, int flag,
863 F*                           int argc, char *argv[]) P*A*Z*
864  *
865 P* Parameters:   cmd_tbl_t *cmdtp
866 P*                - Pointer to our command table entry
867 P*               int flag
868 P*                - If the CMD_FLAG_REPEAT bit is set, then this call is
869 P*                  a repetition
870 P*               int argc
871 P*                - Argument count
872 P*               char *argv[]
873 P*                - Array of the actual arguments
874 P*
875 P* Returnvalue:  int
876 P*                - 0 is always returned.
877  *
878 Z* Intention:    Implement the "kbd" command.
879 Z*               The keyboard status is read.  The result is printed on
880 Z*               the console and written into the "keybd" environment
881 Z*               variable.
882  *
883 D* Design:       wd@denx.de
884 C* Coding:       wd@denx.de
885 V* Verification: dzu@denx.de
886  ***********************************************************************/
887 int do_kbd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
888 {
889         uchar kbd_data[KEYBD_DATALEN];
890         uchar keybd_env[2 * KEYBD_DATALEN + 1];
891         uchar val;
892         int i;
893
894 #if 0 /* Done in kbd_init */
895         i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
896 #endif
897
898         /* Read keys */
899         val = KEYBD_CMD_READ_KEYS;
900         i2c_write (kbd_addr, 0, 0, &val, 1);
901         i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
902
903         puts ("Keys:");
904         for (i = 0; i < KEYBD_DATALEN; ++i) {
905                 sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
906                 printf (" %02x", kbd_data[i]);
907         }
908         putc ('\n');
909         setenv ("keybd", keybd_env);
910         return 0;
911 }
912
913 cmd_tbl_t U_BOOT_CMD(kdb) = MK_CMD_ENTRY(
914         "kbd",  1,      1,      do_kbd,
915         "kbd     - read keyboard status\n",
916         NULL
917 );
918
919 /* Read and set LSB switch */
920 #define CFG_PC_TXD1_ENA         0x0008          /* PC.12 */
921
922 /***********************************************************************
923 F* Function:     int do_lsb (cmd_tbl_t *cmdtp, int flag,
924 F*                           int argc, char *argv[]) P*A*Z*
925  *
926 P* Parameters:   cmd_tbl_t *cmdtp
927 P*                - Pointer to our command table entry
928 P*               int flag
929 P*                - If the CMD_FLAG_REPEAT bit is set, then this call is
930 P*                  a repetition
931 P*               int argc
932 P*                - Argument count
933 P*               char *argv[]
934 P*                - Array of the actual arguments
935 P*
936 P* Returnvalue:  int
937 P*                - 0  The command was handled successfully
938 P*                  1  An error occurred
939  *
940 Z* Intention:    Implement the "lsb [on|off]" commands.
941 Z*               The lsb is switched according to the first parameter by
942 Z*               by signaling the PIC I/O expander.
943 Z*               Called with no arguments, the current setting is
944 Z*               printed.
945  *
946 D* Design:       wd@denx.de
947 C* Coding:       wd@denx.de
948 V* Verification: dzu@denx.de
949  ***********************************************************************/
950 int do_lsb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
951 {
952         uchar val;
953         immap_t *immr = (immap_t *) CFG_IMMR;
954
955         switch (argc) {
956         case 1:                                 /* lsb - print setting */
957                 val = pic_read (0x60);
958                 printf ("LSB is o%s\n", (val & 0x20) ? "n" : "ff");
959                 return 0;
960         case 2:                                 /* lsb on or lsb off - set switch */
961                 val = pic_read (0x60);
962
963                 if (strcmp (argv[1], "on") == 0) {
964                         val |= 0x20;
965                         immr->im_ioport.iop_pcpar &= ~(CFG_PC_TXD1_ENA);
966                         immr->im_ioport.iop_pcdat |= CFG_PC_TXD1_ENA;
967                         immr->im_ioport.iop_pcdir |= CFG_PC_TXD1_ENA;
968                 } else if (strcmp (argv[1], "off") == 0) {
969                         val &= ~0x20;
970                         immr->im_ioport.iop_pcpar &= ~(CFG_PC_TXD1_ENA);
971                         immr->im_ioport.iop_pcdat &= ~(CFG_PC_TXD1_ENA);
972                         immr->im_ioport.iop_pcdir |= CFG_PC_TXD1_ENA;
973                 } else {
974                         break;
975                 }
976                 pic_write (0x60, val);
977                 return 0;
978         default:
979                 break;
980         }
981         printf ("Usage:\n%s\n", cmdtp->usage);
982         return 1;
983 }
984
985 cmd_tbl_t U_BOOT_CMD(lsb) = MK_CMD_ENTRY(
986         "lsb",  2,      1,      do_lsb,
987         "lsb     - check and set LSB switch\n",
988         "on  - switch LSB on\n"
989         "lsb off - switch LSB off\n"
990         "lsb     - print current setting\n"
991 );
992
993 #endif /* CFG_CMD_BSP */
994
995 /*----------------------------- Utilities -----------------------------*/
996 /***********************************************************************
997 F* Function:     uchar pic_read (uchar reg) P*A*Z*
998  *
999 P* Parameters:   uchar reg
1000 P*                - Register to read
1001 P*
1002 P* Returnvalue:  uchar
1003 P*                - Value read from register
1004  *
1005 Z* Intention:    Read a register from the PIC I/O expander.
1006  *
1007 D* Design:       wd@denx.de
1008 C* Coding:       wd@denx.de
1009 V* Verification: dzu@denx.de
1010  ***********************************************************************/
1011 uchar pic_read (uchar reg)
1012 {
1013         return (i2c_reg_read (CFG_I2C_PICIO_ADDR, reg));
1014 }
1015
1016 /***********************************************************************
1017 F* Function:     void pic_write (uchar reg, uchar val) P*A*Z*
1018  *
1019 P* Parameters:   uchar reg
1020 P*                - Register to read
1021 P*               uchar val
1022 P*                - Value to write
1023 P*
1024 P* Returnvalue:  none
1025  *
1026 Z* Intention:    Write to a register on the PIC I/O expander.
1027  *
1028 D* Design:       wd@denx.de
1029 C* Coding:       wd@denx.de
1030 V* Verification: dzu@denx.de
1031  ***********************************************************************/
1032 void pic_write (uchar reg, uchar val)
1033 {
1034         i2c_reg_write (CFG_I2C_PICIO_ADDR, reg, val);
1035 }
1036
1037 /*---------------------- Board Control Functions ----------------------*/
1038 /***********************************************************************
1039 F* Function:     void board_poweroff (void) P*A*Z*
1040  *
1041 P* Parameters:   none
1042 P*
1043 P* Returnvalue:  none
1044  *
1045 Z* Intention:    Turn off the battery power and loop endless, so this
1046 Z*               should better be the last function you call...
1047  *
1048 D* Design:       wd@denx.de
1049 C* Coding:       wd@denx.de
1050 V* Verification: dzu@denx.de
1051  ***********************************************************************/
1052 void board_poweroff (void)
1053 {
1054     /* Turn battery off */
1055     ((volatile immap_t *)CFG_IMMR)->im_ioport.iop_pcdat &= ~(1 << (31 - 13));
1056
1057     while (1);
1058 }
1059
1060 #ifdef CONFIG_MODEM_SUPPORT
1061 static int key_pressed(void)
1062 {
1063         uchar kbd_data[KEYBD_DATALEN];
1064         uchar val;
1065
1066         /* Read keys */
1067         val = KEYBD_CMD_READ_KEYS;
1068         i2c_write (kbd_addr, 0, 0, &val, 1);
1069         i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
1070
1071         return (compare_magic(kbd_data, CONFIG_MODEM_KEY_MAGIC) == 0);
1072 }
1073 #endif  /* CONFIG_MODEM_SUPPORT */