Subversion Repositories HelenOS

Rev

Rev 3478 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2215 kebrt 1
/*
2263 kebrt 2
 * Copyright (c) 2007 Michal Kebrt, Petr Stepan
2215 kebrt 3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
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
15
 *   derived from this software without specific prior written permission.
16
 *
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
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
2419 kebrt 29
/** @addtogroup arm32gxemul
2215 kebrt 30
 * @{
31
 */
32
/** @file
2410 stepan 33
 *  @brief GXemul drivers.
2215 kebrt 34
 */
35
 
36
#include <interrupt.h>
37
#include <ipc/irq.h>
38
#include <console/chardev.h>
39
#include <arch/drivers/gxemul.h>
40
#include <console/console.h>
41
#include <sysinfo/sysinfo.h>
42
#include <print.h>
2245 stepan 43
#include <ddi/device.h>
2263 kebrt 44
#include <mm/page.h>
2274 kebrt 45
#include <arch/machine.h>
2326 kebrt 46
#include <arch/debug/print.h>
3529 pillai 47
#include <genarch/fb/fb.h>
48
#include <genarch/fb/visuals.h>
2215 kebrt 49
 
2414 kebrt 50
/* Addresses of devices. */
2263 kebrt 51
#define GXEMUL_VIDEORAM            0x10000000
52
#define GXEMUL_KBD                 0x10000000
2300 kebrt 53
#define GXEMUL_HALT_OFFSET         0x10
2263 kebrt 54
#define GXEMUL_RTC                 0x15000000
55
#define GXEMUL_RTC_FREQ_OFFSET     0x100
56
#define GXEMUL_RTC_ACK_OFFSET      0x110
57
#define GXEMUL_IRQC                0x16000000
58
#define GXEMUL_IRQC_MASK_OFFSET    0x4
59
#define GXEMUL_IRQC_UNMASK_OFFSET  0x8
60
#define GXEMUL_MP                  0x11000000
61
#define GXEMUL_MP_MEMSIZE_OFFSET   0x0090
2340 kebrt 62
#define GXEMUL_FB                  0x12000000
2215 kebrt 63
 
2414 kebrt 64
/* IRQs */
2245 stepan 65
#define GXEMUL_KBD_IRQ      2
66
#define GXEMUL_TIMER_IRQ    4
67
 
2263 kebrt 68
static gxemul_hw_map_t gxemul_hw_map;
2215 kebrt 69
static chardev_t console;
2414 kebrt 70
static irq_t gxemul_console_irq;
2245 stepan 71
static irq_t gxemul_timer_irq;
2215 kebrt 72
 
2290 kebrt 73
static bool hw_map_init_called = false;
74
 
2414 kebrt 75
static void gxemul_kbd_enable(chardev_t *dev);
76
static void gxemul_kbd_disable(chardev_t *dev);
2215 kebrt 77
static void gxemul_write(chardev_t *dev, const char ch);
78
static char gxemul_do_read(chardev_t *dev);
79
 
80
static chardev_operations_t gxemul_ops = {
2414 kebrt 81
    .resume = gxemul_kbd_enable,
82
    .suspend = gxemul_kbd_disable,
2215 kebrt 83
    .write = gxemul_write,
84
    .read = gxemul_do_read,
85
};
86
 
2357 kebrt 87
 
2355 stepan 88
/** Returns the mask of active interrupts. */
2340 kebrt 89
static inline uint32_t gxemul_irqc_get_sources(void)
90
{
2464 jermar 91
    return *((uint32_t *) gxemul_hw_map.irqc);
2340 kebrt 92
}
2263 kebrt 93
 
2357 kebrt 94
 
2340 kebrt 95
/** Masks interrupt.
96
 *
97
 * @param irq interrupt number
98
 */
99
static inline void gxemul_irqc_mask(uint32_t irq)
100
{
2464 jermar 101
    *((uint32_t *) gxemul_hw_map.irqc_mask) = irq;
2340 kebrt 102
}
103
 
2357 kebrt 104
 
2340 kebrt 105
/** Unmasks interrupt.
106
 *
107
 * @param irq interrupt number
108
 */
109
static inline void gxemul_irqc_unmask(uint32_t irq)
110
{
2464 jermar 111
    *((uint32_t *) gxemul_hw_map.irqc_unmask) = irq;
2340 kebrt 112
}
113
 
