Rev 1920 | Rev 1931 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1844 | jermar | 1 | /* |
1921 | 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> |
||
41 | #include <arch/drivers/ns16550.h> |
||
1920 | jermar | 42 | #include <ddi/irq.h> |
1844 | jermar | 43 | #include <arch/interrupt.h> |
44 | #include <cpu.h> |
||
45 | #include <arch/asm.h> |
||
46 | #include <arch.h> |
||
47 | #include <typedefs.h> |
||
48 | #include <console/chardev.h> |
||
49 | #include <console/console.h> |
||
50 | #include <interrupt.h> |
||
1880 | jermar | 51 | #include <sysinfo/sysinfo.h> |
1844 | jermar | 52 | |
53 | #define LSR_DATA_READY 0x01 |
||
54 | |||
1921 | jermar | 55 | /** Structure representing the ns16550. */ |
56 | static ns16550_t ns16550; |
||
57 | |||
58 | /** Structure for ns16550's IRQ. */ |
||
59 | static irq_t ns16550_irq; |
||
60 | |||
1844 | jermar | 61 | /* |
62 | * These codes read from ns16550 data register are silently ignored. |
||
63 | */ |
||
64 | #define IGNORE_CODE 0x7f /* all keys up */ |
||
65 | |||
66 | static void ns16550_suspend(chardev_t *); |
||
67 | static void ns16550_resume(chardev_t *); |
||
68 | |||
69 | static chardev_operations_t ops = { |
||
70 | .suspend = ns16550_suspend, |
||
71 | .resume = ns16550_resume, |
||
1896 | jermar | 72 | .read = ns16550_key_read |
1844 | jermar | 73 | }; |
74 | |||
75 | void ns16550_interrupt(int n, istate_t *istate); |
||
76 | void ns16550_wait(void); |
||
77 | |||
78 | /** Initialize keyboard and service interrupts using kernel routine */ |
||
79 | void ns16550_grab(void) |
||
80 | { |
||
81 | /* TODO */ |
||
82 | } |
||
83 | |||
84 | /** Resume the former interrupt vector */ |
||
85 | void ns16550_release(void) |
||
86 | { |
||
87 | /* TODO */ |
||
88 | } |
||
89 | |||
1921 | jermar | 90 | /** Initialize ns16550. |
91 | * |
||
92 | * @param devno Device number. |
||
93 | * @param inr Interrupt number. |
||
94 | * @param vaddr Virtual address of device's registers. |
||
95 | */ |
||
96 | void ns16550_init(devno_t devno, inr_t inr, uintptr_t vaddr) |
||
1844 | jermar | 97 | { |
98 | ns16550_grab(); |
||
99 | chardev_initialize("ns16550_kbd", &kbrd, &ops); |
||
100 | stdin = &kbrd; |
||
1880 | jermar | 101 | |
1921 | jermar | 102 | ns16550.devno = devno; |
103 | ns16550.reg = (uint8_t *) vaddr; |
||
104 | |||
105 | irq_initialize(&ns16550_irq); |
||
106 | ns16550_irq.devno = devno; |
||
107 | ns16550_irq.inr = inr; |
||
108 | ns16550_irq.claim = ns16550_claim; |
||
109 | ns16550_irq.handler = ns16550_irq_handler; |
||
110 | irq_register(&ns16550_irq); |
||
111 | |||
1880 | jermar | 112 | sysinfo_set_item_val("kbd", NULL, true); |
1921 | jermar | 113 | sysinfo_set_item_val("kbd.devno", NULL, devno); |
114 | sysinfo_set_item_val("kbd.inr", NULL, inr); |
||
115 | sysinfo_set_item_val("kbd.address.virtual", NULL, vaddr); |
||
1911 | jermar | 116 | |
1921 | jermar | 117 | ns16550_ier_write(&ns16550, IER_ERBFI); /* enable receiver interrupt */ |
1911 | jermar | 118 | |
1921 | jermar | 119 | while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) |
120 | (void) ns16550_rbr_read(&ns16550); |
||
1844 | jermar | 121 | } |
122 | |||
123 | /** Process ns16550 interrupt. |
||
124 | * |
||
125 | * @param n Interrupt vector. |
||
126 | * @param istate Interrupted state. |
||
127 | */ |
||
128 | void ns16550_interrupt(int n, istate_t *istate) |
||
129 | { |
||
130 | /* TODO */ |
||
131 | } |
||
132 | |||
133 | /** Wait until the controller reads its data. */ |
||
1888 | jermar | 134 | void ns16550_wait(void) |
135 | { |
||
1844 | jermar | 136 | } |
137 | |||
138 | /* Called from getc(). */ |
||
139 | void ns16550_resume(chardev_t *d) |
||
140 | { |
||
141 | } |
||
142 | |||
143 | /* Called from getc(). */ |
||
144 | void ns16550_suspend(chardev_t *d) |
||
145 | { |
||
146 | } |
||
147 | |||
1896 | jermar | 148 | char ns16550_key_read(chardev_t *d) |
1844 | jermar | 149 | { |
150 | char ch; |
||
151 | |||
152 | while(!(ch = active_read_buff_read())) { |
||
153 | uint8_t x; |
||
1921 | jermar | 154 | while (!(ns16550_lsr_read(&ns16550) & LSR_DATA_READY)) |
1844 | jermar | 155 | ; |
1921 | jermar | 156 | x = ns16550_rbr_read(&ns16550); |
1844 | jermar | 157 | if (x != IGNORE_CODE) { |
158 | if (x & KEY_RELEASE) |
||
159 | key_released(x ^ KEY_RELEASE); |
||
160 | else |
||
161 | active_read_key_pressed(x); |
||
162 | } |
||
163 | } |
||
164 | return ch; |
||
165 | } |
||
166 | |||
167 | /** Poll for key press and release events. |
||
168 | * |
||
169 | * This function can be used to implement keyboard polling. |
||
170 | */ |
||
171 | void ns16550_poll(void) |
||
172 | { |
||
173 | uint8_t x; |
||
174 | |||
1921 | jermar | 175 | while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) { |
176 | x = ns16550_rbr_read(&ns16550); |
||
1844 | jermar | 177 | if (x != IGNORE_CODE) { |
178 | if (x & KEY_RELEASE) |
||
179 | key_released(x ^ KEY_RELEASE); |
||
180 | else |
||
181 | key_pressed(x); |
||
182 | } |
||
183 | } |
||
184 | } |
||
185 | |||
1919 | jermar | 186 | irq_ownership_t ns16550_claim(void) |
187 | { |
||
188 | /* TODO */ |
||
189 | return IRQ_ACCEPT; |
||
190 | } |
||
191 | |||
192 | void ns16550_irq_handler(irq_t *irq, void *arg, ...) |
||
193 | { |
||
194 | panic("Not yet implemented.\n"); |
||
195 | } |
||
196 | |||
1844 | jermar | 197 | /** @} |
198 | */ |