summaryrefslogtreecommitdiff
path: root/board/tigertail/board.c
blob: f4d0382b9c37e7d53f7fd161938d5bc746b1ff1a (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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/* Copyright 2017 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.
 */

/* Tigertail board configuration */

#include "adc.h"
#include "common.h"
#include "console.h"
#include "ec_version.h"
#include "gpio.h"
#include "hooks.h"
#include "i2c.h"
#include "ina2xx.h"
#include "queue_policies.h"
#include "registers.h"
#include "task.h"
#include "timer.h"
#include "update_fw.h"
#include "usart-stm32f0.h"
#include "usart_tx_dma.h"
#include "usart_rx_dma.h"
#include "usb_i2c.h"
#include "usb-stream.h"
#include "util.h"

#include "gpio_list.h"


#define CPRINTS(format, args...) cprints(CC_SYSTEM, format, ## args)


/******************************************************************************
 * Forward UARTs as a USB serial interface.
 */

#define USB_STREAM_RX_SIZE	16
#define USB_STREAM_TX_SIZE	16

/******************************************************************************
 * Forward USART1 as a simple USB serial interface.
 */
static struct usart_config const usart1;
struct usb_stream_config const usart1_usb;

static struct queue const usart1_to_usb = QUEUE_DIRECT(64, uint8_t,
	usart1.producer, usart1_usb.consumer);
static struct queue const usb_to_usart1 = QUEUE_DIRECT(64, uint8_t,
	usart1_usb.producer, usart1.consumer);

static struct usart_config const usart1 =
	USART_CONFIG(usart1_hw,
		usart_rx_interrupt,
		usart_tx_interrupt,
		115200,
		0,
		usart1_to_usb,
		usb_to_usart1);

USB_STREAM_CONFIG(usart1_usb,
	USB_IFACE_USART1_STREAM,
	USB_STR_USART1_STREAM_NAME,
	USB_EP_USART1_STREAM,
	USB_STREAM_RX_SIZE,
	USB_STREAM_TX_SIZE,
	usb_to_usart1,
	usart1_to_usb)


/******************************************************************************
 * Define the strings used in our USB descriptors.
 */
const void *const usb_strings[] = {
	[USB_STR_DESC]         = usb_string_desc,
	[USB_STR_VENDOR]       = USB_STRING_DESC("Google Inc."),
	[USB_STR_PRODUCT]      = USB_STRING_DESC("Tigertail"),
	[USB_STR_SERIALNO]     = 0,
	[USB_STR_VERSION]      = USB_STRING_DESC(CROS_EC_VERSION32),
	[USB_STR_I2C_NAME]     = USB_STRING_DESC("I2C"),
	[USB_STR_USART1_STREAM_NAME]  = USB_STRING_DESC("DUT UART"),
	[USB_STR_CONSOLE_NAME] = USB_STRING_DESC("Tigertail Console"),
	[USB_STR_UPDATE_NAME]  = USB_STRING_DESC("Firmware update"),
};

BUILD_ASSERT(ARRAY_SIZE(usb_strings) == USB_STR_COUNT);

/******************************************************************************
 * ADC support for SBU flip detect.
 */
/* ADC channels */
const struct adc_t adc_channels[] = {
	[ADC_SBU1] = {"SBU1", 3300, 4096, 0, STM32_AIN(6)},
	[ADC_SBU2] = {"SBU2", 3300, 4096, 0, STM32_AIN(7)},
};
BUILD_ASSERT(ARRAY_SIZE(adc_channels) == ADC_CH_COUNT);



/******************************************************************************
 * Support I2C bridging over USB.
 */

/* I2C ports */
const struct i2c_port_t i2c_ports[] = {
	{"master", I2C_PORT_MASTER, 100,
		GPIO_MASTER_I2C_SCL, GPIO_MASTER_I2C_SDA},
};
const unsigned int i2c_ports_used = ARRAY_SIZE(i2c_ports);

int usb_i2c_board_is_enabled(void) { return 1; }

/******************************************************************************
 * Console commands.
 */

/* State to indicate current GPIO config. */
static int uart_state = UART_OFF;
/* State to indicate current autodetect mode. */
static int uart_detect = UART_DETECT_AUTO;

const char *const uart_state_names[] = {
	[UART_OFF] = "off",
	[UART_ON_PP1800] = "on @ 1.8v",
	[UART_FLIP_PP1800] = "flip @ 1.8v",
	[UART_ON_PP3300] = "on @ 3.3v",
	[UART_FLIP_PP3300] = "flip @ 3.3v",
	[UART_AUTO] = "auto",
};

/* Set GPIOs to configure UART mode. */
static void set_uart_gpios(int state)
{
	int uart = GPIO_INPUT;
	int dir = 0;
	int voltage = 1;  /* 1: 1.8v, 0: 3.3v */
	int enabled = 0;

	gpio_set_level(GPIO_ST_UART_LVL_DIS, 1);

	switch (state) {
	case UART_ON_PP1800:
		uart = GPIO_ALTERNATE;
		dir = 1;
		voltage = 1;
		enabled = 1;
		break;

	case UART_FLIP_PP1800:
		uart = GPIO_ALTERNATE;
		dir = 0;
		voltage = 1;
		enabled = 1;
		break;

	case UART_ON_PP3300:
		uart = GPIO_ALTERNATE;
		dir = 1;
		voltage = 0;
		enabled = 1;
		break;

	case UART_FLIP_PP3300:
		uart = GPIO_ALTERNATE;
		dir = 0;
		voltage = 0;
		enabled = 1;
		break;

	default:
		/* Default to UART_OFF. */
		uart = GPIO_INPUT;
		dir = 0;
		enabled = 0;
	}

	/* Set level shifter direction and voltage. */
	gpio_set_level(GPIO_ST_UART_VREF, voltage);
	gpio_set_level(GPIO_ST_UART_TX_DIR, dir);
	gpio_set_level(GPIO_ST_UART_TX_DIR_N, !dir);

	/* Enable STM pinmux */
	gpio_set_flags(GPIO_USART1_TX, uart);
	gpio_set_flags(GPIO_USART1_RX, uart);

	/* Flip uart orientation if necessary. */
	STM32_USART_CR1(STM32_USART1_BASE) &= ~(STM32_USART_CR1_UE);
	if (dir)
		STM32_USART_CR2(STM32_USART1_BASE) &= ~(STM32_USART_CR2_SWAP);
	else
		STM32_USART_CR2(STM32_USART1_BASE) |= (STM32_USART_CR2_SWAP);
	STM32_USART_CR1(STM32_USART1_BASE) |= STM32_USART_CR1_UE;

	/* Enable level shifter. */
	usleep(1000);
	gpio_set_level(GPIO_ST_UART_LVL_DIS, !enabled);
}

/*
 * Detect if a UART is plugged into SBU. Tigertail UART must be off
 * for this to return useful info.
 */
static int is_low(int mv)
{
	return (mv < 190);
}

static int is_3300(int mv)
{
	return ((mv > 3000) && (mv < 3400));
}

static int is_1800(int mv)
{
	return ((mv > 1600) && (mv < 1900));
}

static int detect_uart_orientation(void)
{
	int sbu1 = adc_read_channel(ADC_SBU1);
	int sbu2 = adc_read_channel(ADC_SBU2);
	int state = UART_OFF;

	/*
	 * Here we check if one or the other SBU is 1.8v, as DUT
	 * TX should idle high.
	 */
	if (is_low(sbu1) && is_1800(sbu2))
		state = UART_ON_PP1800;
	else if (is_low(sbu2) && is_1800(sbu1))
		state = UART_FLIP_PP1800;
	else if (is_low(sbu1) && is_3300(sbu2))
		state = UART_ON_PP3300;
	else if (is_low(sbu2) && is_3300(sbu1))
		state = UART_FLIP_PP3300;
	else
		state = UART_OFF;

	return state;
}

/*
 * Detect if UART has been unplugged. Normal UARTs should
 * have both lines idling high at 1.8v.
 */
static int detect_uart_idle(void)
{
	int sbu1 = adc_read_channel(ADC_SBU1);
	int sbu2 = adc_read_channel(ADC_SBU2);
	int enabled = 0;

	if (is_1800(sbu1) && is_1800(sbu2))
		enabled = 1;

	if (is_3300(sbu1) && is_3300(sbu2))
		enabled = 1;

	return enabled;
}

/* Set the UART state and gpios, and autodetect if necessary. */
void set_uart_state(int state)
{
	if (state == UART_AUTO) {
		set_uart_gpios(UART_OFF);
		msleep(10);

		uart_detect = UART_DETECT_AUTO;
		state = detect_uart_orientation();
	} else {
		uart_detect = UART_DETECT_OFF;
	}

	uart_state = state;
	set_uart_gpios(state);
}

/*
 * Autodetect UART state:
 * We will check every 250ms, and change state if 1 second has passed
 * in the new state.
 */
void uart_sbu_tick(void)
{
	static int debounce;  /* = 0 */

	if (uart_detect != UART_DETECT_AUTO)
		return;

	if (uart_state == UART_OFF) {
		int state = detect_uart_orientation();

		if (state != UART_OFF) {
			debounce++;
			if (debounce > 4) {
				debounce = 0;
				CPRINTS("UART autoenable %s",
					uart_state_names[state]);
				uart_state = state;
				set_uart_gpios(state);
			}
			return;
		}
	} else {
		int enabled = detect_uart_idle();

		if (!enabled) {
			debounce++;
			if (debounce > 4) {
				debounce = 0;
				CPRINTS("UART autodisable");
				uart_state = UART_OFF;
				set_uart_gpios(UART_OFF);
			}
			return;
		}
	}
	debounce = 0;
}
DECLARE_HOOK(HOOK_TICK, uart_sbu_tick, HOOK_PRIO_DEFAULT);

static int command_uart(int argc, char **argv)
{
	const char *uart_state_str = "off";
	const char *uart_detect_str = "manual";

	if (argc > 1) {
		if (!strcasecmp("off", argv[1]))
			set_uart_state(UART_OFF);
		else if (!strcasecmp("on18", argv[1]))
			set_uart_state(UART_ON_PP1800);
		else if (!strcasecmp("on33", argv[1]))
			set_uart_state(UART_ON_PP3300);
		else if (!strcasecmp("flip18", argv[1]))
			set_uart_state(UART_FLIP_PP1800);
		else if (!strcasecmp("flip33", argv[1]))
			set_uart_state(UART_FLIP_PP3300);
		else if (!strcasecmp("auto", argv[1]))
			set_uart_state(UART_AUTO);
		else
			return EC_ERROR_PARAM1;
	}

	uart_state_str = uart_state_names[uart_state];
	if (uart_detect == UART_DETECT_AUTO)
		uart_detect_str = "auto";
	ccprintf("UART mux is: %s, setting: %s\n",
		uart_state_str, uart_detect_str);

	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(uart, command_uart,
	"[off|on18|on33|flip18|flip33|auto]",
	"Set the sbu uart state\n"
	"WARNING: 3.3v may damage 1.8v devices.\n");

static void set_led_a(int r, int g, int b)
{
	/* LEDs are active low */
	gpio_set_level(GPIO_LED_R_L, !r);
	gpio_set_level(GPIO_LED_G_L, !g);
	gpio_set_level(GPIO_LED_B_L, !b);
}

static void set_led_b(int r, int g, int b)
{
	gpio_set_level(GPIO_LED2_R_L, !r);
	gpio_set_level(GPIO_LED2_G_L, !g);
	gpio_set_level(GPIO_LED2_B_L, !b);
}

/* State we intend the mux GPIOs to be set. */
static int mux_state = MUX_OFF;
static int last_mux_state = MUX_OFF;

/* Set the state variable and GPIO configs to mux as requested. */
void set_mux_state(int state)
{
	int enabled = (state == MUX_A) || (state == MUX_B);
	/* dir: 0 -> A, dir: 1 -> B */
	int dir = (state == MUX_B);

	if (mux_state != state)
		last_mux_state = mux_state;

	/* Disconnect first. */
	gpio_set_level(GPIO_USB_C_OE_N, 1);
	gpio_set_level(GPIO_SEL_RELAY_A, 0);
	gpio_set_level(GPIO_SEL_RELAY_B, 0);

	/* Let USB disconnect. */
	msleep(100);

	/* Reconnect VBUS/CC in the requested direction. */
	gpio_set_level(GPIO_SEL_RELAY_A, !dir && enabled);
	gpio_set_level(GPIO_SEL_RELAY_B, dir && enabled);

	/* Reconnect data. */
	msleep(10);

	gpio_set_level(GPIO_USB_C_SEL_B, dir);
	gpio_set_level(GPIO_USB_C_OE_N, !enabled);

	if (!enabled)
		mux_state = MUX_OFF;
	else
		mux_state = state;

	if (state == MUX_A)
		set_led_a(0, 1, 0);
	else
		set_led_a(1, 0, 0);

	if (state == MUX_B)
		set_led_b(0, 1, 0);
	else
		set_led_b(1, 0, 0);
}


/* On button press, toggle between mux A, B, off. */
static int button_ready = 1;
void button_interrupt_deferred(void)
{
	switch (mux_state) {
	case MUX_OFF:
		if (last_mux_state == MUX_A)
			set_mux_state(MUX_B);
		else
			set_mux_state(MUX_A);
		break;

	case MUX_A:
	case MUX_B:
	default:
		set_mux_state(MUX_OFF);
		break;
	}

	button_ready = 1;
}
DECLARE_DEFERRED(button_interrupt_deferred);

/* On button press, toggle between mux A, B, off. */
void button_interrupt(enum gpio_signal signal)
{
	if (!button_ready)
		return;

	button_ready = 0;
	/*
	 * button_ready is not set until set_mux_state completes,
	 * which has ~100ms settle time for the mux, which also
	 * provides for debouncing.
	 */
	hook_call_deferred(&button_interrupt_deferred_data, 0);
}

static int command_mux(int argc, char **argv)
{
	char *mux_state_str = "off";

	if (argc > 1) {
		if (!strcasecmp("off", argv[1]))
			set_mux_state(MUX_OFF);
		else if (!strcasecmp("a", argv[1]))
			set_mux_state(MUX_A);
		else if (!strcasecmp("b", argv[1]))
			set_mux_state(MUX_B);
		else
			return EC_ERROR_PARAM1;
	}

	if (mux_state == MUX_A)
		mux_state_str = "A";
	if (mux_state == MUX_B)
		mux_state_str = "B";
	ccprintf("TYPE-C mux is %s\n", mux_state_str);

	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(mux, command_mux,
	"[off|A|B]",
	"Get/set the mux and enable state of the TYPE-C mux");

/******************************************************************************
 * Initialize board.
 */
static void board_init(void)
{
	/* USB to serial queues */
	queue_init(&usart1_to_usb);
	queue_init(&usb_to_usart1);

	/* UART init */
	usart_init(&usart1);

	/*
	 * Default to port A, to allow easier charging and
	 * detection of unconfigured devices.
	 */
	set_mux_state(MUX_A);

	/* Note that we can't enable AUTO until after init. */
	set_uart_gpios(UART_OFF);

	/* Calibrate INA0 (VBUS) with 1mA/LSB scale */
	ina2xx_init(0, 0x8000, INA2XX_CALIB_1MA(15 /*mOhm*/));
	ina2xx_init(1, 0x8000, INA2XX_CALIB_1MA(15 /*mOhm*/));
	ina2xx_init(4, 0x8000, INA2XX_CALIB_1MA(15 /*mOhm*/));

	gpio_enable_interrupt(GPIO_BUTTON_L);
}
DECLARE_HOOK(HOOK_INIT, board_init, HOOK_PRIO_DEFAULT);