xref: /openbmc/phosphor-bmc-code-mgmt/i2c-vr/i2cvr_software_manager.cpp (revision c1b36628298d7799677680e70d455f85acf83650)
1 #include "i2cvr_software_manager.hpp"
2 
3 #include "common/include/dbus_helper.hpp"
4 #include "common/include/software_manager.hpp"
5 #include "i2cvr_device.hpp"
6 #include "vr.hpp"
7 
8 #include <phosphor-logging/lg2.hpp>
9 #include <sdbusplus/async.hpp>
10 #include <sdbusplus/bus.hpp>
11 #include <xyz/openbmc_project/ObjectMapper/client.hpp>
12 
13 #include <cstdint>
14 
15 PHOSPHOR_LOG2_USING;
16 
17 namespace VR = phosphor::software::VR;
18 namespace I2CDevice = phosphor::software::i2c_vr::device;
19 namespace SoftwareInf = phosphor::software;
20 namespace ManagerInf = phosphor::software::manager;
21 
22 const std::string configDBusName = "I2CVR";
23 const std::vector<std::string> emConfigTypes = {
24     "XDPE1X2XXFirmware",    "ISL69269Firmware", "MP2X6XXFirmware",
25     "MP297XFirmware",       "MP5998Firmware",   "RAA22XGen2Firmware",
26     "RAA22XGen3p5Firmware", "TDA38640AFirmware"};
27 
I2CVRSoftwareManager(sdbusplus::async::context & ctx)28 I2CVRSoftwareManager::I2CVRSoftwareManager(sdbusplus::async::context& ctx) :
29     ManagerInf::SoftwareManager(ctx, configDBusName)
30 {}
31 
start()32 void I2CVRSoftwareManager::start()
33 {
34     std::vector<std::string> configIntfs;
35     configIntfs.reserve(emConfigTypes.size());
36     for (auto& name : emConfigTypes)
37     {
38         configIntfs.push_back("xyz.openbmc_project.Configuration." + name);
39     }
40 
41     ctx.spawn(initDevices(configIntfs));
42     ctx.run();
43 }
44 
initDevice(const std::string & service,const std::string & path,SoftwareConfig & config)45 sdbusplus::async::task<bool> I2CVRSoftwareManager::initDevice(
46     const std::string& service, const std::string& path, SoftwareConfig& config)
47 {
48     std::string configIface =
49         "xyz.openbmc_project.Configuration." + config.configType;
50 
51     std::optional<uint64_t> busNum = co_await dbusGetRequiredProperty<uint64_t>(
52         ctx, service, path, configIface, "Bus");
53     std::optional<uint64_t> address =
54         co_await dbusGetRequiredProperty<uint64_t>(ctx, service, path,
55                                                    configIface, "Address");
56     std::optional<std::string> vrChipType =
57         co_await dbusGetRequiredProperty<std::string>(ctx, service, path,
58                                                       configIface, "Type");
59 
60     if (!busNum.has_value() || !address.has_value() || !vrChipType.has_value())
61     {
62         error("missing config property");
63         co_return false;
64     }
65 
66     VR::VRType vrType;
67     if (!VR::stringToEnum(vrChipType.value(), vrType))
68     {
69         error("unknown voltage regulator type: {TYPE}", "TYPE",
70               vrChipType.value());
71         co_return false;
72     }
73 
74     lg2::debug(
75         "[config] Voltage regulator device type: {TYPE} on Bus: {BUS} at Address: {ADDR}",
76         "TYPE", vrChipType.value(), "BUS", busNum.value(), "ADDR",
77         address.value());
78 
79     auto i2cDevice = std::make_unique<I2CDevice::I2CVRDevice>(
80         ctx, vrType, static_cast<uint16_t>(busNum.value()),
81         static_cast<uint16_t>(address.value()), config, this);
82 
83     std::unique_ptr<SoftwareInf::Software> software =
84         std::make_unique<SoftwareInf::Software>(ctx, *i2cDevice);
85 
86     uint32_t sum;
87     if (!(co_await i2cDevice->getVersion(&sum)))
88     {
89         error("unable to obtain Version/CRC from voltage regulator");
90         co_return false;
91     }
92 
93     software->setVersion(std::format("{:X}", sum),
94                          SoftwareInf::SoftwareVersion::VersionPurpose::Other);
95 
96     software->enableUpdate({RequestedApplyTimes::OnReset});
97 
98     i2cDevice->softwareCurrent = std::move(software);
99 
100     devices.insert({config.objectPath, std::move(i2cDevice)});
101 
102     co_return true;
103 }
104 
main()105 int main()
106 {
107     sdbusplus::async::context ctx;
108 
109     I2CVRSoftwareManager i2cVRSoftwareManager(ctx);
110 
111     i2cVRSoftwareManager.start();
112     return 0;
113 }
114