1 /*
2  * Copyright (C) 2018-2022 Intel Corporation.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <types.h>
8 #include <rtl.h>
9 #include <pci.h>
10 #include <uart16550.h>
11 #include <dbg_cmd.h>
12 
13 #define MAX_PORT			0x10000  /* port 0 - 64K */
14 #define DEFAULT_UART_PORT	0x3F8
15 
16 #define MAX_CMD_LEN		64
17 
18 static struct uart_cmd {
19 	const char *const str;
20 	int type;
21 } cmd_list[] = {
22 	{ "uart=port@",	PIO },	/* uart=port@0x3F8 */
23 	{ "uart=bdf@",	PCI },	/* uart=bdf@0xc1 */
24 	{ "uart=mmio@",	MMIO },	/* uart=mmio@0xfe040000 */
25 	{ "uart=disabled", INVALID }
26 };
27 
handle_dbg_cmd(const char * cmd,int32_t len)28 bool handle_dbg_cmd(const char *cmd, int32_t len)
29 {
30 	uint32_t i;
31 	uint64_t data;
32 
33 	for (i = 0; i < ARRAY_SIZE(cmd_list); i++) {
34 		int32_t tmp = strnlen_s(cmd_list[i].str, MAX_CMD_LEN);
35 		int type = cmd_list[i].type;
36 
37 		/* cmd prefix should be same with one in cmd_list */
38 		if (len < tmp)
39 			continue;
40 
41 		if (strncmp(cmd_list[i].str, cmd, tmp) != 0)
42 			continue;
43 
44 		if (type == INVALID) {
45 			/* set uart disabled*/
46 			uart16550_set_property(false, type, 0UL);
47 		} else if (type == PIO) {
48 			data = strtoul_hex(cmd + tmp);
49 
50 			if (data > MAX_PORT) {
51 				data = DEFAULT_UART_PORT;
52 			}
53 
54 			uart16550_set_property(true, type, data);
55 		} else {
56 			data = strtoul_hex(cmd + tmp);
57 			uart16550_set_property(true, type, data);
58 		}
59 	}
60 
61 	return (i < ARRAY_SIZE(cmd_list));
62 }
63