blob: f3f999ff6ecacde35177614afd82a35554e5c492 [file] [log] [blame]
Andrew Jeffery4fe996c2018-02-27 12:16:48 +10301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +11003
4#define _GNU_SOURCE
5#include <assert.h>
6#include <errno.h>
7#include <fcntl.h>
8#include <getopt.h>
9#include <limits.h>
10#include <poll.h>
11#include <stdbool.h>
12#include <stdint.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <syslog.h>
17#include <signal.h>
18#include <sys/ioctl.h>
19#include <sys/mman.h>
20#include <sys/stat.h>
21#include <sys/timerfd.h>
22#include <sys/types.h>
23#include <time.h>
24#include <unistd.h>
25#include <inttypes.h>
26
Andrew Jeffery26558db2018-08-10 00:22:38 +093027#include "mboxd.h"
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110028#include "common.h"
Andrew Jefferycd186112018-08-08 10:47:55 +093029#include "lpc.h"
Evan Lojewskif1e547c2019-03-14 14:34:33 +103030#include "backend.h"
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110031#include <linux/aspeed-lpc-ctrl.h>
32
33#define LPC_CTRL_PATH "/dev/aspeed-lpc-ctrl"
34
Andrew Jefferycb9b2102018-08-08 16:31:07 +093035int __lpc_dev_init(struct mbox_context *context, const char *path)
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110036{
37 struct aspeed_lpc_ctrl_mapping map = {
38 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
39 .window_id = 0, /* There's only one */
40 .flags = 0,
41 .addr = 0,
42 .offset = 0,
43 .size = 0
44 };
45 int fd;
46
47 /* Open LPC Device */
Suraj Jitindar Singh28519592017-04-27 14:48:58 +100048 MSG_DBG("Opening %s\n", path);
Andrew Jeffery07a76842017-04-12 14:09:06 +093049 fd = open(path, O_RDWR | O_SYNC);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110050 if (fd < 0) {
51 MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n",
Andrew Jeffery07a76842017-04-12 14:09:06 +093052 path, strerror(errno));
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110053 return -errno;
54 }
55
56 context->fds[LPC_CTRL_FD].fd = fd;
57
58 /* Find Size of Reserved Memory Region */
Suraj Jitindar Singh28519592017-04-27 14:48:58 +100059 MSG_DBG("Getting buffer size...\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110060 if (ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map) < 0) {
61 MSG_ERR("Couldn't get lpc control buffer size: %s\n",
62 strerror(errno));
63 return -errno;
64 }
65
66 context->mem_size = map.size;
67 /* Map at the top of the 28-bit LPC firmware address space-0 */
68 context->lpc_base = 0x0FFFFFFF & -context->mem_size;
Suraj Jitindar Singh28519592017-04-27 14:48:58 +100069
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110070 /* mmap the Reserved Memory Region */
Suraj Jitindar Singh28519592017-04-27 14:48:58 +100071 MSG_DBG("Mapping in 0x%.8x bytes of %s\n", context->mem_size, path);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110072 context->mem = mmap(NULL, context->mem_size, PROT_READ | PROT_WRITE,
73 MAP_SHARED, fd, 0);
74 if (context->mem == MAP_FAILED) {
Andrew Jeffery093d65a2017-04-24 12:34:56 +093075 MSG_ERR("Failed to map %s: %s\n", path, strerror(errno));
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110076 return -errno;
77 }
78
79 return 0;
80}
81
Andrew Jefferycb9b2102018-08-08 16:31:07 +093082int lpc_dev_init(struct mbox_context *context)
Andrew Jeffery07a76842017-04-12 14:09:06 +093083{
Andrew Jefferycb9b2102018-08-08 16:31:07 +093084 return __lpc_dev_init(context, LPC_CTRL_PATH);
Andrew Jeffery07a76842017-04-12 14:09:06 +093085}
86
Andrew Jeffery2e2df282018-08-08 16:32:22 +093087void lpc_dev_free(struct mbox_context *context)
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110088{
89 if (context->mem) {
90 munmap(context->mem, context->mem_size);
91 }
92 close(context->fds[LPC_CTRL_FD].fd);
93}
94
95/*
Andrew Jefferyec0f2302018-08-08 16:33:27 +093096 * lpc_map_flash() - Point the lpc bus mapping to the actual flash device
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110097 * @context: The mbox context pointer
98 *
99 * Return: 0 on success otherwise negative error code
100 */
Andrew Jefferyec0f2302018-08-08 16:33:27 +0930101int lpc_map_flash(struct mbox_context *context)
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100102{
103 struct aspeed_lpc_ctrl_mapping map = {
104 .window_type = ASPEED_LPC_CTRL_WINDOW_FLASH,
105 .window_id = 0, /* Theres only one */
106 .flags = 0,
107 /*
108 * The mask is because the top nibble is the host LPC FW space,
109 * we want space 0.
110 */
Evan Lojewskif1e547c2019-03-14 14:34:33 +1030111 .addr = 0x0FFFFFFF & -context->backend.flash_size,
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100112 .offset = 0,
Evan Lojewskif1e547c2019-03-14 14:34:33 +1030113 .size = context->backend.flash_size
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100114 };
115
116 if (context->state & MAPS_FLASH) {
117 return 0; /* LPC Bus already points to flash */
118 }
119 /* Don't let the host access flash while we're suspended */
120 if (context->state & STATE_SUSPENDED) {
121 MSG_ERR("Can't point lpc mapping to flash while suspended\n");
Andrew Jeffery8eab2152018-08-09 23:47:29 +0930122 return -EBUSY;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100123 }
124
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000125 MSG_INFO("Pointing HOST LPC bus at the flash\n");
126 MSG_INFO("Assuming %dMB of flash: HOST LPC 0x%08x\n",
Evan Lojewskif1e547c2019-03-14 14:34:33 +1030127 context->backend.flash_size >> 20, map.addr);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100128
129 if (ioctl(context->fds[LPC_CTRL_FD].fd, ASPEED_LPC_CTRL_IOCTL_MAP, &map)
130 == -1) {
131 MSG_ERR("Failed to point the LPC BUS at the actual flash: %s\n",
132 strerror(errno));
Andrew Jeffery8eab2152018-08-09 23:47:29 +0930133 return -errno;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100134 }
135
136 context->state = ACTIVE_MAPS_FLASH;
137 /*
138 * Since the host now has access to the flash it can change it out from
139 * under us
140 */
Andrew Jeffery0297e5b2019-03-14 16:36:27 +1030141 return backend_set_bytemap(&context->backend, 0,
142 context->backend.flash_size, FLASH_DIRTY);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100143}
144
145/*
Andrew Jeffery17079d12018-08-08 16:35:09 +0930146 * lpc_map_memory() - Point the lpc bus mapping to the reserved memory region
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100147 * @context: The mbox context pointer
148 *
149 * Return: 0 on success otherwise negative error code
150 */
Andrew Jeffery17079d12018-08-08 16:35:09 +0930151int lpc_map_memory(struct mbox_context *context)
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100152{
153 struct aspeed_lpc_ctrl_mapping map = {
154 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
155 .window_id = 0, /* There's only one */
156 .flags = 0,
157 .addr = context->lpc_base,
158 .offset = 0,
159 .size = context->mem_size
160 };
161
162 if (context->state & MAPS_MEM) {
163 return 0; /* LPC Bus already points to reserved memory area */
164 }
165
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000166 MSG_INFO("Pointing HOST LPC bus at memory region %p of size 0x%.8x\n",
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100167 context->mem, context->mem_size);
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000168 MSG_INFO("LPC address 0x%.8x\n", map.addr);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100169
170 if (ioctl(context->fds[LPC_CTRL_FD].fd, ASPEED_LPC_CTRL_IOCTL_MAP,
171 &map)) {
172 MSG_ERR("Failed to point the LPC BUS to memory: %s\n",
173 strerror(errno));
Andrew Jeffery1e531af2018-08-07 13:32:57 +0930174 return -errno;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100175 }
176
177 /* LPC now maps memory (keep suspended state) */
178 context->state = MAPS_MEM | (context->state & STATE_SUSPENDED);
179
180 return 0;
181}