Subversion Repositories HelenOS

Rev

Rev 4343 | Go to most recent revision | Blame | 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 <arch/drivers/serial.h>
  38. #include <console/chardev.h>
  39. #include <console/console.h>
  40.  
  41. #define SERIAL_IRQ 2
  42.  
  43. static irq_t serial_irq;
  44. static chardev_t console;
  45. static serial_t sconf[SERIAL_MAX];
  46. static bool kb_enabled;
  47.  
  48. static void serial_write(chardev_t *d, const char ch, bool silent)
  49. {
  50.     if (!silent) {
  51.         serial_t *sd = (serial_t *)d->data;
  52.        
  53.         if (ch == '\n')
  54.             serial_write(d, '\r', false);
  55.        
  56.         /* Wait until transmit buffer empty */
  57.         while (!(SERIAL_READ_LSR(sd->port) & (1 << TRANSMIT_EMPTY_BIT)));
  58.         SERIAL_WRITE(sd->port, ch);
  59.     }
  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)
  115. {
  116.     serial_handler();
  117. }
  118.  
  119. static irq_ownership_t serial_claim(irq_t *irq)
  120. {
  121.     return IRQ_ACCEPT;
  122. }
  123.  
  124. static chardev_operations_t serial_ops = {
  125.     .resume = serial_enable,
  126.     .suspend = serial_disable,
  127.     .write = serial_write,
  128.     .read = serial_do_read
  129. };
  130.  
  131. void serial_console(devno_t devno)
  132. {
  133.     serial_t *sd = &sconf[0];
  134.    
  135.     chardev_initialize("serial_console", &console, &serial_ops);
  136.     console.data = sd;
  137.     kb_enabled = true;
  138.    
  139.     irq_initialize(&serial_irq);
  140.     serial_irq.devno = devno;
  141.     serial_irq.inr = SERIAL_IRQ;
  142.     serial_irq.claim = serial_claim;
  143.     serial_irq.handler = serial_irq_handler;
  144.     irq_register(&serial_irq);
  145.    
  146.     /* I don't know why, but the serial interrupts simply
  147.        don't work on simics */
  148.     virtual_timer_fnc = &serial_handler;
  149.    
  150.     stdin = &console;
  151.     stdout = &console;
  152. }
  153.  
  154. /** @}
  155.  */
  156.