Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Wolfgang Denk | ad5bb45 | 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 | ad5bb45 | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Wolfgang Denk | ad5bb45 | 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 | 60aaaa0 | 2010-10-22 00:20:30 -0500 | [diff] [blame] | 12 | * #ifdef CONFIG_SYS_POST_I2C_ADDRS |
Peter Tyser | 9d921f1 | 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 | 60aaaa0 | 2010-10-22 00:20:30 -0500 | [diff] [blame] | 19 | * #else [ ! CONFIG_SYS_POST_I2C_ADDRS ] |
Wolfgang Denk | ad5bb45 | 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 | b9b1bc8 | 2010-10-22 00:20:27 -0500 | [diff] [blame] | 24 | #include <common.h> |
Wolfgang Denk | ad5bb45 | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 25 | #include <post.h> |
| 26 | #include <i2c.h> |
| 27 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 28 | #if CONFIG_POST & CONFIG_SYS_POST_I2C |
Wolfgang Denk | ad5bb45 | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 29 | |
Peter Tyser | 9d921f1 | 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 | ad5bb45 | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 44 | int i2c_post_test (int flags) |
| 45 | { |
| 46 | unsigned int i; |
Peter Tyser | 60aaaa0 | 2010-10-22 00:20:30 -0500 | [diff] [blame] | 47 | #ifndef CONFIG_SYS_POST_I2C_ADDRS |
Peter Tyser | 9f949c9 | 2010-10-22 00:20:29 -0500 | [diff] [blame] | 48 | /* Start at address 1, address 0 is the general call address */ |
Heiko Schocher | 9b107e6 | 2010-11-15 08:20:39 +0100 | [diff] [blame] | 49 | for (i = 1; i < 128; i++) { |
Peter Tyser | 9d921f1 | 2010-10-22 00:20:31 -0500 | [diff] [blame] | 50 | if (i2c_ignore_device(i)) |
| 51 | continue; |
Peter Tyser | b9b1bc8 | 2010-10-22 00:20:27 -0500 | [diff] [blame] | 52 | if (i2c_probe (i) == 0) |
| 53 | return 0; |
Heiko Schocher | 9b107e6 | 2010-11-15 08:20:39 +0100 | [diff] [blame] | 54 | } |
Peter Tyser | b9b1bc8 | 2010-10-22 00:20:27 -0500 | [diff] [blame] | 55 | |
| 56 | /* No devices found */ |
| 57 | return -1; |
| 58 | #else |
Peter Tyser | 7e263ce | 2010-10-22 00:20:28 -0500 | [diff] [blame] | 59 | unsigned int ret = 0; |
Wolfgang Denk | ad5bb45 | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 60 | int j; |
Wolfgang Denk | 8343f8a | 2010-10-26 23:22:36 +0200 | [diff] [blame] | 61 | unsigned char i2c_addr_list[] = CONFIG_SYS_POST_I2C_ADDRS; |
Wolfgang Denk | ad5bb45 | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 62 | |
Peter Tyser | 9f949c9 | 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 | 9d921f1 | 2010-10-22 00:20:31 -0500 | [diff] [blame] | 65 | if (i2c_ignore_device(i)) |
| 66 | continue; |
Peter Tyser | b9b1bc8 | 2010-10-22 00:20:27 -0500 | [diff] [blame] | 67 | if (i2c_probe(i) != 0) |
| 68 | continue; |
Peter Tyser | 7e263ce | 2010-10-22 00:20:28 -0500 | [diff] [blame] | 69 | |
Peter Tyser | b9b1bc8 | 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 | 7e263ce | 2010-10-22 00:20:28 -0500 | [diff] [blame] | 72 | i2c_addr_list[j] = 0xff; |
Peter Tyser | b9b1bc8 | 2010-10-22 00:20:27 -0500 | [diff] [blame] | 73 | break; |
Wolfgang Denk | ad5bb45 | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 74 | } |
Peter Tyser | b9b1bc8 | 2010-10-22 00:20:27 -0500 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | if (j == sizeof(i2c_addr_list)) { |
Peter Tyser | 7e263ce | 2010-10-22 00:20:28 -0500 | [diff] [blame] | 78 | ret = -1; |
| 79 | post_log("I2C: addr %02x not expected\n", i); |
Wolfgang Denk | ad5bb45 | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
Peter Tyser | 7e263ce | 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 | c552850 | 2013-04-15 04:01:07 +0000 | [diff] [blame] | 86 | if (i2c_ignore_device(i2c_addr_list[i])) |
| 87 | continue; |
Peter Tyser | 7e263ce | 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 | ad5bb45 | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 90 | } |
Peter Tyser | 7e263ce | 2010-10-22 00:20:28 -0500 | [diff] [blame] | 91 | |
| 92 | return ret; |
Wolfgang Denk | ad5bb45 | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 93 | #endif |
| 94 | } |
| 95 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 96 | #endif /* CONFIG_POST & CONFIG_SYS_POST_I2C */ |