Subversion Repositories HelenOS

Rev

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

Rev 3959 Rev 3982
1
/*
1
/*
2
 * Copyright (c) 2001-2006 Jakub Jermar
2
 * Copyright (c) 2009 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 <cpu.h>
43
#include <cpu.h>
44
#include <arch/asm.h>
44
#include <arch/asm.h>
45
#include <arch.h>
45
#include <arch.h>
46
#include <console/chardev.h>
46
#include <console/chardev.h>
47
#include <console/console.h>
47
#include <console/console.h>
48
#include <interrupt.h>
48
#include <interrupt.h>
49
#include <arch/interrupt.h>
49
#include <arch/interrupt.h>
50
#include <sysinfo/sysinfo.h>
-
 
51
#include <synch/spinlock.h>
50
#include <synch/spinlock.h>
52
#include <mm/slab.h>
51
#include <mm/slab.h>
53
 
52
 
54
#define LSR_DATA_READY  0x01
53
#define LSR_DATA_READY  0x01
55
 
54
 
56
/*
55
/*
57
 * These codes read from ns16550 data register are silently ignored.
56
 * These codes read from ns16550 data register are silently ignored.
58
 */
57
 */
59
#define IGNORE_CODE 0x7f        /* all keys up */
58
#define IGNORE_CODE 0x7f        /* all keys up */
60
 
59
 
61
static void ns16550_suspend(chardev_t *);
60
static void ns16550_suspend(chardev_t *);
62
static void ns16550_resume(chardev_t *);
61
static void ns16550_resume(chardev_t *);
63
 
62
 
64
static chardev_operations_t ops = {
63
static chardev_operations_t ops = {
65
    .suspend = ns16550_suspend,
64
    .suspend = ns16550_suspend,
66
    .resume = ns16550_resume,
65
    .resume = ns16550_resume,
67
};
66
};
68
 
67
 
69
/** Initialize ns16550.
68
/** Initialize ns16550.
70
 *
69
 *
71
 * @param dev       Addrress of the beginning of the device in I/O space.
70
 * @param dev       Addrress of the beginning of the device in I/O space.
72
 * @param devno     Device number.
71
 * @param devno     Device number.
73
 * @param inr       Interrupt number.
72
 * @param inr       Interrupt number.
74
 * @param cir       Clear interrupt function.
73
 * @param cir       Clear interrupt function.
75
 * @param cir_arg   First argument to cir.
74
 * @param cir_arg   First argument to cir.
76
 *
75
 *
77
 * @return      True on success, false on failure.
76
 * @return      True on success, false on failure.
78
 */
77
 */
79
bool
78
bool
80
ns16550_init(ns16550_t *dev, devno_t devno, inr_t inr, cir_t cir, void *cir_arg)
79
ns16550_init(ns16550_t *dev, devno_t devno, inr_t inr, cir_t cir, void *cir_arg)
81
{
80
{
82
    ns16550_instance_t *instance;
81
    ns16550_instance_t *instance;
83
 
82
 
84
    chardev_initialize("ns16550_kbd", &kbrd, &ops);
83
    chardev_initialize("ns16550_kbd", &kbrd, &ops);
85
    stdin = &kbrd;
84
    stdin = &kbrd;
86
   
85
   
87
    instance = malloc(sizeof(ns16550_instance_t), FRAME_ATOMIC);
86
    instance = malloc(sizeof(ns16550_instance_t), FRAME_ATOMIC);
88
    if (!instance)
87
    if (!instance)
89
        return false;
88
        return false;
90
 
89
 
91
    instance->devno = devno;
90
    instance->devno = devno;
92
    instance->ns16550 = dev;
91
    instance->ns16550 = dev;
93
   
92
   
94
    irq_initialize(&instance->irq);
93
    irq_initialize(&instance->irq);
95
    instance->irq.devno = devno;
94
    instance->irq.devno = devno;
96
    instance->irq.inr = inr;
95
    instance->irq.inr = inr;
97
    instance->irq.claim = ns16550_claim;
96
    instance->irq.claim = ns16550_claim;
98
    instance->irq.handler = ns16550_irq_handler;
97
    instance->irq.handler = ns16550_irq_handler;
99
    instance->irq.instance = instance;
98
    instance->irq.instance = instance;
100
    instance->irq.cir = cir;
99
    instance->irq.cir = cir;
101
    instance->irq.cir_arg = cir_arg;
100
    instance->irq.cir_arg = cir_arg;
102
    irq_register(&instance->irq);
101
    irq_register(&instance->irq);
103
 
102
 
104
    while ((pio_read_8(&dev->lsr) & LSR_DATA_READY))
103
    while ((pio_read_8(&dev->lsr) & LSR_DATA_READY))
105
        (void) pio_read_8(&dev->rbr);
104
        (void) pio_read_8(&dev->rbr);
106
   
105
   
107
    /*
-
 
108
     * This is the necessary evil until the userspace driver is entirely
-
 
109
     * self-sufficient.
-
 
110
     */
-
 
111
    sysinfo_set_item_val("kbd", NULL, true);
-
 
112
    sysinfo_set_item_val("kbd.type", NULL, KBD_NS16550);
-
 
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, (uintptr_t) dev);
-
 
116
    sysinfo_set_item_val("kbd.port", NULL, (uintptr_t) dev);
-
 
117
   
-
 
118
    /* Enable interrupts */
106
    /* Enable interrupts */
119
    pio_write_8(&dev->ier, IER_ERBFI);
107
    pio_write_8(&dev->ier, IER_ERBFI);
120
    pio_write_8(&dev->mcr, MCR_OUT2);
108
    pio_write_8(&dev->mcr, MCR_OUT2);
121
   
109
   
122
    return true;
110
    return true;
123
}
111
}
124
 
