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