Subversion Repositories HelenOS

Rev

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

Rev 2215 Rev 2245
1
/*
1
/*
2
 * Copyright (c) 2005-2007 Ondrej Palkovsky, Michal Kebrt
2
 * Copyright (c) 2005-2007 Ondrej Palkovsky, Michal Kebrt, Petr Stepan
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 arm32
29
/** @addtogroup arm32
30
 * @{
30
 * @{
31
 */
31
 */
32
/** @file
32
/** @file
33
 */
33
 */
34
 
34
 
35
#include <interrupt.h>
35
#include <interrupt.h>
36
#include <ipc/irq.h>
36
#include <ipc/irq.h>
37
#include <console/chardev.h>
37
#include <console/chardev.h>
38
#include <arch/drivers/gxemul.h>
38
#include <arch/drivers/gxemul.h>
39
#include <console/console.h>
39
#include <console/console.h>
40
#include <sysinfo/sysinfo.h>
40
#include <sysinfo/sysinfo.h>
41
#include <print.h>
41
#include <print.h>
-
 
42
#include <ddi/device.h>
42
 
43
 
43
/** Address of devices. */
44
/** Address of devices. */
44
#define GXEMUL_VIDEORAM     0x10000000
45
#define GXEMUL_VIDEORAM     0x10000000
45
#define GXEMUL_KBD_ADDRESS  0x10000000
46
#define GXEMUL_KBD_ADDRESS  0x10000000
-
 
47
#define GXEMUL_RTC      0x15000000
-
 
48
#define GXEMUL_RTC_FREQ     0x15000100
-
 
49
#define GXEMUL_RTC_ACK      0x15000110
-
 
50
#define GXEMUL_IRQC     0x16000000
-
 
51
#define GXEMUL_IRQC_MASK    0x16000004
-
 
52
#define GXEMUL_IRQC_UNMASK  0x16000008
-
 
53
 
-
 
54
/** IRQs */
46
#define GXEMUL_KBD_IRQ      0
55
#define GXEMUL_KBD_IRQ      2
-
 
56
#define GXEMUL_TIMER_IRQ    4
47
 
57
 
48
static chardev_t console;
58
static chardev_t console;
49
static irq_t gxemul_irq;
59
static irq_t gxemul_irq;
-
 
60
static irq_t gxemul_timer_irq;
50
 
61
 
51
static void gxemul_write(chardev_t *dev, const char ch);
62
static void gxemul_write(chardev_t *dev, const char ch);
52
static void gxemul_enable(chardev_t *dev);
63
static void gxemul_enable(chardev_t *dev);
53
static void gxemul_disable(chardev_t *dev);
64
static void gxemul_disable(chardev_t *dev);
54
static char gxemul_do_read(chardev_t *dev);
65
static char gxemul_do_read(chardev_t *dev);
55
 
66
 
56
static chardev_operations_t gxemul_ops = {
67
static chardev_operations_t gxemul_ops = {
57
    .resume = gxemul_enable,
68
    .resume = gxemul_enable,
58
    .suspend = gxemul_disable,
69
    .suspend = gxemul_disable,
59
    .write = gxemul_write,
70
    .write = gxemul_write,
60
    .read = gxemul_do_read,
71
    .read = gxemul_do_read,
61
};
72
};
62
 
73
 
63
/** Putchar that works with gxemul */
74
/** Putchar that works with gxemul */
64
void gxemul_write(chardev_t *dev, const char ch)
75
void gxemul_write(chardev_t *dev, const char ch)
65
{
76
{
66
    *((char *) GXEMUL_VIDEORAM) = ch;
77
    *((char *) GXEMUL_VIDEORAM) = ch;
67
}
78
}
68
 
79
 
69
/* Called from getc(). */
80
/* Called from getc(). */
70
void gxemul_enable(chardev_t *dev)
81
void gxemul_enable(chardev_t *dev)
71
{
82
{
72
//  cp0_unmask_int(GXEMUL_KBD_IRQ);
83
//  cp0_unmask_int(GXEMUL_KBD_IRQ);
-
 
84
    gxemul_irqc_unmask(GXEMUL_KBD_IRQ);
73
}
85
}
74
 
86
 
75
/* Called from getc(). */
87
/* Called from getc(). */
76
void gxemul_disable(chardev_t *dev)
88
void gxemul_disable(chardev_t *dev)
77
{
89
{
78
//  cp0_mask_int(GXEMUL_KBD_IRQ);
90
//  cp0_mask_int(GXEMUL_KBD_IRQ);
-
 
91
    gxemul_irqc_mask(GXEMUL_KBD_IRQ);
79
}
92
}
80
 
93
 
