Subversion Repositories HelenOS

Rev

Rev 1888 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1888 Rev 1936
1
/*
1
/*
2
 * Copyright (C) 2005 Ondrej Palkovsky
2
 * Copyright (C) 2005 Ondrej Palkovsky
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup mips32 
29
/** @addtogroup mips32 
30
 * @{
30
 * @{
31
 */
31
 */
32
/** @file
32
/** @file
33
 */
33
 */
34
 
34
 
35
#include <interrupt.h>
35
#include <interrupt.h>
36
#include <arch/cp0.h>
36
#include <arch/cp0.h>
37
#include <arch/drivers/serial.h>
37
#include <arch/drivers/serial.h>
38
#include <console/chardev.h>
38
#include <console/chardev.h>
39
#include <console/console.h>
39
#include <console/console.h>
-
 
40
#include <ddi/irq.h>
40
 
41
 
-
 
42
#define SERIAL_IRQ 2
-
 
43
 
-
 
44
static irq_t serial_irq;
41
static chardev_t console;
45
static chardev_t console;
42
static serial_t sconf[SERIAL_MAX];
46
static serial_t sconf[SERIAL_MAX];
43
static bool kb_enabled;
47
static bool kb_enabled;
44
 
48
 
45
static void serial_write(chardev_t *d, const char ch)
49
static void serial_write(chardev_t *d, const char ch)
46
{
50
{
47
    serial_t *sd = (serial_t *)d->data;
51
    serial_t *sd = (serial_t *)d->data;
48
 
52
 
49
    if (ch == '\n')
53
    if (ch == '\n')
50
        serial_write(d, '\r');
54
        serial_write(d, '\r');
51
    /* Wait until transmit buffer empty */
55
    /* Wait until transmit buffer empty */
52
    while (! (SERIAL_READ_LSR(sd->port) & (1<<TRANSMIT_EMPTY_BIT)))
56
    while (! (SERIAL_READ_LSR(sd->port) & (1<<TRANSMIT_EMPTY_BIT)))
53
        ;
57
        ;
54
    SERIAL_WRITE(sd->port, ch);
58
    SERIAL_WRITE(sd->port, ch);
55
}
59
}
56
 
60
 
57
static void serial_enable(chardev_t *d)
61
static void serial_enable(chardev_t *d)
58
{
62
{
59
    kb_enabled = true;
63
    kb_enabled = true;
60
}
64
}
61
 
65
 
62
static void serial_disable(chardev_t *d)
66
static void serial_disable(chardev_t *d)
63
{
67
{
64
    kb_enabled = false;
68
    kb_enabled = false;
65
}
69
}
66
 
70
 
67
int serial_init(void)
71
int serial_init(void)
68
{
72
{
69
    int i = 0;
73
    int i = 0;
70
    if (SERIAL_READ_LSR(SERIAL_COM1) == 0x60) {
74
    if (SERIAL_READ_LSR(SERIAL_COM1) == 0x60) {
71
        sconf[i].port = SERIAL_COM1;
75
        sconf[i].port = SERIAL_COM1;
72
        sconf[i].irq = SERIAL_COM1_IRQ;
76
        sconf[i].irq = SERIAL_COM1_IRQ;
73
        /* Enable interrupt on available data */
77
        /* Enable interrupt on available data */
74
        i++;
78
        i++;
75
    }
79
    }
76
    return i;
80
    return i;
77
}
81
}
78
 
82
 
79
/** Read character from serial port, wait until available */
83
/** Read character from serial port, wait until available */
80
static char serial_do_read(chardev_t *dev)
84
static char serial_do_read(chardev_t *dev)
81
{
85
{
82
    serial_t *sd = (serial_t *)dev->data;
86
    serial_t *sd = (serial_t *)dev->data;
83
    char ch;
87
    char ch;
84
 
88
 
85
    while (!(SERIAL_READ_LSR(sd->port) & 1))
89
    while (!(SERIAL_READ_LSR(sd->port) & 1))
86
        ;
90
        ;
87
    ch = SERIAL_READ(sd->port);
91
    ch = SERIAL_READ(sd->port);
88
 
92
 
89
    if (ch =='\r')
93
    if (ch =='\r')
90
        ch = '\n';
94
        ch = '\n';
91
    return ch;
95
    return ch;
92
}
96
}
93
 
