blob: 195c1f71046aff911e44790860ffce77c8607adf [file] [log] [blame]
Murulidhar Nataraju207bed42017-03-31 07:38:55 -05001#include <iostream>
2#include <stdexcept>
3#include <array>
Murulidhar Nataraju06a0c2c2017-07-11 08:55:33 -05004#include <endian.h>
Murulidhar Nataraju207bed42017-03-31 07:38:55 -05005#include "sbe_interfaces.hpp"
Murulidhar Nataraju1adec022017-04-20 12:05:51 -05006#include "sbe_chipOp_handler.hpp"
Murulidhar Nataraju207bed42017-03-31 07:38:55 -05007
8namespace openpower
9{
10namespace sbe
11{
12
Murulidhar Nataraju1adec022017-04-20 12:05:51 -050013constexpr size_t RESP_HEADER_LEN = 0x3;
14
Murulidhar Nataraju207bed42017-03-31 07:38:55 -050015//Helper interfaces
16static inline uint32_t upper(uint64_t value)
17{
18 return ((value & 0xFFFFFFFF00000000ull) >> 32);
19}
20
21static inline uint32_t lower(uint64_t value)
22{
23 return (value & 0xFFFFFFFF);
24}
25
26using sbe_word_t = uint32_t;
27
28namespace scom
29{
30
31//Constants specific to SCOM operations
32static constexpr sbe_word_t READ_OPCODE = 0x0000A201;
33static constexpr sbe_word_t WRITE_OPCODE = 0x0000A202;
34static constexpr size_t READ_CMD_LENGTH = 0x4;
35static constexpr size_t WRITE_CMD_LENGTH = 0x6;
Murulidhar Nataraju1adec022017-04-20 12:05:51 -050036static constexpr size_t READ_RESP_LENGTH = 0x2;
Murulidhar Nataraju207bed42017-03-31 07:38:55 -050037
38//Reading SCOM Registers
39uint64_t read(const char* devPath,
40 uint64_t address)
41{
42 uint64_t value = 0;
43
44 //Validate input device path
45 if (devPath == nullptr)
46 {
47 throw std::runtime_error("NULL FIFO device path");
48 }
49
Murulidhar Nataraju06a0c2c2017-07-11 08:55:33 -050050 //Build SCOM read request command.
51 //Handle byte order mismatch ,SBE is big endian and BMC is
52 //little endian.
Murulidhar Nataraju207bed42017-03-31 07:38:55 -050053 std::array<sbe_word_t, READ_CMD_LENGTH> command =
54 {
Benjamin Herrenschmidt7a6479f2018-05-17 15:25:24 +100055 static_cast<sbe_word_t>(htobe32(READ_CMD_LENGTH)),
Murulidhar Nataraju06a0c2c2017-07-11 08:55:33 -050056 htobe32(READ_OPCODE),
57 htobe32(upper(address)),
58 htobe32(lower(address))
Murulidhar Nataraju207bed42017-03-31 07:38:55 -050059 };
60
Murulidhar Nataraju1adec022017-04-20 12:05:51 -050061 //Buffer to hold the response data along with the SBE header
62 const size_t respLength = RESP_HEADER_LEN + READ_RESP_LENGTH ;
63 std::array<sbe_word_t, respLength> response = {};
Murulidhar Nataraju207bed42017-03-31 07:38:55 -050064
Murulidhar Nataraju1adec022017-04-20 12:05:51 -050065 //Write the command buffer to the SBE FIFO and obtain the response from the
66 //SBE FIFO device.This interface will parse the obtained SBE response and
67 //any internal SBE failures will be communicated via exceptions
68 invokeSBEChipOperation(devPath, command, response);
Murulidhar Nataraju207bed42017-03-31 07:38:55 -050069
Murulidhar Nataraju1adec022017-04-20 12:05:51 -050070 value = (((static_cast<uint64_t>(response[0])) << 32) | response[1]);
Murulidhar Nataraju207bed42017-03-31 07:38:55 -050071 return value;
72}
73
74void write(const char* devPath,
75 uint64_t address,
76 uint64_t data)
77{
78 //Validate input device path
79 if (devPath == nullptr)
80 {
81 throw std::runtime_error("NULL FIFO device path");
82 }
83
84 //Build SCOM write request command
Murulidhar Nataraju06a0c2c2017-07-11 08:55:33 -050085 //Handle byte order mismatch, SBE is big endian and BMC is
86 //little endian.
Murulidhar Nataraju207bed42017-03-31 07:38:55 -050087 std::array<sbe_word_t, WRITE_CMD_LENGTH> command =
88 {
Benjamin Herrenschmidt7a6479f2018-05-17 15:25:24 +100089 static_cast<sbe_word_t>(htobe32(WRITE_CMD_LENGTH)),
Murulidhar Nataraju06a0c2c2017-07-11 08:55:33 -050090 htobe32(WRITE_OPCODE),
91 htobe32(upper(address)),
92 htobe32(lower(address)),
93 htobe32(upper(data)),
94 htobe32(lower(data))
Murulidhar Nataraju207bed42017-03-31 07:38:55 -050095 };
96
Murulidhar Nataraju1adec022017-04-20 12:05:51 -050097 //Buffer to hold the SBE response status
98 const size_t respLength = RESP_HEADER_LEN;
99 std::array<sbe_word_t, respLength> response = {};
Murulidhar Nataraju207bed42017-03-31 07:38:55 -0500100
Murulidhar Nataraju1adec022017-04-20 12:05:51 -0500101 //Write the command buffer to the SBE FIFO and obtain the response from the
102 //SBE FIFO device.This interface will parse the obtained SBE response and
103 //any internal SBE failures will be communicated via exceptions
104 invokeSBEChipOperation(devPath, command, response);
Murulidhar Nataraju207bed42017-03-31 07:38:55 -0500105}
106
107} // namespace scom
108} // namespace sbe
109} // namespace openpower