Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Google, Inc |
| 4 | * |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 5 | * Note: Test coverage does not include 10-bit addressing |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <fdtdec.h> |
| 11 | #include <i2c.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 12 | #include <asm/state.h> |
| 13 | #include <asm/test.h> |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 14 | #include <dm/device-internal.h> |
| 15 | #include <dm/test.h> |
| 16 | #include <dm/uclass-internal.h> |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 17 | #include <dm/util.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 18 | #include <test/ut.h> |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 19 | |
| 20 | static const int busnum; |
| 21 | static const int chip = 0x2c; |
| 22 | |
| 23 | /* Test that we can find buses and chips */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 24 | static int dm_test_i2c_find(struct unit_test_state *uts) |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 25 | { |
| 26 | struct udevice *bus, *dev; |
| 27 | const int no_chip = 0x10; |
| 28 | |
| 29 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_I2C, busnum, |
| 30 | false, &bus)); |
| 31 | |
| 32 | /* |
Simon Glass | 1823034 | 2016-07-05 17:10:10 -0600 | [diff] [blame] | 33 | * The post_bind() method will bind devices to chip selects. Check |
| 34 | * this then remove the emulation and the slave device. |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 35 | */ |
| 36 | ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus)); |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 37 | ut_assertok(dm_i2c_probe(bus, chip, 0, &dev)); |
| 38 | ut_asserteq(-ENODEV, dm_i2c_probe(bus, no_chip, 0, &dev)); |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 39 | ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_I2C, 1, &bus)); |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | DM_TEST(dm_test_i2c_find, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 44 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 45 | static int dm_test_i2c_read_write(struct unit_test_state *uts) |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 46 | { |
| 47 | struct udevice *bus, *dev; |
| 48 | uint8_t buf[5]; |
| 49 | |
| 50 | ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus)); |
Simon Glass | a2723ae | 2015-01-25 08:26:55 -0700 | [diff] [blame] | 51 | ut_assertok(i2c_get_chip(bus, chip, 1, &dev)); |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 52 | ut_assertok(dm_i2c_read(dev, 0, buf, 5)); |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 53 | ut_assertok(memcmp(buf, "\0\0\0\0\0", sizeof(buf))); |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 54 | ut_assertok(dm_i2c_write(dev, 2, (uint8_t *)"AB", 2)); |
| 55 | ut_assertok(dm_i2c_read(dev, 0, buf, 5)); |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 56 | ut_assertok(memcmp(buf, "\0\0AB\0", sizeof(buf))); |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | DM_TEST(dm_test_i2c_read_write, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 61 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 62 | static int dm_test_i2c_speed(struct unit_test_state *uts) |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 63 | { |
| 64 | struct udevice *bus, *dev; |
| 65 | uint8_t buf[5]; |
| 66 | |
| 67 | ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus)); |
Simon Glass | 4c70ed9 | 2015-04-20 12:37:15 -0600 | [diff] [blame] | 68 | |
| 69 | /* Use test mode so we create the required errors for invalid speeds */ |
| 70 | sandbox_i2c_set_test_mode(bus, true); |
Simon Glass | a2723ae | 2015-01-25 08:26:55 -0700 | [diff] [blame] | 71 | ut_assertok(i2c_get_chip(bus, chip, 1, &dev)); |
Simon Glass | e4e8ff2 | 2015-02-05 21:41:32 -0700 | [diff] [blame] | 72 | ut_assertok(dm_i2c_set_bus_speed(bus, 100000)); |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 73 | ut_assertok(dm_i2c_read(dev, 0, buf, 5)); |
Simon Glass | e4e8ff2 | 2015-02-05 21:41:32 -0700 | [diff] [blame] | 74 | ut_assertok(dm_i2c_set_bus_speed(bus, 400000)); |
| 75 | ut_asserteq(400000, dm_i2c_get_bus_speed(bus)); |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 76 | ut_assertok(dm_i2c_read(dev, 0, buf, 5)); |
| 77 | ut_asserteq(-EINVAL, dm_i2c_write(dev, 0, buf, 5)); |
Simon Glass | 4c70ed9 | 2015-04-20 12:37:15 -0600 | [diff] [blame] | 78 | sandbox_i2c_set_test_mode(bus, false); |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | DM_TEST(dm_test_i2c_speed, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 83 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 84 | static int dm_test_i2c_offset_len(struct unit_test_state *uts) |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 85 | { |
| 86 | struct udevice *bus, *dev; |
| 87 | uint8_t buf[5]; |
| 88 | |
| 89 | ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus)); |
Simon Glass | a2723ae | 2015-01-25 08:26:55 -0700 | [diff] [blame] | 90 | ut_assertok(i2c_get_chip(bus, chip, 1, &dev)); |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 91 | ut_assertok(i2c_set_chip_offset_len(dev, 1)); |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 92 | ut_assertok(dm_i2c_read(dev, 0, buf, 5)); |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 93 | |
| 94 | /* This is not supported by the uclass */ |
| 95 | ut_asserteq(-EINVAL, i2c_set_chip_offset_len(dev, 5)); |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | DM_TEST(dm_test_i2c_offset_len, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 100 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 101 | static int dm_test_i2c_probe_empty(struct unit_test_state *uts) |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 102 | { |
| 103 | struct udevice *bus, *dev; |
| 104 | |
| 105 | ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus)); |
Simon Glass | 4c70ed9 | 2015-04-20 12:37:15 -0600 | [diff] [blame] | 106 | |
| 107 | /* Use test mode so that this chip address will always probe */ |
| 108 | sandbox_i2c_set_test_mode(bus, true); |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 109 | ut_assertok(dm_i2c_probe(bus, SANDBOX_I2C_TEST_ADDR, 0, &dev)); |
Simon Glass | 4c70ed9 | 2015-04-20 12:37:15 -0600 | [diff] [blame] | 110 | sandbox_i2c_set_test_mode(bus, false); |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | DM_TEST(dm_test_i2c_probe_empty, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 115 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 116 | static int dm_test_i2c_bytewise(struct unit_test_state *uts) |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 117 | { |
| 118 | struct udevice *bus, *dev; |
| 119 | struct udevice *eeprom; |
| 120 | uint8_t buf[5]; |
| 121 | |
| 122 | ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus)); |
Simon Glass | a2723ae | 2015-01-25 08:26:55 -0700 | [diff] [blame] | 123 | ut_assertok(i2c_get_chip(bus, chip, 1, &dev)); |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 124 | ut_assertok(dm_i2c_read(dev, 0, buf, 5)); |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 125 | ut_assertok(memcmp(buf, "\0\0\0\0\0", sizeof(buf))); |
| 126 | |
| 127 | /* Tell the EEPROM to only read/write one register at a time */ |
| 128 | ut_assertok(uclass_first_device(UCLASS_I2C_EMUL, &eeprom)); |
| 129 | ut_assertnonnull(eeprom); |
| 130 | sandbox_i2c_eeprom_set_test_mode(eeprom, SIE_TEST_MODE_SINGLE_BYTE); |
| 131 | |
| 132 | /* Now we only get the first byte - the rest will be 0xff */ |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 133 | ut_assertok(dm_i2c_read(dev, 0, buf, 5)); |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 134 | ut_assertok(memcmp(buf, "\0\xff\xff\xff\xff", sizeof(buf))); |
| 135 | |
| 136 | /* If we do a separate transaction for each byte, it works */ |
| 137 | ut_assertok(i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS)); |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 138 | ut_assertok(dm_i2c_read(dev, 0, buf, 5)); |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 139 | ut_assertok(memcmp(buf, "\0\0\0\0\0", sizeof(buf))); |
| 140 | |
| 141 | /* This will only write A */ |
| 142 | ut_assertok(i2c_set_chip_flags(dev, 0)); |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 143 | ut_assertok(dm_i2c_write(dev, 2, (uint8_t *)"AB", 2)); |
| 144 | ut_assertok(dm_i2c_read(dev, 0, buf, 5)); |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 145 | ut_assertok(memcmp(buf, "\0\xff\xff\xff\xff", sizeof(buf))); |
| 146 | |
| 147 | /* Check that the B was ignored */ |
| 148 | ut_assertok(i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS)); |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 149 | ut_assertok(dm_i2c_read(dev, 0, buf, 5)); |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 150 | ut_assertok(memcmp(buf, "\0\0A\0\0\0", sizeof(buf))); |
| 151 | |
| 152 | /* Now write it again with the new flags, it should work */ |
| 153 | ut_assertok(i2c_set_chip_flags(dev, DM_I2C_CHIP_WR_ADDRESS)); |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 154 | ut_assertok(dm_i2c_write(dev, 2, (uint8_t *)"AB", 2)); |
| 155 | ut_assertok(dm_i2c_read(dev, 0, buf, 5)); |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 156 | ut_assertok(memcmp(buf, "\0\xff\xff\xff\xff", sizeof(buf))); |
| 157 | |
| 158 | ut_assertok(i2c_set_chip_flags(dev, DM_I2C_CHIP_WR_ADDRESS | |
| 159 | DM_I2C_CHIP_RD_ADDRESS)); |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 160 | ut_assertok(dm_i2c_read(dev, 0, buf, 5)); |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 161 | ut_assertok(memcmp(buf, "\0\0AB\0\0", sizeof(buf))); |
| 162 | |
| 163 | /* Restore defaults */ |
| 164 | sandbox_i2c_eeprom_set_test_mode(eeprom, SIE_TEST_MODE_NONE); |
| 165 | ut_assertok(i2c_set_chip_flags(dev, 0)); |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | DM_TEST(dm_test_i2c_bytewise, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 170 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 171 | static int dm_test_i2c_offset(struct unit_test_state *uts) |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 172 | { |
| 173 | struct udevice *eeprom; |
| 174 | struct udevice *dev; |
| 175 | uint8_t buf[5]; |
| 176 | |
Simon Glass | a2723ae | 2015-01-25 08:26:55 -0700 | [diff] [blame] | 177 | ut_assertok(i2c_get_chip_for_busnum(busnum, chip, 1, &dev)); |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 178 | |
| 179 | /* Do a transfer so we can find the emulator */ |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 180 | ut_assertok(dm_i2c_read(dev, 0, buf, 5)); |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 181 | ut_assertok(uclass_first_device(UCLASS_I2C_EMUL, &eeprom)); |
| 182 | |
| 183 | /* Offset length 0 */ |
| 184 | sandbox_i2c_eeprom_set_offset_len(eeprom, 0); |
| 185 | ut_assertok(i2c_set_chip_offset_len(dev, 0)); |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 186 | ut_assertok(dm_i2c_write(dev, 10 /* ignored */, (uint8_t *)"AB", 2)); |
| 187 | ut_assertok(dm_i2c_read(dev, 0, buf, 5)); |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 188 | ut_assertok(memcmp(buf, "AB\0\0\0\0", sizeof(buf))); |
| 189 | |
| 190 | /* Offset length 1 */ |
| 191 | sandbox_i2c_eeprom_set_offset_len(eeprom, 1); |
| 192 | ut_assertok(i2c_set_chip_offset_len(dev, 1)); |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 193 | ut_assertok(dm_i2c_write(dev, 2, (uint8_t *)"AB", 2)); |
| 194 | ut_assertok(dm_i2c_read(dev, 0, buf, 5)); |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 195 | ut_assertok(memcmp(buf, "ABAB\0", sizeof(buf))); |
| 196 | |
| 197 | /* Offset length 2 */ |
| 198 | sandbox_i2c_eeprom_set_offset_len(eeprom, 2); |
| 199 | ut_assertok(i2c_set_chip_offset_len(dev, 2)); |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 200 | ut_assertok(dm_i2c_write(dev, 0x210, (uint8_t *)"AB", 2)); |
| 201 | ut_assertok(dm_i2c_read(dev, 0x210, buf, 5)); |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 202 | ut_assertok(memcmp(buf, "AB\0\0\0", sizeof(buf))); |
| 203 | |
| 204 | /* Offset length 3 */ |
| 205 | sandbox_i2c_eeprom_set_offset_len(eeprom, 2); |
| 206 | ut_assertok(i2c_set_chip_offset_len(dev, 2)); |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 207 | ut_assertok(dm_i2c_write(dev, 0x410, (uint8_t *)"AB", 2)); |
| 208 | ut_assertok(dm_i2c_read(dev, 0x410, buf, 5)); |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 209 | ut_assertok(memcmp(buf, "AB\0\0\0", sizeof(buf))); |
| 210 | |
| 211 | /* Offset length 4 */ |
| 212 | sandbox_i2c_eeprom_set_offset_len(eeprom, 2); |
| 213 | ut_assertok(i2c_set_chip_offset_len(dev, 2)); |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 214 | ut_assertok(dm_i2c_write(dev, 0x420, (uint8_t *)"AB", 2)); |
| 215 | ut_assertok(dm_i2c_read(dev, 0x420, buf, 5)); |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 216 | ut_assertok(memcmp(buf, "AB\0\0\0", sizeof(buf))); |
| 217 | |
| 218 | /* Restore defaults */ |
| 219 | sandbox_i2c_eeprom_set_offset_len(eeprom, 1); |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | DM_TEST(dm_test_i2c_offset, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |