blob: b54f1d7a117fbc84215332917c84015bf37e02e3 [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
Julius Wernercd49cce2019-03-05 16:53:33 -080025#if CONFIG(DEBUG_SMBUS)
Kyösti Mälkkib5d998b2017-08-20 21:36:08 +030026#define dprintk(args...) printk(BIOS_DEBUG, ##args)
27#else
28#define dprintk(args...) do {} while (0)
29#endif
30
Kyösti Mälkki7f40bd62020-01-06 19:00:31 +020031/* SMBus register offsets. */
32#define SMBHSTSTAT 0x0
33#define SMBHSTCTL 0x2
34#define SMBHSTCMD 0x3
35#define SMBXMITADD 0x4
36#define SMBHSTDAT0 0x5
37#define SMBHSTDAT1 0x6
38#define SMBBLKDAT 0x7
39#define SMBTRNSADD 0x9
40#define SMBSLVDATA 0xa
41#define SMLINK_PIN_CTL 0xe
42#define SMBUS_PIN_CTL 0xf
43#define SMBSLVCMD 0x11
44
45#define SMB_RCV_SLVA SMBTRNSADD
46
Arthur Heymans16fe7902017-04-12 17:01:31 +020047/* I801 command constants */
48#define I801_QUICK (0 << 2)
49#define I801_BYTE (1 << 2)
50#define I801_BYTE_DATA (2 << 2)
51#define I801_WORD_DATA (3 << 2)
52#define I801_BLOCK_DATA (5 << 2)
53#define I801_I2C_BLOCK_DATA (6 << 2) /* ICH5 and later */
54
55/* I801 Host Control register bits */
56#define SMBHSTCNT_INTREN (1 << 0)
57#define SMBHSTCNT_KILL (1 << 1)
58#define SMBHSTCNT_LAST_BYTE (1 << 5)
59#define SMBHSTCNT_START (1 << 6)
60#define SMBHSTCNT_PEC_EN (1 << 7) /* ICH3 and later */
61
62/* I801 Hosts Status register bits */
63#define SMBHSTSTS_BYTE_DONE (1 << 7)
64#define SMBHSTSTS_INUSE_STS (1 << 6)
65#define SMBHSTSTS_SMBALERT_STS (1 << 5)
66#define SMBHSTSTS_FAILED (1 << 4)
67#define SMBHSTSTS_BUS_ERR (1 << 3)
68#define SMBHSTSTS_DEV_ERR (1 << 2)
69#define SMBHSTSTS_INTR (1 << 1)
70#define SMBHSTSTS_HOST_BUSY (1 << 0)
71
Kyösti Mälkki957511c2017-08-20 21:36:11 +030072/* For SMBXMITADD register. */
73#define XMIT_WRITE(dev) (((dev) << 1) | 0)
74#define XMIT_READ(dev) (((dev) << 1) | 1)
75
Arthur Heymans16fe7902017-04-12 17:01:31 +020076#define SMBUS_TIMEOUT (10 * 1000 * 100)
Elyes HAOUASb0f19882018-06-09 11:59:00 +020077#define SMBUS_BLOCK_MAXLEN 32
Arthur Heymans16fe7902017-04-12 17:01:31 +020078
Kyösti Mälkki893edee2017-08-20 21:36:24 +030079/* block_cmd_loop flags */
80#define BLOCK_READ 0
81#define BLOCK_WRITE (1 << 0)
82#define BLOCK_I2C (1 << 1)
83
Arthur Heymans16fe7902017-04-12 17:01:31 +020084static void smbus_delay(void)
85{
86 inb(0x80);
87}
88
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +020089static void host_outb(uintptr_t base, u8 reg, u8 value)
Kyösti Mälkkib49638d2020-01-02 16:36:56 +020090{
91 outb(value, base + reg);
92}
93
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +020094static u8 host_inb(uintptr_t base, u8 reg)
Kyösti Mälkkib49638d2020-01-02 16:36:56 +020095{
96 return inb(base + reg);
97}
98
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +020099static void host_and_or(uintptr_t base, u8 reg, u8 mask, u8 or)
Kyösti Mälkki65f5de22020-01-02 16:36:56 +0200100{
101 u8 value;
102 value = host_inb(base, reg);
103 value &= mask;
104 value |= or;
105 host_outb(base, reg, value);
106}
107
Kyösti Mälkki7cdcc382020-01-06 19:00:31 +0200108void smbus_host_reset(uintptr_t base)
109{
110 /* Disable interrupt generation. */
111 host_outb(base, SMBHSTCTL, 0);
112
113 /* Clear any lingering errors, so transactions can run. */
114 host_and_or(base, SMBHSTSTAT, 0xff, 0);
115}
116
Kyösti Mälkki73451fd2020-01-06 19:00:31 +0200117void smbus_set_slave_addr(uintptr_t base, u8 slave_address)
118{
119 host_outb(base, SMB_RCV_SLVA, slave_address);
120}
121
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300122static int host_completed(u8 status)
123{
124 if (status & SMBHSTSTS_HOST_BUSY)
125 return 0;
Kyösti Mälkki44206e32019-02-26 17:17:24 +0200126
127 /* These status bits do not imply completion of transaction. */
128 status &= ~(SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INUSE_STS |
129 SMBHSTSTS_SMBALERT_STS);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300130 return status != 0;
131}
132
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200133static int recover_master(uintptr_t base, int ret)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200134{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300135 /* TODO: Depending of the failure, drive KILL transaction
136 * or force soft reset on SMBus master controller.
137 */
138 printk(BIOS_ERR, "SMBus: Fatal master timeout (%d)\n", ret);
139 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200140}
141
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300142static int cb_err_from_stat(u8 status)
143{
Kyösti Mälkki44206e32019-02-26 17:17:24 +0200144 /* These status bits do not imply errors. */
145 status &= ~(SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INUSE_STS |
146 SMBHSTSTS_SMBALERT_STS);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300147
148 if (status == SMBHSTSTS_INTR)
149 return 0;
150
151 return SMBUS_ERROR;
152}
153
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200154static int setup_command(uintptr_t base, u8 ctrl, u8 xmitadd)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200155{
156 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300157 u8 host_busy;
158
Arthur Heymans16fe7902017-04-12 17:01:31 +0200159 do {
160 smbus_delay();
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200161 host_busy = host_inb(base, SMBHSTSTAT) & SMBHSTSTS_HOST_BUSY;
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300162 } while (--loops && host_busy);
163
164 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200165 return recover_master(base, SMBUS_WAIT_UNTIL_READY_TIMEOUT);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300166
167 /* Clear any lingering errors, so the transaction will run. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200168 host_and_or(base, SMBHSTSTAT, 0xff, 0);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300169
170 /* Set up transaction */
171 /* Disable interrupts */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200172 host_outb(base, SMBHSTCTL, ctrl);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300173
174 /* Set the device I'm talking to. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200175 host_outb(base, SMBXMITADD, xmitadd);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300176
177 return 0;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200178}
179
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200180static int execute_command(uintptr_t base)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200181{
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300182 unsigned int loops = SMBUS_TIMEOUT;
183 u8 status;
184
185 /* Start the command. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200186 host_and_or(base, SMBHSTCTL, 0xff, SMBHSTCNT_START);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300187
188 /* Poll for it to start. */
Arthur Heymans16fe7902017-04-12 17:01:31 +0200189 do {
Arthur Heymans16fe7902017-04-12 17:01:31 +0200190 smbus_delay();
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300191
192 /* If we poll too slow, we could miss HOST_BUSY flag
193 * set and detect INTR or x_ERR flags instead here.
194 */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200195 status = host_inb(base, SMBHSTSTAT);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300196 status &= ~(SMBHSTSTS_SMBALERT_STS | SMBHSTSTS_INUSE_STS);
197 } while (--loops && status == 0);
198
199 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200200 return recover_master(base,
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300201 SMBUS_WAIT_UNTIL_ACTIVE_TIMEOUT);
202
203 return 0;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200204}
205
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200206static int complete_command(uintptr_t base)
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300207{
208 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300209 u8 status;
210
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300211 do {
212 smbus_delay();
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200213 status = host_inb(base, SMBHSTSTAT);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300214 } while (--loops && !host_completed(status));
215
216 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200217 return recover_master(base,
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300218 SMBUS_WAIT_UNTIL_DONE_TIMEOUT);
219
220 return cb_err_from_stat(status);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300221}
222
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200223static int smbus_read_cmd(uintptr_t base, u8 ctrl, u8 device, u8 address)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200224{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300225 int ret;
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200226 u16 word;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200227
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300228 /* Set up for a byte data read. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200229 ret = setup_command(base, ctrl, XMIT_READ(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300230 if (ret < 0)
231 return ret;
232
Arthur Heymans16fe7902017-04-12 17:01:31 +0200233 /* Set the command/address... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200234 host_outb(base, SMBHSTCMD, address);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200235
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200236 /* Clear the data bytes... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200237 host_outb(base, SMBHSTDAT0, 0);
238 host_outb(base, SMBHSTDAT1, 0);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200239
240 /* Start the command */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200241 ret = execute_command(base);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300242 if (ret < 0)
243 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200244
245 /* Poll for transaction completion */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200246 ret = complete_command(base);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300247 if (ret < 0)
248 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200249
250 /* Read results of transaction */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200251 word = host_inb(base, SMBHSTDAT0);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200252 if (ctrl == I801_WORD_DATA)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200253 word |= host_inb(base, SMBHSTDAT1) << 8;
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200254
255 return word;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200256}
257
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200258static int smbus_write_cmd(uintptr_t base, u8 ctrl, u8 device, u8 address, u16 data)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200259{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300260 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200261
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300262 /* Set up for a byte data write. */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200263 ret = setup_command(base, ctrl, XMIT_WRITE(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300264 if (ret < 0)
265 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200266
Arthur Heymans16fe7902017-04-12 17:01:31 +0200267 /* Set the command/address... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200268 host_outb(base, SMBHSTCMD, address);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200269
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200270 /* Set the data bytes... */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200271 host_outb(base, SMBHSTDAT0, data & 0xff);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200272 if (ctrl == I801_WORD_DATA)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200273 host_outb(base, SMBHSTDAT1, data >> 8);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200274
275 /* Start the command */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200276 ret = execute_command(base);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300277 if (ret < 0)
278 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200279
280 /* Poll for transaction completion */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200281 return complete_command(base);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200282}
283
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200284static int block_cmd_loop(uintptr_t base, u8 *buf, size_t max_bytes, int flags)
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300285{
286 u8 status;
287 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200288 int ret;
289 size_t bytes = 0;
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300290 int is_write_cmd = flags & BLOCK_WRITE;
291 int sw_drives_nak = flags & BLOCK_I2C;
292
293 /* Hardware limitations. */
294 if (flags == (BLOCK_WRITE | BLOCK_I2C))
295 return SMBUS_ERROR;
296
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300297 /* Set number of bytes to transfer. */
298 /* Reset number of bytes to transfer so we notice later it
299 * was really updated with the transaction. */
300 if (!sw_drives_nak) {
301 if (is_write_cmd)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200302 host_outb(base, SMBHSTDAT0, max_bytes);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300303 else
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200304 host_outb(base, SMBHSTDAT0, 0);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300305 }
306
307 /* Send first byte from buffer, bytes_sent increments after
308 * hardware acknowledges it.
309 */
310 if (is_write_cmd)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200311 host_outb(base, SMBBLKDAT, *buf++);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300312
313 /* Start the command */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200314 ret = execute_command(base);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300315 if (ret < 0)
316 return ret;
317
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300318 /* Poll for transaction completion */
319 do {
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200320 status = host_inb(base, SMBHSTSTAT);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300321
322 if (status & SMBHSTSTS_BYTE_DONE) { /* Byte done */
323
324 if (is_write_cmd) {
325 bytes++;
326 if (bytes < max_bytes)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200327 host_outb(base, SMBBLKDAT, *buf++);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300328 } else {
329 if (bytes < max_bytes)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200330 *buf++ = host_inb(base, SMBBLKDAT);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300331 bytes++;
332
333 /* Indicate that next byte is the last one. */
334 if (sw_drives_nak && (bytes + 1 >= max_bytes)) {
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200335 host_and_or(base, SMBHSTCTL, 0xff,
336 SMBHSTCNT_LAST_BYTE);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300337 }
338
339 }
340
341 /* Engine internally completes the transaction
342 * and clears HOST_BUSY flag once the byte count
343 * has been reached or LAST_BYTE was set.
344 */
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200345 host_outb(base, SMBHSTSTAT, SMBHSTSTS_BYTE_DONE);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300346 }
347
348 } while (--loops && !host_completed(status));
349
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200350 dprintk("%s: status = %02x, len = %zd / %zd, loops = %d\n",
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300351 __func__, status, bytes, max_bytes, SMBUS_TIMEOUT - loops);
352
353 if (loops == 0)
Kyösti Mälkki5e9ae0c2020-01-06 13:35:59 +0200354 return recover_master(base, SMBUS_WAIT_UNTIL_DONE_TIMEOUT);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300355
356 ret = cb_err_from_stat(status);
357 if (ret < 0)
358 return ret;
359
360 return bytes;
361}
362
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200363int do_smbus_read_byte(uintptr_t base, u8 device, u8 address)
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200364{
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200365 return smbus_read_cmd(base, I801_BYTE_DATA, device, address);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200366}
367
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200368int do_smbus_read_word(uintptr_t base, u8 device, u8 address)
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200369{
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200370 return smbus_read_cmd(base, I801_WORD_DATA, device, address);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200371}
372
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200373int do_smbus_write_byte(uintptr_t base, u8 device, u8 address, u8 data)
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200374{
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200375 return smbus_write_cmd(base, I801_BYTE_DATA, device, address, data);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200376}
377
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200378int do_smbus_write_word(uintptr_t base, u8 device, u8 address, u16 data)
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200379{
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200380 return smbus_write_cmd(base, I801_WORD_DATA, device, address, data);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200381}
382
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200383int do_smbus_block_read(uintptr_t base, u8 device, u8 cmd, size_t max_bytes, u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200384{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300385 int ret, slave_bytes;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200386
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300387 max_bytes = MIN(SMBUS_BLOCK_MAXLEN, max_bytes);
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200388
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300389 /* Set up for a block data read. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200390 ret = setup_command(base, I801_BLOCK_DATA, XMIT_READ(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300391 if (ret < 0)
392 return ret;
393
Arthur Heymans16fe7902017-04-12 17:01:31 +0200394 /* Set the command/address... */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200395 host_outb(base, SMBHSTCMD, cmd);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200396
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300397 /* Execute block transaction. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200398 ret = block_cmd_loop(base, buf, max_bytes, BLOCK_READ);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300399 if (ret < 0)
400 return ret;
401
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300402 /* Post-check we received complete message. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200403 slave_bytes = host_inb(base, SMBHSTDAT0);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300404 if (ret < slave_bytes)
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200405 return SMBUS_ERROR;
406
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300407 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200408}
409
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200410int 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 +0200411{
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300412 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200413
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300414 if (bytes > SMBUS_BLOCK_MAXLEN)
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200415 return SMBUS_ERROR;
416
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300417 /* Set up for a block data write. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200418 ret = setup_command(base, I801_BLOCK_DATA, XMIT_WRITE(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300419 if (ret < 0)
420 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200421
Arthur Heymans16fe7902017-04-12 17:01:31 +0200422 /* Set the command/address... */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200423 host_outb(base, SMBHSTCMD, cmd);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200424
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300425 /* Execute block transaction. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200426 ret = block_cmd_loop(base, (u8 *)buf, bytes, BLOCK_WRITE);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300427 if (ret < 0)
428 return ret;
429
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300430 if (ret < bytes)
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300431 return SMBUS_ERROR;
432
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300433 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200434}
435
436/* Only since ICH5 */
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200437static int has_i2c_read_command(void)
438{
Julius Wernercd49cce2019-03-05 16:53:33 -0800439 if (CONFIG(SOUTHBRIDGE_INTEL_I82371EB) ||
440 CONFIG(SOUTHBRIDGE_INTEL_I82801DX))
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200441 return 0;
442 return 1;
443}
444
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200445int do_i2c_eeprom_read(uintptr_t base, u8 device, u8 offset, const size_t bytes, u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200446{
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300447 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200448
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200449 if (!has_i2c_read_command())
450 return SMBUS_ERROR;
451
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300452 /* Set up for a i2c block data read.
453 *
454 * FIXME: Address parameter changes to XMIT_READ(device) with
455 * some revision of PCH. Presumably hardware revisions that
456 * do not have i2c block write support internally set LSB.
457 */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200458 ret = setup_command(base, I801_I2C_BLOCK_DATA,
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300459 XMIT_WRITE(device));
460 if (ret < 0)
461 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200462
463 /* device offset */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200464 host_outb(base, SMBHSTDAT1, offset);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200465
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300466 /* Execute block transaction. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200467 ret = block_cmd_loop(base, buf, bytes, BLOCK_READ | BLOCK_I2C);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300468 if (ret < 0)
469 return ret;
470
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300471 /* Post-check we received complete message. */
472 if (ret < bytes)
Kyösti Mälkki1e392362017-08-20 21:36:03 +0300473 return SMBUS_ERROR;
474
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300475 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200476}
Frans Hendrikse48be352019-06-19 11:01:27 +0200477
478/*
479 * The caller is responsible of settings HOSTC I2C_EN bit prior to making this
480 * call!
481 */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200482int do_i2c_block_write(uintptr_t base, u8 device, size_t bytes, u8 *buf)
Frans Hendrikse48be352019-06-19 11:01:27 +0200483{
484 u8 cmd;
485 int ret;
486
487 if (!CONFIG(SOC_INTEL_BRASWELL))
488 return SMBUS_ERROR;
489
490 if (!bytes || (bytes > SMBUS_BLOCK_MAXLEN))
491 return SMBUS_ERROR;
492
493 /* Set up for a block data write. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200494 ret = setup_command(base, I801_BLOCK_DATA, XMIT_WRITE(device));
Frans Hendrikse48be352019-06-19 11:01:27 +0200495 if (ret < 0)
496 return ret;
497
498 /*
499 * In i2c mode SMBus controller sequence on bus will be:
500 * <SMBXINTADD> <SMBHSTDAT1> <SMBBLKDAT> .. <SMBBLKDAT>
501 * The SMBHSTCMD must be written also to ensure the SMBUs controller
502 * will generate the i2c sequence.
503 */
504 cmd = *buf++;
505 bytes--;
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200506 host_outb(base, SMBHSTCMD, cmd);
507 host_outb(base, SMBHSTDAT1, cmd);
Frans Hendrikse48be352019-06-19 11:01:27 +0200508
509 /* Execute block transaction. */
Kyösti Mälkkic5284262020-01-06 12:31:34 +0200510 ret = block_cmd_loop(base, buf, bytes, BLOCK_WRITE);
Frans Hendrikse48be352019-06-19 11:01:27 +0200511 if (ret < 0)
512 return ret;
513
514 if (ret < bytes)
515 return SMBUS_ERROR;
516
517 ret++; /* 1st byte has been written using SMBHSTDAT1 */
518 return ret;
519}