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"
15 #include "hw/m68k/mcf.h"
16 #include "qemu/timer.h"
17 #include "hw/ptimer.h"
18 #include "sysemu/sysemu.h"
20 /* General purpose timer module. */
41 static void m5206_timer_update(m5206_timer_state
*s
)
43 if ((s
->tmr
& TMR_ORI
) != 0 && (s
->ter
& TER_REF
))
44 qemu_irq_raise(s
->irq
);
46 qemu_irq_lower(s
->irq
);
49 static void m5206_timer_reset(m5206_timer_state
*s
)
55 static void m5206_timer_recalibrate(m5206_timer_state
*s
)
60 ptimer_transaction_begin(s
->timer
);
61 ptimer_stop(s
->timer
);
63 if ((s
->tmr
& TMR_RST
) == 0) {
67 prescale
= (s
->tmr
>> 8) + 1;
68 mode
= (s
->tmr
>> 1) & 3;
72 if (mode
== 3 || mode
== 0)
73 hw_error("m5206_timer: mode %d not implemented\n", mode
);
74 if ((s
->tmr
& TMR_FRR
) == 0)
75 hw_error("m5206_timer: free running mode not implemented\n");
77 /* Assume 66MHz system clock. */
78 ptimer_set_freq(s
->timer
, 66000000 / prescale
);
80 ptimer_set_limit(s
->timer
, s
->trr
, 0);
82 ptimer_run(s
->timer
, 0);
84 ptimer_transaction_commit(s
->timer
);
87 static void m5206_timer_trigger(void *opaque
)
89 m5206_timer_state
*s
= (m5206_timer_state
*)opaque
;
91 m5206_timer_update(s
);
94 static uint32_t m5206_timer_read(m5206_timer_state
*s
, uint32_t addr
)
104 return s
->trr
- ptimer_get_count(s
->timer
);
112 static void m5206_timer_write(m5206_timer_state
*s
, uint32_t addr
, uint32_t val
)
116 if ((s
->tmr
& TMR_RST
) != 0 && (val
& TMR_RST
) == 0) {
117 m5206_timer_reset(s
);
120 m5206_timer_recalibrate(s
);
124 m5206_timer_recalibrate(s
);
130 ptimer_transaction_begin(s
->timer
);
131 ptimer_set_count(s
->timer
, val
);
132 ptimer_transaction_commit(s
->timer
);
140 m5206_timer_update(s
);
143 static m5206_timer_state
*m5206_timer_init(qemu_irq irq
)
145 m5206_timer_state
*s
;
147 s
= g_new0(m5206_timer_state
, 1);
148 s
->timer
= ptimer_init(m5206_timer_trigger
, s
, PTIMER_POLICY_DEFAULT
);
150 m5206_timer_reset(s
);
154 /* System Integration Module. */
159 m5206_timer_state
*timer
[2];
163 uint16_t imr
; /* 1 == interrupt is masked. */
168 /* Include the UART vector registers here. */
172 /* Interrupt controller. */
174 static int m5206_find_pending_irq(m5206_mbar_state
*s
)
183 active
= s
->ipr
& ~s
->imr
;
187 for (i
= 1; i
< 14; i
++) {
188 if (active
& (1 << i
)) {
189 if ((s
->icr
[i
] & 0x1f) > level
) {
190 level
= s
->icr
[i
] & 0x1f;
202 static void m5206_mbar_update(m5206_mbar_state
*s
)
208 irq
= m5206_find_pending_irq(s
);
212 level
= (tmp
>> 2) & 7;
228 /* Unknown vector. */
229 qemu_log_mask(LOG_UNIMP
, "%s: Unhandled vector for IRQ %d\n",
239 m68k_set_irq_level(s
->cpu
, level
, vector
);
242 static void m5206_mbar_set_irq(void *opaque
, int irq
, int level
)
244 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
248 s
->ipr
&= ~(1 << irq
);
250 m5206_mbar_update(s
);
253 /* System Integration Module. */
255 static void m5206_mbar_reset(m5206_mbar_state
*s
)
277 static uint64_t m5206_mbar_read(m5206_mbar_state
*s
,
278 uint16_t offset
, unsigned size
)
280 if (offset
>= 0x100 && offset
< 0x120) {
281 return m5206_timer_read(s
->timer
[0], offset
- 0x100);
282 } else if (offset
>= 0x120 && offset
< 0x140) {
283 return m5206_timer_read(s
->timer
[1], offset
- 0x120);
284 } else if (offset
>= 0x140 && offset
< 0x160) {
285 return mcf_uart_read(s
->uart
[0], offset
- 0x140, size
);
286 } else if (offset
>= 0x180 && offset
< 0x1a0) {
287 return mcf_uart_read(s
->uart
[1], offset
- 0x180, size
);
290 case 0x03: return s
->scr
;
291 case 0x14 ... 0x20: return s
->icr
[offset
- 0x13];
292 case 0x36: return s
->imr
;
293 case 0x3a: return s
->ipr
;
294 case 0x40: return s
->rsr
;
296 case 0x42: return s
->swivr
;
298 /* DRAM mask register. */
299 /* FIXME: currently hardcoded to 128Mb. */
302 while (mask
> ram_size
)
304 return mask
& 0x0ffe0000;
306 case 0x5c: return 1; /* DRAM bank 1 empty. */
307 case 0xcb: return s
->par
;
308 case 0x170: return s
->uivr
[0];
309 case 0x1b0: return s
->uivr
[1];
311 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad MBAR offset 0x%"PRIx16
"\n",
316 static void m5206_mbar_write(m5206_mbar_state
*s
, uint16_t offset
,
317 uint64_t value
, unsigned size
)
319 if (offset
>= 0x100 && offset
< 0x120) {
320 m5206_timer_write(s
->timer
[0], offset
- 0x100, value
);
322 } else if (offset
>= 0x120 && offset
< 0x140) {
323 m5206_timer_write(s
->timer
[1], offset
- 0x120, value
);
325 } else if (offset
>= 0x140 && offset
< 0x160) {
326 mcf_uart_write(s
->uart
[0], offset
- 0x140, value
, size
);
328 } else if (offset
>= 0x180 && offset
< 0x1a0) {
329 mcf_uart_write(s
->uart
[1], offset
- 0x180, value
, size
);
337 s
->icr
[offset
- 0x13] = value
;
338 m5206_mbar_update(s
);
342 m5206_mbar_update(s
);
348 /* TODO: implement watchdog. */
359 case 0x178: case 0x17c: case 0x1c8: case 0x1bc:
360 /* Not implemented: UART Output port bits. */
366 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad MBAR offset 0x%"PRIx16
"\n",
372 /* Internal peripherals use a variety of register widths.
373 This lookup table allows a single routine to handle all of them. */
374 static const uint8_t m5206_mbar_width
[] =
376 /* 000-040 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
377 /* 040-080 */ 1, 2, 2, 2, 4, 1, 2, 4, 1, 2, 4, 2, 2, 4, 2, 2,
378 /* 080-0c0 */ 4, 2, 2, 4, 2, 2, 4, 2, 2, 4, 2, 2, 4, 2, 2, 4,
379 /* 0c0-100 */ 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
380 /* 100-140 */ 2, 2, 2, 2, 1, 0, 0, 0, 2, 2, 2, 2, 1, 0, 0, 0,
381 /* 140-180 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
382 /* 180-1c0 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
383 /* 1c0-200 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
386 static uint32_t m5206_mbar_readw(void *opaque
, hwaddr offset
);
387 static uint32_t m5206_mbar_readl(void *opaque
, hwaddr offset
);
389 static uint32_t m5206_mbar_readb(void *opaque
, hwaddr offset
)
391 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
393 if (offset
>= 0x200) {
394 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
396 if (m5206_mbar_width
[offset
>> 2] > 1) {
398 val
= m5206_mbar_readw(opaque
, offset
& ~1);
399 if ((offset
& 1) == 0) {
404 return m5206_mbar_read(s
, offset
, 1);
407 static uint32_t m5206_mbar_readw(void *opaque
, hwaddr offset
)
409 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
412 if (offset
>= 0x200) {
413 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
415 width
= m5206_mbar_width
[offset
>> 2];
418 val
= m5206_mbar_readl(opaque
, offset
& ~3);
419 if ((offset
& 3) == 0)
422 } else if (width
< 2) {
424 val
= m5206_mbar_readb(opaque
, offset
) << 8;
425 val
|= m5206_mbar_readb(opaque
, offset
+ 1);
428 return m5206_mbar_read(s
, offset
, 2);
431 static uint32_t m5206_mbar_readl(void *opaque
, hwaddr offset
)
433 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
436 if (offset
>= 0x200) {
437 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
439 width
= m5206_mbar_width
[offset
>> 2];
442 val
= m5206_mbar_readw(opaque
, offset
) << 16;
443 val
|= m5206_mbar_readw(opaque
, offset
+ 2);
446 return m5206_mbar_read(s
, offset
, 4);
449 static void m5206_mbar_writew(void *opaque
, hwaddr offset
,
451 static void m5206_mbar_writel(void *opaque
, hwaddr offset
,
454 static void m5206_mbar_writeb(void *opaque
, hwaddr offset
,
457 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
460 if (offset
>= 0x200) {
461 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
463 width
= m5206_mbar_width
[offset
>> 2];
466 tmp
= m5206_mbar_readw(opaque
, offset
& ~1);
468 tmp
= (tmp
& 0xff00) | value
;
470 tmp
= (tmp
& 0x00ff) | (value
<< 8);
472 m5206_mbar_writew(opaque
, offset
& ~1, tmp
);
475 m5206_mbar_write(s
, offset
, value
, 1);
478 static void m5206_mbar_writew(void *opaque
, hwaddr offset
,
481 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
484 if (offset
>= 0x200) {
485 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
487 width
= m5206_mbar_width
[offset
>> 2];
490 tmp
= m5206_mbar_readl(opaque
, offset
& ~3);
492 tmp
= (tmp
& 0xffff0000) | value
;
494 tmp
= (tmp
& 0x0000ffff) | (value
<< 16);
496 m5206_mbar_writel(opaque
, offset
& ~3, tmp
);
498 } else if (width
< 2) {
499 m5206_mbar_writeb(opaque
, offset
, value
>> 8);
500 m5206_mbar_writeb(opaque
, offset
+ 1, value
& 0xff);
503 m5206_mbar_write(s
, offset
, value
, 2);
506 static void m5206_mbar_writel(void *opaque
, hwaddr offset
,
509 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
512 if (offset
>= 0x200) {
513 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
515 width
= m5206_mbar_width
[offset
>> 2];
517 m5206_mbar_writew(opaque
, offset
, value
>> 16);
518 m5206_mbar_writew(opaque
, offset
+ 2, value
& 0xffff);
521 m5206_mbar_write(s
, offset
, value
, 4);
524 static uint64_t m5206_mbar_readfn(void *opaque
, hwaddr addr
, unsigned size
)
528 return m5206_mbar_readb(opaque
, addr
);
530 return m5206_mbar_readw(opaque
, addr
);
532 return m5206_mbar_readl(opaque
, addr
);
534 g_assert_not_reached();
538 static void m5206_mbar_writefn(void *opaque
, hwaddr addr
,
539 uint64_t value
, unsigned size
)
543 m5206_mbar_writeb(opaque
, addr
, value
);
546 m5206_mbar_writew(opaque
, addr
, value
);
549 m5206_mbar_writel(opaque
, addr
, value
);
552 g_assert_not_reached();
556 static const MemoryRegionOps m5206_mbar_ops
= {
557 .read
= m5206_mbar_readfn
,
558 .write
= m5206_mbar_writefn
,
559 .valid
.min_access_size
= 1,
560 .valid
.max_access_size
= 4,
561 .endianness
= DEVICE_NATIVE_ENDIAN
,
564 qemu_irq
*mcf5206_init(MemoryRegion
*sysmem
, uint32_t base
, M68kCPU
*cpu
)
569 s
= g_new0(m5206_mbar_state
, 1);
571 memory_region_init_io(&s
->iomem
, NULL
, &m5206_mbar_ops
, s
,
573 memory_region_add_subregion(sysmem
, base
, &s
->iomem
);
575 pic
= qemu_allocate_irqs(m5206_mbar_set_irq
, s
, 14);
576 s
->timer
[0] = m5206_timer_init(pic
[9]);
577 s
->timer
[1] = m5206_timer_init(pic
[10]);
578 s
->uart
[0] = mcf_uart_init(pic
[12], serial_hd(0));
579 s
->uart
[1] = mcf_uart_init(pic
[13], serial_hd(1));