xref: /qemu/hw/i2c/aspeed_i2c.c (revision 545d6bef7097129040bddc86fe09326ee0a14aae)
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"
26*545d6befSCédric Le Goater #include "qemu/error-report.h"
27*545d6befSCédric Le Goater #include "qapi/error.h"
2816020011SCédric Le Goater #include "hw/i2c/aspeed_i2c.h"
2964552b6bSMarkus Armbruster #include "hw/irq.h"
30*545d6befSCédric Le Goater #include "hw/qdev-properties.h"
3116020011SCédric Le Goater 
3216020011SCédric Le Goater /* I2C Global Register */
3316020011SCédric Le Goater 
3416020011SCédric Le Goater #define I2C_CTRL_STATUS         0x00        /* Device Interrupt Status */
3516020011SCédric Le Goater #define I2C_CTRL_ASSIGN         0x08        /* Device Interrupt Target
3616020011SCédric Le Goater                                                Assignment */
37aab90b1cSCédric Le Goater #define I2C_CTRL_GLOBAL         0x0C        /* Global Control Register */
38aab90b1cSCédric Le Goater #define   I2C_CTRL_SRAM_EN                 BIT(0)
3916020011SCédric Le Goater 
4016020011SCédric Le Goater /* I2C Device (Bus) Register */
4116020011SCédric Le Goater 
4216020011SCédric Le Goater #define I2CD_FUN_CTRL_REG       0x00       /* I2CD Function Control  */
436054fc73SCédric Le Goater #define   I2CD_POOL_PAGE_SEL(x)            (((x) >> 20) & 0x7)  /* AST2400 */
4416020011SCédric Le Goater #define   I2CD_M_SDA_LOCK_EN               (0x1 << 16)
4516020011SCédric Le Goater #define   I2CD_MULTI_MASTER_DIS            (0x1 << 15)
4616020011SCédric Le Goater #define   I2CD_M_SCL_DRIVE_EN              (0x1 << 14)
4716020011SCédric Le Goater #define   I2CD_MSB_STS                     (0x1 << 9)
4816020011SCédric Le Goater #define   I2CD_SDA_DRIVE_1T_EN             (0x1 << 8)
4916020011SCédric Le Goater #define   I2CD_M_SDA_DRIVE_1T_EN           (0x1 << 7)
5016020011SCédric Le Goater #define   I2CD_M_HIGH_SPEED_EN             (0x1 << 6)
5116020011SCédric Le Goater #define   I2CD_DEF_ADDR_EN                 (0x1 << 5)
5216020011SCédric Le Goater #define   I2CD_DEF_ALERT_EN                (0x1 << 4)
5316020011SCédric Le Goater #define   I2CD_DEF_ARP_EN                  (0x1 << 3)
5416020011SCédric Le Goater #define   I2CD_DEF_GCALL_EN                (0x1 << 2)
5516020011SCédric Le Goater #define   I2CD_SLAVE_EN                    (0x1 << 1)
5616020011SCédric Le Goater #define   I2CD_MASTER_EN                   (0x1)
5716020011SCédric Le Goater 
5816020011SCédric Le Goater #define I2CD_AC_TIMING_REG1     0x04       /* Clock and AC Timing Control #1 */
5916020011SCédric Le Goater #define I2CD_AC_TIMING_REG2     0x08       /* Clock and AC Timing Control #1 */
6016020011SCédric Le Goater #define I2CD_INTR_CTRL_REG      0x0c       /* I2CD Interrupt Control */
6116020011SCédric Le Goater #define I2CD_INTR_STS_REG       0x10       /* I2CD Interrupt Status */
625540cb97SCédric Le Goater 
635540cb97SCédric Le Goater #define   I2CD_INTR_SLAVE_ADDR_MATCH       (0x1 << 31) /* 0: addr1 1: addr2 */
645540cb97SCédric Le Goater #define   I2CD_INTR_SLAVE_ADDR_RX_PENDING  (0x1 << 30)
655540cb97SCédric Le Goater /* bits[19-16] Reserved */
665540cb97SCédric Le Goater 
675540cb97SCédric Le Goater /* All bits below are cleared by writing 1 */
685540cb97SCédric Le Goater #define   I2CD_INTR_SLAVE_INACTIVE_TIMEOUT (0x1 << 15)
6916020011SCédric Le Goater #define   I2CD_INTR_SDA_DL_TIMEOUT         (0x1 << 14)
7016020011SCédric Le Goater #define   I2CD_INTR_BUS_RECOVER_DONE       (0x1 << 13)
7116020011SCédric Le Goater #define   I2CD_INTR_SMBUS_ALERT            (0x1 << 12) /* Bus [0-3] only */
7216020011SCédric Le Goater #define   I2CD_INTR_SMBUS_ARP_ADDR         (0x1 << 11) /* Removed */
7316020011SCédric Le Goater #define   I2CD_INTR_SMBUS_DEV_ALERT_ADDR   (0x1 << 10) /* Removed */
7416020011SCédric Le Goater #define   I2CD_INTR_SMBUS_DEF_ADDR         (0x1 << 9)  /* Removed */
7516020011SCédric Le Goater #define   I2CD_INTR_GCALL_ADDR             (0x1 << 8)  /* Removed */
765540cb97SCédric Le Goater #define   I2CD_INTR_SLAVE_ADDR_RX_MATCH    (0x1 << 7)  /* use RX_DONE */
7716020011SCédric Le Goater #define   I2CD_INTR_SCL_TIMEOUT            (0x1 << 6)
7816020011SCédric Le Goater #define   I2CD_INTR_ABNORMAL               (0x1 << 5)
7916020011SCédric Le Goater #define   I2CD_INTR_NORMAL_STOP            (0x1 << 4)
8016020011SCédric Le Goater #define   I2CD_INTR_ARBIT_LOSS             (0x1 << 3)
8116020011SCédric Le Goater #define   I2CD_INTR_RX_DONE                (0x1 << 2)
8216020011SCédric Le Goater #define   I2CD_INTR_TX_NAK                 (0x1 << 1)
8316020011SCédric Le Goater #define   I2CD_INTR_TX_ACK                 (0x1 << 0)
8416020011SCédric Le Goater 
8516020011SCédric Le Goater #define I2CD_CMD_REG            0x14       /* I2CD Command/Status */
8616020011SCédric Le Goater #define   I2CD_SDA_OE                      (0x1 << 28)
8716020011SCédric Le Goater #define   I2CD_SDA_O                       (0x1 << 27)
8816020011SCédric Le Goater #define   I2CD_SCL_OE                      (0x1 << 26)
8916020011SCédric Le Goater #define   I2CD_SCL_O                       (0x1 << 25)
9016020011SCédric Le Goater #define   I2CD_TX_TIMING                   (0x1 << 24)
9116020011SCédric Le Goater #define   I2CD_TX_STATUS                   (0x1 << 23)
9216020011SCédric Le Goater 
9316020011SCédric Le Goater #define   I2CD_TX_STATE_SHIFT              19 /* Tx State Machine */
9416020011SCédric Le Goater #define   I2CD_TX_STATE_MASK                  0xf
9516020011SCédric Le Goater #define     I2CD_IDLE                         0x0
9616020011SCédric Le Goater #define     I2CD_MACTIVE                      0x8
9716020011SCédric Le Goater #define     I2CD_MSTART                       0x9
9816020011SCédric Le Goater #define     I2CD_MSTARTR                      0xa
9916020011SCédric Le Goater #define     I2CD_MSTOP                        0xb
10016020011SCédric Le Goater #define     I2CD_MTXD                         0xc
10116020011SCédric Le Goater #define     I2CD_MRXACK                       0xd
10216020011SCédric Le Goater #define     I2CD_MRXD                         0xe
10316020011SCédric Le Goater #define     I2CD_MTXACK                       0xf
10416020011SCédric Le Goater #define     I2CD_SWAIT                        0x1
10516020011SCédric Le Goater #define     I2CD_SRXD                         0x4
10616020011SCédric Le Goater #define     I2CD_STXACK                       0x5
10716020011SCédric Le Goater #define     I2CD_STXD                         0x6
10816020011SCédric Le Goater #define     I2CD_SRXACK                       0x7
10916020011SCédric Le Goater #define     I2CD_RECOVER                      0x3
11016020011SCédric Le Goater 
11116020011SCédric Le Goater #define   I2CD_SCL_LINE_STS                (0x1 << 18)
11216020011SCédric Le Goater #define   I2CD_SDA_LINE_STS                (0x1 << 17)
11316020011SCédric Le Goater #define   I2CD_BUS_BUSY_STS                (0x1 << 16)
11416020011SCédric Le Goater #define   I2CD_SDA_OE_OUT_DIR              (0x1 << 15)
11516020011SCédric Le Goater #define   I2CD_SDA_O_OUT_DIR               (0x1 << 14)
11616020011SCédric Le Goater #define   I2CD_SCL_OE_OUT_DIR              (0x1 << 13)
11716020011SCédric Le Goater #define   I2CD_SCL_O_OUT_DIR               (0x1 << 12)
11816020011SCédric Le Goater #define   I2CD_BUS_RECOVER_CMD_EN          (0x1 << 11)
11916020011SCédric Le Goater #define   I2CD_S_ALT_EN                    (0x1 << 10)
12016020011SCédric Le Goater 
12116020011SCédric Le Goater /* Command Bit */
1226054fc73SCédric Le Goater #define   I2CD_RX_DMA_ENABLE               (0x1 << 9)
1236054fc73SCédric Le Goater #define   I2CD_TX_DMA_ENABLE               (0x1 << 8)
1246054fc73SCédric Le Goater #define   I2CD_RX_BUFF_ENABLE              (0x1 << 7)
1256054fc73SCédric Le Goater #define   I2CD_TX_BUFF_ENABLE              (0x1 << 6)
12616020011SCédric Le Goater #define   I2CD_M_STOP_CMD                  (0x1 << 5)
12716020011SCédric Le Goater #define   I2CD_M_S_RX_CMD_LAST             (0x1 << 4)
12816020011SCédric Le Goater #define   I2CD_M_RX_CMD                    (0x1 << 3)
12916020011SCédric Le Goater #define   I2CD_S_TX_CMD                    (0x1 << 2)
13016020011SCédric Le Goater #define   I2CD_M_TX_CMD                    (0x1 << 1)
13116020011SCédric Le Goater #define   I2CD_M_START_CMD                 (0x1)
13216020011SCédric Le Goater 
13316020011SCédric Le Goater #define I2CD_DEV_ADDR_REG       0x18       /* Slave Device Address */
1346054fc73SCédric Le Goater #define I2CD_POOL_CTRL_REG      0x1c       /* Pool Buffer Control */
1356054fc73SCédric Le Goater #define   I2CD_POOL_RX_COUNT(x)            (((x) >> 24) & 0xff)
1366054fc73SCédric Le Goater #define   I2CD_POOL_RX_SIZE(x)             ((((x) >> 16) & 0xff) + 1)
1376054fc73SCédric Le Goater #define   I2CD_POOL_TX_COUNT(x)            ((((x) >> 8) & 0xff) + 1)
1386054fc73SCédric Le Goater #define   I2CD_POOL_OFFSET(x)              (((x) & 0x3f) << 2)  /* AST2400 */
13916020011SCédric Le Goater #define I2CD_BYTE_BUF_REG       0x20       /* Transmit/Receive Byte Buffer */
14016020011SCédric Le Goater #define   I2CD_BYTE_BUF_TX_SHIFT           0
14116020011SCédric Le Goater #define   I2CD_BYTE_BUF_TX_MASK            0xff
14216020011SCédric Le Goater #define   I2CD_BYTE_BUF_RX_SHIFT           8
14316020011SCédric Le Goater #define   I2CD_BYTE_BUF_RX_MASK            0xff
144*545d6befSCédric Le Goater #define I2CD_DMA_ADDR           0x24       /* DMA Buffer Address */
145*545d6befSCédric Le Goater #define I2CD_DMA_LEN            0x28       /* DMA Transfer Length < 4KB */
14616020011SCédric Le Goater 
14716020011SCédric Le Goater static inline bool aspeed_i2c_bus_is_master(AspeedI2CBus *bus)
14816020011SCédric Le Goater {
14916020011SCédric Le Goater     return bus->ctrl & I2CD_MASTER_EN;
15016020011SCédric Le Goater }
15116020011SCédric Le Goater 
15216020011SCédric Le Goater static inline bool aspeed_i2c_bus_is_enabled(AspeedI2CBus *bus)
15316020011SCédric Le Goater {
15416020011SCédric Le Goater     return bus->ctrl & (I2CD_MASTER_EN | I2CD_SLAVE_EN);
15516020011SCédric Le Goater }
15616020011SCédric Le Goater 
15716020011SCédric Le Goater static inline void aspeed_i2c_bus_raise_interrupt(AspeedI2CBus *bus)
15816020011SCédric Le Goater {
15951dd4923SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
16051dd4923SCédric Le Goater 
16116020011SCédric Le Goater     bus->intr_status &= bus->intr_ctrl;
16216020011SCédric Le Goater     if (bus->intr_status) {
16316020011SCédric Le Goater         bus->controller->intr_status |= 1 << bus->id;
16451dd4923SCédric Le Goater         qemu_irq_raise(aic->bus_get_irq(bus));
16516020011SCédric Le Goater     }
16616020011SCédric Le Goater }
16716020011SCédric Le Goater 
16816020011SCédric Le Goater static uint64_t aspeed_i2c_bus_read(void *opaque, hwaddr offset,
16916020011SCédric Le Goater                                     unsigned size)
17016020011SCédric Le Goater {
17116020011SCédric Le Goater     AspeedI2CBus *bus = opaque;
172*545d6befSCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
17316020011SCédric Le Goater 
17416020011SCédric Le Goater     switch (offset) {
17516020011SCédric Le Goater     case I2CD_FUN_CTRL_REG:
17616020011SCédric Le Goater         return bus->ctrl;
17716020011SCédric Le Goater     case I2CD_AC_TIMING_REG1:
17816020011SCédric Le Goater         return bus->timing[0];
17916020011SCédric Le Goater     case I2CD_AC_TIMING_REG2:
18016020011SCédric Le Goater         return bus->timing[1];
18116020011SCédric Le Goater     case I2CD_INTR_CTRL_REG:
18216020011SCédric Le Goater         return bus->intr_ctrl;
18316020011SCédric Le Goater     case I2CD_INTR_STS_REG:
18416020011SCédric Le Goater         return bus->intr_status;
1856054fc73SCédric Le Goater     case I2CD_POOL_CTRL_REG:
1866054fc73SCédric Le Goater         return bus->pool_ctrl;
18716020011SCédric Le Goater     case I2CD_BYTE_BUF_REG:
18816020011SCédric Le Goater         return bus->buf;
18916020011SCédric Le Goater     case I2CD_CMD_REG:
19016020011SCédric Le Goater         return bus->cmd | (i2c_bus_busy(bus->bus) << 16);
191*545d6befSCédric Le Goater     case I2CD_DMA_ADDR:
192*545d6befSCédric Le Goater         if (!aic->has_dma) {
193*545d6befSCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n",  __func__);
194*545d6befSCédric Le Goater             return -1;
195*545d6befSCédric Le Goater         }
196*545d6befSCédric Le Goater         return bus->dma_addr;
197*545d6befSCédric Le Goater     case I2CD_DMA_LEN:
198*545d6befSCédric Le Goater         if (!aic->has_dma) {
199*545d6befSCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n",  __func__);
200*545d6befSCédric Le Goater             return -1;
201*545d6befSCédric Le Goater         }
202*545d6befSCédric Le Goater         return bus->dma_len;
20316020011SCédric Le Goater     default:
20416020011SCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR,
20516020011SCédric Le Goater                       "%s: Bad offset 0x%" HWADDR_PRIx "\n", __func__, offset);
20616020011SCédric Le Goater         return -1;
20716020011SCédric Le Goater     }
20816020011SCédric Le Goater }
20916020011SCédric Le Goater 
2104960f084SCédric Le Goater static void aspeed_i2c_set_state(AspeedI2CBus *bus, uint8_t state)
2114960f084SCédric Le Goater {
2124960f084SCédric Le Goater     bus->cmd &= ~(I2CD_TX_STATE_MASK << I2CD_TX_STATE_SHIFT);
2134960f084SCédric Le Goater     bus->cmd |= (state & I2CD_TX_STATE_MASK) << I2CD_TX_STATE_SHIFT;
2144960f084SCédric Le Goater }
2154960f084SCédric Le Goater 
2164960f084SCédric Le Goater static uint8_t aspeed_i2c_get_state(AspeedI2CBus *bus)
2174960f084SCédric Le Goater {
2184960f084SCédric Le Goater     return (bus->cmd >> I2CD_TX_STATE_SHIFT) & I2CD_TX_STATE_MASK;
2194960f084SCédric Le Goater }
2204960f084SCédric Le Goater 
221*545d6befSCédric Le Goater static int aspeed_i2c_dma_read(AspeedI2CBus *bus, uint8_t *data)
222*545d6befSCédric Le Goater {
223*545d6befSCédric Le Goater     MemTxResult result;
224*545d6befSCédric Le Goater     AspeedI2CState *s = bus->controller;
225*545d6befSCédric Le Goater 
226*545d6befSCédric Le Goater     result = address_space_read(&s->dram_as, bus->dma_addr,
227*545d6befSCédric Le Goater                                 MEMTXATTRS_UNSPECIFIED, data, 1);
228*545d6befSCédric Le Goater     if (result != MEMTX_OK) {
229*545d6befSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "%s: DRAM read failed @%08x\n",
230*545d6befSCédric Le Goater                       __func__, bus->dma_addr);
231*545d6befSCédric Le Goater         return -1;
232*545d6befSCédric Le Goater     }
233*545d6befSCédric Le Goater 
234*545d6befSCédric Le Goater     bus->dma_addr++;
235*545d6befSCédric Le Goater     bus->dma_len--;
236*545d6befSCédric Le Goater     return 0;
237*545d6befSCédric Le Goater }
238*545d6befSCédric Le Goater 
2396054fc73SCédric Le Goater static int aspeed_i2c_bus_send(AspeedI2CBus *bus, uint8_t pool_start)
2406054fc73SCédric Le Goater {
2416054fc73SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
2426054fc73SCédric Le Goater     int ret = -1;
2436054fc73SCédric Le Goater     int i;
2446054fc73SCédric Le Goater 
2456054fc73SCédric Le Goater     if (bus->cmd & I2CD_TX_BUFF_ENABLE) {
2466054fc73SCédric Le Goater         for (i = pool_start; i < I2CD_POOL_TX_COUNT(bus->pool_ctrl); i++) {
2476054fc73SCédric Le Goater             uint8_t *pool_base = aic->bus_pool_base(bus);
2486054fc73SCédric Le Goater 
2496054fc73SCédric Le Goater             ret = i2c_send(bus->bus, pool_base[i]);
2506054fc73SCédric Le Goater             if (ret) {
2516054fc73SCédric Le Goater                 break;
2526054fc73SCédric Le Goater             }
2536054fc73SCédric Le Goater         }
2546054fc73SCédric Le Goater         bus->cmd &= ~I2CD_TX_BUFF_ENABLE;
255*545d6befSCédric Le Goater     } else if (bus->cmd & I2CD_TX_DMA_ENABLE) {
256*545d6befSCédric Le Goater         while (bus->dma_len) {
257*545d6befSCédric Le Goater             uint8_t data;
258*545d6befSCédric Le Goater             aspeed_i2c_dma_read(bus, &data);
259*545d6befSCédric Le Goater             ret = i2c_send(bus->bus, data);
260*545d6befSCédric Le Goater             if (ret) {
261*545d6befSCédric Le Goater                 break;
262*545d6befSCédric Le Goater             }
263*545d6befSCédric Le Goater         }
264*545d6befSCédric Le Goater         bus->cmd &= ~I2CD_TX_DMA_ENABLE;
2656054fc73SCédric Le Goater     } else {
2666054fc73SCédric Le Goater         ret = i2c_send(bus->bus, bus->buf);
2676054fc73SCédric Le Goater     }
2686054fc73SCédric Le Goater 
2696054fc73SCédric Le Goater     return ret;
2706054fc73SCédric Le Goater }
2716054fc73SCédric Le Goater 
2726054fc73SCédric Le Goater static void aspeed_i2c_bus_recv(AspeedI2CBus *bus)
2736054fc73SCédric Le Goater {
2746054fc73SCédric Le Goater     AspeedI2CState *s = bus->controller;
2756054fc73SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
2766054fc73SCédric Le Goater     uint8_t data;
2776054fc73SCédric Le Goater     int i;
2786054fc73SCédric Le Goater 
2796054fc73SCédric Le Goater     if (bus->cmd & I2CD_RX_BUFF_ENABLE) {
2806054fc73SCédric Le Goater         uint8_t *pool_base = aic->bus_pool_base(bus);
2816054fc73SCédric Le Goater 
2826054fc73SCédric Le Goater         for (i = 0; i < I2CD_POOL_RX_SIZE(bus->pool_ctrl); i++) {
2836054fc73SCédric Le Goater             pool_base[i] = i2c_recv(bus->bus);
2846054fc73SCédric Le Goater         }
2856054fc73SCédric Le Goater 
2866054fc73SCédric Le Goater         /* Update RX count */
2876054fc73SCédric Le Goater         bus->pool_ctrl &= ~(0xff << 24);
2886054fc73SCédric Le Goater         bus->pool_ctrl |= (i & 0xff) << 24;
2896054fc73SCédric Le Goater         bus->cmd &= ~I2CD_RX_BUFF_ENABLE;
290*545d6befSCédric Le Goater     } else if (bus->cmd & I2CD_RX_DMA_ENABLE) {
291*545d6befSCédric Le Goater         uint8_t data;
292*545d6befSCédric Le Goater 
293*545d6befSCédric Le Goater         while (bus->dma_len) {
294*545d6befSCédric Le Goater             MemTxResult result;
295*545d6befSCédric Le Goater 
296*545d6befSCédric Le Goater             data = i2c_recv(bus->bus);
297*545d6befSCédric Le Goater             result = address_space_write(&s->dram_as, bus->dma_addr,
298*545d6befSCédric Le Goater                                          MEMTXATTRS_UNSPECIFIED, &data, 1);
299*545d6befSCédric Le Goater             if (result != MEMTX_OK) {
300*545d6befSCédric Le Goater                 qemu_log_mask(LOG_GUEST_ERROR, "%s: DRAM write failed @%08x\n",
301*545d6befSCédric Le Goater                               __func__, bus->dma_addr);
302*545d6befSCédric Le Goater                 return;
303*545d6befSCédric Le Goater             }
304*545d6befSCédric Le Goater             bus->dma_addr++;
305*545d6befSCédric Le Goater             bus->dma_len--;
306*545d6befSCédric Le Goater         }
307*545d6befSCédric Le Goater         bus->cmd &= ~I2CD_RX_DMA_ENABLE;
3086054fc73SCédric Le Goater     } else {
3096054fc73SCédric Le Goater         data = i2c_recv(bus->bus);
3106054fc73SCédric Le Goater         bus->buf = (data & I2CD_BYTE_BUF_RX_MASK) << I2CD_BYTE_BUF_RX_SHIFT;
3116054fc73SCédric Le Goater     }
3126054fc73SCédric Le Goater }
3136054fc73SCédric Le Goater 
3147bd9c60dSGuenter Roeck static void aspeed_i2c_handle_rx_cmd(AspeedI2CBus *bus)
3157bd9c60dSGuenter Roeck {
3167bd9c60dSGuenter Roeck     aspeed_i2c_set_state(bus, I2CD_MRXD);
3176054fc73SCédric Le Goater     aspeed_i2c_bus_recv(bus);
3187bd9c60dSGuenter Roeck     bus->intr_status |= I2CD_INTR_RX_DONE;
3197bd9c60dSGuenter Roeck     if (bus->cmd & I2CD_M_S_RX_CMD_LAST) {
3207bd9c60dSGuenter Roeck         i2c_nack(bus->bus);
3217bd9c60dSGuenter Roeck     }
3227bd9c60dSGuenter Roeck     bus->cmd &= ~(I2CD_M_RX_CMD | I2CD_M_S_RX_CMD_LAST);
3237bd9c60dSGuenter Roeck     aspeed_i2c_set_state(bus, I2CD_MACTIVE);
3247bd9c60dSGuenter Roeck }
3257bd9c60dSGuenter Roeck 
3266054fc73SCédric Le Goater static uint8_t aspeed_i2c_get_addr(AspeedI2CBus *bus)
3276054fc73SCédric Le Goater {
3286054fc73SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
3296054fc73SCédric Le Goater 
3306054fc73SCédric Le Goater     if (bus->cmd & I2CD_TX_BUFF_ENABLE) {
3316054fc73SCédric Le Goater         uint8_t *pool_base = aic->bus_pool_base(bus);
3326054fc73SCédric Le Goater 
3336054fc73SCédric Le Goater         return pool_base[0];
334*545d6befSCédric Le Goater     } else if (bus->cmd & I2CD_TX_DMA_ENABLE) {
335*545d6befSCédric Le Goater         uint8_t data;
336*545d6befSCédric Le Goater 
337*545d6befSCédric Le Goater         aspeed_i2c_dma_read(bus, &data);
338*545d6befSCédric Le Goater         return data;
3396054fc73SCédric Le Goater     } else {
3406054fc73SCédric Le Goater         return bus->buf;
3416054fc73SCédric Le Goater     }
3426054fc73SCédric Le Goater }
3436054fc73SCédric Le Goater 
344aab90b1cSCédric Le Goater static bool aspeed_i2c_check_sram(AspeedI2CBus *bus)
345aab90b1cSCédric Le Goater {
346aab90b1cSCédric Le Goater     AspeedI2CState *s = bus->controller;
347aab90b1cSCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
348aab90b1cSCédric Le Goater 
349aab90b1cSCédric Le Goater     if (!aic->check_sram) {
350aab90b1cSCédric Le Goater         return true;
351aab90b1cSCédric Le Goater     }
352aab90b1cSCédric Le Goater 
353aab90b1cSCédric Le Goater     /*
354aab90b1cSCédric Le Goater      * AST2500: SRAM must be enabled before using the Buffer Pool or
355aab90b1cSCédric Le Goater      * DMA mode.
356aab90b1cSCédric Le Goater      */
357aab90b1cSCédric Le Goater     if (!(s->ctrl_global & I2C_CTRL_SRAM_EN) &&
358aab90b1cSCédric Le Goater         (bus->cmd & (I2CD_RX_DMA_ENABLE | I2CD_TX_DMA_ENABLE |
359aab90b1cSCédric Le Goater                      I2CD_RX_BUFF_ENABLE | I2CD_TX_BUFF_ENABLE))) {
360aab90b1cSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "%s: SRAM is not enabled\n", __func__);
361aab90b1cSCédric Le Goater         return false;
362aab90b1cSCédric Le Goater     }
363aab90b1cSCédric Le Goater 
364aab90b1cSCédric Le Goater     return true;
365aab90b1cSCédric Le Goater }
366aab90b1cSCédric Le Goater 
3674960f084SCédric Le Goater /*
3684960f084SCédric Le Goater  * The state machine needs some refinement. It is only used to track
3694960f084SCédric Le Goater  * invalid STOP commands for the moment.
3704960f084SCédric Le Goater  */
37116020011SCédric Le Goater static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value)
37216020011SCédric Le Goater {
3736054fc73SCédric Le Goater     uint8_t pool_start = 0;
3746054fc73SCédric Le Goater 
375ddabca75SCédric Le Goater     bus->cmd &= ~0xFFFF;
37616020011SCédric Le Goater     bus->cmd |= value & 0xFFFF;
37716020011SCédric Le Goater 
378aab90b1cSCédric Le Goater     if (!aspeed_i2c_check_sram(bus)) {
379aab90b1cSCédric Le Goater         return;
380aab90b1cSCédric Le Goater     }
381aab90b1cSCédric Le Goater 
38216020011SCédric Le Goater     if (bus->cmd & I2CD_M_START_CMD) {
3834960f084SCédric Le Goater         uint8_t state = aspeed_i2c_get_state(bus) & I2CD_MACTIVE ?
3844960f084SCédric Le Goater             I2CD_MSTARTR : I2CD_MSTART;
3856054fc73SCédric Le Goater         uint8_t addr;
3864960f084SCédric Le Goater 
3874960f084SCédric Le Goater         aspeed_i2c_set_state(bus, state);
3884960f084SCédric Le Goater 
3896054fc73SCédric Le Goater         addr = aspeed_i2c_get_addr(bus);
3906054fc73SCédric Le Goater 
3916054fc73SCédric Le Goater         if (i2c_start_transfer(bus->bus, extract32(addr, 1, 7),
3926054fc73SCédric Le Goater                                extract32(addr, 0, 1))) {
39316020011SCédric Le Goater             bus->intr_status |= I2CD_INTR_TX_NAK;
39416020011SCédric Le Goater         } else {
39516020011SCédric Le Goater             bus->intr_status |= I2CD_INTR_TX_ACK;
39616020011SCédric Le Goater         }
39716020011SCédric Le Goater 
3986054fc73SCédric Le Goater         bus->cmd &= ~I2CD_M_START_CMD;
3996054fc73SCédric Le Goater 
4006054fc73SCédric Le Goater         /*
4016054fc73SCédric Le Goater          * The START command is also a TX command, as the slave
4026054fc73SCédric Le Goater          * address is sent on the bus. Drop the TX flag if nothing
4036054fc73SCédric Le Goater          * else needs to be sent in this sequence.
4046054fc73SCédric Le Goater          */
4056054fc73SCédric Le Goater         if (bus->cmd & I2CD_TX_BUFF_ENABLE) {
4066054fc73SCédric Le Goater             if (I2CD_POOL_TX_COUNT(bus->pool_ctrl) == 1) {
4076054fc73SCédric Le Goater                 bus->cmd &= ~I2CD_M_TX_CMD;
4086054fc73SCédric Le Goater             } else {
4096054fc73SCédric Le Goater                 /*
4106054fc73SCédric Le Goater                  * Increase the start index in the TX pool buffer to
4116054fc73SCédric Le Goater                  * skip the address byte.
4126054fc73SCédric Le Goater                  */
4136054fc73SCédric Le Goater                 pool_start++;
4146054fc73SCédric Le Goater             }
415*545d6befSCédric Le Goater         } else if (bus->cmd & I2CD_TX_DMA_ENABLE) {
416*545d6befSCédric Le Goater             if (bus->dma_len == 0) {
417*545d6befSCédric Le Goater                 bus->cmd &= ~I2CD_M_TX_CMD;
418*545d6befSCédric Le Goater             }
4196054fc73SCédric Le Goater         } else {
4206054fc73SCédric Le Goater             bus->cmd &= ~I2CD_M_TX_CMD;
4216054fc73SCédric Le Goater         }
422ddabca75SCédric Le Goater 
423ddabca75SCédric Le Goater         /* No slave found */
424ddabca75SCédric Le Goater         if (!i2c_bus_busy(bus->bus)) {
425ddabca75SCédric Le Goater             return;
426ddabca75SCédric Le Goater         }
4274960f084SCédric Le Goater         aspeed_i2c_set_state(bus, I2CD_MACTIVE);
428ddabca75SCédric Le Goater     }
429ddabca75SCédric Le Goater 
430ddabca75SCédric Le Goater     if (bus->cmd & I2CD_M_TX_CMD) {
4314960f084SCédric Le Goater         aspeed_i2c_set_state(bus, I2CD_MTXD);
4326054fc73SCédric Le Goater         if (aspeed_i2c_bus_send(bus, pool_start)) {
4334960f084SCédric Le Goater             bus->intr_status |= (I2CD_INTR_TX_NAK);
43416020011SCédric Le Goater             i2c_end_transfer(bus->bus);
43516020011SCédric Le Goater         } else {
43616020011SCédric Le Goater             bus->intr_status |= I2CD_INTR_TX_ACK;
43716020011SCédric Le Goater         }
438ddabca75SCédric Le Goater         bus->cmd &= ~I2CD_M_TX_CMD;
4394960f084SCédric Le Goater         aspeed_i2c_set_state(bus, I2CD_MACTIVE);
440ddabca75SCédric Le Goater     }
44116020011SCédric Le Goater 
442bb626e5bSGuenter Roeck     if ((bus->cmd & (I2CD_M_RX_CMD | I2CD_M_S_RX_CMD_LAST)) &&
443bb626e5bSGuenter Roeck         !(bus->intr_status & I2CD_INTR_RX_DONE)) {
4447bd9c60dSGuenter Roeck         aspeed_i2c_handle_rx_cmd(bus);
44516020011SCédric Le Goater     }
44616020011SCédric Le Goater 
447d0efdc16SCédric Le Goater     if (bus->cmd & I2CD_M_STOP_CMD) {
4484960f084SCédric Le Goater         if (!(aspeed_i2c_get_state(bus) & I2CD_MACTIVE)) {
4494960f084SCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "%s: abnormal stop\n", __func__);
45016020011SCédric Le Goater             bus->intr_status |= I2CD_INTR_ABNORMAL;
45116020011SCédric Le Goater         } else {
4524960f084SCédric Le Goater             aspeed_i2c_set_state(bus, I2CD_MSTOP);
45316020011SCédric Le Goater             i2c_end_transfer(bus->bus);
45416020011SCédric Le Goater             bus->intr_status |= I2CD_INTR_NORMAL_STOP;
45516020011SCédric Le Goater         }
456ddabca75SCédric Le Goater         bus->cmd &= ~I2CD_M_STOP_CMD;
4574960f084SCédric Le Goater         aspeed_i2c_set_state(bus, I2CD_IDLE);
45816020011SCédric Le Goater     }
45916020011SCédric Le Goater }
46016020011SCédric Le Goater 
46116020011SCédric Le Goater static void aspeed_i2c_bus_write(void *opaque, hwaddr offset,
46216020011SCédric Le Goater                                  uint64_t value, unsigned size)
46316020011SCédric Le Goater {
46416020011SCédric Le Goater     AspeedI2CBus *bus = opaque;
46551dd4923SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
466bb626e5bSGuenter Roeck     bool handle_rx;
46716020011SCédric Le Goater 
46816020011SCédric Le Goater     switch (offset) {
46916020011SCédric Le Goater     case I2CD_FUN_CTRL_REG:
47016020011SCédric Le Goater         if (value & I2CD_SLAVE_EN) {
47116020011SCédric Le Goater             qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n",
47216020011SCédric Le Goater                           __func__);
47316020011SCédric Le Goater             break;
47416020011SCédric Le Goater         }
47516020011SCédric Le Goater         bus->ctrl = value & 0x0071C3FF;
47616020011SCédric Le Goater         break;
47716020011SCédric Le Goater     case I2CD_AC_TIMING_REG1:
47816020011SCédric Le Goater         bus->timing[0] = value & 0xFFFFF0F;
47916020011SCédric Le Goater         break;
48016020011SCédric Le Goater     case I2CD_AC_TIMING_REG2:
48116020011SCédric Le Goater         bus->timing[1] = value & 0x7;
48216020011SCédric Le Goater         break;
48316020011SCédric Le Goater     case I2CD_INTR_CTRL_REG:
48416020011SCédric Le Goater         bus->intr_ctrl = value & 0x7FFF;
48516020011SCédric Le Goater         break;
48616020011SCédric Le Goater     case I2CD_INTR_STS_REG:
487bb626e5bSGuenter Roeck         handle_rx = (bus->intr_status & I2CD_INTR_RX_DONE) &&
488bb626e5bSGuenter Roeck                 (value & I2CD_INTR_RX_DONE);
48916020011SCédric Le Goater         bus->intr_status &= ~(value & 0x7FFF);
4905540cb97SCédric Le Goater         if (!bus->intr_status) {
49116020011SCédric Le Goater             bus->controller->intr_status &= ~(1 << bus->id);
49251dd4923SCédric Le Goater             qemu_irq_lower(aic->bus_get_irq(bus));
4935540cb97SCédric Le Goater         }
494bb626e5bSGuenter Roeck         if (handle_rx && (bus->cmd & (I2CD_M_RX_CMD | I2CD_M_S_RX_CMD_LAST))) {
495bb626e5bSGuenter Roeck             aspeed_i2c_handle_rx_cmd(bus);
496bb626e5bSGuenter Roeck             aspeed_i2c_bus_raise_interrupt(bus);
497bb626e5bSGuenter Roeck         }
49816020011SCédric Le Goater         break;
49916020011SCédric Le Goater     case I2CD_DEV_ADDR_REG:
50016020011SCédric Le Goater         qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n",
50116020011SCédric Le Goater                       __func__);
50216020011SCédric Le Goater         break;
5036054fc73SCédric Le Goater     case I2CD_POOL_CTRL_REG:
5046054fc73SCédric Le Goater         bus->pool_ctrl &= ~0xffffff;
5056054fc73SCédric Le Goater         bus->pool_ctrl |= (value & 0xffffff);
5066054fc73SCédric Le Goater         break;
5076054fc73SCédric Le Goater 
50816020011SCédric Le Goater     case I2CD_BYTE_BUF_REG:
50916020011SCédric Le Goater         bus->buf = (value & I2CD_BYTE_BUF_TX_MASK) << I2CD_BYTE_BUF_TX_SHIFT;
51016020011SCédric Le Goater         break;
51116020011SCédric Le Goater     case I2CD_CMD_REG:
51216020011SCédric Le Goater         if (!aspeed_i2c_bus_is_enabled(bus)) {
51316020011SCédric Le Goater             break;
51416020011SCédric Le Goater         }
51516020011SCédric Le Goater 
51616020011SCédric Le Goater         if (!aspeed_i2c_bus_is_master(bus)) {
51716020011SCédric Le Goater             qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n",
51816020011SCédric Le Goater                           __func__);
51916020011SCédric Le Goater             break;
52016020011SCédric Le Goater         }
52116020011SCédric Le Goater 
522*545d6befSCédric Le Goater         if (!aic->has_dma &&
523*545d6befSCédric Le Goater             value & (I2CD_RX_DMA_ENABLE | I2CD_TX_DMA_ENABLE)) {
524*545d6befSCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n",  __func__);
525*545d6befSCédric Le Goater             break;
526*545d6befSCédric Le Goater         }
527*545d6befSCédric Le Goater 
52816020011SCédric Le Goater         aspeed_i2c_bus_handle_cmd(bus, value);
529ddabca75SCédric Le Goater         aspeed_i2c_bus_raise_interrupt(bus);
53016020011SCédric Le Goater         break;
531*545d6befSCédric Le Goater     case I2CD_DMA_ADDR:
532*545d6befSCédric Le Goater         if (!aic->has_dma) {
533*545d6befSCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n",  __func__);
534*545d6befSCédric Le Goater             break;
535*545d6befSCédric Le Goater         }
536*545d6befSCédric Le Goater 
537*545d6befSCédric Le Goater         bus->dma_addr = value & 0xfffffffc;
538*545d6befSCédric Le Goater         break;
539*545d6befSCédric Le Goater 
540*545d6befSCédric Le Goater     case I2CD_DMA_LEN:
541*545d6befSCédric Le Goater         if (!aic->has_dma) {
542*545d6befSCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n",  __func__);
543*545d6befSCédric Le Goater             break;
544*545d6befSCédric Le Goater         }
545*545d6befSCédric Le Goater 
546*545d6befSCédric Le Goater         bus->dma_len = value & 0xfff;
547*545d6befSCédric Le Goater         if (!bus->dma_len) {
548*545d6befSCédric Le Goater             qemu_log_mask(LOG_UNIMP, "%s: invalid DMA length\n",  __func__);
549*545d6befSCédric Le Goater         }
550*545d6befSCédric Le Goater         break;
55116020011SCédric Le Goater 
55216020011SCédric Le Goater     default:
55316020011SCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
55416020011SCédric Le Goater                       __func__, offset);
55516020011SCédric Le Goater     }
55616020011SCédric Le Goater }
55716020011SCédric Le Goater 
55816020011SCédric Le Goater static uint64_t aspeed_i2c_ctrl_read(void *opaque, hwaddr offset,
55916020011SCédric Le Goater                                    unsigned size)
56016020011SCédric Le Goater {
56116020011SCédric Le Goater     AspeedI2CState *s = opaque;
56216020011SCédric Le Goater 
56316020011SCédric Le Goater     switch (offset) {
56416020011SCédric Le Goater     case I2C_CTRL_STATUS:
56516020011SCédric Le Goater         return s->intr_status;
566aab90b1cSCédric Le Goater     case I2C_CTRL_GLOBAL:
567aab90b1cSCédric Le Goater         return s->ctrl_global;
56816020011SCédric Le Goater     default:
56916020011SCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
57016020011SCédric Le Goater                       __func__, offset);
57116020011SCédric Le Goater         break;
57216020011SCédric Le Goater     }
57316020011SCédric Le Goater 
57416020011SCédric Le Goater     return -1;
57516020011SCédric Le Goater }
57616020011SCédric Le Goater 
57716020011SCédric Le Goater static void aspeed_i2c_ctrl_write(void *opaque, hwaddr offset,
57816020011SCédric Le Goater                                   uint64_t value, unsigned size)
57916020011SCédric Le Goater {
580aab90b1cSCédric Le Goater     AspeedI2CState *s = opaque;
581aab90b1cSCédric Le Goater 
58216020011SCédric Le Goater     switch (offset) {
583aab90b1cSCédric Le Goater     case I2C_CTRL_GLOBAL:
584aab90b1cSCédric Le Goater         s->ctrl_global = value;
585aab90b1cSCédric Le Goater         break;
58616020011SCédric Le Goater     case I2C_CTRL_STATUS:
58716020011SCédric Le Goater     default:
58816020011SCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
58916020011SCédric Le Goater                       __func__, offset);
59016020011SCédric Le Goater         break;
59116020011SCédric Le Goater     }
59216020011SCédric Le Goater }
59316020011SCédric Le Goater 
59416020011SCédric Le Goater static const MemoryRegionOps aspeed_i2c_bus_ops = {
59516020011SCédric Le Goater     .read = aspeed_i2c_bus_read,
59616020011SCédric Le Goater     .write = aspeed_i2c_bus_write,
59716020011SCédric Le Goater     .endianness = DEVICE_LITTLE_ENDIAN,
59816020011SCédric Le Goater };
59916020011SCédric Le Goater 
60016020011SCédric Le Goater static const MemoryRegionOps aspeed_i2c_ctrl_ops = {
60116020011SCédric Le Goater     .read = aspeed_i2c_ctrl_read,
60216020011SCédric Le Goater     .write = aspeed_i2c_ctrl_write,
60316020011SCédric Le Goater     .endianness = DEVICE_LITTLE_ENDIAN,
60416020011SCédric Le Goater };
60516020011SCédric Le Goater 
6066054fc73SCédric Le Goater static uint64_t aspeed_i2c_pool_read(void *opaque, hwaddr offset,
6076054fc73SCédric Le Goater                                      unsigned size)
6086054fc73SCédric Le Goater {
6096054fc73SCédric Le Goater     AspeedI2CState *s = opaque;
6106054fc73SCédric Le Goater     uint64_t ret = 0;
6116054fc73SCédric Le Goater     int i;
6126054fc73SCédric Le Goater 
6136054fc73SCédric Le Goater     for (i = 0; i < size; i++) {
6146054fc73SCédric Le Goater         ret |= (uint64_t) s->pool[offset + i] << (8 * i);
6156054fc73SCédric Le Goater     }
6166054fc73SCédric Le Goater 
6176054fc73SCédric Le Goater     return ret;
6186054fc73SCédric Le Goater }
6196054fc73SCédric Le Goater 
6206054fc73SCédric Le Goater static void aspeed_i2c_pool_write(void *opaque, hwaddr offset,
6216054fc73SCédric Le Goater                                   uint64_t value, unsigned size)
6226054fc73SCédric Le Goater {
6236054fc73SCédric Le Goater     AspeedI2CState *s = opaque;
6246054fc73SCédric Le Goater     int i;
6256054fc73SCédric Le Goater 
6266054fc73SCédric Le Goater     for (i = 0; i < size; i++) {
6276054fc73SCédric Le Goater         s->pool[offset + i] = (value >> (8 * i)) & 0xFF;
6286054fc73SCédric Le Goater     }
6296054fc73SCédric Le Goater }
6306054fc73SCédric Le Goater 
6316054fc73SCédric Le Goater static const MemoryRegionOps aspeed_i2c_pool_ops = {
6326054fc73SCédric Le Goater     .read = aspeed_i2c_pool_read,
6336054fc73SCédric Le Goater     .write = aspeed_i2c_pool_write,
6346054fc73SCédric Le Goater     .endianness = DEVICE_LITTLE_ENDIAN,
6356054fc73SCédric Le Goater     .valid = {
6366054fc73SCédric Le Goater         .min_access_size = 1,
6376054fc73SCédric Le Goater         .max_access_size = 4,
6386054fc73SCédric Le Goater     },
6396054fc73SCédric Le Goater };
6406054fc73SCédric Le Goater 
64116020011SCédric Le Goater static const VMStateDescription aspeed_i2c_bus_vmstate = {
64216020011SCédric Le Goater     .name = TYPE_ASPEED_I2C,
643*545d6befSCédric Le Goater     .version_id = 3,
644*545d6befSCédric Le Goater     .minimum_version_id = 3,
64516020011SCédric Le Goater     .fields = (VMStateField[]) {
64616020011SCédric Le Goater         VMSTATE_UINT8(id, AspeedI2CBus),
64716020011SCédric Le Goater         VMSTATE_UINT32(ctrl, AspeedI2CBus),
64816020011SCédric Le Goater         VMSTATE_UINT32_ARRAY(timing, AspeedI2CBus, 2),
64916020011SCédric Le Goater         VMSTATE_UINT32(intr_ctrl, AspeedI2CBus),
65016020011SCédric Le Goater         VMSTATE_UINT32(intr_status, AspeedI2CBus),
65116020011SCédric Le Goater         VMSTATE_UINT32(cmd, AspeedI2CBus),
65216020011SCédric Le Goater         VMSTATE_UINT32(buf, AspeedI2CBus),
6536054fc73SCédric Le Goater         VMSTATE_UINT32(pool_ctrl, AspeedI2CBus),
654*545d6befSCédric Le Goater         VMSTATE_UINT32(dma_addr, AspeedI2CBus),
655*545d6befSCédric Le Goater         VMSTATE_UINT32(dma_len, AspeedI2CBus),
65616020011SCédric Le Goater         VMSTATE_END_OF_LIST()
65716020011SCédric Le Goater     }
65816020011SCédric Le Goater };
65916020011SCédric Le Goater 
66016020011SCédric Le Goater static const VMStateDescription aspeed_i2c_vmstate = {
66116020011SCédric Le Goater     .name = TYPE_ASPEED_I2C,
6626054fc73SCédric Le Goater     .version_id = 2,
6636054fc73SCédric Le Goater     .minimum_version_id = 2,
66416020011SCédric Le Goater     .fields = (VMStateField[]) {
66516020011SCédric Le Goater         VMSTATE_UINT32(intr_status, AspeedI2CState),
66616020011SCédric Le Goater         VMSTATE_STRUCT_ARRAY(busses, AspeedI2CState,
66716020011SCédric Le Goater                              ASPEED_I2C_NR_BUSSES, 1, aspeed_i2c_bus_vmstate,
66816020011SCédric Le Goater                              AspeedI2CBus),
6696054fc73SCédric Le Goater         VMSTATE_UINT8_ARRAY(pool, AspeedI2CState, ASPEED_I2C_MAX_POOL_SIZE),
67016020011SCédric Le Goater         VMSTATE_END_OF_LIST()
67116020011SCédric Le Goater     }
67216020011SCédric Le Goater };
67316020011SCédric Le Goater 
67416020011SCédric Le Goater static void aspeed_i2c_reset(DeviceState *dev)
67516020011SCédric Le Goater {
67616020011SCédric Le Goater     int i;
67716020011SCédric Le Goater     AspeedI2CState *s = ASPEED_I2C(dev);
678f7da1aa8SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
67916020011SCédric Le Goater 
68016020011SCédric Le Goater     s->intr_status = 0;
68116020011SCédric Le Goater 
682f7da1aa8SCédric Le Goater     for (i = 0; i < aic->num_busses; i++) {
68316020011SCédric Le Goater         s->busses[i].intr_ctrl = 0;
68416020011SCédric Le Goater         s->busses[i].intr_status = 0;
68516020011SCédric Le Goater         s->busses[i].cmd = 0;
68616020011SCédric Le Goater         s->busses[i].buf = 0;
687*545d6befSCédric Le Goater         s->busses[i].dma_addr = 0;
688*545d6befSCédric Le Goater         s->busses[i].dma_len = 0;
68916020011SCédric Le Goater         i2c_end_transfer(s->busses[i].bus);
69016020011SCédric Le Goater     }
69116020011SCédric Le Goater }
69216020011SCédric Le Goater 
69316020011SCédric Le Goater /*
694f7da1aa8SCédric Le Goater  * Address Definitions (AST2400 and AST2500)
69516020011SCédric Le Goater  *
69616020011SCédric Le Goater  *   0x000 ... 0x03F: Global Register
69716020011SCédric Le Goater  *   0x040 ... 0x07F: Device 1
69816020011SCédric Le Goater  *   0x080 ... 0x0BF: Device 2
69916020011SCédric Le Goater  *   0x0C0 ... 0x0FF: Device 3
70016020011SCédric Le Goater  *   0x100 ... 0x13F: Device 4
70116020011SCédric Le Goater  *   0x140 ... 0x17F: Device 5
70216020011SCédric Le Goater  *   0x180 ... 0x1BF: Device 6
70316020011SCédric Le Goater  *   0x1C0 ... 0x1FF: Device 7
70416020011SCédric Le Goater  *   0x200 ... 0x2FF: Buffer Pool  (unused in linux driver)
70516020011SCédric Le Goater  *   0x300 ... 0x33F: Device 8
70616020011SCédric Le Goater  *   0x340 ... 0x37F: Device 9
70716020011SCédric Le Goater  *   0x380 ... 0x3BF: Device 10
70816020011SCédric Le Goater  *   0x3C0 ... 0x3FF: Device 11
70916020011SCédric Le Goater  *   0x400 ... 0x43F: Device 12
71016020011SCédric Le Goater  *   0x440 ... 0x47F: Device 13
71116020011SCédric Le Goater  *   0x480 ... 0x4BF: Device 14
71216020011SCédric Le Goater  *   0x800 ... 0xFFF: Buffer Pool  (unused in linux driver)
71316020011SCédric Le Goater  */
71416020011SCédric Le Goater static void aspeed_i2c_realize(DeviceState *dev, Error **errp)
71516020011SCédric Le Goater {
71616020011SCédric Le Goater     int i;
71716020011SCédric Le Goater     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
71816020011SCédric Le Goater     AspeedI2CState *s = ASPEED_I2C(dev);
719f7da1aa8SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
72016020011SCédric Le Goater 
72116020011SCédric Le Goater     sysbus_init_irq(sbd, &s->irq);
72216020011SCédric Le Goater     memory_region_init_io(&s->iomem, OBJECT(s), &aspeed_i2c_ctrl_ops, s,
72316020011SCédric Le Goater                           "aspeed.i2c", 0x1000);
72416020011SCédric Le Goater     sysbus_init_mmio(sbd, &s->iomem);
72516020011SCédric Le Goater 
726f7da1aa8SCédric Le Goater     for (i = 0; i < aic->num_busses; i++) {
727f7da1aa8SCédric Le Goater         char name[32];
728f7da1aa8SCédric Le Goater         int offset = i < aic->gap ? 1 : 5;
72951dd4923SCédric Le Goater 
73051dd4923SCédric Le Goater         sysbus_init_irq(sbd, &s->busses[i].irq);
73116020011SCédric Le Goater         snprintf(name, sizeof(name), "aspeed.i2c.%d", i);
73216020011SCédric Le Goater         s->busses[i].controller = s;
73316020011SCédric Le Goater         s->busses[i].id = i;
73416020011SCédric Le Goater         s->busses[i].bus = i2c_init_bus(dev, name);
73516020011SCédric Le Goater         memory_region_init_io(&s->busses[i].mr, OBJECT(dev),
736f7da1aa8SCédric Le Goater                               &aspeed_i2c_bus_ops, &s->busses[i], name,
737f7da1aa8SCédric Le Goater                               aic->reg_size);
738f7da1aa8SCédric Le Goater         memory_region_add_subregion(&s->iomem, aic->reg_size * (i + offset),
73916020011SCédric Le Goater                                     &s->busses[i].mr);
74016020011SCédric Le Goater     }
7416054fc73SCédric Le Goater 
7426054fc73SCédric Le Goater     memory_region_init_io(&s->pool_iomem, OBJECT(s), &aspeed_i2c_pool_ops, s,
7436054fc73SCédric Le Goater                           "aspeed.i2c-pool", aic->pool_size);
7446054fc73SCédric Le Goater     memory_region_add_subregion(&s->iomem, aic->pool_base, &s->pool_iomem);
745*545d6befSCédric Le Goater 
746*545d6befSCédric Le Goater     if (aic->has_dma) {
747*545d6befSCédric Le Goater         if (!s->dram_mr) {
748*545d6befSCédric Le Goater             error_setg(errp, TYPE_ASPEED_I2C ": 'dram' link not set");
749*545d6befSCédric Le Goater             return;
75016020011SCédric Le Goater         }
75116020011SCédric Le Goater 
752*545d6befSCédric Le Goater         address_space_init(&s->dram_as, s->dram_mr, "dma-dram");
753*545d6befSCédric Le Goater     }
754*545d6befSCédric Le Goater }
755*545d6befSCédric Le Goater 
756*545d6befSCédric Le Goater static Property aspeed_i2c_properties[] = {
757*545d6befSCédric Le Goater     DEFINE_PROP_LINK("dram", AspeedI2CState, dram_mr,
758*545d6befSCédric Le Goater                      TYPE_MEMORY_REGION, MemoryRegion *),
759*545d6befSCédric Le Goater     DEFINE_PROP_END_OF_LIST(),
760*545d6befSCédric Le Goater };
761*545d6befSCédric Le Goater 
76216020011SCédric Le Goater static void aspeed_i2c_class_init(ObjectClass *klass, void *data)
76316020011SCédric Le Goater {
76416020011SCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
76516020011SCédric Le Goater 
76616020011SCédric Le Goater     dc->vmsd = &aspeed_i2c_vmstate;
76716020011SCédric Le Goater     dc->reset = aspeed_i2c_reset;
768*545d6befSCédric Le Goater     dc->props = aspeed_i2c_properties;
76916020011SCédric Le Goater     dc->realize = aspeed_i2c_realize;
77016020011SCédric Le Goater     dc->desc = "Aspeed I2C Controller";
77116020011SCédric Le Goater }
77216020011SCédric Le Goater 
77316020011SCédric Le Goater static const TypeInfo aspeed_i2c_info = {
77416020011SCédric Le Goater     .name          = TYPE_ASPEED_I2C,
77516020011SCédric Le Goater     .parent        = TYPE_SYS_BUS_DEVICE,
77616020011SCédric Le Goater     .instance_size = sizeof(AspeedI2CState),
77716020011SCédric Le Goater     .class_init    = aspeed_i2c_class_init,
778f7da1aa8SCédric Le Goater     .class_size = sizeof(AspeedI2CClass),
779f7da1aa8SCédric Le Goater     .abstract   = true,
780f7da1aa8SCédric Le Goater };
781f7da1aa8SCédric Le Goater 
78251dd4923SCédric Le Goater static qemu_irq aspeed_2400_i2c_bus_get_irq(AspeedI2CBus *bus)
78351dd4923SCédric Le Goater {
78451dd4923SCédric Le Goater     return bus->controller->irq;
78551dd4923SCédric Le Goater }
78651dd4923SCédric Le Goater 
7876054fc73SCédric Le Goater static uint8_t *aspeed_2400_i2c_bus_pool_base(AspeedI2CBus *bus)
7886054fc73SCédric Le Goater {
7896054fc73SCédric Le Goater     uint8_t *pool_page =
7906054fc73SCédric Le Goater         &bus->controller->pool[I2CD_POOL_PAGE_SEL(bus->ctrl) * 0x100];
7916054fc73SCédric Le Goater 
7926054fc73SCédric Le Goater     return &pool_page[I2CD_POOL_OFFSET(bus->pool_ctrl)];
7936054fc73SCédric Le Goater }
7946054fc73SCédric Le Goater 
795f7da1aa8SCédric Le Goater static void aspeed_2400_i2c_class_init(ObjectClass *klass, void *data)
796f7da1aa8SCédric Le Goater {
797f7da1aa8SCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
798f7da1aa8SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_CLASS(klass);
799f7da1aa8SCédric Le Goater 
800f7da1aa8SCédric Le Goater     dc->desc = "ASPEED 2400 I2C Controller";
801f7da1aa8SCédric Le Goater 
802f7da1aa8SCédric Le Goater     aic->num_busses = 14;
803f7da1aa8SCédric Le Goater     aic->reg_size = 0x40;
804f7da1aa8SCédric Le Goater     aic->gap = 7;
80551dd4923SCédric Le Goater     aic->bus_get_irq = aspeed_2400_i2c_bus_get_irq;
8066054fc73SCédric Le Goater     aic->pool_size = 0x800;
8076054fc73SCédric Le Goater     aic->pool_base = 0x800;
8086054fc73SCédric Le Goater     aic->bus_pool_base = aspeed_2400_i2c_bus_pool_base;
809f7da1aa8SCédric Le Goater }
810f7da1aa8SCédric Le Goater 
811f7da1aa8SCédric Le Goater static const TypeInfo aspeed_2400_i2c_info = {
812f7da1aa8SCédric Le Goater     .name = TYPE_ASPEED_2400_I2C,
813f7da1aa8SCédric Le Goater     .parent = TYPE_ASPEED_I2C,
814f7da1aa8SCédric Le Goater     .class_init = aspeed_2400_i2c_class_init,
815f7da1aa8SCédric Le Goater };
816f7da1aa8SCédric Le Goater 
81751dd4923SCédric Le Goater static qemu_irq aspeed_2500_i2c_bus_get_irq(AspeedI2CBus *bus)
81851dd4923SCédric Le Goater {
81951dd4923SCédric Le Goater     return bus->controller->irq;
82051dd4923SCédric Le Goater }
82151dd4923SCédric Le Goater 
8226054fc73SCédric Le Goater static uint8_t *aspeed_2500_i2c_bus_pool_base(AspeedI2CBus *bus)
8236054fc73SCédric Le Goater {
8246054fc73SCédric Le Goater     return &bus->controller->pool[bus->id * 0x10];
8256054fc73SCédric Le Goater }
8266054fc73SCédric Le Goater 
827f7da1aa8SCédric Le Goater static void aspeed_2500_i2c_class_init(ObjectClass *klass, void *data)
828f7da1aa8SCédric Le Goater {
829f7da1aa8SCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
830f7da1aa8SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_CLASS(klass);
831f7da1aa8SCédric Le Goater 
832f7da1aa8SCédric Le Goater     dc->desc = "ASPEED 2500 I2C Controller";
833f7da1aa8SCédric Le Goater 
834f7da1aa8SCédric Le Goater     aic->num_busses = 14;
835f7da1aa8SCédric Le Goater     aic->reg_size = 0x40;
836f7da1aa8SCédric Le Goater     aic->gap = 7;
83751dd4923SCédric Le Goater     aic->bus_get_irq = aspeed_2500_i2c_bus_get_irq;
8386054fc73SCédric Le Goater     aic->pool_size = 0x100;
8396054fc73SCédric Le Goater     aic->pool_base = 0x200;
8406054fc73SCédric Le Goater     aic->bus_pool_base = aspeed_2500_i2c_bus_pool_base;
841aab90b1cSCédric Le Goater     aic->check_sram = true;
842*545d6befSCédric Le Goater     aic->has_dma = true;
843f7da1aa8SCédric Le Goater }
844f7da1aa8SCédric Le Goater 
845f7da1aa8SCédric Le Goater static const TypeInfo aspeed_2500_i2c_info = {
846f7da1aa8SCédric Le Goater     .name = TYPE_ASPEED_2500_I2C,
847f7da1aa8SCédric Le Goater     .parent = TYPE_ASPEED_I2C,
848f7da1aa8SCédric Le Goater     .class_init = aspeed_2500_i2c_class_init,
84916020011SCédric Le Goater };
85016020011SCédric Le Goater 
85151dd4923SCédric Le Goater static qemu_irq aspeed_2600_i2c_bus_get_irq(AspeedI2CBus *bus)
85251dd4923SCédric Le Goater {
85351dd4923SCédric Le Goater     return bus->irq;
85451dd4923SCédric Le Goater }
85551dd4923SCédric Le Goater 
8566054fc73SCédric Le Goater static uint8_t *aspeed_2600_i2c_bus_pool_base(AspeedI2CBus *bus)
8576054fc73SCédric Le Goater {
8586054fc73SCédric Le Goater    return &bus->controller->pool[bus->id * 0x20];
8596054fc73SCédric Le Goater }
8606054fc73SCédric Le Goater 
86151dd4923SCédric Le Goater static void aspeed_2600_i2c_class_init(ObjectClass *klass, void *data)
86251dd4923SCédric Le Goater {
86351dd4923SCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
86451dd4923SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_CLASS(klass);
86551dd4923SCédric Le Goater 
86651dd4923SCédric Le Goater     dc->desc = "ASPEED 2600 I2C Controller";
86751dd4923SCédric Le Goater 
86851dd4923SCédric Le Goater     aic->num_busses = 16;
86951dd4923SCédric Le Goater     aic->reg_size = 0x80;
87051dd4923SCédric Le Goater     aic->gap = -1; /* no gap */
87151dd4923SCédric Le Goater     aic->bus_get_irq = aspeed_2600_i2c_bus_get_irq;
8726054fc73SCédric Le Goater     aic->pool_size = 0x200;
8736054fc73SCédric Le Goater     aic->pool_base = 0xC00;
8746054fc73SCédric Le Goater     aic->bus_pool_base = aspeed_2600_i2c_bus_pool_base;
875*545d6befSCédric Le Goater     aic->has_dma = true;
87651dd4923SCédric Le Goater }
87751dd4923SCédric Le Goater 
87851dd4923SCédric Le Goater static const TypeInfo aspeed_2600_i2c_info = {
87951dd4923SCédric Le Goater     .name = TYPE_ASPEED_2600_I2C,
88051dd4923SCédric Le Goater     .parent = TYPE_ASPEED_I2C,
88151dd4923SCédric Le Goater     .class_init = aspeed_2600_i2c_class_init,
88251dd4923SCédric Le Goater };
88351dd4923SCédric Le Goater 
88416020011SCédric Le Goater static void aspeed_i2c_register_types(void)
88516020011SCédric Le Goater {
88616020011SCédric Le Goater     type_register_static(&aspeed_i2c_info);
887f7da1aa8SCédric Le Goater     type_register_static(&aspeed_2400_i2c_info);
888f7da1aa8SCédric Le Goater     type_register_static(&aspeed_2500_i2c_info);
88951dd4923SCédric Le Goater     type_register_static(&aspeed_2600_i2c_info);
89016020011SCédric Le Goater }
89116020011SCédric Le Goater 
89216020011SCédric Le Goater type_init(aspeed_i2c_register_types)
89316020011SCédric Le Goater 
89416020011SCédric Le Goater 
89516020011SCédric Le Goater I2CBus *aspeed_i2c_get_bus(DeviceState *dev, int busnr)
89616020011SCédric Le Goater {
89716020011SCédric Le Goater     AspeedI2CState *s = ASPEED_I2C(dev);
898f7da1aa8SCédric Le Goater     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
89916020011SCédric Le Goater     I2CBus *bus = NULL;
90016020011SCédric Le Goater 
901f7da1aa8SCédric Le Goater     if (busnr >= 0 && busnr < aic->num_busses) {
90216020011SCédric Le Goater         bus = s->busses[busnr].bus;
90316020011SCédric Le Goater     }
90416020011SCédric Le Goater 
90516020011SCédric Le Goater     return bus;
90616020011SCédric Le Goater }
907