blob: 057454ffd8f5d69b7825ed70fbb31949e0aa3e07 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Wolfgang Denkad5bb452007-03-06 18:08:43 +01002/*
3 * (C) Copyright 2002
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Wolfgang Denkad5bb452007-03-06 18:08:43 +01005 */
6
Wolfgang Denkad5bb452007-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 Tyser60aaaa02010-10-22 00:20:30 -050012 * #ifdef CONFIG_SYS_POST_I2C_ADDRS
Peter Tyser9d921f12010-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 Tyser60aaaa02010-10-22 00:20:30 -050019 * #else [ ! CONFIG_SYS_POST_I2C_ADDRS ]
Wolfgang Denkad5bb452007-03-06 18:08:43 +010020 * The test is considered as passed if any I2C device is found.
21 * #endif
22 */
23
Peter Tyserb9b1bc82010-10-22 00:20:27 -050024#include <common.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060025#include <log.h>
Wolfgang Denkad5bb452007-03-06 18:08:43 +010026#include <post.h>
27#include <i2c.h>
28
Tom Rini9cebc4a2022-11-19 18:45:44 -050029#if CFG_POST & CONFIG_SYS_POST_I2C
Wolfgang Denkad5bb452007-03-06 18:08:43 +010030
Peter Tyser9d921f12010-10-22 00:20:31 -050031static int i2c_ignore_device(unsigned int chip)
32{
33#ifdef CONFIG_SYS_POST_I2C_IGNORES
34 const unsigned char i2c_ignore_list[] = CONFIG_SYS_POST_I2C_IGNORES;
35 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 Denkad5bb452007-03-06 18:08:43 +010045int i2c_post_test (int flags)
46{
47 unsigned int i;
Peter Tyser60aaaa02010-10-22 00:20:30 -050048#ifndef CONFIG_SYS_POST_I2C_ADDRS
Peter Tyser9f949c92010-10-22 00:20:29 -050049 /* Start at address 1, address 0 is the general call address */
Heiko Schocher9b107e62010-11-15 08:20:39 +010050 for (i = 1; i < 128; i++) {
Peter Tyser9d921f12010-10-22 00:20:31 -050051 if (i2c_ignore_device(i))
52 continue;
Peter Tyserb9b1bc82010-10-22 00:20:27 -050053 if (i2c_probe (i) == 0)
54 return 0;
Heiko Schocher9b107e62010-11-15 08:20:39 +010055 }
Peter Tyserb9b1bc82010-10-22 00:20:27 -050056
57 /* No devices found */
58 return -1;
59#else
Peter Tyser7e263ce2010-10-22 00:20:28 -050060 unsigned int ret = 0;
Wolfgang Denkad5bb452007-03-06 18:08:43 +010061 int j;
Wolfgang Denk8343f8a2010-10-26 23:22:36 +020062 unsigned char i2c_addr_list[] = CONFIG_SYS_POST_I2C_ADDRS;
Wolfgang Denkad5bb452007-03-06 18:08:43 +010063
Peter Tyser9f949c92010-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 Tyser9d921f12010-10-22 00:20:31 -050066 if (i2c_ignore_device(i))
67 continue;
Peter Tyserb9b1bc82010-10-22 00:20:27 -050068 if (i2c_probe(i) != 0)
69 continue;
Peter Tyser7e263ce2010-10-22 00:20:28 -050070
Peter Tyserb9b1bc82010-10-22 00:20:27 -050071 for (j = 0; j < sizeof(i2c_addr_list); ++j) {
72 if (i == i2c_addr_list[j]) {
Peter Tyser7e263ce2010-10-22 00:20:28 -050073 i2c_addr_list[j] = 0xff;
Peter Tyserb9b1bc82010-10-22 00:20:27 -050074 break;
Wolfgang Denkad5bb452007-03-06 18:08:43 +010075 }
Peter Tyserb9b1bc82010-10-22 00:20:27 -050076 }
77
78 if (j == sizeof(i2c_addr_list)) {
Peter Tyser7e263ce2010-10-22 00:20:28 -050079 ret = -1;
80 post_log("I2C: addr %02x not expected\n", i);
Wolfgang Denkad5bb452007-03-06 18:08:43 +010081 }
82 }
83
Peter Tyser7e263ce2010-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 Gustschinc5528502013-04-15 04:01:07 +000087 if (i2c_ignore_device(i2c_addr_list[i]))
88 continue;
Peter Tyser7e263ce2010-10-22 00:20:28 -050089 post_log("I2C: addr %02x did not respond\n", i2c_addr_list[i]);
90 ret = -1;
Wolfgang Denkad5bb452007-03-06 18:08:43 +010091 }
Peter Tyser7e263ce2010-10-22 00:20:28 -050092
93 return ret;
Wolfgang Denkad5bb452007-03-06 18:08:43 +010094#endif
95}
96
Tom Rini9cebc4a2022-11-19 18:45:44 -050097#endif /* CFG_POST & CONFIG_SYS_POST_I2C */