Subversion Repositories HelenOS

Rev

Rev 1896 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (C) 2001-2004 Jakub Jermar
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup genarch
  30.  * @{
  31.  */
  32. /**
  33.  * @file
  34.  * @brief   Zilog 8530 serial port / keyboard driver.
  35.  */
  36.  
  37. #include <genarch/kbd/z8530.h>
  38. #include <genarch/kbd/key.h>
  39. #include <genarch/kbd/scanc.h>
  40. #include <genarch/kbd/scanc_sun.h>
  41. #include <arch/drivers/z8530.h>
  42. #include <arch/interrupt.h>
  43. #include <arch/drivers/kbd.h>
  44. #include <cpu.h>
  45. #include <arch/asm.h>
  46. #include <arch.h>
  47. #include <typedefs.h>
  48. #include <console/chardev.h>
  49. #include <console/console.h>
  50. #include <interrupt.h>
  51. #include <sysinfo/sysinfo.h>
  52. #include <print.h>
  53.  
  54. /*
  55.  * These codes read from z8530 data register are silently ignored.
  56.  */
  57. #define IGNORE_CODE 0x7f        /* all keys up */
  58.  
  59. bool z8530_belongs_to_kernel = true;
  60.  
  61. static void z8530_suspend(chardev_t *);
  62. static void z8530_resume(chardev_t *);
  63.  
  64. static chardev_operations_t ops = {
  65.     .suspend = z8530_suspend,
  66.     .resume = z8530_resume,
  67.     .read = z8530_key_read
  68. };
  69.  
  70. void z8530_wait(void);
  71.  
  72. /** Initialize keyboard and service interrupts using kernel routine */
  73. void z8530_grab(void)
  74. {
  75.     z8530_belongs_to_kernel = true;
  76. }
  77.  
  78. /** Resume the former interrupt vector */
  79. void z8530_release(void)
  80. {
  81.     z8530_belongs_to_kernel = false;
  82. }
  83.  
  84. /** Initialize z8530. */
  85. void z8530_init(void)
  86. {
  87.     chardev_initialize("z8530_kbd", &kbrd, &ops);
  88.     stdin = &kbrd;
  89.  
  90.     sysinfo_set_item_val("kbd", NULL, true);
  91.     sysinfo_set_item_val("kbd.irq", NULL, 0);
  92.     sysinfo_set_item_val("kbd.address.virtual", NULL, (uintptr_t) kbd_virt_address);
  93.  
  94.     (void) z8530_read_a(RR8);
  95.  
  96.     /*
  97.      * Clear any pending TX interrupts or we never manage
  98.      * to set FHC UART interrupt state to idle.
  99.      */
  100.     z8530_write_a(WR0, WR0_TX_IP_RST);
  101.  
  102.     z8530_write_a(WR1, WR1_IARCSC);     /* interrupt on all characters */
  103.  
  104.     /* 8 bits per character and enable receiver */
  105.     z8530_write_a(WR3, WR3_RX8BITSCH | WR3_RX_ENABLE);
  106.    
  107.     z8530_write_a(WR9, WR9_MIE);        /* Master Interrupt Enable. */
  108. }
  109.  
  110. /** Process z8530 interrupt.
  111.  *
  112.  * @param n Interrupt vector.
  113.  * @param istate Interrupted state.
  114.  */
  115. void z8530_interrupt(void)
  116. {
  117.     z8530_poll();
  118. }
  119.  
  120. /** Wait until the controller reads its data. */
  121. void z8530_wait(void) {
  122. }
  123.  
  124. /* Called from getc(). */
  125. void z8530_resume(chardev_t *d)
  126. {
  127. }
  128.  
  129. /* Called from getc(). */
  130. void z8530_suspend(chardev_t *d)
  131. {
  132. }
  133.  
  134. char z8530_key_read(chardev_t *d)
  135. {
  136.     char ch;   
  137.  
  138.     while(!(ch = active_read_buff_read())) {
  139.         uint8_t x;
  140.         while (!(z8530_read_a(RR0) & RR0_RCA))
  141.             ;
  142.         x = z8530_read_a(RR8);
  143.         if (x != IGNORE_CODE) {
  144.             if (x & KEY_RELEASE)
  145.                 key_released(x ^ KEY_RELEASE);
  146.             else
  147.                 active_read_key_pressed(x);
  148.         }
  149.     }
  150.     return ch;
  151. }
  152.  
  153. /** Poll for key press and release events.
  154.  *
  155.  * This function can be used to implement keyboard polling.
  156.  */
  157. void z8530_poll(void)
  158. {
  159.     uint8_t x;
  160.  
  161.     while (z8530_read_a(RR0) & RR0_RCA) {
  162.         x = z8530_read_a(RR8);
  163.         if (x != IGNORE_CODE) {
  164.             if (x & KEY_RELEASE)
  165.                 key_released(x ^ KEY_RELEASE);
  166.             else
  167.                 key_pressed(x);
  168.         }
  169.     }
  170. }
  171.  
  172. /** @}
  173.  */
  174.