Debian Codegraph @

src/southbridge/intel/common/smbus.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <arch/io.h>
4 #include <console/console.h>
5 #include <delay.h>
6 #include <device/smbus_def.h>
7 #include <device/smbus_host.h>
8 #include <types.h>
9
10 #if CONFIG(DEBUG_SMBUS)
11 #define dprintk(args...) printk(BIOS_DEBUG, ##args)
12 #else
13 #define dprintk(args...) do {} while (0)
14 #endif
15
16 /* SMBus register offsets. */
17 #define SMBHSTSTAT 0x0
18 #define SMBHSTCTL 0x2
19 #define SMBHSTCMD 0x3
20 #define SMBXMITADD 0x4
21 #define SMBHSTDAT0 0x5
22 #define SMBHSTDAT1 0x6
23 #define SMBBLKDAT 0x7
24 #define SMBTRNSADD 0x9
25 #define SMBSLVDATA 0xa
26 #define SMLINK_PIN_CTL 0xe
27 #define SMBUS_PIN_CTL 0xf
28 #define SMBSLVCMD 0x11
29
30 #define SMB_RCV_SLVA SMBTRNSADD
31
32 /* I801 command constants */
33 #define I801_QUICK (0 << 2)
34 #define I801_BYTE (1 << 2)
35 #define I801_BYTE_DATA (2 << 2)
36 #define I801_WORD_DATA (3 << 2)
37 #define I801_PROCESS_CALL (4 << 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
58 /* For SMBXMITADD register. */
59 #define XMIT_WRITE(dev) (((dev) << 1) | 0)
60 #define XMIT_READ(dev) (((dev) << 1) | 1)
61
62 #define SMBUS_TIMEOUT (10 * 1000 * 100)
63 #define SMBUS_BLOCK_MAXLEN 32
64
65 /* block_cmd_loop flags */
66 #define BLOCK_READ 0
67 #define BLOCK_WRITE (1 << 0)
68 #define BLOCK_I2C (1 << 1)
69
70 static void host_outb(uintptr_t base, u8 reg, u8 value)
71 {
72 outb(value, base + reg);
73 }
74
75 static u8 host_inb(uintptr_t base, u8 reg)
76 {
77 return inb(base + reg);
78 }
79
80 static void host_and_or(uintptr_t base, u8 reg, u8 mask, u8 or)
81 {
82 u8 value;
83 value = host_inb(base, reg);
84 value &= mask;
85 value |= or;
86 host_outb(base, reg, value);
87 }
88
89 void smbus_host_reset(uintptr_t base)
90 {
91 /* Disable interrupt generation. */
92 host_outb(base, SMBHSTCTL, 0);
93
94 /* Clear any lingering errors, so transactions can run. */
95 host_and_or(base, SMBHSTSTAT, 0xff, 0);
96 }
97
98 void smbus_set_slave_addr(uintptr_t base, u8 slave_address)
99 {
100 host_outb(base, SMB_RCV_SLVA, slave_address);
101 }
102
103 static int host_completed(u8 status)
104 {
105 if (status & SMBHSTSTS_HOST_BUSY)
106 return 0;
107
108 /* These status bits do not imply completion of transaction. */
109 status &= ~(SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INUSE_STS |
110 SMBHSTSTS_SMBALERT_STS);
111 return status != 0;
112 }
113
114 static int recover_master(uintptr_t base, int ret)
115 {
116 /* TODO: Depending of the failure, drive KILL transaction
117 * or force soft reset on SMBus master controller.
118 */
119 printk(BIOS_ERR, "SMBus: Fatal master timeout (%d)\n", ret);
120 return ret;
121 }
122
123 static int cb_err_from_stat(u8 status)
124 {
125 /* These status bits do not imply errors. */
126 status &= ~(SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INUSE_STS |
127 SMBHSTSTS_SMBALERT_STS);
128
129 if (status == SMBHSTSTS_INTR)
130 return 0;
131
132 return SMBUS_ERROR;
133 }
134
135 static int setup_command(uintptr_t base, u8 ctrl, u8 xmitadd)
136 {
137 unsigned int loops = SMBUS_TIMEOUT;
138 u8 host_busy;
139
140 do {
141 udelay(1);
142 host_busy = host_inb(base, SMBHSTSTAT) & SMBHSTSTS_HOST_BUSY;
143 } while (--loops && host_busy);
144
145 if (loops == 0)
146 return recover_master(base, SMBUS_WAIT_UNTIL_READY_TIMEOUT);
147
148 /* Clear any lingering errors, so the transaction will run. */
149 host_and_or(base, SMBHSTSTAT, 0xff, 0);
150
151 /* Set up transaction */
152 /* Disable interrupts */
153 host_outb(base, SMBHSTCTL, ctrl);
154
155 /* Set the device I'm talking to. */
156 host_outb(base, SMBXMITADD, xmitadd);
157
158 return 0;
159 }
160
161 static int execute_command(uintptr_t base)
162 {
163 unsigned int loops = SMBUS_TIMEOUT;
164 u8 status;
165
166 /* Start the command. */
167 host_and_or(base, SMBHSTCTL, 0xff, SMBHSTCNT_START);
168
169 /* Poll for it to start. */
170 do {
171 udelay(1);
172
173 /* If we poll too slow, we could miss HOST_BUSY flag
174 * set and detect INTR or x_ERR flags instead here.
175 */
176 status = host_inb(base, SMBHSTSTAT);
177 status &= ~(SMBHSTSTS_SMBALERT_STS | SMBHSTSTS_INUSE_STS);
178 } while (--loops && status == 0);
179
180 if (loops == 0)
181 return recover_master(base,
182 SMBUS_WAIT_UNTIL_ACTIVE_TIMEOUT);
183
184 return 0;
185 }
186
187 static int complete_command(uintptr_t base)
188 {
189 unsigned int loops = SMBUS_TIMEOUT;
190 u8 status;
191
192 do {
193 udelay(1);
194 status = host_inb(base, SMBHSTSTAT);
195 } while (--loops && !host_completed(status));
196
197 if (loops == 0)
198 return recover_master(base,
199 SMBUS_WAIT_UNTIL_DONE_TIMEOUT);
200
201 return cb_err_from_stat(status);
202 }
203
204 static int smbus_read_cmd(uintptr_t base, u8 ctrl, u8 device, u8 address)
205 {
206 int ret;
207 u16 word;
208
209 /* Set up for a byte data read. */
210 ret = setup_command(base, ctrl, XMIT_READ(device));
211 if (ret < 0)
212 return ret;
213
214 /* Set the command/address... */
215 host_outb(base, SMBHSTCMD, address);
216
217 /* Clear the data bytes... */
218 host_outb(base, SMBHSTDAT0, 0);
219 host_outb(base, SMBHSTDAT1, 0);
220
221 /* Start the command */
222 ret = execute_command(base);
223 if (ret < 0)
224 return ret;
225
226 /* Poll for transaction completion */
227 ret = complete_command(base);
228 if (ret < 0)
229 return ret;
230
231 /* Read results of transaction */
232 word = host_inb(base, SMBHSTDAT0);
233 if (ctrl == I801_WORD_DATA)
234 word |= host_inb(base, SMBHSTDAT1) << 8;
235
236 return word;
237 }
238
239 static int smbus_write_cmd(uintptr_t base, u8 ctrl, u8 device, u8 address, u16 data)
240 {
241 int ret;
242
243 /* Set up for a byte data write. */
244 ret = setup_command(base, ctrl, XMIT_WRITE(device));
245 if (ret < 0)
246 return ret;
247
248 /* Set the command/address... */
249 host_outb(base, SMBHSTCMD, address);
250
251 /* Set the data bytes... */
252 if (ctrl >= I801_BYTE_DATA)
253 host_outb(base, SMBHSTDAT0, data & 0xff);
254 if (ctrl == I801_WORD_DATA)
255 host_outb(base, SMBHSTDAT1, data >> 8);
256
257 /* Start the command */
258 ret = execute_command(base);
259 if (ret < 0)
260 return ret;
261
262 /* Poll for transaction completion */
263 return complete_command(base);
264 }
265
266 static int block_cmd_loop(uintptr_t base, u8 *buf, size_t max_bytes, int flags)
267 {
268 u8 status;
269 unsigned int loops = SMBUS_TIMEOUT;
270 int ret;
271 size_t bytes = 0;
272 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
279 /* 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)
284 host_outb(base, SMBHSTDAT0, max_bytes);
285 else
286 host_outb(base, SMBHSTDAT0, 0);
287 }
288
289 /* Send first byte from buffer, bytes_sent increments after
290 * hardware acknowledges it.
291 */
292 if (is_write_cmd)
293 host_outb(base, SMBBLKDAT, *buf++);
294
295 /* Start the command */
296 ret = execute_command(base);
297 if (ret < 0)
298 return ret;
299
300 /* Poll for transaction completion */
301 do {
302 status = host_inb(base, SMBHSTSTAT);
303
304 if (status & SMBHSTSTS_BYTE_DONE) { /* Byte done */
305
306 if (is_write_cmd) {
307 bytes++;
308 if (bytes < max_bytes)
309 host_outb(base, SMBBLKDAT, *buf++);
310 } else {
311 if (bytes < max_bytes)
312 *buf++ = host_inb(base, SMBBLKDAT);
313 bytes++;
314
315 /* Indicate that next byte is the last one. */
316 if (sw_drives_nak && (bytes + 1 >= max_bytes)) {
317 host_and_or(base, SMBHSTCTL, 0xff,
318 SMBHSTCNT_LAST_BYTE);
319 }
320 }
321
322 /* Engine internally completes the transaction
323 * and clears HOST_BUSY flag once the byte count
324 * has been reached or LAST_BYTE was set.
325 */
326 host_outb(base, SMBHSTSTAT, SMBHSTSTS_BYTE_DONE);
327 }
328
329 } while (--loops && !host_completed(status));
330
331 dprintk("%s: status = %02x, len = %zd / %zd, loops = %d\n",
332 __func__, status, bytes, max_bytes, SMBUS_TIMEOUT - loops);
333
334 if (loops == 0)
335 return recover_master(base, SMBUS_WAIT_UNTIL_DONE_TIMEOUT);
336
337 ret = cb_err_from_stat(status);
338 if (ret < 0)
339 return ret;
340
341 return bytes;
342 }
343
344 int do_smbus_read_byte(uintptr_t base, u8 device, u8 address)
345 {
346 return smbus_read_cmd(base, I801_BYTE_DATA, device, address);
347 }
348
349 int do_smbus_read_word(uintptr_t base, u8 device, u8 address)
350 {
351 return smbus_read_cmd(base, I801_WORD_DATA, device, address);
352 }
353
354 int do_smbus_send_byte(uintptr_t base, u8 device, u8 data)
355 {
356 return smbus_write_cmd(base, I801_BYTE, device, data, 0x00);
357 }
358
359 int do_smbus_write_byte(uintptr_t base, u8 device, u8 address, u8 data)
360 {
361 return smbus_write_cmd(base, I801_BYTE_DATA, device, address, data);
362 }
363
364 int do_smbus_write_word(uintptr_t base, u8 device, u8 address, u16 data)
365 {
366 return smbus_write_cmd(base, I801_WORD_DATA, device, address, data);
367 }
368
369 int do_smbus_block_read(uintptr_t base, u8 device, u8 cmd, size_t max_bytes, u8 *buf)
370 {
371 int ret, slave_bytes;
372
373 max_bytes = MIN(SMBUS_BLOCK_MAXLEN, max_bytes);
374
375 /* Set up for a block data read. */
376 ret = setup_command(base, I801_BLOCK_DATA, XMIT_READ(device));
377 if (ret < 0)
378 return ret;
379
380 /* Set the command/address... */
381 host_outb(base, SMBHSTCMD, cmd);
382
383 /* Execute block transaction. */
384 ret = block_cmd_loop(base, buf, max_bytes, BLOCK_READ);
385 if (ret < 0)
386 return ret;
387
388 /* Post-check we received complete message. */
389 slave_bytes = host_inb(base, SMBHSTDAT0);
390 if (ret < slave_bytes)
391 return SMBUS_ERROR;
392
393 return ret;
394 }
395
396 /*
397 * The caller is responsible of settings HOSTC I2C_EN bit prior to making this
398 * call!
399 */
400 int do_smbus_process_call(uintptr_t base, u8 device, u8 cmd, u16 data, u16 *buf)
401 {
402 int ret;
403
404 /* Set up for process call */
405 ret = setup_command(base, I801_PROCESS_CALL, XMIT_WRITE(device));
406 if (ret < 0)
407 return ret;
408
409 /* cmd will only be send if I2C_EN is zero */
410 host_outb(base, SMBHSTCMD, cmd);
411
412 host_outb(base, SMBHSTDAT0, data & 0x00ff);
413 host_outb(base, SMBHSTDAT1, (data & 0xff00) >> 8);
414
415 /* Start the command */
416 ret = execute_command(base);
417 if (ret < 0)
418 return ret;
419
420 /* Poll for transaction completion */
421 ret = complete_command(base);
422 if (ret < 0)
423 return ret;
424
425 /* Read results of transaction */
426 *buf = host_inb(base, SMBHSTDAT0);
427 *buf |= (host_inb(base, SMBHSTDAT1) << 8);
428
429 return ret;
430 }
431
432 int do_smbus_block_write(uintptr_t base, u8 device, u8 cmd, const size_t bytes, const u8 *buf)
433 {
434 int ret;
435
436 if (bytes > SMBUS_BLOCK_MAXLEN)
437 return SMBUS_ERROR;
438
439 /* Set up for a block data write. */
440 ret = setup_command(base, I801_BLOCK_DATA, XMIT_WRITE(device));
441 if (ret < 0)
442 return ret;
443
444 /* Set the command/address... */
445 host_outb(base, SMBHSTCMD, cmd);
446
447 /* Execute block transaction. */
448 ret = block_cmd_loop(base, (u8 *)buf, bytes, BLOCK_WRITE);
449 if (ret < 0)
450 return ret;
451
452 if (ret < bytes)
453 return SMBUS_ERROR;
454
455 return ret;
456 }
457
458 /* Only since ICH5 */
459 static int has_i2c_read_command(void)
460 {
461 if (CONFIG(SOUTHBRIDGE_INTEL_I82371EB) ||
462 CONFIG(SOUTHBRIDGE_INTEL_I82801DX))
463 return 0;
464 return 1;
465 }
466
467 int do_i2c_eeprom_read(uintptr_t base, u8 device, u8 offset, const size_t bytes, u8 *buf)
468 {
469 int ret;
470
471 if (!has_i2c_read_command())
472 return SMBUS_ERROR;
473
474 /* Set up for a i2c block data read.
475 *
476 * FIXME: Address parameter changes to XMIT_READ(device) with
477 * some revision of PCH. Presumably hardware revisions that
478 * do not have i2c block write support internally set LSB.
479 */
480 ret = setup_command(base, I801_I2C_BLOCK_DATA,
481 XMIT_WRITE(device));
482 if (ret < 0)
483 return ret;
484
485 /* device offset */
486 host_outb(base, SMBHSTDAT1, offset);
487
488 /* Execute block transaction. */
489 ret = block_cmd_loop(base, buf, bytes, BLOCK_READ | BLOCK_I2C);
490 if (ret < 0)
491 return ret;
492
493 /* Post-check we received complete message. */
494 if (ret < bytes)
495 return SMBUS_ERROR;
496
497 return ret;
498 }
499
500 /*
501 * The caller is responsible of settings HOSTC I2C_EN bit prior to making this
502 * call!
503 */
504 int do_i2c_block_write(uintptr_t base, u8 device, size_t bytes, u8 *buf)
505 {
506 u8 cmd;
507 int ret;
508
509 if (!CONFIG(SOC_INTEL_BRASWELL))
510 return SMBUS_ERROR;
511
512 if (!bytes || (bytes > SMBUS_BLOCK_MAXLEN))
513 return SMBUS_ERROR;
514
515 /* Set up for a block data write. */
516 ret = setup_command(base, I801_BLOCK_DATA, XMIT_WRITE(device));
517 if (ret < 0)
518 return ret;
519
520 /*
521 * In i2c mode SMBus controller sequence on bus will be:
522 * <SMBXINTADD> <SMBHSTDAT1> <SMBBLKDAT> .. <SMBBLKDAT>
523 * The SMBHSTCMD must be written also to ensure the SMBUs controller
524 * will generate the i2c sequence.
525 */
526 cmd = *buf++;
527 bytes--;
528 host_outb(base, SMBHSTCMD, cmd);
529 host_outb(base, SMBHSTDAT1, cmd);
530
531 /* Execute block transaction. */
532 ret = block_cmd_loop(base, buf, bytes, BLOCK_WRITE);
533 if (ret < 0)
534 return ret;
535
536 if (ret < bytes)
537 return SMBUS_ERROR;
538
539 ret++; /* 1st byte has been written using SMBHSTDAT1 */
540 return ret;
541 }