Andrew Jeffery | 4fe996c | 2018-02-27 12:16:48 +1030 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (C) 2018 IBM Corp. |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 3 | |
| 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" |
| 29 | #include "mboxd_lpc.h" |
Andrew Jeffery | eebc6bd | 2018-08-08 10:38:19 +0930 | [diff] [blame] | 30 | #include "flash.h" |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 31 | #include <linux/aspeed-lpc-ctrl.h> |
| 32 | |
| 33 | #define LPC_CTRL_PATH "/dev/aspeed-lpc-ctrl" |
| 34 | |
Andrew Jeffery | 07a7684 | 2017-04-12 14:09:06 +0930 | [diff] [blame] | 35 | int __init_lpc_dev(struct mbox_context *context, const char *path) |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 36 | { |
| 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 Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 48 | MSG_DBG("Opening %s\n", path); |
Andrew Jeffery | 07a7684 | 2017-04-12 14:09:06 +0930 | [diff] [blame] | 49 | fd = open(path, O_RDWR | O_SYNC); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 50 | if (fd < 0) { |
| 51 | MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n", |
Andrew Jeffery | 07a7684 | 2017-04-12 14:09:06 +0930 | [diff] [blame] | 52 | path, strerror(errno)); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 53 | return -errno; |
| 54 | } |
| 55 | |
| 56 | context->fds[LPC_CTRL_FD].fd = fd; |
| 57 | |
| 58 | /* Find Size of Reserved Memory Region */ |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 59 | MSG_DBG("Getting buffer size...\n"); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 60 | 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 Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 69 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 70 | /* mmap the Reserved Memory Region */ |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 71 | MSG_DBG("Mapping in 0x%.8x bytes of %s\n", context->mem_size, path); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 72 | context->mem = mmap(NULL, context->mem_size, PROT_READ | PROT_WRITE, |
| 73 | MAP_SHARED, fd, 0); |
| 74 | if (context->mem == MAP_FAILED) { |
Andrew Jeffery | 093d65a | 2017-04-24 12:34:56 +0930 | [diff] [blame] | 75 | MSG_ERR("Failed to map %s: %s\n", path, strerror(errno)); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 76 | return -errno; |
| 77 | } |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
Andrew Jeffery | 07a7684 | 2017-04-12 14:09:06 +0930 | [diff] [blame] | 82 | int init_lpc_dev(struct mbox_context *context) |
| 83 | { |
| 84 | return __init_lpc_dev(context, LPC_CTRL_PATH); |
| 85 | } |
| 86 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 87 | void free_lpc_dev(struct mbox_context *context) |
| 88 | { |
| 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 | */ |
| 101 | int 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 Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 125 | MSG_INFO("Pointing HOST LPC bus at the flash\n"); |
| 126 | MSG_INFO("Assuming %dMB of flash: HOST LPC 0x%08x\n", |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 127 | 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 | */ |
| 141 | return set_flash_bytemap(context, 0, context->flash_size, FLASH_DIRTY); |
| 142 | } |
| 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 | */ |
| 150 | int 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 Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 165 | MSG_INFO("Pointing HOST LPC bus at memory region %p of size 0x%.8x\n", |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 166 | context->mem, context->mem_size); |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 167 | MSG_INFO("LPC address 0x%.8x\n", map.addr); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 168 | |
| 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 | } |