xref: /qemu/hw/ssi/aspeed_smc.c (revision 30b6852ce4c3398c54fc6f6c7ff5ccbf8c15cf4f)
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  */
184*30b6852cSCédric Le Goater #define DMA_DRAM_ADDR(asc, val)   ((val) & (asc)->dma_dram_mask)
185*30b6852cSCé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  */
199*30b6852cSCédric Le Goater static const AspeedSegments aspeed_2400_fmc_segments[];
200*30b6852cSCédric Le Goater static const AspeedSegments aspeed_2400_spi1_segments[];
201*30b6852cSCédric Le Goater static const AspeedSegments aspeed_2500_fmc_segments[];
202*30b6852cSCédric Le Goater static const AspeedSegments aspeed_2500_spi1_segments[];
203*30b6852cSCédric Le Goater static const AspeedSegments aspeed_2500_spi2_segments[];
204*30b6852cSCédric Le Goater static const AspeedSegments aspeed_2600_fmc_segments[];
2051769a70eSCédric Le Goater 
2061c5ee69dSCédric Le Goater #define ASPEED_SMC_FEATURE_DMA       0x1
2071769a70eSCédric Le Goater #define ASPEED_SMC_FEATURE_DMA_GRANT 0x2
20845a904afSCédric Le Goater #define ASPEED_SMC_FEATURE_WDT_CONTROL 0x4
2091c5ee69dSCédric Le Goater 
210*30b6852cSCédric Le Goater static inline bool aspeed_smc_has_dma(const AspeedSMCClass *asc)
2111c5ee69dSCédric Le Goater {
212*30b6852cSCédric Le Goater     return !!(asc->features & ASPEED_SMC_FEATURE_DMA);
2131c5ee69dSCédric Le Goater }
214bcaa8dddSCédric Le Goater 
215*30b6852cSCédric Le Goater static inline bool aspeed_smc_has_wdt_control(const AspeedSMCClass *asc)
21645a904afSCédric Le Goater {
217*30b6852cSCédric Le Goater     return !!(asc->features & ASPEED_SMC_FEATURE_WDT_CONTROL);
218bcaa8dddSCédric Le Goater }
219bcaa8dddSCédric Le Goater 
22032c54bd0SCédric Le Goater #define aspeed_smc_error(fmt, ...)                                      \
22132c54bd0SCédric Le Goater     qemu_log_mask(LOG_GUEST_ERROR, "%s: " fmt "\n", __func__, ## __VA_ARGS__)
22232c54bd0SCédric Le Goater 
223a03cb1daSCédric Le Goater static bool aspeed_smc_flash_overlap(const AspeedSMCState *s,
224a03cb1daSCédric Le Goater                                      const AspeedSegments *new,
225a03cb1daSCédric Le Goater                                      int cs)
226a03cb1daSCédric Le Goater {
227*30b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_GET_CLASS(s);
228a03cb1daSCédric Le Goater     AspeedSegments seg;
229a03cb1daSCédric Le Goater     int i;
230a03cb1daSCédric Le Goater 
231*30b6852cSCédric Le Goater     for (i = 0; i < asc->max_peripherals; i++) {
232a03cb1daSCédric Le Goater         if (i == cs) {
233a03cb1daSCédric Le Goater             continue;
234a03cb1daSCédric Le Goater         }
235a03cb1daSCédric Le Goater 
236*30b6852cSCédric Le Goater         asc->reg_to_segment(s, s->regs[R_SEG_ADDR0 + i], &seg);
237a03cb1daSCédric Le Goater 
238a03cb1daSCédric Le Goater         if (new->addr + new->size > seg.addr &&
239a03cb1daSCédric Le Goater             new->addr < seg.addr + seg.size) {
24032c54bd0SCédric Le Goater             aspeed_smc_error("new segment CS%d [ 0x%"
241a03cb1daSCédric Le Goater                              HWADDR_PRIx" - 0x%"HWADDR_PRIx" ] overlaps with "
24232c54bd0SCédric Le Goater                              "CS%d [ 0x%"HWADDR_PRIx" - 0x%"HWADDR_PRIx" ]",
24332c54bd0SCédric Le Goater                              cs, new->addr, new->addr + new->size,
244a03cb1daSCédric Le Goater                              i, seg.addr, seg.addr + seg.size);
245a03cb1daSCédric Le Goater             return true;
246a03cb1daSCédric Le Goater         }
247a03cb1daSCédric Le Goater     }
248a03cb1daSCédric Le Goater     return false;
249a03cb1daSCédric Le Goater }
250a03cb1daSCédric Le Goater 
251673b1f86SCédric Le Goater static void aspeed_smc_flash_set_segment_region(AspeedSMCState *s, int cs,
252673b1f86SCédric Le Goater                                                 uint64_t regval)
253673b1f86SCédric Le Goater {
254*30b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_GET_CLASS(s);
255673b1f86SCédric Le Goater     AspeedSMCFlash *fl = &s->flashes[cs];
256673b1f86SCédric Le Goater     AspeedSegments seg;
257673b1f86SCédric Le Goater 
258*30b6852cSCédric Le Goater     asc->reg_to_segment(s, regval, &seg);
259673b1f86SCédric Le Goater 
260673b1f86SCédric Le Goater     memory_region_transaction_begin();
261673b1f86SCédric Le Goater     memory_region_set_size(&fl->mmio, seg.size);
262*30b6852cSCédric Le Goater     memory_region_set_address(&fl->mmio, seg.addr - asc->flash_window_base);
2632175eacfSCédric Le Goater     memory_region_set_enabled(&fl->mmio, !!seg.size);
264673b1f86SCédric Le Goater     memory_region_transaction_commit();
265673b1f86SCédric Le Goater 
266673b1f86SCédric Le Goater     s->regs[R_SEG_ADDR0 + cs] = regval;
267673b1f86SCédric Le Goater }
268673b1f86SCédric Le Goater 
269a03cb1daSCédric Le Goater static void aspeed_smc_flash_set_segment(AspeedSMCState *s, int cs,
270a03cb1daSCédric Le Goater                                          uint64_t new)
271a03cb1daSCédric Le Goater {
272*30b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_GET_CLASS(s);
273a03cb1daSCédric Le Goater     AspeedSegments seg;
274a03cb1daSCédric Le Goater 
275*30b6852cSCédric Le Goater     asc->reg_to_segment(s, new, &seg);
276a03cb1daSCédric Le Goater 
277bd6ce9a6SCédric Le Goater     trace_aspeed_smc_flash_set_segment(cs, new, seg.addr, seg.addr + seg.size);
278bd6ce9a6SCédric Le Goater 
279a03cb1daSCédric Le Goater     /* The start address of CS0 is read-only */
280*30b6852cSCédric Le Goater     if (cs == 0 && seg.addr != asc->flash_window_base) {
28132c54bd0SCédric Le Goater         aspeed_smc_error("Tried to change CS0 start address to 0x%"
28232c54bd0SCédric Le Goater                          HWADDR_PRIx, seg.addr);
283*30b6852cSCédric Le Goater         seg.addr = asc->flash_window_base;
284*30b6852cSCédric Le Goater         new = asc->segment_to_reg(s, &seg);
285a03cb1daSCédric Le Goater     }
286a03cb1daSCédric Le Goater 
287a03cb1daSCédric Le Goater     /*
288a03cb1daSCédric Le Goater      * The end address of the AST2500 spi controllers is also
289a03cb1daSCédric Le Goater      * read-only.
290a03cb1daSCédric Le Goater      */
291*30b6852cSCédric Le Goater     if ((asc->segments == aspeed_2500_spi1_segments ||
292*30b6852cSCédric Le Goater          asc->segments == aspeed_2500_spi2_segments) &&
293*30b6852cSCédric Le Goater         cs == asc->max_peripherals &&
294*30b6852cSCédric Le Goater         seg.addr + seg.size != asc->segments[cs].addr +
295*30b6852cSCédric Le Goater         asc->segments[cs].size) {
29632c54bd0SCédric Le Goater         aspeed_smc_error("Tried to change CS%d end address to 0x%"
29732c54bd0SCédric Le Goater                          HWADDR_PRIx, cs, seg.addr + seg.size);
298*30b6852cSCédric Le Goater         seg.size = asc->segments[cs].addr + asc->segments[cs].size -
2990584d3c3SCédric Le Goater             seg.addr;
300*30b6852cSCédric Le Goater         new = asc->segment_to_reg(s, &seg);
301a03cb1daSCédric Le Goater     }
302a03cb1daSCédric Le Goater 
303a03cb1daSCédric Le Goater     /* Keep the segment in the overall flash window */
3042175eacfSCédric Le Goater     if (seg.size &&
305*30b6852cSCédric Le Goater         (seg.addr + seg.size <= asc->flash_window_base ||
306*30b6852cSCédric Le Goater          seg.addr > asc->flash_window_base + asc->flash_window_size)) {
30732c54bd0SCédric Le Goater         aspeed_smc_error("new segment for CS%d is invalid : "
30832c54bd0SCédric Le Goater                          "[ 0x%"HWADDR_PRIx" - 0x%"HWADDR_PRIx" ]",
30932c54bd0SCédric Le Goater                          cs, seg.addr, seg.addr + seg.size);
310a03cb1daSCédric Le Goater         return;
311a03cb1daSCédric Le Goater     }
312a03cb1daSCédric Le Goater 
313a03cb1daSCédric Le Goater     /* Check start address vs. alignment */
3140584d3c3SCédric Le Goater     if (seg.size && !QEMU_IS_ALIGNED(seg.addr, seg.size)) {
31532c54bd0SCédric Le Goater         aspeed_smc_error("new segment for CS%d is not "
31632c54bd0SCédric Le Goater                          "aligned : [ 0x%"HWADDR_PRIx" - 0x%"HWADDR_PRIx" ]",
31732c54bd0SCédric Le Goater                          cs, seg.addr, seg.addr + seg.size);
318a03cb1daSCédric Le Goater     }
319a03cb1daSCédric Le Goater 
3200584d3c3SCédric Le Goater     /* And segments should not overlap (in the specs) */
3210584d3c3SCédric Le Goater     aspeed_smc_flash_overlap(s, &seg, cs);
322a03cb1daSCédric Le Goater 
323a03cb1daSCédric Le Goater     /* All should be fine now to move the region */
324673b1f86SCédric Le Goater     aspeed_smc_flash_set_segment_region(s, cs, new);
325a03cb1daSCédric Le Goater }
326a03cb1daSCédric Le Goater 
327924ed163SCédric Le Goater static uint64_t aspeed_smc_flash_default_read(void *opaque, hwaddr addr,
328924ed163SCédric Le Goater                                               unsigned size)
329924ed163SCédric Le Goater {
33032c54bd0SCédric Le Goater     aspeed_smc_error("To 0x%" HWADDR_PRIx " of size %u" PRIx64, addr, size);
331924ed163SCédric Le Goater     return 0;
332924ed163SCédric Le Goater }
333924ed163SCédric Le Goater 
334924ed163SCédric Le Goater static void aspeed_smc_flash_default_write(void *opaque, hwaddr addr,
335924ed163SCédric Le Goater                                            uint64_t data, unsigned size)
336924ed163SCédric Le Goater {
33732c54bd0SCédric Le Goater     aspeed_smc_error("To 0x%" HWADDR_PRIx " of size %u: 0x%" PRIx64,
33832c54bd0SCédric Le Goater                      addr, size, data);
339924ed163SCédric Le Goater }
340924ed163SCédric Le Goater 
341924ed163SCédric Le Goater static const MemoryRegionOps aspeed_smc_flash_default_ops = {
342924ed163SCédric Le Goater     .read = aspeed_smc_flash_default_read,
343924ed163SCédric Le Goater     .write = aspeed_smc_flash_default_write,
344924ed163SCédric Le Goater     .endianness = DEVICE_LITTLE_ENDIAN,
345924ed163SCédric Le Goater     .valid = {
346924ed163SCédric Le Goater         .min_access_size = 1,
347924ed163SCédric Le Goater         .max_access_size = 4,
348924ed163SCédric Le Goater     },
349924ed163SCédric Le Goater };
350924ed163SCédric Le Goater 
351f248a9dbSCédric Le Goater static inline int aspeed_smc_flash_mode(const AspeedSMCFlash *fl)
352924ed163SCédric Le Goater {
353f248a9dbSCédric Le Goater     const AspeedSMCState *s = fl->controller;
354f248a9dbSCédric Le Goater 
355f248a9dbSCédric Le Goater     return s->regs[s->r_ctrl0 + fl->id] & CTRL_CMD_MODE_MASK;
356924ed163SCédric Le Goater }
357924ed163SCédric Le Goater 
358f248a9dbSCédric Le Goater static inline bool aspeed_smc_is_writable(const AspeedSMCFlash *fl)
359924ed163SCédric Le Goater {
360f248a9dbSCédric Le Goater     const AspeedSMCState *s = fl->controller;
361f248a9dbSCédric Le Goater 
362f248a9dbSCédric Le Goater     return s->regs[s->r_conf] & (1 << (s->conf_enable_w0 + fl->id));
363924ed163SCédric Le Goater }
364924ed163SCédric Le Goater 
365fcdf2c59SCédric Le Goater static inline int aspeed_smc_flash_cmd(const AspeedSMCFlash *fl)
366fcdf2c59SCédric Le Goater {
367fcdf2c59SCédric Le Goater     const AspeedSMCState *s = fl->controller;
368fcdf2c59SCédric Le Goater     int cmd = (s->regs[s->r_ctrl0 + fl->id] >> CTRL_CMD_SHIFT) & CTRL_CMD_MASK;
369fcdf2c59SCédric Le Goater 
370bcaa8dddSCédric Le Goater     /*
371bcaa8dddSCédric Le Goater      * In read mode, the default SPI command is READ (0x3). In other
372bcaa8dddSCédric Le Goater      * modes, the command should necessarily be defined
373bcaa8dddSCédric Le Goater      *
374bcaa8dddSCédric Le Goater      * TODO: add support for READ4 (0x13) on AST2600
375bcaa8dddSCédric Le Goater      */
376fcdf2c59SCédric Le Goater     if (aspeed_smc_flash_mode(fl) == CTRL_READMODE) {
377fcdf2c59SCédric Le Goater         cmd = SPI_OP_READ;
378fcdf2c59SCédric Le Goater     }
379fcdf2c59SCédric Le Goater 
380fcdf2c59SCédric Le Goater     if (!cmd) {
38132c54bd0SCédric Le Goater         aspeed_smc_error("no command defined for mode %d",
38232c54bd0SCédric Le Goater                          aspeed_smc_flash_mode(fl));
383fcdf2c59SCédric Le Goater     }
384fcdf2c59SCédric Le Goater 
385fcdf2c59SCédric Le Goater     return cmd;
386fcdf2c59SCédric Le Goater }
387fcdf2c59SCédric Le Goater 
388fcdf2c59SCédric Le Goater static inline int aspeed_smc_flash_is_4byte(const AspeedSMCFlash *fl)
389fcdf2c59SCédric Le Goater {
390fcdf2c59SCédric Le Goater     const AspeedSMCState *s = fl->controller;
391*30b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_GET_CLASS(s);
392fcdf2c59SCédric Le Goater 
393*30b6852cSCédric Le Goater     if (asc->segments == aspeed_2400_spi1_segments) {
394fcdf2c59SCédric Le Goater         return s->regs[s->r_ctrl0] & CTRL_AST2400_SPI_4BYTE;
395fcdf2c59SCédric Le Goater     } else {
396fcdf2c59SCédric Le Goater         return s->regs[s->r_ce_ctrl] & (1 << (CTRL_EXTENDED0 + fl->id));
397fcdf2c59SCédric Le Goater     }
398fcdf2c59SCédric Le Goater }
399fcdf2c59SCédric Le Goater 
400e7e741caSCédric Le Goater static void aspeed_smc_flash_do_select(AspeedSMCFlash *fl, bool unselect)
401fcdf2c59SCédric Le Goater {
402e7e741caSCédric Le Goater     AspeedSMCState *s = fl->controller;
403fcdf2c59SCédric Le Goater 
404e7e741caSCédric Le Goater     trace_aspeed_smc_flash_select(fl->id, unselect ? "un" : "");
405e7e741caSCédric Le Goater 
406e7e741caSCédric Le Goater     qemu_set_irq(s->cs_lines[fl->id], unselect);
407fcdf2c59SCédric Le Goater }
408fcdf2c59SCédric Le Goater 
409fcdf2c59SCédric Le Goater static void aspeed_smc_flash_select(AspeedSMCFlash *fl)
410fcdf2c59SCédric Le Goater {
411e7e741caSCédric Le Goater     aspeed_smc_flash_do_select(fl, false);
412fcdf2c59SCédric Le Goater }
413fcdf2c59SCédric Le Goater 
414fcdf2c59SCédric Le Goater static void aspeed_smc_flash_unselect(AspeedSMCFlash *fl)
415fcdf2c59SCédric Le Goater {
416e7e741caSCédric Le Goater     aspeed_smc_flash_do_select(fl, true);
417fcdf2c59SCédric Le Goater }
418fcdf2c59SCédric Le Goater 
419fcdf2c59SCédric Le Goater static uint32_t aspeed_smc_check_segment_addr(const AspeedSMCFlash *fl,
420fcdf2c59SCédric Le Goater                                               uint32_t addr)
421fcdf2c59SCédric Le Goater {
422fcdf2c59SCédric Le Goater     const AspeedSMCState *s = fl->controller;
423*30b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_GET_CLASS(s);
424fcdf2c59SCédric Le Goater     AspeedSegments seg;
425fcdf2c59SCédric Le Goater 
426*30b6852cSCédric Le Goater     asc->reg_to_segment(s, s->regs[R_SEG_ADDR0 + fl->id], &seg);
427b4cc583fSCédric Le Goater     if ((addr % seg.size) != addr) {
42832c54bd0SCédric Le Goater         aspeed_smc_error("invalid address 0x%08x for CS%d segment : "
42932c54bd0SCédric Le Goater                          "[ 0x%"HWADDR_PRIx" - 0x%"HWADDR_PRIx" ]",
43032c54bd0SCédric Le Goater                          addr, fl->id, seg.addr, seg.addr + seg.size);
431b4cc583fSCédric Le Goater         addr %= seg.size;
432fcdf2c59SCédric Le Goater     }
433fcdf2c59SCédric Le Goater 
434fcdf2c59SCédric Le Goater     return addr;
435fcdf2c59SCédric Le Goater }
436fcdf2c59SCédric Le Goater 
437ac2810deSCédric Le Goater static int aspeed_smc_flash_dummies(const AspeedSMCFlash *fl)
438ac2810deSCédric Le Goater {
439ac2810deSCédric Le Goater     const AspeedSMCState *s = fl->controller;
440ac2810deSCédric Le Goater     uint32_t r_ctrl0 = s->regs[s->r_ctrl0 + fl->id];
441ac2810deSCédric Le Goater     uint32_t dummy_high = (r_ctrl0 >> CTRL_DUMMY_HIGH_SHIFT) & 0x1;
442ac2810deSCédric Le Goater     uint32_t dummy_low = (r_ctrl0 >> CTRL_DUMMY_LOW_SHIFT) & 0x3;
4430721309eSCédric Le Goater     uint32_t dummies = ((dummy_high << 2) | dummy_low) * 8;
444ac2810deSCédric Le Goater 
4450721309eSCédric Le Goater     if (r_ctrl0 & CTRL_IO_DUAL_ADDR_DATA) {
4460721309eSCédric Le Goater         dummies /= 2;
4470721309eSCédric Le Goater     }
4480721309eSCédric Le Goater 
4490721309eSCédric Le Goater     return dummies;
450ac2810deSCédric Le Goater }
451ac2810deSCédric Le Goater 
45296c4be95SCédric Le Goater static void aspeed_smc_flash_setup(AspeedSMCFlash *fl, uint32_t addr)
453fcdf2c59SCédric Le Goater {
454fcdf2c59SCédric Le Goater     const AspeedSMCState *s = fl->controller;
455fcdf2c59SCédric Le Goater     uint8_t cmd = aspeed_smc_flash_cmd(fl);
456af453a5eSCédric Le Goater     int i = aspeed_smc_flash_is_4byte(fl) ? 4 : 3;
457fcdf2c59SCédric Le Goater 
458fcdf2c59SCédric Le Goater     /* Flash access can not exceed CS segment */
459fcdf2c59SCédric Le Goater     addr = aspeed_smc_check_segment_addr(fl, addr);
460fcdf2c59SCédric Le Goater 
461fcdf2c59SCédric Le Goater     ssi_transfer(s->spi, cmd);
462af453a5eSCédric Le Goater     while (i--) {
463af453a5eSCédric Le Goater         if (aspeed_smc_addr_byte_enabled(s, i)) {
464af453a5eSCédric Le Goater             ssi_transfer(s->spi, (addr >> (i * 8)) & 0xff);
465fcdf2c59SCédric Le Goater         }
466af453a5eSCédric Le Goater     }
46796c4be95SCédric Le Goater 
46896c4be95SCédric Le Goater     /*
46996c4be95SCédric Le Goater      * Use fake transfers to model dummy bytes. The value should
47096c4be95SCédric Le Goater      * be configured to some non-zero value in fast read mode and
47196c4be95SCédric Le Goater      * zero in read mode. But, as the HW allows inconsistent
47296c4be95SCédric Le Goater      * settings, let's check for fast read mode.
47396c4be95SCédric Le Goater      */
47496c4be95SCédric Le Goater     if (aspeed_smc_flash_mode(fl) == CTRL_FREADMODE) {
47596c4be95SCédric Le Goater         for (i = 0; i < aspeed_smc_flash_dummies(fl); i++) {
4769149af2aSCédric Le Goater             ssi_transfer(fl->controller->spi, s->regs[R_DUMMY_DATA] & 0xff);
47796c4be95SCédric Le Goater         }
47896c4be95SCédric Le Goater     }
479fcdf2c59SCédric Le Goater }
480fcdf2c59SCédric Le Goater 
481924ed163SCédric Le Goater static uint64_t aspeed_smc_flash_read(void *opaque, hwaddr addr, unsigned size)
482924ed163SCédric Le Goater {
483924ed163SCédric Le Goater     AspeedSMCFlash *fl = opaque;
484fcdf2c59SCédric Le Goater     AspeedSMCState *s = fl->controller;
485924ed163SCédric Le Goater     uint64_t ret = 0;
486924ed163SCédric Le Goater     int i;
487924ed163SCédric Le Goater 
488fcdf2c59SCédric Le Goater     switch (aspeed_smc_flash_mode(fl)) {
489fcdf2c59SCédric Le Goater     case CTRL_USERMODE:
490924ed163SCédric Le Goater         for (i = 0; i < size; i++) {
491924ed163SCédric Le Goater             ret |= ssi_transfer(s->spi, 0x0) << (8 * i);
492924ed163SCédric Le Goater         }
493fcdf2c59SCédric Le Goater         break;
494fcdf2c59SCédric Le Goater     case CTRL_READMODE:
495fcdf2c59SCédric Le Goater     case CTRL_FREADMODE:
496fcdf2c59SCédric Le Goater         aspeed_smc_flash_select(fl);
49796c4be95SCédric Le Goater         aspeed_smc_flash_setup(fl, addr);
498ac2810deSCédric Le Goater 
499fcdf2c59SCédric Le Goater         for (i = 0; i < size; i++) {
500fcdf2c59SCédric Le Goater             ret |= ssi_transfer(s->spi, 0x0) << (8 * i);
501fcdf2c59SCédric Le Goater         }
502fcdf2c59SCédric Le Goater 
503fcdf2c59SCédric Le Goater         aspeed_smc_flash_unselect(fl);
504fcdf2c59SCédric Le Goater         break;
505fcdf2c59SCédric Le Goater     default:
50632c54bd0SCédric Le Goater         aspeed_smc_error("invalid flash mode %d", aspeed_smc_flash_mode(fl));
507924ed163SCédric Le Goater     }
508924ed163SCédric Le Goater 
509bd6ce9a6SCédric Le Goater     trace_aspeed_smc_flash_read(fl->id, addr, size, ret,
510bd6ce9a6SCédric Le Goater                                 aspeed_smc_flash_mode(fl));
511924ed163SCédric Le Goater     return ret;
512924ed163SCédric Le Goater }
513924ed163SCédric Le Goater 
514f95c4bffSCédric Le Goater /*
515f95c4bffSCédric Le Goater  * TODO (clg@kaod.org): stolen from xilinx_spips.c. Should move to a
516f95c4bffSCédric Le Goater  * common include header.
517f95c4bffSCédric Le Goater  */
518f95c4bffSCédric Le Goater typedef enum {
519f95c4bffSCédric Le Goater     READ = 0x3,         READ_4 = 0x13,
520f95c4bffSCédric Le Goater     FAST_READ = 0xb,    FAST_READ_4 = 0x0c,
521f95c4bffSCédric Le Goater     DOR = 0x3b,         DOR_4 = 0x3c,
522f95c4bffSCédric Le Goater     QOR = 0x6b,         QOR_4 = 0x6c,
523f95c4bffSCédric Le Goater     DIOR = 0xbb,        DIOR_4 = 0xbc,
524f95c4bffSCédric Le Goater     QIOR = 0xeb,        QIOR_4 = 0xec,
525f95c4bffSCédric Le Goater 
526f95c4bffSCédric Le Goater     PP = 0x2,           PP_4 = 0x12,
527f95c4bffSCédric Le Goater     DPP = 0xa2,
528f95c4bffSCédric Le Goater     QPP = 0x32,         QPP_4 = 0x34,
529f95c4bffSCédric Le Goater } FlashCMD;
530f95c4bffSCédric Le Goater 
531f95c4bffSCédric Le Goater static int aspeed_smc_num_dummies(uint8_t command)
532f95c4bffSCédric Le Goater {
533f95c4bffSCédric Le Goater     switch (command) { /* check for dummies */
534f95c4bffSCédric Le Goater     case READ: /* no dummy bytes/cycles */
535f95c4bffSCédric Le Goater     case PP:
536f95c4bffSCédric Le Goater     case DPP:
537f95c4bffSCédric Le Goater     case QPP:
538f95c4bffSCédric Le Goater     case READ_4:
539f95c4bffSCédric Le Goater     case PP_4:
540f95c4bffSCédric Le Goater     case QPP_4:
541f95c4bffSCédric Le Goater         return 0;
542f95c4bffSCédric Le Goater     case FAST_READ:
543f95c4bffSCédric Le Goater     case DOR:
544f95c4bffSCédric Le Goater     case QOR:
5457faf6f17SGuenter Roeck     case FAST_READ_4:
546f95c4bffSCédric Le Goater     case DOR_4:
547f95c4bffSCédric Le Goater     case QOR_4:
548f95c4bffSCédric Le Goater         return 1;
549f95c4bffSCédric Le Goater     case DIOR:
550f95c4bffSCédric Le Goater     case DIOR_4:
551f95c4bffSCédric Le Goater         return 2;
552f95c4bffSCédric Le Goater     case QIOR:
553f95c4bffSCédric Le Goater     case QIOR_4:
554f95c4bffSCédric Le Goater         return 4;
555f95c4bffSCédric Le Goater     default:
556f95c4bffSCédric Le Goater         return -1;
557f95c4bffSCédric Le Goater     }
558f95c4bffSCédric Le Goater }
559f95c4bffSCédric Le Goater 
560f95c4bffSCédric Le Goater static bool aspeed_smc_do_snoop(AspeedSMCFlash *fl,  uint64_t data,
561f95c4bffSCédric Le Goater                                 unsigned size)
562f95c4bffSCédric Le Goater {
563f95c4bffSCédric Le Goater     AspeedSMCState *s = fl->controller;
564f95c4bffSCédric Le Goater     uint8_t addr_width = aspeed_smc_flash_is_4byte(fl) ? 4 : 3;
565f95c4bffSCédric Le Goater 
566bd6ce9a6SCédric Le Goater     trace_aspeed_smc_do_snoop(fl->id, s->snoop_index, s->snoop_dummies,
567bd6ce9a6SCédric Le Goater                               (uint8_t) data & 0xff);
568bd6ce9a6SCédric Le Goater 
569f95c4bffSCédric Le Goater     if (s->snoop_index == SNOOP_OFF) {
570f95c4bffSCédric Le Goater         return false; /* Do nothing */
571f95c4bffSCédric Le Goater 
572f95c4bffSCédric Le Goater     } else if (s->snoop_index == SNOOP_START) {
573f95c4bffSCédric Le Goater         uint8_t cmd = data & 0xff;
574f95c4bffSCédric Le Goater         int ndummies = aspeed_smc_num_dummies(cmd);
575f95c4bffSCédric Le Goater 
576f95c4bffSCédric Le Goater         /*
577f95c4bffSCédric Le Goater          * No dummy cycles are expected with the current command. Turn
578f95c4bffSCédric Le Goater          * off snooping and let the transfer proceed normally.
579f95c4bffSCédric Le Goater          */
580f95c4bffSCédric Le Goater         if (ndummies <= 0) {
581f95c4bffSCédric Le Goater             s->snoop_index = SNOOP_OFF;
582f95c4bffSCédric Le Goater             return false;
583f95c4bffSCédric Le Goater         }
584f95c4bffSCédric Le Goater 
585f95c4bffSCédric Le Goater         s->snoop_dummies = ndummies * 8;
586f95c4bffSCédric Le Goater 
587f95c4bffSCédric Le Goater     } else if (s->snoop_index >= addr_width + 1) {
588f95c4bffSCédric Le Goater 
589f95c4bffSCédric Le Goater         /* The SPI transfer has reached the dummy cycles sequence */
590f95c4bffSCédric Le Goater         for (; s->snoop_dummies; s->snoop_dummies--) {
591f95c4bffSCédric Le Goater             ssi_transfer(s->spi, s->regs[R_DUMMY_DATA] & 0xff);
592f95c4bffSCédric Le Goater         }
593f95c4bffSCédric Le Goater 
594f95c4bffSCédric Le Goater         /* If no more dummy cycles are expected, turn off snooping */
595f95c4bffSCédric Le Goater         if (!s->snoop_dummies) {
596f95c4bffSCédric Le Goater             s->snoop_index = SNOOP_OFF;
597f95c4bffSCédric Le Goater         } else {
598f95c4bffSCédric Le Goater             s->snoop_index += size;
599f95c4bffSCédric Le Goater         }
600f95c4bffSCédric Le Goater 
601f95c4bffSCédric Le Goater         /*
602f95c4bffSCédric Le Goater          * Dummy cycles have been faked already. Ignore the current
603f95c4bffSCédric Le Goater          * SPI transfer
604f95c4bffSCédric Le Goater          */
605f95c4bffSCédric Le Goater         return true;
606f95c4bffSCédric Le Goater     }
607f95c4bffSCédric Le Goater 
608f95c4bffSCédric Le Goater     s->snoop_index += size;
609f95c4bffSCédric Le Goater     return false;
610f95c4bffSCédric Le Goater }
611f95c4bffSCédric Le Goater 
612924ed163SCédric Le Goater static void aspeed_smc_flash_write(void *opaque, hwaddr addr, uint64_t data,
613924ed163SCédric Le Goater                                    unsigned size)
614924ed163SCédric Le Goater {
615924ed163SCédric Le Goater     AspeedSMCFlash *fl = opaque;
616fcdf2c59SCédric Le Goater     AspeedSMCState *s = fl->controller;
617924ed163SCédric Le Goater     int i;
618924ed163SCédric Le Goater 
619bd6ce9a6SCédric Le Goater     trace_aspeed_smc_flash_write(fl->id, addr, size, data,
620bd6ce9a6SCédric Le Goater                                  aspeed_smc_flash_mode(fl));
621bd6ce9a6SCédric Le Goater 
622f248a9dbSCédric Le Goater     if (!aspeed_smc_is_writable(fl)) {
62332c54bd0SCédric Le Goater         aspeed_smc_error("flash is not writable at 0x%" HWADDR_PRIx, addr);
624924ed163SCédric Le Goater         return;
625924ed163SCédric Le Goater     }
626924ed163SCédric Le Goater 
627fcdf2c59SCédric Le Goater     switch (aspeed_smc_flash_mode(fl)) {
628fcdf2c59SCédric Le Goater     case CTRL_USERMODE:
629f95c4bffSCédric Le Goater         if (aspeed_smc_do_snoop(fl, data, size)) {
630f95c4bffSCédric Le Goater             break;
631f95c4bffSCédric Le Goater         }
632f95c4bffSCédric Le Goater 
633fcdf2c59SCédric Le Goater         for (i = 0; i < size; i++) {
634fcdf2c59SCédric Le Goater             ssi_transfer(s->spi, (data >> (8 * i)) & 0xff);
635924ed163SCédric Le Goater         }
636fcdf2c59SCédric Le Goater         break;
637fcdf2c59SCédric Le Goater     case CTRL_WRITEMODE:
638fcdf2c59SCédric Le Goater         aspeed_smc_flash_select(fl);
63996c4be95SCédric Le Goater         aspeed_smc_flash_setup(fl, addr);
640924ed163SCédric Le Goater 
641924ed163SCédric Le Goater         for (i = 0; i < size; i++) {
642924ed163SCédric Le Goater             ssi_transfer(s->spi, (data >> (8 * i)) & 0xff);
643924ed163SCédric Le Goater         }
644fcdf2c59SCédric Le Goater 
645fcdf2c59SCédric Le Goater         aspeed_smc_flash_unselect(fl);
646fcdf2c59SCédric Le Goater         break;
647fcdf2c59SCédric Le Goater     default:
64832c54bd0SCédric Le Goater         aspeed_smc_error("invalid flash mode %d", aspeed_smc_flash_mode(fl));
649fcdf2c59SCédric Le Goater     }
650924ed163SCédric Le Goater }
651924ed163SCédric Le Goater 
652924ed163SCédric Le Goater static const MemoryRegionOps aspeed_smc_flash_ops = {
653924ed163SCédric Le Goater     .read = aspeed_smc_flash_read,
654924ed163SCédric Le Goater     .write = aspeed_smc_flash_write,
655924ed163SCédric Le Goater     .endianness = DEVICE_LITTLE_ENDIAN,
656924ed163SCédric Le Goater     .valid = {
657924ed163SCédric Le Goater         .min_access_size = 1,
658924ed163SCédric Le Goater         .max_access_size = 4,
659924ed163SCédric Le Goater     },
6607c1c69bcSCédric Le Goater };
6617c1c69bcSCédric Le Goater 
662e7e741caSCédric Le Goater static void aspeed_smc_flash_update_ctrl(AspeedSMCFlash *fl, uint32_t value)
6637c1c69bcSCédric Le Goater {
664f95c4bffSCédric Le Goater     AspeedSMCState *s = fl->controller;
665e7e741caSCédric Le Goater     bool unselect;
666f95c4bffSCédric Le Goater 
667e7e741caSCédric Le Goater     /* User mode selects the CS, other modes unselect */
668e7e741caSCédric Le Goater     unselect = (value & CTRL_CMD_MODE_MASK) != CTRL_USERMODE;
6697c1c69bcSCédric Le Goater 
670e7e741caSCédric Le Goater     /* A change of CTRL_CE_STOP_ACTIVE from 0 to 1, unselects the CS */
671e7e741caSCédric Le Goater     if (!(s->regs[s->r_ctrl0 + fl->id] & CTRL_CE_STOP_ACTIVE) &&
672e7e741caSCédric Le Goater         value & CTRL_CE_STOP_ACTIVE) {
673e7e741caSCédric Le Goater         unselect = true;
674e7e741caSCédric Le Goater     }
675e7e741caSCédric Le Goater 
676e7e741caSCédric Le Goater     s->regs[s->r_ctrl0 + fl->id] = value;
677e7e741caSCédric Le Goater 
678e7e741caSCédric Le Goater     s->snoop_index = unselect ? SNOOP_OFF : SNOOP_START;
679e7e741caSCédric Le Goater 
680e7e741caSCédric Le Goater     aspeed_smc_flash_do_select(fl, unselect);
6817c1c69bcSCédric Le Goater }
6827c1c69bcSCédric Le Goater 
6837c1c69bcSCédric Le Goater static void aspeed_smc_reset(DeviceState *d)
6847c1c69bcSCédric Le Goater {
6857c1c69bcSCédric Le Goater     AspeedSMCState *s = ASPEED_SMC(d);
686*30b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_GET_CLASS(s);
6877c1c69bcSCédric Le Goater     int i;
6887c1c69bcSCédric Le Goater 
6897c1c69bcSCédric Le Goater     memset(s->regs, 0, sizeof s->regs);
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 */
698*30b6852cSCédric Le Goater     for (i = 0; i < asc->max_peripherals; ++i) {
699673b1f86SCédric Le Goater         aspeed_smc_flash_set_segment_region(s, i,
700*30b6852cSCédric Le Goater                     asc->segment_to_reg(s, &asc->segments[i]));
701a03cb1daSCédric Le Goater     }
7020707b34dSCédric Le Goater 
703bcaa8dddSCédric Le Goater     /* HW strapping flash type for the AST2600 controllers  */
704*30b6852cSCédric Le Goater     if (asc->segments == aspeed_2600_fmc_segments) {
705bcaa8dddSCédric Le Goater         /* flash type is fixed to SPI for all */
706bcaa8dddSCédric Le Goater         s->regs[s->r_conf] |= (CONF_FLASH_TYPE_SPI << CONF_FLASH_TYPE0);
707bcaa8dddSCédric Le Goater         s->regs[s->r_conf] |= (CONF_FLASH_TYPE_SPI << CONF_FLASH_TYPE1);
708bcaa8dddSCédric Le Goater         s->regs[s->r_conf] |= (CONF_FLASH_TYPE_SPI << CONF_FLASH_TYPE2);
709bcaa8dddSCédric Le Goater     }
710bcaa8dddSCédric Le Goater 
711a57baeb4SCédric Le Goater     /* HW strapping flash type for FMC controllers  */
712*30b6852cSCédric Le Goater     if (asc->segments == aspeed_2500_fmc_segments) {
7130707b34dSCédric Le Goater         /* flash type is fixed to SPI for CE0 and CE1 */
7140707b34dSCédric Le Goater         s->regs[s->r_conf] |= (CONF_FLASH_TYPE_SPI << CONF_FLASH_TYPE0);
7150707b34dSCédric Le Goater         s->regs[s->r_conf] |= (CONF_FLASH_TYPE_SPI << CONF_FLASH_TYPE1);
7160707b34dSCédric Le Goater     }
7170707b34dSCédric Le Goater 
7180707b34dSCédric Le Goater     /* HW strapping for AST2400 FMC controllers (SCU70). Let's use the
7190707b34dSCédric Le Goater      * configuration of the palmetto-bmc machine */
720*30b6852cSCédric Le Goater     if (asc->segments == aspeed_2400_fmc_segments) {
7210707b34dSCédric Le Goater         s->regs[s->r_conf] |= (CONF_FLASH_TYPE_SPI << CONF_FLASH_TYPE0);
7220707b34dSCédric Le Goater     }
723f95c4bffSCédric Le Goater 
724f95c4bffSCédric Le Goater     s->snoop_index = SNOOP_OFF;
725f95c4bffSCédric Le Goater     s->snoop_dummies = 0;
7267c1c69bcSCédric Le Goater }
7277c1c69bcSCédric Le Goater 
7287c1c69bcSCédric Le Goater static uint64_t aspeed_smc_read(void *opaque, hwaddr addr, unsigned int size)
7297c1c69bcSCédric Le Goater {
7307c1c69bcSCédric Le Goater     AspeedSMCState *s = ASPEED_SMC(opaque);
731*30b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_GET_CLASS(opaque);
7327c1c69bcSCédric Le Goater 
7337c1c69bcSCédric Le Goater     addr >>= 2;
7347c1c69bcSCédric Le Goater 
73597c2ed5dSCédric Le Goater     if (addr == s->r_conf ||
736f286f04cSCédric Le Goater         (addr >= s->r_timings &&
737*30b6852cSCédric Le Goater          addr < s->r_timings + asc->nregs_timings) ||
73897c2ed5dSCédric Le Goater         addr == s->r_ce_ctrl ||
739af453a5eSCédric Le Goater         addr == R_CE_CMD_CTRL ||
7402e1f0502SCédric Le Goater         addr == R_INTR_CTRL ||
7419149af2aSCédric Le Goater         addr == R_DUMMY_DATA ||
742*30b6852cSCédric Le Goater         (aspeed_smc_has_wdt_control(asc) && addr == R_FMC_WDT2_CTRL) ||
743*30b6852cSCédric Le Goater         (aspeed_smc_has_dma(asc) && addr == R_DMA_CTRL) ||
744*30b6852cSCédric Le Goater         (aspeed_smc_has_dma(asc) && addr == R_DMA_FLASH_ADDR) ||
745*30b6852cSCédric Le Goater         (aspeed_smc_has_dma(asc) && addr == R_DMA_DRAM_ADDR) ||
746*30b6852cSCédric Le Goater         (aspeed_smc_has_dma(asc) && addr == R_DMA_LEN) ||
747*30b6852cSCédric Le Goater         (aspeed_smc_has_dma(asc) && addr == R_DMA_CHECKSUM) ||
7485ade579bSPhilippe Mathieu-Daudé         (addr >= R_SEG_ADDR0 &&
749*30b6852cSCédric Le Goater          addr < R_SEG_ADDR0 + asc->max_peripherals) ||
750*30b6852cSCédric Le Goater         (addr >= s->r_ctrl0 && addr < s->r_ctrl0 + asc->max_peripherals)) {
751bd6ce9a6SCédric Le Goater 
752bd6ce9a6SCédric Le Goater         trace_aspeed_smc_read(addr, size, s->regs[addr]);
753bd6ce9a6SCédric Le Goater 
75497c2ed5dSCédric Le Goater         return s->regs[addr];
75597c2ed5dSCédric Le Goater     } else {
7567c1c69bcSCédric Le Goater         qemu_log_mask(LOG_UNIMP, "%s: not implemented: 0x%" HWADDR_PRIx "\n",
7577c1c69bcSCédric Le Goater                       __func__, addr);
758b617ca92SCédric Le Goater         return -1;
7597c1c69bcSCédric Le Goater     }
7607c1c69bcSCédric Le Goater }
7617c1c69bcSCédric Le Goater 
7620d72c717SCédric Le Goater static uint8_t aspeed_smc_hclk_divisor(uint8_t hclk_mask)
7630d72c717SCédric Le Goater {
7640d72c717SCédric Le Goater     /* HCLK/1 .. HCLK/16 */
7650d72c717SCédric Le Goater     const uint8_t hclk_divisors[] = {
7660d72c717SCédric Le Goater         15, 7, 14, 6, 13, 5, 12, 4, 11, 3, 10, 2, 9, 1, 8, 0
7670d72c717SCédric Le Goater     };
7680d72c717SCédric Le Goater     int i;
7690d72c717SCédric Le Goater 
7700d72c717SCédric Le Goater     for (i = 0; i < ARRAY_SIZE(hclk_divisors); i++) {
7710d72c717SCédric Le Goater         if (hclk_mask == hclk_divisors[i]) {
7720d72c717SCédric Le Goater             return i + 1;
7730d72c717SCédric Le Goater         }
7740d72c717SCédric Le Goater     }
7750d72c717SCédric Le Goater 
77632c54bd0SCédric Le Goater     aspeed_smc_error("invalid HCLK mask %x", hclk_mask);
7770d72c717SCédric Le Goater     return 0;
7780d72c717SCédric Le Goater }
7790d72c717SCédric Le Goater 
7800d72c717SCédric Le Goater /*
7810d72c717SCédric Le Goater  * When doing calibration, the SPI clock rate in the CE0 Control
7820d72c717SCédric Le Goater  * Register and the read delay cycles in the Read Timing Compensation
7830d72c717SCédric Le Goater  * Register are set using bit[11:4] of the DMA Control Register.
7840d72c717SCédric Le Goater  */
7850d72c717SCédric Le Goater static void aspeed_smc_dma_calibration(AspeedSMCState *s)
7860d72c717SCédric Le Goater {
7870d72c717SCédric Le Goater     uint8_t delay =
7880d72c717SCédric Le Goater         (s->regs[R_DMA_CTRL] >> DMA_CTRL_DELAY_SHIFT) & DMA_CTRL_DELAY_MASK;
7890d72c717SCédric Le Goater     uint8_t hclk_mask =
7900d72c717SCédric Le Goater         (s->regs[R_DMA_CTRL] >> DMA_CTRL_FREQ_SHIFT) & DMA_CTRL_FREQ_MASK;
7910d72c717SCédric Le Goater     uint8_t hclk_div = aspeed_smc_hclk_divisor(hclk_mask);
7920d72c717SCédric Le Goater     uint32_t hclk_shift = (hclk_div - 1) << 2;
7930d72c717SCédric Le Goater     uint8_t cs;
7940d72c717SCédric Le Goater 
7950d72c717SCédric Le Goater     /*
7960d72c717SCédric Le Goater      * The Read Timing Compensation Register values apply to all CS on
7970d72c717SCédric Le Goater      * the SPI bus and only HCLK/1 - HCLK/5 can have tunable delays
7980d72c717SCédric Le Goater      */
7990d72c717SCédric Le Goater     if (hclk_div && hclk_div < 6) {
8000d72c717SCédric Le Goater         s->regs[s->r_timings] &= ~(0xf << hclk_shift);
8010d72c717SCédric Le Goater         s->regs[s->r_timings] |= delay << hclk_shift;
8020d72c717SCédric Le Goater     }
8030d72c717SCédric Le Goater 
8040d72c717SCédric Le Goater     /*
8050d72c717SCédric Le Goater      * TODO: compute the CS from the DMA address and the segment
8060d72c717SCédric Le Goater      * registers. This is not really a problem for now because the
8070d72c717SCédric Le Goater      * Timing Register values apply to all CS and software uses CS0 to
8080d72c717SCédric Le Goater      * do calibration.
8090d72c717SCédric Le Goater      */
8100d72c717SCédric Le Goater     cs = 0;
8110d72c717SCédric Le Goater     s->regs[s->r_ctrl0 + cs] &=
8120d72c717SCédric Le Goater         ~(CE_CTRL_CLOCK_FREQ_MASK << CE_CTRL_CLOCK_FREQ_SHIFT);
8130d72c717SCédric Le Goater     s->regs[s->r_ctrl0 + cs] |= CE_CTRL_CLOCK_FREQ(hclk_div);
8140d72c717SCédric Le Goater }
8150d72c717SCédric Le Goater 
816c4e1f0b4SCédric Le Goater /*
8175258c2a6SCédric Le Goater  * Emulate read errors in the DMA Checksum Register for high
8185258c2a6SCédric Le Goater  * frequencies and optimistic settings of the Read Timing Compensation
8195258c2a6SCédric Le Goater  * Register. This will help in tuning the SPI timing calibration
8205258c2a6SCédric Le Goater  * algorithm.
8215258c2a6SCédric Le Goater  */
8225258c2a6SCédric Le Goater static bool aspeed_smc_inject_read_failure(AspeedSMCState *s)
8235258c2a6SCédric Le Goater {
8245258c2a6SCédric Le Goater     uint8_t delay =
8255258c2a6SCédric Le Goater         (s->regs[R_DMA_CTRL] >> DMA_CTRL_DELAY_SHIFT) & DMA_CTRL_DELAY_MASK;
8265258c2a6SCédric Le Goater     uint8_t hclk_mask =
8275258c2a6SCédric Le Goater         (s->regs[R_DMA_CTRL] >> DMA_CTRL_FREQ_SHIFT) & DMA_CTRL_FREQ_MASK;
8285258c2a6SCédric Le Goater 
8295258c2a6SCédric Le Goater     /*
8305258c2a6SCédric Le Goater      * Typical values of a palmetto-bmc machine.
8315258c2a6SCédric Le Goater      */
8325258c2a6SCédric Le Goater     switch (aspeed_smc_hclk_divisor(hclk_mask)) {
8335258c2a6SCédric Le Goater     case 4 ... 16:
8345258c2a6SCédric Le Goater         return false;
8355258c2a6SCédric Le Goater     case 3: /* at least one HCLK cycle delay */
8365258c2a6SCédric Le Goater         return (delay & 0x7) < 1;
8375258c2a6SCédric Le Goater     case 2: /* at least two HCLK cycle delay */
8385258c2a6SCédric Le Goater         return (delay & 0x7) < 2;
8395258c2a6SCédric Le Goater     case 1: /* (> 100MHz) is above the max freq of the controller */
8405258c2a6SCédric Le Goater         return true;
8415258c2a6SCédric Le Goater     default:
8425258c2a6SCédric Le Goater         g_assert_not_reached();
8435258c2a6SCédric Le Goater     }
8445258c2a6SCédric Le Goater }
8455258c2a6SCédric Le Goater 
8465258c2a6SCédric Le Goater /*
847c4e1f0b4SCédric Le Goater  * Accumulate the result of the reads to provide a checksum that will
848c4e1f0b4SCédric Le Goater  * be used to validate the read timing settings.
849c4e1f0b4SCédric Le Goater  */
850c4e1f0b4SCédric Le Goater static void aspeed_smc_dma_checksum(AspeedSMCState *s)
851c4e1f0b4SCédric Le Goater {
852c4e1f0b4SCédric Le Goater     MemTxResult result;
853c4e1f0b4SCédric Le Goater     uint32_t data;
854c4e1f0b4SCédric Le Goater 
855c4e1f0b4SCédric Le Goater     if (s->regs[R_DMA_CTRL] & DMA_CTRL_WRITE) {
85632c54bd0SCédric Le Goater         aspeed_smc_error("invalid direction for DMA checksum");
857c4e1f0b4SCédric Le Goater         return;
858c4e1f0b4SCédric Le Goater     }
859c4e1f0b4SCédric Le Goater 
8600d72c717SCédric Le Goater     if (s->regs[R_DMA_CTRL] & DMA_CTRL_CALIB) {
8610d72c717SCédric Le Goater         aspeed_smc_dma_calibration(s);
8620d72c717SCédric Le Goater     }
8630d72c717SCédric Le Goater 
864c4e1f0b4SCédric Le Goater     while (s->regs[R_DMA_LEN]) {
865c4e1f0b4SCédric Le Goater         data = address_space_ldl_le(&s->flash_as, s->regs[R_DMA_FLASH_ADDR],
866c4e1f0b4SCédric Le Goater                                     MEMTXATTRS_UNSPECIFIED, &result);
867c4e1f0b4SCédric Le Goater         if (result != MEMTX_OK) {
86832c54bd0SCédric Le Goater             aspeed_smc_error("Flash read failed @%08x",
86932c54bd0SCédric Le Goater                              s->regs[R_DMA_FLASH_ADDR]);
870c4e1f0b4SCédric Le Goater             return;
871c4e1f0b4SCédric Le Goater         }
872bd6ce9a6SCédric Le Goater         trace_aspeed_smc_dma_checksum(s->regs[R_DMA_FLASH_ADDR], data);
873c4e1f0b4SCédric Le Goater 
874c4e1f0b4SCédric Le Goater         /*
875c4e1f0b4SCédric Le Goater          * When the DMA is on-going, the DMA registers are updated
876c4e1f0b4SCédric Le Goater          * with the current working addresses and length.
877c4e1f0b4SCédric Le Goater          */
878c4e1f0b4SCédric Le Goater         s->regs[R_DMA_CHECKSUM] += data;
879c4e1f0b4SCédric Le Goater         s->regs[R_DMA_FLASH_ADDR] += 4;
880c4e1f0b4SCédric Le Goater         s->regs[R_DMA_LEN] -= 4;
881c4e1f0b4SCédric Le Goater     }
8825258c2a6SCédric Le Goater 
8835258c2a6SCédric Le Goater     if (s->inject_failure && aspeed_smc_inject_read_failure(s)) {
8845258c2a6SCédric Le Goater         s->regs[R_DMA_CHECKSUM] = 0xbadc0de;
8855258c2a6SCédric Le Goater     }
8865258c2a6SCédric Le Goater 
887c4e1f0b4SCédric Le Goater }
888c4e1f0b4SCédric Le Goater 
889c4e1f0b4SCédric Le Goater static void aspeed_smc_dma_rw(AspeedSMCState *s)
890c4e1f0b4SCédric Le Goater {
891c4e1f0b4SCédric Le Goater     MemTxResult result;
892c4e1f0b4SCédric Le Goater     uint32_t data;
893c4e1f0b4SCédric Le Goater 
8944dabf395SCédric Le Goater     trace_aspeed_smc_dma_rw(s->regs[R_DMA_CTRL] & DMA_CTRL_WRITE ?
8954dabf395SCédric Le Goater                             "write" : "read",
8964dabf395SCédric Le Goater                             s->regs[R_DMA_FLASH_ADDR],
8974dabf395SCédric Le Goater                             s->regs[R_DMA_DRAM_ADDR],
8984dabf395SCédric Le Goater                             s->regs[R_DMA_LEN]);
899c4e1f0b4SCédric Le Goater     while (s->regs[R_DMA_LEN]) {
900c4e1f0b4SCédric Le Goater         if (s->regs[R_DMA_CTRL] & DMA_CTRL_WRITE) {
901c4e1f0b4SCédric Le Goater             data = address_space_ldl_le(&s->dram_as, s->regs[R_DMA_DRAM_ADDR],
902c4e1f0b4SCédric Le Goater                                         MEMTXATTRS_UNSPECIFIED, &result);
903c4e1f0b4SCédric Le Goater             if (result != MEMTX_OK) {
90432c54bd0SCédric Le Goater                 aspeed_smc_error("DRAM read failed @%08x",
90532c54bd0SCédric Le Goater                                  s->regs[R_DMA_DRAM_ADDR]);
906c4e1f0b4SCédric Le Goater                 return;
907c4e1f0b4SCédric Le Goater             }
908c4e1f0b4SCédric Le Goater 
909c4e1f0b4SCédric Le Goater             address_space_stl_le(&s->flash_as, s->regs[R_DMA_FLASH_ADDR],
910c4e1f0b4SCédric Le Goater                                  data, MEMTXATTRS_UNSPECIFIED, &result);
911c4e1f0b4SCédric Le Goater             if (result != MEMTX_OK) {
91232c54bd0SCédric Le Goater                 aspeed_smc_error("Flash write failed @%08x",
91332c54bd0SCédric Le Goater                                  s->regs[R_DMA_FLASH_ADDR]);
914c4e1f0b4SCédric Le Goater                 return;
915c4e1f0b4SCédric Le Goater             }
916c4e1f0b4SCédric Le Goater         } else {
917c4e1f0b4SCédric Le Goater             data = address_space_ldl_le(&s->flash_as, s->regs[R_DMA_FLASH_ADDR],
918c4e1f0b4SCédric Le Goater                                         MEMTXATTRS_UNSPECIFIED, &result);
919c4e1f0b4SCédric Le Goater             if (result != MEMTX_OK) {
92032c54bd0SCédric Le Goater                 aspeed_smc_error("Flash read failed @%08x",
92132c54bd0SCédric Le Goater                                  s->regs[R_DMA_FLASH_ADDR]);
922c4e1f0b4SCédric Le Goater                 return;
923c4e1f0b4SCédric Le Goater             }
924c4e1f0b4SCédric Le Goater 
925c4e1f0b4SCédric Le Goater             address_space_stl_le(&s->dram_as, s->regs[R_DMA_DRAM_ADDR],
926c4e1f0b4SCédric Le Goater                                  data, MEMTXATTRS_UNSPECIFIED, &result);
927c4e1f0b4SCédric Le Goater             if (result != MEMTX_OK) {
92832c54bd0SCédric Le Goater                 aspeed_smc_error("DRAM write failed @%08x",
92932c54bd0SCédric Le Goater                                  s->regs[R_DMA_DRAM_ADDR]);
930c4e1f0b4SCédric Le Goater                 return;
931c4e1f0b4SCédric Le Goater             }
932c4e1f0b4SCédric Le Goater         }
933c4e1f0b4SCédric Le Goater 
934c4e1f0b4SCédric Le Goater         /*
935c4e1f0b4SCédric Le Goater          * When the DMA is on-going, the DMA registers are updated
936c4e1f0b4SCédric Le Goater          * with the current working addresses and length.
937c4e1f0b4SCédric Le Goater          */
938c4e1f0b4SCédric Le Goater         s->regs[R_DMA_FLASH_ADDR] += 4;
939c4e1f0b4SCédric Le Goater         s->regs[R_DMA_DRAM_ADDR] += 4;
940c4e1f0b4SCédric Le Goater         s->regs[R_DMA_LEN] -= 4;
941ae275f71SChristian Svensson         s->regs[R_DMA_CHECKSUM] += data;
942c4e1f0b4SCédric Le Goater     }
943c4e1f0b4SCédric Le Goater }
944c4e1f0b4SCédric Le Goater 
945c4e1f0b4SCédric Le Goater static void aspeed_smc_dma_stop(AspeedSMCState *s)
946c4e1f0b4SCédric Le Goater {
947c4e1f0b4SCédric Le Goater     /*
948c4e1f0b4SCédric Le Goater      * When the DMA is disabled, INTR_CTRL_DMA_STATUS=0 means the
949c4e1f0b4SCédric Le Goater      * engine is idle
950c4e1f0b4SCédric Le Goater      */
951c4e1f0b4SCédric Le Goater     s->regs[R_INTR_CTRL] &= ~INTR_CTRL_DMA_STATUS;
952c4e1f0b4SCédric Le Goater     s->regs[R_DMA_CHECKSUM] = 0;
953c4e1f0b4SCédric Le Goater 
954c4e1f0b4SCédric Le Goater     /*
955c4e1f0b4SCédric Le Goater      * Lower the DMA irq in any case. The IRQ control register could
956c4e1f0b4SCédric Le Goater      * have been cleared before disabling the DMA.
957c4e1f0b4SCédric Le Goater      */
958c4e1f0b4SCédric Le Goater     qemu_irq_lower(s->irq);
959c4e1f0b4SCédric Le Goater }
960c4e1f0b4SCédric Le Goater 
961c4e1f0b4SCédric Le Goater /*
962c4e1f0b4SCédric Le Goater  * When INTR_CTRL_DMA_STATUS=1, the DMA has completed and a new DMA
963c4e1f0b4SCédric Le Goater  * can start even if the result of the previous was not collected.
964c4e1f0b4SCédric Le Goater  */
965c4e1f0b4SCédric Le Goater static bool aspeed_smc_dma_in_progress(AspeedSMCState *s)
966c4e1f0b4SCédric Le Goater {
967c4e1f0b4SCédric Le Goater     return s->regs[R_DMA_CTRL] & DMA_CTRL_ENABLE &&
968c4e1f0b4SCédric Le Goater         !(s->regs[R_INTR_CTRL] & INTR_CTRL_DMA_STATUS);
969c4e1f0b4SCédric Le Goater }
970c4e1f0b4SCédric Le Goater 
971c4e1f0b4SCédric Le Goater static void aspeed_smc_dma_done(AspeedSMCState *s)
972c4e1f0b4SCédric Le Goater {
973c4e1f0b4SCédric Le Goater     s->regs[R_INTR_CTRL] |= INTR_CTRL_DMA_STATUS;
974c4e1f0b4SCédric Le Goater     if (s->regs[R_INTR_CTRL] & INTR_CTRL_DMA_EN) {
975c4e1f0b4SCédric Le Goater         qemu_irq_raise(s->irq);
976c4e1f0b4SCédric Le Goater     }
977c4e1f0b4SCédric Le Goater }
978c4e1f0b4SCédric Le Goater 
9791769a70eSCédric Le Goater static void aspeed_smc_dma_ctrl(AspeedSMCState *s, uint32_t dma_ctrl)
980c4e1f0b4SCédric Le Goater {
981c4e1f0b4SCédric Le Goater     if (!(dma_ctrl & DMA_CTRL_ENABLE)) {
982c4e1f0b4SCédric Le Goater         s->regs[R_DMA_CTRL] = dma_ctrl;
983c4e1f0b4SCédric Le Goater 
984c4e1f0b4SCédric Le Goater         aspeed_smc_dma_stop(s);
985c4e1f0b4SCédric Le Goater         return;
986c4e1f0b4SCédric Le Goater     }
987c4e1f0b4SCédric Le Goater 
988c4e1f0b4SCédric Le Goater     if (aspeed_smc_dma_in_progress(s)) {
98932c54bd0SCédric Le Goater         aspeed_smc_error("DMA in progress !");
990c4e1f0b4SCédric Le Goater         return;
991c4e1f0b4SCédric Le Goater     }
992c4e1f0b4SCédric Le Goater 
993c4e1f0b4SCédric Le Goater     s->regs[R_DMA_CTRL] = dma_ctrl;
994c4e1f0b4SCédric Le Goater 
995c4e1f0b4SCédric Le Goater     if (s->regs[R_DMA_CTRL] & DMA_CTRL_CKSUM) {
996c4e1f0b4SCédric Le Goater         aspeed_smc_dma_checksum(s);
997c4e1f0b4SCédric Le Goater     } else {
998c4e1f0b4SCédric Le Goater         aspeed_smc_dma_rw(s);
999c4e1f0b4SCédric Le Goater     }
1000c4e1f0b4SCédric Le Goater 
1001c4e1f0b4SCédric Le Goater     aspeed_smc_dma_done(s);
1002c4e1f0b4SCédric Le Goater }
1003c4e1f0b4SCédric Le Goater 
10041769a70eSCédric Le Goater static inline bool aspeed_smc_dma_granted(AspeedSMCState *s)
10051769a70eSCédric Le Goater {
1006*30b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_GET_CLASS(s);
1007*30b6852cSCédric Le Goater 
1008*30b6852cSCédric Le Goater     if (!(asc->features & ASPEED_SMC_FEATURE_DMA_GRANT)) {
10091769a70eSCédric Le Goater         return true;
10101769a70eSCédric Le Goater     }
10111769a70eSCédric Le Goater 
10121769a70eSCédric Le Goater     if (!(s->regs[R_DMA_CTRL] & DMA_CTRL_GRANT)) {
101332c54bd0SCédric Le Goater         aspeed_smc_error("DMA not granted");
10141769a70eSCédric Le Goater         return false;
10151769a70eSCédric Le Goater     }
10161769a70eSCédric Le Goater 
10171769a70eSCédric Le Goater     return true;
10181769a70eSCédric Le Goater }
10191769a70eSCédric Le Goater 
10201769a70eSCédric Le Goater static void aspeed_2600_smc_dma_ctrl(AspeedSMCState *s, uint32_t dma_ctrl)
10211769a70eSCédric Le Goater {
10221769a70eSCédric Le Goater     /* Preserve DMA bits  */
10231769a70eSCédric Le Goater     dma_ctrl |= s->regs[R_DMA_CTRL] & (DMA_CTRL_REQUEST | DMA_CTRL_GRANT);
10241769a70eSCédric Le Goater 
10251769a70eSCédric Le Goater     if (dma_ctrl == 0xAEED0000) {
10261769a70eSCédric Le Goater         /* automatically grant request */
10271769a70eSCédric Le Goater         s->regs[R_DMA_CTRL] |= (DMA_CTRL_REQUEST | DMA_CTRL_GRANT);
10281769a70eSCédric Le Goater         return;
10291769a70eSCédric Le Goater     }
10301769a70eSCédric Le Goater 
10311769a70eSCédric Le Goater     /* clear request */
10321769a70eSCédric Le Goater     if (dma_ctrl == 0xDEEA0000) {
10331769a70eSCédric Le Goater         s->regs[R_DMA_CTRL] &= ~(DMA_CTRL_REQUEST | DMA_CTRL_GRANT);
10341769a70eSCédric Le Goater         return;
10351769a70eSCédric Le Goater     }
10361769a70eSCédric Le Goater 
10371769a70eSCédric Le Goater     if (!aspeed_smc_dma_granted(s)) {
103832c54bd0SCédric Le Goater         aspeed_smc_error("DMA not granted");
10391769a70eSCédric Le Goater         return;
10401769a70eSCédric Le Goater     }
10411769a70eSCédric Le Goater 
10421769a70eSCédric Le Goater     aspeed_smc_dma_ctrl(s, dma_ctrl);
10431769a70eSCédric Le Goater     s->regs[R_DMA_CTRL] &= ~(DMA_CTRL_REQUEST | DMA_CTRL_GRANT);
10441769a70eSCédric Le Goater }
10451769a70eSCédric Le Goater 
10467c1c69bcSCédric Le Goater static void aspeed_smc_write(void *opaque, hwaddr addr, uint64_t data,
10477c1c69bcSCédric Le Goater                              unsigned int size)
10487c1c69bcSCédric Le Goater {
10497c1c69bcSCédric Le Goater     AspeedSMCState *s = ASPEED_SMC(opaque);
1050*30b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_GET_CLASS(s);
10517c1c69bcSCédric Le Goater     uint32_t value = data;
10527c1c69bcSCédric Le Goater 
10537c1c69bcSCédric Le Goater     addr >>= 2;
10547c1c69bcSCédric Le Goater 
1055bd6ce9a6SCédric Le Goater     trace_aspeed_smc_write(addr, size, data);
1056bd6ce9a6SCédric Le Goater 
105797c2ed5dSCédric Le Goater     if (addr == s->r_conf ||
1058f286f04cSCédric Le Goater         (addr >= s->r_timings &&
1059*30b6852cSCédric Le Goater          addr < s->r_timings + asc->nregs_timings) ||
106097c2ed5dSCédric Le Goater         addr == s->r_ce_ctrl) {
106197c2ed5dSCédric Le Goater         s->regs[addr] = value;
106297c2ed5dSCédric Le Goater     } else if (addr >= s->r_ctrl0 && addr < s->r_ctrl0 + s->num_cs) {
1063f248a9dbSCédric Le Goater         int cs = addr - s->r_ctrl0;
1064e7e741caSCédric Le Goater         aspeed_smc_flash_update_ctrl(&s->flashes[cs], value);
1065a03cb1daSCédric Le Goater     } else if (addr >= R_SEG_ADDR0 &&
1066*30b6852cSCédric Le Goater                addr < R_SEG_ADDR0 + asc->max_peripherals) {
1067a03cb1daSCédric Le Goater         int cs = addr - R_SEG_ADDR0;
1068a03cb1daSCédric Le Goater 
1069a03cb1daSCédric Le Goater         if (value != s->regs[R_SEG_ADDR0 + cs]) {
1070a03cb1daSCédric Le Goater             aspeed_smc_flash_set_segment(s, cs, value);
1071a03cb1daSCédric Le Goater         }
1072af453a5eSCédric Le Goater     } else if (addr == R_CE_CMD_CTRL) {
1073af453a5eSCédric Le Goater         s->regs[addr] = value & 0xff;
10749149af2aSCédric Le Goater     } else if (addr == R_DUMMY_DATA) {
10759149af2aSCédric Le Goater         s->regs[addr] = value & 0xff;
1076*30b6852cSCédric Le Goater     } else if (aspeed_smc_has_wdt_control(asc) && addr == R_FMC_WDT2_CTRL) {
107745a904afSCédric Le Goater         s->regs[addr] = value & FMC_WDT2_CTRL_EN;
1078c4e1f0b4SCédric Le Goater     } else if (addr == R_INTR_CTRL) {
1079c4e1f0b4SCédric Le Goater         s->regs[addr] = value;
1080*30b6852cSCédric Le Goater     } else if (aspeed_smc_has_dma(asc) && addr == R_DMA_CTRL) {
1081*30b6852cSCédric Le Goater         asc->dma_ctrl(s, value);
1082*30b6852cSCédric Le Goater     } else if (aspeed_smc_has_dma(asc) && addr == R_DMA_DRAM_ADDR &&
10831769a70eSCédric Le Goater                aspeed_smc_dma_granted(s)) {
1084*30b6852cSCédric Le Goater         s->regs[addr] = DMA_DRAM_ADDR(asc, value);
1085*30b6852cSCédric Le Goater     } else if (aspeed_smc_has_dma(asc) && addr == R_DMA_FLASH_ADDR &&
10861769a70eSCédric Le Goater                aspeed_smc_dma_granted(s)) {
1087*30b6852cSCédric Le Goater         s->regs[addr] = DMA_FLASH_ADDR(asc, value);
1088*30b6852cSCédric Le Goater     } else if (aspeed_smc_has_dma(asc) && addr == R_DMA_LEN &&
10891769a70eSCédric Le Goater                aspeed_smc_dma_granted(s)) {
1090c4e1f0b4SCédric Le Goater         s->regs[addr] = DMA_LENGTH(value);
109197c2ed5dSCédric Le Goater     } else {
10927c1c69bcSCédric Le Goater         qemu_log_mask(LOG_UNIMP, "%s: not implemented: 0x%" HWADDR_PRIx "\n",
10937c1c69bcSCédric Le Goater                       __func__, addr);
10947c1c69bcSCédric Le Goater         return;
10957c1c69bcSCédric Le Goater     }
10967c1c69bcSCédric Le Goater }
10977c1c69bcSCédric Le Goater 
10987c1c69bcSCédric Le Goater static const MemoryRegionOps aspeed_smc_ops = {
10997c1c69bcSCédric Le Goater     .read = aspeed_smc_read,
11007c1c69bcSCédric Le Goater     .write = aspeed_smc_write,
11017c1c69bcSCédric Le Goater     .endianness = DEVICE_LITTLE_ENDIAN,
11027c1c69bcSCédric Le Goater };
11037c1c69bcSCédric Le Goater 
1104c4e1f0b4SCédric Le Goater /*
1105c4e1f0b4SCédric Le Goater  * Initialize the custom address spaces for DMAs
1106c4e1f0b4SCédric Le Goater  */
1107c4e1f0b4SCédric Le Goater static void aspeed_smc_dma_setup(AspeedSMCState *s, Error **errp)
1108c4e1f0b4SCédric Le Goater {
1109c4e1f0b4SCédric Le Goater     if (!s->dram_mr) {
1110c4e1f0b4SCédric Le Goater         error_setg(errp, TYPE_ASPEED_SMC ": 'dram' link not set");
1111c4e1f0b4SCédric Le Goater         return;
1112c4e1f0b4SCédric Le Goater     }
1113c4e1f0b4SCédric Le Goater 
1114d0180a3aSCédric Le Goater     address_space_init(&s->flash_as, &s->mmio_flash,
1115d0180a3aSCédric Le Goater                        TYPE_ASPEED_SMC ".dma-flash");
1116d0180a3aSCédric Le Goater     address_space_init(&s->dram_as, s->dram_mr,
1117d0180a3aSCédric Le Goater                        TYPE_ASPEED_SMC ".dma-dram");
1118c4e1f0b4SCédric Le Goater }
1119c4e1f0b4SCédric Le Goater 
11207c1c69bcSCédric Le Goater static void aspeed_smc_realize(DeviceState *dev, Error **errp)
11217c1c69bcSCédric Le Goater {
11227c1c69bcSCédric Le Goater     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
11237c1c69bcSCédric Le Goater     AspeedSMCState *s = ASPEED_SMC(dev);
1124*30b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_GET_CLASS(s);
11257c1c69bcSCédric Le Goater     int i;
1126924ed163SCédric Le Goater     char name[32];
1127924ed163SCédric Le Goater     hwaddr offset = 0;
11287c1c69bcSCédric Le Goater 
11297c1c69bcSCédric Le Goater     /* keep a copy under AspeedSMCState to speed up accesses */
1130*30b6852cSCédric Le Goater     s->r_conf = asc->r_conf;
1131*30b6852cSCédric Le Goater     s->r_ce_ctrl = asc->r_ce_ctrl;
1132*30b6852cSCédric Le Goater     s->r_ctrl0 = asc->r_ctrl0;
1133*30b6852cSCédric Le Goater     s->r_timings = asc->r_timings;
1134*30b6852cSCédric Le Goater     s->conf_enable_w0 = asc->conf_enable_w0;
11357c1c69bcSCédric Le Goater 
11367c1c69bcSCédric Le Goater     /* Enforce some real HW limits */
1137*30b6852cSCédric Le Goater     if (s->num_cs > asc->max_peripherals) {
1138*30b6852cSCédric Le Goater         aspeed_smc_error("num_cs cannot exceed: %d", asc->max_peripherals);
1139*30b6852cSCédric Le Goater         s->num_cs = asc->max_peripherals;
11407c1c69bcSCédric Le Goater     }
11417c1c69bcSCédric Le Goater 
1142c4e1f0b4SCédric Le Goater     /* DMA irq. Keep it first for the initialization in the SoC */
1143c4e1f0b4SCédric Le Goater     sysbus_init_irq(sbd, &s->irq);
1144c4e1f0b4SCédric Le Goater 
11457c1c69bcSCédric Le Goater     s->spi = ssi_create_bus(dev, "spi");
11467c1c69bcSCédric Le Goater 
11475ade579bSPhilippe Mathieu-Daudé     /* Setup cs_lines for peripherals */
11487c1c69bcSCédric Le Goater     s->cs_lines = g_new0(qemu_irq, s->num_cs);
11497c1c69bcSCédric Le Goater 
11507c1c69bcSCédric Le Goater     for (i = 0; i < s->num_cs; ++i) {
11517c1c69bcSCédric Le Goater         sysbus_init_irq(sbd, &s->cs_lines[i]);
11527c1c69bcSCédric Le Goater     }
11537c1c69bcSCédric Le Goater 
11542da95fd8SCédric Le Goater     /* The memory region for the controller registers */
11557c1c69bcSCédric Le Goater     memory_region_init_io(&s->mmio, OBJECT(s), &aspeed_smc_ops, s,
1156*30b6852cSCédric Le Goater                           TYPE_ASPEED_SMC, asc->nregs * 4);
11577c1c69bcSCédric Le Goater     sysbus_init_mmio(sbd, &s->mmio);
1158924ed163SCédric Le Goater 
1159924ed163SCédric Le Goater     /*
11602da95fd8SCédric Le Goater      * The container memory region representing the address space
11612da95fd8SCédric Le Goater      * window in which the flash modules are mapped. The size and
11622da95fd8SCédric Le Goater      * address depends on the SoC model and controller type.
1163924ed163SCédric Le Goater      */
1164924ed163SCédric Le Goater     memory_region_init_io(&s->mmio_flash, OBJECT(s),
1165d0180a3aSCédric Le Goater                           &aspeed_smc_flash_default_ops, s,
1166d0180a3aSCédric Le Goater                           TYPE_ASPEED_SMC ".flash",
1167*30b6852cSCédric Le Goater                           asc->flash_window_size);
1168d0180a3aSCédric Le Goater     memory_region_init_alias(&s->mmio_flash_alias, OBJECT(s),
1169d0180a3aSCédric Le Goater                              TYPE_ASPEED_SMC ".flash",
1170*30b6852cSCédric Le Goater                              &s->mmio_flash, 0, asc->flash_window_size);
1171e9c568dbSPhilippe Mathieu-Daudé     sysbus_init_mmio(sbd, &s->mmio_flash_alias);
1172924ed163SCédric Le Goater 
1173*30b6852cSCédric Le Goater     s->flashes = g_new0(AspeedSMCFlash, asc->max_peripherals);
1174924ed163SCédric Le Goater 
11752da95fd8SCédric Le Goater     /*
11765ade579bSPhilippe Mathieu-Daudé      * Let's create a sub memory region for each possible peripheral. All
11772da95fd8SCédric Le Goater      * have a configurable memory segment in the overall flash mapping
11782da95fd8SCédric Le Goater      * window of the controller but, there is not necessarily a flash
11792da95fd8SCédric Le Goater      * module behind to handle the memory accesses. This depends on
11802da95fd8SCédric Le Goater      * the board configuration.
11812da95fd8SCédric Le Goater      */
1182*30b6852cSCédric Le Goater     for (i = 0; i < asc->max_peripherals; ++i) {
1183924ed163SCédric Le Goater         AspeedSMCFlash *fl = &s->flashes[i];
1184924ed163SCédric Le Goater 
1185d0180a3aSCédric Le Goater         snprintf(name, sizeof(name), TYPE_ASPEED_SMC ".flash.%d", i);
1186924ed163SCédric Le Goater 
1187924ed163SCédric Le Goater         fl->id = i;
1188924ed163SCédric Le Goater         fl->controller = s;
1189*30b6852cSCédric Le Goater         fl->size = asc->segments[i].size;
1190924ed163SCédric Le Goater         memory_region_init_io(&fl->mmio, OBJECT(s), &aspeed_smc_flash_ops,
1191924ed163SCédric Le Goater                               fl, name, fl->size);
1192924ed163SCédric Le Goater         memory_region_add_subregion(&s->mmio_flash, offset, &fl->mmio);
1193924ed163SCédric Le Goater         offset += fl->size;
1194924ed163SCédric Le Goater     }
1195c4e1f0b4SCédric Le Goater 
1196c4e1f0b4SCédric Le Goater     /* DMA support */
1197*30b6852cSCédric Le Goater     if (aspeed_smc_has_dma(asc)) {
1198c4e1f0b4SCédric Le Goater         aspeed_smc_dma_setup(s, errp);
1199c4e1f0b4SCédric Le Goater     }
12007c1c69bcSCédric Le Goater }
12017c1c69bcSCédric Le Goater 
12027c1c69bcSCédric Le Goater static const VMStateDescription vmstate_aspeed_smc = {
12037c1c69bcSCédric Le Goater     .name = "aspeed.smc",
1204f95c4bffSCédric Le Goater     .version_id = 2,
1205f95c4bffSCédric Le Goater     .minimum_version_id = 2,
12067c1c69bcSCédric Le Goater     .fields = (VMStateField[]) {
12077c1c69bcSCédric Le Goater         VMSTATE_UINT32_ARRAY(regs, AspeedSMCState, ASPEED_SMC_R_MAX),
1208f95c4bffSCédric Le Goater         VMSTATE_UINT8(snoop_index, AspeedSMCState),
1209f95c4bffSCédric Le Goater         VMSTATE_UINT8(snoop_dummies, AspeedSMCState),
12107c1c69bcSCédric Le Goater         VMSTATE_END_OF_LIST()
12117c1c69bcSCédric Le Goater     }
12127c1c69bcSCédric Le Goater };
12137c1c69bcSCédric Le Goater 
12147c1c69bcSCédric Le Goater static Property aspeed_smc_properties[] = {
12157c1c69bcSCédric Le Goater     DEFINE_PROP_UINT32("num-cs", AspeedSMCState, num_cs, 1),
12165258c2a6SCédric Le Goater     DEFINE_PROP_BOOL("inject-failure", AspeedSMCState, inject_failure, false),
1217c4e1f0b4SCédric Le Goater     DEFINE_PROP_LINK("dram", AspeedSMCState, dram_mr,
1218c4e1f0b4SCédric Le Goater                      TYPE_MEMORY_REGION, MemoryRegion *),
12197c1c69bcSCédric Le Goater     DEFINE_PROP_END_OF_LIST(),
12207c1c69bcSCédric Le Goater };
12217c1c69bcSCédric Le Goater 
12227c1c69bcSCédric Le Goater static void aspeed_smc_class_init(ObjectClass *klass, void *data)
12237c1c69bcSCédric Le Goater {
12247c1c69bcSCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
12257c1c69bcSCédric Le Goater 
12267c1c69bcSCédric Le Goater     dc->realize = aspeed_smc_realize;
12277c1c69bcSCédric Le Goater     dc->reset = aspeed_smc_reset;
12284f67d30bSMarc-André Lureau     device_class_set_props(dc, aspeed_smc_properties);
12297c1c69bcSCédric Le Goater     dc->vmsd = &vmstate_aspeed_smc;
12307c1c69bcSCédric Le Goater }
12317c1c69bcSCédric Le Goater 
12327c1c69bcSCédric Le Goater static const TypeInfo aspeed_smc_info = {
12337c1c69bcSCédric Le Goater     .name           = TYPE_ASPEED_SMC,
12347c1c69bcSCédric Le Goater     .parent         = TYPE_SYS_BUS_DEVICE,
12357c1c69bcSCédric Le Goater     .instance_size  = sizeof(AspeedSMCState),
12367c1c69bcSCédric Le Goater     .class_size     = sizeof(AspeedSMCClass),
1237*30b6852cSCédric Le Goater     .class_init     = aspeed_smc_class_init,
12387c1c69bcSCédric Le Goater     .abstract       = true,
12397c1c69bcSCédric Le Goater };
12407c1c69bcSCédric Le Goater 
1241*30b6852cSCédric Le Goater 
1242*30b6852cSCédric Le Goater /*
1243*30b6852cSCédric Le Goater  * The Segment Registers of the AST2400 and AST2500 have a 8MB
1244*30b6852cSCédric Le Goater  * unit. The address range of a flash SPI peripheral is encoded with
1245*30b6852cSCédric Le Goater  * absolute addresses which should be part of the overall controller
1246*30b6852cSCédric Le Goater  * window.
1247*30b6852cSCédric Le Goater  */
1248*30b6852cSCédric Le Goater static uint32_t aspeed_smc_segment_to_reg(const AspeedSMCState *s,
1249*30b6852cSCédric Le Goater                                           const AspeedSegments *seg)
1250*30b6852cSCédric Le Goater {
1251*30b6852cSCédric Le Goater     uint32_t reg = 0;
1252*30b6852cSCédric Le Goater     reg |= ((seg->addr >> 23) & SEG_START_MASK) << SEG_START_SHIFT;
1253*30b6852cSCédric Le Goater     reg |= (((seg->addr + seg->size) >> 23) & SEG_END_MASK) << SEG_END_SHIFT;
1254*30b6852cSCédric Le Goater     return reg;
1255*30b6852cSCédric Le Goater }
1256*30b6852cSCédric Le Goater 
1257*30b6852cSCédric Le Goater static void aspeed_smc_reg_to_segment(const AspeedSMCState *s,
1258*30b6852cSCédric Le Goater                                       uint32_t reg, AspeedSegments *seg)
1259*30b6852cSCédric Le Goater {
1260*30b6852cSCédric Le Goater     seg->addr = ((reg >> SEG_START_SHIFT) & SEG_START_MASK) << 23;
1261*30b6852cSCédric Le Goater     seg->size = (((reg >> SEG_END_SHIFT) & SEG_END_MASK) << 23) - seg->addr;
1262*30b6852cSCédric Le Goater }
1263*30b6852cSCédric Le Goater 
1264*30b6852cSCédric Le Goater static const AspeedSegments aspeed_2400_smc_segments[] = {
1265*30b6852cSCédric Le Goater     { 0x10000000, 32 * MiB },
1266*30b6852cSCédric Le Goater };
1267*30b6852cSCédric Le Goater 
1268*30b6852cSCédric Le Goater static void aspeed_2400_smc_class_init(ObjectClass *klass, void *data)
1269*30b6852cSCédric Le Goater {
1270*30b6852cSCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
1271*30b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_CLASS(klass);
1272*30b6852cSCédric Le Goater 
1273*30b6852cSCédric Le Goater     dc->desc               = "Aspeed 2400 SMC Controller";
1274*30b6852cSCédric Le Goater     asc->r_conf            = R_CONF;
1275*30b6852cSCédric Le Goater     asc->r_ce_ctrl         = R_CE_CTRL;
1276*30b6852cSCédric Le Goater     asc->r_ctrl0           = R_CTRL0;
1277*30b6852cSCédric Le Goater     asc->r_timings         = R_TIMINGS;
1278*30b6852cSCédric Le Goater     asc->nregs_timings     = 1;
1279*30b6852cSCédric Le Goater     asc->conf_enable_w0    = CONF_ENABLE_W0;
1280*30b6852cSCédric Le Goater     asc->max_peripherals   = 1;
1281*30b6852cSCédric Le Goater     asc->segments          = aspeed_2400_smc_segments;
1282*30b6852cSCédric Le Goater     asc->flash_window_base = 0x10000000;
1283*30b6852cSCédric Le Goater     asc->flash_window_size = 0x6000000;
1284*30b6852cSCédric Le Goater     asc->features          = 0x0;
1285*30b6852cSCédric Le Goater     asc->nregs             = ASPEED_SMC_R_SMC_MAX;
1286*30b6852cSCédric Le Goater     asc->segment_to_reg    = aspeed_smc_segment_to_reg;
1287*30b6852cSCédric Le Goater     asc->reg_to_segment    = aspeed_smc_reg_to_segment;
1288*30b6852cSCédric Le Goater     asc->dma_ctrl          = aspeed_smc_dma_ctrl;
1289*30b6852cSCédric Le Goater }
1290*30b6852cSCédric Le Goater 
1291*30b6852cSCédric Le Goater static const TypeInfo aspeed_2400_smc_info = {
1292*30b6852cSCédric Le Goater     .name =  "aspeed.smc-ast2400",
1293*30b6852cSCédric Le Goater     .parent = TYPE_ASPEED_SMC,
1294*30b6852cSCédric Le Goater     .class_init = aspeed_2400_smc_class_init,
1295*30b6852cSCédric Le Goater };
1296*30b6852cSCédric Le Goater 
1297*30b6852cSCédric Le Goater static const AspeedSegments aspeed_2400_fmc_segments[] = {
1298*30b6852cSCédric Le Goater     { 0x20000000, 64 * MiB }, /* start address is readonly */
1299*30b6852cSCédric Le Goater     { 0x24000000, 32 * MiB },
1300*30b6852cSCédric Le Goater     { 0x26000000, 32 * MiB },
1301*30b6852cSCédric Le Goater     { 0x28000000, 32 * MiB },
1302*30b6852cSCédric Le Goater     { 0x2A000000, 32 * MiB }
1303*30b6852cSCédric Le Goater };
1304*30b6852cSCédric Le Goater 
1305*30b6852cSCédric Le Goater static void aspeed_2400_fmc_class_init(ObjectClass *klass, void *data)
1306*30b6852cSCédric Le Goater {
1307*30b6852cSCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
1308*30b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_CLASS(klass);
1309*30b6852cSCédric Le Goater 
1310*30b6852cSCédric Le Goater     dc->desc               = "Aspeed 2400 FMC Controller";
1311*30b6852cSCédric Le Goater     asc->r_conf            = R_CONF;
1312*30b6852cSCédric Le Goater     asc->r_ce_ctrl         = R_CE_CTRL;
1313*30b6852cSCédric Le Goater     asc->r_ctrl0           = R_CTRL0;
1314*30b6852cSCédric Le Goater     asc->r_timings         = R_TIMINGS;
1315*30b6852cSCédric Le Goater     asc->nregs_timings     = 1;
1316*30b6852cSCédric Le Goater     asc->conf_enable_w0    = CONF_ENABLE_W0;
1317*30b6852cSCédric Le Goater     asc->max_peripherals   = 5;
1318*30b6852cSCédric Le Goater     asc->segments          = aspeed_2400_fmc_segments;
1319*30b6852cSCédric Le Goater     asc->flash_window_base = 0x20000000;
1320*30b6852cSCédric Le Goater     asc->flash_window_size = 0x10000000;
1321*30b6852cSCédric Le Goater     asc->features          = ASPEED_SMC_FEATURE_DMA;
1322*30b6852cSCédric Le Goater     asc->dma_flash_mask    = 0x0FFFFFFC;
1323*30b6852cSCédric Le Goater     asc->dma_dram_mask     = 0x1FFFFFFC;
1324*30b6852cSCédric Le Goater     asc->nregs             = ASPEED_SMC_R_MAX;
1325*30b6852cSCédric Le Goater     asc->segment_to_reg    = aspeed_smc_segment_to_reg;
1326*30b6852cSCédric Le Goater     asc->reg_to_segment    = aspeed_smc_reg_to_segment;
1327*30b6852cSCédric Le Goater     asc->dma_ctrl          = aspeed_smc_dma_ctrl;
1328*30b6852cSCédric Le Goater }
1329*30b6852cSCédric Le Goater 
1330*30b6852cSCédric Le Goater static const TypeInfo aspeed_2400_fmc_info = {
1331*30b6852cSCédric Le Goater     .name =  "aspeed.fmc-ast2400",
1332*30b6852cSCédric Le Goater     .parent = TYPE_ASPEED_SMC,
1333*30b6852cSCédric Le Goater     .class_init = aspeed_2400_fmc_class_init,
1334*30b6852cSCédric Le Goater };
1335*30b6852cSCédric Le Goater 
1336*30b6852cSCédric Le Goater static const AspeedSegments aspeed_2400_spi1_segments[] = {
1337*30b6852cSCédric Le Goater     { 0x30000000, 64 * MiB },
1338*30b6852cSCédric Le Goater };
1339*30b6852cSCédric Le Goater 
1340*30b6852cSCédric Le Goater static void aspeed_2400_spi1_class_init(ObjectClass *klass, void *data)
1341*30b6852cSCédric Le Goater {
1342*30b6852cSCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
1343*30b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_CLASS(klass);
1344*30b6852cSCédric Le Goater 
1345*30b6852cSCédric Le Goater     dc->desc               = "Aspeed 2400 SPI1 Controller";
1346*30b6852cSCédric Le Goater     asc->r_conf            = R_SPI_CONF;
1347*30b6852cSCédric Le Goater     asc->r_ce_ctrl         = 0xff;
1348*30b6852cSCédric Le Goater     asc->r_ctrl0           = R_SPI_CTRL0;
1349*30b6852cSCédric Le Goater     asc->r_timings         = R_SPI_TIMINGS;
1350*30b6852cSCédric Le Goater     asc->nregs_timings     = 1;
1351*30b6852cSCédric Le Goater     asc->conf_enable_w0    = SPI_CONF_ENABLE_W0;
1352*30b6852cSCédric Le Goater     asc->max_peripherals   = 1;
1353*30b6852cSCédric Le Goater     asc->segments          = aspeed_2400_spi1_segments;
1354*30b6852cSCédric Le Goater     asc->flash_window_base = 0x30000000;
1355*30b6852cSCédric Le Goater     asc->flash_window_size = 0x10000000;
1356*30b6852cSCédric Le Goater     asc->features          = 0x0;
1357*30b6852cSCédric Le Goater     asc->nregs             = ASPEED_SMC_R_SPI_MAX;
1358*30b6852cSCédric Le Goater     asc->segment_to_reg    = aspeed_smc_segment_to_reg;
1359*30b6852cSCédric Le Goater     asc->reg_to_segment    = aspeed_smc_reg_to_segment;
1360*30b6852cSCédric Le Goater     asc->dma_ctrl          = aspeed_smc_dma_ctrl;
1361*30b6852cSCédric Le Goater }
1362*30b6852cSCédric Le Goater 
1363*30b6852cSCédric Le Goater static const TypeInfo aspeed_2400_spi1_info = {
1364*30b6852cSCédric Le Goater     .name =  "aspeed.spi1-ast2400",
1365*30b6852cSCédric Le Goater     .parent = TYPE_ASPEED_SMC,
1366*30b6852cSCédric Le Goater     .class_init = aspeed_2400_spi1_class_init,
1367*30b6852cSCédric Le Goater };
1368*30b6852cSCédric Le Goater 
1369*30b6852cSCédric Le Goater static const AspeedSegments aspeed_2500_fmc_segments[] = {
1370*30b6852cSCédric Le Goater     { 0x20000000, 128 * MiB }, /* start address is readonly */
1371*30b6852cSCédric Le Goater     { 0x28000000,  32 * MiB },
1372*30b6852cSCédric Le Goater     { 0x2A000000,  32 * MiB },
1373*30b6852cSCédric Le Goater };
1374*30b6852cSCédric Le Goater 
1375*30b6852cSCédric Le Goater static void aspeed_2500_fmc_class_init(ObjectClass *klass, void *data)
1376*30b6852cSCédric Le Goater {
1377*30b6852cSCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
1378*30b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_CLASS(klass);
1379*30b6852cSCédric Le Goater 
1380*30b6852cSCédric Le Goater     dc->desc               = "Aspeed 2600 FMC Controller";
1381*30b6852cSCédric Le Goater     asc->r_conf            = R_CONF;
1382*30b6852cSCédric Le Goater     asc->r_ce_ctrl         = R_CE_CTRL;
1383*30b6852cSCédric Le Goater     asc->r_ctrl0           = R_CTRL0;
1384*30b6852cSCédric Le Goater     asc->r_timings         = R_TIMINGS;
1385*30b6852cSCédric Le Goater     asc->nregs_timings     = 1;
1386*30b6852cSCédric Le Goater     asc->conf_enable_w0    = CONF_ENABLE_W0;
1387*30b6852cSCédric Le Goater     asc->max_peripherals   = 3;
1388*30b6852cSCédric Le Goater     asc->segments          = aspeed_2500_fmc_segments;
1389*30b6852cSCédric Le Goater     asc->flash_window_base = 0x20000000;
1390*30b6852cSCédric Le Goater     asc->flash_window_size = 0x10000000;
1391*30b6852cSCédric Le Goater     asc->features          = ASPEED_SMC_FEATURE_DMA;
1392*30b6852cSCédric Le Goater     asc->dma_flash_mask    = 0x0FFFFFFC;
1393*30b6852cSCédric Le Goater     asc->dma_dram_mask     = 0x3FFFFFFC;
1394*30b6852cSCédric Le Goater     asc->nregs             = ASPEED_SMC_R_MAX;
1395*30b6852cSCédric Le Goater     asc->segment_to_reg    = aspeed_smc_segment_to_reg;
1396*30b6852cSCédric Le Goater     asc->reg_to_segment    = aspeed_smc_reg_to_segment;
1397*30b6852cSCédric Le Goater     asc->dma_ctrl          = aspeed_smc_dma_ctrl;
1398*30b6852cSCédric Le Goater }
1399*30b6852cSCédric Le Goater 
1400*30b6852cSCédric Le Goater static const TypeInfo aspeed_2500_fmc_info = {
1401*30b6852cSCédric Le Goater     .name =  "aspeed.fmc-ast2500",
1402*30b6852cSCédric Le Goater     .parent = TYPE_ASPEED_SMC,
1403*30b6852cSCédric Le Goater     .class_init = aspeed_2500_fmc_class_init,
1404*30b6852cSCédric Le Goater };
1405*30b6852cSCédric Le Goater 
1406*30b6852cSCédric Le Goater static const AspeedSegments aspeed_2500_spi1_segments[] = {
1407*30b6852cSCédric Le Goater     { 0x30000000, 32 * MiB }, /* start address is readonly */
1408*30b6852cSCédric Le Goater     { 0x32000000, 96 * MiB }, /* end address is readonly */
1409*30b6852cSCédric Le Goater };
1410*30b6852cSCédric Le Goater 
1411*30b6852cSCédric Le Goater static void aspeed_2500_spi1_class_init(ObjectClass *klass, void *data)
1412*30b6852cSCédric Le Goater {
1413*30b6852cSCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
1414*30b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_CLASS(klass);
1415*30b6852cSCédric Le Goater 
1416*30b6852cSCédric Le Goater     dc->desc               = "Aspeed 2600 SPI1 Controller";
1417*30b6852cSCédric Le Goater     asc->r_conf            = R_CONF;
1418*30b6852cSCédric Le Goater     asc->r_ce_ctrl         = R_CE_CTRL;
1419*30b6852cSCédric Le Goater     asc->r_ctrl0           = R_CTRL0;
1420*30b6852cSCédric Le Goater     asc->r_timings         = R_TIMINGS;
1421*30b6852cSCédric Le Goater     asc->nregs_timings     = 1;
1422*30b6852cSCédric Le Goater     asc->conf_enable_w0    = CONF_ENABLE_W0;
1423*30b6852cSCédric Le Goater     asc->max_peripherals   = 2;
1424*30b6852cSCédric Le Goater     asc->segments          = aspeed_2500_spi1_segments;
1425*30b6852cSCédric Le Goater     asc->flash_window_base = 0x30000000;
1426*30b6852cSCédric Le Goater     asc->flash_window_size = 0x8000000;
1427*30b6852cSCédric Le Goater     asc->features          = 0x0;
1428*30b6852cSCédric Le Goater     asc->nregs             = ASPEED_SMC_R_MAX;
1429*30b6852cSCédric Le Goater     asc->segment_to_reg    = aspeed_smc_segment_to_reg;
1430*30b6852cSCédric Le Goater     asc->reg_to_segment    = aspeed_smc_reg_to_segment;
1431*30b6852cSCédric Le Goater     asc->dma_ctrl          = aspeed_smc_dma_ctrl;
1432*30b6852cSCédric Le Goater }
1433*30b6852cSCédric Le Goater 
1434*30b6852cSCédric Le Goater static const TypeInfo aspeed_2500_spi1_info = {
1435*30b6852cSCédric Le Goater     .name =  "aspeed.spi1-ast2500",
1436*30b6852cSCédric Le Goater     .parent = TYPE_ASPEED_SMC,
1437*30b6852cSCédric Le Goater     .class_init = aspeed_2500_spi1_class_init,
1438*30b6852cSCédric Le Goater };
1439*30b6852cSCédric Le Goater 
1440*30b6852cSCédric Le Goater static const AspeedSegments aspeed_2500_spi2_segments[] = {
1441*30b6852cSCédric Le Goater     { 0x38000000, 32 * MiB }, /* start address is readonly */
1442*30b6852cSCédric Le Goater     { 0x3A000000, 96 * MiB }, /* end address is readonly */
1443*30b6852cSCédric Le Goater };
1444*30b6852cSCédric Le Goater 
1445*30b6852cSCédric Le Goater static void aspeed_2500_spi2_class_init(ObjectClass *klass, void *data)
1446*30b6852cSCédric Le Goater {
1447*30b6852cSCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
1448*30b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_CLASS(klass);
1449*30b6852cSCédric Le Goater 
1450*30b6852cSCédric Le Goater     dc->desc               = "Aspeed 2600 SPI2 Controller";
1451*30b6852cSCédric Le Goater     asc->r_conf            = R_CONF;
1452*30b6852cSCédric Le Goater     asc->r_ce_ctrl         = R_CE_CTRL;
1453*30b6852cSCédric Le Goater     asc->r_ctrl0           = R_CTRL0;
1454*30b6852cSCédric Le Goater     asc->r_timings         = R_TIMINGS;
1455*30b6852cSCédric Le Goater     asc->nregs_timings     = 1;
1456*30b6852cSCédric Le Goater     asc->conf_enable_w0    = CONF_ENABLE_W0;
1457*30b6852cSCédric Le Goater     asc->max_peripherals   = 2;
1458*30b6852cSCédric Le Goater     asc->segments          = aspeed_2500_spi2_segments;
1459*30b6852cSCédric Le Goater     asc->flash_window_base = 0x38000000;
1460*30b6852cSCédric Le Goater     asc->flash_window_size = 0x8000000;
1461*30b6852cSCédric Le Goater     asc->features          = 0x0;
1462*30b6852cSCédric Le Goater     asc->nregs             = ASPEED_SMC_R_MAX;
1463*30b6852cSCédric Le Goater     asc->segment_to_reg    = aspeed_smc_segment_to_reg;
1464*30b6852cSCédric Le Goater     asc->reg_to_segment    = aspeed_smc_reg_to_segment;
1465*30b6852cSCédric Le Goater     asc->dma_ctrl          = aspeed_smc_dma_ctrl;
1466*30b6852cSCédric Le Goater }
1467*30b6852cSCédric Le Goater 
1468*30b6852cSCédric Le Goater static const TypeInfo aspeed_2500_spi2_info = {
1469*30b6852cSCédric Le Goater     .name =  "aspeed.spi2-ast2500",
1470*30b6852cSCédric Le Goater     .parent = TYPE_ASPEED_SMC,
1471*30b6852cSCédric Le Goater     .class_init = aspeed_2500_spi2_class_init,
1472*30b6852cSCédric Le Goater };
1473*30b6852cSCédric Le Goater 
1474*30b6852cSCédric Le Goater /*
1475*30b6852cSCédric Le Goater  * The Segment Registers of the AST2600 have a 1MB unit. The address
1476*30b6852cSCédric Le Goater  * range of a flash SPI peripheral is encoded with offsets in the overall
1477*30b6852cSCédric Le Goater  * controller window. The previous SoC AST2400 and AST2500 used
1478*30b6852cSCédric Le Goater  * absolute addresses. Only bits [27:20] are relevant and the end
1479*30b6852cSCédric Le Goater  * address is an upper bound limit.
1480*30b6852cSCédric Le Goater  */
1481*30b6852cSCédric Le Goater #define AST2600_SEG_ADDR_MASK 0x0ff00000
1482*30b6852cSCédric Le Goater 
1483*30b6852cSCédric Le Goater static uint32_t aspeed_2600_smc_segment_to_reg(const AspeedSMCState *s,
1484*30b6852cSCédric Le Goater                                                const AspeedSegments *seg)
1485*30b6852cSCédric Le Goater {
1486*30b6852cSCédric Le Goater     uint32_t reg = 0;
1487*30b6852cSCédric Le Goater 
1488*30b6852cSCédric Le Goater     /* Disabled segments have a nil register */
1489*30b6852cSCédric Le Goater     if (!seg->size) {
1490*30b6852cSCédric Le Goater         return 0;
1491*30b6852cSCédric Le Goater     }
1492*30b6852cSCédric Le Goater 
1493*30b6852cSCédric Le Goater     reg |= (seg->addr & AST2600_SEG_ADDR_MASK) >> 16; /* start offset */
1494*30b6852cSCédric Le Goater     reg |= (seg->addr + seg->size - 1) & AST2600_SEG_ADDR_MASK; /* end offset */
1495*30b6852cSCédric Le Goater     return reg;
1496*30b6852cSCédric Le Goater }
1497*30b6852cSCédric Le Goater 
1498*30b6852cSCédric Le Goater static void aspeed_2600_smc_reg_to_segment(const AspeedSMCState *s,
1499*30b6852cSCédric Le Goater                                            uint32_t reg, AspeedSegments *seg)
1500*30b6852cSCédric Le Goater {
1501*30b6852cSCédric Le Goater     uint32_t start_offset = (reg << 16) & AST2600_SEG_ADDR_MASK;
1502*30b6852cSCédric Le Goater     uint32_t end_offset = reg & AST2600_SEG_ADDR_MASK;
1503*30b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_GET_CLASS(s);
1504*30b6852cSCédric Le Goater 
1505*30b6852cSCédric Le Goater     if (reg) {
1506*30b6852cSCédric Le Goater         seg->addr = asc->flash_window_base + start_offset;
1507*30b6852cSCédric Le Goater         seg->size = end_offset + MiB - start_offset;
1508*30b6852cSCédric Le Goater     } else {
1509*30b6852cSCédric Le Goater         seg->addr = asc->flash_window_base;
1510*30b6852cSCédric Le Goater         seg->size = 0;
1511*30b6852cSCédric Le Goater     }
1512*30b6852cSCédric Le Goater }
1513*30b6852cSCédric Le Goater 
1514*30b6852cSCédric Le Goater static const AspeedSegments aspeed_2600_fmc_segments[] = {
1515*30b6852cSCédric Le Goater     { 0x0, 128 * MiB }, /* start address is readonly */
1516*30b6852cSCédric Le Goater     { 128 * MiB, 128 * MiB }, /* default is disabled but needed for -kernel */
1517*30b6852cSCédric Le Goater     { 0x0, 0 }, /* disabled */
1518*30b6852cSCédric Le Goater };
1519*30b6852cSCédric Le Goater 
1520*30b6852cSCédric Le Goater static void aspeed_2600_fmc_class_init(ObjectClass *klass, void *data)
1521*30b6852cSCédric Le Goater {
1522*30b6852cSCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
1523*30b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_CLASS(klass);
1524*30b6852cSCédric Le Goater 
1525*30b6852cSCédric Le Goater     dc->desc               = "Aspeed 2600 FMC Controller";
1526*30b6852cSCédric Le Goater     asc->r_conf            = R_CONF;
1527*30b6852cSCédric Le Goater     asc->r_ce_ctrl         = R_CE_CTRL;
1528*30b6852cSCédric Le Goater     asc->r_ctrl0           = R_CTRL0;
1529*30b6852cSCédric Le Goater     asc->r_timings         = R_TIMINGS;
1530*30b6852cSCédric Le Goater     asc->nregs_timings     = 1;
1531*30b6852cSCédric Le Goater     asc->conf_enable_w0    = CONF_ENABLE_W0;
1532*30b6852cSCédric Le Goater     asc->max_peripherals   = 3;
1533*30b6852cSCédric Le Goater     asc->segments          = aspeed_2600_fmc_segments;
1534*30b6852cSCédric Le Goater     asc->flash_window_base = 0x20000000;
1535*30b6852cSCédric Le Goater     asc->flash_window_size = 0x10000000;
1536*30b6852cSCédric Le Goater     asc->features          = ASPEED_SMC_FEATURE_DMA |
1537*30b6852cSCédric Le Goater                              ASPEED_SMC_FEATURE_WDT_CONTROL;
1538*30b6852cSCédric Le Goater     asc->dma_flash_mask    = 0x0FFFFFFC;
1539*30b6852cSCédric Le Goater     asc->dma_dram_mask     = 0x3FFFFFFC;
1540*30b6852cSCédric Le Goater     asc->nregs             = ASPEED_SMC_R_MAX;
1541*30b6852cSCédric Le Goater     asc->segment_to_reg    = aspeed_2600_smc_segment_to_reg;
1542*30b6852cSCédric Le Goater     asc->reg_to_segment    = aspeed_2600_smc_reg_to_segment;
1543*30b6852cSCédric Le Goater     asc->dma_ctrl          = aspeed_2600_smc_dma_ctrl;
1544*30b6852cSCédric Le Goater }
1545*30b6852cSCédric Le Goater 
1546*30b6852cSCédric Le Goater static const TypeInfo aspeed_2600_fmc_info = {
1547*30b6852cSCédric Le Goater     .name =  "aspeed.fmc-ast2600",
1548*30b6852cSCédric Le Goater     .parent = TYPE_ASPEED_SMC,
1549*30b6852cSCédric Le Goater     .class_init = aspeed_2600_fmc_class_init,
1550*30b6852cSCédric Le Goater };
1551*30b6852cSCédric Le Goater 
1552*30b6852cSCédric Le Goater static const AspeedSegments aspeed_2600_spi1_segments[] = {
1553*30b6852cSCédric Le Goater     { 0x0, 128 * MiB }, /* start address is readonly */
1554*30b6852cSCédric Le Goater     { 0x0, 0 }, /* disabled */
1555*30b6852cSCédric Le Goater };
1556*30b6852cSCédric Le Goater 
1557*30b6852cSCédric Le Goater static void aspeed_2600_spi1_class_init(ObjectClass *klass, void *data)
1558*30b6852cSCédric Le Goater {
1559*30b6852cSCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
1560*30b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_CLASS(klass);
1561*30b6852cSCédric Le Goater 
1562*30b6852cSCédric Le Goater     dc->desc               = "Aspeed 2600 SPI1 Controller";
1563*30b6852cSCédric Le Goater     asc->r_conf            = R_CONF;
1564*30b6852cSCédric Le Goater     asc->r_ce_ctrl         = R_CE_CTRL;
1565*30b6852cSCédric Le Goater     asc->r_ctrl0           = R_CTRL0;
1566*30b6852cSCédric Le Goater     asc->r_timings         = R_TIMINGS;
1567*30b6852cSCédric Le Goater     asc->nregs_timings     = 2;
1568*30b6852cSCédric Le Goater     asc->conf_enable_w0    = CONF_ENABLE_W0;
1569*30b6852cSCédric Le Goater     asc->max_peripherals   = 2;
1570*30b6852cSCédric Le Goater     asc->segments          = aspeed_2600_spi1_segments;
1571*30b6852cSCédric Le Goater     asc->flash_window_base = 0x30000000;
1572*30b6852cSCédric Le Goater     asc->flash_window_size = 0x10000000;
1573*30b6852cSCédric Le Goater     asc->features          = ASPEED_SMC_FEATURE_DMA |
1574*30b6852cSCédric Le Goater                              ASPEED_SMC_FEATURE_DMA_GRANT;
1575*30b6852cSCédric Le Goater     asc->dma_flash_mask    = 0x0FFFFFFC;
1576*30b6852cSCédric Le Goater     asc->dma_dram_mask     = 0x3FFFFFFC;
1577*30b6852cSCédric Le Goater     asc->nregs             = ASPEED_SMC_R_MAX;
1578*30b6852cSCédric Le Goater     asc->segment_to_reg    = aspeed_2600_smc_segment_to_reg;
1579*30b6852cSCédric Le Goater     asc->reg_to_segment    = aspeed_2600_smc_reg_to_segment;
1580*30b6852cSCédric Le Goater     asc->dma_ctrl          = aspeed_2600_smc_dma_ctrl;
1581*30b6852cSCédric Le Goater }
1582*30b6852cSCédric Le Goater 
1583*30b6852cSCédric Le Goater static const TypeInfo aspeed_2600_spi1_info = {
1584*30b6852cSCédric Le Goater     .name =  "aspeed.spi1-ast2600",
1585*30b6852cSCédric Le Goater     .parent = TYPE_ASPEED_SMC,
1586*30b6852cSCédric Le Goater     .class_init = aspeed_2600_spi1_class_init,
1587*30b6852cSCédric Le Goater };
1588*30b6852cSCédric Le Goater 
1589*30b6852cSCédric Le Goater static const AspeedSegments aspeed_2600_spi2_segments[] = {
1590*30b6852cSCédric Le Goater     { 0x0, 128 * MiB }, /* start address is readonly */
1591*30b6852cSCédric Le Goater     { 0x0, 0 }, /* disabled */
1592*30b6852cSCédric Le Goater     { 0x0, 0 }, /* disabled */
1593*30b6852cSCédric Le Goater };
1594*30b6852cSCédric Le Goater 
1595*30b6852cSCédric Le Goater static void aspeed_2600_spi2_class_init(ObjectClass *klass, void *data)
1596*30b6852cSCédric Le Goater {
1597*30b6852cSCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
1598*30b6852cSCédric Le Goater     AspeedSMCClass *asc = ASPEED_SMC_CLASS(klass);
1599*30b6852cSCédric Le Goater 
1600*30b6852cSCédric Le Goater     dc->desc               = "Aspeed 2600 SPI2 Controller";
1601*30b6852cSCédric Le Goater     asc->r_conf            = R_CONF;
1602*30b6852cSCédric Le Goater     asc->r_ce_ctrl         = R_CE_CTRL;
1603*30b6852cSCédric Le Goater     asc->r_ctrl0           = R_CTRL0;
1604*30b6852cSCédric Le Goater     asc->r_timings         = R_TIMINGS;
1605*30b6852cSCédric Le Goater     asc->nregs_timings     = 3;
1606*30b6852cSCédric Le Goater     asc->conf_enable_w0    = CONF_ENABLE_W0;
1607*30b6852cSCédric Le Goater     asc->max_peripherals   = 3;
1608*30b6852cSCédric Le Goater     asc->segments          = aspeed_2600_spi2_segments;
1609*30b6852cSCédric Le Goater     asc->flash_window_base = 0x50000000;
1610*30b6852cSCédric Le Goater     asc->flash_window_size = 0x10000000;
1611*30b6852cSCédric Le Goater     asc->features          = ASPEED_SMC_FEATURE_DMA |
1612*30b6852cSCédric Le Goater                              ASPEED_SMC_FEATURE_DMA_GRANT;
1613*30b6852cSCédric Le Goater     asc->dma_flash_mask    = 0x0FFFFFFC;
1614*30b6852cSCédric Le Goater     asc->dma_dram_mask     = 0x3FFFFFFC;
1615*30b6852cSCédric Le Goater     asc->nregs             = ASPEED_SMC_R_MAX;
1616*30b6852cSCédric Le Goater     asc->segment_to_reg    = aspeed_2600_smc_segment_to_reg;
1617*30b6852cSCédric Le Goater     asc->reg_to_segment    = aspeed_2600_smc_reg_to_segment;
1618*30b6852cSCédric Le Goater     asc->dma_ctrl          = aspeed_2600_smc_dma_ctrl;
1619*30b6852cSCédric Le Goater }
1620*30b6852cSCédric Le Goater 
1621*30b6852cSCédric Le Goater static const TypeInfo aspeed_2600_spi2_info = {
1622*30b6852cSCédric Le Goater     .name =  "aspeed.spi2-ast2600",
1623*30b6852cSCédric Le Goater     .parent = TYPE_ASPEED_SMC,
1624*30b6852cSCédric Le Goater     .class_init = aspeed_2600_spi2_class_init,
1625*30b6852cSCédric Le Goater };
1626*30b6852cSCédric Le Goater 
16277c1c69bcSCédric Le Goater static void aspeed_smc_register_types(void)
16287c1c69bcSCédric Le Goater {
16297c1c69bcSCédric Le Goater     type_register_static(&aspeed_smc_info);
1630*30b6852cSCédric Le Goater     type_register_static(&aspeed_2400_smc_info);
1631*30b6852cSCédric Le Goater     type_register_static(&aspeed_2400_fmc_info);
1632*30b6852cSCédric Le Goater     type_register_static(&aspeed_2400_spi1_info);
1633*30b6852cSCédric Le Goater     type_register_static(&aspeed_2500_fmc_info);
1634*30b6852cSCédric Le Goater     type_register_static(&aspeed_2500_spi1_info);
1635*30b6852cSCédric Le Goater     type_register_static(&aspeed_2500_spi2_info);
1636*30b6852cSCédric Le Goater     type_register_static(&aspeed_2600_fmc_info);
1637*30b6852cSCédric Le Goater     type_register_static(&aspeed_2600_spi1_info);
1638*30b6852cSCédric Le Goater     type_register_static(&aspeed_2600_spi2_info);
16397c1c69bcSCédric Le Goater }
16407c1c69bcSCédric Le Goater 
16417c1c69bcSCédric Le Goater type_init(aspeed_smc_register_types)
1642