blob: ee58aab995c9016991ae20bbc9ab665cf0407b25 [file] [log] [blame]
Arthur Heymans16fe7902017-04-12 17:01:31 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2005 Yinghai Lu <yinghailu@gmail.com>
5 * Copyright (C) 2009 coresystems GmbH
6 * Copyright (C) 2013 Vladimir Serbinenko
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#include <arch/io.h>
19#include <device/smbus_def.h>
Arthur Heymans1b04aa22017-08-04 14:28:50 +020020#include <stdlib.h>
Arthur Heymans16fe7902017-04-12 17:01:31 +020021#include "smbus.h"
22
23
24/* I801 command constants */
25#define I801_QUICK (0 << 2)
26#define I801_BYTE (1 << 2)
27#define I801_BYTE_DATA (2 << 2)
28#define I801_WORD_DATA (3 << 2)
29#define I801_BLOCK_DATA (5 << 2)
30#define I801_I2C_BLOCK_DATA (6 << 2) /* ICH5 and later */
31
32/* I801 Host Control register bits */
33#define SMBHSTCNT_INTREN (1 << 0)
34#define SMBHSTCNT_KILL (1 << 1)
35#define SMBHSTCNT_LAST_BYTE (1 << 5)
36#define SMBHSTCNT_START (1 << 6)
37#define SMBHSTCNT_PEC_EN (1 << 7) /* ICH3 and later */
38
39/* I801 Hosts Status register bits */
40#define SMBHSTSTS_BYTE_DONE (1 << 7)
41#define SMBHSTSTS_INUSE_STS (1 << 6)
42#define SMBHSTSTS_SMBALERT_STS (1 << 5)
43#define SMBHSTSTS_FAILED (1 << 4)
44#define SMBHSTSTS_BUS_ERR (1 << 3)
45#define SMBHSTSTS_DEV_ERR (1 << 2)
46#define SMBHSTSTS_INTR (1 << 1)
47#define SMBHSTSTS_HOST_BUSY (1 << 0)
48
49#define SMBUS_TIMEOUT (10 * 1000 * 100)
50
51static void smbus_delay(void)
52{
53 inb(0x80);
54}
55
56static int smbus_wait_until_ready(u16 smbus_base)
57{
58 unsigned int loops = SMBUS_TIMEOUT;
59 unsigned char byte;
60 do {
61 smbus_delay();
62 if (--loops == 0)
63 break;
64 byte = inb(smbus_base + SMBHSTSTAT);
65 } while (byte & SMBHSTSTS_HOST_BUSY);
66 return loops ? 0 : -1;
67}
68
69static int smbus_wait_until_done(u16 smbus_base)
70{
71 unsigned int loops = SMBUS_TIMEOUT;
72 unsigned char byte;
73 do {
74 smbus_delay();
75 if (--loops == 0)
76 break;
77 byte = inb(smbus_base + SMBHSTSTAT);
78 } while ((byte & SMBHSTSTS_HOST_BUSY)
79 || (byte & ~(SMBHSTSTS_INUSE_STS | SMBHSTSTS_HOST_BUSY)) == 0);
80 return loops ? 0 : -1;
81}
82
83static int smbus_wait_until_active(u16 smbus_base)
84{
85 unsigned long loops;
86 loops = SMBUS_TIMEOUT;
87 do {
88 unsigned char val;
89 smbus_delay();
90 val = inb(smbus_base + SMBHSTSTAT);
91 if ((val & SMBHSTSTS_HOST_BUSY)) {
92 break;
93 }
94 } while (--loops);
95 return loops ? 0 : -1;
96}
97
98int do_smbus_read_byte(unsigned int smbus_base, u8 device,
99 unsigned int address)
100{
101 unsigned char status;
102 unsigned char byte;
103
104 if (smbus_wait_until_ready(smbus_base) < 0)
105 return SMBUS_WAIT_UNTIL_READY_TIMEOUT;
106 /* Set up transaction */
107 /* Disable interrupts */
108 outb(inb(smbus_base + SMBHSTCTL) & ~SMBHSTCNT_INTREN,
109 smbus_base + SMBHSTCTL);
110 /* Set the device I'm talking too */
111 outb(((device & 0x7f) << 1) | 1, smbus_base + SMBXMITADD);
112 /* Set the command/address... */
113 outb(address & 0xff, smbus_base + SMBHSTCMD);
114 /* Set up for a byte data read */
115 outb((inb(smbus_base + SMBHSTCTL) & 0xe3) | I801_BYTE_DATA,
116 (smbus_base + SMBHSTCTL));
117 /* Clear any lingering errors, so the transaction will run */
118 outb(inb(smbus_base + SMBHSTSTAT), smbus_base + SMBHSTSTAT);
119
120 /* Clear the data byte... */
121 outb(0, smbus_base + SMBHSTDAT0);
122
123 /* Start the command */
124 outb((inb(smbus_base + SMBHSTCTL) | SMBHSTCNT_START),
125 smbus_base + SMBHSTCTL);
126
127 /* poll for it to start */
128 if (smbus_wait_until_active(smbus_base) < 0)
129 return SMBUS_WAIT_UNTIL_ACTIVE_TIMEOUT;
130
131 /* Poll for transaction completion */
132 if (smbus_wait_until_done(smbus_base) < 0)
133 return SMBUS_WAIT_UNTIL_DONE_TIMEOUT;
134
135 status = inb(smbus_base + SMBHSTSTAT);
136
137 /* Ignore the "In Use" status... */
138 status &= ~(SMBHSTSTS_SMBALERT_STS | SMBHSTSTS_INUSE_STS);
139
140 /* Read results of transaction */
141 byte = inb(smbus_base + SMBHSTDAT0);
142 if (status != SMBHSTSTS_INTR)
143 return SMBUS_ERROR;
144 return byte;
145}
146
147int do_smbus_write_byte(unsigned int smbus_base, u8 device,
148 unsigned int address, unsigned int data)
149{
150 unsigned char status;
151
152 if (smbus_wait_until_ready(smbus_base) < 0)
153 return SMBUS_WAIT_UNTIL_READY_TIMEOUT;
154
155 /* Set up transaction */
156 /* Disable interrupts */
157 outb(inb(smbus_base + SMBHSTCTL) & ~SMBHSTCNT_INTREN,
158 smbus_base + SMBHSTCTL);
159 /* Set the device I'm talking too */
160 outb(((device & 0x7f) << 1) & ~0x01, smbus_base + SMBXMITADD);
161 /* Set the command/address... */
162 outb(address & 0xff, smbus_base + SMBHSTCMD);
163 /* Set up for a byte data read */
164 outb((inb(smbus_base + SMBHSTCTL) & 0xe3) | I801_BYTE_DATA,
165 (smbus_base + SMBHSTCTL));
166 /* Clear any lingering errors, so the transaction will run */
167 outb(inb(smbus_base + SMBHSTSTAT), smbus_base + SMBHSTSTAT);
168
169 /* Clear the data byte... */
170 outb(data, smbus_base + SMBHSTDAT0);
171
172 /* Start the command */
173 outb((inb(smbus_base + SMBHSTCTL) | SMBHSTCNT_START),
174 smbus_base + SMBHSTCTL);
175
176 /* poll for it to start */
177 if (smbus_wait_until_active(smbus_base) < 0)
178 return SMBUS_WAIT_UNTIL_ACTIVE_TIMEOUT;
179
180 /* Poll for transaction completion */
181 if (smbus_wait_until_done(smbus_base) < 0)
182 return SMBUS_WAIT_UNTIL_DONE_TIMEOUT;
183
184 status = inb(smbus_base + SMBHSTSTAT);
185
186 /* Ignore the "In Use" status... */
187 status &= ~(SMBHSTSTS_SMBALERT_STS | SMBHSTSTS_INUSE_STS);
188
189 /* Read results of transaction */
190 if (status != SMBHSTSTS_INTR)
191 return SMBUS_ERROR;
192
193 return 0;
194}
195
196int do_smbus_block_read(unsigned int smbus_base, u8 device, u8 cmd,
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200197 unsigned int max_bytes, u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200198{
199 u8 status;
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200200 int slave_bytes;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200201 int bytes_read = 0;
202 unsigned int loops = SMBUS_TIMEOUT;
203 if (smbus_wait_until_ready(smbus_base) < 0)
204 return SMBUS_WAIT_UNTIL_READY_TIMEOUT;
205
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200206 max_bytes = MIN(32, max_bytes);
207
Arthur Heymans16fe7902017-04-12 17:01:31 +0200208 /* Set up transaction */
209 /* Disable interrupts */
210 outb(inb(smbus_base + SMBHSTCTL) & ~SMBHSTCNT_INTREN,
211 smbus_base + SMBHSTCTL);
212 /* Set the device I'm talking too */
213 outb(((device & 0x7f) << 1) | 1, smbus_base + SMBXMITADD);
214 /* Set the command/address... */
215 outb(cmd & 0xff, smbus_base + SMBHSTCMD);
216 /* Set up for a block data read */
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200217 outb((inb(smbus_base + SMBHSTCTL) & 0xc3) | I801_BLOCK_DATA,
Arthur Heymans16fe7902017-04-12 17:01:31 +0200218 (smbus_base + SMBHSTCTL));
219 /* Clear any lingering errors, so the transaction will run */
220 outb(inb(smbus_base + SMBHSTSTAT), smbus_base + SMBHSTSTAT);
221
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200222 /* Reset number of bytes to transfer so we notice later it
223 * was really updated with the transaction. */
224 outb(0, smbus_base + SMBHSTDAT0);
225
Arthur Heymans16fe7902017-04-12 17:01:31 +0200226 /* Start the command */
227 outb((inb(smbus_base + SMBHSTCTL) | SMBHSTCNT_START),
228 smbus_base + SMBHSTCTL);
229
230 /* poll for it to start */
231 if (smbus_wait_until_active(smbus_base) < 0)
232 return SMBUS_WAIT_UNTIL_ACTIVE_TIMEOUT;
233
234 /* Poll for transaction completion */
235 do {
236 loops--;
237 status = inb(smbus_base + SMBHSTSTAT);
238 if (status & (SMBHSTSTS_FAILED | /* FAILED */
239 SMBHSTSTS_BUS_ERR | /* BUS ERR */
240 SMBHSTSTS_DEV_ERR)) /* DEV ERR */
241 return SMBUS_ERROR;
242
243 if (status & SMBHSTSTS_BYTE_DONE) { /* Byte done */
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200244
245 if (bytes_read < max_bytes) {
246 *buf = inb(smbus_base + SMBBLKDAT);
247 buf++;
248 bytes_read++;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200249 }
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200250
251 /* Engine internally completes the transaction
252 * and clears HOST_BUSY flag once the byte count
253 * from slave is reached.
254 */
Arthur Heymans16fe7902017-04-12 17:01:31 +0200255 outb(status, smbus_base + SMBHSTSTAT);
256 }
257 } while ((status & SMBHSTSTS_HOST_BUSY) && loops);
258
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200259 /* Post-check we received complete message. */
260 slave_bytes = inb(smbus_base + SMBHSTDAT0);
261 if (bytes_read < slave_bytes)
262 return SMBUS_ERROR;
263
Arthur Heymans16fe7902017-04-12 17:01:31 +0200264 return bytes_read;
265}
266
267int do_smbus_block_write(unsigned int smbus_base, u8 device, u8 cmd,
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200268 const unsigned int bytes, const u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200269{
270 u8 status;
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200271 int bytes_sent = 0;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200272 unsigned int loops = SMBUS_TIMEOUT;
273
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200274 if (bytes > 32)
275 return SMBUS_ERROR;
276
Arthur Heymans16fe7902017-04-12 17:01:31 +0200277 if (smbus_wait_until_ready(smbus_base) < 0)
278 return SMBUS_WAIT_UNTIL_READY_TIMEOUT;
279
280 /* Set up transaction */
281 /* Disable interrupts */
282 outb(inb(smbus_base + SMBHSTCTL) & ~SMBHSTCNT_INTREN,
283 smbus_base + SMBHSTCTL);
284 /* Set the device I'm talking too */
285 outb(((device & 0x7f) << 1) & ~0x01, smbus_base + SMBXMITADD);
286 /* Set the command/address... */
287 outb(cmd & 0xff, smbus_base + SMBHSTCMD);
288 /* Set up for a block data write */
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200289 outb((inb(smbus_base + SMBHSTCTL) & 0xc3) | I801_BLOCK_DATA,
Arthur Heymans16fe7902017-04-12 17:01:31 +0200290 (smbus_base + SMBHSTCTL));
291 /* Clear any lingering errors, so the transaction will run */
292 outb(inb(smbus_base + SMBHSTSTAT), smbus_base + SMBHSTSTAT);
293
294 /* set number of bytes to transfer */
295 outb(bytes, smbus_base + SMBHSTDAT0);
296
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200297 /* Send first byte from buffer, bytes_sent increments after
298 * hardware acknowledges it.
299 */
Arthur Heymans16fe7902017-04-12 17:01:31 +0200300 outb(*buf++, smbus_base + SMBBLKDAT);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200301
302 /* Start the command */
303 outb((inb(smbus_base + SMBHSTCTL) | SMBHSTCNT_START),
304 smbus_base + SMBHSTCTL);
305
306 /* poll for it to start */
307 if (smbus_wait_until_active(smbus_base) < 0)
308 return SMBUS_WAIT_UNTIL_ACTIVE_TIMEOUT;
309
310 /* Poll for transaction completion */
311 do {
312 loops--;
313 status = inb(smbus_base + SMBHSTSTAT);
314 if (status & (SMBHSTSTS_FAILED | /* FAILED */
315 SMBHSTSTS_BUS_ERR | /* BUS ERR */
316 SMBHSTSTS_DEV_ERR)) /* DEV ERR */
317 return SMBUS_ERROR;
318
319 if (status & SMBHSTSTS_BYTE_DONE) {
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200320 bytes_sent++;
321 if (bytes_sent < bytes)
322 outb(*buf++, smbus_base + SMBBLKDAT);
323
324 /* Engine internally completes the transaction
325 * and clears HOST_BUSY flag once the byte count
326 * has been reached.
327 */
Arthur Heymans16fe7902017-04-12 17:01:31 +0200328 outb(status, smbus_base + SMBHSTSTAT);
329 }
330 } while ((status & SMBHSTSTS_HOST_BUSY) && loops);
331
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200332 return bytes_sent;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200333}
334
335/* Only since ICH5 */
336int do_i2c_block_read(unsigned int smbus_base, u8 device,
337 unsigned int offset, u32 bytes, u8 *buf)
338{
339 u8 status;
340 int bytes_read = 0;
341 unsigned int loops = SMBUS_TIMEOUT;
342 if (smbus_wait_until_ready(smbus_base) < 0)
343 return SMBUS_WAIT_UNTIL_READY_TIMEOUT;
344
345 /* Set upp transaction */
346 /* Disable interrupts */
347 outb(inb(smbus_base + SMBHSTCTL) & SMBHSTCNT_INTREN,
348 smbus_base + SMBHSTCTL);
349 /* Set the device I'm talking to */
350 outb((device & 0x7f) << 1, smbus_base + SMBXMITADD);
351
352 /* device offset */
353 outb(offset, smbus_base + SMBHSTDAT1);
354
355 /* Set up for a i2c block data read */
356 outb((inb(smbus_base + SMBHSTCTL) & 0xc3) | I801_I2C_BLOCK_DATA,
357 (smbus_base + SMBHSTCTL));
358
359 /* Clear any lingering errors, so the transaction will run */
360 outb(inb(smbus_base + SMBHSTSTAT), smbus_base + SMBHSTSTAT);
361 /* Start the command */
362 outb((inb(smbus_base + SMBHSTCTL) | SMBHSTCNT_START),
363 smbus_base + SMBHSTCTL);
364
365 /* poll for it to start */
366 if (smbus_wait_until_active(smbus_base) < 0)
367 return SMBUS_WAIT_UNTIL_ACTIVE_TIMEOUT;
368
369 /* Poll for transaction completion */
370 do {
371 loops--;
372 status = inb(smbus_base + SMBHSTSTAT);
373 if (status & (SMBHSTSTS_FAILED | /* FAILED */
374 SMBHSTSTS_BUS_ERR | /* BUS ERR */
375 SMBHSTSTS_DEV_ERR)) /* DEV ERR */
376 return SMBUS_ERROR;
377
378 if (status & SMBHSTSTS_BYTE_DONE) {
379 *buf = inb(smbus_base + SMBBLKDAT);
380 buf++;
381 bytes_read++;
382 if (--bytes == 1) {
383 /* indicate that next byte is the last one */
384 outb(inb(smbus_base + SMBHSTCTL)
385 | SMBHSTCNT_LAST_BYTE,
386 smbus_base + SMBHSTCTL);
387 }
388 outb(status, smbus_base + SMBHSTSTAT);
389 }
390 } while ((status & SMBHSTSTS_HOST_BUSY) && loops);
391
392 return bytes_read;
393}