kbd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /*
  2. * (C) Copyright 2001
  3. * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. *
  7. * Source partly derived from:
  8. * linux/drivers/char/pc_keyb.c
  9. */
  10. #include <common.h>
  11. #include <console.h>
  12. #include <asm/processor.h>
  13. #include <stdio_dev.h>
  14. #include "isa.h"
  15. #include "kbd.h"
  16. unsigned char kbd_read_status(void);
  17. unsigned char kbd_read_input(void);
  18. void kbd_send_data(unsigned char data);
  19. void disable_8259A_irq(unsigned int irq);
  20. void enable_8259A_irq(unsigned int irq);
  21. /* used only by send_data - set by keyboard_interrupt */
  22. #undef KBG_DEBUG
  23. #ifdef KBG_DEBUG
  24. #define PRINTF(fmt,args...) printf (fmt ,##args)
  25. #else
  26. #define PRINTF(fmt,args...)
  27. #endif
  28. #define KBD_STAT_KOBF 0x01
  29. #define KBD_STAT_IBF 0x02
  30. #define KBD_STAT_SYS 0x04
  31. #define KBD_STAT_CD 0x08
  32. #define KBD_STAT_LOCK 0x10
  33. #define KBD_STAT_MOBF 0x20
  34. #define KBD_STAT_TI_OUT 0x40
  35. #define KBD_STAT_PARERR 0x80
  36. #define KBD_INIT_TIMEOUT 1000 /* Timeout in ms for initializing the keyboard */
  37. #define KBC_TIMEOUT 250 /* Timeout in ms for sending to keyboard controller */
  38. #define KBD_TIMEOUT 2000 /* Timeout in ms for keyboard command acknowledge */
  39. /*
  40. * Keyboard Controller Commands
  41. */
  42. #define KBD_CCMD_READ_MODE 0x20 /* Read mode bits */
  43. #define KBD_CCMD_WRITE_MODE 0x60 /* Write mode bits */
  44. #define KBD_CCMD_GET_VERSION 0xA1 /* Get controller version */
  45. #define KBD_CCMD_MOUSE_DISABLE 0xA7 /* Disable mouse interface */
  46. #define KBD_CCMD_MOUSE_ENABLE 0xA8 /* Enable mouse interface */
  47. #define KBD_CCMD_TEST_MOUSE 0xA9 /* Mouse interface test */
  48. #define KBD_CCMD_SELF_TEST 0xAA /* Controller self test */
  49. #define KBD_CCMD_KBD_TEST 0xAB /* Keyboard interface test */
  50. #define KBD_CCMD_KBD_DISABLE 0xAD /* Keyboard interface disable */
  51. #define KBD_CCMD_KBD_ENABLE 0xAE /* Keyboard interface enable */
  52. #define KBD_CCMD_WRITE_AUX_OBUF 0xD3 /* Write to output buffer as if
  53. initiated by the auxiliary device */
  54. #define KBD_CCMD_WRITE_MOUSE 0xD4 /* Write the following byte to the mouse */
  55. /*
  56. * Keyboard Commands
  57. */
  58. #define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */
  59. #define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */
  60. #define KBD_CMD_ENABLE 0xF4 /* Enable scanning */
  61. #define KBD_CMD_DISABLE 0xF5 /* Disable scanning */
  62. #define KBD_CMD_RESET 0xFF /* Reset */
  63. /*
  64. * Keyboard Replies
  65. */
  66. #define KBD_REPLY_POR 0xAA /* Power on reset */
  67. #define KBD_REPLY_ACK 0xFA /* Command ACK */
  68. #define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */
  69. /*
  70. * Status Register Bits
  71. */
  72. #define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */
  73. #define KBD_STAT_IBF 0x02 /* Keyboard input buffer full */
  74. #define KBD_STAT_SELFTEST 0x04 /* Self test successful */
  75. #define KBD_STAT_CMD 0x08 /* Last write was a command write (0=data) */
  76. #define KBD_STAT_UNLOCKED 0x10 /* Zero if keyboard locked */
  77. #define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */
  78. #define KBD_STAT_GTO 0x40 /* General receive/xmit timeout */
  79. #define KBD_STAT_PERR 0x80 /* Parity error */
  80. #define AUX_STAT_OBF (KBD_STAT_OBF | KBD_STAT_MOUSE_OBF)
  81. /*
  82. * Controller Mode Register Bits
  83. */
  84. #define KBD_MODE_KBD_INT 0x01 /* Keyboard data generate IRQ1 */
  85. #define KBD_MODE_MOUSE_INT 0x02 /* Mouse data generate IRQ12 */
  86. #define KBD_MODE_SYS 0x04 /* The system flag (?) */
  87. #define KBD_MODE_NO_KEYLOCK 0x08 /* The keylock doesn't affect the keyboard if set */
  88. #define KBD_MODE_DISABLE_KBD 0x10 /* Disable keyboard interface */
  89. #define KBD_MODE_DISABLE_MOUSE 0x20 /* Disable mouse interface */
  90. #define KBD_MODE_KCC 0x40 /* Scan code conversion to PC format */
  91. #define KBD_MODE_RFU 0x80
  92. #define KDB_DATA_PORT 0x60
  93. #define KDB_COMMAND_PORT 0x64
  94. #define LED_SCR 0x01 /* scroll lock led */
  95. #define LED_CAP 0x04 /* caps lock led */
  96. #define LED_NUM 0x02 /* num lock led */
  97. #define KBD_BUFFER_LEN 0x20 /* size of the keyboardbuffer */
  98. static volatile char kbd_buffer[KBD_BUFFER_LEN];
  99. static volatile int in_pointer = 0;
  100. static volatile int out_pointer = 0;
  101. static unsigned char num_lock = 0;
  102. static unsigned char caps_lock = 0;
  103. static unsigned char scroll_lock = 0;
  104. static unsigned char shift = 0;
  105. static unsigned char ctrl = 0;
  106. static unsigned char alt = 0;
  107. static unsigned char e0 = 0;
  108. static unsigned char leds = 0;
  109. #define DEVNAME "kbd"
  110. /* Simple translation table for the keys */
  111. static unsigned char kbd_plain_xlate[] = {
  112. 0xff,0x1b, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=','\b','\t', /* 0x00 - 0x0f */
  113. 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']','\r',0xff, 'a', 's', /* 0x10 - 0x1f */
  114. 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';','\'', '`',0xff,'\\', 'z', 'x', 'c', 'v', /* 0x20 - 0x2f */
  115. 'b', 'n', 'm', ',', '.', '/',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */
  116. 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
  117. '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */
  118. '\r',0xff,0xff
  119. };
  120. static unsigned char kbd_shift_xlate[] = {
  121. 0xff,0x1b, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+','\b','\t', /* 0x00 - 0x0f */
  122. 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}','\r',0xff, 'A', 'S', /* 0x10 - 0x1f */
  123. 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',0xff, '|', 'Z', 'X', 'C', 'V', /* 0x20 - 0x2f */
  124. 'B', 'N', 'M', '<', '>', '?',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */
  125. 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
  126. '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */
  127. '\r',0xff,0xff
  128. };
  129. static unsigned char kbd_ctrl_xlate[] = {
  130. 0xff,0x1b, '1',0x00, '3', '4', '5',0x1E, '7', '8', '9', '0',0x1F, '=','\b','\t', /* 0x00 - 0x0f */
  131. 0x11,0x17,0x05,0x12,0x14,0x18,0x15,0x09,0x0f,0x10,0x1b,0x1d,'\n',0xff,0x01,0x13, /* 0x10 - 0x1f */
  132. 0x04,0x06,0x08,0x09,0x0a,0x0b,0x0c, ';','\'', '~',0x00,0x1c,0x1a,0x18,0x03,0x16, /* 0x20 - 0x2f */
  133. 0x02,0x0e,0x0d, '<', '>', '?',0xff,0xff,0xff,0x00,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */
  134. 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
  135. '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */
  136. '\r',0xff,0xff
  137. };
  138. /******************************************************************
  139. * Init
  140. ******************************************************************/
  141. int isa_kbd_init(void)
  142. {
  143. char* result;
  144. result=kbd_initialize();
  145. if(result==NULL) {
  146. PRINTF("AT Keyboard initialized\n");
  147. irq_install_handler(25, (interrupt_handler_t *)handle_isa_int, NULL);
  148. isa_irq_install_handler(KBD_INTERRUPT, (interrupt_handler_t *)kbd_interrupt, NULL);
  149. return (1);
  150. } else {
  151. printf("%s\n",result);
  152. return (-1);
  153. }
  154. }
  155. #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
  156. extern int overwrite_console (void);
  157. #else
  158. int overwrite_console (void)
  159. {
  160. return (0);
  161. }
  162. #endif
  163. int drv_isa_kbd_init (void)
  164. {
  165. int error;
  166. struct stdio_dev kbddev ;
  167. char *stdinname = getenv ("stdin");
  168. if(isa_kbd_init()==-1)
  169. return -1;
  170. memset (&kbddev, 0, sizeof(kbddev));
  171. strcpy(kbddev.name, DEVNAME);
  172. kbddev.flags = DEV_FLAGS_INPUT;
  173. kbddev.getc = kbd_getc ;
  174. kbddev.tstc = kbd_testc ;
  175. error = stdio_register (&kbddev);
  176. if(error==0) {
  177. /* check if this is the standard input device */
  178. if(strcmp(stdinname,DEVNAME)==0) {
  179. /* reassign the console */
  180. if(overwrite_console()) {
  181. return 1;
  182. }
  183. error=console_assign(stdin,DEVNAME);
  184. if(error==0)
  185. return 1;
  186. else
  187. return error;
  188. }
  189. return 1;
  190. }
  191. return error;
  192. }
  193. /******************************************************************
  194. * Queue handling
  195. ******************************************************************/
  196. /* puts character in the queue and sets up the in and out pointer */
  197. void kbd_put_queue(char data)
  198. {
  199. if((in_pointer+1)==KBD_BUFFER_LEN) {
  200. if(out_pointer==0) {
  201. return; /* buffer full */
  202. } else{
  203. in_pointer=0;
  204. }
  205. } else {
  206. if((in_pointer+1)==out_pointer)
  207. return; /* buffer full */
  208. in_pointer++;
  209. }
  210. kbd_buffer[in_pointer]=data;
  211. return;
  212. }
  213. /* test if a character is in the queue */
  214. int kbd_testc(struct stdio_dev *dev)
  215. {
  216. if(in_pointer==out_pointer)
  217. return(0); /* no data */
  218. else
  219. return(1);
  220. }
  221. /* gets the character from the queue */
  222. int kbd_getc(struct stdio_dev *dev)
  223. {
  224. char c;
  225. while(in_pointer==out_pointer);
  226. if((out_pointer+1)==KBD_BUFFER_LEN)
  227. out_pointer=0;
  228. else
  229. out_pointer++;
  230. c=kbd_buffer[out_pointer];
  231. return (int)c;
  232. }
  233. /* set LEDs */
  234. void kbd_set_leds(void)
  235. {
  236. if(caps_lock==0)
  237. leds&=~LED_CAP; /* switch caps_lock off */
  238. else
  239. leds|=LED_CAP; /* switch on LED */
  240. if(num_lock==0)
  241. leds&=~LED_NUM; /* switch LED off */
  242. else
  243. leds|=LED_NUM; /* switch on LED */
  244. if(scroll_lock==0)
  245. leds&=~LED_SCR; /* switch LED off */
  246. else
  247. leds|=LED_SCR; /* switch on LED */
  248. kbd_send_data(KBD_CMD_SET_LEDS);
  249. kbd_send_data(leds);
  250. }
  251. void handle_keyboard_event (unsigned char scancode)
  252. {
  253. unsigned char keycode;
  254. /* Convert scancode to keycode */
  255. PRINTF ("scancode %x\n", scancode);
  256. if (scancode == 0xe0) {
  257. e0 = 1; /* special charakters */
  258. return;
  259. }
  260. if (e0 == 1) {
  261. e0 = 0; /* delete flag */
  262. if (!(((scancode & 0x7F) == 0x38) || /* the right ctrl key */
  263. ((scancode & 0x7F) == 0x1D) || /* the right alt key */
  264. ((scancode & 0x7F) == 0x35) || /* the right '/' key */
  265. ((scancode & 0x7F) == 0x1C)))
  266. /* the right enter key */
  267. /* we swallow unknown e0 codes */
  268. return;
  269. }
  270. /* special cntrl keys */
  271. switch (scancode) {
  272. case 0x2A:
  273. case 0x36: /* shift pressed */
  274. shift = 1;
  275. return; /* do nothing else */
  276. case 0xAA:
  277. case 0xB6: /* shift released */
  278. shift = 0;
  279. return; /* do nothing else */
  280. case 0x38: /* alt pressed */
  281. alt = 1;
  282. return; /* do nothing else */
  283. case 0xB8: /* alt released */
  284. alt = 0;
  285. return; /* do nothing else */
  286. case 0x1d: /* ctrl pressed */
  287. ctrl = 1;
  288. return; /* do nothing else */
  289. case 0x9d: /* ctrl released */
  290. ctrl = 0;
  291. return; /* do nothing else */
  292. case 0x46: /* scrollock pressed */
  293. scroll_lock = ~scroll_lock;
  294. kbd_set_leds ();
  295. return; /* do nothing else */
  296. case 0x3A: /* capslock pressed */
  297. caps_lock = ~caps_lock;
  298. kbd_set_leds ();
  299. return;
  300. case 0x45: /* numlock pressed */
  301. num_lock = ~num_lock;
  302. kbd_set_leds ();
  303. return;
  304. case 0xC6: /* scroll lock released */
  305. case 0xC5: /* num lock released */
  306. case 0xBA: /* caps lock released */
  307. return; /* just swallow */
  308. }
  309. if ((scancode & 0x80) == 0x80) /* key released */
  310. return;
  311. /* now, decide which table we need */
  312. if (scancode > (sizeof (kbd_plain_xlate) / sizeof (kbd_plain_xlate[0]))) { /* scancode not in list */
  313. PRINTF ("unkown scancode %X\n", scancode);
  314. return; /* swallow it */
  315. }
  316. /* setup plain code first */
  317. keycode = kbd_plain_xlate[scancode];
  318. if (caps_lock == 1) { /* caps_lock is pressed, overwrite plain code */
  319. if (scancode > (sizeof (kbd_shift_xlate) / sizeof (kbd_shift_xlate[0]))) { /* scancode not in list */
  320. PRINTF ("unkown caps-locked scancode %X\n", scancode);
  321. return; /* swallow it */
  322. }
  323. keycode = kbd_shift_xlate[scancode];
  324. if (keycode < 'A') { /* we only want the alphas capital */
  325. keycode = kbd_plain_xlate[scancode];
  326. }
  327. }
  328. if (shift == 1) { /* shift overwrites caps_lock */
  329. if (scancode > (sizeof (kbd_shift_xlate) / sizeof (kbd_shift_xlate[0]))) { /* scancode not in list */
  330. PRINTF ("unkown shifted scancode %X\n", scancode);
  331. return; /* swallow it */
  332. }
  333. keycode = kbd_shift_xlate[scancode];
  334. }
  335. if (ctrl == 1) { /* ctrl overwrites caps_lock and shift */
  336. if (scancode > (sizeof (kbd_ctrl_xlate) / sizeof (kbd_ctrl_xlate[0]))) { /* scancode not in list */
  337. PRINTF ("unkown ctrl scancode %X\n", scancode);
  338. return; /* swallow it */
  339. }
  340. keycode = kbd_ctrl_xlate[scancode];
  341. }
  342. /* check if valid keycode */
  343. if (keycode == 0xff) {
  344. PRINTF ("unkown scancode %X\n", scancode);
  345. return; /* swallow unknown codes */
  346. }
  347. kbd_put_queue (keycode);
  348. PRINTF ("%x\n", keycode);
  349. }
  350. /*
  351. * This reads the keyboard status port, and does the
  352. * appropriate action.
  353. *
  354. */
  355. unsigned char handle_kbd_event(void)
  356. {
  357. unsigned char status = kbd_read_status();
  358. unsigned int work = 10000;
  359. while ((--work > 0) && (status & KBD_STAT_OBF)) {
  360. unsigned char scancode;
  361. scancode = kbd_read_input();
  362. /* Error bytes must be ignored to make the
  363. Synaptics touchpads compaq use work */
  364. /* Ignore error bytes */
  365. if (!(status & (KBD_STAT_GTO | KBD_STAT_PERR)))
  366. {
  367. if (status & KBD_STAT_MOUSE_OBF)
  368. ; /* not supported: handle_mouse_event(scancode); */
  369. else
  370. handle_keyboard_event(scancode);
  371. }
  372. status = kbd_read_status();
  373. }
  374. if (!work)
  375. PRINTF("pc_keyb: controller jammed (0x%02X).\n", status);
  376. return status;
  377. }
  378. /******************************************************************************
  379. * Lowlevel Part of keyboard section
  380. */
  381. unsigned char kbd_read_status(void)
  382. {
  383. return(in8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_COMMAND_PORT));
  384. }
  385. unsigned char kbd_read_input(void)
  386. {
  387. return(in8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_DATA_PORT));
  388. }
  389. void kbd_write_command(unsigned char cmd)
  390. {
  391. out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_COMMAND_PORT,cmd);
  392. }
  393. void kbd_write_output(unsigned char data)
  394. {
  395. out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_DATA_PORT, data);
  396. }
  397. int kbd_read_data(void)
  398. {
  399. int val;
  400. unsigned char status;
  401. val = -1;
  402. status = kbd_read_status();
  403. if (status & KBD_STAT_OBF) {
  404. val = kbd_read_input();
  405. if (status & (KBD_STAT_GTO | KBD_STAT_PERR))
  406. val = -2;
  407. }
  408. return val;
  409. }
  410. int kbd_wait_for_input(void)
  411. {
  412. unsigned long timeout;
  413. int val;
  414. timeout = KBD_TIMEOUT;
  415. val=kbd_read_data();
  416. while(val < 0)
  417. {
  418. if(timeout--==0)
  419. return -1;
  420. udelay(1000);
  421. val=kbd_read_data();
  422. }
  423. return val;
  424. }
  425. int kb_wait(void)
  426. {
  427. unsigned long timeout = KBC_TIMEOUT * 10;
  428. do {
  429. unsigned char status = handle_kbd_event();
  430. if (!(status & KBD_STAT_IBF))
  431. return 0; /* ok */
  432. udelay(1000);
  433. timeout--;
  434. } while (timeout);
  435. return 1;
  436. }
  437. void kbd_write_command_w(int data)
  438. {
  439. if(kb_wait())
  440. PRINTF("timeout in kbd_write_command_w\n");
  441. kbd_write_command(data);
  442. }
  443. void kbd_write_output_w(int data)
  444. {
  445. if(kb_wait())
  446. PRINTF("timeout in kbd_write_output_w\n");
  447. kbd_write_output(data);
  448. }
  449. void kbd_send_data(unsigned char data)
  450. {
  451. unsigned char status;
  452. disable_8259A_irq(1); /* disable interrupt */
  453. kbd_write_output_w(data);
  454. status = kbd_wait_for_input();
  455. if (status == KBD_REPLY_ACK)
  456. enable_8259A_irq(1); /* enable interrupt */
  457. }
  458. char * kbd_initialize(void)
  459. {
  460. int status;
  461. in_pointer = 0; /* delete in Buffer */
  462. out_pointer = 0;
  463. /*
  464. * Test the keyboard interface.
  465. * This seems to be the only way to get it going.
  466. * If the test is successful a x55 is placed in the input buffer.
  467. */
  468. kbd_write_command_w(KBD_CCMD_SELF_TEST);
  469. if (kbd_wait_for_input() != 0x55)
  470. return "Kbd: failed self test";
  471. /*
  472. * Perform a keyboard interface test. This causes the controller
  473. * to test the keyboard clock and data lines. The results of the
  474. * test are placed in the input buffer.
  475. */
  476. kbd_write_command_w(KBD_CCMD_KBD_TEST);
  477. if (kbd_wait_for_input() != 0x00)
  478. return "Kbd: interface failed self test";
  479. /*
  480. * Enable the keyboard by allowing the keyboard clock to run.
  481. */
  482. kbd_write_command_w(KBD_CCMD_KBD_ENABLE);
  483. status = kbd_wait_for_input();
  484. /*
  485. * Reset keyboard. If the read times out
  486. * then the assumption is that no keyboard is
  487. * plugged into the machine.
  488. * This defaults the keyboard to scan-code set 2.
  489. *
  490. * Set up to try again if the keyboard asks for RESEND.
  491. */
  492. do {
  493. kbd_write_output_w(KBD_CMD_RESET);
  494. status = kbd_wait_for_input();
  495. if (status == KBD_REPLY_ACK)
  496. break;
  497. if (status != KBD_REPLY_RESEND) {
  498. PRINTF("status: %X\n",status);
  499. return "Kbd: reset failed, no ACK";
  500. }
  501. } while (1);
  502. if (kbd_wait_for_input() != KBD_REPLY_POR)
  503. return "Kbd: reset failed, no POR";
  504. /*
  505. * Set keyboard controller mode. During this, the keyboard should be
  506. * in the disabled state.
  507. *
  508. * Set up to try again if the keyboard asks for RESEND.
  509. */
  510. do {
  511. kbd_write_output_w(KBD_CMD_DISABLE);
  512. status = kbd_wait_for_input();
  513. if (status == KBD_REPLY_ACK)
  514. break;
  515. if (status != KBD_REPLY_RESEND)
  516. return "Kbd: disable keyboard: no ACK";
  517. } while (1);
  518. kbd_write_command_w(KBD_CCMD_WRITE_MODE);
  519. kbd_write_output_w(KBD_MODE_KBD_INT
  520. | KBD_MODE_SYS
  521. | KBD_MODE_DISABLE_MOUSE
  522. | KBD_MODE_KCC);
  523. /* AMCC powerpc portables need this to use scan-code set 1 -- Cort */
  524. kbd_write_command_w(KBD_CCMD_READ_MODE);
  525. if (!(kbd_wait_for_input() & KBD_MODE_KCC)) {
  526. /*
  527. * If the controller does not support conversion,
  528. * Set the keyboard to scan-code set 1.
  529. */
  530. kbd_write_output_w(0xF0);
  531. kbd_wait_for_input();
  532. kbd_write_output_w(0x01);
  533. kbd_wait_for_input();
  534. }
  535. kbd_write_output_w(KBD_CMD_ENABLE);
  536. if (kbd_wait_for_input() != KBD_REPLY_ACK)
  537. return "Kbd: enable keyboard: no ACK";
  538. /*
  539. * Finally, set the typematic rate to maximum.
  540. */
  541. kbd_write_output_w(KBD_CMD_SET_RATE);
  542. if (kbd_wait_for_input() != KBD_REPLY_ACK)
  543. return "Kbd: Set rate: no ACK";
  544. kbd_write_output_w(0x00);
  545. if (kbd_wait_for_input() != KBD_REPLY_ACK)
  546. return "Kbd: Set rate: no ACK";
  547. return NULL;
  548. }
  549. void kbd_interrupt(void)
  550. {
  551. handle_kbd_event();
  552. }