Subversion Repositories HelenOS

Rev

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

Rev 3646 Rev 3655
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
#ifndef ia64
41
#ifndef ia64
42
#include <arch/drivers/kbd.h>
42
#include <arch/drivers/kbd.h>
43
#include <arch/drivers/ns16550.h>
43
#include <arch/drivers/ns16550.h>
44
#endif
44
#endif
45
#include <ddi/irq.h>
45
#include <ddi/irq.h>
46
#include <ipc/irq.h>
46
#include <ipc/irq.h>
47
#include <cpu.h>
47
#include <cpu.h>
48
#include <arch/asm.h>
48
#include <arch/asm.h>
49
#include <arch.h>
49
#include <arch.h>
50
#include <console/chardev.h>
50
#include <console/chardev.h>
51
#include <console/console.h>
51
#include <console/console.h>
52
#include <interrupt.h>
52
#include <interrupt.h>
53
#include <arch/interrupt.h>
53
#include <arch/interrupt.h>
54
#include <sysinfo/sysinfo.h>
54
#include <sysinfo/sysinfo.h>
55
#include <synch/spinlock.h>
55
#include <synch/spinlock.h>
56
 
56
 
57
#define LSR_DATA_READY  0x01
57
#define LSR_DATA_READY  0x01
58
 
58
 
59
/** Structure representing the ns16550. */
59
/** Structure representing the ns16550. */
60
static ns16550_t ns16550;
60
static ns16550_t ns16550;
61
 
61
 
62
/** Structure for ns16550's IRQ. */
62
/** Structure for ns16550's IRQ. */
63
static irq_t ns16550_irq;
63
static irq_t ns16550_irq;
64
 
64
 
65
/*
65
/*
66
 * These codes read from ns16550 data register are silently ignored.
66
 * These codes read from ns16550 data register are silently ignored.
67
 */
67
 */
68
#define IGNORE_CODE 0x7f        /* all keys up */
68
#define IGNORE_CODE 0x7f        /* all keys up */
69
 
69
 
70
static void ns16550_suspend(chardev_t *);
70
static void ns16550_suspend(chardev_t *);
71
static void ns16550_resume(chardev_t *);
71
static void ns16550_resume(chardev_t *);
72
 
72
 
73
static chardev_operations_t ops = {
73
static chardev_operations_t ops = {
74
    .suspend = ns16550_suspend,
74
    .suspend = ns16550_suspend,
75
    .resume = ns16550_resume,
75
    .resume = ns16550_resume,
76
    .read = ns16550_key_read
76
    .read = ns16550_key_read
77
};
77
};
78
 
78
 
79
void ns16550_interrupt(void);
79
void ns16550_interrupt(void);
80
 
80
 
81
/** Initialize keyboard and service interrupts using kernel routine */
81
/** Initialize keyboard and service interrupts using kernel routine */
82
void ns16550_grab(void)
82
void ns16550_grab(void)
83
{
83
{
84
    ipl_t ipl = interrupts_disable();
84
    ipl_t ipl = interrupts_disable();
85
 
85
 
86
    ns16550_ier_write(&ns16550, IER_ERBFI);     /* enable receiver interrupt */
86
    ns16550_ier_write(&ns16550, IER_ERBFI);     /* enable receiver interrupt */
87
   
87
   
88
    while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY)
88
    while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY)
89
        (void) ns16550_rbr_read(&ns16550);
89
        (void) ns16550_rbr_read(&ns16550);
90
 
90
 
91
    spinlock_lock(&ns16550_irq.lock);
91
    spinlock_lock(&ns16550_irq.lock);
92
    ns16550_irq.notif_cfg.notify = false;
92
    ns16550_irq.notif_cfg.notify = false;
93
    spinlock_unlock(&ns16550_irq.lock);
93
    spinlock_unlock(&ns16550_irq.lock);
94
    interrupts_restore(ipl);
94
    interrupts_restore(ipl);
95
}
95
}
96
 
