xref: /qemu/hw/ssi/aspeed_smc.c (revision e2804a1ec97ceede14b69a2a6e9a8b5dfa0b15c2)
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"
27d6454270SMarkus Armbruster #include "migration/vmstate.h"
287c1c69bcSCédric Le Goater #include "qemu/log.h"
290b8fa32fSMarkus Armbruster #include "qemu/module.h"
30d6e3f50aSPhilippe Mathieu-Daudé #include "qemu/error-report.h"
31c4e1f0b4SCédric Le Goater #include "qapi/error.h"
32bcaa8dddSCédric Le Goater #include "qemu/units.h"
33bd6ce9a6SCédric Le Goater #include "trace.h"
347c1c69bcSCédric Le Goater 
3564552b6bSMarkus Armbruster #include "hw/irq.h"
36a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
377c1c69bcSCédric Le Goater #include "hw/ssi/aspeed_smc.h"
387c1c69bcSCédric Le Goater 
397c1c69bcSCédric Le Goater /* CE Type Setting Register */
407c1c69bcSCédric Le Goater #define R_CONF            (0x00 / 4)
417c1c69bcSCédric Le Goater #define   CONF_LEGACY_DISABLE  (1 << 31)
427c1c69bcSCédric Le Goater #define   CONF_ENABLE_W4       20
437c1c69bcSCédric Le Goater #define   CONF_ENABLE_W3       19
447c1c69bcSCédric Le Goater #define   CONF_ENABLE_W2       18
457c1c69bcSCédric Le Goater #define   CONF_ENABLE_W1       17
467c1c69bcSCédric Le Goater #define   CONF_ENABLE_W0       16
470707b34dSCédric Le Goater #define   CONF_FLASH_TYPE4     8
480707b34dSCédric Le Goater #define   CONF_FLASH_TYPE3     6
490707b34dSCédric Le Goater #define   CONF_FLASH_TYPE2     4
500707b34dSCédric Le Goater #define   CONF_FLASH_TYPE1     2
510707b34dSCédric Le Goater #define   CONF_FLASH_TYPE0     0
520707b34dSCédric Le Goater #define      CONF_FLASH_TYPE_NOR   0x0
530707b34dSCédric Le Goater #define      CONF_FLASH_TYPE_NAND  0x1
54bcaa8dddSCédric Le Goater #define      CONF_FLASH_TYPE_SPI   0x2 /* AST2600 is SPI only */
557c1c69bcSCédric Le Goater 
567c1c69bcSCédric Le Goater /* CE Control Register */
577c1c69bcSCédric Le Goater #define R_CE_CTRL            (0x04 / 4)
587c1c69bcSCédric Le Goater #define   CTRL_EXTENDED4       4  /* 32 bit addressing for SPI */
597c1c69bcSCédric Le Goater #define   CTRL_EXTENDED3       3  /* 32 bit addressing for SPI */
607c1c69bcSCédric Le Goater #define   CTRL_EXTENDED2       2  /* 32 bit addressing for SPI */
617c1c69bcSCédric Le Goater #define   CTRL_EXTENDED1       1  /* 32 bit addressing for SPI */
627c1c69bcSCédric Le Goater #define   CTRL_EXTENDED0       0  /* 32 bit addressing for SPI */
637c1c69bcSCédric Le Goater 
647c1c69bcSCédric Le Goater /* Interrupt Control and Status Register */
657c1c69bcSCédric Le Goater #define R_INTR_CTRL       (0x08 / 4)
667c1c69bcSCédric Le Goater #define   INTR_CTRL_DMA_STATUS            (1 << 11)
677c1c69bcSCédric Le Goater #define   INTR_CTRL_CMD_ABORT_STATUS      (1 << 10)
687c1c69bcSCédric Le Goater #define   INTR_CTRL_WRITE_PROTECT_STATUS  (1 << 9)
697c1c69bcSCédric Le Goater #define   INTR_CTRL_DMA_EN                (1 << 3)
707c1c69bcSCédric Le Goater #define   INTR_CTRL_CMD_ABORT_EN          (1 << 2)
717c1c69bcSCédric Le Goater #define   INTR_CTRL_WRITE_PROTECT_EN      (1 << 1)
727c1c69bcSCédric Le Goater 
73af453a5eSCédric Le Goater /* Command Control Register */
74af453a5eSCédric Le Goater #define R_CE_CMD_CTRL      (0x0C / 4)
75af453a5eSCédric Le Goater #define   CTRL_ADDR_BYTE0_DISABLE_SHIFT       4
76af453a5eSCédric Le Goater #define   CTRL_DATA_BYTE0_DISABLE_SHIFT       0
77af453a5eSCédric Le Goater 
78af453a5eSCédric Le Goater #define aspeed_smc_addr_byte_enabled(s, i)                               \
79af453a5eSCédric Le Goater     (!((s)->regs[R_CE_CMD_CTRL] & (1 << (CTRL_ADDR_BYTE0_DISABLE_SHIFT + (i)))))
80af453a5eSCédric Le Goater #define aspeed_smc_data_byte_enabled(s, i)                               \
81af453a5eSCédric Le Goater     (!((s)->regs[R_CE_CMD_CTRL] & (1 << (CTRL_DATA_BYTE0_DISABLE_SHIFT + (i)))))
82af453a5eSCédric Le Goater 
837c1c69bcSCédric Le Goater /* CEx Control Register */
847c1c69bcSCédric Le Goater #define R_CTRL0           (0x10 / 4)
85bcaa8dddSCédric Le Goater #define   CTRL_IO_QPI              (1 << 31)
86bcaa8dddSCédric Le Goater #define   CTRL_IO_QUAD_DATA        (1 << 30)
870721309eSCédric Le Goater #define   CTRL_IO_DUAL_DATA        (1 << 29)
880721309eSCédric Le Goater #define   CTRL_IO_DUAL_ADDR_DATA   (1 << 28) /* Includes dummies */
89bcaa8dddSCédric Le Goater #define   CTRL_IO_QUAD_ADDR_DATA   (1 << 28) /* Includes dummies */
907c1c69bcSCédric Le Goater #define   CTRL_CMD_SHIFT           16
917c1c69bcSCédric Le Goater #define   CTRL_CMD_MASK            0xff
92ac2810deSCédric Le Goater #define   CTRL_DUMMY_HIGH_SHIFT    14
93fcdf2c59SCédric Le Goater #define   CTRL_AST2400_SPI_4BYTE   (1 << 13)
940d72c717SCédric Le Goater #define CE_CTRL_CLOCK_FREQ_SHIFT   8
950d72c717SCédric Le Goater #define CE_CTRL_CLOCK_FREQ_MASK    0xf
960d72c717SCédric Le Goater #define CE_CTRL_CLOCK_FREQ(div)                                         \
970d72c717SCédric Le Goater     (((div) & CE_CTRL_CLOCK_FREQ_MASK) << CE_CTRL_CLOCK_FREQ_SHIFT)
98ac2810deSCédric Le Goater #define   CTRL_DUMMY_LOW_SHIFT     6 /* 2 bits [7:6] */
997c1c69bcSCédric Le Goater #define   CTRL_CE_STOP_ACTIVE      (1 << 2)
1007c1c69bcSCédric Le Goater #define   CTRL_CMD_MODE_MASK       0x3
1017c1c69bcSCédric Le Goater #define     CTRL_READMODE          0x0
1027c1c69bcSCédric Le Goater #define     CTRL_FREADMODE         0x1
1037c1c69bcSCédric Le Goater #define     CTRL_WRITEMODE         0x2
1047c1c69bcSCédric Le Goater #define     CTRL_USERMODE          0x3
1057c1c69bcSCédric Le Goater #define R_CTRL1           (0x14 / 4)
1067c1c69bcSCédric Le Goater #define R_CTRL2           (0x18 / 4)
1077c1c69bcSCédric Le Goater #define R_CTRL3           (0x1C / 4)
1087c1c69bcSCédric Le Goater #define R_CTRL4           (0x20 / 4)
1097c1c69bcSCédric Le Goater 
1107c1c69bcSCédric Le Goater /* CEx Segment Address Register */
1117c1c69bcSCédric Le Goater #define R_SEG_ADDR0       (0x30 / 4)
112a03cb1daSCédric Le Goater #define   SEG_END_SHIFT        24   /* 8MB units */
113a03cb1daSCédric Le Goater #define   SEG_END_MASK         0xff
1147c1c69bcSCédric Le Goater #define   SEG_START_SHIFT      16   /* address bit [A29-A23] */
115a03cb1daSCédric Le Goater #define   SEG_START_MASK       0xff
1167c1c69bcSCédric Le Goater #define R_SEG_ADDR1       (0x34 / 4)
1177c1c69bcSCédric Le Goater #define R_SEG_ADDR2       (0x38 / 4)
1187c1c69bcSCédric Le Goater #define R_SEG_ADDR3       (0x3C / 4)
1197c1c69bcSCédric Le Goater #define R_SEG_ADDR4       (0x40 / 4)
1207c1c69bcSCédric Le Goater 
1217c1c69bcSCédric Le Goater /* Misc Control Register #1 */
1227c1c69bcSCédric Le Goater #define R_MISC_CTRL1      (0x50 / 4)
1237c1c69bcSCédric Le Goater 
1249149af2aSCédric Le Goater /* SPI dummy cycle data */
1259149af2aSCédric Le Goater #define R_DUMMY_DATA      (0x54 / 4)
1267c1c69bcSCédric Le Goater 
12745a904afSCédric Le Goater /* FMC_WDT2 Control/Status Register for Alternate Boot (AST2600) */
12845a904afSCédric Le Goater #define R_FMC_WDT2_CTRL   (0x64 / 4)
12945a904afSCédric Le Goater #define   FMC_WDT2_CTRL_ALT_BOOT_MODE    BIT(6) /* O: 2 chips 1: 1 chip */
13045a904afSCédric Le Goater #define   FMC_WDT2_CTRL_SINGLE_BOOT_MODE BIT(5)
13145a904afSCédric Le Goater #define   FMC_WDT2_CTRL_BOOT_SOURCE      BIT(4) /* O: primary 1: alternate */
13245a904afSCédric Le Goater #define   FMC_WDT2_CTRL_EN               BIT(0)
13345a904afSCédric Le Goater 
1347c1c69bcSCédric Le Goater /* DMA Control/Status Register */
1357c1c69bcSCédric Le Goater #define R_DMA_CTRL        (0x80 / 4)
1361769a70eSCédric Le Goater #define   DMA_CTRL_REQUEST      (1 << 31)
1371769a70eSCédric Le Goater #define   DMA_CTRL_GRANT        (1 << 30)
1387c1c69bcSCédric Le Goater #define   DMA_CTRL_DELAY_MASK   0xf
1397c1c69bcSCédric Le Goater #define   DMA_CTRL_DELAY_SHIFT  8
1407c1c69bcSCédric Le Goater #define   DMA_CTRL_FREQ_MASK    0xf
1417c1c69bcSCédric Le Goater #define   DMA_CTRL_FREQ_SHIFT   4
1420d72c717SCédric Le Goater #define   DMA_CTRL_CALIB        (1 << 3)
1437c1c69bcSCédric Le Goater #define   DMA_CTRL_CKSUM        (1 << 2)
144c4e1f0b4SCédric Le Goater #define   DMA_CTRL_WRITE        (1 << 1)
145c4e1f0b4SCédric Le Goater #define   DMA_CTRL_ENABLE       (1 << 0)
1467c1c69bcSCédric Le Goater 
1477c1c69bcSCédric Le Goater /* DMA Flash Side Address */
1487c1c69bcSCédric Le Goater #define R_DMA_FLASH_ADDR  (0x84 / 4)
1497c1c69bcSCédric Le Goater 
1507c1c69bcSCédric Le Goater /* DMA DRAM Side Address */
1517c1c69bcSCédric Le Goater #define R_DMA_DRAM_ADDR   (0x88 / 4)
1527c1c69bcSCédric Le Goater 
1537c1c69bcSCédric Le Goater /* DMA Length Register */
1547c1c69bcSCédric Le Goater #define R_DMA_LEN         (0x8C / 4)
1557c1c69bcSCédric Le Goater 
1567c1c69bcSCédric Le Goater /* Checksum Calculation Result */
1577c1c69bcSCédric Le Goater #define R_DMA_CHECKSUM    (0x90 / 4)
1587c1c69bcSCédric Le Goater 
159f286f04cSCédric Le Goater /* Read Timing Compensation Register */
1607c1c69bcSCédric Le Goater #define R_TIMINGS         (0x94 / 4)
1617c1c69bcSCédric Le Goater 
162bcaa8dddSCédric Le Goater /* SPI controller registers and bits (AST2400) */
1637c1c69bcSCédric Le Goater #define R_SPI_CONF        (0x00 / 4)
1647c1c69bcSCédric Le Goater #define   SPI_CONF_ENABLE_W0   0
1657c1c69bcSCédric Le Goater #define R_SPI_CTRL0       (0x4 / 4)
1667c1c69bcSCédric Le Goater #define R_SPI_MISC_CTRL   (0x10 / 4)
1677c1c69bcSCédric Le Goater #define R_SPI_TIMINGS     (0x14 / 4)
1687c1c69bcSCédric Le Goater 
169087b57c9SCédric Le Goater #define ASPEED_SMC_R_SPI_MAX (0x20 / 4)
170087b57c9SCédric Le Goater #define ASPEED_SMC_R_SMC_MAX (0x20 / 4)
171087b57c9SCédric Le Goater 
172c4e1f0b4SCédric Le Goater /*
173c4e1f0b4SCédric Le Goater  * DMA DRAM addresses should be 4 bytes aligned and the valid address
174c4e1f0b4SCédric Le Goater  * range is 0x40000000 - 0x5FFFFFFF (AST2400)
175c4e1f0b4SCédric Le Goater  *          0x80000000 - 0xBFFFFFFF (AST2500)
176c4e1f0b4SCédric Le Goater  *
177c4e1f0b4SCédric Le Goater  * DMA flash addresses should be 4 bytes aligned and the valid address
178c4e1f0b4SCédric Le Goater  * range is 0x20000000 - 0x2FFFFFFF.
179c4e1f0b4SCédric Le Goater  *
180c4e1f0b4SCédric Le Goater  * DMA length is from 4 bytes to 32MB
181c4e1f0b4SCédric Le Goater  *   0: 4 bytes
182c4e1f0b4SCédric Le Goater  *   0x7FFFFF: 32M bytes
183c4e1f0b4SCédric Le Goater  */
18430b6852cSCédric Le Goater #define DMA_DRAM_ADDR(asc, val)   ((val) & (asc)->dma_dram_mask)
18530b6852cSCédric Le Goater #define DMA_FLASH_ADDR(asc, val)  ((val) & (asc)->dma_flash_mask)
186c4e1f0b4SCédric Le Goater #define DMA_LENGTH(val)         ((val) & 0x01FFFFFC)
187c4e1f0b4SCédric Le Goater 
188fcdf2c59SCédric Le Goater /* Flash opcodes. */
189fcdf2c59SCédric Le Goater #define SPI_OP_READ       0x03    /* Read data bytes (low frequency) */
190fcdf2c59SCédric Le Goater 
191f95c4bffSCédric Le Goater #define SNOOP_OFF         0xFF
192f95c4bffSCédric Le Goater #define SNOOP_START       0x0
193f95c4bffSCédric Le Goater 
194924ed163SCédric Le Goater /*
1955ade579bSPhilippe Mathieu-Daudé  * Default segments mapping addresses and size for each peripheral per
196924ed163SCédric Le Goater  * controller. These can be changed when board is initialized with the
197a03cb1daSCédric Le Goater  * Segment Address Registers.
198924ed163SCédric Le Goater  */
19930b6852cSCédric Le Goater static const AspeedSegments aspeed_2500_spi1_segments[];
20030b6852cSCédric Le Goater static const AspeedSegments aspeed_2500_spi2_segments[];
2011769a70eSCédric Le Goater 
2021c5ee69dSCédric Le Goater #define ASPEED_SMC_FEATURE_DMA       0x1
2031769a70eSCédric Le Goater #define ASPEED_SMC_FEATURE_DMA_GRANT 0x2
20445a904afSCédric Le Goater #define ASPEED_SMC_FEATURE_WDT_CONTROL 0x4
2051c5ee69dSCédric Le Goater 
20630b6852cSCédric Le Goater static inline bool aspeed_smc_has_dma(const AspeedSMCClass *asc)
2071c5ee69dSCédric Le Goater {
20830b6852cSCédric Le Goater     return !!(asc->features & ASPEED_SMC_FEATURE_DMA);
2091c5ee69dSCédric Le Goater }
210bcaa8dddSCédric Le Goater 
21130b6852cSCédric Le Goater static inline bool aspeed_smc_has_wdt_control(const AspeedSMCClass *asc)
21245a904afSCédric Le Goater {
21330b6852cSCédric Le Goater     return !!(asc->features & ASPEED_SMC_FEATURE_WDT_CONTROL);
214bcaa8dddSCédric Le Goater }
215bcaa8dddSCédric Le Goater 
21632c54bd0SCédric Le Goater #define aspeed_smc_error(fmt, ...)                                      \
21732c54bd0SCédric Le Goater     qemu_log_mask(LOG_GUEST_ERROR, "%s: " fmt "\n", __func__, ## __VA_ARGS__)
21832c54bd0SCédric Le Goater 
219a03cb1daSCédric Le Goater static bool aspeed_smc_flash_overlap(const AspeedSMCState *s,
220a03cb1daSCédric Le Goater                                      const AspeedSegments *new,
221a03cb1daSCédric Le Goater                                      int cs)
222a03cb1daSCédric Le Goater {
22330b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_GET_CLASS(s);
224a03cb1daSCédric Le Goater     AspeedSegments seg;
225a03cb1daSCédric Le Goater     int i;
226a03cb1daSCédric Le Goater 
22730b6852cSCédric Le Goater     for (i = 0; i < asc->max_peripherals; i++) {
228a03cb1daSCédric Le Goater         if (i == cs) {
229a03cb1daSCédric Le Goater             continue;
230a03cb1daSCédric Le Goater         }
231a03cb1daSCédric Le Goater 
23230b6852cSCédric Le Goater         asc->reg_to_segment(s, s->regs[R_SEG_ADDR0 + i], &seg);
233a03cb1daSCédric Le Goater 
234a03cb1daSCédric Le Goater         if (new->addr + new->size > seg.addr &&
235a03cb1daSCédric Le Goater             new->addr < seg.addr + seg.size) {
23632c54bd0SCédric Le Goater             aspeed_smc_error("new segment CS%d [ 0x%"
237a03cb1daSCédric Le Goater                              HWADDR_PRIx" - 0x%"HWADDR_PRIx" ] overlaps with "
23832c54bd0SCédric Le Goater                              "CS%d [ 0x%"HWADDR_PRIx" - 0x%"HWADDR_PRIx" ]",
23932c54bd0SCédric Le Goater                              cs, new->addr, new->addr + new->size,
240a03cb1daSCédric Le Goater                              i, seg.addr, seg.addr + seg.size);
241a03cb1daSCédric Le Goater             return true;
242a03cb1daSCédric Le Goater         }
243a03cb1daSCédric Le Goater     }
244a03cb1daSCédric Le Goater     return false;
245a03cb1daSCédric Le Goater }
246a03cb1daSCédric Le Goater 
247673b1f86SCédric Le Goater static void aspeed_smc_flash_set_segment_region(AspeedSMCState *s, int cs,
248673b1f86SCédric Le Goater                                                 uint64_t regval)
249673b1f86SCédric Le Goater {
25030b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_GET_CLASS(s);
251673b1f86SCédric Le Goater     AspeedSMCFlash *fl = &s->flashes[cs];
252673b1f86SCédric Le Goater     AspeedSegments seg;
253673b1f86SCédric Le Goater 
25430b6852cSCédric Le Goater     asc->reg_to_segment(s, regval, &seg);
255673b1f86SCédric Le Goater 
256673b1f86SCédric Le Goater     memory_region_transaction_begin();
257673b1f86SCédric Le Goater     memory_region_set_size(&fl->mmio, seg.size);
25830b6852cSCédric Le Goater     memory_region_set_address(&fl->mmio, seg.addr - asc->flash_window_base);
2592175eacfSCédric Le Goater     memory_region_set_enabled(&fl->mmio, !!seg.size);
260673b1f86SCédric Le Goater     memory_region_transaction_commit();
261673b1f86SCédric Le Goater 
262673b1f86SCédric Le Goater     s->regs[R_SEG_ADDR0 + cs] = regval;
263673b1f86SCédric Le Goater }
264673b1f86SCédric Le Goater 
265a03cb1daSCédric Le Goater static void aspeed_smc_flash_set_segment(AspeedSMCState *s, int cs,
266a03cb1daSCédric Le Goater                                          uint64_t new)
267a03cb1daSCédric Le Goater {
26830b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_GET_CLASS(s);
269a03cb1daSCédric Le Goater     AspeedSegments seg;
270a03cb1daSCédric Le Goater 
27130b6852cSCédric Le Goater     asc->reg_to_segment(s, new, &seg);
272a03cb1daSCédric Le Goater 
273bd6ce9a6SCédric Le Goater     trace_aspeed_smc_flash_set_segment(cs, new, seg.addr, seg.addr + seg.size);
274bd6ce9a6SCédric Le Goater 
275a03cb1daSCédric Le Goater     /* The start address of CS0 is read-only */
27630b6852cSCédric Le Goater     if (cs == 0 && seg.addr != asc->flash_window_base) {
27732c54bd0SCédric Le Goater         aspeed_smc_error("Tried to change CS0 start address to 0x%"
27832c54bd0SCédric Le Goater                          HWADDR_PRIx, seg.addr);
27930b6852cSCédric Le Goater         seg.addr = asc->flash_window_base;
28030b6852cSCédric Le Goater         new = asc->segment_to_reg(s, &seg);
281a03cb1daSCédric Le Goater     }
282a03cb1daSCédric Le Goater 
283a03cb1daSCédric Le Goater     /*
284a03cb1daSCédric Le Goater      * The end address of the AST2500 spi controllers is also
285a03cb1daSCédric Le Goater      * read-only.
286a03cb1daSCédric Le Goater      */
28730b6852cSCédric Le Goater     if ((asc->segments == aspeed_2500_spi1_segments ||
28830b6852cSCédric Le Goater          asc->segments == aspeed_2500_spi2_segments) &&
28930b6852cSCédric Le Goater         cs == asc->max_peripherals &&
29030b6852cSCédric Le Goater         seg.addr + seg.size != asc->segments[cs].addr +
29130b6852cSCédric Le Goater         asc->segments[cs].size) {
29232c54bd0SCédric Le Goater         aspeed_smc_error("Tried to change CS%d end address to 0x%"
29332c54bd0SCédric Le Goater                          HWADDR_PRIx, cs, seg.addr + seg.size);
29430b6852cSCédric Le Goater         seg.size = asc->segments[cs].addr + asc->segments[cs].size -
2950584d3c3SCédric Le Goater             seg.addr;
29630b6852cSCédric Le Goater         new = asc->segment_to_reg(s, &seg);
297a03cb1daSCédric Le Goater     }
298a03cb1daSCédric Le Goater 
299a03cb1daSCédric Le Goater     /* Keep the segment in the overall flash window */
3002175eacfSCédric Le Goater     if (seg.size &&
30130b6852cSCédric Le Goater         (seg.addr + seg.size <= asc->flash_window_base ||
30230b6852cSCédric Le Goater          seg.addr > asc->flash_window_base + asc->flash_window_size)) {
30332c54bd0SCédric Le Goater         aspeed_smc_error("new segment for CS%d is invalid : "
30432c54bd0SCédric Le Goater                          "[ 0x%"HWADDR_PRIx" - 0x%"HWADDR_PRIx" ]",
30532c54bd0SCédric Le Goater                          cs, seg.addr, seg.addr + seg.size);
306a03cb1daSCédric Le Goater         return;
307a03cb1daSCédric Le Goater     }
308a03cb1daSCédric Le Goater 
309a03cb1daSCédric Le Goater     /* Check start address vs. alignment */
3100584d3c3SCédric Le Goater     if (seg.size && !QEMU_IS_ALIGNED(seg.addr, seg.size)) {
31132c54bd0SCédric Le Goater         aspeed_smc_error("new segment for CS%d is not "
31232c54bd0SCédric Le Goater                          "aligned : [ 0x%"HWADDR_PRIx" - 0x%"HWADDR_PRIx" ]",
31332c54bd0SCédric Le Goater                          cs, seg.addr, seg.addr + seg.size);
314a03cb1daSCédric Le Goater     }
315a03cb1daSCédric Le Goater 
3160584d3c3SCédric Le Goater     /* And segments should not overlap (in the specs) */
3170584d3c3SCédric Le Goater     aspeed_smc_flash_overlap(s, &seg, cs);
318a03cb1daSCédric Le Goater 
319a03cb1daSCédric Le Goater     /* All should be fine now to move the region */
320673b1f86SCédric Le Goater     aspeed_smc_flash_set_segment_region(s, cs, new);
321a03cb1daSCédric Le Goater }
322a03cb1daSCédric Le Goater 
323924ed163SCédric Le Goater static uint64_t aspeed_smc_flash_default_read(void *opaque, hwaddr addr,
324924ed163SCédric Le Goater                                               unsigned size)
325924ed163SCédric Le Goater {
32632c54bd0SCédric Le Goater     aspeed_smc_error("To 0x%" HWADDR_PRIx " of size %u" PRIx64, addr, size);
327924ed163SCédric Le Goater     return 0;
328924ed163SCédric Le Goater }
329924ed163SCédric Le Goater 
330924ed163SCédric Le Goater static void aspeed_smc_flash_default_write(void *opaque, hwaddr addr,
331924ed163SCédric Le Goater                                            uint64_t data, unsigned size)
332924ed163SCédric Le Goater {
33332c54bd0SCédric Le Goater     aspeed_smc_error("To 0x%" HWADDR_PRIx " of size %u: 0x%" PRIx64,
33432c54bd0SCédric Le Goater                      addr, size, data);
335924ed163SCédric Le Goater }
336924ed163SCédric Le Goater 
337924ed163SCédric Le Goater static const MemoryRegionOps aspeed_smc_flash_default_ops = {
338924ed163SCédric Le Goater     .read = aspeed_smc_flash_default_read,
339924ed163SCédric Le Goater     .write = aspeed_smc_flash_default_write,
340924ed163SCédric Le Goater     .endianness = DEVICE_LITTLE_ENDIAN,
341924ed163SCédric Le Goater     .valid = {
342924ed163SCédric Le Goater         .min_access_size = 1,
343924ed163SCédric Le Goater         .max_access_size = 4,
344924ed163SCédric Le Goater     },
345924ed163SCédric Le Goater };
346924ed163SCédric Le Goater 
347f248a9dbSCédric Le Goater static inline int aspeed_smc_flash_mode(const AspeedSMCFlash *fl)
348924ed163SCédric Le Goater {
349f248a9dbSCédric Le Goater     const AspeedSMCState *s = fl->controller;
350f248a9dbSCédric Le Goater 
35110f915e4SCédric Le Goater     return s->regs[s->r_ctrl0 + fl->cs] & CTRL_CMD_MODE_MASK;
352924ed163SCédric Le Goater }
353924ed163SCédric Le Goater 
354f248a9dbSCédric Le Goater static inline bool aspeed_smc_is_writable(const AspeedSMCFlash *fl)
355924ed163SCédric Le Goater {
356f248a9dbSCédric Le Goater     const AspeedSMCState *s = fl->controller;
357f248a9dbSCédric Le Goater 
35810f915e4SCédric Le Goater     return s->regs[s->r_conf] & (1 << (s->conf_enable_w0 + fl->cs));
359924ed163SCédric Le Goater }
360924ed163SCédric Le Goater 
361fcdf2c59SCédric Le Goater static inline int aspeed_smc_flash_cmd(const AspeedSMCFlash *fl)
362fcdf2c59SCédric Le Goater {
363fcdf2c59SCédric Le Goater     const AspeedSMCState *s = fl->controller;
36410f915e4SCédric Le Goater     int cmd = (s->regs[s->r_ctrl0 + fl->cs] >> CTRL_CMD_SHIFT) & CTRL_CMD_MASK;
365fcdf2c59SCédric Le Goater 
366bcaa8dddSCédric Le Goater     /*
367bcaa8dddSCédric Le Goater      * In read mode, the default SPI command is READ (0x3). In other
368bcaa8dddSCédric Le Goater      * modes, the command should necessarily be defined
369bcaa8dddSCédric Le Goater      *
370bcaa8dddSCédric Le Goater      * TODO: add support for READ4 (0x13) on AST2600
371bcaa8dddSCédric Le Goater      */
372fcdf2c59SCédric Le Goater     if (aspeed_smc_flash_mode(fl) == CTRL_READMODE) {
373fcdf2c59SCédric Le Goater         cmd = SPI_OP_READ;
374fcdf2c59SCédric Le Goater     }
375fcdf2c59SCédric Le Goater 
376fcdf2c59SCédric Le Goater     if (!cmd) {
37732c54bd0SCédric Le Goater         aspeed_smc_error("no command defined for mode %d",
37832c54bd0SCédric Le Goater                          aspeed_smc_flash_mode(fl));
379fcdf2c59SCédric Le Goater     }
380fcdf2c59SCédric Le Goater 
381fcdf2c59SCédric Le Goater     return cmd;
382fcdf2c59SCédric Le Goater }
383fcdf2c59SCédric Le Goater 
384a779e37cSCédric Le Goater static inline int aspeed_smc_flash_addr_width(const AspeedSMCFlash *fl)
385fcdf2c59SCédric Le Goater {
386fcdf2c59SCédric Le Goater     const AspeedSMCState *s = fl->controller;
38730b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_GET_CLASS(s);
388fcdf2c59SCédric Le Goater 
389a779e37cSCédric Le Goater     if (asc->addr_width) {
390a779e37cSCédric Le Goater         return asc->addr_width(s);
391fcdf2c59SCédric Le Goater     } else {
392a779e37cSCédric Le Goater         return s->regs[s->r_ce_ctrl] & (1 << (CTRL_EXTENDED0 + fl->cs)) ? 4 : 3;
393fcdf2c59SCédric Le Goater     }
394fcdf2c59SCédric Le Goater }
395fcdf2c59SCédric Le Goater 
396e7e741caSCédric Le Goater static void aspeed_smc_flash_do_select(AspeedSMCFlash *fl, bool unselect)
397fcdf2c59SCédric Le Goater {
398e7e741caSCédric Le Goater     AspeedSMCState *s = fl->controller;
399fcdf2c59SCédric Le Goater 
40010f915e4SCédric Le Goater     trace_aspeed_smc_flash_select(fl->cs, unselect ? "un" : "");
401e7e741caSCédric Le Goater 
40210f915e4SCédric Le Goater     qemu_set_irq(s->cs_lines[fl->cs], unselect);
403fcdf2c59SCédric Le Goater }
404fcdf2c59SCédric Le Goater 
405fcdf2c59SCédric Le Goater static void aspeed_smc_flash_select(AspeedSMCFlash *fl)
406fcdf2c59SCédric Le Goater {
407e7e741caSCédric Le Goater     aspeed_smc_flash_do_select(fl, false);
408fcdf2c59SCédric Le Goater }
409fcdf2c59SCédric Le Goater 
410fcdf2c59SCédric Le Goater static void aspeed_smc_flash_unselect(AspeedSMCFlash *fl)
411fcdf2c59SCédric Le Goater {
412e7e741caSCédric Le Goater     aspeed_smc_flash_do_select(fl, true);
413fcdf2c59SCédric Le Goater }
414fcdf2c59SCédric Le Goater 
415fcdf2c59SCédric Le Goater static uint32_t aspeed_smc_check_segment_addr(const AspeedSMCFlash *fl,
416fcdf2c59SCédric Le Goater                                               uint32_t addr)
417fcdf2c59SCédric Le Goater {
418fcdf2c59SCédric Le Goater     const AspeedSMCState *s = fl->controller;
41930b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_GET_CLASS(s);
420fcdf2c59SCédric Le Goater     AspeedSegments seg;
421fcdf2c59SCédric Le Goater 
42210f915e4SCédric Le Goater     asc->reg_to_segment(s, s->regs[R_SEG_ADDR0 + fl->cs], &seg);
423b4cc583fSCédric Le Goater     if ((addr % seg.size) != addr) {
42432c54bd0SCédric Le Goater         aspeed_smc_error("invalid address 0x%08x for CS%d segment : "
42532c54bd0SCédric Le Goater                          "[ 0x%"HWADDR_PRIx" - 0x%"HWADDR_PRIx" ]",
42610f915e4SCédric Le Goater                          addr, fl->cs, seg.addr, seg.addr + seg.size);
427b4cc583fSCédric Le Goater         addr %= seg.size;
428fcdf2c59SCédric Le Goater     }
429fcdf2c59SCédric Le Goater 
430fcdf2c59SCédric Le Goater     return addr;
431fcdf2c59SCédric Le Goater }
432fcdf2c59SCédric Le Goater 
433ac2810deSCédric Le Goater static int aspeed_smc_flash_dummies(const AspeedSMCFlash *fl)
434ac2810deSCédric Le Goater {
435ac2810deSCédric Le Goater     const AspeedSMCState *s = fl->controller;
43610f915e4SCédric Le Goater     uint32_t r_ctrl0 = s->regs[s->r_ctrl0 + fl->cs];
437ac2810deSCédric Le Goater     uint32_t dummy_high = (r_ctrl0 >> CTRL_DUMMY_HIGH_SHIFT) & 0x1;
438ac2810deSCédric Le Goater     uint32_t dummy_low = (r_ctrl0 >> CTRL_DUMMY_LOW_SHIFT) & 0x3;
4390721309eSCédric Le Goater     uint32_t dummies = ((dummy_high << 2) | dummy_low) * 8;
440ac2810deSCédric Le Goater 
4410721309eSCédric Le Goater     if (r_ctrl0 & CTRL_IO_DUAL_ADDR_DATA) {
4420721309eSCédric Le Goater         dummies /= 2;
4430721309eSCédric Le Goater     }
4440721309eSCédric Le Goater 
4450721309eSCédric Le Goater     return dummies;
446ac2810deSCédric Le Goater }
447ac2810deSCédric Le Goater 
44896c4be95SCédric Le Goater static void aspeed_smc_flash_setup(AspeedSMCFlash *fl, uint32_t addr)
449fcdf2c59SCédric Le Goater {
450fcdf2c59SCédric Le Goater     const AspeedSMCState *s = fl->controller;
451fcdf2c59SCédric Le Goater     uint8_t cmd = aspeed_smc_flash_cmd(fl);
452a779e37cSCédric Le Goater     int i = aspeed_smc_flash_addr_width(fl);
453fcdf2c59SCédric Le Goater 
454fcdf2c59SCédric Le Goater     /* Flash access can not exceed CS segment */
455fcdf2c59SCédric Le Goater     addr = aspeed_smc_check_segment_addr(fl, addr);
456fcdf2c59SCédric Le Goater 
457fcdf2c59SCédric Le Goater     ssi_transfer(s->spi, cmd);
458af453a5eSCédric Le Goater     while (i--) {
459af453a5eSCédric Le Goater         if (aspeed_smc_addr_byte_enabled(s, i)) {
460af453a5eSCédric Le Goater             ssi_transfer(s->spi, (addr >> (i * 8)) & 0xff);
461fcdf2c59SCédric Le Goater         }
462af453a5eSCédric Le Goater     }
46396c4be95SCédric Le Goater 
46496c4be95SCédric Le Goater     /*
46596c4be95SCédric Le Goater      * Use fake transfers to model dummy bytes. The value should
46696c4be95SCédric Le Goater      * be configured to some non-zero value in fast read mode and
46796c4be95SCédric Le Goater      * zero in read mode. But, as the HW allows inconsistent
46896c4be95SCédric Le Goater      * settings, let's check for fast read mode.
46996c4be95SCédric Le Goater      */
47096c4be95SCédric Le Goater     if (aspeed_smc_flash_mode(fl) == CTRL_FREADMODE) {
47196c4be95SCédric Le Goater         for (i = 0; i < aspeed_smc_flash_dummies(fl); i++) {
4729149af2aSCédric Le Goater             ssi_transfer(fl->controller->spi, s->regs[R_DUMMY_DATA] & 0xff);
47396c4be95SCédric Le Goater         }
47496c4be95SCédric Le Goater     }
475fcdf2c59SCédric Le Goater }
476fcdf2c59SCédric Le Goater 
477924ed163SCédric Le Goater static uint64_t aspeed_smc_flash_read(void *opaque, hwaddr addr, unsigned size)
478924ed163SCédric Le Goater {
479924ed163SCédric Le Goater     AspeedSMCFlash *fl = opaque;
480fcdf2c59SCédric Le Goater     AspeedSMCState *s = fl->controller;
481924ed163SCédric Le Goater     uint64_t ret = 0;
482924ed163SCédric Le Goater     int i;
483924ed163SCédric Le Goater 
484fcdf2c59SCédric Le Goater     switch (aspeed_smc_flash_mode(fl)) {
485fcdf2c59SCédric Le Goater     case CTRL_USERMODE:
486924ed163SCédric Le Goater         for (i = 0; i < size; i++) {
487924ed163SCédric Le Goater             ret |= ssi_transfer(s->spi, 0x0) << (8 * i);
488924ed163SCédric Le Goater         }
489fcdf2c59SCédric Le Goater         break;
490fcdf2c59SCédric Le Goater     case CTRL_READMODE:
491fcdf2c59SCédric Le Goater     case CTRL_FREADMODE:
492fcdf2c59SCédric Le Goater         aspeed_smc_flash_select(fl);
49396c4be95SCédric Le Goater         aspeed_smc_flash_setup(fl, addr);
494ac2810deSCédric Le Goater 
495fcdf2c59SCédric Le Goater         for (i = 0; i < size; i++) {
496fcdf2c59SCédric Le Goater             ret |= ssi_transfer(s->spi, 0x0) << (8 * i);
497fcdf2c59SCédric Le Goater         }
498fcdf2c59SCédric Le Goater 
499fcdf2c59SCédric Le Goater         aspeed_smc_flash_unselect(fl);
500fcdf2c59SCédric Le Goater         break;
501fcdf2c59SCédric Le Goater     default:
50232c54bd0SCédric Le Goater         aspeed_smc_error("invalid flash mode %d", aspeed_smc_flash_mode(fl));
503924ed163SCédric Le Goater     }
504924ed163SCédric Le Goater 
50510f915e4SCédric Le Goater     trace_aspeed_smc_flash_read(fl->cs, addr, size, ret,
506bd6ce9a6SCédric Le Goater                                 aspeed_smc_flash_mode(fl));
507924ed163SCédric Le Goater     return ret;
508924ed163SCédric Le Goater }
509924ed163SCédric Le Goater 
510f95c4bffSCédric Le Goater /*
511f95c4bffSCédric Le Goater  * TODO (clg@kaod.org): stolen from xilinx_spips.c. Should move to a
512f95c4bffSCédric Le Goater  * common include header.
513f95c4bffSCédric Le Goater  */
514f95c4bffSCédric Le Goater typedef enum {
515f95c4bffSCédric Le Goater     READ = 0x3,         READ_4 = 0x13,
516f95c4bffSCédric Le Goater     FAST_READ = 0xb,    FAST_READ_4 = 0x0c,
517f95c4bffSCédric Le Goater     DOR = 0x3b,         DOR_4 = 0x3c,
518f95c4bffSCédric Le Goater     QOR = 0x6b,         QOR_4 = 0x6c,
519f95c4bffSCédric Le Goater     DIOR = 0xbb,        DIOR_4 = 0xbc,
520f95c4bffSCédric Le Goater     QIOR = 0xeb,        QIOR_4 = 0xec,
521f95c4bffSCédric Le Goater 
522f95c4bffSCédric Le Goater     PP = 0x2,           PP_4 = 0x12,
523f95c4bffSCédric Le Goater     DPP = 0xa2,
524f95c4bffSCédric Le Goater     QPP = 0x32,         QPP_4 = 0x34,
525f95c4bffSCédric Le Goater } FlashCMD;
526f95c4bffSCédric Le Goater 
527f95c4bffSCédric Le Goater static int aspeed_smc_num_dummies(uint8_t command)
528f95c4bffSCédric Le Goater {
529f95c4bffSCédric Le Goater     switch (command) { /* check for dummies */
530f95c4bffSCédric Le Goater     case READ: /* no dummy bytes/cycles */
531f95c4bffSCédric Le Goater     case PP:
532f95c4bffSCédric Le Goater     case DPP:
533f95c4bffSCédric Le Goater     case QPP:
534f95c4bffSCédric Le Goater     case READ_4:
535f95c4bffSCédric Le Goater     case PP_4:
536f95c4bffSCédric Le Goater     case QPP_4:
537f95c4bffSCédric Le Goater         return 0;
538f95c4bffSCédric Le Goater     case FAST_READ:
539f95c4bffSCédric Le Goater     case DOR:
540f95c4bffSCédric Le Goater     case QOR:
5417faf6f17SGuenter Roeck     case FAST_READ_4:
542f95c4bffSCédric Le Goater     case DOR_4:
543f95c4bffSCédric Le Goater     case QOR_4:
544f95c4bffSCédric Le Goater         return 1;
545f95c4bffSCédric Le Goater     case DIOR:
546f95c4bffSCédric Le Goater     case DIOR_4:
547f95c4bffSCédric Le Goater         return 2;
548f95c4bffSCédric Le Goater     case QIOR:
549f95c4bffSCédric Le Goater     case QIOR_4:
550f95c4bffSCédric Le Goater         return 4;
551f95c4bffSCédric Le Goater     default:
552f95c4bffSCédric Le Goater         return -1;
553f95c4bffSCédric Le Goater     }
554f95c4bffSCédric Le Goater }
555f95c4bffSCédric Le Goater 
556f95c4bffSCédric Le Goater static bool aspeed_smc_do_snoop(AspeedSMCFlash *fl,  uint64_t data,
557f95c4bffSCédric Le Goater                                 unsigned size)
558f95c4bffSCédric Le Goater {
559f95c4bffSCédric Le Goater     AspeedSMCState *s = fl->controller;
560a779e37cSCédric Le Goater     uint8_t addr_width = aspeed_smc_flash_addr_width(fl);
561f95c4bffSCédric Le Goater 
56210f915e4SCédric Le Goater     trace_aspeed_smc_do_snoop(fl->cs, s->snoop_index, s->snoop_dummies,
563bd6ce9a6SCédric Le Goater                               (uint8_t) data & 0xff);
564bd6ce9a6SCédric Le Goater 
565f95c4bffSCédric Le Goater     if (s->snoop_index == SNOOP_OFF) {
566f95c4bffSCédric Le Goater         return false; /* Do nothing */
567f95c4bffSCédric Le Goater 
568f95c4bffSCédric Le Goater     } else if (s->snoop_index == SNOOP_START) {
569f95c4bffSCédric Le Goater         uint8_t cmd = data & 0xff;
570f95c4bffSCédric Le Goater         int ndummies = aspeed_smc_num_dummies(cmd);
571f95c4bffSCédric Le Goater 
572f95c4bffSCédric Le Goater         /*
573f95c4bffSCédric Le Goater          * No dummy cycles are expected with the current command. Turn
574f95c4bffSCédric Le Goater          * off snooping and let the transfer proceed normally.
575f95c4bffSCédric Le Goater          */
576f95c4bffSCédric Le Goater         if (ndummies <= 0) {
577f95c4bffSCédric Le Goater             s->snoop_index = SNOOP_OFF;
578f95c4bffSCédric Le Goater             return false;
579f95c4bffSCédric Le Goater         }
580f95c4bffSCédric Le Goater 
581f95c4bffSCédric Le Goater         s->snoop_dummies = ndummies * 8;
582f95c4bffSCédric Le Goater 
583f95c4bffSCédric Le Goater     } else if (s->snoop_index >= addr_width + 1) {
584f95c4bffSCédric Le Goater 
585f95c4bffSCédric Le Goater         /* The SPI transfer has reached the dummy cycles sequence */
586f95c4bffSCédric Le Goater         for (; s->snoop_dummies; s->snoop_dummies--) {
587f95c4bffSCédric Le Goater             ssi_transfer(s->spi, s->regs[R_DUMMY_DATA] & 0xff);
588f95c4bffSCédric Le Goater         }
589f95c4bffSCédric Le Goater 
590f95c4bffSCédric Le Goater         /* If no more dummy cycles are expected, turn off snooping */
591f95c4bffSCédric Le Goater         if (!s->snoop_dummies) {
592f95c4bffSCédric Le Goater             s->snoop_index = SNOOP_OFF;
593f95c4bffSCédric Le Goater         } else {
594f95c4bffSCédric Le Goater             s->snoop_index += size;
595f95c4bffSCédric Le Goater         }
596f95c4bffSCédric Le Goater 
597f95c4bffSCédric Le Goater         /*
598f95c4bffSCédric Le Goater          * Dummy cycles have been faked already. Ignore the current
599f95c4bffSCédric Le Goater          * SPI transfer
600f95c4bffSCédric Le Goater          */
601f95c4bffSCédric Le Goater         return true;
602f95c4bffSCédric Le Goater     }
603f95c4bffSCédric Le Goater 
604f95c4bffSCédric Le Goater     s->snoop_index += size;
605f95c4bffSCédric Le Goater     return false;
606f95c4bffSCédric Le Goater }
607f95c4bffSCédric Le Goater 
608924ed163SCédric Le Goater static void aspeed_smc_flash_write(void *opaque, hwaddr addr, uint64_t data,
609924ed163SCédric Le Goater                                    unsigned size)
610924ed163SCédric Le Goater {
611924ed163SCédric Le Goater     AspeedSMCFlash *fl = opaque;
612fcdf2c59SCédric Le Goater     AspeedSMCState *s = fl->controller;
613924ed163SCédric Le Goater     int i;
614924ed163SCédric Le Goater 
61510f915e4SCédric Le Goater     trace_aspeed_smc_flash_write(fl->cs, addr, size, data,
616bd6ce9a6SCédric Le Goater                                  aspeed_smc_flash_mode(fl));
617bd6ce9a6SCédric Le Goater 
618f248a9dbSCédric Le Goater     if (!aspeed_smc_is_writable(fl)) {
61932c54bd0SCédric Le Goater         aspeed_smc_error("flash is not writable at 0x%" HWADDR_PRIx, addr);
620924ed163SCédric Le Goater         return;
621924ed163SCédric Le Goater     }
622924ed163SCédric Le Goater 
623fcdf2c59SCédric Le Goater     switch (aspeed_smc_flash_mode(fl)) {
624fcdf2c59SCédric Le Goater     case CTRL_USERMODE:
625f95c4bffSCédric Le Goater         if (aspeed_smc_do_snoop(fl, data, size)) {
626f95c4bffSCédric Le Goater             break;
627f95c4bffSCédric Le Goater         }
628f95c4bffSCédric Le Goater 
629fcdf2c59SCédric Le Goater         for (i = 0; i < size; i++) {
630fcdf2c59SCédric Le Goater             ssi_transfer(s->spi, (data >> (8 * i)) & 0xff);
631924ed163SCédric Le Goater         }
632fcdf2c59SCédric Le Goater         break;
633fcdf2c59SCédric Le Goater     case CTRL_WRITEMODE:
634fcdf2c59SCédric Le Goater         aspeed_smc_flash_select(fl);
63596c4be95SCédric Le Goater         aspeed_smc_flash_setup(fl, addr);
636924ed163SCédric Le Goater 
637924ed163SCédric Le Goater         for (i = 0; i < size; i++) {
638924ed163SCédric Le Goater             ssi_transfer(s->spi, (data >> (8 * i)) & 0xff);
639924ed163SCédric Le Goater         }
640fcdf2c59SCédric Le Goater 
641fcdf2c59SCédric Le Goater         aspeed_smc_flash_unselect(fl);
642fcdf2c59SCédric Le Goater         break;
643fcdf2c59SCédric Le Goater     default:
64432c54bd0SCédric Le Goater         aspeed_smc_error("invalid flash mode %d", aspeed_smc_flash_mode(fl));
645fcdf2c59SCédric Le Goater     }
646924ed163SCédric Le Goater }
647924ed163SCédric Le Goater 
648924ed163SCédric Le Goater static const MemoryRegionOps aspeed_smc_flash_ops = {
649924ed163SCédric Le Goater     .read = aspeed_smc_flash_read,
650924ed163SCédric Le Goater     .write = aspeed_smc_flash_write,
651924ed163SCédric Le Goater     .endianness = DEVICE_LITTLE_ENDIAN,
652924ed163SCédric Le Goater     .valid = {
653924ed163SCédric Le Goater         .min_access_size = 1,
654924ed163SCédric Le Goater         .max_access_size = 4,
655924ed163SCédric Le Goater     },
6567c1c69bcSCédric Le Goater };
6577c1c69bcSCédric Le Goater 
658e7e741caSCédric Le Goater static void aspeed_smc_flash_update_ctrl(AspeedSMCFlash *fl, uint32_t value)
6597c1c69bcSCédric Le Goater {
660f95c4bffSCédric Le Goater     AspeedSMCState *s = fl->controller;
661e7e741caSCédric Le Goater     bool unselect;
662f95c4bffSCédric Le Goater 
663e7e741caSCédric Le Goater     /* User mode selects the CS, other modes unselect */
664e7e741caSCédric Le Goater     unselect = (value & CTRL_CMD_MODE_MASK) != CTRL_USERMODE;
6657c1c69bcSCédric Le Goater 
666e7e741caSCédric Le Goater     /* A change of CTRL_CE_STOP_ACTIVE from 0 to 1, unselects the CS */
66710f915e4SCédric Le Goater     if (!(s->regs[s->r_ctrl0 + fl->cs] & CTRL_CE_STOP_ACTIVE) &&
668e7e741caSCédric Le Goater         value & CTRL_CE_STOP_ACTIVE) {
669e7e741caSCédric Le Goater         unselect = true;
670e7e741caSCédric Le Goater     }
671e7e741caSCédric Le Goater 
67210f915e4SCédric Le Goater     s->regs[s->r_ctrl0 + fl->cs] = value;
673e7e741caSCédric Le Goater 
674e7e741caSCédric Le Goater     s->snoop_index = unselect ? SNOOP_OFF : SNOOP_START;
675e7e741caSCédric Le Goater 
676e7e741caSCédric Le Goater     aspeed_smc_flash_do_select(fl, unselect);
6777c1c69bcSCédric Le Goater }
6787c1c69bcSCédric Le Goater 
6797c1c69bcSCédric Le Goater static void aspeed_smc_reset(DeviceState *d)
6807c1c69bcSCédric Le Goater {
6817c1c69bcSCédric Le Goater     AspeedSMCState *s = ASPEED_SMC(d);
68230b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_GET_CLASS(s);
6837c1c69bcSCédric Le Goater     int i;
6847c1c69bcSCédric Le Goater 
68571255c48SCédric Le Goater     if (asc->resets) {
68671255c48SCédric Le Goater         memcpy(s->regs, asc->resets, sizeof s->regs);
68771255c48SCédric Le Goater     } else {
6887c1c69bcSCédric Le Goater         memset(s->regs, 0, sizeof s->regs);
68971255c48SCédric Le Goater     }
6907c1c69bcSCédric Le Goater 
6915ade579bSPhilippe Mathieu-Daudé     /* Unselect all peripherals */
6927c1c69bcSCédric Le Goater     for (i = 0; i < s->num_cs; ++i) {
6937c1c69bcSCédric Le Goater         s->regs[s->r_ctrl0 + i] |= CTRL_CE_STOP_ACTIVE;
6941d247bd0SCédric Le Goater         qemu_set_irq(s->cs_lines[i], true);
6957c1c69bcSCédric Le Goater     }
6967c1c69bcSCédric Le Goater 
697673b1f86SCédric Le Goater     /* setup the default segment register values and regions for all */
69830b6852cSCédric Le Goater     for (i = 0; i < asc->max_peripherals; ++i) {
699673b1f86SCédric Le Goater         aspeed_smc_flash_set_segment_region(s, i,
70030b6852cSCédric Le Goater                     asc->segment_to_reg(s, &asc->segments[i]));
701a03cb1daSCédric Le Goater     }
7020707b34dSCédric Le Goater 
703f95c4bffSCédric Le Goater     s->snoop_index = SNOOP_OFF;
704f95c4bffSCédric Le Goater     s->snoop_dummies = 0;
7057c1c69bcSCédric Le Goater }
7067c1c69bcSCédric Le Goater 
7077c1c69bcSCédric Le Goater static uint64_t aspeed_smc_read(void *opaque, hwaddr addr, unsigned int size)
7087c1c69bcSCédric Le Goater {
7097c1c69bcSCédric Le Goater     AspeedSMCState *s = ASPEED_SMC(opaque);
71030b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_GET_CLASS(opaque);
7117c1c69bcSCédric Le Goater 
7127c1c69bcSCédric Le Goater     addr >>= 2;
7137c1c69bcSCédric Le Goater 
71497c2ed5dSCédric Le Goater     if (addr == s->r_conf ||
715f286f04cSCédric Le Goater         (addr >= s->r_timings &&
71630b6852cSCédric Le Goater          addr < s->r_timings + asc->nregs_timings) ||
71797c2ed5dSCédric Le Goater         addr == s->r_ce_ctrl ||
718af453a5eSCédric Le Goater         addr == R_CE_CMD_CTRL ||
7192e1f0502SCédric Le Goater         addr == R_INTR_CTRL ||
7209149af2aSCédric Le Goater         addr == R_DUMMY_DATA ||
72130b6852cSCédric Le Goater         (aspeed_smc_has_wdt_control(asc) && addr == R_FMC_WDT2_CTRL) ||
72230b6852cSCédric Le Goater         (aspeed_smc_has_dma(asc) && addr == R_DMA_CTRL) ||
72330b6852cSCédric Le Goater         (aspeed_smc_has_dma(asc) && addr == R_DMA_FLASH_ADDR) ||
72430b6852cSCédric Le Goater         (aspeed_smc_has_dma(asc) && addr == R_DMA_DRAM_ADDR) ||
72530b6852cSCédric Le Goater         (aspeed_smc_has_dma(asc) && addr == R_DMA_LEN) ||
72630b6852cSCédric Le Goater         (aspeed_smc_has_dma(asc) && addr == R_DMA_CHECKSUM) ||
7275ade579bSPhilippe Mathieu-Daudé         (addr >= R_SEG_ADDR0 &&
72830b6852cSCédric Le Goater          addr < R_SEG_ADDR0 + asc->max_peripherals) ||
72930b6852cSCédric Le Goater         (addr >= s->r_ctrl0 && addr < s->r_ctrl0 + asc->max_peripherals)) {
730bd6ce9a6SCédric Le Goater 
731*e2804a1eSCédric Le Goater         trace_aspeed_smc_read(addr << 2, size, s->regs[addr]);
732bd6ce9a6SCédric Le Goater 
73397c2ed5dSCédric Le Goater         return s->regs[addr];
73497c2ed5dSCédric Le Goater     } else {
7357c1c69bcSCédric Le Goater         qemu_log_mask(LOG_UNIMP, "%s: not implemented: 0x%" HWADDR_PRIx "\n",
7367c1c69bcSCédric Le Goater                       __func__, addr);
737b617ca92SCédric Le Goater         return -1;
7387c1c69bcSCédric Le Goater     }
7397c1c69bcSCédric Le Goater }
7407c1c69bcSCédric Le Goater 
7410d72c717SCédric Le Goater static uint8_t aspeed_smc_hclk_divisor(uint8_t hclk_mask)
7420d72c717SCédric Le Goater {
7430d72c717SCédric Le Goater     /* HCLK/1 .. HCLK/16 */
7440d72c717SCédric Le Goater     const uint8_t hclk_divisors[] = {
7450d72c717SCédric Le Goater         15, 7, 14, 6, 13, 5, 12, 4, 11, 3, 10, 2, 9, 1, 8, 0
7460d72c717SCédric Le Goater     };
7470d72c717SCédric Le Goater     int i;
7480d72c717SCédric Le Goater 
7490d72c717SCédric Le Goater     for (i = 0; i < ARRAY_SIZE(hclk_divisors); i++) {
7500d72c717SCédric Le Goater         if (hclk_mask == hclk_divisors[i]) {
7510d72c717SCédric Le Goater             return i + 1;
7520d72c717SCédric Le Goater         }
7530d72c717SCédric Le Goater     }
7540d72c717SCédric Le Goater 
75532c54bd0SCédric Le Goater     aspeed_smc_error("invalid HCLK mask %x", hclk_mask);
7560d72c717SCédric Le Goater     return 0;
7570d72c717SCédric Le Goater }
7580d72c717SCédric Le Goater 
7590d72c717SCédric Le Goater /*
7600d72c717SCédric Le Goater  * When doing calibration, the SPI clock rate in the CE0 Control
7610d72c717SCédric Le Goater  * Register and the read delay cycles in the Read Timing Compensation
7620d72c717SCédric Le Goater  * Register are set using bit[11:4] of the DMA Control Register.
7630d72c717SCédric Le Goater  */
7640d72c717SCédric Le Goater static void aspeed_smc_dma_calibration(AspeedSMCState *s)
7650d72c717SCédric Le Goater {
7660d72c717SCédric Le Goater     uint8_t delay =
7670d72c717SCédric Le Goater         (s->regs[R_DMA_CTRL] >> DMA_CTRL_DELAY_SHIFT) & DMA_CTRL_DELAY_MASK;
7680d72c717SCédric Le Goater     uint8_t hclk_mask =
7690d72c717SCédric Le Goater         (s->regs[R_DMA_CTRL] >> DMA_CTRL_FREQ_SHIFT) & DMA_CTRL_FREQ_MASK;
7700d72c717SCédric Le Goater     uint8_t hclk_div = aspeed_smc_hclk_divisor(hclk_mask);
7710d72c717SCédric Le Goater     uint32_t hclk_shift = (hclk_div - 1) << 2;
7720d72c717SCédric Le Goater     uint8_t cs;
7730d72c717SCédric Le Goater 
7740d72c717SCédric Le Goater     /*
7750d72c717SCédric Le Goater      * The Read Timing Compensation Register values apply to all CS on
7760d72c717SCédric Le Goater      * the SPI bus and only HCLK/1 - HCLK/5 can have tunable delays
7770d72c717SCédric Le Goater      */
7780d72c717SCédric Le Goater     if (hclk_div && hclk_div < 6) {
7790d72c717SCédric Le Goater         s->regs[s->r_timings] &= ~(0xf << hclk_shift);
7800d72c717SCédric Le Goater         s->regs[s->r_timings] |= delay << hclk_shift;
7810d72c717SCédric Le Goater     }
7820d72c717SCédric Le Goater 
7830d72c717SCédric Le Goater     /*
7840d72c717SCédric Le Goater      * TODO: compute the CS from the DMA address and the segment
7850d72c717SCédric Le Goater      * registers. This is not really a problem for now because the
7860d72c717SCédric Le Goater      * Timing Register values apply to all CS and software uses CS0 to
7870d72c717SCédric Le Goater      * do calibration.
7880d72c717SCédric Le Goater      */
7890d72c717SCédric Le Goater     cs = 0;
7900d72c717SCédric Le Goater     s->regs[s->r_ctrl0 + cs] &=
7910d72c717SCédric Le Goater         ~(CE_CTRL_CLOCK_FREQ_MASK << CE_CTRL_CLOCK_FREQ_SHIFT);
7920d72c717SCédric Le Goater     s->regs[s->r_ctrl0 + cs] |= CE_CTRL_CLOCK_FREQ(hclk_div);
7930d72c717SCédric Le Goater }
7940d72c717SCédric Le Goater 
795c4e1f0b4SCédric Le Goater /*
7965258c2a6SCédric Le Goater  * Emulate read errors in the DMA Checksum Register for high
7975258c2a6SCédric Le Goater  * frequencies and optimistic settings of the Read Timing Compensation
7985258c2a6SCédric Le Goater  * Register. This will help in tuning the SPI timing calibration
7995258c2a6SCédric Le Goater  * algorithm.
8005258c2a6SCédric Le Goater  */
8015258c2a6SCédric Le Goater static bool aspeed_smc_inject_read_failure(AspeedSMCState *s)
8025258c2a6SCédric Le Goater {
8035258c2a6SCédric Le Goater     uint8_t delay =
8045258c2a6SCédric Le Goater         (s->regs[R_DMA_CTRL] >> DMA_CTRL_DELAY_SHIFT) & DMA_CTRL_DELAY_MASK;
8055258c2a6SCédric Le Goater     uint8_t hclk_mask =
8065258c2a6SCédric Le Goater         (s->regs[R_DMA_CTRL] >> DMA_CTRL_FREQ_SHIFT) & DMA_CTRL_FREQ_MASK;
8075258c2a6SCédric Le Goater 
8085258c2a6SCédric Le Goater     /*
8095258c2a6SCédric Le Goater      * Typical values of a palmetto-bmc machine.
8105258c2a6SCédric Le Goater      */
8115258c2a6SCédric Le Goater     switch (aspeed_smc_hclk_divisor(hclk_mask)) {
8125258c2a6SCédric Le Goater     case 4 ... 16:
8135258c2a6SCédric Le Goater         return false;
8145258c2a6SCédric Le Goater     case 3: /* at least one HCLK cycle delay */
8155258c2a6SCédric Le Goater         return (delay & 0x7) < 1;
8165258c2a6SCédric Le Goater     case 2: /* at least two HCLK cycle delay */
8175258c2a6SCédric Le Goater         return (delay & 0x7) < 2;
8185258c2a6SCédric Le Goater     case 1: /* (> 100MHz) is above the max freq of the controller */
8195258c2a6SCédric Le Goater         return true;
8205258c2a6SCédric Le Goater     default:
8215258c2a6SCédric Le Goater         g_assert_not_reached();
8225258c2a6SCédric Le Goater     }
8235258c2a6SCédric Le Goater }
8245258c2a6SCédric Le Goater 
8255258c2a6SCédric Le Goater /*
826c4e1f0b4SCédric Le Goater  * Accumulate the result of the reads to provide a checksum that will
827c4e1f0b4SCédric Le Goater  * be used to validate the read timing settings.
828c4e1f0b4SCédric Le Goater  */
829c4e1f0b4SCédric Le Goater static void aspeed_smc_dma_checksum(AspeedSMCState *s)
830c4e1f0b4SCédric Le Goater {
831c4e1f0b4SCédric Le Goater     MemTxResult result;
832c4e1f0b4SCédric Le Goater     uint32_t data;
833c4e1f0b4SCédric Le Goater 
834c4e1f0b4SCédric Le Goater     if (s->regs[R_DMA_CTRL] & DMA_CTRL_WRITE) {
83532c54bd0SCédric Le Goater         aspeed_smc_error("invalid direction for DMA checksum");
836c4e1f0b4SCédric Le Goater         return;
837c4e1f0b4SCédric Le Goater     }
838c4e1f0b4SCédric Le Goater 
8390d72c717SCédric Le Goater     if (s->regs[R_DMA_CTRL] & DMA_CTRL_CALIB) {
8400d72c717SCédric Le Goater         aspeed_smc_dma_calibration(s);
8410d72c717SCédric Le Goater     }
8420d72c717SCédric Le Goater 
843c4e1f0b4SCédric Le Goater     while (s->regs[R_DMA_LEN]) {
844c4e1f0b4SCédric Le Goater         data = address_space_ldl_le(&s->flash_as, s->regs[R_DMA_FLASH_ADDR],
845c4e1f0b4SCédric Le Goater                                     MEMTXATTRS_UNSPECIFIED, &result);
846c4e1f0b4SCédric Le Goater         if (result != MEMTX_OK) {
84732c54bd0SCédric Le Goater             aspeed_smc_error("Flash read failed @%08x",
84832c54bd0SCédric Le Goater                              s->regs[R_DMA_FLASH_ADDR]);
849c4e1f0b4SCédric Le Goater             return;
850c4e1f0b4SCédric Le Goater         }
851bd6ce9a6SCédric Le Goater         trace_aspeed_smc_dma_checksum(s->regs[R_DMA_FLASH_ADDR], data);
852c4e1f0b4SCédric Le Goater 
853c4e1f0b4SCédric Le Goater         /*
854c4e1f0b4SCédric Le Goater          * When the DMA is on-going, the DMA registers are updated
855c4e1f0b4SCédric Le Goater          * with the current working addresses and length.
856c4e1f0b4SCédric Le Goater          */
857c4e1f0b4SCédric Le Goater         s->regs[R_DMA_CHECKSUM] += data;
858c4e1f0b4SCédric Le Goater         s->regs[R_DMA_FLASH_ADDR] += 4;
859c4e1f0b4SCédric Le Goater         s->regs[R_DMA_LEN] -= 4;
860c4e1f0b4SCédric Le Goater     }
8615258c2a6SCédric Le Goater 
8625258c2a6SCédric Le Goater     if (s->inject_failure && aspeed_smc_inject_read_failure(s)) {
8635258c2a6SCédric Le Goater         s->regs[R_DMA_CHECKSUM] = 0xbadc0de;
8645258c2a6SCédric Le Goater     }
8655258c2a6SCédric Le Goater 
866c4e1f0b4SCédric Le Goater }
867c4e1f0b4SCédric Le Goater 
868c4e1f0b4SCédric Le Goater static void aspeed_smc_dma_rw(AspeedSMCState *s)
869c4e1f0b4SCédric Le Goater {
870c4e1f0b4SCédric Le Goater     MemTxResult result;
871c4e1f0b4SCédric Le Goater     uint32_t data;
872c4e1f0b4SCédric Le Goater 
8734dabf395SCédric Le Goater     trace_aspeed_smc_dma_rw(s->regs[R_DMA_CTRL] & DMA_CTRL_WRITE ?
8744dabf395SCédric Le Goater                             "write" : "read",
8754dabf395SCédric Le Goater                             s->regs[R_DMA_FLASH_ADDR],
8764dabf395SCédric Le Goater                             s->regs[R_DMA_DRAM_ADDR],
8774dabf395SCédric Le Goater                             s->regs[R_DMA_LEN]);
878c4e1f0b4SCédric Le Goater     while (s->regs[R_DMA_LEN]) {
879c4e1f0b4SCédric Le Goater         if (s->regs[R_DMA_CTRL] & DMA_CTRL_WRITE) {
880c4e1f0b4SCédric Le Goater             data = address_space_ldl_le(&s->dram_as, s->regs[R_DMA_DRAM_ADDR],
881c4e1f0b4SCédric Le Goater                                         MEMTXATTRS_UNSPECIFIED, &result);
882c4e1f0b4SCédric Le Goater             if (result != MEMTX_OK) {
88332c54bd0SCédric Le Goater                 aspeed_smc_error("DRAM read failed @%08x",
88432c54bd0SCédric Le Goater                                  s->regs[R_DMA_DRAM_ADDR]);
885c4e1f0b4SCédric Le Goater                 return;
886c4e1f0b4SCédric Le Goater             }
887c4e1f0b4SCédric Le Goater 
888c4e1f0b4SCédric Le Goater             address_space_stl_le(&s->flash_as, s->regs[R_DMA_FLASH_ADDR],
889c4e1f0b4SCédric Le Goater                                  data, MEMTXATTRS_UNSPECIFIED, &result);
890c4e1f0b4SCédric Le Goater             if (result != MEMTX_OK) {
89132c54bd0SCédric Le Goater                 aspeed_smc_error("Flash write failed @%08x",
89232c54bd0SCédric Le Goater                                  s->regs[R_DMA_FLASH_ADDR]);
893c4e1f0b4SCédric Le Goater                 return;
894c4e1f0b4SCédric Le Goater             }
895c4e1f0b4SCédric Le Goater         } else {
896c4e1f0b4SCédric Le Goater             data = address_space_ldl_le(&s->flash_as, s->regs[R_DMA_FLASH_ADDR],
897c4e1f0b4SCédric Le Goater                                         MEMTXATTRS_UNSPECIFIED, &result);
898c4e1f0b4SCédric Le Goater             if (result != MEMTX_OK) {
89932c54bd0SCédric Le Goater                 aspeed_smc_error("Flash read failed @%08x",
90032c54bd0SCédric Le Goater                                  s->regs[R_DMA_FLASH_ADDR]);
901c4e1f0b4SCédric Le Goater                 return;
902c4e1f0b4SCédric Le Goater             }
903c4e1f0b4SCédric Le Goater 
904c4e1f0b4SCédric Le Goater             address_space_stl_le(&s->dram_as, s->regs[R_DMA_DRAM_ADDR],
905c4e1f0b4SCédric Le Goater                                  data, MEMTXATTRS_UNSPECIFIED, &result);
906c4e1f0b4SCédric Le Goater             if (result != MEMTX_OK) {
90732c54bd0SCédric Le Goater                 aspeed_smc_error("DRAM write failed @%08x",
90832c54bd0SCédric Le Goater                                  s->regs[R_DMA_DRAM_ADDR]);
909c4e1f0b4SCédric Le Goater                 return;
910c4e1f0b4SCédric Le Goater             }
911c4e1f0b4SCédric Le Goater         }
912c4e1f0b4SCédric Le Goater 
913c4e1f0b4SCédric Le Goater         /*
914c4e1f0b4SCédric Le Goater          * When the DMA is on-going, the DMA registers are updated
915c4e1f0b4SCédric Le Goater          * with the current working addresses and length.
916c4e1f0b4SCédric Le Goater          */
917c4e1f0b4SCédric Le Goater         s->regs[R_DMA_FLASH_ADDR] += 4;
918c4e1f0b4SCédric Le Goater         s->regs[R_DMA_DRAM_ADDR] += 4;
919c4e1f0b4SCédric Le Goater         s->regs[R_DMA_LEN] -= 4;
920ae275f71SChristian Svensson         s->regs[R_DMA_CHECKSUM] += data;
921c4e1f0b4SCédric Le Goater     }
922c4e1f0b4SCédric Le Goater }
923c4e1f0b4SCédric Le Goater 
924c4e1f0b4SCédric Le Goater static void aspeed_smc_dma_stop(AspeedSMCState *s)
925c4e1f0b4SCédric Le Goater {
926c4e1f0b4SCédric Le Goater     /*
927c4e1f0b4SCédric Le Goater      * When the DMA is disabled, INTR_CTRL_DMA_STATUS=0 means the
928c4e1f0b4SCédric Le Goater      * engine is idle
929c4e1f0b4SCédric Le Goater      */
930c4e1f0b4SCédric Le Goater     s->regs[R_INTR_CTRL] &= ~INTR_CTRL_DMA_STATUS;
931c4e1f0b4SCédric Le Goater     s->regs[R_DMA_CHECKSUM] = 0;
932c4e1f0b4SCédric Le Goater 
933c4e1f0b4SCédric Le Goater     /*
934c4e1f0b4SCédric Le Goater      * Lower the DMA irq in any case. The IRQ control register could
935c4e1f0b4SCédric Le Goater      * have been cleared before disabling the DMA.
936c4e1f0b4SCédric Le Goater      */
937c4e1f0b4SCédric Le Goater     qemu_irq_lower(s->irq);
938c4e1f0b4SCédric Le Goater }
939c4e1f0b4SCédric Le Goater 
940c4e1f0b4SCédric Le Goater /*
941c4e1f0b4SCédric Le Goater  * When INTR_CTRL_DMA_STATUS=1, the DMA has completed and a new DMA
942c4e1f0b4SCédric Le Goater  * can start even if the result of the previous was not collected.
943c4e1f0b4SCédric Le Goater  */
944c4e1f0b4SCédric Le Goater static bool aspeed_smc_dma_in_progress(AspeedSMCState *s)
945c4e1f0b4SCédric Le Goater {
946c4e1f0b4SCédric Le Goater     return s->regs[R_DMA_CTRL] & DMA_CTRL_ENABLE &&
947c4e1f0b4SCédric Le Goater         !(s->regs[R_INTR_CTRL] & INTR_CTRL_DMA_STATUS);
948c4e1f0b4SCédric Le Goater }
949c4e1f0b4SCédric Le Goater 
950c4e1f0b4SCédric Le Goater static void aspeed_smc_dma_done(AspeedSMCState *s)
951c4e1f0b4SCédric Le Goater {
952c4e1f0b4SCédric Le Goater     s->regs[R_INTR_CTRL] |= INTR_CTRL_DMA_STATUS;
953c4e1f0b4SCédric Le Goater     if (s->regs[R_INTR_CTRL] & INTR_CTRL_DMA_EN) {
954c4e1f0b4SCédric Le Goater         qemu_irq_raise(s->irq);
955c4e1f0b4SCédric Le Goater     }
956c4e1f0b4SCédric Le Goater }
957c4e1f0b4SCédric Le Goater 
9581769a70eSCédric Le Goater static void aspeed_smc_dma_ctrl(AspeedSMCState *s, uint32_t dma_ctrl)
959c4e1f0b4SCédric Le Goater {
960c4e1f0b4SCédric Le Goater     if (!(dma_ctrl & DMA_CTRL_ENABLE)) {
961c4e1f0b4SCédric Le Goater         s->regs[R_DMA_CTRL] = dma_ctrl;
962c4e1f0b4SCédric Le Goater 
963c4e1f0b4SCédric Le Goater         aspeed_smc_dma_stop(s);
964c4e1f0b4SCédric Le Goater         return;
965c4e1f0b4SCédric Le Goater     }
966c4e1f0b4SCédric Le Goater 
967c4e1f0b4SCédric Le Goater     if (aspeed_smc_dma_in_progress(s)) {
96832c54bd0SCédric Le Goater         aspeed_smc_error("DMA in progress !");
969c4e1f0b4SCédric Le Goater         return;
970c4e1f0b4SCédric Le Goater     }
971c4e1f0b4SCédric Le Goater 
972c4e1f0b4SCédric Le Goater     s->regs[R_DMA_CTRL] = dma_ctrl;
973c4e1f0b4SCédric Le Goater 
974c4e1f0b4SCédric Le Goater     if (s->regs[R_DMA_CTRL] & DMA_CTRL_CKSUM) {
975c4e1f0b4SCédric Le Goater         aspeed_smc_dma_checksum(s);
976c4e1f0b4SCédric Le Goater     } else {
977c4e1f0b4SCédric Le Goater         aspeed_smc_dma_rw(s);
978c4e1f0b4SCédric Le Goater     }
979c4e1f0b4SCédric Le Goater 
980c4e1f0b4SCédric Le Goater     aspeed_smc_dma_done(s);
981c4e1f0b4SCédric Le Goater }
982c4e1f0b4SCédric Le Goater 
9831769a70eSCédric Le Goater static inline bool aspeed_smc_dma_granted(AspeedSMCState *s)
9841769a70eSCédric Le Goater {
98530b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_GET_CLASS(s);
98630b6852cSCédric Le Goater 
98730b6852cSCédric Le Goater     if (!(asc->features & ASPEED_SMC_FEATURE_DMA_GRANT)) {
9881769a70eSCédric Le Goater         return true;
9891769a70eSCédric Le Goater     }
9901769a70eSCédric Le Goater 
9911769a70eSCédric Le Goater     if (!(s->regs[R_DMA_CTRL] & DMA_CTRL_GRANT)) {
99232c54bd0SCédric Le Goater         aspeed_smc_error("DMA not granted");
9931769a70eSCédric Le Goater         return false;
9941769a70eSCédric Le Goater     }
9951769a70eSCédric Le Goater 
9961769a70eSCédric Le Goater     return true;
9971769a70eSCédric Le Goater }
9981769a70eSCédric Le Goater 
9991769a70eSCédric Le Goater static void aspeed_2600_smc_dma_ctrl(AspeedSMCState *s, uint32_t dma_ctrl)
10001769a70eSCédric Le Goater {
10011769a70eSCédric Le Goater     /* Preserve DMA bits  */
10021769a70eSCédric Le Goater     dma_ctrl |= s->regs[R_DMA_CTRL] & (DMA_CTRL_REQUEST | DMA_CTRL_GRANT);
10031769a70eSCédric Le Goater 
10041769a70eSCédric Le Goater     if (dma_ctrl == 0xAEED0000) {
10051769a70eSCédric Le Goater         /* automatically grant request */
10061769a70eSCédric Le Goater         s->regs[R_DMA_CTRL] |= (DMA_CTRL_REQUEST | DMA_CTRL_GRANT);
10071769a70eSCédric Le Goater         return;
10081769a70eSCédric Le Goater     }
10091769a70eSCédric Le Goater 
10101769a70eSCédric Le Goater     /* clear request */
10111769a70eSCédric Le Goater     if (dma_ctrl == 0xDEEA0000) {
10121769a70eSCédric Le Goater         s->regs[R_DMA_CTRL] &= ~(DMA_CTRL_REQUEST | DMA_CTRL_GRANT);
10131769a70eSCédric Le Goater         return;
10141769a70eSCédric Le Goater     }
10151769a70eSCédric Le Goater 
10161769a70eSCédric Le Goater     if (!aspeed_smc_dma_granted(s)) {
101732c54bd0SCédric Le Goater         aspeed_smc_error("DMA not granted");
10181769a70eSCédric Le Goater         return;
10191769a70eSCédric Le Goater     }
10201769a70eSCédric Le Goater 
10211769a70eSCédric Le Goater     aspeed_smc_dma_ctrl(s, dma_ctrl);
10221769a70eSCédric Le Goater     s->regs[R_DMA_CTRL] &= ~(DMA_CTRL_REQUEST | DMA_CTRL_GRANT);
10231769a70eSCédric Le Goater }
10241769a70eSCédric Le Goater 
10257c1c69bcSCédric Le Goater static void aspeed_smc_write(void *opaque, hwaddr addr, uint64_t data,
10267c1c69bcSCédric Le Goater                              unsigned int size)
10277c1c69bcSCédric Le Goater {
10287c1c69bcSCédric Le Goater     AspeedSMCState *s = ASPEED_SMC(opaque);
102930b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_GET_CLASS(s);
10307c1c69bcSCédric Le Goater     uint32_t value = data;
10317c1c69bcSCédric Le Goater 
1032bd6ce9a6SCédric Le Goater     trace_aspeed_smc_write(addr, size, data);
1033bd6ce9a6SCédric Le Goater 
1034*e2804a1eSCédric Le Goater     addr >>= 2;
1035*e2804a1eSCédric Le Goater 
103697c2ed5dSCédric Le Goater     if (addr == s->r_conf ||
1037f286f04cSCédric Le Goater         (addr >= s->r_timings &&
103830b6852cSCédric Le Goater          addr < s->r_timings + asc->nregs_timings) ||
103997c2ed5dSCédric Le Goater         addr == s->r_ce_ctrl) {
104097c2ed5dSCédric Le Goater         s->regs[addr] = value;
104197c2ed5dSCédric Le Goater     } else if (addr >= s->r_ctrl0 && addr < s->r_ctrl0 + s->num_cs) {
1042f248a9dbSCédric Le Goater         int cs = addr - s->r_ctrl0;
1043e7e741caSCédric Le Goater         aspeed_smc_flash_update_ctrl(&s->flashes[cs], value);
1044a03cb1daSCédric Le Goater     } else if (addr >= R_SEG_ADDR0 &&
104530b6852cSCédric Le Goater                addr < R_SEG_ADDR0 + asc->max_peripherals) {
1046a03cb1daSCédric Le Goater         int cs = addr - R_SEG_ADDR0;
1047a03cb1daSCédric Le Goater 
1048a03cb1daSCédric Le Goater         if (value != s->regs[R_SEG_ADDR0 + cs]) {
1049a03cb1daSCédric Le Goater             aspeed_smc_flash_set_segment(s, cs, value);
1050a03cb1daSCédric Le Goater         }
1051af453a5eSCédric Le Goater     } else if (addr == R_CE_CMD_CTRL) {
1052af453a5eSCédric Le Goater         s->regs[addr] = value & 0xff;
10539149af2aSCédric Le Goater     } else if (addr == R_DUMMY_DATA) {
10549149af2aSCédric Le Goater         s->regs[addr] = value & 0xff;
105530b6852cSCédric Le Goater     } else if (aspeed_smc_has_wdt_control(asc) && addr == R_FMC_WDT2_CTRL) {
105645a904afSCédric Le Goater         s->regs[addr] = value & FMC_WDT2_CTRL_EN;
1057c4e1f0b4SCédric Le Goater     } else if (addr == R_INTR_CTRL) {
1058c4e1f0b4SCédric Le Goater         s->regs[addr] = value;
105930b6852cSCédric Le Goater     } else if (aspeed_smc_has_dma(asc) && addr == R_DMA_CTRL) {
106030b6852cSCédric Le Goater         asc->dma_ctrl(s, value);
106130b6852cSCédric Le Goater     } else if (aspeed_smc_has_dma(asc) && addr == R_DMA_DRAM_ADDR &&
10621769a70eSCédric Le Goater                aspeed_smc_dma_granted(s)) {
106330b6852cSCédric Le Goater         s->regs[addr] = DMA_DRAM_ADDR(asc, value);
106430b6852cSCédric Le Goater     } else if (aspeed_smc_has_dma(asc) && addr == R_DMA_FLASH_ADDR &&
10651769a70eSCédric Le Goater                aspeed_smc_dma_granted(s)) {
106630b6852cSCédric Le Goater         s->regs[addr] = DMA_FLASH_ADDR(asc, value);
106730b6852cSCédric Le Goater     } else if (aspeed_smc_has_dma(asc) && addr == R_DMA_LEN &&
10681769a70eSCédric Le Goater                aspeed_smc_dma_granted(s)) {
1069c4e1f0b4SCédric Le Goater         s->regs[addr] = DMA_LENGTH(value);
107097c2ed5dSCédric Le Goater     } else {
10717c1c69bcSCédric Le Goater         qemu_log_mask(LOG_UNIMP, "%s: not implemented: 0x%" HWADDR_PRIx "\n",
10727c1c69bcSCédric Le Goater                       __func__, addr);
10737c1c69bcSCédric Le Goater         return;
10747c1c69bcSCédric Le Goater     }
10757c1c69bcSCédric Le Goater }
10767c1c69bcSCédric Le Goater 
10777c1c69bcSCédric Le Goater static const MemoryRegionOps aspeed_smc_ops = {
10787c1c69bcSCédric Le Goater     .read = aspeed_smc_read,
10797c1c69bcSCédric Le Goater     .write = aspeed_smc_write,
10807c1c69bcSCédric Le Goater     .endianness = DEVICE_LITTLE_ENDIAN,
10817c1c69bcSCédric Le Goater };
10827c1c69bcSCédric Le Goater 
1083f75b5331SCédric Le Goater static void aspeed_smc_instance_init(Object *obj)
1084f75b5331SCédric Le Goater {
1085f75b5331SCédric Le Goater     AspeedSMCState *s = ASPEED_SMC(obj);
1086f75b5331SCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_GET_CLASS(s);
1087f75b5331SCédric Le Goater     int i;
1088f75b5331SCédric Le Goater 
1089f75b5331SCédric Le Goater     for (i = 0; i < asc->max_peripherals; i++) {
1090f75b5331SCédric Le Goater         object_initialize_child(obj, "flash[*]", &s->flashes[i],
1091f75b5331SCédric Le Goater                                 TYPE_ASPEED_SMC_FLASH);
1092f75b5331SCédric Le Goater     }
1093f75b5331SCédric Le Goater }
1094f75b5331SCédric Le Goater 
1095c4e1f0b4SCédric Le Goater /*
1096c4e1f0b4SCédric Le Goater  * Initialize the custom address spaces for DMAs
1097c4e1f0b4SCédric Le Goater  */
1098c4e1f0b4SCédric Le Goater static void aspeed_smc_dma_setup(AspeedSMCState *s, Error **errp)
1099c4e1f0b4SCédric Le Goater {
1100c4e1f0b4SCédric Le Goater     if (!s->dram_mr) {
1101c4e1f0b4SCédric Le Goater         error_setg(errp, TYPE_ASPEED_SMC ": 'dram' link not set");
1102c4e1f0b4SCédric Le Goater         return;
1103c4e1f0b4SCédric Le Goater     }
1104c4e1f0b4SCédric Le Goater 
1105d0180a3aSCédric Le Goater     address_space_init(&s->flash_as, &s->mmio_flash,
1106d0180a3aSCédric Le Goater                        TYPE_ASPEED_SMC ".dma-flash");
1107d0180a3aSCédric Le Goater     address_space_init(&s->dram_as, s->dram_mr,
1108d0180a3aSCédric Le Goater                        TYPE_ASPEED_SMC ".dma-dram");
1109c4e1f0b4SCédric Le Goater }
1110c4e1f0b4SCédric Le Goater 
11117c1c69bcSCédric Le Goater static void aspeed_smc_realize(DeviceState *dev, Error **errp)
11127c1c69bcSCédric Le Goater {
11137c1c69bcSCédric Le Goater     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
11147c1c69bcSCédric Le Goater     AspeedSMCState *s = ASPEED_SMC(dev);
111530b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_GET_CLASS(s);
11167c1c69bcSCédric Le Goater     int i;
1117924ed163SCédric Le Goater     hwaddr offset = 0;
11187c1c69bcSCédric Le Goater 
11197c1c69bcSCédric Le Goater     /* keep a copy under AspeedSMCState to speed up accesses */
112030b6852cSCédric Le Goater     s->r_conf = asc->r_conf;
112130b6852cSCédric Le Goater     s->r_ce_ctrl = asc->r_ce_ctrl;
112230b6852cSCédric Le Goater     s->r_ctrl0 = asc->r_ctrl0;
112330b6852cSCédric Le Goater     s->r_timings = asc->r_timings;
112430b6852cSCédric Le Goater     s->conf_enable_w0 = asc->conf_enable_w0;
11257c1c69bcSCédric Le Goater 
11267c1c69bcSCédric Le Goater     /* Enforce some real HW limits */
112730b6852cSCédric Le Goater     if (s->num_cs > asc->max_peripherals) {
112830b6852cSCédric Le Goater         aspeed_smc_error("num_cs cannot exceed: %d", asc->max_peripherals);
112930b6852cSCédric Le Goater         s->num_cs = asc->max_peripherals;
11307c1c69bcSCédric Le Goater     }
11317c1c69bcSCédric Le Goater 
1132c4e1f0b4SCédric Le Goater     /* DMA irq. Keep it first for the initialization in the SoC */
1133c4e1f0b4SCédric Le Goater     sysbus_init_irq(sbd, &s->irq);
1134c4e1f0b4SCédric Le Goater 
11357c1c69bcSCédric Le Goater     s->spi = ssi_create_bus(dev, "spi");
11367c1c69bcSCédric Le Goater 
11375ade579bSPhilippe Mathieu-Daudé     /* Setup cs_lines for peripherals */
11387c1c69bcSCédric Le Goater     s->cs_lines = g_new0(qemu_irq, s->num_cs);
11397c1c69bcSCédric Le Goater 
11407c1c69bcSCédric Le Goater     for (i = 0; i < s->num_cs; ++i) {
11417c1c69bcSCédric Le Goater         sysbus_init_irq(sbd, &s->cs_lines[i]);
11427c1c69bcSCédric Le Goater     }
11437c1c69bcSCédric Le Goater 
11442da95fd8SCédric Le Goater     /* The memory region for the controller registers */
11457c1c69bcSCédric Le Goater     memory_region_init_io(&s->mmio, OBJECT(s), &aspeed_smc_ops, s,
114630b6852cSCédric Le Goater                           TYPE_ASPEED_SMC, asc->nregs * 4);
11477c1c69bcSCédric Le Goater     sysbus_init_mmio(sbd, &s->mmio);
1148924ed163SCédric Le Goater 
1149924ed163SCédric Le Goater     /*
11502da95fd8SCédric Le Goater      * The container memory region representing the address space
11512da95fd8SCédric Le Goater      * window in which the flash modules are mapped. The size and
11522da95fd8SCédric Le Goater      * address depends on the SoC model and controller type.
1153924ed163SCédric Le Goater      */
1154924ed163SCédric Le Goater     memory_region_init_io(&s->mmio_flash, OBJECT(s),
1155d0180a3aSCédric Le Goater                           &aspeed_smc_flash_default_ops, s,
1156d0180a3aSCédric Le Goater                           TYPE_ASPEED_SMC ".flash",
115730b6852cSCédric Le Goater                           asc->flash_window_size);
1158d0180a3aSCédric Le Goater     memory_region_init_alias(&s->mmio_flash_alias, OBJECT(s),
1159d0180a3aSCédric Le Goater                              TYPE_ASPEED_SMC ".flash",
116030b6852cSCédric Le Goater                              &s->mmio_flash, 0, asc->flash_window_size);
1161e9c568dbSPhilippe Mathieu-Daudé     sysbus_init_mmio(sbd, &s->mmio_flash_alias);
1162924ed163SCédric Le Goater 
11632da95fd8SCédric Le Goater     /*
11645ade579bSPhilippe Mathieu-Daudé      * Let's create a sub memory region for each possible peripheral. All
11652da95fd8SCédric Le Goater      * have a configurable memory segment in the overall flash mapping
11662da95fd8SCédric Le Goater      * window of the controller but, there is not necessarily a flash
11672da95fd8SCédric Le Goater      * module behind to handle the memory accesses. This depends on
11682da95fd8SCédric Le Goater      * the board configuration.
11692da95fd8SCédric Le Goater      */
117030b6852cSCédric Le Goater     for (i = 0; i < asc->max_peripherals; ++i) {
1171924ed163SCédric Le Goater         AspeedSMCFlash *fl = &s->flashes[i];
1172924ed163SCédric Le Goater 
1173f75b5331SCédric Le Goater         if (!object_property_set_link(OBJECT(fl), "controller", OBJECT(s),
1174f75b5331SCédric Le Goater                                       errp)) {
1175f75b5331SCédric Le Goater             return;
1176f75b5331SCédric Le Goater         }
1177f75b5331SCédric Le Goater         if (!object_property_set_uint(OBJECT(fl), "cs", i, errp)) {
1178f75b5331SCédric Le Goater             return;
1179f75b5331SCédric Le Goater         }
1180f75b5331SCédric Le Goater         if (!sysbus_realize(SYS_BUS_DEVICE(fl), errp)) {
1181f75b5331SCédric Le Goater             return;
1182f75b5331SCédric Le Goater         }
1183924ed163SCédric Le Goater 
1184924ed163SCédric Le Goater         memory_region_add_subregion(&s->mmio_flash, offset, &fl->mmio);
11856bb55e79SCédric Le Goater         offset += asc->segments[i].size;
1186924ed163SCédric Le Goater     }
1187c4e1f0b4SCédric Le Goater 
1188c4e1f0b4SCédric Le Goater     /* DMA support */
118930b6852cSCédric Le Goater     if (aspeed_smc_has_dma(asc)) {
1190c4e1f0b4SCédric Le Goater         aspeed_smc_dma_setup(s, errp);
1191c4e1f0b4SCédric Le Goater     }
11927c1c69bcSCédric Le Goater }
11937c1c69bcSCédric Le Goater 
11947c1c69bcSCédric Le Goater static const VMStateDescription vmstate_aspeed_smc = {
11957c1c69bcSCédric Le Goater     .name = "aspeed.smc",
1196f95c4bffSCédric Le Goater     .version_id = 2,
1197f95c4bffSCédric Le Goater     .minimum_version_id = 2,
11987c1c69bcSCédric Le Goater     .fields = (VMStateField[]) {
11997c1c69bcSCédric Le Goater         VMSTATE_UINT32_ARRAY(regs, AspeedSMCState, ASPEED_SMC_R_MAX),
1200f95c4bffSCédric Le Goater         VMSTATE_UINT8(snoop_index, AspeedSMCState),
1201f95c4bffSCédric Le Goater         VMSTATE_UINT8(snoop_dummies, AspeedSMCState),
12027c1c69bcSCédric Le Goater         VMSTATE_END_OF_LIST()
12037c1c69bcSCédric Le Goater     }
12047c1c69bcSCédric Le Goater };
12057c1c69bcSCédric Le Goater 
12067c1c69bcSCédric Le Goater static Property aspeed_smc_properties[] = {
12077c1c69bcSCédric Le Goater     DEFINE_PROP_UINT32("num-cs", AspeedSMCState, num_cs, 1),
12085258c2a6SCédric Le Goater     DEFINE_PROP_BOOL("inject-failure", AspeedSMCState, inject_failure, false),
1209c4e1f0b4SCédric Le Goater     DEFINE_PROP_LINK("dram", AspeedSMCState, dram_mr,
1210c4e1f0b4SCédric Le Goater                      TYPE_MEMORY_REGION, MemoryRegion *),
12117c1c69bcSCédric Le Goater     DEFINE_PROP_END_OF_LIST(),
12127c1c69bcSCédric Le Goater };
12137c1c69bcSCédric Le Goater 
12147c1c69bcSCédric Le Goater static void aspeed_smc_class_init(ObjectClass *klass, void *data)
12157c1c69bcSCédric Le Goater {
12167c1c69bcSCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
12177c1c69bcSCédric Le Goater 
12187c1c69bcSCédric Le Goater     dc->realize = aspeed_smc_realize;
12197c1c69bcSCédric Le Goater     dc->reset = aspeed_smc_reset;
12204f67d30bSMarc-André Lureau     device_class_set_props(dc, aspeed_smc_properties);
12217c1c69bcSCédric Le Goater     dc->vmsd = &vmstate_aspeed_smc;
12227c1c69bcSCédric Le Goater }
12237c1c69bcSCédric Le Goater 
12247c1c69bcSCédric Le Goater static const TypeInfo aspeed_smc_info = {
12257c1c69bcSCédric Le Goater     .name           = TYPE_ASPEED_SMC,
12267c1c69bcSCédric Le Goater     .parent         = TYPE_SYS_BUS_DEVICE,
1227f75b5331SCédric Le Goater     .instance_init  = aspeed_smc_instance_init,
12287c1c69bcSCédric Le Goater     .instance_size  = sizeof(AspeedSMCState),
12297c1c69bcSCédric Le Goater     .class_size     = sizeof(AspeedSMCClass),
123030b6852cSCédric Le Goater     .class_init     = aspeed_smc_class_init,
12317c1c69bcSCédric Le Goater     .abstract       = true,
12327c1c69bcSCédric Le Goater };
12337c1c69bcSCédric Le Goater 
1234f75b5331SCédric Le Goater static void aspeed_smc_flash_realize(DeviceState *dev, Error **errp)
1235f75b5331SCédric Le Goater {
1236f75b5331SCédric Le Goater     AspeedSMCFlash *s = ASPEED_SMC_FLASH(dev);
1237f75b5331SCédric Le Goater     AspeedSMCClass *asc;
1238f75b5331SCédric Le Goater     g_autofree char *name = g_strdup_printf(TYPE_ASPEED_SMC_FLASH ".%d", s->cs);
1239f75b5331SCédric Le Goater 
1240f75b5331SCédric Le Goater     if (!s->controller) {
1241f75b5331SCédric Le Goater         error_setg(errp, TYPE_ASPEED_SMC_FLASH ": 'controller' link not set");
1242f75b5331SCédric Le Goater         return;
1243f75b5331SCédric Le Goater     }
1244f75b5331SCédric Le Goater 
1245f75b5331SCédric Le Goater     asc = ASPEED_SMC_GET_CLASS(s->controller);
1246f75b5331SCédric Le Goater 
1247f75b5331SCédric Le Goater     /*
1248f75b5331SCédric Le Goater      * Use the default segment value to size the memory region. This
1249f75b5331SCédric Le Goater      * can be changed by FW at runtime.
1250f75b5331SCédric Le Goater      */
1251f75b5331SCédric Le Goater     memory_region_init_io(&s->mmio, OBJECT(s), &aspeed_smc_flash_ops,
1252f75b5331SCédric Le Goater                           s, name, asc->segments[s->cs].size);
1253f75b5331SCédric Le Goater     sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->mmio);
1254f75b5331SCédric Le Goater }
1255f75b5331SCédric Le Goater 
1256f75b5331SCédric Le Goater static Property aspeed_smc_flash_properties[] = {
1257f75b5331SCédric Le Goater     DEFINE_PROP_UINT8("cs", AspeedSMCFlash, cs, 0),
1258f75b5331SCédric Le Goater     DEFINE_PROP_LINK("controller", AspeedSMCFlash, controller, TYPE_ASPEED_SMC,
1259f75b5331SCédric Le Goater                      AspeedSMCState *),
1260f75b5331SCédric Le Goater     DEFINE_PROP_END_OF_LIST(),
1261f75b5331SCédric Le Goater };
1262f75b5331SCédric Le Goater 
1263f75b5331SCédric Le Goater static void aspeed_smc_flash_class_init(ObjectClass *klass, void *data)
1264f75b5331SCédric Le Goater {
1265f75b5331SCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
1266f75b5331SCédric Le Goater 
1267f75b5331SCédric Le Goater     dc->desc = "Aspeed SMC Flash device region";
1268f75b5331SCédric Le Goater     dc->realize = aspeed_smc_flash_realize;
1269f75b5331SCédric Le Goater     device_class_set_props(dc, aspeed_smc_flash_properties);
1270f75b5331SCédric Le Goater }
1271f75b5331SCédric Le Goater 
1272f75b5331SCédric Le Goater static const TypeInfo aspeed_smc_flash_info = {
1273f75b5331SCédric Le Goater     .name           = TYPE_ASPEED_SMC_FLASH,
1274f75b5331SCédric Le Goater     .parent         = TYPE_SYS_BUS_DEVICE,
1275f75b5331SCédric Le Goater     .instance_size  = sizeof(AspeedSMCFlash),
1276f75b5331SCédric Le Goater     .class_init     = aspeed_smc_flash_class_init,
1277f75b5331SCédric Le Goater };
127830b6852cSCédric Le Goater 
127930b6852cSCédric Le Goater /*
128030b6852cSCédric Le Goater  * The Segment Registers of the AST2400 and AST2500 have a 8MB
128130b6852cSCédric Le Goater  * unit. The address range of a flash SPI peripheral is encoded with
128230b6852cSCédric Le Goater  * absolute addresses which should be part of the overall controller
128330b6852cSCédric Le Goater  * window.
128430b6852cSCédric Le Goater  */
128530b6852cSCédric Le Goater static uint32_t aspeed_smc_segment_to_reg(const AspeedSMCState *s,
128630b6852cSCédric Le Goater                                           const AspeedSegments *seg)
128730b6852cSCédric Le Goater {
128830b6852cSCédric Le Goater     uint32_t reg = 0;
128930b6852cSCédric Le Goater     reg |= ((seg->addr >> 23) & SEG_START_MASK) << SEG_START_SHIFT;
129030b6852cSCédric Le Goater     reg |= (((seg->addr + seg->size) >> 23) & SEG_END_MASK) << SEG_END_SHIFT;
129130b6852cSCédric Le Goater     return reg;
129230b6852cSCédric Le Goater }
129330b6852cSCédric Le Goater 
129430b6852cSCédric Le Goater static void aspeed_smc_reg_to_segment(const AspeedSMCState *s,
129530b6852cSCédric Le Goater                                       uint32_t reg, AspeedSegments *seg)
129630b6852cSCédric Le Goater {
129730b6852cSCédric Le Goater     seg->addr = ((reg >> SEG_START_SHIFT) & SEG_START_MASK) << 23;
129830b6852cSCédric Le Goater     seg->size = (((reg >> SEG_END_SHIFT) & SEG_END_MASK) << 23) - seg->addr;
129930b6852cSCédric Le Goater }
130030b6852cSCédric Le Goater 
130130b6852cSCédric Le Goater static const AspeedSegments aspeed_2400_smc_segments[] = {
130230b6852cSCédric Le Goater     { 0x10000000, 32 * MiB },
130330b6852cSCédric Le Goater };
130430b6852cSCédric Le Goater 
130530b6852cSCédric Le Goater static void aspeed_2400_smc_class_init(ObjectClass *klass, void *data)
130630b6852cSCédric Le Goater {
130730b6852cSCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
130830b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_CLASS(klass);
130930b6852cSCédric Le Goater 
131030b6852cSCédric Le Goater     dc->desc               = "Aspeed 2400 SMC Controller";
131130b6852cSCédric Le Goater     asc->r_conf            = R_CONF;
131230b6852cSCédric Le Goater     asc->r_ce_ctrl         = R_CE_CTRL;
131330b6852cSCédric Le Goater     asc->r_ctrl0           = R_CTRL0;
131430b6852cSCédric Le Goater     asc->r_timings         = R_TIMINGS;
131530b6852cSCédric Le Goater     asc->nregs_timings     = 1;
131630b6852cSCédric Le Goater     asc->conf_enable_w0    = CONF_ENABLE_W0;
131730b6852cSCédric Le Goater     asc->max_peripherals   = 1;
131830b6852cSCédric Le Goater     asc->segments          = aspeed_2400_smc_segments;
131930b6852cSCédric Le Goater     asc->flash_window_base = 0x10000000;
132030b6852cSCédric Le Goater     asc->flash_window_size = 0x6000000;
132130b6852cSCédric Le Goater     asc->features          = 0x0;
132230b6852cSCédric Le Goater     asc->nregs             = ASPEED_SMC_R_SMC_MAX;
132330b6852cSCédric Le Goater     asc->segment_to_reg    = aspeed_smc_segment_to_reg;
132430b6852cSCédric Le Goater     asc->reg_to_segment    = aspeed_smc_reg_to_segment;
132530b6852cSCédric Le Goater     asc->dma_ctrl          = aspeed_smc_dma_ctrl;
132630b6852cSCédric Le Goater }
132730b6852cSCédric Le Goater 
132830b6852cSCédric Le Goater static const TypeInfo aspeed_2400_smc_info = {
132930b6852cSCédric Le Goater     .name =  "aspeed.smc-ast2400",
133030b6852cSCédric Le Goater     .parent = TYPE_ASPEED_SMC,
133130b6852cSCédric Le Goater     .class_init = aspeed_2400_smc_class_init,
133230b6852cSCédric Le Goater };
133330b6852cSCédric Le Goater 
133471255c48SCédric Le Goater static const uint32_t aspeed_2400_fmc_resets[ASPEED_SMC_R_MAX] = {
133571255c48SCédric Le Goater     /*
133671255c48SCédric Le Goater      * CE0 and CE1 types are HW strapped in SCU70. Do it here to
133771255c48SCédric Le Goater      * simplify the model.
133871255c48SCédric Le Goater      */
133971255c48SCédric Le Goater     [R_CONF] = CONF_FLASH_TYPE_SPI << CONF_FLASH_TYPE0,
134071255c48SCédric Le Goater };
134171255c48SCédric Le Goater 
134230b6852cSCédric Le Goater static const AspeedSegments aspeed_2400_fmc_segments[] = {
134330b6852cSCédric Le Goater     { 0x20000000, 64 * MiB }, /* start address is readonly */
134430b6852cSCédric Le Goater     { 0x24000000, 32 * MiB },
134530b6852cSCédric Le Goater     { 0x26000000, 32 * MiB },
134630b6852cSCédric Le Goater     { 0x28000000, 32 * MiB },
134730b6852cSCédric Le Goater     { 0x2A000000, 32 * MiB }
134830b6852cSCédric Le Goater };
134930b6852cSCédric Le Goater 
135030b6852cSCédric Le Goater static void aspeed_2400_fmc_class_init(ObjectClass *klass, void *data)
135130b6852cSCédric Le Goater {
135230b6852cSCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
135330b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_CLASS(klass);
135430b6852cSCédric Le Goater 
135530b6852cSCédric Le Goater     dc->desc               = "Aspeed 2400 FMC Controller";
135630b6852cSCédric Le Goater     asc->r_conf            = R_CONF;
135730b6852cSCédric Le Goater     asc->r_ce_ctrl         = R_CE_CTRL;
135830b6852cSCédric Le Goater     asc->r_ctrl0           = R_CTRL0;
135930b6852cSCédric Le Goater     asc->r_timings         = R_TIMINGS;
136030b6852cSCédric Le Goater     asc->nregs_timings     = 1;
136130b6852cSCédric Le Goater     asc->conf_enable_w0    = CONF_ENABLE_W0;
136230b6852cSCédric Le Goater     asc->max_peripherals   = 5;
136330b6852cSCédric Le Goater     asc->segments          = aspeed_2400_fmc_segments;
136471255c48SCédric Le Goater     asc->resets            = aspeed_2400_fmc_resets;
136530b6852cSCédric Le Goater     asc->flash_window_base = 0x20000000;
136630b6852cSCédric Le Goater     asc->flash_window_size = 0x10000000;
136730b6852cSCédric Le Goater     asc->features          = ASPEED_SMC_FEATURE_DMA;
136830b6852cSCédric Le Goater     asc->dma_flash_mask    = 0x0FFFFFFC;
136930b6852cSCédric Le Goater     asc->dma_dram_mask     = 0x1FFFFFFC;
137030b6852cSCédric Le Goater     asc->nregs             = ASPEED_SMC_R_MAX;
137130b6852cSCédric Le Goater     asc->segment_to_reg    = aspeed_smc_segment_to_reg;
137230b6852cSCédric Le Goater     asc->reg_to_segment    = aspeed_smc_reg_to_segment;
137330b6852cSCédric Le Goater     asc->dma_ctrl          = aspeed_smc_dma_ctrl;
137430b6852cSCédric Le Goater }
137530b6852cSCédric Le Goater 
137630b6852cSCédric Le Goater static const TypeInfo aspeed_2400_fmc_info = {
137730b6852cSCédric Le Goater     .name =  "aspeed.fmc-ast2400",
137830b6852cSCédric Le Goater     .parent = TYPE_ASPEED_SMC,
137930b6852cSCédric Le Goater     .class_init = aspeed_2400_fmc_class_init,
138030b6852cSCédric Le Goater };
138130b6852cSCédric Le Goater 
138230b6852cSCédric Le Goater static const AspeedSegments aspeed_2400_spi1_segments[] = {
138330b6852cSCédric Le Goater     { 0x30000000, 64 * MiB },
138430b6852cSCédric Le Goater };
138530b6852cSCédric Le Goater 
1386a779e37cSCédric Le Goater static int aspeed_2400_spi1_addr_width(const AspeedSMCState *s)
1387a779e37cSCédric Le Goater {
1388a779e37cSCédric Le Goater     return s->regs[R_SPI_CTRL0] & CTRL_AST2400_SPI_4BYTE ? 4 : 3;
1389a779e37cSCédric Le Goater }
1390a779e37cSCédric Le Goater 
139130b6852cSCédric Le Goater static void aspeed_2400_spi1_class_init(ObjectClass *klass, void *data)
139230b6852cSCédric Le Goater {
139330b6852cSCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
139430b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_CLASS(klass);
139530b6852cSCédric Le Goater 
139630b6852cSCédric Le Goater     dc->desc               = "Aspeed 2400 SPI1 Controller";
139730b6852cSCédric Le Goater     asc->r_conf            = R_SPI_CONF;
139830b6852cSCédric Le Goater     asc->r_ce_ctrl         = 0xff;
139930b6852cSCédric Le Goater     asc->r_ctrl0           = R_SPI_CTRL0;
140030b6852cSCédric Le Goater     asc->r_timings         = R_SPI_TIMINGS;
140130b6852cSCédric Le Goater     asc->nregs_timings     = 1;
140230b6852cSCédric Le Goater     asc->conf_enable_w0    = SPI_CONF_ENABLE_W0;
140330b6852cSCédric Le Goater     asc->max_peripherals   = 1;
140430b6852cSCédric Le Goater     asc->segments          = aspeed_2400_spi1_segments;
140530b6852cSCédric Le Goater     asc->flash_window_base = 0x30000000;
140630b6852cSCédric Le Goater     asc->flash_window_size = 0x10000000;
140730b6852cSCédric Le Goater     asc->features          = 0x0;
140830b6852cSCédric Le Goater     asc->nregs             = ASPEED_SMC_R_SPI_MAX;
140930b6852cSCédric Le Goater     asc->segment_to_reg    = aspeed_smc_segment_to_reg;
141030b6852cSCédric Le Goater     asc->reg_to_segment    = aspeed_smc_reg_to_segment;
141130b6852cSCédric Le Goater     asc->dma_ctrl          = aspeed_smc_dma_ctrl;
1412a779e37cSCédric Le Goater     asc->addr_width        = aspeed_2400_spi1_addr_width;
141330b6852cSCédric Le Goater }
141430b6852cSCédric Le Goater 
141530b6852cSCédric Le Goater static const TypeInfo aspeed_2400_spi1_info = {
141630b6852cSCédric Le Goater     .name =  "aspeed.spi1-ast2400",
141730b6852cSCédric Le Goater     .parent = TYPE_ASPEED_SMC,
141830b6852cSCédric Le Goater     .class_init = aspeed_2400_spi1_class_init,
141930b6852cSCédric Le Goater };
142030b6852cSCédric Le Goater 
142171255c48SCédric Le Goater static const uint32_t aspeed_2500_fmc_resets[ASPEED_SMC_R_MAX] = {
142271255c48SCédric Le Goater     [R_CONF] = (CONF_FLASH_TYPE_SPI << CONF_FLASH_TYPE0 |
142371255c48SCédric Le Goater                 CONF_FLASH_TYPE_SPI << CONF_FLASH_TYPE1),
142471255c48SCédric Le Goater };
142571255c48SCédric Le Goater 
142630b6852cSCédric Le Goater static const AspeedSegments aspeed_2500_fmc_segments[] = {
142730b6852cSCédric Le Goater     { 0x20000000, 128 * MiB }, /* start address is readonly */
142830b6852cSCédric Le Goater     { 0x28000000,  32 * MiB },
142930b6852cSCédric Le Goater     { 0x2A000000,  32 * MiB },
143030b6852cSCédric Le Goater };
143130b6852cSCédric Le Goater 
143230b6852cSCédric Le Goater static void aspeed_2500_fmc_class_init(ObjectClass *klass, void *data)
143330b6852cSCédric Le Goater {
143430b6852cSCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
143530b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_CLASS(klass);
143630b6852cSCédric Le Goater 
143730b6852cSCédric Le Goater     dc->desc               = "Aspeed 2600 FMC Controller";
143830b6852cSCédric Le Goater     asc->r_conf            = R_CONF;
143930b6852cSCédric Le Goater     asc->r_ce_ctrl         = R_CE_CTRL;
144030b6852cSCédric Le Goater     asc->r_ctrl0           = R_CTRL0;
144130b6852cSCédric Le Goater     asc->r_timings         = R_TIMINGS;
144230b6852cSCédric Le Goater     asc->nregs_timings     = 1;
144330b6852cSCédric Le Goater     asc->conf_enable_w0    = CONF_ENABLE_W0;
144430b6852cSCédric Le Goater     asc->max_peripherals   = 3;
144530b6852cSCédric Le Goater     asc->segments          = aspeed_2500_fmc_segments;
144671255c48SCédric Le Goater     asc->resets            = aspeed_2500_fmc_resets;
144730b6852cSCédric Le Goater     asc->flash_window_base = 0x20000000;
144830b6852cSCédric Le Goater     asc->flash_window_size = 0x10000000;
144930b6852cSCédric Le Goater     asc->features          = ASPEED_SMC_FEATURE_DMA;
145030b6852cSCédric Le Goater     asc->dma_flash_mask    = 0x0FFFFFFC;
145130b6852cSCédric Le Goater     asc->dma_dram_mask     = 0x3FFFFFFC;
145230b6852cSCédric Le Goater     asc->nregs             = ASPEED_SMC_R_MAX;
145330b6852cSCédric Le Goater     asc->segment_to_reg    = aspeed_smc_segment_to_reg;
145430b6852cSCédric Le Goater     asc->reg_to_segment    = aspeed_smc_reg_to_segment;
145530b6852cSCédric Le Goater     asc->dma_ctrl          = aspeed_smc_dma_ctrl;
145630b6852cSCédric Le Goater }
145730b6852cSCédric Le Goater 
145830b6852cSCédric Le Goater static const TypeInfo aspeed_2500_fmc_info = {
145930b6852cSCédric Le Goater     .name =  "aspeed.fmc-ast2500",
146030b6852cSCédric Le Goater     .parent = TYPE_ASPEED_SMC,
146130b6852cSCédric Le Goater     .class_init = aspeed_2500_fmc_class_init,
146230b6852cSCédric Le Goater };
146330b6852cSCédric Le Goater 
146430b6852cSCédric Le Goater static const AspeedSegments aspeed_2500_spi1_segments[] = {
146530b6852cSCédric Le Goater     { 0x30000000, 32 * MiB }, /* start address is readonly */
146630b6852cSCédric Le Goater     { 0x32000000, 96 * MiB }, /* end address is readonly */
146730b6852cSCédric Le Goater };
146830b6852cSCédric Le Goater 
146930b6852cSCédric Le Goater static void aspeed_2500_spi1_class_init(ObjectClass *klass, void *data)
147030b6852cSCédric Le Goater {
147130b6852cSCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
147230b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_CLASS(klass);
147330b6852cSCédric Le Goater 
147430b6852cSCédric Le Goater     dc->desc               = "Aspeed 2600 SPI1 Controller";
147530b6852cSCédric Le Goater     asc->r_conf            = R_CONF;
147630b6852cSCédric Le Goater     asc->r_ce_ctrl         = R_CE_CTRL;
147730b6852cSCédric Le Goater     asc->r_ctrl0           = R_CTRL0;
147830b6852cSCédric Le Goater     asc->r_timings         = R_TIMINGS;
147930b6852cSCédric Le Goater     asc->nregs_timings     = 1;
148030b6852cSCédric Le Goater     asc->conf_enable_w0    = CONF_ENABLE_W0;
148130b6852cSCédric Le Goater     asc->max_peripherals   = 2;
148230b6852cSCédric Le Goater     asc->segments          = aspeed_2500_spi1_segments;
148330b6852cSCédric Le Goater     asc->flash_window_base = 0x30000000;
148430b6852cSCédric Le Goater     asc->flash_window_size = 0x8000000;
148530b6852cSCédric Le Goater     asc->features          = 0x0;
148630b6852cSCédric Le Goater     asc->nregs             = ASPEED_SMC_R_MAX;
148730b6852cSCédric Le Goater     asc->segment_to_reg    = aspeed_smc_segment_to_reg;
148830b6852cSCédric Le Goater     asc->reg_to_segment    = aspeed_smc_reg_to_segment;
148930b6852cSCédric Le Goater     asc->dma_ctrl          = aspeed_smc_dma_ctrl;
149030b6852cSCédric Le Goater }
149130b6852cSCédric Le Goater 
149230b6852cSCédric Le Goater static const TypeInfo aspeed_2500_spi1_info = {
149330b6852cSCédric Le Goater     .name =  "aspeed.spi1-ast2500",
149430b6852cSCédric Le Goater     .parent = TYPE_ASPEED_SMC,
149530b6852cSCédric Le Goater     .class_init = aspeed_2500_spi1_class_init,
149630b6852cSCédric Le Goater };
149730b6852cSCédric Le Goater 
149830b6852cSCédric Le Goater static const AspeedSegments aspeed_2500_spi2_segments[] = {
149930b6852cSCédric Le Goater     { 0x38000000, 32 * MiB }, /* start address is readonly */
150030b6852cSCédric Le Goater     { 0x3A000000, 96 * MiB }, /* end address is readonly */
150130b6852cSCédric Le Goater };
150230b6852cSCédric Le Goater 
150330b6852cSCédric Le Goater static void aspeed_2500_spi2_class_init(ObjectClass *klass, void *data)
150430b6852cSCédric Le Goater {
150530b6852cSCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
150630b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_CLASS(klass);
150730b6852cSCédric Le Goater 
150830b6852cSCédric Le Goater     dc->desc               = "Aspeed 2600 SPI2 Controller";
150930b6852cSCédric Le Goater     asc->r_conf            = R_CONF;
151030b6852cSCédric Le Goater     asc->r_ce_ctrl         = R_CE_CTRL;
151130b6852cSCédric Le Goater     asc->r_ctrl0           = R_CTRL0;
151230b6852cSCédric Le Goater     asc->r_timings         = R_TIMINGS;
151330b6852cSCédric Le Goater     asc->nregs_timings     = 1;
151430b6852cSCédric Le Goater     asc->conf_enable_w0    = CONF_ENABLE_W0;
151530b6852cSCédric Le Goater     asc->max_peripherals   = 2;
151630b6852cSCédric Le Goater     asc->segments          = aspeed_2500_spi2_segments;
151730b6852cSCédric Le Goater     asc->flash_window_base = 0x38000000;
151830b6852cSCédric Le Goater     asc->flash_window_size = 0x8000000;
151930b6852cSCédric Le Goater     asc->features          = 0x0;
152030b6852cSCédric Le Goater     asc->nregs             = ASPEED_SMC_R_MAX;
152130b6852cSCédric Le Goater     asc->segment_to_reg    = aspeed_smc_segment_to_reg;
152230b6852cSCédric Le Goater     asc->reg_to_segment    = aspeed_smc_reg_to_segment;
152330b6852cSCédric Le Goater     asc->dma_ctrl          = aspeed_smc_dma_ctrl;
152430b6852cSCédric Le Goater }
152530b6852cSCédric Le Goater 
152630b6852cSCédric Le Goater static const TypeInfo aspeed_2500_spi2_info = {
152730b6852cSCédric Le Goater     .name =  "aspeed.spi2-ast2500",
152830b6852cSCédric Le Goater     .parent = TYPE_ASPEED_SMC,
152930b6852cSCédric Le Goater     .class_init = aspeed_2500_spi2_class_init,
153030b6852cSCédric Le Goater };
153130b6852cSCédric Le Goater 
153230b6852cSCédric Le Goater /*
153330b6852cSCédric Le Goater  * The Segment Registers of the AST2600 have a 1MB unit. The address
153430b6852cSCédric Le Goater  * range of a flash SPI peripheral is encoded with offsets in the overall
153530b6852cSCédric Le Goater  * controller window. The previous SoC AST2400 and AST2500 used
153630b6852cSCédric Le Goater  * absolute addresses. Only bits [27:20] are relevant and the end
153730b6852cSCédric Le Goater  * address is an upper bound limit.
153830b6852cSCédric Le Goater  */
153930b6852cSCédric Le Goater #define AST2600_SEG_ADDR_MASK 0x0ff00000
154030b6852cSCédric Le Goater 
154130b6852cSCédric Le Goater static uint32_t aspeed_2600_smc_segment_to_reg(const AspeedSMCState *s,
154230b6852cSCédric Le Goater                                                const AspeedSegments *seg)
154330b6852cSCédric Le Goater {
154430b6852cSCédric Le Goater     uint32_t reg = 0;
154530b6852cSCédric Le Goater 
154630b6852cSCédric Le Goater     /* Disabled segments have a nil register */
154730b6852cSCédric Le Goater     if (!seg->size) {
154830b6852cSCédric Le Goater         return 0;
154930b6852cSCédric Le Goater     }
155030b6852cSCédric Le Goater 
155130b6852cSCédric Le Goater     reg |= (seg->addr & AST2600_SEG_ADDR_MASK) >> 16; /* start offset */
155230b6852cSCédric Le Goater     reg |= (seg->addr + seg->size - 1) & AST2600_SEG_ADDR_MASK; /* end offset */
155330b6852cSCédric Le Goater     return reg;
155430b6852cSCédric Le Goater }
155530b6852cSCédric Le Goater 
155630b6852cSCédric Le Goater static void aspeed_2600_smc_reg_to_segment(const AspeedSMCState *s,
155730b6852cSCédric Le Goater                                            uint32_t reg, AspeedSegments *seg)
155830b6852cSCédric Le Goater {
155930b6852cSCédric Le Goater     uint32_t start_offset = (reg << 16) & AST2600_SEG_ADDR_MASK;
156030b6852cSCédric Le Goater     uint32_t end_offset = reg & AST2600_SEG_ADDR_MASK;
156130b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_GET_CLASS(s);
156230b6852cSCédric Le Goater 
156330b6852cSCédric Le Goater     if (reg) {
156430b6852cSCédric Le Goater         seg->addr = asc->flash_window_base + start_offset;
156530b6852cSCédric Le Goater         seg->size = end_offset + MiB - start_offset;
156630b6852cSCédric Le Goater     } else {
156730b6852cSCédric Le Goater         seg->addr = asc->flash_window_base;
156830b6852cSCédric Le Goater         seg->size = 0;
156930b6852cSCédric Le Goater     }
157030b6852cSCédric Le Goater }
157130b6852cSCédric Le Goater 
157271255c48SCédric Le Goater static const uint32_t aspeed_2600_fmc_resets[ASPEED_SMC_R_MAX] = {
157371255c48SCédric Le Goater     [R_CONF] = (CONF_FLASH_TYPE_SPI << CONF_FLASH_TYPE0 |
157471255c48SCédric Le Goater                 CONF_FLASH_TYPE_SPI << CONF_FLASH_TYPE1 |
157571255c48SCédric Le Goater                 CONF_FLASH_TYPE_SPI << CONF_FLASH_TYPE2),
157671255c48SCédric Le Goater };
157771255c48SCédric Le Goater 
157830b6852cSCédric Le Goater static const AspeedSegments aspeed_2600_fmc_segments[] = {
157930b6852cSCédric Le Goater     { 0x0, 128 * MiB }, /* start address is readonly */
158030b6852cSCédric Le Goater     { 128 * MiB, 128 * MiB }, /* default is disabled but needed for -kernel */
158130b6852cSCédric Le Goater     { 0x0, 0 }, /* disabled */
158230b6852cSCédric Le Goater };
158330b6852cSCédric Le Goater 
158430b6852cSCédric Le Goater static void aspeed_2600_fmc_class_init(ObjectClass *klass, void *data)
158530b6852cSCédric Le Goater {
158630b6852cSCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
158730b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_CLASS(klass);
158830b6852cSCédric Le Goater 
158930b6852cSCédric Le Goater     dc->desc               = "Aspeed 2600 FMC Controller";
159030b6852cSCédric Le Goater     asc->r_conf            = R_CONF;
159130b6852cSCédric Le Goater     asc->r_ce_ctrl         = R_CE_CTRL;
159230b6852cSCédric Le Goater     asc->r_ctrl0           = R_CTRL0;
159330b6852cSCédric Le Goater     asc->r_timings         = R_TIMINGS;
159430b6852cSCédric Le Goater     asc->nregs_timings     = 1;
159530b6852cSCédric Le Goater     asc->conf_enable_w0    = CONF_ENABLE_W0;
159630b6852cSCédric Le Goater     asc->max_peripherals   = 3;
159730b6852cSCédric Le Goater     asc->segments          = aspeed_2600_fmc_segments;
159871255c48SCédric Le Goater     asc->resets            = aspeed_2600_fmc_resets;
159930b6852cSCédric Le Goater     asc->flash_window_base = 0x20000000;
160030b6852cSCédric Le Goater     asc->flash_window_size = 0x10000000;
160130b6852cSCédric Le Goater     asc->features          = ASPEED_SMC_FEATURE_DMA |
160230b6852cSCédric Le Goater                              ASPEED_SMC_FEATURE_WDT_CONTROL;
160330b6852cSCédric Le Goater     asc->dma_flash_mask    = 0x0FFFFFFC;
160430b6852cSCédric Le Goater     asc->dma_dram_mask     = 0x3FFFFFFC;
160530b6852cSCédric Le Goater     asc->nregs             = ASPEED_SMC_R_MAX;
160630b6852cSCédric Le Goater     asc->segment_to_reg    = aspeed_2600_smc_segment_to_reg;
160730b6852cSCédric Le Goater     asc->reg_to_segment    = aspeed_2600_smc_reg_to_segment;
160830b6852cSCédric Le Goater     asc->dma_ctrl          = aspeed_2600_smc_dma_ctrl;
160930b6852cSCédric Le Goater }
161030b6852cSCédric Le Goater 
161130b6852cSCédric Le Goater static const TypeInfo aspeed_2600_fmc_info = {
161230b6852cSCédric Le Goater     .name =  "aspeed.fmc-ast2600",
161330b6852cSCédric Le Goater     .parent = TYPE_ASPEED_SMC,
161430b6852cSCédric Le Goater     .class_init = aspeed_2600_fmc_class_init,
161530b6852cSCédric Le Goater };
161630b6852cSCédric Le Goater 
161730b6852cSCédric Le Goater static const AspeedSegments aspeed_2600_spi1_segments[] = {
161830b6852cSCédric Le Goater     { 0x0, 128 * MiB }, /* start address is readonly */
161930b6852cSCédric Le Goater     { 0x0, 0 }, /* disabled */
162030b6852cSCédric Le Goater };
162130b6852cSCédric Le Goater 
162230b6852cSCédric Le Goater static void aspeed_2600_spi1_class_init(ObjectClass *klass, void *data)
162330b6852cSCédric Le Goater {
162430b6852cSCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
162530b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_CLASS(klass);
162630b6852cSCédric Le Goater 
162730b6852cSCédric Le Goater     dc->desc               = "Aspeed 2600 SPI1 Controller";
162830b6852cSCédric Le Goater     asc->r_conf            = R_CONF;
162930b6852cSCédric Le Goater     asc->r_ce_ctrl         = R_CE_CTRL;
163030b6852cSCédric Le Goater     asc->r_ctrl0           = R_CTRL0;
163130b6852cSCédric Le Goater     asc->r_timings         = R_TIMINGS;
163230b6852cSCédric Le Goater     asc->nregs_timings     = 2;
163330b6852cSCédric Le Goater     asc->conf_enable_w0    = CONF_ENABLE_W0;
163430b6852cSCédric Le Goater     asc->max_peripherals   = 2;
163530b6852cSCédric Le Goater     asc->segments          = aspeed_2600_spi1_segments;
163630b6852cSCédric Le Goater     asc->flash_window_base = 0x30000000;
163730b6852cSCédric Le Goater     asc->flash_window_size = 0x10000000;
163830b6852cSCédric Le Goater     asc->features          = ASPEED_SMC_FEATURE_DMA |
163930b6852cSCédric Le Goater                              ASPEED_SMC_FEATURE_DMA_GRANT;
164030b6852cSCédric Le Goater     asc->dma_flash_mask    = 0x0FFFFFFC;
164130b6852cSCédric Le Goater     asc->dma_dram_mask     = 0x3FFFFFFC;
164230b6852cSCédric Le Goater     asc->nregs             = ASPEED_SMC_R_MAX;
164330b6852cSCédric Le Goater     asc->segment_to_reg    = aspeed_2600_smc_segment_to_reg;
164430b6852cSCédric Le Goater     asc->reg_to_segment    = aspeed_2600_smc_reg_to_segment;
164530b6852cSCédric Le Goater     asc->dma_ctrl          = aspeed_2600_smc_dma_ctrl;
164630b6852cSCédric Le Goater }
164730b6852cSCédric Le Goater 
164830b6852cSCédric Le Goater static const TypeInfo aspeed_2600_spi1_info = {
164930b6852cSCédric Le Goater     .name =  "aspeed.spi1-ast2600",
165030b6852cSCédric Le Goater     .parent = TYPE_ASPEED_SMC,
165130b6852cSCédric Le Goater     .class_init = aspeed_2600_spi1_class_init,
165230b6852cSCédric Le Goater };
165330b6852cSCédric Le Goater 
165430b6852cSCédric Le Goater static const AspeedSegments aspeed_2600_spi2_segments[] = {
165530b6852cSCédric Le Goater     { 0x0, 128 * MiB }, /* start address is readonly */
165630b6852cSCédric Le Goater     { 0x0, 0 }, /* disabled */
165730b6852cSCédric Le Goater     { 0x0, 0 }, /* disabled */
165830b6852cSCédric Le Goater };
165930b6852cSCédric Le Goater 
166030b6852cSCédric Le Goater static void aspeed_2600_spi2_class_init(ObjectClass *klass, void *data)
166130b6852cSCédric Le Goater {
166230b6852cSCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
166330b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_CLASS(klass);
166430b6852cSCédric Le Goater 
166530b6852cSCédric Le Goater     dc->desc               = "Aspeed 2600 SPI2 Controller";
166630b6852cSCédric Le Goater     asc->r_conf            = R_CONF;
166730b6852cSCédric Le Goater     asc->r_ce_ctrl         = R_CE_CTRL;
166830b6852cSCédric Le Goater     asc->r_ctrl0           = R_CTRL0;
166930b6852cSCédric Le Goater     asc->r_timings         = R_TIMINGS;
167030b6852cSCédric Le Goater     asc->nregs_timings     = 3;
167130b6852cSCédric Le Goater     asc->conf_enable_w0    = CONF_ENABLE_W0;
167230b6852cSCédric Le Goater     asc->max_peripherals   = 3;
167330b6852cSCédric Le Goater     asc->segments          = aspeed_2600_spi2_segments;
167430b6852cSCédric Le Goater     asc->flash_window_base = 0x50000000;
167530b6852cSCédric Le Goater     asc->flash_window_size = 0x10000000;
167630b6852cSCédric Le Goater     asc->features          = ASPEED_SMC_FEATURE_DMA |
167730b6852cSCédric Le Goater                              ASPEED_SMC_FEATURE_DMA_GRANT;
167830b6852cSCédric Le Goater     asc->dma_flash_mask    = 0x0FFFFFFC;
167930b6852cSCédric Le Goater     asc->dma_dram_mask     = 0x3FFFFFFC;
168030b6852cSCédric Le Goater     asc->nregs             = ASPEED_SMC_R_MAX;
168130b6852cSCédric Le Goater     asc->segment_to_reg    = aspeed_2600_smc_segment_to_reg;
168230b6852cSCédric Le Goater     asc->reg_to_segment    = aspeed_2600_smc_reg_to_segment;
168330b6852cSCédric Le Goater     asc->dma_ctrl          = aspeed_2600_smc_dma_ctrl;
168430b6852cSCédric Le Goater }
168530b6852cSCédric Le Goater 
168630b6852cSCédric Le Goater static const TypeInfo aspeed_2600_spi2_info = {
168730b6852cSCédric Le Goater     .name =  "aspeed.spi2-ast2600",
168830b6852cSCédric Le Goater     .parent = TYPE_ASPEED_SMC,
168930b6852cSCédric Le Goater     .class_init = aspeed_2600_spi2_class_init,
169030b6852cSCédric Le Goater };
169130b6852cSCédric Le Goater 
16927c1c69bcSCédric Le Goater static void aspeed_smc_register_types(void)
16937c1c69bcSCédric Le Goater {
1694f75b5331SCédric Le Goater     type_register_static(&aspeed_smc_flash_info);
16957c1c69bcSCédric Le Goater     type_register_static(&aspeed_smc_info);
169630b6852cSCédric Le Goater     type_register_static(&aspeed_2400_smc_info);
169730b6852cSCédric Le Goater     type_register_static(&aspeed_2400_fmc_info);
169830b6852cSCédric Le Goater     type_register_static(&aspeed_2400_spi1_info);
169930b6852cSCédric Le Goater     type_register_static(&aspeed_2500_fmc_info);
170030b6852cSCédric Le Goater     type_register_static(&aspeed_2500_spi1_info);
170130b6852cSCédric Le Goater     type_register_static(&aspeed_2500_spi2_info);
170230b6852cSCédric Le Goater     type_register_static(&aspeed_2600_fmc_info);
170330b6852cSCédric Le Goater     type_register_static(&aspeed_2600_spi1_info);
170430b6852cSCédric Le Goater     type_register_static(&aspeed_2600_spi2_info);
17057c1c69bcSCédric Le Goater }
17067c1c69bcSCédric Le Goater 
17077c1c69bcSCédric Le Goater type_init(aspeed_smc_register_types)
1708