Debian Codegraph @

src/drivers/intel/usb4/retimer/retimer.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include <acpi/acpigen.h>
4 #include <acpi/acpi_device.h>
5 #include <acpi/acpi_pld.h>
6 #include <console/console.h>
7 #include <device/device.h>
8 #include <drivers/usb/acpi/chip.h>
9 #include <gpio.h>
10 #include <stdio.h>
11
12 #include "chip.h"
13 #include "retimer.h"
14
15 /* Unique ID for the retimer _DSM. */
16 #define INTEL_USB4_RETIMER_DSM_UUID "E0053122-795B-4122-8A5E-57BE1D26ACB3"
17
18 static const char *usb4_retimer_scope;
19 static const char *usb4_retimer_path_arg(const char *arg)
20 {
21 /* \\_SB.PCI0.TDMx.ARG */
22 static char name[DEVICE_PATH_MAX];
23 snprintf(name, sizeof(name), "%s%c%s", usb4_retimer_scope, '.', arg);
24 return name;
25 }
26
27 /* Each polling cycle takes up to 25 ms with a total of 12 of these iterations */
28 #define USB4_RETIMER_ITERATION_NUM 12
29 #define USB4_RETIMER_POLL_CYCLE_MS 25
30 static void usb4_retimer_execute_ec_cmd(uint8_t port, uint8_t cmd, uint8_t expected_value,
31 struct acpi_gpio *power_gpio)
32 {
33 const char *RFWU = ec_retimer_fw_update_path();
34 const uint8_t data = cmd << USB_RETIMER_FW_UPDATE_OP_SHIFT | port;
35
36 /* Invoke EC Retimer firmware update command execution */
37 ec_retimer_fw_update(data);
38 /* If RFWU has return value 0xfe, return error -1 */
39 if (RFWU) {
40 acpigen_write_if_lequal_namestr_int(RFWU, USB_RETIMER_FW_UPDATE_ERROR);
41 acpigen_disable_tx_gpio(power_gpio);
42 acpigen_write_return_integer(-1);
43 acpigen_pop_len(); /* If */
44 }
45
46 acpigen_write_store_int_to_op(USB4_RETIMER_ITERATION_NUM, LOCAL2_OP);
47 acpigen_emit_byte(WHILE_OP);
48 acpigen_write_len_f();
49 acpigen_emit_byte(LGREATER_OP);
50 acpigen_emit_byte(LOCAL2_OP);
51 acpigen_emit_byte(ZERO_OP);
52
53 if (RFWU) {
54 acpigen_write_if_lequal_namestr_int(RFWU, expected_value);
55 acpigen_emit_byte(BREAK_OP);
56 acpigen_pop_len(); /* If */
57
58 if (cmd == USB_RETIMER_FW_UPDATE_GET_MUX) {
59 acpigen_write_if_lequal_namestr_int(RFWU, USB_RETIMER_FW_UPDATE_INVALID_MUX);
60 acpigen_write_sleep(USB4_RETIMER_POLL_CYCLE_MS);
61 acpigen_emit_byte(DECREMENT_OP);
62 acpigen_emit_byte(LOCAL2_OP);
63 acpigen_emit_byte(CONTINUE_OP);
64 acpigen_pop_len(); /* If */
65
66 acpigen_emit_byte(AND_OP);
67 acpigen_emit_namestring(RFWU);
68 acpigen_write_integer(USB_RETIMER_FW_UPDATE_MUX_MASK);
69 acpigen_emit_byte(LOCAL3_OP);
70 acpigen_write_if();
71 acpigen_emit_byte(LNOT_OP);
72 acpigen_emit_byte(LEQUAL_OP);
73 acpigen_emit_byte(LOCAL3_OP);
74 acpigen_emit_byte(0);
75 acpigen_disable_tx_gpio(power_gpio);
76 acpigen_write_return_integer(-1);
77 acpigen_pop_len(); /* If */
78 } else if (cmd == USB_RETIMER_FW_UPDATE_SET_TBT) {
79 /*
80 * EC return either USB_PD_MUX_USB4_ENABLED or USB_PD_MUX_TBT_COMPAT_ENABLED
81 * to RFWU after the USB_RETIMER_FW_UPDATE_SET_TBT command execution. It is
82 * needed to add additional check for USB_PD_MUX_TBT_COMPAT_ENABLED.
83 */
84 acpigen_write_if_lequal_namestr_int(RFWU, USB_PD_MUX_TBT_COMPAT_ENABLED);
85 acpigen_emit_byte(BREAK_OP);
86 acpigen_pop_len(); /* If */
87 }
88 }
89
90 acpigen_write_sleep(USB4_RETIMER_POLL_CYCLE_MS);
91 acpigen_emit_byte(DECREMENT_OP);
92 acpigen_emit_byte(LOCAL2_OP);
93 acpigen_pop_len(); /* While */
94
95 /*
96 * Check whether there is timeout error
97 * Return: -1 if timeout error occurring
98 */
99 acpigen_write_if_lequal_op_int(LOCAL2_OP, 0);
100 acpigen_disable_tx_gpio(power_gpio);
101 acpigen_write_return_integer(-1);
102 acpigen_pop_len(); /* If */
103 }
104
105 static void enable_retimer_online_state(uint8_t port, struct acpi_gpio *power_gpio)
106 {
107 uint8_t expected_value;
108
109 /*
110 * Enable_retimer_online_state under NDA
111 * 1. Force power on
112 * 2. Check if there is a device connected
113 * 3. Suspend PD
114 * 4. Set Mux to USB mode
115 * 5. Set Mux to Safe mode
116 * 6. Set Mux to TBT mode
117 */
118
119 /* Force power on for the retimer on the port */
120 acpigen_enable_tx_gpio(power_gpio);
121
122 /*
123 * Get MUX mode state
124 * Return -1 if there is a device connected on the port.
125 * Otherwise proceed Retimer firmware upgrade operation.
126 */
127 expected_value = USB_PD_MUX_NONE;
128 usb4_retimer_execute_ec_cmd(port, USB_RETIMER_FW_UPDATE_GET_MUX, expected_value,
129 power_gpio);
130
131 /*
132 * Suspend PD
133 * Command: USB_RETIMER_FW_UPDATE_SUSPEND_PD
134 * Expect return value: 0
135 */
136 expected_value = 0;
137 usb4_retimer_execute_ec_cmd(port, USB_RETIMER_FW_UPDATE_SUSPEND_PD, expected_value,
138 power_gpio);
139
140 /*
141 * Set MUX USB Mode
142 * Command: USB_RETIMER_FW_UPDATE_SUSPEND_PD
143 * Expect return value: USB_PD_MUX_USB_ENABLED
144 */
145 expected_value = USB_PD_MUX_USB_ENABLED;
146 usb4_retimer_execute_ec_cmd(port, USB_RETIMER_FW_UPDATE_SET_USB, expected_value,
147 power_gpio);
148
149 /*
150 * Set MUX Safe Mode
151 * Command: USB_RETIMER_FW_UPDATE_SET_SAFE
152 * Expect return value: USB_PD_MUX_SAFE_MODE
153 */
154 expected_value = USB_PD_MUX_SAFE_MODE;
155 usb4_retimer_execute_ec_cmd(port, USB_RETIMER_FW_UPDATE_SET_SAFE, expected_value,
156 power_gpio);
157
158 /*
159 * Set MUX TBT Mode
160 * Command: USB_RETIMER_FW_UPDATE_SET_TBT
161 * Expect return value: USB_PD_MUX_USB4_ENABLED or USB_PD_MUX_TBT_COMPAT_ENABLED
162 */
163 expected_value = USB_PD_MUX_USB4_ENABLED;
164 usb4_retimer_execute_ec_cmd(port, USB_RETIMER_FW_UPDATE_SET_TBT, expected_value,
165 power_gpio);
166 }
167
168 static void disable_retimer_online_state(uint8_t port, struct acpi_gpio *power_gpio)
169 {
170 uint8_t expected_value;
171
172 /*
173 * Disable_retimer_online_state
174 * 1. Set Mux to disconnect mode
175 * 2. Resume PD
176 * 3. Force power off
177 */
178
179 /*
180 * Set MUX Disconnect Mode
181 * Command: USB_RETIMER_FW_UPDATE_DISCONNECT
182 * Expect return value: 0
183 */
184 expected_value = 0;
185 usb4_retimer_execute_ec_cmd(port, USB_RETIMER_FW_UPDATE_DISCONNECT, expected_value,
186 power_gpio);
187
188 /*
189 * Resume PD
190 * Command: USB_RETIMER_FW_UPDATE_RESUME_PD
191 * Expect return value: 1
192 */
193 expected_value = 1;
194 usb4_retimer_execute_ec_cmd(port, USB_RETIMER_FW_UPDATE_RESUME_PD, expected_value,
195 power_gpio);
196
197 /* Force power off */
198 acpigen_disable_tx_gpio(power_gpio);
199 }
200
201 /*
202 * Arg0: UUID e0053122-795b-4122-8a5e-57be1d26acb3
203 * Arg1: Revision ID (set to 1)
204 * Arg2: Function Index
205 * 0: Query command implemented
206 * 1: Get power state
207 * 2: Set power state
208 * Arg3: A package containing parameters for the function specified
209 * by the UUID, revision ID, function index and port index.
210 */
211 static void usb4_retimer_cb_standard_query(uint8_t port, void *arg)
212 {
213 /*
214 * ToInteger (Arg1, Local1)
215 * If (Local1 == 1) {
216 * Return(Buffer() {0x7})
217 * }
218 * Return (Buffer() {0x01})
219 */
220 acpigen_write_to_integer(ARG1_OP, LOCAL1_OP);
221
222 /* Revision 1 supports 2 Functions beyond the standard query */
223 acpigen_write_if_lequal_op_int(LOCAL1_OP, 1);
224 acpigen_write_return_singleton_buffer(0x7);
225 acpigen_pop_len(); /* If */
226
227 /* Other revisions support no additional functions */
228 acpigen_write_return_singleton_buffer(0x1);
229 }
230
231 static void usb4_retimer_cb_get_power_state(uint8_t port, void *arg)
232 {
233 const char *PWR;
234 char pwr[DEVICE_PATH_MAX];
235
236 snprintf(pwr, sizeof(pwr), "HR.DFP%1d.PWR", port);
237 PWR = usb4_retimer_path_arg(pwr);
238
239 /*
240 * If (PWR > 0) {
241 * Return (1)
242 * }
243 */
244 acpigen_write_if();
245 acpigen_emit_byte(LGREATER_OP);
246 acpigen_emit_namestring(PWR);
247 acpigen_emit_byte(0);
248 acpigen_write_return_integer(1);
249
250 /*
251 * Else {
252 * Return (0)
253 * }
254 */
255 acpigen_write_else();
256 acpigen_write_return_integer(0);
257 acpigen_pop_len();
258 }
259
260 static void usb4_retimer_cb_set_power_state(uint8_t port, void *arg)
261 {
262 struct acpi_gpio *power_gpio = arg;
263 const char *PWR;
264 char pwr[DEVICE_PATH_MAX];
265
266 snprintf(pwr, sizeof(pwr), "HR.DFP%1d.PWR", port);
267 PWR = usb4_retimer_path_arg(pwr);
268
269 /*
270 * Get information to set retimer power state from Arg3[0]
271 * Local1 = DeRefOf (Arg3[0])
272 */
273 acpigen_get_package_op_element(ARG3_OP, 0, LOCAL1_OP);
274
275 /*
276 * If ((Local1 == 0) && (PWR > 0)) {
277 * PWR--
278 * If (PWR == 0) {
279 * // Disable retimer online state
280 * }
281 * }
282 */
283 acpigen_write_if();
284 acpigen_emit_byte(LAND_OP);
285 acpigen_emit_byte(LEQUAL_OP);
286 acpigen_emit_byte(LOCAL1_OP);
287 acpigen_emit_byte(0);
288 acpigen_emit_byte(LGREATER_OP);
289 acpigen_emit_namestring(PWR);
290 acpigen_emit_byte(0);
291 /* PWR-- */
292 acpigen_emit_byte(DECREMENT_OP);
293 acpigen_emit_namestring(PWR);
294 acpigen_write_if_lequal_namestr_int(PWR, 0); /* If (PWR == 0) */
295 disable_retimer_online_state(port, power_gpio);
296 acpigen_pop_len(); /* If (PWR == 0) */
297
298 /*
299 * Else If ((Local1 == 1) && (PWR == 0)) {
300 * // Enable retimer online state
301 * PWR++
302 * }
303 */
304 acpigen_write_else();
305 acpigen_write_if();
306 acpigen_emit_byte(LAND_OP);
307 acpigen_emit_byte(LEQUAL_OP);
308 acpigen_emit_byte(LOCAL1_OP);
309 acpigen_emit_byte(1);
310 acpigen_emit_byte(LEQUAL_OP);
311 acpigen_emit_namestring(PWR);
312 acpigen_emit_byte(0);
313 enable_retimer_online_state(port, power_gpio);
314 /* PWR++ */
315 acpigen_emit_byte(INCREMENT_OP);
316 acpigen_emit_namestring(PWR);
317
318 /*
319 * Else {
320 * Return (0)
321 * }
322 */
323 acpigen_write_else();
324 acpigen_write_return_integer(0);
325 acpigen_pop_len(); /* Else */
326 acpigen_pop_len(); /* If */
327
328 /*
329 * If (PWR == 1) {
330 * Return (1)
331 * }
332 */
333 acpigen_write_if_lequal_namestr_int(PWR, 1);
334 acpigen_write_return_integer(1);
335
336 /*
337 * Else {
338 * Return (0)
339 * }
340 */
341 acpigen_write_else();
342 acpigen_write_return_integer(0);
343 acpigen_pop_len();
344 }
345
346 static void (*usb4_retimer_callbacks[3])(uint8_t port, void *) = {
347 usb4_retimer_cb_standard_query, /* Function 0 */
348 usb4_retimer_cb_get_power_state, /* Function 1 */
349 usb4_retimer_cb_set_power_state, /* Function 2 */
350 };
351
352 static void usb4_retimer_write_dsm(uint8_t port, const char *uuid,
353 void (**callbacks)(uint8_t port, void *), size_t count, void *arg)
354 {
355 struct usb4_retimer_dsm_uuid id = DSM_UUID(uuid, callbacks, count, arg);
356 size_t i;
357
358 acpigen_write_to_integer(ARG2_OP, LOCAL0_OP);
359
360 for (i = 0; i < id.count; i++) {
361 /* If (LEqual (Local0, i)) */
362 acpigen_write_if_lequal_op_int(LOCAL0_OP, i);
363
364 /* Callback to write if handler. */
365 if (id.callbacks[i])
366 id.callbacks[i](port, id.arg);
367
368 acpigen_pop_len(); /* If */
369 }
370 }
371
372 static void usb4_retimer_fill_ssdt(const struct device *dev)
373 {
374 struct drivers_intel_usb4_retimer_config *config = dev->chip_info;
375 const struct device *usb_device;
376 static char dfp[DEVICE_PATH_MAX];
377 struct acpi_pld pld;
378 uint8_t dfp_port, usb_port;
379 int ec_port = 0;
380
381 usb4_retimer_scope = acpi_device_scope(dev);
382 if (!usb4_retimer_scope || !config)
383 return;
384
385 /* Scope */
386 acpigen_write_scope(usb4_retimer_scope);
387
388 /* Host router */
389 acpigen_write_device("HR");
390 acpigen_write_ADR(0);
391 acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_ON);
392
393 for (dfp_port = 0; dfp_port < DFP_NUM_MAX; dfp_port++) {
394 if (!config->dfp[dfp_port].power_gpio.pin_count) {
395 printk(BIOS_WARNING, "%s: No DFP%1d power GPIO for %s\n",
396 __func__, dfp_port, dev_path(dev));
397 continue;
398 }
399
400 usb_device = config->dfp[dfp_port].typec_port;
401 usb_port = usb_device->path.usb.port_id;
402
403 /* Map to the EC port number if it is explicitly defined in the device tree */
404 ec_port = (config->dfp[dfp_port].ec_port) ?
405 config->dfp[dfp_port].ec_port - EC_TYPEC_PORT_0 :
406 retimer_get_index_for_typec(usb_port);
407 if (ec_port == -1) {
408 printk(BIOS_ERR, "%s: No relative EC port found for TC port %d\n",
409 __func__, usb_port);
410 continue;
411 }
412 printk(BIOS_INFO, "USB Type-C %d mapped to EC port %d\n", usb_port, ec_port);
413 /* DFPx */
414 snprintf(dfp, sizeof(dfp), "DFP%1d", ec_port);
415 acpigen_write_device(dfp);
416 /* _ADR part is for the lane adapter */
417 acpigen_write_ADR(dfp_port*2 + 1);
418
419 /* Fill _PLD with the same USB 3.x object on the Type-C connector */
420 if (CONFIG(DRIVERS_USB_ACPI)) {
421 if (usb_acpi_get_pld(usb_device, &pld))
422 acpigen_write_pld(&pld);
423 else
424 printk(BIOS_ERR, "Error retrieving PLD for USB Type-C %d\n",
425 usb_port);
426 }
427
428 /* Power online reference counter(_PWR) */
429 acpigen_write_name("PWR");
430 acpigen_write_zero();
431
432 /* Method (_DSM, 4, Serialized) */
433 acpigen_write_method_serialized("_DSM", 0x4);
434 /* ToBuffer (Arg0, Local0) */
435 acpigen_write_to_buffer(ARG0_OP, LOCAL0_OP);
436 acpigen_write_if(); /* If (UUID != INTEL_USB4_RETIMER_DSM_UUID) */
437 acpigen_emit_byte(LNOT_OP);
438 acpigen_emit_byte(LEQUAL_OP);
439 acpigen_emit_byte(LOCAL0_OP);
440 acpigen_write_uuid(INTEL_USB4_RETIMER_DSM_UUID);
441 /* Return (Buffer (One) { 0x0 }) */
442 acpigen_write_return_singleton_buffer(0x0);
443 acpigen_pop_len();
444 usb4_retimer_write_dsm(ec_port, INTEL_USB4_RETIMER_DSM_UUID,
445 usb4_retimer_callbacks, ARRAY_SIZE(usb4_retimer_callbacks),
446 (void *)&config->dfp[dfp_port].power_gpio);
447 /* Default case: Return (Buffer (One) { 0x0 }) */
448 acpigen_write_return_singleton_buffer(0x0);
449
450 acpigen_pop_len(); /* Method _DSM */
451 acpigen_pop_len(); /* DFP */
452 }
453 acpigen_pop_len(); /* Host Router */
454 acpigen_pop_len(); /* Scope */
455
456 printk(BIOS_INFO, "%s.HR: %s at %s\n", usb4_retimer_scope, dev->chip_ops->name,
457 dev_path(dev));
458 }
459
460 static struct device_operations usb4_retimer_dev_ops = {
461 .read_resources = noop_read_resources,
462 .set_resources = noop_set_resources,
463 .acpi_fill_ssdt = usb4_retimer_fill_ssdt,
464 };
465
466 static void usb4_retimer_enable(struct device *dev)
467 {
468 dev->ops = &usb4_retimer_dev_ops;
469 }
470
471 struct chip_operations drivers_intel_usb4_retimer_ops = {
472 .name = "Intel USB4 Retimer",
473 .enable_dev = usb4_retimer_enable
474 };
475
476 __weak const char *ec_retimer_fw_update_path(void)
477 {
478 return NULL;
479 }
480
481 __weak void ec_retimer_fw_update(uint8_t data)
482 {
483 }
484
485 /*
486 * This function will convert CPU physical port mapping to abstract
487 * EC port mapping.
488 * For example, board might have enabled TCSS port 1 and 3 as per physical
489 * port mapping. Since only 2 TCSS ports are enabled EC will index it as port 0
490 * and port 1. So there will be an issue when coreboot sends command to EC for
491 * port 3 (with coreboot index of 2). EC will produce an error due to wrong index.
492 *
493 * Note: Each SoC code using retimer driver needs to implement this function
494 * since SoC will have physical port details.
495 */
496 __weak int retimer_get_index_for_typec(uint8_t typec_port)
497 {
498 /* By default assume that retimer port index = Type C port */
499 return (int)typec_port;
500 }