blob: 25f027ae74ddabbe7bb2925592ae8a060451c480 [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 Venture28abae72018-12-14 09:44:02 -080037 LpcMapperAspeed::createAspeedMapper(std::size_t regionSize)
Patrick Venturee7728422018-11-14 20:16:33 -080038{
39 /* NOTE: considered using a joint factory to create one or the other, for
40 * now, separate factories.
41 */
Patrick Venture7b91cbc2018-11-28 14:24:41 -080042 return std::make_unique<LpcMapperAspeed>(regionSize);
Patrick Venturee7728422018-11-14 20:16:33 -080043}
44
45std::pair<std::uint32_t, std::uint32_t>
46 LpcMapperAspeed::mapWindow(std::uint32_t address, std::uint32_t length)
47{
Patrick Venture7b91cbc2018-11-28 14:24:41 -080048 static const std::uint32_t MASK_64K = 0xFFFFU;
49 const std::uint32_t offset = address & MASK_64K;
50
51 if (offset + length > regionSize)
52 {
53 std::fprintf(stderr,
54 "requested window size %" PRIu32 ", offset %#" PRIx32
55 " is too large for mem region"
56 " of size %zu\n",
57 length, offset, regionSize);
58 /* TODO: need to throw an exception at this point to store the data to
59 * provide an EBIG response later.
60 */
61 /* *windowSize = regionSize - offset; */
62 return std::make_pair(0, 0);
63 }
64
65 struct aspeed_lpc_ctrl_mapping map = {
66 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
67 .window_id = 0,
68 .flags = 0,
69 .addr = address & ~MASK_64K,
70 .offset = 0,
71 .size = __ALIGN_KERNEL_MASK(offset + length, MASK_64K),
72 };
73
74 std::fprintf(stderr,
75 "requesting Aspeed LPC window at %#" PRIx32 " of size %" PRIu32
76 "\n",
77 map.addr, map.size);
78
Patrick Venturebf064632019-01-17 12:20:21 -080079 const auto lpcControlFd = sys->open(lpcControlPath.c_str(), O_RDWR);
Patrick Venture7b91cbc2018-11-28 14:24:41 -080080 if (lpcControlFd == -1)
81 {
82 std::fprintf(stderr,
83 "cannot open Aspeed LPC kernel control dev \"%s\"\n",
Patrick Venturebf064632019-01-17 12:20:21 -080084 lpcControlPath.c_str());
Patrick Venture7b91cbc2018-11-28 14:24:41 -080085 return std::make_pair(0, 0);
86 }
87
88 if (sys->ioctl(lpcControlFd, ASPEED_LPC_CTRL_IOCTL_MAP, &map) == -1)
89 {
90 std::fprintf(stderr, "Failed to ioctl Aspeed LPC map with error %s\n",
91 std::strerror(errno));
92 sys->close(lpcControlFd);
93 return std::make_pair(0, 0);
94 }
95
96 sys->close(lpcControlFd);
97 return std::make_pair(offset, length);
Patrick Venturee7728422018-11-14 20:16:33 -080098}
99
Patrick Venture517710d2019-01-17 11:37:40 -0800100std::vector<std::uint8_t> LpcMapperAspeed::copyFrom(std::uint32_t length)
101{
102 /* TODO: Implement this. */
103 return {};
104}
105
Patrick Venturee7728422018-11-14 20:16:33 -0800106} // namespace blobs