summaryrefslogtreecommitdiff
path: root/chip/g/usart.c
blob: 5412f8e1318ba5608aa27125684eb5489a2487eb (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* Copyright 2016 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifdef BOARD_CR50
#include "ec_comm.h"
#endif
#include "queue.h"
#include "queue_policies.h"
#ifdef CONFIG_STREAM_SIGNATURE
#include "signing.h"
#endif
#ifdef CONFIG_UART_BITBANG
#include "uart_bitbang.h"
#endif
#include "uartn.h"
#include "usart.h"
#include "usb-stream.h"

#define USE_UART_INTERRUPTS (!(defined(CONFIG_CUSTOMIZED_RO) && \
defined(SECTION_IS_RO)))
#define QUEUE_SIZE 64
/*
 * Want to be able to accumulate larger amounts of data while USB is
 * momentarily stalled for whatever reason.
 */
#define QUEUE_SIZE_UART_RX 512

#ifdef CONFIG_STREAM_SIGNATURE
/*
 * When signing over streaming data, up the relevant queue sizes.
 */
#define QUEUE_SIZE_SIG_IN  1024
#define QUEUE_SIZE_USB_IN  8192
#define QUEUE_SIZE_UART_IN 1024
#else
#define QUEUE_SIZE_SIG_IN  QUEUE_SIZE
#define QUEUE_SIZE_USB_IN  QUEUE_SIZE
#define QUEUE_SIZE_UART_IN QUEUE_SIZE
#endif


#ifdef CONFIG_STREAM_USART1
struct usb_stream_config const ap_usb;
struct usart_config const ap_uart;

#ifdef CONFIG_STREAM_SIGNATURE
/*
 * This code adds the ability to capture UART data received, and
 * sign it with H1's key. This allows the log output to be verified
 * as actual UART output from this board.
 *
 * This functionality is enabled by redirecting the UART receive queue
 * to feed into the signing module rather than the usb tx. After being
 * added to the running hash, the data is then pushed by the signer
 * into the usb tx queue.
 */
struct signer_config const sig;
static struct queue const ap_uart_output =
	QUEUE_DIRECT(QUEUE_SIZE_SIG_IN, uint8_t, ap_uart.producer,
		     sig.consumer);
static struct queue const sig_to_usb =
	QUEUE_DIRECT(QUEUE_SIZE_USB_IN, uint8_t, sig.producer,
		     ap_usb.consumer);

SIGNER_CONFIG(sig, stream_uart, sig_to_usb, ap_uart_output);

#else  /* Not CONFIG_STREAM_SIGNATURE */
static struct queue const ap_uart_output =
	QUEUE_DIRECT(QUEUE_SIZE_UART_RX, uint8_t,
		     ap_uart.producer, ap_usb.consumer);
#endif

static struct queue const ap_usb_to_uart =
	QUEUE_DIRECT(QUEUE_SIZE_UART_IN, uint8_t, ap_usb.producer,
		     ap_uart.consumer);

/*
 * AP UART data is sent to the ap_uart_output queue, and received from
 * the ap_usb_to_uart queue. The ap_uart_output queue is received by the
 * USB bridge, or if a signer is enabled, received by the signer, which then
 * passes the data to the USB bridge after processing it.
 */
USART_CONFIG(ap_uart,
	     UART_AP,
	     ap_uart_output,
	     ap_usb_to_uart);

/*
 * The UART USB bridge receives character data from the UART's queue,
 * unless signing is enabled, in which case it receives data from the
 * signer's queue, after the signer has received it from the UART and
 * processed it.
 */
USB_STREAM_CONFIG(ap_usb,
		  USB_IFACE_AP,
		  USB_STR_AP_NAME,
		  USB_EP_AP,
		  USB_MAX_PACKET_SIZE,
		  USB_MAX_PACKET_SIZE,
		  ap_usb_to_uart,
#ifdef CONFIG_STREAM_SIGNATURE
		  sig_to_usb)
#else
		  ap_uart_output)
#endif
#endif  /* CONFIG_STREAM_USART1 */

#ifdef CONFIG_STREAM_USART2
struct usb_stream_config const ec_usb;
struct usart_config const ec_uart;

static struct queue const ec_uart_to_usb =
	QUEUE_DIRECT(QUEUE_SIZE_UART_RX, uint8_t,
		     ec_uart.producer, ec_usb.consumer);
static struct queue const ec_usb_to_uart =
	QUEUE_DIRECT(QUEUE_SIZE, uint8_t, ec_usb.producer, ec_uart.consumer);

USART_CONFIG(ec_uart,
	     UART_EC,
	     ec_uart_to_usb,
	     ec_usb_to_uart);

USB_STREAM_CONFIG(ec_usb,
		  USB_IFACE_EC,
		  USB_STR_EC_NAME,
		  USB_EP_EC,
		  USB_MAX_PACKET_SIZE,
		  USB_MAX_PACKET_SIZE,
		  ec_usb_to_uart,
		  ec_uart_to_usb)
#endif

