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

#include <acpi/acpigen.h>
#include <acpi/acpi_device.h>
#include <console/console.h>
#include <device/device.h>
#include <device/path.h>
#include <gpio.h>

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

/* Unique ID for the retimer _DSM. */
#define INTEL_USB4_RETIMER_DSM_UUID	"61788900-C470-42BB-80F0-23A313864593"

/*
 * Arg0: UUID
 * Arg1: Revision ID (set to 1)
 * Arg2: Function Index
 *       0: Query command implemented
 *       1: Query force power enable state
 *       2: Set force power state
 *       3: Get Retimer FW Update EC Ram value
 *       4: Set Retimer FW Update EC Ram value
 * Arg3: A package containing parameters for the function specified
 *       by the UUID, revision ID and function index.
 */

static void usb4_retimer_cb_standard_query(void *arg)
{
	/*
	 * ToInteger (Arg1, Local2)
	 * If (Local2 == 1) {
	 *     Return(Buffer() {0x1f})
	 * }
	 * Return (Buffer() {0x01})
	 */
	acpigen_write_to_integer(ARG1_OP, LOCAL2_OP);

	/* Revision 1 supports 4 Functions beyond the standard query */
	acpigen_write_if_lequal_op_int(LOCAL2_OP, 1);
	acpigen_write_return_singleton_buffer(0x1f);
	acpigen_pop_len(); /* If */

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

static void usb4_retimer_cb_get_power_state(void *arg)
{
	struct acpi_gpio *power_gpio = arg;

	/*
	 * Read power gpio into Local0
	 * Store (\_SB.PCI0.GTXS (power_gpio), Local0)
	 * Return (Local0)
	 */
	acpigen_get_tx_gpio(power_gpio);
	acpigen_write_return_op(LOCAL0_OP);
}

static void usb4_retimer_cb_set_power_state(void *arg)
{
	struct acpi_gpio *power_gpio = arg;

	/*
	 * Get information to set to retimer info from Arg3[0]
	 * Local0 = DeRefOf (Arg3[0])
	 */
	acpigen_get_package_op_element(ARG3_OP, 0, LOCAL0_OP);

	/*
	 * If (Local0 == 0) {
	 *     // Turn power off
	 *     \_SB.PCI0.CTXS (power_gpio)
	 * }
	 */
	acpigen_write_if_lequal_op_int(LOCAL0_OP, 0);
	acpigen_disable_tx_gpio(power_gpio);
	acpigen_pop_len(); /* If */

	/*
	 * Else {
	 *     // Turn power on
	 *     \_SB.PCI0.STXS (power_gpio)
	 * }
	 */
	acpigen_write_else();
	acpigen_enable_tx_gpio(power_gpio);
	acpigen_pop_len();

	/* Return (Zero) */
	acpigen_write_return_integer(0);
}

static void usb4_retimer_cb_get_retimer_info(void *arg)
{
	const char *RFWU = ec_retimer_fw_update_path();

	/*
	 * Read Mux Retimer info from EC RAM
	 * Return RFWU if RFWU is not NULL. Otherwise return -1 to
	 * inform kernel about error.
	 */
	if (!RFWU)
		acpigen_write_return_byte(-1);
	else
		acpigen_write_return_namestr(RFWU);
}

static void usb4_retimer_cb_set_retimer_info(void *arg)
{
	ec_retimer_fw_update(arg);
}

static void (*usb4_retimer_callbacks[5])(void *) = {
	usb4_retimer_cb_standard_query,		/* Function 0 */
	usb4_retimer_cb_get_power_state,	/* Function 1 */
	usb4_retimer_cb_set_power_state,	/* Function 2 */
	usb4_retimer_cb_get_retimer_info,	/* Function 3 */
	usb4_retimer_cb_set_retimer_info,	/* Function 4 */
};

static void usb4_retimer_fill_ssdt(const struct device *dev)
{
	const struct drivers_intel_usb4_retimer_config *config = dev->chip_info;
	const char *scope = acpi_device_scope(dev);

	if (!scope || !config)
		return;

	if (!config->power_gpio.pin_count) {
		printk(BIOS_ERR, "%s: Power GPIO required for %s\n", __func__, dev_path(dev));
		return;
	}

	/* Write the _DSM that toggles power with provided GPIO. */
	acpigen_write_scope(scope);
	acpigen_write_dsm(INTEL_USB4_RETIMER_DSM_UUID, usb4_retimer_callbacks,
			  ARRAY_SIZE(usb4_retimer_callbacks), (void *)&config->power_gpio);
	acpigen_pop_len(); /* Scope */

	printk(BIOS_INFO, "%s: %s at %s\n", acpi_device_path(dev), 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 = {
	CHIP_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(void *arg)
{
}