Subversion Repositories HelenOS

Rev

Rev 1931 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1844 jermar 1
/*
1921 jermar 2
 * Copyright (C) 2001-2006 Jakub Jermar
1844 jermar 3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
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
15
 *   derived from this software without specific prior written permission.
16
 *
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
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
/** @addtogroup genarch
30
 * @{
31
 */
32
/**
33
 * @file
34
 * @brief   NS 16550 serial port / keyboard driver.
35
 */
36
 
37
#include <genarch/kbd/ns16550.h>
38
#include <genarch/kbd/key.h>
39
#include <genarch/kbd/scanc.h>
40
#include <genarch/kbd/scanc_sun.h>
1931 jermar 41
#include <arch/drivers/kbd.h>
1844 jermar 42
#include <arch/drivers/ns16550.h>
1920 jermar 43
#include <ddi/irq.h>
1931 jermar 44
#include <ipc/irq.h>
1844 jermar 45
#include <cpu.h>
46
#include <arch/asm.h>
47
#include <arch.h>
48
#include <typedefs.h>
49
#include <console/chardev.h>
50
#include <console/console.h>
51
#include <interrupt.h>
1931 jermar 52
#include <arch/interrupt.h>
1880 jermar 53
#include <sysinfo/sysinfo.h>
1931 jermar 54
#include <synch/spinlock.h>
1844 jermar 55
 
56
#define LSR_DATA_READY  0x01
57
 
1921 jermar 58
/** Structure representing the ns16550. */
59
static ns16550_t ns16550;
60
 
61
/** Structure for ns16550's IRQ. */
62
static irq_t ns16550_irq;
63
 
1844 jermar 64
/*
65
 * These codes read from ns16550 data register are silently ignored.
66
 */
67
#define IGNORE_CODE 0x7f        /* all keys up */
68
 
69
static void ns16550_suspend(chardev_t *);
70
static void ns16550_resume(chardev_t *);
71
 
72
static chardev_operations_t ops = {
73
    .suspend = ns16550_suspend,
74
    .resume = ns16550_resume,
1896 jermar 75
    .read = ns16550_key_read
1844 jermar 76
};
77
 
1931 jermar 78
void ns16550_interrupt(void);
1844 jermar 79
 
80
/** Initialize keyboard and service interrupts using kernel routine */
81
void ns16550_grab(void)
82
{
1931 jermar 83
    ns16550_ier_write(&ns16550, IER_ERBFI);     /* enable receiver interrupt */
84
 
85
    while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY)
86
        (void) ns16550_rbr_read(&ns16550);
87
 
1932 jermar 88
    ns16550_irq.notif_cfg.notify = false;
1844 jermar 89
}
90
 
91
/** Resume the former interrupt vector */
92
void ns16550_release(void)
93
{
1932 jermar 94
    if (ns16550_irq.notif_cfg.answerbox)
95
        ns16550_irq.notif_cfg.notify = true;
1844 jermar 96
}
97
 
1921 jermar 98
/** Initialize ns16550.
99
 *
100
 * @param devno Device number.
101
 * @param inr Interrupt number.
102
 * @param vaddr Virtual address of device's registers.
103
 */
104
void ns16550_init(devno_t devno, inr_t inr, uintptr_t vaddr)
1844 jermar 105
{
106
    chardev_initialize("ns16550_kbd", &kbrd, &ops);
107
    stdin = &kbrd;
1880 jermar 108
 
1921 jermar 109
    ns16550.devno = devno;
110
    ns16550.reg = (uint8_t *) vaddr;
111
 
112
    irq_initialize(&ns16550_irq);
113
    ns16550_irq.devno = devno;
114
    ns16550_irq.inr = inr;
115
    ns16550_irq.claim = ns16550_claim;
116
    ns16550_irq.handler = ns16550_irq_handler;
117
    irq_register(&ns16550_irq);
118
 
1880 jermar 119
    sysinfo_set_item_val("kbd", NULL, true);
1931 jermar 120
    sysinfo_set_item_val("kbd.type", NULL, KBD_NS16550);
1921 jermar 121
    sysinfo_set_item_val("kbd.devno", NULL, devno);
122
    sysinfo_set_item_val("kbd.inr", NULL, inr);
123
    sysinfo_set_item_val("kbd.address.virtual", NULL, vaddr);
1911 jermar 124
 
1931 jermar 125
    ns16550_grab();
1844 jermar 126
}
127
 
1931 jermar 128
/** Process ns16550 interrupt. */
129
void ns16550_interrupt(void)
1844 jermar 130
{
1931 jermar 131
    /* TODO
132
     *
133
     * ns16550 works in the polled mode so far.
134
     */
1844 jermar 135
}
136
 
137
/* Called from getc(). */
138
void ns16550_resume(chardev_t *d)
139
{
140
}
141
 
142
/* Called from getc(). */
143
void ns16550_suspend(chardev_t *d)
144
{
145
}
146
 
1896 jermar 147
char ns16550_key_read(chardev_t *d)
1844 jermar 148
{
149
    char ch;   
150
 
151
    while(!(ch = active_read_buff_read())) {
152
        uint8_t x;
1921 jermar 153
        while (!(ns16550_lsr_read(&ns16550) & LSR_DATA_READY))
1844 jermar 154
            ;
1921 jermar 155
        x = ns16550_rbr_read(&ns16550);
1844 jermar 156
        if (x != IGNORE_CODE) {
157
            if (x & KEY_RELEASE)
158
                key_released(x ^ KEY_RELEASE);
159
            else
160
                active_read_key_pressed(x);
161
        }
162
    }
163
    return ch;
164
}
165
 
166
/** Poll for key press and release events.
167
 *
168
 * This function can be used to implement keyboard polling.
169
 */
170
void ns16550_poll(void)
171
{
1931 jermar 172
    ipl_t ipl;
1844 jermar 173
 
1931 jermar 174
    ipl = interrupts_disable();
175
    spinlock_lock(&ns16550_irq.lock);
176
 
177
    if (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) {
1932 jermar 178
        if (ns16550_irq.notif_cfg.notify && ns16550_irq.notif_cfg.answerbox) {
1931 jermar 179
            /*
180
             * Send IPC notification.
181
             */
182
            ipc_irq_send_notif(&ns16550_irq);
183
            spinlock_unlock(&ns16550_irq.lock);
184
            interrupts_restore(ipl);
185
            return;
186
        }
187
    }
188
 
189
    spinlock_unlock(&ns16550_irq.lock);
190
    interrupts_restore(ipl);
191
 
1921 jermar 192
    while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) {
1931 jermar 193
        uint8_t x;
194
 
1921 jermar 195
        x = ns16550_rbr_read(&ns16550);
1844 jermar 196
        if (x != IGNORE_CODE) {
197
            if (x & KEY_RELEASE)
198
                key_released(x ^ KEY_RELEASE);
199
            else
200
                key_pressed(x);
201
        }
202
    }
203
}
204
 
1919 jermar 205
irq_ownership_t ns16550_claim(void)
206
{
1931 jermar 207
    return (ns16550_lsr_read(&ns16550) & LSR_DATA_READY);
1919 jermar 208
}
209
 
210
void ns16550_irq_handler(irq_t *irq, void *arg, ...)
211
{
1931 jermar 212
    panic("Not yet implemented, ns16550 works in polled mode.\n");
1919 jermar 213
}
214
 
1844 jermar 215
/** @}
216
 */