summaryrefslogtreecommitdiff
path: root/src/drivers/intel/usb4/retimer/retimer.c
blob: 3769fc9193f1d090ab85c27501a48c7939e881ee (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
/* SPDX-License-Identifier: GPL-2.0-or-later */

#include <acpi/acpigen.h>
#include <acpi/acpi_device.h>
#include <acpi/acpi_pld.h>
#include <console/console.h>
#include <device/device.h>
#include <drivers/usb/acpi/chip.h>
#include <gpio.h>
#include <stdio.h>

#include "chip.h"
#include "retimer.h"

/* Unique ID for the retimer _DSM. */
#define INTEL_USB4_RETIMER_DSM_UUID	"E0053122-795B-4122-8A5E-57BE1D26ACB3"

static const char *usb4_retimer_scope;
static const char *usb4_retimer_path_arg(const char *arg)
{
	/* \\_SB.PCI0.TDMx.ARG */
	static char name[DEVICE_PATH_MAX];
	snprintf(name, sizeof(name), "%s%c%s", usb4_retimer_scope, '.', arg);
	return name;
}

/* Each polling cycle takes up to 25 ms with a total of 12 of these iterations */
#define USB4_RETIMER_ITERATION_NUM	12
#define USB4_RETIMER_POLL_CYCLE_MS	25
static void usb4_retimer_execute_ec_cmd(uint8_t port, uint8_t cmd, uint8_t expected_value,
					struct acpi_gpio *power_gpio)
{
	const char *RFWU = ec_retimer_fw_update_path();
	const uint8_t data = cmd << USB_RETIMER_FW_UPDATE_OP_SHIFT | port;

	/* Invoke EC Retimer firmware update command execution */
	ec_retimer_fw_update(data);
	/* If RFWU has return value 0xfe, return error -1 */
	acpigen_write_if_lequal_namestr_int(RFWU, USB_RETIMER_FW_UPDATE_ERROR);
	acpigen_disable_tx_gpio(power_gpio);
	acpigen_write_return_integer(-1);
	acpigen_pop_len(); /* If */

	acpigen_write_store_int_to_op(USB4_RETIMER_ITERATION_NUM, LOCAL2_OP);
	acpigen_emit_byte(WHILE_OP);
	acpigen_write_len_f();
	acpigen_emit_byte(LGREATER_OP);
	acpigen_emit_byte(LOCAL2_OP);
	acpigen_emit_byte(ZERO_OP);
	acpigen_write_if_lequal_namestr_int(RFWU, expected_value);
	acpigen_emit_byte(BREAK_OP);
	acpigen_pop_len(); /* If */

	if (cmd == USB_RETIMER_FW_UPDATE_GET_MUX) {
		acpigen_write_if_lequal_namestr_int(RFWU, USB_RETIMER_FW_UPDATE_INVALID_MUX);
		acpigen_write_sleep(USB4_RETIMER_POLL_CYCLE_MS);
		acpigen_emit_byte(DECREMENT_OP);
		acpigen_emit_byte(LOCAL2_OP);
		acpigen_emit_byte(CONTINUE_OP);
		acpigen_pop_len(); /* If */

		acpigen_emit_byte(AND_OP);
		acpigen_emit_namestring(RFWU);
		acpigen_write_integer(USB_RETIMER_FW_UPDATE_MUX_MASK);
		acpigen_emit_byte(LOCAL3_OP);
		acpigen_write_if();
		acpigen_emit_byte(LNOT_OP);
		acpigen_emit_byte(LEQUAL_OP);
		acpigen_emit_byte(LOCAL3_OP);
		acpigen_emit_byte(0);
		acpigen_disable_tx_gpio(power_gpio);
		acpigen_write_return_integer(-1);
		acpigen_pop_len(); /* If */
	} else if (cmd == USB_RETIMER_FW_UPDATE_SET_TBT) {
		/*
		 * EC return either USB_PD_MUX_USB4_ENABLED or USB_PD_MUX_TBT_COMPAT_ENABLED
		 * to RFWU after the USB_RETIMER_FW_UPDATE_SET_TBT command execution. It is
		 * needed to add additional check for USB_PD_MUX_TBT_COMPAT_ENABLED.
		 */
		acpigen_write_if_lequal_namestr_int(RFWU, USB_PD_MUX_TBT_COMPAT_ENABLED);
		acpigen_emit_byte(BREAK_OP);
		acpigen_pop_len(); /* If */
	}

	acpigen_write_sleep(USB4_RETIMER_POLL_CYCLE_MS);
	acpigen_emit_byte(DECREMENT_OP);
	acpigen_emit_byte(LOCAL2_OP);
	acpigen_pop_len(); /* While */

	/*
	 * Check whether there is timeout error
	 * Return: -1 if timeout error occurring
	 */
	acpigen_write_if_lequal_op_int(LOCAL2_OP, 0);
	acpigen_disable_tx_gpio(power_gpio);
	acpigen_write_return_integer(-1);
	acpigen_pop_len(); /* If */
}

static void enable_retimer_online_state(uint8_t port, struct acpi_gpio *power_gpio)
{
	uint8_t expected_value;

	/*
	 * Enable_retimer_online_state under NDA
	 * 1. Force power on
	 * 2. Check if there is a device connected
	 * 3. Suspend PD
	 * 4. Set Mux to USB mode
	 * 5. Set Mux to Safe mode
	 * 6. Set Mux to TBT mode
	 */

	/* Force power on for the retimer on the port */
	acpigen_enable_tx_gpio(power_gpio);

	/*
	 * Get MUX mode state
	 * Return -1 if there is a device connected on the port.
	 * Otherwise proceed Retimer firmware upgrade operation.
	 */
	expected_value = USB_PD_MUX_NONE;
	usb4_retimer_execute_ec_cmd(port, USB_RETIMER_FW_UPDATE_GET_MUX, expected_value,
					power_gpio);

	/*
	 * Suspend PD
	 * Command: USB_RETIMER_FW_UPDATE_SUSPEND_PD
	 * Expect return value: 0
	 */
	expected_value = 0;
	usb4_retimer_execute_ec_cmd(port, USB_RETIMER_FW_UPDATE_SUSPEND_PD, expected_value,
					power_gpio);

	/*
	 * Set MUX USB Mode
	 * Command: USB_RETIMER_FW_UPDATE_SUSPEND_PD
	 * Expect return value: USB_PD_MUX_USB_ENABLED
	 */
	expected_value = USB_PD_MUX_USB_ENABLED;
	usb4_retimer_execute_ec_cmd(port, USB_RETIMER_FW_UPDATE_SET_USB, expected_value,
					power_gpio);

	/*
	 * Set MUX Safe Mode
	 * Command: USB_RETIMER_FW_UPDATE_SET_SAFE
	 * Expect return value: USB_PD_MUX_SAFE_MODE
	 */
	expected_value = USB_PD_MUX_SAFE_MODE;
	usb4_retimer_execute_ec_cmd(port, USB_RETIMER_FW_UPDATE_SET_SAFE, expected_value,
					power_gpio);

	/*
	 * Set MUX TBT Mode
	 * Command: USB_RETIMER_FW_UPDATE_SET_TBT
	 * Expect return value: USB_PD_MUX_USB4_ENABLED or USB_PD_MUX_TBT_COMPAT_ENABLED
	 */
	expected_value = USB_PD_MUX_USB4_ENABLED;
	usb4_retimer_execute_ec_cmd(port, USB_RETIMER_FW_UPDATE_SET_TBT, expected_value,
					power_gpio);
}

static void disable_retimer_online_state(uint8_t port, struct acpi_gpio *power_gpio)
{
	uint8_t expected_value;

	/*
	 * Disable_retimer_online_state
	 * 1. Set Mux to disconnect mode
	 * 2. Resume PD
	 * 3. Force power off
	 */

	/*
	 * Set MUX Disconnect Mode
	 * Command: USB_RETIMER_FW_UPDATE_DISCONNECT
	 * Expect return value: 0
	 */
	expected_value = 0;
	usb4_retimer_execute_ec_cmd(port, USB_RETIMER_FW_UPDATE_DISCONNECT, expected_value,
					power_gpio);

	/*
	 * Resume PD
	 * Command: USB_RETIMER_FW_UPDATE_RESUME_PD
	 * Expect return value: 1
	 */
	expected_value = 1;
	usb4_retimer_execute_ec_cmd(port, USB_RETIMER_FW_UPDATE_RESUME_PD, expected_value,
					power_gpio);

	/* Force power off */
	acpigen_disable_tx_gpio(power_gpio);
}

/*
 * Arg0: UUID e0053122-795b-4122-8a5e-57be1d26acb3
 * Arg1: Revision ID (set to 1)
 * Arg2: Function Index
 *       0: Query command implemented
 *       1: Get power state
 *       2: Set power state
 * Arg3: A package containing parameters for the function specified
 *       by the UUID, revision ID, function index and port index.
 */
static void usb4_retimer_cb_standard_query(uint8_t port, void *arg)
{
	/*
	 * ToInteger (Arg1, Local1)
	 * If (Local1 == 1) {
	 *     Return(Buffer() {0x7})
	 * }
	 * Return (Buffer() {0x01})
	 */
	acpigen_write_to_integer(ARG1_OP, LOCAL1_OP);

	/* Revision 1 supports 2 Functions beyond the standard query */
	acpigen_write_if_lequal_op_int(LOCAL1_OP, 1);
	acpigen_write_return_singleton_buffer(0x7);
	acpigen_pop_len(); /* If */

	/* Other revisions support no additional functions */
	acpigen_write_return_singleton_buffer(0x1);
}

static void usb4_retimer_cb_get_power_state(uint8_t port, void *arg)
{
	const char *PWR;
	char pwr[DEVICE_PATH_MAX];

	snprintf(pwr, sizeof(pwr), "HR.DFP%1d.PWR", port);
	PWR = usb4_retimer_path_arg(pwr);

	/*
	 * If (PWR > 0) {
	 *      Return (1)
	 * }
	 */
	acpigen_write_if();
	acpigen_emit_byte(LGREATER_OP);
	acpigen_emit_namestring(PWR);
	acpigen_emit_byte(0);
	acpigen_write_return_integer(1);

	/*
	 * Else {
	 *      Return (0)
	 * }
	 */
	acpigen_write_else();
	acpigen_write_return_integer(0);
	acpigen_pop_len();
}

static void usb4_retimer_cb_set_power_state(uint8_t port, void *arg)
{
	struct acpi_gpio *power_gpio = arg;
	const char *PWR;
	char pwr[DEVICE_PATH_MAX];

	snprintf(pwr, sizeof(pwr), "HR.DFP%1d.PWR", port);
	PWR = usb4_retimer_path_arg(pwr);

	/*
	 * Get information to set retimer power state from Arg3[0]
	 * Local1 = DeRefOf (Arg3[0])
	 */
	acpigen_get_package_op_element(ARG3_OP, 0, LOCAL1_OP);

	/*
	 * If ((Local1 == 0) && (PWR > 0)) {
	 *      PWR--
	 *      If (PWR == 0) {
	 *		// Disable retimer online state
	 *      }
	 * }
	 */
	acpigen_write_if();
	acpigen_emit_byte(LAND_OP);
	acpigen_emit_byte(LEQUAL_OP);
	acpigen_emit_byte(LOCAL1_OP);
	acpigen_emit_byte(0);
	acpigen_emit_byte(LGREATER_OP);
	acpigen_emit_namestring(PWR);
	acpigen_emit_byte(0);
	/* PWR-- */
	acpigen_emit_byte(DECREMENT_OP);
	acpigen_emit_namestring(PWR);
	acpigen_write_if_lequal_namestr_int(PWR, 0); /* If (PWR == 0) */
	disable_retimer_online_state(port, power_gpio);
	acpigen_pop_len(); /* If (PWR == 0) */

	/*
	 * Else If ((Local1 == 1) && (PWR == 0)) {
	 *       // Enable retimer online state
	 *       PWR++
	 * }
	 */
	acpigen_write_else();
	acpigen_write_if();
	acpigen_emit_byte(LAND_OP);
	acpigen_emit_byte(LEQUAL_OP);
	acpigen_emit_byte(LOCAL1_OP);
	acpigen_emit_byte(1);
	acpigen_emit_byte(LEQUAL_OP);
	acpigen_emit_namestring(PWR);
	acpigen_emit_byte(0);
	enable_retimer_online_state(port, power_gpio);
	/* PWR++ */
	acpigen_emit_byte(INCREMENT_OP);
	acpigen_emit_namestring(PWR);

	/*
	 * Else {
	 *      Return (0)
	 * }
	 */
	acpigen_write_else();
	acpigen_write_return_integer(0);
	acpigen_pop_len(); /* Else */
	acpigen_pop_len(); /* If */

	/*
	 * If (PWR == 1) {
	 *      Return (1)
	 * }
	 */
	acpigen_write_if_lequal_namestr_int(PWR, 1);
	acpigen_write_return_integer(1);

	/*
	 * Else {
	 *      Return (0)
	 * }
	*/
	acpigen_write_else();
	acpigen_write_return_integer(0);
	acpigen_pop_len();
}

static void (*usb4_retimer_callbacks[3])(uint8_t port, void *) = {
	usb4_retimer_cb_standard_query,		/* Function 0 */
	usb4_retimer_cb_get_power_state,	/* Function 1 */
	usb4_retimer_cb_set_power_state,	/* Function 2 */
};

static void usb4_retimer_write_dsm(uint8_t port, const char *uuid,
			void (**callbacks)(uint8_t port, void *), size_t count, void *arg)
{
	struct usb4_retimer_dsm_uuid id = DSM_UUID(uuid, callbacks, count, arg);
	size_t i;

	acpigen_write_to_integer(ARG2_OP, LOCAL0_OP);

	for (i = 0; i < id.count; i++) {
		/* If (LEqual (Local0, i)) */
		acpigen_write_if_lequal_op_int(LOCAL0_OP, i);

		/* Callback to write if handler. */
		if (id.callbacks[i])
			id.callbacks[i](port, id.arg);

		acpigen_pop_len(); /* If */
	}
}

static void usb4_retimer_fill_ssdt(const struct device *dev)
{
	struct drivers_intel_usb4_retimer_config *config = dev->chip_info;
	const struct device *usb_device;
	static char dfp[DEVICE_PATH_MAX];
	struct acpi_pld pld;
	uint8_t dfp_port, usb_port;
	int ec_port = 0;

	usb4_retimer_scope = acpi_device_scope(dev);
	if (!usb4_retimer_scope || !config)
		return;

	/* Scope */
	acpigen_write_scope(usb4_retimer_scope);

	/* Host router */
	acpigen_write_device("HR");
	acpigen_write_ADR(0);
	acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_ON);

	for (dfp_port = 0; dfp_port < DFP_NUM_MAX; dfp_port++) {
		if (!config->dfp[dfp_port].power_gpio.pin_count) {
			printk(BIOS_WARNING, "%s: No DFP%1d power GPIO for %s\n",
				__func__, dfp_port, dev_path(dev));
			continue;
		}

		usb_device = config->dfp[dfp_port].typec_port;
		usb_port = usb_device->path.usb.port_id;

		ec_port = retimer_get_index_for_typec(usb_port);
		if (ec_port == -1) {
			printk(BIOS_ERR, "%s: No relative EC port found for TC port %d\n",
				__func__, usb_port);
			continue;
		}
		/* DFPx */
		snprintf(dfp, sizeof(dfp), "DFP%1d", ec_port);
		acpigen_write_device(dfp);
		/* _ADR part is for the lane adapter */
		acpigen_write_ADR(dfp_port*2 + 1);

		/* Fill _PLD with the same USB 3.x object on the Type-C connector */
		if (CONFIG(DRIVERS_USB_ACPI)) {
			if (usb_acpi_get_pld(usb_device, &pld))
				acpigen_write_pld(&pld);
			else
				printk(BIOS_ERR, "Error retrieving PLD for USB Type-C %d\n",
					usb_port);
		}

		/* Power online reference counter(_PWR) */
		acpigen_write_name("PWR");
		acpigen_write_zero();

		/* Method (_DSM, 4, Serialized) */
		acpigen_write_method_serialized("_DSM", 0x4);
		/* ToBuffer (Arg0, Local0) */
		acpigen_write_to_buffer(ARG0_OP, LOCAL0_OP);
		acpigen_write_if();  /* If (UUID != INTEL_USB4_RETIMER_DSM_UUID) */
		acpigen_emit_byte(LNOT_OP);
		acpigen_emit_byte(LEQUAL_OP);
		acpigen_emit_byte(LOCAL0_OP);
		acpigen_write_uuid(INTEL_USB4_RETIMER_DSM_UUID);
		/* Return (Buffer (One) { 0x0 }) */
		acpigen_write_return_singleton_buffer(0x0);
		acpigen_pop_len();
		usb4_retimer_write_dsm(ec_port, INTEL_USB4_RETIMER_DSM_UUID,
			usb4_retimer_callbacks, ARRAY_SIZE(usb4_retimer_callbacks),
			(void *)&config->dfp[dfp_port].power_gpio);
		/* Default case: Return (Buffer (One) { 0x0 }) */
		acpigen_write_return_singleton_buffer(0x0);

		acpigen_pop_len(); /* Method _DSM */
		acpigen_pop_len(); /* DFP */
	}
	acpigen_pop_len(); /* Host Router */
	acpigen_pop_len(); /* Scope */

	printk(BIOS_INFO, "%s.HR: %s at %s\n", usb4_retimer_scope, dev->chip_ops->name,
	       dev_path(dev));
}

