Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
78 jermar 1
/*
2071 jermar 2
 * Copyright (c) 2005 Jakub Jermar
78 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
 
1888 jermar 29
/** @addtogroup ia64   
1702 cejka 30
 * @{
31
 */
32
/** @file
33
 */
34
 
78 jermar 35
#include <arch/ski/ski.h>
577 palkovsky 36
#include <console/console.h>
37
#include <console/chardev.h>
1507 vana 38
#include <arch/interrupt.h>
39
#include <sysinfo/sysinfo.h>
1942 jermar 40
#include <arch/types.h>
41
#include <ddi/device.h>
42
#include <ddi/irq.h>
43
#include <ipc/irq.h>
1945 jermar 44
#include <proc/thread.h>
1942 jermar 45
#include <synch/spinlock.h>
46
#include <arch/asm.h>
78 jermar 47
 
1942 jermar 48
#define SKI_KBD_INR 0
49
 
50
static irq_t ski_kbd_irq;
51
static devno_t ski_kbd_devno;
52
 
1507 vana 53
chardev_t ski_console;
54
chardev_t ski_uconsole;
78 jermar 55
 
1942 jermar 56
static bool kbd_disabled;
57
 
586 jermar 58
static void ski_putchar(chardev_t *d, const char ch);
1780 jermar 59
static int32_t ski_getchar(void);
583 jermar 60
 
79 jermar 61
/** Display character on debug console
62
 *
63
 * Use SSC (Simulator System Call) to
64
 * display character on debug console.
65
 *
583 jermar 66
 * @param d Character device.
67
 * @param ch Character to be printed.
79 jermar 68
 */
586 jermar 69
void ski_putchar(chardev_t *d, const char ch)
78 jermar 70
{
2082 decky 71
    asm volatile (
1942 jermar 72
        "mov r15 = %0\n"
73
        "mov r32 = %1\n"    /* r32 is in0 */
78 jermar 74
        "break 0x80000\n"   /* modifies r8 */
75
        :
76
        : "i" (SKI_PUTCHAR), "r" (ch)
77
        : "r15", "in0", "r8"
78
    );
79
 
583 jermar 80
    if (ch == '\n')
586 jermar 81
        ski_putchar(d, '\r');
78 jermar 82
}
519 vana 83
 
523 jermar 84
/** Ask debug console if a key was pressed.
519 vana 85
 *
86
 * Use SSC (Simulator System Call) to
87
 * get character from debug console.
523 jermar 88
 *
89
 * This call is non-blocking.
90
 *
91
 * @return ASCII code of pressed key or 0 if no key pressed.
519 vana 92
 */
1780 jermar 93
int32_t ski_getchar(void)
519 vana 94
{
1780 jermar 95
    uint64_t ch;
519 vana 96
 
2082 decky 97
    asm volatile (
1942 jermar 98
        "mov r15 = %1\n"
519 vana 99
        "break 0x80000;;\n" /* modifies r8 */
1942 jermar 100
        "mov %0 = r8;;\n"      
519 vana 101
 
584 jermar 102
        : "=r" (ch)
103
        : "i" (SKI_GETCHAR)
1942 jermar 104
        : "r15", "r8"
519 vana 105
    );
106
 
1780 jermar 107
    return (int32_t) ch;
519 vana 108
}
577 palkovsky 109
 
892 vana 110
/**
893 jermar 111
 * This is a blocking wrapper for ski_getchar().
112
 * To be used when the kernel crashes.
113
 */
892 vana 114
static char ski_getchar_blocking(chardev_t *d)
115
{
893 jermar 116
    int ch;
117
 
1942 jermar 118
    while(!(ch = ski_getchar()))
893 jermar 119
        ;
120
    if(ch == '\r')
121
        ch = '\n';
892 vana 122
    return (char) ch;
123
}
124
 
577 palkovsky 125
/** Ask keyboard if a key was pressed. */
1945 jermar 126
static void poll_keyboard(void)
577 palkovsky 127
{
128
    char ch;
1570 vana 129
    static char last;
1942 jermar 130
    ipl_t ipl;
577 palkovsky 131
 
1942 jermar 132
    ipl = interrupts_disable();
133
 
134
    if (kbd_disabled) {
135
        interrupts_restore(ipl);
577 palkovsky 136
        return;
1942 jermar 137
    }
138
 
139
    spinlock_lock(&ski_kbd_irq.lock);
577 palkovsky 140
 
141
    ch = ski_getchar();
142
    if(ch == '\r')
143
        ch = '\n';
1942 jermar 144
    if (ch) {
145
        if (ski_kbd_irq.notif_cfg.notify && ski_kbd_irq.notif_cfg.answerbox) {
1507 vana 146
            chardev_push_character(&ski_uconsole, ch);
1942 jermar 147
            ipc_irq_send_notif(&ski_kbd_irq);
148
        } else {
1507 vana 149
            chardev_push_character(&ski_console, ch);
1570 vana 150
        }  
1942 jermar 151
        last = ch;
152
        spinlock_unlock(&ski_kbd_irq.lock);
153
        interrupts_restore(ipl);
1570 vana 154
        return;
1942 jermar 155
    }
1507 vana 156
 
1942 jermar 157
    if (last) {
158
        if (ski_kbd_irq.notif_cfg.notify && ski_kbd_irq.notif_cfg.answerbox) {
1570 vana 159
            chardev_push_character(&ski_uconsole, 0);
1942 jermar 160
            ipc_irq_send_notif(&ski_kbd_irq);
1570 vana 161
        }
1942 jermar 162
        last = 0;
163
    }
1570 vana 164
 
1942 jermar 165
    spinlock_unlock(&ski_kbd_irq.lock);
166
    interrupts_restore(ipl);
577 palkovsky 167
}
168
 
