Wolfgang Denk | ad5bb45 | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Wolfgang Denk | ad5bb45 | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Wolfgang Denk | ad5bb45 | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 8 | /* |
| 9 | * I2C test |
| 10 | * |
| 11 | * For verifying the I2C bus, a full I2C bus scanning is performed. |
| 12 | * |
Peter Tyser | 60aaaa0 | 2010-10-22 00:20:30 -0500 | [diff] [blame] | 13 | * #ifdef CONFIG_SYS_POST_I2C_ADDRS |
Peter Tyser | 9d921f1 | 2010-10-22 00:20:31 -0500 | [diff] [blame] | 14 | * 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 Tyser | 60aaaa0 | 2010-10-22 00:20:30 -0500 | [diff] [blame] | 20 | * #else [ ! CONFIG_SYS_POST_I2C_ADDRS ] |
Wolfgang Denk | ad5bb45 | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 21 | * The test is considered as passed if any I2C device is found. |
| 22 | * #endif |
| 23 | */ |
| 24 | |
Peter Tyser | b9b1bc8 | 2010-10-22 00:20:27 -0500 | [diff] [blame] | 25 | #include <common.h> |
Wolfgang Denk | ad5bb45 | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 26 | #include <post.h> |
| 27 | #include <i2c.h> |
| 28 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 29 | #if CONFIG_POST & CONFIG_SYS_POST_I2C |
Wolfgang Denk | ad5bb45 | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 30 | |
Peter Tyser | 9d921f1 | 2010-10-22 00:20:31 -0500 | [diff] [blame] | 31 | static 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 Denk | ad5bb45 | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 45 | int i2c_post_test (int flags) |
| 46 | { |
| 47 | unsigned int i; |
Peter Tyser | 60aaaa0 | 2010-10-22 00:20:30 -0500 | [diff] [blame] | 48 | #ifndef CONFIG_SYS_POST_I2C_ADDRS |
Peter Tyser | 9f949c9 | 2010-10-22 00:20:29 -0500 | [diff] [blame] | 49 | /* Start at address 1, address 0 is the general call address */ |
Heiko Schocher | 9b107e6 | 2010-11-15 08:20:39 +0100 | [diff] [blame] | 50 | for (i = 1; i < 128; i++) { |
Peter Tyser | 9d921f1 | 2010-10-22 00:20:31 -0500 | [diff] [blame] | 51 | if (i2c_ignore_device(i)) |
| 52 | continue; |
Peter Tyser | b9b1bc8 | 2010-10-22 00:20:27 -0500 | [diff] [blame] | 53 | if (i2c_probe (i) == 0) |
| 54 | return 0; |
Heiko Schocher | 9b107e6 | 2010-11-15 08:20:39 +0100 | [diff] [blame] | 55 | } |
Peter Tyser | b9b1bc8 | 2010-10-22 00:20:27 -0500 | [diff] [blame] | 56 | |
| 57 | /* No devices found */ |
| 58 | return -1; |
| 59 | #else |
Peter Tyser | 7e263ce | 2010-10-22 00:20:28 -0500 | [diff] [blame] | 60 | unsigned int ret = 0; |
Wolfgang Denk | ad5bb45 | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 61 | int j; |
Wolfgang Denk | 8343f8a | 2010-10-26 23:22:36 +0200 | [diff] [blame] | 62 | unsigned char i2c_addr_list[] = CONFIG_SYS_POST_I2C_ADDRS; |
Wolfgang Denk | ad5bb45 | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 63 | |
Peter Tyser | 9f949c9 | 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 | 9d921f1 | 2010-10-22 00:20:31 -0500 | [diff] [blame] | 66 | if (i2c_ignore_device(i)) |
| 67 | continue; |
Peter Tyser | b9b1bc8 | 2010-10-22 00:20:27 -0500 | [diff] [blame] | 68 | if (i2c_probe(i) != 0) |
| 69 | continue; |
Peter Tyser | 7e263ce | 2010-10-22 00:20:28 -0500 | [diff] [blame] | 70 | |
Peter Tyser | b9b1bc8 | 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 | 7e263ce | 2010-10-22 00:20:28 -0500 | [diff] [blame] | 73 | i2c_addr_list[j] = 0xff; |
Peter Tyser | b9b1bc8 | 2010-10-22 00:20:27 -0500 | [diff] [blame] | 74 | break; |
Wolfgang Denk | ad5bb45 | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 75 | } |
Peter Tyser | b9b1bc8 | 2010-10-22 00:20:27 -0500 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | if (j == sizeof(i2c_addr_list)) { |
Peter Tyser | 7e263ce | 2010-10-22 00:20:28 -0500 | [diff] [blame] | 79 | ret = -1; |
| 80 | post_log("I2C: addr %02x not expected\n", i); |
Wolfgang Denk | ad5bb45 | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Peter Tyser | 7e263ce | 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 | c552850 | 2013-04-15 04:01:07 +0000 | [diff] [blame] | 87 | if (i2c_ignore_device(i2c_addr_list[i])) |
| 88 | continue; |
Peter Tyser | 7e263ce | 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 | ad5bb45 | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 91 | } |
Peter Tyser | 7e263ce | 2010-10-22 00:20:28 -0500 | [diff] [blame] | 92 | |
| 93 | return ret; |
Wolfgang Denk | ad5bb45 | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 94 | #endif |
| 95 | } |
| 96 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 97 | #endif /* CONFIG_POST & CONFIG_SYS_POST_I2C */ |