Subversion Repositories HelenOS

Rev

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

Rev 3900 Rev 3906
1
/*
1
/*
2
 * Copyright (c) 2001-2006 Jakub Jermar
2
 * Copyright (c) 2001-2006 Jakub Jermar
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 genarch
29
/** @addtogroup genarch
30
 * @{
30
 * @{
31
 */
31
 */
32
/**
32
/**
33
 * @file
33
 * @file
34
 * @brief   NS 16550 serial port / keyboard driver.
34
 * @brief   NS 16550 serial port / keyboard driver.
35
 */
35
 */
36
 
36
 
37
#include <genarch/kbd/ns16550.h>
37
#include <genarch/kbd/ns16550.h>
38
#include <genarch/kbd/key.h>
38
#include <genarch/kbd/key.h>
39
#include <genarch/kbd/scanc.h>
39
#include <genarch/kbd/scanc.h>
40
#include <genarch/kbd/scanc_sun.h>
40
#include <genarch/kbd/scanc_sun.h>
41
#include <arch/drivers/kbd.h>
41
#include <arch/drivers/kbd.h>
42
#include <ddi/irq.h>
42
#include <ddi/irq.h>
43
#include <ipc/irq.h>
43
#include <ipc/irq.h>
44
#include <cpu.h>
44
#include <cpu.h>
45
#include <arch/asm.h>
45
#include <arch/asm.h>
46
#include <arch.h>
46
#include <arch.h>
47
#include <console/chardev.h>
47
#include <console/chardev.h>
48
#include <console/console.h>
48
#include <console/console.h>
49
#include <interrupt.h>
49
#include <interrupt.h>
50
#include <arch/interrupt.h>
50
#include <arch/interrupt.h>
51
#include <sysinfo/sysinfo.h>
51
#include <sysinfo/sysinfo.h>
52
#include <synch/spinlock.h>
52
#include <synch/spinlock.h>
53
 
53
 
54
#define LSR_DATA_READY  0x01
54
#define LSR_DATA_READY  0x01
55
 
55
 
56
/** Structure representing the ns16550. */
56
/** Structure representing the ns16550. */
57
static ns16550_t ns16550;
57
static ns16550_t ns16550;
58
 
58
 
59
/** Structure for ns16550's IRQ. */
59
/** Structure for ns16550's IRQ. */
60
static irq_t ns16550_irq;
60
static irq_t ns16550_irq;
61
 
61
 
62
/*
62
/*
63
 * These codes read from ns16550 data register are silently ignored.
63
 * These codes read from ns16550 data register are silently ignored.
64
 */
64
 */
65
#define IGNORE_CODE 0x7f        /* all keys up */
65
#define IGNORE_CODE 0x7f        /* all keys up */
66
 
66
 
67
static void ns16550_suspend(chardev_t *);
67
static void ns16550_suspend(chardev_t *);
68
static void ns16550_resume(chardev_t *);
68
static void ns16550_resume(chardev_t *);
69
 
69
 
70
static chardev_operations_t ops = {
70
static chardev_operations_t ops = {
71
    .suspend = ns16550_suspend,
71
    .suspend = ns16550_suspend,
72
    .resume = ns16550_resume,
72
    .resume = ns16550_resume,
73
    .read = ns16550_key_read
73
    .read = ns16550_key_read
74
};
74
};
75
 
75
 
76
void ns16550_interrupt(void);
76
void ns16550_interrupt(void);
77
 
77
 
78
/** Initialize keyboard and service interrupts using kernel routine */
78
/** Initialize keyboard and service interrupts using kernel routine */
79
void ns16550_grab(void)
79
void ns16550_grab(void)
80
{
80
{
81
    ipl_t ipl = interrupts_disable();
81
    ipl_t ipl = interrupts_disable();
82
 
82
 
83
    ns16550_ier_write(&ns16550, IER_ERBFI);     /* enable receiver interrupt */
83
    ns16550_ier_write(&ns16550, IER_ERBFI);     /* enable receiver interrupt */
84
   
84
   
85
    while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY)
85
    while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY)
86
        (void) ns16550_rbr_read(&ns16550);
86
        (void) ns16550_rbr_read(&ns16550);
87
 
87
 
88
    spinlock_lock(&ns16550_irq.lock);
88
    spinlock_lock(&ns16550_irq.lock);
89
    ns16550_irq.notif_cfg.notify = false;
89
    ns16550_irq.notif_cfg.notify = false;
90
    spinlock_unlock(&ns16550_irq.lock);
90
    spinlock_unlock(&ns16550_irq.lock);
91
    interrupts_restore(ipl);
91
    interrupts_restore(ipl);
92
}
92
}
93
 
93
 
