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 */ 555540cb97SCédric Le Goater 565540cb97SCédric Le Goater #define I2CD_INTR_SLAVE_ADDR_MATCH (0x1 << 31) /* 0: addr1 1: addr2 */ 575540cb97SCédric Le Goater #define I2CD_INTR_SLAVE_ADDR_RX_PENDING (0x1 << 30) 585540cb97SCédric Le Goater /* bits[19-16] Reserved */ 595540cb97SCédric Le Goater 605540cb97SCédric Le Goater /* All bits below are cleared by writing 1 */ 615540cb97SCédric Le Goater #define I2CD_INTR_SLAVE_INACTIVE_TIMEOUT (0x1 << 15) 6216020011SCédric Le Goater #define I2CD_INTR_SDA_DL_TIMEOUT (0x1 << 14) 6316020011SCédric Le Goater #define I2CD_INTR_BUS_RECOVER_DONE (0x1 << 13) 6416020011SCédric Le Goater #define I2CD_INTR_SMBUS_ALERT (0x1 << 12) /* Bus [0-3] only */ 6516020011SCédric Le Goater #define I2CD_INTR_SMBUS_ARP_ADDR (0x1 << 11) /* Removed */ 6616020011SCédric Le Goater #define I2CD_INTR_SMBUS_DEV_ALERT_ADDR (0x1 << 10) /* Removed */ 6716020011SCédric Le Goater #define I2CD_INTR_SMBUS_DEF_ADDR (0x1 << 9) /* Removed */ 6816020011SCédric Le Goater #define I2CD_INTR_GCALL_ADDR (0x1 << 8) /* Removed */ 695540cb97SCédric Le Goater #define I2CD_INTR_SLAVE_ADDR_RX_MATCH (0x1 << 7) /* use RX_DONE */ 7016020011SCédric Le Goater #define I2CD_INTR_SCL_TIMEOUT (0x1 << 6) 7116020011SCédric Le Goater #define I2CD_INTR_ABNORMAL (0x1 << 5) 7216020011SCédric Le Goater #define I2CD_INTR_NORMAL_STOP (0x1 << 4) 7316020011SCédric Le Goater #define I2CD_INTR_ARBIT_LOSS (0x1 << 3) 7416020011SCédric Le Goater #define I2CD_INTR_RX_DONE (0x1 << 2) 7516020011SCédric Le Goater #define I2CD_INTR_TX_NAK (0x1 << 1) 7616020011SCédric Le Goater #define I2CD_INTR_TX_ACK (0x1 << 0) 7716020011SCédric Le Goater 7816020011SCédric Le Goater #define I2CD_CMD_REG 0x14 /* I2CD Command/Status */ 7916020011SCédric Le Goater #define I2CD_SDA_OE (0x1 << 28) 8016020011SCédric Le Goater #define I2CD_SDA_O (0x1 << 27) 8116020011SCédric Le Goater #define I2CD_SCL_OE (0x1 << 26) 8216020011SCédric Le Goater #define I2CD_SCL_O (0x1 << 25) 8316020011SCédric Le Goater #define I2CD_TX_TIMING (0x1 << 24) 8416020011SCédric Le Goater #define I2CD_TX_STATUS (0x1 << 23) 8516020011SCédric Le Goater 8616020011SCédric Le Goater #define I2CD_TX_STATE_SHIFT 19 /* Tx State Machine */ 8716020011SCédric Le Goater #define I2CD_TX_STATE_MASK 0xf 8816020011SCédric Le Goater #define I2CD_IDLE 0x0 8916020011SCédric Le Goater #define I2CD_MACTIVE 0x8 9016020011SCédric Le Goater #define I2CD_MSTART 0x9 9116020011SCédric Le Goater #define I2CD_MSTARTR 0xa 9216020011SCédric Le Goater #define I2CD_MSTOP 0xb 9316020011SCédric Le Goater #define I2CD_MTXD 0xc 9416020011SCédric Le Goater #define I2CD_MRXACK 0xd 9516020011SCédric Le Goater #define I2CD_MRXD 0xe 9616020011SCédric Le Goater #define I2CD_MTXACK 0xf 9716020011SCédric Le Goater #define I2CD_SWAIT 0x1 9816020011SCédric Le Goater #define I2CD_SRXD 0x4 9916020011SCédric Le Goater #define I2CD_STXACK 0x5 10016020011SCédric Le Goater #define I2CD_STXD 0x6 10116020011SCédric Le Goater #define I2CD_SRXACK 0x7 10216020011SCédric Le Goater #define I2CD_RECOVER 0x3 10316020011SCédric Le Goater 10416020011SCédric Le Goater #define I2CD_SCL_LINE_STS (0x1 << 18) 10516020011SCédric Le Goater #define I2CD_SDA_LINE_STS (0x1 << 17) 10616020011SCédric Le Goater #define I2CD_BUS_BUSY_STS (0x1 << 16) 10716020011SCédric Le Goater #define I2CD_SDA_OE_OUT_DIR (0x1 << 15) 10816020011SCédric Le Goater #define I2CD_SDA_O_OUT_DIR (0x1 << 14) 10916020011SCédric Le Goater #define I2CD_SCL_OE_OUT_DIR (0x1 << 13) 11016020011SCédric Le Goater #define I2CD_SCL_O_OUT_DIR (0x1 << 12) 11116020011SCédric Le Goater #define I2CD_BUS_RECOVER_CMD_EN (0x1 << 11) 11216020011SCédric Le Goater #define I2CD_S_ALT_EN (0x1 << 10) 11316020011SCédric Le Goater #define I2CD_RX_DMA_ENABLE (0x1 << 9) 11416020011SCédric Le Goater #define I2CD_TX_DMA_ENABLE (0x1 << 8) 11516020011SCédric Le Goater 11616020011SCédric Le Goater /* Command Bit */ 11716020011SCédric Le Goater #define I2CD_M_STOP_CMD (0x1 << 5) 11816020011SCédric Le Goater #define I2CD_M_S_RX_CMD_LAST (0x1 << 4) 11916020011SCédric Le Goater #define I2CD_M_RX_CMD (0x1 << 3) 12016020011SCédric Le Goater #define I2CD_S_TX_CMD (0x1 << 2) 12116020011SCédric Le Goater #define I2CD_M_TX_CMD (0x1 << 1) 12216020011SCédric Le Goater #define I2CD_M_START_CMD (0x1) 12316020011SCédric Le Goater 12416020011SCédric Le Goater #define I2CD_DEV_ADDR_REG 0x18 /* Slave Device Address */ 12516020011SCédric Le Goater #define I2CD_BUF_CTRL_REG 0x1c /* Pool Buffer Control */ 12616020011SCédric Le Goater #define I2CD_BYTE_BUF_REG 0x20 /* Transmit/Receive Byte Buffer */ 12716020011SCédric Le Goater #define I2CD_BYTE_BUF_TX_SHIFT 0 12816020011SCédric Le Goater #define I2CD_BYTE_BUF_TX_MASK 0xff 12916020011SCédric Le Goater #define I2CD_BYTE_BUF_RX_SHIFT 8 13016020011SCédric Le Goater #define I2CD_BYTE_BUF_RX_MASK 0xff 13116020011SCédric Le Goater 13216020011SCédric Le Goater 13316020011SCédric Le Goater static inline bool aspeed_i2c_bus_is_master(AspeedI2CBus *bus) 13416020011SCédric Le Goater { 13516020011SCédric Le Goater return bus->ctrl & I2CD_MASTER_EN; 13616020011SCédric Le Goater } 13716020011SCédric Le Goater 13816020011SCédric Le Goater static inline bool aspeed_i2c_bus_is_enabled(AspeedI2CBus *bus) 13916020011SCédric Le Goater { 14016020011SCédric Le Goater return bus->ctrl & (I2CD_MASTER_EN | I2CD_SLAVE_EN); 14116020011SCédric Le Goater } 14216020011SCédric Le Goater 14316020011SCédric Le Goater static inline void aspeed_i2c_bus_raise_interrupt(AspeedI2CBus *bus) 14416020011SCédric Le Goater { 14516020011SCédric Le Goater bus->intr_status &= bus->intr_ctrl; 14616020011SCédric Le Goater if (bus->intr_status) { 14716020011SCédric Le Goater bus->controller->intr_status |= 1 << bus->id; 14816020011SCédric Le Goater qemu_irq_raise(bus->controller->irq); 14916020011SCédric Le Goater } 15016020011SCédric Le Goater } 15116020011SCédric Le Goater 15216020011SCédric Le Goater static uint64_t aspeed_i2c_bus_read(void *opaque, hwaddr offset, 15316020011SCédric Le Goater unsigned size) 15416020011SCédric Le Goater { 15516020011SCédric Le Goater AspeedI2CBus *bus = opaque; 15616020011SCédric Le Goater 15716020011SCédric Le Goater switch (offset) { 15816020011SCédric Le Goater case I2CD_FUN_CTRL_REG: 15916020011SCédric Le Goater return bus->ctrl; 16016020011SCédric Le Goater case I2CD_AC_TIMING_REG1: 16116020011SCédric Le Goater return bus->timing[0]; 16216020011SCédric Le Goater case I2CD_AC_TIMING_REG2: 16316020011SCédric Le Goater return bus->timing[1]; 16416020011SCédric Le Goater case I2CD_INTR_CTRL_REG: 16516020011SCédric Le Goater return bus->intr_ctrl; 16616020011SCédric Le Goater case I2CD_INTR_STS_REG: 16716020011SCédric Le Goater return bus->intr_status; 16816020011SCédric Le Goater case I2CD_BYTE_BUF_REG: 16916020011SCédric Le Goater return bus->buf; 17016020011SCédric Le Goater case I2CD_CMD_REG: 17116020011SCédric Le Goater return bus->cmd | (i2c_bus_busy(bus->bus) << 16); 17216020011SCédric Le Goater default: 17316020011SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, 17416020011SCédric Le Goater "%s: Bad offset 0x%" HWADDR_PRIx "\n", __func__, offset); 17516020011SCédric Le Goater return -1; 17616020011SCédric Le Goater } 17716020011SCédric Le Goater } 17816020011SCédric Le Goater 1794960f084SCédric Le Goater static void aspeed_i2c_set_state(AspeedI2CBus *bus, uint8_t state) 1804960f084SCédric Le Goater { 1814960f084SCédric Le Goater bus->cmd &= ~(I2CD_TX_STATE_MASK << I2CD_TX_STATE_SHIFT); 1824960f084SCédric Le Goater bus->cmd |= (state & I2CD_TX_STATE_MASK) << I2CD_TX_STATE_SHIFT; 1834960f084SCédric Le Goater } 1844960f084SCédric Le Goater 1854960f084SCédric Le Goater static uint8_t aspeed_i2c_get_state(AspeedI2CBus *bus) 1864960f084SCédric Le Goater { 1874960f084SCédric Le Goater return (bus->cmd >> I2CD_TX_STATE_SHIFT) & I2CD_TX_STATE_MASK; 1884960f084SCédric Le Goater } 1894960f084SCédric Le Goater 1907bd9c60dSGuenter Roeck static void aspeed_i2c_handle_rx_cmd(AspeedI2CBus *bus) 1917bd9c60dSGuenter Roeck { 1927bd9c60dSGuenter Roeck int ret; 1937bd9c60dSGuenter Roeck 1947bd9c60dSGuenter Roeck aspeed_i2c_set_state(bus, I2CD_MRXD); 1957bd9c60dSGuenter Roeck ret = i2c_recv(bus->bus); 1967bd9c60dSGuenter Roeck if (ret < 0) { 1977bd9c60dSGuenter Roeck qemu_log_mask(LOG_GUEST_ERROR, "%s: read failed\n", __func__); 1987bd9c60dSGuenter Roeck ret = 0xff; 1997bd9c60dSGuenter Roeck } else { 2007bd9c60dSGuenter Roeck bus->intr_status |= I2CD_INTR_RX_DONE; 2017bd9c60dSGuenter Roeck } 2027bd9c60dSGuenter Roeck bus->buf = (ret & I2CD_BYTE_BUF_RX_MASK) << I2CD_BYTE_BUF_RX_SHIFT; 2037bd9c60dSGuenter Roeck if (bus->cmd & I2CD_M_S_RX_CMD_LAST) { 2047bd9c60dSGuenter Roeck i2c_nack(bus->bus); 2057bd9c60dSGuenter Roeck } 2067bd9c60dSGuenter Roeck bus->cmd &= ~(I2CD_M_RX_CMD | I2CD_M_S_RX_CMD_LAST); 2077bd9c60dSGuenter Roeck aspeed_i2c_set_state(bus, I2CD_MACTIVE); 2087bd9c60dSGuenter Roeck } 2097bd9c60dSGuenter Roeck 2104960f084SCédric Le Goater /* 2114960f084SCédric Le Goater * The state machine needs some refinement. It is only used to track 2124960f084SCédric Le Goater * invalid STOP commands for the moment. 2134960f084SCédric Le Goater */ 21416020011SCédric Le Goater static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value) 21516020011SCédric Le Goater { 216ddabca75SCédric Le Goater bus->cmd &= ~0xFFFF; 21716020011SCédric Le Goater bus->cmd |= value & 0xFFFF; 21816020011SCédric Le Goater 21916020011SCédric Le Goater if (bus->cmd & I2CD_M_START_CMD) { 2204960f084SCédric Le Goater uint8_t state = aspeed_i2c_get_state(bus) & I2CD_MACTIVE ? 2214960f084SCédric Le Goater I2CD_MSTARTR : I2CD_MSTART; 2224960f084SCédric Le Goater 2234960f084SCédric Le Goater aspeed_i2c_set_state(bus, state); 2244960f084SCédric Le Goater 22516020011SCédric Le Goater if (i2c_start_transfer(bus->bus, extract32(bus->buf, 1, 7), 22616020011SCédric Le Goater extract32(bus->buf, 0, 1))) { 22716020011SCédric Le Goater bus->intr_status |= I2CD_INTR_TX_NAK; 22816020011SCédric Le Goater } else { 22916020011SCédric Le Goater bus->intr_status |= I2CD_INTR_TX_ACK; 23016020011SCédric Le Goater } 23116020011SCédric Le Goater 232ddabca75SCédric Le Goater /* START command is also a TX command, as the slave address is 233ddabca75SCédric Le Goater * sent on the bus */ 234ddabca75SCédric Le Goater bus->cmd &= ~(I2CD_M_START_CMD | I2CD_M_TX_CMD); 235ddabca75SCédric Le Goater 236ddabca75SCédric Le Goater /* No slave found */ 237ddabca75SCédric Le Goater if (!i2c_bus_busy(bus->bus)) { 238ddabca75SCédric Le Goater return; 239ddabca75SCédric Le Goater } 2404960f084SCédric Le Goater aspeed_i2c_set_state(bus, I2CD_MACTIVE); 241ddabca75SCédric Le Goater } 242ddabca75SCédric Le Goater 243ddabca75SCédric Le Goater if (bus->cmd & I2CD_M_TX_CMD) { 2444960f084SCédric Le Goater aspeed_i2c_set_state(bus, I2CD_MTXD); 24516020011SCédric Le Goater if (i2c_send(bus->bus, bus->buf)) { 2464960f084SCédric Le Goater bus->intr_status |= (I2CD_INTR_TX_NAK); 24716020011SCédric Le Goater i2c_end_transfer(bus->bus); 24816020011SCédric Le Goater } else { 24916020011SCédric Le Goater bus->intr_status |= I2CD_INTR_TX_ACK; 25016020011SCédric Le Goater } 251ddabca75SCédric Le Goater bus->cmd &= ~I2CD_M_TX_CMD; 2524960f084SCédric Le Goater aspeed_i2c_set_state(bus, I2CD_MACTIVE); 253ddabca75SCédric Le Goater } 25416020011SCédric Le Goater 255*bb626e5bSGuenter Roeck if ((bus->cmd & (I2CD_M_RX_CMD | I2CD_M_S_RX_CMD_LAST)) && 256*bb626e5bSGuenter Roeck !(bus->intr_status & I2CD_INTR_RX_DONE)) { 2577bd9c60dSGuenter Roeck aspeed_i2c_handle_rx_cmd(bus); 25816020011SCédric Le Goater } 25916020011SCédric Le Goater 260d0efdc16SCédric Le Goater if (bus->cmd & I2CD_M_STOP_CMD) { 2614960f084SCédric Le Goater if (!(aspeed_i2c_get_state(bus) & I2CD_MACTIVE)) { 2624960f084SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: abnormal stop\n", __func__); 26316020011SCédric Le Goater bus->intr_status |= I2CD_INTR_ABNORMAL; 26416020011SCédric Le Goater } else { 2654960f084SCédric Le Goater aspeed_i2c_set_state(bus, I2CD_MSTOP); 26616020011SCédric Le Goater i2c_end_transfer(bus->bus); 26716020011SCédric Le Goater bus->intr_status |= I2CD_INTR_NORMAL_STOP; 26816020011SCédric Le Goater } 269ddabca75SCédric Le Goater bus->cmd &= ~I2CD_M_STOP_CMD; 2704960f084SCédric Le Goater aspeed_i2c_set_state(bus, I2CD_IDLE); 27116020011SCédric Le Goater } 27216020011SCédric Le Goater } 27316020011SCédric Le Goater 27416020011SCédric Le Goater static void aspeed_i2c_bus_write(void *opaque, hwaddr offset, 27516020011SCédric Le Goater uint64_t value, unsigned size) 27616020011SCédric Le Goater { 27716020011SCédric Le Goater AspeedI2CBus *bus = opaque; 278*bb626e5bSGuenter Roeck bool handle_rx; 27916020011SCédric Le Goater 28016020011SCédric Le Goater switch (offset) { 28116020011SCédric Le Goater case I2CD_FUN_CTRL_REG: 28216020011SCédric Le Goater if (value & I2CD_SLAVE_EN) { 28316020011SCédric Le Goater qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n", 28416020011SCédric Le Goater __func__); 28516020011SCédric Le Goater break; 28616020011SCédric Le Goater } 28716020011SCédric Le Goater bus->ctrl = value & 0x0071C3FF; 28816020011SCédric Le Goater break; 28916020011SCédric Le Goater case I2CD_AC_TIMING_REG1: 29016020011SCédric Le Goater bus->timing[0] = value & 0xFFFFF0F; 29116020011SCédric Le Goater break; 29216020011SCédric Le Goater case I2CD_AC_TIMING_REG2: 29316020011SCédric Le Goater bus->timing[1] = value & 0x7; 29416020011SCédric Le Goater break; 29516020011SCédric Le Goater case I2CD_INTR_CTRL_REG: 29616020011SCédric Le Goater bus->intr_ctrl = value & 0x7FFF; 29716020011SCédric Le Goater break; 29816020011SCédric Le Goater case I2CD_INTR_STS_REG: 299*bb626e5bSGuenter Roeck handle_rx = (bus->intr_status & I2CD_INTR_RX_DONE) && 300*bb626e5bSGuenter Roeck (value & I2CD_INTR_RX_DONE); 30116020011SCédric Le Goater bus->intr_status &= ~(value & 0x7FFF); 3025540cb97SCédric Le Goater if (!bus->intr_status) { 30316020011SCédric Le Goater bus->controller->intr_status &= ~(1 << bus->id); 30416020011SCédric Le Goater qemu_irq_lower(bus->controller->irq); 3055540cb97SCédric Le Goater } 306*bb626e5bSGuenter Roeck if (handle_rx && (bus->cmd & (I2CD_M_RX_CMD | I2CD_M_S_RX_CMD_LAST))) { 307*bb626e5bSGuenter Roeck aspeed_i2c_handle_rx_cmd(bus); 308*bb626e5bSGuenter Roeck aspeed_i2c_bus_raise_interrupt(bus); 309*bb626e5bSGuenter Roeck } 31016020011SCédric Le Goater break; 31116020011SCédric Le Goater case I2CD_DEV_ADDR_REG: 31216020011SCédric Le Goater qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n", 31316020011SCédric Le Goater __func__); 31416020011SCédric Le Goater break; 31516020011SCédric Le Goater case I2CD_BYTE_BUF_REG: 31616020011SCédric Le Goater bus->buf = (value & I2CD_BYTE_BUF_TX_MASK) << I2CD_BYTE_BUF_TX_SHIFT; 31716020011SCédric Le Goater break; 31816020011SCédric Le Goater case I2CD_CMD_REG: 31916020011SCédric Le Goater if (!aspeed_i2c_bus_is_enabled(bus)) { 32016020011SCédric Le Goater break; 32116020011SCédric Le Goater } 32216020011SCédric Le Goater 32316020011SCédric Le Goater if (!aspeed_i2c_bus_is_master(bus)) { 32416020011SCédric Le Goater qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n", 32516020011SCédric Le Goater __func__); 32616020011SCédric Le Goater break; 32716020011SCédric Le Goater } 32816020011SCédric Le Goater 32916020011SCédric Le Goater aspeed_i2c_bus_handle_cmd(bus, value); 330ddabca75SCédric Le Goater aspeed_i2c_bus_raise_interrupt(bus); 33116020011SCédric Le Goater break; 33216020011SCédric Le Goater 33316020011SCédric Le Goater default: 33416020011SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n", 33516020011SCédric Le Goater __func__, offset); 33616020011SCédric Le Goater } 33716020011SCédric Le Goater } 33816020011SCédric Le Goater 33916020011SCédric Le Goater static uint64_t aspeed_i2c_ctrl_read(void *opaque, hwaddr offset, 34016020011SCédric Le Goater unsigned size) 34116020011SCédric Le Goater { 34216020011SCédric Le Goater AspeedI2CState *s = opaque; 34316020011SCédric Le Goater 34416020011SCédric Le Goater switch (offset) { 34516020011SCédric Le Goater case I2C_CTRL_STATUS: 34616020011SCédric Le Goater return s->intr_status; 34716020011SCédric Le Goater default: 34816020011SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n", 34916020011SCédric Le Goater __func__, offset); 35016020011SCédric Le Goater break; 35116020011SCédric Le Goater } 35216020011SCédric Le Goater 35316020011SCédric Le Goater return -1; 35416020011SCédric Le Goater } 35516020011SCédric Le Goater 35616020011SCédric Le Goater static void aspeed_i2c_ctrl_write(void *opaque, hwaddr offset, 35716020011SCédric Le Goater uint64_t value, unsigned size) 35816020011SCédric Le Goater { 35916020011SCédric Le Goater switch (offset) { 36016020011SCédric Le Goater case I2C_CTRL_STATUS: 36116020011SCédric Le Goater default: 36216020011SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n", 36316020011SCédric Le Goater __func__, offset); 36416020011SCédric Le Goater break; 36516020011SCédric Le Goater } 36616020011SCédric Le Goater } 36716020011SCédric Le Goater 36816020011SCédric Le Goater static const MemoryRegionOps aspeed_i2c_bus_ops = { 36916020011SCédric Le Goater .read = aspeed_i2c_bus_read, 37016020011SCédric Le Goater .write = aspeed_i2c_bus_write, 37116020011SCédric Le Goater .endianness = DEVICE_LITTLE_ENDIAN, 37216020011SCédric Le Goater }; 37316020011SCédric Le Goater 37416020011SCédric Le Goater static const MemoryRegionOps aspeed_i2c_ctrl_ops = { 37516020011SCédric Le Goater .read = aspeed_i2c_ctrl_read, 37616020011SCédric Le Goater .write = aspeed_i2c_ctrl_write, 37716020011SCédric Le Goater .endianness = DEVICE_LITTLE_ENDIAN, 37816020011SCédric Le Goater }; 37916020011SCédric Le Goater 38016020011SCédric Le Goater static const VMStateDescription aspeed_i2c_bus_vmstate = { 38116020011SCédric Le Goater .name = TYPE_ASPEED_I2C, 38216020011SCédric Le Goater .version_id = 1, 38316020011SCédric Le Goater .minimum_version_id = 1, 38416020011SCédric Le Goater .fields = (VMStateField[]) { 38516020011SCédric Le Goater VMSTATE_UINT8(id, AspeedI2CBus), 38616020011SCédric Le Goater VMSTATE_UINT32(ctrl, AspeedI2CBus), 38716020011SCédric Le Goater VMSTATE_UINT32_ARRAY(timing, AspeedI2CBus, 2), 38816020011SCédric Le Goater VMSTATE_UINT32(intr_ctrl, AspeedI2CBus), 38916020011SCédric Le Goater VMSTATE_UINT32(intr_status, AspeedI2CBus), 39016020011SCédric Le Goater VMSTATE_UINT32(cmd, AspeedI2CBus), 39116020011SCédric Le Goater VMSTATE_UINT32(buf, AspeedI2CBus), 39216020011SCédric Le Goater VMSTATE_END_OF_LIST() 39316020011SCédric Le Goater } 39416020011SCédric Le Goater }; 39516020011SCédric Le Goater 39616020011SCédric Le Goater static const VMStateDescription aspeed_i2c_vmstate = { 39716020011SCédric Le Goater .name = TYPE_ASPEED_I2C, 39816020011SCédric Le Goater .version_id = 1, 39916020011SCédric Le Goater .minimum_version_id = 1, 40016020011SCédric Le Goater .fields = (VMStateField[]) { 40116020011SCédric Le Goater VMSTATE_UINT32(intr_status, AspeedI2CState), 40216020011SCédric Le Goater VMSTATE_STRUCT_ARRAY(busses, AspeedI2CState, 40316020011SCédric Le Goater ASPEED_I2C_NR_BUSSES, 1, aspeed_i2c_bus_vmstate, 40416020011SCédric Le Goater AspeedI2CBus), 40516020011SCédric Le Goater VMSTATE_END_OF_LIST() 40616020011SCédric Le Goater } 40716020011SCédric Le Goater }; 40816020011SCédric Le Goater 40916020011SCédric Le Goater static void aspeed_i2c_reset(DeviceState *dev) 41016020011SCédric Le Goater { 41116020011SCédric Le Goater int i; 41216020011SCédric Le Goater AspeedI2CState *s = ASPEED_I2C(dev); 41316020011SCédric Le Goater 41416020011SCédric Le Goater s->intr_status = 0; 41516020011SCédric Le Goater 41616020011SCédric Le Goater for (i = 0; i < ASPEED_I2C_NR_BUSSES; i++) { 41716020011SCédric Le Goater s->busses[i].intr_ctrl = 0; 41816020011SCédric Le Goater s->busses[i].intr_status = 0; 41916020011SCédric Le Goater s->busses[i].cmd = 0; 42016020011SCédric Le Goater s->busses[i].buf = 0; 42116020011SCédric Le Goater i2c_end_transfer(s->busses[i].bus); 42216020011SCédric Le Goater } 42316020011SCédric Le Goater } 42416020011SCédric Le Goater 42516020011SCédric Le Goater /* 42616020011SCédric Le Goater * Address Definitions 42716020011SCédric Le Goater * 42816020011SCédric Le Goater * 0x000 ... 0x03F: Global Register 42916020011SCédric Le Goater * 0x040 ... 0x07F: Device 1 43016020011SCédric Le Goater * 0x080 ... 0x0BF: Device 2 43116020011SCédric Le Goater * 0x0C0 ... 0x0FF: Device 3 43216020011SCédric Le Goater * 0x100 ... 0x13F: Device 4 43316020011SCédric Le Goater * 0x140 ... 0x17F: Device 5 43416020011SCédric Le Goater * 0x180 ... 0x1BF: Device 6 43516020011SCédric Le Goater * 0x1C0 ... 0x1FF: Device 7 43616020011SCédric Le Goater * 0x200 ... 0x2FF: Buffer Pool (unused in linux driver) 43716020011SCédric Le Goater * 0x300 ... 0x33F: Device 8 43816020011SCédric Le Goater * 0x340 ... 0x37F: Device 9 43916020011SCédric Le Goater * 0x380 ... 0x3BF: Device 10 44016020011SCédric Le Goater * 0x3C0 ... 0x3FF: Device 11 44116020011SCédric Le Goater * 0x400 ... 0x43F: Device 12 44216020011SCédric Le Goater * 0x440 ... 0x47F: Device 13 44316020011SCédric Le Goater * 0x480 ... 0x4BF: Device 14 44416020011SCédric Le Goater * 0x800 ... 0xFFF: Buffer Pool (unused in linux driver) 44516020011SCédric Le Goater */ 44616020011SCédric Le Goater static void aspeed_i2c_realize(DeviceState *dev, Error **errp) 44716020011SCédric Le Goater { 44816020011SCédric Le Goater int i; 44916020011SCédric Le Goater SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 45016020011SCédric Le Goater AspeedI2CState *s = ASPEED_I2C(dev); 45116020011SCédric Le Goater 45216020011SCédric Le Goater sysbus_init_irq(sbd, &s->irq); 45316020011SCédric Le Goater memory_region_init_io(&s->iomem, OBJECT(s), &aspeed_i2c_ctrl_ops, s, 45416020011SCédric Le Goater "aspeed.i2c", 0x1000); 45516020011SCédric Le Goater sysbus_init_mmio(sbd, &s->iomem); 45616020011SCédric Le Goater 45716020011SCédric Le Goater for (i = 0; i < ASPEED_I2C_NR_BUSSES; i++) { 45816020011SCédric Le Goater char name[16]; 45916020011SCédric Le Goater int offset = i < 7 ? 1 : 5; 46016020011SCédric Le Goater snprintf(name, sizeof(name), "aspeed.i2c.%d", i); 46116020011SCédric Le Goater s->busses[i].controller = s; 46216020011SCédric Le Goater s->busses[i].id = i; 46316020011SCédric Le Goater s->busses[i].bus = i2c_init_bus(dev, name); 46416020011SCédric Le Goater memory_region_init_io(&s->busses[i].mr, OBJECT(dev), 46516020011SCédric Le Goater &aspeed_i2c_bus_ops, &s->busses[i], name, 0x40); 46616020011SCédric Le Goater memory_region_add_subregion(&s->iomem, 0x40 * (i + offset), 46716020011SCédric Le Goater &s->busses[i].mr); 46816020011SCédric Le Goater } 46916020011SCédric Le Goater } 47016020011SCédric Le Goater 47116020011SCédric Le Goater static void aspeed_i2c_class_init(ObjectClass *klass, void *data) 47216020011SCédric Le Goater { 47316020011SCédric Le Goater DeviceClass *dc = DEVICE_CLASS(klass); 47416020011SCédric Le Goater 47516020011SCédric Le Goater dc->vmsd = &aspeed_i2c_vmstate; 47616020011SCédric Le Goater dc->reset = aspeed_i2c_reset; 47716020011SCédric Le Goater dc->realize = aspeed_i2c_realize; 47816020011SCédric Le Goater dc->desc = "Aspeed I2C Controller"; 47916020011SCédric Le Goater } 48016020011SCédric Le Goater 48116020011SCédric Le Goater static const TypeInfo aspeed_i2c_info = { 48216020011SCédric Le Goater .name = TYPE_ASPEED_I2C, 48316020011SCédric Le Goater .parent = TYPE_SYS_BUS_DEVICE, 48416020011SCédric Le Goater .instance_size = sizeof(AspeedI2CState), 48516020011SCédric Le Goater .class_init = aspeed_i2c_class_init, 48616020011SCédric Le Goater }; 48716020011SCédric Le Goater 48816020011SCédric Le Goater static void aspeed_i2c_register_types(void) 48916020011SCédric Le Goater { 49016020011SCédric Le Goater type_register_static(&aspeed_i2c_info); 49116020011SCédric Le Goater } 49216020011SCédric Le Goater 49316020011SCédric Le Goater type_init(aspeed_i2c_register_types) 49416020011SCédric Le Goater 49516020011SCédric Le Goater 49616020011SCédric Le Goater I2CBus *aspeed_i2c_get_bus(DeviceState *dev, int busnr) 49716020011SCédric Le Goater { 49816020011SCédric Le Goater AspeedI2CState *s = ASPEED_I2C(dev); 49916020011SCédric Le Goater I2CBus *bus = NULL; 50016020011SCédric Le Goater 50116020011SCédric Le Goater if (busnr >= 0 && busnr < ASPEED_I2C_NR_BUSSES) { 50216020011SCédric Le Goater bus = s->busses[busnr].bus; 50316020011SCédric Le Goater } 50416020011SCédric Le Goater 50516020011SCédric Le Goater return bus; 50616020011SCédric Le Goater } 507