| [973be64e] | 1 | /*
|
|---|
| [df4ed85] | 2 | * Copyright (c) 2005 Ondrej Palkovsky
|
|---|
| [973be64e] | 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 |
|
|---|
| [516ff92] | 29 | /** @addtogroup mips32
|
|---|
| [b45c443] | 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| [973be64e] | 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 |
|
|---|
| [7688b5d] | 41 | #define SERIAL_IRQ 2
|
|---|
| 42 |
|
|---|
| 43 | static irq_t serial_irq;
|
|---|
| [973be64e] | 44 | static chardev_t console;
|
|---|
| 45 | static serial_t sconf[SERIAL_MAX];
|
|---|
| [6095342] | 46 | static bool kb_enabled;
|
|---|
| [973be64e] | 47 |
|
|---|
| [516ff92] | 48 | static void serial_write(chardev_t *d, const char ch, bool silent)
|
|---|
| [973be64e] | 49 | {
|
|---|
| [516ff92] | 50 | if (!silent) {
|
|---|
| 51 | serial_t *sd = (serial_t *)d->data;
|
|---|
| 52 |
|
|---|
| 53 | if (ch == '\n')
|
|---|
| [b7c4044] | 54 | serial_write(d, '\r', false);
|
|---|
| [516ff92] | 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 | }
|
|---|
| [973be64e] | 60 | }
|
|---|
| 61 |
|
|---|
| 62 | static void serial_enable(chardev_t *d)
|
|---|
| 63 | {
|
|---|
| [6095342] | 64 | kb_enabled = true;
|
|---|
| [973be64e] | 65 | }
|
|---|
| 66 |
|
|---|
| 67 | static void serial_disable(chardev_t *d)
|
|---|
| 68 | {
|
|---|
| [6095342] | 69 | kb_enabled = false;
|
|---|
| [973be64e] | 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 |
|
|---|
| [93b84b3] | 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 |
|
|---|
| [7688b5d] | 99 | static void serial_handler(void)
|
|---|
| [973be64e] | 100 | {
|
|---|
| [7688b5d] | 101 | serial_t *sd = (serial_t *) console.data;
|
|---|
| [973be64e] | 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 |
|
|---|
| [7688b5d] | 113 | /** Process keyboard interrupt. Does not work in simics? */
|
|---|
| [6cd9aa6] | 114 | static void serial_irq_handler(irq_t *irq)
|
|---|
| [7688b5d] | 115 | {
|
|---|
| [0c33687a] | 116 | serial_handler();
|
|---|
| [7688b5d] | 117 | }
|
|---|
| [973be64e] | 118 |
|
|---|
| [c9b550b] | 119 | static irq_ownership_t serial_claim(irq_t *irq)
|
|---|
| [7688b5d] | 120 | {
|
|---|
| 121 | return IRQ_ACCEPT;
|
|---|
| 122 | }
|
|---|
| [93b84b3] | 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 |
|
|---|
| [7688b5d] | 131 | void serial_console(devno_t devno)
|
|---|
| [973be64e] | 132 | {
|
|---|
| 133 | serial_t *sd = &sconf[0];
|
|---|
| [516ff92] | 134 |
|
|---|
| [973be64e] | 135 | chardev_initialize("serial_console", &console, &serial_ops);
|
|---|
| 136 | console.data = sd;
|
|---|
| [6095342] | 137 | kb_enabled = true;
|
|---|
| [7688b5d] | 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);
|
|---|
| [516ff92] | 145 |
|
|---|
| [973be64e] | 146 | /* I don't know why, but the serial interrupts simply
|
|---|
| [516ff92] | 147 | don't work on simics */
|
|---|
| [8c84448] | 148 | virtual_timer_fnc = &serial_handler;
|
|---|
| [973be64e] | 149 |
|
|---|
| [af9a7c5] | 150 | stdin = &console;
|
|---|
| 151 | stdout = &console;
|
|---|
| [973be64e] | 152 | }
|
|---|
| [b45c443] | 153 |
|
|---|
| [06e1e95] | 154 | /** @}
|
|---|
| [b45c443] | 155 | */
|
|---|