Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
1844 jermar 1
/*
3982 jermar 2
 * Copyright (c) 2009 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>
1844 jermar 43
#include <cpu.h>
44
#include <arch/asm.h>
45
#include <arch.h>
46
#include <console/chardev.h>
47
#include <console/console.h>
48
#include <interrupt.h>
1931 jermar 49
#include <arch/interrupt.h>
50
#include <synch/spinlock.h>
3934 jermar 51
#include <mm/slab.h>
1844 jermar 52
 
53
#define LSR_DATA_READY  0x01
54
 
55
/*
56
 * These codes read from ns16550 data register are silently ignored.
57
 */
58
#define IGNORE_CODE 0x7f        /* all keys up */
59
 
60
static void ns16550_suspend(chardev_t *);
61
static void ns16550_resume(chardev_t *);
62
 
63
static chardev_operations_t ops = {
64
    .suspend = ns16550_suspend,
65
    .resume = ns16550_resume,
66
};
67
 
1921 jermar 68
/** Initialize ns16550.
69
 *
3934 jermar 70
 * @param dev       Addrress of the beginning of the device in I/O space.
3655 jermar 71
 * @param devno     Device number.
72
 * @param inr       Interrupt number.
73
 * @param cir       Clear interrupt function.
74
 * @param cir_arg   First argument to cir.
3934 jermar 75
 *
76
 * @return      True on success, false on failure.
1921 jermar 77
 */
3934 jermar 78
bool
79
ns16550_init(ns16550_t *dev, devno_t devno, inr_t inr, cir_t cir, void *cir_arg)
1844 jermar 80
{
3934 jermar 81
    ns16550_instance_t *instance;
82
 
1844 jermar 83
    chardev_initialize("ns16550_kbd", &kbrd, &ops);
84
    stdin = &kbrd;
1880 jermar 85
 
3934 jermar 86
    instance = malloc(sizeof(ns16550_instance_t), FRAME_ATOMIC);
87
    if (!instance)
88
        return false;
89
 
90
    instance->devno = devno;
91
    instance->ns16550 = dev;
1921 jermar 92
 
3938 jermar 93
    irq_initialize(&instance->irq);
94
    instance->irq.devno = devno;
95
    instance->irq.inr = inr;
96
    instance->irq.claim = ns16550_claim;
97
    instance->irq.handler = ns16550_irq_handler;
98
    instance->irq.instance = instance;
99
    instance->irq.cir = cir;
100
    instance->irq.cir_arg = cir_arg;
101
    irq_register(&instance->irq);
3934 jermar 102
 
3938 jermar 103
    while ((pio_read_8(&dev->lsr) & LSR_DATA_READY))
3934 jermar 104
        (void) pio_read_8(&dev->rbr);
1921 jermar 105
 
3646 jermar 106
    /* Enable interrupts */
3934 jermar 107
    pio_write_8(&dev->ier, IER_ERBFI);
108
    pio_write_8(&dev->mcr, MCR_OUT2);
1911 jermar 109
 
3934 jermar 110
    return true;
1844 jermar 111
}
112
 
113
/* Called from getc(). */
114
void ns16550_resume(chardev_t *d)
115
{
116
}
117
 
118
/* Called from getc(). */
119
void ns16550_suspend(chardev_t *d)
120
{
121
}
122
 
3941 jermar 123
irq_ownership_t ns16550_claim(irq_t *irq)
1844 jermar 124
{
3941 jermar 125
    ns16550_instance_t *ns16550_instance = irq->instance;
3934 jermar 126
    ns16550_t *dev = ns16550_instance->ns16550;
1844 jermar 127
 
3934 jermar 128
    if (pio_read_8(&dev->lsr) & LSR_DATA_READY)
129
        return IRQ_ACCEPT;
130
    else
131
        return IRQ_DECLINE;
1844 jermar 132
}
133
 
3934 jermar 134
void ns16550_irq_handler(irq_t *irq)
1844 jermar 135
{
3934 jermar 136
    ns16550_instance_t *ns16550_instance = irq->instance;
137
    ns16550_t *dev = ns16550_instance->ns16550;
138
 
139
    if (pio_read_8(&dev->lsr) & LSR_DATA_READY) {
1931 jermar 140
        uint8_t x;
141
 
3934 jermar 142
        x = pio_read_8(&dev->rbr);
3877 decky 143
 
1844 jermar 144
        if (x != IGNORE_CODE) {
145
            if (x & KEY_RELEASE)
146
                key_released(x ^ KEY_RELEASE);
147
            else
148
                key_pressed(x);
149
        }
150
    }
151
 
1919 jermar 152
}
153
 
1844 jermar 154
/** @}
155
 */