94
/** Resume the former interrupt vector */
94
/** Resume the former interrupt vector */
95
void ns16550_release(void)
95
void ns16550_release(void)
96
{
96
{
97
    ipl_t ipl = interrupts_disable();
97
    ipl_t ipl = interrupts_disable();
98
    spinlock_lock(&ns16550_irq.lock);
98
    spinlock_lock(&ns16550_irq.lock);
99
    if (ns16550_irq.notif_cfg.answerbox)
99
    if (ns16550_irq.notif_cfg.answerbox)
100
        ns16550_irq.notif_cfg.notify = true;
100
        ns16550_irq.notif_cfg.notify = true;
101
    spinlock_unlock(&ns16550_irq.lock);
101
    spinlock_unlock(&ns16550_irq.lock);
102
    interrupts_restore(ipl);
102
    interrupts_restore(ipl);
103
}
103
}
104
 
104
 
105
/** Initialize ns16550.
105
/** Initialize ns16550.
106
 *
106
 *
107
 * @param devno     Device number.
107
 * @param devno     Device number.
108
 * @param port      Virtual/IO address of device's registers.
108
 * @param port      Virtual/IO address of device's registers.
109
 * @param inr       Interrupt number.
109
 * @param inr       Interrupt number.
110
 * @param cir       Clear interrupt function.
110
 * @param cir       Clear interrupt function.
111
 * @param cir_arg   First argument to cir.
111
 * @param cir_arg   First argument to cir.
112
 */
112
 */
113
void
113
void
114
ns16550_init(devno_t devno, ioport_t port, inr_t inr, cir_t cir, void *cir_arg)
114
ns16550_init(devno_t devno, ioport_t port, inr_t inr, cir_t cir, void *cir_arg)
115
{
115
{
116
    chardev_initialize("ns16550_kbd", &kbrd, &ops);
116
    chardev_initialize("ns16550_kbd", &kbrd, &ops);
117
    stdin = &kbrd;
117
    stdin = &kbrd;
118
   
118
   
119
    ns16550.devno = devno;
119
    ns16550.devno = devno;
120
    ns16550.io_port = port;
120
    ns16550.io_port = port;
121
   
121
   
122
    irq_initialize(&ns16550_irq);
122
    irq_initialize(&ns16550_irq);
123
    ns16550_irq.devno = devno;
123
    ns16550_irq.devno = devno;
124
    ns16550_irq.inr = inr;
124
    ns16550_irq.inr = inr;
125
    ns16550_irq.claim = ns16550_claim;
125
    ns16550_irq.claim = ns16550_claim;
126
    ns16550_irq.handler = ns16550_irq_handler;
126
    ns16550_irq.handler = ns16550_irq_handler;
127
    ns16550_irq.cir = cir;
127
    ns16550_irq.cir = cir;
128
    ns16550_irq.cir_arg = cir_arg;
128
    ns16550_irq.cir_arg = cir_arg;
129
    irq_register(&ns16550_irq);
129
    irq_register(&ns16550_irq);
130
   
130
   
131
    while ((ns16550_lsr_read(&ns16550) & LSR_DATA_READY))
131
    while ((ns16550_lsr_read(&ns16550) & LSR_DATA_READY))
132
        ns16550_rbr_read(&ns16550);
132
        ns16550_rbr_read(&ns16550);
133
   
133
   
134
    sysinfo_set_item_val("kbd", NULL, true);
134
    sysinfo_set_item_val("kbd", NULL, true);
135
    sysinfo_set_item_val("kbd.type", NULL, KBD_NS16550);
135
    sysinfo_set_item_val("kbd.type", NULL, KBD_NS16550);
136
    sysinfo_set_item_val("kbd.devno", NULL, devno);
136
    sysinfo_set_item_val("kbd.devno", NULL, devno);
137
    sysinfo_set_item_val("kbd.inr", NULL, inr);
137
    sysinfo_set_item_val("kbd.inr", NULL, inr);
138
    sysinfo_set_item_val("kbd.address.virtual", NULL, port);
138
    sysinfo_set_item_val("kbd.address.virtual", NULL, port);
139
    sysinfo_set_item_val("kbd.port", NULL, port);
139
    sysinfo_set_item_val("kbd.port", NULL, port);
140
   
140
   
141
    /* Enable interrupts */
141
    /* Enable interrupts */
142
    ns16550_ier_write(&ns16550, IER_ERBFI);
142
    ns16550_ier_write(&ns16550, IER_ERBFI);
143
    ns16550_mcr_write(&ns16550, MCR_OUT2);
143
    ns16550_mcr_write(&ns16550, MCR_OUT2);
144
   
144
   
145
    uint8_t c;
145
    uint8_t c;
146
    // This switches rbr & ier to mode when accept baudrate constant
146
    // This switches rbr & ier to mode when accept baudrate constant
147
    c = ns16550_lcr_read(&ns16550);
147
    c = ns16550_lcr_read(&ns16550);
148
    ns16550_lcr_write(&ns16550, 0x80 | c);
148
    ns16550_lcr_write(&ns16550, 0x80 | c);
149
    ns16550_rbr_write(&ns16550, 0x0c);
149
    ns16550_rbr_write(&ns16550, 0x0c);
150
    ns16550_ier_write(&ns16550, 0x00);
150
    ns16550_ier_write(&ns16550, 0x00);
151
    ns16550_lcr_write(&ns16550, c);
151
    ns16550_lcr_write(&ns16550, c);
152
   
152
   
153
    ns16550_grab();
153
    ns16550_grab();
154
}
154
}
155
 
