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 /*------------------------- dspic io expander -----------------------*/
49 #define DSPIC_PON_STATUS_REG 0x80A
50 #define DSPIC_PON_INV_STATUS_REG 0x80C
51 #define DSPIC_PON_KEY_REG 0x810
52 /*------------------------- Keyboard controller -----------------------*/
54 #define KEYBD_CMD_READ_KEYS 0x01
55 #define KEYBD_CMD_READ_VERSION 0x02
56 #define KEYBD_CMD_READ_STATUS 0x03
57 #define KEYBD_CMD_RESET_ERRORS 0x10
60 #define KEYBD_STATUS_MASK 0x3F
61 #define KEYBD_STATUS_H_RESET 0x20
62 #define KEYBD_STATUS_BROWNOUT 0x10
63 #define KEYBD_STATUS_WD_RESET 0x08
64 #define KEYBD_STATUS_OVERLOAD 0x04
65 #define KEYBD_STATUS_ILLEGAL_WR 0x02
66 #define KEYBD_STATUS_ILLEGAL_RD 0x01
68 /* Number of bytes returned from Keyboard Controller */
69 #define KEYBD_VERSIONLEN 2 /* version information */
72 * This is different from the "old" lwmon dsPIC kbd controller
73 * implementation. Now the controller still answers with 9 bytes,
74 * but the last 3 bytes are always "0x06 0x07 0x08". So we just
75 * set the length to compare to 6 instead of 9.
77 #define KEYBD_DATALEN 6 /* normal key scan data */
79 /* maximum number of "magic" key codes that can be assigned */
81 static uchar kbd_addr = CONFIG_SYS_I2C_KEYBD_ADDR;
82 static uchar dspic_addr = CONFIG_SYS_I2C_DSPIC_IO_ADDR;
84 static uchar *key_match (uchar *);
86 #define KEYBD_SET_DEBUGMODE '#' /* Magic key to enable debug output */
88 /***********************************************************************
89 F* Function: int board_postclk_init (void) P*A*Z*
94 P* - 0 is always returned.
96 Z* Intention: This function is the board_postclk_init() method implementation
97 Z* for the lwmon board.
99 ***********************************************************************/
100 int board_postclk_init (void)
107 static void kbd_init (void)
109 uchar kbd_data[KEYBD_DATALEN];
110 uchar tmp_data[KEYBD_DATALEN];
114 i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
118 /* Forced by PIC. Delays <= 175us loose */
121 /* Read initial keyboard error code */
122 val = KEYBD_CMD_READ_STATUS;
123 i2c_write (kbd_addr, 0, 0, &val, 1);
124 i2c_read (kbd_addr, 0, 0, &errcd, 1);
125 /* clear unused bits */
126 errcd &= KEYBD_STATUS_MASK;
127 /* clear "irrelevant" bits. Recommended by Martin Rajek, LWN */
128 errcd &= ~(KEYBD_STATUS_H_RESET|KEYBD_STATUS_BROWNOUT);
130 gd->kbd_status |= errcd << 8;
132 /* Reset error code and verify */
133 val = KEYBD_CMD_RESET_ERRORS;
134 i2c_write (kbd_addr, 0, 0, &val, 1);
135 udelay(1000); /* delay NEEDED by keyboard PIC !!! */
137 val = KEYBD_CMD_READ_STATUS;
138 i2c_write (kbd_addr, 0, 0, &val, 1);
139 i2c_read (kbd_addr, 0, 0, &val, 1);
141 val &= KEYBD_STATUS_MASK; /* clear unused bits */
142 if (val) { /* permanent error, report it */
143 gd->kbd_status |= val;
148 * Read current keyboard state.
150 * After the error reset it may take some time before the
151 * keyboard PIC picks up a valid keyboard scan - the total
152 * scan time is approx. 1.6 ms (information by Martin Rajek,
153 * 28 Sep 2002). We read a couple of times for the keyboard
154 * to stabilize, using a big enough delay.
155 * 10 times should be enough. If the data is still changing,
156 * we use what we get :-(
159 memset (tmp_data, 0xFF, KEYBD_DATALEN); /* impossible value */
160 for (i=0; i<10; ++i) {
161 val = KEYBD_CMD_READ_KEYS;
162 i2c_write (kbd_addr, 0, 0, &val, 1);
163 i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
165 if (memcmp(kbd_data, tmp_data, KEYBD_DATALEN) == 0) {
166 /* consistent state, done */
169 /* remeber last state, delay, and retry */
170 memcpy (tmp_data, kbd_data, KEYBD_DATALEN);
176 /* Read a register from the dsPIC. */
177 int _dspic_read(ushort reg, ushort *data)
179 uchar buf[sizeof(*data)];
182 if (i2c_read(dspic_addr, reg, 2, buf, 2))
185 rval = i2c_read(dspic_addr, reg, sizeof(reg), buf, sizeof(*data));
186 *data = (buf[0] << 8) | buf[1];
192 /***********************************************************************
193 F* Function: int misc_init_r (void) P*A*Z*
198 P* - 0 is always returned, even in the case of a keyboard
201 Z* Intention: This function is the misc_init_r() method implementation
202 Z* for the lwmon board.
203 Z* The keyboard controller is initialized and the result
204 Z* of a read copied to the environment variable "keybd".
205 Z* If KEYBD_SET_DEBUGMODE is defined, a check is made for
206 Z* this key, and if found display to the LCD will be enabled.
207 Z* The keys in "keybd" are checked against the magic
208 Z* keycommands defined in the environment.
209 Z* See also key_match().
211 D* Design: wd@denx.de
212 C* Coding: wd@denx.de
213 V* Verification: dzu@denx.de
214 ***********************************************************************/
215 int misc_init_r_kbd (void)
217 uchar kbd_data[KEYBD_DATALEN];
218 char keybd_env[2 * KEYBD_DATALEN + 1];
219 uchar kbd_init_status = gd->kbd_status >> 8;
220 uchar kbd_status = gd->kbd_status;
222 ushort data, inv_data;
226 if (kbd_init_status) {
227 printf ("KEYBD: Error %02X\n", kbd_init_status);
229 if (kbd_status) { /* permanent error, report it */
230 printf ("*** Keyboard error code %02X ***\n", kbd_status);
231 sprintf (keybd_env, "%02X", kbd_status);
232 setenv ("keybd", keybd_env);
237 * Now we know that we have a working keyboard, so disable
238 * all output to the LCD except when a key press is detected.
241 if ((console_assign (stdout, "serial") < 0) ||
242 (console_assign (stderr, "serial") < 0)) {
243 printf ("Can't assign serial port as output device\n");
247 val = KEYBD_CMD_READ_VERSION;
248 i2c_write (kbd_addr, 0, 0, &val, 1);
249 i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_VERSIONLEN);
250 printf ("KEYBD: Version %d.%d\n", kbd_data[0], kbd_data[1]);
252 /* Read current keyboard state */
253 val = KEYBD_CMD_READ_KEYS;
254 i2c_write (kbd_addr, 0, 0, &val, 1);
255 i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
257 /* read out start key from bse01 received via can */
258 _dspic_read(DSPIC_PON_STATUS_REG, &data);
259 /* check highbyte from status register */
261 _dspic_read(DSPIC_PON_INV_STATUS_REG, &inv_data);
263 /* check inverse data */
264 if ((data+inv_data) == 0xFFFF) {
265 /* don't overwrite local key */
266 if (kbd_data[1] == 0) {
268 _dspic_read(DSPIC_PON_KEY_REG, &data);
271 kbd_data[1] = str[1];
272 kbd_data[2] = str[0];
273 printf("CAN received startkey: 0x%X\n", data);
278 for (i = 0; i < KEYBD_DATALEN; ++i) {
279 sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
282 setenv ("keybd", keybd_env);
284 str = strdup ((char *)key_match (kbd_data)); /* decode keys */
285 #ifdef KEYBD_SET_DEBUGMODE
286 if (kbd_data[0] == KEYBD_SET_DEBUGMODE) { /* set debug mode */
287 if ((console_assign (stdout, "lcd") < 0) ||
288 (console_assign (stderr, "lcd") < 0)) {
289 printf ("Can't assign LCD display as output device\n");
292 #endif /* KEYBD_SET_DEBUGMODE */
293 #ifdef CONFIG_PREBOOT /* automatically configure "preboot" command on key match */
294 setenv ("preboot", str); /* set or delete definition */
295 #endif /* CONFIG_PREBOOT */
302 #ifdef CONFIG_PREBOOT
304 static uchar kbd_magic_prefix[] = "key_magic";
305 static uchar kbd_command_prefix[] = "key_cmd";
307 static int compare_magic (uchar *kbd_data, uchar *str)
309 uchar compare[KEYBD_DATALEN-1];
313 /* Don't include modifier byte */
314 memcpy (compare, kbd_data+1, KEYBD_DATALEN-1);
316 for (; str != NULL; str = (*nxt) ? (uchar *)(nxt+1) : (uchar *)nxt) {
320 c = (uchar) simple_strtoul ((char *)str, (char **) (&nxt), 16);
322 if (str == (uchar *)nxt) { /* invalid character */
327 * Check if this key matches the input.
328 * Set matches to zero, so they match only once
329 * and we can find duplicates or extra keys
331 for (k = 0; k < sizeof(compare); ++k) {
332 if (compare[k] == '\0') /* only non-zero entries */
334 if (c == compare[k]) { /* found matching key */
339 if (k == sizeof(compare)) {
340 return -1; /* unmatched key */
345 * A full match leaves no keys in the `compare' array,
347 for (i = 0; i < sizeof(compare); ++i) {
357 /***********************************************************************
358 F* Function: static uchar *key_match (uchar *kbd_data) P*A*Z*
360 P* Parameters: uchar *kbd_data
361 P* - The keys to match against our magic definitions
363 P* Returnvalue: uchar *
364 P* - != NULL: Pointer to the corresponding command(s)
365 P* NULL: No magic is about to happen
367 Z* Intention: Check if pressed key(s) match magic sequence,
368 Z* and return the command string associated with that key(s).
370 Z* If no key press was decoded, NULL is returned.
372 Z* Note: the first character of the argument will be
373 Z* overwritten with the "magic charcter code" of the
374 Z* decoded key(s), or '\0'.
376 Z* Note: the string points to static environment data
377 Z* and must be saved before you call any function that
378 Z* modifies the environment.
380 D* Design: wd@denx.de
381 C* Coding: wd@denx.de
382 V* Verification: dzu@denx.de
383 ***********************************************************************/
384 static uchar *key_match (uchar *kbd_data)
386 char magic[sizeof (kbd_magic_prefix) + 1];
388 char *kbd_magic_keys;
391 * The following string defines the characters that can pe appended
392 * to "key_magic" to form the names of environment variables that
393 * hold "magic" key codes, i. e. such key codes that can cause
394 * pre-boot actions. If the string is empty (""), then only
395 * "key_magic" is checked (old behaviour); the string "125" causes
396 * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
398 if ((kbd_magic_keys = getenv ("magic_keys")) == NULL)
401 /* loop over all magic keys;
402 * use '\0' suffix in case of empty string
404 for (suffix=(uchar *)kbd_magic_keys; *suffix || suffix==(uchar *)kbd_magic_keys; ++suffix) {
405 sprintf (magic, "%s%c", kbd_magic_prefix, *suffix);
406 debug ("### Check magic \"%s\"\n", magic);
407 if (compare_magic(kbd_data, (uchar *)getenv(magic)) == 0) {
408 char cmd_name[sizeof (kbd_command_prefix) + 1];
411 sprintf (cmd_name, "%s%c", kbd_command_prefix, *suffix);
413 cmd = getenv (cmd_name);
414 debug ("### Set PREBOOT to $(%s): \"%s\"\n",
415 cmd_name, cmd ? cmd : "<<NULL>>");
417 return ((uchar *)cmd);
420 debug ("### Delete PREBOOT\n");
424 #endif /* CONFIG_PREBOOT */
426 /***********************************************************************
427 F* Function: int do_kbd (cmd_tbl_t *cmdtp, int flag,
428 F* int argc, char * const argv[]) P*A*Z*
430 P* Parameters: cmd_tbl_t *cmdtp
431 P* - Pointer to our command table entry
433 P* - If the CMD_FLAG_REPEAT bit is set, then this call is
437 P* char * const argv[]
438 P* - Array of the actual arguments
441 P* - 0 is always returned.
443 Z* Intention: Implement the "kbd" command.
444 Z* The keyboard status is read. The result is printed on
445 Z* the console and written into the "keybd" environment
448 D* Design: wd@denx.de
449 C* Coding: wd@denx.de
450 V* Verification: dzu@denx.de
451 ***********************************************************************/
452 int do_kbd (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
454 uchar kbd_data[KEYBD_DATALEN];
455 char keybd_env[2 * KEYBD_DATALEN + 1];
459 #if 0 /* Done in kbd_init */
460 i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
464 val = KEYBD_CMD_READ_KEYS;
465 i2c_write (kbd_addr, 0, 0, &val, 1);
466 i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
469 for (i = 0; i < KEYBD_DATALEN; ++i) {
470 sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
471 printf (" %02x", kbd_data[i]);
474 setenv ("keybd", keybd_env);
480 "read keyboard status",
484 /*----------------------------- Utilities -----------------------------*/
488 * Returns 1 if keys pressed to start the power-on long-running tests
489 * Called from board_init_f().
491 int post_hotkeys_pressed(void)
493 uchar kbd_data[KEYBD_DATALEN];
497 val = KEYBD_CMD_READ_KEYS;
498 i2c_write (kbd_addr, 0, 0, &val, 1);
499 i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
501 return (compare_magic(kbd_data, (uchar *)CONFIG_POST_KEY_MAGIC) == 0);