Subversion Repositories HelenOS

Rev

Rev 3906 | Rev 3938 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2001-2006 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   NS 16550 serial port / keyboard driver.
  35.  */
  36.  
  37. #include <genarch/kbd/ns16550.h>
  38. #include <genarch/kbd/key.h>
  39. #include <genarch/kbd/scanc.h>
  40. #include <genarch/kbd/scanc_sun.h>
  41. #include <arch/drivers/kbd.h>
  42. #include <ddi/irq.h>
  43. #include <ipc/irq.h>
  44. #include <cpu.h>
  45. #include <arch/asm.h>
  46. #include <arch.h>
  47. #include <console/chardev.h>
  48. #include <console/console.h>
  49. #include <interrupt.h>
  50. #include <arch/interrupt.h>
  51. #include <sysinfo/sysinfo.h>
  52. #include <synch/spinlock.h>
  53. #include <mm/slab.h>
  54.  
  55. #define LSR_DATA_READY  0x01
  56.  
  57. static irq_t *ns16550_irq;
  58.  
  59. /*
  60.  * These codes read from ns16550 data register are silently ignored.
  61.  */
  62. #define IGNORE_CODE 0x7f        /* all keys up */
  63.  
  64. static void ns16550_suspend(chardev_t *);
  65. static void ns16550_resume(chardev_t *);
  66.  
  67. static chardev_operations_t ops = {
  68.     .suspend = ns16550_suspend,
  69.     .resume = ns16550_resume,
  70. };
  71.  
  72. /** Initialize keyboard and service interrupts using kernel routine */
  73. void ns16550_grab(void)
  74. {
  75.     ipl_t ipl = interrupts_disable();
  76.     spinlock_lock(&ns16550_irq->lock);
  77.     ns16550_irq->notif_cfg.notify = false;
  78.     spinlock_unlock(&ns16550_irq->lock);
  79.     interrupts_restore(ipl);
  80. }
  81.  
  82. /** Resume the former interrupt vector */
  83. void ns16550_release(void)
  84. {
  85.     ipl_t ipl = interrupts_disable();
  86.     spinlock_lock(&ns16550_irq->lock);
  87.     if (ns16550_irq->notif_cfg.answerbox)
  88.         ns16550_irq->notif_cfg.notify = true;
  89.     spinlock_unlock(&ns16550_irq->lock);
  90.     interrupts_restore(ipl);
  91. }
  92.  
  93. /** Initialize ns16550.
  94.  *
  95.  * @param dev       Addrress of the beginning of the device in I/O space.
  96.  * @param devno     Device number.
  97.  * @param inr       Interrupt number.
  98.  * @param cir       Clear interrupt function.
  99.  * @param cir_arg   First argument to cir.
  100.  *
  101.  * @return      True on success, false on failure.
  102.  */
  103. bool
  104. ns16550_init(ns16550_t *dev, devno_t devno, inr_t inr, cir_t cir, void *cir_arg)
  105. {
  106.     ns16550_instance_t *instance;
  107.     irq_t *irq;
  108.  
  109.     chardev_initialize("ns16550_kbd", &kbrd, &ops);
  110.     stdin = &kbrd;
  111.    
  112.     instance = malloc(sizeof(ns16550_instance_t), FRAME_ATOMIC);
  113.     if (!instance)
  114.         return false;
  115.  
  116.     irq = malloc(sizeof(irq_t), FRAME_ATOMIC);
  117.     if (!irq) {
  118.         free(instance);
  119.         return false;
  120.     }
  121.  
  122.     instance->devno = devno;
  123.     instance->ns16550 = dev;
  124.     instance->irq = irq;
  125.    
  126.     irq_initialize(irq);
  127.     irq->devno = devno;
  128.     irq->inr = inr;
  129.     irq->claim = ns16550_claim;
  130.     irq->handler = ns16550_irq_handler;
  131.     irq->instance = instance;
  132.     irq->cir = cir;
  133.     irq->cir_arg = cir_arg;
  134.     irq_register(irq);
  135.  
  136.     ns16550_irq = irq;  /* TODO: remove me soon */
  137.    
  138.     while ((pio_read_8(&dev->lsr) & LSR_DATA_READY))
  139.         (void) pio_read_8(&dev->rbr);
  140.    
  141.     sysinfo_set_item_val("kbd", NULL, true);
  142.     sysinfo_set_item_val("kbd.type", NULL, KBD_NS16550);
  143.     sysinfo_set_item_val("kbd.devno", NULL, devno);
  144.     sysinfo_set_item_val("kbd.inr", NULL, inr);
  145.     sysinfo_set_item_val("kbd.address.virtual", NULL, (uintptr_t) dev);
  146.     sysinfo_set_item_val("kbd.port", NULL, (uintptr_t) dev);
  147.    
  148.     /* Enable interrupts */
  149.     pio_write_8(&dev->ier, IER_ERBFI);
  150.     pio_write_8(&dev->mcr, MCR_OUT2);
  151.    
  152.     ns16550_grab();
  153.    
  154.     return true;
  155. }
  156.  
  157. /* Called from getc(). */
  158. void ns16550_resume(chardev_t *d)
  159. {
  160. }
  161.  
  162. /* Called from getc(). */
  163. void ns16550_suspend(chardev_t *d)
  164. {
  165. }
  166.  
  167. irq_ownership_t ns16550_claim(void *instance)
  168. {
  169.     ns16550_instance_t *ns16550_instance = instance;
  170.     ns16550_t *dev = ns16550_instance->ns16550;
  171.  
  172.     if (pio_read_8(&dev->lsr) & LSR_DATA_READY)
  173.         return IRQ_ACCEPT;
  174.     else
  175.         return IRQ_DECLINE;
  176. }
  177.  
  178. void ns16550_irq_handler(irq_t *irq)
  179. {
  180.     if (irq->notif_cfg.notify && irq->notif_cfg.answerbox) {
  181.         /*
  182.          * This will hopefully go to the IRQ dispatch code soon.
  183.          */
  184.         ipc_irq_send_notif(irq);
  185.         return;
  186.     }
  187.  
  188.     ns16550_instance_t *ns16550_instance = irq->instance;
  189.     ns16550_t *dev = ns16550_instance->ns16550;
  190.  
  191.     if (pio_read_8(&dev->lsr) & LSR_DATA_READY) {
  192.         uint8_t x;
  193.        
  194.         x = pio_read_8(&dev->rbr);
  195.        
  196.         if (x != IGNORE_CODE) {
  197.             if (x & KEY_RELEASE)
  198.                 key_released(x ^ KEY_RELEASE);
  199.             else
  200.                 key_pressed(x);
  201.         }
  202.     }
  203.  
  204. }
  205.  
  206. /** @}
  207.  */
  208.