Unbreak out-of-tree builds
[qemu/mini2440.git] / hw / slavio_misc.c
blobecece54e543ca9573c9da7c4bf9093578a5c5e32
1 /*
2 * QEMU Sparc SLAVIO aux io port emulation
4 * Copyright (c) 2005 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "hw.h"
25 #include "sun4m.h"
26 #include "sysemu.h"
28 /* debug misc */
29 //#define DEBUG_MISC
32 * This is the auxio port, chip control and system control part of
33 * chip STP2001 (Slave I/O), also produced as NCR89C105. See
34 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt
36 * This also includes the PMC CPU idle controller.
39 #ifdef DEBUG_MISC
40 #define MISC_DPRINTF(fmt, ...) \
41 do { printf("MISC: " fmt , ## __VA_ARGS__); } while (0)
42 #else
43 #define MISC_DPRINTF(fmt, ...)
44 #endif
46 typedef struct MiscState {
47 qemu_irq irq;
48 uint8_t config;
49 uint8_t aux1, aux2;
50 uint8_t diag, mctrl;
51 uint32_t sysctrl;
52 uint16_t leds;
53 qemu_irq cpu_halt;
54 qemu_irq fdc_tc;
55 } MiscState;
57 #define MISC_SIZE 1
58 #define SYSCTRL_SIZE 4
60 #define MISC_LEDS 0x01600000
61 #define MISC_CFG 0x01800000
62 #define MISC_DIAG 0x01a00000
63 #define MISC_MDM 0x01b00000
64 #define MISC_SYS 0x01f00000
66 #define AUX1_TC 0x02
68 #define AUX2_PWROFF 0x01
69 #define AUX2_PWRINTCLR 0x02
70 #define AUX2_PWRFAIL 0x20
72 #define CFG_PWRINTEN 0x08
74 #define SYS_RESET 0x01
75 #define SYS_RESETSTAT 0x02
77 static void slavio_misc_update_irq(void *opaque)
79 MiscState *s = opaque;
81 if ((s->aux2 & AUX2_PWRFAIL) && (s->config & CFG_PWRINTEN)) {
82 MISC_DPRINTF("Raise IRQ\n");
83 qemu_irq_raise(s->irq);
84 } else {
85 MISC_DPRINTF("Lower IRQ\n");
86 qemu_irq_lower(s->irq);
90 static void slavio_misc_reset(void *opaque)
92 MiscState *s = opaque;
94 // Diagnostic and system control registers not cleared in reset
95 s->config = s->aux1 = s->aux2 = s->mctrl = 0;
98 void slavio_set_power_fail(void *opaque, int power_failing)
100 MiscState *s = opaque;
102 MISC_DPRINTF("Power fail: %d, config: %d\n", power_failing, s->config);
103 if (power_failing && (s->config & CFG_PWRINTEN)) {
104 s->aux2 |= AUX2_PWRFAIL;
105 } else {
106 s->aux2 &= ~AUX2_PWRFAIL;
108 slavio_misc_update_irq(s);
111 static void slavio_cfg_mem_writeb(void *opaque, target_phys_addr_t addr,
112 uint32_t val)
114 MiscState *s = opaque;
116 MISC_DPRINTF("Write config %2.2x\n", val & 0xff);
117 s->config = val & 0xff;
118 slavio_misc_update_irq(s);
121 static uint32_t slavio_cfg_mem_readb(void *opaque, target_phys_addr_t addr)
123 MiscState *s = opaque;
124 uint32_t ret = 0;
126 ret = s->config;
127 MISC_DPRINTF("Read config %2.2x\n", ret);
128 return ret;
131 static CPUReadMemoryFunc *slavio_cfg_mem_read[3] = {
132 slavio_cfg_mem_readb,
133 NULL,
134 NULL,
137 static CPUWriteMemoryFunc *slavio_cfg_mem_write[3] = {
138 slavio_cfg_mem_writeb,
139 NULL,
140 NULL,
143 static void slavio_diag_mem_writeb(void *opaque, target_phys_addr_t addr,
144 uint32_t val)
146 MiscState *s = opaque;
148 MISC_DPRINTF("Write diag %2.2x\n", val & 0xff);
149 s->diag = val & 0xff;
152 static uint32_t slavio_diag_mem_readb(void *opaque, target_phys_addr_t addr)
154 MiscState *s = opaque;
155 uint32_t ret = 0;
157 ret = s->diag;
158 MISC_DPRINTF("Read diag %2.2x\n", ret);
159 return ret;
162 static CPUReadMemoryFunc *slavio_diag_mem_read[3] = {
163 slavio_diag_mem_readb,
164 NULL,
165 NULL,
168 static CPUWriteMemoryFunc *slavio_diag_mem_write[3] = {
169 slavio_diag_mem_writeb,
170 NULL,
171 NULL,
174 static void slavio_mdm_mem_writeb(void *opaque, target_phys_addr_t addr,
175 uint32_t val)
177 MiscState *s = opaque;
179 MISC_DPRINTF("Write modem control %2.2x\n", val & 0xff);
180 s->mctrl = val & 0xff;
183 static uint32_t slavio_mdm_mem_readb(void *opaque, target_phys_addr_t addr)
185 MiscState *s = opaque;
186 uint32_t ret = 0;
188 ret = s->mctrl;
189 MISC_DPRINTF("Read modem control %2.2x\n", ret);
190 return ret;
193 static CPUReadMemoryFunc *slavio_mdm_mem_read[3] = {
194 slavio_mdm_mem_readb,
195 NULL,
196 NULL,
199 static CPUWriteMemoryFunc *slavio_mdm_mem_write[3] = {
200 slavio_mdm_mem_writeb,
201 NULL,
202 NULL,
205 static void slavio_aux1_mem_writeb(void *opaque, target_phys_addr_t addr,
206 uint32_t val)
208 MiscState *s = opaque;
210 MISC_DPRINTF("Write aux1 %2.2x\n", val & 0xff);
211 if (val & AUX1_TC) {
212 // Send a pulse to floppy terminal count line
213 if (s->fdc_tc) {
214 qemu_irq_raise(s->fdc_tc);
215 qemu_irq_lower(s->fdc_tc);
217 val &= ~AUX1_TC;
219 s->aux1 = val & 0xff;
222 static uint32_t slavio_aux1_mem_readb(void *opaque, target_phys_addr_t addr)
224 MiscState *s = opaque;
225 uint32_t ret = 0;
227 ret = s->aux1;
228 MISC_DPRINTF("Read aux1 %2.2x\n", ret);
230 return ret;
233 static CPUReadMemoryFunc *slavio_aux1_mem_read[3] = {
234 slavio_aux1_mem_readb,
235 NULL,
236 NULL,
239 static CPUWriteMemoryFunc *slavio_aux1_mem_write[3] = {
240 slavio_aux1_mem_writeb,
241 NULL,
242 NULL,
245 static void slavio_aux2_mem_writeb(void *opaque, target_phys_addr_t addr,
246 uint32_t val)
248 MiscState *s = opaque;
250 val &= AUX2_PWRINTCLR | AUX2_PWROFF;
251 MISC_DPRINTF("Write aux2 %2.2x\n", val);
252 val |= s->aux2 & AUX2_PWRFAIL;
253 if (val & AUX2_PWRINTCLR) // Clear Power Fail int
254 val &= AUX2_PWROFF;
255 s->aux2 = val;
256 if (val & AUX2_PWROFF)
257 qemu_system_shutdown_request();
258 slavio_misc_update_irq(s);
261 static uint32_t slavio_aux2_mem_readb(void *opaque, target_phys_addr_t addr)
263 MiscState *s = opaque;
264 uint32_t ret = 0;
266 ret = s->aux2;
267 MISC_DPRINTF("Read aux2 %2.2x\n", ret);
269 return ret;
272 static CPUReadMemoryFunc *slavio_aux2_mem_read[3] = {
273 slavio_aux2_mem_readb,
274 NULL,
275 NULL,
278 static CPUWriteMemoryFunc *slavio_aux2_mem_write[3] = {
279 slavio_aux2_mem_writeb,
280 NULL,
281 NULL,
284 static void apc_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
286 MiscState *s = opaque;
288 MISC_DPRINTF("Write power management %2.2x\n", val & 0xff);
289 qemu_irq_raise(s->cpu_halt);
292 static uint32_t apc_mem_readb(void *opaque, target_phys_addr_t addr)
294 uint32_t ret = 0;
296 MISC_DPRINTF("Read power management %2.2x\n", ret);
297 return ret;
300 static CPUReadMemoryFunc *apc_mem_read[3] = {
301 apc_mem_readb,
302 NULL,
303 NULL,
306 static CPUWriteMemoryFunc *apc_mem_write[3] = {
307 apc_mem_writeb,
308 NULL,
309 NULL,
312 static uint32_t slavio_sysctrl_mem_readl(void *opaque, target_phys_addr_t addr)
314 MiscState *s = opaque;
315 uint32_t ret = 0;
317 switch (addr) {
318 case 0:
319 ret = s->sysctrl;
320 break;
321 default:
322 break;
324 MISC_DPRINTF("Read system control %08x\n", ret);
325 return ret;
328 static void slavio_sysctrl_mem_writel(void *opaque, target_phys_addr_t addr,
329 uint32_t val)
331 MiscState *s = opaque;
333 MISC_DPRINTF("Write system control %08x\n", val);
334 switch (addr) {
335 case 0:
336 if (val & SYS_RESET) {
337 s->sysctrl = SYS_RESETSTAT;
338 qemu_system_reset_request();
340 break;
341 default:
342 break;
346 static CPUReadMemoryFunc *slavio_sysctrl_mem_read[3] = {
347 NULL,
348 NULL,
349 slavio_sysctrl_mem_readl,
352 static CPUWriteMemoryFunc *slavio_sysctrl_mem_write[3] = {
353 NULL,
354 NULL,
355 slavio_sysctrl_mem_writel,
358 static uint32_t slavio_led_mem_readw(void *opaque, target_phys_addr_t addr)
360 MiscState *s = opaque;
361 uint32_t ret = 0;
363 switch (addr) {
364 case 0:
365 ret = s->leds;
366 break;
367 default:
368 break;
370 MISC_DPRINTF("Read diagnostic LED %04x\n", ret);
371 return ret;
374 static void slavio_led_mem_writew(void *opaque, target_phys_addr_t addr,
375 uint32_t val)
377 MiscState *s = opaque;
379 MISC_DPRINTF("Write diagnostic LED %04x\n", val & 0xffff);
380 switch (addr) {
381 case 0:
382 s->leds = val;
383 break;
384 default:
385 break;
389 static CPUReadMemoryFunc *slavio_led_mem_read[3] = {
390 NULL,
391 slavio_led_mem_readw,
392 NULL,
395 static CPUWriteMemoryFunc *slavio_led_mem_write[3] = {
396 NULL,
397 slavio_led_mem_writew,
398 NULL,
401 static void slavio_misc_save(QEMUFile *f, void *opaque)
403 MiscState *s = opaque;
404 uint32_t tmp = 0;
405 uint8_t tmp8;
407 qemu_put_be32s(f, &tmp); /* ignored, was IRQ. */
408 qemu_put_8s(f, &s->config);
409 qemu_put_8s(f, &s->aux1);
410 qemu_put_8s(f, &s->aux2);
411 qemu_put_8s(f, &s->diag);
412 qemu_put_8s(f, &s->mctrl);
413 tmp8 = s->sysctrl & 0xff;
414 qemu_put_8s(f, &tmp8);
417 static int slavio_misc_load(QEMUFile *f, void *opaque, int version_id)
419 MiscState *s = opaque;
420 uint32_t tmp;
421 uint8_t tmp8;
423 if (version_id != 1)
424 return -EINVAL;
426 qemu_get_be32s(f, &tmp);
427 qemu_get_8s(f, &s->config);
428 qemu_get_8s(f, &s->aux1);
429 qemu_get_8s(f, &s->aux2);
430 qemu_get_8s(f, &s->diag);
431 qemu_get_8s(f, &s->mctrl);
432 qemu_get_8s(f, &tmp8);
433 s->sysctrl = (uint32_t)tmp8;
434 return 0;
437 void *slavio_misc_init(target_phys_addr_t base, target_phys_addr_t power_base,
438 target_phys_addr_t aux1_base,
439 target_phys_addr_t aux2_base, qemu_irq irq,
440 qemu_irq cpu_halt, qemu_irq **fdc_tc)
442 int io;
443 MiscState *s;
445 s = qemu_mallocz(sizeof(MiscState));
447 if (base) {
448 /* 8 bit registers */
450 // Slavio control
451 io = cpu_register_io_memory(0, slavio_cfg_mem_read,
452 slavio_cfg_mem_write, s);
453 cpu_register_physical_memory(base + MISC_CFG, MISC_SIZE, io);
455 // Diagnostics
456 io = cpu_register_io_memory(0, slavio_diag_mem_read,
457 slavio_diag_mem_write, s);
458 cpu_register_physical_memory(base + MISC_DIAG, MISC_SIZE, io);
460 // Modem control
461 io = cpu_register_io_memory(0, slavio_mdm_mem_read,
462 slavio_mdm_mem_write, s);
463 cpu_register_physical_memory(base + MISC_MDM, MISC_SIZE, io);
465 /* 16 bit registers */
466 io = cpu_register_io_memory(0, slavio_led_mem_read,
467 slavio_led_mem_write, s);
468 /* ss600mp diag LEDs */
469 cpu_register_physical_memory(base + MISC_LEDS, MISC_SIZE, io);
471 /* 32 bit registers */
472 io = cpu_register_io_memory(0, slavio_sysctrl_mem_read,
473 slavio_sysctrl_mem_write, s);
474 // System control
475 cpu_register_physical_memory(base + MISC_SYS, SYSCTRL_SIZE, io);
478 // AUX 1 (Misc System Functions)
479 if (aux1_base) {
480 io = cpu_register_io_memory(0, slavio_aux1_mem_read,
481 slavio_aux1_mem_write, s);
482 cpu_register_physical_memory(aux1_base, MISC_SIZE, io);
485 // AUX 2 (Software Powerdown Control)
486 if (aux2_base) {
487 io = cpu_register_io_memory(0, slavio_aux2_mem_read,
488 slavio_aux2_mem_write, s);
489 cpu_register_physical_memory(aux2_base, MISC_SIZE, io);
492 // Power management (APC) XXX: not a Slavio device
493 if (power_base) {
494 io = cpu_register_io_memory(0, apc_mem_read, apc_mem_write, s);
495 cpu_register_physical_memory(power_base, MISC_SIZE, io);
498 s->irq = irq;
499 s->cpu_halt = cpu_halt;
500 *fdc_tc = &s->fdc_tc;
502 register_savevm("slavio_misc", base, 1, slavio_misc_save, slavio_misc_load,
504 qemu_register_reset(slavio_misc_reset, s);
505 slavio_misc_reset(s);
507 return s;