Subversion Repositories HelenOS

Rev

Rev 2131 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1844 jermar 1
/*
2071 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 <console/chardev.h>
49
#include <console/console.h>
50
#include <interrupt.h>
1931 jermar 51
#include <arch/interrupt.h>
1880 jermar 52
#include <sysinfo/sysinfo.h>
1931 jermar 53
#include <synch/spinlock.h>
2456 hudecek 54
#include <synch/rcu.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
{
1944 jermar 83
    ipl_t ipl = interrupts_disable();
84
 
1931 jermar 85
    ns16550_ier_write(&ns16550, IER_ERBFI);     /* enable receiver interrupt */
86
 
87
    while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY)
88
        (void) ns16550_rbr_read(&ns16550);
89
 
2456 hudecek 90
    //rcu_read_lock() is not needed, ints are disabled
91
    //rcu: atomic update doesn't need reallocation  
92
    rcu_dereference_pointer(ns16550_irq.notif_cfg).notify = false;
1944 jermar 93
    interrupts_restore(ipl);
1844 jermar 94
}
95
 
96
/** Resume the former interrupt vector */
97
void ns16550_release(void)
98
{
1944 jermar 99
    ipl_t ipl = interrupts_disable();
2456 hudecek 100
    //rcu: atomic update doesn't need reallocation
1944 jermar 101
    spinlock_lock(&ns16550_irq.lock);
2456 hudecek 102
    if (rcu_dereference_pointer(ns16550_irq.notif_cfg).answerbox)
103
        rcu_dereference_pointer(ns16550_irq.notif_cfg).notify = true;
1944 jermar 104
    spinlock_unlock(&ns16550_irq.lock);
105
    interrupts_restore(ipl);
1844 jermar 106
}
107
 
1921 jermar 108
/** Initialize ns16550.
109
 *
110
 * @param devno Device number.
111
 * @param inr Interrupt number.
112
 * @param vaddr Virtual address of device's registers.
113
 */
114
void ns16550_init(devno_t devno, inr_t inr, uintptr_t vaddr)
1844 jermar 115
{
116
    chardev_initialize("ns16550_kbd", &kbrd, &ops);
117
    stdin = &kbrd;
1880 jermar 118
 
1921 jermar 119
    ns16550.devno = devno;
120
    ns16550.reg = (uint8_t *) vaddr;
121
 
122
    irq_initialize(&ns16550_irq);
123
    ns16550_irq.devno = devno;
124
    ns16550_irq.inr = inr;
125
    ns16550_irq.claim = ns16550_claim;
126
    ns16550_irq.handler = ns16550_irq_handler;
127
    irq_register(&ns16550_irq);
128
 
1880 jermar 129
    sysinfo_set_item_val("kbd", NULL, true);
1931 jermar 130
    sysinfo_set_item_val("kbd.type", NULL, KBD_NS16550);
1921 jermar 131
    sysinfo_set_item_val("kbd.devno", NULL, devno);
132
    sysinfo_set_item_val("kbd.inr", NULL, inr);
133
    sysinfo_set_item_val("kbd.address.virtual", NULL, vaddr);
1911 jermar 134
 
1931 jermar 135
    ns16550_grab();
1844 jermar 136
}
137
 
1931 jermar 138
/** Process ns16550 interrupt. */
139
void ns16550_interrupt(void)
1844 jermar 140
{
1931 jermar 141
    /* TODO
142
     *
143
     * ns16550 works in the polled mode so far.
144
     */
1844 jermar 145
}
146
 
147
/* Called from getc(). */
148
void ns16550_resume(chardev_t *d)
149
{
150
}
151
 
152
/* Called from getc(). */
153
void ns16550_suspend(chardev_t *d)
154
{
155
}
156
 
1896 jermar 157
char ns16550_key_read(chardev_t *d)
1844 jermar 158
{
159
    char ch;   
160
 
161
    while(!(ch = active_read_buff_read())) {
162
        uint8_t x;
1921 jermar 163
        while (!(ns16550_lsr_read(&ns16550) & LSR_DATA_READY))
1844 jermar 164
            ;
1921 jermar 165
        x = ns16550_rbr_read(&ns16550);
1844 jermar 166
        if (x != IGNORE_CODE) {
167
            if (x & KEY_RELEASE)
168
                key_released(x ^ KEY_RELEASE);
169
            else
170
                active_read_key_pressed(x);
171
        }
172
    }
173
    return ch;
174
}
175
 
176
/** Poll for key press and release events.
177
 *
178
 * This function can be used to implement keyboard polling.
179
 */
180
void ns16550_poll(void)
181
{
1931 jermar 182
    ipl_t ipl;
1844 jermar 183
 
1931 jermar 184
    ipl = interrupts_disable();
185
    spinlock_lock(&ns16550_irq.lock);
186
 
187
    if (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) {
2456 hudecek 188
        if (rcu_dereference_pointer(ns16550_irq.notif_cfg).notify && rcu_dereference_pointer(ns16550_irq.notif_cfg).answerbox) {
1931 jermar 189
            /*
190
             * Send IPC notification.
191
             */
192
            ipc_irq_send_notif(&ns16550_irq);
193
            spinlock_unlock(&ns16550_irq.lock);
194
            interrupts_restore(ipl);
195
            return;
196
        }
197
    }
198
 
199
    spinlock_unlock(&ns16550_irq.lock);
200
    interrupts_restore(ipl);
201
 
1921 jermar 202
    while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) {
1931 jermar 203
        uint8_t x;
204
 
1921 jermar 205
        x = ns16550_rbr_read(&ns16550);
1844 jermar 206
        if (x != IGNORE_CODE) {
207
            if (x & KEY_RELEASE)
208
                key_released(x ^ KEY_RELEASE);
209
            else
210
                key_pressed(x);
211
        }
212
    }
213
}
214
 
1919 jermar 215
irq_ownership_t ns16550_claim(void)
216
{
1931 jermar 217
    return (ns16550_lsr_read(&ns16550) & LSR_DATA_READY);
1919 jermar 218
}
219
 
220
void ns16550_irq_handler(irq_t *irq, void *arg, ...)
221
{
1931 jermar 222
    panic("Not yet implemented, ns16550 works in polled mode.\n");
1919 jermar 223
}
224
 
1844 jermar 225
/** @}
226
 */