169
/* Called from getc(). */
1942 jermar 170
static void ski_kbd_enable(chardev_t *d)
577 palkovsky 171
{
1942 jermar 172
    kbd_disabled = false;
577 palkovsky 173
}
174
 
175
/* Called from getc(). */
1942 jermar 176
static void ski_kbd_disable(chardev_t *d)
577 palkovsky 177
{
1942 jermar 178
    kbd_disabled = true;   
577 palkovsky 179
}
180
 
1942 jermar 181
/** Decline to service hardware IRQ.
182
 *
183
 * This is only a virtual IRQ, so always decline.
184
 *
185
 * @return Always IRQ_DECLINE.
186
 */
187
static irq_ownership_t ski_kbd_claim(void)
188
{
189
    return IRQ_DECLINE;
190
}
191
 
577 palkovsky 192
static chardev_operations_t ski_ops = {
1942 jermar 193
    .resume = ski_kbd_enable,
194
    .suspend = ski_kbd_disable,
892 vana 195
    .write = ski_putchar,
196
    .read = ski_getchar_blocking
577 palkovsky 197
};
198
 
199
/** Initialize debug console
200
 *
201
 * Issue SSC (Simulator System Call) to
202
 * to open debug console.
203
 */
204
void ski_init_console(void)
205
{
2082 decky 206
    asm volatile (
1942 jermar 207
        "mov r15 = %0\n"
577 palkovsky 208
        "break 0x80000\n"
209
        :
210
        : "i" (SKI_INIT_CONSOLE)
211
        : "r15", "r8"
212
    );
213
 
214
    chardev_initialize("ski_console", &ski_console, &ski_ops);
1507 vana 215
    chardev_initialize("ski_uconsole", &ski_uconsole, &ski_ops);
577 palkovsky 216
    stdin = &ski_console;
217
    stdout = &ski_console;
1507 vana 218
 
1942 jermar 219
    ski_kbd_devno = device_assign_devno();
220
 
221
    irq_initialize(&ski_kbd_irq);
222
    ski_kbd_irq.inr = SKI_KBD_INR;
223
    ski_kbd_irq.devno = ski_kbd_devno;
224
    ski_kbd_irq.claim = ski_kbd_claim;
225
    irq_register(&ski_kbd_irq);
226
 
227
    sysinfo_set_item_val("kbd", NULL, true);
228
    sysinfo_set_item_val("kbd.inr", NULL, SKI_KBD_INR);
229
    sysinfo_set_item_val("kbd.devno", NULL, ski_kbd_devno);
1507 vana 230
}
1702 cejka 231
 
1942 jermar 232
void ski_kbd_grab(void)
233
{
1944 jermar 234
    ipl_t ipl = interrupts_disable();
235
    spinlock_lock(&ski_kbd_irq.lock);
1942 jermar 236
    ski_kbd_irq.notif_cfg.notify = false;
1944 jermar 237
    spinlock_unlock(&ski_kbd_irq.lock);
238
    interrupts_restore(ipl);
1942 jermar 239
}
240
 
241
void ski_kbd_release(void)
242
{
1944 jermar 243
    ipl_t ipl = interrupts_disable();
244
    spinlock_lock(&ski_kbd_irq.lock);
1942 jermar 245
    if (ski_kbd_irq.notif_cfg.answerbox)
246
        ski_kbd_irq.notif_cfg.notify = true;
1944 jermar 247
    spinlock_unlock(&ski_kbd_irq.lock);
248
    interrupts_restore(ipl);
1942 jermar 249
}
250
 
1945 jermar 251
 
252
#define POLL_INTERVAL       50000       /* 50 ms */
253
 
254
/** Kernel thread for polling keyboard. */
255
void kkbdpoll(void *arg)
256
{
257
    while (1) {
258
        poll_keyboard();
259
        thread_usleep(POLL_INTERVAL);
260
    }
261
}
262
 
1888 jermar 263
/** @}
1702 cejka 264
 */