Rev 1931 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1931 | Rev 1932 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2001-2006 Jakub Jermar |
2 | * Copyright (C) 2001-2006 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup genarch |
29 | /** @addtogroup genarch |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | /** |
32 | /** |
33 | * @file |
33 | * @file |
34 | * @brief NS 16550 serial port / keyboard driver. |
34 | * @brief NS 16550 serial port / keyboard driver. |
35 | */ |
35 | */ |
36 | 36 | ||
37 | #include <genarch/kbd/ns16550.h> |
37 | #include <genarch/kbd/ns16550.h> |
38 | #include <genarch/kbd/key.h> |
38 | #include <genarch/kbd/key.h> |
39 | #include <genarch/kbd/scanc.h> |
39 | #include <genarch/kbd/scanc.h> |
40 | #include <genarch/kbd/scanc_sun.h> |
40 | #include <genarch/kbd/scanc_sun.h> |
41 | #include <arch/drivers/kbd.h> |
41 | #include <arch/drivers/kbd.h> |
42 | #include <arch/drivers/ns16550.h> |
42 | #include <arch/drivers/ns16550.h> |
43 | #include <ddi/irq.h> |
43 | #include <ddi/irq.h> |
44 | #include <ipc/irq.h> |
44 | #include <ipc/irq.h> |
45 | #include <cpu.h> |
45 | #include <cpu.h> |
46 | #include <arch/asm.h> |
46 | #include <arch/asm.h> |
47 | #include <arch.h> |
47 | #include <arch.h> |
48 | #include <typedefs.h> |
48 | #include <typedefs.h> |
49 | #include <console/chardev.h> |
49 | #include <console/chardev.h> |
50 | #include <console/console.h> |
50 | #include <console/console.h> |
51 | #include <interrupt.h> |
51 | #include <interrupt.h> |
52 | #include <arch/interrupt.h> |
52 | #include <arch/interrupt.h> |
53 | #include <sysinfo/sysinfo.h> |
53 | #include <sysinfo/sysinfo.h> |
54 | #include <synch/spinlock.h> |
54 | #include <synch/spinlock.h> |
55 | 55 | ||
56 | #define LSR_DATA_READY 0x01 |
56 | #define LSR_DATA_READY 0x01 |
57 | 57 | ||
58 | /** Structure representing the ns16550. */ |
58 | /** Structure representing the ns16550. */ |
59 | static ns16550_t ns16550; |
59 | static ns16550_t ns16550; |
60 | 60 | ||
61 | /** Structure for ns16550's IRQ. */ |
61 | /** Structure for ns16550's IRQ. */ |
62 | static irq_t ns16550_irq; |
62 | static irq_t ns16550_irq; |
63 | 63 | ||
64 | static ipc_notif_cfg_t saved_notif_cfg; |
- | |
65 | - | ||
66 | /* |
64 | /* |
67 | * These codes read from ns16550 data register are silently ignored. |
65 | * These codes read from ns16550 data register are silently ignored. |
68 | */ |
66 | */ |
69 | #define IGNORE_CODE 0x7f /* all keys up */ |
67 | #define IGNORE_CODE 0x7f /* all keys up */ |
70 | 68 | ||
71 | static void ns16550_suspend(chardev_t *); |
69 | static void ns16550_suspend(chardev_t *); |
72 | static void ns16550_resume(chardev_t *); |
70 | static void ns16550_resume(chardev_t *); |
73 | 71 | ||
74 | static chardev_operations_t ops = { |
72 | static chardev_operations_t ops = { |
75 | .suspend = ns16550_suspend, |
73 | .suspend = ns16550_suspend, |
76 | .resume = ns16550_resume, |
74 | .resume = ns16550_resume, |
77 | .read = ns16550_key_read |
75 | .read = ns16550_key_read |
78 | }; |
76 | }; |
79 | 77 | ||
80 | void ns16550_interrupt(void); |
78 | void ns16550_interrupt(void); |
81 | 79 | ||
82 | /** Initialize keyboard and service interrupts using kernel routine */ |
80 | /** Initialize keyboard and service interrupts using kernel routine */ |
83 | void ns16550_grab(void) |
81 | void ns16550_grab(void) |
84 | { |
82 | { |
85 | ns16550_ier_write(&ns16550, IER_ERBFI); /* enable receiver interrupt */ |
83 | ns16550_ier_write(&ns16550, IER_ERBFI); /* enable receiver interrupt */ |
86 | 84 | ||
87 | while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) |
85 | while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) |
88 | (void) ns16550_rbr_read(&ns16550); |
86 | (void) ns16550_rbr_read(&ns16550); |
89 | 87 | ||
90 | if (ns16550_irq.notif_cfg.answerbox) { |
- | |
91 | saved_notif_cfg = ns16550_irq.notif_cfg; |
- | |
92 | ns16550_irq.notif_cfg.answerbox = NULL; |
- | |
93 | ns16550_irq.notif_cfg.code = NULL; |
88 | ns16550_irq.notif_cfg.notify = false; |
94 | ns16550_irq.notif_cfg.method = 0; |
- | |
95 | ns16550_irq.notif_cfg.counter = 0; |
- | |
96 | } |
- | |
97 | } |
89 | } |
98 | 90 | ||
99 | /** Resume the former interrupt vector */ |
91 | /** Resume the former interrupt vector */ |
100 | void ns16550_release(void) |
92 | void ns16550_release(void) |
101 | { |
93 | { |
102 | if (saved_notif_cfg.answerbox) |
94 | if (ns16550_irq.notif_cfg.answerbox) |
103 | ns16550_irq.notif_cfg = saved_notif_cfg; |
95 | ns16550_irq.notif_cfg.notify = true; |
104 | } |
96 | } |
105 | 97 | ||
106 | /** Initialize ns16550. |
98 | /** Initialize ns16550. |
107 | * |
99 | * |
108 | * @param devno Device number. |
100 | * @param devno Device number. |
109 | * @param inr Interrupt number. |
101 | * @param inr Interrupt number. |
110 | * @param vaddr Virtual address of device's registers. |
102 | * @param vaddr Virtual address of device's registers. |
111 | */ |
103 | */ |
112 | void ns16550_init(devno_t devno, inr_t inr, uintptr_t vaddr) |
104 | void ns16550_init(devno_t devno, inr_t inr, uintptr_t vaddr) |
113 | { |
105 | { |
114 | chardev_initialize("ns16550_kbd", &kbrd, &ops); |
106 | chardev_initialize("ns16550_kbd", &kbrd, &ops); |
115 | stdin = &kbrd; |
107 | stdin = &kbrd; |
116 | 108 | ||
117 | ns16550.devno = devno; |
109 | ns16550.devno = devno; |
118 | ns16550.reg = (uint8_t *) vaddr; |
110 | ns16550.reg = (uint8_t *) vaddr; |
119 | 111 | ||
120 | irq_initialize(&ns16550_irq); |
112 | irq_initialize(&ns16550_irq); |
121 | ns16550_irq.devno = devno; |
113 | ns16550_irq.devno = devno; |
122 | ns16550_irq.inr = inr; |
114 | ns16550_irq.inr = inr; |
123 | ns16550_irq.claim = ns16550_claim; |
115 | ns16550_irq.claim = ns16550_claim; |
124 | ns16550_irq.handler = ns16550_irq_handler; |
116 | ns16550_irq.handler = ns16550_irq_handler; |
125 | irq_register(&ns16550_irq); |
117 | irq_register(&ns16550_irq); |
126 | 118 | ||
127 | sysinfo_set_item_val("kbd", NULL, true); |
119 | sysinfo_set_item_val("kbd", NULL, true); |
128 | sysinfo_set_item_val("kbd.type", NULL, KBD_NS16550); |
120 | sysinfo_set_item_val("kbd.type", NULL, KBD_NS16550); |
129 | sysinfo_set_item_val("kbd.devno", NULL, devno); |
121 | sysinfo_set_item_val("kbd.devno", NULL, devno); |
130 | sysinfo_set_item_val("kbd.inr", NULL, inr); |
122 | sysinfo_set_item_val("kbd.inr", NULL, inr); |
131 | sysinfo_set_item_val("kbd.address.virtual", NULL, vaddr); |
123 | sysinfo_set_item_val("kbd.address.virtual", NULL, vaddr); |
132 | 124 | ||
133 | ns16550_grab(); |
125 | ns16550_grab(); |
134 | } |
126 | } |
135 | 127 | ||
136 | /** Process ns16550 interrupt. */ |
128 | /** Process ns16550 interrupt. */ |
137 | void ns16550_interrupt(void) |
129 | void ns16550_interrupt(void) |
138 | { |
130 | { |
139 | /* TODO |
131 | /* TODO |
140 | * |
132 | * |
141 | * ns16550 works in the polled mode so far. |
133 | * ns16550 works in the polled mode so far. |
142 | */ |
134 | */ |
143 | } |
135 | } |
144 | 136 | ||
145 | /* Called from getc(). */ |
137 | /* Called from getc(). */ |
146 | void ns16550_resume(chardev_t *d) |
138 | void ns16550_resume(chardev_t *d) |
147 | { |
139 | { |
148 | } |
140 | } |
149 | 141 | ||
150 | /* Called from getc(). */ |
142 | /* Called from getc(). */ |
151 | void ns16550_suspend(chardev_t *d) |
143 | void ns16550_suspend(chardev_t *d) |
152 | { |
144 | { |
153 | } |
145 | } |
154 | 146 | ||
155 | char ns16550_key_read(chardev_t *d) |
147 | char ns16550_key_read(chardev_t *d) |
156 | { |
148 | { |
157 | char ch; |
149 | char ch; |
158 | 150 | ||
159 | while(!(ch = active_read_buff_read())) { |
151 | while(!(ch = active_read_buff_read())) { |
160 | uint8_t x; |
152 | uint8_t x; |
161 | while (!(ns16550_lsr_read(&ns16550) & LSR_DATA_READY)) |
153 | while (!(ns16550_lsr_read(&ns16550) & LSR_DATA_READY)) |
162 | ; |
154 | ; |
163 | x = ns16550_rbr_read(&ns16550); |
155 | x = ns16550_rbr_read(&ns16550); |
164 | if (x != IGNORE_CODE) { |
156 | if (x != IGNORE_CODE) { |
165 | if (x & KEY_RELEASE) |
157 | if (x & KEY_RELEASE) |
166 | key_released(x ^ KEY_RELEASE); |
158 | key_released(x ^ KEY_RELEASE); |
167 | else |
159 | else |
168 | active_read_key_pressed(x); |
160 | active_read_key_pressed(x); |
169 | } |
161 | } |
170 | } |
162 | } |
171 | return ch; |
163 | return ch; |
172 | } |
164 | } |
173 | 165 | ||
174 | /** Poll for key press and release events. |
166 | /** Poll for key press and release events. |
175 | * |
167 | * |
176 | * This function can be used to implement keyboard polling. |
168 | * This function can be used to implement keyboard polling. |
177 | */ |
169 | */ |
178 | void ns16550_poll(void) |
170 | void ns16550_poll(void) |
179 | { |
171 | { |
180 | ipl_t ipl; |
172 | ipl_t ipl; |
181 | 173 | ||
182 | ipl = interrupts_disable(); |
174 | ipl = interrupts_disable(); |
183 | spinlock_lock(&ns16550_irq.lock); |
175 | spinlock_lock(&ns16550_irq.lock); |
184 | 176 | ||
185 | if (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) { |
177 | if (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) { |
186 | if (ns16550_irq.notif_cfg.answerbox) { |
178 | if (ns16550_irq.notif_cfg.notify && ns16550_irq.notif_cfg.answerbox) { |
187 | /* |
179 | /* |
188 | * Send IPC notification. |
180 | * Send IPC notification. |
189 | */ |
181 | */ |
190 | ipc_irq_send_notif(&ns16550_irq); |
182 | ipc_irq_send_notif(&ns16550_irq); |
191 | spinlock_unlock(&ns16550_irq.lock); |
183 | spinlock_unlock(&ns16550_irq.lock); |
192 | interrupts_restore(ipl); |
184 | interrupts_restore(ipl); |
193 | return; |
185 | return; |
194 | } |
186 | } |
195 | } |
187 | } |
196 | 188 | ||
197 | spinlock_unlock(&ns16550_irq.lock); |
189 | spinlock_unlock(&ns16550_irq.lock); |
198 | interrupts_restore(ipl); |
190 | interrupts_restore(ipl); |
199 | 191 | ||
200 | while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) { |
192 | while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) { |
201 | uint8_t x; |
193 | uint8_t x; |
202 | 194 | ||
203 | x = ns16550_rbr_read(&ns16550); |
195 | x = ns16550_rbr_read(&ns16550); |
204 | if (x != IGNORE_CODE) { |
196 | if (x != IGNORE_CODE) { |
205 | if (x & KEY_RELEASE) |
197 | if (x & KEY_RELEASE) |
206 | key_released(x ^ KEY_RELEASE); |
198 | key_released(x ^ KEY_RELEASE); |
207 | else |
199 | else |
208 | key_pressed(x); |
200 | key_pressed(x); |
209 | } |
201 | } |
210 | } |
202 | } |
211 | } |
203 | } |
212 | 204 | ||
213 | irq_ownership_t ns16550_claim(void) |
205 | irq_ownership_t ns16550_claim(void) |
214 | { |
206 | { |
215 | return (ns16550_lsr_read(&ns16550) & LSR_DATA_READY); |
207 | return (ns16550_lsr_read(&ns16550) & LSR_DATA_READY); |
216 | } |
208 | } |
217 | 209 | ||
218 | void ns16550_irq_handler(irq_t *irq, void *arg, ...) |
210 | void ns16550_irq_handler(irq_t *irq, void *arg, ...) |
219 | { |
211 | { |
220 | panic("Not yet implemented, ns16550 works in polled mode.\n"); |
212 | panic("Not yet implemented, ns16550 works in polled mode.\n"); |
221 | } |
213 | } |
222 | 214 | ||
223 | /** @} |
215 | /** @} |
224 | */ |
216 | */ |
225 | 217 |