mcf5208.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Motorola ColdFire MCF5208 SoC emulation.
  3. *
  4. * Copyright (c) 2007 CodeSourcery.
  5. *
  6. * This code is licensed under the GPL
  7. */
  8. #include "qemu/osdep.h"
  9. #include "qemu/units.h"
  10. #include "qemu/error-report.h"
  11. #include "qapi/error.h"
  12. #include "qemu-common.h"
  13. #include "cpu.h"
  14. #include "hw/hw.h"
  15. #include "hw/irq.h"
  16. #include "hw/m68k/mcf.h"
  17. #include "hw/m68k/mcf_fec.h"
  18. #include "qemu/timer.h"
  19. #include "hw/ptimer.h"
  20. #include "sysemu/sysemu.h"
  21. #include "sysemu/qtest.h"
  22. #include "net/net.h"
  23. #include "hw/boards.h"
  24. #include "hw/loader.h"
  25. #include "hw/sysbus.h"
  26. #include "elf.h"
  27. #include "exec/address-spaces.h"
  28. #define SYS_FREQ 166666666
  29. #define ROM_SIZE 0x200000
  30. #define PCSR_EN 0x0001
  31. #define PCSR_RLD 0x0002
  32. #define PCSR_PIF 0x0004
  33. #define PCSR_PIE 0x0008
  34. #define PCSR_OVW 0x0010
  35. #define PCSR_DBG 0x0020
  36. #define PCSR_DOZE 0x0040
  37. #define PCSR_PRE_SHIFT 8
  38. #define PCSR_PRE_MASK 0x0f00
  39. typedef struct {
  40. MemoryRegion iomem;
  41. qemu_irq irq;
  42. ptimer_state *timer;
  43. uint16_t pcsr;
  44. uint16_t pmr;
  45. uint16_t pcntr;
  46. } m5208_timer_state;
  47. static void m5208_timer_update(m5208_timer_state *s)
  48. {
  49. if ((s->pcsr & (PCSR_PIE | PCSR_PIF)) == (PCSR_PIE | PCSR_PIF))
  50. qemu_irq_raise(s->irq);
  51. else
  52. qemu_irq_lower(s->irq);
  53. }
  54. static void m5208_timer_write(void *opaque, hwaddr offset,
  55. uint64_t value, unsigned size)
  56. {
  57. m5208_timer_state *s = (m5208_timer_state *)opaque;
  58. int prescale;
  59. int limit;
  60. switch (offset) {
  61. case 0:
  62. /* The PIF bit is set-to-clear. */
  63. if (value & PCSR_PIF) {
  64. s->pcsr &= ~PCSR_PIF;
  65. value &= ~PCSR_PIF;
  66. }
  67. /* Avoid frobbing the timer if we're just twiddling IRQ bits. */
  68. if (((s->pcsr ^ value) & ~PCSR_PIE) == 0) {
  69. s->pcsr = value;
  70. m5208_timer_update(s);
  71. return;
  72. }
  73. ptimer_transaction_begin(s->timer);
  74. if (s->pcsr & PCSR_EN)
  75. ptimer_stop(s->timer);
  76. s->pcsr = value;
  77. prescale = 1 << ((s->pcsr & PCSR_PRE_MASK) >> PCSR_PRE_SHIFT);
  78. ptimer_set_freq(s->timer, (SYS_FREQ / 2) / prescale);
  79. if (s->pcsr & PCSR_RLD)
  80. limit = s->pmr;
  81. else
  82. limit = 0xffff;
  83. ptimer_set_limit(s->timer, limit, 0);
  84. if (s->pcsr & PCSR_EN)
  85. ptimer_run(s->timer, 0);
  86. ptimer_transaction_commit(s->timer);
  87. break;
  88. case 2:
  89. ptimer_transaction_begin(s->timer);
  90. s->pmr = value;
  91. s->pcsr &= ~PCSR_PIF;
  92. if ((s->pcsr & PCSR_RLD) == 0) {
  93. if (s->pcsr & PCSR_OVW)
  94. ptimer_set_count(s->timer, value);
  95. } else {
  96. ptimer_set_limit(s->timer, value, s->pcsr & PCSR_OVW);
  97. }
  98. ptimer_transaction_commit(s->timer);
  99. break;
  100. case 4:
  101. break;
  102. default:
  103. hw_error("m5208_timer_write: Bad offset 0x%x\n", (int)offset);
  104. break;
  105. }
  106. m5208_timer_update(s);
  107. }
  108. static void m5208_timer_trigger(void *opaque)
  109. {
  110. m5208_timer_state *s = (m5208_timer_state *)opaque;
  111. s->pcsr |= PCSR_PIF;
  112. m5208_timer_update(s);
  113. }
  114. static uint64_t m5208_timer_read(void *opaque, hwaddr addr,
  115. unsigned size)
  116. {
  117. m5208_timer_state *s = (m5208_timer_state *)opaque;
  118. switch (addr) {
  119. case 0:
  120. return s->pcsr;
  121. case 2:
  122. return s->pmr;
  123. case 4:
  124. return ptimer_get_count(s->timer);
  125. default:
  126. hw_error("m5208_timer_read: Bad offset 0x%x\n", (int)addr);
  127. return 0;
  128. }
  129. }
  130. static const MemoryRegionOps m5208_timer_ops = {
  131. .read = m5208_timer_read,
  132. .write = m5208_timer_write,
  133. .endianness = DEVICE_NATIVE_ENDIAN,
  134. };
  135. static uint64_t m5208_sys_read(void *opaque, hwaddr addr,
  136. unsigned size)
  137. {
  138. switch (addr) {
  139. case 0x110: /* SDCS0 */
  140. {
  141. int n;
  142. for (n = 0; n < 32; n++) {
  143. if (ram_size < (2u << n))
  144. break;
  145. }
  146. return (n - 1) | 0x40000000;
  147. }
  148. case 0x114: /* SDCS1 */
  149. return 0;
  150. default:
  151. hw_error("m5208_sys_read: Bad offset 0x%x\n", (int)addr);
  152. return 0;
  153. }
  154. }
  155. static void m5208_sys_write(void *opaque, hwaddr addr,
  156. uint64_t value, unsigned size)
  157. {
  158. hw_error("m5208_sys_write: Bad offset 0x%x\n", (int)addr);
  159. }
  160. static const MemoryRegionOps m5208_sys_ops = {
  161. .read = m5208_sys_read,
  162. .write = m5208_sys_write,
  163. .endianness = DEVICE_NATIVE_ENDIAN,
  164. };
  165. static void mcf5208_sys_init(MemoryRegion *address_space, qemu_irq *pic)
  166. {
  167. MemoryRegion *iomem = g_new(MemoryRegion, 1);
  168. m5208_timer_state *s;
  169. int i;
  170. /* SDRAMC. */
  171. memory_region_init_io(iomem, NULL, &m5208_sys_ops, NULL, "m5208-sys", 0x00004000);
  172. memory_region_add_subregion(address_space, 0xfc0a8000, iomem);
  173. /* Timers. */
  174. for (i = 0; i < 2; i++) {
  175. s = g_new0(m5208_timer_state, 1);
  176. s->timer = ptimer_init(m5208_timer_trigger, s, PTIMER_POLICY_DEFAULT);
  177. memory_region_init_io(&s->iomem, NULL, &m5208_timer_ops, s,
  178. "m5208-timer", 0x00004000);
  179. memory_region_add_subregion(address_space, 0xfc080000 + 0x4000 * i,
  180. &s->iomem);
  181. s->irq = pic[4 + i];
  182. }
  183. }
  184. static void mcf_fec_init(MemoryRegion *sysmem, NICInfo *nd, hwaddr base,
  185. qemu_irq *irqs)
  186. {
  187. DeviceState *dev;
  188. SysBusDevice *s;
  189. int i;
  190. qemu_check_nic_model(nd, TYPE_MCF_FEC_NET);
  191. dev = qdev_create(NULL, TYPE_MCF_FEC_NET);
  192. qdev_set_nic_properties(dev, nd);
  193. qdev_init_nofail(dev);
  194. s = SYS_BUS_DEVICE(dev);
  195. for (i = 0; i < FEC_NUM_IRQ; i++) {
  196. sysbus_connect_irq(s, i, irqs[i]);
  197. }
  198. memory_region_add_subregion(sysmem, base, sysbus_mmio_get_region(s, 0));
  199. }
  200. static void mcf5208evb_init(MachineState *machine)
  201. {
  202. ram_addr_t ram_size = machine->ram_size;
  203. const char *kernel_filename = machine->kernel_filename;
  204. M68kCPU *cpu;
  205. CPUM68KState *env;
  206. int kernel_size;
  207. uint64_t elf_entry;
  208. hwaddr entry;
  209. qemu_irq *pic;
  210. MemoryRegion *address_space_mem = get_system_memory();
  211. MemoryRegion *rom = g_new(MemoryRegion, 1);
  212. MemoryRegion *ram = g_new(MemoryRegion, 1);
  213. MemoryRegion *sram = g_new(MemoryRegion, 1);
  214. cpu = M68K_CPU(cpu_create(machine->cpu_type));
  215. env = &cpu->env;
  216. /* Initialize CPU registers. */
  217. env->vbr = 0;
  218. /* TODO: Configure BARs. */
  219. /* ROM at 0x00000000 */
  220. memory_region_init_rom(rom, NULL, "mcf5208.rom", ROM_SIZE, &error_fatal);
  221. memory_region_add_subregion(address_space_mem, 0x00000000, rom);
  222. /* DRAM at 0x40000000 */
  223. memory_region_allocate_system_memory(ram, NULL, "mcf5208.ram", ram_size);
  224. memory_region_add_subregion(address_space_mem, 0x40000000, ram);
  225. /* Internal SRAM. */
  226. memory_region_init_ram(sram, NULL, "mcf5208.sram", 16 * KiB, &error_fatal);
  227. memory_region_add_subregion(address_space_mem, 0x80000000, sram);
  228. /* Internal peripherals. */
  229. pic = mcf_intc_init(address_space_mem, 0xfc048000, cpu);
  230. mcf_uart_mm_init(0xfc060000, pic[26], serial_hd(0));
  231. mcf_uart_mm_init(0xfc064000, pic[27], serial_hd(1));
  232. mcf_uart_mm_init(0xfc068000, pic[28], serial_hd(2));
  233. mcf5208_sys_init(address_space_mem, pic);
  234. if (nb_nics > 1) {
  235. error_report("Too many NICs");
  236. exit(1);
  237. }
  238. if (nd_table[0].used) {
  239. mcf_fec_init(address_space_mem, &nd_table[0],
  240. 0xfc030000, pic + 36);
  241. }
  242. g_free(pic);
  243. /* 0xfc000000 SCM. */
  244. /* 0xfc004000 XBS. */
  245. /* 0xfc008000 FlexBus CS. */
  246. /* 0xfc030000 FEC. */
  247. /* 0xfc040000 SCM + Power management. */
  248. /* 0xfc044000 eDMA. */
  249. /* 0xfc048000 INTC. */
  250. /* 0xfc058000 I2C. */
  251. /* 0xfc05c000 QSPI. */
  252. /* 0xfc060000 UART0. */
  253. /* 0xfc064000 UART0. */
  254. /* 0xfc068000 UART0. */
  255. /* 0xfc070000 DMA timers. */
  256. /* 0xfc080000 PIT0. */
  257. /* 0xfc084000 PIT1. */
  258. /* 0xfc088000 EPORT. */
  259. /* 0xfc08c000 Watchdog. */
  260. /* 0xfc090000 clock module. */
  261. /* 0xfc0a0000 CCM + reset. */
  262. /* 0xfc0a4000 GPIO. */
  263. /* 0xfc0a8000 SDRAM controller. */
  264. /* Load firmware */
  265. if (bios_name) {
  266. char *fn;
  267. uint8_t *ptr;
  268. fn = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  269. if (!fn) {
  270. error_report("Could not find ROM image '%s'", bios_name);
  271. exit(1);
  272. }
  273. if (load_image_targphys(fn, 0x0, ROM_SIZE) < 8) {
  274. error_report("Could not load ROM image '%s'", bios_name);
  275. exit(1);
  276. }
  277. g_free(fn);
  278. /* Initial PC is always at offset 4 in firmware binaries */
  279. ptr = rom_ptr(0x4, 4);
  280. assert(ptr != NULL);
  281. env->pc = ldl_p(ptr);
  282. }
  283. /* Load kernel. */
  284. if (!kernel_filename) {
  285. if (qtest_enabled() || bios_name) {
  286. return;
  287. }
  288. error_report("Kernel image must be specified");
  289. exit(1);
  290. }
  291. kernel_size = load_elf(kernel_filename, NULL, NULL, NULL, &elf_entry,
  292. NULL, NULL, 1, EM_68K, 0, 0);
  293. entry = elf_entry;
  294. if (kernel_size < 0) {
  295. kernel_size = load_uimage(kernel_filename, &entry, NULL, NULL,
  296. NULL, NULL);
  297. }
  298. if (kernel_size < 0) {
  299. kernel_size = load_image_targphys(kernel_filename, 0x40000000,
  300. ram_size);
  301. entry = 0x40000000;
  302. }
  303. if (kernel_size < 0) {
  304. error_report("Could not load kernel '%s'", kernel_filename);
  305. exit(1);
  306. }
  307. env->pc = entry;
  308. }
  309. static void mcf5208evb_machine_init(MachineClass *mc)
  310. {
  311. mc->desc = "MCF5208EVB";
  312. mc->init = mcf5208evb_init;
  313. mc->is_default = 1;
  314. mc->default_cpu_type = M68K_CPU_TYPE_NAME("m5208");
  315. }
  316. DEFINE_MACHINE("mcf5208evb", mcf5208evb_machine_init)