Subversion Repositories HelenOS

Rev

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

Rev 2071 Rev 2089
1
/*
1
/*
2
 * Copyright (c) 2001-2004 Jakub Jermar
2
 * Copyright (c) 2001-2004 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   Zilog 8530 serial port / keyboard driver.
34
 * @brief   Zilog 8530 serial port / keyboard driver.
35
 */
35
 */
36
 
36
 
37
#include <genarch/kbd/z8530.h>
37
#include <genarch/kbd/z8530.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/z8530.h>
41
#include <arch/drivers/z8530.h>
42
#include <ddi/irq.h>
42
#include <ddi/irq.h>
43
#include <ipc/irq.h>
43
#include <ipc/irq.h>
44
#include <arch/interrupt.h>
44
#include <arch/interrupt.h>
45
#include <arch/drivers/kbd.h>
45
#include <arch/drivers/kbd.h>
46
#include <arch/drivers/fhc.h>
46
#include <arch/drivers/fhc.h>
47
#include <cpu.h>
47
#include <cpu.h>
48
#include <arch/asm.h>
48
#include <arch/asm.h>
49
#include <arch.h>
49
#include <arch.h>
50
#include <typedefs.h>
-
 
51
#include <console/chardev.h>
50
#include <console/chardev.h>
52
#include <console/console.h>
51
#include <console/console.h>
53
#include <interrupt.h>
52
#include <interrupt.h>
54
#include <sysinfo/sysinfo.h>
53
#include <sysinfo/sysinfo.h>
55
#include <print.h>
54
#include <print.h>
56
 
55
 
57
/*
56
/*
58
 * These codes read from z8530 data register are silently ignored.
57
 * These codes read from z8530 data register are silently ignored.
59
 */
58
 */
60
#define IGNORE_CODE 0x7f        /* all keys up */
59
#define IGNORE_CODE 0x7f        /* all keys up */
61
 
60
 
62
static z8530_t z8530;       /**< z8530 device structure. */
61
static z8530_t z8530;       /**< z8530 device structure. */
63
static irq_t z8530_irq;     /**< z8530's IRQ. */
62
static irq_t z8530_irq;     /**< z8530's IRQ. */
64
 
63
 
65
static void z8530_suspend(chardev_t *);
64
static void z8530_suspend(chardev_t *);
66
static void z8530_resume(chardev_t *);
65
static void z8530_resume(chardev_t *);
67
 
66
 
68
static chardev_operations_t ops = {
67
static chardev_operations_t ops = {
69
    .suspend = z8530_suspend,
68
    .suspend = z8530_suspend,
70
    .resume = z8530_resume,
69
    .resume = z8530_resume,
71
    .read = z8530_key_read
70
    .read = z8530_key_read
72
};
71
};
73
 
72
 
74
/** Initialize keyboard and service interrupts using kernel routine. */
73
/** Initialize keyboard and service interrupts using kernel routine. */
75
void z8530_grab(void)
74
void z8530_grab(void)
76
{
75
{
77
    ipl_t ipl = interrupts_disable();
76
    ipl_t ipl = interrupts_disable();
78
 
77
 
79
    (void) z8530_read_a(&z8530, RR8);
78
    (void) z8530_read_a(&z8530, RR8);
80
 
79
 
81
    /*
80
    /*
82
     * Clear any pending TX interrupts or we never manage
81
     * Clear any pending TX interrupts or we never manage
83
     * to set FHC UART interrupt state to idle.
82
     * to set FHC UART interrupt state to idle.
84
     */
83
     */
85
    z8530_write_a(&z8530, WR0, WR0_TX_IP_RST);
84
    z8530_write_a(&z8530, WR0, WR0_TX_IP_RST);
86
 
85
 
87
    z8530_write_a(&z8530, WR1, WR1_IARCSC);     /* interrupt on all characters */
86
    z8530_write_a(&z8530, WR1, WR1_IARCSC);     /* interrupt on all characters */
88
 
87
 
89
    /* 8 bits per character and enable receiver */
88
    /* 8 bits per character and enable receiver */
90
    z8530_write_a(&z8530, WR3, WR3_RX8BITSCH | WR3_RX_ENABLE);
89
    z8530_write_a(&z8530, WR3, WR3_RX8BITSCH | WR3_RX_ENABLE);
91
   
90
   
92
    z8530_write_a(&z8530, WR9, WR9_MIE);        /* Master Interrupt Enable. */
91
    z8530_write_a(&z8530, WR9, WR9_MIE);        /* Master Interrupt Enable. */
93
   
92
   
94
    spinlock_lock(&z8530_irq.lock);
93
    spinlock_lock(&z8530_irq.lock);
95
    z8530_irq.notif_cfg.notify = false;
94
    z8530_irq.notif_cfg.notify = false;
96
    spinlock_unlock(&z8530_irq.lock);
95
    spinlock_unlock(&z8530_irq.lock);
97
    interrupts_restore(ipl);
96
    interrupts_restore(ipl);
98
}
97
}
99
 
