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
25 #include "qemu/osdep.h"
27 #include "hw/sysbus.h"
28 #include "migration/vmstate.h"
29 #include "qemu/module.h"
30 #include "sysemu/runstate.h"
34 * This is the auxio port, chip control and system control part of
35 * chip STP2001 (Slave I/O), also produced as NCR89C105. See
36 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt
38 * This also includes the PMC CPU idle controller.
41 #define TYPE_SLAVIO_MISC "slavio_misc"
42 #define SLAVIO_MISC(obj) OBJECT_CHECK(MiscState, (obj), TYPE_SLAVIO_MISC)
44 typedef struct MiscState
{
45 SysBusDevice parent_obj
;
47 MemoryRegion cfg_iomem
;
48 MemoryRegion diag_iomem
;
49 MemoryRegion mdm_iomem
;
50 MemoryRegion led_iomem
;
51 MemoryRegion sysctrl_iomem
;
52 MemoryRegion aux1_iomem
;
53 MemoryRegion aux2_iomem
;
64 #define TYPE_APC "apc"
65 #define APC(obj) OBJECT_CHECK(APCState, (obj), TYPE_APC)
67 typedef struct APCState
{
68 SysBusDevice parent_obj
;
76 #define SYSCTRL_SIZE 4
80 #define AUX2_PWROFF 0x01
81 #define AUX2_PWRINTCLR 0x02
82 #define AUX2_PWRFAIL 0x20
84 #define CFG_PWRINTEN 0x08
86 #define SYS_RESET 0x01
87 #define SYS_RESETSTAT 0x02
89 static void slavio_misc_update_irq(void *opaque
)
91 MiscState
*s
= opaque
;
93 if ((s
->aux2
& AUX2_PWRFAIL
) && (s
->config
& CFG_PWRINTEN
)) {
94 trace_slavio_misc_update_irq_raise();
95 qemu_irq_raise(s
->irq
);
97 trace_slavio_misc_update_irq_lower();
98 qemu_irq_lower(s
->irq
);
102 static void slavio_misc_reset(DeviceState
*d
)
104 MiscState
*s
= SLAVIO_MISC(d
);
106 // Diagnostic and system control registers not cleared in reset
107 s
->config
= s
->aux1
= s
->aux2
= s
->mctrl
= 0;
110 static void slavio_set_power_fail(void *opaque
, int irq
, int power_failing
)
112 MiscState
*s
= opaque
;
114 trace_slavio_set_power_fail(power_failing
, s
->config
);
115 if (power_failing
&& (s
->config
& CFG_PWRINTEN
)) {
116 s
->aux2
|= AUX2_PWRFAIL
;
118 s
->aux2
&= ~AUX2_PWRFAIL
;
120 slavio_misc_update_irq(s
);
123 static void slavio_cfg_mem_writeb(void *opaque
, hwaddr addr
,
124 uint64_t val
, unsigned size
)
126 MiscState
*s
= opaque
;
128 trace_slavio_cfg_mem_writeb(val
& 0xff);
129 s
->config
= val
& 0xff;
130 slavio_misc_update_irq(s
);
133 static uint64_t slavio_cfg_mem_readb(void *opaque
, hwaddr addr
,
136 MiscState
*s
= opaque
;
140 trace_slavio_cfg_mem_readb(ret
);
144 static const MemoryRegionOps slavio_cfg_mem_ops
= {
145 .read
= slavio_cfg_mem_readb
,
146 .write
= slavio_cfg_mem_writeb
,
147 .endianness
= DEVICE_NATIVE_ENDIAN
,
149 .min_access_size
= 1,
150 .max_access_size
= 1,
154 static void slavio_diag_mem_writeb(void *opaque
, hwaddr addr
,
155 uint64_t val
, unsigned size
)
157 MiscState
*s
= opaque
;
159 trace_slavio_diag_mem_writeb(val
& 0xff);
160 s
->diag
= val
& 0xff;
163 static uint64_t slavio_diag_mem_readb(void *opaque
, hwaddr addr
,
166 MiscState
*s
= opaque
;
170 trace_slavio_diag_mem_readb(ret
);
174 static const MemoryRegionOps slavio_diag_mem_ops
= {
175 .read
= slavio_diag_mem_readb
,
176 .write
= slavio_diag_mem_writeb
,
177 .endianness
= DEVICE_NATIVE_ENDIAN
,
179 .min_access_size
= 1,
180 .max_access_size
= 1,
184 static void slavio_mdm_mem_writeb(void *opaque
, hwaddr addr
,
185 uint64_t val
, unsigned size
)
187 MiscState
*s
= opaque
;
189 trace_slavio_mdm_mem_writeb(val
& 0xff);
190 s
->mctrl
= val
& 0xff;
193 static uint64_t slavio_mdm_mem_readb(void *opaque
, hwaddr addr
,
196 MiscState
*s
= opaque
;
200 trace_slavio_mdm_mem_readb(ret
);
204 static const MemoryRegionOps slavio_mdm_mem_ops
= {
205 .read
= slavio_mdm_mem_readb
,
206 .write
= slavio_mdm_mem_writeb
,
207 .endianness
= DEVICE_NATIVE_ENDIAN
,
209 .min_access_size
= 1,
210 .max_access_size
= 1,
214 static void slavio_aux1_mem_writeb(void *opaque
, hwaddr addr
,
215 uint64_t val
, unsigned size
)
217 MiscState
*s
= opaque
;
219 trace_slavio_aux1_mem_writeb(val
& 0xff);
221 // Send a pulse to floppy terminal count line
223 qemu_irq_raise(s
->fdc_tc
);
224 qemu_irq_lower(s
->fdc_tc
);
228 s
->aux1
= val
& 0xff;
231 static uint64_t slavio_aux1_mem_readb(void *opaque
, hwaddr addr
,
234 MiscState
*s
= opaque
;
238 trace_slavio_aux1_mem_readb(ret
);
242 static const MemoryRegionOps slavio_aux1_mem_ops
= {
243 .read
= slavio_aux1_mem_readb
,
244 .write
= slavio_aux1_mem_writeb
,
245 .endianness
= DEVICE_NATIVE_ENDIAN
,
247 .min_access_size
= 1,
248 .max_access_size
= 1,
252 static void slavio_aux2_mem_writeb(void *opaque
, hwaddr addr
,
253 uint64_t val
, unsigned size
)
255 MiscState
*s
= opaque
;
257 val
&= AUX2_PWRINTCLR
| AUX2_PWROFF
;
258 trace_slavio_aux2_mem_writeb(val
& 0xff);
259 val
|= s
->aux2
& AUX2_PWRFAIL
;
260 if (val
& AUX2_PWRINTCLR
) // Clear Power Fail int
263 if (val
& AUX2_PWROFF
)
264 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN
);
265 slavio_misc_update_irq(s
);
268 static uint64_t slavio_aux2_mem_readb(void *opaque
, hwaddr addr
,
271 MiscState
*s
= opaque
;
275 trace_slavio_aux2_mem_readb(ret
);
279 static const MemoryRegionOps slavio_aux2_mem_ops
= {
280 .read
= slavio_aux2_mem_readb
,
281 .write
= slavio_aux2_mem_writeb
,
282 .endianness
= DEVICE_NATIVE_ENDIAN
,
284 .min_access_size
= 1,
285 .max_access_size
= 1,
289 static void apc_mem_writeb(void *opaque
, hwaddr addr
,
290 uint64_t val
, unsigned size
)
292 APCState
*s
= opaque
;
294 trace_apc_mem_writeb(val
& 0xff);
295 qemu_irq_raise(s
->cpu_halt
);
298 static uint64_t apc_mem_readb(void *opaque
, hwaddr addr
,
303 trace_apc_mem_readb(ret
);
307 static const MemoryRegionOps apc_mem_ops
= {
308 .read
= apc_mem_readb
,
309 .write
= apc_mem_writeb
,
310 .endianness
= DEVICE_NATIVE_ENDIAN
,
312 .min_access_size
= 1,
313 .max_access_size
= 1,
317 static uint64_t slavio_sysctrl_mem_readl(void *opaque
, hwaddr addr
,
320 MiscState
*s
= opaque
;
330 trace_slavio_sysctrl_mem_readl(ret
);
334 static void slavio_sysctrl_mem_writel(void *opaque
, hwaddr addr
,
335 uint64_t val
, unsigned size
)
337 MiscState
*s
= opaque
;
339 trace_slavio_sysctrl_mem_writel(val
);
342 if (val
& SYS_RESET
) {
343 s
->sysctrl
= SYS_RESETSTAT
;
344 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
352 static const MemoryRegionOps slavio_sysctrl_mem_ops
= {
353 .read
= slavio_sysctrl_mem_readl
,
354 .write
= slavio_sysctrl_mem_writel
,
355 .endianness
= DEVICE_NATIVE_ENDIAN
,
357 .min_access_size
= 4,
358 .max_access_size
= 4,
362 static uint64_t slavio_led_mem_readw(void *opaque
, hwaddr addr
,
365 MiscState
*s
= opaque
;
375 trace_slavio_led_mem_readw(ret
);
379 static void slavio_led_mem_writew(void *opaque
, hwaddr addr
,
380 uint64_t val
, unsigned size
)
382 MiscState
*s
= opaque
;
384 trace_slavio_led_mem_writew(val
& 0xffff);
394 static const MemoryRegionOps slavio_led_mem_ops
= {
395 .read
= slavio_led_mem_readw
,
396 .write
= slavio_led_mem_writew
,
397 .endianness
= DEVICE_NATIVE_ENDIAN
,
399 .min_access_size
= 2,
400 .max_access_size
= 2,
404 static const VMStateDescription vmstate_misc
= {
405 .name
="slavio_misc",
407 .minimum_version_id
= 1,
408 .fields
= (VMStateField
[]) {
409 VMSTATE_UINT32(dummy
, MiscState
),
410 VMSTATE_UINT8(config
, MiscState
),
411 VMSTATE_UINT8(aux1
, MiscState
),
412 VMSTATE_UINT8(aux2
, MiscState
),
413 VMSTATE_UINT8(diag
, MiscState
),
414 VMSTATE_UINT8(mctrl
, MiscState
),
415 VMSTATE_UINT8(sysctrl
, MiscState
),
416 VMSTATE_END_OF_LIST()
420 static void apc_init(Object
*obj
)
422 APCState
*s
= APC(obj
);
423 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
425 sysbus_init_irq(dev
, &s
->cpu_halt
);
427 /* Power management (APC) XXX: not a Slavio device */
428 memory_region_init_io(&s
->iomem
, obj
, &apc_mem_ops
, s
,
430 sysbus_init_mmio(dev
, &s
->iomem
);
433 static void slavio_misc_init(Object
*obj
)
435 DeviceState
*dev
= DEVICE(obj
);
436 MiscState
*s
= SLAVIO_MISC(obj
);
437 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
439 sysbus_init_irq(sbd
, &s
->irq
);
440 sysbus_init_irq(sbd
, &s
->fdc_tc
);
442 /* 8 bit registers */
444 memory_region_init_io(&s
->cfg_iomem
, obj
, &slavio_cfg_mem_ops
, s
,
445 "configuration", MISC_SIZE
);
446 sysbus_init_mmio(sbd
, &s
->cfg_iomem
);
449 memory_region_init_io(&s
->diag_iomem
, obj
, &slavio_diag_mem_ops
, s
,
450 "diagnostic", MISC_SIZE
);
451 sysbus_init_mmio(sbd
, &s
->diag_iomem
);
454 memory_region_init_io(&s
->mdm_iomem
, obj
, &slavio_mdm_mem_ops
, s
,
456 sysbus_init_mmio(sbd
, &s
->mdm_iomem
);
458 /* 16 bit registers */
459 /* ss600mp diag LEDs */
460 memory_region_init_io(&s
->led_iomem
, obj
, &slavio_led_mem_ops
, s
,
462 sysbus_init_mmio(sbd
, &s
->led_iomem
);
464 /* 32 bit registers */
466 memory_region_init_io(&s
->sysctrl_iomem
, obj
, &slavio_sysctrl_mem_ops
, s
,
467 "system-control", SYSCTRL_SIZE
);
468 sysbus_init_mmio(sbd
, &s
->sysctrl_iomem
);
470 /* AUX 1 (Misc System Functions) */
471 memory_region_init_io(&s
->aux1_iomem
, obj
, &slavio_aux1_mem_ops
, s
,
472 "misc-system-functions", MISC_SIZE
);
473 sysbus_init_mmio(sbd
, &s
->aux1_iomem
);
475 /* AUX 2 (Software Powerdown Control) */
476 memory_region_init_io(&s
->aux2_iomem
, obj
, &slavio_aux2_mem_ops
, s
,
477 "software-powerdown-control", MISC_SIZE
);
478 sysbus_init_mmio(sbd
, &s
->aux2_iomem
);
480 qdev_init_gpio_in(dev
, slavio_set_power_fail
, 1);
483 static void slavio_misc_class_init(ObjectClass
*klass
, void *data
)
485 DeviceClass
*dc
= DEVICE_CLASS(klass
);
487 dc
->reset
= slavio_misc_reset
;
488 dc
->vmsd
= &vmstate_misc
;
491 static const TypeInfo slavio_misc_info
= {
492 .name
= TYPE_SLAVIO_MISC
,
493 .parent
= TYPE_SYS_BUS_DEVICE
,
494 .instance_size
= sizeof(MiscState
),
495 .instance_init
= slavio_misc_init
,
496 .class_init
= slavio_misc_class_init
,
499 static const TypeInfo apc_info
= {
501 .parent
= TYPE_SYS_BUS_DEVICE
,
502 .instance_size
= sizeof(MiscState
),
503 .instance_init
= apc_init
,
506 static void slavio_misc_register_types(void)
508 type_register_static(&slavio_misc_info
);
509 type_register_static(&apc_info
);
512 type_init(slavio_misc_register_types
)