1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
/*
* This file is part of the coreboot project.
*
* Copyright (C) 2007 Advanced Micro Devices, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define SMBUS_ERROR -1
#define SMBUS_WAIT_UNTIL_READY_TIMEOUT -2
#define SMBUS_WAIT_UNTIL_DONE_TIMEOUT -3
#define SMB_SDA 0x00
#define SMB_STS 0x01
#define SMB_CTRL_STS 0x02
#define SMB_CTRL1 0x03
#define SMB_ADD 0x04
#define SMB_CTRL2 0x05
#define SMB_CTRL3 0x06
#define SMB_STS_SLVSTP (0x01 << 7)
#define SMB_STS_SDAST (0x01 << 6)
#define SMB_STS_BER (0x01 << 5)
#define SMB_STS_NEGACK (0x01 << 4)
#define SMB_STS_STASTR (0x01 << 3)
#define SMB_STS_NMATCH (0x01 << 2)
#define SMB_STS_MASTER (0x01 << 1)
#define SMB_STS_XMIT (0x01 << 0)
#define SMB_CSTS_TGSCL (0x01 << 5)
#define SMB_CSTS_TSDA (0x01 << 4)
#define SMB_CSTS_GCMTCH (0x01 << 3)
#define SMB_CSTS_MATCH (0x01 << 2)
#define SMB_CSTS_BB (0x01 << 1)
#define SMB_CSTS_BUSY (0x01 << 0)
#define SMB_CTRL1_STASTRE (0x01 << 7)
#define SMB_CTRL1_NMINTE (0x01 << 6)
#define SMB_CTRL1_GCMEN (0x01 << 5)
#define SMB_CTRL1_ACK (0x01 << 4)
#define SMB_CTRL1_RSVD (0x01 << 3)
#define SMB_CTRL1_INTEN (0x01 << 2)
#define SMB_CTRL1_STOP (0x01 << 1)
#define SMB_CTRL1_START (0x01 << 0)
#define SMB_ADD_SAEN (0x01 << 7)
#define SMB_CTRL2_ENABLE 0x01
#define SMBUS_TIMEOUT (100*1000*10)
#define SMBUS_STATUS_MASK 0xfbff
static void smbus_delay(void)
{
inb(0x80);
}
static int smbus_wait(unsigned smbus_io_base)
{
unsigned long loops = SMBUS_TIMEOUT;
unsigned char val;
do {
smbus_delay();
val = inb(smbus_io_base + SMB_STS);
if ((val & SMB_STS_SDAST) != 0)
break;
if (val & (SMB_STS_BER | SMB_STS_NEGACK)) {
printk(BIOS_DEBUG, "SMBUS WAIT ERROR %x\n", val);
return SMBUS_ERROR;
}
} while (--loops);
outb(0, smbus_io_base + SMB_STS);
return loops ? 0 : SMBUS_WAIT_UNTIL_READY_TIMEOUT;
}
static int smbus_write(unsigned smbus_io_base, unsigned char byte)
{
outb(byte, smbus_io_base + SMB_SDA);
return smbus_wait(smbus_io_base);
}
/* generate a smbus start condition */
static int smbus_start_condition(unsigned smbus_io_base)
{
unsigned char val;
/* issue a START condition */
val = inb(smbus_io_base + SMB_CTRL1);
outb(val | SMB_CTRL1_START, smbus_io_base + SMB_CTRL1);
/* check for bus conflict */
val = inb(smbus_io_base + SMB_STS);
if ((val & SMB_STS_BER) != 0)
return SMBUS_ERROR;
return smbus_wait(smbus_io_base);
}
static int smbus_check_stop_condition(unsigned smbus_io_base)
{
unsigned char val;
unsigned long loops;
loops = SMBUS_TIMEOUT;
/* check for SDA status */
do {
smbus_delay();
val = inb(smbus_io_base + SMB_CTRL1);
if ((val & SMB_CTRL1_STOP) == 0) {
break;
}
} while (--loops);
return loops ? 0 : SMBUS_WAIT_UNTIL_READY_TIMEOUT;
/* Make sure everything is cleared and ready to go */
val = inb(smbus_io_base + SMB_CTRL1);
outb(val & ~(SMB_CTRL1_STASTRE | SMB_CTRL1_NMINTE),
smbus_io_base + SMB_CTRL1);
outb(SMB_STS_BER | SMB_STS_NEGACK | SMB_STS_STASTR,
smbus_io_base + SMB_STS);
val = inb(smbus_io_base + SMB_CTRL_STS);
outb(val | SMB_CSTS_BB, smbus_io_base + SMB_CTRL_STS);
}
static int smbus_stop_condition(unsigned smbus_io_base)
{
unsigned char val;
val = inb(smbus_io_base + SMB_CTRL1);
outb(SMB_CTRL1_STOP, smbus_io_base + SMB_CTRL1);
return 0;
}
static int smbus_ack(unsigned smbus_io_base, int state)
{
unsigned char val = inb(smbus_io_base + SMB_CTRL1);
if (state)
outb(val | SMB_CTRL1_ACK, smbus_io_base + SMB_CTRL1);
else
outb(val & ~SMB_CTRL1_ACK, smbus_io_base + SMB_CTRL1);
return 0;
}
static int smbus_send_slave_address(unsigned smbus_io_base,
unsigned char device)
{
unsigned char val;
/* send the slave address */
outb(device, smbus_io_base + SMB_SDA);
/* check for bus conflict and NACK */
val = inb(smbus_io_base + SMB_STS);
if (((val & SMB_STS_BER) != 0) || ((val & SMB_STS_NEGACK) != 0)) {
printk(BIOS_DEBUG, "SEND SLAVE ERROR (%x)\n", val);
return SMBUS_ERROR;
}
return smbus_wait(smbus_io_base);
}
static int smbus_send_command(unsigned smbus_io_base, unsigned char command)
{
unsigned char val;
/* send the command */
outb(command, smbus_io_base + SMB_SDA);
/* check for bus conflict and NACK */
val = inb(smbus_io_base + SMB_STS);
if (((val & SMB_STS_BER) != 0) || ((val & SMB_STS_NEGACK) != 0))
return SMBUS_ERROR;
return smbus_wait(smbus_io_base);
}
static void _doread(unsigned smbus_io_base, unsigned char device,
unsigned char address, unsigned char *data, int count)
{
int ret;
int index = 0;
unsigned char val;
if ((ret = smbus_check_stop_condition(smbus_io_base)))
goto err;
index++;
if ((ret = smbus_start_condition(smbus_io_base)))
goto err;
index++; /* 2 */
if ((ret = smbus_send_slave_address(smbus_io_base, device)))
goto err;
index++;
if ((ret = smbus_send_command(smbus_io_base, address)))
goto err;
index++;
if ((ret = smbus_start_condition(smbus_io_base)))
goto err;
/* Clear the ack for multiple byte reads */
smbus_ack(smbus_io_base, (count == 1) ? 1 : 0);
index++;
if ((ret = smbus_send_slave_address(smbus_io_base, device | 0x01)))
goto err;
while (count) {
/* Set the ACK if this is the next to last byte */
smbus_ack(smbus_io_base, (count == 2) ? 1 : 0);
/* Set the stop bit if this is the last byte to read */
if (count == 1)
smbus_stop_condition(smbus_io_base);
val = inb(smbus_io_base + SMB_SDA);
*data++ = val;
if (count > 1) {
ret = smbus_wait(smbus_io_base);
if (ret)
return;
}
count--;
}
return;
err:
printk(BIOS_DEBUG, "SMBUS READ ERROR (%d): %d\n", index, ret);
}
static inline unsigned char do_smbus_read_byte(unsigned smbus_io_base,
unsigned char device, unsigned char address)
{
unsigned char val = 0;
_doread(smbus_io_base, device, address, &val, sizeof(val));
return val;
}
static inline unsigned short do_smbus_read_word(unsigned smbus_io_base,
unsigned char device, unsigned char address)
{
unsigned short val = 0;
_doread(smbus_io_base, device, address, (unsigned char *)&val,
sizeof(val));
return val;
}
static int _dowrite(unsigned smbus_io_base, unsigned char device,
unsigned char address, unsigned char *data, int count)
{
int ret;
if ((ret = smbus_check_stop_condition(smbus_io_base)))
goto err;
if ((ret = smbus_start_condition(smbus_io_base)))
goto err;
if ((ret = smbus_send_slave_address(smbus_io_base, device)))
goto err;
if ((ret = smbus_send_command(smbus_io_base, address)))
goto err;
while (count) {
if ((ret = smbus_write(smbus_io_base, *data++)))
goto err;
count--;
}
smbus_stop_condition(smbus_io_base);
return 0;
err:
printk(BIOS_DEBUG, "SMBUS WRITE ERROR: %d\n", ret);
return -1;
}
static inline int do_smbus_write_byte(unsigned smbus_io_base,
unsigned char device, unsigned char address, unsigned char data)
{
return _dowrite(smbus_io_base, device, address,
(unsigned char *)&data, 1);
}
static inline int do_smbus_write_word(unsigned smbus_io_base,
unsigned char device, unsigned char address, unsigned short data)
{
return _dowrite(smbus_io_base, device, address, (unsigned char *)&data,
2);
}
|