/src/u-boot/drivers/axi/sandbox_store.c
Line | Count | Source |
1 | | // 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 <axi.h> |
8 | | #include <dm.h> |
9 | | #include <log.h> |
10 | | #include <malloc.h> |
11 | | |
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 | | */ |
17 | | struct 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 | | */ |
30 | | static int copy_axi_data(void *src, void *dst, enum axi_size_t size) |
31 | 0 | { |
32 | 0 | switch (size) { |
33 | 0 | case AXI_SIZE_8: |
34 | 0 | *((u8 *)dst) = *((u8 *)src); |
35 | 0 | return 0; |
36 | 0 | case AXI_SIZE_16: |
37 | 0 | *((u16 *)dst) = be16_to_cpu(*((u16 *)src)); |
38 | 0 | return 0; |
39 | 0 | case AXI_SIZE_32: |
40 | 0 | *((u32 *)dst) = be32_to_cpu(*((u32 *)src)); |
41 | 0 | return 0; |
42 | 0 | default: |
43 | 0 | debug("%s: Unknown AXI transfer size '%d'\n", __func__, size); |
44 | 0 | return -EINVAL; |
45 | 0 | } |
46 | 0 | } |
47 | | |
48 | | static int sandbox_store_read(struct udevice *dev, ulong address, void *data, |
49 | | enum axi_size_t size) |
50 | 0 | { |
51 | 0 | struct sandbox_store_priv *priv = dev_get_priv(dev); |
52 | |
|
53 | 0 | return copy_axi_data(priv->store + address, data, size); |
54 | 0 | } |
55 | | |
56 | | static int sandbox_store_write(struct udevice *dev, ulong address, void *data, |
57 | | enum axi_size_t size) |
58 | 0 | { |
59 | 0 | struct sandbox_store_priv *priv = dev_get_priv(dev); |
60 | |
|
61 | 0 | return copy_axi_data(data, priv->store + address, size); |
62 | 0 | } |
63 | | |
64 | | static int sandbox_store_get_store(struct udevice *dev, u8 **store) |
65 | 0 | { |
66 | 0 | struct sandbox_store_priv *priv = dev_get_priv(dev); |
67 | |
|
68 | 0 | *store = priv->store; |
69 | |
|
70 | 0 | return 0; |
71 | 0 | } |
72 | | |
73 | | static const struct udevice_id sandbox_store_ids[] = { |
74 | | { .compatible = "sandbox,sandbox_store" }, |
75 | | { /* sentinel */ } |
76 | | }; |
77 | | |
78 | | static 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 | | |
84 | | static int sandbox_store_probe(struct udevice *dev) |
85 | 0 | { |
86 | 0 | struct sandbox_store_priv *priv = dev_get_priv(dev); |
87 | 0 | u32 reg[2]; |
88 | 0 | int ret; |
89 | |
|
90 | 0 | ret = dev_read_u32_array(dev, "reg", reg, ARRAY_SIZE(reg)); |
91 | 0 | if (ret) { |
92 | 0 | debug("%s: Could not read 'reg' property\n", dev->name); |
93 | 0 | return -EINVAL; |
94 | 0 | } |
95 | | |
96 | | /* |
97 | | * Allocate the device's internal storage that will be read |
98 | | * from/written to |
99 | | */ |
100 | 0 | priv->store = calloc(reg[1], 1); |
101 | 0 | if (!priv->store) |
102 | 0 | return -ENOMEM; |
103 | | |
104 | 0 | return 0; |
105 | 0 | } |
106 | | |
107 | | static int sandbox_store_remove(struct udevice *dev) |
108 | 0 | { |
109 | 0 | struct sandbox_store_priv *priv = dev_get_priv(dev); |
110 | |
|
111 | 0 | free(priv->store); |
112 | |
|
113 | 0 | return 0; |
114 | 0 | } |
115 | | |
116 | | U_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 = sizeof(struct sandbox_store_priv), |
122 | | .probe = sandbox_store_probe, |
123 | | .remove = sandbox_store_remove, |
124 | | }; |