pm_smbus: remove #ifdef DEBUG.
[qemu/aliguori-queue.git] / hw / pm_smbus.c
blob9929d721ebbca565c0f48d5d79c25245ce4e5e4e
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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
20 #include "hw.h"
21 #include "pc.h"
22 #include "pm_smbus.h"
23 #include "pci.h"
24 #include "qemu-timer.h"
25 #include "sysemu.h"
26 #include "i2c.h"
27 #include "smbus.h"
28 #include "kvm.h"
30 /* no save/load? */
32 #define SMBHSTSTS 0x00
33 #define SMBHSTCNT 0x02
34 #define SMBHSTCMD 0x03
35 #define SMBHSTADD 0x04
36 #define SMBHSTDAT0 0x05
37 #define SMBHSTDAT1 0x06
38 #define SMBBLKDAT 0x07
40 //#define DEBUG
42 #ifdef DEBUG
43 # define SMBUS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
44 #else
45 # define SMBUS_DPRINTF(format, ...) do { } while (0)
46 #endif
49 static void smb_transaction(PMSMBus *s)
51 uint8_t prot = (s->smb_ctl >> 2) & 0x07;
52 uint8_t read = s->smb_addr & 0x01;
53 uint8_t cmd = s->smb_cmd;
54 uint8_t addr = s->smb_addr >> 1;
55 i2c_bus *bus = s->smbus;
57 SMBUS_DPRINTF("SMBus trans addr=0x%02x prot=0x%02x\n", addr, prot);
58 switch(prot) {
59 case 0x0:
60 smbus_quick_command(bus, addr, read);
61 break;
62 case 0x1:
63 if (read) {
64 s->smb_data0 = smbus_receive_byte(bus, addr);
65 } else {
66 smbus_send_byte(bus, addr, cmd);
68 break;
69 case 0x2:
70 if (read) {
71 s->smb_data0 = smbus_read_byte(bus, addr, cmd);
72 } else {
73 smbus_write_byte(bus, addr, cmd, s->smb_data0);
75 break;
76 case 0x3:
77 if (read) {
78 uint16_t val;
79 val = smbus_read_word(bus, addr, cmd);
80 s->smb_data0 = val;
81 s->smb_data1 = val >> 8;
82 } else {
83 smbus_write_word(bus, addr, cmd, (s->smb_data1 << 8) | s->smb_data0);
85 break;
86 case 0x5:
87 if (read) {
88 s->smb_data0 = smbus_read_block(bus, addr, cmd, s->smb_data);
89 } else {
90 smbus_write_block(bus, addr, cmd, s->smb_data, s->smb_data0);
92 break;
93 default:
94 goto error;
96 return;
98 error:
99 s->smb_stat |= 0x04;
102 void smb_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
104 PMSMBus *s = opaque;
105 addr &= 0x3f;
106 SMBUS_DPRINTF("SMB writeb port=0x%04x val=0x%02x\n", addr, val);
107 switch(addr) {
108 case SMBHSTSTS:
109 s->smb_stat = 0;
110 s->smb_index = 0;
111 break;
112 case SMBHSTCNT:
113 s->smb_ctl = val;
114 if (val & 0x40)
115 smb_transaction(s);
116 break;
117 case SMBHSTCMD:
118 s->smb_cmd = val;
119 break;
120 case SMBHSTADD:
121 s->smb_addr = val;
122 break;
123 case SMBHSTDAT0:
124 s->smb_data0 = val;
125 break;
126 case SMBHSTDAT1:
127 s->smb_data1 = val;
128 break;
129 case SMBBLKDAT:
130 s->smb_data[s->smb_index++] = val;
131 if (s->smb_index > 31)
132 s->smb_index = 0;
133 break;
134 default:
135 break;
139 uint32_t smb_ioport_readb(void *opaque, uint32_t addr)
141 PMSMBus *s = opaque;
142 uint32_t val;
144 addr &= 0x3f;
145 switch(addr) {
146 case SMBHSTSTS:
147 val = s->smb_stat;
148 break;
149 case SMBHSTCNT:
150 s->smb_index = 0;
151 val = s->smb_ctl & 0x1f;
152 break;
153 case SMBHSTCMD:
154 val = s->smb_cmd;
155 break;
156 case SMBHSTADD:
157 val = s->smb_addr;
158 break;
159 case SMBHSTDAT0:
160 val = s->smb_data0;
161 break;
162 case SMBHSTDAT1:
163 val = s->smb_data1;
164 break;
165 case SMBBLKDAT:
166 val = s->smb_data[s->smb_index++];
167 if (s->smb_index > 31)
168 s->smb_index = 0;
169 break;
170 default:
171 val = 0;
172 break;
174 SMBUS_DPRINTF("SMB readb port=0x%04x val=0x%02x\n", addr, val);
175 return val;
178 void pm_smbus_init(DeviceState *parent, PMSMBus *smb)
180 smb->smbus = i2c_init_bus(parent, "i2c");