blob: e575abc40e5269a8efd7809f5e4dbdc7fb4009d5 [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
Frans Hendrikse48be352019-06-19 11:01:27 +02007 * Copyright (C) 2018-2019 Eltan B.V.
Arthur Heymans16fe7902017-04-12 17:01:31 +02008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 */
18
19#include <arch/io.h>
Kyösti Mälkkib5d998b2017-08-20 21:36:08 +030020#include <console/console.h>
Arthur Heymans16fe7902017-04-12 17:01:31 +020021#include <device/smbus_def.h>
Arthur Heymans1b04aa22017-08-04 14:28:50 +020022#include <stdlib.h>
Elyes HAOUASab89edb2019-05-15 21:10:44 +020023#include <types.h>
24
Arthur Heymans16fe7902017-04-12 17:01:31 +020025#include "smbus.h"
26
27
Julius Wernercd49cce2019-03-05 16:53:33 -080028#if CONFIG(DEBUG_SMBUS)
Kyösti Mälkkib5d998b2017-08-20 21:36:08 +030029#define dprintk(args...) printk(BIOS_DEBUG, ##args)
30#else
31#define dprintk(args...) do {} while (0)
32#endif
33
Arthur Heymans16fe7902017-04-12 17:01:31 +020034/* I801 command constants */
35#define I801_QUICK (0 << 2)
36#define I801_BYTE (1 << 2)
37#define I801_BYTE_DATA (2 << 2)
38#define I801_WORD_DATA (3 << 2)
39#define I801_BLOCK_DATA (5 << 2)
40#define I801_I2C_BLOCK_DATA (6 << 2) /* ICH5 and later */
41
42/* I801 Host Control register bits */
43#define SMBHSTCNT_INTREN (1 << 0)
44#define SMBHSTCNT_KILL (1 << 1)
45#define SMBHSTCNT_LAST_BYTE (1 << 5)
46#define SMBHSTCNT_START (1 << 6)
47#define SMBHSTCNT_PEC_EN (1 << 7) /* ICH3 and later */
48
49/* I801 Hosts Status register bits */
50#define SMBHSTSTS_BYTE_DONE (1 << 7)
51#define SMBHSTSTS_INUSE_STS (1 << 6)
52#define SMBHSTSTS_SMBALERT_STS (1 << 5)
53#define SMBHSTSTS_FAILED (1 << 4)
54#define SMBHSTSTS_BUS_ERR (1 << 3)
55#define SMBHSTSTS_DEV_ERR (1 << 2)
56#define SMBHSTSTS_INTR (1 << 1)
57#define SMBHSTSTS_HOST_BUSY (1 << 0)
58
Kyösti Mälkki957511c2017-08-20 21:36:11 +030059/* For SMBXMITADD register. */
60#define XMIT_WRITE(dev) (((dev) << 1) | 0)
61#define XMIT_READ(dev) (((dev) << 1) | 1)
62
Arthur Heymans16fe7902017-04-12 17:01:31 +020063#define SMBUS_TIMEOUT (10 * 1000 * 100)
Elyes HAOUASb0f19882018-06-09 11:59:00 +020064#define SMBUS_BLOCK_MAXLEN 32
Arthur Heymans16fe7902017-04-12 17:01:31 +020065
Kyösti Mälkki893edee2017-08-20 21:36:24 +030066/* block_cmd_loop flags */
67#define BLOCK_READ 0
68#define BLOCK_WRITE (1 << 0)
69#define BLOCK_I2C (1 << 1)
70
Arthur Heymans16fe7902017-04-12 17:01:31 +020071static void smbus_delay(void)
72{
73 inb(0x80);
74}
75
Kyösti Mälkkic38d5432017-08-20 21:36:18 +030076static int host_completed(u8 status)
77{
78 if (status & SMBHSTSTS_HOST_BUSY)
79 return 0;
Kyösti Mälkki44206e32019-02-26 17:17:24 +020080
81 /* These status bits do not imply completion of transaction. */
82 status &= ~(SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INUSE_STS |
83 SMBHSTSTS_SMBALERT_STS);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +030084 return status != 0;
85}
86
Kyösti Mälkki957511c2017-08-20 21:36:11 +030087static int recover_master(int smbus_base, int ret)
Arthur Heymans16fe7902017-04-12 17:01:31 +020088{
Kyösti Mälkki957511c2017-08-20 21:36:11 +030089 /* TODO: Depending of the failure, drive KILL transaction
90 * or force soft reset on SMBus master controller.
91 */
92 printk(BIOS_ERR, "SMBus: Fatal master timeout (%d)\n", ret);
93 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +020094}
95
Kyösti Mälkkic38d5432017-08-20 21:36:18 +030096static int cb_err_from_stat(u8 status)
97{
Kyösti Mälkki44206e32019-02-26 17:17:24 +020098 /* These status bits do not imply errors. */
99 status &= ~(SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INUSE_STS |
100 SMBHSTSTS_SMBALERT_STS);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300101
102 if (status == SMBHSTSTS_INTR)
103 return 0;
104
105 return SMBUS_ERROR;
106}
107
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300108static int setup_command(unsigned int smbus_base, u8 ctrl, u8 xmitadd)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200109{
110 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300111 u8 host_busy;
112
Arthur Heymans16fe7902017-04-12 17:01:31 +0200113 do {
114 smbus_delay();
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300115 host_busy = inb(smbus_base + SMBHSTSTAT) & SMBHSTSTS_HOST_BUSY;
116 } while (--loops && host_busy);
117
118 if (loops == 0)
119 return recover_master(smbus_base,
120 SMBUS_WAIT_UNTIL_READY_TIMEOUT);
121
122 /* Clear any lingering errors, so the transaction will run. */
123 outb(inb(smbus_base + SMBHSTSTAT), smbus_base + SMBHSTSTAT);
124
125 /* Set up transaction */
126 /* Disable interrupts */
127 outb(ctrl, (smbus_base + SMBHSTCTL));
128
129 /* Set the device I'm talking to. */
130 outb(xmitadd, smbus_base + SMBXMITADD);
131
132 return 0;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200133}
134
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300135static int execute_command(unsigned int smbus_base)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200136{
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300137 unsigned int loops = SMBUS_TIMEOUT;
138 u8 status;
139
140 /* Start the command. */
141 outb((inb(smbus_base + SMBHSTCTL) | SMBHSTCNT_START),
142 smbus_base + SMBHSTCTL);
143
144 /* Poll for it to start. */
Arthur Heymans16fe7902017-04-12 17:01:31 +0200145 do {
Arthur Heymans16fe7902017-04-12 17:01:31 +0200146 smbus_delay();
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300147
148 /* If we poll too slow, we could miss HOST_BUSY flag
149 * set and detect INTR or x_ERR flags instead here.
150 */
151 status = inb(smbus_base + SMBHSTSTAT);
152 status &= ~(SMBHSTSTS_SMBALERT_STS | SMBHSTSTS_INUSE_STS);
153 } while (--loops && status == 0);
154
155 if (loops == 0)
156 return recover_master(smbus_base,
157 SMBUS_WAIT_UNTIL_ACTIVE_TIMEOUT);
158
159 return 0;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200160}
161
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300162static int complete_command(unsigned int smbus_base)
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300163{
164 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300165 u8 status;
166
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300167 do {
168 smbus_delay();
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300169 status = inb(smbus_base + SMBHSTSTAT);
170 } while (--loops && !host_completed(status));
171
172 if (loops == 0)
173 return recover_master(smbus_base,
174 SMBUS_WAIT_UNTIL_DONE_TIMEOUT);
175
176 return cb_err_from_stat(status);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300177}
178
Arthur Heymans16fe7902017-04-12 17:01:31 +0200179int do_smbus_read_byte(unsigned int smbus_base, u8 device,
180 unsigned int address)
181{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300182 int ret;
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300183 u8 byte;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200184
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300185 /* Set up for a byte data read. */
186 ret = setup_command(smbus_base, I801_BYTE_DATA, XMIT_READ(device));
187 if (ret < 0)
188 return ret;
189
Arthur Heymans16fe7902017-04-12 17:01:31 +0200190 /* Set the command/address... */
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300191 outb(address, smbus_base + SMBHSTCMD);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200192
193 /* Clear the data byte... */
194 outb(0, smbus_base + SMBHSTDAT0);
195
196 /* Start the command */
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300197 ret = execute_command(smbus_base);
198 if (ret < 0)
199 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200200
201 /* Poll for transaction completion */
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300202 ret = complete_command(smbus_base);
203 if (ret < 0)
204 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200205
206 /* Read results of transaction */
207 byte = inb(smbus_base + SMBHSTDAT0);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200208 return byte;
209}
210
211int do_smbus_write_byte(unsigned int smbus_base, u8 device,
212 unsigned int address, unsigned int data)
213{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300214 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200215
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300216 /* Set up for a byte data write. */
217 ret = setup_command(smbus_base, I801_BYTE_DATA, XMIT_WRITE(device));
218 if (ret < 0)
219 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200220
Arthur Heymans16fe7902017-04-12 17:01:31 +0200221 /* Set the command/address... */
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300222 outb(address, smbus_base + SMBHSTCMD);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200223
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300224 /* Set the data byte... */
Arthur Heymans16fe7902017-04-12 17:01:31 +0200225 outb(data, smbus_base + SMBHSTDAT0);
226
227 /* Start the command */
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300228 ret = execute_command(smbus_base);
229 if (ret < 0)
230 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200231
232 /* Poll for transaction completion */
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300233 return complete_command(smbus_base);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200234}
235
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300236static int block_cmd_loop(unsigned int smbus_base,
237 u8 *buf, const unsigned int max_bytes, int flags)
238{
239 u8 status;
240 unsigned int loops = SMBUS_TIMEOUT;
241 int ret, bytes = 0;
242 int is_write_cmd = flags & BLOCK_WRITE;
243 int sw_drives_nak = flags & BLOCK_I2C;
244
245 /* Hardware limitations. */
246 if (flags == (BLOCK_WRITE | BLOCK_I2C))
247 return SMBUS_ERROR;
248
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300249 /* Set number of bytes to transfer. */
250 /* Reset number of bytes to transfer so we notice later it
251 * was really updated with the transaction. */
252 if (!sw_drives_nak) {
253 if (is_write_cmd)
254 outb(max_bytes, smbus_base + SMBHSTDAT0);
255 else
256 outb(0, smbus_base + SMBHSTDAT0);
257 }
258
259 /* Send first byte from buffer, bytes_sent increments after
260 * hardware acknowledges it.
261 */
262 if (is_write_cmd)
263 outb(*buf++, smbus_base + SMBBLKDAT);
264
265 /* Start the command */
266 ret = execute_command(smbus_base);
267 if (ret < 0)
268 return ret;
269
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300270 /* Poll for transaction completion */
271 do {
272 status = inb(smbus_base + SMBHSTSTAT);
273
274 if (status & SMBHSTSTS_BYTE_DONE) { /* Byte done */
275
276 if (is_write_cmd) {
277 bytes++;
278 if (bytes < max_bytes)
279 outb(*buf++, smbus_base + SMBBLKDAT);
280 } else {
281 if (bytes < max_bytes)
282 *buf++ = inb(smbus_base + SMBBLKDAT);
283 bytes++;
284
285 /* Indicate that next byte is the last one. */
286 if (sw_drives_nak && (bytes + 1 >= max_bytes)) {
287 outb(inb(smbus_base + SMBHSTCTL)
288 | SMBHSTCNT_LAST_BYTE,
289 smbus_base + SMBHSTCTL);
290 }
291
292 }
293
294 /* Engine internally completes the transaction
295 * and clears HOST_BUSY flag once the byte count
296 * has been reached or LAST_BYTE was set.
297 */
298 outb(SMBHSTSTS_BYTE_DONE, smbus_base + SMBHSTSTAT);
299 }
300
301 } while (--loops && !host_completed(status));
302
303 dprintk("%s: status = %02x, len = %d / %d, loops = %d\n",
304 __func__, status, bytes, max_bytes, SMBUS_TIMEOUT - loops);
305
306 if (loops == 0)
307 return recover_master(smbus_base,
308 SMBUS_WAIT_UNTIL_DONE_TIMEOUT);
309
310 ret = cb_err_from_stat(status);
311 if (ret < 0)
312 return ret;
313
314 return bytes;
315}
316
Arthur Heymans16fe7902017-04-12 17:01:31 +0200317int do_smbus_block_read(unsigned int smbus_base, u8 device, u8 cmd,
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200318 unsigned int max_bytes, u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200319{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300320 int ret, slave_bytes;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200321
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300322 max_bytes = MIN(SMBUS_BLOCK_MAXLEN, max_bytes);
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200323
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300324 /* Set up for a block data read. */
325 ret = setup_command(smbus_base, I801_BLOCK_DATA, XMIT_READ(device));
326 if (ret < 0)
327 return ret;
328
Arthur Heymans16fe7902017-04-12 17:01:31 +0200329 /* Set the command/address... */
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300330 outb(cmd, smbus_base + SMBHSTCMD);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200331
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300332 /* Execute block transaction. */
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300333 ret = block_cmd_loop(smbus_base, buf, max_bytes, BLOCK_READ);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300334 if (ret < 0)
335 return ret;
336
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300337 /* Post-check we received complete message. */
338 slave_bytes = inb(smbus_base + SMBHSTDAT0);
339 if (ret < slave_bytes)
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200340 return SMBUS_ERROR;
341
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300342 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200343}
344
345int do_smbus_block_write(unsigned int smbus_base, u8 device, u8 cmd,
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200346 const unsigned int bytes, const u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200347{
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300348 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200349
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300350 if (bytes > SMBUS_BLOCK_MAXLEN)
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200351 return SMBUS_ERROR;
352
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300353 /* Set up for a block data write. */
354 ret = setup_command(smbus_base, I801_BLOCK_DATA, XMIT_WRITE(device));
355 if (ret < 0)
356 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200357
Arthur Heymans16fe7902017-04-12 17:01:31 +0200358 /* Set the command/address... */
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300359 outb(cmd, smbus_base + SMBHSTCMD);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200360
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300361 /* Execute block transaction. */
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300362 ret = block_cmd_loop(smbus_base, (u8 *)buf, bytes, BLOCK_WRITE);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300363 if (ret < 0)
364 return ret;
365
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300366 if (ret < bytes)
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300367 return SMBUS_ERROR;
368
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300369 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200370}
371
372/* Only since ICH5 */
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200373static int has_i2c_read_command(void)
374{
Julius Wernercd49cce2019-03-05 16:53:33 -0800375 if (CONFIG(SOUTHBRIDGE_INTEL_I82371EB) ||
376 CONFIG(SOUTHBRIDGE_INTEL_I82801DX))
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200377 return 0;
378 return 1;
379}
380
381int do_i2c_eeprom_read(unsigned int smbus_base, u8 device,
Kyösti Mälkki1e392362017-08-20 21:36:03 +0300382 unsigned int offset, const unsigned int bytes, u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200383{
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300384 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200385
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200386 if (!has_i2c_read_command())
387 return SMBUS_ERROR;
388
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300389 /* Set up for a i2c block data read.
390 *
391 * FIXME: Address parameter changes to XMIT_READ(device) with
392 * some revision of PCH. Presumably hardware revisions that
393 * do not have i2c block write support internally set LSB.
394 */
395 ret = setup_command(smbus_base, I801_I2C_BLOCK_DATA,
396 XMIT_WRITE(device));
397 if (ret < 0)
398 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200399
400 /* device offset */
401 outb(offset, smbus_base + SMBHSTDAT1);
402
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300403 /* Execute block transaction. */
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300404 ret = block_cmd_loop(smbus_base, buf, bytes, BLOCK_READ | BLOCK_I2C);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300405 if (ret < 0)
406 return ret;
407
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300408 /* Post-check we received complete message. */
409 if (ret < bytes)
Kyösti Mälkki1e392362017-08-20 21:36:03 +0300410 return SMBUS_ERROR;
411
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300412 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200413}
Frans Hendrikse48be352019-06-19 11:01:27 +0200414
415/*
416 * The caller is responsible of settings HOSTC I2C_EN bit prior to making this
417 * call!
418 */
419int do_i2c_block_write(unsigned int smbus_base, u8 device,
420 unsigned int bytes, u8 *buf)
421{
422 u8 cmd;
423 int ret;
424
425 if (!CONFIG(SOC_INTEL_BRASWELL))
426 return SMBUS_ERROR;
427
428 if (!bytes || (bytes > SMBUS_BLOCK_MAXLEN))
429 return SMBUS_ERROR;
430
431 /* Set up for a block data write. */
432 ret = setup_command(smbus_base, I801_BLOCK_DATA, XMIT_WRITE(device));
433 if (ret < 0)
434 return ret;
435
436 /*
437 * In i2c mode SMBus controller sequence on bus will be:
438 * <SMBXINTADD> <SMBHSTDAT1> <SMBBLKDAT> .. <SMBBLKDAT>
439 * The SMBHSTCMD must be written also to ensure the SMBUs controller
440 * will generate the i2c sequence.
441 */
442 cmd = *buf++;
443 bytes--;
444 outb(cmd, smbus_base + SMBHSTCMD);
445 outb(cmd, smbus_base + SMBHSTDAT1);
446
447 /* Execute block transaction. */
448 ret = block_cmd_loop(smbus_base, buf, bytes, BLOCK_WRITE);
449 if (ret < 0)
450 return ret;
451
452 if (ret < bytes)
453 return SMBUS_ERROR;
454
455 ret++; /* 1st byte has been written using SMBHSTDAT1 */
456 return ret;
457}