Subversion Repositories HelenOS

Rev

Rev 3906 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3906 Rev 3934
Line 48... Line 48...
48
#include <console/console.h>
48
#include <console/console.h>
49
#include <interrupt.h>
49
#include <interrupt.h>
50
#include <arch/interrupt.h>
50
#include <arch/interrupt.h>
51
#include <sysinfo/sysinfo.h>
51
#include <sysinfo/sysinfo.h>
52
#include <synch/spinlock.h>
52
#include <synch/spinlock.h>
-
 
53
#include <mm/slab.h>
53
 
54
 
54
#define LSR_DATA_READY  0x01
55
#define LSR_DATA_READY  0x01
55
 
56
 
56
/** Structure representing the ns16550. */
-
 
57
static ns16550_t ns16550;
-
 
58
 
-
 
59
/** Structure for ns16550's IRQ. */
-
 
60
static irq_t ns16550_irq;
57
static irq_t *ns16550_irq;
61
 
58
 
62
/*
59
/*
63
 * These codes read from ns16550 data register are silently ignored.
60
 * These codes read from ns16550 data register are silently ignored.
64
 */
61
 */
65
#define IGNORE_CODE 0x7f        /* all keys up */
62
#define IGNORE_CODE 0x7f        /* all keys up */
Line 68... Line 65...
68
static void ns16550_resume(chardev_t *);
65
static void ns16550_resume(chardev_t *);
69
 
66
 
70
static chardev_operations_t ops = {
67
static chardev_operations_t ops = {
71
    .suspend = ns16550_suspend,
68
    .suspend = ns16550_suspend,
72
    .resume = ns16550_resume,
69
    .resume = ns16550_resume,
73
    .read = ns16550_key_read
-
 
74
};
70
};
75
 
71
 
76
void ns16550_interrupt(void);
-
 
77
 
-
 
78
/** Initialize keyboard and service interrupts using kernel routine */
72
/** Initialize keyboard and service interrupts using kernel routine */
79
void ns16550_grab(void)
73
void ns16550_grab(void)
80
{
74
{
81
    ipl_t ipl = interrupts_disable();
75
    ipl_t ipl = interrupts_disable();
82
 
-
 
83
    ns16550_ier_write(&ns16550, IER_ERBFI);     /* enable receiver interrupt */
-
 
84
   
-
 
85
    while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY)
-
 
86
        (void) ns16550_rbr_read(&ns16550);
-
 
87
 
-
 
88
    spinlock_lock(&ns16550_irq.lock);
76
    spinlock_lock(&ns16550_irq->lock);
89
    ns16550_irq.notif_cfg.notify = false;
77
    ns16550_irq->notif_cfg.notify = false;
90
    spinlock_unlock(&ns16550_irq.lock);
78
    spinlock_unlock(&ns16550_irq->lock);
91
    interrupts_restore(ipl);
79
    interrupts_restore(ipl);
92
}
80
}
93
 
81
 
94
/** Resume the former interrupt vector */
82
/** Resume the former interrupt vector */
95
void ns16550_release(void)
83
void ns16550_release(void)
96
{
84
{
97
    ipl_t ipl = interrupts_disable();
85
    ipl_t ipl = interrupts_disable();
98
    spinlock_lock(&ns16550_irq.lock);
86
    spinlock_lock(&ns16550_irq->lock);
99
    if (ns16550_irq.notif_cfg.answerbox)
87
    if (ns16550_irq->notif_cfg.answerbox)
100
        ns16550_irq.notif_cfg.notify = true;
88
        ns16550_irq->notif_cfg.notify = true;
101
    spinlock_unlock(&ns16550_irq.lock);
89
    spinlock_unlock(&ns16550_irq->lock);
102
    interrupts_restore(ipl);
90
    interrupts_restore(ipl);
103
}
91
}
104
 
92
 
105
/** Initialize ns16550.
93
/** Initialize ns16550.
106
 *
94
 *
-
 
95
 * @param dev       Addrress of the beginning of the device in I/O space.
107
 * @param devno     Device number.
96
 * @param devno     Device number.
108
 * @param port      Virtual/IO address of device's registers.
-
 
109
 * @param inr       Interrupt number.
97
 * @param inr       Interrupt number.
110
 * @param cir       Clear interrupt function.
98
 * @param cir       Clear interrupt function.
111
 * @param cir_arg   First argument to cir.
99
 * @param cir_arg   First argument to cir.
-
 
100
 *
-
 
101
 * @return      True on success, false on failure.
112
 */
