soc/intel/pantherlake: Remove soc_info.[hc] interface
[coreboot2.git] / src / soc / intel / common / block / tcss / tcss.c
blob1ca51a2c272261c52a0afce87a53771f1c2db55c
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 #define __SIMPLE_DEVICE__
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>
21 #define WAIT_FOR_DISPLAYPORT_TIMEOUT_MS 1000
22 #define WAIT_FOR_DP_MODE_ENTRY_TIMEOUT_MS 1500
23 #define WAIT_FOR_HPD_TIMEOUT_MS 3000
25 static uint32_t tcss_make_conn_cmd(int u, int u3, int u2, int ufp, int hsl,
26 int sbu, int acc)
28 return TCSS_CD_FIELD(USAGE, u) |
29 TCSS_CD_FIELD(USB3, u3) |
30 TCSS_CD_FIELD(USB2, u2) |
31 TCSS_CD_FIELD(UFP, ufp) |
32 TCSS_CD_FIELD(HSL, hsl) |
33 TCSS_CD_FIELD(SBU, sbu) |
34 TCSS_CD_FIELD(ACC, acc);
37 static uint32_t tcss_make_alt_mode_cmd_buf_0(int u, int u3, int m)
39 return TCSS_ALT_FIELD(USAGE, u) |
40 TCSS_ALT_FIELD(USB3, u3) |
41 TCSS_ALT_FIELD(MODE, m);
44 static uint32_t tcss_make_alt_mode_cmd_buf_1(int p, int c, int ufp, int dp)
46 return TCSS_ALT_FIELD(POLARITY, p) |
47 TCSS_ALT_FIELD(CABLE, c) |
48 TCSS_ALT_FIELD(UFP, ufp) |
49 TCSS_ALT_FIELD(DP_MODE, dp);
52 static uint32_t tcss_make_safe_mode_cmd(int u, int u3)
54 return TCSS_CD_FIELD(USAGE, u) |
55 TCSS_CD_FIELD(USB3, u3);
59 static uint32_t tcss_make_hpd_mode_cmd(int u, int u3, int hpd_lvl, int hpd_irq)
61 return TCSS_HPD_FIELD(USAGE, u) |
62 TCSS_HPD_FIELD(USB3, u3) |
63 TCSS_HPD_FIELD(LVL, hpd_lvl) |
64 TCSS_HPD_FIELD(IRQ, hpd_irq);
67 static int send_pmc_req(int cmd_type, const struct pmc_ipc_buffer *req,
68 struct pmc_ipc_buffer *res, uint32_t size)
70 uint32_t cmd_reg;
71 uint32_t res_reg;
72 int tries = 2;
73 int r;
75 cmd_reg = pmc_make_ipc_cmd(PMC_IPC_USBC_CMD_ID, PMC_IPC_USBC_SUBCMD_ID,
76 size);
78 printk(BIOS_DEBUG, "Raw Buffer output 0 %08" PRIx32 "\n", req->buf[0]);
79 printk(BIOS_DEBUG, "Raw Buffer output 1 %08" PRIx32 "\n", req->buf[1]);
81 do {
82 r = pmc_send_ipc_cmd(cmd_reg, req, res);
83 if (r < 0) {
84 printk(BIOS_ERR, "pmc_send_ipc_cmd failed\n");
85 return -1;
88 res_reg = res->buf[0];
89 if (cmd_type == CONNECT_REQ) {
90 if (!TCSS_CONN_STATUS_HAS_FAILED(res_reg)) {
91 printk(BIOS_DEBUG, "pmc_send_ipc_cmd succeeded\n");
92 return 0;
95 if (TCSS_CONN_STATUS_IS_FATAL(res_reg)) {
96 printk(BIOS_ERR, "pmc_send_ipc_cmd status: fatal\n");
97 return -1;
99 } else {
100 if (!TCSS_STATUS_HAS_FAILED(res_reg)) {
101 printk(BIOS_DEBUG, "pmc_send_ipc_cmd succeeded\n");
102 return 0;
105 if (TCSS_STATUS_IS_FATAL(res_reg)) {
106 printk(BIOS_ERR, "pmc_send_ipc_cmd status: fatal\n");
107 return -1;
110 } while (--tries >= 0);
112 printk(BIOS_ERR, "pmc_send_ipc_cmd failed after retries\n");
113 return -1;
116 static int send_pmc_disconnect_request(int port, const struct tcss_port_map *port_map)
118 uint32_t cmd;
119 struct pmc_ipc_buffer req = { 0 };
120 struct pmc_ipc_buffer rsp;
122 cmd = tcss_make_conn_cmd(PMC_IPC_TCSS_DISC_REQ_RES, port_map->usb3_port,
123 port_map->usb2_port, 0, 0, 0, 0);
125 req.buf[0] = cmd;
127 printk(BIOS_DEBUG, "port C%d DISC req: usage %d usb3 %d usb2 %d\n",
128 port,
129 GET_TCSS_CD_FIELD(USAGE, cmd),
130 GET_TCSS_CD_FIELD(USB3, cmd),
131 GET_TCSS_CD_FIELD(USB2, cmd));
133 return send_pmc_req(CONNECT_REQ, &req, &rsp, PMC_IPC_DISC_REQ_SIZE);
136 static int send_pmc_connect_request(int port, const struct usbc_mux_info *mux_data,
137 const struct tcss_port_map *port_map)
139 uint32_t cmd;
140 struct pmc_ipc_buffer req = { 0 };
141 struct pmc_ipc_buffer rsp;
143 cmd = tcss_make_conn_cmd(
144 PMC_IPC_TCSS_CONN_REQ_RES,
145 port_map->usb3_port,
146 port_map->usb2_port,
147 mux_data->ufp,
148 mux_data->polarity,
149 mux_data->polarity,
150 mux_data->dbg_acc);
152 req.buf[0] = cmd;
154 printk(BIOS_DEBUG, "port C%d CONN req: usage %d usb3 %d usb2 %d "
155 "ufp %d ori_hsl %d ori_sbu %d dbg_acc %d\n",
156 port,
157 GET_TCSS_CD_FIELD(USAGE, cmd),
158 GET_TCSS_CD_FIELD(USB3, cmd),
159 GET_TCSS_CD_FIELD(USB2, cmd),
160 GET_TCSS_CD_FIELD(UFP, cmd),
161 GET_TCSS_CD_FIELD(HSL, cmd),
162 GET_TCSS_CD_FIELD(SBU, cmd),
163 GET_TCSS_CD_FIELD(ACC, cmd));
165 return send_pmc_req(CONNECT_REQ, &req, &rsp, PMC_IPC_CONN_REQ_SIZE);
168 static int send_pmc_safe_mode_request(int port, const struct usbc_mux_info *mux_data,
169 const struct tcss_port_map *port_map)
171 uint32_t cmd;
172 struct pmc_ipc_buffer req = { 0 };
173 struct pmc_ipc_buffer rsp;
175 cmd = tcss_make_safe_mode_cmd(PMC_IPC_TCSS_SAFE_MODE_REQ_RES, port_map->usb3_port);
177 req.buf[0] = cmd;
179 printk(BIOS_DEBUG, "port C%d SAFE req: usage %d usb3 %d\n",
180 port,
181 GET_TCSS_CD_FIELD(USAGE, cmd),
182 GET_TCSS_CD_FIELD(USB3, cmd));
184 return send_pmc_req(SAFE_REQ, &req, &rsp, PMC_IPC_SAFE_REQ_SIZE);
187 static int send_pmc_dp_hpd_request(int port, const struct usbc_mux_info *mux_data,
188 const struct tcss_port_map *port_map)
190 struct pmc_ipc_buffer req = { 0 };
191 struct pmc_ipc_buffer rsp;
192 uint32_t cmd;
194 cmd = tcss_make_hpd_mode_cmd(
195 PMC_IPC_TCSS_HPD_REQ_RES,
196 port_map->usb3_port,
197 mux_data->hpd_lvl,
198 mux_data->hpd_irq);
200 req.buf[0] = cmd;
202 return send_pmc_req(HPD_REQ, &req, &rsp, PMC_IPC_HPD_REQ_SIZE);
205 static uint8_t get_dp_mode(uint8_t dp_pin_mode)
207 switch (dp_pin_mode) {
208 case MODE_DP_PIN_A:
209 case MODE_DP_PIN_B:
210 case MODE_DP_PIN_C:
211 case MODE_DP_PIN_D:
212 case MODE_DP_PIN_E:
213 case MODE_DP_PIN_F:
214 return log2(dp_pin_mode) + 1;
215 default:
216 return 0;
220 static int send_pmc_dp_mode_request(int port, const struct usbc_mux_info *mux_data,
221 const struct tcss_port_map *port_map)
223 uint32_t cmd;
224 uint8_t dp_mode;
225 int ret;
227 struct pmc_ipc_buffer req = { 0 };
228 struct pmc_ipc_buffer rsp;
230 cmd = tcss_make_alt_mode_cmd_buf_0(
231 PMC_IPC_TCSS_ALTMODE_REQ_RES,
232 port_map->usb3_port,
233 PMC_IPC_DP_MODE);
235 req.buf[0] = cmd;
237 printk(BIOS_DEBUG, "port C%d ALT_1 req: usage %d usb3 %d dp_mode %d\n",
238 port,
239 GET_TCSS_ALT_FIELD(USAGE, cmd),
240 GET_TCSS_ALT_FIELD(USB3, cmd),
241 GET_TCSS_ALT_FIELD(MODE, cmd));
243 dp_mode = get_dp_mode(mux_data->dp_pin_mode);
244 cmd = tcss_make_alt_mode_cmd_buf_1(
245 mux_data->polarity,
246 mux_data->cable,
247 0, /* ufp is not supported in DP ALT Mode request */
248 dp_mode);
250 printk(BIOS_DEBUG, "port C%d ALT_2 req: polarity %d cable %d ufp %d "
251 "dp_mode %d\n",
252 port,
253 GET_TCSS_ALT_FIELD(POLARITY, cmd),
254 GET_TCSS_ALT_FIELD(CABLE, cmd),
255 GET_TCSS_ALT_FIELD(UFP, cmd),
256 GET_TCSS_ALT_FIELD(DP_MODE, cmd));
258 req.buf[1] = cmd;
260 ret = send_pmc_req(DP_REQ, &req, &rsp, PMC_IPC_ALT_REQ_SIZE);
261 if (ret)
262 return ret;
264 send_pmc_dp_hpd_request(port, mux_data, port_map);
265 return 0;
268 static void disconnect_tcss_devices(int port, const struct tcss_port_map *port_map)
270 int ret;
272 ret = send_pmc_disconnect_request(port, port_map);
273 if (ret)
274 printk(BIOS_ERR, "Failed to setup port:%d to initial state\n", port);
277 static void tcss_configure_dp_mode(const struct tcss_port_map *port_map, size_t num_ports)
279 int ret, port_bitmask;
280 size_t i;
281 const struct usbc_ops *ops;
282 struct usbc_mux_info mux_info;
283 const struct tcss_port_map *port_info;
285 if (!display_init_required())
286 return;
288 ops = usbc_get_ops();
289 if (ops == NULL)
290 return;
292 port_bitmask = ops->dp_ops.wait_for_connection(WAIT_FOR_DISPLAYPORT_TIMEOUT_MS);
293 if (!port_bitmask) /* No DP device is connected */
294 return;
296 for (i = 0; i < num_ports; i++) {
297 if (!(port_bitmask & BIT(i)))
298 continue;
300 ret = ops->dp_ops.enter_dp_mode(i);
301 if (ret < 0)
302 continue;
304 ret = ops->dp_ops.wait_for_dp_mode_entry(i, WAIT_FOR_DP_MODE_ENTRY_TIMEOUT_MS);
305 if (ret < 0)
306 continue;
308 ret = ops->dp_ops.wait_for_hpd(i, WAIT_FOR_HPD_TIMEOUT_MS);
309 if (ret < 0)
310 continue;
312 ret = ops->mux_ops.get_mux_info(i, &mux_info);
313 if (ret < 0)
314 continue;
316 port_info = &port_map[i];
318 ret = send_pmc_connect_request(i, &mux_info, port_info);
319 if (ret) {
320 printk(BIOS_ERR, "Port %zu connect request failed\n", i);
321 continue;
323 ret = send_pmc_safe_mode_request(i, &mux_info, port_info);
324 if (ret) {
325 printk(BIOS_ERR, "Port %zu safe mode request failed\n", i);
326 continue;
329 ret = send_pmc_dp_mode_request(i, &mux_info, port_info);
330 if (ret) {
331 printk(BIOS_ERR, "Port C%zu mux set failed with error %d\n", i, ret);
332 } else {
333 printk(BIOS_INFO, "Port C%zu is configured to DP mode!\n", i);
334 return;
339 static void tcss_configure_usb_mode(const struct tcss_port_map *port_map, size_t num_ports)
341 int ret;
342 size_t i;
343 const struct usbc_ops *ops;
344 struct usbc_mux_info mux_info;
345 const struct tcss_port_map *port_info;
347 ops = usbc_get_ops();
348 if (ops == NULL)
349 return;
351 for (i = 0; i < num_ports; i++) {
352 ret = ops->mux_ops.get_mux_info(i, &mux_info);
353 if ((ret < 0) || !mux_info.usb || (mux_info.dp && mux_info.hpd_lvl))
354 continue;
356 port_info = &port_map[i];
357 ret = send_pmc_connect_request(i, &mux_info, port_info);
358 if (ret) {
359 printk(BIOS_ERR, "Port %zu connect request failed\n", i);
360 continue;
365 static uint32_t calc_bias_ctrl_reg_value(gpio_t pad)
367 unsigned int vw_index, vw_bit;
368 const unsigned int cpu_pid = gpio_get_pad_cpu_portid(pad);
369 if (!gpio_get_vw_info(pad, &vw_index, &vw_bit) || !cpu_pid)
370 return 0;
372 return vw_index << BIAS_CTRL_VW_INDEX_SHIFT |
373 vw_bit << BIAS_CTRL_BIT_POS_SHIFT |
374 cpu_pid;
377 void tcss_configure_aux_bias_pads_regbar(
378 const struct typec_aux_bias_pads *pads)
380 for (size_t i = 0; i < MAX_TYPE_C_PORTS; i++) {
381 if (pads[i].pad_auxn_dc && pads[i].pad_auxp_dc) {
382 REGBAR32(PID_IOM, IOM_AUX_BIAS_CTRL_PULLUP_OFFSET(i)) =
383 calc_bias_ctrl_reg_value(pads[i].pad_auxp_dc);
384 REGBAR32(PID_IOM, IOM_AUX_BIAS_CTRL_PULLDOWN_OFFSET(i)) =
385 calc_bias_ctrl_reg_value(pads[i].pad_auxn_dc);
390 void ioe_tcss_configure_aux_bias_pads_sbi(
391 const struct typec_aux_bias_pads *pads)
393 for (size_t i = 0; i < MAX_TYPE_C_PORTS; i++) {
394 if (pads[i].pad_auxn_dc && pads[i].pad_auxp_dc) {
395 ioe_p2sb_sbi_write(PID_IOM, IOM_AUX_BIAS_CTRL_PULLUP_OFFSET(i),
396 calc_bias_ctrl_reg_value(pads[i].pad_auxp_dc));
397 ioe_p2sb_sbi_write(PID_IOM, IOM_AUX_BIAS_CTRL_PULLDOWN_OFFSET(i),
398 calc_bias_ctrl_reg_value(pads[i].pad_auxn_dc));
403 const struct tcss_port_map *tcss_get_port_info(size_t *num_ports)
405 static struct tcss_port_map port_map[MAX_TYPE_C_PORTS];
406 size_t active_ports = 0;
407 size_t port;
409 for (port = 0; port < MAX_TYPE_C_PORTS; port++) {
410 const struct device_path conn_path[] = {
411 {.type = DEVICE_PATH_PCI, .pci.devfn = PCH_DEVFN_PMC},
412 {.type = DEVICE_PATH_GENERIC, .generic.id = 0, .generic.subid = 0},
413 {.type = DEVICE_PATH_GENERIC, .generic.id = port},
415 const struct device *conn = find_dev_nested_path(pci_root_bus(), conn_path,
416 ARRAY_SIZE(conn_path));
417 unsigned int usb2_port, usb3_port;
419 if (!is_dev_enabled(conn))
420 continue;
422 if (CONFIG(DRIVERS_INTEL_PMC) &&
423 intel_pmc_mux_conn_get_ports(conn, &usb2_port, &usb3_port)) {
424 port_map[active_ports].usb2_port = usb2_port;
425 port_map[active_ports].usb3_port = usb3_port;
426 ++active_ports;
430 *num_ports = active_ports;
431 return port_map;
434 void tcss_configure(const struct typec_aux_bias_pads aux_bias_pads[MAX_TYPE_C_PORTS])
436 const struct tcss_port_map *port_map;
437 size_t num_ports;
438 size_t i;
440 port_map = tcss_get_port_info(&num_ports);
441 if ((port_map == NULL) || platform_is_resuming())
442 return;
444 if (CONFIG(TCSS_HAS_USBC_OPS))
445 for (i = 0; i < num_ports; i++)
446 disconnect_tcss_devices(i, &port_map[i]);
448 /* This should be performed before alternate modes are entered */
449 if (tcss_ops.configure_aux_bias_pads)
450 tcss_ops.configure_aux_bias_pads(aux_bias_pads);
452 if (CONFIG(ENABLE_TCSS_DISPLAY_DETECTION))
453 tcss_configure_dp_mode(port_map, num_ports);
455 if (CONFIG(ENABLE_TCSS_USB_DETECTION))
456 tcss_configure_usb_mode(port_map, num_ports);
459 bool tcss_valid_tbt_auth(void)
461 return REGBAR32(PID_IOM, IOM_CSME_IMR_TBT_STATUS) & TBT_VALID_AUTHENTICATION;