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