blob: 95da2ebdaf528b191bb4c2d83235d4381779869e [file] [log] [blame]
Wolfgang Denkad5bb452007-03-06 18:08:43 +01001/*
2 * (C) Copyright 2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Wolfgang Denkad5bb452007-03-06 18:08:43 +01006 */
7
Wolfgang Denkad5bb452007-03-06 18:08:43 +01008/*
9 * I2C test
10 *
11 * For verifying the I2C bus, a full I2C bus scanning is performed.
12 *
Peter Tyser60aaaa02010-10-22 00:20:30 -050013 * #ifdef CONFIG_SYS_POST_I2C_ADDRS
Peter Tyser9d921f12010-10-22 00:20:31 -050014 * The test is considered as passed if all the devices and only the devices
15 * in the list are found.
16 * #ifdef CONFIG_SYS_POST_I2C_IGNORES
17 * Ignore devices listed in CONFIG_SYS_POST_I2C_IGNORES. These devices
18 * are optional or not vital to board functionality.
19 * #endif
Peter Tyser60aaaa02010-10-22 00:20:30 -050020 * #else [ ! CONFIG_SYS_POST_I2C_ADDRS ]
Wolfgang Denkad5bb452007-03-06 18:08:43 +010021 * The test is considered as passed if any I2C device is found.
22 * #endif
23 */
24
Peter Tyserb9b1bc82010-10-22 00:20:27 -050025#include <common.h>
Wolfgang Denkad5bb452007-03-06 18:08:43 +010026#include <post.h>
27#include <i2c.h>
28
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020029#if CONFIG_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
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020097#endif /* CONFIG_POST & CONFIG_SYS_POST_I2C */