xref: /openbmc/phosphor-bmc-code-mgmt/cpld/lattice/lattice.hpp (revision 61e12672130e6724256f4fd3806e1032d0112940)
1f6470b5eSDaniel Hsu #pragma once
2f6470b5eSDaniel Hsu #include "common/include/i2c/i2c.hpp"
3f6470b5eSDaniel Hsu 
4f6470b5eSDaniel Hsu #include <chrono>
5f6470b5eSDaniel Hsu #include <iostream>
6f6470b5eSDaniel Hsu #include <string_view>
7f6470b5eSDaniel Hsu #include <utility>
8f6470b5eSDaniel Hsu 
9*61e12672SDaniel Hsu struct cpldInfo
10*61e12672SDaniel Hsu {
11*61e12672SDaniel Hsu     std::string chipName;
12*61e12672SDaniel Hsu     std::vector<uint8_t> deviceId;
13*61e12672SDaniel Hsu };
14*61e12672SDaniel Hsu 
15*61e12672SDaniel Hsu const std::map<std::string, cpldInfo> supportedDeviceMap = {
16*61e12672SDaniel Hsu     {"LatticeLCMXO3LF_2100CFirmware",
17*61e12672SDaniel Hsu      {"LCMXO3LF-2100C", {0x61, 0x2b, 0xb0, 0x43}}},
18*61e12672SDaniel Hsu     {"LatticeLCMXO3LF_4300CFirmware",
19*61e12672SDaniel Hsu      {"LCMXO3LF-4300C", {0x61, 0x2b, 0xc0, 0x43}}},
20*61e12672SDaniel Hsu };
21*61e12672SDaniel Hsu 
CpldLatticeManager(sdbusplus::async::context & ctx,const uint16_t bus,const uint8_t address,const uint8_t * image,size_t imageSize,const std::string & chip,const std::string & target,const bool debugMode)22f6470b5eSDaniel Hsu struct cpldI2cInfo
23f6470b5eSDaniel Hsu {
24f6470b5eSDaniel Hsu     unsigned long int QF; // Quantity of Fuses
25f6470b5eSDaniel Hsu     unsigned int* UFM;    // User Flash Memory
26f6470b5eSDaniel Hsu     unsigned int version;
27f6470b5eSDaniel Hsu     unsigned int checksum;
28f6470b5eSDaniel Hsu     std::vector<uint8_t> cfgData;
29f6470b5eSDaniel Hsu     std::vector<uint8_t> ufmData;
30f6470b5eSDaniel Hsu };
31f6470b5eSDaniel Hsu 
32f6470b5eSDaniel Hsu class CpldLatticeManager
33f6470b5eSDaniel Hsu {
34f6470b5eSDaniel Hsu   public:
35f6470b5eSDaniel Hsu     CpldLatticeManager(sdbusplus::async::context& ctx, const uint16_t bus,
36f6470b5eSDaniel Hsu                        const uint8_t address, const uint8_t* image,
37f6470b5eSDaniel Hsu                        size_t imageSize, const std::string& chip,
38f6470b5eSDaniel Hsu                        const std::string& target, const bool debugMode) :
39f6470b5eSDaniel Hsu         ctx(ctx), image(image), imageSize(imageSize), chip(chip),
40f6470b5eSDaniel Hsu         target(target), debugMode(debugMode),
41f6470b5eSDaniel Hsu         i2cInterface(phosphor::i2c::I2C(bus, address))
42f6470b5eSDaniel Hsu     {}
43f6470b5eSDaniel Hsu     sdbusplus::async::task<bool> updateFirmware(
44f6470b5eSDaniel Hsu         std::function<bool(int)> progressCallBack);
45f6470b5eSDaniel Hsu     sdbusplus::async::task<bool> getVersion(std::string& version);
46f6470b5eSDaniel Hsu 
47f6470b5eSDaniel Hsu   private:
48f6470b5eSDaniel Hsu     sdbusplus::async::context& ctx;
49f6470b5eSDaniel Hsu     cpldI2cInfo fwInfo{};
50f6470b5eSDaniel Hsu     const uint8_t* image;
51f6470b5eSDaniel Hsu     size_t imageSize;
52f6470b5eSDaniel Hsu     std::string chip;
53f6470b5eSDaniel Hsu     std::string target;
54f6470b5eSDaniel Hsu     bool isLCMXO3D = false;
55f6470b5eSDaniel Hsu     bool debugMode = false;
56f6470b5eSDaniel Hsu     phosphor::i2c::I2C i2cInterface;
57f6470b5eSDaniel Hsu 
58f6470b5eSDaniel Hsu     sdbusplus::async::task<bool> XO2XO3FamilyUpdate(
59f6470b5eSDaniel Hsu         std::function<bool(int)> progressCallBack);
60f6470b5eSDaniel Hsu 
61f6470b5eSDaniel Hsu     int indexof(const char* str, const char* ptn);
62f6470b5eSDaniel Hsu     bool jedFileParser();
63f6470b5eSDaniel Hsu     bool verifyChecksum();
64f6470b5eSDaniel Hsu     sdbusplus::async::task<bool> readDeviceId();
65f6470b5eSDaniel Hsu     sdbusplus::async::task<bool> enableProgramMode();
66f6470b5eSDaniel Hsu     sdbusplus::async::task<bool> eraseFlash();
67f6470b5eSDaniel Hsu     sdbusplus::async::task<bool> resetConfigFlash();
68f6470b5eSDaniel Hsu     sdbusplus::async::task<bool> writeProgramPage();
69f6470b5eSDaniel Hsu     sdbusplus::async::task<bool> programUserCode();
70f6470b5eSDaniel Hsu     sdbusplus::async::task<bool> programDone();
71f6470b5eSDaniel Hsu     sdbusplus::async::task<bool> disableConfigInterface();
72f6470b5eSDaniel Hsu     sdbusplus::async::task<bool> readBusyFlag(uint8_t& busyFlag);
73f6470b5eSDaniel Hsu     sdbusplus::async::task<bool> readStatusReg(uint8_t& statusReg);
74f6470b5eSDaniel Hsu     sdbusplus::async::task<bool> waitBusyAndVerify();
75f6470b5eSDaniel Hsu     sdbusplus::async::task<bool> readUserCode(uint32_t& userCode);
76f6470b5eSDaniel Hsu };
77