kbd.c 14 KB

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