blob: 28f0bb9f8775485949cd67114499981cba856f98 [file] [log] [blame]
Murulidhar Nataraju207bed42017-03-31 07:38:55 -05001#include <iostream>
2#include <stdexcept>
3#include <array>
4#include "sbe_interfaces.hpp"
Murulidhar Nataraju1adec022017-04-20 12:05:51 -05005#include "sbe_chipOp_handler.hpp"
Murulidhar Nataraju207bed42017-03-31 07:38:55 -05006
7namespace openpower
8{
9namespace sbe
10{
11
Murulidhar Nataraju1adec022017-04-20 12:05:51 -050012constexpr size_t RESP_HEADER_LEN = 0x3;
13
Murulidhar Nataraju207bed42017-03-31 07:38:55 -050014//Helper interfaces
15static inline uint32_t upper(uint64_t value)
16{
17 return ((value & 0xFFFFFFFF00000000ull) >> 32);
18}
19
20static inline uint32_t lower(uint64_t value)
21{
22 return (value & 0xFFFFFFFF);
23}
24
25using sbe_word_t = uint32_t;
26
27namespace scom
28{
29
30//Constants specific to SCOM operations
31static constexpr sbe_word_t READ_OPCODE = 0x0000A201;
32static constexpr sbe_word_t WRITE_OPCODE = 0x0000A202;
33static constexpr size_t READ_CMD_LENGTH = 0x4;
34static constexpr size_t WRITE_CMD_LENGTH = 0x6;
Murulidhar Nataraju1adec022017-04-20 12:05:51 -050035static constexpr size_t READ_RESP_LENGTH = 0x2;
Murulidhar Nataraju207bed42017-03-31 07:38:55 -050036
37//Reading SCOM Registers
38uint64_t read(const char* devPath,
39 uint64_t address)
40{
41 uint64_t value = 0;
42
43 //Validate input device path
44 if (devPath == nullptr)
45 {
46 throw std::runtime_error("NULL FIFO device path");
47 }
48
49 //Build SCOM read request command
50 std::array<sbe_word_t, READ_CMD_LENGTH> command =
51 {
52 static_cast<sbe_word_t>(READ_CMD_LENGTH),
53 READ_OPCODE,
54 upper(address),
55 lower(address)
56 };
57
Murulidhar Nataraju1adec022017-04-20 12:05:51 -050058 //Buffer to hold the response data along with the SBE header
59 const size_t respLength = RESP_HEADER_LEN + READ_RESP_LENGTH ;
60 std::array<sbe_word_t, respLength> response = {};
Murulidhar Nataraju207bed42017-03-31 07:38:55 -050061
Murulidhar Nataraju1adec022017-04-20 12:05:51 -050062 //Write the command buffer to the SBE FIFO and obtain the response from the
63 //SBE FIFO device.This interface will parse the obtained SBE response and
64 //any internal SBE failures will be communicated via exceptions
65 invokeSBEChipOperation(devPath, command, response);
Murulidhar Nataraju207bed42017-03-31 07:38:55 -050066
Murulidhar Nataraju1adec022017-04-20 12:05:51 -050067 value = (((static_cast<uint64_t>(response[0])) << 32) | response[1]);
Murulidhar Nataraju207bed42017-03-31 07:38:55 -050068 return value;
69}
70
71void write(const char* devPath,
72 uint64_t address,
73 uint64_t data)
74{
75 //Validate input device path
76 if (devPath == nullptr)
77 {
78 throw std::runtime_error("NULL FIFO device path");
79 }
80
81 //Build SCOM write request command
82 std::array<sbe_word_t, WRITE_CMD_LENGTH> command =
83 {
84 static_cast<sbe_word_t>(WRITE_CMD_LENGTH),
85 WRITE_OPCODE,
86 upper(address),
87 lower(address),
88 upper(data),
89 lower(data)
90 };
91
Murulidhar Nataraju1adec022017-04-20 12:05:51 -050092 //Buffer to hold the SBE response status
93 const size_t respLength = RESP_HEADER_LEN;
94 std::array<sbe_word_t, respLength> response = {};
Murulidhar Nataraju207bed42017-03-31 07:38:55 -050095
Murulidhar Nataraju1adec022017-04-20 12:05:51 -050096 //Write the command buffer to the SBE FIFO and obtain the response from the
97 //SBE FIFO device.This interface will parse the obtained SBE response and
98 //any internal SBE failures will be communicated via exceptions
99 invokeSBEChipOperation(devPath, command, response);
Murulidhar Nataraju207bed42017-03-31 07:38:55 -0500100}
101
102} // namespace scom
103} // namespace sbe
104} // namespace openpower