blob: 97962daf93948773db6b8edaddaf71512d3917e5 [file] [log] [blame]
Stefan Roese3ad63872007-08-21 16:27:57 +02001/*
2 * (C) Copyright 2007
3 * Stefan Roese, DENX Software Engineering, sr@denx.de.
4 *
5 * (C) Copyright 2001, 2002
6 * DENX Software Engineering
7 * Wolfgang Denk, wd@denx.de
8 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
Stefan Roese3ad63872007-08-21 16:27:57 +020010 */
11
12/* define DEBUG for debugging output (obviously ;-)) */
13#if 0
14#define DEBUG
15#endif
16
17#include <common.h>
18#include <i2c.h>
19#include <command.h>
20#include <post.h>
21#include <serial.h>
22#include <malloc.h>
23
24#include <linux/types.h>
25#include <linux/string.h> /* for strdup */
26
27DECLARE_GLOBAL_DATA_PTR;
28
29static void kbd_init (void);
30static int compare_magic (uchar *kbd_data, uchar *str);
31
32/*--------------------- Local macros and constants --------------------*/
33#define _NOT_USED_ 0xFFFFFFFF
34
Sascha Lauef14ae412010-08-19 09:38:56 +020035/*------------------------- 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
Stefan Roese3ad63872007-08-21 16:27:57 +020039/*------------------------- Keyboard controller -----------------------*/
40/* command codes */
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
45
46/* status codes */
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
54
55/* Number of bytes returned from Keyboard Controller */
56#define KEYBD_VERSIONLEN 2 /* version information */
57
58/*
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.
63 */
64#define KEYBD_DATALEN 6 /* normal key scan data */
65
66/* maximum number of "magic" key codes that can be assigned */
67
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020068static uchar kbd_addr = CONFIG_SYS_I2C_KEYBD_ADDR;
Sascha Lauef14ae412010-08-19 09:38:56 +020069static uchar dspic_addr = CONFIG_SYS_I2C_DSPIC_IO_ADDR;
Stefan Roese3ad63872007-08-21 16:27:57 +020070
71static uchar *key_match (uchar *);
72
73#define KEYBD_SET_DEBUGMODE '#' /* Magic key to enable debug output */
74
75/***********************************************************************
76F* Function: int board_postclk_init (void) P*A*Z*
77 *
78P* Parameters: none
79P*
80P* Returnvalue: int
81P* - 0 is always returned.
82 *
83Z* Intention: This function is the board_postclk_init() method implementation
84Z* for the lwmon board.
85 *
86 ***********************************************************************/
87int board_postclk_init (void)
88{
89 kbd_init();
90
91 return (0);
92}
93
94static void kbd_init (void)
95{
96 uchar kbd_data[KEYBD_DATALEN];
97 uchar tmp_data[KEYBD_DATALEN];
98 uchar val, errcd;
99 int i;
100
Dirk Eibach880540d2013-04-25 02:40:01 +0000101 i2c_set_bus_num(0);
Stefan Roese3ad63872007-08-21 16:27:57 +0200102
Simon Glass225ca832012-12-13 20:49:04 +0000103 gd->arch.kbd_status = 0;
Stefan Roese3ad63872007-08-21 16:27:57 +0200104
105 /* Forced by PIC. Delays <= 175us loose */
106 udelay(1000);
107
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);
116 if (errcd) {
Simon Glass225ca832012-12-13 20:49:04 +0000117 gd->arch.kbd_status |= errcd << 8;
Stefan Roese3ad63872007-08-21 16:27:57 +0200118 }
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 !!! */
123
124 val = KEYBD_CMD_READ_STATUS;
125 i2c_write (kbd_addr, 0, 0, &val, 1);
126 i2c_read (kbd_addr, 0, 0, &val, 1);
127
128 val &= KEYBD_STATUS_MASK; /* clear unused bits */
129 if (val) { /* permanent error, report it */
Simon Glass225ca832012-12-13 20:49:04 +0000130 gd->arch.kbd_status |= val;
Stefan Roese3ad63872007-08-21 16:27:57 +0200131 return;
132 }
133
134 /*
135 * Read current keyboard state.
136 *
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 :-(
144 */
145
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);
151
152 if (memcmp(kbd_data, tmp_data, KEYBD_DATALEN) == 0) {
153 /* consistent state, done */
154 break;
155 }
156 /* remeber last state, delay, and retry */
157 memcpy (tmp_data, kbd_data, KEYBD_DATALEN);
158 udelay (5000);
159 }
160}
161
Sascha Lauef14ae412010-08-19 09:38:56 +0200162
163/* Read a register from the dsPIC. */
164int _dspic_read(ushort reg, ushort *data)
165{
166 uchar buf[sizeof(*data)];
167 int rval;
168
169 if (i2c_read(dspic_addr, reg, 2, buf, 2))
170 return -1;
171
172 rval = i2c_read(dspic_addr, reg, sizeof(reg), buf, sizeof(*data));
173 *data = (buf[0] << 8) | buf[1];
174
175 return rval;
176}
177
178
Stefan Roese3ad63872007-08-21 16:27:57 +0200179/***********************************************************************
180F* Function: int misc_init_r (void) P*A*Z*
181 *
182P* Parameters: none
183P*
184P* Returnvalue: int
185P* - 0 is always returned, even in the case of a keyboard
186P* error.
187 *
188Z* Intention: This function is the misc_init_r() method implementation
189Z* for the lwmon board.
190Z* The keyboard controller is initialized and the result
191Z* of a read copied to the environment variable "keybd".
192Z* If KEYBD_SET_DEBUGMODE is defined, a check is made for
193Z* this key, and if found display to the LCD will be enabled.
194Z* The keys in "keybd" are checked against the magic
195Z* keycommands defined in the environment.
196Z* See also key_match().
197 *
198D* Design: wd@denx.de
199C* Coding: wd@denx.de
200V* Verification: dzu@denx.de
201 ***********************************************************************/
202int misc_init_r_kbd (void)
203{
204 uchar kbd_data[KEYBD_DATALEN];
205 char keybd_env[2 * KEYBD_DATALEN + 1];
Simon Glass225ca832012-12-13 20:49:04 +0000206 uchar kbd_init_status = gd->arch.kbd_status >> 8;
207 uchar kbd_status = gd->arch.kbd_status;
Stefan Roese3ad63872007-08-21 16:27:57 +0200208 uchar val;
Sascha Lauef14ae412010-08-19 09:38:56 +0200209 ushort data, inv_data;
Stefan Roese3ad63872007-08-21 16:27:57 +0200210 char *str;
211 int i;
212
213 if (kbd_init_status) {
214 printf ("KEYBD: Error %02X\n", kbd_init_status);
215 }
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);
220 return 0;
221 }
222
223 /*
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.
226 */
227
228 if ((console_assign (stdout, "serial") < 0) ||
229 (console_assign (stderr, "serial") < 0)) {
230 printf ("Can't assign serial port as output device\n");
231 }
232
233 /* Read Version */
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]);
238
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);
243
Sascha Lauef14ae412010-08-19 09:38:56 +0200244 /* read out start key from bse01 received via can */
245 _dspic_read(DSPIC_PON_STATUS_REG, &data);
246 /* check highbyte from status register */
247 if (data > 0xFF) {
248 _dspic_read(DSPIC_PON_INV_STATUS_REG, &inv_data);
249
250 /* check inverse data */
251 if ((data+inv_data) == 0xFFFF) {
252 /* don't overwrite local key */
253 if (kbd_data[1] == 0) {
254 /* read key value */
255 _dspic_read(DSPIC_PON_KEY_REG, &data);
256 str = (char *)&data;
257 /* swap bytes */
258 kbd_data[1] = str[1];
259 kbd_data[2] = str[0];
260 printf("CAN received startkey: 0x%X\n", data);
261 }
262 }
263 }
264
Stefan Roese3ad63872007-08-21 16:27:57 +0200265 for (i = 0; i < KEYBD_DATALEN; ++i) {
266 sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
267 }
Sascha Lauef14ae412010-08-19 09:38:56 +0200268
Stefan Roese3ad63872007-08-21 16:27:57 +0200269 setenv ("keybd", keybd_env);
270
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");
277 }
278 }
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 */
283 if (str != NULL) {
284 free (str);
285 }
286 return (0);
287}
288
289#ifdef CONFIG_PREBOOT
290
291static uchar kbd_magic_prefix[] = "key_magic";
292static uchar kbd_command_prefix[] = "key_cmd";
293
294static int compare_magic (uchar *kbd_data, uchar *str)
295{
296 uchar compare[KEYBD_DATALEN-1];
297 char *nxt;
298 int i;
299
300 /* Don't include modifier byte */
301 memcpy (compare, kbd_data+1, KEYBD_DATALEN-1);
302
303 for (; str != NULL; str = (*nxt) ? (uchar *)(nxt+1) : (uchar *)nxt) {
304 uchar c;
305 int k;
306
307 c = (uchar) simple_strtoul ((char *)str, (char **) (&nxt), 16);
308
309 if (str == (uchar *)nxt) { /* invalid character */
310 break;
311 }
312
313 /*
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
317 */
318 for (k = 0; k < sizeof(compare); ++k) {
319 if (compare[k] == '\0') /* only non-zero entries */
320 continue;
321 if (c == compare[k]) { /* found matching key */
322 compare[k] = '\0';
323 break;
324 }
325 }
326 if (k == sizeof(compare)) {
327 return -1; /* unmatched key */
328 }
329 }
330
331 /*
332 * A full match leaves no keys in the `compare' array,
333 */
334 for (i = 0; i < sizeof(compare); ++i) {
335 if (compare[i])
336 {
337 return -1;
338 }
339 }
340
341 return 0;
342}
343
344/***********************************************************************
345F* Function: static uchar *key_match (uchar *kbd_data) P*A*Z*
346 *
347P* Parameters: uchar *kbd_data
348P* - The keys to match against our magic definitions
349P*
350P* Returnvalue: uchar *
351P* - != NULL: Pointer to the corresponding command(s)
352P* NULL: No magic is about to happen
353 *
354Z* Intention: Check if pressed key(s) match magic sequence,
355Z* and return the command string associated with that key(s).
356Z*
357Z* If no key press was decoded, NULL is returned.
358Z*
359Z* Note: the first character of the argument will be
360Z* overwritten with the "magic charcter code" of the
361Z* decoded key(s), or '\0'.
362Z*
363Z* Note: the string points to static environment data
364Z* and must be saved before you call any function that
365Z* modifies the environment.
366 *
367D* Design: wd@denx.de
368C* Coding: wd@denx.de
369V* Verification: dzu@denx.de
370 ***********************************************************************/
371static uchar *key_match (uchar *kbd_data)
372{
373 char magic[sizeof (kbd_magic_prefix) + 1];
374 uchar *suffix;
375 char *kbd_magic_keys;
376
377 /*
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.
384 */
385 if ((kbd_magic_keys = getenv ("magic_keys")) == NULL)
386 kbd_magic_keys = "";
387
388 /* loop over all magic keys;
389 * use '\0' suffix in case of empty string
390 */
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];
396 char *cmd;
397
398 sprintf (cmd_name, "%s%c", kbd_command_prefix, *suffix);
399
400 cmd = getenv (cmd_name);
401 debug ("### Set PREBOOT to $(%s): \"%s\"\n",
402 cmd_name, cmd ? cmd : "<<NULL>>");
403 *kbd_data = *suffix;
404 return ((uchar *)cmd);
405 }
406 }
407 debug ("### Delete PREBOOT\n");
408 *kbd_data = '\0';
409 return (NULL);
410}
411#endif /* CONFIG_PREBOOT */
412
413/***********************************************************************
414F* Function: int do_kbd (cmd_tbl_t *cmdtp, int flag,
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200415F* int argc, char * const argv[]) P*A*Z*
Stefan Roese3ad63872007-08-21 16:27:57 +0200416 *
417P* Parameters: cmd_tbl_t *cmdtp
418P* - Pointer to our command table entry
419P* int flag
420P* - If the CMD_FLAG_REPEAT bit is set, then this call is
421P* a repetition
422P* int argc
423P* - Argument count
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200424P* char * const argv[]
Stefan Roese3ad63872007-08-21 16:27:57 +0200425P* - Array of the actual arguments
426P*
427P* Returnvalue: int
428P* - 0 is always returned.
429 *
430Z* Intention: Implement the "kbd" command.
431Z* The keyboard status is read. The result is printed on
432Z* the console and written into the "keybd" environment
433Z* variable.
434 *
435D* Design: wd@denx.de
436C* Coding: wd@denx.de
437V* Verification: dzu@denx.de
438 ***********************************************************************/
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200439int do_kbd (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Stefan Roese3ad63872007-08-21 16:27:57 +0200440{
441 uchar kbd_data[KEYBD_DATALEN];
442 char keybd_env[2 * KEYBD_DATALEN + 1];
443 uchar val;
444 int i;
445
446#if 0 /* Done in kbd_init */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200447 i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
Stefan Roese3ad63872007-08-21 16:27:57 +0200448#endif
449
450 /* Read keys */
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);
454
455 puts ("Keys:");
456 for (i = 0; i < KEYBD_DATALEN; ++i) {
457 sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
458 printf (" %02x", kbd_data[i]);
459 }
460 putc ('\n');
461 setenv ("keybd", keybd_env);
462 return 0;
463}
464
465U_BOOT_CMD(
466 kbd, 1, 1, do_kbd,
Peter Tyser2fb26042009-01-27 18:03:12 -0600467 "read keyboard status",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200468 ""
Stefan Roese3ad63872007-08-21 16:27:57 +0200469);
470
471/*----------------------------- Utilities -----------------------------*/
472
473#ifdef CONFIG_POST
474/*
475 * Returns 1 if keys pressed to start the power-on long-running tests
476 * Called from board_init_f().
477 */
478int post_hotkeys_pressed(void)
479{
480 uchar kbd_data[KEYBD_DATALEN];
481 uchar val;
482
483 /* Read keys */
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);
487
488 return (compare_magic(kbd_data, (uchar *)CONFIG_POST_KEY_MAGIC) == 0);
489}
490#endif