Subversion Repositories HelenOS

Rev

Rev 2131 | Blame | Compare with Previous | 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 <ddi/irq.h>
  43. #include <ipc/irq.h>
  44. #include <arch/interrupt.h>
  45. #include <arch/drivers/kbd.h>
  46. #include <arch/drivers/fhc.h>
  47. #include <cpu.h>
  48. #include <arch/asm.h>
  49. #include <arch.h>
  50. #include <console/chardev.h>
  51. #include <console/console.h>
  52. #include <interrupt.h>
  53. #include <sysinfo/sysinfo.h>
  54. #include <print.h>
  55. #include <synch/rcu.h>
  56.  
  57. /*
  58.  * These codes read from z8530 data register are silently ignored.
  59.  */
  60. #define IGNORE_CODE 0x7f        /* all keys up */
  61.  
  62. static z8530_t z8530;       /**< z8530 device structure. */
  63. static irq_t z8530_irq;     /**< z8530's IRQ. */
  64.  
  65. static void z8530_suspend(chardev_t *);
  66. static void z8530_resume(chardev_t *);
  67.  
  68. static chardev_operations_t ops = {
  69.     .suspend = z8530_suspend,
  70.     .resume = z8530_resume,
  71.     .read = z8530_key_read
  72. };
  73.  
  74. /** Initialize keyboard and service interrupts using kernel routine. */
  75. void z8530_grab(void)
  76. {
  77.     ipl_t ipl = interrupts_disable();
  78.  
  79.     (void) z8530_read_a(&z8530, RR8);
  80.  
  81.     /*
  82.      * Clear any pending TX interrupts or we never manage
  83.      * to set FHC UART interrupt state to idle.
  84.      */
  85.     z8530_write_a(&z8530, WR0, WR0_TX_IP_RST);
  86.  
  87.     z8530_write_a(&z8530, WR1, WR1_IARCSC);     /* interrupt on all characters */
  88.  
  89.     /* 8 bits per character and enable receiver */
  90.     z8530_write_a(&z8530, WR3, WR3_RX8BITSCH | WR3_RX_ENABLE);
  91.    
  92.     z8530_write_a(&z8530, WR9, WR9_MIE);        /* Master Interrupt Enable. */
  93.    
  94.     //rcu_read_lock() is not needed, ints are disabled
  95.     //rcu: atomic update doesn't need reallocation
  96.     rcu_dereference_pointer(z8530_irq.notif_cfg).notify = false;
  97.     interrupts_restore(ipl);
  98. }
  99.  
  100. /** Resume the former IPC notification behavior. */
  101. void z8530_release(void)
  102. {
  103.     ipl_t ipl = interrupts_disable();
  104.     //rcu: atomic update doesn't need reallocation
  105.     spinlock_lock(&z8530_irq.lock);
  106.     if (rcu_dereference_pointer(z8530_irq.notif_cfg).answerbox)
  107.         rcu_dereference_pointer(z8530_irq.notif_cfg).notify = true;
  108.     spinlock_unlock(&z8530_irq.lock);
  109.     interrupts_restore(ipl);
  110. }
  111.  
  112. /** Initialize z8530. */
  113. void z8530_init(devno_t devno, inr_t inr, uintptr_t vaddr)
  114. {
  115.     chardev_initialize("z8530_kbd", &kbrd, &ops);
  116.     stdin = &kbrd;
  117.  
  118.     z8530.devno = devno;
  119.     z8530.reg = (uint8_t *) vaddr;
  120.  
  121.     irq_initialize(&z8530_irq);
  122.     z8530_irq.devno = devno;
  123.     z8530_irq.inr = inr;
  124.     z8530_irq.claim = z8530_claim;
  125.     z8530_irq.handler = z8530_irq_handler;
  126.     irq_register(&z8530_irq);
  127.  
  128.     sysinfo_set_item_val("kbd", NULL, true);
  129.     sysinfo_set_item_val("kbd.type", NULL, KBD_Z8530);
  130.     sysinfo_set_item_val("kbd.devno", NULL, devno);
  131.     sysinfo_set_item_val("kbd.inr", NULL, inr);
  132.     sysinfo_set_item_val("kbd.address.virtual", NULL, vaddr);
  133.  
  134.     z8530_grab();
  135. }
  136.  
  137. /** Process z8530 interrupt.
  138.  *
  139.  * @param n Interrupt vector.
  140.  * @param istate Interrupted state.
  141.  */
  142. void z8530_interrupt(void)
  143. {
  144.     z8530_poll();
  145. }
  146.  
  147. /* Called from getc(). */
  148. void z8530_resume(chardev_t *d)
  149. {
  150. }
  151.  
  152. /* Called from getc(). */
  153. void z8530_suspend(chardev_t *d)
  154. {
  155. }
  156.  
  157. char z8530_key_read(chardev_t *d)
  158. {
  159.     char ch;   
  160.  
  161.     while(!(ch = active_read_buff_read())) {
  162.         uint8_t x;
  163.         while (!(z8530_read_a(&z8530, RR0) & RR0_RCA))
  164.             ;
  165.         x = z8530_read_a(&z8530, RR8);
  166.         if (x != IGNORE_CODE) {
  167.             if (x & KEY_RELEASE)
  168.                 key_released(x ^ KEY_RELEASE);
  169.             else
  170.                 active_read_key_pressed(x);
  171.         }
  172.     }
  173.     return ch;
  174. }
  175.  
  176. /** Poll for key press and release events.
  177.  *
  178.  * This function can be used to implement keyboard polling.
  179.  */
  180. void z8530_poll(void)
  181. {
  182.     uint8_t x;
  183.  
  184.     while (z8530_read_a(&z8530, RR0) & RR0_RCA) {
  185.         x = z8530_read_a(&z8530, RR8);
  186.         if (x != IGNORE_CODE) {
  187.             if (x & KEY_RELEASE)
  188.                 key_released(x ^ KEY_RELEASE);
  189.             else
  190.                 key_pressed(x);
  191.         }
  192.     }
  193. }
  194.  
  195. irq_ownership_t z8530_claim(void)
  196. {
  197.     return (z8530_read_a(&z8530, RR0) & RR0_RCA);
  198. }
  199.  
  200. void z8530_irq_handler(irq_t *irq, void *arg, ...)
  201. {
  202.     /*
  203.      * So far, we know we got this interrupt through the FHC.
  204.      * Since we don't have enough documentation about the FHC
  205.      * and because the interrupt looks like level sensitive,
  206.      * we cannot handle it by scheduling one of the level
  207.      * interrupt traps. Process the interrupt directly.
  208.      */
  209.     if (rcu_dereference_pointer(irq->notif_cfg).notify && rcu_dereference_pointer(irq->notif_cfg).answerbox)
  210.         ipc_irq_send_notif(irq);
  211.     else
  212.         z8530_interrupt();
  213.     fhc_clear_interrupt(central_fhc, irq->inr);
  214. }
  215.  
  216. /** @}
  217.  */
  218.