xref: /OK3568_Linux_fs/u-boot/post/drivers/i2c.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * (C) Copyright 2002
3*4882a593Smuzhiyun  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun /*
9*4882a593Smuzhiyun  * I2C test
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * For verifying the I2C bus, a full I2C bus scanning is performed.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * #ifdef CONFIG_SYS_POST_I2C_ADDRS
14*4882a593Smuzhiyun  *   The test is considered as passed if all the devices and only the devices
15*4882a593Smuzhiyun  *   in the list are found.
16*4882a593Smuzhiyun  *   #ifdef CONFIG_SYS_POST_I2C_IGNORES
17*4882a593Smuzhiyun  *     Ignore devices listed in CONFIG_SYS_POST_I2C_IGNORES.  These devices
18*4882a593Smuzhiyun  *     are optional or not vital to board functionality.
19*4882a593Smuzhiyun  *   #endif
20*4882a593Smuzhiyun  * #else [ ! CONFIG_SYS_POST_I2C_ADDRS ]
21*4882a593Smuzhiyun  *   The test is considered as passed if any I2C device is found.
22*4882a593Smuzhiyun  * #endif
23*4882a593Smuzhiyun  */
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #include <common.h>
26*4882a593Smuzhiyun #include <post.h>
27*4882a593Smuzhiyun #include <i2c.h>
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #if CONFIG_POST & CONFIG_SYS_POST_I2C
30*4882a593Smuzhiyun 
i2c_ignore_device(unsigned int chip)31*4882a593Smuzhiyun static int i2c_ignore_device(unsigned int chip)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun #ifdef CONFIG_SYS_POST_I2C_IGNORES
34*4882a593Smuzhiyun 	const unsigned char i2c_ignore_list[] = CONFIG_SYS_POST_I2C_IGNORES;
35*4882a593Smuzhiyun 	int i;
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	for (i = 0; i < sizeof(i2c_ignore_list); i++)
38*4882a593Smuzhiyun 		if (i2c_ignore_list[i] == chip)
39*4882a593Smuzhiyun 			return 1;
40*4882a593Smuzhiyun #endif
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	return 0;
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun 
i2c_post_test(int flags)45*4882a593Smuzhiyun int i2c_post_test (int flags)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun 	unsigned int i;
48*4882a593Smuzhiyun #ifndef CONFIG_SYS_POST_I2C_ADDRS
49*4882a593Smuzhiyun 	/* Start at address 1, address 0 is the general call address */
50*4882a593Smuzhiyun 	for (i = 1; i < 128; i++) {
51*4882a593Smuzhiyun 		if (i2c_ignore_device(i))
52*4882a593Smuzhiyun 			continue;
53*4882a593Smuzhiyun 		if (i2c_probe (i) == 0)
54*4882a593Smuzhiyun 			return 0;
55*4882a593Smuzhiyun 	}
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	/* No devices found */
58*4882a593Smuzhiyun 	return -1;
59*4882a593Smuzhiyun #else
60*4882a593Smuzhiyun 	unsigned int ret  = 0;
61*4882a593Smuzhiyun 	int j;
62*4882a593Smuzhiyun 	unsigned char i2c_addr_list[] = CONFIG_SYS_POST_I2C_ADDRS;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	/* Start at address 1, address 0 is the general call address */
65*4882a593Smuzhiyun 	for (i = 1; i < 128; i++) {
66*4882a593Smuzhiyun 		if (i2c_ignore_device(i))
67*4882a593Smuzhiyun 			continue;
68*4882a593Smuzhiyun 		if (i2c_probe(i) != 0)
69*4882a593Smuzhiyun 			continue;
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 		for (j = 0; j < sizeof(i2c_addr_list); ++j) {
72*4882a593Smuzhiyun 			if (i == i2c_addr_list[j]) {
73*4882a593Smuzhiyun 				i2c_addr_list[j] = 0xff;
74*4882a593Smuzhiyun 				break;
75*4882a593Smuzhiyun 			}
76*4882a593Smuzhiyun 		}
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 		if (j == sizeof(i2c_addr_list)) {
79*4882a593Smuzhiyun 			ret = -1;
80*4882a593Smuzhiyun 			post_log("I2C: addr %02x not expected\n", i);
81*4882a593Smuzhiyun 		}
82*4882a593Smuzhiyun 	}
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	for (i = 0; i < sizeof(i2c_addr_list); ++i) {
85*4882a593Smuzhiyun 		if (i2c_addr_list[i] == 0xff)
86*4882a593Smuzhiyun 			continue;
87*4882a593Smuzhiyun 		if (i2c_ignore_device(i2c_addr_list[i]))
88*4882a593Smuzhiyun 			continue;
89*4882a593Smuzhiyun 		post_log("I2C: addr %02x did not respond\n", i2c_addr_list[i]);
90*4882a593Smuzhiyun 		ret = -1;
91*4882a593Smuzhiyun 	}
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	return ret;
94*4882a593Smuzhiyun #endif
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun #endif /* CONFIG_POST & CONFIG_SYS_POST_I2C */
98