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/error-report.h"
10 #include "qemu-common.h"
13 #include "hw/m68k/mcf.h"
14 #include "qemu/timer.h"
15 #include "hw/ptimer.h"
16 #include "sysemu/sysemu.h"
17 #include "exec/address-spaces.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_stop(s
->timer
);
61 if ((s
->tmr
& TMR_RST
) == 0)
64 prescale
= (s
->tmr
>> 8) + 1;
65 mode
= (s
->tmr
>> 1) & 3;
69 if (mode
== 3 || mode
== 0)
70 hw_error("m5206_timer: mode %d not implemented\n", mode
);
71 if ((s
->tmr
& TMR_FRR
) == 0)
72 hw_error("m5206_timer: free running mode not implemented\n");
74 /* Assume 66MHz system clock. */
75 ptimer_set_freq(s
->timer
, 66000000 / prescale
);
77 ptimer_set_limit(s
->timer
, s
->trr
, 0);
79 ptimer_run(s
->timer
, 0);
82 static void m5206_timer_trigger(void *opaque
)
84 m5206_timer_state
*s
= (m5206_timer_state
*)opaque
;
86 m5206_timer_update(s
);
89 static uint32_t m5206_timer_read(m5206_timer_state
*s
, uint32_t addr
)
99 return s
->trr
- ptimer_get_count(s
->timer
);
107 static void m5206_timer_write(m5206_timer_state
*s
, uint32_t addr
, uint32_t val
)
111 if ((s
->tmr
& TMR_RST
) != 0 && (val
& TMR_RST
) == 0) {
112 m5206_timer_reset(s
);
115 m5206_timer_recalibrate(s
);
119 m5206_timer_recalibrate(s
);
125 ptimer_set_count(s
->timer
, val
);
133 m5206_timer_update(s
);
136 static m5206_timer_state
*m5206_timer_init(qemu_irq irq
)
138 m5206_timer_state
*s
;
141 s
= g_new0(m5206_timer_state
, 1);
142 bh
= qemu_bh_new(m5206_timer_trigger
, s
);
143 s
->timer
= ptimer_init(bh
, PTIMER_POLICY_DEFAULT
);
145 m5206_timer_reset(s
);
149 /* System Integration Module. */
154 m5206_timer_state
*timer
[2];
158 uint16_t imr
; /* 1 == interrupt is masked. */
163 /* Include the UART vector registers here. */
167 /* Interrupt controller. */
169 static int m5206_find_pending_irq(m5206_mbar_state
*s
)
178 active
= s
->ipr
& ~s
->imr
;
182 for (i
= 1; i
< 14; i
++) {
183 if (active
& (1 << i
)) {
184 if ((s
->icr
[i
] & 0x1f) > level
) {
185 level
= s
->icr
[i
] & 0x1f;
197 static void m5206_mbar_update(m5206_mbar_state
*s
)
203 irq
= m5206_find_pending_irq(s
);
207 level
= (tmp
>> 2) & 7;
223 /* Unknown vector. */
224 error_report("Unhandled vector for IRQ %d", irq
);
233 m68k_set_irq_level(s
->cpu
, level
, vector
);
236 static void m5206_mbar_set_irq(void *opaque
, int irq
, int level
)
238 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
242 s
->ipr
&= ~(1 << irq
);
244 m5206_mbar_update(s
);
247 /* System Integration Module. */
249 static void m5206_mbar_reset(m5206_mbar_state
*s
)
271 static uint64_t m5206_mbar_read(m5206_mbar_state
*s
,
272 uint64_t offset
, unsigned size
)
274 if (offset
>= 0x100 && offset
< 0x120) {
275 return m5206_timer_read(s
->timer
[0], offset
- 0x100);
276 } else if (offset
>= 0x120 && offset
< 0x140) {
277 return m5206_timer_read(s
->timer
[1], offset
- 0x120);
278 } else if (offset
>= 0x140 && offset
< 0x160) {
279 return mcf_uart_read(s
->uart
[0], offset
- 0x140, size
);
280 } else if (offset
>= 0x180 && offset
< 0x1a0) {
281 return mcf_uart_read(s
->uart
[1], offset
- 0x180, size
);
284 case 0x03: return s
->scr
;
285 case 0x14 ... 0x20: return s
->icr
[offset
- 0x13];
286 case 0x36: return s
->imr
;
287 case 0x3a: return s
->ipr
;
288 case 0x40: return s
->rsr
;
290 case 0x42: return s
->swivr
;
292 /* DRAM mask register. */
293 /* FIXME: currently hardcoded to 128Mb. */
296 while (mask
> ram_size
)
298 return mask
& 0x0ffe0000;
300 case 0x5c: return 1; /* DRAM bank 1 empty. */
301 case 0xcb: return s
->par
;
302 case 0x170: return s
->uivr
[0];
303 case 0x1b0: return s
->uivr
[1];
305 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
309 static void m5206_mbar_write(m5206_mbar_state
*s
, uint32_t offset
,
310 uint64_t value
, unsigned size
)
312 if (offset
>= 0x100 && offset
< 0x120) {
313 m5206_timer_write(s
->timer
[0], offset
- 0x100, value
);
315 } else if (offset
>= 0x120 && offset
< 0x140) {
316 m5206_timer_write(s
->timer
[1], offset
- 0x120, value
);
318 } else if (offset
>= 0x140 && offset
< 0x160) {
319 mcf_uart_write(s
->uart
[0], offset
- 0x140, value
, size
);
321 } else if (offset
>= 0x180 && offset
< 0x1a0) {
322 mcf_uart_write(s
->uart
[1], offset
- 0x180, value
, size
);
330 s
->icr
[offset
- 0x13] = value
;
331 m5206_mbar_update(s
);
335 m5206_mbar_update(s
);
341 /* TODO: implement watchdog. */
352 case 0x178: case 0x17c: case 0x1c8: case 0x1bc:
353 /* Not implemented: UART Output port bits. */
359 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
364 /* Internal peripherals use a variety of register widths.
365 This lookup table allows a single routine to handle all of them. */
366 static const uint8_t m5206_mbar_width
[] =
368 /* 000-040 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
369 /* 040-080 */ 1, 2, 2, 2, 4, 1, 2, 4, 1, 2, 4, 2, 2, 4, 2, 2,
370 /* 080-0c0 */ 4, 2, 2, 4, 2, 2, 4, 2, 2, 4, 2, 2, 4, 2, 2, 4,
371 /* 0c0-100 */ 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
372 /* 100-140 */ 2, 2, 2, 2, 1, 0, 0, 0, 2, 2, 2, 2, 1, 0, 0, 0,
373 /* 140-180 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
374 /* 180-1c0 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
375 /* 1c0-200 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
378 static uint32_t m5206_mbar_readw(void *opaque
, hwaddr offset
);
379 static uint32_t m5206_mbar_readl(void *opaque
, hwaddr offset
);
381 static uint32_t m5206_mbar_readb(void *opaque
, hwaddr offset
)
383 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
385 if (offset
>= 0x200) {
386 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
388 if (m5206_mbar_width
[offset
>> 2] > 1) {
390 val
= m5206_mbar_readw(opaque
, offset
& ~1);
391 if ((offset
& 1) == 0) {
396 return m5206_mbar_read(s
, offset
, 1);
399 static uint32_t m5206_mbar_readw(void *opaque
, hwaddr offset
)
401 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
404 if (offset
>= 0x200) {
405 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
407 width
= m5206_mbar_width
[offset
>> 2];
410 val
= m5206_mbar_readl(opaque
, offset
& ~3);
411 if ((offset
& 3) == 0)
414 } else if (width
< 2) {
416 val
= m5206_mbar_readb(opaque
, offset
) << 8;
417 val
|= m5206_mbar_readb(opaque
, offset
+ 1);
420 return m5206_mbar_read(s
, offset
, 2);
423 static uint32_t m5206_mbar_readl(void *opaque
, hwaddr offset
)
425 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
428 if (offset
>= 0x200) {
429 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
431 width
= m5206_mbar_width
[offset
>> 2];
434 val
= m5206_mbar_readw(opaque
, offset
) << 16;
435 val
|= m5206_mbar_readw(opaque
, offset
+ 2);
438 return m5206_mbar_read(s
, offset
, 4);
441 static void m5206_mbar_writew(void *opaque
, hwaddr offset
,
443 static void m5206_mbar_writel(void *opaque
, hwaddr offset
,
446 static void m5206_mbar_writeb(void *opaque
, hwaddr offset
,
449 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
452 if (offset
>= 0x200) {
453 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
455 width
= m5206_mbar_width
[offset
>> 2];
458 tmp
= m5206_mbar_readw(opaque
, offset
& ~1);
460 tmp
= (tmp
& 0xff00) | value
;
462 tmp
= (tmp
& 0x00ff) | (value
<< 8);
464 m5206_mbar_writew(opaque
, offset
& ~1, tmp
);
467 m5206_mbar_write(s
, offset
, value
, 1);
470 static void m5206_mbar_writew(void *opaque
, hwaddr offset
,
473 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
476 if (offset
>= 0x200) {
477 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
479 width
= m5206_mbar_width
[offset
>> 2];
482 tmp
= m5206_mbar_readl(opaque
, offset
& ~3);
484 tmp
= (tmp
& 0xffff0000) | value
;
486 tmp
= (tmp
& 0x0000ffff) | (value
<< 16);
488 m5206_mbar_writel(opaque
, offset
& ~3, tmp
);
490 } else if (width
< 2) {
491 m5206_mbar_writeb(opaque
, offset
, value
>> 8);
492 m5206_mbar_writeb(opaque
, offset
+ 1, value
& 0xff);
495 m5206_mbar_write(s
, offset
, value
, 2);
498 static void m5206_mbar_writel(void *opaque
, hwaddr offset
,
501 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
504 if (offset
>= 0x200) {
505 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
507 width
= m5206_mbar_width
[offset
>> 2];
509 m5206_mbar_writew(opaque
, offset
, value
>> 16);
510 m5206_mbar_writew(opaque
, offset
+ 2, value
& 0xffff);
513 m5206_mbar_write(s
, offset
, value
, 4);
516 static const MemoryRegionOps m5206_mbar_ops
= {
529 .endianness
= DEVICE_NATIVE_ENDIAN
,
532 qemu_irq
*mcf5206_init(MemoryRegion
*sysmem
, uint32_t base
, M68kCPU
*cpu
)
537 s
= g_new0(m5206_mbar_state
, 1);
539 memory_region_init_io(&s
->iomem
, NULL
, &m5206_mbar_ops
, s
,
541 memory_region_add_subregion(sysmem
, base
, &s
->iomem
);
543 pic
= qemu_allocate_irqs(m5206_mbar_set_irq
, s
, 14);
544 s
->timer
[0] = m5206_timer_init(pic
[9]);
545 s
->timer
[1] = m5206_timer_init(pic
[10]);
546 s
->uart
[0] = mcf_uart_init(pic
[12], serial_hd(0));
547 s
->uart
[1] = mcf_uart_init(pic
[13], serial_hd(1));