1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
7 #include <debug_uart.h>
14 #include <linux/compiler.h>
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;
26 /* Convert a lower-case character to its ctrl-char equivalent */
27 #define CTL_CH(c) ((c) - 'a' + 1)
29 int serial_efi_setbrg(struct udevice *dev, int baudrate)
34 static int serial_efi_get_key(struct serial_efi_priv *priv)
40 ret = priv->con_in->read_key_stroke(priv->con_in, &priv->key);
41 if (ret == EFI_NOT_READY)
43 else if (ret != EFI_SUCCESS)
46 priv->have_key = true;
51 static int serial_efi_getc(struct udevice *dev)
53 struct serial_efi_priv *priv = dev_get_priv(dev);
54 char conv_scan[10] = {0, 'p', 'n', 'f', 'b', 'a', 'e', 0, 8};
57 ret = serial_efi_get_key(priv);
61 priv->have_key = false;
62 ch = priv->key.unicode_char;
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.
69 if (!ch && priv->key.scan_code < sizeof(conv_scan)) {
70 ch = conv_scan[priv->key.scan_code];
74 debug(" [%x %x %x] ", ch, priv->key.unicode_char, priv->key.scan_code);
79 static int serial_efi_putc(struct udevice *dev, const char ch)
81 struct serial_efi_priv *priv = dev_get_priv(dev);
87 ret = priv->con_out->output_string(priv->con_out, ucode);
94 static int serial_efi_pending(struct udevice *dev, bool input)
96 struct serial_efi_priv *priv = dev_get_priv(dev);
99 /* We assume that EFI will stall if its output buffer fills up */
103 ret = serial_efi_get_key(priv);
113 * There is nothing to init here since the EFI console is already running by
114 * the time we enter U-Boot.
116 static inline void _debug_uart_init(void)
120 static inline void _debug_uart_putc(int ch)
122 struct efi_system_table *sys_table = efi_get_sys_table();
127 sys_table->con_out->output_string(sys_table->con_out, ucode);
132 static int serial_efi_probe(struct udevice *dev)
134 struct efi_system_table *table = efi_get_sys_table();
135 struct serial_efi_priv *priv = dev_get_priv(dev);
137 priv->con_in = table->con_in;
138 priv->con_out = table->con_out;
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,
150 static const struct udevice_id serial_efi_ids[] = {
151 { .compatible = "efi,uart" },
155 U_BOOT_DRIVER(serial_efi) = {
156 .name = "serial_efi",
158 .of_match = serial_efi_ids,
159 .priv_auto = sizeof(struct serial_efi_priv),
160 .probe = serial_efi_probe,
161 .ops = &serial_efi_ops,