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" 29*d6e3f50aSPhilippe Mathieu-Daudé #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 72ac2810deSCédric Le Goater #define CTRL_DUMMY_HIGH_SHIFT 14 73fcdf2c59SCédric Le Goater #define CTRL_AST2400_SPI_4BYTE (1 << 13) 74ac2810deSCédric Le Goater #define CTRL_DUMMY_LOW_SHIFT 6 /* 2 bits [7:6] */ 757c1c69bcSCédric Le Goater #define CTRL_CE_STOP_ACTIVE (1 << 2) 767c1c69bcSCédric Le Goater #define CTRL_CMD_MODE_MASK 0x3 777c1c69bcSCédric Le Goater #define CTRL_READMODE 0x0 787c1c69bcSCédric Le Goater #define CTRL_FREADMODE 0x1 797c1c69bcSCédric Le Goater #define CTRL_WRITEMODE 0x2 807c1c69bcSCédric Le Goater #define CTRL_USERMODE 0x3 817c1c69bcSCédric Le Goater #define R_CTRL1 (0x14 / 4) 827c1c69bcSCédric Le Goater #define R_CTRL2 (0x18 / 4) 837c1c69bcSCédric Le Goater #define R_CTRL3 (0x1C / 4) 847c1c69bcSCédric Le Goater #define R_CTRL4 (0x20 / 4) 857c1c69bcSCédric Le Goater 867c1c69bcSCédric Le Goater /* CEx Segment Address Register */ 877c1c69bcSCédric Le Goater #define R_SEG_ADDR0 (0x30 / 4) 88a03cb1daSCédric Le Goater #define SEG_END_SHIFT 24 /* 8MB units */ 89a03cb1daSCédric Le Goater #define SEG_END_MASK 0xff 907c1c69bcSCédric Le Goater #define SEG_START_SHIFT 16 /* address bit [A29-A23] */ 91a03cb1daSCédric Le Goater #define SEG_START_MASK 0xff 927c1c69bcSCédric Le Goater #define R_SEG_ADDR1 (0x34 / 4) 937c1c69bcSCédric Le Goater #define R_SEG_ADDR2 (0x38 / 4) 947c1c69bcSCédric Le Goater #define R_SEG_ADDR3 (0x3C / 4) 957c1c69bcSCédric Le Goater #define R_SEG_ADDR4 (0x40 / 4) 967c1c69bcSCédric Le Goater 977c1c69bcSCédric Le Goater /* Misc Control Register #1 */ 987c1c69bcSCédric Le Goater #define R_MISC_CTRL1 (0x50 / 4) 997c1c69bcSCédric Le Goater 1007c1c69bcSCédric Le Goater /* Misc Control Register #2 */ 1017c1c69bcSCédric Le Goater #define R_MISC_CTRL2 (0x54 / 4) 1027c1c69bcSCédric Le Goater 1037c1c69bcSCédric Le Goater /* DMA Control/Status Register */ 1047c1c69bcSCédric Le Goater #define R_DMA_CTRL (0x80 / 4) 1057c1c69bcSCédric Le Goater #define DMA_CTRL_DELAY_MASK 0xf 1067c1c69bcSCédric Le Goater #define DMA_CTRL_DELAY_SHIFT 8 1077c1c69bcSCédric Le Goater #define DMA_CTRL_FREQ_MASK 0xf 1087c1c69bcSCédric Le Goater #define DMA_CTRL_FREQ_SHIFT 4 1097c1c69bcSCédric Le Goater #define DMA_CTRL_MODE (1 << 3) 1107c1c69bcSCédric Le Goater #define DMA_CTRL_CKSUM (1 << 2) 1117c1c69bcSCédric Le Goater #define DMA_CTRL_DIR (1 << 1) 1127c1c69bcSCédric Le Goater #define DMA_CTRL_EN (1 << 0) 1137c1c69bcSCédric Le Goater 1147c1c69bcSCédric Le Goater /* DMA Flash Side Address */ 1157c1c69bcSCédric Le Goater #define R_DMA_FLASH_ADDR (0x84 / 4) 1167c1c69bcSCédric Le Goater 1177c1c69bcSCédric Le Goater /* DMA DRAM Side Address */ 1187c1c69bcSCédric Le Goater #define R_DMA_DRAM_ADDR (0x88 / 4) 1197c1c69bcSCédric Le Goater 1207c1c69bcSCédric Le Goater /* DMA Length Register */ 1217c1c69bcSCédric Le Goater #define R_DMA_LEN (0x8C / 4) 1227c1c69bcSCédric Le Goater 1237c1c69bcSCédric Le Goater /* Checksum Calculation Result */ 1247c1c69bcSCédric Le Goater #define R_DMA_CHECKSUM (0x90 / 4) 1257c1c69bcSCédric Le Goater 1267c1c69bcSCédric Le Goater /* Misc Control Register #2 */ 1277c1c69bcSCédric Le Goater #define R_TIMINGS (0x94 / 4) 1287c1c69bcSCédric Le Goater 1297c1c69bcSCédric Le Goater /* SPI controller registers and bits */ 1307c1c69bcSCédric Le Goater #define R_SPI_CONF (0x00 / 4) 1317c1c69bcSCédric Le Goater #define SPI_CONF_ENABLE_W0 0 1327c1c69bcSCédric Le Goater #define R_SPI_CTRL0 (0x4 / 4) 1337c1c69bcSCédric Le Goater #define R_SPI_MISC_CTRL (0x10 / 4) 1347c1c69bcSCédric Le Goater #define R_SPI_TIMINGS (0x14 / 4) 1357c1c69bcSCédric Le Goater 136087b57c9SCédric Le Goater #define ASPEED_SMC_R_SPI_MAX (0x20 / 4) 137087b57c9SCédric Le Goater #define ASPEED_SMC_R_SMC_MAX (0x20 / 4) 138087b57c9SCédric Le Goater 139dcb83444SCédric Le Goater #define ASPEED_SOC_SMC_FLASH_BASE 0x10000000 140dcb83444SCédric Le Goater #define ASPEED_SOC_FMC_FLASH_BASE 0x20000000 141dcb83444SCédric Le Goater #define ASPEED_SOC_SPI_FLASH_BASE 0x30000000 1426dc52326SCédric Le Goater #define ASPEED_SOC_SPI2_FLASH_BASE 0x38000000 143dcb83444SCédric Le Goater 144fcdf2c59SCédric Le Goater /* Flash opcodes. */ 145fcdf2c59SCédric Le Goater #define SPI_OP_READ 0x03 /* Read data bytes (low frequency) */ 146fcdf2c59SCédric Le Goater 147924ed163SCédric Le Goater /* 148924ed163SCédric Le Goater * Default segments mapping addresses and size for each slave per 149924ed163SCédric Le Goater * controller. These can be changed when board is initialized with the 150a03cb1daSCédric Le Goater * Segment Address Registers. 151924ed163SCédric Le Goater */ 152924ed163SCédric Le Goater static const AspeedSegments aspeed_segments_legacy[] = { 153924ed163SCédric Le Goater { 0x10000000, 32 * 1024 * 1024 }, 154924ed163SCédric Le Goater }; 155924ed163SCédric Le Goater 156924ed163SCédric Le Goater static const AspeedSegments aspeed_segments_fmc[] = { 1576dc52326SCédric Le Goater { 0x20000000, 64 * 1024 * 1024 }, /* start address is readonly */ 158924ed163SCédric Le Goater { 0x24000000, 32 * 1024 * 1024 }, 159924ed163SCédric Le Goater { 0x26000000, 32 * 1024 * 1024 }, 160924ed163SCédric Le Goater { 0x28000000, 32 * 1024 * 1024 }, 161924ed163SCédric Le Goater { 0x2A000000, 32 * 1024 * 1024 } 162924ed163SCédric Le Goater }; 163924ed163SCédric Le Goater 164924ed163SCédric Le Goater static const AspeedSegments aspeed_segments_spi[] = { 165924ed163SCédric Le Goater { 0x30000000, 64 * 1024 * 1024 }, 166924ed163SCédric Le Goater }; 167924ed163SCédric Le Goater 1686dc52326SCédric Le Goater static const AspeedSegments aspeed_segments_ast2500_fmc[] = { 1696dc52326SCédric Le Goater { 0x20000000, 128 * 1024 * 1024 }, /* start address is readonly */ 1706dc52326SCédric Le Goater { 0x28000000, 32 * 1024 * 1024 }, 1716dc52326SCédric Le Goater { 0x2A000000, 32 * 1024 * 1024 }, 1726dc52326SCédric Le Goater }; 1736dc52326SCédric Le Goater 1746dc52326SCédric Le Goater static const AspeedSegments aspeed_segments_ast2500_spi1[] = { 1756dc52326SCédric Le Goater { 0x30000000, 32 * 1024 * 1024 }, /* start address is readonly */ 1766dc52326SCédric Le Goater { 0x32000000, 96 * 1024 * 1024 }, /* end address is readonly */ 1776dc52326SCédric Le Goater }; 1786dc52326SCédric Le Goater 1796dc52326SCédric Le Goater static const AspeedSegments aspeed_segments_ast2500_spi2[] = { 1806dc52326SCédric Le Goater { 0x38000000, 32 * 1024 * 1024 }, /* start address is readonly */ 1816dc52326SCédric Le Goater { 0x3A000000, 96 * 1024 * 1024 }, /* end address is readonly */ 1826dc52326SCédric Le Goater }; 1836dc52326SCédric Le Goater 1847c1c69bcSCédric Le Goater static const AspeedSMCController controllers[] = { 185d09dc5b7SCédric Le Goater { 186d09dc5b7SCédric Le Goater .name = "aspeed.smc.smc", 187d09dc5b7SCédric Le Goater .r_conf = R_CONF, 188d09dc5b7SCédric Le Goater .r_ce_ctrl = R_CE_CTRL, 189d09dc5b7SCédric Le Goater .r_ctrl0 = R_CTRL0, 190d09dc5b7SCédric Le Goater .r_timings = R_TIMINGS, 191d09dc5b7SCédric Le Goater .conf_enable_w0 = CONF_ENABLE_W0, 192d09dc5b7SCédric Le Goater .max_slaves = 5, 193d09dc5b7SCédric Le Goater .segments = aspeed_segments_legacy, 194d09dc5b7SCédric Le Goater .flash_window_base = ASPEED_SOC_SMC_FLASH_BASE, 195d09dc5b7SCédric Le Goater .flash_window_size = 0x6000000, 196d09dc5b7SCédric Le Goater .has_dma = false, 197087b57c9SCédric Le Goater .nregs = ASPEED_SMC_R_SMC_MAX, 198d09dc5b7SCédric Le Goater }, { 199d09dc5b7SCédric Le Goater .name = "aspeed.smc.fmc", 200d09dc5b7SCédric Le Goater .r_conf = R_CONF, 201d09dc5b7SCédric Le Goater .r_ce_ctrl = R_CE_CTRL, 202d09dc5b7SCédric Le Goater .r_ctrl0 = R_CTRL0, 203d09dc5b7SCédric Le Goater .r_timings = R_TIMINGS, 204d09dc5b7SCédric Le Goater .conf_enable_w0 = CONF_ENABLE_W0, 205d09dc5b7SCédric Le Goater .max_slaves = 5, 206d09dc5b7SCédric Le Goater .segments = aspeed_segments_fmc, 207d09dc5b7SCédric Le Goater .flash_window_base = ASPEED_SOC_FMC_FLASH_BASE, 208d09dc5b7SCédric Le Goater .flash_window_size = 0x10000000, 209d09dc5b7SCédric Le Goater .has_dma = true, 210087b57c9SCédric Le Goater .nregs = ASPEED_SMC_R_MAX, 211d09dc5b7SCédric Le Goater }, { 212d09dc5b7SCédric Le Goater .name = "aspeed.smc.spi", 213d09dc5b7SCédric Le Goater .r_conf = R_SPI_CONF, 214d09dc5b7SCédric Le Goater .r_ce_ctrl = 0xff, 215d09dc5b7SCédric Le Goater .r_ctrl0 = R_SPI_CTRL0, 216d09dc5b7SCédric Le Goater .r_timings = R_SPI_TIMINGS, 217d09dc5b7SCédric Le Goater .conf_enable_w0 = SPI_CONF_ENABLE_W0, 218d09dc5b7SCédric Le Goater .max_slaves = 1, 219d09dc5b7SCédric Le Goater .segments = aspeed_segments_spi, 220d09dc5b7SCédric Le Goater .flash_window_base = ASPEED_SOC_SPI_FLASH_BASE, 221d09dc5b7SCédric Le Goater .flash_window_size = 0x10000000, 222d09dc5b7SCédric Le Goater .has_dma = false, 223087b57c9SCédric Le Goater .nregs = ASPEED_SMC_R_SPI_MAX, 224d09dc5b7SCédric Le Goater }, { 225d09dc5b7SCédric Le Goater .name = "aspeed.smc.ast2500-fmc", 226d09dc5b7SCédric Le Goater .r_conf = R_CONF, 227d09dc5b7SCédric Le Goater .r_ce_ctrl = R_CE_CTRL, 228d09dc5b7SCédric Le Goater .r_ctrl0 = R_CTRL0, 229d09dc5b7SCédric Le Goater .r_timings = R_TIMINGS, 230d09dc5b7SCédric Le Goater .conf_enable_w0 = CONF_ENABLE_W0, 231d09dc5b7SCédric Le Goater .max_slaves = 3, 232d09dc5b7SCédric Le Goater .segments = aspeed_segments_ast2500_fmc, 233d09dc5b7SCédric Le Goater .flash_window_base = ASPEED_SOC_FMC_FLASH_BASE, 234d09dc5b7SCédric Le Goater .flash_window_size = 0x10000000, 235d09dc5b7SCédric Le Goater .has_dma = true, 236087b57c9SCédric Le Goater .nregs = ASPEED_SMC_R_MAX, 237d09dc5b7SCédric Le Goater }, { 238d09dc5b7SCédric Le Goater .name = "aspeed.smc.ast2500-spi1", 239d09dc5b7SCédric Le Goater .r_conf = R_CONF, 240d09dc5b7SCédric Le Goater .r_ce_ctrl = R_CE_CTRL, 241d09dc5b7SCédric Le Goater .r_ctrl0 = R_CTRL0, 242d09dc5b7SCédric Le Goater .r_timings = R_TIMINGS, 243d09dc5b7SCédric Le Goater .conf_enable_w0 = CONF_ENABLE_W0, 244d09dc5b7SCédric Le Goater .max_slaves = 2, 245d09dc5b7SCédric Le Goater .segments = aspeed_segments_ast2500_spi1, 246d09dc5b7SCédric Le Goater .flash_window_base = ASPEED_SOC_SPI_FLASH_BASE, 247d09dc5b7SCédric Le Goater .flash_window_size = 0x8000000, 248d09dc5b7SCédric Le Goater .has_dma = false, 249087b57c9SCédric Le Goater .nregs = ASPEED_SMC_R_MAX, 250d09dc5b7SCédric Le Goater }, { 251d09dc5b7SCédric Le Goater .name = "aspeed.smc.ast2500-spi2", 252d09dc5b7SCédric Le Goater .r_conf = R_CONF, 253d09dc5b7SCédric Le Goater .r_ce_ctrl = R_CE_CTRL, 254d09dc5b7SCédric Le Goater .r_ctrl0 = R_CTRL0, 255d09dc5b7SCédric Le Goater .r_timings = R_TIMINGS, 256d09dc5b7SCédric Le Goater .conf_enable_w0 = CONF_ENABLE_W0, 257d09dc5b7SCédric Le Goater .max_slaves = 2, 258d09dc5b7SCédric Le Goater .segments = aspeed_segments_ast2500_spi2, 259d09dc5b7SCédric Le Goater .flash_window_base = ASPEED_SOC_SPI2_FLASH_BASE, 260d09dc5b7SCédric Le Goater .flash_window_size = 0x8000000, 261d09dc5b7SCédric Le Goater .has_dma = false, 262087b57c9SCédric Le Goater .nregs = ASPEED_SMC_R_MAX, 263d09dc5b7SCédric Le Goater }, 264924ed163SCédric Le Goater }; 265924ed163SCédric Le Goater 266a03cb1daSCédric Le Goater /* 267a03cb1daSCédric Le Goater * The Segment Register uses a 8MB unit to encode the start address 268a03cb1daSCédric Le Goater * and the end address of the mapping window of a flash SPI slave : 269a03cb1daSCédric Le Goater * 270a03cb1daSCédric Le Goater * | byte 1 | byte 2 | byte 3 | byte 4 | 271a03cb1daSCédric Le Goater * +--------+--------+--------+--------+ 272a03cb1daSCédric Le Goater * | end | start | 0 | 0 | 273a03cb1daSCédric Le Goater * 274a03cb1daSCédric Le Goater */ 275a03cb1daSCédric Le Goater static inline uint32_t aspeed_smc_segment_to_reg(const AspeedSegments *seg) 276a03cb1daSCédric Le Goater { 277a03cb1daSCédric Le Goater uint32_t reg = 0; 278a03cb1daSCédric Le Goater reg |= ((seg->addr >> 23) & SEG_START_MASK) << SEG_START_SHIFT; 279a03cb1daSCédric Le Goater reg |= (((seg->addr + seg->size) >> 23) & SEG_END_MASK) << SEG_END_SHIFT; 280a03cb1daSCédric Le Goater return reg; 281a03cb1daSCédric Le Goater } 282a03cb1daSCédric Le Goater 283a03cb1daSCédric Le Goater static inline void aspeed_smc_reg_to_segment(uint32_t reg, AspeedSegments *seg) 284a03cb1daSCédric Le Goater { 285a03cb1daSCédric Le Goater seg->addr = ((reg >> SEG_START_SHIFT) & SEG_START_MASK) << 23; 286a03cb1daSCédric Le Goater seg->size = (((reg >> SEG_END_SHIFT) & SEG_END_MASK) << 23) - seg->addr; 287a03cb1daSCédric Le Goater } 288a03cb1daSCédric Le Goater 289a03cb1daSCédric Le Goater static bool aspeed_smc_flash_overlap(const AspeedSMCState *s, 290a03cb1daSCédric Le Goater const AspeedSegments *new, 291a03cb1daSCédric Le Goater int cs) 292a03cb1daSCédric Le Goater { 293a03cb1daSCédric Le Goater AspeedSegments seg; 294a03cb1daSCédric Le Goater int i; 295a03cb1daSCédric Le Goater 296a03cb1daSCédric Le Goater for (i = 0; i < s->ctrl->max_slaves; i++) { 297a03cb1daSCédric Le Goater if (i == cs) { 298a03cb1daSCédric Le Goater continue; 299a03cb1daSCédric Le Goater } 300a03cb1daSCédric Le Goater 301a03cb1daSCédric Le Goater aspeed_smc_reg_to_segment(s->regs[R_SEG_ADDR0 + i], &seg); 302a03cb1daSCédric Le Goater 303a03cb1daSCédric Le Goater if (new->addr + new->size > seg.addr && 304a03cb1daSCédric Le Goater new->addr < seg.addr + seg.size) { 305a03cb1daSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: new segment CS%d [ 0x%" 306a03cb1daSCédric Le Goater HWADDR_PRIx" - 0x%"HWADDR_PRIx" ] overlaps with " 307a03cb1daSCédric Le Goater "CS%d [ 0x%"HWADDR_PRIx" - 0x%"HWADDR_PRIx" ]\n", 308a03cb1daSCédric Le Goater s->ctrl->name, cs, new->addr, new->addr + new->size, 309a03cb1daSCédric Le Goater i, seg.addr, seg.addr + seg.size); 310a03cb1daSCédric Le Goater return true; 311a03cb1daSCédric Le Goater } 312a03cb1daSCédric Le Goater } 313a03cb1daSCédric Le Goater return false; 314a03cb1daSCédric Le Goater } 315a03cb1daSCédric Le Goater 316a03cb1daSCédric Le Goater static void aspeed_smc_flash_set_segment(AspeedSMCState *s, int cs, 317a03cb1daSCédric Le Goater uint64_t new) 318a03cb1daSCédric Le Goater { 319a03cb1daSCédric Le Goater AspeedSMCFlash *fl = &s->flashes[cs]; 320a03cb1daSCédric Le Goater AspeedSegments seg; 321a03cb1daSCédric Le Goater 322a03cb1daSCédric Le Goater aspeed_smc_reg_to_segment(new, &seg); 323a03cb1daSCédric Le Goater 324a03cb1daSCédric Le Goater /* The start address of CS0 is read-only */ 325a03cb1daSCédric Le Goater if (cs == 0 && seg.addr != s->ctrl->flash_window_base) { 326a03cb1daSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, 327a03cb1daSCédric Le Goater "%s: Tried to change CS0 start address to 0x%" 328a03cb1daSCédric Le Goater HWADDR_PRIx "\n", s->ctrl->name, seg.addr); 3290584d3c3SCédric Le Goater seg.addr = s->ctrl->flash_window_base; 3300584d3c3SCédric Le Goater new = aspeed_smc_segment_to_reg(&seg); 331a03cb1daSCédric Le Goater } 332a03cb1daSCédric Le Goater 333a03cb1daSCédric Le Goater /* 334a03cb1daSCédric Le Goater * The end address of the AST2500 spi controllers is also 335a03cb1daSCédric Le Goater * read-only. 336a03cb1daSCédric Le Goater */ 337a03cb1daSCédric Le Goater if ((s->ctrl->segments == aspeed_segments_ast2500_spi1 || 338a03cb1daSCédric Le Goater s->ctrl->segments == aspeed_segments_ast2500_spi2) && 339a03cb1daSCédric Le Goater cs == s->ctrl->max_slaves && 340a03cb1daSCédric Le Goater seg.addr + seg.size != s->ctrl->segments[cs].addr + 341a03cb1daSCédric Le Goater s->ctrl->segments[cs].size) { 342a03cb1daSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, 343a03cb1daSCédric Le Goater "%s: Tried to change CS%d end address to 0x%" 3440584d3c3SCédric Le Goater HWADDR_PRIx "\n", s->ctrl->name, cs, seg.addr + seg.size); 3450584d3c3SCédric Le Goater seg.size = s->ctrl->segments[cs].addr + s->ctrl->segments[cs].size - 3460584d3c3SCédric Le Goater seg.addr; 3470584d3c3SCédric Le Goater new = aspeed_smc_segment_to_reg(&seg); 348a03cb1daSCédric Le Goater } 349a03cb1daSCédric Le Goater 350a03cb1daSCédric Le Goater /* Keep the segment in the overall flash window */ 351a03cb1daSCédric Le Goater if (seg.addr + seg.size <= s->ctrl->flash_window_base || 352a03cb1daSCédric Le Goater seg.addr > s->ctrl->flash_window_base + s->ctrl->flash_window_size) { 353a03cb1daSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: new segment for CS%d is invalid : " 354a03cb1daSCédric Le Goater "[ 0x%"HWADDR_PRIx" - 0x%"HWADDR_PRIx" ]\n", 355a03cb1daSCédric Le Goater s->ctrl->name, cs, seg.addr, seg.addr + seg.size); 356a03cb1daSCédric Le Goater return; 357a03cb1daSCédric Le Goater } 358a03cb1daSCédric Le Goater 359a03cb1daSCédric Le Goater /* Check start address vs. alignment */ 3600584d3c3SCédric Le Goater if (seg.size && !QEMU_IS_ALIGNED(seg.addr, seg.size)) { 361a03cb1daSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: new segment for CS%d is not " 362a03cb1daSCédric Le Goater "aligned : [ 0x%"HWADDR_PRIx" - 0x%"HWADDR_PRIx" ]\n", 363a03cb1daSCédric Le Goater s->ctrl->name, cs, seg.addr, seg.addr + seg.size); 364a03cb1daSCédric Le Goater } 365a03cb1daSCédric Le Goater 3660584d3c3SCédric Le Goater /* And segments should not overlap (in the specs) */ 3670584d3c3SCédric Le Goater aspeed_smc_flash_overlap(s, &seg, cs); 368a03cb1daSCédric Le Goater 369a03cb1daSCédric Le Goater /* All should be fine now to move the region */ 370a03cb1daSCédric Le Goater memory_region_transaction_begin(); 371a03cb1daSCédric Le Goater memory_region_set_size(&fl->mmio, seg.size); 372a03cb1daSCédric Le Goater memory_region_set_address(&fl->mmio, seg.addr - s->ctrl->flash_window_base); 373a03cb1daSCédric Le Goater memory_region_set_enabled(&fl->mmio, true); 374a03cb1daSCédric Le Goater memory_region_transaction_commit(); 375a03cb1daSCédric Le Goater 376a03cb1daSCédric Le Goater s->regs[R_SEG_ADDR0 + cs] = new; 377a03cb1daSCédric Le Goater } 378a03cb1daSCédric Le Goater 379924ed163SCédric Le Goater static uint64_t aspeed_smc_flash_default_read(void *opaque, hwaddr addr, 380924ed163SCédric Le Goater unsigned size) 381924ed163SCédric Le Goater { 382924ed163SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: To 0x%" HWADDR_PRIx " of size %u" 383924ed163SCédric Le Goater PRIx64 "\n", __func__, addr, size); 384924ed163SCédric Le Goater return 0; 385924ed163SCédric Le Goater } 386924ed163SCédric Le Goater 387924ed163SCédric Le Goater static void aspeed_smc_flash_default_write(void *opaque, hwaddr addr, 388924ed163SCédric Le Goater uint64_t data, unsigned size) 389924ed163SCédric Le Goater { 390924ed163SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: To 0x%" HWADDR_PRIx " of size %u: 0x%" 391924ed163SCédric Le Goater PRIx64 "\n", __func__, addr, size, data); 392924ed163SCédric Le Goater } 393924ed163SCédric Le Goater 394924ed163SCédric Le Goater static const MemoryRegionOps aspeed_smc_flash_default_ops = { 395924ed163SCédric Le Goater .read = aspeed_smc_flash_default_read, 396924ed163SCédric Le Goater .write = aspeed_smc_flash_default_write, 397924ed163SCédric Le Goater .endianness = DEVICE_LITTLE_ENDIAN, 398924ed163SCédric Le Goater .valid = { 399924ed163SCédric Le Goater .min_access_size = 1, 400924ed163SCédric Le Goater .max_access_size = 4, 401924ed163SCédric Le Goater }, 402924ed163SCédric Le Goater }; 403924ed163SCédric Le Goater 404f248a9dbSCédric Le Goater static inline int aspeed_smc_flash_mode(const AspeedSMCFlash *fl) 405924ed163SCédric Le Goater { 406f248a9dbSCédric Le Goater const AspeedSMCState *s = fl->controller; 407f248a9dbSCédric Le Goater 408f248a9dbSCédric Le Goater return s->regs[s->r_ctrl0 + fl->id] & CTRL_CMD_MODE_MASK; 409924ed163SCédric Le Goater } 410924ed163SCédric Le Goater 411f248a9dbSCédric Le Goater static inline bool aspeed_smc_is_writable(const AspeedSMCFlash *fl) 412924ed163SCédric Le Goater { 413f248a9dbSCédric Le Goater const AspeedSMCState *s = fl->controller; 414f248a9dbSCédric Le Goater 415f248a9dbSCédric Le Goater return s->regs[s->r_conf] & (1 << (s->conf_enable_w0 + fl->id)); 416924ed163SCédric Le Goater } 417924ed163SCédric Le Goater 418fcdf2c59SCédric Le Goater static inline int aspeed_smc_flash_cmd(const AspeedSMCFlash *fl) 419fcdf2c59SCédric Le Goater { 420fcdf2c59SCédric Le Goater const AspeedSMCState *s = fl->controller; 421fcdf2c59SCédric Le Goater int cmd = (s->regs[s->r_ctrl0 + fl->id] >> CTRL_CMD_SHIFT) & CTRL_CMD_MASK; 422fcdf2c59SCédric Le Goater 423fcdf2c59SCédric Le Goater /* In read mode, the default SPI command is READ (0x3). In other 424fcdf2c59SCédric Le Goater * modes, the command should necessarily be defined */ 425fcdf2c59SCédric Le Goater if (aspeed_smc_flash_mode(fl) == CTRL_READMODE) { 426fcdf2c59SCédric Le Goater cmd = SPI_OP_READ; 427fcdf2c59SCédric Le Goater } 428fcdf2c59SCédric Le Goater 429fcdf2c59SCédric Le Goater if (!cmd) { 430fcdf2c59SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: no command defined for mode %d\n", 431fcdf2c59SCédric Le Goater __func__, aspeed_smc_flash_mode(fl)); 432fcdf2c59SCédric Le Goater } 433fcdf2c59SCédric Le Goater 434fcdf2c59SCédric Le Goater return cmd; 435fcdf2c59SCédric Le Goater } 436fcdf2c59SCédric Le Goater 437fcdf2c59SCédric Le Goater static inline int aspeed_smc_flash_is_4byte(const AspeedSMCFlash *fl) 438fcdf2c59SCédric Le Goater { 439fcdf2c59SCédric Le Goater const AspeedSMCState *s = fl->controller; 440fcdf2c59SCédric Le Goater 441fcdf2c59SCédric Le Goater if (s->ctrl->segments == aspeed_segments_spi) { 442fcdf2c59SCédric Le Goater return s->regs[s->r_ctrl0] & CTRL_AST2400_SPI_4BYTE; 443fcdf2c59SCédric Le Goater } else { 444fcdf2c59SCédric Le Goater return s->regs[s->r_ce_ctrl] & (1 << (CTRL_EXTENDED0 + fl->id)); 445fcdf2c59SCédric Le Goater } 446fcdf2c59SCédric Le Goater } 447fcdf2c59SCédric Le Goater 448fcdf2c59SCédric Le Goater static inline bool aspeed_smc_is_ce_stop_active(const AspeedSMCFlash *fl) 449fcdf2c59SCédric Le Goater { 450fcdf2c59SCédric Le Goater const AspeedSMCState *s = fl->controller; 451fcdf2c59SCédric Le Goater 452fcdf2c59SCédric Le Goater return s->regs[s->r_ctrl0 + fl->id] & CTRL_CE_STOP_ACTIVE; 453fcdf2c59SCédric Le Goater } 454fcdf2c59SCédric Le Goater 455fcdf2c59SCédric Le Goater static void aspeed_smc_flash_select(AspeedSMCFlash *fl) 456fcdf2c59SCédric Le Goater { 457fcdf2c59SCédric Le Goater AspeedSMCState *s = fl->controller; 458fcdf2c59SCédric Le Goater 459fcdf2c59SCédric Le Goater s->regs[s->r_ctrl0 + fl->id] &= ~CTRL_CE_STOP_ACTIVE; 460fcdf2c59SCédric Le Goater qemu_set_irq(s->cs_lines[fl->id], aspeed_smc_is_ce_stop_active(fl)); 461fcdf2c59SCédric Le Goater } 462fcdf2c59SCédric Le Goater 463fcdf2c59SCédric Le Goater static void aspeed_smc_flash_unselect(AspeedSMCFlash *fl) 464fcdf2c59SCédric Le Goater { 465fcdf2c59SCédric Le Goater AspeedSMCState *s = fl->controller; 466fcdf2c59SCédric Le Goater 467fcdf2c59SCédric Le Goater s->regs[s->r_ctrl0 + fl->id] |= CTRL_CE_STOP_ACTIVE; 468fcdf2c59SCédric Le Goater qemu_set_irq(s->cs_lines[fl->id], aspeed_smc_is_ce_stop_active(fl)); 469fcdf2c59SCédric Le Goater } 470fcdf2c59SCédric Le Goater 471fcdf2c59SCédric Le Goater static uint32_t aspeed_smc_check_segment_addr(const AspeedSMCFlash *fl, 472fcdf2c59SCédric Le Goater uint32_t addr) 473fcdf2c59SCédric Le Goater { 474fcdf2c59SCédric Le Goater const AspeedSMCState *s = fl->controller; 475fcdf2c59SCédric Le Goater AspeedSegments seg; 476fcdf2c59SCédric Le Goater 477fcdf2c59SCédric Le Goater aspeed_smc_reg_to_segment(s->regs[R_SEG_ADDR0 + fl->id], &seg); 478b4cc583fSCédric Le Goater if ((addr % seg.size) != addr) { 479fcdf2c59SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, 480fcdf2c59SCédric Le Goater "%s: invalid address 0x%08x for CS%d segment : " 481fcdf2c59SCédric Le Goater "[ 0x%"HWADDR_PRIx" - 0x%"HWADDR_PRIx" ]\n", 482fcdf2c59SCédric Le Goater s->ctrl->name, addr, fl->id, seg.addr, 483fcdf2c59SCédric Le Goater seg.addr + seg.size); 484b4cc583fSCédric Le Goater addr %= seg.size; 485fcdf2c59SCédric Le Goater } 486fcdf2c59SCédric Le Goater 487fcdf2c59SCédric Le Goater return addr; 488fcdf2c59SCédric Le Goater } 489fcdf2c59SCédric Le Goater 490ac2810deSCédric Le Goater static int aspeed_smc_flash_dummies(const AspeedSMCFlash *fl) 491ac2810deSCédric Le Goater { 492ac2810deSCédric Le Goater const AspeedSMCState *s = fl->controller; 493ac2810deSCédric Le Goater uint32_t r_ctrl0 = s->regs[s->r_ctrl0 + fl->id]; 494ac2810deSCédric Le Goater uint32_t dummy_high = (r_ctrl0 >> CTRL_DUMMY_HIGH_SHIFT) & 0x1; 495ac2810deSCédric Le Goater uint32_t dummy_low = (r_ctrl0 >> CTRL_DUMMY_LOW_SHIFT) & 0x3; 496ac2810deSCédric Le Goater 497ac2810deSCédric Le Goater return ((dummy_high << 2) | dummy_low) * 8; 498ac2810deSCédric Le Goater } 499ac2810deSCédric Le Goater 500fcdf2c59SCédric Le Goater static void aspeed_smc_flash_send_addr(AspeedSMCFlash *fl, uint32_t addr) 501fcdf2c59SCédric Le Goater { 502fcdf2c59SCédric Le Goater const AspeedSMCState *s = fl->controller; 503fcdf2c59SCédric Le Goater uint8_t cmd = aspeed_smc_flash_cmd(fl); 504fcdf2c59SCédric Le Goater 505fcdf2c59SCédric Le Goater /* Flash access can not exceed CS segment */ 506fcdf2c59SCédric Le Goater addr = aspeed_smc_check_segment_addr(fl, addr); 507fcdf2c59SCédric Le Goater 508fcdf2c59SCédric Le Goater ssi_transfer(s->spi, cmd); 509fcdf2c59SCédric Le Goater 510fcdf2c59SCédric Le Goater if (aspeed_smc_flash_is_4byte(fl)) { 511fcdf2c59SCédric Le Goater ssi_transfer(s->spi, (addr >> 24) & 0xff); 512fcdf2c59SCédric Le Goater } 513fcdf2c59SCédric Le Goater ssi_transfer(s->spi, (addr >> 16) & 0xff); 514fcdf2c59SCédric Le Goater ssi_transfer(s->spi, (addr >> 8) & 0xff); 515fcdf2c59SCédric Le Goater ssi_transfer(s->spi, (addr & 0xff)); 516fcdf2c59SCédric Le Goater } 517fcdf2c59SCédric Le Goater 518924ed163SCédric Le Goater static uint64_t aspeed_smc_flash_read(void *opaque, hwaddr addr, unsigned size) 519924ed163SCédric Le Goater { 520924ed163SCédric Le Goater AspeedSMCFlash *fl = opaque; 521fcdf2c59SCédric Le Goater AspeedSMCState *s = fl->controller; 522924ed163SCédric Le Goater uint64_t ret = 0; 523924ed163SCédric Le Goater int i; 524924ed163SCédric Le Goater 525fcdf2c59SCédric Le Goater switch (aspeed_smc_flash_mode(fl)) { 526fcdf2c59SCédric Le Goater case CTRL_USERMODE: 527924ed163SCédric Le Goater for (i = 0; i < size; i++) { 528924ed163SCédric Le Goater ret |= ssi_transfer(s->spi, 0x0) << (8 * i); 529924ed163SCédric Le Goater } 530fcdf2c59SCédric Le Goater break; 531fcdf2c59SCédric Le Goater case CTRL_READMODE: 532fcdf2c59SCédric Le Goater case CTRL_FREADMODE: 533fcdf2c59SCédric Le Goater aspeed_smc_flash_select(fl); 534fcdf2c59SCédric Le Goater aspeed_smc_flash_send_addr(fl, addr); 535fcdf2c59SCédric Le Goater 536ac2810deSCédric Le Goater /* 537ac2810deSCédric Le Goater * Use fake transfers to model dummy bytes. The value should 538ac2810deSCédric Le Goater * be configured to some non-zero value in fast read mode and 5391a6d4fc2SCédric Le Goater * zero in read mode. But, as the HW allows inconsistent 5401a6d4fc2SCédric Le Goater * settings, let's check for fast read mode. 541ac2810deSCédric Le Goater */ 5421a6d4fc2SCédric Le Goater if (aspeed_smc_flash_mode(fl) == CTRL_FREADMODE) { 543ac2810deSCédric Le Goater for (i = 0; i < aspeed_smc_flash_dummies(fl); i++) { 544ac2810deSCédric Le Goater ssi_transfer(fl->controller->spi, 0xFF); 545ac2810deSCédric Le Goater } 5461a6d4fc2SCédric Le Goater } 547ac2810deSCédric Le Goater 548fcdf2c59SCédric Le Goater for (i = 0; i < size; i++) { 549fcdf2c59SCédric Le Goater ret |= ssi_transfer(s->spi, 0x0) << (8 * i); 550fcdf2c59SCédric Le Goater } 551fcdf2c59SCédric Le Goater 552fcdf2c59SCédric Le Goater aspeed_smc_flash_unselect(fl); 553fcdf2c59SCédric Le Goater break; 554fcdf2c59SCédric Le Goater default: 555fcdf2c59SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid flash mode %d\n", 556fcdf2c59SCédric Le Goater __func__, aspeed_smc_flash_mode(fl)); 557924ed163SCédric Le Goater } 558924ed163SCédric Le Goater 559924ed163SCédric Le Goater return ret; 560924ed163SCédric Le Goater } 561924ed163SCédric Le Goater 562924ed163SCédric Le Goater static void aspeed_smc_flash_write(void *opaque, hwaddr addr, uint64_t data, 563924ed163SCédric Le Goater unsigned size) 564924ed163SCédric Le Goater { 565924ed163SCédric Le Goater AspeedSMCFlash *fl = opaque; 566fcdf2c59SCédric Le Goater AspeedSMCState *s = fl->controller; 567924ed163SCédric Le Goater int i; 568924ed163SCédric Le Goater 569f248a9dbSCédric Le Goater if (!aspeed_smc_is_writable(fl)) { 570924ed163SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: flash is not writable at 0x%" 571924ed163SCédric Le Goater HWADDR_PRIx "\n", __func__, addr); 572924ed163SCédric Le Goater return; 573924ed163SCédric Le Goater } 574924ed163SCédric Le Goater 575fcdf2c59SCédric Le Goater switch (aspeed_smc_flash_mode(fl)) { 576fcdf2c59SCédric Le Goater case CTRL_USERMODE: 577fcdf2c59SCédric Le Goater for (i = 0; i < size; i++) { 578fcdf2c59SCédric Le Goater ssi_transfer(s->spi, (data >> (8 * i)) & 0xff); 579924ed163SCédric Le Goater } 580fcdf2c59SCédric Le Goater break; 581fcdf2c59SCédric Le Goater case CTRL_WRITEMODE: 582fcdf2c59SCédric Le Goater aspeed_smc_flash_select(fl); 583fcdf2c59SCédric Le Goater aspeed_smc_flash_send_addr(fl, addr); 584924ed163SCédric Le Goater 585924ed163SCédric Le Goater for (i = 0; i < size; i++) { 586924ed163SCédric Le Goater ssi_transfer(s->spi, (data >> (8 * i)) & 0xff); 587924ed163SCédric Le Goater } 588fcdf2c59SCédric Le Goater 589fcdf2c59SCédric Le Goater aspeed_smc_flash_unselect(fl); 590fcdf2c59SCédric Le Goater break; 591fcdf2c59SCédric Le Goater default: 592fcdf2c59SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid flash mode %d\n", 593fcdf2c59SCédric Le Goater __func__, aspeed_smc_flash_mode(fl)); 594fcdf2c59SCédric Le Goater } 595924ed163SCédric Le Goater } 596924ed163SCédric Le Goater 597924ed163SCédric Le Goater static const MemoryRegionOps aspeed_smc_flash_ops = { 598924ed163SCédric Le Goater .read = aspeed_smc_flash_read, 599924ed163SCédric Le Goater .write = aspeed_smc_flash_write, 600924ed163SCédric Le Goater .endianness = DEVICE_LITTLE_ENDIAN, 601924ed163SCédric Le Goater .valid = { 602924ed163SCédric Le Goater .min_access_size = 1, 603924ed163SCédric Le Goater .max_access_size = 4, 604924ed163SCédric Le Goater }, 6057c1c69bcSCédric Le Goater }; 6067c1c69bcSCédric Le Goater 607f248a9dbSCédric Le Goater static void aspeed_smc_flash_update_cs(AspeedSMCFlash *fl) 6087c1c69bcSCédric Le Goater { 609f248a9dbSCédric Le Goater const AspeedSMCState *s = fl->controller; 6107c1c69bcSCédric Le Goater 611f248a9dbSCédric Le Goater qemu_set_irq(s->cs_lines[fl->id], aspeed_smc_is_ce_stop_active(fl)); 6127c1c69bcSCédric Le Goater } 6137c1c69bcSCédric Le Goater 6147c1c69bcSCédric Le Goater static void aspeed_smc_reset(DeviceState *d) 6157c1c69bcSCédric Le Goater { 6167c1c69bcSCédric Le Goater AspeedSMCState *s = ASPEED_SMC(d); 6177c1c69bcSCédric Le Goater int i; 6187c1c69bcSCédric Le Goater 6197c1c69bcSCédric Le Goater memset(s->regs, 0, sizeof s->regs); 6207c1c69bcSCédric Le Goater 6212e1f0502SCédric Le Goater /* Pretend DMA is done (u-boot initialization) */ 6222e1f0502SCédric Le Goater s->regs[R_INTR_CTRL] = INTR_CTRL_DMA_STATUS; 6232e1f0502SCédric Le Goater 6247c1c69bcSCédric Le Goater /* Unselect all slaves */ 6257c1c69bcSCédric Le Goater for (i = 0; i < s->num_cs; ++i) { 6267c1c69bcSCédric Le Goater s->regs[s->r_ctrl0 + i] |= CTRL_CE_STOP_ACTIVE; 6271d247bd0SCédric Le Goater qemu_set_irq(s->cs_lines[i], true); 6287c1c69bcSCédric Le Goater } 6297c1c69bcSCédric Le Goater 630a03cb1daSCédric Le Goater /* setup default segment register values for all */ 631a03cb1daSCédric Le Goater for (i = 0; i < s->ctrl->max_slaves; ++i) { 632a03cb1daSCédric Le Goater s->regs[R_SEG_ADDR0 + i] = 633a03cb1daSCédric Le Goater aspeed_smc_segment_to_reg(&s->ctrl->segments[i]); 634a03cb1daSCédric Le Goater } 6350707b34dSCédric Le Goater 6360707b34dSCédric Le Goater /* HW strapping for AST2500 FMC controllers */ 6370707b34dSCédric Le Goater if (s->ctrl->segments == aspeed_segments_ast2500_fmc) { 6380707b34dSCédric Le Goater /* flash type is fixed to SPI for CE0 and CE1 */ 6390707b34dSCédric Le Goater s->regs[s->r_conf] |= (CONF_FLASH_TYPE_SPI << CONF_FLASH_TYPE0); 6400707b34dSCédric Le Goater s->regs[s->r_conf] |= (CONF_FLASH_TYPE_SPI << CONF_FLASH_TYPE1); 6410707b34dSCédric Le Goater 6420707b34dSCédric Le Goater /* 4BYTE mode is autodetected for CE0. Let's force it to 1 for 6430707b34dSCédric Le Goater * now */ 6440707b34dSCédric Le Goater s->regs[s->r_ce_ctrl] |= (1 << (CTRL_EXTENDED0)); 6450707b34dSCédric Le Goater } 6460707b34dSCédric Le Goater 6470707b34dSCédric Le Goater /* HW strapping for AST2400 FMC controllers (SCU70). Let's use the 6480707b34dSCédric Le Goater * configuration of the palmetto-bmc machine */ 6490707b34dSCédric Le Goater if (s->ctrl->segments == aspeed_segments_fmc) { 6500707b34dSCédric Le Goater s->regs[s->r_conf] |= (CONF_FLASH_TYPE_SPI << CONF_FLASH_TYPE0); 6510707b34dSCédric Le Goater 6520707b34dSCédric Le Goater s->regs[s->r_ce_ctrl] |= (1 << (CTRL_EXTENDED0)); 6530707b34dSCédric Le Goater } 6547c1c69bcSCédric Le Goater } 6557c1c69bcSCédric Le Goater 6567c1c69bcSCédric Le Goater static uint64_t aspeed_smc_read(void *opaque, hwaddr addr, unsigned int size) 6577c1c69bcSCédric Le Goater { 6587c1c69bcSCédric Le Goater AspeedSMCState *s = ASPEED_SMC(opaque); 6597c1c69bcSCédric Le Goater 6607c1c69bcSCédric Le Goater addr >>= 2; 6617c1c69bcSCédric Le Goater 66297c2ed5dSCédric Le Goater if (addr == s->r_conf || 66397c2ed5dSCédric Le Goater addr == s->r_timings || 66497c2ed5dSCédric Le Goater addr == s->r_ce_ctrl || 6652e1f0502SCédric Le Goater addr == R_INTR_CTRL || 666a03cb1daSCédric Le Goater (addr >= R_SEG_ADDR0 && addr < R_SEG_ADDR0 + s->ctrl->max_slaves) || 66797c2ed5dSCédric Le Goater (addr >= s->r_ctrl0 && addr < s->r_ctrl0 + s->num_cs)) { 66897c2ed5dSCédric Le Goater return s->regs[addr]; 66997c2ed5dSCédric Le Goater } else { 6707c1c69bcSCédric Le Goater qemu_log_mask(LOG_UNIMP, "%s: not implemented: 0x%" HWADDR_PRIx "\n", 6717c1c69bcSCédric Le Goater __func__, addr); 6727c1c69bcSCédric Le Goater return 0; 6737c1c69bcSCédric Le Goater } 6747c1c69bcSCédric Le Goater } 6757c1c69bcSCédric Le Goater 6767c1c69bcSCédric Le Goater static void aspeed_smc_write(void *opaque, hwaddr addr, uint64_t data, 6777c1c69bcSCédric Le Goater unsigned int size) 6787c1c69bcSCédric Le Goater { 6797c1c69bcSCédric Le Goater AspeedSMCState *s = ASPEED_SMC(opaque); 6807c1c69bcSCédric Le Goater uint32_t value = data; 6817c1c69bcSCédric Le Goater 6827c1c69bcSCédric Le Goater addr >>= 2; 6837c1c69bcSCédric Le Goater 68497c2ed5dSCédric Le Goater if (addr == s->r_conf || 68597c2ed5dSCédric Le Goater addr == s->r_timings || 68697c2ed5dSCédric Le Goater addr == s->r_ce_ctrl) { 68797c2ed5dSCédric Le Goater s->regs[addr] = value; 68897c2ed5dSCédric Le Goater } else if (addr >= s->r_ctrl0 && addr < s->r_ctrl0 + s->num_cs) { 689f248a9dbSCédric Le Goater int cs = addr - s->r_ctrl0; 69097c2ed5dSCédric Le Goater s->regs[addr] = value; 691f248a9dbSCédric Le Goater aspeed_smc_flash_update_cs(&s->flashes[cs]); 692a03cb1daSCédric Le Goater } else if (addr >= R_SEG_ADDR0 && 693a03cb1daSCédric Le Goater addr < R_SEG_ADDR0 + s->ctrl->max_slaves) { 694a03cb1daSCédric Le Goater int cs = addr - R_SEG_ADDR0; 695a03cb1daSCédric Le Goater 696a03cb1daSCédric Le Goater if (value != s->regs[R_SEG_ADDR0 + cs]) { 697a03cb1daSCédric Le Goater aspeed_smc_flash_set_segment(s, cs, value); 698a03cb1daSCédric Le Goater } 69997c2ed5dSCédric Le Goater } else { 7007c1c69bcSCédric Le Goater qemu_log_mask(LOG_UNIMP, "%s: not implemented: 0x%" HWADDR_PRIx "\n", 7017c1c69bcSCédric Le Goater __func__, addr); 7027c1c69bcSCédric Le Goater return; 7037c1c69bcSCédric Le Goater } 7047c1c69bcSCédric Le Goater } 7057c1c69bcSCédric Le Goater 7067c1c69bcSCédric Le Goater static const MemoryRegionOps aspeed_smc_ops = { 7077c1c69bcSCédric Le Goater .read = aspeed_smc_read, 7087c1c69bcSCédric Le Goater .write = aspeed_smc_write, 7097c1c69bcSCédric Le Goater .endianness = DEVICE_LITTLE_ENDIAN, 7107c1c69bcSCédric Le Goater .valid.unaligned = true, 7117c1c69bcSCédric Le Goater }; 7127c1c69bcSCédric Le Goater 7137c1c69bcSCédric Le Goater static void aspeed_smc_realize(DeviceState *dev, Error **errp) 7147c1c69bcSCédric Le Goater { 7157c1c69bcSCédric Le Goater SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 7167c1c69bcSCédric Le Goater AspeedSMCState *s = ASPEED_SMC(dev); 7177c1c69bcSCédric Le Goater AspeedSMCClass *mc = ASPEED_SMC_GET_CLASS(s); 7187c1c69bcSCédric Le Goater int i; 719924ed163SCédric Le Goater char name[32]; 720924ed163SCédric Le Goater hwaddr offset = 0; 7217c1c69bcSCédric Le Goater 7227c1c69bcSCédric Le Goater s->ctrl = mc->ctrl; 7237c1c69bcSCédric Le Goater 7247c1c69bcSCédric Le Goater /* keep a copy under AspeedSMCState to speed up accesses */ 7257c1c69bcSCédric Le Goater s->r_conf = s->ctrl->r_conf; 7267c1c69bcSCédric Le Goater s->r_ce_ctrl = s->ctrl->r_ce_ctrl; 7277c1c69bcSCédric Le Goater s->r_ctrl0 = s->ctrl->r_ctrl0; 7287c1c69bcSCédric Le Goater s->r_timings = s->ctrl->r_timings; 7297c1c69bcSCédric Le Goater s->conf_enable_w0 = s->ctrl->conf_enable_w0; 7307c1c69bcSCédric Le Goater 7317c1c69bcSCédric Le Goater /* Enforce some real HW limits */ 7327c1c69bcSCédric Le Goater if (s->num_cs > s->ctrl->max_slaves) { 7337c1c69bcSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: num_cs cannot exceed: %d\n", 7347c1c69bcSCédric Le Goater __func__, s->ctrl->max_slaves); 7357c1c69bcSCédric Le Goater s->num_cs = s->ctrl->max_slaves; 7367c1c69bcSCédric Le Goater } 7377c1c69bcSCédric Le Goater 7387c1c69bcSCédric Le Goater s->spi = ssi_create_bus(dev, "spi"); 7397c1c69bcSCédric Le Goater 7407c1c69bcSCédric Le Goater /* Setup cs_lines for slaves */ 7417c1c69bcSCédric Le Goater sysbus_init_irq(sbd, &s->irq); 7427c1c69bcSCédric Le Goater s->cs_lines = g_new0(qemu_irq, s->num_cs); 7437c1c69bcSCédric Le Goater ssi_auto_connect_slaves(dev, s->cs_lines, s->spi); 7447c1c69bcSCédric Le Goater 7457c1c69bcSCédric Le Goater for (i = 0; i < s->num_cs; ++i) { 7467c1c69bcSCédric Le Goater sysbus_init_irq(sbd, &s->cs_lines[i]); 7477c1c69bcSCédric Le Goater } 7487c1c69bcSCédric Le Goater 7492da95fd8SCédric Le Goater /* The memory region for the controller registers */ 7507c1c69bcSCédric Le Goater memory_region_init_io(&s->mmio, OBJECT(s), &aspeed_smc_ops, s, 751087b57c9SCédric Le Goater s->ctrl->name, s->ctrl->nregs * 4); 7527c1c69bcSCédric Le Goater sysbus_init_mmio(sbd, &s->mmio); 753924ed163SCédric Le Goater 754924ed163SCédric Le Goater /* 7552da95fd8SCédric Le Goater * The container memory region representing the address space 7562da95fd8SCédric Le Goater * window in which the flash modules are mapped. The size and 7572da95fd8SCédric Le Goater * address depends on the SoC model and controller type. 758924ed163SCédric Le Goater */ 759924ed163SCédric Le Goater snprintf(name, sizeof(name), "%s.flash", s->ctrl->name); 760924ed163SCédric Le Goater 761924ed163SCédric Le Goater memory_region_init_io(&s->mmio_flash, OBJECT(s), 762924ed163SCédric Le Goater &aspeed_smc_flash_default_ops, s, name, 763dcb83444SCédric Le Goater s->ctrl->flash_window_size); 764924ed163SCédric Le Goater sysbus_init_mmio(sbd, &s->mmio_flash); 765924ed163SCédric Le Goater 7662da95fd8SCédric Le Goater s->flashes = g_new0(AspeedSMCFlash, s->ctrl->max_slaves); 767924ed163SCédric Le Goater 7682da95fd8SCédric Le Goater /* 7692da95fd8SCédric Le Goater * Let's create a sub memory region for each possible slave. All 7702da95fd8SCédric Le Goater * have a configurable memory segment in the overall flash mapping 7712da95fd8SCédric Le Goater * window of the controller but, there is not necessarily a flash 7722da95fd8SCédric Le Goater * module behind to handle the memory accesses. This depends on 7732da95fd8SCédric Le Goater * the board configuration. 7742da95fd8SCédric Le Goater */ 7752da95fd8SCédric Le Goater for (i = 0; i < s->ctrl->max_slaves; ++i) { 776924ed163SCédric Le Goater AspeedSMCFlash *fl = &s->flashes[i]; 777924ed163SCédric Le Goater 778924ed163SCédric Le Goater snprintf(name, sizeof(name), "%s.%d", s->ctrl->name, i); 779924ed163SCédric Le Goater 780924ed163SCédric Le Goater fl->id = i; 781924ed163SCédric Le Goater fl->controller = s; 782924ed163SCédric Le Goater fl->size = s->ctrl->segments[i].size; 783924ed163SCédric Le Goater memory_region_init_io(&fl->mmio, OBJECT(s), &aspeed_smc_flash_ops, 784924ed163SCédric Le Goater fl, name, fl->size); 785924ed163SCédric Le Goater memory_region_add_subregion(&s->mmio_flash, offset, &fl->mmio); 786924ed163SCédric Le Goater offset += fl->size; 787924ed163SCédric Le Goater } 7887c1c69bcSCédric Le Goater } 7897c1c69bcSCédric Le Goater 7907c1c69bcSCédric Le Goater static const VMStateDescription vmstate_aspeed_smc = { 7917c1c69bcSCédric Le Goater .name = "aspeed.smc", 7927c1c69bcSCédric Le Goater .version_id = 1, 7937c1c69bcSCédric Le Goater .minimum_version_id = 1, 7947c1c69bcSCédric Le Goater .fields = (VMStateField[]) { 7957c1c69bcSCédric Le Goater VMSTATE_UINT32_ARRAY(regs, AspeedSMCState, ASPEED_SMC_R_MAX), 7967c1c69bcSCédric Le Goater VMSTATE_END_OF_LIST() 7977c1c69bcSCédric Le Goater } 7987c1c69bcSCédric Le Goater }; 7997c1c69bcSCédric Le Goater 8007c1c69bcSCédric Le Goater static Property aspeed_smc_properties[] = { 8017c1c69bcSCédric Le Goater DEFINE_PROP_UINT32("num-cs", AspeedSMCState, num_cs, 1), 8027c1c69bcSCédric Le Goater DEFINE_PROP_END_OF_LIST(), 8037c1c69bcSCédric Le Goater }; 8047c1c69bcSCédric Le Goater 8057c1c69bcSCédric Le Goater static void aspeed_smc_class_init(ObjectClass *klass, void *data) 8067c1c69bcSCédric Le Goater { 8077c1c69bcSCédric Le Goater DeviceClass *dc = DEVICE_CLASS(klass); 8087c1c69bcSCédric Le Goater AspeedSMCClass *mc = ASPEED_SMC_CLASS(klass); 8097c1c69bcSCédric Le Goater 8107c1c69bcSCédric Le Goater dc->realize = aspeed_smc_realize; 8117c1c69bcSCédric Le Goater dc->reset = aspeed_smc_reset; 8127c1c69bcSCédric Le Goater dc->props = aspeed_smc_properties; 8137c1c69bcSCédric Le Goater dc->vmsd = &vmstate_aspeed_smc; 8147c1c69bcSCédric Le Goater mc->ctrl = data; 8157c1c69bcSCédric Le Goater } 8167c1c69bcSCédric Le Goater 8177c1c69bcSCédric Le Goater static const TypeInfo aspeed_smc_info = { 8187c1c69bcSCédric Le Goater .name = TYPE_ASPEED_SMC, 8197c1c69bcSCédric Le Goater .parent = TYPE_SYS_BUS_DEVICE, 8207c1c69bcSCédric Le Goater .instance_size = sizeof(AspeedSMCState), 8217c1c69bcSCédric Le Goater .class_size = sizeof(AspeedSMCClass), 8227c1c69bcSCédric Le Goater .abstract = true, 8237c1c69bcSCédric Le Goater }; 8247c1c69bcSCédric Le Goater 8257c1c69bcSCédric Le Goater static void aspeed_smc_register_types(void) 8267c1c69bcSCédric Le Goater { 8277c1c69bcSCédric Le Goater int i; 8287c1c69bcSCédric Le Goater 8297c1c69bcSCédric Le Goater type_register_static(&aspeed_smc_info); 8307c1c69bcSCédric Le Goater for (i = 0; i < ARRAY_SIZE(controllers); ++i) { 8317c1c69bcSCédric Le Goater TypeInfo ti = { 8327c1c69bcSCédric Le Goater .name = controllers[i].name, 8337c1c69bcSCédric Le Goater .parent = TYPE_ASPEED_SMC, 8347c1c69bcSCédric Le Goater .class_init = aspeed_smc_class_init, 8357c1c69bcSCédric Le Goater .class_data = (void *)&controllers[i], 8367c1c69bcSCédric Le Goater }; 8377c1c69bcSCédric Le Goater type_register(&ti); 8387c1c69bcSCédric Le Goater } 8397c1c69bcSCédric Le Goater } 8407c1c69bcSCédric Le Goater 8417c1c69bcSCédric Le Goater type_init(aspeed_smc_register_types) 842