Rev 2340 | 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 | |
2355 | stepan | 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 | |
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 | |||
2355 | stepan | 86 | /** Returns the mask of active interrupts. */ |
2340 | kebrt | 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 | |||
2355 | stepan | 241 | /** Timer interrupt handler. |
242 | * |
||
243 | * @param irq interrupt information |
||
244 | */ |
||
2245 | stepan | 245 | static void gxemul_timer_irq_handler(irq_t *irq, void *arg, ...) |
246 | { |
||
247 | /* TODO time drifts ?? |
||
248 | unsigned long drift; |
||
249 | |||
250 | drift = cp0_count_read() - nextcount; |
||
251 | while (drift > cp0_compare_value) { |
||
252 | drift -= cp0_compare_value; |
||
253 | CPU->missed_clock_ticks++; |
||
254 | } |
||
255 | nextcount = cp0_count_read() + cp0_compare_value - drift; |
||
256 | cp0_compare_write(nextcount); |
||
257 | */ |
||
258 | |||
259 | /* |
||
260 | * We are holding a lock which prevents preemption. |
||
261 | * Release the lock, call clock() and reacquire the lock again. |
||
262 | */ |
||
263 | spinlock_unlock(&irq->lock); |
||
2286 | stepan | 264 | clock(); |
2245 | stepan | 265 | spinlock_lock(&irq->lock); |
2261 | stepan | 266 | |
267 | /* acknowledge tick */ |
||
2263 | kebrt | 268 | *(uint32_t*) gxemul_hw_map.rtc_ack = 0; |
2245 | stepan | 269 | |
270 | /* TODO what's that? * |
||
271 | if (virtual_timer_fnc != NULL) |
||
272 | virtual_timer_fnc(); |
||
273 | */ |
||
274 | } |
||
275 | |||
2355 | stepan | 276 | /** Initializes and registers timer interrupt handler. |
2245 | stepan | 277 | */ |
2340 | kebrt | 278 | static void gxemul_timer_irq_init() |
2245 | stepan | 279 | { |
280 | irq_initialize(&gxemul_timer_irq); |
||
281 | gxemul_timer_irq.devno = device_assign_devno(); |
||
282 | gxemul_timer_irq.inr = GXEMUL_TIMER_IRQ; |
||
283 | gxemul_timer_irq.claim = gxemul_timer_claim; |
||
284 | gxemul_timer_irq.handler = gxemul_timer_irq_handler; |
||
2261 | stepan | 285 | |
2245 | stepan | 286 | irq_register(&gxemul_timer_irq); |
287 | } |
||
288 | |||
2355 | stepan | 289 | /** Starts timer. |
290 | * |
||
291 | * Initiates regular timer interrupts after initializing |
||
292 | * corresponding interrupt handler. |
||
293 | */ |
||
2274 | kebrt | 294 | void machine_timer_irq_start() |
2263 | kebrt | 295 | { |
2274 | kebrt | 296 | gxemul_timer_irq_init(); |
297 | gxemul_timer_start(GXEMUL_TIMER_FREQ); |
||
298 | } |
||
299 | |||
2355 | stepan | 300 | /** Returns the size of emulated memory. |
301 | * |
||
302 | * @return size in bytes |
||
303 | */ |
||
2274 | kebrt | 304 | size_t machine_get_memory_size(void) |
305 | { |
||
2263 | kebrt | 306 | return *((int*)(GXEMUL_MP + GXEMUL_MP_MEMSIZE_OFFSET)); |
307 | } |
||
2245 | stepan | 308 | |
2300 | kebrt | 309 | void machine_debug_putc(char ch) |
310 | { |
||
311 | char * addr = 0; |
||
2290 | kebrt | 312 | if (!hw_map_init_called) { |
2300 | kebrt | 313 | addr = (char *) GXEMUL_KBD; |
2290 | kebrt | 314 | } else { |
2300 | kebrt | 315 | addr = (char *) gxemul_hw_map.videoram; |
2290 | kebrt | 316 | } |
2300 | kebrt | 317 | |
318 | *(addr) = ch; |
||
2264 | kebrt | 319 | } |
320 | |||
2355 | stepan | 321 | /** Stops gxemul. */ |
2300 | kebrt | 322 | void machine_cpu_halt(void) |
323 | { |
||
324 | char * addr = 0; |
||
325 | if (!hw_map_init_called) { |
||
326 | addr = (char *) GXEMUL_KBD; |
||
327 | } else { |
||
328 | addr = (char *) gxemul_hw_map.videoram; |
||
329 | } |
||
330 | |||
331 | *(addr + GXEMUL_HALT_OFFSET) = '\0'; |
||
332 | } |
||
333 | |||
2355 | stepan | 334 | /** Gxemul specific interrupt exception handler. |
335 | * |
||
336 | * Determines sources of the interrupt from interrupt controller and |
||
337 | * calls high-level handlers for them. |
||
338 | * |
||
339 | * @param exc_no interrupt exception number |
||
340 | * @param istate saved processor state |
||
341 | */ |
||
2306 | kebrt | 342 | void machine_irq_exception(int exc_no, istate_t *istate) |
343 | { |
||
344 | uint32_t sources = gxemul_irqc_get_sources(); |
||
345 | int i = 0; |
||
346 | for (; i < GXEMUL_IRQC_MAX_IRQ; i++) { |
||
347 | if (sources & (1 << i)) { |
||
348 | irq_t *irq = irq_dispatch_and_lock(i); |
||
349 | if (irq) { |
||
350 | /* The IRQ handler was found. */ |
||
351 | irq->handler(irq, irq->arg); |
||
352 | spinlock_unlock(&irq->lock); |
||
353 | } else { |
||
354 | /* Spurious interrupt.*/ |
||
355 | dprintf("cpu%d: spurious interrupt (inum=%d)\n", CPU->id, i); |
||
356 | } |
||
357 | } |
||
358 | } |
||
359 | } |
||
360 | |||
2340 | kebrt | 361 | |
2215 | kebrt | 362 | /** @} |
363 | */ |