1 /*
2  * Copyright (c) 2024 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  */
7 
8 #ifndef ZEPHYR_MCTP_UART_H_
9 #define ZEPHYR_MCTP_UART_H_
10 
11 #include <stdint.h>
12 #include <zephyr/kernel.h>
13 #include <zephyr/device.h>
14 #include <libmctp.h>
15 
16 /**
17  * @brief An MCTP binding for Zephyr's asynchronous UART interface
18  */
19 struct mctp_binding_uart {
20 	/** @cond INTERNAL_HIDDEN */
21 	struct mctp_binding binding;
22 	const struct device *dev;
23 
24 	/* receive buffers and state */
25 	uint8_t rx_buf[2][256];
26 	bool rx_buf_used[2];
27 	struct mctp_pktbuf *rx_pkt;
28 	uint8_t rx_exp_len;
29 	uint16_t rx_fcs;
30 	uint16_t rx_fcs_calc;
31 	enum {
32 		STATE_WAIT_SYNC_START,
33 		STATE_WAIT_REVISION,
34 		STATE_WAIT_LEN,
35 		STATE_DATA,
36 		STATE_DATA_ESCAPED,
37 		STATE_WAIT_FCS1,
38 		STATE_WAIT_FCS2,
39 		STATE_WAIT_SYNC_END,
40 	} rx_state;
41 	int rx_res;
42 
43 	/* staging buffer for tx */
44 	uint8_t tx_buf[256];
45 	int tx_res;
46 
47 	/** @endcond INTERNAL_HIDDEN */
48 };
49 
50 /**
51  * @brief Start the receive of a single mctp message
52  *
53  * Will read a single mctp message from the uart.
54  *
55  * @param uart MCTP UART binding
56  */
57 void mctp_uart_start_rx(struct mctp_binding_uart *uart);
58 
59 /** @cond INTERNAL_HIDDEN */
60 int mctp_uart_start(struct mctp_binding *binding);
61 int mctp_uart_tx(struct mctp_binding *binding, struct mctp_pktbuf *pkt);
62 /** @endcond INTERNAL_HIDDEN */
63 
64 /**
65  * @brief Statically define a MCTP bus binding for a UART
66  *
67  * @param _name Symbolic name of the bus binding variable
68  * @param _dev UART device
69  */
70 #define MCTP_UART_DT_DEFINE(_name, _dev)                                                           \
71 	struct mctp_binding_uart _name = {                                                         \
72 		.binding =                                                                         \
73 			{                                                                          \
74 				.name = STRINGIFY(_name), .version = 1,                            \
75 						  .pkt_size = MCTP_PACKET_SIZE(MCTP_BTU),          \
76 						  .pkt_header = 0, .pkt_trailer = 0,               \
77 						  .start = mctp_uart_start, .tx = mctp_uart_tx,    \
78 				},                                                                 \
79 				.dev = _dev,                                                       \
80 				.rx_state = STATE_WAIT_SYNC_START,                                 \
81 				.rx_pkt = NULL,                                                    \
82 				.rx_res = 0,                                                       \
83 				.tx_res = 0,                                                       \
84 	};
85 
86 #endif /* ZEPHYR_MCTP_UART_H_ */
87