kbd.c 18 KB

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