2
0

aspeed_scu.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /*
  2. * ASPEED System Control Unit
  3. *
  4. * Andrew Jeffery <andrew@aj.id.au>
  5. *
  6. * Copyright 2016 IBM Corp.
  7. *
  8. * This code is licensed under the GPL version 2 or later. See
  9. * the COPYING file in the top-level directory.
  10. */
  11. #include "qemu/osdep.h"
  12. #include "hw/misc/aspeed_scu.h"
  13. #include "hw/qdev-properties.h"
  14. #include "migration/vmstate.h"
  15. #include "qapi/error.h"
  16. #include "qapi/visitor.h"
  17. #include "qemu/bitops.h"
  18. #include "qemu/log.h"
  19. #include "qemu/guest-random.h"
  20. #include "qemu/module.h"
  21. #include "trace.h"
  22. #define TO_REG(offset) ((offset) >> 2)
  23. #define PROT_KEY TO_REG(0x00)
  24. #define SYS_RST_CTRL TO_REG(0x04)
  25. #define CLK_SEL TO_REG(0x08)
  26. #define CLK_STOP_CTRL TO_REG(0x0C)
  27. #define FREQ_CNTR_CTRL TO_REG(0x10)
  28. #define FREQ_CNTR_EVAL TO_REG(0x14)
  29. #define IRQ_CTRL TO_REG(0x18)
  30. #define D2PLL_PARAM TO_REG(0x1C)
  31. #define MPLL_PARAM TO_REG(0x20)
  32. #define HPLL_PARAM TO_REG(0x24)
  33. #define FREQ_CNTR_RANGE TO_REG(0x28)
  34. #define MISC_CTRL1 TO_REG(0x2C)
  35. #define PCI_CTRL1 TO_REG(0x30)
  36. #define PCI_CTRL2 TO_REG(0x34)
  37. #define PCI_CTRL3 TO_REG(0x38)
  38. #define SYS_RST_STATUS TO_REG(0x3C)
  39. #define SOC_SCRATCH1 TO_REG(0x40)
  40. #define SOC_SCRATCH2 TO_REG(0x44)
  41. #define MAC_CLK_DELAY TO_REG(0x48)
  42. #define MISC_CTRL2 TO_REG(0x4C)
  43. #define VGA_SCRATCH1 TO_REG(0x50)
  44. #define VGA_SCRATCH2 TO_REG(0x54)
  45. #define VGA_SCRATCH3 TO_REG(0x58)
  46. #define VGA_SCRATCH4 TO_REG(0x5C)
  47. #define VGA_SCRATCH5 TO_REG(0x60)
  48. #define VGA_SCRATCH6 TO_REG(0x64)
  49. #define VGA_SCRATCH7 TO_REG(0x68)
  50. #define VGA_SCRATCH8 TO_REG(0x6C)
  51. #define HW_STRAP1 TO_REG(0x70)
  52. #define RNG_CTRL TO_REG(0x74)
  53. #define RNG_DATA TO_REG(0x78)
  54. #define SILICON_REV TO_REG(0x7C)
  55. #define PINMUX_CTRL1 TO_REG(0x80)
  56. #define PINMUX_CTRL2 TO_REG(0x84)
  57. #define PINMUX_CTRL3 TO_REG(0x88)
  58. #define PINMUX_CTRL4 TO_REG(0x8C)
  59. #define PINMUX_CTRL5 TO_REG(0x90)
  60. #define PINMUX_CTRL6 TO_REG(0x94)
  61. #define WDT_RST_CTRL TO_REG(0x9C)
  62. #define PINMUX_CTRL7 TO_REG(0xA0)
  63. #define PINMUX_CTRL8 TO_REG(0xA4)
  64. #define PINMUX_CTRL9 TO_REG(0xA8)
  65. #define WAKEUP_EN TO_REG(0xC0)
  66. #define WAKEUP_CTRL TO_REG(0xC4)
  67. #define HW_STRAP2 TO_REG(0xD0)
  68. #define FREE_CNTR4 TO_REG(0xE0)
  69. #define FREE_CNTR4_EXT TO_REG(0xE4)
  70. #define CPU2_CTRL TO_REG(0x100)
  71. #define CPU2_BASE_SEG1 TO_REG(0x104)
  72. #define CPU2_BASE_SEG2 TO_REG(0x108)
  73. #define CPU2_BASE_SEG3 TO_REG(0x10C)
  74. #define CPU2_BASE_SEG4 TO_REG(0x110)
  75. #define CPU2_BASE_SEG5 TO_REG(0x114)
  76. #define CPU2_CACHE_CTRL TO_REG(0x118)
  77. #define CHIP_ID0 TO_REG(0x150)
  78. #define CHIP_ID1 TO_REG(0x154)
  79. #define UART_HPLL_CLK TO_REG(0x160)
  80. #define PCIE_CTRL TO_REG(0x180)
  81. #define BMC_MMIO_CTRL TO_REG(0x184)
  82. #define RELOC_DECODE_BASE1 TO_REG(0x188)
  83. #define RELOC_DECODE_BASE2 TO_REG(0x18C)
  84. #define MAILBOX_DECODE_BASE TO_REG(0x190)
  85. #define SRAM_DECODE_BASE1 TO_REG(0x194)
  86. #define SRAM_DECODE_BASE2 TO_REG(0x198)
  87. #define BMC_REV TO_REG(0x19C)
  88. #define BMC_DEV_ID TO_REG(0x1A4)
  89. #define AST2600_PROT_KEY TO_REG(0x00)
  90. #define AST2600_SILICON_REV TO_REG(0x04)
  91. #define AST2600_SILICON_REV2 TO_REG(0x14)
  92. #define AST2600_SYS_RST_CTRL TO_REG(0x40)
  93. #define AST2600_SYS_RST_CTRL_CLR TO_REG(0x44)
  94. #define AST2600_SYS_RST_CTRL2 TO_REG(0x50)
  95. #define AST2600_SYS_RST_CTRL2_CLR TO_REG(0x54)
  96. #define AST2600_CLK_STOP_CTRL TO_REG(0x80)
  97. #define AST2600_CLK_STOP_CTRL_CLR TO_REG(0x84)
  98. #define AST2600_CLK_STOP_CTRL2 TO_REG(0x90)
  99. #define AST2600_CLK_STOP_CTRL2_CLR TO_REG(0x94)
  100. #define AST2600_DEBUG_CTRL TO_REG(0xC8)
  101. #define AST2600_DEBUG_CTRL2 TO_REG(0xD8)
  102. #define AST2600_SDRAM_HANDSHAKE TO_REG(0x100)
  103. #define AST2600_HPLL_PARAM TO_REG(0x200)
  104. #define AST2600_HPLL_EXT TO_REG(0x204)
  105. #define AST2600_APLL_PARAM TO_REG(0x210)
  106. #define AST2600_APLL_EXT TO_REG(0x214)
  107. #define AST2600_MPLL_PARAM TO_REG(0x220)
  108. #define AST2600_MPLL_EXT TO_REG(0x224)
  109. #define AST2600_EPLL_PARAM TO_REG(0x240)
  110. #define AST2600_EPLL_EXT TO_REG(0x244)
  111. #define AST2600_DPLL_PARAM TO_REG(0x260)
  112. #define AST2600_DPLL_EXT TO_REG(0x264)
  113. #define AST2600_CLK_SEL TO_REG(0x300)
  114. #define AST2600_CLK_SEL2 TO_REG(0x304)
  115. #define AST2600_CLK_SEL3 TO_REG(0x308)
  116. #define AST2600_CLK_SEL4 TO_REG(0x310)
  117. #define AST2600_CLK_SEL5 TO_REG(0x314)
  118. #define AST2600_UARTCLK TO_REG(0x338)
  119. #define AST2600_HUARTCLK TO_REG(0x33C)
  120. #define AST2600_HW_STRAP1 TO_REG(0x500)
  121. #define AST2600_HW_STRAP1_CLR TO_REG(0x504)
  122. #define AST2600_HW_STRAP1_PROT TO_REG(0x508)
  123. #define AST2600_HW_STRAP2 TO_REG(0x510)
  124. #define AST2600_HW_STRAP2_CLR TO_REG(0x514)
  125. #define AST2600_HW_STRAP2_PROT TO_REG(0x518)
  126. #define AST2600_RNG_CTRL TO_REG(0x524)
  127. #define AST2600_RNG_DATA TO_REG(0x540)
  128. #define AST2600_CHIP_ID0 TO_REG(0x5B0)
  129. #define AST2600_CHIP_ID1 TO_REG(0x5B4)
  130. #define AST2600_CLK TO_REG(0x40)
  131. #define SCU_IO_REGION_SIZE 0x1000
  132. static const uint32_t ast2400_a0_resets[ASPEED_SCU_NR_REGS] = {
  133. [SYS_RST_CTRL] = 0xFFCFFEDCU,
  134. [CLK_SEL] = 0xF3F40000U,
  135. [CLK_STOP_CTRL] = 0x19FC3E8BU,
  136. [D2PLL_PARAM] = 0x00026108U,
  137. [MPLL_PARAM] = 0x00030291U,
  138. [HPLL_PARAM] = 0x00000291U,
  139. [MISC_CTRL1] = 0x00000010U,
  140. [PCI_CTRL1] = 0x20001A03U,
  141. [PCI_CTRL2] = 0x20001A03U,
  142. [PCI_CTRL3] = 0x04000030U,
  143. [SYS_RST_STATUS] = 0x00000001U,
  144. [SOC_SCRATCH1] = 0x000000C0U, /* SoC completed DRAM init */
  145. [MISC_CTRL2] = 0x00000023U,
  146. [RNG_CTRL] = 0x0000000EU,
  147. [PINMUX_CTRL2] = 0x0000F000U,
  148. [PINMUX_CTRL3] = 0x01000000U,
  149. [PINMUX_CTRL4] = 0x000000FFU,
  150. [PINMUX_CTRL5] = 0x0000A000U,
  151. [WDT_RST_CTRL] = 0x003FFFF3U,
  152. [PINMUX_CTRL8] = 0xFFFF0000U,
  153. [PINMUX_CTRL9] = 0x000FFFFFU,
  154. [FREE_CNTR4] = 0x000000FFU,
  155. [FREE_CNTR4_EXT] = 0x000000FFU,
  156. [CPU2_BASE_SEG1] = 0x80000000U,
  157. [CPU2_BASE_SEG4] = 0x1E600000U,
  158. [CPU2_BASE_SEG5] = 0xC0000000U,
  159. [UART_HPLL_CLK] = 0x00001903U,
  160. [PCIE_CTRL] = 0x0000007BU,
  161. [BMC_DEV_ID] = 0x00002402U
  162. };
  163. /* SCU70 bit 23: 0 24Mhz. bit 11:9: 0b001 AXI:ABH ratio 2:1 */
  164. /* AST2500 revision A1 */
  165. static const uint32_t ast2500_a1_resets[ASPEED_SCU_NR_REGS] = {
  166. [SYS_RST_CTRL] = 0xFFCFFEDCU,
  167. [CLK_SEL] = 0xF3F40000U,
  168. [CLK_STOP_CTRL] = 0x19FC3E8BU,
  169. [D2PLL_PARAM] = 0x00026108U,
  170. [MPLL_PARAM] = 0x00030291U,
  171. [HPLL_PARAM] = 0x93000400U,
  172. [MISC_CTRL1] = 0x00000010U,
  173. [PCI_CTRL1] = 0x20001A03U,
  174. [PCI_CTRL2] = 0x20001A03U,
  175. [PCI_CTRL3] = 0x04000030U,
  176. [SYS_RST_STATUS] = 0x00000001U,
  177. [SOC_SCRATCH1] = 0x000000C0U, /* SoC completed DRAM init */
  178. [MISC_CTRL2] = 0x00000023U,
  179. [RNG_CTRL] = 0x0000000EU,
  180. [PINMUX_CTRL2] = 0x0000F000U,
  181. [PINMUX_CTRL3] = 0x03000000U,
  182. [PINMUX_CTRL4] = 0x00000000U,
  183. [PINMUX_CTRL5] = 0x0000A000U,
  184. [WDT_RST_CTRL] = 0x023FFFF3U,
  185. [PINMUX_CTRL8] = 0xFFFF0000U,
  186. [PINMUX_CTRL9] = 0x000FFFFFU,
  187. [FREE_CNTR4] = 0x000000FFU,
  188. [FREE_CNTR4_EXT] = 0x000000FFU,
  189. [CPU2_BASE_SEG1] = 0x80000000U,
  190. [CPU2_BASE_SEG4] = 0x1E600000U,
  191. [CPU2_BASE_SEG5] = 0xC0000000U,
  192. [CHIP_ID0] = 0x1234ABCDU,
  193. [CHIP_ID1] = 0x88884444U,
  194. [UART_HPLL_CLK] = 0x00001903U,
  195. [PCIE_CTRL] = 0x0000007BU,
  196. [BMC_DEV_ID] = 0x00002402U
  197. };
  198. static uint32_t aspeed_scu_get_random(void)
  199. {
  200. uint32_t num;
  201. qemu_guest_getrandom_nofail(&num, sizeof(num));
  202. return num;
  203. }
  204. uint32_t aspeed_scu_get_apb_freq(AspeedSCUState *s)
  205. {
  206. return ASPEED_SCU_GET_CLASS(s)->get_apb(s);
  207. }
  208. static uint32_t aspeed_2400_scu_get_apb_freq(AspeedSCUState *s)
  209. {
  210. AspeedSCUClass *asc = ASPEED_SCU_GET_CLASS(s);
  211. uint32_t hpll = asc->calc_hpll(s, s->regs[HPLL_PARAM]);
  212. return hpll / (SCU_CLK_GET_PCLK_DIV(s->regs[CLK_SEL]) + 1)
  213. / asc->apb_divider;
  214. }
  215. static uint32_t aspeed_2600_scu_get_apb_freq(AspeedSCUState *s)
  216. {
  217. AspeedSCUClass *asc = ASPEED_SCU_GET_CLASS(s);
  218. uint32_t hpll = asc->calc_hpll(s, s->regs[AST2600_HPLL_PARAM]);
  219. return hpll / (SCU_CLK_GET_PCLK_DIV(s->regs[AST2600_CLK_SEL]) + 1)
  220. / asc->apb_divider;
  221. }
  222. static uint32_t aspeed_1030_scu_get_apb_freq(AspeedSCUState *s)
  223. {
  224. AspeedSCUClass *asc = ASPEED_SCU_GET_CLASS(s);
  225. uint32_t hpll = asc->calc_hpll(s, s->regs[AST2600_HPLL_PARAM]);
  226. return hpll / (SCU_AST1030_CLK_GET_PCLK_DIV(s->regs[AST2600_CLK_SEL4]) + 1)
  227. / asc->apb_divider;
  228. }
  229. static uint64_t aspeed_scu_read(void *opaque, hwaddr offset, unsigned size)
  230. {
  231. AspeedSCUState *s = ASPEED_SCU(opaque);
  232. int reg = TO_REG(offset);
  233. if (reg >= ASPEED_SCU_NR_REGS) {
  234. qemu_log_mask(LOG_GUEST_ERROR,
  235. "%s: Out-of-bounds read at offset 0x%" HWADDR_PRIx "\n",
  236. __func__, offset);
  237. return 0;
  238. }
  239. switch (reg) {
  240. case RNG_DATA:
  241. /* On hardware, RNG_DATA works regardless of
  242. * the state of the enable bit in RNG_CTRL
  243. */
  244. s->regs[RNG_DATA] = aspeed_scu_get_random();
  245. break;
  246. case WAKEUP_EN:
  247. qemu_log_mask(LOG_GUEST_ERROR,
  248. "%s: Read of write-only offset 0x%" HWADDR_PRIx "\n",
  249. __func__, offset);
  250. break;
  251. }
  252. trace_aspeed_scu_read(offset, size, s->regs[reg]);
  253. return s->regs[reg];
  254. }
  255. static void aspeed_ast2400_scu_write(void *opaque, hwaddr offset,
  256. uint64_t data, unsigned size)
  257. {
  258. AspeedSCUState *s = ASPEED_SCU(opaque);
  259. int reg = TO_REG(offset);
  260. if (reg >= ASPEED_SCU_NR_REGS) {
  261. qemu_log_mask(LOG_GUEST_ERROR,
  262. "%s: Out-of-bounds write at offset 0x%" HWADDR_PRIx "\n",
  263. __func__, offset);
  264. return;
  265. }
  266. if (reg > PROT_KEY && reg < CPU2_BASE_SEG1 &&
  267. !s->regs[PROT_KEY]) {
  268. qemu_log_mask(LOG_GUEST_ERROR, "%s: SCU is locked!\n", __func__);
  269. }
  270. trace_aspeed_scu_write(offset, size, data);
  271. switch (reg) {
  272. case PROT_KEY:
  273. s->regs[reg] = (data == ASPEED_SCU_PROT_KEY) ? 1 : 0;
  274. return;
  275. case SILICON_REV:
  276. case FREQ_CNTR_EVAL:
  277. case VGA_SCRATCH1 ... VGA_SCRATCH8:
  278. case RNG_DATA:
  279. case FREE_CNTR4:
  280. case FREE_CNTR4_EXT:
  281. qemu_log_mask(LOG_GUEST_ERROR,
  282. "%s: Write to read-only offset 0x%" HWADDR_PRIx "\n",
  283. __func__, offset);
  284. return;
  285. }
  286. s->regs[reg] = data;
  287. }
  288. static void aspeed_ast2500_scu_write(void *opaque, hwaddr offset,
  289. uint64_t data, unsigned size)
  290. {
  291. AspeedSCUState *s = ASPEED_SCU(opaque);
  292. int reg = TO_REG(offset);
  293. if (reg >= ASPEED_SCU_NR_REGS) {
  294. qemu_log_mask(LOG_GUEST_ERROR,
  295. "%s: Out-of-bounds write at offset 0x%" HWADDR_PRIx "\n",
  296. __func__, offset);
  297. return;
  298. }
  299. if (reg > PROT_KEY && reg < CPU2_BASE_SEG1 &&
  300. !s->regs[PROT_KEY]) {
  301. qemu_log_mask(LOG_GUEST_ERROR, "%s: SCU is locked!\n", __func__);
  302. return;
  303. }
  304. trace_aspeed_scu_write(offset, size, data);
  305. switch (reg) {
  306. case PROT_KEY:
  307. s->regs[reg] = (data == ASPEED_SCU_PROT_KEY) ? 1 : 0;
  308. return;
  309. case HW_STRAP1:
  310. s->regs[HW_STRAP1] |= data;
  311. return;
  312. case SILICON_REV:
  313. s->regs[HW_STRAP1] &= ~data;
  314. return;
  315. case FREQ_CNTR_EVAL:
  316. case VGA_SCRATCH1 ... VGA_SCRATCH8:
  317. case RNG_DATA:
  318. case FREE_CNTR4:
  319. case FREE_CNTR4_EXT:
  320. case CHIP_ID0:
  321. case CHIP_ID1:
  322. qemu_log_mask(LOG_GUEST_ERROR,
  323. "%s: Write to read-only offset 0x%" HWADDR_PRIx "\n",
  324. __func__, offset);
  325. return;
  326. }
  327. s->regs[reg] = data;
  328. }
  329. static const MemoryRegionOps aspeed_ast2400_scu_ops = {
  330. .read = aspeed_scu_read,
  331. .write = aspeed_ast2400_scu_write,
  332. .endianness = DEVICE_LITTLE_ENDIAN,
  333. .valid = {
  334. .min_access_size = 1,
  335. .max_access_size = 4,
  336. },
  337. };
  338. static const MemoryRegionOps aspeed_ast2500_scu_ops = {
  339. .read = aspeed_scu_read,
  340. .write = aspeed_ast2500_scu_write,
  341. .endianness = DEVICE_LITTLE_ENDIAN,
  342. .valid.min_access_size = 4,
  343. .valid.max_access_size = 4,
  344. .valid.unaligned = false,
  345. };
  346. static uint32_t aspeed_scu_get_clkin(AspeedSCUState *s)
  347. {
  348. if (s->hw_strap1 & SCU_HW_STRAP_CLK_25M_IN ||
  349. ASPEED_SCU_GET_CLASS(s)->clkin_25Mhz) {
  350. return 25000000;
  351. } else if (s->hw_strap1 & SCU_HW_STRAP_CLK_48M_IN) {
  352. return 48000000;
  353. } else {
  354. return 24000000;
  355. }
  356. }
  357. /*
  358. * Strapped frequencies for the AST2400 in MHz. They depend on the
  359. * clkin frequency.
  360. */
  361. static const uint32_t hpll_ast2400_freqs[][4] = {
  362. { 384, 360, 336, 408 }, /* 24MHz or 48MHz */
  363. { 400, 375, 350, 425 }, /* 25MHz */
  364. };
  365. static uint32_t aspeed_2400_scu_calc_hpll(AspeedSCUState *s, uint32_t hpll_reg)
  366. {
  367. uint8_t freq_select;
  368. bool clk_25m_in;
  369. uint32_t clkin = aspeed_scu_get_clkin(s);
  370. if (hpll_reg & SCU_AST2400_H_PLL_OFF) {
  371. return 0;
  372. }
  373. if (hpll_reg & SCU_AST2400_H_PLL_PROGRAMMED) {
  374. uint32_t multiplier = 1;
  375. if (!(hpll_reg & SCU_AST2400_H_PLL_BYPASS_EN)) {
  376. uint32_t n = (hpll_reg >> 5) & 0x3f;
  377. uint32_t od = (hpll_reg >> 4) & 0x1;
  378. uint32_t d = hpll_reg & 0xf;
  379. multiplier = (2 - od) * ((n + 2) / (d + 1));
  380. }
  381. return clkin * multiplier;
  382. }
  383. /* HW strapping */
  384. clk_25m_in = !!(s->hw_strap1 & SCU_HW_STRAP_CLK_25M_IN);
  385. freq_select = SCU_AST2400_HW_STRAP_GET_H_PLL_CLK(s->hw_strap1);
  386. return hpll_ast2400_freqs[clk_25m_in][freq_select] * 1000000;
  387. }
  388. static uint32_t aspeed_2500_scu_calc_hpll(AspeedSCUState *s, uint32_t hpll_reg)
  389. {
  390. uint32_t multiplier = 1;
  391. uint32_t clkin = aspeed_scu_get_clkin(s);
  392. if (hpll_reg & SCU_H_PLL_OFF) {
  393. return 0;
  394. }
  395. if (!(hpll_reg & SCU_H_PLL_BYPASS_EN)) {
  396. uint32_t p = (hpll_reg >> 13) & 0x3f;
  397. uint32_t m = (hpll_reg >> 5) & 0xff;
  398. uint32_t n = hpll_reg & 0x1f;
  399. multiplier = ((m + 1) / (n + 1)) / (p + 1);
  400. }
  401. return clkin * multiplier;
  402. }
  403. static uint32_t aspeed_2600_scu_calc_hpll(AspeedSCUState *s, uint32_t hpll_reg)
  404. {
  405. uint32_t multiplier = 1;
  406. uint32_t clkin = aspeed_scu_get_clkin(s);
  407. if (hpll_reg & SCU_AST2600_H_PLL_OFF) {
  408. return 0;
  409. }
  410. if (!(hpll_reg & SCU_AST2600_H_PLL_BYPASS_EN)) {
  411. uint32_t p = (hpll_reg >> 19) & 0xf;
  412. uint32_t n = (hpll_reg >> 13) & 0x3f;
  413. uint32_t m = hpll_reg & 0x1fff;
  414. multiplier = ((m + 1) / (n + 1)) / (p + 1);
  415. }
  416. return clkin * multiplier;
  417. }
  418. static void aspeed_scu_reset(DeviceState *dev)
  419. {
  420. AspeedSCUState *s = ASPEED_SCU(dev);
  421. AspeedSCUClass *asc = ASPEED_SCU_GET_CLASS(dev);
  422. memcpy(s->regs, asc->resets, asc->nr_regs * 4);
  423. s->regs[SILICON_REV] = s->silicon_rev;
  424. s->regs[HW_STRAP1] = s->hw_strap1;
  425. s->regs[HW_STRAP2] = s->hw_strap2;
  426. s->regs[PROT_KEY] = s->hw_prot_key;
  427. }
  428. static uint32_t aspeed_silicon_revs[] = {
  429. AST2400_A0_SILICON_REV,
  430. AST2400_A1_SILICON_REV,
  431. AST2500_A0_SILICON_REV,
  432. AST2500_A1_SILICON_REV,
  433. AST2600_A0_SILICON_REV,
  434. AST2600_A1_SILICON_REV,
  435. AST2600_A2_SILICON_REV,
  436. AST2600_A3_SILICON_REV,
  437. AST1030_A0_SILICON_REV,
  438. AST1030_A1_SILICON_REV,
  439. };
  440. bool is_supported_silicon_rev(uint32_t silicon_rev)
  441. {
  442. int i;
  443. for (i = 0; i < ARRAY_SIZE(aspeed_silicon_revs); i++) {
  444. if (silicon_rev == aspeed_silicon_revs[i]) {
  445. return true;
  446. }
  447. }
  448. return false;
  449. }
  450. static void aspeed_scu_realize(DeviceState *dev, Error **errp)
  451. {
  452. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  453. AspeedSCUState *s = ASPEED_SCU(dev);
  454. AspeedSCUClass *asc = ASPEED_SCU_GET_CLASS(dev);
  455. if (!is_supported_silicon_rev(s->silicon_rev)) {
  456. error_setg(errp, "Unknown silicon revision: 0x%" PRIx32,
  457. s->silicon_rev);
  458. return;
  459. }
  460. memory_region_init_io(&s->iomem, OBJECT(s), asc->ops, s,
  461. TYPE_ASPEED_SCU, SCU_IO_REGION_SIZE);
  462. sysbus_init_mmio(sbd, &s->iomem);
  463. }
  464. static const VMStateDescription vmstate_aspeed_scu = {
  465. .name = "aspeed.scu",
  466. .version_id = 2,
  467. .minimum_version_id = 2,
  468. .fields = (VMStateField[]) {
  469. VMSTATE_UINT32_ARRAY(regs, AspeedSCUState, ASPEED_AST2600_SCU_NR_REGS),
  470. VMSTATE_END_OF_LIST()
  471. }
  472. };
  473. static Property aspeed_scu_properties[] = {
  474. DEFINE_PROP_UINT32("silicon-rev", AspeedSCUState, silicon_rev, 0),
  475. DEFINE_PROP_UINT32("hw-strap1", AspeedSCUState, hw_strap1, 0),
  476. DEFINE_PROP_UINT32("hw-strap2", AspeedSCUState, hw_strap2, 0),
  477. DEFINE_PROP_UINT32("hw-prot-key", AspeedSCUState, hw_prot_key, 0),
  478. DEFINE_PROP_END_OF_LIST(),
  479. };
  480. static void aspeed_scu_class_init(ObjectClass *klass, void *data)
  481. {
  482. DeviceClass *dc = DEVICE_CLASS(klass);
  483. dc->realize = aspeed_scu_realize;
  484. dc->reset = aspeed_scu_reset;
  485. dc->desc = "ASPEED System Control Unit";
  486. dc->vmsd = &vmstate_aspeed_scu;
  487. device_class_set_props(dc, aspeed_scu_properties);
  488. }
  489. static const TypeInfo aspeed_scu_info = {
  490. .name = TYPE_ASPEED_SCU,
  491. .parent = TYPE_SYS_BUS_DEVICE,
  492. .instance_size = sizeof(AspeedSCUState),
  493. .class_init = aspeed_scu_class_init,
  494. .class_size = sizeof(AspeedSCUClass),
  495. .abstract = true,
  496. };
  497. static void aspeed_2400_scu_class_init(ObjectClass *klass, void *data)
  498. {
  499. DeviceClass *dc = DEVICE_CLASS(klass);
  500. AspeedSCUClass *asc = ASPEED_SCU_CLASS(klass);
  501. dc->desc = "ASPEED 2400 System Control Unit";
  502. asc->resets = ast2400_a0_resets;
  503. asc->calc_hpll = aspeed_2400_scu_calc_hpll;
  504. asc->get_apb = aspeed_2400_scu_get_apb_freq;
  505. asc->apb_divider = 2;
  506. asc->nr_regs = ASPEED_SCU_NR_REGS;
  507. asc->clkin_25Mhz = false;
  508. asc->ops = &aspeed_ast2400_scu_ops;
  509. }
  510. static const TypeInfo aspeed_2400_scu_info = {
  511. .name = TYPE_ASPEED_2400_SCU,
  512. .parent = TYPE_ASPEED_SCU,
  513. .instance_size = sizeof(AspeedSCUState),
  514. .class_init = aspeed_2400_scu_class_init,
  515. };
  516. static void aspeed_2500_scu_class_init(ObjectClass *klass, void *data)
  517. {
  518. DeviceClass *dc = DEVICE_CLASS(klass);
  519. AspeedSCUClass *asc = ASPEED_SCU_CLASS(klass);
  520. dc->desc = "ASPEED 2500 System Control Unit";
  521. asc->resets = ast2500_a1_resets;
  522. asc->calc_hpll = aspeed_2500_scu_calc_hpll;
  523. asc->get_apb = aspeed_2400_scu_get_apb_freq;
  524. asc->apb_divider = 4;
  525. asc->nr_regs = ASPEED_SCU_NR_REGS;
  526. asc->clkin_25Mhz = false;
  527. asc->ops = &aspeed_ast2500_scu_ops;
  528. }
  529. static const TypeInfo aspeed_2500_scu_info = {
  530. .name = TYPE_ASPEED_2500_SCU,
  531. .parent = TYPE_ASPEED_SCU,
  532. .instance_size = sizeof(AspeedSCUState),
  533. .class_init = aspeed_2500_scu_class_init,
  534. };
  535. static uint64_t aspeed_ast2600_scu_read(void *opaque, hwaddr offset,
  536. unsigned size)
  537. {
  538. AspeedSCUState *s = ASPEED_SCU(opaque);
  539. int reg = TO_REG(offset);
  540. if (reg >= ASPEED_AST2600_SCU_NR_REGS) {
  541. qemu_log_mask(LOG_GUEST_ERROR,
  542. "%s: Out-of-bounds read at offset 0x%" HWADDR_PRIx "\n",
  543. __func__, offset);
  544. return 0;
  545. }
  546. switch (reg) {
  547. case AST2600_HPLL_EXT:
  548. case AST2600_EPLL_EXT:
  549. case AST2600_MPLL_EXT:
  550. /* PLLs are always "locked" */
  551. return s->regs[reg] | BIT(31);
  552. case AST2600_RNG_DATA:
  553. /*
  554. * On hardware, RNG_DATA works regardless of the state of the
  555. * enable bit in RNG_CTRL
  556. *
  557. * TODO: Check this is true for ast2600
  558. */
  559. s->regs[AST2600_RNG_DATA] = aspeed_scu_get_random();
  560. break;
  561. }
  562. trace_aspeed_scu_read(offset, size, s->regs[reg]);
  563. return s->regs[reg];
  564. }
  565. static void aspeed_ast2600_scu_write(void *opaque, hwaddr offset,
  566. uint64_t data64, unsigned size)
  567. {
  568. AspeedSCUState *s = ASPEED_SCU(opaque);
  569. int reg = TO_REG(offset);
  570. /* Truncate here so bitwise operations below behave as expected */
  571. uint32_t data = data64;
  572. if (reg >= ASPEED_AST2600_SCU_NR_REGS) {
  573. qemu_log_mask(LOG_GUEST_ERROR,
  574. "%s: Out-of-bounds write at offset 0x%" HWADDR_PRIx "\n",
  575. __func__, offset);
  576. return;
  577. }
  578. if (reg > PROT_KEY && !s->regs[PROT_KEY]) {
  579. qemu_log_mask(LOG_GUEST_ERROR, "%s: SCU is locked!\n", __func__);
  580. }
  581. trace_aspeed_scu_write(offset, size, data);
  582. switch (reg) {
  583. case AST2600_PROT_KEY:
  584. s->regs[reg] = (data == ASPEED_SCU_PROT_KEY) ? 1 : 0;
  585. return;
  586. case AST2600_HW_STRAP1:
  587. case AST2600_HW_STRAP2:
  588. if (s->regs[reg + 2]) {
  589. return;
  590. }
  591. /* fall through */
  592. case AST2600_SYS_RST_CTRL:
  593. case AST2600_SYS_RST_CTRL2:
  594. case AST2600_CLK_STOP_CTRL:
  595. case AST2600_CLK_STOP_CTRL2:
  596. /* W1S (Write 1 to set) registers */
  597. s->regs[reg] |= data;
  598. return;
  599. case AST2600_SYS_RST_CTRL_CLR:
  600. case AST2600_SYS_RST_CTRL2_CLR:
  601. case AST2600_CLK_STOP_CTRL_CLR:
  602. case AST2600_CLK_STOP_CTRL2_CLR:
  603. case AST2600_HW_STRAP1_CLR:
  604. case AST2600_HW_STRAP2_CLR:
  605. /*
  606. * W1C (Write 1 to clear) registers are offset by one address from
  607. * the data register
  608. */
  609. s->regs[reg - 1] &= ~data;
  610. return;
  611. case AST2600_RNG_DATA:
  612. case AST2600_SILICON_REV:
  613. case AST2600_SILICON_REV2:
  614. case AST2600_CHIP_ID0:
  615. case AST2600_CHIP_ID1:
  616. /* Add read only registers here */
  617. qemu_log_mask(LOG_GUEST_ERROR,
  618. "%s: Write to read-only offset 0x%" HWADDR_PRIx "\n",
  619. __func__, offset);
  620. return;
  621. }
  622. s->regs[reg] = data;
  623. }
  624. static const MemoryRegionOps aspeed_ast2600_scu_ops = {
  625. .read = aspeed_ast2600_scu_read,
  626. .write = aspeed_ast2600_scu_write,
  627. .endianness = DEVICE_LITTLE_ENDIAN,
  628. .valid.min_access_size = 4,
  629. .valid.max_access_size = 4,
  630. .valid.unaligned = false,
  631. };
  632. static const uint32_t ast2600_a3_resets[ASPEED_AST2600_SCU_NR_REGS] = {
  633. [AST2600_SYS_RST_CTRL] = 0xF7C3FED8,
  634. [AST2600_SYS_RST_CTRL2] = 0x0DFFFFFC,
  635. [AST2600_CLK_STOP_CTRL] = 0xFFFF7F8A,
  636. [AST2600_CLK_STOP_CTRL2] = 0xFFF0FFF0,
  637. [AST2600_DEBUG_CTRL] = 0x00000FFF,
  638. [AST2600_DEBUG_CTRL2] = 0x000000FF,
  639. [AST2600_SDRAM_HANDSHAKE] = 0x00000000,
  640. [AST2600_HPLL_PARAM] = 0x1000408F,
  641. [AST2600_APLL_PARAM] = 0x1000405F,
  642. [AST2600_MPLL_PARAM] = 0x1008405F,
  643. [AST2600_EPLL_PARAM] = 0x1004077F,
  644. [AST2600_DPLL_PARAM] = 0x1078405F,
  645. [AST2600_CLK_SEL] = 0xF3940000,
  646. [AST2600_CLK_SEL2] = 0x00700000,
  647. [AST2600_CLK_SEL3] = 0x00000000,
  648. [AST2600_CLK_SEL4] = 0xF3F40000,
  649. [AST2600_CLK_SEL5] = 0x30000000,
  650. [AST2600_UARTCLK] = 0x00014506,
  651. [AST2600_HUARTCLK] = 0x000145C0,
  652. [AST2600_CHIP_ID0] = 0x1234ABCD,
  653. [AST2600_CHIP_ID1] = 0x88884444,
  654. };
  655. static void aspeed_ast2600_scu_reset(DeviceState *dev)
  656. {
  657. AspeedSCUState *s = ASPEED_SCU(dev);
  658. AspeedSCUClass *asc = ASPEED_SCU_GET_CLASS(dev);
  659. memcpy(s->regs, asc->resets, asc->nr_regs * 4);
  660. /*
  661. * A0 reports A0 in _REV, but subsequent revisions report A1 regardless
  662. * of actual revision. QEMU and Linux only support A1 onwards so this is
  663. * sufficient.
  664. */
  665. s->regs[AST2600_SILICON_REV] = AST2600_A3_SILICON_REV;
  666. s->regs[AST2600_SILICON_REV2] = s->silicon_rev;
  667. s->regs[AST2600_HW_STRAP1] = s->hw_strap1;
  668. s->regs[AST2600_HW_STRAP2] = s->hw_strap2;
  669. s->regs[PROT_KEY] = s->hw_prot_key;
  670. }
  671. static void aspeed_2600_scu_class_init(ObjectClass *klass, void *data)
  672. {
  673. DeviceClass *dc = DEVICE_CLASS(klass);
  674. AspeedSCUClass *asc = ASPEED_SCU_CLASS(klass);
  675. dc->desc = "ASPEED 2600 System Control Unit";
  676. dc->reset = aspeed_ast2600_scu_reset;
  677. asc->resets = ast2600_a3_resets;
  678. asc->calc_hpll = aspeed_2600_scu_calc_hpll;
  679. asc->get_apb = aspeed_2600_scu_get_apb_freq;
  680. asc->apb_divider = 4;
  681. asc->nr_regs = ASPEED_AST2600_SCU_NR_REGS;
  682. asc->clkin_25Mhz = true;
  683. asc->ops = &aspeed_ast2600_scu_ops;
  684. }
  685. static const TypeInfo aspeed_2600_scu_info = {
  686. .name = TYPE_ASPEED_2600_SCU,
  687. .parent = TYPE_ASPEED_SCU,
  688. .instance_size = sizeof(AspeedSCUState),
  689. .class_init = aspeed_2600_scu_class_init,
  690. };
  691. static const uint32_t ast1030_a1_resets[ASPEED_AST2600_SCU_NR_REGS] = {
  692. [AST2600_SYS_RST_CTRL] = 0xFFC3FED8,
  693. [AST2600_SYS_RST_CTRL2] = 0x09FFFFFC,
  694. [AST2600_CLK_STOP_CTRL] = 0xFFFF7F8A,
  695. [AST2600_CLK_STOP_CTRL2] = 0xFFF0FFF0,
  696. [AST2600_DEBUG_CTRL2] = 0x00000000,
  697. [AST2600_HPLL_PARAM] = 0x10004077,
  698. [AST2600_HPLL_EXT] = 0x00000031,
  699. [AST2600_CLK_SEL4] = 0x43F90900,
  700. [AST2600_CLK_SEL5] = 0x40000000,
  701. [AST2600_CHIP_ID0] = 0xDEADBEEF,
  702. [AST2600_CHIP_ID1] = 0x0BADCAFE,
  703. };
  704. static void aspeed_ast1030_scu_reset(DeviceState *dev)
  705. {
  706. AspeedSCUState *s = ASPEED_SCU(dev);
  707. AspeedSCUClass *asc = ASPEED_SCU_GET_CLASS(dev);
  708. memcpy(s->regs, asc->resets, asc->nr_regs * 4);
  709. s->regs[AST2600_SILICON_REV] = AST1030_A1_SILICON_REV;
  710. s->regs[AST2600_SILICON_REV2] = s->silicon_rev;
  711. s->regs[AST2600_HW_STRAP1] = s->hw_strap1;
  712. s->regs[AST2600_HW_STRAP2] = s->hw_strap2;
  713. s->regs[PROT_KEY] = s->hw_prot_key;
  714. }
  715. static void aspeed_1030_scu_class_init(ObjectClass *klass, void *data)
  716. {
  717. DeviceClass *dc = DEVICE_CLASS(klass);
  718. AspeedSCUClass *asc = ASPEED_SCU_CLASS(klass);
  719. dc->desc = "ASPEED 1030 System Control Unit";
  720. dc->reset = aspeed_ast1030_scu_reset;
  721. asc->resets = ast1030_a1_resets;
  722. asc->calc_hpll = aspeed_2600_scu_calc_hpll;
  723. asc->get_apb = aspeed_1030_scu_get_apb_freq;
  724. asc->apb_divider = 2;
  725. asc->nr_regs = ASPEED_AST2600_SCU_NR_REGS;
  726. asc->clkin_25Mhz = true;
  727. asc->ops = &aspeed_ast2600_scu_ops;
  728. }
  729. static const TypeInfo aspeed_1030_scu_info = {
  730. .name = TYPE_ASPEED_1030_SCU,
  731. .parent = TYPE_ASPEED_SCU,
  732. .instance_size = sizeof(AspeedSCUState),
  733. .class_init = aspeed_1030_scu_class_init,
  734. };
  735. static void aspeed_scu_register_types(void)
  736. {
  737. type_register_static(&aspeed_scu_info);
  738. type_register_static(&aspeed_2400_scu_info);
  739. type_register_static(&aspeed_2500_scu_info);
  740. type_register_static(&aspeed_2600_scu_info);
  741. type_register_static(&aspeed_1030_scu_info);
  742. }
  743. type_init(aspeed_scu_register_types);