102
 */
113
void
103
bool
114
ns16550_init(devno_t devno, ioport_t port, inr_t inr, cir_t cir, void *cir_arg)
104
ns16550_init(ns16550_t *dev, devno_t devno, inr_t inr, cir_t cir, void *cir_arg)
115
{
105
{
-
 
106
    ns16550_instance_t *instance;
-
 
107
    irq_t *irq;
-
 
108
 
116
    chardev_initialize("ns16550_kbd", &kbrd, &ops);
109
    chardev_initialize("ns16550_kbd", &kbrd, &ops);
117
    stdin = &kbrd;
110
    stdin = &kbrd;
118
   
111
   
-
 
112
    instance = malloc(sizeof(ns16550_instance_t), FRAME_ATOMIC);
-
 
113
    if (!instance)
-
 
114
        return false;
-
 
115
 
-
 
116
    irq = malloc(sizeof(irq_t), FRAME_ATOMIC);
-
 
117
    if (!irq) {
-
 
118
        free(instance);
-
 
119
        return false;
-
 
120
    }
-
 
121
 
119
    ns16550.devno = devno;
122
    instance->devno = devno;
-
 
123
    instance->ns16550 = dev;
120
    ns16550.io_port = port;
124
    instance->irq = irq;
121
   
125
   
122
    irq_initialize(&ns16550_irq);
126
    irq_initialize(irq);
123
    ns16550_irq.devno = devno;
127
    irq->devno = devno;
124
    ns16550_irq.inr = inr;
128
    irq->inr = inr;
125
    ns16550_irq.claim = ns16550_claim;
129
    irq->claim = ns16550_claim;
126
    ns16550_irq.handler = ns16550_irq_handler;
130
    irq->handler = ns16550_irq_handler;
-
 
131
    irq->instance = instance;
127
    ns16550_irq.cir = cir;
132
    irq->cir = cir;
128
    ns16550_irq.cir_arg = cir_arg;
133
    irq->cir_arg = cir_arg;
129
    irq_register(&ns16550_irq);
134
    irq_register(irq);
-
 
135
 
-
 
136
    ns16550_irq = irq;  /* TODO: remove me soon */
130
   
137
   
131
    while ((ns16550_lsr_read(&ns16550) & LSR_DATA_READY))
138
    while ((pio_read_8(&dev->lsr) & LSR_DATA_READY))
132
        ns16550_rbr_read(&ns16550);
139
        (void) pio_read_8(&dev->rbr);
133
   
140
   
134
    sysinfo_set_item_val("kbd", NULL, true);
141
    sysinfo_set_item_val("kbd", NULL, true);
135
    sysinfo_set_item_val("kbd.type", NULL, KBD_NS16550);
142
    sysinfo_set_item_val("kbd.type", NULL, KBD_NS16550);
136
    sysinfo_set_item_val("kbd.devno", NULL, devno);
143
    sysinfo_set_item_val("kbd.devno", NULL, devno);
137
    sysinfo_set_item_val("kbd.inr", NULL, inr);
144
    sysinfo_set_item_val("kbd.inr", NULL, inr);
138
    sysinfo_set_item_val("kbd.address.virtual", NULL, port);
145
    sysinfo_set_item_val("kbd.address.virtual", NULL, (uintptr_t) dev);
139
    sysinfo_set_item_val("kbd.port", NULL, port);
146
    sysinfo_set_item_val("kbd.port", NULL, (uintptr_t) dev);
140
   
147
   
141
    /* Enable interrupts */
148
    /* Enable interrupts */
142
    ns16550_ier_write(&ns16550, IER_ERBFI);
149
    pio_write_8(&dev->ier, IER_ERBFI);
143
    ns16550_mcr_write(&ns16550, MCR_OUT2);
150
    pio_write_8(&dev->mcr, MCR_OUT2);
144
   
-
 
145
    uint8_t c;
-
 
146
    // This switches rbr & ier to mode when accept baudrate constant
-
 
147
    c = ns16550_lcr_read(&ns16550);
-
 
148
    ns16550_lcr_write(&ns16550, 0x80 | c);
-
 
149
    ns16550_rbr_write(&ns16550, 0x0c);
-
 
150
    ns16550_ier_write(&ns16550, 0x00);
-
 
151
    ns16550_lcr_write(&ns16550, c);
-
 
152
   
151
   
153
    ns16550_grab();
152
    ns16550_grab();
154
}
153
   
155
 
-
 
156
/** Process ns16550 interrupt. */
-
 
157
void ns16550_interrupt(void)
-
 
158
{
-
 
159
    ns16550_poll();
154
    return true;
160
}
155
}
161
 
