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>
12 * tb_lc_read_uuid() - Read switch UUID from link controller common register
13 * @sw: Switch whose UUID is read
14 * @uuid: UUID is placed here
16 int tb_lc_read_uuid(struct tb_switch *sw, u32 *uuid)
20 return tb_sw_read(sw, uuid, TB_CFG_SWITCH, sw->cap_lc + TB_LC_FUSE, 4);
23 static int read_lc_desc(struct tb_switch *sw, u32 *desc)
27 return tb_sw_read(sw, desc, TB_CFG_SWITCH, sw->cap_lc + TB_LC_DESC, 1);
30 static int find_port_lc_cap(struct tb_port *port)
32 struct tb_switch *sw = port->sw;
33 int start, phys, ret, size;
36 ret = read_lc_desc(sw, &desc);
40 /* Start of port LC registers */
41 start = (desc & TB_LC_DESC_SIZE_MASK) >> TB_LC_DESC_SIZE_SHIFT;
42 size = (desc & TB_LC_DESC_PORT_SIZE_MASK) >> TB_LC_DESC_PORT_SIZE_SHIFT;
43 phys = tb_phy_port_from_link(port->port);
45 return sw->cap_lc + start + phys * size;
48 static int tb_lc_set_port_configured(struct tb_port *port, bool configured)
50 bool upstream = tb_is_upstream_port(port);
51 struct tb_switch *sw = port->sw;
55 if (sw->generation < 2)
58 cap = find_port_lc_cap(port);
62 ret = tb_sw_read(sw, &ctrl, TB_CFG_SWITCH, cap + TB_LC_SX_CTRL, 1);
66 /* Resolve correct lane */
68 lane = TB_LC_SX_CTRL_L1C;
70 lane = TB_LC_SX_CTRL_L2C;
75 ctrl |= TB_LC_SX_CTRL_UPSTREAM;
79 ctrl &= ~TB_LC_SX_CTRL_UPSTREAM;
82 return tb_sw_write(sw, &ctrl, TB_CFG_SWITCH, cap + TB_LC_SX_CTRL, 1);
86 * tb_lc_configure_port() - Let LC know about configured port
87 * @port: Port that is set as configured
89 * Sets the port configured for power management purposes.
91 int tb_lc_configure_port(struct tb_port *port)
93 return tb_lc_set_port_configured(port, true);
97 * tb_lc_unconfigure_port() - Let LC know about unconfigured port
98 * @port: Port that is set as configured
100 * Sets the port unconfigured for power management purposes.
102 void tb_lc_unconfigure_port(struct tb_port *port)
104 tb_lc_set_port_configured(port, false);
107 static int tb_lc_set_xdomain_configured(struct tb_port *port, bool configure)
109 struct tb_switch *sw = port->sw;
113 if (sw->generation < 2)
116 cap = find_port_lc_cap(port);
120 ret = tb_sw_read(sw, &ctrl, TB_CFG_SWITCH, cap + TB_LC_SX_CTRL, 1);
124 /* Resolve correct lane */
126 lane = TB_LC_SX_CTRL_L1D;
128 lane = TB_LC_SX_CTRL_L2D;
135 return tb_sw_write(sw, &ctrl, TB_CFG_SWITCH, cap + TB_LC_SX_CTRL, 1);
139 * tb_lc_configure_xdomain() - Inform LC that the link is XDomain
140 * @port: Switch downstream port connected to another host
142 * Sets the lane configured for XDomain accordingly so that the LC knows
143 * about this. Returns %0 in success and negative errno in failure.
145 int tb_lc_configure_xdomain(struct tb_port *port)
147 return tb_lc_set_xdomain_configured(port, true);
151 * tb_lc_unconfigure_xdomain() - Unconfigure XDomain from port
152 * @port: Switch downstream port that was connected to another host
154 * Unsets the lane XDomain configuration.
156 void tb_lc_unconfigure_xdomain(struct tb_port *port)
158 tb_lc_set_xdomain_configured(port, false);
162 * tb_lc_start_lane_initialization() - Start lane initialization
163 * @port: Device router lane 0 adapter
165 * Starts lane initialization for @port after the router resumed from
166 * sleep. Should be called for those downstream lane adapters that were
167 * not connected (tb_lc_configure_port() was not called) before sleep.
169 * Returns %0 in success and negative errno in case of failure.
171 int tb_lc_start_lane_initialization(struct tb_port *port)
173 struct tb_switch *sw = port->sw;
180 if (sw->generation < 2)
183 cap = find_port_lc_cap(port);
187 ret = tb_sw_read(sw, &ctrl, TB_CFG_SWITCH, cap + TB_LC_SX_CTRL, 1);
191 ctrl |= TB_LC_SX_CTRL_SLI;
193 return tb_sw_write(sw, &ctrl, TB_CFG_SWITCH, cap + TB_LC_SX_CTRL, 1);
197 * tb_lc_is_clx_supported() - Check whether CLx is supported by the lane adapter
198 * @port: Lane adapter
200 * TB_LC_LINK_ATTR_CPS bit reflects if the link supports CLx including
201 * active cables (if connected on the link).
203 bool tb_lc_is_clx_supported(struct tb_port *port)
205 struct tb_switch *sw = port->sw;
209 cap = find_port_lc_cap(port);
213 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, cap + TB_LC_LINK_ATTR, 1);
217 return !!(val & TB_LC_LINK_ATTR_CPS);
221 * tb_lc_is_usb_plugged() - Is there USB device connected to port
222 * @port: Device router lane 0 adapter
224 * Returns true if the @port has USB type-C device connected.
226 bool tb_lc_is_usb_plugged(struct tb_port *port)
228 struct tb_switch *sw = port->sw;
232 if (sw->generation != 3)
235 cap = find_port_lc_cap(port);
239 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, cap + TB_LC_CS_42, 1);
243 return !!(val & TB_LC_CS_42_USB_PLUGGED);
247 * tb_lc_is_xhci_connected() - Is the internal xHCI connected
248 * @port: Device router lane 0 adapter
250 * Returns true if the internal xHCI has been connected to @port.
252 bool tb_lc_is_xhci_connected(struct tb_port *port)
254 struct tb_switch *sw = port->sw;
258 if (sw->generation != 3)
261 cap = find_port_lc_cap(port);
265 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, cap + TB_LC_LINK_REQ, 1);
269 return !!(val & TB_LC_LINK_REQ_XHCI_CONNECT);
272 static int __tb_lc_xhci_connect(struct tb_port *port, bool connect)
274 struct tb_switch *sw = port->sw;
278 if (sw->generation != 3)
281 cap = find_port_lc_cap(port);
285 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, cap + TB_LC_LINK_REQ, 1);
290 val |= TB_LC_LINK_REQ_XHCI_CONNECT;
292 val &= ~TB_LC_LINK_REQ_XHCI_CONNECT;
294 return tb_sw_write(sw, &val, TB_CFG_SWITCH, cap + TB_LC_LINK_REQ, 1);
298 * tb_lc_xhci_connect() - Connect internal xHCI
299 * @port: Device router lane 0 adapter
301 * Tells LC to connect the internal xHCI to @port. Returns %0 on success
302 * and negative errno in case of failure. Can be called for Thunderbolt 3
305 int tb_lc_xhci_connect(struct tb_port *port)
309 ret = __tb_lc_xhci_connect(port, true);
313 tb_port_dbg(port, "xHCI connected\n");
318 * tb_lc_xhci_disconnect() - Disconnect internal xHCI
319 * @port: Device router lane 0 adapter
321 * Tells LC to disconnect the internal xHCI from @port. Can be called
322 * for Thunderbolt 3 routers only.
324 void tb_lc_xhci_disconnect(struct tb_port *port)
326 __tb_lc_xhci_connect(port, false);
327 tb_port_dbg(port, "xHCI disconnected\n");
330 static int tb_lc_set_wake_one(struct tb_switch *sw, unsigned int offset,
337 * Enable wake on PCIe and USB4 (wake coming from another
340 ret = tb_sw_read(sw, &ctrl, TB_CFG_SWITCH,
341 offset + TB_LC_SX_CTRL, 1);
345 ctrl &= ~(TB_LC_SX_CTRL_WOC | TB_LC_SX_CTRL_WOD | TB_LC_SX_CTRL_WODPC |
346 TB_LC_SX_CTRL_WODPD | TB_LC_SX_CTRL_WOP | TB_LC_SX_CTRL_WOU4);
348 if (flags & TB_WAKE_ON_CONNECT)
349 ctrl |= TB_LC_SX_CTRL_WOC | TB_LC_SX_CTRL_WOD;
350 if (flags & TB_WAKE_ON_USB4)
351 ctrl |= TB_LC_SX_CTRL_WOU4;
352 if (flags & TB_WAKE_ON_PCIE)
353 ctrl |= TB_LC_SX_CTRL_WOP;
354 if (flags & TB_WAKE_ON_DP)
355 ctrl |= TB_LC_SX_CTRL_WODPC | TB_LC_SX_CTRL_WODPD;
357 return tb_sw_write(sw, &ctrl, TB_CFG_SWITCH, offset + TB_LC_SX_CTRL, 1);
361 * tb_lc_set_wake() - Enable/disable wake
362 * @sw: Switch whose wakes to configure
363 * @flags: Wakeup flags (%0 to disable)
365 * For each LC sets wake bits accordingly.
367 int tb_lc_set_wake(struct tb_switch *sw, unsigned int flags)
369 int start, size, nlc, ret, i;
372 if (sw->generation < 2)
378 ret = read_lc_desc(sw, &desc);
382 /* Figure out number of link controllers */
383 nlc = desc & TB_LC_DESC_NLC_MASK;
384 start = (desc & TB_LC_DESC_SIZE_MASK) >> TB_LC_DESC_SIZE_SHIFT;
385 size = (desc & TB_LC_DESC_PORT_SIZE_MASK) >> TB_LC_DESC_PORT_SIZE_SHIFT;
387 /* For each link controller set sleep bit */
388 for (i = 0; i < nlc; i++) {
389 unsigned int offset = sw->cap_lc + start + i * size;
391 ret = tb_lc_set_wake_one(sw, offset, flags);
400 * tb_lc_set_sleep() - Inform LC that the switch is going to sleep
401 * @sw: Switch to set sleep
403 * Let the switch link controllers know that the switch is going to
406 int tb_lc_set_sleep(struct tb_switch *sw)
408 int start, size, nlc, ret, i;
411 if (sw->generation < 2)
414 ret = read_lc_desc(sw, &desc);
418 /* Figure out number of link controllers */
419 nlc = desc & TB_LC_DESC_NLC_MASK;
420 start = (desc & TB_LC_DESC_SIZE_MASK) >> TB_LC_DESC_SIZE_SHIFT;
421 size = (desc & TB_LC_DESC_PORT_SIZE_MASK) >> TB_LC_DESC_PORT_SIZE_SHIFT;
423 /* For each link controller set sleep bit */
424 for (i = 0; i < nlc; i++) {
425 unsigned int offset = sw->cap_lc + start + i * size;
428 ret = tb_sw_read(sw, &ctrl, TB_CFG_SWITCH,
429 offset + TB_LC_SX_CTRL, 1);
433 ctrl |= TB_LC_SX_CTRL_SLP;
434 ret = tb_sw_write(sw, &ctrl, TB_CFG_SWITCH,
435 offset + TB_LC_SX_CTRL, 1);
444 * tb_lc_lane_bonding_possible() - Is lane bonding possible towards switch
445 * @sw: Switch to check
447 * Checks whether conditions for lane bonding from parent to @sw are
450 bool tb_lc_lane_bonding_possible(struct tb_switch *sw)
456 if (sw->generation < 2)
459 up = tb_upstream_port(sw);
460 cap = find_port_lc_cap(up);
464 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, cap + TB_LC_PORT_ATTR, 1);
468 return !!(val & TB_LC_PORT_ATTR_BE);
471 static int tb_lc_dp_sink_from_port(const struct tb_switch *sw,
474 struct tb_port *port;
476 /* The first DP IN port is sink 0 and second is sink 1 */
477 tb_switch_for_each_port(sw, port) {
478 if (tb_port_is_dpin(port))
485 static int tb_lc_dp_sink_available(struct tb_switch *sw, int sink)
490 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH,
491 sw->cap_lc + TB_LC_SNK_ALLOCATION, 1);
496 * Sink is available for CM/SW to use if the allocation valie is
500 alloc = val & TB_LC_SNK_ALLOCATION_SNK0_MASK;
501 if (!alloc || alloc == TB_LC_SNK_ALLOCATION_SNK0_CM)
504 alloc = (val & TB_LC_SNK_ALLOCATION_SNK1_MASK) >>
505 TB_LC_SNK_ALLOCATION_SNK1_SHIFT;
506 if (!alloc || alloc == TB_LC_SNK_ALLOCATION_SNK1_CM)
514 * tb_lc_dp_sink_query() - Is DP sink available for DP IN port
515 * @sw: Switch whose DP sink is queried
516 * @in: DP IN port to check
518 * Queries through LC SNK_ALLOCATION registers whether DP sink is available
519 * for the given DP IN port or not.
521 bool tb_lc_dp_sink_query(struct tb_switch *sw, struct tb_port *in)
526 * For older generations sink is always available as there is no
527 * allocation mechanism.
529 if (sw->generation < 3)
532 sink = tb_lc_dp_sink_from_port(sw, in);
536 return !tb_lc_dp_sink_available(sw, sink);
540 * tb_lc_dp_sink_alloc() - Allocate DP sink
541 * @sw: Switch whose DP sink is allocated
542 * @in: DP IN port the DP sink is allocated for
544 * Allocate DP sink for @in via LC SNK_ALLOCATION registers. If the
545 * resource is available and allocation is successful returns %0. In all
546 * other cases returs negative errno. In particular %-EBUSY is returned if
547 * the resource was not available.
549 int tb_lc_dp_sink_alloc(struct tb_switch *sw, struct tb_port *in)
554 if (sw->generation < 3)
557 sink = tb_lc_dp_sink_from_port(sw, in);
561 ret = tb_lc_dp_sink_available(sw, sink);
565 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH,
566 sw->cap_lc + TB_LC_SNK_ALLOCATION, 1);
571 val &= ~TB_LC_SNK_ALLOCATION_SNK0_MASK;
572 val |= TB_LC_SNK_ALLOCATION_SNK0_CM;
574 val &= ~TB_LC_SNK_ALLOCATION_SNK1_MASK;
575 val |= TB_LC_SNK_ALLOCATION_SNK1_CM <<
576 TB_LC_SNK_ALLOCATION_SNK1_SHIFT;
579 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH,
580 sw->cap_lc + TB_LC_SNK_ALLOCATION, 1);
585 tb_port_dbg(in, "sink %d allocated\n", sink);
590 * tb_lc_dp_sink_dealloc() - De-allocate DP sink
591 * @sw: Switch whose DP sink is de-allocated
592 * @in: DP IN port whose DP sink is de-allocated
594 * De-allocate DP sink from @in using LC SNK_ALLOCATION registers.
596 int tb_lc_dp_sink_dealloc(struct tb_switch *sw, struct tb_port *in)
601 if (sw->generation < 3)
604 sink = tb_lc_dp_sink_from_port(sw, in);
608 /* Needs to be owned by CM/SW */
609 ret = tb_lc_dp_sink_available(sw, sink);
613 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH,
614 sw->cap_lc + TB_LC_SNK_ALLOCATION, 1);
619 val &= ~TB_LC_SNK_ALLOCATION_SNK0_MASK;
621 val &= ~TB_LC_SNK_ALLOCATION_SNK1_MASK;
623 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH,
624 sw->cap_lc + TB_LC_SNK_ALLOCATION, 1);
628 tb_port_dbg(in, "sink %d de-allocated\n", sink);
633 * tb_lc_force_power() - Forces LC to be powered on
634 * @sw: Thunderbolt switch
636 * This is useful to let authentication cycle pass even without
637 * a Thunderbolt link present.
639 int tb_lc_force_power(struct tb_switch *sw)
643 return tb_sw_write(sw, &in, TB_CFG_SWITCH, TB_LC_POWER, 1);