static struct device_operations usb4_retimer_dev_ops = {
	.read_resources	= noop_read_resources,
	.set_resources	= noop_set_resources,
	.acpi_fill_ssdt	= usb4_retimer_fill_ssdt,
};

static void usb4_retimer_enable(struct device *dev)
{
	dev->ops = &usb4_retimer_dev_ops;
}

struct chip_operations drivers_intel_usb4_retimer_ops = {
	.name = "Intel USB4 Retimer",
	.enable_dev = usb4_retimer_enable
};

__weak const char *ec_retimer_fw_update_path(void)
{
	return NULL;
}

__weak void ec_retimer_fw_update(uint8_t data)
{
}

/*
 * This function will convert CPU physical port mapping to abstract
 * EC port mapping.
 * For example, board might have enabled TCSS port 1 and 3 as per physical
 * port mapping. Since only 2 TCSS ports are enabled EC will index it as port 0
 * and port 1. So there will be an issue when coreboot sends command to EC for
 * port 3 (with coreboot index of 2). EC will produce an error due to wrong index.
 *
 * Note: Each SoC code using retimer driver needs to implement this function
 * since SoC will have physical port details.
 */
__weak int retimer_get_index_for_typec(uint8_t typec_port)
{
	/* By default assume that retimer port index = Type C port */
	return (int)typec_port;
}