blob: 30d0cd098ccbd17f679f7bf4a0dce43ea0a76e71 [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älkkib49638d2020-01-02 16:36:56 +020075static void host_outb(unsigned int base, u8 reg, u8 value)
76{
77 outb(value, base + reg);
78}
79
80static u8 host_inb(unsigned int base, u8 reg)
81{
82 return inb(base + reg);
83}
84
Kyösti Mälkki65f5de22020-01-02 16:36:56 +020085static void host_and_or(unsigned int base, u8 reg, u8 mask, u8 or)
86{
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älkki957511c2017-08-20 21:36:11 +0300105static int recover_master(int smbus_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älkki957511c2017-08-20 21:36:11 +0300126static int setup_command(unsigned int smbus_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älkkib49638d2020-01-02 16:36:56 +0200133 host_busy = host_inb(smbus_base, SMBHSTSTAT) & SMBHSTSTS_HOST_BUSY;
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300134 } while (--loops && host_busy);
135
136 if (loops == 0)
137 return recover_master(smbus_base,
138 SMBUS_WAIT_UNTIL_READY_TIMEOUT);
139
140 /* Clear any lingering errors, so the transaction will run. */
Kyösti Mälkki65f5de22020-01-02 16:36:56 +0200141 host_and_or(smbus_base, SMBHSTSTAT, 0xff, 0);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300142
143 /* Set up transaction */
144 /* Disable interrupts */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200145 host_outb(smbus_base, SMBHSTCTL, ctrl);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300146
147 /* Set the device I'm talking to. */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200148 host_outb(smbus_base, SMBXMITADD, xmitadd);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300149
150 return 0;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200151}
152
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300153static int execute_command(unsigned int smbus_base)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200154{
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300155 unsigned int loops = SMBUS_TIMEOUT;
156 u8 status;
157
158 /* Start the command. */
Kyösti Mälkki65f5de22020-01-02 16:36:56 +0200159 host_and_or(smbus_base, SMBHSTCTL, 0xff, SMBHSTCNT_START);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300160
161 /* Poll for it to start. */
Arthur Heymans16fe7902017-04-12 17:01:31 +0200162 do {
Arthur Heymans16fe7902017-04-12 17:01:31 +0200163 smbus_delay();
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300164
165 /* If we poll too slow, we could miss HOST_BUSY flag
166 * set and detect INTR or x_ERR flags instead here.
167 */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200168 status = host_inb(smbus_base, SMBHSTSTAT);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300169 status &= ~(SMBHSTSTS_SMBALERT_STS | SMBHSTSTS_INUSE_STS);
170 } while (--loops && status == 0);
171
172 if (loops == 0)
173 return recover_master(smbus_base,
174 SMBUS_WAIT_UNTIL_ACTIVE_TIMEOUT);
175
176 return 0;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200177}
178
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300179static int complete_command(unsigned int smbus_base)
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300180{
181 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300182 u8 status;
183
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300184 do {
185 smbus_delay();
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200186 status = host_inb(smbus_base, SMBHSTSTAT);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300187 } while (--loops && !host_completed(status));
188
189 if (loops == 0)
190 return recover_master(smbus_base,
191 SMBUS_WAIT_UNTIL_DONE_TIMEOUT);
192
193 return cb_err_from_stat(status);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300194}
195
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200196static int smbus_read_cmd(unsigned int smbus_base, u8 ctrl, u8 device,
Arthur Heymans16fe7902017-04-12 17:01:31 +0200197 unsigned int address)
198{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300199 int ret;
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200200 u16 word;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200201
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300202 /* Set up for a byte data read. */
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200203 ret = setup_command(smbus_base, ctrl, XMIT_READ(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300204 if (ret < 0)
205 return ret;
206
Arthur Heymans16fe7902017-04-12 17:01:31 +0200207 /* Set the command/address... */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200208 host_outb(smbus_base, SMBHSTCMD, address);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200209
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200210 /* Clear the data bytes... */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200211 host_outb(smbus_base, SMBHSTDAT0, 0);
212 host_outb(smbus_base, SMBHSTDAT1, 0);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200213
214 /* Start the command */
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300215 ret = execute_command(smbus_base);
216 if (ret < 0)
217 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200218
219 /* Poll for transaction completion */
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300220 ret = complete_command(smbus_base);
221 if (ret < 0)
222 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200223
224 /* Read results of transaction */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200225 word = host_inb(smbus_base, SMBHSTDAT0);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200226 if (ctrl == I801_WORD_DATA)
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200227 word |= host_inb(smbus_base, SMBHSTDAT1) << 8;
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200228
229 return word;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200230}
231
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200232static int smbus_write_cmd(unsigned int smbus_base, u8 ctrl, u8 device,
Arthur Heymans16fe7902017-04-12 17:01:31 +0200233 unsigned int address, unsigned int data)
234{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300235 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200236
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300237 /* Set up for a byte data write. */
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200238 ret = setup_command(smbus_base, ctrl, XMIT_WRITE(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300239 if (ret < 0)
240 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200241
Arthur Heymans16fe7902017-04-12 17:01:31 +0200242 /* Set the command/address... */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200243 host_outb(smbus_base, SMBHSTCMD, address);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200244
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200245 /* Set the data bytes... */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200246 host_outb(smbus_base, SMBHSTDAT0, data & 0xff);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200247 if (ctrl == I801_WORD_DATA)
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200248 host_outb(smbus_base, SMBHSTDAT1, data >> 8);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200249
250 /* Start the command */
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300251 ret = execute_command(smbus_base);
252 if (ret < 0)
253 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200254
255 /* Poll for transaction completion */
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300256 return complete_command(smbus_base);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200257}
258
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300259static int block_cmd_loop(unsigned int smbus_base,
260 u8 *buf, const unsigned int max_bytes, int flags)
261{
262 u8 status;
263 unsigned int loops = SMBUS_TIMEOUT;
264 int ret, bytes = 0;
265 int is_write_cmd = flags & BLOCK_WRITE;
266 int sw_drives_nak = flags & BLOCK_I2C;
267
268 /* Hardware limitations. */
269 if (flags == (BLOCK_WRITE | BLOCK_I2C))
270 return SMBUS_ERROR;
271
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300272 /* Set number of bytes to transfer. */
273 /* Reset number of bytes to transfer so we notice later it
274 * was really updated with the transaction. */
275 if (!sw_drives_nak) {
276 if (is_write_cmd)
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200277 host_outb(smbus_base, SMBHSTDAT0, max_bytes);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300278 else
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200279 host_outb(smbus_base, SMBHSTDAT0, 0);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300280 }
281
282 /* Send first byte from buffer, bytes_sent increments after
283 * hardware acknowledges it.
284 */
285 if (is_write_cmd)
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200286 host_outb(smbus_base, SMBBLKDAT, *buf++);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300287
288 /* Start the command */
289 ret = execute_command(smbus_base);
290 if (ret < 0)
291 return ret;
292
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300293 /* Poll for transaction completion */
294 do {
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200295 status = host_inb(smbus_base, SMBHSTSTAT);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300296
297 if (status & SMBHSTSTS_BYTE_DONE) { /* Byte done */
298
299 if (is_write_cmd) {
300 bytes++;
301 if (bytes < max_bytes)
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200302 host_outb(smbus_base, SMBBLKDAT, *buf++);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300303 } else {
304 if (bytes < max_bytes)
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200305 *buf++ = host_inb(smbus_base, SMBBLKDAT);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300306 bytes++;
307
308 /* Indicate that next byte is the last one. */
309 if (sw_drives_nak && (bytes + 1 >= max_bytes)) {
Kyösti Mälkki65f5de22020-01-02 16:36:56 +0200310 host_and_or(smbus_base, SMBHSTCTL,
311 0xff, SMBHSTCNT_LAST_BYTE);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300312 }
313
314 }
315
316 /* Engine internally completes the transaction
317 * and clears HOST_BUSY flag once the byte count
318 * has been reached or LAST_BYTE was set.
319 */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200320 host_outb(smbus_base, SMBHSTSTAT, SMBHSTSTS_BYTE_DONE);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300321 }
322
323 } while (--loops && !host_completed(status));
324
325 dprintk("%s: status = %02x, len = %d / %d, loops = %d\n",
326 __func__, status, bytes, max_bytes, SMBUS_TIMEOUT - loops);
327
328 if (loops == 0)
329 return recover_master(smbus_base,
330 SMBUS_WAIT_UNTIL_DONE_TIMEOUT);
331
332 ret = cb_err_from_stat(status);
333 if (ret < 0)
334 return ret;
335
336 return bytes;
337}
338
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200339int do_smbus_read_byte(unsigned int smbus_base, u8 device, unsigned int address)
340{
341 return smbus_read_cmd(smbus_base, I801_BYTE_DATA, device, address);
342}
343
344int do_smbus_read_word(unsigned int smbus_base, u8 device, unsigned int address)
345{
346 return smbus_read_cmd(smbus_base, I801_WORD_DATA, device, address);
347}
348
349int do_smbus_write_byte(unsigned int smbus_base, u8 device, unsigned int address,
350 unsigned int data)
351{
352 return smbus_write_cmd(smbus_base, I801_BYTE_DATA, device, address, data);
353}
354
355int do_smbus_write_word(unsigned int smbus_base, u8 device, unsigned int address,
356 unsigned int data)
357{
358 return smbus_write_cmd(smbus_base, I801_WORD_DATA, device, address, data);
359}
360
Arthur Heymans16fe7902017-04-12 17:01:31 +0200361int do_smbus_block_read(unsigned int smbus_base, u8 device, u8 cmd,
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200362 unsigned int max_bytes, u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200363{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300364 int ret, slave_bytes;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200365
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300366 max_bytes = MIN(SMBUS_BLOCK_MAXLEN, max_bytes);
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200367
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300368 /* Set up for a block data read. */
369 ret = setup_command(smbus_base, I801_BLOCK_DATA, XMIT_READ(device));
370 if (ret < 0)
371 return ret;
372
Arthur Heymans16fe7902017-04-12 17:01:31 +0200373 /* Set the command/address... */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200374 host_outb(smbus_base, SMBHSTCMD, cmd);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200375
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300376 /* Execute block transaction. */
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300377 ret = block_cmd_loop(smbus_base, buf, max_bytes, BLOCK_READ);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300378 if (ret < 0)
379 return ret;
380
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300381 /* Post-check we received complete message. */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200382 slave_bytes = host_inb(smbus_base, SMBHSTDAT0);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300383 if (ret < slave_bytes)
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200384 return SMBUS_ERROR;
385
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300386 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200387}
388
389int do_smbus_block_write(unsigned int smbus_base, u8 device, u8 cmd,
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200390 const unsigned int bytes, const u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200391{
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300392 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200393
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300394 if (bytes > SMBUS_BLOCK_MAXLEN)
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200395 return SMBUS_ERROR;
396
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300397 /* Set up for a block data write. */
398 ret = setup_command(smbus_base, I801_BLOCK_DATA, XMIT_WRITE(device));
399 if (ret < 0)
400 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200401
Arthur Heymans16fe7902017-04-12 17:01:31 +0200402 /* Set the command/address... */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200403 host_outb(smbus_base, SMBHSTCMD, cmd);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200404
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300405 /* Execute block transaction. */
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300406 ret = block_cmd_loop(smbus_base, (u8 *)buf, bytes, BLOCK_WRITE);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300407 if (ret < 0)
408 return ret;
409
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300410 if (ret < bytes)
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300411 return SMBUS_ERROR;
412
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300413 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200414}
415
416/* Only since ICH5 */
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200417static int has_i2c_read_command(void)
418{
Julius Wernercd49cce2019-03-05 16:53:33 -0800419 if (CONFIG(SOUTHBRIDGE_INTEL_I82371EB) ||
420 CONFIG(SOUTHBRIDGE_INTEL_I82801DX))
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200421 return 0;
422 return 1;
423}
424
425int do_i2c_eeprom_read(unsigned int smbus_base, u8 device,
Kyösti Mälkki1e392362017-08-20 21:36:03 +0300426 unsigned int offset, const unsigned int bytes, u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200427{
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300428 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200429
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200430 if (!has_i2c_read_command())
431 return SMBUS_ERROR;
432
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300433 /* Set up for a i2c block data read.
434 *
435 * FIXME: Address parameter changes to XMIT_READ(device) with
436 * some revision of PCH. Presumably hardware revisions that
437 * do not have i2c block write support internally set LSB.
438 */
439 ret = setup_command(smbus_base, I801_I2C_BLOCK_DATA,
440 XMIT_WRITE(device));
441 if (ret < 0)
442 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200443
444 /* device offset */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200445 host_outb(smbus_base, SMBHSTDAT1, offset);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200446
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300447 /* Execute block transaction. */
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300448 ret = block_cmd_loop(smbus_base, buf, bytes, BLOCK_READ | BLOCK_I2C);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300449 if (ret < 0)
450 return ret;
451
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300452 /* Post-check we received complete message. */
453 if (ret < bytes)
Kyösti Mälkki1e392362017-08-20 21:36:03 +0300454 return SMBUS_ERROR;
455
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300456 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200457}
Frans Hendrikse48be352019-06-19 11:01:27 +0200458
459/*
460 * The caller is responsible of settings HOSTC I2C_EN bit prior to making this
461 * call!
462 */
463int do_i2c_block_write(unsigned int smbus_base, u8 device,
464 unsigned int bytes, u8 *buf)
465{
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. */
476 ret = setup_command(smbus_base, I801_BLOCK_DATA, XMIT_WRITE(device));
477 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älkkib49638d2020-01-02 16:36:56 +0200488 host_outb(smbus_base, SMBHSTCMD, cmd);
489 host_outb(smbus_base, SMBHSTDAT1, cmd);
Frans Hendrikse48be352019-06-19 11:01:27 +0200490
491 /* Execute block transaction. */
492 ret = block_cmd_loop(smbus_base, buf, bytes, BLOCK_WRITE);
493 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}