blob: 0e26b890b2c84dd61a812f5a2bff2439e67524ec [file] [log] [blame]
Stefan Roesef55a22c2007-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 *
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.
13 *
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.
18 *
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,
22 * MA 02111-1307 USA
23 */
24
25/* define DEBUG for debugging output (obviously ;-)) */
26#if 0
27#define DEBUG
28#endif
29
30#include <common.h>
31#include <i2c.h>
32#include <command.h>
33#include <post.h>
34#include <serial.h>
35#include <malloc.h>
36
37#include <linux/types.h>
38#include <linux/string.h> /* for strdup */
39
40DECLARE_GLOBAL_DATA_PTR;
41
42static void kbd_init (void);
43static int compare_magic (uchar *kbd_data, uchar *str);
44
45/*--------------------- Local macros and constants --------------------*/
46#define _NOT_USED_ 0xFFFFFFFF
47
48/*------------------------- Keyboard controller -----------------------*/
49/* command codes */
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
54
55/* status codes */
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
63
64/* Number of bytes returned from Keyboard Controller */
65#define KEYBD_VERSIONLEN 2 /* version information */
66
67/*
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.
72 */
73#define KEYBD_DATALEN 6 /* normal key scan data */
74
75/* maximum number of "magic" key codes that can be assigned */
76
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020077static uchar kbd_addr = CONFIG_SYS_I2C_KEYBD_ADDR;
Stefan Roesef55a22c2007-08-21 16:27:57 +020078
79static uchar *key_match (uchar *);
80
81#define KEYBD_SET_DEBUGMODE '#' /* Magic key to enable debug output */
82
83/***********************************************************************
84F* Function: int board_postclk_init (void) P*A*Z*
85 *
86P* Parameters: none
87P*
88P* Returnvalue: int
89P* - 0 is always returned.
90 *
91Z* Intention: This function is the board_postclk_init() method implementation
92Z* for the lwmon board.
93 *
94 ***********************************************************************/
95int board_postclk_init (void)
96{
97 kbd_init();
98
99 return (0);
100}
101
102static void kbd_init (void)
103{
104 uchar kbd_data[KEYBD_DATALEN];
105 uchar tmp_data[KEYBD_DATALEN];
106 uchar val, errcd;
107 int i;
108
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200109 i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
Stefan Roesef55a22c2007-08-21 16:27:57 +0200110
111 gd->kbd_status = 0;
112
113 /* Forced by PIC. Delays <= 175us loose */
114 udelay(1000);
115
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);
124 if (errcd) {
125 gd->kbd_status |= errcd << 8;
126 }
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 !!! */
131
132 val = KEYBD_CMD_READ_STATUS;
133 i2c_write (kbd_addr, 0, 0, &val, 1);
134 i2c_read (kbd_addr, 0, 0, &val, 1);
135
136 val &= KEYBD_STATUS_MASK; /* clear unused bits */
137 if (val) { /* permanent error, report it */
138 gd->kbd_status |= val;
139 return;
140 }
141
142 /*
143 * Read current keyboard state.
144 *
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 :-(
152 */
153
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);
159
160 if (memcmp(kbd_data, tmp_data, KEYBD_DATALEN) == 0) {
161 /* consistent state, done */
162 break;
163 }
164 /* remeber last state, delay, and retry */
165 memcpy (tmp_data, kbd_data, KEYBD_DATALEN);
166 udelay (5000);
167 }
168}
169
170/***********************************************************************
171F* Function: int misc_init_r (void) P*A*Z*
172 *
173P* Parameters: none
174P*
175P* Returnvalue: int
176P* - 0 is always returned, even in the case of a keyboard
177P* error.
178 *
179Z* Intention: This function is the misc_init_r() method implementation
180Z* for the lwmon board.
181Z* The keyboard controller is initialized and the result
182Z* of a read copied to the environment variable "keybd".
183Z* If KEYBD_SET_DEBUGMODE is defined, a check is made for
184Z* this key, and if found display to the LCD will be enabled.
185Z* The keys in "keybd" are checked against the magic
186Z* keycommands defined in the environment.
187Z* See also key_match().
188 *
189D* Design: wd@denx.de
190C* Coding: wd@denx.de
191V* Verification: dzu@denx.de
192 ***********************************************************************/
193int misc_init_r_kbd (void)
194{
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;
199 uchar val;
200 char *str;
201 int i;
202
203 if (kbd_init_status) {
204 printf ("KEYBD: Error %02X\n", kbd_init_status);
205 }
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);
210 return 0;
211 }
212
213 /*
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.
216 */
217
218 if ((console_assign (stdout, "serial") < 0) ||
219 (console_assign (stderr, "serial") < 0)) {
220 printf ("Can't assign serial port as output device\n");
221 }
222
223 /* Read Version */
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]);
228
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);
233
234 for (i = 0; i < KEYBD_DATALEN; ++i) {
235 sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
236 }
237 setenv ("keybd", keybd_env);
238
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");
245 }
246 }
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 */
251 if (str != NULL) {
252 free (str);
253 }
254 return (0);
255}
256
257#ifdef CONFIG_PREBOOT
258
259static uchar kbd_magic_prefix[] = "key_magic";
260static uchar kbd_command_prefix[] = "key_cmd";
261
262static int compare_magic (uchar *kbd_data, uchar *str)
263{
264 uchar compare[KEYBD_DATALEN-1];
265 char *nxt;
266 int i;
267
268 /* Don't include modifier byte */
269 memcpy (compare, kbd_data+1, KEYBD_DATALEN-1);
270
271 for (; str != NULL; str = (*nxt) ? (uchar *)(nxt+1) : (uchar *)nxt) {
272 uchar c;
273 int k;
274
275 c = (uchar) simple_strtoul ((char *)str, (char **) (&nxt), 16);
276
277 if (str == (uchar *)nxt) { /* invalid character */
278 break;
279 }
280
281 /*
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
285 */
286 for (k = 0; k < sizeof(compare); ++k) {
287 if (compare[k] == '\0') /* only non-zero entries */
288 continue;
289 if (c == compare[k]) { /* found matching key */
290 compare[k] = '\0';
291 break;
292 }
293 }
294 if (k == sizeof(compare)) {
295 return -1; /* unmatched key */
296 }
297 }
298
299 /*
300 * A full match leaves no keys in the `compare' array,
301 */
302 for (i = 0; i < sizeof(compare); ++i) {
303 if (compare[i])
304 {
305 return -1;
306 }
307 }
308
309 return 0;
310}
311
312/***********************************************************************
313F* Function: static uchar *key_match (uchar *kbd_data) P*A*Z*
314 *
315P* Parameters: uchar *kbd_data
316P* - The keys to match against our magic definitions
317P*
318P* Returnvalue: uchar *
319P* - != NULL: Pointer to the corresponding command(s)
320P* NULL: No magic is about to happen
321 *
322Z* Intention: Check if pressed key(s) match magic sequence,
323Z* and return the command string associated with that key(s).
324Z*
325Z* If no key press was decoded, NULL is returned.
326Z*
327Z* Note: the first character of the argument will be
328Z* overwritten with the "magic charcter code" of the
329Z* decoded key(s), or '\0'.
330Z*
331Z* Note: the string points to static environment data
332Z* and must be saved before you call any function that
333Z* modifies the environment.
334 *
335D* Design: wd@denx.de
336C* Coding: wd@denx.de
337V* Verification: dzu@denx.de
338 ***********************************************************************/
339static uchar *key_match (uchar *kbd_data)
340{
341 char magic[sizeof (kbd_magic_prefix) + 1];
342 uchar *suffix;
343 char *kbd_magic_keys;
344
345 /*
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.
352 */
353 if ((kbd_magic_keys = getenv ("magic_keys")) == NULL)
354 kbd_magic_keys = "";
355
356 /* loop over all magic keys;
357 * use '\0' suffix in case of empty string
358 */
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];
364 char *cmd;
365
366 sprintf (cmd_name, "%s%c", kbd_command_prefix, *suffix);
367
368 cmd = getenv (cmd_name);
369 debug ("### Set PREBOOT to $(%s): \"%s\"\n",
370 cmd_name, cmd ? cmd : "<<NULL>>");
371 *kbd_data = *suffix;
372 return ((uchar *)cmd);
373 }
374 }
375 debug ("### Delete PREBOOT\n");
376 *kbd_data = '\0';
377 return (NULL);
378}
379#endif /* CONFIG_PREBOOT */
380
381/***********************************************************************
382F* Function: int do_kbd (cmd_tbl_t *cmdtp, int flag,
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200383F* int argc, char * const argv[]) P*A*Z*
Stefan Roesef55a22c2007-08-21 16:27:57 +0200384 *
385P* Parameters: cmd_tbl_t *cmdtp
386P* - Pointer to our command table entry
387P* int flag
388P* - If the CMD_FLAG_REPEAT bit is set, then this call is
389P* a repetition
390P* int argc
391P* - Argument count
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200392P* char * const argv[]
Stefan Roesef55a22c2007-08-21 16:27:57 +0200393P* - Array of the actual arguments
394P*
395P* Returnvalue: int
396P* - 0 is always returned.
397 *
398Z* Intention: Implement the "kbd" command.
399Z* The keyboard status is read. The result is printed on
400Z* the console and written into the "keybd" environment
401Z* variable.
402 *
403D* Design: wd@denx.de
404C* Coding: wd@denx.de
405V* Verification: dzu@denx.de
406 ***********************************************************************/
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200407int do_kbd (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Stefan Roesef55a22c2007-08-21 16:27:57 +0200408{
409 uchar kbd_data[KEYBD_DATALEN];
410 char keybd_env[2 * KEYBD_DATALEN + 1];
411 uchar val;
412 int i;
413
414#if 0 /* Done in kbd_init */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200415 i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
Stefan Roesef55a22c2007-08-21 16:27:57 +0200416#endif
417
418 /* Read keys */
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);
422
423 puts ("Keys:");
424 for (i = 0; i < KEYBD_DATALEN; ++i) {
425 sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
426 printf (" %02x", kbd_data[i]);
427 }
428 putc ('\n');
429 setenv ("keybd", keybd_env);
430 return 0;
431}
432
433U_BOOT_CMD(
434 kbd, 1, 1, do_kbd,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600435 "read keyboard status",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200436 ""
Stefan Roesef55a22c2007-08-21 16:27:57 +0200437);
438
439/*----------------------------- Utilities -----------------------------*/
440
441#ifdef CONFIG_POST
442/*
443 * Returns 1 if keys pressed to start the power-on long-running tests
444 * Called from board_init_f().
445 */
446int post_hotkeys_pressed(void)
447{
448 uchar kbd_data[KEYBD_DATALEN];
449 uchar val;
450
451 /* Read keys */
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);
455
456 return (compare_magic(kbd_data, (uchar *)CONFIG_POST_KEY_MAGIC) == 0);
457}
458#endif