spi: sprd: adi: Remove redundant address bits setting
[linux/fpc-iii.git] / drivers / thunderbolt / lc.c
blobae1e92611c3e0dfcf030203f6aed44e201d3b725
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Thunderbolt link controller support
5 * Copyright (C) 2019, Intel Corporation
6 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
7 */
9 #include "tb.h"
11 /**
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)
18 if (!sw->cap_lc)
19 return -EINVAL;
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)
25 if (!sw->cap_lc)
26 return -EINVAL;
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;
34 u32 desc;
36 ret = read_lc_desc(sw, &desc);
37 if (ret)
38 return ret;
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_configure_lane(struct tb_port *port, bool configure)
50 bool upstream = tb_is_upstream_port(port);
51 struct tb_switch *sw = port->sw;
52 u32 ctrl, lane;
53 int cap, ret;
55 if (sw->generation < 2)
56 return 0;
58 cap = find_port_lc_cap(port);
59 if (cap < 0)
60 return cap;
62 ret = tb_sw_read(sw, &ctrl, TB_CFG_SWITCH, cap + TB_LC_SX_CTRL, 1);
63 if (ret)
64 return ret;
66 /* Resolve correct lane */
67 if (port->port % 2)
68 lane = TB_LC_SX_CTRL_L1C;
69 else
70 lane = TB_LC_SX_CTRL_L2C;
72 if (configure) {
73 ctrl |= lane;
74 if (upstream)
75 ctrl |= TB_LC_SX_CTRL_UPSTREAM;
76 } else {
77 ctrl &= ~lane;
78 if (upstream)
79 ctrl &= ~TB_LC_SX_CTRL_UPSTREAM;
82 return tb_sw_write(sw, &ctrl, TB_CFG_SWITCH, cap + TB_LC_SX_CTRL, 1);
85 /**
86 * tb_lc_configure_link() - Let LC know about configured link
87 * @sw: Switch that is being added
89 * Informs LC of both parent switch and @sw that there is established
90 * link between the two.
92 int tb_lc_configure_link(struct tb_switch *sw)
94 struct tb_port *up, *down;
95 int ret;
97 if (!sw->config.enabled || !tb_route(sw))
98 return 0;
100 up = tb_upstream_port(sw);
101 down = tb_port_at(tb_route(sw), tb_to_switch(sw->dev.parent));
103 /* Configure parent link toward this switch */
104 ret = tb_lc_configure_lane(down, true);
105 if (ret)
106 return ret;
108 /* Configure upstream link from this switch to the parent */
109 ret = tb_lc_configure_lane(up, true);
110 if (ret)
111 tb_lc_configure_lane(down, false);
113 return ret;
117 * tb_lc_unconfigure_link() - Let LC know about unconfigured link
118 * @sw: Switch to unconfigure
120 * Informs LC of both parent switch and @sw that the link between the
121 * two does not exist anymore.
123 void tb_lc_unconfigure_link(struct tb_switch *sw)
125 struct tb_port *up, *down;
127 if (sw->is_unplugged || !sw->config.enabled || !tb_route(sw))
128 return;
130 up = tb_upstream_port(sw);
131 down = tb_port_at(tb_route(sw), tb_to_switch(sw->dev.parent));
133 tb_lc_configure_lane(up, false);
134 tb_lc_configure_lane(down, false);
138 * tb_lc_set_sleep() - Inform LC that the switch is going to sleep
139 * @sw: Switch to set sleep
141 * Let the switch link controllers know that the switch is going to
142 * sleep.
144 int tb_lc_set_sleep(struct tb_switch *sw)
146 int start, size, nlc, ret, i;
147 u32 desc;
149 if (sw->generation < 2)
150 return 0;
152 ret = read_lc_desc(sw, &desc);
153 if (ret)
154 return ret;
156 /* Figure out number of link controllers */
157 nlc = desc & TB_LC_DESC_NLC_MASK;
158 start = (desc & TB_LC_DESC_SIZE_MASK) >> TB_LC_DESC_SIZE_SHIFT;
159 size = (desc & TB_LC_DESC_PORT_SIZE_MASK) >> TB_LC_DESC_PORT_SIZE_SHIFT;
161 /* For each link controller set sleep bit */
162 for (i = 0; i < nlc; i++) {
163 unsigned int offset = sw->cap_lc + start + i * size;
164 u32 ctrl;
166 ret = tb_sw_read(sw, &ctrl, TB_CFG_SWITCH,
167 offset + TB_LC_SX_CTRL, 1);
168 if (ret)
169 return ret;
171 ctrl |= TB_LC_SX_CTRL_SLP;
172 ret = tb_sw_write(sw, &ctrl, TB_CFG_SWITCH,
173 offset + TB_LC_SX_CTRL, 1);
174 if (ret)
175 return ret;
178 return 0;