pc: Set fw_cfg data based on APIC ID calculation
[qemu/rayw.git] / hw / pm_smbus.c
blobea1380ca680d1b4dceae4a923b32ddad5e26bb94
1 /*
2 * PC SMBus implementation
3 * splitted from acpi.c
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 "hw.h"
21 #include "pc.h"
22 #include "pm_smbus.h"
23 #include "smbus.h"
25 /* no save/load? */
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
35 //#define DEBUG
37 #ifdef DEBUG
38 # define SMBUS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
39 #else
40 # define SMBUS_DPRINTF(format, ...) do { } while (0)
41 #endif
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);
53 switch(prot) {
54 case 0x0:
55 smbus_quick_command(bus, addr, read);
56 break;
57 case 0x1:
58 if (read) {
59 s->smb_data0 = smbus_receive_byte(bus, addr);
60 } else {
61 smbus_send_byte(bus, addr, cmd);
63 break;
64 case 0x2:
65 if (read) {
66 s->smb_data0 = smbus_read_byte(bus, addr, cmd);
67 } else {
68 smbus_write_byte(bus, addr, cmd, s->smb_data0);
70 break;
71 case 0x3:
72 if (read) {
73 uint16_t val;
74 val = smbus_read_word(bus, addr, cmd);
75 s->smb_data0 = val;
76 s->smb_data1 = val >> 8;
77 } else {
78 smbus_write_word(bus, addr, cmd, (s->smb_data1 << 8) | s->smb_data0);
80 break;
81 case 0x5:
82 if (read) {
83 s->smb_data0 = smbus_read_block(bus, addr, cmd, s->smb_data);
84 } else {
85 smbus_write_block(bus, addr, cmd, s->smb_data, s->smb_data0);
87 break;
88 default:
89 goto error;
91 return;
93 error:
94 s->smb_stat |= 0x04;
97 static void smb_ioport_writeb(void *opaque, hwaddr addr, uint64_t val,
98 unsigned width)
100 PMSMBus *s = opaque;
102 SMBUS_DPRINTF("SMB writeb port=0x%04x val=0x%02x\n", addr, val);
103 switch(addr) {
104 case SMBHSTSTS:
105 s->smb_stat = 0;
106 s->smb_index = 0;
107 break;
108 case SMBHSTCNT:
109 s->smb_ctl = val;
110 if (val & 0x40)
111 smb_transaction(s);
112 break;
113 case SMBHSTCMD:
114 s->smb_cmd = val;
115 break;
116 case SMBHSTADD:
117 s->smb_addr = val;
118 break;
119 case SMBHSTDAT0:
120 s->smb_data0 = val;
121 break;
122 case SMBHSTDAT1:
123 s->smb_data1 = val;
124 break;
125 case SMBBLKDAT:
126 s->smb_data[s->smb_index++] = val;
127 if (s->smb_index > 31)
128 s->smb_index = 0;
129 break;
130 default:
131 break;
135 static uint64_t smb_ioport_readb(void *opaque, hwaddr addr, unsigned width)
137 PMSMBus *s = opaque;
138 uint32_t val;
140 switch(addr) {
141 case SMBHSTSTS:
142 val = s->smb_stat;
143 break;
144 case SMBHSTCNT:
145 s->smb_index = 0;
146 val = s->smb_ctl & 0x1f;
147 break;
148 case SMBHSTCMD:
149 val = s->smb_cmd;
150 break;
151 case SMBHSTADD:
152 val = s->smb_addr;
153 break;
154 case SMBHSTDAT0:
155 val = s->smb_data0;
156 break;
157 case SMBHSTDAT1:
158 val = s->smb_data1;
159 break;
160 case SMBBLKDAT:
161 val = s->smb_data[s->smb_index++];
162 if (s->smb_index > 31)
163 s->smb_index = 0;
164 break;
165 default:
166 val = 0;
167 break;
169 SMBUS_DPRINTF("SMB readb port=0x%04x val=0x%02x\n", addr, val);
170 return val;
173 static const MemoryRegionOps pm_smbus_ops = {
174 .read = smb_ioport_readb,
175 .write = smb_ioport_writeb,
176 .valid.min_access_size = 1,
177 .valid.max_access_size = 1,
178 .endianness = DEVICE_LITTLE_ENDIAN,
181 void pm_smbus_init(DeviceState *parent, PMSMBus *smb)
183 smb->smbus = i2c_init_bus(parent, "i2c");
184 memory_region_init_io(&smb->io, &pm_smbus_ops, smb, "pm-smbus", 64);