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"
11 #include "qemu/main-loop.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_stop(s
->timer
);
62 if ((s
->tmr
& TMR_RST
) == 0)
65 prescale
= (s
->tmr
>> 8) + 1;
66 mode
= (s
->tmr
>> 1) & 3;
70 if (mode
== 3 || mode
== 0)
71 hw_error("m5206_timer: mode %d not implemented\n", mode
);
72 if ((s
->tmr
& TMR_FRR
) == 0)
73 hw_error("m5206_timer: free running mode not implemented\n");
75 /* Assume 66MHz system clock. */
76 ptimer_set_freq(s
->timer
, 66000000 / prescale
);
78 ptimer_set_limit(s
->timer
, s
->trr
, 0);
80 ptimer_run(s
->timer
, 0);
83 static void m5206_timer_trigger(void *opaque
)
85 m5206_timer_state
*s
= (m5206_timer_state
*)opaque
;
87 m5206_timer_update(s
);
90 static uint32_t m5206_timer_read(m5206_timer_state
*s
, uint32_t addr
)
100 return s
->trr
- ptimer_get_count(s
->timer
);
108 static void m5206_timer_write(m5206_timer_state
*s
, uint32_t addr
, uint32_t val
)
112 if ((s
->tmr
& TMR_RST
) != 0 && (val
& TMR_RST
) == 0) {
113 m5206_timer_reset(s
);
116 m5206_timer_recalibrate(s
);
120 m5206_timer_recalibrate(s
);
126 ptimer_set_count(s
->timer
, val
);
134 m5206_timer_update(s
);
137 static m5206_timer_state
*m5206_timer_init(qemu_irq irq
)
139 m5206_timer_state
*s
;
142 s
= g_new0(m5206_timer_state
, 1);
143 bh
= qemu_bh_new(m5206_timer_trigger
, s
);
144 s
->timer
= ptimer_init(bh
, PTIMER_POLICY_DEFAULT
);
146 m5206_timer_reset(s
);
150 /* System Integration Module. */
155 m5206_timer_state
*timer
[2];
159 uint16_t imr
; /* 1 == interrupt is masked. */
164 /* Include the UART vector registers here. */
168 /* Interrupt controller. */
170 static int m5206_find_pending_irq(m5206_mbar_state
*s
)
179 active
= s
->ipr
& ~s
->imr
;
183 for (i
= 1; i
< 14; i
++) {
184 if (active
& (1 << i
)) {
185 if ((s
->icr
[i
] & 0x1f) > level
) {
186 level
= s
->icr
[i
] & 0x1f;
198 static void m5206_mbar_update(m5206_mbar_state
*s
)
204 irq
= m5206_find_pending_irq(s
);
208 level
= (tmp
>> 2) & 7;
224 /* Unknown vector. */
225 error_report("Unhandled vector for IRQ %d", irq
);
234 m68k_set_irq_level(s
->cpu
, level
, vector
);
237 static void m5206_mbar_set_irq(void *opaque
, int irq
, int level
)
239 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
243 s
->ipr
&= ~(1 << irq
);
245 m5206_mbar_update(s
);
248 /* System Integration Module. */
250 static void m5206_mbar_reset(m5206_mbar_state
*s
)
272 static uint64_t m5206_mbar_read(m5206_mbar_state
*s
,
273 uint64_t offset
, unsigned size
)
275 if (offset
>= 0x100 && offset
< 0x120) {
276 return m5206_timer_read(s
->timer
[0], offset
- 0x100);
277 } else if (offset
>= 0x120 && offset
< 0x140) {
278 return m5206_timer_read(s
->timer
[1], offset
- 0x120);
279 } else if (offset
>= 0x140 && offset
< 0x160) {
280 return mcf_uart_read(s
->uart
[0], offset
- 0x140, size
);
281 } else if (offset
>= 0x180 && offset
< 0x1a0) {
282 return mcf_uart_read(s
->uart
[1], offset
- 0x180, size
);
285 case 0x03: return s
->scr
;
286 case 0x14 ... 0x20: return s
->icr
[offset
- 0x13];
287 case 0x36: return s
->imr
;
288 case 0x3a: return s
->ipr
;
289 case 0x40: return s
->rsr
;
291 case 0x42: return s
->swivr
;
293 /* DRAM mask register. */
294 /* FIXME: currently hardcoded to 128Mb. */
297 while (mask
> ram_size
)
299 return mask
& 0x0ffe0000;
301 case 0x5c: return 1; /* DRAM bank 1 empty. */
302 case 0xcb: return s
->par
;
303 case 0x170: return s
->uivr
[0];
304 case 0x1b0: return s
->uivr
[1];
306 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
310 static void m5206_mbar_write(m5206_mbar_state
*s
, uint32_t offset
,
311 uint64_t value
, unsigned size
)
313 if (offset
>= 0x100 && offset
< 0x120) {
314 m5206_timer_write(s
->timer
[0], offset
- 0x100, value
);
316 } else if (offset
>= 0x120 && offset
< 0x140) {
317 m5206_timer_write(s
->timer
[1], offset
- 0x120, value
);
319 } else if (offset
>= 0x140 && offset
< 0x160) {
320 mcf_uart_write(s
->uart
[0], offset
- 0x140, value
, size
);
322 } else if (offset
>= 0x180 && offset
< 0x1a0) {
323 mcf_uart_write(s
->uart
[1], offset
- 0x180, value
, size
);
331 s
->icr
[offset
- 0x13] = value
;
332 m5206_mbar_update(s
);
336 m5206_mbar_update(s
);
342 /* TODO: implement watchdog. */
353 case 0x178: case 0x17c: case 0x1c8: case 0x1bc:
354 /* Not implemented: UART Output port bits. */
360 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
365 /* Internal peripherals use a variety of register widths.
366 This lookup table allows a single routine to handle all of them. */
367 static const uint8_t m5206_mbar_width
[] =
369 /* 000-040 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
370 /* 040-080 */ 1, 2, 2, 2, 4, 1, 2, 4, 1, 2, 4, 2, 2, 4, 2, 2,
371 /* 080-0c0 */ 4, 2, 2, 4, 2, 2, 4, 2, 2, 4, 2, 2, 4, 2, 2, 4,
372 /* 0c0-100 */ 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
373 /* 100-140 */ 2, 2, 2, 2, 1, 0, 0, 0, 2, 2, 2, 2, 1, 0, 0, 0,
374 /* 140-180 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
375 /* 180-1c0 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
376 /* 1c0-200 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
379 static uint32_t m5206_mbar_readw(void *opaque
, hwaddr offset
);
380 static uint32_t m5206_mbar_readl(void *opaque
, hwaddr offset
);
382 static uint32_t m5206_mbar_readb(void *opaque
, hwaddr offset
)
384 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
386 if (offset
>= 0x200) {
387 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
389 if (m5206_mbar_width
[offset
>> 2] > 1) {
391 val
= m5206_mbar_readw(opaque
, offset
& ~1);
392 if ((offset
& 1) == 0) {
397 return m5206_mbar_read(s
, offset
, 1);
400 static uint32_t m5206_mbar_readw(void *opaque
, hwaddr offset
)
402 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
405 if (offset
>= 0x200) {
406 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
408 width
= m5206_mbar_width
[offset
>> 2];
411 val
= m5206_mbar_readl(opaque
, offset
& ~3);
412 if ((offset
& 3) == 0)
415 } else if (width
< 2) {
417 val
= m5206_mbar_readb(opaque
, offset
) << 8;
418 val
|= m5206_mbar_readb(opaque
, offset
+ 1);
421 return m5206_mbar_read(s
, offset
, 2);
424 static uint32_t m5206_mbar_readl(void *opaque
, hwaddr offset
)
426 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
429 if (offset
>= 0x200) {
430 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
432 width
= m5206_mbar_width
[offset
>> 2];
435 val
= m5206_mbar_readw(opaque
, offset
) << 16;
436 val
|= m5206_mbar_readw(opaque
, offset
+ 2);
439 return m5206_mbar_read(s
, offset
, 4);
442 static void m5206_mbar_writew(void *opaque
, hwaddr offset
,
444 static void m5206_mbar_writel(void *opaque
, hwaddr offset
,
447 static void m5206_mbar_writeb(void *opaque
, hwaddr offset
,
450 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
453 if (offset
>= 0x200) {
454 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
456 width
= m5206_mbar_width
[offset
>> 2];
459 tmp
= m5206_mbar_readw(opaque
, offset
& ~1);
461 tmp
= (tmp
& 0xff00) | value
;
463 tmp
= (tmp
& 0x00ff) | (value
<< 8);
465 m5206_mbar_writew(opaque
, offset
& ~1, tmp
);
468 m5206_mbar_write(s
, offset
, value
, 1);
471 static void m5206_mbar_writew(void *opaque
, hwaddr offset
,
474 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
477 if (offset
>= 0x200) {
478 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
480 width
= m5206_mbar_width
[offset
>> 2];
483 tmp
= m5206_mbar_readl(opaque
, offset
& ~3);
485 tmp
= (tmp
& 0xffff0000) | value
;
487 tmp
= (tmp
& 0x0000ffff) | (value
<< 16);
489 m5206_mbar_writel(opaque
, offset
& ~3, tmp
);
491 } else if (width
< 2) {
492 m5206_mbar_writeb(opaque
, offset
, value
>> 8);
493 m5206_mbar_writeb(opaque
, offset
+ 1, value
& 0xff);
496 m5206_mbar_write(s
, offset
, value
, 2);
499 static void m5206_mbar_writel(void *opaque
, hwaddr offset
,
502 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
505 if (offset
>= 0x200) {
506 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
508 width
= m5206_mbar_width
[offset
>> 2];
510 m5206_mbar_writew(opaque
, offset
, value
>> 16);
511 m5206_mbar_writew(opaque
, offset
+ 2, value
& 0xffff);
514 m5206_mbar_write(s
, offset
, value
, 4);
517 static uint64_t m5206_mbar_readfn(void *opaque
, hwaddr addr
, unsigned size
)
521 return m5206_mbar_readb(opaque
, addr
);
523 return m5206_mbar_readw(opaque
, addr
);
525 return m5206_mbar_readl(opaque
, addr
);
527 g_assert_not_reached();
531 static void m5206_mbar_writefn(void *opaque
, hwaddr addr
,
532 uint64_t value
, unsigned size
)
536 m5206_mbar_writeb(opaque
, addr
, value
);
539 m5206_mbar_writew(opaque
, addr
, value
);
542 m5206_mbar_writel(opaque
, addr
, value
);
545 g_assert_not_reached();
549 static const MemoryRegionOps m5206_mbar_ops
= {
550 .read
= m5206_mbar_readfn
,
551 .write
= m5206_mbar_writefn
,
552 .valid
.min_access_size
= 1,
553 .valid
.max_access_size
= 4,
554 .endianness
= DEVICE_NATIVE_ENDIAN
,
557 qemu_irq
*mcf5206_init(MemoryRegion
*sysmem
, uint32_t base
, M68kCPU
*cpu
)
562 s
= g_new0(m5206_mbar_state
, 1);
564 memory_region_init_io(&s
->iomem
, NULL
, &m5206_mbar_ops
, s
,
566 memory_region_add_subregion(sysmem
, base
, &s
->iomem
);
568 pic
= qemu_allocate_irqs(m5206_mbar_set_irq
, s
, 14);
569 s
->timer
[0] = m5206_timer_init(pic
[9]);
570 s
->timer
[1] = m5206_timer_init(pic
[10]);
571 s
->uart
[0] = mcf_uart_init(pic
[12], serial_hd(0));
572 s
->uart
[1] = mcf_uart_init(pic
[13], serial_hd(1));