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 <common.h>
8 #include <axi.h>
9 #include <dm.h>
10 #include <log.h>
11 #include <dm/device-internal.h>
12 #include <asm/axi.h>
13 
axi_sandbox_get_emul(struct udevice * bus,ulong address,enum axi_size_t size,struct udevice ** emulp)14 int axi_sandbox_get_emul(struct udevice *bus, ulong address,
15 			 enum axi_size_t size, struct udevice **emulp)
16 {
17 	struct udevice *dev;
18 	u32 reg[2];
19 	uint offset;
20 
21 	switch (size) {
22 	case AXI_SIZE_8:
23 		offset = 1;
24 		break;
25 	case AXI_SIZE_16:
26 		offset = 2;
27 		break;
28 	case AXI_SIZE_32:
29 		offset = 4;
30 		break;
31 	default:
32 		debug("%s: Unknown AXI transfer size '%d'", bus->name, size);
33 		offset = 0;
34 	}
35 
36 	/*
37 	 * Note: device_find_* don't activate the devices; they're activated
38 	 *	 as-needed below.
39 	 */
40 	for (device_find_first_child(bus, &dev);
41 	     dev;
42 	     device_find_next_child(&dev)) {
43 		int ret;
44 
45 		ret = dev_read_u32_array(dev, "reg", reg, ARRAY_SIZE(reg));
46 		if (ret) {
47 			debug("%s: Could not read 'reg' property of %s\n",
48 			      bus->name, dev->name);
49 			continue;
50 		}
51 
52 		/*
53 		 * Does the transfer's address fall into this device's address
54 		 * space?
55 		 */
56 		if (address >= reg[0] && address <= reg[0] + reg[1] - offset) {
57 			/* If yes, activate it... */
58 			if (device_probe(dev)) {
59 				debug("%s: Could not activate %s\n",
60 				      bus->name, dev->name);
61 				return -ENODEV;
62 			}
63 
64 			/* ...and return it */
65 			*emulp = dev;
66 			return 0;
67 		}
68 	}
69 
70 	return -ENODEV;
71 }
72 
axi_get_store(struct udevice * dev,u8 ** storep)73 int axi_get_store(struct udevice *dev, u8 **storep)
74 {
75 	struct axi_emul_ops *ops = axi_emul_get_ops(dev);
76 
77 	if (!ops->get_store)
78 		return -ENOSYS;
79 
80 	return ops->get_store(dev, storep);
81 }
82 
83 UCLASS_DRIVER(axi_emul) = {
84 	.id		= UCLASS_AXI_EMUL,
85 	.name		= "axi_emul",
86 };
87