xref: /qemu/hw/i2c/aspeed_i2c.c (revision 3be3d6ccf2adbc305645d097fb364753e11edb0f)
116020011SCédric Le Goater /*
216020011SCédric Le Goater  * ARM Aspeed I2C controller
316020011SCédric Le Goater  *
416020011SCédric Le Goater  * Copyright (C) 2016 IBM Corp.
516020011SCédric Le Goater  *
616020011SCédric Le Goater  * This program is free software; you can redistribute it and/or
716020011SCédric Le Goater  * modify it under the terms of the GNU General Public License
816020011SCédric Le Goater  * as published by the Free Software Foundation; either version 2
916020011SCédric Le Goater  * of the License, or (at your option) any later version.
1016020011SCédric Le Goater  *
1116020011SCédric Le Goater  * This program is distributed in the hope that it will be useful,
1216020011SCédric Le Goater  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1316020011SCédric Le Goater  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1416020011SCédric Le Goater  * GNU General Public License for more details.
1516020011SCédric Le Goater  *
1616020011SCédric Le Goater  * You should have received a copy of the GNU General Public License
1716020011SCédric Le Goater  * along with this program; if not, see <http://www.gnu.org/licenses/>.
1816020011SCédric Le Goater  *
1916020011SCédric Le Goater  */
2016020011SCédric Le Goater 
2116020011SCédric Le Goater #include "qemu/osdep.h"
2216020011SCédric Le Goater #include "hw/sysbus.h"
23d6454270SMarkus Armbruster #include "migration/vmstate.h"
2416020011SCédric Le Goater #include "qemu/log.h"
250b8fa32fSMarkus Armbruster #include "qemu/module.h"
26545d6befSCédric Le Goater #include "qemu/error-report.h"
27545d6befSCédric Le Goater #include "qapi/error.h"
2816020011SCédric Le Goater #include "hw/i2c/aspeed_i2c.h"
2964552b6bSMarkus Armbruster #include "hw/irq.h"
30545d6befSCédric Le Goater #include "hw/qdev-properties.h"
31*3be3d6ccSJoe Komlodi #include "hw/registerfields.h"
3266cc84a1SCédric Le Goater #include "trace.h"
3316020011SCédric Le Goater 
3416020011SCédric Le Goater /* I2C Global Register */
35*3be3d6ccSJoe Komlodi REG32(I2C_CTRL_STATUS, 0x0) /* Device Interrupt Status */
36*3be3d6ccSJoe Komlodi REG32(I2C_CTRL_ASSIGN, 0x8) /* Device Interrupt Target Assignment */
37*3be3d6ccSJoe Komlodi REG32(I2C_CTRL_GLOBAL, 0xC) /* Global Control Register */
38*3be3d6ccSJoe Komlodi     FIELD(I2C_CTRL_GLOBAL, SRAM_EN, 0, 1)
3916020011SCédric Le Goater 
4016020011SCédric Le Goater /* I2C Device (Bus) Register */
41*3be3d6ccSJoe Komlodi REG32(I2CD_FUN_CTRL, 0x0) /* I2CD Function Control  */
42*3be3d6ccSJoe Komlodi     FIELD(I2CD_FUN_CTRL, POOL_PAGE_SEL, 20, 3) /* AST2400 */
43*3be3d6ccSJoe Komlodi     FIELD(I2CD_FUN_CTRL, M_SDA_LOCK_EN, 16, 1)
44*3be3d6ccSJoe Komlodi     FIELD(I2CD_FUN_CTRL, MULTI_MASTER_DIS, 15, 1)
45*3be3d6ccSJoe Komlodi     FIELD(I2CD_FUN_CTRL, M_SCL_DRIVE_EN, 14, 1)
46*3be3d6ccSJoe Komlodi     FIELD(I2CD_FUN_CTRL, MSB_STS, 9, 1)
47*3be3d6ccSJoe Komlodi     FIELD(I2CD_FUN_CTRL, SDA_DRIVE_IT_EN, 8, 1)
48*3be3d6ccSJoe Komlodi     FIELD(I2CD_FUN_CTRL, M_SDA_DRIVE_IT_EN, 7, 1)
49*3be3d6ccSJoe Komlodi     FIELD(I2CD_FUN_CTRL, M_HIGH_SPEED_EN, 6, 1)
50*3be3d6ccSJoe Komlodi     FIELD(I2CD_FUN_CTRL, DEF_ADDR_EN, 5, 1)
51*3be3d6ccSJoe Komlodi     FIELD(I2CD_FUN_CTRL, DEF_ALERT_EN, 4, 1)
52*3be3d6ccSJoe Komlodi     FIELD(I2CD_FUN_CTRL, DEF_ARP_EN, 3, 1)
53*3be3d6ccSJoe Komlodi     FIELD(I2CD_FUN_CTRL, DEF_GCALL_EN, 2, 1)
54*3be3d6ccSJoe Komlodi     FIELD(I2CD_FUN_CTRL, SLAVE_EN, 1, 1)
55*3be3d6ccSJoe Komlodi     FIELD(I2CD_FUN_CTRL, MASTER_EN, 0, 1)
56*3be3d6ccSJoe Komlodi REG32(I2CD_AC_TIMING1, 0x04) /* Clock and AC Timing Control #1 */
57*3be3d6ccSJoe Komlodi REG32(I2CD_AC_TIMING2, 0x08) /* Clock and AC Timing Control #2 */
58*3be3d6ccSJoe Komlodi REG32(I2CD_INTR_CTRL, 0x0C)  /* I2CD Interrupt Control */
59*3be3d6ccSJoe Komlodi REG32(I2CD_INTR_STS, 0x10)   /* I2CD Interrupt Status */
60*3be3d6ccSJoe Komlodi     FIELD(I2CD_INTR_STS, SLAVE_ADDR_MATCH, 31, 1)    /* 0: addr1 1: addr2 */
61*3be3d6ccSJoe Komlodi     FIELD(I2CD_INTR_STS, SLAVE_ADDR_RX_PENDING, 29, 1)
62*3be3d6ccSJoe Komlodi     FIELD(I2CD_INTR_STS, SLAVE_INACTIVE_TIMEOUT, 15, 1)
63*3be3d6ccSJoe Komlodi     FIELD(I2CD_INTR_STS, SDA_DL_TIMEOUT, 14, 1)
64*3be3d6ccSJoe Komlodi     FIELD(I2CD_INTR_STS, BUS_RECOVER_DONE, 13, 1)
65*3be3d6ccSJoe Komlodi     FIELD(I2CD_INTR_STS, SMBUS_ALERT, 12, 1)            /* Bus [0-3] only */
66*3be3d6ccSJoe Komlodi     FIELD(I2CD_INTR_STS, SMBUS_ARP_ADDR, 11, 1)         /* Removed */
67*3be3d6ccSJoe Komlodi     FIELD(I2CD_INTR_STS, SMBUS_DEV_ALERT_ADDR, 10, 1)   /* Removed */
68*3be3d6ccSJoe Komlodi     FIELD(I2CD_INTR_STS, SMBUS_DEF_ADDR, 9, 1)          /* Removed */
69*3be3d6ccSJoe Komlodi     FIELD(I2CD_INTR_STS, GCALL_ADDR, 8, 1)              /* Removed */
70*3be3d6ccSJoe Komlodi     FIELD(I2CD_INTR_STS, SLAVE_ADDR_RX_MATCH, 7, 1)     /* use RX_DONE */
71*3be3d6ccSJoe Komlodi     FIELD(I2CD_INTR_STS, SCL_TIMEOUT, 6, 1)
72*3be3d6ccSJoe Komlodi     FIELD(I2CD_INTR_STS, ABNORMAL, 5, 1)
73*3be3d6ccSJoe Komlodi     FIELD(I2CD_INTR_STS, NORMAL_STOP, 4, 1)
74*3be3d6ccSJoe Komlodi     FIELD(I2CD_INTR_STS, ARBIT_LOSS, 3, 1)
75*3be3d6ccSJoe Komlodi     FIELD(I2CD_INTR_STS, RX_DONE, 2, 1)
76*3be3d6ccSJoe Komlodi     FIELD(I2CD_INTR_STS, TX_NAK, 1, 1)
77*3be3d6ccSJoe Komlodi     FIELD(I2CD_INTR_STS, TX_ACK, 0, 1)
78*3be3d6ccSJoe Komlodi REG32(I2CD_CMD, 0x14) /* I2CD Command/Status */
79*3be3d6ccSJoe Komlodi     FIELD(I2CD_CMD, SDA_OE, 28, 1)
80*3be3d6ccSJoe Komlodi     FIELD(I2CD_CMD, SDA_O, 27, 1)
81*3be3d6ccSJoe Komlodi     FIELD(I2CD_CMD, SCL_OE, 26, 1)
82*3be3d6ccSJoe Komlodi     FIELD(I2CD_CMD, SCL_O, 25, 1)
83*3be3d6ccSJoe Komlodi     FIELD(I2CD_CMD, TX_TIMING, 23, 2)
84*3be3d6ccSJoe Komlodi     FIELD(I2CD_CMD, TX_STATE, 19, 4)
85*3be3d6ccSJoe Komlodi /* Tx State Machine */
8616020011SCédric Le Goater #define   I2CD_TX_STATE_MASK                  0xf
8716020011SCédric Le Goater #define     I2CD_IDLE                         0x0
8816020011SCédric Le Goater #define     I2CD_MACTIVE                      0x8
8916020011SCédric Le Goater #define     I2CD_MSTART                       0x9
9016020011SCédric Le Goater #define     I2CD_MSTARTR                      0xa
9116020011SCédric Le Goater #define     I2CD_MSTOP                        0xb
9216020011SCédric Le Goater #define     I2CD_MTXD                         0xc
9316020011SCédric Le Goater #define     I2CD_MRXACK                       0xd
9416020011SCédric Le Goater #define     I2CD_MRXD                         0xe
9516020011SCédric Le Goater #define     I2CD_MTXACK                       0xf
9616020011SCédric Le Goater #define     I2CD_SWAIT                        0x1
9716020011SCédric Le Goater #define     I2CD_SRXD                         0x4
9816020011SCédric Le Goater #define     I2CD_STXACK                       0x5
9916020011SCédric Le Goater #define     I2CD_STXD                         0x6
10016020011SCédric Le Goater #define     I2CD_SRXACK                       0x7
10116020011SCédric Le Goater #define     I2CD_RECOVER                      0x3
102*3be3d6ccSJoe Komlodi     FIELD(I2CD_CMD, SCL_LINE_STS, 18, 1)
103*3be3d6ccSJoe Komlodi     FIELD(I2CD_CMD, SDA_LINE_STS, 17, 1)
104*3be3d6ccSJoe Komlodi     FIELD(I2CD_CMD, BUS_BUSY_STS, 16, 1)
105*3be3d6ccSJoe Komlodi     FIELD(I2CD_CMD, SDA_OE_OUT_DIR, 15, 1)
106*3be3d6ccSJoe Komlodi     FIELD(I2CD_CMD, SDA_O_OUT_DIR, 14, 1)
107*3be3d6ccSJoe Komlodi     FIELD(I2CD_CMD, SCL_OE_OUT_DIR, 13, 1)
108*3be3d6ccSJoe Komlodi     FIELD(I2CD_CMD, SCL_O_OUT_DIR, 12, 1)
109*3be3d6ccSJoe Komlodi     FIELD(I2CD_CMD, BUS_RECOVER_CMD_EN, 11, 1)
110*3be3d6ccSJoe Komlodi     FIELD(I2CD_CMD, S_ALT_EN, 10, 1)
111*3be3d6ccSJoe Komlodi     /* Command Bits */
112*3be3d6ccSJoe Komlodi     FIELD(I2CD_CMD, RX_DMA_EN, 9, 1)
113*3be3d6ccSJoe Komlodi     FIELD(I2CD_CMD, TX_DMA_EN, 8, 1)
114*3be3d6ccSJoe Komlodi     FIELD(I2CD_CMD, RX_BUFF_EN, 7, 1)
115*3be3d6ccSJoe Komlodi     FIELD(I2CD_CMD, TX_BUFF_EN, 6, 1)
116*3be3d6ccSJoe Komlodi     FIELD(I2CD_CMD, M_STOP_CMD, 5, 1)
117*3be3d6ccSJoe Komlodi     FIELD(I2CD_CMD, M_S_RX_CMD_LAST, 4, 1)
118*3be3d6ccSJoe Komlodi     FIELD(I2CD_CMD, M_RX_CMD, 3, 1)
119*3be3d6ccSJoe Komlodi     FIELD(I2CD_CMD, S_TX_CMD, 2, 1)
120*3be3d6ccSJoe Komlodi     FIELD(I2CD_CMD, M_TX_CMD, 1, 1)
121*3be3d6ccSJoe Komlodi     FIELD(I2CD_CMD, M_START_CMD, 0, 1)
122*3be3d6ccSJoe Komlodi REG32(I2CD_DEV_ADDR, 0x18) /* Slave Device Address */
123*3be3d6ccSJoe Komlodi REG32(I2CD_POOL_CTRL, 0x1C) /* Pool Buffer Control */
124*3be3d6ccSJoe Komlodi     FIELD(I2CD_POOL_CTRL, RX_COUNT, 24, 5)
125*3be3d6ccSJoe Komlodi     FIELD(I2CD_POOL_CTRL, RX_SIZE, 16, 5)
126*3be3d6ccSJoe Komlodi     FIELD(I2CD_POOL_CTRL, TX_COUNT, 9, 5)
127*3be3d6ccSJoe Komlodi     FIELD(I2CD_POOL_CTRL, OFFSET, 2, 6) /* AST2400 */
128*3be3d6ccSJoe Komlodi REG32(I2CD_BYTE_BUF, 0x20) /* Transmit/Receive Byte Buffer */
129*3be3d6ccSJoe Komlodi     FIELD(I2CD_BYTE_BUF, RX_BUF, 8, 8)
130*3be3d6ccSJoe Komlodi     FIELD(I2CD_BYTE_BUF, TX_BUF, 0, 8)
131*3be3d6ccSJoe Komlodi REG32(I2CD_DMA_ADDR, 0x24) /* DMA Buffer Address */
132*3be3d6ccSJoe Komlodi REG32(I2CD_DMA_LEN, 0x28) /* DMA Transfer Length < 4KB */
13316020011SCédric Le Goater 
13416020011SCédric Le Goater static inline bool aspeed_i2c_bus_is_master(AspeedI2CBus *bus)
13516020011SCédric Le Goater {
136*3be3d6ccSJoe Komlodi     return FIELD_EX32(bus->ctrl, I2CD_FUN_CTRL, MASTER_EN);
13716020011SCédric Le Goater }
13816020011SCédric Le Goater 
13916020011SCédric Le Goater static inline bool aspeed_i2c_bus_is_enabled(AspeedI2CBus *bus)
14016020011SCédric Le Goater {
141*3be3d6ccSJoe Komlodi     return FIELD_EX32(bus->ctrl, I2CD_FUN_CTRL, MASTER_EN) ||
142*3be3d6ccSJoe Komlodi            FIELD_EX32(bus->ctrl, I2CD_FUN_CTRL, SLAVE_EN);
14316020011SCédric Le Goater }
14416020011SCédric Le Goater 
14516020011SCédric Le Goater static inline void aspeed_i2c_bus_raise_interrupt(AspeedI2CBus *bus)
14616020011SCédric Le Goater {
14751dd4923SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
14851dd4923SCédric Le Goater 
14966cc84a1SCédric Le Goater     trace_aspeed_i2c_bus_raise_interrupt(bus->intr_status,
150*3be3d6ccSJoe Komlodi         FIELD_EX32(bus->intr_status, I2CD_INTR_STS, TX_NAK) ? "nak|" : "",
151*3be3d6ccSJoe Komlodi         FIELD_EX32(bus->intr_status, I2CD_INTR_STS, TX_ACK) ? "ack|" : "",
152*3be3d6ccSJoe Komlodi         FIELD_EX32(bus->intr_status, I2CD_INTR_STS, RX_DONE) ? "done|" : "",
153*3be3d6ccSJoe Komlodi         FIELD_EX32(bus->intr_status, I2CD_INTR_STS, NORMAL_STOP) ? "normal|"
154*3be3d6ccSJoe Komlodi                                                                  : "",
155*3be3d6ccSJoe Komlodi         FIELD_EX32(bus->intr_status, I2CD_INTR_STS, ABNORMAL) ? "abnormal"
156*3be3d6ccSJoe Komlodi                                                               : "");
15766cc84a1SCédric Le Goater 
15816020011SCédric Le Goater     bus->intr_status &= bus->intr_ctrl;
15916020011SCédric Le Goater     if (bus->intr_status) {
16016020011SCédric Le Goater         bus->controller->intr_status |= 1 << bus->id;
16151dd4923SCédric Le Goater         qemu_irq_raise(aic->bus_get_irq(bus));
16216020011SCédric Le Goater     }
16316020011SCédric Le Goater }
16416020011SCédric Le Goater 
16516020011SCédric Le Goater static uint64_t aspeed_i2c_bus_read(void *opaque, hwaddr offset,
16616020011SCédric Le Goater                                     unsigned size)
16716020011SCédric Le Goater {
16816020011SCédric Le Goater     AspeedI2CBus *bus = opaque;
169545d6befSCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
17066cc84a1SCédric Le Goater     uint64_t value = -1;
17116020011SCédric Le Goater 
17216020011SCédric Le Goater     switch (offset) {
173*3be3d6ccSJoe Komlodi     case A_I2CD_FUN_CTRL:
17466cc84a1SCédric Le Goater         value = bus->ctrl;
17566cc84a1SCédric Le Goater         break;
176*3be3d6ccSJoe Komlodi     case A_I2CD_AC_TIMING1:
17766cc84a1SCédric Le Goater         value = bus->timing[0];
17866cc84a1SCédric Le Goater         break;
179*3be3d6ccSJoe Komlodi     case A_I2CD_AC_TIMING2:
18066cc84a1SCédric Le Goater         value = bus->timing[1];
18166cc84a1SCédric Le Goater         break;
182*3be3d6ccSJoe Komlodi     case A_I2CD_INTR_CTRL:
18366cc84a1SCédric Le Goater         value = bus->intr_ctrl;
18466cc84a1SCédric Le Goater         break;
185*3be3d6ccSJoe Komlodi     case A_I2CD_INTR_STS:
18666cc84a1SCédric Le Goater         value = bus->intr_status;
18766cc84a1SCédric Le Goater         break;
188*3be3d6ccSJoe Komlodi     case A_I2CD_POOL_CTRL:
18966cc84a1SCédric Le Goater         value = bus->pool_ctrl;
19066cc84a1SCédric Le Goater         break;
191*3be3d6ccSJoe Komlodi     case A_I2CD_BYTE_BUF:
19266cc84a1SCédric Le Goater         value = bus->buf;
19366cc84a1SCédric Le Goater         break;
194*3be3d6ccSJoe Komlodi     case A_I2CD_CMD:
19566cc84a1SCédric Le Goater         value = bus->cmd | (i2c_bus_busy(bus->bus) << 16);
19666cc84a1SCédric Le Goater         break;
197*3be3d6ccSJoe Komlodi     case A_I2CD_DMA_ADDR:
198545d6befSCédric Le Goater         if (!aic->has_dma) {
199545d6befSCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n",  __func__);
20066cc84a1SCédric Le Goater             break;
201545d6befSCédric Le Goater         }
20266cc84a1SCédric Le Goater         value = bus->dma_addr;
20366cc84a1SCédric Le Goater         break;
204*3be3d6ccSJoe Komlodi     case A_I2CD_DMA_LEN:
205545d6befSCédric Le Goater         if (!aic->has_dma) {
206545d6befSCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n",  __func__);
20766cc84a1SCédric Le Goater             break;
208545d6befSCédric Le Goater         }
20966cc84a1SCédric Le Goater         value = bus->dma_len;
21066cc84a1SCédric Le Goater         break;
21166cc84a1SCédric Le Goater 
21216020011SCédric Le Goater     default:
21316020011SCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR,
21416020011SCédric Le Goater                       "%s: Bad offset 0x%" HWADDR_PRIx "\n", __func__, offset);
21566cc84a1SCédric Le Goater         value = -1;
21666cc84a1SCédric Le Goater         break;
21716020011SCédric Le Goater     }
21866cc84a1SCédric Le Goater 
21966cc84a1SCédric Le Goater     trace_aspeed_i2c_bus_read(bus->id, offset, size, value);
22066cc84a1SCédric Le Goater     return value;
22116020011SCédric Le Goater }
22216020011SCédric Le Goater 
2234960f084SCédric Le Goater static void aspeed_i2c_set_state(AspeedI2CBus *bus, uint8_t state)
2244960f084SCédric Le Goater {
225*3be3d6ccSJoe Komlodi     bus->cmd = FIELD_DP32(bus->cmd, I2CD_CMD, TX_STATE, state);
2264960f084SCédric Le Goater }
2274960f084SCédric Le Goater 
2284960f084SCédric Le Goater static uint8_t aspeed_i2c_get_state(AspeedI2CBus *bus)
2294960f084SCédric Le Goater {
230*3be3d6ccSJoe Komlodi     return FIELD_EX32(bus->cmd, I2CD_CMD, TX_STATE);
2314960f084SCédric Le Goater }
2324960f084SCédric Le Goater 
233545d6befSCédric Le Goater static int aspeed_i2c_dma_read(AspeedI2CBus *bus, uint8_t *data)
234545d6befSCédric Le Goater {
235545d6befSCédric Le Goater     MemTxResult result;
236545d6befSCédric Le Goater     AspeedI2CState *s = bus->controller;
237545d6befSCédric Le Goater 
238545d6befSCédric Le Goater     result = address_space_read(&s->dram_as, bus->dma_addr,
239545d6befSCédric Le Goater                                 MEMTXATTRS_UNSPECIFIED, data, 1);
240545d6befSCédric Le Goater     if (result != MEMTX_OK) {
241545d6befSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "%s: DRAM read failed @%08x\n",
242545d6befSCédric Le Goater                       __func__, bus->dma_addr);
243545d6befSCédric Le Goater         return -1;
244545d6befSCédric Le Goater     }
245545d6befSCédric Le Goater 
246545d6befSCédric Le Goater     bus->dma_addr++;
247545d6befSCédric Le Goater     bus->dma_len--;
248545d6befSCédric Le Goater     return 0;
249545d6befSCédric Le Goater }
250545d6befSCédric Le Goater 
2516054fc73SCédric Le Goater static int aspeed_i2c_bus_send(AspeedI2CBus *bus, uint8_t pool_start)
2526054fc73SCédric Le Goater {
2536054fc73SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
2546054fc73SCédric Le Goater     int ret = -1;
2556054fc73SCédric Le Goater     int i;
256*3be3d6ccSJoe Komlodi     int pool_tx_count = FIELD_EX32(bus->pool_ctrl, I2CD_POOL_CTRL, TX_COUNT);
2576054fc73SCédric Le Goater 
258*3be3d6ccSJoe Komlodi     if (FIELD_EX32(bus->cmd, I2CD_CMD, TX_BUFF_EN)) {
259*3be3d6ccSJoe Komlodi         for (i = pool_start; i < pool_tx_count; i++) {
2606054fc73SCédric Le Goater             uint8_t *pool_base = aic->bus_pool_base(bus);
2616054fc73SCédric Le Goater 
262*3be3d6ccSJoe Komlodi             trace_aspeed_i2c_bus_send("BUF", i + 1, pool_tx_count,
26366cc84a1SCédric Le Goater                                       pool_base[i]);
2646054fc73SCédric Le Goater             ret = i2c_send(bus->bus, pool_base[i]);
2656054fc73SCédric Le Goater             if (ret) {
2666054fc73SCédric Le Goater                 break;
2676054fc73SCédric Le Goater             }
2686054fc73SCédric Le Goater         }
269*3be3d6ccSJoe Komlodi         bus->cmd = FIELD_DP32(bus->cmd, I2CD_CMD, TX_BUFF_EN, 0);
270*3be3d6ccSJoe Komlodi     } else if (FIELD_EX32(bus->cmd, I2CD_CMD, TX_DMA_EN)) {
271545d6befSCédric Le Goater         while (bus->dma_len) {
272545d6befSCédric Le Goater             uint8_t data;
273545d6befSCédric Le Goater             aspeed_i2c_dma_read(bus, &data);
27466cc84a1SCédric Le Goater             trace_aspeed_i2c_bus_send("DMA", bus->dma_len, bus->dma_len, data);
275545d6befSCédric Le Goater             ret = i2c_send(bus->bus, data);
276545d6befSCédric Le Goater             if (ret) {
277545d6befSCédric Le Goater                 break;
278545d6befSCédric Le Goater             }
279545d6befSCédric Le Goater         }
280*3be3d6ccSJoe Komlodi         bus->cmd = FIELD_DP32(bus->cmd, I2CD_CMD, TX_DMA_EN, 0);
2816054fc73SCédric Le Goater     } else {
28266cc84a1SCédric Le Goater         trace_aspeed_i2c_bus_send("BYTE", pool_start, 1, bus->buf);
2836054fc73SCédric Le Goater         ret = i2c_send(bus->bus, bus->buf);
2846054fc73SCédric Le Goater     }
2856054fc73SCédric Le Goater 
2866054fc73SCédric Le Goater     return ret;
2876054fc73SCédric Le Goater }
2886054fc73SCédric Le Goater 
2896054fc73SCédric Le Goater static void aspeed_i2c_bus_recv(AspeedI2CBus *bus)
2906054fc73SCédric Le Goater {
2916054fc73SCédric Le Goater     AspeedI2CState *s = bus->controller;
2926054fc73SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
2936054fc73SCédric Le Goater     uint8_t data;
2946054fc73SCédric Le Goater     int i;
295*3be3d6ccSJoe Komlodi     int pool_rx_count = FIELD_EX32(bus->pool_ctrl, I2CD_POOL_CTRL, RX_COUNT);
2966054fc73SCédric Le Goater 
297*3be3d6ccSJoe Komlodi     if (FIELD_EX32(bus->cmd, I2CD_CMD, RX_BUFF_EN)) {
2986054fc73SCédric Le Goater         uint8_t *pool_base = aic->bus_pool_base(bus);
2996054fc73SCédric Le Goater 
300*3be3d6ccSJoe Komlodi         for (i = 0; i < pool_rx_count; i++) {
3016054fc73SCédric Le Goater             pool_base[i] = i2c_recv(bus->bus);
302*3be3d6ccSJoe Komlodi             trace_aspeed_i2c_bus_recv("BUF", i + 1, pool_rx_count,
30366cc84a1SCédric Le Goater                                       pool_base[i]);
3046054fc73SCédric Le Goater         }
3056054fc73SCédric Le Goater 
3066054fc73SCédric Le Goater         /* Update RX count */
307*3be3d6ccSJoe Komlodi         bus->pool_ctrl = FIELD_DP32(bus->pool_ctrl, I2CD_POOL_CTRL, RX_COUNT,
308*3be3d6ccSJoe Komlodi                                     i & 0xff);
309*3be3d6ccSJoe Komlodi         bus->cmd = FIELD_DP32(bus->cmd, I2CD_CMD, RX_BUFF_EN, 0);
310*3be3d6ccSJoe Komlodi     } else if (FIELD_EX32(bus->cmd, I2CD_CMD, RX_DMA_EN)) {
311545d6befSCédric Le Goater         uint8_t data;
312545d6befSCédric Le Goater 
313545d6befSCédric Le Goater         while (bus->dma_len) {
314545d6befSCédric Le Goater             MemTxResult result;
315545d6befSCédric Le Goater 
316545d6befSCédric Le Goater             data = i2c_recv(bus->bus);
31766cc84a1SCédric Le Goater             trace_aspeed_i2c_bus_recv("DMA", bus->dma_len, bus->dma_len, data);
318545d6befSCédric Le Goater             result = address_space_write(&s->dram_as, bus->dma_addr,
319545d6befSCédric Le Goater                                          MEMTXATTRS_UNSPECIFIED, &data, 1);
320545d6befSCédric Le Goater             if (result != MEMTX_OK) {
321545d6befSCédric Le Goater                 qemu_log_mask(LOG_GUEST_ERROR, "%s: DRAM write failed @%08x\n",
322545d6befSCédric Le Goater                               __func__, bus->dma_addr);
323545d6befSCédric Le Goater                 return;
324545d6befSCédric Le Goater             }
325545d6befSCédric Le Goater             bus->dma_addr++;
326545d6befSCédric Le Goater             bus->dma_len--;
327545d6befSCédric Le Goater         }
328*3be3d6ccSJoe Komlodi         bus->cmd = FIELD_DP32(bus->cmd, I2CD_CMD, RX_DMA_EN, 0);
3296054fc73SCédric Le Goater     } else {
3306054fc73SCédric Le Goater         data = i2c_recv(bus->bus);
33166cc84a1SCédric Le Goater         trace_aspeed_i2c_bus_recv("BYTE", 1, 1, bus->buf);
332*3be3d6ccSJoe Komlodi         bus->buf = FIELD_DP32(bus->buf, I2CD_BYTE_BUF, RX_BUF, data);
3336054fc73SCédric Le Goater     }
3346054fc73SCédric Le Goater }
3356054fc73SCédric Le Goater 
3367bd9c60dSGuenter Roeck static void aspeed_i2c_handle_rx_cmd(AspeedI2CBus *bus)
3377bd9c60dSGuenter Roeck {
3387bd9c60dSGuenter Roeck     aspeed_i2c_set_state(bus, I2CD_MRXD);
3396054fc73SCédric Le Goater     aspeed_i2c_bus_recv(bus);
340*3be3d6ccSJoe Komlodi     bus->intr_status = FIELD_DP32(bus->intr_status, I2CD_INTR_STS, RX_DONE, 1);
341*3be3d6ccSJoe Komlodi     if (FIELD_EX32(bus->cmd, I2CD_CMD, M_S_RX_CMD_LAST)) {
3427bd9c60dSGuenter Roeck         i2c_nack(bus->bus);
3437bd9c60dSGuenter Roeck     }
344*3be3d6ccSJoe Komlodi     bus->cmd = FIELD_DP32(bus->cmd, I2CD_CMD, M_RX_CMD, 0);
345*3be3d6ccSJoe Komlodi     bus->cmd = FIELD_DP32(bus->cmd, I2CD_CMD, M_S_RX_CMD_LAST, 0);
3467bd9c60dSGuenter Roeck     aspeed_i2c_set_state(bus, I2CD_MACTIVE);
3477bd9c60dSGuenter Roeck }
3487bd9c60dSGuenter Roeck 
3496054fc73SCédric Le Goater static uint8_t aspeed_i2c_get_addr(AspeedI2CBus *bus)
3506054fc73SCédric Le Goater {
3516054fc73SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
3526054fc73SCédric Le Goater 
353*3be3d6ccSJoe Komlodi     if (FIELD_EX32(bus->cmd, I2CD_CMD, TX_BUFF_EN)) {
3546054fc73SCédric Le Goater         uint8_t *pool_base = aic->bus_pool_base(bus);
3556054fc73SCédric Le Goater 
3566054fc73SCédric Le Goater         return pool_base[0];
357*3be3d6ccSJoe Komlodi     } else if (FIELD_EX32(bus->cmd, I2CD_CMD, TX_DMA_EN)) {
358545d6befSCédric Le Goater         uint8_t data;
359545d6befSCédric Le Goater 
360545d6befSCédric Le Goater         aspeed_i2c_dma_read(bus, &data);
361545d6befSCédric Le Goater         return data;
3626054fc73SCédric Le Goater     } else {
3636054fc73SCédric Le Goater         return bus->buf;
3646054fc73SCédric Le Goater     }
3656054fc73SCédric Le Goater }
3666054fc73SCédric Le Goater 
367aab90b1cSCédric Le Goater static bool aspeed_i2c_check_sram(AspeedI2CBus *bus)
368aab90b1cSCédric Le Goater {
369aab90b1cSCédric Le Goater     AspeedI2CState *s = bus->controller;
370aab90b1cSCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
371*3be3d6ccSJoe Komlodi     bool dma_en = FIELD_EX32(bus->cmd, I2CD_CMD, RX_DMA_EN)  ||
372*3be3d6ccSJoe Komlodi                   FIELD_EX32(bus->cmd, I2CD_CMD, TX_DMA_EN)  ||
373*3be3d6ccSJoe Komlodi                   FIELD_EX32(bus->cmd, I2CD_CMD, RX_BUFF_EN) ||
374*3be3d6ccSJoe Komlodi                   FIELD_EX32(bus->cmd, I2CD_CMD, TX_BUFF_EN);
375aab90b1cSCédric Le Goater     if (!aic->check_sram) {
376aab90b1cSCédric Le Goater         return true;
377aab90b1cSCédric Le Goater     }
378aab90b1cSCédric Le Goater 
379aab90b1cSCédric Le Goater     /*
380aab90b1cSCédric Le Goater      * AST2500: SRAM must be enabled before using the Buffer Pool or
381aab90b1cSCédric Le Goater      * DMA mode.
382aab90b1cSCédric Le Goater      */
383*3be3d6ccSJoe Komlodi     if (!FIELD_EX32(s->ctrl_global, I2C_CTRL_GLOBAL, SRAM_EN) && dma_en) {
384aab90b1cSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "%s: SRAM is not enabled\n", __func__);
385aab90b1cSCédric Le Goater         return false;
386aab90b1cSCédric Le Goater     }
387aab90b1cSCédric Le Goater 
388aab90b1cSCédric Le Goater     return true;
389aab90b1cSCédric Le Goater }
390aab90b1cSCédric Le Goater 
39166cc84a1SCédric Le Goater static void aspeed_i2c_bus_cmd_dump(AspeedI2CBus *bus)
39266cc84a1SCédric Le Goater {
393f821bac4SMiroslav Rezanina     g_autofree char *cmd_flags = NULL;
39466cc84a1SCédric Le Goater     uint32_t count;
395*3be3d6ccSJoe Komlodi     if (FIELD_EX32(bus->cmd, I2CD_CMD, RX_BUFF_EN)) {
396*3be3d6ccSJoe Komlodi         count = FIELD_EX32(bus->pool_ctrl, I2CD_POOL_CTRL, TX_COUNT);
397*3be3d6ccSJoe Komlodi     } else if (FIELD_EX32(bus->cmd, I2CD_CMD, RX_DMA_EN)) {
39866cc84a1SCédric Le Goater         count = bus->dma_len;
39966cc84a1SCédric Le Goater     } else { /* BYTE mode */
40066cc84a1SCédric Le Goater         count = 1;
40166cc84a1SCédric Le Goater     }
40266cc84a1SCédric Le Goater 
40366cc84a1SCédric Le Goater     cmd_flags = g_strdup_printf("%s%s%s%s%s%s%s%s%s",
404*3be3d6ccSJoe Komlodi              FIELD_EX32(bus->cmd, I2CD_CMD, M_START_CMD) ? "start|" : "",
405*3be3d6ccSJoe Komlodi              FIELD_EX32(bus->cmd, I2CD_CMD, RX_DMA_EN) ? "rxdma|" : "",
406*3be3d6ccSJoe Komlodi              FIELD_EX32(bus->cmd, I2CD_CMD, TX_DMA_EN) ? "txdma|" : "",
407*3be3d6ccSJoe Komlodi              FIELD_EX32(bus->cmd, I2CD_CMD, RX_BUFF_EN) ? "rxbuf|" : "",
408*3be3d6ccSJoe Komlodi              FIELD_EX32(bus->cmd, I2CD_CMD, TX_BUFF_EN) ? "txbuf|" : "",
409*3be3d6ccSJoe Komlodi              FIELD_EX32(bus->cmd, I2CD_CMD, M_TX_CMD) ? "tx|" : "",
410*3be3d6ccSJoe Komlodi              FIELD_EX32(bus->cmd, I2CD_CMD, M_RX_CMD) ? "rx|" : "",
411*3be3d6ccSJoe Komlodi              FIELD_EX32(bus->cmd, I2CD_CMD, M_S_RX_CMD_LAST) ? "last|" : "",
412*3be3d6ccSJoe Komlodi              FIELD_EX32(bus->cmd, I2CD_CMD, M_STOP_CMD) ? "stop" : "");
41366cc84a1SCédric Le Goater 
41466cc84a1SCédric Le Goater     trace_aspeed_i2c_bus_cmd(bus->cmd, cmd_flags, count, bus->intr_status);
41566cc84a1SCédric Le Goater }
41666cc84a1SCédric Le Goater 
4174960f084SCédric Le Goater /*
4184960f084SCédric Le Goater  * The state machine needs some refinement. It is only used to track
4194960f084SCédric Le Goater  * invalid STOP commands for the moment.
4204960f084SCédric Le Goater  */
42116020011SCédric Le Goater static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value)
42216020011SCédric Le Goater {
4236054fc73SCédric Le Goater     uint8_t pool_start = 0;
4246054fc73SCédric Le Goater 
425ddabca75SCédric Le Goater     bus->cmd &= ~0xFFFF;
42616020011SCédric Le Goater     bus->cmd |= value & 0xFFFF;
42716020011SCédric Le Goater 
428aab90b1cSCédric Le Goater     if (!aspeed_i2c_check_sram(bus)) {
429aab90b1cSCédric Le Goater         return;
430aab90b1cSCédric Le Goater     }
431aab90b1cSCédric Le Goater 
43266cc84a1SCédric Le Goater     if (trace_event_get_state_backends(TRACE_ASPEED_I2C_BUS_CMD)) {
43366cc84a1SCédric Le Goater         aspeed_i2c_bus_cmd_dump(bus);
43466cc84a1SCédric Le Goater     }
43566cc84a1SCédric Le Goater 
436*3be3d6ccSJoe Komlodi     if (FIELD_EX32(bus->cmd, I2CD_CMD, M_START_CMD)) {
4374960f084SCédric Le Goater         uint8_t state = aspeed_i2c_get_state(bus) & I2CD_MACTIVE ?
4384960f084SCédric Le Goater             I2CD_MSTARTR : I2CD_MSTART;
4396054fc73SCédric Le Goater         uint8_t addr;
4404960f084SCédric Le Goater 
4414960f084SCédric Le Goater         aspeed_i2c_set_state(bus, state);
4424960f084SCédric Le Goater 
4436054fc73SCédric Le Goater         addr = aspeed_i2c_get_addr(bus);
4446054fc73SCédric Le Goater 
4456054fc73SCédric Le Goater         if (i2c_start_transfer(bus->bus, extract32(addr, 1, 7),
4466054fc73SCédric Le Goater                                extract32(addr, 0, 1))) {
447*3be3d6ccSJoe Komlodi             bus->intr_status = FIELD_DP32(bus->intr_status, I2CD_INTR_STS,
448*3be3d6ccSJoe Komlodi                                           TX_NAK, 1);
44916020011SCédric Le Goater         } else {
450*3be3d6ccSJoe Komlodi             bus->intr_status = FIELD_DP32(bus->intr_status, I2CD_INTR_STS,
451*3be3d6ccSJoe Komlodi                                           TX_ACK, 1);
45216020011SCédric Le Goater         }
45316020011SCédric Le Goater 
454*3be3d6ccSJoe Komlodi         bus->cmd = FIELD_DP32(bus->cmd, I2CD_CMD, M_START_CMD, 0);
4556054fc73SCédric Le Goater 
4566054fc73SCédric Le Goater         /*
4576054fc73SCédric Le Goater          * The START command is also a TX command, as the slave
4586054fc73SCédric Le Goater          * address is sent on the bus. Drop the TX flag if nothing
4596054fc73SCédric Le Goater          * else needs to be sent in this sequence.
4606054fc73SCédric Le Goater          */
461*3be3d6ccSJoe Komlodi         if (FIELD_EX32(bus->cmd, I2CD_CMD, TX_BUFF_EN)) {
462*3be3d6ccSJoe Komlodi             if (FIELD_EX32(bus->pool_ctrl, I2CD_POOL_CTRL, TX_COUNT) == 1) {
463*3be3d6ccSJoe Komlodi                 bus->cmd = FIELD_DP32(bus->cmd, I2CD_CMD, M_TX_CMD, 0);
4646054fc73SCédric Le Goater             } else {
4656054fc73SCédric Le Goater                 /*
4666054fc73SCédric Le Goater                  * Increase the start index in the TX pool buffer to
4676054fc73SCédric Le Goater                  * skip the address byte.
4686054fc73SCédric Le Goater                  */
4696054fc73SCédric Le Goater                 pool_start++;
4706054fc73SCédric Le Goater             }
471*3be3d6ccSJoe Komlodi         } else if (FIELD_EX32(bus->cmd, I2CD_CMD, TX_DMA_EN)) {
472545d6befSCédric Le Goater             if (bus->dma_len == 0) {
473*3be3d6ccSJoe Komlodi                 bus->cmd = FIELD_DP32(bus->cmd, I2CD_CMD, M_TX_CMD, 0);
474545d6befSCédric Le Goater             }
4756054fc73SCédric Le Goater         } else {
476*3be3d6ccSJoe Komlodi             bus->cmd = FIELD_DP32(bus->cmd, I2CD_CMD, M_TX_CMD, 0);
4776054fc73SCédric Le Goater         }
478ddabca75SCédric Le Goater 
479ddabca75SCédric Le Goater         /* No slave found */
480ddabca75SCédric Le Goater         if (!i2c_bus_busy(bus->bus)) {
481ddabca75SCédric Le Goater             return;
482ddabca75SCédric Le Goater         }
4834960f084SCédric Le Goater         aspeed_i2c_set_state(bus, I2CD_MACTIVE);
484ddabca75SCédric Le Goater     }
485ddabca75SCédric Le Goater 
486*3be3d6ccSJoe Komlodi     if (FIELD_EX32(bus->cmd, I2CD_CMD, M_TX_CMD)) {
4874960f084SCédric Le Goater         aspeed_i2c_set_state(bus, I2CD_MTXD);
4886054fc73SCédric Le Goater         if (aspeed_i2c_bus_send(bus, pool_start)) {
489*3be3d6ccSJoe Komlodi             bus->intr_status = FIELD_DP32(bus->intr_status, I2CD_INTR_STS,
490*3be3d6ccSJoe Komlodi                                           TX_NAK, 1);
49116020011SCédric Le Goater             i2c_end_transfer(bus->bus);
49216020011SCédric Le Goater         } else {
493*3be3d6ccSJoe Komlodi             bus->intr_status = FIELD_DP32(bus->intr_status, I2CD_INTR_STS,
494*3be3d6ccSJoe Komlodi                                           TX_ACK, 1);
49516020011SCédric Le Goater         }
496*3be3d6ccSJoe Komlodi         bus->cmd = FIELD_DP32(bus->cmd, I2CD_CMD, M_TX_CMD, 0);
4974960f084SCédric Le Goater         aspeed_i2c_set_state(bus, I2CD_MACTIVE);
498ddabca75SCédric Le Goater     }
49916020011SCédric Le Goater 
500*3be3d6ccSJoe Komlodi     if ((FIELD_EX32(bus->cmd, I2CD_CMD, M_RX_CMD) ||
501*3be3d6ccSJoe Komlodi          FIELD_EX32(bus->cmd, I2CD_CMD, M_S_RX_CMD_LAST)) &&
502*3be3d6ccSJoe Komlodi         !FIELD_EX32(bus->intr_status, I2CD_INTR_STS, RX_DONE)) {
5037bd9c60dSGuenter Roeck         aspeed_i2c_handle_rx_cmd(bus);
50416020011SCédric Le Goater     }
50516020011SCédric Le Goater 
506*3be3d6ccSJoe Komlodi     if (FIELD_EX32(bus->cmd, I2CD_CMD, M_STOP_CMD)) {
5074960f084SCédric Le Goater         if (!(aspeed_i2c_get_state(bus) & I2CD_MACTIVE)) {
5084960f084SCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "%s: abnormal stop\n", __func__);
509*3be3d6ccSJoe Komlodi             bus->intr_status = FIELD_DP32(bus->intr_status, I2CD_INTR_STS,
510*3be3d6ccSJoe Komlodi                                           ABNORMAL, 1);
51116020011SCédric Le Goater         } else {
5124960f084SCédric Le Goater             aspeed_i2c_set_state(bus, I2CD_MSTOP);
51316020011SCédric Le Goater             i2c_end_transfer(bus->bus);
514*3be3d6ccSJoe Komlodi             bus->intr_status = FIELD_DP32(bus->intr_status, I2CD_INTR_STS,
515*3be3d6ccSJoe Komlodi                                           NORMAL_STOP, 1);
51616020011SCédric Le Goater         }
517*3be3d6ccSJoe Komlodi         bus->cmd = FIELD_DP32(bus->cmd, I2CD_CMD, M_STOP_CMD, 0);
5184960f084SCédric Le Goater         aspeed_i2c_set_state(bus, I2CD_IDLE);
51916020011SCédric Le Goater     }
52016020011SCédric Le Goater }
52116020011SCédric Le Goater 
52216020011SCédric Le Goater static void aspeed_i2c_bus_write(void *opaque, hwaddr offset,
52316020011SCédric Le Goater                                  uint64_t value, unsigned size)
52416020011SCédric Le Goater {
52516020011SCédric Le Goater     AspeedI2CBus *bus = opaque;
52651dd4923SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
527bb626e5bSGuenter Roeck     bool handle_rx;
52816020011SCédric Le Goater 
52966cc84a1SCédric Le Goater     trace_aspeed_i2c_bus_write(bus->id, offset, size, value);
53066cc84a1SCédric Le Goater 
53116020011SCédric Le Goater     switch (offset) {
532*3be3d6ccSJoe Komlodi     case A_I2CD_FUN_CTRL:
533*3be3d6ccSJoe Komlodi         if (FIELD_EX32(value, I2CD_FUN_CTRL, SLAVE_EN)) {
53416020011SCédric Le Goater             qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n",
53516020011SCédric Le Goater                           __func__);
53616020011SCédric Le Goater             break;
53716020011SCédric Le Goater         }
53816020011SCédric Le Goater         bus->ctrl = value & 0x0071C3FF;
53916020011SCédric Le Goater         break;
540*3be3d6ccSJoe Komlodi     case A_I2CD_AC_TIMING1:
54116020011SCédric Le Goater         bus->timing[0] = value & 0xFFFFF0F;
54216020011SCédric Le Goater         break;
543*3be3d6ccSJoe Komlodi     case A_I2CD_AC_TIMING2:
54416020011SCédric Le Goater         bus->timing[1] = value & 0x7;
54516020011SCédric Le Goater         break;
546*3be3d6ccSJoe Komlodi     case A_I2CD_INTR_CTRL:
54716020011SCédric Le Goater         bus->intr_ctrl = value & 0x7FFF;
54816020011SCédric Le Goater         break;
549*3be3d6ccSJoe Komlodi     case A_I2CD_INTR_STS:
550*3be3d6ccSJoe Komlodi         handle_rx = FIELD_EX32(bus->intr_status, I2CD_INTR_STS, RX_DONE) &&
551*3be3d6ccSJoe Komlodi                     FIELD_EX32(value, I2CD_INTR_STS, RX_DONE);
55216020011SCédric Le Goater         bus->intr_status &= ~(value & 0x7FFF);
5535540cb97SCédric Le Goater         if (!bus->intr_status) {
55416020011SCédric Le Goater             bus->controller->intr_status &= ~(1 << bus->id);
55551dd4923SCédric Le Goater             qemu_irq_lower(aic->bus_get_irq(bus));
5565540cb97SCédric Le Goater         }
557*3be3d6ccSJoe Komlodi         if (handle_rx && (FIELD_EX32(bus->cmd, I2CD_CMD, M_RX_CMD) ||
558*3be3d6ccSJoe Komlodi                          FIELD_EX32(bus->cmd, I2CD_CMD, M_S_RX_CMD_LAST))) {
559bb626e5bSGuenter Roeck             aspeed_i2c_handle_rx_cmd(bus);
560bb626e5bSGuenter Roeck             aspeed_i2c_bus_raise_interrupt(bus);
561bb626e5bSGuenter Roeck         }
56216020011SCédric Le Goater         break;
563*3be3d6ccSJoe Komlodi     case A_I2CD_DEV_ADDR:
56416020011SCédric Le Goater         qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n",
56516020011SCédric Le Goater                       __func__);
56616020011SCédric Le Goater         break;
567*3be3d6ccSJoe Komlodi     case A_I2CD_POOL_CTRL:
5686054fc73SCédric Le Goater         bus->pool_ctrl &= ~0xffffff;
5696054fc73SCédric Le Goater         bus->pool_ctrl |= (value & 0xffffff);
5706054fc73SCédric Le Goater         break;
5716054fc73SCédric Le Goater 
572*3be3d6ccSJoe Komlodi     case A_I2CD_BYTE_BUF:
573*3be3d6ccSJoe Komlodi         bus->buf = FIELD_DP32(bus->buf, I2CD_BYTE_BUF, TX_BUF, value);
57416020011SCédric Le Goater         break;
575*3be3d6ccSJoe Komlodi     case A_I2CD_CMD:
57616020011SCédric Le Goater         if (!aspeed_i2c_bus_is_enabled(bus)) {
57716020011SCédric Le Goater             break;
57816020011SCédric Le Goater         }
57916020011SCédric Le Goater 
58016020011SCédric Le Goater         if (!aspeed_i2c_bus_is_master(bus)) {
58116020011SCédric Le Goater             qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n",
58216020011SCédric Le Goater                           __func__);
58316020011SCédric Le Goater             break;
58416020011SCédric Le Goater         }
58516020011SCédric Le Goater 
586545d6befSCédric Le Goater         if (!aic->has_dma &&
587*3be3d6ccSJoe Komlodi             (FIELD_EX32(value, I2CD_CMD, RX_DMA_EN) ||
588*3be3d6ccSJoe Komlodi              FIELD_EX32(value, I2CD_CMD, TX_DMA_EN))) {
589545d6befSCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n",  __func__);
590545d6befSCédric Le Goater             break;
591545d6befSCédric Le Goater         }
592545d6befSCédric Le Goater 
59316020011SCédric Le Goater         aspeed_i2c_bus_handle_cmd(bus, value);
594ddabca75SCédric Le Goater         aspeed_i2c_bus_raise_interrupt(bus);
59516020011SCédric Le Goater         break;
596*3be3d6ccSJoe Komlodi     case A_I2CD_DMA_ADDR:
597545d6befSCédric Le Goater         if (!aic->has_dma) {
598545d6befSCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n",  __func__);
599545d6befSCédric Le Goater             break;
600545d6befSCédric Le Goater         }
601545d6befSCédric Le Goater 
60274925159SCédric Le Goater         bus->dma_addr = value & 0x3ffffffc;
603545d6befSCédric Le Goater         break;
604545d6befSCédric Le Goater 
605*3be3d6ccSJoe Komlodi     case A_I2CD_DMA_LEN:
606545d6befSCédric Le Goater         if (!aic->has_dma) {
607545d6befSCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n",  __func__);
608545d6befSCédric Le Goater             break;
609545d6befSCédric Le Goater         }
610545d6befSCédric Le Goater 
611545d6befSCédric Le Goater         bus->dma_len = value & 0xfff;
612545d6befSCédric Le Goater         if (!bus->dma_len) {
613545d6befSCédric Le Goater             qemu_log_mask(LOG_UNIMP, "%s: invalid DMA length\n",  __func__);
614545d6befSCédric Le Goater         }
615545d6befSCédric Le Goater         break;
61616020011SCédric Le Goater 
61716020011SCédric Le Goater     default:
61816020011SCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
61916020011SCédric Le Goater                       __func__, offset);
62016020011SCédric Le Goater     }
62116020011SCédric Le Goater }
62216020011SCédric Le Goater 
62316020011SCédric Le Goater static uint64_t aspeed_i2c_ctrl_read(void *opaque, hwaddr offset,
62416020011SCédric Le Goater                                    unsigned size)
62516020011SCédric Le Goater {
62616020011SCédric Le Goater     AspeedI2CState *s = opaque;
62716020011SCédric Le Goater 
62816020011SCédric Le Goater     switch (offset) {
629*3be3d6ccSJoe Komlodi     case A_I2C_CTRL_STATUS:
63016020011SCédric Le Goater         return s->intr_status;
631*3be3d6ccSJoe Komlodi     case A_I2C_CTRL_GLOBAL:
632aab90b1cSCédric Le Goater         return s->ctrl_global;
63316020011SCédric Le Goater     default:
63416020011SCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
63516020011SCédric Le Goater                       __func__, offset);
63616020011SCédric Le Goater         break;
63716020011SCédric Le Goater     }
63816020011SCédric Le Goater 
63916020011SCédric Le Goater     return -1;
64016020011SCédric Le Goater }
64116020011SCédric Le Goater 
64216020011SCédric Le Goater static void aspeed_i2c_ctrl_write(void *opaque, hwaddr offset,
64316020011SCédric Le Goater                                   uint64_t value, unsigned size)
64416020011SCédric Le Goater {
645aab90b1cSCédric Le Goater     AspeedI2CState *s = opaque;
646aab90b1cSCédric Le Goater 
64716020011SCédric Le Goater     switch (offset) {
648*3be3d6ccSJoe Komlodi     case A_I2C_CTRL_GLOBAL:
649aab90b1cSCédric Le Goater         s->ctrl_global = value;
650aab90b1cSCédric Le Goater         break;
651*3be3d6ccSJoe Komlodi     case A_I2C_CTRL_STATUS:
65216020011SCédric Le Goater     default:
65316020011SCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
65416020011SCédric Le Goater                       __func__, offset);
65516020011SCédric Le Goater         break;
65616020011SCédric Le Goater     }
65716020011SCédric Le Goater }
65816020011SCédric Le Goater 
65916020011SCédric Le Goater static const MemoryRegionOps aspeed_i2c_bus_ops = {
66016020011SCédric Le Goater     .read = aspeed_i2c_bus_read,
66116020011SCédric Le Goater     .write = aspeed_i2c_bus_write,
66216020011SCédric Le Goater     .endianness = DEVICE_LITTLE_ENDIAN,
66316020011SCédric Le Goater };
66416020011SCédric Le Goater 
66516020011SCédric Le Goater static const MemoryRegionOps aspeed_i2c_ctrl_ops = {
66616020011SCédric Le Goater     .read = aspeed_i2c_ctrl_read,
66716020011SCédric Le Goater     .write = aspeed_i2c_ctrl_write,
66816020011SCédric Le Goater     .endianness = DEVICE_LITTLE_ENDIAN,
66916020011SCédric Le Goater };
67016020011SCédric Le Goater 
6716054fc73SCédric Le Goater static uint64_t aspeed_i2c_pool_read(void *opaque, hwaddr offset,
6726054fc73SCédric Le Goater                                      unsigned size)
6736054fc73SCédric Le Goater {
6746054fc73SCédric Le Goater     AspeedI2CState *s = opaque;
6756054fc73SCédric Le Goater     uint64_t ret = 0;
6766054fc73SCédric Le Goater     int i;
6776054fc73SCédric Le Goater 
6786054fc73SCédric Le Goater     for (i = 0; i < size; i++) {
6796054fc73SCédric Le Goater         ret |= (uint64_t) s->pool[offset + i] << (8 * i);
6806054fc73SCédric Le Goater     }
6816054fc73SCédric Le Goater 
6826054fc73SCédric Le Goater     return ret;
6836054fc73SCédric Le Goater }
6846054fc73SCédric Le Goater 
6856054fc73SCédric Le Goater static void aspeed_i2c_pool_write(void *opaque, hwaddr offset,
6866054fc73SCédric Le Goater                                   uint64_t value, unsigned size)
6876054fc73SCédric Le Goater {
6886054fc73SCédric Le Goater     AspeedI2CState *s = opaque;
6896054fc73SCédric Le Goater     int i;
6906054fc73SCédric Le Goater 
6916054fc73SCédric Le Goater     for (i = 0; i < size; i++) {
6926054fc73SCédric Le Goater         s->pool[offset + i] = (value >> (8 * i)) & 0xFF;
6936054fc73SCédric Le Goater     }
6946054fc73SCédric Le Goater }
6956054fc73SCédric Le Goater 
6966054fc73SCédric Le Goater static const MemoryRegionOps aspeed_i2c_pool_ops = {
6976054fc73SCédric Le Goater     .read = aspeed_i2c_pool_read,
6986054fc73SCédric Le Goater     .write = aspeed_i2c_pool_write,
6996054fc73SCédric Le Goater     .endianness = DEVICE_LITTLE_ENDIAN,
7006054fc73SCédric Le Goater     .valid = {
7016054fc73SCédric Le Goater         .min_access_size = 1,
7026054fc73SCédric Le Goater         .max_access_size = 4,
7036054fc73SCédric Le Goater     },
7046054fc73SCédric Le Goater };
7056054fc73SCédric Le Goater 
70616020011SCédric Le Goater static const VMStateDescription aspeed_i2c_bus_vmstate = {
70716020011SCédric Le Goater     .name = TYPE_ASPEED_I2C,
708545d6befSCédric Le Goater     .version_id = 3,
709545d6befSCédric Le Goater     .minimum_version_id = 3,
71016020011SCédric Le Goater     .fields = (VMStateField[]) {
71116020011SCédric Le Goater         VMSTATE_UINT8(id, AspeedI2CBus),
71216020011SCédric Le Goater         VMSTATE_UINT32(ctrl, AspeedI2CBus),
71316020011SCédric Le Goater         VMSTATE_UINT32_ARRAY(timing, AspeedI2CBus, 2),
71416020011SCédric Le Goater         VMSTATE_UINT32(intr_ctrl, AspeedI2CBus),
71516020011SCédric Le Goater         VMSTATE_UINT32(intr_status, AspeedI2CBus),
71616020011SCédric Le Goater         VMSTATE_UINT32(cmd, AspeedI2CBus),
71716020011SCédric Le Goater         VMSTATE_UINT32(buf, AspeedI2CBus),
7186054fc73SCédric Le Goater         VMSTATE_UINT32(pool_ctrl, AspeedI2CBus),
719545d6befSCédric Le Goater         VMSTATE_UINT32(dma_addr, AspeedI2CBus),
720545d6befSCédric Le Goater         VMSTATE_UINT32(dma_len, AspeedI2CBus),
72116020011SCédric Le Goater         VMSTATE_END_OF_LIST()
72216020011SCédric Le Goater     }
72316020011SCédric Le Goater };
72416020011SCédric Le Goater 
72516020011SCédric Le Goater static const VMStateDescription aspeed_i2c_vmstate = {
72616020011SCédric Le Goater     .name = TYPE_ASPEED_I2C,
7276054fc73SCédric Le Goater     .version_id = 2,
7286054fc73SCédric Le Goater     .minimum_version_id = 2,
72916020011SCédric Le Goater     .fields = (VMStateField[]) {
73016020011SCédric Le Goater         VMSTATE_UINT32(intr_status, AspeedI2CState),
73116020011SCédric Le Goater         VMSTATE_STRUCT_ARRAY(busses, AspeedI2CState,
73216020011SCédric Le Goater                              ASPEED_I2C_NR_BUSSES, 1, aspeed_i2c_bus_vmstate,
73316020011SCédric Le Goater                              AspeedI2CBus),
7346054fc73SCédric Le Goater         VMSTATE_UINT8_ARRAY(pool, AspeedI2CState, ASPEED_I2C_MAX_POOL_SIZE),
73516020011SCédric Le Goater         VMSTATE_END_OF_LIST()
73616020011SCédric Le Goater     }
73716020011SCédric Le Goater };
73816020011SCédric Le Goater 
73916020011SCédric Le Goater static void aspeed_i2c_reset(DeviceState *dev)
74016020011SCédric Le Goater {
74116020011SCédric Le Goater     AspeedI2CState *s = ASPEED_I2C(dev);
74216020011SCédric Le Goater 
74316020011SCédric Le Goater     s->intr_status = 0;
74460261038SCédric Le Goater }
74560261038SCédric Le Goater 
74660261038SCédric Le Goater static void aspeed_i2c_instance_init(Object *obj)
74760261038SCédric Le Goater {
74860261038SCédric Le Goater     AspeedI2CState *s = ASPEED_I2C(obj);
74960261038SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
75060261038SCédric Le Goater     int i;
75116020011SCédric Le Goater 
752f7da1aa8SCédric Le Goater     for (i = 0; i < aic->num_busses; i++) {
75360261038SCédric Le Goater         object_initialize_child(obj, "bus[*]", &s->busses[i],
75460261038SCédric Le Goater                                 TYPE_ASPEED_I2C_BUS);
75516020011SCédric Le Goater     }
75616020011SCédric Le Goater }
75716020011SCédric Le Goater 
75816020011SCédric Le Goater /*
759f7da1aa8SCédric Le Goater  * Address Definitions (AST2400 and AST2500)
76016020011SCédric Le Goater  *
76116020011SCédric Le Goater  *   0x000 ... 0x03F: Global Register
76216020011SCédric Le Goater  *   0x040 ... 0x07F: Device 1
76316020011SCédric Le Goater  *   0x080 ... 0x0BF: Device 2
76416020011SCédric Le Goater  *   0x0C0 ... 0x0FF: Device 3
76516020011SCédric Le Goater  *   0x100 ... 0x13F: Device 4
76616020011SCédric Le Goater  *   0x140 ... 0x17F: Device 5
76716020011SCédric Le Goater  *   0x180 ... 0x1BF: Device 6
76816020011SCédric Le Goater  *   0x1C0 ... 0x1FF: Device 7
76916020011SCédric Le Goater  *   0x200 ... 0x2FF: Buffer Pool  (unused in linux driver)
77016020011SCédric Le Goater  *   0x300 ... 0x33F: Device 8
77116020011SCédric Le Goater  *   0x340 ... 0x37F: Device 9
77216020011SCédric Le Goater  *   0x380 ... 0x3BF: Device 10
77316020011SCédric Le Goater  *   0x3C0 ... 0x3FF: Device 11
77416020011SCédric Le Goater  *   0x400 ... 0x43F: Device 12
77516020011SCédric Le Goater  *   0x440 ... 0x47F: Device 13
77616020011SCédric Le Goater  *   0x480 ... 0x4BF: Device 14
77716020011SCédric Le Goater  *   0x800 ... 0xFFF: Buffer Pool  (unused in linux driver)
77816020011SCédric Le Goater  */
77916020011SCédric Le Goater static void aspeed_i2c_realize(DeviceState *dev, Error **errp)
78016020011SCédric Le Goater {
78116020011SCédric Le Goater     int i;
78216020011SCédric Le Goater     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
78316020011SCédric Le Goater     AspeedI2CState *s = ASPEED_I2C(dev);
784f7da1aa8SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
78516020011SCédric Le Goater 
78616020011SCédric Le Goater     sysbus_init_irq(sbd, &s->irq);
78716020011SCédric Le Goater     memory_region_init_io(&s->iomem, OBJECT(s), &aspeed_i2c_ctrl_ops, s,
78816020011SCédric Le Goater                           "aspeed.i2c", 0x1000);
78916020011SCédric Le Goater     sysbus_init_mmio(sbd, &s->iomem);
79016020011SCédric Le Goater 
791f7da1aa8SCédric Le Goater     for (i = 0; i < aic->num_busses; i++) {
79260261038SCédric Le Goater         Object *bus = OBJECT(&s->busses[i]);
793f7da1aa8SCédric Le Goater         int offset = i < aic->gap ? 1 : 5;
79451dd4923SCédric Le Goater 
79560261038SCédric Le Goater         if (!object_property_set_link(bus, "controller", OBJECT(s), errp)) {
79660261038SCédric Le Goater             return;
79760261038SCédric Le Goater         }
79860261038SCédric Le Goater 
79960261038SCédric Le Goater         if (!object_property_set_uint(bus, "bus-id", i, errp)) {
80060261038SCédric Le Goater             return;
80160261038SCédric Le Goater         }
80260261038SCédric Le Goater 
80360261038SCédric Le Goater         if (!sysbus_realize(SYS_BUS_DEVICE(bus), errp)) {
80460261038SCédric Le Goater             return;
80560261038SCédric Le Goater         }
80660261038SCédric Le Goater 
807f7da1aa8SCédric Le Goater         memory_region_add_subregion(&s->iomem, aic->reg_size * (i + offset),
80816020011SCédric Le Goater                                     &s->busses[i].mr);
80916020011SCédric Le Goater     }
8106054fc73SCédric Le Goater 
8116054fc73SCédric Le Goater     memory_region_init_io(&s->pool_iomem, OBJECT(s), &aspeed_i2c_pool_ops, s,
8126054fc73SCédric Le Goater                           "aspeed.i2c-pool", aic->pool_size);
8136054fc73SCédric Le Goater     memory_region_add_subregion(&s->iomem, aic->pool_base, &s->pool_iomem);
814545d6befSCédric Le Goater 
815545d6befSCédric Le Goater     if (aic->has_dma) {
816545d6befSCédric Le Goater         if (!s->dram_mr) {
817545d6befSCédric Le Goater             error_setg(errp, TYPE_ASPEED_I2C ": 'dram' link not set");
818545d6befSCédric Le Goater             return;
81916020011SCédric Le Goater         }
82016020011SCédric Le Goater 
8213f7a53b2SCédric Le Goater         address_space_init(&s->dram_as, s->dram_mr,
8223f7a53b2SCédric Le Goater                            TYPE_ASPEED_I2C "-dma-dram");
823545d6befSCédric Le Goater     }
824545d6befSCédric Le Goater }
825545d6befSCédric Le Goater 
826545d6befSCédric Le Goater static Property aspeed_i2c_properties[] = {
827545d6befSCédric Le Goater     DEFINE_PROP_LINK("dram", AspeedI2CState, dram_mr,
828545d6befSCédric Le Goater                      TYPE_MEMORY_REGION, MemoryRegion *),
829545d6befSCédric Le Goater     DEFINE_PROP_END_OF_LIST(),
830545d6befSCédric Le Goater };
831545d6befSCédric Le Goater 
83216020011SCédric Le Goater static void aspeed_i2c_class_init(ObjectClass *klass, void *data)
83316020011SCédric Le Goater {
83416020011SCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
83516020011SCédric Le Goater 
83616020011SCédric Le Goater     dc->vmsd = &aspeed_i2c_vmstate;
83716020011SCédric Le Goater     dc->reset = aspeed_i2c_reset;
8384f67d30bSMarc-André Lureau     device_class_set_props(dc, aspeed_i2c_properties);
83916020011SCédric Le Goater     dc->realize = aspeed_i2c_realize;
84016020011SCédric Le Goater     dc->desc = "Aspeed I2C Controller";
84116020011SCédric Le Goater }
84216020011SCédric Le Goater 
84316020011SCédric Le Goater static const TypeInfo aspeed_i2c_info = {
84416020011SCédric Le Goater     .name          = TYPE_ASPEED_I2C,
84516020011SCédric Le Goater     .parent        = TYPE_SYS_BUS_DEVICE,
84660261038SCédric Le Goater     .instance_init = aspeed_i2c_instance_init,
84716020011SCédric Le Goater     .instance_size = sizeof(AspeedI2CState),
84816020011SCédric Le Goater     .class_init    = aspeed_i2c_class_init,
849f7da1aa8SCédric Le Goater     .class_size = sizeof(AspeedI2CClass),
850f7da1aa8SCédric Le Goater     .abstract   = true,
851f7da1aa8SCédric Le Goater };
852f7da1aa8SCédric Le Goater 
85360261038SCédric Le Goater static void aspeed_i2c_bus_reset(DeviceState *dev)
85460261038SCédric Le Goater {
85560261038SCédric Le Goater     AspeedI2CBus *s = ASPEED_I2C_BUS(dev);
85660261038SCédric Le Goater 
85760261038SCédric Le Goater     s->intr_ctrl = 0;
85860261038SCédric Le Goater     s->intr_status = 0;
85960261038SCédric Le Goater     s->cmd = 0;
86060261038SCédric Le Goater     s->buf = 0;
86160261038SCédric Le Goater     s->dma_addr = 0;
86260261038SCédric Le Goater     s->dma_len = 0;
86360261038SCédric Le Goater     i2c_end_transfer(s->bus);
86460261038SCédric Le Goater }
86560261038SCédric Le Goater 
86660261038SCédric Le Goater static void aspeed_i2c_bus_realize(DeviceState *dev, Error **errp)
86760261038SCédric Le Goater {
86860261038SCédric Le Goater     AspeedI2CBus *s = ASPEED_I2C_BUS(dev);
86960261038SCédric Le Goater     AspeedI2CClass *aic;
87060261038SCédric Le Goater     g_autofree char *name = g_strdup_printf(TYPE_ASPEED_I2C_BUS ".%d", s->id);
87160261038SCédric Le Goater 
87260261038SCédric Le Goater     if (!s->controller) {
87360261038SCédric Le Goater         error_setg(errp, TYPE_ASPEED_I2C_BUS ": 'controller' link not set");
87460261038SCédric Le Goater         return;
87560261038SCédric Le Goater     }
87660261038SCédric Le Goater 
87760261038SCédric Le Goater     aic = ASPEED_I2C_GET_CLASS(s->controller);
87860261038SCédric Le Goater 
87960261038SCédric Le Goater     sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->irq);
88060261038SCédric Le Goater 
88160261038SCédric Le Goater     s->bus = i2c_init_bus(dev, name);
88260261038SCédric Le Goater 
88360261038SCédric Le Goater     memory_region_init_io(&s->mr, OBJECT(s), &aspeed_i2c_bus_ops,
88460261038SCédric Le Goater                           s, name, aic->reg_size);
88560261038SCédric Le Goater     sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->mr);
88660261038SCédric Le Goater }
88760261038SCédric Le Goater 
88860261038SCédric Le Goater static Property aspeed_i2c_bus_properties[] = {
88960261038SCédric Le Goater     DEFINE_PROP_UINT8("bus-id", AspeedI2CBus, id, 0),
89060261038SCédric Le Goater     DEFINE_PROP_LINK("controller", AspeedI2CBus, controller, TYPE_ASPEED_I2C,
89160261038SCédric Le Goater                      AspeedI2CState *),
89260261038SCédric Le Goater     DEFINE_PROP_END_OF_LIST(),
89360261038SCédric Le Goater };
89460261038SCédric Le Goater 
89560261038SCédric Le Goater static void aspeed_i2c_bus_class_init(ObjectClass *klass, void *data)
89660261038SCédric Le Goater {
89760261038SCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
89860261038SCédric Le Goater 
89960261038SCédric Le Goater     dc->desc = "Aspeed I2C Bus";
90060261038SCédric Le Goater     dc->realize = aspeed_i2c_bus_realize;
90160261038SCédric Le Goater     dc->reset = aspeed_i2c_bus_reset;
90260261038SCédric Le Goater     device_class_set_props(dc, aspeed_i2c_bus_properties);
90360261038SCédric Le Goater }
90460261038SCédric Le Goater 
90560261038SCédric Le Goater static const TypeInfo aspeed_i2c_bus_info = {
90660261038SCédric Le Goater     .name           = TYPE_ASPEED_I2C_BUS,
90760261038SCédric Le Goater     .parent         = TYPE_SYS_BUS_DEVICE,
90860261038SCédric Le Goater     .instance_size  = sizeof(AspeedI2CBus),
90960261038SCédric Le Goater     .class_init     = aspeed_i2c_bus_class_init,
91060261038SCédric Le Goater };
91160261038SCédric Le Goater 
91251dd4923SCédric Le Goater static qemu_irq aspeed_2400_i2c_bus_get_irq(AspeedI2CBus *bus)
91351dd4923SCédric Le Goater {
91451dd4923SCédric Le Goater     return bus->controller->irq;
91551dd4923SCédric Le Goater }
91651dd4923SCédric Le Goater 
9176054fc73SCédric Le Goater static uint8_t *aspeed_2400_i2c_bus_pool_base(AspeedI2CBus *bus)
9186054fc73SCédric Le Goater {
9196054fc73SCédric Le Goater     uint8_t *pool_page =
920*3be3d6ccSJoe Komlodi         &bus->controller->pool[FIELD_EX32(bus->ctrl, I2CD_FUN_CTRL,
921*3be3d6ccSJoe Komlodi                                           POOL_PAGE_SEL) * 0x100];
9226054fc73SCédric Le Goater 
923*3be3d6ccSJoe Komlodi     return &pool_page[FIELD_EX32(bus->pool_ctrl, I2CD_POOL_CTRL, OFFSET)];
9246054fc73SCédric Le Goater }
9256054fc73SCédric Le Goater 
926f7da1aa8SCédric Le Goater static void aspeed_2400_i2c_class_init(ObjectClass *klass, void *data)
927f7da1aa8SCédric Le Goater {
928f7da1aa8SCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
929f7da1aa8SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_CLASS(klass);
930f7da1aa8SCédric Le Goater 
931f7da1aa8SCédric Le Goater     dc->desc = "ASPEED 2400 I2C Controller";
932f7da1aa8SCédric Le Goater 
933f7da1aa8SCédric Le Goater     aic->num_busses = 14;
934f7da1aa8SCédric Le Goater     aic->reg_size = 0x40;
935f7da1aa8SCédric Le Goater     aic->gap = 7;
93651dd4923SCédric Le Goater     aic->bus_get_irq = aspeed_2400_i2c_bus_get_irq;
9376054fc73SCédric Le Goater     aic->pool_size = 0x800;
9386054fc73SCédric Le Goater     aic->pool_base = 0x800;
9396054fc73SCédric Le Goater     aic->bus_pool_base = aspeed_2400_i2c_bus_pool_base;
940f7da1aa8SCédric Le Goater }
941f7da1aa8SCédric Le Goater 
942f7da1aa8SCédric Le Goater static const TypeInfo aspeed_2400_i2c_info = {
943f7da1aa8SCédric Le Goater     .name = TYPE_ASPEED_2400_I2C,
944f7da1aa8SCédric Le Goater     .parent = TYPE_ASPEED_I2C,
945f7da1aa8SCédric Le Goater     .class_init = aspeed_2400_i2c_class_init,
946f7da1aa8SCédric Le Goater };
947f7da1aa8SCédric Le Goater 
94851dd4923SCédric Le Goater static qemu_irq aspeed_2500_i2c_bus_get_irq(AspeedI2CBus *bus)
94951dd4923SCédric Le Goater {
95051dd4923SCédric Le Goater     return bus->controller->irq;
95151dd4923SCédric Le Goater }
95251dd4923SCédric Le Goater 
9536054fc73SCédric Le Goater static uint8_t *aspeed_2500_i2c_bus_pool_base(AspeedI2CBus *bus)
9546054fc73SCédric Le Goater {
9556054fc73SCédric Le Goater     return &bus->controller->pool[bus->id * 0x10];
9566054fc73SCédric Le Goater }
9576054fc73SCédric Le Goater 
958f7da1aa8SCédric Le Goater static void aspeed_2500_i2c_class_init(ObjectClass *klass, void *data)
959f7da1aa8SCédric Le Goater {
960f7da1aa8SCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
961f7da1aa8SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_CLASS(klass);
962f7da1aa8SCédric Le Goater 
963f7da1aa8SCédric Le Goater     dc->desc = "ASPEED 2500 I2C Controller";
964f7da1aa8SCédric Le Goater 
965f7da1aa8SCédric Le Goater     aic->num_busses = 14;
966f7da1aa8SCédric Le Goater     aic->reg_size = 0x40;
967f7da1aa8SCédric Le Goater     aic->gap = 7;
96851dd4923SCédric Le Goater     aic->bus_get_irq = aspeed_2500_i2c_bus_get_irq;
9696054fc73SCédric Le Goater     aic->pool_size = 0x100;
9706054fc73SCédric Le Goater     aic->pool_base = 0x200;
9716054fc73SCédric Le Goater     aic->bus_pool_base = aspeed_2500_i2c_bus_pool_base;
972aab90b1cSCédric Le Goater     aic->check_sram = true;
973545d6befSCédric Le Goater     aic->has_dma = true;
974f7da1aa8SCédric Le Goater }
975f7da1aa8SCédric Le Goater 
976f7da1aa8SCédric Le Goater static const TypeInfo aspeed_2500_i2c_info = {
977f7da1aa8SCédric Le Goater     .name = TYPE_ASPEED_2500_I2C,
978f7da1aa8SCédric Le Goater     .parent = TYPE_ASPEED_I2C,
979f7da1aa8SCédric Le Goater     .class_init = aspeed_2500_i2c_class_init,
98016020011SCédric Le Goater };
98116020011SCédric Le Goater 
98251dd4923SCédric Le Goater static qemu_irq aspeed_2600_i2c_bus_get_irq(AspeedI2CBus *bus)
98351dd4923SCédric Le Goater {
98451dd4923SCédric Le Goater     return bus->irq;
98551dd4923SCédric Le Goater }
98651dd4923SCédric Le Goater 
9876054fc73SCédric Le Goater static uint8_t *aspeed_2600_i2c_bus_pool_base(AspeedI2CBus *bus)
9886054fc73SCédric Le Goater {
9896054fc73SCédric Le Goater    return &bus->controller->pool[bus->id * 0x20];
9906054fc73SCédric Le Goater }
9916054fc73SCédric Le Goater 
99251dd4923SCédric Le Goater static void aspeed_2600_i2c_class_init(ObjectClass *klass, void *data)
99351dd4923SCédric Le Goater {
99451dd4923SCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
99551dd4923SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_CLASS(klass);
99651dd4923SCédric Le Goater 
99751dd4923SCédric Le Goater     dc->desc = "ASPEED 2600 I2C Controller";
99851dd4923SCédric Le Goater 
99951dd4923SCédric Le Goater     aic->num_busses = 16;
100051dd4923SCédric Le Goater     aic->reg_size = 0x80;
100151dd4923SCédric Le Goater     aic->gap = -1; /* no gap */
100251dd4923SCédric Le Goater     aic->bus_get_irq = aspeed_2600_i2c_bus_get_irq;
10036054fc73SCédric Le Goater     aic->pool_size = 0x200;
10046054fc73SCédric Le Goater     aic->pool_base = 0xC00;
10056054fc73SCédric Le Goater     aic->bus_pool_base = aspeed_2600_i2c_bus_pool_base;
1006545d6befSCédric Le Goater     aic->has_dma = true;
100751dd4923SCédric Le Goater }
100851dd4923SCédric Le Goater 
100951dd4923SCédric Le Goater static const TypeInfo aspeed_2600_i2c_info = {
101051dd4923SCédric Le Goater     .name = TYPE_ASPEED_2600_I2C,
101151dd4923SCédric Le Goater     .parent = TYPE_ASPEED_I2C,
101251dd4923SCédric Le Goater     .class_init = aspeed_2600_i2c_class_init,
101351dd4923SCédric Le Goater };
101451dd4923SCédric Le Goater 
101516020011SCédric Le Goater static void aspeed_i2c_register_types(void)
101616020011SCédric Le Goater {
101760261038SCédric Le Goater     type_register_static(&aspeed_i2c_bus_info);
101816020011SCédric Le Goater     type_register_static(&aspeed_i2c_info);
1019f7da1aa8SCédric Le Goater     type_register_static(&aspeed_2400_i2c_info);
1020f7da1aa8SCédric Le Goater     type_register_static(&aspeed_2500_i2c_info);
102151dd4923SCédric Le Goater     type_register_static(&aspeed_2600_i2c_info);
102216020011SCédric Le Goater }
102316020011SCédric Le Goater 
102416020011SCédric Le Goater type_init(aspeed_i2c_register_types)
102516020011SCédric Le Goater 
102616020011SCédric Le Goater 
10277a204cbdSPhilippe Mathieu-Daudé I2CBus *aspeed_i2c_get_bus(AspeedI2CState *s, int busnr)
102816020011SCédric Le Goater {
1029f7da1aa8SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
103016020011SCédric Le Goater     I2CBus *bus = NULL;
103116020011SCédric Le Goater 
1032f7da1aa8SCédric Le Goater     if (busnr >= 0 && busnr < aic->num_busses) {
103316020011SCédric Le Goater         bus = s->busses[busnr].bus;
103416020011SCédric Le Goater     }
103516020011SCédric Le Goater 
103616020011SCédric Le Goater     return bus;
103716020011SCédric Le Goater }
1038