kbd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  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. /* define DEBUG for debugging output (obviously ;-)) */
  25. #if 0
  26. #define DEBUG
  27. #endif
  28. #include <common.h>
  29. #include <i2c.h>
  30. #include <command.h>
  31. #include <post.h>
  32. #include <serial.h>
  33. #include <malloc.h>
  34. #include <linux/types.h>
  35. #include <linux/string.h> /* for strdup */
  36. DECLARE_GLOBAL_DATA_PTR;
  37. static void kbd_init (void);
  38. static int compare_magic (uchar *kbd_data, uchar *str);
  39. /*--------------------- Local macros and constants --------------------*/
  40. #define _NOT_USED_ 0xFFFFFFFF
  41. /*------------------------- Keyboard controller -----------------------*/
  42. /* command codes */
  43. #define KEYBD_CMD_READ_KEYS 0x01
  44. #define KEYBD_CMD_READ_VERSION 0x02
  45. #define KEYBD_CMD_READ_STATUS 0x03
  46. #define KEYBD_CMD_RESET_ERRORS 0x10
  47. /* status codes */
  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. /* Number of bytes returned from Keyboard Controller */
  56. #define KEYBD_VERSIONLEN 2 /* version information */
  57. /*
  58. * This is different from the "old" lwmon dsPIC kbd controller
  59. * implementation. Now the controller still answers with 9 bytes,
  60. * but the last 3 bytes are always "0x06 0x07 0x08". So we just
  61. * set the length to compare to 6 instead of 9.
  62. */
  63. #define KEYBD_DATALEN 6 /* normal key scan data */
  64. /* maximum number of "magic" key codes that can be assigned */
  65. static uchar kbd_addr = CONFIG_SYS_I2C_KEYBD_ADDR;
  66. static uchar *key_match (uchar *);
  67. #define KEYBD_SET_DEBUGMODE '#' /* Magic key to enable debug output */
  68. /***********************************************************************
  69. F* Function: int board_postclk_init (void) P*A*Z*
  70. *
  71. P* Parameters: none
  72. P*
  73. P* Returnvalue: int
  74. P* - 0 is always returned.
  75. *
  76. Z* Intention: This function is the board_postclk_init() method implementation
  77. Z* for the lwmon board.
  78. *
  79. ***********************************************************************/
  80. int board_postclk_init (void)
  81. {
  82. kbd_init();
  83. return (0);
  84. }
  85. static void kbd_init (void)
  86. {
  87. uchar kbd_data[KEYBD_DATALEN];
  88. uchar tmp_data[KEYBD_DATALEN];
  89. uchar val, errcd;
  90. int i;
  91. i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  92. gd->kbd_status = 0;
  93. /* Forced by PIC. Delays <= 175us loose */
  94. udelay(1000);
  95. /* Read initial keyboard error code */
  96. val = KEYBD_CMD_READ_STATUS;
  97. i2c_write (kbd_addr, 0, 0, &val, 1);
  98. i2c_read (kbd_addr, 0, 0, &errcd, 1);
  99. /* clear unused bits */
  100. errcd &= KEYBD_STATUS_MASK;
  101. /* clear "irrelevant" bits. Recommended by Martin Rajek, LWN */
  102. errcd &= ~(KEYBD_STATUS_H_RESET|KEYBD_STATUS_BROWNOUT);
  103. if (errcd) {
  104. gd->kbd_status |= errcd << 8;
  105. }
  106. /* Reset error code and verify */
  107. val = KEYBD_CMD_RESET_ERRORS;
  108. i2c_write (kbd_addr, 0, 0, &val, 1);
  109. udelay(1000); /* delay NEEDED by keyboard PIC !!! */
  110. val = KEYBD_CMD_READ_STATUS;
  111. i2c_write (kbd_addr, 0, 0, &val, 1);
  112. i2c_read (kbd_addr, 0, 0, &val, 1);
  113. val &= KEYBD_STATUS_MASK; /* clear unused bits */
  114. if (val) { /* permanent error, report it */
  115. gd->kbd_status |= val;
  116. return;
  117. }
  118. /*
  119. * Read current keyboard state.
  120. *
  121. * After the error reset it may take some time before the
  122. * keyboard PIC picks up a valid keyboard scan - the total
  123. * scan time is approx. 1.6 ms (information by Martin Rajek,
  124. * 28 Sep 2002). We read a couple of times for the keyboard
  125. * to stabilize, using a big enough delay.
  126. * 10 times should be enough. If the data is still changing,
  127. * we use what we get :-(
  128. */
  129. memset (tmp_data, 0xFF, KEYBD_DATALEN); /* impossible value */
  130. for (i=0; i<10; ++i) {
  131. val = KEYBD_CMD_READ_KEYS;
  132. i2c_write (kbd_addr, 0, 0, &val, 1);
  133. i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
  134. if (memcmp(kbd_data, tmp_data, KEYBD_DATALEN) == 0) {
  135. /* consistent state, done */
  136. break;
  137. }
  138. /* remeber last state, delay, and retry */
  139. memcpy (tmp_data, kbd_data, KEYBD_DATALEN);
  140. udelay (5000);
  141. }
  142. }
  143. /***********************************************************************
  144. F* Function: int misc_init_r (void) P*A*Z*
  145. *
  146. P* Parameters: none
  147. P*
  148. P* Returnvalue: int
  149. P* - 0 is always returned, even in the case of a keyboard
  150. P* error.
  151. *
  152. Z* Intention: This function is the misc_init_r() method implementation
  153. Z* for the lwmon board.
  154. Z* The keyboard controller is initialized and the result
  155. Z* of a read copied to the environment variable "keybd".
  156. Z* If KEYBD_SET_DEBUGMODE is defined, a check is made for
  157. Z* this key, and if found display to the LCD will be enabled.
  158. Z* The keys in "keybd" are checked against the magic
  159. Z* keycommands defined in the environment.
  160. Z* See also key_match().
  161. *
  162. D* Design: wd@denx.de
  163. C* Coding: wd@denx.de
  164. V* Verification: dzu@denx.de
  165. ***********************************************************************/
  166. int misc_init_r_kbd (void)
  167. {
  168. uchar kbd_data[KEYBD_DATALEN];
  169. char keybd_env[2 * KEYBD_DATALEN + 1];
  170. uchar kbd_init_status = gd->kbd_status >> 8;
  171. uchar kbd_status = gd->kbd_status;
  172. uchar val;
  173. char *str;
  174. int i;
  175. if (kbd_init_status) {
  176. printf ("KEYBD: Error %02X\n", kbd_init_status);
  177. }
  178. if (kbd_status) { /* permanent error, report it */
  179. printf ("*** Keyboard error code %02X ***\n", kbd_status);
  180. sprintf (keybd_env, "%02X", kbd_status);
  181. setenv ("keybd", keybd_env);
  182. return 0;
  183. }
  184. /*
  185. * Now we know that we have a working keyboard, so disable
  186. * all output to the LCD except when a key press is detected.
  187. */
  188. if ((console_assign (stdout, "serial") < 0) ||
  189. (console_assign (stderr, "serial") < 0)) {
  190. printf ("Can't assign serial port as output device\n");
  191. }
  192. /* Read Version */
  193. val = KEYBD_CMD_READ_VERSION;
  194. i2c_write (kbd_addr, 0, 0, &val, 1);
  195. i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_VERSIONLEN);
  196. printf ("KEYBD: Version %d.%d\n", kbd_data[0], kbd_data[1]);
  197. /* Read current keyboard state */
  198. val = KEYBD_CMD_READ_KEYS;
  199. i2c_write (kbd_addr, 0, 0, &val, 1);
  200. i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
  201. for (i = 0; i < KEYBD_DATALEN; ++i) {
  202. sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
  203. }
  204. setenv ("keybd", keybd_env);
  205. str = strdup ((char *)key_match (kbd_data)); /* decode keys */
  206. #ifdef KEYBD_SET_DEBUGMODE
  207. if (kbd_data[0] == KEYBD_SET_DEBUGMODE) { /* set debug mode */
  208. if ((console_assign (stdout, "lcd") < 0) ||
  209. (console_assign (stderr, "lcd") < 0)) {
  210. printf ("Can't assign LCD display as output device\n");
  211. }
  212. }
  213. #endif /* KEYBD_SET_DEBUGMODE */
  214. #ifdef CONFIG_PREBOOT /* automatically configure "preboot" command on key match */
  215. setenv ("preboot", str); /* set or delete definition */
  216. #endif /* CONFIG_PREBOOT */
  217. if (str != NULL) {
  218. free (str);
  219. }
  220. return (0);
  221. }
  222. #ifdef CONFIG_PREBOOT
  223. static uchar kbd_magic_prefix[] = "key_magic";
  224. static uchar kbd_command_prefix[] = "key_cmd";
  225. static int compare_magic (uchar *kbd_data, uchar *str)
  226. {
  227. uchar compare[KEYBD_DATALEN-1];
  228. char *nxt;
  229. int i;
  230. /* Don't include modifier byte */
  231. memcpy (compare, kbd_data+1, KEYBD_DATALEN-1);
  232. for (; str != NULL; str = (*nxt) ? (uchar *)(nxt+1) : (uchar *)nxt) {
  233. uchar c;
  234. int k;
  235. c = (uchar) simple_strtoul ((char *)str, (char **) (&nxt), 16);
  236. if (str == (uchar *)nxt) { /* invalid character */
  237. break;
  238. }
  239. /*
  240. * Check if this key matches the input.
  241. * Set matches to zero, so they match only once
  242. * and we can find duplicates or extra keys
  243. */
  244. for (k = 0; k < sizeof(compare); ++k) {
  245. if (compare[k] == '\0') /* only non-zero entries */
  246. continue;
  247. if (c == compare[k]) { /* found matching key */
  248. compare[k] = '\0';
  249. break;
  250. }
  251. }
  252. if (k == sizeof(compare)) {
  253. return -1; /* unmatched key */
  254. }
  255. }
  256. /*
  257. * A full match leaves no keys in the `compare' array,
  258. */
  259. for (i = 0; i < sizeof(compare); ++i) {
  260. if (compare[i])
  261. {
  262. return -1;
  263. }
  264. }
  265. return 0;
  266. }
  267. /***********************************************************************
  268. F* Function: static uchar *key_match (uchar *kbd_data) P*A*Z*
  269. *
  270. P* Parameters: uchar *kbd_data
  271. P* - The keys to match against our magic definitions
  272. P*
  273. P* Returnvalue: uchar *
  274. P* - != NULL: Pointer to the corresponding command(s)
  275. P* NULL: No magic is about to happen
  276. *
  277. Z* Intention: Check if pressed key(s) match magic sequence,
  278. Z* and return the command string associated with that key(s).
  279. Z*
  280. Z* If no key press was decoded, NULL is returned.
  281. Z*
  282. Z* Note: the first character of the argument will be
  283. Z* overwritten with the "magic charcter code" of the
  284. Z* decoded key(s), or '\0'.
  285. Z*
  286. Z* Note: the string points to static environment data
  287. Z* and must be saved before you call any function that
  288. Z* modifies the environment.
  289. *
  290. D* Design: wd@denx.de
  291. C* Coding: wd@denx.de
  292. V* Verification: dzu@denx.de
  293. ***********************************************************************/
  294. static uchar *key_match (uchar *kbd_data)
  295. {
  296. char magic[sizeof (kbd_magic_prefix) + 1];
  297. uchar *suffix;
  298. char *kbd_magic_keys;
  299. /*
  300. * The following string defines the characters that can pe appended
  301. * to "key_magic" to form the names of environment variables that
  302. * hold "magic" key codes, i. e. such key codes that can cause
  303. * pre-boot actions. If the string is empty (""), then only
  304. * "key_magic" is checked (old behaviour); the string "125" causes
  305. * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
  306. */
  307. if ((kbd_magic_keys = getenv ("magic_keys")) == NULL)
  308. kbd_magic_keys = "";
  309. /* loop over all magic keys;
  310. * use '\0' suffix in case of empty string
  311. */
  312. for (suffix=(uchar *)kbd_magic_keys; *suffix || suffix==(uchar *)kbd_magic_keys; ++suffix) {
  313. sprintf (magic, "%s%c", kbd_magic_prefix, *suffix);
  314. debug ("### Check magic \"%s\"\n", magic);
  315. if (compare_magic(kbd_data, (uchar *)getenv(magic)) == 0) {
  316. char cmd_name[sizeof (kbd_command_prefix) + 1];
  317. char *cmd;
  318. sprintf (cmd_name, "%s%c", kbd_command_prefix, *suffix);
  319. cmd = getenv (cmd_name);
  320. debug ("### Set PREBOOT to $(%s): \"%s\"\n",
  321. cmd_name, cmd ? cmd : "<<NULL>>");
  322. *kbd_data = *suffix;
  323. return ((uchar *)cmd);
  324. }
  325. }
  326. debug ("### Delete PREBOOT\n");
  327. *kbd_data = '\0';
  328. return (NULL);
  329. }
  330. #endif /* CONFIG_PREBOOT */
  331. /***********************************************************************
  332. F* Function: int do_kbd (cmd_tbl_t *cmdtp, int flag,
  333. F* int argc, char *argv[]) P*A*Z*
  334. *
  335. P* Parameters: cmd_tbl_t *cmdtp
  336. P* - Pointer to our command table entry
  337. P* int flag
  338. P* - If the CMD_FLAG_REPEAT bit is set, then this call is
  339. P* a repetition
  340. P* int argc
  341. P* - Argument count
  342. P* char *argv[]
  343. P* - Array of the actual arguments
  344. P*
  345. P* Returnvalue: int
  346. P* - 0 is always returned.
  347. *
  348. Z* Intention: Implement the "kbd" command.
  349. Z* The keyboard status is read. The result is printed on
  350. Z* the console and written into the "keybd" environment
  351. Z* variable.
  352. *
  353. D* Design: wd@denx.de
  354. C* Coding: wd@denx.de
  355. V* Verification: dzu@denx.de
  356. ***********************************************************************/
  357. int do_kbd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  358. {
  359. uchar kbd_data[KEYBD_DATALEN];
  360. char keybd_env[2 * KEYBD_DATALEN + 1];
  361. uchar val;
  362. int i;
  363. #if 0 /* Done in kbd_init */
  364. i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  365. #endif
  366. /* Read keys */
  367. val = KEYBD_CMD_READ_KEYS;
  368. i2c_write (kbd_addr, 0, 0, &val, 1);
  369. i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
  370. puts ("Keys:");
  371. for (i = 0; i < KEYBD_DATALEN; ++i) {
  372. sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
  373. printf (" %02x", kbd_data[i]);
  374. }
  375. putc ('\n');
  376. setenv ("keybd", keybd_env);
  377. return 0;
  378. }
  379. U_BOOT_CMD(
  380. kbd, 1, 1, do_kbd,
  381. "read keyboard status",
  382. NULL
  383. );
  384. /*----------------------------- Utilities -----------------------------*/
  385. #ifdef CONFIG_POST
  386. /*
  387. * Returns 1 if keys pressed to start the power-on long-running tests
  388. * Called from board_init_f().
  389. */
  390. int post_hotkeys_pressed(void)
  391. {
  392. uchar kbd_data[KEYBD_DATALEN];
  393. uchar val;
  394. /* Read keys */
  395. val = KEYBD_CMD_READ_KEYS;
  396. i2c_write (kbd_addr, 0, 0, &val, 1);
  397. i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
  398. return (compare_magic(kbd_data, (uchar *)CONFIG_POST_KEY_MAGIC) == 0);
  399. }
  400. #endif