blob: 95c2fd0957bddb9487fe8001e529f01efd62c793 [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Arthur Heymans16fe7902017-04-12 17:01:31 +02002
3#include <arch/io.h>
Kyösti Mälkkib5d998b2017-08-20 21:36:08 +03004#include <console/console.h>
Arthur Heymans16fe7902017-04-12 17:01:31 +02005#include <device/smbus_def.h>
Kyösti Mälkki1cae4542020-01-06 12:31:34 +02006#include <device/smbus_host.h>
Elyes HAOUASab89edb2019-05-15 21:10:44 +02007#include <types.h>
8
Julius Wernercd49cce2019-03-05 16:53:33 -08009#if CONFIG(DEBUG_SMBUS)
Kyösti Mälkkib5d998b2017-08-20 21:36:08 +030010#define dprintk(args...) printk(BIOS_DEBUG, ##args)
11#else
12#define dprintk(args...) do {} while (0)
13#endif
14
Kyösti Mälkki7f40bd62020-01-06 19:00:31 +020015/* SMBus register offsets. */
16#define SMBHSTSTAT 0x0
17#define SMBHSTCTL 0x2
18#define SMBHSTCMD 0x3
19#define SMBXMITADD 0x4
20#define SMBHSTDAT0 0x5
21#define SMBHSTDAT1 0x6
22#define SMBBLKDAT 0x7
23#define SMBTRNSADD 0x9
24#define SMBSLVDATA 0xa
25#define SMLINK_PIN_CTL 0xe
26#define SMBUS_PIN_CTL 0xf
27#define SMBSLVCMD 0x11
28
29#define SMB_RCV_SLVA SMBTRNSADD
30
Arthur Heymans16fe7902017-04-12 17:01:31 +020031/* I801 command constants */
32#define I801_QUICK (0 << 2)
33#define I801_BYTE (1 << 2)
34#define I801_BYTE_DATA (2 << 2)
35#define I801_WORD_DATA (3 << 2)
36#define I801_BLOCK_DATA (5 << 2)
37#define I801_I2C_BLOCK_DATA (6 << 2) /* ICH5 and later */
38
39/* I801 Host Control register bits */
40#define SMBHSTCNT_INTREN (1 << 0)
41#define SMBHSTCNT_KILL (1 << 1)
42#define SMBHSTCNT_LAST_BYTE (1 << 5)
43#define SMBHSTCNT_START (1 << 6)
44#define SMBHSTCNT_PEC_EN (1 << 7) /* ICH3 and later */
45
46/* I801 Hosts Status register bits */
47#define SMBHSTSTS_BYTE_DONE (1 << 7)
48#define SMBHSTSTS_INUSE_STS (1 << 6)
49#define SMBHSTSTS_SMBALERT_STS (1 << 5)
50#define SMBHSTSTS_FAILED (1 << 4)
51#define SMBHSTSTS_BUS_ERR (1 << 3)
52#define SMBHSTSTS_DEV_ERR (1 << 2)
53#define SMBHSTSTS_INTR (1 << 1)
54#define SMBHSTSTS_HOST_BUSY (1 << 0)
55
Kyösti Mälkki957511c2017-08-20 21:36:11 +030056/* For SMBXMITADD register. */
57#define XMIT_WRITE(dev) (((dev) << 1) | 0)
58#define XMIT_READ(dev) (((dev) << 1) | 1)
59
Arthur Heymans16fe7902017-04-12 17:01:31 +020060#define SMBUS_TIMEOUT (10 * 1000 * 100)
Elyes HAOUASb0f19882018-06-09 11:59:00 +020061#define SMBUS_BLOCK_MAXLEN 32
Arthur Heymans16fe7902017-04-12 17:01:31 +020062
Kyösti Mälkki893edee2017-08-20 21:36:24 +030063/* block_cmd_loop flags */
64#define BLOCK_READ 0
65#define BLOCK_WRITE (1 << 0)
66#define BLOCK_I2C (1 << 1)
67
Arthur Heymans16fe7902017-04-12 17:01:31 +020068static void smbus_delay(void)
69{
70 inb(0x80);
71}
72
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +020073static void host_outb(uintptr_t base, u8 reg, u8 value)
Kyösti Mälkkib49638d2020-01-02 16:36:56 +020074{
75 outb(value, base + reg);
76}
77
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +020078static u8 host_inb(uintptr_t base, u8 reg)
Kyösti Mälkkib49638d2020-01-02 16:36:56 +020079{
80 return inb(base + reg);
81}
82
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +020083static void host_and_or(uintptr_t base, u8 reg, u8 mask, u8 or)
Kyösti Mälkki65f5de22020-01-02 16:36:56 +020084{
85 u8 value;
86 value = host_inb(base, reg);
87 value &= mask;
88 value |= or;
89 host_outb(base, reg, value);
90}
91
Kyösti Mälkki7cdcc382020-01-06 19:00:31 +020092void smbus_host_reset(uintptr_t base)
93{
94 /* Disable interrupt generation. */
95 host_outb(base, SMBHSTCTL, 0);
96
97 /* Clear any lingering errors, so transactions can run. */
98 host_and_or(base, SMBHSTSTAT, 0xff, 0);
99}
100
Kyösti Mälkki73451fd2020-01-06 19:00:31 +0200101void smbus_set_slave_addr(uintptr_t base, u8 slave_address)
102{
103 host_outb(base, SMB_RCV_SLVA, slave_address);
104}
105
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300106static int host_completed(u8 status)
107{
108 if (status & SMBHSTSTS_HOST_BUSY)
109 return 0;
Kyösti Mälkki44206e32019-02-26 17:17:24 +0200110
111 /* These status bits do not imply completion of transaction. */
112 status &= ~(SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INUSE_STS |
113 SMBHSTSTS_SMBALERT_STS);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300114 return status != 0;
115}
116
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200117static int recover_master(uintptr_t base, int ret)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200118{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300119 /* TODO: Depending of the failure, drive KILL transaction
120 * or force soft reset on SMBus master controller.
121 */
122 printk(BIOS_ERR, "SMBus: Fatal master timeout (%d)\n", ret);
123 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200124}
125
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300126static int cb_err_from_stat(u8 status)
127{
Kyösti Mälkki44206e32019-02-26 17:17:24 +0200128 /* These status bits do not imply errors. */
129 status &= ~(SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INUSE_STS |
130 SMBHSTSTS_SMBALERT_STS);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300131
132 if (status == SMBHSTSTS_INTR)
133 return 0;
134
135 return SMBUS_ERROR;
136}
137
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200138static int setup_command(uintptr_t base, u8 ctrl, u8 xmitadd)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200139{
140 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300141 u8 host_busy;
142
Arthur Heymans16fe7902017-04-12 17:01:31 +0200143 do {
144 smbus_delay();
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200145 host_busy = host_inb(base, SMBHSTSTAT) & SMBHSTSTS_HOST_BUSY;
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300146 } while (--loops && host_busy);
147
148 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200149 return recover_master(base, SMBUS_WAIT_UNTIL_READY_TIMEOUT);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300150
151 /* Clear any lingering errors, so the transaction will run. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200152 host_and_or(base, SMBHSTSTAT, 0xff, 0);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300153
154 /* Set up transaction */
155 /* Disable interrupts */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200156 host_outb(base, SMBHSTCTL, ctrl);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300157
158 /* Set the device I'm talking to. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200159 host_outb(base, SMBXMITADD, xmitadd);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300160
161 return 0;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200162}
163
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200164static int execute_command(uintptr_t base)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200165{
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300166 unsigned int loops = SMBUS_TIMEOUT;
167 u8 status;
168
169 /* Start the command. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200170 host_and_or(base, SMBHSTCTL, 0xff, SMBHSTCNT_START);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300171
172 /* Poll for it to start. */
Arthur Heymans16fe7902017-04-12 17:01:31 +0200173 do {
Arthur Heymans16fe7902017-04-12 17:01:31 +0200174 smbus_delay();
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300175
176 /* If we poll too slow, we could miss HOST_BUSY flag
177 * set and detect INTR or x_ERR flags instead here.
178 */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200179 status = host_inb(base, SMBHSTSTAT);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300180 status &= ~(SMBHSTSTS_SMBALERT_STS | SMBHSTSTS_INUSE_STS);
181 } while (--loops && status == 0);
182
183 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200184 return recover_master(base,
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300185 SMBUS_WAIT_UNTIL_ACTIVE_TIMEOUT);
186
187 return 0;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200188}
189
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200190static int complete_command(uintptr_t base)
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300191{
192 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300193 u8 status;
194
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300195 do {
196 smbus_delay();
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200197 status = host_inb(base, SMBHSTSTAT);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300198 } while (--loops && !host_completed(status));
199
200 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200201 return recover_master(base,
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300202 SMBUS_WAIT_UNTIL_DONE_TIMEOUT);
203
204 return cb_err_from_stat(status);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300205}
206
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200207static int smbus_read_cmd(uintptr_t base, u8 ctrl, u8 device, u8 address)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200208{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300209 int ret;
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200210 u16 word;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200211
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300212 /* Set up for a byte data read. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200213 ret = setup_command(base, ctrl, XMIT_READ(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300214 if (ret < 0)
215 return ret;
216
Arthur Heymans16fe7902017-04-12 17:01:31 +0200217 /* Set the command/address... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200218 host_outb(base, SMBHSTCMD, address);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200219
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200220 /* Clear the data bytes... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200221 host_outb(base, SMBHSTDAT0, 0);
222 host_outb(base, SMBHSTDAT1, 0);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200223
224 /* Start the command */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200225 ret = execute_command(base);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300226 if (ret < 0)
227 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200228
229 /* Poll for transaction completion */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200230 ret = complete_command(base);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300231 if (ret < 0)
232 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200233
234 /* Read results of transaction */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200235 word = host_inb(base, SMBHSTDAT0);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200236 if (ctrl == I801_WORD_DATA)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200237 word |= host_inb(base, SMBHSTDAT1) << 8;
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200238
239 return word;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200240}
241
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200242static int smbus_write_cmd(uintptr_t base, u8 ctrl, u8 device, u8 address, u16 data)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200243{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300244 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200245
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300246 /* Set up for a byte data write. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200247 ret = setup_command(base, ctrl, XMIT_WRITE(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300248 if (ret < 0)
249 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200250
Arthur Heymans16fe7902017-04-12 17:01:31 +0200251 /* Set the command/address... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200252 host_outb(base, SMBHSTCMD, address);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200253
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200254 /* Set the data bytes... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200255 host_outb(base, SMBHSTDAT0, data & 0xff);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200256 if (ctrl == I801_WORD_DATA)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200257 host_outb(base, SMBHSTDAT1, data >> 8);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200258
259 /* Start the command */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200260 ret = execute_command(base);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300261 if (ret < 0)
262 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200263
264 /* Poll for transaction completion */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200265 return complete_command(base);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200266}
267
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200268static int block_cmd_loop(uintptr_t base, u8 *buf, size_t max_bytes, int flags)
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300269{
270 u8 status;
271 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200272 int ret;
273 size_t bytes = 0;
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300274 int is_write_cmd = flags & BLOCK_WRITE;
275 int sw_drives_nak = flags & BLOCK_I2C;
276
277 /* Hardware limitations. */
278 if (flags == (BLOCK_WRITE | BLOCK_I2C))
279 return SMBUS_ERROR;
280
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300281 /* Set number of bytes to transfer. */
282 /* Reset number of bytes to transfer so we notice later it
283 * was really updated with the transaction. */
284 if (!sw_drives_nak) {
285 if (is_write_cmd)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200286 host_outb(base, SMBHSTDAT0, max_bytes);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300287 else
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200288 host_outb(base, SMBHSTDAT0, 0);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300289 }
290
291 /* Send first byte from buffer, bytes_sent increments after
292 * hardware acknowledges it.
293 */
294 if (is_write_cmd)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200295 host_outb(base, SMBBLKDAT, *buf++);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300296
297 /* Start the command */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200298 ret = execute_command(base);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300299 if (ret < 0)
300 return ret;
301
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300302 /* Poll for transaction completion */
303 do {
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200304 status = host_inb(base, SMBHSTSTAT);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300305
306 if (status & SMBHSTSTS_BYTE_DONE) { /* Byte done */
307
308 if (is_write_cmd) {
309 bytes++;
310 if (bytes < max_bytes)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200311 host_outb(base, SMBBLKDAT, *buf++);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300312 } else {
313 if (bytes < max_bytes)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200314 *buf++ = host_inb(base, SMBBLKDAT);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300315 bytes++;
316
317 /* Indicate that next byte is the last one. */
318 if (sw_drives_nak && (bytes + 1 >= max_bytes)) {
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200319 host_and_or(base, SMBHSTCTL, 0xff,
320 SMBHSTCNT_LAST_BYTE);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300321 }
322
323 }
324
325 /* Engine internally completes the transaction
326 * and clears HOST_BUSY flag once the byte count
327 * has been reached or LAST_BYTE was set.
328 */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200329 host_outb(base, SMBHSTSTAT, SMBHSTSTS_BYTE_DONE);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300330 }
331
332 } while (--loops && !host_completed(status));
333
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200334 dprintk("%s: status = %02x, len = %zd / %zd, loops = %d\n",
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300335 __func__, status, bytes, max_bytes, SMBUS_TIMEOUT - loops);
336
337 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200338 return recover_master(base, SMBUS_WAIT_UNTIL_DONE_TIMEOUT);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300339
340 ret = cb_err_from_stat(status);
341 if (ret < 0)
342 return ret;
343
344 return bytes;
345}
346
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200347int do_smbus_read_byte(uintptr_t base, u8 device, u8 address)
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200348{
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200349 return smbus_read_cmd(base, I801_BYTE_DATA, device, address);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200350}
351
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200352int do_smbus_read_word(uintptr_t base, u8 device, u8 address)
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200353{
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200354 return smbus_read_cmd(base, I801_WORD_DATA, device, address);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200355}
356
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200357int do_smbus_write_byte(uintptr_t base, u8 device, u8 address, u8 data)
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200358{
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200359 return smbus_write_cmd(base, I801_BYTE_DATA, device, address, data);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200360}
361
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200362int do_smbus_write_word(uintptr_t base, u8 device, u8 address, u16 data)
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200363{
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200364 return smbus_write_cmd(base, I801_WORD_DATA, device, address, data);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200365}
366
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200367int do_smbus_block_read(uintptr_t base, u8 device, u8 cmd, size_t max_bytes, u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200368{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300369 int ret, slave_bytes;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200370
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300371 max_bytes = MIN(SMBUS_BLOCK_MAXLEN, max_bytes);
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200372
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300373 /* Set up for a block data read. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200374 ret = setup_command(base, I801_BLOCK_DATA, XMIT_READ(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300375 if (ret < 0)
376 return ret;
377
Arthur Heymans16fe7902017-04-12 17:01:31 +0200378 /* Set the command/address... */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200379 host_outb(base, SMBHSTCMD, cmd);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200380
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300381 /* Execute block transaction. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200382 ret = block_cmd_loop(base, buf, max_bytes, BLOCK_READ);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300383 if (ret < 0)
384 return ret;
385
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300386 /* Post-check we received complete message. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200387 slave_bytes = host_inb(base, SMBHSTDAT0);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300388 if (ret < slave_bytes)
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200389 return SMBUS_ERROR;
390
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300391 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200392}
393
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200394int 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 +0200395{
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300396 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200397
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300398 if (bytes > SMBUS_BLOCK_MAXLEN)
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200399 return SMBUS_ERROR;
400
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300401 /* Set up for a block data write. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200402 ret = setup_command(base, I801_BLOCK_DATA, XMIT_WRITE(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300403 if (ret < 0)
404 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200405
Arthur Heymans16fe7902017-04-12 17:01:31 +0200406 /* Set the command/address... */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200407 host_outb(base, SMBHSTCMD, cmd);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200408
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300409 /* Execute block transaction. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200410 ret = block_cmd_loop(base, (u8 *)buf, bytes, BLOCK_WRITE);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300411 if (ret < 0)
412 return ret;
413
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300414 if (ret < bytes)
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300415 return SMBUS_ERROR;
416
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300417 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200418}
419
420/* Only since ICH5 */
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200421static int has_i2c_read_command(void)
422{
Julius Wernercd49cce2019-03-05 16:53:33 -0800423 if (CONFIG(SOUTHBRIDGE_INTEL_I82371EB) ||
424 CONFIG(SOUTHBRIDGE_INTEL_I82801DX))
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200425 return 0;
426 return 1;
427}
428
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200429int do_i2c_eeprom_read(uintptr_t base, u8 device, u8 offset, const size_t bytes, u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200430{
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300431 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200432
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200433 if (!has_i2c_read_command())
434 return SMBUS_ERROR;
435
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300436 /* Set up for a i2c block data read.
437 *
438 * FIXME: Address parameter changes to XMIT_READ(device) with
439 * some revision of PCH. Presumably hardware revisions that
440 * do not have i2c block write support internally set LSB.
441 */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200442 ret = setup_command(base, I801_I2C_BLOCK_DATA,
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300443 XMIT_WRITE(device));
444 if (ret < 0)
445 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200446
447 /* device offset */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200448 host_outb(base, SMBHSTDAT1, offset);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200449
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300450 /* Execute block transaction. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200451 ret = block_cmd_loop(base, buf, bytes, BLOCK_READ | BLOCK_I2C);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300452 if (ret < 0)
453 return ret;
454
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300455 /* Post-check we received complete message. */
456 if (ret < bytes)
Kyösti Mälkki1e392362017-08-20 21:36:03 +0300457 return SMBUS_ERROR;
458
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300459 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200460}
Frans Hendrikse48be352019-06-19 11:01:27 +0200461
462/*
463 * The caller is responsible of settings HOSTC I2C_EN bit prior to making this
464 * call!
465 */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200466int do_i2c_block_write(uintptr_t base, u8 device, size_t bytes, u8 *buf)
Frans Hendrikse48be352019-06-19 11:01:27 +0200467{
468 u8 cmd;
469 int ret;
470
471 if (!CONFIG(SOC_INTEL_BRASWELL))
472 return SMBUS_ERROR;
473
474 if (!bytes || (bytes > SMBUS_BLOCK_MAXLEN))
475 return SMBUS_ERROR;
476
477 /* Set up for a block data write. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200478 ret = setup_command(base, I801_BLOCK_DATA, XMIT_WRITE(device));
Frans Hendrikse48be352019-06-19 11:01:27 +0200479 if (ret < 0)
480 return ret;
481
482 /*
483 * In i2c mode SMBus controller sequence on bus will be:
484 * <SMBXINTADD> <SMBHSTDAT1> <SMBBLKDAT> .. <SMBBLKDAT>
485 * The SMBHSTCMD must be written also to ensure the SMBUs controller
486 * will generate the i2c sequence.
487 */
488 cmd = *buf++;
489 bytes--;
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200490 host_outb(base, SMBHSTCMD, cmd);
491 host_outb(base, SMBHSTDAT1, cmd);
Frans Hendrikse48be352019-06-19 11:01:27 +0200492
493 /* Execute block transaction. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200494 ret = block_cmd_loop(base, buf, bytes, BLOCK_WRITE);
Frans Hendrikse48be352019-06-19 11:01:27 +0200495 if (ret < 0)
496 return ret;
497
498 if (ret < bytes)
499 return SMBUS_ERROR;
500
501 ret++; /* 1st byte has been written using SMBHSTDAT1 */
502 return ret;
503}