Rev 2264 | 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 |
||
49 | #define GXEMUL_RTC 0x15000000 |
||
50 | #define GXEMUL_RTC_FREQ_OFFSET 0x100 |
||
51 | #define GXEMUL_RTC_ACK_OFFSET 0x110 |
||
52 | #define GXEMUL_IRQC 0x16000000 |
||
53 | #define GXEMUL_IRQC_MASK_OFFSET 0x4 |
||
54 | #define GXEMUL_IRQC_UNMASK_OFFSET 0x8 |
||
55 | #define GXEMUL_MP 0x11000000 |
||
56 | #define GXEMUL_MP_MEMSIZE_OFFSET 0x0090 |
||
2215 | kebrt | 57 | |
2263 | kebrt | 58 | |
2245 | stepan | 59 | /** IRQs */ |
60 | #define GXEMUL_KBD_IRQ 2 |
||
61 | #define GXEMUL_TIMER_IRQ 4 |
||
62 | |||
2263 | kebrt | 63 | static gxemul_hw_map_t gxemul_hw_map; |
2215 | kebrt | 64 | static chardev_t console; |
65 | static irq_t gxemul_irq; |
||
2245 | stepan | 66 | static irq_t gxemul_timer_irq; |
2215 | kebrt | 67 | |
68 | static void gxemul_write(chardev_t *dev, const char ch); |
||
69 | static void gxemul_enable(chardev_t *dev); |
||
70 | static void gxemul_disable(chardev_t *dev); |
||
71 | static char gxemul_do_read(chardev_t *dev); |
||
72 | |||
73 | static chardev_operations_t gxemul_ops = { |
||
74 | .resume = gxemul_enable, |
||
75 | .suspend = gxemul_disable, |
||
76 | .write = gxemul_write, |
||
77 | .read = gxemul_do_read, |
||
78 | }; |
||
79 | |||
2263 | kebrt | 80 | |
81 | /** Initializes #gxemul_hw_map. */ |
||
2274 | kebrt | 82 | void machine_hw_map_init(void) |
2263 | kebrt | 83 | { |
84 | gxemul_hw_map.videoram = hw_map(GXEMUL_VIDEORAM, PAGE_SIZE); |
||
85 | gxemul_hw_map.kbd = hw_map(GXEMUL_KBD, PAGE_SIZE); |
||
86 | gxemul_hw_map.rtc = hw_map(GXEMUL_RTC, PAGE_SIZE); |
||
87 | gxemul_hw_map.irqc = hw_map(GXEMUL_IRQC, PAGE_SIZE); |
||
88 | |||
89 | gxemul_hw_map.rtc_freq = gxemul_hw_map.rtc + GXEMUL_RTC_FREQ_OFFSET; |
||
90 | gxemul_hw_map.rtc_ack = gxemul_hw_map.rtc + GXEMUL_RTC_ACK_OFFSET; |
||
91 | gxemul_hw_map.irqc_mask = gxemul_hw_map.irqc + GXEMUL_IRQC_MASK_OFFSET; |
||
92 | gxemul_hw_map.irqc_unmask = gxemul_hw_map.irqc + GXEMUL_IRQC_UNMASK_OFFSET; |
||
2264 | kebrt | 93 | |
2263 | kebrt | 94 | } |
95 | |||
2215 | kebrt | 96 | /** Putchar that works with gxemul */ |
97 | void gxemul_write(chardev_t *dev, const char ch) |
||
98 | { |
||
2263 | kebrt | 99 | *((char *) gxemul_hw_map.videoram) = ch; |
2215 | kebrt | 100 | } |
101 | |||
102 | /* Called from getc(). */ |
||
103 | void gxemul_enable(chardev_t *dev) |
||
104 | { |
||
105 | // cp0_unmask_int(GXEMUL_KBD_IRQ); |
||
2245 | stepan | 106 | gxemul_irqc_unmask(GXEMUL_KBD_IRQ); |
2215 | kebrt | 107 | } |
108 | |||
109 | /* Called from getc(). */ |
||
110 | void gxemul_disable(chardev_t *dev) |
||
111 | { |
||
112 | // cp0_mask_int(GXEMUL_KBD_IRQ); |
||
2245 | stepan | 113 | gxemul_irqc_mask(GXEMUL_KBD_IRQ); |
2215 | kebrt | 114 | } |
115 | |||
116 | /** Read character using polling, assume interrupts disabled */ |
||
117 | static char gxemul_do_read(chardev_t *dev) |
||
118 | { |
||
119 | char ch; |
||
120 | |||
121 | while (1) { |
||
2263 | kebrt | 122 | ch = *((volatile char *) gxemul_hw_map.kbd); |
2215 | kebrt | 123 | if (ch) { |
124 | if (ch == '\r') |
||
125 | return '\n'; |
||
126 | if (ch == 0x7f) |
||
127 | return '\b'; |
||
128 | return ch; |
||
129 | } |
||
130 | } |
||
131 | } |
||
132 | |||
133 | /** Process keyboard interrupt. */ |
||
134 | static void gxemul_irq_handler(irq_t *irq, void *arg, ...) |
||
135 | { |
||
136 | if ((irq->notif_cfg.notify) && (irq->notif_cfg.answerbox)) |
||
137 | ipc_irq_send_notif(irq); |
||
138 | else { |
||
139 | char ch = 0; |
||
140 | |||
2263 | kebrt | 141 | ch = *((char *) gxemul_hw_map.kbd); |
2215 | kebrt | 142 | if (ch =='\r') |
143 | ch = '\n'; |
||
144 | if (ch == 0x7f) |
||
145 | ch = '\b'; |
||
2245 | stepan | 146 | //chardev_push_character(&console, ch); |
147 | printf("%c", ch); |
||
2215 | kebrt | 148 | } |
149 | } |
||
150 | |||
151 | static irq_ownership_t gxemul_claim(void) |
||
152 | { |
||
153 | return IRQ_ACCEPT; |
||
154 | } |
||
155 | |||
2274 | kebrt | 156 | void machine_grab_console(void) |
2215 | kebrt | 157 | { |
158 | ipl_t ipl = interrupts_disable(); |
||
2263 | kebrt | 159 | spinlock_lock(&gxemul_irq.lock); |
2215 | kebrt | 160 | gxemul_irq.notif_cfg.notify = false; |
161 | spinlock_unlock(&gxemul_irq.lock); |
||
162 | interrupts_restore(ipl); |
||
163 | } |
||
164 | |||
2274 | kebrt | 165 | void machine_release_console(void) |
2215 | kebrt | 166 | { |
167 | ipl_t ipl = interrupts_disable(); |
||
168 | spinlock_lock(&gxemul_irq.lock); |
||
169 | if (gxemul_irq.notif_cfg.answerbox) |
||
170 | gxemul_irq.notif_cfg.notify = true; |
||
171 | spinlock_unlock(&gxemul_irq.lock); |
||
172 | interrupts_restore(ipl); |
||
173 | } |
||
174 | |||
175 | |||
2263 | kebrt | 176 | /** Return console object representing gxemul console */ |
2274 | kebrt | 177 | void machine_console_init(devno_t devno) |
2215 | kebrt | 178 | { |
2263 | kebrt | 179 | chardev_initialize("gxemul_console", &console, &gxemul_ops); |
2215 | kebrt | 180 | stdin = &console; |
181 | stdout = &console; |
||
182 | |||
183 | irq_initialize(&gxemul_irq); |
||
184 | gxemul_irq.devno = devno; |
||
185 | gxemul_irq.inr = GXEMUL_KBD_IRQ; |
||
186 | gxemul_irq.claim = gxemul_claim; |
||
187 | gxemul_irq.handler = gxemul_irq_handler; |
||
188 | irq_register(&gxemul_irq); |
||
189 | |||
190 | // cp0_unmask_int(GXEMUL_KBD_IRQ); |
||
2245 | stepan | 191 | gxemul_irqc_unmask(GXEMUL_KBD_IRQ); |
2215 | kebrt | 192 | |
193 | sysinfo_set_item_val("kbd", NULL, true); |
||
194 | sysinfo_set_item_val("kbd.devno", NULL, devno); |
||
195 | sysinfo_set_item_val("kbd.inr", NULL, GXEMUL_KBD_IRQ); |
||
2263 | kebrt | 196 | sysinfo_set_item_val("kbd.address.virtual", NULL, gxemul_hw_map.kbd); |
2215 | kebrt | 197 | } |
198 | |||
2245 | stepan | 199 | /** Return the mask of active interrupts. */ |
200 | inline uint32_t gxemul_irqc_get_sources(void) |
||
201 | { |
||
2263 | kebrt | 202 | return *(uint32_t*) gxemul_hw_map.irqc; |
2245 | stepan | 203 | } |
204 | |||
205 | /** Masks interrupt. |
||
206 | * |
||
207 | * @param irq interrupt number |
||
208 | */ |
||
209 | inline void gxemul_irqc_mask(uint32_t irq) |
||
210 | { |
||
2263 | kebrt | 211 | *(uint32_t*) gxemul_hw_map.irqc_mask = irq; |
2245 | stepan | 212 | } |
213 | |||
214 | /** Unmasks interrupt. |
||
215 | * |
||
216 | * @param irq interrupt number |
||
217 | */ |
||
218 | inline void gxemul_irqc_unmask(uint32_t irq) |
||
219 | { |
||
2263 | kebrt | 220 | *(uint32_t*) gxemul_hw_map.irqc_unmask = irq; |
2245 | stepan | 221 | } |
222 | |||
223 | |||
224 | /** Starts gxemul Real Time Clock device, which asserts regular interrupts. |
||
225 | * |
||
226 | * @param frequency interrupts frequency (0 disables RTC) |
||
227 | */ |
||
228 | void gxemul_timer_start(uint32_t frequency) |
||
229 | { |
||
2263 | kebrt | 230 | *(uint32_t*) gxemul_hw_map.rtc_freq = frequency; |
2245 | stepan | 231 | } |
232 | |||
233 | static irq_ownership_t gxemul_timer_claim(void) |
||
234 | { |
||
235 | return IRQ_ACCEPT; |
||
236 | } |
||
237 | |||
238 | static void gxemul_timer_irq_handler(irq_t *irq, void *arg, ...) |
||
239 | { |
||
240 | /* TODO time drifts ?? |
||
241 | unsigned long drift; |
||
242 | |||
243 | drift = cp0_count_read() - nextcount; |
||
244 | while (drift > cp0_compare_value) { |
||
245 | drift -= cp0_compare_value; |
||
246 | CPU->missed_clock_ticks++; |
||
247 | } |
||
248 | nextcount = cp0_count_read() + cp0_compare_value - drift; |
||
249 | cp0_compare_write(nextcount); |
||
250 | */ |
||
251 | |||
252 | /* |
||
253 | * We are holding a lock which prevents preemption. |
||
254 | * Release the lock, call clock() and reacquire the lock again. |
||
255 | */ |
||
256 | spinlock_unlock(&irq->lock); |
||
257 | //clock(); |
||
258 | puts(" "); |
||
259 | spinlock_lock(&irq->lock); |
||
2261 | stepan | 260 | |
261 | /* acknowledge tick */ |
||
2263 | kebrt | 262 | *(uint32_t*) gxemul_hw_map.rtc_ack = 0; |
2245 | stepan | 263 | |
264 | /* TODO what's that? * |
||
265 | if (virtual_timer_fnc != NULL) |
||
266 | virtual_timer_fnc(); |
||
267 | */ |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Initializes and registers timer interrupt handler. |
||
272 | */ |
||
273 | void gxemul_timer_irq_init() |
||
274 | { |
||
275 | irq_initialize(&gxemul_timer_irq); |
||
276 | gxemul_timer_irq.devno = device_assign_devno(); |
||
277 | gxemul_timer_irq.inr = GXEMUL_TIMER_IRQ; |
||
278 | gxemul_timer_irq.claim = gxemul_timer_claim; |
||
279 | gxemul_timer_irq.handler = gxemul_timer_irq_handler; |
||
2261 | stepan | 280 | |
2245 | stepan | 281 | irq_register(&gxemul_timer_irq); |
282 | } |
||
283 | |||
2274 | kebrt | 284 | void machine_timer_irq_start() |
2263 | kebrt | 285 | { |
2274 | kebrt | 286 | gxemul_timer_irq_init(); |
287 | gxemul_timer_start(GXEMUL_TIMER_FREQ); |
||
288 | } |
||
289 | |||
290 | size_t machine_get_memory_size(void) |
||
291 | { |
||
2263 | kebrt | 292 | return *((int*)(GXEMUL_MP + GXEMUL_MP_MEMSIZE_OFFSET)); |
293 | } |
||
2245 | stepan | 294 | |
2264 | kebrt | 295 | void gxemul_debug_putc(char ch) { |
296 | *((volatile char *) GXEMUL_KBD) = ch; |
||
297 | } |
||
298 | |||
2215 | kebrt | 299 | /** @} |
300 | */ |