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"
14 #include "hw/m68k/mcf.h"
15 #include "qemu/timer.h"
16 #include "hw/ptimer.h"
17 #include "sysemu/sysemu.h"
19 /* General purpose timer module. */
40 static void m5206_timer_update(m5206_timer_state
*s
)
42 if ((s
->tmr
& TMR_ORI
) != 0 && (s
->ter
& TER_REF
))
43 qemu_irq_raise(s
->irq
);
45 qemu_irq_lower(s
->irq
);
48 static void m5206_timer_reset(m5206_timer_state
*s
)
54 static void m5206_timer_recalibrate(m5206_timer_state
*s
)
59 ptimer_transaction_begin(s
->timer
);
60 ptimer_stop(s
->timer
);
62 if ((s
->tmr
& TMR_RST
) == 0) {
66 prescale
= (s
->tmr
>> 8) + 1;
67 mode
= (s
->tmr
>> 1) & 3;
71 if (mode
== 3 || mode
== 0) {
72 qemu_log_mask(LOG_UNIMP
, "m5206_timer: mode %d not implemented\n",
76 if ((s
->tmr
& TMR_FRR
) == 0) {
77 qemu_log_mask(LOG_UNIMP
,
78 "m5206_timer: free running mode not implemented\n");
82 /* Assume 66MHz system clock. */
83 ptimer_set_freq(s
->timer
, 66000000 / prescale
);
85 ptimer_set_limit(s
->timer
, s
->trr
, 0);
87 ptimer_run(s
->timer
, 0);
89 ptimer_transaction_commit(s
->timer
);
92 static void m5206_timer_trigger(void *opaque
)
94 m5206_timer_state
*s
= (m5206_timer_state
*)opaque
;
96 m5206_timer_update(s
);
99 static uint32_t m5206_timer_read(m5206_timer_state
*s
, uint32_t addr
)
109 return s
->trr
- ptimer_get_count(s
->timer
);
117 static void m5206_timer_write(m5206_timer_state
*s
, uint32_t addr
, uint32_t val
)
121 if ((s
->tmr
& TMR_RST
) != 0 && (val
& TMR_RST
) == 0) {
122 m5206_timer_reset(s
);
125 m5206_timer_recalibrate(s
);
129 m5206_timer_recalibrate(s
);
135 ptimer_transaction_begin(s
->timer
);
136 ptimer_set_count(s
->timer
, val
);
137 ptimer_transaction_commit(s
->timer
);
145 m5206_timer_update(s
);
148 static m5206_timer_state
*m5206_timer_init(qemu_irq irq
)
150 m5206_timer_state
*s
;
152 s
= g_new0(m5206_timer_state
, 1);
153 s
->timer
= ptimer_init(m5206_timer_trigger
, s
, PTIMER_POLICY_DEFAULT
);
155 m5206_timer_reset(s
);
159 /* System Integration Module. */
164 m5206_timer_state
*timer
[2];
168 uint16_t imr
; /* 1 == interrupt is masked. */
173 /* Include the UART vector registers here. */
177 /* Interrupt controller. */
179 static int m5206_find_pending_irq(m5206_mbar_state
*s
)
188 active
= s
->ipr
& ~s
->imr
;
192 for (i
= 1; i
< 14; i
++) {
193 if (active
& (1 << i
)) {
194 if ((s
->icr
[i
] & 0x1f) > level
) {
195 level
= s
->icr
[i
] & 0x1f;
207 static void m5206_mbar_update(m5206_mbar_state
*s
)
213 irq
= m5206_find_pending_irq(s
);
217 level
= (tmp
>> 2) & 7;
233 /* Unknown vector. */
234 qemu_log_mask(LOG_UNIMP
, "%s: Unhandled vector for IRQ %d\n",
244 m68k_set_irq_level(s
->cpu
, level
, vector
);
247 static void m5206_mbar_set_irq(void *opaque
, int irq
, int level
)
249 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
253 s
->ipr
&= ~(1 << irq
);
255 m5206_mbar_update(s
);
258 /* System Integration Module. */
260 static void m5206_mbar_reset(m5206_mbar_state
*s
)
282 static uint64_t m5206_mbar_read(m5206_mbar_state
*s
,
283 uint16_t offset
, unsigned size
)
285 if (offset
>= 0x100 && offset
< 0x120) {
286 return m5206_timer_read(s
->timer
[0], offset
- 0x100);
287 } else if (offset
>= 0x120 && offset
< 0x140) {
288 return m5206_timer_read(s
->timer
[1], offset
- 0x120);
289 } else if (offset
>= 0x140 && offset
< 0x160) {
290 return mcf_uart_read(s
->uart
[0], offset
- 0x140, size
);
291 } else if (offset
>= 0x180 && offset
< 0x1a0) {
292 return mcf_uart_read(s
->uart
[1], offset
- 0x180, size
);
295 case 0x03: return s
->scr
;
296 case 0x14 ... 0x20: return s
->icr
[offset
- 0x13];
297 case 0x36: return s
->imr
;
298 case 0x3a: return s
->ipr
;
299 case 0x40: return s
->rsr
;
301 case 0x42: return s
->swivr
;
303 /* DRAM mask register. */
304 /* FIXME: currently hardcoded to 128Mb. */
307 while (mask
> ram_size
)
309 return mask
& 0x0ffe0000;
311 case 0x5c: return 1; /* DRAM bank 1 empty. */
312 case 0xcb: return s
->par
;
313 case 0x170: return s
->uivr
[0];
314 case 0x1b0: return s
->uivr
[1];
316 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad MBAR offset 0x%"PRIx16
"\n",
321 static void m5206_mbar_write(m5206_mbar_state
*s
, uint16_t offset
,
322 uint64_t value
, unsigned size
)
324 if (offset
>= 0x100 && offset
< 0x120) {
325 m5206_timer_write(s
->timer
[0], offset
- 0x100, value
);
327 } else if (offset
>= 0x120 && offset
< 0x140) {
328 m5206_timer_write(s
->timer
[1], offset
- 0x120, value
);
330 } else if (offset
>= 0x140 && offset
< 0x160) {
331 mcf_uart_write(s
->uart
[0], offset
- 0x140, value
, size
);
333 } else if (offset
>= 0x180 && offset
< 0x1a0) {
334 mcf_uart_write(s
->uart
[1], offset
- 0x180, value
, size
);
342 s
->icr
[offset
- 0x13] = value
;
343 m5206_mbar_update(s
);
347 m5206_mbar_update(s
);
353 /* TODO: implement watchdog. */
364 case 0x178: case 0x17c: case 0x1c8: case 0x1bc:
365 /* Not implemented: UART Output port bits. */
371 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad MBAR offset 0x%"PRIx16
"\n",
377 /* Internal peripherals use a variety of register widths.
378 This lookup table allows a single routine to handle all of them. */
379 static const uint8_t m5206_mbar_width
[] =
381 /* 000-040 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
382 /* 040-080 */ 1, 2, 2, 2, 4, 1, 2, 4, 1, 2, 4, 2, 2, 4, 2, 2,
383 /* 080-0c0 */ 4, 2, 2, 4, 2, 2, 4, 2, 2, 4, 2, 2, 4, 2, 2, 4,
384 /* 0c0-100 */ 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
385 /* 100-140 */ 2, 2, 2, 2, 1, 0, 0, 0, 2, 2, 2, 2, 1, 0, 0, 0,
386 /* 140-180 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
387 /* 180-1c0 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
388 /* 1c0-200 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
391 static uint32_t m5206_mbar_readw(void *opaque
, hwaddr offset
);
392 static uint32_t m5206_mbar_readl(void *opaque
, hwaddr offset
);
394 static uint32_t m5206_mbar_readb(void *opaque
, hwaddr offset
)
396 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
398 if (offset
>= 0x200) {
399 qemu_log_mask(LOG_GUEST_ERROR
, "Bad MBAR read offset 0x%" HWADDR_PRIX
,
403 if (m5206_mbar_width
[offset
>> 2] > 1) {
405 val
= m5206_mbar_readw(opaque
, offset
& ~1);
406 if ((offset
& 1) == 0) {
411 return m5206_mbar_read(s
, offset
, 1);
414 static uint32_t m5206_mbar_readw(void *opaque
, hwaddr offset
)
416 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
419 if (offset
>= 0x200) {
420 qemu_log_mask(LOG_GUEST_ERROR
, "Bad MBAR read offset 0x%" HWADDR_PRIX
,
424 width
= m5206_mbar_width
[offset
>> 2];
427 val
= m5206_mbar_readl(opaque
, offset
& ~3);
428 if ((offset
& 3) == 0)
431 } else if (width
< 2) {
433 val
= m5206_mbar_readb(opaque
, offset
) << 8;
434 val
|= m5206_mbar_readb(opaque
, offset
+ 1);
437 return m5206_mbar_read(s
, offset
, 2);
440 static uint32_t m5206_mbar_readl(void *opaque
, hwaddr offset
)
442 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
445 if (offset
>= 0x200) {
446 qemu_log_mask(LOG_GUEST_ERROR
, "Bad MBAR read offset 0x%" HWADDR_PRIX
,
450 width
= m5206_mbar_width
[offset
>> 2];
453 val
= m5206_mbar_readw(opaque
, offset
) << 16;
454 val
|= m5206_mbar_readw(opaque
, offset
+ 2);
457 return m5206_mbar_read(s
, offset
, 4);
460 static void m5206_mbar_writew(void *opaque
, hwaddr offset
,
462 static void m5206_mbar_writel(void *opaque
, hwaddr offset
,
465 static void m5206_mbar_writeb(void *opaque
, hwaddr offset
,
468 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
471 if (offset
>= 0x200) {
472 qemu_log_mask(LOG_GUEST_ERROR
, "Bad MBAR write offset 0x%" HWADDR_PRIX
,
476 width
= m5206_mbar_width
[offset
>> 2];
479 tmp
= m5206_mbar_readw(opaque
, offset
& ~1);
481 tmp
= (tmp
& 0xff00) | value
;
483 tmp
= (tmp
& 0x00ff) | (value
<< 8);
485 m5206_mbar_writew(opaque
, offset
& ~1, tmp
);
488 m5206_mbar_write(s
, offset
, value
, 1);
491 static void m5206_mbar_writew(void *opaque
, hwaddr offset
,
494 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
497 if (offset
>= 0x200) {
498 qemu_log_mask(LOG_GUEST_ERROR
, "Bad MBAR write offset 0x%" HWADDR_PRIX
,
502 width
= m5206_mbar_width
[offset
>> 2];
505 tmp
= m5206_mbar_readl(opaque
, offset
& ~3);
507 tmp
= (tmp
& 0xffff0000) | value
;
509 tmp
= (tmp
& 0x0000ffff) | (value
<< 16);
511 m5206_mbar_writel(opaque
, offset
& ~3, tmp
);
513 } else if (width
< 2) {
514 m5206_mbar_writeb(opaque
, offset
, value
>> 8);
515 m5206_mbar_writeb(opaque
, offset
+ 1, value
& 0xff);
518 m5206_mbar_write(s
, offset
, value
, 2);
521 static void m5206_mbar_writel(void *opaque
, hwaddr offset
,
524 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
527 if (offset
>= 0x200) {
528 qemu_log_mask(LOG_GUEST_ERROR
, "Bad MBAR write offset 0x%" HWADDR_PRIX
,
532 width
= m5206_mbar_width
[offset
>> 2];
534 m5206_mbar_writew(opaque
, offset
, value
>> 16);
535 m5206_mbar_writew(opaque
, offset
+ 2, value
& 0xffff);
538 m5206_mbar_write(s
, offset
, value
, 4);
541 static uint64_t m5206_mbar_readfn(void *opaque
, hwaddr addr
, unsigned size
)
545 return m5206_mbar_readb(opaque
, addr
);
547 return m5206_mbar_readw(opaque
, addr
);
549 return m5206_mbar_readl(opaque
, addr
);
551 g_assert_not_reached();
555 static void m5206_mbar_writefn(void *opaque
, hwaddr addr
,
556 uint64_t value
, unsigned size
)
560 m5206_mbar_writeb(opaque
, addr
, value
);
563 m5206_mbar_writew(opaque
, addr
, value
);
566 m5206_mbar_writel(opaque
, addr
, value
);
569 g_assert_not_reached();
573 static const MemoryRegionOps m5206_mbar_ops
= {
574 .read
= m5206_mbar_readfn
,
575 .write
= m5206_mbar_writefn
,
576 .valid
.min_access_size
= 1,
577 .valid
.max_access_size
= 4,
578 .endianness
= DEVICE_NATIVE_ENDIAN
,
581 qemu_irq
*mcf5206_init(MemoryRegion
*sysmem
, uint32_t base
, M68kCPU
*cpu
)
586 s
= g_new0(m5206_mbar_state
, 1);
588 memory_region_init_io(&s
->iomem
, NULL
, &m5206_mbar_ops
, s
,
590 memory_region_add_subregion(sysmem
, base
, &s
->iomem
);
592 pic
= qemu_allocate_irqs(m5206_mbar_set_irq
, s
, 14);
593 s
->timer
[0] = m5206_timer_init(pic
[9]);
594 s
->timer
[1] = m5206_timer_init(pic
[10]);
595 s
->uart
[0] = mcf_uart_init(pic
[12], serial_hd(0));
596 s
->uart
[1] = mcf_uart_init(pic
[13], serial_hd(1));