2 * Motorola ColdFire MCF5208 SoC emulation.
4 * Copyright (c) 2007 CodeSourcery.
6 * This code is licensed under the GPL
8 * This file models both the MCF5208 SoC, and the
9 * MCF5208EVB evaluation board. For details see
11 * "MCF5208 Reference Manual"
12 * https://www.nxp.com/docs/en/reference-manual/MCF5208RM.pdf
13 * "M5208EVB-RevB 32-bit Microcontroller User Manual"
14 * https://www.nxp.com/docs/en/reference-manual/M5208EVBUM.pdf
17 #include "qemu/osdep.h"
18 #include "qemu/units.h"
19 #include "qemu/error-report.h"
21 #include "qapi/error.h"
22 #include "qemu/datadir.h"
25 #include "hw/m68k/mcf.h"
26 #include "hw/m68k/mcf_fec.h"
27 #include "qemu/timer.h"
28 #include "hw/ptimer.h"
29 #include "sysemu/sysemu.h"
30 #include "sysemu/qtest.h"
32 #include "hw/boards.h"
33 #include "hw/loader.h"
34 #include "hw/sysbus.h"
37 #define SYS_FREQ 166666666
39 #define ROM_SIZE 0x200000
41 #define PCSR_EN 0x0001
42 #define PCSR_RLD 0x0002
43 #define PCSR_PIF 0x0004
44 #define PCSR_PIE 0x0008
45 #define PCSR_OVW 0x0010
46 #define PCSR_DBG 0x0020
47 #define PCSR_DOZE 0x0040
48 #define PCSR_PRE_SHIFT 8
49 #define PCSR_PRE_MASK 0x0f00
51 #define RCR_SOFTRST 0x80
62 static void m5208_timer_update(m5208_timer_state
*s
)
64 if ((s
->pcsr
& (PCSR_PIE
| PCSR_PIF
)) == (PCSR_PIE
| PCSR_PIF
))
65 qemu_irq_raise(s
->irq
);
67 qemu_irq_lower(s
->irq
);
70 static void m5208_timer_write(void *opaque
, hwaddr offset
,
71 uint64_t value
, unsigned size
)
73 m5208_timer_state
*s
= (m5208_timer_state
*)opaque
;
78 /* The PIF bit is set-to-clear. */
79 if (value
& PCSR_PIF
) {
83 /* Avoid frobbing the timer if we're just twiddling IRQ bits. */
84 if (((s
->pcsr
^ value
) & ~PCSR_PIE
) == 0) {
86 m5208_timer_update(s
);
90 ptimer_transaction_begin(s
->timer
);
91 if (s
->pcsr
& PCSR_EN
)
92 ptimer_stop(s
->timer
);
96 prescale
= 1 << ((s
->pcsr
& PCSR_PRE_MASK
) >> PCSR_PRE_SHIFT
);
97 ptimer_set_freq(s
->timer
, (SYS_FREQ
/ 2) / prescale
);
98 if (s
->pcsr
& PCSR_RLD
)
102 ptimer_set_limit(s
->timer
, limit
, 0);
104 if (s
->pcsr
& PCSR_EN
)
105 ptimer_run(s
->timer
, 0);
106 ptimer_transaction_commit(s
->timer
);
109 ptimer_transaction_begin(s
->timer
);
111 s
->pcsr
&= ~PCSR_PIF
;
112 if ((s
->pcsr
& PCSR_RLD
) == 0) {
113 if (s
->pcsr
& PCSR_OVW
)
114 ptimer_set_count(s
->timer
, value
);
116 ptimer_set_limit(s
->timer
, value
, s
->pcsr
& PCSR_OVW
);
118 ptimer_transaction_commit(s
->timer
);
123 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset 0x%" HWADDR_PRIX
"\n",
127 m5208_timer_update(s
);
130 static void m5208_timer_trigger(void *opaque
)
132 m5208_timer_state
*s
= (m5208_timer_state
*)opaque
;
134 m5208_timer_update(s
);
137 static uint64_t m5208_timer_read(void *opaque
, hwaddr addr
,
140 m5208_timer_state
*s
= (m5208_timer_state
*)opaque
;
147 return ptimer_get_count(s
->timer
);
149 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset 0x%" HWADDR_PRIX
"\n",
155 static const MemoryRegionOps m5208_timer_ops
= {
156 .read
= m5208_timer_read
,
157 .write
= m5208_timer_write
,
158 .endianness
= DEVICE_NATIVE_ENDIAN
,
161 static uint64_t m5208_sys_read(void *opaque
, hwaddr addr
,
165 case 0x110: /* SDCS0 */
168 for (n
= 0; n
< 32; n
++) {
169 if (current_machine
->ram_size
< (2ULL << n
)) {
173 return (n
- 1) | 0x40000000;
175 case 0x114: /* SDCS1 */
179 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset 0x%" HWADDR_PRIX
"\n",
185 static void m5208_sys_write(void *opaque
, hwaddr addr
,
186 uint64_t value
, unsigned size
)
188 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset 0x%" HWADDR_PRIX
"\n",
192 static const MemoryRegionOps m5208_sys_ops
= {
193 .read
= m5208_sys_read
,
194 .write
= m5208_sys_write
,
195 .endianness
= DEVICE_NATIVE_ENDIAN
,
198 static uint64_t m5208_rcm_read(void *opaque
, hwaddr addr
,
204 static void m5208_rcm_write(void *opaque
, hwaddr addr
,
205 uint64_t value
, unsigned size
)
207 M68kCPU
*cpu
= opaque
;
208 CPUState
*cs
= CPU(cpu
);
211 if (value
& RCR_SOFTRST
) {
213 cpu
->env
.aregs
[7] = ldl_phys(cs
->as
, 0);
214 cpu
->env
.pc
= ldl_phys(cs
->as
, 4);
218 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset 0x%" HWADDR_PRIX
"\n",
224 static const MemoryRegionOps m5208_rcm_ops
= {
225 .read
= m5208_rcm_read
,
226 .write
= m5208_rcm_write
,
227 .endianness
= DEVICE_NATIVE_ENDIAN
,
230 static void mcf5208_sys_init(MemoryRegion
*address_space
, qemu_irq
*pic
,
233 MemoryRegion
*iomem
= g_new(MemoryRegion
, 1);
234 MemoryRegion
*iomem_rcm
= g_new(MemoryRegion
, 1);
235 m5208_timer_state
*s
;
239 memory_region_init_io(iomem_rcm
, NULL
, &m5208_rcm_ops
, cpu
,
240 "m5208-rcm", 0x00000080);
241 memory_region_add_subregion(address_space
, 0xfc0a0000, iomem_rcm
);
243 memory_region_init_io(iomem
, NULL
, &m5208_sys_ops
, NULL
, "m5208-sys", 0x00004000);
244 memory_region_add_subregion(address_space
, 0xfc0a8000, iomem
);
246 for (i
= 0; i
< 2; i
++) {
247 s
= g_new0(m5208_timer_state
, 1);
248 s
->timer
= ptimer_init(m5208_timer_trigger
, s
, PTIMER_POLICY_LEGACY
);
249 memory_region_init_io(&s
->iomem
, NULL
, &m5208_timer_ops
, s
,
250 "m5208-timer", 0x00004000);
251 memory_region_add_subregion(address_space
, 0xfc080000 + 0x4000 * i
,
257 static void mcf_fec_init(MemoryRegion
*sysmem
, hwaddr base
, qemu_irq
*irqs
)
263 dev
= qemu_create_nic_device(TYPE_MCF_FEC_NET
, true, NULL
);
268 s
= SYS_BUS_DEVICE(dev
);
269 sysbus_realize_and_unref(s
, &error_fatal
);
270 for (i
= 0; i
< FEC_NUM_IRQ
; i
++) {
271 sysbus_connect_irq(s
, i
, irqs
[i
]);
274 memory_region_add_subregion(sysmem
, base
, sysbus_mmio_get_region(s
, 0));
277 static void mcf5208evb_init(MachineState
*machine
)
279 ram_addr_t ram_size
= machine
->ram_size
;
280 const char *kernel_filename
= machine
->kernel_filename
;
287 MemoryRegion
*address_space_mem
= get_system_memory();
288 MemoryRegion
*rom
= g_new(MemoryRegion
, 1);
289 MemoryRegion
*sram
= g_new(MemoryRegion
, 1);
291 cpu
= M68K_CPU(cpu_create(machine
->cpu_type
));
294 /* Initialize CPU registers. */
296 /* TODO: Configure BARs. */
298 /* ROM at 0x00000000 */
299 memory_region_init_rom(rom
, NULL
, "mcf5208.rom", ROM_SIZE
, &error_fatal
);
300 memory_region_add_subregion(address_space_mem
, 0x00000000, rom
);
302 /* DRAM at 0x40000000 */
303 memory_region_add_subregion(address_space_mem
, 0x40000000, machine
->ram
);
306 memory_region_init_ram(sram
, NULL
, "mcf5208.sram", 16 * KiB
, &error_fatal
);
307 memory_region_add_subregion(address_space_mem
, 0x80000000, sram
);
309 /* Internal peripherals. */
310 pic
= mcf_intc_init(address_space_mem
, 0xfc048000, cpu
);
312 mcf_uart_create_mmap(0xfc060000, pic
[26], serial_hd(0));
313 mcf_uart_create_mmap(0xfc064000, pic
[27], serial_hd(1));
314 mcf_uart_create_mmap(0xfc068000, pic
[28], serial_hd(2));
316 mcf5208_sys_init(address_space_mem
, pic
, cpu
);
318 mcf_fec_init(address_space_mem
, 0xfc030000, pic
+ 36);
322 /* 0xfc000000 SCM. */
323 /* 0xfc004000 XBS. */
324 /* 0xfc008000 FlexBus CS. */
325 /* 0xfc030000 FEC. */
326 /* 0xfc040000 SCM + Power management. */
327 /* 0xfc044000 eDMA. */
328 /* 0xfc048000 INTC. */
329 /* 0xfc058000 I2C. */
330 /* 0xfc05c000 QSPI. */
331 /* 0xfc060000 UART0. */
332 /* 0xfc064000 UART0. */
333 /* 0xfc068000 UART0. */
334 /* 0xfc070000 DMA timers. */
335 /* 0xfc080000 PIT0. */
336 /* 0xfc084000 PIT1. */
337 /* 0xfc088000 EPORT. */
338 /* 0xfc08c000 Watchdog. */
339 /* 0xfc090000 clock module. */
340 /* 0xfc0a0000 CCM + reset. */
341 /* 0xfc0a4000 GPIO. */
342 /* 0xfc0a8000 SDRAM controller. */
345 if (machine
->firmware
) {
349 fn
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, machine
->firmware
);
351 error_report("Could not find ROM image '%s'", machine
->firmware
);
354 if (load_image_targphys(fn
, 0x0, ROM_SIZE
) < 8) {
355 error_report("Could not load ROM image '%s'", machine
->firmware
);
359 /* Initial PC is always at offset 4 in firmware binaries */
360 ptr
= rom_ptr(0x4, 4);
362 env
->pc
= ldl_be_p(ptr
);
366 if (!kernel_filename
) {
367 if (qtest_enabled() || machine
->firmware
) {
370 error_report("Kernel image must be specified");
374 kernel_size
= load_elf(kernel_filename
, NULL
, NULL
, NULL
, &elf_entry
,
375 NULL
, NULL
, NULL
, 1, EM_68K
, 0, 0);
377 if (kernel_size
< 0) {
378 kernel_size
= load_uimage(kernel_filename
, &entry
, NULL
, NULL
,
381 if (kernel_size
< 0) {
382 kernel_size
= load_image_targphys(kernel_filename
, 0x40000000,
386 if (kernel_size
< 0) {
387 error_report("Could not load kernel '%s'", kernel_filename
);
394 static void mcf5208evb_machine_init(MachineClass
*mc
)
396 mc
->desc
= "MCF5208EVB";
397 mc
->init
= mcf5208evb_init
;
398 mc
->is_default
= true;
399 mc
->default_cpu_type
= M68K_CPU_TYPE_NAME("m5208");
400 mc
->default_ram_id
= "mcf5208.ram";
403 DEFINE_MACHINE("mcf5208evb", mcf5208evb_machine_init
)