i2c.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * (C) Copyright 2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * I2C test
  25. *
  26. * For verifying the I2C bus, a full I2C bus scanning is performed.
  27. *
  28. * #ifdef CONFIG_SYS_POST_I2C_ADDRS
  29. * The test is considered as passed if all the devices and only the devices
  30. * in the list are found.
  31. * #ifdef CONFIG_SYS_POST_I2C_IGNORES
  32. * Ignore devices listed in CONFIG_SYS_POST_I2C_IGNORES. These devices
  33. * are optional or not vital to board functionality.
  34. * #endif
  35. * #else [ ! CONFIG_SYS_POST_I2C_ADDRS ]
  36. * The test is considered as passed if any I2C device is found.
  37. * #endif
  38. */
  39. #include <common.h>
  40. #include <post.h>
  41. #include <i2c.h>
  42. #if CONFIG_POST & CONFIG_SYS_POST_I2C
  43. static int i2c_ignore_device(unsigned int chip)
  44. {
  45. #ifdef CONFIG_SYS_POST_I2C_IGNORES
  46. const unsigned char i2c_ignore_list[] = CONFIG_SYS_POST_I2C_IGNORES;
  47. int i;
  48. for (i = 0; i < sizeof(i2c_ignore_list); i++)
  49. if (i2c_ignore_list[i] == chip)
  50. return 1;
  51. #endif
  52. return 0;
  53. }
  54. int i2c_post_test (int flags)
  55. {
  56. unsigned int i;
  57. #ifndef CONFIG_SYS_POST_I2C_ADDRS
  58. /* Start at address 1, address 0 is the general call address */
  59. for (i = 1; i < 128; i++) {
  60. if (i2c_ignore_device(i))
  61. continue;
  62. if (i2c_probe (i) == 0)
  63. return 0;
  64. }
  65. /* No devices found */
  66. return -1;
  67. #else
  68. unsigned int ret = 0;
  69. int j;
  70. unsigned char i2c_addr_list[] = CONFIG_SYS_POST_I2C_ADDRS;
  71. /* Start at address 1, address 0 is the general call address */
  72. for (i = 1; i < 128; i++) {
  73. if (i2c_ignore_device(i))
  74. continue;
  75. if (i2c_probe(i) != 0)
  76. continue;
  77. for (j = 0; j < sizeof(i2c_addr_list); ++j) {
  78. if (i == i2c_addr_list[j]) {
  79. i2c_addr_list[j] = 0xff;
  80. break;
  81. }
  82. }
  83. if (j == sizeof(i2c_addr_list)) {
  84. ret = -1;
  85. post_log("I2C: addr %02x not expected\n", i);
  86. }
  87. }
  88. for (i = 0; i < sizeof(i2c_addr_list); ++i) {
  89. if (i2c_addr_list[i] == 0xff)
  90. continue;
  91. if (i2c_ignore_device(i2c_addr_list[i]))
  92. continue;
  93. post_log("I2C: addr %02x did not respond\n", i2c_addr_list[i]);
  94. ret = -1;
  95. }
  96. return ret;
  97. #endif
  98. }
  99. #endif /* CONFIG_POST & CONFIG_SYS_POST_I2C */