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 | * |
Peter Tyser | 3f1d0db | 2010-10-22 00:20:30 -0500 | [diff] [blame] | 12 | * #ifdef CONFIG_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. |
| 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 Tyser | 3f1d0db | 2010-10-22 00:20:30 -0500 | [diff] [blame] | 19 | * #else [ ! CONFIG_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 | |
Peter Tyser | de2ef6b | 2010-10-22 00:20:27 -0500 | [diff] [blame] | 24 | #include <common.h> |
Wolfgang Denk | b38e0df | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 25 | #include <post.h> |
| 26 | #include <i2c.h> |
| 27 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 28 | #if CONFIG_POST & CONFIG_SYS_POST_I2C |
Wolfgang Denk | b38e0df | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 29 | |
Peter Tyser | d482dd3 | 2010-10-22 00:20:31 -0500 | [diff] [blame] | 30 | static 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 Denk | b38e0df | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 44 | int i2c_post_test (int flags) |
| 45 | { |
| 46 | unsigned int i; |
Peter Tyser | 3f1d0db | 2010-10-22 00:20:30 -0500 | [diff] [blame] | 47 | #ifndef CONFIG_SYS_POST_I2C_ADDRS |
Peter Tyser | 0ce0189 | 2010-10-22 00:20:29 -0500 | [diff] [blame] | 48 | /* Start at address 1, address 0 is the general call address */ |
Heiko Schocher | 0022475 | 2010-11-15 08:20:39 +0100 | [diff] [blame] | 49 | for (i = 1; i < 128; i++) { |
Peter Tyser | d482dd3 | 2010-10-22 00:20:31 -0500 | [diff] [blame] | 50 | if (i2c_ignore_device(i)) |
| 51 | continue; |
Peter Tyser | de2ef6b | 2010-10-22 00:20:27 -0500 | [diff] [blame] | 52 | if (i2c_probe (i) == 0) |
| 53 | return 0; |
Heiko Schocher | 0022475 | 2010-11-15 08:20:39 +0100 | [diff] [blame] | 54 | } |
Peter Tyser | de2ef6b | 2010-10-22 00:20:27 -0500 | [diff] [blame] | 55 | |
| 56 | /* No devices found */ |
| 57 | return -1; |
| 58 | #else |
Peter Tyser | 5d93325 | 2010-10-22 00:20:28 -0500 | [diff] [blame] | 59 | unsigned int ret = 0; |
Wolfgang Denk | b38e0df | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 60 | int j; |
Wolfgang Denk | 69fc70a | 2010-10-26 23:22:36 +0200 | [diff] [blame] | 61 | unsigned char i2c_addr_list[] = CONFIG_SYS_POST_I2C_ADDRS; |
Wolfgang Denk | b38e0df | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 62 | |
Peter Tyser | 0ce0189 | 2010-10-22 00:20:29 -0500 | [diff] [blame] | 63 | /* Start at address 1, address 0 is the general call address */ |
| 64 | for (i = 1; i < 128; i++) { |
Peter Tyser | d482dd3 | 2010-10-22 00:20:31 -0500 | [diff] [blame] | 65 | if (i2c_ignore_device(i)) |
| 66 | continue; |
Peter Tyser | de2ef6b | 2010-10-22 00:20:27 -0500 | [diff] [blame] | 67 | if (i2c_probe(i) != 0) |
| 68 | continue; |
Peter Tyser | 5d93325 | 2010-10-22 00:20:28 -0500 | [diff] [blame] | 69 | |
Peter Tyser | de2ef6b | 2010-10-22 00:20:27 -0500 | [diff] [blame] | 70 | for (j = 0; j < sizeof(i2c_addr_list); ++j) { |
| 71 | if (i == i2c_addr_list[j]) { |
Peter Tyser | 5d93325 | 2010-10-22 00:20:28 -0500 | [diff] [blame] | 72 | i2c_addr_list[j] = 0xff; |
Peter Tyser | de2ef6b | 2010-10-22 00:20:27 -0500 | [diff] [blame] | 73 | break; |
Wolfgang Denk | b38e0df | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 74 | } |
Peter Tyser | de2ef6b | 2010-10-22 00:20:27 -0500 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | if (j == sizeof(i2c_addr_list)) { |
Peter Tyser | 5d93325 | 2010-10-22 00:20:28 -0500 | [diff] [blame] | 78 | ret = -1; |
| 79 | post_log("I2C: addr %02x not expected\n", i); |
Wolfgang Denk | b38e0df | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
Peter Tyser | 5d93325 | 2010-10-22 00:20:28 -0500 | [diff] [blame] | 83 | for (i = 0; i < sizeof(i2c_addr_list); ++i) { |
| 84 | if (i2c_addr_list[i] == 0xff) |
| 85 | continue; |
Anatolij Gustschin | b5a389f | 2013-04-15 04:01:07 +0000 | [diff] [blame] | 86 | if (i2c_ignore_device(i2c_addr_list[i])) |
| 87 | continue; |
Peter Tyser | 5d93325 | 2010-10-22 00:20:28 -0500 | [diff] [blame] | 88 | post_log("I2C: addr %02x did not respond\n", i2c_addr_list[i]); |
| 89 | ret = -1; |
Wolfgang Denk | b38e0df | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 90 | } |
Peter Tyser | 5d93325 | 2010-10-22 00:20:28 -0500 | [diff] [blame] | 91 | |
| 92 | return ret; |
Wolfgang Denk | b38e0df | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 93 | #endif |
| 94 | } |
| 95 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 96 | #endif /* CONFIG_POST & CONFIG_SYS_POST_I2C */ |