1c2da8a8bSCédric Le Goater /* 2c2da8a8bSCédric Le Goater * ASPEED SDRAM Memory Controller 3c2da8a8bSCédric Le Goater * 4c2da8a8bSCédric Le Goater * Copyright (C) 2016 IBM Corp. 5c2da8a8bSCédric Le Goater * 6c2da8a8bSCédric Le Goater * This code is licensed under the GPL version 2 or later. See 7c2da8a8bSCédric Le Goater * the COPYING file in the top-level directory. 8c2da8a8bSCédric Le Goater */ 9c2da8a8bSCédric Le Goater 10c2da8a8bSCédric Le Goater #include "qemu/osdep.h" 11c2da8a8bSCédric Le Goater #include "qemu/log.h" 12b2fd4545SCédric Le Goater #include "qemu/error-report.h" 13c2da8a8bSCédric Le Goater #include "hw/misc/aspeed_sdmc.h" 14c2da8a8bSCédric Le Goater #include "hw/misc/aspeed_scu.h" 15c2da8a8bSCédric Le Goater #include "hw/qdev-properties.h" 16c2da8a8bSCédric Le Goater #include "qapi/error.h" 17c2da8a8bSCédric Le Goater #include "trace.h" 18c2da8a8bSCédric Le Goater 19c2da8a8bSCédric Le Goater /* Protection Key Register */ 20c2da8a8bSCédric Le Goater #define R_PROT (0x00 / 4) 21c2da8a8bSCédric Le Goater #define PROT_KEY_UNLOCK 0xFC600309 22c2da8a8bSCédric Le Goater 23c2da8a8bSCédric Le Goater /* Configuration Register */ 24c2da8a8bSCédric Le Goater #define R_CONF (0x04 / 4) 25c2da8a8bSCédric Le Goater 26*33883ce8SJoel Stanley /* Control/Status Register #1 (ast2500) */ 27*33883ce8SJoel Stanley #define R_STATUS1 (0x60 / 4) 28*33883ce8SJoel Stanley #define PHY_BUSY_STATE BIT(0) 29*33883ce8SJoel Stanley 30c2da8a8bSCédric Le Goater /* 31c2da8a8bSCédric Le Goater * Configuration register Ox4 (for Aspeed AST2400 SOC) 32c2da8a8bSCédric Le Goater * 33c2da8a8bSCédric Le Goater * These are for the record and future use. ASPEED_SDMC_DRAM_SIZE is 34c2da8a8bSCédric Le Goater * what we care about right now as it is checked by U-Boot to 35c2da8a8bSCédric Le Goater * determine the RAM size. 36c2da8a8bSCédric Le Goater */ 37c2da8a8bSCédric Le Goater 38c2da8a8bSCédric Le Goater #define ASPEED_SDMC_RESERVED 0xFFFFF800 /* 31:11 reserved */ 39c2da8a8bSCédric Le Goater #define ASPEED_SDMC_AST2300_COMPAT (1 << 10) 40c2da8a8bSCédric Le Goater #define ASPEED_SDMC_SCRAMBLE_PATTERN (1 << 9) 41c2da8a8bSCédric Le Goater #define ASPEED_SDMC_DATA_SCRAMBLE (1 << 8) 42c2da8a8bSCédric Le Goater #define ASPEED_SDMC_ECC_ENABLE (1 << 7) 43c2da8a8bSCédric Le Goater #define ASPEED_SDMC_VGA_COMPAT (1 << 6) /* readonly */ 44c2da8a8bSCédric Le Goater #define ASPEED_SDMC_DRAM_BANK (1 << 5) 45c2da8a8bSCédric Le Goater #define ASPEED_SDMC_DRAM_BURST (1 << 4) 46c2da8a8bSCédric Le Goater #define ASPEED_SDMC_VGA_APERTURE(x) ((x & 0x3) << 2) /* readonly */ 47c2da8a8bSCédric Le Goater #define ASPEED_SDMC_VGA_8MB 0x0 48c2da8a8bSCédric Le Goater #define ASPEED_SDMC_VGA_16MB 0x1 49c2da8a8bSCédric Le Goater #define ASPEED_SDMC_VGA_32MB 0x2 50c2da8a8bSCédric Le Goater #define ASPEED_SDMC_VGA_64MB 0x3 51c2da8a8bSCédric Le Goater #define ASPEED_SDMC_DRAM_SIZE(x) (x & 0x3) 52c2da8a8bSCédric Le Goater #define ASPEED_SDMC_DRAM_64MB 0x0 53c2da8a8bSCédric Le Goater #define ASPEED_SDMC_DRAM_128MB 0x1 54c2da8a8bSCédric Le Goater #define ASPEED_SDMC_DRAM_256MB 0x2 55c2da8a8bSCédric Le Goater #define ASPEED_SDMC_DRAM_512MB 0x3 56c2da8a8bSCédric Le Goater 57c2da8a8bSCédric Le Goater #define ASPEED_SDMC_READONLY_MASK \ 58c2da8a8bSCédric Le Goater (ASPEED_SDMC_RESERVED | ASPEED_SDMC_VGA_COMPAT | \ 59c2da8a8bSCédric Le Goater ASPEED_SDMC_VGA_APERTURE(ASPEED_SDMC_VGA_64MB)) 60c2da8a8bSCédric Le Goater /* 61c2da8a8bSCédric Le Goater * Configuration register Ox4 (for Aspeed AST2500 SOC and higher) 62c2da8a8bSCédric Le Goater * 63c2da8a8bSCédric Le Goater * Incompatibilities are annotated in the list. ASPEED_SDMC_HW_VERSION 64c2da8a8bSCédric Le Goater * should be set to 1 for the AST2500 SOC. 65c2da8a8bSCédric Le Goater */ 66c2da8a8bSCédric Le Goater #define ASPEED_SDMC_HW_VERSION(x) ((x & 0xf) << 28) /* readonly */ 67c2da8a8bSCédric Le Goater #define ASPEED_SDMC_SW_VERSION ((x & 0xff) << 20) 68c2da8a8bSCédric Le Goater #define ASPEED_SDMC_CACHE_INITIAL_DONE (1 << 19) /* readonly */ 69c2da8a8bSCédric Le Goater #define ASPEED_SDMC_AST2500_RESERVED 0x7C000 /* 18:14 reserved */ 70c2da8a8bSCédric Le Goater #define ASPEED_SDMC_CACHE_DDR4_CONF (1 << 13) 71c2da8a8bSCédric Le Goater #define ASPEED_SDMC_CACHE_INITIAL (1 << 12) 72c2da8a8bSCédric Le Goater #define ASPEED_SDMC_CACHE_RANGE_CTRL (1 << 11) 73c2da8a8bSCédric Le Goater #define ASPEED_SDMC_CACHE_ENABLE (1 << 10) /* differs from AST2400 */ 74c2da8a8bSCédric Le Goater #define ASPEED_SDMC_DRAM_TYPE (1 << 4) /* differs from AST2400 */ 75c2da8a8bSCédric Le Goater 76c2da8a8bSCédric Le Goater /* DRAM size definitions differs */ 77c2da8a8bSCédric Le Goater #define ASPEED_SDMC_AST2500_128MB 0x0 78c2da8a8bSCédric Le Goater #define ASPEED_SDMC_AST2500_256MB 0x1 79c2da8a8bSCédric Le Goater #define ASPEED_SDMC_AST2500_512MB 0x2 80c2da8a8bSCédric Le Goater #define ASPEED_SDMC_AST2500_1024MB 0x3 81c2da8a8bSCédric Le Goater 82c2da8a8bSCédric Le Goater #define ASPEED_SDMC_AST2500_READONLY_MASK \ 83c2da8a8bSCédric Le Goater (ASPEED_SDMC_HW_VERSION(0xf) | ASPEED_SDMC_CACHE_INITIAL_DONE | \ 84c2da8a8bSCédric Le Goater ASPEED_SDMC_AST2500_RESERVED | ASPEED_SDMC_VGA_COMPAT | \ 85c2da8a8bSCédric Le Goater ASPEED_SDMC_VGA_APERTURE(ASPEED_SDMC_VGA_64MB)) 86c2da8a8bSCédric Le Goater 87c2da8a8bSCédric Le Goater static uint64_t aspeed_sdmc_read(void *opaque, hwaddr addr, unsigned size) 88c2da8a8bSCédric Le Goater { 89c2da8a8bSCédric Le Goater AspeedSDMCState *s = ASPEED_SDMC(opaque); 90c2da8a8bSCédric Le Goater 91c2da8a8bSCédric Le Goater addr >>= 2; 92c2da8a8bSCédric Le Goater 93c2da8a8bSCédric Le Goater if (addr >= ARRAY_SIZE(s->regs)) { 94c2da8a8bSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, 95c2da8a8bSCédric Le Goater "%s: Out-of-bounds read at offset 0x%" HWADDR_PRIx "\n", 96c2da8a8bSCédric Le Goater __func__, addr); 97c2da8a8bSCédric Le Goater return 0; 98c2da8a8bSCédric Le Goater } 99c2da8a8bSCédric Le Goater 100c2da8a8bSCédric Le Goater return s->regs[addr]; 101c2da8a8bSCédric Le Goater } 102c2da8a8bSCédric Le Goater 103c2da8a8bSCédric Le Goater static void aspeed_sdmc_write(void *opaque, hwaddr addr, uint64_t data, 104c2da8a8bSCédric Le Goater unsigned int size) 105c2da8a8bSCédric Le Goater { 106c2da8a8bSCédric Le Goater AspeedSDMCState *s = ASPEED_SDMC(opaque); 107c2da8a8bSCédric Le Goater 108c2da8a8bSCédric Le Goater addr >>= 2; 109c2da8a8bSCédric Le Goater 110c2da8a8bSCédric Le Goater if (addr >= ARRAY_SIZE(s->regs)) { 111c2da8a8bSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, 112c2da8a8bSCédric Le Goater "%s: Out-of-bounds write at offset 0x%" HWADDR_PRIx "\n", 113c2da8a8bSCédric Le Goater __func__, addr); 114c2da8a8bSCédric Le Goater return; 115c2da8a8bSCédric Le Goater } 116c2da8a8bSCédric Le Goater 1175c1d3a2bSHugo Landau if (addr == R_PROT) { 1185c1d3a2bSHugo Landau s->regs[addr] = (data == PROT_KEY_UNLOCK) ? 1 : 0; 1195c1d3a2bSHugo Landau return; 1205c1d3a2bSHugo Landau } 1215c1d3a2bSHugo Landau 1225c1d3a2bSHugo Landau if (!s->regs[R_PROT]) { 123c2da8a8bSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: SDMC is locked!\n", __func__); 124c2da8a8bSCédric Le Goater return; 125c2da8a8bSCédric Le Goater } 126c2da8a8bSCédric Le Goater 127c2da8a8bSCédric Le Goater if (addr == R_CONF) { 128c2da8a8bSCédric Le Goater /* Make sure readonly bits are kept */ 129c2da8a8bSCédric Le Goater switch (s->silicon_rev) { 130c2da8a8bSCédric Le Goater case AST2400_A0_SILICON_REV: 1316efbac90SCédric Le Goater case AST2400_A1_SILICON_REV: 132c2da8a8bSCédric Le Goater data &= ~ASPEED_SDMC_READONLY_MASK; 133d131bc28SJoel Stanley data |= s->fixed_conf; 134c2da8a8bSCédric Le Goater break; 135c2da8a8bSCédric Le Goater case AST2500_A0_SILICON_REV: 1365c1d3a2bSHugo Landau case AST2500_A1_SILICON_REV: 137c2da8a8bSCédric Le Goater data &= ~ASPEED_SDMC_AST2500_READONLY_MASK; 138d131bc28SJoel Stanley data |= s->fixed_conf; 139c2da8a8bSCédric Le Goater break; 140c2da8a8bSCédric Le Goater default: 141c2da8a8bSCédric Le Goater g_assert_not_reached(); 142c2da8a8bSCédric Le Goater } 143c2da8a8bSCédric Le Goater } 144*33883ce8SJoel Stanley if (s->silicon_rev == AST2500_A0_SILICON_REV || 145*33883ce8SJoel Stanley s->silicon_rev == AST2500_A1_SILICON_REV) { 146*33883ce8SJoel Stanley switch (addr) { 147*33883ce8SJoel Stanley case R_STATUS1: 148*33883ce8SJoel Stanley /* Will never return 'busy' */ 149*33883ce8SJoel Stanley data &= ~PHY_BUSY_STATE; 150*33883ce8SJoel Stanley break; 151*33883ce8SJoel Stanley default: 152*33883ce8SJoel Stanley break; 153*33883ce8SJoel Stanley } 154*33883ce8SJoel Stanley } 155c2da8a8bSCédric Le Goater 156c2da8a8bSCédric Le Goater s->regs[addr] = data; 157c2da8a8bSCédric Le Goater } 158c2da8a8bSCédric Le Goater 159c2da8a8bSCédric Le Goater static const MemoryRegionOps aspeed_sdmc_ops = { 160c2da8a8bSCédric Le Goater .read = aspeed_sdmc_read, 161c2da8a8bSCédric Le Goater .write = aspeed_sdmc_write, 162c2da8a8bSCédric Le Goater .endianness = DEVICE_LITTLE_ENDIAN, 163c2da8a8bSCédric Le Goater .valid.min_access_size = 4, 164c2da8a8bSCédric Le Goater .valid.max_access_size = 4, 165c2da8a8bSCédric Le Goater }; 166c2da8a8bSCédric Le Goater 167c6c7cfb0SCédric Le Goater static int ast2400_rambits(AspeedSDMCState *s) 168c2da8a8bSCédric Le Goater { 169c6c7cfb0SCédric Le Goater switch (s->ram_size >> 20) { 170c2da8a8bSCédric Le Goater case 64: 171c2da8a8bSCédric Le Goater return ASPEED_SDMC_DRAM_64MB; 172c2da8a8bSCédric Le Goater case 128: 173c2da8a8bSCédric Le Goater return ASPEED_SDMC_DRAM_128MB; 174c2da8a8bSCédric Le Goater case 256: 175c2da8a8bSCédric Le Goater return ASPEED_SDMC_DRAM_256MB; 176c2da8a8bSCédric Le Goater case 512: 177c2da8a8bSCédric Le Goater return ASPEED_SDMC_DRAM_512MB; 178c2da8a8bSCédric Le Goater default: 179c2da8a8bSCédric Le Goater break; 180c2da8a8bSCédric Le Goater } 181c2da8a8bSCédric Le Goater 182b2fd4545SCédric Le Goater /* use a common default */ 1833dc6f869SAlistair Francis warn_report("Invalid RAM size 0x%" PRIx64 ". Using default 256M", 1843dc6f869SAlistair Francis s->ram_size); 185c6c7cfb0SCédric Le Goater s->ram_size = 256 << 20; 186b2fd4545SCédric Le Goater return ASPEED_SDMC_DRAM_256MB; 187c2da8a8bSCédric Le Goater } 188c2da8a8bSCédric Le Goater 189c6c7cfb0SCédric Le Goater static int ast2500_rambits(AspeedSDMCState *s) 190c2da8a8bSCédric Le Goater { 191c6c7cfb0SCédric Le Goater switch (s->ram_size >> 20) { 192c2da8a8bSCédric Le Goater case 128: 193c2da8a8bSCédric Le Goater return ASPEED_SDMC_AST2500_128MB; 194c2da8a8bSCédric Le Goater case 256: 195c2da8a8bSCédric Le Goater return ASPEED_SDMC_AST2500_256MB; 196c2da8a8bSCédric Le Goater case 512: 197c2da8a8bSCédric Le Goater return ASPEED_SDMC_AST2500_512MB; 198c2da8a8bSCédric Le Goater case 1024: 199c2da8a8bSCédric Le Goater return ASPEED_SDMC_AST2500_1024MB; 200c2da8a8bSCédric Le Goater default: 201c2da8a8bSCédric Le Goater break; 202c2da8a8bSCédric Le Goater } 203c2da8a8bSCédric Le Goater 204b2fd4545SCédric Le Goater /* use a common default */ 2053dc6f869SAlistair Francis warn_report("Invalid RAM size 0x%" PRIx64 ". Using default 512M", 2063dc6f869SAlistair Francis s->ram_size); 207c6c7cfb0SCédric Le Goater s->ram_size = 512 << 20; 208b2fd4545SCédric Le Goater return ASPEED_SDMC_AST2500_512MB; 209c2da8a8bSCédric Le Goater } 210c2da8a8bSCédric Le Goater 211c2da8a8bSCédric Le Goater static void aspeed_sdmc_reset(DeviceState *dev) 212c2da8a8bSCédric Le Goater { 213c2da8a8bSCédric Le Goater AspeedSDMCState *s = ASPEED_SDMC(dev); 214c2da8a8bSCédric Le Goater 215c2da8a8bSCédric Le Goater memset(s->regs, 0, sizeof(s->regs)); 216c2da8a8bSCédric Le Goater 217c2da8a8bSCédric Le Goater /* Set ram size bit and defaults values */ 218d131bc28SJoel Stanley s->regs[R_CONF] = s->fixed_conf; 219c2da8a8bSCédric Le Goater } 220c2da8a8bSCédric Le Goater 221c2da8a8bSCédric Le Goater static void aspeed_sdmc_realize(DeviceState *dev, Error **errp) 222c2da8a8bSCédric Le Goater { 223c2da8a8bSCédric Le Goater SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 224c2da8a8bSCédric Le Goater AspeedSDMCState *s = ASPEED_SDMC(dev); 225c2da8a8bSCédric Le Goater 226c2da8a8bSCédric Le Goater if (!is_supported_silicon_rev(s->silicon_rev)) { 227c2da8a8bSCédric Le Goater error_setg(errp, "Unknown silicon revision: 0x%" PRIx32, 228c2da8a8bSCédric Le Goater s->silicon_rev); 229c2da8a8bSCédric Le Goater return; 230c2da8a8bSCédric Le Goater } 231c2da8a8bSCédric Le Goater 2323755f9e3SCédric Le Goater switch (s->silicon_rev) { 2333755f9e3SCédric Le Goater case AST2400_A0_SILICON_REV: 2346efbac90SCédric Le Goater case AST2400_A1_SILICON_REV: 235c6c7cfb0SCédric Le Goater s->ram_bits = ast2400_rambits(s); 236d131bc28SJoel Stanley s->fixed_conf = ASPEED_SDMC_VGA_COMPAT | 237d131bc28SJoel Stanley ASPEED_SDMC_DRAM_SIZE(s->ram_bits); 2383755f9e3SCédric Le Goater break; 2393755f9e3SCédric Le Goater case AST2500_A0_SILICON_REV: 2403755f9e3SCédric Le Goater case AST2500_A1_SILICON_REV: 241c6c7cfb0SCédric Le Goater s->ram_bits = ast2500_rambits(s); 242d131bc28SJoel Stanley s->fixed_conf = ASPEED_SDMC_HW_VERSION(1) | 243d131bc28SJoel Stanley ASPEED_SDMC_VGA_APERTURE(ASPEED_SDMC_VGA_64MB) | 244b33f1e0bSJoel Stanley ASPEED_SDMC_CACHE_INITIAL_DONE | 245d131bc28SJoel Stanley ASPEED_SDMC_DRAM_SIZE(s->ram_bits); 2463755f9e3SCédric Le Goater break; 2473755f9e3SCédric Le Goater default: 2483755f9e3SCédric Le Goater g_assert_not_reached(); 2493755f9e3SCédric Le Goater } 2503755f9e3SCédric Le Goater 251c2da8a8bSCédric Le Goater memory_region_init_io(&s->iomem, OBJECT(s), &aspeed_sdmc_ops, s, 252c2da8a8bSCédric Le Goater TYPE_ASPEED_SDMC, 0x1000); 253c2da8a8bSCédric Le Goater sysbus_init_mmio(sbd, &s->iomem); 254c2da8a8bSCédric Le Goater } 255c2da8a8bSCédric Le Goater 256c2da8a8bSCédric Le Goater static const VMStateDescription vmstate_aspeed_sdmc = { 257c2da8a8bSCédric Le Goater .name = "aspeed.sdmc", 258c2da8a8bSCédric Le Goater .version_id = 1, 259c2da8a8bSCédric Le Goater .minimum_version_id = 1, 260c2da8a8bSCédric Le Goater .fields = (VMStateField[]) { 261c2da8a8bSCédric Le Goater VMSTATE_UINT32_ARRAY(regs, AspeedSDMCState, ASPEED_SDMC_NR_REGS), 262c2da8a8bSCédric Le Goater VMSTATE_END_OF_LIST() 263c2da8a8bSCédric Le Goater } 264c2da8a8bSCédric Le Goater }; 265c2da8a8bSCédric Le Goater 266c2da8a8bSCédric Le Goater static Property aspeed_sdmc_properties[] = { 267c2da8a8bSCédric Le Goater DEFINE_PROP_UINT32("silicon-rev", AspeedSDMCState, silicon_rev, 0), 268c6c7cfb0SCédric Le Goater DEFINE_PROP_UINT64("ram-size", AspeedSDMCState, ram_size, 0), 269c2da8a8bSCédric Le Goater DEFINE_PROP_END_OF_LIST(), 270c2da8a8bSCédric Le Goater }; 271c2da8a8bSCédric Le Goater 272c2da8a8bSCédric Le Goater static void aspeed_sdmc_class_init(ObjectClass *klass, void *data) 273c2da8a8bSCédric Le Goater { 274c2da8a8bSCédric Le Goater DeviceClass *dc = DEVICE_CLASS(klass); 275c2da8a8bSCédric Le Goater dc->realize = aspeed_sdmc_realize; 276c2da8a8bSCédric Le Goater dc->reset = aspeed_sdmc_reset; 277c2da8a8bSCédric Le Goater dc->desc = "ASPEED SDRAM Memory Controller"; 278c2da8a8bSCédric Le Goater dc->vmsd = &vmstate_aspeed_sdmc; 279c2da8a8bSCédric Le Goater dc->props = aspeed_sdmc_properties; 280c2da8a8bSCédric Le Goater } 281c2da8a8bSCédric Le Goater 282c2da8a8bSCédric Le Goater static const TypeInfo aspeed_sdmc_info = { 283c2da8a8bSCédric Le Goater .name = TYPE_ASPEED_SDMC, 284c2da8a8bSCédric Le Goater .parent = TYPE_SYS_BUS_DEVICE, 285c2da8a8bSCédric Le Goater .instance_size = sizeof(AspeedSDMCState), 286c2da8a8bSCédric Le Goater .class_init = aspeed_sdmc_class_init, 287c2da8a8bSCédric Le Goater }; 288c2da8a8bSCédric Le Goater 289c2da8a8bSCédric Le Goater static void aspeed_sdmc_register_types(void) 290c2da8a8bSCédric Le Goater { 291c2da8a8bSCédric Le Goater type_register_static(&aspeed_sdmc_info); 292c2da8a8bSCédric Le Goater } 293c2da8a8bSCédric Le Goater 294c2da8a8bSCédric Le Goater type_init(aspeed_sdmc_register_types); 295