blob: d253ae7e04f78f523d38a1451fb243dc66a92962 [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älkki7cdcc382020-01-06 19:00:31 +020095void smbus_host_reset(uintptr_t base)
96{
97 /* Disable interrupt generation. */
98 host_outb(base, SMBHSTCTL, 0);
99
100 /* Clear any lingering errors, so transactions can run. */
101 host_and_or(base, SMBHSTSTAT, 0xff, 0);
102}
103
Kyösti Mälkki73451fd2020-01-06 19:00:31 +0200104void smbus_set_slave_addr(uintptr_t base, u8 slave_address)
105{
106 host_outb(base, SMB_RCV_SLVA, slave_address);
107}
108
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300109static int host_completed(u8 status)
110{
111 if (status & SMBHSTSTS_HOST_BUSY)
112 return 0;
Kyösti Mälkki44206e32019-02-26 17:17:24 +0200113
114 /* These status bits do not imply completion of transaction. */
115 status &= ~(SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INUSE_STS |
116 SMBHSTSTS_SMBALERT_STS);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300117 return status != 0;
118}
119
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200120static int recover_master(uintptr_t base, int ret)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200121{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300122 /* TODO: Depending of the failure, drive KILL transaction
123 * or force soft reset on SMBus master controller.
124 */
125 printk(BIOS_ERR, "SMBus: Fatal master timeout (%d)\n", ret);
126 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200127}
128
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300129static int cb_err_from_stat(u8 status)
130{
Kyösti Mälkki44206e32019-02-26 17:17:24 +0200131 /* These status bits do not imply errors. */
132 status &= ~(SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INUSE_STS |
133 SMBHSTSTS_SMBALERT_STS);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300134
135 if (status == SMBHSTSTS_INTR)
136 return 0;
137
138 return SMBUS_ERROR;
139}
140
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200141static int setup_command(uintptr_t base, u8 ctrl, u8 xmitadd)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200142{
143 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300144 u8 host_busy;
145
Arthur Heymans16fe7902017-04-12 17:01:31 +0200146 do {
147 smbus_delay();
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200148 host_busy = host_inb(base, SMBHSTSTAT) & SMBHSTSTS_HOST_BUSY;
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300149 } while (--loops && host_busy);
150
151 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200152 return recover_master(base, SMBUS_WAIT_UNTIL_READY_TIMEOUT);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300153
154 /* Clear any lingering errors, so the transaction will run. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200155 host_and_or(base, SMBHSTSTAT, 0xff, 0);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300156
157 /* Set up transaction */
158 /* Disable interrupts */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200159 host_outb(base, SMBHSTCTL, ctrl);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300160
161 /* Set the device I'm talking to. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200162 host_outb(base, SMBXMITADD, xmitadd);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300163
164 return 0;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200165}
166
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200167static int execute_command(uintptr_t base)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200168{
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300169 unsigned int loops = SMBUS_TIMEOUT;
170 u8 status;
171
172 /* Start the command. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200173 host_and_or(base, SMBHSTCTL, 0xff, SMBHSTCNT_START);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300174
175 /* Poll for it to start. */
Arthur Heymans16fe7902017-04-12 17:01:31 +0200176 do {
Arthur Heymans16fe7902017-04-12 17:01:31 +0200177 smbus_delay();
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300178
179 /* If we poll too slow, we could miss HOST_BUSY flag
180 * set and detect INTR or x_ERR flags instead here.
181 */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200182 status = host_inb(base, SMBHSTSTAT);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300183 status &= ~(SMBHSTSTS_SMBALERT_STS | SMBHSTSTS_INUSE_STS);
184 } while (--loops && status == 0);
185
186 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200187 return recover_master(base,
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300188 SMBUS_WAIT_UNTIL_ACTIVE_TIMEOUT);
189
190 return 0;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200191}
192
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200193static int complete_command(uintptr_t base)
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300194{
195 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300196 u8 status;
197
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300198 do {
199 smbus_delay();
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200200 status = host_inb(base, SMBHSTSTAT);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300201 } while (--loops && !host_completed(status));
202
203 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200204 return recover_master(base,
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300205 SMBUS_WAIT_UNTIL_DONE_TIMEOUT);
206
207 return cb_err_from_stat(status);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300208}
209
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200210static int smbus_read_cmd(uintptr_t base, u8 ctrl, u8 device, u8 address)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200211{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300212 int ret;
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200213 u16 word;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200214
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300215 /* Set up for a byte data read. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200216 ret = setup_command(base, ctrl, XMIT_READ(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300217 if (ret < 0)
218 return ret;
219
Arthur Heymans16fe7902017-04-12 17:01:31 +0200220 /* Set the command/address... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200221 host_outb(base, SMBHSTCMD, address);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200222
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200223 /* Clear the data bytes... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200224 host_outb(base, SMBHSTDAT0, 0);
225 host_outb(base, SMBHSTDAT1, 0);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200226
227 /* Start the command */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200228 ret = execute_command(base);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300229 if (ret < 0)
230 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200231
232 /* Poll for transaction completion */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200233 ret = complete_command(base);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300234 if (ret < 0)
235 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200236
237 /* Read results of transaction */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200238 word = host_inb(base, SMBHSTDAT0);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200239 if (ctrl == I801_WORD_DATA)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200240 word |= host_inb(base, SMBHSTDAT1) << 8;
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200241
242 return word;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200243}
244
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200245static int smbus_write_cmd(uintptr_t base, u8 ctrl, u8 device, u8 address, u16 data)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200246{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300247 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200248
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300249 /* Set up for a byte data write. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200250 ret = setup_command(base, ctrl, XMIT_WRITE(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300251 if (ret < 0)
252 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200253
Arthur Heymans16fe7902017-04-12 17:01:31 +0200254 /* Set the command/address... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200255 host_outb(base, SMBHSTCMD, address);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200256
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200257 /* Set the data bytes... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200258 host_outb(base, SMBHSTDAT0, data & 0xff);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200259 if (ctrl == I801_WORD_DATA)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200260 host_outb(base, SMBHSTDAT1, data >> 8);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200261
262 /* Start the command */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200263 ret = execute_command(base);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300264 if (ret < 0)
265 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200266
267 /* Poll for transaction completion */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200268 return complete_command(base);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200269}
270
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200271static int block_cmd_loop(uintptr_t base, u8 *buf, size_t max_bytes, int flags)
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300272{
273 u8 status;
274 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200275 int ret;
276 size_t bytes = 0;
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300277 int is_write_cmd = flags & BLOCK_WRITE;
278 int sw_drives_nak = flags & BLOCK_I2C;
279
280 /* Hardware limitations. */
281 if (flags == (BLOCK_WRITE | BLOCK_I2C))
282 return SMBUS_ERROR;
283
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300284 /* Set number of bytes to transfer. */
285 /* Reset number of bytes to transfer so we notice later it
286 * was really updated with the transaction. */
287 if (!sw_drives_nak) {
288 if (is_write_cmd)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200289 host_outb(base, SMBHSTDAT0, max_bytes);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300290 else
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200291 host_outb(base, SMBHSTDAT0, 0);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300292 }
293
294 /* Send first byte from buffer, bytes_sent increments after
295 * hardware acknowledges it.
296 */
297 if (is_write_cmd)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200298 host_outb(base, SMBBLKDAT, *buf++);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300299
300 /* Start the command */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200301 ret = execute_command(base);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300302 if (ret < 0)
303 return ret;
304
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300305 /* Poll for transaction completion */
306 do {
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200307 status = host_inb(base, SMBHSTSTAT);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300308
309 if (status & SMBHSTSTS_BYTE_DONE) { /* Byte done */
310
311 if (is_write_cmd) {
312 bytes++;
313 if (bytes < max_bytes)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200314 host_outb(base, SMBBLKDAT, *buf++);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300315 } else {
316 if (bytes < max_bytes)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200317 *buf++ = host_inb(base, SMBBLKDAT);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300318 bytes++;
319
320 /* Indicate that next byte is the last one. */
321 if (sw_drives_nak && (bytes + 1 >= max_bytes)) {
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200322 host_and_or(base, SMBHSTCTL, 0xff,
323 SMBHSTCNT_LAST_BYTE);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300324 }
325
326 }
327
328 /* Engine internally completes the transaction
329 * and clears HOST_BUSY flag once the byte count
330 * has been reached or LAST_BYTE was set.
331 */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200332 host_outb(base, SMBHSTSTAT, SMBHSTSTS_BYTE_DONE);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300333 }
334
335 } while (--loops && !host_completed(status));
336
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200337 dprintk("%s: status = %02x, len = %zd / %zd, loops = %d\n",
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300338 __func__, status, bytes, max_bytes, SMBUS_TIMEOUT - loops);
339
340 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200341 return recover_master(base, SMBUS_WAIT_UNTIL_DONE_TIMEOUT);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300342
343 ret = cb_err_from_stat(status);
344 if (ret < 0)
345 return ret;
346
347 return bytes;
348}
349
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200350int do_smbus_read_byte(uintptr_t base, u8 device, u8 address)
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200351{
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200352 return smbus_read_cmd(base, I801_BYTE_DATA, device, address);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200353}
354
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200355int do_smbus_read_word(uintptr_t base, u8 device, u8 address)
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200356{
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200357 return smbus_read_cmd(base, I801_WORD_DATA, device, address);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200358}
359
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200360int do_smbus_write_byte(uintptr_t base, u8 device, u8 address, u8 data)
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200361{
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200362 return smbus_write_cmd(base, I801_BYTE_DATA, device, address, data);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200363}
364
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200365int do_smbus_write_word(uintptr_t base, u8 device, u8 address, u16 data)
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200366{
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200367 return smbus_write_cmd(base, I801_WORD_DATA, device, address, data);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200368}
369
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200370int do_smbus_block_read(uintptr_t base, u8 device, u8 cmd, size_t max_bytes, u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200371{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300372 int ret, slave_bytes;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200373
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300374 max_bytes = MIN(SMBUS_BLOCK_MAXLEN, max_bytes);
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200375
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300376 /* Set up for a block data read. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200377 ret = setup_command(base, I801_BLOCK_DATA, XMIT_READ(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300378 if (ret < 0)
379 return ret;
380
Arthur Heymans16fe7902017-04-12 17:01:31 +0200381 /* Set the command/address... */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200382 host_outb(base, SMBHSTCMD, cmd);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200383
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300384 /* Execute block transaction. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200385 ret = block_cmd_loop(base, buf, max_bytes, BLOCK_READ);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300386 if (ret < 0)
387 return ret;
388
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300389 /* Post-check we received complete message. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200390 slave_bytes = host_inb(base, SMBHSTDAT0);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300391 if (ret < slave_bytes)
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200392 return SMBUS_ERROR;
393
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300394 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200395}
396
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200397int 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 +0200398{
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300399 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200400
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300401 if (bytes > SMBUS_BLOCK_MAXLEN)
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200402 return SMBUS_ERROR;
403
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300404 /* Set up for a block data write. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200405 ret = setup_command(base, I801_BLOCK_DATA, XMIT_WRITE(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300406 if (ret < 0)
407 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200408
Arthur Heymans16fe7902017-04-12 17:01:31 +0200409 /* Set the command/address... */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200410 host_outb(base, SMBHSTCMD, cmd);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200411
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300412 /* Execute block transaction. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200413 ret = block_cmd_loop(base, (u8 *)buf, bytes, BLOCK_WRITE);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300414 if (ret < 0)
415 return ret;
416
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300417 if (ret < bytes)
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300418 return SMBUS_ERROR;
419
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300420 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200421}
422
423/* Only since ICH5 */
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200424static int has_i2c_read_command(void)
425{
Julius Wernercd49cce2019-03-05 16:53:33 -0800426 if (CONFIG(SOUTHBRIDGE_INTEL_I82371EB) ||
427 CONFIG(SOUTHBRIDGE_INTEL_I82801DX))
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200428 return 0;
429 return 1;
430}
431
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200432int do_i2c_eeprom_read(uintptr_t base, u8 device, u8 offset, const size_t bytes, u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200433{
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300434 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200435
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200436 if (!has_i2c_read_command())
437 return SMBUS_ERROR;
438
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300439 /* Set up for a i2c block data read.
440 *
441 * FIXME: Address parameter changes to XMIT_READ(device) with
442 * some revision of PCH. Presumably hardware revisions that
443 * do not have i2c block write support internally set LSB.
444 */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200445 ret = setup_command(base, I801_I2C_BLOCK_DATA,
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300446 XMIT_WRITE(device));
447 if (ret < 0)
448 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200449
450 /* device offset */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200451 host_outb(base, SMBHSTDAT1, offset);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200452
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300453 /* Execute block transaction. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200454 ret = block_cmd_loop(base, buf, bytes, BLOCK_READ | BLOCK_I2C);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300455 if (ret < 0)
456 return ret;
457
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300458 /* Post-check we received complete message. */
459 if (ret < bytes)
Kyösti Mälkki1e392362017-08-20 21:36:03 +0300460 return SMBUS_ERROR;
461
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300462 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200463}
Frans Hendrikse48be352019-06-19 11:01:27 +0200464
465/*
466 * The caller is responsible of settings HOSTC I2C_EN bit prior to making this
467 * call!
468 */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200469int do_i2c_block_write(uintptr_t base, u8 device, size_t bytes, u8 *buf)
Frans Hendrikse48be352019-06-19 11:01:27 +0200470{
471 u8 cmd;
472 int ret;
473
474 if (!CONFIG(SOC_INTEL_BRASWELL))
475 return SMBUS_ERROR;
476
477 if (!bytes || (bytes > SMBUS_BLOCK_MAXLEN))
478 return SMBUS_ERROR;
479
480 /* Set up for a block data write. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200481 ret = setup_command(base, I801_BLOCK_DATA, XMIT_WRITE(device));
Frans Hendrikse48be352019-06-19 11:01:27 +0200482 if (ret < 0)
483 return ret;
484
485 /*
486 * In i2c mode SMBus controller sequence on bus will be:
487 * <SMBXINTADD> <SMBHSTDAT1> <SMBBLKDAT> .. <SMBBLKDAT>
488 * The SMBHSTCMD must be written also to ensure the SMBUs controller
489 * will generate the i2c sequence.
490 */
491 cmd = *buf++;
492 bytes--;
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200493 host_outb(base, SMBHSTCMD, cmd);
494 host_outb(base, SMBHSTDAT1, cmd);
Frans Hendrikse48be352019-06-19 11:01:27 +0200495
496 /* Execute block transaction. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200497 ret = block_cmd_loop(base, buf, bytes, BLOCK_WRITE);
Frans Hendrikse48be352019-06-19 11:01:27 +0200498 if (ret < 0)
499 return ret;
500
501 if (ret < bytes)
502 return SMBUS_ERROR;
503
504 ret++; /* 1st byte has been written using SMBHSTDAT1 */
505 return ret;
506}