blob: 5b2b41a97603d7991bc4a129389ba5b00303196e [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>
wdenke2211742002-11-02 23:30:20 +000041#include <malloc.h>
42#include <post.h>
wdenk281e00a2004-08-01 22:48:16 +000043#include <serial.h>
wdenke2211742002-11-02 23:30:20 +000044
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 *
wdenk8bde7f72003-06-27 21:31:46 +000076 * 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.
wdenke2211742002-11-02 23:30:20 +000079 *
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 *
wdenk8bde7f72003-06-27 21:31:46 +0000124 * 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.
wdenke2211742002-11-02 23:30:20 +0000127 *
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 */
wdenk8bde7f72003-06-27 21:31:46 +0000135 _NOT_USED_,
wdenke2211742002-11-02 23:30:20 +0000136 _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 */
wdenk8bde7f72003-06-27 21:31:46 +0000149 _NOT_USED_, _NOT_USED_,
wdenke2211742002-11-02 23:30:20 +0000150 _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{
wdenk8564acf2003-07-14 22:13:32 +0000188 puts ("Board: LICCON Konsole LCD2\n");
wdenke2211742002-11-02 23:30:20 +0000189 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
wdenk2535d602003-07-17 23:16:40 +0000250 memctl->memc_mamr |= MAMR_PTAE; /* refresh enabled */
wdenke2211742002-11-02 23:30:20 +0000251
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;
wdenk2535d602003-07-17 23:16:40 +0000280 memctl->memc_mamr = CFG_MAMR_9COL | MAMR_PTAE;
wdenke2211742002-11-02 23:30:20 +0000281 udelay (500);
282 } else { /* back to 8 columns */
283 size_b0 = size8;
wdenk2535d602003-07-17 23:16:40 +0000284 memctl->memc_mamr = CFG_MAMR_8COL | MAMR_PTAE;
wdenke2211742002-11-02 23:30:20 +0000285 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;
wdenke2211742002-11-02 23:30:20 +0000330
331 memctl->memc_mamr = mamr_value;
332
wdenkc83bf6a2004-01-06 22:38:14 +0000333 return (get_ram_size(base, maxsize));
wdenke2211742002-11-02 23:30:20 +0000334}
335
336/* ------------------------------------------------------------------------- */
337
338#ifndef PB_ENET_TENA
339# define PB_ENET_TENA ((uint)0x00002000) /* PB 18 */
340#endif
341
342/***********************************************************************
wdenkc837dcb2004-01-20 23:12:12 +0000343F* Function: int board_early_init_f (void) P*A*Z*
wdenke2211742002-11-02 23:30:20 +0000344 *
345P* Parameters: none
346P*
347P* Returnvalue: int
348P* - 0 is always returned.
349 *
wdenkc837dcb2004-01-20 23:12:12 +0000350Z* Intention: This function is the board_early_init_f() method implementation
wdenke2211742002-11-02 23:30:20 +0000351Z* for the lwmon board.
352Z* Disable Ethernet TENA on Port B.
353 *
354D* Design: wd@denx.de
355C* Coding: wd@denx.de
356V* Verification: dzu@denx.de
357 ***********************************************************************/
wdenkc837dcb2004-01-20 23:12:12 +0000358int board_early_init_f (void)
wdenke2211742002-11-02 23:30:20 +0000359{
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;
wdenk4532cb62003-04-27 22:52:51 +0000374
wdenke2211742002-11-02 23:30:20 +0000375 return (0);
376}
377
378/* ------------------------------------------------------------------------- */
379
380/***********************************************************************
381F* Function: void reset_phy (void) P*A*Z*
382 *
383P* Parameters: none
384P*
385P* Returnvalue: none
386 *
387Z* Intention: Reset the PHY. In the lwmon case we do this by the
388Z* signaling the PIC I/O expander.
389 *
390D* Design: wd@denx.de
391C* Coding: wd@denx.de
392V* Verification: dzu@denx.de
393 ***********************************************************************/
394void 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
438static uchar kbd_addr = CFG_I2C_KEYBD_ADDR;
439
440static uchar *key_match (uchar *);
441
442#define KEYBD_SET_DEBUGMODE '#' /* Magic key to enable debug output */
443
444/***********************************************************************
wdenk4532cb62003-04-27 22:52:51 +0000445F* Function: int board_postclk_init (void) P*A*Z*
wdenke2211742002-11-02 23:30:20 +0000446 *
447P* Parameters: none
448P*
449P* Returnvalue: int
wdenk4532cb62003-04-27 22:52:51 +0000450P* - 0 is always returned.
wdenke2211742002-11-02 23:30:20 +0000451 *
wdenk4532cb62003-04-27 22:52:51 +0000452Z* Intention: This function is the board_postclk_init() method implementation
wdenke2211742002-11-02 23:30:20 +0000453Z* for the lwmon board.
wdenke2211742002-11-02 23:30:20 +0000454 *
wdenke2211742002-11-02 23:30:20 +0000455 ***********************************************************************/
wdenk4532cb62003-04-27 22:52:51 +0000456int board_postclk_init (void)
wdenke2211742002-11-02 23:30:20 +0000457{
wdenk4532cb62003-04-27 22:52:51 +0000458 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
wdenk281e00a2004-08-01 22:48:16 +0000472struct 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
wdenk4532cb62003-04-27 22:52:51 +0000479static void kbd_init (void)
480{
481 DECLARE_GLOBAL_DATA_PTR;
482
wdenke2211742002-11-02 23:30:20 +0000483 uchar kbd_data[KEYBD_DATALEN];
484 uchar tmp_data[KEYBD_DATALEN];
wdenke2211742002-11-02 23:30:20 +0000485 uchar val, errcd;
wdenke2211742002-11-02 23:30:20 +0000486 int i;
487
488 i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
wdenk8bde7f72003-06-27 21:31:46 +0000489
wdenk4532cb62003-04-27 22:52:51 +0000490 gd->kbd_status = 0;
wdenke2211742002-11-02 23:30:20 +0000491
wdenkd1cbe852003-06-28 17:24:46 +0000492 /* Forced by PIC. Delays <= 175us loose */
493 udelay(1000);
494
wdenke2211742002-11-02 23:30:20 +0000495 /* 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) {
wdenk4532cb62003-04-27 22:52:51 +0000504 gd->kbd_status |= errcd << 8;
wdenke2211742002-11-02 23:30:20 +0000505 }
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 */
wdenk4532cb62003-04-27 22:52:51 +0000517 gd->kbd_status |= val;
518 return;
wdenke2211742002-11-02 23:30:20 +0000519 }
520
521 /*
wdenke2211742002-11-02 23:30:20 +0000522 * Read current keyboard state.
523 *
wdenk8bde7f72003-06-27 21:31:46 +0000524 * 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 :-(
wdenke2211742002-11-02 23:30:20 +0000531 */
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 }
wdenk4532cb62003-04-27 22:52:51 +0000547}
548
549/***********************************************************************
550F* Function: int misc_init_r (void) P*A*Z*
551 *
552P* Parameters: none
553P*
554P* Returnvalue: int
555P* - 0 is always returned, even in the case of a keyboard
556P* error.
557 *
558Z* Intention: This function is the misc_init_r() method implementation
559Z* for the lwmon board.
560Z* The keyboard controller is initialized and the result
561Z* of a read copied to the environment variable "keybd".
562Z* If KEYBD_SET_DEBUGMODE is defined, a check is made for
563Z* this key, and if found display to the LCD will be enabled.
564Z* The keys in "keybd" are checked against the magic
565Z* keycommands defined in the environment.
566Z* See also key_match().
567 *
568D* Design: wd@denx.de
569C* Coding: wd@denx.de
570V* Verification: dzu@denx.de
571 ***********************************************************************/
572int 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);
wdenke2211742002-11-02 23:30:20 +0000614
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
640static uchar kbd_magic_prefix[] = "key_magic";
641static uchar kbd_command_prefix[] = "key_cmd";
642
wdenk4532cb62003-04-27 22:52:51 +0000643static int compare_magic (uchar *kbd_data, uchar *str)
644{
645 uchar compare[KEYBD_DATALEN-1];
wdenkeedcd072004-09-08 22:03:11 +0000646 char *nxt;
wdenk4532cb62003-04-27 22:52:51 +0000647 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
wdenkeedcd072004-09-08 22:03:11 +0000658 if (str == (uchar *)nxt) { /* invalid character */
wdenk4532cb62003-04-27 22:52:51 +0000659 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
wdenke2211742002-11-02 23:30:20 +0000693/***********************************************************************
694F* Function: static uchar *key_match (uchar *kbd_data) P*A*Z*
695 *
696P* Parameters: uchar *kbd_data
697P* - The keys to match against our magic definitions
698P*
699P* Returnvalue: uchar *
700P* - != NULL: Pointer to the corresponding command(s)
701P* NULL: No magic is about to happen
702 *
703Z* Intention: Check if pressed key(s) match magic sequence,
704Z* and return the command string associated with that key(s).
705Z*
706Z* If no key press was decoded, NULL is returned.
707Z*
708Z* Note: the first character of the argument will be
709Z* overwritten with the "magic charcter code" of the
710Z* decoded key(s), or '\0'.
711Z*
712Z* Note: the string points to static environment data
713Z* and must be saved before you call any function that
714Z* modifies the environment.
715 *
716D* Design: wd@denx.de
717C* Coding: wd@denx.de
718V* Verification: dzu@denx.de
719 ***********************************************************************/
720static uchar *key_match (uchar *kbd_data)
721{
wdenke2211742002-11-02 23:30:20 +0000722 uchar magic[sizeof (kbd_magic_prefix) + 1];
wdenk4532cb62003-04-27 22:52:51 +0000723 uchar *suffix;
wdenke2211742002-11-02 23:30:20 +0000724 uchar *kbd_magic_keys;
wdenke2211742002-11-02 23:30:20 +0000725
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
wdenk4532cb62003-04-27 22:52:51 +0000745 if (compare_magic(kbd_data, getenv(magic)) == 0) {
wdenke2211742002-11-02 23:30:20 +0000746 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/***********************************************************************
772F* Function: int do_pic (cmd_tbl_t *cmdtp, int flag,
773F* int argc, char *argv[]) P*A*Z*
774 *
775P* Parameters: cmd_tbl_t *cmdtp
776P* - Pointer to our command table entry
777P* int flag
778P* - If the CMD_FLAG_REPEAT bit is set, then this call is
779P* a repetition
780P* int argc
781P* - Argument count
782P* char *argv[]
783P* - Array of the actual arguments
784P*
785P* Returnvalue: int
786P* - 0 The command was handled successfully
787P* 1 An error occurred
788 *
789Z* Intention: Implement the "pic [read|write]" commands.
790Z* The read subcommand takes one argument, the register,
791Z* whereas the write command takes two, the register and
792Z* the new value.
793 *
794D* Design: wd@denx.de
795C* Coding: wd@denx.de
796V* Verification: dzu@denx.de
797 ***********************************************************************/
798int 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}
wdenk0d498392003-07-01 21:06:45 +0000830U_BOOT_CMD(
831 pic, 4, 1, do_pic,
wdenk8bde7f72003-06-27 21:31:46 +0000832 "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);
wdenke2211742002-11-02 23:30:20 +0000836
837/***********************************************************************
838F* Function: int do_kbd (cmd_tbl_t *cmdtp, int flag,
839F* int argc, char *argv[]) P*A*Z*
840 *
841P* Parameters: cmd_tbl_t *cmdtp
842P* - Pointer to our command table entry
843P* int flag
844P* - If the CMD_FLAG_REPEAT bit is set, then this call is
845P* a repetition
846P* int argc
847P* - Argument count
848P* char *argv[]
849P* - Array of the actual arguments
850P*
851P* Returnvalue: int
852P* - 0 is always returned.
853 *
854Z* Intention: Implement the "kbd" command.
855Z* The keyboard status is read. The result is printed on
856Z* the console and written into the "keybd" environment
857Z* variable.
858 *
859D* Design: wd@denx.de
860C* Coding: wd@denx.de
861V* Verification: dzu@denx.de
862 ***********************************************************************/
863int 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
wdenk4532cb62003-04-27 22:52:51 +0000870#if 0 /* Done in kbd_init */
wdenke2211742002-11-02 23:30:20 +0000871 i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
wdenk4532cb62003-04-27 22:52:51 +0000872#endif
wdenke2211742002-11-02 23:30:20 +0000873
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
wdenk0d498392003-07-01 21:06:45 +0000889U_BOOT_CMD(
890 kbd, 1, 1, do_kbd,
wdenk8bde7f72003-06-27 21:31:46 +0000891 "kbd - read keyboard status\n",
892 NULL
893);
894
wdenke2211742002-11-02 23:30:20 +0000895/* Read and set LSB switch */
wdenkd0fb80c2003-01-11 09:48:40 +0000896#define CFG_PC_TXD1_ENA 0x0008 /* PC.12 */
wdenke2211742002-11-02 23:30:20 +0000897
898/***********************************************************************
899F* Function: int do_lsb (cmd_tbl_t *cmdtp, int flag,
900F* int argc, char *argv[]) P*A*Z*
901 *
902P* Parameters: cmd_tbl_t *cmdtp
903P* - Pointer to our command table entry
904P* int flag
905P* - If the CMD_FLAG_REPEAT bit is set, then this call is
906P* a repetition
907P* int argc
908P* - Argument count
909P* char *argv[]
910P* - Array of the actual arguments
911P*
912P* Returnvalue: int
913P* - 0 The command was handled successfully
914P* 1 An error occurred
915 *
916Z* Intention: Implement the "lsb [on|off]" commands.
917Z* The lsb is switched according to the first parameter by
918Z* by signaling the PIC I/O expander.
919Z* Called with no arguments, the current setting is
920Z* printed.
921 *
922D* Design: wd@denx.de
923C* Coding: wd@denx.de
924V* Verification: dzu@denx.de
925 ***********************************************************************/
926int 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
wdenk0d498392003-07-01 21:06:45 +0000961U_BOOT_CMD(
962 lsb, 2, 1, do_lsb,
wdenk8bde7f72003-06-27 21:31:46 +0000963 "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
wdenke2211742002-11-02 23:30:20 +0000969#endif /* CFG_CMD_BSP */
970
971/*----------------------------- Utilities -----------------------------*/
972/***********************************************************************
973F* Function: uchar pic_read (uchar reg) P*A*Z*
974 *
975P* Parameters: uchar reg
976P* - Register to read
977P*
978P* Returnvalue: uchar
979P* - Value read from register
980 *
981Z* Intention: Read a register from the PIC I/O expander.
982 *
983D* Design: wd@denx.de
984C* Coding: wd@denx.de
985V* Verification: dzu@denx.de
986 ***********************************************************************/
987uchar pic_read (uchar reg)
988{
989 return (i2c_reg_read (CFG_I2C_PICIO_ADDR, reg));
990}
991
992/***********************************************************************
993F* Function: void pic_write (uchar reg, uchar val) P*A*Z*
994 *
995P* Parameters: uchar reg
996P* - Register to read
997P* uchar val
998P* - Value to write
999P*
1000P* Returnvalue: none
1001 *
1002Z* Intention: Write to a register on the PIC I/O expander.
1003 *
1004D* Design: wd@denx.de
1005C* Coding: wd@denx.de
1006V* Verification: dzu@denx.de
1007 ***********************************************************************/
1008void pic_write (uchar reg, uchar val)
1009{
1010 i2c_reg_write (CFG_I2C_PICIO_ADDR, reg, val);
1011}
1012
1013/*---------------------- Board Control Functions ----------------------*/
1014/***********************************************************************
1015F* Function: void board_poweroff (void) P*A*Z*
1016 *
1017P* Parameters: none
1018P*
1019P* Returnvalue: none
1020 *
1021Z* Intention: Turn off the battery power and loop endless, so this
1022Z* should better be the last function you call...
1023 *
1024D* Design: wd@denx.de
1025C* Coding: wd@denx.de
1026V* Verification: dzu@denx.de
1027 ***********************************************************************/
1028void 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}
wdenk4532cb62003-04-27 22:52:51 +00001035
1036#ifdef CONFIG_MODEM_SUPPORT
1037static 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 */
wdenk8564acf2003-07-14 22:13:32 +00001050
1051#ifdef CONFIG_POST
wdenk945af8d2003-07-16 21:53:01 +00001052/*
wdenk8564acf2003-07-14 22:13:32 +00001053 * Returns 1 if keys pressed to start the power-on long-running tests
1054 * Called from board_init_f().
1055 */
wdenk27b207f2003-07-24 23:38:38 +00001056int post_hotkeys_pressed(void)
wdenk8564acf2003-07-14 22:13:32 +00001057{
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
wdenk6dff5522003-07-15 07:45:49 +00001066 return (compare_magic(kbd_data, CONFIG_POST_KEY_MAGIC) == 0);
wdenk8564acf2003-07-14 22:13:32 +00001067}
1068#endif