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