Subversion Repositories HelenOS

Rev

Rev 3938 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3938 Rev 3941
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 <ddi/irq.h>
42
#include <ddi/irq.h>
43
#include <ipc/irq.h>
43
#include <ipc/irq.h>
44
#include <cpu.h>
44
#include <cpu.h>
45
#include <arch/asm.h>
45
#include <arch/asm.h>
46
#include <arch.h>
46
#include <arch.h>
47
#include <console/chardev.h>
47
#include <console/chardev.h>
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
#include <mm/slab.h>
54
 
54
 
55
#define LSR_DATA_READY  0x01
55
#define LSR_DATA_READY  0x01
56
 
56
 
57
static irq_t *ns16550_irq;
57
static irq_t *ns16550_irq;
58
 
58
 
59
/*
59
/*
60
 * These codes read from ns16550 data register are silently ignored.
60
 * These codes read from ns16550 data register are silently ignored.
61
 */
61
 */
62
#define IGNORE_CODE 0x7f        /* all keys up */
62
#define IGNORE_CODE 0x7f        /* all keys up */
63
 
63
 
64
static void ns16550_suspend(chardev_t *);
64
static void ns16550_suspend(chardev_t *);
65
static void ns16550_resume(chardev_t *);
65
static void ns16550_resume(chardev_t *);
66
 
66
 
67
static chardev_operations_t ops = {
67
static chardev_operations_t ops = {
68
    .suspend = ns16550_suspend,
68
    .suspend = ns16550_suspend,
69
    .resume = ns16550_resume,
69
    .resume = ns16550_resume,
70
};
70
};
71
 
71
 
72
/** Initialize keyboard and service interrupts using kernel routine */
72
/** Initialize keyboard and service interrupts using kernel routine */
73
void ns16550_grab(void)
73
void ns16550_grab(void)
74
{
74
{
75
    ipl_t ipl = interrupts_disable();
75
    ipl_t ipl = interrupts_disable();
76
    spinlock_lock(&ns16550_irq->lock);
76
    spinlock_lock(&ns16550_irq->lock);
77
    ns16550_irq->notif_cfg.notify = false;
77
    ns16550_irq->notif_cfg.notify = false;
78
    spinlock_unlock(&ns16550_irq->lock);
78
    spinlock_unlock(&ns16550_irq->lock);
79
    interrupts_restore(ipl);
79
    interrupts_restore(ipl);
80
}
80
}
81
 
81
 
82
/** Resume the former interrupt vector */
82
/** Resume the former interrupt vector */
83
void ns16550_release(void)
83
void ns16550_release(void)
84
{
84
{
85
    ipl_t ipl = interrupts_disable();
85
    ipl_t ipl = interrupts_disable();
86
    spinlock_lock(&ns16550_irq->lock);
86
    spinlock_lock(&ns16550_irq->lock);
87
    if (ns16550_irq->notif_cfg.answerbox)
87
    if (ns16550_irq->notif_cfg.answerbox)
88
        ns16550_irq->notif_cfg.notify = true;
88
        ns16550_irq->notif_cfg.notify = true;
89
    spinlock_unlock(&ns16550_irq->lock);
89
    spinlock_unlock(&ns16550_irq->lock);
90
    interrupts_restore(ipl);
90
    interrupts_restore(ipl);
91
}
91
}
92
 
92
 
93
/** Initialize ns16550.
93
/** Initialize ns16550.
94
 *
94
 *
95
 * @param dev       Addrress of the beginning of the device in I/O space.
95
 * @param dev       Addrress of the beginning of the device in I/O space.
96
 * @param devno     Device number.
96
 * @param devno     Device number.
97
 * @param inr       Interrupt number.
97
 * @param inr       Interrupt number.
98
 * @param cir       Clear interrupt function.
98
 * @param cir       Clear interrupt function.
99
 * @param cir_arg   First argument to cir.
99
 * @param cir_arg   First argument to cir.
100
 *
100
 *
101
 * @return      True on success, false on failure.
101
 * @return      True on success, false on failure.
102
 */
102
 */
