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