blob: 23d612eb81eb77368181891a3ef123d51d88bbcd [file] [log] [blame]
Simon Glassecc2ed52014-12-10 08:55:55 -07001/*
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
9#include <common.h>
10#include <dm.h>
11#include <fdtdec.h>
12#include <i2c.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050013#include <asm/state.h>
14#include <asm/test.h>
Simon Glassecc2ed52014-12-10 08:55:55 -070015#include <dm/device-internal.h>
16#include <dm/test.h>
17#include <dm/uclass-internal.h>
Simon Glassecc2ed52014-12-10 08:55:55 -070018#include <dm/util.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050019#include <test/ut.h>
Simon Glassecc2ed52014-12-10 08:55:55 -070020
21static const int busnum;
22static const int chip = 0x2c;
23
24/* Test that we can find buses and chips */
Joe Hershbergere721b882015-05-20 14:27:27 -050025static int dm_test_i2c_find(struct unit_test_state *uts)
Simon Glassecc2ed52014-12-10 08:55:55 -070026{
27 struct udevice *bus, *dev;
28 const int no_chip = 0x10;
29
30 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_I2C, busnum,
31 false, &bus));
32
33 /*
34 * i2c_post_bind() will bind devices to chip selects. Check this then
35 * remove the emulation and the slave device.
36 */
37 ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
Simon Glassf9a4c2d2015-01-12 18:02:07 -070038 ut_assertok(dm_i2c_probe(bus, chip, 0, &dev));
39 ut_asserteq(-ENODEV, dm_i2c_probe(bus, no_chip, 0, &dev));
Simon Glassecc2ed52014-12-10 08:55:55 -070040 ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_I2C, 1, &bus));
41
42 return 0;
43}
44DM_TEST(dm_test_i2c_find, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
45
Joe Hershbergere721b882015-05-20 14:27:27 -050046static int dm_test_i2c_read_write(struct unit_test_state *uts)
Simon Glassecc2ed52014-12-10 08:55:55 -070047{
48 struct udevice *bus, *dev;
49 uint8_t buf[5];
50
51 ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
Simon Glass25ab4b02015-01-25 08:26:55 -070052 ut_assertok(i2c_get_chip(bus, chip, 1, &dev));
Simon Glassf9a4c2d2015-01-12 18:02:07 -070053 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassecc2ed52014-12-10 08:55:55 -070054 ut_assertok(memcmp(buf, "\0\0\0\0\0", sizeof(buf)));
Simon Glassf9a4c2d2015-01-12 18:02:07 -070055 ut_assertok(dm_i2c_write(dev, 2, (uint8_t *)"AB", 2));
56 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassecc2ed52014-12-10 08:55:55 -070057 ut_assertok(memcmp(buf, "\0\0AB\0", sizeof(buf)));
58
59 return 0;
60}
61DM_TEST(dm_test_i2c_read_write, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
62
Joe Hershbergere721b882015-05-20 14:27:27 -050063static int dm_test_i2c_speed(struct unit_test_state *uts)
Simon Glassecc2ed52014-12-10 08:55:55 -070064{
65 struct udevice *bus, *dev;
66 uint8_t buf[5];
67
68 ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
Simon Glass182bf922015-04-20 12:37:15 -060069
70 /* Use test mode so we create the required errors for invalid speeds */
71 sandbox_i2c_set_test_mode(bus, true);
Simon Glass25ab4b02015-01-25 08:26:55 -070072 ut_assertok(i2c_get_chip(bus, chip, 1, &dev));
Simon Glassca88b9b2015-02-05 21:41:32 -070073 ut_assertok(dm_i2c_set_bus_speed(bus, 100000));
Simon Glassf9a4c2d2015-01-12 18:02:07 -070074 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassca88b9b2015-02-05 21:41:32 -070075 ut_assertok(dm_i2c_set_bus_speed(bus, 400000));
76 ut_asserteq(400000, dm_i2c_get_bus_speed(bus));
Simon Glassf9a4c2d2015-01-12 18:02:07 -070077 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
78 ut_asserteq(-EINVAL, dm_i2c_write(dev, 0, buf, 5));
Simon Glass182bf922015-04-20 12:37:15 -060079 sandbox_i2c_set_test_mode(bus, false);
Simon Glassecc2ed52014-12-10 08:55:55 -070080
81 return 0;
82}
83DM_TEST(dm_test_i2c_speed, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
84
Joe Hershbergere721b882015-05-20 14:27:27 -050085static int dm_test_i2c_offset_len(struct unit_test_state *uts)
Simon Glassecc2ed52014-12-10 08:55:55 -070086{
87 struct udevice *bus, *dev;
88 uint8_t buf[5];
89
90 ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
Simon Glass25ab4b02015-01-25 08:26:55 -070091 ut_assertok(i2c_get_chip(bus, chip, 1, &dev));
Simon Glassecc2ed52014-12-10 08:55:55 -070092 ut_assertok(i2c_set_chip_offset_len(dev, 1));
Simon Glassf9a4c2d2015-01-12 18:02:07 -070093 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassecc2ed52014-12-10 08:55:55 -070094
95 /* This is not supported by the uclass */
96 ut_asserteq(-EINVAL, i2c_set_chip_offset_len(dev, 5));
97
98 return 0;
99}
100DM_TEST(dm_test_i2c_offset_len, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
101
Joe Hershbergere721b882015-05-20 14:27:27 -0500102static int dm_test_i2c_probe_empty(struct unit_test_state *uts)
Simon Glassecc2ed52014-12-10 08:55:55 -0700103{
104 struct udevice *bus, *dev;
105
106 ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
Simon Glass182bf922015-04-20 12:37:15 -0600107
108 /* Use test mode so that this chip address will always probe */
109 sandbox_i2c_set_test_mode(bus, true);
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700110 ut_assertok(dm_i2c_probe(bus, SANDBOX_I2C_TEST_ADDR, 0, &dev));
Simon Glass182bf922015-04-20 12:37:15 -0600111 sandbox_i2c_set_test_mode(bus, false);
Simon Glassecc2ed52014-12-10 08:55:55 -0700112
113 return 0;
114}
115DM_TEST(dm_test_i2c_probe_empty, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
116
Joe Hershbergere721b882015-05-20 14:27:27 -0500117static int dm_test_i2c_bytewise(struct unit_test_state *uts)
Simon Glassecc2ed52014-12-10 08:55:55 -0700118{
119 struct udevice *bus, *dev;
120 struct udevice *eeprom;
121 uint8_t buf[5];
122
123 ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
Simon Glass25ab4b02015-01-25 08:26:55 -0700124 ut_assertok(i2c_get_chip(bus, chip, 1, &dev));
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700125 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassecc2ed52014-12-10 08:55:55 -0700126 ut_assertok(memcmp(buf, "\0\0\0\0\0", sizeof(buf)));
127
128 /* Tell the EEPROM to only read/write one register at a time */
129 ut_assertok(uclass_first_device(UCLASS_I2C_EMUL, &eeprom));
130 ut_assertnonnull(eeprom);
131 sandbox_i2c_eeprom_set_test_mode(eeprom, SIE_TEST_MODE_SINGLE_BYTE);
132
133 /* Now we only get the first byte - the rest will be 0xff */
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700134 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassecc2ed52014-12-10 08:55:55 -0700135 ut_assertok(memcmp(buf, "\0\xff\xff\xff\xff", sizeof(buf)));
136
137 /* If we do a separate transaction for each byte, it works */
138 ut_assertok(i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS));
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700139 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassecc2ed52014-12-10 08:55:55 -0700140 ut_assertok(memcmp(buf, "\0\0\0\0\0", sizeof(buf)));
141
142 /* This will only write A */
143 ut_assertok(i2c_set_chip_flags(dev, 0));
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700144 ut_assertok(dm_i2c_write(dev, 2, (uint8_t *)"AB", 2));
145 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassecc2ed52014-12-10 08:55:55 -0700146 ut_assertok(memcmp(buf, "\0\xff\xff\xff\xff", sizeof(buf)));
147
148 /* Check that the B was ignored */
149 ut_assertok(i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS));
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700150 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassecc2ed52014-12-10 08:55:55 -0700151 ut_assertok(memcmp(buf, "\0\0A\0\0\0", sizeof(buf)));
152
153 /* Now write it again with the new flags, it should work */
154 ut_assertok(i2c_set_chip_flags(dev, DM_I2C_CHIP_WR_ADDRESS));
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700155 ut_assertok(dm_i2c_write(dev, 2, (uint8_t *)"AB", 2));
156 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassecc2ed52014-12-10 08:55:55 -0700157 ut_assertok(memcmp(buf, "\0\xff\xff\xff\xff", sizeof(buf)));
158
159 ut_assertok(i2c_set_chip_flags(dev, DM_I2C_CHIP_WR_ADDRESS |
160 DM_I2C_CHIP_RD_ADDRESS));
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700161 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassecc2ed52014-12-10 08:55:55 -0700162 ut_assertok(memcmp(buf, "\0\0AB\0\0", sizeof(buf)));
163
164 /* Restore defaults */
165 sandbox_i2c_eeprom_set_test_mode(eeprom, SIE_TEST_MODE_NONE);
166 ut_assertok(i2c_set_chip_flags(dev, 0));
167
168 return 0;
169}
170DM_TEST(dm_test_i2c_bytewise, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
171
Joe Hershbergere721b882015-05-20 14:27:27 -0500172static int dm_test_i2c_offset(struct unit_test_state *uts)
Simon Glassecc2ed52014-12-10 08:55:55 -0700173{
174 struct udevice *eeprom;
175 struct udevice *dev;
176 uint8_t buf[5];
177
Simon Glass25ab4b02015-01-25 08:26:55 -0700178 ut_assertok(i2c_get_chip_for_busnum(busnum, chip, 1, &dev));
Simon Glassecc2ed52014-12-10 08:55:55 -0700179
180 /* Do a transfer so we can find the emulator */
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700181 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassecc2ed52014-12-10 08:55:55 -0700182 ut_assertok(uclass_first_device(UCLASS_I2C_EMUL, &eeprom));
183
184 /* Offset length 0 */
185 sandbox_i2c_eeprom_set_offset_len(eeprom, 0);
186 ut_assertok(i2c_set_chip_offset_len(dev, 0));
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700187 ut_assertok(dm_i2c_write(dev, 10 /* ignored */, (uint8_t *)"AB", 2));
188 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassecc2ed52014-12-10 08:55:55 -0700189 ut_assertok(memcmp(buf, "AB\0\0\0\0", sizeof(buf)));
190
191 /* Offset length 1 */
192 sandbox_i2c_eeprom_set_offset_len(eeprom, 1);
193 ut_assertok(i2c_set_chip_offset_len(dev, 1));
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700194 ut_assertok(dm_i2c_write(dev, 2, (uint8_t *)"AB", 2));
195 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassecc2ed52014-12-10 08:55:55 -0700196 ut_assertok(memcmp(buf, "ABAB\0", sizeof(buf)));
197
198 /* Offset length 2 */
199 sandbox_i2c_eeprom_set_offset_len(eeprom, 2);
200 ut_assertok(i2c_set_chip_offset_len(dev, 2));
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700201 ut_assertok(dm_i2c_write(dev, 0x210, (uint8_t *)"AB", 2));
202 ut_assertok(dm_i2c_read(dev, 0x210, buf, 5));
Simon Glassecc2ed52014-12-10 08:55:55 -0700203 ut_assertok(memcmp(buf, "AB\0\0\0", sizeof(buf)));
204
205 /* Offset length 3 */
206 sandbox_i2c_eeprom_set_offset_len(eeprom, 2);
207 ut_assertok(i2c_set_chip_offset_len(dev, 2));
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700208 ut_assertok(dm_i2c_write(dev, 0x410, (uint8_t *)"AB", 2));
209 ut_assertok(dm_i2c_read(dev, 0x410, buf, 5));
Simon Glassecc2ed52014-12-10 08:55:55 -0700210 ut_assertok(memcmp(buf, "AB\0\0\0", sizeof(buf)));
211
212 /* Offset length 4 */
213 sandbox_i2c_eeprom_set_offset_len(eeprom, 2);
214 ut_assertok(i2c_set_chip_offset_len(dev, 2));
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700215 ut_assertok(dm_i2c_write(dev, 0x420, (uint8_t *)"AB", 2));
216 ut_assertok(dm_i2c_read(dev, 0x420, buf, 5));
Simon Glassecc2ed52014-12-10 08:55:55 -0700217 ut_assertok(memcmp(buf, "AB\0\0\0", sizeof(buf)));
218
219 /* Restore defaults */
220 sandbox_i2c_eeprom_set_offset_len(eeprom, 1);
221
222 return 0;
223}
224DM_TEST(dm_test_i2c_offset, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);