aboutsummaryrefslogtreecommitdiff
path: root/drivers/axi/sandbox_store.c
blob: ef349a50b7981736912b38b9966e0138bd6b584b (plain)
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
// SPDX-License-Identifier: GPL-2.0+
/*
 * (C) Copyright 2018
 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
 */

#include <common.h>
#include <axi.h>
#include <dm.h>
#include <log.h>
#include <malloc.h>

/**
 * struct sandbox_store_priv - Private data structure of a AXI store device
 * @store: The buffer holding the device's internal memory, which is read from
 *	   and written to using the driver's methods
 */
struct sandbox_store_priv {
	u8 *store;
};

/**
 * copy_axi_data() - Copy data from source to destination with a given AXI
 *		     transfer width
 * @src:  Pointer to the data source from where data will be read
 * @dst:  Pointer to the data destination where data will be written to
 * @size: Size of the data to be copied given by a axi_size_t enum value
 *
 * Return: 0 if OK, -ve on error
 */
static int copy_axi_data(void *src, void *dst, enum axi_size_t size)
{
	switch (size) {
	case AXI_SIZE_8:
		*((u8 *)dst) = *((u8 *)src);
		return 0;
	case AXI_SIZE_16:
		*((u16 *)dst) = be16_to_cpu(*((u16 *)src));
		return 0;
	case AXI_SIZE_32:
		*((u32 *)dst) = be32_to_cpu(*((u32 *)src));
		return 0;
	default:
		debug("%s: Unknown AXI transfer size '%d'\n", __func__, size);
		return -EINVAL;
	}
}

static int sandbox_store_read(struct udevice *dev, ulong address, void *data,
			      enum axi_size_t size)
{
	struct sandbox_store_priv *priv = dev_get_priv(dev);

	return copy_axi_data(priv->store + address, data, size);
}

static int sandbox_store_write(struct udevice *dev, ulong address, void *data,
			       enum axi_size_t size)
{
	struct sandbox_store_priv *priv = dev_get_priv(dev);

	return copy_axi_data(data, priv->store + address, size);
}

static int sandbox_store_get_store(struct udevice *dev, u8 **store)
{
	struct sandbox_store_priv *priv = dev_get_priv(dev);

	*store = priv->store;

	return 0;
}

static const struct udevice_id sandbox_store_ids[] = {
	{ .compatible = "sandbox,sandbox_store" },
	{ /* sentinel */ }
};

static const struct axi_emul_ops sandbox_store_ops = {
	.read = sandbox_store_read,
	.write = sandbox_store_write,
	.get_store = sandbox_store_get_store,
};

static int sandbox_store_probe(struct udevice *dev)
{
	struct sandbox_store_priv *priv = dev_get_priv(dev);
	u32 reg[2];
	int ret;

	ret = dev_read_u32_array(dev, "reg", reg, ARRAY_SIZE(reg));
	if (ret) {
		debug("%s: Could not read 'reg' property\n", dev->name);
		return -EINVAL;
	}

	/*
	 * Allocate the device's internal storage that will be read
	 * from/written to
	 */
	priv->store = calloc(reg[1], 1);
	if (!priv->store)
		return -ENOMEM;

	return 0;
}

static int sandbox_store_remove(struct udevice *dev)
{
	struct sandbox_store_priv *priv = dev_get_priv(dev);

	free(priv->store);

	return 0;
}

U_BOOT_DRIVER(sandbox_axi_store) = {
	.name           = "sandbox_axi_store",
	.id             = UCLASS_AXI_EMUL,
	.of_match       = sandbox_store_ids,
	.ops		= &sandbox_store_ops,
	.priv_auto	= sizeof(struct sandbox_store_priv),
	.probe          = sandbox_store_probe,
	.remove		= sandbox_store_remove,
};