97
 
94
 
-
 
95
/** Process keyboard interrupt. Does not work in simics? */
-
 
96
static void serial_interrupt(int n, void *stack)
98
static void serial_handler(void)
97
{
99
{
98
    serial_t *sd = (serial_t *)console.data;
100
    serial_t *sd = (serial_t *) console.data;
99
    char ch;
101
    char ch;
100
 
102
 
101
    if (!(SERIAL_READ_LSR(sd->port) & 1))
103
    if (!(SERIAL_READ_LSR(sd->port) & 1))
102
        return;
104
        return;
103
    ch = SERIAL_READ(sd->port);
105
    ch = SERIAL_READ(sd->port);
104
 
106
 
105
    if (ch =='\r')
107
    if (ch =='\r')
106
        ch = '\n';
108
        ch = '\n';
107
    chardev_push_character(&console, ch);
109
    chardev_push_character(&console, ch);
108
}
110
}
109
 
111
 
-
 
112
/** Process keyboard interrupt. Does not work in simics? */
-
 
113
static void serial_irq_handler(irq_t *irq, void *arg, ...)
-
 
114
{
-
 
115
    if ((irq->notif_cfg.notify) && (irq->notif_cfg.answerbox))
-
 
116
        ipc_irq_send_notif(irq);
-
 
117
    else
-
 
118
        serial_handler();
-
 
119
}
110
 
120
 
-
 
121
static irq_ownership_t serial_claim(void)
-
 
122
{
-
 
123
    return IRQ_ACCEPT;
-
 
124
}
111
 
125
 
112
static chardev_operations_t serial_ops = {
126
static chardev_operations_t serial_ops = {
113
    .resume = serial_enable,
127
    .resume = serial_enable,
114
    .suspend = serial_disable,
128
    .suspend = serial_disable,
115
    .write = serial_write,
129
    .write = serial_write,
116
    .read = serial_do_read
130
    .read = serial_do_read
117
};
131
};
118
 
132
 
119
iroutine old_timer;
-
 
120
/** Do polling on timer interrupt */
-
 
121
static void timer_replace(int n, istate_t *istate)
-
 
122
{
-
 
123
    old_timer(n, istate);
-
 
124
    serial_interrupt(n, istate);
-
 
125
}
-
 
126
 
-
 
127
void serial_console(void)
133
void serial_console(devno_t devno)
128
{
134
{
129
    serial_t *sd = &sconf[0];
135
    serial_t *sd = &sconf[0];
130
 
136
 
131
 
137
 
132
    chardev_initialize("serial_console", &console, &serial_ops);
138
    chardev_initialize("serial_console", &console, &serial_ops);
133
    console.data = sd;
139
    console.data = sd;
134
    kb_enabled = true;
140
    kb_enabled = true;
-
 
141
   
-
 
142
    irq_initialize(&serial_irq);
-
 
143
    serial_irq.devno = devno;
-
 
144
    serial_irq.inr = SERIAL_IRQ;
-
 
145
    serial_irq.claim = serial_claim;
-
 
146
    serial_irq.handler = serial_irq_handler;
-
 
147
    irq_register(&serial_irq);
135
 
148
 
136
//  int_register(2, "serial_drvr", serial_interrupt);
-
 
137
    /* I don't know why, but the serial interrupts simply
149
    /* I don't know why, but the serial interrupts simply
138
     * don't work on simics
150
     * don't work on simics
139
     */
151
     */
140
    old_timer = int_register(TIMER_IRQ, "serial_drvr_poll", timer_replace);
152
    timer_fnc = &serial_handler;
141
   
153
   
142
    stdin = &console;
154
    stdin = &console;
143
    stdout = &console;
155
    stdout = &console;
144
}
156
}
145
 
157
 
146
/** @}
158
/** @}
147
 */
159
 */
148
 
160