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