3529 pillai 114
/** Initializes the gxemul testarm frame buffer */
115
void gxemul_fb_init(void)
116
{
117
    fb_init(gxemul_get_fb_address(), 640, 480, 1920, VISUAL_RGB_8_8_8);
118
}
2357 kebrt 119
 
2263 kebrt 120
/** Initializes #gxemul_hw_map. */
2358 kebrt 121
void gxemul_hw_map_init(void)
2263 kebrt 122
{
123
    gxemul_hw_map.videoram = hw_map(GXEMUL_VIDEORAM, PAGE_SIZE);
124
    gxemul_hw_map.kbd = hw_map(GXEMUL_KBD, PAGE_SIZE);
125
    gxemul_hw_map.rtc = hw_map(GXEMUL_RTC, PAGE_SIZE);
126
    gxemul_hw_map.irqc = hw_map(GXEMUL_IRQC, PAGE_SIZE);
127
 
128
    gxemul_hw_map.rtc_freq = gxemul_hw_map.rtc + GXEMUL_RTC_FREQ_OFFSET;
129
    gxemul_hw_map.rtc_ack = gxemul_hw_map.rtc + GXEMUL_RTC_ACK_OFFSET;
130
    gxemul_hw_map.irqc_mask = gxemul_hw_map.irqc + GXEMUL_IRQC_MASK_OFFSET;
2464 jermar 131
    gxemul_hw_map.irqc_unmask = gxemul_hw_map.irqc +
132
        GXEMUL_IRQC_UNMASK_OFFSET;
2264 kebrt 133
 
2290 kebrt 134
    hw_map_init_called = true;
2263 kebrt 135
}
136
 
2357 kebrt 137
 
2414 kebrt 138
/** Putchar that works with gxemul.
139
 *
140
 * @param dev Not used.
141
 * @param ch Characted to be printed.
142
 */
2340 kebrt 143
static void gxemul_write(chardev_t *dev, const char ch)
2215 kebrt 144
{
2263 kebrt 145
    *((char *) gxemul_hw_map.videoram) = ch;
2215 kebrt 146
}
147
 
2414 kebrt 148
/** Enables gxemul keyboard (interrupt unmasked).
149
 *
150
 * @param dev Not used.
151
 *
152
 * Called from getc().
153
 */
154
static void gxemul_kbd_enable(chardev_t *dev)
2215 kebrt 155
{
2245 stepan 156
    gxemul_irqc_unmask(GXEMUL_KBD_IRQ);
2215 kebrt 157
}
158
 
2414 kebrt 159
/** Disables gxemul keyboard (interrupt masked).
160
 *
161
 * @param dev not used
162
 *
163
 * Called from getc().
164
 */
165
static void gxemul_kbd_disable(chardev_t *dev)
2215 kebrt 166
{
2245 stepan 167
    gxemul_irqc_mask(GXEMUL_KBD_IRQ);
2215 kebrt 168
}
169
 
2357 kebrt 170
/** Read character using polling, assume interrupts disabled.
171
 *
172
 *  @param dev Not used.
173
 */
2215 kebrt 174
static char gxemul_do_read(chardev_t *dev)
175
{
176
    char ch;
177
 
178
    while (1) {
2263 kebrt 179
        ch = *((volatile char *) gxemul_hw_map.kbd);
2215 kebrt 180
        if (ch) {
181
            if (ch == '\r')
182
                return '\n';
183
            if (ch == 0x7f)
184
                return '\b';
185
            return ch;
186
        }
187
    }
188
}
189
 
2414 kebrt 190
/** Process keyboard interrupt.
191
 *  
192
 *  @param irq IRQ information.
193
 *  @param arg Not used.
194
 */
2215 kebrt 195
static void gxemul_irq_handler(irq_t *irq, void *arg, ...)
196
{
2357 kebrt 197
    if ((irq->notif_cfg.notify) && (irq->notif_cfg.answerbox)) {
2215 kebrt 198
        ipc_irq_send_notif(irq);
2357 kebrt 199
    } else {
2215 kebrt 200
        char ch = 0;
201
 
2357 kebrt 202
        ch = *((char *) gxemul_hw_map.kbd);
203
        if (ch == '\r') {
204
            ch = '\n';
205
        }
206
        if (ch == 0x7f) {
207
            ch = '\b';
208
        }
209
        chardev_push_character(&console, ch);
2215 kebrt 210
    }
211
}
212
 
