Subversion Repositories HelenOS

Rev

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

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