blob: 8b7967cd9dc885726e24f4be6289f90242c1ed8c [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 *
Peter Tyser3f1d0db2010-10-22 00:20:30 -050012 * #ifdef CONFIG_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.
15 * #ifdef CONFIG_SYS_POST_I2C_IGNORES
16 * Ignore devices listed in CONFIG_SYS_POST_I2C_IGNORES. These devices
17 * are optional or not vital to board functionality.
18 * #endif
Peter Tyser3f1d0db2010-10-22 00:20:30 -050019 * #else [ ! CONFIG_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
Peter Tyserde2ef6b2010-10-22 00:20:27 -050024#include <common.h>
Wolfgang Denkb38e0df2007-03-06 18:08:43 +010025#include <post.h>
26#include <i2c.h>
27
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020028#if CONFIG_POST & CONFIG_SYS_POST_I2C
Wolfgang Denkb38e0df2007-03-06 18:08:43 +010029
Peter Tyserd482dd32010-10-22 00:20:31 -050030static int i2c_ignore_device(unsigned int chip)
31{
32#ifdef CONFIG_SYS_POST_I2C_IGNORES
33 const unsigned char i2c_ignore_list[] = CONFIG_SYS_POST_I2C_IGNORES;
34 int i;
35
36 for (i = 0; i < sizeof(i2c_ignore_list); i++)
37 if (i2c_ignore_list[i] == chip)
38 return 1;
39#endif
40
41 return 0;
42}
43
Wolfgang Denkb38e0df2007-03-06 18:08:43 +010044int i2c_post_test (int flags)
45{
46 unsigned int i;
Peter Tyser3f1d0db2010-10-22 00:20:30 -050047#ifndef CONFIG_SYS_POST_I2C_ADDRS
Peter Tyser0ce01892010-10-22 00:20:29 -050048 /* Start at address 1, address 0 is the general call address */
Heiko Schocher00224752010-11-15 08:20:39 +010049 for (i = 1; i < 128; i++) {
Peter Tyserd482dd32010-10-22 00:20:31 -050050 if (i2c_ignore_device(i))
51 continue;
Peter Tyserde2ef6b2010-10-22 00:20:27 -050052 if (i2c_probe (i) == 0)
53 return 0;
Heiko Schocher00224752010-11-15 08:20:39 +010054 }
Peter Tyserde2ef6b2010-10-22 00:20:27 -050055
56 /* No devices found */
57 return -1;
58#else
Peter Tyser5d933252010-10-22 00:20:28 -050059 unsigned int ret = 0;
Wolfgang Denkb38e0df2007-03-06 18:08:43 +010060 int j;
Wolfgang Denk69fc70a2010-10-26 23:22:36 +020061 unsigned char i2c_addr_list[] = CONFIG_SYS_POST_I2C_ADDRS;
Wolfgang Denkb38e0df2007-03-06 18:08:43 +010062
Peter Tyser0ce01892010-10-22 00:20:29 -050063 /* Start at address 1, address 0 is the general call address */
64 for (i = 1; i < 128; i++) {
Peter Tyserd482dd32010-10-22 00:20:31 -050065 if (i2c_ignore_device(i))
66 continue;
Peter Tyserde2ef6b2010-10-22 00:20:27 -050067 if (i2c_probe(i) != 0)
68 continue;
Peter Tyser5d933252010-10-22 00:20:28 -050069
Peter Tyserde2ef6b2010-10-22 00:20:27 -050070 for (j = 0; j < sizeof(i2c_addr_list); ++j) {
71 if (i == i2c_addr_list[j]) {
Peter Tyser5d933252010-10-22 00:20:28 -050072 i2c_addr_list[j] = 0xff;
Peter Tyserde2ef6b2010-10-22 00:20:27 -050073 break;
Wolfgang Denkb38e0df2007-03-06 18:08:43 +010074 }
Peter Tyserde2ef6b2010-10-22 00:20:27 -050075 }
76
77 if (j == sizeof(i2c_addr_list)) {
Peter Tyser5d933252010-10-22 00:20:28 -050078 ret = -1;
79 post_log("I2C: addr %02x not expected\n", i);
Wolfgang Denkb38e0df2007-03-06 18:08:43 +010080 }
81 }
82
Peter Tyser5d933252010-10-22 00:20:28 -050083 for (i = 0; i < sizeof(i2c_addr_list); ++i) {
84 if (i2c_addr_list[i] == 0xff)
85 continue;
Anatolij Gustschinb5a389f2013-04-15 04:01:07 +000086 if (i2c_ignore_device(i2c_addr_list[i]))
87 continue;
Peter Tyser5d933252010-10-22 00:20:28 -050088 post_log("I2C: addr %02x did not respond\n", i2c_addr_list[i]);
89 ret = -1;
Wolfgang Denkb38e0df2007-03-06 18:08:43 +010090 }
Peter Tyser5d933252010-10-22 00:20:28 -050091
92 return ret;
Wolfgang Denkb38e0df2007-03-06 18:08:43 +010093#endif
94}
95
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020096#endif /* CONFIG_POST & CONFIG_SYS_POST_I2C */