Rev 3906 | Rev 3938 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1844 | jermar | 1 | /* |
2071 | jermar | 2 | * Copyright (c) 2001-2006 Jakub Jermar |
1844 | 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 | |||
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> |
||
3657 | vana | 41 | #include <arch/drivers/kbd.h> |
1920 | jermar | 42 | #include <ddi/irq.h> |
1931 | jermar | 43 | #include <ipc/irq.h> |
1844 | jermar | 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> |
||
1931 | jermar | 50 | #include <arch/interrupt.h> |
1880 | jermar | 51 | #include <sysinfo/sysinfo.h> |
1931 | jermar | 52 | #include <synch/spinlock.h> |
3934 | jermar | 53 | #include <mm/slab.h> |
1844 | jermar | 54 | |
55 | #define LSR_DATA_READY 0x01 |
||
56 | |||
3934 | jermar | 57 | static irq_t *ns16550_irq; |
1921 | jermar | 58 | |
1844 | jermar | 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 | { |
||
1944 | jermar | 75 | ipl_t ipl = interrupts_disable(); |
3934 | jermar | 76 | spinlock_lock(&ns16550_irq->lock); |
77 | ns16550_irq->notif_cfg.notify = false; |
||
78 | spinlock_unlock(&ns16550_irq->lock); |
||
1944 | jermar | 79 | interrupts_restore(ipl); |
1844 | jermar | 80 | } |
81 | |||
82 | /** Resume the former interrupt vector */ |
||
83 | void ns16550_release(void) |
||
84 | { |
||
1944 | jermar | 85 | ipl_t ipl = interrupts_disable(); |
3934 | jermar | 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); |
||
1944 | jermar | 90 | interrupts_restore(ipl); |
1844 | jermar | 91 | } |
92 | |||
1921 | jermar | 93 | /** Initialize ns16550. |
94 | * |
||
3934 | jermar | 95 | * @param dev Addrress of the beginning of the device in I/O space. |
3655 | jermar | 96 | * @param devno Device number. |
97 | * @param inr Interrupt number. |
||
98 | * @param cir Clear interrupt function. |
||
99 | * @param cir_arg First argument to cir. |
||
3934 | jermar | 100 | * |
101 | * @return True on success, false on failure. |
||
1921 | jermar | 102 | */ |
3934 | jermar | 103 | bool |
104 | ns16550_init(ns16550_t *dev, devno_t devno, inr_t inr, cir_t cir, void *cir_arg) |
||
1844 | jermar | 105 | { |
3934 | jermar | 106 | ns16550_instance_t *instance; |
107 | irq_t *irq; |
||
108 | |||
1844 | jermar | 109 | chardev_initialize("ns16550_kbd", &kbrd, &ops); |
110 | stdin = &kbrd; |
||
1880 | jermar | 111 | |
3934 | jermar | 112 | instance = malloc(sizeof(ns16550_instance_t), FRAME_ATOMIC); |
113 | if (!instance) |
||
114 | return false; |
||
115 | |||
116 | irq = malloc(sizeof(irq_t), FRAME_ATOMIC); |
||
117 | if (!irq) { |
||
118 | free(instance); |
||
119 | return false; |
||
120 | } |
||
121 | |||
122 | instance->devno = devno; |
||
123 | instance->ns16550 = dev; |
||
124 | instance->irq = irq; |
||
1921 | jermar | 125 | |
3934 | jermar | 126 | irq_initialize(irq); |
127 | irq->devno = devno; |
||
128 | irq->inr = inr; |
||
129 | irq->claim = ns16550_claim; |
||
130 | irq->handler = ns16550_irq_handler; |
||
131 | irq->instance = instance; |
||
132 | irq->cir = cir; |
||
133 | irq->cir_arg = cir_arg; |
||
134 | irq_register(irq); |
||
135 | |||
136 | ns16550_irq = irq; /* TODO: remove me soon */ |
||
3877 | decky | 137 | |
3934 | jermar | 138 | while ((pio_read_8(&dev->lsr) & LSR_DATA_READY)) |
139 | (void) pio_read_8(&dev->rbr); |
||
1921 | jermar | 140 | |
1880 | jermar | 141 | sysinfo_set_item_val("kbd", NULL, true); |
1931 | jermar | 142 | sysinfo_set_item_val("kbd.type", NULL, KBD_NS16550); |
1921 | jermar | 143 | sysinfo_set_item_val("kbd.devno", NULL, devno); |
144 | sysinfo_set_item_val("kbd.inr", NULL, inr); |
||
3934 | jermar | 145 | sysinfo_set_item_val("kbd.address.virtual", NULL, (uintptr_t) dev); |
146 | sysinfo_set_item_val("kbd.port", NULL, (uintptr_t) dev); |
||
3877 | decky | 147 | |
3646 | jermar | 148 | /* Enable interrupts */ |
3934 | jermar | 149 | pio_write_8(&dev->ier, IER_ERBFI); |
150 | pio_write_8(&dev->mcr, MCR_OUT2); |
||
1911 | jermar | 151 | |
3934 | jermar | 152 | ns16550_grab(); |
3877 | decky | 153 | |
3934 | jermar | 154 | return true; |
1844 | jermar | 155 | } |
156 | |||
157 | /* Called from getc(). */ |
||
158 | void ns16550_resume(chardev_t *d) |
||
159 | { |
||
160 | } |
||
161 | |||
162 | /* Called from getc(). */ |
||
163 | void ns16550_suspend(chardev_t *d) |
||
164 | { |
||
165 | } |
||
166 | |||
3934 | jermar | 167 | irq_ownership_t ns16550_claim(void *instance) |
1844 | jermar | 168 | { |
3934 | jermar | 169 | ns16550_instance_t *ns16550_instance = instance; |
170 | ns16550_t *dev = ns16550_instance->ns16550; |
||
1844 | jermar | 171 | |
3934 | jermar | 172 | if (pio_read_8(&dev->lsr) & LSR_DATA_READY) |
173 | return IRQ_ACCEPT; |
||
174 | else |
||
175 | return IRQ_DECLINE; |
||
1844 | jermar | 176 | } |
177 | |||
3934 | jermar | 178 | void ns16550_irq_handler(irq_t *irq) |
1844 | jermar | 179 | { |
3934 | jermar | 180 | if (irq->notif_cfg.notify && irq->notif_cfg.answerbox) { |
181 | /* |
||
182 | * This will hopefully go to the IRQ dispatch code soon. |
||
183 | */ |
||
184 | ipc_irq_send_notif(irq); |
||
185 | return; |
||
186 | } |
||
187 | |||
188 | ns16550_instance_t *ns16550_instance = irq->instance; |
||
189 | ns16550_t *dev = ns16550_instance->ns16550; |
||
190 | |||
191 | if (pio_read_8(&dev->lsr) & LSR_DATA_READY) { |
||
1931 | jermar | 192 | uint8_t x; |
193 | |||
3934 | jermar | 194 | x = pio_read_8(&dev->rbr); |
3877 | decky | 195 | |
1844 | jermar | 196 | if (x != IGNORE_CODE) { |
197 | if (x & KEY_RELEASE) |
||
198 | key_released(x ^ KEY_RELEASE); |
||
199 | else |
||
200 | key_pressed(x); |
||
201 | } |
||
202 | } |
||
203 | |||
1919 | jermar | 204 | } |
205 | |||
1844 | jermar | 206 | /** @} |
207 | */ |