Subversion Repositories HelenOS

Rev

Rev 2131 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2005 Ondrej Palkovsky
  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 mips32 
  30.  * @{
  31.  */
  32. /** @file
  33.  */
  34.  
  35. #include <interrupt.h>
  36. #include <arch/cp0.h>
  37. #include <ipc/irq.h>
  38. #include <arch/drivers/serial.h>
  39. #include <console/chardev.h>
  40. #include <console/console.h>
  41. #include <synch/rcu.h>
  42.  
  43. #define SERIAL_IRQ 2
  44.  
  45. static irq_t serial_irq;
  46. static chardev_t console;
  47. static serial_t sconf[SERIAL_MAX];
  48. static bool kb_enabled;
  49.  
  50. static void serial_write(chardev_t *d, const char ch)
  51. {
  52.     serial_t *sd = (serial_t *)d->data;
  53.  
  54.     if (ch == '\n')
  55.         serial_write(d, '\r');
  56.     /* Wait until transmit buffer empty */
  57.     while (! (SERIAL_READ_LSR(sd->port) & (1<<TRANSMIT_EMPTY_BIT)))
  58.         ;
  59.     SERIAL_WRITE(sd->port, ch);
  60. }
  61.  
  62. static void serial_enable(chardev_t *d)
  63. {
  64.     kb_enabled = true;
  65. }
  66.  
  67. static void serial_disable(chardev_t *d)
  68. {
  69.     kb_enabled = false;
  70. }
  71.  
  72. int serial_init(void)
  73. {
  74.     int i = 0;
  75.     if (SERIAL_READ_LSR(SERIAL_COM1) == 0x60) {
  76.         sconf[i].port = SERIAL_COM1;
  77.         sconf[i].irq = SERIAL_COM1_IRQ;
  78.         /* Enable interrupt on available data */
  79.         i++;
  80.     }
  81.     return i;
  82. }
  83.  
  84. /** Read character from serial port, wait until available */
  85. static char serial_do_read(chardev_t *dev)
  86. {
  87.     serial_t *sd = (serial_t *)dev->data;
  88.     char ch;
  89.  
  90.     while (!(SERIAL_READ_LSR(sd->port) & 1))
  91.         ;
  92.     ch = SERIAL_READ(sd->port);
  93.  
  94.     if (ch =='\r')
  95.         ch = '\n';
  96.     return ch;
  97. }
  98.  
  99. static void serial_handler(void)
  100. {
  101.     serial_t *sd = (serial_t *) console.data;
  102.     char ch;
  103.  
  104.     if (!(SERIAL_READ_LSR(sd->port) & 1))
  105.         return;
  106.     ch = SERIAL_READ(sd->port);
  107.  
  108.     if (ch =='\r')
  109.         ch = '\n';
  110.     chardev_push_character(&console, ch);
  111. }
  112.  
  113. /** Process keyboard interrupt. Does not work in simics? */
  114. static void serial_irq_handler(irq_t *irq, void *arg, ...)
  115. {
  116.     if ((rcu_dereference_pointer(irq->notif_cfg).notify) && (rcu_dereference_pointer(irq->notif_cfg).answerbox))
  117.         ipc_irq_send_notif(irq);
  118.     else
  119.         serial_handler();
  120. }
  121.  
  122. static irq_ownership_t serial_claim(void)
  123. {
  124.     return IRQ_ACCEPT;
  125. }
  126.  
  127. static chardev_operations_t serial_ops = {
  128.     .resume = serial_enable,
  129.     .suspend = serial_disable,
  130.     .write = serial_write,
  131.     .read = serial_do_read
  132. };
  133.  
  134. void serial_console(devno_t devno)
  135. {
  136.     serial_t *sd = &sconf[0];
  137.  
  138.  
  139.     chardev_initialize("serial_console", &console, &serial_ops);
  140.     console.data = sd;
  141.     kb_enabled = true;
  142.    
  143.     irq_initialize(&serial_irq);
  144.     serial_irq.devno = devno;
  145.     serial_irq.inr = SERIAL_IRQ;
  146.     serial_irq.claim = serial_claim;
  147.     serial_irq.handler = serial_irq_handler;
  148.     irq_register(&serial_irq);
  149.  
  150.     /* I don't know why, but the serial interrupts simply
  151.      * don't work on simics
  152.      */
  153.     virtual_timer_fnc = &serial_handler;
  154.    
  155.     stdin = &console;
  156.     stdout = &console;
  157. }
  158.  
  159. /** @}
  160.  */
  161.