1*16020011SCédric Le Goater /* 2*16020011SCédric Le Goater * ARM Aspeed I2C controller 3*16020011SCédric Le Goater * 4*16020011SCédric Le Goater * Copyright (C) 2016 IBM Corp. 5*16020011SCédric Le Goater * 6*16020011SCédric Le Goater * This program is free software; you can redistribute it and/or 7*16020011SCédric Le Goater * modify it under the terms of the GNU General Public License 8*16020011SCédric Le Goater * as published by the Free Software Foundation; either version 2 9*16020011SCédric Le Goater * of the License, or (at your option) any later version. 10*16020011SCédric Le Goater * 11*16020011SCédric Le Goater * This program is distributed in the hope that it will be useful, 12*16020011SCédric Le Goater * but WITHOUT ANY WARRANTY; without even the implied warranty of 13*16020011SCédric Le Goater * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*16020011SCédric Le Goater * GNU General Public License for more details. 15*16020011SCédric Le Goater * 16*16020011SCédric Le Goater * You should have received a copy of the GNU General Public License 17*16020011SCédric Le Goater * along with this program; if not, see <http://www.gnu.org/licenses/>. 18*16020011SCédric Le Goater * 19*16020011SCédric Le Goater */ 20*16020011SCédric Le Goater 21*16020011SCédric Le Goater #include "qemu/osdep.h" 22*16020011SCédric Le Goater #include "hw/sysbus.h" 23*16020011SCédric Le Goater #include "qemu/log.h" 24*16020011SCédric Le Goater #include "hw/i2c/aspeed_i2c.h" 25*16020011SCédric Le Goater 26*16020011SCédric Le Goater /* I2C Global Register */ 27*16020011SCédric Le Goater 28*16020011SCédric Le Goater #define I2C_CTRL_STATUS 0x00 /* Device Interrupt Status */ 29*16020011SCédric Le Goater #define I2C_CTRL_ASSIGN 0x08 /* Device Interrupt Target 30*16020011SCédric Le Goater Assignment */ 31*16020011SCédric Le Goater 32*16020011SCédric Le Goater /* I2C Device (Bus) Register */ 33*16020011SCédric Le Goater 34*16020011SCédric Le Goater #define I2CD_FUN_CTRL_REG 0x00 /* I2CD Function Control */ 35*16020011SCédric Le Goater #define I2CD_BUFF_SEL_MASK (0x7 << 20) 36*16020011SCédric Le Goater #define I2CD_BUFF_SEL(x) (x << 20) 37*16020011SCédric Le Goater #define I2CD_M_SDA_LOCK_EN (0x1 << 16) 38*16020011SCédric Le Goater #define I2CD_MULTI_MASTER_DIS (0x1 << 15) 39*16020011SCédric Le Goater #define I2CD_M_SCL_DRIVE_EN (0x1 << 14) 40*16020011SCédric Le Goater #define I2CD_MSB_STS (0x1 << 9) 41*16020011SCédric Le Goater #define I2CD_SDA_DRIVE_1T_EN (0x1 << 8) 42*16020011SCédric Le Goater #define I2CD_M_SDA_DRIVE_1T_EN (0x1 << 7) 43*16020011SCédric Le Goater #define I2CD_M_HIGH_SPEED_EN (0x1 << 6) 44*16020011SCédric Le Goater #define I2CD_DEF_ADDR_EN (0x1 << 5) 45*16020011SCédric Le Goater #define I2CD_DEF_ALERT_EN (0x1 << 4) 46*16020011SCédric Le Goater #define I2CD_DEF_ARP_EN (0x1 << 3) 47*16020011SCédric Le Goater #define I2CD_DEF_GCALL_EN (0x1 << 2) 48*16020011SCédric Le Goater #define I2CD_SLAVE_EN (0x1 << 1) 49*16020011SCédric Le Goater #define I2CD_MASTER_EN (0x1) 50*16020011SCédric Le Goater 51*16020011SCédric Le Goater #define I2CD_AC_TIMING_REG1 0x04 /* Clock and AC Timing Control #1 */ 52*16020011SCédric Le Goater #define I2CD_AC_TIMING_REG2 0x08 /* Clock and AC Timing Control #1 */ 53*16020011SCédric Le Goater #define I2CD_INTR_CTRL_REG 0x0c /* I2CD Interrupt Control */ 54*16020011SCédric Le Goater #define I2CD_INTR_STS_REG 0x10 /* I2CD Interrupt Status */ 55*16020011SCédric Le Goater #define I2CD_INTR_SDA_DL_TIMEOUT (0x1 << 14) 56*16020011SCédric Le Goater #define I2CD_INTR_BUS_RECOVER_DONE (0x1 << 13) 57*16020011SCédric Le Goater #define I2CD_INTR_SMBUS_ALERT (0x1 << 12) /* Bus [0-3] only */ 58*16020011SCédric Le Goater #define I2CD_INTR_SMBUS_ARP_ADDR (0x1 << 11) /* Removed */ 59*16020011SCédric Le Goater #define I2CD_INTR_SMBUS_DEV_ALERT_ADDR (0x1 << 10) /* Removed */ 60*16020011SCédric Le Goater #define I2CD_INTR_SMBUS_DEF_ADDR (0x1 << 9) /* Removed */ 61*16020011SCédric Le Goater #define I2CD_INTR_GCALL_ADDR (0x1 << 8) /* Removed */ 62*16020011SCédric Le Goater #define I2CD_INTR_SLAVE_MATCH (0x1 << 7) /* use RX_DONE */ 63*16020011SCédric Le Goater #define I2CD_INTR_SCL_TIMEOUT (0x1 << 6) 64*16020011SCédric Le Goater #define I2CD_INTR_ABNORMAL (0x1 << 5) 65*16020011SCédric Le Goater #define I2CD_INTR_NORMAL_STOP (0x1 << 4) 66*16020011SCédric Le Goater #define I2CD_INTR_ARBIT_LOSS (0x1 << 3) 67*16020011SCédric Le Goater #define I2CD_INTR_RX_DONE (0x1 << 2) 68*16020011SCédric Le Goater #define I2CD_INTR_TX_NAK (0x1 << 1) 69*16020011SCédric Le Goater #define I2CD_INTR_TX_ACK (0x1 << 0) 70*16020011SCédric Le Goater 71*16020011SCédric Le Goater #define I2CD_CMD_REG 0x14 /* I2CD Command/Status */ 72*16020011SCédric Le Goater #define I2CD_SDA_OE (0x1 << 28) 73*16020011SCédric Le Goater #define I2CD_SDA_O (0x1 << 27) 74*16020011SCédric Le Goater #define I2CD_SCL_OE (0x1 << 26) 75*16020011SCédric Le Goater #define I2CD_SCL_O (0x1 << 25) 76*16020011SCédric Le Goater #define I2CD_TX_TIMING (0x1 << 24) 77*16020011SCédric Le Goater #define I2CD_TX_STATUS (0x1 << 23) 78*16020011SCédric Le Goater 79*16020011SCédric Le Goater #define I2CD_TX_STATE_SHIFT 19 /* Tx State Machine */ 80*16020011SCédric Le Goater #define I2CD_TX_STATE_MASK 0xf 81*16020011SCédric Le Goater #define I2CD_IDLE 0x0 82*16020011SCédric Le Goater #define I2CD_MACTIVE 0x8 83*16020011SCédric Le Goater #define I2CD_MSTART 0x9 84*16020011SCédric Le Goater #define I2CD_MSTARTR 0xa 85*16020011SCédric Le Goater #define I2CD_MSTOP 0xb 86*16020011SCédric Le Goater #define I2CD_MTXD 0xc 87*16020011SCédric Le Goater #define I2CD_MRXACK 0xd 88*16020011SCédric Le Goater #define I2CD_MRXD 0xe 89*16020011SCédric Le Goater #define I2CD_MTXACK 0xf 90*16020011SCédric Le Goater #define I2CD_SWAIT 0x1 91*16020011SCédric Le Goater #define I2CD_SRXD 0x4 92*16020011SCédric Le Goater #define I2CD_STXACK 0x5 93*16020011SCédric Le Goater #define I2CD_STXD 0x6 94*16020011SCédric Le Goater #define I2CD_SRXACK 0x7 95*16020011SCédric Le Goater #define I2CD_RECOVER 0x3 96*16020011SCédric Le Goater 97*16020011SCédric Le Goater #define I2CD_SCL_LINE_STS (0x1 << 18) 98*16020011SCédric Le Goater #define I2CD_SDA_LINE_STS (0x1 << 17) 99*16020011SCédric Le Goater #define I2CD_BUS_BUSY_STS (0x1 << 16) 100*16020011SCédric Le Goater #define I2CD_SDA_OE_OUT_DIR (0x1 << 15) 101*16020011SCédric Le Goater #define I2CD_SDA_O_OUT_DIR (0x1 << 14) 102*16020011SCédric Le Goater #define I2CD_SCL_OE_OUT_DIR (0x1 << 13) 103*16020011SCédric Le Goater #define I2CD_SCL_O_OUT_DIR (0x1 << 12) 104*16020011SCédric Le Goater #define I2CD_BUS_RECOVER_CMD_EN (0x1 << 11) 105*16020011SCédric Le Goater #define I2CD_S_ALT_EN (0x1 << 10) 106*16020011SCédric Le Goater #define I2CD_RX_DMA_ENABLE (0x1 << 9) 107*16020011SCédric Le Goater #define I2CD_TX_DMA_ENABLE (0x1 << 8) 108*16020011SCédric Le Goater 109*16020011SCédric Le Goater /* Command Bit */ 110*16020011SCédric Le Goater #define I2CD_M_STOP_CMD (0x1 << 5) 111*16020011SCédric Le Goater #define I2CD_M_S_RX_CMD_LAST (0x1 << 4) 112*16020011SCédric Le Goater #define I2CD_M_RX_CMD (0x1 << 3) 113*16020011SCédric Le Goater #define I2CD_S_TX_CMD (0x1 << 2) 114*16020011SCédric Le Goater #define I2CD_M_TX_CMD (0x1 << 1) 115*16020011SCédric Le Goater #define I2CD_M_START_CMD (0x1) 116*16020011SCédric Le Goater 117*16020011SCédric Le Goater #define I2CD_DEV_ADDR_REG 0x18 /* Slave Device Address */ 118*16020011SCédric Le Goater #define I2CD_BUF_CTRL_REG 0x1c /* Pool Buffer Control */ 119*16020011SCédric Le Goater #define I2CD_BYTE_BUF_REG 0x20 /* Transmit/Receive Byte Buffer */ 120*16020011SCédric Le Goater #define I2CD_BYTE_BUF_TX_SHIFT 0 121*16020011SCédric Le Goater #define I2CD_BYTE_BUF_TX_MASK 0xff 122*16020011SCédric Le Goater #define I2CD_BYTE_BUF_RX_SHIFT 8 123*16020011SCédric Le Goater #define I2CD_BYTE_BUF_RX_MASK 0xff 124*16020011SCédric Le Goater 125*16020011SCédric Le Goater 126*16020011SCédric Le Goater static inline bool aspeed_i2c_bus_is_master(AspeedI2CBus *bus) 127*16020011SCédric Le Goater { 128*16020011SCédric Le Goater return bus->ctrl & I2CD_MASTER_EN; 129*16020011SCédric Le Goater } 130*16020011SCédric Le Goater 131*16020011SCédric Le Goater static inline bool aspeed_i2c_bus_is_enabled(AspeedI2CBus *bus) 132*16020011SCédric Le Goater { 133*16020011SCédric Le Goater return bus->ctrl & (I2CD_MASTER_EN | I2CD_SLAVE_EN); 134*16020011SCédric Le Goater } 135*16020011SCédric Le Goater 136*16020011SCédric Le Goater static inline void aspeed_i2c_bus_raise_interrupt(AspeedI2CBus *bus) 137*16020011SCédric Le Goater { 138*16020011SCédric Le Goater bus->intr_status &= bus->intr_ctrl; 139*16020011SCédric Le Goater if (bus->intr_status) { 140*16020011SCédric Le Goater bus->controller->intr_status |= 1 << bus->id; 141*16020011SCédric Le Goater qemu_irq_raise(bus->controller->irq); 142*16020011SCédric Le Goater } 143*16020011SCédric Le Goater } 144*16020011SCédric Le Goater 145*16020011SCédric Le Goater static uint64_t aspeed_i2c_bus_read(void *opaque, hwaddr offset, 146*16020011SCédric Le Goater unsigned size) 147*16020011SCédric Le Goater { 148*16020011SCédric Le Goater AspeedI2CBus *bus = opaque; 149*16020011SCédric Le Goater 150*16020011SCédric Le Goater switch (offset) { 151*16020011SCédric Le Goater case I2CD_FUN_CTRL_REG: 152*16020011SCédric Le Goater return bus->ctrl; 153*16020011SCédric Le Goater case I2CD_AC_TIMING_REG1: 154*16020011SCédric Le Goater return bus->timing[0]; 155*16020011SCédric Le Goater case I2CD_AC_TIMING_REG2: 156*16020011SCédric Le Goater return bus->timing[1]; 157*16020011SCédric Le Goater case I2CD_INTR_CTRL_REG: 158*16020011SCédric Le Goater return bus->intr_ctrl; 159*16020011SCédric Le Goater case I2CD_INTR_STS_REG: 160*16020011SCédric Le Goater return bus->intr_status; 161*16020011SCédric Le Goater case I2CD_BYTE_BUF_REG: 162*16020011SCédric Le Goater return bus->buf; 163*16020011SCédric Le Goater case I2CD_CMD_REG: 164*16020011SCédric Le Goater return bus->cmd | (i2c_bus_busy(bus->bus) << 16); 165*16020011SCédric Le Goater default: 166*16020011SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, 167*16020011SCédric Le Goater "%s: Bad offset 0x%" HWADDR_PRIx "\n", __func__, offset); 168*16020011SCédric Le Goater return -1; 169*16020011SCédric Le Goater } 170*16020011SCédric Le Goater } 171*16020011SCédric Le Goater 172*16020011SCédric Le Goater static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value) 173*16020011SCédric Le Goater { 174*16020011SCédric Le Goater bus->cmd |= value & 0xFFFF; 175*16020011SCédric Le Goater bus->intr_status = 0; 176*16020011SCédric Le Goater 177*16020011SCédric Le Goater if (bus->cmd & I2CD_M_START_CMD) { 178*16020011SCédric Le Goater if (i2c_start_transfer(bus->bus, extract32(bus->buf, 1, 7), 179*16020011SCédric Le Goater extract32(bus->buf, 0, 1))) { 180*16020011SCédric Le Goater bus->intr_status |= I2CD_INTR_TX_NAK; 181*16020011SCédric Le Goater } else { 182*16020011SCédric Le Goater bus->intr_status |= I2CD_INTR_TX_ACK; 183*16020011SCédric Le Goater } 184*16020011SCédric Le Goater 185*16020011SCédric Le Goater } else if (bus->cmd & I2CD_M_TX_CMD) { 186*16020011SCédric Le Goater if (i2c_send(bus->bus, bus->buf)) { 187*16020011SCédric Le Goater bus->intr_status |= (I2CD_INTR_TX_NAK | I2CD_INTR_ABNORMAL); 188*16020011SCédric Le Goater i2c_end_transfer(bus->bus); 189*16020011SCédric Le Goater } else { 190*16020011SCédric Le Goater bus->intr_status |= I2CD_INTR_TX_ACK; 191*16020011SCédric Le Goater } 192*16020011SCédric Le Goater 193*16020011SCédric Le Goater } else if (bus->cmd & I2CD_M_RX_CMD) { 194*16020011SCédric Le Goater int ret = i2c_recv(bus->bus); 195*16020011SCédric Le Goater if (ret < 0) { 196*16020011SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: read failed\n", __func__); 197*16020011SCédric Le Goater ret = 0xff; 198*16020011SCédric Le Goater } else { 199*16020011SCédric Le Goater bus->intr_status |= I2CD_INTR_RX_DONE; 200*16020011SCédric Le Goater } 201*16020011SCédric Le Goater bus->buf = (ret & I2CD_BYTE_BUF_RX_MASK) << I2CD_BYTE_BUF_RX_SHIFT; 202*16020011SCédric Le Goater } 203*16020011SCédric Le Goater 204*16020011SCédric Le Goater if (bus->cmd & (I2CD_M_STOP_CMD | I2CD_M_S_RX_CMD_LAST)) { 205*16020011SCédric Le Goater if (!i2c_bus_busy(bus->bus)) { 206*16020011SCédric Le Goater bus->intr_status |= I2CD_INTR_ABNORMAL; 207*16020011SCédric Le Goater } else { 208*16020011SCédric Le Goater i2c_end_transfer(bus->bus); 209*16020011SCédric Le Goater bus->intr_status |= I2CD_INTR_NORMAL_STOP; 210*16020011SCédric Le Goater } 211*16020011SCédric Le Goater } 212*16020011SCédric Le Goater 213*16020011SCédric Le Goater /* command is handled, reset it and check for interrupts */ 214*16020011SCédric Le Goater bus->cmd &= ~0xFFFF; 215*16020011SCédric Le Goater aspeed_i2c_bus_raise_interrupt(bus); 216*16020011SCédric Le Goater } 217*16020011SCédric Le Goater 218*16020011SCédric Le Goater static void aspeed_i2c_bus_write(void *opaque, hwaddr offset, 219*16020011SCédric Le Goater uint64_t value, unsigned size) 220*16020011SCédric Le Goater { 221*16020011SCédric Le Goater AspeedI2CBus *bus = opaque; 222*16020011SCédric Le Goater 223*16020011SCédric Le Goater switch (offset) { 224*16020011SCédric Le Goater case I2CD_FUN_CTRL_REG: 225*16020011SCédric Le Goater if (value & I2CD_SLAVE_EN) { 226*16020011SCédric Le Goater qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n", 227*16020011SCédric Le Goater __func__); 228*16020011SCédric Le Goater break; 229*16020011SCédric Le Goater } 230*16020011SCédric Le Goater bus->ctrl = value & 0x0071C3FF; 231*16020011SCédric Le Goater break; 232*16020011SCédric Le Goater case I2CD_AC_TIMING_REG1: 233*16020011SCédric Le Goater bus->timing[0] = value & 0xFFFFF0F; 234*16020011SCédric Le Goater break; 235*16020011SCédric Le Goater case I2CD_AC_TIMING_REG2: 236*16020011SCédric Le Goater bus->timing[1] = value & 0x7; 237*16020011SCédric Le Goater break; 238*16020011SCédric Le Goater case I2CD_INTR_CTRL_REG: 239*16020011SCédric Le Goater bus->intr_ctrl = value & 0x7FFF; 240*16020011SCédric Le Goater break; 241*16020011SCédric Le Goater case I2CD_INTR_STS_REG: 242*16020011SCédric Le Goater bus->intr_status &= ~(value & 0x7FFF); 243*16020011SCédric Le Goater bus->controller->intr_status &= ~(1 << bus->id); 244*16020011SCédric Le Goater qemu_irq_lower(bus->controller->irq); 245*16020011SCédric Le Goater break; 246*16020011SCédric Le Goater case I2CD_DEV_ADDR_REG: 247*16020011SCédric Le Goater qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n", 248*16020011SCédric Le Goater __func__); 249*16020011SCédric Le Goater break; 250*16020011SCédric Le Goater case I2CD_BYTE_BUF_REG: 251*16020011SCédric Le Goater bus->buf = (value & I2CD_BYTE_BUF_TX_MASK) << I2CD_BYTE_BUF_TX_SHIFT; 252*16020011SCédric Le Goater break; 253*16020011SCédric Le Goater case I2CD_CMD_REG: 254*16020011SCédric Le Goater if (!aspeed_i2c_bus_is_enabled(bus)) { 255*16020011SCédric Le Goater break; 256*16020011SCédric Le Goater } 257*16020011SCédric Le Goater 258*16020011SCédric Le Goater if (!aspeed_i2c_bus_is_master(bus)) { 259*16020011SCédric Le Goater qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n", 260*16020011SCédric Le Goater __func__); 261*16020011SCédric Le Goater break; 262*16020011SCédric Le Goater } 263*16020011SCédric Le Goater 264*16020011SCédric Le Goater aspeed_i2c_bus_handle_cmd(bus, value); 265*16020011SCédric Le Goater break; 266*16020011SCédric Le Goater 267*16020011SCédric Le Goater default: 268*16020011SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n", 269*16020011SCédric Le Goater __func__, offset); 270*16020011SCédric Le Goater } 271*16020011SCédric Le Goater } 272*16020011SCédric Le Goater 273*16020011SCédric Le Goater static uint64_t aspeed_i2c_ctrl_read(void *opaque, hwaddr offset, 274*16020011SCédric Le Goater unsigned size) 275*16020011SCédric Le Goater { 276*16020011SCédric Le Goater AspeedI2CState *s = opaque; 277*16020011SCédric Le Goater 278*16020011SCédric Le Goater switch (offset) { 279*16020011SCédric Le Goater case I2C_CTRL_STATUS: 280*16020011SCédric Le Goater return s->intr_status; 281*16020011SCédric Le Goater default: 282*16020011SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n", 283*16020011SCédric Le Goater __func__, offset); 284*16020011SCédric Le Goater break; 285*16020011SCédric Le Goater } 286*16020011SCédric Le Goater 287*16020011SCédric Le Goater return -1; 288*16020011SCédric Le Goater } 289*16020011SCédric Le Goater 290*16020011SCédric Le Goater static void aspeed_i2c_ctrl_write(void *opaque, hwaddr offset, 291*16020011SCédric Le Goater uint64_t value, unsigned size) 292*16020011SCédric Le Goater { 293*16020011SCédric Le Goater switch (offset) { 294*16020011SCédric Le Goater case I2C_CTRL_STATUS: 295*16020011SCédric Le Goater default: 296*16020011SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n", 297*16020011SCédric Le Goater __func__, offset); 298*16020011SCédric Le Goater break; 299*16020011SCédric Le Goater } 300*16020011SCédric Le Goater } 301*16020011SCédric Le Goater 302*16020011SCédric Le Goater static const MemoryRegionOps aspeed_i2c_bus_ops = { 303*16020011SCédric Le Goater .read = aspeed_i2c_bus_read, 304*16020011SCédric Le Goater .write = aspeed_i2c_bus_write, 305*16020011SCédric Le Goater .endianness = DEVICE_LITTLE_ENDIAN, 306*16020011SCédric Le Goater }; 307*16020011SCédric Le Goater 308*16020011SCédric Le Goater static const MemoryRegionOps aspeed_i2c_ctrl_ops = { 309*16020011SCédric Le Goater .read = aspeed_i2c_ctrl_read, 310*16020011SCédric Le Goater .write = aspeed_i2c_ctrl_write, 311*16020011SCédric Le Goater .endianness = DEVICE_LITTLE_ENDIAN, 312*16020011SCédric Le Goater }; 313*16020011SCédric Le Goater 314*16020011SCédric Le Goater static const VMStateDescription aspeed_i2c_bus_vmstate = { 315*16020011SCédric Le Goater .name = TYPE_ASPEED_I2C, 316*16020011SCédric Le Goater .version_id = 1, 317*16020011SCédric Le Goater .minimum_version_id = 1, 318*16020011SCédric Le Goater .fields = (VMStateField[]) { 319*16020011SCédric Le Goater VMSTATE_UINT8(id, AspeedI2CBus), 320*16020011SCédric Le Goater VMSTATE_UINT32(ctrl, AspeedI2CBus), 321*16020011SCédric Le Goater VMSTATE_UINT32_ARRAY(timing, AspeedI2CBus, 2), 322*16020011SCédric Le Goater VMSTATE_UINT32(intr_ctrl, AspeedI2CBus), 323*16020011SCédric Le Goater VMSTATE_UINT32(intr_status, AspeedI2CBus), 324*16020011SCédric Le Goater VMSTATE_UINT32(cmd, AspeedI2CBus), 325*16020011SCédric Le Goater VMSTATE_UINT32(buf, AspeedI2CBus), 326*16020011SCédric Le Goater VMSTATE_END_OF_LIST() 327*16020011SCédric Le Goater } 328*16020011SCédric Le Goater }; 329*16020011SCédric Le Goater 330*16020011SCédric Le Goater static const VMStateDescription aspeed_i2c_vmstate = { 331*16020011SCédric Le Goater .name = TYPE_ASPEED_I2C, 332*16020011SCédric Le Goater .version_id = 1, 333*16020011SCédric Le Goater .minimum_version_id = 1, 334*16020011SCédric Le Goater .fields = (VMStateField[]) { 335*16020011SCédric Le Goater VMSTATE_UINT32(intr_status, AspeedI2CState), 336*16020011SCédric Le Goater VMSTATE_STRUCT_ARRAY(busses, AspeedI2CState, 337*16020011SCédric Le Goater ASPEED_I2C_NR_BUSSES, 1, aspeed_i2c_bus_vmstate, 338*16020011SCédric Le Goater AspeedI2CBus), 339*16020011SCédric Le Goater VMSTATE_END_OF_LIST() 340*16020011SCédric Le Goater } 341*16020011SCédric Le Goater }; 342*16020011SCédric Le Goater 343*16020011SCédric Le Goater static void aspeed_i2c_reset(DeviceState *dev) 344*16020011SCédric Le Goater { 345*16020011SCédric Le Goater int i; 346*16020011SCédric Le Goater AspeedI2CState *s = ASPEED_I2C(dev); 347*16020011SCédric Le Goater 348*16020011SCédric Le Goater s->intr_status = 0; 349*16020011SCédric Le Goater 350*16020011SCédric Le Goater for (i = 0; i < ASPEED_I2C_NR_BUSSES; i++) { 351*16020011SCédric Le Goater s->busses[i].intr_ctrl = 0; 352*16020011SCédric Le Goater s->busses[i].intr_status = 0; 353*16020011SCédric Le Goater s->busses[i].cmd = 0; 354*16020011SCédric Le Goater s->busses[i].buf = 0; 355*16020011SCédric Le Goater i2c_end_transfer(s->busses[i].bus); 356*16020011SCédric Le Goater } 357*16020011SCédric Le Goater } 358*16020011SCédric Le Goater 359*16020011SCédric Le Goater /* 360*16020011SCédric Le Goater * Address Definitions 361*16020011SCédric Le Goater * 362*16020011SCédric Le Goater * 0x000 ... 0x03F: Global Register 363*16020011SCédric Le Goater * 0x040 ... 0x07F: Device 1 364*16020011SCédric Le Goater * 0x080 ... 0x0BF: Device 2 365*16020011SCédric Le Goater * 0x0C0 ... 0x0FF: Device 3 366*16020011SCédric Le Goater * 0x100 ... 0x13F: Device 4 367*16020011SCédric Le Goater * 0x140 ... 0x17F: Device 5 368*16020011SCédric Le Goater * 0x180 ... 0x1BF: Device 6 369*16020011SCédric Le Goater * 0x1C0 ... 0x1FF: Device 7 370*16020011SCédric Le Goater * 0x200 ... 0x2FF: Buffer Pool (unused in linux driver) 371*16020011SCédric Le Goater * 0x300 ... 0x33F: Device 8 372*16020011SCédric Le Goater * 0x340 ... 0x37F: Device 9 373*16020011SCédric Le Goater * 0x380 ... 0x3BF: Device 10 374*16020011SCédric Le Goater * 0x3C0 ... 0x3FF: Device 11 375*16020011SCédric Le Goater * 0x400 ... 0x43F: Device 12 376*16020011SCédric Le Goater * 0x440 ... 0x47F: Device 13 377*16020011SCédric Le Goater * 0x480 ... 0x4BF: Device 14 378*16020011SCédric Le Goater * 0x800 ... 0xFFF: Buffer Pool (unused in linux driver) 379*16020011SCédric Le Goater */ 380*16020011SCédric Le Goater static void aspeed_i2c_realize(DeviceState *dev, Error **errp) 381*16020011SCédric Le Goater { 382*16020011SCédric Le Goater int i; 383*16020011SCédric Le Goater SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 384*16020011SCédric Le Goater AspeedI2CState *s = ASPEED_I2C(dev); 385*16020011SCédric Le Goater 386*16020011SCédric Le Goater sysbus_init_irq(sbd, &s->irq); 387*16020011SCédric Le Goater memory_region_init_io(&s->iomem, OBJECT(s), &aspeed_i2c_ctrl_ops, s, 388*16020011SCédric Le Goater "aspeed.i2c", 0x1000); 389*16020011SCédric Le Goater sysbus_init_mmio(sbd, &s->iomem); 390*16020011SCédric Le Goater 391*16020011SCédric Le Goater for (i = 0; i < ASPEED_I2C_NR_BUSSES; i++) { 392*16020011SCédric Le Goater char name[16]; 393*16020011SCédric Le Goater int offset = i < 7 ? 1 : 5; 394*16020011SCédric Le Goater snprintf(name, sizeof(name), "aspeed.i2c.%d", i); 395*16020011SCédric Le Goater s->busses[i].controller = s; 396*16020011SCédric Le Goater s->busses[i].id = i; 397*16020011SCédric Le Goater s->busses[i].bus = i2c_init_bus(dev, name); 398*16020011SCédric Le Goater memory_region_init_io(&s->busses[i].mr, OBJECT(dev), 399*16020011SCédric Le Goater &aspeed_i2c_bus_ops, &s->busses[i], name, 0x40); 400*16020011SCédric Le Goater memory_region_add_subregion(&s->iomem, 0x40 * (i + offset), 401*16020011SCédric Le Goater &s->busses[i].mr); 402*16020011SCédric Le Goater } 403*16020011SCédric Le Goater } 404*16020011SCédric Le Goater 405*16020011SCédric Le Goater static void aspeed_i2c_class_init(ObjectClass *klass, void *data) 406*16020011SCédric Le Goater { 407*16020011SCédric Le Goater DeviceClass *dc = DEVICE_CLASS(klass); 408*16020011SCédric Le Goater 409*16020011SCédric Le Goater dc->vmsd = &aspeed_i2c_vmstate; 410*16020011SCédric Le Goater dc->reset = aspeed_i2c_reset; 411*16020011SCédric Le Goater dc->realize = aspeed_i2c_realize; 412*16020011SCédric Le Goater dc->desc = "Aspeed I2C Controller"; 413*16020011SCédric Le Goater } 414*16020011SCédric Le Goater 415*16020011SCédric Le Goater static const TypeInfo aspeed_i2c_info = { 416*16020011SCédric Le Goater .name = TYPE_ASPEED_I2C, 417*16020011SCédric Le Goater .parent = TYPE_SYS_BUS_DEVICE, 418*16020011SCédric Le Goater .instance_size = sizeof(AspeedI2CState), 419*16020011SCédric Le Goater .class_init = aspeed_i2c_class_init, 420*16020011SCédric Le Goater }; 421*16020011SCédric Le Goater 422*16020011SCédric Le Goater static void aspeed_i2c_register_types(void) 423*16020011SCédric Le Goater { 424*16020011SCédric Le Goater type_register_static(&aspeed_i2c_info); 425*16020011SCédric Le Goater } 426*16020011SCédric Le Goater 427*16020011SCédric Le Goater type_init(aspeed_i2c_register_types) 428*16020011SCédric Le Goater 429*16020011SCédric Le Goater 430*16020011SCédric Le Goater I2CBus *aspeed_i2c_get_bus(DeviceState *dev, int busnr) 431*16020011SCédric Le Goater { 432*16020011SCédric Le Goater AspeedI2CState *s = ASPEED_I2C(dev); 433*16020011SCédric Le Goater I2CBus *bus = NULL; 434*16020011SCédric Le Goater 435*16020011SCédric Le Goater if (busnr >= 0 && busnr < ASPEED_I2C_NR_BUSSES) { 436*16020011SCédric Le Goater bus = s->busses[busnr].bus; 437*16020011SCédric Le Goater } 438*16020011SCédric Le Goater 439*16020011SCédric Le Goater return bus; 440*16020011SCédric Le Goater } 441