summaryrefslogtreecommitdiff
path: root/board/cr50/rdd.c
blob: 0bb7055ca2ea93585aeef361bb684b6ca5af6334 (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
/* 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"
#include "console.h"
#include "device_state.h"
#include "gpio.h"
#include "hooks.h"
#include "rbox.h"
#include "rdd.h"
#include "registers.h"
#include "system.h"
#include "uartn.h"
#include "usb_api.h"

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

static int ec_uart_enabled, enable_usb_wakeup;
static int usb_is_initialized;

struct uart_config {
	const char *name;
	enum device_type device;
	int tx_signal;
};

static struct uart_config uarts[] = {
	[UART_AP] = {"AP", DEVICE_AP, GC_PINMUX_UART1_TX_SEL},
	[UART_EC] = {"EC", DEVICE_EC, GC_PINMUX_UART2_TX_SEL},
};

static int ccd_is_enabled(void)
{
	return !gpio_get_level(GPIO_CCD_MODE_L);
}

int is_utmi_wakeup_allowed(void)
{
	return enable_usb_wakeup;
}


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

/* Connect the UART pin to the given signal */
static void uart_select_tx(int uart, int signal)
{
	if (uart == UART_AP)
		GWRITE(PINMUX, DIOA7_SEL, signal);
	else
		GWRITE(PINMUX, DIOB5_SEL, signal);
}

static int servo_is_connected(void)
{
	return device_get_state(DEVICE_SERVO) == DEVICE_STATE_ON;
}

void uartn_tx_connect(int uart)
{
	if (uart == UART_EC && !ec_uart_enabled)
		return;

	if (!ccd_is_enabled())
		return;

	if (servo_is_connected()) {
		CPRINTS("Servo is attached cannot enable %s UART",
			uarts[uart].name);
		return;
	}

	if (device_get_state(uarts[uart].device) == DEVICE_STATE_ON)
		uart_select_tx(uart, uarts[uart].tx_signal);
	else if (!uartn_enabled(uart))
		CPRINTS("%s is powered off", uarts[uart].name);
}

void uartn_tx_disconnect(int uart)
{
	/* If servo is connected disable UART */
	if (servo_is_connected())
		ec_uart_enabled = 0;

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

void rdd_attached(void)
{
	/* Indicate case-closed debug mode (active low) */
	gpio_set_level(GPIO_CCD_MODE_L, 0);

	/* Enable CCD */
	ccd_set_mode(CCD_MODE_ENABLED);

	enable_usb_wakeup = 1;

	uartn_tx_connect(UART_AP);
}

void rdd_detached(void)
{
	/* Disconnect from AP and EC UART TX peripheral from gpios */
	uartn_tx_disconnect(UART_EC);
	uartn_tx_disconnect(UART_AP);

	/* Disable the AP and EC UART peripheral */
	uartn_disable(UART_AP);
	uartn_disable(UART_EC);

	/* Done with case-closed debug mode */
	gpio_set_level(GPIO_CCD_MODE_L, 1);

	enable_usb_wakeup = 0;
	ec_uart_enabled = 0;

	/* Disable CCD */
	ccd_set_mode(CCD_MODE_DISABLED);
}

void ccd_phy_init(int enable_ccd)
{
	uint32_t properties = system_get_board_properties();
	/*
	 * For boards that have one phy connected to the AP and one to the
	 * external port PHY0 is for the AP and PHY1 is for CCD.
	 */
	uint32_t which_phy = enable_ccd ? USB_SEL_PHY1 : USB_SEL_PHY0;

	/*
	 * TODO: if both PHYs are connected to the external port select the
	 * PHY based on the detected polarity
	 */
	usb_select_phy(which_phy);

	/*
	 * If the usb is going to be initialized on the AP PHY, but the AP is
	 * off, wait until HOOK_CHIPSET_RESUME to initialize usb.
	 */
	if (!enable_ccd && device_get_state(DEVICE_AP) != DEVICE_STATE_ON) {
		usb_is_initialized = 0;
		return;
	}

	/*
	 * If the board has the non-ccd phy connected to the AP initialize the
	 * phy no matter what. Otherwise only initialized the phy if ccd is
	 * enabled.
	 */
	if ((properties & BOARD_USB_AP) || enable_ccd) {
		usb_init();
		usb_is_initialized = 1;
	}
}

void disable_ap_usb(void)
{
	if ((system_get_board_properties() & BOARD_USB_AP) &&
	    !ccd_is_enabled() && usb_is_initialized) {
		usb_release();
		usb_is_initialized = 0;
	}
}
DECLARE_HOOK(HOOK_CHIPSET_SHUTDOWN, disable_ap_usb, HOOK_PRIO_DEFAULT);

void enable_ap_usb(void)
{
	if ((system_get_board_properties() & BOARD_USB_AP) &&
	    !ccd_is_enabled() && !usb_is_initialized) {
		usb_is_initialized = 1;
		usb_init();
	}
}
DECLARE_HOOK(HOOK_CHIPSET_RESUME, enable_ap_usb, HOOK_PRIO_DEFAULT);

static int command_ccd(int argc, char **argv)
{
	int val;

	if (argc > 1) {
		if (!strcasecmp("uart", argv[1]) && argc > 2) {
			if (!parse_bool(argv[2], &val))
				return EC_ERROR_PARAM2;

			if (val) {
				ec_uart_enabled = 1;
				uartn_tx_connect(UART_EC);
			} else {
				ec_uart_enabled = 0;
				uartn_tx_disconnect(UART_EC);
			}
		} else if (argc == 2) {
			if (!parse_bool(argv[1], &val))
				return EC_ERROR_PARAM1;

			if (val)
				rdd_attached();
			else
				rdd_detached();
		} else
			return EC_ERROR_PARAM1;
	}

	ccprintf("CCD:     %s\nAP UART: %s\nEC UART: %s\n",
		ccd_is_enabled() ? " enabled" : "disabled",
		uartn_enabled(UART_AP) ? " enabled" : "disabled",
		uartn_enabled(UART_EC) ? " enabled" : "disabled");
	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(ccd, command_ccd,
			"[uart] [<BOOLEAN>]",
			"Get/set the case closed debug state");

static int command_sys_rst(int argc, char **argv)
{
	int val;

	if (argc > 1) {
		if (parse_bool(argv[1], &val)) {
			if (val)
				assert_sys_rst();
			else
				deassert_sys_rst();
		} else
			return EC_ERROR_PARAM1;
		ccprintf("SYS_RST_L is %s\n", is_sys_rst_asserted() ?
			"asserted" : "deasserted");
	} else {
		ccprintf("Issuing AP reset\n");
		assert_sys_rst();
		usleep(200);
		deassert_sys_rst();
	}

	return EC_SUCCESS;

}
DECLARE_SAFE_CONSOLE_COMMAND(sysrst, command_sys_rst,
	"[<BOOLEAN>]",
	"Assert/deassert SYS_RST_L to reset the AP");

static int command_ec_rst(int argc, char **argv)
{
	int val;


	if (argc > 1) {
		if (parse_bool(argv[1], &val)) {
			if (val)
				assert_ec_rst();
			else
				deassert_ec_rst();
		} else
			return EC_ERROR_PARAM1;

		ccprintf("EC_RST_L is %s\n", is_ec_rst_asserted() ?
			"asserted" : "deasserted");
	} else {
		ccprintf("Issuing EC reset\n");
		assert_ec_rst();
		usleep(200);
		deassert_ec_rst();
	}
	return EC_SUCCESS;
}
DECLARE_SAFE_CONSOLE_COMMAND(ecrst, command_ec_rst,
	"[<BOOLEAN>]",
	"Assert/deassert EC_RST_L");

static int command_powerbtn(int argc, char **argv)
{
	char *e;
	int ms = 200;

	if (argc == 2) {
		ms = strtoi(argv[1], &e, 0);
		if (*e)
			return EC_ERROR_PARAM1;
	}
	ccprintf("Simulating %dms power button press\n", ms);
	rbox_press_power_btn(ms);

	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(powerbtn, command_powerbtn,
			"ms",
			"Simulate a power button press");