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 ;-)) */
25 #include <linux/types.h>
26 #include <linux/string.h> /* for strdup */
28 DECLARE_GLOBAL_DATA_PTR
;
30 static void kbd_init (void);
31 static int compare_magic (uchar
*kbd_data
, uchar
*str
);
33 /*--------------------- Local macros and constants --------------------*/
34 #define _NOT_USED_ 0xFFFFFFFF
36 /*------------------------- dspic io expander -----------------------*/
37 #define DSPIC_PON_STATUS_REG 0x80A
38 #define DSPIC_PON_INV_STATUS_REG 0x80C
39 #define DSPIC_PON_KEY_REG 0x810
40 /*------------------------- Keyboard controller -----------------------*/
42 #define KEYBD_CMD_READ_KEYS 0x01
43 #define KEYBD_CMD_READ_VERSION 0x02
44 #define KEYBD_CMD_READ_STATUS 0x03
45 #define KEYBD_CMD_RESET_ERRORS 0x10
48 #define KEYBD_STATUS_MASK 0x3F
49 #define KEYBD_STATUS_H_RESET 0x20
50 #define KEYBD_STATUS_BROWNOUT 0x10
51 #define KEYBD_STATUS_WD_RESET 0x08
52 #define KEYBD_STATUS_OVERLOAD 0x04
53 #define KEYBD_STATUS_ILLEGAL_WR 0x02
54 #define KEYBD_STATUS_ILLEGAL_RD 0x01
56 /* Number of bytes returned from Keyboard Controller */
57 #define KEYBD_VERSIONLEN 2 /* version information */
60 * This is different from the "old" lwmon dsPIC kbd controller
61 * implementation. Now the controller still answers with 9 bytes,
62 * but the last 3 bytes are always "0x06 0x07 0x08". So we just
63 * set the length to compare to 6 instead of 9.
65 #define KEYBD_DATALEN 6 /* normal key scan data */
67 /* maximum number of "magic" key codes that can be assigned */
69 static uchar kbd_addr
= CONFIG_SYS_I2C_KEYBD_ADDR
;
70 static uchar dspic_addr
= CONFIG_SYS_I2C_DSPIC_IO_ADDR
;
72 static uchar
*key_match (uchar
*);
74 #define KEYBD_SET_DEBUGMODE '#' /* Magic key to enable debug output */
76 /***********************************************************************
77 F* Function: int board_postclk_init (void) P*A*Z*
82 P* - 0 is always returned.
84 Z* Intention: This function is the board_postclk_init() method implementation
85 Z* for the lwmon board.
87 ***********************************************************************/
88 int board_postclk_init (void)
95 static void kbd_init (void)
97 uchar kbd_data
[KEYBD_DATALEN
];
98 uchar tmp_data
[KEYBD_DATALEN
];
104 gd
->arch
.kbd_status
= 0;
106 /* Forced by PIC. Delays <= 175us loose */
109 /* Read initial keyboard error code */
110 val
= KEYBD_CMD_READ_STATUS
;
111 i2c_write (kbd_addr
, 0, 0, &val
, 1);
112 i2c_read (kbd_addr
, 0, 0, &errcd
, 1);
113 /* clear unused bits */
114 errcd
&= KEYBD_STATUS_MASK
;
115 /* clear "irrelevant" bits. Recommended by Martin Rajek, LWN */
116 errcd
&= ~(KEYBD_STATUS_H_RESET
|KEYBD_STATUS_BROWNOUT
);
118 gd
->arch
.kbd_status
|= errcd
<< 8;
120 /* Reset error code and verify */
121 val
= KEYBD_CMD_RESET_ERRORS
;
122 i2c_write (kbd_addr
, 0, 0, &val
, 1);
123 udelay(1000); /* delay NEEDED by keyboard PIC !!! */
125 val
= KEYBD_CMD_READ_STATUS
;
126 i2c_write (kbd_addr
, 0, 0, &val
, 1);
127 i2c_read (kbd_addr
, 0, 0, &val
, 1);
129 val
&= KEYBD_STATUS_MASK
; /* clear unused bits */
130 if (val
) { /* permanent error, report it */
131 gd
->arch
.kbd_status
|= val
;
136 * Read current keyboard state.
138 * After the error reset it may take some time before the
139 * keyboard PIC picks up a valid keyboard scan - the total
140 * scan time is approx. 1.6 ms (information by Martin Rajek,
141 * 28 Sep 2002). We read a couple of times for the keyboard
142 * to stabilize, using a big enough delay.
143 * 10 times should be enough. If the data is still changing,
144 * we use what we get :-(
147 memset (tmp_data
, 0xFF, KEYBD_DATALEN
); /* impossible value */
148 for (i
=0; i
<10; ++i
) {
149 val
= KEYBD_CMD_READ_KEYS
;
150 i2c_write (kbd_addr
, 0, 0, &val
, 1);
151 i2c_read (kbd_addr
, 0, 0, kbd_data
, KEYBD_DATALEN
);
153 if (memcmp(kbd_data
, tmp_data
, KEYBD_DATALEN
) == 0) {
154 /* consistent state, done */
157 /* remeber last state, delay, and retry */
158 memcpy (tmp_data
, kbd_data
, KEYBD_DATALEN
);
164 /* Read a register from the dsPIC. */
165 int _dspic_read(ushort reg
, ushort
*data
)
167 uchar buf
[sizeof(*data
)];
170 if (i2c_read(dspic_addr
, reg
, 2, buf
, 2))
173 rval
= i2c_read(dspic_addr
, reg
, sizeof(reg
), buf
, sizeof(*data
));
174 *data
= (buf
[0] << 8) | buf
[1];
180 /***********************************************************************
181 F* Function: int misc_init_r (void) P*A*Z*
186 P* - 0 is always returned, even in the case of a keyboard
189 Z* Intention: This function is the misc_init_r() method implementation
190 Z* for the lwmon board.
191 Z* The keyboard controller is initialized and the result
192 Z* of a read copied to the environment variable "keybd".
193 Z* If KEYBD_SET_DEBUGMODE is defined, a check is made for
194 Z* this key, and if found display to the LCD will be enabled.
195 Z* The keys in "keybd" are checked against the magic
196 Z* keycommands defined in the environment.
197 Z* See also key_match().
199 D* Design: wd@denx.de
200 C* Coding: wd@denx.de
201 V* Verification: dzu@denx.de
202 ***********************************************************************/
203 int misc_init_r_kbd (void)
205 uchar kbd_data
[KEYBD_DATALEN
];
206 char keybd_env
[2 * KEYBD_DATALEN
+ 1];
207 uchar kbd_init_status
= gd
->arch
.kbd_status
>> 8;
208 uchar kbd_status
= gd
->arch
.kbd_status
;
210 ushort data
, inv_data
;
214 if (kbd_init_status
) {
215 printf ("KEYBD: Error %02X\n", kbd_init_status
);
217 if (kbd_status
) { /* permanent error, report it */
218 printf ("*** Keyboard error code %02X ***\n", kbd_status
);
219 sprintf (keybd_env
, "%02X", kbd_status
);
220 setenv ("keybd", keybd_env
);
225 * Now we know that we have a working keyboard, so disable
226 * all output to the LCD except when a key press is detected.
229 if ((console_assign (stdout
, "serial") < 0) ||
230 (console_assign (stderr
, "serial") < 0)) {
231 printf ("Can't assign serial port as output device\n");
235 val
= KEYBD_CMD_READ_VERSION
;
236 i2c_write (kbd_addr
, 0, 0, &val
, 1);
237 i2c_read (kbd_addr
, 0, 0, kbd_data
, KEYBD_VERSIONLEN
);
238 printf ("KEYBD: Version %d.%d\n", kbd_data
[0], kbd_data
[1]);
240 /* Read current keyboard state */
241 val
= KEYBD_CMD_READ_KEYS
;
242 i2c_write (kbd_addr
, 0, 0, &val
, 1);
243 i2c_read (kbd_addr
, 0, 0, kbd_data
, KEYBD_DATALEN
);
245 /* read out start key from bse01 received via can */
246 _dspic_read(DSPIC_PON_STATUS_REG
, &data
);
247 /* check highbyte from status register */
249 _dspic_read(DSPIC_PON_INV_STATUS_REG
, &inv_data
);
251 /* check inverse data */
252 if ((data
+inv_data
) == 0xFFFF) {
253 /* don't overwrite local key */
254 if (kbd_data
[1] == 0) {
256 _dspic_read(DSPIC_PON_KEY_REG
, &data
);
259 kbd_data
[1] = str
[1];
260 kbd_data
[2] = str
[0];
261 printf("CAN received startkey: 0x%X\n", data
);
266 for (i
= 0; i
< KEYBD_DATALEN
; ++i
) {
267 sprintf (keybd_env
+ i
+ i
, "%02X", kbd_data
[i
]);
270 setenv ("keybd", keybd_env
);
272 str
= strdup ((char *)key_match (kbd_data
)); /* decode keys */
273 #ifdef KEYBD_SET_DEBUGMODE
274 if (kbd_data
[0] == KEYBD_SET_DEBUGMODE
) { /* set debug mode */
275 if ((console_assign (stdout
, "lcd") < 0) ||
276 (console_assign (stderr
, "lcd") < 0)) {
277 printf ("Can't assign LCD display as output device\n");
280 #endif /* KEYBD_SET_DEBUGMODE */
281 #ifdef CONFIG_PREBOOT /* automatically configure "preboot" command on key match */
282 setenv ("preboot", str
); /* set or delete definition */
283 #endif /* CONFIG_PREBOOT */
290 #ifdef CONFIG_PREBOOT
292 static uchar kbd_magic_prefix
[] = "key_magic";
293 static uchar kbd_command_prefix
[] = "key_cmd";
295 static int compare_magic (uchar
*kbd_data
, uchar
*str
)
297 uchar compare
[KEYBD_DATALEN
-1];
301 /* Don't include modifier byte */
302 memcpy (compare
, kbd_data
+1, KEYBD_DATALEN
-1);
304 for (; str
!= NULL
; str
= (*nxt
) ? (uchar
*)(nxt
+1) : (uchar
*)nxt
) {
308 c
= (uchar
) simple_strtoul ((char *)str
, (char **) (&nxt
), 16);
310 if (str
== (uchar
*)nxt
) { /* invalid character */
315 * Check if this key matches the input.
316 * Set matches to zero, so they match only once
317 * and we can find duplicates or extra keys
319 for (k
= 0; k
< sizeof(compare
); ++k
) {
320 if (compare
[k
] == '\0') /* only non-zero entries */
322 if (c
== compare
[k
]) { /* found matching key */
327 if (k
== sizeof(compare
)) {
328 return -1; /* unmatched key */
333 * A full match leaves no keys in the `compare' array,
335 for (i
= 0; i
< sizeof(compare
); ++i
) {
345 /***********************************************************************
346 F* Function: static uchar *key_match (uchar *kbd_data) P*A*Z*
348 P* Parameters: uchar *kbd_data
349 P* - The keys to match against our magic definitions
351 P* Returnvalue: uchar *
352 P* - != NULL: Pointer to the corresponding command(s)
353 P* NULL: No magic is about to happen
355 Z* Intention: Check if pressed key(s) match magic sequence,
356 Z* and return the command string associated with that key(s).
358 Z* If no key press was decoded, NULL is returned.
360 Z* Note: the first character of the argument will be
361 Z* overwritten with the "magic charcter code" of the
362 Z* decoded key(s), or '\0'.
364 Z* Note: the string points to static environment data
365 Z* and must be saved before you call any function that
366 Z* modifies the environment.
368 D* Design: wd@denx.de
369 C* Coding: wd@denx.de
370 V* Verification: dzu@denx.de
371 ***********************************************************************/
372 static uchar
*key_match (uchar
*kbd_data
)
374 char magic
[sizeof (kbd_magic_prefix
) + 1];
376 char *kbd_magic_keys
;
379 * The following string defines the characters that can pe appended
380 * to "key_magic" to form the names of environment variables that
381 * hold "magic" key codes, i. e. such key codes that can cause
382 * pre-boot actions. If the string is empty (""), then only
383 * "key_magic" is checked (old behaviour); the string "125" causes
384 * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
386 if ((kbd_magic_keys
= getenv ("magic_keys")) == NULL
)
389 /* loop over all magic keys;
390 * use '\0' suffix in case of empty string
392 for (suffix
=(uchar
*)kbd_magic_keys
; *suffix
|| suffix
==(uchar
*)kbd_magic_keys
; ++suffix
) {
393 sprintf (magic
, "%s%c", kbd_magic_prefix
, *suffix
);
394 debug ("### Check magic \"%s\"\n", magic
);
395 if (compare_magic(kbd_data
, (uchar
*)getenv(magic
)) == 0) {
396 char cmd_name
[sizeof (kbd_command_prefix
) + 1];
399 sprintf (cmd_name
, "%s%c", kbd_command_prefix
, *suffix
);
401 cmd
= getenv (cmd_name
);
402 debug ("### Set PREBOOT to $(%s): \"%s\"\n",
403 cmd_name
, cmd
? cmd
: "<<NULL>>");
405 return ((uchar
*)cmd
);
408 debug ("### Delete PREBOOT\n");
412 #endif /* CONFIG_PREBOOT */
414 /***********************************************************************
415 F* Function: int do_kbd (cmd_tbl_t *cmdtp, int flag,
416 F* int argc, char * const argv[]) P*A*Z*
418 P* Parameters: cmd_tbl_t *cmdtp
419 P* - Pointer to our command table entry
421 P* - If the CMD_FLAG_REPEAT bit is set, then this call is
425 P* char * const argv[]
426 P* - Array of the actual arguments
429 P* - 0 is always returned.
431 Z* Intention: Implement the "kbd" command.
432 Z* The keyboard status is read. The result is printed on
433 Z* the console and written into the "keybd" environment
436 D* Design: wd@denx.de
437 C* Coding: wd@denx.de
438 V* Verification: dzu@denx.de
439 ***********************************************************************/
440 int do_kbd (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
442 uchar kbd_data
[KEYBD_DATALEN
];
443 char keybd_env
[2 * KEYBD_DATALEN
+ 1];
447 #if 0 /* Done in kbd_init */
448 i2c_init (CONFIG_SYS_I2C_SPEED
, CONFIG_SYS_I2C_SLAVE
);
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
);
457 for (i
= 0; i
< KEYBD_DATALEN
; ++i
) {
458 sprintf (keybd_env
+ i
+ i
, "%02X", kbd_data
[i
]);
459 printf (" %02x", kbd_data
[i
]);
462 setenv ("keybd", keybd_env
);
468 "read keyboard status",
472 /*----------------------------- Utilities -----------------------------*/
476 * Returns 1 if keys pressed to start the power-on long-running tests
477 * Called from board_init_f().
479 int post_hotkeys_pressed(void)
481 uchar kbd_data
[KEYBD_DATALEN
];
485 val
= KEYBD_CMD_READ_KEYS
;
486 i2c_write (kbd_addr
, 0, 0, &val
, 1);
487 i2c_read (kbd_addr
, 0, 0, kbd_data
, KEYBD_DATALEN
);
489 return (compare_magic(kbd_data
, (uchar
*)CONFIG_POST_KEY_MAGIC
) == 0);