blob: 31cde2dbac07eb0c55a400ca12e303712edc3dec [file] [log] [blame]
Mario Sixa8ce0ee2018-07-31 14:24:14 +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 <dm.h>
9#include <misc.h>
10
11struct misc_sandbox_priv {
12 u8 mem[128];
13 ulong last_ioctl;
14 bool enabled;
15};
16
17int misc_sandbox_read(struct udevice *dev, int offset, void *buf, int size)
18{
19 struct misc_sandbox_priv *priv = dev_get_priv(dev);
20
21 memcpy(buf, priv->mem + offset, size);
22
Simon Glass587dc402018-11-06 15:21:39 -070023 return size;
Mario Sixa8ce0ee2018-07-31 14:24:14 +020024}
25
26int misc_sandbox_write(struct udevice *dev, int offset, const void *buf,
27 int size)
28{
29 struct misc_sandbox_priv *priv = dev_get_priv(dev);
30
31 memcpy(priv->mem + offset, buf, size);
32
Simon Glass587dc402018-11-06 15:21:39 -070033 return size;
Mario Sixa8ce0ee2018-07-31 14:24:14 +020034}
35
36int misc_sandbox_ioctl(struct udevice *dev, unsigned long request, void *buf)
37{
38 struct misc_sandbox_priv *priv = dev_get_priv(dev);
39
40 priv->last_ioctl = request;
41
42 return 0;
43}
44
45int misc_sandbox_call(struct udevice *dev, int msgid, void *tx_msg,
46 int tx_size, void *rx_msg, int rx_size)
47{
48 struct misc_sandbox_priv *priv = dev_get_priv(dev);
49
50 if (msgid == 0) {
51 int num = *(int *)tx_msg;
52
53 switch (num) {
54 case 0:
55 strncpy(rx_msg, "Zero", rx_size);
56 break;
57 case 1:
58 strncpy(rx_msg, "One", rx_size);
59 break;
60 case 2:
61 strncpy(rx_msg, "Two", rx_size);
62 break;
63 default:
64 return -EINVAL;
65 }
66 }
67
68 if (msgid == 1) {
69 int num = *(int *)tx_msg;
70
71 switch (num) {
72 case 0:
73 strncpy(rx_msg, "Forty", rx_size);
74 break;
75 case 1:
76 strncpy(rx_msg, "Forty-one", rx_size);
77 break;
78 case 2:
79 strncpy(rx_msg, "Forty-two", rx_size);
80 break;
81 default:
82 return -EINVAL;
83 }
84 }
85
86 if (msgid == 2)
87 memcpy(rx_msg, &priv->last_ioctl, sizeof(priv->last_ioctl));
88
89 if (msgid == 3)
90 memcpy(rx_msg, &priv->enabled, sizeof(priv->enabled));
91
92 return 0;
93}
94
95int misc_sandbox_set_enabled(struct udevice *dev, bool val)
96{
97 struct misc_sandbox_priv *priv = dev_get_priv(dev);
98
99 priv->enabled = !priv->enabled;
100
101 return 0;
102}
103
104static const struct misc_ops misc_sandbox_ops = {
105 .read = misc_sandbox_read,
106 .write = misc_sandbox_write,
107 .ioctl = misc_sandbox_ioctl,
108 .call = misc_sandbox_call,
109 .set_enabled = misc_sandbox_set_enabled,
110};
111
112int misc_sandbox_probe(struct udevice *dev)
113{
114 struct misc_sandbox_priv *priv = dev_get_priv(dev);
Sean Anderson13652b82022-05-05 13:11:44 -0400115 /* For eth5 */
116 const u8 mac[] = { 0x02, 0x00, 0x11, 0x22, 0x33, 0x46 };
Mario Sixa8ce0ee2018-07-31 14:24:14 +0200117
118 priv->enabled = true;
Sean Anderson13652b82022-05-05 13:11:44 -0400119 memcpy(&priv->mem[16], mac, sizeof(mac));
Mario Sixa8ce0ee2018-07-31 14:24:14 +0200120
121 return 0;
122}
123
124static const struct udevice_id misc_sandbox_ids[] = {
125 { .compatible = "sandbox,misc_sandbox" },
126 { }
127};
128
129U_BOOT_DRIVER(misc_sandbox) = {
130 .name = "misc_sandbox",
131 .id = UCLASS_MISC,
132 .ops = &misc_sandbox_ops,
133 .of_match = misc_sandbox_ids,
134 .probe = misc_sandbox_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700135 .priv_auto = sizeof(struct misc_sandbox_priv),
Mario Sixa8ce0ee2018-07-31 14:24:14 +0200136};