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+
19 #include "gdsys_ioep.h"
22 * struct gdsys_ioep_priv - Private data structure for IOEP devices
23 * @map: Register map to be used for the device
24 * @state: Flag to keep the current status of the RX control (enabled/disabled)
26 struct gdsys_ioep_priv {
32 * enum last_spec - Convenience enum for read data sanity check
33 * @READ_DATA_IS_LAST: The data to be read should be the final data of the
35 * @READ_DATA_IS_NOT_LAST: The data to be read should not be the final data of
40 READ_DATA_IS_NOT_LAST,
43 static int gdsys_ioep_set_receive(struct udevice *dev, bool val)
45 struct gdsys_ioep_priv *priv = dev_get_priv(dev);
48 priv->state = !priv->state;
51 state = CTRL_PROC_RECEIVE_ENABLE;
53 state = ~CTRL_PROC_RECEIVE_ENABLE;
55 gdsys_ioep_set(priv->map, tx_control, state);
58 /* Set device address to dummy 1 */
59 gdsys_ioep_set(priv->map, device_address, 1);
65 static int gdsys_ioep_send(struct udevice *dev, int offset,
66 const void *buf, int size)
68 struct gdsys_ioep_priv *priv = dev_get_priv(dev);
72 for (k = 0; k < size; ++k)
73 gdsys_ioep_set(priv->map, transmit_data, *(p++));
75 gdsys_ioep_set(priv->map, tx_control, CTRL_PROC_RECEIVE_ENABLE |
76 CTRL_FLUSH_TRANSMIT_BUFFER);
82 * receive_byte_buffer() - Read data from a IOEP device
83 * @dev: The IOEP device to read data from
84 * @len: The length of the data to read
85 * @buffer: The buffer to read the data into
86 * @last_spec: Flag to indicate if the data to be read in this call should be
87 * the final data of the current packet (i.e. it should be empty
90 * Return: 0 if OK, -ve on error
92 static int receive_byte_buffer(struct udevice *dev, uint len,
93 u16 *buffer, enum last_spec last_spec)
95 struct gdsys_ioep_priv *priv = dev_get_priv(dev);
99 for (k = 0; k < len; ++k) {
102 gdsys_ioep_get(priv->map, receive_data, buffer++);
104 gdsys_ioep_get(priv->map, rx_tx_status, &rx_tx_status);
106 * Sanity check: If the data read should have been the last,
107 * but wasn't, something is wrong
109 if (k == (len - 1) && (last_spec == READ_DATA_IS_NOT_LAST ||
110 rx_tx_status & STATE_RX_DATA_LAST))
115 debug("%s: Error while receiving bufer (err = %d)\n",
121 static int gdsys_ioep_receive(struct udevice *dev, int offset, void *buf,
125 struct io_generic_packet header;
127 const int header_words = sizeof(struct io_generic_packet) / sizeof(u16);
130 /* Read the packet header */
131 ret = receive_byte_buffer(dev, header_words, p, READ_DATA_IS_NOT_LAST);
133 debug("%s: Failed to read header data (err = %d)\n",
138 memcpy(&header, p, header_words * sizeof(u16));
141 /* Get payload data length */
142 len = (header.packet_length + 1) / sizeof(u16);
144 /* Read the packet payload */
145 ret = receive_byte_buffer(dev, len, p, READ_DATA_IS_LAST);
147 debug("%s: Failed to read payload data (err = %d)\n",
155 static int gdsys_ioep_get_and_reset_status(struct udevice *dev, int msgid,
156 void *tx_msg, int tx_size,
157 void *rx_msg, int rx_size)
159 struct gdsys_ioep_priv *priv = dev_get_priv(dev);
160 const u16 mask = STATE_RX_DIST_ERR | STATE_RX_LENGTH_ERR |
161 STATE_RX_FRAME_CTR_ERR | STATE_RX_FCS_ERR |
162 STATE_RX_PACKET_DROPPED | STATE_TX_ERR;
163 u16 *status = rx_msg;
165 gdsys_ioep_get(priv->map, rx_tx_status, status);
167 gdsys_ioep_set(priv->map, rx_tx_status, *status);
169 return (*status & mask) ? 1 : 0;
172 static const struct misc_ops gdsys_ioep_ops = {
173 .set_enabled = gdsys_ioep_set_receive,
174 .write = gdsys_ioep_send,
175 .read = gdsys_ioep_receive,
176 .call = gdsys_ioep_get_and_reset_status,
179 static int gdsys_ioep_probe(struct udevice *dev)
181 struct gdsys_ioep_priv *priv = dev_get_priv(dev);
184 ret = regmap_init_mem(dev_ofnode(dev), &priv->map);
186 debug("%s: Could not initialize regmap (err = %d)",
196 static const struct udevice_id gdsys_ioep_ids[] = {
197 { .compatible = "gdsys,io-endpoint" },
201 U_BOOT_DRIVER(gdsys_ioep) = {
202 .name = "gdsys_ioep",
204 .ops = &gdsys_ioep_ops,
205 .flags = DM_UC_FLAG_SEQ_ALIAS,
206 .of_match = gdsys_ioep_ids,
207 .probe = gdsys_ioep_probe,
208 .priv_auto_alloc_size = sizeof(struct gdsys_ioep_priv),