[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>
|
---|
[8513ad7] | 41 | #include <arch/drivers/kbd.h>
|
---|
[7dcf22a] | 42 | #include <ddi/irq.h>
|
---|
[8513ad7] | 43 | #include <ipc/irq.h>
|
---|
[8b4be29] | 44 | #include <cpu.h>
|
---|
| 45 | #include <arch/asm.h>
|
---|
| 46 | #include <arch.h>
|
---|
| 47 | #include <console/chardev.h>
|
---|
| 48 | #include <console/console.h>
|
---|
| 49 | #include <interrupt.h>
|
---|
[8513ad7] | 50 | #include <arch/interrupt.h>
|
---|
[7bb6b06] | 51 | #include <sysinfo/sysinfo.h>
|
---|
[8513ad7] | 52 | #include <synch/spinlock.h>
|
---|
[013c4d6] | 53 | #include <mm/slab.h>
|
---|
[8b4be29] | 54 |
|
---|
| 55 | #define LSR_DATA_READY 0x01
|
---|
| 56 |
|
---|
[013c4d6] | 57 | static irq_t *ns16550_irq;
|
---|
[63530c62] | 58 |
|
---|
[8b4be29] | 59 | /*
|
---|
| 60 | * These codes read from ns16550 data register are silently ignored.
|
---|
| 61 | */
|
---|
| 62 | #define IGNORE_CODE 0x7f /* all keys up */
|
---|
| 63 |
|
---|
| 64 | static void ns16550_suspend(chardev_t *);
|
---|
| 65 | static void ns16550_resume(chardev_t *);
|
---|
| 66 |
|
---|
| 67 | static chardev_operations_t ops = {
|
---|
| 68 | .suspend = ns16550_suspend,
|
---|
| 69 | .resume = ns16550_resume,
|
---|
| 70 | };
|
---|
| 71 |
|
---|
| 72 | /** Initialize keyboard and service interrupts using kernel routine */
|
---|
| 73 | void ns16550_grab(void)
|
---|
| 74 | {
|
---|
[3dea17f] | 75 | ipl_t ipl = interrupts_disable();
|
---|
[013c4d6] | 76 | spinlock_lock(&ns16550_irq->lock);
|
---|
| 77 | ns16550_irq->notif_cfg.notify = false;
|
---|
| 78 | spinlock_unlock(&ns16550_irq->lock);
|
---|
[3dea17f] | 79 | interrupts_restore(ipl);
|
---|
[8b4be29] | 80 | }
|
---|
| 81 |
|
---|
| 82 | /** Resume the former interrupt vector */
|
---|
| 83 | void ns16550_release(void)
|
---|
| 84 | {
|
---|
[3dea17f] | 85 | ipl_t ipl = interrupts_disable();
|
---|
[013c4d6] | 86 | spinlock_lock(&ns16550_irq->lock);
|
---|
| 87 | if (ns16550_irq->notif_cfg.answerbox)
|
---|
| 88 | ns16550_irq->notif_cfg.notify = true;
|
---|
| 89 | spinlock_unlock(&ns16550_irq->lock);
|
---|
[3dea17f] | 90 | interrupts_restore(ipl);
|
---|
[8b4be29] | 91 | }
|
---|
| 92 |
|
---|
[63530c62] | 93 | /** Initialize ns16550.
|
---|
| 94 | *
|
---|
[013c4d6] | 95 | * @param dev Addrress of the beginning of the device in I/O space.
|
---|
[8d2760f] | 96 | * @param devno Device number.
|
---|
| 97 | * @param inr Interrupt number.
|
---|
| 98 | * @param cir Clear interrupt function.
|
---|
| 99 | * @param cir_arg First argument to cir.
|
---|
[013c4d6] | 100 | *
|
---|
| 101 | * @return True on success, false on failure.
|
---|
[63530c62] | 102 | */
|
---|
[013c4d6] | 103 | bool
|
---|
| 104 | ns16550_init(ns16550_t *dev, devno_t devno, inr_t inr, cir_t cir, void *cir_arg)
|
---|
[8b4be29] | 105 | {
|
---|
[013c4d6] | 106 | ns16550_instance_t *instance;
|
---|
| 107 |
|
---|
[8b4be29] | 108 | chardev_initialize("ns16550_kbd", &kbrd, &ops);
|
---|
| 109 | stdin = &kbrd;
|
---|
[7bb6b06] | 110 |
|
---|
[013c4d6] | 111 | instance = malloc(sizeof(ns16550_instance_t), FRAME_ATOMIC);
|
---|
| 112 | if (!instance)
|
---|
| 113 | return false;
|
---|
| 114 |
|
---|
| 115 | instance->devno = devno;
|
---|
| 116 | instance->ns16550 = dev;
|
---|
[63530c62] | 117 |
|
---|
[dc22844] | 118 | irq_initialize(&instance->irq);
|
---|
| 119 | instance->irq.devno = devno;
|
---|
| 120 | instance->irq.inr = inr;
|
---|
| 121 | instance->irq.claim = ns16550_claim;
|
---|
| 122 | instance->irq.handler = ns16550_irq_handler;
|
---|
| 123 | instance->irq.instance = instance;
|
---|
| 124 | instance->irq.cir = cir;
|
---|
| 125 | instance->irq.cir_arg = cir_arg;
|
---|
| 126 | irq_register(&instance->irq);
|
---|
[013c4d6] | 127 |
|
---|
[dc22844] | 128 | ns16550_irq = &instance->irq; /* TODO: remove me soon */
|
---|
[af75db9] | 129 |
|
---|
[dc22844] | 130 | while ((pio_read_8(&dev->lsr) & LSR_DATA_READY))
|
---|
[013c4d6] | 131 | (void) pio_read_8(&dev->rbr);
|
---|
[63530c62] | 132 |
|
---|
[7bb6b06] | 133 | sysinfo_set_item_val("kbd", NULL, true);
|
---|
[8513ad7] | 134 | sysinfo_set_item_val("kbd.type", NULL, KBD_NS16550);
|
---|
[63530c62] | 135 | sysinfo_set_item_val("kbd.devno", NULL, devno);
|
---|
| 136 | sysinfo_set_item_val("kbd.inr", NULL, inr);
|
---|
[013c4d6] | 137 | sysinfo_set_item_val("kbd.address.virtual", NULL, (uintptr_t) dev);
|
---|
| 138 | sysinfo_set_item_val("kbd.port", NULL, (uintptr_t) dev);
|
---|
[af75db9] | 139 |
|
---|
[3e53ab7] | 140 | /* Enable interrupts */
|
---|
[013c4d6] | 141 | pio_write_8(&dev->ier, IER_ERBFI);
|
---|
| 142 | pio_write_8(&dev->mcr, MCR_OUT2);
|
---|
[e2cc9a0] | 143 |
|
---|
[8513ad7] | 144 | ns16550_grab();
|
---|
[013c4d6] | 145 |
|
---|
| 146 | return true;
|
---|
[8b4be29] | 147 | }
|
---|
| 148 |
|
---|
| 149 | /* Called from getc(). */
|
---|
| 150 | void ns16550_resume(chardev_t *d)
|
---|
| 151 | {
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /* Called from getc(). */
|
---|
| 155 | void ns16550_suspend(chardev_t *d)
|
---|
| 156 | {
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[c9b550b] | 159 | irq_ownership_t ns16550_claim(irq_t *irq)
|
---|
[8b4be29] | 160 | {
|
---|
[c9b550b] | 161 | ns16550_instance_t *ns16550_instance = irq->instance;
|
---|
[013c4d6] | 162 | ns16550_t *dev = ns16550_instance->ns16550;
|
---|
[8b4be29] | 163 |
|
---|
[013c4d6] | 164 | if (pio_read_8(&dev->lsr) & LSR_DATA_READY)
|
---|
| 165 | return IRQ_ACCEPT;
|
---|
| 166 | else
|
---|
| 167 | return IRQ_DECLINE;
|
---|
[8b4be29] | 168 | }
|
---|
| 169 |
|
---|
[013c4d6] | 170 | void ns16550_irq_handler(irq_t *irq)
|
---|
[8b4be29] | 171 | {
|
---|
[013c4d6] | 172 | if (irq->notif_cfg.notify && irq->notif_cfg.answerbox) {
|
---|
| 173 | /*
|
---|
| 174 | * This will hopefully go to the IRQ dispatch code soon.
|
---|
| 175 | */
|
---|
| 176 | ipc_irq_send_notif(irq);
|
---|
| 177 | return;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | ns16550_instance_t *ns16550_instance = irq->instance;
|
---|
| 181 | ns16550_t *dev = ns16550_instance->ns16550;
|
---|
| 182 |
|
---|
| 183 | if (pio_read_8(&dev->lsr) & LSR_DATA_READY) {
|
---|
[8513ad7] | 184 | uint8_t x;
|
---|
| 185 |
|
---|
[013c4d6] | 186 | x = pio_read_8(&dev->rbr);
|
---|
[af75db9] | 187 |
|
---|
[8b4be29] | 188 | if (x != IGNORE_CODE) {
|
---|
| 189 | if (x & KEY_RELEASE)
|
---|
| 190 | key_released(x ^ KEY_RELEASE);
|
---|
| 191 | else
|
---|
| 192 | key_pressed(x);
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
[0d107f31] | 195 |
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[8b4be29] | 198 | /** @}
|
---|
| 199 | */
|
---|