Rev 2326 | 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 |
||
33 | */ |
||
34 | |||
35 | #include <interrupt.h> |
||
36 | #include <ipc/irq.h> |
||
37 | #include <console/chardev.h> |
||
38 | #include <arch/drivers/gxemul.h> |
||
39 | #include <console/console.h> |
||
40 | #include <sysinfo/sysinfo.h> |
||
41 | #include <print.h> |
||
2245 | stepan | 42 | #include <ddi/device.h> |
2263 | kebrt | 43 | #include <mm/page.h> |
2274 | kebrt | 44 | #include <arch/machine.h> |
2326 | kebrt | 45 | #include <arch/debug/print.h> |
2215 | kebrt | 46 | |
2340 | kebrt | 47 | |
2215 | kebrt | 48 | /** Address 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 | |
2263 | kebrt | 62 | |
2245 | stepan | 63 | /** IRQs */ |
64 | #define GXEMUL_KBD_IRQ 2 |
||
65 | #define GXEMUL_TIMER_IRQ 4 |
||
66 | |||
2263 | kebrt | 67 | static gxemul_hw_map_t gxemul_hw_map; |
2215 | kebrt | 68 | static chardev_t console; |
69 | static irq_t gxemul_irq; |
||
2245 | stepan | 70 | static irq_t gxemul_timer_irq; |
2215 | kebrt | 71 | |
2290 | kebrt | 72 | static bool hw_map_init_called = false; |
73 | |||
2215 | kebrt | 74 | static void gxemul_write(chardev_t *dev, const char ch); |
75 | static void gxemul_enable(chardev_t *dev); |
||
76 | static void gxemul_disable(chardev_t *dev); |
||
77 | static char gxemul_do_read(chardev_t *dev); |
||
78 | |||
79 | static chardev_operations_t gxemul_ops = { |
||
80 | .resume = gxemul_enable, |
||
81 | .suspend = gxemul_disable, |
||
82 | .write = gxemul_write, |
||
83 | .read = gxemul_do_read, |
||
84 | }; |
||
85 | |||
2340 | kebrt | 86 | /** Return the mask of active interrupts. */ |
87 | static inline uint32_t gxemul_irqc_get_sources(void) |
||
88 | { |
||
89 | return *(uint32_t*) gxemul_hw_map.irqc; |
||
90 | } |
||
2263 | kebrt | 91 | |
2340 | kebrt | 92 | /** Masks interrupt. |
93 | * |
||
94 | * @param irq interrupt number |
||
95 | */ |
||
96 | static inline void gxemul_irqc_mask(uint32_t irq) |
||
97 | { |
||
98 | *(uint32_t*) gxemul_hw_map.irqc_mask = irq; |
||
99 | } |
||
100 | |||
101 | /** Unmasks interrupt. |
||
102 | * |
||
103 | * @param irq interrupt number |
||
104 | */ |
||
105 | static inline void gxemul_irqc_unmask(uint32_t irq) |
||
106 | { |
||
107 | *(uint32_t*) gxemul_hw_map.irqc_unmask = irq; |
||
108 | } |
||
109 | |||
2263 | kebrt | 110 | /** Initializes #gxemul_hw_map. */ |
2294 | kebrt | 111 | void machine_hw_map_init(void) |
2263 | kebrt | 112 | { |
113 | gxemul_hw_map.videoram = hw_map(GXEMUL_VIDEORAM, PAGE_SIZE); |
||
114 | gxemul_hw_map.kbd = hw_map(GXEMUL_KBD, PAGE_SIZE); |
||
115 | gxemul_hw_map.rtc = hw_map(GXEMUL_RTC, PAGE_SIZE); |
||
116 | gxemul_hw_map.irqc = hw_map(GXEMUL_IRQC, PAGE_SIZE); |
||
117 | |||
118 | gxemul_hw_map.rtc_freq = gxemul_hw_map.rtc + GXEMUL_RTC_FREQ_OFFSET; |
||
119 | gxemul_hw_map.rtc_ack = gxemul_hw_map.rtc + GXEMUL_RTC_ACK_OFFSET; |
||
120 | gxemul_hw_map.irqc_mask = gxemul_hw_map.irqc + GXEMUL_IRQC_MASK_OFFSET; |
||
121 | gxemul_hw_map.irqc_unmask = gxemul_hw_map.irqc + GXEMUL_IRQC_UNMASK_OFFSET; |
||
2264 | kebrt | 122 | |
2290 | kebrt | 123 | hw_map_init_called = true; |
2263 | kebrt | 124 | } |
125 | |||
2215 | kebrt | 126 | /** Putchar that works with gxemul */ |
2340 | kebrt | 127 | static void gxemul_write(chardev_t *dev, const char ch) |
2215 | kebrt | 128 | { |
2263 | kebrt | 129 | *((char *) gxemul_hw_map.videoram) = ch; |
2215 | kebrt | 130 | } |
131 | |||
132 | /* Called from getc(). */ |
||
2340 | kebrt | 133 | static void gxemul_enable(chardev_t *dev) |
2215 | kebrt | 134 | { |
2245 | stepan | 135 | gxemul_irqc_unmask(GXEMUL_KBD_IRQ); |
2215 | kebrt | 136 | } |
137 | |||
138 | /* Called from getc(). */ |
||
2340 | kebrt | 139 | static void gxemul_disable(chardev_t *dev) |
2215 | kebrt | 140 | { |
2245 | stepan | 141 | gxemul_irqc_mask(GXEMUL_KBD_IRQ); |
2215 | kebrt | 142 | } |
143 | |||
144 | /** Read character using polling, assume interrupts disabled */ |
||
145 | static char gxemul_do_read(chardev_t *dev) |
||
146 | { |
||
147 | char ch; |
||
148 | |||
149 | while (1) { |
||
2263 | kebrt | 150 | ch = *((volatile char *) gxemul_hw_map.kbd); |
2215 | kebrt | 151 | if (ch) { |
152 | if (ch == '\r') |
||
153 | return '\n'; |
||
154 | if (ch == 0x7f) |
||
155 | return '\b'; |
||
156 | return ch; |
||
157 | } |
||
158 | } |
||
159 | } |
||
160 | |||
161 | /** Process keyboard interrupt. */ |
||
162 | static void gxemul_irq_handler(irq_t *irq, void *arg, ...) |
||
163 | { |
||
164 | if ((irq->notif_cfg.notify) && (irq->notif_cfg.answerbox)) |
||
165 | ipc_irq_send_notif(irq); |
||
166 | else { |
||
167 | char ch = 0; |
||
168 | |||
2263 | kebrt | 169 | ch = *((char *) gxemul_hw_map.kbd); |
2215 | kebrt | 170 | if (ch =='\r') |
171 | ch = '\n'; |
||
172 | if (ch == 0x7f) |
||
173 | ch = '\b'; |
||
2284 | stepan | 174 | chardev_push_character(&console, ch); |
2215 | kebrt | 175 | } |
176 | } |
||
177 | |||
178 | static irq_ownership_t gxemul_claim(void) |
||
179 | { |
||
180 | return IRQ_ACCEPT; |
||
181 | } |
||
182 | |||
2274 | kebrt | 183 | void machine_grab_console(void) |
2215 | kebrt | 184 | { |
185 | ipl_t ipl = interrupts_disable(); |
||
2263 | kebrt | 186 | spinlock_lock(&gxemul_irq.lock); |
2215 | kebrt | 187 | gxemul_irq.notif_cfg.notify = false; |
188 | spinlock_unlock(&gxemul_irq.lock); |
||
189 | interrupts_restore(ipl); |
||
190 | } |
||
191 | |||
2274 | kebrt | 192 | void machine_release_console(void) |
2215 | kebrt | 193 | { |
194 | ipl_t ipl = interrupts_disable(); |
||
195 | spinlock_lock(&gxemul_irq.lock); |
||
196 | if (gxemul_irq.notif_cfg.answerbox) |
||
197 | gxemul_irq.notif_cfg.notify = true; |
||
198 | spinlock_unlock(&gxemul_irq.lock); |
||
199 | interrupts_restore(ipl); |
||
200 | } |
||
201 | |||
202 | |||
2263 | kebrt | 203 | /** Return console object representing gxemul console */ |
2274 | kebrt | 204 | void machine_console_init(devno_t devno) |
2215 | kebrt | 205 | { |
2263 | kebrt | 206 | chardev_initialize("gxemul_console", &console, &gxemul_ops); |
2215 | kebrt | 207 | stdin = &console; |
208 | stdout = &console; |
||
209 | |||
210 | irq_initialize(&gxemul_irq); |
||
211 | gxemul_irq.devno = devno; |
||
212 | gxemul_irq.inr = GXEMUL_KBD_IRQ; |
||
213 | gxemul_irq.claim = gxemul_claim; |
||
214 | gxemul_irq.handler = gxemul_irq_handler; |
||
215 | irq_register(&gxemul_irq); |
||
216 | |||
2245 | stepan | 217 | gxemul_irqc_unmask(GXEMUL_KBD_IRQ); |
2215 | kebrt | 218 | |
219 | sysinfo_set_item_val("kbd", NULL, true); |
||
220 | sysinfo_set_item_val("kbd.devno", NULL, devno); |
||
221 | sysinfo_set_item_val("kbd.inr", NULL, GXEMUL_KBD_IRQ); |
||
2263 | kebrt | 222 | sysinfo_set_item_val("kbd.address.virtual", NULL, gxemul_hw_map.kbd); |
2215 | kebrt | 223 | } |
224 | |||
2245 | stepan | 225 | |
226 | |||
227 | /** Starts gxemul Real Time Clock device, which asserts regular interrupts. |
||
228 | * |
||
229 | * @param frequency interrupts frequency (0 disables RTC) |
||
230 | */ |
||
2340 | kebrt | 231 | static void gxemul_timer_start(uint32_t frequency) |
2245 | stepan | 232 | { |
2263 | kebrt | 233 | *(uint32_t*) gxemul_hw_map.rtc_freq = frequency; |
2245 | stepan | 234 | } |
235 | |||
236 | static irq_ownership_t gxemul_timer_claim(void) |
||
237 | { |
||
238 | return IRQ_ACCEPT; |
||
239 | } |
||
240 | |||
241 | static void gxemul_timer_irq_handler(irq_t *irq, void *arg, ...) |
||
242 | { |
||
243 | /* TODO time drifts ?? |
||
244 | unsigned long drift; |
||
245 | |||
246 | drift = cp0_count_read() - nextcount; |
||
247 | while (drift > cp0_compare_value) { |
||
248 | drift -= cp0_compare_value; |
||
249 | CPU->missed_clock_ticks++; |
||
250 | } |
||
251 | nextcount = cp0_count_read() + cp0_compare_value - drift; |
||
252 | cp0_compare_write(nextcount); |
||
253 | */ |
||
254 | |||
255 | /* |
||
256 | * We are holding a lock which prevents preemption. |
||
257 | * Release the lock, call clock() and reacquire the lock again. |
||
258 | */ |
||
259 | spinlock_unlock(&irq->lock); |
||
2286 | stepan | 260 | clock(); |
2245 | stepan | 261 | spinlock_lock(&irq->lock); |
2261 | stepan | 262 | |
263 | /* acknowledge tick */ |
||
2263 | kebrt | 264 | *(uint32_t*) gxemul_hw_map.rtc_ack = 0; |
2245 | stepan | 265 | |
266 | /* TODO what's that? * |
||
267 | if (virtual_timer_fnc != NULL) |
||
268 | virtual_timer_fnc(); |
||
269 | */ |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Initializes and registers timer interrupt handler. |
||
274 | */ |
||
2340 | kebrt | 275 | static void gxemul_timer_irq_init() |
2245 | stepan | 276 | { |
277 | irq_initialize(&gxemul_timer_irq); |
||
278 | gxemul_timer_irq.devno = device_assign_devno(); |
||
279 | gxemul_timer_irq.inr = GXEMUL_TIMER_IRQ; |
||
280 | gxemul_timer_irq.claim = gxemul_timer_claim; |
||
281 | gxemul_timer_irq.handler = gxemul_timer_irq_handler; |
||
2261 | stepan | 282 | |
2245 | stepan | 283 | irq_register(&gxemul_timer_irq); |
284 | } |
||
285 | |||
2274 | kebrt | 286 | void machine_timer_irq_start() |
2263 | kebrt | 287 | { |
2274 | kebrt | 288 | gxemul_timer_irq_init(); |
289 | gxemul_timer_start(GXEMUL_TIMER_FREQ); |
||
290 | } |
||
291 | |||
292 | size_t machine_get_memory_size(void) |
||
293 | { |
||
2263 | kebrt | 294 | return *((int*)(GXEMUL_MP + GXEMUL_MP_MEMSIZE_OFFSET)); |
295 | } |
||
2245 | stepan | 296 | |
2300 | kebrt | 297 | void machine_debug_putc(char ch) |
298 | { |
||
299 | char * addr = 0; |
||
2290 | kebrt | 300 | if (!hw_map_init_called) { |
2300 | kebrt | 301 | addr = (char *) GXEMUL_KBD; |
2290 | kebrt | 302 | } else { |
2300 | kebrt | 303 | addr = (char *) gxemul_hw_map.videoram; |
2290 | kebrt | 304 | } |
2300 | kebrt | 305 | |
306 | *(addr) = ch; |
||
2264 | kebrt | 307 | } |
308 | |||
2300 | kebrt | 309 | void machine_cpu_halt(void) |
310 | { |
||
311 | char * addr = 0; |
||
312 | if (!hw_map_init_called) { |
||
313 | addr = (char *) GXEMUL_KBD; |
||
314 | } else { |
||
315 | addr = (char *) gxemul_hw_map.videoram; |
||
316 | } |
||
317 | |||
318 | *(addr + GXEMUL_HALT_OFFSET) = '\0'; |
||
319 | } |
||
320 | |||
2306 | kebrt | 321 | void machine_irq_exception(int exc_no, istate_t *istate) |
322 | { |
||
2340 | kebrt | 323 | /* switch to Undefined mode */ |
324 | /* |
||
325 | asm volatile( |
||
326 | "stmfd sp!, {r0-r3}\n" |
||
327 | "mov r1, sp\n" |
||
328 | "mov r2, lr\n" |
||
329 | "mrs r3, spsr\n" |
||
330 | "mrs r0, cpsr\n" |
||
331 | "bic r0, r0, #0x1f\n" |
||
332 | "orr r0, r0, #0x1b\n" |
||
333 | "msr cpsr_c, r0\n" |
||
334 | "mov sp, r1\n" |
||
335 | "mov lr, r2\n" |
||
336 | "msr spsr, r3\n" |
||
337 | "ldmfd sp!, {r0-r3}\n" |
||
338 | ); |
||
339 | */ |
||
340 | |||
2306 | kebrt | 341 | uint32_t sources = gxemul_irqc_get_sources(); |
342 | int i = 0; |
||
343 | for (; i < GXEMUL_IRQC_MAX_IRQ; i++) { |
||
344 | if (sources & (1 << i)) { |
||
345 | irq_t *irq = irq_dispatch_and_lock(i); |
||
346 | if (irq) { |
||
347 | /* The IRQ handler was found. */ |
||
348 | irq->handler(irq, irq->arg); |
||
349 | spinlock_unlock(&irq->lock); |
||
350 | } else { |
||
351 | /* Spurious interrupt.*/ |
||
352 | dprintf("cpu%d: spurious interrupt (inum=%d)\n", CPU->id, i); |
||
353 | } |
||
354 | } |
||
355 | } |
||
356 | /* TODO remove after testing the above code |
||
357 | noirq = 0; |
||
358 | if (i == CONSOLE_IRQ) { |
||
359 | char readchar = *(char*)0x10000000; |
||
360 | if (readchar == 0) { |
||
361 | aux_puts("?"); |
||
362 | } |
||
363 | else { |
||
364 | dprintf("%c", readchar); |
||
365 | } |
||
366 | |||
367 | } |
||
368 | else if (i == TIMER_IRQ) { |
||
369 | dprintf("\n.\n"); |
||
370 | //acknowledge |
||
371 | *(uint32_t*)0x15000110 = 0; |
||
372 | } |
||
373 | } |
||
374 | } |
||
2300 | kebrt | 375 | |
2306 | kebrt | 376 | if (noirq) |
377 | aux_puts("IRQ exception without source\n");*/ |
||
378 | } |
||
379 | |||
2340 | kebrt | 380 | |
2215 | kebrt | 381 | /** @} |
382 | */ |