[8b4be29] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2001-2006 Jakub Jermar
|
---|
[8b4be29] | 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>
|
---|
[a2a5529] | 41 | #ifndef ia64
|
---|
[8513ad7] | 42 | #include <arch/drivers/kbd.h>
|
---|
[8b4be29] | 43 | #include <arch/drivers/ns16550.h>
|
---|
[a2a5529] | 44 | #endif
|
---|
[7dcf22a] | 45 | #include <ddi/irq.h>
|
---|
[8513ad7] | 46 | #include <ipc/irq.h>
|
---|
[8b4be29] | 47 | #include <cpu.h>
|
---|
| 48 | #include <arch/asm.h>
|
---|
| 49 | #include <arch.h>
|
---|
| 50 | #include <console/chardev.h>
|
---|
| 51 | #include <console/console.h>
|
---|
| 52 | #include <interrupt.h>
|
---|
[8513ad7] | 53 | #include <arch/interrupt.h>
|
---|
[7bb6b06] | 54 | #include <sysinfo/sysinfo.h>
|
---|
[8513ad7] | 55 | #include <synch/spinlock.h>
|
---|
[8b4be29] | 56 |
|
---|
| 57 | #define LSR_DATA_READY 0x01
|
---|
| 58 |
|
---|
[63530c62] | 59 | /** Structure representing the ns16550. */
|
---|
| 60 | static ns16550_t ns16550;
|
---|
| 61 |
|
---|
| 62 | /** Structure for ns16550's IRQ. */
|
---|
| 63 | static irq_t ns16550_irq;
|
---|
| 64 |
|
---|
[8b4be29] | 65 | /*
|
---|
| 66 | * These codes read from ns16550 data register are silently ignored.
|
---|
| 67 | */
|
---|
| 68 | #define IGNORE_CODE 0x7f /* all keys up */
|
---|
| 69 |
|
---|
| 70 | static void ns16550_suspend(chardev_t *);
|
---|
| 71 | static void ns16550_resume(chardev_t *);
|
---|
| 72 |
|
---|
| 73 | static chardev_operations_t ops = {
|
---|
| 74 | .suspend = ns16550_suspend,
|
---|
| 75 | .resume = ns16550_resume,
|
---|
[28ecadb] | 76 | .read = ns16550_key_read
|
---|
[8b4be29] | 77 | };
|
---|
| 78 |
|
---|
[8513ad7] | 79 | void ns16550_interrupt(void);
|
---|
[8b4be29] | 80 |
|
---|
| 81 | /** Initialize keyboard and service interrupts using kernel routine */
|
---|
| 82 | void ns16550_grab(void)
|
---|
| 83 | {
|
---|
[3dea17f] | 84 | ipl_t ipl = interrupts_disable();
|
---|
| 85 |
|
---|
[8513ad7] | 86 | ns16550_ier_write(&ns16550, IER_ERBFI); /* enable receiver interrupt */
|
---|
| 87 |
|
---|
| 88 | while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY)
|
---|
| 89 | (void) ns16550_rbr_read(&ns16550);
|
---|
| 90 |
|
---|
[3dea17f] | 91 | spinlock_lock(&ns16550_irq.lock);
|
---|
[4874c2d] | 92 | ns16550_irq.notif_cfg.notify = false;
|
---|
[3dea17f] | 93 | spinlock_unlock(&ns16550_irq.lock);
|
---|
| 94 | interrupts_restore(ipl);
|
---|
[8b4be29] | 95 | }
|
---|
| 96 |
|
---|
| 97 | /** Resume the former interrupt vector */
|
---|
| 98 | void ns16550_release(void)
|
---|
| 99 | {
|
---|
[3dea17f] | 100 | ipl_t ipl = interrupts_disable();
|
---|
| 101 | spinlock_lock(&ns16550_irq.lock);
|
---|
[4874c2d] | 102 | if (ns16550_irq.notif_cfg.answerbox)
|
---|
| 103 | ns16550_irq.notif_cfg.notify = true;
|
---|
[3dea17f] | 104 | spinlock_unlock(&ns16550_irq.lock);
|
---|
| 105 | interrupts_restore(ipl);
|
---|
[8b4be29] | 106 | }
|
---|
| 107 |
|
---|
[63530c62] | 108 | /** Initialize ns16550.
|
---|
| 109 | *
|
---|
| 110 | * @param devno Device number.
|
---|
| 111 | * @param inr Interrupt number.
|
---|
| 112 | * @param vaddr Virtual address of device's registers.
|
---|
| 113 | */
|
---|
[a2a5529] | 114 | void ns16550_init(devno_t devno, inr_t inr, ioport_t port)
|
---|
[8b4be29] | 115 | {
|
---|
| 116 | chardev_initialize("ns16550_kbd", &kbrd, &ops);
|
---|
| 117 | stdin = &kbrd;
|
---|
[7bb6b06] | 118 |
|
---|
[63530c62] | 119 | ns16550.devno = devno;
|
---|
[a2a5529] | 120 | ns16550.io_port = port;
|
---|
[63530c62] | 121 |
|
---|
| 122 | irq_initialize(&ns16550_irq);
|
---|
| 123 | ns16550_irq.devno = devno;
|
---|
| 124 | ns16550_irq.inr = inr;
|
---|
| 125 | ns16550_irq.claim = ns16550_claim;
|
---|
| 126 | ns16550_irq.handler = ns16550_irq_handler;
|
---|
| 127 | irq_register(&ns16550_irq);
|
---|
| 128 |
|
---|
[7bb6b06] | 129 | sysinfo_set_item_val("kbd", NULL, true);
|
---|
[a2a5529] | 130 | #ifndef ia64
|
---|
[8513ad7] | 131 | sysinfo_set_item_val("kbd.type", NULL, KBD_NS16550);
|
---|
[a2a5529] | 132 | #endif
|
---|
[63530c62] | 133 | sysinfo_set_item_val("kbd.devno", NULL, devno);
|
---|
| 134 | sysinfo_set_item_val("kbd.inr", NULL, inr);
|
---|
[a2a5529] | 135 | sysinfo_set_item_val("kbd.address.virtual", NULL, port);
|
---|
| 136 |
|
---|
| 137 | #ifdef ia64
|
---|
| 138 | uint8_t c;
|
---|
| 139 | c=ns16550_lcr_read(&ns16550);
|
---|
| 140 | ns16550_lcr_write(&ns16550,0x80|c);
|
---|
| 141 | ns16550_rbr_write(&ns16550,0x0c);
|
---|
| 142 | ns16550_ier_write(&ns16550,0x00);
|
---|
| 143 | ns16550_lcr_write(&ns16550,c);
|
---|
| 144 | #endif
|
---|
[e2cc9a0] | 145 |
|
---|
[8513ad7] | 146 | ns16550_grab();
|
---|
[8b4be29] | 147 | }
|
---|
| 148 |
|
---|
[8513ad7] | 149 | /** Process ns16550 interrupt. */
|
---|
| 150 | void ns16550_interrupt(void)
|
---|
[06e1e95] | 151 | {
|
---|
[8513ad7] | 152 | /* TODO
|
---|
| 153 | *
|
---|
| 154 | * ns16550 works in the polled mode so far.
|
---|
| 155 | */
|
---|
[8b4be29] | 156 | }
|
---|
| 157 |
|
---|
| 158 | /* Called from getc(). */
|
---|
| 159 | void ns16550_resume(chardev_t *d)
|
---|
| 160 | {
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /* Called from getc(). */
|
---|
| 164 | void ns16550_suspend(chardev_t *d)
|
---|
| 165 | {
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[a2a5529] | 168 |
|
---|
[28ecadb] | 169 | char ns16550_key_read(chardev_t *d)
|
---|
[8b4be29] | 170 | {
|
---|
| 171 | char ch;
|
---|
| 172 |
|
---|
| 173 | while(!(ch = active_read_buff_read())) {
|
---|
| 174 | uint8_t x;
|
---|
[63530c62] | 175 | while (!(ns16550_lsr_read(&ns16550) & LSR_DATA_READY))
|
---|
[8b4be29] | 176 | ;
|
---|
[63530c62] | 177 | x = ns16550_rbr_read(&ns16550);
|
---|
[a2a5529] | 178 | #ifndef ia64
|
---|
[8b4be29] | 179 | if (x != IGNORE_CODE) {
|
---|
| 180 | if (x & KEY_RELEASE)
|
---|
| 181 | key_released(x ^ KEY_RELEASE);
|
---|
| 182 | else
|
---|
| 183 | active_read_key_pressed(x);
|
---|
| 184 | }
|
---|
[a2a5529] | 185 | #else
|
---|
| 186 | extern chardev_t kbrd;
|
---|
| 187 | if(x!=0x0d)
|
---|
| 188 | {
|
---|
| 189 | if(x==0x7f) x='\b';
|
---|
| 190 | chardev_push_character(&kbrd,x);
|
---|
| 191 | }
|
---|
| 192 | #endif
|
---|
| 193 |
|
---|
[8b4be29] | 194 | }
|
---|
| 195 | return ch;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | /** Poll for key press and release events.
|
---|
| 199 | *
|
---|
| 200 | * This function can be used to implement keyboard polling.
|
---|
| 201 | */
|
---|
| 202 | void ns16550_poll(void)
|
---|
| 203 | {
|
---|
[8513ad7] | 204 | ipl_t ipl;
|
---|
| 205 |
|
---|
| 206 | ipl = interrupts_disable();
|
---|
| 207 | spinlock_lock(&ns16550_irq.lock);
|
---|
| 208 |
|
---|
| 209 | if (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) {
|
---|
[4874c2d] | 210 | if (ns16550_irq.notif_cfg.notify && ns16550_irq.notif_cfg.answerbox) {
|
---|
[8513ad7] | 211 | /*
|
---|
| 212 | * Send IPC notification.
|
---|
| 213 | */
|
---|
| 214 | ipc_irq_send_notif(&ns16550_irq);
|
---|
| 215 | spinlock_unlock(&ns16550_irq.lock);
|
---|
| 216 | interrupts_restore(ipl);
|
---|
| 217 | return;
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | spinlock_unlock(&ns16550_irq.lock);
|
---|
| 222 | interrupts_restore(ipl);
|
---|
[8b4be29] | 223 |
|
---|
[63530c62] | 224 | while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) {
|
---|
[8513ad7] | 225 | uint8_t x;
|
---|
| 226 |
|
---|
[63530c62] | 227 | x = ns16550_rbr_read(&ns16550);
|
---|
[a2a5529] | 228 | #ifndef ia64
|
---|
[8b4be29] | 229 | if (x != IGNORE_CODE) {
|
---|
| 230 | if (x & KEY_RELEASE)
|
---|
| 231 | key_released(x ^ KEY_RELEASE);
|
---|
| 232 | else
|
---|
| 233 | key_pressed(x);
|
---|
| 234 | }
|
---|
[a2a5529] | 235 | #else
|
---|
| 236 | extern chardev_t kbrd;
|
---|
| 237 | if(x!=0x0d)
|
---|
| 238 | {
|
---|
| 239 | if(x==0x7f) x='\b';
|
---|
| 240 | chardev_push_character(&kbrd,x);
|
---|
| 241 | }
|
---|
| 242 | #endif
|
---|
| 243 |
|
---|
[8b4be29] | 244 | }
|
---|
| 245 | }
|
---|
| 246 |
|
---|
[0d107f31] | 247 | irq_ownership_t ns16550_claim(void)
|
---|
| 248 | {
|
---|
[8513ad7] | 249 | return (ns16550_lsr_read(&ns16550) & LSR_DATA_READY);
|
---|
[0d107f31] | 250 | }
|
---|
| 251 |
|
---|
| 252 | void ns16550_irq_handler(irq_t *irq, void *arg, ...)
|
---|
| 253 | {
|
---|
[8513ad7] | 254 | panic("Not yet implemented, ns16550 works in polled mode.\n");
|
---|
[0d107f31] | 255 | }
|
---|
| 256 |
|
---|
[8b4be29] | 257 | /** @}
|
---|
| 258 | */
|
---|