Rev 3581 | Rev 3655 | 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> |
||
3577 | vana | 41 | #ifndef ia64 |
1931 | jermar | 42 | #include <arch/drivers/kbd.h> |
1844 | jermar | 43 | #include <arch/drivers/ns16550.h> |
3577 | vana | 44 | #endif |
1920 | jermar | 45 | #include <ddi/irq.h> |
1931 | jermar | 46 | #include <ipc/irq.h> |
1844 | jermar | 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> |
||
1931 | jermar | 53 | #include <arch/interrupt.h> |
1880 | jermar | 54 | #include <sysinfo/sysinfo.h> |
1931 | jermar | 55 | #include <synch/spinlock.h> |
1844 | jermar | 56 | |
57 | #define LSR_DATA_READY 0x01 |
||
58 | |||
1921 | jermar | 59 | /** Structure representing the ns16550. */ |
60 | static ns16550_t ns16550; |
||
61 | |||
62 | /** Structure for ns16550's IRQ. */ |
||
63 | static irq_t ns16550_irq; |
||
64 | |||
1844 | jermar | 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, |
||
1896 | jermar | 76 | .read = ns16550_key_read |
1844 | jermar | 77 | }; |
78 | |||
1931 | jermar | 79 | void ns16550_interrupt(void); |
1844 | jermar | 80 | |
81 | /** Initialize keyboard and service interrupts using kernel routine */ |
||
82 | void ns16550_grab(void) |
||
83 | { |
||
1944 | jermar | 84 | ipl_t ipl = interrupts_disable(); |
85 | |||
1931 | jermar | 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 | |||
1944 | jermar | 91 | spinlock_lock(&ns16550_irq.lock); |
1932 | jermar | 92 | ns16550_irq.notif_cfg.notify = false; |
1944 | jermar | 93 | spinlock_unlock(&ns16550_irq.lock); |
94 | interrupts_restore(ipl); |
||
1844 | jermar | 95 | } |
96 | |||
97 | /** Resume the former interrupt vector */ |
||
98 | void ns16550_release(void) |
||
99 | { |
||
1944 | jermar | 100 | ipl_t ipl = interrupts_disable(); |
101 | spinlock_lock(&ns16550_irq.lock); |
||
1932 | jermar | 102 | if (ns16550_irq.notif_cfg.answerbox) |
103 | ns16550_irq.notif_cfg.notify = true; |
||
1944 | jermar | 104 | spinlock_unlock(&ns16550_irq.lock); |
105 | interrupts_restore(ipl); |
||
1844 | jermar | 106 | } |
107 | |||
1921 | jermar | 108 | /** Initialize ns16550. |
109 | * |
||
110 | * @param devno Device number. |
||
111 | * @param inr Interrupt number. |
||
112 | * @param vaddr Virtual address of device's registers. |
||
113 | */ |
||
3577 | vana | 114 | void ns16550_init(devno_t devno, inr_t inr, ioport_t port) |
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; |
||
127 | irq_register(&ns16550_irq); |
||
128 | |||
1880 | jermar | 129 | sysinfo_set_item_val("kbd", NULL, true); |
3577 | vana | 130 | #ifndef ia64 |
1931 | jermar | 131 | sysinfo_set_item_val("kbd.type", NULL, KBD_NS16550); |
3577 | vana | 132 | #endif |
1921 | jermar | 133 | sysinfo_set_item_val("kbd.devno", NULL, devno); |
134 | sysinfo_set_item_val("kbd.inr", NULL, inr); |
||
3577 | vana | 135 | sysinfo_set_item_val("kbd.address.virtual", NULL, port); |
136 | |||
3646 | jermar | 137 | #ifdef CONFIG_NS16550_INTERRUPT_DRIVEN |
138 | /* Enable interrupts */ |
||
139 | ns16550_ier_write(&ns16550, IER_ERBFI); |
||
140 | ns16550_mcr_write(&ns16550, MCR_OUT2); |
||
141 | #endif |
||
142 | |||
3577 | vana | 143 | #ifdef ia64 |
144 | uint8_t c; |
||
3581 | jermar | 145 | c = ns16550_lcr_read(&ns16550); |
146 | ns16550_lcr_write(&ns16550, 0x80 | c); |
||
147 | ns16550_rbr_write(&ns16550, 0x0c); |
||
148 | ns16550_ier_write(&ns16550, 0x00); |
||
149 | ns16550_lcr_write(&ns16550, c); |
||
3577 | vana | 150 | #endif |
1911 | jermar | 151 | |
1931 | jermar | 152 | ns16550_grab(); |
1844 | jermar | 153 | } |
154 | |||
1931 | jermar | 155 | /** Process ns16550 interrupt. */ |
156 | void ns16550_interrupt(void) |
||
1844 | jermar | 157 | { |
3646 | jermar | 158 | ns16550_poll(); |
1844 | jermar | 159 | } |
160 | |||
161 | /* Called from getc(). */ |
||
162 | void ns16550_resume(chardev_t *d) |
||
163 | { |
||
164 | } |
||
165 | |||
166 | /* Called from getc(). */ |
||
167 | void ns16550_suspend(chardev_t *d) |
||
168 | { |
||
169 | } |
||
170 | |||
3577 | vana | 171 | |
1896 | jermar | 172 | char ns16550_key_read(chardev_t *d) |
1844 | jermar | 173 | { |
174 | char ch; |
||
175 | |||
176 | while(!(ch = active_read_buff_read())) { |
||
177 | uint8_t x; |
||
1921 | jermar | 178 | while (!(ns16550_lsr_read(&ns16550) & LSR_DATA_READY)) |
1844 | jermar | 179 | ; |
1921 | jermar | 180 | x = ns16550_rbr_read(&ns16550); |
3577 | vana | 181 | #ifndef ia64 |
1844 | jermar | 182 | if (x != IGNORE_CODE) { |
183 | if (x & KEY_RELEASE) |
||
184 | key_released(x ^ KEY_RELEASE); |
||
185 | else |
||
186 | active_read_key_pressed(x); |
||
187 | } |
||
3577 | vana | 188 | #else |
3581 | jermar | 189 | extern chardev_t kbrd; |
190 | if(x != 0x0d) { |
||
191 | if(x == 0x7f) |
||
192 | x = '\b'; |
||
193 | chardev_push_character(&kbrd, x); |
||
194 | } |
||
3577 | vana | 195 | #endif |
196 | |||
1844 | jermar | 197 | } |
198 | return ch; |
||
199 | } |
||
200 | |||
201 | /** Poll for key press and release events. |
||
202 | * |
||
203 | * This function can be used to implement keyboard polling. |
||
204 | */ |
||
205 | void ns16550_poll(void) |
||
206 | { |
||
3646 | jermar | 207 | #ifndef CONFIG_NS16550_INTERRUPT_DRIVEN |
1931 | jermar | 208 | ipl_t ipl; |
1844 | jermar | 209 | |
1931 | jermar | 210 | ipl = interrupts_disable(); |
211 | spinlock_lock(&ns16550_irq.lock); |
||
212 | |||
213 | if (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) { |
||
1932 | jermar | 214 | if (ns16550_irq.notif_cfg.notify && ns16550_irq.notif_cfg.answerbox) { |
1931 | jermar | 215 | /* |
216 | * Send IPC notification. |
||
217 | */ |
||
218 | ipc_irq_send_notif(&ns16550_irq); |
||
219 | spinlock_unlock(&ns16550_irq.lock); |
||
220 | interrupts_restore(ipl); |
||
221 | return; |
||
222 | } |
||
223 | } |
||
224 | |||
225 | spinlock_unlock(&ns16550_irq.lock); |
||
226 | interrupts_restore(ipl); |
||
3646 | jermar | 227 | #endif |
1931 | jermar | 228 | |
1921 | jermar | 229 | while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) { |
1931 | jermar | 230 | uint8_t x; |
231 | |||
1921 | jermar | 232 | x = ns16550_rbr_read(&ns16550); |
3577 | vana | 233 | #ifndef ia64 |
1844 | jermar | 234 | if (x != IGNORE_CODE) { |
235 | if (x & KEY_RELEASE) |
||
236 | key_released(x ^ KEY_RELEASE); |
||
237 | else |
||
238 | key_pressed(x); |
||
239 | } |
||
3577 | vana | 240 | #else |
3581 | jermar | 241 | extern chardev_t kbrd; |
242 | if(x != 0x0d) { |
||
243 | if (x == 0x7f) |
||
244 | x = '\b'; |
||
245 | chardev_push_character(&kbrd, x); |
||
246 | } |
||
3577 | vana | 247 | #endif |
248 | |||
1844 | jermar | 249 | } |
250 | } |
||
251 | |||
1919 | jermar | 252 | irq_ownership_t ns16550_claim(void) |
253 | { |
||
1931 | jermar | 254 | return (ns16550_lsr_read(&ns16550) & LSR_DATA_READY); |
1919 | jermar | 255 | } |
256 | |||
257 | void ns16550_irq_handler(irq_t *irq, void *arg, ...) |
||
258 | { |
||
3646 | jermar | 259 | if (irq->notif_cfg.notify && irq->notif_cfg.answerbox) |
260 | ipc_irq_send_notif(irq); |
||
261 | else |
||
262 | ns16550_interrupt(); |
||
1919 | jermar | 263 | } |
264 | |||
1844 | jermar | 265 | /** @} |
266 | */ |