Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
1 jermar 1
/*
2071 jermar 2
 * Copyright (c) 2001-2004 Jakub Jermar
1 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
 
1754 jermar 29
/** @addtogroup genarch
1702 cejka 30
 * @{
31
 */
1754 jermar 32
/**
1757 jermar 33
 * @file
1842 jermar 34
 * @brief   Zilog 8530 serial port / keyboard driver.
1702 cejka 35
 */
36
 
1842 jermar 37
#include <genarch/kbd/z8530.h>
1843 jermar 38
#include <genarch/kbd/key.h>
1842 jermar 39
#include <genarch/kbd/scanc.h>
40
#include <genarch/kbd/scanc_sun.h>
41
#include <arch/drivers/z8530.h>
1920 jermar 42
#include <ddi/irq.h>
1923 jermar 43
#include <ipc/irq.h>
1 jermar 44
#include <arch/interrupt.h>
1875 jermar 45
#include <arch/drivers/kbd.h>
1 jermar 46
#include <cpu.h>
47
#include <arch/asm.h>
48
#include <arch.h>
511 jermar 49
#include <console/chardev.h>
50
#include <console/console.h>
576 palkovsky 51
#include <interrupt.h>
1875 jermar 52
#include <sysinfo/sysinfo.h>
53
#include <print.h>
1 jermar 54
 
670 vana 55
/*
1842 jermar 56
 * These codes read from z8530 data register are silently ignored.
895 jermar 57
 */
1842 jermar 58
#define IGNORE_CODE 0x7f        /* all keys up */
895 jermar 59
 
1921 jermar 60
static z8530_t z8530;       /**< z8530 device structure. */
61
static irq_t z8530_irq;     /**< z8530's IRQ. */
62
 
1842 jermar 63
static void z8530_suspend(chardev_t *);
64
static void z8530_resume(chardev_t *);
511 jermar 65
 
66
static chardev_operations_t ops = {
1842 jermar 67
    .suspend = z8530_suspend,
68
    .resume = z8530_resume,
1896 jermar 69
    .read = z8530_key_read
511 jermar 70
};
71
 
1923 jermar 72
/** Initialize keyboard and service interrupts using kernel routine. */
1842 jermar 73
void z8530_grab(void)
1 jermar 74
{
1944 jermar 75
    ipl_t ipl = interrupts_disable();
76
 
1925 jermar 77
    (void) z8530_read_a(&z8530, RR8);
78
 
79
    /*
80
     * Clear any pending TX interrupts or we never manage
81
     * to set FHC UART interrupt state to idle.
82
     */
83
    z8530_write_a(&z8530, WR0, WR0_TX_IP_RST);
84
 
3655 jermar 85
    /* interrupt on all characters */
86
    z8530_write_a(&z8530, WR1, WR1_IARCSC);
1925 jermar 87
 
88
    /* 8 bits per character and enable receiver */
89
    z8530_write_a(&z8530, WR3, WR3_RX8BITSCH | WR3_RX_ENABLE);
90
 
3655 jermar 91
    /* Master Interrupt Enable. */
92
    z8530_write_a(&z8530, WR9, WR9_MIE);
1925 jermar 93
 
1944 jermar 94
    spinlock_lock(&z8530_irq.lock);
1932 jermar 95
    z8530_irq.notif_cfg.notify = false;
1944 jermar 96
    spinlock_unlock(&z8530_irq.lock);
97
    interrupts_restore(ipl);
1474 palkovsky 98
}
1849 jermar 99
 
1923 jermar 100
/** Resume the former IPC notification behavior. */
1842 jermar 101
void z8530_release(void)
1474 palkovsky 102
{
1944 jermar 103
    ipl_t ipl = interrupts_disable();
104
    spinlock_lock(&z8530_irq.lock);
1932 jermar 105
    if (z8530_irq.notif_cfg.answerbox)
106
        z8530_irq.notif_cfg.notify = true;
1944 jermar 107
    spinlock_unlock(&z8530_irq.lock);
108
    interrupts_restore(ipl);
1474 palkovsky 109
}
670 vana 110
 
