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 "qemu/osdep.h"
22 #include "hw/boards.h"
23 #include "hw/i2c/pm_smbus.h"
24 #include "hw/i2c/smbus_master.h"
25 #include "migration/vmstate.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
34 #define SMBAUXCTL 0x0d
36 #define STS_HOST_BUSY (1 << 0)
37 #define STS_INTR (1 << 1)
38 #define STS_DEV_ERR (1 << 2)
39 #define STS_BUS_ERR (1 << 3)
40 #define STS_FAILED (1 << 4)
41 #define STS_SMBALERT (1 << 5)
42 #define STS_INUSE_STS (1 << 6)
43 #define STS_BYTE_DONE (1 << 7)
44 /* Signs of successfully transaction end :
45 * ByteDoneStatus = 1 (STS_BYTE_DONE) and INTR = 1 (STS_INTR )
48 #define CTL_INTREN (1 << 0)
49 #define CTL_KILL (1 << 1)
50 #define CTL_LAST_BYTE (1 << 5)
51 #define CTL_START (1 << 6)
52 #define CTL_PEC_EN (1 << 7)
53 #define CTL_RETURN_MASK 0x1f
57 #define PROT_BYTE_DATA 2
58 #define PROT_WORD_DATA 3
59 #define PROT_PROC_CALL 4
60 #define PROT_BLOCK_DATA 5
61 #define PROT_I2C_BLOCK_READ 6
63 #define AUX_PEC (1 << 0)
64 #define AUX_BLK (1 << 1)
70 # define SMBUS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
72 # define SMBUS_DPRINTF(format, ...) do { } while (0)
76 static void smb_transaction(PMSMBus
*s
)
78 uint8_t prot
= (s
->smb_ctl
>> 2) & 0x07;
79 uint8_t read
= s
->smb_addr
& 0x01;
80 uint8_t cmd
= s
->smb_cmd
;
81 uint8_t addr
= s
->smb_addr
>> 1;
82 I2CBus
*bus
= s
->smbus
;
85 SMBUS_DPRINTF("SMBus trans addr=0x%02x prot=0x%02x\n", addr
, prot
);
86 /* Transaction isn't exec if STS_DEV_ERR bit set */
87 if ((s
->smb_stat
& STS_DEV_ERR
) != 0) {
93 ret
= smbus_quick_command(bus
, addr
, read
);
97 ret
= smbus_receive_byte(bus
, addr
);
100 ret
= smbus_send_byte(bus
, addr
, cmd
);
105 ret
= smbus_read_byte(bus
, addr
, cmd
);
108 ret
= smbus_write_byte(bus
, addr
, cmd
, s
->smb_data0
);
114 ret
= smbus_read_word(bus
, addr
, cmd
);
117 ret
= smbus_write_word(bus
, addr
, cmd
,
118 (s
->smb_data1
<< 8) | s
->smb_data0
);
122 case PROT_I2C_BLOCK_READ
:
123 /* According to the Linux i2c-i801 driver:
124 * NB: page 240 of ICH5 datasheet shows that the R/#W
125 * bit should be cleared here, even when reading.
126 * However if SPD Write Disable is set (Lynx Point and later),
127 * the read will fail if we don't set the R/#W bit.
128 * So at least Linux may or may not set the read bit here.
129 * So just ignore the read bit for this command.
131 if (i2c_start_transfer(bus
, addr
, 0)) {
134 ret
= i2c_send(bus
, s
->smb_data1
);
138 if (i2c_start_transfer(bus
, addr
, 1)) {
141 s
->in_i2c_block_read
= true;
142 s
->smb_blkdata
= i2c_recv(s
->smbus
);
144 s
->smb_stat
|= STS_HOST_BUSY
| STS_BYTE_DONE
;
147 case PROT_BLOCK_DATA
:
149 ret
= smbus_read_block(bus
, addr
, cmd
, s
->smb_data
,
150 sizeof(s
->smb_data
), !s
->i2c_enable
,
157 if (s
->smb_auxctl
& AUX_BLK
) {
158 s
->smb_stat
|= STS_INTR
;
160 s
->smb_blkdata
= s
->smb_data
[0];
161 s
->smb_stat
|= STS_HOST_BUSY
| STS_BYTE_DONE
;
166 if (s
->smb_auxctl
& AUX_BLK
) {
167 if (s
->smb_index
!= s
->smb_data0
) {
171 /* Data is already all written to the queue, just do
174 ret
= smbus_write_block(bus
, addr
, cmd
, s
->smb_data
,
175 s
->smb_data0
, !s
->i2c_enable
);
180 s
->smb_stat
|= STS_INTR
;
181 s
->smb_stat
&= ~STS_HOST_BUSY
;
184 s
->smb_stat
|= STS_HOST_BUSY
| STS_BYTE_DONE
;
185 s
->smb_data
[0] = s
->smb_blkdata
;
201 s
->smb_data1
= ret
>> 8;
211 s
->smb_stat
|= STS_INTR
;
216 s
->smb_stat
|= STS_DEV_ERR
;
220 static void smb_transaction_start(PMSMBus
*s
)
222 if (s
->smb_ctl
& CTL_INTREN
) {
224 s
->start_transaction_on_status_read
= false;
226 /* Do not execute immediately the command; it will be
227 * executed when guest will read SMB_STAT register. This
228 * is to work around a bug in AMIBIOS (that is working
229 * around another bug in some specific hardware) where
230 * it waits for STS_HOST_BUSY to be set before waiting
231 * checking for status. If STS_HOST_BUSY doesn't get
232 * set, it gets stuck. */
233 s
->smb_stat
|= STS_HOST_BUSY
;
234 s
->start_transaction_on_status_read
= true;
239 smb_irq_value(PMSMBus
*s
)
241 return ((s
->smb_stat
& ~STS_HOST_BUSY
) != 0) && (s
->smb_ctl
& CTL_INTREN
);
245 smb_byte_by_byte(PMSMBus
*s
)
250 if (s
->in_i2c_block_read
) {
253 return !(s
->smb_auxctl
& AUX_BLK
);
256 static void smb_ioport_writeb(void *opaque
, hwaddr addr
, uint64_t val
,
260 uint8_t clear_byte_done
;
262 SMBUS_DPRINTF("SMB writeb port=0x%04" HWADDR_PRIx
263 " val=0x%02" PRIx64
"\n", addr
, val
);
266 clear_byte_done
= s
->smb_stat
& val
& STS_BYTE_DONE
;
267 s
->smb_stat
&= ~(val
& ~STS_HOST_BUSY
);
268 if (clear_byte_done
&& smb_byte_by_byte(s
)) {
269 uint8_t read
= s
->smb_addr
& 0x01;
271 if (s
->in_i2c_block_read
) {
272 /* See comment below PROT_I2C_BLOCK_READ above. */
277 if (s
->smb_index
>= PM_SMBUS_MAX_MSG_SIZE
) {
280 if (!read
&& s
->smb_index
== s
->smb_data0
) {
281 uint8_t prot
= (s
->smb_ctl
>> 2) & 0x07;
282 uint8_t cmd
= s
->smb_cmd
;
283 uint8_t addr
= s
->smb_addr
>> 1;
286 if (prot
== PROT_I2C_BLOCK_READ
) {
287 s
->smb_stat
|= STS_DEV_ERR
;
291 ret
= smbus_write_block(s
->smbus
, addr
, cmd
, s
->smb_data
,
292 s
->smb_data0
, !s
->i2c_enable
);
294 s
->smb_stat
|= STS_DEV_ERR
;
298 s
->smb_stat
|= STS_INTR
;
299 s
->smb_stat
&= ~STS_HOST_BUSY
;
301 s
->smb_data
[s
->smb_index
] = s
->smb_blkdata
;
302 s
->smb_stat
|= STS_BYTE_DONE
;
303 } else if (s
->smb_ctl
& CTL_LAST_BYTE
) {
305 if (s
->in_i2c_block_read
) {
306 s
->in_i2c_block_read
= false;
307 s
->smb_blkdata
= i2c_recv(s
->smbus
);
309 i2c_end_transfer(s
->smbus
);
311 s
->smb_blkdata
= s
->smb_data
[s
->smb_index
];
314 s
->smb_stat
|= STS_INTR
;
315 s
->smb_stat
&= ~STS_HOST_BUSY
;
317 if (s
->in_i2c_block_read
) {
318 s
->smb_blkdata
= i2c_recv(s
->smbus
);
320 s
->smb_blkdata
= s
->smb_data
[s
->smb_index
];
322 s
->smb_stat
|= STS_BYTE_DONE
;
327 s
->smb_ctl
= val
& ~CTL_START
; /* CTL_START always reads 0 */
328 if (val
& CTL_START
) {
332 if (s
->in_i2c_block_read
) {
333 s
->in_i2c_block_read
= false;
334 i2c_end_transfer(s
->smbus
);
337 smb_transaction_start(s
);
339 if (s
->smb_ctl
& CTL_KILL
) {
342 s
->smb_stat
|= STS_FAILED
;
343 s
->smb_stat
&= ~STS_HOST_BUSY
;
359 if (s
->smb_index
>= PM_SMBUS_MAX_MSG_SIZE
) {
362 if (s
->smb_auxctl
& AUX_BLK
) {
363 s
->smb_data
[s
->smb_index
++] = val
;
365 s
->smb_blkdata
= val
;
369 s
->smb_auxctl
= val
& AUX_MASK
;
377 s
->set_irq(s
, smb_irq_value(s
));
381 static uint64_t smb_ioport_readb(void *opaque
, hwaddr addr
, unsigned width
)
389 if (s
->start_transaction_on_status_read
) {
390 /* execute command now */
391 s
->start_transaction_on_status_read
= false;
392 s
->smb_stat
&= ~STS_HOST_BUSY
;
397 val
= s
->smb_ctl
& CTL_RETURN_MASK
;
412 if (s
->smb_auxctl
& AUX_BLK
&& !s
->in_i2c_block_read
) {
413 if (s
->smb_index
>= PM_SMBUS_MAX_MSG_SIZE
) {
416 val
= s
->smb_data
[s
->smb_index
++];
417 if (!s
->op_done
&& s
->smb_index
== s
->smb_data0
) {
420 s
->smb_stat
&= ~STS_HOST_BUSY
;
423 val
= s
->smb_blkdata
;
433 SMBUS_DPRINTF("SMB readb port=0x%04" HWADDR_PRIx
" val=0x%02x\n",
437 s
->set_irq(s
, smb_irq_value(s
));
443 static void pm_smbus_reset(PMSMBus
*s
)
450 static const MemoryRegionOps pm_smbus_ops
= {
451 .read
= smb_ioport_readb
,
452 .write
= smb_ioport_writeb
,
453 .valid
.min_access_size
= 1,
454 .valid
.max_access_size
= 1,
455 .endianness
= DEVICE_LITTLE_ENDIAN
,
458 bool pm_smbus_vmstate_needed(void)
460 MachineClass
*mc
= MACHINE_GET_CLASS(qdev_get_machine());
462 return !mc
->smbus_no_migration_support
;
465 const VMStateDescription pmsmb_vmstate
= {
468 .minimum_version_id
= 1,
469 .fields
= (VMStateField
[]) {
470 VMSTATE_UINT8(smb_stat
, PMSMBus
),
471 VMSTATE_UINT8(smb_ctl
, PMSMBus
),
472 VMSTATE_UINT8(smb_cmd
, PMSMBus
),
473 VMSTATE_UINT8(smb_addr
, PMSMBus
),
474 VMSTATE_UINT8(smb_data0
, PMSMBus
),
475 VMSTATE_UINT8(smb_data1
, PMSMBus
),
476 VMSTATE_UINT32(smb_index
, PMSMBus
),
477 VMSTATE_UINT8_ARRAY(smb_data
, PMSMBus
, PM_SMBUS_MAX_MSG_SIZE
),
478 VMSTATE_UINT8(smb_auxctl
, PMSMBus
),
479 VMSTATE_UINT8(smb_blkdata
, PMSMBus
),
480 VMSTATE_BOOL(i2c_enable
, PMSMBus
),
481 VMSTATE_BOOL(op_done
, PMSMBus
),
482 VMSTATE_BOOL(in_i2c_block_read
, PMSMBus
),
483 VMSTATE_BOOL(start_transaction_on_status_read
, PMSMBus
),
484 VMSTATE_END_OF_LIST()
488 void pm_smbus_init(DeviceState
*parent
, PMSMBus
*smb
, bool force_aux_blk
)
491 smb
->reset
= pm_smbus_reset
;
492 smb
->smbus
= i2c_init_bus(parent
, "i2c");
494 smb
->smb_auxctl
|= AUX_BLK
;
496 memory_region_init_io(&smb
->io
, OBJECT(parent
), &pm_smbus_ops
, smb
,