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 * SPDX-License-Identifier: GPL-2.0+
12 /* define DEBUG for debugging output (obviously ;-)) */
24 #include <linux/types.h>
25 #include <linux/string.h> /* for strdup */
27 DECLARE_GLOBAL_DATA_PTR;
29 static void kbd_init (void);
30 static int compare_magic (uchar *kbd_data, uchar *str);
32 /*--------------------- Local macros and constants --------------------*/
33 #define _NOT_USED_ 0xFFFFFFFF
35 /*------------------------- dspic io expander -----------------------*/
36 #define DSPIC_PON_STATUS_REG 0x80A
37 #define DSPIC_PON_INV_STATUS_REG 0x80C
38 #define DSPIC_PON_KEY_REG 0x810
39 /*------------------------- Keyboard controller -----------------------*/
41 #define KEYBD_CMD_READ_KEYS 0x01
42 #define KEYBD_CMD_READ_VERSION 0x02
43 #define KEYBD_CMD_READ_STATUS 0x03
44 #define KEYBD_CMD_RESET_ERRORS 0x10
47 #define KEYBD_STATUS_MASK 0x3F
48 #define KEYBD_STATUS_H_RESET 0x20
49 #define KEYBD_STATUS_BROWNOUT 0x10
50 #define KEYBD_STATUS_WD_RESET 0x08
51 #define KEYBD_STATUS_OVERLOAD 0x04
52 #define KEYBD_STATUS_ILLEGAL_WR 0x02
53 #define KEYBD_STATUS_ILLEGAL_RD 0x01
55 /* Number of bytes returned from Keyboard Controller */
56 #define KEYBD_VERSIONLEN 2 /* version information */
59 * This is different from the "old" lwmon dsPIC kbd controller
60 * implementation. Now the controller still answers with 9 bytes,
61 * but the last 3 bytes are always "0x06 0x07 0x08". So we just
62 * set the length to compare to 6 instead of 9.
64 #define KEYBD_DATALEN 6 /* normal key scan data */
66 /* maximum number of "magic" key codes that can be assigned */
68 static uchar kbd_addr = CONFIG_SYS_I2C_KEYBD_ADDR;
69 static uchar dspic_addr = CONFIG_SYS_I2C_DSPIC_IO_ADDR;
71 static uchar *key_match (uchar *);
73 #define KEYBD_SET_DEBUGMODE '#' /* Magic key to enable debug output */
75 /***********************************************************************
76 F* Function: int board_postclk_init (void) P*A*Z*
81 P* - 0 is always returned.
83 Z* Intention: This function is the board_postclk_init() method implementation
84 Z* for the lwmon board.
86 ***********************************************************************/
87 int board_postclk_init (void)
94 static void kbd_init (void)
96 uchar kbd_data[KEYBD_DATALEN];
97 uchar tmp_data[KEYBD_DATALEN];
103 gd->arch.kbd_status = 0;
105 /* Forced by PIC. Delays <= 175us loose */
108 /* Read initial keyboard error code */
109 val = KEYBD_CMD_READ_STATUS;
110 i2c_write (kbd_addr, 0, 0, &val, 1);
111 i2c_read (kbd_addr, 0, 0, &errcd, 1);
112 /* clear unused bits */
113 errcd &= KEYBD_STATUS_MASK;
114 /* clear "irrelevant" bits. Recommended by Martin Rajek, LWN */
115 errcd &= ~(KEYBD_STATUS_H_RESET|KEYBD_STATUS_BROWNOUT);
117 gd->arch.kbd_status |= errcd << 8;
119 /* Reset error code and verify */
120 val = KEYBD_CMD_RESET_ERRORS;
121 i2c_write (kbd_addr, 0, 0, &val, 1);
122 udelay(1000); /* delay NEEDED by keyboard PIC !!! */
124 val = KEYBD_CMD_READ_STATUS;
125 i2c_write (kbd_addr, 0, 0, &val, 1);
126 i2c_read (kbd_addr, 0, 0, &val, 1);
128 val &= KEYBD_STATUS_MASK; /* clear unused bits */
129 if (val) { /* permanent error, report it */
130 gd->arch.kbd_status |= val;
135 * Read current keyboard state.
137 * After the error reset it may take some time before the
138 * keyboard PIC picks up a valid keyboard scan - the total
139 * scan time is approx. 1.6 ms (information by Martin Rajek,
140 * 28 Sep 2002). We read a couple of times for the keyboard
141 * to stabilize, using a big enough delay.
142 * 10 times should be enough. If the data is still changing,
143 * we use what we get :-(
146 memset (tmp_data, 0xFF, KEYBD_DATALEN); /* impossible value */
147 for (i=0; i<10; ++i) {
148 val = KEYBD_CMD_READ_KEYS;
149 i2c_write (kbd_addr, 0, 0, &val, 1);
150 i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
152 if (memcmp(kbd_data, tmp_data, KEYBD_DATALEN) == 0) {
153 /* consistent state, done */
156 /* remeber last state, delay, and retry */
157 memcpy (tmp_data, kbd_data, KEYBD_DATALEN);
163 /* Read a register from the dsPIC. */
164 int _dspic_read(ushort reg, ushort *data)
166 uchar buf[sizeof(*data)];
169 if (i2c_read(dspic_addr, reg, 2, buf, 2))
172 rval = i2c_read(dspic_addr, reg, sizeof(reg), buf, sizeof(*data));
173 *data = (buf[0] << 8) | buf[1];
179 /***********************************************************************
180 F* Function: int misc_init_r (void) P*A*Z*
185 P* - 0 is always returned, even in the case of a keyboard
188 Z* Intention: This function is the misc_init_r() method implementation
189 Z* for the lwmon board.
190 Z* The keyboard controller is initialized and the result
191 Z* of a read copied to the environment variable "keybd".
192 Z* If KEYBD_SET_DEBUGMODE is defined, a check is made for
193 Z* this key, and if found display to the LCD will be enabled.
194 Z* The keys in "keybd" are checked against the magic
195 Z* keycommands defined in the environment.
196 Z* See also key_match().
198 D* Design: wd@denx.de
199 C* Coding: wd@denx.de
200 V* Verification: dzu@denx.de
201 ***********************************************************************/
202 int misc_init_r_kbd (void)
204 uchar kbd_data[KEYBD_DATALEN];
205 char keybd_env[2 * KEYBD_DATALEN + 1];
206 uchar kbd_init_status = gd->arch.kbd_status >> 8;
207 uchar kbd_status = gd->arch.kbd_status;
209 ushort data, inv_data;
213 if (kbd_init_status) {
214 printf ("KEYBD: Error %02X\n", kbd_init_status);
216 if (kbd_status) { /* permanent error, report it */
217 printf ("*** Keyboard error code %02X ***\n", kbd_status);
218 sprintf (keybd_env, "%02X", kbd_status);
219 setenv ("keybd", keybd_env);
224 * Now we know that we have a working keyboard, so disable
225 * all output to the LCD except when a key press is detected.
228 if ((console_assign (stdout, "serial") < 0) ||
229 (console_assign (stderr, "serial") < 0)) {
230 printf ("Can't assign serial port as output device\n");
234 val = KEYBD_CMD_READ_VERSION;
235 i2c_write (kbd_addr, 0, 0, &val, 1);
236 i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_VERSIONLEN);
237 printf ("KEYBD: Version %d.%d\n", kbd_data[0], kbd_data[1]);
239 /* Read current keyboard state */
240 val = KEYBD_CMD_READ_KEYS;
241 i2c_write (kbd_addr, 0, 0, &val, 1);
242 i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
244 /* read out start key from bse01 received via can */
245 _dspic_read(DSPIC_PON_STATUS_REG, &data);
246 /* check highbyte from status register */
248 _dspic_read(DSPIC_PON_INV_STATUS_REG, &inv_data);
250 /* check inverse data */
251 if ((data+inv_data) == 0xFFFF) {
252 /* don't overwrite local key */
253 if (kbd_data[1] == 0) {
255 _dspic_read(DSPIC_PON_KEY_REG, &data);
258 kbd_data[1] = str[1];
259 kbd_data[2] = str[0];
260 printf("CAN received startkey: 0x%X\n", data);
265 for (i = 0; i < KEYBD_DATALEN; ++i) {
266 sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
269 setenv ("keybd", keybd_env);
271 str = strdup ((char *)key_match (kbd_data)); /* decode keys */
272 #ifdef KEYBD_SET_DEBUGMODE
273 if (kbd_data[0] == KEYBD_SET_DEBUGMODE) { /* set debug mode */
274 if ((console_assign (stdout, "lcd") < 0) ||
275 (console_assign (stderr, "lcd") < 0)) {
276 printf ("Can't assign LCD display as output device\n");
279 #endif /* KEYBD_SET_DEBUGMODE */
280 #ifdef CONFIG_PREBOOT /* automatically configure "preboot" command on key match */
281 setenv ("preboot", str); /* set or delete definition */
282 #endif /* CONFIG_PREBOOT */
289 #ifdef CONFIG_PREBOOT
291 static uchar kbd_magic_prefix[] = "key_magic";
292 static uchar kbd_command_prefix[] = "key_cmd";
294 static int compare_magic (uchar *kbd_data, uchar *str)
296 uchar compare[KEYBD_DATALEN-1];
300 /* Don't include modifier byte */
301 memcpy (compare, kbd_data+1, KEYBD_DATALEN-1);
303 for (; str != NULL; str = (*nxt) ? (uchar *)(nxt+1) : (uchar *)nxt) {
307 c = (uchar) simple_strtoul ((char *)str, (char **) (&nxt), 16);
309 if (str == (uchar *)nxt) { /* invalid character */
314 * Check if this key matches the input.
315 * Set matches to zero, so they match only once
316 * and we can find duplicates or extra keys
318 for (k = 0; k < sizeof(compare); ++k) {
319 if (compare[k] == '\0') /* only non-zero entries */
321 if (c == compare[k]) { /* found matching key */
326 if (k == sizeof(compare)) {
327 return -1; /* unmatched key */
332 * A full match leaves no keys in the `compare' array,
334 for (i = 0; i < sizeof(compare); ++i) {
344 /***********************************************************************
345 F* Function: static uchar *key_match (uchar *kbd_data) P*A*Z*
347 P* Parameters: uchar *kbd_data
348 P* - The keys to match against our magic definitions
350 P* Returnvalue: uchar *
351 P* - != NULL: Pointer to the corresponding command(s)
352 P* NULL: No magic is about to happen
354 Z* Intention: Check if pressed key(s) match magic sequence,
355 Z* and return the command string associated with that key(s).
357 Z* If no key press was decoded, NULL is returned.
359 Z* Note: the first character of the argument will be
360 Z* overwritten with the "magic charcter code" of the
361 Z* decoded key(s), or '\0'.
363 Z* Note: the string points to static environment data
364 Z* and must be saved before you call any function that
365 Z* modifies the environment.
367 D* Design: wd@denx.de
368 C* Coding: wd@denx.de
369 V* Verification: dzu@denx.de
370 ***********************************************************************/
371 static uchar *key_match (uchar *kbd_data)
373 char magic[sizeof (kbd_magic_prefix) + 1];
375 char *kbd_magic_keys;
378 * The following string defines the characters that can pe appended
379 * to "key_magic" to form the names of environment variables that
380 * hold "magic" key codes, i. e. such key codes that can cause
381 * pre-boot actions. If the string is empty (""), then only
382 * "key_magic" is checked (old behaviour); the string "125" causes
383 * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
385 if ((kbd_magic_keys = getenv ("magic_keys")) == NULL)
388 /* loop over all magic keys;
389 * use '\0' suffix in case of empty string
391 for (suffix=(uchar *)kbd_magic_keys; *suffix || suffix==(uchar *)kbd_magic_keys; ++suffix) {
392 sprintf (magic, "%s%c", kbd_magic_prefix, *suffix);
393 debug ("### Check magic \"%s\"\n", magic);
394 if (compare_magic(kbd_data, (uchar *)getenv(magic)) == 0) {
395 char cmd_name[sizeof (kbd_command_prefix) + 1];
398 sprintf (cmd_name, "%s%c", kbd_command_prefix, *suffix);
400 cmd = getenv (cmd_name);
401 debug ("### Set PREBOOT to $(%s): \"%s\"\n",
402 cmd_name, cmd ? cmd : "<<NULL>>");
404 return ((uchar *)cmd);
407 debug ("### Delete PREBOOT\n");
411 #endif /* CONFIG_PREBOOT */
413 /***********************************************************************
414 F* Function: int do_kbd (cmd_tbl_t *cmdtp, int flag,
415 F* int argc, char * const argv[]) P*A*Z*
417 P* Parameters: cmd_tbl_t *cmdtp
418 P* - Pointer to our command table entry
420 P* - If the CMD_FLAG_REPEAT bit is set, then this call is
424 P* char * const argv[]
425 P* - Array of the actual arguments
428 P* - 0 is always returned.
430 Z* Intention: Implement the "kbd" command.
431 Z* The keyboard status is read. The result is printed on
432 Z* the console and written into the "keybd" environment
435 D* Design: wd@denx.de
436 C* Coding: wd@denx.de
437 V* Verification: dzu@denx.de
438 ***********************************************************************/
439 int do_kbd (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
441 uchar kbd_data[KEYBD_DATALEN];
442 char keybd_env[2 * KEYBD_DATALEN + 1];
446 #if 0 /* Done in kbd_init */
447 i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
451 val = KEYBD_CMD_READ_KEYS;
452 i2c_write (kbd_addr, 0, 0, &val, 1);
453 i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
456 for (i = 0; i < KEYBD_DATALEN; ++i) {
457 sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
458 printf (" %02x", kbd_data[i]);
461 setenv ("keybd", keybd_env);
467 "read keyboard status",
471 /*----------------------------- Utilities -----------------------------*/
475 * Returns 1 if keys pressed to start the power-on long-running tests
476 * Called from board_init_f().
478 int post_hotkeys_pressed(void)
480 uchar kbd_data[KEYBD_DATALEN];
484 val = KEYBD_CMD_READ_KEYS;
485 i2c_write (kbd_addr, 0, 0, &val, 1);
486 i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
488 return (compare_magic(kbd_data, (uchar *)CONFIG_POST_KEY_MAGIC) == 0);