1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
/* Copyright 2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define _GNU_SOURCE
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <endian.h>
#include "bitutils.h"
#include "operations.h"
#include "hwunit.h"
#define OPENFSI_LEGACY_PATH "/sys/bus/platform/devices/gpio-fsi/"
#define OPENFSI_PATH "/sys/class/fsi-master/"
const char *fsi_base;
const char *kernel_get_fsi_path(void)
{
int rc;
if (fsi_base)
return fsi_base;
rc = access(OPENFSI_PATH, F_OK);
if (rc == 0) {
fsi_base = OPENFSI_PATH;
return fsi_base;
}
rc = access(OPENFSI_LEGACY_PATH, F_OK);
if (rc == 0) {
fsi_base = OPENFSI_LEGACY_PATH;
return fsi_base;
}
/* This is an error, but callers use this function when probing */
PR_DEBUG("Failed to find kernel FSI path\n");
return NULL;
}
static int kernel_fsi_getcfam(struct fsi *fsi, uint32_t addr64, uint32_t *value)
{
int rc;
uint32_t tmp, addr = (addr64 & 0x7ffc00) | ((addr64 & 0x3ff) << 2);
rc = lseek(fsi->fd, addr, SEEK_SET);
if (rc < 0) {
rc = errno;
PR_WARNING("seek failed: %s\n", strerror(errno));
return rc;
}
rc = read(fsi->fd, &tmp, 4);
if (rc < 0) {
rc = errno;
if ((addr64 & 0xfff) != 0xc09)
/* We expect reads of 0xc09 to occasionally
* fail as the probing code uses it to see
* if anything is present on the link. */
PR_ERROR("Failed to read from 0x%08" PRIx32 " (%016" PRIx32 ")\n", (uint32_t) addr, addr64);
return rc;
}
*value = be32toh(tmp);
return 0;
}
static int kernel_fsi_putcfam(struct fsi *fsi, uint32_t addr64, uint32_t data)
{
int rc;
uint32_t tmp, addr = (addr64 & 0x7ffc00) | ((addr64 & 0x3ff) << 2);
rc = lseek(fsi->fd, addr, SEEK_SET);
if (rc < 0) {
rc = errno;
PR_WARNING("seek failed: %s\n", strerror(errno));
return rc;
}
tmp = htobe32(data);
rc = write(fsi->fd, &tmp, 4);
if (rc < 0) {
rc = errno;
PR_ERROR("Failed to write to 0x%08" PRIx32 " (%016" PRIx32 ")\n", addr, addr64);
return rc;
}
return 0;
}
static void kernel_fsi_scan_devices(void)
{
const char one = '1';
const char *kernel_path = kernel_get_fsi_path();
char *path;
int rc, fd;
if (!kernel_path)
return;
rc = asprintf(&path, "%s/fsi0/rescan", kernel_path);
if (rc < 0) {
PR_ERROR("Unable to create fsi path\n");
return;
}
fd = open(path, O_WRONLY | O_SYNC);
if (fd < 0) {
PR_ERROR("Unable to open %s\n", path);
free(path);
return;
}
rc = write(fd, &one, sizeof(one));
if (rc < 0)
PR_ERROR("Unable to write to %s\n", path);
free(path);
close(fd);
}
int kernel_fsi_probe(struct pdbg_target *target)
{
struct fsi *fsi = target_to_fsi(target);
int tries = 5;
int rc;
const char *kernel_path = kernel_get_fsi_path();
const char *fsi_path;
char *path;
static bool first_probe = true;
if (!kernel_path)
return -1;
fsi_path = pdbg_target_property(target, "device-path", NULL);
assert(fsi_path);
rc = asprintf(&path, "%s%s", kernel_get_fsi_path(), fsi_path);
if (rc < 0) {
PR_ERROR("Unable to create fsi path\n");
return rc;
}
while (tries) {
fsi->fd = open(path, O_RDWR | O_SYNC);
if (fsi->fd >= 0) {
free(path);
first_probe = false;
return 0;
}
tries--;
/*
* On fsi bus rescan, kernel re-creates all the slave device
* entries. It means any currently open devices will be
* invalid and need to be re-opened. So avoid scanning if
* some devices are already probed.
*/
if (first_probe) {
kernel_fsi_scan_devices();
sleep(1);
} else {
break;
}
}
PR_INFO("Unable to open %s\n", path);
free(path);
return -1;
}
static void kernel_fsi_release(struct pdbg_target *target)
{
struct fsi *fsi = target_to_fsi(target);
if (fsi->fd != -1) {
close(fsi->fd);
fsi->fd = -1;
}
}
static struct fsi kernel_fsi = {
.target = {
.name = "Kernel based FSI master",
.compatible = "ibm,kernel-fsi",
.class = "fsi",
.probe = kernel_fsi_probe,
.release = kernel_fsi_release,
},
.read = kernel_fsi_getcfam,
.write = kernel_fsi_putcfam,
};
DECLARE_HW_UNIT(kernel_fsi);
static int kernel_pib_getscom(struct pib *pib, uint64_t addr, uint64_t *value)
{
int rc;
rc = pread(pib->fd, value, 8, addr);
if (rc < 0) {
rc = errno;
PR_DEBUG("Failed to read scom addr 0x%016"PRIx64"\n", addr);
return rc;
}
return 0;
}
static int kernel_pib_putscom(struct pib *pib, uint64_t addr, uint64_t value)
{
int rc;
rc = pwrite(pib->fd, &value, 8, addr);
if (rc < 0) {
rc = errno;
PR_DEBUG("Failed to write scom addr 0x%016"PRIx64"\n", addr);
return rc;
}
return 0;
}
static int kernel_pib_probe(struct pdbg_target *target)
{
struct pib *pib = target_to_pib(target);
const char *scom_path;
scom_path = pdbg_target_property(target, "device-path", NULL);
assert(scom_path);
pib->fd = open(scom_path, O_RDWR | O_SYNC);
if (pib->fd < 0) {
PR_INFO("Unable to open %s\n", scom_path);
return -1;
}
lseek(pib->fd, 0, SEEK_SET);
return 0;
}
struct pib kernel_pib = {
.target = {
.name = "Kernel based FSI SCOM",
.compatible = "ibm,kernel-pib",
.class = "pib",
.probe = kernel_pib_probe,
},
.read = kernel_pib_getscom,
.write = kernel_pib_putscom,
};
DECLARE_HW_UNIT(kernel_pib);
__attribute__((constructor))
static void register_kernel(void)
{
pdbg_hwunit_register(PDBG_DEFAULT_BACKEND, &kernel_fsi_hw_unit);
pdbg_hwunit_register(PDBG_DEFAULT_BACKEND, &kernel_pib_hw_unit);
}
|