blob: 0fb6386d4c5007979f9ffd6225e516c6f6a6305d [file] [log] [blame]
Arthur Heymans16fe7902017-04-12 17:01:31 +02001/*
2 * This file is part of the coreboot project.
3 *
Arthur Heymans16fe7902017-04-12 17:01:31 +02004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15#include <arch/io.h>
Kyösti Mälkkib5d998b2017-08-20 21:36:08 +030016#include <console/console.h>
Arthur Heymans16fe7902017-04-12 17:01:31 +020017#include <device/smbus_def.h>
Kyösti Mälkki1cae4542020-01-06 12:31:34 +020018#include <device/smbus_host.h>
Elyes HAOUASab89edb2019-05-15 21:10:44 +020019#include <types.h>
20
Julius Wernercd49cce2019-03-05 16:53:33 -080021#if CONFIG(DEBUG_SMBUS)
Kyösti Mälkkib5d998b2017-08-20 21:36:08 +030022#define dprintk(args...) printk(BIOS_DEBUG, ##args)
23#else
24#define dprintk(args...) do {} while (0)
25#endif
26
Kyösti Mälkki7f40bd62020-01-06 19:00:31 +020027/* SMBus register offsets. */
28#define SMBHSTSTAT 0x0
29#define SMBHSTCTL 0x2
30#define SMBHSTCMD 0x3
31#define SMBXMITADD 0x4
32#define SMBHSTDAT0 0x5
33#define SMBHSTDAT1 0x6
34#define SMBBLKDAT 0x7
35#define SMBTRNSADD 0x9
36#define SMBSLVDATA 0xa
37#define SMLINK_PIN_CTL 0xe
38#define SMBUS_PIN_CTL 0xf
39#define SMBSLVCMD 0x11
40
41#define SMB_RCV_SLVA SMBTRNSADD
42
Arthur Heymans16fe7902017-04-12 17:01:31 +020043/* I801 command constants */
44#define I801_QUICK (0 << 2)
45#define I801_BYTE (1 << 2)
46#define I801_BYTE_DATA (2 << 2)
47#define I801_WORD_DATA (3 << 2)
48#define I801_BLOCK_DATA (5 << 2)
49#define I801_I2C_BLOCK_DATA (6 << 2) /* ICH5 and later */
50
51/* I801 Host Control register bits */
52#define SMBHSTCNT_INTREN (1 << 0)
53#define SMBHSTCNT_KILL (1 << 1)
54#define SMBHSTCNT_LAST_BYTE (1 << 5)
55#define SMBHSTCNT_START (1 << 6)
56#define SMBHSTCNT_PEC_EN (1 << 7) /* ICH3 and later */
57
58/* I801 Hosts Status register bits */
59#define SMBHSTSTS_BYTE_DONE (1 << 7)
60#define SMBHSTSTS_INUSE_STS (1 << 6)
61#define SMBHSTSTS_SMBALERT_STS (1 << 5)
62#define SMBHSTSTS_FAILED (1 << 4)
63#define SMBHSTSTS_BUS_ERR (1 << 3)
64#define SMBHSTSTS_DEV_ERR (1 << 2)
65#define SMBHSTSTS_INTR (1 << 1)
66#define SMBHSTSTS_HOST_BUSY (1 << 0)
67
Kyösti Mälkki957511c2017-08-20 21:36:11 +030068/* For SMBXMITADD register. */
69#define XMIT_WRITE(dev) (((dev) << 1) | 0)
70#define XMIT_READ(dev) (((dev) << 1) | 1)
71
Arthur Heymans16fe7902017-04-12 17:01:31 +020072#define SMBUS_TIMEOUT (10 * 1000 * 100)
Elyes HAOUASb0f19882018-06-09 11:59:00 +020073#define SMBUS_BLOCK_MAXLEN 32
Arthur Heymans16fe7902017-04-12 17:01:31 +020074
Kyösti Mälkki893edee2017-08-20 21:36:24 +030075/* block_cmd_loop flags */
76#define BLOCK_READ 0
77#define BLOCK_WRITE (1 << 0)
78#define BLOCK_I2C (1 << 1)
79
Arthur Heymans16fe7902017-04-12 17:01:31 +020080static void smbus_delay(void)
81{
82 inb(0x80);
83}
84
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +020085static void host_outb(uintptr_t base, u8 reg, u8 value)
Kyösti Mälkkib49638d2020-01-02 16:36:56 +020086{
87 outb(value, base + reg);
88}
89
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +020090static u8 host_inb(uintptr_t base, u8 reg)
Kyösti Mälkkib49638d2020-01-02 16:36:56 +020091{
92 return inb(base + reg);
93}
94
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +020095static void host_and_or(uintptr_t base, u8 reg, u8 mask, u8 or)
Kyösti Mälkki65f5de22020-01-02 16:36:56 +020096{
97 u8 value;
98 value = host_inb(base, reg);
99 value &= mask;
100 value |= or;
101 host_outb(base, reg, value);
102}
103
Kyösti Mälkki7cdcc382020-01-06 19:00:31 +0200104void smbus_host_reset(uintptr_t base)
105{
106 /* Disable interrupt generation. */
107 host_outb(base, SMBHSTCTL, 0);
108
109 /* Clear any lingering errors, so transactions can run. */
110 host_and_or(base, SMBHSTSTAT, 0xff, 0);
111}
112
Kyösti Mälkki73451fd2020-01-06 19:00:31 +0200113void smbus_set_slave_addr(uintptr_t base, u8 slave_address)
114{
115 host_outb(base, SMB_RCV_SLVA, slave_address);
116}
117
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300118static int host_completed(u8 status)
119{
120 if (status & SMBHSTSTS_HOST_BUSY)
121 return 0;
Kyösti Mälkki44206e32019-02-26 17:17:24 +0200122
123 /* These status bits do not imply completion of transaction. */
124 status &= ~(SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INUSE_STS |
125 SMBHSTSTS_SMBALERT_STS);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300126 return status != 0;
127}
128
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200129static int recover_master(uintptr_t base, int ret)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200130{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300131 /* TODO: Depending of the failure, drive KILL transaction
132 * or force soft reset on SMBus master controller.
133 */
134 printk(BIOS_ERR, "SMBus: Fatal master timeout (%d)\n", ret);
135 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200136}
137
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300138static int cb_err_from_stat(u8 status)
139{
Kyösti Mälkki44206e32019-02-26 17:17:24 +0200140 /* These status bits do not imply errors. */
141 status &= ~(SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INUSE_STS |
142 SMBHSTSTS_SMBALERT_STS);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300143
144 if (status == SMBHSTSTS_INTR)
145 return 0;
146
147 return SMBUS_ERROR;
148}
149
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200150static int setup_command(uintptr_t base, u8 ctrl, u8 xmitadd)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200151{
152 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300153 u8 host_busy;
154
Arthur Heymans16fe7902017-04-12 17:01:31 +0200155 do {
156 smbus_delay();
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200157 host_busy = host_inb(base, SMBHSTSTAT) & SMBHSTSTS_HOST_BUSY;
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300158 } while (--loops && host_busy);
159
160 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200161 return recover_master(base, SMBUS_WAIT_UNTIL_READY_TIMEOUT);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300162
163 /* Clear any lingering errors, so the transaction will run. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200164 host_and_or(base, SMBHSTSTAT, 0xff, 0);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300165
166 /* Set up transaction */
167 /* Disable interrupts */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200168 host_outb(base, SMBHSTCTL, ctrl);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300169
170 /* Set the device I'm talking to. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200171 host_outb(base, SMBXMITADD, xmitadd);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300172
173 return 0;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200174}
175
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200176static int execute_command(uintptr_t base)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200177{
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300178 unsigned int loops = SMBUS_TIMEOUT;
179 u8 status;
180
181 /* Start the command. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200182 host_and_or(base, SMBHSTCTL, 0xff, SMBHSTCNT_START);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300183
184 /* Poll for it to start. */
Arthur Heymans16fe7902017-04-12 17:01:31 +0200185 do {
Arthur Heymans16fe7902017-04-12 17:01:31 +0200186 smbus_delay();
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300187
188 /* If we poll too slow, we could miss HOST_BUSY flag
189 * set and detect INTR or x_ERR flags instead here.
190 */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200191 status = host_inb(base, SMBHSTSTAT);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300192 status &= ~(SMBHSTSTS_SMBALERT_STS | SMBHSTSTS_INUSE_STS);
193 } while (--loops && status == 0);
194
195 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200196 return recover_master(base,
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300197 SMBUS_WAIT_UNTIL_ACTIVE_TIMEOUT);
198
199 return 0;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200200}
201
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200202static int complete_command(uintptr_t base)
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300203{
204 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300205 u8 status;
206
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300207 do {
208 smbus_delay();
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200209 status = host_inb(base, SMBHSTSTAT);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300210 } while (--loops && !host_completed(status));
211
212 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200213 return recover_master(base,
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300214 SMBUS_WAIT_UNTIL_DONE_TIMEOUT);
215
216 return cb_err_from_stat(status);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300217}
218
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200219static int smbus_read_cmd(uintptr_t base, u8 ctrl, u8 device, u8 address)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200220{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300221 int ret;
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200222 u16 word;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200223
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300224 /* Set up for a byte data read. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200225 ret = setup_command(base, ctrl, XMIT_READ(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300226 if (ret < 0)
227 return ret;
228
Arthur Heymans16fe7902017-04-12 17:01:31 +0200229 /* Set the command/address... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200230 host_outb(base, SMBHSTCMD, address);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200231
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200232 /* Clear the data bytes... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200233 host_outb(base, SMBHSTDAT0, 0);
234 host_outb(base, SMBHSTDAT1, 0);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200235
236 /* Start the command */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200237 ret = execute_command(base);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300238 if (ret < 0)
239 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200240
241 /* Poll for transaction completion */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200242 ret = complete_command(base);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300243 if (ret < 0)
244 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200245
246 /* Read results of transaction */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200247 word = host_inb(base, SMBHSTDAT0);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200248 if (ctrl == I801_WORD_DATA)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200249 word |= host_inb(base, SMBHSTDAT1) << 8;
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200250
251 return word;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200252}
253
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200254static int smbus_write_cmd(uintptr_t base, u8 ctrl, u8 device, u8 address, u16 data)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200255{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300256 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200257
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300258 /* Set up for a byte data write. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200259 ret = setup_command(base, ctrl, XMIT_WRITE(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300260 if (ret < 0)
261 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200262
Arthur Heymans16fe7902017-04-12 17:01:31 +0200263 /* Set the command/address... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200264 host_outb(base, SMBHSTCMD, address);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200265
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200266 /* Set the data bytes... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200267 host_outb(base, SMBHSTDAT0, data & 0xff);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200268 if (ctrl == I801_WORD_DATA)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200269 host_outb(base, SMBHSTDAT1, data >> 8);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200270
271 /* Start the command */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200272 ret = execute_command(base);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300273 if (ret < 0)
274 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200275
276 /* Poll for transaction completion */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200277 return complete_command(base);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200278}
279
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200280static int block_cmd_loop(uintptr_t base, u8 *buf, size_t max_bytes, int flags)
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300281{
282 u8 status;
283 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200284 int ret;
285 size_t bytes = 0;
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300286 int is_write_cmd = flags & BLOCK_WRITE;
287 int sw_drives_nak = flags & BLOCK_I2C;
288
289 /* Hardware limitations. */
290 if (flags == (BLOCK_WRITE | BLOCK_I2C))
291 return SMBUS_ERROR;
292
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300293 /* Set number of bytes to transfer. */
294 /* Reset number of bytes to transfer so we notice later it
295 * was really updated with the transaction. */
296 if (!sw_drives_nak) {
297 if (is_write_cmd)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200298 host_outb(base, SMBHSTDAT0, max_bytes);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300299 else
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200300 host_outb(base, SMBHSTDAT0, 0);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300301 }
302
303 /* Send first byte from buffer, bytes_sent increments after
304 * hardware acknowledges it.
305 */
306 if (is_write_cmd)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200307 host_outb(base, SMBBLKDAT, *buf++);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300308
309 /* Start the command */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200310 ret = execute_command(base);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300311 if (ret < 0)
312 return ret;
313
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300314 /* Poll for transaction completion */
315 do {
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200316 status = host_inb(base, SMBHSTSTAT);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300317
318 if (status & SMBHSTSTS_BYTE_DONE) { /* Byte done */
319
320 if (is_write_cmd) {
321 bytes++;
322 if (bytes < max_bytes)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200323 host_outb(base, SMBBLKDAT, *buf++);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300324 } else {
325 if (bytes < max_bytes)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200326 *buf++ = host_inb(base, SMBBLKDAT);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300327 bytes++;
328
329 /* Indicate that next byte is the last one. */
330 if (sw_drives_nak && (bytes + 1 >= max_bytes)) {
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200331 host_and_or(base, SMBHSTCTL, 0xff,
332 SMBHSTCNT_LAST_BYTE);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300333 }
334
335 }
336
337 /* Engine internally completes the transaction
338 * and clears HOST_BUSY flag once the byte count
339 * has been reached or LAST_BYTE was set.
340 */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200341 host_outb(base, SMBHSTSTAT, SMBHSTSTS_BYTE_DONE);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300342 }
343
344 } while (--loops && !host_completed(status));
345
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200346 dprintk("%s: status = %02x, len = %zd / %zd, loops = %d\n",
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300347 __func__, status, bytes, max_bytes, SMBUS_TIMEOUT - loops);
348
349 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200350 return recover_master(base, SMBUS_WAIT_UNTIL_DONE_TIMEOUT);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300351
352 ret = cb_err_from_stat(status);
353 if (ret < 0)
354 return ret;
355
356 return bytes;
357}
358
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200359int do_smbus_read_byte(uintptr_t base, u8 device, u8 address)
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200360{
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200361 return smbus_read_cmd(base, I801_BYTE_DATA, device, address);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200362}
363
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200364int do_smbus_read_word(uintptr_t base, u8 device, u8 address)
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200365{
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200366 return smbus_read_cmd(base, I801_WORD_DATA, device, address);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200367}
368
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200369int do_smbus_write_byte(uintptr_t base, u8 device, u8 address, u8 data)
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200370{
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200371 return smbus_write_cmd(base, I801_BYTE_DATA, device, address, data);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200372}
373
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200374int do_smbus_write_word(uintptr_t base, u8 device, u8 address, u16 data)
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200375{
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200376 return smbus_write_cmd(base, I801_WORD_DATA, device, address, data);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200377}
378
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200379int do_smbus_block_read(uintptr_t base, u8 device, u8 cmd, size_t max_bytes, u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200380{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300381 int ret, slave_bytes;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200382
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300383 max_bytes = MIN(SMBUS_BLOCK_MAXLEN, max_bytes);
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200384
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300385 /* Set up for a block data read. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200386 ret = setup_command(base, I801_BLOCK_DATA, XMIT_READ(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300387 if (ret < 0)
388 return ret;
389
Arthur Heymans16fe7902017-04-12 17:01:31 +0200390 /* Set the command/address... */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200391 host_outb(base, SMBHSTCMD, cmd);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200392
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300393 /* Execute block transaction. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200394 ret = block_cmd_loop(base, buf, max_bytes, BLOCK_READ);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300395 if (ret < 0)
396 return ret;
397
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300398 /* Post-check we received complete message. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200399 slave_bytes = host_inb(base, SMBHSTDAT0);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300400 if (ret < slave_bytes)
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200401 return SMBUS_ERROR;
402
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300403 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200404}
405
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200406int do_smbus_block_write(uintptr_t base, u8 device, u8 cmd, const size_t bytes, const u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200407{
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300408 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200409
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300410 if (bytes > SMBUS_BLOCK_MAXLEN)
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200411 return SMBUS_ERROR;
412
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300413 /* Set up for a block data write. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200414 ret = setup_command(base, I801_BLOCK_DATA, XMIT_WRITE(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300415 if (ret < 0)
416 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200417
Arthur Heymans16fe7902017-04-12 17:01:31 +0200418 /* Set the command/address... */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200419 host_outb(base, SMBHSTCMD, cmd);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200420
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300421 /* Execute block transaction. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200422 ret = block_cmd_loop(base, (u8 *)buf, bytes, BLOCK_WRITE);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300423 if (ret < 0)
424 return ret;
425
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300426 if (ret < bytes)
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300427 return SMBUS_ERROR;
428
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300429 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200430}
431
432/* Only since ICH5 */
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200433static int has_i2c_read_command(void)
434{
Julius Wernercd49cce2019-03-05 16:53:33 -0800435 if (CONFIG(SOUTHBRIDGE_INTEL_I82371EB) ||
436 CONFIG(SOUTHBRIDGE_INTEL_I82801DX))
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200437 return 0;
438 return 1;
439}
440
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200441int do_i2c_eeprom_read(uintptr_t base, u8 device, u8 offset, const size_t bytes, u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200442{
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300443 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200444
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200445 if (!has_i2c_read_command())
446 return SMBUS_ERROR;
447
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300448 /* Set up for a i2c block data read.
449 *
450 * FIXME: Address parameter changes to XMIT_READ(device) with
451 * some revision of PCH. Presumably hardware revisions that
452 * do not have i2c block write support internally set LSB.
453 */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200454 ret = setup_command(base, I801_I2C_BLOCK_DATA,
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300455 XMIT_WRITE(device));
456 if (ret < 0)
457 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200458
459 /* device offset */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200460 host_outb(base, SMBHSTDAT1, offset);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200461
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300462 /* Execute block transaction. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200463 ret = block_cmd_loop(base, buf, bytes, BLOCK_READ | BLOCK_I2C);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300464 if (ret < 0)
465 return ret;
466
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300467 /* Post-check we received complete message. */
468 if (ret < bytes)
Kyösti Mälkki1e392362017-08-20 21:36:03 +0300469 return SMBUS_ERROR;
470
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300471 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200472}
Frans Hendrikse48be352019-06-19 11:01:27 +0200473
474/*
475 * The caller is responsible of settings HOSTC I2C_EN bit prior to making this
476 * call!
477 */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200478int do_i2c_block_write(uintptr_t base, u8 device, size_t bytes, u8 *buf)
Frans Hendrikse48be352019-06-19 11:01:27 +0200479{
480 u8 cmd;
481 int ret;
482
483 if (!CONFIG(SOC_INTEL_BRASWELL))
484 return SMBUS_ERROR;
485
486 if (!bytes || (bytes > SMBUS_BLOCK_MAXLEN))
487 return SMBUS_ERROR;
488
489 /* Set up for a block data write. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200490 ret = setup_command(base, I801_BLOCK_DATA, XMIT_WRITE(device));
Frans Hendrikse48be352019-06-19 11:01:27 +0200491 if (ret < 0)
492 return ret;
493
494 /*
495 * In i2c mode SMBus controller sequence on bus will be:
496 * <SMBXINTADD> <SMBHSTDAT1> <SMBBLKDAT> .. <SMBBLKDAT>
497 * The SMBHSTCMD must be written also to ensure the SMBUs controller
498 * will generate the i2c sequence.
499 */
500 cmd = *buf++;
501 bytes--;
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200502 host_outb(base, SMBHSTCMD, cmd);
503 host_outb(base, SMBHSTDAT1, cmd);
Frans Hendrikse48be352019-06-19 11:01:27 +0200504
505 /* Execute block transaction. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200506 ret = block_cmd_loop(base, buf, bytes, BLOCK_WRITE);
Frans Hendrikse48be352019-06-19 11:01:27 +0200507 if (ret < 0)
508 return ret;
509
510 if (ret < bytes)
511 return SMBUS_ERROR;
512
513 ret++; /* 1st byte has been written using SMBHSTDAT1 */
514 return ret;
515}