blob: 2e87fa954398fc9974907d3b9110b5d6859a03c2 [file] [log] [blame]
AppaRao Pulic532f552019-07-05 15:23:50 +05301/*
2// Copyright (c) 2019 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
17#include <unistd.h>
18#include <iostream>
19#include <sstream>
20#include <iomanip>
21#include "pfr.hpp"
22#include "file.hpp"
23
24namespace intel
25{
26namespace pfr
27{
28// TODO: Dynamically pull these values from configuration
29// entity-manager, when needed
30static constexpr int i2cBusNumber = 4;
31static constexpr int i2cSlaveAddress = 0x70;
32
33// CPLD mailbox registers
34static constexpr uint8_t cpldROTVersion = 0x01;
35static constexpr uint8_t cpldROTSvn = 0x02;
36static constexpr uint8_t platformState = 0x03;
37static constexpr uint8_t recoveryCount = 0x04;
38static constexpr uint8_t lastRecoveryReason = 0x05;
39static constexpr uint8_t panicEventCount = 0x06;
40static constexpr uint8_t panicEventReason = 0x07;
41static constexpr uint8_t majorErrorCode = 0x08;
42static constexpr uint8_t minorErrorCode = 0x09;
43static constexpr uint8_t provisioningStatus = 0x0A;
AppaRao Puli46cead92019-07-22 16:50:09 +053044static constexpr uint8_t bmcBootCheckpoint = 0x0F;
AppaRao Pulic532f552019-07-05 15:23:50 +053045static constexpr uint8_t pchActiveMajorVersion = 0x17;
46static constexpr uint8_t pchActiveMinorVersion = 0x18;
47static constexpr uint8_t bmcActiveMajorVersion = 0x19;
48static constexpr uint8_t bmcActiveMinorVersion = 0x1A;
49static constexpr uint8_t pchRecoveryMajorVersion = 0x1C;
50static constexpr uint8_t pchRecoveryMinorVersion = 0x1D;
51static constexpr uint8_t bmcRecoveryMajorVersion = 0x1E;
52static constexpr uint8_t bmcRecoveryMinorVersion = 0x1F;
53
54static constexpr uint8_t ufmLockedMask = (0x1 << 0x04);
55static constexpr uint8_t ufmProvisionedMask = (0x1 << 0x05);
56
57template <typename T> std::string int_to_hexstring(T i)
58{
59 std::stringstream stream;
60 stream << std::setfill('0') << std::setw(sizeof(T) * 2) << std::hex
61 << static_cast<int>(i);
62 return stream.str();
63}
64
65std::string getVersionInfoCPLD(ImageType& imgType)
66{
67 try
68 {
69 uint8_t majorReg;
70 uint8_t minorReg;
71
72 switch (imgType)
73 {
74 case (ImageType::cpld):
75 {
76 majorReg = cpldROTVersion;
77 minorReg = cpldROTSvn;
78 break;
79 }
80 case (ImageType::biosActive):
81 {
82 majorReg = pchActiveMajorVersion;
83 minorReg = pchActiveMinorVersion;
84 break;
85 }
86 case (ImageType::biosRecovery):
87 {
88 majorReg = pchRecoveryMajorVersion;
89 minorReg = pchRecoveryMinorVersion;
90 break;
91 }
92 case (ImageType::bmcActive):
93 {
94 majorReg = bmcActiveMajorVersion;
95 minorReg = bmcActiveMinorVersion;
96 break;
97 }
98 case (ImageType::bmcRecovery):
99 {
100 majorReg = bmcRecoveryMajorVersion;
101 minorReg = bmcRecoveryMinorVersion;
102 break;
103 }
104 default:
105 // Invalid image Type.
106 return "";
107 }
108
109 I2CFile cpldDev(i2cBusNumber, i2cSlaveAddress, O_RDWR | O_CLOEXEC);
110 uint8_t majorVer = cpldDev.i2cReadByteData(majorReg);
111 uint8_t minorVer = cpldDev.i2cReadByteData(minorReg);
112 std::string version =
113 int_to_hexstring(majorVer) + "." + int_to_hexstring(minorVer);
114 return version;
115 }
116 catch (const std::exception& e)
117 {
118 phosphor::logging::log<phosphor::logging::level::ERR>(
119 "Exception caught in getVersionInfoCPLD.",
120 phosphor::logging::entry("MSG=%s", e.what()));
121 return "";
122 }
123}
124
125int getProvisioningStatus(bool& ufmLocked, bool& ufmProvisioned)
126{
127 try
128 {
129 I2CFile cpldDev(i2cBusNumber, i2cSlaveAddress, O_RDWR | O_CLOEXEC);
130 uint8_t provStatus = cpldDev.i2cReadByteData(provisioningStatus);
131 ufmLocked = (provStatus & ufmLockedMask);
132 ufmProvisioned = (provStatus & ufmProvisionedMask);
133 return 0;
134 }
135 catch (const std::exception& e)
136 {
137 phosphor::logging::log<phosphor::logging::level::ERR>(
138 "Exception caught in getProvisioningStatus.",
139 phosphor::logging::entry("MSG=%s", e.what()));
140 return -1;
141 }
142}
143
AppaRao Puli88aa33b2019-07-18 23:49:55 +0530144int readCpldReg(const ActionType& action, uint8_t value)
145{
146 uint8_t cpldReg;
147
148 switch (action)
149 {
150 case (ActionType::recoveryCount):
151 cpldReg = recoveryCount;
152 break;
153 case (ActionType::recoveryReason):
154 cpldReg = lastRecoveryReason;
155 break;
156 case (ActionType::panicCount):
157 cpldReg = panicEventCount;
158 break;
159 case (ActionType::panicReason):
160 cpldReg = panicEventReason;
161 break;
162 case (ActionType::majorError):
163 cpldReg = majorErrorCode;
164 break;
165 case (ActionType::minorError):
166 cpldReg = minorErrorCode;
167 break;
168
169 default:
170 phosphor::logging::log<phosphor::logging::level::ERR>(
171 "Invalid CPLD read action.");
172 return -1;
173 }
174
175 try
176 {
177 I2CFile cpldDev(i2cBusNumber, i2cSlaveAddress, O_RDWR | O_CLOEXEC);
178 value = cpldDev.i2cReadByteData(cpldReg);
179 return 0;
180 }
181 catch (const std::exception& e)
182 {
183 phosphor::logging::log<phosphor::logging::level::ERR>(
184 "Exception caught in readCpldReg.",
185 phosphor::logging::entry("MSG=%s", e.what()));
186 return -1;
187 }
188}
189
AppaRao Puli46cead92019-07-22 16:50:09 +0530190int setBMCBootCheckpoint(const uint8_t checkPoint)
191{
192 try
193 {
194 I2CFile cpldDev(i2cBusNumber, i2cSlaveAddress, O_RDWR | O_CLOEXEC);
195 cpldDev.i2cWriteByteData(bmcBootCheckpoint, checkPoint);
196 return 0;
197 }
198 catch (const std::exception& e)
199 {
200 phosphor::logging::log<phosphor::logging::level::ERR>(
201 "Exception caught in setBMCBootCheckout.",
202 phosphor::logging::entry("MSG=%s", e.what()));
203 return -1;
204 }
205}
206
AppaRao Pulic532f552019-07-05 15:23:50 +0530207} // namespace pfr
208} // namespace intel