3 * Stefan Roese, DENX Software Engineering, sr@denx.de.
5 * (C) Copyright 2001, 2002
6 * DENX Software Engineering
7 * Wolfgang Denk, wd@denx.de
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 /* define DEBUG for debugging output (obviously ;-)) */
37 #include <linux/types.h>
38 #include <linux/string.h> /* for strdup */
40 DECLARE_GLOBAL_DATA_PTR
;
42 static void kbd_init (void);
43 static int compare_magic (uchar
*kbd_data
, uchar
*str
);
45 /*--------------------- Local macros and constants --------------------*/
46 #define _NOT_USED_ 0xFFFFFFFF
48 /*------------------------- Keyboard controller -----------------------*/
50 #define KEYBD_CMD_READ_KEYS 0x01
51 #define KEYBD_CMD_READ_VERSION 0x02
52 #define KEYBD_CMD_READ_STATUS 0x03
53 #define KEYBD_CMD_RESET_ERRORS 0x10
56 #define KEYBD_STATUS_MASK 0x3F
57 #define KEYBD_STATUS_H_RESET 0x20
58 #define KEYBD_STATUS_BROWNOUT 0x10
59 #define KEYBD_STATUS_WD_RESET 0x08
60 #define KEYBD_STATUS_OVERLOAD 0x04
61 #define KEYBD_STATUS_ILLEGAL_WR 0x02
62 #define KEYBD_STATUS_ILLEGAL_RD 0x01
64 /* Number of bytes returned from Keyboard Controller */
65 #define KEYBD_VERSIONLEN 2 /* version information */
68 * This is different from the "old" lwmon dsPIC kbd controller
69 * implementation. Now the controller still answers with 9 bytes,
70 * but the last 3 bytes are always "0x06 0x07 0x08". So we just
71 * set the length to compare to 6 instead of 9.
73 #define KEYBD_DATALEN 6 /* normal key scan data */
75 /* maximum number of "magic" key codes that can be assigned */
77 static uchar kbd_addr
= CONFIG_SYS_I2C_KEYBD_ADDR
;
79 static uchar
*key_match (uchar
*);
81 #define KEYBD_SET_DEBUGMODE '#' /* Magic key to enable debug output */
83 /***********************************************************************
84 F* Function: int board_postclk_init (void) P*A*Z*
89 P* - 0 is always returned.
91 Z* Intention: This function is the board_postclk_init() method implementation
92 Z* for the lwmon board.
94 ***********************************************************************/
95 int board_postclk_init (void)
102 static void kbd_init (void)
104 uchar kbd_data
[KEYBD_DATALEN
];
105 uchar tmp_data
[KEYBD_DATALEN
];
109 i2c_init (CONFIG_SYS_I2C_SPEED
, CONFIG_SYS_I2C_SLAVE
);
113 /* Forced by PIC. Delays <= 175us loose */
116 /* Read initial keyboard error code */
117 val
= KEYBD_CMD_READ_STATUS
;
118 i2c_write (kbd_addr
, 0, 0, &val
, 1);
119 i2c_read (kbd_addr
, 0, 0, &errcd
, 1);
120 /* clear unused bits */
121 errcd
&= KEYBD_STATUS_MASK
;
122 /* clear "irrelevant" bits. Recommended by Martin Rajek, LWN */
123 errcd
&= ~(KEYBD_STATUS_H_RESET
|KEYBD_STATUS_BROWNOUT
);
125 gd
->kbd_status
|= errcd
<< 8;
127 /* Reset error code and verify */
128 val
= KEYBD_CMD_RESET_ERRORS
;
129 i2c_write (kbd_addr
, 0, 0, &val
, 1);
130 udelay(1000); /* delay NEEDED by keyboard PIC !!! */
132 val
= KEYBD_CMD_READ_STATUS
;
133 i2c_write (kbd_addr
, 0, 0, &val
, 1);
134 i2c_read (kbd_addr
, 0, 0, &val
, 1);
136 val
&= KEYBD_STATUS_MASK
; /* clear unused bits */
137 if (val
) { /* permanent error, report it */
138 gd
->kbd_status
|= val
;
143 * Read current keyboard state.
145 * After the error reset it may take some time before the
146 * keyboard PIC picks up a valid keyboard scan - the total
147 * scan time is approx. 1.6 ms (information by Martin Rajek,
148 * 28 Sep 2002). We read a couple of times for the keyboard
149 * to stabilize, using a big enough delay.
150 * 10 times should be enough. If the data is still changing,
151 * we use what we get :-(
154 memset (tmp_data
, 0xFF, KEYBD_DATALEN
); /* impossible value */
155 for (i
=0; i
<10; ++i
) {
156 val
= KEYBD_CMD_READ_KEYS
;
157 i2c_write (kbd_addr
, 0, 0, &val
, 1);
158 i2c_read (kbd_addr
, 0, 0, kbd_data
, KEYBD_DATALEN
);
160 if (memcmp(kbd_data
, tmp_data
, KEYBD_DATALEN
) == 0) {
161 /* consistent state, done */
164 /* remeber last state, delay, and retry */
165 memcpy (tmp_data
, kbd_data
, KEYBD_DATALEN
);
170 /***********************************************************************
171 F* Function: int misc_init_r (void) P*A*Z*
176 P* - 0 is always returned, even in the case of a keyboard
179 Z* Intention: This function is the misc_init_r() method implementation
180 Z* for the lwmon board.
181 Z* The keyboard controller is initialized and the result
182 Z* of a read copied to the environment variable "keybd".
183 Z* If KEYBD_SET_DEBUGMODE is defined, a check is made for
184 Z* this key, and if found display to the LCD will be enabled.
185 Z* The keys in "keybd" are checked against the magic
186 Z* keycommands defined in the environment.
187 Z* See also key_match().
189 D* Design: wd@denx.de
190 C* Coding: wd@denx.de
191 V* Verification: dzu@denx.de
192 ***********************************************************************/
193 int misc_init_r_kbd (void)
195 uchar kbd_data
[KEYBD_DATALEN
];
196 char keybd_env
[2 * KEYBD_DATALEN
+ 1];
197 uchar kbd_init_status
= gd
->kbd_status
>> 8;
198 uchar kbd_status
= gd
->kbd_status
;
203 if (kbd_init_status
) {
204 printf ("KEYBD: Error %02X\n", kbd_init_status
);
206 if (kbd_status
) { /* permanent error, report it */
207 printf ("*** Keyboard error code %02X ***\n", kbd_status
);
208 sprintf (keybd_env
, "%02X", kbd_status
);
209 setenv ("keybd", keybd_env
);
214 * Now we know that we have a working keyboard, so disable
215 * all output to the LCD except when a key press is detected.
218 if ((console_assign (stdout
, "serial") < 0) ||
219 (console_assign (stderr
, "serial") < 0)) {
220 printf ("Can't assign serial port as output device\n");
224 val
= KEYBD_CMD_READ_VERSION
;
225 i2c_write (kbd_addr
, 0, 0, &val
, 1);
226 i2c_read (kbd_addr
, 0, 0, kbd_data
, KEYBD_VERSIONLEN
);
227 printf ("KEYBD: Version %d.%d\n", kbd_data
[0], kbd_data
[1]);
229 /* Read current keyboard state */
230 val
= KEYBD_CMD_READ_KEYS
;
231 i2c_write (kbd_addr
, 0, 0, &val
, 1);
232 i2c_read (kbd_addr
, 0, 0, kbd_data
, KEYBD_DATALEN
);
234 for (i
= 0; i
< KEYBD_DATALEN
; ++i
) {
235 sprintf (keybd_env
+ i
+ i
, "%02X", kbd_data
[i
]);
237 setenv ("keybd", keybd_env
);
239 str
= strdup ((char *)key_match (kbd_data
)); /* decode keys */
240 #ifdef KEYBD_SET_DEBUGMODE
241 if (kbd_data
[0] == KEYBD_SET_DEBUGMODE
) { /* set debug mode */
242 if ((console_assign (stdout
, "lcd") < 0) ||
243 (console_assign (stderr
, "lcd") < 0)) {
244 printf ("Can't assign LCD display as output device\n");
247 #endif /* KEYBD_SET_DEBUGMODE */
248 #ifdef CONFIG_PREBOOT /* automatically configure "preboot" command on key match */
249 setenv ("preboot", str
); /* set or delete definition */
250 #endif /* CONFIG_PREBOOT */
257 #ifdef CONFIG_PREBOOT
259 static uchar kbd_magic_prefix
[] = "key_magic";
260 static uchar kbd_command_prefix
[] = "key_cmd";
262 static int compare_magic (uchar
*kbd_data
, uchar
*str
)
264 uchar compare
[KEYBD_DATALEN
-1];
268 /* Don't include modifier byte */
269 memcpy (compare
, kbd_data
+1, KEYBD_DATALEN
-1);
271 for (; str
!= NULL
; str
= (*nxt
) ? (uchar
*)(nxt
+1) : (uchar
*)nxt
) {
275 c
= (uchar
) simple_strtoul ((char *)str
, (char **) (&nxt
), 16);
277 if (str
== (uchar
*)nxt
) { /* invalid character */
282 * Check if this key matches the input.
283 * Set matches to zero, so they match only once
284 * and we can find duplicates or extra keys
286 for (k
= 0; k
< sizeof(compare
); ++k
) {
287 if (compare
[k
] == '\0') /* only non-zero entries */
289 if (c
== compare
[k
]) { /* found matching key */
294 if (k
== sizeof(compare
)) {
295 return -1; /* unmatched key */
300 * A full match leaves no keys in the `compare' array,
302 for (i
= 0; i
< sizeof(compare
); ++i
) {
312 /***********************************************************************
313 F* Function: static uchar *key_match (uchar *kbd_data) P*A*Z*
315 P* Parameters: uchar *kbd_data
316 P* - The keys to match against our magic definitions
318 P* Returnvalue: uchar *
319 P* - != NULL: Pointer to the corresponding command(s)
320 P* NULL: No magic is about to happen
322 Z* Intention: Check if pressed key(s) match magic sequence,
323 Z* and return the command string associated with that key(s).
325 Z* If no key press was decoded, NULL is returned.
327 Z* Note: the first character of the argument will be
328 Z* overwritten with the "magic charcter code" of the
329 Z* decoded key(s), or '\0'.
331 Z* Note: the string points to static environment data
332 Z* and must be saved before you call any function that
333 Z* modifies the environment.
335 D* Design: wd@denx.de
336 C* Coding: wd@denx.de
337 V* Verification: dzu@denx.de
338 ***********************************************************************/
339 static uchar
*key_match (uchar
*kbd_data
)
341 char magic
[sizeof (kbd_magic_prefix
) + 1];
343 char *kbd_magic_keys
;
346 * The following string defines the characters that can pe appended
347 * to "key_magic" to form the names of environment variables that
348 * hold "magic" key codes, i. e. such key codes that can cause
349 * pre-boot actions. If the string is empty (""), then only
350 * "key_magic" is checked (old behaviour); the string "125" causes
351 * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
353 if ((kbd_magic_keys
= getenv ("magic_keys")) == NULL
)
356 /* loop over all magic keys;
357 * use '\0' suffix in case of empty string
359 for (suffix
=(uchar
*)kbd_magic_keys
; *suffix
|| suffix
==(uchar
*)kbd_magic_keys
; ++suffix
) {
360 sprintf (magic
, "%s%c", kbd_magic_prefix
, *suffix
);
361 debug ("### Check magic \"%s\"\n", magic
);
362 if (compare_magic(kbd_data
, (uchar
*)getenv(magic
)) == 0) {
363 char cmd_name
[sizeof (kbd_command_prefix
) + 1];
366 sprintf (cmd_name
, "%s%c", kbd_command_prefix
, *suffix
);
368 cmd
= getenv (cmd_name
);
369 debug ("### Set PREBOOT to $(%s): \"%s\"\n",
370 cmd_name
, cmd
? cmd
: "<<NULL>>");
372 return ((uchar
*)cmd
);
375 debug ("### Delete PREBOOT\n");
379 #endif /* CONFIG_PREBOOT */
381 /***********************************************************************
382 F* Function: int do_kbd (cmd_tbl_t *cmdtp, int flag,
383 F* int argc, char *argv[]) P*A*Z*
385 P* Parameters: cmd_tbl_t *cmdtp
386 P* - Pointer to our command table entry
388 P* - If the CMD_FLAG_REPEAT bit is set, then this call is
393 P* - Array of the actual arguments
396 P* - 0 is always returned.
398 Z* Intention: Implement the "kbd" command.
399 Z* The keyboard status is read. The result is printed on
400 Z* the console and written into the "keybd" environment
403 D* Design: wd@denx.de
404 C* Coding: wd@denx.de
405 V* Verification: dzu@denx.de
406 ***********************************************************************/
407 int do_kbd (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
409 uchar kbd_data
[KEYBD_DATALEN
];
410 char keybd_env
[2 * KEYBD_DATALEN
+ 1];
414 #if 0 /* Done in kbd_init */
415 i2c_init (CONFIG_SYS_I2C_SPEED
, CONFIG_SYS_I2C_SLAVE
);
419 val
= KEYBD_CMD_READ_KEYS
;
420 i2c_write (kbd_addr
, 0, 0, &val
, 1);
421 i2c_read (kbd_addr
, 0, 0, kbd_data
, KEYBD_DATALEN
);
424 for (i
= 0; i
< KEYBD_DATALEN
; ++i
) {
425 sprintf (keybd_env
+ i
+ i
, "%02X", kbd_data
[i
]);
426 printf (" %02x", kbd_data
[i
]);
429 setenv ("keybd", keybd_env
);
435 "kbd - read keyboard status\n",
439 /*----------------------------- Utilities -----------------------------*/
443 * Returns 1 if keys pressed to start the power-on long-running tests
444 * Called from board_init_f().
446 int post_hotkeys_pressed(void)
448 uchar kbd_data
[KEYBD_DATALEN
];
452 val
= KEYBD_CMD_READ_KEYS
;
453 i2c_write (kbd_addr
, 0, 0, &val
, 1);
454 i2c_read (kbd_addr
, 0, 0, kbd_data
, KEYBD_DATALEN
);
456 return (compare_magic(kbd_data
, (uchar
*)CONFIG_POST_KEY_MAGIC
) == 0);