summaryrefslogtreecommitdiff
path: root/board/fruitpie/board.c
blob: 7d88bbcc3680f4a4fa64d54335aad955822e4c37 (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
/* Copyright (c) 2014 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.
 */
/* Fruitpie board configuration */

#include "adc.h"
#include "adc_chip.h"
#include "common.h"
#include "console.h"
#include "gpio.h"
#include "hooks.h"
#include "i2c.h"
#include "registers.h"
#include "task.h"
#include "timer.h"
#include "usb_pd.h"
#include "util.h"

#include "driver/tsu6721.h"

void rohm_event(enum gpio_signal signal)
{
	ccprintf("ROHM!\n");
}

void vbus_event(enum gpio_signal signal)
{
	ccprintf("VBUS!\n");
}

void tsu_event(enum gpio_signal signal)
{
	ccprintf("TSU!\n");
}

#include "gpio_list.h"

/* Initialize board. */
static void board_init(void)
{
	/* enable SYSCFG clock */
	STM32_RCC_APB2ENR |= 1 << 0;

	/* Remap SPI2 to DMA channels 6 and 7 */
	STM32_SYSCFG_CFGR1 |= (1 << 24);
}
DECLARE_HOOK(HOOK_INIT, board_init, HOOK_PRIO_DEFAULT);

/* Pins with alternate functions */
const struct gpio_alt_func gpio_alt_funcs[] = {
	{GPIO_B, 0x2000, 0, MODULE_USB_PD},/* SPI2: SCK(PB13) */
	{GPIO_B, 0x0200, 2, MODULE_USB_PD},/* TIM17_CH1: PB9) */
	{GPIO_A, 0xC000, 1, MODULE_UART},  /* USART2: PA14/PA15 */
	{GPIO_B, 0x0cc0, 1, MODULE_I2C},   /* I2C SLAVE:PB6/7 MASTER:PB10/11 */
};
const int gpio_alt_funcs_count = ARRAY_SIZE(gpio_alt_funcs);

/* ADC channels */
const struct adc_t adc_channels[] = {
	/* USB PD CC lines sensing. Converted to mV (3300mV/4096). */
	[ADC_CH_CC1_PD] = {"CC1_PD", 3300, 4096, 0, STM32_AIN(0)},
	[ADC_CH_CC2_PD] = {"CC2_PD", 3300, 4096, 0, STM32_AIN(4)},
};
BUILD_ASSERT(ARRAY_SIZE(adc_channels) == ADC_CH_COUNT);

/* 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 board_set_debug(int enable)
{
	timestamp_t timeout;
	int rv = EC_SUCCESS;

	if (enable) {
		/* Disable the PD module */
		gpio_config_module(MODULE_USB_PD, 0);

		/* Suspend the USB PD task */
		pd_set_suspend(1);

		/* Decrease BCDv1.2 timer to 0.6s */
		tsu6721_write(TSU6721_REG_TIMER, 0x05);

		timeout.val = get_time().val + DEBUG_SWITCH_TIMEOUT_MSEC;
		/* Wait for power to be detected to allow switching debug mux */
		while (!(tsu6721_read(TSU6721_REG_DEV_TYPE3) & 0x74)) {
			if (get_time().val > timeout.val)
				return EC_ERROR_TIMEOUT;

			/* Not already powered by cable, turn on regulator */
			gpio_set_level(GPIO_USB_C_5V_EN, 1);

			ccputs("Sleeping for 1s, waiting for TSU6721...\n");
			usleep(1000*MSEC);
		}

		/* Enable manual switching */
		rv = tsu6721_mux(TSU6721_MUX_USB);
		if (rv)
			return rv;

		/* Switch debug mux */
		tsu6721_set_pins(TSU6721_PIN_MANUAL2_BOOT);

		/* Set pins PD_CLK_IN, PD_TX_DATA, and
		 * VCONN1_EN to alternate function. */
		/* Set pin PD_TX_EN (NSS) to general purpose output mode. */
		STM32_GPIO_MODER(GPIO_B) &= ~0xff000000;
		STM32_GPIO_MODER(GPIO_B) |= 0xa9000000;

		/* Set all four pins to alternate function 0 */
		STM32_GPIO_AFRH(GPIO_B) &= ~(0xffff0000);

		/* Set all four pins to output push-pull */
		STM32_GPIO_OTYPER(GPIO_B) &= ~(0xf000);

		/* Set pullup on PD_TX_EN */
		STM32_GPIO_PUPDR(GPIO_B) |= 0x1000000;

		/* Set all four pins to high speed */
		STM32_GPIO_OSPEEDR(GPIO_B) |= 0xff000000;

		/* Reset SPI2 */
		STM32_RCC_APB1RSTR |= (1 << 14);
		STM32_RCC_APB1RSTR &= ~(1 << 14);

		/* Enable clocks to SPI2 module */
		STM32_RCC_APB1ENR |= STM32_RCC_PB1_SPI2;
	} else {
		/* Reset SPI2 */
		STM32_RCC_APB1RSTR |= (1 << 14);
		STM32_RCC_APB1RSTR &= ~(1 << 14);

		/* Set all but VCONN1_EN to input mode */
		STM32_GPIO_MODER(GPIO_B) &= ~0x3f000000;

		/* Unset pullup on PD_TX_EN/SPI_NSS */
		gpio_set_flags(GPIO_PD_TX_EN, GPIO_OUT_LOW);

		/* Turn off debug mux */
		tsu6721_set_pins(0);

		/* Disable manual switching */
		rv = tsu6721_mux(TSU6721_MUX_AUTO);
		if (rv)
			return rv;

		/* Disable power on USB_C_5V_EN pin */
		gpio_set_level(GPIO_USB_C_5V_EN, 0);

		/* Restore BCDv1.2 timer to 1.6s */
		tsu6721_write(TSU6721_REG_TIMER, 0x15);

		/* Restore the USB PD task */
		pd_set_suspend(0);
	}

	return rv;
}

