1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt link controller support
5 * Copyright (C) 2019, Intel Corporation
6 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
9 #include <linux/delay.h>
14 * tb_lc_read_uuid() - Read switch UUID from link controller common register
15 * @sw: Switch whose UUID is read
16 * @uuid: UUID is placed here
18 int tb_lc_read_uuid(struct tb_switch
*sw
, u32
*uuid
)
22 return tb_sw_read(sw
, uuid
, TB_CFG_SWITCH
, sw
->cap_lc
+ TB_LC_FUSE
, 4);
25 static int read_lc_desc(struct tb_switch
*sw
, u32
*desc
)
29 return tb_sw_read(sw
, desc
, TB_CFG_SWITCH
, sw
->cap_lc
+ TB_LC_DESC
, 1);
32 static int find_port_lc_cap(struct tb_port
*port
)
34 struct tb_switch
*sw
= port
->sw
;
35 int start
, phys
, ret
, size
;
38 ret
= read_lc_desc(sw
, &desc
);
42 /* Start of port LC registers */
43 start
= (desc
& TB_LC_DESC_SIZE_MASK
) >> TB_LC_DESC_SIZE_SHIFT
;
44 size
= (desc
& TB_LC_DESC_PORT_SIZE_MASK
) >> TB_LC_DESC_PORT_SIZE_SHIFT
;
45 phys
= tb_phy_port_from_link(port
->port
);
47 return sw
->cap_lc
+ start
+ phys
* size
;
51 * tb_lc_reset_port() - Trigger downstream port reset through LC
52 * @port: Port that is reset
54 * Triggers downstream port reset through link controller registers.
55 * Returns %0 in case of success negative errno otherwise. Only supports
56 * non-USB4 routers with link controller (that's Thunderbolt 2 and
59 int tb_lc_reset_port(struct tb_port
*port
)
61 struct tb_switch
*sw
= port
->sw
;
65 if (sw
->generation
< 2)
68 cap
= find_port_lc_cap(port
);
72 ret
= tb_sw_read(sw
, &mode
, TB_CFG_SWITCH
, cap
+ TB_LC_PORT_MODE
, 1);
76 mode
|= TB_LC_PORT_MODE_DPR
;
78 ret
= tb_sw_write(sw
, &mode
, TB_CFG_SWITCH
, cap
+ TB_LC_PORT_MODE
, 1);
84 ret
= tb_sw_read(sw
, &mode
, TB_CFG_SWITCH
, cap
+ TB_LC_PORT_MODE
, 1);
88 mode
&= ~TB_LC_PORT_MODE_DPR
;
90 return tb_sw_write(sw
, &mode
, TB_CFG_SWITCH
, cap
+ TB_LC_PORT_MODE
, 1);
93 static int tb_lc_set_port_configured(struct tb_port
*port
, bool configured
)
95 bool upstream
= tb_is_upstream_port(port
);
96 struct tb_switch
*sw
= port
->sw
;
100 if (sw
->generation
< 2)
103 cap
= find_port_lc_cap(port
);
107 ret
= tb_sw_read(sw
, &ctrl
, TB_CFG_SWITCH
, cap
+ TB_LC_SX_CTRL
, 1);
111 /* Resolve correct lane */
113 lane
= TB_LC_SX_CTRL_L1C
;
115 lane
= TB_LC_SX_CTRL_L2C
;
120 ctrl
|= TB_LC_SX_CTRL_UPSTREAM
;
124 ctrl
&= ~TB_LC_SX_CTRL_UPSTREAM
;
127 return tb_sw_write(sw
, &ctrl
, TB_CFG_SWITCH
, cap
+ TB_LC_SX_CTRL
, 1);
131 * tb_lc_configure_port() - Let LC know about configured port
132 * @port: Port that is set as configured
134 * Sets the port configured for power management purposes.
136 int tb_lc_configure_port(struct tb_port
*port
)
138 return tb_lc_set_port_configured(port
, true);
142 * tb_lc_unconfigure_port() - Let LC know about unconfigured port
143 * @port: Port that is set as configured
145 * Sets the port unconfigured for power management purposes.
147 void tb_lc_unconfigure_port(struct tb_port
*port
)
149 tb_lc_set_port_configured(port
, false);
152 static int tb_lc_set_xdomain_configured(struct tb_port
*port
, bool configure
)
154 struct tb_switch
*sw
= port
->sw
;
158 if (sw
->generation
< 2)
161 cap
= find_port_lc_cap(port
);
165 ret
= tb_sw_read(sw
, &ctrl
, TB_CFG_SWITCH
, cap
+ TB_LC_SX_CTRL
, 1);
169 /* Resolve correct lane */
171 lane
= TB_LC_SX_CTRL_L1D
;
173 lane
= TB_LC_SX_CTRL_L2D
;
180 return tb_sw_write(sw
, &ctrl
, TB_CFG_SWITCH
, cap
+ TB_LC_SX_CTRL
, 1);
184 * tb_lc_configure_xdomain() - Inform LC that the link is XDomain
185 * @port: Switch downstream port connected to another host
187 * Sets the lane configured for XDomain accordingly so that the LC knows
188 * about this. Returns %0 in success and negative errno in failure.
190 int tb_lc_configure_xdomain(struct tb_port
*port
)
192 return tb_lc_set_xdomain_configured(port
, true);
196 * tb_lc_unconfigure_xdomain() - Unconfigure XDomain from port
197 * @port: Switch downstream port that was connected to another host
199 * Unsets the lane XDomain configuration.
201 void tb_lc_unconfigure_xdomain(struct tb_port
*port
)
203 tb_lc_set_xdomain_configured(port
, false);
207 * tb_lc_start_lane_initialization() - Start lane initialization
208 * @port: Device router lane 0 adapter
210 * Starts lane initialization for @port after the router resumed from
211 * sleep. Should be called for those downstream lane adapters that were
212 * not connected (tb_lc_configure_port() was not called) before sleep.
214 * Returns %0 in success and negative errno in case of failure.
216 int tb_lc_start_lane_initialization(struct tb_port
*port
)
218 struct tb_switch
*sw
= port
->sw
;
225 if (sw
->generation
< 2)
228 cap
= find_port_lc_cap(port
);
232 ret
= tb_sw_read(sw
, &ctrl
, TB_CFG_SWITCH
, cap
+ TB_LC_SX_CTRL
, 1);
236 ctrl
|= TB_LC_SX_CTRL_SLI
;
238 return tb_sw_write(sw
, &ctrl
, TB_CFG_SWITCH
, cap
+ TB_LC_SX_CTRL
, 1);
242 * tb_lc_is_clx_supported() - Check whether CLx is supported by the lane adapter
243 * @port: Lane adapter
245 * TB_LC_LINK_ATTR_CPS bit reflects if the link supports CLx including
246 * active cables (if connected on the link).
248 bool tb_lc_is_clx_supported(struct tb_port
*port
)
250 struct tb_switch
*sw
= port
->sw
;
254 cap
= find_port_lc_cap(port
);
258 ret
= tb_sw_read(sw
, &val
, TB_CFG_SWITCH
, cap
+ TB_LC_LINK_ATTR
, 1);
262 return !!(val
& TB_LC_LINK_ATTR_CPS
);
266 * tb_lc_is_usb_plugged() - Is there USB device connected to port
267 * @port: Device router lane 0 adapter
269 * Returns true if the @port has USB type-C device connected.
271 bool tb_lc_is_usb_plugged(struct tb_port
*port
)
273 struct tb_switch
*sw
= port
->sw
;
277 if (sw
->generation
!= 3)
280 cap
= find_port_lc_cap(port
);
284 ret
= tb_sw_read(sw
, &val
, TB_CFG_SWITCH
, cap
+ TB_LC_CS_42
, 1);
288 return !!(val
& TB_LC_CS_42_USB_PLUGGED
);
292 * tb_lc_is_xhci_connected() - Is the internal xHCI connected
293 * @port: Device router lane 0 adapter
295 * Returns true if the internal xHCI has been connected to @port.
297 bool tb_lc_is_xhci_connected(struct tb_port
*port
)
299 struct tb_switch
*sw
= port
->sw
;
303 if (sw
->generation
!= 3)
306 cap
= find_port_lc_cap(port
);
310 ret
= tb_sw_read(sw
, &val
, TB_CFG_SWITCH
, cap
+ TB_LC_LINK_REQ
, 1);
314 return !!(val
& TB_LC_LINK_REQ_XHCI_CONNECT
);
317 static int __tb_lc_xhci_connect(struct tb_port
*port
, bool connect
)
319 struct tb_switch
*sw
= port
->sw
;
323 if (sw
->generation
!= 3)
326 cap
= find_port_lc_cap(port
);
330 ret
= tb_sw_read(sw
, &val
, TB_CFG_SWITCH
, cap
+ TB_LC_LINK_REQ
, 1);
335 val
|= TB_LC_LINK_REQ_XHCI_CONNECT
;
337 val
&= ~TB_LC_LINK_REQ_XHCI_CONNECT
;
339 return tb_sw_write(sw
, &val
, TB_CFG_SWITCH
, cap
+ TB_LC_LINK_REQ
, 1);
343 * tb_lc_xhci_connect() - Connect internal xHCI
344 * @port: Device router lane 0 adapter
346 * Tells LC to connect the internal xHCI to @port. Returns %0 on success
347 * and negative errno in case of failure. Can be called for Thunderbolt 3
350 int tb_lc_xhci_connect(struct tb_port
*port
)
354 ret
= __tb_lc_xhci_connect(port
, true);
358 tb_port_dbg(port
, "xHCI connected\n");
363 * tb_lc_xhci_disconnect() - Disconnect internal xHCI
364 * @port: Device router lane 0 adapter
366 * Tells LC to disconnect the internal xHCI from @port. Can be called
367 * for Thunderbolt 3 routers only.
369 void tb_lc_xhci_disconnect(struct tb_port
*port
)
371 __tb_lc_xhci_connect(port
, false);
372 tb_port_dbg(port
, "xHCI disconnected\n");
375 static int tb_lc_set_wake_one(struct tb_switch
*sw
, unsigned int offset
,
382 * Enable wake on PCIe and USB4 (wake coming from another
385 ret
= tb_sw_read(sw
, &ctrl
, TB_CFG_SWITCH
,
386 offset
+ TB_LC_SX_CTRL
, 1);
390 ctrl
&= ~(TB_LC_SX_CTRL_WOC
| TB_LC_SX_CTRL_WOD
| TB_LC_SX_CTRL_WODPC
|
391 TB_LC_SX_CTRL_WODPD
| TB_LC_SX_CTRL_WOP
| TB_LC_SX_CTRL_WOU4
);
393 if (flags
& TB_WAKE_ON_CONNECT
)
394 ctrl
|= TB_LC_SX_CTRL_WOC
| TB_LC_SX_CTRL_WOD
;
395 if (flags
& TB_WAKE_ON_USB4
)
396 ctrl
|= TB_LC_SX_CTRL_WOU4
;
397 if (flags
& TB_WAKE_ON_PCIE
)
398 ctrl
|= TB_LC_SX_CTRL_WOP
;
399 if (flags
& TB_WAKE_ON_DP
)
400 ctrl
|= TB_LC_SX_CTRL_WODPC
| TB_LC_SX_CTRL_WODPD
;
402 return tb_sw_write(sw
, &ctrl
, TB_CFG_SWITCH
, offset
+ TB_LC_SX_CTRL
, 1);
406 * tb_lc_set_wake() - Enable/disable wake
407 * @sw: Switch whose wakes to configure
408 * @flags: Wakeup flags (%0 to disable)
410 * For each LC sets wake bits accordingly.
412 int tb_lc_set_wake(struct tb_switch
*sw
, unsigned int flags
)
414 int start
, size
, nlc
, ret
, i
;
417 if (sw
->generation
< 2)
423 ret
= read_lc_desc(sw
, &desc
);
427 /* Figure out number of link controllers */
428 nlc
= desc
& TB_LC_DESC_NLC_MASK
;
429 start
= (desc
& TB_LC_DESC_SIZE_MASK
) >> TB_LC_DESC_SIZE_SHIFT
;
430 size
= (desc
& TB_LC_DESC_PORT_SIZE_MASK
) >> TB_LC_DESC_PORT_SIZE_SHIFT
;
432 /* For each link controller set sleep bit */
433 for (i
= 0; i
< nlc
; i
++) {
434 unsigned int offset
= sw
->cap_lc
+ start
+ i
* size
;
436 ret
= tb_lc_set_wake_one(sw
, offset
, flags
);
445 * tb_lc_set_sleep() - Inform LC that the switch is going to sleep
446 * @sw: Switch to set sleep
448 * Let the switch link controllers know that the switch is going to
451 int tb_lc_set_sleep(struct tb_switch
*sw
)
453 int start
, size
, nlc
, ret
, i
;
456 if (sw
->generation
< 2)
459 ret
= read_lc_desc(sw
, &desc
);
463 /* Figure out number of link controllers */
464 nlc
= desc
& TB_LC_DESC_NLC_MASK
;
465 start
= (desc
& TB_LC_DESC_SIZE_MASK
) >> TB_LC_DESC_SIZE_SHIFT
;
466 size
= (desc
& TB_LC_DESC_PORT_SIZE_MASK
) >> TB_LC_DESC_PORT_SIZE_SHIFT
;
468 /* For each link controller set sleep bit */
469 for (i
= 0; i
< nlc
; i
++) {
470 unsigned int offset
= sw
->cap_lc
+ start
+ i
* size
;
473 ret
= tb_sw_read(sw
, &ctrl
, TB_CFG_SWITCH
,
474 offset
+ TB_LC_SX_CTRL
, 1);
478 ctrl
|= TB_LC_SX_CTRL_SLP
;
479 ret
= tb_sw_write(sw
, &ctrl
, TB_CFG_SWITCH
,
480 offset
+ TB_LC_SX_CTRL
, 1);
489 * tb_lc_lane_bonding_possible() - Is lane bonding possible towards switch
490 * @sw: Switch to check
492 * Checks whether conditions for lane bonding from parent to @sw are
495 bool tb_lc_lane_bonding_possible(struct tb_switch
*sw
)
501 if (sw
->generation
< 2)
504 up
= tb_upstream_port(sw
);
505 cap
= find_port_lc_cap(up
);
509 ret
= tb_sw_read(sw
, &val
, TB_CFG_SWITCH
, cap
+ TB_LC_PORT_ATTR
, 1);
513 return !!(val
& TB_LC_PORT_ATTR_BE
);
516 static int tb_lc_dp_sink_from_port(const struct tb_switch
*sw
,
519 struct tb_port
*port
;
521 /* The first DP IN port is sink 0 and second is sink 1 */
522 tb_switch_for_each_port(sw
, port
) {
523 if (tb_port_is_dpin(port
))
530 static int tb_lc_dp_sink_available(struct tb_switch
*sw
, int sink
)
535 ret
= tb_sw_read(sw
, &val
, TB_CFG_SWITCH
,
536 sw
->cap_lc
+ TB_LC_SNK_ALLOCATION
, 1);
541 * Sink is available for CM/SW to use if the allocation valie is
545 alloc
= val
& TB_LC_SNK_ALLOCATION_SNK0_MASK
;
546 if (!alloc
|| alloc
== TB_LC_SNK_ALLOCATION_SNK0_CM
)
549 alloc
= (val
& TB_LC_SNK_ALLOCATION_SNK1_MASK
) >>
550 TB_LC_SNK_ALLOCATION_SNK1_SHIFT
;
551 if (!alloc
|| alloc
== TB_LC_SNK_ALLOCATION_SNK1_CM
)
559 * tb_lc_dp_sink_query() - Is DP sink available for DP IN port
560 * @sw: Switch whose DP sink is queried
561 * @in: DP IN port to check
563 * Queries through LC SNK_ALLOCATION registers whether DP sink is available
564 * for the given DP IN port or not.
566 bool tb_lc_dp_sink_query(struct tb_switch
*sw
, struct tb_port
*in
)
571 * For older generations sink is always available as there is no
572 * allocation mechanism.
574 if (sw
->generation
< 3)
577 sink
= tb_lc_dp_sink_from_port(sw
, in
);
581 return !tb_lc_dp_sink_available(sw
, sink
);
585 * tb_lc_dp_sink_alloc() - Allocate DP sink
586 * @sw: Switch whose DP sink is allocated
587 * @in: DP IN port the DP sink is allocated for
589 * Allocate DP sink for @in via LC SNK_ALLOCATION registers. If the
590 * resource is available and allocation is successful returns %0. In all
591 * other cases returs negative errno. In particular %-EBUSY is returned if
592 * the resource was not available.
594 int tb_lc_dp_sink_alloc(struct tb_switch
*sw
, struct tb_port
*in
)
599 if (sw
->generation
< 3)
602 sink
= tb_lc_dp_sink_from_port(sw
, in
);
606 ret
= tb_lc_dp_sink_available(sw
, sink
);
610 ret
= tb_sw_read(sw
, &val
, TB_CFG_SWITCH
,
611 sw
->cap_lc
+ TB_LC_SNK_ALLOCATION
, 1);
616 val
&= ~TB_LC_SNK_ALLOCATION_SNK0_MASK
;
617 val
|= TB_LC_SNK_ALLOCATION_SNK0_CM
;
619 val
&= ~TB_LC_SNK_ALLOCATION_SNK1_MASK
;
620 val
|= TB_LC_SNK_ALLOCATION_SNK1_CM
<<
621 TB_LC_SNK_ALLOCATION_SNK1_SHIFT
;
624 ret
= tb_sw_write(sw
, &val
, TB_CFG_SWITCH
,
625 sw
->cap_lc
+ TB_LC_SNK_ALLOCATION
, 1);
630 tb_port_dbg(in
, "sink %d allocated\n", sink
);
635 * tb_lc_dp_sink_dealloc() - De-allocate DP sink
636 * @sw: Switch whose DP sink is de-allocated
637 * @in: DP IN port whose DP sink is de-allocated
639 * De-allocate DP sink from @in using LC SNK_ALLOCATION registers.
641 int tb_lc_dp_sink_dealloc(struct tb_switch
*sw
, struct tb_port
*in
)
646 if (sw
->generation
< 3)
649 sink
= tb_lc_dp_sink_from_port(sw
, in
);
653 /* Needs to be owned by CM/SW */
654 ret
= tb_lc_dp_sink_available(sw
, sink
);
658 ret
= tb_sw_read(sw
, &val
, TB_CFG_SWITCH
,
659 sw
->cap_lc
+ TB_LC_SNK_ALLOCATION
, 1);
664 val
&= ~TB_LC_SNK_ALLOCATION_SNK0_MASK
;
666 val
&= ~TB_LC_SNK_ALLOCATION_SNK1_MASK
;
668 ret
= tb_sw_write(sw
, &val
, TB_CFG_SWITCH
,
669 sw
->cap_lc
+ TB_LC_SNK_ALLOCATION
, 1);
673 tb_port_dbg(in
, "sink %d de-allocated\n", sink
);
678 * tb_lc_force_power() - Forces LC to be powered on
679 * @sw: Thunderbolt switch
681 * This is useful to let authentication cycle pass even without
682 * a Thunderbolt link present.
684 int tb_lc_force_power(struct tb_switch
*sw
)
688 return tb_sw_write(sw
, &in
, TB_CFG_SWITCH
, TB_LC_POWER
, 1);