blob: 962a7621d9872e9fba8055554d179c5ad225d8d5 [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>
Elyes HAOUASab89edb2019-05-15 21:10:44 +020022#include <types.h>
23
Arthur Heymans16fe7902017-04-12 17:01:31 +020024#include "smbus.h"
25
26
Julius Wernercd49cce2019-03-05 16:53:33 -080027#if CONFIG(DEBUG_SMBUS)
Kyösti Mälkkib5d998b2017-08-20 21:36:08 +030028#define dprintk(args...) printk(BIOS_DEBUG, ##args)
29#else
30#define dprintk(args...) do {} while (0)
31#endif
32
Arthur Heymans16fe7902017-04-12 17:01:31 +020033/* I801 command constants */
34#define I801_QUICK (0 << 2)
35#define I801_BYTE (1 << 2)
36#define I801_BYTE_DATA (2 << 2)
37#define I801_WORD_DATA (3 << 2)
38#define I801_BLOCK_DATA (5 << 2)
39#define I801_I2C_BLOCK_DATA (6 << 2) /* ICH5 and later */
40
41/* I801 Host Control register bits */
42#define SMBHSTCNT_INTREN (1 << 0)
43#define SMBHSTCNT_KILL (1 << 1)
44#define SMBHSTCNT_LAST_BYTE (1 << 5)
45#define SMBHSTCNT_START (1 << 6)
46#define SMBHSTCNT_PEC_EN (1 << 7) /* ICH3 and later */
47
48/* I801 Hosts Status register bits */
49#define SMBHSTSTS_BYTE_DONE (1 << 7)
50#define SMBHSTSTS_INUSE_STS (1 << 6)
51#define SMBHSTSTS_SMBALERT_STS (1 << 5)
52#define SMBHSTSTS_FAILED (1 << 4)
53#define SMBHSTSTS_BUS_ERR (1 << 3)
54#define SMBHSTSTS_DEV_ERR (1 << 2)
55#define SMBHSTSTS_INTR (1 << 1)
56#define SMBHSTSTS_HOST_BUSY (1 << 0)
57
Kyösti Mälkki957511c2017-08-20 21:36:11 +030058/* For SMBXMITADD register. */
59#define XMIT_WRITE(dev) (((dev) << 1) | 0)
60#define XMIT_READ(dev) (((dev) << 1) | 1)
61
Arthur Heymans16fe7902017-04-12 17:01:31 +020062#define SMBUS_TIMEOUT (10 * 1000 * 100)
Elyes HAOUASb0f19882018-06-09 11:59:00 +020063#define SMBUS_BLOCK_MAXLEN 32
Arthur Heymans16fe7902017-04-12 17:01:31 +020064
Kyösti Mälkki893edee2017-08-20 21:36:24 +030065/* block_cmd_loop flags */
66#define BLOCK_READ 0
67#define BLOCK_WRITE (1 << 0)
68#define BLOCK_I2C (1 << 1)
69
Arthur Heymans16fe7902017-04-12 17:01:31 +020070static void smbus_delay(void)
71{
72 inb(0x80);
73}
74
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +020075static void host_outb(uintptr_t base, u8 reg, u8 value)
Kyösti Mälkkib49638d2020-01-02 16:36:56 +020076{
77 outb(value, base + reg);
78}
79
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +020080static u8 host_inb(uintptr_t base, u8 reg)
Kyösti Mälkkib49638d2020-01-02 16:36:56 +020081{
82 return inb(base + reg);
83}
84
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +020085static void host_and_or(uintptr_t base, u8 reg, u8 mask, u8 or)
Kyösti Mälkki65f5de22020-01-02 16:36:56 +020086{
87 u8 value;
88 value = host_inb(base, reg);
89 value &= mask;
90 value |= or;
91 host_outb(base, reg, value);
92}
93
Kyösti Mälkkic38d5432017-08-20 21:36:18 +030094static int host_completed(u8 status)
95{
96 if (status & SMBHSTSTS_HOST_BUSY)
97 return 0;
Kyösti Mälkki44206e32019-02-26 17:17:24 +020098
99 /* These status bits do not imply completion of transaction. */
100 status &= ~(SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INUSE_STS |
101 SMBHSTSTS_SMBALERT_STS);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300102 return status != 0;
103}
104
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200105static int recover_master(uintptr_t base, int ret)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200106{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300107 /* TODO: Depending of the failure, drive KILL transaction
108 * or force soft reset on SMBus master controller.
109 */
110 printk(BIOS_ERR, "SMBus: Fatal master timeout (%d)\n", ret);
111 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200112}
113
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300114static int cb_err_from_stat(u8 status)
115{
Kyösti Mälkki44206e32019-02-26 17:17:24 +0200116 /* These status bits do not imply errors. */
117 status &= ~(SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INUSE_STS |
118 SMBHSTSTS_SMBALERT_STS);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300119
120 if (status == SMBHSTSTS_INTR)
121 return 0;
122
123 return SMBUS_ERROR;
124}
125
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200126static int setup_command(uintptr_t base, u8 ctrl, u8 xmitadd)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200127{
128 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300129 u8 host_busy;
130
Arthur Heymans16fe7902017-04-12 17:01:31 +0200131 do {
132 smbus_delay();
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200133 host_busy = host_inb(base, SMBHSTSTAT) & SMBHSTSTS_HOST_BUSY;
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300134 } while (--loops && host_busy);
135
136 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200137 return recover_master(base, SMBUS_WAIT_UNTIL_READY_TIMEOUT);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300138
139 /* Clear any lingering errors, so the transaction will run. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200140 host_and_or(base, SMBHSTSTAT, 0xff, 0);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300141
142 /* Set up transaction */
143 /* Disable interrupts */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200144 host_outb(base, SMBHSTCTL, ctrl);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300145
146 /* Set the device I'm talking to. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200147 host_outb(base, SMBXMITADD, xmitadd);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300148
149 return 0;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200150}
151
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200152static int execute_command(uintptr_t base)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200153{
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300154 unsigned int loops = SMBUS_TIMEOUT;
155 u8 status;
156
157 /* Start the command. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200158 host_and_or(base, SMBHSTCTL, 0xff, SMBHSTCNT_START);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300159
160 /* Poll for it to start. */
Arthur Heymans16fe7902017-04-12 17:01:31 +0200161 do {
Arthur Heymans16fe7902017-04-12 17:01:31 +0200162 smbus_delay();
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300163
164 /* If we poll too slow, we could miss HOST_BUSY flag
165 * set and detect INTR or x_ERR flags instead here.
166 */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200167 status = host_inb(base, SMBHSTSTAT);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300168 status &= ~(SMBHSTSTS_SMBALERT_STS | SMBHSTSTS_INUSE_STS);
169 } while (--loops && status == 0);
170
171 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200172 return recover_master(base,
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300173 SMBUS_WAIT_UNTIL_ACTIVE_TIMEOUT);
174
175 return 0;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200176}
177
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200178static int complete_command(uintptr_t base)
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300179{
180 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300181 u8 status;
182
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300183 do {
184 smbus_delay();
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200185 status = host_inb(base, SMBHSTSTAT);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300186 } while (--loops && !host_completed(status));
187
188 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200189 return recover_master(base,
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300190 SMBUS_WAIT_UNTIL_DONE_TIMEOUT);
191
192 return cb_err_from_stat(status);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300193}
194
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200195static int smbus_read_cmd(uintptr_t base, u8 ctrl, u8 device, u8 address)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200196{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300197 int ret;
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200198 u16 word;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200199
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300200 /* Set up for a byte data read. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200201 ret = setup_command(base, ctrl, XMIT_READ(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300202 if (ret < 0)
203 return ret;
204
Arthur Heymans16fe7902017-04-12 17:01:31 +0200205 /* Set the command/address... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200206 host_outb(base, SMBHSTCMD, address);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200207
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200208 /* Clear the data bytes... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200209 host_outb(base, SMBHSTDAT0, 0);
210 host_outb(base, SMBHSTDAT1, 0);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200211
212 /* Start the command */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200213 ret = execute_command(base);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300214 if (ret < 0)
215 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200216
217 /* Poll for transaction completion */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200218 ret = complete_command(base);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300219 if (ret < 0)
220 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200221
222 /* Read results of transaction */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200223 word = host_inb(base, SMBHSTDAT0);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200224 if (ctrl == I801_WORD_DATA)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200225 word |= host_inb(base, SMBHSTDAT1) << 8;
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200226
227 return word;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200228}
229
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200230static int smbus_write_cmd(uintptr_t base, u8 ctrl, u8 device, u8 address, u16 data)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200231{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300232 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200233
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300234 /* Set up for a byte data write. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200235 ret = setup_command(base, ctrl, XMIT_WRITE(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300236 if (ret < 0)
237 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200238
Arthur Heymans16fe7902017-04-12 17:01:31 +0200239 /* Set the command/address... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200240 host_outb(base, SMBHSTCMD, address);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200241
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200242 /* Set the data bytes... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200243 host_outb(base, SMBHSTDAT0, data & 0xff);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200244 if (ctrl == I801_WORD_DATA)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200245 host_outb(base, SMBHSTDAT1, data >> 8);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200246
247 /* Start the command */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200248 ret = execute_command(base);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300249 if (ret < 0)
250 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200251
252 /* Poll for transaction completion */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200253 return complete_command(base);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200254}
255
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200256static int block_cmd_loop(uintptr_t base, u8 *buf, size_t max_bytes, int flags)
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300257{
258 u8 status;
259 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200260 int ret;
261 size_t bytes = 0;
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300262 int is_write_cmd = flags & BLOCK_WRITE;
263 int sw_drives_nak = flags & BLOCK_I2C;
264
265 /* Hardware limitations. */
266 if (flags == (BLOCK_WRITE | BLOCK_I2C))
267 return SMBUS_ERROR;
268
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300269 /* Set number of bytes to transfer. */
270 /* Reset number of bytes to transfer so we notice later it
271 * was really updated with the transaction. */
272 if (!sw_drives_nak) {
273 if (is_write_cmd)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200274 host_outb(base, SMBHSTDAT0, max_bytes);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300275 else
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200276 host_outb(base, SMBHSTDAT0, 0);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300277 }
278
279 /* Send first byte from buffer, bytes_sent increments after
280 * hardware acknowledges it.
281 */
282 if (is_write_cmd)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200283 host_outb(base, SMBBLKDAT, *buf++);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300284
285 /* Start the command */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200286 ret = execute_command(base);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300287 if (ret < 0)
288 return ret;
289
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300290 /* Poll for transaction completion */
291 do {
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200292 status = host_inb(base, SMBHSTSTAT);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300293
294 if (status & SMBHSTSTS_BYTE_DONE) { /* Byte done */
295
296 if (is_write_cmd) {
297 bytes++;
298 if (bytes < max_bytes)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200299 host_outb(base, SMBBLKDAT, *buf++);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300300 } else {
301 if (bytes < max_bytes)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200302 *buf++ = host_inb(base, SMBBLKDAT);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300303 bytes++;
304
305 /* Indicate that next byte is the last one. */
306 if (sw_drives_nak && (bytes + 1 >= max_bytes)) {
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200307 host_and_or(base, SMBHSTCTL, 0xff,
308 SMBHSTCNT_LAST_BYTE);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300309 }
310
311 }
312
313 /* Engine internally completes the transaction
314 * and clears HOST_BUSY flag once the byte count
315 * has been reached or LAST_BYTE was set.
316 */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200317 host_outb(base, SMBHSTSTAT, SMBHSTSTS_BYTE_DONE);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300318 }
319
320 } while (--loops && !host_completed(status));
321
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200322 dprintk("%s: status = %02x, len = %zd / %zd, loops = %d\n",
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300323 __func__, status, bytes, max_bytes, SMBUS_TIMEOUT - loops);
324
325 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200326 return recover_master(base, SMBUS_WAIT_UNTIL_DONE_TIMEOUT);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300327
328 ret = cb_err_from_stat(status);
329 if (ret < 0)
330 return ret;
331
332 return bytes;
333}
334
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200335int do_smbus_read_byte(unsigned int smbus_base, u8 device, unsigned int address)
336{
337 return smbus_read_cmd(smbus_base, I801_BYTE_DATA, device, address);
338}
339
340int do_smbus_read_word(unsigned int smbus_base, u8 device, unsigned int address)
341{
342 return smbus_read_cmd(smbus_base, I801_WORD_DATA, device, address);
343}
344
345int do_smbus_write_byte(unsigned int smbus_base, u8 device, unsigned int address,
346 unsigned int data)
347{
348 return smbus_write_cmd(smbus_base, I801_BYTE_DATA, device, address, data);
349}
350
351int do_smbus_write_word(unsigned int smbus_base, u8 device, unsigned int address,
352 unsigned int data)
353{
354 return smbus_write_cmd(smbus_base, I801_WORD_DATA, device, address, data);
355}
356
Arthur Heymans16fe7902017-04-12 17:01:31 +0200357int do_smbus_block_read(unsigned int smbus_base, u8 device, u8 cmd,
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200358 unsigned int max_bytes, u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200359{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300360 int ret, slave_bytes;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200361
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300362 max_bytes = MIN(SMBUS_BLOCK_MAXLEN, max_bytes);
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200363
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300364 /* Set up for a block data read. */
365 ret = setup_command(smbus_base, I801_BLOCK_DATA, XMIT_READ(device));
366 if (ret < 0)
367 return ret;
368
Arthur Heymans16fe7902017-04-12 17:01:31 +0200369 /* Set the command/address... */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200370 host_outb(smbus_base, SMBHSTCMD, cmd);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200371
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300372 /* Execute block transaction. */
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300373 ret = block_cmd_loop(smbus_base, buf, max_bytes, BLOCK_READ);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300374 if (ret < 0)
375 return ret;
376
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300377 /* Post-check we received complete message. */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200378 slave_bytes = host_inb(smbus_base, SMBHSTDAT0);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300379 if (ret < slave_bytes)
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200380 return SMBUS_ERROR;
381
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300382 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200383}
384
385int do_smbus_block_write(unsigned int smbus_base, u8 device, u8 cmd,
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200386 const unsigned int bytes, const u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200387{
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300388 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200389
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300390 if (bytes > SMBUS_BLOCK_MAXLEN)
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200391 return SMBUS_ERROR;
392
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300393 /* Set up for a block data write. */
394 ret = setup_command(smbus_base, I801_BLOCK_DATA, XMIT_WRITE(device));
395 if (ret < 0)
396 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200397
Arthur Heymans16fe7902017-04-12 17:01:31 +0200398 /* Set the command/address... */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200399 host_outb(smbus_base, SMBHSTCMD, cmd);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200400
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300401 /* Execute block transaction. */
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300402 ret = block_cmd_loop(smbus_base, (u8 *)buf, bytes, BLOCK_WRITE);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300403 if (ret < 0)
404 return ret;
405
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300406 if (ret < bytes)
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300407 return SMBUS_ERROR;
408
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300409 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200410}
411
412/* Only since ICH5 */
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200413static int has_i2c_read_command(void)
414{
Julius Wernercd49cce2019-03-05 16:53:33 -0800415 if (CONFIG(SOUTHBRIDGE_INTEL_I82371EB) ||
416 CONFIG(SOUTHBRIDGE_INTEL_I82801DX))
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200417 return 0;
418 return 1;
419}
420
421int do_i2c_eeprom_read(unsigned int smbus_base, u8 device,
Kyösti Mälkki1e392362017-08-20 21:36:03 +0300422 unsigned int offset, const unsigned int bytes, u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200423{
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300424 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200425
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200426 if (!has_i2c_read_command())
427 return SMBUS_ERROR;
428
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300429 /* Set up for a i2c block data read.
430 *
431 * FIXME: Address parameter changes to XMIT_READ(device) with
432 * some revision of PCH. Presumably hardware revisions that
433 * do not have i2c block write support internally set LSB.
434 */
435 ret = setup_command(smbus_base, I801_I2C_BLOCK_DATA,
436 XMIT_WRITE(device));
437 if (ret < 0)
438 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200439
440 /* device offset */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200441 host_outb(smbus_base, SMBHSTDAT1, offset);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200442
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300443 /* Execute block transaction. */
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300444 ret = block_cmd_loop(smbus_base, buf, bytes, BLOCK_READ | BLOCK_I2C);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300445 if (ret < 0)
446 return ret;
447
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300448 /* Post-check we received complete message. */
449 if (ret < bytes)
Kyösti Mälkki1e392362017-08-20 21:36:03 +0300450 return SMBUS_ERROR;
451
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300452 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200453}
Frans Hendrikse48be352019-06-19 11:01:27 +0200454
455/*
456 * The caller is responsible of settings HOSTC I2C_EN bit prior to making this
457 * call!
458 */
459int do_i2c_block_write(unsigned int smbus_base, u8 device,
460 unsigned int bytes, u8 *buf)
461{
462 u8 cmd;
463 int ret;
464
465 if (!CONFIG(SOC_INTEL_BRASWELL))
466 return SMBUS_ERROR;
467
468 if (!bytes || (bytes > SMBUS_BLOCK_MAXLEN))
469 return SMBUS_ERROR;
470
471 /* Set up for a block data write. */
472 ret = setup_command(smbus_base, I801_BLOCK_DATA, XMIT_WRITE(device));
473 if (ret < 0)
474 return ret;
475
476 /*
477 * In i2c mode SMBus controller sequence on bus will be:
478 * <SMBXINTADD> <SMBHSTDAT1> <SMBBLKDAT> .. <SMBBLKDAT>
479 * The SMBHSTCMD must be written also to ensure the SMBUs controller
480 * will generate the i2c sequence.
481 */
482 cmd = *buf++;
483 bytes--;
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200484 host_outb(smbus_base, SMBHSTCMD, cmd);
485 host_outb(smbus_base, SMBHSTDAT1, cmd);
Frans Hendrikse48be352019-06-19 11:01:27 +0200486
487 /* Execute block transaction. */
488 ret = block_cmd_loop(smbus_base, buf, bytes, BLOCK_WRITE);
489 if (ret < 0)
490 return ret;
491
492 if (ret < bytes)
493 return SMBUS_ERROR;
494
495 ret++; /* 1st byte has been written using SMBHSTDAT1 */
496 return ret;
497}