blob: 1b005d78e013c2cf34337be069a481992382f277 [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>
Kyösti Mälkki1cae4542020-01-06 12:31:34 +020022#include <device/smbus_host.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älkki5e9ae0c2020-01-06 13:35:59 +020076static void host_outb(uintptr_t base, u8 reg, u8 value)
Kyösti Mälkkib49638d2020-01-02 16:36:56 +020077{
78 outb(value, base + reg);
79}
80
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +020081static u8 host_inb(uintptr_t base, u8 reg)
Kyösti Mälkkib49638d2020-01-02 16:36:56 +020082{
83 return inb(base + reg);
84}
85
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +020086static void host_and_or(uintptr_t base, u8 reg, u8 mask, u8 or)
Kyösti Mälkki65f5de22020-01-02 16:36:56 +020087{
88 u8 value;
89 value = host_inb(base, reg);
90 value &= mask;
91 value |= or;
92 host_outb(base, reg, value);
93}
94
Kyösti Mälkkic38d5432017-08-20 21:36:18 +030095static int host_completed(u8 status)
96{
97 if (status & SMBHSTSTS_HOST_BUSY)
98 return 0;
Kyösti Mälkki44206e32019-02-26 17:17:24 +020099
100 /* These status bits do not imply completion of transaction. */
101 status &= ~(SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INUSE_STS |
102 SMBHSTSTS_SMBALERT_STS);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300103 return status != 0;
104}
105
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200106static int recover_master(uintptr_t base, int ret)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200107{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300108 /* TODO: Depending of the failure, drive KILL transaction
109 * or force soft reset on SMBus master controller.
110 */
111 printk(BIOS_ERR, "SMBus: Fatal master timeout (%d)\n", ret);
112 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200113}
114
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300115static int cb_err_from_stat(u8 status)
116{
Kyösti Mälkki44206e32019-02-26 17:17:24 +0200117 /* These status bits do not imply errors. */
118 status &= ~(SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INUSE_STS |
119 SMBHSTSTS_SMBALERT_STS);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300120
121 if (status == SMBHSTSTS_INTR)
122 return 0;
123
124 return SMBUS_ERROR;
125}
126
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200127static int setup_command(uintptr_t base, u8 ctrl, u8 xmitadd)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200128{
129 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300130 u8 host_busy;
131
Arthur Heymans16fe7902017-04-12 17:01:31 +0200132 do {
133 smbus_delay();
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200134 host_busy = host_inb(base, SMBHSTSTAT) & SMBHSTSTS_HOST_BUSY;
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300135 } while (--loops && host_busy);
136
137 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200138 return recover_master(base, SMBUS_WAIT_UNTIL_READY_TIMEOUT);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300139
140 /* Clear any lingering errors, so the transaction will run. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200141 host_and_or(base, SMBHSTSTAT, 0xff, 0);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300142
143 /* Set up transaction */
144 /* Disable interrupts */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200145 host_outb(base, SMBHSTCTL, ctrl);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300146
147 /* Set the device I'm talking to. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200148 host_outb(base, SMBXMITADD, xmitadd);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300149
150 return 0;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200151}
152
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200153static int execute_command(uintptr_t base)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200154{
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300155 unsigned int loops = SMBUS_TIMEOUT;
156 u8 status;
157
158 /* Start the command. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200159 host_and_or(base, SMBHSTCTL, 0xff, SMBHSTCNT_START);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300160
161 /* Poll for it to start. */
Arthur Heymans16fe7902017-04-12 17:01:31 +0200162 do {
Arthur Heymans16fe7902017-04-12 17:01:31 +0200163 smbus_delay();
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300164
165 /* If we poll too slow, we could miss HOST_BUSY flag
166 * set and detect INTR or x_ERR flags instead here.
167 */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200168 status = host_inb(base, SMBHSTSTAT);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300169 status &= ~(SMBHSTSTS_SMBALERT_STS | SMBHSTSTS_INUSE_STS);
170 } while (--loops && status == 0);
171
172 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200173 return recover_master(base,
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300174 SMBUS_WAIT_UNTIL_ACTIVE_TIMEOUT);
175
176 return 0;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200177}
178
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200179static int complete_command(uintptr_t base)
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300180{
181 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300182 u8 status;
183
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300184 do {
185 smbus_delay();
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200186 status = host_inb(base, SMBHSTSTAT);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300187 } while (--loops && !host_completed(status));
188
189 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200190 return recover_master(base,
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300191 SMBUS_WAIT_UNTIL_DONE_TIMEOUT);
192
193 return cb_err_from_stat(status);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300194}
195
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200196static int smbus_read_cmd(uintptr_t base, u8 ctrl, u8 device, u8 address)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200197{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300198 int ret;
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200199 u16 word;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200200
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300201 /* Set up for a byte data read. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200202 ret = setup_command(base, ctrl, XMIT_READ(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300203 if (ret < 0)
204 return ret;
205
Arthur Heymans16fe7902017-04-12 17:01:31 +0200206 /* Set the command/address... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200207 host_outb(base, SMBHSTCMD, address);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200208
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200209 /* Clear the data bytes... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200210 host_outb(base, SMBHSTDAT0, 0);
211 host_outb(base, SMBHSTDAT1, 0);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200212
213 /* Start the command */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200214 ret = execute_command(base);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300215 if (ret < 0)
216 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200217
218 /* Poll for transaction completion */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200219 ret = complete_command(base);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300220 if (ret < 0)
221 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200222
223 /* Read results of transaction */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200224 word = host_inb(base, SMBHSTDAT0);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200225 if (ctrl == I801_WORD_DATA)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200226 word |= host_inb(base, SMBHSTDAT1) << 8;
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200227
228 return word;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200229}
230
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200231static int smbus_write_cmd(uintptr_t base, u8 ctrl, u8 device, u8 address, u16 data)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200232{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300233 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200234
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300235 /* Set up for a byte data write. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200236 ret = setup_command(base, ctrl, XMIT_WRITE(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300237 if (ret < 0)
238 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200239
Arthur Heymans16fe7902017-04-12 17:01:31 +0200240 /* Set the command/address... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200241 host_outb(base, SMBHSTCMD, address);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200242
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200243 /* Set the data bytes... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200244 host_outb(base, SMBHSTDAT0, data & 0xff);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200245 if (ctrl == I801_WORD_DATA)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200246 host_outb(base, SMBHSTDAT1, data >> 8);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200247
248 /* Start the command */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200249 ret = execute_command(base);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300250 if (ret < 0)
251 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200252
253 /* Poll for transaction completion */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200254 return complete_command(base);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200255}
256
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200257static int block_cmd_loop(uintptr_t base, u8 *buf, size_t max_bytes, int flags)
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300258{
259 u8 status;
260 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200261 int ret;
262 size_t bytes = 0;
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300263 int is_write_cmd = flags & BLOCK_WRITE;
264 int sw_drives_nak = flags & BLOCK_I2C;
265
266 /* Hardware limitations. */
267 if (flags == (BLOCK_WRITE | BLOCK_I2C))
268 return SMBUS_ERROR;
269
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300270 /* Set number of bytes to transfer. */
271 /* Reset number of bytes to transfer so we notice later it
272 * was really updated with the transaction. */
273 if (!sw_drives_nak) {
274 if (is_write_cmd)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200275 host_outb(base, SMBHSTDAT0, max_bytes);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300276 else
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200277 host_outb(base, SMBHSTDAT0, 0);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300278 }
279
280 /* Send first byte from buffer, bytes_sent increments after
281 * hardware acknowledges it.
282 */
283 if (is_write_cmd)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200284 host_outb(base, SMBBLKDAT, *buf++);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300285
286 /* Start the command */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200287 ret = execute_command(base);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300288 if (ret < 0)
289 return ret;
290
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300291 /* Poll for transaction completion */
292 do {
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200293 status = host_inb(base, SMBHSTSTAT);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300294
295 if (status & SMBHSTSTS_BYTE_DONE) { /* Byte done */
296
297 if (is_write_cmd) {
298 bytes++;
299 if (bytes < max_bytes)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200300 host_outb(base, SMBBLKDAT, *buf++);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300301 } else {
302 if (bytes < max_bytes)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200303 *buf++ = host_inb(base, SMBBLKDAT);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300304 bytes++;
305
306 /* Indicate that next byte is the last one. */
307 if (sw_drives_nak && (bytes + 1 >= max_bytes)) {
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200308 host_and_or(base, SMBHSTCTL, 0xff,
309 SMBHSTCNT_LAST_BYTE);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300310 }
311
312 }
313
314 /* Engine internally completes the transaction
315 * and clears HOST_BUSY flag once the byte count
316 * has been reached or LAST_BYTE was set.
317 */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200318 host_outb(base, SMBHSTSTAT, SMBHSTSTS_BYTE_DONE);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300319 }
320
321 } while (--loops && !host_completed(status));
322
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200323 dprintk("%s: status = %02x, len = %zd / %zd, loops = %d\n",
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300324 __func__, status, bytes, max_bytes, SMBUS_TIMEOUT - loops);
325
326 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200327 return recover_master(base, SMBUS_WAIT_UNTIL_DONE_TIMEOUT);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300328
329 ret = cb_err_from_stat(status);
330 if (ret < 0)
331 return ret;
332
333 return bytes;
334}
335
Kyösti Mälkki1cae4542020-01-06 12:31:34 +0200336int do_smbus_read_byte(uintptr_t smbus_base, u8 device, u8 address)
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200337{
338 return smbus_read_cmd(smbus_base, I801_BYTE_DATA, device, address);
339}
340
Kyösti Mälkki1cae4542020-01-06 12:31:34 +0200341int do_smbus_read_word(uintptr_t smbus_base, u8 device, u8 address)
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200342{
343 return smbus_read_cmd(smbus_base, I801_WORD_DATA, device, address);
344}
345
Kyösti Mälkki1cae4542020-01-06 12:31:34 +0200346int do_smbus_write_byte(uintptr_t smbus_base, u8 device, u8 address, u8 data)
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200347{
348 return smbus_write_cmd(smbus_base, I801_BYTE_DATA, device, address, data);
349}
350
Kyösti Mälkki1cae4542020-01-06 12:31:34 +0200351int do_smbus_write_word(uintptr_t smbus_base, u8 device, u8 address, u16 data)
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200352{
353 return smbus_write_cmd(smbus_base, I801_WORD_DATA, device, address, data);
354}
355
Kyösti Mälkki1cae4542020-01-06 12:31:34 +0200356int do_smbus_block_read(uintptr_t smbus_base, u8 device, u8 cmd, size_t max_bytes, u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200357{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300358 int ret, slave_bytes;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200359
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300360 max_bytes = MIN(SMBUS_BLOCK_MAXLEN, max_bytes);
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200361
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300362 /* Set up for a block data read. */
363 ret = setup_command(smbus_base, I801_BLOCK_DATA, XMIT_READ(device));
364 if (ret < 0)
365 return ret;
366
Arthur Heymans16fe7902017-04-12 17:01:31 +0200367 /* Set the command/address... */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200368 host_outb(smbus_base, SMBHSTCMD, cmd);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200369
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300370 /* Execute block transaction. */
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300371 ret = block_cmd_loop(smbus_base, buf, max_bytes, BLOCK_READ);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300372 if (ret < 0)
373 return ret;
374
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300375 /* Post-check we received complete message. */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200376 slave_bytes = host_inb(smbus_base, SMBHSTDAT0);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300377 if (ret < slave_bytes)
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200378 return SMBUS_ERROR;
379
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300380 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200381}
382
Kyösti Mälkki1cae4542020-01-06 12:31:34 +0200383int do_smbus_block_write(uintptr_t smbus_base, u8 device, u8 cmd, const size_t bytes, const u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200384{
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300385 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200386
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300387 if (bytes > SMBUS_BLOCK_MAXLEN)
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200388 return SMBUS_ERROR;
389
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300390 /* Set up for a block data write. */
391 ret = setup_command(smbus_base, I801_BLOCK_DATA, XMIT_WRITE(device));
392 if (ret < 0)
393 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200394
Arthur Heymans16fe7902017-04-12 17:01:31 +0200395 /* Set the command/address... */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200396 host_outb(smbus_base, SMBHSTCMD, cmd);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200397
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300398 /* Execute block transaction. */
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300399 ret = block_cmd_loop(smbus_base, (u8 *)buf, bytes, BLOCK_WRITE);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300400 if (ret < 0)
401 return ret;
402
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300403 if (ret < bytes)
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300404 return SMBUS_ERROR;
405
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300406 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200407}
408
409/* Only since ICH5 */
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200410static int has_i2c_read_command(void)
411{
Julius Wernercd49cce2019-03-05 16:53:33 -0800412 if (CONFIG(SOUTHBRIDGE_INTEL_I82371EB) ||
413 CONFIG(SOUTHBRIDGE_INTEL_I82801DX))
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200414 return 0;
415 return 1;
416}
417
Kyösti Mälkki1cae4542020-01-06 12:31:34 +0200418int do_i2c_eeprom_read(uintptr_t smbus_base, u8 device, u8 offset, const size_t bytes, u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200419{
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300420 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200421
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200422 if (!has_i2c_read_command())
423 return SMBUS_ERROR;
424
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300425 /* Set up for a i2c block data read.
426 *
427 * FIXME: Address parameter changes to XMIT_READ(device) with
428 * some revision of PCH. Presumably hardware revisions that
429 * do not have i2c block write support internally set LSB.
430 */
431 ret = setup_command(smbus_base, I801_I2C_BLOCK_DATA,
432 XMIT_WRITE(device));
433 if (ret < 0)
434 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200435
436 /* device offset */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200437 host_outb(smbus_base, SMBHSTDAT1, offset);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200438
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300439 /* Execute block transaction. */
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300440 ret = block_cmd_loop(smbus_base, buf, bytes, BLOCK_READ | BLOCK_I2C);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300441 if (ret < 0)
442 return ret;
443
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300444 /* Post-check we received complete message. */
445 if (ret < bytes)
Kyösti Mälkki1e392362017-08-20 21:36:03 +0300446 return SMBUS_ERROR;
447
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300448 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200449}
Frans Hendrikse48be352019-06-19 11:01:27 +0200450
451/*
452 * The caller is responsible of settings HOSTC I2C_EN bit prior to making this
453 * call!
454 */
Kyösti Mälkki1cae4542020-01-06 12:31:34 +0200455int do_i2c_block_write(uintptr_t smbus_base, u8 device, size_t bytes, u8 *buf)
Frans Hendrikse48be352019-06-19 11:01:27 +0200456{
457 u8 cmd;
458 int ret;
459
460 if (!CONFIG(SOC_INTEL_BRASWELL))
461 return SMBUS_ERROR;
462
463 if (!bytes || (bytes > SMBUS_BLOCK_MAXLEN))
464 return SMBUS_ERROR;
465
466 /* Set up for a block data write. */
467 ret = setup_command(smbus_base, I801_BLOCK_DATA, XMIT_WRITE(device));
468 if (ret < 0)
469 return ret;
470
471 /*
472 * In i2c mode SMBus controller sequence on bus will be:
473 * <SMBXINTADD> <SMBHSTDAT1> <SMBBLKDAT> .. <SMBBLKDAT>
474 * The SMBHSTCMD must be written also to ensure the SMBUs controller
475 * will generate the i2c sequence.
476 */
477 cmd = *buf++;
478 bytes--;
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200479 host_outb(smbus_base, SMBHSTCMD, cmd);
480 host_outb(smbus_base, SMBHSTDAT1, cmd);
Frans Hendrikse48be352019-06-19 11:01:27 +0200481
482 /* Execute block transaction. */
483 ret = block_cmd_loop(smbus_base, buf, bytes, BLOCK_WRITE);
484 if (ret < 0)
485 return ret;
486
487 if (ret < bytes)
488 return SMBUS_ERROR;
489
490 ret++; /* 1st byte has been written using SMBHSTDAT1 */
491 return ret;
492}