xref: /openbmc/phosphor-bmc-code-mgmt/cpld/lattice/lattice_cpld_factory.cpp (revision b602aad5026847e2a4895598c6b5b7b08a377282)
1 #include "lattice_cpld_factory.hpp"
2 
3 #include "lattice_xo3_cpld.hpp"
4 
5 #include <phosphor-logging/lg2.hpp>
6 
7 namespace phosphor::software::cpld
8 {
9 
10 std::unique_ptr<LatticeBaseCPLD> LatticeCPLDFactory::getLatticeCPLD()
11 {
12     if (supportedDeviceMap.find(chipEnum) == supportedDeviceMap.end())
13     {
14         // invalid
15         lg2::error("Unsupported Lattice CPLD chip enum: {CHIPENUM}", "CHIPENUM",
16                    chipEnum);
17         return nullptr;
18     }
19 
20     auto chipFamily = supportedDeviceMap.at(chipEnum).chipFamily;
21     auto chipModelStr =
22         getLatticeChipStr(chipEnum, latticeStringType::modelString);
23     switch (chipFamily)
24     {
25         case latticeChipFamily::XO2:
26         case latticeChipFamily::XO3:
27             return std::make_unique<LatticeXO3CPLD>(
28                 CPLDInterface::ctx, CPLDInterface::bus, CPLDInterface::address,
29                 chipModelStr, "CFG0", false);
30         default:
31             lg2::error("Unsupported Lattice CPLD chip family: {CHIPMODEL}",
32                        "CHIPMODEL", chipModelStr);
33             return nullptr;
34     }
35 }
36 
37 sdbusplus::async::task<bool> LatticeCPLDFactory::updateFirmware(
38     bool /*force*/, const uint8_t* image, size_t imageSize,
39     std::function<bool(int)> progressCallBack)
40 {
41     lg2::info("Updating Lattice CPLD firmware");
42     auto cpldManager = getLatticeCPLD();
43     if (cpldManager == nullptr)
44     {
45         lg2::error("CPLD manager is not initialized.");
46         co_return false;
47     }
48     co_return co_await cpldManager->updateFirmware(image, imageSize,
49                                                    progressCallBack);
50 }
51 
52 sdbusplus::async::task<bool> LatticeCPLDFactory::getVersion(
53     std::string& version)
54 {
55     lg2::info("Getting Lattice CPLD version");
56     auto cpldManager = getLatticeCPLD();
57     if (cpldManager == nullptr)
58     {
59         lg2::error("CPLD manager is not initialized.");
60         co_return false;
61     }
62     co_return co_await cpldManager->getVersion(version);
63 }
64 
65 } // namespace phosphor::software::cpld
66 
67 // Factory function to create lattice CPLD device
68 namespace
69 {
70 using namespace phosphor::software::cpld;
71 
72 // Register all the CPLD type with the CPLD factory
73 const bool vendorRegistered = [] {
74     for (const auto& [chipEnum, info] : supportedDeviceMap)
75     {
76         auto typeStr =
77             getLatticeChipStr(chipEnum, latticeStringType::typeString);
78         CPLDFactory::instance().registerCPLD(
79             typeStr, [chipEnum](sdbusplus::async::context& ctx,
80                                 const std::string& chipName, uint16_t bus,
81                                 uint8_t address) {
82                 // Create and return a LatticeCPLD instance
83                 // Pass the parameters to the constructor
84                 return std::make_unique<LatticeCPLDFactory>(
85                     ctx, chipName, chipEnum, bus, address);
86             });
87     }
88     return true;
89 }();
90 
91 } // namespace
92