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