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/boards.h"
23 #include "hw/i2c/pm_smbus.h"
24 #include "hw/i2c/smbus_master.h"
26 #define SMBHSTSTS 0x00
27 #define SMBHSTCNT 0x02
28 #define SMBHSTCMD 0x03
29 #define SMBHSTADD 0x04
30 #define SMBHSTDAT0 0x05
31 #define SMBHSTDAT1 0x06
32 #define SMBBLKDAT 0x07
33 #define SMBAUXCTL 0x0d
35 #define STS_HOST_BUSY (1 << 0)
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 )
47 #define CTL_INTREN (1 << 0)
48 #define CTL_KILL (1 << 1)
49 #define CTL_LAST_BYTE (1 << 5)
50 #define CTL_START (1 << 6)
51 #define CTL_PEC_EN (1 << 7)
52 #define CTL_RETURN_MASK 0x1f
56 #define PROT_BYTE_DATA 2
57 #define PROT_WORD_DATA 3
58 #define PROT_PROC_CALL 4
59 #define PROT_BLOCK_DATA 5
60 #define PROT_I2C_BLOCK_READ 6
62 #define AUX_PEC (1 << 0)
63 #define AUX_BLK (1 << 1)
69 # define SMBUS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
71 # define SMBUS_DPRINTF(format, ...) do { } while (0)
75 static void smb_transaction(PMSMBus
*s
)
77 uint8_t prot
= (s
->smb_ctl
>> 2) & 0x07;
78 uint8_t read
= s
->smb_addr
& 0x01;
79 uint8_t cmd
= s
->smb_cmd
;
80 uint8_t addr
= s
->smb_addr
>> 1;
81 I2CBus
*bus
= s
->smbus
;
84 SMBUS_DPRINTF("SMBus trans addr=0x%02x prot=0x%02x\n", addr
, prot
);
85 /* Transaction isn't exec if STS_DEV_ERR bit set */
86 if ((s
->smb_stat
& STS_DEV_ERR
) != 0) {
92 ret
= smbus_quick_command(bus
, addr
, read
);
96 ret
= smbus_receive_byte(bus
, addr
);
99 ret
= smbus_send_byte(bus
, addr
, cmd
);
104 ret
= smbus_read_byte(bus
, addr
, cmd
);
107 ret
= smbus_write_byte(bus
, addr
, cmd
, s
->smb_data0
);
113 ret
= smbus_read_word(bus
, addr
, cmd
);
116 ret
= smbus_write_word(bus
, addr
, cmd
,
117 (s
->smb_data1
<< 8) | s
->smb_data0
);
121 case PROT_I2C_BLOCK_READ
:
122 /* According to the Linux i2c-i801 driver:
123 * NB: page 240 of ICH5 datasheet shows that the R/#W
124 * bit should be cleared here, even when reading.
125 * However if SPD Write Disable is set (Lynx Point and later),
126 * the read will fail if we don't set the R/#W bit.
127 * So at least Linux may or may not set the read bit here.
128 * So just ignore the read bit for this command.
130 if (i2c_start_transfer(bus
, addr
, 0)) {
133 ret
= i2c_send(bus
, s
->smb_data1
);
137 if (i2c_start_transfer(bus
, addr
, 1)) {
140 s
->in_i2c_block_read
= true;
141 s
->smb_blkdata
= i2c_recv(s
->smbus
);
143 s
->smb_stat
|= STS_HOST_BUSY
| STS_BYTE_DONE
;
146 case PROT_BLOCK_DATA
:
148 ret
= smbus_read_block(bus
, addr
, cmd
, s
->smb_data
,
149 sizeof(s
->smb_data
), !s
->i2c_enable
,
156 if (s
->smb_auxctl
& AUX_BLK
) {
157 s
->smb_stat
|= STS_INTR
;
159 s
->smb_blkdata
= s
->smb_data
[0];
160 s
->smb_stat
|= STS_HOST_BUSY
| STS_BYTE_DONE
;
165 if (s
->smb_auxctl
& AUX_BLK
) {
166 if (s
->smb_index
!= s
->smb_data0
) {
170 /* Data is already all written to the queue, just do
173 ret
= smbus_write_block(bus
, addr
, cmd
, s
->smb_data
,
174 s
->smb_data0
, !s
->i2c_enable
);
179 s
->smb_stat
|= STS_INTR
;
180 s
->smb_stat
&= ~STS_HOST_BUSY
;
183 s
->smb_stat
|= STS_HOST_BUSY
| STS_BYTE_DONE
;
184 s
->smb_data
[0] = s
->smb_blkdata
;
200 s
->smb_data1
= ret
>> 8;
210 s
->smb_stat
|= STS_INTR
;
215 s
->smb_stat
|= STS_DEV_ERR
;
219 static void smb_transaction_start(PMSMBus
*s
)
221 if (s
->smb_ctl
& CTL_INTREN
) {
223 s
->start_transaction_on_status_read
= false;
225 /* Do not execute immediately the command; it will be
226 * executed when guest will read SMB_STAT register. This
227 * is to work around a bug in AMIBIOS (that is working
228 * around another bug in some specific hardware) where
229 * it waits for STS_HOST_BUSY to be set before waiting
230 * checking for status. If STS_HOST_BUSY doesn't get
231 * set, it gets stuck. */
232 s
->smb_stat
|= STS_HOST_BUSY
;
233 s
->start_transaction_on_status_read
= true;
238 smb_irq_value(PMSMBus
*s
)
240 return ((s
->smb_stat
& ~STS_HOST_BUSY
) != 0) && (s
->smb_ctl
& CTL_INTREN
);
244 smb_byte_by_byte(PMSMBus
*s
)
249 if (s
->in_i2c_block_read
) {
252 return !(s
->smb_auxctl
& AUX_BLK
);
255 static void smb_ioport_writeb(void *opaque
, hwaddr addr
, uint64_t val
,
259 uint8_t clear_byte_done
;
261 SMBUS_DPRINTF("SMB writeb port=0x%04" HWADDR_PRIx
262 " val=0x%02" PRIx64
"\n", addr
, val
);
265 clear_byte_done
= s
->smb_stat
& val
& STS_BYTE_DONE
;
266 s
->smb_stat
&= ~(val
& ~STS_HOST_BUSY
);
267 if (clear_byte_done
&& smb_byte_by_byte(s
)) {
268 uint8_t read
= s
->smb_addr
& 0x01;
270 if (s
->in_i2c_block_read
) {
271 /* See comment below PROT_I2C_BLOCK_READ above. */
276 if (s
->smb_index
>= PM_SMBUS_MAX_MSG_SIZE
) {
279 if (!read
&& s
->smb_index
== s
->smb_data0
) {
280 uint8_t prot
= (s
->smb_ctl
>> 2) & 0x07;
281 uint8_t cmd
= s
->smb_cmd
;
282 uint8_t addr
= s
->smb_addr
>> 1;
285 if (prot
== PROT_I2C_BLOCK_READ
) {
286 s
->smb_stat
|= STS_DEV_ERR
;
290 ret
= smbus_write_block(s
->smbus
, addr
, cmd
, s
->smb_data
,
291 s
->smb_data0
, !s
->i2c_enable
);
293 s
->smb_stat
|= STS_DEV_ERR
;
297 s
->smb_stat
|= STS_INTR
;
298 s
->smb_stat
&= ~STS_HOST_BUSY
;
300 s
->smb_data
[s
->smb_index
] = s
->smb_blkdata
;
301 s
->smb_stat
|= STS_BYTE_DONE
;
302 } else if (s
->smb_ctl
& CTL_LAST_BYTE
) {
304 if (s
->in_i2c_block_read
) {
305 s
->in_i2c_block_read
= false;
306 s
->smb_blkdata
= i2c_recv(s
->smbus
);
308 i2c_end_transfer(s
->smbus
);
310 s
->smb_blkdata
= s
->smb_data
[s
->smb_index
];
313 s
->smb_stat
|= STS_INTR
;
314 s
->smb_stat
&= ~STS_HOST_BUSY
;
316 if (s
->in_i2c_block_read
) {
317 s
->smb_blkdata
= i2c_recv(s
->smbus
);
319 s
->smb_blkdata
= s
->smb_data
[s
->smb_index
];
321 s
->smb_stat
|= STS_BYTE_DONE
;
326 s
->smb_ctl
= val
& ~CTL_START
; /* CTL_START always reads 0 */
327 if (val
& CTL_START
) {
331 if (s
->in_i2c_block_read
) {
332 s
->in_i2c_block_read
= false;
333 i2c_end_transfer(s
->smbus
);
336 smb_transaction_start(s
);
338 if (s
->smb_ctl
& CTL_KILL
) {
341 s
->smb_stat
|= STS_FAILED
;
342 s
->smb_stat
&= ~STS_HOST_BUSY
;
358 if (s
->smb_index
>= PM_SMBUS_MAX_MSG_SIZE
) {
361 if (s
->smb_auxctl
& AUX_BLK
) {
362 s
->smb_data
[s
->smb_index
++] = val
;
364 s
->smb_blkdata
= val
;
368 s
->smb_auxctl
= val
& AUX_MASK
;
376 s
->set_irq(s
, smb_irq_value(s
));
380 static uint64_t smb_ioport_readb(void *opaque
, hwaddr addr
, unsigned width
)
388 if (s
->start_transaction_on_status_read
) {
389 /* execute command now */
390 s
->start_transaction_on_status_read
= false;
391 s
->smb_stat
&= ~STS_HOST_BUSY
;
396 val
= s
->smb_ctl
& CTL_RETURN_MASK
;
411 if (s
->smb_auxctl
& AUX_BLK
&& !s
->in_i2c_block_read
) {
412 if (s
->smb_index
>= PM_SMBUS_MAX_MSG_SIZE
) {
415 val
= s
->smb_data
[s
->smb_index
++];
416 if (!s
->op_done
&& s
->smb_index
== s
->smb_data0
) {
419 s
->smb_stat
&= ~STS_HOST_BUSY
;
422 val
= s
->smb_blkdata
;
432 SMBUS_DPRINTF("SMB readb port=0x%04" HWADDR_PRIx
" val=0x%02x\n",
436 s
->set_irq(s
, smb_irq_value(s
));
442 static void pm_smbus_reset(PMSMBus
*s
)
449 static const MemoryRegionOps pm_smbus_ops
= {
450 .read
= smb_ioport_readb
,
451 .write
= smb_ioport_writeb
,
452 .valid
.min_access_size
= 1,
453 .valid
.max_access_size
= 1,
454 .endianness
= DEVICE_LITTLE_ENDIAN
,
457 bool pm_smbus_vmstate_needed(void)
459 MachineClass
*mc
= MACHINE_GET_CLASS(qdev_get_machine());
461 return !mc
->smbus_no_migration_support
;
464 const VMStateDescription pmsmb_vmstate
= {
467 .minimum_version_id
= 1,
468 .fields
= (VMStateField
[]) {
469 VMSTATE_UINT8(smb_stat
, PMSMBus
),
470 VMSTATE_UINT8(smb_ctl
, PMSMBus
),
471 VMSTATE_UINT8(smb_cmd
, PMSMBus
),
472 VMSTATE_UINT8(smb_addr
, PMSMBus
),
473 VMSTATE_UINT8(smb_data0
, PMSMBus
),
474 VMSTATE_UINT8(smb_data1
, PMSMBus
),
475 VMSTATE_UINT32(smb_index
, PMSMBus
),
476 VMSTATE_UINT8_ARRAY(smb_data
, PMSMBus
, PM_SMBUS_MAX_MSG_SIZE
),
477 VMSTATE_UINT8(smb_auxctl
, PMSMBus
),
478 VMSTATE_UINT8(smb_blkdata
, PMSMBus
),
479 VMSTATE_BOOL(i2c_enable
, PMSMBus
),
480 VMSTATE_BOOL(op_done
, PMSMBus
),
481 VMSTATE_BOOL(in_i2c_block_read
, PMSMBus
),
482 VMSTATE_BOOL(start_transaction_on_status_read
, PMSMBus
),
483 VMSTATE_END_OF_LIST()
487 void pm_smbus_init(DeviceState
*parent
, PMSMBus
*smb
, bool force_aux_blk
)
490 smb
->reset
= pm_smbus_reset
;
491 smb
->smbus
= i2c_init_bus(parent
, "i2c");
493 smb
->smb_auxctl
|= AUX_BLK
;
495 memory_region_init_io(&smb
->io
, OBJECT(parent
), &pm_smbus_ops
, smb
,