Rev 2071 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2071 | Rev 2089 | ||
---|---|---|---|
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> |
- | |
49 | #include <console/chardev.h> |
48 | #include <console/chardev.h> |
50 | #include <console/console.h> |
49 | #include <console/console.h> |
51 | #include <interrupt.h> |
50 | #include <interrupt.h> |
52 | #include <arch/interrupt.h> |
51 | #include <arch/interrupt.h> |
53 | #include <sysinfo/sysinfo.h> |
52 | #include <sysinfo/sysinfo.h> |
54 | #include <synch/spinlock.h> |
53 | #include <synch/spinlock.h> |
55 | 54 | ||
56 | #define LSR_DATA_READY 0x01 |
55 | #define LSR_DATA_READY 0x01 |
57 | 56 | ||
58 | /** Structure representing the ns16550. */ |
57 | /** Structure representing the ns16550. */ |
59 | static ns16550_t ns16550; |
58 | static ns16550_t ns16550; |
60 | 59 | ||
61 | /** Structure for ns16550's IRQ. */ |
60 | /** Structure for ns16550's IRQ. */ |
62 | static irq_t ns16550_irq; |
61 | static irq_t ns16550_irq; |
63 | 62 | ||
64 | /* |
63 | /* |
65 | * These codes read from ns16550 data register are silently ignored. |
64 | * These codes read from ns16550 data register are silently ignored. |
66 | */ |
65 | */ |
67 | #define IGNORE_CODE 0x7f /* all keys up */ |
66 | #define IGNORE_CODE 0x7f /* all keys up */ |
68 | 67 | ||
69 | static void ns16550_suspend(chardev_t *); |
68 | static void ns16550_suspend(chardev_t *); |
70 | static void ns16550_resume(chardev_t *); |
69 | static void ns16550_resume(chardev_t *); |
71 | 70 | ||
72 | static chardev_operations_t ops = { |
71 | static chardev_operations_t ops = { |
73 | .suspend = ns16550_suspend, |
72 | .suspend = ns16550_suspend, |
74 | .resume = ns16550_resume, |
73 | .resume = ns16550_resume, |
75 | .read = ns16550_key_read |
74 | .read = ns16550_key_read |
76 | }; |
75 | }; |
77 | 76 | ||
78 | void ns16550_interrupt(void); |
77 | void ns16550_interrupt(void); |
79 | 78 | ||
80 | /** Initialize keyboard and service interrupts using kernel routine */ |
79 | /** Initialize keyboard and service interrupts using kernel routine */ |
81 | void ns16550_grab(void) |
80 | void ns16550_grab(void) |
82 | { |
81 | { |
83 | ipl_t ipl = interrupts_disable(); |
82 | ipl_t ipl = interrupts_disable(); |
84 | 83 | ||
85 | ns16550_ier_write(&ns16550, IER_ERBFI); /* enable receiver interrupt */ |
84 | ns16550_ier_write(&ns16550, IER_ERBFI); /* enable receiver interrupt */ |
86 | 85 | ||
87 | while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) |
86 | while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) |
88 | (void) ns16550_rbr_read(&ns16550); |
87 | (void) ns16550_rbr_read(&ns16550); |
89 | 88 | ||
90 | spinlock_lock(&ns16550_irq.lock); |
89 | spinlock_lock(&ns16550_irq.lock); |
91 | ns16550_irq.notif_cfg.notify = false; |
90 | ns16550_irq.notif_cfg.notify = false; |
92 | spinlock_unlock(&ns16550_irq.lock); |
91 | spinlock_unlock(&ns16550_irq.lock); |
93 | interrupts_restore(ipl); |
92 | interrupts_restore(ipl); |
94 | } |
93 | } |
95 | 94 | ||
96 | /** Resume the former interrupt vector */ |
95 | /** Resume the former interrupt vector */ |
97 | void ns16550_release(void) |
96 | void ns16550_release(void) |
98 | { |
97 | { |
99 | ipl_t ipl = interrupts_disable(); |
98 | ipl_t ipl = interrupts_disable(); |
100 | spinlock_lock(&ns16550_irq.lock); |
99 | spinlock_lock(&ns16550_irq.lock); |
101 | if (ns16550_irq.notif_cfg.answerbox) |
100 | if (ns16550_irq.notif_cfg.answerbox) |
102 | ns16550_irq.notif_cfg.notify = true; |
101 | ns16550_irq.notif_cfg.notify = true; |
103 | spinlock_unlock(&ns16550_irq.lock); |
102 | spinlock_unlock(&ns16550_irq.lock); |
104 | interrupts_restore(ipl); |
103 | interrupts_restore(ipl); |
105 | } |
104 | } |
106 | 105 | ||
107 | /** Initialize ns16550. |
106 | /** Initialize ns16550. |
108 | * |
107 | * |
109 | * @param devno Device number. |
108 | * @param devno Device number. |
110 | * @param inr Interrupt number. |
109 | * @param inr Interrupt number. |
111 | * @param vaddr Virtual address of device's registers. |
110 | * @param vaddr Virtual address of device's registers. |
112 | */ |
111 | */ |
113 | void ns16550_init(devno_t devno, inr_t inr, uintptr_t vaddr) |
112 | void ns16550_init(devno_t devno, inr_t inr, uintptr_t vaddr) |
114 | { |
113 | { |
115 | chardev_initialize("ns16550_kbd", &kbrd, &ops); |
114 | chardev_initialize("ns16550_kbd", &kbrd, &ops); |
116 | stdin = &kbrd; |
115 | stdin = &kbrd; |
117 | 116 | ||
118 | ns16550.devno = devno; |
117 | ns16550.devno = devno; |
119 | ns16550.reg = (uint8_t *) vaddr; |
118 | ns16550.reg = (uint8_t *) vaddr; |
120 | 119 | ||
121 | irq_initialize(&ns16550_irq); |
120 | irq_initialize(&ns16550_irq); |
122 | ns16550_irq.devno = devno; |
121 | ns16550_irq.devno = devno; |
123 | ns16550_irq.inr = inr; |
122 | ns16550_irq.inr = inr; |
124 | ns16550_irq.claim = ns16550_claim; |
123 | ns16550_irq.claim = ns16550_claim; |
125 | ns16550_irq.handler = ns16550_irq_handler; |
124 | ns16550_irq.handler = ns16550_irq_handler; |
126 | irq_register(&ns16550_irq); |
125 | irq_register(&ns16550_irq); |
127 | 126 | ||
128 | sysinfo_set_item_val("kbd", NULL, true); |
127 | sysinfo_set_item_val("kbd", NULL, true); |
129 | sysinfo_set_item_val("kbd.type", NULL, KBD_NS16550); |
128 | sysinfo_set_item_val("kbd.type", NULL, KBD_NS16550); |
130 | sysinfo_set_item_val("kbd.devno", NULL, devno); |
129 | sysinfo_set_item_val("kbd.devno", NULL, devno); |
131 | sysinfo_set_item_val("kbd.inr", NULL, inr); |
130 | sysinfo_set_item_val("kbd.inr", NULL, inr); |
132 | sysinfo_set_item_val("kbd.address.virtual", NULL, vaddr); |
131 | sysinfo_set_item_val("kbd.address.virtual", NULL, vaddr); |
133 | 132 | ||
134 | ns16550_grab(); |
133 | ns16550_grab(); |
135 | } |
134 | } |
136 | 135 | ||
137 | /** Process ns16550 interrupt. */ |
136 | /** Process ns16550 interrupt. */ |
138 | void ns16550_interrupt(void) |
137 | void ns16550_interrupt(void) |
139 | { |
138 | { |
140 | /* TODO |
139 | /* TODO |
141 | * |
140 | * |
142 | * ns16550 works in the polled mode so far. |
141 | * ns16550 works in the polled mode so far. |
143 | */ |
142 | */ |
144 | } |
143 | } |
145 | 144 | ||
146 | /* Called from getc(). */ |
145 | /* Called from getc(). */ |
147 | void ns16550_resume(chardev_t *d) |
146 | void ns16550_resume(chardev_t *d) |
148 | { |
147 | { |
149 | } |
148 | } |
150 | 149 | ||
151 | /* Called from getc(). */ |
150 | /* Called from getc(). */ |
152 | void ns16550_suspend(chardev_t *d) |
151 | void ns16550_suspend(chardev_t *d) |
153 | { |
152 | { |
154 | } |
153 | } |
155 | 154 | ||
156 | char ns16550_key_read(chardev_t *d) |
155 | char ns16550_key_read(chardev_t *d) |
157 | { |
156 | { |
158 | char ch; |
157 | char ch; |
159 | 158 | ||
160 | while(!(ch = active_read_buff_read())) { |
159 | while(!(ch = active_read_buff_read())) { |
161 | uint8_t x; |
160 | uint8_t x; |
162 | while (!(ns16550_lsr_read(&ns16550) & LSR_DATA_READY)) |
161 | while (!(ns16550_lsr_read(&ns16550) & LSR_DATA_READY)) |
163 | ; |
162 | ; |
164 | x = ns16550_rbr_read(&ns16550); |
163 | x = ns16550_rbr_read(&ns16550); |
165 | if (x != IGNORE_CODE) { |
164 | if (x != IGNORE_CODE) { |
166 | if (x & KEY_RELEASE) |
165 | if (x & KEY_RELEASE) |
167 | key_released(x ^ KEY_RELEASE); |
166 | key_released(x ^ KEY_RELEASE); |
168 | else |
167 | else |
169 | active_read_key_pressed(x); |
168 | active_read_key_pressed(x); |
170 | } |
169 | } |
171 | } |
170 | } |
172 | return ch; |
171 | return ch; |
173 | } |
172 | } |
174 | 173 | ||
175 | /** Poll for key press and release events. |
174 | /** Poll for key press and release events. |
176 | * |
175 | * |
177 | * This function can be used to implement keyboard polling. |
176 | * This function can be used to implement keyboard polling. |
178 | */ |
177 | */ |
179 | void ns16550_poll(void) |
178 | void ns16550_poll(void) |
180 | { |
179 | { |
181 | ipl_t ipl; |
180 | ipl_t ipl; |
182 | 181 | ||
183 | ipl = interrupts_disable(); |
182 | ipl = interrupts_disable(); |
184 | spinlock_lock(&ns16550_irq.lock); |
183 | spinlock_lock(&ns16550_irq.lock); |
185 | 184 | ||
186 | if (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) { |
185 | if (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) { |
187 | if (ns16550_irq.notif_cfg.notify && ns16550_irq.notif_cfg.answerbox) { |
186 | if (ns16550_irq.notif_cfg.notify && ns16550_irq.notif_cfg.answerbox) { |
188 | /* |
187 | /* |
189 | * Send IPC notification. |
188 | * Send IPC notification. |
190 | */ |
189 | */ |
191 | ipc_irq_send_notif(&ns16550_irq); |
190 | ipc_irq_send_notif(&ns16550_irq); |
192 | spinlock_unlock(&ns16550_irq.lock); |
191 | spinlock_unlock(&ns16550_irq.lock); |
193 | interrupts_restore(ipl); |
192 | interrupts_restore(ipl); |
194 | return; |
193 | return; |
195 | } |
194 | } |
196 | } |
195 | } |
197 | 196 | ||
198 | spinlock_unlock(&ns16550_irq.lock); |
197 | spinlock_unlock(&ns16550_irq.lock); |
199 | interrupts_restore(ipl); |
198 | interrupts_restore(ipl); |
200 | 199 | ||
201 | while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) { |
200 | while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) { |
202 | uint8_t x; |
201 | uint8_t x; |
203 | 202 | ||
204 | x = ns16550_rbr_read(&ns16550); |
203 | x = ns16550_rbr_read(&ns16550); |
205 | if (x != IGNORE_CODE) { |
204 | if (x != IGNORE_CODE) { |
206 | if (x & KEY_RELEASE) |
205 | if (x & KEY_RELEASE) |
207 | key_released(x ^ KEY_RELEASE); |
206 | key_released(x ^ KEY_RELEASE); |
208 | else |
207 | else |
209 | key_pressed(x); |
208 | key_pressed(x); |
210 | } |
209 | } |
211 | } |
210 | } |
212 | } |
211 | } |
213 | 212 | ||
214 | irq_ownership_t ns16550_claim(void) |
213 | irq_ownership_t ns16550_claim(void) |
215 | { |
214 | { |
216 | return (ns16550_lsr_read(&ns16550) & LSR_DATA_READY); |
215 | return (ns16550_lsr_read(&ns16550) & LSR_DATA_READY); |
217 | } |
216 | } |
218 | 217 | ||
219 | void ns16550_irq_handler(irq_t *irq, void *arg, ...) |
218 | void ns16550_irq_handler(irq_t *irq, void *arg, ...) |
220 | { |
219 | { |
221 | panic("Not yet implemented, ns16550 works in polled mode.\n"); |
220 | panic("Not yet implemented, ns16550 works in polled mode.\n"); |
222 | } |
221 | } |
223 | 222 | ||
224 | /** @} |
223 | /** @} |
225 | */ |
224 | */ |
226 | 225 |