kbd.c 14 KB

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