96
 
97
/** Resume the former interrupt vector */
97
/** Resume the former interrupt vector */
98
void ns16550_release(void)
98
void ns16550_release(void)
99
{
99
{
100
    ipl_t ipl = interrupts_disable();
100
    ipl_t ipl = interrupts_disable();
101
    spinlock_lock(&ns16550_irq.lock);
101
    spinlock_lock(&ns16550_irq.lock);
102
    if (ns16550_irq.notif_cfg.answerbox)
102
    if (ns16550_irq.notif_cfg.answerbox)
103
        ns16550_irq.notif_cfg.notify = true;
103
        ns16550_irq.notif_cfg.notify = true;
104
    spinlock_unlock(&ns16550_irq.lock);
104
    spinlock_unlock(&ns16550_irq.lock);
105
    interrupts_restore(ipl);
105
    interrupts_restore(ipl);
106
}
106
}
107
 
107
 
108
/** Initialize ns16550.
108
/** Initialize ns16550.
109
 *
109
 *
110
 * @param devno Device number.
110
 * @param devno     Device number.
-
 
111
 * @param port      Virtual/IO address of device's registers.
111
 * @param inr Interrupt number.
112
 * @param inr       Interrupt number.
-
 
113
 * @param cir       Clear interrupt function.
112
 * @param vaddr Virtual address of device's registers.
114
 * @param cir_arg   First argument to cir.
113
 */
115
 */
-
 
116
void
114
void ns16550_init(devno_t devno, inr_t inr, ioport_t port)
117
ns16550_init(devno_t devno, ioport_t port, inr_t inr, cir_t cir, void *cir_arg)
115
{
118
{
116
    chardev_initialize("ns16550_kbd", &kbrd, &ops);
119
    chardev_initialize("ns16550_kbd", &kbrd, &ops);
117
    stdin = &kbrd;
120
    stdin = &kbrd;
118
   
121
   
119
    ns16550.devno = devno;
122
    ns16550.devno = devno;
120
    ns16550.io_port = port;
123
    ns16550.io_port = port;
121
   
124
   
122
    irq_initialize(&ns16550_irq);
125
    irq_initialize(&ns16550_irq);
123
    ns16550_irq.devno = devno;
126
    ns16550_irq.devno = devno;
124
    ns16550_irq.inr = inr;
127
    ns16550_irq.inr = inr;
125
    ns16550_irq.claim = ns16550_claim;
128
    ns16550_irq.claim = ns16550_claim;
126
    ns16550_irq.handler = ns16550_irq_handler;
129
    ns16550_irq.handler = ns16550_irq_handler;
-
 
130
    ns16550_irq.cir = cir;
-
 
131
    ns16550_irq.cir_arg = cir_arg;
127
    irq_register(&ns16550_irq);
132
    irq_register(&ns16550_irq);
128
   
133
   
129
    sysinfo_set_item_val("kbd", NULL, true);
134
    sysinfo_set_item_val("kbd", NULL, true);
130
#ifndef ia64
135
#ifndef ia64
131
    sysinfo_set_item_val("kbd.type", NULL, KBD_NS16550);
136
    sysinfo_set_item_val("kbd.type", NULL, KBD_NS16550);
132
#endif
137
#endif
133
    sysinfo_set_item_val("kbd.devno", NULL, devno);
138
    sysinfo_set_item_val("kbd.devno", NULL, devno);
134
    sysinfo_set_item_val("kbd.inr", NULL, inr);
139
    sysinfo_set_item_val("kbd.inr", NULL, inr);
135
    sysinfo_set_item_val("kbd.address.virtual", NULL, port);
140
    sysinfo_set_item_val("kbd.address.virtual", NULL, port);
136
 
141
 
137
#ifdef CONFIG_NS16550_INTERRUPT_DRIVEN
142
#ifdef CONFIG_NS16550_INTERRUPT_DRIVEN
138
    /* Enable interrupts */
143
    /* Enable interrupts */
139
        ns16550_ier_write(&ns16550, IER_ERBFI);
144
        ns16550_ier_write(&ns16550, IER_ERBFI);
140
    ns16550_mcr_write(&ns16550, MCR_OUT2);
145
    ns16550_mcr_write(&ns16550, MCR_OUT2);
141
#endif
146
#endif
142
 
147
 
143
#ifdef ia64
148
#ifdef ia64
144
        uint8_t c;
149
        uint8_t c;
145
        c = ns16550_lcr_read(&ns16550);
150
        c = ns16550_lcr_read(&ns16550);
146
        ns16550_lcr_write(&ns16550, 0x80 | c);
151
        ns16550_lcr_write(&ns16550, 0x80 | c);
147
        ns16550_rbr_write(&ns16550, 0x0c);
152
        ns16550_rbr_write(&ns16550, 0x0c);
148
        ns16550_ier_write(&ns16550, 0x00);
153
        ns16550_ier_write(&ns16550, 0x00);
149
        ns16550_lcr_write(&ns16550, c);
154
        ns16550_lcr_write(&ns16550, c);
150
#endif
155
#endif
151
   
156
   
152
    ns16550_grab();
157
    ns16550_grab();
153
}
158
}
154
 
