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/>.
20 #include "qemu/osdep.h"
22 #include "hw/i2c/pm_smbus.h"
23 #include "hw/i2c/smbus.h"
25 #define SMBHSTSTS 0x00
26 #define SMBHSTCNT 0x02
27 #define SMBHSTCMD 0x03
28 #define SMBHSTADD 0x04
29 #define SMBHSTDAT0 0x05
30 #define SMBHSTDAT1 0x06
31 #define SMBBLKDAT 0x07
33 #define STS_HOST_BUSY (1 << 0)
34 #define STS_INTR (1 << 1)
35 #define STS_DEV_ERR (1 << 2)
36 #define STS_BUS_ERR (1 << 3)
37 #define STS_FAILED (1 << 4)
38 #define STS_SMBALERT (1 << 5)
39 #define STS_INUSE_STS (1 << 6)
40 #define STS_BYTE_DONE (1 << 7)
41 /* Signs of successfully transaction end :
42 * ByteDoneStatus = 1 (STS_BYTE_DONE) and INTR = 1 (STS_INTR )
45 #define CTL_INTREN (1 << 0)
46 #define CTL_KILL (1 << 1)
47 #define CTL_LAST_BYTE (1 << 5)
48 #define CTL_START (1 << 6)
49 #define CTL_PEC_EN (1 << 7)
50 #define CTL_RETURN_MASK 0x1f
54 #define PROT_BYTE_DATA 2
55 #define PROT_WORD_DATA 3
56 #define PROT_PROC_CALL 4
57 #define PROT_BLOCK_DATA 5
58 #define PROT_I2C_BLOCK_DATA 6
63 # define SMBUS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
65 # define SMBUS_DPRINTF(format, ...) do { } while (0)
69 static void smb_transaction(PMSMBus
*s
)
71 uint8_t prot
= (s
->smb_ctl
>> 2) & 0x07;
72 uint8_t read
= s
->smb_addr
& 0x01;
73 uint8_t cmd
= s
->smb_cmd
;
74 uint8_t addr
= s
->smb_addr
>> 1;
75 I2CBus
*bus
= s
->smbus
;
78 assert(s
->smb_stat
& STS_HOST_BUSY
);
79 s
->smb_stat
&= ~STS_HOST_BUSY
;
81 SMBUS_DPRINTF("SMBus trans addr=0x%02x prot=0x%02x\n", addr
, prot
);
82 /* Transaction isn't exec if STS_DEV_ERR bit set */
83 if ((s
->smb_stat
& STS_DEV_ERR
) != 0) {
89 ret
= smbus_quick_command(bus
, addr
, read
);
93 ret
= smbus_receive_byte(bus
, addr
);
96 ret
= smbus_send_byte(bus
, addr
, cmd
);
101 ret
= smbus_read_byte(bus
, addr
, cmd
);
104 ret
= smbus_write_byte(bus
, addr
, cmd
, s
->smb_data0
);
110 ret
= smbus_read_word(bus
, addr
, cmd
);
113 ret
= smbus_write_word(bus
, addr
, cmd
,
114 (s
->smb_data1
<< 8) | s
->smb_data0
);
118 case PROT_I2C_BLOCK_DATA
:
120 int xfersize
= s
->smb_data0
;
121 if (xfersize
> sizeof(s
->smb_data
)) {
122 xfersize
= sizeof(s
->smb_data
);
124 ret
= smbus_read_block(bus
, addr
, s
->smb_data1
, s
->smb_data
,
125 xfersize
, false, true);
128 ret
= smbus_write_block(bus
, addr
, cmd
, s
->smb_data
, s
->smb_data0
,
142 s
->smb_data1
= ret
>> 8;
152 s
->smb_stat
|= STS_BYTE_DONE
| STS_INTR
;
156 s
->smb_stat
|= STS_DEV_ERR
;
161 static void smb_transaction_start(PMSMBus
*s
)
163 /* Do not execute immediately the command ; it will be
164 * executed when guest will read SMB_STAT register */
165 s
->smb_stat
|= STS_HOST_BUSY
;
168 static void smb_ioport_writeb(void *opaque
, hwaddr addr
, uint64_t val
,
173 SMBUS_DPRINTF("SMB writeb port=0x%04" HWADDR_PRIx
174 " val=0x%02" PRIx64
"\n", addr
, val
);
177 s
->smb_stat
= (~(val
& 0xff)) & s
->smb_stat
;
182 if (s
->smb_ctl
& CTL_START
) {
183 smb_transaction_start(s
);
199 s
->smb_data
[s
->smb_index
++] = val
;
200 if (s
->smb_index
> 31)
208 static uint64_t smb_ioport_readb(void *opaque
, hwaddr addr
, unsigned width
)
216 if (s
->smb_stat
& STS_HOST_BUSY
) {
217 /* execute command now */
223 val
= s
->smb_ctl
& CTL_RETURN_MASK
;
238 val
= s
->smb_data
[s
->smb_index
++];
239 if (s
->smb_index
> 31)
246 SMBUS_DPRINTF("SMB readb port=0x%04" HWADDR_PRIx
" val=0x%02x\n",
252 static const MemoryRegionOps pm_smbus_ops
= {
253 .read
= smb_ioport_readb
,
254 .write
= smb_ioport_writeb
,
255 .valid
.min_access_size
= 1,
256 .valid
.max_access_size
= 1,
257 .endianness
= DEVICE_LITTLE_ENDIAN
,
260 void pm_smbus_init(DeviceState
*parent
, PMSMBus
*smb
)
262 smb
->smbus
= i2c_init_bus(parent
, "i2c");
263 memory_region_init_io(&smb
->io
, OBJECT(parent
), &pm_smbus_ops
, smb
,