2
0

integratorcp.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /*
  2. * ARM Integrator CP System emulation.
  3. *
  4. * Copyright (c) 2005-2007 CodeSourcery.
  5. * Written by Paul Brook
  6. *
  7. * This code is licensed under the GPL
  8. */
  9. #include "sysbus.h"
  10. #include "primecell.h"
  11. #include "devices.h"
  12. #include "boards.h"
  13. #include "arm-misc.h"
  14. #include "net.h"
  15. #include "exec-memory.h"
  16. #include "sysemu.h"
  17. typedef struct {
  18. SysBusDevice busdev;
  19. uint32_t memsz;
  20. MemoryRegion flash;
  21. bool flash_mapped;
  22. uint32_t cm_osc;
  23. uint32_t cm_ctrl;
  24. uint32_t cm_lock;
  25. uint32_t cm_auxosc;
  26. uint32_t cm_sdram;
  27. uint32_t cm_init;
  28. uint32_t cm_flags;
  29. uint32_t cm_nvflags;
  30. uint32_t int_level;
  31. uint32_t irq_enabled;
  32. uint32_t fiq_enabled;
  33. } integratorcm_state;
  34. static uint8_t integrator_spd[128] = {
  35. 128, 8, 4, 11, 9, 1, 64, 0, 2, 0xa0, 0xa0, 0, 0, 8, 0, 1,
  36. 0xe, 4, 0x1c, 1, 2, 0x20, 0xc0, 0, 0, 0, 0, 0x30, 0x28, 0x30, 0x28, 0x40
  37. };
  38. static uint32_t integratorcm_read(void *opaque, target_phys_addr_t offset)
  39. {
  40. integratorcm_state *s = (integratorcm_state *)opaque;
  41. if (offset >= 0x100 && offset < 0x200) {
  42. /* CM_SPD */
  43. if (offset >= 0x180)
  44. return 0;
  45. return integrator_spd[offset >> 2];
  46. }
  47. switch (offset >> 2) {
  48. case 0: /* CM_ID */
  49. return 0x411a3001;
  50. case 1: /* CM_PROC */
  51. return 0;
  52. case 2: /* CM_OSC */
  53. return s->cm_osc;
  54. case 3: /* CM_CTRL */
  55. return s->cm_ctrl;
  56. case 4: /* CM_STAT */
  57. return 0x00100000;
  58. case 5: /* CM_LOCK */
  59. if (s->cm_lock == 0xa05f) {
  60. return 0x1a05f;
  61. } else {
  62. return s->cm_lock;
  63. }
  64. case 6: /* CM_LMBUSCNT */
  65. /* ??? High frequency timer. */
  66. hw_error("integratorcm_read: CM_LMBUSCNT");
  67. case 7: /* CM_AUXOSC */
  68. return s->cm_auxosc;
  69. case 8: /* CM_SDRAM */
  70. return s->cm_sdram;
  71. case 9: /* CM_INIT */
  72. return s->cm_init;
  73. case 10: /* CM_REFCT */
  74. /* ??? High frequency timer. */
  75. hw_error("integratorcm_read: CM_REFCT");
  76. case 12: /* CM_FLAGS */
  77. return s->cm_flags;
  78. case 14: /* CM_NVFLAGS */
  79. return s->cm_nvflags;
  80. case 16: /* CM_IRQ_STAT */
  81. return s->int_level & s->irq_enabled;
  82. case 17: /* CM_IRQ_RSTAT */
  83. return s->int_level;
  84. case 18: /* CM_IRQ_ENSET */
  85. return s->irq_enabled;
  86. case 20: /* CM_SOFT_INTSET */
  87. return s->int_level & 1;
  88. case 24: /* CM_FIQ_STAT */
  89. return s->int_level & s->fiq_enabled;
  90. case 25: /* CM_FIQ_RSTAT */
  91. return s->int_level;
  92. case 26: /* CM_FIQ_ENSET */
  93. return s->fiq_enabled;
  94. case 32: /* CM_VOLTAGE_CTL0 */
  95. case 33: /* CM_VOLTAGE_CTL1 */
  96. case 34: /* CM_VOLTAGE_CTL2 */
  97. case 35: /* CM_VOLTAGE_CTL3 */
  98. /* ??? Voltage control unimplemented. */
  99. return 0;
  100. default:
  101. hw_error("integratorcm_read: Unimplemented offset 0x%x\n",
  102. (int)offset);
  103. return 0;
  104. }
  105. }
  106. static void integratorcm_do_remap(integratorcm_state *s, int flash)
  107. {
  108. if (flash) {
  109. if (s->flash_mapped) {
  110. sysbus_del_memory(&s->busdev, &s->flash);
  111. s->flash_mapped = false;
  112. }
  113. } else {
  114. if (!s->flash_mapped) {
  115. sysbus_add_memory_overlap(&s->busdev, 0, &s->flash, 1);
  116. s->flash_mapped = true;
  117. }
  118. }
  119. //??? tlb_flush (cpu_single_env, 1);
  120. }
  121. static void integratorcm_set_ctrl(integratorcm_state *s, uint32_t value)
  122. {
  123. if (value & 8) {
  124. qemu_system_reset_request();
  125. }
  126. if ((s->cm_ctrl ^ value) & 4) {
  127. integratorcm_do_remap(s, (value & 4) == 0);
  128. }
  129. if ((s->cm_ctrl ^ value) & 1) {
  130. /* (value & 1) != 0 means the green "MISC LED" is lit.
  131. * We don't have any nice place to display LEDs. printf is a bad
  132. * idea because Linux uses the LED as a heartbeat and the output
  133. * will swamp anything else on the terminal.
  134. */
  135. }
  136. /* Note that the RESET bit [3] always reads as zero */
  137. s->cm_ctrl = (s->cm_ctrl & ~5) | (value & 5);
  138. }
  139. static void integratorcm_update(integratorcm_state *s)
  140. {
  141. /* ??? The CPU irq/fiq is raised when either the core module or base PIC
  142. are active. */
  143. if (s->int_level & (s->irq_enabled | s->fiq_enabled))
  144. hw_error("Core module interrupt\n");
  145. }
  146. static void integratorcm_write(void *opaque, target_phys_addr_t offset,
  147. uint32_t value)
  148. {
  149. integratorcm_state *s = (integratorcm_state *)opaque;
  150. switch (offset >> 2) {
  151. case 2: /* CM_OSC */
  152. if (s->cm_lock == 0xa05f)
  153. s->cm_osc = value;
  154. break;
  155. case 3: /* CM_CTRL */
  156. integratorcm_set_ctrl(s, value);
  157. break;
  158. case 5: /* CM_LOCK */
  159. s->cm_lock = value & 0xffff;
  160. break;
  161. case 7: /* CM_AUXOSC */
  162. if (s->cm_lock == 0xa05f)
  163. s->cm_auxosc = value;
  164. break;
  165. case 8: /* CM_SDRAM */
  166. s->cm_sdram = value;
  167. break;
  168. case 9: /* CM_INIT */
  169. /* ??? This can change the memory bus frequency. */
  170. s->cm_init = value;
  171. break;
  172. case 12: /* CM_FLAGSS */
  173. s->cm_flags |= value;
  174. break;
  175. case 13: /* CM_FLAGSC */
  176. s->cm_flags &= ~value;
  177. break;
  178. case 14: /* CM_NVFLAGSS */
  179. s->cm_nvflags |= value;
  180. break;
  181. case 15: /* CM_NVFLAGSS */
  182. s->cm_nvflags &= ~value;
  183. break;
  184. case 18: /* CM_IRQ_ENSET */
  185. s->irq_enabled |= value;
  186. integratorcm_update(s);
  187. break;
  188. case 19: /* CM_IRQ_ENCLR */
  189. s->irq_enabled &= ~value;
  190. integratorcm_update(s);
  191. break;
  192. case 20: /* CM_SOFT_INTSET */
  193. s->int_level |= (value & 1);
  194. integratorcm_update(s);
  195. break;
  196. case 21: /* CM_SOFT_INTCLR */
  197. s->int_level &= ~(value & 1);
  198. integratorcm_update(s);
  199. break;
  200. case 26: /* CM_FIQ_ENSET */
  201. s->fiq_enabled |= value;
  202. integratorcm_update(s);
  203. break;
  204. case 27: /* CM_FIQ_ENCLR */
  205. s->fiq_enabled &= ~value;
  206. integratorcm_update(s);
  207. break;
  208. case 32: /* CM_VOLTAGE_CTL0 */
  209. case 33: /* CM_VOLTAGE_CTL1 */
  210. case 34: /* CM_VOLTAGE_CTL2 */
  211. case 35: /* CM_VOLTAGE_CTL3 */
  212. /* ??? Voltage control unimplemented. */
  213. break;
  214. default:
  215. hw_error("integratorcm_write: Unimplemented offset 0x%x\n",
  216. (int)offset);
  217. break;
  218. }
  219. }
  220. /* Integrator/CM control registers. */
  221. static CPUReadMemoryFunc * const integratorcm_readfn[] = {
  222. integratorcm_read,
  223. integratorcm_read,
  224. integratorcm_read
  225. };
  226. static CPUWriteMemoryFunc * const integratorcm_writefn[] = {
  227. integratorcm_write,
  228. integratorcm_write,
  229. integratorcm_write
  230. };
  231. static int integratorcm_init(SysBusDevice *dev)
  232. {
  233. int iomemtype;
  234. integratorcm_state *s = FROM_SYSBUS(integratorcm_state, dev);
  235. s->cm_osc = 0x01000048;
  236. /* ??? What should the high bits of this value be? */
  237. s->cm_auxosc = 0x0007feff;
  238. s->cm_sdram = 0x00011122;
  239. if (s->memsz >= 256) {
  240. integrator_spd[31] = 64;
  241. s->cm_sdram |= 0x10;
  242. } else if (s->memsz >= 128) {
  243. integrator_spd[31] = 32;
  244. s->cm_sdram |= 0x0c;
  245. } else if (s->memsz >= 64) {
  246. integrator_spd[31] = 16;
  247. s->cm_sdram |= 0x08;
  248. } else if (s->memsz >= 32) {
  249. integrator_spd[31] = 4;
  250. s->cm_sdram |= 0x04;
  251. } else {
  252. integrator_spd[31] = 2;
  253. }
  254. memcpy(integrator_spd + 73, "QEMU-MEMORY", 11);
  255. s->cm_init = 0x00000112;
  256. memory_region_init_ram(&s->flash, NULL, "integrator.flash", 0x100000);
  257. s->flash_mapped = false;
  258. iomemtype = cpu_register_io_memory(integratorcm_readfn,
  259. integratorcm_writefn, s,
  260. DEVICE_NATIVE_ENDIAN);
  261. sysbus_init_mmio(dev, 0x00800000, iomemtype);
  262. integratorcm_do_remap(s, 1);
  263. /* ??? Save/restore. */
  264. return 0;
  265. }
  266. /* Integrator/CP hardware emulation. */
  267. /* Primary interrupt controller. */
  268. typedef struct icp_pic_state
  269. {
  270. SysBusDevice busdev;
  271. uint32_t level;
  272. uint32_t irq_enabled;
  273. uint32_t fiq_enabled;
  274. qemu_irq parent_irq;
  275. qemu_irq parent_fiq;
  276. } icp_pic_state;
  277. static void icp_pic_update(icp_pic_state *s)
  278. {
  279. uint32_t flags;
  280. flags = (s->level & s->irq_enabled);
  281. qemu_set_irq(s->parent_irq, flags != 0);
  282. flags = (s->level & s->fiq_enabled);
  283. qemu_set_irq(s->parent_fiq, flags != 0);
  284. }
  285. static void icp_pic_set_irq(void *opaque, int irq, int level)
  286. {
  287. icp_pic_state *s = (icp_pic_state *)opaque;
  288. if (level)
  289. s->level |= 1 << irq;
  290. else
  291. s->level &= ~(1 << irq);
  292. icp_pic_update(s);
  293. }
  294. static uint32_t icp_pic_read(void *opaque, target_phys_addr_t offset)
  295. {
  296. icp_pic_state *s = (icp_pic_state *)opaque;
  297. switch (offset >> 2) {
  298. case 0: /* IRQ_STATUS */
  299. return s->level & s->irq_enabled;
  300. case 1: /* IRQ_RAWSTAT */
  301. return s->level;
  302. case 2: /* IRQ_ENABLESET */
  303. return s->irq_enabled;
  304. case 4: /* INT_SOFTSET */
  305. return s->level & 1;
  306. case 8: /* FRQ_STATUS */
  307. return s->level & s->fiq_enabled;
  308. case 9: /* FRQ_RAWSTAT */
  309. return s->level;
  310. case 10: /* FRQ_ENABLESET */
  311. return s->fiq_enabled;
  312. case 3: /* IRQ_ENABLECLR */
  313. case 5: /* INT_SOFTCLR */
  314. case 11: /* FRQ_ENABLECLR */
  315. default:
  316. printf ("icp_pic_read: Bad register offset 0x%x\n", (int)offset);
  317. return 0;
  318. }
  319. }
  320. static void icp_pic_write(void *opaque, target_phys_addr_t offset,
  321. uint32_t value)
  322. {
  323. icp_pic_state *s = (icp_pic_state *)opaque;
  324. switch (offset >> 2) {
  325. case 2: /* IRQ_ENABLESET */
  326. s->irq_enabled |= value;
  327. break;
  328. case 3: /* IRQ_ENABLECLR */
  329. s->irq_enabled &= ~value;
  330. break;
  331. case 4: /* INT_SOFTSET */
  332. if (value & 1)
  333. icp_pic_set_irq(s, 0, 1);
  334. break;
  335. case 5: /* INT_SOFTCLR */
  336. if (value & 1)
  337. icp_pic_set_irq(s, 0, 0);
  338. break;
  339. case 10: /* FRQ_ENABLESET */
  340. s->fiq_enabled |= value;
  341. break;
  342. case 11: /* FRQ_ENABLECLR */
  343. s->fiq_enabled &= ~value;
  344. break;
  345. case 0: /* IRQ_STATUS */
  346. case 1: /* IRQ_RAWSTAT */
  347. case 8: /* FRQ_STATUS */
  348. case 9: /* FRQ_RAWSTAT */
  349. default:
  350. printf ("icp_pic_write: Bad register offset 0x%x\n", (int)offset);
  351. return;
  352. }
  353. icp_pic_update(s);
  354. }
  355. static CPUReadMemoryFunc * const icp_pic_readfn[] = {
  356. icp_pic_read,
  357. icp_pic_read,
  358. icp_pic_read
  359. };
  360. static CPUWriteMemoryFunc * const icp_pic_writefn[] = {
  361. icp_pic_write,
  362. icp_pic_write,
  363. icp_pic_write
  364. };
  365. static int icp_pic_init(SysBusDevice *dev)
  366. {
  367. icp_pic_state *s = FROM_SYSBUS(icp_pic_state, dev);
  368. int iomemtype;
  369. qdev_init_gpio_in(&dev->qdev, icp_pic_set_irq, 32);
  370. sysbus_init_irq(dev, &s->parent_irq);
  371. sysbus_init_irq(dev, &s->parent_fiq);
  372. iomemtype = cpu_register_io_memory(icp_pic_readfn,
  373. icp_pic_writefn, s,
  374. DEVICE_NATIVE_ENDIAN);
  375. sysbus_init_mmio(dev, 0x00800000, iomemtype);
  376. return 0;
  377. }
  378. /* CP control registers. */
  379. static uint32_t icp_control_read(void *opaque, target_phys_addr_t offset)
  380. {
  381. switch (offset >> 2) {
  382. case 0: /* CP_IDFIELD */
  383. return 0x41034003;
  384. case 1: /* CP_FLASHPROG */
  385. return 0;
  386. case 2: /* CP_INTREG */
  387. return 0;
  388. case 3: /* CP_DECODE */
  389. return 0x11;
  390. default:
  391. hw_error("icp_control_read: Bad offset %x\n", (int)offset);
  392. return 0;
  393. }
  394. }
  395. static void icp_control_write(void *opaque, target_phys_addr_t offset,
  396. uint32_t value)
  397. {
  398. switch (offset >> 2) {
  399. case 1: /* CP_FLASHPROG */
  400. case 2: /* CP_INTREG */
  401. case 3: /* CP_DECODE */
  402. /* Nothing interesting implemented yet. */
  403. break;
  404. default:
  405. hw_error("icp_control_write: Bad offset %x\n", (int)offset);
  406. }
  407. }
  408. static CPUReadMemoryFunc * const icp_control_readfn[] = {
  409. icp_control_read,
  410. icp_control_read,
  411. icp_control_read
  412. };
  413. static CPUWriteMemoryFunc * const icp_control_writefn[] = {
  414. icp_control_write,
  415. icp_control_write,
  416. icp_control_write
  417. };
  418. static void icp_control_init(uint32_t base)
  419. {
  420. int iomemtype;
  421. iomemtype = cpu_register_io_memory(icp_control_readfn,
  422. icp_control_writefn, NULL,
  423. DEVICE_NATIVE_ENDIAN);
  424. cpu_register_physical_memory(base, 0x00800000, iomemtype);
  425. /* ??? Save/restore. */
  426. }
  427. /* Board init. */
  428. static struct arm_boot_info integrator_binfo = {
  429. .loader_start = 0x0,
  430. .board_id = 0x113,
  431. };
  432. static void integratorcp_init(ram_addr_t ram_size,
  433. const char *boot_device,
  434. const char *kernel_filename, const char *kernel_cmdline,
  435. const char *initrd_filename, const char *cpu_model)
  436. {
  437. CPUState *env;
  438. MemoryRegion *address_space_mem = get_system_memory();
  439. MemoryRegion *ram = g_new(MemoryRegion, 1);
  440. MemoryRegion *ram_alias = g_new(MemoryRegion, 1);
  441. qemu_irq pic[32];
  442. qemu_irq *cpu_pic;
  443. DeviceState *dev;
  444. int i;
  445. if (!cpu_model)
  446. cpu_model = "arm926";
  447. env = cpu_init(cpu_model);
  448. if (!env) {
  449. fprintf(stderr, "Unable to find CPU definition\n");
  450. exit(1);
  451. }
  452. memory_region_init_ram(ram, NULL, "integrator.ram", ram_size);
  453. /* ??? On a real system the first 1Mb is mapped as SSRAM or boot flash. */
  454. /* ??? RAM should repeat to fill physical memory space. */
  455. /* SDRAM at address zero*/
  456. memory_region_add_subregion(address_space_mem, 0, ram);
  457. /* And again at address 0x80000000 */
  458. memory_region_init_alias(ram_alias, "ram.alias", ram, 0, ram_size);
  459. memory_region_add_subregion(address_space_mem, 0x80000000, ram_alias);
  460. dev = qdev_create(NULL, "integrator_core");
  461. qdev_prop_set_uint32(dev, "memsz", ram_size >> 20);
  462. qdev_init_nofail(dev);
  463. sysbus_mmio_map((SysBusDevice *)dev, 0, 0x10000000);
  464. cpu_pic = arm_pic_init_cpu(env);
  465. dev = sysbus_create_varargs("integrator_pic", 0x14000000,
  466. cpu_pic[ARM_PIC_CPU_IRQ],
  467. cpu_pic[ARM_PIC_CPU_FIQ], NULL);
  468. for (i = 0; i < 32; i++) {
  469. pic[i] = qdev_get_gpio_in(dev, i);
  470. }
  471. sysbus_create_simple("integrator_pic", 0xca000000, pic[26]);
  472. sysbus_create_varargs("integrator_pit", 0x13000000,
  473. pic[5], pic[6], pic[7], NULL);
  474. sysbus_create_simple("pl031", 0x15000000, pic[8]);
  475. sysbus_create_simple("pl011", 0x16000000, pic[1]);
  476. sysbus_create_simple("pl011", 0x17000000, pic[2]);
  477. icp_control_init(0xcb000000);
  478. sysbus_create_simple("pl050_keyboard", 0x18000000, pic[3]);
  479. sysbus_create_simple("pl050_mouse", 0x19000000, pic[4]);
  480. sysbus_create_varargs("pl181", 0x1c000000, pic[23], pic[24], NULL);
  481. if (nd_table[0].vlan)
  482. smc91c111_init(&nd_table[0], 0xc8000000, pic[27]);
  483. sysbus_create_simple("pl110", 0xc0000000, pic[22]);
  484. integrator_binfo.ram_size = ram_size;
  485. integrator_binfo.kernel_filename = kernel_filename;
  486. integrator_binfo.kernel_cmdline = kernel_cmdline;
  487. integrator_binfo.initrd_filename = initrd_filename;
  488. arm_load_kernel(env, &integrator_binfo);
  489. }
  490. static QEMUMachine integratorcp_machine = {
  491. .name = "integratorcp",
  492. .desc = "ARM Integrator/CP (ARM926EJ-S)",
  493. .init = integratorcp_init,
  494. .is_default = 1,
  495. };
  496. static void integratorcp_machine_init(void)
  497. {
  498. qemu_register_machine(&integratorcp_machine);
  499. }
  500. machine_init(integratorcp_machine_init);
  501. static SysBusDeviceInfo core_info = {
  502. .init = integratorcm_init,
  503. .qdev.name = "integrator_core",
  504. .qdev.size = sizeof(integratorcm_state),
  505. .qdev.props = (Property[]) {
  506. DEFINE_PROP_UINT32("memsz", integratorcm_state, memsz, 0),
  507. DEFINE_PROP_END_OF_LIST(),
  508. }
  509. };
  510. static void integratorcp_register_devices(void)
  511. {
  512. sysbus_register_dev("integrator_pic", sizeof(icp_pic_state), icp_pic_init);
  513. sysbus_register_withprop(&core_info);
  514. }
  515. device_init(integratorcp_register_devices)