summaryrefslogtreecommitdiff
path: root/drivers/misc/gdsys_ioep.c
blob: 7f17095cc581ab9e443121c231f429228dc38cdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// SPDX-License-Identifier: GPL-2.0+
/*
 * (C) Copyright 2017
 * Mario Six,  Guntermann & Drunck GmbH, mario.six@gdsys.cc
 *
 * based on the cmd_ioloop driver/command, which is
 *
 * (C) Copyright 2014
 * Dirk Eibach, Guntermann & Drunck GmbH, dirk.eibach@gdsys.cc
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include <common.h>
#include <dm.h>
#include <misc.h>
#include <regmap.h>

#include "gdsys_ioep.h"

/**
 * struct gdsys_ioep_priv - Private data structure for IOEP devices
 * @map:   Register map to be used for the device
 * @state: Flag to keep the current status of the RX control (enabled/disabled)
 */
struct gdsys_ioep_priv {
	struct regmap *map;
	bool state;
};

/**
 * enum last_spec - Convenience enum for read data sanity check
 * @READ_DATA_IS_LAST:     The data to be read should be the final data of the
 *			   current packet
 * @READ_DATA_IS_NOT_LAST: The data to be read should not be the final data of
 *			   the current packet
 */
enum last_spec {
	READ_DATA_IS_LAST,
	READ_DATA_IS_NOT_LAST,
};

static int gdsys_ioep_set_receive(struct udevice *dev, bool val)
{
	struct gdsys_ioep_priv *priv = dev_get_priv(dev);
	u16 state;

	priv->state = !priv->state;

	if (val)
		state = CTRL_PROC_RECEIVE_ENABLE;
	else
		state = ~CTRL_PROC_RECEIVE_ENABLE;

	gdsys_ioep_set(priv->map, tx_control, state);

	if (val) {
		/* Set device address to dummy 1 */
		gdsys_ioep_set(priv->map, device_address, 1);
	}

	return !priv->state;
}

static int gdsys_ioep_send(struct udevice *dev, int offset,
			   const void *buf, int size)
{
	struct gdsys_ioep_priv *priv = dev_get_priv(dev);
	int k;
	u16 *p = (u16 *)buf;

	for (k = 0; k < size; ++k)
		gdsys_ioep_set(priv->map, transmit_data, *(p++));

	gdsys_ioep_set(priv->map, tx_control, CTRL_PROC_RECEIVE_ENABLE |
					      CTRL_FLUSH_TRANSMIT_BUFFER);

	return 0;
}

/**
 * receive_byte_buffer() - Read data from a IOEP device
 * @dev:       The IOEP device to read data from
 * @len:       The length of the data to read
 * @buffer:    The buffer to read the data into
 * @last_spec: Flag to indicate if the data to be read in this call should be
 *	       the final data of the current packet (i.e. it should be empty
 *	       after this read)
 *
 * Return: 0 if OK, -ve on error
 */
static int receive_byte_buffer(struct udevice *dev, uint len,
			       u16 *buffer, enum last_spec last_spec)
{
	struct gdsys_ioep_priv *priv = dev_get_priv(dev);
	int k;
	int ret = -EIO;

	for (k = 0; k < len; ++k) {
		u16 rx_tx_status;

		gdsys_ioep_get(priv->map, receive_data, buffer++);

		gdsys_ioep_get(priv->map, rx_tx_status, &rx_tx_status);
		/*
		 * Sanity check: If the data read should have been the last,
		 * but wasn't, something is wrong
		 */
		if (k == (len - 1) && (last_spec == READ_DATA_IS_NOT_LAST ||
				       rx_tx_status & STATE_RX_DATA_LAST))
			ret = 0;
	}

	if (ret)
		debug("%s: Error while receiving bufer (err = %d)\n",
		      dev->name, ret);

	return ret;
}

static int gdsys_ioep_receive(struct udevice *dev, int offset, void *buf,
			      int size)
{
	int ret;
	struct io_generic_packet header;
	u16 *p = (u16 *)buf;
	const int header_words = sizeof(struct io_generic_packet) / sizeof(u16);
	uint len;

	/* Read the packet header */
	ret = receive_byte_buffer(dev, header_words, p, READ_DATA_IS_NOT_LAST);
	if (ret) {
		debug("%s: Failed to read header data (err = %d)\n",
		      dev->name, ret);
		return ret;
	}

	memcpy(&header, p, header_words * sizeof(u16));
	p += header_words;

	/* Get payload data length */
	len = (header.packet_length + 1) / sizeof(u16);

	/* Read the packet payload */
	ret = receive_byte_buffer(dev, len, p, READ_DATA_IS_LAST);
	if (ret) {
		debug("%s: Failed to read payload data (err = %d)\n",
		      dev->name, ret);
		return ret;
	}

	return 0;
}

static int gdsys_ioep_get_and_reset_status(struct udevice *dev, int msgid,
					   void *tx_msg, int tx_size,
					   void *rx_msg, int rx_size)
{
	struct gdsys_ioep_priv *priv = dev_get_priv(dev);
	const u16 mask = STATE_RX_DIST_ERR | STATE_RX_LENGTH_ERR |
			 STATE_RX_FRAME_CTR_ERR | STATE_RX_FCS_ERR |
			 STATE_RX_PACKET_DROPPED | STATE_TX_ERR;
	u16 *status = rx_msg;

	gdsys_ioep_get(priv->map, rx_tx_status, status);

	gdsys_ioep_set(priv->map, rx_tx_status, *status);

	return (*status & mask) ? 1 : 0;
}

static const struct misc_ops gdsys_ioep_ops = {
	.set_enabled = gdsys_ioep_set_receive,
	.write = gdsys_ioep_send,
	.read = gdsys_ioep_receive,
	.call = gdsys_ioep_get_and_reset_status,
};

static int gdsys_ioep_probe(struct udevice *dev)
{
	struct gdsys_ioep_priv *priv = dev_get_priv(dev);
	int ret;

	ret = regmap_init_mem(dev_ofnode(dev), &priv->map);
	if (ret) {
		debug("%s: Could not initialize regmap (err = %d)",
		      dev->name, ret);
		return ret;
	}

	priv->state = false;

	return 0;
}

static const struct udevice_id gdsys_ioep_ids[] = {
	{ .compatible = "gdsys,io-endpoint" },
	{ }
};

U_BOOT_DRIVER(gdsys_ioep) = {
	.name           = "gdsys_ioep",
	.id             = UCLASS_MISC,
	.ops		= &gdsys_ioep_ops,
	.flags		= DM_UC_FLAG_SEQ_ALIAS,
	.of_match       = gdsys_ioep_ids,
	.probe          = gdsys_ioep_probe,
	.priv_auto_alloc_size = sizeof(struct gdsys_ioep_priv),
};