xref: /openbmc/qemu/tests/qtest/max34451-test.c (revision a6caeee8)
1 /*
2  * QTests for the MAX34451 device
3  *
4  * Copyright 2021 Google LLC
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #include "qemu/osdep.h"
10 #include "hw/i2c/pmbus_device.h"
11 #include "libqtest-single.h"
12 #include "libqos/qgraph.h"
13 #include "libqos/i2c.h"
14 #include "qapi/qmp/qdict.h"
15 #include "qapi/qmp/qnum.h"
16 #include "qemu/bitops.h"
17 
18 #define TEST_ID "max34451-test"
19 #define TEST_ADDR (0x4e)
20 
21 #define MAX34451_MFR_VOUT_PEAK          0xD4
22 #define MAX34451_MFR_IOUT_PEAK          0xD5
23 #define MAX34451_MFR_TEMPERATURE_PEAK   0xD6
24 #define MAX34451_MFR_VOUT_MIN           0xD7
25 
26 #define DEFAULT_VOUT                    0
27 #define DEFAULT_UV_LIMIT                0
28 #define DEFAULT_TEMPERATURE             2500
29 #define DEFAULT_SCALE                   0x7FFF
30 #define DEFAULT_OV_LIMIT                0x7FFF
31 #define DEFAULT_OC_LIMIT                0x7FFF
32 #define DEFAULT_OT_LIMIT                0x7FFF
33 #define DEFAULT_VMIN                    0x7FFF
34 #define DEFAULT_TON_FAULT_LIMIT         0xFFFF
35 #define DEFAULT_CHANNEL_CONFIG          0x20
36 #define DEFAULT_TEXT                    0x20
37 
38 #define MAX34451_NUM_PWR_DEVICES        16
39 #define MAX34451_NUM_TEMP_DEVICES       5
40 
41 
42 static uint16_t qmp_max34451_get(const char *id, const char *property)
43 {
44     QDict *response;
45     uint16_t ret;
46     response = qmp("{ 'execute': 'qom-get', 'arguments': { 'path': %s, "
47                    "'property': %s } }", id, property);
48     g_assert(qdict_haskey(response, "return"));
49     ret = qnum_get_uint(qobject_to(QNum, qdict_get(response, "return")));
50     qobject_unref(response);
51     return ret;
52 }
53 
54 static void qmp_max34451_set(const char *id,
55                              const char *property,
56                              uint16_t value)
57 {
58     QDict *response;
59 
60     response = qmp("{ 'execute': 'qom-set', 'arguments': { 'path': %s, "
61                    "'property': %s, 'value': %u } }",
62                    id, property, value);
63     g_assert(qdict_haskey(response, "return"));
64     qobject_unref(response);
65 }
66 
67 /* PMBus commands are little endian vs i2c_set16 in i2c.h which is big endian */
68 static uint16_t max34451_i2c_get16(QI2CDevice *i2cdev, uint8_t reg)
69 {
70     uint8_t resp[2];
71     i2c_read_block(i2cdev, reg, resp, sizeof(resp));
72     return (resp[1] << 8) | resp[0];
73 }
74 
75 /* PMBus commands are little endian vs i2c_set16 in i2c.h which is big endian */
76 static void max34451_i2c_set16(QI2CDevice *i2cdev, uint8_t reg, uint16_t value)
77 {
78     uint8_t data[2];
79 
80     data[0] = value & 255;
81     data[1] = value >> 8;
82     i2c_write_block(i2cdev, reg, data, sizeof(data));
83 }
84 
85 /* Test default values */
86 static void test_defaults(void *obj, void *data, QGuestAllocator *alloc)
87 {
88     uint16_t value, i2c_value;
89     QI2CDevice *i2cdev = (QI2CDevice *)obj;
90     char *path;
91 
92     /* Default temperatures and temperature fault limits */
93     for (int i = 0; i < MAX34451_NUM_TEMP_DEVICES; i++) {
94         path = g_strdup_printf("temperature[%d]", i);
95         value = qmp_max34451_get(TEST_ID, path);
96         g_assert_cmpuint(value, ==, DEFAULT_TEMPERATURE);
97         g_free(path);
98 
99         /* Temperature sensors start on page 16 */
100         i2c_set8(i2cdev, PMBUS_PAGE, i + 16);
101         i2c_value = max34451_i2c_get16(i2cdev, PMBUS_READ_TEMPERATURE_1);
102         g_assert_cmpuint(i2c_value, ==, DEFAULT_TEMPERATURE);
103 
104         i2c_value = max34451_i2c_get16(i2cdev, PMBUS_OT_FAULT_LIMIT);
105         g_assert_cmpuint(i2c_value, ==, DEFAULT_OT_LIMIT);
106 
107         i2c_value = max34451_i2c_get16(i2cdev, PMBUS_OT_WARN_LIMIT);
108         g_assert_cmpuint(i2c_value, ==, DEFAULT_OT_LIMIT);
109     }
110 
111     /* Default voltages and fault limits */
112     for (int i = 0; i < MAX34451_NUM_PWR_DEVICES; i++) {
113         path = g_strdup_printf("vout[%d]", i);
114         value = qmp_max34451_get(TEST_ID, path);
115         g_assert_cmpuint(value, ==, DEFAULT_VOUT);
116         g_free(path);
117 
118         i2c_set8(i2cdev, PMBUS_PAGE, i);
119         i2c_value = max34451_i2c_get16(i2cdev, PMBUS_READ_VOUT);
120         g_assert_cmpuint(i2c_value, ==, DEFAULT_VOUT);
121 
122         i2c_value = max34451_i2c_get16(i2cdev, PMBUS_VOUT_OV_FAULT_LIMIT);
123         g_assert_cmpuint(i2c_value, ==, DEFAULT_OV_LIMIT);
124 
125         i2c_value = max34451_i2c_get16(i2cdev, PMBUS_VOUT_OV_WARN_LIMIT);
126         g_assert_cmpuint(i2c_value, ==, DEFAULT_OV_LIMIT);
127 
128         i2c_value = max34451_i2c_get16(i2cdev, PMBUS_VOUT_UV_WARN_LIMIT);
129         g_assert_cmpuint(i2c_value, ==, DEFAULT_UV_LIMIT);
130 
131         i2c_value = max34451_i2c_get16(i2cdev, PMBUS_VOUT_UV_FAULT_LIMIT);
132         g_assert_cmpuint(i2c_value, ==, DEFAULT_UV_LIMIT);
133 
134         i2c_value = max34451_i2c_get16(i2cdev, MAX34451_MFR_VOUT_MIN);
135         g_assert_cmpuint(i2c_value, ==, DEFAULT_VMIN);
136     }
137 
138     i2c_value = i2c_get8(i2cdev, PMBUS_VOUT_MODE);
139     g_assert_cmphex(i2c_value, ==, 0x40); /* DIRECT mode */
140 
141     i2c_value = i2c_get8(i2cdev, PMBUS_REVISION);
142     g_assert_cmphex(i2c_value, ==, 0x11); /* Rev 1.1 */
143 }
144 
145 /* Test setting temperature */
146 static void test_temperature(void *obj, void *data, QGuestAllocator *alloc)
147 {
148     uint16_t value, i2c_value;
149     QI2CDevice *i2cdev = (QI2CDevice *)obj;
150     char *path;
151 
152     for (int i = 0; i < MAX34451_NUM_TEMP_DEVICES; i++) {
153         path = g_strdup_printf("temperature[%d]", i);
154         qmp_max34451_set(TEST_ID, path, 0xBE00 + i);
155         value = qmp_max34451_get(TEST_ID, path);
156         g_assert_cmphex(value, ==, 0xBE00 + i);
157         g_free(path);
158     }
159 
160     /* compare qmp read with i2c read separately */
161     for (int i = 0; i < MAX34451_NUM_TEMP_DEVICES; i++) {
162         /* temperature[0] is on page 16 */
163         i2c_set8(i2cdev, PMBUS_PAGE, i + 16);
164         i2c_value = max34451_i2c_get16(i2cdev, PMBUS_READ_TEMPERATURE_1);
165         g_assert_cmphex(i2c_value, ==, 0xBE00 + i);
166 
167         i2c_value = max34451_i2c_get16(i2cdev, MAX34451_MFR_TEMPERATURE_PEAK);
168         g_assert_cmphex(i2c_value, ==, 0xBE00 + i);
169     }
170 }
171 
172 /* Test setting voltage */
173 static void test_voltage(void *obj, void *data, QGuestAllocator *alloc)
174 {
175     uint16_t value, i2c_value;
176     QI2CDevice *i2cdev = (QI2CDevice *)obj;
177     char *path;
178 
179     for (int i = 0; i < MAX34451_NUM_PWR_DEVICES; i++) {
180         path = g_strdup_printf("vout[%d]", i);
181         qmp_max34451_set(TEST_ID, path, 3000 + i);
182         value = qmp_max34451_get(TEST_ID, path);
183         g_assert_cmpuint(value, ==, 3000 + i);
184         g_free(path);
185     }
186 
187     /* compare qmp read with i2c read separately */
188     for (int i = 0; i < MAX34451_NUM_PWR_DEVICES; i++) {
189         i2c_set8(i2cdev, PMBUS_PAGE, i);
190         i2c_value = max34451_i2c_get16(i2cdev, PMBUS_READ_VOUT);
191         g_assert_cmpuint(i2c_value, ==, 3000 + i);
192 
193         i2c_value = max34451_i2c_get16(i2cdev, MAX34451_MFR_VOUT_PEAK);
194         g_assert_cmpuint(i2c_value, ==, 3000 + i);
195 
196         i2c_value = max34451_i2c_get16(i2cdev, MAX34451_MFR_VOUT_MIN);
197         g_assert_cmpuint(i2c_value, ==, 3000 + i);
198     }
199 }
200 
201 /* Test setting some read/write registers */
202 static void test_rw_regs(void *obj, void *data, QGuestAllocator *alloc)
203 {
204     uint16_t i2c_value;
205     QI2CDevice *i2cdev = (QI2CDevice *)obj;
206 
207     i2c_set8(i2cdev, PMBUS_PAGE, 11);
208     i2c_value = i2c_get8(i2cdev, PMBUS_PAGE);
209     g_assert_cmpuint(i2c_value, ==, 11);
210 
211     i2c_set8(i2cdev, PMBUS_OPERATION, 1);
212     i2c_value = i2c_get8(i2cdev, PMBUS_OPERATION);
213     g_assert_cmpuint(i2c_value, ==, 1);
214 
215     max34451_i2c_set16(i2cdev, PMBUS_VOUT_MARGIN_HIGH, 5000);
216     i2c_value = max34451_i2c_get16(i2cdev, PMBUS_VOUT_MARGIN_HIGH);
217     g_assert_cmpuint(i2c_value, ==, 5000);
218 
219     max34451_i2c_set16(i2cdev, PMBUS_VOUT_MARGIN_LOW, 4000);
220     i2c_value = max34451_i2c_get16(i2cdev, PMBUS_VOUT_MARGIN_LOW);
221     g_assert_cmpuint(i2c_value, ==, 4000);
222 
223     max34451_i2c_set16(i2cdev, PMBUS_VOUT_OV_FAULT_LIMIT, 5500);
224     i2c_value = max34451_i2c_get16(i2cdev, PMBUS_VOUT_OV_FAULT_LIMIT);
225     g_assert_cmpuint(i2c_value, ==, 5500);
226 
227     max34451_i2c_set16(i2cdev, PMBUS_VOUT_OV_WARN_LIMIT, 5600);
228     i2c_value = max34451_i2c_get16(i2cdev, PMBUS_VOUT_OV_WARN_LIMIT);
229     g_assert_cmpuint(i2c_value, ==, 5600);
230 
231     max34451_i2c_set16(i2cdev, PMBUS_VOUT_UV_FAULT_LIMIT, 5700);
232     i2c_value = max34451_i2c_get16(i2cdev, PMBUS_VOUT_UV_FAULT_LIMIT);
233     g_assert_cmpuint(i2c_value, ==, 5700);
234 
235     max34451_i2c_set16(i2cdev, PMBUS_VOUT_UV_WARN_LIMIT, 5800);
236     i2c_value = max34451_i2c_get16(i2cdev, PMBUS_VOUT_UV_WARN_LIMIT);
237     g_assert_cmpuint(i2c_value, ==, 5800);
238 
239     max34451_i2c_set16(i2cdev, PMBUS_POWER_GOOD_ON, 5900);
240     i2c_value = max34451_i2c_get16(i2cdev, PMBUS_POWER_GOOD_ON);
241     g_assert_cmpuint(i2c_value, ==, 5900);
242 
243     max34451_i2c_set16(i2cdev, PMBUS_POWER_GOOD_OFF, 6100);
244     i2c_value = max34451_i2c_get16(i2cdev, PMBUS_POWER_GOOD_OFF);
245     g_assert_cmpuint(i2c_value, ==, 6100);
246 }
247 
248 /* Test that Read only registers can't be written */
249 static void test_ro_regs(void *obj, void *data, QGuestAllocator *alloc)
250 {
251     uint16_t i2c_value, i2c_init_value;
252     QI2CDevice *i2cdev = (QI2CDevice *)obj;
253 
254     i2c_set8(i2cdev, PMBUS_PAGE, 1); /* move to page 1 */
255     i2c_init_value = i2c_get8(i2cdev, PMBUS_CAPABILITY);
256     i2c_set8(i2cdev, PMBUS_CAPABILITY, 0xF9);
257     i2c_value = i2c_get8(i2cdev, PMBUS_CAPABILITY);
258     g_assert_cmpuint(i2c_init_value, ==, i2c_value);
259 
260     i2c_init_value = max34451_i2c_get16(i2cdev, PMBUS_READ_VOUT);
261     max34451_i2c_set16(i2cdev, PMBUS_READ_VOUT, 0xDEAD);
262     i2c_value = max34451_i2c_get16(i2cdev, PMBUS_READ_VOUT);
263     g_assert_cmpuint(i2c_init_value, ==, i2c_value);
264     g_assert_cmphex(i2c_value, !=, 0xDEAD);
265 
266     i2c_set8(i2cdev, PMBUS_PAGE, 16); /* move to page 16 */
267     i2c_init_value = max34451_i2c_get16(i2cdev, PMBUS_READ_TEMPERATURE_1);
268     max34451_i2c_set16(i2cdev, PMBUS_READ_TEMPERATURE_1, 0xABBA);
269     i2c_value = max34451_i2c_get16(i2cdev, PMBUS_READ_TEMPERATURE_1);
270     g_assert_cmpuint(i2c_init_value, ==, i2c_value);
271     g_assert_cmphex(i2c_value, !=, 0xABBA);
272 }
273 
274 /* test over voltage faults */
275 static void test_ov_faults(void *obj, void *data, QGuestAllocator *alloc)
276 {
277     uint16_t i2c_value;
278     uint8_t i2c_byte;
279     QI2CDevice *i2cdev = (QI2CDevice *)obj;
280     char *path;
281     /* Test ov fault reporting */
282     for (int i = 0; i < MAX34451_NUM_PWR_DEVICES; i++) {
283         path = g_strdup_printf("vout[%d]", i);
284         i2c_set8(i2cdev, PMBUS_PAGE, i);
285         max34451_i2c_set16(i2cdev, PMBUS_VOUT_OV_FAULT_LIMIT, 5000);
286         qmp_max34451_set(TEST_ID, path, 5100);
287         g_free(path);
288 
289         i2c_value = max34451_i2c_get16(i2cdev, PMBUS_STATUS_WORD);
290         i2c_byte = i2c_get8(i2cdev, PMBUS_STATUS_VOUT);
291         g_assert_true((i2c_value & PB_STATUS_VOUT) != 0);
292         g_assert_true((i2c_byte & PB_STATUS_VOUT_OV_FAULT) != 0);
293     }
294 }
295 
296 /* test over temperature faults */
297 static void test_ot_faults(void *obj, void *data, QGuestAllocator *alloc)
298 {
299     uint16_t i2c_value;
300     uint8_t i2c_byte;
301     QI2CDevice *i2cdev = (QI2CDevice *)obj;
302     char *path;
303 
304     for (int i = 0; i < MAX34451_NUM_TEMP_DEVICES; i++) {
305         path = g_strdup_printf("temperature[%d]", i);
306         i2c_set8(i2cdev, PMBUS_PAGE, i + 16);
307         max34451_i2c_set16(i2cdev, PMBUS_OT_FAULT_LIMIT, 6000);
308         qmp_max34451_set(TEST_ID, path, 6100);
309         g_free(path);
310 
311         i2c_value = max34451_i2c_get16(i2cdev, PMBUS_STATUS_WORD);
312         i2c_byte = i2c_get8(i2cdev, PMBUS_STATUS_TEMPERATURE);
313         g_assert_true((i2c_value & PB_STATUS_TEMPERATURE) != 0);
314         g_assert_true((i2c_byte & PB_STATUS_OT_FAULT) != 0);
315     }
316 }
317 
318 static void max34451_register_nodes(void)
319 {
320     QOSGraphEdgeOptions opts = {
321         .extra_device_opts = "id=" TEST_ID ",address=0x4e"
322     };
323     add_qi2c_address(&opts, &(QI2CAddress) { TEST_ADDR });
324 
325     qos_node_create_driver("max34451", i2c_device_create);
326     qos_node_consumes("max34451", "i2c-bus", &opts);
327 
328     qos_add_test("test_defaults", "max34451", test_defaults, NULL);
329     qos_add_test("test_temperature", "max34451", test_temperature, NULL);
330     qos_add_test("test_voltage", "max34451", test_voltage, NULL);
331     qos_add_test("test_rw_regs", "max34451", test_rw_regs, NULL);
332     qos_add_test("test_ro_regs", "max34451", test_ro_regs, NULL);
333     qos_add_test("test_ov_faults", "max34451", test_ov_faults, NULL);
334     qos_add_test("test_ot_faults", "max34451", test_ot_faults, NULL);
335 }
336 libqos_init(max34451_register_nodes);
337