blob: 557d6329a4f0e815c65b06f9bf1ac4ead8d69f4c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Wolfgang Denkb38e0df2007-03-06 18:08:43 +01002/*
3 * (C) Copyright 2002
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Wolfgang Denkb38e0df2007-03-06 18:08:43 +01005 */
6
Wolfgang Denkb38e0df2007-03-06 18:08:43 +01007/*
8 * I2C test
9 *
10 * For verifying the I2C bus, a full I2C bus scanning is performed.
11 *
Tom Rini3dd5d4a2022-12-04 10:14:17 -050012 * #ifdef CFG_SYS_POST_I2C_ADDRS
Peter Tyserd482dd32010-10-22 00:20:31 -050013 * The test is considered as passed if all the devices and only the devices
14 * in the list are found.
Tom Rini3dd5d4a2022-12-04 10:14:17 -050015 * #ifdef CFG_SYS_POST_I2C_IGNORES
16 * Ignore devices listed in CFG_SYS_POST_I2C_IGNORES. These devices
Peter Tyserd482dd32010-10-22 00:20:31 -050017 * are optional or not vital to board functionality.
18 * #endif
Tom Rini3dd5d4a2022-12-04 10:14:17 -050019 * #else [ ! CFG_SYS_POST_I2C_ADDRS ]
Wolfgang Denkb38e0df2007-03-06 18:08:43 +010020 * The test is considered as passed if any I2C device is found.
21 * #endif
22 */
23
Tom Riniabb9a042024-05-18 20:20:43 -060024#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060025#include <log.h>
Wolfgang Denkb38e0df2007-03-06 18:08:43 +010026#include <post.h>
27#include <i2c.h>
28
Tom Rini3dd5d4a2022-12-04 10:14:17 -050029#if CFG_POST & CFG_SYS_POST_I2C
Wolfgang Denkb38e0df2007-03-06 18:08:43 +010030
Peter Tyserd482dd32010-10-22 00:20:31 -050031static int i2c_ignore_device(unsigned int chip)
32{
Tom Rini3dd5d4a2022-12-04 10:14:17 -050033#ifdef CFG_SYS_POST_I2C_IGNORES
34 const unsigned char i2c_ignore_list[] = CFG_SYS_POST_I2C_IGNORES;
Peter Tyserd482dd32010-10-22 00:20:31 -050035 int i;
36
37 for (i = 0; i < sizeof(i2c_ignore_list); i++)
38 if (i2c_ignore_list[i] == chip)
39 return 1;
40#endif
41
42 return 0;
43}
44
Wolfgang Denkb38e0df2007-03-06 18:08:43 +010045int i2c_post_test (int flags)
46{
47 unsigned int i;
Tom Rini3dd5d4a2022-12-04 10:14:17 -050048#ifndef CFG_SYS_POST_I2C_ADDRS
Peter Tyser0ce01892010-10-22 00:20:29 -050049 /* Start at address 1, address 0 is the general call address */
Heiko Schocher00224752010-11-15 08:20:39 +010050 for (i = 1; i < 128; i++) {
Peter Tyserd482dd32010-10-22 00:20:31 -050051 if (i2c_ignore_device(i))
52 continue;
Peter Tyserde2ef6b2010-10-22 00:20:27 -050053 if (i2c_probe (i) == 0)
54 return 0;
Heiko Schocher00224752010-11-15 08:20:39 +010055 }
Peter Tyserde2ef6b2010-10-22 00:20:27 -050056
57 /* No devices found */
58 return -1;
59#else
Peter Tyser5d933252010-10-22 00:20:28 -050060 unsigned int ret = 0;
Wolfgang Denkb38e0df2007-03-06 18:08:43 +010061 int j;
Tom Rini3dd5d4a2022-12-04 10:14:17 -050062 unsigned char i2c_addr_list[] = CFG_SYS_POST_I2C_ADDRS;
Wolfgang Denkb38e0df2007-03-06 18:08:43 +010063
Peter Tyser0ce01892010-10-22 00:20:29 -050064 /* Start at address 1, address 0 is the general call address */
65 for (i = 1; i < 128; i++) {
Peter Tyserd482dd32010-10-22 00:20:31 -050066 if (i2c_ignore_device(i))
67 continue;
Peter Tyserde2ef6b2010-10-22 00:20:27 -050068 if (i2c_probe(i) != 0)
69 continue;
Peter Tyser5d933252010-10-22 00:20:28 -050070
Peter Tyserde2ef6b2010-10-22 00:20:27 -050071 for (j = 0; j < sizeof(i2c_addr_list); ++j) {
72 if (i == i2c_addr_list[j]) {
Peter Tyser5d933252010-10-22 00:20:28 -050073 i2c_addr_list[j] = 0xff;
Peter Tyserde2ef6b2010-10-22 00:20:27 -050074 break;
Wolfgang Denkb38e0df2007-03-06 18:08:43 +010075 }
Peter Tyserde2ef6b2010-10-22 00:20:27 -050076 }
77
78 if (j == sizeof(i2c_addr_list)) {
Peter Tyser5d933252010-10-22 00:20:28 -050079 ret = -1;
80 post_log("I2C: addr %02x not expected\n", i);
Wolfgang Denkb38e0df2007-03-06 18:08:43 +010081 }
82 }
83
Peter Tyser5d933252010-10-22 00:20:28 -050084 for (i = 0; i < sizeof(i2c_addr_list); ++i) {
85 if (i2c_addr_list[i] == 0xff)
86 continue;
Anatolij Gustschinb5a389f2013-04-15 04:01:07 +000087 if (i2c_ignore_device(i2c_addr_list[i]))
88 continue;
Peter Tyser5d933252010-10-22 00:20:28 -050089 post_log("I2C: addr %02x did not respond\n", i2c_addr_list[i]);
90 ret = -1;
Wolfgang Denkb38e0df2007-03-06 18:08:43 +010091 }
Peter Tyser5d933252010-10-22 00:20:28 -050092
93 return ret;
Wolfgang Denkb38e0df2007-03-06 18:08:43 +010094#endif
95}
96
Tom Rini3dd5d4a2022-12-04 10:14:17 -050097#endif /* CFG_POST & CFG_SYS_POST_I2C */