blob: dabea8a801969794b0e934c7f85a1a46859320d3 [file] [log] [blame]
Patrick Venture22e38752018-11-21 08:52:49 -08001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Patrick Venturee7728422018-11-14 20:16:33 -080017#include "lpc_aspeed.hpp"
18
Patrick Venture5251da92019-01-17 11:25:26 -080019#include "window_hw_interface.hpp"
Patrick Venturee7728422018-11-14 20:16:33 -080020
Patrick Venture7b91cbc2018-11-28 14:24:41 -080021#include <fcntl.h>
22#include <linux/aspeed-lpc-ctrl.h>
23#include <linux/kernel.h>
24
Patrick Venturee7728422018-11-14 20:16:33 -080025#include <cstdint>
Patrick Venture7b91cbc2018-11-28 14:24:41 -080026#include <cstring>
Patrick Venturee7728422018-11-14 20:16:33 -080027#include <memory>
Patrick Venturebf064632019-01-17 12:20:21 -080028#include <string>
Patrick Venturee7728422018-11-14 20:16:33 -080029#include <utility>
30
31namespace blobs
32{
33
Patrick Venturebf064632019-01-17 12:20:21 -080034const std::string LpcMapperAspeed::lpcControlPath = "/dev/aspeed-lpc-ctrl";
35
Patrick Venture5251da92019-01-17 11:25:26 -080036std::unique_ptr<HardwareMapperInterface>
Patrick Venture78b1a662019-01-17 12:38:26 -080037 LpcMapperAspeed::createAspeedMapper(std::uint32_t regionAddress,
38 std::size_t regionSize)
Patrick Venturee7728422018-11-14 20:16:33 -080039{
40 /* NOTE: considered using a joint factory to create one or the other, for
41 * now, separate factories.
42 */
Patrick Venture78b1a662019-01-17 12:38:26 -080043 return std::make_unique<LpcMapperAspeed>(regionAddress, regionSize);
Patrick Venturee7728422018-11-14 20:16:33 -080044}
45
46std::pair<std::uint32_t, std::uint32_t>
47 LpcMapperAspeed::mapWindow(std::uint32_t address, std::uint32_t length)
48{
Patrick Venture7b91cbc2018-11-28 14:24:41 -080049 static const std::uint32_t MASK_64K = 0xFFFFU;
50 const std::uint32_t offset = address & MASK_64K;
51
52 if (offset + length > regionSize)
53 {
54 std::fprintf(stderr,
55 "requested window size %" PRIu32 ", offset %#" PRIx32
56 " is too large for mem region"
57 " of size %zu\n",
58 length, offset, regionSize);
59 /* TODO: need to throw an exception at this point to store the data to
60 * provide an EBIG response later.
61 */
62 /* *windowSize = regionSize - offset; */
63 return std::make_pair(0, 0);
64 }
65
66 struct aspeed_lpc_ctrl_mapping map = {
67 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
68 .window_id = 0,
69 .flags = 0,
70 .addr = address & ~MASK_64K,
71 .offset = 0,
72 .size = __ALIGN_KERNEL_MASK(offset + length, MASK_64K),
73 };
74
75 std::fprintf(stderr,
76 "requesting Aspeed LPC window at %#" PRIx32 " of size %" PRIu32
77 "\n",
78 map.addr, map.size);
79
Patrick Venturebf064632019-01-17 12:20:21 -080080 const auto lpcControlFd = sys->open(lpcControlPath.c_str(), O_RDWR);
Patrick Venture7b91cbc2018-11-28 14:24:41 -080081 if (lpcControlFd == -1)
82 {
83 std::fprintf(stderr,
84 "cannot open Aspeed LPC kernel control dev \"%s\"\n",
Patrick Venturebf064632019-01-17 12:20:21 -080085 lpcControlPath.c_str());
Patrick Venture7b91cbc2018-11-28 14:24:41 -080086 return std::make_pair(0, 0);
87 }
88
89 if (sys->ioctl(lpcControlFd, ASPEED_LPC_CTRL_IOCTL_MAP, &map) == -1)
90 {
91 std::fprintf(stderr, "Failed to ioctl Aspeed LPC map with error %s\n",
92 std::strerror(errno));
93 sys->close(lpcControlFd);
94 return std::make_pair(0, 0);
95 }
96
97 sys->close(lpcControlFd);
98 return std::make_pair(offset, length);
Patrick Venturee7728422018-11-14 20:16:33 -080099}
100
Patrick Venture517710d2019-01-17 11:37:40 -0800101std::vector<std::uint8_t> LpcMapperAspeed::copyFrom(std::uint32_t length)
102{
103 /* TODO: Implement this. */
104 return {};
105}
106
Patrick Venturee7728422018-11-14 20:16:33 -0800107} // namespace blobs