kbd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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. }
  169. else {
  170. printf("%s\n",result);
  171. return (-1);
  172. }
  173. }
  174. #ifdef CFG_CONSOLE_OVERWRITE_ROUTINE
  175. extern int overwrite_console (void);
  176. #else
  177. int overwrite_console (void)
  178. {
  179. return (0);
  180. }
  181. #endif
  182. int drv_isa_kbd_init (void)
  183. {
  184. int error;
  185. device_t kbddev ;
  186. char *stdinname = getenv ("stdin");
  187. if(isa_kbd_init()==-1)
  188. return -1;
  189. memset (&kbddev, 0, sizeof(kbddev));
  190. strcpy(kbddev.name, DEVNAME);
  191. kbddev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
  192. kbddev.putc = NULL ;
  193. kbddev.puts = NULL ;
  194. kbddev.getc = kbd_getc ;
  195. kbddev.tstc = kbd_testc ;
  196. error = device_register (&kbddev);
  197. if(error==0) {
  198. /* check if this is the standard input device */
  199. if(strcmp(stdinname,DEVNAME)==0) {
  200. /* reassign the console */
  201. if(overwrite_console()) {
  202. return 1;
  203. }
  204. error=console_assign(stdin,DEVNAME);
  205. if(error==0)
  206. return 1;
  207. else
  208. return error;
  209. }
  210. return 1;
  211. }
  212. return error;
  213. }
  214. /******************************************************************
  215. * Queue handling
  216. ******************************************************************/
  217. /* puts character in the queue and sets up the in and out pointer */
  218. void kbd_put_queue(char data)
  219. {
  220. if((in_pointer+1)==KBD_BUFFER_LEN) {
  221. if(out_pointer==0) {
  222. return; /* buffer full */
  223. } else{
  224. in_pointer=0;
  225. }
  226. } else {
  227. if((in_pointer+1)==out_pointer)
  228. return; /* buffer full */
  229. in_pointer++;
  230. }
  231. kbd_buffer[in_pointer]=data;
  232. return;
  233. }
  234. /* test if a character is in the queue */
  235. int kbd_testc(void)
  236. {
  237. if(in_pointer==out_pointer)
  238. return(0); /* no data */
  239. else
  240. return(1);
  241. }
  242. /* gets the character from the queue */
  243. int kbd_getc(void)
  244. {
  245. char c;
  246. while(in_pointer==out_pointer);
  247. if((out_pointer+1)==KBD_BUFFER_LEN)
  248. out_pointer=0;
  249. else
  250. out_pointer++;
  251. c=kbd_buffer[out_pointer];
  252. return (int)c;
  253. }
  254. /* set LEDs */
  255. void kbd_set_leds(void)
  256. {
  257. if(caps_lock==0)
  258. leds&=~LED_CAP; /* switch caps_lock off */
  259. else
  260. leds|=LED_CAP; /* switch on LED */
  261. if(num_lock==0)
  262. leds&=~LED_NUM; /* switch LED off */
  263. else
  264. leds|=LED_NUM; /* switch on LED */
  265. if(scroll_lock==0)
  266. leds&=~LED_SCR; /* switch LED off */
  267. else
  268. leds|=LED_SCR; /* switch on LED */
  269. kbd_send_data(KBD_CMD_SET_LEDS);
  270. kbd_send_data(leds);
  271. }
  272. void handle_keyboard_event(unsigned char scancode)
  273. {
  274. unsigned char keycode;
  275. /* Convert scancode to keycode */
  276. PRINTF("scancode %x\n",scancode);
  277. if(scancode==0xe0) {
  278. e0=1; /* special charakters */
  279. return;
  280. }
  281. if(e0==1) {
  282. e0=0; /* delete flag */
  283. if(!( ((scancode&0x7F)==0x38)|| /* the right ctrl key */
  284. ((scancode&0x7F)==0x1D)|| /* the right alt key */
  285. ((scancode&0x7F)==0x35)|| /* the right '/' key */
  286. ((scancode&0x7F)==0x1C) )) /* the right enter key */
  287. /* we swallow unknown e0 codes */
  288. return;
  289. }
  290. /* special cntrl keys */
  291. switch(scancode)
  292. {
  293. case 0x2A:
  294. case 0x36: /* shift pressed */
  295. shift=1;
  296. return; /* do nothing else */
  297. case 0xAA:
  298. case 0xB6: /* shift released */
  299. shift=0;
  300. return; /* do nothing else */
  301. case 0x38: /* alt pressed */
  302. alt=1;
  303. return; /* do nothing else */
  304. case 0xB8: /* alt released */
  305. alt=0;
  306. return; /* do nothing else */
  307. case 0x1d: /* ctrl pressed */
  308. ctrl=1;
  309. return; /* do nothing else */
  310. case 0x9d: /* ctrl released */
  311. ctrl=0;
  312. return; /* do nothing else */
  313. case 0x46: /* scrollock pressed */
  314. scroll_lock=~scroll_lock;
  315. kbd_set_leds();
  316. return; /* do nothing else */
  317. case 0x3A: /* capslock pressed */
  318. caps_lock=~caps_lock;
  319. kbd_set_leds();
  320. return;
  321. case 0x45: /* numlock pressed */
  322. num_lock=~num_lock;
  323. kbd_set_leds();
  324. return;
  325. case 0xC6: /* scroll lock released */
  326. case 0xC5: /* num lock released */
  327. case 0xBA: /* caps lock released */
  328. return; /* just swallow */
  329. }
  330. if((scancode&0x80)==0x80) /* key released */
  331. return;
  332. /* now, decide which table we need */
  333. if(scancode > (sizeof(kbd_plain_xlate)/sizeof(kbd_plain_xlate[0]))) { /* scancode not in list */
  334. PRINTF("unkown scancode %X\n",scancode);
  335. return; /* swallow it */
  336. }
  337. /* setup plain code first */
  338. keycode=kbd_plain_xlate[scancode];
  339. if(caps_lock==1) { /* caps_lock is pressed, overwrite plain code */
  340. if(scancode > (sizeof(kbd_shift_xlate)/sizeof(kbd_shift_xlate[0]))) { /* scancode not in list */
  341. PRINTF("unkown caps-locked scancode %X\n",scancode);
  342. return; /* swallow it */
  343. }
  344. keycode=kbd_shift_xlate[scancode];
  345. if(keycode<'A') { /* we only want the alphas capital */
  346. keycode=kbd_plain_xlate[scancode];
  347. }
  348. }
  349. if(shift==1) { /* shift overwrites caps_lock */
  350. if(scancode > (sizeof(kbd_shift_xlate)/sizeof(kbd_shift_xlate[0]))) { /* scancode not in list */
  351. PRINTF("unkown shifted scancode %X\n",scancode);
  352. return; /* swallow it */
  353. }
  354. keycode=kbd_shift_xlate[scancode];
  355. }
  356. if(ctrl==1) { /* ctrl overwrites caps_lock and shift */
  357. if(scancode > (sizeof(kbd_ctrl_xlate)/sizeof(kbd_ctrl_xlate[0]))) { /* scancode not in list */
  358. PRINTF("unkown ctrl scancode %X\n",scancode);
  359. return; /* swallow it */
  360. }
  361. keycode=kbd_ctrl_xlate[scancode];
  362. }
  363. /* check if valid keycode */
  364. if(keycode==0xff) {
  365. PRINTF("unkown scancode %X\n",scancode);
  366. return; /* swallow unknown codes */
  367. }
  368. kbd_put_queue(keycode);
  369. PRINTF("%x\n",keycode);
  370. }
  371. /*
  372. * This reads the keyboard status port, and does the
  373. * appropriate action.
  374. *
  375. */
  376. unsigned char handle_kbd_event(void)
  377. {
  378. unsigned char status = kbd_read_status();
  379. unsigned int work = 10000;
  380. while ((--work > 0) && (status & KBD_STAT_OBF)) {
  381. unsigned char scancode;
  382. scancode = kbd_read_input();
  383. /* Error bytes must be ignored to make the
  384. Synaptics touchpads compaq use work */
  385. /* Ignore error bytes */
  386. if (!(status & (KBD_STAT_GTO | KBD_STAT_PERR)))
  387. {
  388. if (status & KBD_STAT_MOUSE_OBF)
  389. ; /* not supported: handle_mouse_event(scancode); */
  390. else
  391. handle_keyboard_event(scancode);
  392. }
  393. status = kbd_read_status();
  394. }
  395. if (!work)
  396. PRINTF("pc_keyb: controller jammed (0x%02X).\n", status);
  397. return status;
  398. }
  399. /******************************************************************************
  400. * Lowlevel Part of keyboard section
  401. */
  402. unsigned char kbd_read_status(void)
  403. {
  404. return(in8(CFG_ISA_IO_BASE_ADDRESS + KDB_COMMAND_PORT));
  405. }
  406. unsigned char kbd_read_input(void)
  407. {
  408. return(in8(CFG_ISA_IO_BASE_ADDRESS + KDB_DATA_PORT));
  409. }
  410. void kbd_write_command(unsigned char cmd)
  411. {
  412. out8(CFG_ISA_IO_BASE_ADDRESS + KDB_COMMAND_PORT,cmd);
  413. }
  414. void kbd_write_output(unsigned char data)
  415. {
  416. out8(CFG_ISA_IO_BASE_ADDRESS + KDB_DATA_PORT, data);
  417. }
  418. int kbd_read_data(void)
  419. {
  420. int val;
  421. unsigned char status;
  422. val=-1;
  423. status = kbd_read_status();
  424. if (status & KBD_STAT_OBF) {
  425. val = kbd_read_input();
  426. if (status & (KBD_STAT_GTO | KBD_STAT_PERR))
  427. val = -2;
  428. }
  429. return val;
  430. }
  431. int kbd_wait_for_input(void)
  432. {
  433. unsigned long timeout;
  434. int val;
  435. timeout = KBD_TIMEOUT;
  436. val=kbd_read_data();
  437. while(val < 0)
  438. {
  439. if(timeout--==0)
  440. return -1;
  441. udelay(1000);
  442. val=kbd_read_data();
  443. }
  444. return val;
  445. }
  446. int kb_wait(void)
  447. {
  448. unsigned long timeout = KBC_TIMEOUT * 10;
  449. do {
  450. unsigned char status = handle_kbd_event();
  451. if (!(status & KBD_STAT_IBF))
  452. return 0; /* ok */
  453. udelay(1000);
  454. timeout--;
  455. } while (timeout);
  456. return 1;
  457. }
  458. void kbd_write_command_w(int data)
  459. {
  460. if(kb_wait())
  461. PRINTF("timeout in kbd_write_command_w\n");
  462. kbd_write_command(data);
  463. }
  464. void kbd_write_output_w(int data)
  465. {
  466. if(kb_wait())
  467. PRINTF("timeout in kbd_write_output_w\n");
  468. kbd_write_output(data);
  469. }
  470. void kbd_send_data(unsigned char data)
  471. {
  472. unsigned char status;
  473. disable_8259A_irq(1); /* disable interrupt */
  474. kbd_write_output_w(data);
  475. status = kbd_wait_for_input();
  476. if (status == KBD_REPLY_ACK)
  477. enable_8259A_irq(1); /* enable interrupt */
  478. }
  479. char * kbd_initialize(void)
  480. {
  481. int status;
  482. in_pointer = 0; /* delete in Buffer */
  483. out_pointer = 0;
  484. /*
  485. * Test the keyboard interface.
  486. * This seems to be the only way to get it going.
  487. * If the test is successful a x55 is placed in the input buffer.
  488. */
  489. kbd_write_command_w(KBD_CCMD_SELF_TEST);
  490. if (kbd_wait_for_input() != 0x55)
  491. return "Kbd: failed self test";
  492. /*
  493. * Perform a keyboard interface test. This causes the controller
  494. * to test the keyboard clock and data lines. The results of the
  495. * test are placed in the input buffer.
  496. */
  497. kbd_write_command_w(KBD_CCMD_KBD_TEST);
  498. if (kbd_wait_for_input() != 0x00)
  499. return "Kbd: interface failed self test";
  500. /*
  501. * Enable the keyboard by allowing the keyboard clock to run.
  502. */
  503. kbd_write_command_w(KBD_CCMD_KBD_ENABLE);
  504. status = kbd_wait_for_input();
  505. /*
  506. * Reset keyboard. If the read times out
  507. * then the assumption is that no keyboard is
  508. * plugged into the machine.
  509. * This defaults the keyboard to scan-code set 2.
  510. *
  511. * Set up to try again if the keyboard asks for RESEND.
  512. */
  513. do {
  514. kbd_write_output_w(KBD_CMD_RESET);
  515. status = kbd_wait_for_input();
  516. if (status == KBD_REPLY_ACK)
  517. break;
  518. if (status != KBD_REPLY_RESEND)
  519. {
  520. PRINTF("status: %X\n",status);
  521. return "Kbd: reset failed, no ACK";
  522. }
  523. } while (1);
  524. if (kbd_wait_for_input() != KBD_REPLY_POR)
  525. return "Kbd: reset failed, no POR";
  526. /*
  527. * Set keyboard controller mode. During this, the keyboard should be
  528. * in the disabled state.
  529. *
  530. * Set up to try again if the keyboard asks for RESEND.
  531. */
  532. do {
  533. kbd_write_output_w(KBD_CMD_DISABLE);
  534. status = kbd_wait_for_input();
  535. if (status == KBD_REPLY_ACK)
  536. break;
  537. if (status != KBD_REPLY_RESEND)
  538. return "Kbd: disable keyboard: no ACK";
  539. } while (1);
  540. kbd_write_command_w(KBD_CCMD_WRITE_MODE);
  541. kbd_write_output_w(KBD_MODE_KBD_INT
  542. | KBD_MODE_SYS
  543. | KBD_MODE_DISABLE_MOUSE
  544. | KBD_MODE_KCC);
  545. /* AMCC powerpc portables need this to use scan-code set 1 -- Cort */
  546. kbd_write_command_w(KBD_CCMD_READ_MODE);
  547. if (!(kbd_wait_for_input() & KBD_MODE_KCC)) {
  548. /*
  549. * If the controller does not support conversion,
  550. * Set the keyboard to scan-code set 1.
  551. */
  552. kbd_write_output_w(0xF0);
  553. kbd_wait_for_input();
  554. kbd_write_output_w(0x01);
  555. kbd_wait_for_input();
  556. }
  557. kbd_write_output_w(KBD_CMD_ENABLE);
  558. if (kbd_wait_for_input() != KBD_REPLY_ACK)
  559. return "Kbd: enable keyboard: no ACK";
  560. /*
  561. * Finally, set the typematic rate to maximum.
  562. */
  563. kbd_write_output_w(KBD_CMD_SET_RATE);
  564. if (kbd_wait_for_input() != KBD_REPLY_ACK)
  565. return "Kbd: Set rate: no ACK";
  566. kbd_write_output_w(0x00);
  567. if (kbd_wait_for_input() != KBD_REPLY_ACK)
  568. return "Kbd: Set rate: no ACK";
  569. return NULL;
  570. }
  571. void kbd_interrupt(void)
  572. {
  573. handle_kbd_event();
  574. }