2 * Motorola ColdFire MCF5206 SoC embedded peripheral emulation.
4 * Copyright (c) 2007 CodeSourcery.
6 * This code is licensed under the GPL
8 #include "qemu/osdep.h"
9 #include "qemu-common.h"
12 #include "hw/m68k/mcf.h"
13 #include "qemu/timer.h"
14 #include "hw/ptimer.h"
15 #include "sysemu/sysemu.h"
16 #include "exec/address-spaces.h"
18 /* General purpose timer module. */
39 static void m5206_timer_update(m5206_timer_state
*s
)
41 if ((s
->tmr
& TMR_ORI
) != 0 && (s
->ter
& TER_REF
))
42 qemu_irq_raise(s
->irq
);
44 qemu_irq_lower(s
->irq
);
47 static void m5206_timer_reset(m5206_timer_state
*s
)
53 static void m5206_timer_recalibrate(m5206_timer_state
*s
)
58 ptimer_stop(s
->timer
);
60 if ((s
->tmr
& TMR_RST
) == 0)
63 prescale
= (s
->tmr
>> 8) + 1;
64 mode
= (s
->tmr
>> 1) & 3;
68 if (mode
== 3 || mode
== 0)
69 hw_error("m5206_timer: mode %d not implemented\n", mode
);
70 if ((s
->tmr
& TMR_FRR
) == 0)
71 hw_error("m5206_timer: free running mode not implemented\n");
73 /* Assume 66MHz system clock. */
74 ptimer_set_freq(s
->timer
, 66000000 / prescale
);
76 ptimer_set_limit(s
->timer
, s
->trr
, 0);
78 ptimer_run(s
->timer
, 0);
81 static void m5206_timer_trigger(void *opaque
)
83 m5206_timer_state
*s
= (m5206_timer_state
*)opaque
;
85 m5206_timer_update(s
);
88 static uint32_t m5206_timer_read(m5206_timer_state
*s
, uint32_t addr
)
98 return s
->trr
- ptimer_get_count(s
->timer
);
106 static void m5206_timer_write(m5206_timer_state
*s
, uint32_t addr
, uint32_t val
)
110 if ((s
->tmr
& TMR_RST
) != 0 && (val
& TMR_RST
) == 0) {
111 m5206_timer_reset(s
);
114 m5206_timer_recalibrate(s
);
118 m5206_timer_recalibrate(s
);
124 ptimer_set_count(s
->timer
, val
);
132 m5206_timer_update(s
);
135 static m5206_timer_state
*m5206_timer_init(qemu_irq irq
)
137 m5206_timer_state
*s
;
140 s
= (m5206_timer_state
*)g_malloc0(sizeof(m5206_timer_state
));
141 bh
= qemu_bh_new(m5206_timer_trigger
, s
);
142 s
->timer
= ptimer_init(bh
, PTIMER_POLICY_DEFAULT
);
144 m5206_timer_reset(s
);
148 /* System Integration Module. */
153 m5206_timer_state
*timer
[2];
157 uint16_t imr
; /* 1 == interrupt is masked. */
162 /* Include the UART vector registers here. */
166 /* Interrupt controller. */
168 static int m5206_find_pending_irq(m5206_mbar_state
*s
)
177 active
= s
->ipr
& ~s
->imr
;
181 for (i
= 1; i
< 14; i
++) {
182 if (active
& (1 << i
)) {
183 if ((s
->icr
[i
] & 0x1f) > level
) {
184 level
= s
->icr
[i
] & 0x1f;
196 static void m5206_mbar_update(m5206_mbar_state
*s
)
202 irq
= m5206_find_pending_irq(s
);
206 level
= (tmp
>> 2) & 7;
222 /* Unknown vector. */
223 fprintf(stderr
, "Unhandled vector for IRQ %d\n", irq
);
232 m68k_set_irq_level(s
->cpu
, level
, vector
);
235 static void m5206_mbar_set_irq(void *opaque
, int irq
, int level
)
237 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
241 s
->ipr
&= ~(1 << irq
);
243 m5206_mbar_update(s
);
246 /* System Integration Module. */
248 static void m5206_mbar_reset(m5206_mbar_state
*s
)
270 static uint64_t m5206_mbar_read(m5206_mbar_state
*s
,
271 uint64_t offset
, unsigned size
)
273 if (offset
>= 0x100 && offset
< 0x120) {
274 return m5206_timer_read(s
->timer
[0], offset
- 0x100);
275 } else if (offset
>= 0x120 && offset
< 0x140) {
276 return m5206_timer_read(s
->timer
[1], offset
- 0x120);
277 } else if (offset
>= 0x140 && offset
< 0x160) {
278 return mcf_uart_read(s
->uart
[0], offset
- 0x140, size
);
279 } else if (offset
>= 0x180 && offset
< 0x1a0) {
280 return mcf_uart_read(s
->uart
[1], offset
- 0x180, size
);
283 case 0x03: return s
->scr
;
284 case 0x14 ... 0x20: return s
->icr
[offset
- 0x13];
285 case 0x36: return s
->imr
;
286 case 0x3a: return s
->ipr
;
287 case 0x40: return s
->rsr
;
289 case 0x42: return s
->swivr
;
291 /* DRAM mask register. */
292 /* FIXME: currently hardcoded to 128Mb. */
295 while (mask
> ram_size
)
297 return mask
& 0x0ffe0000;
299 case 0x5c: return 1; /* DRAM bank 1 empty. */
300 case 0xcb: return s
->par
;
301 case 0x170: return s
->uivr
[0];
302 case 0x1b0: return s
->uivr
[1];
304 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
308 static void m5206_mbar_write(m5206_mbar_state
*s
, uint32_t offset
,
309 uint64_t value
, unsigned size
)
311 if (offset
>= 0x100 && offset
< 0x120) {
312 m5206_timer_write(s
->timer
[0], offset
- 0x100, value
);
314 } else if (offset
>= 0x120 && offset
< 0x140) {
315 m5206_timer_write(s
->timer
[1], offset
- 0x120, value
);
317 } else if (offset
>= 0x140 && offset
< 0x160) {
318 mcf_uart_write(s
->uart
[0], offset
- 0x140, value
, size
);
320 } else if (offset
>= 0x180 && offset
< 0x1a0) {
321 mcf_uart_write(s
->uart
[1], offset
- 0x180, value
, size
);
329 s
->icr
[offset
- 0x13] = value
;
330 m5206_mbar_update(s
);
334 m5206_mbar_update(s
);
340 /* TODO: implement watchdog. */
351 case 0x178: case 0x17c: case 0x1c8: case 0x1bc:
352 /* Not implemented: UART Output port bits. */
358 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
363 /* Internal peripherals use a variety of register widths.
364 This lookup table allows a single routine to handle all of them. */
365 static const uint8_t m5206_mbar_width
[] =
367 /* 000-040 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
368 /* 040-080 */ 1, 2, 2, 2, 4, 1, 2, 4, 1, 2, 4, 2, 2, 4, 2, 2,
369 /* 080-0c0 */ 4, 2, 2, 4, 2, 2, 4, 2, 2, 4, 2, 2, 4, 2, 2, 4,
370 /* 0c0-100 */ 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
371 /* 100-140 */ 2, 2, 2, 2, 1, 0, 0, 0, 2, 2, 2, 2, 1, 0, 0, 0,
372 /* 140-180 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
373 /* 180-1c0 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
374 /* 1c0-200 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
377 static uint32_t m5206_mbar_readw(void *opaque
, hwaddr offset
);
378 static uint32_t m5206_mbar_readl(void *opaque
, hwaddr offset
);
380 static uint32_t m5206_mbar_readb(void *opaque
, hwaddr offset
)
382 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
384 if (offset
>= 0x200) {
385 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
387 if (m5206_mbar_width
[offset
>> 2] > 1) {
389 val
= m5206_mbar_readw(opaque
, offset
& ~1);
390 if ((offset
& 1) == 0) {
395 return m5206_mbar_read(s
, offset
, 1);
398 static uint32_t m5206_mbar_readw(void *opaque
, hwaddr offset
)
400 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
403 if (offset
>= 0x200) {
404 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
406 width
= m5206_mbar_width
[offset
>> 2];
409 val
= m5206_mbar_readl(opaque
, offset
& ~3);
410 if ((offset
& 3) == 0)
413 } else if (width
< 2) {
415 val
= m5206_mbar_readb(opaque
, offset
) << 8;
416 val
|= m5206_mbar_readb(opaque
, offset
+ 1);
419 return m5206_mbar_read(s
, offset
, 2);
422 static uint32_t m5206_mbar_readl(void *opaque
, hwaddr offset
)
424 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
427 if (offset
>= 0x200) {
428 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
430 width
= m5206_mbar_width
[offset
>> 2];
433 val
= m5206_mbar_readw(opaque
, offset
) << 16;
434 val
|= m5206_mbar_readw(opaque
, offset
+ 2);
437 return m5206_mbar_read(s
, offset
, 4);
440 static void m5206_mbar_writew(void *opaque
, hwaddr offset
,
442 static void m5206_mbar_writel(void *opaque
, hwaddr offset
,
445 static void m5206_mbar_writeb(void *opaque
, hwaddr offset
,
448 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
451 if (offset
>= 0x200) {
452 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
454 width
= m5206_mbar_width
[offset
>> 2];
457 tmp
= m5206_mbar_readw(opaque
, offset
& ~1);
459 tmp
= (tmp
& 0xff00) | value
;
461 tmp
= (tmp
& 0x00ff) | (value
<< 8);
463 m5206_mbar_writew(opaque
, offset
& ~1, tmp
);
466 m5206_mbar_write(s
, offset
, value
, 1);
469 static void m5206_mbar_writew(void *opaque
, hwaddr offset
,
472 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
475 if (offset
>= 0x200) {
476 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
478 width
= m5206_mbar_width
[offset
>> 2];
481 tmp
= m5206_mbar_readl(opaque
, offset
& ~3);
483 tmp
= (tmp
& 0xffff0000) | value
;
485 tmp
= (tmp
& 0x0000ffff) | (value
<< 16);
487 m5206_mbar_writel(opaque
, offset
& ~3, tmp
);
489 } else if (width
< 2) {
490 m5206_mbar_writeb(opaque
, offset
, value
>> 8);
491 m5206_mbar_writeb(opaque
, offset
+ 1, value
& 0xff);
494 m5206_mbar_write(s
, offset
, value
, 2);
497 static void m5206_mbar_writel(void *opaque
, hwaddr offset
,
500 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
503 if (offset
>= 0x200) {
504 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
506 width
= m5206_mbar_width
[offset
>> 2];
508 m5206_mbar_writew(opaque
, offset
, value
>> 16);
509 m5206_mbar_writew(opaque
, offset
+ 2, value
& 0xffff);
512 m5206_mbar_write(s
, offset
, value
, 4);
515 static const MemoryRegionOps m5206_mbar_ops
= {
528 .endianness
= DEVICE_NATIVE_ENDIAN
,
531 qemu_irq
*mcf5206_init(MemoryRegion
*sysmem
, uint32_t base
, M68kCPU
*cpu
)
536 s
= (m5206_mbar_state
*)g_malloc0(sizeof(m5206_mbar_state
));
538 memory_region_init_io(&s
->iomem
, NULL
, &m5206_mbar_ops
, s
,
540 memory_region_add_subregion(sysmem
, base
, &s
->iomem
);
542 pic
= qemu_allocate_irqs(m5206_mbar_set_irq
, s
, 14);
543 s
->timer
[0] = m5206_timer_init(pic
[9]);
544 s
->timer
[1] = m5206_timer_init(pic
[10]);
545 s
->uart
[0] = mcf_uart_init(pic
[12], serial_hds
[0]);
546 s
->uart
[1] = mcf_uart_init(pic
[13], serial_hds
[1]);