1
2
3
4
5
6
7
8
9
10
11
12
13#if 0
14#define DEBUG
15#endif
16
17#include <common.h>
18#include <i2c.h>
19#include <command.h>
20#include <console.h>
21#include <post.h>
22#include <serial.h>
23#include <malloc.h>
24
25#include <linux/types.h>
26#include <linux/string.h>
27
28DECLARE_GLOBAL_DATA_PTR;
29
30static void kbd_init (void);
31static int compare_magic (uchar *kbd_data, uchar *str);
32
33
34#define _NOT_USED_ 0xFFFFFFFF
35
36
37#define DSPIC_PON_STATUS_REG 0x80A
38#define DSPIC_PON_INV_STATUS_REG 0x80C
39#define DSPIC_PON_KEY_REG 0x810
40
41
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
46
47
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
55
56
57#define KEYBD_VERSIONLEN 2
58
59
60
61
62
63
64
65#define KEYBD_DATALEN 6
66
67
68
69static uchar kbd_addr = CONFIG_SYS_I2C_KEYBD_ADDR;
70static uchar dspic_addr = CONFIG_SYS_I2C_DSPIC_IO_ADDR;
71
72static uchar *key_match (uchar *);
73
74#define KEYBD_SET_DEBUGMODE '#'
75
76
77
78
79
80
81
82
83
84
85
86
87
88int board_postclk_init (void)
89{
90 kbd_init();
91
92 return (0);
93}
94
95static void kbd_init (void)
96{
97 uchar kbd_data[KEYBD_DATALEN];
98 uchar tmp_data[KEYBD_DATALEN];
99 uchar val, errcd;
100 int i;
101
102 i2c_set_bus_num(0);
103
104 gd->arch.kbd_status = 0;
105
106
107 udelay(1000);
108
109
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
114 errcd &= KEYBD_STATUS_MASK;
115
116 errcd &= ~(KEYBD_STATUS_H_RESET|KEYBD_STATUS_BROWNOUT);
117 if (errcd) {
118 gd->arch.kbd_status |= errcd << 8;
119 }
120
121 val = KEYBD_CMD_RESET_ERRORS;
122 i2c_write (kbd_addr, 0, 0, &val, 1);
123 udelay(1000);
124
125 val = KEYBD_CMD_READ_STATUS;
126 i2c_write (kbd_addr, 0, 0, &val, 1);
127 i2c_read (kbd_addr, 0, 0, &val, 1);
128
129 val &= KEYBD_STATUS_MASK;
130 if (val) {
131 gd->arch.kbd_status |= val;
132 return;
133 }
134
135
136
137
138
139
140
141
142
143
144
145
146
147 memset (tmp_data, 0xFF, KEYBD_DATALEN);
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);
152
153 if (memcmp(kbd_data, tmp_data, KEYBD_DATALEN) == 0) {
154
155 break;
156 }
157
158 memcpy (tmp_data, kbd_data, KEYBD_DATALEN);
159 udelay (5000);
160 }
161}
162
163
164
165int _dspic_read(ushort reg, ushort *data)
166{
167 uchar buf[sizeof(*data)];
168 int rval;
169
170 if (i2c_read(dspic_addr, reg, 2, buf, 2))
171 return -1;
172
173 rval = i2c_read(dspic_addr, reg, sizeof(reg), buf, sizeof(*data));
174 *data = (buf[0] << 8) | buf[1];
175
176 return rval;
177}
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203int misc_init_r_kbd (void)
204{
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;
209 uchar val;
210 ushort data, inv_data;
211 char *str;
212 int i;
213
214 if (kbd_init_status) {
215 printf ("KEYBD: Error %02X\n", kbd_init_status);
216 }
217 if (kbd_status) {
218 printf ("*** Keyboard error code %02X ***\n", kbd_status);
219 sprintf (keybd_env, "%02X", kbd_status);
220 setenv ("keybd", keybd_env);
221 return 0;
222 }
223
224
225
226
227
228
229 if ((console_assign (stdout, "serial") < 0) ||
230 (console_assign (stderr, "serial") < 0)) {
231 printf ("Can't assign serial port as output device\n");
232 }
233
234
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]);
239
240
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);
244
245
246 _dspic_read(DSPIC_PON_STATUS_REG, &data);
247
248 if (data > 0xFF) {
249 _dspic_read(DSPIC_PON_INV_STATUS_REG, &inv_data);
250
251
252 if ((data+inv_data) == 0xFFFF) {
253
254 if (kbd_data[1] == 0) {
255
256 _dspic_read(DSPIC_PON_KEY_REG, &data);
257 str = (char *)&data;
258
259 kbd_data[1] = str[1];
260 kbd_data[2] = str[0];
261 printf("CAN received startkey: 0x%X\n", data);
262 }
263 }
264 }
265
266 for (i = 0; i < KEYBD_DATALEN; ++i) {
267 sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
268 }
269
270 setenv ("keybd", keybd_env);
271
272 str = strdup ((char *)key_match (kbd_data));
273#ifdef KEYBD_SET_DEBUGMODE
274 if (kbd_data[0] == KEYBD_SET_DEBUGMODE) {
275 if ((console_assign (stdout, "lcd") < 0) ||
276 (console_assign (stderr, "lcd") < 0)) {
277 printf ("Can't assign LCD display as output device\n");
278 }
279 }
280#endif
281#ifdef CONFIG_PREBOOT
282 setenv ("preboot", str);
283#endif
284 if (str != NULL) {
285 free (str);
286 }
287 return (0);
288}
289
290#ifdef CONFIG_PREBOOT
291
292static uchar kbd_magic_prefix[] = "key_magic";
293static uchar kbd_command_prefix[] = "key_cmd";
294
295static int compare_magic (uchar *kbd_data, uchar *str)
296{
297 uchar compare[KEYBD_DATALEN-1];
298 char *nxt;
299 int i;
300
301
302 memcpy (compare, kbd_data+1, KEYBD_DATALEN-1);
303
304 for (; str != NULL; str = (*nxt) ? (uchar *)(nxt+1) : (uchar *)nxt) {
305 uchar c;
306 int k;
307
308 c = (uchar) simple_strtoul ((char *)str, (char **) (&nxt), 16);
309
310 if (str == (uchar *)nxt) {
311 break;
312 }
313
314
315
316
317
318
319 for (k = 0; k < sizeof(compare); ++k) {
320 if (compare[k] == '\0')
321 continue;
322 if (c == compare[k]) {
323 compare[k] = '\0';
324 break;
325 }
326 }
327 if (k == sizeof(compare)) {
328 return -1;
329 }
330 }
331
332
333
334
335 for (i = 0; i < sizeof(compare); ++i) {
336 if (compare[i])
337 {
338 return -1;
339 }
340 }
341
342 return 0;
343}
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372static uchar *key_match (uchar *kbd_data)
373{
374 char magic[sizeof (kbd_magic_prefix) + 1];
375 uchar *suffix;
376 char *kbd_magic_keys;
377
378
379
380
381
382
383
384
385
386 if ((kbd_magic_keys = getenv ("magic_keys")) == NULL)
387 kbd_magic_keys = "";
388
389
390
391
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];
397 char *cmd;
398
399 sprintf (cmd_name, "%s%c", kbd_command_prefix, *suffix);
400
401 cmd = getenv (cmd_name);
402 debug ("### Set PREBOOT to $(%s): \"%s\"\n",
403 cmd_name, cmd ? cmd : "<<NULL>>");
404 *kbd_data = *suffix;
405 return ((uchar *)cmd);
406 }
407 }
408 debug ("### Delete PREBOOT\n");
409 *kbd_data = '\0';
410 return (NULL);
411}
412#endif
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440int do_kbd (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
441{
442 uchar kbd_data[KEYBD_DATALEN];
443 char keybd_env[2 * KEYBD_DATALEN + 1];
444 uchar val;
445 int i;
446
447#if 0
448 i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
449#endif
450
451
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 puts ("Keys:");
457 for (i = 0; i < KEYBD_DATALEN; ++i) {
458 sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
459 printf (" %02x", kbd_data[i]);
460 }
461 putc ('\n');
462 setenv ("keybd", keybd_env);
463 return 0;
464}
465
466U_BOOT_CMD(
467 kbd, 1, 1, do_kbd,
468 "read keyboard status",
469 ""
470);
471
472
473
474#ifdef CONFIG_POST
475
476
477
478
479int post_hotkeys_pressed(void)
480{
481 uchar kbd_data[KEYBD_DATALEN];
482 uchar val;
483
484
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);
488
489 return (compare_magic(kbd_data, (uchar *)CONFIG_POST_KEY_MAGIC) == 0);
490}
491#endif
492