blob: f6805ad50fcdfa868c151230f2c410144986e115 [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älkkic38d5432017-08-20 21:36:18 +0300104static int host_completed(u8 status)
105{
106 if (status & SMBHSTSTS_HOST_BUSY)
107 return 0;
Kyösti Mälkki44206e32019-02-26 17:17:24 +0200108
109 /* These status bits do not imply completion of transaction. */
110 status &= ~(SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INUSE_STS |
111 SMBHSTSTS_SMBALERT_STS);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300112 return status != 0;
113}
114
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200115static int recover_master(uintptr_t base, int ret)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200116{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300117 /* TODO: Depending of the failure, drive KILL transaction
118 * or force soft reset on SMBus master controller.
119 */
120 printk(BIOS_ERR, "SMBus: Fatal master timeout (%d)\n", ret);
121 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200122}
123
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300124static int cb_err_from_stat(u8 status)
125{
Kyösti Mälkki44206e32019-02-26 17:17:24 +0200126 /* These status bits do not imply errors. */
127 status &= ~(SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INUSE_STS |
128 SMBHSTSTS_SMBALERT_STS);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300129
130 if (status == SMBHSTSTS_INTR)
131 return 0;
132
133 return SMBUS_ERROR;
134}
135
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200136static int setup_command(uintptr_t base, u8 ctrl, u8 xmitadd)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200137{
138 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300139 u8 host_busy;
140
Arthur Heymans16fe7902017-04-12 17:01:31 +0200141 do {
142 smbus_delay();
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200143 host_busy = host_inb(base, SMBHSTSTAT) & SMBHSTSTS_HOST_BUSY;
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300144 } while (--loops && host_busy);
145
146 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200147 return recover_master(base, SMBUS_WAIT_UNTIL_READY_TIMEOUT);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300148
149 /* Clear any lingering errors, so the transaction will run. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200150 host_and_or(base, SMBHSTSTAT, 0xff, 0);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300151
152 /* Set up transaction */
153 /* Disable interrupts */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200154 host_outb(base, SMBHSTCTL, ctrl);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300155
156 /* Set the device I'm talking to. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200157 host_outb(base, SMBXMITADD, xmitadd);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300158
159 return 0;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200160}
161
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200162static int execute_command(uintptr_t base)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200163{
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300164 unsigned int loops = SMBUS_TIMEOUT;
165 u8 status;
166
167 /* Start the command. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200168 host_and_or(base, SMBHSTCTL, 0xff, SMBHSTCNT_START);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300169
170 /* Poll for it to start. */
Arthur Heymans16fe7902017-04-12 17:01:31 +0200171 do {
Arthur Heymans16fe7902017-04-12 17:01:31 +0200172 smbus_delay();
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300173
174 /* If we poll too slow, we could miss HOST_BUSY flag
175 * set and detect INTR or x_ERR flags instead here.
176 */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200177 status = host_inb(base, SMBHSTSTAT);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300178 status &= ~(SMBHSTSTS_SMBALERT_STS | SMBHSTSTS_INUSE_STS);
179 } while (--loops && status == 0);
180
181 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200182 return recover_master(base,
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300183 SMBUS_WAIT_UNTIL_ACTIVE_TIMEOUT);
184
185 return 0;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200186}
187
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200188static int complete_command(uintptr_t base)
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300189{
190 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300191 u8 status;
192
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300193 do {
194 smbus_delay();
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200195 status = host_inb(base, SMBHSTSTAT);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300196 } while (--loops && !host_completed(status));
197
198 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200199 return recover_master(base,
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300200 SMBUS_WAIT_UNTIL_DONE_TIMEOUT);
201
202 return cb_err_from_stat(status);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300203}
204
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200205static int smbus_read_cmd(uintptr_t base, u8 ctrl, u8 device, u8 address)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200206{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300207 int ret;
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200208 u16 word;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200209
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300210 /* Set up for a byte data read. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200211 ret = setup_command(base, ctrl, XMIT_READ(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300212 if (ret < 0)
213 return ret;
214
Arthur Heymans16fe7902017-04-12 17:01:31 +0200215 /* Set the command/address... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200216 host_outb(base, SMBHSTCMD, address);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200217
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200218 /* Clear the data bytes... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200219 host_outb(base, SMBHSTDAT0, 0);
220 host_outb(base, SMBHSTDAT1, 0);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200221
222 /* Start the command */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200223 ret = execute_command(base);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300224 if (ret < 0)
225 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200226
227 /* Poll for transaction completion */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200228 ret = complete_command(base);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300229 if (ret < 0)
230 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200231
232 /* Read results of transaction */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200233 word = host_inb(base, SMBHSTDAT0);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200234 if (ctrl == I801_WORD_DATA)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200235 word |= host_inb(base, SMBHSTDAT1) << 8;
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200236
237 return word;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200238}
239
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200240static int smbus_write_cmd(uintptr_t base, u8 ctrl, u8 device, u8 address, u16 data)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200241{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300242 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200243
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300244 /* Set up for a byte data write. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200245 ret = setup_command(base, ctrl, XMIT_WRITE(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300246 if (ret < 0)
247 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200248
Arthur Heymans16fe7902017-04-12 17:01:31 +0200249 /* Set the command/address... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200250 host_outb(base, SMBHSTCMD, address);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200251
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200252 /* Set the data bytes... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200253 host_outb(base, SMBHSTDAT0, data & 0xff);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200254 if (ctrl == I801_WORD_DATA)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200255 host_outb(base, SMBHSTDAT1, data >> 8);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200256
257 /* Start the command */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200258 ret = execute_command(base);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300259 if (ret < 0)
260 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200261
262 /* Poll for transaction completion */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200263 return complete_command(base);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200264}
265
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200266static int block_cmd_loop(uintptr_t base, u8 *buf, size_t max_bytes, int flags)
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300267{
268 u8 status;
269 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200270 int ret;
271 size_t bytes = 0;
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300272 int is_write_cmd = flags & BLOCK_WRITE;
273 int sw_drives_nak = flags & BLOCK_I2C;
274
275 /* Hardware limitations. */
276 if (flags == (BLOCK_WRITE | BLOCK_I2C))
277 return SMBUS_ERROR;
278
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300279 /* Set number of bytes to transfer. */
280 /* Reset number of bytes to transfer so we notice later it
281 * was really updated with the transaction. */
282 if (!sw_drives_nak) {
283 if (is_write_cmd)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200284 host_outb(base, SMBHSTDAT0, max_bytes);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300285 else
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200286 host_outb(base, SMBHSTDAT0, 0);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300287 }
288
289 /* Send first byte from buffer, bytes_sent increments after
290 * hardware acknowledges it.
291 */
292 if (is_write_cmd)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200293 host_outb(base, SMBBLKDAT, *buf++);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300294
295 /* Start the command */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200296 ret = execute_command(base);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300297 if (ret < 0)
298 return ret;
299
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300300 /* Poll for transaction completion */
301 do {
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200302 status = host_inb(base, SMBHSTSTAT);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300303
304 if (status & SMBHSTSTS_BYTE_DONE) { /* Byte done */
305
306 if (is_write_cmd) {
307 bytes++;
308 if (bytes < max_bytes)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200309 host_outb(base, SMBBLKDAT, *buf++);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300310 } else {
311 if (bytes < max_bytes)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200312 *buf++ = host_inb(base, SMBBLKDAT);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300313 bytes++;
314
315 /* Indicate that next byte is the last one. */
316 if (sw_drives_nak && (bytes + 1 >= max_bytes)) {
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200317 host_and_or(base, SMBHSTCTL, 0xff,
318 SMBHSTCNT_LAST_BYTE);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300319 }
320
321 }
322
323 /* Engine internally completes the transaction
324 * and clears HOST_BUSY flag once the byte count
325 * has been reached or LAST_BYTE was set.
326 */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200327 host_outb(base, SMBHSTSTAT, SMBHSTSTS_BYTE_DONE);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300328 }
329
330 } while (--loops && !host_completed(status));
331
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200332 dprintk("%s: status = %02x, len = %zd / %zd, loops = %d\n",
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300333 __func__, status, bytes, max_bytes, SMBUS_TIMEOUT - loops);
334
335 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200336 return recover_master(base, SMBUS_WAIT_UNTIL_DONE_TIMEOUT);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300337
338 ret = cb_err_from_stat(status);
339 if (ret < 0)
340 return ret;
341
342 return bytes;
343}
344
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200345int do_smbus_read_byte(uintptr_t base, u8 device, u8 address)
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200346{
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200347 return smbus_read_cmd(base, I801_BYTE_DATA, device, address);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200348}
349
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200350int do_smbus_read_word(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_WORD_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_write_byte(uintptr_t base, u8 device, u8 address, u8 data)
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200356{
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200357 return smbus_write_cmd(base, I801_BYTE_DATA, device, address, data);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200358}
359
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200360int do_smbus_write_word(uintptr_t base, u8 device, u8 address, u16 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_WORD_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_block_read(uintptr_t base, u8 device, u8 cmd, size_t max_bytes, u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200366{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300367 int ret, slave_bytes;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200368
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300369 max_bytes = MIN(SMBUS_BLOCK_MAXLEN, max_bytes);
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200370
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300371 /* Set up for a block data read. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200372 ret = setup_command(base, I801_BLOCK_DATA, XMIT_READ(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300373 if (ret < 0)
374 return ret;
375
Arthur Heymans16fe7902017-04-12 17:01:31 +0200376 /* Set the command/address... */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200377 host_outb(base, SMBHSTCMD, cmd);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200378
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300379 /* Execute block transaction. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200380 ret = block_cmd_loop(base, buf, max_bytes, BLOCK_READ);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300381 if (ret < 0)
382 return ret;
383
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300384 /* Post-check we received complete message. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200385 slave_bytes = host_inb(base, SMBHSTDAT0);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300386 if (ret < slave_bytes)
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200387 return SMBUS_ERROR;
388
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300389 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200390}
391
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200392int 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 +0200393{
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300394 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200395
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300396 if (bytes > SMBUS_BLOCK_MAXLEN)
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200397 return SMBUS_ERROR;
398
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300399 /* Set up for a block data write. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200400 ret = setup_command(base, I801_BLOCK_DATA, XMIT_WRITE(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300401 if (ret < 0)
402 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200403
Arthur Heymans16fe7902017-04-12 17:01:31 +0200404 /* Set the command/address... */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200405 host_outb(base, SMBHSTCMD, cmd);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200406
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300407 /* Execute block transaction. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200408 ret = block_cmd_loop(base, (u8 *)buf, bytes, BLOCK_WRITE);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300409 if (ret < 0)
410 return ret;
411
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300412 if (ret < bytes)
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300413 return SMBUS_ERROR;
414
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300415 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200416}
417
418/* Only since ICH5 */
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200419static int has_i2c_read_command(void)
420{
Julius Wernercd49cce2019-03-05 16:53:33 -0800421 if (CONFIG(SOUTHBRIDGE_INTEL_I82371EB) ||
422 CONFIG(SOUTHBRIDGE_INTEL_I82801DX))
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200423 return 0;
424 return 1;
425}
426
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200427int do_i2c_eeprom_read(uintptr_t base, u8 device, u8 offset, const size_t bytes, u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200428{
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300429 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200430
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200431 if (!has_i2c_read_command())
432 return SMBUS_ERROR;
433
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300434 /* Set up for a i2c block data read.
435 *
436 * FIXME: Address parameter changes to XMIT_READ(device) with
437 * some revision of PCH. Presumably hardware revisions that
438 * do not have i2c block write support internally set LSB.
439 */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200440 ret = setup_command(base, I801_I2C_BLOCK_DATA,
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300441 XMIT_WRITE(device));
442 if (ret < 0)
443 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200444
445 /* device offset */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200446 host_outb(base, SMBHSTDAT1, offset);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200447
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300448 /* Execute block transaction. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200449 ret = block_cmd_loop(base, buf, bytes, BLOCK_READ | BLOCK_I2C);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300450 if (ret < 0)
451 return ret;
452
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300453 /* Post-check we received complete message. */
454 if (ret < bytes)
Kyösti Mälkki1e392362017-08-20 21:36:03 +0300455 return SMBUS_ERROR;
456
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300457 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200458}
Frans Hendrikse48be352019-06-19 11:01:27 +0200459
460/*
461 * The caller is responsible of settings HOSTC I2C_EN bit prior to making this
462 * call!
463 */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200464int do_i2c_block_write(uintptr_t base, u8 device, size_t bytes, u8 *buf)
Frans Hendrikse48be352019-06-19 11:01:27 +0200465{
466 u8 cmd;
467 int ret;
468
469 if (!CONFIG(SOC_INTEL_BRASWELL))
470 return SMBUS_ERROR;
471
472 if (!bytes || (bytes > SMBUS_BLOCK_MAXLEN))
473 return SMBUS_ERROR;
474
475 /* Set up for a block data write. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200476 ret = setup_command(base, I801_BLOCK_DATA, XMIT_WRITE(device));
Frans Hendrikse48be352019-06-19 11:01:27 +0200477 if (ret < 0)
478 return ret;
479
480 /*
481 * In i2c mode SMBus controller sequence on bus will be:
482 * <SMBXINTADD> <SMBHSTDAT1> <SMBBLKDAT> .. <SMBBLKDAT>
483 * The SMBHSTCMD must be written also to ensure the SMBUs controller
484 * will generate the i2c sequence.
485 */
486 cmd = *buf++;
487 bytes--;
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200488 host_outb(base, SMBHSTCMD, cmd);
489 host_outb(base, SMBHSTDAT1, cmd);
Frans Hendrikse48be352019-06-19 11:01:27 +0200490
491 /* Execute block transaction. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200492 ret = block_cmd_loop(base, buf, bytes, BLOCK_WRITE);
Frans Hendrikse48be352019-06-19 11:01:27 +0200493 if (ret < 0)
494 return ret;
495
496 if (ret < bytes)
497 return SMBUS_ERROR;
498
499 ret++; /* 1st byte has been written using SMBHSTDAT1 */
500 return ret;
501}