xref: /qemu/hw/i2c/aspeed_i2c.c (revision 4960f084cfb4d42bb8995dd23ccad168c3e4ad3a)
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"
2316020011SCédric Le Goater #include "qemu/log.h"
2416020011SCédric Le Goater #include "hw/i2c/aspeed_i2c.h"
2516020011SCédric Le Goater 
2616020011SCédric Le Goater /* I2C Global Register */
2716020011SCédric Le Goater 
2816020011SCédric Le Goater #define I2C_CTRL_STATUS         0x00        /* Device Interrupt Status */
2916020011SCédric Le Goater #define I2C_CTRL_ASSIGN         0x08        /* Device Interrupt Target
3016020011SCédric Le Goater                                                Assignment */
3116020011SCédric Le Goater 
3216020011SCédric Le Goater /* I2C Device (Bus) Register */
3316020011SCédric Le Goater 
3416020011SCédric Le Goater #define I2CD_FUN_CTRL_REG       0x00       /* I2CD Function Control  */
3516020011SCédric Le Goater #define   I2CD_BUFF_SEL_MASK               (0x7 << 20)
3616020011SCédric Le Goater #define   I2CD_BUFF_SEL(x)                 (x << 20)
3716020011SCédric Le Goater #define   I2CD_M_SDA_LOCK_EN               (0x1 << 16)
3816020011SCédric Le Goater #define   I2CD_MULTI_MASTER_DIS            (0x1 << 15)
3916020011SCédric Le Goater #define   I2CD_M_SCL_DRIVE_EN              (0x1 << 14)
4016020011SCédric Le Goater #define   I2CD_MSB_STS                     (0x1 << 9)
4116020011SCédric Le Goater #define   I2CD_SDA_DRIVE_1T_EN             (0x1 << 8)
4216020011SCédric Le Goater #define   I2CD_M_SDA_DRIVE_1T_EN           (0x1 << 7)
4316020011SCédric Le Goater #define   I2CD_M_HIGH_SPEED_EN             (0x1 << 6)
4416020011SCédric Le Goater #define   I2CD_DEF_ADDR_EN                 (0x1 << 5)
4516020011SCédric Le Goater #define   I2CD_DEF_ALERT_EN                (0x1 << 4)
4616020011SCédric Le Goater #define   I2CD_DEF_ARP_EN                  (0x1 << 3)
4716020011SCédric Le Goater #define   I2CD_DEF_GCALL_EN                (0x1 << 2)
4816020011SCédric Le Goater #define   I2CD_SLAVE_EN                    (0x1 << 1)
4916020011SCédric Le Goater #define   I2CD_MASTER_EN                   (0x1)
5016020011SCédric Le Goater 
5116020011SCédric Le Goater #define I2CD_AC_TIMING_REG1     0x04       /* Clock and AC Timing Control #1 */
5216020011SCédric Le Goater #define I2CD_AC_TIMING_REG2     0x08       /* Clock and AC Timing Control #1 */
5316020011SCédric Le Goater #define I2CD_INTR_CTRL_REG      0x0c       /* I2CD Interrupt Control */
5416020011SCédric Le Goater #define I2CD_INTR_STS_REG       0x10       /* I2CD Interrupt Status */
5516020011SCédric Le Goater #define   I2CD_INTR_SDA_DL_TIMEOUT         (0x1 << 14)
5616020011SCédric Le Goater #define   I2CD_INTR_BUS_RECOVER_DONE       (0x1 << 13)
5716020011SCédric Le Goater #define   I2CD_INTR_SMBUS_ALERT            (0x1 << 12) /* Bus [0-3] only */
5816020011SCédric Le Goater #define   I2CD_INTR_SMBUS_ARP_ADDR         (0x1 << 11) /* Removed */
5916020011SCédric Le Goater #define   I2CD_INTR_SMBUS_DEV_ALERT_ADDR   (0x1 << 10) /* Removed */
6016020011SCédric Le Goater #define   I2CD_INTR_SMBUS_DEF_ADDR         (0x1 << 9)  /* Removed */
6116020011SCédric Le Goater #define   I2CD_INTR_GCALL_ADDR             (0x1 << 8)  /* Removed */
6216020011SCédric Le Goater #define   I2CD_INTR_SLAVE_MATCH            (0x1 << 7)  /* use RX_DONE */
6316020011SCédric Le Goater #define   I2CD_INTR_SCL_TIMEOUT            (0x1 << 6)
6416020011SCédric Le Goater #define   I2CD_INTR_ABNORMAL               (0x1 << 5)
6516020011SCédric Le Goater #define   I2CD_INTR_NORMAL_STOP            (0x1 << 4)
6616020011SCédric Le Goater #define   I2CD_INTR_ARBIT_LOSS             (0x1 << 3)
6716020011SCédric Le Goater #define   I2CD_INTR_RX_DONE                (0x1 << 2)
6816020011SCédric Le Goater #define   I2CD_INTR_TX_NAK                 (0x1 << 1)
6916020011SCédric Le Goater #define   I2CD_INTR_TX_ACK                 (0x1 << 0)
7016020011SCédric Le Goater 
7116020011SCédric Le Goater #define I2CD_CMD_REG            0x14       /* I2CD Command/Status */
7216020011SCédric Le Goater #define   I2CD_SDA_OE                      (0x1 << 28)
7316020011SCédric Le Goater #define   I2CD_SDA_O                       (0x1 << 27)
7416020011SCédric Le Goater #define   I2CD_SCL_OE                      (0x1 << 26)
7516020011SCédric Le Goater #define   I2CD_SCL_O                       (0x1 << 25)
7616020011SCédric Le Goater #define   I2CD_TX_TIMING                   (0x1 << 24)
7716020011SCédric Le Goater #define   I2CD_TX_STATUS                   (0x1 << 23)
7816020011SCédric Le Goater 
7916020011SCédric Le Goater #define   I2CD_TX_STATE_SHIFT              19 /* Tx State Machine */
8016020011SCédric Le Goater #define   I2CD_TX_STATE_MASK                  0xf
8116020011SCédric Le Goater #define     I2CD_IDLE                         0x0
8216020011SCédric Le Goater #define     I2CD_MACTIVE                      0x8
8316020011SCédric Le Goater #define     I2CD_MSTART                       0x9
8416020011SCédric Le Goater #define     I2CD_MSTARTR                      0xa
8516020011SCédric Le Goater #define     I2CD_MSTOP                        0xb
8616020011SCédric Le Goater #define     I2CD_MTXD                         0xc
8716020011SCédric Le Goater #define     I2CD_MRXACK                       0xd
8816020011SCédric Le Goater #define     I2CD_MRXD                         0xe
8916020011SCédric Le Goater #define     I2CD_MTXACK                       0xf
9016020011SCédric Le Goater #define     I2CD_SWAIT                        0x1
9116020011SCédric Le Goater #define     I2CD_SRXD                         0x4
9216020011SCédric Le Goater #define     I2CD_STXACK                       0x5
9316020011SCédric Le Goater #define     I2CD_STXD                         0x6
9416020011SCédric Le Goater #define     I2CD_SRXACK                       0x7
9516020011SCédric Le Goater #define     I2CD_RECOVER                      0x3
9616020011SCédric Le Goater 
9716020011SCédric Le Goater #define   I2CD_SCL_LINE_STS                (0x1 << 18)
9816020011SCédric Le Goater #define   I2CD_SDA_LINE_STS                (0x1 << 17)
9916020011SCédric Le Goater #define   I2CD_BUS_BUSY_STS                (0x1 << 16)
10016020011SCédric Le Goater #define   I2CD_SDA_OE_OUT_DIR              (0x1 << 15)
10116020011SCédric Le Goater #define   I2CD_SDA_O_OUT_DIR               (0x1 << 14)
10216020011SCédric Le Goater #define   I2CD_SCL_OE_OUT_DIR              (0x1 << 13)
10316020011SCédric Le Goater #define   I2CD_SCL_O_OUT_DIR               (0x1 << 12)
10416020011SCédric Le Goater #define   I2CD_BUS_RECOVER_CMD_EN          (0x1 << 11)
10516020011SCédric Le Goater #define   I2CD_S_ALT_EN                    (0x1 << 10)
10616020011SCédric Le Goater #define   I2CD_RX_DMA_ENABLE               (0x1 << 9)
10716020011SCédric Le Goater #define   I2CD_TX_DMA_ENABLE               (0x1 << 8)
10816020011SCédric Le Goater 
10916020011SCédric Le Goater /* Command Bit */
11016020011SCédric Le Goater #define   I2CD_M_STOP_CMD                  (0x1 << 5)
11116020011SCédric Le Goater #define   I2CD_M_S_RX_CMD_LAST             (0x1 << 4)
11216020011SCédric Le Goater #define   I2CD_M_RX_CMD                    (0x1 << 3)
11316020011SCédric Le Goater #define   I2CD_S_TX_CMD                    (0x1 << 2)
11416020011SCédric Le Goater #define   I2CD_M_TX_CMD                    (0x1 << 1)
11516020011SCédric Le Goater #define   I2CD_M_START_CMD                 (0x1)
11616020011SCédric Le Goater 
11716020011SCédric Le Goater #define I2CD_DEV_ADDR_REG       0x18       /* Slave Device Address */
11816020011SCédric Le Goater #define I2CD_BUF_CTRL_REG       0x1c       /* Pool Buffer Control */
11916020011SCédric Le Goater #define I2CD_BYTE_BUF_REG       0x20       /* Transmit/Receive Byte Buffer */
12016020011SCédric Le Goater #define   I2CD_BYTE_BUF_TX_SHIFT           0
12116020011SCédric Le Goater #define   I2CD_BYTE_BUF_TX_MASK            0xff
12216020011SCédric Le Goater #define   I2CD_BYTE_BUF_RX_SHIFT           8
12316020011SCédric Le Goater #define   I2CD_BYTE_BUF_RX_MASK            0xff
12416020011SCédric Le Goater 
12516020011SCédric Le Goater 
12616020011SCédric Le Goater static inline bool aspeed_i2c_bus_is_master(AspeedI2CBus *bus)
12716020011SCédric Le Goater {
12816020011SCédric Le Goater     return bus->ctrl & I2CD_MASTER_EN;
12916020011SCédric Le Goater }
13016020011SCédric Le Goater 
13116020011SCédric Le Goater static inline bool aspeed_i2c_bus_is_enabled(AspeedI2CBus *bus)
13216020011SCédric Le Goater {
13316020011SCédric Le Goater     return bus->ctrl & (I2CD_MASTER_EN | I2CD_SLAVE_EN);
13416020011SCédric Le Goater }
13516020011SCédric Le Goater 
13616020011SCédric Le Goater static inline void aspeed_i2c_bus_raise_interrupt(AspeedI2CBus *bus)
13716020011SCédric Le Goater {
13816020011SCédric Le Goater     bus->intr_status &= bus->intr_ctrl;
13916020011SCédric Le Goater     if (bus->intr_status) {
14016020011SCédric Le Goater         bus->controller->intr_status |= 1 << bus->id;
14116020011SCédric Le Goater         qemu_irq_raise(bus->controller->irq);
14216020011SCédric Le Goater     }
14316020011SCédric Le Goater }
14416020011SCédric Le Goater 
14516020011SCédric Le Goater static uint64_t aspeed_i2c_bus_read(void *opaque, hwaddr offset,
14616020011SCédric Le Goater                                     unsigned size)
14716020011SCédric Le Goater {
14816020011SCédric Le Goater     AspeedI2CBus *bus = opaque;
14916020011SCédric Le Goater 
15016020011SCédric Le Goater     switch (offset) {
15116020011SCédric Le Goater     case I2CD_FUN_CTRL_REG:
15216020011SCédric Le Goater         return bus->ctrl;
15316020011SCédric Le Goater     case I2CD_AC_TIMING_REG1:
15416020011SCédric Le Goater         return bus->timing[0];
15516020011SCédric Le Goater     case I2CD_AC_TIMING_REG2:
15616020011SCédric Le Goater         return bus->timing[1];
15716020011SCédric Le Goater     case I2CD_INTR_CTRL_REG:
15816020011SCédric Le Goater         return bus->intr_ctrl;
15916020011SCédric Le Goater     case I2CD_INTR_STS_REG:
16016020011SCédric Le Goater         return bus->intr_status;
16116020011SCédric Le Goater     case I2CD_BYTE_BUF_REG:
16216020011SCédric Le Goater         return bus->buf;
16316020011SCédric Le Goater     case I2CD_CMD_REG:
16416020011SCédric Le Goater         return bus->cmd | (i2c_bus_busy(bus->bus) << 16);
16516020011SCédric Le Goater     default:
16616020011SCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR,
16716020011SCédric Le Goater                       "%s: Bad offset 0x%" HWADDR_PRIx "\n", __func__, offset);
16816020011SCédric Le Goater         return -1;
16916020011SCédric Le Goater     }
17016020011SCédric Le Goater }
17116020011SCédric Le Goater 
172*4960f084SCédric Le Goater static void aspeed_i2c_set_state(AspeedI2CBus *bus, uint8_t state)
173*4960f084SCédric Le Goater {
174*4960f084SCédric Le Goater     bus->cmd &= ~(I2CD_TX_STATE_MASK << I2CD_TX_STATE_SHIFT);
175*4960f084SCédric Le Goater     bus->cmd |= (state & I2CD_TX_STATE_MASK) << I2CD_TX_STATE_SHIFT;
176*4960f084SCédric Le Goater }
177*4960f084SCédric Le Goater 
178*4960f084SCédric Le Goater static uint8_t aspeed_i2c_get_state(AspeedI2CBus *bus)
179*4960f084SCédric Le Goater {
180*4960f084SCédric Le Goater     return (bus->cmd >> I2CD_TX_STATE_SHIFT) & I2CD_TX_STATE_MASK;
181*4960f084SCédric Le Goater }
182*4960f084SCédric Le Goater 
183*4960f084SCédric Le Goater /*
184*4960f084SCédric Le Goater  * The state machine needs some refinement. It is only used to track
185*4960f084SCédric Le Goater  * invalid STOP commands for the moment.
186*4960f084SCédric Le Goater  */
18716020011SCédric Le Goater static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value)
18816020011SCédric Le Goater {
189ddabca75SCédric Le Goater     bus->cmd &= ~0xFFFF;
19016020011SCédric Le Goater     bus->cmd |= value & 0xFFFF;
19116020011SCédric Le Goater     bus->intr_status = 0;
19216020011SCédric Le Goater 
19316020011SCédric Le Goater     if (bus->cmd & I2CD_M_START_CMD) {
194*4960f084SCédric Le Goater         uint8_t state = aspeed_i2c_get_state(bus) & I2CD_MACTIVE ?
195*4960f084SCédric Le Goater             I2CD_MSTARTR : I2CD_MSTART;
196*4960f084SCédric Le Goater 
197*4960f084SCédric Le Goater         aspeed_i2c_set_state(bus, state);
198*4960f084SCédric Le Goater 
19916020011SCédric Le Goater         if (i2c_start_transfer(bus->bus, extract32(bus->buf, 1, 7),
20016020011SCédric Le Goater                                extract32(bus->buf, 0, 1))) {
20116020011SCédric Le Goater             bus->intr_status |= I2CD_INTR_TX_NAK;
20216020011SCédric Le Goater         } else {
20316020011SCédric Le Goater             bus->intr_status |= I2CD_INTR_TX_ACK;
20416020011SCédric Le Goater         }
20516020011SCédric Le Goater 
206ddabca75SCédric Le Goater         /* START command is also a TX command, as the slave address is
207ddabca75SCédric Le Goater          * sent on the bus */
208ddabca75SCédric Le Goater         bus->cmd &= ~(I2CD_M_START_CMD | I2CD_M_TX_CMD);
209ddabca75SCédric Le Goater 
210ddabca75SCédric Le Goater         /* No slave found */
211ddabca75SCédric Le Goater         if (!i2c_bus_busy(bus->bus)) {
212ddabca75SCédric Le Goater             return;
213ddabca75SCédric Le Goater         }
214*4960f084SCédric Le Goater         aspeed_i2c_set_state(bus, I2CD_MACTIVE);
215ddabca75SCédric Le Goater     }
216ddabca75SCédric Le Goater 
217ddabca75SCédric Le Goater     if (bus->cmd & I2CD_M_TX_CMD) {
218*4960f084SCédric Le Goater         aspeed_i2c_set_state(bus, I2CD_MTXD);
21916020011SCédric Le Goater         if (i2c_send(bus->bus, bus->buf)) {
220*4960f084SCédric Le Goater             bus->intr_status |= (I2CD_INTR_TX_NAK);
22116020011SCédric Le Goater             i2c_end_transfer(bus->bus);
22216020011SCédric Le Goater         } else {
22316020011SCédric Le Goater             bus->intr_status |= I2CD_INTR_TX_ACK;
22416020011SCédric Le Goater         }
225ddabca75SCédric Le Goater         bus->cmd &= ~I2CD_M_TX_CMD;
226*4960f084SCédric Le Goater         aspeed_i2c_set_state(bus, I2CD_MACTIVE);
227ddabca75SCédric Le Goater     }
22816020011SCédric Le Goater 
229d0efdc16SCédric Le Goater     if (bus->cmd & (I2CD_M_RX_CMD | I2CD_M_S_RX_CMD_LAST)) {
230*4960f084SCédric Le Goater         int ret;
231*4960f084SCédric Le Goater 
232*4960f084SCédric Le Goater         aspeed_i2c_set_state(bus, I2CD_MRXD);
233*4960f084SCédric Le Goater         ret = i2c_recv(bus->bus);
23416020011SCédric Le Goater         if (ret < 0) {
23516020011SCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "%s: read failed\n", __func__);
23616020011SCédric Le Goater             ret = 0xff;
23716020011SCédric Le Goater         } else {
23816020011SCédric Le Goater             bus->intr_status |= I2CD_INTR_RX_DONE;
23916020011SCédric Le Goater         }
24016020011SCédric Le Goater         bus->buf = (ret & I2CD_BYTE_BUF_RX_MASK) << I2CD_BYTE_BUF_RX_SHIFT;
241d0efdc16SCédric Le Goater         if (bus->cmd & I2CD_M_S_RX_CMD_LAST) {
242d0efdc16SCédric Le Goater             i2c_nack(bus->bus);
243d0efdc16SCédric Le Goater         }
244d0efdc16SCédric Le Goater         bus->cmd &= ~(I2CD_M_RX_CMD | I2CD_M_S_RX_CMD_LAST);
245*4960f084SCédric Le Goater         aspeed_i2c_set_state(bus, I2CD_MACTIVE);
24616020011SCédric Le Goater     }
24716020011SCédric Le Goater 
248d0efdc16SCédric Le Goater     if (bus->cmd & I2CD_M_STOP_CMD) {
249*4960f084SCédric Le Goater         if (!(aspeed_i2c_get_state(bus) & I2CD_MACTIVE)) {
250*4960f084SCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "%s: abnormal stop\n", __func__);
25116020011SCédric Le Goater             bus->intr_status |= I2CD_INTR_ABNORMAL;
25216020011SCédric Le Goater         } else {
253*4960f084SCédric Le Goater             aspeed_i2c_set_state(bus, I2CD_MSTOP);
25416020011SCédric Le Goater             i2c_end_transfer(bus->bus);
25516020011SCédric Le Goater             bus->intr_status |= I2CD_INTR_NORMAL_STOP;
25616020011SCédric Le Goater         }
257ddabca75SCédric Le Goater         bus->cmd &= ~I2CD_M_STOP_CMD;
258*4960f084SCédric Le Goater         aspeed_i2c_set_state(bus, I2CD_IDLE);
25916020011SCédric Le Goater     }
26016020011SCédric Le Goater }
26116020011SCédric Le Goater 
26216020011SCédric Le Goater static void aspeed_i2c_bus_write(void *opaque, hwaddr offset,
26316020011SCédric Le Goater                                  uint64_t value, unsigned size)
26416020011SCédric Le Goater {
26516020011SCédric Le Goater     AspeedI2CBus *bus = opaque;
26616020011SCédric Le Goater 
26716020011SCédric Le Goater     switch (offset) {
26816020011SCédric Le Goater     case I2CD_FUN_CTRL_REG:
26916020011SCédric Le Goater         if (value & I2CD_SLAVE_EN) {
27016020011SCédric Le Goater             qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n",
27116020011SCédric Le Goater                           __func__);
27216020011SCédric Le Goater             break;
27316020011SCédric Le Goater         }
27416020011SCédric Le Goater         bus->ctrl = value & 0x0071C3FF;
27516020011SCédric Le Goater         break;
27616020011SCédric Le Goater     case I2CD_AC_TIMING_REG1:
27716020011SCédric Le Goater         bus->timing[0] = value & 0xFFFFF0F;
27816020011SCédric Le Goater         break;
27916020011SCédric Le Goater     case I2CD_AC_TIMING_REG2:
28016020011SCédric Le Goater         bus->timing[1] = value & 0x7;
28116020011SCédric Le Goater         break;
28216020011SCédric Le Goater     case I2CD_INTR_CTRL_REG:
28316020011SCédric Le Goater         bus->intr_ctrl = value & 0x7FFF;
28416020011SCédric Le Goater         break;
28516020011SCédric Le Goater     case I2CD_INTR_STS_REG:
28616020011SCédric Le Goater         bus->intr_status &= ~(value & 0x7FFF);
28716020011SCédric Le Goater         bus->controller->intr_status &= ~(1 << bus->id);
28816020011SCédric Le Goater         qemu_irq_lower(bus->controller->irq);
28916020011SCédric Le Goater         break;
29016020011SCédric Le Goater     case I2CD_DEV_ADDR_REG:
29116020011SCédric Le Goater         qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n",
29216020011SCédric Le Goater                       __func__);
29316020011SCédric Le Goater         break;
29416020011SCédric Le Goater     case I2CD_BYTE_BUF_REG:
29516020011SCédric Le Goater         bus->buf = (value & I2CD_BYTE_BUF_TX_MASK) << I2CD_BYTE_BUF_TX_SHIFT;
29616020011SCédric Le Goater         break;
29716020011SCédric Le Goater     case I2CD_CMD_REG:
29816020011SCédric Le Goater         if (!aspeed_i2c_bus_is_enabled(bus)) {
29916020011SCédric Le Goater             break;
30016020011SCédric Le Goater         }
30116020011SCédric Le Goater 
30216020011SCédric Le Goater         if (!aspeed_i2c_bus_is_master(bus)) {
30316020011SCédric Le Goater             qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n",
30416020011SCédric Le Goater                           __func__);
30516020011SCédric Le Goater             break;
30616020011SCédric Le Goater         }
30716020011SCédric Le Goater 
30816020011SCédric Le Goater         aspeed_i2c_bus_handle_cmd(bus, value);
309ddabca75SCédric Le Goater         aspeed_i2c_bus_raise_interrupt(bus);
31016020011SCédric Le Goater         break;
31116020011SCédric Le Goater 
31216020011SCédric Le Goater     default:
31316020011SCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
31416020011SCédric Le Goater                       __func__, offset);
31516020011SCédric Le Goater     }
31616020011SCédric Le Goater }
31716020011SCédric Le Goater 
31816020011SCédric Le Goater static uint64_t aspeed_i2c_ctrl_read(void *opaque, hwaddr offset,
31916020011SCédric Le Goater                                    unsigned size)
32016020011SCédric Le Goater {
32116020011SCédric Le Goater     AspeedI2CState *s = opaque;
32216020011SCédric Le Goater 
32316020011SCédric Le Goater     switch (offset) {
32416020011SCédric Le Goater     case I2C_CTRL_STATUS:
32516020011SCédric Le Goater         return s->intr_status;
32616020011SCédric Le Goater     default:
32716020011SCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
32816020011SCédric Le Goater                       __func__, offset);
32916020011SCédric Le Goater         break;
33016020011SCédric Le Goater     }
33116020011SCédric Le Goater 
33216020011SCédric Le Goater     return -1;
33316020011SCédric Le Goater }
33416020011SCédric Le Goater 
33516020011SCédric Le Goater static void aspeed_i2c_ctrl_write(void *opaque, hwaddr offset,
33616020011SCédric Le Goater                                   uint64_t value, unsigned size)
33716020011SCédric Le Goater {
33816020011SCédric Le Goater     switch (offset) {
33916020011SCédric Le Goater     case I2C_CTRL_STATUS:
34016020011SCédric Le Goater     default:
34116020011SCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
34216020011SCédric Le Goater                       __func__, offset);
34316020011SCédric Le Goater         break;
34416020011SCédric Le Goater     }
34516020011SCédric Le Goater }
34616020011SCédric Le Goater 
34716020011SCédric Le Goater static const MemoryRegionOps aspeed_i2c_bus_ops = {
34816020011SCédric Le Goater     .read = aspeed_i2c_bus_read,
34916020011SCédric Le Goater     .write = aspeed_i2c_bus_write,
35016020011SCédric Le Goater     .endianness = DEVICE_LITTLE_ENDIAN,
35116020011SCédric Le Goater };
35216020011SCédric Le Goater 
35316020011SCédric Le Goater static const MemoryRegionOps aspeed_i2c_ctrl_ops = {
35416020011SCédric Le Goater     .read = aspeed_i2c_ctrl_read,
35516020011SCédric Le Goater     .write = aspeed_i2c_ctrl_write,
35616020011SCédric Le Goater     .endianness = DEVICE_LITTLE_ENDIAN,
35716020011SCédric Le Goater };
35816020011SCédric Le Goater 
35916020011SCédric Le Goater static const VMStateDescription aspeed_i2c_bus_vmstate = {
36016020011SCédric Le Goater     .name = TYPE_ASPEED_I2C,
36116020011SCédric Le Goater     .version_id = 1,
36216020011SCédric Le Goater     .minimum_version_id = 1,
36316020011SCédric Le Goater     .fields = (VMStateField[]) {
36416020011SCédric Le Goater         VMSTATE_UINT8(id, AspeedI2CBus),
36516020011SCédric Le Goater         VMSTATE_UINT32(ctrl, AspeedI2CBus),
36616020011SCédric Le Goater         VMSTATE_UINT32_ARRAY(timing, AspeedI2CBus, 2),
36716020011SCédric Le Goater         VMSTATE_UINT32(intr_ctrl, AspeedI2CBus),
36816020011SCédric Le Goater         VMSTATE_UINT32(intr_status, AspeedI2CBus),
36916020011SCédric Le Goater         VMSTATE_UINT32(cmd, AspeedI2CBus),
37016020011SCédric Le Goater         VMSTATE_UINT32(buf, AspeedI2CBus),
37116020011SCédric Le Goater         VMSTATE_END_OF_LIST()
37216020011SCédric Le Goater     }
37316020011SCédric Le Goater };
37416020011SCédric Le Goater 
37516020011SCédric Le Goater static const VMStateDescription aspeed_i2c_vmstate = {
37616020011SCédric Le Goater     .name = TYPE_ASPEED_I2C,
37716020011SCédric Le Goater     .version_id = 1,
37816020011SCédric Le Goater     .minimum_version_id = 1,
37916020011SCédric Le Goater     .fields = (VMStateField[]) {
38016020011SCédric Le Goater         VMSTATE_UINT32(intr_status, AspeedI2CState),
38116020011SCédric Le Goater         VMSTATE_STRUCT_ARRAY(busses, AspeedI2CState,
38216020011SCédric Le Goater                              ASPEED_I2C_NR_BUSSES, 1, aspeed_i2c_bus_vmstate,
38316020011SCédric Le Goater                              AspeedI2CBus),
38416020011SCédric Le Goater         VMSTATE_END_OF_LIST()
38516020011SCédric Le Goater     }
38616020011SCédric Le Goater };
38716020011SCédric Le Goater 
38816020011SCédric Le Goater static void aspeed_i2c_reset(DeviceState *dev)
38916020011SCédric Le Goater {
39016020011SCédric Le Goater     int i;
39116020011SCédric Le Goater     AspeedI2CState *s = ASPEED_I2C(dev);
39216020011SCédric Le Goater 
39316020011SCédric Le Goater     s->intr_status = 0;
39416020011SCédric Le Goater 
39516020011SCédric Le Goater     for (i = 0; i < ASPEED_I2C_NR_BUSSES; i++) {
39616020011SCédric Le Goater         s->busses[i].intr_ctrl = 0;
39716020011SCédric Le Goater         s->busses[i].intr_status = 0;
39816020011SCédric Le Goater         s->busses[i].cmd = 0;
39916020011SCédric Le Goater         s->busses[i].buf = 0;
40016020011SCédric Le Goater         i2c_end_transfer(s->busses[i].bus);
40116020011SCédric Le Goater     }
40216020011SCédric Le Goater }
40316020011SCédric Le Goater 
40416020011SCédric Le Goater /*
40516020011SCédric Le Goater  * Address Definitions
40616020011SCédric Le Goater  *
40716020011SCédric Le Goater  *   0x000 ... 0x03F: Global Register
40816020011SCédric Le Goater  *   0x040 ... 0x07F: Device 1
40916020011SCédric Le Goater  *   0x080 ... 0x0BF: Device 2
41016020011SCédric Le Goater  *   0x0C0 ... 0x0FF: Device 3
41116020011SCédric Le Goater  *   0x100 ... 0x13F: Device 4
41216020011SCédric Le Goater  *   0x140 ... 0x17F: Device 5
41316020011SCédric Le Goater  *   0x180 ... 0x1BF: Device 6
41416020011SCédric Le Goater  *   0x1C0 ... 0x1FF: Device 7
41516020011SCédric Le Goater  *   0x200 ... 0x2FF: Buffer Pool  (unused in linux driver)
41616020011SCédric Le Goater  *   0x300 ... 0x33F: Device 8
41716020011SCédric Le Goater  *   0x340 ... 0x37F: Device 9
41816020011SCédric Le Goater  *   0x380 ... 0x3BF: Device 10
41916020011SCédric Le Goater  *   0x3C0 ... 0x3FF: Device 11
42016020011SCédric Le Goater  *   0x400 ... 0x43F: Device 12
42116020011SCédric Le Goater  *   0x440 ... 0x47F: Device 13
42216020011SCédric Le Goater  *   0x480 ... 0x4BF: Device 14
42316020011SCédric Le Goater  *   0x800 ... 0xFFF: Buffer Pool  (unused in linux driver)
42416020011SCédric Le Goater  */
42516020011SCédric Le Goater static void aspeed_i2c_realize(DeviceState *dev, Error **errp)
42616020011SCédric Le Goater {
42716020011SCédric Le Goater     int i;
42816020011SCédric Le Goater     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
42916020011SCédric Le Goater     AspeedI2CState *s = ASPEED_I2C(dev);
43016020011SCédric Le Goater 
43116020011SCédric Le Goater     sysbus_init_irq(sbd, &s->irq);
43216020011SCédric Le Goater     memory_region_init_io(&s->iomem, OBJECT(s), &aspeed_i2c_ctrl_ops, s,
43316020011SCédric Le Goater                           "aspeed.i2c", 0x1000);
43416020011SCédric Le Goater     sysbus_init_mmio(sbd, &s->iomem);
43516020011SCédric Le Goater 
43616020011SCédric Le Goater     for (i = 0; i < ASPEED_I2C_NR_BUSSES; i++) {
43716020011SCédric Le Goater         char name[16];
43816020011SCédric Le Goater         int offset = i < 7 ? 1 : 5;
43916020011SCédric Le Goater         snprintf(name, sizeof(name), "aspeed.i2c.%d", i);
44016020011SCédric Le Goater         s->busses[i].controller = s;
44116020011SCédric Le Goater         s->busses[i].id = i;
44216020011SCédric Le Goater         s->busses[i].bus = i2c_init_bus(dev, name);
44316020011SCédric Le Goater         memory_region_init_io(&s->busses[i].mr, OBJECT(dev),
44416020011SCédric Le Goater                               &aspeed_i2c_bus_ops, &s->busses[i], name, 0x40);
44516020011SCédric Le Goater         memory_region_add_subregion(&s->iomem, 0x40 * (i + offset),
44616020011SCédric Le Goater                                     &s->busses[i].mr);
44716020011SCédric Le Goater     }
44816020011SCédric Le Goater }
44916020011SCédric Le Goater 
45016020011SCédric Le Goater static void aspeed_i2c_class_init(ObjectClass *klass, void *data)
45116020011SCédric Le Goater {
45216020011SCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
45316020011SCédric Le Goater 
45416020011SCédric Le Goater     dc->vmsd = &aspeed_i2c_vmstate;
45516020011SCédric Le Goater     dc->reset = aspeed_i2c_reset;
45616020011SCédric Le Goater     dc->realize = aspeed_i2c_realize;
45716020011SCédric Le Goater     dc->desc = "Aspeed I2C Controller";
45816020011SCédric Le Goater }
45916020011SCédric Le Goater 
46016020011SCédric Le Goater static const TypeInfo aspeed_i2c_info = {
46116020011SCédric Le Goater     .name          = TYPE_ASPEED_I2C,
46216020011SCédric Le Goater     .parent        = TYPE_SYS_BUS_DEVICE,
46316020011SCédric Le Goater     .instance_size = sizeof(AspeedI2CState),
46416020011SCédric Le Goater     .class_init    = aspeed_i2c_class_init,
46516020011SCédric Le Goater };
46616020011SCédric Le Goater 
46716020011SCédric Le Goater static void aspeed_i2c_register_types(void)
46816020011SCédric Le Goater {
46916020011SCédric Le Goater     type_register_static(&aspeed_i2c_info);
47016020011SCédric Le Goater }
47116020011SCédric Le Goater 
47216020011SCédric Le Goater type_init(aspeed_i2c_register_types)
47316020011SCédric Le Goater 
47416020011SCédric Le Goater 
47516020011SCédric Le Goater I2CBus *aspeed_i2c_get_bus(DeviceState *dev, int busnr)
47616020011SCédric Le Goater {
47716020011SCédric Le Goater     AspeedI2CState *s = ASPEED_I2C(dev);
47816020011SCédric Le Goater     I2CBus *bus = NULL;
47916020011SCédric Le Goater 
48016020011SCédric Le Goater     if (busnr >= 0 && busnr < ASPEED_I2C_NR_BUSSES) {
48116020011SCédric Le Goater         bus = s->busses[busnr].bus;
48216020011SCédric Le Goater     }
48316020011SCédric Le Goater 
48416020011SCédric Le Goater     return bus;
48516020011SCédric Le Goater }
486