Rev 3657 | Rev 3900 | 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> |
1844 | jermar | 53 | |
54 | #define LSR_DATA_READY 0x01 |
||
55 | |||
1921 | jermar | 56 | /** Structure representing the ns16550. */ |
57 | static ns16550_t ns16550; |
||
58 | |||
59 | /** Structure for ns16550's IRQ. */ |
||
60 | static irq_t ns16550_irq; |
||
61 | |||
1844 | jermar | 62 | /* |
63 | * These codes read from ns16550 data register are silently ignored. |
||
64 | */ |
||
65 | #define IGNORE_CODE 0x7f /* all keys up */ |
||
66 | |||
67 | static void ns16550_suspend(chardev_t *); |
||
68 | static void ns16550_resume(chardev_t *); |
||
69 | |||
70 | static chardev_operations_t ops = { |
||
71 | .suspend = ns16550_suspend, |
||
72 | .resume = ns16550_resume, |
||
1896 | jermar | 73 | .read = ns16550_key_read |
1844 | jermar | 74 | }; |
75 | |||
1931 | jermar | 76 | void ns16550_interrupt(void); |
1844 | jermar | 77 | |
78 | /** Initialize keyboard and service interrupts using kernel routine */ |
||
79 | void ns16550_grab(void) |
||
80 | { |
||
1944 | jermar | 81 | ipl_t ipl = interrupts_disable(); |
82 | |||
1931 | jermar | 83 | ns16550_ier_write(&ns16550, IER_ERBFI); /* enable receiver interrupt */ |
84 | |||
85 | while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) |
||
86 | (void) ns16550_rbr_read(&ns16550); |
||
87 | |||
1944 | jermar | 88 | spinlock_lock(&ns16550_irq.lock); |
1932 | jermar | 89 | ns16550_irq.notif_cfg.notify = false; |
1944 | jermar | 90 | spinlock_unlock(&ns16550_irq.lock); |
91 | interrupts_restore(ipl); |
||
1844 | jermar | 92 | } |
93 | |||
94 | /** Resume the former interrupt vector */ |
||
95 | void ns16550_release(void) |
||
96 | { |
||
1944 | jermar | 97 | ipl_t ipl = interrupts_disable(); |
98 | spinlock_lock(&ns16550_irq.lock); |
||
1932 | jermar | 99 | if (ns16550_irq.notif_cfg.answerbox) |
100 | ns16550_irq.notif_cfg.notify = true; |
||
1944 | jermar | 101 | spinlock_unlock(&ns16550_irq.lock); |
102 | interrupts_restore(ipl); |
||
1844 | jermar | 103 | } |
104 | |||
1921 | jermar | 105 | /** Initialize ns16550. |
106 | * |
||
3655 | jermar | 107 | * @param devno Device number. |
108 | * @param port Virtual/IO address of device's registers. |
||
109 | * @param inr Interrupt number. |
||
110 | * @param cir Clear interrupt function. |
||
111 | * @param cir_arg First argument to cir. |
||
1921 | jermar | 112 | */ |
3655 | jermar | 113 | void |
114 | ns16550_init(devno_t devno, ioport_t port, inr_t inr, cir_t cir, void *cir_arg) |
||
1844 | jermar | 115 | { |
116 | chardev_initialize("ns16550_kbd", &kbrd, &ops); |
||
117 | stdin = &kbrd; |
||
1880 | jermar | 118 | |
1921 | jermar | 119 | ns16550.devno = devno; |
3577 | vana | 120 | ns16550.io_port = port; |
1921 | jermar | 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; |
||
3655 | jermar | 127 | ns16550_irq.cir = cir; |
128 | ns16550_irq.cir_arg = cir_arg; |
||
1921 | jermar | 129 | irq_register(&ns16550_irq); |
3877 | decky | 130 | |
3657 | vana | 131 | while ((ns16550_lsr_read(&ns16550) & LSR_DATA_READY)) |
132 | ns16550_rbr_read(&ns16550); |
||
1921 | jermar | 133 | |
1880 | jermar | 134 | sysinfo_set_item_val("kbd", NULL, true); |
1931 | jermar | 135 | sysinfo_set_item_val("kbd.type", NULL, KBD_NS16550); |
1921 | jermar | 136 | sysinfo_set_item_val("kbd.devno", NULL, devno); |
137 | sysinfo_set_item_val("kbd.inr", NULL, inr); |
||
3577 | vana | 138 | sysinfo_set_item_val("kbd.address.virtual", NULL, port); |
3657 | vana | 139 | sysinfo_set_item_val("kbd.port", NULL, port); |
3877 | decky | 140 | |
3646 | jermar | 141 | #ifdef CONFIG_NS16550_INTERRUPT_DRIVEN |
142 | /* Enable interrupts */ |
||
3877 | decky | 143 | ns16550_ier_write(&ns16550, IER_ERBFI); |
3646 | jermar | 144 | ns16550_mcr_write(&ns16550, MCR_OUT2); |
145 | #endif |
||
1911 | jermar | 146 | |
3877 | decky | 147 | uint8_t c; |
148 | // This switches rbr & ier to mode when accept baudrate constant |
||
149 | c = ns16550_lcr_read(&ns16550); |
||
150 | ns16550_lcr_write(&ns16550, 0x80 | c); |
||
151 | ns16550_rbr_write(&ns16550, 0x0c); |
||
152 | ns16550_ier_write(&ns16550, 0x00); |
||
153 | ns16550_lcr_write(&ns16550, c); |
||
154 | |||
1931 | jermar | 155 | ns16550_grab(); |
1844 | jermar | 156 | } |
157 | |||
1931 | jermar | 158 | /** Process ns16550 interrupt. */ |
159 | void ns16550_interrupt(void) |
||
1844 | jermar | 160 | { |
3646 | jermar | 161 | ns16550_poll(); |
1844 | jermar | 162 | } |
163 | |||
164 | /* Called from getc(). */ |
||
165 | void ns16550_resume(chardev_t *d) |
||
166 | { |
||
167 | } |
||
168 | |||
169 | /* Called from getc(). */ |
||
170 | void ns16550_suspend(chardev_t *d) |
||
171 | { |
||
172 | } |
||
173 | |||
3577 | vana | 174 | |
1896 | jermar | 175 | char ns16550_key_read(chardev_t *d) |
1844 | jermar | 176 | { |
177 | char ch; |
||
178 | |||
179 | while(!(ch = active_read_buff_read())) { |
||
180 | uint8_t x; |
||
3877 | decky | 181 | while (!(ns16550_lsr_read(&ns16550) & LSR_DATA_READY)); |
182 | |||
1921 | jermar | 183 | x = ns16550_rbr_read(&ns16550); |
3877 | decky | 184 | |
1844 | jermar | 185 | if (x != IGNORE_CODE) { |
186 | if (x & KEY_RELEASE) |
||
187 | key_released(x ^ KEY_RELEASE); |
||
188 | else |
||
189 | active_read_key_pressed(x); |
||
190 | } |
||
191 | } |
||
192 | return ch; |
||
193 | } |
||
194 | |||
195 | /** Poll for key press and release events. |
||
196 | * |
||
197 | * This function can be used to implement keyboard polling. |
||
198 | */ |
||
199 | void ns16550_poll(void) |
||
200 | { |
||
3877 | decky | 201 | #ifndef CONFIG_NS16550_INTERRUPT_DRIVEN |
1931 | jermar | 202 | ipl_t ipl; |
1844 | jermar | 203 | |
1931 | jermar | 204 | ipl = interrupts_disable(); |
205 | spinlock_lock(&ns16550_irq.lock); |
||
206 | |||
207 | if (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) { |
||
1932 | jermar | 208 | if (ns16550_irq.notif_cfg.notify && ns16550_irq.notif_cfg.answerbox) { |
1931 | jermar | 209 | /* |
210 | * Send IPC notification. |
||
211 | */ |
||
212 | ipc_irq_send_notif(&ns16550_irq); |
||
213 | spinlock_unlock(&ns16550_irq.lock); |
||
214 | interrupts_restore(ipl); |
||
215 | return; |
||
216 | } |
||
217 | } |
||
218 | |||
219 | spinlock_unlock(&ns16550_irq.lock); |
||
220 | interrupts_restore(ipl); |
||
3646 | jermar | 221 | #endif |
1931 | jermar | 222 | |
1921 | jermar | 223 | while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) { |
1931 | jermar | 224 | uint8_t x; |
225 | |||
1921 | jermar | 226 | x = ns16550_rbr_read(&ns16550); |
3877 | decky | 227 | |
1844 | jermar | 228 | if (x != IGNORE_CODE) { |
229 | if (x & KEY_RELEASE) |
||
230 | key_released(x ^ KEY_RELEASE); |
||
231 | else |
||
232 | key_pressed(x); |
||
233 | } |
||
234 | } |
||
235 | } |
||
236 | |||
1919 | jermar | 237 | irq_ownership_t ns16550_claim(void) |
238 | { |
||
1931 | jermar | 239 | return (ns16550_lsr_read(&ns16550) & LSR_DATA_READY); |
1919 | jermar | 240 | } |
241 | |||
242 | void ns16550_irq_handler(irq_t *irq, void *arg, ...) |
||
243 | { |
||
3646 | jermar | 244 | if (irq->notif_cfg.notify && irq->notif_cfg.answerbox) |
245 | ipc_irq_send_notif(irq); |
||
246 | else |
||
247 | ns16550_interrupt(); |
||
1919 | jermar | 248 | } |
249 | |||
1844 | jermar | 250 | /** @} |
251 | */ |