hil.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:12k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  2.  *  linux/arch/m68k/hp300/hil.c
  3.  *
  4.  *  Copyright (C) 1998 Philip Blundell <philb@gnu.org>
  5.  *
  6.  *  HP300 Human Interface Loop driver.  This handles the keyboard and mouse.
  7.  */
  8. #include <linux/stddef.h>
  9. #include <linux/kernel.h>
  10. #include <linux/sched.h>
  11. #include <linux/init.h>
  12. #include <linux/keyboard.h>
  13. #include <linux/kbd_ll.h>
  14. #include <asm/io.h>
  15. #include <asm/hwtest.h>
  16. #include <asm/ptrace.h>
  17. #include <asm/irq.h>
  18. #include <asm/system.h>
  19. #define HILBASE 0xf0428000
  20. #define HIL_DATA 0x1
  21. #define HIL_CMD 0x3
  22. #define HIL_BUSY 0x02
  23. #define HIL_DATA_RDY 0x01
  24. #define hil_busy() (readb(HILBASE + HIL_CMD) & HIL_BUSY)
  25. #define hil_data_available() (readb(HILBASE + HIL_CMD) & HIL_DATA_RDY)
  26. #define hil_status() (readb(HILBASE + HIL_CMD))
  27. #define hil_command(x) do { writeb((x), HILBASE + HIL_CMD); } while (0)
  28. #define hil_read_data() (readb(HILBASE + HIL_DATA))
  29. #define hil_write_data(x) do { writeb((x), HILBASE + HIL_DATA); } while (0)
  30. #define HIL_SETARD 0xA0 /* set auto-repeat delay */
  31. #define HIL_SETARR 0xA2 /* set auto-repeat rate */
  32. #define HIL_SETTONE 0xA3 /* set tone generator */
  33. #define HIL_CNMT 0xB2 /* clear nmi */
  34. #define HIL_INTON 0x5C /* Turn on interrupts. */
  35. #define HIL_INTOFF 0x5D /* Turn off interrupts. */
  36. #define HIL_TRIGGER 0xC5 /* trigger command */
  37. #define HIL_STARTCMD 0xE0 /* start loop command */
  38. #define HIL_TIMEOUT 0xFE /* timeout */
  39. #define HIL_READTIME 0x13 /* Read real time register */
  40. #define HIL_READBUSY 0x02 /* internal "busy" register */
  41. #define HIL_READKBDLANG 0x12 /* read keyboard language code */
  42. #define HIL_READKBDSADR   0xF9
  43. #define HIL_WRITEKBDSADR  0xE9
  44. #define HIL_READLPSTAT   0xFA
  45. #define HIL_WRITELPSTAT  0xEA
  46. #define HIL_READLPCTRL   0xFB
  47. #define HIL_WRITELPCTRL  0xEB
  48. #define HIL_IRQ 1
  49. #define plain_map hp_plain_map
  50. #define shift_map hp_shift_map
  51. #define altgr_map hp_altgr_map
  52. #define ctrl_map hp_ctrl_map
  53. #define shift_ctrl_map hp_shift_ctrl_map
  54. #define alt_map hp_alt_map
  55. #define ctrl_alt_map hp_ctrl_alt_map
  56. u_short plain_map[NR_KEYS] = {
  57. 0xf200, 0xf200, 0xf703, 0xf703, 0xf700, 0xf700, 0xf702, 0xf200,
  58. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  59. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  60. 0xfb62, 0xfb76, 0xfb63, 0xfb78, 0xfb7a, 0xf200, 0xf200, 0xf01b,
  61. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  62. 0xfb68, 0xfb67, 0xfb66, 0xfb64, 0xfb73, 0xfb61, 0xf200, 0xf207,
  63. 0xfb75, 0xfb79, 0xfb74, 0xfb72, 0xfb65, 0xfb77, 0xfb71, 0xf009,
  64. 0xf037, 0xf036, 0xf035, 0xf034, 0xf033, 0xf032, 0xf031, 0xf060,
  65. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  66. 0xf200, 0xf103, 0xf102, 0xf101, 0xf100, 0xf200, 0xf200, 0xf200,
  67. 0xf200, 0xf104, 0xf105, 0xf106, 0xf107, 0xf200, 0xf200, 0xf200,
  68. 0xf038, 0xf039, 0xf030, 0xf02d, 0xf03d, 0xf008, 0xf200, 0xf200,
  69. 0xfb69, 0xfb6f, 0xfb70, 0xf05b, 0xf05d, 0xf05c, 0xf200, 0xf200,
  70. 0xfb6a, 0xfb6b, 0xfb6c, 0xf03b, 0xf027, 0xf201, 0xf200, 0xf200,
  71. 0xfb6d, 0xf02c, 0xf02e, 0xf02f, 0xf200, 0xf200, 0xf200, 0xf200,
  72. 0xfb6e, 0xf020, 0xf200, 0xf200, 0xf601, 0xf600, 0xf603, 0xf602,
  73. };
  74. u_short shift_map[NR_KEYS] = {
  75. 0xf200, 0xf200, 0xf703, 0xf703, 0xf700, 0xf700, 0xf702, 0xf200,
  76. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  77. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  78. 0xfb42, 0xfb56, 0xfb43, 0xfb58, 0xfb5a, 0xf200, 0xf200, 0xf07f,
  79. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  80. 0xfb48, 0xfb47, 0xfb46, 0xfb44, 0xfb53, 0xfb41, 0xf200, 0xf207,
  81. 0xfb55, 0xfb59, 0xfb54, 0xfb52, 0xfb45, 0xfb57, 0xfb51, 0xf009,
  82. 0xf026, 0xf05e, 0xf025, 0xf024, 0xf023, 0xf040, 0xf021, 0xf07e,
  83. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  84. 0xf200, 0xf103, 0xf102, 0xf101, 0xf100, 0xf200, 0xf200, 0xf200,
  85. 0xf200, 0xf104, 0xf105, 0xf106, 0xf107, 0xf200, 0xf200, 0xf200,
  86. 0xf02a, 0xf028, 0xf029, 0xf05f, 0xf02b, 0xf200, 0xf200, 0xf200,
  87. 0xfb49, 0xfb4f, 0xfb50, 0xf07b, 0xf07d, 0xf07c, 0xf200, 0xf200,
  88. 0xfb4a, 0xfb4b, 0xfb4c, 0xf03a, 0xf022, 0xf201, 0xf200, 0xf200,
  89. 0xfb4d, 0xf03c, 0xf03e, 0xf03f, 0xf200, 0xf200, 0xf200, 0xf200,
  90. 0xfb4e, 0xf020, 0xf200, 0xf200, 0xf601, 0xf600, 0xf603, 0xf602,
  91. };
  92. u_short altgr_map[NR_KEYS] = {
  93. 0xf200, 0xf200, 0xf703, 0xf703, 0xf700, 0xf700, 0xf702, 0xf200,
  94. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  95. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  96. 0xfb62, 0xfb76, 0xfb63, 0xfb78, 0xfb7a, 0xf200, 0xf200, 0xf200,
  97. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  98. 0xfb68, 0xfb67, 0xfb66, 0xfb64, 0xfb73, 0xfb61, 0xf200, 0xf207,
  99. 0xfb75, 0xfb79, 0xfb74, 0xfb72, 0xfb65, 0xfb77, 0xfb71, 0xf200,
  100. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf040, 0xf021, 0xf200,
  101. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  102. 0xf200, 0xf103, 0xf102, 0xf101, 0xf100, 0xf200, 0xf200, 0xf200,
  103. 0xf200, 0xf104, 0xf105, 0xf106, 0xf107, 0xf200, 0xf200, 0xf200,
  104. 0xf02a, 0xf05b, 0xf05d, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  105. 0xfb69, 0xfb6f, 0xfb70, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  106. 0xfb6a, 0xfb6b, 0xfb6c, 0xf200, 0xf200, 0xf201, 0xf200, 0xf200,
  107. 0xfb6d, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  108. 0xfb6e, 0xf200, 0xf200, 0xf200, 0xf601, 0xf600, 0xf603, 0xf602,
  109. };
  110. u_short ctrl_map[NR_KEYS] = {
  111. 0xf200, 0xf200, 0xf703, 0xf703, 0xf700, 0xf700, 0xf702, 0xf200,
  112. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  113. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  114. 0xf002, 0xf016, 0xf003, 0xf018, 0xf01a, 0xf200, 0xf200, 0xf200,
  115. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  116. 0xf008, 0xf007, 0xf006, 0xf004, 0xf013, 0xf001, 0xf200, 0xf207,
  117. 0xf015, 0xf019, 0xf014, 0xf012, 0xf005, 0xf017, 0xf011, 0xf200,
  118. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf000,
  119. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  120. 0xf200, 0xf503, 0xf502, 0xf501, 0xf500, 0xf200, 0xf200, 0xf200,
  121. 0xf200, 0xf504, 0xf505, 0xf506, 0xf507, 0xf200, 0xf200, 0xf200,
  122. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  123. 0xf009, 0xf00f, 0xf010, 0xf200, 0xf200, 0xf01c, 0xf200, 0xf200,
  124. 0xf00a, 0xf00b, 0xf00c, 0xf200, 0xf007, 0xf201, 0xf200, 0xf200,
  125. 0xf00d, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  126. 0xf00e, 0xf200, 0xf200, 0xf200, 0xf601, 0xf600, 0xf603, 0xf602,
  127. };
  128. u_short shift_ctrl_map[NR_KEYS] = {
  129. 0xf200, 0xf200, 0xf703, 0xf703, 0xf700, 0xf700, 0xf702, 0xf200,
  130. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  131. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  132. 0xf002, 0xf016, 0xf003, 0xf018, 0xf01a, 0xf200, 0xf200, 0xf200,
  133. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  134. 0xf008, 0xf007, 0xf006, 0xf004, 0xf013, 0xf001, 0xf200, 0xf207,
  135. 0xf015, 0xf019, 0xf014, 0xf012, 0xf005, 0xf017, 0xf011, 0xf200,
  136. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  137. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  138. 0xf200, 0xf103, 0xf102, 0xf101, 0xf100, 0xf200, 0xf200, 0xf200,
  139. 0xf200, 0xf104, 0xf105, 0xf106, 0xf107, 0xf200, 0xf200, 0xf200,
  140. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  141. 0xf009, 0xf00f, 0xf010, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  142. 0xf00a, 0xf00b, 0xf00c, 0xf200, 0xf200, 0xf201, 0xf200, 0xf200,
  143. 0xf00d, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  144. 0xf00e, 0xf200, 0xf200, 0xf200, 0xf601, 0xf600, 0xf603, 0xf602,
  145. };
  146. u_short alt_map[NR_KEYS] = {
  147. 0xf200, 0xf200, 0xf703, 0xf703, 0xf700, 0xf700, 0xf702, 0xf200,
  148. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  149. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  150. 0xf862, 0xf876, 0xf863, 0xf878, 0xf87a, 0xf200, 0xf200, 0xf200,
  151. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  152. 0xf868, 0xf867, 0xf866, 0xf864, 0xf873, 0xf861, 0xf200, 0xf207,
  153. 0xf875, 0xf879, 0xf874, 0xf872, 0xf865, 0xf877, 0xf871, 0xf809,
  154. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf860,
  155. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  156. 0xf200, 0xf103, 0xf102, 0xf101, 0xf100, 0xf200, 0xf200, 0xf200,
  157. 0xf200, 0xf104, 0xf105, 0xf106, 0xf107, 0xf200, 0xf200, 0xf200,
  158. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  159. 0xf869, 0xf86f, 0xf870, 0xf200, 0xf200, 0xf85c, 0xf200, 0xf200,
  160. 0xf86a, 0xf86b, 0xf86c, 0xf83b, 0xf827, 0xf201, 0xf200, 0xf200,
  161. 0xf86d, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  162. 0xf86e, 0xf200, 0xf200, 0xf200, 0xf601, 0xf600, 0xf603, 0xf602,
  163. };
  164. u_short ctrl_alt_map[NR_KEYS] = {
  165. 0xf200, 0xf200, 0xf703, 0xf703, 0xf700, 0xf700, 0xf702, 0xf200,
  166. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  167. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  168. 0xf802, 0xf816, 0xf803, 0xf818, 0xf81a, 0xf200, 0xf200, 0xf200,
  169. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  170. 0xf808, 0xf807, 0xf806, 0xf804, 0xf813, 0xf801, 0xf200, 0xf207,
  171. 0xf815, 0xf819, 0xf814, 0xf812, 0xf805, 0xf817, 0xf811, 0xf200,
  172. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  173. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  174. 0xf200, 0xf103, 0xf102, 0xf101, 0xf100, 0xf200, 0xf200, 0xf200,
  175. 0xf200, 0xf104, 0xf105, 0xf106, 0xf107, 0xf200, 0xf200, 0xf200,
  176. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  177. 0xf809, 0xf80f, 0xf810, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  178. 0xf80a, 0xf80b, 0xf80c, 0xf200, 0xf200, 0xf201, 0xf200, 0xf200,
  179. 0xf80d, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  180. 0xf80e, 0xf200, 0xf200, 0xf200, 0xf601, 0xf600, 0xf603, 0xf602,
  181. };
  182. #undef plain_map
  183. #undef ctrl_alt_map
  184. #undef shift_map
  185. #undef altgr_map
  186. #undef ctrl_map
  187. #undef shift_ctrl_map
  188. #undef alt_map
  189. struct {
  190.   unsigned char s, c;
  191.   int valid;
  192. } hil_last;
  193. #define hil_getlast(s,c)  do { s = hil_last.s; c = hil_last.c; hil_last.valid = 0; } while (0)
  194. struct {
  195.   unsigned char data[16];
  196.   unsigned int ptr;
  197. } poll;
  198. unsigned char curdev = 0;
  199. static void poll_finished(void)
  200. {
  201.   switch (poll.data[0])
  202.   {
  203.   case 0x40:
  204.     {
  205.       int down = (poll.data[1] & 1) == 0;
  206.       unsigned char scode = poll.data[1] >> 1;
  207. #if 0
  208.       if (down)
  209. printk("[%02x]", scode);
  210. #endif
  211.       handle_scancode(scode, down);
  212.     }
  213.     break;
  214.   }
  215.   curdev = 0;
  216. }
  217. static inline void handle_status(unsigned char s, unsigned char c)
  218. {
  219.   if (c & 0x8) {
  220.     /* End of block */
  221.     if (c & 0x10)
  222.       poll_finished();
  223.   } else {
  224.     if (c & 0x10) {
  225.       if (curdev)
  226. poll_finished(); /* just in case */
  227.       curdev = c & 7;
  228.       poll.ptr = 0;
  229.     }
  230.   }
  231. }
  232. static inline void handle_data(unsigned char s, unsigned char c)
  233. {
  234.   if (curdev)
  235.     poll.data[poll.ptr++] = c;
  236. }
  237. /* 
  238.  * Handle HIL interrupts.
  239.  */
  240. static void hil_interrupt(int irq, void *handle, struct pt_regs *regs)
  241. {
  242.   unsigned char s, c;
  243.   s = hil_status(); c = hil_read_data();
  244.   switch (s >> 4)
  245.   {
  246.   case 0x5:
  247.     handle_status(s, c);
  248.     break;
  249.   case 0x6:
  250.     handle_data(s, c);
  251.     break;
  252.   case 0x4:
  253.     hil_last.s = s;
  254.     hil_last.c = c;
  255.     mb();
  256.     hil_last.valid = 1;
  257.     break;
  258.   }
  259. }
  260. /*
  261.  * Send a command to the HIL
  262.  */
  263. static void hil_do(unsigned char cmd, unsigned char *data, unsigned int len)
  264. {
  265.   unsigned long flags;
  266.   save_flags(flags); cli();
  267.   while (hil_busy());
  268.   hil_command(cmd);
  269.   while (len--) {
  270.     while (hil_busy());
  271.     hil_write_data(*(data++));
  272.   }
  273.   restore_flags(flags);
  274. }
  275. /*
  276.  * Initialise HIL. 
  277.  */
  278. int __init hp300_keyb_init(void)
  279. {
  280.   unsigned char s, c, kbid;
  281.   unsigned int n = 0;
  282.   memcpy(key_maps[0], hp_plain_map, sizeof(plain_map));
  283.   memcpy(key_maps[1], hp_shift_map, sizeof(plain_map));
  284.   memcpy(key_maps[4], hp_ctrl_map, sizeof(plain_map));
  285.   if (!hwreg_present((void *)(HILBASE + HIL_DATA)))
  286.     return 1; /* maybe this can happen */
  287.   request_irq(HIL_IRQ, hil_interrupt, 0, "HIL", NULL);
  288.   /* Turn on interrupts */
  289.   hil_do(HIL_INTON, NULL, 0);
  290.   /* Look for keyboards */
  291.   hil_do(HIL_READKBDSADR, NULL, 0);
  292.   while (!hil_last.valid) {
  293.     if (n++ > 100000) {
  294.       printk("HIL: timed out, assuming no keyboard present.n");
  295.       return 1;
  296.     }
  297.     mb();
  298.   }
  299.   hil_getlast(s, c);
  300.   if (c == 0) {
  301.     printk("HIL: no keyboard present.n");
  302.     return 1;
  303.   }
  304.   for (kbid = 0; (kbid < 8) && ((c & (1<<kbid)) == 0); kbid++);
  305.   printk("HIL: keyboard found at id %dn", kbid);
  306.   /* set it to raw mode */
  307.   c = 0;
  308.   hil_do(HIL_WRITEKBDSADR, &c, 1);
  309.   return 0;
  310. }