Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Wolfgang Denk | b38e0df | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2002 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Wolfgang Denk | b38e0df | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Wolfgang Denk | b38e0df | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 7 | /* |
| 8 | * I2C test |
| 9 | * |
| 10 | * For verifying the I2C bus, a full I2C bus scanning is performed. |
| 11 | * |
Tom Rini | 3dd5d4a | 2022-12-04 10:14:17 -0500 | [diff] [blame] | 12 | * #ifdef CFG_SYS_POST_I2C_ADDRS |
Peter Tyser | d482dd3 | 2010-10-22 00:20:31 -0500 | [diff] [blame] | 13 | * The test is considered as passed if all the devices and only the devices |
| 14 | * in the list are found. |
Tom Rini | 3dd5d4a | 2022-12-04 10:14:17 -0500 | [diff] [blame] | 15 | * #ifdef CFG_SYS_POST_I2C_IGNORES |
| 16 | * Ignore devices listed in CFG_SYS_POST_I2C_IGNORES. These devices |
Peter Tyser | d482dd3 | 2010-10-22 00:20:31 -0500 | [diff] [blame] | 17 | * are optional or not vital to board functionality. |
| 18 | * #endif |
Tom Rini | 3dd5d4a | 2022-12-04 10:14:17 -0500 | [diff] [blame] | 19 | * #else [ ! CFG_SYS_POST_I2C_ADDRS ] |
Wolfgang Denk | b38e0df | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 20 | * The test is considered as passed if any I2C device is found. |
| 21 | * #endif |
| 22 | */ |
| 23 | |
Tom Rini | abb9a04 | 2024-05-18 20:20:43 -0600 | [diff] [blame] | 24 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 25 | #include <log.h> |
Wolfgang Denk | b38e0df | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 26 | #include <post.h> |
| 27 | #include <i2c.h> |
| 28 | |
Tom Rini | 3dd5d4a | 2022-12-04 10:14:17 -0500 | [diff] [blame] | 29 | #if CFG_POST & CFG_SYS_POST_I2C |
Wolfgang Denk | b38e0df | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 30 | |
Peter Tyser | d482dd3 | 2010-10-22 00:20:31 -0500 | [diff] [blame] | 31 | static int i2c_ignore_device(unsigned int chip) |
| 32 | { |
Tom Rini | 3dd5d4a | 2022-12-04 10:14:17 -0500 | [diff] [blame] | 33 | #ifdef CFG_SYS_POST_I2C_IGNORES |
| 34 | const unsigned char i2c_ignore_list[] = CFG_SYS_POST_I2C_IGNORES; |
Peter Tyser | d482dd3 | 2010-10-22 00:20:31 -0500 | [diff] [blame] | 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 Denk | b38e0df | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 45 | int i2c_post_test (int flags) |
| 46 | { |
| 47 | unsigned int i; |
Tom Rini | 3dd5d4a | 2022-12-04 10:14:17 -0500 | [diff] [blame] | 48 | #ifndef CFG_SYS_POST_I2C_ADDRS |
Peter Tyser | 0ce0189 | 2010-10-22 00:20:29 -0500 | [diff] [blame] | 49 | /* Start at address 1, address 0 is the general call address */ |
Heiko Schocher | 0022475 | 2010-11-15 08:20:39 +0100 | [diff] [blame] | 50 | for (i = 1; i < 128; i++) { |
Peter Tyser | d482dd3 | 2010-10-22 00:20:31 -0500 | [diff] [blame] | 51 | if (i2c_ignore_device(i)) |
| 52 | continue; |
Peter Tyser | de2ef6b | 2010-10-22 00:20:27 -0500 | [diff] [blame] | 53 | if (i2c_probe (i) == 0) |
| 54 | return 0; |
Heiko Schocher | 0022475 | 2010-11-15 08:20:39 +0100 | [diff] [blame] | 55 | } |
Peter Tyser | de2ef6b | 2010-10-22 00:20:27 -0500 | [diff] [blame] | 56 | |
| 57 | /* No devices found */ |
| 58 | return -1; |
| 59 | #else |
Peter Tyser | 5d93325 | 2010-10-22 00:20:28 -0500 | [diff] [blame] | 60 | unsigned int ret = 0; |
Wolfgang Denk | b38e0df | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 61 | int j; |
Tom Rini | 3dd5d4a | 2022-12-04 10:14:17 -0500 | [diff] [blame] | 62 | unsigned char i2c_addr_list[] = CFG_SYS_POST_I2C_ADDRS; |
Wolfgang Denk | b38e0df | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 63 | |
Peter Tyser | 0ce0189 | 2010-10-22 00:20:29 -0500 | [diff] [blame] | 64 | /* Start at address 1, address 0 is the general call address */ |
| 65 | for (i = 1; i < 128; i++) { |
Peter Tyser | d482dd3 | 2010-10-22 00:20:31 -0500 | [diff] [blame] | 66 | if (i2c_ignore_device(i)) |
| 67 | continue; |
Peter Tyser | de2ef6b | 2010-10-22 00:20:27 -0500 | [diff] [blame] | 68 | if (i2c_probe(i) != 0) |
| 69 | continue; |
Peter Tyser | 5d93325 | 2010-10-22 00:20:28 -0500 | [diff] [blame] | 70 | |
Peter Tyser | de2ef6b | 2010-10-22 00:20:27 -0500 | [diff] [blame] | 71 | for (j = 0; j < sizeof(i2c_addr_list); ++j) { |
| 72 | if (i == i2c_addr_list[j]) { |
Peter Tyser | 5d93325 | 2010-10-22 00:20:28 -0500 | [diff] [blame] | 73 | i2c_addr_list[j] = 0xff; |
Peter Tyser | de2ef6b | 2010-10-22 00:20:27 -0500 | [diff] [blame] | 74 | break; |
Wolfgang Denk | b38e0df | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 75 | } |
Peter Tyser | de2ef6b | 2010-10-22 00:20:27 -0500 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | if (j == sizeof(i2c_addr_list)) { |
Peter Tyser | 5d93325 | 2010-10-22 00:20:28 -0500 | [diff] [blame] | 79 | ret = -1; |
| 80 | post_log("I2C: addr %02x not expected\n", i); |
Wolfgang Denk | b38e0df | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Peter Tyser | 5d93325 | 2010-10-22 00:20:28 -0500 | [diff] [blame] | 84 | for (i = 0; i < sizeof(i2c_addr_list); ++i) { |
| 85 | if (i2c_addr_list[i] == 0xff) |
| 86 | continue; |
Anatolij Gustschin | b5a389f | 2013-04-15 04:01:07 +0000 | [diff] [blame] | 87 | if (i2c_ignore_device(i2c_addr_list[i])) |
| 88 | continue; |
Peter Tyser | 5d93325 | 2010-10-22 00:20:28 -0500 | [diff] [blame] | 89 | post_log("I2C: addr %02x did not respond\n", i2c_addr_list[i]); |
| 90 | ret = -1; |
Wolfgang Denk | b38e0df | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 91 | } |
Peter Tyser | 5d93325 | 2010-10-22 00:20:28 -0500 | [diff] [blame] | 92 | |
| 93 | return ret; |
Wolfgang Denk | b38e0df | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 94 | #endif |
| 95 | } |
| 96 | |
Tom Rini | 3dd5d4a | 2022-12-04 10:14:17 -0500 | [diff] [blame] | 97 | #endif /* CFG_POST & CFG_SYS_POST_I2C */ |