blob: d74f5f9fbc7c70bab4423ba1aaf6c6c6ec366a62 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass7df766e2014-12-10 08:55:55 -07002/*
3 * Copyright (C) 2013 Google, Inc
4 *
Simon Glass7df766e2014-12-10 08:55:55 -07005 * 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 Hershberger3a77be52015-05-20 14:27:27 -050012#include <asm/state.h>
13#include <asm/test.h>
Simon Glass7df766e2014-12-10 08:55:55 -070014#include <dm/device-internal.h>
15#include <dm/test.h>
16#include <dm/uclass-internal.h>
Simon Glass7df766e2014-12-10 08:55:55 -070017#include <dm/util.h>
Robert Beckett1fe8a492019-10-28 17:44:58 +000018#include <hexdump.h>
Simon Glass75c4d412020-07-19 10:15:37 -060019#include <test/test.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050020#include <test/ut.h>
Simon Glass7df766e2014-12-10 08:55:55 -070021
22static const int busnum;
23static const int chip = 0x2c;
24
25/* Test that we can find buses and chips */
Joe Hershberger3a77be52015-05-20 14:27:27 -050026static int dm_test_i2c_find(struct unit_test_state *uts)
Simon Glass7df766e2014-12-10 08:55:55 -070027{
28 struct udevice *bus, *dev;
29 const int no_chip = 0x10;
30
Simon Glass7df766e2014-12-10 08:55:55 -070031 /*
Simon Glass18230342016-07-05 17:10:10 -060032 * The post_bind() method will bind devices to chip selects. Check
33 * this then remove the emulation and the slave device.
Simon Glass7df766e2014-12-10 08:55:55 -070034 */
35 ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
Simon Glass7d722762015-01-12 18:02:07 -070036 ut_assertok(dm_i2c_probe(bus, chip, 0, &dev));
Simon Glass17b56f62018-11-18 08:14:34 -070037 ut_asserteq(-ENOENT, dm_i2c_probe(bus, no_chip, 0, &dev));
Simon Glass7df766e2014-12-10 08:55:55 -070038 ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_I2C, 1, &bus));
39
40 return 0;
41}
Simon Glass974dccd2020-07-28 19:41:12 -060042DM_TEST(dm_test_i2c_find, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass7df766e2014-12-10 08:55:55 -070043
Joe Hershberger3a77be52015-05-20 14:27:27 -050044static int dm_test_i2c_read_write(struct unit_test_state *uts)
Simon Glass7df766e2014-12-10 08:55:55 -070045{
46 struct udevice *bus, *dev;
47 uint8_t buf[5];
48
49 ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
Simon Glassa2723ae2015-01-25 08:26:55 -070050 ut_assertok(i2c_get_chip(bus, chip, 1, &dev));
Simon Glass7d722762015-01-12 18:02:07 -070051 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassa3186e62020-05-10 12:52:45 -060052 ut_asserteq_mem(buf, "\0\0\0\0\0", sizeof(buf));
Simon Glass7d722762015-01-12 18:02:07 -070053 ut_assertok(dm_i2c_write(dev, 2, (uint8_t *)"AB", 2));
54 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassa3186e62020-05-10 12:52:45 -060055 ut_asserteq_mem(buf, "\0\0AB\0", sizeof(buf));
Simon Glass7df766e2014-12-10 08:55:55 -070056
57 return 0;
58}
Simon Glass974dccd2020-07-28 19:41:12 -060059DM_TEST(dm_test_i2c_read_write, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass7df766e2014-12-10 08:55:55 -070060
Joe Hershberger3a77be52015-05-20 14:27:27 -050061static int dm_test_i2c_speed(struct unit_test_state *uts)
Simon Glass7df766e2014-12-10 08:55:55 -070062{
63 struct udevice *bus, *dev;
64 uint8_t buf[5];
65
66 ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
Simon Glass4c70ed92015-04-20 12:37:15 -060067
68 /* Use test mode so we create the required errors for invalid speeds */
69 sandbox_i2c_set_test_mode(bus, true);
Simon Glassa2723ae2015-01-25 08:26:55 -070070 ut_assertok(i2c_get_chip(bus, chip, 1, &dev));
Simon Glasse4e8ff22015-02-05 21:41:32 -070071 ut_assertok(dm_i2c_set_bus_speed(bus, 100000));
Simon Glass7d722762015-01-12 18:02:07 -070072 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glasse4e8ff22015-02-05 21:41:32 -070073 ut_assertok(dm_i2c_set_bus_speed(bus, 400000));
74 ut_asserteq(400000, dm_i2c_get_bus_speed(bus));
Simon Glass7d722762015-01-12 18:02:07 -070075 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
76 ut_asserteq(-EINVAL, dm_i2c_write(dev, 0, buf, 5));
Simon Glass4c70ed92015-04-20 12:37:15 -060077 sandbox_i2c_set_test_mode(bus, false);
Simon Glass7df766e2014-12-10 08:55:55 -070078
79 return 0;
80}
Simon Glass974dccd2020-07-28 19:41:12 -060081DM_TEST(dm_test_i2c_speed, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass7df766e2014-12-10 08:55:55 -070082
Joe Hershberger3a77be52015-05-20 14:27:27 -050083static int dm_test_i2c_offset_len(struct unit_test_state *uts)
Simon Glass7df766e2014-12-10 08:55:55 -070084{
85 struct udevice *bus, *dev;
86 uint8_t buf[5];
87
88 ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
Simon Glassa2723ae2015-01-25 08:26:55 -070089 ut_assertok(i2c_get_chip(bus, chip, 1, &dev));
Simon Glass7df766e2014-12-10 08:55:55 -070090 ut_assertok(i2c_set_chip_offset_len(dev, 1));
Simon Glass7d722762015-01-12 18:02:07 -070091 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glass7df766e2014-12-10 08:55:55 -070092
93 /* This is not supported by the uclass */
94 ut_asserteq(-EINVAL, i2c_set_chip_offset_len(dev, 5));
95
96 return 0;
97}
Simon Glass974dccd2020-07-28 19:41:12 -060098DM_TEST(dm_test_i2c_offset_len, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass7df766e2014-12-10 08:55:55 -070099
Joe Hershberger3a77be52015-05-20 14:27:27 -0500100static int dm_test_i2c_probe_empty(struct unit_test_state *uts)
Simon Glass7df766e2014-12-10 08:55:55 -0700101{
102 struct udevice *bus, *dev;
103
104 ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
Simon Glass4c70ed92015-04-20 12:37:15 -0600105
106 /* Use test mode so that this chip address will always probe */
107 sandbox_i2c_set_test_mode(bus, true);
Simon Glass7d722762015-01-12 18:02:07 -0700108 ut_assertok(dm_i2c_probe(bus, SANDBOX_I2C_TEST_ADDR, 0, &dev));
Simon Glass4c70ed92015-04-20 12:37:15 -0600109 sandbox_i2c_set_test_mode(bus, false);
Simon Glass7df766e2014-12-10 08:55:55 -0700110
111 return 0;
112}
Simon Glass974dccd2020-07-28 19:41:12 -0600113DM_TEST(dm_test_i2c_probe_empty, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass7df766e2014-12-10 08:55:55 -0700114
Joe Hershberger3a77be52015-05-20 14:27:27 -0500115static int dm_test_i2c_bytewise(struct unit_test_state *uts)
Simon Glass7df766e2014-12-10 08:55:55 -0700116{
117 struct udevice *bus, *dev;
118 struct udevice *eeprom;
119 uint8_t buf[5];
120
121 ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
Simon Glassa2723ae2015-01-25 08:26:55 -0700122 ut_assertok(i2c_get_chip(bus, chip, 1, &dev));
Simon Glass7d722762015-01-12 18:02:07 -0700123 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassa3186e62020-05-10 12:52:45 -0600124 ut_asserteq_mem(buf, "\0\0\0\0\0", sizeof(buf));
Simon Glass7df766e2014-12-10 08:55:55 -0700125
126 /* Tell the EEPROM to only read/write one register at a time */
127 ut_assertok(uclass_first_device(UCLASS_I2C_EMUL, &eeprom));
128 ut_assertnonnull(eeprom);
129 sandbox_i2c_eeprom_set_test_mode(eeprom, SIE_TEST_MODE_SINGLE_BYTE);
130
131 /* Now we only get the first byte - the rest will be 0xff */
Simon Glass7d722762015-01-12 18:02:07 -0700132 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassa3186e62020-05-10 12:52:45 -0600133 ut_asserteq_mem(buf, "\0\xff\xff\xff\xff", sizeof(buf));
Simon Glass7df766e2014-12-10 08:55:55 -0700134
135 /* If we do a separate transaction for each byte, it works */
136 ut_assertok(i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS));
Simon Glass7d722762015-01-12 18:02:07 -0700137 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassa3186e62020-05-10 12:52:45 -0600138 ut_asserteq_mem(buf, "\0\0\0\0\0", sizeof(buf));
Simon Glass7df766e2014-12-10 08:55:55 -0700139
140 /* This will only write A */
141 ut_assertok(i2c_set_chip_flags(dev, 0));
Simon Glass7d722762015-01-12 18:02:07 -0700142 ut_assertok(dm_i2c_write(dev, 2, (uint8_t *)"AB", 2));
143 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassa3186e62020-05-10 12:52:45 -0600144 ut_asserteq_mem(buf, "\0\xff\xff\xff\xff", sizeof(buf));
Simon Glass7df766e2014-12-10 08:55:55 -0700145
146 /* Check that the B was ignored */
147 ut_assertok(i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS));
Simon Glass7d722762015-01-12 18:02:07 -0700148 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassa3186e62020-05-10 12:52:45 -0600149 ut_asserteq_mem(buf, "\0\0A\0\0\0", sizeof(buf));
Simon Glass7df766e2014-12-10 08:55:55 -0700150
151 /* Now write it again with the new flags, it should work */
152 ut_assertok(i2c_set_chip_flags(dev, DM_I2C_CHIP_WR_ADDRESS));
Simon Glass7d722762015-01-12 18:02:07 -0700153 ut_assertok(dm_i2c_write(dev, 2, (uint8_t *)"AB", 2));
154 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassa3186e62020-05-10 12:52:45 -0600155 ut_asserteq_mem(buf, "\0\xff\xff\xff\xff", sizeof(buf));
Simon Glass7df766e2014-12-10 08:55:55 -0700156
157 ut_assertok(i2c_set_chip_flags(dev, DM_I2C_CHIP_WR_ADDRESS |
158 DM_I2C_CHIP_RD_ADDRESS));
Simon Glass7d722762015-01-12 18:02:07 -0700159 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassa3186e62020-05-10 12:52:45 -0600160 ut_asserteq_mem(buf, "\0\0AB\0\0", sizeof(buf));
Simon Glass7df766e2014-12-10 08:55:55 -0700161
162 /* Restore defaults */
163 sandbox_i2c_eeprom_set_test_mode(eeprom, SIE_TEST_MODE_NONE);
164 ut_assertok(i2c_set_chip_flags(dev, 0));
165
166 return 0;
167}
Simon Glass974dccd2020-07-28 19:41:12 -0600168DM_TEST(dm_test_i2c_bytewise, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass7df766e2014-12-10 08:55:55 -0700169
Joe Hershberger3a77be52015-05-20 14:27:27 -0500170static int dm_test_i2c_offset(struct unit_test_state *uts)
Simon Glass7df766e2014-12-10 08:55:55 -0700171{
172 struct udevice *eeprom;
173 struct udevice *dev;
174 uint8_t buf[5];
175
Simon Glassa2723ae2015-01-25 08:26:55 -0700176 ut_assertok(i2c_get_chip_for_busnum(busnum, chip, 1, &dev));
Simon Glass7df766e2014-12-10 08:55:55 -0700177
178 /* Do a transfer so we can find the emulator */
Simon Glass7d722762015-01-12 18:02:07 -0700179 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glass7df766e2014-12-10 08:55:55 -0700180 ut_assertok(uclass_first_device(UCLASS_I2C_EMUL, &eeprom));
181
182 /* Offset length 0 */
183 sandbox_i2c_eeprom_set_offset_len(eeprom, 0);
184 ut_assertok(i2c_set_chip_offset_len(dev, 0));
Simon Glass7d722762015-01-12 18:02:07 -0700185 ut_assertok(dm_i2c_write(dev, 10 /* ignored */, (uint8_t *)"AB", 2));
186 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Robert Beckett1fe8a492019-10-28 17:44:58 +0000187 ut_asserteq_mem("AB\0\0\0\0", buf, sizeof(buf));
188 ut_asserteq(0, sanbox_i2c_eeprom_get_prev_offset(eeprom));
Simon Glass7df766e2014-12-10 08:55:55 -0700189
190 /* Offset length 1 */
191 sandbox_i2c_eeprom_set_offset_len(eeprom, 1);
192 ut_assertok(i2c_set_chip_offset_len(dev, 1));
Simon Glass7d722762015-01-12 18:02:07 -0700193 ut_assertok(dm_i2c_write(dev, 2, (uint8_t *)"AB", 2));
Robert Beckett1fe8a492019-10-28 17:44:58 +0000194 ut_asserteq(2, sanbox_i2c_eeprom_get_prev_offset(eeprom));
Simon Glass7d722762015-01-12 18:02:07 -0700195 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Robert Beckett1fe8a492019-10-28 17:44:58 +0000196 ut_asserteq_mem("ABAB\0", buf, sizeof(buf));
197 ut_asserteq(0, sanbox_i2c_eeprom_get_prev_offset(eeprom));
Simon Glass7df766e2014-12-10 08:55:55 -0700198
Robert Beckett1fe8a492019-10-28 17:44:58 +0000199 /* Offset length 2 boundary - check model wrapping */
Simon Glass7df766e2014-12-10 08:55:55 -0700200 sandbox_i2c_eeprom_set_offset_len(eeprom, 2);
201 ut_assertok(i2c_set_chip_offset_len(dev, 2));
Robert Beckett1fe8a492019-10-28 17:44:58 +0000202 ut_assertok(dm_i2c_write(dev, 0xFF, (uint8_t *)"A", 1));
203 ut_asserteq(0xFF, sanbox_i2c_eeprom_get_prev_offset(eeprom));
204 ut_assertok(dm_i2c_write(dev, 0x100, (uint8_t *)"B", 1));
205 ut_asserteq(0x100, sanbox_i2c_eeprom_get_prev_offset(eeprom));
206 ut_assertok(dm_i2c_write(dev, 0x101, (uint8_t *)"C", 1));
207 ut_asserteq(0x101, sanbox_i2c_eeprom_get_prev_offset(eeprom));
208 ut_assertok(dm_i2c_read(dev, 0xFF, buf, 5));
209 ut_asserteq_mem("ABCAB", buf, sizeof(buf));
210 ut_asserteq(0xFF, sanbox_i2c_eeprom_get_prev_offset(eeprom));
Simon Glass7df766e2014-12-10 08:55:55 -0700211
Robert Beckett1fe8a492019-10-28 17:44:58 +0000212 /* Offset length 2 */
Simon Glass7df766e2014-12-10 08:55:55 -0700213 sandbox_i2c_eeprom_set_offset_len(eeprom, 2);
214 ut_assertok(i2c_set_chip_offset_len(dev, 2));
Robert Beckett1fe8a492019-10-28 17:44:58 +0000215 ut_assertok(dm_i2c_write(dev, 0x2020, (uint8_t *)"AB", 2));
216 ut_assertok(dm_i2c_read(dev, 0x2020, buf, 5));
217 ut_asserteq_mem("AB\0\0\0", buf, sizeof(buf));
218 ut_asserteq(0x2020, sanbox_i2c_eeprom_get_prev_offset(eeprom));
219
220 /* Offset length 3 */
221 sandbox_i2c_eeprom_set_offset_len(eeprom, 3);
222 ut_assertok(i2c_set_chip_offset_len(dev, 3));
223 ut_assertok(dm_i2c_write(dev, 0x303030, (uint8_t *)"AB", 2));
224 ut_assertok(dm_i2c_read(dev, 0x303030, buf, 5));
225 ut_asserteq_mem("AB\0\0\0", buf, sizeof(buf));
226 ut_asserteq(0x303030, sanbox_i2c_eeprom_get_prev_offset(eeprom));
Simon Glass7df766e2014-12-10 08:55:55 -0700227
228 /* Offset length 4 */
Robert Beckett1fe8a492019-10-28 17:44:58 +0000229 sandbox_i2c_eeprom_set_offset_len(eeprom, 4);
230 ut_assertok(i2c_set_chip_offset_len(dev, 4));
231 ut_assertok(dm_i2c_write(dev, 0x40404040, (uint8_t *)"AB", 2));
232 ut_assertok(dm_i2c_read(dev, 0x40404040, buf, 5));
233 ut_asserteq_mem("AB\0\0\0", buf, sizeof(buf));
234 ut_asserteq(0x40404040, sanbox_i2c_eeprom_get_prev_offset(eeprom));
Simon Glass7df766e2014-12-10 08:55:55 -0700235
236 /* Restore defaults */
237 sandbox_i2c_eeprom_set_offset_len(eeprom, 1);
238
239 return 0;
240}
Simon Glass974dccd2020-07-28 19:41:12 -0600241DM_TEST(dm_test_i2c_offset, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Robert Beckettf695f6e2019-10-28 17:44:59 +0000242
243static int dm_test_i2c_addr_offset(struct unit_test_state *uts)
244{
245 struct udevice *eeprom;
246 struct udevice *dev;
247 u8 buf[5];
248
249 ut_assertok(i2c_get_chip_for_busnum(busnum, chip, 1, &dev));
250
251 /* Do a transfer so we can find the emulator */
252 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
253 ut_assertok(uclass_first_device(UCLASS_I2C_EMUL, &eeprom));
254
255 /* Offset length 0 */
256 sandbox_i2c_eeprom_set_offset_len(eeprom, 0);
257 sandbox_i2c_eeprom_set_chip_addr_offset_mask(eeprom, 0x3);
258 ut_assertok(i2c_set_chip_offset_len(dev, 0));
259 ut_assertok(i2c_set_chip_addr_offset_mask(dev, 0x3));
260 ut_assertok(dm_i2c_write(dev, 0x3, (uint8_t *)"AB", 2));
261 ut_assertok(dm_i2c_read(dev, 0x3, buf, 5));
262 ut_asserteq_mem("AB\0\0\0\0", buf, sizeof(buf));
263 ut_asserteq(0x3, sanbox_i2c_eeprom_get_prev_offset(eeprom));
264 ut_asserteq(chip | 0x3, sanbox_i2c_eeprom_get_prev_addr(eeprom));
265
266 /* Offset length 1 */
267 sandbox_i2c_eeprom_set_offset_len(eeprom, 1);
268 sandbox_i2c_eeprom_set_chip_addr_offset_mask(eeprom, 0x3);
269 ut_assertok(i2c_set_chip_offset_len(dev, 1));
270 ut_assertok(i2c_set_chip_addr_offset_mask(dev, 0x3));
271 ut_assertok(dm_i2c_write(dev, 0x310, (uint8_t *)"AB", 2));
272 ut_assertok(dm_i2c_read(dev, 0x310, buf, 5));
273 ut_asserteq_mem("AB\0\0\0\0", buf, sizeof(buf));
274 ut_asserteq(0x310, sanbox_i2c_eeprom_get_prev_offset(eeprom));
275 ut_asserteq(chip | 0x3, sanbox_i2c_eeprom_get_prev_addr(eeprom));
276
277 /* Offset length 2 */
278 sandbox_i2c_eeprom_set_offset_len(eeprom, 2);
279 sandbox_i2c_eeprom_set_chip_addr_offset_mask(eeprom, 0x3);
280 ut_assertok(i2c_set_chip_offset_len(dev, 2));
281 ut_assertok(i2c_set_chip_addr_offset_mask(dev, 0x3));
282 ut_assertok(dm_i2c_write(dev, 0x32020, (uint8_t *)"AB", 2));
283 ut_assertok(dm_i2c_read(dev, 0x32020, buf, 5));
284 ut_asserteq_mem("AB\0\0\0\0", buf, sizeof(buf));
285 ut_asserteq(0x32020, sanbox_i2c_eeprom_get_prev_offset(eeprom));
286 ut_asserteq(chip | 0x3, sanbox_i2c_eeprom_get_prev_addr(eeprom));
287
288 /* Offset length 3 */
289 sandbox_i2c_eeprom_set_offset_len(eeprom, 3);
290 sandbox_i2c_eeprom_set_chip_addr_offset_mask(eeprom, 0x3);
291 ut_assertok(i2c_set_chip_offset_len(dev, 3));
292 ut_assertok(i2c_set_chip_addr_offset_mask(dev, 0x3));
293 ut_assertok(dm_i2c_write(dev, 0x3303030, (uint8_t *)"AB", 2));
294 ut_assertok(dm_i2c_read(dev, 0x3303030, buf, 5));
295 ut_asserteq_mem("AB\0\0\0\0", buf, sizeof(buf));
296 ut_asserteq(0x3303030, sanbox_i2c_eeprom_get_prev_offset(eeprom));
297 ut_asserteq(chip | 0x3, sanbox_i2c_eeprom_get_prev_addr(eeprom));
298
299 /* Restore defaults */
300 sandbox_i2c_eeprom_set_offset_len(eeprom, 1);
301 sandbox_i2c_eeprom_set_chip_addr_offset_mask(eeprom, 0);
302
303 return 0;
304}
305
Simon Glass974dccd2020-07-28 19:41:12 -0600306DM_TEST(dm_test_i2c_addr_offset, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);