2 * Motorola ColdFire MCF5206 SoC embedded peripheral emulation.
4 * Copyright (c) 2007 CodeSourcery.
6 * This code is licensed under the GPL
10 #include "qemu/timer.h"
12 #include "sysemu/sysemu.h"
13 #include "exec/address-spaces.h"
15 /* General purpose timer module. */
36 static void m5206_timer_update(m5206_timer_state
*s
)
38 if ((s
->tmr
& TMR_ORI
) != 0 && (s
->ter
& TER_REF
))
39 qemu_irq_raise(s
->irq
);
41 qemu_irq_lower(s
->irq
);
44 static void m5206_timer_reset(m5206_timer_state
*s
)
50 static void m5206_timer_recalibrate(m5206_timer_state
*s
)
55 ptimer_stop(s
->timer
);
57 if ((s
->tmr
& TMR_RST
) == 0)
60 prescale
= (s
->tmr
>> 8) + 1;
61 mode
= (s
->tmr
>> 1) & 3;
65 if (mode
== 3 || mode
== 0)
66 hw_error("m5206_timer: mode %d not implemented\n", mode
);
67 if ((s
->tmr
& TMR_FRR
) == 0)
68 hw_error("m5206_timer: free running mode not implemented\n");
70 /* Assume 66MHz system clock. */
71 ptimer_set_freq(s
->timer
, 66000000 / prescale
);
73 ptimer_set_limit(s
->timer
, s
->trr
, 0);
75 ptimer_run(s
->timer
, 0);
78 static void m5206_timer_trigger(void *opaque
)
80 m5206_timer_state
*s
= (m5206_timer_state
*)opaque
;
82 m5206_timer_update(s
);
85 static uint32_t m5206_timer_read(m5206_timer_state
*s
, uint32_t addr
)
95 return s
->trr
- ptimer_get_count(s
->timer
);
103 static void m5206_timer_write(m5206_timer_state
*s
, uint32_t addr
, uint32_t val
)
107 if ((s
->tmr
& TMR_RST
) != 0 && (val
& TMR_RST
) == 0) {
108 m5206_timer_reset(s
);
111 m5206_timer_recalibrate(s
);
115 m5206_timer_recalibrate(s
);
121 ptimer_set_count(s
->timer
, val
);
129 m5206_timer_update(s
);
132 static m5206_timer_state
*m5206_timer_init(qemu_irq irq
)
134 m5206_timer_state
*s
;
137 s
= (m5206_timer_state
*)g_malloc0(sizeof(m5206_timer_state
));
138 bh
= qemu_bh_new(m5206_timer_trigger
, s
);
139 s
->timer
= ptimer_init(bh
);
141 m5206_timer_reset(s
);
145 /* System Integration Module. */
150 m5206_timer_state
*timer
[2];
154 uint16_t imr
; /* 1 == interrupt is masked. */
159 /* Include the UART vector registers here. */
163 /* Interrupt controller. */
165 static int m5206_find_pending_irq(m5206_mbar_state
*s
)
174 active
= s
->ipr
& ~s
->imr
;
178 for (i
= 1; i
< 14; i
++) {
179 if (active
& (1 << i
)) {
180 if ((s
->icr
[i
] & 0x1f) > level
) {
181 level
= s
->icr
[i
] & 0x1f;
193 static void m5206_mbar_update(m5206_mbar_state
*s
)
199 irq
= m5206_find_pending_irq(s
);
203 level
= (tmp
>> 2) & 7;
219 /* Unknown vector. */
220 fprintf(stderr
, "Unhandled vector for IRQ %d\n", irq
);
229 m68k_set_irq_level(s
->cpu
, level
, vector
);
232 static void m5206_mbar_set_irq(void *opaque
, int irq
, int level
)
234 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
238 s
->ipr
&= ~(1 << irq
);
240 m5206_mbar_update(s
);
243 /* System Integration Module. */
245 static void m5206_mbar_reset(m5206_mbar_state
*s
)
267 static uint64_t m5206_mbar_read(m5206_mbar_state
*s
,
268 uint64_t offset
, unsigned size
)
270 if (offset
>= 0x100 && offset
< 0x120) {
271 return m5206_timer_read(s
->timer
[0], offset
- 0x100);
272 } else if (offset
>= 0x120 && offset
< 0x140) {
273 return m5206_timer_read(s
->timer
[1], offset
- 0x120);
274 } else if (offset
>= 0x140 && offset
< 0x160) {
275 return mcf_uart_read(s
->uart
[0], offset
- 0x140, size
);
276 } else if (offset
>= 0x180 && offset
< 0x1a0) {
277 return mcf_uart_read(s
->uart
[1], offset
- 0x180, size
);
280 case 0x03: return s
->scr
;
281 case 0x14 ... 0x20: return s
->icr
[offset
- 0x13];
282 case 0x36: return s
->imr
;
283 case 0x3a: return s
->ipr
;
284 case 0x40: return s
->rsr
;
286 case 0x42: return s
->swivr
;
288 /* DRAM mask register. */
289 /* FIXME: currently hardcoded to 128Mb. */
292 while (mask
> ram_size
)
294 return mask
& 0x0ffe0000;
296 case 0x5c: return 1; /* DRAM bank 1 empty. */
297 case 0xcb: return s
->par
;
298 case 0x170: return s
->uivr
[0];
299 case 0x1b0: return s
->uivr
[1];
301 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
305 static void m5206_mbar_write(m5206_mbar_state
*s
, uint32_t offset
,
306 uint64_t value
, unsigned size
)
308 if (offset
>= 0x100 && offset
< 0x120) {
309 m5206_timer_write(s
->timer
[0], offset
- 0x100, value
);
311 } else if (offset
>= 0x120 && offset
< 0x140) {
312 m5206_timer_write(s
->timer
[1], offset
- 0x120, value
);
314 } else if (offset
>= 0x140 && offset
< 0x160) {
315 mcf_uart_write(s
->uart
[0], offset
- 0x140, value
, size
);
317 } else if (offset
>= 0x180 && offset
< 0x1a0) {
318 mcf_uart_write(s
->uart
[1], offset
- 0x180, value
, size
);
326 s
->icr
[offset
- 0x13] = value
;
327 m5206_mbar_update(s
);
331 m5206_mbar_update(s
);
337 /* TODO: implement watchdog. */
348 case 0x178: case 0x17c: case 0x1c8: case 0x1bc:
349 /* Not implemented: UART Output port bits. */
355 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
360 /* Internal peripherals use a variety of register widths.
361 This lookup table allows a single routine to handle all of them. */
362 static const uint8_t m5206_mbar_width
[] =
364 /* 000-040 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
365 /* 040-080 */ 1, 2, 2, 2, 4, 1, 2, 4, 1, 2, 4, 2, 2, 4, 2, 2,
366 /* 080-0c0 */ 4, 2, 2, 4, 2, 2, 4, 2, 2, 4, 2, 2, 4, 2, 2, 4,
367 /* 0c0-100 */ 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
368 /* 100-140 */ 2, 2, 2, 2, 1, 0, 0, 0, 2, 2, 2, 2, 1, 0, 0, 0,
369 /* 140-180 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
370 /* 180-1c0 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
371 /* 1c0-200 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
374 static uint32_t m5206_mbar_readw(void *opaque
, hwaddr offset
);
375 static uint32_t m5206_mbar_readl(void *opaque
, hwaddr offset
);
377 static uint32_t m5206_mbar_readb(void *opaque
, hwaddr offset
)
379 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
381 if (offset
>= 0x200) {
382 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
384 if (m5206_mbar_width
[offset
>> 2] > 1) {
386 val
= m5206_mbar_readw(opaque
, offset
& ~1);
387 if ((offset
& 1) == 0) {
392 return m5206_mbar_read(s
, offset
, 1);
395 static uint32_t m5206_mbar_readw(void *opaque
, hwaddr offset
)
397 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
400 if (offset
>= 0x200) {
401 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
403 width
= m5206_mbar_width
[offset
>> 2];
406 val
= m5206_mbar_readl(opaque
, offset
& ~3);
407 if ((offset
& 3) == 0)
410 } else if (width
< 2) {
412 val
= m5206_mbar_readb(opaque
, offset
) << 8;
413 val
|= m5206_mbar_readb(opaque
, offset
+ 1);
416 return m5206_mbar_read(s
, offset
, 2);
419 static uint32_t m5206_mbar_readl(void *opaque
, hwaddr offset
)
421 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
424 if (offset
>= 0x200) {
425 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
427 width
= m5206_mbar_width
[offset
>> 2];
430 val
= m5206_mbar_readw(opaque
, offset
) << 16;
431 val
|= m5206_mbar_readw(opaque
, offset
+ 2);
434 return m5206_mbar_read(s
, offset
, 4);
437 static void m5206_mbar_writew(void *opaque
, hwaddr offset
,
439 static void m5206_mbar_writel(void *opaque
, hwaddr offset
,
442 static void m5206_mbar_writeb(void *opaque
, hwaddr offset
,
445 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
448 if (offset
>= 0x200) {
449 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
451 width
= m5206_mbar_width
[offset
>> 2];
454 tmp
= m5206_mbar_readw(opaque
, offset
& ~1);
456 tmp
= (tmp
& 0xff00) | value
;
458 tmp
= (tmp
& 0x00ff) | (value
<< 8);
460 m5206_mbar_writew(opaque
, offset
& ~1, tmp
);
463 m5206_mbar_write(s
, offset
, value
, 1);
466 static void m5206_mbar_writew(void *opaque
, hwaddr offset
,
469 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
472 if (offset
>= 0x200) {
473 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
475 width
= m5206_mbar_width
[offset
>> 2];
478 tmp
= m5206_mbar_readl(opaque
, offset
& ~3);
480 tmp
= (tmp
& 0xffff0000) | value
;
482 tmp
= (tmp
& 0x0000ffff) | (value
<< 16);
484 m5206_mbar_writel(opaque
, offset
& ~3, tmp
);
486 } else if (width
< 2) {
487 m5206_mbar_writeb(opaque
, offset
, value
>> 8);
488 m5206_mbar_writeb(opaque
, offset
+ 1, value
& 0xff);
491 m5206_mbar_write(s
, offset
, value
, 2);
494 static void m5206_mbar_writel(void *opaque
, hwaddr offset
,
497 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
500 if (offset
>= 0x200) {
501 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
503 width
= m5206_mbar_width
[offset
>> 2];
505 m5206_mbar_writew(opaque
, offset
, value
>> 16);
506 m5206_mbar_writew(opaque
, offset
+ 2, value
& 0xffff);
509 m5206_mbar_write(s
, offset
, value
, 4);
512 static const MemoryRegionOps m5206_mbar_ops
= {
525 .endianness
= DEVICE_NATIVE_ENDIAN
,
528 qemu_irq
*mcf5206_init(MemoryRegion
*sysmem
, uint32_t base
, M68kCPU
*cpu
)
533 s
= (m5206_mbar_state
*)g_malloc0(sizeof(m5206_mbar_state
));
535 memory_region_init_io(&s
->iomem
, &m5206_mbar_ops
, s
,
537 memory_region_add_subregion(sysmem
, base
, &s
->iomem
);
539 pic
= qemu_allocate_irqs(m5206_mbar_set_irq
, s
, 14);
540 s
->timer
[0] = m5206_timer_init(pic
[9]);
541 s
->timer
[1] = m5206_timer_init(pic
[10]);
542 s
->uart
[0] = mcf_uart_init(pic
[12], serial_hds
[0]);
543 s
->uart
[1] = mcf_uart_init(pic
[13], serial_hds
[1]);