1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt Time Management Unit (TMU) support
5 * Copyright (C) 2019, Intel Corporation
6 * Authors: Mika Westerberg <mika.westerberg@linux.intel.com>
7 * Rajmohan Mani <rajmohan.mani@intel.com>
10 #include <linux/delay.h>
14 static const char *tb_switch_tmu_mode_name(const struct tb_switch *sw)
16 bool root_switch = !tb_route(sw);
18 switch (sw->tmu.rate) {
19 case TB_SWITCH_TMU_RATE_OFF:
22 case TB_SWITCH_TMU_RATE_HIFI:
23 /* Root switch does not have upstream directionality */
26 if (sw->tmu.unidirectional)
27 return "uni-directional, HiFi";
28 return "bi-directional, HiFi";
30 case TB_SWITCH_TMU_RATE_NORMAL:
33 return "uni-directional, normal";
40 static bool tb_switch_tmu_ucap_supported(struct tb_switch *sw)
45 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH,
46 sw->tmu.cap + TMU_RTR_CS_0, 1);
50 return !!(val & TMU_RTR_CS_0_UCAP);
53 static int tb_switch_tmu_rate_read(struct tb_switch *sw)
58 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH,
59 sw->tmu.cap + TMU_RTR_CS_3, 1);
63 val >>= TMU_RTR_CS_3_TS_PACKET_INTERVAL_SHIFT;
67 static int tb_switch_tmu_rate_write(struct tb_switch *sw, int rate)
72 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH,
73 sw->tmu.cap + TMU_RTR_CS_3, 1);
77 val &= ~TMU_RTR_CS_3_TS_PACKET_INTERVAL_MASK;
78 val |= rate << TMU_RTR_CS_3_TS_PACKET_INTERVAL_SHIFT;
80 return tb_sw_write(sw, &val, TB_CFG_SWITCH,
81 sw->tmu.cap + TMU_RTR_CS_3, 1);
84 static int tb_port_tmu_write(struct tb_port *port, u8 offset, u32 mask,
90 ret = tb_port_read(port, &data, TB_CFG_PORT, port->cap_tmu + offset, 1);
97 return tb_port_write(port, &data, TB_CFG_PORT,
98 port->cap_tmu + offset, 1);
101 static int tb_port_tmu_set_unidirectional(struct tb_port *port,
106 if (!port->sw->tmu.has_ucap)
109 val = unidirectional ? TMU_ADP_CS_3_UDM : 0;
110 return tb_port_tmu_write(port, TMU_ADP_CS_3, TMU_ADP_CS_3_UDM, val);
113 static inline int tb_port_tmu_unidirectional_disable(struct tb_port *port)
115 return tb_port_tmu_set_unidirectional(port, false);
118 static inline int tb_port_tmu_unidirectional_enable(struct tb_port *port)
120 return tb_port_tmu_set_unidirectional(port, true);
123 static bool tb_port_tmu_is_unidirectional(struct tb_port *port)
128 ret = tb_port_read(port, &val, TB_CFG_PORT,
129 port->cap_tmu + TMU_ADP_CS_3, 1);
133 return val & TMU_ADP_CS_3_UDM;
136 static int tb_port_tmu_time_sync(struct tb_port *port, bool time_sync)
138 u32 val = time_sync ? TMU_ADP_CS_6_DTS : 0;
140 return tb_port_tmu_write(port, TMU_ADP_CS_6, TMU_ADP_CS_6_DTS, val);
143 static int tb_port_tmu_time_sync_disable(struct tb_port *port)
145 return tb_port_tmu_time_sync(port, true);
148 static int tb_port_tmu_time_sync_enable(struct tb_port *port)
150 return tb_port_tmu_time_sync(port, false);
153 static int tb_switch_tmu_set_time_disruption(struct tb_switch *sw, bool set)
155 u32 val, offset, bit;
158 if (tb_switch_is_usb4(sw)) {
159 offset = sw->tmu.cap + TMU_RTR_CS_0;
160 bit = TMU_RTR_CS_0_TD;
162 offset = sw->cap_vsec_tmu + TB_TIME_VSEC_3_CS_26;
163 bit = TB_TIME_VSEC_3_CS_26_TD;
166 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, offset, 1);
175 return tb_sw_write(sw, &val, TB_CFG_SWITCH, offset, 1);
179 * tb_switch_tmu_init() - Initialize switch TMU structures
180 * @sw: Switch to initialized
182 * This function must be called before other TMU related functions to
183 * makes the internal structures are filled in correctly. Does not
184 * change any hardware configuration.
186 int tb_switch_tmu_init(struct tb_switch *sw)
188 struct tb_port *port;
191 if (tb_switch_is_icm(sw))
194 ret = tb_switch_find_cap(sw, TB_SWITCH_CAP_TMU);
198 tb_switch_for_each_port(sw, port) {
201 cap = tb_port_find_cap(port, TB_PORT_CAP_TIME1);
206 ret = tb_switch_tmu_rate_read(sw);
212 sw->tmu.has_ucap = tb_switch_tmu_ucap_supported(sw);
213 if (sw->tmu.has_ucap) {
214 tb_sw_dbg(sw, "TMU: supports uni-directional mode\n");
217 struct tb_port *up = tb_upstream_port(sw);
219 sw->tmu.unidirectional =
220 tb_port_tmu_is_unidirectional(up);
223 sw->tmu.unidirectional = false;
226 tb_sw_dbg(sw, "TMU: current mode: %s\n", tb_switch_tmu_mode_name(sw));
231 * tb_switch_tmu_post_time() - Update switch local time
232 * @sw: Switch whose time to update
234 * Updates switch local time using time posting procedure.
236 int tb_switch_tmu_post_time(struct tb_switch *sw)
238 unsigned int post_time_high_offset, post_time_high = 0;
239 unsigned int post_local_time_offset, post_time_offset;
240 struct tb_switch *root_switch = sw->tb->root_switch;
241 u64 hi, mid, lo, local_time, post_time;
242 int i, ret, retries = 100;
243 u32 gm_local_time[3];
248 if (!tb_switch_is_usb4(sw))
251 /* Need to be able to read the grand master time */
252 if (!root_switch->tmu.cap)
255 ret = tb_sw_read(root_switch, gm_local_time, TB_CFG_SWITCH,
256 root_switch->tmu.cap + TMU_RTR_CS_1,
257 ARRAY_SIZE(gm_local_time));
261 for (i = 0; i < ARRAY_SIZE(gm_local_time); i++)
262 tb_sw_dbg(root_switch, "local_time[%d]=0x%08x\n", i,
265 /* Convert to nanoseconds (drop fractional part) */
266 hi = gm_local_time[2] & TMU_RTR_CS_3_LOCAL_TIME_NS_MASK;
267 mid = gm_local_time[1];
268 lo = (gm_local_time[0] & TMU_RTR_CS_1_LOCAL_TIME_NS_MASK) >>
269 TMU_RTR_CS_1_LOCAL_TIME_NS_SHIFT;
270 local_time = hi << 48 | mid << 16 | lo;
272 /* Tell the switch that time sync is disrupted for a while */
273 ret = tb_switch_tmu_set_time_disruption(sw, true);
277 post_local_time_offset = sw->tmu.cap + TMU_RTR_CS_22;
278 post_time_offset = sw->tmu.cap + TMU_RTR_CS_24;
279 post_time_high_offset = sw->tmu.cap + TMU_RTR_CS_25;
282 * Write the Grandmaster time to the Post Local Time registers
285 ret = tb_sw_write(sw, &local_time, TB_CFG_SWITCH,
286 post_local_time_offset, 2);
291 * Have the new switch update its local time by:
292 * 1) writing 0x1 to the Post Time Low register and 0xffffffff to
293 * Post Time High register.
294 * 2) write 0 to Post Time High register and then wait for
295 * the completion of the post_time register becomes 0.
296 * This means the time has been converged properly.
298 post_time = 0xffffffff00000001ULL;
300 ret = tb_sw_write(sw, &post_time, TB_CFG_SWITCH, post_time_offset, 2);
304 ret = tb_sw_write(sw, &post_time_high, TB_CFG_SWITCH,
305 post_time_high_offset, 1);
311 ret = tb_sw_read(sw, &post_time, TB_CFG_SWITCH,
312 post_time_offset, 2);
315 } while (--retries && post_time);
322 tb_sw_dbg(sw, "TMU: updated local time to %#llx\n", local_time);
325 tb_switch_tmu_set_time_disruption(sw, false);
330 * tb_switch_tmu_disable() - Disable TMU of a switch
331 * @sw: Switch whose TMU to disable
333 * Turns off TMU of @sw if it is enabled. If not enabled does nothing.
335 int tb_switch_tmu_disable(struct tb_switch *sw)
338 * No need to disable TMU on devices that don't support CLx since
339 * on these devices e.g. Alpine Ridge and earlier, the TMU mode
340 * HiFi bi-directional is enabled by default and we don't change it.
342 if (!tb_switch_is_clx_supported(sw))
345 /* Already disabled? */
346 if (sw->tmu.rate == TB_SWITCH_TMU_RATE_OFF)
351 bool unidirectional = tb_switch_tmu_hifi_is_enabled(sw, true);
352 struct tb_switch *parent = tb_switch_parent(sw);
353 struct tb_port *down, *up;
356 down = tb_port_at(tb_route(sw), parent);
357 up = tb_upstream_port(sw);
359 * In case of uni-directional time sync, TMU handshake is
360 * initiated by upstream router. In case of bi-directional
361 * time sync, TMU handshake is initiated by downstream router.
362 * Therefore, we change the rate to off in the respective
366 tb_switch_tmu_rate_write(parent, TB_SWITCH_TMU_RATE_OFF);
368 tb_switch_tmu_rate_write(sw, TB_SWITCH_TMU_RATE_OFF);
370 tb_port_tmu_time_sync_disable(up);
371 ret = tb_port_tmu_time_sync_disable(down);
375 if (unidirectional) {
376 /* The switch may be unplugged so ignore any errors */
377 tb_port_tmu_unidirectional_disable(up);
378 ret = tb_port_tmu_unidirectional_disable(down);
383 tb_switch_tmu_rate_write(sw, TB_SWITCH_TMU_RATE_OFF);
386 sw->tmu.unidirectional = false;
387 sw->tmu.rate = TB_SWITCH_TMU_RATE_OFF;
389 tb_sw_dbg(sw, "TMU: disabled\n");
393 static void __tb_switch_tmu_off(struct tb_switch *sw, bool unidirectional)
395 struct tb_switch *parent = tb_switch_parent(sw);
396 struct tb_port *down, *up;
398 down = tb_port_at(tb_route(sw), parent);
399 up = tb_upstream_port(sw);
401 * In case of any failure in one of the steps when setting
402 * bi-directional or uni-directional TMU mode, get back to the TMU
403 * configurations in off mode. In case of additional failures in
404 * the functions below, ignore them since the caller shall already
407 tb_port_tmu_time_sync_disable(down);
408 tb_port_tmu_time_sync_disable(up);
410 tb_switch_tmu_rate_write(parent, TB_SWITCH_TMU_RATE_OFF);
412 tb_switch_tmu_rate_write(sw, TB_SWITCH_TMU_RATE_OFF);
414 tb_port_tmu_unidirectional_disable(down);
415 tb_port_tmu_unidirectional_disable(up);
419 * This function is called when the previous TMU mode was
420 * TB_SWITCH_TMU_RATE_OFF.
422 static int __tb_switch_tmu_enable_bidirectional(struct tb_switch *sw)
424 struct tb_switch *parent = tb_switch_parent(sw);
425 struct tb_port *up, *down;
428 up = tb_upstream_port(sw);
429 down = tb_port_at(tb_route(sw), parent);
431 ret = tb_port_tmu_unidirectional_disable(up);
435 ret = tb_port_tmu_unidirectional_disable(down);
439 ret = tb_switch_tmu_rate_write(sw, TB_SWITCH_TMU_RATE_HIFI);
443 ret = tb_port_tmu_time_sync_enable(up);
447 ret = tb_port_tmu_time_sync_enable(down);
454 __tb_switch_tmu_off(sw, false);
458 static int tb_switch_tmu_objection_mask(struct tb_switch *sw)
463 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH,
464 sw->cap_vsec_tmu + TB_TIME_VSEC_3_CS_9, 1);
468 val &= ~TB_TIME_VSEC_3_CS_9_TMU_OBJ_MASK;
470 return tb_sw_write(sw, &val, TB_CFG_SWITCH,
471 sw->cap_vsec_tmu + TB_TIME_VSEC_3_CS_9, 1);
474 static int tb_switch_tmu_unidirectional_enable(struct tb_switch *sw)
476 struct tb_port *up = tb_upstream_port(sw);
478 return tb_port_tmu_write(up, TMU_ADP_CS_6,
479 TMU_ADP_CS_6_DISABLE_TMU_OBJ_MASK,
480 TMU_ADP_CS_6_DISABLE_TMU_OBJ_MASK);
484 * This function is called when the previous TMU mode was
485 * TB_SWITCH_TMU_RATE_OFF.
487 static int __tb_switch_tmu_enable_unidirectional(struct tb_switch *sw)
489 struct tb_switch *parent = tb_switch_parent(sw);
490 struct tb_port *up, *down;
493 up = tb_upstream_port(sw);
494 down = tb_port_at(tb_route(sw), parent);
495 ret = tb_switch_tmu_rate_write(parent, TB_SWITCH_TMU_RATE_HIFI);
499 ret = tb_port_tmu_unidirectional_enable(up);
503 ret = tb_port_tmu_time_sync_enable(up);
507 ret = tb_port_tmu_unidirectional_enable(down);
511 ret = tb_port_tmu_time_sync_enable(down);
518 __tb_switch_tmu_off(sw, true);
522 static int tb_switch_tmu_hifi_enable(struct tb_switch *sw)
524 bool unidirectional = sw->tmu.unidirectional_request;
527 if (unidirectional && !sw->tmu.has_ucap)
531 * No need to enable TMU on devices that don't support CLx since on
532 * these devices e.g. Alpine Ridge and earlier, the TMU mode HiFi
533 * bi-directional is enabled by default.
535 if (!tb_switch_is_clx_supported(sw))
538 if (tb_switch_tmu_hifi_is_enabled(sw, sw->tmu.unidirectional_request))
541 if (tb_switch_is_titan_ridge(sw) && unidirectional) {
542 /* Titan Ridge supports only CL0s */
543 if (!tb_switch_is_cl0s_enabled(sw))
546 ret = tb_switch_tmu_objection_mask(sw);
550 ret = tb_switch_tmu_unidirectional_enable(sw);
555 ret = tb_switch_tmu_set_time_disruption(sw, true);
560 /* The used mode changes are from OFF to HiFi-Uni/HiFi-BiDir */
561 if (sw->tmu.rate == TB_SWITCH_TMU_RATE_OFF) {
563 ret = __tb_switch_tmu_enable_unidirectional(sw);
565 ret = __tb_switch_tmu_enable_bidirectional(sw);
569 sw->tmu.unidirectional = unidirectional;
572 * Host router port configurations are written as
573 * part of configurations for downstream port of the parent
574 * of the child node - see above.
575 * Here only the host router' rate configuration is written.
577 ret = tb_switch_tmu_rate_write(sw, TB_SWITCH_TMU_RATE_HIFI);
582 sw->tmu.rate = TB_SWITCH_TMU_RATE_HIFI;
584 tb_sw_dbg(sw, "TMU: mode set to: %s\n", tb_switch_tmu_mode_name(sw));
585 return tb_switch_tmu_set_time_disruption(sw, false);
589 * tb_switch_tmu_enable() - Enable TMU on a router
590 * @sw: Router whose TMU to enable
592 * Enables TMU of a router to be in uni-directional or bi-directional HiFi mode.
593 * Calling tb_switch_tmu_configure() is required before calling this function,
594 * to select the mode HiFi and directionality (uni-directional/bi-directional).
595 * In both modes all tunneling should work. Uni-directional mode is required for
596 * CLx (Link Low-Power) to work.
598 int tb_switch_tmu_enable(struct tb_switch *sw)
600 if (sw->tmu.rate_request == TB_SWITCH_TMU_RATE_NORMAL)
603 return tb_switch_tmu_hifi_enable(sw);
607 * tb_switch_tmu_configure() - Configure the TMU rate and directionality
608 * @sw: Router whose mode to change
609 * @rate: Rate to configure Off/LowRes/HiFi
610 * @unidirectional: If uni-directional (bi-directional otherwise)
612 * Selects the rate of the TMU and directionality (uni-directional or
613 * bi-directional). Must be called before tb_switch_tmu_enable().
615 void tb_switch_tmu_configure(struct tb_switch *sw,
616 enum tb_switch_tmu_rate rate, bool unidirectional)
618 sw->tmu.unidirectional_request = unidirectional;
619 sw->tmu.rate_request = rate;