81
/** Read character using polling, assume interrupts disabled */
94
/** Read character using polling, assume interrupts disabled */
82
static char gxemul_do_read(chardev_t *dev)
95
static char gxemul_do_read(chardev_t *dev)
83
{
96
{
84
    char ch;
97
    char ch;
85
 
98
 
86
    while (1) {
99
    while (1) {
87
        ch = *((volatile char *) GXEMUL_KBD_ADDRESS);
100
        ch = *((volatile char *) GXEMUL_KBD_ADDRESS);
88
        if (ch) {
101
        if (ch) {
89
            if (ch == '\r')
102
            if (ch == '\r')
90
                return '\n';
103
                return '\n';
91
            if (ch == 0x7f)
104
            if (ch == 0x7f)
92
                return '\b';
105
                return '\b';
93
            return ch;
106
            return ch;
94
        }
107
        }
95
    }
108
    }
96
}
109
}
97
 
110
 
98
/** Process keyboard interrupt. */
111
/** Process keyboard interrupt. */
99
static void gxemul_irq_handler(irq_t *irq, void *arg, ...)
112
static void gxemul_irq_handler(irq_t *irq, void *arg, ...)
100
{
113
{
101
    if ((irq->notif_cfg.notify) && (irq->notif_cfg.answerbox))
114
    if ((irq->notif_cfg.notify) && (irq->notif_cfg.answerbox))
102
        ipc_irq_send_notif(irq);
115
        ipc_irq_send_notif(irq);
103
    else {
116
    else {
104
        char ch = 0;
117
        char ch = 0;
105
       
118
       
106
            ch = *((char *) GXEMUL_KBD_ADDRESS);
119
            ch = *((char *) GXEMUL_KBD_ADDRESS);
107
            if (ch =='\r')
120
            if (ch =='\r')
108
                ch = '\n';
121
                ch = '\n';
109
            if (ch == 0x7f)
122
            if (ch == 0x7f)
110
                ch = '\b';
123
                ch = '\b';
111
            chardev_push_character(&console, ch);
124
            //chardev_push_character(&console, ch);
-
 
125
            printf("%c", ch);
112
    }
126
    }
113
}
127
}
114
 
128
 
115
static irq_ownership_t gxemul_claim(void)
129
static irq_ownership_t gxemul_claim(void)
116
{
130
{
117
    return IRQ_ACCEPT;
131
    return IRQ_ACCEPT;
118
}
132
}
119
 
133
 
120
void gxemul_kbd_grab(void)
134
void gxemul_kbd_grab(void)
121
{
135
{
122
    ipl_t ipl = interrupts_disable();
136
    ipl_t ipl = interrupts_disable();
123
    spinlock_lock(&msim_irq.lock);
137
    spinlock_lock(&msim_irq.lock);
124
    gxemul_irq.notif_cfg.notify = false;
138
    gxemul_irq.notif_cfg.notify = false;
125
    spinlock_unlock(&gxemul_irq.lock);
139
    spinlock_unlock(&gxemul_irq.lock);
126
    interrupts_restore(ipl);
140
    interrupts_restore(ipl);
127
}
141
}
128
 
142
 
129
void gxemul_kbd_release(void)
143
void gxemul_kbd_release(void)
130
{
144
{
131
    ipl_t ipl = interrupts_disable();
145
    ipl_t ipl = interrupts_disable();
132
    spinlock_lock(&gxemul_irq.lock);
146
    spinlock_lock(&gxemul_irq.lock);
133
    if (gxemul_irq.notif_cfg.answerbox)
147
    if (gxemul_irq.notif_cfg.answerbox)
134
        gxemul_irq.notif_cfg.notify = true;
148
        gxemul_irq.notif_cfg.notify = true;
135
    spinlock_unlock(&gxemul_irq.lock);
149
    spinlock_unlock(&gxemul_irq.lock);
136
    interrupts_restore(ipl);
150
    interrupts_restore(ipl);
137
}
151
}
138
 
152
 
139
 
153
 
