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/>.
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
38 # define SMBUS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
40 # define SMBUS_DPRINTF(format, ...) do { } while (0)
44 static void smb_transaction(PMSMBus
*s
)
46 uint8_t prot
= (s
->smb_ctl
>> 2) & 0x07;
47 uint8_t read
= s
->smb_addr
& 0x01;
48 uint8_t cmd
= s
->smb_cmd
;
49 uint8_t addr
= s
->smb_addr
>> 1;
50 i2c_bus
*bus
= s
->smbus
;
52 SMBUS_DPRINTF("SMBus trans addr=0x%02x prot=0x%02x\n", addr
, prot
);
55 smbus_quick_command(bus
, addr
, read
);
59 s
->smb_data0
= smbus_receive_byte(bus
, addr
);
61 smbus_send_byte(bus
, addr
, cmd
);
66 s
->smb_data0
= smbus_read_byte(bus
, addr
, cmd
);
68 smbus_write_byte(bus
, addr
, cmd
, s
->smb_data0
);
74 val
= smbus_read_word(bus
, addr
, cmd
);
76 s
->smb_data1
= val
>> 8;
78 smbus_write_word(bus
, addr
, cmd
, (s
->smb_data1
<< 8) | s
->smb_data0
);
83 s
->smb_data0
= smbus_read_block(bus
, addr
, cmd
, s
->smb_data
);
85 smbus_write_block(bus
, addr
, cmd
, s
->smb_data
, s
->smb_data0
);
97 void smb_ioport_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
101 SMBUS_DPRINTF("SMB writeb port=0x%04x val=0x%02x\n", addr
, val
);
125 s
->smb_data
[s
->smb_index
++] = val
;
126 if (s
->smb_index
> 31)
134 uint32_t smb_ioport_readb(void *opaque
, uint32_t addr
)
146 val
= s
->smb_ctl
& 0x1f;
161 val
= s
->smb_data
[s
->smb_index
++];
162 if (s
->smb_index
> 31)
169 SMBUS_DPRINTF("SMB readb port=0x%04x val=0x%02x\n", addr
, val
);
173 void pm_smbus_init(DeviceState
*parent
, PMSMBus
*smb
)
175 smb
->smbus
= i2c_init_bus(parent
, "i2c");