blob: a6f483ed2511a9da7fba9a867a4e9c5ccb823d6f [file] [log] [blame]
Mario Six9a8bcab2018-08-09 14:51:18 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2018
4 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
5 */
6
7#include <common.h>
8#include <axi.h>
9#include <dm.h>
Simon Glass336d4612020-02-03 07:36:16 -070010#include <malloc.h>
Mario Six9a8bcab2018-08-09 14:51:18 +020011
12/**
13 * struct sandbox_store_priv - Private data structure of a AXI store device
14 * @store: The buffer holding the device's internal memory, which is read from
15 * and written to using the driver's methods
16 */
17struct sandbox_store_priv {
18 u8 *store;
19};
20
21/**
22 * copy_axi_data() - Copy data from source to destination with a given AXI
23 * transfer width
24 * @src: Pointer to the data source from where data will be read
25 * @dst: Pointer to the data destination where data will be written to
26 * @size: Size of the data to be copied given by a axi_size_t enum value
27 *
28 * Return: 0 if OK, -ve on error
29 */
30static int copy_axi_data(void *src, void *dst, enum axi_size_t size)
31{
32 switch (size) {
33 case AXI_SIZE_8:
34 *((u8 *)dst) = *((u8 *)src);
35 return 0;
36 case AXI_SIZE_16:
37 *((u16 *)dst) = be16_to_cpu(*((u16 *)src));
38 return 0;
39 case AXI_SIZE_32:
40 *((u32 *)dst) = be32_to_cpu(*((u32 *)src));
41 return 0;
42 default:
43 debug("%s: Unknown AXI transfer size '%d'\n", __func__, size);
44 return -EINVAL;
45 }
46}
47
48static int sandbox_store_read(struct udevice *dev, ulong address, void *data,
49 enum axi_size_t size)
50{
51 struct sandbox_store_priv *priv = dev_get_priv(dev);
52
53 return copy_axi_data(priv->store + address, data, size);
54}
55
56static int sandbox_store_write(struct udevice *dev, ulong address, void *data,
57 enum axi_size_t size)
58{
59 struct sandbox_store_priv *priv = dev_get_priv(dev);
60
61 return copy_axi_data(data, priv->store + address, size);
62}
63
64static int sandbox_store_get_store(struct udevice *dev, u8 **store)
65{
66 struct sandbox_store_priv *priv = dev_get_priv(dev);
67
68 *store = priv->store;
69
70 return 0;
71}
72
73static const struct udevice_id sandbox_store_ids[] = {
74 { .compatible = "sandbox,sandbox_store" },
75 { /* sentinel */ }
76};
77
78static const struct axi_emul_ops sandbox_store_ops = {
79 .read = sandbox_store_read,
80 .write = sandbox_store_write,
81 .get_store = sandbox_store_get_store,
82};
83
84static int sandbox_store_probe(struct udevice *dev)
85{
86 struct sandbox_store_priv *priv = dev_get_priv(dev);
87 u32 reg[2];
88 int ret;
89
90 ret = dev_read_u32_array(dev, "reg", reg, ARRAY_SIZE(reg));
91 if (ret) {
92 debug("%s: Could not read 'reg' property\n", dev->name);
93 return -EINVAL;
94 }
95
96 /*
97 * Allocate the device's internal storage that will be read
98 * from/written to
99 */
100 priv->store = calloc(reg[1], 1);
101 if (!priv->store)
102 return -ENOMEM;
103
104 return 0;
105}
106
107static int sandbox_store_remove(struct udevice *dev)
108{
109 struct sandbox_store_priv *priv = dev_get_priv(dev);
110
111 free(priv->store);
112
113 return 0;
114}
115
116U_BOOT_DRIVER(sandbox_axi_store) = {
117 .name = "sandbox_axi_store",
118 .id = UCLASS_AXI_EMUL,
119 .of_match = sandbox_store_ids,
120 .ops = &sandbox_store_ops,
121 .priv_auto_alloc_size = sizeof(struct sandbox_store_priv),
122 .probe = sandbox_store_probe,
123 .remove = sandbox_store_remove,
124};