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
32 #define SMBAUXCTL 0x0d
34 #define STS_HOST_BUSY (1 << 0)
35 #define STS_INTR (1 << 1)
36 #define STS_DEV_ERR (1 << 2)
37 #define STS_BUS_ERR (1 << 3)
38 #define STS_FAILED (1 << 4)
39 #define STS_SMBALERT (1 << 5)
40 #define STS_INUSE_STS (1 << 6)
41 #define STS_BYTE_DONE (1 << 7)
42 /* Signs of successfully transaction end :
43 * ByteDoneStatus = 1 (STS_BYTE_DONE) and INTR = 1 (STS_INTR )
46 #define CTL_INTREN (1 << 0)
47 #define CTL_KILL (1 << 1)
48 #define CTL_LAST_BYTE (1 << 5)
49 #define CTL_START (1 << 6)
50 #define CTL_PEC_EN (1 << 7)
51 #define CTL_RETURN_MASK 0x1f
55 #define PROT_BYTE_DATA 2
56 #define PROT_WORD_DATA 3
57 #define PROT_PROC_CALL 4
58 #define PROT_BLOCK_DATA 5
59 #define PROT_I2C_BLOCK_READ 6
61 #define AUX_PEC (1 << 0)
62 #define AUX_BLK (1 << 1)
68 # define SMBUS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
70 # define SMBUS_DPRINTF(format, ...) do { } while (0)
74 static void smb_transaction(PMSMBus
*s
)
76 uint8_t prot
= (s
->smb_ctl
>> 2) & 0x07;
77 uint8_t read
= s
->smb_addr
& 0x01;
78 uint8_t cmd
= s
->smb_cmd
;
79 uint8_t addr
= s
->smb_addr
>> 1;
80 I2CBus
*bus
= s
->smbus
;
83 SMBUS_DPRINTF("SMBus trans addr=0x%02x prot=0x%02x\n", addr
, prot
);
84 /* Transaction isn't exec if STS_DEV_ERR bit set */
85 if ((s
->smb_stat
& STS_DEV_ERR
) != 0) {
91 ret
= smbus_quick_command(bus
, addr
, read
);
95 ret
= smbus_receive_byte(bus
, addr
);
98 ret
= smbus_send_byte(bus
, addr
, cmd
);
103 ret
= smbus_read_byte(bus
, addr
, cmd
);
106 ret
= smbus_write_byte(bus
, addr
, cmd
, s
->smb_data0
);
112 ret
= smbus_read_word(bus
, addr
, cmd
);
115 ret
= smbus_write_word(bus
, addr
, cmd
,
116 (s
->smb_data1
<< 8) | s
->smb_data0
);
120 case PROT_I2C_BLOCK_READ
:
122 int xfersize
= s
->smb_data0
;
123 if (xfersize
> sizeof(s
->smb_data
)) {
124 xfersize
= sizeof(s
->smb_data
);
126 ret
= smbus_read_block(bus
, addr
, s
->smb_data1
, s
->smb_data
,
127 xfersize
, false, true);
130 /* The manual says the behavior is undefined, just set DEV_ERR. */
134 case PROT_BLOCK_DATA
:
136 ret
= smbus_read_block(bus
, addr
, cmd
, s
->smb_data
,
137 sizeof(s
->smb_data
), !s
->i2c_enable
,
144 if (s
->smb_auxctl
& AUX_BLK
) {
145 s
->smb_stat
|= STS_INTR
;
147 s
->smb_blkdata
= s
->smb_data
[0];
148 s
->smb_stat
|= STS_HOST_BUSY
| STS_BYTE_DONE
;
153 if (s
->smb_auxctl
& AUX_BLK
) {
154 if (s
->smb_index
!= s
->smb_data0
) {
158 /* Data is already all written to the queue, just do
161 ret
= smbus_write_block(bus
, addr
, cmd
, s
->smb_data
,
162 s
->smb_data0
, !s
->i2c_enable
);
167 s
->smb_stat
|= STS_INTR
;
168 s
->smb_stat
&= ~STS_HOST_BUSY
;
171 s
->smb_stat
|= STS_HOST_BUSY
| STS_BYTE_DONE
;
172 s
->smb_data
[0] = s
->smb_blkdata
;
188 s
->smb_data1
= ret
>> 8;
198 s
->smb_stat
|= STS_INTR
;
203 s
->smb_stat
|= STS_DEV_ERR
;
207 static void smb_transaction_start(PMSMBus
*s
)
209 if (s
->smb_ctl
& CTL_INTREN
) {
212 /* Do not execute immediately the command; it will be
213 * executed when guest will read SMB_STAT register. This
214 * is to work around a bug in AMIBIOS (that is working
215 * around another bug in some specific hardware) where
216 * it waits for STS_HOST_BUSY to be set before waiting
217 * checking for status. If STS_HOST_BUSY doesn't get
218 * set, it gets stuck. */
219 s
->smb_stat
|= STS_HOST_BUSY
;
224 smb_irq_value(PMSMBus
*s
)
226 return ((s
->smb_stat
& ~STS_HOST_BUSY
) != 0) && (s
->smb_ctl
& CTL_INTREN
);
229 static void smb_ioport_writeb(void *opaque
, hwaddr addr
, uint64_t val
,
234 SMBUS_DPRINTF("SMB writeb port=0x%04" HWADDR_PRIx
235 " val=0x%02" PRIx64
"\n", addr
, val
);
238 s
->smb_stat
&= ~(val
& ~STS_HOST_BUSY
);
239 if (!s
->op_done
&& !(s
->smb_auxctl
& AUX_BLK
)) {
240 uint8_t read
= s
->smb_addr
& 0x01;
243 if (s
->smb_index
>= PM_SMBUS_MAX_MSG_SIZE
) {
246 if (!read
&& s
->smb_index
== s
->smb_data0
) {
247 uint8_t prot
= (s
->smb_ctl
>> 2) & 0x07;
248 uint8_t cmd
= s
->smb_cmd
;
249 uint8_t addr
= s
->smb_addr
>> 1;
252 if (prot
== PROT_I2C_BLOCK_READ
) {
253 s
->smb_stat
|= STS_DEV_ERR
;
257 ret
= smbus_write_block(s
->smbus
, addr
, cmd
, s
->smb_data
,
258 s
->smb_data0
, !s
->i2c_enable
);
260 s
->smb_stat
|= STS_DEV_ERR
;
264 s
->smb_stat
|= STS_INTR
;
265 s
->smb_stat
&= ~STS_HOST_BUSY
;
267 s
->smb_data
[s
->smb_index
] = s
->smb_blkdata
;
268 s
->smb_stat
|= STS_BYTE_DONE
;
269 } else if (s
->smb_ctl
& CTL_LAST_BYTE
) {
271 s
->smb_blkdata
= s
->smb_data
[s
->smb_index
];
273 s
->smb_stat
|= STS_INTR
;
274 s
->smb_stat
&= ~STS_HOST_BUSY
;
276 s
->smb_blkdata
= s
->smb_data
[s
->smb_index
];
277 s
->smb_stat
|= STS_BYTE_DONE
;
282 s
->smb_ctl
= val
& ~CTL_START
; /* CTL_START always reads 0 */
283 if (val
& CTL_START
) {
288 smb_transaction_start(s
);
290 if (s
->smb_ctl
& CTL_KILL
) {
293 s
->smb_stat
|= STS_FAILED
;
294 s
->smb_stat
&= ~STS_HOST_BUSY
;
310 if (s
->smb_index
>= PM_SMBUS_MAX_MSG_SIZE
) {
313 if (s
->smb_auxctl
& AUX_BLK
) {
314 s
->smb_data
[s
->smb_index
++] = val
;
316 s
->smb_blkdata
= val
;
320 s
->smb_auxctl
= val
& AUX_MASK
;
328 s
->set_irq(s
, smb_irq_value(s
));
332 static uint64_t smb_ioport_readb(void *opaque
, hwaddr addr
, unsigned width
)
340 if (s
->smb_stat
& STS_HOST_BUSY
) {
341 /* execute command now */
342 s
->smb_stat
&= ~STS_HOST_BUSY
;
347 val
= s
->smb_ctl
& CTL_RETURN_MASK
;
362 if (s
->smb_index
>= PM_SMBUS_MAX_MSG_SIZE
) {
365 if (s
->smb_auxctl
& AUX_BLK
) {
366 val
= s
->smb_data
[s
->smb_index
++];
367 if (!s
->op_done
&& s
->smb_index
== s
->smb_data0
) {
370 s
->smb_stat
&= ~STS_HOST_BUSY
;
373 val
= s
->smb_blkdata
;
383 SMBUS_DPRINTF("SMB readb port=0x%04" HWADDR_PRIx
" val=0x%02x\n",
387 s
->set_irq(s
, smb_irq_value(s
));
393 static void pm_smbus_reset(PMSMBus
*s
)
400 static const MemoryRegionOps pm_smbus_ops
= {
401 .read
= smb_ioport_readb
,
402 .write
= smb_ioport_writeb
,
403 .valid
.min_access_size
= 1,
404 .valid
.max_access_size
= 1,
405 .endianness
= DEVICE_LITTLE_ENDIAN
,
408 void pm_smbus_init(DeviceState
*parent
, PMSMBus
*smb
, bool force_aux_blk
)
411 smb
->reset
= pm_smbus_reset
;
412 smb
->smbus
= i2c_init_bus(parent
, "i2c");
414 smb
->smb_auxctl
|= AUX_BLK
;
416 memory_region_init_io(&smb
->io
, OBJECT(parent
), &pm_smbus_ops
, smb
,