root / hw / pm_smbus.c @ 5c3234c6
History | View | Annotate | Download (4.2 kB)
1 | fc0bdd99 | Isaku Yamahata | /*
|
---|---|---|---|
2 | fc0bdd99 | Isaku Yamahata | * PC SMBus implementation
|
3 | fc0bdd99 | Isaku Yamahata | * splitted from acpi.c
|
4 | fc0bdd99 | Isaku Yamahata | *
|
5 | fc0bdd99 | Isaku Yamahata | * Copyright (c) 2006 Fabrice Bellard
|
6 | fc0bdd99 | Isaku Yamahata | *
|
7 | fc0bdd99 | Isaku Yamahata | * This library is free software; you can redistribute it and/or
|
8 | fc0bdd99 | Isaku Yamahata | * modify it under the terms of the GNU Lesser General Public
|
9 | fc0bdd99 | Isaku Yamahata | * License version 2 as published by the Free Software Foundation.
|
10 | fc0bdd99 | Isaku Yamahata | *
|
11 | fc0bdd99 | Isaku Yamahata | * This library is distributed in the hope that it will be useful,
|
12 | fc0bdd99 | Isaku Yamahata | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | fc0bdd99 | Isaku Yamahata | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | fc0bdd99 | Isaku Yamahata | * Lesser General Public License for more details.
|
15 | fc0bdd99 | Isaku Yamahata | *
|
16 | fc0bdd99 | Isaku Yamahata | * You should have received a copy of the GNU Lesser General Public
|
17 | 1012e960 | Blue Swirl | * License along with this library; if not, see
|
18 | 1012e960 | Blue Swirl | * <http://www.gnu.org/licenses/>.
|
19 | fc0bdd99 | Isaku Yamahata | */
|
20 | fc0bdd99 | Isaku Yamahata | #include "hw.h" |
21 | fc0bdd99 | Isaku Yamahata | #include "pc.h" |
22 | fc0bdd99 | Isaku Yamahata | #include "pm_smbus.h" |
23 | fc0bdd99 | Isaku Yamahata | #include "smbus.h" |
24 | fc0bdd99 | Isaku Yamahata | |
25 | fc0bdd99 | Isaku Yamahata | /* no save/load? */
|
26 | fc0bdd99 | Isaku Yamahata | |
27 | fc0bdd99 | Isaku Yamahata | #define SMBHSTSTS 0x00 |
28 | fc0bdd99 | Isaku Yamahata | #define SMBHSTCNT 0x02 |
29 | fc0bdd99 | Isaku Yamahata | #define SMBHSTCMD 0x03 |
30 | fc0bdd99 | Isaku Yamahata | #define SMBHSTADD 0x04 |
31 | fc0bdd99 | Isaku Yamahata | #define SMBHSTDAT0 0x05 |
32 | fc0bdd99 | Isaku Yamahata | #define SMBHSTDAT1 0x06 |
33 | fc0bdd99 | Isaku Yamahata | #define SMBBLKDAT 0x07 |
34 | fc0bdd99 | Isaku Yamahata | |
35 | b246eebb | Isaku Yamahata | //#define DEBUG
|
36 | b246eebb | Isaku Yamahata | |
37 | b246eebb | Isaku Yamahata | #ifdef DEBUG
|
38 | b246eebb | Isaku Yamahata | # define SMBUS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__) |
39 | b246eebb | Isaku Yamahata | #else
|
40 | b246eebb | Isaku Yamahata | # define SMBUS_DPRINTF(format, ...) do { } while (0) |
41 | b246eebb | Isaku Yamahata | #endif
|
42 | b246eebb | Isaku Yamahata | |
43 | b246eebb | Isaku Yamahata | |
44 | fc0bdd99 | Isaku Yamahata | static void smb_transaction(PMSMBus *s) |
45 | fc0bdd99 | Isaku Yamahata | { |
46 | fc0bdd99 | Isaku Yamahata | uint8_t prot = (s->smb_ctl >> 2) & 0x07; |
47 | fc0bdd99 | Isaku Yamahata | uint8_t read = s->smb_addr & 0x01;
|
48 | fc0bdd99 | Isaku Yamahata | uint8_t cmd = s->smb_cmd; |
49 | fc0bdd99 | Isaku Yamahata | uint8_t addr = s->smb_addr >> 1;
|
50 | fc0bdd99 | Isaku Yamahata | i2c_bus *bus = s->smbus; |
51 | fc0bdd99 | Isaku Yamahata | |
52 | b246eebb | Isaku Yamahata | SMBUS_DPRINTF("SMBus trans addr=0x%02x prot=0x%02x\n", addr, prot);
|
53 | fc0bdd99 | Isaku Yamahata | switch(prot) {
|
54 | fc0bdd99 | Isaku Yamahata | case 0x0: |
55 | fc0bdd99 | Isaku Yamahata | smbus_quick_command(bus, addr, read); |
56 | fc0bdd99 | Isaku Yamahata | break;
|
57 | fc0bdd99 | Isaku Yamahata | case 0x1: |
58 | fc0bdd99 | Isaku Yamahata | if (read) {
|
59 | fc0bdd99 | Isaku Yamahata | s->smb_data0 = smbus_receive_byte(bus, addr); |
60 | fc0bdd99 | Isaku Yamahata | } else {
|
61 | fc0bdd99 | Isaku Yamahata | smbus_send_byte(bus, addr, cmd); |
62 | fc0bdd99 | Isaku Yamahata | } |
63 | fc0bdd99 | Isaku Yamahata | break;
|
64 | fc0bdd99 | Isaku Yamahata | case 0x2: |
65 | fc0bdd99 | Isaku Yamahata | if (read) {
|
66 | fc0bdd99 | Isaku Yamahata | s->smb_data0 = smbus_read_byte(bus, addr, cmd); |
67 | fc0bdd99 | Isaku Yamahata | } else {
|
68 | fc0bdd99 | Isaku Yamahata | smbus_write_byte(bus, addr, cmd, s->smb_data0); |
69 | fc0bdd99 | Isaku Yamahata | } |
70 | fc0bdd99 | Isaku Yamahata | break;
|
71 | fc0bdd99 | Isaku Yamahata | case 0x3: |
72 | fc0bdd99 | Isaku Yamahata | if (read) {
|
73 | fc0bdd99 | Isaku Yamahata | uint16_t val; |
74 | fc0bdd99 | Isaku Yamahata | val = smbus_read_word(bus, addr, cmd); |
75 | fc0bdd99 | Isaku Yamahata | s->smb_data0 = val; |
76 | fc0bdd99 | Isaku Yamahata | s->smb_data1 = val >> 8;
|
77 | fc0bdd99 | Isaku Yamahata | } else {
|
78 | fc0bdd99 | Isaku Yamahata | smbus_write_word(bus, addr, cmd, (s->smb_data1 << 8) | s->smb_data0);
|
79 | fc0bdd99 | Isaku Yamahata | } |
80 | fc0bdd99 | Isaku Yamahata | break;
|
81 | fc0bdd99 | Isaku Yamahata | case 0x5: |
82 | fc0bdd99 | Isaku Yamahata | if (read) {
|
83 | fc0bdd99 | Isaku Yamahata | s->smb_data0 = smbus_read_block(bus, addr, cmd, s->smb_data); |
84 | fc0bdd99 | Isaku Yamahata | } else {
|
85 | fc0bdd99 | Isaku Yamahata | smbus_write_block(bus, addr, cmd, s->smb_data, s->smb_data0); |
86 | fc0bdd99 | Isaku Yamahata | } |
87 | fc0bdd99 | Isaku Yamahata | break;
|
88 | fc0bdd99 | Isaku Yamahata | default:
|
89 | fc0bdd99 | Isaku Yamahata | goto error;
|
90 | fc0bdd99 | Isaku Yamahata | } |
91 | fc0bdd99 | Isaku Yamahata | return;
|
92 | fc0bdd99 | Isaku Yamahata | |
93 | fc0bdd99 | Isaku Yamahata | error:
|
94 | fc0bdd99 | Isaku Yamahata | s->smb_stat |= 0x04;
|
95 | fc0bdd99 | Isaku Yamahata | } |
96 | fc0bdd99 | Isaku Yamahata | |
97 | fc0bdd99 | Isaku Yamahata | void smb_ioport_writeb(void *opaque, uint32_t addr, uint32_t val) |
98 | fc0bdd99 | Isaku Yamahata | { |
99 | fc0bdd99 | Isaku Yamahata | PMSMBus *s = opaque; |
100 | fc0bdd99 | Isaku Yamahata | addr &= 0x3f;
|
101 | b246eebb | Isaku Yamahata | SMBUS_DPRINTF("SMB writeb port=0x%04x val=0x%02x\n", addr, val);
|
102 | fc0bdd99 | Isaku Yamahata | switch(addr) {
|
103 | fc0bdd99 | Isaku Yamahata | case SMBHSTSTS:
|
104 | fc0bdd99 | Isaku Yamahata | s->smb_stat = 0;
|
105 | fc0bdd99 | Isaku Yamahata | s->smb_index = 0;
|
106 | fc0bdd99 | Isaku Yamahata | break;
|
107 | fc0bdd99 | Isaku Yamahata | case SMBHSTCNT:
|
108 | fc0bdd99 | Isaku Yamahata | s->smb_ctl = val; |
109 | fc0bdd99 | Isaku Yamahata | if (val & 0x40) |
110 | fc0bdd99 | Isaku Yamahata | smb_transaction(s); |
111 | fc0bdd99 | Isaku Yamahata | break;
|
112 | fc0bdd99 | Isaku Yamahata | case SMBHSTCMD:
|
113 | fc0bdd99 | Isaku Yamahata | s->smb_cmd = val; |
114 | fc0bdd99 | Isaku Yamahata | break;
|
115 | fc0bdd99 | Isaku Yamahata | case SMBHSTADD:
|
116 | fc0bdd99 | Isaku Yamahata | s->smb_addr = val; |
117 | fc0bdd99 | Isaku Yamahata | break;
|
118 | fc0bdd99 | Isaku Yamahata | case SMBHSTDAT0:
|
119 | fc0bdd99 | Isaku Yamahata | s->smb_data0 = val; |
120 | fc0bdd99 | Isaku Yamahata | break;
|
121 | fc0bdd99 | Isaku Yamahata | case SMBHSTDAT1:
|
122 | fc0bdd99 | Isaku Yamahata | s->smb_data1 = val; |
123 | fc0bdd99 | Isaku Yamahata | break;
|
124 | fc0bdd99 | Isaku Yamahata | case SMBBLKDAT:
|
125 | fc0bdd99 | Isaku Yamahata | s->smb_data[s->smb_index++] = val; |
126 | fc0bdd99 | Isaku Yamahata | if (s->smb_index > 31) |
127 | fc0bdd99 | Isaku Yamahata | s->smb_index = 0;
|
128 | fc0bdd99 | Isaku Yamahata | break;
|
129 | fc0bdd99 | Isaku Yamahata | default:
|
130 | fc0bdd99 | Isaku Yamahata | break;
|
131 | fc0bdd99 | Isaku Yamahata | } |
132 | fc0bdd99 | Isaku Yamahata | } |
133 | fc0bdd99 | Isaku Yamahata | |
134 | fc0bdd99 | Isaku Yamahata | uint32_t smb_ioport_readb(void *opaque, uint32_t addr)
|
135 | fc0bdd99 | Isaku Yamahata | { |
136 | fc0bdd99 | Isaku Yamahata | PMSMBus *s = opaque; |
137 | fc0bdd99 | Isaku Yamahata | uint32_t val; |
138 | fc0bdd99 | Isaku Yamahata | |
139 | fc0bdd99 | Isaku Yamahata | addr &= 0x3f;
|
140 | fc0bdd99 | Isaku Yamahata | switch(addr) {
|
141 | fc0bdd99 | Isaku Yamahata | case SMBHSTSTS:
|
142 | fc0bdd99 | Isaku Yamahata | val = s->smb_stat; |
143 | fc0bdd99 | Isaku Yamahata | break;
|
144 | fc0bdd99 | Isaku Yamahata | case SMBHSTCNT:
|
145 | fc0bdd99 | Isaku Yamahata | s->smb_index = 0;
|
146 | fc0bdd99 | Isaku Yamahata | val = s->smb_ctl & 0x1f;
|
147 | fc0bdd99 | Isaku Yamahata | break;
|
148 | fc0bdd99 | Isaku Yamahata | case SMBHSTCMD:
|
149 | fc0bdd99 | Isaku Yamahata | val = s->smb_cmd; |
150 | fc0bdd99 | Isaku Yamahata | break;
|
151 | fc0bdd99 | Isaku Yamahata | case SMBHSTADD:
|
152 | fc0bdd99 | Isaku Yamahata | val = s->smb_addr; |
153 | fc0bdd99 | Isaku Yamahata | break;
|
154 | fc0bdd99 | Isaku Yamahata | case SMBHSTDAT0:
|
155 | fc0bdd99 | Isaku Yamahata | val = s->smb_data0; |
156 | fc0bdd99 | Isaku Yamahata | break;
|
157 | fc0bdd99 | Isaku Yamahata | case SMBHSTDAT1:
|
158 | fc0bdd99 | Isaku Yamahata | val = s->smb_data1; |
159 | fc0bdd99 | Isaku Yamahata | break;
|
160 | fc0bdd99 | Isaku Yamahata | case SMBBLKDAT:
|
161 | fc0bdd99 | Isaku Yamahata | val = s->smb_data[s->smb_index++]; |
162 | fc0bdd99 | Isaku Yamahata | if (s->smb_index > 31) |
163 | fc0bdd99 | Isaku Yamahata | s->smb_index = 0;
|
164 | fc0bdd99 | Isaku Yamahata | break;
|
165 | fc0bdd99 | Isaku Yamahata | default:
|
166 | fc0bdd99 | Isaku Yamahata | val = 0;
|
167 | fc0bdd99 | Isaku Yamahata | break;
|
168 | fc0bdd99 | Isaku Yamahata | } |
169 | b246eebb | Isaku Yamahata | SMBUS_DPRINTF("SMB readb port=0x%04x val=0x%02x\n", addr, val);
|
170 | fc0bdd99 | Isaku Yamahata | return val;
|
171 | fc0bdd99 | Isaku Yamahata | } |
172 | fc0bdd99 | Isaku Yamahata | |
173 | fc0bdd99 | Isaku Yamahata | void pm_smbus_init(DeviceState *parent, PMSMBus *smb)
|
174 | fc0bdd99 | Isaku Yamahata | { |
175 | fc0bdd99 | Isaku Yamahata | smb->smbus = i2c_init_bus(parent, "i2c");
|
176 | fc0bdd99 | Isaku Yamahata | } |