1
2
3
4
5
6
7
8
9#ifndef HW_PMBUS_DEVICE_H
10#define HW_PMBUS_DEVICE_H
11
12#include "qemu/bitops.h"
13#include "hw/i2c/smbus_slave.h"
14
15enum pmbus_registers {
16 PMBUS_PAGE = 0x00,
17 PMBUS_OPERATION = 0x01,
18 PMBUS_ON_OFF_CONFIG = 0x02,
19 PMBUS_CLEAR_FAULTS = 0x03,
20 PMBUS_PHASE = 0x04,
21 PMBUS_PAGE_PLUS_WRITE = 0x05,
22 PMBUS_PAGE_PLUS_READ = 0x06,
23 PMBUS_WRITE_PROTECT = 0x10,
24 PMBUS_STORE_DEFAULT_ALL = 0x11,
25 PMBUS_RESTORE_DEFAULT_ALL = 0x12,
26 PMBUS_STORE_DEFAULT_CODE = 0x13,
27 PMBUS_RESTORE_DEFAULT_CODE = 0x14,
28 PMBUS_STORE_USER_ALL = 0x15,
29 PMBUS_RESTORE_USER_ALL = 0x16,
30 PMBUS_STORE_USER_CODE = 0x17,
31 PMBUS_RESTORE_USER_CODE = 0x18,
32 PMBUS_CAPABILITY = 0x19,
33 PMBUS_QUERY = 0x1A,
34 PMBUS_SMBALERT_MASK = 0x1B,
35 PMBUS_VOUT_MODE = 0x20,
36 PMBUS_VOUT_COMMAND = 0x21,
37 PMBUS_VOUT_TRIM = 0x22,
38 PMBUS_VOUT_CAL_OFFSET = 0x23,
39 PMBUS_VOUT_MAX = 0x24,
40 PMBUS_VOUT_MARGIN_HIGH = 0x25,
41 PMBUS_VOUT_MARGIN_LOW = 0x26,
42 PMBUS_VOUT_TRANSITION_RATE = 0x27,
43 PMBUS_VOUT_DROOP = 0x28,
44 PMBUS_VOUT_SCALE_LOOP = 0x29,
45 PMBUS_VOUT_SCALE_MONITOR = 0x2A,
46 PMBUS_VOUT_MIN = 0x2B,
47 PMBUS_COEFFICIENTS = 0x30,
48 PMBUS_POUT_MAX = 0x31,
49 PMBUS_MAX_DUTY = 0x32,
50 PMBUS_FREQUENCY_SWITCH = 0x33,
51 PMBUS_VIN_ON = 0x35,
52 PMBUS_VIN_OFF = 0x36,
53 PMBUS_INTERLEAVE = 0x37,
54 PMBUS_IOUT_CAL_GAIN = 0x38,
55 PMBUS_IOUT_CAL_OFFSET = 0x39,
56 PMBUS_FAN_CONFIG_1_2 = 0x3A,
57 PMBUS_FAN_COMMAND_1 = 0x3B,
58 PMBUS_FAN_COMMAND_2 = 0x3C,
59 PMBUS_FAN_CONFIG_3_4 = 0x3D,
60 PMBUS_FAN_COMMAND_3 = 0x3E,
61 PMBUS_FAN_COMMAND_4 = 0x3F,
62 PMBUS_VOUT_OV_FAULT_LIMIT = 0x40,
63 PMBUS_VOUT_OV_FAULT_RESPONSE = 0x41,
64 PMBUS_VOUT_OV_WARN_LIMIT = 0x42,
65 PMBUS_VOUT_UV_WARN_LIMIT = 0x43,
66 PMBUS_VOUT_UV_FAULT_LIMIT = 0x44,
67 PMBUS_VOUT_UV_FAULT_RESPONSE = 0x45,
68 PMBUS_IOUT_OC_FAULT_LIMIT = 0x46,
69 PMBUS_IOUT_OC_FAULT_RESPONSE = 0x47,
70 PMBUS_IOUT_OC_LV_FAULT_LIMIT = 0x48,
71 PMBUS_IOUT_OC_LV_FAULT_RESPONSE = 0x49,
72 PMBUS_IOUT_OC_WARN_LIMIT = 0x4A,
73 PMBUS_IOUT_UC_FAULT_LIMIT = 0x4B,
74 PMBUS_IOUT_UC_FAULT_RESPONSE = 0x4C,
75 PMBUS_OT_FAULT_LIMIT = 0x4F,
76 PMBUS_OT_FAULT_RESPONSE = 0x50,
77 PMBUS_OT_WARN_LIMIT = 0x51,
78 PMBUS_UT_WARN_LIMIT = 0x52,
79 PMBUS_UT_FAULT_LIMIT = 0x53,
80 PMBUS_UT_FAULT_RESPONSE = 0x54,
81 PMBUS_VIN_OV_FAULT_LIMIT = 0x55,
82 PMBUS_VIN_OV_FAULT_RESPONSE = 0x56,
83 PMBUS_VIN_OV_WARN_LIMIT = 0x57,
84 PMBUS_VIN_UV_WARN_LIMIT = 0x58,
85 PMBUS_VIN_UV_FAULT_LIMIT = 0x59,
86 PMBUS_VIN_UV_FAULT_RESPONSE = 0x5A,
87 PMBUS_IIN_OC_FAULT_LIMIT = 0x5B,
88 PMBUS_IIN_OC_FAULT_RESPONSE = 0x5C,
89 PMBUS_IIN_OC_WARN_LIMIT = 0x5D,
90 PMBUS_POWER_GOOD_ON = 0x5E,
91 PMBUS_POWER_GOOD_OFF = 0x5F,
92 PMBUS_TON_DELAY = 0x60,
93 PMBUS_TON_RISE = 0x61,
94 PMBUS_TON_MAX_FAULT_LIMIT = 0x62,
95 PMBUS_TON_MAX_FAULT_RESPONSE = 0x63,
96 PMBUS_TOFF_DELAY = 0x64,
97 PMBUS_TOFF_FALL = 0x65,
98 PMBUS_TOFF_MAX_WARN_LIMIT = 0x66,
99 PMBUS_POUT_OP_FAULT_LIMIT = 0x68,
100 PMBUS_POUT_OP_FAULT_RESPONSE = 0x69,
101 PMBUS_POUT_OP_WARN_LIMIT = 0x6A,
102 PMBUS_PIN_OP_WARN_LIMIT = 0x6B,
103 PMBUS_STATUS_BYTE = 0x78,
104 PMBUS_STATUS_WORD = 0x79,
105 PMBUS_STATUS_VOUT = 0x7A,
106 PMBUS_STATUS_IOUT = 0x7B,
107 PMBUS_STATUS_INPUT = 0x7C,
108 PMBUS_STATUS_TEMPERATURE = 0x7D,
109 PMBUS_STATUS_CML = 0x7E,
110 PMBUS_STATUS_OTHER = 0x7F,
111 PMBUS_STATUS_MFR_SPECIFIC = 0x80,
112 PMBUS_STATUS_FANS_1_2 = 0x81,
113 PMBUS_STATUS_FANS_3_4 = 0x82,
114 PMBUS_READ_EIN = 0x86,
115 PMBUS_READ_EOUT = 0x87,
116 PMBUS_READ_VIN = 0x88,
117 PMBUS_READ_IIN = 0x89,
118 PMBUS_READ_VCAP = 0x8A,
119 PMBUS_READ_VOUT = 0x8B,
120 PMBUS_READ_IOUT = 0x8C,
121 PMBUS_READ_TEMPERATURE_1 = 0x8D,
122 PMBUS_READ_TEMPERATURE_2 = 0x8E,
123 PMBUS_READ_TEMPERATURE_3 = 0x8F,
124 PMBUS_READ_FAN_SPEED_1 = 0x90,
125 PMBUS_READ_FAN_SPEED_2 = 0x91,
126 PMBUS_READ_FAN_SPEED_3 = 0x92,
127 PMBUS_READ_FAN_SPEED_4 = 0x93,
128 PMBUS_READ_DUTY_CYCLE = 0x94,
129 PMBUS_READ_FREQUENCY = 0x95,
130 PMBUS_READ_POUT = 0x96,
131 PMBUS_READ_PIN = 0x97,
132 PMBUS_REVISION = 0x98,
133 PMBUS_MFR_ID = 0x99,
134 PMBUS_MFR_MODEL = 0x9A,
135 PMBUS_MFR_REVISION = 0x9B,
136 PMBUS_MFR_LOCATION = 0x9C,
137 PMBUS_MFR_DATE = 0x9D,
138 PMBUS_MFR_SERIAL = 0x9E,
139 PMBUS_APP_PROFILE_SUPPORT = 0x9F,
140 PMBUS_MFR_VIN_MIN = 0xA0,
141 PMBUS_MFR_VIN_MAX = 0xA1,
142 PMBUS_MFR_IIN_MAX = 0xA2,
143 PMBUS_MFR_PIN_MAX = 0xA3,
144 PMBUS_MFR_VOUT_MIN = 0xA4,
145 PMBUS_MFR_VOUT_MAX = 0xA5,
146 PMBUS_MFR_IOUT_MAX = 0xA6,
147 PMBUS_MFR_POUT_MAX = 0xA7,
148 PMBUS_MFR_TAMBIENT_MAX = 0xA8,
149 PMBUS_MFR_TAMBIENT_MIN = 0xA9,
150 PMBUS_MFR_EFFICIENCY_LL = 0xAA,
151 PMBUS_MFR_EFFICIENCY_HL = 0xAB,
152 PMBUS_MFR_PIN_ACCURACY = 0xAC,
153 PMBUS_IC_DEVICE_ID = 0xAD,
154 PMBUS_IC_DEVICE_REV = 0xAE,
155 PMBUS_MFR_MAX_TEMP_1 = 0xC0,
156 PMBUS_MFR_MAX_TEMP_2 = 0xC1,
157 PMBUS_MFR_MAX_TEMP_3 = 0xC2,
158 PMBUS_IDLE_STATE = 0xFF,
159};
160
161
162#define PB_STATUS_VOUT BIT(15)
163#define PB_STATUS_IOUT_POUT BIT(14)
164#define PB_STATUS_INPUT BIT(13)
165#define PB_STATUS_WORD_MFR BIT(12)
166#define PB_STATUS_POWER_GOOD_N BIT(11)
167#define PB_STATUS_FAN BIT(10)
168#define PB_STATUS_OTHER BIT(9)
169#define PB_STATUS_UNKNOWN BIT(8)
170
171#define PB_STATUS_BUSY BIT(7)
172#define PB_STATUS_OFF BIT(6)
173#define PB_STATUS_VOUT_OV BIT(5)
174#define PB_STATUS_IOUT_OC BIT(4)
175#define PB_STATUS_VIN_UV BIT(3)
176#define PB_STATUS_TEMPERATURE BIT(2)
177#define PB_STATUS_CML BIT(1)
178#define PB_STATUS_NONE_ABOVE BIT(0)
179
180
181#define PB_STATUS_VOUT_OV_FAULT BIT(7)
182#define PB_STATUS_VOUT_OV_WARN BIT(6)
183#define PB_STATUS_VOUT_UV_WARN BIT(5)
184#define PB_STATUS_VOUT_UV_FAULT BIT(4)
185#define PB_STATUS_VOUT_MAX BIT(3)
186#define PB_STATUS_VOUT_TON_MAX_FAULT BIT(2)
187#define PB_STATUS_VOUT_TOFF_MAX_WARN BIT(1)
188
189
190#define PB_STATUS_IOUT_OC_FAULT BIT(7)
191#define PB_STATUS_IOUT_OC_LV_FAULT BIT(6)
192#define PB_STATUS_IOUT_OC_WARN BIT(5)
193#define PB_STATUS_IOUT_UC_FAULT BIT(4)
194#define PB_STATUS_CURR_SHARE BIT(3)
195#define PB_STATUS_PWR_LIM_MODE BIT(2)
196#define PB_STATUS_POUT_OP_FAULT BIT(1)
197#define PB_STATUS_POUT_OP_WARN BIT(0)
198
199
200#define PB_STATUS_INPUT_VIN_OV_FAULT BIT(7)
201#define PB_STATUS_INPUT_VIN_OV_WARN BIT(6)
202#define PB_STATUS_INPUT_VIN_UV_WARN BIT(5)
203#define PB_STATUS_INPUT_VIN_UV_FAULT BIT(4)
204#define PB_STATUS_INPUT_IIN_OC_FAULT BIT(2)
205#define PB_STATUS_INPUT_IIN_OC_WARN BIT(1)
206#define PB_STATUS_INPUT_PIN_OP_WARN BIT(0)
207
208
209#define PB_STATUS_OT_FAULT BIT(7)
210#define PB_STATUS_OT_WARN BIT(6)
211#define PB_STATUS_UT_WARN BIT(5)
212#define PB_STATUS_UT_FAULT BIT(4)
213
214
215#define PB_CML_FAULT_INVALID_CMD BIT(7)
216#define PB_CML_FAULT_INVALID_DATA BIT(6)
217#define PB_CML_FAULT_PEC BIT(5)
218#define PB_CML_FAULT_MEMORY BIT(4)
219#define PB_CML_FAULT_PROCESSOR BIT(3)
220#define PB_CML_FAULT_OTHER_COMM BIT(1)
221#define PB_CML_FAULT_OTHER_MEM_LOGIC BIT(0)
222
223
224#define PB_OP_ON BIT(7)
225#define PB_OP_MARGIN_HIGH BIT(5)
226#define PB_OP_MARGIN_LOW BIT(4)
227
228
229#define PB_MAX_PAGES 0x1F
230#define PB_ALL_PAGES 0xFF
231
232#define PMBUS_ERR_BYTE 0xFF
233
234#define TYPE_PMBUS_DEVICE "pmbus-device"
235OBJECT_DECLARE_TYPE(PMBusDevice, PMBusDeviceClass,
236 PMBUS_DEVICE)
237
238
239#define PB_HAS_COEFFICIENTS BIT_ULL(9)
240#define PB_HAS_VIN BIT_ULL(10)
241#define PB_HAS_VOUT BIT_ULL(11)
242#define PB_HAS_VOUT_MARGIN BIT_ULL(12)
243#define PB_HAS_VIN_RATING BIT_ULL(13)
244#define PB_HAS_VOUT_RATING BIT_ULL(14)
245#define PB_HAS_VOUT_MODE BIT_ULL(15)
246#define PB_HAS_IOUT BIT_ULL(21)
247#define PB_HAS_IIN BIT_ULL(22)
248#define PB_HAS_IOUT_RATING BIT_ULL(23)
249#define PB_HAS_IIN_RATING BIT_ULL(24)
250#define PB_HAS_IOUT_GAIN BIT_ULL(25)
251#define PB_HAS_POUT BIT_ULL(30)
252#define PB_HAS_PIN BIT_ULL(31)
253#define PB_HAS_EIN BIT_ULL(32)
254#define PB_HAS_EOUT BIT_ULL(33)
255#define PB_HAS_POUT_RATING BIT_ULL(34)
256#define PB_HAS_PIN_RATING BIT_ULL(35)
257#define PB_HAS_TEMPERATURE BIT_ULL(40)
258#define PB_HAS_TEMP2 BIT_ULL(41)
259#define PB_HAS_TEMP3 BIT_ULL(42)
260#define PB_HAS_TEMP_RATING BIT_ULL(43)
261#define PB_HAS_MFR_INFO BIT_ULL(50)
262#define PB_HAS_STATUS_MFR_SPECIFIC BIT_ULL(51)
263
264struct PMBusDeviceClass {
265 SMBusDeviceClass parent_class;
266 uint8_t device_num_pages;
267
268
269
270
271
272 void (*quick_cmd)(PMBusDevice *dev, uint8_t read);
273 int (*write_data)(PMBusDevice *dev, const uint8_t *buf, uint8_t len);
274 uint8_t (*receive_byte)(PMBusDevice *dev);
275};
276
277
278
279
280
281
282
283
284typedef struct PMBusPage {
285 uint64_t page_flags;
286
287 uint8_t page;
288 uint8_t operation;
289 uint8_t on_off_config;
290 uint8_t write_protect;
291 uint8_t phase;
292 uint8_t vout_mode;
293 uint16_t vout_command;
294 uint16_t vout_trim;
295 uint16_t vout_cal_offset;
296 uint16_t vout_max;
297 uint16_t vout_margin_high;
298 uint16_t vout_margin_low;
299 uint16_t vout_transition_rate;
300 uint16_t vout_droop;
301 uint16_t vout_scale_loop;
302 uint16_t vout_scale_monitor;
303 uint16_t vout_min;
304 uint8_t coefficients[5];
305 uint16_t pout_max;
306 uint16_t max_duty;
307 uint16_t frequency_switch;
308 uint16_t vin_on;
309 uint16_t vin_off;
310 uint16_t iout_cal_gain;
311 uint16_t iout_cal_offset;
312 uint8_t fan_config_1_2;
313 uint16_t fan_command_1;
314 uint16_t fan_command_2;
315 uint8_t fan_config_3_4;
316 uint16_t fan_command_3;
317 uint16_t fan_command_4;
318 uint16_t vout_ov_fault_limit;
319 uint8_t vout_ov_fault_response;
320 uint16_t vout_ov_warn_limit;
321 uint16_t vout_uv_warn_limit;
322 uint16_t vout_uv_fault_limit;
323 uint8_t vout_uv_fault_response;
324 uint16_t iout_oc_fault_limit;
325 uint8_t iout_oc_fault_response;
326 uint16_t iout_oc_lv_fault_limit;
327 uint8_t iout_oc_lv_fault_response;
328 uint16_t iout_oc_warn_limit;
329 uint16_t iout_uc_fault_limit;
330 uint8_t iout_uc_fault_response;
331 uint16_t ot_fault_limit;
332 uint8_t ot_fault_response;
333 uint16_t ot_warn_limit;
334 uint16_t ut_warn_limit;
335 uint16_t ut_fault_limit;
336 uint8_t ut_fault_response;
337 uint16_t vin_ov_fault_limit;
338 uint8_t vin_ov_fault_response;
339 uint16_t vin_ov_warn_limit;
340 uint16_t vin_uv_warn_limit;
341 uint16_t vin_uv_fault_limit;
342 uint8_t vin_uv_fault_response;
343 uint16_t iin_oc_fault_limit;
344 uint8_t iin_oc_fault_response;
345 uint16_t iin_oc_warn_limit;
346 uint16_t power_good_on;
347 uint16_t power_good_off;
348 uint16_t ton_delay;
349 uint16_t ton_rise;
350 uint16_t ton_max_fault_limit;
351 uint8_t ton_max_fault_response;
352 uint16_t toff_delay;
353 uint16_t toff_fall;
354 uint16_t toff_max_warn_limit;
355 uint16_t pout_op_fault_limit;
356 uint8_t pout_op_fault_response;
357 uint16_t pout_op_warn_limit;
358 uint16_t pin_op_warn_limit;
359 uint16_t status_word;
360 uint8_t status_vout;
361 uint8_t status_iout;
362 uint8_t status_input;
363 uint8_t status_temperature;
364 uint8_t status_cml;
365 uint8_t status_other;
366 uint8_t status_mfr_specific;
367 uint8_t status_fans_1_2;
368 uint8_t status_fans_3_4;
369 uint8_t read_ein[5];
370 uint8_t read_eout[5];
371 uint16_t read_vin;
372 uint16_t read_iin;
373 uint16_t read_vcap;
374 uint16_t read_vout;
375 uint16_t read_iout;
376 uint16_t read_temperature_1;
377 uint16_t read_temperature_2;
378 uint16_t read_temperature_3;
379 uint16_t read_fan_speed_1;
380 uint16_t read_fan_speed_2;
381 uint16_t read_fan_speed_3;
382 uint16_t read_fan_speed_4;
383 uint16_t read_duty_cycle;
384 uint16_t read_frequency;
385 uint16_t read_pout;
386 uint16_t read_pin;
387 uint8_t revision;
388 const char *mfr_id;
389 const char *mfr_model;
390 const char *mfr_revision;
391 const char *mfr_location;
392 const char *mfr_date;
393 const char *mfr_serial;
394 const char *app_profile_support;
395 uint16_t mfr_vin_min;
396 uint16_t mfr_vin_max;
397 uint16_t mfr_iin_max;
398 uint16_t mfr_pin_max;
399 uint16_t mfr_vout_min;
400 uint16_t mfr_vout_max;
401 uint16_t mfr_iout_max;
402 uint16_t mfr_pout_max;
403 uint16_t mfr_tambient_max;
404 uint16_t mfr_tambient_min;
405 uint8_t mfr_efficiency_ll[14];
406 uint8_t mfr_efficiency_hl[14];
407 uint8_t mfr_pin_accuracy;
408 uint16_t mfr_max_temp_1;
409 uint16_t mfr_max_temp_2;
410 uint16_t mfr_max_temp_3;
411} PMBusPage;
412
413
414struct PMBusDevice {
415 SMBusDevice smb;
416
417 uint8_t num_pages;
418 uint8_t code;
419 uint8_t page;
420
421
422
423
424
425 PMBusPage *pages;
426 uint8_t capability;
427
428
429 int32_t in_buf_len;
430 uint8_t *in_buf;
431 int32_t out_buf_len;
432 uint8_t out_buf[SMBUS_DATA_MAX_LEN];
433};
434
435
436
437
438
439
440
441typedef struct PMBusCoefficients {
442 int32_t m;
443 int64_t b;
444 int32_t R;
445} PMBusCoefficients;
446
447
448
449
450
451
452
453
454uint16_t pmbus_data2direct_mode(PMBusCoefficients c, uint32_t value);
455
456
457
458
459
460
461
462
463uint32_t pmbus_direct_mode2data(PMBusCoefficients c, uint16_t value);
464
465
466
467
468
469
470
471
472uint16_t pmbus_data2linear_mode(uint16_t value, int exp);
473
474
475
476
477
478
479
480
481uint16_t pmbus_linear_mode2data(uint16_t value, int exp);
482
483
484
485
486
487
488
489
490
491
492void pmbus_send(PMBusDevice *state, const uint8_t *data, uint16_t len);
493void pmbus_send8(PMBusDevice *state, uint8_t data);
494void pmbus_send16(PMBusDevice *state, uint16_t data);
495void pmbus_send32(PMBusDevice *state, uint32_t data);
496void pmbus_send64(PMBusDevice *state, uint64_t data);
497
498
499
500
501
502void pmbus_send_string(PMBusDevice *state, const char *data);
503
504
505
506
507
508
509uint8_t pmbus_receive8(PMBusDevice *pmdev);
510uint16_t pmbus_receive16(PMBusDevice *pmdev);
511uint32_t pmbus_receive32(PMBusDevice *pmdev);
512uint64_t pmbus_receive64(PMBusDevice *pmdev);
513
514
515
516
517
518
519
520
521
522int pmbus_page_config(PMBusDevice *pmdev, uint8_t page_index, uint64_t flags);
523
524
525
526
527
528
529void pmbus_check_limits(PMBusDevice *pmdev);
530
531
532
533
534
535void pmbus_idle(PMBusDevice *pmdev);
536
537extern const VMStateDescription vmstate_pmbus_device;
538
539#define VMSTATE_PMBUS_DEVICE(_field, _state) { \
540 .name = (stringify(_field)), \
541 .size = sizeof(PMBusDevice), \
542 .vmsd = &vmstate_pmbus_device, \
543 .flags = VMS_STRUCT, \
544 .offset = vmstate_offset_value(_state, _field, PMBusDevice), \
545}
546
547#endif
548