2 * ASPEED SDRAM Memory Controller
4 * Copyright (C) 2016 IBM Corp.
6 * This code is licensed under the GPL version 2 or later. See
7 * the COPYING file in the top-level directory.
10 #include "qemu/osdep.h"
12 #include "qemu/module.h"
13 #include "qemu/error-report.h"
14 #include "hw/misc/aspeed_sdmc.h"
15 #include "hw/misc/aspeed_scu.h"
16 #include "hw/qdev-properties.h"
17 #include "migration/vmstate.h"
18 #include "qapi/error.h"
20 #include "qemu/units.h"
21 #include "qemu/cutils.h"
22 #include "qapi/visitor.h"
24 /* Protection Key Register */
25 #define R_PROT (0x00 / 4)
26 #define PROT_UNLOCKED 0x01
27 #define PROT_HARDLOCKED 0x10 /* AST2600 */
28 #define PROT_SOFTLOCKED 0x00
30 #define PROT_KEY_UNLOCK 0xFC600309
31 #define PROT_KEY_HARDLOCK 0xDEADDEAD /* AST2600 */
33 /* Configuration Register */
34 #define R_CONF (0x04 / 4)
36 /* Control/Status Register #1 (ast2500) */
37 #define R_STATUS1 (0x60 / 4)
38 #define PHY_BUSY_STATE BIT(0)
39 #define PHY_PLL_LOCK_STATUS BIT(4)
41 #define R_ECC_TEST_CTRL (0x70 / 4)
42 #define ECC_TEST_FINISHED BIT(12)
43 #define ECC_TEST_FAIL BIT(13)
46 * Configuration register Ox4 (for Aspeed AST2400 SOC)
48 * These are for the record and future use. ASPEED_SDMC_DRAM_SIZE is
49 * what we care about right now as it is checked by U-Boot to
50 * determine the RAM size.
53 #define ASPEED_SDMC_RESERVED 0xFFFFF800 /* 31:11 reserved */
54 #define ASPEED_SDMC_AST2300_COMPAT (1 << 10)
55 #define ASPEED_SDMC_SCRAMBLE_PATTERN (1 << 9)
56 #define ASPEED_SDMC_DATA_SCRAMBLE (1 << 8)
57 #define ASPEED_SDMC_ECC_ENABLE (1 << 7)
58 #define ASPEED_SDMC_VGA_COMPAT (1 << 6) /* readonly */
59 #define ASPEED_SDMC_DRAM_BANK (1 << 5)
60 #define ASPEED_SDMC_DRAM_BURST (1 << 4)
61 #define ASPEED_SDMC_VGA_APERTURE(x) ((x & 0x3) << 2) /* readonly */
62 #define ASPEED_SDMC_VGA_8MB 0x0
63 #define ASPEED_SDMC_VGA_16MB 0x1
64 #define ASPEED_SDMC_VGA_32MB 0x2
65 #define ASPEED_SDMC_VGA_64MB 0x3
66 #define ASPEED_SDMC_DRAM_SIZE(x) (x & 0x3)
67 #define ASPEED_SDMC_DRAM_64MB 0x0
68 #define ASPEED_SDMC_DRAM_128MB 0x1
69 #define ASPEED_SDMC_DRAM_256MB 0x2
70 #define ASPEED_SDMC_DRAM_512MB 0x3
72 #define ASPEED_SDMC_READONLY_MASK \
73 (ASPEED_SDMC_RESERVED | ASPEED_SDMC_VGA_COMPAT | \
74 ASPEED_SDMC_VGA_APERTURE(ASPEED_SDMC_VGA_64MB))
76 * Configuration register Ox4 (for Aspeed AST2500 SOC and higher)
78 * Incompatibilities are annotated in the list. ASPEED_SDMC_HW_VERSION
79 * should be set to 1 for the AST2500 SOC.
81 #define ASPEED_SDMC_HW_VERSION(x) ((x & 0xf) << 28) /* readonly */
82 #define ASPEED_SDMC_SW_VERSION ((x & 0xff) << 20)
83 #define ASPEED_SDMC_CACHE_INITIAL_DONE (1 << 19) /* readonly */
84 #define ASPEED_SDMC_AST2500_RESERVED 0x7C000 /* 18:14 reserved */
85 #define ASPEED_SDMC_CACHE_DDR4_CONF (1 << 13)
86 #define ASPEED_SDMC_CACHE_INITIAL (1 << 12)
87 #define ASPEED_SDMC_CACHE_RANGE_CTRL (1 << 11)
88 #define ASPEED_SDMC_CACHE_ENABLE (1 << 10) /* differs from AST2400 */
89 #define ASPEED_SDMC_DRAM_TYPE (1 << 4) /* differs from AST2400 */
91 /* DRAM size definitions differs */
92 #define ASPEED_SDMC_AST2500_128MB 0x0
93 #define ASPEED_SDMC_AST2500_256MB 0x1
94 #define ASPEED_SDMC_AST2500_512MB 0x2
95 #define ASPEED_SDMC_AST2500_1024MB 0x3
97 #define ASPEED_SDMC_AST2600_256MB 0x0
98 #define ASPEED_SDMC_AST2600_512MB 0x1
99 #define ASPEED_SDMC_AST2600_1024MB 0x2
100 #define ASPEED_SDMC_AST2600_2048MB 0x3
102 #define ASPEED_SDMC_AST2500_READONLY_MASK \
103 (ASPEED_SDMC_HW_VERSION(0xf) | ASPEED_SDMC_CACHE_INITIAL_DONE | \
104 ASPEED_SDMC_AST2500_RESERVED | ASPEED_SDMC_VGA_COMPAT | \
105 ASPEED_SDMC_VGA_APERTURE(ASPEED_SDMC_VGA_64MB))
107 static uint64_t aspeed_sdmc_read(void *opaque
, hwaddr addr
, unsigned size
)
109 AspeedSDMCState
*s
= ASPEED_SDMC(opaque
);
113 if (addr
>= ARRAY_SIZE(s
->regs
)) {
114 qemu_log_mask(LOG_GUEST_ERROR
,
115 "%s: Out-of-bounds read at offset 0x%" HWADDR_PRIx
"\n",
120 return s
->regs
[addr
];
123 static void aspeed_sdmc_write(void *opaque
, hwaddr addr
, uint64_t data
,
126 AspeedSDMCState
*s
= ASPEED_SDMC(opaque
);
127 AspeedSDMCClass
*asc
= ASPEED_SDMC_GET_CLASS(s
);
131 if (addr
>= ARRAY_SIZE(s
->regs
)) {
132 qemu_log_mask(LOG_GUEST_ERROR
,
133 "%s: Out-of-bounds write at offset 0x%" HWADDR_PRIx
"\n",
138 asc
->write(s
, addr
, data
);
141 static const MemoryRegionOps aspeed_sdmc_ops
= {
142 .read
= aspeed_sdmc_read
,
143 .write
= aspeed_sdmc_write
,
144 .endianness
= DEVICE_LITTLE_ENDIAN
,
145 .valid
.min_access_size
= 4,
146 .valid
.max_access_size
= 4,
149 static int ast2400_rambits(AspeedSDMCState
*s
)
151 switch (s
->ram_size
>> 20) {
153 return ASPEED_SDMC_DRAM_64MB
;
155 return ASPEED_SDMC_DRAM_128MB
;
157 return ASPEED_SDMC_DRAM_256MB
;
159 return ASPEED_SDMC_DRAM_512MB
;
161 g_assert_not_reached();
166 static int ast2500_rambits(AspeedSDMCState
*s
)
168 switch (s
->ram_size
>> 20) {
170 return ASPEED_SDMC_AST2500_128MB
;
172 return ASPEED_SDMC_AST2500_256MB
;
174 return ASPEED_SDMC_AST2500_512MB
;
176 return ASPEED_SDMC_AST2500_1024MB
;
178 g_assert_not_reached();
183 static int ast2600_rambits(AspeedSDMCState
*s
)
185 switch (s
->ram_size
>> 20) {
187 return ASPEED_SDMC_AST2600_256MB
;
189 return ASPEED_SDMC_AST2600_512MB
;
191 return ASPEED_SDMC_AST2600_1024MB
;
193 return ASPEED_SDMC_AST2600_2048MB
;
195 g_assert_not_reached();
200 static void aspeed_sdmc_reset(DeviceState
*dev
)
202 AspeedSDMCState
*s
= ASPEED_SDMC(dev
);
203 AspeedSDMCClass
*asc
= ASPEED_SDMC_GET_CLASS(s
);
205 memset(s
->regs
, 0, sizeof(s
->regs
));
207 /* Set ram size bit and defaults values */
208 s
->regs
[R_CONF
] = asc
->compute_conf(s
, 0);
211 static void aspeed_sdmc_get_ram_size(Object
*obj
, Visitor
*v
, const char *name
,
212 void *opaque
, Error
**errp
)
214 AspeedSDMCState
*s
= ASPEED_SDMC(obj
);
215 int64_t value
= s
->ram_size
;
217 visit_type_int(v
, name
, &value
, errp
);
220 static void aspeed_sdmc_set_ram_size(Object
*obj
, Visitor
*v
, const char *name
,
221 void *opaque
, Error
**errp
)
226 AspeedSDMCState
*s
= ASPEED_SDMC(obj
);
227 AspeedSDMCClass
*asc
= ASPEED_SDMC_GET_CLASS(s
);
229 if (!visit_type_int(v
, name
, &value
, errp
)) {
233 for (i
= 0; asc
->valid_ram_sizes
[i
]; i
++) {
234 if (value
== asc
->valid_ram_sizes
[i
]) {
240 sz
= size_to_str(value
);
241 error_setg(errp
, "Invalid RAM size %s", sz
);
245 static void aspeed_sdmc_initfn(Object
*obj
)
247 object_property_add(obj
, "ram-size", "int",
248 aspeed_sdmc_get_ram_size
, aspeed_sdmc_set_ram_size
,
252 static void aspeed_sdmc_realize(DeviceState
*dev
, Error
**errp
)
254 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
255 AspeedSDMCState
*s
= ASPEED_SDMC(dev
);
256 AspeedSDMCClass
*asc
= ASPEED_SDMC_GET_CLASS(s
);
258 s
->max_ram_size
= asc
->max_ram_size
;
260 memory_region_init_io(&s
->iomem
, OBJECT(s
), &aspeed_sdmc_ops
, s
,
261 TYPE_ASPEED_SDMC
, 0x1000);
262 sysbus_init_mmio(sbd
, &s
->iomem
);
265 static const VMStateDescription vmstate_aspeed_sdmc
= {
266 .name
= "aspeed.sdmc",
268 .minimum_version_id
= 1,
269 .fields
= (VMStateField
[]) {
270 VMSTATE_UINT32_ARRAY(regs
, AspeedSDMCState
, ASPEED_SDMC_NR_REGS
),
271 VMSTATE_END_OF_LIST()
275 static Property aspeed_sdmc_properties
[] = {
276 DEFINE_PROP_UINT64("max-ram-size", AspeedSDMCState
, max_ram_size
, 0),
277 DEFINE_PROP_END_OF_LIST(),
280 static void aspeed_sdmc_class_init(ObjectClass
*klass
, void *data
)
282 DeviceClass
*dc
= DEVICE_CLASS(klass
);
283 dc
->realize
= aspeed_sdmc_realize
;
284 dc
->reset
= aspeed_sdmc_reset
;
285 dc
->desc
= "ASPEED SDRAM Memory Controller";
286 dc
->vmsd
= &vmstate_aspeed_sdmc
;
287 device_class_set_props(dc
, aspeed_sdmc_properties
);
290 static const TypeInfo aspeed_sdmc_info
= {
291 .name
= TYPE_ASPEED_SDMC
,
292 .parent
= TYPE_SYS_BUS_DEVICE
,
293 .instance_size
= sizeof(AspeedSDMCState
),
294 .instance_init
= aspeed_sdmc_initfn
,
295 .class_init
= aspeed_sdmc_class_init
,
296 .class_size
= sizeof(AspeedSDMCClass
),
300 static uint32_t aspeed_2400_sdmc_compute_conf(AspeedSDMCState
*s
, uint32_t data
)
302 uint32_t fixed_conf
= ASPEED_SDMC_VGA_COMPAT
|
303 ASPEED_SDMC_DRAM_SIZE(ast2400_rambits(s
));
305 /* Make sure readonly bits are kept */
306 data
&= ~ASPEED_SDMC_READONLY_MASK
;
308 return data
| fixed_conf
;
311 static void aspeed_2400_sdmc_write(AspeedSDMCState
*s
, uint32_t reg
,
315 s
->regs
[reg
] = (data
== PROT_KEY_UNLOCK
) ? PROT_UNLOCKED
: PROT_SOFTLOCKED
;
319 if (!s
->regs
[R_PROT
]) {
320 qemu_log_mask(LOG_GUEST_ERROR
, "%s: SDMC is locked!\n", __func__
);
326 data
= aspeed_2400_sdmc_compute_conf(s
, data
);
335 static const uint64_t
336 aspeed_2400_ram_sizes
[] = { 64 * MiB
, 128 * MiB
, 256 * MiB
, 512 * MiB
, 0};
338 static void aspeed_2400_sdmc_class_init(ObjectClass
*klass
, void *data
)
340 DeviceClass
*dc
= DEVICE_CLASS(klass
);
341 AspeedSDMCClass
*asc
= ASPEED_SDMC_CLASS(klass
);
343 dc
->desc
= "ASPEED 2400 SDRAM Memory Controller";
344 asc
->max_ram_size
= 512 << 20;
345 asc
->compute_conf
= aspeed_2400_sdmc_compute_conf
;
346 asc
->write
= aspeed_2400_sdmc_write
;
347 asc
->valid_ram_sizes
= aspeed_2400_ram_sizes
;
350 static const TypeInfo aspeed_2400_sdmc_info
= {
351 .name
= TYPE_ASPEED_2400_SDMC
,
352 .parent
= TYPE_ASPEED_SDMC
,
353 .class_init
= aspeed_2400_sdmc_class_init
,
356 static uint32_t aspeed_2500_sdmc_compute_conf(AspeedSDMCState
*s
, uint32_t data
)
358 uint32_t fixed_conf
= ASPEED_SDMC_HW_VERSION(1) |
359 ASPEED_SDMC_VGA_APERTURE(ASPEED_SDMC_VGA_64MB
) |
360 ASPEED_SDMC_CACHE_INITIAL_DONE
|
361 ASPEED_SDMC_DRAM_SIZE(ast2500_rambits(s
));
363 /* Make sure readonly bits are kept */
364 data
&= ~ASPEED_SDMC_AST2500_READONLY_MASK
;
366 return data
| fixed_conf
;
369 static void aspeed_2500_sdmc_write(AspeedSDMCState
*s
, uint32_t reg
,
373 s
->regs
[reg
] = (data
== PROT_KEY_UNLOCK
) ? PROT_UNLOCKED
: PROT_SOFTLOCKED
;
377 if (!s
->regs
[R_PROT
]) {
378 qemu_log_mask(LOG_GUEST_ERROR
, "%s: SDMC is locked!\n", __func__
);
384 data
= aspeed_2500_sdmc_compute_conf(s
, data
);
387 /* Will never return 'busy' */
388 data
&= ~PHY_BUSY_STATE
;
390 case R_ECC_TEST_CTRL
:
391 /* Always done, always happy */
392 data
|= ECC_TEST_FINISHED
;
393 data
&= ~ECC_TEST_FAIL
;
402 static const uint64_t
403 aspeed_2500_ram_sizes
[] = { 128 * MiB
, 256 * MiB
, 512 * MiB
, 1024 * MiB
, 0};
405 static void aspeed_2500_sdmc_class_init(ObjectClass
*klass
, void *data
)
407 DeviceClass
*dc
= DEVICE_CLASS(klass
);
408 AspeedSDMCClass
*asc
= ASPEED_SDMC_CLASS(klass
);
410 dc
->desc
= "ASPEED 2500 SDRAM Memory Controller";
411 asc
->max_ram_size
= 1024 << 20;
412 asc
->compute_conf
= aspeed_2500_sdmc_compute_conf
;
413 asc
->write
= aspeed_2500_sdmc_write
;
414 asc
->valid_ram_sizes
= aspeed_2500_ram_sizes
;
417 static const TypeInfo aspeed_2500_sdmc_info
= {
418 .name
= TYPE_ASPEED_2500_SDMC
,
419 .parent
= TYPE_ASPEED_SDMC
,
420 .class_init
= aspeed_2500_sdmc_class_init
,
423 static uint32_t aspeed_2600_sdmc_compute_conf(AspeedSDMCState
*s
, uint32_t data
)
425 uint32_t fixed_conf
= ASPEED_SDMC_HW_VERSION(3) |
426 ASPEED_SDMC_VGA_APERTURE(ASPEED_SDMC_VGA_64MB
) |
427 ASPEED_SDMC_DRAM_SIZE(ast2600_rambits(s
));
429 /* Make sure readonly bits are kept (use ast2500 mask) */
430 data
&= ~ASPEED_SDMC_AST2500_READONLY_MASK
;
432 return data
| fixed_conf
;
435 static void aspeed_2600_sdmc_write(AspeedSDMCState
*s
, uint32_t reg
,
438 if (s
->regs
[R_PROT
] == PROT_HARDLOCKED
) {
439 qemu_log_mask(LOG_GUEST_ERROR
, "%s: SDMC is locked until system reset!\n",
444 if (reg
!= R_PROT
&& s
->regs
[R_PROT
] == PROT_SOFTLOCKED
) {
445 qemu_log_mask(LOG_GUEST_ERROR
, "%s: SDMC is locked!\n", __func__
);
451 if (data
== PROT_KEY_UNLOCK
) {
452 data
= PROT_UNLOCKED
;
453 } else if (data
== PROT_KEY_HARDLOCK
) {
454 data
= PROT_HARDLOCKED
;
456 data
= PROT_SOFTLOCKED
;
460 data
= aspeed_2600_sdmc_compute_conf(s
, data
);
463 /* Will never return 'busy'. 'lock status' is always set */
464 data
&= ~PHY_BUSY_STATE
;
465 data
|= PHY_PLL_LOCK_STATUS
;
467 case R_ECC_TEST_CTRL
:
468 /* Always done, always happy */
469 data
|= ECC_TEST_FINISHED
;
470 data
&= ~ECC_TEST_FAIL
;
479 static const uint64_t
480 aspeed_2600_ram_sizes
[] = { 256 * MiB
, 512 * MiB
, 1024 * MiB
, 2048 * MiB
, 0};
482 static void aspeed_2600_sdmc_class_init(ObjectClass
*klass
, void *data
)
484 DeviceClass
*dc
= DEVICE_CLASS(klass
);
485 AspeedSDMCClass
*asc
= ASPEED_SDMC_CLASS(klass
);
487 dc
->desc
= "ASPEED 2600 SDRAM Memory Controller";
488 asc
->max_ram_size
= 2048 << 20;
489 asc
->compute_conf
= aspeed_2600_sdmc_compute_conf
;
490 asc
->write
= aspeed_2600_sdmc_write
;
491 asc
->valid_ram_sizes
= aspeed_2600_ram_sizes
;
494 static const TypeInfo aspeed_2600_sdmc_info
= {
495 .name
= TYPE_ASPEED_2600_SDMC
,
496 .parent
= TYPE_ASPEED_SDMC
,
497 .class_init
= aspeed_2600_sdmc_class_init
,
500 static void aspeed_sdmc_register_types(void)
502 type_register_static(&aspeed_sdmc_info
);
503 type_register_static(&aspeed_2400_sdmc_info
);
504 type_register_static(&aspeed_2500_sdmc_info
);
505 type_register_static(&aspeed_2600_sdmc_info
);
508 type_init(aspeed_sdmc_register_types
);