summaryrefslogtreecommitdiff
path: root/zephyr/projects/intelrvp/mtlrvp/src/mtlrvp.c
blob: 9d96a087126a9c4a4e23111beba363d087e7272d (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
/* Copyright 2022 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "battery.h"
#include "battery_fuel_gauge.h"
#include "charger.h"
#include "common.h"
#include "console.h"
#include "driver/retimer/bb_retimer_public.h"
#include "driver/tcpm/ccgxxf.h"
#include "driver/tcpm/nct38xx.h"
#include "driver/tcpm/tcpci.h"
#include "extpower.h"
#include "gpio.h"
#include "gpio/gpio_int.h"
#include "hooks.h"
#include "i2c.h"
#include "intelrvp.h"
#include "intel_rvp_board_id.h"
#include "ioexpander.h"
#include "isl9241.h"
#include "keyboard_raw.h"
#include "power/meteorlake.h"
#include "sn5s330.h"
#include "system.h"
#include "task.h"
#include "tusb1064.h"
#include "usb_mux.h"
#include "usbc_ppc.h"
#include "util.h"

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

/*******************************************************************/
/* USB-C Configuration Start */

/* PPC */
#define I2C_ADDR_SN5S330_P0 0x40
#define I2C_ADDR_SN5S330_P1 0x41

/* IOEX ports */
enum ioex_port {
	IOEX_KBD = 0,
#if defined(HAS_TASK_PD_C2)
	IOEX_C2_CCGXXF,
#endif
	IOEX_COUNT
};

/* USB-C ports */
enum usbc_port {
	USBC_PORT_C0 = 0,
	USBC_PORT_C1,
#if defined(HAS_TASK_PD_C2)
	USBC_PORT_C2,
	USBC_PORT_C3,
#endif
	USBC_PORT_COUNT
};
BUILD_ASSERT(USBC_PORT_COUNT == CONFIG_USB_PD_PORT_MAX_COUNT);

/* USB-C PPC configuration */
struct ppc_config_t ppc_chips[] = {
	[USBC_PORT_C0] = {
		.i2c_port = I2C_PORT_TYPEC_AIC_1,
		.i2c_addr_flags = I2C_ADDR_SN5S330_P0,
		.drv = &sn5s330_drv,
	},
	[USBC_PORT_C1] = {
		.i2c_port = I2C_PORT_TYPEC_AIC_1,
		.i2c_addr_flags = I2C_ADDR_SN5S330_P1,
		.drv = &sn5s330_drv,
	},
};
unsigned int ppc_cnt = ARRAY_SIZE(ppc_chips);

/* TCPC AIC GPIO Configuration */
const struct tcpc_aic_gpio_config_t tcpc_aic_gpios[] = {
	[USBC_PORT_C0] = {
		.tcpc_alert = GPIO_SIGNAL(DT_NODELABEL(usbc_tcpc_alrt_p0)),
		.ppc_alert = GPIO_SIGNAL(DT_NODELABEL(usbc_tcpc_ppc_alrt_p0)),
		.ppc_intr_handler = sn5s330_interrupt,
	},
	[USBC_PORT_C1] = {
		.tcpc_alert = GPIO_SIGNAL(DT_NODELABEL(usbc_tcpc_alrt_p0)),
		.ppc_alert = GPIO_SIGNAL(DT_NODELABEL(usbc_tcpc_ppc_alrt_p1)),
		.ppc_intr_handler = sn5s330_interrupt,
	},
#if defined(HAS_TASK_PD_C2)
	[USBC_PORT_C2] = {
		.tcpc_alert = GPIO_SIGNAL(DT_NODELABEL(usbc_tcpc_alrt_p2)),
		/* No PPC alert for CCGXXF */
	},
	[USBC_PORT_C3] = {
		.tcpc_alert = GPIO_SIGNAL(DT_NODELABEL(usbc_tcpc_alrt_p3)),
		/* No PPC alert for CCGXXF */
	},
#endif
};
BUILD_ASSERT(ARRAY_SIZE(tcpc_aic_gpios) == CONFIG_USB_PD_PORT_MAX_COUNT);

static void board_connect_c0_sbu_deferred(void)
{
	enum pd_power_role prole;

	if (gpio_get_level(GPIO_CCD_MODE_ODL)) {
		CPRINTS("Default AUX line connected");
		/* Default set the SBU lines to AUX mode */
		ioex_set_level(IOEX_USB_C0_MUX_SBU_SEL_1, 0);
		ioex_set_level(IOEX_USB_C0_MUX_SBU_SEL_0, 1);
	} else {
		prole = pd_get_power_role(USBC_PORT_C0);
		CPRINTS("%s debug device is attached",
			prole == PD_ROLE_SINK ? "Servo V4C/SuzyQ" : "Intel");

		if (prole == PD_ROLE_SINK) {
			/* Set the SBU lines to Google CCD mode */
			ioex_set_level(IOEX_USB_C0_MUX_SBU_SEL_1, 1);
			ioex_set_level(IOEX_USB_C0_MUX_SBU_SEL_0, 1);
		} else {
			/* Set the SBU lines to Intel CCD mode */
			ioex_set_level(IOEX_USB_C0_MUX_SBU_SEL_1, 0);
			ioex_set_level(IOEX_USB_C0_MUX_SBU_SEL_0, 0);
		}
	}
}
DECLARE_DEFERRED(board_connect_c0_sbu_deferred);

void board_overcurrent_event(int port, int is_overcurrented)
{
	/*
	 * TODO: Meteorlake PCH does not use Physical GPIO for over current
	 * error, hence Send 'Over Current Virtual Wire' eSPI signal.
	 */
}

void board_reset_pd_mcu(void)
{
	/* Reset NCT38XX TCPC */
	gpio_pin_set_dt(GPIO_DT_FROM_NODELABEL(usb_c0_c1_tcpc_rst_odl), 0);
	msleep(NCT38XX_RESET_HOLD_DELAY_MS);
	gpio_pin_set_dt(GPIO_DT_FROM_NODELABEL(usb_c0_c1_tcpc_rst_odl), 1);
	nct38xx_reset_notify(0);
	nct38xx_reset_notify(1);

	if (NCT3807_RESET_POST_DELAY_MS != 0) {
		msleep(NCT3807_RESET_POST_DELAY_MS);
	}

	/* NCT38XX chip uses gpio ioex */
	gpio_reset_port(DEVICE_DT_GET(DT_NODELABEL(ioex_c0)));
	gpio_reset_port(DEVICE_DT_GET(DT_NODELABEL(ioex_c1)));

#if defined(HAS_TASK_PD_C2)
	/* Reset the ccgxxf ports only resetting 1 is required */
	ccgxxf_reset(USBC_PORT_C2);

	/* CCGXXF has ioex on port 2 */
	ioex_init(IOEX_C2_CCGXXF);
#endif
}

void board_connect_c0_sbu(enum gpio_signal signal)
{
	hook_call_deferred(&board_connect_c0_sbu_deferred_data, 0);
}

/******************************************************************************/
/* KSO mapping for discrete keyboard */
__override const uint8_t it8801_kso_mapping[] = {
	0, 1, 20, 3, 4, 5, 6, 11, 12, 13, 14, 15, 16,
};
BUILD_ASSERT(ARRAY_SIZE(it8801_kso_mapping) == KEYBOARD_COLS_MAX);

/* PWROK signal configuration */
/*
 * On MTLRVP, SYS_PWROK_EC is an output controlled by EC and uses ALL_SYS_PWRGD
 * as input.
 */
const struct intel_x86_pwrok_signal pwrok_signal_assert_list[] = {
	{
		.gpio = GPIO_PCH_SYS_PWROK,
		.delay_ms = 3,
	},
};
const int pwrok_signal_assert_count = ARRAY_SIZE(pwrok_signal_assert_list);

const struct intel_x86_pwrok_signal pwrok_signal_deassert_list[] = {
	{
		.gpio = GPIO_PCH_SYS_PWROK,
	},
};
const int pwrok_signal_deassert_count = ARRAY_SIZE(pwrok_signal_deassert_list);

/*
 * Returns board information (board id[7:0] and Fab id[15:8]) on success
 * -1 on error.
 */
__override int board_get_version(void)
{
	/* Cache the MTLRVP board ID */
	static int mtlrvp_board_id;

	int i;
	int rv = EC_ERROR_UNKNOWN;
	int fab_id, board_id, bom_id;

	/* Board ID is already read */
	if (mtlrvp_board_id)
		return mtlrvp_board_id;

	/*
	 * IOExpander that has Board ID information is on DSW-VAL rail on
	 * ADL RVP. On cold boot cycles, DSW-VAL rail is taking time to settle.
	 * This loop retries to ensure rail is settled and read is successful
	 */
	for (i = 0; i < RVP_VERSION_READ_RETRY_CNT; i++) {
		rv = gpio_pin_get_dt(&bom_id_config[0]);

		if (rv >= 0)
			break;

		k_msleep(1);
	}

	/* return -1 if failed to read board id */
	if (rv)
		return -1;

	/*
	 * BOM ID [2]   : IOEX[0]
	 * BOM ID [1:0] : IOEX[15:14]
	 */
	bom_id = gpio_pin_get_dt(&bom_id_config[0]) << 2;
	bom_id |= gpio_pin_get_dt(&bom_id_config[1]) << 1;
	bom_id |= gpio_pin_get_dt(&bom_id_config[2]);
	/*
	 * FAB ID [1:0] : IOEX[2:1] + 1
	 */
	fab_id = gpio_pin_get_dt(&fab_id_config[0]) << 1;
	fab_id |= gpio_pin_get_dt(&fab_id_config[1]);
	fab_id += 1;

	/*
	 * BOARD ID[5:0] : IOEX[13:8]
	 */
	board_id = gpio_pin_get_dt(&board_id_config[0]) << 5;
	board_id |= gpio_pin_get_dt(&board_id_config[1]) << 4;
	board_id |= gpio_pin_get_dt(&board_id_config[2]) << 3;
	board_id |= gpio_pin_get_dt(&board_id_config[3]) << 2;
	board_id |= gpio_pin_get_dt(&board_id_config[4]) << 1;
	board_id |= gpio_pin_get_dt(&board_id_config[5]);

	CPRINTF("BID:0x%x, FID:0x%x, BOM:0x%x", board_id, fab_id, bom_id);

	mtlrvp_board_id = board_id | (fab_id << 8);
	return mtlrvp_board_id;
}

static void board_int_init(void)
{
	/* Enable PPC interrupts. */
	gpio_enable_dt_interrupt(GPIO_INT_FROM_NODELABEL(int_usb_c0_ppc));
	gpio_enable_dt_interrupt(GPIO_INT_FROM_NODELABEL(int_usb_c1_ppc));

	/* Enable TCPC interrupts. */
	gpio_enable_dt_interrupt(GPIO_INT_FROM_NODELABEL(int_usb_c0_c1_tcpc));
#if defined(HAS_TASK_PD_C2)
	gpio_enable_dt_interrupt(GPIO_INT_FROM_NODELABEL(int_usb_c2_tcpc));
	gpio_enable_dt_interrupt(GPIO_INT_FROM_NODELABEL(int_usb_c3_tcpc));
#endif

	/* Enable CCD Mode interrupt */
	gpio_enable_dt_interrupt(GPIO_INT_FROM_NODELABEL(int_ccd_mode));
}

static int board_pre_task_peripheral_init(const struct device *unused)
{
	ARG_UNUSED(unused);

	/* Only reset tcpc/pd if not sysjump */
	if (!system_jumped_late()) {
		/* Initialize tcpc and all ioex */
		board_reset_pd_mcu();
	}

	/* Initialize all interrupts */
	board_int_init();

	/* Make sure SBU are routed to CCD or AUX based on CCD status at init */
	board_connect_c0_sbu_deferred();

	return 0;
}
SYS_INIT(board_pre_task_peripheral_init, APPLICATION,
	 CONFIG_APPLICATION_INIT_PRIORITY);

/*
 * Since MTLRVP has both PPC and TCPC ports override to check if the port
 * is a PPC or non PPC port
 */
__override bool pd_check_vbus_level(int port, enum vbus_level level)
{
	if (!board_port_has_ppc(port)) {
		return tcpm_check_vbus_level(port, level);
	} else if (level == VBUS_PRESENT) {
		return pd_snk_is_vbus_provided(port);
	} else {
		return !pd_snk_is_vbus_provided(port);
	}
}

__override bool board_port_has_ppc(int port)
{
	bool ppc_port;

	switch (port) {
	case USBC_PORT_C0:
	case USBC_PORT_C1:
		ppc_port = true;
		break;
	default:
		ppc_port = false;
		break;
	}

	return ppc_port;
}