1 // SPDX-License-Identifier: GPL-2.0+
4 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
6 * based on the cmd_ioloop driver/command, which is
9 * Dirk Eibach, Guntermann & Drunck GmbH, dirk.eibach@gdsys.cc
11 * SPDX-License-Identifier: GPL-2.0+
20 #include "gdsys_ioep.h"
23 * struct gdsys_ioep_priv - Private data structure for IOEP devices
24 * @map: Register map to be used for the device
25 * @state: Flag to keep the current status of the RX control (enabled/disabled)
27 struct gdsys_ioep_priv {
33 * enum last_spec - Convenience enum for read data sanity check
34 * @READ_DATA_IS_LAST: The data to be read should be the final data of the
36 * @READ_DATA_IS_NOT_LAST: The data to be read should not be the final data of
41 READ_DATA_IS_NOT_LAST,
44 static int gdsys_ioep_set_receive(struct udevice *dev, bool val)
46 struct gdsys_ioep_priv *priv = dev_get_priv(dev);
49 priv->state = !priv->state;
52 state = CTRL_PROC_RECEIVE_ENABLE;
54 state = ~CTRL_PROC_RECEIVE_ENABLE;
56 gdsys_ioep_set(priv->map, tx_control, state);
59 /* Set device address to dummy 1 */
60 gdsys_ioep_set(priv->map, device_address, 1);
66 static int gdsys_ioep_send(struct udevice *dev, int offset,
67 const void *buf, int size)
69 struct gdsys_ioep_priv *priv = dev_get_priv(dev);
73 for (k = 0; k < size; ++k)
74 gdsys_ioep_set(priv->map, transmit_data, *(p++));
76 gdsys_ioep_set(priv->map, tx_control, CTRL_PROC_RECEIVE_ENABLE |
77 CTRL_FLUSH_TRANSMIT_BUFFER);
83 * receive_byte_buffer() - Read data from a IOEP device
84 * @dev: The IOEP device to read data from
85 * @len: The length of the data to read
86 * @buffer: The buffer to read the data into
87 * @last_spec: Flag to indicate if the data to be read in this call should be
88 * the final data of the current packet (i.e. it should be empty
91 * Return: 0 if OK, -ve on error
93 static int receive_byte_buffer(struct udevice *dev, uint len,
94 u16 *buffer, enum last_spec last_spec)
96 struct gdsys_ioep_priv *priv = dev_get_priv(dev);
100 for (k = 0; k < len; ++k) {
103 gdsys_ioep_get(priv->map, receive_data, buffer++);
105 gdsys_ioep_get(priv->map, rx_tx_status, &rx_tx_status);
107 * Sanity check: If the data read should have been the last,
108 * but wasn't, something is wrong
110 if (k == (len - 1) && (last_spec == READ_DATA_IS_NOT_LAST ||
111 rx_tx_status & STATE_RX_DATA_LAST))
116 debug("%s: Error while receiving bufer (err = %d)\n",
122 static int gdsys_ioep_receive(struct udevice *dev, int offset, void *buf,
126 struct io_generic_packet header;
128 const int header_words = sizeof(struct io_generic_packet) / sizeof(u16);
131 /* Read the packet header */
132 ret = receive_byte_buffer(dev, header_words, p, READ_DATA_IS_NOT_LAST);
134 debug("%s: Failed to read header data (err = %d)\n",
139 memcpy(&header, p, header_words * sizeof(u16));
142 /* Get payload data length */
143 len = (header.packet_length + 1) / sizeof(u16);
145 /* Read the packet payload */
146 ret = receive_byte_buffer(dev, len, p, READ_DATA_IS_LAST);
148 debug("%s: Failed to read payload data (err = %d)\n",
156 static int gdsys_ioep_get_and_reset_status(struct udevice *dev, int msgid,
157 void *tx_msg, int tx_size,
158 void *rx_msg, int rx_size)
160 struct gdsys_ioep_priv *priv = dev_get_priv(dev);
161 const u16 mask = STATE_RX_DIST_ERR | STATE_RX_LENGTH_ERR |
162 STATE_RX_FRAME_CTR_ERR | STATE_RX_FCS_ERR |
163 STATE_RX_PACKET_DROPPED | STATE_TX_ERR;
164 u16 *status = rx_msg;
166 gdsys_ioep_get(priv->map, rx_tx_status, status);
168 gdsys_ioep_set(priv->map, rx_tx_status, *status);
170 return (*status & mask) ? 1 : 0;
173 static const struct misc_ops gdsys_ioep_ops = {
174 .set_enabled = gdsys_ioep_set_receive,
175 .write = gdsys_ioep_send,
176 .read = gdsys_ioep_receive,
177 .call = gdsys_ioep_get_and_reset_status,
180 static int gdsys_ioep_probe(struct udevice *dev)
182 struct gdsys_ioep_priv *priv = dev_get_priv(dev);
185 ret = regmap_init_mem(dev_ofnode(dev), &priv->map);
187 debug("%s: Could not initialize regmap (err = %d)",
197 static const struct udevice_id gdsys_ioep_ids[] = {
198 { .compatible = "gdsys,io-endpoint" },
202 U_BOOT_DRIVER(gdsys_ioep) = {
203 .name = "gdsys_ioep",
205 .ops = &gdsys_ioep_ops,
206 .flags = DM_UC_FLAG_SEQ_ALIAS,
207 .of_match = gdsys_ioep_ids,
208 .probe = gdsys_ioep_probe,
209 .priv_auto = sizeof(struct gdsys_ioep_priv),