140
/* Return console object representing msim console */
154
/** Return console object representing msim console */
141
void gxemul_console(devno_t devno)
155
void gxemul_console(devno_t devno)
142
{
156
{
143
    chardev_initialize("msim_console", &console, &gxemul_ops);
157
    chardev_initialize("msim_console", &console, &gxemul_ops);
144
    stdin = &console;
158
    stdin = &console;
145
    stdout = &console;
159
    stdout = &console;
146
   
160
   
147
    irq_initialize(&gxemul_irq);
161
    irq_initialize(&gxemul_irq);
148
    gxemul_irq.devno = devno;
162
    gxemul_irq.devno = devno;
149
    gxemul_irq.inr = GXEMUL_KBD_IRQ;
163
    gxemul_irq.inr = GXEMUL_KBD_IRQ;
150
    gxemul_irq.claim = gxemul_claim;
164
    gxemul_irq.claim = gxemul_claim;
151
    gxemul_irq.handler = gxemul_irq_handler;
165
    gxemul_irq.handler = gxemul_irq_handler;
152
    irq_register(&gxemul_irq);
166
    irq_register(&gxemul_irq);
153
   
167
   
154
//  cp0_unmask_int(GXEMUL_KBD_IRQ);
168
//  cp0_unmask_int(GXEMUL_KBD_IRQ);
-
 
169
    gxemul_irqc_unmask(GXEMUL_KBD_IRQ);
155
   
170
   
156
    sysinfo_set_item_val("kbd", NULL, true);
171
    sysinfo_set_item_val("kbd", NULL, true);
157
    sysinfo_set_item_val("kbd.devno", NULL, devno);
172
    sysinfo_set_item_val("kbd.devno", NULL, devno);
158
    sysinfo_set_item_val("kbd.inr", NULL, GXEMUL_KBD_IRQ);
173
    sysinfo_set_item_val("kbd.inr", NULL, GXEMUL_KBD_IRQ);
159
    sysinfo_set_item_val("kbd.address.virtual", NULL, GXEMUL_KBD_ADDRESS);
174
    sysinfo_set_item_val("kbd.address.virtual", NULL, GXEMUL_KBD_ADDRESS);
160
}
175
}
161
 
176
 
-
 
177
/** Return the mask of active interrupts. */
-
 
178
inline uint32_t gxemul_irqc_get_sources(void)
-
 
179
{
-
 
180
    return *(uint32_t*) GXEMUL_IRQC;
-
 
181
}
-
 
182
 
-
 
183
/** Masks interrupt.
-
 
184
 *
-
 
185
 * @param irq interrupt number
-
 
186
 */
-
 
187
inline void gxemul_irqc_mask(uint32_t irq)
-
 
188
{
-
 
189
    *(uint32_t*) GXEMUL_IRQC_MASK = irq;
-
 
190
}
-
 
191
 
-
 
192
/** Unmasks interrupt.
-
 
193
 *
-
 
194
 * @param irq interrupt number
-
 
195
 */
-
 
196
inline void gxemul_irqc_unmask(uint32_t irq)
-
 
197
{
-
 
198
    *(uint32_t*) GXEMUL_IRQC_UNMASK = irq;
-
 
199
}
-
 
200
 
-
 
201
 
-
 
202
/** Starts gxemul Real Time Clock device, which asserts regular interrupts.
-
 
203
 *
-
 
204
 * @param frequency interrupts frequency (0 disables RTC)
-
 
205
 */
-
 
206
void gxemul_timer_start(uint32_t frequency)
-
 
207
{
-
 
208
    *(uint32_t*) GXEMUL_RTC_FREQ = frequency;
-
 
209
}
-
 
210
 
-
 
211
static irq_ownership_t gxemul_timer_claim(void)
-
 
212
{
-
 
213
    return IRQ_ACCEPT;
-
 
214
}
-
 
215
 
-
 
216
static void gxemul_timer_irq_handler(irq_t *irq, void *arg, ...)
-
 
217
{
-
 
218
    /* TODO time drifts ??
-
 
219
    unsigned long drift;
-
 
220
 
-
 
221
    drift = cp0_count_read() - nextcount;
-
 
222
    while (drift > cp0_compare_value) {
-
 
223
        drift -= cp0_compare_value;
-
 
224
        CPU->missed_clock_ticks++;
-
 
225
    }
-
 
226
    nextcount = cp0_count_read() + cp0_compare_value - drift;
-
 
227
    cp0_compare_write(nextcount);
-
 
228
    */
-
 
229
 
-
 
230
    /*
-
 
231
    * We are holding a lock which prevents preemption.
-
 
232
    * Release the lock, call clock() and reacquire the lock again.
-
 
233
    */
-
 
234
    spinlock_unlock(&irq->lock);
-
 
235
    //clock();
-
 
236
    puts(" ");
-
 
237
    spinlock_lock(&irq->lock);
-
 
238
   
-
 
239
    /* TODO what's that? *
-
 
240
    if (virtual_timer_fnc != NULL)
-
 
241
        virtual_timer_fnc();
-
 
242
    */
-
 
243
}
-
 
244
 
-
 
245
/**
-
 
246
 * Initializes and registers timer interrupt handler.
-
 
247
 */
-
 
248
void gxemul_timer_irq_init()
-
 
249
{
-
 
250
    irq_initialize(&gxemul_timer_irq);
-
 
251
    gxemul_timer_irq.devno = device_assign_devno();
-
 
252
    gxemul_timer_irq.inr = GXEMUL_TIMER_IRQ;
-
 
253
    gxemul_timer_irq.claim = gxemul_timer_claim;
-
 
254
    gxemul_timer_irq.handler = gxemul_timer_irq_handler;
-
 
255
    irq_register(&gxemul_timer_irq);
-
 
256
}
-
 
257
 
-
 
258
 
162
/** @}
259
/** @}
163
 */
260
 */
164
 
261