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