Subversion Repositories HelenOS

Rev

Rev 2131 | 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
 
1888 jermar 29
/** @addtogroup mips32 
1702 cejka 30
 * @{
31
 */
32
/** @file
33
 */
34
 
575 palkovsky 35
#include <interrupt.h>
36
#include <arch/cp0.h>
2089 decky 37
#include <ipc/irq.h>
575 palkovsky 38
#include <arch/drivers/serial.h>
39
#include <console/chardev.h>
40
#include <console/console.h>
2456 hudecek 41
#include <synch/rcu.h>
575 palkovsky 42
 
1936 decky 43
#define SERIAL_IRQ 2
44
 
45
static irq_t serial_irq;
575 palkovsky 46
static chardev_t console;
47
static serial_t sconf[SERIAL_MAX];
577 palkovsky 48
static bool kb_enabled;
575 palkovsky 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
{
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? */
114
static void serial_irq_handler(irq_t *irq, void *arg, ...)
115
{
2456 hudecek 116
    if ((rcu_dereference_pointer(irq->notif_cfg).notify) && (rcu_dereference_pointer(irq->notif_cfg).answerbox))
1936 decky 117
        ipc_irq_send_notif(irq);
118
    else
119
        serial_handler();
120
}
575 palkovsky 121
 
1936 decky 122
static irq_ownership_t serial_claim(void)
123
{
124
    return IRQ_ACCEPT;
125
}
607 palkovsky 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
 
1936 decky 134
void serial_console(devno_t devno)
575 palkovsky 135
{
136
    serial_t *sd = &sconf[0];
137
 
138
 
139
    chardev_initialize("serial_console", &console, &serial_ops);
140
    console.data = sd;
577 palkovsky 141
    kb_enabled = true;
1936 decky 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);
575 palkovsky 149
 
150
    /* I don't know why, but the serial interrupts simply
151
     * don't work on simics
152
     */
1955 decky 153
    virtual_timer_fnc = &serial_handler;
575 palkovsky 154
 
606 palkovsky 155
    stdin = &console;
156
    stdout = &console;
575 palkovsky 157
}
1702 cejka 158
 
1888 jermar 159
/** @}
1702 cejka 160
 */