98
 
100
/** Resume the former IPC notification behavior. */
99
/** Resume the former IPC notification behavior. */
101
void z8530_release(void)
100
void z8530_release(void)
102
{
101
{
103
    ipl_t ipl = interrupts_disable();
102
    ipl_t ipl = interrupts_disable();
104
    spinlock_lock(&z8530_irq.lock);
103
    spinlock_lock(&z8530_irq.lock);
105
    if (z8530_irq.notif_cfg.answerbox)
104
    if (z8530_irq.notif_cfg.answerbox)
106
        z8530_irq.notif_cfg.notify = true;
105
        z8530_irq.notif_cfg.notify = true;
107
    spinlock_unlock(&z8530_irq.lock);
106
    spinlock_unlock(&z8530_irq.lock);
108
    interrupts_restore(ipl);
107
    interrupts_restore(ipl);
109
}
108
}
110
 
109
 
111
/** Initialize z8530. */
110
/** Initialize z8530. */
112
void z8530_init(devno_t devno, inr_t inr, uintptr_t vaddr)
111
void z8530_init(devno_t devno, inr_t inr, uintptr_t vaddr)
113
{
112
{
114
    chardev_initialize("z8530_kbd", &kbrd, &ops);
113
    chardev_initialize("z8530_kbd", &kbrd, &ops);
115
    stdin = &kbrd;
114
    stdin = &kbrd;
116
 
115
 
117
    z8530.devno = devno;
116
    z8530.devno = devno;
118
    z8530.reg = (uint8_t *) vaddr;
117
    z8530.reg = (uint8_t *) vaddr;
119
 
118
 
120
    irq_initialize(&z8530_irq);
119
    irq_initialize(&z8530_irq);
121
    z8530_irq.devno = devno;
120
    z8530_irq.devno = devno;
122
    z8530_irq.inr = inr;
121
    z8530_irq.inr = inr;
123
    z8530_irq.claim = z8530_claim;
122
    z8530_irq.claim = z8530_claim;
124
    z8530_irq.handler = z8530_irq_handler;
123
    z8530_irq.handler = z8530_irq_handler;
125
    irq_register(&z8530_irq);
124
    irq_register(&z8530_irq);
126
 
125
 
127
    sysinfo_set_item_val("kbd", NULL, true);
126
    sysinfo_set_item_val("kbd", NULL, true);
128
    sysinfo_set_item_val("kbd.type", NULL, KBD_Z8530);
127
    sysinfo_set_item_val("kbd.type", NULL, KBD_Z8530);
129
    sysinfo_set_item_val("kbd.devno", NULL, devno);
128
    sysinfo_set_item_val("kbd.devno", NULL, devno);
130
    sysinfo_set_item_val("kbd.inr", NULL, inr);
129
    sysinfo_set_item_val("kbd.inr", NULL, inr);
131
    sysinfo_set_item_val("kbd.address.virtual", NULL, vaddr);
130
    sysinfo_set_item_val("kbd.address.virtual", NULL, vaddr);
132
 
131
 
133
    z8530_grab();
132
    z8530_grab();
134
}
133
}
135
 
134
 
136
/** Process z8530 interrupt.
135
/** Process z8530 interrupt.
137
 *
136
 *
138
 * @param n Interrupt vector.
137
 * @param n Interrupt vector.
139
 * @param istate Interrupted state.
138
 * @param istate Interrupted state.
140
 */
139
 */
141
void z8530_interrupt(void)
140
void z8530_interrupt(void)
142
{
141
{
143
    z8530_poll();
142
    z8530_poll();
144
}
143
}
145
 
144
 
146
/* Called from getc(). */
145
/* Called from getc(). */
147
void z8530_resume(chardev_t *d)
146
void z8530_resume(chardev_t *d)
148
{
147
{
149
}
148
}
150
 
