npcm7xx.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. /*
  2. * Nuvoton NPCM7xx SoC family.
  3. *
  4. * Copyright 2020 Google LLC
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * for more details.
  15. */
  16. #include "qemu/osdep.h"
  17. #include "hw/arm/boot.h"
  18. #include "hw/arm/npcm7xx.h"
  19. #include "hw/char/serial-mm.h"
  20. #include "hw/loader.h"
  21. #include "hw/misc/unimp.h"
  22. #include "hw/qdev-clock.h"
  23. #include "hw/qdev-properties.h"
  24. #include "qapi/error.h"
  25. #include "qemu/bswap.h"
  26. #include "qemu/units.h"
  27. #include "system/system.h"
  28. #include "target/arm/cpu-qom.h"
  29. /*
  30. * This covers the whole MMIO space. We'll use this to catch any MMIO accesses
  31. * that aren't handled by any device.
  32. */
  33. #define NPCM7XX_MMIO_BA (0x80000000)
  34. #define NPCM7XX_MMIO_SZ (0x7ffd0000)
  35. /* OTP key storage and fuse strap array */
  36. #define NPCM7XX_OTP1_BA (0xf0189000)
  37. #define NPCM7XX_OTP2_BA (0xf018a000)
  38. /* Core system modules. */
  39. #define NPCM7XX_L2C_BA (0xf03fc000)
  40. #define NPCM7XX_CPUP_BA (0xf03fe000)
  41. #define NPCM7XX_GCR_BA (0xf0800000)
  42. #define NPCM7XX_CLK_BA (0xf0801000)
  43. #define NPCM7XX_MC_BA (0xf0824000)
  44. #define NPCM7XX_RNG_BA (0xf000b000)
  45. /* USB Host modules */
  46. #define NPCM7XX_EHCI_BA (0xf0806000)
  47. #define NPCM7XX_OHCI_BA (0xf0807000)
  48. /* ADC Module */
  49. #define NPCM7XX_ADC_BA (0xf000c000)
  50. /* Internal AHB SRAM */
  51. #define NPCM7XX_RAM3_BA (0xc0008000)
  52. #define NPCM7XX_RAM3_SZ (4 * KiB)
  53. /* Memory blocks at the end of the address space */
  54. #define NPCM7XX_RAM2_BA (0xfffd0000)
  55. #define NPCM7XX_RAM2_SZ (128 * KiB)
  56. #define NPCM7XX_ROM_BA (0xffff0000)
  57. #define NPCM7XX_ROM_SZ (64 * KiB)
  58. /* SDHCI Modules */
  59. #define NPCM7XX_MMC_BA (0xf0842000)
  60. /* Clock configuration values to be fixed up when bypassing bootloader */
  61. /* Run PLL1 at 1600 MHz */
  62. #define NPCM7XX_PLLCON1_FIXUP_VAL (0x00402101)
  63. /* Run the CPU from PLL1 and UART from PLL2 */
  64. #define NPCM7XX_CLKSEL_FIXUP_VAL (0x004aaba9)
  65. /*
  66. * Interrupt lines going into the GIC. This does not include internal Cortex-A9
  67. * interrupts.
  68. */
  69. enum NPCM7xxInterrupt {
  70. NPCM7XX_ADC_IRQ = 0,
  71. NPCM7XX_UART0_IRQ = 2,
  72. NPCM7XX_UART1_IRQ,
  73. NPCM7XX_UART2_IRQ,
  74. NPCM7XX_UART3_IRQ,
  75. NPCM7XX_GMAC1_IRQ = 14,
  76. NPCM7XX_EMC1RX_IRQ = 15,
  77. NPCM7XX_EMC1TX_IRQ,
  78. NPCM7XX_GMAC2_IRQ,
  79. NPCM7XX_MMC_IRQ = 26,
  80. NPCM7XX_PSPI2_IRQ = 28,
  81. NPCM7XX_PSPI1_IRQ = 31,
  82. NPCM7XX_TIMER0_IRQ = 32, /* Timer Module 0 */
  83. NPCM7XX_TIMER1_IRQ,
  84. NPCM7XX_TIMER2_IRQ,
  85. NPCM7XX_TIMER3_IRQ,
  86. NPCM7XX_TIMER4_IRQ,
  87. NPCM7XX_TIMER5_IRQ, /* Timer Module 1 */
  88. NPCM7XX_TIMER6_IRQ,
  89. NPCM7XX_TIMER7_IRQ,
  90. NPCM7XX_TIMER8_IRQ,
  91. NPCM7XX_TIMER9_IRQ,
  92. NPCM7XX_TIMER10_IRQ, /* Timer Module 2 */
  93. NPCM7XX_TIMER11_IRQ,
  94. NPCM7XX_TIMER12_IRQ,
  95. NPCM7XX_TIMER13_IRQ,
  96. NPCM7XX_TIMER14_IRQ,
  97. NPCM7XX_WDG0_IRQ = 47, /* Timer Module 0 Watchdog */
  98. NPCM7XX_WDG1_IRQ, /* Timer Module 1 Watchdog */
  99. NPCM7XX_WDG2_IRQ, /* Timer Module 2 Watchdog */
  100. NPCM7XX_EHCI_IRQ = 61,
  101. NPCM7XX_OHCI_IRQ = 62,
  102. NPCM7XX_SMBUS0_IRQ = 64,
  103. NPCM7XX_SMBUS1_IRQ,
  104. NPCM7XX_SMBUS2_IRQ,
  105. NPCM7XX_SMBUS3_IRQ,
  106. NPCM7XX_SMBUS4_IRQ,
  107. NPCM7XX_SMBUS5_IRQ,
  108. NPCM7XX_SMBUS6_IRQ,
  109. NPCM7XX_SMBUS7_IRQ,
  110. NPCM7XX_SMBUS8_IRQ,
  111. NPCM7XX_SMBUS9_IRQ,
  112. NPCM7XX_SMBUS10_IRQ,
  113. NPCM7XX_SMBUS11_IRQ,
  114. NPCM7XX_SMBUS12_IRQ,
  115. NPCM7XX_SMBUS13_IRQ,
  116. NPCM7XX_SMBUS14_IRQ,
  117. NPCM7XX_SMBUS15_IRQ,
  118. NPCM7XX_PWM0_IRQ = 93, /* PWM module 0 */
  119. NPCM7XX_PWM1_IRQ, /* PWM module 1 */
  120. NPCM7XX_MFT0_IRQ = 96, /* MFT module 0 */
  121. NPCM7XX_MFT1_IRQ, /* MFT module 1 */
  122. NPCM7XX_MFT2_IRQ, /* MFT module 2 */
  123. NPCM7XX_MFT3_IRQ, /* MFT module 3 */
  124. NPCM7XX_MFT4_IRQ, /* MFT module 4 */
  125. NPCM7XX_MFT5_IRQ, /* MFT module 5 */
  126. NPCM7XX_MFT6_IRQ, /* MFT module 6 */
  127. NPCM7XX_MFT7_IRQ, /* MFT module 7 */
  128. NPCM7XX_EMC2RX_IRQ = 114,
  129. NPCM7XX_EMC2TX_IRQ,
  130. NPCM7XX_GPIO0_IRQ = 116,
  131. NPCM7XX_GPIO1_IRQ,
  132. NPCM7XX_GPIO2_IRQ,
  133. NPCM7XX_GPIO3_IRQ,
  134. NPCM7XX_GPIO4_IRQ,
  135. NPCM7XX_GPIO5_IRQ,
  136. NPCM7XX_GPIO6_IRQ,
  137. NPCM7XX_GPIO7_IRQ,
  138. };
  139. /* Total number of GIC interrupts, including internal Cortex-A9 interrupts. */
  140. #define NPCM7XX_NUM_IRQ (160)
  141. /* Register base address for each Timer Module */
  142. static const hwaddr npcm7xx_tim_addr[] = {
  143. 0xf0008000,
  144. 0xf0009000,
  145. 0xf000a000,
  146. };
  147. /* Register base address for each 16550 UART */
  148. static const hwaddr npcm7xx_uart_addr[] = {
  149. 0xf0001000,
  150. 0xf0002000,
  151. 0xf0003000,
  152. 0xf0004000,
  153. };
  154. /* Direct memory-mapped access to SPI0 CS0-1. */
  155. static const hwaddr npcm7xx_fiu0_flash_addr[] = {
  156. 0x80000000, /* CS0 */
  157. 0x88000000, /* CS1 */
  158. };
  159. /* Direct memory-mapped access to SPI3 CS0-3. */
  160. static const hwaddr npcm7xx_fiu3_flash_addr[] = {
  161. 0xa0000000, /* CS0 */
  162. 0xa8000000, /* CS1 */
  163. 0xb0000000, /* CS2 */
  164. 0xb8000000, /* CS3 */
  165. };
  166. /* Register base address for each PWM Module */
  167. static const hwaddr npcm7xx_pwm_addr[] = {
  168. 0xf0103000,
  169. 0xf0104000,
  170. };
  171. /* Register base address for each MFT Module */
  172. static const hwaddr npcm7xx_mft_addr[] = {
  173. 0xf0180000,
  174. 0xf0181000,
  175. 0xf0182000,
  176. 0xf0183000,
  177. 0xf0184000,
  178. 0xf0185000,
  179. 0xf0186000,
  180. 0xf0187000,
  181. };
  182. /* Direct memory-mapped access to each SMBus Module. */
  183. static const hwaddr npcm7xx_smbus_addr[] = {
  184. 0xf0080000,
  185. 0xf0081000,
  186. 0xf0082000,
  187. 0xf0083000,
  188. 0xf0084000,
  189. 0xf0085000,
  190. 0xf0086000,
  191. 0xf0087000,
  192. 0xf0088000,
  193. 0xf0089000,
  194. 0xf008a000,
  195. 0xf008b000,
  196. 0xf008c000,
  197. 0xf008d000,
  198. 0xf008e000,
  199. 0xf008f000,
  200. };
  201. /* Register base address for each EMC Module */
  202. static const hwaddr npcm7xx_emc_addr[] = {
  203. 0xf0825000,
  204. 0xf0826000,
  205. };
  206. /* Register base address for each PSPI Module */
  207. static const hwaddr npcm7xx_pspi_addr[] = {
  208. 0xf0200000,
  209. 0xf0201000,
  210. };
  211. /* Register base address for each GMAC Module */
  212. static const hwaddr npcm7xx_gmac_addr[] = {
  213. 0xf0802000,
  214. 0xf0804000,
  215. };
  216. static const struct {
  217. hwaddr regs_addr;
  218. uint32_t unconnected_pins;
  219. uint32_t reset_pu;
  220. uint32_t reset_pd;
  221. uint32_t reset_osrc;
  222. uint32_t reset_odsc;
  223. } npcm7xx_gpio[] = {
  224. {
  225. .regs_addr = 0xf0010000,
  226. .reset_pu = 0xff03ffff,
  227. .reset_pd = 0x00fc0000,
  228. }, {
  229. .regs_addr = 0xf0011000,
  230. .unconnected_pins = 0x0000001e,
  231. .reset_pu = 0xfefffe07,
  232. .reset_pd = 0x010001e0,
  233. }, {
  234. .regs_addr = 0xf0012000,
  235. .reset_pu = 0x780fffff,
  236. .reset_pd = 0x07f00000,
  237. .reset_odsc = 0x00700000,
  238. }, {
  239. .regs_addr = 0xf0013000,
  240. .reset_pu = 0x00fc0000,
  241. .reset_pd = 0xff000000,
  242. }, {
  243. .regs_addr = 0xf0014000,
  244. .reset_pu = 0xffffffff,
  245. }, {
  246. .regs_addr = 0xf0015000,
  247. .reset_pu = 0xbf83f801,
  248. .reset_pd = 0x007c0000,
  249. .reset_osrc = 0x000000f1,
  250. .reset_odsc = 0x3f9f80f1,
  251. }, {
  252. .regs_addr = 0xf0016000,
  253. .reset_pu = 0xfc00f801,
  254. .reset_pd = 0x000007fe,
  255. .reset_odsc = 0x00000800,
  256. }, {
  257. .regs_addr = 0xf0017000,
  258. .unconnected_pins = 0xffffff00,
  259. .reset_pu = 0x0000007f,
  260. .reset_osrc = 0x0000007f,
  261. .reset_odsc = 0x0000007f,
  262. },
  263. };
  264. static const struct {
  265. const char *name;
  266. hwaddr regs_addr;
  267. int cs_count;
  268. const hwaddr *flash_addr;
  269. size_t flash_size;
  270. } npcm7xx_fiu[] = {
  271. {
  272. .name = "fiu0",
  273. .regs_addr = 0xfb000000,
  274. .cs_count = ARRAY_SIZE(npcm7xx_fiu0_flash_addr),
  275. .flash_addr = npcm7xx_fiu0_flash_addr,
  276. .flash_size = 128 * MiB,
  277. }, {
  278. .name = "fiu3",
  279. .regs_addr = 0xc0000000,
  280. .cs_count = ARRAY_SIZE(npcm7xx_fiu3_flash_addr),
  281. .flash_addr = npcm7xx_fiu3_flash_addr,
  282. .flash_size = 128 * MiB,
  283. },
  284. };
  285. static void npcm7xx_write_board_setup(ARMCPU *cpu,
  286. const struct arm_boot_info *info)
  287. {
  288. uint32_t board_setup[] = {
  289. 0xe59f0010, /* ldr r0, clk_base_addr */
  290. 0xe59f1010, /* ldr r1, pllcon1_value */
  291. 0xe5801010, /* str r1, [r0, #16] */
  292. 0xe59f100c, /* ldr r1, clksel_value */
  293. 0xe5801004, /* str r1, [r0, #4] */
  294. 0xe12fff1e, /* bx lr */
  295. NPCM7XX_CLK_BA,
  296. NPCM7XX_PLLCON1_FIXUP_VAL,
  297. NPCM7XX_CLKSEL_FIXUP_VAL,
  298. };
  299. int i;
  300. for (i = 0; i < ARRAY_SIZE(board_setup); i++) {
  301. board_setup[i] = tswap32(board_setup[i]);
  302. }
  303. rom_add_blob_fixed("board-setup", board_setup, sizeof(board_setup),
  304. info->board_setup_addr);
  305. }
  306. static void npcm7xx_write_secondary_boot(ARMCPU *cpu,
  307. const struct arm_boot_info *info)
  308. {
  309. /*
  310. * The default smpboot stub halts the secondary CPU with a 'wfi'
  311. * instruction, but the arch/arm/mach-npcm/platsmp.c in the Linux kernel
  312. * does not send an IPI to wake it up, so the second CPU fails to boot. So
  313. * we need to provide our own smpboot stub that can not use 'wfi', it has
  314. * to spin the secondary CPU until the first CPU writes to the SCRPAD reg.
  315. */
  316. uint32_t smpboot[] = {
  317. 0xe59f2018, /* ldr r2, bootreg_addr */
  318. 0xe3a00000, /* mov r0, #0 */
  319. 0xe5820000, /* str r0, [r2] */
  320. 0xe320f002, /* wfe */
  321. 0xe5921000, /* ldr r1, [r2] */
  322. 0xe1110001, /* tst r1, r1 */
  323. 0x0afffffb, /* beq <wfe> */
  324. 0xe12fff11, /* bx r1 */
  325. NPCM7XX_SMP_BOOTREG_ADDR,
  326. };
  327. int i;
  328. for (i = 0; i < ARRAY_SIZE(smpboot); i++) {
  329. smpboot[i] = tswap32(smpboot[i]);
  330. }
  331. rom_add_blob_fixed("smpboot", smpboot, sizeof(smpboot),
  332. NPCM7XX_SMP_LOADER_START);
  333. }
  334. static struct arm_boot_info npcm7xx_binfo = {
  335. .loader_start = NPCM7XX_LOADER_START,
  336. .smp_loader_start = NPCM7XX_SMP_LOADER_START,
  337. .smp_bootreg_addr = NPCM7XX_SMP_BOOTREG_ADDR,
  338. .gic_cpu_if_addr = NPCM7XX_GIC_CPU_IF_ADDR,
  339. .write_secondary_boot = npcm7xx_write_secondary_boot,
  340. .board_id = -1,
  341. .board_setup_addr = NPCM7XX_BOARD_SETUP_ADDR,
  342. .write_board_setup = npcm7xx_write_board_setup,
  343. };
  344. void npcm7xx_load_kernel(MachineState *machine, NPCM7xxState *soc)
  345. {
  346. npcm7xx_binfo.ram_size = machine->ram_size;
  347. arm_load_kernel(&soc->cpu[0], machine, &npcm7xx_binfo);
  348. }
  349. static void npcm7xx_init_fuses(NPCM7xxState *s)
  350. {
  351. NPCM7xxClass *nc = NPCM7XX_GET_CLASS(s);
  352. uint32_t value;
  353. /*
  354. * The initial mask of disabled modules indicates the chip derivative (e.g.
  355. * NPCM750 or NPCM730).
  356. */
  357. value = cpu_to_le32(nc->disabled_modules);
  358. npcm7xx_otp_array_write(&s->fuse_array, &value, NPCM7XX_FUSE_DERIVATIVE,
  359. sizeof(value));
  360. }
  361. static void npcm7xx_write_adc_calibration(NPCM7xxState *s)
  362. {
  363. /* Both ADC and the fuse array must have realized. */
  364. QEMU_BUILD_BUG_ON(sizeof(s->adc.calibration_r_values) != 4);
  365. npcm7xx_otp_array_write(&s->fuse_array, s->adc.calibration_r_values,
  366. NPCM7XX_FUSE_ADC_CALIB, sizeof(s->adc.calibration_r_values));
  367. }
  368. static qemu_irq npcm7xx_irq(NPCM7xxState *s, int n)
  369. {
  370. return qdev_get_gpio_in(DEVICE(&s->a9mpcore), n);
  371. }
  372. static void npcm7xx_init(Object *obj)
  373. {
  374. NPCM7xxState *s = NPCM7XX(obj);
  375. int i;
  376. for (i = 0; i < NPCM7XX_MAX_NUM_CPUS; i++) {
  377. object_initialize_child(obj, "cpu[*]", &s->cpu[i],
  378. ARM_CPU_TYPE_NAME("cortex-a9"));
  379. }
  380. object_initialize_child(obj, "a9mpcore", &s->a9mpcore, TYPE_A9MPCORE_PRIV);
  381. object_initialize_child(obj, "gcr", &s->gcr, TYPE_NPCM7XX_GCR);
  382. object_property_add_alias(obj, "power-on-straps", OBJECT(&s->gcr),
  383. "power-on-straps");
  384. object_initialize_child(obj, "clk", &s->clk, TYPE_NPCM7XX_CLK);
  385. object_initialize_child(obj, "otp1", &s->key_storage,
  386. TYPE_NPCM7XX_KEY_STORAGE);
  387. object_initialize_child(obj, "otp2", &s->fuse_array,
  388. TYPE_NPCM7XX_FUSE_ARRAY);
  389. object_initialize_child(obj, "mc", &s->mc, TYPE_NPCM7XX_MC);
  390. object_initialize_child(obj, "rng", &s->rng, TYPE_NPCM7XX_RNG);
  391. object_initialize_child(obj, "adc", &s->adc, TYPE_NPCM7XX_ADC);
  392. for (i = 0; i < ARRAY_SIZE(s->tim); i++) {
  393. object_initialize_child(obj, "tim[*]", &s->tim[i], TYPE_NPCM7XX_TIMER);
  394. }
  395. for (i = 0; i < ARRAY_SIZE(s->gpio); i++) {
  396. object_initialize_child(obj, "gpio[*]", &s->gpio[i], TYPE_NPCM7XX_GPIO);
  397. }
  398. for (i = 0; i < ARRAY_SIZE(s->smbus); i++) {
  399. object_initialize_child(obj, "smbus[*]", &s->smbus[i],
  400. TYPE_NPCM7XX_SMBUS);
  401. }
  402. object_initialize_child(obj, "ehci", &s->ehci, TYPE_NPCM7XX_EHCI);
  403. object_initialize_child(obj, "ohci", &s->ohci, TYPE_SYSBUS_OHCI);
  404. QEMU_BUILD_BUG_ON(ARRAY_SIZE(npcm7xx_fiu) != ARRAY_SIZE(s->fiu));
  405. for (i = 0; i < ARRAY_SIZE(s->fiu); i++) {
  406. object_initialize_child(obj, npcm7xx_fiu[i].name, &s->fiu[i],
  407. TYPE_NPCM7XX_FIU);
  408. }
  409. for (i = 0; i < ARRAY_SIZE(s->pwm); i++) {
  410. object_initialize_child(obj, "pwm[*]", &s->pwm[i], TYPE_NPCM7XX_PWM);
  411. }
  412. for (i = 0; i < ARRAY_SIZE(s->mft); i++) {
  413. object_initialize_child(obj, "mft[*]", &s->mft[i], TYPE_NPCM7XX_MFT);
  414. }
  415. for (i = 0; i < ARRAY_SIZE(s->emc); i++) {
  416. object_initialize_child(obj, "emc[*]", &s->emc[i], TYPE_NPCM7XX_EMC);
  417. }
  418. for (i = 0; i < ARRAY_SIZE(s->pspi); i++) {
  419. object_initialize_child(obj, "pspi[*]", &s->pspi[i], TYPE_NPCM_PSPI);
  420. }
  421. for (i = 0; i < ARRAY_SIZE(s->gmac); i++) {
  422. object_initialize_child(obj, "gmac[*]", &s->gmac[i], TYPE_NPCM_GMAC);
  423. }
  424. object_initialize_child(obj, "mmc", &s->mmc, TYPE_NPCM7XX_SDHCI);
  425. }
  426. static void npcm7xx_realize(DeviceState *dev, Error **errp)
  427. {
  428. NPCM7xxState *s = NPCM7XX(dev);
  429. NPCM7xxClass *nc = NPCM7XX_GET_CLASS(s);
  430. int i;
  431. if (memory_region_size(s->dram) > NPCM7XX_DRAM_SZ) {
  432. error_setg(errp, "%s: NPCM7xx cannot address more than %" PRIu64
  433. " MiB of DRAM", __func__, NPCM7XX_DRAM_SZ / MiB);
  434. return;
  435. }
  436. /* CPUs */
  437. for (i = 0; i < nc->num_cpus; i++) {
  438. object_property_set_int(OBJECT(&s->cpu[i]), "reset-cbar",
  439. NPCM7XX_GIC_CPU_IF_ADDR, &error_abort);
  440. object_property_set_bool(OBJECT(&s->cpu[i]), "reset-hivecs", true,
  441. &error_abort);
  442. /* Disable security extensions. */
  443. object_property_set_bool(OBJECT(&s->cpu[i]), "has_el3", false,
  444. &error_abort);
  445. if (!qdev_realize(DEVICE(&s->cpu[i]), NULL, errp)) {
  446. return;
  447. }
  448. }
  449. /* A9MPCORE peripherals. Can only fail if we pass bad parameters here. */
  450. object_property_set_int(OBJECT(&s->a9mpcore), "num-cpu", nc->num_cpus,
  451. &error_abort);
  452. object_property_set_int(OBJECT(&s->a9mpcore), "num-irq", NPCM7XX_NUM_IRQ,
  453. &error_abort);
  454. sysbus_realize(SYS_BUS_DEVICE(&s->a9mpcore), &error_abort);
  455. sysbus_mmio_map(SYS_BUS_DEVICE(&s->a9mpcore), 0, NPCM7XX_CPUP_BA);
  456. for (i = 0; i < nc->num_cpus; i++) {
  457. sysbus_connect_irq(SYS_BUS_DEVICE(&s->a9mpcore), i,
  458. qdev_get_gpio_in(DEVICE(&s->cpu[i]), ARM_CPU_IRQ));
  459. sysbus_connect_irq(SYS_BUS_DEVICE(&s->a9mpcore), i + nc->num_cpus,
  460. qdev_get_gpio_in(DEVICE(&s->cpu[i]), ARM_CPU_FIQ));
  461. }
  462. /* L2 cache controller */
  463. sysbus_create_simple("l2x0", NPCM7XX_L2C_BA, NULL);
  464. /* System Global Control Registers (GCR). Can fail due to user input. */
  465. object_property_set_int(OBJECT(&s->gcr), "disabled-modules",
  466. nc->disabled_modules, &error_abort);
  467. object_property_add_const_link(OBJECT(&s->gcr), "dram-mr", OBJECT(s->dram));
  468. if (!sysbus_realize(SYS_BUS_DEVICE(&s->gcr), errp)) {
  469. return;
  470. }
  471. sysbus_mmio_map(SYS_BUS_DEVICE(&s->gcr), 0, NPCM7XX_GCR_BA);
  472. /* Clock Control Registers (CLK). Cannot fail. */
  473. sysbus_realize(SYS_BUS_DEVICE(&s->clk), &error_abort);
  474. sysbus_mmio_map(SYS_BUS_DEVICE(&s->clk), 0, NPCM7XX_CLK_BA);
  475. /* OTP key storage and fuse strap array. Cannot fail. */
  476. sysbus_realize(SYS_BUS_DEVICE(&s->key_storage), &error_abort);
  477. sysbus_mmio_map(SYS_BUS_DEVICE(&s->key_storage), 0, NPCM7XX_OTP1_BA);
  478. sysbus_realize(SYS_BUS_DEVICE(&s->fuse_array), &error_abort);
  479. sysbus_mmio_map(SYS_BUS_DEVICE(&s->fuse_array), 0, NPCM7XX_OTP2_BA);
  480. npcm7xx_init_fuses(s);
  481. /* Fake Memory Controller (MC). Cannot fail. */
  482. sysbus_realize(SYS_BUS_DEVICE(&s->mc), &error_abort);
  483. sysbus_mmio_map(SYS_BUS_DEVICE(&s->mc), 0, NPCM7XX_MC_BA);
  484. /* ADC Modules. Cannot fail. */
  485. qdev_connect_clock_in(DEVICE(&s->adc), "clock", qdev_get_clock_out(
  486. DEVICE(&s->clk), "adc-clock"));
  487. sysbus_realize(SYS_BUS_DEVICE(&s->adc), &error_abort);
  488. sysbus_mmio_map(SYS_BUS_DEVICE(&s->adc), 0, NPCM7XX_ADC_BA);
  489. sysbus_connect_irq(SYS_BUS_DEVICE(&s->adc), 0,
  490. npcm7xx_irq(s, NPCM7XX_ADC_IRQ));
  491. npcm7xx_write_adc_calibration(s);
  492. /* Timer Modules (TIM). Cannot fail. */
  493. QEMU_BUILD_BUG_ON(ARRAY_SIZE(npcm7xx_tim_addr) != ARRAY_SIZE(s->tim));
  494. for (i = 0; i < ARRAY_SIZE(s->tim); i++) {
  495. SysBusDevice *sbd = SYS_BUS_DEVICE(&s->tim[i]);
  496. int first_irq;
  497. int j;
  498. /* Connect the timer clock. */
  499. qdev_connect_clock_in(DEVICE(&s->tim[i]), "clock", qdev_get_clock_out(
  500. DEVICE(&s->clk), "timer-clock"));
  501. sysbus_realize(sbd, &error_abort);
  502. sysbus_mmio_map(sbd, 0, npcm7xx_tim_addr[i]);
  503. first_irq = NPCM7XX_TIMER0_IRQ + i * NPCM7XX_TIMERS_PER_CTRL;
  504. for (j = 0; j < NPCM7XX_TIMERS_PER_CTRL; j++) {
  505. qemu_irq irq = npcm7xx_irq(s, first_irq + j);
  506. sysbus_connect_irq(sbd, j, irq);
  507. }
  508. /* IRQ for watchdogs */
  509. sysbus_connect_irq(sbd, NPCM7XX_TIMERS_PER_CTRL,
  510. npcm7xx_irq(s, NPCM7XX_WDG0_IRQ + i));
  511. /* GPIO that connects clk module with watchdog */
  512. qdev_connect_gpio_out_named(DEVICE(&s->tim[i]),
  513. NPCM7XX_WATCHDOG_RESET_GPIO_OUT, 0,
  514. qdev_get_gpio_in_named(DEVICE(&s->clk),
  515. NPCM7XX_WATCHDOG_RESET_GPIO_IN, i));
  516. }
  517. /* UART0..3 (16550 compatible) */
  518. for (i = 0; i < ARRAY_SIZE(npcm7xx_uart_addr); i++) {
  519. serial_mm_init(get_system_memory(), npcm7xx_uart_addr[i], 2,
  520. npcm7xx_irq(s, NPCM7XX_UART0_IRQ + i), 115200,
  521. serial_hd(i), DEVICE_LITTLE_ENDIAN);
  522. }
  523. /* Random Number Generator. Cannot fail. */
  524. sysbus_realize(SYS_BUS_DEVICE(&s->rng), &error_abort);
  525. sysbus_mmio_map(SYS_BUS_DEVICE(&s->rng), 0, NPCM7XX_RNG_BA);
  526. /* GPIO modules. Cannot fail. */
  527. QEMU_BUILD_BUG_ON(ARRAY_SIZE(npcm7xx_gpio) != ARRAY_SIZE(s->gpio));
  528. for (i = 0; i < ARRAY_SIZE(s->gpio); i++) {
  529. Object *obj = OBJECT(&s->gpio[i]);
  530. object_property_set_uint(obj, "reset-pullup",
  531. npcm7xx_gpio[i].reset_pu, &error_abort);
  532. object_property_set_uint(obj, "reset-pulldown",
  533. npcm7xx_gpio[i].reset_pd, &error_abort);
  534. object_property_set_uint(obj, "reset-osrc",
  535. npcm7xx_gpio[i].reset_osrc, &error_abort);
  536. object_property_set_uint(obj, "reset-odsc",
  537. npcm7xx_gpio[i].reset_odsc, &error_abort);
  538. sysbus_realize(SYS_BUS_DEVICE(obj), &error_abort);
  539. sysbus_mmio_map(SYS_BUS_DEVICE(obj), 0, npcm7xx_gpio[i].regs_addr);
  540. sysbus_connect_irq(SYS_BUS_DEVICE(obj), 0,
  541. npcm7xx_irq(s, NPCM7XX_GPIO0_IRQ + i));
  542. }
  543. /* SMBus modules. Cannot fail. */
  544. QEMU_BUILD_BUG_ON(ARRAY_SIZE(npcm7xx_smbus_addr) != ARRAY_SIZE(s->smbus));
  545. for (i = 0; i < ARRAY_SIZE(s->smbus); i++) {
  546. Object *obj = OBJECT(&s->smbus[i]);
  547. sysbus_realize(SYS_BUS_DEVICE(obj), &error_abort);
  548. sysbus_mmio_map(SYS_BUS_DEVICE(obj), 0, npcm7xx_smbus_addr[i]);
  549. sysbus_connect_irq(SYS_BUS_DEVICE(obj), 0,
  550. npcm7xx_irq(s, NPCM7XX_SMBUS0_IRQ + i));
  551. }
  552. /* USB Host */
  553. object_property_set_bool(OBJECT(&s->ehci), "companion-enable", true,
  554. &error_abort);
  555. sysbus_realize(SYS_BUS_DEVICE(&s->ehci), &error_abort);
  556. sysbus_mmio_map(SYS_BUS_DEVICE(&s->ehci), 0, NPCM7XX_EHCI_BA);
  557. sysbus_connect_irq(SYS_BUS_DEVICE(&s->ehci), 0,
  558. npcm7xx_irq(s, NPCM7XX_EHCI_IRQ));
  559. object_property_set_str(OBJECT(&s->ohci), "masterbus", "usb-bus.0",
  560. &error_abort);
  561. object_property_set_uint(OBJECT(&s->ohci), "num-ports", 1, &error_abort);
  562. sysbus_realize(SYS_BUS_DEVICE(&s->ohci), &error_abort);
  563. sysbus_mmio_map(SYS_BUS_DEVICE(&s->ohci), 0, NPCM7XX_OHCI_BA);
  564. sysbus_connect_irq(SYS_BUS_DEVICE(&s->ohci), 0,
  565. npcm7xx_irq(s, NPCM7XX_OHCI_IRQ));
  566. /* PWM Modules. Cannot fail. */
  567. QEMU_BUILD_BUG_ON(ARRAY_SIZE(npcm7xx_pwm_addr) != ARRAY_SIZE(s->pwm));
  568. for (i = 0; i < ARRAY_SIZE(s->pwm); i++) {
  569. SysBusDevice *sbd = SYS_BUS_DEVICE(&s->pwm[i]);
  570. qdev_connect_clock_in(DEVICE(&s->pwm[i]), "clock", qdev_get_clock_out(
  571. DEVICE(&s->clk), "apb3-clock"));
  572. sysbus_realize(sbd, &error_abort);
  573. sysbus_mmio_map(sbd, 0, npcm7xx_pwm_addr[i]);
  574. sysbus_connect_irq(sbd, i, npcm7xx_irq(s, NPCM7XX_PWM0_IRQ + i));
  575. }
  576. /* MFT Modules. Cannot fail. */
  577. QEMU_BUILD_BUG_ON(ARRAY_SIZE(npcm7xx_mft_addr) != ARRAY_SIZE(s->mft));
  578. for (i = 0; i < ARRAY_SIZE(s->mft); i++) {
  579. SysBusDevice *sbd = SYS_BUS_DEVICE(&s->mft[i]);
  580. qdev_connect_clock_in(DEVICE(&s->mft[i]), "clock-in",
  581. qdev_get_clock_out(DEVICE(&s->clk),
  582. "apb4-clock"));
  583. sysbus_realize(sbd, &error_abort);
  584. sysbus_mmio_map(sbd, 0, npcm7xx_mft_addr[i]);
  585. sysbus_connect_irq(sbd, 0, npcm7xx_irq(s, NPCM7XX_MFT0_IRQ + i));
  586. }
  587. /*
  588. * EMC Modules. Cannot fail.
  589. * Use the available NIC configurations in order, allowing 'emc0' and
  590. * 'emc1' to by used as aliases for the model= parameter to override.
  591. *
  592. * This works around the inability to specify the netdev property for the
  593. * emc device: it's not pluggable and thus the -device option can't be
  594. * used.
  595. */
  596. QEMU_BUILD_BUG_ON(ARRAY_SIZE(npcm7xx_emc_addr) != ARRAY_SIZE(s->emc));
  597. QEMU_BUILD_BUG_ON(ARRAY_SIZE(s->emc) != 2);
  598. for (i = 0; i < ARRAY_SIZE(s->emc); i++) {
  599. SysBusDevice *sbd = SYS_BUS_DEVICE(&s->emc[i]);
  600. char alias[6];
  601. s->emc[i].emc_num = i;
  602. snprintf(alias, sizeof(alias), "emc%u", i);
  603. qemu_configure_nic_device(DEVICE(sbd), true, alias);
  604. /*
  605. * The device exists regardless of whether it's connected to a QEMU
  606. * netdev backend. So always instantiate it even if there is no
  607. * backend.
  608. */
  609. sysbus_realize(sbd, &error_abort);
  610. sysbus_mmio_map(sbd, 0, npcm7xx_emc_addr[i]);
  611. int tx_irq = i == 0 ? NPCM7XX_EMC1TX_IRQ : NPCM7XX_EMC2TX_IRQ;
  612. int rx_irq = i == 0 ? NPCM7XX_EMC1RX_IRQ : NPCM7XX_EMC2RX_IRQ;
  613. /*
  614. * N.B. The values for the second argument sysbus_connect_irq are
  615. * chosen to match the registration order in npcm7xx_emc_realize.
  616. */
  617. sysbus_connect_irq(sbd, 0, npcm7xx_irq(s, tx_irq));
  618. sysbus_connect_irq(sbd, 1, npcm7xx_irq(s, rx_irq));
  619. }
  620. /*
  621. * GMAC Modules. Cannot fail.
  622. */
  623. QEMU_BUILD_BUG_ON(ARRAY_SIZE(npcm7xx_gmac_addr) != ARRAY_SIZE(s->gmac));
  624. QEMU_BUILD_BUG_ON(ARRAY_SIZE(s->gmac) != 2);
  625. for (i = 0; i < ARRAY_SIZE(s->gmac); i++) {
  626. SysBusDevice *sbd = SYS_BUS_DEVICE(&s->gmac[i]);
  627. qemu_configure_nic_device(DEVICE(sbd), false, NULL);
  628. /*
  629. * The device exists regardless of whether it's connected to a QEMU
  630. * netdev backend. So always instantiate it even if there is no
  631. * backend.
  632. */
  633. sysbus_realize(sbd, &error_abort);
  634. sysbus_mmio_map(sbd, 0, npcm7xx_gmac_addr[i]);
  635. int irq = i == 0 ? NPCM7XX_GMAC1_IRQ : NPCM7XX_GMAC2_IRQ;
  636. /*
  637. * N.B. The values for the second argument sysbus_connect_irq are
  638. * chosen to match the registration order in npcm7xx_emc_realize.
  639. */
  640. sysbus_connect_irq(sbd, 0, npcm7xx_irq(s, irq));
  641. }
  642. /*
  643. * Flash Interface Unit (FIU). Can fail if incorrect number of chip selects
  644. * specified, but this is a programming error.
  645. */
  646. QEMU_BUILD_BUG_ON(ARRAY_SIZE(npcm7xx_fiu) != ARRAY_SIZE(s->fiu));
  647. for (i = 0; i < ARRAY_SIZE(s->fiu); i++) {
  648. SysBusDevice *sbd = SYS_BUS_DEVICE(&s->fiu[i]);
  649. int j;
  650. object_property_set_int(OBJECT(sbd), "cs-count",
  651. npcm7xx_fiu[i].cs_count, &error_abort);
  652. object_property_set_int(OBJECT(sbd), "flash-size",
  653. npcm7xx_fiu[i].flash_size, &error_abort);
  654. sysbus_realize(sbd, &error_abort);
  655. sysbus_mmio_map(sbd, 0, npcm7xx_fiu[i].regs_addr);
  656. for (j = 0; j < npcm7xx_fiu[i].cs_count; j++) {
  657. sysbus_mmio_map(sbd, j + 1, npcm7xx_fiu[i].flash_addr[j]);
  658. }
  659. }
  660. /* RAM2 (SRAM) */
  661. memory_region_init_ram(&s->sram, OBJECT(dev), "ram2",
  662. NPCM7XX_RAM2_SZ, &error_abort);
  663. memory_region_add_subregion(get_system_memory(), NPCM7XX_RAM2_BA, &s->sram);
  664. /* RAM3 (SRAM) */
  665. memory_region_init_ram(&s->ram3, OBJECT(dev), "ram3",
  666. NPCM7XX_RAM3_SZ, &error_abort);
  667. memory_region_add_subregion(get_system_memory(), NPCM7XX_RAM3_BA, &s->ram3);
  668. /* Internal ROM */
  669. memory_region_init_rom(&s->irom, OBJECT(dev), "irom", NPCM7XX_ROM_SZ,
  670. &error_abort);
  671. memory_region_add_subregion(get_system_memory(), NPCM7XX_ROM_BA, &s->irom);
  672. /* SDHCI */
  673. sysbus_realize(SYS_BUS_DEVICE(&s->mmc), &error_abort);
  674. sysbus_mmio_map(SYS_BUS_DEVICE(&s->mmc), 0, NPCM7XX_MMC_BA);
  675. sysbus_connect_irq(SYS_BUS_DEVICE(&s->mmc), 0,
  676. npcm7xx_irq(s, NPCM7XX_MMC_IRQ));
  677. /* PSPI */
  678. QEMU_BUILD_BUG_ON(ARRAY_SIZE(npcm7xx_pspi_addr) != ARRAY_SIZE(s->pspi));
  679. for (i = 0; i < ARRAY_SIZE(s->pspi); i++) {
  680. SysBusDevice *sbd = SYS_BUS_DEVICE(&s->pspi[i]);
  681. int irq = (i == 0) ? NPCM7XX_PSPI1_IRQ : NPCM7XX_PSPI2_IRQ;
  682. sysbus_realize(sbd, &error_abort);
  683. sysbus_mmio_map(sbd, 0, npcm7xx_pspi_addr[i]);
  684. sysbus_connect_irq(sbd, 0, npcm7xx_irq(s, irq));
  685. }
  686. create_unimplemented_device("npcm7xx.shm", 0xc0001000, 4 * KiB);
  687. create_unimplemented_device("npcm7xx.vdmx", 0xe0800000, 4 * KiB);
  688. create_unimplemented_device("npcm7xx.pcierc", 0xe1000000, 64 * KiB);
  689. create_unimplemented_device("npcm7xx.kcs", 0xf0007000, 4 * KiB);
  690. create_unimplemented_device("npcm7xx.gfxi", 0xf000e000, 4 * KiB);
  691. create_unimplemented_device("npcm7xx.espi", 0xf009f000, 4 * KiB);
  692. create_unimplemented_device("npcm7xx.peci", 0xf0100000, 4 * KiB);
  693. create_unimplemented_device("npcm7xx.siox[1]", 0xf0101000, 4 * KiB);
  694. create_unimplemented_device("npcm7xx.siox[2]", 0xf0102000, 4 * KiB);
  695. create_unimplemented_device("npcm7xx.ahbpci", 0xf0400000, 1 * MiB);
  696. create_unimplemented_device("npcm7xx.mcphy", 0xf05f0000, 64 * KiB);
  697. create_unimplemented_device("npcm7xx.vcd", 0xf0810000, 64 * KiB);
  698. create_unimplemented_device("npcm7xx.ece", 0xf0820000, 8 * KiB);
  699. create_unimplemented_device("npcm7xx.vdma", 0xf0822000, 8 * KiB);
  700. create_unimplemented_device("npcm7xx.usbd[0]", 0xf0830000, 4 * KiB);
  701. create_unimplemented_device("npcm7xx.usbd[1]", 0xf0831000, 4 * KiB);
  702. create_unimplemented_device("npcm7xx.usbd[2]", 0xf0832000, 4 * KiB);
  703. create_unimplemented_device("npcm7xx.usbd[3]", 0xf0833000, 4 * KiB);
  704. create_unimplemented_device("npcm7xx.usbd[4]", 0xf0834000, 4 * KiB);
  705. create_unimplemented_device("npcm7xx.usbd[5]", 0xf0835000, 4 * KiB);
  706. create_unimplemented_device("npcm7xx.usbd[6]", 0xf0836000, 4 * KiB);
  707. create_unimplemented_device("npcm7xx.usbd[7]", 0xf0837000, 4 * KiB);
  708. create_unimplemented_device("npcm7xx.usbd[8]", 0xf0838000, 4 * KiB);
  709. create_unimplemented_device("npcm7xx.usbd[9]", 0xf0839000, 4 * KiB);
  710. create_unimplemented_device("npcm7xx.sd", 0xf0840000, 8 * KiB);
  711. create_unimplemented_device("npcm7xx.pcimbx", 0xf0848000, 512 * KiB);
  712. create_unimplemented_device("npcm7xx.aes", 0xf0858000, 4 * KiB);
  713. create_unimplemented_device("npcm7xx.des", 0xf0859000, 4 * KiB);
  714. create_unimplemented_device("npcm7xx.sha", 0xf085a000, 4 * KiB);
  715. create_unimplemented_device("npcm7xx.secacc", 0xf085b000, 4 * KiB);
  716. create_unimplemented_device("npcm7xx.spixcs0", 0xf8000000, 16 * MiB);
  717. create_unimplemented_device("npcm7xx.spixcs1", 0xf9000000, 16 * MiB);
  718. create_unimplemented_device("npcm7xx.spix", 0xfb001000, 4 * KiB);
  719. }
  720. static const Property npcm7xx_properties[] = {
  721. DEFINE_PROP_LINK("dram-mr", NPCM7xxState, dram, TYPE_MEMORY_REGION,
  722. MemoryRegion *),
  723. };
  724. static void npcm7xx_class_init(ObjectClass *oc, void *data)
  725. {
  726. DeviceClass *dc = DEVICE_CLASS(oc);
  727. dc->realize = npcm7xx_realize;
  728. dc->user_creatable = false;
  729. device_class_set_props(dc, npcm7xx_properties);
  730. }
  731. static void npcm730_class_init(ObjectClass *oc, void *data)
  732. {
  733. NPCM7xxClass *nc = NPCM7XX_CLASS(oc);
  734. /* NPCM730 is optimized for data center use, so no graphics, etc. */
  735. nc->disabled_modules = 0x00300395;
  736. nc->num_cpus = 2;
  737. }
  738. static void npcm750_class_init(ObjectClass *oc, void *data)
  739. {
  740. NPCM7xxClass *nc = NPCM7XX_CLASS(oc);
  741. /* NPCM750 has 2 cores and a full set of peripherals */
  742. nc->disabled_modules = 0x00000000;
  743. nc->num_cpus = 2;
  744. }
  745. static const TypeInfo npcm7xx_soc_types[] = {
  746. {
  747. .name = TYPE_NPCM7XX,
  748. .parent = TYPE_DEVICE,
  749. .instance_size = sizeof(NPCM7xxState),
  750. .instance_init = npcm7xx_init,
  751. .class_size = sizeof(NPCM7xxClass),
  752. .class_init = npcm7xx_class_init,
  753. .abstract = true,
  754. }, {
  755. .name = TYPE_NPCM730,
  756. .parent = TYPE_NPCM7XX,
  757. .class_init = npcm730_class_init,
  758. }, {
  759. .name = TYPE_NPCM750,
  760. .parent = TYPE_NPCM7XX,
  761. .class_init = npcm750_class_init,
  762. },
  763. };
  764. DEFINE_TYPES(npcm7xx_soc_types);