blob: e6b1188faaa4bc3b60dcbd40d0c6519d60bb7c0a [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>
Kyösti Mälkkib5d998b2017-08-20 21:36:08 +030019#include <console/console.h>
Arthur Heymans16fe7902017-04-12 17:01:31 +020020#include <device/smbus_def.h>
Arthur Heymans1b04aa22017-08-04 14:28:50 +020021#include <stdlib.h>
Arthur Heymans16fe7902017-04-12 17:01:31 +020022#include "smbus.h"
23
24
Kyösti Mälkkib5d998b2017-08-20 21:36:08 +030025#if IS_ENABLED(CONFIG_DEBUG_SMBUS)
26#define dprintk(args...) printk(BIOS_DEBUG, ##args)
27#else
28#define dprintk(args...) do {} while (0)
29#endif
30
Arthur Heymans16fe7902017-04-12 17:01:31 +020031/* I801 command constants */
32#define I801_QUICK (0 << 2)
33#define I801_BYTE (1 << 2)
34#define I801_BYTE_DATA (2 << 2)
35#define I801_WORD_DATA (3 << 2)
36#define I801_BLOCK_DATA (5 << 2)
37#define I801_I2C_BLOCK_DATA (6 << 2) /* ICH5 and later */
38
39/* I801 Host Control register bits */
40#define SMBHSTCNT_INTREN (1 << 0)
41#define SMBHSTCNT_KILL (1 << 1)
42#define SMBHSTCNT_LAST_BYTE (1 << 5)
43#define SMBHSTCNT_START (1 << 6)
44#define SMBHSTCNT_PEC_EN (1 << 7) /* ICH3 and later */
45
46/* I801 Hosts Status register bits */
47#define SMBHSTSTS_BYTE_DONE (1 << 7)
48#define SMBHSTSTS_INUSE_STS (1 << 6)
49#define SMBHSTSTS_SMBALERT_STS (1 << 5)
50#define SMBHSTSTS_FAILED (1 << 4)
51#define SMBHSTSTS_BUS_ERR (1 << 3)
52#define SMBHSTSTS_DEV_ERR (1 << 2)
53#define SMBHSTSTS_INTR (1 << 1)
54#define SMBHSTSTS_HOST_BUSY (1 << 0)
55
56#define SMBUS_TIMEOUT (10 * 1000 * 100)
Kyösti Mälkkic17e8552017-08-20 23:48:23 +030057#define SMBUS_BLOCK_MAXLEN 32
Arthur Heymans16fe7902017-04-12 17:01:31 +020058
59static void smbus_delay(void)
60{
61 inb(0x80);
62}
63
64static int smbus_wait_until_ready(u16 smbus_base)
65{
66 unsigned int loops = SMBUS_TIMEOUT;
67 unsigned char byte;
68 do {
69 smbus_delay();
70 if (--loops == 0)
71 break;
72 byte = inb(smbus_base + SMBHSTSTAT);
73 } while (byte & SMBHSTSTS_HOST_BUSY);
74 return loops ? 0 : -1;
75}
76
77static int smbus_wait_until_done(u16 smbus_base)
78{
79 unsigned int loops = SMBUS_TIMEOUT;
80 unsigned char byte;
81 do {
82 smbus_delay();
83 if (--loops == 0)
84 break;
85 byte = inb(smbus_base + SMBHSTSTAT);
86 } while ((byte & SMBHSTSTS_HOST_BUSY)
87 || (byte & ~(SMBHSTSTS_INUSE_STS | SMBHSTSTS_HOST_BUSY)) == 0);
88 return loops ? 0 : -1;
89}
90
91static int smbus_wait_until_active(u16 smbus_base)
92{
93 unsigned long loops;
94 loops = SMBUS_TIMEOUT;
95 do {
96 unsigned char val;
97 smbus_delay();
98 val = inb(smbus_base + SMBHSTSTAT);
99 if ((val & SMBHSTSTS_HOST_BUSY)) {
100 break;
101 }
102 } while (--loops);
103 return loops ? 0 : -1;
104}
105
106int do_smbus_read_byte(unsigned int smbus_base, u8 device,
107 unsigned int address)
108{
109 unsigned char status;
110 unsigned char byte;
111
112 if (smbus_wait_until_ready(smbus_base) < 0)
113 return SMBUS_WAIT_UNTIL_READY_TIMEOUT;
114 /* Set up transaction */
115 /* Disable interrupts */
116 outb(inb(smbus_base + SMBHSTCTL) & ~SMBHSTCNT_INTREN,
117 smbus_base + SMBHSTCTL);
118 /* Set the device I'm talking too */
119 outb(((device & 0x7f) << 1) | 1, smbus_base + SMBXMITADD);
120 /* Set the command/address... */
121 outb(address & 0xff, smbus_base + SMBHSTCMD);
122 /* Set up for a byte data read */
123 outb((inb(smbus_base + SMBHSTCTL) & 0xe3) | I801_BYTE_DATA,
124 (smbus_base + SMBHSTCTL));
125 /* Clear any lingering errors, so the transaction will run */
126 outb(inb(smbus_base + SMBHSTSTAT), smbus_base + SMBHSTSTAT);
127
128 /* Clear the data byte... */
129 outb(0, smbus_base + SMBHSTDAT0);
130
131 /* Start the command */
132 outb((inb(smbus_base + SMBHSTCTL) | SMBHSTCNT_START),
133 smbus_base + SMBHSTCTL);
134
135 /* poll for it to start */
136 if (smbus_wait_until_active(smbus_base) < 0)
137 return SMBUS_WAIT_UNTIL_ACTIVE_TIMEOUT;
138
139 /* Poll for transaction completion */
140 if (smbus_wait_until_done(smbus_base) < 0)
141 return SMBUS_WAIT_UNTIL_DONE_TIMEOUT;
142
143 status = inb(smbus_base + SMBHSTSTAT);
144
145 /* Ignore the "In Use" status... */
146 status &= ~(SMBHSTSTS_SMBALERT_STS | SMBHSTSTS_INUSE_STS);
147
148 /* Read results of transaction */
149 byte = inb(smbus_base + SMBHSTDAT0);
150 if (status != SMBHSTSTS_INTR)
151 return SMBUS_ERROR;
152 return byte;
153}
154
155int do_smbus_write_byte(unsigned int smbus_base, u8 device,
156 unsigned int address, unsigned int data)
157{
158 unsigned char status;
159
160 if (smbus_wait_until_ready(smbus_base) < 0)
161 return SMBUS_WAIT_UNTIL_READY_TIMEOUT;
162
163 /* Set up transaction */
164 /* Disable interrupts */
165 outb(inb(smbus_base + SMBHSTCTL) & ~SMBHSTCNT_INTREN,
166 smbus_base + SMBHSTCTL);
167 /* Set the device I'm talking too */
168 outb(((device & 0x7f) << 1) & ~0x01, smbus_base + SMBXMITADD);
169 /* Set the command/address... */
170 outb(address & 0xff, smbus_base + SMBHSTCMD);
171 /* Set up for a byte data read */
172 outb((inb(smbus_base + SMBHSTCTL) & 0xe3) | I801_BYTE_DATA,
173 (smbus_base + SMBHSTCTL));
174 /* Clear any lingering errors, so the transaction will run */
175 outb(inb(smbus_base + SMBHSTSTAT), smbus_base + SMBHSTSTAT);
176
177 /* Clear the data byte... */
178 outb(data, smbus_base + SMBHSTDAT0);
179
180 /* Start the command */
181 outb((inb(smbus_base + SMBHSTCTL) | SMBHSTCNT_START),
182 smbus_base + SMBHSTCTL);
183
184 /* poll for it to start */
185 if (smbus_wait_until_active(smbus_base) < 0)
186 return SMBUS_WAIT_UNTIL_ACTIVE_TIMEOUT;
187
188 /* Poll for transaction completion */
189 if (smbus_wait_until_done(smbus_base) < 0)
190 return SMBUS_WAIT_UNTIL_DONE_TIMEOUT;
191
192 status = inb(smbus_base + SMBHSTSTAT);
193
194 /* Ignore the "In Use" status... */
195 status &= ~(SMBHSTSTS_SMBALERT_STS | SMBHSTSTS_INUSE_STS);
196
197 /* Read results of transaction */
198 if (status != SMBHSTSTS_INTR)
199 return SMBUS_ERROR;
200
201 return 0;
202}
203
204int do_smbus_block_read(unsigned int smbus_base, u8 device, u8 cmd,
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200205 unsigned int max_bytes, u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200206{
207 u8 status;
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200208 int slave_bytes;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200209 int bytes_read = 0;
210 unsigned int loops = SMBUS_TIMEOUT;
211 if (smbus_wait_until_ready(smbus_base) < 0)
212 return SMBUS_WAIT_UNTIL_READY_TIMEOUT;
213
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300214 max_bytes = MIN(SMBUS_BLOCK_MAXLEN, max_bytes);
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200215
Arthur Heymans16fe7902017-04-12 17:01:31 +0200216 /* Set up transaction */
217 /* Disable interrupts */
218 outb(inb(smbus_base + SMBHSTCTL) & ~SMBHSTCNT_INTREN,
219 smbus_base + SMBHSTCTL);
220 /* Set the device I'm talking too */
221 outb(((device & 0x7f) << 1) | 1, smbus_base + SMBXMITADD);
222 /* Set the command/address... */
223 outb(cmd & 0xff, smbus_base + SMBHSTCMD);
224 /* Set up for a block data read */
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200225 outb((inb(smbus_base + SMBHSTCTL) & 0xc3) | I801_BLOCK_DATA,
Arthur Heymans16fe7902017-04-12 17:01:31 +0200226 (smbus_base + SMBHSTCTL));
227 /* Clear any lingering errors, so the transaction will run */
228 outb(inb(smbus_base + SMBHSTSTAT), smbus_base + SMBHSTSTAT);
229
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200230 /* Reset number of bytes to transfer so we notice later it
231 * was really updated with the transaction. */
232 outb(0, smbus_base + SMBHSTDAT0);
233
Arthur Heymans16fe7902017-04-12 17:01:31 +0200234 /* Start the command */
235 outb((inb(smbus_base + SMBHSTCTL) | SMBHSTCNT_START),
236 smbus_base + SMBHSTCTL);
237
238 /* poll for it to start */
239 if (smbus_wait_until_active(smbus_base) < 0)
240 return SMBUS_WAIT_UNTIL_ACTIVE_TIMEOUT;
241
242 /* Poll for transaction completion */
243 do {
244 loops--;
245 status = inb(smbus_base + SMBHSTSTAT);
246 if (status & (SMBHSTSTS_FAILED | /* FAILED */
247 SMBHSTSTS_BUS_ERR | /* BUS ERR */
248 SMBHSTSTS_DEV_ERR)) /* DEV ERR */
249 return SMBUS_ERROR;
250
251 if (status & SMBHSTSTS_BYTE_DONE) { /* Byte done */
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200252
253 if (bytes_read < max_bytes) {
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300254 *buf++ = inb(smbus_base + SMBBLKDAT);
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200255 bytes_read++;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200256 }
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200257
258 /* Engine internally completes the transaction
259 * and clears HOST_BUSY flag once the byte count
260 * from slave is reached.
261 */
Arthur Heymans16fe7902017-04-12 17:01:31 +0200262 outb(status, smbus_base + SMBHSTSTAT);
263 }
264 } while ((status & SMBHSTSTS_HOST_BUSY) && loops);
265
Kyösti Mälkkif51c5fd2017-09-09 20:45:47 +0300266 /* Post-check we received complete message. */
267 slave_bytes = inb(smbus_base + SMBHSTDAT0);
268
Kyösti Mälkkib5d998b2017-08-20 21:36:08 +0300269 dprintk("%s: status = %02x, len = %d / %d, loops = %d\n",
270 __func__, status, bytes_read, slave_bytes, loops);
271
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200272 if (bytes_read < slave_bytes)
273 return SMBUS_ERROR;
274
Arthur Heymans16fe7902017-04-12 17:01:31 +0200275 return bytes_read;
276}
277
278int do_smbus_block_write(unsigned int smbus_base, u8 device, u8 cmd,
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200279 const unsigned int bytes, const u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200280{
281 u8 status;
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200282 int bytes_sent = 0;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200283 unsigned int loops = SMBUS_TIMEOUT;
284
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300285 if (bytes > SMBUS_BLOCK_MAXLEN)
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200286 return SMBUS_ERROR;
287
Arthur Heymans16fe7902017-04-12 17:01:31 +0200288 if (smbus_wait_until_ready(smbus_base) < 0)
289 return SMBUS_WAIT_UNTIL_READY_TIMEOUT;
290
291 /* Set up transaction */
292 /* Disable interrupts */
293 outb(inb(smbus_base + SMBHSTCTL) & ~SMBHSTCNT_INTREN,
294 smbus_base + SMBHSTCTL);
295 /* Set the device I'm talking too */
296 outb(((device & 0x7f) << 1) & ~0x01, smbus_base + SMBXMITADD);
297 /* Set the command/address... */
298 outb(cmd & 0xff, smbus_base + SMBHSTCMD);
299 /* Set up for a block data write */
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200300 outb((inb(smbus_base + SMBHSTCTL) & 0xc3) | I801_BLOCK_DATA,
Arthur Heymans16fe7902017-04-12 17:01:31 +0200301 (smbus_base + SMBHSTCTL));
302 /* Clear any lingering errors, so the transaction will run */
303 outb(inb(smbus_base + SMBHSTSTAT), smbus_base + SMBHSTSTAT);
304
305 /* set number of bytes to transfer */
306 outb(bytes, smbus_base + SMBHSTDAT0);
307
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200308 /* Send first byte from buffer, bytes_sent increments after
309 * hardware acknowledges it.
310 */
Arthur Heymans16fe7902017-04-12 17:01:31 +0200311 outb(*buf++, smbus_base + SMBBLKDAT);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200312
313 /* Start the command */
314 outb((inb(smbus_base + SMBHSTCTL) | SMBHSTCNT_START),
315 smbus_base + SMBHSTCTL);
316
317 /* poll for it to start */
318 if (smbus_wait_until_active(smbus_base) < 0)
319 return SMBUS_WAIT_UNTIL_ACTIVE_TIMEOUT;
320
321 /* Poll for transaction completion */
322 do {
323 loops--;
324 status = inb(smbus_base + SMBHSTSTAT);
325 if (status & (SMBHSTSTS_FAILED | /* FAILED */
326 SMBHSTSTS_BUS_ERR | /* BUS ERR */
327 SMBHSTSTS_DEV_ERR)) /* DEV ERR */
328 return SMBUS_ERROR;
329
330 if (status & SMBHSTSTS_BYTE_DONE) {
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200331 bytes_sent++;
332 if (bytes_sent < bytes)
333 outb(*buf++, smbus_base + SMBBLKDAT);
334
335 /* Engine internally completes the transaction
336 * and clears HOST_BUSY flag once the byte count
337 * has been reached.
338 */
Arthur Heymans16fe7902017-04-12 17:01:31 +0200339 outb(status, smbus_base + SMBHSTSTAT);
340 }
341 } while ((status & SMBHSTSTS_HOST_BUSY) && loops);
342
Kyösti Mälkkib5d998b2017-08-20 21:36:08 +0300343 dprintk("%s: status = %02x, len = %d / %d, loops = %d\n",
344 __func__, status, bytes_sent, bytes, loops);
345
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300346 if (bytes_sent < bytes)
347 return SMBUS_ERROR;
348
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200349 return bytes_sent;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200350}
351
352/* Only since ICH5 */
353int do_i2c_block_read(unsigned int smbus_base, u8 device,
Kyösti Mälkki1e392362017-08-20 21:36:03 +0300354 unsigned int offset, const unsigned int bytes, u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200355{
356 u8 status;
357 int bytes_read = 0;
358 unsigned int loops = SMBUS_TIMEOUT;
359 if (smbus_wait_until_ready(smbus_base) < 0)
360 return SMBUS_WAIT_UNTIL_READY_TIMEOUT;
361
362 /* Set upp transaction */
363 /* Disable interrupts */
364 outb(inb(smbus_base + SMBHSTCTL) & SMBHSTCNT_INTREN,
365 smbus_base + SMBHSTCTL);
366 /* Set the device I'm talking to */
367 outb((device & 0x7f) << 1, smbus_base + SMBXMITADD);
368
369 /* device offset */
370 outb(offset, smbus_base + SMBHSTDAT1);
371
372 /* Set up for a i2c block data read */
373 outb((inb(smbus_base + SMBHSTCTL) & 0xc3) | I801_I2C_BLOCK_DATA,
374 (smbus_base + SMBHSTCTL));
375
376 /* Clear any lingering errors, so the transaction will run */
377 outb(inb(smbus_base + SMBHSTSTAT), smbus_base + SMBHSTSTAT);
378 /* Start the command */
379 outb((inb(smbus_base + SMBHSTCTL) | SMBHSTCNT_START),
380 smbus_base + SMBHSTCTL);
381
382 /* poll for it to start */
383 if (smbus_wait_until_active(smbus_base) < 0)
384 return SMBUS_WAIT_UNTIL_ACTIVE_TIMEOUT;
385
386 /* Poll for transaction completion */
387 do {
388 loops--;
389 status = inb(smbus_base + SMBHSTSTAT);
390 if (status & (SMBHSTSTS_FAILED | /* FAILED */
391 SMBHSTSTS_BUS_ERR | /* BUS ERR */
392 SMBHSTSTS_DEV_ERR)) /* DEV ERR */
393 return SMBUS_ERROR;
394
395 if (status & SMBHSTSTS_BYTE_DONE) {
Kyösti Mälkki1e392362017-08-20 21:36:03 +0300396
397 if (bytes_read < bytes) {
398 *buf++ = inb(smbus_base + SMBBLKDAT);
399 bytes_read++;
400 }
401
402 if (bytes_read + 1 >= bytes) {
Arthur Heymans16fe7902017-04-12 17:01:31 +0200403 /* indicate that next byte is the last one */
404 outb(inb(smbus_base + SMBHSTCTL)
405 | SMBHSTCNT_LAST_BYTE,
406 smbus_base + SMBHSTCTL);
407 }
Kyösti Mälkki1e392362017-08-20 21:36:03 +0300408
Arthur Heymans16fe7902017-04-12 17:01:31 +0200409 outb(status, smbus_base + SMBHSTSTAT);
410 }
411 } while ((status & SMBHSTSTS_HOST_BUSY) && loops);
412
Kyösti Mälkkib5d998b2017-08-20 21:36:08 +0300413 dprintk("%s: status = %02x, len = %d / %d, loops = %d\n",
414 __func__, status, bytes_read, bytes, loops);
415
Kyösti Mälkki1e392362017-08-20 21:36:03 +0300416 if (bytes_read < bytes)
417 return SMBUS_ERROR;
418
Arthur Heymans16fe7902017-04-12 17:01:31 +0200419 return bytes_read;
420}