155
 
156
/** Process ns16550 interrupt. */
156
/** Process ns16550 interrupt. */
157
void ns16550_interrupt(void)
157
void ns16550_interrupt(void)
158
{
158
{
159
    ns16550_poll();
159
    ns16550_poll();
160
}
160
}
161
 
161
 
162
/* Called from getc(). */
162
/* Called from getc(). */
163
void ns16550_resume(chardev_t *d)
163
void ns16550_resume(chardev_t *d)
164
{
164
{
165
}
165
}
166
 
166
 
167
/* Called from getc(). */
167
/* Called from getc(). */
168
void ns16550_suspend(chardev_t *d)
168
void ns16550_suspend(chardev_t *d)
169
{
169
{
170
}
170
}
171
 
171
 
172
 
172
 
173
char ns16550_key_read(chardev_t *d)
173
char ns16550_key_read(chardev_t *d)
174
{
174
{
175
    char ch;   
175
    char ch;   
176
 
176
 
177
    while(!(ch = active_read_buff_read())) {
177
    while(!(ch = active_read_buff_read())) {
178
        uint8_t x;
178
        uint8_t x;
179
        while (!(ns16550_lsr_read(&ns16550) & LSR_DATA_READY));
179
        while (!(ns16550_lsr_read(&ns16550) & LSR_DATA_READY));
180
       
180
       
181
        x = ns16550_rbr_read(&ns16550);
181
        x = ns16550_rbr_read(&ns16550);
182
       
182
       
183
        if (x != IGNORE_CODE) {
183
        if (x != IGNORE_CODE) {
184
            if (x & KEY_RELEASE)
184
            if (x & KEY_RELEASE)
185
                key_released(x ^ KEY_RELEASE);
185
                key_released(x ^ KEY_RELEASE);
186
            else
186
            else
187
                active_read_key_pressed(x);
187
                active_read_key_pressed(x);
188
        }
188
        }
189
    }
189
    }
190
    return ch;
190
    return ch;
191
}
191
}
192
 
192
 
193
/** Poll for key press and release events.
193
/** Poll for key press and release events.
194
 *
194
 *
195
 * This function can be used to implement keyboard polling.
195
 * This function can be used to implement keyboard polling.
196
 */
196
 */
197
void ns16550_poll(void)
197
void ns16550_poll(void)
198
{
198
{
199
    while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) {
199
    while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) {
200
        uint8_t x;
200
        uint8_t x;
201
       
201
       
202
        x = ns16550_rbr_read(&ns16550);
202
        x = ns16550_rbr_read(&ns16550);
203
       
203
       
204
        if (x != IGNORE_CODE) {
204
        if (x != IGNORE_CODE) {
205
            if (x & KEY_RELEASE)
205
            if (x & KEY_RELEASE)
206
                key_released(x ^ KEY_RELEASE);
206
                key_released(x ^ KEY_RELEASE);
207
            else
207
            else
208
                key_pressed(x);
208
                key_pressed(x);
209
        }
209
        }
210
    }
210
    }
211
}
211
}
212
 
212
 
213
irq_ownership_t ns16550_claim(void)
213
irq_ownership_t ns16550_claim(void *instance)
214
{
214
{
215
    return (ns16550_lsr_read(&ns16550) & LSR_DATA_READY);
215
    return (ns16550_lsr_read(&ns16550) & LSR_DATA_READY);
216
}
216
}
217
 
217
 
218
void ns16550_irq_handler(irq_t *irq, void *arg, ...)
218
void ns16550_irq_handler(irq_t *irq)
219
{
219
{
220
    if (irq->notif_cfg.notify && irq->notif_cfg.answerbox)
220
    if (irq->notif_cfg.notify && irq->notif_cfg.answerbox)
221
        ipc_irq_send_notif(irq);
221
        ipc_irq_send_notif(irq);
222
    else
222
    else
223
        ns16550_interrupt();
223
        ns16550_interrupt();
224
}
224
}
225
 
225
 
226
/** @}
226
/** @}
227
 */
227
 */
228
 
228