Merge remote-tracking branch 'remotes/dgilbert-gitlab/tags/pull-migration-20210726a...
[qemu/armbru.git] / hw / misc / aspeed_scu.c
blob40a38ebd854977e7273fd9322951628d108b0e6d
1 /*
2 * ASPEED System Control Unit
4 * Andrew Jeffery <andrew@aj.id.au>
6 * Copyright 2016 IBM Corp.
8 * This code is licensed under the GPL version 2 or later. See
9 * the COPYING file in the top-level directory.
12 #include "qemu/osdep.h"
13 #include "hw/misc/aspeed_scu.h"
14 #include "hw/qdev-properties.h"
15 #include "migration/vmstate.h"
16 #include "qapi/error.h"
17 #include "qapi/visitor.h"
18 #include "qemu/bitops.h"
19 #include "qemu/log.h"
20 #include "qemu/guest-random.h"
21 #include "qemu/module.h"
22 #include "trace.h"
24 #define TO_REG(offset) ((offset) >> 2)
26 #define PROT_KEY TO_REG(0x00)
27 #define SYS_RST_CTRL TO_REG(0x04)
28 #define CLK_SEL TO_REG(0x08)
29 #define CLK_STOP_CTRL TO_REG(0x0C)
30 #define FREQ_CNTR_CTRL TO_REG(0x10)
31 #define FREQ_CNTR_EVAL TO_REG(0x14)
32 #define IRQ_CTRL TO_REG(0x18)
33 #define D2PLL_PARAM TO_REG(0x1C)
34 #define MPLL_PARAM TO_REG(0x20)
35 #define HPLL_PARAM TO_REG(0x24)
36 #define FREQ_CNTR_RANGE TO_REG(0x28)
37 #define MISC_CTRL1 TO_REG(0x2C)
38 #define PCI_CTRL1 TO_REG(0x30)
39 #define PCI_CTRL2 TO_REG(0x34)
40 #define PCI_CTRL3 TO_REG(0x38)
41 #define SYS_RST_STATUS TO_REG(0x3C)
42 #define SOC_SCRATCH1 TO_REG(0x40)
43 #define SOC_SCRATCH2 TO_REG(0x44)
44 #define MAC_CLK_DELAY TO_REG(0x48)
45 #define MISC_CTRL2 TO_REG(0x4C)
46 #define VGA_SCRATCH1 TO_REG(0x50)
47 #define VGA_SCRATCH2 TO_REG(0x54)
48 #define VGA_SCRATCH3 TO_REG(0x58)
49 #define VGA_SCRATCH4 TO_REG(0x5C)
50 #define VGA_SCRATCH5 TO_REG(0x60)
51 #define VGA_SCRATCH6 TO_REG(0x64)
52 #define VGA_SCRATCH7 TO_REG(0x68)
53 #define VGA_SCRATCH8 TO_REG(0x6C)
54 #define HW_STRAP1 TO_REG(0x70)
55 #define RNG_CTRL TO_REG(0x74)
56 #define RNG_DATA TO_REG(0x78)
57 #define SILICON_REV TO_REG(0x7C)
58 #define PINMUX_CTRL1 TO_REG(0x80)
59 #define PINMUX_CTRL2 TO_REG(0x84)
60 #define PINMUX_CTRL3 TO_REG(0x88)
61 #define PINMUX_CTRL4 TO_REG(0x8C)
62 #define PINMUX_CTRL5 TO_REG(0x90)
63 #define PINMUX_CTRL6 TO_REG(0x94)
64 #define WDT_RST_CTRL TO_REG(0x9C)
65 #define PINMUX_CTRL7 TO_REG(0xA0)
66 #define PINMUX_CTRL8 TO_REG(0xA4)
67 #define PINMUX_CTRL9 TO_REG(0xA8)
68 #define WAKEUP_EN TO_REG(0xC0)
69 #define WAKEUP_CTRL TO_REG(0xC4)
70 #define HW_STRAP2 TO_REG(0xD0)
71 #define FREE_CNTR4 TO_REG(0xE0)
72 #define FREE_CNTR4_EXT TO_REG(0xE4)
73 #define CPU2_CTRL TO_REG(0x100)
74 #define CPU2_BASE_SEG1 TO_REG(0x104)
75 #define CPU2_BASE_SEG2 TO_REG(0x108)
76 #define CPU2_BASE_SEG3 TO_REG(0x10C)
77 #define CPU2_BASE_SEG4 TO_REG(0x110)
78 #define CPU2_BASE_SEG5 TO_REG(0x114)
79 #define CPU2_CACHE_CTRL TO_REG(0x118)
80 #define CHIP_ID0 TO_REG(0x150)
81 #define CHIP_ID1 TO_REG(0x154)
82 #define UART_HPLL_CLK TO_REG(0x160)
83 #define PCIE_CTRL TO_REG(0x180)
84 #define BMC_MMIO_CTRL TO_REG(0x184)
85 #define RELOC_DECODE_BASE1 TO_REG(0x188)
86 #define RELOC_DECODE_BASE2 TO_REG(0x18C)
87 #define MAILBOX_DECODE_BASE TO_REG(0x190)
88 #define SRAM_DECODE_BASE1 TO_REG(0x194)
89 #define SRAM_DECODE_BASE2 TO_REG(0x198)
90 #define BMC_REV TO_REG(0x19C)
91 #define BMC_DEV_ID TO_REG(0x1A4)
93 #define AST2600_PROT_KEY TO_REG(0x00)
94 #define AST2600_SILICON_REV TO_REG(0x04)
95 #define AST2600_SILICON_REV2 TO_REG(0x14)
96 #define AST2600_SYS_RST_CTRL TO_REG(0x40)
97 #define AST2600_SYS_RST_CTRL_CLR TO_REG(0x44)
98 #define AST2600_SYS_RST_CTRL2 TO_REG(0x50)
99 #define AST2600_SYS_RST_CTRL2_CLR TO_REG(0x54)
100 #define AST2600_CLK_STOP_CTRL TO_REG(0x80)
101 #define AST2600_CLK_STOP_CTRL_CLR TO_REG(0x84)
102 #define AST2600_CLK_STOP_CTRL2 TO_REG(0x90)
103 #define AST2600_CLK_STOP_CTRL2_CLR TO_REG(0x94)
104 #define AST2600_SDRAM_HANDSHAKE TO_REG(0x100)
105 #define AST2600_HPLL_PARAM TO_REG(0x200)
106 #define AST2600_HPLL_EXT TO_REG(0x204)
107 #define AST2600_MPLL_EXT TO_REG(0x224)
108 #define AST2600_EPLL_EXT TO_REG(0x244)
109 #define AST2600_CLK_SEL TO_REG(0x300)
110 #define AST2600_CLK_SEL2 TO_REG(0x304)
111 #define AST2600_CLK_SEL3 TO_REG(0x310)
112 #define AST2600_HW_STRAP1 TO_REG(0x500)
113 #define AST2600_HW_STRAP1_CLR TO_REG(0x504)
114 #define AST2600_HW_STRAP1_PROT TO_REG(0x508)
115 #define AST2600_HW_STRAP2 TO_REG(0x510)
116 #define AST2600_HW_STRAP2_CLR TO_REG(0x514)
117 #define AST2600_HW_STRAP2_PROT TO_REG(0x518)
118 #define AST2600_RNG_CTRL TO_REG(0x524)
119 #define AST2600_RNG_DATA TO_REG(0x540)
120 #define AST2600_CHIP_ID0 TO_REG(0x5B0)
121 #define AST2600_CHIP_ID1 TO_REG(0x5B4)
123 #define AST2600_CLK TO_REG(0x40)
125 #define SCU_IO_REGION_SIZE 0x1000
127 static const uint32_t ast2400_a0_resets[ASPEED_SCU_NR_REGS] = {
128 [SYS_RST_CTRL] = 0xFFCFFEDCU,
129 [CLK_SEL] = 0xF3F40000U,
130 [CLK_STOP_CTRL] = 0x19FC3E8BU,
131 [D2PLL_PARAM] = 0x00026108U,
132 [MPLL_PARAM] = 0x00030291U,
133 [HPLL_PARAM] = 0x00000291U,
134 [MISC_CTRL1] = 0x00000010U,
135 [PCI_CTRL1] = 0x20001A03U,
136 [PCI_CTRL2] = 0x20001A03U,
137 [PCI_CTRL3] = 0x04000030U,
138 [SYS_RST_STATUS] = 0x00000001U,
139 [SOC_SCRATCH1] = 0x000000C0U, /* SoC completed DRAM init */
140 [MISC_CTRL2] = 0x00000023U,
141 [RNG_CTRL] = 0x0000000EU,
142 [PINMUX_CTRL2] = 0x0000F000U,
143 [PINMUX_CTRL3] = 0x01000000U,
144 [PINMUX_CTRL4] = 0x000000FFU,
145 [PINMUX_CTRL5] = 0x0000A000U,
146 [WDT_RST_CTRL] = 0x003FFFF3U,
147 [PINMUX_CTRL8] = 0xFFFF0000U,
148 [PINMUX_CTRL9] = 0x000FFFFFU,
149 [FREE_CNTR4] = 0x000000FFU,
150 [FREE_CNTR4_EXT] = 0x000000FFU,
151 [CPU2_BASE_SEG1] = 0x80000000U,
152 [CPU2_BASE_SEG4] = 0x1E600000U,
153 [CPU2_BASE_SEG5] = 0xC0000000U,
154 [UART_HPLL_CLK] = 0x00001903U,
155 [PCIE_CTRL] = 0x0000007BU,
156 [BMC_DEV_ID] = 0x00002402U
159 /* SCU70 bit 23: 0 24Mhz. bit 11:9: 0b001 AXI:ABH ratio 2:1 */
160 /* AST2500 revision A1 */
162 static const uint32_t ast2500_a1_resets[ASPEED_SCU_NR_REGS] = {
163 [SYS_RST_CTRL] = 0xFFCFFEDCU,
164 [CLK_SEL] = 0xF3F40000U,
165 [CLK_STOP_CTRL] = 0x19FC3E8BU,
166 [D2PLL_PARAM] = 0x00026108U,
167 [MPLL_PARAM] = 0x00030291U,
168 [HPLL_PARAM] = 0x93000400U,
169 [MISC_CTRL1] = 0x00000010U,
170 [PCI_CTRL1] = 0x20001A03U,
171 [PCI_CTRL2] = 0x20001A03U,
172 [PCI_CTRL3] = 0x04000030U,
173 [SYS_RST_STATUS] = 0x00000001U,
174 [SOC_SCRATCH1] = 0x000000C0U, /* SoC completed DRAM init */
175 [MISC_CTRL2] = 0x00000023U,
176 [RNG_CTRL] = 0x0000000EU,
177 [PINMUX_CTRL2] = 0x0000F000U,
178 [PINMUX_CTRL3] = 0x03000000U,
179 [PINMUX_CTRL4] = 0x00000000U,
180 [PINMUX_CTRL5] = 0x0000A000U,
181 [WDT_RST_CTRL] = 0x023FFFF3U,
182 [PINMUX_CTRL8] = 0xFFFF0000U,
183 [PINMUX_CTRL9] = 0x000FFFFFU,
184 [FREE_CNTR4] = 0x000000FFU,
185 [FREE_CNTR4_EXT] = 0x000000FFU,
186 [CPU2_BASE_SEG1] = 0x80000000U,
187 [CPU2_BASE_SEG4] = 0x1E600000U,
188 [CPU2_BASE_SEG5] = 0xC0000000U,
189 [CHIP_ID0] = 0x1234ABCDU,
190 [CHIP_ID1] = 0x88884444U,
191 [UART_HPLL_CLK] = 0x00001903U,
192 [PCIE_CTRL] = 0x0000007BU,
193 [BMC_DEV_ID] = 0x00002402U
196 static uint32_t aspeed_scu_get_random(void)
198 uint32_t num;
199 qemu_guest_getrandom_nofail(&num, sizeof(num));
200 return num;
203 uint32_t aspeed_scu_get_apb_freq(AspeedSCUState *s)
205 AspeedSCUClass *asc = ASPEED_SCU_GET_CLASS(s);
206 uint32_t hpll = asc->calc_hpll(s, s->regs[HPLL_PARAM]);
208 return hpll / (SCU_CLK_GET_PCLK_DIV(s->regs[CLK_SEL]) + 1)
209 / asc->apb_divider;
212 static uint64_t aspeed_scu_read(void *opaque, hwaddr offset, unsigned size)
214 AspeedSCUState *s = ASPEED_SCU(opaque);
215 int reg = TO_REG(offset);
217 if (reg >= ASPEED_SCU_NR_REGS) {
218 qemu_log_mask(LOG_GUEST_ERROR,
219 "%s: Out-of-bounds read at offset 0x%" HWADDR_PRIx "\n",
220 __func__, offset);
221 return 0;
224 switch (reg) {
225 case RNG_DATA:
226 /* On hardware, RNG_DATA works regardless of
227 * the state of the enable bit in RNG_CTRL
229 s->regs[RNG_DATA] = aspeed_scu_get_random();
230 break;
231 case WAKEUP_EN:
232 qemu_log_mask(LOG_GUEST_ERROR,
233 "%s: Read of write-only offset 0x%" HWADDR_PRIx "\n",
234 __func__, offset);
235 break;
238 return s->regs[reg];
241 static void aspeed_ast2400_scu_write(void *opaque, hwaddr offset,
242 uint64_t data, unsigned size)
244 AspeedSCUState *s = ASPEED_SCU(opaque);
245 int reg = TO_REG(offset);
247 if (reg >= ASPEED_SCU_NR_REGS) {
248 qemu_log_mask(LOG_GUEST_ERROR,
249 "%s: Out-of-bounds write at offset 0x%" HWADDR_PRIx "\n",
250 __func__, offset);
251 return;
254 if (reg > PROT_KEY && reg < CPU2_BASE_SEG1 &&
255 !s->regs[PROT_KEY]) {
256 qemu_log_mask(LOG_GUEST_ERROR, "%s: SCU is locked!\n", __func__);
259 trace_aspeed_scu_write(offset, size, data);
261 switch (reg) {
262 case PROT_KEY:
263 s->regs[reg] = (data == ASPEED_SCU_PROT_KEY) ? 1 : 0;
264 return;
265 case SILICON_REV:
266 case FREQ_CNTR_EVAL:
267 case VGA_SCRATCH1 ... VGA_SCRATCH8:
268 case RNG_DATA:
269 case FREE_CNTR4:
270 case FREE_CNTR4_EXT:
271 qemu_log_mask(LOG_GUEST_ERROR,
272 "%s: Write to read-only offset 0x%" HWADDR_PRIx "\n",
273 __func__, offset);
274 return;
277 s->regs[reg] = data;
280 static void aspeed_ast2500_scu_write(void *opaque, hwaddr offset,
281 uint64_t data, unsigned size)
283 AspeedSCUState *s = ASPEED_SCU(opaque);
284 int reg = TO_REG(offset);
286 if (reg >= ASPEED_SCU_NR_REGS) {
287 qemu_log_mask(LOG_GUEST_ERROR,
288 "%s: Out-of-bounds write at offset 0x%" HWADDR_PRIx "\n",
289 __func__, offset);
290 return;
293 if (reg > PROT_KEY && reg < CPU2_BASE_SEG1 &&
294 !s->regs[PROT_KEY]) {
295 qemu_log_mask(LOG_GUEST_ERROR, "%s: SCU is locked!\n", __func__);
296 return;
299 trace_aspeed_scu_write(offset, size, data);
301 switch (reg) {
302 case PROT_KEY:
303 s->regs[reg] = (data == ASPEED_SCU_PROT_KEY) ? 1 : 0;
304 return;
305 case HW_STRAP1:
306 s->regs[HW_STRAP1] |= data;
307 return;
308 case SILICON_REV:
309 s->regs[HW_STRAP1] &= ~data;
310 return;
311 case FREQ_CNTR_EVAL:
312 case VGA_SCRATCH1 ... VGA_SCRATCH8:
313 case RNG_DATA:
314 case FREE_CNTR4:
315 case FREE_CNTR4_EXT:
316 case CHIP_ID0:
317 case CHIP_ID1:
318 qemu_log_mask(LOG_GUEST_ERROR,
319 "%s: Write to read-only offset 0x%" HWADDR_PRIx "\n",
320 __func__, offset);
321 return;
324 s->regs[reg] = data;
327 static const MemoryRegionOps aspeed_ast2400_scu_ops = {
328 .read = aspeed_scu_read,
329 .write = aspeed_ast2400_scu_write,
330 .endianness = DEVICE_LITTLE_ENDIAN,
331 .valid = {
332 .min_access_size = 1,
333 .max_access_size = 4,
337 static const MemoryRegionOps aspeed_ast2500_scu_ops = {
338 .read = aspeed_scu_read,
339 .write = aspeed_ast2500_scu_write,
340 .endianness = DEVICE_LITTLE_ENDIAN,
341 .valid.min_access_size = 4,
342 .valid.max_access_size = 4,
343 .valid.unaligned = false,
346 static uint32_t aspeed_scu_get_clkin(AspeedSCUState *s)
348 if (s->hw_strap1 & SCU_HW_STRAP_CLK_25M_IN) {
349 return 25000000;
350 } else if (s->hw_strap1 & SCU_HW_STRAP_CLK_48M_IN) {
351 return 48000000;
352 } else {
353 return 24000000;
358 * Strapped frequencies for the AST2400 in MHz. They depend on the
359 * clkin frequency.
361 static const uint32_t hpll_ast2400_freqs[][4] = {
362 { 384, 360, 336, 408 }, /* 24MHz or 48MHz */
363 { 400, 375, 350, 425 }, /* 25MHz */
366 static uint32_t aspeed_2400_scu_calc_hpll(AspeedSCUState *s, uint32_t hpll_reg)
368 uint8_t freq_select;
369 bool clk_25m_in;
370 uint32_t clkin = aspeed_scu_get_clkin(s);
372 if (hpll_reg & SCU_AST2400_H_PLL_OFF) {
373 return 0;
376 if (hpll_reg & SCU_AST2400_H_PLL_PROGRAMMED) {
377 uint32_t multiplier = 1;
379 if (!(hpll_reg & SCU_AST2400_H_PLL_BYPASS_EN)) {
380 uint32_t n = (hpll_reg >> 5) & 0x3f;
381 uint32_t od = (hpll_reg >> 4) & 0x1;
382 uint32_t d = hpll_reg & 0xf;
384 multiplier = (2 - od) * ((n + 2) / (d + 1));
387 return clkin * multiplier;
390 /* HW strapping */
391 clk_25m_in = !!(s->hw_strap1 & SCU_HW_STRAP_CLK_25M_IN);
392 freq_select = SCU_AST2400_HW_STRAP_GET_H_PLL_CLK(s->hw_strap1);
394 return hpll_ast2400_freqs[clk_25m_in][freq_select] * 1000000;
397 static uint32_t aspeed_2500_scu_calc_hpll(AspeedSCUState *s, uint32_t hpll_reg)
399 uint32_t multiplier = 1;
400 uint32_t clkin = aspeed_scu_get_clkin(s);
402 if (hpll_reg & SCU_H_PLL_OFF) {
403 return 0;
406 if (!(hpll_reg & SCU_H_PLL_BYPASS_EN)) {
407 uint32_t p = (hpll_reg >> 13) & 0x3f;
408 uint32_t m = (hpll_reg >> 5) & 0xff;
409 uint32_t n = hpll_reg & 0x1f;
411 multiplier = ((m + 1) / (n + 1)) / (p + 1);
414 return clkin * multiplier;
417 static void aspeed_scu_reset(DeviceState *dev)
419 AspeedSCUState *s = ASPEED_SCU(dev);
420 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;
429 static uint32_t aspeed_silicon_revs[] = {
430 AST2400_A0_SILICON_REV,
431 AST2400_A1_SILICON_REV,
432 AST2500_A0_SILICON_REV,
433 AST2500_A1_SILICON_REV,
434 AST2600_A0_SILICON_REV,
435 AST2600_A1_SILICON_REV,
438 bool is_supported_silicon_rev(uint32_t silicon_rev)
440 int i;
442 for (i = 0; i < ARRAY_SIZE(aspeed_silicon_revs); i++) {
443 if (silicon_rev == aspeed_silicon_revs[i]) {
444 return true;
448 return false;
451 static void aspeed_scu_realize(DeviceState *dev, Error **errp)
453 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
454 AspeedSCUState *s = ASPEED_SCU(dev);
455 AspeedSCUClass *asc = ASPEED_SCU_GET_CLASS(dev);
457 if (!is_supported_silicon_rev(s->silicon_rev)) {
458 error_setg(errp, "Unknown silicon revision: 0x%" PRIx32,
459 s->silicon_rev);
460 return;
463 memory_region_init_io(&s->iomem, OBJECT(s), asc->ops, s,
464 TYPE_ASPEED_SCU, SCU_IO_REGION_SIZE);
466 sysbus_init_mmio(sbd, &s->iomem);
469 static const VMStateDescription vmstate_aspeed_scu = {
470 .name = "aspeed.scu",
471 .version_id = 2,
472 .minimum_version_id = 2,
473 .fields = (VMStateField[]) {
474 VMSTATE_UINT32_ARRAY(regs, AspeedSCUState, ASPEED_AST2600_SCU_NR_REGS),
475 VMSTATE_END_OF_LIST()
479 static Property aspeed_scu_properties[] = {
480 DEFINE_PROP_UINT32("silicon-rev", AspeedSCUState, silicon_rev, 0),
481 DEFINE_PROP_UINT32("hw-strap1", AspeedSCUState, hw_strap1, 0),
482 DEFINE_PROP_UINT32("hw-strap2", AspeedSCUState, hw_strap2, 0),
483 DEFINE_PROP_UINT32("hw-prot-key", AspeedSCUState, hw_prot_key, 0),
484 DEFINE_PROP_END_OF_LIST(),
487 static void aspeed_scu_class_init(ObjectClass *klass, void *data)
489 DeviceClass *dc = DEVICE_CLASS(klass);
490 dc->realize = aspeed_scu_realize;
491 dc->reset = aspeed_scu_reset;
492 dc->desc = "ASPEED System Control Unit";
493 dc->vmsd = &vmstate_aspeed_scu;
494 device_class_set_props(dc, aspeed_scu_properties);
497 static const TypeInfo aspeed_scu_info = {
498 .name = TYPE_ASPEED_SCU,
499 .parent = TYPE_SYS_BUS_DEVICE,
500 .instance_size = sizeof(AspeedSCUState),
501 .class_init = aspeed_scu_class_init,
502 .class_size = sizeof(AspeedSCUClass),
503 .abstract = true,
506 static void aspeed_2400_scu_class_init(ObjectClass *klass, void *data)
508 DeviceClass *dc = DEVICE_CLASS(klass);
509 AspeedSCUClass *asc = ASPEED_SCU_CLASS(klass);
511 dc->desc = "ASPEED 2400 System Control Unit";
512 asc->resets = ast2400_a0_resets;
513 asc->calc_hpll = aspeed_2400_scu_calc_hpll;
514 asc->apb_divider = 2;
515 asc->nr_regs = ASPEED_SCU_NR_REGS;
516 asc->ops = &aspeed_ast2400_scu_ops;
519 static const TypeInfo aspeed_2400_scu_info = {
520 .name = TYPE_ASPEED_2400_SCU,
521 .parent = TYPE_ASPEED_SCU,
522 .instance_size = sizeof(AspeedSCUState),
523 .class_init = aspeed_2400_scu_class_init,
526 static void aspeed_2500_scu_class_init(ObjectClass *klass, void *data)
528 DeviceClass *dc = DEVICE_CLASS(klass);
529 AspeedSCUClass *asc = ASPEED_SCU_CLASS(klass);
531 dc->desc = "ASPEED 2500 System Control Unit";
532 asc->resets = ast2500_a1_resets;
533 asc->calc_hpll = aspeed_2500_scu_calc_hpll;
534 asc->apb_divider = 4;
535 asc->nr_regs = ASPEED_SCU_NR_REGS;
536 asc->ops = &aspeed_ast2500_scu_ops;
539 static const TypeInfo aspeed_2500_scu_info = {
540 .name = TYPE_ASPEED_2500_SCU,
541 .parent = TYPE_ASPEED_SCU,
542 .instance_size = sizeof(AspeedSCUState),
543 .class_init = aspeed_2500_scu_class_init,
546 static uint64_t aspeed_ast2600_scu_read(void *opaque, hwaddr offset,
547 unsigned size)
549 AspeedSCUState *s = ASPEED_SCU(opaque);
550 int reg = TO_REG(offset);
552 if (reg >= ASPEED_AST2600_SCU_NR_REGS) {
553 qemu_log_mask(LOG_GUEST_ERROR,
554 "%s: Out-of-bounds read at offset 0x%" HWADDR_PRIx "\n",
555 __func__, offset);
556 return 0;
559 switch (reg) {
560 case AST2600_HPLL_EXT:
561 case AST2600_EPLL_EXT:
562 case AST2600_MPLL_EXT:
563 /* PLLs are always "locked" */
564 return s->regs[reg] | BIT(31);
565 case AST2600_RNG_DATA:
567 * On hardware, RNG_DATA works regardless of the state of the
568 * enable bit in RNG_CTRL
570 * TODO: Check this is true for ast2600
572 s->regs[AST2600_RNG_DATA] = aspeed_scu_get_random();
573 break;
576 return s->regs[reg];
579 static void aspeed_ast2600_scu_write(void *opaque, hwaddr offset,
580 uint64_t data64, unsigned size)
582 AspeedSCUState *s = ASPEED_SCU(opaque);
583 int reg = TO_REG(offset);
584 /* Truncate here so bitwise operations below behave as expected */
585 uint32_t data = data64;
587 if (reg >= ASPEED_AST2600_SCU_NR_REGS) {
588 qemu_log_mask(LOG_GUEST_ERROR,
589 "%s: Out-of-bounds write at offset 0x%" HWADDR_PRIx "\n",
590 __func__, offset);
591 return;
594 if (reg > PROT_KEY && !s->regs[PROT_KEY]) {
595 qemu_log_mask(LOG_GUEST_ERROR, "%s: SCU is locked!\n", __func__);
598 trace_aspeed_scu_write(offset, size, data);
600 switch (reg) {
601 case AST2600_PROT_KEY:
602 s->regs[reg] = (data == ASPEED_SCU_PROT_KEY) ? 1 : 0;
603 return;
604 case AST2600_HW_STRAP1:
605 case AST2600_HW_STRAP2:
606 if (s->regs[reg + 2]) {
607 return;
609 /* fall through */
610 case AST2600_SYS_RST_CTRL:
611 case AST2600_SYS_RST_CTRL2:
612 case AST2600_CLK_STOP_CTRL:
613 case AST2600_CLK_STOP_CTRL2:
614 /* W1S (Write 1 to set) registers */
615 s->regs[reg] |= data;
616 return;
617 case AST2600_SYS_RST_CTRL_CLR:
618 case AST2600_SYS_RST_CTRL2_CLR:
619 case AST2600_CLK_STOP_CTRL_CLR:
620 case AST2600_CLK_STOP_CTRL2_CLR:
621 case AST2600_HW_STRAP1_CLR:
622 case AST2600_HW_STRAP2_CLR:
624 * W1C (Write 1 to clear) registers are offset by one address from
625 * the data register
627 s->regs[reg - 1] &= ~data;
628 return;
630 case AST2600_RNG_DATA:
631 case AST2600_SILICON_REV:
632 case AST2600_SILICON_REV2:
633 case AST2600_CHIP_ID0:
634 case AST2600_CHIP_ID1:
635 /* Add read only registers here */
636 qemu_log_mask(LOG_GUEST_ERROR,
637 "%s: Write to read-only offset 0x%" HWADDR_PRIx "\n",
638 __func__, offset);
639 return;
642 s->regs[reg] = data;
645 static const MemoryRegionOps aspeed_ast2600_scu_ops = {
646 .read = aspeed_ast2600_scu_read,
647 .write = aspeed_ast2600_scu_write,
648 .endianness = DEVICE_LITTLE_ENDIAN,
649 .valid.min_access_size = 4,
650 .valid.max_access_size = 4,
651 .valid.unaligned = false,
654 static const uint32_t ast2600_a1_resets[ASPEED_AST2600_SCU_NR_REGS] = {
655 [AST2600_SYS_RST_CTRL] = 0xF7C3FED8,
656 [AST2600_SYS_RST_CTRL2] = 0xFFFFFFFC,
657 [AST2600_CLK_STOP_CTRL] = 0xFFFF7F8A,
658 [AST2600_CLK_STOP_CTRL2] = 0xFFF0FFF0,
659 [AST2600_SDRAM_HANDSHAKE] = 0x00000000,
660 [AST2600_HPLL_PARAM] = 0x1000405F,
661 [AST2600_CHIP_ID0] = 0x1234ABCD,
662 [AST2600_CHIP_ID1] = 0x88884444,
666 static void aspeed_ast2600_scu_reset(DeviceState *dev)
668 AspeedSCUState *s = ASPEED_SCU(dev);
669 AspeedSCUClass *asc = ASPEED_SCU_GET_CLASS(dev);
671 memcpy(s->regs, asc->resets, asc->nr_regs * 4);
674 * A0 reports A0 in _REV, but subsequent revisions report A1 regardless
675 * of actual revision. QEMU and Linux only support A1 onwards so this is
676 * sufficient.
678 s->regs[AST2600_SILICON_REV] = AST2600_A1_SILICON_REV;
679 s->regs[AST2600_SILICON_REV2] = s->silicon_rev;
680 s->regs[AST2600_HW_STRAP1] = s->hw_strap1;
681 s->regs[AST2600_HW_STRAP2] = s->hw_strap2;
682 s->regs[PROT_KEY] = s->hw_prot_key;
685 static void aspeed_2600_scu_class_init(ObjectClass *klass, void *data)
687 DeviceClass *dc = DEVICE_CLASS(klass);
688 AspeedSCUClass *asc = ASPEED_SCU_CLASS(klass);
690 dc->desc = "ASPEED 2600 System Control Unit";
691 dc->reset = aspeed_ast2600_scu_reset;
692 asc->resets = ast2600_a1_resets;
693 asc->calc_hpll = aspeed_2500_scu_calc_hpll; /* No change since AST2500 */
694 asc->apb_divider = 4;
695 asc->nr_regs = ASPEED_AST2600_SCU_NR_REGS;
696 asc->ops = &aspeed_ast2600_scu_ops;
699 static const TypeInfo aspeed_2600_scu_info = {
700 .name = TYPE_ASPEED_2600_SCU,
701 .parent = TYPE_ASPEED_SCU,
702 .instance_size = sizeof(AspeedSCUState),
703 .class_init = aspeed_2600_scu_class_init,
706 static void aspeed_scu_register_types(void)
708 type_register_static(&aspeed_scu_info);
709 type_register_static(&aspeed_2400_scu_info);
710 type_register_static(&aspeed_2500_scu_info);
711 type_register_static(&aspeed_2600_scu_info);
714 type_init(aspeed_scu_register_types);