213
static irq_ownership_t gxemul_claim(void)
214
{
215
    return IRQ_ACCEPT;
216
}
217
 
2357 kebrt 218
 
2412 kebrt 219
/** Acquire console back for kernel. */
2358 kebrt 220
void gxemul_grab_console(void)
2215 kebrt 221
{
222
    ipl_t ipl = interrupts_disable();
2414 kebrt 223
    spinlock_lock(&gxemul_console_irq.lock);
224
    gxemul_console_irq.notif_cfg.notify = false;
225
    spinlock_unlock(&gxemul_console_irq.lock);
2215 kebrt 226
    interrupts_restore(ipl);
227
}
228
 
2412 kebrt 229
/** Return console to userspace. */
2358 kebrt 230
void gxemul_release_console(void)
2215 kebrt 231
{
232
    ipl_t ipl = interrupts_disable();
2414 kebrt 233
    spinlock_lock(&gxemul_console_irq.lock);
234
    if (gxemul_console_irq.notif_cfg.answerbox) {
235
        gxemul_console_irq.notif_cfg.notify = true;
2357 kebrt 236
    }
2414 kebrt 237
    spinlock_unlock(&gxemul_console_irq.lock);
2215 kebrt 238
    interrupts_restore(ipl);
239
}
240
 
2357 kebrt 241
/** Initializes console object representing gxemul console.
242
 *
2418 kebrt 243
 *  @param devno device number.
2357 kebrt 244
 */
2358 kebrt 245
void gxemul_console_init(devno_t devno)
2215 kebrt 246
{
2263 kebrt 247
    chardev_initialize("gxemul_console", &console, &gxemul_ops);
2215 kebrt 248
    stdin = &console;
249
    stdout = &console;
250
 
2414 kebrt 251
    irq_initialize(&gxemul_console_irq);
252
    gxemul_console_irq.devno = devno;
253
    gxemul_console_irq.inr = GXEMUL_KBD_IRQ;
254
    gxemul_console_irq.claim = gxemul_claim;
255
    gxemul_console_irq.handler = gxemul_irq_handler;
256
    irq_register(&gxemul_console_irq);
2215 kebrt 257
 
2245 stepan 258
    gxemul_irqc_unmask(GXEMUL_KBD_IRQ);
2215 kebrt 259
 
260
    sysinfo_set_item_val("kbd", NULL, true);
261
    sysinfo_set_item_val("kbd.devno", NULL, devno);
262
    sysinfo_set_item_val("kbd.inr", NULL, GXEMUL_KBD_IRQ);
2263 kebrt 263
    sysinfo_set_item_val("kbd.address.virtual", NULL, gxemul_hw_map.kbd);
2215 kebrt 264
}
265
 
2245 stepan 266
/** Starts gxemul Real Time Clock device, which asserts regular interrupts.
267
 *
2357 kebrt 268
 * @param frequency Interrupts frequency (0 disables RTC).
2245 stepan 269
 */
2340 kebrt 270
static void gxemul_timer_start(uint32_t frequency)
2245 stepan 271
{
2464 jermar 272
    *((uint32_t*) gxemul_hw_map.rtc_freq) = frequency;
2245 stepan 273
}
274
 
275
static irq_ownership_t gxemul_timer_claim(void)
276
{
277
    return IRQ_ACCEPT;
278
}
279
 
2355 stepan 280
/** Timer interrupt handler.
281
 *
2357 kebrt 282
 * @param irq Interrupt information.
283
 * @param arg Not used.
2355 stepan 284
 */
2245 stepan 285
static void gxemul_timer_irq_handler(irq_t *irq, void *arg, ...)
286
{
287
    /*
288
    * We are holding a lock which prevents preemption.
289
    * Release the lock, call clock() and reacquire the lock again.
290
    */
291
    spinlock_unlock(&irq->lock);
2286 stepan 292
    clock();
2245 stepan 293
    spinlock_lock(&irq->lock);
2261 stepan 294
 
295
    /* acknowledge tick */
2464 jermar 296
    *((uint32_t*) gxemul_hw_map.rtc_ack) = 0;
2245 stepan 297
}
298
 
