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 int tb_switch_set_tmu_mode_params(struct tb_switch *sw,
15 enum tb_switch_tmu_rate rate)
17 u32 freq_meas_wind[2] = { 30, 800 };
18 u32 avg_const[2] = { 4, 8 };
22 if (rate == TB_SWITCH_TMU_RATE_NORMAL) {
23 freq = freq_meas_wind[0];
25 } else if (rate == TB_SWITCH_TMU_RATE_HIFI) {
26 freq = freq_meas_wind[1];
32 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH,
33 sw->tmu.cap + TMU_RTR_CS_0, 1);
37 val &= ~TMU_RTR_CS_0_FREQ_WIND_MASK;
38 val |= FIELD_PREP(TMU_RTR_CS_0_FREQ_WIND_MASK, freq);
40 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH,
41 sw->tmu.cap + TMU_RTR_CS_0, 1);
45 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH,
46 sw->tmu.cap + TMU_RTR_CS_15, 1);
50 val &= ~TMU_RTR_CS_15_FREQ_AVG_MASK &
51 ~TMU_RTR_CS_15_DELAY_AVG_MASK &
52 ~TMU_RTR_CS_15_OFFSET_AVG_MASK &
53 ~TMU_RTR_CS_15_ERROR_AVG_MASK;
54 val |= FIELD_PREP(TMU_RTR_CS_15_FREQ_AVG_MASK, avg) |
55 FIELD_PREP(TMU_RTR_CS_15_DELAY_AVG_MASK, avg) |
56 FIELD_PREP(TMU_RTR_CS_15_OFFSET_AVG_MASK, avg) |
57 FIELD_PREP(TMU_RTR_CS_15_ERROR_AVG_MASK, avg);
59 return tb_sw_write(sw, &val, TB_CFG_SWITCH,
60 sw->tmu.cap + TMU_RTR_CS_15, 1);
63 static const char *tb_switch_tmu_mode_name(const struct tb_switch *sw)
65 bool root_switch = !tb_route(sw);
67 switch (sw->tmu.rate) {
68 case TB_SWITCH_TMU_RATE_OFF:
71 case TB_SWITCH_TMU_RATE_HIFI:
72 /* Root switch does not have upstream directionality */
75 if (sw->tmu.unidirectional)
76 return "uni-directional, HiFi";
77 return "bi-directional, HiFi";
79 case TB_SWITCH_TMU_RATE_NORMAL:
82 return "uni-directional, normal";
89 static bool tb_switch_tmu_ucap_supported(struct tb_switch *sw)
94 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH,
95 sw->tmu.cap + TMU_RTR_CS_0, 1);
99 return !!(val & TMU_RTR_CS_0_UCAP);
102 static int tb_switch_tmu_rate_read(struct tb_switch *sw)
107 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH,
108 sw->tmu.cap + TMU_RTR_CS_3, 1);
112 val >>= TMU_RTR_CS_3_TS_PACKET_INTERVAL_SHIFT;
116 static int tb_switch_tmu_rate_write(struct tb_switch *sw, int rate)
121 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH,
122 sw->tmu.cap + TMU_RTR_CS_3, 1);
126 val &= ~TMU_RTR_CS_3_TS_PACKET_INTERVAL_MASK;
127 val |= rate << TMU_RTR_CS_3_TS_PACKET_INTERVAL_SHIFT;
129 return tb_sw_write(sw, &val, TB_CFG_SWITCH,
130 sw->tmu.cap + TMU_RTR_CS_3, 1);
133 static int tb_port_tmu_write(struct tb_port *port, u8 offset, u32 mask,
139 ret = tb_port_read(port, &data, TB_CFG_PORT, port->cap_tmu + offset, 1);
146 return tb_port_write(port, &data, TB_CFG_PORT,
147 port->cap_tmu + offset, 1);
150 static int tb_port_tmu_set_unidirectional(struct tb_port *port,
155 if (!port->sw->tmu.has_ucap)
158 val = unidirectional ? TMU_ADP_CS_3_UDM : 0;
159 return tb_port_tmu_write(port, TMU_ADP_CS_3, TMU_ADP_CS_3_UDM, val);
162 static inline int tb_port_tmu_unidirectional_disable(struct tb_port *port)
164 return tb_port_tmu_set_unidirectional(port, false);
167 static inline int tb_port_tmu_unidirectional_enable(struct tb_port *port)
169 return tb_port_tmu_set_unidirectional(port, true);
172 static bool tb_port_tmu_is_unidirectional(struct tb_port *port)
177 ret = tb_port_read(port, &val, TB_CFG_PORT,
178 port->cap_tmu + TMU_ADP_CS_3, 1);
182 return val & TMU_ADP_CS_3_UDM;
185 static int tb_port_tmu_time_sync(struct tb_port *port, bool time_sync)
187 u32 val = time_sync ? TMU_ADP_CS_6_DTS : 0;
189 return tb_port_tmu_write(port, TMU_ADP_CS_6, TMU_ADP_CS_6_DTS, val);
192 static int tb_port_tmu_time_sync_disable(struct tb_port *port)
194 return tb_port_tmu_time_sync(port, true);
197 static int tb_port_tmu_time_sync_enable(struct tb_port *port)
199 return tb_port_tmu_time_sync(port, false);
202 static int tb_switch_tmu_set_time_disruption(struct tb_switch *sw, bool set)
204 u32 val, offset, bit;
207 if (tb_switch_is_usb4(sw)) {
208 offset = sw->tmu.cap + TMU_RTR_CS_0;
209 bit = TMU_RTR_CS_0_TD;
211 offset = sw->cap_vsec_tmu + TB_TIME_VSEC_3_CS_26;
212 bit = TB_TIME_VSEC_3_CS_26_TD;
215 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, offset, 1);
224 return tb_sw_write(sw, &val, TB_CFG_SWITCH, offset, 1);
228 * tb_switch_tmu_init() - Initialize switch TMU structures
229 * @sw: Switch to initialized
231 * This function must be called before other TMU related functions to
232 * makes the internal structures are filled in correctly. Does not
233 * change any hardware configuration.
235 int tb_switch_tmu_init(struct tb_switch *sw)
237 struct tb_port *port;
240 if (tb_switch_is_icm(sw))
243 ret = tb_switch_find_cap(sw, TB_SWITCH_CAP_TMU);
247 tb_switch_for_each_port(sw, port) {
250 cap = tb_port_find_cap(port, TB_PORT_CAP_TIME1);
255 ret = tb_switch_tmu_rate_read(sw);
261 sw->tmu.has_ucap = tb_switch_tmu_ucap_supported(sw);
262 if (sw->tmu.has_ucap) {
263 tb_sw_dbg(sw, "TMU: supports uni-directional mode\n");
266 struct tb_port *up = tb_upstream_port(sw);
268 sw->tmu.unidirectional =
269 tb_port_tmu_is_unidirectional(up);
272 sw->tmu.unidirectional = false;
275 tb_sw_dbg(sw, "TMU: current mode: %s\n", tb_switch_tmu_mode_name(sw));
280 * tb_switch_tmu_post_time() - Update switch local time
281 * @sw: Switch whose time to update
283 * Updates switch local time using time posting procedure.
285 int tb_switch_tmu_post_time(struct tb_switch *sw)
287 unsigned int post_time_high_offset, post_time_high = 0;
288 unsigned int post_local_time_offset, post_time_offset;
289 struct tb_switch *root_switch = sw->tb->root_switch;
290 u64 hi, mid, lo, local_time, post_time;
291 int i, ret, retries = 100;
292 u32 gm_local_time[3];
297 if (!tb_switch_is_usb4(sw))
300 /* Need to be able to read the grand master time */
301 if (!root_switch->tmu.cap)
304 ret = tb_sw_read(root_switch, gm_local_time, TB_CFG_SWITCH,
305 root_switch->tmu.cap + TMU_RTR_CS_1,
306 ARRAY_SIZE(gm_local_time));
310 for (i = 0; i < ARRAY_SIZE(gm_local_time); i++)
311 tb_sw_dbg(root_switch, "local_time[%d]=0x%08x\n", i,
314 /* Convert to nanoseconds (drop fractional part) */
315 hi = gm_local_time[2] & TMU_RTR_CS_3_LOCAL_TIME_NS_MASK;
316 mid = gm_local_time[1];
317 lo = (gm_local_time[0] & TMU_RTR_CS_1_LOCAL_TIME_NS_MASK) >>
318 TMU_RTR_CS_1_LOCAL_TIME_NS_SHIFT;
319 local_time = hi << 48 | mid << 16 | lo;
321 /* Tell the switch that time sync is disrupted for a while */
322 ret = tb_switch_tmu_set_time_disruption(sw, true);
326 post_local_time_offset = sw->tmu.cap + TMU_RTR_CS_22;
327 post_time_offset = sw->tmu.cap + TMU_RTR_CS_24;
328 post_time_high_offset = sw->tmu.cap + TMU_RTR_CS_25;
331 * Write the Grandmaster time to the Post Local Time registers
334 ret = tb_sw_write(sw, &local_time, TB_CFG_SWITCH,
335 post_local_time_offset, 2);
340 * Have the new switch update its local time by:
341 * 1) writing 0x1 to the Post Time Low register and 0xffffffff to
342 * Post Time High register.
343 * 2) write 0 to Post Time High register and then wait for
344 * the completion of the post_time register becomes 0.
345 * This means the time has been converged properly.
347 post_time = 0xffffffff00000001ULL;
349 ret = tb_sw_write(sw, &post_time, TB_CFG_SWITCH, post_time_offset, 2);
353 ret = tb_sw_write(sw, &post_time_high, TB_CFG_SWITCH,
354 post_time_high_offset, 1);
360 ret = tb_sw_read(sw, &post_time, TB_CFG_SWITCH,
361 post_time_offset, 2);
364 } while (--retries && post_time);
371 tb_sw_dbg(sw, "TMU: updated local time to %#llx\n", local_time);
374 tb_switch_tmu_set_time_disruption(sw, false);
379 * tb_switch_tmu_disable() - Disable TMU of a switch
380 * @sw: Switch whose TMU to disable
382 * Turns off TMU of @sw if it is enabled. If not enabled does nothing.
384 int tb_switch_tmu_disable(struct tb_switch *sw)
387 * No need to disable TMU on devices that don't support CLx since
388 * on these devices e.g. Alpine Ridge and earlier, the TMU mode
389 * HiFi bi-directional is enabled by default and we don't change it.
391 if (!tb_switch_is_clx_supported(sw))
394 /* Already disabled? */
395 if (sw->tmu.rate == TB_SWITCH_TMU_RATE_OFF)
400 bool unidirectional = sw->tmu.unidirectional;
401 struct tb_switch *parent = tb_switch_parent(sw);
402 struct tb_port *down, *up;
405 down = tb_port_at(tb_route(sw), parent);
406 up = tb_upstream_port(sw);
408 * In case of uni-directional time sync, TMU handshake is
409 * initiated by upstream router. In case of bi-directional
410 * time sync, TMU handshake is initiated by downstream router.
411 * We change downstream router's rate to off for both uni/bidir
412 * cases although it is needed only for the bi-directional mode.
413 * We avoid changing upstream router's mode since it might
414 * have another downstream router plugged, that is set to
415 * uni-directional mode and we don't want to change it's TMU
418 tb_switch_tmu_rate_write(sw, TB_SWITCH_TMU_RATE_OFF);
420 tb_port_tmu_time_sync_disable(up);
421 ret = tb_port_tmu_time_sync_disable(down);
425 if (unidirectional) {
426 /* The switch may be unplugged so ignore any errors */
427 tb_port_tmu_unidirectional_disable(up);
428 ret = tb_port_tmu_unidirectional_disable(down);
433 tb_switch_tmu_rate_write(sw, TB_SWITCH_TMU_RATE_OFF);
436 sw->tmu.unidirectional = false;
437 sw->tmu.rate = TB_SWITCH_TMU_RATE_OFF;
439 tb_sw_dbg(sw, "TMU: disabled\n");
443 static void __tb_switch_tmu_off(struct tb_switch *sw, bool unidirectional)
445 struct tb_switch *parent = tb_switch_parent(sw);
446 struct tb_port *down, *up;
448 down = tb_port_at(tb_route(sw), parent);
449 up = tb_upstream_port(sw);
451 * In case of any failure in one of the steps when setting
452 * bi-directional or uni-directional TMU mode, get back to the TMU
453 * configurations in off mode. In case of additional failures in
454 * the functions below, ignore them since the caller shall already
457 tb_port_tmu_time_sync_disable(down);
458 tb_port_tmu_time_sync_disable(up);
460 tb_switch_tmu_rate_write(parent, TB_SWITCH_TMU_RATE_OFF);
462 tb_switch_tmu_rate_write(sw, TB_SWITCH_TMU_RATE_OFF);
464 tb_switch_set_tmu_mode_params(sw, sw->tmu.rate);
465 tb_port_tmu_unidirectional_disable(down);
466 tb_port_tmu_unidirectional_disable(up);
470 * This function is called when the previous TMU mode was
471 * TB_SWITCH_TMU_RATE_OFF.
473 static int __tb_switch_tmu_enable_bidirectional(struct tb_switch *sw)
475 struct tb_switch *parent = tb_switch_parent(sw);
476 struct tb_port *up, *down;
479 up = tb_upstream_port(sw);
480 down = tb_port_at(tb_route(sw), parent);
482 ret = tb_port_tmu_unidirectional_disable(up);
486 ret = tb_port_tmu_unidirectional_disable(down);
490 ret = tb_switch_tmu_rate_write(sw, TB_SWITCH_TMU_RATE_HIFI);
494 ret = tb_port_tmu_time_sync_enable(up);
498 ret = tb_port_tmu_time_sync_enable(down);
505 __tb_switch_tmu_off(sw, false);
509 static int tb_switch_tmu_objection_mask(struct tb_switch *sw)
514 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH,
515 sw->cap_vsec_tmu + TB_TIME_VSEC_3_CS_9, 1);
519 val &= ~TB_TIME_VSEC_3_CS_9_TMU_OBJ_MASK;
521 return tb_sw_write(sw, &val, TB_CFG_SWITCH,
522 sw->cap_vsec_tmu + TB_TIME_VSEC_3_CS_9, 1);
525 static int tb_switch_tmu_unidirectional_enable(struct tb_switch *sw)
527 struct tb_port *up = tb_upstream_port(sw);
529 return tb_port_tmu_write(up, TMU_ADP_CS_6,
530 TMU_ADP_CS_6_DISABLE_TMU_OBJ_MASK,
531 TMU_ADP_CS_6_DISABLE_TMU_OBJ_MASK);
535 * This function is called when the previous TMU mode was
536 * TB_SWITCH_TMU_RATE_OFF.
538 static int __tb_switch_tmu_enable_unidirectional(struct tb_switch *sw)
540 struct tb_switch *parent = tb_switch_parent(sw);
541 struct tb_port *up, *down;
544 up = tb_upstream_port(sw);
545 down = tb_port_at(tb_route(sw), parent);
546 ret = tb_switch_tmu_rate_write(parent, sw->tmu.rate_request);
550 ret = tb_switch_set_tmu_mode_params(sw, sw->tmu.rate_request);
554 ret = tb_port_tmu_unidirectional_enable(up);
558 ret = tb_port_tmu_time_sync_enable(up);
562 ret = tb_port_tmu_unidirectional_enable(down);
566 ret = tb_port_tmu_time_sync_enable(down);
573 __tb_switch_tmu_off(sw, true);
577 static void __tb_switch_tmu_change_mode_prev(struct tb_switch *sw)
579 struct tb_switch *parent = tb_switch_parent(sw);
580 struct tb_port *down, *up;
582 down = tb_port_at(tb_route(sw), parent);
583 up = tb_upstream_port(sw);
585 * In case of any failure in one of the steps when change mode,
586 * get back to the TMU configurations in previous mode.
587 * In case of additional failures in the functions below,
588 * ignore them since the caller shall already report a failure.
590 tb_port_tmu_set_unidirectional(down, sw->tmu.unidirectional);
591 if (sw->tmu.unidirectional_request)
592 tb_switch_tmu_rate_write(parent, sw->tmu.rate);
594 tb_switch_tmu_rate_write(sw, sw->tmu.rate);
596 tb_switch_set_tmu_mode_params(sw, sw->tmu.rate);
597 tb_port_tmu_set_unidirectional(up, sw->tmu.unidirectional);
600 static int __tb_switch_tmu_change_mode(struct tb_switch *sw)
602 struct tb_switch *parent = tb_switch_parent(sw);
603 struct tb_port *up, *down;
606 up = tb_upstream_port(sw);
607 down = tb_port_at(tb_route(sw), parent);
608 ret = tb_port_tmu_set_unidirectional(down, sw->tmu.unidirectional_request);
612 if (sw->tmu.unidirectional_request)
613 ret = tb_switch_tmu_rate_write(parent, sw->tmu.rate_request);
615 ret = tb_switch_tmu_rate_write(sw, sw->tmu.rate_request);
619 ret = tb_switch_set_tmu_mode_params(sw, sw->tmu.rate_request);
623 ret = tb_port_tmu_set_unidirectional(up, sw->tmu.unidirectional_request);
627 ret = tb_port_tmu_time_sync_enable(down);
631 ret = tb_port_tmu_time_sync_enable(up);
638 __tb_switch_tmu_change_mode_prev(sw);
643 * tb_switch_tmu_enable() - Enable TMU on a router
644 * @sw: Router whose TMU to enable
646 * Enables TMU of a router to be in uni-directional Normal/HiFi
647 * or bi-directional HiFi mode. Calling tb_switch_tmu_configure() is required
648 * before calling this function, to select the mode Normal/HiFi and
649 * directionality (uni-directional/bi-directional).
650 * In HiFi mode all tunneling should work. In Normal mode, DP tunneling can't
651 * work. Uni-directional mode is required for CLx (Link Low-Power) to work.
653 int tb_switch_tmu_enable(struct tb_switch *sw)
655 bool unidirectional = sw->tmu.unidirectional_request;
658 if (unidirectional && !sw->tmu.has_ucap)
662 * No need to enable TMU on devices that don't support CLx since on
663 * these devices e.g. Alpine Ridge and earlier, the TMU mode HiFi
664 * bi-directional is enabled by default.
666 if (!tb_switch_is_clx_supported(sw))
669 if (tb_switch_tmu_is_enabled(sw, sw->tmu.unidirectional_request))
672 if (tb_switch_is_titan_ridge(sw) && unidirectional) {
674 * Titan Ridge supports CL0s and CL1 only. CL0s and CL1 are
675 * enabled and supported together.
677 if (!tb_switch_is_clx_enabled(sw, TB_CL1))
680 ret = tb_switch_tmu_objection_mask(sw);
684 ret = tb_switch_tmu_unidirectional_enable(sw);
689 ret = tb_switch_tmu_set_time_disruption(sw, true);
695 * The used mode changes are from OFF to
696 * HiFi-Uni/HiFi-BiDir/Normal-Uni or from Normal-Uni to
699 if (sw->tmu.rate == TB_SWITCH_TMU_RATE_OFF) {
701 ret = __tb_switch_tmu_enable_unidirectional(sw);
703 ret = __tb_switch_tmu_enable_bidirectional(sw);
706 } else if (sw->tmu.rate == TB_SWITCH_TMU_RATE_NORMAL) {
707 ret = __tb_switch_tmu_change_mode(sw);
711 sw->tmu.unidirectional = unidirectional;
714 * Host router port configurations are written as
715 * part of configurations for downstream port of the parent
716 * of the child node - see above.
717 * Here only the host router' rate configuration is written.
719 ret = tb_switch_tmu_rate_write(sw, sw->tmu.rate_request);
724 sw->tmu.rate = sw->tmu.rate_request;
726 tb_sw_dbg(sw, "TMU: mode set to: %s\n", tb_switch_tmu_mode_name(sw));
727 return tb_switch_tmu_set_time_disruption(sw, false);
731 * tb_switch_tmu_configure() - Configure the TMU rate and directionality
732 * @sw: Router whose mode to change
733 * @rate: Rate to configure Off/Normal/HiFi
734 * @unidirectional: If uni-directional (bi-directional otherwise)
736 * Selects the rate of the TMU and directionality (uni-directional or
737 * bi-directional). Must be called before tb_switch_tmu_enable().
739 void tb_switch_tmu_configure(struct tb_switch *sw,
740 enum tb_switch_tmu_rate rate, bool unidirectional)
742 sw->tmu.unidirectional_request = unidirectional;
743 sw->tmu.rate_request = rate;
746 static int tb_switch_tmu_config_enable(struct device *dev, void *rate)
748 if (tb_is_switch(dev)) {
749 struct tb_switch *sw = tb_to_switch(dev);
751 tb_switch_tmu_configure(sw, *(enum tb_switch_tmu_rate *)rate,
752 tb_switch_is_clx_enabled(sw, TB_CL1));
753 if (tb_switch_tmu_enable(sw))
754 tb_sw_dbg(sw, "fail switching TMU mode for 1st depth router\n");
761 * tb_switch_enable_tmu_1st_child - Configure and enable TMU for 1st chidren
762 * @sw: The router to configure and enable it's children TMU
763 * @rate: Rate of the TMU to configure the router's chidren to
765 * Configures and enables the TMU mode of 1st depth children of the specified
766 * router to the specified rate.
768 void tb_switch_enable_tmu_1st_child(struct tb_switch *sw,
769 enum tb_switch_tmu_rate rate)
771 device_for_each_child(&sw->dev, &rate,
772 tb_switch_tmu_config_enable);