112
 
125
/* Called from getc(). */
113
/* Called from getc(). */
126
void ns16550_resume(chardev_t *d)
114
void ns16550_resume(chardev_t *d)
127
{
115
{
128
}
116
}
129
 
117
 
130
/* Called from getc(). */
118
/* Called from getc(). */
131
void ns16550_suspend(chardev_t *d)
119
void ns16550_suspend(chardev_t *d)
132
{
120
{
133
}
121
}
134
 
122
 
135
irq_ownership_t ns16550_claim(irq_t *irq)
123
irq_ownership_t ns16550_claim(irq_t *irq)
136
{
124
{
137
    ns16550_instance_t *ns16550_instance = irq->instance;
125
    ns16550_instance_t *ns16550_instance = irq->instance;
138
    ns16550_t *dev = ns16550_instance->ns16550;
126
    ns16550_t *dev = ns16550_instance->ns16550;
139
 
127
 
140
    if (pio_read_8(&dev->lsr) & LSR_DATA_READY)
128
    if (pio_read_8(&dev->lsr) & LSR_DATA_READY)
141
        return IRQ_ACCEPT;
129
        return IRQ_ACCEPT;
142
    else
130
    else
143
        return IRQ_DECLINE;
131
        return IRQ_DECLINE;
144
}
132
}
145
 
133
 
146
void ns16550_irq_handler(irq_t *irq)
134
void ns16550_irq_handler(irq_t *irq)
147
{
135
{
148
    ns16550_instance_t *ns16550_instance = irq->instance;
136
    ns16550_instance_t *ns16550_instance = irq->instance;
149
    ns16550_t *dev = ns16550_instance->ns16550;
137
    ns16550_t *dev = ns16550_instance->ns16550;
150
 
138
 
151
    if (pio_read_8(&dev->lsr) & LSR_DATA_READY) {
139
    if (pio_read_8(&dev->lsr) & LSR_DATA_READY) {
152
        uint8_t x;
140
        uint8_t x;
153
       
141
       
154
        x = pio_read_8(&dev->rbr);
142
        x = pio_read_8(&dev->rbr);
155
       
143
       
156
        if (x != IGNORE_CODE) {
144
        if (x != IGNORE_CODE) {
157
            if (x & KEY_RELEASE)
145
            if (x & KEY_RELEASE)
158
                key_released(x ^ KEY_RELEASE);
146
                key_released(x ^ KEY_RELEASE);
159
            else
147
            else
160
                key_pressed(x);
148
                key_pressed(x);
161
        }
149
        }
162
    }
150
    }
163
 
151
 
164
}
152
}
165
 
153
 
166
/** @}
154
/** @}
167
 */
155
 */
168
 
156