2 * Motorola ColdFire MCF5208 SoC emulation.
4 * Copyright (c) 2007 CodeSourcery.
6 * This code is licensed under the GPL
8 #include "qemu/osdep.h"
9 #include "qapi/error.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 "sysemu/qtest.h"
19 #include "hw/boards.h"
20 #include "hw/loader.h"
22 #include "exec/address-spaces.h"
24 #define SYS_FREQ 166666666
26 #define PCSR_EN 0x0001
27 #define PCSR_RLD 0x0002
28 #define PCSR_PIF 0x0004
29 #define PCSR_PIE 0x0008
30 #define PCSR_OVW 0x0010
31 #define PCSR_DBG 0x0020
32 #define PCSR_DOZE 0x0040
33 #define PCSR_PRE_SHIFT 8
34 #define PCSR_PRE_MASK 0x0f00
45 static void m5208_timer_update(m5208_timer_state
*s
)
47 if ((s
->pcsr
& (PCSR_PIE
| PCSR_PIF
)) == (PCSR_PIE
| PCSR_PIF
))
48 qemu_irq_raise(s
->irq
);
50 qemu_irq_lower(s
->irq
);
53 static void m5208_timer_write(void *opaque
, hwaddr offset
,
54 uint64_t value
, unsigned size
)
56 m5208_timer_state
*s
= (m5208_timer_state
*)opaque
;
61 /* The PIF bit is set-to-clear. */
62 if (value
& PCSR_PIF
) {
66 /* Avoid frobbing the timer if we're just twiddling IRQ bits. */
67 if (((s
->pcsr
^ value
) & ~PCSR_PIE
) == 0) {
69 m5208_timer_update(s
);
73 if (s
->pcsr
& PCSR_EN
)
74 ptimer_stop(s
->timer
);
78 prescale
= 1 << ((s
->pcsr
& PCSR_PRE_MASK
) >> PCSR_PRE_SHIFT
);
79 ptimer_set_freq(s
->timer
, (SYS_FREQ
/ 2) / prescale
);
80 if (s
->pcsr
& PCSR_RLD
)
84 ptimer_set_limit(s
->timer
, limit
, 0);
86 if (s
->pcsr
& PCSR_EN
)
87 ptimer_run(s
->timer
, 0);
92 if ((s
->pcsr
& PCSR_RLD
) == 0) {
93 if (s
->pcsr
& PCSR_OVW
)
94 ptimer_set_count(s
->timer
, value
);
96 ptimer_set_limit(s
->timer
, value
, s
->pcsr
& PCSR_OVW
);
102 hw_error("m5208_timer_write: Bad offset 0x%x\n", (int)offset
);
105 m5208_timer_update(s
);
108 static void m5208_timer_trigger(void *opaque
)
110 m5208_timer_state
*s
= (m5208_timer_state
*)opaque
;
112 m5208_timer_update(s
);
115 static uint64_t m5208_timer_read(void *opaque
, hwaddr addr
,
118 m5208_timer_state
*s
= (m5208_timer_state
*)opaque
;
125 return ptimer_get_count(s
->timer
);
127 hw_error("m5208_timer_read: Bad offset 0x%x\n", (int)addr
);
132 static const MemoryRegionOps m5208_timer_ops
= {
133 .read
= m5208_timer_read
,
134 .write
= m5208_timer_write
,
135 .endianness
= DEVICE_NATIVE_ENDIAN
,
138 static uint64_t m5208_sys_read(void *opaque
, hwaddr addr
,
142 case 0x110: /* SDCS0 */
145 for (n
= 0; n
< 32; n
++) {
146 if (ram_size
< (2u << n
))
149 return (n
- 1) | 0x40000000;
151 case 0x114: /* SDCS1 */
155 hw_error("m5208_sys_read: Bad offset 0x%x\n", (int)addr
);
160 static void m5208_sys_write(void *opaque
, hwaddr addr
,
161 uint64_t value
, unsigned size
)
163 hw_error("m5208_sys_write: Bad offset 0x%x\n", (int)addr
);
166 static const MemoryRegionOps m5208_sys_ops
= {
167 .read
= m5208_sys_read
,
168 .write
= m5208_sys_write
,
169 .endianness
= DEVICE_NATIVE_ENDIAN
,
172 static void mcf5208_sys_init(MemoryRegion
*address_space
, qemu_irq
*pic
)
174 MemoryRegion
*iomem
= g_new(MemoryRegion
, 1);
175 m5208_timer_state
*s
;
180 memory_region_init_io(iomem
, NULL
, &m5208_sys_ops
, NULL
, "m5208-sys", 0x00004000);
181 memory_region_add_subregion(address_space
, 0xfc0a8000, iomem
);
183 for (i
= 0; i
< 2; i
++) {
184 s
= (m5208_timer_state
*)g_malloc0(sizeof(m5208_timer_state
));
185 bh
= qemu_bh_new(m5208_timer_trigger
, s
);
186 s
->timer
= ptimer_init(bh
, PTIMER_POLICY_DEFAULT
);
187 memory_region_init_io(&s
->iomem
, NULL
, &m5208_timer_ops
, s
,
188 "m5208-timer", 0x00004000);
189 memory_region_add_subregion(address_space
, 0xfc080000 + 0x4000 * i
,
195 static void mcf5208evb_init(MachineState
*machine
)
197 ram_addr_t ram_size
= machine
->ram_size
;
198 const char *cpu_model
= machine
->cpu_model
;
199 const char *kernel_filename
= machine
->kernel_filename
;
206 MemoryRegion
*address_space_mem
= get_system_memory();
207 MemoryRegion
*ram
= g_new(MemoryRegion
, 1);
208 MemoryRegion
*sram
= g_new(MemoryRegion
, 1);
213 cpu
= cpu_m68k_init(cpu_model
);
215 fprintf(stderr
, "Unable to find m68k CPU definition\n");
220 /* Initialize CPU registers. */
222 /* TODO: Configure BARs. */
224 /* DRAM at 0x40000000 */
225 memory_region_allocate_system_memory(ram
, NULL
, "mcf5208.ram", ram_size
);
226 memory_region_add_subregion(address_space_mem
, 0x40000000, ram
);
229 memory_region_init_ram(sram
, NULL
, "mcf5208.sram", 16384, &error_fatal
);
230 vmstate_register_ram_global(sram
);
231 memory_region_add_subregion(address_space_mem
, 0x80000000, sram
);
233 /* Internal peripherals. */
234 pic
= mcf_intc_init(address_space_mem
, 0xfc048000, cpu
);
236 mcf_uart_mm_init(address_space_mem
, 0xfc060000, pic
[26], serial_hds
[0]);
237 mcf_uart_mm_init(address_space_mem
, 0xfc064000, pic
[27], serial_hds
[1]);
238 mcf_uart_mm_init(address_space_mem
, 0xfc068000, pic
[28], serial_hds
[2]);
240 mcf5208_sys_init(address_space_mem
, pic
);
243 fprintf(stderr
, "Too many NICs\n");
246 if (nd_table
[0].used
)
247 mcf_fec_init(address_space_mem
, &nd_table
[0],
248 0xfc030000, pic
+ 36);
250 /* 0xfc000000 SCM. */
251 /* 0xfc004000 XBS. */
252 /* 0xfc008000 FlexBus CS. */
253 /* 0xfc030000 FEC. */
254 /* 0xfc040000 SCM + Power management. */
255 /* 0xfc044000 eDMA. */
256 /* 0xfc048000 INTC. */
257 /* 0xfc058000 I2C. */
258 /* 0xfc05c000 QSPI. */
259 /* 0xfc060000 UART0. */
260 /* 0xfc064000 UART0. */
261 /* 0xfc068000 UART0. */
262 /* 0xfc070000 DMA timers. */
263 /* 0xfc080000 PIT0. */
264 /* 0xfc084000 PIT1. */
265 /* 0xfc088000 EPORT. */
266 /* 0xfc08c000 Watchdog. */
267 /* 0xfc090000 clock module. */
268 /* 0xfc0a0000 CCM + reset. */
269 /* 0xfc0a4000 GPIO. */
270 /* 0xfc0a8000 SDRAM controller. */
273 if (!kernel_filename
) {
274 if (qtest_enabled()) {
277 fprintf(stderr
, "Kernel image must be specified\n");
281 kernel_size
= load_elf(kernel_filename
, NULL
, NULL
, &elf_entry
,
282 NULL
, NULL
, 1, EM_68K
, 0, 0);
284 if (kernel_size
< 0) {
285 kernel_size
= load_uimage(kernel_filename
, &entry
, NULL
, NULL
,
288 if (kernel_size
< 0) {
289 kernel_size
= load_image_targphys(kernel_filename
, 0x40000000,
293 if (kernel_size
< 0) {
294 fprintf(stderr
, "qemu: could not load kernel '%s'\n", kernel_filename
);
301 static void mcf5208evb_machine_init(MachineClass
*mc
)
303 mc
->desc
= "MCF5206EVB";
304 mc
->init
= mcf5208evb_init
;
308 DEFINE_MACHINE("mcf5208evb", mcf5208evb_machine_init
)