void board_set_usb_mux(enum typec_mux mux)
{
	/* reset everything */
	gpio_set_level(GPIO_SS1_EN_L, 1);
	gpio_set_level(GPIO_SS2_EN_L, 1);
	gpio_set_level(GPIO_DP_MODE, 0);
	gpio_set_level(GPIO_SS1_USB_MODE_L, 1);
	gpio_set_level(GPIO_SS2_USB_MODE_L, 1);
	switch (mux) {
	case TYPEC_MUX_NONE:
		/* everything is already disabled, we can return */
		return;
	case TYPEC_MUX_USB1:
		gpio_set_level(GPIO_SS1_USB_MODE_L, 0);
		break;
	case TYPEC_MUX_USB2:
		gpio_set_level(GPIO_SS2_USB_MODE_L, 0);
		break;
	case TYPEC_MUX_DP1:
		gpio_set_level(GPIO_DP_POLARITY_L, 1);
		gpio_set_level(GPIO_DP_MODE, 1);
		break;
	case TYPEC_MUX_DP2:
		gpio_set_level(GPIO_DP_POLARITY_L, 0);
		gpio_set_level(GPIO_DP_MODE, 1);
		break;
	}
	gpio_set_level(GPIO_SS1_EN_L, 0);
	gpio_set_level(GPIO_SS2_EN_L, 0);
}

static int command_typec(int argc, char **argv)
{
	const char * const mux_name[] = {"none", "usb1", "usb2", "dp1", "dp2"};

	if (argc < 2) {
		/* dump current state */
		ccprintf("CC1 %d mV  CC2 %d mV\n",
			adc_read_channel(ADC_CH_CC1_PD),
			adc_read_channel(ADC_CH_CC2_PD));
		ccprintf("DP %d Polarity %d\n", gpio_get_level(GPIO_DP_MODE),
			!!gpio_get_level(GPIO_DP_POLARITY_L) + 1);
		ccprintf("Superspeed %s\n",
			gpio_get_level(GPIO_SS1_EN_L) ? "None" :
			(gpio_get_level(GPIO_DP_MODE) ? "DP" :
			(!gpio_get_level(GPIO_SS1_USB_MODE_L) ? "USB1" : "USB2")
			));
		return EC_SUCCESS;
	}

	if (!strcasecmp(argv[1], "mux")) {
		enum typec_mux mux = TYPEC_MUX_NONE;
		int i;

		if (argc < 3)
			return EC_ERROR_PARAM2;

		for (i = 0; i < ARRAY_SIZE(mux_name); i++)
			if (!strcasecmp(argv[2], mux_name[i]))
				mux = i;
		board_set_usb_mux(mux);
		return EC_SUCCESS;
	} else {
		return EC_ERROR_PARAM1;
	}

	return EC_ERROR_UNKNOWN;
}
DECLARE_CONSOLE_COMMAND(typec, command_typec,
			"[mux none|usb1|usb2|dp1|d2]",
			"Control type-C connector",
			NULL);