159
 
155
/** Process ns16550 interrupt. */
160
/** Process ns16550 interrupt. */
156
void ns16550_interrupt(void)
161
void ns16550_interrupt(void)
157
{
162
{
158
    ns16550_poll();
163
    ns16550_poll();
159
}
164
}
160
 
165
 
161
/* Called from getc(). */
166
/* Called from getc(). */
162
void ns16550_resume(chardev_t *d)
167
void ns16550_resume(chardev_t *d)
163
{
168
{
164
}
169
}
165
 
170
 
166
/* Called from getc(). */
171
/* Called from getc(). */
167
void ns16550_suspend(chardev_t *d)
172
void ns16550_suspend(chardev_t *d)
168
{
173
{
169
}
174
}
170
 
175
 
171
 
176
 
172
char ns16550_key_read(chardev_t *d)
177
char ns16550_key_read(chardev_t *d)
173
{
178
{
174
    char ch;   
179
    char ch;   
175
 
180
 
176
    while(!(ch = active_read_buff_read())) {
181
    while(!(ch = active_read_buff_read())) {
177
        uint8_t x;
182
        uint8_t x;
178
        while (!(ns16550_lsr_read(&ns16550) & LSR_DATA_READY))
183
        while (!(ns16550_lsr_read(&ns16550) & LSR_DATA_READY))
179
            ;
184
            ;
180
        x = ns16550_rbr_read(&ns16550);
185
        x = ns16550_rbr_read(&ns16550);
181
#ifndef ia64
186
#ifndef ia64
182
        if (x != IGNORE_CODE) {
187
        if (x != IGNORE_CODE) {
183
            if (x & KEY_RELEASE)
188
            if (x & KEY_RELEASE)
184
                key_released(x ^ KEY_RELEASE);
189
                key_released(x ^ KEY_RELEASE);
185
            else
190
            else
186
                active_read_key_pressed(x);
191
                active_read_key_pressed(x);
187
        }
192
        }
188
#else
193
#else
189
        extern chardev_t kbrd;
194
        extern chardev_t kbrd;
190
        if(x != 0x0d) {
195
        if(x != 0x0d) {
191
            if(x == 0x7f)
196
            if(x == 0x7f)
192
                x = '\b';
197
                x = '\b';
193
             chardev_push_character(&kbrd, x);
198
             chardev_push_character(&kbrd, x);
194
        }    
199
        }    
195
#endif      
200
#endif      
196
 
201
 
197
    }
202
    }
198
    return ch;
203
    return ch;
199
}
204
}
200
 
205
 
201
/** Poll for key press and release events.
206
/** Poll for key press and release events.
202
 *
207
 *
203
 * This function can be used to implement keyboard polling.
208
 * This function can be used to implement keyboard polling.
204
 */