1842 jermar 111
/** Initialize z8530. */
3655 jermar 112
void
113
z8530_init(devno_t devno, uintptr_t vaddr, inr_t inr, cir_t cir, void *cir_arg)
1474 palkovsky 114
{
1842 jermar 115
    chardev_initialize("z8530_kbd", &kbrd, &ops);
511 jermar 116
    stdin = &kbrd;
1195 jermar 117
 
1921 jermar 118
    z8530.devno = devno;
119
    z8530.reg = (uint8_t *) vaddr;
120
 
121
    irq_initialize(&z8530_irq);
122
    z8530_irq.devno = devno;
123
    z8530_irq.inr = inr;
124
    z8530_irq.claim = z8530_claim;
125
    z8530_irq.handler = z8530_irq_handler;
3655 jermar 126
    z8530_irq.cir = cir;
127
    z8530_irq.cir_arg = cir_arg;
1921 jermar 128
    irq_register(&z8530_irq);
129
 
1875 jermar 130
    sysinfo_set_item_val("kbd", NULL, true);
1931 jermar 131
    sysinfo_set_item_val("kbd.type", NULL, KBD_Z8530);
1921 jermar 132
    sysinfo_set_item_val("kbd.devno", NULL, devno);
133
    sysinfo_set_item_val("kbd.inr", NULL, inr);
134
    sysinfo_set_item_val("kbd.address.virtual", NULL, vaddr);
1875 jermar 135
 
1925 jermar 136
    z8530_grab();
1 jermar 137
}
138
 
1842 jermar 139
/** Process z8530 interrupt.
508 jermar 140
 *
141
 * @param n Interrupt vector.
1708 jermar 142
 * @param istate Interrupted state.
508 jermar 143
 */
1849 jermar 144
void z8530_interrupt(void)
1 jermar 145
{
1849 jermar 146
    z8530_poll();
1 jermar 147
}
508 jermar 148
 
511 jermar 149
/* Called from getc(). */
1842 jermar 150
void z8530_resume(chardev_t *d)
511 jermar 151
{
152
}
153
 
154
/* Called from getc(). */
1842 jermar 155
void z8530_suspend(chardev_t *d)
511 jermar 156
{
157
}
878 vana 158
 
1896 jermar 159
char z8530_key_read(chardev_t *d)
878 vana 160
{
161
    char ch;   
162
 
894 jermar 163
    while(!(ch = active_read_buff_read())) {
1780 jermar 164
        uint8_t x;
1921 jermar 165
        while (!(z8530_read_a(&z8530, RR0) & RR0_RCA))
894 jermar 166
            ;
1921 jermar 167
        x = z8530_read_a(&z8530, RR8);
895 jermar 168
        if (x != IGNORE_CODE) {
169
            if (x & KEY_RELEASE)
170
                key_released(x ^ KEY_RELEASE);
171
            else
172
                active_read_key_pressed(x);
173
        }
878 vana 174
    }
175
    return ch;
176
}
895 jermar 177
 
178
/** Poll for key press and release events.
179
 *
180
 * This function can be used to implement keyboard polling.
181
 */
1842 jermar 182
void z8530_poll(void)
895 jermar 183
{
1780 jermar 184
    uint8_t x;
895 jermar 185
 
1921 jermar 186
    while (z8530_read_a(&z8530, RR0) & RR0_RCA) {
187
        x = z8530_read_a(&z8530, RR8);
895 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
}
1702 cejka 196
 
1919 jermar 197
irq_ownership_t z8530_claim(void)
198
{
1921 jermar 199
    return (z8530_read_a(&z8530, RR0) & RR0_RCA);
1919 jermar 200
}
201
 
202
void z8530_irq_handler(irq_t *irq, void *arg, ...)
203
{
1932 jermar 204
    if (irq->notif_cfg.notify && irq->notif_cfg.answerbox)
1923 jermar 205
        ipc_irq_send_notif(irq);
206
    else
1919 jermar 207
        z8530_interrupt();
208
}
209
 
1754 jermar 210
/** @}
1702 cejka 211
 */