2 * Motorola ColdFire MCF5206 SoC embedded peripheral emulation.
4 * Copyright (c) 2007 CodeSourcery.
6 * This code is licenced under the GPL
10 #include "qemu-timer.h"
13 /* General purpose timer module. */
34 static void m5206_timer_update(m5206_timer_state
*s
)
36 if ((s
->tmr
& TMR_ORI
) != 0 && (s
->ter
& TER_REF
))
37 qemu_irq_raise(s
->irq
);
39 qemu_irq_lower(s
->irq
);
42 static void m5206_timer_reset(m5206_timer_state
*s
)
48 static void m5206_timer_recalibrate(m5206_timer_state
*s
)
53 ptimer_stop(s
->timer
);
55 if ((s
->tmr
& TMR_RST
) == 0)
58 prescale
= (s
->tmr
>> 8) + 1;
59 mode
= (s
->tmr
>> 1) & 3;
63 if (mode
== 3 || mode
== 0)
64 hw_error("m5206_timer: mode %d not implemented\n", mode
);
65 if ((s
->tmr
& TMR_FRR
) == 0)
66 hw_error("m5206_timer: free running mode not implemented\n");
68 /* Assume 66MHz system clock. */
69 ptimer_set_freq(s
->timer
, 66000000 / prescale
);
71 ptimer_set_limit(s
->timer
, s
->trr
, 0);
73 ptimer_run(s
->timer
, 0);
76 static void m5206_timer_trigger(void *opaque
)
78 m5206_timer_state
*s
= (m5206_timer_state
*)opaque
;
80 m5206_timer_update(s
);
83 static uint32_t m5206_timer_read(m5206_timer_state
*s
, uint32_t addr
)
93 return s
->trr
- ptimer_get_count(s
->timer
);
101 static void m5206_timer_write(m5206_timer_state
*s
, uint32_t addr
, uint32_t val
)
105 if ((s
->tmr
& TMR_RST
) != 0 && (val
& TMR_RST
) == 0) {
106 m5206_timer_reset(s
);
109 m5206_timer_recalibrate(s
);
113 m5206_timer_recalibrate(s
);
119 ptimer_set_count(s
->timer
, val
);
127 m5206_timer_update(s
);
130 static m5206_timer_state
*m5206_timer_init(qemu_irq irq
)
132 m5206_timer_state
*s
;
135 s
= (m5206_timer_state
*)qemu_mallocz(sizeof(m5206_timer_state
));
136 bh
= qemu_bh_new(m5206_timer_trigger
, s
);
137 s
->timer
= ptimer_init(bh
);
139 m5206_timer_reset(s
);
143 /* System Integration Module. */
147 m5206_timer_state
*timer
[2];
151 uint16_t imr
; /* 1 == interrupt is masked. */
156 /* Include the UART vector registers here. */
160 /* Interrupt controller. */
162 static int m5206_find_pending_irq(m5206_mbar_state
*s
)
171 active
= s
->ipr
& ~s
->imr
;
175 for (i
= 1; i
< 14; i
++) {
176 if (active
& (1 << i
)) {
177 if ((s
->icr
[i
] & 0x1f) > level
) {
178 level
= s
->icr
[i
] & 0x1f;
190 static void m5206_mbar_update(m5206_mbar_state
*s
)
196 irq
= m5206_find_pending_irq(s
);
200 level
= (tmp
>> 2) & 7;
216 /* Unknown vector. */
217 fprintf(stderr
, "Unhandled vector for IRQ %d\n", irq
);
226 m68k_set_irq_level(s
->env
, level
, vector
);
229 static void m5206_mbar_set_irq(void *opaque
, int irq
, int level
)
231 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
235 s
->ipr
&= ~(1 << irq
);
237 m5206_mbar_update(s
);
240 /* System Integration Module. */
242 static void m5206_mbar_reset(m5206_mbar_state
*s
)
264 static uint32_t m5206_mbar_read(m5206_mbar_state
*s
, uint32_t offset
)
266 if (offset
>= 0x100 && offset
< 0x120) {
267 return m5206_timer_read(s
->timer
[0], offset
- 0x100);
268 } else if (offset
>= 0x120 && offset
< 0x140) {
269 return m5206_timer_read(s
->timer
[1], offset
- 0x120);
270 } else if (offset
>= 0x140 && offset
< 0x160) {
271 return mcf_uart_read(s
->uart
[0], offset
- 0x140);
272 } else if (offset
>= 0x180 && offset
< 0x1a0) {
273 return mcf_uart_read(s
->uart
[1], offset
- 0x180);
276 case 0x03: return s
->scr
;
277 case 0x14 ... 0x20: return s
->icr
[offset
- 0x13];
278 case 0x36: return s
->imr
;
279 case 0x3a: return s
->ipr
;
280 case 0x40: return s
->rsr
;
282 case 0x42: return s
->swivr
;
284 /* DRAM mask register. */
285 /* FIXME: currently hardcoded to 128Mb. */
288 while (mask
> ram_size
)
290 return mask
& 0x0ffe0000;
292 case 0x5c: return 1; /* DRAM bank 1 empty. */
293 case 0xcb: return s
->par
;
294 case 0x170: return s
->uivr
[0];
295 case 0x1b0: return s
->uivr
[1];
297 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
301 static void m5206_mbar_write(m5206_mbar_state
*s
, uint32_t offset
,
304 if (offset
>= 0x100 && offset
< 0x120) {
305 m5206_timer_write(s
->timer
[0], offset
- 0x100, value
);
307 } else if (offset
>= 0x120 && offset
< 0x140) {
308 m5206_timer_write(s
->timer
[1], offset
- 0x120, value
);
310 } else if (offset
>= 0x140 && offset
< 0x160) {
311 mcf_uart_write(s
->uart
[0], offset
- 0x140, value
);
313 } else if (offset
>= 0x180 && offset
< 0x1a0) {
314 mcf_uart_write(s
->uart
[1], offset
- 0x180, value
);
322 s
->icr
[offset
- 0x13] = value
;
323 m5206_mbar_update(s
);
327 m5206_mbar_update(s
);
333 /* TODO: implement watchdog. */
344 case 0x178: case 0x17c: case 0x1c8: case 0x1bc:
345 /* Not implemented: UART Output port bits. */
351 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
356 /* Internal peripherals use a variety of register widths.
357 This lookup table allows a single routine to handle all of them. */
358 static const int m5206_mbar_width
[] =
360 /* 000-040 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
361 /* 040-080 */ 1, 2, 2, 2, 4, 1, 2, 4, 1, 2, 4, 2, 2, 4, 2, 2,
362 /* 080-0c0 */ 4, 2, 2, 4, 2, 2, 4, 2, 2, 4, 2, 2, 4, 2, 2, 4,
363 /* 0c0-100 */ 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
364 /* 100-140 */ 2, 2, 2, 2, 1, 0, 0, 0, 2, 2, 2, 2, 1, 0, 0, 0,
365 /* 140-180 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
366 /* 180-1c0 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
367 /* 1c0-200 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
370 static uint32_t m5206_mbar_readw(void *opaque
, target_phys_addr_t offset
);
371 static uint32_t m5206_mbar_readl(void *opaque
, target_phys_addr_t offset
);
373 static uint32_t m5206_mbar_readb(void *opaque
, target_phys_addr_t offset
)
375 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
377 if (offset
> 0x200) {
378 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
380 if (m5206_mbar_width
[offset
>> 2] > 1) {
382 val
= m5206_mbar_readw(opaque
, offset
& ~1);
383 if ((offset
& 1) == 0) {
388 return m5206_mbar_read(s
, offset
);
391 static uint32_t m5206_mbar_readw(void *opaque
, target_phys_addr_t offset
)
393 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
396 if (offset
> 0x200) {
397 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
399 width
= m5206_mbar_width
[offset
>> 2];
402 val
= m5206_mbar_readl(opaque
, offset
& ~3);
403 if ((offset
& 3) == 0)
406 } else if (width
< 2) {
408 val
= m5206_mbar_readb(opaque
, offset
) << 8;
409 val
|= m5206_mbar_readb(opaque
, offset
+ 1);
412 return m5206_mbar_read(s
, offset
);
415 static uint32_t m5206_mbar_readl(void *opaque
, target_phys_addr_t offset
)
417 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
420 if (offset
> 0x200) {
421 hw_error("Bad MBAR read offset 0x%x", (int)offset
);
423 width
= m5206_mbar_width
[offset
>> 2];
426 val
= m5206_mbar_readw(opaque
, offset
) << 16;
427 val
|= m5206_mbar_readw(opaque
, offset
+ 2);
430 return m5206_mbar_read(s
, offset
);
433 static void m5206_mbar_writew(void *opaque
, target_phys_addr_t offset
,
435 static void m5206_mbar_writel(void *opaque
, target_phys_addr_t offset
,
438 static void m5206_mbar_writeb(void *opaque
, target_phys_addr_t offset
,
441 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
444 if (offset
> 0x200) {
445 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
447 width
= m5206_mbar_width
[offset
>> 2];
450 tmp
= m5206_mbar_readw(opaque
, offset
& ~1);
452 tmp
= (tmp
& 0xff00) | value
;
454 tmp
= (tmp
& 0x00ff) | (value
<< 8);
456 m5206_mbar_writew(opaque
, offset
& ~1, tmp
);
459 m5206_mbar_write(s
, offset
, value
);
462 static void m5206_mbar_writew(void *opaque
, target_phys_addr_t offset
,
465 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
468 if (offset
> 0x200) {
469 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
471 width
= m5206_mbar_width
[offset
>> 2];
474 tmp
= m5206_mbar_readl(opaque
, offset
& ~3);
476 tmp
= (tmp
& 0xffff0000) | value
;
478 tmp
= (tmp
& 0x0000ffff) | (value
<< 16);
480 m5206_mbar_writel(opaque
, offset
& ~3, tmp
);
482 } else if (width
< 2) {
483 m5206_mbar_writeb(opaque
, offset
, value
>> 8);
484 m5206_mbar_writeb(opaque
, offset
+ 1, value
& 0xff);
487 m5206_mbar_write(s
, offset
, value
);
490 static void m5206_mbar_writel(void *opaque
, target_phys_addr_t offset
,
493 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
496 if (offset
> 0x200) {
497 hw_error("Bad MBAR write offset 0x%x", (int)offset
);
499 width
= m5206_mbar_width
[offset
>> 2];
501 m5206_mbar_writew(opaque
, offset
, value
>> 16);
502 m5206_mbar_writew(opaque
, offset
+ 2, value
& 0xffff);
505 m5206_mbar_write(s
, offset
, value
);
508 static CPUReadMemoryFunc
*m5206_mbar_readfn
[] = {
514 static CPUWriteMemoryFunc
*m5206_mbar_writefn
[] = {
520 qemu_irq
*mcf5206_init(uint32_t base
, CPUState
*env
)
526 s
= (m5206_mbar_state
*)qemu_mallocz(sizeof(m5206_mbar_state
));
527 iomemtype
= cpu_register_io_memory(0, m5206_mbar_readfn
,
528 m5206_mbar_writefn
, s
);
529 cpu_register_physical_memory(base
, 0x00001000, iomemtype
);
531 pic
= qemu_allocate_irqs(m5206_mbar_set_irq
, s
, 14);
532 s
->timer
[0] = m5206_timer_init(pic
[9]);
533 s
->timer
[1] = m5206_timer_init(pic
[10]);
534 s
->uart
[0] = mcf_uart_init(pic
[12], serial_hds
[0]);
535 s
->uart
[1] = mcf_uart_init(pic
[13], serial_hds
[1]);