realview.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * ARM RealView Baseboard System emulation.
  3. *
  4. * Copyright (c) 2006-2007 CodeSourcery.
  5. * Written by Paul Brook
  6. *
  7. * This code is licensed under the GPL.
  8. */
  9. #include "sysbus.h"
  10. #include "arm-misc.h"
  11. #include "primecell.h"
  12. #include "devices.h"
  13. #include "pci.h"
  14. #include "usb-ohci.h"
  15. #include "net.h"
  16. #include "sysemu.h"
  17. #include "boards.h"
  18. #include "bitbang_i2c.h"
  19. #include "blockdev.h"
  20. #include "exec-memory.h"
  21. #define SMP_BOOT_ADDR 0xe0000000
  22. typedef struct {
  23. SysBusDevice busdev;
  24. MemoryRegion iomem;
  25. bitbang_i2c_interface *bitbang;
  26. int out;
  27. int in;
  28. } RealViewI2CState;
  29. static uint64_t realview_i2c_read(void *opaque, target_phys_addr_t offset,
  30. unsigned size)
  31. {
  32. RealViewI2CState *s = (RealViewI2CState *)opaque;
  33. if (offset == 0) {
  34. return (s->out & 1) | (s->in << 1);
  35. } else {
  36. hw_error("realview_i2c_read: Bad offset 0x%x\n", (int)offset);
  37. return -1;
  38. }
  39. }
  40. static void realview_i2c_write(void *opaque, target_phys_addr_t offset,
  41. uint64_t value, unsigned size)
  42. {
  43. RealViewI2CState *s = (RealViewI2CState *)opaque;
  44. switch (offset) {
  45. case 0:
  46. s->out |= value & 3;
  47. break;
  48. case 4:
  49. s->out &= ~value;
  50. break;
  51. default:
  52. hw_error("realview_i2c_write: Bad offset 0x%x\n", (int)offset);
  53. }
  54. bitbang_i2c_set(s->bitbang, BITBANG_I2C_SCL, (s->out & 1) != 0);
  55. s->in = bitbang_i2c_set(s->bitbang, BITBANG_I2C_SDA, (s->out & 2) != 0);
  56. }
  57. static const MemoryRegionOps realview_i2c_ops = {
  58. .read = realview_i2c_read,
  59. .write = realview_i2c_write,
  60. .endianness = DEVICE_NATIVE_ENDIAN,
  61. };
  62. static int realview_i2c_init(SysBusDevice *dev)
  63. {
  64. RealViewI2CState *s = FROM_SYSBUS(RealViewI2CState, dev);
  65. i2c_bus *bus;
  66. bus = i2c_init_bus(&dev->qdev, "i2c");
  67. s->bitbang = bitbang_i2c_init(bus);
  68. memory_region_init_io(&s->iomem, &realview_i2c_ops, s,
  69. "realview-i2c", 0x1000);
  70. sysbus_init_mmio_region(dev, &s->iomem);
  71. return 0;
  72. }
  73. static SysBusDeviceInfo realview_i2c_info = {
  74. .init = realview_i2c_init,
  75. .qdev.name = "realview_i2c",
  76. .qdev.size = sizeof(RealViewI2CState),
  77. };
  78. static void realview_register_devices(void)
  79. {
  80. sysbus_register_withprop(&realview_i2c_info);
  81. }
  82. /* Board init. */
  83. static struct arm_boot_info realview_binfo = {
  84. .smp_loader_start = SMP_BOOT_ADDR,
  85. };
  86. /* The following two lists must be consistent. */
  87. enum realview_board_type {
  88. BOARD_EB,
  89. BOARD_EB_MPCORE,
  90. BOARD_PB_A8,
  91. BOARD_PBX_A9,
  92. };
  93. static const int realview_board_id[] = {
  94. 0x33b,
  95. 0x33b,
  96. 0x769,
  97. 0x76d
  98. };
  99. static void realview_init(ram_addr_t ram_size,
  100. const char *boot_device,
  101. const char *kernel_filename, const char *kernel_cmdline,
  102. const char *initrd_filename, const char *cpu_model,
  103. enum realview_board_type board_type)
  104. {
  105. CPUState *env = NULL;
  106. MemoryRegion *sysmem = get_system_memory();
  107. MemoryRegion *ram_lo = g_new(MemoryRegion, 1);
  108. MemoryRegion *ram_hi = g_new(MemoryRegion, 1);
  109. MemoryRegion *ram_alias = g_new(MemoryRegion, 1);
  110. MemoryRegion *ram_hack = g_new(MemoryRegion, 1);
  111. DeviceState *dev, *sysctl, *gpio2, *pl041;
  112. SysBusDevice *busdev;
  113. qemu_irq *irqp;
  114. qemu_irq pic[64];
  115. qemu_irq mmc_irq[2];
  116. PCIBus *pci_bus;
  117. NICInfo *nd;
  118. i2c_bus *i2c;
  119. int n;
  120. int done_nic = 0;
  121. qemu_irq cpu_irq[4];
  122. int is_mpcore = 0;
  123. int is_pb = 0;
  124. uint32_t proc_id = 0;
  125. uint32_t sys_id;
  126. ram_addr_t low_ram_size;
  127. switch (board_type) {
  128. case BOARD_EB:
  129. break;
  130. case BOARD_EB_MPCORE:
  131. is_mpcore = 1;
  132. break;
  133. case BOARD_PB_A8:
  134. is_pb = 1;
  135. break;
  136. case BOARD_PBX_A9:
  137. is_mpcore = 1;
  138. is_pb = 1;
  139. break;
  140. }
  141. for (n = 0; n < smp_cpus; n++) {
  142. env = cpu_init(cpu_model);
  143. if (!env) {
  144. fprintf(stderr, "Unable to find CPU definition\n");
  145. exit(1);
  146. }
  147. irqp = arm_pic_init_cpu(env);
  148. cpu_irq[n] = irqp[ARM_PIC_CPU_IRQ];
  149. }
  150. if (arm_feature(env, ARM_FEATURE_V7)) {
  151. if (is_mpcore) {
  152. proc_id = 0x0c000000;
  153. } else {
  154. proc_id = 0x0e000000;
  155. }
  156. } else if (arm_feature(env, ARM_FEATURE_V6K)) {
  157. proc_id = 0x06000000;
  158. } else if (arm_feature(env, ARM_FEATURE_V6)) {
  159. proc_id = 0x04000000;
  160. } else {
  161. proc_id = 0x02000000;
  162. }
  163. if (is_pb && ram_size > 0x20000000) {
  164. /* Core tile RAM. */
  165. low_ram_size = ram_size - 0x20000000;
  166. ram_size = 0x20000000;
  167. memory_region_init_ram(ram_lo, NULL, "realview.lowmem", low_ram_size);
  168. memory_region_add_subregion(sysmem, 0x20000000, ram_lo);
  169. }
  170. memory_region_init_ram(ram_hi, NULL, "realview.highmem", ram_size);
  171. low_ram_size = ram_size;
  172. if (low_ram_size > 0x10000000)
  173. low_ram_size = 0x10000000;
  174. /* SDRAM at address zero. */
  175. memory_region_init_alias(ram_alias, "realview.alias",
  176. ram_hi, 0, low_ram_size);
  177. memory_region_add_subregion(sysmem, 0, ram_alias);
  178. if (is_pb) {
  179. /* And again at a high address. */
  180. memory_region_add_subregion(sysmem, 0x70000000, ram_hi);
  181. } else {
  182. ram_size = low_ram_size;
  183. }
  184. sys_id = is_pb ? 0x01780500 : 0xc1400400;
  185. sysctl = qdev_create(NULL, "realview_sysctl");
  186. qdev_prop_set_uint32(sysctl, "sys_id", sys_id);
  187. qdev_init_nofail(sysctl);
  188. qdev_prop_set_uint32(sysctl, "proc_id", proc_id);
  189. sysbus_mmio_map(sysbus_from_qdev(sysctl), 0, 0x10000000);
  190. if (is_mpcore) {
  191. dev = qdev_create(NULL, is_pb ? "a9mpcore_priv": "realview_mpcore");
  192. qdev_prop_set_uint32(dev, "num-cpu", smp_cpus);
  193. qdev_init_nofail(dev);
  194. busdev = sysbus_from_qdev(dev);
  195. if (is_pb) {
  196. realview_binfo.smp_priv_base = 0x1f000000;
  197. } else {
  198. realview_binfo.smp_priv_base = 0x10100000;
  199. }
  200. sysbus_mmio_map(busdev, 0, realview_binfo.smp_priv_base);
  201. for (n = 0; n < smp_cpus; n++) {
  202. sysbus_connect_irq(busdev, n, cpu_irq[n]);
  203. }
  204. } else {
  205. uint32_t gic_addr = is_pb ? 0x1e000000 : 0x10040000;
  206. /* For now just create the nIRQ GIC, and ignore the others. */
  207. dev = sysbus_create_simple("realview_gic", gic_addr, cpu_irq[0]);
  208. }
  209. for (n = 0; n < 64; n++) {
  210. pic[n] = qdev_get_gpio_in(dev, n);
  211. }
  212. pl041 = qdev_create(NULL, "pl041");
  213. qdev_prop_set_uint32(pl041, "nc_fifo_depth", 512);
  214. qdev_init_nofail(pl041);
  215. sysbus_mmio_map(sysbus_from_qdev(pl041), 0, 0x10004000);
  216. sysbus_connect_irq(sysbus_from_qdev(pl041), 0, pic[19]);
  217. sysbus_create_simple("pl050_keyboard", 0x10006000, pic[20]);
  218. sysbus_create_simple("pl050_mouse", 0x10007000, pic[21]);
  219. sysbus_create_simple("pl011", 0x10009000, pic[12]);
  220. sysbus_create_simple("pl011", 0x1000a000, pic[13]);
  221. sysbus_create_simple("pl011", 0x1000b000, pic[14]);
  222. sysbus_create_simple("pl011", 0x1000c000, pic[15]);
  223. /* DMA controller is optional, apparently. */
  224. sysbus_create_simple("pl081", 0x10030000, pic[24]);
  225. sysbus_create_simple("sp804", 0x10011000, pic[4]);
  226. sysbus_create_simple("sp804", 0x10012000, pic[5]);
  227. sysbus_create_simple("pl061", 0x10013000, pic[6]);
  228. sysbus_create_simple("pl061", 0x10014000, pic[7]);
  229. gpio2 = sysbus_create_simple("pl061", 0x10015000, pic[8]);
  230. sysbus_create_simple("pl111", 0x10020000, pic[23]);
  231. dev = sysbus_create_varargs("pl181", 0x10005000, pic[17], pic[18], NULL);
  232. /* Wire up MMC card detect and read-only signals. These have
  233. * to go to both the PL061 GPIO and the sysctl register.
  234. * Note that the PL181 orders these lines (readonly,inserted)
  235. * and the PL061 has them the other way about. Also the card
  236. * detect line is inverted.
  237. */
  238. mmc_irq[0] = qemu_irq_split(
  239. qdev_get_gpio_in(sysctl, ARM_SYSCTL_GPIO_MMC_WPROT),
  240. qdev_get_gpio_in(gpio2, 1));
  241. mmc_irq[1] = qemu_irq_split(
  242. qdev_get_gpio_in(sysctl, ARM_SYSCTL_GPIO_MMC_CARDIN),
  243. qemu_irq_invert(qdev_get_gpio_in(gpio2, 0)));
  244. qdev_connect_gpio_out(dev, 0, mmc_irq[0]);
  245. qdev_connect_gpio_out(dev, 1, mmc_irq[1]);
  246. sysbus_create_simple("pl031", 0x10017000, pic[10]);
  247. if (!is_pb) {
  248. dev = qdev_create(NULL, "realview_pci");
  249. busdev = sysbus_from_qdev(dev);
  250. qdev_init_nofail(dev);
  251. sysbus_mmio_map(busdev, 0, 0x61000000); /* PCI self-config */
  252. sysbus_mmio_map(busdev, 1, 0x62000000); /* PCI config */
  253. sysbus_mmio_map(busdev, 2, 0x63000000); /* PCI I/O */
  254. sysbus_connect_irq(busdev, 0, pic[48]);
  255. sysbus_connect_irq(busdev, 1, pic[49]);
  256. sysbus_connect_irq(busdev, 2, pic[50]);
  257. sysbus_connect_irq(busdev, 3, pic[51]);
  258. pci_bus = (PCIBus *)qdev_get_child_bus(dev, "pci");
  259. if (usb_enabled) {
  260. usb_ohci_init_pci(pci_bus, -1);
  261. }
  262. n = drive_get_max_bus(IF_SCSI);
  263. while (n >= 0) {
  264. pci_create_simple(pci_bus, -1, "lsi53c895a");
  265. n--;
  266. }
  267. }
  268. for(n = 0; n < nb_nics; n++) {
  269. nd = &nd_table[n];
  270. if (!done_nic && (!nd->model ||
  271. strcmp(nd->model, is_pb ? "lan9118" : "smc91c111") == 0)) {
  272. if (is_pb) {
  273. lan9118_init(nd, 0x4e000000, pic[28]);
  274. } else {
  275. smc91c111_init(nd, 0x4e000000, pic[28]);
  276. }
  277. done_nic = 1;
  278. } else {
  279. pci_nic_init_nofail(nd, "rtl8139", NULL);
  280. }
  281. }
  282. dev = sysbus_create_simple("realview_i2c", 0x10002000, NULL);
  283. i2c = (i2c_bus *)qdev_get_child_bus(dev, "i2c");
  284. i2c_create_slave(i2c, "ds1338", 0x68);
  285. /* Memory map for RealView Emulation Baseboard: */
  286. /* 0x10000000 System registers. */
  287. /* 0x10001000 System controller. */
  288. /* 0x10002000 Two-Wire Serial Bus. */
  289. /* 0x10003000 Reserved. */
  290. /* 0x10004000 AACI. */
  291. /* 0x10005000 MCI. */
  292. /* 0x10006000 KMI0. */
  293. /* 0x10007000 KMI1. */
  294. /* 0x10008000 Character LCD. (EB) */
  295. /* 0x10009000 UART0. */
  296. /* 0x1000a000 UART1. */
  297. /* 0x1000b000 UART2. */
  298. /* 0x1000c000 UART3. */
  299. /* 0x1000d000 SSPI. */
  300. /* 0x1000e000 SCI. */
  301. /* 0x1000f000 Reserved. */
  302. /* 0x10010000 Watchdog. */
  303. /* 0x10011000 Timer 0+1. */
  304. /* 0x10012000 Timer 2+3. */
  305. /* 0x10013000 GPIO 0. */
  306. /* 0x10014000 GPIO 1. */
  307. /* 0x10015000 GPIO 2. */
  308. /* 0x10002000 Two-Wire Serial Bus - DVI. (PB) */
  309. /* 0x10017000 RTC. */
  310. /* 0x10018000 DMC. */
  311. /* 0x10019000 PCI controller config. */
  312. /* 0x10020000 CLCD. */
  313. /* 0x10030000 DMA Controller. */
  314. /* 0x10040000 GIC1. (EB) */
  315. /* 0x10050000 GIC2. (EB) */
  316. /* 0x10060000 GIC3. (EB) */
  317. /* 0x10070000 GIC4. (EB) */
  318. /* 0x10080000 SMC. */
  319. /* 0x1e000000 GIC1. (PB) */
  320. /* 0x1e001000 GIC2. (PB) */
  321. /* 0x1e002000 GIC3. (PB) */
  322. /* 0x1e003000 GIC4. (PB) */
  323. /* 0x40000000 NOR flash. */
  324. /* 0x44000000 DoC flash. */
  325. /* 0x48000000 SRAM. */
  326. /* 0x4c000000 Configuration flash. */
  327. /* 0x4e000000 Ethernet. */
  328. /* 0x4f000000 USB. */
  329. /* 0x50000000 PISMO. */
  330. /* 0x54000000 PISMO. */
  331. /* 0x58000000 PISMO. */
  332. /* 0x5c000000 PISMO. */
  333. /* 0x60000000 PCI. */
  334. /* 0x61000000 PCI Self Config. */
  335. /* 0x62000000 PCI Config. */
  336. /* 0x63000000 PCI IO. */
  337. /* 0x64000000 PCI mem 0. */
  338. /* 0x68000000 PCI mem 1. */
  339. /* 0x6c000000 PCI mem 2. */
  340. /* ??? Hack to map an additional page of ram for the secondary CPU
  341. startup code. I guess this works on real hardware because the
  342. BootROM happens to be in ROM/flash or in memory that isn't clobbered
  343. until after Linux boots the secondary CPUs. */
  344. memory_region_init_ram(ram_hack, NULL, "realview.hack", 0x1000);
  345. memory_region_add_subregion(sysmem, SMP_BOOT_ADDR, ram_hack);
  346. realview_binfo.ram_size = ram_size;
  347. realview_binfo.kernel_filename = kernel_filename;
  348. realview_binfo.kernel_cmdline = kernel_cmdline;
  349. realview_binfo.initrd_filename = initrd_filename;
  350. realview_binfo.nb_cpus = smp_cpus;
  351. realview_binfo.board_id = realview_board_id[board_type];
  352. realview_binfo.loader_start = (board_type == BOARD_PB_A8 ? 0x70000000 : 0);
  353. arm_load_kernel(first_cpu, &realview_binfo);
  354. }
  355. static void realview_eb_init(ram_addr_t ram_size,
  356. const char *boot_device,
  357. const char *kernel_filename, const char *kernel_cmdline,
  358. const char *initrd_filename, const char *cpu_model)
  359. {
  360. if (!cpu_model) {
  361. cpu_model = "arm926";
  362. }
  363. realview_init(ram_size, boot_device, kernel_filename, kernel_cmdline,
  364. initrd_filename, cpu_model, BOARD_EB);
  365. }
  366. static void realview_eb_mpcore_init(ram_addr_t ram_size,
  367. const char *boot_device,
  368. const char *kernel_filename, const char *kernel_cmdline,
  369. const char *initrd_filename, const char *cpu_model)
  370. {
  371. if (!cpu_model) {
  372. cpu_model = "arm11mpcore";
  373. }
  374. realview_init(ram_size, boot_device, kernel_filename, kernel_cmdline,
  375. initrd_filename, cpu_model, BOARD_EB_MPCORE);
  376. }
  377. static void realview_pb_a8_init(ram_addr_t ram_size,
  378. const char *boot_device,
  379. const char *kernel_filename, const char *kernel_cmdline,
  380. const char *initrd_filename, const char *cpu_model)
  381. {
  382. if (!cpu_model) {
  383. cpu_model = "cortex-a8";
  384. }
  385. realview_init(ram_size, boot_device, kernel_filename, kernel_cmdline,
  386. initrd_filename, cpu_model, BOARD_PB_A8);
  387. }
  388. static void realview_pbx_a9_init(ram_addr_t ram_size,
  389. const char *boot_device,
  390. const char *kernel_filename, const char *kernel_cmdline,
  391. const char *initrd_filename, const char *cpu_model)
  392. {
  393. if (!cpu_model) {
  394. cpu_model = "cortex-a9";
  395. }
  396. realview_init(ram_size, boot_device, kernel_filename, kernel_cmdline,
  397. initrd_filename, cpu_model, BOARD_PBX_A9);
  398. }
  399. static QEMUMachine realview_eb_machine = {
  400. .name = "realview-eb",
  401. .desc = "ARM RealView Emulation Baseboard (ARM926EJ-S)",
  402. .init = realview_eb_init,
  403. .use_scsi = 1,
  404. };
  405. static QEMUMachine realview_eb_mpcore_machine = {
  406. .name = "realview-eb-mpcore",
  407. .desc = "ARM RealView Emulation Baseboard (ARM11MPCore)",
  408. .init = realview_eb_mpcore_init,
  409. .use_scsi = 1,
  410. .max_cpus = 4,
  411. };
  412. static QEMUMachine realview_pb_a8_machine = {
  413. .name = "realview-pb-a8",
  414. .desc = "ARM RealView Platform Baseboard for Cortex-A8",
  415. .init = realview_pb_a8_init,
  416. };
  417. static QEMUMachine realview_pbx_a9_machine = {
  418. .name = "realview-pbx-a9",
  419. .desc = "ARM RealView Platform Baseboard Explore for Cortex-A9",
  420. .init = realview_pbx_a9_init,
  421. .use_scsi = 1,
  422. .max_cpus = 4,
  423. };
  424. static void realview_machine_init(void)
  425. {
  426. qemu_register_machine(&realview_eb_machine);
  427. qemu_register_machine(&realview_eb_mpcore_machine);
  428. qemu_register_machine(&realview_pb_a8_machine);
  429. qemu_register_machine(&realview_pbx_a9_machine);
  430. }
  431. machine_init(realview_machine_init);
  432. device_init(realview_register_devices)