Subversion Repositories HelenOS

Rev

Rev 3934 | Rev 3941 | Go to most recent revision | 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>
3657 vana 41
#include <arch/drivers/kbd.h>
1920 jermar 42
#include <ddi/irq.h>
1931 jermar 43
#include <ipc/irq.h>
1844 jermar 44
#include <cpu.h>
45
#include <arch/asm.h>
46
#include <arch.h>
47
#include <console/chardev.h>
48
#include <console/console.h>
49
#include <interrupt.h>
1931 jermar 50
#include <arch/interrupt.h>
1880 jermar 51
#include <sysinfo/sysinfo.h>
1931 jermar 52
#include <synch/spinlock.h>
3934 jermar 53
#include <mm/slab.h>
1844 jermar 54
 
55
#define LSR_DATA_READY  0x01
56
 
3934 jermar 57
static irq_t *ns16550_irq;
1921 jermar 58
 
1844 jermar 59
/*
60
 * These codes read from ns16550 data register are silently ignored.
61
 */
62
#define IGNORE_CODE 0x7f        /* all keys up */
63
 
64
static void ns16550_suspend(chardev_t *);
65
static void ns16550_resume(chardev_t *);
66
 
67
static chardev_operations_t ops = {
68
    .suspend = ns16550_suspend,
69
    .resume = ns16550_resume,
70
};
71
 
72
/** Initialize keyboard and service interrupts using kernel routine */
73
void ns16550_grab(void)
74
{
1944 jermar 75
    ipl_t ipl = interrupts_disable();
3934 jermar 76
    spinlock_lock(&ns16550_irq->lock);
77
    ns16550_irq->notif_cfg.notify = false;
78
    spinlock_unlock(&ns16550_irq->lock);
1944 jermar 79
    interrupts_restore(ipl);
1844 jermar 80
}
81
 
82
/** Resume the former interrupt vector */
83
void ns16550_release(void)
84
{
1944 jermar 85
    ipl_t ipl = interrupts_disable();
3934 jermar 86
    spinlock_lock(&ns16550_irq->lock);
87
    if (ns16550_irq->notif_cfg.answerbox)
88
        ns16550_irq->notif_cfg.notify = true;
89
    spinlock_unlock(&ns16550_irq->lock);
1944 jermar 90
    interrupts_restore(ipl);
1844 jermar 91
}
92
 
1921 jermar 93
/** Initialize ns16550.
94
 *
3934 jermar 95
 * @param dev       Addrress of the beginning of the device in I/O space.
3655 jermar 96
 * @param devno     Device number.
97
 * @param inr       Interrupt number.
98
 * @param cir       Clear interrupt function.
99
 * @param cir_arg   First argument to cir.
3934 jermar 100
 *
101
 * @return      True on success, false on failure.
1921 jermar 102
 */
3934 jermar 103
bool
104
ns16550_init(ns16550_t *dev, devno_t devno, inr_t inr, cir_t cir, void *cir_arg)
1844 jermar 105
{
3934 jermar 106
    ns16550_instance_t *instance;
107
 
1844 jermar 108
    chardev_initialize("ns16550_kbd", &kbrd, &ops);
109
    stdin = &kbrd;
1880 jermar 110
 
3934 jermar 111
    instance = malloc(sizeof(ns16550_instance_t), FRAME_ATOMIC);
112
    if (!instance)
113
        return false;
114
 
115
    instance->devno = devno;
116
    instance->ns16550 = dev;
1921 jermar 117
 
3938 jermar 118
    irq_initialize(&instance->irq);
119
    instance->irq.devno = devno;
120
    instance->irq.inr = inr;
121
    instance->irq.claim = ns16550_claim;
122
    instance->irq.handler = ns16550_irq_handler;
123
    instance->irq.instance = instance;
124
    instance->irq.cir = cir;
125
    instance->irq.cir_arg = cir_arg;
126
    irq_register(&instance->irq);
3934 jermar 127
 
3938 jermar 128
    ns16550_irq = &instance->irq;   /* TODO: remove me soon */
3877 decky 129
 
3938 jermar 130
    while ((pio_read_8(&dev->lsr) & LSR_DATA_READY))
3934 jermar 131
        (void) pio_read_8(&dev->rbr);
1921 jermar 132
 
1880 jermar 133
    sysinfo_set_item_val("kbd", NULL, true);
1931 jermar 134
    sysinfo_set_item_val("kbd.type", NULL, KBD_NS16550);
1921 jermar 135
    sysinfo_set_item_val("kbd.devno", NULL, devno);
136
    sysinfo_set_item_val("kbd.inr", NULL, inr);
3934 jermar 137
    sysinfo_set_item_val("kbd.address.virtual", NULL, (uintptr_t) dev);
138
    sysinfo_set_item_val("kbd.port", NULL, (uintptr_t) dev);
3877 decky 139
 
3646 jermar 140
    /* Enable interrupts */
3934 jermar 141
    pio_write_8(&dev->ier, IER_ERBFI);
142
    pio_write_8(&dev->mcr, MCR_OUT2);
1911 jermar 143
 
3934 jermar 144
    ns16550_grab();
3877 decky 145
 
3934 jermar 146
    return true;
1844 jermar 147
}
148
 
149
/* Called from getc(). */
150
void ns16550_resume(chardev_t *d)
151
{
152
}
153
 
154
/* Called from getc(). */
155
void ns16550_suspend(chardev_t *d)
156
{
157
}
158
 
3934 jermar 159
irq_ownership_t ns16550_claim(void *instance)
1844 jermar 160
{
3934 jermar 161
    ns16550_instance_t *ns16550_instance = instance;
162
    ns16550_t *dev = ns16550_instance->ns16550;
1844 jermar 163
 
3934 jermar 164
    if (pio_read_8(&dev->lsr) & LSR_DATA_READY)
165
        return IRQ_ACCEPT;
166
    else
167
        return IRQ_DECLINE;
1844 jermar 168
}
169
 
3934 jermar 170
void ns16550_irq_handler(irq_t *irq)
1844 jermar 171
{
3934 jermar 172
    if (irq->notif_cfg.notify && irq->notif_cfg.answerbox) {
173
        /*
174
         * This will hopefully go to the IRQ dispatch code soon.
175
         */
176
        ipc_irq_send_notif(irq);
177
        return;
178
    }
179
 
180
    ns16550_instance_t *ns16550_instance = irq->instance;
181
    ns16550_t *dev = ns16550_instance->ns16550;
182
 
183
    if (pio_read_8(&dev->lsr) & LSR_DATA_READY) {
1931 jermar 184
        uint8_t x;
185
 
3934 jermar 186
        x = pio_read_8(&dev->rbr);
3877 decky 187
 
1844 jermar 188
        if (x != IGNORE_CODE) {
189
            if (x & KEY_RELEASE)
190
                key_released(x ^ KEY_RELEASE);
191
            else
192
                key_pressed(x);
193
        }
194
    }
195
 
1919 jermar 196
}
197
 
1844 jermar 198
/** @}
199
 */