2 * PC SMBus implementation
5 * Copyright (c) 2006 Fabrice Bellard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License version 2 as published by the Free Software Foundation.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see
18 * <http://www.gnu.org/licenses/>.
21 #include "hw/i386/pc.h"
22 #include "hw/i2c/pm_smbus.h"
23 #include "hw/i2c/smbus.h"
27 #define SMBHSTSTS 0x00
28 #define SMBHSTCNT 0x02
29 #define SMBHSTCMD 0x03
30 #define SMBHSTADD 0x04
31 #define SMBHSTDAT0 0x05
32 #define SMBHSTDAT1 0x06
33 #define SMBBLKDAT 0x07
35 #define STS_HOST_BUSY (1)
36 #define STS_INTR (1<<1)
37 #define STS_DEV_ERR (1<<2)
38 #define STS_BUS_ERR (1<<3)
39 #define STS_FAILED (1<<4)
40 #define STS_SMBALERT (1<<5)
41 #define STS_INUSE_STS (1<<6)
42 #define STS_BYTE_DONE (1<<7)
43 /* Signs of successfully transaction end :
44 * ByteDoneStatus = 1 (STS_BYTE_DONE) and INTR = 1 (STS_INTR )
50 # define SMBUS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
52 # define SMBUS_DPRINTF(format, ...) do { } while (0)
56 static void smb_transaction(PMSMBus *s)
58 uint8_t prot = (s->smb_ctl >> 2) & 0x07;
59 uint8_t read = s->smb_addr & 0x01;
60 uint8_t cmd = s->smb_cmd;
61 uint8_t addr = s->smb_addr >> 1;
62 I2CBus *bus = s->smbus;
64 SMBUS_DPRINTF("SMBus trans addr=0x%02x prot=0x%02x\n", addr, prot);
65 /* Transaction isn't exec if STS_DEV_ERR bit set */
66 if ((s->smb_stat & STS_DEV_ERR) != 0) {
71 smbus_quick_command(bus, addr, read);
72 s->smb_stat |= STS_BYTE_DONE | STS_INTR;
76 s->smb_data0 = smbus_receive_byte(bus, addr);
78 smbus_send_byte(bus, addr, cmd);
80 s->smb_stat |= STS_BYTE_DONE | STS_INTR;
84 s->smb_data0 = smbus_read_byte(bus, addr, cmd);
86 smbus_write_byte(bus, addr, cmd, s->smb_data0);
88 s->smb_stat |= STS_BYTE_DONE | STS_INTR;
93 val = smbus_read_word(bus, addr, cmd);
95 s->smb_data1 = val >> 8;
97 smbus_write_word(bus, addr, cmd, (s->smb_data1 << 8) | s->smb_data0);
99 s->smb_stat |= STS_BYTE_DONE | STS_INTR;
103 s->smb_data0 = smbus_read_block(bus, addr, cmd, s->smb_data);
105 smbus_write_block(bus, addr, cmd, s->smb_data, s->smb_data0);
107 s->smb_stat |= STS_BYTE_DONE | STS_INTR;
115 s->smb_stat |= STS_DEV_ERR;
118 static void smb_ioport_writeb(void *opaque, hwaddr addr, uint64_t val,
123 SMBUS_DPRINTF("SMB writeb port=0x%04x val=0x%02x\n", addr, val);
126 s->smb_stat = (~(val & 0xff)) & s->smb_stat;
147 s->smb_data[s->smb_index++] = val;
148 if (s->smb_index > 31)
156 static uint64_t smb_ioport_readb(void *opaque, hwaddr addr, unsigned width)
167 val = s->smb_ctl & 0x1f;
182 val = s->smb_data[s->smb_index++];
183 if (s->smb_index > 31)
190 SMBUS_DPRINTF("SMB readb port=0x%04x val=0x%02x\n", addr, val);
194 static const MemoryRegionOps pm_smbus_ops = {
195 .read = smb_ioport_readb,
196 .write = smb_ioport_writeb,
197 .valid.min_access_size = 1,
198 .valid.max_access_size = 1,
199 .endianness = DEVICE_LITTLE_ENDIAN,
202 void pm_smbus_init(DeviceState *parent, PMSMBus *smb)
204 smb->smbus = i2c_init_bus(parent, "i2c");
205 memory_region_init_io(&smb->io, OBJECT(parent), &pm_smbus_ops, smb,