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 hw_error("m5206_timer: mode %d not implemented\n", mode
);
73 if ((s
->tmr
& TMR_FRR
) == 0)
74 hw_error("m5206_timer: free running mode not implemented\n");
76 /* Assume 66MHz system clock. */
77 ptimer_set_freq(s
->timer
, 66000000 / prescale
);
79 ptimer_set_limit(s
->timer
, s
->trr
, 0);
81 ptimer_run(s
->timer
, 0);
83 ptimer_transaction_commit(s
->timer
);
86 static void m5206_timer_trigger(void *opaque
)
88 m5206_timer_state
*s
= (m5206_timer_state
*)opaque
;
90 m5206_timer_update(s
);
93 static uint32_t m5206_timer_read(m5206_timer_state
*s
, uint32_t addr
)
103 return s
->trr
- ptimer_get_count(s
->timer
);
111 static void m5206_timer_write(m5206_timer_state
*s
, uint32_t addr
, uint32_t val
)
115 if ((s
->tmr
& TMR_RST
) != 0 && (val
& TMR_RST
) == 0) {
116 m5206_timer_reset(s
);
119 m5206_timer_recalibrate(s
);
123 m5206_timer_recalibrate(s
);
129 ptimer_transaction_begin(s
->timer
);
130 ptimer_set_count(s
->timer
, val
);
131 ptimer_transaction_commit(s
->timer
);
139 m5206_timer_update(s
);
142 static m5206_timer_state
*m5206_timer_init(qemu_irq irq
)
144 m5206_timer_state
*s
;
146 s
= g_new0(m5206_timer_state
, 1);
147 s
->timer
= ptimer_init(m5206_timer_trigger
, s
, PTIMER_POLICY_DEFAULT
);
149 m5206_timer_reset(s
);
153 /* System Integration Module. */
158 m5206_timer_state
*timer
[2];
162 uint16_t imr
; /* 1 == interrupt is masked. */
167 /* Include the UART vector registers here. */
171 /* Interrupt controller. */
173 static int m5206_find_pending_irq(m5206_mbar_state
*s
)
182 active
= s
->ipr
& ~s
->imr
;
186 for (i
= 1; i
< 14; i
++) {
187 if (active
& (1 << i
)) {
188 if ((s
->icr
[i
] & 0x1f) > level
) {
189 level
= s
->icr
[i
] & 0x1f;
201 static void m5206_mbar_update(m5206_mbar_state
*s
)
207 irq
= m5206_find_pending_irq(s
);
211 level
= (tmp
>> 2) & 7;
227 /* Unknown vector. */
228 error_report("Unhandled vector for IRQ %d", irq
);
237 m68k_set_irq_level(s
->cpu
, level
, vector
);
240 static void m5206_mbar_set_irq(void *opaque
, int irq
, int level
)
242 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
246 s
->ipr
&= ~(1 << irq
);
248 m5206_mbar_update(s
);
251 /* System Integration Module. */
253 static void m5206_mbar_reset(m5206_mbar_state
*s
)
275 static uint64_t m5206_mbar_read(m5206_mbar_state
*s
,
276 uint64_t offset
, unsigned size
)
278 if (offset
>= 0x100 && offset
< 0x120) {
279 return m5206_timer_read(s
->timer
[0], offset
- 0x100);
280 } else if (offset
>= 0x120 && offset
< 0x140) {
281 return m5206_timer_read(s
->timer
[1], offset
- 0x120);
282 } else if (offset
>= 0x140 && offset
< 0x160) {
283 return mcf_uart_read(s
->uart
[0], offset
- 0x140, size
);
284 } else if (offset
>= 0x180 && offset
< 0x1a0) {
285 return mcf_uart_read(s
->uart
[1], offset
- 0x180, size
);
288 case 0x03: return s
->scr
;
289 case 0x14 ... 0x20: return s
->icr
[offset
- 0x13];
290 case 0x36: return s
->imr
;
291 case 0x3a: return s
->ipr
;
292 case 0x40: return s
->rsr
;
294 case 0x42: return s
->swivr
;
296 /* DRAM mask register. */
297 /* FIXME: currently hardcoded to 128Mb. */
300 while (mask
> ram_size
)
302 return mask
& 0x0ffe0000;
304 case 0x5c: return 1; /* DRAM bank 1 empty. */
305 case 0xcb: return s
->par
;
306 case 0x170: return s
->uivr
[0];
307 case 0x1b0: return s
->uivr
[1];
309 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
313 static void m5206_mbar_write(m5206_mbar_state
*s
, uint32_t offset
,
314 uint64_t value
, unsigned size
)
316 if (offset
>= 0x100 && offset
< 0x120) {
317 m5206_timer_write(s
->timer
[0], offset
- 0x100, value
);
319 } else if (offset
>= 0x120 && offset
< 0x140) {
320 m5206_timer_write(s
->timer
[1], offset
- 0x120, value
);
322 } else if (offset
>= 0x140 && offset
< 0x160) {
323 mcf_uart_write(s
->uart
[0], offset
- 0x140, value
, size
);
325 } else if (offset
>= 0x180 && offset
< 0x1a0) {
326 mcf_uart_write(s
->uart
[1], offset
- 0x180, value
, size
);
334 s
->icr
[offset
- 0x13] = value
;
335 m5206_mbar_update(s
);
339 m5206_mbar_update(s
);
345 /* TODO: implement watchdog. */
356 case 0x178: case 0x17c: case 0x1c8: case 0x1bc:
357 /* Not implemented: UART Output port bits. */
363 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
368 /* Internal peripherals use a variety of register widths.
369 This lookup table allows a single routine to handle all of them. */
370 static const uint8_t m5206_mbar_width
[] =
372 /* 000-040 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
373 /* 040-080 */ 1, 2, 2, 2, 4, 1, 2, 4, 1, 2, 4, 2, 2, 4, 2, 2,
374 /* 080-0c0 */ 4, 2, 2, 4, 2, 2, 4, 2, 2, 4, 2, 2, 4, 2, 2, 4,
375 /* 0c0-100 */ 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
376 /* 100-140 */ 2, 2, 2, 2, 1, 0, 0, 0, 2, 2, 2, 2, 1, 0, 0, 0,
377 /* 140-180 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
378 /* 180-1c0 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
379 /* 1c0-200 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
382 static uint32_t m5206_mbar_readw(void *opaque
, hwaddr offset
);
383 static uint32_t m5206_mbar_readl(void *opaque
, hwaddr offset
);
385 static uint32_t m5206_mbar_readb(void *opaque
, hwaddr offset
)
387 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
389 if (offset
>= 0x200) {
390 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
392 if (m5206_mbar_width
[offset
>> 2] > 1) {
394 val
= m5206_mbar_readw(opaque
, offset
& ~1);
395 if ((offset
& 1) == 0) {
400 return m5206_mbar_read(s
, offset
, 1);
403 static uint32_t m5206_mbar_readw(void *opaque
, hwaddr offset
)
405 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
408 if (offset
>= 0x200) {
409 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
411 width
= m5206_mbar_width
[offset
>> 2];
414 val
= m5206_mbar_readl(opaque
, offset
& ~3);
415 if ((offset
& 3) == 0)
418 } else if (width
< 2) {
420 val
= m5206_mbar_readb(opaque
, offset
) << 8;
421 val
|= m5206_mbar_readb(opaque
, offset
+ 1);
424 return m5206_mbar_read(s
, offset
, 2);
427 static uint32_t m5206_mbar_readl(void *opaque
, hwaddr offset
)
429 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
432 if (offset
>= 0x200) {
433 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
435 width
= m5206_mbar_width
[offset
>> 2];
438 val
= m5206_mbar_readw(opaque
, offset
) << 16;
439 val
|= m5206_mbar_readw(opaque
, offset
+ 2);
442 return m5206_mbar_read(s
, offset
, 4);
445 static void m5206_mbar_writew(void *opaque
, hwaddr offset
,
447 static void m5206_mbar_writel(void *opaque
, hwaddr offset
,
450 static void m5206_mbar_writeb(void *opaque
, hwaddr offset
,
453 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
456 if (offset
>= 0x200) {
457 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
459 width
= m5206_mbar_width
[offset
>> 2];
462 tmp
= m5206_mbar_readw(opaque
, offset
& ~1);
464 tmp
= (tmp
& 0xff00) | value
;
466 tmp
= (tmp
& 0x00ff) | (value
<< 8);
468 m5206_mbar_writew(opaque
, offset
& ~1, tmp
);
471 m5206_mbar_write(s
, offset
, value
, 1);
474 static void m5206_mbar_writew(void *opaque
, hwaddr offset
,
477 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
480 if (offset
>= 0x200) {
481 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
483 width
= m5206_mbar_width
[offset
>> 2];
486 tmp
= m5206_mbar_readl(opaque
, offset
& ~3);
488 tmp
= (tmp
& 0xffff0000) | value
;
490 tmp
= (tmp
& 0x0000ffff) | (value
<< 16);
492 m5206_mbar_writel(opaque
, offset
& ~3, tmp
);
494 } else if (width
< 2) {
495 m5206_mbar_writeb(opaque
, offset
, value
>> 8);
496 m5206_mbar_writeb(opaque
, offset
+ 1, value
& 0xff);
499 m5206_mbar_write(s
, offset
, value
, 2);
502 static void m5206_mbar_writel(void *opaque
, hwaddr offset
,
505 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
508 if (offset
>= 0x200) {
509 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
511 width
= m5206_mbar_width
[offset
>> 2];
513 m5206_mbar_writew(opaque
, offset
, value
>> 16);
514 m5206_mbar_writew(opaque
, offset
+ 2, value
& 0xffff);
517 m5206_mbar_write(s
, offset
, value
, 4);
520 static uint64_t m5206_mbar_readfn(void *opaque
, hwaddr addr
, unsigned size
)
524 return m5206_mbar_readb(opaque
, addr
);
526 return m5206_mbar_readw(opaque
, addr
);
528 return m5206_mbar_readl(opaque
, addr
);
530 g_assert_not_reached();
534 static void m5206_mbar_writefn(void *opaque
, hwaddr addr
,
535 uint64_t value
, unsigned size
)
539 m5206_mbar_writeb(opaque
, addr
, value
);
542 m5206_mbar_writew(opaque
, addr
, value
);
545 m5206_mbar_writel(opaque
, addr
, value
);
548 g_assert_not_reached();
552 static const MemoryRegionOps m5206_mbar_ops
= {
553 .read
= m5206_mbar_readfn
,
554 .write
= m5206_mbar_writefn
,
555 .valid
.min_access_size
= 1,
556 .valid
.max_access_size
= 4,
557 .endianness
= DEVICE_NATIVE_ENDIAN
,
560 qemu_irq
*mcf5206_init(MemoryRegion
*sysmem
, uint32_t base
, M68kCPU
*cpu
)
565 s
= g_new0(m5206_mbar_state
, 1);
567 memory_region_init_io(&s
->iomem
, NULL
, &m5206_mbar_ops
, s
,
569 memory_region_add_subregion(sysmem
, base
, &s
->iomem
);
571 pic
= qemu_allocate_irqs(m5206_mbar_set_irq
, s
, 14);
572 s
->timer
[0] = m5206_timer_init(pic
[9]);
573 s
->timer
[1] = m5206_timer_init(pic
[10]);
574 s
->uart
[0] = mcf_uart_init(pic
[12], serial_hd(0));
575 s
->uart
[1] = mcf_uart_init(pic
[13], serial_hd(1));