2 * Motorola ColdFire MCF5206 SoC embedded peripheral emulation.
4 * Copyright (c) 2007 CodeSourcery.
6 * This code is licensed under the GPL
9 #include "qemu/osdep.h"
10 #include "qemu/error-report.h"
13 #include "hw/qdev-properties.h"
14 #include "hw/boards.h"
16 #include "hw/m68k/mcf.h"
17 #include "qemu/timer.h"
18 #include "hw/ptimer.h"
19 #include "sysemu/sysemu.h"
20 #include "hw/sysbus.h"
22 /* General purpose timer module. */
43 static void m5206_timer_update(m5206_timer_state
*s
)
45 if ((s
->tmr
& TMR_ORI
) != 0 && (s
->ter
& TER_REF
))
46 qemu_irq_raise(s
->irq
);
48 qemu_irq_lower(s
->irq
);
51 static void m5206_timer_reset(m5206_timer_state
*s
)
57 static void m5206_timer_recalibrate(m5206_timer_state
*s
)
62 ptimer_transaction_begin(s
->timer
);
63 ptimer_stop(s
->timer
);
65 if ((s
->tmr
& TMR_RST
) == 0) {
69 prescale
= (s
->tmr
>> 8) + 1;
70 mode
= (s
->tmr
>> 1) & 3;
74 if (mode
== 3 || mode
== 0) {
75 qemu_log_mask(LOG_UNIMP
, "m5206_timer: mode %d not implemented\n",
79 if ((s
->tmr
& TMR_FRR
) == 0) {
80 qemu_log_mask(LOG_UNIMP
,
81 "m5206_timer: free running mode not implemented\n");
85 /* Assume 66MHz system clock. */
86 ptimer_set_freq(s
->timer
, 66000000 / prescale
);
88 ptimer_set_limit(s
->timer
, s
->trr
, 0);
90 ptimer_run(s
->timer
, 0);
92 ptimer_transaction_commit(s
->timer
);
95 static void m5206_timer_trigger(void *opaque
)
97 m5206_timer_state
*s
= (m5206_timer_state
*)opaque
;
99 m5206_timer_update(s
);
102 static uint32_t m5206_timer_read(m5206_timer_state
*s
, uint32_t addr
)
112 return s
->trr
- ptimer_get_count(s
->timer
);
120 static void m5206_timer_write(m5206_timer_state
*s
, uint32_t addr
, uint32_t val
)
124 if ((s
->tmr
& TMR_RST
) != 0 && (val
& TMR_RST
) == 0) {
125 m5206_timer_reset(s
);
128 m5206_timer_recalibrate(s
);
132 m5206_timer_recalibrate(s
);
138 ptimer_transaction_begin(s
->timer
);
139 ptimer_set_count(s
->timer
, val
);
140 ptimer_transaction_commit(s
->timer
);
148 m5206_timer_update(s
);
151 static void m5206_timer_init(m5206_timer_state
*s
, qemu_irq irq
)
153 s
->timer
= ptimer_init(m5206_timer_trigger
, s
, PTIMER_POLICY_LEGACY
);
155 m5206_timer_reset(s
);
158 /* System Integration Module. */
161 SysBusDevice parent_obj
;
166 m5206_timer_state timer
[2];
167 DeviceState
*uart
[2];
170 uint16_t imr
; /* 1 == interrupt is masked. */
175 /* Include the UART vector registers here. */
179 #define MCF5206_MBAR(obj) OBJECT_CHECK(m5206_mbar_state, (obj), TYPE_MCF5206_MBAR)
181 /* Interrupt controller. */
183 static int m5206_find_pending_irq(m5206_mbar_state
*s
)
192 active
= s
->ipr
& ~s
->imr
;
196 for (i
= 1; i
< 14; i
++) {
197 if (active
& (1 << i
)) {
198 if ((s
->icr
[i
] & 0x1f) > level
) {
199 level
= s
->icr
[i
] & 0x1f;
211 static void m5206_mbar_update(m5206_mbar_state
*s
)
217 irq
= m5206_find_pending_irq(s
);
221 level
= (tmp
>> 2) & 7;
237 /* Unknown vector. */
238 qemu_log_mask(LOG_UNIMP
, "%s: Unhandled vector for IRQ %d\n",
248 m68k_set_irq_level(s
->cpu
, level
, vector
);
251 static void m5206_mbar_set_irq(void *opaque
, int irq
, int level
)
253 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
257 s
->ipr
&= ~(1 << irq
);
259 m5206_mbar_update(s
);
262 /* System Integration Module. */
264 static void m5206_mbar_reset(DeviceState
*dev
)
266 m5206_mbar_state
*s
= MCF5206_MBAR(dev
);
288 static uint64_t m5206_mbar_read(m5206_mbar_state
*s
,
289 uint16_t offset
, unsigned size
)
291 if (offset
>= 0x100 && offset
< 0x120) {
292 return m5206_timer_read(&s
->timer
[0], offset
- 0x100);
293 } else if (offset
>= 0x120 && offset
< 0x140) {
294 return m5206_timer_read(&s
->timer
[1], offset
- 0x120);
295 } else if (offset
>= 0x140 && offset
< 0x160) {
296 return mcf_uart_read(s
->uart
[0], offset
- 0x140, size
);
297 } else if (offset
>= 0x180 && offset
< 0x1a0) {
298 return mcf_uart_read(s
->uart
[1], offset
- 0x180, size
);
301 case 0x03: return s
->scr
;
302 case 0x14 ... 0x20: return s
->icr
[offset
- 0x13];
303 case 0x36: return s
->imr
;
304 case 0x3a: return s
->ipr
;
305 case 0x40: return s
->rsr
;
307 case 0x42: return s
->swivr
;
309 /* DRAM mask register. */
310 /* FIXME: currently hardcoded to 128Mb. */
313 while (mask
> current_machine
->ram_size
) {
316 return mask
& 0x0ffe0000;
318 case 0x5c: return 1; /* DRAM bank 1 empty. */
319 case 0xcb: return s
->par
;
320 case 0x170: return s
->uivr
[0];
321 case 0x1b0: return s
->uivr
[1];
323 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad MBAR offset 0x%"PRIx16
"\n",
328 static void m5206_mbar_write(m5206_mbar_state
*s
, uint16_t offset
,
329 uint64_t value
, unsigned size
)
331 if (offset
>= 0x100 && offset
< 0x120) {
332 m5206_timer_write(&s
->timer
[0], offset
- 0x100, value
);
334 } else if (offset
>= 0x120 && offset
< 0x140) {
335 m5206_timer_write(&s
->timer
[1], offset
- 0x120, value
);
337 } else if (offset
>= 0x140 && offset
< 0x160) {
338 mcf_uart_write(s
->uart
[0], offset
- 0x140, value
, size
);
340 } else if (offset
>= 0x180 && offset
< 0x1a0) {
341 mcf_uart_write(s
->uart
[1], offset
- 0x180, value
, size
);
349 s
->icr
[offset
- 0x13] = value
;
350 m5206_mbar_update(s
);
354 m5206_mbar_update(s
);
360 /* TODO: implement watchdog. */
371 case 0x178: case 0x17c: case 0x1c8: case 0x1bc:
372 /* Not implemented: UART Output port bits. */
378 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad MBAR offset 0x%"PRIx16
"\n",
384 /* Internal peripherals use a variety of register widths.
385 This lookup table allows a single routine to handle all of them. */
386 static const uint8_t m5206_mbar_width
[] =
388 /* 000-040 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
389 /* 040-080 */ 1, 2, 2, 2, 4, 1, 2, 4, 1, 2, 4, 2, 2, 4, 2, 2,
390 /* 080-0c0 */ 4, 2, 2, 4, 2, 2, 4, 2, 2, 4, 2, 2, 4, 2, 2, 4,
391 /* 0c0-100 */ 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
392 /* 100-140 */ 2, 2, 2, 2, 1, 0, 0, 0, 2, 2, 2, 2, 1, 0, 0, 0,
393 /* 140-180 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
394 /* 180-1c0 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
395 /* 1c0-200 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
398 static uint32_t m5206_mbar_readw(void *opaque
, hwaddr offset
);
399 static uint32_t m5206_mbar_readl(void *opaque
, hwaddr offset
);
401 static uint32_t m5206_mbar_readb(void *opaque
, hwaddr offset
)
403 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
405 if (offset
>= 0x200) {
406 qemu_log_mask(LOG_GUEST_ERROR
, "Bad MBAR read offset 0x%" HWADDR_PRIX
,
410 if (m5206_mbar_width
[offset
>> 2] > 1) {
412 val
= m5206_mbar_readw(opaque
, offset
& ~1);
413 if ((offset
& 1) == 0) {
418 return m5206_mbar_read(s
, offset
, 1);
421 static uint32_t m5206_mbar_readw(void *opaque
, hwaddr offset
)
423 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
426 if (offset
>= 0x200) {
427 qemu_log_mask(LOG_GUEST_ERROR
, "Bad MBAR read offset 0x%" HWADDR_PRIX
,
431 width
= m5206_mbar_width
[offset
>> 2];
434 val
= m5206_mbar_readl(opaque
, offset
& ~3);
435 if ((offset
& 3) == 0)
438 } else if (width
< 2) {
440 val
= m5206_mbar_readb(opaque
, offset
) << 8;
441 val
|= m5206_mbar_readb(opaque
, offset
+ 1);
444 return m5206_mbar_read(s
, offset
, 2);
447 static uint32_t m5206_mbar_readl(void *opaque
, hwaddr offset
)
449 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
452 if (offset
>= 0x200) {
453 qemu_log_mask(LOG_GUEST_ERROR
, "Bad MBAR read offset 0x%" HWADDR_PRIX
,
457 width
= m5206_mbar_width
[offset
>> 2];
460 val
= m5206_mbar_readw(opaque
, offset
) << 16;
461 val
|= m5206_mbar_readw(opaque
, offset
+ 2);
464 return m5206_mbar_read(s
, offset
, 4);
467 static void m5206_mbar_writew(void *opaque
, hwaddr offset
,
469 static void m5206_mbar_writel(void *opaque
, hwaddr offset
,
472 static void m5206_mbar_writeb(void *opaque
, hwaddr offset
,
475 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
478 if (offset
>= 0x200) {
479 qemu_log_mask(LOG_GUEST_ERROR
, "Bad MBAR write offset 0x%" HWADDR_PRIX
,
483 width
= m5206_mbar_width
[offset
>> 2];
486 tmp
= m5206_mbar_readw(opaque
, offset
& ~1);
488 tmp
= (tmp
& 0xff00) | value
;
490 tmp
= (tmp
& 0x00ff) | (value
<< 8);
492 m5206_mbar_writew(opaque
, offset
& ~1, tmp
);
495 m5206_mbar_write(s
, offset
, value
, 1);
498 static void m5206_mbar_writew(void *opaque
, hwaddr offset
,
501 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
504 if (offset
>= 0x200) {
505 qemu_log_mask(LOG_GUEST_ERROR
, "Bad MBAR write offset 0x%" HWADDR_PRIX
,
509 width
= m5206_mbar_width
[offset
>> 2];
512 tmp
= m5206_mbar_readl(opaque
, offset
& ~3);
514 tmp
= (tmp
& 0xffff0000) | value
;
516 tmp
= (tmp
& 0x0000ffff) | (value
<< 16);
518 m5206_mbar_writel(opaque
, offset
& ~3, tmp
);
520 } else if (width
< 2) {
521 m5206_mbar_writeb(opaque
, offset
, value
>> 8);
522 m5206_mbar_writeb(opaque
, offset
+ 1, value
& 0xff);
525 m5206_mbar_write(s
, offset
, value
, 2);
528 static void m5206_mbar_writel(void *opaque
, hwaddr offset
,
531 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
534 if (offset
>= 0x200) {
535 qemu_log_mask(LOG_GUEST_ERROR
, "Bad MBAR write offset 0x%" HWADDR_PRIX
,
539 width
= m5206_mbar_width
[offset
>> 2];
541 m5206_mbar_writew(opaque
, offset
, value
>> 16);
542 m5206_mbar_writew(opaque
, offset
+ 2, value
& 0xffff);
545 m5206_mbar_write(s
, offset
, value
, 4);
548 static uint64_t m5206_mbar_readfn(void *opaque
, hwaddr addr
, unsigned size
)
552 return m5206_mbar_readb(opaque
, addr
);
554 return m5206_mbar_readw(opaque
, addr
);
556 return m5206_mbar_readl(opaque
, addr
);
558 g_assert_not_reached();
562 static void m5206_mbar_writefn(void *opaque
, hwaddr addr
,
563 uint64_t value
, unsigned size
)
567 m5206_mbar_writeb(opaque
, addr
, value
);
570 m5206_mbar_writew(opaque
, addr
, value
);
573 m5206_mbar_writel(opaque
, addr
, value
);
576 g_assert_not_reached();
580 static const MemoryRegionOps m5206_mbar_ops
= {
581 .read
= m5206_mbar_readfn
,
582 .write
= m5206_mbar_writefn
,
583 .valid
.min_access_size
= 1,
584 .valid
.max_access_size
= 4,
585 .endianness
= DEVICE_NATIVE_ENDIAN
,
588 static void mcf5206_mbar_realize(DeviceState
*dev
, Error
**errp
)
590 m5206_mbar_state
*s
= MCF5206_MBAR(dev
);
592 memory_region_init_io(&s
->iomem
, NULL
, &m5206_mbar_ops
, s
,
594 sysbus_init_mmio(SYS_BUS_DEVICE(dev
), &s
->iomem
);
596 s
->pic
= qemu_allocate_irqs(m5206_mbar_set_irq
, s
, 14);
597 m5206_timer_init(&s
->timer
[0], s
->pic
[9]);
598 m5206_timer_init(&s
->timer
[1], s
->pic
[10]);
599 s
->uart
[0] = mcf_uart_create(s
->pic
[12], serial_hd(0));
600 s
->uart
[1] = mcf_uart_create(s
->pic
[13], serial_hd(1));
603 static Property mcf5206_mbar_properties
[] = {
604 DEFINE_PROP_LINK("m68k-cpu", m5206_mbar_state
, cpu
,
605 TYPE_M68K_CPU
, M68kCPU
*),
606 DEFINE_PROP_END_OF_LIST(),
609 static void mcf5206_mbar_class_init(ObjectClass
*oc
, void *data
)
611 DeviceClass
*dc
= DEVICE_CLASS(oc
);
613 device_class_set_props(dc
, mcf5206_mbar_properties
);
614 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
615 dc
->desc
= "MCF5206 system integration module";
616 dc
->realize
= mcf5206_mbar_realize
;
617 device_class_set_legacy_reset(dc
, m5206_mbar_reset
);
620 static const TypeInfo mcf5206_mbar_info
= {
621 .name
= TYPE_MCF5206_MBAR
,
622 .parent
= TYPE_SYS_BUS_DEVICE
,
623 .instance_size
= sizeof(m5206_mbar_state
),
624 .class_init
= mcf5206_mbar_class_init
,
627 static void mcf5206_mbar_register_types(void)
629 type_register_static(&mcf5206_mbar_info
);
632 type_init(mcf5206_mbar_register_types
)