i2c.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (C) 2013 Google, Inc
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. *
  6. * Note: Test coverage does not include 10-bit addressing
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <fdtdec.h>
  11. #include <i2c.h>
  12. #include <dm/device-internal.h>
  13. #include <dm/test.h>
  14. #include <dm/uclass-internal.h>
  15. #include <dm/ut.h>
  16. #include <dm/util.h>
  17. #include <asm/state.h>
  18. #include <asm/test.h>
  19. static const int busnum;
  20. static const int chip = 0x2c;
  21. /* Test that we can find buses and chips */
  22. static int dm_test_i2c_find(struct dm_test_state *dms)
  23. {
  24. struct udevice *bus, *dev;
  25. const int no_chip = 0x10;
  26. ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_I2C, busnum,
  27. false, &bus));
  28. /*
  29. * i2c_post_bind() will bind devices to chip selects. Check this then
  30. * remove the emulation and the slave device.
  31. */
  32. ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
  33. ut_assertok(dm_i2c_probe(bus, chip, 0, &dev));
  34. ut_asserteq(-ENODEV, dm_i2c_probe(bus, no_chip, 0, &dev));
  35. ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_I2C, 1, &bus));
  36. return 0;
  37. }
  38. DM_TEST(dm_test_i2c_find, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  39. static int dm_test_i2c_read_write(struct dm_test_state *dms)
  40. {
  41. struct udevice *bus, *dev;
  42. uint8_t buf[5];
  43. ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
  44. ut_assertok(i2c_get_chip(bus, chip, 1, &dev));
  45. ut_assertok(dm_i2c_read(dev, 0, buf, 5));
  46. ut_assertok(memcmp(buf, "\0\0\0\0\0", sizeof(buf)));
  47. ut_assertok(dm_i2c_write(dev, 2, (uint8_t *)"AB", 2));
  48. ut_assertok(dm_i2c_read(dev, 0, buf, 5));
  49. ut_assertok(memcmp(buf, "\0\0AB\0", sizeof(buf)));
  50. return 0;
  51. }
  52. DM_TEST(dm_test_i2c_read_write, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  53. static int dm_test_i2c_speed(struct dm_test_state *dms)
  54. {
  55. struct udevice *bus, *dev;
  56. uint8_t buf[5];
  57. ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
  58. ut_assertok(i2c_get_chip(bus, chip, 1, &dev));
  59. ut_assertok(i2c_set_bus_speed(bus, 100000));
  60. ut_assertok(dm_i2c_read(dev, 0, buf, 5));
  61. ut_assertok(i2c_set_bus_speed(bus, 400000));
  62. ut_asserteq(400000, i2c_get_bus_speed(bus));
  63. ut_assertok(dm_i2c_read(dev, 0, buf, 5));
  64. ut_asserteq(-EINVAL, dm_i2c_write(dev, 0, buf, 5));
  65. return 0;
  66. }
  67. DM_TEST(dm_test_i2c_speed, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  68. static int dm_test_i2c_offset_len(struct dm_test_state *dms)
  69. {
  70. struct udevice *bus, *dev;
  71. uint8_t buf[5];
  72. ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
  73. ut_assertok(i2c_get_chip(bus, chip, 1, &dev));
  74. ut_assertok(i2c_set_chip_offset_len(dev, 1));
  75. ut_assertok(dm_i2c_read(dev, 0, buf, 5));
  76. /* This is not supported by the uclass */
  77. ut_asserteq(-EINVAL, i2c_set_chip_offset_len(dev, 5));
  78. return 0;
  79. }
  80. DM_TEST(dm_test_i2c_offset_len, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  81. static int dm_test_i2c_probe_empty(struct dm_test_state *dms)
  82. {
  83. struct udevice *bus, *dev;
  84. ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
  85. ut_assertok(dm_i2c_probe(bus, SANDBOX_I2C_TEST_ADDR, 0, &dev));
  86. return 0;
  87. }
  88. DM_TEST(dm_test_i2c_probe_empty, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  89. static int dm_test_i2c_bytewise(struct dm_test_state *dms)
  90. {
  91. struct udevice *bus, *dev;
  92. struct udevice *eeprom;
  93. uint8_t buf[5];
  94. ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
  95. ut_assertok(i2c_get_chip(bus, chip, 1, &dev));
  96. ut_assertok(dm_i2c_read(dev, 0, buf, 5));
  97. ut_assertok(memcmp(buf, "\0\0\0\0\0", sizeof(buf)));
  98. /* Tell the EEPROM to only read/write one register at a time */
  99. ut_assertok(uclass_first_device(UCLASS_I2C_EMUL, &eeprom));
  100. ut_assertnonnull(eeprom);
  101. sandbox_i2c_eeprom_set_test_mode(eeprom, SIE_TEST_MODE_SINGLE_BYTE);
  102. /* Now we only get the first byte - the rest will be 0xff */
  103. ut_assertok(dm_i2c_read(dev, 0, buf, 5));
  104. ut_assertok(memcmp(buf, "\0\xff\xff\xff\xff", sizeof(buf)));
  105. /* If we do a separate transaction for each byte, it works */
  106. ut_assertok(i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS));
  107. ut_assertok(dm_i2c_read(dev, 0, buf, 5));
  108. ut_assertok(memcmp(buf, "\0\0\0\0\0", sizeof(buf)));
  109. /* This will only write A */
  110. ut_assertok(i2c_set_chip_flags(dev, 0));
  111. ut_assertok(dm_i2c_write(dev, 2, (uint8_t *)"AB", 2));
  112. ut_assertok(dm_i2c_read(dev, 0, buf, 5));
  113. ut_assertok(memcmp(buf, "\0\xff\xff\xff\xff", sizeof(buf)));
  114. /* Check that the B was ignored */
  115. ut_assertok(i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS));
  116. ut_assertok(dm_i2c_read(dev, 0, buf, 5));
  117. ut_assertok(memcmp(buf, "\0\0A\0\0\0", sizeof(buf)));
  118. /* Now write it again with the new flags, it should work */
  119. ut_assertok(i2c_set_chip_flags(dev, DM_I2C_CHIP_WR_ADDRESS));
  120. ut_assertok(dm_i2c_write(dev, 2, (uint8_t *)"AB", 2));
  121. ut_assertok(dm_i2c_read(dev, 0, buf, 5));
  122. ut_assertok(memcmp(buf, "\0\xff\xff\xff\xff", sizeof(buf)));
  123. ut_assertok(i2c_set_chip_flags(dev, DM_I2C_CHIP_WR_ADDRESS |
  124. DM_I2C_CHIP_RD_ADDRESS));
  125. ut_assertok(dm_i2c_read(dev, 0, buf, 5));
  126. ut_assertok(memcmp(buf, "\0\0AB\0\0", sizeof(buf)));
  127. /* Restore defaults */
  128. sandbox_i2c_eeprom_set_test_mode(eeprom, SIE_TEST_MODE_NONE);
  129. ut_assertok(i2c_set_chip_flags(dev, 0));
  130. return 0;
  131. }
  132. DM_TEST(dm_test_i2c_bytewise, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  133. static int dm_test_i2c_offset(struct dm_test_state *dms)
  134. {
  135. struct udevice *eeprom;
  136. struct udevice *dev;
  137. uint8_t buf[5];
  138. ut_assertok(i2c_get_chip_for_busnum(busnum, chip, 1, &dev));
  139. /* Do a transfer so we can find the emulator */
  140. ut_assertok(dm_i2c_read(dev, 0, buf, 5));
  141. ut_assertok(uclass_first_device(UCLASS_I2C_EMUL, &eeprom));
  142. /* Offset length 0 */
  143. sandbox_i2c_eeprom_set_offset_len(eeprom, 0);
  144. ut_assertok(i2c_set_chip_offset_len(dev, 0));
  145. ut_assertok(dm_i2c_write(dev, 10 /* ignored */, (uint8_t *)"AB", 2));
  146. ut_assertok(dm_i2c_read(dev, 0, buf, 5));
  147. ut_assertok(memcmp(buf, "AB\0\0\0\0", sizeof(buf)));
  148. /* Offset length 1 */
  149. sandbox_i2c_eeprom_set_offset_len(eeprom, 1);
  150. ut_assertok(i2c_set_chip_offset_len(dev, 1));
  151. ut_assertok(dm_i2c_write(dev, 2, (uint8_t *)"AB", 2));
  152. ut_assertok(dm_i2c_read(dev, 0, buf, 5));
  153. ut_assertok(memcmp(buf, "ABAB\0", sizeof(buf)));
  154. /* Offset length 2 */
  155. sandbox_i2c_eeprom_set_offset_len(eeprom, 2);
  156. ut_assertok(i2c_set_chip_offset_len(dev, 2));
  157. ut_assertok(dm_i2c_write(dev, 0x210, (uint8_t *)"AB", 2));
  158. ut_assertok(dm_i2c_read(dev, 0x210, buf, 5));
  159. ut_assertok(memcmp(buf, "AB\0\0\0", sizeof(buf)));
  160. /* Offset length 3 */
  161. sandbox_i2c_eeprom_set_offset_len(eeprom, 2);
  162. ut_assertok(i2c_set_chip_offset_len(dev, 2));
  163. ut_assertok(dm_i2c_write(dev, 0x410, (uint8_t *)"AB", 2));
  164. ut_assertok(dm_i2c_read(dev, 0x410, buf, 5));
  165. ut_assertok(memcmp(buf, "AB\0\0\0", sizeof(buf)));
  166. /* Offset length 4 */
  167. sandbox_i2c_eeprom_set_offset_len(eeprom, 2);
  168. ut_assertok(i2c_set_chip_offset_len(dev, 2));
  169. ut_assertok(dm_i2c_write(dev, 0x420, (uint8_t *)"AB", 2));
  170. ut_assertok(dm_i2c_read(dev, 0x420, buf, 5));
  171. ut_assertok(memcmp(buf, "AB\0\0\0", sizeof(buf)));
  172. /* Restore defaults */
  173. sandbox_i2c_eeprom_set_offset_len(eeprom, 1);
  174. return 0;
  175. }
  176. DM_TEST(dm_test_i2c_offset, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);