Rev 2360 | 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 | |
2355 | stepan | 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 | |
2245 | stepan | 64 | /** IRQs */ |
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; |
70 | static irq_t gxemul_irq; |
||
2245 | stepan | 71 | static irq_t gxemul_timer_irq; |
2215 | kebrt | 72 | |
2290 | kebrt | 73 | static bool hw_map_init_called = false; |
74 | |||
2215 | kebrt | 75 | static void gxemul_write(chardev_t *dev, const char ch); |
76 | static void gxemul_enable(chardev_t *dev); |
||
77 | static void gxemul_disable(chardev_t *dev); |
||
78 | static char gxemul_do_read(chardev_t *dev); |
||
79 | |||
80 | static chardev_operations_t gxemul_ops = { |
||
81 | .resume = gxemul_enable, |
||
82 | .suspend = gxemul_disable, |
||
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 | |
2215 | kebrt | 132 | /** Putchar that works with gxemul */ |
2340 | kebrt | 133 | static void gxemul_write(chardev_t *dev, const char ch) |
2215 | kebrt | 134 | { |
2263 | kebrt | 135 | *((char *) gxemul_hw_map.videoram) = ch; |
2215 | kebrt | 136 | } |
137 | |||
2357 | kebrt | 138 | |
2215 | kebrt | 139 | /* Called from getc(). */ |
2340 | kebrt | 140 | static void gxemul_enable(chardev_t *dev) |
2215 | kebrt | 141 | { |
2245 | stepan | 142 | gxemul_irqc_unmask(GXEMUL_KBD_IRQ); |
2215 | kebrt | 143 | } |
144 | |||
2357 | kebrt | 145 | |
2215 | kebrt | 146 | /* Called from getc(). */ |
2340 | kebrt | 147 | static void gxemul_disable(chardev_t *dev) |
2215 | kebrt | 148 | { |
2245 | stepan | 149 | gxemul_irqc_mask(GXEMUL_KBD_IRQ); |
2215 | kebrt | 150 | } |
151 | |||
2357 | kebrt | 152 | |
153 | /** Read character using polling, assume interrupts disabled. |
||
154 | * |
||
155 | * @param dev Not used. |
||
156 | */ |
||
2215 | kebrt | 157 | static char gxemul_do_read(chardev_t *dev) |
158 | { |
||
159 | char ch; |
||
160 | |||
161 | while (1) { |
||
2263 | kebrt | 162 | ch = *((volatile char *) gxemul_hw_map.kbd); |
2215 | kebrt | 163 | if (ch) { |
164 | if (ch == '\r') |
||
165 | return '\n'; |
||
166 | if (ch == 0x7f) |
||
167 | return '\b'; |
||
168 | return ch; |
||
169 | } |
||
170 | } |
||
171 | } |
||
172 | |||
2357 | kebrt | 173 | |
2215 | kebrt | 174 | /** Process keyboard interrupt. */ |
175 | static void gxemul_irq_handler(irq_t *irq, void *arg, ...) |
||
176 | { |
||
2357 | kebrt | 177 | if ((irq->notif_cfg.notify) && (irq->notif_cfg.answerbox)) { |
2215 | kebrt | 178 | ipc_irq_send_notif(irq); |
2357 | kebrt | 179 | } else { |
2215 | kebrt | 180 | char ch = 0; |
181 | |||
2357 | kebrt | 182 | ch = *((char *) gxemul_hw_map.kbd); |
183 | if (ch == '\r') { |
||
184 | ch = '\n'; |
||
185 | } |
||
186 | if (ch == 0x7f) { |
||
187 | ch = '\b'; |
||
188 | } |
||
189 | chardev_push_character(&console, ch); |
||
2215 | kebrt | 190 | } |
191 | } |
||
192 | |||
2357 | kebrt | 193 | |
2215 | kebrt | 194 | static irq_ownership_t gxemul_claim(void) |
195 | { |
||
196 | return IRQ_ACCEPT; |
||
197 | } |
||
198 | |||
2357 | kebrt | 199 | |
2358 | kebrt | 200 | void gxemul_grab_console(void) |
2215 | kebrt | 201 | { |
202 | ipl_t ipl = interrupts_disable(); |
||
2263 | kebrt | 203 | spinlock_lock(&gxemul_irq.lock); |
2215 | kebrt | 204 | gxemul_irq.notif_cfg.notify = false; |
205 | spinlock_unlock(&gxemul_irq.lock); |
||
206 | interrupts_restore(ipl); |
||
207 | } |
||
208 | |||
2357 | kebrt | 209 | |
2358 | kebrt | 210 | void gxemul_release_console(void) |
2215 | kebrt | 211 | { |
212 | ipl_t ipl = interrupts_disable(); |
||
213 | spinlock_lock(&gxemul_irq.lock); |
||
2357 | kebrt | 214 | if (gxemul_irq.notif_cfg.answerbox) { |
2215 | kebrt | 215 | gxemul_irq.notif_cfg.notify = true; |
2357 | kebrt | 216 | } |
2215 | kebrt | 217 | spinlock_unlock(&gxemul_irq.lock); |
218 | interrupts_restore(ipl); |
||
219 | } |
||
220 | |||
221 | |||
2357 | kebrt | 222 | /** Initializes console object representing gxemul console. |
223 | * |
||
224 | * @param Console device number. |
||
225 | */ |
||
2358 | kebrt | 226 | void gxemul_console_init(devno_t devno) |
2215 | kebrt | 227 | { |
2263 | kebrt | 228 | chardev_initialize("gxemul_console", &console, &gxemul_ops); |
2215 | kebrt | 229 | stdin = &console; |
230 | stdout = &console; |
||
231 | |||
232 | irq_initialize(&gxemul_irq); |
||
233 | gxemul_irq.devno = devno; |
||
234 | gxemul_irq.inr = GXEMUL_KBD_IRQ; |
||
235 | gxemul_irq.claim = gxemul_claim; |
||
236 | gxemul_irq.handler = gxemul_irq_handler; |
||
237 | irq_register(&gxemul_irq); |
||
238 | |||
2245 | stepan | 239 | gxemul_irqc_unmask(GXEMUL_KBD_IRQ); |
2215 | kebrt | 240 | |
241 | sysinfo_set_item_val("kbd", NULL, true); |
||
242 | sysinfo_set_item_val("kbd.devno", NULL, devno); |
||
243 | sysinfo_set_item_val("kbd.inr", NULL, GXEMUL_KBD_IRQ); |
||
2263 | kebrt | 244 | sysinfo_set_item_val("kbd.address.virtual", NULL, gxemul_hw_map.kbd); |
2215 | kebrt | 245 | } |
246 | |||
2245 | stepan | 247 | |
248 | |||
249 | /** Starts gxemul Real Time Clock device, which asserts regular interrupts. |
||
250 | * |
||
2357 | kebrt | 251 | * @param frequency Interrupts frequency (0 disables RTC). |
2245 | stepan | 252 | */ |
2340 | kebrt | 253 | static void gxemul_timer_start(uint32_t frequency) |
2245 | stepan | 254 | { |
2263 | kebrt | 255 | *(uint32_t*) gxemul_hw_map.rtc_freq = frequency; |
2245 | stepan | 256 | } |
257 | |||
2357 | kebrt | 258 | |
2245 | stepan | 259 | static irq_ownership_t gxemul_timer_claim(void) |
260 | { |
||
261 | return IRQ_ACCEPT; |
||
262 | } |
||
263 | |||
2357 | kebrt | 264 | |
2355 | stepan | 265 | /** Timer interrupt handler. |
266 | * |
||
2357 | kebrt | 267 | * @param irq Interrupt information. |
268 | * @param arg Not used. |
||
2355 | stepan | 269 | */ |
2245 | stepan | 270 | static void gxemul_timer_irq_handler(irq_t *irq, void *arg, ...) |
271 | { |
||
272 | /* TODO time drifts ?? |
||
273 | unsigned long drift; |
||
274 | |||
275 | drift = cp0_count_read() - nextcount; |
||
276 | while (drift > cp0_compare_value) { |
||
277 | drift -= cp0_compare_value; |
||
278 | CPU->missed_clock_ticks++; |
||
279 | } |
||
280 | nextcount = cp0_count_read() + cp0_compare_value - drift; |
||
281 | cp0_compare_write(nextcount); |
||
282 | */ |
||
283 | |||
284 | /* |
||
285 | * We are holding a lock which prevents preemption. |
||
286 | * Release the lock, call clock() and reacquire the lock again. |
||
287 | */ |
||
288 | spinlock_unlock(&irq->lock); |
||
2286 | stepan | 289 | clock(); |
2245 | stepan | 290 | spinlock_lock(&irq->lock); |
2261 | stepan | 291 | |
292 | /* acknowledge tick */ |
||
2263 | kebrt | 293 | *(uint32_t*) gxemul_hw_map.rtc_ack = 0; |
2245 | stepan | 294 | |
295 | /* TODO what's that? * |
||
296 | if (virtual_timer_fnc != NULL) |
||
297 | virtual_timer_fnc(); |
||
298 | */ |
||
299 | } |
||
300 | |||
2357 | kebrt | 301 | |
302 | /** Initializes and registers timer interrupt handler. */ |
||
2340 | kebrt | 303 | static void gxemul_timer_irq_init() |
2245 | stepan | 304 | { |
305 | irq_initialize(&gxemul_timer_irq); |
||
306 | gxemul_timer_irq.devno = device_assign_devno(); |
||
307 | gxemul_timer_irq.inr = GXEMUL_TIMER_IRQ; |
||
308 | gxemul_timer_irq.claim = gxemul_timer_claim; |
||
309 | gxemul_timer_irq.handler = gxemul_timer_irq_handler; |
||
2261 | stepan | 310 | |
2245 | stepan | 311 | irq_register(&gxemul_timer_irq); |
312 | } |
||
313 | |||
2357 | kebrt | 314 | |
2355 | stepan | 315 | /** Starts timer. |
316 | * |
||
317 | * Initiates regular timer interrupts after initializing |
||
318 | * corresponding interrupt handler. |
||
319 | */ |
||
2358 | kebrt | 320 | void gxemul_timer_irq_start() |
2263 | kebrt | 321 | { |
2274 | kebrt | 322 | gxemul_timer_irq_init(); |
323 | gxemul_timer_start(GXEMUL_TIMER_FREQ); |
||
324 | } |
||
325 | |||
2357 | kebrt | 326 | |
2355 | stepan | 327 | /** Returns the size of emulated memory. |
328 | * |
||
2357 | kebrt | 329 | * @return Size in bytes. |
2355 | stepan | 330 | */ |
2358 | kebrt | 331 | size_t gxemul_get_memory_size(void) |
2274 | kebrt | 332 | { |
2263 | kebrt | 333 | return *((int*)(GXEMUL_MP + GXEMUL_MP_MEMSIZE_OFFSET)); |
334 | } |
||
2245 | stepan | 335 | |
2357 | kebrt | 336 | |
2358 | kebrt | 337 | void gxemul_debug_putc(char ch) |
2300 | kebrt | 338 | { |
339 | char * addr = 0; |
||
2290 | kebrt | 340 | if (!hw_map_init_called) { |
2300 | kebrt | 341 | addr = (char *) GXEMUL_KBD; |
2290 | kebrt | 342 | } else { |
2300 | kebrt | 343 | addr = (char *) gxemul_hw_map.videoram; |
2290 | kebrt | 344 | } |
2300 | kebrt | 345 | |
346 | *(addr) = ch; |
||
2264 | kebrt | 347 | } |
348 | |||
2357 | kebrt | 349 | |
2355 | stepan | 350 | /** Stops gxemul. */ |
2358 | kebrt | 351 | void gxemul_cpu_halt(void) |
2300 | kebrt | 352 | { |
353 | char * addr = 0; |
||
354 | if (!hw_map_init_called) { |
||
355 | addr = (char *) GXEMUL_KBD; |
||
356 | } else { |
||
357 | addr = (char *) gxemul_hw_map.videoram; |
||
358 | } |
||
359 | |||
360 | *(addr + GXEMUL_HALT_OFFSET) = '\0'; |
||
361 | } |
||
362 | |||
2357 | kebrt | 363 | |
2355 | stepan | 364 | /** Gxemul specific interrupt exception handler. |
365 | * |
||
366 | * Determines sources of the interrupt from interrupt controller and |
||
367 | * calls high-level handlers for them. |
||
368 | * |
||
2357 | kebrt | 369 | * @param exc_no Interrupt exception number. |
370 | * @param istate Saved processor state. |
||
2355 | stepan | 371 | */ |
2358 | kebrt | 372 | void gxemul_irq_exception(int exc_no, istate_t *istate) |
2306 | kebrt | 373 | { |
374 | uint32_t sources = gxemul_irqc_get_sources(); |
||
375 | int i = 0; |
||
376 | for (; i < GXEMUL_IRQC_MAX_IRQ; i++) { |
||
377 | if (sources & (1 << i)) { |
||
378 | irq_t *irq = irq_dispatch_and_lock(i); |
||
379 | if (irq) { |
||
380 | /* The IRQ handler was found. */ |
||
381 | irq->handler(irq, irq->arg); |
||
382 | spinlock_unlock(&irq->lock); |
||
383 | } else { |
||
384 | /* Spurious interrupt.*/ |
||
385 | dprintf("cpu%d: spurious interrupt (inum=%d)\n", CPU->id, i); |
||
386 | } |
||
387 | } |
||
388 | } |
||
389 | } |
||
390 | |||
2340 | kebrt | 391 | |
2358 | kebrt | 392 | uintptr_t gxemul_get_fb_address(void) |
2357 | kebrt | 393 | { |
2360 | kebrt | 394 | return (uintptr_t) GXEMUL_FB; |
2357 | kebrt | 395 | } |
396 | |||
2215 | kebrt | 397 | /** @} |
398 | */ |