virtex_ml507.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Model of Xilinx Virtex5 ML507 PPC-440 refdesign.
  3. *
  4. * Copyright (c) 2010 Edgar E. Iglesias.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qemu/datadir.h"
  26. #include "qemu/units.h"
  27. #include "exec/page-protection.h"
  28. #include "cpu.h"
  29. #include "hw/sysbus.h"
  30. #include "hw/char/serial-mm.h"
  31. #include "hw/block/flash.h"
  32. #include "sysemu/sysemu.h"
  33. #include "sysemu/reset.h"
  34. #include "hw/boards.h"
  35. #include "sysemu/device_tree.h"
  36. #include "hw/loader.h"
  37. #include "elf.h"
  38. #include "qapi/error.h"
  39. #include "qemu/error-report.h"
  40. #include "qemu/option.h"
  41. #include "hw/intc/ppc-uic.h"
  42. #include "hw/ppc/ppc.h"
  43. #include "hw/ppc/ppc4xx.h"
  44. #include "hw/qdev-properties.h"
  45. #include <libfdt.h>
  46. #define EPAPR_MAGIC (0x45504150)
  47. #define FLASH_SIZE (16 * MiB)
  48. #define INTC_BASEADDR 0x81800000
  49. #define UART16550_BASEADDR 0x83e01003
  50. #define TIMER_BASEADDR 0x83c00000
  51. #define PFLASH_BASEADDR 0xfc000000
  52. #define TIMER_IRQ 3
  53. #define UART16550_IRQ 9
  54. static struct boot_info
  55. {
  56. uint32_t bootstrap_pc;
  57. uint32_t cmdline;
  58. uint32_t fdt;
  59. uint32_t ima_size;
  60. void *vfdt;
  61. } boot_info;
  62. /* Create reset TLB entries for BookE, spanning the 32bit addr space. */
  63. static void mmubooke_create_initial_mapping(CPUPPCState *env,
  64. target_ulong va,
  65. hwaddr pa)
  66. {
  67. ppcemb_tlb_t *tlb = &env->tlb.tlbe[0];
  68. tlb->attr = 0;
  69. tlb->prot = PAGE_VALID | ((PAGE_READ | PAGE_WRITE | PAGE_EXEC) << 4);
  70. tlb->size = 1U << 31; /* up to 0x80000000 */
  71. tlb->EPN = va & TARGET_PAGE_MASK;
  72. tlb->RPN = pa & TARGET_PAGE_MASK;
  73. tlb->PID = 0;
  74. tlb = &env->tlb.tlbe[1];
  75. tlb->attr = 0;
  76. tlb->prot = PAGE_VALID | ((PAGE_READ | PAGE_WRITE | PAGE_EXEC) << 4);
  77. tlb->size = 1U << 31; /* up to 0xffffffff */
  78. tlb->EPN = 0x80000000 & TARGET_PAGE_MASK;
  79. tlb->RPN = 0x80000000 & TARGET_PAGE_MASK;
  80. tlb->PID = 0;
  81. }
  82. static PowerPCCPU *ppc440_init_xilinx(const char *cpu_type, uint32_t sysclk)
  83. {
  84. PowerPCCPU *cpu;
  85. CPUPPCState *env;
  86. DeviceState *uicdev;
  87. SysBusDevice *uicsbd;
  88. cpu = POWERPC_CPU(cpu_create(cpu_type));
  89. env = &cpu->env;
  90. ppc_booke_timers_init(cpu, sysclk, 0/* no flags */);
  91. ppc_dcr_init(env, NULL, NULL);
  92. /* interrupt controller */
  93. uicdev = qdev_new(TYPE_PPC_UIC);
  94. ppc4xx_dcr_realize(PPC4xx_DCR_DEVICE(uicdev), cpu, &error_fatal);
  95. object_unref(OBJECT(uicdev));
  96. uicsbd = SYS_BUS_DEVICE(uicdev);
  97. sysbus_connect_irq(uicsbd, PPCUIC_OUTPUT_INT,
  98. qdev_get_gpio_in(DEVICE(cpu), PPC40x_INPUT_INT));
  99. sysbus_connect_irq(uicsbd, PPCUIC_OUTPUT_CINT,
  100. qdev_get_gpio_in(DEVICE(cpu), PPC40x_INPUT_CINT));
  101. /* This board doesn't wire anything up to the inputs of the UIC. */
  102. return cpu;
  103. }
  104. static void main_cpu_reset(void *opaque)
  105. {
  106. PowerPCCPU *cpu = opaque;
  107. CPUPPCState *env = &cpu->env;
  108. struct boot_info *bi = env->load_info;
  109. cpu_reset(CPU(cpu));
  110. /* Linux Kernel Parameters (passing device tree):
  111. * r3: pointer to the fdt
  112. * r4: 0
  113. * r5: 0
  114. * r6: epapr magic
  115. * r7: size of IMA in bytes
  116. * r8: 0
  117. * r9: 0
  118. */
  119. env->gpr[1] = (16 * MiB) - 8;
  120. /* Provide a device-tree. */
  121. env->gpr[3] = bi->fdt;
  122. env->nip = bi->bootstrap_pc;
  123. /* Create a mapping for the kernel. */
  124. mmubooke_create_initial_mapping(env, 0, 0);
  125. env->gpr[6] = tswap32(EPAPR_MAGIC);
  126. env->gpr[7] = bi->ima_size;
  127. }
  128. #define BINARY_DEVICE_TREE_FILE "virtex-ml507.dtb"
  129. static int xilinx_load_device_tree(MachineState *machine,
  130. hwaddr addr,
  131. hwaddr initrd_base,
  132. hwaddr initrd_size)
  133. {
  134. char *path;
  135. int fdt_size;
  136. void *fdt = NULL;
  137. int r;
  138. const char *dtb_filename;
  139. dtb_filename = machine->dtb;
  140. if (dtb_filename) {
  141. fdt = load_device_tree(dtb_filename, &fdt_size);
  142. if (!fdt) {
  143. error_report("Error while loading device tree file '%s'",
  144. dtb_filename);
  145. }
  146. } else {
  147. /* Try the local "ppc.dtb" override. */
  148. fdt = load_device_tree("ppc.dtb", &fdt_size);
  149. if (!fdt) {
  150. path = qemu_find_file(QEMU_FILE_TYPE_BIOS, BINARY_DEVICE_TREE_FILE);
  151. if (path) {
  152. fdt = load_device_tree(path, &fdt_size);
  153. g_free(path);
  154. }
  155. }
  156. }
  157. if (!fdt) {
  158. return 0;
  159. }
  160. r = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start",
  161. initrd_base);
  162. if (r < 0) {
  163. error_report("couldn't set /chosen/linux,initrd-start");
  164. }
  165. r = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
  166. (initrd_base + initrd_size));
  167. if (r < 0) {
  168. error_report("couldn't set /chosen/linux,initrd-end");
  169. }
  170. r = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
  171. machine->kernel_cmdline);
  172. if (r < 0)
  173. fprintf(stderr, "couldn't set /chosen/bootargs\n");
  174. cpu_physical_memory_write(addr, fdt, fdt_size);
  175. /* Set machine->fdt for 'dumpdtb' QMP/HMP command */
  176. machine->fdt = fdt;
  177. return fdt_size;
  178. }
  179. static void virtex_init(MachineState *machine)
  180. {
  181. const char *kernel_filename = machine->kernel_filename;
  182. hwaddr initrd_base = 0;
  183. int initrd_size = 0;
  184. MemoryRegion *address_space_mem = get_system_memory();
  185. DeviceState *dev;
  186. PowerPCCPU *cpu;
  187. CPUPPCState *env;
  188. hwaddr ram_base = 0;
  189. DriveInfo *dinfo;
  190. qemu_irq irq[32], cpu_irq;
  191. int kernel_size;
  192. int i;
  193. /* init CPUs */
  194. cpu = ppc440_init_xilinx(machine->cpu_type, 400000000);
  195. env = &cpu->env;
  196. if (env->mmu_model != POWERPC_MMU_BOOKE) {
  197. error_report("MMU model %i not supported by this machine",
  198. env->mmu_model);
  199. exit(1);
  200. }
  201. qemu_register_reset(main_cpu_reset, cpu);
  202. memory_region_add_subregion(address_space_mem, ram_base, machine->ram);
  203. dinfo = drive_get(IF_PFLASH, 0, 0);
  204. pflash_cfi01_register(PFLASH_BASEADDR, "virtex.flash", FLASH_SIZE,
  205. dinfo ? blk_by_legacy_dinfo(dinfo) : NULL,
  206. 64 * KiB, 1, 0x89, 0x18, 0x0000, 0x0, 1);
  207. cpu_irq = qdev_get_gpio_in(DEVICE(cpu), PPC40x_INPUT_INT);
  208. dev = qdev_new("xlnx.xps-intc");
  209. qdev_prop_set_uint32(dev, "kind-of-intr", 0);
  210. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  211. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, INTC_BASEADDR);
  212. sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, cpu_irq);
  213. for (i = 0; i < 32; i++) {
  214. irq[i] = qdev_get_gpio_in(dev, i);
  215. }
  216. serial_mm_init(address_space_mem, UART16550_BASEADDR, 2, irq[UART16550_IRQ],
  217. 115200, serial_hd(0), DEVICE_LITTLE_ENDIAN);
  218. /* 2 timers at irq 2 @ 62 Mhz. */
  219. dev = qdev_new("xlnx.xps-timer");
  220. qdev_prop_set_uint32(dev, "one-timer-only", 0);
  221. qdev_prop_set_uint32(dev, "clock-frequency", 62 * 1000000);
  222. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  223. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, TIMER_BASEADDR);
  224. sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, irq[TIMER_IRQ]);
  225. if (kernel_filename) {
  226. uint64_t entry, high;
  227. hwaddr boot_offset;
  228. /* Boots a kernel elf binary. */
  229. kernel_size = load_elf(kernel_filename, NULL, NULL, NULL,
  230. &entry, NULL, &high, NULL, 1, PPC_ELF_MACHINE,
  231. 0, 0);
  232. boot_info.bootstrap_pc = entry & 0x00ffffff;
  233. if (kernel_size < 0) {
  234. boot_offset = 0x1200000;
  235. /* If we failed loading ELF's try a raw image. */
  236. kernel_size = load_image_targphys(kernel_filename,
  237. boot_offset,
  238. machine->ram_size);
  239. boot_info.bootstrap_pc = boot_offset;
  240. high = boot_info.bootstrap_pc + kernel_size + 8192;
  241. }
  242. boot_info.ima_size = kernel_size;
  243. /* Load initrd. */
  244. if (machine->initrd_filename) {
  245. initrd_base = high = ROUND_UP(high, 4);
  246. initrd_size = load_image_targphys(machine->initrd_filename,
  247. high, machine->ram_size - high);
  248. if (initrd_size < 0) {
  249. error_report("couldn't load ram disk '%s'",
  250. machine->initrd_filename);
  251. exit(1);
  252. }
  253. high = ROUND_UP(high + initrd_size, 4);
  254. }
  255. /* Provide a device-tree. */
  256. boot_info.fdt = high + (8192 * 2);
  257. boot_info.fdt &= ~8191;
  258. xilinx_load_device_tree(machine, boot_info.fdt,
  259. initrd_base, initrd_size);
  260. }
  261. env->load_info = &boot_info;
  262. }
  263. static void virtex_machine_init(MachineClass *mc)
  264. {
  265. mc->desc = "Xilinx Virtex ML507 reference design";
  266. mc->init = virtex_init;
  267. mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("440-xilinx");
  268. mc->default_ram_id = "ram";
  269. }
  270. DEFINE_MACHINE("virtex-ml507", virtex_machine_init)