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 cpu_abort(cpu_single_env
,
65 "m5206_timer: mode %d not implemented\n", mode
);
66 if ((s
->tmr
& TMR_FRR
) == 0)
67 cpu_abort(cpu_single_env
,
68 "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
*)qemu_mallocz(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. */
149 m5206_timer_state
*timer
[2];
153 uint16_t imr
; /* 1 == interrupt is masked. */
158 /* Include the UART vector registers here. */
162 /* Interrupt controller. */
164 static int m5206_find_pending_irq(m5206_mbar_state
*s
)
173 active
= s
->ipr
& ~s
->imr
;
177 for (i
= 1; i
< 14; i
++) {
178 if (active
& (1 << i
)) {
179 if ((s
->icr
[i
] & 0x1f) > level
) {
180 level
= s
->icr
[i
] & 0x1f;
192 static void m5206_mbar_update(m5206_mbar_state
*s
)
198 irq
= m5206_find_pending_irq(s
);
202 level
= (tmp
>> 2) & 7;
218 /* Unknown vector. */
219 fprintf(stderr
, "Unhandled vector for IRQ %d\n", irq
);
228 m68k_set_irq_level(s
->env
, level
, vector
);
231 static void m5206_mbar_set_irq(void *opaque
, int irq
, int level
)
233 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
237 s
->ipr
&= ~(1 << irq
);
239 m5206_mbar_update(s
);
242 /* System Integration Module. */
244 static void m5206_mbar_reset(m5206_mbar_state
*s
)
266 static uint32_t m5206_mbar_read(m5206_mbar_state
*s
, uint32_t offset
)
268 if (offset
>= 0x100 && offset
< 0x120) {
269 return m5206_timer_read(s
->timer
[0], offset
- 0x100);
270 } else if (offset
>= 0x120 && offset
< 0x140) {
271 return m5206_timer_read(s
->timer
[1], offset
- 0x120);
272 } else if (offset
>= 0x140 && offset
< 0x160) {
273 return mcf_uart_read(s
->uart
[0], offset
- 0x140);
274 } else if (offset
>= 0x180 && offset
< 0x1a0) {
275 return mcf_uart_read(s
->uart
[1], offset
- 0x180);
278 case 0x03: return s
->scr
;
279 case 0x14 ... 0x20: return s
->icr
[offset
- 0x13];
280 case 0x36: return s
->imr
;
281 case 0x3a: return s
->ipr
;
282 case 0x40: return s
->rsr
;
284 case 0x42: return s
->swivr
;
286 /* DRAM mask register. */
287 /* FIXME: currently hardcoded to 128Mb. */
290 while (mask
> ram_size
)
292 return mask
& 0x0ffe0000;
294 case 0x5c: return 1; /* DRAM bank 1 empty. */
295 case 0xcb: return s
->par
;
296 case 0x170: return s
->uivr
[0];
297 case 0x1b0: return s
->uivr
[1];
299 cpu_abort(cpu_single_env
, "Bad MBAR read offset 0x%x", (int)offset
);
303 static void m5206_mbar_write(m5206_mbar_state
*s
, uint32_t offset
,
306 if (offset
>= 0x100 && offset
< 0x120) {
307 m5206_timer_write(s
->timer
[0], offset
- 0x100, value
);
309 } else if (offset
>= 0x120 && offset
< 0x140) {
310 m5206_timer_write(s
->timer
[1], offset
- 0x120, value
);
312 } else if (offset
>= 0x140 && offset
< 0x160) {
313 mcf_uart_write(s
->uart
[0], offset
- 0x140, value
);
315 } else if (offset
>= 0x180 && offset
< 0x1a0) {
316 mcf_uart_write(s
->uart
[1], offset
- 0x180, value
);
324 s
->icr
[offset
- 0x13] = value
;
325 m5206_mbar_update(s
);
329 m5206_mbar_update(s
);
335 /* TODO: implement watchdog. */
346 case 0x178: case 0x17c: case 0x1c8: case 0x1bc:
347 /* Not implemented: UART Output port bits. */
353 cpu_abort(cpu_single_env
, "Bad MBAR write offset 0x%x", (int)offset
);
358 /* Internal peripherals use a variety of register widths.
359 This lookup table allows a single routine to handle all of them. */
360 static const int m5206_mbar_width
[] =
362 /* 000-040 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
363 /* 040-080 */ 1, 2, 2, 2, 4, 1, 2, 4, 1, 2, 4, 2, 2, 4, 2, 2,
364 /* 080-0c0 */ 4, 2, 2, 4, 2, 2, 4, 2, 2, 4, 2, 2, 4, 2, 2, 4,
365 /* 0c0-100 */ 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
366 /* 100-140 */ 2, 2, 2, 2, 1, 0, 0, 0, 2, 2, 2, 2, 1, 0, 0, 0,
367 /* 140-180 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
368 /* 180-1c0 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
369 /* 1c0-200 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
372 static uint32_t m5206_mbar_readw(void *opaque
, target_phys_addr_t offset
);
373 static uint32_t m5206_mbar_readl(void *opaque
, target_phys_addr_t offset
);
375 static uint32_t m5206_mbar_readb(void *opaque
, target_phys_addr_t offset
)
377 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
379 if (offset
> 0x200) {
380 cpu_abort(cpu_single_env
, "Bad MBAR read offset 0x%x", (int)offset
);
382 if (m5206_mbar_width
[offset
>> 2] > 1) {
384 val
= m5206_mbar_readw(opaque
, offset
& ~1);
385 if ((offset
& 1) == 0) {
390 return m5206_mbar_read(s
, offset
);
393 static uint32_t m5206_mbar_readw(void *opaque
, target_phys_addr_t offset
)
395 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
398 if (offset
> 0x200) {
399 cpu_abort(cpu_single_env
, "Bad MBAR read offset 0x%x", (int)offset
);
401 width
= m5206_mbar_width
[offset
>> 2];
404 val
= m5206_mbar_readl(opaque
, offset
& ~3);
405 if ((offset
& 3) == 0)
408 } else if (width
< 2) {
410 val
= m5206_mbar_readb(opaque
, offset
) << 8;
411 val
|= m5206_mbar_readb(opaque
, offset
+ 1);
414 return m5206_mbar_read(s
, offset
);
417 static uint32_t m5206_mbar_readl(void *opaque
, target_phys_addr_t offset
)
419 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
422 if (offset
> 0x200) {
423 cpu_abort(cpu_single_env
, "Bad MBAR read offset 0x%x", (int)offset
);
425 width
= m5206_mbar_width
[offset
>> 2];
428 val
= m5206_mbar_readw(opaque
, offset
) << 16;
429 val
|= m5206_mbar_readw(opaque
, offset
+ 2);
432 return m5206_mbar_read(s
, offset
);
435 static void m5206_mbar_writew(void *opaque
, target_phys_addr_t offset
,
437 static void m5206_mbar_writel(void *opaque
, target_phys_addr_t offset
,
440 static void m5206_mbar_writeb(void *opaque
, target_phys_addr_t offset
,
443 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
446 if (offset
> 0x200) {
447 cpu_abort(cpu_single_env
, "Bad MBAR write offset 0x%x", (int)offset
);
449 width
= m5206_mbar_width
[offset
>> 2];
452 tmp
= m5206_mbar_readw(opaque
, offset
& ~1);
454 tmp
= (tmp
& 0xff00) | value
;
456 tmp
= (tmp
& 0x00ff) | (value
<< 8);
458 m5206_mbar_writew(opaque
, offset
& ~1, tmp
);
461 m5206_mbar_write(s
, offset
, value
);
464 static void m5206_mbar_writew(void *opaque
, target_phys_addr_t offset
,
467 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
470 if (offset
> 0x200) {
471 cpu_abort(cpu_single_env
, "Bad MBAR write offset 0x%x", (int)offset
);
473 width
= m5206_mbar_width
[offset
>> 2];
476 tmp
= m5206_mbar_readl(opaque
, offset
& ~3);
478 tmp
= (tmp
& 0xffff0000) | value
;
480 tmp
= (tmp
& 0x0000ffff) | (value
<< 16);
482 m5206_mbar_writel(opaque
, offset
& ~3, tmp
);
484 } else if (width
< 2) {
485 m5206_mbar_writeb(opaque
, offset
, value
>> 8);
486 m5206_mbar_writeb(opaque
, offset
+ 1, value
& 0xff);
489 m5206_mbar_write(s
, offset
, value
);
492 static void m5206_mbar_writel(void *opaque
, target_phys_addr_t offset
,
495 m5206_mbar_state
*s
= (m5206_mbar_state
*)opaque
;
498 if (offset
> 0x200) {
499 cpu_abort(cpu_single_env
, "Bad MBAR write offset 0x%x", (int)offset
);
501 width
= m5206_mbar_width
[offset
>> 2];
503 m5206_mbar_writew(opaque
, offset
, value
>> 16);
504 m5206_mbar_writew(opaque
, offset
+ 2, value
& 0xffff);
507 m5206_mbar_write(s
, offset
, value
);
510 static CPUReadMemoryFunc
*m5206_mbar_readfn
[] = {
516 static CPUWriteMemoryFunc
*m5206_mbar_writefn
[] = {
522 qemu_irq
*mcf5206_init(uint32_t base
, CPUState
*env
)
528 s
= (m5206_mbar_state
*)qemu_mallocz(sizeof(m5206_mbar_state
));
529 iomemtype
= cpu_register_io_memory(0, m5206_mbar_readfn
,
530 m5206_mbar_writefn
, s
);
531 cpu_register_physical_memory(base
, 0x00001000, iomemtype
);
533 pic
= qemu_allocate_irqs(m5206_mbar_set_irq
, s
, 14);
534 s
->timer
[0] = m5206_timer_init(pic
[9]);
535 s
->timer
[1] = m5206_timer_init(pic
[10]);
536 s
->uart
[0] = mcf_uart_init(pic
[12], serial_hds
[0]);
537 s
->uart
[1] = mcf_uart_init(pic
[13], serial_hds
[1]);