blob: 5f5fb5895beffc317f68502fbaef82ab494b8004 [file] [log] [blame]
Daniel Hsub602aad2025-08-14 10:44:38 +08001#include "lattice_cpld_factory.hpp"
2
3#include "lattice_xo3_cpld.hpp"
Daniel Hsua3902c82025-08-14 11:15:17 +08004#include "lattice_xo5_cpld.hpp"
Daniel Hsub602aad2025-08-14 10:44:38 +08005
6#include <phosphor-logging/lg2.hpp>
7
8namespace phosphor::software::cpld
9{
10
11std::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);
Daniel Hsua3902c82025-08-14 11:15:17 +080031 case latticeChipFamily::XO5:
32 return std::make_unique<LatticeXO5CPLD>(
33 CPLDInterface::ctx, CPLDInterface::bus, CPLDInterface::address,
34 chipModelStr, "CFG0", false);
Daniel Hsub602aad2025-08-14 10:44:38 +080035 default:
36 lg2::error("Unsupported Lattice CPLD chip family: {CHIPMODEL}",
37 "CHIPMODEL", chipModelStr);
38 return nullptr;
39 }
40}
41
42sdbusplus::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
57sdbusplus::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
73namespace
74{
75using namespace phosphor::software::cpld;
76
77// Register all the CPLD type with the CPLD factory
78const 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