103
bool
103
bool
104
ns16550_init(ns16550_t *dev, devno_t devno, 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)
105
{
105
{
106
    ns16550_instance_t *instance;
106
    ns16550_instance_t *instance;
107
 
107
 
108
    chardev_initialize("ns16550_kbd", &kbrd, &ops);
108
    chardev_initialize("ns16550_kbd", &kbrd, &ops);
109
    stdin = &kbrd;
109
    stdin = &kbrd;
110
   
110
   
111
    instance = malloc(sizeof(ns16550_instance_t), FRAME_ATOMIC);
111
    instance = malloc(sizeof(ns16550_instance_t), FRAME_ATOMIC);
112
    if (!instance)
112
    if (!instance)
113
        return false;
113
        return false;
114
 
114
 
115
    instance->devno = devno;
115
    instance->devno = devno;
116
    instance->ns16550 = dev;
116
    instance->ns16550 = dev;
117
   
117
   
118
    irq_initialize(&instance->irq);
118
    irq_initialize(&instance->irq);
119
    instance->irq.devno = devno;
119
    instance->irq.devno = devno;
120
    instance->irq.inr = inr;
120
    instance->irq.inr = inr;
121
    instance->irq.claim = ns16550_claim;
121
    instance->irq.claim = ns16550_claim;
122
    instance->irq.handler = ns16550_irq_handler;
122
    instance->irq.handler = ns16550_irq_handler;
123
    instance->irq.instance = instance;
123
    instance->irq.instance = instance;
124
    instance->irq.cir = cir;
124
    instance->irq.cir = cir;
125
    instance->irq.cir_arg = cir_arg;
125
    instance->irq.cir_arg = cir_arg;
126
    irq_register(&instance->irq);
126
    irq_register(&instance->irq);
127
 
127
 
128
    ns16550_irq = &instance->irq;   /* TODO: remove me soon */
128
    ns16550_irq = &instance->irq;   /* TODO: remove me soon */
129
   
129
   
130
    while ((pio_read_8(&dev->lsr) & LSR_DATA_READY))
130
    while ((pio_read_8(&dev->lsr) & LSR_DATA_READY))
131
        (void) pio_read_8(&dev->rbr);
131
        (void) pio_read_8(&dev->rbr);
132
   
132
   
133
    sysinfo_set_item_val("kbd", NULL, true);
133
    sysinfo_set_item_val("kbd", NULL, true);
134
    sysinfo_set_item_val("kbd.type", NULL, KBD_NS16550);
134
    sysinfo_set_item_val("kbd.type", NULL, KBD_NS16550);
135
    sysinfo_set_item_val("kbd.devno", NULL, devno);
135
    sysinfo_set_item_val("kbd.devno", NULL, devno);
136
    sysinfo_set_item_val("kbd.inr", NULL, inr);
136
    sysinfo_set_item_val("kbd.inr", NULL, inr);
137
    sysinfo_set_item_val("kbd.address.virtual", NULL, (uintptr_t) dev);
137
    sysinfo_set_item_val("kbd.address.virtual", NULL, (uintptr_t) dev);
138
    sysinfo_set_item_val("kbd.port", NULL, (uintptr_t) dev);
138
    sysinfo_set_item_val("kbd.port", NULL, (uintptr_t) dev);
139
   
139
   
140
    /* Enable interrupts */
140
    /* Enable interrupts */
141
    pio_write_8(&dev->ier, IER_ERBFI);
141
    pio_write_8(&dev->ier, IER_ERBFI);
142
    pio_write_8(&dev->mcr, MCR_OUT2);
142
    pio_write_8(&dev->mcr, MCR_OUT2);
143
   
143
   
144
    ns16550_grab();
144
    ns16550_grab();
145
   
145
   
146
    return true;
146
    return true;
147
}
147
}
148
 
148
 
149
/* Called from getc(). */
149
/* Called from getc(). */
150
void ns16550_resume(chardev_t *d)
150
void ns16550_resume(chardev_t *d)
151
{
151
{
152
}
152
}
153
 
153
 
154
/* Called from getc(). */
154
/* Called from getc(). */
155
void ns16550_suspend(chardev_t *d)
155
void ns16550_suspend(chardev_t *d)
156
{
156
{
157
}
157
}
158
 
158
 
159
irq_ownership_t ns16550_claim(void *instance)
159
irq_ownership_t ns16550_claim(irq_t *irq)
160
{
160
{
161
    ns16550_instance_t *ns16550_instance = instance;
161
    ns16550_instance_t *ns16550_instance = irq->instance;
162
    ns16550_t *dev = ns16550_instance->ns16550;
162
    ns16550_t *dev = ns16550_instance->ns16550;
163
 
163
 
164
    if (pio_read_8(&dev->lsr) & LSR_DATA_READY)
164
    if (pio_read_8(&dev->lsr) & LSR_DATA_READY)
165
        return IRQ_ACCEPT;
165
        return IRQ_ACCEPT;
166
    else
166
    else
167
        return IRQ_DECLINE;
167
        return IRQ_DECLINE;
168
}
168
}
169
 
169
 
170
void ns16550_irq_handler(irq_t *irq)
170
void ns16550_irq_handler(irq_t *irq)
171
{
171
{
172
    if (irq->notif_cfg.notify && irq->notif_cfg.answerbox) {
172
    if (irq->notif_cfg.notify && irq->notif_cfg.answerbox) {
173
        /*
173
        /*
174
         * This will hopefully go to the IRQ dispatch code soon.
174
         * This will hopefully go to the IRQ dispatch code soon.
175
         */
175
         */
176
        ipc_irq_send_notif(irq);
176
        ipc_irq_send_notif(irq);
177
        return;
177
        return;
178
    }
178
    }
179
 
179
 
180
    ns16550_instance_t *ns16550_instance = irq->instance;
180
    ns16550_instance_t *ns16550_instance = irq->instance;
181
    ns16550_t *dev = ns16550_instance->ns16550;
181
    ns16550_t *dev = ns16550_instance->ns16550;
182
 
182
 
183
    if (pio_read_8(&dev->lsr) & LSR_DATA_READY) {
183
    if (pio_read_8(&dev->lsr) & LSR_DATA_READY) {
184
        uint8_t x;
184
        uint8_t x;
185
       
185
       
186
        x = pio_read_8(&dev->rbr);
186
        x = pio_read_8(&dev->rbr);
187
       
187
       
188
        if (x != IGNORE_CODE) {
188
        if (x != IGNORE_CODE) {
189
            if (x & KEY_RELEASE)
189
            if (x & KEY_RELEASE)
190
                key_released(x ^ KEY_RELEASE);
190
                key_released(x ^ KEY_RELEASE);
191
            else
191
            else
192
                key_pressed(x);
192
                key_pressed(x);
193
        }
193
        }
194
    }
194
    }
195
 
195
 
196
}
196
}
197
 
197
 
198
/** @}
198
/** @}
199
 */
199
 */
200
 
200