xref: /openbmc/qemu/hw/misc/aspeed_scu.c (revision 88cd34ee)
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 
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"
23 
24 #define TO_REG(offset) ((offset) >> 2)
25 
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)
92 
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)
122 
123 #define AST2600_CLK TO_REG(0x40)
124 
125 #define SCU_IO_REGION_SIZE 0x1000
126 
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
157 };
158 
159 /* SCU70 bit 23: 0 24Mhz. bit 11:9: 0b001 AXI:ABH ratio 2:1 */
160 /* AST2500 revision A1 */
161 
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
194 };
195 
196 static uint32_t aspeed_scu_get_random(void)
197 {
198     uint32_t num;
199     qemu_guest_getrandom_nofail(&num, sizeof(num));
200     return num;
201 }
202 
203 uint32_t aspeed_scu_get_apb_freq(AspeedSCUState *s)
204 {
205     AspeedSCUClass *asc = ASPEED_SCU_GET_CLASS(s);
206     uint32_t hpll = asc->calc_hpll(s, s->regs[HPLL_PARAM]);
207 
208     return hpll / (SCU_CLK_GET_PCLK_DIV(s->regs[CLK_SEL]) + 1)
209         / asc->apb_divider;
210 }
211 
212 static uint64_t aspeed_scu_read(void *opaque, hwaddr offset, unsigned size)
213 {
214     AspeedSCUState *s = ASPEED_SCU(opaque);
215     int reg = TO_REG(offset);
216 
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;
222     }
223 
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
228          */
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;
236     }
237 
238     return s->regs[reg];
239 }
240 
241 static void aspeed_ast2400_scu_write(void *opaque, hwaddr offset,
242                                      uint64_t data, unsigned size)
243 {
244     AspeedSCUState *s = ASPEED_SCU(opaque);
245     int reg = TO_REG(offset);
246 
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;
252     }
253 
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__);
257     }
258 
259     trace_aspeed_scu_write(offset, size, data);
260 
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;
275     }
276 
277     s->regs[reg] = data;
278 }
279 
280 static void aspeed_ast2500_scu_write(void *opaque, hwaddr offset,
281                                      uint64_t data, unsigned size)
282 {
283     AspeedSCUState *s = ASPEED_SCU(opaque);
284     int reg = TO_REG(offset);
285 
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;
291     }
292 
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;
297     }
298 
299     trace_aspeed_scu_write(offset, size, data);
300 
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;
322     }
323 
324     s->regs[reg] = data;
325 }
326 
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.min_access_size = 4,
332     .valid.max_access_size = 4,
333     .valid.unaligned = false,
334 };
335 
336 static const MemoryRegionOps aspeed_ast2500_scu_ops = {
337     .read = aspeed_scu_read,
338     .write = aspeed_ast2500_scu_write,
339     .endianness = DEVICE_LITTLE_ENDIAN,
340     .valid.min_access_size = 4,
341     .valid.max_access_size = 4,
342     .valid.unaligned = false,
343 };
344 
345 static uint32_t aspeed_scu_get_clkin(AspeedSCUState *s)
346 {
347     if (s->hw_strap1 & SCU_HW_STRAP_CLK_25M_IN) {
348         return 25000000;
349     } else if (s->hw_strap1 & SCU_HW_STRAP_CLK_48M_IN) {
350         return 48000000;
351     } else {
352         return 24000000;
353     }
354 }
355 
356 /*
357  * Strapped frequencies for the AST2400 in MHz. They depend on the
358  * clkin frequency.
359  */
360 static const uint32_t hpll_ast2400_freqs[][4] = {
361     { 384, 360, 336, 408 }, /* 24MHz or 48MHz */
362     { 400, 375, 350, 425 }, /* 25MHz */
363 };
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 
371     if (hpll_reg & SCU_AST2400_H_PLL_OFF) {
372         return 0;
373     }
374 
375     if (hpll_reg & SCU_AST2400_H_PLL_PROGRAMMED) {
376         uint32_t multiplier = 1;
377 
378         if (!(hpll_reg & SCU_AST2400_H_PLL_BYPASS_EN)) {
379             uint32_t n  = (hpll_reg >> 5) & 0x3f;
380             uint32_t od = (hpll_reg >> 4) & 0x1;
381             uint32_t d  = hpll_reg & 0xf;
382 
383             multiplier = (2 - od) * ((n + 2) / (d + 1));
384         }
385 
386         return clkin * multiplier;
387     }
388 
389     /* HW strapping */
390     clk_25m_in = !!(s->hw_strap1 & SCU_HW_STRAP_CLK_25M_IN);
391     freq_select = SCU_AST2400_HW_STRAP_GET_H_PLL_CLK(s->hw_strap1);
392 
393     return hpll_ast2400_freqs[clk_25m_in][freq_select] * 1000000;
394 }
395 
396 static uint32_t aspeed_2500_scu_calc_hpll(AspeedSCUState *s, uint32_t hpll_reg)
397 {
398     uint32_t multiplier = 1;
399     uint32_t clkin = aspeed_scu_get_clkin(s);
400 
401     if (hpll_reg & SCU_H_PLL_OFF) {
402         return 0;
403     }
404 
405     if (!(hpll_reg & SCU_H_PLL_BYPASS_EN)) {
406         uint32_t p = (hpll_reg >> 13) & 0x3f;
407         uint32_t m = (hpll_reg >> 5) & 0xff;
408         uint32_t n = hpll_reg & 0x1f;
409 
410         multiplier = ((m + 1) / (n + 1)) / (p + 1);
411     }
412 
413     return clkin * multiplier;
414 }
415 
416 static void aspeed_scu_reset(DeviceState *dev)
417 {
418     AspeedSCUState *s = ASPEED_SCU(dev);
419     AspeedSCUClass *asc = ASPEED_SCU_GET_CLASS(dev);
420 
421     memcpy(s->regs, asc->resets, asc->nr_regs * 4);
422     s->regs[SILICON_REV] = s->silicon_rev;
423     s->regs[HW_STRAP1] = s->hw_strap1;
424     s->regs[HW_STRAP2] = s->hw_strap2;
425     s->regs[PROT_KEY] = s->hw_prot_key;
426 }
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 };
435 
436 bool is_supported_silicon_rev(uint32_t silicon_rev)
437 {
438     int i;
439 
440     for (i = 0; i < ARRAY_SIZE(aspeed_silicon_revs); i++) {
441         if (silicon_rev == aspeed_silicon_revs[i]) {
442             return true;
443         }
444     }
445 
446     return false;
447 }
448 
449 static void aspeed_scu_realize(DeviceState *dev, Error **errp)
450 {
451     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
452     AspeedSCUState *s = ASPEED_SCU(dev);
453     AspeedSCUClass *asc = ASPEED_SCU_GET_CLASS(dev);
454 
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 
461     memory_region_init_io(&s->iomem, OBJECT(s), asc->ops, s,
462                           TYPE_ASPEED_SCU, SCU_IO_REGION_SIZE);
463 
464     sysbus_init_mmio(sbd, &s->iomem);
465 }
466 
467 static const VMStateDescription vmstate_aspeed_scu = {
468     .name = "aspeed.scu",
469     .version_id = 2,
470     .minimum_version_id = 2,
471     .fields = (VMStateField[]) {
472         VMSTATE_UINT32_ARRAY(regs, AspeedSCUState, ASPEED_AST2600_SCU_NR_REGS),
473         VMSTATE_END_OF_LIST()
474     }
475 };
476 
477 static Property aspeed_scu_properties[] = {
478     DEFINE_PROP_UINT32("silicon-rev", AspeedSCUState, silicon_rev, 0),
479     DEFINE_PROP_UINT32("hw-strap1", AspeedSCUState, hw_strap1, 0),
480     DEFINE_PROP_UINT32("hw-strap2", AspeedSCUState, hw_strap2, 0),
481     DEFINE_PROP_UINT32("hw-prot-key", AspeedSCUState, hw_prot_key, 0),
482     DEFINE_PROP_END_OF_LIST(),
483 };
484 
485 static void aspeed_scu_class_init(ObjectClass *klass, void *data)
486 {
487     DeviceClass *dc = DEVICE_CLASS(klass);
488     dc->realize = aspeed_scu_realize;
489     dc->reset = aspeed_scu_reset;
490     dc->desc = "ASPEED System Control Unit";
491     dc->vmsd = &vmstate_aspeed_scu;
492     device_class_set_props(dc, aspeed_scu_properties);
493 }
494 
495 static const TypeInfo aspeed_scu_info = {
496     .name = TYPE_ASPEED_SCU,
497     .parent = TYPE_SYS_BUS_DEVICE,
498     .instance_size = sizeof(AspeedSCUState),
499     .class_init = aspeed_scu_class_init,
500     .class_size    = sizeof(AspeedSCUClass),
501     .abstract      = true,
502 };
503 
504 static void aspeed_2400_scu_class_init(ObjectClass *klass, void *data)
505 {
506     DeviceClass *dc = DEVICE_CLASS(klass);
507     AspeedSCUClass *asc = ASPEED_SCU_CLASS(klass);
508 
509     dc->desc = "ASPEED 2400 System Control Unit";
510     asc->resets = ast2400_a0_resets;
511     asc->calc_hpll = aspeed_2400_scu_calc_hpll;
512     asc->apb_divider = 2;
513     asc->nr_regs = ASPEED_SCU_NR_REGS;
514     asc->ops = &aspeed_ast2400_scu_ops;
515 }
516 
517 static const TypeInfo aspeed_2400_scu_info = {
518     .name = TYPE_ASPEED_2400_SCU,
519     .parent = TYPE_ASPEED_SCU,
520     .instance_size = sizeof(AspeedSCUState),
521     .class_init = aspeed_2400_scu_class_init,
522 };
523 
524 static void aspeed_2500_scu_class_init(ObjectClass *klass, void *data)
525 {
526     DeviceClass *dc = DEVICE_CLASS(klass);
527     AspeedSCUClass *asc = ASPEED_SCU_CLASS(klass);
528 
529     dc->desc = "ASPEED 2500 System Control Unit";
530     asc->resets = ast2500_a1_resets;
531     asc->calc_hpll = aspeed_2500_scu_calc_hpll;
532     asc->apb_divider = 4;
533     asc->nr_regs = ASPEED_SCU_NR_REGS;
534     asc->ops = &aspeed_ast2500_scu_ops;
535 }
536 
537 static const TypeInfo aspeed_2500_scu_info = {
538     .name = TYPE_ASPEED_2500_SCU,
539     .parent = TYPE_ASPEED_SCU,
540     .instance_size = sizeof(AspeedSCUState),
541     .class_init = aspeed_2500_scu_class_init,
542 };
543 
544 static uint64_t aspeed_ast2600_scu_read(void *opaque, hwaddr offset,
545                                         unsigned size)
546 {
547     AspeedSCUState *s = ASPEED_SCU(opaque);
548     int reg = TO_REG(offset);
549 
550     if (reg >= ASPEED_AST2600_SCU_NR_REGS) {
551         qemu_log_mask(LOG_GUEST_ERROR,
552                       "%s: Out-of-bounds read at offset 0x%" HWADDR_PRIx "\n",
553                       __func__, offset);
554         return 0;
555     }
556 
557     switch (reg) {
558     case AST2600_HPLL_EXT:
559     case AST2600_EPLL_EXT:
560     case AST2600_MPLL_EXT:
561         /* PLLs are always "locked" */
562         return s->regs[reg] | BIT(31);
563     case AST2600_RNG_DATA:
564         /*
565          * On hardware, RNG_DATA works regardless of the state of the
566          * enable bit in RNG_CTRL
567          *
568          * TODO: Check this is true for ast2600
569          */
570         s->regs[AST2600_RNG_DATA] = aspeed_scu_get_random();
571         break;
572     }
573 
574     return s->regs[reg];
575 }
576 
577 static void aspeed_ast2600_scu_write(void *opaque, hwaddr offset,
578                                      uint64_t data64, unsigned size)
579 {
580     AspeedSCUState *s = ASPEED_SCU(opaque);
581     int reg = TO_REG(offset);
582     /* Truncate here so bitwise operations below behave as expected */
583     uint32_t data = data64;
584 
585     if (reg >= ASPEED_AST2600_SCU_NR_REGS) {
586         qemu_log_mask(LOG_GUEST_ERROR,
587                       "%s: Out-of-bounds write at offset 0x%" HWADDR_PRIx "\n",
588                       __func__, offset);
589         return;
590     }
591 
592     if (reg > PROT_KEY && !s->regs[PROT_KEY]) {
593         qemu_log_mask(LOG_GUEST_ERROR, "%s: SCU is locked!\n", __func__);
594     }
595 
596     trace_aspeed_scu_write(offset, size, data);
597 
598     switch (reg) {
599     case AST2600_PROT_KEY:
600         s->regs[reg] = (data == ASPEED_SCU_PROT_KEY) ? 1 : 0;
601         return;
602     case AST2600_HW_STRAP1:
603     case AST2600_HW_STRAP2:
604         if (s->regs[reg + 2]) {
605             return;
606         }
607         /* fall through */
608     case AST2600_SYS_RST_CTRL:
609     case AST2600_SYS_RST_CTRL2:
610     case AST2600_CLK_STOP_CTRL:
611     case AST2600_CLK_STOP_CTRL2:
612         /* W1S (Write 1 to set) registers */
613         s->regs[reg] |= data;
614         return;
615     case AST2600_SYS_RST_CTRL_CLR:
616     case AST2600_SYS_RST_CTRL2_CLR:
617     case AST2600_CLK_STOP_CTRL_CLR:
618     case AST2600_CLK_STOP_CTRL2_CLR:
619     case AST2600_HW_STRAP1_CLR:
620     case AST2600_HW_STRAP2_CLR:
621         /*
622          * W1C (Write 1 to clear) registers are offset by one address from
623          * the data register
624          */
625         s->regs[reg - 1] &= ~data;
626         return;
627 
628     case AST2600_RNG_DATA:
629     case AST2600_SILICON_REV:
630     case AST2600_SILICON_REV2:
631     case AST2600_CHIP_ID0:
632     case AST2600_CHIP_ID1:
633         /* Add read only registers here */
634         qemu_log_mask(LOG_GUEST_ERROR,
635                       "%s: Write to read-only offset 0x%" HWADDR_PRIx "\n",
636                       __func__, offset);
637         return;
638     }
639 
640     s->regs[reg] = data;
641 }
642 
643 static const MemoryRegionOps aspeed_ast2600_scu_ops = {
644     .read = aspeed_ast2600_scu_read,
645     .write = aspeed_ast2600_scu_write,
646     .endianness = DEVICE_LITTLE_ENDIAN,
647     .valid.min_access_size = 4,
648     .valid.max_access_size = 4,
649     .valid.unaligned = false,
650 };
651 
652 static const uint32_t ast2600_a0_resets[ASPEED_AST2600_SCU_NR_REGS] = {
653     [AST2600_SILICON_REV]       = AST2600_SILICON_REV,
654     [AST2600_SILICON_REV2]      = AST2600_SILICON_REV,
655     [AST2600_SYS_RST_CTRL]      = 0xF7CFFEDC | 0x100,
656     [AST2600_SYS_RST_CTRL2]     = 0xFFFFFFFC,
657     [AST2600_CLK_STOP_CTRL]     = 0xEFF43E8B,
658     [AST2600_CLK_STOP_CTRL2]    = 0xFFF0FFF0,
659     [AST2600_SDRAM_HANDSHAKE]   = 0x00000040,  /* SoC completed DRAM init */
660     [AST2600_HPLL_PARAM]        = 0x1000405F,
661     [AST2600_CHIP_ID0]          = 0x1234ABCD,
662     [AST2600_CHIP_ID1]          = 0x88884444,
663 
664 };
665 
666 static void aspeed_ast2600_scu_reset(DeviceState *dev)
667 {
668     AspeedSCUState *s = ASPEED_SCU(dev);
669     AspeedSCUClass *asc = ASPEED_SCU_GET_CLASS(dev);
670 
671     memcpy(s->regs, asc->resets, asc->nr_regs * 4);
672 
673     s->regs[AST2600_SILICON_REV] = s->silicon_rev;
674     s->regs[AST2600_SILICON_REV2] = s->silicon_rev;
675     s->regs[AST2600_HW_STRAP1] = s->hw_strap1;
676     s->regs[AST2600_HW_STRAP2] = s->hw_strap2;
677     s->regs[PROT_KEY] = s->hw_prot_key;
678 }
679 
680 static void aspeed_2600_scu_class_init(ObjectClass *klass, void *data)
681 {
682     DeviceClass *dc = DEVICE_CLASS(klass);
683     AspeedSCUClass *asc = ASPEED_SCU_CLASS(klass);
684 
685     dc->desc = "ASPEED 2600 System Control Unit";
686     dc->reset = aspeed_ast2600_scu_reset;
687     asc->resets = ast2600_a0_resets;
688     asc->calc_hpll = aspeed_2500_scu_calc_hpll; /* No change since AST2500 */
689     asc->apb_divider = 4;
690     asc->nr_regs = ASPEED_AST2600_SCU_NR_REGS;
691     asc->ops = &aspeed_ast2600_scu_ops;
692 }
693 
694 static const TypeInfo aspeed_2600_scu_info = {
695     .name = TYPE_ASPEED_2600_SCU,
696     .parent = TYPE_ASPEED_SCU,
697     .instance_size = sizeof(AspeedSCUState),
698     .class_init = aspeed_2600_scu_class_init,
699 };
700 
701 static void aspeed_scu_register_types(void)
702 {
703     type_register_static(&aspeed_scu_info);
704     type_register_static(&aspeed_2400_scu_info);
705     type_register_static(&aspeed_2500_scu_info);
706     type_register_static(&aspeed_2600_scu_info);
707 }
708 
709 type_init(aspeed_scu_register_types);
710