source: mainline/kernel/genarch/src/kbd/ns16550.c@ 6c441cf8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6c441cf8 was b3f8fb7, checked in by Martin Decky <martin@…>, 19 years ago

huge type system cleanup
remove cyclical type dependencies across multiple header files
many minor coding style fixes

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