blob: af1eb602a00c75f32fcb7d61b65af76192f92993 [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>
Elyes HAOUASab89edb2019-05-15 21:10:44 +020022#include <types.h>
23
Arthur Heymans16fe7902017-04-12 17:01:31 +020024#include "smbus.h"
25
26
Julius Wernercd49cce2019-03-05 16:53:33 -080027#if CONFIG(DEBUG_SMBUS)
Kyösti Mälkkib5d998b2017-08-20 21:36:08 +030028#define dprintk(args...) printk(BIOS_DEBUG, ##args)
29#else
30#define dprintk(args...) do {} while (0)
31#endif
32
Arthur Heymans16fe7902017-04-12 17:01:31 +020033/* I801 command constants */
34#define I801_QUICK (0 << 2)
35#define I801_BYTE (1 << 2)
36#define I801_BYTE_DATA (2 << 2)
37#define I801_WORD_DATA (3 << 2)
38#define I801_BLOCK_DATA (5 << 2)
39#define I801_I2C_BLOCK_DATA (6 << 2) /* ICH5 and later */
40
41/* I801 Host Control register bits */
42#define SMBHSTCNT_INTREN (1 << 0)
43#define SMBHSTCNT_KILL (1 << 1)
44#define SMBHSTCNT_LAST_BYTE (1 << 5)
45#define SMBHSTCNT_START (1 << 6)
46#define SMBHSTCNT_PEC_EN (1 << 7) /* ICH3 and later */
47
48/* I801 Hosts Status register bits */
49#define SMBHSTSTS_BYTE_DONE (1 << 7)
50#define SMBHSTSTS_INUSE_STS (1 << 6)
51#define SMBHSTSTS_SMBALERT_STS (1 << 5)
52#define SMBHSTSTS_FAILED (1 << 4)
53#define SMBHSTSTS_BUS_ERR (1 << 3)
54#define SMBHSTSTS_DEV_ERR (1 << 2)
55#define SMBHSTSTS_INTR (1 << 1)
56#define SMBHSTSTS_HOST_BUSY (1 << 0)
57
Kyösti Mälkki957511c2017-08-20 21:36:11 +030058/* For SMBXMITADD register. */
59#define XMIT_WRITE(dev) (((dev) << 1) | 0)
60#define XMIT_READ(dev) (((dev) << 1) | 1)
61
Arthur Heymans16fe7902017-04-12 17:01:31 +020062#define SMBUS_TIMEOUT (10 * 1000 * 100)
Elyes HAOUASb0f19882018-06-09 11:59:00 +020063#define SMBUS_BLOCK_MAXLEN 32
Arthur Heymans16fe7902017-04-12 17:01:31 +020064
Kyösti Mälkki893edee2017-08-20 21:36:24 +030065/* block_cmd_loop flags */
66#define BLOCK_READ 0
67#define BLOCK_WRITE (1 << 0)
68#define BLOCK_I2C (1 << 1)
69
Arthur Heymans16fe7902017-04-12 17:01:31 +020070static void smbus_delay(void)
71{
72 inb(0x80);
73}
74
Kyösti Mälkkic38d5432017-08-20 21:36:18 +030075static int host_completed(u8 status)
76{
77 if (status & SMBHSTSTS_HOST_BUSY)
78 return 0;
Kyösti Mälkki44206e32019-02-26 17:17:24 +020079
80 /* These status bits do not imply completion of transaction. */
81 status &= ~(SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INUSE_STS |
82 SMBHSTSTS_SMBALERT_STS);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +030083 return status != 0;
84}
85
Kyösti Mälkki957511c2017-08-20 21:36:11 +030086static int recover_master(int smbus_base, int ret)
Arthur Heymans16fe7902017-04-12 17:01:31 +020087{
Kyösti Mälkki957511c2017-08-20 21:36:11 +030088 /* TODO: Depending of the failure, drive KILL transaction
89 * or force soft reset on SMBus master controller.
90 */
91 printk(BIOS_ERR, "SMBus: Fatal master timeout (%d)\n", ret);
92 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +020093}
94
Kyösti Mälkkic38d5432017-08-20 21:36:18 +030095static int cb_err_from_stat(u8 status)
96{
Kyösti Mälkki44206e32019-02-26 17:17:24 +020097 /* These status bits do not imply errors. */
98 status &= ~(SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INUSE_STS |
99 SMBHSTSTS_SMBALERT_STS);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300100
101 if (status == SMBHSTSTS_INTR)
102 return 0;
103
104 return SMBUS_ERROR;
105}
106
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300107static int setup_command(unsigned int smbus_base, u8 ctrl, u8 xmitadd)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200108{
109 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300110 u8 host_busy;
111
Arthur Heymans16fe7902017-04-12 17:01:31 +0200112 do {
113 smbus_delay();
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300114 host_busy = inb(smbus_base + SMBHSTSTAT) & SMBHSTSTS_HOST_BUSY;
115 } while (--loops && host_busy);
116
117 if (loops == 0)
118 return recover_master(smbus_base,
119 SMBUS_WAIT_UNTIL_READY_TIMEOUT);
120
121 /* Clear any lingering errors, so the transaction will run. */
122 outb(inb(smbus_base + SMBHSTSTAT), smbus_base + SMBHSTSTAT);
123
124 /* Set up transaction */
125 /* Disable interrupts */
126 outb(ctrl, (smbus_base + SMBHSTCTL));
127
128 /* Set the device I'm talking to. */
129 outb(xmitadd, smbus_base + SMBXMITADD);
130
131 return 0;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200132}
133
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300134static int execute_command(unsigned int smbus_base)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200135{
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300136 unsigned int loops = SMBUS_TIMEOUT;
137 u8 status;
138
139 /* Start the command. */
140 outb((inb(smbus_base + SMBHSTCTL) | SMBHSTCNT_START),
141 smbus_base + SMBHSTCTL);
142
143 /* Poll for it to start. */
Arthur Heymans16fe7902017-04-12 17:01:31 +0200144 do {
Arthur Heymans16fe7902017-04-12 17:01:31 +0200145 smbus_delay();
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300146
147 /* If we poll too slow, we could miss HOST_BUSY flag
148 * set and detect INTR or x_ERR flags instead here.
149 */
150 status = inb(smbus_base + SMBHSTSTAT);
151 status &= ~(SMBHSTSTS_SMBALERT_STS | SMBHSTSTS_INUSE_STS);
152 } while (--loops && status == 0);
153
154 if (loops == 0)
155 return recover_master(smbus_base,
156 SMBUS_WAIT_UNTIL_ACTIVE_TIMEOUT);
157
158 return 0;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200159}
160
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300161static int complete_command(unsigned int smbus_base)
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300162{
163 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300164 u8 status;
165
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300166 do {
167 smbus_delay();
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300168 status = inb(smbus_base + SMBHSTSTAT);
169 } while (--loops && !host_completed(status));
170
171 if (loops == 0)
172 return recover_master(smbus_base,
173 SMBUS_WAIT_UNTIL_DONE_TIMEOUT);
174
175 return cb_err_from_stat(status);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300176}
177
Arthur Heymans16fe7902017-04-12 17:01:31 +0200178int do_smbus_read_byte(unsigned int smbus_base, u8 device,
179 unsigned int address)
180{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300181 int ret;
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300182 u8 byte;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200183
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300184 /* Set up for a byte data read. */
185 ret = setup_command(smbus_base, I801_BYTE_DATA, XMIT_READ(device));
186 if (ret < 0)
187 return ret;
188
Arthur Heymans16fe7902017-04-12 17:01:31 +0200189 /* Set the command/address... */
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300190 outb(address, smbus_base + SMBHSTCMD);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200191
192 /* Clear the data byte... */
193 outb(0, smbus_base + SMBHSTDAT0);
194
195 /* Start the command */
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300196 ret = execute_command(smbus_base);
197 if (ret < 0)
198 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200199
200 /* Poll for transaction completion */
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300201 ret = complete_command(smbus_base);
202 if (ret < 0)
203 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200204
205 /* Read results of transaction */
206 byte = inb(smbus_base + SMBHSTDAT0);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200207 return byte;
208}
209
210int do_smbus_write_byte(unsigned int smbus_base, u8 device,
211 unsigned int address, unsigned int data)
212{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300213 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200214
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300215 /* Set up for a byte data write. */
216 ret = setup_command(smbus_base, I801_BYTE_DATA, XMIT_WRITE(device));
217 if (ret < 0)
218 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200219
Arthur Heymans16fe7902017-04-12 17:01:31 +0200220 /* Set the command/address... */
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300221 outb(address, smbus_base + SMBHSTCMD);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200222
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300223 /* Set the data byte... */
Arthur Heymans16fe7902017-04-12 17:01:31 +0200224 outb(data, smbus_base + SMBHSTDAT0);
225
226 /* Start the command */
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300227 ret = execute_command(smbus_base);
228 if (ret < 0)
229 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200230
231 /* Poll for transaction completion */
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300232 return complete_command(smbus_base);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200233}
234
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300235static int block_cmd_loop(unsigned int smbus_base,
236 u8 *buf, const unsigned int max_bytes, int flags)
237{
238 u8 status;
239 unsigned int loops = SMBUS_TIMEOUT;
240 int ret, bytes = 0;
241 int is_write_cmd = flags & BLOCK_WRITE;
242 int sw_drives_nak = flags & BLOCK_I2C;
243
244 /* Hardware limitations. */
245 if (flags == (BLOCK_WRITE | BLOCK_I2C))
246 return SMBUS_ERROR;
247
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300248 /* Set number of bytes to transfer. */
249 /* Reset number of bytes to transfer so we notice later it
250 * was really updated with the transaction. */
251 if (!sw_drives_nak) {
252 if (is_write_cmd)
253 outb(max_bytes, smbus_base + SMBHSTDAT0);
254 else
255 outb(0, smbus_base + SMBHSTDAT0);
256 }
257
258 /* Send first byte from buffer, bytes_sent increments after
259 * hardware acknowledges it.
260 */
261 if (is_write_cmd)
262 outb(*buf++, smbus_base + SMBBLKDAT);
263
264 /* Start the command */
265 ret = execute_command(smbus_base);
266 if (ret < 0)
267 return ret;
268
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300269 /* Poll for transaction completion */
270 do {
271 status = inb(smbus_base + SMBHSTSTAT);
272
273 if (status & SMBHSTSTS_BYTE_DONE) { /* Byte done */
274
275 if (is_write_cmd) {
276 bytes++;
277 if (bytes < max_bytes)
278 outb(*buf++, smbus_base + SMBBLKDAT);
279 } else {
280 if (bytes < max_bytes)
281 *buf++ = inb(smbus_base + SMBBLKDAT);
282 bytes++;
283
284 /* Indicate that next byte is the last one. */
285 if (sw_drives_nak && (bytes + 1 >= max_bytes)) {
286 outb(inb(smbus_base + SMBHSTCTL)
287 | SMBHSTCNT_LAST_BYTE,
288 smbus_base + SMBHSTCTL);
289 }
290
291 }
292
293 /* Engine internally completes the transaction
294 * and clears HOST_BUSY flag once the byte count
295 * has been reached or LAST_BYTE was set.
296 */
297 outb(SMBHSTSTS_BYTE_DONE, smbus_base + SMBHSTSTAT);
298 }
299
300 } while (--loops && !host_completed(status));
301
302 dprintk("%s: status = %02x, len = %d / %d, loops = %d\n",
303 __func__, status, bytes, max_bytes, SMBUS_TIMEOUT - loops);
304
305 if (loops == 0)
306 return recover_master(smbus_base,
307 SMBUS_WAIT_UNTIL_DONE_TIMEOUT);
308
309 ret = cb_err_from_stat(status);
310 if (ret < 0)
311 return ret;
312
313 return bytes;
314}
315
Arthur Heymans16fe7902017-04-12 17:01:31 +0200316int do_smbus_block_read(unsigned int smbus_base, u8 device, u8 cmd,
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200317 unsigned int max_bytes, u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200318{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300319 int ret, slave_bytes;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200320
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300321 max_bytes = MIN(SMBUS_BLOCK_MAXLEN, max_bytes);
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200322
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300323 /* Set up for a block data read. */
324 ret = setup_command(smbus_base, I801_BLOCK_DATA, XMIT_READ(device));
325 if (ret < 0)
326 return ret;
327
Arthur Heymans16fe7902017-04-12 17:01:31 +0200328 /* Set the command/address... */
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300329 outb(cmd, smbus_base + SMBHSTCMD);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200330
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300331 /* Execute block transaction. */
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300332 ret = block_cmd_loop(smbus_base, buf, max_bytes, BLOCK_READ);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300333 if (ret < 0)
334 return ret;
335
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300336 /* Post-check we received complete message. */
337 slave_bytes = inb(smbus_base + SMBHSTDAT0);
338 if (ret < slave_bytes)
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200339 return SMBUS_ERROR;
340
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300341 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200342}
343
344int do_smbus_block_write(unsigned int smbus_base, u8 device, u8 cmd,
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200345 const unsigned int bytes, const u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200346{
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300347 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200348
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300349 if (bytes > SMBUS_BLOCK_MAXLEN)
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200350 return SMBUS_ERROR;
351
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300352 /* Set up for a block data write. */
353 ret = setup_command(smbus_base, I801_BLOCK_DATA, XMIT_WRITE(device));
354 if (ret < 0)
355 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200356
Arthur Heymans16fe7902017-04-12 17:01:31 +0200357 /* Set the command/address... */
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300358 outb(cmd, smbus_base + SMBHSTCMD);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200359
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300360 /* Execute block transaction. */
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300361 ret = block_cmd_loop(smbus_base, (u8 *)buf, bytes, BLOCK_WRITE);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300362 if (ret < 0)
363 return ret;
364
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300365 if (ret < bytes)
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300366 return SMBUS_ERROR;
367
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300368 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200369}
370
371/* Only since ICH5 */
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200372static int has_i2c_read_command(void)
373{
Julius Wernercd49cce2019-03-05 16:53:33 -0800374 if (CONFIG(SOUTHBRIDGE_INTEL_I82371EB) ||
375 CONFIG(SOUTHBRIDGE_INTEL_I82801DX))
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200376 return 0;
377 return 1;
378}
379
380int do_i2c_eeprom_read(unsigned int smbus_base, u8 device,
Kyösti Mälkki1e392362017-08-20 21:36:03 +0300381 unsigned int offset, const unsigned int bytes, u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200382{
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300383 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200384
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200385 if (!has_i2c_read_command())
386 return SMBUS_ERROR;
387
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300388 /* Set up for a i2c block data read.
389 *
390 * FIXME: Address parameter changes to XMIT_READ(device) with
391 * some revision of PCH. Presumably hardware revisions that
392 * do not have i2c block write support internally set LSB.
393 */
394 ret = setup_command(smbus_base, I801_I2C_BLOCK_DATA,
395 XMIT_WRITE(device));
396 if (ret < 0)
397 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200398
399 /* device offset */
400 outb(offset, smbus_base + SMBHSTDAT1);
401
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300402 /* Execute block transaction. */
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300403 ret = block_cmd_loop(smbus_base, buf, bytes, BLOCK_READ | BLOCK_I2C);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300404 if (ret < 0)
405 return ret;
406
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300407 /* Post-check we received complete message. */
408 if (ret < bytes)
Kyösti Mälkki1e392362017-08-20 21:36:03 +0300409 return SMBUS_ERROR;
410
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300411 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200412}