17c1c69bcSCédric Le Goater /* 27c1c69bcSCédric Le Goater * ASPEED AST2400 SMC Controller (SPI Flash Only) 37c1c69bcSCédric Le Goater * 47c1c69bcSCédric Le Goater * Copyright (C) 2016 IBM Corp. 57c1c69bcSCédric Le Goater * 67c1c69bcSCédric Le Goater * Permission is hereby granted, free of charge, to any person obtaining a copy 77c1c69bcSCédric Le Goater * of this software and associated documentation files (the "Software"), to deal 87c1c69bcSCédric Le Goater * in the Software without restriction, including without limitation the rights 97c1c69bcSCédric Le Goater * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 107c1c69bcSCédric Le Goater * copies of the Software, and to permit persons to whom the Software is 117c1c69bcSCédric Le Goater * furnished to do so, subject to the following conditions: 127c1c69bcSCédric Le Goater * 137c1c69bcSCédric Le Goater * The above copyright notice and this permission notice shall be included in 147c1c69bcSCédric Le Goater * all copies or substantial portions of the Software. 157c1c69bcSCédric Le Goater * 167c1c69bcSCédric Le Goater * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 177c1c69bcSCédric Le Goater * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 187c1c69bcSCédric Le Goater * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 197c1c69bcSCédric Le Goater * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 207c1c69bcSCédric Le Goater * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 217c1c69bcSCédric Le Goater * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 227c1c69bcSCédric Le Goater * THE SOFTWARE. 237c1c69bcSCédric Le Goater */ 247c1c69bcSCédric Le Goater 257c1c69bcSCédric Le Goater #include "qemu/osdep.h" 267c1c69bcSCédric Le Goater #include "hw/sysbus.h" 277c1c69bcSCédric Le Goater #include "sysemu/sysemu.h" 287c1c69bcSCédric Le Goater #include "qemu/log.h" 297c1c69bcSCédric Le Goater #include "include/qemu/error-report.h" 307c1c69bcSCédric Le Goater #include "exec/address-spaces.h" 317c1c69bcSCédric Le Goater 327c1c69bcSCédric Le Goater #include "hw/ssi/aspeed_smc.h" 337c1c69bcSCédric Le Goater 347c1c69bcSCédric Le Goater /* CE Type Setting Register */ 357c1c69bcSCédric Le Goater #define R_CONF (0x00 / 4) 367c1c69bcSCédric Le Goater #define CONF_LEGACY_DISABLE (1 << 31) 377c1c69bcSCédric Le Goater #define CONF_ENABLE_W4 20 387c1c69bcSCédric Le Goater #define CONF_ENABLE_W3 19 397c1c69bcSCédric Le Goater #define CONF_ENABLE_W2 18 407c1c69bcSCédric Le Goater #define CONF_ENABLE_W1 17 417c1c69bcSCédric Le Goater #define CONF_ENABLE_W0 16 420707b34dSCédric Le Goater #define CONF_FLASH_TYPE4 8 430707b34dSCédric Le Goater #define CONF_FLASH_TYPE3 6 440707b34dSCédric Le Goater #define CONF_FLASH_TYPE2 4 450707b34dSCédric Le Goater #define CONF_FLASH_TYPE1 2 460707b34dSCédric Le Goater #define CONF_FLASH_TYPE0 0 470707b34dSCédric Le Goater #define CONF_FLASH_TYPE_NOR 0x0 480707b34dSCédric Le Goater #define CONF_FLASH_TYPE_NAND 0x1 490707b34dSCédric Le Goater #define CONF_FLASH_TYPE_SPI 0x2 507c1c69bcSCédric Le Goater 517c1c69bcSCédric Le Goater /* CE Control Register */ 527c1c69bcSCédric Le Goater #define R_CE_CTRL (0x04 / 4) 537c1c69bcSCédric Le Goater #define CTRL_EXTENDED4 4 /* 32 bit addressing for SPI */ 547c1c69bcSCédric Le Goater #define CTRL_EXTENDED3 3 /* 32 bit addressing for SPI */ 557c1c69bcSCédric Le Goater #define CTRL_EXTENDED2 2 /* 32 bit addressing for SPI */ 567c1c69bcSCédric Le Goater #define CTRL_EXTENDED1 1 /* 32 bit addressing for SPI */ 577c1c69bcSCédric Le Goater #define CTRL_EXTENDED0 0 /* 32 bit addressing for SPI */ 587c1c69bcSCédric Le Goater 597c1c69bcSCédric Le Goater /* Interrupt Control and Status Register */ 607c1c69bcSCédric Le Goater #define R_INTR_CTRL (0x08 / 4) 617c1c69bcSCédric Le Goater #define INTR_CTRL_DMA_STATUS (1 << 11) 627c1c69bcSCédric Le Goater #define INTR_CTRL_CMD_ABORT_STATUS (1 << 10) 637c1c69bcSCédric Le Goater #define INTR_CTRL_WRITE_PROTECT_STATUS (1 << 9) 647c1c69bcSCédric Le Goater #define INTR_CTRL_DMA_EN (1 << 3) 657c1c69bcSCédric Le Goater #define INTR_CTRL_CMD_ABORT_EN (1 << 2) 667c1c69bcSCédric Le Goater #define INTR_CTRL_WRITE_PROTECT_EN (1 << 1) 677c1c69bcSCédric Le Goater 687c1c69bcSCédric Le Goater /* CEx Control Register */ 697c1c69bcSCédric Le Goater #define R_CTRL0 (0x10 / 4) 707c1c69bcSCédric Le Goater #define CTRL_CMD_SHIFT 16 717c1c69bcSCédric Le Goater #define CTRL_CMD_MASK 0xff 72*fcdf2c59SCédric Le Goater #define CTRL_AST2400_SPI_4BYTE (1 << 13) 737c1c69bcSCédric Le Goater #define CTRL_CE_STOP_ACTIVE (1 << 2) 747c1c69bcSCédric Le Goater #define CTRL_CMD_MODE_MASK 0x3 757c1c69bcSCédric Le Goater #define CTRL_READMODE 0x0 767c1c69bcSCédric Le Goater #define CTRL_FREADMODE 0x1 777c1c69bcSCédric Le Goater #define CTRL_WRITEMODE 0x2 787c1c69bcSCédric Le Goater #define CTRL_USERMODE 0x3 797c1c69bcSCédric Le Goater #define R_CTRL1 (0x14 / 4) 807c1c69bcSCédric Le Goater #define R_CTRL2 (0x18 / 4) 817c1c69bcSCédric Le Goater #define R_CTRL3 (0x1C / 4) 827c1c69bcSCédric Le Goater #define R_CTRL4 (0x20 / 4) 837c1c69bcSCédric Le Goater 847c1c69bcSCédric Le Goater /* CEx Segment Address Register */ 857c1c69bcSCédric Le Goater #define R_SEG_ADDR0 (0x30 / 4) 86a03cb1daSCédric Le Goater #define SEG_END_SHIFT 24 /* 8MB units */ 87a03cb1daSCédric Le Goater #define SEG_END_MASK 0xff 887c1c69bcSCédric Le Goater #define SEG_START_SHIFT 16 /* address bit [A29-A23] */ 89a03cb1daSCédric Le Goater #define SEG_START_MASK 0xff 907c1c69bcSCédric Le Goater #define R_SEG_ADDR1 (0x34 / 4) 917c1c69bcSCédric Le Goater #define R_SEG_ADDR2 (0x38 / 4) 927c1c69bcSCédric Le Goater #define R_SEG_ADDR3 (0x3C / 4) 937c1c69bcSCédric Le Goater #define R_SEG_ADDR4 (0x40 / 4) 947c1c69bcSCédric Le Goater 957c1c69bcSCédric Le Goater /* Misc Control Register #1 */ 967c1c69bcSCédric Le Goater #define R_MISC_CTRL1 (0x50 / 4) 977c1c69bcSCédric Le Goater 987c1c69bcSCédric Le Goater /* Misc Control Register #2 */ 997c1c69bcSCédric Le Goater #define R_MISC_CTRL2 (0x54 / 4) 1007c1c69bcSCédric Le Goater 1017c1c69bcSCédric Le Goater /* DMA Control/Status Register */ 1027c1c69bcSCédric Le Goater #define R_DMA_CTRL (0x80 / 4) 1037c1c69bcSCédric Le Goater #define DMA_CTRL_DELAY_MASK 0xf 1047c1c69bcSCédric Le Goater #define DMA_CTRL_DELAY_SHIFT 8 1057c1c69bcSCédric Le Goater #define DMA_CTRL_FREQ_MASK 0xf 1067c1c69bcSCédric Le Goater #define DMA_CTRL_FREQ_SHIFT 4 1077c1c69bcSCédric Le Goater #define DMA_CTRL_MODE (1 << 3) 1087c1c69bcSCédric Le Goater #define DMA_CTRL_CKSUM (1 << 2) 1097c1c69bcSCédric Le Goater #define DMA_CTRL_DIR (1 << 1) 1107c1c69bcSCédric Le Goater #define DMA_CTRL_EN (1 << 0) 1117c1c69bcSCédric Le Goater 1127c1c69bcSCédric Le Goater /* DMA Flash Side Address */ 1137c1c69bcSCédric Le Goater #define R_DMA_FLASH_ADDR (0x84 / 4) 1147c1c69bcSCédric Le Goater 1157c1c69bcSCédric Le Goater /* DMA DRAM Side Address */ 1167c1c69bcSCédric Le Goater #define R_DMA_DRAM_ADDR (0x88 / 4) 1177c1c69bcSCédric Le Goater 1187c1c69bcSCédric Le Goater /* DMA Length Register */ 1197c1c69bcSCédric Le Goater #define R_DMA_LEN (0x8C / 4) 1207c1c69bcSCédric Le Goater 1217c1c69bcSCédric Le Goater /* Checksum Calculation Result */ 1227c1c69bcSCédric Le Goater #define R_DMA_CHECKSUM (0x90 / 4) 1237c1c69bcSCédric Le Goater 1247c1c69bcSCédric Le Goater /* Misc Control Register #2 */ 1257c1c69bcSCédric Le Goater #define R_TIMINGS (0x94 / 4) 1267c1c69bcSCédric Le Goater 1277c1c69bcSCédric Le Goater /* SPI controller registers and bits */ 1287c1c69bcSCédric Le Goater #define R_SPI_CONF (0x00 / 4) 1297c1c69bcSCédric Le Goater #define SPI_CONF_ENABLE_W0 0 1307c1c69bcSCédric Le Goater #define R_SPI_CTRL0 (0x4 / 4) 1317c1c69bcSCédric Le Goater #define R_SPI_MISC_CTRL (0x10 / 4) 1327c1c69bcSCédric Le Goater #define R_SPI_TIMINGS (0x14 / 4) 1337c1c69bcSCédric Le Goater 134087b57c9SCédric Le Goater #define ASPEED_SMC_R_SPI_MAX (0x20 / 4) 135087b57c9SCédric Le Goater #define ASPEED_SMC_R_SMC_MAX (0x20 / 4) 136087b57c9SCédric Le Goater 137dcb83444SCédric Le Goater #define ASPEED_SOC_SMC_FLASH_BASE 0x10000000 138dcb83444SCédric Le Goater #define ASPEED_SOC_FMC_FLASH_BASE 0x20000000 139dcb83444SCédric Le Goater #define ASPEED_SOC_SPI_FLASH_BASE 0x30000000 1406dc52326SCédric Le Goater #define ASPEED_SOC_SPI2_FLASH_BASE 0x38000000 141dcb83444SCédric Le Goater 142*fcdf2c59SCédric Le Goater /* Flash opcodes. */ 143*fcdf2c59SCédric Le Goater #define SPI_OP_READ 0x03 /* Read data bytes (low frequency) */ 144*fcdf2c59SCédric Le Goater 145924ed163SCédric Le Goater /* 146924ed163SCédric Le Goater * Default segments mapping addresses and size for each slave per 147924ed163SCédric Le Goater * controller. These can be changed when board is initialized with the 148a03cb1daSCédric Le Goater * Segment Address Registers. 149924ed163SCédric Le Goater */ 150924ed163SCédric Le Goater static const AspeedSegments aspeed_segments_legacy[] = { 151924ed163SCédric Le Goater { 0x10000000, 32 * 1024 * 1024 }, 152924ed163SCédric Le Goater }; 153924ed163SCédric Le Goater 154924ed163SCédric Le Goater static const AspeedSegments aspeed_segments_fmc[] = { 1556dc52326SCédric Le Goater { 0x20000000, 64 * 1024 * 1024 }, /* start address is readonly */ 156924ed163SCédric Le Goater { 0x24000000, 32 * 1024 * 1024 }, 157924ed163SCédric Le Goater { 0x26000000, 32 * 1024 * 1024 }, 158924ed163SCédric Le Goater { 0x28000000, 32 * 1024 * 1024 }, 159924ed163SCédric Le Goater { 0x2A000000, 32 * 1024 * 1024 } 160924ed163SCédric Le Goater }; 161924ed163SCédric Le Goater 162924ed163SCédric Le Goater static const AspeedSegments aspeed_segments_spi[] = { 163924ed163SCédric Le Goater { 0x30000000, 64 * 1024 * 1024 }, 164924ed163SCédric Le Goater }; 165924ed163SCédric Le Goater 1666dc52326SCédric Le Goater static const AspeedSegments aspeed_segments_ast2500_fmc[] = { 1676dc52326SCédric Le Goater { 0x20000000, 128 * 1024 * 1024 }, /* start address is readonly */ 1686dc52326SCédric Le Goater { 0x28000000, 32 * 1024 * 1024 }, 1696dc52326SCédric Le Goater { 0x2A000000, 32 * 1024 * 1024 }, 1706dc52326SCédric Le Goater }; 1716dc52326SCédric Le Goater 1726dc52326SCédric Le Goater static const AspeedSegments aspeed_segments_ast2500_spi1[] = { 1736dc52326SCédric Le Goater { 0x30000000, 32 * 1024 * 1024 }, /* start address is readonly */ 1746dc52326SCédric Le Goater { 0x32000000, 96 * 1024 * 1024 }, /* end address is readonly */ 1756dc52326SCédric Le Goater }; 1766dc52326SCédric Le Goater 1776dc52326SCédric Le Goater static const AspeedSegments aspeed_segments_ast2500_spi2[] = { 1786dc52326SCédric Le Goater { 0x38000000, 32 * 1024 * 1024 }, /* start address is readonly */ 1796dc52326SCédric Le Goater { 0x3A000000, 96 * 1024 * 1024 }, /* end address is readonly */ 1806dc52326SCédric Le Goater }; 1816dc52326SCédric Le Goater 1827c1c69bcSCédric Le Goater static const AspeedSMCController controllers[] = { 183d09dc5b7SCédric Le Goater { 184d09dc5b7SCédric Le Goater .name = "aspeed.smc.smc", 185d09dc5b7SCédric Le Goater .r_conf = R_CONF, 186d09dc5b7SCédric Le Goater .r_ce_ctrl = R_CE_CTRL, 187d09dc5b7SCédric Le Goater .r_ctrl0 = R_CTRL0, 188d09dc5b7SCédric Le Goater .r_timings = R_TIMINGS, 189d09dc5b7SCédric Le Goater .conf_enable_w0 = CONF_ENABLE_W0, 190d09dc5b7SCédric Le Goater .max_slaves = 5, 191d09dc5b7SCédric Le Goater .segments = aspeed_segments_legacy, 192d09dc5b7SCédric Le Goater .flash_window_base = ASPEED_SOC_SMC_FLASH_BASE, 193d09dc5b7SCédric Le Goater .flash_window_size = 0x6000000, 194d09dc5b7SCédric Le Goater .has_dma = false, 195087b57c9SCédric Le Goater .nregs = ASPEED_SMC_R_SMC_MAX, 196d09dc5b7SCédric Le Goater }, { 197d09dc5b7SCédric Le Goater .name = "aspeed.smc.fmc", 198d09dc5b7SCédric Le Goater .r_conf = R_CONF, 199d09dc5b7SCédric Le Goater .r_ce_ctrl = R_CE_CTRL, 200d09dc5b7SCédric Le Goater .r_ctrl0 = R_CTRL0, 201d09dc5b7SCédric Le Goater .r_timings = R_TIMINGS, 202d09dc5b7SCédric Le Goater .conf_enable_w0 = CONF_ENABLE_W0, 203d09dc5b7SCédric Le Goater .max_slaves = 5, 204d09dc5b7SCédric Le Goater .segments = aspeed_segments_fmc, 205d09dc5b7SCédric Le Goater .flash_window_base = ASPEED_SOC_FMC_FLASH_BASE, 206d09dc5b7SCédric Le Goater .flash_window_size = 0x10000000, 207d09dc5b7SCédric Le Goater .has_dma = true, 208087b57c9SCédric Le Goater .nregs = ASPEED_SMC_R_MAX, 209d09dc5b7SCédric Le Goater }, { 210d09dc5b7SCédric Le Goater .name = "aspeed.smc.spi", 211d09dc5b7SCédric Le Goater .r_conf = R_SPI_CONF, 212d09dc5b7SCédric Le Goater .r_ce_ctrl = 0xff, 213d09dc5b7SCédric Le Goater .r_ctrl0 = R_SPI_CTRL0, 214d09dc5b7SCédric Le Goater .r_timings = R_SPI_TIMINGS, 215d09dc5b7SCédric Le Goater .conf_enable_w0 = SPI_CONF_ENABLE_W0, 216d09dc5b7SCédric Le Goater .max_slaves = 1, 217d09dc5b7SCédric Le Goater .segments = aspeed_segments_spi, 218d09dc5b7SCédric Le Goater .flash_window_base = ASPEED_SOC_SPI_FLASH_BASE, 219d09dc5b7SCédric Le Goater .flash_window_size = 0x10000000, 220d09dc5b7SCédric Le Goater .has_dma = false, 221087b57c9SCédric Le Goater .nregs = ASPEED_SMC_R_SPI_MAX, 222d09dc5b7SCédric Le Goater }, { 223d09dc5b7SCédric Le Goater .name = "aspeed.smc.ast2500-fmc", 224d09dc5b7SCédric Le Goater .r_conf = R_CONF, 225d09dc5b7SCédric Le Goater .r_ce_ctrl = R_CE_CTRL, 226d09dc5b7SCédric Le Goater .r_ctrl0 = R_CTRL0, 227d09dc5b7SCédric Le Goater .r_timings = R_TIMINGS, 228d09dc5b7SCédric Le Goater .conf_enable_w0 = CONF_ENABLE_W0, 229d09dc5b7SCédric Le Goater .max_slaves = 3, 230d09dc5b7SCédric Le Goater .segments = aspeed_segments_ast2500_fmc, 231d09dc5b7SCédric Le Goater .flash_window_base = ASPEED_SOC_FMC_FLASH_BASE, 232d09dc5b7SCédric Le Goater .flash_window_size = 0x10000000, 233d09dc5b7SCédric Le Goater .has_dma = true, 234087b57c9SCédric Le Goater .nregs = ASPEED_SMC_R_MAX, 235d09dc5b7SCédric Le Goater }, { 236d09dc5b7SCédric Le Goater .name = "aspeed.smc.ast2500-spi1", 237d09dc5b7SCédric Le Goater .r_conf = R_CONF, 238d09dc5b7SCédric Le Goater .r_ce_ctrl = R_CE_CTRL, 239d09dc5b7SCédric Le Goater .r_ctrl0 = R_CTRL0, 240d09dc5b7SCédric Le Goater .r_timings = R_TIMINGS, 241d09dc5b7SCédric Le Goater .conf_enable_w0 = CONF_ENABLE_W0, 242d09dc5b7SCédric Le Goater .max_slaves = 2, 243d09dc5b7SCédric Le Goater .segments = aspeed_segments_ast2500_spi1, 244d09dc5b7SCédric Le Goater .flash_window_base = ASPEED_SOC_SPI_FLASH_BASE, 245d09dc5b7SCédric Le Goater .flash_window_size = 0x8000000, 246d09dc5b7SCédric Le Goater .has_dma = false, 247087b57c9SCédric Le Goater .nregs = ASPEED_SMC_R_MAX, 248d09dc5b7SCédric Le Goater }, { 249d09dc5b7SCédric Le Goater .name = "aspeed.smc.ast2500-spi2", 250d09dc5b7SCédric Le Goater .r_conf = R_CONF, 251d09dc5b7SCédric Le Goater .r_ce_ctrl = R_CE_CTRL, 252d09dc5b7SCédric Le Goater .r_ctrl0 = R_CTRL0, 253d09dc5b7SCédric Le Goater .r_timings = R_TIMINGS, 254d09dc5b7SCédric Le Goater .conf_enable_w0 = CONF_ENABLE_W0, 255d09dc5b7SCédric Le Goater .max_slaves = 2, 256d09dc5b7SCédric Le Goater .segments = aspeed_segments_ast2500_spi2, 257d09dc5b7SCédric Le Goater .flash_window_base = ASPEED_SOC_SPI2_FLASH_BASE, 258d09dc5b7SCédric Le Goater .flash_window_size = 0x8000000, 259d09dc5b7SCédric Le Goater .has_dma = false, 260087b57c9SCédric Le Goater .nregs = ASPEED_SMC_R_MAX, 261d09dc5b7SCédric Le Goater }, 262924ed163SCédric Le Goater }; 263924ed163SCédric Le Goater 264a03cb1daSCédric Le Goater /* 265a03cb1daSCédric Le Goater * The Segment Register uses a 8MB unit to encode the start address 266a03cb1daSCédric Le Goater * and the end address of the mapping window of a flash SPI slave : 267a03cb1daSCédric Le Goater * 268a03cb1daSCédric Le Goater * | byte 1 | byte 2 | byte 3 | byte 4 | 269a03cb1daSCédric Le Goater * +--------+--------+--------+--------+ 270a03cb1daSCédric Le Goater * | end | start | 0 | 0 | 271a03cb1daSCédric Le Goater * 272a03cb1daSCédric Le Goater */ 273a03cb1daSCédric Le Goater static inline uint32_t aspeed_smc_segment_to_reg(const AspeedSegments *seg) 274a03cb1daSCédric Le Goater { 275a03cb1daSCédric Le Goater uint32_t reg = 0; 276a03cb1daSCédric Le Goater reg |= ((seg->addr >> 23) & SEG_START_MASK) << SEG_START_SHIFT; 277a03cb1daSCédric Le Goater reg |= (((seg->addr + seg->size) >> 23) & SEG_END_MASK) << SEG_END_SHIFT; 278a03cb1daSCédric Le Goater return reg; 279a03cb1daSCédric Le Goater } 280a03cb1daSCédric Le Goater 281a03cb1daSCédric Le Goater static inline void aspeed_smc_reg_to_segment(uint32_t reg, AspeedSegments *seg) 282a03cb1daSCédric Le Goater { 283a03cb1daSCédric Le Goater seg->addr = ((reg >> SEG_START_SHIFT) & SEG_START_MASK) << 23; 284a03cb1daSCédric Le Goater seg->size = (((reg >> SEG_END_SHIFT) & SEG_END_MASK) << 23) - seg->addr; 285a03cb1daSCédric Le Goater } 286a03cb1daSCédric Le Goater 287a03cb1daSCédric Le Goater static bool aspeed_smc_flash_overlap(const AspeedSMCState *s, 288a03cb1daSCédric Le Goater const AspeedSegments *new, 289a03cb1daSCédric Le Goater int cs) 290a03cb1daSCédric Le Goater { 291a03cb1daSCédric Le Goater AspeedSegments seg; 292a03cb1daSCédric Le Goater int i; 293a03cb1daSCédric Le Goater 294a03cb1daSCédric Le Goater for (i = 0; i < s->ctrl->max_slaves; i++) { 295a03cb1daSCédric Le Goater if (i == cs) { 296a03cb1daSCédric Le Goater continue; 297a03cb1daSCédric Le Goater } 298a03cb1daSCédric Le Goater 299a03cb1daSCédric Le Goater aspeed_smc_reg_to_segment(s->regs[R_SEG_ADDR0 + i], &seg); 300a03cb1daSCédric Le Goater 301a03cb1daSCédric Le Goater if (new->addr + new->size > seg.addr && 302a03cb1daSCédric Le Goater new->addr < seg.addr + seg.size) { 303a03cb1daSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: new segment CS%d [ 0x%" 304a03cb1daSCédric Le Goater HWADDR_PRIx" - 0x%"HWADDR_PRIx" ] overlaps with " 305a03cb1daSCédric Le Goater "CS%d [ 0x%"HWADDR_PRIx" - 0x%"HWADDR_PRIx" ]\n", 306a03cb1daSCédric Le Goater s->ctrl->name, cs, new->addr, new->addr + new->size, 307a03cb1daSCédric Le Goater i, seg.addr, seg.addr + seg.size); 308a03cb1daSCédric Le Goater return true; 309a03cb1daSCédric Le Goater } 310a03cb1daSCédric Le Goater } 311a03cb1daSCédric Le Goater return false; 312a03cb1daSCédric Le Goater } 313a03cb1daSCédric Le Goater 314a03cb1daSCédric Le Goater static void aspeed_smc_flash_set_segment(AspeedSMCState *s, int cs, 315a03cb1daSCédric Le Goater uint64_t new) 316a03cb1daSCédric Le Goater { 317a03cb1daSCédric Le Goater AspeedSMCFlash *fl = &s->flashes[cs]; 318a03cb1daSCédric Le Goater AspeedSegments seg; 319a03cb1daSCédric Le Goater 320a03cb1daSCédric Le Goater aspeed_smc_reg_to_segment(new, &seg); 321a03cb1daSCédric Le Goater 322a03cb1daSCédric Le Goater /* The start address of CS0 is read-only */ 323a03cb1daSCédric Le Goater if (cs == 0 && seg.addr != s->ctrl->flash_window_base) { 324a03cb1daSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, 325a03cb1daSCédric Le Goater "%s: Tried to change CS0 start address to 0x%" 326a03cb1daSCédric Le Goater HWADDR_PRIx "\n", s->ctrl->name, seg.addr); 3270584d3c3SCédric Le Goater seg.addr = s->ctrl->flash_window_base; 3280584d3c3SCédric Le Goater new = aspeed_smc_segment_to_reg(&seg); 329a03cb1daSCédric Le Goater } 330a03cb1daSCédric Le Goater 331a03cb1daSCédric Le Goater /* 332a03cb1daSCédric Le Goater * The end address of the AST2500 spi controllers is also 333a03cb1daSCédric Le Goater * read-only. 334a03cb1daSCédric Le Goater */ 335a03cb1daSCédric Le Goater if ((s->ctrl->segments == aspeed_segments_ast2500_spi1 || 336a03cb1daSCédric Le Goater s->ctrl->segments == aspeed_segments_ast2500_spi2) && 337a03cb1daSCédric Le Goater cs == s->ctrl->max_slaves && 338a03cb1daSCédric Le Goater seg.addr + seg.size != s->ctrl->segments[cs].addr + 339a03cb1daSCédric Le Goater s->ctrl->segments[cs].size) { 340a03cb1daSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, 341a03cb1daSCédric Le Goater "%s: Tried to change CS%d end address to 0x%" 3420584d3c3SCédric Le Goater HWADDR_PRIx "\n", s->ctrl->name, cs, seg.addr + seg.size); 3430584d3c3SCédric Le Goater seg.size = s->ctrl->segments[cs].addr + s->ctrl->segments[cs].size - 3440584d3c3SCédric Le Goater seg.addr; 3450584d3c3SCédric Le Goater new = aspeed_smc_segment_to_reg(&seg); 346a03cb1daSCédric Le Goater } 347a03cb1daSCédric Le Goater 348a03cb1daSCédric Le Goater /* Keep the segment in the overall flash window */ 349a03cb1daSCédric Le Goater if (seg.addr + seg.size <= s->ctrl->flash_window_base || 350a03cb1daSCédric Le Goater seg.addr > s->ctrl->flash_window_base + s->ctrl->flash_window_size) { 351a03cb1daSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: new segment for CS%d is invalid : " 352a03cb1daSCédric Le Goater "[ 0x%"HWADDR_PRIx" - 0x%"HWADDR_PRIx" ]\n", 353a03cb1daSCédric Le Goater s->ctrl->name, cs, seg.addr, seg.addr + seg.size); 354a03cb1daSCédric Le Goater return; 355a03cb1daSCédric Le Goater } 356a03cb1daSCédric Le Goater 357a03cb1daSCédric Le Goater /* Check start address vs. alignment */ 3580584d3c3SCédric Le Goater if (seg.size && !QEMU_IS_ALIGNED(seg.addr, seg.size)) { 359a03cb1daSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: new segment for CS%d is not " 360a03cb1daSCédric Le Goater "aligned : [ 0x%"HWADDR_PRIx" - 0x%"HWADDR_PRIx" ]\n", 361a03cb1daSCédric Le Goater s->ctrl->name, cs, seg.addr, seg.addr + seg.size); 362a03cb1daSCédric Le Goater } 363a03cb1daSCédric Le Goater 3640584d3c3SCédric Le Goater /* And segments should not overlap (in the specs) */ 3650584d3c3SCédric Le Goater aspeed_smc_flash_overlap(s, &seg, cs); 366a03cb1daSCédric Le Goater 367a03cb1daSCédric Le Goater /* All should be fine now to move the region */ 368a03cb1daSCédric Le Goater memory_region_transaction_begin(); 369a03cb1daSCédric Le Goater memory_region_set_size(&fl->mmio, seg.size); 370a03cb1daSCédric Le Goater memory_region_set_address(&fl->mmio, seg.addr - s->ctrl->flash_window_base); 371a03cb1daSCédric Le Goater memory_region_set_enabled(&fl->mmio, true); 372a03cb1daSCédric Le Goater memory_region_transaction_commit(); 373a03cb1daSCédric Le Goater 374a03cb1daSCédric Le Goater s->regs[R_SEG_ADDR0 + cs] = new; 375a03cb1daSCédric Le Goater } 376a03cb1daSCédric Le Goater 377924ed163SCédric Le Goater static uint64_t aspeed_smc_flash_default_read(void *opaque, hwaddr addr, 378924ed163SCédric Le Goater unsigned size) 379924ed163SCédric Le Goater { 380924ed163SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: To 0x%" HWADDR_PRIx " of size %u" 381924ed163SCédric Le Goater PRIx64 "\n", __func__, addr, size); 382924ed163SCédric Le Goater return 0; 383924ed163SCédric Le Goater } 384924ed163SCédric Le Goater 385924ed163SCédric Le Goater static void aspeed_smc_flash_default_write(void *opaque, hwaddr addr, 386924ed163SCédric Le Goater uint64_t data, unsigned size) 387924ed163SCédric Le Goater { 388924ed163SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: To 0x%" HWADDR_PRIx " of size %u: 0x%" 389924ed163SCédric Le Goater PRIx64 "\n", __func__, addr, size, data); 390924ed163SCédric Le Goater } 391924ed163SCédric Le Goater 392924ed163SCédric Le Goater static const MemoryRegionOps aspeed_smc_flash_default_ops = { 393924ed163SCédric Le Goater .read = aspeed_smc_flash_default_read, 394924ed163SCédric Le Goater .write = aspeed_smc_flash_default_write, 395924ed163SCédric Le Goater .endianness = DEVICE_LITTLE_ENDIAN, 396924ed163SCédric Le Goater .valid = { 397924ed163SCédric Le Goater .min_access_size = 1, 398924ed163SCédric Le Goater .max_access_size = 4, 399924ed163SCédric Le Goater }, 400924ed163SCédric Le Goater }; 401924ed163SCédric Le Goater 402f248a9dbSCédric Le Goater static inline int aspeed_smc_flash_mode(const AspeedSMCFlash *fl) 403924ed163SCédric Le Goater { 404f248a9dbSCédric Le Goater const AspeedSMCState *s = fl->controller; 405f248a9dbSCédric Le Goater 406f248a9dbSCédric Le Goater return s->regs[s->r_ctrl0 + fl->id] & CTRL_CMD_MODE_MASK; 407924ed163SCédric Le Goater } 408924ed163SCédric Le Goater 409f248a9dbSCédric Le Goater static inline bool aspeed_smc_is_writable(const AspeedSMCFlash *fl) 410924ed163SCédric Le Goater { 411f248a9dbSCédric Le Goater const AspeedSMCState *s = fl->controller; 412f248a9dbSCédric Le Goater 413f248a9dbSCédric Le Goater return s->regs[s->r_conf] & (1 << (s->conf_enable_w0 + fl->id)); 414924ed163SCédric Le Goater } 415924ed163SCédric Le Goater 416*fcdf2c59SCédric Le Goater static inline int aspeed_smc_flash_cmd(const AspeedSMCFlash *fl) 417*fcdf2c59SCédric Le Goater { 418*fcdf2c59SCédric Le Goater const AspeedSMCState *s = fl->controller; 419*fcdf2c59SCédric Le Goater int cmd = (s->regs[s->r_ctrl0 + fl->id] >> CTRL_CMD_SHIFT) & CTRL_CMD_MASK; 420*fcdf2c59SCédric Le Goater 421*fcdf2c59SCédric Le Goater /* In read mode, the default SPI command is READ (0x3). In other 422*fcdf2c59SCédric Le Goater * modes, the command should necessarily be defined */ 423*fcdf2c59SCédric Le Goater if (aspeed_smc_flash_mode(fl) == CTRL_READMODE) { 424*fcdf2c59SCédric Le Goater cmd = SPI_OP_READ; 425*fcdf2c59SCédric Le Goater } 426*fcdf2c59SCédric Le Goater 427*fcdf2c59SCédric Le Goater if (!cmd) { 428*fcdf2c59SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: no command defined for mode %d\n", 429*fcdf2c59SCédric Le Goater __func__, aspeed_smc_flash_mode(fl)); 430*fcdf2c59SCédric Le Goater } 431*fcdf2c59SCédric Le Goater 432*fcdf2c59SCédric Le Goater return cmd; 433*fcdf2c59SCédric Le Goater } 434*fcdf2c59SCédric Le Goater 435*fcdf2c59SCédric Le Goater static inline int aspeed_smc_flash_is_4byte(const AspeedSMCFlash *fl) 436*fcdf2c59SCédric Le Goater { 437*fcdf2c59SCédric Le Goater const AspeedSMCState *s = fl->controller; 438*fcdf2c59SCédric Le Goater 439*fcdf2c59SCédric Le Goater if (s->ctrl->segments == aspeed_segments_spi) { 440*fcdf2c59SCédric Le Goater return s->regs[s->r_ctrl0] & CTRL_AST2400_SPI_4BYTE; 441*fcdf2c59SCédric Le Goater } else { 442*fcdf2c59SCédric Le Goater return s->regs[s->r_ce_ctrl] & (1 << (CTRL_EXTENDED0 + fl->id)); 443*fcdf2c59SCédric Le Goater } 444*fcdf2c59SCédric Le Goater } 445*fcdf2c59SCédric Le Goater 446*fcdf2c59SCédric Le Goater static inline bool aspeed_smc_is_ce_stop_active(const AspeedSMCFlash *fl) 447*fcdf2c59SCédric Le Goater { 448*fcdf2c59SCédric Le Goater const AspeedSMCState *s = fl->controller; 449*fcdf2c59SCédric Le Goater 450*fcdf2c59SCédric Le Goater return s->regs[s->r_ctrl0 + fl->id] & CTRL_CE_STOP_ACTIVE; 451*fcdf2c59SCédric Le Goater } 452*fcdf2c59SCédric Le Goater 453*fcdf2c59SCédric Le Goater static void aspeed_smc_flash_select(AspeedSMCFlash *fl) 454*fcdf2c59SCédric Le Goater { 455*fcdf2c59SCédric Le Goater AspeedSMCState *s = fl->controller; 456*fcdf2c59SCédric Le Goater 457*fcdf2c59SCédric Le Goater s->regs[s->r_ctrl0 + fl->id] &= ~CTRL_CE_STOP_ACTIVE; 458*fcdf2c59SCédric Le Goater qemu_set_irq(s->cs_lines[fl->id], aspeed_smc_is_ce_stop_active(fl)); 459*fcdf2c59SCédric Le Goater } 460*fcdf2c59SCédric Le Goater 461*fcdf2c59SCédric Le Goater static void aspeed_smc_flash_unselect(AspeedSMCFlash *fl) 462*fcdf2c59SCédric Le Goater { 463*fcdf2c59SCédric Le Goater AspeedSMCState *s = fl->controller; 464*fcdf2c59SCédric Le Goater 465*fcdf2c59SCédric Le Goater s->regs[s->r_ctrl0 + fl->id] |= CTRL_CE_STOP_ACTIVE; 466*fcdf2c59SCédric Le Goater qemu_set_irq(s->cs_lines[fl->id], aspeed_smc_is_ce_stop_active(fl)); 467*fcdf2c59SCédric Le Goater } 468*fcdf2c59SCédric Le Goater 469*fcdf2c59SCédric Le Goater static uint32_t aspeed_smc_check_segment_addr(const AspeedSMCFlash *fl, 470*fcdf2c59SCédric Le Goater uint32_t addr) 471*fcdf2c59SCédric Le Goater { 472*fcdf2c59SCédric Le Goater const AspeedSMCState *s = fl->controller; 473*fcdf2c59SCédric Le Goater AspeedSegments seg; 474*fcdf2c59SCédric Le Goater 475*fcdf2c59SCédric Le Goater aspeed_smc_reg_to_segment(s->regs[R_SEG_ADDR0 + fl->id], &seg); 476*fcdf2c59SCédric Le Goater if ((addr & (seg.size - 1)) != addr) { 477*fcdf2c59SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, 478*fcdf2c59SCédric Le Goater "%s: invalid address 0x%08x for CS%d segment : " 479*fcdf2c59SCédric Le Goater "[ 0x%"HWADDR_PRIx" - 0x%"HWADDR_PRIx" ]\n", 480*fcdf2c59SCédric Le Goater s->ctrl->name, addr, fl->id, seg.addr, 481*fcdf2c59SCédric Le Goater seg.addr + seg.size); 482*fcdf2c59SCédric Le Goater } 483*fcdf2c59SCédric Le Goater 484*fcdf2c59SCédric Le Goater addr &= seg.size - 1; 485*fcdf2c59SCédric Le Goater return addr; 486*fcdf2c59SCédric Le Goater } 487*fcdf2c59SCédric Le Goater 488*fcdf2c59SCédric Le Goater static void aspeed_smc_flash_send_addr(AspeedSMCFlash *fl, uint32_t addr) 489*fcdf2c59SCédric Le Goater { 490*fcdf2c59SCédric Le Goater const AspeedSMCState *s = fl->controller; 491*fcdf2c59SCédric Le Goater uint8_t cmd = aspeed_smc_flash_cmd(fl); 492*fcdf2c59SCédric Le Goater 493*fcdf2c59SCédric Le Goater /* Flash access can not exceed CS segment */ 494*fcdf2c59SCédric Le Goater addr = aspeed_smc_check_segment_addr(fl, addr); 495*fcdf2c59SCédric Le Goater 496*fcdf2c59SCédric Le Goater ssi_transfer(s->spi, cmd); 497*fcdf2c59SCédric Le Goater 498*fcdf2c59SCédric Le Goater if (aspeed_smc_flash_is_4byte(fl)) { 499*fcdf2c59SCédric Le Goater ssi_transfer(s->spi, (addr >> 24) & 0xff); 500*fcdf2c59SCédric Le Goater } 501*fcdf2c59SCédric Le Goater ssi_transfer(s->spi, (addr >> 16) & 0xff); 502*fcdf2c59SCédric Le Goater ssi_transfer(s->spi, (addr >> 8) & 0xff); 503*fcdf2c59SCédric Le Goater ssi_transfer(s->spi, (addr & 0xff)); 504*fcdf2c59SCédric Le Goater } 505*fcdf2c59SCédric Le Goater 506924ed163SCédric Le Goater static uint64_t aspeed_smc_flash_read(void *opaque, hwaddr addr, unsigned size) 507924ed163SCédric Le Goater { 508924ed163SCédric Le Goater AspeedSMCFlash *fl = opaque; 509*fcdf2c59SCédric Le Goater AspeedSMCState *s = fl->controller; 510924ed163SCédric Le Goater uint64_t ret = 0; 511924ed163SCédric Le Goater int i; 512924ed163SCédric Le Goater 513*fcdf2c59SCédric Le Goater switch (aspeed_smc_flash_mode(fl)) { 514*fcdf2c59SCédric Le Goater case CTRL_USERMODE: 515924ed163SCédric Le Goater for (i = 0; i < size; i++) { 516924ed163SCédric Le Goater ret |= ssi_transfer(s->spi, 0x0) << (8 * i); 517924ed163SCédric Le Goater } 518*fcdf2c59SCédric Le Goater break; 519*fcdf2c59SCédric Le Goater case CTRL_READMODE: 520*fcdf2c59SCédric Le Goater case CTRL_FREADMODE: 521*fcdf2c59SCédric Le Goater aspeed_smc_flash_select(fl); 522*fcdf2c59SCédric Le Goater aspeed_smc_flash_send_addr(fl, addr); 523*fcdf2c59SCédric Le Goater 524*fcdf2c59SCédric Le Goater for (i = 0; i < size; i++) { 525*fcdf2c59SCédric Le Goater ret |= ssi_transfer(s->spi, 0x0) << (8 * i); 526*fcdf2c59SCédric Le Goater } 527*fcdf2c59SCédric Le Goater 528*fcdf2c59SCédric Le Goater aspeed_smc_flash_unselect(fl); 529*fcdf2c59SCédric Le Goater break; 530*fcdf2c59SCédric Le Goater default: 531*fcdf2c59SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid flash mode %d\n", 532*fcdf2c59SCédric Le Goater __func__, aspeed_smc_flash_mode(fl)); 533924ed163SCédric Le Goater } 534924ed163SCédric Le Goater 535924ed163SCédric Le Goater return ret; 536924ed163SCédric Le Goater } 537924ed163SCédric Le Goater 538924ed163SCédric Le Goater static void aspeed_smc_flash_write(void *opaque, hwaddr addr, uint64_t data, 539924ed163SCédric Le Goater unsigned size) 540924ed163SCédric Le Goater { 541924ed163SCédric Le Goater AspeedSMCFlash *fl = opaque; 542*fcdf2c59SCédric Le Goater AspeedSMCState *s = fl->controller; 543924ed163SCédric Le Goater int i; 544924ed163SCédric Le Goater 545f248a9dbSCédric Le Goater if (!aspeed_smc_is_writable(fl)) { 546924ed163SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: flash is not writable at 0x%" 547924ed163SCédric Le Goater HWADDR_PRIx "\n", __func__, addr); 548924ed163SCédric Le Goater return; 549924ed163SCédric Le Goater } 550924ed163SCédric Le Goater 551*fcdf2c59SCédric Le Goater switch (aspeed_smc_flash_mode(fl)) { 552*fcdf2c59SCédric Le Goater case CTRL_USERMODE: 553*fcdf2c59SCédric Le Goater for (i = 0; i < size; i++) { 554*fcdf2c59SCédric Le Goater ssi_transfer(s->spi, (data >> (8 * i)) & 0xff); 555924ed163SCédric Le Goater } 556*fcdf2c59SCédric Le Goater break; 557*fcdf2c59SCédric Le Goater case CTRL_WRITEMODE: 558*fcdf2c59SCédric Le Goater aspeed_smc_flash_select(fl); 559*fcdf2c59SCédric Le Goater aspeed_smc_flash_send_addr(fl, addr); 560924ed163SCédric Le Goater 561924ed163SCédric Le Goater for (i = 0; i < size; i++) { 562924ed163SCédric Le Goater ssi_transfer(s->spi, (data >> (8 * i)) & 0xff); 563924ed163SCédric Le Goater } 564*fcdf2c59SCédric Le Goater 565*fcdf2c59SCédric Le Goater aspeed_smc_flash_unselect(fl); 566*fcdf2c59SCédric Le Goater break; 567*fcdf2c59SCédric Le Goater default: 568*fcdf2c59SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid flash mode %d\n", 569*fcdf2c59SCédric Le Goater __func__, aspeed_smc_flash_mode(fl)); 570*fcdf2c59SCédric Le Goater } 571924ed163SCédric Le Goater } 572924ed163SCédric Le Goater 573924ed163SCédric Le Goater static const MemoryRegionOps aspeed_smc_flash_ops = { 574924ed163SCédric Le Goater .read = aspeed_smc_flash_read, 575924ed163SCédric Le Goater .write = aspeed_smc_flash_write, 576924ed163SCédric Le Goater .endianness = DEVICE_LITTLE_ENDIAN, 577924ed163SCédric Le Goater .valid = { 578924ed163SCédric Le Goater .min_access_size = 1, 579924ed163SCédric Le Goater .max_access_size = 4, 580924ed163SCédric Le Goater }, 5817c1c69bcSCédric Le Goater }; 5827c1c69bcSCédric Le Goater 583f248a9dbSCédric Le Goater static void aspeed_smc_flash_update_cs(AspeedSMCFlash *fl) 5847c1c69bcSCédric Le Goater { 585f248a9dbSCédric Le Goater const AspeedSMCState *s = fl->controller; 5867c1c69bcSCédric Le Goater 587f248a9dbSCédric Le Goater qemu_set_irq(s->cs_lines[fl->id], aspeed_smc_is_ce_stop_active(fl)); 5887c1c69bcSCédric Le Goater } 5897c1c69bcSCédric Le Goater 5907c1c69bcSCédric Le Goater static void aspeed_smc_reset(DeviceState *d) 5917c1c69bcSCédric Le Goater { 5927c1c69bcSCédric Le Goater AspeedSMCState *s = ASPEED_SMC(d); 5937c1c69bcSCédric Le Goater int i; 5947c1c69bcSCédric Le Goater 5957c1c69bcSCédric Le Goater memset(s->regs, 0, sizeof s->regs); 5967c1c69bcSCédric Le Goater 5972e1f0502SCédric Le Goater /* Pretend DMA is done (u-boot initialization) */ 5982e1f0502SCédric Le Goater s->regs[R_INTR_CTRL] = INTR_CTRL_DMA_STATUS; 5992e1f0502SCédric Le Goater 6007c1c69bcSCédric Le Goater /* Unselect all slaves */ 6017c1c69bcSCédric Le Goater for (i = 0; i < s->num_cs; ++i) { 6027c1c69bcSCédric Le Goater s->regs[s->r_ctrl0 + i] |= CTRL_CE_STOP_ACTIVE; 6031d247bd0SCédric Le Goater qemu_set_irq(s->cs_lines[i], true); 6047c1c69bcSCédric Le Goater } 6057c1c69bcSCédric Le Goater 606a03cb1daSCédric Le Goater /* setup default segment register values for all */ 607a03cb1daSCédric Le Goater for (i = 0; i < s->ctrl->max_slaves; ++i) { 608a03cb1daSCédric Le Goater s->regs[R_SEG_ADDR0 + i] = 609a03cb1daSCédric Le Goater aspeed_smc_segment_to_reg(&s->ctrl->segments[i]); 610a03cb1daSCédric Le Goater } 6110707b34dSCédric Le Goater 6120707b34dSCédric Le Goater /* HW strapping for AST2500 FMC controllers */ 6130707b34dSCédric Le Goater if (s->ctrl->segments == aspeed_segments_ast2500_fmc) { 6140707b34dSCédric Le Goater /* flash type is fixed to SPI for CE0 and CE1 */ 6150707b34dSCédric Le Goater s->regs[s->r_conf] |= (CONF_FLASH_TYPE_SPI << CONF_FLASH_TYPE0); 6160707b34dSCédric Le Goater s->regs[s->r_conf] |= (CONF_FLASH_TYPE_SPI << CONF_FLASH_TYPE1); 6170707b34dSCédric Le Goater 6180707b34dSCédric Le Goater /* 4BYTE mode is autodetected for CE0. Let's force it to 1 for 6190707b34dSCédric Le Goater * now */ 6200707b34dSCédric Le Goater s->regs[s->r_ce_ctrl] |= (1 << (CTRL_EXTENDED0)); 6210707b34dSCédric Le Goater } 6220707b34dSCédric Le Goater 6230707b34dSCédric Le Goater /* HW strapping for AST2400 FMC controllers (SCU70). Let's use the 6240707b34dSCédric Le Goater * configuration of the palmetto-bmc machine */ 6250707b34dSCédric Le Goater if (s->ctrl->segments == aspeed_segments_fmc) { 6260707b34dSCédric Le Goater s->regs[s->r_conf] |= (CONF_FLASH_TYPE_SPI << CONF_FLASH_TYPE0); 6270707b34dSCédric Le Goater 6280707b34dSCédric Le Goater s->regs[s->r_ce_ctrl] |= (1 << (CTRL_EXTENDED0)); 6290707b34dSCédric Le Goater } 6307c1c69bcSCédric Le Goater } 6317c1c69bcSCédric Le Goater 6327c1c69bcSCédric Le Goater static uint64_t aspeed_smc_read(void *opaque, hwaddr addr, unsigned int size) 6337c1c69bcSCédric Le Goater { 6347c1c69bcSCédric Le Goater AspeedSMCState *s = ASPEED_SMC(opaque); 6357c1c69bcSCédric Le Goater 6367c1c69bcSCédric Le Goater addr >>= 2; 6377c1c69bcSCédric Le Goater 63897c2ed5dSCédric Le Goater if (addr == s->r_conf || 63997c2ed5dSCédric Le Goater addr == s->r_timings || 64097c2ed5dSCédric Le Goater addr == s->r_ce_ctrl || 6412e1f0502SCédric Le Goater addr == R_INTR_CTRL || 642a03cb1daSCédric Le Goater (addr >= R_SEG_ADDR0 && addr < R_SEG_ADDR0 + s->ctrl->max_slaves) || 64397c2ed5dSCédric Le Goater (addr >= s->r_ctrl0 && addr < s->r_ctrl0 + s->num_cs)) { 64497c2ed5dSCédric Le Goater return s->regs[addr]; 64597c2ed5dSCédric Le Goater } else { 6467c1c69bcSCédric Le Goater qemu_log_mask(LOG_UNIMP, "%s: not implemented: 0x%" HWADDR_PRIx "\n", 6477c1c69bcSCédric Le Goater __func__, addr); 6487c1c69bcSCédric Le Goater return 0; 6497c1c69bcSCédric Le Goater } 6507c1c69bcSCédric Le Goater } 6517c1c69bcSCédric Le Goater 6527c1c69bcSCédric Le Goater static void aspeed_smc_write(void *opaque, hwaddr addr, uint64_t data, 6537c1c69bcSCédric Le Goater unsigned int size) 6547c1c69bcSCédric Le Goater { 6557c1c69bcSCédric Le Goater AspeedSMCState *s = ASPEED_SMC(opaque); 6567c1c69bcSCédric Le Goater uint32_t value = data; 6577c1c69bcSCédric Le Goater 6587c1c69bcSCédric Le Goater addr >>= 2; 6597c1c69bcSCédric Le Goater 66097c2ed5dSCédric Le Goater if (addr == s->r_conf || 66197c2ed5dSCédric Le Goater addr == s->r_timings || 66297c2ed5dSCédric Le Goater addr == s->r_ce_ctrl) { 66397c2ed5dSCédric Le Goater s->regs[addr] = value; 66497c2ed5dSCédric Le Goater } else if (addr >= s->r_ctrl0 && addr < s->r_ctrl0 + s->num_cs) { 665f248a9dbSCédric Le Goater int cs = addr - s->r_ctrl0; 66697c2ed5dSCédric Le Goater s->regs[addr] = value; 667f248a9dbSCédric Le Goater aspeed_smc_flash_update_cs(&s->flashes[cs]); 668a03cb1daSCédric Le Goater } else if (addr >= R_SEG_ADDR0 && 669a03cb1daSCédric Le Goater addr < R_SEG_ADDR0 + s->ctrl->max_slaves) { 670a03cb1daSCédric Le Goater int cs = addr - R_SEG_ADDR0; 671a03cb1daSCédric Le Goater 672a03cb1daSCédric Le Goater if (value != s->regs[R_SEG_ADDR0 + cs]) { 673a03cb1daSCédric Le Goater aspeed_smc_flash_set_segment(s, cs, value); 674a03cb1daSCédric Le Goater } 67597c2ed5dSCédric Le Goater } else { 6767c1c69bcSCédric Le Goater qemu_log_mask(LOG_UNIMP, "%s: not implemented: 0x%" HWADDR_PRIx "\n", 6777c1c69bcSCédric Le Goater __func__, addr); 6787c1c69bcSCédric Le Goater return; 6797c1c69bcSCédric Le Goater } 6807c1c69bcSCédric Le Goater } 6817c1c69bcSCédric Le Goater 6827c1c69bcSCédric Le Goater static const MemoryRegionOps aspeed_smc_ops = { 6837c1c69bcSCédric Le Goater .read = aspeed_smc_read, 6847c1c69bcSCédric Le Goater .write = aspeed_smc_write, 6857c1c69bcSCédric Le Goater .endianness = DEVICE_LITTLE_ENDIAN, 6867c1c69bcSCédric Le Goater .valid.unaligned = true, 6877c1c69bcSCédric Le Goater }; 6887c1c69bcSCédric Le Goater 6897c1c69bcSCédric Le Goater static void aspeed_smc_realize(DeviceState *dev, Error **errp) 6907c1c69bcSCédric Le Goater { 6917c1c69bcSCédric Le Goater SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 6927c1c69bcSCédric Le Goater AspeedSMCState *s = ASPEED_SMC(dev); 6937c1c69bcSCédric Le Goater AspeedSMCClass *mc = ASPEED_SMC_GET_CLASS(s); 6947c1c69bcSCédric Le Goater int i; 695924ed163SCédric Le Goater char name[32]; 696924ed163SCédric Le Goater hwaddr offset = 0; 6977c1c69bcSCédric Le Goater 6987c1c69bcSCédric Le Goater s->ctrl = mc->ctrl; 6997c1c69bcSCédric Le Goater 7007c1c69bcSCédric Le Goater /* keep a copy under AspeedSMCState to speed up accesses */ 7017c1c69bcSCédric Le Goater s->r_conf = s->ctrl->r_conf; 7027c1c69bcSCédric Le Goater s->r_ce_ctrl = s->ctrl->r_ce_ctrl; 7037c1c69bcSCédric Le Goater s->r_ctrl0 = s->ctrl->r_ctrl0; 7047c1c69bcSCédric Le Goater s->r_timings = s->ctrl->r_timings; 7057c1c69bcSCédric Le Goater s->conf_enable_w0 = s->ctrl->conf_enable_w0; 7067c1c69bcSCédric Le Goater 7077c1c69bcSCédric Le Goater /* Enforce some real HW limits */ 7087c1c69bcSCédric Le Goater if (s->num_cs > s->ctrl->max_slaves) { 7097c1c69bcSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: num_cs cannot exceed: %d\n", 7107c1c69bcSCédric Le Goater __func__, s->ctrl->max_slaves); 7117c1c69bcSCédric Le Goater s->num_cs = s->ctrl->max_slaves; 7127c1c69bcSCédric Le Goater } 7137c1c69bcSCédric Le Goater 7147c1c69bcSCédric Le Goater s->spi = ssi_create_bus(dev, "spi"); 7157c1c69bcSCédric Le Goater 7167c1c69bcSCédric Le Goater /* Setup cs_lines for slaves */ 7177c1c69bcSCédric Le Goater sysbus_init_irq(sbd, &s->irq); 7187c1c69bcSCédric Le Goater s->cs_lines = g_new0(qemu_irq, s->num_cs); 7197c1c69bcSCédric Le Goater ssi_auto_connect_slaves(dev, s->cs_lines, s->spi); 7207c1c69bcSCédric Le Goater 7217c1c69bcSCédric Le Goater for (i = 0; i < s->num_cs; ++i) { 7227c1c69bcSCédric Le Goater sysbus_init_irq(sbd, &s->cs_lines[i]); 7237c1c69bcSCédric Le Goater } 7247c1c69bcSCédric Le Goater 7252da95fd8SCédric Le Goater /* The memory region for the controller registers */ 7267c1c69bcSCédric Le Goater memory_region_init_io(&s->mmio, OBJECT(s), &aspeed_smc_ops, s, 727087b57c9SCédric Le Goater s->ctrl->name, s->ctrl->nregs * 4); 7287c1c69bcSCédric Le Goater sysbus_init_mmio(sbd, &s->mmio); 729924ed163SCédric Le Goater 730924ed163SCédric Le Goater /* 7312da95fd8SCédric Le Goater * The container memory region representing the address space 7322da95fd8SCédric Le Goater * window in which the flash modules are mapped. The size and 7332da95fd8SCédric Le Goater * address depends on the SoC model and controller type. 734924ed163SCédric Le Goater */ 735924ed163SCédric Le Goater snprintf(name, sizeof(name), "%s.flash", s->ctrl->name); 736924ed163SCédric Le Goater 737924ed163SCédric Le Goater memory_region_init_io(&s->mmio_flash, OBJECT(s), 738924ed163SCédric Le Goater &aspeed_smc_flash_default_ops, s, name, 739dcb83444SCédric Le Goater s->ctrl->flash_window_size); 740924ed163SCédric Le Goater sysbus_init_mmio(sbd, &s->mmio_flash); 741924ed163SCédric Le Goater 7422da95fd8SCédric Le Goater s->flashes = g_new0(AspeedSMCFlash, s->ctrl->max_slaves); 743924ed163SCédric Le Goater 7442da95fd8SCédric Le Goater /* 7452da95fd8SCédric Le Goater * Let's create a sub memory region for each possible slave. All 7462da95fd8SCédric Le Goater * have a configurable memory segment in the overall flash mapping 7472da95fd8SCédric Le Goater * window of the controller but, there is not necessarily a flash 7482da95fd8SCédric Le Goater * module behind to handle the memory accesses. This depends on 7492da95fd8SCédric Le Goater * the board configuration. 7502da95fd8SCédric Le Goater */ 7512da95fd8SCédric Le Goater for (i = 0; i < s->ctrl->max_slaves; ++i) { 752924ed163SCédric Le Goater AspeedSMCFlash *fl = &s->flashes[i]; 753924ed163SCédric Le Goater 754924ed163SCédric Le Goater snprintf(name, sizeof(name), "%s.%d", s->ctrl->name, i); 755924ed163SCédric Le Goater 756924ed163SCédric Le Goater fl->id = i; 757924ed163SCédric Le Goater fl->controller = s; 758924ed163SCédric Le Goater fl->size = s->ctrl->segments[i].size; 759924ed163SCédric Le Goater memory_region_init_io(&fl->mmio, OBJECT(s), &aspeed_smc_flash_ops, 760924ed163SCédric Le Goater fl, name, fl->size); 761924ed163SCédric Le Goater memory_region_add_subregion(&s->mmio_flash, offset, &fl->mmio); 762924ed163SCédric Le Goater offset += fl->size; 763924ed163SCédric Le Goater } 7647c1c69bcSCédric Le Goater } 7657c1c69bcSCédric Le Goater 7667c1c69bcSCédric Le Goater static const VMStateDescription vmstate_aspeed_smc = { 7677c1c69bcSCédric Le Goater .name = "aspeed.smc", 7687c1c69bcSCédric Le Goater .version_id = 1, 7697c1c69bcSCédric Le Goater .minimum_version_id = 1, 7707c1c69bcSCédric Le Goater .fields = (VMStateField[]) { 7717c1c69bcSCédric Le Goater VMSTATE_UINT32_ARRAY(regs, AspeedSMCState, ASPEED_SMC_R_MAX), 7727c1c69bcSCédric Le Goater VMSTATE_END_OF_LIST() 7737c1c69bcSCédric Le Goater } 7747c1c69bcSCédric Le Goater }; 7757c1c69bcSCédric Le Goater 7767c1c69bcSCédric Le Goater static Property aspeed_smc_properties[] = { 7777c1c69bcSCédric Le Goater DEFINE_PROP_UINT32("num-cs", AspeedSMCState, num_cs, 1), 7787c1c69bcSCédric Le Goater DEFINE_PROP_END_OF_LIST(), 7797c1c69bcSCédric Le Goater }; 7807c1c69bcSCédric Le Goater 7817c1c69bcSCédric Le Goater static void aspeed_smc_class_init(ObjectClass *klass, void *data) 7827c1c69bcSCédric Le Goater { 7837c1c69bcSCédric Le Goater DeviceClass *dc = DEVICE_CLASS(klass); 7847c1c69bcSCédric Le Goater AspeedSMCClass *mc = ASPEED_SMC_CLASS(klass); 7857c1c69bcSCédric Le Goater 7867c1c69bcSCédric Le Goater dc->realize = aspeed_smc_realize; 7877c1c69bcSCédric Le Goater dc->reset = aspeed_smc_reset; 7887c1c69bcSCédric Le Goater dc->props = aspeed_smc_properties; 7897c1c69bcSCédric Le Goater dc->vmsd = &vmstate_aspeed_smc; 7907c1c69bcSCédric Le Goater mc->ctrl = data; 7917c1c69bcSCédric Le Goater } 7927c1c69bcSCédric Le Goater 7937c1c69bcSCédric Le Goater static const TypeInfo aspeed_smc_info = { 7947c1c69bcSCédric Le Goater .name = TYPE_ASPEED_SMC, 7957c1c69bcSCédric Le Goater .parent = TYPE_SYS_BUS_DEVICE, 7967c1c69bcSCédric Le Goater .instance_size = sizeof(AspeedSMCState), 7977c1c69bcSCédric Le Goater .class_size = sizeof(AspeedSMCClass), 7987c1c69bcSCédric Le Goater .abstract = true, 7997c1c69bcSCédric Le Goater }; 8007c1c69bcSCédric Le Goater 8017c1c69bcSCédric Le Goater static void aspeed_smc_register_types(void) 8027c1c69bcSCédric Le Goater { 8037c1c69bcSCédric Le Goater int i; 8047c1c69bcSCédric Le Goater 8057c1c69bcSCédric Le Goater type_register_static(&aspeed_smc_info); 8067c1c69bcSCédric Le Goater for (i = 0; i < ARRAY_SIZE(controllers); ++i) { 8077c1c69bcSCédric Le Goater TypeInfo ti = { 8087c1c69bcSCédric Le Goater .name = controllers[i].name, 8097c1c69bcSCédric Le Goater .parent = TYPE_ASPEED_SMC, 8107c1c69bcSCédric Le Goater .class_init = aspeed_smc_class_init, 8117c1c69bcSCédric Le Goater .class_data = (void *)&controllers[i], 8127c1c69bcSCédric Le Goater }; 8137c1c69bcSCédric Le Goater type_register(&ti); 8147c1c69bcSCédric Le Goater } 8157c1c69bcSCédric Le Goater } 8167c1c69bcSCédric Le Goater 8177c1c69bcSCédric Le Goater type_init(aspeed_smc_register_types) 818