156
 
162
/* Called from getc(). */
157
/* Called from getc(). */
163
void ns16550_resume(chardev_t *d)
158
void ns16550_resume(chardev_t *d)
164
{
159
{
Line 167... Line 162...
167
/* Called from getc(). */
162
/* Called from getc(). */
168
void ns16550_suspend(chardev_t *d)
163
void ns16550_suspend(chardev_t *d)
169
{
164
{
170
}
165
}
171
 
166
 
172
 
-
 
173
char ns16550_key_read(chardev_t *d)
167
irq_ownership_t ns16550_claim(void *instance)
174
{
168
{
175
    char ch;   
169
    ns16550_instance_t *ns16550_instance = instance;
-
 
170
    ns16550_t *dev = ns16550_instance->ns16550;
176
 
171
 
177
    while(!(ch = active_read_buff_read())) {
-
 
178
        uint8_t x;
-
 
179
        while (!(ns16550_lsr_read(&ns16550) & LSR_DATA_READY));
172
    if (pio_read_8(&dev->lsr) & LSR_DATA_READY)
180
       
-
 
181
        x = ns16550_rbr_read(&ns16550);
-
 
182
       
-
 
183
        if (x != IGNORE_CODE) {
173
        return IRQ_ACCEPT;
184
            if (x & KEY_RELEASE)
-
 
185
                key_released(x ^ KEY_RELEASE);
-
 
186
            else
174
    else
187
                active_read_key_pressed(x);
-
 
188
        }
-
 
189
    }
-
 
190
    return ch;
175
        return IRQ_DECLINE;
191
}
176
}
192
 
177
 
193
/** Poll for key press and release events.
-
 
194
 *
-
 
195
 * This function can be used to implement keyboard polling.
-
 
196
 */
-
 
197
void ns16550_poll(void)
178
void ns16550_irq_handler(irq_t *irq)
198
{
179
{
-
 
180
    if (irq->notif_cfg.notify && irq->notif_cfg.answerbox) {
-
 
181
        /*
-
 
182
         * This will hopefully go to the IRQ dispatch code soon.
-
 
183
         */
-
 
184
        ipc_irq_send_notif(irq);
-
 
185
        return;
-
 
186
    }
-
 
187
 
-
 
188
    ns16550_instance_t *ns16550_instance = irq->instance;
-
 
189
    ns16550_t *dev = ns16550_instance->ns16550;
-
 
190
 
199
    while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) {
191
    if (pio_read_8(&dev->lsr) & LSR_DATA_READY) {
200
        uint8_t x;
192
        uint8_t x;
201
       
193
       
202
        x = ns16550_rbr_read(&ns16550);
194
        x = pio_read_8(&dev->rbr);
203
       
195
       
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
}
-
 
212
 
203
 
213
irq_ownership_t ns16550_claim(void *instance)
-
 
214
{
-
 
215
    return (ns16550_lsr_read(&ns16550) & LSR_DATA_READY);
-
 
216
}
-
 
217
 
-
 
218
void ns16550_irq_handler(irq_t *irq)
-
 
219
{
-
 
220
    if (irq->notif_cfg.notify && irq->notif_cfg.answerbox)
-
 
221
        ipc_irq_send_notif(irq);
-
 
222
    else
-
 
223
        ns16550_interrupt();
-
 
224
}
204
}
225
 
205
 
226
/** @}
206
/** @}
227
 */
207
 */