Subversion Repositories HelenOS

Rev

Rev 4343 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
575 palkovsky 1
/*
2071 jermar 2
 * Copyright (c) 2005 Ondrej Palkovsky
575 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
 
4341 svoboda 29
/** @addtogroup mips32
1702 cejka 30
 * @{
31
 */
32
/** @file
33
 */
34
 
575 palkovsky 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
 
1936 decky 41
#define SERIAL_IRQ 2
42
 
43
static irq_t serial_irq;
575 palkovsky 44
static chardev_t console;
45
static serial_t sconf[SERIAL_MAX];
577 palkovsky 46
static bool kb_enabled;
575 palkovsky 47
 
4341 svoboda 48
static void serial_write(chardev_t *d, const char ch, bool silent)
575 palkovsky 49
{
4341 svoboda 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
    }
575 palkovsky 60
}
61
 
62
static void serial_enable(chardev_t *d)
63
{
577 palkovsky 64
    kb_enabled = true;
575 palkovsky 65
}
66
 
67
static void serial_disable(chardev_t *d)
68
{
577 palkovsky 69
    kb_enabled = false;
575 palkovsky 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
 
607 palkovsky 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;
575 palkovsky 89
 
607 palkovsky 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
 
1936 decky 99
static void serial_handler(void)
575 palkovsky 100
{
1936 decky 101
    serial_t *sd = (serial_t *) console.data;
575 palkovsky 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
 
1936 decky 113
/** Process keyboard interrupt. Does not work in simics? */
4343 svoboda 114
static void serial_irq_handler(irq_t *irq)
1936 decky 115
{
4344 svoboda 116
    serial_handler();
1936 decky 117
}
575 palkovsky 118
 
4344 svoboda 119
static irq_ownership_t serial_claim(irq_t *irq)
1936 decky 120
{
121
    return IRQ_ACCEPT;
122
}
607 palkovsky 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
 
1936 decky 131
void serial_console(devno_t devno)
575 palkovsky 132
{
133
    serial_t *sd = &sconf[0];
4341 svoboda 134
 
575 palkovsky 135
    chardev_initialize("serial_console", &console, &serial_ops);
136
    console.data = sd;
577 palkovsky 137
    kb_enabled = true;
1936 decky 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);
4341 svoboda 145
 
575 palkovsky 146
    /* I don't know why, but the serial interrupts simply
4341 svoboda 147
       don't work on simics */
1955 decky 148
    virtual_timer_fnc = &serial_handler;
575 palkovsky 149
 
606 palkovsky 150
    stdin = &console;
151
    stdout = &console;
575 palkovsky 152
}
1702 cejka 153
 
1888 jermar 154
/** @}
1702 cejka 155
 */