209
 */
205
void ns16550_poll(void)
210
void ns16550_poll(void)
206
{
211
{
207
#ifndef CONFIG_NS16550_INTERRUPT_DRIVEN 
212
#ifndef CONFIG_NS16550_INTERRUPT_DRIVEN 
208
    ipl_t ipl;
213
    ipl_t ipl;
209
 
214
 
210
    ipl = interrupts_disable();
215
    ipl = interrupts_disable();
211
    spinlock_lock(&ns16550_irq.lock);
216
    spinlock_lock(&ns16550_irq.lock);
212
 
217
 
213
    if (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) {
218
    if (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) {
214
        if (ns16550_irq.notif_cfg.notify && ns16550_irq.notif_cfg.answerbox) {
219
        if (ns16550_irq.notif_cfg.notify && ns16550_irq.notif_cfg.answerbox) {
215
            /*
220
            /*
216
             * Send IPC notification.
221
             * Send IPC notification.
217
             */
222
             */
218
            ipc_irq_send_notif(&ns16550_irq);
223
            ipc_irq_send_notif(&ns16550_irq);
219
            spinlock_unlock(&ns16550_irq.lock);
224
            spinlock_unlock(&ns16550_irq.lock);
220
            interrupts_restore(ipl);
225
            interrupts_restore(ipl);
221
            return;
226
            return;
222
        }
227
        }
223
    }
228
    }
224
 
229
 
225
    spinlock_unlock(&ns16550_irq.lock);
230
    spinlock_unlock(&ns16550_irq.lock);
226
    interrupts_restore(ipl);
231
    interrupts_restore(ipl);
227
#endif
232
#endif
228
 
233
 
229
    while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) {
234
    while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) {
230
        uint8_t x;
235
        uint8_t x;
231
       
236
       
232
        x = ns16550_rbr_read(&ns16550);
237
        x = ns16550_rbr_read(&ns16550);
233
#ifndef ia64
238
#ifndef ia64
234
        if (x != IGNORE_CODE) {
239
        if (x != IGNORE_CODE) {
235
            if (x & KEY_RELEASE)
240
            if (x & KEY_RELEASE)
236
                key_released(x ^ KEY_RELEASE);
241
                key_released(x ^ KEY_RELEASE);
237
            else
242
            else
238
                key_pressed(x);
243
                key_pressed(x);
239
        }
244
        }
240
#else
245
#else
241
        extern chardev_t kbrd;
246
        extern chardev_t kbrd;
242
        if(x != 0x0d) {
247
        if(x != 0x0d) {
243
            if (x == 0x7f)
248
            if (x == 0x7f)
244
                x = '\b';
249
                x = '\b';
245
            chardev_push_character(&kbrd, x);
250
            chardev_push_character(&kbrd, x);
246
        }    
251
        }    
247
#endif      
252
#endif      
248
 
253
 
249
    }
254
    }
250
}
255
}
251
 
256
 
252
irq_ownership_t ns16550_claim(void)
257
irq_ownership_t ns16550_claim(void)
253
{
258
{
254
    return (ns16550_lsr_read(&ns16550) & LSR_DATA_READY);
259
    return (ns16550_lsr_read(&ns16550) & LSR_DATA_READY);
255
}
260
}
256
 
261
 
257
void ns16550_irq_handler(irq_t *irq, void *arg, ...)
262
void ns16550_irq_handler(irq_t *irq, void *arg, ...)
258
{
263
{
259
    if (irq->notif_cfg.notify && irq->notif_cfg.answerbox)
264
    if (irq->notif_cfg.notify && irq->notif_cfg.answerbox)
260
        ipc_irq_send_notif(irq);
265
        ipc_irq_send_notif(irq);
261
    else
266
    else
262
        ns16550_interrupt();
267
        ns16550_interrupt();
263
}
268
}
264
 
269
 
265
/** @}
270
/** @}
266
 */
271
 */
267
 
272