Rev 2412 | 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 | |||
29 | /** @addtogroup arm32 |
||
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> |
2215 | kebrt | 47 | |
2340 | kebrt | 48 | |
2414 | kebrt | 49 | /* Addresses of devices. */ |
2263 | kebrt | 50 | #define GXEMUL_VIDEORAM 0x10000000 |
51 | #define GXEMUL_KBD 0x10000000 |
||
2300 | kebrt | 52 | #define GXEMUL_HALT_OFFSET 0x10 |
2263 | kebrt | 53 | #define GXEMUL_RTC 0x15000000 |
54 | #define GXEMUL_RTC_FREQ_OFFSET 0x100 |
||
55 | #define GXEMUL_RTC_ACK_OFFSET 0x110 |
||
56 | #define GXEMUL_IRQC 0x16000000 |
||
57 | #define GXEMUL_IRQC_MASK_OFFSET 0x4 |
||
58 | #define GXEMUL_IRQC_UNMASK_OFFSET 0x8 |
||
59 | #define GXEMUL_MP 0x11000000 |
||
60 | #define GXEMUL_MP_MEMSIZE_OFFSET 0x0090 |
||
2340 | kebrt | 61 | #define GXEMUL_FB 0x12000000 |
2215 | kebrt | 62 | |
2263 | 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 | { |
||
91 | return *(uint32_t*) gxemul_hw_map.irqc; |
||
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 | { |
||
101 | *(uint32_t*) gxemul_hw_map.irqc_mask = irq; |
||
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 | { |
||
111 | *(uint32_t*) gxemul_hw_map.irqc_unmask = irq; |
||
112 | } |
||
113 | |||
2357 | kebrt | 114 | |
2263 | kebrt | 115 | /** Initializes #gxemul_hw_map. */ |
2358 | kebrt | 116 | void gxemul_hw_map_init(void) |
2263 | kebrt | 117 | { |
118 | gxemul_hw_map.videoram = hw_map(GXEMUL_VIDEORAM, PAGE_SIZE); |
||
119 | gxemul_hw_map.kbd = hw_map(GXEMUL_KBD, PAGE_SIZE); |
||
120 | gxemul_hw_map.rtc = hw_map(GXEMUL_RTC, PAGE_SIZE); |
||
121 | gxemul_hw_map.irqc = hw_map(GXEMUL_IRQC, PAGE_SIZE); |
||
122 | |||
123 | gxemul_hw_map.rtc_freq = gxemul_hw_map.rtc + GXEMUL_RTC_FREQ_OFFSET; |
||
124 | gxemul_hw_map.rtc_ack = gxemul_hw_map.rtc + GXEMUL_RTC_ACK_OFFSET; |
||
125 | gxemul_hw_map.irqc_mask = gxemul_hw_map.irqc + GXEMUL_IRQC_MASK_OFFSET; |
||
126 | gxemul_hw_map.irqc_unmask = gxemul_hw_map.irqc + GXEMUL_IRQC_UNMASK_OFFSET; |
||
2264 | kebrt | 127 | |
2290 | kebrt | 128 | hw_map_init_called = true; |
2263 | kebrt | 129 | } |
130 | |||
2357 | kebrt | 131 | |
2414 | kebrt | 132 | /** Putchar that works with gxemul. |
133 | * |
||
134 | * @param dev Not used. |
||
135 | * @param ch Characted to be printed. |
||
136 | */ |
||
2340 | kebrt | 137 | static void gxemul_write(chardev_t *dev, const char ch) |
2215 | kebrt | 138 | { |
2263 | kebrt | 139 | *((char *) gxemul_hw_map.videoram) = ch; |
2215 | kebrt | 140 | } |
141 | |||
2357 | kebrt | 142 | |
2414 | kebrt | 143 | /** Enables gxemul keyboard (interrupt unmasked). |
144 | * |
||
145 | * @param dev Not used. |
||
146 | * |
||
147 | * Called from getc(). |
||
148 | */ |
||
149 | static void gxemul_kbd_enable(chardev_t *dev) |
||
2215 | kebrt | 150 | { |
2245 | stepan | 151 | gxemul_irqc_unmask(GXEMUL_KBD_IRQ); |
2215 | kebrt | 152 | } |
153 | |||
2357 | kebrt | 154 | |
2414 | kebrt | 155 | /** Disables gxemul keyboard (interrupt masked). |
156 | * |
||
157 | * @param dev not used |
||
158 | * |
||
159 | * Called from getc(). |
||
160 | */ |
||
161 | static void gxemul_kbd_disable(chardev_t *dev) |
||
2215 | kebrt | 162 | { |
2245 | stepan | 163 | gxemul_irqc_mask(GXEMUL_KBD_IRQ); |
2215 | kebrt | 164 | } |
165 | |||
2357 | kebrt | 166 | |
167 | /** Read character using polling, assume interrupts disabled. |
||
168 | * |
||
169 | * @param dev Not used. |
||
170 | */ |
||
2215 | kebrt | 171 | static char gxemul_do_read(chardev_t *dev) |
172 | { |
||
173 | char ch; |
||
174 | |||
175 | while (1) { |
||
2263 | kebrt | 176 | ch = *((volatile char *) gxemul_hw_map.kbd); |
2215 | kebrt | 177 | if (ch) { |
178 | if (ch == '\r') |
||
179 | return '\n'; |
||
180 | if (ch == 0x7f) |
||
181 | return '\b'; |
||
182 | return ch; |
||
183 | } |
||
184 | } |
||
185 | } |
||
186 | |||
2357 | kebrt | 187 | |
2414 | kebrt | 188 | /** Process keyboard interrupt. |
189 | * |
||
190 | * @param irq IRQ information. |
||
191 | * @param arg Not used. |
||
192 | */ |
||
2215 | kebrt | 193 | static void gxemul_irq_handler(irq_t *irq, void *arg, ...) |
194 | { |
||
2357 | kebrt | 195 | if ((irq->notif_cfg.notify) && (irq->notif_cfg.answerbox)) { |
2215 | kebrt | 196 | ipc_irq_send_notif(irq); |
2357 | kebrt | 197 | } else { |
2215 | kebrt | 198 | char ch = 0; |
199 | |||
2357 | kebrt | 200 | ch = *((char *) gxemul_hw_map.kbd); |
201 | if (ch == '\r') { |
||
202 | ch = '\n'; |
||
203 | } |
||
204 | if (ch == 0x7f) { |
||
205 | ch = '\b'; |
||
206 | } |
||
207 | chardev_push_character(&console, ch); |
||
2215 | kebrt | 208 | } |
209 | } |
||
210 | |||
2357 | kebrt | 211 | |
2215 | kebrt | 212 | static irq_ownership_t gxemul_claim(void) |
213 | { |
||
214 | return IRQ_ACCEPT; |
||
215 | } |
||
216 | |||
2357 | kebrt | 217 | |
2412 | kebrt | 218 | /** Acquire console back for kernel. */ |
2358 | kebrt | 219 | void gxemul_grab_console(void) |
2215 | kebrt | 220 | { |
221 | ipl_t ipl = interrupts_disable(); |
||
2414 | kebrt | 222 | spinlock_lock(&gxemul_console_irq.lock); |
223 | gxemul_console_irq.notif_cfg.notify = false; |
||
224 | spinlock_unlock(&gxemul_console_irq.lock); |
||
2215 | kebrt | 225 | interrupts_restore(ipl); |
226 | } |
||
227 | |||
2357 | kebrt | 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 | |||
241 | |||
2357 | kebrt | 242 | /** Initializes console object representing gxemul console. |
243 | * |
||
244 | * @param Console device number. |
||
245 | */ |
||
2358 | kebrt | 246 | void gxemul_console_init(devno_t devno) |
2215 | kebrt | 247 | { |
2263 | kebrt | 248 | chardev_initialize("gxemul_console", &console, &gxemul_ops); |
2215 | kebrt | 249 | stdin = &console; |
250 | stdout = &console; |
||
251 | |||
2414 | kebrt | 252 | irq_initialize(&gxemul_console_irq); |
253 | gxemul_console_irq.devno = devno; |
||
254 | gxemul_console_irq.inr = GXEMUL_KBD_IRQ; |
||
255 | gxemul_console_irq.claim = gxemul_claim; |
||
256 | gxemul_console_irq.handler = gxemul_irq_handler; |
||
257 | irq_register(&gxemul_console_irq); |
||
2215 | kebrt | 258 | |
2245 | stepan | 259 | gxemul_irqc_unmask(GXEMUL_KBD_IRQ); |
2215 | kebrt | 260 | |
261 | sysinfo_set_item_val("kbd", NULL, true); |
||
262 | sysinfo_set_item_val("kbd.devno", NULL, devno); |
||
263 | sysinfo_set_item_val("kbd.inr", NULL, GXEMUL_KBD_IRQ); |
||
2263 | kebrt | 264 | sysinfo_set_item_val("kbd.address.virtual", NULL, gxemul_hw_map.kbd); |
2215 | kebrt | 265 | } |
266 | |||
2245 | stepan | 267 | |
268 | |||
269 | /** Starts gxemul Real Time Clock device, which asserts regular interrupts. |
||
270 | * |
||
2357 | kebrt | 271 | * @param frequency Interrupts frequency (0 disables RTC). |
2245 | stepan | 272 | */ |
2340 | kebrt | 273 | static void gxemul_timer_start(uint32_t frequency) |
2245 | stepan | 274 | { |
2263 | kebrt | 275 | *(uint32_t*) gxemul_hw_map.rtc_freq = frequency; |
2245 | stepan | 276 | } |
277 | |||
2357 | kebrt | 278 | |
2245 | stepan | 279 | static irq_ownership_t gxemul_timer_claim(void) |
280 | { |
||
281 | return IRQ_ACCEPT; |
||
282 | } |
||
283 | |||
2357 | kebrt | 284 | |
2355 | stepan | 285 | /** Timer interrupt handler. |
286 | * |
||
2357 | kebrt | 287 | * @param irq Interrupt information. |
288 | * @param arg Not used. |
||
2355 | stepan | 289 | */ |
2245 | stepan | 290 | static void gxemul_timer_irq_handler(irq_t *irq, void *arg, ...) |
291 | { |
||
292 | /* |
||
293 | * We are holding a lock which prevents preemption. |
||
294 | * Release the lock, call clock() and reacquire the lock again. |
||
295 | */ |
||
296 | spinlock_unlock(&irq->lock); |
||
2286 | stepan | 297 | clock(); |
2245 | stepan | 298 | spinlock_lock(&irq->lock); |
2261 | stepan | 299 | |
300 | /* acknowledge tick */ |
||
2263 | kebrt | 301 | *(uint32_t*) gxemul_hw_map.rtc_ack = 0; |
2245 | stepan | 302 | } |
303 | |||
2357 | kebrt | 304 | |
305 | /** Initializes and registers timer interrupt handler. */ |
||
2340 | kebrt | 306 | static void gxemul_timer_irq_init() |
2245 | stepan | 307 | { |
308 | irq_initialize(&gxemul_timer_irq); |
||
309 | gxemul_timer_irq.devno = device_assign_devno(); |
||
310 | gxemul_timer_irq.inr = GXEMUL_TIMER_IRQ; |
||
311 | gxemul_timer_irq.claim = gxemul_timer_claim; |
||
312 | gxemul_timer_irq.handler = gxemul_timer_irq_handler; |
||
2261 | stepan | 313 | |
2245 | stepan | 314 | irq_register(&gxemul_timer_irq); |
315 | } |
||
316 | |||
2357 | kebrt | 317 | |
2355 | stepan | 318 | /** Starts timer. |
319 | * |
||
320 | * Initiates regular timer interrupts after initializing |
||
321 | * corresponding interrupt handler. |
||
322 | */ |
||
2358 | kebrt | 323 | void gxemul_timer_irq_start() |
2263 | kebrt | 324 | { |
2274 | kebrt | 325 | gxemul_timer_irq_init(); |
326 | gxemul_timer_start(GXEMUL_TIMER_FREQ); |
||
327 | } |
||
328 | |||
2357 | kebrt | 329 | |
2355 | stepan | 330 | /** Returns the size of emulated memory. |
331 | * |
||
2357 | kebrt | 332 | * @return Size in bytes. |
2355 | stepan | 333 | */ |
2358 | kebrt | 334 | size_t gxemul_get_memory_size(void) |
2274 | kebrt | 335 | { |
2263 | kebrt | 336 | return *((int*)(GXEMUL_MP + GXEMUL_MP_MEMSIZE_OFFSET)); |
337 | } |
||
2245 | stepan | 338 | |
2357 | kebrt | 339 | |
2412 | kebrt | 340 | /** Prints a character. |
341 | * |
||
342 | * @param ch Character to be printed. |
||
343 | */ |
||
2358 | kebrt | 344 | void gxemul_debug_putc(char ch) |
2300 | kebrt | 345 | { |
346 | char * addr = 0; |
||
2290 | kebrt | 347 | if (!hw_map_init_called) { |
2300 | kebrt | 348 | addr = (char *) GXEMUL_KBD; |
2290 | kebrt | 349 | } else { |
2300 | kebrt | 350 | addr = (char *) gxemul_hw_map.videoram; |
2290 | kebrt | 351 | } |
2300 | kebrt | 352 | |
353 | *(addr) = ch; |
||
2264 | kebrt | 354 | } |
355 | |||
2357 | kebrt | 356 | |
2355 | stepan | 357 | /** Stops gxemul. */ |
2358 | kebrt | 358 | void gxemul_cpu_halt(void) |
2300 | kebrt | 359 | { |
360 | char * addr = 0; |
||
361 | if (!hw_map_init_called) { |
||
362 | addr = (char *) GXEMUL_KBD; |
||
363 | } else { |
||
364 | addr = (char *) gxemul_hw_map.videoram; |
||
365 | } |
||
366 | |||
367 | *(addr + GXEMUL_HALT_OFFSET) = '\0'; |
||
368 | } |
||
369 | |||
2357 | kebrt | 370 | |
2355 | stepan | 371 | /** Gxemul specific interrupt exception handler. |
372 | * |
||
373 | * Determines sources of the interrupt from interrupt controller and |
||
374 | * calls high-level handlers for them. |
||
375 | * |
||
2357 | kebrt | 376 | * @param exc_no Interrupt exception number. |
377 | * @param istate Saved processor state. |
||
2355 | stepan | 378 | */ |
2358 | kebrt | 379 | void gxemul_irq_exception(int exc_no, istate_t *istate) |
2306 | kebrt | 380 | { |
381 | uint32_t sources = gxemul_irqc_get_sources(); |
||
382 | int i = 0; |
||
383 | for (; i < GXEMUL_IRQC_MAX_IRQ; i++) { |
||
384 | if (sources & (1 << i)) { |
||
385 | irq_t *irq = irq_dispatch_and_lock(i); |
||
386 | if (irq) { |
||
387 | /* The IRQ handler was found. */ |
||
388 | irq->handler(irq, irq->arg); |
||
389 | spinlock_unlock(&irq->lock); |
||
390 | } else { |
||
391 | /* Spurious interrupt.*/ |
||
392 | dprintf("cpu%d: spurious interrupt (inum=%d)\n", CPU->id, i); |
||
393 | } |
||
394 | } |
||
395 | } |
||
396 | } |
||
397 | |||
2412 | kebrt | 398 | /** Returns address of framebuffer device. |
399 | * |
||
400 | * @return Address of framebuffer device. |
||
401 | */ |
||
2358 | kebrt | 402 | uintptr_t gxemul_get_fb_address(void) |
2357 | kebrt | 403 | { |
2360 | kebrt | 404 | return (uintptr_t) GXEMUL_FB; |
2357 | kebrt | 405 | } |
406 | |||
2215 | kebrt | 407 | /** @} |
408 | */ |