blob: 00ff1dd4c2e66f0cb75a5a0a88fea3500e69027f [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älkkic38d5432017-08-20 21:36:18 +030085static int host_completed(u8 status)
86{
87 if (status & SMBHSTSTS_HOST_BUSY)
88 return 0;
Kyösti Mälkki44206e32019-02-26 17:17:24 +020089
90 /* These status bits do not imply completion of transaction. */
91 status &= ~(SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INUSE_STS |
92 SMBHSTSTS_SMBALERT_STS);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +030093 return status != 0;
94}
95
Kyösti Mälkki957511c2017-08-20 21:36:11 +030096static int recover_master(int smbus_base, int ret)
Arthur Heymans16fe7902017-04-12 17:01:31 +020097{
Kyösti Mälkki957511c2017-08-20 21:36:11 +030098 /* TODO: Depending of the failure, drive KILL transaction
99 * or force soft reset on SMBus master controller.
100 */
101 printk(BIOS_ERR, "SMBus: Fatal master timeout (%d)\n", ret);
102 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200103}
104
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300105static int cb_err_from_stat(u8 status)
106{
Kyösti Mälkki44206e32019-02-26 17:17:24 +0200107 /* These status bits do not imply errors. */
108 status &= ~(SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INUSE_STS |
109 SMBHSTSTS_SMBALERT_STS);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300110
111 if (status == SMBHSTSTS_INTR)
112 return 0;
113
114 return SMBUS_ERROR;
115}
116
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300117static int setup_command(unsigned int smbus_base, u8 ctrl, u8 xmitadd)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200118{
119 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300120 u8 host_busy;
121
Arthur Heymans16fe7902017-04-12 17:01:31 +0200122 do {
123 smbus_delay();
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200124 host_busy = host_inb(smbus_base, SMBHSTSTAT) & SMBHSTSTS_HOST_BUSY;
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300125 } while (--loops && host_busy);
126
127 if (loops == 0)
128 return recover_master(smbus_base,
129 SMBUS_WAIT_UNTIL_READY_TIMEOUT);
130
131 /* Clear any lingering errors, so the transaction will run. */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200132 host_outb(smbus_base, SMBHSTSTAT, host_inb(smbus_base, SMBHSTSTAT));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300133
134 /* Set up transaction */
135 /* Disable interrupts */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200136 host_outb(smbus_base, SMBHSTCTL, ctrl);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300137
138 /* Set the device I'm talking to. */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200139 host_outb(smbus_base, SMBXMITADD, xmitadd);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300140
141 return 0;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200142}
143
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300144static int execute_command(unsigned int smbus_base)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200145{
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300146 unsigned int loops = SMBUS_TIMEOUT;
147 u8 status;
148
149 /* Start the command. */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200150 host_outb(smbus_base, SMBHSTCTL, host_inb(smbus_base, SMBHSTCTL) | SMBHSTCNT_START);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300151
152 /* Poll for it to start. */
Arthur Heymans16fe7902017-04-12 17:01:31 +0200153 do {
Arthur Heymans16fe7902017-04-12 17:01:31 +0200154 smbus_delay();
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300155
156 /* If we poll too slow, we could miss HOST_BUSY flag
157 * set and detect INTR or x_ERR flags instead here.
158 */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200159 status = host_inb(smbus_base, SMBHSTSTAT);
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300160 status &= ~(SMBHSTSTS_SMBALERT_STS | SMBHSTSTS_INUSE_STS);
161 } while (--loops && status == 0);
162
163 if (loops == 0)
164 return recover_master(smbus_base,
165 SMBUS_WAIT_UNTIL_ACTIVE_TIMEOUT);
166
167 return 0;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200168}
169
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300170static int complete_command(unsigned int smbus_base)
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300171{
172 unsigned int loops = SMBUS_TIMEOUT;
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300173 u8 status;
174
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300175 do {
176 smbus_delay();
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200177 status = host_inb(smbus_base, SMBHSTSTAT);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300178 } while (--loops && !host_completed(status));
179
180 if (loops == 0)
181 return recover_master(smbus_base,
182 SMBUS_WAIT_UNTIL_DONE_TIMEOUT);
183
184 return cb_err_from_stat(status);
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300185}
186
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200187static int smbus_read_cmd(unsigned int smbus_base, u8 ctrl, u8 device,
Arthur Heymans16fe7902017-04-12 17:01:31 +0200188 unsigned int address)
189{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300190 int ret;
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200191 u16 word;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200192
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300193 /* Set up for a byte data read. */
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200194 ret = setup_command(smbus_base, ctrl, XMIT_READ(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300195 if (ret < 0)
196 return ret;
197
Arthur Heymans16fe7902017-04-12 17:01:31 +0200198 /* Set the command/address... */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200199 host_outb(smbus_base, SMBHSTCMD, address);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200200
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200201 /* Clear the data bytes... */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200202 host_outb(smbus_base, SMBHSTDAT0, 0);
203 host_outb(smbus_base, SMBHSTDAT1, 0);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200204
205 /* Start the command */
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300206 ret = execute_command(smbus_base);
207 if (ret < 0)
208 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200209
210 /* Poll for transaction completion */
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300211 ret = complete_command(smbus_base);
212 if (ret < 0)
213 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200214
215 /* Read results of transaction */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200216 word = host_inb(smbus_base, SMBHSTDAT0);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200217 if (ctrl == I801_WORD_DATA)
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200218 word |= host_inb(smbus_base, SMBHSTDAT1) << 8;
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200219
220 return word;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200221}
222
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200223static int smbus_write_cmd(unsigned int smbus_base, u8 ctrl, u8 device,
Arthur Heymans16fe7902017-04-12 17:01:31 +0200224 unsigned int address, unsigned int data)
225{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300226 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200227
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300228 /* Set up for a byte data write. */
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200229 ret = setup_command(smbus_base, ctrl, XMIT_WRITE(device));
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300230 if (ret < 0)
231 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200232
Arthur Heymans16fe7902017-04-12 17:01:31 +0200233 /* Set the command/address... */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200234 host_outb(smbus_base, SMBHSTCMD, address);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200235
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200236 /* Set the data bytes... */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200237 host_outb(smbus_base, SMBHSTDAT0, data & 0xff);
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200238 if (ctrl == I801_WORD_DATA)
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200239 host_outb(smbus_base, SMBHSTDAT1, data >> 8);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200240
241 /* Start the command */
Kyösti Mälkkia2dcf732017-08-20 21:36:15 +0300242 ret = execute_command(smbus_base);
243 if (ret < 0)
244 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200245
246 /* Poll for transaction completion */
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300247 return complete_command(smbus_base);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200248}
249
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300250static int block_cmd_loop(unsigned int smbus_base,
251 u8 *buf, const unsigned int max_bytes, int flags)
252{
253 u8 status;
254 unsigned int loops = SMBUS_TIMEOUT;
255 int ret, bytes = 0;
256 int is_write_cmd = flags & BLOCK_WRITE;
257 int sw_drives_nak = flags & BLOCK_I2C;
258
259 /* Hardware limitations. */
260 if (flags == (BLOCK_WRITE | BLOCK_I2C))
261 return SMBUS_ERROR;
262
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300263 /* Set number of bytes to transfer. */
264 /* Reset number of bytes to transfer so we notice later it
265 * was really updated with the transaction. */
266 if (!sw_drives_nak) {
267 if (is_write_cmd)
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200268 host_outb(smbus_base, SMBHSTDAT0, max_bytes);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300269 else
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200270 host_outb(smbus_base, SMBHSTDAT0, 0);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300271 }
272
273 /* Send first byte from buffer, bytes_sent increments after
274 * hardware acknowledges it.
275 */
276 if (is_write_cmd)
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200277 host_outb(smbus_base, SMBBLKDAT, *buf++);
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300278
279 /* Start the command */
280 ret = execute_command(smbus_base);
281 if (ret < 0)
282 return ret;
283
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300284 /* Poll for transaction completion */
285 do {
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200286 status = host_inb(smbus_base, SMBHSTSTAT);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300287
288 if (status & SMBHSTSTS_BYTE_DONE) { /* Byte done */
289
290 if (is_write_cmd) {
291 bytes++;
292 if (bytes < max_bytes)
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200293 host_outb(smbus_base, SMBBLKDAT, *buf++);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300294 } else {
295 if (bytes < max_bytes)
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200296 *buf++ = host_inb(smbus_base, SMBBLKDAT);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300297 bytes++;
298
299 /* Indicate that next byte is the last one. */
300 if (sw_drives_nak && (bytes + 1 >= max_bytes)) {
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200301 host_outb(smbus_base, SMBHSTCTL,
302 host_inb(smbus_base, SMBHSTCTL) |
303 SMBHSTCNT_LAST_BYTE);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300304 }
305
306 }
307
308 /* Engine internally completes the transaction
309 * and clears HOST_BUSY flag once the byte count
310 * has been reached or LAST_BYTE was set.
311 */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200312 host_outb(smbus_base, SMBHSTSTAT, SMBHSTSTS_BYTE_DONE);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300313 }
314
315 } while (--loops && !host_completed(status));
316
317 dprintk("%s: status = %02x, len = %d / %d, loops = %d\n",
318 __func__, status, bytes, max_bytes, SMBUS_TIMEOUT - loops);
319
320 if (loops == 0)
321 return recover_master(smbus_base,
322 SMBUS_WAIT_UNTIL_DONE_TIMEOUT);
323
324 ret = cb_err_from_stat(status);
325 if (ret < 0)
326 return ret;
327
328 return bytes;
329}
330
Kyösti Mälkki7ca19b22020-01-02 17:02:54 +0200331int do_smbus_read_byte(unsigned int smbus_base, u8 device, unsigned int address)
332{
333 return smbus_read_cmd(smbus_base, I801_BYTE_DATA, device, address);
334}
335
336int do_smbus_read_word(unsigned int smbus_base, u8 device, unsigned int address)
337{
338 return smbus_read_cmd(smbus_base, I801_WORD_DATA, device, address);
339}
340
341int do_smbus_write_byte(unsigned int smbus_base, u8 device, unsigned int address,
342 unsigned int data)
343{
344 return smbus_write_cmd(smbus_base, I801_BYTE_DATA, device, address, data);
345}
346
347int do_smbus_write_word(unsigned int smbus_base, u8 device, unsigned int address,
348 unsigned int data)
349{
350 return smbus_write_cmd(smbus_base, I801_WORD_DATA, device, address, data);
351}
352
Arthur Heymans16fe7902017-04-12 17:01:31 +0200353int do_smbus_block_read(unsigned int smbus_base, u8 device, u8 cmd,
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200354 unsigned int max_bytes, u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200355{
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300356 int ret, slave_bytes;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200357
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300358 max_bytes = MIN(SMBUS_BLOCK_MAXLEN, max_bytes);
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200359
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300360 /* Set up for a block data read. */
361 ret = setup_command(smbus_base, I801_BLOCK_DATA, XMIT_READ(device));
362 if (ret < 0)
363 return ret;
364
Arthur Heymans16fe7902017-04-12 17:01:31 +0200365 /* Set the command/address... */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200366 host_outb(smbus_base, SMBHSTCMD, cmd);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200367
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300368 /* Execute block transaction. */
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300369 ret = block_cmd_loop(smbus_base, buf, max_bytes, BLOCK_READ);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300370 if (ret < 0)
371 return ret;
372
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300373 /* Post-check we received complete message. */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200374 slave_bytes = host_inb(smbus_base, SMBHSTDAT0);
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300375 if (ret < slave_bytes)
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200376 return SMBUS_ERROR;
377
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300378 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200379}
380
381int do_smbus_block_write(unsigned int smbus_base, u8 device, u8 cmd,
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200382 const unsigned int bytes, const u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200383{
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300384 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200385
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300386 if (bytes > SMBUS_BLOCK_MAXLEN)
Arthur Heymans1b04aa22017-08-04 14:28:50 +0200387 return SMBUS_ERROR;
388
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300389 /* Set up for a block data write. */
390 ret = setup_command(smbus_base, I801_BLOCK_DATA, XMIT_WRITE(device));
391 if (ret < 0)
392 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200393
Arthur Heymans16fe7902017-04-12 17:01:31 +0200394 /* Set the command/address... */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200395 host_outb(smbus_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älkki893edee2017-08-20 21:36:24 +0300398 ret = block_cmd_loop(smbus_base, (u8 *)buf, bytes, BLOCK_WRITE);
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 if (ret < bytes)
Kyösti Mälkkic17e8552017-08-20 23:48:23 +0300403 return SMBUS_ERROR;
404
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300405 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200406}
407
408/* Only since ICH5 */
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200409static int has_i2c_read_command(void)
410{
Julius Wernercd49cce2019-03-05 16:53:33 -0800411 if (CONFIG(SOUTHBRIDGE_INTEL_I82371EB) ||
412 CONFIG(SOUTHBRIDGE_INTEL_I82801DX))
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200413 return 0;
414 return 1;
415}
416
417int do_i2c_eeprom_read(unsigned int smbus_base, u8 device,
Kyösti Mälkki1e392362017-08-20 21:36:03 +0300418 unsigned int offset, const unsigned int bytes, u8 *buf)
Arthur Heymans16fe7902017-04-12 17:01:31 +0200419{
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300420 int ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200421
Kyösti Mälkkic01a5052019-01-30 09:39:23 +0200422 if (!has_i2c_read_command())
423 return SMBUS_ERROR;
424
Kyösti Mälkki957511c2017-08-20 21:36:11 +0300425 /* Set up for a i2c block data read.
426 *
427 * FIXME: Address parameter changes to XMIT_READ(device) with
428 * some revision of PCH. Presumably hardware revisions that
429 * do not have i2c block write support internally set LSB.
430 */
431 ret = setup_command(smbus_base, I801_I2C_BLOCK_DATA,
432 XMIT_WRITE(device));
433 if (ret < 0)
434 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200435
436 /* device offset */
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200437 host_outb(smbus_base, SMBHSTDAT1, offset);
Arthur Heymans16fe7902017-04-12 17:01:31 +0200438
Kyösti Mälkkid6c15d02017-08-20 21:36:24 +0300439 /* Execute block transaction. */
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300440 ret = block_cmd_loop(smbus_base, buf, bytes, BLOCK_READ | BLOCK_I2C);
Kyösti Mälkkic38d5432017-08-20 21:36:18 +0300441 if (ret < 0)
442 return ret;
443
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300444 /* Post-check we received complete message. */
445 if (ret < bytes)
Kyösti Mälkki1e392362017-08-20 21:36:03 +0300446 return SMBUS_ERROR;
447
Kyösti Mälkki893edee2017-08-20 21:36:24 +0300448 return ret;
Arthur Heymans16fe7902017-04-12 17:01:31 +0200449}
Frans Hendrikse48be352019-06-19 11:01:27 +0200450
451/*
452 * The caller is responsible of settings HOSTC I2C_EN bit prior to making this
453 * call!
454 */
455int do_i2c_block_write(unsigned int smbus_base, u8 device,
456 unsigned int bytes, u8 *buf)
457{
458 u8 cmd;
459 int ret;
460
461 if (!CONFIG(SOC_INTEL_BRASWELL))
462 return SMBUS_ERROR;
463
464 if (!bytes || (bytes > SMBUS_BLOCK_MAXLEN))
465 return SMBUS_ERROR;
466
467 /* Set up for a block data write. */
468 ret = setup_command(smbus_base, I801_BLOCK_DATA, XMIT_WRITE(device));
469 if (ret < 0)
470 return ret;
471
472 /*
473 * In i2c mode SMBus controller sequence on bus will be:
474 * <SMBXINTADD> <SMBHSTDAT1> <SMBBLKDAT> .. <SMBBLKDAT>
475 * The SMBHSTCMD must be written also to ensure the SMBUs controller
476 * will generate the i2c sequence.
477 */
478 cmd = *buf++;
479 bytes--;
Kyösti Mälkkib49638d2020-01-02 16:36:56 +0200480 host_outb(smbus_base, SMBHSTCMD, cmd);
481 host_outb(smbus_base, SMBHSTDAT1, cmd);
Frans Hendrikse48be352019-06-19 11:01:27 +0200482
483 /* Execute block transaction. */
484 ret = block_cmd_loop(smbus_base, buf, bytes, BLOCK_WRITE);
485 if (ret < 0)
486 return ret;
487
488 if (ret < bytes)
489 return SMBUS_ERROR;
490
491 ret++; /* 1st byte has been written using SMBHSTDAT1 */
492 return ret;
493}