xref: /qemu/hw/i2c/aspeed_i2c.c (revision 3f7a53b22469ed4b80649ef92b94378c60dda5d0)
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"
3166cc84a1SCédric Le Goater #include "trace.h"
3216020011SCédric Le Goater 
3316020011SCédric Le Goater /* I2C Global Register */
3416020011SCédric Le Goater 
3516020011SCédric Le Goater #define I2C_CTRL_STATUS         0x00        /* Device Interrupt Status */
3616020011SCédric Le Goater #define I2C_CTRL_ASSIGN         0x08        /* Device Interrupt Target
3716020011SCédric Le Goater                                                Assignment */
38aab90b1cSCédric Le Goater #define I2C_CTRL_GLOBAL         0x0C        /* Global Control Register */
39aab90b1cSCédric Le Goater #define   I2C_CTRL_SRAM_EN                 BIT(0)
4016020011SCédric Le Goater 
4116020011SCédric Le Goater /* I2C Device (Bus) Register */
4216020011SCédric Le Goater 
4316020011SCédric Le Goater #define I2CD_FUN_CTRL_REG       0x00       /* I2CD Function Control  */
446054fc73SCédric Le Goater #define   I2CD_POOL_PAGE_SEL(x)            (((x) >> 20) & 0x7)  /* AST2400 */
4516020011SCédric Le Goater #define   I2CD_M_SDA_LOCK_EN               (0x1 << 16)
4616020011SCédric Le Goater #define   I2CD_MULTI_MASTER_DIS            (0x1 << 15)
4716020011SCédric Le Goater #define   I2CD_M_SCL_DRIVE_EN              (0x1 << 14)
4816020011SCédric Le Goater #define   I2CD_MSB_STS                     (0x1 << 9)
4916020011SCédric Le Goater #define   I2CD_SDA_DRIVE_1T_EN             (0x1 << 8)
5016020011SCédric Le Goater #define   I2CD_M_SDA_DRIVE_1T_EN           (0x1 << 7)
5116020011SCédric Le Goater #define   I2CD_M_HIGH_SPEED_EN             (0x1 << 6)
5216020011SCédric Le Goater #define   I2CD_DEF_ADDR_EN                 (0x1 << 5)
5316020011SCédric Le Goater #define   I2CD_DEF_ALERT_EN                (0x1 << 4)
5416020011SCédric Le Goater #define   I2CD_DEF_ARP_EN                  (0x1 << 3)
5516020011SCédric Le Goater #define   I2CD_DEF_GCALL_EN                (0x1 << 2)
5616020011SCédric Le Goater #define   I2CD_SLAVE_EN                    (0x1 << 1)
5716020011SCédric Le Goater #define   I2CD_MASTER_EN                   (0x1)
5816020011SCédric Le Goater 
5916020011SCédric Le Goater #define I2CD_AC_TIMING_REG1     0x04       /* Clock and AC Timing Control #1 */
6016020011SCédric Le Goater #define I2CD_AC_TIMING_REG2     0x08       /* Clock and AC Timing Control #1 */
6116020011SCédric Le Goater #define I2CD_INTR_CTRL_REG      0x0c       /* I2CD Interrupt Control */
6216020011SCédric Le Goater #define I2CD_INTR_STS_REG       0x10       /* I2CD Interrupt Status */
635540cb97SCédric Le Goater 
645540cb97SCédric Le Goater #define   I2CD_INTR_SLAVE_ADDR_MATCH       (0x1 << 31) /* 0: addr1 1: addr2 */
655540cb97SCédric Le Goater #define   I2CD_INTR_SLAVE_ADDR_RX_PENDING  (0x1 << 30)
665540cb97SCédric Le Goater /* bits[19-16] Reserved */
675540cb97SCédric Le Goater 
685540cb97SCédric Le Goater /* All bits below are cleared by writing 1 */
695540cb97SCédric Le Goater #define   I2CD_INTR_SLAVE_INACTIVE_TIMEOUT (0x1 << 15)
7016020011SCédric Le Goater #define   I2CD_INTR_SDA_DL_TIMEOUT         (0x1 << 14)
7116020011SCédric Le Goater #define   I2CD_INTR_BUS_RECOVER_DONE       (0x1 << 13)
7216020011SCédric Le Goater #define   I2CD_INTR_SMBUS_ALERT            (0x1 << 12) /* Bus [0-3] only */
7316020011SCédric Le Goater #define   I2CD_INTR_SMBUS_ARP_ADDR         (0x1 << 11) /* Removed */
7416020011SCédric Le Goater #define   I2CD_INTR_SMBUS_DEV_ALERT_ADDR   (0x1 << 10) /* Removed */
7516020011SCédric Le Goater #define   I2CD_INTR_SMBUS_DEF_ADDR         (0x1 << 9)  /* Removed */
7616020011SCédric Le Goater #define   I2CD_INTR_GCALL_ADDR             (0x1 << 8)  /* Removed */
775540cb97SCédric Le Goater #define   I2CD_INTR_SLAVE_ADDR_RX_MATCH    (0x1 << 7)  /* use RX_DONE */
7816020011SCédric Le Goater #define   I2CD_INTR_SCL_TIMEOUT            (0x1 << 6)
7916020011SCédric Le Goater #define   I2CD_INTR_ABNORMAL               (0x1 << 5)
8016020011SCédric Le Goater #define   I2CD_INTR_NORMAL_STOP            (0x1 << 4)
8116020011SCédric Le Goater #define   I2CD_INTR_ARBIT_LOSS             (0x1 << 3)
8216020011SCédric Le Goater #define   I2CD_INTR_RX_DONE                (0x1 << 2)
8316020011SCédric Le Goater #define   I2CD_INTR_TX_NAK                 (0x1 << 1)
8416020011SCédric Le Goater #define   I2CD_INTR_TX_ACK                 (0x1 << 0)
8516020011SCédric Le Goater 
8616020011SCédric Le Goater #define I2CD_CMD_REG            0x14       /* I2CD Command/Status */
8716020011SCédric Le Goater #define   I2CD_SDA_OE                      (0x1 << 28)
8816020011SCédric Le Goater #define   I2CD_SDA_O                       (0x1 << 27)
8916020011SCédric Le Goater #define   I2CD_SCL_OE                      (0x1 << 26)
9016020011SCédric Le Goater #define   I2CD_SCL_O                       (0x1 << 25)
9116020011SCédric Le Goater #define   I2CD_TX_TIMING                   (0x1 << 24)
9216020011SCédric Le Goater #define   I2CD_TX_STATUS                   (0x1 << 23)
9316020011SCédric Le Goater 
9416020011SCédric Le Goater #define   I2CD_TX_STATE_SHIFT              19 /* Tx State Machine */
9516020011SCédric Le Goater #define   I2CD_TX_STATE_MASK                  0xf
9616020011SCédric Le Goater #define     I2CD_IDLE                         0x0
9716020011SCédric Le Goater #define     I2CD_MACTIVE                      0x8
9816020011SCédric Le Goater #define     I2CD_MSTART                       0x9
9916020011SCédric Le Goater #define     I2CD_MSTARTR                      0xa
10016020011SCédric Le Goater #define     I2CD_MSTOP                        0xb
10116020011SCédric Le Goater #define     I2CD_MTXD                         0xc
10216020011SCédric Le Goater #define     I2CD_MRXACK                       0xd
10316020011SCédric Le Goater #define     I2CD_MRXD                         0xe
10416020011SCédric Le Goater #define     I2CD_MTXACK                       0xf
10516020011SCédric Le Goater #define     I2CD_SWAIT                        0x1
10616020011SCédric Le Goater #define     I2CD_SRXD                         0x4
10716020011SCédric Le Goater #define     I2CD_STXACK                       0x5
10816020011SCédric Le Goater #define     I2CD_STXD                         0x6
10916020011SCédric Le Goater #define     I2CD_SRXACK                       0x7
11016020011SCédric Le Goater #define     I2CD_RECOVER                      0x3
11116020011SCédric Le Goater 
11216020011SCédric Le Goater #define   I2CD_SCL_LINE_STS                (0x1 << 18)
11316020011SCédric Le Goater #define   I2CD_SDA_LINE_STS                (0x1 << 17)
11416020011SCédric Le Goater #define   I2CD_BUS_BUSY_STS                (0x1 << 16)
11516020011SCédric Le Goater #define   I2CD_SDA_OE_OUT_DIR              (0x1 << 15)
11616020011SCédric Le Goater #define   I2CD_SDA_O_OUT_DIR               (0x1 << 14)
11716020011SCédric Le Goater #define   I2CD_SCL_OE_OUT_DIR              (0x1 << 13)
11816020011SCédric Le Goater #define   I2CD_SCL_O_OUT_DIR               (0x1 << 12)
11916020011SCédric Le Goater #define   I2CD_BUS_RECOVER_CMD_EN          (0x1 << 11)
12016020011SCédric Le Goater #define   I2CD_S_ALT_EN                    (0x1 << 10)
12116020011SCédric Le Goater 
12216020011SCédric Le Goater /* Command Bit */
1236054fc73SCédric Le Goater #define   I2CD_RX_DMA_ENABLE               (0x1 << 9)
1246054fc73SCédric Le Goater #define   I2CD_TX_DMA_ENABLE               (0x1 << 8)
1256054fc73SCédric Le Goater #define   I2CD_RX_BUFF_ENABLE              (0x1 << 7)
1266054fc73SCédric Le Goater #define   I2CD_TX_BUFF_ENABLE              (0x1 << 6)
12716020011SCédric Le Goater #define   I2CD_M_STOP_CMD                  (0x1 << 5)
12816020011SCédric Le Goater #define   I2CD_M_S_RX_CMD_LAST             (0x1 << 4)
12916020011SCédric Le Goater #define   I2CD_M_RX_CMD                    (0x1 << 3)
13016020011SCédric Le Goater #define   I2CD_S_TX_CMD                    (0x1 << 2)
13116020011SCédric Le Goater #define   I2CD_M_TX_CMD                    (0x1 << 1)
13216020011SCédric Le Goater #define   I2CD_M_START_CMD                 (0x1)
13316020011SCédric Le Goater 
13416020011SCédric Le Goater #define I2CD_DEV_ADDR_REG       0x18       /* Slave Device Address */
1356054fc73SCédric Le Goater #define I2CD_POOL_CTRL_REG      0x1c       /* Pool Buffer Control */
1366054fc73SCédric Le Goater #define   I2CD_POOL_RX_COUNT(x)            (((x) >> 24) & 0xff)
1376054fc73SCédric Le Goater #define   I2CD_POOL_RX_SIZE(x)             ((((x) >> 16) & 0xff) + 1)
1386054fc73SCédric Le Goater #define   I2CD_POOL_TX_COUNT(x)            ((((x) >> 8) & 0xff) + 1)
1396054fc73SCédric Le Goater #define   I2CD_POOL_OFFSET(x)              (((x) & 0x3f) << 2)  /* AST2400 */
14016020011SCédric Le Goater #define I2CD_BYTE_BUF_REG       0x20       /* Transmit/Receive Byte Buffer */
14116020011SCédric Le Goater #define   I2CD_BYTE_BUF_TX_SHIFT           0
14216020011SCédric Le Goater #define   I2CD_BYTE_BUF_TX_MASK            0xff
14316020011SCédric Le Goater #define   I2CD_BYTE_BUF_RX_SHIFT           8
14416020011SCédric Le Goater #define   I2CD_BYTE_BUF_RX_MASK            0xff
145545d6befSCédric Le Goater #define I2CD_DMA_ADDR           0x24       /* DMA Buffer Address */
146545d6befSCédric Le Goater #define I2CD_DMA_LEN            0x28       /* DMA Transfer Length < 4KB */
14716020011SCédric Le Goater 
14816020011SCédric Le Goater static inline bool aspeed_i2c_bus_is_master(AspeedI2CBus *bus)
14916020011SCédric Le Goater {
15016020011SCédric Le Goater     return bus->ctrl & I2CD_MASTER_EN;
15116020011SCédric Le Goater }
15216020011SCédric Le Goater 
15316020011SCédric Le Goater static inline bool aspeed_i2c_bus_is_enabled(AspeedI2CBus *bus)
15416020011SCédric Le Goater {
15516020011SCédric Le Goater     return bus->ctrl & (I2CD_MASTER_EN | I2CD_SLAVE_EN);
15616020011SCédric Le Goater }
15716020011SCédric Le Goater 
15816020011SCédric Le Goater static inline void aspeed_i2c_bus_raise_interrupt(AspeedI2CBus *bus)
15916020011SCédric Le Goater {
16051dd4923SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
16151dd4923SCédric Le Goater 
16266cc84a1SCédric Le Goater     trace_aspeed_i2c_bus_raise_interrupt(bus->intr_status,
16366cc84a1SCédric Le Goater           bus->intr_status & I2CD_INTR_TX_NAK ? "nak|" : "",
16466cc84a1SCédric Le Goater           bus->intr_status & I2CD_INTR_TX_ACK ? "ack|" : "",
16566cc84a1SCédric Le Goater           bus->intr_status & I2CD_INTR_RX_DONE ? "done|" : "",
16666cc84a1SCédric Le Goater           bus->intr_status & I2CD_INTR_NORMAL_STOP ? "normal|" : "",
16766cc84a1SCédric Le Goater           bus->intr_status & I2CD_INTR_ABNORMAL ? "abnormal" : "");
16866cc84a1SCédric Le Goater 
16916020011SCédric Le Goater     bus->intr_status &= bus->intr_ctrl;
17016020011SCédric Le Goater     if (bus->intr_status) {
17116020011SCédric Le Goater         bus->controller->intr_status |= 1 << bus->id;
17251dd4923SCédric Le Goater         qemu_irq_raise(aic->bus_get_irq(bus));
17316020011SCédric Le Goater     }
17416020011SCédric Le Goater }
17516020011SCédric Le Goater 
17616020011SCédric Le Goater static uint64_t aspeed_i2c_bus_read(void *opaque, hwaddr offset,
17716020011SCédric Le Goater                                     unsigned size)
17816020011SCédric Le Goater {
17916020011SCédric Le Goater     AspeedI2CBus *bus = opaque;
180545d6befSCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
18166cc84a1SCédric Le Goater     uint64_t value = -1;
18216020011SCédric Le Goater 
18316020011SCédric Le Goater     switch (offset) {
18416020011SCédric Le Goater     case I2CD_FUN_CTRL_REG:
18566cc84a1SCédric Le Goater         value = bus->ctrl;
18666cc84a1SCédric Le Goater         break;
18716020011SCédric Le Goater     case I2CD_AC_TIMING_REG1:
18866cc84a1SCédric Le Goater         value = bus->timing[0];
18966cc84a1SCédric Le Goater         break;
19016020011SCédric Le Goater     case I2CD_AC_TIMING_REG2:
19166cc84a1SCédric Le Goater         value = bus->timing[1];
19266cc84a1SCédric Le Goater         break;
19316020011SCédric Le Goater     case I2CD_INTR_CTRL_REG:
19466cc84a1SCédric Le Goater         value = bus->intr_ctrl;
19566cc84a1SCédric Le Goater         break;
19616020011SCédric Le Goater     case I2CD_INTR_STS_REG:
19766cc84a1SCédric Le Goater         value = bus->intr_status;
19866cc84a1SCédric Le Goater         break;
1996054fc73SCédric Le Goater     case I2CD_POOL_CTRL_REG:
20066cc84a1SCédric Le Goater         value = bus->pool_ctrl;
20166cc84a1SCédric Le Goater         break;
20216020011SCédric Le Goater     case I2CD_BYTE_BUF_REG:
20366cc84a1SCédric Le Goater         value = bus->buf;
20466cc84a1SCédric Le Goater         break;
20516020011SCédric Le Goater     case I2CD_CMD_REG:
20666cc84a1SCédric Le Goater         value = bus->cmd | (i2c_bus_busy(bus->bus) << 16);
20766cc84a1SCédric Le Goater         break;
208545d6befSCédric Le Goater     case I2CD_DMA_ADDR:
209545d6befSCédric Le Goater         if (!aic->has_dma) {
210545d6befSCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n",  __func__);
21166cc84a1SCédric Le Goater             break;
212545d6befSCédric Le Goater         }
21366cc84a1SCédric Le Goater         value = bus->dma_addr;
21466cc84a1SCédric Le Goater         break;
215545d6befSCédric Le Goater     case I2CD_DMA_LEN:
216545d6befSCédric Le Goater         if (!aic->has_dma) {
217545d6befSCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n",  __func__);
21866cc84a1SCédric Le Goater             break;
219545d6befSCédric Le Goater         }
22066cc84a1SCédric Le Goater         value = bus->dma_len;
22166cc84a1SCédric Le Goater         break;
22266cc84a1SCédric Le Goater 
22316020011SCédric Le Goater     default:
22416020011SCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR,
22516020011SCédric Le Goater                       "%s: Bad offset 0x%" HWADDR_PRIx "\n", __func__, offset);
22666cc84a1SCédric Le Goater         value = -1;
22766cc84a1SCédric Le Goater         break;
22816020011SCédric Le Goater     }
22966cc84a1SCédric Le Goater 
23066cc84a1SCédric Le Goater     trace_aspeed_i2c_bus_read(bus->id, offset, size, value);
23166cc84a1SCédric Le Goater     return value;
23216020011SCédric Le Goater }
23316020011SCédric Le Goater 
2344960f084SCédric Le Goater static void aspeed_i2c_set_state(AspeedI2CBus *bus, uint8_t state)
2354960f084SCédric Le Goater {
2364960f084SCédric Le Goater     bus->cmd &= ~(I2CD_TX_STATE_MASK << I2CD_TX_STATE_SHIFT);
2374960f084SCédric Le Goater     bus->cmd |= (state & I2CD_TX_STATE_MASK) << I2CD_TX_STATE_SHIFT;
2384960f084SCédric Le Goater }
2394960f084SCédric Le Goater 
2404960f084SCédric Le Goater static uint8_t aspeed_i2c_get_state(AspeedI2CBus *bus)
2414960f084SCédric Le Goater {
2424960f084SCédric Le Goater     return (bus->cmd >> I2CD_TX_STATE_SHIFT) & I2CD_TX_STATE_MASK;
2434960f084SCédric Le Goater }
2444960f084SCédric Le Goater 
245545d6befSCédric Le Goater static int aspeed_i2c_dma_read(AspeedI2CBus *bus, uint8_t *data)
246545d6befSCédric Le Goater {
247545d6befSCédric Le Goater     MemTxResult result;
248545d6befSCédric Le Goater     AspeedI2CState *s = bus->controller;
249545d6befSCédric Le Goater 
250545d6befSCédric Le Goater     result = address_space_read(&s->dram_as, bus->dma_addr,
251545d6befSCédric Le Goater                                 MEMTXATTRS_UNSPECIFIED, data, 1);
252545d6befSCédric Le Goater     if (result != MEMTX_OK) {
253545d6befSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "%s: DRAM read failed @%08x\n",
254545d6befSCédric Le Goater                       __func__, bus->dma_addr);
255545d6befSCédric Le Goater         return -1;
256545d6befSCédric Le Goater     }
257545d6befSCédric Le Goater 
258545d6befSCédric Le Goater     bus->dma_addr++;
259545d6befSCédric Le Goater     bus->dma_len--;
260545d6befSCédric Le Goater     return 0;
261545d6befSCédric Le Goater }
262545d6befSCédric Le Goater 
2636054fc73SCédric Le Goater static int aspeed_i2c_bus_send(AspeedI2CBus *bus, uint8_t pool_start)
2646054fc73SCédric Le Goater {
2656054fc73SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
2666054fc73SCédric Le Goater     int ret = -1;
2676054fc73SCédric Le Goater     int i;
2686054fc73SCédric Le Goater 
2696054fc73SCédric Le Goater     if (bus->cmd & I2CD_TX_BUFF_ENABLE) {
2706054fc73SCédric Le Goater         for (i = pool_start; i < I2CD_POOL_TX_COUNT(bus->pool_ctrl); i++) {
2716054fc73SCédric Le Goater             uint8_t *pool_base = aic->bus_pool_base(bus);
2726054fc73SCédric Le Goater 
27366cc84a1SCédric Le Goater             trace_aspeed_i2c_bus_send("BUF", i + 1,
27466cc84a1SCédric Le Goater                                       I2CD_POOL_TX_COUNT(bus->pool_ctrl),
27566cc84a1SCédric Le Goater                                       pool_base[i]);
2766054fc73SCédric Le Goater             ret = i2c_send(bus->bus, pool_base[i]);
2776054fc73SCédric Le Goater             if (ret) {
2786054fc73SCédric Le Goater                 break;
2796054fc73SCédric Le Goater             }
2806054fc73SCédric Le Goater         }
2816054fc73SCédric Le Goater         bus->cmd &= ~I2CD_TX_BUFF_ENABLE;
282545d6befSCédric Le Goater     } else if (bus->cmd & I2CD_TX_DMA_ENABLE) {
283545d6befSCédric Le Goater         while (bus->dma_len) {
284545d6befSCédric Le Goater             uint8_t data;
285545d6befSCédric Le Goater             aspeed_i2c_dma_read(bus, &data);
28666cc84a1SCédric Le Goater             trace_aspeed_i2c_bus_send("DMA", bus->dma_len, bus->dma_len, data);
287545d6befSCédric Le Goater             ret = i2c_send(bus->bus, data);
288545d6befSCédric Le Goater             if (ret) {
289545d6befSCédric Le Goater                 break;
290545d6befSCédric Le Goater             }
291545d6befSCédric Le Goater         }
292545d6befSCédric Le Goater         bus->cmd &= ~I2CD_TX_DMA_ENABLE;
2936054fc73SCédric Le Goater     } else {
29466cc84a1SCédric Le Goater         trace_aspeed_i2c_bus_send("BYTE", pool_start, 1, bus->buf);
2956054fc73SCédric Le Goater         ret = i2c_send(bus->bus, bus->buf);
2966054fc73SCédric Le Goater     }
2976054fc73SCédric Le Goater 
2986054fc73SCédric Le Goater     return ret;
2996054fc73SCédric Le Goater }
3006054fc73SCédric Le Goater 
3016054fc73SCédric Le Goater static void aspeed_i2c_bus_recv(AspeedI2CBus *bus)
3026054fc73SCédric Le Goater {
3036054fc73SCédric Le Goater     AspeedI2CState *s = bus->controller;
3046054fc73SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
3056054fc73SCédric Le Goater     uint8_t data;
3066054fc73SCédric Le Goater     int i;
3076054fc73SCédric Le Goater 
3086054fc73SCédric Le Goater     if (bus->cmd & I2CD_RX_BUFF_ENABLE) {
3096054fc73SCédric Le Goater         uint8_t *pool_base = aic->bus_pool_base(bus);
3106054fc73SCédric Le Goater 
3116054fc73SCédric Le Goater         for (i = 0; i < I2CD_POOL_RX_SIZE(bus->pool_ctrl); i++) {
3126054fc73SCédric Le Goater             pool_base[i] = i2c_recv(bus->bus);
31366cc84a1SCédric Le Goater             trace_aspeed_i2c_bus_recv("BUF", i + 1,
31466cc84a1SCédric Le Goater                                       I2CD_POOL_RX_SIZE(bus->pool_ctrl),
31566cc84a1SCédric Le Goater                                       pool_base[i]);
3166054fc73SCédric Le Goater         }
3176054fc73SCédric Le Goater 
3186054fc73SCédric Le Goater         /* Update RX count */
3196054fc73SCédric Le Goater         bus->pool_ctrl &= ~(0xff << 24);
3206054fc73SCédric Le Goater         bus->pool_ctrl |= (i & 0xff) << 24;
3216054fc73SCédric Le Goater         bus->cmd &= ~I2CD_RX_BUFF_ENABLE;
322545d6befSCédric Le Goater     } else if (bus->cmd & I2CD_RX_DMA_ENABLE) {
323545d6befSCédric Le Goater         uint8_t data;
324545d6befSCédric Le Goater 
325545d6befSCédric Le Goater         while (bus->dma_len) {
326545d6befSCédric Le Goater             MemTxResult result;
327545d6befSCédric Le Goater 
328545d6befSCédric Le Goater             data = i2c_recv(bus->bus);
32966cc84a1SCédric Le Goater             trace_aspeed_i2c_bus_recv("DMA", bus->dma_len, bus->dma_len, data);
330545d6befSCédric Le Goater             result = address_space_write(&s->dram_as, bus->dma_addr,
331545d6befSCédric Le Goater                                          MEMTXATTRS_UNSPECIFIED, &data, 1);
332545d6befSCédric Le Goater             if (result != MEMTX_OK) {
333545d6befSCédric Le Goater                 qemu_log_mask(LOG_GUEST_ERROR, "%s: DRAM write failed @%08x\n",
334545d6befSCédric Le Goater                               __func__, bus->dma_addr);
335545d6befSCédric Le Goater                 return;
336545d6befSCédric Le Goater             }
337545d6befSCédric Le Goater             bus->dma_addr++;
338545d6befSCédric Le Goater             bus->dma_len--;
339545d6befSCédric Le Goater         }
340545d6befSCédric Le Goater         bus->cmd &= ~I2CD_RX_DMA_ENABLE;
3416054fc73SCédric Le Goater     } else {
3426054fc73SCédric Le Goater         data = i2c_recv(bus->bus);
34366cc84a1SCédric Le Goater         trace_aspeed_i2c_bus_recv("BYTE", 1, 1, bus->buf);
3446054fc73SCédric Le Goater         bus->buf = (data & I2CD_BYTE_BUF_RX_MASK) << I2CD_BYTE_BUF_RX_SHIFT;
3456054fc73SCédric Le Goater     }
3466054fc73SCédric Le Goater }
3476054fc73SCédric Le Goater 
3487bd9c60dSGuenter Roeck static void aspeed_i2c_handle_rx_cmd(AspeedI2CBus *bus)
3497bd9c60dSGuenter Roeck {
3507bd9c60dSGuenter Roeck     aspeed_i2c_set_state(bus, I2CD_MRXD);
3516054fc73SCédric Le Goater     aspeed_i2c_bus_recv(bus);
3527bd9c60dSGuenter Roeck     bus->intr_status |= I2CD_INTR_RX_DONE;
3537bd9c60dSGuenter Roeck     if (bus->cmd & I2CD_M_S_RX_CMD_LAST) {
3547bd9c60dSGuenter Roeck         i2c_nack(bus->bus);
3557bd9c60dSGuenter Roeck     }
3567bd9c60dSGuenter Roeck     bus->cmd &= ~(I2CD_M_RX_CMD | I2CD_M_S_RX_CMD_LAST);
3577bd9c60dSGuenter Roeck     aspeed_i2c_set_state(bus, I2CD_MACTIVE);
3587bd9c60dSGuenter Roeck }
3597bd9c60dSGuenter Roeck 
3606054fc73SCédric Le Goater static uint8_t aspeed_i2c_get_addr(AspeedI2CBus *bus)
3616054fc73SCédric Le Goater {
3626054fc73SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
3636054fc73SCédric Le Goater 
3646054fc73SCédric Le Goater     if (bus->cmd & I2CD_TX_BUFF_ENABLE) {
3656054fc73SCédric Le Goater         uint8_t *pool_base = aic->bus_pool_base(bus);
3666054fc73SCédric Le Goater 
3676054fc73SCédric Le Goater         return pool_base[0];
368545d6befSCédric Le Goater     } else if (bus->cmd & I2CD_TX_DMA_ENABLE) {
369545d6befSCédric Le Goater         uint8_t data;
370545d6befSCédric Le Goater 
371545d6befSCédric Le Goater         aspeed_i2c_dma_read(bus, &data);
372545d6befSCédric Le Goater         return data;
3736054fc73SCédric Le Goater     } else {
3746054fc73SCédric Le Goater         return bus->buf;
3756054fc73SCédric Le Goater     }
3766054fc73SCédric Le Goater }
3776054fc73SCédric Le Goater 
378aab90b1cSCédric Le Goater static bool aspeed_i2c_check_sram(AspeedI2CBus *bus)
379aab90b1cSCédric Le Goater {
380aab90b1cSCédric Le Goater     AspeedI2CState *s = bus->controller;
381aab90b1cSCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
382aab90b1cSCédric Le Goater 
383aab90b1cSCédric Le Goater     if (!aic->check_sram) {
384aab90b1cSCédric Le Goater         return true;
385aab90b1cSCédric Le Goater     }
386aab90b1cSCédric Le Goater 
387aab90b1cSCédric Le Goater     /*
388aab90b1cSCédric Le Goater      * AST2500: SRAM must be enabled before using the Buffer Pool or
389aab90b1cSCédric Le Goater      * DMA mode.
390aab90b1cSCédric Le Goater      */
391aab90b1cSCédric Le Goater     if (!(s->ctrl_global & I2C_CTRL_SRAM_EN) &&
392aab90b1cSCédric Le Goater         (bus->cmd & (I2CD_RX_DMA_ENABLE | I2CD_TX_DMA_ENABLE |
393aab90b1cSCédric Le Goater                      I2CD_RX_BUFF_ENABLE | I2CD_TX_BUFF_ENABLE))) {
394aab90b1cSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "%s: SRAM is not enabled\n", __func__);
395aab90b1cSCédric Le Goater         return false;
396aab90b1cSCédric Le Goater     }
397aab90b1cSCédric Le Goater 
398aab90b1cSCédric Le Goater     return true;
399aab90b1cSCédric Le Goater }
400aab90b1cSCédric Le Goater 
40166cc84a1SCédric Le Goater static void aspeed_i2c_bus_cmd_dump(AspeedI2CBus *bus)
40266cc84a1SCédric Le Goater {
403f821bac4SMiroslav Rezanina     g_autofree char *cmd_flags = NULL;
40466cc84a1SCédric Le Goater     uint32_t count;
40566cc84a1SCédric Le Goater 
40666cc84a1SCédric Le Goater     if (bus->cmd & (I2CD_RX_BUFF_ENABLE | I2CD_RX_BUFF_ENABLE)) {
40766cc84a1SCédric Le Goater         count = I2CD_POOL_TX_COUNT(bus->pool_ctrl);
40866cc84a1SCédric Le Goater     } else if (bus->cmd & (I2CD_RX_DMA_ENABLE | I2CD_RX_DMA_ENABLE)) {
40966cc84a1SCédric Le Goater         count = bus->dma_len;
41066cc84a1SCédric Le Goater     } else { /* BYTE mode */
41166cc84a1SCédric Le Goater         count = 1;
41266cc84a1SCédric Le Goater     }
41366cc84a1SCédric Le Goater 
41466cc84a1SCédric Le Goater     cmd_flags = g_strdup_printf("%s%s%s%s%s%s%s%s%s",
41566cc84a1SCédric Le Goater                                 bus->cmd & I2CD_M_START_CMD ? "start|" : "",
41666cc84a1SCédric Le Goater                                 bus->cmd & I2CD_RX_DMA_ENABLE ? "rxdma|" : "",
41766cc84a1SCédric Le Goater                                 bus->cmd & I2CD_TX_DMA_ENABLE ? "txdma|" : "",
41866cc84a1SCédric Le Goater                                 bus->cmd & I2CD_RX_BUFF_ENABLE ? "rxbuf|" : "",
41966cc84a1SCédric Le Goater                                 bus->cmd & I2CD_TX_BUFF_ENABLE ? "txbuf|" : "",
42066cc84a1SCédric Le Goater                                 bus->cmd & I2CD_M_TX_CMD ? "tx|" : "",
42166cc84a1SCédric Le Goater                                 bus->cmd & I2CD_M_RX_CMD ? "rx|" : "",
42266cc84a1SCédric Le Goater                                 bus->cmd & I2CD_M_S_RX_CMD_LAST ? "last|" : "",
42366cc84a1SCédric Le Goater                                 bus->cmd & I2CD_M_STOP_CMD ? "stop" : "");
42466cc84a1SCédric Le Goater 
42566cc84a1SCédric Le Goater     trace_aspeed_i2c_bus_cmd(bus->cmd, cmd_flags, count, bus->intr_status);
42666cc84a1SCédric Le Goater }
42766cc84a1SCédric Le Goater 
4284960f084SCédric Le Goater /*
4294960f084SCédric Le Goater  * The state machine needs some refinement. It is only used to track
4304960f084SCédric Le Goater  * invalid STOP commands for the moment.
4314960f084SCédric Le Goater  */
43216020011SCédric Le Goater static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value)
43316020011SCédric Le Goater {
4346054fc73SCédric Le Goater     uint8_t pool_start = 0;
4356054fc73SCédric Le Goater 
436ddabca75SCédric Le Goater     bus->cmd &= ~0xFFFF;
43716020011SCédric Le Goater     bus->cmd |= value & 0xFFFF;
43816020011SCédric Le Goater 
439aab90b1cSCédric Le Goater     if (!aspeed_i2c_check_sram(bus)) {
440aab90b1cSCédric Le Goater         return;
441aab90b1cSCédric Le Goater     }
442aab90b1cSCédric Le Goater 
44366cc84a1SCédric Le Goater     if (trace_event_get_state_backends(TRACE_ASPEED_I2C_BUS_CMD)) {
44466cc84a1SCédric Le Goater         aspeed_i2c_bus_cmd_dump(bus);
44566cc84a1SCédric Le Goater     }
44666cc84a1SCédric Le Goater 
44716020011SCédric Le Goater     if (bus->cmd & I2CD_M_START_CMD) {
4484960f084SCédric Le Goater         uint8_t state = aspeed_i2c_get_state(bus) & I2CD_MACTIVE ?
4494960f084SCédric Le Goater             I2CD_MSTARTR : I2CD_MSTART;
4506054fc73SCédric Le Goater         uint8_t addr;
4514960f084SCédric Le Goater 
4524960f084SCédric Le Goater         aspeed_i2c_set_state(bus, state);
4534960f084SCédric Le Goater 
4546054fc73SCédric Le Goater         addr = aspeed_i2c_get_addr(bus);
4556054fc73SCédric Le Goater 
4566054fc73SCédric Le Goater         if (i2c_start_transfer(bus->bus, extract32(addr, 1, 7),
4576054fc73SCédric Le Goater                                extract32(addr, 0, 1))) {
45816020011SCédric Le Goater             bus->intr_status |= I2CD_INTR_TX_NAK;
45916020011SCédric Le Goater         } else {
46016020011SCédric Le Goater             bus->intr_status |= I2CD_INTR_TX_ACK;
46116020011SCédric Le Goater         }
46216020011SCédric Le Goater 
4636054fc73SCédric Le Goater         bus->cmd &= ~I2CD_M_START_CMD;
4646054fc73SCédric Le Goater 
4656054fc73SCédric Le Goater         /*
4666054fc73SCédric Le Goater          * The START command is also a TX command, as the slave
4676054fc73SCédric Le Goater          * address is sent on the bus. Drop the TX flag if nothing
4686054fc73SCédric Le Goater          * else needs to be sent in this sequence.
4696054fc73SCédric Le Goater          */
4706054fc73SCédric Le Goater         if (bus->cmd & I2CD_TX_BUFF_ENABLE) {
4716054fc73SCédric Le Goater             if (I2CD_POOL_TX_COUNT(bus->pool_ctrl) == 1) {
4726054fc73SCédric Le Goater                 bus->cmd &= ~I2CD_M_TX_CMD;
4736054fc73SCédric Le Goater             } else {
4746054fc73SCédric Le Goater                 /*
4756054fc73SCédric Le Goater                  * Increase the start index in the TX pool buffer to
4766054fc73SCédric Le Goater                  * skip the address byte.
4776054fc73SCédric Le Goater                  */
4786054fc73SCédric Le Goater                 pool_start++;
4796054fc73SCédric Le Goater             }
480545d6befSCédric Le Goater         } else if (bus->cmd & I2CD_TX_DMA_ENABLE) {
481545d6befSCédric Le Goater             if (bus->dma_len == 0) {
482545d6befSCédric Le Goater                 bus->cmd &= ~I2CD_M_TX_CMD;
483545d6befSCédric Le Goater             }
4846054fc73SCédric Le Goater         } else {
4856054fc73SCédric Le Goater             bus->cmd &= ~I2CD_M_TX_CMD;
4866054fc73SCédric Le Goater         }
487ddabca75SCédric Le Goater 
488ddabca75SCédric Le Goater         /* No slave found */
489ddabca75SCédric Le Goater         if (!i2c_bus_busy(bus->bus)) {
490ddabca75SCédric Le Goater             return;
491ddabca75SCédric Le Goater         }
4924960f084SCédric Le Goater         aspeed_i2c_set_state(bus, I2CD_MACTIVE);
493ddabca75SCédric Le Goater     }
494ddabca75SCédric Le Goater 
495ddabca75SCédric Le Goater     if (bus->cmd & I2CD_M_TX_CMD) {
4964960f084SCédric Le Goater         aspeed_i2c_set_state(bus, I2CD_MTXD);
4976054fc73SCédric Le Goater         if (aspeed_i2c_bus_send(bus, pool_start)) {
4984960f084SCédric Le Goater             bus->intr_status |= (I2CD_INTR_TX_NAK);
49916020011SCédric Le Goater             i2c_end_transfer(bus->bus);
50016020011SCédric Le Goater         } else {
50116020011SCédric Le Goater             bus->intr_status |= I2CD_INTR_TX_ACK;
50216020011SCédric Le Goater         }
503ddabca75SCédric Le Goater         bus->cmd &= ~I2CD_M_TX_CMD;
5044960f084SCédric Le Goater         aspeed_i2c_set_state(bus, I2CD_MACTIVE);
505ddabca75SCédric Le Goater     }
50616020011SCédric Le Goater 
507bb626e5bSGuenter Roeck     if ((bus->cmd & (I2CD_M_RX_CMD | I2CD_M_S_RX_CMD_LAST)) &&
508bb626e5bSGuenter Roeck         !(bus->intr_status & I2CD_INTR_RX_DONE)) {
5097bd9c60dSGuenter Roeck         aspeed_i2c_handle_rx_cmd(bus);
51016020011SCédric Le Goater     }
51116020011SCédric Le Goater 
512d0efdc16SCédric Le Goater     if (bus->cmd & I2CD_M_STOP_CMD) {
5134960f084SCédric Le Goater         if (!(aspeed_i2c_get_state(bus) & I2CD_MACTIVE)) {
5144960f084SCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "%s: abnormal stop\n", __func__);
51516020011SCédric Le Goater             bus->intr_status |= I2CD_INTR_ABNORMAL;
51616020011SCédric Le Goater         } else {
5174960f084SCédric Le Goater             aspeed_i2c_set_state(bus, I2CD_MSTOP);
51816020011SCédric Le Goater             i2c_end_transfer(bus->bus);
51916020011SCédric Le Goater             bus->intr_status |= I2CD_INTR_NORMAL_STOP;
52016020011SCédric Le Goater         }
521ddabca75SCédric Le Goater         bus->cmd &= ~I2CD_M_STOP_CMD;
5224960f084SCédric Le Goater         aspeed_i2c_set_state(bus, I2CD_IDLE);
52316020011SCédric Le Goater     }
52416020011SCédric Le Goater }
52516020011SCédric Le Goater 
52616020011SCédric Le Goater static void aspeed_i2c_bus_write(void *opaque, hwaddr offset,
52716020011SCédric Le Goater                                  uint64_t value, unsigned size)
52816020011SCédric Le Goater {
52916020011SCédric Le Goater     AspeedI2CBus *bus = opaque;
53051dd4923SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
531bb626e5bSGuenter Roeck     bool handle_rx;
53216020011SCédric Le Goater 
53366cc84a1SCédric Le Goater     trace_aspeed_i2c_bus_write(bus->id, offset, size, value);
53466cc84a1SCédric Le Goater 
53516020011SCédric Le Goater     switch (offset) {
53616020011SCédric Le Goater     case I2CD_FUN_CTRL_REG:
53716020011SCédric Le Goater         if (value & I2CD_SLAVE_EN) {
53816020011SCédric Le Goater             qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n",
53916020011SCédric Le Goater                           __func__);
54016020011SCédric Le Goater             break;
54116020011SCédric Le Goater         }
54216020011SCédric Le Goater         bus->ctrl = value & 0x0071C3FF;
54316020011SCédric Le Goater         break;
54416020011SCédric Le Goater     case I2CD_AC_TIMING_REG1:
54516020011SCédric Le Goater         bus->timing[0] = value & 0xFFFFF0F;
54616020011SCédric Le Goater         break;
54716020011SCédric Le Goater     case I2CD_AC_TIMING_REG2:
54816020011SCédric Le Goater         bus->timing[1] = value & 0x7;
54916020011SCédric Le Goater         break;
55016020011SCédric Le Goater     case I2CD_INTR_CTRL_REG:
55116020011SCédric Le Goater         bus->intr_ctrl = value & 0x7FFF;
55216020011SCédric Le Goater         break;
55316020011SCédric Le Goater     case I2CD_INTR_STS_REG:
554bb626e5bSGuenter Roeck         handle_rx = (bus->intr_status & I2CD_INTR_RX_DONE) &&
555bb626e5bSGuenter Roeck                 (value & I2CD_INTR_RX_DONE);
55616020011SCédric Le Goater         bus->intr_status &= ~(value & 0x7FFF);
5575540cb97SCédric Le Goater         if (!bus->intr_status) {
55816020011SCédric Le Goater             bus->controller->intr_status &= ~(1 << bus->id);
55951dd4923SCédric Le Goater             qemu_irq_lower(aic->bus_get_irq(bus));
5605540cb97SCédric Le Goater         }
561bb626e5bSGuenter Roeck         if (handle_rx && (bus->cmd & (I2CD_M_RX_CMD | I2CD_M_S_RX_CMD_LAST))) {
562bb626e5bSGuenter Roeck             aspeed_i2c_handle_rx_cmd(bus);
563bb626e5bSGuenter Roeck             aspeed_i2c_bus_raise_interrupt(bus);
564bb626e5bSGuenter Roeck         }
56516020011SCédric Le Goater         break;
56616020011SCédric Le Goater     case I2CD_DEV_ADDR_REG:
56716020011SCédric Le Goater         qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n",
56816020011SCédric Le Goater                       __func__);
56916020011SCédric Le Goater         break;
5706054fc73SCédric Le Goater     case I2CD_POOL_CTRL_REG:
5716054fc73SCédric Le Goater         bus->pool_ctrl &= ~0xffffff;
5726054fc73SCédric Le Goater         bus->pool_ctrl |= (value & 0xffffff);
5736054fc73SCédric Le Goater         break;
5746054fc73SCédric Le Goater 
57516020011SCédric Le Goater     case I2CD_BYTE_BUF_REG:
57616020011SCédric Le Goater         bus->buf = (value & I2CD_BYTE_BUF_TX_MASK) << I2CD_BYTE_BUF_TX_SHIFT;
57716020011SCédric Le Goater         break;
57816020011SCédric Le Goater     case I2CD_CMD_REG:
57916020011SCédric Le Goater         if (!aspeed_i2c_bus_is_enabled(bus)) {
58016020011SCédric Le Goater             break;
58116020011SCédric Le Goater         }
58216020011SCédric Le Goater 
58316020011SCédric Le Goater         if (!aspeed_i2c_bus_is_master(bus)) {
58416020011SCédric Le Goater             qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n",
58516020011SCédric Le Goater                           __func__);
58616020011SCédric Le Goater             break;
58716020011SCédric Le Goater         }
58816020011SCédric Le Goater 
589545d6befSCédric Le Goater         if (!aic->has_dma &&
590545d6befSCédric Le Goater             value & (I2CD_RX_DMA_ENABLE | I2CD_TX_DMA_ENABLE)) {
591545d6befSCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n",  __func__);
592545d6befSCédric Le Goater             break;
593545d6befSCédric Le Goater         }
594545d6befSCédric Le Goater 
59516020011SCédric Le Goater         aspeed_i2c_bus_handle_cmd(bus, value);
596ddabca75SCédric Le Goater         aspeed_i2c_bus_raise_interrupt(bus);
59716020011SCédric Le Goater         break;
598545d6befSCédric Le Goater     case I2CD_DMA_ADDR:
599545d6befSCédric Le Goater         if (!aic->has_dma) {
600545d6befSCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n",  __func__);
601545d6befSCédric Le Goater             break;
602545d6befSCédric Le Goater         }
603545d6befSCédric Le Goater 
60474925159SCédric Le Goater         bus->dma_addr = value & 0x3ffffffc;
605545d6befSCédric Le Goater         break;
606545d6befSCédric Le Goater 
607545d6befSCédric Le Goater     case I2CD_DMA_LEN:
608545d6befSCédric Le Goater         if (!aic->has_dma) {
609545d6befSCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n",  __func__);
610545d6befSCédric Le Goater             break;
611545d6befSCédric Le Goater         }
612545d6befSCédric Le Goater 
613545d6befSCédric Le Goater         bus->dma_len = value & 0xfff;
614545d6befSCédric Le Goater         if (!bus->dma_len) {
615545d6befSCédric Le Goater             qemu_log_mask(LOG_UNIMP, "%s: invalid DMA length\n",  __func__);
616545d6befSCédric Le Goater         }
617545d6befSCédric Le Goater         break;
61816020011SCédric Le Goater 
61916020011SCédric Le Goater     default:
62016020011SCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
62116020011SCédric Le Goater                       __func__, offset);
62216020011SCédric Le Goater     }
62316020011SCédric Le Goater }
62416020011SCédric Le Goater 
62516020011SCédric Le Goater static uint64_t aspeed_i2c_ctrl_read(void *opaque, hwaddr offset,
62616020011SCédric Le Goater                                    unsigned size)
62716020011SCédric Le Goater {
62816020011SCédric Le Goater     AspeedI2CState *s = opaque;
62916020011SCédric Le Goater 
63016020011SCédric Le Goater     switch (offset) {
63116020011SCédric Le Goater     case I2C_CTRL_STATUS:
63216020011SCédric Le Goater         return s->intr_status;
633aab90b1cSCédric Le Goater     case I2C_CTRL_GLOBAL:
634aab90b1cSCédric Le Goater         return s->ctrl_global;
63516020011SCédric Le Goater     default:
63616020011SCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
63716020011SCédric Le Goater                       __func__, offset);
63816020011SCédric Le Goater         break;
63916020011SCédric Le Goater     }
64016020011SCédric Le Goater 
64116020011SCédric Le Goater     return -1;
64216020011SCédric Le Goater }
64316020011SCédric Le Goater 
64416020011SCédric Le Goater static void aspeed_i2c_ctrl_write(void *opaque, hwaddr offset,
64516020011SCédric Le Goater                                   uint64_t value, unsigned size)
64616020011SCédric Le Goater {
647aab90b1cSCédric Le Goater     AspeedI2CState *s = opaque;
648aab90b1cSCédric Le Goater 
64916020011SCédric Le Goater     switch (offset) {
650aab90b1cSCédric Le Goater     case I2C_CTRL_GLOBAL:
651aab90b1cSCédric Le Goater         s->ctrl_global = value;
652aab90b1cSCédric Le Goater         break;
65316020011SCédric Le Goater     case I2C_CTRL_STATUS:
65416020011SCédric Le Goater     default:
65516020011SCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
65616020011SCédric Le Goater                       __func__, offset);
65716020011SCédric Le Goater         break;
65816020011SCédric Le Goater     }
65916020011SCédric Le Goater }
66016020011SCédric Le Goater 
66116020011SCédric Le Goater static const MemoryRegionOps aspeed_i2c_bus_ops = {
66216020011SCédric Le Goater     .read = aspeed_i2c_bus_read,
66316020011SCédric Le Goater     .write = aspeed_i2c_bus_write,
66416020011SCédric Le Goater     .endianness = DEVICE_LITTLE_ENDIAN,
66516020011SCédric Le Goater };
66616020011SCédric Le Goater 
66716020011SCédric Le Goater static const MemoryRegionOps aspeed_i2c_ctrl_ops = {
66816020011SCédric Le Goater     .read = aspeed_i2c_ctrl_read,
66916020011SCédric Le Goater     .write = aspeed_i2c_ctrl_write,
67016020011SCédric Le Goater     .endianness = DEVICE_LITTLE_ENDIAN,
67116020011SCédric Le Goater };
67216020011SCédric Le Goater 
6736054fc73SCédric Le Goater static uint64_t aspeed_i2c_pool_read(void *opaque, hwaddr offset,
6746054fc73SCédric Le Goater                                      unsigned size)
6756054fc73SCédric Le Goater {
6766054fc73SCédric Le Goater     AspeedI2CState *s = opaque;
6776054fc73SCédric Le Goater     uint64_t ret = 0;
6786054fc73SCédric Le Goater     int i;
6796054fc73SCédric Le Goater 
6806054fc73SCédric Le Goater     for (i = 0; i < size; i++) {
6816054fc73SCédric Le Goater         ret |= (uint64_t) s->pool[offset + i] << (8 * i);
6826054fc73SCédric Le Goater     }
6836054fc73SCédric Le Goater 
6846054fc73SCédric Le Goater     return ret;
6856054fc73SCédric Le Goater }
6866054fc73SCédric Le Goater 
6876054fc73SCédric Le Goater static void aspeed_i2c_pool_write(void *opaque, hwaddr offset,
6886054fc73SCédric Le Goater                                   uint64_t value, unsigned size)
6896054fc73SCédric Le Goater {
6906054fc73SCédric Le Goater     AspeedI2CState *s = opaque;
6916054fc73SCédric Le Goater     int i;
6926054fc73SCédric Le Goater 
6936054fc73SCédric Le Goater     for (i = 0; i < size; i++) {
6946054fc73SCédric Le Goater         s->pool[offset + i] = (value >> (8 * i)) & 0xFF;
6956054fc73SCédric Le Goater     }
6966054fc73SCédric Le Goater }
6976054fc73SCédric Le Goater 
6986054fc73SCédric Le Goater static const MemoryRegionOps aspeed_i2c_pool_ops = {
6996054fc73SCédric Le Goater     .read = aspeed_i2c_pool_read,
7006054fc73SCédric Le Goater     .write = aspeed_i2c_pool_write,
7016054fc73SCédric Le Goater     .endianness = DEVICE_LITTLE_ENDIAN,
7026054fc73SCédric Le Goater     .valid = {
7036054fc73SCédric Le Goater         .min_access_size = 1,
7046054fc73SCédric Le Goater         .max_access_size = 4,
7056054fc73SCédric Le Goater     },
7066054fc73SCédric Le Goater };
7076054fc73SCédric Le Goater 
70816020011SCédric Le Goater static const VMStateDescription aspeed_i2c_bus_vmstate = {
70916020011SCédric Le Goater     .name = TYPE_ASPEED_I2C,
710545d6befSCédric Le Goater     .version_id = 3,
711545d6befSCédric Le Goater     .minimum_version_id = 3,
71216020011SCédric Le Goater     .fields = (VMStateField[]) {
71316020011SCédric Le Goater         VMSTATE_UINT8(id, AspeedI2CBus),
71416020011SCédric Le Goater         VMSTATE_UINT32(ctrl, AspeedI2CBus),
71516020011SCédric Le Goater         VMSTATE_UINT32_ARRAY(timing, AspeedI2CBus, 2),
71616020011SCédric Le Goater         VMSTATE_UINT32(intr_ctrl, AspeedI2CBus),
71716020011SCédric Le Goater         VMSTATE_UINT32(intr_status, AspeedI2CBus),
71816020011SCédric Le Goater         VMSTATE_UINT32(cmd, AspeedI2CBus),
71916020011SCédric Le Goater         VMSTATE_UINT32(buf, AspeedI2CBus),
7206054fc73SCédric Le Goater         VMSTATE_UINT32(pool_ctrl, AspeedI2CBus),
721545d6befSCédric Le Goater         VMSTATE_UINT32(dma_addr, AspeedI2CBus),
722545d6befSCédric Le Goater         VMSTATE_UINT32(dma_len, AspeedI2CBus),
72316020011SCédric Le Goater         VMSTATE_END_OF_LIST()
72416020011SCédric Le Goater     }
72516020011SCédric Le Goater };
72616020011SCédric Le Goater 
72716020011SCédric Le Goater static const VMStateDescription aspeed_i2c_vmstate = {
72816020011SCédric Le Goater     .name = TYPE_ASPEED_I2C,
7296054fc73SCédric Le Goater     .version_id = 2,
7306054fc73SCédric Le Goater     .minimum_version_id = 2,
73116020011SCédric Le Goater     .fields = (VMStateField[]) {
73216020011SCédric Le Goater         VMSTATE_UINT32(intr_status, AspeedI2CState),
73316020011SCédric Le Goater         VMSTATE_STRUCT_ARRAY(busses, AspeedI2CState,
73416020011SCédric Le Goater                              ASPEED_I2C_NR_BUSSES, 1, aspeed_i2c_bus_vmstate,
73516020011SCédric Le Goater                              AspeedI2CBus),
7366054fc73SCédric Le Goater         VMSTATE_UINT8_ARRAY(pool, AspeedI2CState, ASPEED_I2C_MAX_POOL_SIZE),
73716020011SCédric Le Goater         VMSTATE_END_OF_LIST()
73816020011SCédric Le Goater     }
73916020011SCédric Le Goater };
74016020011SCédric Le Goater 
74116020011SCédric Le Goater static void aspeed_i2c_reset(DeviceState *dev)
74216020011SCédric Le Goater {
74316020011SCédric Le Goater     int i;
74416020011SCédric Le Goater     AspeedI2CState *s = ASPEED_I2C(dev);
745f7da1aa8SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
74616020011SCédric Le Goater 
74716020011SCédric Le Goater     s->intr_status = 0;
74816020011SCédric Le Goater 
749f7da1aa8SCédric Le Goater     for (i = 0; i < aic->num_busses; i++) {
75016020011SCédric Le Goater         s->busses[i].intr_ctrl = 0;
75116020011SCédric Le Goater         s->busses[i].intr_status = 0;
75216020011SCédric Le Goater         s->busses[i].cmd = 0;
75316020011SCédric Le Goater         s->busses[i].buf = 0;
754545d6befSCédric Le Goater         s->busses[i].dma_addr = 0;
755545d6befSCédric Le Goater         s->busses[i].dma_len = 0;
75616020011SCédric Le Goater         i2c_end_transfer(s->busses[i].bus);
75716020011SCédric Le Goater     }
75816020011SCédric Le Goater }
75916020011SCédric Le Goater 
76016020011SCédric Le Goater /*
761f7da1aa8SCédric Le Goater  * Address Definitions (AST2400 and AST2500)
76216020011SCédric Le Goater  *
76316020011SCédric Le Goater  *   0x000 ... 0x03F: Global Register
76416020011SCédric Le Goater  *   0x040 ... 0x07F: Device 1
76516020011SCédric Le Goater  *   0x080 ... 0x0BF: Device 2
76616020011SCédric Le Goater  *   0x0C0 ... 0x0FF: Device 3
76716020011SCédric Le Goater  *   0x100 ... 0x13F: Device 4
76816020011SCédric Le Goater  *   0x140 ... 0x17F: Device 5
76916020011SCédric Le Goater  *   0x180 ... 0x1BF: Device 6
77016020011SCédric Le Goater  *   0x1C0 ... 0x1FF: Device 7
77116020011SCédric Le Goater  *   0x200 ... 0x2FF: Buffer Pool  (unused in linux driver)
77216020011SCédric Le Goater  *   0x300 ... 0x33F: Device 8
77316020011SCédric Le Goater  *   0x340 ... 0x37F: Device 9
77416020011SCédric Le Goater  *   0x380 ... 0x3BF: Device 10
77516020011SCédric Le Goater  *   0x3C0 ... 0x3FF: Device 11
77616020011SCédric Le Goater  *   0x400 ... 0x43F: Device 12
77716020011SCédric Le Goater  *   0x440 ... 0x47F: Device 13
77816020011SCédric Le Goater  *   0x480 ... 0x4BF: Device 14
77916020011SCédric Le Goater  *   0x800 ... 0xFFF: Buffer Pool  (unused in linux driver)
78016020011SCédric Le Goater  */
78116020011SCédric Le Goater static void aspeed_i2c_realize(DeviceState *dev, Error **errp)
78216020011SCédric Le Goater {
78316020011SCédric Le Goater     int i;
78416020011SCédric Le Goater     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
78516020011SCédric Le Goater     AspeedI2CState *s = ASPEED_I2C(dev);
786f7da1aa8SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
78716020011SCédric Le Goater 
78816020011SCédric Le Goater     sysbus_init_irq(sbd, &s->irq);
78916020011SCédric Le Goater     memory_region_init_io(&s->iomem, OBJECT(s), &aspeed_i2c_ctrl_ops, s,
79016020011SCédric Le Goater                           "aspeed.i2c", 0x1000);
79116020011SCédric Le Goater     sysbus_init_mmio(sbd, &s->iomem);
79216020011SCédric Le Goater 
793f7da1aa8SCédric Le Goater     for (i = 0; i < aic->num_busses; i++) {
794f7da1aa8SCédric Le Goater         char name[32];
795f7da1aa8SCédric Le Goater         int offset = i < aic->gap ? 1 : 5;
79651dd4923SCédric Le Goater 
79751dd4923SCédric Le Goater         sysbus_init_irq(sbd, &s->busses[i].irq);
79816020011SCédric Le Goater         snprintf(name, sizeof(name), "aspeed.i2c.%d", i);
79916020011SCédric Le Goater         s->busses[i].controller = s;
80016020011SCédric Le Goater         s->busses[i].id = i;
80116020011SCédric Le Goater         s->busses[i].bus = i2c_init_bus(dev, name);
80216020011SCédric Le Goater         memory_region_init_io(&s->busses[i].mr, OBJECT(dev),
803f7da1aa8SCédric Le Goater                               &aspeed_i2c_bus_ops, &s->busses[i], name,
804f7da1aa8SCédric Le Goater                               aic->reg_size);
805f7da1aa8SCédric Le Goater         memory_region_add_subregion(&s->iomem, aic->reg_size * (i + offset),
80616020011SCédric Le Goater                                     &s->busses[i].mr);
80716020011SCédric Le Goater     }
8086054fc73SCédric Le Goater 
8096054fc73SCédric Le Goater     memory_region_init_io(&s->pool_iomem, OBJECT(s), &aspeed_i2c_pool_ops, s,
8106054fc73SCédric Le Goater                           "aspeed.i2c-pool", aic->pool_size);
8116054fc73SCédric Le Goater     memory_region_add_subregion(&s->iomem, aic->pool_base, &s->pool_iomem);
812545d6befSCédric Le Goater 
813545d6befSCédric Le Goater     if (aic->has_dma) {
814545d6befSCédric Le Goater         if (!s->dram_mr) {
815545d6befSCédric Le Goater             error_setg(errp, TYPE_ASPEED_I2C ": 'dram' link not set");
816545d6befSCédric Le Goater             return;
81716020011SCédric Le Goater         }
81816020011SCédric Le Goater 
819*3f7a53b2SCédric Le Goater         address_space_init(&s->dram_as, s->dram_mr,
820*3f7a53b2SCédric Le Goater                            TYPE_ASPEED_I2C "-dma-dram");
821545d6befSCédric Le Goater     }
822545d6befSCédric Le Goater }
823545d6befSCédric Le Goater 
824545d6befSCédric Le Goater static Property aspeed_i2c_properties[] = {
825545d6befSCédric Le Goater     DEFINE_PROP_LINK("dram", AspeedI2CState, dram_mr,
826545d6befSCédric Le Goater                      TYPE_MEMORY_REGION, MemoryRegion *),
827545d6befSCédric Le Goater     DEFINE_PROP_END_OF_LIST(),
828545d6befSCédric Le Goater };
829545d6befSCédric Le Goater 
83016020011SCédric Le Goater static void aspeed_i2c_class_init(ObjectClass *klass, void *data)
83116020011SCédric Le Goater {
83216020011SCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
83316020011SCédric Le Goater 
83416020011SCédric Le Goater     dc->vmsd = &aspeed_i2c_vmstate;
83516020011SCédric Le Goater     dc->reset = aspeed_i2c_reset;
8364f67d30bSMarc-André Lureau     device_class_set_props(dc, aspeed_i2c_properties);
83716020011SCédric Le Goater     dc->realize = aspeed_i2c_realize;
83816020011SCédric Le Goater     dc->desc = "Aspeed I2C Controller";
83916020011SCédric Le Goater }
84016020011SCédric Le Goater 
84116020011SCédric Le Goater static const TypeInfo aspeed_i2c_info = {
84216020011SCédric Le Goater     .name          = TYPE_ASPEED_I2C,
84316020011SCédric Le Goater     .parent        = TYPE_SYS_BUS_DEVICE,
84416020011SCédric Le Goater     .instance_size = sizeof(AspeedI2CState),
84516020011SCédric Le Goater     .class_init    = aspeed_i2c_class_init,
846f7da1aa8SCédric Le Goater     .class_size = sizeof(AspeedI2CClass),
847f7da1aa8SCédric Le Goater     .abstract   = true,
848f7da1aa8SCédric Le Goater };
849f7da1aa8SCédric Le Goater 
85051dd4923SCédric Le Goater static qemu_irq aspeed_2400_i2c_bus_get_irq(AspeedI2CBus *bus)
85151dd4923SCédric Le Goater {
85251dd4923SCédric Le Goater     return bus->controller->irq;
85351dd4923SCédric Le Goater }
85451dd4923SCédric Le Goater 
8556054fc73SCédric Le Goater static uint8_t *aspeed_2400_i2c_bus_pool_base(AspeedI2CBus *bus)
8566054fc73SCédric Le Goater {
8576054fc73SCédric Le Goater     uint8_t *pool_page =
8586054fc73SCédric Le Goater         &bus->controller->pool[I2CD_POOL_PAGE_SEL(bus->ctrl) * 0x100];
8596054fc73SCédric Le Goater 
8606054fc73SCédric Le Goater     return &pool_page[I2CD_POOL_OFFSET(bus->pool_ctrl)];
8616054fc73SCédric Le Goater }
8626054fc73SCédric Le Goater 
863f7da1aa8SCédric Le Goater static void aspeed_2400_i2c_class_init(ObjectClass *klass, void *data)
864f7da1aa8SCédric Le Goater {
865f7da1aa8SCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
866f7da1aa8SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_CLASS(klass);
867f7da1aa8SCédric Le Goater 
868f7da1aa8SCédric Le Goater     dc->desc = "ASPEED 2400 I2C Controller";
869f7da1aa8SCédric Le Goater 
870f7da1aa8SCédric Le Goater     aic->num_busses = 14;
871f7da1aa8SCédric Le Goater     aic->reg_size = 0x40;
872f7da1aa8SCédric Le Goater     aic->gap = 7;
87351dd4923SCédric Le Goater     aic->bus_get_irq = aspeed_2400_i2c_bus_get_irq;
8746054fc73SCédric Le Goater     aic->pool_size = 0x800;
8756054fc73SCédric Le Goater     aic->pool_base = 0x800;
8766054fc73SCédric Le Goater     aic->bus_pool_base = aspeed_2400_i2c_bus_pool_base;
877f7da1aa8SCédric Le Goater }
878f7da1aa8SCédric Le Goater 
879f7da1aa8SCédric Le Goater static const TypeInfo aspeed_2400_i2c_info = {
880f7da1aa8SCédric Le Goater     .name = TYPE_ASPEED_2400_I2C,
881f7da1aa8SCédric Le Goater     .parent = TYPE_ASPEED_I2C,
882f7da1aa8SCédric Le Goater     .class_init = aspeed_2400_i2c_class_init,
883f7da1aa8SCédric Le Goater };
884f7da1aa8SCédric Le Goater 
88551dd4923SCédric Le Goater static qemu_irq aspeed_2500_i2c_bus_get_irq(AspeedI2CBus *bus)
88651dd4923SCédric Le Goater {
88751dd4923SCédric Le Goater     return bus->controller->irq;
88851dd4923SCédric Le Goater }
88951dd4923SCédric Le Goater 
8906054fc73SCédric Le Goater static uint8_t *aspeed_2500_i2c_bus_pool_base(AspeedI2CBus *bus)
8916054fc73SCédric Le Goater {
8926054fc73SCédric Le Goater     return &bus->controller->pool[bus->id * 0x10];
8936054fc73SCédric Le Goater }
8946054fc73SCédric Le Goater 
895f7da1aa8SCédric Le Goater static void aspeed_2500_i2c_class_init(ObjectClass *klass, void *data)
896f7da1aa8SCédric Le Goater {
897f7da1aa8SCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
898f7da1aa8SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_CLASS(klass);
899f7da1aa8SCédric Le Goater 
900f7da1aa8SCédric Le Goater     dc->desc = "ASPEED 2500 I2C Controller";
901f7da1aa8SCédric Le Goater 
902f7da1aa8SCédric Le Goater     aic->num_busses = 14;
903f7da1aa8SCédric Le Goater     aic->reg_size = 0x40;
904f7da1aa8SCédric Le Goater     aic->gap = 7;
90551dd4923SCédric Le Goater     aic->bus_get_irq = aspeed_2500_i2c_bus_get_irq;
9066054fc73SCédric Le Goater     aic->pool_size = 0x100;
9076054fc73SCédric Le Goater     aic->pool_base = 0x200;
9086054fc73SCédric Le Goater     aic->bus_pool_base = aspeed_2500_i2c_bus_pool_base;
909aab90b1cSCédric Le Goater     aic->check_sram = true;
910545d6befSCédric Le Goater     aic->has_dma = true;
911f7da1aa8SCédric Le Goater }
912f7da1aa8SCédric Le Goater 
913f7da1aa8SCédric Le Goater static const TypeInfo aspeed_2500_i2c_info = {
914f7da1aa8SCédric Le Goater     .name = TYPE_ASPEED_2500_I2C,
915f7da1aa8SCédric Le Goater     .parent = TYPE_ASPEED_I2C,
916f7da1aa8SCédric Le Goater     .class_init = aspeed_2500_i2c_class_init,
91716020011SCédric Le Goater };
91816020011SCédric Le Goater 
91951dd4923SCédric Le Goater static qemu_irq aspeed_2600_i2c_bus_get_irq(AspeedI2CBus *bus)
92051dd4923SCédric Le Goater {
92151dd4923SCédric Le Goater     return bus->irq;
92251dd4923SCédric Le Goater }
92351dd4923SCédric Le Goater 
9246054fc73SCédric Le Goater static uint8_t *aspeed_2600_i2c_bus_pool_base(AspeedI2CBus *bus)
9256054fc73SCédric Le Goater {
9266054fc73SCédric Le Goater    return &bus->controller->pool[bus->id * 0x20];
9276054fc73SCédric Le Goater }
9286054fc73SCédric Le Goater 
92951dd4923SCédric Le Goater static void aspeed_2600_i2c_class_init(ObjectClass *klass, void *data)
93051dd4923SCédric Le Goater {
93151dd4923SCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
93251dd4923SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_CLASS(klass);
93351dd4923SCédric Le Goater 
93451dd4923SCédric Le Goater     dc->desc = "ASPEED 2600 I2C Controller";
93551dd4923SCédric Le Goater 
93651dd4923SCédric Le Goater     aic->num_busses = 16;
93751dd4923SCédric Le Goater     aic->reg_size = 0x80;
93851dd4923SCédric Le Goater     aic->gap = -1; /* no gap */
93951dd4923SCédric Le Goater     aic->bus_get_irq = aspeed_2600_i2c_bus_get_irq;
9406054fc73SCédric Le Goater     aic->pool_size = 0x200;
9416054fc73SCédric Le Goater     aic->pool_base = 0xC00;
9426054fc73SCédric Le Goater     aic->bus_pool_base = aspeed_2600_i2c_bus_pool_base;
943545d6befSCédric Le Goater     aic->has_dma = true;
94451dd4923SCédric Le Goater }
94551dd4923SCédric Le Goater 
94651dd4923SCédric Le Goater static const TypeInfo aspeed_2600_i2c_info = {
94751dd4923SCédric Le Goater     .name = TYPE_ASPEED_2600_I2C,
94851dd4923SCédric Le Goater     .parent = TYPE_ASPEED_I2C,
94951dd4923SCédric Le Goater     .class_init = aspeed_2600_i2c_class_init,
95051dd4923SCédric Le Goater };
95151dd4923SCédric Le Goater 
95216020011SCédric Le Goater static void aspeed_i2c_register_types(void)
95316020011SCédric Le Goater {
95416020011SCédric Le Goater     type_register_static(&aspeed_i2c_info);
955f7da1aa8SCédric Le Goater     type_register_static(&aspeed_2400_i2c_info);
956f7da1aa8SCédric Le Goater     type_register_static(&aspeed_2500_i2c_info);
95751dd4923SCédric Le Goater     type_register_static(&aspeed_2600_i2c_info);
95816020011SCédric Le Goater }
95916020011SCédric Le Goater 
96016020011SCédric Le Goater type_init(aspeed_i2c_register_types)
96116020011SCédric Le Goater 
96216020011SCédric Le Goater 
9637a204cbdSPhilippe Mathieu-Daudé I2CBus *aspeed_i2c_get_bus(AspeedI2CState *s, int busnr)
96416020011SCédric Le Goater {
965f7da1aa8SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
96616020011SCédric Le Goater     I2CBus *bus = NULL;
96716020011SCédric Le Goater 
968f7da1aa8SCédric Le Goater     if (busnr >= 0 && busnr < aic->num_busses) {
96916020011SCédric Le Goater         bus = s->busses[busnr].bus;
97016020011SCédric Le Goater     }
97116020011SCédric Le Goater 
97216020011SCédric Le Goater     return bus;
97316020011SCédric Le Goater }
974