blob: a260c781aba52e0e8049093190822813f3205e24 [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
27#include "mbox.h"
28#include "common.h"
Andrew Jefferycd186112018-08-08 10:47:55 +093029#include "lpc.h"
Andrew Jefferyeebc6bd2018-08-08 10:38:19 +093030#include "flash.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/*
96 * point_to_flash() - Point the lpc bus mapping to the actual flash device
97 * @context: The mbox context pointer
98 *
99 * Return: 0 on success otherwise negative error code
100 */
101int point_to_flash(struct mbox_context *context)
102{
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 */
111 .addr = 0x0FFFFFFF & -context->flash_size,
112 .offset = 0,
113 .size = context->flash_size
114 };
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");
122 return -MBOX_R_PARAM_ERROR;
123 }
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",
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100127 context->flash_size >> 20, map.addr);
128
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));
133 return -MBOX_R_SYSTEM_ERROR;
134 }
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 Jefferyf953f792018-08-08 16:56:03 +0930141 return flash_set_bytemap(context, 0, context->flash_size, FLASH_DIRTY);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100142}
143
144/*
145 * point_to_memory() - Point the lpc bus mapping to the reserved memory region
146 * @context: The mbox context pointer
147 *
148 * Return: 0 on success otherwise negative error code
149 */
150int point_to_memory(struct mbox_context *context)
151{
152 struct aspeed_lpc_ctrl_mapping map = {
153 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
154 .window_id = 0, /* There's only one */
155 .flags = 0,
156 .addr = context->lpc_base,
157 .offset = 0,
158 .size = context->mem_size
159 };
160
161 if (context->state & MAPS_MEM) {
162 return 0; /* LPC Bus already points to reserved memory area */
163 }
164
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000165 MSG_INFO("Pointing HOST LPC bus at memory region %p of size 0x%.8x\n",
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100166 context->mem, context->mem_size);
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000167 MSG_INFO("LPC address 0x%.8x\n", map.addr);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100168
169 if (ioctl(context->fds[LPC_CTRL_FD].fd, ASPEED_LPC_CTRL_IOCTL_MAP,
170 &map)) {
171 MSG_ERR("Failed to point the LPC BUS to memory: %s\n",
172 strerror(errno));
173 return -MBOX_R_SYSTEM_ERROR;
174 }
175
176 /* LPC now maps memory (keep suspended state) */
177 context->state = MAPS_MEM | (context->state & STATE_SUSPENDED);
178
179 return 0;
180}