149
 
151
/* Called from getc(). */
150
/* Called from getc(). */
152
void z8530_suspend(chardev_t *d)
151
void z8530_suspend(chardev_t *d)
153
{
152
{
154
}
153
}
155
 
154
 
156
char z8530_key_read(chardev_t *d)
155
char z8530_key_read(chardev_t *d)
157
{
156
{
158
    char ch;   
157
    char ch;   
159
 
158
 
160
    while(!(ch = active_read_buff_read())) {
159
    while(!(ch = active_read_buff_read())) {
161
        uint8_t x;
160
        uint8_t x;
162
        while (!(z8530_read_a(&z8530, RR0) & RR0_RCA))
161
        while (!(z8530_read_a(&z8530, RR0) & RR0_RCA))
163
            ;
162
            ;
164
        x = z8530_read_a(&z8530, RR8);
163
        x = z8530_read_a(&z8530, RR8);
165
        if (x != IGNORE_CODE) {
164
        if (x != IGNORE_CODE) {
166
            if (x & KEY_RELEASE)
165
            if (x & KEY_RELEASE)
167
                key_released(x ^ KEY_RELEASE);
166
                key_released(x ^ KEY_RELEASE);
168
            else
167
            else
169
                active_read_key_pressed(x);
168
                active_read_key_pressed(x);
170
        }
169
        }
171
    }
170
    }
172
    return ch;
171
    return ch;
173
}
172
}
174
 
173
 
175
/** Poll for key press and release events.
174
/** Poll for key press and release events.
176
 *
175
 *
177
 * This function can be used to implement keyboard polling.
176
 * This function can be used to implement keyboard polling.
178
 */
177
 */
179
void z8530_poll(void)
178
void z8530_poll(void)
180
{
179
{
181
    uint8_t x;
180
    uint8_t x;
182
 
181
 
183
    while (z8530_read_a(&z8530, RR0) & RR0_RCA) {
182
    while (z8530_read_a(&z8530, RR0) & RR0_RCA) {
184
        x = z8530_read_a(&z8530, RR8);
183
        x = z8530_read_a(&z8530, RR8);
185
        if (x != IGNORE_CODE) {
184
        if (x != IGNORE_CODE) {
186
            if (x & KEY_RELEASE)
185
            if (x & KEY_RELEASE)
187
                key_released(x ^ KEY_RELEASE);
186
                key_released(x ^ KEY_RELEASE);
188
            else
187
            else
189
                key_pressed(x);
188
                key_pressed(x);
190
        }
189
        }
191
    }
190
    }
192
}
191
}
193
 
192
 
194
irq_ownership_t z8530_claim(void)
193
irq_ownership_t z8530_claim(void)
195
{
194
{
196
    return (z8530_read_a(&z8530, RR0) & RR0_RCA);
195
    return (z8530_read_a(&z8530, RR0) & RR0_RCA);
197
}
196
}
198
 
197
 
199
void z8530_irq_handler(irq_t *irq, void *arg, ...)
198
void z8530_irq_handler(irq_t *irq, void *arg, ...)
200
{
199
{
201
    /*
200
    /*
202
     * So far, we know we got this interrupt through the FHC.
201
     * So far, we know we got this interrupt through the FHC.
203
     * Since we don't have enough documentation about the FHC
202
     * Since we don't have enough documentation about the FHC
204
     * and because the interrupt looks like level sensitive,
203
     * and because the interrupt looks like level sensitive,
205
     * we cannot handle it by scheduling one of the level
204
     * we cannot handle it by scheduling one of the level
206
     * interrupt traps. Process the interrupt directly.
205
     * interrupt traps. Process the interrupt directly.
207
     */
206
     */
208
    if (irq->notif_cfg.notify && irq->notif_cfg.answerbox)
207
    if (irq->notif_cfg.notify && irq->notif_cfg.answerbox)
209
        ipc_irq_send_notif(irq);
208
        ipc_irq_send_notif(irq);
210
    else
209
    else
211
        z8530_interrupt();
210
        z8530_interrupt();
212
    fhc_clear_interrupt(central_fhc, irq->inr);
211
    fhc_clear_interrupt(central_fhc, irq->inr);
213
}
212
}
214
 
213
 
215
/** @}
214
/** @}
216
 */
215
 */
217
 
216