Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 5a991bd | 2015-08-04 12:33:40 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | 5a991bd | 2015-08-04 12:33:40 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
Simon Glass | 5a991bd | 2015-08-04 12:33:40 -0600 | [diff] [blame] | 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> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 5a991bd | 2015-08-04 12:33:40 -0600 | [diff] [blame] | 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 { |
Heinrich Schuchardt | 3dabffd | 2018-09-08 10:20:10 +0200 | [diff] [blame] | 20 | struct efi_simple_text_input_protocol *con_in; |
Simon Glass | 5a991bd | 2015-08-04 12:33:40 -0600 | [diff] [blame] | 21 | struct efi_simple_text_output_protocol *con_out; |
| 22 | struct efi_input_key key; |
| 23 | bool have_key; |
| 24 | }; |
| 25 | |
Simon Glass | f028a99 | 2021-12-29 11:57:37 -0700 | [diff] [blame] | 26 | /* Convert a lower-case character to its ctrl-char equivalent */ |
| 27 | #define CTL_CH(c) ((c) - 'a' + 1) |
| 28 | |
Simon Glass | 5a991bd | 2015-08-04 12:33:40 -0600 | [diff] [blame] | 29 | int serial_efi_setbrg(struct udevice *dev, int baudrate) |
| 30 | { |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 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 | |
| 51 | static int serial_efi_getc(struct udevice *dev) |
| 52 | { |
| 53 | struct serial_efi_priv *priv = dev_get_priv(dev); |
Simon Glass | f028a99 | 2021-12-29 11:57:37 -0700 | [diff] [blame] | 54 | char conv_scan[10] = {0, 'p', 'n', 'f', 'b', 'a', 'e', 0, 8}; |
Simon Glass | 5a991bd | 2015-08-04 12:33:40 -0600 | [diff] [blame] | 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 | */ |
Simon Glass | f028a99 | 2021-12-29 11:57:37 -0700 | [diff] [blame] | 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 | } |
Simon Glass | 5a991bd | 2015-08-04 12:33:40 -0600 | [diff] [blame] | 74 | debug(" [%x %x %x] ", ch, priv->key.unicode_char, priv->key.scan_code); |
| 75 | |
| 76 | return ch; |
| 77 | } |
| 78 | |
| 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 | |
| 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 | */ |
Simon Glass | 60517d7 | 2015-10-18 19:51:23 -0600 | [diff] [blame] | 116 | static inline void _debug_uart_init(void) |
Simon Glass | 5a991bd | 2015-08-04 12:33:40 -0600 | [diff] [blame] | 117 | { |
| 118 | } |
| 119 | |
| 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 | |
| 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, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 159 | .priv_auto = sizeof(struct serial_efi_priv), |
Simon Glass | 5a991bd | 2015-08-04 12:33:40 -0600 | [diff] [blame] | 160 | .probe = serial_efi_probe, |
| 161 | .ops = &serial_efi_ops, |
Simon Glass | 5a991bd | 2015-08-04 12:33:40 -0600 | [diff] [blame] | 162 | }; |