i2c.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2002
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. /*
  7. * I2C test
  8. *
  9. * For verifying the I2C bus, a full I2C bus scanning is performed.
  10. *
  11. * #ifdef CONFIG_SYS_POST_I2C_ADDRS
  12. * The test is considered as passed if all the devices and only the devices
  13. * in the list are found.
  14. * #ifdef CONFIG_SYS_POST_I2C_IGNORES
  15. * Ignore devices listed in CONFIG_SYS_POST_I2C_IGNORES. These devices
  16. * are optional or not vital to board functionality.
  17. * #endif
  18. * #else [ ! CONFIG_SYS_POST_I2C_ADDRS ]
  19. * The test is considered as passed if any I2C device is found.
  20. * #endif
  21. */
  22. #include <common.h>
  23. #include <post.h>
  24. #include <i2c.h>
  25. #if CONFIG_POST & CONFIG_SYS_POST_I2C
  26. static int i2c_ignore_device(unsigned int chip)
  27. {
  28. #ifdef CONFIG_SYS_POST_I2C_IGNORES
  29. const unsigned char i2c_ignore_list[] = CONFIG_SYS_POST_I2C_IGNORES;
  30. int i;
  31. for (i = 0; i < sizeof(i2c_ignore_list); i++)
  32. if (i2c_ignore_list[i] == chip)
  33. return 1;
  34. #endif
  35. return 0;
  36. }
  37. int i2c_post_test (int flags)
  38. {
  39. unsigned int i;
  40. #ifndef CONFIG_SYS_POST_I2C_ADDRS
  41. /* Start at address 1, address 0 is the general call address */
  42. for (i = 1; i < 128; i++) {
  43. if (i2c_ignore_device(i))
  44. continue;
  45. if (i2c_probe (i) == 0)
  46. return 0;
  47. }
  48. /* No devices found */
  49. return -1;
  50. #else
  51. unsigned int ret = 0;
  52. int j;
  53. unsigned char i2c_addr_list[] = CONFIG_SYS_POST_I2C_ADDRS;
  54. /* Start at address 1, address 0 is the general call address */
  55. for (i = 1; i < 128; i++) {
  56. if (i2c_ignore_device(i))
  57. continue;
  58. if (i2c_probe(i) != 0)
  59. continue;
  60. for (j = 0; j < sizeof(i2c_addr_list); ++j) {
  61. if (i == i2c_addr_list[j]) {
  62. i2c_addr_list[j] = 0xff;
  63. break;
  64. }
  65. }
  66. if (j == sizeof(i2c_addr_list)) {
  67. ret = -1;
  68. post_log("I2C: addr %02x not expected\n", i);
  69. }
  70. }
  71. for (i = 0; i < sizeof(i2c_addr_list); ++i) {
  72. if (i2c_addr_list[i] == 0xff)
  73. continue;
  74. if (i2c_ignore_device(i2c_addr_list[i]))
  75. continue;
  76. post_log("I2C: addr %02x did not respond\n", i2c_addr_list[i]);
  77. ret = -1;
  78. }
  79. return ret;
  80. #endif
  81. }
  82. #endif /* CONFIG_POST & CONFIG_SYS_POST_I2C */