1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #define __SIMPLE_DEVICE__
4
5 #include <bootmode.h>
6 #include <console/console.h>
7 #include <device/pci.h>
8 #include <intelblocks/p2sb.h>
9 #include <intelblocks/pcr.h>
10 #include <intelblocks/pmc_ipc.h>
11 #include <intelblocks/systemagent.h>
12 #include <intelblocks/tcss.h>
13 #include <inttypes.h>
14 #include <lib.h>
15 #include <security/vboot/vboot_common.h>
16 #include <soc/pci_devs.h>
17 #include <soc/pcr_ids.h>
18 #include <soc/tcss.h>
19 #include <drivers/intel/pmc_mux/conn/chip.h>
20
21 #define BIAS_CTRL_VW_INDEX_SHIFT 16
22 #define BIAS_CTRL_BIT_POS_SHIFT 8
23 #define WAIT_FOR_DISPLAYPORT_TIMEOUT_MS 1000
24 #define WAIT_FOR_DP_MODE_ENTRY_TIMEOUT_MS 1500
25 #define WAIT_FOR_HPD_TIMEOUT_MS 3000
26
tcss_make_conn_cmd(int u,int u3,int u2,int ufp,int hsl,int sbu,int acc)27 static uint32_t tcss_make_conn_cmd(int u, int u3, int u2, int ufp, int hsl,
28 int sbu, int acc)
29 {
30 return TCSS_CD_FIELD(USAGE, u) |
31 TCSS_CD_FIELD(USB3, u3) |
32 TCSS_CD_FIELD(USB2, u2) |
33 TCSS_CD_FIELD(UFP, ufp) |
34 TCSS_CD_FIELD(HSL, hsl) |
35 TCSS_CD_FIELD(SBU, sbu) |
36 TCSS_CD_FIELD(ACC, acc);
37 }
38
tcss_make_alt_mode_cmd_buf_0(int u,int u3,int m)39 static uint32_t tcss_make_alt_mode_cmd_buf_0(int u, int u3, int m)
40 {
41 return TCSS_ALT_FIELD(USAGE, u) |
42 TCSS_ALT_FIELD(USB3, u3) |
43 TCSS_ALT_FIELD(MODE, m);
44 }
45
tcss_make_alt_mode_cmd_buf_1(int p,int c,int ufp,int dp)46 static uint32_t tcss_make_alt_mode_cmd_buf_1(int p, int c, int ufp, int dp)
47 {
48 return TCSS_ALT_FIELD(POLARITY, p) |
49 TCSS_ALT_FIELD(CABLE, c) |
50 TCSS_ALT_FIELD(UFP, ufp) |
51 TCSS_ALT_FIELD(DP_MODE, dp);
52 }
53
tcss_make_safe_mode_cmd(int u,int u3)54 static uint32_t tcss_make_safe_mode_cmd(int u, int u3)
55 {
56 return TCSS_CD_FIELD(USAGE, u) |
57 TCSS_CD_FIELD(USB3, u3);
58 }
59
60
tcss_make_hpd_mode_cmd(int u,int u3,int hpd_lvl,int hpd_irq)61 static uint32_t tcss_make_hpd_mode_cmd(int u, int u3, int hpd_lvl, int hpd_irq)
62 {
63 return TCSS_HPD_FIELD(USAGE, u) |
64 TCSS_HPD_FIELD(USB3, u3) |
65 TCSS_HPD_FIELD(LVL, hpd_lvl) |
66 TCSS_HPD_FIELD(IRQ, hpd_irq);
67 }
68
send_pmc_req(int cmd_type,const struct pmc_ipc_buffer * req,struct pmc_ipc_buffer * res,uint32_t size)69 static int send_pmc_req(int cmd_type, const struct pmc_ipc_buffer *req,
70 struct pmc_ipc_buffer *res, uint32_t size)
71 {
72 uint32_t cmd_reg;
73 uint32_t res_reg;
74 int tries = 2;
75 int r;
76
77 cmd_reg = pmc_make_ipc_cmd(PMC_IPC_USBC_CMD_ID, PMC_IPC_USBC_SUBCMD_ID,
78 size);
79
80 printk(BIOS_DEBUG, "Raw Buffer output 0 %08" PRIx32 "\n", req->buf[0]);
81 printk(BIOS_DEBUG, "Raw Buffer output 1 %08" PRIx32 "\n", req->buf[1]);
82
83 do {
84 r = pmc_send_ipc_cmd(cmd_reg, req, res);
85 if (r < 0) {
86 printk(BIOS_ERR, "pmc_send_ipc_cmd failed\n");
87 return -1;
88 }
89
90 res_reg = res->buf[0];
91 if (cmd_type == CONNECT_REQ) {
92 if (!TCSS_CONN_STATUS_HAS_FAILED(res_reg)) {
93 printk(BIOS_DEBUG, "pmc_send_ipc_cmd succeeded\n");
94 return 0;
95 }
96
97 if (TCSS_CONN_STATUS_IS_FATAL(res_reg)) {
98 printk(BIOS_ERR, "pmc_send_ipc_cmd status: fatal\n");
99 return -1;
100 }
101 } else {
102 if (!TCSS_STATUS_HAS_FAILED(res_reg)) {
103 printk(BIOS_DEBUG, "pmc_send_ipc_cmd succeeded\n");
104 return 0;
105 }
106
107 if (TCSS_STATUS_IS_FATAL(res_reg)) {
108 printk(BIOS_ERR, "pmc_send_ipc_cmd status: fatal\n");
109 return -1;
110 }
111 }
112 } while (--tries >= 0);
113
114 printk(BIOS_ERR, "pmc_send_ipc_cmd failed after retries\n");
115 return -1;
116 }
117
send_pmc_disconnect_request(int port,const struct tcss_port_map * port_map)118 static int send_pmc_disconnect_request(int port, const struct tcss_port_map *port_map)
119 {
120 uint32_t cmd;
121 struct pmc_ipc_buffer req = { 0 };
122 struct pmc_ipc_buffer rsp;
123
124 cmd = tcss_make_conn_cmd(PMC_IPC_TCSS_DISC_REQ_RES, port_map->usb3_port,
125 port_map->usb2_port, 0, 0, 0, 0);
126
127 req.buf[0] = cmd;
128
129 printk(BIOS_DEBUG, "port C%d DISC req: usage %d usb3 %d usb2 %d\n",
130 port,
131 GET_TCSS_CD_FIELD(USAGE, cmd),
132 GET_TCSS_CD_FIELD(USB3, cmd),
133 GET_TCSS_CD_FIELD(USB2, cmd));
134
135 return send_pmc_req(CONNECT_REQ, &req, &rsp, PMC_IPC_DISC_REQ_SIZE);
136 }
137
send_pmc_connect_request(int port,const struct usbc_mux_info * mux_data,const struct tcss_port_map * port_map)138 static int send_pmc_connect_request(int port, const struct usbc_mux_info *mux_data,
139 const struct tcss_port_map *port_map)
140 {
141 uint32_t cmd;
142 struct pmc_ipc_buffer req = { 0 };
143 struct pmc_ipc_buffer rsp;
144
145 cmd = tcss_make_conn_cmd(
146 PMC_IPC_TCSS_CONN_REQ_RES,
147 port_map->usb3_port,
148 port_map->usb2_port,
149 mux_data->ufp,
150 mux_data->polarity,
151 mux_data->polarity,
152 mux_data->dbg_acc);
153
154 req.buf[0] = cmd;
155
156 printk(BIOS_DEBUG, "port C%d CONN req: usage %d usb3 %d usb2 %d "
157 "ufp %d ori_hsl %d ori_sbu %d dbg_acc %d\n",
158 port,
159 GET_TCSS_CD_FIELD(USAGE, cmd),
160 GET_TCSS_CD_FIELD(USB3, cmd),
161 GET_TCSS_CD_FIELD(USB2, cmd),
162 GET_TCSS_CD_FIELD(UFP, cmd),
163 GET_TCSS_CD_FIELD(HSL, cmd),
164 GET_TCSS_CD_FIELD(SBU, cmd),
165 GET_TCSS_CD_FIELD(ACC, cmd));
166
167 return send_pmc_req(CONNECT_REQ, &req, &rsp, PMC_IPC_CONN_REQ_SIZE);
168 }
169
send_pmc_safe_mode_request(int port,const struct usbc_mux_info * mux_data,const struct tcss_port_map * port_map)170 static int send_pmc_safe_mode_request(int port, const struct usbc_mux_info *mux_data,
171 const struct tcss_port_map *port_map)
172 {
173 uint32_t cmd;
174 struct pmc_ipc_buffer req = { 0 };
175 struct pmc_ipc_buffer rsp;
176
177 cmd = tcss_make_safe_mode_cmd(PMC_IPC_TCSS_SAFE_MODE_REQ_RES, port_map->usb3_port);
178
179 req.buf[0] = cmd;
180
181 printk(BIOS_DEBUG, "port C%d SAFE req: usage %d usb3 %d\n",
182 port,
183 GET_TCSS_CD_FIELD(USAGE, cmd),
184 GET_TCSS_CD_FIELD(USB3, cmd));
185
186 return send_pmc_req(SAFE_REQ, &req, &rsp, PMC_IPC_SAFE_REQ_SIZE);
187 }
188
send_pmc_dp_hpd_request(int port,const struct usbc_mux_info * mux_data,const struct tcss_port_map * port_map)189 static int send_pmc_dp_hpd_request(int port, const struct usbc_mux_info *mux_data,
190 const struct tcss_port_map *port_map)
191 {
192 struct pmc_ipc_buffer req = { 0 };
193 struct pmc_ipc_buffer rsp;
194 uint32_t cmd;
195
196 cmd = tcss_make_hpd_mode_cmd(
197 PMC_IPC_TCSS_HPD_REQ_RES,
198 port_map->usb3_port,
199 mux_data->hpd_lvl,
200 mux_data->hpd_irq);
201
202 req.buf[0] = cmd;
203
204 return send_pmc_req(HPD_REQ, &req, &rsp, PMC_IPC_HPD_REQ_SIZE);
205 }
206
get_dp_mode(uint8_t dp_pin_mode)207 static uint8_t get_dp_mode(uint8_t dp_pin_mode)
208 {
209 switch (dp_pin_mode) {
210 case MODE_DP_PIN_A:
211 case MODE_DP_PIN_B:
212 case MODE_DP_PIN_C:
213 case MODE_DP_PIN_D:
214 case MODE_DP_PIN_E:
215 case MODE_DP_PIN_F:
216 return log2(dp_pin_mode) + 1;
217 default:
218 return 0;
219 }
220 }
221
send_pmc_dp_mode_request(int port,const struct usbc_mux_info * mux_data,const struct tcss_port_map * port_map)222 static int send_pmc_dp_mode_request(int port, const struct usbc_mux_info *mux_data,
223 const struct tcss_port_map *port_map)
224 {
225 uint32_t cmd;
226 uint8_t dp_mode;
227 int ret;
228
229 struct pmc_ipc_buffer req = { 0 };
230 struct pmc_ipc_buffer rsp;
231
232 cmd = tcss_make_alt_mode_cmd_buf_0(
233 PMC_IPC_TCSS_ALTMODE_REQ_RES,
234 port_map->usb3_port,
235 PMC_IPC_DP_MODE);
236
237 req.buf[0] = cmd;
238
239 printk(BIOS_DEBUG, "port C%d ALT_1 req: usage %d usb3 %d dp_mode %d\n",
240 port,
241 GET_TCSS_ALT_FIELD(USAGE, cmd),
242 GET_TCSS_ALT_FIELD(USB3, cmd),
243 GET_TCSS_ALT_FIELD(MODE, cmd));
244
245 dp_mode = get_dp_mode(mux_data->dp_pin_mode);
246 cmd = tcss_make_alt_mode_cmd_buf_1(
247 mux_data->polarity,
248 mux_data->cable,
249 0, /* ufp is not supported in DP ALT Mode request */
250 dp_mode);
251
252 printk(BIOS_DEBUG, "port C%d ALT_2 req: polarity %d cable %d ufp %d "
253 "dp_mode %d\n",
254 port,
255 GET_TCSS_ALT_FIELD(POLARITY, cmd),
256 GET_TCSS_ALT_FIELD(CABLE, cmd),
257 GET_TCSS_ALT_FIELD(UFP, cmd),
258 GET_TCSS_ALT_FIELD(DP_MODE, cmd));
259
260 req.buf[1] = cmd;
261
262 ret = send_pmc_req(DP_REQ, &req, &rsp, PMC_IPC_ALT_REQ_SIZE);
263 if (ret)
264 return ret;
265
266 send_pmc_dp_hpd_request(port, mux_data, port_map);
267 return 0;
268 }
269
disconnect_tcss_devices(int port,const struct tcss_port_map * port_map)270 static void disconnect_tcss_devices(int port, const struct tcss_port_map *port_map)
271 {
272 int ret;
273
274 ret = send_pmc_disconnect_request(port, port_map);
275 if (ret)
276 printk(BIOS_ERR, "Failed to setup port:%d to initial state\n", port);
277 }
278
tcss_configure_dp_mode(const struct tcss_port_map * port_map,size_t num_ports)279 static void tcss_configure_dp_mode(const struct tcss_port_map *port_map, size_t num_ports)
280 {
281 int ret, port_bitmask;
282 size_t i;
283 const struct usbc_ops *ops;
284 struct usbc_mux_info mux_info;
285 const struct tcss_port_map *port_info;
286
287 if (!display_init_required())
288 return;
289
290 ops = usbc_get_ops();
291 if (ops == NULL)
292 return;
293
294 port_bitmask = ops->dp_ops.wait_for_connection(WAIT_FOR_DISPLAYPORT_TIMEOUT_MS);
295 if (!port_bitmask) /* No DP device is connected */
296 return;
297
298 for (i = 0; i < num_ports; i++) {
299 if (!(port_bitmask & BIT(i)))
300 continue;
301
302 ret = ops->dp_ops.enter_dp_mode(i);
303 if (ret < 0)
304 continue;
305
306 ret = ops->dp_ops.wait_for_dp_mode_entry(i, WAIT_FOR_DP_MODE_ENTRY_TIMEOUT_MS);
307 if (ret < 0)
308 continue;
309
310 ret = ops->dp_ops.wait_for_hpd(i, WAIT_FOR_HPD_TIMEOUT_MS);
311 if (ret < 0)
312 continue;
313
314 ret = ops->mux_ops.get_mux_info(i, &mux_info);
315 if (ret < 0)
316 continue;
317
318 port_info = &port_map[i];
319
320 ret = send_pmc_connect_request(i, &mux_info, port_info);
321 if (ret) {
322 printk(BIOS_ERR, "Port %zu connect request failed\n", i);
323 continue;
324 }
325 ret = send_pmc_safe_mode_request(i, &mux_info, port_info);
326 if (ret) {
327 printk(BIOS_ERR, "Port %zu safe mode request failed\n", i);
328 continue;
329 }
330
331 ret = send_pmc_dp_mode_request(i, &mux_info, port_info);
332 if (ret) {
333 printk(BIOS_ERR, "Port C%zu mux set failed with error %d\n", i, ret);
334 } else {
335 printk(BIOS_INFO, "Port C%zu is configured to DP mode!\n", i);
336 return;
337 }
338 }
339 }
340
tcss_configure_usb_mode(const struct tcss_port_map * port_map,size_t num_ports)341 static void tcss_configure_usb_mode(const struct tcss_port_map *port_map, size_t num_ports)
342 {
343 int ret;
344 size_t i;
345 const struct usbc_ops *ops;
346 struct usbc_mux_info mux_info;
347 const struct tcss_port_map *port_info;
348
349 ops = usbc_get_ops();
350 if (ops == NULL)
351 return;
352
353 for (i = 0; i < num_ports; i++) {
354 ret = ops->mux_ops.get_mux_info(i, &mux_info);
355 if ((ret < 0) || !mux_info.usb || (mux_info.dp && mux_info.hpd_lvl))
356 continue;
357
358 port_info = &port_map[i];
359 ret = send_pmc_connect_request(i, &mux_info, port_info);
360 if (ret) {
361 printk(BIOS_ERR, "Port %zu connect request failed\n", i);
362 continue;
363 }
364 }
365 }
366
calc_bias_ctrl_reg_value(gpio_t pad)367 static uint32_t calc_bias_ctrl_reg_value(gpio_t pad)
368 {
369 unsigned int vw_index, vw_bit;
370 const unsigned int cpu_pid = gpio_get_pad_cpu_portid(pad);
371 if (!gpio_get_vw_info(pad, &vw_index, &vw_bit) || !cpu_pid)
372 return 0;
373
374 return vw_index << BIAS_CTRL_VW_INDEX_SHIFT |
375 vw_bit << BIAS_CTRL_BIT_POS_SHIFT |
376 cpu_pid;
377 }
378
tcss_configure_aux_bias_pads_regbar(const struct typec_aux_bias_pads * pads)379 void tcss_configure_aux_bias_pads_regbar(
380 const struct typec_aux_bias_pads *pads)
381 {
382 for (size_t i = 0; i < MAX_TYPE_C_PORTS; i++) {
383 if (pads[i].pad_auxn_dc && pads[i].pad_auxp_dc) {
384 REGBAR32(PID_IOM, IOM_AUX_BIAS_CTRL_PULLUP_OFFSET(i)) =
385 calc_bias_ctrl_reg_value(pads[i].pad_auxp_dc);
386 REGBAR32(PID_IOM, IOM_AUX_BIAS_CTRL_PULLDOWN_OFFSET(i)) =
387 calc_bias_ctrl_reg_value(pads[i].pad_auxn_dc);
388 }
389 }
390 }
391
ioe_tcss_configure_aux_bias_pads_sbi(const struct typec_aux_bias_pads * pads)392 void ioe_tcss_configure_aux_bias_pads_sbi(
393 const struct typec_aux_bias_pads *pads)
394 {
395 for (size_t i = 0; i < MAX_TYPE_C_PORTS; i++) {
396 if (pads[i].pad_auxn_dc && pads[i].pad_auxp_dc) {
397 ioe_p2sb_sbi_write(PID_IOM, IOM_AUX_BIAS_CTRL_PULLUP_OFFSET(i),
398 calc_bias_ctrl_reg_value(pads[i].pad_auxp_dc));
399 ioe_p2sb_sbi_write(PID_IOM, IOM_AUX_BIAS_CTRL_PULLDOWN_OFFSET(i),
400 calc_bias_ctrl_reg_value(pads[i].pad_auxn_dc));
401 }
402 }
403 }
404
tcss_get_port_info(size_t * num_ports)405 const struct tcss_port_map *tcss_get_port_info(size_t *num_ports)
406 {
407 static struct tcss_port_map port_map[MAX_TYPE_C_PORTS];
408 size_t active_ports = 0;
409 size_t port;
410
411 for (port = 0; port < MAX_TYPE_C_PORTS; port++) {
412 const struct device_path conn_path[] = {
413 {.type = DEVICE_PATH_PCI, .pci.devfn = PCH_DEVFN_PMC},
414 {.type = DEVICE_PATH_GENERIC, .generic.id = 0, .generic.subid = 0},
415 {.type = DEVICE_PATH_GENERIC, .generic.id = port},
416 };
417 const struct device *conn = find_dev_nested_path(pci_root_bus(), conn_path,
418 ARRAY_SIZE(conn_path));
419 unsigned int usb2_port, usb3_port;
420
421 if (!is_dev_enabled(conn))
422 continue;
423
424 if (CONFIG(DRIVERS_INTEL_PMC) &&
425 intel_pmc_mux_conn_get_ports(conn, &usb2_port, &usb3_port)) {
426 port_map[active_ports].usb2_port = usb2_port;
427 port_map[active_ports].usb3_port = usb3_port;
428 ++active_ports;
429 }
430 }
431
432 *num_ports = active_ports;
433 return port_map;
434 }
435
tcss_configure(const struct typec_aux_bias_pads aux_bias_pads[MAX_TYPE_C_PORTS])436 void tcss_configure(const struct typec_aux_bias_pads aux_bias_pads[MAX_TYPE_C_PORTS])
437 {
438 const struct tcss_port_map *port_map;
439 size_t num_ports;
440 size_t i;
441
442 port_map = tcss_get_port_info(&num_ports);
443 if ((port_map == NULL) || platform_is_resuming())
444 return;
445
446 if (CONFIG(TCSS_HAS_USBC_OPS))
447 for (i = 0; i < num_ports; i++)
448 disconnect_tcss_devices(i, &port_map[i]);
449
450 /* This should be performed before alternate modes are entered */
451 if (tcss_ops.configure_aux_bias_pads)
452 tcss_ops.configure_aux_bias_pads(aux_bias_pads);
453
454 if (CONFIG(ENABLE_TCSS_DISPLAY_DETECTION))
455 tcss_configure_dp_mode(port_map, num_ports);
456
457 if (CONFIG(ENABLE_TCSS_USB_DETECTION))
458 tcss_configure_usb_mode(port_map, num_ports);
459 }
460
tcss_valid_tbt_auth(void)461 bool tcss_valid_tbt_auth(void)
462 {
463 return REGBAR32(PID_IOM, IOM_CSME_IMR_TBT_STATUS) & TBT_VALID_AUTHENTICATION;
464 }
465