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 17216020011SCédric Le Goater static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value) 17316020011SCédric Le Goater { 174*ddabca75SCédric Le Goater bus->cmd &= ~0xFFFF; 17516020011SCédric Le Goater bus->cmd |= value & 0xFFFF; 17616020011SCédric Le Goater bus->intr_status = 0; 17716020011SCédric Le Goater 17816020011SCédric Le Goater if (bus->cmd & I2CD_M_START_CMD) { 17916020011SCédric Le Goater if (i2c_start_transfer(bus->bus, extract32(bus->buf, 1, 7), 18016020011SCédric Le Goater extract32(bus->buf, 0, 1))) { 18116020011SCédric Le Goater bus->intr_status |= I2CD_INTR_TX_NAK; 18216020011SCédric Le Goater } else { 18316020011SCédric Le Goater bus->intr_status |= I2CD_INTR_TX_ACK; 18416020011SCédric Le Goater } 18516020011SCédric Le Goater 186*ddabca75SCédric Le Goater /* START command is also a TX command, as the slave address is 187*ddabca75SCédric Le Goater * sent on the bus */ 188*ddabca75SCédric Le Goater bus->cmd &= ~(I2CD_M_START_CMD | I2CD_M_TX_CMD); 189*ddabca75SCédric Le Goater 190*ddabca75SCédric Le Goater /* No slave found */ 191*ddabca75SCédric Le Goater if (!i2c_bus_busy(bus->bus)) { 192*ddabca75SCédric Le Goater return; 193*ddabca75SCédric Le Goater } 194*ddabca75SCédric Le Goater } 195*ddabca75SCédric Le Goater 196*ddabca75SCédric Le Goater if (bus->cmd & I2CD_M_TX_CMD) { 19716020011SCédric Le Goater if (i2c_send(bus->bus, bus->buf)) { 19816020011SCédric Le Goater bus->intr_status |= (I2CD_INTR_TX_NAK | I2CD_INTR_ABNORMAL); 19916020011SCédric Le Goater i2c_end_transfer(bus->bus); 20016020011SCédric Le Goater } else { 20116020011SCédric Le Goater bus->intr_status |= I2CD_INTR_TX_ACK; 20216020011SCédric Le Goater } 203*ddabca75SCédric Le Goater bus->cmd &= ~I2CD_M_TX_CMD; 204*ddabca75SCédric Le Goater } 20516020011SCédric Le Goater 206*ddabca75SCédric Le Goater if (bus->cmd & I2CD_M_RX_CMD) { 20716020011SCédric Le Goater int ret = i2c_recv(bus->bus); 20816020011SCédric Le Goater if (ret < 0) { 20916020011SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: read failed\n", __func__); 21016020011SCédric Le Goater ret = 0xff; 21116020011SCédric Le Goater } else { 21216020011SCédric Le Goater bus->intr_status |= I2CD_INTR_RX_DONE; 21316020011SCédric Le Goater } 21416020011SCédric Le Goater bus->buf = (ret & I2CD_BYTE_BUF_RX_MASK) << I2CD_BYTE_BUF_RX_SHIFT; 215*ddabca75SCédric Le Goater bus->cmd &= ~I2CD_M_RX_CMD; 21616020011SCédric Le Goater } 21716020011SCédric Le Goater 21816020011SCédric Le Goater if (bus->cmd & (I2CD_M_STOP_CMD | I2CD_M_S_RX_CMD_LAST)) { 21916020011SCédric Le Goater if (!i2c_bus_busy(bus->bus)) { 22016020011SCédric Le Goater bus->intr_status |= I2CD_INTR_ABNORMAL; 22116020011SCédric Le Goater } else { 22216020011SCédric Le Goater i2c_end_transfer(bus->bus); 22316020011SCédric Le Goater bus->intr_status |= I2CD_INTR_NORMAL_STOP; 22416020011SCédric Le Goater } 225*ddabca75SCédric Le Goater bus->cmd &= ~I2CD_M_STOP_CMD; 22616020011SCédric Le Goater } 22716020011SCédric Le Goater } 22816020011SCédric Le Goater 22916020011SCédric Le Goater static void aspeed_i2c_bus_write(void *opaque, hwaddr offset, 23016020011SCédric Le Goater uint64_t value, unsigned size) 23116020011SCédric Le Goater { 23216020011SCédric Le Goater AspeedI2CBus *bus = opaque; 23316020011SCédric Le Goater 23416020011SCédric Le Goater switch (offset) { 23516020011SCédric Le Goater case I2CD_FUN_CTRL_REG: 23616020011SCédric Le Goater if (value & I2CD_SLAVE_EN) { 23716020011SCédric Le Goater qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n", 23816020011SCédric Le Goater __func__); 23916020011SCédric Le Goater break; 24016020011SCédric Le Goater } 24116020011SCédric Le Goater bus->ctrl = value & 0x0071C3FF; 24216020011SCédric Le Goater break; 24316020011SCédric Le Goater case I2CD_AC_TIMING_REG1: 24416020011SCédric Le Goater bus->timing[0] = value & 0xFFFFF0F; 24516020011SCédric Le Goater break; 24616020011SCédric Le Goater case I2CD_AC_TIMING_REG2: 24716020011SCédric Le Goater bus->timing[1] = value & 0x7; 24816020011SCédric Le Goater break; 24916020011SCédric Le Goater case I2CD_INTR_CTRL_REG: 25016020011SCédric Le Goater bus->intr_ctrl = value & 0x7FFF; 25116020011SCédric Le Goater break; 25216020011SCédric Le Goater case I2CD_INTR_STS_REG: 25316020011SCédric Le Goater bus->intr_status &= ~(value & 0x7FFF); 25416020011SCédric Le Goater bus->controller->intr_status &= ~(1 << bus->id); 25516020011SCédric Le Goater qemu_irq_lower(bus->controller->irq); 25616020011SCédric Le Goater break; 25716020011SCédric Le Goater case I2CD_DEV_ADDR_REG: 25816020011SCédric Le Goater qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n", 25916020011SCédric Le Goater __func__); 26016020011SCédric Le Goater break; 26116020011SCédric Le Goater case I2CD_BYTE_BUF_REG: 26216020011SCédric Le Goater bus->buf = (value & I2CD_BYTE_BUF_TX_MASK) << I2CD_BYTE_BUF_TX_SHIFT; 26316020011SCédric Le Goater break; 26416020011SCédric Le Goater case I2CD_CMD_REG: 26516020011SCédric Le Goater if (!aspeed_i2c_bus_is_enabled(bus)) { 26616020011SCédric Le Goater break; 26716020011SCédric Le Goater } 26816020011SCédric Le Goater 26916020011SCédric Le Goater if (!aspeed_i2c_bus_is_master(bus)) { 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 27516020011SCédric Le Goater aspeed_i2c_bus_handle_cmd(bus, value); 276*ddabca75SCédric Le Goater aspeed_i2c_bus_raise_interrupt(bus); 27716020011SCédric Le Goater break; 27816020011SCédric Le Goater 27916020011SCédric Le Goater default: 28016020011SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n", 28116020011SCédric Le Goater __func__, offset); 28216020011SCédric Le Goater } 28316020011SCédric Le Goater } 28416020011SCédric Le Goater 28516020011SCédric Le Goater static uint64_t aspeed_i2c_ctrl_read(void *opaque, hwaddr offset, 28616020011SCédric Le Goater unsigned size) 28716020011SCédric Le Goater { 28816020011SCédric Le Goater AspeedI2CState *s = opaque; 28916020011SCédric Le Goater 29016020011SCédric Le Goater switch (offset) { 29116020011SCédric Le Goater case I2C_CTRL_STATUS: 29216020011SCédric Le Goater return s->intr_status; 29316020011SCédric Le Goater default: 29416020011SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n", 29516020011SCédric Le Goater __func__, offset); 29616020011SCédric Le Goater break; 29716020011SCédric Le Goater } 29816020011SCédric Le Goater 29916020011SCédric Le Goater return -1; 30016020011SCédric Le Goater } 30116020011SCédric Le Goater 30216020011SCédric Le Goater static void aspeed_i2c_ctrl_write(void *opaque, hwaddr offset, 30316020011SCédric Le Goater uint64_t value, unsigned size) 30416020011SCédric Le Goater { 30516020011SCédric Le Goater switch (offset) { 30616020011SCédric Le Goater case I2C_CTRL_STATUS: 30716020011SCédric Le Goater default: 30816020011SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n", 30916020011SCédric Le Goater __func__, offset); 31016020011SCédric Le Goater break; 31116020011SCédric Le Goater } 31216020011SCédric Le Goater } 31316020011SCédric Le Goater 31416020011SCédric Le Goater static const MemoryRegionOps aspeed_i2c_bus_ops = { 31516020011SCédric Le Goater .read = aspeed_i2c_bus_read, 31616020011SCédric Le Goater .write = aspeed_i2c_bus_write, 31716020011SCédric Le Goater .endianness = DEVICE_LITTLE_ENDIAN, 31816020011SCédric Le Goater }; 31916020011SCédric Le Goater 32016020011SCédric Le Goater static const MemoryRegionOps aspeed_i2c_ctrl_ops = { 32116020011SCédric Le Goater .read = aspeed_i2c_ctrl_read, 32216020011SCédric Le Goater .write = aspeed_i2c_ctrl_write, 32316020011SCédric Le Goater .endianness = DEVICE_LITTLE_ENDIAN, 32416020011SCédric Le Goater }; 32516020011SCédric Le Goater 32616020011SCédric Le Goater static const VMStateDescription aspeed_i2c_bus_vmstate = { 32716020011SCédric Le Goater .name = TYPE_ASPEED_I2C, 32816020011SCédric Le Goater .version_id = 1, 32916020011SCédric Le Goater .minimum_version_id = 1, 33016020011SCédric Le Goater .fields = (VMStateField[]) { 33116020011SCédric Le Goater VMSTATE_UINT8(id, AspeedI2CBus), 33216020011SCédric Le Goater VMSTATE_UINT32(ctrl, AspeedI2CBus), 33316020011SCédric Le Goater VMSTATE_UINT32_ARRAY(timing, AspeedI2CBus, 2), 33416020011SCédric Le Goater VMSTATE_UINT32(intr_ctrl, AspeedI2CBus), 33516020011SCédric Le Goater VMSTATE_UINT32(intr_status, AspeedI2CBus), 33616020011SCédric Le Goater VMSTATE_UINT32(cmd, AspeedI2CBus), 33716020011SCédric Le Goater VMSTATE_UINT32(buf, AspeedI2CBus), 33816020011SCédric Le Goater VMSTATE_END_OF_LIST() 33916020011SCédric Le Goater } 34016020011SCédric Le Goater }; 34116020011SCédric Le Goater 34216020011SCédric Le Goater static const VMStateDescription aspeed_i2c_vmstate = { 34316020011SCédric Le Goater .name = TYPE_ASPEED_I2C, 34416020011SCédric Le Goater .version_id = 1, 34516020011SCédric Le Goater .minimum_version_id = 1, 34616020011SCédric Le Goater .fields = (VMStateField[]) { 34716020011SCédric Le Goater VMSTATE_UINT32(intr_status, AspeedI2CState), 34816020011SCédric Le Goater VMSTATE_STRUCT_ARRAY(busses, AspeedI2CState, 34916020011SCédric Le Goater ASPEED_I2C_NR_BUSSES, 1, aspeed_i2c_bus_vmstate, 35016020011SCédric Le Goater AspeedI2CBus), 35116020011SCédric Le Goater VMSTATE_END_OF_LIST() 35216020011SCédric Le Goater } 35316020011SCédric Le Goater }; 35416020011SCédric Le Goater 35516020011SCédric Le Goater static void aspeed_i2c_reset(DeviceState *dev) 35616020011SCédric Le Goater { 35716020011SCédric Le Goater int i; 35816020011SCédric Le Goater AspeedI2CState *s = ASPEED_I2C(dev); 35916020011SCédric Le Goater 36016020011SCédric Le Goater s->intr_status = 0; 36116020011SCédric Le Goater 36216020011SCédric Le Goater for (i = 0; i < ASPEED_I2C_NR_BUSSES; i++) { 36316020011SCédric Le Goater s->busses[i].intr_ctrl = 0; 36416020011SCédric Le Goater s->busses[i].intr_status = 0; 36516020011SCédric Le Goater s->busses[i].cmd = 0; 36616020011SCédric Le Goater s->busses[i].buf = 0; 36716020011SCédric Le Goater i2c_end_transfer(s->busses[i].bus); 36816020011SCédric Le Goater } 36916020011SCédric Le Goater } 37016020011SCédric Le Goater 37116020011SCédric Le Goater /* 37216020011SCédric Le Goater * Address Definitions 37316020011SCédric Le Goater * 37416020011SCédric Le Goater * 0x000 ... 0x03F: Global Register 37516020011SCédric Le Goater * 0x040 ... 0x07F: Device 1 37616020011SCédric Le Goater * 0x080 ... 0x0BF: Device 2 37716020011SCédric Le Goater * 0x0C0 ... 0x0FF: Device 3 37816020011SCédric Le Goater * 0x100 ... 0x13F: Device 4 37916020011SCédric Le Goater * 0x140 ... 0x17F: Device 5 38016020011SCédric Le Goater * 0x180 ... 0x1BF: Device 6 38116020011SCédric Le Goater * 0x1C0 ... 0x1FF: Device 7 38216020011SCédric Le Goater * 0x200 ... 0x2FF: Buffer Pool (unused in linux driver) 38316020011SCédric Le Goater * 0x300 ... 0x33F: Device 8 38416020011SCédric Le Goater * 0x340 ... 0x37F: Device 9 38516020011SCédric Le Goater * 0x380 ... 0x3BF: Device 10 38616020011SCédric Le Goater * 0x3C0 ... 0x3FF: Device 11 38716020011SCédric Le Goater * 0x400 ... 0x43F: Device 12 38816020011SCédric Le Goater * 0x440 ... 0x47F: Device 13 38916020011SCédric Le Goater * 0x480 ... 0x4BF: Device 14 39016020011SCédric Le Goater * 0x800 ... 0xFFF: Buffer Pool (unused in linux driver) 39116020011SCédric Le Goater */ 39216020011SCédric Le Goater static void aspeed_i2c_realize(DeviceState *dev, Error **errp) 39316020011SCédric Le Goater { 39416020011SCédric Le Goater int i; 39516020011SCédric Le Goater SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 39616020011SCédric Le Goater AspeedI2CState *s = ASPEED_I2C(dev); 39716020011SCédric Le Goater 39816020011SCédric Le Goater sysbus_init_irq(sbd, &s->irq); 39916020011SCédric Le Goater memory_region_init_io(&s->iomem, OBJECT(s), &aspeed_i2c_ctrl_ops, s, 40016020011SCédric Le Goater "aspeed.i2c", 0x1000); 40116020011SCédric Le Goater sysbus_init_mmio(sbd, &s->iomem); 40216020011SCédric Le Goater 40316020011SCédric Le Goater for (i = 0; i < ASPEED_I2C_NR_BUSSES; i++) { 40416020011SCédric Le Goater char name[16]; 40516020011SCédric Le Goater int offset = i < 7 ? 1 : 5; 40616020011SCédric Le Goater snprintf(name, sizeof(name), "aspeed.i2c.%d", i); 40716020011SCédric Le Goater s->busses[i].controller = s; 40816020011SCédric Le Goater s->busses[i].id = i; 40916020011SCédric Le Goater s->busses[i].bus = i2c_init_bus(dev, name); 41016020011SCédric Le Goater memory_region_init_io(&s->busses[i].mr, OBJECT(dev), 41116020011SCédric Le Goater &aspeed_i2c_bus_ops, &s->busses[i], name, 0x40); 41216020011SCédric Le Goater memory_region_add_subregion(&s->iomem, 0x40 * (i + offset), 41316020011SCédric Le Goater &s->busses[i].mr); 41416020011SCédric Le Goater } 41516020011SCédric Le Goater } 41616020011SCédric Le Goater 41716020011SCédric Le Goater static void aspeed_i2c_class_init(ObjectClass *klass, void *data) 41816020011SCédric Le Goater { 41916020011SCédric Le Goater DeviceClass *dc = DEVICE_CLASS(klass); 42016020011SCédric Le Goater 42116020011SCédric Le Goater dc->vmsd = &aspeed_i2c_vmstate; 42216020011SCédric Le Goater dc->reset = aspeed_i2c_reset; 42316020011SCédric Le Goater dc->realize = aspeed_i2c_realize; 42416020011SCédric Le Goater dc->desc = "Aspeed I2C Controller"; 42516020011SCédric Le Goater } 42616020011SCédric Le Goater 42716020011SCédric Le Goater static const TypeInfo aspeed_i2c_info = { 42816020011SCédric Le Goater .name = TYPE_ASPEED_I2C, 42916020011SCédric Le Goater .parent = TYPE_SYS_BUS_DEVICE, 43016020011SCédric Le Goater .instance_size = sizeof(AspeedI2CState), 43116020011SCédric Le Goater .class_init = aspeed_i2c_class_init, 43216020011SCédric Le Goater }; 43316020011SCédric Le Goater 43416020011SCédric Le Goater static void aspeed_i2c_register_types(void) 43516020011SCédric Le Goater { 43616020011SCédric Le Goater type_register_static(&aspeed_i2c_info); 43716020011SCédric Le Goater } 43816020011SCédric Le Goater 43916020011SCédric Le Goater type_init(aspeed_i2c_register_types) 44016020011SCédric Le Goater 44116020011SCédric Le Goater 44216020011SCédric Le Goater I2CBus *aspeed_i2c_get_bus(DeviceState *dev, int busnr) 44316020011SCédric Le Goater { 44416020011SCédric Le Goater AspeedI2CState *s = ASPEED_I2C(dev); 44516020011SCédric Le Goater I2CBus *bus = NULL; 44616020011SCédric Le Goater 44716020011SCédric Le Goater if (busnr >= 0 && busnr < ASPEED_I2C_NR_BUSSES) { 44816020011SCédric Le Goater bus = s->busses[busnr].bus; 44916020011SCédric Le Goater } 45016020011SCédric Le Goater 45116020011SCédric Le Goater return bus; 45216020011SCédric Le Goater } 453