blob: 472f4dfc05accb5a71076063197ab34010c23c83 [file] [log] [blame]
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +11001/*
2 * Mailbox Daemon LPC Helpers
3 *
4 * Copyright 2016 IBM
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20#define _GNU_SOURCE
21#include <assert.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <getopt.h>
25#include <limits.h>
26#include <poll.h>
27#include <stdbool.h>
28#include <stdint.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <syslog.h>
33#include <signal.h>
34#include <sys/ioctl.h>
35#include <sys/mman.h>
36#include <sys/stat.h>
37#include <sys/timerfd.h>
38#include <sys/types.h>
39#include <time.h>
40#include <unistd.h>
41#include <inttypes.h>
42
43#include "mbox.h"
44#include "common.h"
45#include "mboxd_lpc.h"
46#include "mboxd_flash.h"
47#include <linux/aspeed-lpc-ctrl.h>
48
49#define LPC_CTRL_PATH "/dev/aspeed-lpc-ctrl"
50
Andrew Jeffery07a76842017-04-12 14:09:06 +093051int __init_lpc_dev(struct mbox_context *context, const char *path)
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110052{
53 struct aspeed_lpc_ctrl_mapping map = {
54 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
55 .window_id = 0, /* There's only one */
56 .flags = 0,
57 .addr = 0,
58 .offset = 0,
59 .size = 0
60 };
61 int fd;
62
63 /* Open LPC Device */
Andrew Jeffery07a76842017-04-12 14:09:06 +093064 MSG_OUT("Opening %s\n", path);
65 fd = open(path, O_RDWR | O_SYNC);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110066 if (fd < 0) {
67 MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n",
Andrew Jeffery07a76842017-04-12 14:09:06 +093068 path, strerror(errno));
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110069 return -errno;
70 }
71
72 context->fds[LPC_CTRL_FD].fd = fd;
73
74 /* Find Size of Reserved Memory Region */
75 MSG_OUT("Getting buffer size...\n");
76 if (ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map) < 0) {
77 MSG_ERR("Couldn't get lpc control buffer size: %s\n",
78 strerror(errno));
79 return -errno;
80 }
81
82 context->mem_size = map.size;
83 /* Map at the top of the 28-bit LPC firmware address space-0 */
84 context->lpc_base = 0x0FFFFFFF & -context->mem_size;
85
86 /* mmap the Reserved Memory Region */
Andrew Jeffery07a76842017-04-12 14:09:06 +093087 MSG_OUT("Mapping %s for %u\n", path, context->mem_size);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110088 context->mem = mmap(NULL, context->mem_size, PROT_READ | PROT_WRITE,
89 MAP_SHARED, fd, 0);
90 if (context->mem == MAP_FAILED) {
91 MSG_ERR("Didn't manage to mmap %s: %s\n", LPC_CTRL_PATH,
92 strerror(errno));
93 return -errno;
94 }
95
96 return 0;
97}
98
Andrew Jeffery07a76842017-04-12 14:09:06 +093099int init_lpc_dev(struct mbox_context *context)
100{
101 return __init_lpc_dev(context, LPC_CTRL_PATH);
102}
103
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100104void free_lpc_dev(struct mbox_context *context)
105{
106 if (context->mem) {
107 munmap(context->mem, context->mem_size);
108 }
109 close(context->fds[LPC_CTRL_FD].fd);
110}
111
112/*
113 * point_to_flash() - Point the lpc bus mapping to the actual flash device
114 * @context: The mbox context pointer
115 *
116 * Return: 0 on success otherwise negative error code
117 */
118int point_to_flash(struct mbox_context *context)
119{
120 struct aspeed_lpc_ctrl_mapping map = {
121 .window_type = ASPEED_LPC_CTRL_WINDOW_FLASH,
122 .window_id = 0, /* Theres only one */
123 .flags = 0,
124 /*
125 * The mask is because the top nibble is the host LPC FW space,
126 * we want space 0.
127 */
128 .addr = 0x0FFFFFFF & -context->flash_size,
129 .offset = 0,
130 .size = context->flash_size
131 };
132
133 if (context->state & MAPS_FLASH) {
134 return 0; /* LPC Bus already points to flash */
135 }
136 /* Don't let the host access flash while we're suspended */
137 if (context->state & STATE_SUSPENDED) {
138 MSG_ERR("Can't point lpc mapping to flash while suspended\n");
139 return -MBOX_R_PARAM_ERROR;
140 }
141
142 MSG_OUT("Pointing HOST LPC bus at the actual flash\n");
143 MSG_OUT("Assuming %dMB of flash: HOST LPC 0x%08x\n",
144 context->flash_size >> 20, map.addr);
145
146 if (ioctl(context->fds[LPC_CTRL_FD].fd, ASPEED_LPC_CTRL_IOCTL_MAP, &map)
147 == -1) {
148 MSG_ERR("Failed to point the LPC BUS at the actual flash: %s\n",
149 strerror(errno));
150 return -MBOX_R_SYSTEM_ERROR;
151 }
152
153 context->state = ACTIVE_MAPS_FLASH;
154 /*
155 * Since the host now has access to the flash it can change it out from
156 * under us
157 */
158 return set_flash_bytemap(context, 0, context->flash_size, FLASH_DIRTY);
159}
160
161/*
162 * point_to_memory() - Point the lpc bus mapping to the reserved memory region
163 * @context: The mbox context pointer
164 *
165 * Return: 0 on success otherwise negative error code
166 */
167int point_to_memory(struct mbox_context *context)
168{
169 struct aspeed_lpc_ctrl_mapping map = {
170 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
171 .window_id = 0, /* There's only one */
172 .flags = 0,
173 .addr = context->lpc_base,
174 .offset = 0,
175 .size = context->mem_size
176 };
177
178 if (context->state & MAPS_MEM) {
179 return 0; /* LPC Bus already points to reserved memory area */
180 }
181
182 MSG_OUT("Pointing HOST LPC bus at memory region %p of size 0x%.8x\n",
183 context->mem, context->mem_size);
184 MSG_OUT("LPC address 0x%.8x\n", map.addr);
185
186 if (ioctl(context->fds[LPC_CTRL_FD].fd, ASPEED_LPC_CTRL_IOCTL_MAP,
187 &map)) {
188 MSG_ERR("Failed to point the LPC BUS to memory: %s\n",
189 strerror(errno));
190 return -MBOX_R_SYSTEM_ERROR;
191 }
192
193 /* LPC now maps memory (keep suspended state) */
194 context->state = MAPS_MEM | (context->state & STATE_SUSPENDED);
195
196 return 0;
197}