Rev 2294 | 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> |
2215 | kebrt | 45 | |
46 | /** Address of devices. */ |
||
2263 | kebrt | 47 | #define GXEMUL_VIDEORAM 0x10000000 |
48 | #define GXEMUL_KBD 0x10000000 |
||
2300 | kebrt | 49 | #define GXEMUL_HALT_OFFSET 0x10 |
2263 | kebrt | 50 | #define GXEMUL_RTC 0x15000000 |
51 | #define GXEMUL_RTC_FREQ_OFFSET 0x100 |
||
52 | #define GXEMUL_RTC_ACK_OFFSET 0x110 |
||
53 | #define GXEMUL_IRQC 0x16000000 |
||
54 | #define GXEMUL_IRQC_MASK_OFFSET 0x4 |
||
55 | #define GXEMUL_IRQC_UNMASK_OFFSET 0x8 |
||
56 | #define GXEMUL_MP 0x11000000 |
||
57 | #define GXEMUL_MP_MEMSIZE_OFFSET 0x0090 |
||
2215 | kebrt | 58 | |
2263 | kebrt | 59 | |
2245 | stepan | 60 | /** IRQs */ |
61 | #define GXEMUL_KBD_IRQ 2 |
||
62 | #define GXEMUL_TIMER_IRQ 4 |
||
63 | |||
2263 | kebrt | 64 | static gxemul_hw_map_t gxemul_hw_map; |
2215 | kebrt | 65 | static chardev_t console; |
66 | static irq_t gxemul_irq; |
||
2245 | stepan | 67 | static irq_t gxemul_timer_irq; |
2215 | kebrt | 68 | |
2290 | kebrt | 69 | static bool hw_map_init_called = false; |
70 | |||
2215 | kebrt | 71 | static void gxemul_write(chardev_t *dev, const char ch); |
72 | static void gxemul_enable(chardev_t *dev); |
||
73 | static void gxemul_disable(chardev_t *dev); |
||
74 | static char gxemul_do_read(chardev_t *dev); |
||
75 | |||
76 | static chardev_operations_t gxemul_ops = { |
||
77 | .resume = gxemul_enable, |
||
78 | .suspend = gxemul_disable, |
||
79 | .write = gxemul_write, |
||
80 | .read = gxemul_do_read, |
||
81 | }; |
||
82 | |||
2263 | kebrt | 83 | |
84 | /** Initializes #gxemul_hw_map. */ |
||
2294 | kebrt | 85 | void machine_hw_map_init(void) |
2263 | kebrt | 86 | { |
87 | gxemul_hw_map.videoram = hw_map(GXEMUL_VIDEORAM, PAGE_SIZE); |
||
88 | gxemul_hw_map.kbd = hw_map(GXEMUL_KBD, PAGE_SIZE); |
||
89 | gxemul_hw_map.rtc = hw_map(GXEMUL_RTC, PAGE_SIZE); |
||
90 | gxemul_hw_map.irqc = hw_map(GXEMUL_IRQC, PAGE_SIZE); |
||
91 | |||
92 | gxemul_hw_map.rtc_freq = gxemul_hw_map.rtc + GXEMUL_RTC_FREQ_OFFSET; |
||
93 | gxemul_hw_map.rtc_ack = gxemul_hw_map.rtc + GXEMUL_RTC_ACK_OFFSET; |
||
94 | gxemul_hw_map.irqc_mask = gxemul_hw_map.irqc + GXEMUL_IRQC_MASK_OFFSET; |
||
95 | gxemul_hw_map.irqc_unmask = gxemul_hw_map.irqc + GXEMUL_IRQC_UNMASK_OFFSET; |
||
2264 | kebrt | 96 | |
2290 | kebrt | 97 | hw_map_init_called = true; |
2263 | kebrt | 98 | } |
99 | |||
2215 | kebrt | 100 | /** Putchar that works with gxemul */ |
101 | void gxemul_write(chardev_t *dev, const char ch) |
||
102 | { |
||
2263 | kebrt | 103 | *((char *) gxemul_hw_map.videoram) = ch; |
2215 | kebrt | 104 | } |
105 | |||
106 | /* Called from getc(). */ |
||
107 | void gxemul_enable(chardev_t *dev) |
||
108 | { |
||
109 | // cp0_unmask_int(GXEMUL_KBD_IRQ); |
||
2245 | stepan | 110 | gxemul_irqc_unmask(GXEMUL_KBD_IRQ); |
2215 | kebrt | 111 | } |
112 | |||
113 | /* Called from getc(). */ |
||
114 | void gxemul_disable(chardev_t *dev) |
||
115 | { |
||
116 | // cp0_mask_int(GXEMUL_KBD_IRQ); |
||
2245 | stepan | 117 | gxemul_irqc_mask(GXEMUL_KBD_IRQ); |
2215 | kebrt | 118 | } |
119 | |||
120 | /** Read character using polling, assume interrupts disabled */ |
||
121 | static char gxemul_do_read(chardev_t *dev) |
||
122 | { |
||
123 | char ch; |
||
124 | |||
125 | while (1) { |
||
2263 | kebrt | 126 | ch = *((volatile char *) gxemul_hw_map.kbd); |
2215 | kebrt | 127 | if (ch) { |
128 | if (ch == '\r') |
||
129 | return '\n'; |
||
130 | if (ch == 0x7f) |
||
131 | return '\b'; |
||
132 | return ch; |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 | |||
137 | /** Process keyboard interrupt. */ |
||
138 | static void gxemul_irq_handler(irq_t *irq, void *arg, ...) |
||
139 | { |
||
140 | if ((irq->notif_cfg.notify) && (irq->notif_cfg.answerbox)) |
||
141 | ipc_irq_send_notif(irq); |
||
142 | else { |
||
143 | char ch = 0; |
||
144 | |||
2263 | kebrt | 145 | ch = *((char *) gxemul_hw_map.kbd); |
2215 | kebrt | 146 | if (ch =='\r') |
147 | ch = '\n'; |
||
148 | if (ch == 0x7f) |
||
149 | ch = '\b'; |
||
2284 | stepan | 150 | chardev_push_character(&console, ch); |
2215 | kebrt | 151 | } |
152 | } |
||
153 | |||
154 | static irq_ownership_t gxemul_claim(void) |
||
155 | { |
||
156 | return IRQ_ACCEPT; |
||
157 | } |
||
158 | |||
2274 | kebrt | 159 | void machine_grab_console(void) |
2215 | kebrt | 160 | { |
161 | ipl_t ipl = interrupts_disable(); |
||
2263 | kebrt | 162 | spinlock_lock(&gxemul_irq.lock); |
2215 | kebrt | 163 | gxemul_irq.notif_cfg.notify = false; |
164 | spinlock_unlock(&gxemul_irq.lock); |
||
165 | interrupts_restore(ipl); |
||
166 | } |
||
167 | |||
2274 | kebrt | 168 | void machine_release_console(void) |
2215 | kebrt | 169 | { |
170 | ipl_t ipl = interrupts_disable(); |
||
171 | spinlock_lock(&gxemul_irq.lock); |
||
172 | if (gxemul_irq.notif_cfg.answerbox) |
||
173 | gxemul_irq.notif_cfg.notify = true; |
||
174 | spinlock_unlock(&gxemul_irq.lock); |
||
175 | interrupts_restore(ipl); |
||
176 | } |
||
177 | |||
178 | |||
2263 | kebrt | 179 | /** Return console object representing gxemul console */ |
2274 | kebrt | 180 | void machine_console_init(devno_t devno) |
2215 | kebrt | 181 | { |
2263 | kebrt | 182 | chardev_initialize("gxemul_console", &console, &gxemul_ops); |
2215 | kebrt | 183 | stdin = &console; |
184 | stdout = &console; |
||
185 | |||
186 | irq_initialize(&gxemul_irq); |
||
187 | gxemul_irq.devno = devno; |
||
188 | gxemul_irq.inr = GXEMUL_KBD_IRQ; |
||
189 | gxemul_irq.claim = gxemul_claim; |
||
190 | gxemul_irq.handler = gxemul_irq_handler; |
||
191 | irq_register(&gxemul_irq); |
||
192 | |||
193 | // cp0_unmask_int(GXEMUL_KBD_IRQ); |
||
2245 | stepan | 194 | gxemul_irqc_unmask(GXEMUL_KBD_IRQ); |
2215 | kebrt | 195 | |
196 | sysinfo_set_item_val("kbd", NULL, true); |
||
197 | sysinfo_set_item_val("kbd.devno", NULL, devno); |
||
198 | sysinfo_set_item_val("kbd.inr", NULL, GXEMUL_KBD_IRQ); |
||
2263 | kebrt | 199 | sysinfo_set_item_val("kbd.address.virtual", NULL, gxemul_hw_map.kbd); |
2215 | kebrt | 200 | } |
201 | |||
2245 | stepan | 202 | /** Return the mask of active interrupts. */ |
203 | inline uint32_t gxemul_irqc_get_sources(void) |
||
204 | { |
||
2263 | kebrt | 205 | return *(uint32_t*) gxemul_hw_map.irqc; |
2245 | stepan | 206 | } |
207 | |||
208 | /** Masks interrupt. |
||
209 | * |
||
210 | * @param irq interrupt number |
||
211 | */ |
||
212 | inline void gxemul_irqc_mask(uint32_t irq) |
||
213 | { |
||
2263 | kebrt | 214 | *(uint32_t*) gxemul_hw_map.irqc_mask = irq; |
2245 | stepan | 215 | } |
216 | |||
217 | /** Unmasks interrupt. |
||
218 | * |
||
219 | * @param irq interrupt number |
||
220 | */ |
||
221 | inline void gxemul_irqc_unmask(uint32_t irq) |
||
222 | { |
||
2263 | kebrt | 223 | *(uint32_t*) gxemul_hw_map.irqc_unmask = irq; |
2245 | stepan | 224 | } |
225 | |||
226 | |||
227 | /** Starts gxemul Real Time Clock device, which asserts regular interrupts. |
||
228 | * |
||
229 | * @param frequency interrupts frequency (0 disables RTC) |
||
230 | */ |
||
231 | void gxemul_timer_start(uint32_t frequency) |
||
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 | */ |
||
275 | void gxemul_timer_irq_init() |
||
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 | |||
321 | |||
2215 | kebrt | 322 | /** @} |
323 | */ |