Subversion Repositories HelenOS

Rev

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

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