#ifdef BOARD_CR50
static uint8_t ec_bridge_enabled_;
static uint8_t ec_bridge_tx_enabled_;

void uart_ec_bridge_enable(int enable, int write)
{
	write = enable && write;

	if (write && !ec_bridge_tx_enabled_)
		task_trigger_irq(GC_IRQNUM_UART2_TXINT);

	ec_bridge_enabled_ = enable;
	ec_bridge_tx_enabled_ = write;
}

int uart_ec_bridge_is_enabled(void)
{
	return !!ec_bridge_enabled_;
}

int uart_ec_bridge_tx_is_enabled(void)
{
	return !!ec_bridge_tx_enabled_;
}

#endif  /* BOARD_CR50 */

void get_data_from_usb(struct usart_config const *config)
{
	struct queue const *uart_out = config->consumer.queue;
	int c;

	/* Copy output from buffer until TX fifo full or output buffer empty */
	while (queue_count(uart_out) && QUEUE_REMOVE_UNITS(uart_out, &c, 1))
		uartn_write_char(config->uart, c);

	/* If output buffer is empty, disable transmit interrupt */
	if (!queue_count(uart_out))
		uartn_tx_stop(config->uart);
}

void send_data_to_usb(struct usart_config const *config)
{
	struct queue const *uart_in = config->producer.queue;
	int uart = config->uart;
	size_t count;
	size_t q_room;
	size_t tail;
	size_t mask;

	q_room = queue_space(uart_in);

	mask = uart_in->buffer_units_mask;
	tail = uart_in->state->tail & mask;
	count = 0;

#ifdef BOARD_CR50
	if (ec_comm_is_uart_in_packet_mode(uart)) {
		/*
		 * Even if UART-to-USB data queue is full (count == q_room),
		 * It should drain UART queue, so that an EC packet
		 * can be processed. In this case, EC console data
		 * shall be lost anyway.
		 */
		while (uartn_rx_available(uart)) {
			uint8_t ch = uartn_read_char(uart);

			if (ec_comm_process_packet(ch))
				continue;

			if ((count != q_room) && uart_ec_bridge_is_enabled()) {
				uart_in->buffer[tail] = ch;
				tail = (tail + 1) & mask;
				count++;
			}
		}
		if (count)
			queue_advance_tail(uart_in, count);
		return;
	}

	/*
	 * If UART-to-USB bridging is not allowed, do not put any output
	 * data to uart_in queue.
	 */
	if ((uart == UART_EC) && !uart_ec_bridge_is_enabled())
		return;
#endif  /* BOARD_CR50 */

	while ((count != q_room) && uartn_rx_available(uart)) {
		uart_in->buffer[tail] = uartn_read_char(uart);
		tail = (tail + 1) & mask;
		count++;
	}
	if (count)
		queue_advance_tail(uart_in, count);
}

static void uart_read(struct producer const *producer, size_t count)
{
}

static void uart_written(struct consumer const *consumer, size_t count)
{
	struct usart_config const *config =
		DOWNCAST(consumer, struct usart_config, consumer);

#ifdef CONFIG_UART_BITBANG
	if (uart_bitbang_is_enabled() &&
	    (config->uart == bitbang_config.uart)) {
		uart_bitbang_drain_tx_queue(consumer->queue);
		return;
	}
#endif

#ifdef BOARD_CR50
	if (config->uart == UART_EC) {
		/*
		 * If USB-to-UART bridging is disabled, do not forward data.
		 * Otherwise, data could be pushed into UART TX FIFO, and
		 * transferred to EC eventually once EC-CR50 communication
		 * enables EC UART.
		 */
		if (!uart_ec_bridge_tx_is_enabled()) {
			/*
			 * Empty the RX queue, so that host won't suffer from
			 * congestion. Also, if data remains in the queue, then
			 * they might be transferred when UART TX gets enabled
			 * in future.
			 */
			queue_advance_head(consumer->queue,
					   queue_count(consumer->queue));
			return;
		}

		/*
		 * If EC-CR50 communication is on-going, then let's not forward
		 * console input to EC for now.
		 */
		if (ec_comm_is_uart_in_packet_mode(UART_EC))
			return;
	}
#endif  /* BOARD_CR50 */

	if (uartn_tx_ready(config->uart) && queue_count(consumer->queue))
		uartn_tx_start(config->uart);
}

struct producer_ops const uart_producer_ops = {
	.read = uart_read,
};

struct consumer_ops const uart_consumer_ops = {
	.written = uart_written,
};

#if USE_UART_INTERRUPTS
#ifdef CONFIG_STREAM_USART1
/*
 * Interrupt handlers for UART1
 */
CONFIGURE_INTERRUPTS(ap_uart,
		     GC_IRQNUM_UART1_RXINT,
		     GC_IRQNUM_UART1_TXINT)
#endif

#ifdef CONFIG_STREAM_USART2
/*
 * Interrupt handlers for UART2
 */
CONFIGURE_INTERRUPTS(ec_uart,
		     GC_IRQNUM_UART2_RXINT,
		     GC_IRQNUM_UART2_TXINT)
#endif
#endif