1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2015 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6 
7 #include <debug_uart.h>
8 #include <dm.h>
9 #include <efi.h>
10 #include <efi_api.h>
11 #include <errno.h>
12 #include <fdtdec.h>
13 #include <log.h>
14 #include <linux/compiler.h>
15 #include <asm/io.h>
16 #include <serial.h>
17 
18 /* Information about the efi console */
19 struct serial_efi_priv {
20 	struct efi_simple_text_input_protocol *con_in;
21 	struct efi_simple_text_output_protocol *con_out;
22 	struct efi_input_key key;
23 	bool have_key;
24 };
25 
26 /* Convert a lower-case character to its ctrl-char equivalent */
27 #define CTL_CH(c)		((c) - 'a' + 1)
28 
serial_efi_setbrg(struct udevice * dev,int baudrate)29 int serial_efi_setbrg(struct udevice *dev, int baudrate)
30 {
31 	return 0;
32 }
33 
serial_efi_get_key(struct serial_efi_priv * priv)34 static int serial_efi_get_key(struct serial_efi_priv *priv)
35 {
36 	int ret;
37 
38 	if (priv->have_key)
39 		return 0;
40 	ret = priv->con_in->read_key_stroke(priv->con_in, &priv->key);
41 	if (ret == EFI_NOT_READY)
42 		return -EAGAIN;
43 	else if (ret != EFI_SUCCESS)
44 		return -EIO;
45 
46 	priv->have_key = true;
47 
48 	return 0;
49 }
50 
serial_efi_getc(struct udevice * dev)51 static int serial_efi_getc(struct udevice *dev)
52 {
53 	struct serial_efi_priv *priv = dev_get_priv(dev);
54 	char conv_scan[10] = {0, 'p', 'n', 'f', 'b', 'a', 'e', 0, 8};
55 	int ret, ch;
56 
57 	ret = serial_efi_get_key(priv);
58 	if (ret)
59 		return ret;
60 
61 	priv->have_key = false;
62 	ch = priv->key.unicode_char;
63 
64 	/*
65 	 * Unicode char 8 (for backspace) is never returned. Instead we get a
66 	 * key scan code of 8. Handle this so that backspace works correctly
67 	 * in the U-Boot command line.
68 	 */
69 	if (!ch && priv->key.scan_code < sizeof(conv_scan)) {
70 		ch = conv_scan[priv->key.scan_code];
71 		if (ch >= 'a')
72 			ch -= 'a' - 1;
73 	}
74 	debug(" [%x %x %x] ", ch, priv->key.unicode_char, priv->key.scan_code);
75 
76 	return ch;
77 }
78 
serial_efi_putc(struct udevice * dev,const char ch)79 static int serial_efi_putc(struct udevice *dev, const char ch)
80 {
81 	struct serial_efi_priv *priv = dev_get_priv(dev);
82 	uint16_t ucode[2];
83 	int ret;
84 
85 	ucode[0] = ch;
86 	ucode[1] = '\0';
87 	ret = priv->con_out->output_string(priv->con_out, ucode);
88 	if (ret)
89 		return -EIO;
90 
91 	return 0;
92 }
93 
serial_efi_pending(struct udevice * dev,bool input)94 static int serial_efi_pending(struct udevice *dev, bool input)
95 {
96 	struct serial_efi_priv *priv = dev_get_priv(dev);
97 	int ret;
98 
99 	/* We assume that EFI will stall if its output buffer fills up */
100 	if (!input)
101 		return 0;
102 
103 	ret = serial_efi_get_key(priv);
104 	if (ret == -EAGAIN)
105 		return 0;
106 	else if (ret)
107 		return ret;
108 
109 	return 1;
110 }
111 
112 /*
113  * There is nothing to init here since the EFI console is already running by
114  * the time we enter U-Boot.
115  */
_debug_uart_init(void)116 static inline void _debug_uart_init(void)
117 {
118 }
119 
_debug_uart_putc(int ch)120 static inline void _debug_uart_putc(int ch)
121 {
122 	struct efi_system_table *sys_table = efi_get_sys_table();
123 	uint16_t ucode[2];
124 
125 	ucode[0] = ch;
126 	ucode[1] = '\0';
127 	sys_table->con_out->output_string(sys_table->con_out, ucode);
128 }
129 
130 DEBUG_UART_FUNCS
131 
serial_efi_probe(struct udevice * dev)132 static int serial_efi_probe(struct udevice *dev)
133 {
134 	struct efi_system_table *table = efi_get_sys_table();
135 	struct serial_efi_priv *priv = dev_get_priv(dev);
136 
137 	priv->con_in = table->con_in;
138 	priv->con_out = table->con_out;
139 
140 	return 0;
141 }
142 
143 static const struct dm_serial_ops serial_efi_ops = {
144 	.putc = serial_efi_putc,
145 	.getc = serial_efi_getc,
146 	.pending = serial_efi_pending,
147 	.setbrg = serial_efi_setbrg,
148 };
149 
150 static const struct udevice_id serial_efi_ids[] = {
151 	{ .compatible = "efi,uart" },
152 	{ }
153 };
154 
155 U_BOOT_DRIVER(serial_efi) = {
156 	.name	= "serial_efi",
157 	.id	= UCLASS_SERIAL,
158 	.of_match = serial_efi_ids,
159 	.priv_auto	= sizeof(struct serial_efi_priv),
160 	.probe = serial_efi_probe,
161 	.ops	= &serial_efi_ops,
162 };
163