1 #pragma once
2 
3 #include "device.hpp"
4 #include "pmbus.hpp"
5 #include "tools/i2c/i2c_interface.hpp"
6 
7 #include <sdbusplus/bus.hpp>
8 
9 #include <algorithm>
10 #include <filesystem>
11 
12 namespace phosphor
13 {
14 namespace power
15 {
16 
17 /**
18  * @class MihawkCPLD
19  *
20  * This class implements fault analysis for Mihawk's CPLD
21  * power sequencer device.
22  *
23  */
24 class MihawkCPLD : public Device
25 {
26   public:
27     MihawkCPLD() = delete;
28     ~MihawkCPLD() = default;
29     MihawkCPLD(const MihawkCPLD&) = delete;
30     MihawkCPLD& operator=(const MihawkCPLD&) = delete;
31     MihawkCPLD(MihawkCPLD&&) = default;
32     MihawkCPLD& operator=(MihawkCPLD&&) = default;
33 
34     /**
35      * Constructor
36      *
37      * @param[in] instance - the device instance number
38      * @param[in] bus - D-Bus bus object
39      */
40     MihawkCPLD(size_t instance, sdbusplus::bus_t& bus);
41 
42     /**
43      * Analyzes the device for errors when the device is
44      * known to be in an error state.  A log will be created.
45      */
46     void onFailure() override;
47 
48     /**
49      * Checks the device for errors and only creates a log
50      * if one is found.
51      */
52     void analyze() override;
53 
54     /**
55      * Clears faults in the device
56      */
clearFaults()57     void clearFaults() override {}
58 
59   private:
60     /**
61      * If checkPoweronFault() or checkPowerreadyFault()
62      * returns "true", use readFromCPLDErrorCode()
63      * to read CPLD-error-code-register
64      * to analyze the fail reason.
65      *
66      * @param[in] statusReg - I2C's statusReg, slaveAddr
67      * offset.
68      * ex.Mihawk's CPLD-register is on slaveAddr ox40 of
69      * i2c-11, but poweron_errcode-register is on slaveAddr
70      * offset 0x21, power_ready-errorcode-register is on
71      * slaveAddr offset 0x22.
72      *
73      * @return int - the error-code value which is read on
74      * CPLD-error-code-register.
75      */
76     int readFromCPLDErrorCode(int statusReg);
77 
78     /**
79      * Checks for PoweronFault on Mihawk's
80      * CPLD-power_on-error-interrupt-bit-register
81      * whether is transfered to "1".
82      *
83      * @return bool - true if power_on fail.
84      */
85     bool checkPoweronFault();
86 
87     /**
88      * Clear CPLD intrupt record after reading CPLD_register.
89      */
90     void clearCPLDregister();
91 
92     /**
93      * Check for PowerreadyFault on Mihawk's
94      * CPLD-power_ready-error-interrupt-bit-register
95      * whether is transfered to "1".
96      *
97      * @return bool - true if power_ready fail.
98      */
99     bool checkPowerreadyFault();
100 
101     /**
102      * Use I2CInterface to read & write CPLD_register.
103      */
104     std::unique_ptr<i2c::I2CInterface> i2c;
105 
106     /**
107      * The D-Bus bus object
108      */
109     sdbusplus::bus_t& bus;
110 
111     /**
112      * Open CPLD_register via i2c.
113      */
114     void openCPLDDevice();
115 
116     /**
117      * The parameter which is checked CPLD's the same error
118      * whether is created again.
119      */
120     bool errorcodeMask;
121 
122     enum class ErrorCode : int
123     {
124         /**
125          * All of powerOnErrorcode are the definition of error-code
126          * which are read on CPLD-error-code-register.
127          */
128         /**
129          * The definition of error-code:
130          * Read CPLD-error-code-register fail.
131          */
132         _0 = 0,
133 
134         /**
135          * The definition of error-code:
136          * PSU0_PGOOD fail.
137          */
138         _1 = 1,
139 
140         /**
141          * The definition of error-code:
142          * PSU1_PGOOD fail.
143          */
144         _2 = 2,
145 
146         /**
147          * The definition of error-code:
148          * 240Va_Fault_A fail.
149          */
150         _3 = 3,
151 
152         /**
153          * The definition of error-code:
154          * 240Va_Fault_B fail.
155          */
156         _4 = 4,
157 
158         /**
159          * The definition of error-code:
160          * 240Va_Fault_C fail.
161          */
162         _5 = 5,
163 
164         /**
165          * The definition of error-code:
166          * 240Va_Fault_D fail.
167          */
168         _6 = 6,
169 
170         /**
171          * The definition of error-code:
172          * 240Va_Fault_E fail.
173          */
174         _7 = 7,
175 
176         /**
177          * The definition of error-code:
178          * 240Va_Fault_F fail.
179          */
180         _8 = 8,
181 
182         /**
183          * The definition of error-code:
184          * 240Va_Fault_G fail.
185          */
186         _9 = 9,
187 
188         /**
189          * The definition of error-code:
190          * 240Va_Fault_H fail.
191          */
192         _10 = 10,
193 
194         /**
195          * The definition of error-code:
196          * 240Va_Fault_J fail.
197          */
198         _11 = 11,
199 
200         /**
201          * The definition of error-code:
202          * 240Va_Fault_K fail.
203          */
204         _12 = 12,
205 
206         /**
207          * The definition of error-code:
208          * 240Va_Fault_L fail.
209          */
210         _13 = 13,
211 
212         /**
213          * The definition of error-code:
214          * P5V_PGOOD fail.
215          */
216         _14 = 14,
217 
218         /**
219          * The definition of error-code:
220          * P3V3_PGOOD fail.
221          */
222         _15 = 15,
223 
224         /**
225          * The definition of error-code:
226          * P1V8_PGOOD fail.
227          */
228         _16 = 16,
229 
230         /**
231          * The definition of error-code:
232          * P1V1_PGOOD fail.
233          */
234         _17 = 17,
235 
236         /**
237          * The definition of error-code:
238          * P0V9_PGOOD fail.
239          */
240         _18 = 18,
241 
242         /**
243          * The definition of error-code:
244          * P2V5A_PGOOD fail.
245          */
246         _19 = 19,
247 
248         /**
249          * The definition of error-code:
250          * P2V5B_PGOOD fail.
251          */
252         _20 = 20,
253 
254         /**
255          * The definition of error-code:
256          * Vdn0_PGOOD fail.
257          */
258         _21 = 21,
259 
260         /**
261          * The definition of error-code:
262          * Vdn1_PGOOD fail.
263          */
264         _22 = 22,
265 
266         /**
267          * The definition of error-code:
268          * P1V5_PGOOD fail.
269          */
270         _23 = 23,
271 
272         /**
273          * The definition of error-code:
274          * Vio0_PGOOD fail.
275          */
276         _24 = 24,
277 
278         /**
279          * The definition of error-code:
280          * Vio1_PGOOD fail.
281          */
282         _25 = 25,
283 
284         /**
285          * The definition of error-code:
286          * Vdd0_PGOOD fail.
287          */
288         _26 = 26,
289 
290         /**
291          * The definition of error-code:
292          * Vcs0_PGOOD fail.
293          */
294         _27 = 27,
295 
296         /**
297          * The definition of error-code:
298          * Vdd1_PGOOD fail.
299          */
300         _28 = 28,
301 
302         /**
303          * The definition of error-code:
304          * Vcs1_PGOOD fail.
305          */
306         _29 = 29,
307 
308         /**
309          * The definition of error-code:
310          * Vddr0_PGOOD fail.
311          */
312         _30 = 30,
313 
314         /**
315          * The definition of error-code:
316          * Vtt0_PGOOD fail.
317          */
318         _31 = 31,
319 
320         /**
321          * The definition of error-code:
322          * Vddr1_PGOOD fail.
323          */
324         _32 = 32,
325 
326         /**
327          * The definition of error-code:
328          * Vtt1_PGOOD fail.
329          */
330         _33 = 33,
331 
332         /**
333          * The definition of error-code:
334          * GPU0_PGOOD fail.
335          */
336         _34 = 34,
337 
338         /**
339          * The definition of error-code:
340          * GPU1_PGOOD fail.
341          */
342         _35 = 35,
343 
344         /**
345          * The definition of error-code:
346          * PSU0PSU1_PGOOD fail.
347          */
348         _36 = 170
349     };
350 };
351 
352 } // namespace power
353 } // namespace phosphor
354