Subversion Repositories HelenOS

Rev

Rev 3938 | Rev 3959 | 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.  
  108.     chardev_initialize("ns16550_kbd", &kbrd, &ops);
  109.     stdin = &kbrd;
  110.    
  111.     instance = malloc(sizeof(ns16550_instance_t), FRAME_ATOMIC);
  112.     if (!instance)
  113.         return false;
  114.  
  115.     instance->devno = devno;
  116.     instance->ns16550 = dev;
  117.    
  118.     irq_initialize(&instance->irq);
  119.     instance->irq.devno = devno;
  120.     instance->irq.inr = inr;
  121.     instance->irq.claim = ns16550_claim;
  122.     instance->irq.handler = ns16550_irq_handler;
  123.     instance->irq.instance = instance;
  124.     instance->irq.cir = cir;
  125.     instance->irq.cir_arg = cir_arg;
  126.     irq_register(&instance->irq);
  127.  
  128.     ns16550_irq = &instance->irq;   /* TODO: remove me soon */
  129.    
  130.     while ((pio_read_8(&dev->lsr) & LSR_DATA_READY))
  131.         (void) pio_read_8(&dev->rbr);
  132.    
  133.     sysinfo_set_item_val("kbd", NULL, true);
  134.     sysinfo_set_item_val("kbd.type", NULL, KBD_NS16550);
  135.     sysinfo_set_item_val("kbd.devno", NULL, devno);
  136.     sysinfo_set_item_val("kbd.inr", NULL, inr);
  137.     sysinfo_set_item_val("kbd.address.virtual", NULL, (uintptr_t) dev);
  138.     sysinfo_set_item_val("kbd.port", NULL, (uintptr_t) dev);
  139.    
  140.     /* Enable interrupts */
  141.     pio_write_8(&dev->ier, IER_ERBFI);
  142.     pio_write_8(&dev->mcr, MCR_OUT2);
  143.    
  144.     ns16550_grab();
  145.    
  146.     return true;
  147. }
  148.  
  149. /* Called from getc(). */
  150. void ns16550_resume(chardev_t *d)
  151. {
  152. }
  153.  
  154. /* Called from getc(). */
  155. void ns16550_suspend(chardev_t *d)
  156. {
  157. }
  158.  
  159. irq_ownership_t ns16550_claim(irq_t *irq)
  160. {
  161.     ns16550_instance_t *ns16550_instance = irq->instance;
  162.     ns16550_t *dev = ns16550_instance->ns16550;
  163.  
  164.     if (pio_read_8(&dev->lsr) & LSR_DATA_READY)
  165.         return IRQ_ACCEPT;
  166.     else
  167.         return IRQ_DECLINE;
  168. }
  169.  
  170. void ns16550_irq_handler(irq_t *irq)
  171. {
  172.     if (irq->notif_cfg.notify && irq->notif_cfg.answerbox) {
  173.         /*
  174.          * This will hopefully go to the IRQ dispatch code soon.
  175.          */
  176.         ipc_irq_send_notif(irq);
  177.         return;
  178.     }
  179.  
  180.     ns16550_instance_t *ns16550_instance = irq->instance;
  181.     ns16550_t *dev = ns16550_instance->ns16550;
  182.  
  183.     if (pio_read_8(&dev->lsr) & LSR_DATA_READY) {
  184.         uint8_t x;
  185.        
  186.         x = pio_read_8(&dev->rbr);
  187.        
  188.         if (x != IGNORE_CODE) {
  189.             if (x & KEY_RELEASE)
  190.                 key_released(x ^ KEY_RELEASE);
  191.             else
  192.                 key_pressed(x);
  193.         }
  194.     }
  195.  
  196. }
  197.  
  198. /** @}
  199.  */
  200.