summaryrefslogtreecommitdiff
path: root/board/cr50/rdd.c
blob: 2d1403ec02a86d6956da3d4f56cc60b244c373c5 (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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/* Copyright 2016 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "case_closed_debug.h"  /* For ccd_ext_is_enabled() */
#include "ccd_config.h"
#include "console.h"
#include "gpio.h"
#include "hooks.h"
#include "i2c.h"
#include "rbox.h"
#include "rdd.h"
#include "registers.h"
#include "system.h"
#include "uart_bitbang.h"
#include "uartn.h"
#include "usb_api.h"
#include "usb_console.h"
#include "usb_i2c.h"
#include "usb_spi.h"

/* Include the dazzlingly complex macro to instantiate the USB SPI config */
USB_SPI_CONFIG(ccd_usb_spi, USB_IFACE_SPI, USB_EP_SPI);

#define CPRINTS(format, args...) cprints(CC_USB, format, ## args)
#define CPRINTF(format, args...) cprintf(CC_USB, format, ## args)

static enum device_state state = DEVICE_STATE_INIT;

/* Flags for CCD blocking */
enum ccd_block_flags {
	/*
	 * UARTs.  Disabling these can be helpful if the AP or EC is doing
	 * something which creates an interrupt storm on these ports.
	 */
	CCD_BLOCK_AP_UART = BIT(0),
	CCD_BLOCK_EC_UART = BIT(1),

	/*
	 * Any ports shared with servo.  Disabling these will stop CCD from
	 * interfering with servo, in the case where both CCD and servo is
	 * connected but servo isn't properly detected.
	 */
	CCD_BLOCK_SERVO_SHARED = BIT(2),

	/*
	 * In case of broken hardware use IGNORE_SERVO to bypass the "servo
	 * connected check". If cr50 thinks servo is connected, it won't enable
	 * the AP or EC uart. Using IGNORE_SERVO will force cr50 to enable uart
	 * even if it thinks servo is connected.
	 *
	 * ONLY USE THIS IF SERVO IS DISCONNECTED. If you force enable AP and EC
	 * uart while servo is connected, it could break the hardware and the
	 * ccd uart could become permanently unusable.
	 */
	CCD_BLOCK_IGNORE_SERVO = BIT(3)
};

/* Which UARTs are blocked by console command */
static uint8_t ccd_block;

int ccd_ext_is_enabled(void)
{
	return state == DEVICE_STATE_CONNECTED;
}

/* If the UART TX is connected the pinmux select will have a non-zero value */
int uart_tx_is_connected(int uart)
{
	if (uart == UART_AP)
		return GREAD(PINMUX, DIOA7_SEL);

	/*
	 * Enabling bit bang programming mode disconnected the EC UART from
	 * the external pin, but muxed DIOB5 to a different GPIO bit.
	 */
	return !uart_bitbang_is_enabled() && GREAD(PINMUX, DIOB5_SEL);
}

/**
 * Connect the UART pin to the given signal
 *
 * @param uart		the uart peripheral number
 * @param signal	the pinmux selector value for the gpio or peripheral
 *			function. 0 to disable the output.
 */
static void uart_select_tx(int uart, int signal)
{
	if (uart == UART_AP) {
		GWRITE(PINMUX, DIOA7_SEL, signal);
	} else {
		GWRITE(PINMUX, DIOB5_SEL, signal);

		/* Remove the pulldown when we are driving the signal */
		GWRITE_FIELD(PINMUX, DIOB5_CTL, PD, signal ? 0 : 1);
	}
}

void uartn_tx_connect(int uart)
{
	/*
	 * Don't drive TX unless the debug cable is connected (we have
	 * something to transmit) and servo is disconnected (we won't be
	 * drive-fighting with servo).
	 */
	if (servo_is_connected() || !ccd_ext_is_enabled())
		return;

	if (uart == UART_AP) {
		if (!ccd_is_cap_enabled(CCD_CAP_GSC_TX_AP_RX))
			return;

		if (!ap_uart_is_on())
			return;

		uart_select_tx(UART_AP, GC_PINMUX_UART1_TX_SEL);
	} else {
		if (!ccd_is_cap_enabled(CCD_CAP_GSC_TX_EC_RX))
			return;

		if (!ec_is_on())
			return;

		uart_select_tx(UART_EC, GC_PINMUX_UART2_TX_SEL);
	}
}

void uartn_tx_disconnect(int uart)
{
	/* Disconnect the TX pin from UART peripheral */
	uart_select_tx(uart, 0);
}

/*
 * Flags for the current CCD device state.  This is used for determining what
 * hardware devices we've enabled now, and which we want enabled.
 */
enum ccd_state_flag {
	/* Flags for individual devices/ports */

	/* AP UART is enabled.  RX-only, unless TX is also enabled. */
	CCD_ENABLE_UART_AP		= BIT(0),

	/* AP UART transmit is enabled.  Requires AP UART enabled. */
	CCD_ENABLE_UART_AP_TX		= BIT(1),

	/* EC UART is enabled.  RX-only, unless TX is also enabled. */
	CCD_ENABLE_UART_EC		= BIT(2),

	/* EC UART transmit is enabled.  Requires EC UART enabled. */
	CCD_ENABLE_UART_EC_TX		= BIT(3),

	/*
	 * EC UART bit-banging is enabled.  Requires EC UART enabled, and
	 * blocks EC UART transmit.
	 */
	CCD_ENABLE_UART_EC_BITBANG	= BIT(4),

	/* I2C port is enabled */
	CCD_ENABLE_I2C			= BIT(5),

	/* SPI port is enabled for AP and/or EC flash */
	CCD_ENABLE_SPI			= BIT(6),
};

int console_is_restricted(void)
{
	return !ccd_is_cap_enabled(CCD_CAP_GSC_RESTRICTED_CONSOLE);
}

/**
 * Return the currently enabled state flags (see enum ccd_state_flag).
 */
static uint32_t get_state_flags(void)
{
	uint32_t flags_now = 0;

	if (uartn_is_enabled(UART_AP))
		flags_now |= CCD_ENABLE_UART_AP;
	if (uart_tx_is_connected(UART_AP))
		flags_now |= CCD_ENABLE_UART_AP_TX;
	if (uartn_is_enabled(UART_EC))
		flags_now |= CCD_ENABLE_UART_EC;
	if (uart_tx_is_connected(UART_EC))
		flags_now |= CCD_ENABLE_UART_EC_TX;

#ifdef CONFIG_UART_BITBANG
	if (uart_bitbang_is_enabled())
		flags_now |= CCD_ENABLE_UART_EC_BITBANG;
#endif

	if (usb_i2c_board_is_enabled())
		flags_now |= CCD_ENABLE_I2C;

	if (ccd_usb_spi.state->enabled_device)
		flags_now |= CCD_ENABLE_SPI;

	return flags_now;
}

/**
 * Print the state flags to the specified output channel
 *
 * @param channel	Console channel
 * @param flags		Flags to print
 */
static void print_state_flags(enum console_channel channel, uint32_t flags)
{
	if (flags & CCD_ENABLE_UART_AP)
		cprintf(channel, " UARTAP");
	if (flags & CCD_ENABLE_UART_AP_TX)
		cprintf(channel, "+TX");
	if (flags & CCD_ENABLE_UART_EC)
		cprintf(channel, " UARTEC");
	if (flags & CCD_ENABLE_UART_EC_TX)
		cprintf(channel, "+TX");
	if (flags & CCD_ENABLE_UART_EC_BITBANG)
		cprintf(channel, "+BB");
	if (flags & CCD_ENABLE_I2C)
		cprintf(channel, " I2C");
	if (flags & CCD_ENABLE_SPI)
		cprintf(channel, " SPI");
}

static void ccd_state_change_hook(void)
{
	uint32_t flags_now;
	uint32_t flags_want = 0;
	uint32_t delta;

	/* Check what's enabled now */
	flags_now = get_state_flags();

	/* Start out by figuring what flags we might want enabled */

	/* Enable EC/AP UART RX if that device is on */
	if (ap_uart_is_on())
		flags_want |= CCD_ENABLE_UART_AP;
	if (ec_is_rx_allowed())
		flags_want |= CCD_ENABLE_UART_EC;

#ifdef CONFIG_UART_BITBANG
	if (uart_bitbang_is_wanted())
		flags_want |= CCD_ENABLE_UART_EC_BITBANG;
#endif

	/*
	 * External CCD will try to enable all the ports. If it's disabled,
	 * disable all ports.
	 */
	if (ccd_ext_is_enabled())
		flags_want |= (CCD_ENABLE_UART_AP_TX | CCD_ENABLE_UART_EC_TX |
			       CCD_ENABLE_I2C | CCD_ENABLE_SPI);
	else
		flags_want = 0;

	/* Then disable flags we can't have */

	/* Servo takes over UART TX, I2C, and SPI. */
	if (servo_is_connected() || (ccd_block & CCD_BLOCK_SERVO_SHARED))
		flags_want &= ~(CCD_ENABLE_UART_AP_TX | CCD_ENABLE_UART_EC_TX |
				CCD_ENABLE_UART_EC_BITBANG | CCD_ENABLE_I2C |
				CCD_ENABLE_SPI);

	/* Disable based on capabilities */
	if (!ccd_is_cap_enabled(CCD_CAP_GSC_RX_AP_TX))
		flags_want &= ~CCD_ENABLE_UART_AP;
	if (!ccd_is_cap_enabled(CCD_CAP_GSC_TX_AP_RX))
		flags_want &= ~CCD_ENABLE_UART_AP_TX;
	if (!ccd_is_cap_enabled(CCD_CAP_GSC_RX_EC_TX))
		flags_want &= ~CCD_ENABLE_UART_EC;
	if (!ccd_is_cap_enabled(CCD_CAP_GSC_TX_EC_RX))
		flags_want &= ~(CCD_ENABLE_UART_EC_TX |
				CCD_ENABLE_UART_EC_BITBANG);
	if (!ccd_is_cap_enabled(CCD_CAP_I2C))
		flags_want &= ~CCD_ENABLE_I2C;

	/*
	 * EC and AP flash block on a per-packet basis, but if we don't have
	 * access to either one, turn off SPI.
	 */
	if (!ccd_is_cap_enabled(CCD_CAP_AP_FLASH) &&
	    !ccd_is_cap_enabled(CCD_CAP_EC_FLASH))
		flags_want &= ~CCD_ENABLE_SPI;

	/* EC UART TX blocked by bit-banging */
	if (flags_want & CCD_ENABLE_UART_EC_BITBANG)
		flags_want &= ~CCD_ENABLE_UART_EC_TX;

	/* UARTs can be specifically blocked by console command */
	if (ccd_block & CCD_BLOCK_AP_UART)
		flags_want &= ~CCD_ENABLE_UART_AP;
	if (ccd_block & CCD_BLOCK_EC_UART)
		flags_want &= ~CCD_ENABLE_UART_EC;

	/* UARTs are either RX-only or RX+TX, so no RX implies no TX */
	if (!(flags_want & CCD_ENABLE_UART_AP))
		flags_want &= ~CCD_ENABLE_UART_AP_TX;
	if (!(flags_want & CCD_ENABLE_UART_EC))
		flags_want &= ~CCD_ENABLE_UART_EC_TX;

	/* If no change, we're done */
	if (flags_now == flags_want)
		return;

	CPRINTF("[%pT CCD state:", PRINTF_TIMESTAMP_NOW);
	print_state_flags(CC_USB, flags_want);
	CPRINTF("]\n");

	/* Handle turning things off */
	delta = flags_now & ~flags_want;
	if (delta & CCD_ENABLE_UART_AP)
		uartn_disable(UART_AP);
	if (delta & CCD_ENABLE_UART_AP_TX)
		uartn_tx_disconnect(UART_AP);
	if (delta & CCD_ENABLE_UART_EC)
		uartn_disable(UART_EC);
	if (delta & CCD_ENABLE_UART_EC_TX)
		uartn_tx_disconnect(UART_EC);
#ifdef CONFIG_UART_BITBANG
	if (delta & CCD_ENABLE_UART_EC_BITBANG)
		uart_bitbang_disable();
#endif
	if (delta & CCD_ENABLE_I2C)
		usb_i2c_board_disable();
	if (delta & CCD_ENABLE_SPI)
		usb_spi_enable(&ccd_usb_spi, 0);

	/* Handle turning things on */
	delta = flags_want & ~flags_now;
	if (delta & CCD_ENABLE_UART_AP)
		uartn_enable(UART_AP);
	if (delta & CCD_ENABLE_UART_AP_TX)
		uartn_tx_connect(UART_AP);
	if (delta & CCD_ENABLE_UART_EC)
		uartn_enable(UART_EC);
	if (delta & CCD_ENABLE_UART_EC_TX)
		uartn_tx_connect(UART_EC);
#ifdef CONFIG_UART_BITBANG
	if (delta & CCD_ENABLE_UART_EC_BITBANG) {
		/*
		 * Servo detect interrupt will be re-enabled by the
		 * servo_detect() poll once bit bang mode is disabled.
		 */
		gpio_disable_interrupt(GPIO_DETECT_SERVO);
		uart_bitbang_enable();
	}
#endif
	if (delta & CCD_ENABLE_I2C)
		usb_i2c_board_enable();
	if (delta & CCD_ENABLE_SPI)
		usb_spi_enable(&ccd_usb_spi, 1);
}
DECLARE_DEFERRED(ccd_state_change_hook);

void ccd_update_state(void)
{
	/*
	 * Use a deferred call to serialize changes from CCD config, RDD
	 * attach/detach, EC/AP startup or shutdown, etc.
	 */
	hook_call_deferred(&ccd_state_change_hook_data, 0);
}

/*****************************************************************************/

static void ccd_ext_detect(void)
{
	/* The CCD mode pin is active low. */
	int enable = !gpio_get_level(GPIO_CCD_MODE_L);

	if (enable == ccd_ext_is_enabled())
		return;

	if (enable) {
		/*
		 * If we're not disconnected, release USB to ensure it's in a
		 * good state before we usb_init().  This matches what
		 * common/case_closed_debug.c does.
		 *
		 * Not sure exactly why this is necessary.  It could be because
		 * that also has CCD_MODE_PARTIAL, and the only way to go
		 * cleanly between ENABLED and PARTIAL is to disable things and
		 * then re-enable only what's needed?
		 *
		 * TODO(rspangler): Figure out whether we can delete this.
		 */
		if (state != DEVICE_STATE_DISCONNECTED)
			usb_release();

		CPRINTS("CCD EXT enable");
		state = DEVICE_STATE_CONNECTED;

		usb_init();
		usb_console_enable(1, 0);
	} else {
		CPRINTS("CCD EXT disable");
		state = DEVICE_STATE_DISCONNECTED;

		usb_release();
		usb_console_enable(0, 0);
	}

	ccd_update_state();
}
DECLARE_HOOK(HOOK_SECOND, ccd_ext_detect, HOOK_PRIO_DEFAULT);

static void print_ccd_ports_blocked(void)
{
	/* Regardless, print current state */
	ccputs("CCD ports blocked:");
	if (ccd_block & CCD_BLOCK_AP_UART)
		ccputs(" AP");
	if (ccd_block & CCD_BLOCK_EC_UART)
		ccputs(" EC");
	if (ccd_block & CCD_BLOCK_SERVO_SHARED)
		ccputs(" SERVO");
	if (ccd_block & CCD_BLOCK_IGNORE_SERVO) {
		ccputs(" IGNORE_SERVO");
		ccputs("\nWARNING: enabling UART while servo is connected may "
		       "damage hardware");
	}
	if (!ccd_block)
		ccputs(" (none)");
	ccputs("\n");
}

static int command_ccd_state(int argc, char **argv)
{
	print_ap_state();
	print_ap_uart_state();
	print_ec_state();
	print_rdd_state();
	print_servo_state();

	ccprintf("CCD EXT: %s\n",
		 ccd_ext_is_enabled() ? "enabled" : "disabled");

	ccprintf("State flags:");
	print_state_flags(CC_COMMAND, get_state_flags());
	ccprintf("\n");

	print_ccd_ports_blocked();

	return EC_SUCCESS;
}
DECLARE_SAFE_CONSOLE_COMMAND(ccdstate, command_ccd_state,
			     "",
			     "Print the case closed debug device state");

static int command_ccd_block(int argc, char **argv)
{
	uint8_t block_flag = 0;
	int new_state;

	if (argc == 3) {
		if (!strcasecmp(argv[1], "AP"))
			block_flag = CCD_BLOCK_AP_UART;
		else if (!strcasecmp(argv[1], "EC"))
			block_flag = CCD_BLOCK_EC_UART;
		else if (!strcasecmp(argv[1], "SERVO"))
			block_flag = CCD_BLOCK_SERVO_SHARED;
		else if (!strcasecmp(argv[1], "IGNORE_SERVO"))
			block_flag = CCD_BLOCK_IGNORE_SERVO;
		else
			return EC_ERROR_PARAM1;

		if (!parse_bool(argv[2], &new_state))
			return EC_ERROR_PARAM2;

		if (new_state)
			ccd_block |= block_flag;
		else
			ccd_block &= ~block_flag;

		if (block_flag == CCD_BLOCK_IGNORE_SERVO)
			servo_ignore(new_state);

		/* Update blocked state in deferred function */
		ccd_update_state();
	}

	print_ccd_ports_blocked();

	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(ccdblock, command_ccd_block,
			"[<AP | EC | SERVO | IGNORE_SERVO> [BOOLEAN]]",
			"Force CCD ports disabled");