2357 kebrt 299
/** Initializes and registers timer interrupt handler. */
2464 jermar 300
static void gxemul_timer_irq_init(void)
2245 stepan 301
{
302
    irq_initialize(&gxemul_timer_irq);
303
    gxemul_timer_irq.devno = device_assign_devno();
304
    gxemul_timer_irq.inr = GXEMUL_TIMER_IRQ;
305
    gxemul_timer_irq.claim = gxemul_timer_claim;
306
    gxemul_timer_irq.handler = gxemul_timer_irq_handler;
2261 stepan 307
 
2245 stepan 308
    irq_register(&gxemul_timer_irq);
309
}
310
 
2357 kebrt 311
 
2355 stepan 312
/** Starts timer.
313
 *
314
 * Initiates regular timer interrupts after initializing
315
 * corresponding interrupt handler.
316
 */
2464 jermar 317
void gxemul_timer_irq_start(void)
2263 kebrt 318
{
2274 kebrt 319
    gxemul_timer_irq_init();
320
    gxemul_timer_start(GXEMUL_TIMER_FREQ);
321
}
322
 
2355 stepan 323
/** Returns the size of emulated memory.
324
 *
2357 kebrt 325
 * @return Size in bytes.
2355 stepan 326
 */
2358 kebrt 327
size_t gxemul_get_memory_size(void)
2274 kebrt 328
{
2464 jermar 329
    return  *((int *) (GXEMUL_MP + GXEMUL_MP_MEMSIZE_OFFSET));
2263 kebrt 330
}
2245 stepan 331
 
2412 kebrt 332
/** Prints a character.
333
 *
334
 *  @param ch Character to be printed.
335
 */
2358 kebrt 336
void gxemul_debug_putc(char ch)
2300 kebrt 337
{
2464 jermar 338
    char *addr = 0;
2290 kebrt 339
    if (!hw_map_init_called) {
2300 kebrt 340
        addr = (char *) GXEMUL_KBD;
2290 kebrt 341
    } else {
2300 kebrt 342
        addr = (char *) gxemul_hw_map.videoram;
2290 kebrt 343
    }
2300 kebrt 344
 
345
    *(addr) = ch;
2264 kebrt 346
}
347
 
2355 stepan 348
/** Stops gxemul. */
2358 kebrt 349
void gxemul_cpu_halt(void)
2300 kebrt 350
{
351
    char * addr = 0;
352
    if (!hw_map_init_called) {
353
        addr = (char *) GXEMUL_KBD;
354
    } else {
355
        addr = (char *) gxemul_hw_map.videoram;
356
    }
357
 
358
    *(addr + GXEMUL_HALT_OFFSET) = '\0';
359
}
360
 
2355 stepan 361
/** Gxemul specific interrupt exception handler.
362
 *
363
 * Determines sources of the interrupt from interrupt controller and
364
 * calls high-level handlers for them.
365
 *
2357 kebrt 366
 * @param exc_no Interrupt exception number.
367
 * @param istate Saved processor state.
2355 stepan 368
 */
2358 kebrt 369
void gxemul_irq_exception(int exc_no, istate_t *istate)
2306 kebrt 370
{
371
    uint32_t sources = gxemul_irqc_get_sources();
2464 jermar 372
    int i;
373
 
374
    for (i = 0; i < GXEMUL_IRQC_MAX_IRQ; i++) {
2306 kebrt 375
        if (sources & (1 << i)) {
376
            irq_t *irq = irq_dispatch_and_lock(i);
377
            if (irq) {
378
                /* The IRQ handler was found. */
379
                irq->handler(irq, irq->arg);
380
                spinlock_unlock(&irq->lock);
381
            } else {
382
                /* Spurious interrupt.*/
2464 jermar 383
                dprintf("cpu%d: spurious interrupt (inum=%d)\n",
384
                    CPU->id, i);
2306 kebrt 385
            }
386
        }
387
    }
388
}
389
 
2412 kebrt 390
/** Returns address of framebuffer device.
391
 *
392
 *  @return Address of framebuffer device.
393
 */
2358 kebrt 394
uintptr_t gxemul_get_fb_address(void)
2357 kebrt 395
{
2360 kebrt 396
    return (uintptr_t) GXEMUL_FB;
2357 kebrt 397
}
398
 
2215 kebrt 399
/** @}
400
 */