integratorcp.c 14 KB

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