2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2015-03-05 19:25:29 +00:00
|
|
|
/*
|
|
|
|
* PCI emulation device which swaps the case of text
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Google, Inc
|
|
|
|
* Written by Simon Glass <sjg@chromium.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
2015-05-04 17:31:08 +00:00
|
|
|
#include <errno.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2015-03-05 19:25:29 +00:00
|
|
|
#include <pci.h>
|
|
|
|
#include <asm/test.h>
|
|
|
|
#include <linux/ctype.h>
|
|
|
|
|
|
|
|
/**
|
2020-12-03 23:55:23 +00:00
|
|
|
* struct swap_case_plat - platform data for this device
|
2015-03-05 19:25:29 +00:00
|
|
|
*
|
|
|
|
* @command: Current PCI command value
|
|
|
|
* @bar: Current base address values
|
|
|
|
*/
|
2020-12-03 23:55:23 +00:00
|
|
|
struct swap_case_plat {
|
2015-03-05 19:25:29 +00:00
|
|
|
u16 command;
|
2018-06-12 06:05:02 +00:00
|
|
|
u32 bar[6];
|
2015-03-05 19:25:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
MEM_TEXT_SIZE = 0x100,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum swap_case_op {
|
|
|
|
OP_TO_LOWER,
|
|
|
|
OP_TO_UPPER,
|
|
|
|
OP_SWAP,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct pci_bar {
|
|
|
|
int type;
|
|
|
|
u32 size;
|
|
|
|
} barinfo[] = {
|
|
|
|
{ PCI_BASE_ADDRESS_SPACE_IO, 1 },
|
|
|
|
{ PCI_BASE_ADDRESS_MEM_TYPE_32, MEM_TEXT_SIZE },
|
|
|
|
{ 0, 0 },
|
|
|
|
{ 0, 0 },
|
|
|
|
{ 0, 0 },
|
|
|
|
{ 0, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
struct swap_case_priv {
|
|
|
|
enum swap_case_op op;
|
|
|
|
char mem_text[MEM_TEXT_SIZE];
|
|
|
|
};
|
|
|
|
|
2020-01-27 15:49:37 +00:00
|
|
|
static int sandbox_swap_case_use_ea(const struct udevice *dev)
|
2019-06-07 08:24:24 +00:00
|
|
|
{
|
2020-12-19 17:40:14 +00:00
|
|
|
return !!ofnode_get_property(dev_ofnode(dev), "use-ea", NULL);
|
2019-06-07 08:24:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Please keep these macros in sync with ea_regs below */
|
|
|
|
#define PCI_CAP_ID_EA_SIZE (sizeof(ea_regs) + 4)
|
|
|
|
#define PCI_CAP_ID_EA_ENTRY_CNT 4
|
|
|
|
/* Hardcoded EA structure, excluding 1st DW. */
|
|
|
|
static const u32 ea_regs[] = {
|
|
|
|
/* BEI=0, ES=2, BAR0 32b Base + 32b MaxOffset, I/O space */
|
|
|
|
(2 << 8) | 2,
|
|
|
|
PCI_CAP_EA_BASE_LO0,
|
|
|
|
0,
|
|
|
|
/* BEI=1, ES=2, BAR1 32b Base + 32b MaxOffset */
|
|
|
|
(1 << 4) | 2,
|
|
|
|
PCI_CAP_EA_BASE_LO1,
|
|
|
|
MEM_TEXT_SIZE - 1,
|
|
|
|
/* BEI=2, ES=3, BAR2 64b Base + 32b MaxOffset */
|
|
|
|
(2 << 4) | 3,
|
|
|
|
PCI_CAP_EA_BASE_LO2 | PCI_EA_IS_64,
|
|
|
|
PCI_CAP_EA_SIZE_LO,
|
|
|
|
PCI_CAP_EA_BASE_HI2,
|
|
|
|
/* BEI=4, ES=4, BAR4 64b Base + 64b MaxOffset */
|
|
|
|
(4 << 4) | 4,
|
|
|
|
PCI_CAP_EA_BASE_LO4 | PCI_EA_IS_64,
|
|
|
|
PCI_CAP_EA_SIZE_LO | PCI_EA_IS_64,
|
|
|
|
PCI_CAP_EA_BASE_HI4,
|
|
|
|
PCI_CAP_EA_SIZE_HI,
|
|
|
|
};
|
|
|
|
|
2020-01-27 15:49:37 +00:00
|
|
|
static int sandbox_swap_case_read_ea(const struct udevice *emul, uint offset,
|
2019-06-07 08:24:24 +00:00
|
|
|
ulong *valuep, enum pci_size_t size)
|
|
|
|
{
|
|
|
|
u32 reg;
|
|
|
|
|
|
|
|
offset = offset - PCI_CAP_ID_EA_OFFSET - 4;
|
|
|
|
reg = ea_regs[offset >> 2];
|
|
|
|
reg >>= (offset % 4) * 8;
|
|
|
|
|
|
|
|
*valuep = reg;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-27 15:49:37 +00:00
|
|
|
static int sandbox_swap_case_read_config(const struct udevice *emul,
|
|
|
|
uint offset, ulong *valuep,
|
|
|
|
enum pci_size_t size)
|
2015-03-05 19:25:29 +00:00
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct swap_case_plat *plat = dev_get_plat(emul);
|
2015-03-05 19:25:29 +00:00
|
|
|
|
2019-06-07 08:24:24 +00:00
|
|
|
/*
|
|
|
|
* The content of the EA capability structure is handled elsewhere to
|
|
|
|
* keep the switch/case below sane
|
|
|
|
*/
|
|
|
|
if (offset > PCI_CAP_ID_EA_OFFSET + PCI_CAP_LIST_NEXT &&
|
|
|
|
offset < PCI_CAP_ID_EA_OFFSET + PCI_CAP_ID_EA_SIZE)
|
|
|
|
return sandbox_swap_case_read_ea(emul, offset, valuep, size);
|
|
|
|
|
2015-03-05 19:25:29 +00:00
|
|
|
switch (offset) {
|
|
|
|
case PCI_COMMAND:
|
|
|
|
*valuep = plat->command;
|
|
|
|
break;
|
|
|
|
case PCI_HEADER_TYPE:
|
|
|
|
*valuep = 0;
|
|
|
|
break;
|
|
|
|
case PCI_VENDOR_ID:
|
|
|
|
*valuep = SANDBOX_PCI_VENDOR_ID;
|
|
|
|
break;
|
|
|
|
case PCI_DEVICE_ID:
|
2019-09-25 14:56:01 +00:00
|
|
|
*valuep = SANDBOX_PCI_SWAP_CASE_EMUL_ID;
|
2015-03-05 19:25:29 +00:00
|
|
|
break;
|
|
|
|
case PCI_CLASS_DEVICE:
|
|
|
|
if (size == PCI_SIZE_8) {
|
|
|
|
*valuep = SANDBOX_PCI_CLASS_SUB_CODE;
|
|
|
|
} else {
|
|
|
|
*valuep = (SANDBOX_PCI_CLASS_CODE << 8) |
|
|
|
|
SANDBOX_PCI_CLASS_SUB_CODE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PCI_CLASS_CODE:
|
|
|
|
*valuep = SANDBOX_PCI_CLASS_CODE;
|
|
|
|
break;
|
|
|
|
case PCI_BASE_ADDRESS_0:
|
|
|
|
case PCI_BASE_ADDRESS_1:
|
|
|
|
case PCI_BASE_ADDRESS_2:
|
|
|
|
case PCI_BASE_ADDRESS_3:
|
|
|
|
case PCI_BASE_ADDRESS_4:
|
|
|
|
case PCI_BASE_ADDRESS_5: {
|
|
|
|
int barnum;
|
2019-09-25 14:56:42 +00:00
|
|
|
u32 *bar;
|
2015-03-05 19:25:29 +00:00
|
|
|
|
2019-09-25 14:56:06 +00:00
|
|
|
barnum = pci_offset_to_barnum(offset);
|
2015-03-05 19:25:29 +00:00
|
|
|
bar = &plat->bar[barnum];
|
|
|
|
|
2019-09-25 14:56:42 +00:00
|
|
|
*valuep = sandbox_pci_read_bar(*bar, barinfo[barnum].type,
|
|
|
|
barinfo[barnum].size);
|
2015-03-05 19:25:29 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-08-03 08:14:53 +00:00
|
|
|
case PCI_CAPABILITY_LIST:
|
|
|
|
*valuep = PCI_CAP_ID_PM_OFFSET;
|
|
|
|
break;
|
|
|
|
case PCI_CAP_ID_PM_OFFSET:
|
|
|
|
*valuep = (PCI_CAP_ID_EXP_OFFSET << 8) | PCI_CAP_ID_PM;
|
|
|
|
break;
|
2018-10-15 09:21:22 +00:00
|
|
|
case PCI_CAP_ID_PM_OFFSET + PCI_CAP_LIST_NEXT:
|
|
|
|
*valuep = PCI_CAP_ID_EXP_OFFSET;
|
|
|
|
break;
|
2018-08-03 08:14:53 +00:00
|
|
|
case PCI_CAP_ID_EXP_OFFSET:
|
|
|
|
*valuep = (PCI_CAP_ID_MSIX_OFFSET << 8) | PCI_CAP_ID_EXP;
|
|
|
|
break;
|
2018-10-15 09:21:22 +00:00
|
|
|
case PCI_CAP_ID_EXP_OFFSET + PCI_CAP_LIST_NEXT:
|
|
|
|
*valuep = PCI_CAP_ID_MSIX_OFFSET;
|
|
|
|
break;
|
2018-08-03 08:14:53 +00:00
|
|
|
case PCI_CAP_ID_MSIX_OFFSET:
|
2019-06-07 08:24:24 +00:00
|
|
|
if (sandbox_swap_case_use_ea(emul))
|
|
|
|
*valuep = (PCI_CAP_ID_EA_OFFSET << 8) | PCI_CAP_ID_MSIX;
|
|
|
|
else
|
|
|
|
*valuep = PCI_CAP_ID_MSIX;
|
2018-08-03 08:14:53 +00:00
|
|
|
break;
|
2018-10-15 09:21:22 +00:00
|
|
|
case PCI_CAP_ID_MSIX_OFFSET + PCI_CAP_LIST_NEXT:
|
2019-06-07 08:24:24 +00:00
|
|
|
if (sandbox_swap_case_use_ea(emul))
|
|
|
|
*valuep = PCI_CAP_ID_EA_OFFSET;
|
|
|
|
else
|
|
|
|
*valuep = 0;
|
|
|
|
break;
|
|
|
|
case PCI_CAP_ID_EA_OFFSET:
|
|
|
|
*valuep = (PCI_CAP_ID_EA_ENTRY_CNT << 16) | PCI_CAP_ID_EA;
|
|
|
|
break;
|
|
|
|
case PCI_CAP_ID_EA_OFFSET + PCI_CAP_LIST_NEXT:
|
2018-10-15 09:21:22 +00:00
|
|
|
*valuep = 0;
|
|
|
|
break;
|
2018-08-03 08:14:53 +00:00
|
|
|
case PCI_EXT_CAP_ID_ERR_OFFSET:
|
|
|
|
*valuep = (PCI_EXT_CAP_ID_VC_OFFSET << 20) | PCI_EXT_CAP_ID_ERR;
|
|
|
|
break;
|
|
|
|
case PCI_EXT_CAP_ID_VC_OFFSET:
|
|
|
|
*valuep = (PCI_EXT_CAP_ID_DSN_OFFSET << 20) | PCI_EXT_CAP_ID_VC;
|
|
|
|
break;
|
|
|
|
case PCI_EXT_CAP_ID_DSN_OFFSET:
|
|
|
|
*valuep = PCI_EXT_CAP_ID_DSN;
|
|
|
|
break;
|
2015-03-05 19:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sandbox_swap_case_write_config(struct udevice *emul, uint offset,
|
|
|
|
ulong value, enum pci_size_t size)
|
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct swap_case_plat *plat = dev_get_plat(emul);
|
2015-03-05 19:25:29 +00:00
|
|
|
|
|
|
|
switch (offset) {
|
|
|
|
case PCI_COMMAND:
|
|
|
|
plat->command = value;
|
|
|
|
break;
|
|
|
|
case PCI_BASE_ADDRESS_0:
|
|
|
|
case PCI_BASE_ADDRESS_1: {
|
|
|
|
int barnum;
|
|
|
|
u32 *bar;
|
|
|
|
|
2019-09-25 14:56:06 +00:00
|
|
|
barnum = pci_offset_to_barnum(offset);
|
2015-03-05 19:25:29 +00:00
|
|
|
bar = &plat->bar[barnum];
|
|
|
|
|
|
|
|
debug("w bar %d=%lx\n", barnum, value);
|
|
|
|
*bar = value;
|
2018-08-03 08:14:40 +00:00
|
|
|
/* space indicator (bit#0) is read-only */
|
|
|
|
*bar |= barinfo[barnum].type;
|
2015-03-05 19:25:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sandbox_swap_case_find_bar(struct udevice *emul, unsigned int addr,
|
|
|
|
int *barnump, unsigned int *offsetp)
|
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct swap_case_plat *plat = dev_get_plat(emul);
|
2015-03-05 19:25:29 +00:00
|
|
|
int barnum;
|
|
|
|
|
|
|
|
for (barnum = 0; barnum < ARRAY_SIZE(barinfo); barnum++) {
|
|
|
|
unsigned int size = barinfo[barnum].size;
|
2018-08-03 08:14:40 +00:00
|
|
|
u32 base = plat->bar[barnum] & ~PCI_BASE_ADDRESS_SPACE;
|
2015-03-05 19:25:29 +00:00
|
|
|
|
2018-08-03 08:14:40 +00:00
|
|
|
if (addr >= base && addr < base + size) {
|
2015-03-05 19:25:29 +00:00
|
|
|
*barnump = barnum;
|
2018-08-03 08:14:40 +00:00
|
|
|
*offsetp = addr - base;
|
2015-03-05 19:25:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*barnump = -1;
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sandbox_swap_case_do_op(enum swap_case_op op, char *str, int len)
|
|
|
|
{
|
|
|
|
for (; len > 0; len--, str++) {
|
|
|
|
switch (op) {
|
|
|
|
case OP_TO_UPPER:
|
|
|
|
*str = toupper(*str);
|
|
|
|
break;
|
|
|
|
case OP_TO_LOWER:
|
|
|
|
*str = tolower(*str);
|
|
|
|
break;
|
|
|
|
case OP_SWAP:
|
|
|
|
if (isupper(*str))
|
|
|
|
*str = tolower(*str);
|
|
|
|
else
|
|
|
|
*str = toupper(*str);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 14:56:03 +00:00
|
|
|
static int sandbox_swap_case_read_io(struct udevice *dev, unsigned int addr,
|
|
|
|
ulong *valuep, enum pci_size_t size)
|
2015-03-05 19:25:29 +00:00
|
|
|
{
|
|
|
|
struct swap_case_priv *priv = dev_get_priv(dev);
|
|
|
|
unsigned int offset;
|
|
|
|
int barnum;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (barnum == 0 && offset == 0)
|
|
|
|
*valuep = (*valuep & ~0xff) | priv->op;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-25 14:56:03 +00:00
|
|
|
static int sandbox_swap_case_write_io(struct udevice *dev, unsigned int addr,
|
|
|
|
ulong value, enum pci_size_t size)
|
2015-03-05 19:25:29 +00:00
|
|
|
{
|
|
|
|
struct swap_case_priv *priv = dev_get_priv(dev);
|
|
|
|
unsigned int offset;
|
|
|
|
int barnum;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
if (barnum == 0 && offset == 0)
|
|
|
|
priv->op = value;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-07 08:24:24 +00:00
|
|
|
static int pci_ea_bar2_magic = PCI_EA_BAR2_MAGIC;
|
|
|
|
static int pci_ea_bar4_magic = PCI_EA_BAR4_MAGIC;
|
|
|
|
|
2015-03-05 19:25:29 +00:00
|
|
|
static int sandbox_swap_case_map_physmem(struct udevice *dev,
|
|
|
|
phys_addr_t addr, unsigned long *lenp, void **ptrp)
|
|
|
|
{
|
|
|
|
struct swap_case_priv *priv = dev_get_priv(dev);
|
|
|
|
unsigned int offset, avail;
|
|
|
|
int barnum;
|
|
|
|
int ret;
|
|
|
|
|
2019-06-07 08:24:24 +00:00
|
|
|
if (sandbox_swap_case_use_ea(dev)) {
|
|
|
|
/*
|
|
|
|
* only support mapping base address in EA test for now, we
|
|
|
|
* don't handle mapping an offset inside a BAR. Seems good
|
|
|
|
* enough for the current test.
|
|
|
|
*/
|
|
|
|
switch (addr) {
|
|
|
|
case (phys_addr_t)PCI_CAP_EA_BASE_LO0:
|
|
|
|
*ptrp = &priv->op;
|
|
|
|
*lenp = 4;
|
|
|
|
break;
|
|
|
|
case (phys_addr_t)PCI_CAP_EA_BASE_LO1:
|
|
|
|
*ptrp = priv->mem_text;
|
|
|
|
*lenp = barinfo[1].size - 1;
|
|
|
|
break;
|
|
|
|
case (phys_addr_t)((PCI_CAP_EA_BASE_HI2 << 32) |
|
|
|
|
PCI_CAP_EA_BASE_LO2):
|
|
|
|
*ptrp = &pci_ea_bar2_magic;
|
|
|
|
*lenp = PCI_CAP_EA_SIZE_LO;
|
|
|
|
break;
|
|
|
|
case (phys_addr_t)((PCI_CAP_EA_BASE_HI4 << 32) |
|
|
|
|
PCI_CAP_EA_BASE_LO4):
|
|
|
|
*ptrp = &pci_ea_bar4_magic;
|
|
|
|
*lenp = (PCI_CAP_EA_SIZE_HI << 32) |
|
|
|
|
PCI_CAP_EA_SIZE_LO;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-05 19:25:29 +00:00
|
|
|
ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2019-06-07 08:24:24 +00:00
|
|
|
|
2015-03-05 19:25:29 +00:00
|
|
|
if (barnum == 1) {
|
|
|
|
*ptrp = priv->mem_text + offset;
|
|
|
|
avail = barinfo[1].size - offset;
|
|
|
|
if (avail > barinfo[1].size)
|
|
|
|
*lenp = 0;
|
|
|
|
else
|
|
|
|
*lenp = min(*lenp, (ulong)avail);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sandbox_swap_case_unmap_physmem(struct udevice *dev,
|
|
|
|
const void *vaddr, unsigned long len)
|
|
|
|
{
|
|
|
|
struct swap_case_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
sandbox_swap_case_do_op(priv->op, (void *)vaddr, len);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-25 14:56:03 +00:00
|
|
|
static struct dm_pci_emul_ops sandbox_swap_case_emul_ops = {
|
2015-03-05 19:25:29 +00:00
|
|
|
.read_config = sandbox_swap_case_read_config,
|
|
|
|
.write_config = sandbox_swap_case_write_config,
|
|
|
|
.read_io = sandbox_swap_case_read_io,
|
|
|
|
.write_io = sandbox_swap_case_write_io,
|
|
|
|
.map_physmem = sandbox_swap_case_map_physmem,
|
|
|
|
.unmap_physmem = sandbox_swap_case_unmap_physmem,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id sandbox_swap_case_ids[] = {
|
|
|
|
{ .compatible = "sandbox,swap-case" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(sandbox_swap_case_emul) = {
|
|
|
|
.name = "sandbox_swap_case_emul",
|
|
|
|
.id = UCLASS_PCI_EMUL,
|
|
|
|
.of_match = sandbox_swap_case_ids,
|
|
|
|
.ops = &sandbox_swap_case_emul_ops,
|
2020-12-03 23:55:17 +00:00
|
|
|
.priv_auto = sizeof(struct swap_case_priv),
|
2020-12-03 23:55:23 +00:00
|
|
|
.plat_auto = sizeof(struct swap_case_plat),
|
2015-03-05 19:25:29 +00:00
|
|
|
};
|
2018-08-03 08:14:46 +00:00
|
|
|
|
|
|
|
static struct pci_device_id sandbox_swap_case_supported[] = {
|
2019-09-25 14:56:01 +00:00
|
|
|
{ PCI_VDEVICE(SANDBOX, SANDBOX_PCI_SWAP_CASE_EMUL_ID),
|
|
|
|
SWAP_CASE_DRV_DATA },
|
2018-08-03 08:14:46 +00:00
|
|
|
{},
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_PCI_DEVICE(sandbox_swap_case_emul, sandbox_swap_case_supported);
|